blob: 7a4d1e727c00b59ccd013d232c21c6f121956b72 [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 Ingham7d408382013-03-02 00:26:47 +000049 flavor_string(),
Jim Inghamd2ad7fd2011-09-01 01:11:04 +000050 arch(),
51 some_location_specified (false)
Chris Lattner24943d22010-06-08 16:52:24 +000052{
Greg Clayton143fcc32011-04-13 00:18:08 +000053 OptionParsingStarting();
Chris Lattner24943d22010-06-08 16:52:24 +000054}
55
56CommandObjectDisassemble::CommandOptions::~CommandOptions ()
57{
58}
59
60Error
Greg Clayton143fcc32011-04-13 00:18:08 +000061CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
Chris Lattner24943d22010-06-08 16:52:24 +000062{
63 Error error;
64
Greg Clayton6475c422012-12-04 00:32:51 +000065 const int short_option = m_getopt_table[option_idx].val;
Chris Lattner24943d22010-06-08 16:52:24 +000066
Jim Inghamaa3e3e12011-03-22 01:48:42 +000067 bool success;
68
Chris Lattner24943d22010-06-08 16:52:24 +000069 switch (short_option)
70 {
71 case 'm':
72 show_mixed = true;
73 break;
74
Greg Clayton889fbd02011-03-26 19:14:58 +000075 case 'C':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000076 num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
77 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000078 error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
Jim Inghamaa3e3e12011-03-22 01:48:42 +000079 break;
80
Chris Lattner24943d22010-06-08 16:52:24 +000081 case 'c':
Jim Inghamaa3e3e12011-03-22 01:48:42 +000082 num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
83 if (!success)
Greg Clayton9c236732011-10-26 00:56:27 +000084 error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
Chris Lattner24943d22010-06-08 16:52:24 +000085 break;
86
87 case 'b':
88 show_bytes = true;
89 break;
90
Jim Ingham34e9a982010-06-15 18:47:14 +000091 case 's':
Greg Clayton49d888d2012-12-06 22:49:16 +000092 {
93 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
94 start_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
95 if (start_addr != LLDB_INVALID_ADDRESS)
96 some_location_specified = true;
97 }
Jim Ingham34e9a982010-06-15 18:47:14 +000098 break;
99 case 'e':
Greg Clayton49d888d2012-12-06 22:49:16 +0000100 {
101 ExecutionContext exe_ctx (m_interpreter.GetExecutionContext());
102 end_addr = Args::StringToAddress(&exe_ctx, option_arg, LLDB_INVALID_ADDRESS, &error);
103 if (end_addr != LLDB_INVALID_ADDRESS)
104 some_location_specified = true;
105 }
Chris Lattner24943d22010-06-08 16:52:24 +0000106 break;
Chris Lattner24943d22010-06-08 16:52:24 +0000107 case 'n':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000108 func_name.assign (option_arg);
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000109 some_location_specified = true;
Chris Lattner24943d22010-06-08 16:52:24 +0000110 break;
111
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000112 case 'p':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000113 at_pc = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000114 some_location_specified = true;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000115 break;
116
117 case 'l':
118 frame_line = true;
119 // Disassemble the current source line kind of implies showing mixed
120 // source code context.
121 show_mixed = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000122 some_location_specified = true;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000123 break;
124
Greg Clayton149731c2011-03-25 18:03:16 +0000125 case 'P':
Greg Clayton24bc5d92011-03-30 18:16:51 +0000126 plugin_name.assign (option_arg);
Greg Clayton149731c2011-03-25 18:03:16 +0000127 break;
128
Jim Ingham7d408382013-03-02 00:26:47 +0000129 case 'F':
130 {
131 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
132 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
133 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
134 {
135 flavor_string.assign (option_arg);
136 }
137 else
138 error.SetErrorStringWithFormat("Disassembler flavors are currently only supported for x86 and x86_64 targets.");
139 break;
140 }
Chris Lattner24943d22010-06-08 16:52:24 +0000141 case 'r':
142 raw = true;
143 break;
144
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000145 case 'f':
Greg Clayton8d646f22012-12-14 22:36:35 +0000146 current_function = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000147 some_location_specified = true;
Johnny Chen61c1b8b2010-10-29 19:33:40 +0000148 break;
149
Greg Clayton889fbd02011-03-26 19:14:58 +0000150 case 'a':
Greg Claytonb170aee2012-05-08 01:45:38 +0000151 if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
152 arch.SetTriple (option_arg);
Greg Clayton889fbd02011-03-26 19:14:58 +0000153 break;
154
Chris Lattner24943d22010-06-08 16:52:24 +0000155 default:
Greg Clayton9c236732011-10-26 00:56:27 +0000156 error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
Chris Lattner24943d22010-06-08 16:52:24 +0000157 break;
158 }
159
160 return error;
161}
162
163void
Greg Clayton143fcc32011-04-13 00:18:08 +0000164CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
Chris Lattner24943d22010-06-08 16:52:24 +0000165{
Chris Lattner24943d22010-06-08 16:52:24 +0000166 show_mixed = false;
167 show_bytes = false;
168 num_lines_context = 0;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000169 num_instructions = 0;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000170 func_name.clear();
Greg Clayton8d646f22012-12-14 22:36:35 +0000171 current_function = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000172 at_pc = false;
173 frame_line = false;
174 start_addr = LLDB_INVALID_ADDRESS;
175 end_addr = LLDB_INVALID_ADDRESS;
Sean Callanan944be702010-06-17 00:32:05 +0000176 raw = false;
Greg Clayton24bc5d92011-03-30 18:16:51 +0000177 plugin_name.clear();
Jim Ingham7d408382013-03-02 00:26:47 +0000178
179 Target *target = m_interpreter.GetExecutionContext().GetTargetPtr();
180
181 // This is a hack till we get the ability to specify features based on architecture. For now GetDisassemblyFlavor
182 // is really only valid for x86 (and for the llvm assembler plugin, but I'm papering over that since that is the
183 // only disassembler plugin we have...
184 if (target)
185 {
186 if (target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86
187 || target->GetArchitecture().GetTriple().getArch() == llvm::Triple::x86_64)
188 {
189 flavor_string.assign(target->GetDisassemblyFlavor());
190 }
191 else
192 flavor_string.assign ("default");
193
194 }
195 else
196 flavor_string.assign("default");
197
Greg Clayton24bc5d92011-03-30 18:16:51 +0000198 arch.Clear();
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000199 some_location_specified = false;
200}
201
202Error
203CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
204{
205 if (!some_location_specified)
Greg Clayton8d646f22012-12-14 22:36:35 +0000206 current_function = true;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000207 return Error();
208
Chris Lattner24943d22010-06-08 16:52:24 +0000209}
210
Greg Claytonb3448432011-03-24 21:19:54 +0000211const OptionDefinition*
Chris Lattner24943d22010-06-08 16:52:24 +0000212CommandObjectDisassemble::CommandOptions::GetDefinitions ()
213{
214 return g_option_table;
215}
216
Greg Claytonb3448432011-03-24 21:19:54 +0000217OptionDefinition
Chris Lattner24943d22010-06-08 16:52:24 +0000218CommandObjectDisassemble::CommandOptions::g_option_table[] =
219{
Greg Clayton8d646f22012-12-14 22:36:35 +0000220{ LLDB_OPT_SET_ALL, false, "bytes" , 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."},
221{ LLDB_OPT_SET_ALL, false, "context" , 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."},
222{ LLDB_OPT_SET_ALL, false, "mixed" , 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."},
223{ LLDB_OPT_SET_ALL, false, "raw" , 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."},
224{ LLDB_OPT_SET_ALL, false, "plugin" , 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."},
Jim Ingham7d408382013-03-02 00:26:47 +0000225{ LLDB_OPT_SET_ALL, false, "flavor" , 'F', required_argument , NULL, 0, eArgTypeDisassemblyFlavor, "Name of the disassembly flavor you want to use. "
226 "Currently the only valid options are default, and for Intel"
227 " architectures, att and intel."},
Greg Clayton8d646f22012-12-14 22:36:35 +0000228{ LLDB_OPT_SET_ALL, false, "arch" , 'a', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."},
229{ LLDB_OPT_SET_1 |
Enrico Granata4968ad82013-01-29 01:48:30 +0000230 LLDB_OPT_SET_2 , true , "start-address", 's', required_argument , NULL, 0, eArgTypeAddressOrExpression,"Address at which to start disassembling."},
231{ LLDB_OPT_SET_1 , false, "end-address" , 'e', required_argument , NULL, 0, eArgTypeAddressOrExpression, "Address at which to end disassembling."},
Greg Clayton8d646f22012-12-14 22:36:35 +0000232{ LLDB_OPT_SET_2 |
233 LLDB_OPT_SET_3 |
234 LLDB_OPT_SET_4 |
235 LLDB_OPT_SET_5 , false, "count" , 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."},
236{ LLDB_OPT_SET_3 , false, "name" , 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName,
237 "Disassemble entire contents of the given function name."},
238{ LLDB_OPT_SET_4 , false, "frame" , 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."},
239{ LLDB_OPT_SET_5 , false, "pc" , 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."},
240{ 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."},
241{ 0 , false, NULL , 0, 0 , NULL, 0, eArgTypeNone, NULL }
Chris Lattner24943d22010-06-08 16:52:24 +0000242};
243
244
245
246//-------------------------------------------------------------------------
247// CommandObjectDisassemble
248//-------------------------------------------------------------------------
249
Greg Clayton238c0a12010-09-18 01:14:36 +0000250CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
Jim Inghamda26bd22012-06-08 21:56:10 +0000251 CommandObjectParsed (interpreter,
252 "disassemble",
253 "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.",
254 "disassemble [<cmd-options>]"),
Greg Claytonf15996e2011-04-07 22:46:35 +0000255 m_options (interpreter)
Chris Lattner24943d22010-06-08 16:52:24 +0000256{
257}
258
259CommandObjectDisassemble::~CommandObjectDisassemble()
260{
261}
262
Chris Lattner24943d22010-06-08 16:52:24 +0000263bool
Jim Inghamda26bd22012-06-08 21:56:10 +0000264CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
Chris Lattner24943d22010-06-08 16:52:24 +0000265{
Greg Clayton238c0a12010-09-18 01:14:36 +0000266 Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
Chris Lattner24943d22010-06-08 16:52:24 +0000267 if (target == NULL)
268 {
Greg Claytone1f50b92011-05-03 22:09:39 +0000269 result.AppendError ("invalid target, create a debug target using the 'target create' command");
Chris Lattner24943d22010-06-08 16:52:24 +0000270 result.SetStatus (eReturnStatusFailed);
271 return false;
272 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000273 if (!m_options.arch.IsValid())
274 m_options.arch = target->GetArchitecture();
Chris Lattner24943d22010-06-08 16:52:24 +0000275
Greg Clayton24bc5d92011-03-30 18:16:51 +0000276 if (!m_options.arch.IsValid())
Chris Lattner24943d22010-06-08 16:52:24 +0000277 {
Greg Clayton889fbd02011-03-26 19:14:58 +0000278 result.AppendError ("use the --arch option or set the target architecure to disassemble");
Chris Lattner24943d22010-06-08 16:52:24 +0000279 result.SetStatus (eReturnStatusFailed);
280 return false;
281 }
282
Greg Clayton149731c2011-03-25 18:03:16 +0000283 const char *plugin_name = m_options.GetPluginName ();
Jim Ingham7d408382013-03-02 00:26:47 +0000284 const char *flavor_string = m_options.GetFlavorString();
285
286 DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, flavor_string, plugin_name);
Chris Lattner24943d22010-06-08 16:52:24 +0000287
Sean Callananb386d822012-08-09 00:50:26 +0000288 if (!disassembler)
Chris Lattner24943d22010-06-08 16:52:24 +0000289 {
Greg Clayton149731c2011-03-25 18:03:16 +0000290 if (plugin_name)
Jim Ingham7d408382013-03-02 00:26:47 +0000291 {
292 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
Greg Clayton889fbd02011-03-26 19:14:58 +0000293 plugin_name,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000294 m_options.arch.GetArchitectureName());
Jim Ingham7d408382013-03-02 00:26:47 +0000295 }
Greg Clayton149731c2011-03-25 18:03:16 +0000296 else
Greg Clayton889fbd02011-03-26 19:14:58 +0000297 result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
Greg Clayton24bc5d92011-03-30 18:16:51 +0000298 m_options.arch.GetArchitectureName());
Chris Lattner24943d22010-06-08 16:52:24 +0000299 result.SetStatus (eReturnStatusFailed);
300 return false;
301 }
Jim Ingham7d408382013-03-02 00:26:47 +0000302 else if (flavor_string != NULL && !disassembler->FlavorValidForArchSpec(m_options.arch, flavor_string))
303 result.AppendWarningWithFormat("invalid disassembler flavor \"%s\", using default.\n", flavor_string);
Chris Lattner24943d22010-06-08 16:52:24 +0000304
305 result.SetStatus (eReturnStatusSuccessFinishResult);
306
Greg Clayton70436352010-06-30 23:03:03 +0000307 if (command.GetArgumentCount() != 0)
Chris Lattner24943d22010-06-08 16:52:24 +0000308 {
Greg Clayton238c0a12010-09-18 01:14:36 +0000309 result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
Greg Claytonf15996e2011-04-07 22:46:35 +0000310 GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
Jim Ingham34e9a982010-06-15 18:47:14 +0000311 result.SetStatus (eReturnStatusFailed);
312 return false;
313 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000314
Greg Clayton70436352010-06-30 23:03:03 +0000315 if (m_options.show_mixed && m_options.num_lines_context == 0)
Greg Clayton1924e242010-09-15 05:51:24 +0000316 m_options.num_lines_context = 1;
Chris Lattner24943d22010-06-08 16:52:24 +0000317
Greg Clayton6377f5c2011-06-28 19:01:40 +0000318 // Always show the PC in the disassembly
319 uint32_t options = Disassembler::eOptionMarkPCAddress;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000320
Greg Clayton6377f5c2011-06-28 19:01:40 +0000321 // Mark the source line for the current PC only if we are doing mixed source and assembly
322 if (m_options.show_mixed)
323 options |= Disassembler::eOptionMarkPCSourceLine;
Greg Clayton4bb0f192011-06-22 01:39:49 +0000324
325 if (m_options.show_bytes)
326 options |= Disassembler::eOptionShowBytes;
327
328 if (m_options.raw)
329 options |= Disassembler::eOptionRawOuput;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000330
Greg Clayton24bc5d92011-03-30 18:16:51 +0000331 if (!m_options.func_name.empty())
Greg Clayton70436352010-06-30 23:03:03 +0000332 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000333 ConstString name(m_options.func_name.c_str());
Greg Clayton70436352010-06-30 23:03:03 +0000334
Greg Clayton238c0a12010-09-18 01:14:36 +0000335 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000336 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000337 plugin_name,
Jim Ingham7d408382013-03-02 00:26:47 +0000338 flavor_string,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000339 m_exe_ctx,
Greg Clayton70436352010-06-30 23:03:03 +0000340 name,
341 NULL, // Module *
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000342 m_options.num_instructions,
Greg Clayton70436352010-06-30 23:03:03 +0000343 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000344 options,
Greg Clayton70436352010-06-30 23:03:03 +0000345 result.GetOutputStream()))
Chris Lattner24943d22010-06-08 16:52:24 +0000346 {
Greg Clayton70436352010-06-30 23:03:03 +0000347 result.SetStatus (eReturnStatusSuccessFinishResult);
Chris Lattner24943d22010-06-08 16:52:24 +0000348 }
349 else
350 {
351 result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
352 result.SetStatus (eReturnStatusFailed);
Chris Lattner24943d22010-06-08 16:52:24 +0000353 }
Greg Clayton70436352010-06-30 23:03:03 +0000354 }
Jim Ingham34e9a982010-06-15 18:47:14 +0000355 else
Chris Lattner24943d22010-06-08 16:52:24 +0000356 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000357 AddressRange range;
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000358 StackFrame *frame = m_exe_ctx.GetFramePtr();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000359 if (m_options.frame_line)
Greg Clayton70436352010-06-30 23:03:03 +0000360 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000361 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000362 {
363 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
364 result.SetStatus (eReturnStatusFailed);
365 return false;
366 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000367 LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
Greg Clayton24bc5d92011-03-30 18:16:51 +0000368 if (pc_line_entry.IsValid())
Greg Clayton70436352010-06-30 23:03:03 +0000369 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000370 range = pc_line_entry.range;
Greg Clayton70436352010-06-30 23:03:03 +0000371 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000372 else
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000373 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000374 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
375 m_options.show_mixed = false;
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000376 }
Greg Clayton70436352010-06-30 23:03:03 +0000377 }
Greg Clayton8d646f22012-12-14 22:36:35 +0000378 else if (m_options.current_function)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000379 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000380 if (frame == NULL)
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000381 {
382 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
383 result.SetStatus (eReturnStatusFailed);
384 return false;
385 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000386 Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000387 if (symbol)
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000388 {
389 range.GetBaseAddress() = symbol->GetAddress();
390 range.SetByteSize(symbol->GetByteSize());
391 }
Jim Inghamd2ad7fd2011-09-01 01:11:04 +0000392 }
Greg Clayton24bc5d92011-03-30 18:16:51 +0000393
394 // Did the "m_options.frame_line" find a valid range already? If so
395 // skip the rest...
396 if (range.GetByteSize() == 0)
Greg Clayton70436352010-06-30 23:03:03 +0000397 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000398 if (m_options.at_pc)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000399 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000400 if (frame == NULL)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000401 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000402 result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
403 result.SetStatus (eReturnStatusFailed);
404 return false;
405 }
Greg Clayton567e7f32011-09-22 04:58:26 +0000406 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Greg Clayton24bc5d92011-03-30 18:16:51 +0000407 if (m_options.num_instructions == 0)
408 {
409 // Disassembling at the PC always disassembles some number of instructions (not the whole function).
410 m_options.num_instructions = DEFAULT_DISASM_NUM_INS;
411 }
412 }
413 else
414 {
415 range.GetBaseAddress().SetOffset (m_options.start_addr);
416 if (range.GetBaseAddress().IsValid())
417 {
418 if (m_options.end_addr != LLDB_INVALID_ADDRESS)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000419 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000420 if (m_options.end_addr <= m_options.start_addr)
421 {
422 result.AppendErrorWithFormat ("End address before start address.\n");
423 result.SetStatus (eReturnStatusFailed);
424 return false;
425 }
426 range.SetByteSize (m_options.end_addr - m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000427 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000428 }
429 }
430 }
431
432 if (m_options.num_instructions != 0)
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)
Greg Clayton24bc5d92011-03-30 18:16:51 +0000441 range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000442 else if (sc.symbol && sc.symbol->ValueIsAddress())
443 range.GetBaseAddress() = sc.symbol->GetAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000444 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000445 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000446 }
447
Greg Clayton24bc5d92011-03-30 18:16:51 +0000448 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000449 {
450 result.AppendError ("invalid frame");
451 result.SetStatus (eReturnStatusFailed);
452 return false;
453 }
454 }
455
456 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000457 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000458 plugin_name,
Jim Ingham7d408382013-03-02 00:26:47 +0000459 flavor_string,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000460 m_exe_ctx,
Greg Clayton24bc5d92011-03-30 18:16:51 +0000461 range.GetBaseAddress(),
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000462 m_options.num_instructions,
463 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000464 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000465 result.GetOutputStream()))
466 {
467 result.SetStatus (eReturnStatusSuccessFinishResult);
468 }
469 else
470 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000471 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000472 result.SetStatus (eReturnStatusFailed);
473 }
474 }
475 else
476 {
Greg Clayton24bc5d92011-03-30 18:16:51 +0000477 if (!range.GetBaseAddress().IsValid())
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000478 {
479 // The default action is to disassemble the current frame function.
Greg Clayton567e7f32011-09-22 04:58:26 +0000480 if (frame)
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000481 {
Greg Clayton567e7f32011-09-22 04:58:26 +0000482 SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000483 if (sc.function)
484 range = sc.function->GetAddressRange();
Greg Clayton0c31d3d2012-03-07 21:03:09 +0000485 else if (sc.symbol && sc.symbol->ValueIsAddress())
486 {
487 range.GetBaseAddress() = sc.symbol->GetAddress();
488 range.SetByteSize (sc.symbol->GetByteSize());
489 }
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000490 else
Greg Clayton567e7f32011-09-22 04:58:26 +0000491 range.GetBaseAddress() = frame->GetFrameCodeAddress();
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000492 }
493 else
494 {
495 result.AppendError ("invalid frame");
496 result.SetStatus (eReturnStatusFailed);
497 return false;
498 }
499 }
500 if (range.GetByteSize() == 0)
501 range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
502
503 if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
Greg Clayton24bc5d92011-03-30 18:16:51 +0000504 m_options.arch,
Greg Clayton149731c2011-03-25 18:03:16 +0000505 plugin_name,
Jim Ingham7d408382013-03-02 00:26:47 +0000506 flavor_string,
Greg Claytonea0bb4d2013-01-09 19:44:40 +0000507 m_exe_ctx,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000508 range,
509 m_options.num_instructions,
510 m_options.show_mixed ? m_options.num_lines_context : 0,
Greg Clayton4bb0f192011-06-22 01:39:49 +0000511 options,
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000512 result.GetOutputStream()))
513 {
514 result.SetStatus (eReturnStatusSuccessFinishResult);
515 }
516 else
517 {
Daniel Malea5f35a4b2012-11-29 21:49:15 +0000518 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr);
Jim Inghamaa3e3e12011-03-22 01:48:42 +0000519 result.SetStatus (eReturnStatusFailed);
520 }
Greg Clayton70436352010-06-30 23:03:03 +0000521 }
Chris Lattner24943d22010-06-08 16:52:24 +0000522 }
523
Jim Ingham34e9a982010-06-15 18:47:14 +0000524 return result.Succeeded();
Chris Lattner24943d22010-06-08 16:52:24 +0000525}