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