ThreadPlanTracer.cpp revision 341825
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/DumpRegisterValue.h" 17#include "lldb/Core/Module.h" 18#include "lldb/Core/State.h" 19#include "lldb/Core/StreamFile.h" 20#include "lldb/Core/Value.h" 21#include "lldb/Symbol/TypeList.h" 22#include "lldb/Symbol/TypeSystem.h" 23#include "lldb/Target/ABI.h" 24#include "lldb/Target/Process.h" 25#include "lldb/Target/RegisterContext.h" 26#include "lldb/Target/SectionLoadList.h" 27#include "lldb/Target/Target.h" 28#include "lldb/Target/Thread.h" 29#include "lldb/Target/ThreadPlan.h" 30#include "lldb/Utility/DataBufferHeap.h" 31#include "lldb/Utility/DataExtractor.h" 32#include "lldb/Utility/Log.h" 33 34using namespace lldb; 35using namespace lldb_private; 36 37#pragma mark ThreadPlanTracer 38 39ThreadPlanTracer::ThreadPlanTracer(Thread &thread, lldb::StreamSP &stream_sp) 40 : m_thread(thread), m_single_step(true), m_enabled(false), 41 m_stream_sp(stream_sp) {} 42 43ThreadPlanTracer::ThreadPlanTracer(Thread &thread) 44 : m_thread(thread), m_single_step(true), m_enabled(false), m_stream_sp() {} 45 46Stream *ThreadPlanTracer::GetLogStream() { 47 if (m_stream_sp) 48 return m_stream_sp.get(); 49 else { 50 TargetSP target_sp(m_thread.CalculateTarget()); 51 if (target_sp) 52 return target_sp->GetDebugger().GetOutputFile().get(); 53 } 54 return nullptr; 55} 56 57void ThreadPlanTracer::Log() { 58 SymbolContext sc; 59 bool show_frame_index = false; 60 bool show_fullpaths = false; 61 62 Stream *stream = GetLogStream(); 63 if (stream) { 64 m_thread.GetStackFrameAtIndex(0)->Dump(stream, show_frame_index, 65 show_fullpaths); 66 stream->Printf("\n"); 67 stream->Flush(); 68 } 69} 70 71bool ThreadPlanTracer::TracerExplainsStop() { 72 if (m_enabled && m_single_step) { 73 lldb::StopInfoSP stop_info = m_thread.GetStopInfo(); 74 return (stop_info->GetStopReason() == eStopReasonTrace); 75 } else 76 return false; 77} 78 79#pragma mark ThreadPlanAssemblyTracer 80 81ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread, 82 lldb::StreamSP &stream_sp) 83 : ThreadPlanTracer(thread, stream_sp), m_disassembler_sp(), m_intptr_type(), 84 m_register_values() {} 85 86ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer(Thread &thread) 87 : ThreadPlanTracer(thread), m_disassembler_sp(), m_intptr_type(), 88 m_register_values() {} 89 90Disassembler *ThreadPlanAssemblyTracer::GetDisassembler() { 91 if (!m_disassembler_sp) 92 m_disassembler_sp = Disassembler::FindPlugin( 93 m_thread.GetProcess()->GetTarget().GetArchitecture(), nullptr, nullptr); 94 return m_disassembler_sp.get(); 95} 96 97TypeFromUser ThreadPlanAssemblyTracer::GetIntPointerType() { 98 if (!m_intptr_type.IsValid()) { 99 TargetSP target_sp(m_thread.CalculateTarget()); 100 if (target_sp) { 101 TypeSystem *type_system = 102 target_sp->GetScratchTypeSystemForLanguage(nullptr, eLanguageTypeC); 103 if (type_system) 104 m_intptr_type = 105 TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize( 106 eEncodingUint, 107 target_sp->GetArchitecture().GetAddressByteSize() * 8)); 108 } 109 } 110 return m_intptr_type; 111} 112 113ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer() = default; 114 115void ThreadPlanAssemblyTracer::TracingStarted() { 116 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); 117 118 if (m_register_values.empty()) 119 m_register_values.resize(reg_ctx->GetRegisterCount()); 120} 121 122void ThreadPlanAssemblyTracer::TracingEnded() { m_register_values.clear(); } 123 124void ThreadPlanAssemblyTracer::Log() { 125 Stream *stream = GetLogStream(); 126 127 if (!stream) 128 return; 129 130 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get(); 131 132 lldb::addr_t pc = reg_ctx->GetPC(); 133 ProcessSP process_sp(m_thread.GetProcess()); 134 Address pc_addr; 135 bool addr_valid = false; 136 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction 137 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress( 138 pc, pc_addr); 139 140 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, 141 Address::DumpStyleModuleWithFileAddress); 142 stream->PutCString(" "); 143 144 Disassembler *disassembler = GetDisassembler(); 145 if (disassembler) { 146 Status err; 147 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err); 148 149 if (err.Success()) { 150 DataExtractor extractor(buffer, sizeof(buffer), 151 process_sp->GetByteOrder(), 152 process_sp->GetAddressByteSize()); 153 154 bool data_from_file = false; 155 if (addr_valid) 156 disassembler->DecodeInstructions(pc_addr, extractor, 0, 1, false, 157 data_from_file); 158 else 159 disassembler->DecodeInstructions(Address(pc), extractor, 0, 1, false, 160 data_from_file); 161 162 InstructionList &instruction_list = disassembler->GetInstructionList(); 163 const uint32_t max_opcode_byte_size = 164 instruction_list.GetMaxOpcocdeByteSize(); 165 166 if (instruction_list.GetSize()) { 167 const bool show_bytes = true; 168 const bool show_address = true; 169 Instruction *instruction = 170 instruction_list.GetInstructionAtIndex(0).get(); 171 const FormatEntity::Entry *disassemble_format = 172 m_thread.GetProcess() 173 ->GetTarget() 174 .GetDebugger() 175 .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 = process_sp->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::eValueTypeScalar); 193 // value.SetContext (Value::eContextTypeClangType, 194 // intptr_type.GetOpaqueQualType()); 195 value.SetCompilerType(intptr_type); 196 value_list.PushValue(value); 197 } 198 199 if (abi->GetArgumentValues(m_thread, value_list)) { 200 for (int arg_index = 0; arg_index < num_args; ++arg_index) { 201 stream->Printf( 202 "\n\targ[%d]=%llx", arg_index, 203 value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong()); 204 205 if (arg_index + 1 < num_args) 206 stream->PutCString(", "); 207 } 208 } 209 } 210 211 RegisterValue reg_value; 212 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount(); 213 reg_num < num_registers; ++reg_num) { 214 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num); 215 if (reg_ctx->ReadRegister(reg_info, reg_value)) { 216 assert(reg_num < m_register_values.size()); 217 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid || 218 reg_value != m_register_values[reg_num]) { 219 if (reg_value.GetType() != RegisterValue::eTypeInvalid) { 220 stream->PutCString("\n\t"); 221 DumpRegisterValue(reg_value, stream, reg_info, true, false, 222 eFormatDefault); 223 } 224 } 225 m_register_values[reg_num] = reg_value; 226 } 227 } 228 stream->EOL(); 229 stream->Flush(); 230} 231