blob: c3b52cfeab81ca668a1fc92ca08d8d0e4e1bb8ec [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"
24#include "lldb/Target/StackFrame.h"
25#include "lldb/Symbol/Symbol.h"
26#include "lldb/Target/Process.h"
27#include "lldb/Target/Target.h"
28
29#define DEFAULT_DISASM_BYTE_SIZE 32
Jim Inghamaa3e3e12011-03-22 01:48:42 +000030#define DEFAULT_DISASM_NUM_INS 4
Chris Lattner24943d22010-06-08 16:52:24 +000031
32using namespace lldb;
33using namespace lldb_private;
34
35CommandObjectDisassemble::CommandOptions::CommandOptions () :
36 Options(),
Jim Inghamaa3e3e12011-03-22 01:48:42 +000037 num_lines_context(0),
38 num_instructions (0),
Chris Lattner24943d22010-06-08 16:52:24 +000039 m_func_name(),
Jim Ingham34e9a982010-06-15 18:47:14 +000040 m_start_addr(),
Jim Inghamaa3e3e12011-03-22 01:48:42 +000041 m_end_addr (),
Greg Clayton149731c2011-03-25 18:03:16 +000042 m_at_pc (false),
43 m_plugin_name ()
Chris Lattner24943d22010-06-08 16:52:24 +000044{
45 ResetOptionValues();
46}
47
48CommandObjectDisassemble::CommandOptions::~CommandOptions ()
49{
50}
51
52Error
53CommandObjectDisassemble::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
54{
55 Error error;
56
57 char short_option = (char) m_getopt_table[option_idx].val;
58
Jim Inghamaa3e3e12011-03-22 01:48:42 +000059 bool success;
60
Chris Lattner24943d22010-06-08 16:52:24 +000061 switch (short_option)
62 {
63 case 'm':
64 show_mixed = true;
65 break;
66
Jim Inghamaa3e3e12011-03-22 01:48:42 +000067 case 'x':
68 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
69 if (!success)
70 error.SetErrorStringWithFormat ("Invalid num context lines string: \"%s\".\n", option_arg);
71 break;
72
Chris Lattner24943d22010-06-08 16:52:24 +000073 case 'c':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000074 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
75 if (!success)
76 error.SetErrorStringWithFormat ("Invalid num of instructions string: \"%s\".\n", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000077 break;
78
79 case 'b':
80 show_bytes = true;
81 break;
82
Jim Ingham34e9a982010-06-15 18:47:14 +000083 case 's':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000084 m_start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0);
Jim Ingham34e9a982010-06-15 18:47:14 +000085 if (m_start_addr == LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +000086 m_start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16);
Chris Lattner24943d22010-06-08 16:52:24 +000087
Jim Ingham34e9a982010-06-15 18:47:14 +000088 if (m_start_addr == LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +000089 error.SetErrorStringWithFormat ("Invalid start address string '%s'.\n", option_arg);
Jim Ingham34e9a982010-06-15 18:47:14 +000090 break;
91 case 'e':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000092 m_end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0);
Jim Ingham34e9a982010-06-15 18:47:14 +000093 if (m_end_addr == LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +000094 m_end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16);
Jim Ingham34e9a982010-06-15 18:47:14 +000095
96 if (m_end_addr == LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +000097 error.SetErrorStringWithFormat ("Invalid end address string '%s'.\n", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000098 break;
99
100 case 'n':
Greg Clayton149731c2011-03-25 18:03:16 +0000101 m_func_name.assign (option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000102 break;
103
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000104 case 'p':
105 m_at_pc = true;
106 break;
107
Greg Clayton149731c2011-03-25 18:03:16 +0000108 case 'P':
109 m_plugin_name.assign (option_arg);
110 break;
111
Chris Lattner24943d22010-06-08 16:52:24 +0000112 case 'r':
113 raw = true;
114 break;
115
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000116 case 'f':
117 // The default action is to disassemble the function for the current frame.
118 // There's no need to set any flag.
119 break;
120
Chris Lattner24943d22010-06-08 16:52:24 +0000121 default:
122 error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option);
123 break;
124 }
125
126 return error;
127}
128
129void
130CommandObjectDisassemble::CommandOptions::ResetOptionValues ()
131{
132 Options::ResetOptionValues();
133 show_mixed = false;
134 show_bytes = false;
135 num_lines_context = 0;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000136 num_instructions = 0;
Chris Lattner24943d22010-06-08 16:52:24 +0000137 m_func_name.clear();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000138 m_at_pc = false;
Jim Ingham34e9a982010-06-15 18:47:14 +0000139 m_start_addr = LLDB_INVALID_ADDRESS;
140 m_end_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000141 raw = false;
Greg Clayton149731c2011-03-25 18:03:16 +0000142 m_plugin_name.clear();
Chris Lattner24943d22010-06-08 16:52:24 +0000143}
144
Greg Claytonb3448432011-03-24 21:19:54 +0000145const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000146CommandObjectDisassemble::CommandOptions::GetDefinitions ()
147{
148 return g_option_table;
149}
150
Greg Claytonb3448432011-03-24 21:19:54 +0000151OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000152CommandObjectDisassemble::CommandOptions::g_option_table[] =
153{
Greg Clayton149731c2011-03-25 18:03:16 +0000154{ LLDB_OPT_SET_ALL, false, "bytes", 'b', no_argument, NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
155{ LLDB_OPT_SET_ALL, false, "context", 'x', required_argument, NULL, 0, eArgTypeNumLines,"Number of context lines of source to show."},
156{ LLDB_OPT_SET_ALL, false, "mixed", 'm', no_argument, NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
157{ LLDB_OPT_SET_ALL, false, "raw", 'r', no_argument, NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
158{ LLDB_OPT_SET_ALL, false, "plugin", 'P', required_argument, NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
Chris Lattner24943d22010-06-08 16:52:24 +0000159
Caroline Tice43b014a2010-10-04 22:28:36 +0000160{ LLDB_OPT_SET_1, true, "start-address", 's', required_argument, NULL, 0, eArgTypeStartAddress, "Address at which to start disassembling."},
161{ 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 +0000162
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000163{ LLDB_OPT_SET_2, true, "start-address", 's', required_argument, NULL, 0, eArgTypeStartAddress, "Address at which to start disassembling."},
164{ LLDB_OPT_SET_2, false, "instruction-count", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
Jim Ingham34e9a982010-06-15 18:47:14 +0000165
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000166{ LLDB_OPT_SET_3, true, "name", 'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."},
167{ LLDB_OPT_SET_3, false, "instruction-count", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
168
169{ LLDB_OPT_SET_4, true, "current-frame", 'f', no_argument, NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
170{ LLDB_OPT_SET_4, false, "instruction-count", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
171
172{ LLDB_OPT_SET_5, true, "current-pc", 'p', no_argument, NULL, 0, eArgTypeNone, "Disassemble from the current pc."},
173{ LLDB_OPT_SET_5, false, "instruction-count", 'c', required_argument, NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
Chris Lattner24943d22010-06-08 16:52:24 +0000174
Caroline Tice4d6675c2010-10-01 19:59:14 +0000175{ 0, false, NULL, 0, 0, NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000176};
177
178
179
180//-------------------------------------------------------------------------
181// CommandObjectDisassemble
182//-------------------------------------------------------------------------
183
Greg Clayton238c0a12010-09-18 01:14:36 +0000184CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
185 CommandObject (interpreter,
186 "disassemble",
187 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
188 "disassemble [<cmd-options>]")
Chris Lattner24943d22010-06-08 16:52:24 +0000189{
190}
191
192CommandObjectDisassemble::~CommandObjectDisassemble()
193{
194}
195
Chris Lattner24943d22010-06-08 16:52:24 +0000196bool
197CommandObjectDisassemble::Execute
198(
199 Args& command,
Chris Lattner24943d22010-06-08 16:52:24 +0000200 CommandReturnObject &result
201)
202{
Greg Clayton238c0a12010-09-18 01:14:36 +0000203 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000204 if (target == NULL)
205 {
206 result.AppendError ("invalid target, set executable file using 'file' command");
207 result.SetStatus (eReturnStatusFailed);
208 return false;
209 }
210
211 ArchSpec arch(target->GetArchitecture());
212 if (!arch.IsValid())
213 {
214 result.AppendError ("target needs valid architecure in order to be able to disassemble");
215 result.SetStatus (eReturnStatusFailed);
216 return false;
217 }
218
Greg Clayton149731c2011-03-25 18:03:16 +0000219 const char *plugin_name = m_options.GetPluginName ();
220 Disassembler *disassembler = Disassembler::FindPlugin(arch, plugin_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000221
222 if (disassembler == NULL)
223 {
Greg Clayton149731c2011-03-25 18:03:16 +0000224 if (plugin_name)
225 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture named '%s'.\n",
226 arch.GetArchitectureName(),
227 plugin_name);
228 else
229 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n",
230 arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000231 result.SetStatus (eReturnStatusFailed);
232 return false;
233 }
234
235 result.SetStatus (eReturnStatusSuccessFinishResult);
236
Greg Clayton70436352010-06-30 23:03:03 +0000237 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000238 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000239 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
240 GetOptions()->GenerateOptionUsage (m_interpreter,
241 result.GetErrorStream(),
242 this);
243
Jim Ingham34e9a982010-06-15 18:47:14 +0000244 result.SetStatus (eReturnStatusFailed);
245 return false;
246 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000247
Greg Clayton70436352010-06-30 23:03:03 +0000248 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000249 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000250
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000251 ExecutionContext exe_ctx(m_interpreter.GetDebugger().GetExecutionContext());
252
Greg Clayton70436352010-06-30 23:03:03 +0000253 if (!m_options.m_func_name.empty())
254 {
255 ConstString name(m_options.m_func_name.c_str());
256
Greg Clayton238c0a12010-09-18 01:14:36 +0000257 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton70436352010-06-30 23:03:03 +0000258 arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000259 plugin_name,
Greg Clayton70436352010-06-30 23:03:03 +0000260 exe_ctx,
261 name,
262 NULL, // Module *
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000263 m_options.num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000264 m_options.show_mixed ? m_options.num_lines_context : 0,
265 m_options.show_bytes,
Sean Callanana846cc32011-03-10 23:35:12 +0000266 m_options.raw,
Greg Clayton70436352010-06-30 23:03:03 +0000267 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000268 {
Greg Clayton70436352010-06-30 23:03:03 +0000269 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000270 }
271 else
272 {
273 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
274 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000275 }
Greg Clayton70436352010-06-30 23:03:03 +0000276 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000277 else
Chris Lattner24943d22010-06-08 16:52:24 +0000278 {
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000279 Address start_addr;
280 lldb::addr_t range_byte_size = DEFAULT_DISASM_BYTE_SIZE;
281
282 if (m_options.m_at_pc)
Greg Clayton70436352010-06-30 23:03:03 +0000283 {
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000284 if (exe_ctx.frame == NULL)
Greg Clayton70436352010-06-30 23:03:03 +0000285 {
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000286 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
Greg Clayton70436352010-06-30 23:03:03 +0000287 result.SetStatus (eReturnStatusFailed);
288 return false;
289 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000290 start_addr = exe_ctx.frame->GetFrameCodeAddress();
291 if (m_options.num_instructions == 0)
292 {
293 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
294 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
295 }
Greg Clayton70436352010-06-30 23:03:03 +0000296 }
297 else
298 {
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000299 start_addr.SetOffset (m_options.m_start_addr);
300 if (start_addr.IsValid())
301 {
302 if (m_options.m_end_addr != LLDB_INVALID_ADDRESS)
303 {
304 if (m_options.m_end_addr < m_options.m_start_addr)
305 {
306 result.AppendErrorWithFormat ("End address before start address.\n");
307 result.SetStatus (eReturnStatusFailed);
308 return false;
309 }
310 range_byte_size = m_options.m_end_addr - m_options.m_start_addr;
311 }
312 }
313 }
314
315 if (m_options.num_instructions != 0)
316 {
317 if (!start_addr.IsValid())
318 {
319 // The default action is to disassemble the current frame function.
320 if (exe_ctx.frame)
321 {
322 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
323 if (sc.function)
324 start_addr = sc.function->GetAddressRange().GetBaseAddress();
325 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
326 start_addr = sc.symbol->GetAddressRangePtr()->GetBaseAddress();
327 else
328 start_addr = exe_ctx.frame->GetFrameCodeAddress();
329 }
330
331 if (!start_addr.IsValid())
332 {
333 result.AppendError ("invalid frame");
334 result.SetStatus (eReturnStatusFailed);
335 return false;
336 }
337 }
338
339 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
340 arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000341 plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000342 exe_ctx,
343 start_addr,
344 m_options.num_instructions,
345 m_options.show_mixed ? m_options.num_lines_context : 0,
346 m_options.show_bytes,
347 m_options.raw,
348 result.GetOutputStream()))
349 {
350 result.SetStatus (eReturnStatusSuccessFinishResult);
351 }
352 else
353 {
354 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr);
355 result.SetStatus (eReturnStatusFailed);
356 }
357 }
358 else
359 {
360 AddressRange range;
361 if (start_addr.IsValid())
362 {
363 range.GetBaseAddress() = start_addr;
364 range.SetByteSize (range_byte_size);
365 }
366 else
367 {
368 // The default action is to disassemble the current frame function.
369 if (exe_ctx.frame)
370 {
371 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
372 if (sc.function)
373 range = sc.function->GetAddressRange();
374 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
375 range = *sc.symbol->GetAddressRangePtr();
376 else
377 range.GetBaseAddress() = exe_ctx.frame->GetFrameCodeAddress();
378 }
379 else
380 {
381 result.AppendError ("invalid frame");
382 result.SetStatus (eReturnStatusFailed);
383 return false;
384 }
385 }
386 if (range.GetByteSize() == 0)
387 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
388
389 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
390 arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000391 plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000392 exe_ctx,
393 range,
394 m_options.num_instructions,
395 m_options.show_mixed ? m_options.num_lines_context : 0,
396 m_options.show_bytes,
397 m_options.raw,
398 result.GetOutputStream()))
399 {
400 result.SetStatus (eReturnStatusSuccessFinishResult);
401 }
402 else
403 {
404 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr);
405 result.SetStatus (eReturnStatusFailed);
406 }
Greg Clayton70436352010-06-30 23:03:03 +0000407 }
Chris Lattner24943d22010-06-08 16:52:24 +0000408 }
409
Jim Ingham34e9a982010-06-15 18:47:14 +0000410 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000411}