Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 1 | //===-- LinuxThread.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 | |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 10 | // C Includes |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 11 | #include <errno.h> |
| 12 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 13 | // C++ Includes |
| 14 | // Other libraries and framework includes |
| 15 | #include "lldb/Target/Process.h" |
Stephen Wilson | c8c74e0 | 2011-01-04 21:43:19 +0000 | [diff] [blame] | 16 | #include "lldb/Target/StopInfo.h" |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 17 | #include "lldb/Target/Target.h" |
| 18 | |
| 19 | #include "LinuxThread.h" |
| 20 | #include "ProcessLinux.h" |
| 21 | #include "ProcessMonitor.h" |
| 22 | #include "RegisterContextLinux_x86_64.h" |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 23 | #include "UnwindLLDB.h" |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 24 | |
| 25 | using namespace lldb_private; |
| 26 | |
| 27 | LinuxThread::LinuxThread(Process &process, lldb::tid_t tid) |
| 28 | : Thread(process, tid), |
| 29 | m_frame_ap(0), |
Stephen Wilson | 140f69a | 2011-01-15 00:07:36 +0000 | [diff] [blame] | 30 | m_stop_info_id(0), |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 31 | m_note(eNone) |
| 32 | { |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Stephen Wilson | b47794a | 2011-01-16 16:56:16 +0000 | [diff] [blame] | 35 | LinuxThread::~LinuxThread() |
| 36 | { |
| 37 | DestroyThread(); |
| 38 | } |
| 39 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 40 | ProcessMonitor & |
| 41 | LinuxThread::GetMonitor() |
| 42 | { |
Stephen Wilson | bc1418b | 2011-01-07 00:10:43 +0000 | [diff] [blame] | 43 | ProcessLinux &process = static_cast<ProcessLinux&>(GetProcess()); |
| 44 | return process.GetMonitor(); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | void |
| 48 | LinuxThread::RefreshStateAfterStop() |
| 49 | { |
Stephen Wilson | 140f69a | 2011-01-15 00:07:36 +0000 | [diff] [blame] | 50 | RefreshPrivateStopReason(); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | const char * |
| 54 | LinuxThread::GetInfo() |
| 55 | { |
| 56 | return NULL; |
| 57 | } |
| 58 | |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 59 | lldb::RegisterContextSP |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 60 | LinuxThread::GetRegisterContext() |
| 61 | { |
Stephen Wilson | bc1418b | 2011-01-07 00:10:43 +0000 | [diff] [blame] | 62 | ProcessLinux &process = static_cast<ProcessLinux&>(GetProcess()); |
| 63 | |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 64 | if (!m_reg_context_sp) |
| 65 | { |
| 66 | ArchSpec arch = process.GetTarget().GetArchitecture(); |
| 67 | |
| 68 | switch (arch.GetGenericCPUType()) |
| 69 | { |
| 70 | default: |
| 71 | assert(false && "CPU type not supported!"); |
| 72 | break; |
| 73 | |
| 74 | case ArchSpec::eCPU_x86_64: |
| 75 | m_reg_context_sp.reset(new RegisterContextLinux_x86_64(*this, 0)); |
| 76 | break; |
| 77 | } |
| 78 | } |
Stephen Wilson | bc1418b | 2011-01-07 00:10:43 +0000 | [diff] [blame] | 79 | return m_reg_context_sp; |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | bool |
| 83 | LinuxThread::SaveFrameZeroState(RegisterCheckpoint &checkpoint) |
| 84 | { |
| 85 | return false; |
| 86 | } |
| 87 | |
| 88 | bool |
| 89 | LinuxThread::RestoreSaveFrameZero(const RegisterCheckpoint &checkpoint) |
| 90 | { |
| 91 | return false; |
| 92 | } |
| 93 | |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 94 | lldb::RegisterContextSP |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 95 | LinuxThread::CreateRegisterContextForFrame(lldb_private::StackFrame *frame) |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 96 | { |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 97 | lldb::RegisterContextSP reg_ctx_sp; |
| 98 | uint32_t concrete_frame_idx = 0; |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 99 | |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 100 | if (frame) |
| 101 | concrete_frame_idx = frame->GetConcreteFrameIndex(); |
| 102 | |
| 103 | if (concrete_frame_idx == 0) |
| 104 | reg_ctx_sp = GetRegisterContext(); |
| 105 | else |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 106 | reg_ctx_sp = GetUnwinder()->CreateRegisterContextForFrame(frame); |
| 107 | |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 108 | return reg_ctx_sp; |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Stephen Wilson | ed56002 | 2011-01-04 21:45:02 +0000 | [diff] [blame] | 111 | lldb::StopInfoSP |
| 112 | LinuxThread::GetPrivateStopReason() |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 113 | { |
Stephen Wilson | 140f69a | 2011-01-15 00:07:36 +0000 | [diff] [blame] | 114 | const uint32_t process_stop_id = GetProcess().GetStopID(); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 115 | |
Stephen Wilson | 140f69a | 2011-01-15 00:07:36 +0000 | [diff] [blame] | 116 | if (m_stop_info_id != process_stop_id || !m_stop_info || !m_stop_info->IsValid()) |
| 117 | RefreshPrivateStopReason(); |
| 118 | return m_stop_info; |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 119 | } |
| 120 | |
Stephen Wilson | d61182d | 2011-01-04 21:45:57 +0000 | [diff] [blame] | 121 | Unwind * |
| 122 | LinuxThread::GetUnwinder() |
| 123 | { |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 124 | if (m_unwinder_ap.get() == NULL) |
| 125 | m_unwinder_ap.reset(new UnwindLLDB(*this)); |
| 126 | |
Stephen Wilson | d61182d | 2011-01-04 21:45:57 +0000 | [diff] [blame] | 127 | return m_unwinder_ap.get(); |
| 128 | } |
| 129 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 130 | bool |
| 131 | LinuxThread::WillResume(lldb::StateType resume_state) |
| 132 | { |
| 133 | SetResumeState(resume_state); |
Stephen Wilson | e0c20da | 2011-01-19 01:36:10 +0000 | [diff] [blame] | 134 | |
| 135 | ClearStackFrames(); |
| 136 | if (m_unwinder_ap.get()) |
| 137 | m_unwinder_ap->Clear(); |
| 138 | |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 139 | return Thread::WillResume(resume_state); |
| 140 | } |
| 141 | |
| 142 | bool |
| 143 | LinuxThread::Resume() |
| 144 | { |
| 145 | lldb::StateType resume_state = GetResumeState(); |
| 146 | ProcessMonitor &monitor = GetMonitor(); |
| 147 | bool status; |
| 148 | |
| 149 | switch (GetResumeState()) |
| 150 | { |
| 151 | default: |
| 152 | assert(false && "Unexpected state for resume!"); |
| 153 | status = false; |
| 154 | break; |
| 155 | |
| 156 | case lldb::eStateSuspended: |
| 157 | // FIXME: Implement process suspension. |
| 158 | status = false; |
| 159 | |
| 160 | case lldb::eStateRunning: |
| 161 | SetState(resume_state); |
| 162 | status = monitor.Resume(GetID()); |
| 163 | break; |
| 164 | |
| 165 | case lldb::eStateStepping: |
| 166 | SetState(resume_state); |
Stephen Wilson | f3cbdc2 | 2011-01-19 01:35:00 +0000 | [diff] [blame] | 167 | status = monitor.SingleStep(GetID()); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 168 | break; |
| 169 | } |
| 170 | |
| 171 | m_note = eNone; |
| 172 | return status; |
| 173 | } |
| 174 | |
| 175 | void |
| 176 | LinuxThread::BreakNotify() |
| 177 | { |
| 178 | bool status; |
| 179 | |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 180 | status = GetRegisterContextLinux()->UpdateAfterBreakpoint(); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 181 | assert(status && "Breakpoint update failed!"); |
| 182 | |
| 183 | // With our register state restored, resolve the breakpoint object |
| 184 | // corresponding to our current PC. |
| 185 | lldb::addr_t pc = GetRegisterContext()->GetPC(); |
Greg Clayton | 90ccf88 | 2011-01-06 22:35:55 +0000 | [diff] [blame] | 186 | lldb::BreakpointSiteSP bp_site(GetProcess().GetBreakpointSiteList().FindByAddress(pc)); |
Stephen Wilson | f6f4033 | 2010-07-24 02:19:04 +0000 | [diff] [blame] | 187 | assert(bp_site && bp_site->ValidForThisThread(this)); |
| 188 | |
| 189 | m_note = eBreak; |
| 190 | m_breakpoint = bp_site; |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | LinuxThread::TraceNotify() |
| 195 | { |
| 196 | m_note = eTrace; |
| 197 | } |
| 198 | |
| 199 | void |
| 200 | LinuxThread::ExitNotify() |
| 201 | { |
| 202 | m_note = eExit; |
| 203 | } |
Stephen Wilson | 140f69a | 2011-01-15 00:07:36 +0000 | [diff] [blame] | 204 | |
| 205 | void |
| 206 | LinuxThread::RefreshPrivateStopReason() |
| 207 | { |
| 208 | m_stop_info_id = GetProcess().GetStopID(); |
| 209 | |
| 210 | switch (m_note) { |
| 211 | |
| 212 | default: |
| 213 | case eNone: |
| 214 | m_stop_info.reset(); |
| 215 | break; |
| 216 | |
| 217 | case eBreak: |
| 218 | m_stop_info = StopInfo::CreateStopReasonWithBreakpointSiteID( |
| 219 | *this, m_breakpoint->GetID()); |
| 220 | break; |
| 221 | |
| 222 | case eTrace: |
| 223 | m_stop_info = StopInfo::CreateStopReasonToTrace(*this); |
| 224 | break; |
| 225 | |
| 226 | case eExit: |
| 227 | m_stop_info = StopInfo::CreateStopReasonWithSignal(*this, SIGCHLD); |
| 228 | break; |
| 229 | } |
| 230 | } |