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