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/LLDBLog.h" 29 #include "lldb/Utility/Log.h" 30 #include "lldb/Utility/State.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) {} 40 41 ThreadPlanTracer::ThreadPlanTracer(Thread &thread) 42 : m_process(*thread.GetProcess().get()), m_tid(thread.GetID()), 43 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(GetThread().CalculateTarget()); 50 if (target_sp) 51 return &(target_sp->GetDebugger().GetOutputStream()); 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(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 Stream *stream = GetLogStream(); 70 if (stream) { 71 GetThread().GetStackFrameAtIndex(0)->Dump(stream, show_frame_index, 72 show_fullpaths); 73 stream->Printf("\n"); 74 stream->Flush(); 75 } 76 } 77 78 bool ThreadPlanTracer::TracerExplainsStop() { 79 if (m_enabled) { 80 lldb::StopInfoSP stop_info = GetThread().GetStopInfo(); 81 return (stop_info->GetStopReason() == eStopReasonTrace); 82 } else 83 return false; 84 } 85 86 #pragma mark ThreadPlanAssemblyTracer 87 88 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread, 89 lldb::StreamSP &stream_sp) 90 : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(), 91 m_register_values() {} 92 93 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread) 94 : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(), 95 m_register_values() {} 96 97 Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() { 98 if (!m_disassembler_sp) 99 m_disassembler_sp = Disassembler::FindPlugin( 100 m_process.GetTarget().GetArchitecture(), nullptr, 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(eLanguageTypeC); 109 if (auto err = type_system_or_err.takeError()) { 110 LLDB_LOG_ERROR(GetLog(LLDBLog::Types), std::move(err), 111 "Unable to get integer pointer type from TypeSystem"); 112 } else { 113 m_intptr_type = TypeFromUser( 114 type_system_or_err->GetBuiltinTypeForEncodingAndBitSize( 115 eEncodingUint, 116 target_sp->GetArchitecture().GetAddressByteSize() * 8)); 117 } 118 } 119 } 120 return m_intptr_type; 121 } 122 123 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default; 124 125 void ThreadPlanAssemblyTracer::TracingStarted() { 126 } 127 128 void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); } 129 130 void ThreadPlanAssemblyTracer::Log() { 131 Stream *stream = GetLogStream(); 132 133 if (!stream) 134 return; 135 136 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); 137 138 lldb::addr_t pc = reg_ctx->GetPC(); 139 Address pc_addr; 140 bool addr_valid = false; 141 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction 142 addr_valid = m_process.GetTarget().GetSectionLoadList().ResolveLoadAddress( 143 pc, pc_addr); 144 145 pc_addr.Dump(stream, &GetThread(), Address::DumpStyleResolvedDescription, 146 Address::DumpStyleModuleWithFileAddress); 147 stream->PutCString(" "); 148 149 Disassembler *disassembler = GetDisassembler(); 150 if (disassembler) { 151 Status err; 152 m_process.ReadMemory(pc, buffer, sizeof(buffer), err); 153 154 if (err.Success()) { 155 DataExtractor extractor(buffer, sizeof(buffer), m_process.GetByteOrder(), 156 m_process.GetAddressByteSize()); 157 158 bool data_from_file = false; 159 if (addr_valid) 160 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false, 161 data_from_file); 162 else 163 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false, 164 data_from_file); 165 166 InstructionList &instruction_list = disassembler->GetInstructionList(); 167 const uint32_t max_opcode_byte_size = 168 instruction_list.GetMaxOpcocdeByteSize(); 169 170 if (instruction_list.GetSize()) { 171 const bool show_bytes = true; 172 const bool show_address = true; 173 Instruction *instruction = 174 instruction_list.GetInstructionAtIndex(0).get(); 175 const FormatEntity::Entry *disassemble_format = 176 m_process.GetTarget().GetDebugger().GetDisassemblyFormat(); 177 instruction->Dump(stream, max_opcode_byte_size, show_address, 178 show_bytes, nullptr, nullptr, nullptr, 179 disassemble_format, 0); 180 } 181 } 182 } 183 184 const ABI *abi = m_process.GetABI().get(); 185 TypeFromUser intptr_type = GetIntPointerType(); 186 187 if (abi && intptr_type.IsValid()) { 188 ValueList value_list; 189 const int num_args = 1; 190 191 for (int arg_index = 0; arg_index < num_args; ++arg_index) { 192 Value value; 193 value.SetValueType(Value::ValueType::Scalar); 194 value.SetCompilerType(intptr_type); 195 value_list.PushValue(value); 196 } 197 198 if (abi->GetArgumentValues(GetThread(), 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 if (m_register_values.empty()) { 211 RegisterContext *reg_ctx = GetThread().GetRegisterContext().get(); 212 m_register_values.resize(reg_ctx->GetRegisterCount()); 213 } 214 215 RegisterValue reg_value; 216 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount(); 217 reg_num < num_registers; ++reg_num) { 218 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num); 219 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 220 assert(reg_num < m_register_values.size()); 221 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid || 222 reg_value != m_register_values[reg_num]) { 223 if (reg_value.GetType() != RegisterValue::eTypeInvalid) { 224 stream->PutCString("\n\t"); 225 DumpRegisterValue(reg_value, stream, reg_info, true, false, 226 eFormatDefault); 227 } 228 } 229 m_register_values[reg_num] = reg_value; 230 } 231 } 232 stream->EOL(); 233 stream->Flush(); 234 } 235