ThreadPlanTracer.cpp revision 254729
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
33using namespace lldb;
34using namespace lldb_private;
35
36#pragma mark ThreadPlanTracer
37
38ThreadPlanTracer::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
46ThreadPlanTracer::ThreadPlanTracer (Thread &thread) :
47    m_thread (thread),
48    m_single_step(true),
49    m_enabled (false),
50    m_stream_sp ()
51{
52}
53
54Stream *
55ThreadPlanTracer::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
69void
70ThreadPlanTracer::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
86bool
87ThreadPlanTracer::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
103ThreadPlanAssemblyTracer::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
111ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread) :
112    ThreadPlanTracer (thread),
113    m_disassembler_sp (),
114    m_intptr_type (),
115    m_register_values ()
116{
117}
118
119Disassembler *
120ThreadPlanAssemblyTracer::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
127TypeFromUser
128ThreadPlanAssemblyTracer::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            }
141        }
142    }
143    return m_intptr_type;
144}
145
146
147
148ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer()
149{
150}
151
152void
153ThreadPlanAssemblyTracer::TracingStarted ()
154{
155    RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
156
157    if (m_register_values.size() == 0)
158        m_register_values.resize (reg_ctx->GetRegisterCount());
159}
160
161void
162ThreadPlanAssemblyTracer::TracingEnded ()
163{
164    m_register_values.clear();
165}
166
167static void
168PadOutTo (StreamString &stream, int target)
169{
170    stream.Flush();
171
172    int length = stream.GetString().length();
173
174    if (length + 1 < target)
175        stream.Printf("%*s", target - (length + 1) + 1, "");
176}
177
178void
179ThreadPlanAssemblyTracer::Log ()
180{
181    Stream *stream = GetLogStream ();
182
183    if (!stream)
184        return;
185
186    RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
187
188    lldb::addr_t pc = reg_ctx->GetPC();
189    ProcessSP process_sp (m_thread.GetProcess());
190    Address pc_addr;
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);
194
195    pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
196    stream->PutCString (" ");
197
198    Disassembler *disassembler = GetDisassembler();
199    if (disassembler)
200    {
201        Error err;
202        process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
203
204        if (err.Success())
205        {
206            DataExtractor extractor(buffer, sizeof(buffer),
207                                    process_sp->GetByteOrder(),
208                                    process_sp->GetAddressByteSize());
209
210			bool data_from_file = false;
211            if (addr_valid)
212                disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false, data_from_file);
213            else
214                disassembler->DecodeInstructions (Address (pc), extractor, 0, 1, false, data_from_file);
215
216            InstructionList &instruction_list = disassembler->GetInstructionList();
217            const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
218
219            if (instruction_list.GetSize())
220            {
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,
226                                   show_address,
227                                   show_bytes,
228                                   NULL);
229            }
230        }
231    }
232
233    const ABI *abi = process_sp->GetABI().get();
234    TypeFromUser intptr_type = GetIntPointerType();
235
236    if (abi && intptr_type.IsValid())
237    {
238        ValueList value_list;
239        const int num_args = 1;
240
241        for (int arg_index = 0; arg_index < num_args; ++arg_index)
242        {
243            Value value;
244            value.SetValueType (Value::eValueTypeScalar);
245//            value.SetContext (Value::eContextTypeClangType, intptr_type.GetOpaqueQualType());
246            value.SetClangType (intptr_type);
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