blob: f5d7edd10fba39233fbc6214a85eb8daa2eabd07 [file] [log] [blame]
Chris Lattner24943d22010-06-08 16:52:24 +00001//===-- CommandObjectDisassemble.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 "CommandObjectDisassemble.h"
11
12// C Includes
13// C++ Includes
14// Other libraries and framework includes
15// Project includes
16#include "lldb/Core/AddressRange.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000017#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000018#include "lldb/Interpreter/CommandCompletions.h"
19#include "lldb/Interpreter/CommandInterpreter.h"
20#include "lldb/Interpreter/CommandReturnObject.h"
21#include "lldb/Core/Disassembler.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000022#include "lldb/Interpreter/Options.h"
Chris Lattner24943d22010-06-08 16:52:24 +000023#include "lldb/Core/SourceManager.h"
Jason Molendaa6b71de2010-11-04 09:40:56 +000024#include "lldb/Core/Stream.h"
Chris Lattner24943d22010-06-08 16:52:24 +000025#include "lldb/Target/StackFrame.h"
26#include "lldb/Symbol/Symbol.h"
27#include "lldb/Target/Process.h"
28#include "lldb/Target/Target.h"
29
Jason Molendaa6b71de2010-11-04 09:40:56 +000030#include "lldb/Target/StackFrame.h"
31#include "lldb/Target/Thread.h"
32#include "lldb/Symbol/FuncUnwinders.h"
33#include "lldb/Symbol/UnwindPlan.h"
34#include "lldb/Symbol/DWARFCallFrameInfo.h"
35#include "lldb/Utility/ArchVolatileRegs.h"
36
Chris Lattner24943d22010-06-08 16:52:24 +000037#define DEFAULT_DISASM_BYTE_SIZE 32
38
39using namespace lldb;
40using namespace lldb_private;
41
42CommandObjectDisassemble::CommandOptions::CommandOptions () :
43 Options(),
44 m_func_name(),
Jim Ingham34e9a982010-06-15 18:47:14 +000045 m_start_addr(),
46 m_end_addr ()
Chris Lattner24943d22010-06-08 16:52:24 +000047{
48 ResetOptionValues();
49}
50
51CommandObjectDisassemble::CommandOptions::~CommandOptions ()
52{
53}
54
55Error
56CommandObjectDisassemble::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
57{
58 Error error;
59
60 char short_option = (char) m_getopt_table[option_idx].val;
61
62 switch (short_option)
63 {
64 case 'm':
65 show_mixed = true;
66 break;
67
68 case 'c':
69 num_lines_context = Args::StringToUInt32(option_arg, 0, 0);
70 break;
71
72 case 'b':
73 show_bytes = true;
74 break;
75
Jim Ingham34e9a982010-06-15 18:47:14 +000076 case 's':
77 m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0);
78 if (m_start_addr == LLDB_INVALID_ADDRESS)
79 m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16);
Chris Lattner24943d22010-06-08 16:52:24 +000080
Jim Ingham34e9a982010-06-15 18:47:14 +000081 if (m_start_addr == LLDB_INVALID_ADDRESS)
82 error.SetErrorStringWithFormat ("Invalid start address string '%s'.\n", optarg);
83 break;
84 case 'e':
85 m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0);
86 if (m_end_addr == LLDB_INVALID_ADDRESS)
87 m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16);
88
89 if (m_end_addr == LLDB_INVALID_ADDRESS)
90 error.SetErrorStringWithFormat ("Invalid end address string '%s'.\n", optarg);
Chris Lattner24943d22010-06-08 16:52:24 +000091 break;
92
93 case 'n':
94 m_func_name = option_arg;
95 break;
96
97 case 'r':
98 raw = true;
99 break;
100
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000101 case 'f':
102 // The default action is to disassemble the function for the current frame.
103 // There's no need to set any flag.
104 break;
105
Chris Lattner24943d22010-06-08 16:52:24 +0000106 default:
107 error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option);
108 break;
109 }
110
111 return error;
112}
113
114void
115CommandObjectDisassemble::CommandOptions::ResetOptionValues ()
116{
117 Options::ResetOptionValues();
118 show_mixed = false;
119 show_bytes = false;
120 num_lines_context = 0;
121 m_func_name.clear();
Jim Ingham34e9a982010-06-15 18:47:14 +0000122 m_start_addr = LLDB_INVALID_ADDRESS;
123 m_end_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000124 raw = false;
Chris Lattner24943d22010-06-08 16:52:24 +0000125}
126
127const lldb::OptionDefinition*
128CommandObjectDisassemble::CommandOptions::GetDefinitions ()
129{
130 return g_option_table;
131}
132
133lldb::OptionDefinition
134CommandObjectDisassemble::CommandOptions::g_option_table[] =
135{
Caroline Tice4d6675c2010-10-01 19:59:14 +0000136{ LLDB_OPT_SET_ALL, false, "bytes", 'b', no_argument, NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
137{ LLDB_OPT_SET_ALL, false, "context", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
138{ LLDB_OPT_SET_ALL, false, "mixed", 'm', no_argument, NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
139{ LLDB_OPT_SET_ALL, false, "raw", 'r', no_argument, NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
Chris Lattner24943d22010-06-08 16:52:24 +0000140
Caroline Tice43b014a2010-10-04 22:28:36 +0000141{ LLDB_OPT_SET_1, true, "start-address", 's', required_argument, NULL, 0, eArgTypeStartAddress, "Address at which to start disassembling."},
142{ LLDB_OPT_SET_1, false, "end-address", 'e', required_argument, NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."},
Chris Lattner24943d22010-06-08 16:52:24 +0000143
Caroline Tice4d6675c2010-10-01 19:59:14 +0000144{ LLDB_OPT_SET_2, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."},
Jim Ingham34e9a982010-06-15 18:47:14 +0000145
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000146{ LLDB_OPT_SET_3, true, "current-frame", 'f', no_argument, NULL, 0, eArgTypeNone, "Disassemble entire contents of the current frame's function."},
Chris Lattner24943d22010-06-08 16:52:24 +0000147
Caroline Tice4d6675c2010-10-01 19:59:14 +0000148{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000149};
150
151
152
153//-------------------------------------------------------------------------
154// CommandObjectDisassemble
155//-------------------------------------------------------------------------
156
Greg Clayton238c0a12010-09-18 01:14:36 +0000157CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
158 CommandObject (interpreter,
159 "disassemble",
160 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
161 "disassemble [<cmd-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +0000162{
163}
164
165CommandObjectDisassemble::~CommandObjectDisassemble()
166{
167}
168
Chris Lattner24943d22010-06-08 16:52:24 +0000169bool
170CommandObjectDisassemble::Execute
171(
172 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000173 CommandReturnObject &result
174)
175{
Greg Clayton238c0a12010-09-18 01:14:36 +0000176 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000177 if (target == NULL)
178 {
179 result.AppendError ("invalid target, set executable file using 'file' command");
180 result.SetStatus (eReturnStatusFailed);
181 return false;
182 }
183
184 ArchSpec arch(target->GetArchitecture());
185 if (!arch.IsValid())
186 {
187 result.AppendError ("target needs valid architecure in order to be able to disassemble");
188 result.SetStatus (eReturnStatusFailed);
189 return false;
190 }
191
192 Disassembler *disassembler = Disassembler::FindPlugin(arch);
193
194 if (disassembler == NULL)
195 {
196 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.AsCString());
197 result.SetStatus (eReturnStatusFailed);
198 return false;
199 }
200
201 result.SetStatus (eReturnStatusSuccessFinishResult);
202
Greg Clayton70436352010-06-30 23:03:03 +0000203 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000204 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000205 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
206 GetOptions()->GenerateOptionUsage (m_interpreter,
207 result.GetErrorStream(),
208 this);
209
Jim Ingham34e9a982010-06-15 18:47:14 +0000210 result.SetStatus (eReturnStatusFailed);
211 return false;
212 }
Greg Clayton238c0a12010-09-18 01:14:36 +0000213 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
Chris Lattner24943d22010-06-08 16:52:24 +0000214
Greg Clayton70436352010-06-30 23:03:03 +0000215 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000216 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000217
Greg Clayton70436352010-06-30 23:03:03 +0000218 if (!m_options.m_func_name.empty())
219 {
220 ConstString name(m_options.m_func_name.c_str());
221
Greg Clayton238c0a12010-09-18 01:14:36 +0000222 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton70436352010-06-30 23:03:03 +0000223 arch,
224 exe_ctx,
225 name,
226 NULL, // Module *
227 m_options.show_mixed ? m_options.num_lines_context : 0,
228 m_options.show_bytes,
229 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000230 {
Greg Clayton70436352010-06-30 23:03:03 +0000231 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000232 }
233 else
234 {
235 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
236 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000237 }
Greg Clayton70436352010-06-30 23:03:03 +0000238 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000239 else
Chris Lattner24943d22010-06-08 16:52:24 +0000240 {
Greg Clayton70436352010-06-30 23:03:03 +0000241 AddressRange range;
242 if (m_options.m_start_addr != LLDB_INVALID_ADDRESS)
243 {
244 range.GetBaseAddress().SetOffset (m_options.m_start_addr);
245 if (m_options.m_end_addr != LLDB_INVALID_ADDRESS)
246 {
247 if (m_options.m_end_addr < m_options.m_start_addr)
248 {
249 result.AppendErrorWithFormat ("End address before start address.\n");
250 result.SetStatus (eReturnStatusFailed);
251 return false;
252 }
253 range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr);
254 }
255 else
256 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
257 }
258 else
259 {
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000260 // The default action is to disassemble the current frame function.
Greg Clayton70436352010-06-30 23:03:03 +0000261 if (exe_ctx.frame)
262 {
Jason Molendaa6b71de2010-11-04 09:40:56 +0000263// SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
264 SymbolContext sc(exe_ctx.frame->GetSymbolContext (eSymbolContextEverything));
265
266 ArchVolatileRegs *vr = ArchVolatileRegs::FindPlugin (exe_ctx.target->GetArchitecture());
267 Address pc = exe_ctx.frame->GetFrameCodeAddress();
268 FuncUnwindersSP fu = pc.GetModule()->GetObjectFile()->GetUnwindTable().GetFuncUnwindersContainingAddress (pc, sc);
269 if (fu != NULL)
270 {
271
272 Address first_non_prologue_insn = fu->GetFirstNonPrologueInsn (*exe_ctx.target);
273 UnwindPlan *up = fu->GetUnwindPlanAtCallSite();
274 Stream *s = &result.GetOutputStream();
275 if (up)
276 {
277 s->Printf ("\nJSMDEBUG: unwind plan at call site (from eh_frame)\n");
278 up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread());
279 }
280 else
281 {
282 result.GetOutputStream().Printf("No UnwindPlanAtCallSite available.\n");
283 }
284 up = fu->GetUnwindPlanAtNonCallSite(exe_ctx.frame->GetThread());
285 s->Printf ("\nJSMDEBUG: unwind plan at non-call site (from disassembly)\n");
286 up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread());
287
288 up = fu->GetUnwindPlanFastUnwind(exe_ctx.frame->GetThread());
289 if (up)
290 {
291 s->Printf ("\nJSMDEBUG: fast unwind plan\n");
292 up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread());
293 }
294
295 up = fu->GetUnwindPlanArchitectureDefault(exe_ctx.frame->GetThread());
296 if (up)
297 {
298 s->Printf ("\nJSMDEBUG: architectural default unwind plan\n");
299 up->Dump (result.GetOutputStream(), &exe_ctx.frame->GetThread());
300 }
301 }
302
Greg Clayton70436352010-06-30 23:03:03 +0000303 if (sc.function)
304 range = sc.function->GetAddressRange();
305 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
306 range = *sc.symbol->GetAddressRangePtr();
307 else
Greg Claytonb04e7a82010-08-24 21:05:24 +0000308 range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress();
Greg Clayton70436352010-06-30 23:03:03 +0000309 }
310 else
311 {
312 result.AppendError ("invalid frame");
313 result.SetStatus (eReturnStatusFailed);
314 return false;
315 }
316 }
317 if (range.GetByteSize() == 0)
318 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
319
Greg Clayton238c0a12010-09-18 01:14:36 +0000320 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton70436352010-06-30 23:03:03 +0000321 arch,
322 exe_ctx,
323 range,
324 m_options.show_mixed ? m_options.num_lines_context : 0,
325 m_options.show_bytes,
326 result.GetOutputStream()))
327 {
328 result.SetStatus (eReturnStatusSuccessFinishResult);
329 }
330 else
331 {
332 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr);
333 result.SetStatus (eReturnStatusFailed);
334 }
Chris Lattner24943d22010-06-08 16:52:24 +0000335 }
336
Jim Ingham34e9a982010-06-15 18:47:14 +0000337 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000338}