1af245d11STodd Fiala //===-- NativeThreadLinux.cpp --------------------------------- -*- C++ -*-===//
2af245d11STodd Fiala //
3af245d11STodd Fiala //                     The LLVM Compiler Infrastructure
4af245d11STodd Fiala //
5af245d11STodd Fiala // This file is distributed under the University of Illinois Open Source
6af245d11STodd Fiala // License. See LICENSE.TXT for details.
7af245d11STodd Fiala //
8af245d11STodd Fiala //===----------------------------------------------------------------------===//
9af245d11STodd Fiala 
10af245d11STodd Fiala #include "NativeThreadLinux.h"
11af245d11STodd Fiala 
12af245d11STodd Fiala #include <signal.h>
1318fe6404SChaoren Lin #include <sstream>
14af245d11STodd Fiala 
15af245d11STodd Fiala #include "NativeProcessLinux.h"
163f57216cSOmair Javaid #include "NativeRegisterContextLinux_arm.h"
171e209fccSTamas Berghammer #include "NativeRegisterContextLinux_arm64.h"
182850b1beSTodd Fiala #include "NativeRegisterContextLinux_x86_64.h"
193df471c3SMohit K. Bhakkad #include "NativeRegisterContextLinux_mips64.h"
202850b1beSTodd Fiala 
21af245d11STodd Fiala #include "lldb/Core/Log.h"
22af245d11STodd Fiala #include "lldb/Core/State.h"
23af245d11STodd Fiala #include "lldb/Host/Host.h"
2413b18261SZachary Turner #include "lldb/Host/HostInfo.h"
2539de3110SZachary Turner #include "lldb/Host/HostNativeThread.h"
26c16f5dcaSChaoren Lin #include "lldb/Utility/LLDBAssert.h"
27af245d11STodd Fiala #include "lldb/lldb-enumerations.h"
2839de3110SZachary Turner 
2939de3110SZachary Turner #include "llvm/ADT/SmallString.h"
3039de3110SZachary Turner 
3128e57429SChaoren Lin #include "Plugins/Process/POSIX/CrashReason.h"
3228e57429SChaoren Lin 
333f57216cSOmair Javaid #include "Plugins/Process/Utility/RegisterContextLinux_arm.h"
34b71e89e9STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_arm64.h"
35af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_i386.h"
36af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterContextLinux_x86_64.h"
373df471c3SMohit K. Bhakkad #include "Plugins/Process/Utility/RegisterContextLinux_mips64.h"
38af245d11STodd Fiala #include "Plugins/Process/Utility/RegisterInfoInterface.h"
39af245d11STodd Fiala 
40af245d11STodd Fiala using namespace lldb;
41af245d11STodd Fiala using namespace lldb_private;
42db264a6dSTamas Berghammer using namespace lldb_private::process_linux;
43af245d11STodd Fiala 
44af245d11STodd Fiala namespace
45af245d11STodd Fiala {
46af245d11STodd Fiala     void LogThreadStopInfo (Log &log, const ThreadStopInfo &stop_info, const char *const header)
47af245d11STodd Fiala     {
48af245d11STodd Fiala         switch (stop_info.reason)
49af245d11STodd Fiala         {
5012fd3756SPavel Labath             case eStopReasonNone:
5112fd3756SPavel Labath                 log.Printf ("%s: %s no stop reason", __FUNCTION__, header);
5212fd3756SPavel Labath                 return;
5312fd3756SPavel Labath             case eStopReasonTrace:
5412fd3756SPavel Labath                 log.Printf ("%s: %s trace, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
5512fd3756SPavel Labath                 return;
5612fd3756SPavel Labath             case eStopReasonBreakpoint:
5712fd3756SPavel Labath                 log.Printf ("%s: %s breakpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
5812fd3756SPavel Labath                 return;
5912fd3756SPavel Labath             case eStopReasonWatchpoint:
6012fd3756SPavel Labath                 log.Printf ("%s: %s watchpoint, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
6112fd3756SPavel Labath                 return;
62af245d11STodd Fiala             case eStopReasonSignal:
63ae29d395SChaoren Lin                 log.Printf ("%s: %s signal 0x%02" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
64af245d11STodd Fiala                 return;
65af245d11STodd Fiala             case eStopReasonException:
66ae29d395SChaoren Lin                 log.Printf ("%s: %s exception type 0x%02" PRIx64, __FUNCTION__, header, stop_info.details.exception.type);
67a9882ceeSTodd Fiala                 return;
68a9882ceeSTodd Fiala             case eStopReasonExec:
69a9882ceeSTodd Fiala                 log.Printf ("%s: %s exec, stopping signal 0x%" PRIx32, __FUNCTION__, header, stop_info.details.signal.signo);
70af245d11STodd Fiala                 return;
7112fd3756SPavel Labath             case eStopReasonPlanComplete:
7212fd3756SPavel Labath                 log.Printf ("%s: %s plan complete", __FUNCTION__, header);
7312fd3756SPavel Labath                 return;
7412fd3756SPavel Labath             case eStopReasonThreadExiting:
7512fd3756SPavel Labath                 log.Printf ("%s: %s thread exiting", __FUNCTION__, header);
7612fd3756SPavel Labath                 return;
7712fd3756SPavel Labath             case eStopReasonInstrumentation:
7812fd3756SPavel Labath                 log.Printf ("%s: %s instrumentation", __FUNCTION__, header);
7912fd3756SPavel Labath                 return;
80af245d11STodd Fiala             default:
81a9882ceeSTodd Fiala                 log.Printf ("%s: %s invalid stop reason %" PRIu32, __FUNCTION__, header, static_cast<uint32_t> (stop_info.reason));
82af245d11STodd Fiala         }
83af245d11STodd Fiala     }
84af245d11STodd Fiala }
85af245d11STodd Fiala 
86af245d11STodd Fiala NativeThreadLinux::NativeThreadLinux (NativeProcessLinux *process, lldb::tid_t tid) :
87af245d11STodd Fiala     NativeThreadProtocol (process, tid),
88af245d11STodd Fiala     m_state (StateType::eStateInvalid),
89af245d11STodd Fiala     m_stop_info (),
9028e57429SChaoren Lin     m_reg_context_sp (),
9128e57429SChaoren Lin     m_stop_description ()
92af245d11STodd Fiala {
93af245d11STodd Fiala }
94af245d11STodd Fiala 
957206c6d1STodd Fiala std::string
96af245d11STodd Fiala NativeThreadLinux::GetName()
97af245d11STodd Fiala {
98af245d11STodd Fiala     NativeProcessProtocolSP process_sp = m_process_wp.lock ();
99af245d11STodd Fiala     if (!process_sp)
100af245d11STodd Fiala         return "<unknown: no process>";
101af245d11STodd Fiala 
102af245d11STodd Fiala     // const NativeProcessLinux *const process = reinterpret_cast<NativeProcessLinux*> (process_sp->get ());
10339de3110SZachary Turner     llvm::SmallString<32> thread_name;
10439de3110SZachary Turner     HostNativeThread::GetName(GetID(), thread_name);
10539de3110SZachary Turner     return thread_name.c_str();
106af245d11STodd Fiala }
107af245d11STodd Fiala 
108af245d11STodd Fiala lldb::StateType
109af245d11STodd Fiala NativeThreadLinux::GetState ()
110af245d11STodd Fiala {
111af245d11STodd Fiala     return m_state;
112af245d11STodd Fiala }
113af245d11STodd Fiala 
114af245d11STodd Fiala 
115af245d11STodd Fiala bool
11628e57429SChaoren Lin NativeThreadLinux::GetStopReason (ThreadStopInfo &stop_info, std::string& description)
117af245d11STodd Fiala {
118af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
11928e57429SChaoren Lin 
12028e57429SChaoren Lin     description.clear();
12128e57429SChaoren Lin 
122af245d11STodd Fiala     switch (m_state)
123af245d11STodd Fiala     {
124af245d11STodd Fiala     case eStateStopped:
125af245d11STodd Fiala     case eStateCrashed:
126af245d11STodd Fiala     case eStateExited:
127af245d11STodd Fiala     case eStateSuspended:
128af245d11STodd Fiala     case eStateUnloaded:
129af245d11STodd Fiala         if (log)
130af245d11STodd Fiala             LogThreadStopInfo (*log, m_stop_info, "m_stop_info in thread:");
131af245d11STodd Fiala         stop_info = m_stop_info;
13218fe6404SChaoren Lin         switch (m_stop_info.reason)
13318fe6404SChaoren Lin         {
13418fe6404SChaoren Lin             case StopReason::eStopReasonException:
13518fe6404SChaoren Lin             case StopReason::eStopReasonBreakpoint:
13618fe6404SChaoren Lin             case StopReason::eStopReasonWatchpoint:
13728e57429SChaoren Lin                 description = m_stop_description;
13818fe6404SChaoren Lin             default:
13918fe6404SChaoren Lin                 break;
14018fe6404SChaoren Lin         }
141af245d11STodd Fiala         if (log)
142af245d11STodd Fiala             LogThreadStopInfo (*log, stop_info, "returned stop_info:");
14328e57429SChaoren Lin 
144af245d11STodd Fiala         return true;
145af245d11STodd Fiala 
146af245d11STodd Fiala     case eStateInvalid:
147af245d11STodd Fiala     case eStateConnected:
148af245d11STodd Fiala     case eStateAttaching:
149af245d11STodd Fiala     case eStateLaunching:
150af245d11STodd Fiala     case eStateRunning:
151af245d11STodd Fiala     case eStateStepping:
152af245d11STodd Fiala     case eStateDetached:
153af245d11STodd Fiala         if (log)
154af245d11STodd Fiala         {
155af245d11STodd Fiala             log->Printf ("NativeThreadLinux::%s tid %" PRIu64 " in state %s cannot answer stop reason",
156af245d11STodd Fiala                     __FUNCTION__, GetID (), StateAsCString (m_state));
157af245d11STodd Fiala         }
158af245d11STodd Fiala         return false;
159af245d11STodd Fiala     }
1608faf9370SDavid Majnemer     llvm_unreachable("unhandled StateType!");
161af245d11STodd Fiala }
162af245d11STodd Fiala 
163db264a6dSTamas Berghammer NativeRegisterContextSP
164af245d11STodd Fiala NativeThreadLinux::GetRegisterContext ()
165af245d11STodd Fiala {
166af245d11STodd Fiala     // Return the register context if we already created it.
167af245d11STodd Fiala     if (m_reg_context_sp)
168af245d11STodd Fiala         return m_reg_context_sp;
169af245d11STodd Fiala 
170af245d11STodd Fiala     // First select the appropriate RegisterInfoInterface.
171af245d11STodd Fiala     RegisterInfoInterface *reg_interface = nullptr;
172af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
173af245d11STodd Fiala     if (!m_process_sp)
174af245d11STodd Fiala         return NativeRegisterContextSP ();
175af245d11STodd Fiala 
176af245d11STodd Fiala     ArchSpec target_arch;
177af245d11STodd Fiala     if (!m_process_sp->GetArchitecture (target_arch))
178af245d11STodd Fiala         return NativeRegisterContextSP ();
179af245d11STodd Fiala 
180af245d11STodd Fiala     switch (target_arch.GetTriple().getOS())
181af245d11STodd Fiala     {
182af245d11STodd Fiala         case llvm::Triple::Linux:
183af245d11STodd Fiala             switch (target_arch.GetMachine())
184af245d11STodd Fiala             {
185b71e89e9STodd Fiala             case llvm::Triple::aarch64:
186b71e89e9STodd Fiala                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8) && "Register setting path assumes this is a 64-bit host");
187b71e89e9STodd Fiala                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm64(target_arch));
188b71e89e9STodd Fiala                 break;
1893f57216cSOmair Javaid             case llvm::Triple::arm:
1903f57216cSOmair Javaid                 assert(HostInfo::GetArchitecture ().GetAddressByteSize() == 4);
1913f57216cSOmair Javaid                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_arm(target_arch));
1923f57216cSOmair Javaid                 break;
193af245d11STodd Fiala             case llvm::Triple::x86:
194af245d11STodd Fiala             case llvm::Triple::x86_64:
19513b18261SZachary Turner                 if (HostInfo::GetArchitecture().GetAddressByteSize() == 4)
196af245d11STodd Fiala                 {
197af245d11STodd Fiala                     // 32-bit hosts run with a RegisterContextLinux_i386 context.
198af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_i386(target_arch));
199af245d11STodd Fiala                 }
200af245d11STodd Fiala                 else
201af245d11STodd Fiala                 {
20213b18261SZachary Turner                     assert((HostInfo::GetArchitecture().GetAddressByteSize() == 8) &&
20313b18261SZachary Turner                            "Register setting path assumes this is a 64-bit host");
204af245d11STodd Fiala                     // X86_64 hosts know how to work with 64-bit and 32-bit EXEs using the x86_64 register context.
205af245d11STodd Fiala                     reg_interface = static_cast<RegisterInfoInterface*> (new RegisterContextLinux_x86_64 (target_arch));
206af245d11STodd Fiala                 }
207af245d11STodd Fiala                 break;
2083df471c3SMohit K. Bhakkad             case llvm::Triple::mips64:
2093df471c3SMohit K. Bhakkad             case llvm::Triple::mips64el:
2103df471c3SMohit K. Bhakkad                 assert((HostInfo::GetArchitecture ().GetAddressByteSize() == 8)
2113df471c3SMohit K. Bhakkad                     && "Register setting path assumes this is a 64-bit host");
2123df471c3SMohit K. Bhakkad                 reg_interface = static_cast<RegisterInfoInterface*>(new RegisterContextLinux_mips64 (target_arch));
2133df471c3SMohit K. Bhakkad                 break;
214af245d11STodd Fiala             default:
215af245d11STodd Fiala                 break;
216af245d11STodd Fiala             }
217af245d11STodd Fiala             break;
218af245d11STodd Fiala         default:
219af245d11STodd Fiala             break;
220af245d11STodd Fiala     }
221af245d11STodd Fiala 
222af245d11STodd Fiala     assert(reg_interface && "OS or CPU not supported!");
223af245d11STodd Fiala     if (!reg_interface)
224af245d11STodd Fiala         return NativeRegisterContextSP ();
225af245d11STodd Fiala 
226af245d11STodd Fiala     // Now create the register context.
227af245d11STodd Fiala     switch (target_arch.GetMachine())
228af245d11STodd Fiala     {
229af245d11STodd Fiala #if 0
230af245d11STodd Fiala         case llvm::Triple::mips64:
231af245d11STodd Fiala         {
232af245d11STodd Fiala             RegisterContextPOSIXProcessMonitor_mips64 *reg_ctx = new RegisterContextPOSIXProcessMonitor_mips64(*this, 0, reg_interface);
233af245d11STodd Fiala             m_posix_thread = reg_ctx;
234af245d11STodd Fiala             m_reg_context_sp.reset(reg_ctx);
235af245d11STodd Fiala             break;
236af245d11STodd Fiala         }
237af245d11STodd Fiala #endif
2383df471c3SMohit K. Bhakkad         case llvm::Triple::mips64:
2393df471c3SMohit K. Bhakkad         case llvm::Triple::mips64el:
2403df471c3SMohit K. Bhakkad         {
2413df471c3SMohit K. Bhakkad             const uint32_t concrete_frame_idx = 0;
2423df471c3SMohit K. Bhakkad             m_reg_context_sp.reset (new NativeRegisterContextLinux_mips64 (*this, concrete_frame_idx, reg_interface));
2433df471c3SMohit K. Bhakkad             break;
2443df471c3SMohit K. Bhakkad         }
2451e209fccSTamas Berghammer         case llvm::Triple::aarch64:
2461e209fccSTamas Berghammer         {
2471e209fccSTamas Berghammer             const uint32_t concrete_frame_idx = 0;
2481e209fccSTamas Berghammer             m_reg_context_sp.reset (new NativeRegisterContextLinux_arm64(*this, concrete_frame_idx, reg_interface));
2491e209fccSTamas Berghammer             break;
2501e209fccSTamas Berghammer         }
2513f57216cSOmair Javaid         case llvm::Triple::arm:
2523f57216cSOmair Javaid         {
2533f57216cSOmair Javaid             const uint32_t concrete_frame_idx = 0;
2543f57216cSOmair Javaid             m_reg_context_sp.reset (new NativeRegisterContextLinux_arm(*this, concrete_frame_idx, reg_interface));
2553f57216cSOmair Javaid             break;
2563f57216cSOmair Javaid         }
257af245d11STodd Fiala         case llvm::Triple::x86:
258af245d11STodd Fiala         case llvm::Triple::x86_64:
259af245d11STodd Fiala         {
260af245d11STodd Fiala             const uint32_t concrete_frame_idx = 0;
261af245d11STodd Fiala             m_reg_context_sp.reset (new NativeRegisterContextLinux_x86_64(*this, concrete_frame_idx, reg_interface));
262af245d11STodd Fiala             break;
263af245d11STodd Fiala         }
264af245d11STodd Fiala         default:
265af245d11STodd Fiala             break;
266af245d11STodd Fiala     }
267af245d11STodd Fiala 
268af245d11STodd Fiala     return m_reg_context_sp;
269af245d11STodd Fiala }
270af245d11STodd Fiala 
271af245d11STodd Fiala Error
272af245d11STodd Fiala NativeThreadLinux::SetWatchpoint (lldb::addr_t addr, size_t size, uint32_t watch_flags, bool hardware)
273af245d11STodd Fiala {
27418fe6404SChaoren Lin     if (!hardware)
275af245d11STodd Fiala         return Error ("not implemented");
276f591f69fSChaoren Lin     if (m_state == eStateLaunching)
277f591f69fSChaoren Lin         return Error ();
27818fe6404SChaoren Lin     Error error = RemoveWatchpoint(addr);
27918fe6404SChaoren Lin     if (error.Fail()) return error;
28018fe6404SChaoren Lin     NativeRegisterContextSP reg_ctx = GetRegisterContext ();
28118fe6404SChaoren Lin     uint32_t wp_index =
28218fe6404SChaoren Lin         reg_ctx->SetHardwareWatchpoint (addr, size, watch_flags);
28318fe6404SChaoren Lin     if (wp_index == LLDB_INVALID_INDEX32)
28418fe6404SChaoren Lin         return Error ("Setting hardware watchpoint failed.");
28518fe6404SChaoren Lin     m_watchpoint_index_map.insert({addr, wp_index});
28618fe6404SChaoren Lin     return Error ();
287af245d11STodd Fiala }
288af245d11STodd Fiala 
289af245d11STodd Fiala Error
290af245d11STodd Fiala NativeThreadLinux::RemoveWatchpoint (lldb::addr_t addr)
291af245d11STodd Fiala {
29218fe6404SChaoren Lin     auto wp = m_watchpoint_index_map.find(addr);
29318fe6404SChaoren Lin     if (wp == m_watchpoint_index_map.end())
29418fe6404SChaoren Lin         return Error ();
29518fe6404SChaoren Lin     uint32_t wp_index = wp->second;
29618fe6404SChaoren Lin     m_watchpoint_index_map.erase(wp);
29718fe6404SChaoren Lin     if (GetRegisterContext()->ClearHardwareWatchpoint(wp_index))
29818fe6404SChaoren Lin         return Error ();
29918fe6404SChaoren Lin     return Error ("Clearing hardware watchpoint failed.");
300af245d11STodd Fiala }
301af245d11STodd Fiala 
302af245d11STodd Fiala void
303af245d11STodd Fiala NativeThreadLinux::SetLaunching ()
304af245d11STodd Fiala {
305af245d11STodd Fiala     const StateType new_state = StateType::eStateLaunching;
306af245d11STodd Fiala     MaybeLogStateChange (new_state);
307af245d11STodd Fiala     m_state = new_state;
308af245d11STodd Fiala 
309af245d11STodd Fiala     // Also mark it as stopped since launching temporarily stops the newly created thread
310af245d11STodd Fiala     // in the ptrace machinery.
311af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
312af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
313af245d11STodd Fiala }
314af245d11STodd Fiala 
315af245d11STodd Fiala 
316af245d11STodd Fiala void
317af245d11STodd Fiala NativeThreadLinux::SetRunning ()
318af245d11STodd Fiala {
319af245d11STodd Fiala     const StateType new_state = StateType::eStateRunning;
320af245d11STodd Fiala     MaybeLogStateChange (new_state);
321af245d11STodd Fiala     m_state = new_state;
322af245d11STodd Fiala 
323af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
32428e57429SChaoren Lin     m_stop_description.clear();
32518fe6404SChaoren Lin 
32618fe6404SChaoren Lin     // If watchpoints have been set, but none on this thread,
32718fe6404SChaoren Lin     // then this is a new thread. So set all existing watchpoints.
32818fe6404SChaoren Lin     if (m_watchpoint_index_map.empty())
32918fe6404SChaoren Lin     {
3308bc34f4dSOleksiy Vyalov         const auto process_sp = GetProcess();
3318bc34f4dSOleksiy Vyalov         if (process_sp)
3328bc34f4dSOleksiy Vyalov         {
3338bc34f4dSOleksiy Vyalov             const auto &watchpoint_map = process_sp->GetWatchpointMap();
33418fe6404SChaoren Lin             if (watchpoint_map.empty()) return;
33518fe6404SChaoren Lin             GetRegisterContext()->ClearAllHardwareWatchpoints();
33618fe6404SChaoren Lin             for (const auto &pair : watchpoint_map)
33718fe6404SChaoren Lin             {
33818fe6404SChaoren Lin                 const auto& wp = pair.second;
33918fe6404SChaoren Lin                 SetWatchpoint(wp.m_addr, wp.m_size, wp.m_watch_flags, wp.m_hardware);
34018fe6404SChaoren Lin             }
34118fe6404SChaoren Lin         }
342af245d11STodd Fiala     }
3438bc34f4dSOleksiy Vyalov }
344af245d11STodd Fiala 
345af245d11STodd Fiala void
346af245d11STodd Fiala NativeThreadLinux::SetStepping ()
347af245d11STodd Fiala {
348af245d11STodd Fiala     const StateType new_state = StateType::eStateStepping;
349af245d11STodd Fiala     MaybeLogStateChange (new_state);
350af245d11STodd Fiala     m_state = new_state;
351af245d11STodd Fiala 
352af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
353af245d11STodd Fiala }
354af245d11STodd Fiala 
355af245d11STodd Fiala void
356af245d11STodd Fiala NativeThreadLinux::SetStoppedBySignal (uint32_t signo)
357af245d11STodd Fiala {
358af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
359af245d11STodd Fiala     if (log)
360b8af31d4SChaoren Lin         log->Printf ("NativeThreadLinux::%s called with signal 0x%02" PRIx32, __FUNCTION__, signo);
361af245d11STodd Fiala 
362af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
363af245d11STodd Fiala     MaybeLogStateChange (new_state);
364af245d11STodd Fiala     m_state = new_state;
365af245d11STodd Fiala 
366af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonSignal;
367af245d11STodd Fiala     m_stop_info.details.signal.signo = signo;
368af245d11STodd Fiala }
369af245d11STodd Fiala 
370511e5cdcSTodd Fiala bool
371511e5cdcSTodd Fiala NativeThreadLinux::IsStopped (int *signo)
372511e5cdcSTodd Fiala {
373511e5cdcSTodd Fiala     if (!StateIsStoppedState (m_state, false))
374511e5cdcSTodd Fiala         return false;
375511e5cdcSTodd Fiala 
376511e5cdcSTodd Fiala     // If we are stopped by a signal, return the signo.
377511e5cdcSTodd Fiala     if (signo &&
378511e5cdcSTodd Fiala         m_state == StateType::eStateStopped &&
379511e5cdcSTodd Fiala         m_stop_info.reason == StopReason::eStopReasonSignal)
380511e5cdcSTodd Fiala     {
381511e5cdcSTodd Fiala         *signo = m_stop_info.details.signal.signo;
382511e5cdcSTodd Fiala     }
383511e5cdcSTodd Fiala 
384511e5cdcSTodd Fiala     // Regardless, we are stopped.
385511e5cdcSTodd Fiala     return true;
386511e5cdcSTodd Fiala }
387511e5cdcSTodd Fiala 
388511e5cdcSTodd Fiala 
389af245d11STodd Fiala void
390a9882ceeSTodd Fiala NativeThreadLinux::SetStoppedByExec ()
391a9882ceeSTodd Fiala {
392a9882ceeSTodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
393a9882ceeSTodd Fiala     if (log)
394a9882ceeSTodd Fiala         log->Printf ("NativeThreadLinux::%s()", __FUNCTION__);
395a9882ceeSTodd Fiala 
396a9882ceeSTodd Fiala     const StateType new_state = StateType::eStateStopped;
397a9882ceeSTodd Fiala     MaybeLogStateChange (new_state);
398a9882ceeSTodd Fiala     m_state = new_state;
399a9882ceeSTodd Fiala 
400a9882ceeSTodd Fiala     m_stop_info.reason = StopReason::eStopReasonExec;
401a9882ceeSTodd Fiala     m_stop_info.details.signal.signo = SIGSTOP;
402a9882ceeSTodd Fiala }
403a9882ceeSTodd Fiala 
404a9882ceeSTodd Fiala void
405af245d11STodd Fiala NativeThreadLinux::SetStoppedByBreakpoint ()
406af245d11STodd Fiala {
407af245d11STodd Fiala     const StateType new_state = StateType::eStateStopped;
408af245d11STodd Fiala     MaybeLogStateChange (new_state);
409af245d11STodd Fiala     m_state = new_state;
410af245d11STodd Fiala 
41128e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonBreakpoint;
412af245d11STodd Fiala     m_stop_info.details.signal.signo = SIGTRAP;
41318fe6404SChaoren Lin     m_stop_description.clear();
41418fe6404SChaoren Lin }
41518fe6404SChaoren Lin 
41618fe6404SChaoren Lin void
417c16f5dcaSChaoren Lin NativeThreadLinux::SetStoppedByWatchpoint (uint32_t wp_index)
41818fe6404SChaoren Lin {
41918fe6404SChaoren Lin     const StateType new_state = StateType::eStateStopped;
42018fe6404SChaoren Lin     MaybeLogStateChange (new_state);
42118fe6404SChaoren Lin     m_state = new_state;
42218fe6404SChaoren Lin     m_stop_description.clear ();
423c16f5dcaSChaoren Lin 
424c16f5dcaSChaoren Lin     lldbassert(wp_index != LLDB_INVALID_INDEX32 &&
425c16f5dcaSChaoren Lin                "wp_index cannot be invalid");
426eadb2a9eSTamas Berghammer 
42718fe6404SChaoren Lin     std::ostringstream ostr;
428c16f5dcaSChaoren Lin     ostr << GetRegisterContext()->GetWatchpointAddress(wp_index) << " ";
429c16f5dcaSChaoren Lin     ostr << wp_index;
43018fe6404SChaoren Lin     m_stop_description = ostr.str();
431eadb2a9eSTamas Berghammer 
432eadb2a9eSTamas Berghammer     m_stop_info.reason = StopReason::eStopReasonWatchpoint;
433eadb2a9eSTamas Berghammer     m_stop_info.details.signal.signo = SIGTRAP;
434af245d11STodd Fiala }
435af245d11STodd Fiala 
436af245d11STodd Fiala bool
437af245d11STodd Fiala NativeThreadLinux::IsStoppedAtBreakpoint ()
438af245d11STodd Fiala {
43918fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
44018fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonBreakpoint;
44118fe6404SChaoren Lin }
442af245d11STodd Fiala 
44318fe6404SChaoren Lin bool
44418fe6404SChaoren Lin NativeThreadLinux::IsStoppedAtWatchpoint ()
44518fe6404SChaoren Lin {
44618fe6404SChaoren Lin     return GetState () == StateType::eStateStopped &&
44718fe6404SChaoren Lin         m_stop_info.reason == StopReason::eStopReasonWatchpoint;
448af245d11STodd Fiala }
449af245d11STodd Fiala 
450af245d11STodd Fiala void
45128e57429SChaoren Lin NativeThreadLinux::SetStoppedByTrace ()
45228e57429SChaoren Lin {
45328e57429SChaoren Lin     const StateType new_state = StateType::eStateStopped;
45428e57429SChaoren Lin     MaybeLogStateChange (new_state);
45528e57429SChaoren Lin     m_state = new_state;
45628e57429SChaoren Lin 
45728e57429SChaoren Lin     m_stop_info.reason = StopReason::eStopReasonTrace;
45828e57429SChaoren Lin     m_stop_info.details.signal.signo = SIGTRAP;
45928e57429SChaoren Lin }
46028e57429SChaoren Lin 
46128e57429SChaoren Lin void
46228e57429SChaoren Lin NativeThreadLinux::SetCrashedWithException (const siginfo_t& info)
463af245d11STodd Fiala {
464af245d11STodd Fiala     const StateType new_state = StateType::eStateCrashed;
465af245d11STodd Fiala     MaybeLogStateChange (new_state);
466af245d11STodd Fiala     m_state = new_state;
467af245d11STodd Fiala 
468af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonException;
46928e57429SChaoren Lin     m_stop_info.details.signal.signo = info.si_signo;
470af245d11STodd Fiala 
47128e57429SChaoren Lin     const auto reason = GetCrashReason (info);
472*1fab7b97STamas Berghammer     m_stop_description = GetCrashReasonString (reason, reinterpret_cast<uintptr_t>(info.si_addr));
47328e57429SChaoren Lin }
474af245d11STodd Fiala 
475af245d11STodd Fiala void
476af245d11STodd Fiala NativeThreadLinux::SetSuspended ()
477af245d11STodd Fiala {
478af245d11STodd Fiala     const StateType new_state = StateType::eStateSuspended;
479af245d11STodd Fiala     MaybeLogStateChange (new_state);
480af245d11STodd Fiala     m_state = new_state;
481af245d11STodd Fiala 
482af245d11STodd Fiala     // FIXME what makes sense here? Do we need a suspended StopReason?
483af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonNone;
484af245d11STodd Fiala }
485af245d11STodd Fiala 
486af245d11STodd Fiala void
487af245d11STodd Fiala NativeThreadLinux::SetExited ()
488af245d11STodd Fiala {
489af245d11STodd Fiala     const StateType new_state = StateType::eStateExited;
490af245d11STodd Fiala     MaybeLogStateChange (new_state);
491af245d11STodd Fiala     m_state = new_state;
492af245d11STodd Fiala 
493af245d11STodd Fiala     m_stop_info.reason = StopReason::eStopReasonThreadExiting;
494af245d11STodd Fiala }
495af245d11STodd Fiala 
496af245d11STodd Fiala void
497af245d11STodd Fiala NativeThreadLinux::MaybeLogStateChange (lldb::StateType new_state)
498af245d11STodd Fiala {
499af245d11STodd Fiala     Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_THREAD));
500af245d11STodd Fiala     // If we're not logging, we're done.
501af245d11STodd Fiala     if (!log)
502af245d11STodd Fiala         return;
503af245d11STodd Fiala 
504af245d11STodd Fiala     // If this is a state change to the same state, we're done.
505af245d11STodd Fiala     lldb::StateType old_state = m_state;
506af245d11STodd Fiala     if (new_state == old_state)
507af245d11STodd Fiala         return;
508af245d11STodd Fiala 
509af245d11STodd Fiala     NativeProcessProtocolSP m_process_sp = m_process_wp.lock ();
510af245d11STodd Fiala     lldb::pid_t pid = m_process_sp ? m_process_sp->GetID () : LLDB_INVALID_PROCESS_ID;
511af245d11STodd Fiala 
512af245d11STodd Fiala     // Log it.
513af245d11STodd Fiala     log->Printf ("NativeThreadLinux: thread (pid=%" PRIu64 ", tid=%" PRIu64 ") changing from state %s to %s", pid, GetID (), StateAsCString (old_state), StateAsCString (new_state));
514af245d11STodd Fiala }
515