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