Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 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 | |
Daniel Malea | d891f9b | 2012-12-05 00:20:57 +0000 | [diff] [blame] | 10 | #include "lldb/lldb-python.h" |
| 11 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 12 | #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 Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 19 | #include "lldb/Core/Disassembler.h" |
| 20 | #include "lldb/Core/SourceManager.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 21 | #include "lldb/Interpreter/Args.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 22 | #include "lldb/Interpreter/CommandCompletions.h" |
| 23 | #include "lldb/Interpreter/CommandInterpreter.h" |
| 24 | #include "lldb/Interpreter/CommandReturnObject.h" |
Jim Ingham | 84cdc15 | 2010-06-15 19:49:27 +0000 | [diff] [blame] | 25 | #include "lldb/Interpreter/Options.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 26 | #include "lldb/Symbol/Function.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 27 | #include "lldb/Symbol/Symbol.h" |
| 28 | #include "lldb/Target/Process.h" |
Greg Clayton | 49ce896 | 2012-08-29 21:13:06 +0000 | [diff] [blame] | 29 | #include "lldb/Target/StackFrame.h" |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 30 | #include "lldb/Target/Target.h" |
| 31 | |
| 32 | #define DEFAULT_DISASM_BYTE_SIZE 32 |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 33 | #define DEFAULT_DISASM_NUM_INS 4 |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 34 | |
| 35 | using namespace lldb; |
| 36 | using namespace lldb_private; |
| 37 | |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 38 | CommandObjectDisassemble::CommandOptions::CommandOptions (CommandInterpreter &interpreter) : |
Johnny Chen | 0e0636d | 2011-04-08 22:15:29 +0000 | [diff] [blame] | 39 | Options(interpreter), |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 40 | num_lines_context(0), |
| 41 | num_instructions (0), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 42 | func_name(), |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 43 | cur_function (false), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 44 | start_addr(), |
| 45 | end_addr (), |
| 46 | at_pc (false), |
| 47 | frame_line (false), |
| 48 | plugin_name (), |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 49 | arch(), |
| 50 | some_location_specified (false) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 51 | { |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 52 | OptionParsingStarting(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | CommandObjectDisassemble::CommandOptions::~CommandOptions () |
| 56 | { |
| 57 | } |
| 58 | |
| 59 | Error |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 60 | CommandObjectDisassemble::CommandOptions::SetOptionValue (uint32_t option_idx, const char *option_arg) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 61 | { |
| 62 | Error error; |
| 63 | |
Greg Clayton | 6475c42 | 2012-12-04 00:32:51 +0000 | [diff] [blame] | 64 | const int short_option = m_getopt_table[option_idx].val; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 65 | |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 66 | bool success; |
| 67 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 68 | switch (short_option) |
| 69 | { |
| 70 | case 'm': |
| 71 | show_mixed = true; |
| 72 | break; |
| 73 | |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 74 | case 'C': |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 75 | num_lines_context = Args::StringToUInt32(option_arg, 0, 0, &success); |
| 76 | if (!success) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 77 | error.SetErrorStringWithFormat ("invalid num context lines string: \"%s\"", option_arg); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 78 | break; |
| 79 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 80 | case 'c': |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 81 | num_instructions = Args::StringToUInt32(option_arg, 0, 0, &success); |
| 82 | if (!success) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 83 | error.SetErrorStringWithFormat ("invalid num of instructions string: \"%s\"", option_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 84 | break; |
| 85 | |
| 86 | case 'b': |
| 87 | show_bytes = true; |
| 88 | break; |
| 89 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 90 | case 's': |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 91 | start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0); |
| 92 | if (start_addr == LLDB_INVALID_ADDRESS) |
| 93 | start_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 94 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 95 | if (start_addr == LLDB_INVALID_ADDRESS) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 96 | error.SetErrorStringWithFormat ("invalid start address string '%s'", option_arg); |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 97 | some_location_specified = true; |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 98 | break; |
| 99 | case 'e': |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 100 | end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 0); |
| 101 | if (end_addr == LLDB_INVALID_ADDRESS) |
| 102 | end_addr = Args::StringToUInt64(option_arg, LLDB_INVALID_ADDRESS, 16); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 103 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 104 | if (end_addr == LLDB_INVALID_ADDRESS) |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 105 | error.SetErrorStringWithFormat ("invalid end address string '%s'", option_arg); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 106 | break; |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 107 | some_location_specified = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 108 | case 'n': |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 109 | func_name.assign (option_arg); |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 110 | some_location_specified = true; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 111 | break; |
| 112 | |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 113 | case 'p': |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 114 | at_pc = true; |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 115 | some_location_specified = true; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 116 | break; |
| 117 | |
| 118 | case 'l': |
| 119 | frame_line = true; |
| 120 | // Disassemble the current source line kind of implies showing mixed |
| 121 | // source code context. |
| 122 | show_mixed = true; |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 123 | some_location_specified = true; |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 124 | break; |
| 125 | |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 126 | case 'P': |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 127 | plugin_name.assign (option_arg); |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 128 | break; |
| 129 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 130 | case 'r': |
| 131 | raw = true; |
| 132 | break; |
| 133 | |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 134 | case 'f': |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 135 | cur_function = true; |
| 136 | some_location_specified = true; |
Johnny Chen | 61c1b8b | 2010-10-29 19:33:40 +0000 | [diff] [blame] | 137 | break; |
| 138 | |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 139 | case 'a': |
Greg Clayton | b170aee | 2012-05-08 01:45:38 +0000 | [diff] [blame] | 140 | if (!arch.SetTriple (option_arg, m_interpreter.GetPlatform (true).get())) |
| 141 | arch.SetTriple (option_arg); |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 142 | break; |
| 143 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 144 | default: |
Greg Clayton | 9c23673 | 2011-10-26 00:56:27 +0000 | [diff] [blame] | 145 | error.SetErrorStringWithFormat("unrecognized short option '%c'", short_option); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 146 | break; |
| 147 | } |
| 148 | |
| 149 | return error; |
| 150 | } |
| 151 | |
| 152 | void |
Greg Clayton | 143fcc3 | 2011-04-13 00:18:08 +0000 | [diff] [blame] | 153 | CommandObjectDisassemble::CommandOptions::OptionParsingStarting () |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 154 | { |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 155 | show_mixed = false; |
| 156 | show_bytes = false; |
| 157 | num_lines_context = 0; |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 158 | num_instructions = 0; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 159 | func_name.clear(); |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 160 | cur_function = false; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 161 | at_pc = false; |
| 162 | frame_line = false; |
| 163 | start_addr = LLDB_INVALID_ADDRESS; |
| 164 | end_addr = LLDB_INVALID_ADDRESS; |
Sean Callanan | 944be70 | 2010-06-17 00:32:05 +0000 | [diff] [blame] | 165 | raw = false; |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 166 | plugin_name.clear(); |
| 167 | arch.Clear(); |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 168 | some_location_specified = false; |
| 169 | } |
| 170 | |
| 171 | Error |
| 172 | CommandObjectDisassemble::CommandOptions::OptionParsingFinished () |
| 173 | { |
| 174 | if (!some_location_specified) |
| 175 | at_pc = true; |
| 176 | return Error(); |
| 177 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 180 | const OptionDefinition* |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 181 | CommandObjectDisassemble::CommandOptions::GetDefinitions () |
| 182 | { |
| 183 | return g_option_table; |
| 184 | } |
| 185 | |
Greg Clayton | b344843 | 2011-03-24 21:19:54 +0000 | [diff] [blame] | 186 | OptionDefinition |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 187 | CommandObjectDisassemble::CommandOptions::g_option_table[] = |
| 188 | { |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 189 | { LLDB_OPT_SET_ALL , false , "bytes", 'b', no_argument , NULL, 0, eArgTypeNone, "Show opcode bytes when disassembling."}, |
| 190 | { LLDB_OPT_SET_ALL , false , "context", 'C', required_argument , NULL, 0, eArgTypeNumLines, "Number of context lines of source to show."}, |
| 191 | { LLDB_OPT_SET_ALL , false , "mixed", 'm', no_argument , NULL, 0, eArgTypeNone, "Enable mixed source and assembly display."}, |
| 192 | { LLDB_OPT_SET_ALL , false , "raw", 'r', no_argument , NULL, 0, eArgTypeNone, "Print raw disassembly with no symbol information."}, |
| 193 | { LLDB_OPT_SET_ALL , false , "plugin", 'P', required_argument , NULL, 0, eArgTypePlugin, "Name of the disassembler plugin you want to use."}, |
| 194 | { LLDB_OPT_SET_ALL , false , "arch", 'a', required_argument , NULL, 0, eArgTypeArchitecture,"Specify the architecture to use from cross disassembly."}, |
| 195 | { LLDB_OPT_SET_1 | |
| 196 | LLDB_OPT_SET_2 , true , "start-address" , 's', required_argument , NULL, 0, eArgTypeStartAddress,"Address at which to start disassembling."}, |
| 197 | { LLDB_OPT_SET_1 , false , "end-address" , 'e', required_argument , NULL, 0, eArgTypeEndAddress, "Address at which to end disassembling."}, |
| 198 | { LLDB_OPT_SET_2 | |
| 199 | LLDB_OPT_SET_3 | |
| 200 | LLDB_OPT_SET_4 | |
| 201 | LLDB_OPT_SET_5 , false , "count", 'c', required_argument , NULL, 0, eArgTypeNumLines, "Number of instructions to display."}, |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 202 | { LLDB_OPT_SET_3 , false , "name", 'n', required_argument , NULL, CommandCompletions::eSymbolCompletion, eArgTypeFunctionName, "Disassemble entire contents of the given function name."}, |
| 203 | { LLDB_OPT_SET_4 , false , "frame", 'f', no_argument , NULL, 0, eArgTypeNone, "Disassemble from the start of the current frame's function."}, |
| 204 | { LLDB_OPT_SET_5 , false , "pc", 'p', no_argument , NULL, 0, eArgTypeNone, "Disassemble around the current pc."}, |
| 205 | { 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 Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 206 | { 0 , false , NULL, 0, 0 , NULL, 0, eArgTypeNone, NULL } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | |
| 210 | |
| 211 | //------------------------------------------------------------------------- |
| 212 | // CommandObjectDisassemble |
| 213 | //------------------------------------------------------------------------- |
| 214 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 215 | CommandObjectDisassemble::CommandObjectDisassemble (CommandInterpreter &interpreter) : |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 216 | CommandObjectParsed (interpreter, |
| 217 | "disassemble", |
| 218 | "Disassemble bytes in the current function, or elsewhere in the executable program as specified by the user.", |
| 219 | "disassemble [<cmd-options>]"), |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 220 | m_options (interpreter) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 221 | { |
| 222 | } |
| 223 | |
| 224 | CommandObjectDisassemble::~CommandObjectDisassemble() |
| 225 | { |
| 226 | } |
| 227 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 228 | bool |
Jim Ingham | da26bd2 | 2012-06-08 21:56:10 +0000 | [diff] [blame] | 229 | CommandObjectDisassemble::DoExecute (Args& command, CommandReturnObject &result) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 230 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 231 | Target *target = m_interpreter.GetDebugger().GetSelectedTarget().get(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 232 | if (target == NULL) |
| 233 | { |
Greg Clayton | e1f50b9 | 2011-05-03 22:09:39 +0000 | [diff] [blame] | 234 | result.AppendError ("invalid target, create a debug target using the 'target create' command"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 235 | result.SetStatus (eReturnStatusFailed); |
| 236 | return false; |
| 237 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 238 | if (!m_options.arch.IsValid()) |
| 239 | m_options.arch = target->GetArchitecture(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 240 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 241 | if (!m_options.arch.IsValid()) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 242 | { |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 243 | result.AppendError ("use the --arch option or set the target architecure to disassemble"); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 244 | result.SetStatus (eReturnStatusFailed); |
| 245 | return false; |
| 246 | } |
| 247 | |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 248 | const char *plugin_name = m_options.GetPluginName (); |
Sean Callanan | 4f28c31 | 2012-08-01 18:50:59 +0000 | [diff] [blame] | 249 | DisassemblerSP disassembler = Disassembler::FindPlugin(m_options.arch, plugin_name); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 250 | |
Sean Callanan | b386d82 | 2012-08-09 00:50:26 +0000 | [diff] [blame] | 251 | if (!disassembler) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 252 | { |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 253 | if (plugin_name) |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 254 | result.AppendErrorWithFormat ("Unable to find Disassembler plug-in named '%s' that supports the '%s' architecture.\n", |
| 255 | plugin_name, |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 256 | m_options.arch.GetArchitectureName()); |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 257 | else |
Greg Clayton | 889fbd0 | 2011-03-26 19:14:58 +0000 | [diff] [blame] | 258 | result.AppendErrorWithFormat ("Unable to find Disassembler plug-in for the '%s' architecture.\n", |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 259 | m_options.arch.GetArchitectureName()); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 260 | result.SetStatus (eReturnStatusFailed); |
| 261 | return false; |
| 262 | } |
| 263 | |
| 264 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 265 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 266 | if (command.GetArgumentCount() != 0) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 267 | { |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 268 | result.AppendErrorWithFormat ("\"disassemble\" arguments are specified as options.\n"); |
Greg Clayton | f15996e | 2011-04-07 22:46:35 +0000 | [diff] [blame] | 269 | GetOptions()->GenerateOptionUsage (result.GetErrorStream(), this); |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 270 | result.SetStatus (eReturnStatusFailed); |
| 271 | return false; |
| 272 | } |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 273 | |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 274 | if (m_options.show_mixed && m_options.num_lines_context == 0) |
Greg Clayton | 1924e24 | 2010-09-15 05:51:24 +0000 | [diff] [blame] | 275 | m_options.num_lines_context = 1; |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 276 | |
Greg Clayton | b72d0f0 | 2011-04-12 05:54:46 +0000 | [diff] [blame] | 277 | ExecutionContext exe_ctx(m_interpreter.GetExecutionContext()); |
Greg Clayton | 6377f5c | 2011-06-28 19:01:40 +0000 | [diff] [blame] | 278 | // Always show the PC in the disassembly |
| 279 | uint32_t options = Disassembler::eOptionMarkPCAddress; |
Greg Clayton | 4bb0f19 | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 280 | |
Greg Clayton | 6377f5c | 2011-06-28 19:01:40 +0000 | [diff] [blame] | 281 | // Mark the source line for the current PC only if we are doing mixed source and assembly |
| 282 | if (m_options.show_mixed) |
| 283 | options |= Disassembler::eOptionMarkPCSourceLine; |
Greg Clayton | 4bb0f19 | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 284 | |
| 285 | if (m_options.show_bytes) |
| 286 | options |= Disassembler::eOptionShowBytes; |
| 287 | |
| 288 | if (m_options.raw) |
| 289 | options |= Disassembler::eOptionRawOuput; |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 290 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 291 | if (!m_options.func_name.empty()) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 292 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 293 | ConstString name(m_options.func_name.c_str()); |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 294 | |
Greg Clayton | 238c0a1 | 2010-09-18 01:14:36 +0000 | [diff] [blame] | 295 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 296 | m_options.arch, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 297 | plugin_name, |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 298 | exe_ctx, |
| 299 | name, |
| 300 | NULL, // Module * |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 301 | m_options.num_instructions, |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 302 | m_options.show_mixed ? m_options.num_lines_context : 0, |
Greg Clayton | 4bb0f19 | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 303 | options, |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 304 | result.GetOutputStream())) |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 305 | { |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 306 | result.SetStatus (eReturnStatusSuccessFinishResult); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 307 | } |
| 308 | else |
| 309 | { |
| 310 | result.AppendErrorWithFormat ("Unable to find symbol with name '%s'.\n", name.GetCString()); |
| 311 | result.SetStatus (eReturnStatusFailed); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 312 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 313 | } |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 314 | else |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 315 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 316 | AddressRange range; |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 317 | StackFrame *frame = exe_ctx.GetFramePtr(); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 318 | if (m_options.frame_line) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 319 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 320 | if (frame == NULL) |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 321 | { |
| 322 | result.AppendError ("Cannot disassemble around the current line without a selected frame.\n"); |
| 323 | result.SetStatus (eReturnStatusFailed); |
| 324 | return false; |
| 325 | } |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 326 | LineEntry pc_line_entry (frame->GetSymbolContext(eSymbolContextLineEntry).line_entry); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 327 | if (pc_line_entry.IsValid()) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 328 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 329 | range = pc_line_entry.range; |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 330 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 331 | else |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 332 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 333 | m_options.at_pc = true; // No line entry, so just disassemble around the current pc |
| 334 | m_options.show_mixed = false; |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 335 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 336 | } |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 337 | else if (m_options.cur_function) |
| 338 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 339 | if (frame == NULL) |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 340 | { |
| 341 | result.AppendError ("Cannot disassemble around the current function without a selected frame.\n"); |
| 342 | result.SetStatus (eReturnStatusFailed); |
| 343 | return false; |
| 344 | } |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 345 | Symbol *symbol = frame->GetSymbolContext(eSymbolContextSymbol).symbol; |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 346 | if (symbol) |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 347 | { |
| 348 | range.GetBaseAddress() = symbol->GetAddress(); |
| 349 | range.SetByteSize(symbol->GetByteSize()); |
| 350 | } |
Jim Ingham | d2ad7fd | 2011-09-01 01:11:04 +0000 | [diff] [blame] | 351 | } |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 352 | |
| 353 | // Did the "m_options.frame_line" find a valid range already? If so |
| 354 | // skip the rest... |
| 355 | if (range.GetByteSize() == 0) |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 356 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 357 | if (m_options.at_pc) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 358 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 359 | if (frame == NULL) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 360 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 361 | result.AppendError ("Cannot disassemble around the current PC without a selected frame.\n"); |
| 362 | result.SetStatus (eReturnStatusFailed); |
| 363 | return false; |
| 364 | } |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 365 | range.GetBaseAddress() = frame->GetFrameCodeAddress(); |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 366 | if (m_options.num_instructions == 0) |
| 367 | { |
| 368 | // Disassembling at the PC always disassembles some number of instructions (not the whole function). |
| 369 | m_options.num_instructions = DEFAULT_DISASM_NUM_INS; |
| 370 | } |
| 371 | } |
| 372 | else |
| 373 | { |
| 374 | range.GetBaseAddress().SetOffset (m_options.start_addr); |
| 375 | if (range.GetBaseAddress().IsValid()) |
| 376 | { |
| 377 | if (m_options.end_addr != LLDB_INVALID_ADDRESS) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 378 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 379 | if (m_options.end_addr <= m_options.start_addr) |
| 380 | { |
| 381 | result.AppendErrorWithFormat ("End address before start address.\n"); |
| 382 | result.SetStatus (eReturnStatusFailed); |
| 383 | return false; |
| 384 | } |
| 385 | range.SetByteSize (m_options.end_addr - m_options.start_addr); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 386 | } |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 387 | } |
| 388 | } |
| 389 | } |
| 390 | |
| 391 | if (m_options.num_instructions != 0) |
| 392 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 393 | if (!range.GetBaseAddress().IsValid()) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 394 | { |
| 395 | // The default action is to disassemble the current frame function. |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 396 | if (frame) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 397 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 398 | SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 399 | if (sc.function) |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 400 | range.GetBaseAddress() = sc.function->GetAddressRange().GetBaseAddress(); |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 401 | else if (sc.symbol && sc.symbol->ValueIsAddress()) |
| 402 | range.GetBaseAddress() = sc.symbol->GetAddress(); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 403 | else |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 404 | range.GetBaseAddress() = frame->GetFrameCodeAddress(); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 405 | } |
| 406 | |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 407 | if (!range.GetBaseAddress().IsValid()) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 408 | { |
| 409 | result.AppendError ("invalid frame"); |
| 410 | result.SetStatus (eReturnStatusFailed); |
| 411 | return false; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 416 | m_options.arch, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 417 | plugin_name, |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 418 | exe_ctx, |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 419 | range.GetBaseAddress(), |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 420 | m_options.num_instructions, |
| 421 | m_options.show_mixed ? m_options.num_lines_context : 0, |
Greg Clayton | 4bb0f19 | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 422 | options, |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 423 | result.GetOutputStream())) |
| 424 | { |
| 425 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 426 | } |
| 427 | else |
| 428 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 429 | result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 430 | result.SetStatus (eReturnStatusFailed); |
| 431 | } |
| 432 | } |
| 433 | else |
| 434 | { |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 435 | if (!range.GetBaseAddress().IsValid()) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 436 | { |
| 437 | // The default action is to disassemble the current frame function. |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 438 | if (frame) |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 439 | { |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 440 | SymbolContext sc(frame->GetSymbolContext(eSymbolContextFunction | eSymbolContextSymbol)); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 441 | if (sc.function) |
| 442 | range = sc.function->GetAddressRange(); |
Greg Clayton | 0c31d3d | 2012-03-07 21:03:09 +0000 | [diff] [blame] | 443 | else if (sc.symbol && sc.symbol->ValueIsAddress()) |
| 444 | { |
| 445 | range.GetBaseAddress() = sc.symbol->GetAddress(); |
| 446 | range.SetByteSize (sc.symbol->GetByteSize()); |
| 447 | } |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 448 | else |
Greg Clayton | 567e7f3 | 2011-09-22 04:58:26 +0000 | [diff] [blame] | 449 | range.GetBaseAddress() = frame->GetFrameCodeAddress(); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 450 | } |
| 451 | else |
| 452 | { |
| 453 | result.AppendError ("invalid frame"); |
| 454 | result.SetStatus (eReturnStatusFailed); |
| 455 | return false; |
| 456 | } |
| 457 | } |
| 458 | if (range.GetByteSize() == 0) |
| 459 | range.SetByteSize(DEFAULT_DISASM_BYTE_SIZE); |
| 460 | |
| 461 | if (Disassembler::Disassemble (m_interpreter.GetDebugger(), |
Greg Clayton | 24bc5d9 | 2011-03-30 18:16:51 +0000 | [diff] [blame] | 462 | m_options.arch, |
Greg Clayton | 149731c | 2011-03-25 18:03:16 +0000 | [diff] [blame] | 463 | plugin_name, |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 464 | exe_ctx, |
| 465 | range, |
| 466 | m_options.num_instructions, |
| 467 | m_options.show_mixed ? m_options.num_lines_context : 0, |
Greg Clayton | 4bb0f19 | 2011-06-22 01:39:49 +0000 | [diff] [blame] | 468 | options, |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 469 | result.GetOutputStream())) |
| 470 | { |
| 471 | result.SetStatus (eReturnStatusSuccessFinishResult); |
| 472 | } |
| 473 | else |
| 474 | { |
Daniel Malea | 5f35a4b | 2012-11-29 21:49:15 +0000 | [diff] [blame] | 475 | result.AppendErrorWithFormat ("Failed to disassemble memory at 0x%8.8" PRIx64 ".\n", m_options.start_addr); |
Jim Ingham | aa3e3e1 | 2011-03-22 01:48:42 +0000 | [diff] [blame] | 476 | result.SetStatus (eReturnStatusFailed); |
| 477 | } |
Greg Clayton | 7043635 | 2010-06-30 23:03:03 +0000 | [diff] [blame] | 478 | } |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Jim Ingham | 34e9a98 | 2010-06-15 18:47:14 +0000 | [diff] [blame] | 481 | return result.Succeeded(); |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 482 | } |