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