CommandObjectDisassemble.cpp revision 84cdc15005983e5244d665fa779e33c2b6fac95f
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
31using namespace lldb;
32using namespace lldb_private;
33
34CommandObjectDisassemble::CommandOptions::CommandOptions () :
35    Options(),
36    m_func_name(),
37    m_start_addr(),
38    m_end_addr ()
39{
40    ResetOptionValues();
41}
42
43CommandObjectDisassemble::CommandOptions::~CommandOptions ()
44{
45}
46
47Error
48CommandObjectDisassemble::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
101void
102CommandObjectDisassemble::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}
112
113const lldb::OptionDefinition*
114CommandObjectDisassemble::CommandOptions::GetDefinitions ()
115{
116    return g_option_table;
117}
118
119lldb::OptionDefinition
120CommandObjectDisassemble::CommandOptions::g_option_table[] =
121{
122{ LLDB_OPT_SET_ALL, false, "bytes",    'b', no_argument,       NULL, 0, NULL,             "Show opcode bytes when disassembling."},
123{ LLDB_OPT_SET_ALL, false, "context",  'c', required_argument, NULL, 0, "<num-lines>",    "Number of context lines of source to show."},
124{ LLDB_OPT_SET_ALL, false, "mixed",    'm', no_argument,       NULL, 0, NULL,             "Enable mixed source and assembly display."},
125{ LLDB_OPT_SET_ALL, false, "raw",      'r', no_argument,       NULL, 0, NULL,             "Print raw disassembly with no symbol information."},
126
127{ LLDB_OPT_SET_1, true, "start-address",  's', required_argument, NULL, 0, "<start-address>",      "Address to start disassembling."},
128{ LLDB_OPT_SET_1, false, "end-address",  'e', required_argument, NULL, 0, "<end-address>",      "Address to start disassembling."},
129
130{ LLDB_OPT_SET_2, true, "name",     'n', required_argument, NULL, CommandCompletions::eSymbolCompletion, "<function-name>",             "Disassemble entire contents of the given function name."},
131
132{ LLDB_OPT_SET_3, false, "current-frame",     'f', no_argument, NULL, 0, "<current-frame>",             "Disassemble entire contents of the current frame's function."},
133
134{ 0, false, NULL, 0, 0, NULL, 0, NULL, NULL }
135};
136
137
138
139//-------------------------------------------------------------------------
140// CommandObjectDisassemble
141//-------------------------------------------------------------------------
142
143CommandObjectDisassemble::CommandObjectDisassemble () :
144    CommandObject ("disassemble",
145                     "Disassemble bytes in the current function or anywhere in the inferior program.",
146                     "disassemble [<cmd-options>]")
147{
148}
149
150CommandObjectDisassemble::~CommandObjectDisassemble()
151{
152}
153
154void
155CommandObjectDisassemble::Disassemble
156(
157    CommandContext *context,
158    CommandInterpreter *interpreter,
159    CommandReturnObject &result,
160    Disassembler *disassembler,
161    const SymbolContextList &sc_list
162)
163{
164    const size_t count = sc_list.GetSize();
165    SymbolContext sc;
166    AddressRange range;
167    for (size_t i=0; i<count; ++i)
168    {
169        if (sc_list.GetContextAtIndex(i, sc) == false)
170            break;
171        if (sc.GetAddressRange(eSymbolContextFunction | eSymbolContextSymbol, range))
172        {
173            lldb::addr_t addr = range.GetBaseAddress().GetLoadAddress(context->GetExecutionContext().process);
174            if (addr != LLDB_INVALID_ADDRESS)
175            {
176                lldb::addr_t end_addr = addr + range.GetByteSize();
177                Disassemble (context, interpreter, result, disassembler, addr, end_addr);
178            }
179        }
180    }
181}
182
183void
184CommandObjectDisassemble::Disassemble
185(
186    CommandContext *context,
187    CommandInterpreter *interpreter,
188    CommandReturnObject &result,
189    Disassembler *disassembler,
190    lldb::addr_t addr,
191    lldb::addr_t end_addr
192)
193{
194    if (addr == LLDB_INVALID_ADDRESS)
195        return;
196
197    if (end_addr == LLDB_INVALID_ADDRESS || addr >= end_addr)
198        end_addr = addr + DEFAULT_DISASM_BYTE_SIZE;
199
200    ExecutionContext exe_ctx (context->GetExecutionContext());
201    DataExtractor data;
202    size_t bytes_disassembled = disassembler->ParseInstructions (&exe_ctx, eAddressTypeLoad, addr, end_addr - addr, data);
203    if (bytes_disassembled == 0)
204    {
205        // Nothing got disassembled...
206    }
207    else
208    {
209        // We got some things disassembled...
210        size_t num_instructions = disassembler->GetInstructionList().GetSize();
211        uint32_t offset = 0;
212        Stream &output_stream = result.GetOutputStream();
213        SymbolContext sc;
214        SymbolContext prev_sc;
215        AddressRange sc_range;
216        if (m_options.show_mixed)
217            output_stream.IndentMore ();
218
219        for (size_t i=0; i<num_instructions; ++i)
220        {
221            Disassembler::Instruction *inst = disassembler->GetInstructionList().GetInstructionAtIndex (i);
222            if (inst)
223            {
224                lldb::addr_t curr_addr = addr + offset;
225                if (m_options.show_mixed)
226                {
227                    Process *process = context->GetExecutionContext().process;
228                    if (!sc_range.ContainsLoadAddress (curr_addr, process))
229                    {
230                        prev_sc = sc;
231                        Address curr_so_addr;
232                        if (process && process->ResolveLoadAddress (curr_addr, curr_so_addr))
233                        {
234                            if (curr_so_addr.GetSection())
235                            {
236                                Module *module = curr_so_addr.GetSection()->GetModule();
237                                uint32_t resolved_mask = module->ResolveSymbolContextForAddress(curr_so_addr, eSymbolContextEverything, sc);
238                                if (resolved_mask)
239                                {
240                                    sc.GetAddressRange (eSymbolContextEverything, sc_range);
241                                    if (sc != prev_sc)
242                                    {
243                                        if (offset != 0)
244                                            output_stream.EOL();
245
246                                        sc.DumpStopContext(&output_stream, process, curr_so_addr);
247                                        output_stream.EOL();
248                                        if (sc.comp_unit && sc.line_entry.IsValid())
249                                        {
250                                            interpreter->GetSourceManager().DisplaySourceLinesWithLineNumbers (
251                                                    sc.line_entry.file,
252                                                    sc.line_entry.line,
253                                                    m_options.num_lines_context,
254                                                    m_options.num_lines_context,
255                                                    m_options.num_lines_context ? "->" : "",
256                                                    &output_stream);
257                                        }
258                                    }
259                                }
260                            }
261                        }
262                    }
263                }
264                if (m_options.show_mixed)
265                    output_stream.IndentMore ();
266                output_stream.Indent();
267                size_t inst_byte_size = inst->GetByteSize();
268                inst->Dump(&output_stream, curr_addr, m_options.show_bytes ? &data : NULL, offset, exe_ctx, m_options.raw);
269                output_stream.EOL();
270                offset += inst_byte_size;
271                if (m_options.show_mixed)
272                    output_stream.IndentLess ();
273            }
274            else
275            {
276                break;
277            }
278        }
279        if (m_options.show_mixed)
280            output_stream.IndentLess ();
281
282    }
283}
284
285bool
286CommandObjectDisassemble::Execute
287(
288    Args& command,
289    CommandContext *context,
290    CommandInterpreter *interpreter,
291    CommandReturnObject &result
292)
293{
294    Target *target = context->GetTarget();
295    if (target == NULL)
296    {
297        result.AppendError ("invalid target, set executable file using 'file' command");
298        result.SetStatus (eReturnStatusFailed);
299        return false;
300    }
301
302    ArchSpec arch(target->GetArchitecture());
303    if (!arch.IsValid())
304    {
305        result.AppendError ("target needs valid architecure in order to be able to disassemble");
306        result.SetStatus (eReturnStatusFailed);
307        return false;
308    }
309
310    Disassembler *disassembler = Disassembler::FindPlugin(arch);
311
312    if (disassembler == NULL)
313    {
314        result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for %s architecture.\n", arch.AsCString());
315        result.SetStatus (eReturnStatusFailed);
316        return false;
317    }
318
319    result.SetStatus (eReturnStatusSuccessFinishResult);
320
321    lldb::addr_t addr = LLDB_INVALID_ADDRESS;
322    lldb::addr_t end_addr = LLDB_INVALID_ADDRESS;
323    ConstString name;
324    const size_t argc = command.GetArgumentCount();
325    if (argc != 0)
326    {
327        result.AppendErrorWithFormat ("\"disassemble\" doesn't take any arguments.\n");
328        result.SetStatus (eReturnStatusFailed);
329        return false;
330    }
331
332    if (m_options.m_start_addr != LLDB_INVALID_ADDRESS)
333    {
334        addr = m_options.m_start_addr;
335        if (m_options.m_end_addr != LLDB_INVALID_ADDRESS)
336        {
337            end_addr = m_options.m_end_addr;
338            if (end_addr < addr)
339            {
340                result.AppendErrorWithFormat ("End address before start address.\n");
341                result.SetStatus (eReturnStatusFailed);
342                return false;
343            }
344        }
345        else
346            end_addr = addr + DEFAULT_DISASM_BYTE_SIZE;
347    }
348    else if (!m_options.m_func_name.empty())
349    {
350        ConstString tmpname(m_options.m_func_name.c_str());
351        name = tmpname;
352    }
353    else
354    {
355        ExecutionContext exe_ctx(context->GetExecutionContext());
356        if (exe_ctx.frame)
357        {
358            SymbolContext sc(exe_ctx.frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol));
359            if (sc.function)
360            {
361                addr = sc.function->GetAddressRange().GetBaseAddress().GetLoadAddress(exe_ctx.process);
362                if (addr != LLDB_INVALID_ADDRESS)
363                    end_addr = addr + sc.function->GetAddressRange().GetByteSize();
364            }
365            else if (sc.symbol && sc.symbol->GetAddressRangePtr())
366            {
367                addr = sc.symbol->GetAddressRangePtr()->GetBaseAddress().GetLoadAddress(exe_ctx.process);
368                if (addr != LLDB_INVALID_ADDRESS)
369                {
370                    end_addr = addr + sc.symbol->GetAddressRangePtr()->GetByteSize();
371                    if (addr == end_addr)
372                        end_addr += DEFAULT_DISASM_BYTE_SIZE;
373                }
374            }
375            else
376            {
377                addr = exe_ctx.frame->GetPC().GetLoadAddress(exe_ctx.process);
378                if (addr != LLDB_INVALID_ADDRESS)
379                    end_addr = addr + DEFAULT_DISASM_BYTE_SIZE;
380            }
381        }
382        else
383        {
384            result.AppendError ("invalid frame");
385            result.SetStatus (eReturnStatusFailed);
386            return false;
387        }
388    }
389
390    if (!name.IsEmpty())
391    {
392        SymbolContextList sc_list;
393
394        if (target->GetImages().FindFunctions(name, sc_list))
395        {
396            Disassemble (context, interpreter, result, disassembler, sc_list);
397        }
398        else if (target->GetImages().FindSymbolsWithNameAndType(name, eSymbolTypeCode, sc_list))
399        {
400            Disassemble (context, interpreter, result, disassembler, sc_list);
401        }
402        else
403        {
404            result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString());
405            result.SetStatus (eReturnStatusFailed);
406            return false;
407        }
408    }
409    else
410    {
411        Disassemble (context, interpreter, result, disassembler, addr, end_addr);
412    }
413
414    return result.Succeeded();
415}
416