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