1 | //===-- ThreadPlanTracer.cpp ----------------------------------------------===// |
2 | // |
3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
4 | // See https://llvm.org/LICENSE.txt for license information. |
5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #include <cstring> |
10 | |
11 | #include "lldb/Core/Debugger.h" |
12 | #include "lldb/Core/Disassembler.h" |
13 | #include "lldb/Core/DumpRegisterValue.h" |
14 | #include "lldb/Core/Module.h" |
15 | #include "lldb/Core/Value.h" |
16 | #include "lldb/Symbol/TypeList.h" |
17 | #include "lldb/Symbol/TypeSystem.h" |
18 | #include "lldb/Target/ABI.h" |
19 | #include "lldb/Target/Process.h" |
20 | #include "lldb/Target/RegisterContext.h" |
21 | #include "lldb/Target/SectionLoadList.h" |
22 | #include "lldb/Target/Target.h" |
23 | #include "lldb/Target/Thread.h" |
24 | #include "lldb/Target/ThreadPlan.h" |
25 | #include "lldb/Utility/DataBufferHeap.h" |
26 | #include "lldb/Utility/DataExtractor.h" |
27 | #include "lldb/Utility/LLDBLog.h" |
28 | #include "lldb/Utility/Log.h" |
29 | #include "lldb/Utility/State.h" |
30 | #include "lldb/lldb-forward.h" |
31 | |
32 | using namespace lldb; |
33 | using namespace lldb_private; |
34 | |
35 | #pragma mark ThreadPlanTracer |
36 | |
37 | ThreadPlanTracer::ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp) |
38 | : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()), |
39 | m_enabled(false), m_stream_sp(stream_sp), m_thread(nullptr) {} |
40 | |
41 | ThreadPlanTracer::ThreadPlanTracer(Thread &thread) |
42 | : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()), |
43 | m_enabled(false), m_stream_sp(), m_thread(nullptr) {} |
44 | |
45 | StreamSP ThreadPlanTracer::GetLogStreamSP() { |
46 | if (m_stream_sp) |
47 | return m_stream_sp; |
48 | else { |
49 | TargetSP target_sp(GetThread().CalculateTarget()); |
50 | if (target_sp) |
51 | return target_sp->GetDebugger().GetAsyncOutputStream(); |
52 | } |
53 | return nullptr; |
54 | } |
55 | |
56 | Thread &ThreadPlanTracer::GetThread() { |
57 | if (m_thread) |
58 | return *m_thread; |
59 | |
60 | ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(tid: m_tid); |
61 | m_thread = thread_sp.get(); |
62 | return *m_thread; |
63 | } |
64 | void ThreadPlanTracer::Log() { |
65 | SymbolContext sc; |
66 | bool show_frame_index = false; |
67 | bool show_fullpaths = false; |
68 | |
69 | if (StreamSP stream_sp = GetLogStreamSP()) { |
70 | GetThread().GetStackFrameAtIndex(idx: 0)->Dump(strm: stream_sp.get(), show_frame_index, |
71 | show_fullpaths); |
72 | stream_sp->Printf(format: "\n" ); |
73 | stream_sp->Flush(); |
74 | } |
75 | } |
76 | |
77 | bool ThreadPlanTracer::TracerExplainsStop() { |
78 | if (m_enabled) { |
79 | lldb::StopInfoSP stop_info = GetThread().GetStopInfo(); |
80 | return (stop_info->GetStopReason() == eStopReasonTrace); |
81 | } else |
82 | return false; |
83 | } |
84 | |
85 | #pragma mark ThreadPlanAssemblyTracer |
86 | |
87 | ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread, |
88 | lldb::StreamSP &stream_sp) |
89 | : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(), |
90 | m_register_values() {} |
91 | |
92 | ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread) |
93 | : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(), |
94 | m_register_values() {} |
95 | |
96 | Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() { |
97 | if (!m_disassembler_sp) |
98 | m_disassembler_sp = |
99 | Disassembler::FindPlugin(arch: m_process.GetTarget().GetArchitecture(), |
100 | flavor: nullptr, cpu: nullptr, features: nullptr, plugin_name: nullptr); |
101 | return m_disassembler_sp.get(); |
102 | } |
103 | |
104 | TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() { |
105 | if (!m_intptr_type.IsValid()) { |
106 | if (auto target_sp = m_process.CalculateTarget()) { |
107 | auto type_system_or_err = |
108 | target_sp->GetScratchTypeSystemForLanguage(language: eLanguageTypeC); |
109 | if (auto err = type_system_or_err.takeError()) { |
110 | LLDB_LOG_ERROR( |
111 | GetLog(LLDBLog::Types), std::move(err), |
112 | "Unable to get integer pointer type from TypeSystem: {0}" ); |
113 | } else { |
114 | if (auto ts = *type_system_or_err) |
115 | m_intptr_type = TypeFromUser(ts->GetBuiltinTypeForEncodingAndBitSize( |
116 | encoding: eEncodingUint, |
117 | bit_size: target_sp->GetArchitecture().GetAddressByteSize() * 8)); |
118 | } |
119 | } |
120 | } |
121 | return m_intptr_type; |
122 | } |
123 | |
124 | ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default; |
125 | |
126 | void ThreadPlanAssemblyTracer::TracingStarted() { |
127 | } |
128 | |
129 | void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); } |
130 | |
131 | void ThreadPlanAssemblyTracer::Log() { |
132 | StreamSP stream_sp = GetLogStreamSP(); |
133 | |
134 | if (!stream_sp) |
135 | return; |
136 | |
137 | RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); |
138 | |
139 | lldb::addr_t pc = reg_ctx->GetPC(); |
140 | Address pc_addr; |
141 | bool addr_valid = false; |
142 | uint8_t buffer[16] = {0}; // Must be big enough for any single instruction |
143 | addr_valid = m_process.GetTarget().ResolveLoadAddress(load_addr: pc, so_addr&: pc_addr); |
144 | |
145 | pc_addr.Dump(s: stream_sp.get(), exe_scope: &GetThread(), |
146 | style: Address::DumpStyleResolvedDescription, |
147 | fallback_style: Address::DumpStyleModuleWithFileAddress); |
148 | stream_sp->PutCString(cstr: " " ); |
149 | |
150 | Disassembler *disassembler = GetDisassembler(); |
151 | if (disassembler) { |
152 | Status err; |
153 | m_process.ReadMemory(vm_addr: pc, buf: buffer, size: sizeof(buffer), error&: err); |
154 | |
155 | if (err.Success()) { |
156 | DataExtractor (buffer, sizeof(buffer), m_process.GetByteOrder(), |
157 | m_process.GetAddressByteSize()); |
158 | |
159 | bool data_from_file = false; |
160 | if (addr_valid) |
161 | disassembler->DecodeInstructions(base_addr: pc_addr, data: extractor, data_offset: 0, num_instructions: 1, append: false, |
162 | data_from_file); |
163 | else |
164 | disassembler->DecodeInstructions(base_addr: Address(pc), data: extractor, data_offset: 0, num_instructions: 1, append: false, |
165 | data_from_file); |
166 | |
167 | InstructionList &instruction_list = disassembler->GetInstructionList(); |
168 | const uint32_t max_opcode_byte_size = |
169 | instruction_list.GetMaxOpcocdeByteSize(); |
170 | |
171 | if (instruction_list.GetSize()) { |
172 | const bool show_bytes = true; |
173 | const bool show_address = true; |
174 | const bool show_control_flow_kind = true; |
175 | Instruction *instruction = |
176 | instruction_list.GetInstructionAtIndex(idx: 0).get(); |
177 | FormatEntity::Entry disassemble_format = |
178 | m_process.GetTarget().GetDebugger().GetDisassemblyFormat(); |
179 | instruction->Dump(s: stream_sp.get(), max_opcode_byte_size, show_address, |
180 | show_bytes, show_control_flow_kind, exe_ctx: nullptr, sym_ctx: nullptr, |
181 | prev_sym_ctx: nullptr, disassembly_addr_format: &disassemble_format, max_address_text_size: 0); |
182 | } |
183 | } |
184 | } |
185 | |
186 | const ABI *abi = m_process.GetABI().get(); |
187 | TypeFromUser intptr_type = GetIntPointerType(); |
188 | |
189 | if (abi && intptr_type.IsValid()) { |
190 | ValueList value_list; |
191 | const int num_args = 1; |
192 | |
193 | for (int arg_index = 0; arg_index < num_args; ++arg_index) { |
194 | Value value; |
195 | value.SetValueType(Value::ValueType::Scalar); |
196 | value.SetCompilerType(intptr_type); |
197 | value_list.PushValue(value); |
198 | } |
199 | |
200 | if (abi->GetArgumentValues(thread&: GetThread(), values&: value_list)) { |
201 | for (int arg_index = 0; arg_index < num_args; ++arg_index) { |
202 | stream_sp->Printf( |
203 | format: "\n\targ[%d]=%llx" , arg_index, |
204 | value_list.GetValueAtIndex(idx: arg_index)->GetScalar().ULongLong()); |
205 | |
206 | if (arg_index + 1 < num_args) |
207 | stream_sp->PutCString(cstr: ", " ); |
208 | } |
209 | } |
210 | } |
211 | |
212 | if (m_register_values.empty()) { |
213 | RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); |
214 | m_register_values.resize(new_size: reg_ctx->GetRegisterCount()); |
215 | } |
216 | |
217 | RegisterValue reg_value; |
218 | for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount(); |
219 | reg_num < num_registers; ++reg_num) { |
220 | const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg: reg_num); |
221 | if (reg_ctx->ReadRegister(reg_info, reg_value)) { |
222 | assert(reg_num < m_register_values.size()); |
223 | if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid || |
224 | reg_value != m_register_values[reg_num]) { |
225 | if (reg_value.GetType() != RegisterValue::eTypeInvalid) { |
226 | stream_sp->PutCString(cstr: "\n\t" ); |
227 | DumpRegisterValue(reg_val: reg_value, s&: *stream_sp, reg_info: *reg_info, prefix_with_name: true, prefix_with_alt_name: false, |
228 | format: eFormatDefault); |
229 | } |
230 | } |
231 | m_register_values[reg_num] = reg_value; |
232 | } |
233 | } |
234 | stream_sp->EOL(); |
235 | stream_sp->Flush(); |
236 | } |
237 | |