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/Debugger.h"
20 #include "lldb/Core/Disassembler.h"
21 #include "lldb/Core/Log.h"
22 #include "lldb/Core/Module.h"
23 #include "lldb/Core/State.h"
24 #include "lldb/Core/Value.h"
25 #include "lldb/Symbol/TypeList.h"
26 #include "lldb/Target/RegisterContext.h"
27 #include "lldb/Target/Thread.h"
28 #include "lldb/Target/Process.h"
29 #include "lldb/Target/Target.h"
30 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 #pragma mark ThreadPlanTracer
35 
36 ThreadPlanTracer::ThreadPlanTracer (Thread &thread, lldb::StreamSP &stream_sp) :
37     m_thread (thread),
38     m_single_step(true),
39     m_enabled (false),
40     m_stream_sp (stream_sp)
41 {
42 }
43 
44 ThreadPlanTracer::ThreadPlanTracer (Thread &thread) :
45     m_thread (thread),
46     m_single_step(true),
47     m_enabled (false),
48     m_stream_sp ()
49 {
50 }
51 
52 Stream *
53 ThreadPlanTracer::GetLogStream ()
54 {
55 
56     if (m_stream_sp.get())
57         return m_stream_sp.get();
58     else
59     {
60         TargetSP target_sp (m_thread.CalculateTarget());
61         if (target_sp)
62             return &target_sp->GetDebugger().GetOutputStream();
63     }
64     return NULL;
65 }
66 
67 void
68 ThreadPlanTracer::Log()
69 {
70     SymbolContext sc;
71     bool show_frame_index = false;
72     bool show_fullpaths = false;
73 
74     Stream *stream = GetLogStream();
75     if (stream)
76     {
77         m_thread.GetStackFrameAtIndex(0)->Dump (stream, show_frame_index, show_fullpaths);
78         stream->Printf("\n");
79         stream->Flush();
80     }
81 
82 }
83 
84 bool
85 ThreadPlanTracer::TracerExplainsStop ()
86 {
87     if (m_enabled && m_single_step)
88     {
89         lldb::StopInfoSP stop_info = m_thread.GetStopInfo();
90         if (stop_info->GetStopReason() == eStopReasonTrace)
91             return true;
92         else
93             return false;
94     }
95     else
96         return false;
97 }
98 
99 #pragma mark ThreadPlanAssemblyTracer
100 
101 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread, lldb::StreamSP &stream_sp) :
102     ThreadPlanTracer (thread, stream_sp),
103     m_disassembler_sp (),
104     m_intptr_type (),
105     m_register_values ()
106 {
107 }
108 
109 ThreadPlanAssemblyTracer::ThreadPlanAssemblyTracer (Thread &thread) :
110     ThreadPlanTracer (thread),
111     m_disassembler_sp (),
112     m_intptr_type (),
113     m_register_values ()
114 {
115 }
116 
117 Disassembler *
118 ThreadPlanAssemblyTracer::GetDisassembler ()
119 {
120     if (m_disassembler_sp.get() == NULL)
121         m_disassembler_sp = Disassembler::FindPlugin(m_thread.GetProcess()->GetTarget().GetArchitecture(), NULL);
122     return m_disassembler_sp.get();
123 }
124 
125 TypeFromUser
126 ThreadPlanAssemblyTracer::GetIntPointerType()
127 {
128     if (!m_intptr_type.IsValid ())
129     {
130         TargetSP target_sp (m_thread.CalculateTarget());
131         if (target_sp)
132         {
133             Module *exe_module = target_sp->GetExecutableModulePointer();
134 
135             if (exe_module)
136             {
137                 m_intptr_type = TypeFromUser(exe_module->GetClangASTContext().GetBuiltinTypeForEncodingAndBitSize(eEncodingUint, target_sp->GetArchitecture().GetAddressByteSize() * 8),
138                                              exe_module->GetClangASTContext().getASTContext());
139             }
140         }
141     }
142     return m_intptr_type;
143 }
144 
145 
146 
147 ThreadPlanAssemblyTracer::~ThreadPlanAssemblyTracer()
148 {
149 }
150 
151 void
152 ThreadPlanAssemblyTracer::TracingStarted ()
153 {
154     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
155 
156     if (m_register_values.size() == 0)
157         m_register_values.resize (reg_ctx->GetRegisterCount());
158 }
159 
160 void
161 ThreadPlanAssemblyTracer::TracingEnded ()
162 {
163     m_register_values.clear();
164 }
165 
166 static void
167 PadOutTo (StreamString &stream, int target)
168 {
169     stream.Flush();
170 
171     int length = stream.GetString().length();
172 
173     if (length + 1 < target)
174         stream.Printf("%*s", target - (length + 1) + 1, "");
175 }
176 
177 void
178 ThreadPlanAssemblyTracer::Log ()
179 {
180     Stream *stream = GetLogStream ();
181 
182     if (!stream)
183         return;
184 
185     RegisterContext *reg_ctx = m_thread.GetRegisterContext().get();
186 
187     lldb::addr_t pc = reg_ctx->GetPC();
188     ProcessSP process_sp (m_thread.GetProcess());
189     Address pc_addr;
190     bool addr_valid = false;
191     uint8_t buffer[16] = {0}; // Must be big enough for any single instruction
192     addr_valid = process_sp->GetTarget().GetSectionLoadList().ResolveLoadAddress (pc, pc_addr);
193 
194     pc_addr.Dump(stream, &m_thread, Address::DumpStyleResolvedDescription, Address::DumpStyleModuleWithFileAddress);
195     stream->PutCString (" ");
196 
197     Disassembler *disassembler = GetDisassembler();
198     if (disassembler)
199     {
200         Error err;
201         process_sp->ReadMemory(pc, buffer, sizeof(buffer), err);
202 
203         if (err.Success())
204         {
205             DataExtractor extractor(buffer, sizeof(buffer),
206                                     process_sp->GetByteOrder(),
207                                     process_sp->GetAddressByteSize());
208 
209             if (addr_valid)
210                 disassembler->DecodeInstructions (pc_addr, extractor, 0, 1, false);
211             else
212                 disassembler->DecodeInstructions (Address (pc), extractor, 0, 1, false);
213 
214             InstructionList &instruction_list = disassembler->GetInstructionList();
215             const uint32_t max_opcode_byte_size = instruction_list.GetMaxOpcocdeByteSize();
216 
217             if (instruction_list.GetSize())
218             {
219                 const bool show_bytes = true;
220                 const bool show_address = true;
221                 Instruction *instruction = instruction_list.GetInstructionAtIndex(0).get();
222                 instruction->Dump (stream,
223                                    max_opcode_byte_size,
224                                    show_address,
225                                    show_bytes,
226                                    NULL);
227             }
228         }
229     }
230 
231     const ABI *abi = process_sp->GetABI().get();
232     TypeFromUser intptr_type = GetIntPointerType();
233 
234     if (abi && intptr_type.IsValid())
235     {
236         ValueList value_list;
237         const int num_args = 1;
238 
239         for (int arg_index = 0; arg_index < num_args; ++arg_index)
240         {
241             Value value;
242             value.SetValueType (Value::eValueTypeScalar);
243             value.SetContext (Value::eContextTypeClangType, intptr_type.GetOpaqueQualType());
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 }
284