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