blob: 9f594f35d70deb753c9943cfc521a1fa1c8056fe [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
Greg Claytona63d08c2011-07-19 03:57:15 +000010#include "ThreadKDP.h"
11
Jim Ingham46d005d2014-04-02 22:53:21 +000012#include "lldb/Utility/SafeMachO.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000013
Kate Stoneb9c1b512016-09-06 20:57:50 +000014#include "lldb/Breakpoint/Watchpoint.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000015#include "lldb/Core/ArchSpec.h"
16#include "lldb/Core/DataExtractor.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000017#include "lldb/Core/State.h"
Kate Stoneb9c1b512016-09-06 20:57:50 +000018#include "lldb/Core/StreamString.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000019#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"
Greg Claytona63d08c2011-07-19 03:57:15 +000024
Kate Stoneb9c1b512016-09-06 20:57:50 +000025#include "Plugins/Process/Utility/StopInfoMachException.h"
Greg Claytona63d08c2011-07-19 03:57:15 +000026#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 Claytona63d08c2011-07-19 03:57:15 +000032
33using namespace lldb;
34using namespace lldb_private;
35
36//----------------------------------------------------------------------
37// Thread Registers
38//----------------------------------------------------------------------
39
Kate Stoneb9c1b512016-09-06 20:57:50 +000040ThreadKDP::ThreadKDP(Process &process, lldb::tid_t tid)
41 : Thread(process, tid), m_thread_name(), m_dispatch_queue_name(),
42 m_thread_dispatch_qaddr(LLDB_INVALID_ADDRESS) {
43 ProcessKDPLog::LogIf(KDP_LOG_THREAD,
44 "%p: ThreadKDP::ThreadKDP (tid = 0x%4.4x)", this,
45 GetID());
Greg Claytona63d08c2011-07-19 03:57:15 +000046}
47
Kate Stoneb9c1b512016-09-06 20:57:50 +000048ThreadKDP::~ThreadKDP() {
49 ProcessKDPLog::LogIf(KDP_LOG_THREAD,
50 "%p: ThreadKDP::~ThreadKDP (tid = 0x%4.4x)", this,
51 GetID());
52 DestroyThread();
Greg Claytona63d08c2011-07-19 03:57:15 +000053}
54
Kate Stoneb9c1b512016-09-06 20:57:50 +000055const char *ThreadKDP::GetName() {
56 if (m_thread_name.empty())
Greg Claytona63d08c2011-07-19 03:57:15 +000057 return NULL;
Kate Stoneb9c1b512016-09-06 20:57:50 +000058 return m_thread_name.c_str();
Greg Claytona63d08c2011-07-19 03:57:15 +000059}
60
Kate Stoneb9c1b512016-09-06 20:57:50 +000061const char *ThreadKDP::GetQueueName() { return NULL; }
62
63void ThreadKDP::RefreshStateAfterStop() {
64 // Invalidate all registers in our register context. We don't set "force" to
65 // true because the stop reply packet might have had some register values
66 // that were expedited and these will already be copied into the register
67 // context by the time this function gets called. The KDPRegisterContext
68 // class has been made smart enough to detect when it needs to invalidate
69 // which registers are valid by putting hooks in the register read and
70 // register supply functions where they check the process stop ID and do
71 // the right thing.
72 const bool force = false;
73 lldb::RegisterContextSP reg_ctx_sp(GetRegisterContext());
74 if (reg_ctx_sp)
75 reg_ctx_sp->InvalidateIfNeeded(force);
Greg Claytona63d08c2011-07-19 03:57:15 +000076}
77
Kate Stoneb9c1b512016-09-06 20:57:50 +000078bool ThreadKDP::ThreadIDIsValid(lldb::tid_t thread) { return thread != 0; }
79
80void ThreadKDP::Dump(Log *log, uint32_t index) {}
81
82bool ThreadKDP::ShouldStop(bool &step_more) { return true; }
83lldb::RegisterContextSP ThreadKDP::GetRegisterContext() {
84 if (m_reg_context_sp.get() == NULL)
85 m_reg_context_sp = CreateRegisterContextForFrame(NULL);
86 return m_reg_context_sp;
Greg Claytona63d08c2011-07-19 03:57:15 +000087}
88
Kate Stoneb9c1b512016-09-06 20:57:50 +000089lldb::RegisterContextSP
90ThreadKDP::CreateRegisterContextForFrame(StackFrame *frame) {
91 lldb::RegisterContextSP reg_ctx_sp;
92 uint32_t concrete_frame_idx = 0;
93
94 if (frame)
95 concrete_frame_idx = frame->GetConcreteFrameIndex();
96
97 if (concrete_frame_idx == 0) {
98 ProcessSP process_sp(CalculateProcess());
99 if (process_sp) {
100 switch (static_cast<ProcessKDP *>(process_sp.get())
101 ->GetCommunication()
102 .GetCPUType()) {
103 case llvm::MachO::CPU_TYPE_ARM:
104 reg_ctx_sp.reset(new RegisterContextKDP_arm(*this, concrete_frame_idx));
105 break;
106 case llvm::MachO::CPU_TYPE_ARM64:
107 reg_ctx_sp.reset(
108 new RegisterContextKDP_arm64(*this, concrete_frame_idx));
109 break;
110 case llvm::MachO::CPU_TYPE_I386:
111 reg_ctx_sp.reset(
112 new RegisterContextKDP_i386(*this, concrete_frame_idx));
113 break;
114 case llvm::MachO::CPU_TYPE_X86_64:
115 reg_ctx_sp.reset(
116 new RegisterContextKDP_x86_64(*this, concrete_frame_idx));
117 break;
118 default:
David Blaikiea322f362017-01-06 00:38:06 +0000119 llvm_unreachable("Add CPU type support in KDP");
Kate Stoneb9c1b512016-09-06 20:57:50 +0000120 }
121 }
122 } else {
123 Unwind *unwinder = GetUnwinder();
124 if (unwinder)
125 reg_ctx_sp = unwinder->CreateRegisterContextForFrame(frame);
126 }
127 return reg_ctx_sp;
Greg Claytona63d08c2011-07-19 03:57:15 +0000128}
129
Kate Stoneb9c1b512016-09-06 20:57:50 +0000130bool ThreadKDP::CalculateStopInfo() {
131 ProcessSP process_sp(GetProcess());
132 if (process_sp) {
133 if (m_cached_stop_info_sp) {
134 SetStopInfo(m_cached_stop_info_sp);
135 } else {
136 SetStopInfo(StopInfo::CreateStopReasonWithSignal(*this, SIGSTOP));
137 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000138 return true;
Kate Stoneb9c1b512016-09-06 20:57:50 +0000139 }
140 return false;
Greg Claytona63d08c2011-07-19 03:57:15 +0000141}
142
Kate Stoneb9c1b512016-09-06 20:57:50 +0000143void ThreadKDP::SetStopInfoFrom_KDP_EXCEPTION(
144 const DataExtractor &exc_reply_packet) {
145 lldb::offset_t offset = 0;
146 uint8_t reply_command = exc_reply_packet.GetU8(&offset);
147 if (reply_command == CommunicationKDP::KDP_EXCEPTION) {
148 offset = 8;
149 const uint32_t count = exc_reply_packet.GetU32(&offset);
150 if (count >= 1) {
151 // const uint32_t cpu = exc_reply_packet.GetU32 (&offset);
152 offset += 4; // Skip the useless CPU field
153 const uint32_t exc_type = exc_reply_packet.GetU32(&offset);
154 const uint32_t exc_code = exc_reply_packet.GetU32(&offset);
155 const uint32_t exc_subcode = exc_reply_packet.GetU32(&offset);
156 // We have to make a copy of the stop info because the thread list
157 // will iterate through the threads and clear all stop infos..
Greg Claytona63d08c2011-07-19 03:57:15 +0000158
Kate Stoneb9c1b512016-09-06 20:57:50 +0000159 // Let the StopInfoMachException::CreateStopReasonWithMachException()
160 // function update the PC if needed as we might hit a software breakpoint
161 // and need to decrement the PC (i386 and x86_64 need this) and KDP
162 // doesn't do this for us.
163 const bool pc_already_adjusted = false;
164 const bool adjust_pc_if_needed = true;
165
166 m_cached_stop_info_sp =
167 StopInfoMachException::CreateStopReasonWithMachException(
168 *this, exc_type, 2, exc_code, exc_subcode, 0, pc_already_adjusted,
169 adjust_pc_if_needed);
Greg Claytona63d08c2011-07-19 03:57:15 +0000170 }
Kate Stoneb9c1b512016-09-06 20:57:50 +0000171 }
Greg Claytona63d08c2011-07-19 03:57:15 +0000172}