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