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 //===----------------------------------------------------------------------===//
12 #include "lldb/Core/Debugger.h"
13 #include "lldb/Core/Disassembler.h"
14 #include "lldb/Core/DumpRegisterValue.h"
15 #include "lldb/Core/Module.h"
16 #include "lldb/Core/StreamFile.h"
17 #include "lldb/Core/Value.h"
18 #include "lldb/Symbol/TypeList.h"
19 #include "lldb/Symbol/TypeSystem.h"
20 #include "lldb/Target/ABI.h"
21 #include "lldb/Target/Process.h"
22 #include "lldb/Target/RegisterContext.h"
23 #include "lldb/Target/SectionLoadList.h"
24 #include "lldb/Target/Target.h"
25 #include "lldb/Target/Thread.h"
26 #include "lldb/Target/ThreadPlan.h"
27 #include "lldb/Utility/DataBufferHeap.h"
28 #include "lldb/Utility/DataExtractor.h"
29 #include "lldb/Utility/Log.h"
30 #include "lldb/Utility/State.h"
33 using namespace lldb_private;
35 #pragma mark ThreadPlanTracer
37 ThreadPlanTracer::ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp)
38 : m_thread(thread), m_single_step(true), m_enabled(false),
39 m_stream_sp(stream_sp) {}
41 ThreadPlanTracer::ThreadPlanTracer(Thread &thread)
42 : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {}
44 Stream *ThreadPlanTracer::GetLogStream() {
46 return m_stream_sp.get();
48 TargetSP target_sp(m_thread.CalculateTarget());
50 return target_sp->GetDebugger().GetOutputFile().get();
55 void ThreadPlanTracer::Log() {
57 bool show_frame_index = false;
58 bool show_fullpaths = false;
60 Stream *stream = GetLogStream();
62 m_thread.GetStackFrameAtIndex(0)->Dump(stream, show_frame_index,
69 bool ThreadPlanTracer::TracerExplainsStop() {
70 if (m_enabled && m_single_step) {
71 lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
72 return (stop_info->GetStopReason() == eStopReasonTrace);
77 #pragma mark ThreadPlanAssemblyTracer
79 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread,
80 lldb::StreamSP &stream_sp)
81 : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(),
82 m_register_values() {}
84 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread)
85 : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(),
86 m_register_values() {}
88 Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() {
89 if (!m_disassembler_sp)
90 m_disassembler_sp = Disassembler::FindPlugin(
91 m_thread.GetProcess()->GetTarget().GetArchitecture(), nullptr, nullptr);
92 return m_disassembler_sp.get();
95 TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() {
96 if (!m_intptr_type.IsValid()) {
97 TargetSP target_sp(m_thread.CalculateTarget());
99 TypeSystem *type_system =
100 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
103 TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize(
105 target_sp->GetArchitecture().GetAddressByteSize() * 8));
108 return m_intptr_type;
111 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default;
113 void ThreadPlanAssemblyTracer::TracingStarted() {
114 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
116 if (m_register_values.empty())
117 m_register_values.resize(reg_ctx->GetRegisterCount());
120 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); }
122 void ThreadPlanAssemblyTracer::Log() {
123 Stream *stream = GetLogStream();
128 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
130 lldb::addr_t pc = reg_ctx->GetPC();
131 ProcessSP process_sp(m_thread.GetProcess());
133 bool addr_valid = false;
134 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
135 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
138 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription,
139 Address::DumpStyleModuleWithFileAddress);
140 stream->PutCString(" ");
142 Disassembler *disassembler = GetDisassembler();
145 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
148 DataExtractor extractor(buffer, sizeof(buffer),
149 process_sp->GetByteOrder(),
150 process_sp->GetAddressByteSize());
152 bool data_from_file = false;
154 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false,
157 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false,
160 InstructionList &instruction_list = disassembler->GetInstructionList();
161 const uint32_t max_opcode_byte_size =
162 instruction_list.GetMaxOpcocdeByteSize();
164 if (instruction_list.GetSize()) {
165 const bool show_bytes = true;
166 const bool show_address = true;
167 Instruction *instruction =
168 instruction_list.GetInstructionAtIndex(0).get();
169 const FormatEntity::Entry *disassemble_format =
170 m_thread.GetProcess()
173 .GetDisassemblyFormat();
174 instruction->Dump(stream, max_opcode_byte_size, show_address,
175 show_bytes, nullptr, nullptr, nullptr,
176 disassemble_format, 0);
181 const ABI *abi = process_sp->GetABI().get();
182 TypeFromUser intptr_type = GetIntPointerType();
184 if (abi && intptr_type.IsValid()) {
185 ValueList value_list;
186 const int num_args = 1;
188 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
190 value.SetValueType(Value::eValueTypeScalar);
191 // value.SetContext (Value::eContextTypeClangType,
192 // intptr_type.GetOpaqueQualType());
193 value.SetCompilerType(intptr_type);
194 value_list.PushValue(value);
197 if (abi->GetArgumentValues(m_thread, value_list)) {
198 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
200 "\n\targ[%d]=%llx", arg_index,
201 value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
203 if (arg_index + 1 < num_args)
204 stream->PutCString(", ");
209 RegisterValue reg_value;
210 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
211 reg_num < num_registers; ++reg_num) {
212 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
213 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
214 assert(reg_num < m_register_values.size());
215 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
216 reg_value != m_register_values[reg_num]) {
217 if (reg_value.GetType() != RegisterValue::eTypeInvalid) {
218 stream->PutCString("\n\t");
219 DumpRegisterValue(reg_value, stream, reg_info, true, false,
223 m_register_values[reg_num] = reg_value;