1 //===-- ThreadPlanTracer.cpp ------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
14 #include "lldb/Core/Debugger.h"
15 #include "lldb/Core/Disassembler.h"
16 #include "lldb/Core/DumpRegisterValue.h"
17 #include "lldb/Core/Module.h"
18 #include "lldb/Core/State.h"
19 #include "lldb/Core/StreamFile.h"
20 #include "lldb/Core/Value.h"
21 #include "lldb/Symbol/TypeList.h"
22 #include "lldb/Symbol/TypeSystem.h"
23 #include "lldb/Target/ABI.h"
24 #include "lldb/Target/Process.h"
25 #include "lldb/Target/RegisterContext.h"
26 #include "lldb/Target/SectionLoadList.h"
27 #include "lldb/Target/Target.h"
28 #include "lldb/Target/Thread.h"
29 #include "lldb/Target/ThreadPlan.h"
30 #include "lldb/Utility/DataBufferHeap.h"
31 #include "lldb/Utility/DataExtractor.h"
32 #include "lldb/Utility/Log.h"
35 using namespace lldb_private;
37 #pragma mark ThreadPlanTracer
39 ThreadPlanTracer::ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp)
40 : m_thread(thread), m_single_step(true), m_enabled(false),
41 m_stream_sp(stream_sp) {}
43 ThreadPlanTracer::ThreadPlanTracer(Thread &thread)
44 : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {}
46 Stream *ThreadPlanTracer::GetLogStream() {
48 return m_stream_sp.get();
50 TargetSP target_sp(m_thread.CalculateTarget());
52 return target_sp->GetDebugger().GetOutputFile().get();
57 void ThreadPlanTracer::Log() {
59 bool show_frame_index = false;
60 bool show_fullpaths = false;
62 Stream *stream = GetLogStream();
64 m_thread.GetStackFrameAtIndex(0)->Dump(stream, show_frame_index,
71 bool ThreadPlanTracer::TracerExplainsStop() {
72 if (m_enabled && m_single_step) {
73 lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
74 return (stop_info->GetStopReason() == eStopReasonTrace);
79 #pragma mark ThreadPlanAssemblyTracer
81 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread,
82 lldb::StreamSP &stream_sp)
83 : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(),
84 m_register_values() {}
86 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread)
87 : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(),
88 m_register_values() {}
90 Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() {
91 if (!m_disassembler_sp)
92 m_disassembler_sp = Disassembler::FindPlugin(
93 m_thread.GetProcess()->GetTarget().GetArchitecture(), nullptr, nullptr);
94 return m_disassembler_sp.get();
97 TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() {
98 if (!m_intptr_type.IsValid()) {
99 TargetSP target_sp(m_thread.CalculateTarget());
101 TypeSystem *type_system =
102 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
105 TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize(
107 target_sp->GetArchitecture().GetAddressByteSize() * 8));
110 return m_intptr_type;
113 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default;
115 void ThreadPlanAssemblyTracer::TracingStarted() {
116 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
118 if (m_register_values.empty())
119 m_register_values.resize(reg_ctx->GetRegisterCount());
122 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); }
124 void ThreadPlanAssemblyTracer::Log() {
125 Stream *stream = GetLogStream();
130 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
132 lldb::addr_t pc = reg_ctx->GetPC();
133 ProcessSP process_sp(m_thread.GetProcess());
135 bool addr_valid = false;
136 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
137 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
140 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription,
141 Address::DumpStyleModuleWithFileAddress);
142 stream->PutCString(" ");
144 Disassembler *disassembler = GetDisassembler();
147 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
150 DataExtractor extractor(buffer, sizeof(buffer),
151 process_sp->GetByteOrder(),
152 process_sp->GetAddressByteSize());
154 bool data_from_file = false;
156 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false,
159 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false,
162 InstructionList &instruction_list = disassembler->GetInstructionList();
163 const uint32_t max_opcode_byte_size =
164 instruction_list.GetMaxOpcocdeByteSize();
166 if (instruction_list.GetSize()) {
167 const bool show_bytes = true;
168 const bool show_address = true;
169 Instruction *instruction =
170 instruction_list.GetInstructionAtIndex(0).get();
171 const FormatEntity::Entry *disassemble_format =
172 m_thread.GetProcess()
175 .GetDisassemblyFormat();
176 instruction->Dump(stream, max_opcode_byte_size, show_address,
177 show_bytes, nullptr, nullptr, nullptr,
178 disassemble_format, 0);
183 const ABI *abi = process_sp->GetABI().get();
184 TypeFromUser intptr_type = GetIntPointerType();
186 if (abi && intptr_type.IsValid()) {
187 ValueList value_list;
188 const int num_args = 1;
190 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
192 value.SetValueType(Value::eValueTypeScalar);
193 // value.SetContext (Value::eContextTypeClangType,
194 // intptr_type.GetOpaqueQualType());
195 value.SetCompilerType(intptr_type);
196 value_list.PushValue(value);
199 if (abi->GetArgumentValues(m_thread, value_list)) {
200 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
202 "\n\targ[%d]=%llx", arg_index,
203 value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
205 if (arg_index + 1 < num_args)
206 stream->PutCString(", ");
211 RegisterValue reg_value;
212 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
213 reg_num < num_registers; ++reg_num) {
214 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
215 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
216 assert(reg_num < m_register_values.size());
217 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
218 reg_value != m_register_values[reg_num]) {
219 if (reg_value.GetType() != RegisterValue::eTypeInvalid) {
220 stream->PutCString("\n\t");
221 DumpRegisterValue(reg_value, stream, reg_info, true, false,
225 m_register_values[reg_num] = reg_value;