LLDB  mainline
ThreadPlanTracer.cpp
Go to the documentation of this file.
1 //===-- ThreadPlanTracer.cpp ------------------------------------*- C++ -*-===//
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"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/StreamFile.h"
16 #include "lldb/Core/Value.h"
17 #include "lldb/Symbol/TypeList.h"
18 #include "lldb/Symbol/TypeSystem.h"
19 #include "lldb/Target/ABI.h"
20 #include "lldb/Target/Process.h"
23 #include "lldb/Target/Target.h"
24 #include "lldb/Target/Thread.h"
25 #include "lldb/Target/ThreadPlan.h"
28 #include "lldb/Utility/Log.h"
29 #include "lldb/Utility/State.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 #pragma mark ThreadPlanTracer
35 
36 ThreadPlanTracer::ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp)
37  : m_thread(thread), m_single_step(true), m_enabled(false),
38  m_stream_sp(stream_sp) {}
39 
41  : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {}
42 
44  if (m_stream_sp)
45  return m_stream_sp.get();
46  else {
47  TargetSP target_sp(m_thread.CalculateTarget());
48  if (target_sp)
49  return target_sp->GetDebugger().GetOutputFile().get();
50  }
51  return nullptr;
52 }
53 
55  SymbolContext sc;
56  bool show_frame_index = false;
57  bool show_fullpaths = false;
58 
59  Stream *stream = GetLogStream();
60  if (stream) {
61  m_thread.GetStackFrameAtIndex(0)->Dump(stream, show_frame_index,
62  show_fullpaths);
63  stream->Printf("\n");
64  stream->Flush();
65  }
66 }
67 
68 bool ThreadPlanTracer::TracerExplainsStop() {
69  if (m_enabled && m_single_step) {
70  lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
71  return (stop_info->GetStopReason() == eStopReasonTrace);
72  } else
73  return false;
74 }
75 
76 #pragma mark ThreadPlanAssemblyTracer
77 
79  lldb::StreamSP &stream_sp)
80  : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(),
81  m_register_values() {}
82 
84  : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(),
85  m_register_values() {}
86 
87 Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() {
88  if (!m_disassembler_sp)
89  m_disassembler_sp = Disassembler::FindPlugin(
90  m_thread.GetProcess()->GetTarget().GetArchitecture(), nullptr, nullptr);
91  return m_disassembler_sp.get();
92 }
93 
94 TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() {
95  if (!m_intptr_type.IsValid()) {
96  TargetSP target_sp(m_thread.CalculateTarget());
97  if (target_sp) {
98  TypeSystem *type_system =
99  target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC);
100  if (type_system)
101  m_intptr_type =
104  target_sp->GetArchitecture().GetAddressByteSize() * 8));
105  }
106  }
107  return m_intptr_type;
108 }
109 
111 
113  RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
114 
115  if (m_register_values.empty())
116  m_register_values.resize(reg_ctx->GetRegisterCount());
117 }
118 
119 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); }
120 
122  Stream *stream = GetLogStream();
123 
124  if (!stream)
125  return;
126 
127  RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
128 
129  lldb::addr_t pc = reg_ctx->GetPC();
130  ProcessSP process_sp(m_thread.GetProcess());
131  Address pc_addr;
132  bool addr_valid = false;
133  uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
134  addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress(
135  pc, pc_addr);
136 
139  stream->PutCString(" ");
140 
141  Disassembler *disassembler = GetDisassembler();
142  if (disassembler) {
143  Status err;
144  process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
145 
146  if (err.Success()) {
147  DataExtractor extractor(buffer, sizeof(buffer),
148  process_sp->GetByteOrder(),
149  process_sp->GetAddressByteSize());
150 
151  bool data_from_file = false;
152  if (addr_valid)
153  disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false,
154  data_from_file);
155  else
156  disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false,
157  data_from_file);
158 
159  InstructionList &instruction_list = disassembler->GetInstructionList();
160  const uint32_t max_opcode_byte_size =
161  instruction_list.GetMaxOpcocdeByteSize();
162 
163  if (instruction_list.GetSize()) {
164  const bool show_bytes = true;
165  const bool show_address = true;
166  Instruction *instruction =
167  instruction_list.GetInstructionAtIndex(0).get();
168  const FormatEntity::Entry *disassemble_format =
170  ->GetTarget()
171  .GetDebugger()
172  .GetDisassemblyFormat();
173  instruction->Dump(stream, max_opcode_byte_size, show_address,
174  show_bytes, nullptr, nullptr, nullptr,
175  disassemble_format, 0);
176  }
177  }
178  }
179 
180  const ABI *abi = process_sp->GetABI().get();
181  TypeFromUser intptr_type = GetIntPointerType();
182 
183  if (abi && intptr_type.IsValid()) {
184  ValueList value_list;
185  const int num_args = 1;
186 
187  for (int arg_index = 0; arg_index < num_args; ++arg_index) {
188  Value value;
190  // value.SetContext (Value::eContextTypeClangType,
191  // intptr_type.GetOpaqueQualType());
192  value.SetCompilerType(intptr_type);
193  value_list.PushValue(value);
194  }
195 
196  if (abi->GetArgumentValues(m_thread, value_list)) {
197  for (int arg_index = 0; arg_index < num_args; ++arg_index) {
198  stream->Printf(
199  "\n\targ[%d]=%llx", arg_index,
200  value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
201 
202  if (arg_index + 1 < num_args)
203  stream->PutCString(", ");
204  }
205  }
206  }
207 
208  RegisterValue reg_value;
209  for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
210  reg_num < num_registers; ++reg_num) {
211  const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
212  if (reg_ctx->ReadRegister(reg_info, reg_value)) {
213  assert(reg_num < m_register_values.size());
214  if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
215  reg_value != m_register_values[reg_num]) {
216  if (reg_value.GetType() != RegisterValue::eTypeInvalid) {
217  stream->PutCString("\n\t");
218  DumpRegisterValue(reg_value, stream, reg_info, true, false,
220  }
221  }
222  m_register_values[reg_num] = reg_value;
223  }
224  }
225  stream->EOL();
226  stream->Flush();
227 }
size_t PutCString(llvm::StringRef cstr)
Output a C string to the stream.
Definition: Stream.cpp:61
An data extractor class.
Definition: DataExtractor.h:47
Enumerations for broadcasting.
Definition: SBLaunchInfo.h:14
A stream class that can stream formatted output to a file.
Definition: Stream.h:28
Defines a symbol context baton that can be handed other debug core functions.
Definition: SymbolContext.h:33
virtual void Dump(Stream *s, uint32_t max_opcode_byte_size, bool show_address, bool show_bytes, const ExecutionContext *exe_ctx, const SymbolContext *sym_ctx, const SymbolContext *prev_sym_ctx, const FormatEntity::Entry *disassembly_addr_format, size_t max_address_text_size)
Dump the text representation of this Instruction to a Stream.
TaggedASTType< 1 > TypeFromUser
Definition: TaggedASTType.h:40
bool Dump(Stream *s, ExecutionContextScope *exe_scope, DumpStyle style, DumpStyle fallback_style=DumpStyleInvalid, uint32_t addr_byte_size=UINT32_MAX) const
Dump a description of this object to a Stream.
Definition: Address.cpp:374
Value * GetValueAtIndex(size_t idx)
Definition: Value.cpp:701
void SetValueType(ValueType value_type)
Definition: Value.h:154
virtual void Flush()=0
Flush the stream.
virtual size_t DecodeInstructions(const Address &base_addr, const DataExtractor &data, lldb::offset_t data_offset, size_t num_instructions, bool append, bool data_from_file)=0
static lldb::DisassemblerSP FindPlugin(const ArchSpec &arch, const char *flavor, const char *plugin_name)
bool DumpRegisterValue(const RegisterValue &reg_val, Stream *s, const RegisterInfo *reg_info, bool prefix_with_name, bool prefix_with_alt_name, lldb::Format format, uint32_t reg_name_right_align_at=0)
virtual size_t GetRegisterCount()=0
size_t EOL()
Output and End of Line character to the stream.
Definition: Stream.cpp:127
uint64_t GetPC(uint64_t fail_value=LLDB_INVALID_ADDRESS)
lldb::TargetSP CalculateTarget() override
Definition: Thread.cpp:1584
unsigned long long ULongLong(unsigned long long fail_value=0) const
Definition: Scalar.cpp:1566
virtual lldb::RegisterContextSP GetRegisterContext()=0
virtual const RegisterInfo * GetRegisterInfoAtIndex(size_t reg)=0
virtual bool ReadRegister(const RegisterInfo *reg_info, RegisterValue &reg_value)=0
size_t Printf(const char *format,...) __attribute__((format(printf
Output printf formatted output to the stream.
Definition: Stream.cpp:106
RegisterValue::Type GetType() const
Definition: RegisterValue.h:79
virtual CompilerType GetBuiltinTypeForEncodingAndBitSize(lldb::Encoding encoding, size_t bit_size)=0
bool Success() const
Test for success condition.
Definition: Status.cpp:287
A section + offset based address class.
Definition: Address.h:80
InstructionList & GetInstructionList()
lldb::ProcessSP GetProcess() const
Definition: Thread.h:154
virtual bool GetArgumentValues(Thread &thread, ValueList &values) const =0
ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp)
uint32_t GetMaxOpcocdeByteSize() const
lldb::InstructionSP GetInstructionAtIndex(size_t idx) const
Display the details about what an address resolves to.
Definition: Address.h:108
uint64_t addr_t
Definition: lldb-types.h:83
ThreadPlanAssemblyTracer(Thread &thread, lldb::StreamSP &stream_sp)
Non-standardized C, such as K&R.
Definition: SBAddress.h:15
Display as the file address with the module name prepended (if any).
Definition: Address.h:99
void SetCompilerType(const CompilerType &compiler_type)
Definition: Value.cpp:268
const Scalar & GetScalar() const
Definition: Value.h:178
virtual lldb::StackFrameSP GetStackFrameAtIndex(uint32_t idx)
Definition: Thread.h:395
lldb::StopInfoSP GetStopInfo()
Definition: Thread.cpp:381
An error handling class.
Definition: Status.h:44
void PushValue(const Value &value)
Definition: Value.cpp:697