1 //===-- 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"
17 #include "lldb/Interpreter/Args.h"
18 #include "lldb/Interpreter/CommandCompletions.h"
19 #include "lldb/Interpreter/CommandInterpreter.h"
20 #include "lldb/Interpreter/CommandReturnObject.h"
21 #include "lldb/Core/Disassembler.h"
22 #include "lldb/Interpreter/Options.h"
23 #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
30 #define DEFAULT_DISASM_NUM_INS  4
31 
32 using namespace lldb;
33 using namespace lldb_private;
34 
35 CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) :
36     Options(interpreter),
37     num_lines_context(0),
38     num_instructions (0),
39     func_name(),
40     cur_function (false),
41     start_addr(),
42     end_addr (),
43     at_pc (false),
44     frame_line (false),
45     plugin_name (),
46     arch(),
47     some_location_specified (false)
48 {
49     OptionParsingStarting();
50 }
51 
52 CommandObjectDisassemble::CommandOptions::~CommandOptions ()
53 {
54 }
55 
56 Error
57 CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg)
58 {
59     Error error;
60 
61     char short_option = (char) m_getopt_table[option_idx].val;
62 
63     bool success;
64 
65     switch (short_option)
66     {
67     case 'm':
68         show_mixed = true;
69         break;
70 
71     case 'C':
72         num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success);
73         if (!success)
74             error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg);
75         break;
76 
77     case 'c':
78         num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success);
79         if (!success)
80             error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg);
81         break;
82 
83     case 'b':
84         show_bytes = true;
85         break;
86 
87     case 's':
88         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);
91 
92         if (start_addr == LLDB_INVALID_ADDRESS)
93             error.SetErrorStringWithFormat ("invalid start address string '%s'", option_arg);
94         some_location_specified = true;
95         break;
96     case 'e':
97         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);
100 
101         if (end_addr == LLDB_INVALID_ADDRESS)
102             error.SetErrorStringWithFormat ("invalid end address string '%s'", option_arg);
103         break;
104         some_location_specified = true;
105     case 'n':
106         func_name.assign (option_arg);
107         some_location_specified = true;
108         break;
109 
110     case 'p':
111         at_pc = true;
112         some_location_specified = true;
113         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;
120         some_location_specified = true;
121         break;
122 
123     case 'P':
124         plugin_name.assign (option_arg);
125         break;
126 
127     case 'r':
128         raw = true;
129         break;
130 
131     case 'f':
132         cur_function = true;
133         some_location_specified = true;
134         break;
135 
136     case 'a':
137         if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get()))
138             arch.SetTriple (option_arg);
139         break;
140 
141     default:
142         error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option);
143         break;
144     }
145 
146     return error;
147 }
148 
149 void
150 CommandObjectDisassemble::CommandOptions::OptionParsingStarting ()
151 {
152     show_mixed = false;
153     show_bytes = false;
154     num_lines_context = 0;
155     num_instructions = 0;
156     func_name.clear();
157     cur_function = false;
158     at_pc = false;
159     frame_line = false;
160     start_addr = LLDB_INVALID_ADDRESS;
161     end_addr = LLDB_INVALID_ADDRESS;
162     raw = false;
163     plugin_name.clear();
164     arch.Clear();
165     some_location_specified = false;
166 }
167 
168 Error
169 CommandObjectDisassemble::CommandOptions::OptionParsingFinished ()
170 {
171     if (!some_location_specified)
172         at_pc = true;
173     return Error();
174 
175 }
176 
177 const OptionDefinition*
178 CommandObjectDisassemble::CommandOptions::GetDefinitions ()
179 {
180     return g_option_table;
181 }
182 
183 OptionDefinition
184 CommandObjectDisassemble::CommandOptions::g_option_table[] =
185 {
186 { 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."},
199 { 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."},
203 { 0                 , false , NULL,             0,   0                  , NULL, 0, eArgTypeNone,        NULL }
204 };
205 
206 
207 
208 //-------------------------------------------------------------------------
209 // CommandObjectDisassemble
210 //-------------------------------------------------------------------------
211 
212 CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) :
213     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>]"),
217     m_options (interpreter)
218 {
219 }
220 
221 CommandObjectDisassemble::~CommandObjectDisassemble()
222 {
223 }
224 
225 bool
226 CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result)
227 {
228     Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get();
229     if (target == NULL)
230     {
231         result.AppendError ("invalid target, create a debug target using the 'target create' command");
232         result.SetStatus (eReturnStatusFailed);
233         return false;
234     }
235     if (!m_options.arch.IsValid())
236         m_options.arch = target->GetArchitecture();
237 
238     if (!m_options.arch.IsValid())
239     {
240         result.AppendError ("use the --arch option or set the target architecure to disassemble");
241         result.SetStatus (eReturnStatusFailed);
242         return false;
243     }
244 
245     const char *plugin_name = m_options.GetPluginName ();
246     DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, plugin_name);
247 
248     if (!disassembler)
249     {
250         if (plugin_name)
251             result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n",
252                                           plugin_name,
253                                           m_options.arch.GetArchitectureName());
254         else
255             result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n",
256                                           m_options.arch.GetArchitectureName());
257         result.SetStatus (eReturnStatusFailed);
258         return false;
259     }
260 
261     result.SetStatus (eReturnStatusSuccessFinishResult);
262 
263     if (command.GetArgumentCount() != 0)
264     {
265         result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n");
266         GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this);
267         result.SetStatus (eReturnStatusFailed);
268         return false;
269     }
270 
271     if (m_options.show_mixed && m_options.num_lines_context == 0)
272         m_options.num_lines_context = 1;
273 
274     ExecutionContext exe_ctx(m_interpreter.GetExecutionContext());
275     // Always show the PC in the disassembly
276     uint32_t options = Disassembler::eOptionMarkPCAddress;
277 
278     // 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;
281 
282     if (m_options.show_bytes)
283         options |= Disassembler::eOptionShowBytes;
284 
285     if (m_options.raw)
286         options |= Disassembler::eOptionRawOuput;
287 
288     if (!m_options.func_name.empty())
289     {
290         ConstString name(m_options.func_name.c_str());
291 
292         if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
293                                        m_options.arch,
294                                        plugin_name,
295                                        exe_ctx,
296                                        name,
297                                        NULL,    // Module *
298                                        m_options.num_instructions,
299                                        m_options.show_mixed ? m_options.num_lines_context : 0,
300                                        options,
301                                        result.GetOutputStream()))
302         {
303             result.SetStatus (eReturnStatusSuccessFinishResult);
304         }
305         else
306         {
307             result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
308             result.SetStatus (eReturnStatusFailed);
309         }
310     }
311     else
312     {
313         AddressRange range;
314         StackFrame *frame = exe_ctx.GetFramePtr();
315         if (m_options.frame_line)
316         {
317             if (frame == NULL)
318             {
319                 result.AppendError ("Cannot disassemble around the current line without a selected frame.\n");
320                 result.SetStatus (eReturnStatusFailed);
321                 return false;
322             }
323             LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry);
324             if (pc_line_entry.IsValid())
325             {
326                 range = pc_line_entry.range;
327             }
328             else
329             {
330                 m_options.at_pc = true; // No line entry, so just disassemble around the current pc
331                 m_options.show_mixed = false;
332             }
333         }
334         else if (m_options.cur_function)
335         {
336             if (frame == NULL)
337             {
338                 result.AppendError ("Cannot disassemble around the current function without a selected frame.\n");
339                 result.SetStatus (eReturnStatusFailed);
340                 return false;
341             }
342             Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol;
343             if (symbol)
344             {
345                 range.GetBaseAddress() = symbol->GetAddress();
346                 range.SetByteSize(symbol->GetByteSize());
347             }
348         }
349 
350         // Did the "m_options.frame_line" find a valid range already? If so
351         // skip the rest...
352         if (range.GetByteSize() == 0)
353         {
354             if (m_options.at_pc)
355             {
356                 if (frame == NULL)
357                 {
358                     result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n");
359                     result.SetStatus (eReturnStatusFailed);
360                     return false;
361                 }
362                 range.GetBaseAddress() = frame->GetFrameCodeAddress();
363                 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)
375                     {
376                         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);
383                     }
384                 }
385             }
386         }
387 
388         if (m_options.num_instructions != 0)
389         {
390             if (!range.GetBaseAddress().IsValid())
391             {
392                 // The default action is to disassemble the current frame function.
393                 if (frame)
394                 {
395                     SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
396                     if (sc.function)
397                         range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress();
398                     else if (sc.symbol && sc.symbol->ValueIsAddress())
399                         range.GetBaseAddress() = sc.symbol->GetAddress();
400                     else
401                         range.GetBaseAddress() = frame->GetFrameCodeAddress();
402                 }
403 
404                 if (!range.GetBaseAddress().IsValid())
405                 {
406                     result.AppendError ("invalid frame");
407                     result.SetStatus (eReturnStatusFailed);
408                     return false;
409                 }
410             }
411 
412             if (Disassembler::Disassemble (m_interpreter.GetDebugger(),
413                                            m_options.arch,
414                                            plugin_name,
415                                            exe_ctx,
416                                            range.GetBaseAddress(),
417                                            m_options.num_instructions,
418                                            m_options.show_mixed ? m_options.num_lines_context : 0,
419                                            options,
420                                            result.GetOutputStream()))
421             {
422                 result.SetStatus (eReturnStatusSuccessFinishResult);
423             }
424             else
425             {
426                 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.start_addr);
427                 result.SetStatus (eReturnStatusFailed);
428             }
429         }
430         else
431         {
432             if (!range.GetBaseAddress().IsValid())
433             {
434                 // The default action is to disassemble the current frame function.
435                 if (frame)
436                 {
437                     SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
438                     if (sc.function)
439                         range = sc.function->GetAddressRange();
440                     else if (sc.symbol && sc.symbol->ValueIsAddress())
441                     {
442                         range.GetBaseAddress() = sc.symbol->GetAddress();
443                         range.SetByteSize (sc.symbol->GetByteSize());
444                     }
445                     else
446                         range.GetBaseAddress() = frame->GetFrameCodeAddress();
447                 }
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(),
459                                            m_options.arch,
460                                            plugin_name,
461                                            exe_ctx,
462                                            range,
463                                            m_options.num_instructions,
464                                            m_options.show_mixed ? m_options.num_lines_context : 0,
465                                            options,
466                                            result.GetOutputStream()))
467             {
468                 result.SetStatus (eReturnStatusSuccessFinishResult);
469             }
470             else
471             {
472                 result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.start_addr);
473                 result.SetStatus (eReturnStatusFailed);
474             }
475         }
476     }
477 
478     return result.Succeeded();
479 }
480