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.h" 17 #include "SingleStepCheck.h" 18 19 #include "lldb/Core/Log.h" 20 #include "lldb/Core/State.h" 21 #include "lldb/Host/HostNativeThread.h" 22 #include "lldb/Host/linux/Ptrace.h" 23 #include "lldb/Utility/LLDBAssert.h" 24 #include "lldb/lldb-enumerations.h" 25 26 #include "llvm/ADT/SmallString.h" 27 28 #include "Plugins/Process/POSIX/CrashReason.h" 29 30 #include <sys/syscall.h> 31 // Try to define a macro to encapsulate the tgkill syscall 32 #define tgkill(pid, tid, sig) \ 33 syscall(SYS_tgkill, static_cast< ::pid_t>(pid), static_cast< ::pid_t>(tid), sig) 34 35 using namespace lldb; 36 using namespace lldb_private; 37 using namespace lldb_private::process_linux; 38 39 namespace 40 { 41 void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) 42 { 43 switch (stop_info.reason) 44 { 45 case eStopReasonNone: 46 log.Printf ("%s: %s no stop reason", __FUNCTION__, header); 47 return; 48 case eStopReasonTrace: 49 log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 50 return; 51 case eStopReasonBreakpoint: 52 log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 53 return; 54 case eStopReasonWatchpoint: 55 log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 56 return; 57 case eStopReasonSignal: 58 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 59 return; 60 case eStopReasonException: 61 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); 62 return; 63 case eStopReasonExec: 64 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); 65 return; 66 case eStopReasonPlanComplete: 67 log.Printf ("%s: %s plan complete", __FUNCTION__, header); 68 return; 69 case eStopReasonThreadExiting: 70 log.Printf ("%s: %s thread exiting", __FUNCTION__, header); 71 return; 72 case eStopReasonInstrumentation: 73 log.Printf ("%s: %s instrumentation", __FUNCTION__, header); 74 return; 75 default: 76 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); 77 } 78 } 79 } 80 81 NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : 82 NativeThreadProtocol (process, tid), 83 m_state (StateType::eStateInvalid), 84 m_stop_info (), 85 m_reg_context_sp (), 86 m_stop_description () 87 { 88 } 89 90 std::string 91 NativeThreadLinux::GetName() 92 { 93 NativeProcessProtocolSP process_sp = m_process_wp.lock (); 94 if (!process_sp) 95 return "<unknown: no process>"; 96 97 // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); 98 llvm::SmallString<32> thread_name; 99 HostNativeThread::GetName(GetID(), thread_name); 100 return thread_name.c_str(); 101 } 102 103 lldb::StateType 104 NativeThreadLinux::GetState () 105 { 106 return m_state; 107 } 108 109 110 bool 111 NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description) 112 { 113 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 114 115 description.clear(); 116 117 switch (m_state) 118 { 119 case eStateStopped: 120 case eStateCrashed: 121 case eStateExited: 122 case eStateSuspended: 123 case eStateUnloaded: 124 if (log) 125 LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); 126 stop_info = m_stop_info; 127 description = m_stop_description; 128 if (log) 129 LogThreadStopInfo (*log, stop_info, "returned stop_info:"); 130 131 return true; 132 133 case eStateInvalid: 134 case eStateConnected: 135 case eStateAttaching: 136 case eStateLaunching: 137 case eStateRunning: 138 case eStateStepping: 139 case eStateDetached: 140 if (log) 141 { 142 log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", 143 __FUNCTION__, GetID (), StateAsCString (m_state)); 144 } 145 return false; 146 } 147 llvm_unreachable("unhandled StateType!"); 148 } 149 150 NativeRegisterContextSP 151 NativeThreadLinux::GetRegisterContext () 152 { 153 // Return the register context if we already created it. 154 if (m_reg_context_sp) 155 return m_reg_context_sp; 156 157 NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); 158 if (!m_process_sp) 159 return NativeRegisterContextSP (); 160 161 ArchSpec target_arch; 162 if (!m_process_sp->GetArchitecture (target_arch)) 163 return NativeRegisterContextSP (); 164 165 const uint32_t concrete_frame_idx = 0; 166 m_reg_context_sp.reset (NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(target_arch, 167 *this, 168 concrete_frame_idx)); 169 170 return m_reg_context_sp; 171 } 172 173 Error 174 NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) 175 { 176 if (!hardware) 177 return Error ("not implemented"); 178 if (m_state == eStateLaunching) 179 return Error (); 180 Error error = RemoveWatchpoint(addr); 181 if (error.Fail()) return error; 182 NativeRegisterContextSP reg_ctx = GetRegisterContext (); 183 uint32_t wp_index = 184 reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags); 185 if (wp_index == LLDB_INVALID_INDEX32) 186 return Error ("Setting hardware watchpoint failed."); 187 m_watchpoint_index_map.insert({addr, wp_index}); 188 return Error (); 189 } 190 191 Error 192 NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) 193 { 194 auto wp = m_watchpoint_index_map.find(addr); 195 if (wp == m_watchpoint_index_map.end()) 196 return Error (); 197 uint32_t wp_index = wp->second; 198 m_watchpoint_index_map.erase(wp); 199 if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index)) 200 return Error (); 201 return Error ("Clearing hardware watchpoint failed."); 202 } 203 204 Error 205 NativeThreadLinux::Resume(uint32_t signo) 206 { 207 const StateType new_state = StateType::eStateRunning; 208 MaybeLogStateChange (new_state); 209 m_state = new_state; 210 211 m_stop_info.reason = StopReason::eStopReasonNone; 212 m_stop_description.clear(); 213 214 // If watchpoints have been set, but none on this thread, 215 // then this is a new thread. So set all existing watchpoints. 216 if (m_watchpoint_index_map.empty()) 217 { 218 NativeProcessLinux &process = GetProcess(); 219 220 const auto &watchpoint_map = process.GetWatchpointMap(); 221 GetRegisterContext()->ClearAllHardwareWatchpoints(); 222 for (const auto &pair : watchpoint_map) 223 { 224 const auto &wp = pair.second; 225 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware); 226 } 227 } 228 229 intptr_t data = 0; 230 231 if (signo != LLDB_INVALID_SIGNAL_NUMBER) 232 data = signo; 233 234 return NativeProcessLinux::PtraceWrapper(PTRACE_CONT, GetID(), nullptr, reinterpret_cast<void *>(data)); 235 } 236 237 void 238 NativeThreadLinux::MaybePrepareSingleStepWorkaround() 239 { 240 if (!SingleStepWorkaroundNeeded()) 241 return; 242 243 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 244 245 if (sched_getaffinity(static_cast<::pid_t>(m_tid), sizeof m_original_cpu_set, &m_original_cpu_set) != 0) 246 { 247 // This should really not fail. But, just in case... 248 if (log) 249 { 250 Error error(errno, eErrorTypePOSIX); 251 log->Printf("NativeThreadLinux::%s Unable to get cpu affinity for thread %" PRIx64 ": %s", __FUNCTION__, 252 m_tid, error.AsCString()); 253 } 254 return; 255 } 256 257 cpu_set_t set; 258 CPU_ZERO(&set); 259 CPU_SET(0, &set); 260 if (sched_setaffinity(static_cast<::pid_t>(m_tid), sizeof set, &set) != 0 && log) 261 { 262 // This may fail in very locked down systems, if the thread is not allowed to run on 263 // cpu 0. If that happens, only thing we can do is it log it and continue... 264 Error error(errno, eErrorTypePOSIX); 265 log->Printf("NativeThreadLinux::%s Unable to set cpu affinity for thread %" PRIx64 ": %s", __FUNCTION__, m_tid, 266 error.AsCString()); 267 } 268 } 269 270 void 271 NativeThreadLinux::MaybeCleanupSingleStepWorkaround() 272 { 273 if (!SingleStepWorkaroundNeeded()) 274 return; 275 276 if (sched_setaffinity(static_cast<::pid_t>(m_tid), sizeof m_original_cpu_set, &m_original_cpu_set) != 0) 277 { 278 Error error(errno, eErrorTypePOSIX); 279 Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_THREAD)); 280 log->Printf("NativeThreadLinux::%s Unable to reset cpu affinity for thread %" PRIx64 ": %s", __FUNCTION__, 281 m_tid, error.AsCString()); 282 } 283 } 284 285 Error 286 NativeThreadLinux::SingleStep(uint32_t signo) 287 { 288 const StateType new_state = StateType::eStateStepping; 289 MaybeLogStateChange (new_state); 290 m_state = new_state; 291 m_stop_info.reason = StopReason::eStopReasonNone; 292 293 MaybePrepareSingleStepWorkaround(); 294 295 intptr_t data = 0; 296 if (signo != LLDB_INVALID_SIGNAL_NUMBER) 297 data = signo; 298 299 // If hardware single-stepping is not supported, we just do a continue. The breakpoint on the 300 // next instruction has been setup in NativeProcessLinux::Resume. 301 return NativeProcessLinux::PtraceWrapper(GetProcess().SupportHardwareSingleStepping() ? PTRACE_SINGLESTEP 302 : PTRACE_CONT, 303 m_tid, nullptr, reinterpret_cast<void *>(data)); 304 } 305 306 void 307 NativeThreadLinux::SetStoppedBySignal(uint32_t signo, const siginfo_t *info) 308 { 309 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 310 if (log) 311 log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo); 312 313 SetStopped(); 314 315 m_stop_info.reason = StopReason::eStopReasonSignal; 316 m_stop_info.details.signal.signo = signo; 317 318 m_stop_description.clear(); 319 if (info) 320 { 321 switch (signo) 322 { 323 case SIGSEGV: 324 case SIGBUS: 325 case SIGFPE: 326 case SIGILL: 327 //In case of MIPS64 target, SI_KERNEL is generated for invalid 64bit address. 328 const auto reason = (info->si_signo == SIGBUS && info->si_code == SI_KERNEL) ? 329 CrashReason::eInvalidAddress : GetCrashReason(*info); 330 m_stop_description = GetCrashReasonString(reason, reinterpret_cast<uintptr_t>(info->si_addr)); 331 break; 332 } 333 } 334 } 335 336 bool 337 NativeThreadLinux::IsStopped (int *signo) 338 { 339 if (!StateIsStoppedState (m_state, false)) 340 return false; 341 342 // If we are stopped by a signal, return the signo. 343 if (signo && 344 m_state == StateType::eStateStopped && 345 m_stop_info.reason == StopReason::eStopReasonSignal) 346 { 347 *signo = m_stop_info.details.signal.signo; 348 } 349 350 // Regardless, we are stopped. 351 return true; 352 } 353 354 void 355 NativeThreadLinux::SetStopped() 356 { 357 if (m_state == StateType::eStateStepping) 358 MaybeCleanupSingleStepWorkaround(); 359 360 const StateType new_state = StateType::eStateStopped; 361 MaybeLogStateChange(new_state); 362 m_state = new_state; 363 m_stop_description.clear(); 364 } 365 366 void 367 NativeThreadLinux::SetStoppedByExec () 368 { 369 Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 370 if (log) 371 log->Printf ("NativeThreadLinux::%s()", __FUNCTION__); 372 373 SetStopped(); 374 375 m_stop_info.reason = StopReason::eStopReasonExec; 376 m_stop_info.details.signal.signo = SIGSTOP; 377 } 378 379 void 380 NativeThreadLinux::SetStoppedByBreakpoint () 381 { 382 SetStopped(); 383 384 m_stop_info.reason = StopReason::eStopReasonBreakpoint; 385 m_stop_info.details.signal.signo = SIGTRAP; 386 m_stop_description.clear(); 387 } 388 389 void 390 NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index) 391 { 392 SetStopped(); 393 394 lldbassert(wp_index != LLDB_INVALID_INDEX32 && 395 "wp_index cannot be invalid"); 396 397 std::ostringstream ostr; 398 ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " "; 399 ostr << wp_index; 400 401 /* 402 * MIPS: Last 3bits of the watchpoint address are masked by the kernel. For example: 403 * 'n' is at 0x120010d00 and 'm' is 0x120010d04. When a watchpoint is set at 'm', then 404 * watch exception is generated even when 'n' is read/written. To handle this case, 405 * find the base address of the load/store instruction and append it in the stop-info 406 * packet. 407 */ 408 ostr << " " << GetRegisterContext()->GetWatchpointHitAddress(wp_index); 409 410 m_stop_description = ostr.str(); 411 412 m_stop_info.reason = StopReason::eStopReasonWatchpoint; 413 m_stop_info.details.signal.signo = SIGTRAP; 414 } 415 416 bool 417 NativeThreadLinux::IsStoppedAtBreakpoint () 418 { 419 return GetState () == StateType::eStateStopped && 420 m_stop_info.reason == StopReason::eStopReasonBreakpoint; 421 } 422 423 bool 424 NativeThreadLinux::IsStoppedAtWatchpoint () 425 { 426 return GetState () == StateType::eStateStopped && 427 m_stop_info.reason == StopReason::eStopReasonWatchpoint; 428 } 429 430 void 431 NativeThreadLinux::SetStoppedByTrace () 432 { 433 SetStopped(); 434 435 m_stop_info.reason = StopReason::eStopReasonTrace; 436 m_stop_info.details.signal.signo = SIGTRAP; 437 } 438 439 void 440 NativeThreadLinux::SetStoppedWithNoReason () 441 { 442 SetStopped(); 443 444 m_stop_info.reason = StopReason::eStopReasonNone; 445 m_stop_info.details.signal.signo = 0; 446 } 447 448 void 449 NativeThreadLinux::SetExited () 450 { 451 const StateType new_state = StateType::eStateExited; 452 MaybeLogStateChange (new_state); 453 m_state = new_state; 454 455 m_stop_info.reason = StopReason::eStopReasonThreadExiting; 456 } 457 458 Error 459 NativeThreadLinux::RequestStop () 460 { 461 Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); 462 463 NativeProcessLinux &process = GetProcess(); 464 465 lldb::pid_t pid = process.GetID(); 466 lldb::tid_t tid = GetID(); 467 468 if (log) 469 log->Printf ("NativeThreadLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); 470 471 Error err; 472 errno = 0; 473 if (::tgkill (pid, tid, SIGSTOP) != 0) 474 { 475 err.SetErrorToErrno (); 476 if (log) 477 log->Printf ("NativeThreadLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); 478 } 479 480 return err; 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 503 NativeProcessLinux & 504 NativeThreadLinux::GetProcess() 505 { 506 auto process_sp = std::static_pointer_cast<NativeProcessLinux>(NativeThreadProtocol::GetProcess()); 507 assert(process_sp); 508 return *process_sp; 509 } 510