1 //===-- ThreadPlanTracer.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 #include <cstring>
11
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"
31
32 using namespace lldb;
33 using namespace lldb_private;
34
35 #pragma mark ThreadPlanTracer
36
ThreadPlanTracer(Thread & thread,lldb::StreamSP & stream_sp)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) {}
40
ThreadPlanTracer(Thread & thread)41 ThreadPlanTracer::ThreadPlanTracer(Thread &thread)
42 : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {}
43
GetLogStream()44 Stream *ThreadPlanTracer::GetLogStream() {
45 if (m_stream_sp)
46 return m_stream_sp.get();
47 else {
48 TargetSP target_sp(m_thread.CalculateTarget());
49 if (target_sp)
50 return target_sp->GetDebugger().GetOutputFile().get();
51 }
52 return nullptr;
53 }
54
Log()55 void ThreadPlanTracer::Log() {
56 SymbolContext sc;
57 bool show_frame_index = false;
58 bool show_fullpaths = false;
59
60 Stream *stream = GetLogStream();
61 if (stream) {
62 m_thread.GetStackFrameAtIndex(0)->Dump(stream, show_frame_index,
63 show_fullpaths);
64 stream->Printf("\n");
65 stream->Flush();
66 }
67 }
68
TracerExplainsStop()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);
73 } else
74 return false;
75 }
76
77 #pragma mark ThreadPlanAssemblyTracer
78
ThreadPlanAssemblyTracer(Thread & thread,lldb::StreamSP & stream_sp)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() {}
83
ThreadPlanAssemblyTracer(Thread & thread)84 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread)
85 : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(),
86 m_register_values() {}
87
GetDisassembler()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();
93 }
94
GetIntPointerType()95 TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() {
96 if (!m_intptr_type.IsValid()) {
97 TargetSP target_sp(m_thread.CalculateTarget());
98 if (target_sp) {
99 TypeSystem *type_system =
100 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
101 if (type_system)
102 m_intptr_type =
103 TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize(
104 eEncodingUint,
105 target_sp->GetArchitecture().GetAddressByteSize() * 8));
106 }
107 }
108 return m_intptr_type;
109 }
110
111 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default;
112
TracingStarted()113 void ThreadPlanAssemblyTracer::TracingStarted() {
114 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
115
116 if (m_register_values.empty())
117 m_register_values.resize(reg_ctx->GetRegisterCount());
118 }
119
TracingEnded()120 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); }
121
Log()122 void ThreadPlanAssemblyTracer::Log() {
123 Stream *stream = GetLogStream();
124
125 if (!stream)
126 return;
127
128 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
129
130 lldb::addr_t pc = reg_ctx->GetPC();
131 ProcessSP process_sp(m_thread.GetProcess());
132 Address pc_addr;
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(
136 pc, pc_addr);
137
138 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription,
139 Address::DumpStyleModuleWithFileAddress);
140 stream->PutCString(" ");
141
142 Disassembler *disassembler = GetDisassembler();
143 if (disassembler) {
144 Status err;
145 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
146
147 if (err.Success()) {
148 DataExtractor extractor(buffer, sizeof(buffer),
149 process_sp->GetByteOrder(),
150 process_sp->GetAddressByteSize());
151
152 bool data_from_file = false;
153 if (addr_valid)
154 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false,
155 data_from_file);
156 else
157 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false,
158 data_from_file);
159
160 InstructionList &instruction_list = disassembler->GetInstructionList();
161 const uint32_t max_opcode_byte_size =
162 instruction_list.GetMaxOpcocdeByteSize();
163
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()
171 ->GetTarget()
172 .GetDebugger()
173 .GetDisassemblyFormat();
174 instruction->Dump(stream, max_opcode_byte_size, show_address,
175 show_bytes, nullptr, nullptr, nullptr,
176 disassemble_format, 0);
177 }
178 }
179 }
180
181 const ABI *abi = process_sp->GetABI().get();
182 TypeFromUser intptr_type = GetIntPointerType();
183
184 if (abi && intptr_type.IsValid()) {
185 ValueList value_list;
186 const int num_args = 1;
187
188 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
189 Value value;
190 value.SetValueType(Value::eValueTypeScalar);
191 // value.SetContext (Value::eContextTypeClangType,
192 // intptr_type.GetOpaqueQualType());
193 value.SetCompilerType(intptr_type);
194 value_list.PushValue(value);
195 }
196
197 if (abi->GetArgumentValues(m_thread, value_list)) {
198 for (int arg_index = 0; arg_index < num_args; ++arg_index) {
199 stream->Printf(
200 "\n\targ[%d]=%llx", arg_index,
201 value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
202
203 if (arg_index + 1 < num_args)
204 stream->PutCString(", ");
205 }
206 }
207 }
208
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,
220 eFormatDefault);
221 }
222 }
223 m_register_values[reg_num] = reg_value;
224 }
225 }
226 stream->EOL();
227 stream->Flush();
228 }
229