Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 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> |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 13 | #include <sstream> |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 14 | |
| 15 | #include "NativeProcessLinux.h" |
Omair Javaid | 3f57216 | 2015-04-14 07:30:20 +0000 | [diff] [blame] | 16 | #include "NativeRegisterContextLinux_arm.h" |
Tamas Berghammer | 1e209fc | 2015-03-13 11:36:47 +0000 | [diff] [blame] | 17 | #include "NativeRegisterContextLinux_arm64.h" |
Todd Fiala | 2850b1b | 2014-06-30 23:51:35 +0000 | [diff] [blame] | 18 | #include "NativeRegisterContextLinux_x86_64.h" |
Mohit K. Bhakkad | 3df471c | 2015-03-17 11:43:56 +0000 | [diff] [blame] | 19 | #include "NativeRegisterContextLinux_mips64.h" |
Todd Fiala | 2850b1b | 2014-06-30 23:51:35 +0000 | [diff] [blame] | 20 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 21 | #include "lldb/Core/Log.h" |
| 22 | #include "lldb/Core/State.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 23 | #include "lldb/Host/HostNativeThread.h" |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 24 | #include "lldb/Utility/LLDBAssert.h" |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 25 | #include "lldb/lldb-enumerations.h" |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 26 | |
| 27 | #include "llvm/ADT/SmallString.h" |
| 28 | |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 29 | #include "Plugins/Process/POSIX/CrashReason.h" |
| 30 | |
Pavel Labath | 8c8ff7a | 2015-05-11 10:03:10 +0000 | [diff] [blame] | 31 | #include <sys/syscall.h> |
| 32 | // Try to define a macro to encapsulate the tgkill syscall |
| 33 | #define tgkill(pid, tid, sig) \ |
| 34 | syscall(SYS_tgkill, static_cast<::pid_t>(pid), static_cast<::pid_t>(tid), sig) |
| 35 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 36 | using namespace lldb; |
| 37 | using namespace lldb_private; |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 38 | using namespace lldb_private::process_linux; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 39 | |
| 40 | namespace |
| 41 | { |
| 42 | void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header) |
| 43 | { |
| 44 | switch (stop_info.reason) |
| 45 | { |
Pavel Labath | 12fd375 | 2015-03-20 14:45:13 +0000 | [diff] [blame] | 46 | case eStopReasonNone: |
| 47 | log.Printf ("%s: %s no stop reason", __FUNCTION__, header); |
| 48 | return; |
| 49 | case eStopReasonTrace: |
| 50 | log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); |
| 51 | return; |
| 52 | case eStopReasonBreakpoint: |
| 53 | log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); |
| 54 | return; |
| 55 | case eStopReasonWatchpoint: |
| 56 | log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); |
| 57 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 58 | case eStopReasonSignal: |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 59 | log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 60 | return; |
| 61 | case eStopReasonException: |
Chaoren Lin | ae29d39 | 2015-02-03 01:50:46 +0000 | [diff] [blame] | 62 | log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type); |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 63 | return; |
| 64 | case eStopReasonExec: |
| 65 | log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 66 | return; |
Pavel Labath | 12fd375 | 2015-03-20 14:45:13 +0000 | [diff] [blame] | 67 | case eStopReasonPlanComplete: |
| 68 | log.Printf ("%s: %s plan complete", __FUNCTION__, header); |
| 69 | return; |
| 70 | case eStopReasonThreadExiting: |
| 71 | log.Printf ("%s: %s thread exiting", __FUNCTION__, header); |
| 72 | return; |
| 73 | case eStopReasonInstrumentation: |
| 74 | log.Printf ("%s: %s instrumentation", __FUNCTION__, header); |
| 75 | return; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 76 | default: |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 77 | log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 78 | } |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) : |
| 83 | NativeThreadProtocol (process, tid), |
| 84 | m_state (StateType::eStateInvalid), |
| 85 | m_stop_info (), |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 86 | m_reg_context_sp (), |
| 87 | m_stop_description () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 88 | { |
| 89 | } |
| 90 | |
Todd Fiala | 7206c6d | 2014-09-12 22:51:49 +0000 | [diff] [blame] | 91 | std::string |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 92 | NativeThreadLinux::GetName() |
| 93 | { |
| 94 | NativeProcessProtocolSP process_sp = m_process_wp.lock (); |
| 95 | if (!process_sp) |
| 96 | return "<unknown: no process>"; |
| 97 | |
| 98 | // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ()); |
Zachary Turner | 39de311 | 2014-09-09 20:54:56 +0000 | [diff] [blame] | 99 | llvm::SmallString<32> thread_name; |
| 100 | HostNativeThread::GetName(GetID(), thread_name); |
| 101 | return thread_name.c_str(); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | lldb::StateType |
| 105 | NativeThreadLinux::GetState () |
| 106 | { |
| 107 | return m_state; |
| 108 | } |
| 109 | |
| 110 | |
| 111 | bool |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 112 | NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 113 | { |
| 114 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 115 | |
| 116 | description.clear(); |
| 117 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 118 | switch (m_state) |
| 119 | { |
| 120 | case eStateStopped: |
| 121 | case eStateCrashed: |
| 122 | case eStateExited: |
| 123 | case eStateSuspended: |
| 124 | case eStateUnloaded: |
| 125 | if (log) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 126 | LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 127 | stop_info = m_stop_info; |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 128 | description = m_stop_description; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 129 | if (log) |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 130 | LogThreadStopInfo (*log, stop_info, "returned stop_info:"); |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 131 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 132 | return true; |
| 133 | |
| 134 | case eStateInvalid: |
| 135 | case eStateConnected: |
| 136 | case eStateAttaching: |
| 137 | case eStateLaunching: |
| 138 | case eStateRunning: |
| 139 | case eStateStepping: |
| 140 | case eStateDetached: |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 141 | if (log) |
| 142 | { |
| 143 | log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason", |
| 144 | __FUNCTION__, GetID (), StateAsCString (m_state)); |
| 145 | } |
| 146 | return false; |
| 147 | } |
David Majnemer | 8faf937 | 2014-09-16 06:34:29 +0000 | [diff] [blame] | 148 | llvm_unreachable("unhandled StateType!"); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Tamas Berghammer | db264a6 | 2015-03-31 09:52:22 +0000 | [diff] [blame] | 151 | NativeRegisterContextSP |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 152 | NativeThreadLinux::GetRegisterContext () |
| 153 | { |
| 154 | // Return the register context if we already created it. |
| 155 | if (m_reg_context_sp) |
| 156 | return m_reg_context_sp; |
| 157 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 158 | NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); |
| 159 | if (!m_process_sp) |
| 160 | return NativeRegisterContextSP (); |
| 161 | |
| 162 | ArchSpec target_arch; |
| 163 | if (!m_process_sp->GetArchitecture (target_arch)) |
| 164 | return NativeRegisterContextSP (); |
| 165 | |
Tamas Berghammer | 068f8a7 | 2015-05-26 11:58:52 +0000 | [diff] [blame] | 166 | const uint32_t concrete_frame_idx = 0; |
| 167 | m_reg_context_sp.reset (NativeRegisterContextLinux::CreateHostNativeRegisterContextLinux(target_arch, |
| 168 | *this, |
| 169 | concrete_frame_idx)); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 170 | |
| 171 | return m_reg_context_sp; |
| 172 | } |
| 173 | |
| 174 | Error |
| 175 | NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware) |
| 176 | { |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 177 | if (!hardware) |
| 178 | return Error ("not implemented"); |
Chaoren Lin | f591f69 | 2015-02-26 19:48:15 +0000 | [diff] [blame] | 179 | if (m_state == eStateLaunching) |
| 180 | return Error (); |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 181 | Error error = RemoveWatchpoint(addr); |
| 182 | if (error.Fail()) return error; |
| 183 | NativeRegisterContextSP reg_ctx = GetRegisterContext (); |
| 184 | uint32_t wp_index = |
| 185 | reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags); |
| 186 | if (wp_index == LLDB_INVALID_INDEX32) |
| 187 | return Error ("Setting hardware watchpoint failed."); |
| 188 | m_watchpoint_index_map.insert({addr, wp_index}); |
| 189 | return Error (); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | Error |
| 193 | NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr) |
| 194 | { |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 195 | auto wp = m_watchpoint_index_map.find(addr); |
| 196 | if (wp == m_watchpoint_index_map.end()) |
| 197 | return Error (); |
| 198 | uint32_t wp_index = wp->second; |
| 199 | m_watchpoint_index_map.erase(wp); |
| 200 | if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index)) |
| 201 | return Error (); |
| 202 | return Error ("Clearing hardware watchpoint failed."); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 203 | } |
| 204 | |
| 205 | void |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 206 | NativeThreadLinux::SetRunning () |
| 207 | { |
| 208 | const StateType new_state = StateType::eStateRunning; |
| 209 | MaybeLogStateChange (new_state); |
| 210 | m_state = new_state; |
| 211 | |
| 212 | m_stop_info.reason = StopReason::eStopReasonNone; |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 213 | m_stop_description.clear(); |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 214 | |
| 215 | // If watchpoints have been set, but none on this thread, |
| 216 | // then this is a new thread. So set all existing watchpoints. |
| 217 | if (m_watchpoint_index_map.empty()) |
| 218 | { |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 219 | const auto process_sp = GetProcess(); |
| 220 | if (process_sp) |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 221 | { |
Oleksiy Vyalov | 8bc34f4 | 2015-02-19 17:58:04 +0000 | [diff] [blame] | 222 | const auto &watchpoint_map = process_sp->GetWatchpointMap(); |
| 223 | if (watchpoint_map.empty()) return; |
| 224 | GetRegisterContext()->ClearAllHardwareWatchpoints(); |
| 225 | for (const auto &pair : watchpoint_map) |
| 226 | { |
| 227 | const auto& wp = pair.second; |
| 228 | SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware); |
| 229 | } |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | void |
| 235 | NativeThreadLinux::SetStepping () |
| 236 | { |
| 237 | const StateType new_state = StateType::eStateStepping; |
| 238 | MaybeLogStateChange (new_state); |
| 239 | m_state = new_state; |
| 240 | |
| 241 | m_stop_info.reason = StopReason::eStopReasonNone; |
| 242 | } |
| 243 | |
| 244 | void |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 245 | NativeThreadLinux::SetStoppedBySignal(uint32_t signo, const siginfo_t *info) |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 246 | { |
| 247 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 248 | if (log) |
Chaoren Lin | b8af31d | 2015-02-03 01:50:51 +0000 | [diff] [blame] | 249 | log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo); |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 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 = signo; |
Pavel Labath | c4e25c9 | 2015-05-29 10:13:03 +0000 | [diff] [blame] | 257 | |
| 258 | m_stop_description.clear(); |
| 259 | switch (signo) |
| 260 | { |
| 261 | case SIGSEGV: |
| 262 | case SIGBUS: |
| 263 | case SIGFPE: |
| 264 | case SIGILL: |
| 265 | if (! info) |
| 266 | break; |
| 267 | const auto reason = GetCrashReason(*info); |
| 268 | m_stop_description = GetCrashReasonString(reason, reinterpret_cast<uintptr_t>(info->si_addr)); |
| 269 | break; |
| 270 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 271 | } |
| 272 | |
Todd Fiala | 511e5cd | 2014-09-11 23:29:14 +0000 | [diff] [blame] | 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 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 292 | void |
Todd Fiala | a9882ce | 2014-08-28 15:46:54 +0000 | [diff] [blame] | 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 |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 308 | NativeThreadLinux::SetStoppedByBreakpoint () |
| 309 | { |
| 310 | const StateType new_state = StateType::eStateStopped; |
| 311 | MaybeLogStateChange (new_state); |
| 312 | m_state = new_state; |
| 313 | |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 314 | m_stop_info.reason = StopReason::eStopReasonBreakpoint; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 315 | m_stop_info.details.signal.signo = SIGTRAP; |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 316 | m_stop_description.clear(); |
| 317 | } |
| 318 | |
| 319 | void |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 320 | NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index) |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 321 | { |
| 322 | const StateType new_state = StateType::eStateStopped; |
| 323 | MaybeLogStateChange (new_state); |
| 324 | m_state = new_state; |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 325 | m_stop_description.clear (); |
Tamas Berghammer | eadb2a9 | 2015-03-17 14:40:57 +0000 | [diff] [blame] | 326 | |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 327 | lldbassert(wp_index != LLDB_INVALID_INDEX32 && |
| 328 | "wp_index cannot be invalid"); |
Tamas Berghammer | eadb2a9 | 2015-03-17 14:40:57 +0000 | [diff] [blame] | 329 | |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 330 | std::ostringstream ostr; |
| 331 | ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " "; |
| 332 | ostr << wp_index; |
| 333 | m_stop_description = ostr.str(); |
Tamas Berghammer | eadb2a9 | 2015-03-17 14:40:57 +0000 | [diff] [blame] | 334 | |
Chaoren Lin | c16f5dc | 2015-03-19 23:28:10 +0000 | [diff] [blame] | 335 | m_stop_info.reason = StopReason::eStopReasonWatchpoint; |
| 336 | m_stop_info.details.signal.signo = SIGTRAP; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | bool |
| 340 | NativeThreadLinux::IsStoppedAtBreakpoint () |
| 341 | { |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 342 | return GetState () == StateType::eStateStopped && |
| 343 | m_stop_info.reason == StopReason::eStopReasonBreakpoint; |
| 344 | } |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 345 | |
Chaoren Lin | 18fe640 | 2015-02-03 01:51:47 +0000 | [diff] [blame] | 346 | bool |
| 347 | NativeThreadLinux::IsStoppedAtWatchpoint () |
| 348 | { |
| 349 | return GetState () == StateType::eStateStopped && |
| 350 | m_stop_info.reason == StopReason::eStopReasonWatchpoint; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | void |
Chaoren Lin | 28e5742 | 2015-02-03 01:51:25 +0000 | [diff] [blame] | 354 | NativeThreadLinux::SetStoppedByTrace () |
| 355 | { |
| 356 | const StateType new_state = StateType::eStateStopped; |
| 357 | MaybeLogStateChange (new_state); |
| 358 | m_state = new_state; |
| 359 | |
| 360 | m_stop_info.reason = StopReason::eStopReasonTrace; |
| 361 | m_stop_info.details.signal.signo = SIGTRAP; |
| 362 | } |
| 363 | |
| 364 | void |
Pavel Labath | 05569f6 | 2015-07-23 09:09:29 +0000 | [diff] [blame^] | 365 | NativeThreadLinux::SetStoppedWithNoReason () |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 366 | { |
Pavel Labath | 05569f6 | 2015-07-23 09:09:29 +0000 | [diff] [blame^] | 367 | const StateType new_state = StateType::eStateStopped; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 368 | MaybeLogStateChange (new_state); |
| 369 | m_state = new_state; |
| 370 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 371 | m_stop_info.reason = StopReason::eStopReasonNone; |
Pavel Labath | 05569f6 | 2015-07-23 09:09:29 +0000 | [diff] [blame^] | 372 | m_stop_info.details.signal.signo = 0; |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 373 | } |
| 374 | |
| 375 | void |
| 376 | NativeThreadLinux::SetExited () |
| 377 | { |
| 378 | const StateType new_state = StateType::eStateExited; |
| 379 | MaybeLogStateChange (new_state); |
| 380 | m_state = new_state; |
| 381 | |
| 382 | m_stop_info.reason = StopReason::eStopReasonThreadExiting; |
| 383 | } |
| 384 | |
Pavel Labath | 8c8ff7a | 2015-05-11 10:03:10 +0000 | [diff] [blame] | 385 | Error |
| 386 | NativeThreadLinux::RequestStop () |
| 387 | { |
| 388 | Log* log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 389 | |
| 390 | const auto process_sp = GetProcess(); |
| 391 | if (! process_sp) |
| 392 | return Error("Process is null."); |
| 393 | |
| 394 | lldb::pid_t pid = process_sp->GetID(); |
| 395 | lldb::tid_t tid = GetID(); |
| 396 | |
| 397 | if (log) |
| 398 | log->Printf ("NativeThreadLinux::%s requesting thread stop(pid: %" PRIu64 ", tid: %" PRIu64 ")", __FUNCTION__, pid, tid); |
| 399 | |
| 400 | Error err; |
| 401 | errno = 0; |
| 402 | if (::tgkill (pid, tid, SIGSTOP) != 0) |
| 403 | { |
| 404 | err.SetErrorToErrno (); |
| 405 | if (log) |
| 406 | log->Printf ("NativeThreadLinux::%s tgkill(%" PRIu64 ", %" PRIu64 ", SIGSTOP) failed: %s", __FUNCTION__, pid, tid, err.AsCString ()); |
| 407 | } |
| 408 | else |
| 409 | m_thread_context.stop_requested = true; |
| 410 | |
| 411 | return err; |
| 412 | } |
| 413 | |
Todd Fiala | af245d1 | 2014-06-30 21:05:18 +0000 | [diff] [blame] | 414 | void |
| 415 | NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state) |
| 416 | { |
| 417 | Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD)); |
| 418 | // If we're not logging, we're done. |
| 419 | if (!log) |
| 420 | return; |
| 421 | |
| 422 | // If this is a state change to the same state, we're done. |
| 423 | lldb::StateType old_state = m_state; |
| 424 | if (new_state == old_state) |
| 425 | return; |
| 426 | |
| 427 | NativeProcessProtocolSP m_process_sp = m_process_wp.lock (); |
| 428 | lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID; |
| 429 | |
| 430 | // Log it. |
| 431 | log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state)); |
| 432 | } |