1 //===-- NativeThreadLinux.cpp --------------------------------- -*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "NativeThreadLinux.h" 11 12 #include <signal.h> 13 14 #include "NativeProcessLinux.h" 15 #include "NativeRegisterContextLinux_x86_64.h" 16 17 #include "lldb/Core/Log.h" 18 #include "lldb/Core/State.h" 19 #include "lldb/Host/Host.h" 20 #include "lldb/Host/HostInfo.h" 21 #include "lldb/Host/HostNativeThread.h" 22 #include "lldb/lldb-enumerations.h" 23 #include "lldb/lldb-private-log.h" 24 25 #include "llvm/ADT/SmallString.h" 26 27 #include "Plugins/Process/POSIX/CrashReason.h" 28 29 #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h" 30 #include "Plugins/Process/Utility/RegisterContextLinux_i386.h" 31 #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h" 32 #include "Plugins/Process/Utility/RegisterInfoInterface.h" 33 34 using namespace lldb; 35 using namespace lldb_private; 36 37 namespace 38 { 39 void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) 40 { 41 switch (stop_info.reason) 42 { 43 case eStopReasonSignal: 44 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 45 return; 46 case eStopReasonException: 47 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); 48 return; 49 case eStopReasonExec: 50 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 51 return; 52 default: 53 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); 54 } 55 } 56 } 57 58 NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : 59 NativeThreadProtocol (process, tid), 60 m_state (StateType::eStateInvalid), 61 m_stop_info (), 62 m_reg_context_sp (), 63 m_stop_description () 64 { 65 } 66 67 std::string 68 NativeThreadLinux::GetName() 69 { 70 NativeProcessProtocolSP process_sp = m_process_wp.lock (); 71 if (!process_sp) 72 return "<unknown: no process>"; 73 74 // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); 75 llvm::SmallString<32> thread_name; 76 HostNativeThread::GetName(GetID(), thread_name); 77 return thread_name.c_str(); 78 } 79 80 lldb::StateType 81 NativeThreadLinux::GetState () 82 { 83 return m_state; 84 } 85 86 87 bool 88 NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description) 89 { 90 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 91 92 description.clear(); 93 94 switch (m_state) 95 { 96 case eStateStopped: 97 case eStateCrashed: 98 case eStateExited: 99 case eStateSuspended: 100 case eStateUnloaded: 101 if (log) 102 LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); 103 stop_info = m_stop_info; 104 if (m_stop_info.reason == StopReason::eStopReasonException) 105 description = m_stop_description; 106 if (log) 107 LogThreadStopInfo (*log, stop_info, "returned stop_info:"); 108 109 return true; 110 111 case eStateInvalid: 112 case eStateConnected: 113 case eStateAttaching: 114 case eStateLaunching: 115 case eStateRunning: 116 case eStateStepping: 117 case eStateDetached: 118 if (log) 119 { 120 log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", 121 __FUNCTION__, GetID (), StateAsCString (m_state)); 122 } 123 return false; 124 } 125 llvm_unreachable("unhandled StateType!"); 126 } 127 128 lldb_private::NativeRegisterContextSP 129 NativeThreadLinux::GetRegisterContext () 130 { 131 // Return the register context if we already created it. 132 if (m_reg_context_sp) 133 return m_reg_context_sp; 134 135 // First select the appropriate RegisterInfoInterface. 136 RegisterInfoInterface *reg_interface = nullptr; 137 NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 138 if (!m_process_sp) 139 return NativeRegisterContextSP (); 140 141 ArchSpec target_arch; 142 if (!m_process_sp->GetArchitecture (target_arch)) 143 return NativeRegisterContextSP (); 144 145 switch (target_arch.GetTriple().getOS()) 146 { 147 case llvm::Triple::Linux: 148 switch (target_arch.GetMachine()) 149 { 150 case llvm::Triple::aarch64: 151 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host"); 152 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch)); 153 break; 154 case llvm::Triple::x86: 155 case llvm::Triple::x86_64: 156 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) 157 { 158 // 32-bit hosts run with a RegisterContextLinux_i386 context. 159 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch)); 160 } 161 else 162 { 163 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && 164 "Register setting path assumes this is a 64-bit host"); 165 // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context. 166 reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch)); 167 } 168 break; 169 default: 170 break; 171 } 172 break; 173 default: 174 break; 175 } 176 177 assert(reg_interface && "OS or CPU not supported!"); 178 if (!reg_interface) 179 return NativeRegisterContextSP (); 180 181 // Now create the register context. 182 switch (target_arch.GetMachine()) 183 { 184 #if 0 185 case llvm::Triple::mips64: 186 { 187 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface); 188 m_posix_thread = reg_ctx; 189 m_reg_context_sp.reset(reg_ctx); 190 break; 191 } 192 #endif 193 #if 0 194 case llvm::Triple::x86: 195 #endif 196 case llvm::Triple::x86_64: 197 { 198 const uint32_t concrete_frame_idx = 0; 199 m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface)); 200 break; 201 } 202 default: 203 break; 204 } 205 206 return m_reg_context_sp; 207 } 208 209 Error 210 NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 211 { 212 // TODO implement 213 return Error ("not implemented"); 214 } 215 216 Error 217 NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) 218 { 219 // TODO implement 220 return Error ("not implemented"); 221 } 222 223 void 224 NativeThreadLinux::SetLaunching () 225 { 226 const StateType new_state = StateType::eStateLaunching; 227 MaybeLogStateChange (new_state); 228 m_state = new_state; 229 230 // Also mark it as stopped since launching temporarily stops the newly created thread 231 // in the ptrace machinery. 232 m_stop_info.reason = StopReason::eStopReasonSignal; 233 m_stop_info.details.signal.signo = SIGSTOP; 234 } 235 236 237 void 238 NativeThreadLinux::SetRunning () 239 { 240 const StateType new_state = StateType::eStateRunning; 241 MaybeLogStateChange (new_state); 242 m_state = new_state; 243 244 m_stop_info.reason = StopReason::eStopReasonNone; 245 m_stop_description.clear(); 246 } 247 248 void 249 NativeThreadLinux::SetStepping () 250 { 251 const StateType new_state = StateType::eStateStepping; 252 MaybeLogStateChange (new_state); 253 m_state = new_state; 254 255 m_stop_info.reason = StopReason::eStopReasonNone; 256 } 257 258 void 259 NativeThreadLinux::SetStoppedBySignal (uint32_t signo) 260 { 261 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 262 if (log) 263 log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo); 264 265 const StateType new_state = StateType::eStateStopped; 266 MaybeLogStateChange (new_state); 267 m_state = new_state; 268 269 m_stop_info.reason = StopReason::eStopReasonSignal; 270 m_stop_info.details.signal.signo = signo; 271 } 272 273 bool 274 NativeThreadLinux::IsStopped (int *signo) 275 { 276 if (!StateIsStoppedState (m_state, false)) 277 return false; 278 279 // If we are stopped by a signal, return the signo. 280 if (signo && 281 m_state == StateType::eStateStopped && 282 m_stop_info.reason == StopReason::eStopReasonSignal) 283 { 284 *signo = m_stop_info.details.signal.signo; 285 } 286 287 // Regardless, we are stopped. 288 return true; 289 } 290 291 292 void 293 NativeThreadLinux::SetStoppedByExec () 294 { 295 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 296 if (log) 297 log->Printf ("NativeThreadLinux::%s()", __FUNCTION__); 298 299 const StateType new_state = StateType::eStateStopped; 300 MaybeLogStateChange (new_state); 301 m_state = new_state; 302 303 m_stop_info.reason = StopReason::eStopReasonExec; 304 m_stop_info.details.signal.signo = SIGSTOP; 305 } 306 307 void 308 NativeThreadLinux::SetStoppedByBreakpoint () 309 { 310 const StateType new_state = StateType::eStateStopped; 311 MaybeLogStateChange (new_state); 312 m_state = new_state; 313 314 m_stop_info.reason = StopReason::eStopReasonBreakpoint; 315 m_stop_info.details.signal.signo = SIGTRAP; 316 } 317 318 bool 319 NativeThreadLinux::IsStoppedAtBreakpoint () 320 { 321 // Are we stopped? If not, this can't be a breakpoint. 322 if (GetState () != StateType::eStateStopped) 323 return false; 324 325 // Was the stop reason a signal with signal number SIGTRAP? If not, not a breakpoint. 326 return (m_stop_info.reason == StopReason::eStopReasonBreakpoint) && 327 (m_stop_info.details.signal.signo == SIGTRAP); 328 } 329 330 void 331 NativeThreadLinux::SetStoppedByTrace () 332 { 333 const StateType new_state = StateType::eStateStopped; 334 MaybeLogStateChange (new_state); 335 m_state = new_state; 336 337 m_stop_info.reason = StopReason::eStopReasonTrace; 338 m_stop_info.details.signal.signo = SIGTRAP; 339 } 340 341 void 342 NativeThreadLinux::SetCrashedWithException (const siginfo_t& info) 343 { 344 const StateType new_state = StateType::eStateCrashed; 345 MaybeLogStateChange (new_state); 346 m_state = new_state; 347 348 m_stop_info.reason = StopReason::eStopReasonException; 349 m_stop_info.details.signal.signo = info.si_signo; 350 351 const auto reason = GetCrashReason (info); 352 m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr)); 353 } 354 355 void 356 NativeThreadLinux::SetSuspended () 357 { 358 const StateType new_state = StateType::eStateSuspended; 359 MaybeLogStateChange (new_state); 360 m_state = new_state; 361 362 // FIXME what makes sense here? Do we need a suspended StopReason? 363 m_stop_info.reason = StopReason::eStopReasonNone; 364 } 365 366 void 367 NativeThreadLinux::SetExited () 368 { 369 const StateType new_state = StateType::eStateExited; 370 MaybeLogStateChange (new_state); 371 m_state = new_state; 372 373 m_stop_info.reason = StopReason::eStopReasonThreadExiting; 374 } 375 376 void 377 NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state) 378 { 379 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 380 // If we're not logging, we're done. 381 if (!log) 382 return; 383 384 // If this is a state change to the same state, we're done. 385 lldb::StateType old_state = m_state; 386 if (new_state == old_state) 387 return; 388 389 NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 390 lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID; 391 392 // Log it. 393 log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state)); 394 } 395