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