blob: 562ba8b0a16c51c3e526f3530814956f89de3c0a [file] [log] [blame]
Greg Claytona63d08c2011-07-19 03:57:15 +00001//===-- ThreadKDP.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
11#include "ThreadKDP.h"
12
13#include "llvm/Support/MachO.h"
14
15#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataExtractor.h"
17#include "lldb/Core/StreamString.h"
18#include "lldb/Core/State.h"
19#include "lldb/Target/Process.h"
20#include "lldb/Target/RegisterContext.h"
21#include "lldb/Target/StopInfo.h"
22#include "lldb/Target/Target.h"
23#include "lldb/Target/Unwind.h"
Johnny Chen01a67862011-10-14 00:42:25 +000024#include "lldb/Breakpoint/Watchpoint.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000025
26#include "ProcessKDP.h"
27#include "ProcessKDPLog.h"
28#include "RegisterContextKDP_arm.h"
Jason Molendaa3329782014-03-29 18:54:20 +000029#include "RegisterContextKDP_arm64.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000030#include "RegisterContextKDP_i386.h"
31#include "RegisterContextKDP_x86_64.h"
Greg Clayton97d5cf02012-09-25 02:40:06 +000032#include "Plugins/Process/Utility/StopInfoMachException.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000033
34using namespace lldb;
35using namespace lldb_private;
36
37//----------------------------------------------------------------------
38// Thread Registers
39//----------------------------------------------------------------------
40
Jim Ingham4f465cf2012-10-10 18:32:14 +000041ThreadKDP::ThreadKDP (Process &process, lldb::tid_t tid) :
42 Thread(process, tid),
Greg Claytona63d08c2011-07-19 03:57:15 +000043 m_thread_name (),
44 m_dispatch_queue_name (),
45 m_thread_dispatch_qaddr (LLDB_INVALID_ADDRESS)
46{
Greg Clayton1ac04c32012-02-21 00:09:25 +000047 ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this, GetID());
Greg Claytona63d08c2011-07-19 03:57:15 +000048}
49
50ThreadKDP::~ThreadKDP ()
51{
Greg Clayton1ac04c32012-02-21 00:09:25 +000052 ProcessKDPLog::LogIf(KDP_LOG_THREAD, "%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this, GetID());
Greg Claytona63d08c2011-07-19 03:57:15 +000053 DestroyThread();
54}
55
Greg Claytona63d08c2011-07-19 03:57:15 +000056const char *
57ThreadKDP::GetName ()
58{
59 if (m_thread_name.empty())
60 return NULL;
61 return m_thread_name.c_str();
62}
63
64const char *
65ThreadKDP::GetQueueName ()
66{
67 return NULL;
68}
69
Greg Claytona63d08c2011-07-19 03:57:15 +000070void
71ThreadKDP::RefreshStateAfterStop()
72{
73 // Invalidate all registers in our register context. We don't set "force" to
74 // true because the stop reply packet might have had some register values
75 // that were expedited and these will already be copied into the register
76 // context by the time this function gets called. The KDPRegisterContext
77 // class has been made smart enough to detect when it needs to invalidate
78 // which registers are valid by putting hooks in the register read and
79 // register supply functions where they check the process stop ID and do
80 // the right thing.
81 const bool force = false;
Greg Clayton1afa68e2013-04-02 20:32:37 +000082 lldb::RegisterContextSP reg_ctx_sp (GetRegisterContext());
83 if (reg_ctx_sp)
84 reg_ctx_sp->InvalidateIfNeeded (force);
Greg Claytona63d08c2011-07-19 03:57:15 +000085}
86
Greg Claytona63d08c2011-07-19 03:57:15 +000087bool
88ThreadKDP::ThreadIDIsValid (lldb::tid_t thread)
89{
90 return thread != 0;
91}
92
93void
94ThreadKDP::Dump(Log *log, uint32_t index)
95{
96}
97
98
99bool
100ThreadKDP::ShouldStop (bool &step_more)
101{
102 return true;
103}
104lldb::RegisterContextSP
105ThreadKDP::GetRegisterContext ()
106{
107 if (m_reg_context_sp.get() == NULL)
108 m_reg_context_sp = CreateRegisterContextForFrame (NULL);
109 return m_reg_context_sp;
110}
111
112lldb::RegisterContextSP
Jason Molendab57e4a12013-11-04 09:33:30 +0000113ThreadKDP::CreateRegisterContextForFrame (StackFrame *frame)
Greg Claytona63d08c2011-07-19 03:57:15 +0000114{
115 lldb::RegisterContextSP reg_ctx_sp;
116 uint32_t concrete_frame_idx = 0;
117
118 if (frame)
119 concrete_frame_idx = frame->GetConcreteFrameIndex ();
120
121 if (concrete_frame_idx == 0)
122 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000123 ProcessSP process_sp (CalculateProcess());
124 if (process_sp)
Greg Claytona63d08c2011-07-19 03:57:15 +0000125 {
Greg Clayton1ac04c32012-02-21 00:09:25 +0000126 switch (static_cast<ProcessKDP *>(process_sp.get())->GetCommunication().GetCPUType())
127 {
Charles Davis510938e2013-08-27 05:04:57 +0000128 case llvm::MachO::CPU_TYPE_ARM:
Greg Clayton1ac04c32012-02-21 00:09:25 +0000129 reg_ctx_sp.reset (new RegisterContextKDP_arm (*this, concrete_frame_idx));
130 break;
Jason Molendaa3329782014-03-29 18:54:20 +0000131 case llvm::MachO::CPU_TYPE_ARM64:
132 reg_ctx_sp.reset (new RegisterContextKDP_arm64 (*this, concrete_frame_idx));
133 break;
Charles Davis510938e2013-08-27 05:04:57 +0000134 case llvm::MachO::CPU_TYPE_I386:
Greg Clayton1ac04c32012-02-21 00:09:25 +0000135 reg_ctx_sp.reset (new RegisterContextKDP_i386 (*this, concrete_frame_idx));
136 break;
Charles Davis510938e2013-08-27 05:04:57 +0000137 case llvm::MachO::CPU_TYPE_X86_64:
Greg Clayton1ac04c32012-02-21 00:09:25 +0000138 reg_ctx_sp.reset (new RegisterContextKDP_x86_64 (*this, concrete_frame_idx));
139 break;
140 default:
141 assert (!"Add CPU type support in KDP");
142 break;
143 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000144 }
145 }
Greg Claytonb3ae8762013-04-12 20:07:46 +0000146 else
147 {
148 Unwind *unwinder = GetUnwinder ();
149 if (unwinder)
150 reg_ctx_sp = unwinder->CreateRegisterContextForFrame (frame);
151 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000152 return reg_ctx_sp;
153}
154
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000155bool
156ThreadKDP::CalculateStopInfo ()
Greg Claytona63d08c2011-07-19 03:57:15 +0000157{
Greg Clayton1ac04c32012-02-21 00:09:25 +0000158 ProcessSP process_sp (GetProcess());
159 if (process_sp)
Greg Claytona63d08c2011-07-19 03:57:15 +0000160 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000161 if (m_cached_stop_info_sp)
Greg Clayton1ac04c32012-02-21 00:09:25 +0000162 {
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000163 SetStopInfo (m_cached_stop_info_sp);
Greg Clayton1ac04c32012-02-21 00:09:25 +0000164 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000165 else
166 {
167 SetStopInfo(StopInfo::CreateStopReasonWithSignal (*this, SIGSTOP));
168 }
169 return true;
Greg Claytona63d08c2011-07-19 03:57:15 +0000170 }
Greg Clayton6e0ff1a2013-05-09 01:55:29 +0000171 return false;
Greg Claytona63d08c2011-07-19 03:57:15 +0000172}
173
Greg Clayton97d5cf02012-09-25 02:40:06 +0000174void
175ThreadKDP::SetStopInfoFrom_KDP_EXCEPTION (const DataExtractor &exc_reply_packet)
176{
Greg Claytonc7bece562013-01-25 18:06:21 +0000177 lldb::offset_t offset = 0;
Greg Clayton97d5cf02012-09-25 02:40:06 +0000178 uint8_t reply_command = exc_reply_packet.GetU8(&offset);
179 if (reply_command == CommunicationKDP::KDP_EXCEPTION)
180 {
181 offset = 8;
182 const uint32_t count = exc_reply_packet.GetU32 (&offset);
183 if (count >= 1)
184 {
185 //const uint32_t cpu = exc_reply_packet.GetU32 (&offset);
186 offset += 4; // Skip the useless CPU field
187 const uint32_t exc_type = exc_reply_packet.GetU32 (&offset);
188 const uint32_t exc_code = exc_reply_packet.GetU32 (&offset);
189 const uint32_t exc_subcode = exc_reply_packet.GetU32 (&offset);
190 // We have to make a copy of the stop info because the thread list
191 // will iterate through the threads and clear all stop infos..
192
193 // Let the StopInfoMachException::CreateStopReasonWithMachException()
194 // function update the PC if needed as we might hit a software breakpoint
195 // and need to decrement the PC (i386 and x86_64 need this) and KDP
196 // doesn't do this for us.
197 const bool pc_already_adjusted = false;
198 const bool adjust_pc_if_needed = true;
199
200 m_cached_stop_info_sp = StopInfoMachException::CreateStopReasonWithMachException (*this,
201 exc_type,
202 2,
203 exc_code,
204 exc_subcode,
205 0,
206 pc_already_adjusted,
207 adjust_pc_if_needed);
208 }
209 }
210}
Greg Claytona63d08c2011-07-19 03:57:15 +0000211