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