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/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 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_process(*thread.GetProcess().get()), m_tid(thread.GetID()), 38 m_enabled(false), m_stream_sp(stream_sp) {} 39 40 ThreadPlanTracer::ThreadPlanTracer(Thread &thread) 41 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()), 42 m_enabled(false), m_stream_sp() {} 43 44 Stream *ThreadPlanTracer::GetLogStream() { 45 if (m_stream_sp) 46 return m_stream_sp.get(); 47 else { 48 TargetSP target_sp(GetThread().CalculateTarget()); 49 if (target_sp) 50 return &(target_sp->GetDebugger().GetOutputStream()); 51 } 52 return nullptr; 53 } 54 55 Thread &ThreadPlanTracer::GetThread() { 56 if (m_thread) 57 return *m_thread; 58 59 ThreadSP thread_sp = m_process.GetThreadList().FindThreadByID(m_tid); 60 m_thread = thread_sp.get(); 61 return *m_thread; 62 } 63 void ThreadPlanTracer::Log() { 64 SymbolContext sc; 65 bool show_frame_index = false; 66 bool show_fullpaths = false; 67 68 Stream *stream = GetLogStream(); 69 if (stream) { 70 GetThread().GetStackFrameAtIndex(0)->Dump(stream, show_frame_index, 71 show_fullpaths); 72 stream->Printf("\n"); 73 stream->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 = Disassembler::FindPlugin( 99 m_process.GetTarget().GetArchitecture(), nullptr, nullptr); 100 return m_disassembler_sp.get(); 101 } 102 103 TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() { 104 if (!m_intptr_type.IsValid()) { 105 if (auto target_sp = m_process.CalculateTarget()) { 106 auto type_system_or_err = 107 target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC); 108 if (auto err = type_system_or_err.takeError()) { 109 LLDB_LOG_ERROR(GetLog(LLDBLog::Commands), std::move(err), 110 "Unable to get integer pointer type from TypeSystem"); 111 } else { 112 m_intptr_type = TypeFromUser( 113 type_system_or_err->GetBuiltinTypeForEncodingAndBitSize( 114 eEncodingUint, 115 target_sp->GetArchitecture().GetAddressByteSize() * 8)); 116 } 117 } 118 } 119 return m_intptr_type; 120 } 121 122 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default; 123 124 void ThreadPlanAssemblyTracer::TracingStarted() { 125 } 126 127 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); } 128 129 void ThreadPlanAssemblyTracer::Log() { 130 Stream *stream = GetLogStream(); 131 132 if (!stream) 133 return; 134 135 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); 136 137 lldb::addr_t pc = reg_ctx->GetPC(); 138 Address pc_addr; 139 bool addr_valid = false; 140 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction 141 addr_valid = m_process.GetTarget().GetSectionLoadList().ResolveLoadAddress( 142 pc, pc_addr); 143 144 pc_addr.Dump(stream, &GetThread(), Address::DumpStyleResolvedDescription, 145 Address::DumpStyleModuleWithFileAddress); 146 stream->PutCString(" "); 147 148 Disassembler *disassembler = GetDisassembler(); 149 if (disassembler) { 150 Status err; 151 m_process.ReadMemory(pc, buffer, sizeof(buffer), err); 152 153 if (err.Success()) { 154 DataExtractor extractor(buffer, sizeof(buffer), m_process.GetByteOrder(), 155 m_process.GetAddressByteSize()); 156 157 bool data_from_file = false; 158 if (addr_valid) 159 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false, 160 data_from_file); 161 else 162 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false, 163 data_from_file); 164 165 InstructionList &instruction_list = disassembler->GetInstructionList(); 166 const uint32_t max_opcode_byte_size = 167 instruction_list.GetMaxOpcocdeByteSize(); 168 169 if (instruction_list.GetSize()) { 170 const bool show_bytes = true; 171 const bool show_address = true; 172 Instruction *instruction = 173 instruction_list.GetInstructionAtIndex(0).get(); 174 const FormatEntity::Entry *disassemble_format = 175 m_process.GetTarget().GetDebugger().GetDisassemblyFormat(); 176 instruction->Dump(stream, max_opcode_byte_size, show_address, 177 show_bytes, nullptr, nullptr, nullptr, 178 disassemble_format, 0); 179 } 180 } 181 } 182 183 const ABI *abi = m_process.GetABI().get(); 184 TypeFromUser intptr_type = GetIntPointerType(); 185 186 if (abi && intptr_type.IsValid()) { 187 ValueList value_list; 188 const int num_args = 1; 189 190 for (int arg_index = 0; arg_index < num_args; ++arg_index) { 191 Value value; 192 value.SetValueType(Value::ValueType::Scalar); 193 value.SetCompilerType(intptr_type); 194 value_list.PushValue(value); 195 } 196 197 if (abi->GetArgumentValues(GetThread(), 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 if (m_register_values.empty()) { 210 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); 211 m_register_values.resize(reg_ctx->GetRegisterCount()); 212 } 213 214 RegisterValue reg_value; 215 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount(); 216 reg_num < num_registers; ++reg_num) { 217 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num); 218 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 219 assert(reg_num < m_register_values.size()); 220 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid || 221 reg_value != m_register_values[reg_num]) { 222 if (reg_value.GetType() != RegisterValue::eTypeInvalid) { 223 stream->PutCString("\n\t"); 224 DumpRegisterValue(reg_value, stream, reg_info, true, false, 225 eFormatDefault); 226 } 227 } 228 m_register_values[reg_num] = reg_value; 229 } 230 } 231 stream->EOL(); 232 stream->Flush(); 233 } 234