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 #include <sstream> 14 15 #include "NativeProcessLinux.h" 16 #include "NativeRegisterContextLinux_arm64.h" 17 #include "NativeRegisterContextLinux_x86_64.h" 18 #include "NativeRegisterContextLinux_mips64.h" 19 20 #include "lldb/Core/Log.h" 21 #include "lldb/Core/State.h" 22 #include "lldb/Host/Host.h" 23 #include "lldb/Host/HostInfo.h" 24 #include "lldb/Host/HostNativeThread.h" 25 #include "lldb/Utility/LLDBAssert.h" 26 #include "lldb/lldb-enumerations.h" 27 28 #include "llvm/ADT/SmallString.h" 29 30 #include "Plugins/Process/POSIX/CrashReason.h" 31 32 #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h" 33 #include "Plugins/Process/Utility/RegisterContextLinux_i386.h" 34 #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h" 35 #include "Plugins/Process/Utility/RegisterContextLinux_mips64.h" 36 #include "Plugins/Process/Utility/RegisterInfoInterface.h" 37 38 using namespace lldb; 39 using namespace lldb_private; 40 41 namespace 42 { 43 void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) 44 { 45 switch (stop_info.reason) 46 { 47 case eStopReasonNone: 48 log.Printf ("%s: %s no stop reason", __FUNCTION__, header); 49 return; 50 case eStopReasonTrace: 51 log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 52 return; 53 case eStopReasonBreakpoint: 54 log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 55 return; 56 case eStopReasonWatchpoint: 57 log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 58 return; 59 case eStopReasonSignal: 60 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 61 return; 62 case eStopReasonException: 63 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); 64 return; 65 case eStopReasonExec: 66 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 67 return; 68 case eStopReasonPlanComplete: 69 log.Printf ("%s: %s plan complete", __FUNCTION__, header); 70 return; 71 case eStopReasonThreadExiting: 72 log.Printf ("%s: %s thread exiting", __FUNCTION__, header); 73 return; 74 case eStopReasonInstrumentation: 75 log.Printf ("%s: %s instrumentation", __FUNCTION__, header); 76 return; 77 default: 78 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); 79 } 80 } 81 } 82 83 NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : 84 NativeThreadProtocol (process, tid), 85 m_state (StateType::eStateInvalid), 86 m_stop_info (), 87 m_reg_context_sp (), 88 m_stop_description () 89 { 90 } 91 92 std::string 93 NativeThreadLinux::GetName() 94 { 95 NativeProcessProtocolSP process_sp = m_process_wp.lock (); 96 if (!process_sp) 97 return "<unknown: no process>"; 98 99 // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); 100 llvm::SmallString<32> thread_name; 101 HostNativeThread::GetName(GetID(), thread_name); 102 return thread_name.c_str(); 103 } 104 105 lldb::StateType 106 NativeThreadLinux::GetState () 107 { 108 return m_state; 109 } 110 111 112 bool 113 NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description) 114 { 115 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 116 117 description.clear(); 118 119 switch (m_state) 120 { 121 case eStateStopped: 122 case eStateCrashed: 123 case eStateExited: 124 case eStateSuspended: 125 case eStateUnloaded: 126 if (log) 127 LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); 128 stop_info = m_stop_info; 129 switch (m_stop_info.reason) 130 { 131 case StopReason::eStopReasonException: 132 case StopReason::eStopReasonBreakpoint: 133 case StopReason::eStopReasonWatchpoint: 134 description = m_stop_description; 135 default: 136 break; 137 } 138 if (log) 139 LogThreadStopInfo (*log, stop_info, "returned stop_info:"); 140 141 return true; 142 143 case eStateInvalid: 144 case eStateConnected: 145 case eStateAttaching: 146 case eStateLaunching: 147 case eStateRunning: 148 case eStateStepping: 149 case eStateDetached: 150 if (log) 151 { 152 log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", 153 __FUNCTION__, GetID (), StateAsCString (m_state)); 154 } 155 return false; 156 } 157 llvm_unreachable("unhandled StateType!"); 158 } 159 160 lldb_private::NativeRegisterContextSP 161 NativeThreadLinux::GetRegisterContext () 162 { 163 // Return the register context if we already created it. 164 if (m_reg_context_sp) 165 return m_reg_context_sp; 166 167 // First select the appropriate RegisterInfoInterface. 168 RegisterInfoInterface *reg_interface = nullptr; 169 NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 170 if (!m_process_sp) 171 return NativeRegisterContextSP (); 172 173 ArchSpec target_arch; 174 if (!m_process_sp->GetArchitecture (target_arch)) 175 return NativeRegisterContextSP (); 176 177 switch (target_arch.GetTriple().getOS()) 178 { 179 case llvm::Triple::Linux: 180 switch (target_arch.GetMachine()) 181 { 182 case llvm::Triple::aarch64: 183 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host"); 184 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch)); 185 break; 186 case llvm::Triple::x86: 187 case llvm::Triple::x86_64: 188 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4) 189 { 190 // 32-bit hosts run with a RegisterContextLinux_i386 context. 191 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch)); 192 } 193 else 194 { 195 assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) && 196 "Register setting path assumes this is a 64-bit host"); 197 // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context. 198 reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch)); 199 } 200 break; 201 case llvm::Triple::mips64: 202 case llvm::Triple::mips64el: 203 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) 204 && "Register setting path assumes this is a 64-bit host"); 205 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_mips64 (target_arch)); 206 break; 207 default: 208 break; 209 } 210 break; 211 default: 212 break; 213 } 214 215 assert(reg_interface && "OS or CPU not supported!"); 216 if (!reg_interface) 217 return NativeRegisterContextSP (); 218 219 // Now create the register context. 220 switch (target_arch.GetMachine()) 221 { 222 #if 0 223 case llvm::Triple::mips64: 224 { 225 RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface); 226 m_posix_thread = reg_ctx; 227 m_reg_context_sp.reset(reg_ctx); 228 break; 229 } 230 #endif 231 case llvm::Triple::mips64: 232 case llvm::Triple::mips64el: 233 { 234 const uint32_t concrete_frame_idx = 0; 235 m_reg_context_sp.reset (new NativeRegisterContextLinux_mips64 (*this, concrete_frame_idx, reg_interface)); 236 break; 237 } 238 case llvm::Triple::aarch64: 239 { 240 const uint32_t concrete_frame_idx = 0; 241 m_reg_context_sp.reset (new NativeRegisterContextLinux_arm64(*this, concrete_frame_idx, reg_interface)); 242 break; 243 } 244 case llvm::Triple::x86: 245 case llvm::Triple::x86_64: 246 { 247 const uint32_t concrete_frame_idx = 0; 248 m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface)); 249 break; 250 } 251 default: 252 break; 253 } 254 255 return m_reg_context_sp; 256 } 257 258 Error 259 NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 260 { 261 if (!hardware) 262 return Error ("not implemented"); 263 if (m_state == eStateLaunching) 264 return Error (); 265 Error error = RemoveWatchpoint(addr); 266 if (error.Fail()) return error; 267 NativeRegisterContextSP reg_ctx = GetRegisterContext (); 268 uint32_t wp_index = 269 reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags); 270 if (wp_index == LLDB_INVALID_INDEX32) 271 return Error ("Setting hardware watchpoint failed."); 272 m_watchpoint_index_map.insert({addr, wp_index}); 273 return Error (); 274 } 275 276 Error 277 NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) 278 { 279 auto wp = m_watchpoint_index_map.find(addr); 280 if (wp == m_watchpoint_index_map.end()) 281 return Error (); 282 uint32_t wp_index = wp->second; 283 m_watchpoint_index_map.erase(wp); 284 if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index)) 285 return Error (); 286 return Error ("Clearing hardware watchpoint failed."); 287 } 288 289 void 290 NativeThreadLinux::SetLaunching () 291 { 292 const StateType new_state = StateType::eStateLaunching; 293 MaybeLogStateChange (new_state); 294 m_state = new_state; 295 296 // Also mark it as stopped since launching temporarily stops the newly created thread 297 // in the ptrace machinery. 298 m_stop_info.reason = StopReason::eStopReasonSignal; 299 m_stop_info.details.signal.signo = SIGSTOP; 300 } 301 302 303 void 304 NativeThreadLinux::SetRunning () 305 { 306 const StateType new_state = StateType::eStateRunning; 307 MaybeLogStateChange (new_state); 308 m_state = new_state; 309 310 m_stop_info.reason = StopReason::eStopReasonNone; 311 m_stop_description.clear(); 312 313 // If watchpoints have been set, but none on this thread, 314 // then this is a new thread. So set all existing watchpoints. 315 if (m_watchpoint_index_map.empty()) 316 { 317 const auto process_sp = GetProcess(); 318 if (process_sp) 319 { 320 const auto &watchpoint_map = process_sp->GetWatchpointMap(); 321 if (watchpoint_map.empty()) return; 322 GetRegisterContext()->ClearAllHardwareWatchpoints(); 323 for (const auto &pair : watchpoint_map) 324 { 325 const auto& wp = pair.second; 326 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware); 327 } 328 } 329 } 330 } 331 332 void 333 NativeThreadLinux::SetStepping () 334 { 335 const StateType new_state = StateType::eStateStepping; 336 MaybeLogStateChange (new_state); 337 m_state = new_state; 338 339 m_stop_info.reason = StopReason::eStopReasonNone; 340 } 341 342 void 343 NativeThreadLinux::SetStoppedBySignal (uint32_t signo) 344 { 345 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 346 if (log) 347 log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo); 348 349 const StateType new_state = StateType::eStateStopped; 350 MaybeLogStateChange (new_state); 351 m_state = new_state; 352 353 m_stop_info.reason = StopReason::eStopReasonSignal; 354 m_stop_info.details.signal.signo = signo; 355 } 356 357 bool 358 NativeThreadLinux::IsStopped (int *signo) 359 { 360 if (!StateIsStoppedState (m_state, false)) 361 return false; 362 363 // If we are stopped by a signal, return the signo. 364 if (signo && 365 m_state == StateType::eStateStopped && 366 m_stop_info.reason == StopReason::eStopReasonSignal) 367 { 368 *signo = m_stop_info.details.signal.signo; 369 } 370 371 // Regardless, we are stopped. 372 return true; 373 } 374 375 376 void 377 NativeThreadLinux::SetStoppedByExec () 378 { 379 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 380 if (log) 381 log->Printf ("NativeThreadLinux::%s()", __FUNCTION__); 382 383 const StateType new_state = StateType::eStateStopped; 384 MaybeLogStateChange (new_state); 385 m_state = new_state; 386 387 m_stop_info.reason = StopReason::eStopReasonExec; 388 m_stop_info.details.signal.signo = SIGSTOP; 389 } 390 391 void 392 NativeThreadLinux::SetStoppedByBreakpoint () 393 { 394 const StateType new_state = StateType::eStateStopped; 395 MaybeLogStateChange (new_state); 396 m_state = new_state; 397 398 m_stop_info.reason = StopReason::eStopReasonBreakpoint; 399 m_stop_info.details.signal.signo = SIGTRAP; 400 m_stop_description.clear(); 401 } 402 403 void 404 NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index) 405 { 406 const StateType new_state = StateType::eStateStopped; 407 MaybeLogStateChange (new_state); 408 m_state = new_state; 409 m_stop_description.clear (); 410 411 lldbassert(wp_index != LLDB_INVALID_INDEX32 && 412 "wp_index cannot be invalid"); 413 414 std::ostringstream ostr; 415 ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " "; 416 ostr << wp_index; 417 m_stop_description = ostr.str(); 418 419 m_stop_info.reason = StopReason::eStopReasonWatchpoint; 420 m_stop_info.details.signal.signo = SIGTRAP; 421 } 422 423 bool 424 NativeThreadLinux::IsStoppedAtBreakpoint () 425 { 426 return GetState () == StateType::eStateStopped && 427 m_stop_info.reason == StopReason::eStopReasonBreakpoint; 428 } 429 430 bool 431 NativeThreadLinux::IsStoppedAtWatchpoint () 432 { 433 return GetState () == StateType::eStateStopped && 434 m_stop_info.reason == StopReason::eStopReasonWatchpoint; 435 } 436 437 void 438 NativeThreadLinux::SetStoppedByTrace () 439 { 440 const StateType new_state = StateType::eStateStopped; 441 MaybeLogStateChange (new_state); 442 m_state = new_state; 443 444 m_stop_info.reason = StopReason::eStopReasonTrace; 445 m_stop_info.details.signal.signo = SIGTRAP; 446 } 447 448 void 449 NativeThreadLinux::SetCrashedWithException (const siginfo_t& info) 450 { 451 const StateType new_state = StateType::eStateCrashed; 452 MaybeLogStateChange (new_state); 453 m_state = new_state; 454 455 m_stop_info.reason = StopReason::eStopReasonException; 456 m_stop_info.details.signal.signo = info.si_signo; 457 458 const auto reason = GetCrashReason (info); 459 m_stop_description = GetCrashReasonString (reason, reinterpret_cast<lldb::addr_t> (info.si_addr)); 460 } 461 462 void 463 NativeThreadLinux::SetSuspended () 464 { 465 const StateType new_state = StateType::eStateSuspended; 466 MaybeLogStateChange (new_state); 467 m_state = new_state; 468 469 // FIXME what makes sense here? Do we need a suspended StopReason? 470 m_stop_info.reason = StopReason::eStopReasonNone; 471 } 472 473 void 474 NativeThreadLinux::SetExited () 475 { 476 const StateType new_state = StateType::eStateExited; 477 MaybeLogStateChange (new_state); 478 m_state = new_state; 479 480 m_stop_info.reason = StopReason::eStopReasonThreadExiting; 481 } 482 483 void 484 NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state) 485 { 486 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 487 // If we're not logging, we're done. 488 if (!log) 489 return; 490 491 // If this is a state change to the same state, we're done. 492 lldb::StateType old_state = m_state; 493 if (new_state == old_state) 494 return; 495 496 NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 497 lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID; 498 499 // Log it. 500 log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state)); 501 } 502