ThreadPlanTracer.cpp revision 360660
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"
13#include "lldb/Core/DumpRegisterValue.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"
21#include "lldb/Target/RegisterContext.h"
22#include "lldb/Target/SectionLoadList.h"
23#include "lldb/Target/Target.h"
24#include "lldb/Target/Thread.h"
25#include "lldb/Target/ThreadPlan.h"
26#include "lldb/Utility/DataBufferHeap.h"
27#include "lldb/Utility/DataExtractor.h"
28#include "lldb/Utility/Log.h"
29#include "lldb/Utility/State.h"
30
31using namespace lldb;
32using namespace lldb_private;
33
34#pragma mark ThreadPlanTracer
35
36ThreadPlanTracer::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
40ThreadPlanTracer::ThreadPlanTracer(Thread &thread)
41    : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {}
42
43Stream *ThreadPlanTracer::GetLogStream() {
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
54void ThreadPlanTracer::Log() {
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
68bool 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
78ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread,
79                                                   lldb::StreamSP &stream_sp)
80    : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(),
81      m_register_values() {}
82
83ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread)
84    : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(),
85      m_register_values() {}
86
87Disassembler *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
94TypeFromUser 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 =
102            TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize(
103                eEncodingUint,
104                target_sp->GetArchitecture().GetAddressByteSize() * 8));
105    }
106  }
107  return m_intptr_type;
108}
109
110ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default;
111
112void ThreadPlanAssemblyTracer::TracingStarted() {
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
119void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); }
120
121void ThreadPlanAssemblyTracer::Log() {
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
137  pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription,
138               Address::DumpStyleModuleWithFileAddress);
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 =
169            m_thread.GetProcess()
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;
189      value.SetValueType(Value::eValueTypeScalar);
190      value.SetCompilerType(intptr_type);
191      value_list.PushValue(value);
192    }
193
194    if (abi->GetArgumentValues(m_thread, value_list)) {
195      for (int arg_index = 0; arg_index < num_args; ++arg_index) {
196        stream->Printf(
197            "\n\targ[%d]=%llx", arg_index,
198            value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
199
200        if (arg_index + 1 < num_args)
201          stream->PutCString(", ");
202      }
203    }
204  }
205
206  RegisterValue reg_value;
207  for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
208       reg_num < num_registers; ++reg_num) {
209    const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
210    if (reg_ctx->ReadRegister(reg_info, reg_value)) {
211      assert(reg_num < m_register_values.size());
212      if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
213          reg_value != m_register_values[reg_num]) {
214        if (reg_value.GetType() != RegisterValue::eTypeInvalid) {
215          stream->PutCString("\n\t");
216          DumpRegisterValue(reg_value, stream, reg_info, true, false,
217                            eFormatDefault);
218        }
219      }
220      m_register_values[reg_num] = reg_value;
221    }
222  }
223  stream->EOL();
224  stream->Flush();
225}
226