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/Module.h"
17 #include "lldb/Core/State.h"
18 #include "lldb/Core/StreamFile.h"
19 #include "lldb/Core/Value.h"
20 #include "lldb/Symbol/TypeList.h"
21 #include "lldb/Symbol/TypeSystem.h"
22 #include "lldb/Target/ABI.h"
23 #include "lldb/Target/Process.h"
24 #include "lldb/Target/RegisterContext.h"
25 #include "lldb/Target/SectionLoadList.h"
26 #include "lldb/Target/Target.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Target/ThreadPlan.h"
29 #include "lldb/Utility/DataBufferHeap.h"
30 #include "lldb/Utility/DataExtractor.h"
31 #include "lldb/Utility/Log.h"
34 using namespace lldb_private;
36 #pragma mark ThreadPlanTracer
38 ThreadPlanTracer::ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp)
39 : m_thread(thread), m_single_step(true), m_enabled(false),
40 m_stream_sp(stream_sp) {}
42 ThreadPlanTracer::ThreadPlanTracer(Thread &thread)
43 : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {}
45 Stream *ThreadPlanTracer::GetLogStream() {
47 return m_stream_sp.get();
49 TargetSP target_sp(m_thread.CalculateTarget());
51 return target_sp->GetDebugger().GetOutputFile().get();
56 void ThreadPlanTracer::Log() {
58 bool show_frame_index = false;
59 bool show_fullpaths = false;
61 Stream *stream = GetLogStream();
63 m_thread.GetStackFrameAtIndex(0)->Dump(stream, show_frame_index,
70 bool ThreadPlanTracer::TracerExplainsStop() {
71 if (m_enabled && m_single_step) {
72 lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
73 return (stop_info->GetStopReason() == eStopReasonTrace);
78 #pragma mark ThreadPlanAssemblyTracer
80 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread,
81 lldb::StreamSP &stream_sp)
82 : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(),
83 m_register_values() {}
85 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread)
86 : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(),
87 m_register_values() {}
89 Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() {
90 if (!m_disassembler_sp)
91 m_disassembler_sp = Disassembler::FindPlugin(
92 m_thread.GetProcess()->GetTarget().GetArchitecture(), nullptr, nullptr);
93 return m_disassembler_sp.get();
96 TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() {
97 if (!m_intptr_type.IsValid()) {
98 TargetSP target_sp(m_thread.CalculateTarget());
100 TypeSystem *type_system =
101 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
104 TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize(
106 target_sp->GetArchitecture().GetAddressByteSize() * 8));
109 return m_intptr_type;
112 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default;
114 void ThreadPlanAssemblyTracer::TracingStarted() {
115 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
117 if (m_register_values.empty())
118 m_register_values.resize(reg_ctx->GetRegisterCount());
121 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); }
123 void ThreadPlanAssemblyTracer::Log() {
124 Stream *stream = GetLogStream();
129 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
131 lldb::addr_t pc = reg_ctx->GetPC();
132 ProcessSP process_sp(m_thread.GetProcess());
134 bool addr_valid = false;
135 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
136 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
139 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription,
140 Address::DumpStyleModuleWithFileAddress);
141 stream->PutCString(" ");
143 Disassembler *disassembler = GetDisassembler();
146 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
149 DataExtractor extractor(buffer, sizeof(buffer),
150 process_sp->GetByteOrder(),
151 process_sp->GetAddressByteSize());
153 bool data_from_file = false;
155 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false,
158 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false,
161 InstructionList &instruction_list = disassembler->GetInstructionList();
162 const uint32_t max_opcode_byte_size =
163 instruction_list.GetMaxOpcocdeByteSize();
165 if (instruction_list.GetSize()) {
166 const bool show_bytes = true;
167 const bool show_address = true;
168 Instruction *instruction =
169 instruction_list.GetInstructionAtIndex(0).get();
170 const FormatEntity::Entry *disassemble_format =
171 m_thread.GetProcess()
174 .GetDisassemblyFormat();
175 instruction->Dump(stream, max_opcode_byte_size, show_address,
176 show_bytes, nullptr, nullptr, nullptr,
177 disassemble_format, 0);
182 const ABI *abi = process_sp->GetABI().get();
183 TypeFromUser intptr_type = GetIntPointerType();
185 if (abi && intptr_type.IsValid()) {
186 ValueList value_list;
187 const int num_args = 1;
189 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
191 value.SetValueType(Value::eValueTypeScalar);
192 // value.SetContext (Value::eContextTypeClangType,
193 // intptr_type.GetOpaqueQualType());
194 value.SetCompilerType(intptr_type);
195 value_list.PushValue(value);
198 if (abi->GetArgumentValues(m_thread, value_list)) {
199 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
201 "\n\targ[%d]=%llx", arg_index,
202 value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
204 if (arg_index + 1 < num_args)
205 stream->PutCString(", ");
210 RegisterValue reg_value;
211 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
212 reg_num < num_registers; ++reg_num) {
213 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
214 if (reg_ctx->ReadRegister(reg_info, reg_value)) {
215 assert(reg_num < m_register_values.size());
216 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
217 reg_value != m_register_values[reg_num]) {
218 if (reg_value.GetType() != RegisterValue::eTypeInvalid) {
219 stream->PutCString("\n\t");
220 reg_value.Dump(stream, reg_info, true, false, eFormatDefault);
223 m_register_values[reg_num] = reg_value;