1 //===-- ThreadPlan.cpp ------------------------------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "lldb/lldb-python.h"
12 #include "lldb/Target/ThreadPlan.h"
17 // Other libraries and framework 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/StreamFile.h"
27 #include "lldb/Core/Value.h"
28 #include "lldb/Symbol/TypeList.h"
29 #include "lldb/Target/RegisterContext.h"
30 #include "lldb/Target/Thread.h"
31 #include "lldb/Target/Process.h"
32 #include "lldb/Target/SectionLoadList.h"
33 #include "lldb/Target/Target.h"
36 using namespace lldb_private;
38 #pragma mark ThreadPlanTracer
40 ThreadPlanTracer::ThreadPlanTracer (Thread &thread, lldb::StreamSP &stream_sp) :
44 m_stream_sp (stream_sp)
48 ThreadPlanTracer::ThreadPlanTracer (Thread &thread) :
57 ThreadPlanTracer::GetLogStream ()
60 if (m_stream_sp.get())
61 return m_stream_sp.get();
64 TargetSP target_sp (m_thread.CalculateTarget());
66 return target_sp->GetDebugger().GetOutputFile().get();
72 ThreadPlanTracer::Log()
75 bool show_frame_index = false;
76 bool show_fullpaths = false;
78 Stream *stream = GetLogStream();
81 m_thread.GetStackFrameAtIndex(0)->Dump (stream, show_frame_index, show_fullpaths);
89 ThreadPlanTracer::TracerExplainsStop ()
91 if (m_enabled && m_single_step)
93 lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
94 if (stop_info->GetStopReason() == eStopReasonTrace)
103 #pragma mark ThreadPlanAssemblyTracer
105 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread, lldb::StreamSP &stream_sp) :
106 ThreadPlanTracer (thread, stream_sp),
107 m_disassembler_sp (),
113 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread) :
114 ThreadPlanTracer (thread),
115 m_disassembler_sp (),
122 ThreadPlanAssemblyTracer::GetDisassembler ()
124 if (m_disassembler_sp.get() == NULL)
125 m_disassembler_sp = Disassembler::FindPlugin(m_thread.GetProcess()->GetTarget().GetArchitecture(), NULL, NULL);
126 return m_disassembler_sp.get();
130 ThreadPlanAssemblyTracer::GetIntPointerType()
132 if (!m_intptr_type.IsValid ())
134 TargetSP target_sp (m_thread.CalculateTarget());
137 Module *exe_module = target_sp->GetExecutableModulePointer();
141 m_intptr_type = TypeFromUser(exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, target_sp->GetArchitecture().GetAddressByteSize() * 8));
145 return m_intptr_type;
150 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer()
155 ThreadPlanAssemblyTracer::TracingStarted ()
157 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
159 if (m_register_values.size() == 0)
160 m_register_values.resize (reg_ctx->GetRegisterCount());
164 ThreadPlanAssemblyTracer::TracingEnded ()
166 m_register_values.clear();
170 ThreadPlanAssemblyTracer::Log ()
172 Stream *stream = GetLogStream ();
177 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
179 lldb::addr_t pc = reg_ctx->GetPC();
180 ProcessSP process_sp (m_thread.GetProcess());
182 bool addr_valid = false;
183 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
184 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
186 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
187 stream->PutCString (" ");
189 Disassembler *disassembler = GetDisassembler();
193 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
197 DataExtractor extractor(buffer, sizeof(buffer),
198 process_sp->GetByteOrder(),
199 process_sp->GetAddressByteSize());
201 bool data_from_file = false;
203 disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false, data_from_file);
205 disassembler->DecodeInstructions (Address (pc), extractor, 0, 1, false, data_from_file);
207 InstructionList &instruction_list = disassembler->GetInstructionList();
208 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
210 if (instruction_list.GetSize())
212 const bool show_bytes = true;
213 const bool show_address = true;
214 Instruction *instruction = instruction_list.GetInstructionAtIndex(0).get();
215 const char *disassemble_format = "${addr-file-or-load}: ";
216 instruction->Dump (stream,
217 max_opcode_byte_size,
228 const ABI *abi = process_sp->GetABI().get();
229 TypeFromUser intptr_type = GetIntPointerType();
231 if (abi && intptr_type.IsValid())
233 ValueList value_list;
234 const int num_args = 1;
236 for (int arg_index = 0; arg_index < num_args; ++arg_index)
239 value.SetValueType (Value::eValueTypeScalar);
240 // value.SetContext (Value::eContextTypeClangType, intptr_type.GetOpaqueQualType());
241 value.SetClangType (intptr_type);
242 value_list.PushValue (value);
245 if (abi->GetArgumentValues (m_thread, value_list))
247 for (int arg_index = 0; arg_index < num_args; ++arg_index)
249 stream->Printf("\n\targ[%d]=%llx", arg_index, value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
251 if (arg_index + 1 < num_args)
252 stream->PutCString (", ");
258 RegisterValue reg_value;
259 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
260 reg_num < num_registers;
263 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
264 if (reg_ctx->ReadRegister (reg_info, reg_value))
266 assert (reg_num < m_register_values.size());
267 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
268 reg_value != m_register_values[reg_num])
270 if (reg_value.GetType() != RegisterValue::eTypeInvalid)
272 stream->PutCString ("\n\t");
273 reg_value.Dump(stream, reg_info, true, false, eFormatDefault);
276 m_register_values[reg_num] = reg_value;