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/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"
34 using namespace lldb_private;
36 #pragma mark ThreadPlanTracer
38 ThreadPlanTracer::ThreadPlanTracer (Thread &thread, lldb::StreamSP &stream_sp) :
42 m_stream_sp (stream_sp)
46 ThreadPlanTracer::ThreadPlanTracer (Thread &thread) :
55 ThreadPlanTracer::GetLogStream ()
58 if (m_stream_sp.get())
59 return m_stream_sp.get();
62 TargetSP target_sp (m_thread.CalculateTarget());
64 return &target_sp->GetDebugger().GetOutputStream();
70 ThreadPlanTracer::Log()
73 bool show_frame_index = false;
74 bool show_fullpaths = false;
76 Stream *stream = GetLogStream();
79 m_thread.GetStackFrameAtIndex(0)->Dump (stream, show_frame_index, show_fullpaths);
87 ThreadPlanTracer::TracerExplainsStop ()
89 if (m_enabled && m_single_step)
91 lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
92 if (stop_info->GetStopReason() == eStopReasonTrace)
101 #pragma mark ThreadPlanAssemblyTracer
103 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread, lldb::StreamSP &stream_sp) :
104 ThreadPlanTracer (thread, stream_sp),
105 m_disassembler_sp (),
111 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread) :
112 ThreadPlanTracer (thread),
113 m_disassembler_sp (),
120 ThreadPlanAssemblyTracer::GetDisassembler ()
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();
128 ThreadPlanAssemblyTracer::GetIntPointerType()
130 if (!m_intptr_type.IsValid ())
132 TargetSP target_sp (m_thread.CalculateTarget());
135 Module *exe_module = target_sp->GetExecutableModulePointer();
139 m_intptr_type = TypeFromUser(exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, target_sp->GetArchitecture().GetAddressByteSize() * 8));
143 return m_intptr_type;
148 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer()
153 ThreadPlanAssemblyTracer::TracingStarted ()
155 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
157 if (m_register_values.size() == 0)
158 m_register_values.resize (reg_ctx->GetRegisterCount());
162 ThreadPlanAssemblyTracer::TracingEnded ()
164 m_register_values.clear();
168 PadOutTo (StreamString &stream, int target)
172 int length = stream.GetString().length();
174 if (length + 1 < target)
175 stream.Printf("%*s", target - (length + 1) + 1, "");
179 ThreadPlanAssemblyTracer::Log ()
181 Stream *stream = GetLogStream ();
186 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
188 lldb::addr_t pc = reg_ctx->GetPC();
189 ProcessSP process_sp (m_thread.GetProcess());
191 bool addr_valid = false;
192 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
193 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
195 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
196 stream->PutCString (" ");
198 Disassembler *disassembler = GetDisassembler();
202 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
206 DataExtractor extractor(buffer, sizeof(buffer),
207 process_sp->GetByteOrder(),
208 process_sp->GetAddressByteSize());
210 bool data_from_file = false;
212 disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false, data_from_file);
214 disassembler->DecodeInstructions (Address (pc), extractor, 0, 1, false, data_from_file);
216 InstructionList &instruction_list = disassembler->GetInstructionList();
217 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
219 if (instruction_list.GetSize())
221 const bool show_bytes = true;
222 const bool show_address = true;
223 Instruction *instruction = instruction_list.GetInstructionAtIndex(0).get();
224 instruction->Dump (stream,
225 max_opcode_byte_size,
233 const ABI *abi = process_sp->GetABI().get();
234 TypeFromUser intptr_type = GetIntPointerType();
236 if (abi && intptr_type.IsValid())
238 ValueList value_list;
239 const int num_args = 1;
241 for (int arg_index = 0; arg_index < num_args; ++arg_index)
244 value.SetValueType (Value::eValueTypeScalar);
245 // value.SetContext (Value::eContextTypeClangType, intptr_type.GetOpaqueQualType());
246 value.SetClangType (intptr_type);
247 value_list.PushValue (value);
250 if (abi->GetArgumentValues (m_thread, value_list))
252 for (int arg_index = 0; arg_index < num_args; ++arg_index)
254 stream->Printf("\n\targ[%d]=%llx", arg_index, value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
256 if (arg_index + 1 < num_args)
257 stream->PutCString (", ");
263 RegisterValue reg_value;
264 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
265 reg_num < num_registers;
268 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
269 if (reg_ctx->ReadRegister (reg_info, reg_value))
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])
275 if (reg_value.GetType() != RegisterValue::eTypeInvalid)
277 stream->PutCString ("\n\t");
278 reg_value.Dump(stream, reg_info, true, false, eFormatDefault);
281 m_register_values[reg_num] = reg_value;