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