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 PadOutTo (StreamString &stream, int target)
174 int length = stream.GetString().length();
176 if (length + 1 < target)
177 stream.Printf("%*s", target - (length + 1) + 1, "");
181 ThreadPlanAssemblyTracer::Log ()
183 Stream *stream = GetLogStream ();
188 RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
190 lldb::addr_t pc = reg_ctx->GetPC();
191 ProcessSP process_sp (m_thread.GetProcess());
193 bool addr_valid = false;
194 uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
195 addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
197 pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
198 stream->PutCString (" ");
200 Disassembler *disassembler = GetDisassembler();
204 process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
208 DataExtractor extractor(buffer, sizeof(buffer),
209 process_sp->GetByteOrder(),
210 process_sp->GetAddressByteSize());
212 bool data_from_file = false;
214 disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false, data_from_file);
216 disassembler->DecodeInstructions (Address (pc), extractor, 0, 1, false, data_from_file);
218 InstructionList &instruction_list = disassembler->GetInstructionList();
219 const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
221 if (instruction_list.GetSize())
223 const bool show_bytes = true;
224 const bool show_address = true;
225 Instruction *instruction = instruction_list.GetInstructionAtIndex(0).get();
226 instruction->Dump (stream,
227 max_opcode_byte_size,
235 const ABI *abi = process_sp->GetABI().get();
236 TypeFromUser intptr_type = GetIntPointerType();
238 if (abi && intptr_type.IsValid())
240 ValueList value_list;
241 const int num_args = 1;
243 for (int arg_index = 0; arg_index < num_args; ++arg_index)
246 value.SetValueType (Value::eValueTypeScalar);
247 // value.SetContext (Value::eContextTypeClangType, intptr_type.GetOpaqueQualType());
248 value.SetClangType (intptr_type);
249 value_list.PushValue (value);
252 if (abi->GetArgumentValues (m_thread, value_list))
254 for (int arg_index = 0; arg_index < num_args; ++arg_index)
256 stream->Printf("\n\targ[%d]=%llx", arg_index, value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
258 if (arg_index + 1 < num_args)
259 stream->PutCString (", ");
265 RegisterValue reg_value;
266 for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
267 reg_num < num_registers;
270 const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
271 if (reg_ctx->ReadRegister (reg_info, reg_value))
273 assert (reg_num < m_register_values.size());
274 if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
275 reg_value != m_register_values[reg_num])
277 if (reg_value.GetType() != RegisterValue::eTypeInvalid)
279 stream->PutCString ("\n\t");
280 reg_value.Dump(stream, reg_info, true, false, eFormatDefault);
283 m_register_values[reg_num] = reg_value;