1 /* MI Command Set - disassemble commands.
2 Copyright (C) 2000-2024 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
5 This file is part of GDB.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
20 #include "arch-utils.h"
21 #include "progspace.h"
25 #include "mi-getopt.h"
29 /* The arguments to be passed on the command line and parsed here are
32 START-ADDRESS: address to start the disassembly at.
33 END-ADDRESS: address to end the disassembly at.
37 FILENAME: The name of the file where we want disassemble from.
38 LINE: The line around which we want to disassemble. It will
39 disassemble the function that contains that line.
40 HOW_MANY: Number of disassembly lines to display. With source, it
41 is the number of disassembly lines only, not counting the source
46 MODE: 0 -- disassembly.
47 1 -- disassembly and source (with deprecated source-centric view).
48 2 -- disassembly and opcodes.
49 3 -- disassembly, source-centric and opcodes.
50 4 -- disassembly, and source (with pc-centric view).
51 5 -- disassembly, source (pc-centric) and opcodes. */
54 mi_cmd_disassemble (const char *command
, const char *const *argv
, int argc
)
56 struct gdbarch
*gdbarch
= get_current_arch ();
57 struct ui_out
*uiout
= current_uiout
;
61 gdb_disassembly_flags disasm_flags
;
64 /* Which options have we processed ... */
65 bool file_seen
= false;
66 bool line_seen
= false;
67 bool num_seen
= false;
68 bool start_seen
= false;
69 bool end_seen
= false;
70 bool addr_seen
= false;
71 bool opcodes_seen
= false;
72 bool source_seen
= false;
74 /* ... and their corresponding value. */
75 const char *file_string
= NULL
;
82 /* Flags to handle the --opcodes option. */
85 OPCODES_DEFAULT
, OPCODES_NONE
, OPCODES_DISPLAY
, OPCODES_BYTES
87 enum opcodes_mode opcodes_mode
= OPCODES_DEFAULT
;
89 /* Handle the -source option. */
90 bool show_source
= false;
92 /* Options processing stuff. */
97 FILE_OPT
, LINE_OPT
, NUM_OPT
, START_OPT
, END_OPT
, ADDR_OPT
, OPCODES_OPT
,
100 static const struct mi_opt opts
[] =
108 {"-opcodes", OPCODES_OPT
, 1},
109 {"-source", SHOW_SRC_OPT
, 0},
113 /* Get the options with their arguments. Keep track of what we
117 int opt
= mi_getopt ("-data-disassemble", argc
, argv
, opts
,
121 switch ((enum opt
) opt
)
128 line_num
= atoi (oarg
);
132 how_many
= atoi (oarg
);
136 low
= parse_and_eval_address (oarg
);
140 high
= parse_and_eval_address (oarg
);
144 addr
= parse_and_eval_address (oarg
);
149 if (strcmp (oarg
, "none") == 0)
150 opcodes_mode
= OPCODES_NONE
;
151 else if (strcmp (oarg
, "display") == 0)
152 opcodes_mode
= OPCODES_DISPLAY
;
153 else if (strcmp (oarg
, "bytes") == 0)
154 opcodes_mode
= OPCODES_BYTES
;
156 error (_("-data-disassemble: unknown value for -opcodes argument"));
167 /* Allow only filename + linenum (with how_many which is not
168 required) OR start_addr + end_addr OR addr. */
171 ( line_seen
&& file_seen
&& !start_seen
&& !end_seen
174 || (!line_seen
&& !file_seen
&& !num_seen
&& start_seen
&& end_seen
177 || (!line_seen
&& !file_seen
&& !num_seen
&& !start_seen
&& !end_seen
180 error (_("-data-disassemble: Usage: "
181 "( -f filename -l linenum [-n howmany] |"
182 " -s startaddr -e endaddr | -a addr ) "
183 "[ --opcodes mode ] [ --source ] [ [--] mode ]."));
187 mode
= atoi (argv
[0]);
188 if (mode
< 0 || mode
> 5)
189 error (_("-data-disassemble: Mode argument must be in the range 0-5."));
194 if (mode
!= 0 && (source_seen
|| opcodes_seen
))
195 error (_("-data-disassemble: --opcodes and --source can only be used with mode 0"));
197 /* Convert the mode into a set of disassembly flags. */
199 disasm_flags
= 0; /* Initialize here for -Wall. */
205 disasm_flags
|= DISASSEMBLY_SOURCE_DEPRECATED
;
208 disasm_flags
|= DISASSEMBLY_RAW_BYTES
;
211 disasm_flags
|= DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_RAW_BYTES
;
214 disasm_flags
|= DISASSEMBLY_SOURCE
;
217 disasm_flags
|= DISASSEMBLY_SOURCE
| DISASSEMBLY_RAW_BYTES
;
220 gdb_assert_not_reached ("bad disassembly mode");
223 /* Now handle the (optional) --opcodes argument. This partially
224 overrides the mode value. */
225 if (opcodes_mode
!= OPCODES_DEFAULT
)
227 /* Remove any existing flags related to opcodes display. */
228 disasm_flags
&= ~(DISASSEMBLY_RAW_BYTES
| DISASSEMBLY_RAW_INSN
);
230 /* Add back any required flags. */
231 if (opcodes_mode
== OPCODES_DISPLAY
)
232 disasm_flags
|= DISASSEMBLY_RAW_INSN
;
233 else if (opcodes_mode
== OPCODES_BYTES
)
234 disasm_flags
|= DISASSEMBLY_RAW_BYTES
;
237 /* Handle the optional --source argument. */
240 disasm_flags
&= ~DISASSEMBLY_SOURCE_DEPRECATED
;
241 disasm_flags
|= DISASSEMBLY_SOURCE
;
244 /* We must get the function beginning and end where line_num is
247 if (line_seen
&& file_seen
)
249 s
= lookup_symtab (current_program_space
, file_string
);
251 error (_("-data-disassemble: Invalid filename."));
252 if (!find_line_pc (s
, line_num
, &start
))
253 error (_("-data-disassemble: Invalid line number"));
254 if (find_pc_partial_function (start
, NULL
, &low
, &high
) == 0)
255 error (_("-data-disassemble: "
256 "No function contains specified address"));
260 if (find_pc_partial_function (addr
, NULL
, &low
, &high
) == 0)
261 error (_("-data-disassemble: "
262 "No function contains specified address"));
265 gdb_disassembly (gdbarch
, uiout
,
267 how_many
, low
, high
);