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