blob: 5247743f6addebbe003a74a9260e91c56d8f182b [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
Greg Claytonf15996e2011-04-07 22:46:35 +000035CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
Johnny Chen0e0636d2011-04-08 22:15:29 +000036 Options(interpreter),
Jim Inghamaa3e3e12011-03-22 01:48:42 +000037 num_lines_context(0),
38 num_instructions (0),
Greg Clayton24bc5d92011-03-30 18:16:51 +000039 func_name(),
Jim Inghamd2ad7fd2011-09-01 01:11:04 +000040 cur_function (false),
Greg Clayton24bc5d92011-03-30 18:16:51 +000041 start_addr(),
42 end_addr (),
43 at_pc (false),
44 frame_line (false),
45 plugin_name (),
Jim Inghamd2ad7fd2011-09-01 01:11:04 +000046 arch(),
47 some_location_specified (false)
Chris Lattner24943d22010-06-08 16:52:24 +000048{
Greg Clayton143fcc32011-04-13 00:18:08 +000049 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000050}
51
52CommandObjectDisassemble::CommandOptions::~CommandOptions ()
53{
54}
55
56Error
Greg Clayton143fcc32011-04-13 00:18:08 +000057CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000058{
59 Error error;
60
61 char short_option = (char) m_getopt_table[option_idx].val;
62
Jim Inghamaa3e3e12011-03-22 01:48:42 +000063 bool success;
64
Chris Lattner24943d22010-06-08 16:52:24 +000065 switch (short_option)
66 {
67 case 'm':
68 show_mixed = true;
69 break;
70
Greg Clayton889fbd02011-03-26 19:14:58 +000071 case 'C':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000072 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
73 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000074 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
Jim Inghamaa3e3e12011-03-22 01:48:42 +000075 break;
76
Chris Lattner24943d22010-06-08 16:52:24 +000077 case 'c':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000078 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
79 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000080 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000081 break;
82
83 case 'b':
84 show_bytes = true;
85 break;
86
Jim Ingham34e9a982010-06-15 18:47:14 +000087 case 's':
Greg Clayton24bc5d92011-03-30 18:16:51 +000088 start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0);
89 if (start_addr == LLDB_INVALID_ADDRESS)
90 start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16);
Chris Lattner24943d22010-06-08 16:52:24 +000091
Greg Clayton24bc5d92011-03-30 18:16:51 +000092 if (start_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +000093 error.SetErrorStringWithFormat ("invalid start address string '%s'", option_arg);
Jim Inghamd2ad7fd2011-09-01 01:11:04 +000094 some_location_specified = true;
Jim Ingham34e9a982010-06-15 18:47:14 +000095 break;
96 case 'e':
Greg Clayton24bc5d92011-03-30 18:16:51 +000097 end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0);
98 if (end_addr == LLDB_INVALID_ADDRESS)
99 end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16);
Jim Ingham34e9a982010-06-15 18:47:14 +0000100
Greg Clayton24bc5d92011-03-30 18:16:51 +0000101 if (end_addr == LLDB_INVALID_ADDRESS)
Greg Clayton9c236732011-10-26 00:56:27 +0000102 error.SetErrorStringWithFormat ("invalid end address string '%s'", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +0000103 break;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000104 some_location_specified = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000105 case 'n':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000106 func_name.assign (option_arg);
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000107 some_location_specified = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000108 break;
109
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000110 case 'p':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000111 at_pc = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000112 some_location_specified = true;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000113 break;
114
115 case 'l':
116 frame_line = true;
117 // Disassemble the current source line kind of implies showing mixed
118 // source code context.
119 show_mixed = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000120 some_location_specified = true;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000121 break;
122
Greg Clayton149731c2011-03-25 18:03:16 +0000123 case 'P':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000124 plugin_name.assign (option_arg);
Greg Clayton149731c2011-03-25 18:03:16 +0000125 break;
126
Chris Lattner24943d22010-06-08 16:52:24 +0000127 case 'r':
128 raw = true;
129 break;
130
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000131 case 'f':
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000132 cur_function = true;
133 some_location_specified = true;
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000134 break;
135
Greg Clayton889fbd02011-03-26 19:14:58 +0000136 case 'a':
Greg Claytonb170aee2012-05-08 01:45:38 +0000137 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
138 arch.SetTriple (option_arg);
Greg Clayton889fbd02011-03-26 19:14:58 +0000139 break;
140
Chris Lattner24943d22010-06-08 16:52:24 +0000141 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000142 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000143 break;
144 }
145
146 return error;
147}
148
149void
Greg Clayton143fcc32011-04-13 00:18:08 +0000150CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000151{
Chris Lattner24943d22010-06-08 16:52:24 +0000152 show_mixed = false;
153 show_bytes = false;
154 num_lines_context = 0;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000155 num_instructions = 0;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000156 func_name.clear();
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000157 cur_function = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000158 at_pc = false;
159 frame_line = false;
160 start_addr = LLDB_INVALID_ADDRESS;
161 end_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000162 raw = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000163 plugin_name.clear();
164 arch.Clear();
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000165 some_location_specified = false;
166}
167
168Error
169CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
170{
171 if (!some_location_specified)
172 at_pc = true;
173 return Error();
174
Chris Lattner24943d22010-06-08 16:52:24 +0000175}
176
Greg Claytonb3448432011-03-24 21:19:54 +0000177const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000178CommandObjectDisassemble::CommandOptions::GetDefinitions ()
179{
180 return g_option_table;
181}
182
Greg Claytonb3448432011-03-24 21:19:54 +0000183OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000184CommandObjectDisassemble::CommandOptions::g_option_table[] =
185{
Greg Clayton889fbd02011-03-26 19:14:58 +0000186{ LLDB_OPT_SET_ALL , false , "bytes", 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
187{ LLDB_OPT_SET_ALL , false , "context", 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
188{ LLDB_OPT_SET_ALL , false , "mixed", 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
189{ LLDB_OPT_SET_ALL , false , "raw", 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
190{ LLDB_OPT_SET_ALL , false , "plugin", 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
191{ LLDB_OPT_SET_ALL , false , "arch", 'a', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
192{ LLDB_OPT_SET_1 |
193 LLDB_OPT_SET_2 , true , "start-address" , 's', required_argument , NULL, 0, eArgTypeStartAddress,"Address at which to start disassembling."},
194{ LLDB_OPT_SET_1 , false , "end-address" , 'e', required_argument , NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."},
195{ LLDB_OPT_SET_2 |
196 LLDB_OPT_SET_3 |
197 LLDB_OPT_SET_4 |
198 LLDB_OPT_SET_5 , false , "count", 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000199{ LLDB_OPT_SET_3 , false , "name", 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."},
200{ LLDB_OPT_SET_4 , false , "frame", 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
201{ LLDB_OPT_SET_5 , false , "pc", 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
202{ 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."},
Greg Clayton889fbd02011-03-26 19:14:58 +0000203{ 0 , false , NULL, 0, 0 , NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000204};
205
206
207
208//-------------------------------------------------------------------------
209// CommandObjectDisassemble
210//-------------------------------------------------------------------------
211
Greg Clayton238c0a12010-09-18 01:14:36 +0000212CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000213 CommandObjectParsed (interpreter,
214 "disassemble",
215 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
216 "disassemble [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000217 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000218{
219}
220
221CommandObjectDisassemble::~CommandObjectDisassemble()
222{
223}
224
Chris Lattner24943d22010-06-08 16:52:24 +0000225bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000226CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000227{
Greg Clayton238c0a12010-09-18 01:14:36 +0000228 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000229 if (target == NULL)
230 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000231 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000232 result.SetStatus (eReturnStatusFailed);
233 return false;
234 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000235 if (!m_options.arch.IsValid())
236 m_options.arch = target->GetArchitecture();
Chris Lattner24943d22010-06-08 16:52:24 +0000237
Greg Clayton24bc5d92011-03-30 18:16:51 +0000238 if (!m_options.arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000239 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000240 result.AppendError ("use the --arch option or set the target architecure to disassemble");
Chris Lattner24943d22010-06-08 16:52:24 +0000241 result.SetStatus (eReturnStatusFailed);
242 return false;
243 }
244
Greg Clayton149731c2011-03-25 18:03:16 +0000245 const char *plugin_name = m_options.GetPluginName ();
Sean Callanan4f28c312012-08-01 18:50:59 +0000246 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, plugin_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000247
Sean Callananb386d822012-08-09 00:50:26 +0000248 if (!disassembler)
Chris Lattner24943d22010-06-08 16:52:24 +0000249 {
Greg Clayton149731c2011-03-25 18:03:16 +0000250 if (plugin_name)
Greg Clayton889fbd02011-03-26 19:14:58 +0000251 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
252 plugin_name,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000253 m_options.arch.GetArchitectureName());
Greg Clayton149731c2011-03-25 18:03:16 +0000254 else
Greg Clayton889fbd02011-03-26 19:14:58 +0000255 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
Greg Clayton24bc5d92011-03-30 18:16:51 +0000256 m_options.arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000257 result.SetStatus (eReturnStatusFailed);
258 return false;
259 }
260
261 result.SetStatus (eReturnStatusSuccessFinishResult);
262
Greg Clayton70436352010-06-30 23:03:03 +0000263 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000264 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000265 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
Greg Claytonf15996e2011-04-07 22:46:35 +0000266 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
Jim Ingham34e9a982010-06-15 18:47:14 +0000267 result.SetStatus (eReturnStatusFailed);
268 return false;
269 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000270
Greg Clayton70436352010-06-30 23:03:03 +0000271 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000272 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000273
Greg Claytonb72d0f02011-04-12 05:54:46 +0000274 ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
Greg Clayton6377f5c2011-06-28 19:01:40 +0000275 // Always show the PC in the disassembly
276 uint32_t options = Disassembler::eOptionMarkPCAddress;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000277
Greg Clayton6377f5c2011-06-28 19:01:40 +0000278 // Mark the source line for the current PC only if we are doing mixed source and assembly
279 if (m_options.show_mixed)
280 options |= Disassembler::eOptionMarkPCSourceLine;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000281
282 if (m_options.show_bytes)
283 options |= Disassembler::eOptionShowBytes;
284
285 if (m_options.raw)
286 options |= Disassembler::eOptionRawOuput;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000287
Greg Clayton24bc5d92011-03-30 18:16:51 +0000288 if (!m_options.func_name.empty())
Greg Clayton70436352010-06-30 23:03:03 +0000289 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000290 ConstString name(m_options.func_name.c_str());
Greg Clayton70436352010-06-30 23:03:03 +0000291
Greg Clayton238c0a12010-09-18 01:14:36 +0000292 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000293 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000294 plugin_name,
Greg Clayton70436352010-06-30 23:03:03 +0000295 exe_ctx,
296 name,
297 NULL, // Module *
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000298 m_options.num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000299 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000300 options,
Greg Clayton70436352010-06-30 23:03:03 +0000301 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000302 {
Greg Clayton70436352010-06-30 23:03:03 +0000303 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000304 }
305 else
306 {
307 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
308 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000309 }
Greg Clayton70436352010-06-30 23:03:03 +0000310 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000311 else
Chris Lattner24943d22010-06-08 16:52:24 +0000312 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000313 AddressRange range;
Greg Clayton567e7f32011-09-22 04:58:26 +0000314 StackFrame *frame = exe_ctx.GetFramePtr();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000315 if (m_options.frame_line)
Greg Clayton70436352010-06-30 23:03:03 +0000316 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000317 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000318 {
319 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
320 result.SetStatus (eReturnStatusFailed);
321 return false;
322 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000323 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000324 if (pc_line_entry.IsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000325 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000326 range = pc_line_entry.range;
Greg Clayton70436352010-06-30 23:03:03 +0000327 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000328 else
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000329 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000330 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
331 m_options.show_mixed = false;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000332 }
Greg Clayton70436352010-06-30 23:03:03 +0000333 }
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000334 else if (m_options.cur_function)
335 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000336 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000337 {
338 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
339 result.SetStatus (eReturnStatusFailed);
340 return false;
341 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000342 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000343 if (symbol)
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000344 {
345 range.GetBaseAddress() = symbol->GetAddress();
346 range.SetByteSize(symbol->GetByteSize());
347 }
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000348 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000349
350 // Did the "m_options.frame_line" find a valid range already? If so
351 // skip the rest...
352 if (range.GetByteSize() == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000353 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000354 if (m_options.at_pc)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000355 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000356 if (frame == NULL)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000357 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000358 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
359 result.SetStatus (eReturnStatusFailed);
360 return false;
361 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000362 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000363 if (m_options.num_instructions == 0)
364 {
365 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
366 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
367 }
368 }
369 else
370 {
371 range.GetBaseAddress().SetOffset (m_options.start_addr);
372 if (range.GetBaseAddress().IsValid())
373 {
374 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000375 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000376 if (m_options.end_addr <= m_options.start_addr)
377 {
378 result.AppendErrorWithFormat ("End address before start address.\n");
379 result.SetStatus (eReturnStatusFailed);
380 return false;
381 }
382 range.SetByteSize (m_options.end_addr - m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000383 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000384 }
385 }
386 }
387
388 if (m_options.num_instructions != 0)
389 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000390 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000391 {
392 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000393 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000394 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000395 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000396 if (sc.function)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000397 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000398 else if (sc.symbol && sc.symbol->ValueIsAddress())
399 range.GetBaseAddress() = sc.symbol->GetAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000400 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000401 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000402 }
403
Greg Clayton24bc5d92011-03-30 18:16:51 +0000404 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000405 {
406 result.AppendError ("invalid frame");
407 result.SetStatus (eReturnStatusFailed);
408 return false;
409 }
410 }
411
412 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000413 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000414 plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000415 exe_ctx,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000416 range.GetBaseAddress(),
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000417 m_options.num_instructions,
418 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000419 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000420 result.GetOutputStream()))
421 {
422 result.SetStatus (eReturnStatusSuccessFinishResult);
423 }
424 else
425 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000426 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000427 result.SetStatus (eReturnStatusFailed);
428 }
429 }
430 else
431 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000432 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000433 {
434 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000435 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000436 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000437 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000438 if (sc.function)
439 range = sc.function->GetAddressRange();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000440 else if (sc.symbol && sc.symbol->ValueIsAddress())
441 {
442 range.GetBaseAddress() = sc.symbol->GetAddress();
443 range.SetByteSize (sc.symbol->GetByteSize());
444 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000445 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000446 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000447 }
448 else
449 {
450 result.AppendError ("invalid frame");
451 result.SetStatus (eReturnStatusFailed);
452 return false;
453 }
454 }
455 if (range.GetByteSize() == 0)
456 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
457
458 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000459 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000460 plugin_name,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000461 exe_ctx,
462 range,
463 m_options.num_instructions,
464 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000465 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000466 result.GetOutputStream()))
467 {
468 result.SetStatus (eReturnStatusSuccessFinishResult);
469 }
470 else
471 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000472 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000473 result.SetStatus (eReturnStatusFailed);
474 }
Greg Clayton70436352010-06-30 23:03:03 +0000475 }
Chris Lattner24943d22010-06-08 16:52:24 +0000476 }
477
Jim Ingham34e9a982010-06-15 18:47:14 +0000478 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000479}