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 
31 using namespace lldb;
32 using namespace lldb_private;
33 
34 CommandObjectDisassemble::CommandOptions::CommandOptions () :
35     Options(),
36     m_func_name(),
37     m_start_addr(),
38     m_end_addr ()
39 {
40     ResetOptionValues();
41 }
42 
43 CommandObjectDisassemble::CommandOptions::~CommandOptions ()
44 {
45 }
46 
47 Error
48 CommandObjectDisassemble::CommandOptions::SetOptionValue (int option_idx, const char *option_arg)
49 {
50     Error error;
51 
52     char short_option = (char) m_getopt_table[option_idx].val;
53 
54     switch (short_option)
55     {
56     case 'm':
57         show_mixed = true;
58         break;
59 
60     case 'c':
61         num_lines_context = Args::StringToUInt32(option_arg, 0, 0);
62         break;
63 
64     case 'b':
65         show_bytes = true;
66         break;
67 
68     case 's':
69         m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0);
70         if (m_start_addr == LLDB_INVALID_ADDRESS)
71             m_start_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16);
72 
73         if (m_start_addr == LLDB_INVALID_ADDRESS)
74             error.SetErrorStringWithFormat ("Invalid start address string '%s'.\n", optarg);
75         break;
76     case 'e':
77         m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 0);
78         if (m_end_addr == LLDB_INVALID_ADDRESS)
79             m_end_addr = Args::StringToUInt64(optarg, LLDB_INVALID_ADDRESS, 16);
80 
81         if (m_end_addr == LLDB_INVALID_ADDRESS)
82             error.SetErrorStringWithFormat ("Invalid end address string '%s'.\n", optarg);
83         break;
84 
85     case 'n':
86         m_func_name = option_arg;
87         break;
88 
89     case 'r':
90         raw = true;
91         break;
92 
93     default:
94         error.SetErrorStringWithFormat("Unrecognized short option '%c'.\n", short_option);
95         break;
96     }
97 
98     return error;
99 }
100 
101 void
102 CommandObjectDisassemble::CommandOptions::ResetOptionValues ()
103 {
104     Options::ResetOptionValues();
105     show_mixed = false;
106     show_bytes = false;
107     num_lines_context = 0;
108     m_func_name.clear();
109     m_start_addr = LLDB_INVALID_ADDRESS;
110     m_end_addr = LLDB_INVALID_ADDRESS;
111     raw = false;
112 }
113 
114 const lldb::OptionDefinition*
115 CommandObjectDisassemble::CommandOptions::GetDefinitions ()
116 {
117     return g_option_table;
118 }
119 
120 lldb::OptionDefinition
121 CommandObjectDisassemble::CommandOptions::g_option_table[] =
122 {
123 { LLDB_OPT_SET_ALL, false, "bytes",    'b', no_argument,       NULL, 0, NULL,             "Show opcode bytes when disassembling."},
124 { LLDB_OPT_SET_ALL, false, "context",  'c', required_argument, NULL, 0, "<num-lines>",    "Number of context lines of source to show."},
125 { LLDB_OPT_SET_ALL, false, "mixed",    'm', no_argument,       NULL, 0, NULL,             "Enable mixed source and assembly display."},
126 { LLDB_OPT_SET_ALL, false, "raw",      'r', no_argument,       NULL, 0, NULL,             "Print raw disassembly with no symbol information."},
127 
128 { LLDB_OPT_SET_1, true, "start-address",  's', required_argument, NULL, 0, "<start-address>",      "Address to start disassembling."},
129 { LLDB_OPT_SET_1, false, "end-address",  'e', required_argument, NULL, 0, "<end-address>",      "Address to start disassembling."},
130 
131 { LLDB_OPT_SET_2, true, "name",     'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, "<function-name>",             "Disassemble entire contents of the given function name."},
132 
133 { LLDB_OPT_SET_3, false, "current-frame",     'f', no_argument, NULL, 0, "<current-frame>",             "Disassemble entire contents of the current frame's function."},
134 
135 { 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
136 };
137 
138 
139 
140 //-------------------------------------------------------------------------
141 // CommandObjectDisassemble
142 //-------------------------------------------------------------------------
143 
144 CommandObjectDisassemble::CommandObjectDisassemble () :
145     CommandObject ("disassemble",
146                      "Disassemble bytes in the current function or anywhere in the inferior program.",
147                      "disassemble [<cmd-options>]")
148 {
149 }
150 
151 CommandObjectDisassemble::~CommandObjectDisassemble()
152 {
153 }
154 
155 bool
156 CommandObjectDisassemble::Execute
157 (
158     CommandInterpreter &interpreter,
159     Args& command,
160     CommandReturnObject &result
161 )
162 {
163     Target *target = interpreter.GetDebugger().GetCurrentTarget().get();
164     if (target == NULL)
165     {
166         result.AppendError ("invalid target, set executable file using 'file' command");
167         result.SetStatus (eReturnStatusFailed);
168         return false;
169     }
170 
171     ArchSpec arch(target->GetArchitecture());
172     if (!arch.IsValid())
173     {
174         result.AppendError ("target needs valid architecure in order to be able to disassemble");
175         result.SetStatus (eReturnStatusFailed);
176         return false;
177     }
178 
179     Disassembler *disassembler = Disassembler::FindPlugin(arch);
180 
181     if (disassembler == NULL)
182     {
183         result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.AsCString());
184         result.SetStatus (eReturnStatusFailed);
185         return false;
186     }
187 
188     result.SetStatus (eReturnStatusSuccessFinishResult);
189 
190     if (command.GetArgumentCount() != 0)
191     {
192         result.AppendErrorWithFormat ("\"disassemble\" doesn't take any arguments.\n");
193         result.SetStatus (eReturnStatusFailed);
194         return false;
195     }
196     ExecutionContext exe_ctx(interpreter.GetDebugger().GetExecutionContext());
197 
198     if (m_options.show_mixed && m_options.num_lines_context == 0)
199         m_options.num_lines_context = 3;
200 
201     if (!m_options.m_func_name.empty())
202     {
203         ConstString name(m_options.m_func_name.c_str());
204 
205         if (Disassembler::Disassemble (interpreter.GetDebugger(),
206                                        arch,
207                                        exe_ctx,
208                                        name,
209                                        NULL,    // Module *
210                                        m_options.show_mixed ? m_options.num_lines_context : 0,
211                                        m_options.show_bytes,
212                                        result.GetOutputStream()))
213         {
214             result.SetStatus (eReturnStatusSuccessFinishResult);
215         }
216         else
217         {
218             result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
219             result.SetStatus (eReturnStatusFailed);
220         }
221     }
222     else
223     {
224         AddressRange range;
225         if (m_options.m_start_addr != LLDB_INVALID_ADDRESS)
226         {
227             range.GetBaseAddress().SetOffset (m_options.m_start_addr);
228             if (m_options.m_end_addr != LLDB_INVALID_ADDRESS)
229             {
230                 if (m_options.m_end_addr < m_options.m_start_addr)
231                 {
232                     result.AppendErrorWithFormat ("End address before start address.\n");
233                     result.SetStatus (eReturnStatusFailed);
234                     return false;
235                 }
236                 range.SetByteSize (m_options.m_end_addr - m_options.m_start_addr);
237             }
238             else
239                 range.SetByteSize (DEFAULT_DISASM_BYTE_SIZE);
240         }
241         else
242         {
243             if (exe_ctx.frame)
244             {
245                 SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
246                 if (sc.function)
247                     range = sc.function->GetAddressRange();
248                 else if (sc.symbol && sc.symbol->GetAddressRangePtr())
249                     range = *sc.symbol->GetAddressRangePtr();
250                 else
251                     range.GetBaseAddress() = exe_ctx.frame->GetPC();
252             }
253             else
254             {
255                 result.AppendError ("invalid frame");
256                 result.SetStatus (eReturnStatusFailed);
257                 return false;
258             }
259         }
260         if (range.GetByteSize() == 0)
261             range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE);
262 
263         if (Disassembler::Disassemble (interpreter.GetDebugger(),
264                                        arch,
265                                        exe_ctx,
266                                        range,
267                                        m_options.show_mixed ? m_options.num_lines_context : 0,
268                                        m_options.show_bytes,
269                                        result.GetOutputStream()))
270         {
271             result.SetStatus (eReturnStatusSuccessFinishResult);
272         }
273         else
274         {
275             result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8llx.\n", m_options.m_start_addr);
276             result.SetStatus (eReturnStatusFailed);
277         }
278     }
279 
280     return result.Succeeded();
281 }
282