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/TypeList.h"
28 #include "lldb/Symbol/TypeSystem.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             TypeSystem *type_system = target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeC);
139             if (type_system)
140                 m_intptr_type = TypeFromUser(type_system->GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, target_sp->GetArchitecture().GetAddressByteSize() * 8));
141         }
142     }
143     return m_intptr_type;
144 }
145 
146 
147 
148 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer()
149 {
150 }
151 
152 void
153 ThreadPlanAssemblyTracer::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 
161 void
162 ThreadPlanAssemblyTracer::TracingEnded ()
163 {
164     m_register_values.clear();
165 }
166 
167 void
168 ThreadPlanAssemblyTracer::Log ()
169 {
170     Stream *stream = GetLogStream ();
171 
172     if (!stream)
173         return;
174 
175     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
176 
177     lldb::addr_t pc = reg_ctx->GetPC();
178     ProcessSP process_sp (m_thread.GetProcess());
179     Address pc_addr;
180     bool addr_valid = false;
181     uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
182     addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
183 
184     pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
185     stream->PutCString (" ");
186 
187     Disassembler *disassembler = GetDisassembler();
188     if (disassembler)
189     {
190         Error err;
191         process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
192 
193         if (err.Success())
194         {
195             DataExtractor extractor(buffer, sizeof(buffer),
196                                     process_sp->GetByteOrder(),
197                                     process_sp->GetAddressByteSize());
198 
199 			bool data_from_file = false;
200             if (addr_valid)
201                 disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false, data_from_file);
202             else
203                 disassembler->DecodeInstructions (Address (pc), extractor, 0, 1, false, data_from_file);
204 
205             InstructionList &instruction_list = disassembler->GetInstructionList();
206             const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
207 
208             if (instruction_list.GetSize())
209             {
210                 const bool show_bytes = true;
211                 const bool show_address = true;
212                 Instruction *instruction = instruction_list.GetInstructionAtIndex(0).get();
213                 const FormatEntity::Entry *disassemble_format = m_thread.GetProcess()->GetTarget().GetDebugger().GetDisassemblyFormat();
214                 instruction->Dump (stream,
215                                    max_opcode_byte_size,
216                                    show_address,
217                                    show_bytes,
218                                    NULL,
219                                    NULL,
220                                    NULL,
221                                    disassemble_format,
222                                    0);
223             }
224         }
225     }
226 
227     const ABI *abi = process_sp->GetABI().get();
228     TypeFromUser intptr_type = GetIntPointerType();
229 
230     if (abi && intptr_type.IsValid())
231     {
232         ValueList value_list;
233         const int num_args = 1;
234 
235         for (int arg_index = 0; arg_index < num_args; ++arg_index)
236         {
237             Value value;
238             value.SetValueType (Value::eValueTypeScalar);
239 //            value.SetContext (Value::eContextTypeClangType, intptr_type.GetOpaqueQualType());
240             value.SetCompilerType (intptr_type);
241             value_list.PushValue (value);
242         }
243 
244         if (abi->GetArgumentValues (m_thread, value_list))
245         {
246             for (int arg_index = 0; arg_index < num_args; ++arg_index)
247             {
248                 stream->Printf("\n\targ[%d]=%llx", arg_index, value_list.GetValueAtIndex(arg_index)->GetScalar().ULongLong());
249 
250                 if (arg_index + 1 < num_args)
251                     stream->PutCString (", ");
252             }
253         }
254     }
255 
256 
257     RegisterValue reg_value;
258     for (uint32_t reg_num = 0, num_registers = reg_ctx->GetRegisterCount();
259          reg_num < num_registers;
260          ++reg_num)
261     {
262         const RegisterInfo *reg_info = reg_ctx->GetRegisterInfoAtIndex(reg_num);
263         if (reg_ctx->ReadRegister (reg_info, reg_value))
264         {
265             assert (reg_num < m_register_values.size());
266             if (m_register_values[reg_num].GetType() == RegisterValue::eTypeInvalid ||
267                 reg_value != m_register_values[reg_num])
268             {
269                 if (reg_value.GetType() != RegisterValue::eTypeInvalid)
270                 {
271                     stream->PutCString ("\n\t");
272                     reg_value.Dump(stream, reg_info, true, false, eFormatDefault);
273                 }
274             }
275             m_register_values[reg_num] = reg_value;
276         }
277     }
278     stream->EOL();
279     stream->Flush();
280 }
281