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