blob: ae3ff3454440114342eeeb28991be44c3ff2ee90 [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
Daniel Malead891f9b2012-12-05 00:20:57 +000010#include "lldb/lldb-python.h"
11
Chris Lattner24943d22010-06-08 16:52:24 +000012#include "CommandObjectDisassemble.h"
13
14// C Includes
15// C++ Includes
16// Other libraries and framework includes
17// Project includes
18#include "lldb/Core/AddressRange.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000019#include "lldb/Core/Disassembler.h"
20#include "lldb/Core/SourceManager.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000021#include "lldb/Interpreter/Args.h"
Chris Lattner24943d22010-06-08 16:52:24 +000022#include "lldb/Interpreter/CommandCompletions.h"
23#include "lldb/Interpreter/CommandInterpreter.h"
24#include "lldb/Interpreter/CommandReturnObject.h"
Jim Ingham84cdc152010-06-15 19:49:27 +000025#include "lldb/Interpreter/Options.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000026#include "lldb/Symbol/Function.h"
Chris Lattner24943d22010-06-08 16:52:24 +000027#include "lldb/Symbol/Symbol.h"
28#include "lldb/Target/Process.h"
Greg Clayton49ce8962012-08-29 21:13:06 +000029#include "lldb/Target/StackFrame.h"
Chris Lattner24943d22010-06-08 16:52:24 +000030#include "lldb/Target/Target.h"
31
32#define DEFAULT_DISASM_BYTE_SIZE 32
Jim Inghamaa3e3e12011-03-22 01:48:42 +000033#define DEFAULT_DISASM_NUM_INS 4
Chris Lattner24943d22010-06-08 16:52:24 +000034
35using namespace lldb;
36using namespace lldb_private;
37
Greg Claytonf15996e2011-04-07 22:46:35 +000038CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen0e0636d2011-04-08 22:15:29 +000039 Options(interpreter),
Jim Inghamaa3e3e12011-03-22 01:48:42 +000040 num_lines_context(0),
41 num_instructions (0),
Greg Clayton24bc5d92011-03-30 18:16:51 +000042 func_name(),
Greg Clayton8d646f22012-12-14 22:36:35 +000043 current_function (false),
Greg Clayton24bc5d92011-03-30 18:16:51 +000044 start_addr(),
45 end_addr (),
46 at_pc (false),
47 frame_line (false),
48 plugin_name (),
Jim Inghamd2ad7fd2011-09-01 01:11:04 +000049 arch(),
50 some_location_specified (false)
Chris Lattner24943d22010-06-08 16:52:24 +000051{
Greg Clayton143fcc32011-04-13 00:18:08 +000052 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000053}
54
55CommandObjectDisassemble::CommandOptions::~CommandOptions ()
56{
57}
58
59Error
Greg Clayton143fcc32011-04-13 00:18:08 +000060CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000061{
62 Error error;
63
Greg Clayton6475c422012-12-04 00:32:51 +000064 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +000065
Jim Inghamaa3e3e12011-03-22 01:48:42 +000066 bool success;
67
Chris Lattner24943d22010-06-08 16:52:24 +000068 switch (short_option)
69 {
70 case 'm':
71 show_mixed = true;
72 break;
73
Greg Clayton889fbd02011-03-26 19:14:58 +000074 case 'C':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000075 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
76 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000077 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
Jim Inghamaa3e3e12011-03-22 01:48:42 +000078 break;
79
Chris Lattner24943d22010-06-08 16:52:24 +000080 case 'c':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000081 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
82 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000083 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000084 break;
85
86 case 'b':
87 show_bytes = true;
88 break;
89
Jim Ingham34e9a982010-06-15 18:47:14 +000090 case 's':
Greg Clayton49d888d2012-12-06 22:49:16 +000091 {
92 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
93 start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
94 if (start_addr != LLDB_INVALID_ADDRESS)
95 some_location_specified = true;
96 }
Jim Ingham34e9a982010-06-15 18:47:14 +000097 break;
98 case 'e':
Greg Clayton49d888d2012-12-06 22:49:16 +000099 {
100 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
101 end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
102 if (end_addr != LLDB_INVALID_ADDRESS)
103 some_location_specified = true;
104 }
Chris Lattner24943d22010-06-08 16:52:24 +0000105 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000106 case 'n':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000107 func_name.assign (option_arg);
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000108 some_location_specified = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000109 break;
110
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000111 case 'p':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000112 at_pc = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000113 some_location_specified = true;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000114 break;
115
116 case 'l':
117 frame_line = true;
118 // Disassemble the current source line kind of implies showing mixed
119 // source code context.
120 show_mixed = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000121 some_location_specified = true;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000122 break;
123
Greg Clayton149731c2011-03-25 18:03:16 +0000124 case 'P':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000125 plugin_name.assign (option_arg);
Greg Clayton149731c2011-03-25 18:03:16 +0000126 break;
127
Chris Lattner24943d22010-06-08 16:52:24 +0000128 case 'r':
129 raw = true;
130 break;
131
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000132 case 'f':
Greg Clayton8d646f22012-12-14 22:36:35 +0000133 current_function = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000134 some_location_specified = true;
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000135 break;
136
Greg Clayton889fbd02011-03-26 19:14:58 +0000137 case 'a':
Greg Claytonb170aee2012-05-08 01:45:38 +0000138 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
139 arch.SetTriple (option_arg);
Greg Clayton889fbd02011-03-26 19:14:58 +0000140 break;
141
Chris Lattner24943d22010-06-08 16:52:24 +0000142 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000143 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000144 break;
145 }
146
147 return error;
148}
149
150void
Greg Clayton143fcc32011-04-13 00:18:08 +0000151CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000152{
Chris Lattner24943d22010-06-08 16:52:24 +0000153 show_mixed = false;
154 show_bytes = false;
155 num_lines_context = 0;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000156 num_instructions = 0;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000157 func_name.clear();
Greg Clayton8d646f22012-12-14 22:36:35 +0000158 current_function = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000159 at_pc = false;
160 frame_line = false;
161 start_addr = LLDB_INVALID_ADDRESS;
162 end_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000163 raw = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000164 plugin_name.clear();
165 arch.Clear();
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000166 some_location_specified = false;
167}
168
169Error
170CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
171{
172 if (!some_location_specified)
Greg Clayton8d646f22012-12-14 22:36:35 +0000173 current_function = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000174 return Error();
175
Chris Lattner24943d22010-06-08 16:52:24 +0000176}
177
Greg Claytonb3448432011-03-24 21:19:54 +0000178const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000179CommandObjectDisassemble::CommandOptions::GetDefinitions ()
180{
181 return g_option_table;
182}
183
Greg Claytonb3448432011-03-24 21:19:54 +0000184OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000185CommandObjectDisassemble::CommandOptions::g_option_table[] =
186{
Greg Clayton8d646f22012-12-14 22:36:35 +0000187{ LLDB_OPT_SET_ALL, false, "bytes" , 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
188{ LLDB_OPT_SET_ALL, false, "context" , 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
189{ LLDB_OPT_SET_ALL, false, "mixed" , 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
190{ LLDB_OPT_SET_ALL, false, "raw" , 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
191{ LLDB_OPT_SET_ALL, false, "plugin" , 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
192{ LLDB_OPT_SET_ALL, false, "arch" , 'a', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
193{ LLDB_OPT_SET_1 |
194 LLDB_OPT_SET_2 , true , "start-address", 's', required_argument , NULL, 0, eArgTypeStartAddress,"Address at which to start disassembling."},
195{ LLDB_OPT_SET_1 , false, "end-address" , 'e', required_argument , NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."},
196{ LLDB_OPT_SET_2 |
197 LLDB_OPT_SET_3 |
198 LLDB_OPT_SET_4 |
199 LLDB_OPT_SET_5 , false, "count" , 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
200{ LLDB_OPT_SET_3 , false, "name" , 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
201 "Disassemble entire contents of the given function name."},
202{ LLDB_OPT_SET_4 , false, "frame" , 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
203{ LLDB_OPT_SET_5 , false, "pc" , 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
204{ LLDB_OPT_SET_6 , false, "line" , 'l', no_argument , NULL, 0, eArgTypeNone, "Disassemble the current frame's current source line instructions if there debug line table information, else disasemble around the pc."},
205{ 0 , false, NULL , 0, 0 , NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000206};
207
208
209
210//-------------------------------------------------------------------------
211// CommandObjectDisassemble
212//-------------------------------------------------------------------------
213
Greg Clayton238c0a12010-09-18 01:14:36 +0000214CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000215 CommandObjectParsed (interpreter,
216 "disassemble",
217 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
218 "disassemble [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000219 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000220{
221}
222
223CommandObjectDisassemble::~CommandObjectDisassemble()
224{
225}
226
Chris Lattner24943d22010-06-08 16:52:24 +0000227bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000228CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000229{
Greg Clayton238c0a12010-09-18 01:14:36 +0000230 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000231 if (target == NULL)
232 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000233 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000234 result.SetStatus (eReturnStatusFailed);
235 return false;
236 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000237 if (!m_options.arch.IsValid())
238 m_options.arch = target->GetArchitecture();
Chris Lattner24943d22010-06-08 16:52:24 +0000239
Greg Clayton24bc5d92011-03-30 18:16:51 +0000240 if (!m_options.arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000241 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000242 result.AppendError ("use the --arch option or set the target architecure to disassemble");
Chris Lattner24943d22010-06-08 16:52:24 +0000243 result.SetStatus (eReturnStatusFailed);
244 return false;
245 }
246
Greg Clayton149731c2011-03-25 18:03:16 +0000247 const char *plugin_name = m_options.GetPluginName ();
Sean Callanan4f28c312012-08-01 18:50:59 +0000248 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, plugin_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000249
Sean Callananb386d822012-08-09 00:50:26 +0000250 if (!disassembler)
Chris Lattner24943d22010-06-08 16:52:24 +0000251 {
Greg Clayton149731c2011-03-25 18:03:16 +0000252 if (plugin_name)
Greg Clayton889fbd02011-03-26 19:14:58 +0000253 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
254 plugin_name,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000255 m_options.arch.GetArchitectureName());
Greg Clayton149731c2011-03-25 18:03:16 +0000256 else
Greg Clayton889fbd02011-03-26 19:14:58 +0000257 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
Greg Clayton24bc5d92011-03-30 18:16:51 +0000258 m_options.arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000259 result.SetStatus (eReturnStatusFailed);
260 return false;
261 }
262
263 result.SetStatus (eReturnStatusSuccessFinishResult);
264
Greg Clayton70436352010-06-30 23:03:03 +0000265 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000266 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000267 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
Greg Claytonf15996e2011-04-07 22:46:35 +0000268 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
Jim Ingham34e9a982010-06-15 18:47:14 +0000269 result.SetStatus (eReturnStatusFailed);
270 return false;
271 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000272
Greg Clayton70436352010-06-30 23:03:03 +0000273 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000274 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000275
Greg Claytonb72d0f02011-04-12 05:54:46 +0000276 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton6377f5c2011-06-28 19:01:40 +0000277 // Always show the PC in the disassembly
278 uint32_t options = Disassembler::eOptionMarkPCAddress;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000279
Greg Clayton6377f5c2011-06-28 19:01:40 +0000280 // Mark the source line for the current PC only if we are doing mixed source and assembly
281 if (m_options.show_mixed)
282 options |= Disassembler::eOptionMarkPCSourceLine;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000283
284 if (m_options.show_bytes)
285 options |= Disassembler::eOptionShowBytes;
286
287 if (m_options.raw)
288 options |= Disassembler::eOptionRawOuput;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000289
Greg Clayton24bc5d92011-03-30 18:16:51 +0000290 if (!m_options.func_name.empty())
Greg Clayton70436352010-06-30 23:03:03 +0000291 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000292 ConstString name(m_options.func_name.c_str());
Greg Clayton70436352010-06-30 23:03:03 +0000293
Greg Clayton238c0a12010-09-18 01:14:36 +0000294 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000295 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000296 plugin_name,
Greg Clayton70436352010-06-30 23:03:03 +0000297 exe_ctx,
298 name,
299 NULL, // Module *
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000300 m_options.num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000301 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000302 options,
Greg Clayton70436352010-06-30 23:03:03 +0000303 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000304 {
Greg Clayton70436352010-06-30 23:03:03 +0000305 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000306 }
307 else
308 {
309 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
310 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000311 }
Greg Clayton70436352010-06-30 23:03:03 +0000312 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000313 else
Chris Lattner24943d22010-06-08 16:52:24 +0000314 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000315 AddressRange range;
Greg Clayton567e7f32011-09-22 04:58:26 +0000316 StackFrame *frame = exe_ctx.GetFramePtr();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000317 if (m_options.frame_line)
Greg Clayton70436352010-06-30 23:03:03 +0000318 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000319 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000320 {
321 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
322 result.SetStatus (eReturnStatusFailed);
323 return false;
324 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000325 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000326 if (pc_line_entry.IsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000327 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000328 range = pc_line_entry.range;
Greg Clayton70436352010-06-30 23:03:03 +0000329 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000330 else
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000331 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000332 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
333 m_options.show_mixed = false;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000334 }
Greg Clayton70436352010-06-30 23:03:03 +0000335 }
Greg Clayton8d646f22012-12-14 22:36:35 +0000336 else if (m_options.current_function)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000337 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000338 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000339 {
340 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
341 result.SetStatus (eReturnStatusFailed);
342 return false;
343 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000344 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000345 if (symbol)
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000346 {
347 range.GetBaseAddress() = symbol->GetAddress();
348 range.SetByteSize(symbol->GetByteSize());
349 }
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000350 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000351
352 // Did the "m_options.frame_line" find a valid range already? If so
353 // skip the rest...
354 if (range.GetByteSize() == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000355 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000356 if (m_options.at_pc)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000357 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000358 if (frame == NULL)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000359 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000360 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
361 result.SetStatus (eReturnStatusFailed);
362 return false;
363 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000364 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000365 if (m_options.num_instructions == 0)
366 {
367 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
368 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
369 }
370 }
371 else
372 {
373 range.GetBaseAddress().SetOffset (m_options.start_addr);
374 if (range.GetBaseAddress().IsValid())
375 {
376 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000377 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000378 if (m_options.end_addr <= m_options.start_addr)
379 {
380 result.AppendErrorWithFormat ("End address before start address.\n");
381 result.SetStatus (eReturnStatusFailed);
382 return false;
383 }
384 range.SetByteSize (m_options.end_addr - m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000385 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000386 }
387 }
388 }
389
390 if (m_options.num_instructions != 0)
391 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000392 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000393 {
394 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000395 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000396 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000397 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000398 if (sc.function)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000399 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000400 else if (sc.symbol && sc.symbol->ValueIsAddress())
401 range.GetBaseAddress() = sc.symbol->GetAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000402 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000403 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000404 }
405
Greg Clayton24bc5d92011-03-30 18:16:51 +0000406 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000407 {
408 result.AppendError ("invalid frame");
409 result.SetStatus (eReturnStatusFailed);
410 return false;
411 }
412 }
413
414 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000415 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000416 plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000417 exe_ctx,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000418 range.GetBaseAddress(),
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000419 m_options.num_instructions,
420 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000421 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000422 result.GetOutputStream()))
423 {
424 result.SetStatus (eReturnStatusSuccessFinishResult);
425 }
426 else
427 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000428 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000429 result.SetStatus (eReturnStatusFailed);
430 }
431 }
432 else
433 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000434 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000435 {
436 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000437 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000438 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000439 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000440 if (sc.function)
441 range = sc.function->GetAddressRange();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000442 else if (sc.symbol && sc.symbol->ValueIsAddress())
443 {
444 range.GetBaseAddress() = sc.symbol->GetAddress();
445 range.SetByteSize (sc.symbol->GetByteSize());
446 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000447 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000448 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000449 }
450 else
451 {
452 result.AppendError ("invalid frame");
453 result.SetStatus (eReturnStatusFailed);
454 return false;
455 }
456 }
457 if (range.GetByteSize() == 0)
458 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
459
460 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000461 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000462 plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000463 exe_ctx,
464 range,
465 m_options.num_instructions,
466 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000467 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000468 result.GetOutputStream()))
469 {
470 result.SetStatus (eReturnStatusSuccessFinishResult);
471 }
472 else
473 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000474 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000475 result.SetStatus (eReturnStatusFailed);
476 }
Greg Clayton70436352010-06-30 23:03:03 +0000477 }
Chris Lattner24943d22010-06-08 16:52:24 +0000478 }
479
Jim Ingham34e9a982010-06-15 18:47:14 +0000480 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000481}