Remove MI version 1
[binutils-gdb.git] / gdb / mi / mi-cmd-disas.c
1 /* MI Command Set - disassemble commands.
2 Copyright (C) 2000-2022 Free Software Foundation, Inc.
3 Contributed by Cygnus Solutions (a Red Hat company).
4
5 This file is part of GDB.
6
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.
11
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.
16
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/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "target.h"
23 #include "value.h"
24 #include "mi-cmds.h"
25 #include "mi-getopt.h"
26 #include "ui-out.h"
27 #include "disasm.h"
28
29 /* The arguments to be passed on the command line and parsed here are
30 either:
31
32 START-ADDRESS: address to start the disassembly at.
33 END-ADDRESS: address to end the disassembly at.
34
35 or:
36
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
42 lines.
43
44 always required:
45
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. */
52
53 void
54 mi_cmd_disassemble (const char *command, char **argv, int argc)
55 {
56 struct gdbarch *gdbarch = get_current_arch ();
57 struct ui_out *uiout = current_uiout;
58 CORE_ADDR start;
59
60 int mode;
61 gdb_disassembly_flags disasm_flags;
62 struct symtab *s;
63
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;
73
74 /* ... and their corresponding value. */
75 char *file_string = NULL;
76 int line_num = -1;
77 int how_many = -1;
78 CORE_ADDR low = 0;
79 CORE_ADDR high = 0;
80 CORE_ADDR addr = 0;
81
82 /* Flags to handle the --opcodes option. */
83 enum opcodes_mode
84 {
85 OPCODES_DEFAULT, OPCODES_NONE, OPCODES_DISPLAY, OPCODES_BYTES
86 };
87 enum opcodes_mode opcodes_mode = OPCODES_DEFAULT;
88
89 /* Handle the -source option. */
90 bool show_source = false;
91
92 /* Options processing stuff. */
93 int oind = 0;
94 char *oarg;
95 enum opt
96 {
97 FILE_OPT, LINE_OPT, NUM_OPT, START_OPT, END_OPT, ADDR_OPT, OPCODES_OPT,
98 SHOW_SRC_OPT
99 };
100 static const struct mi_opt opts[] =
101 {
102 {"f", FILE_OPT, 1},
103 {"l", LINE_OPT, 1},
104 {"n", NUM_OPT, 1},
105 {"s", START_OPT, 1},
106 {"e", END_OPT, 1},
107 {"a", ADDR_OPT, 1},
108 {"-opcodes", OPCODES_OPT, 1},
109 {"-source", SHOW_SRC_OPT, 0},
110 { 0, 0, 0 }
111 };
112
113 /* Get the options with their arguments. Keep track of what we
114 encountered. */
115 while (1)
116 {
117 int opt = mi_getopt ("-data-disassemble", argc, argv, opts,
118 &oind, &oarg);
119 if (opt < 0)
120 break;
121 switch ((enum opt) opt)
122 {
123 case FILE_OPT:
124 file_string = oarg;
125 file_seen = true;
126 break;
127 case LINE_OPT:
128 line_num = atoi (oarg);
129 line_seen = true;
130 break;
131 case NUM_OPT:
132 how_many = atoi (oarg);
133 num_seen = true;
134 break;
135 case START_OPT:
136 low = parse_and_eval_address (oarg);
137 start_seen = true;
138 break;
139 case END_OPT:
140 high = parse_and_eval_address (oarg);
141 end_seen = true;
142 break;
143 case ADDR_OPT:
144 addr = parse_and_eval_address (oarg);
145 addr_seen = true;
146 break;
147 case OPCODES_OPT:
148 opcodes_seen = true;
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;
155 else
156 error (_("-data-disassemble: unknown value for -opcodes argument"));
157 break;
158 case SHOW_SRC_OPT:
159 source_seen = true;
160 show_source = true;
161 break;
162 }
163 }
164 argv += oind;
165 argc -= oind;
166
167 /* Allow only filename + linenum (with how_many which is not
168 required) OR start_addr + end_addr OR addr. */
169
170 if (!(
171 ( line_seen && file_seen && !start_seen && !end_seen
172 && !addr_seen)
173
174 || (!line_seen && !file_seen && !num_seen && start_seen && end_seen
175 && !addr_seen)
176
177 || (!line_seen && !file_seen && !num_seen && !start_seen && !end_seen
178 && addr_seen))
179 || argc > 1)
180 error (_("-data-disassemble: Usage: "
181 "( -f filename -l linenum [-n howmany] |"
182 " -s startaddr -e endaddr | -a addr ) "
183 "[ --opcodes mode ] [ --source ] [ [--] mode ]."));
184
185 if (argc == 1)
186 {
187 mode = atoi (argv[0]);
188 if (mode < 0 || mode > 5)
189 error (_("-data-disassemble: Mode argument must be in the range 0-5."));
190 }
191 else
192 mode = 0;
193
194 if (mode != 0 && (source_seen || opcodes_seen))
195 error (_("-data-disassemble: --opcodes and --source can only be used with mode 0"));
196
197 /* Convert the mode into a set of disassembly flags. */
198
199 disasm_flags = 0; /* Initialize here for -Wall. */
200 switch (mode)
201 {
202 case 0:
203 break;
204 case 1:
205 disasm_flags |= DISASSEMBLY_SOURCE_DEPRECATED;
206 break;
207 case 2:
208 disasm_flags |= DISASSEMBLY_RAW_BYTES;
209 break;
210 case 3:
211 disasm_flags |= DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_RAW_BYTES;
212 break;
213 case 4:
214 disasm_flags |= DISASSEMBLY_SOURCE;
215 break;
216 case 5:
217 disasm_flags |= DISASSEMBLY_SOURCE | DISASSEMBLY_RAW_BYTES;
218 break;
219 default:
220 gdb_assert_not_reached ("bad disassembly mode");
221 }
222
223 /* Now handle the (optional) --opcodes argument. This partially
224 overrides the mode value. */
225 if (opcodes_mode != OPCODES_DEFAULT)
226 {
227 /* Remove any existing flags related to opcodes display. */
228 disasm_flags &= ~(DISASSEMBLY_RAW_BYTES | DISASSEMBLY_RAW_INSN);
229
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;
235 }
236
237 /* Handle the optional --source argument. */
238 if (show_source)
239 {
240 disasm_flags &= ~DISASSEMBLY_SOURCE_DEPRECATED;
241 disasm_flags |= DISASSEMBLY_SOURCE;
242 }
243
244 /* We must get the function beginning and end where line_num is
245 contained. */
246
247 if (line_seen && file_seen)
248 {
249 s = lookup_symtab (file_string);
250 if (s == NULL)
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"));
257 }
258 else if (addr_seen)
259 {
260 if (find_pc_partial_function (addr, NULL, &low, &high) == 0)
261 error (_("-data-disassemble: "
262 "No function contains specified address"));
263 }
264
265 gdb_disassembly (gdbarch, uiout,
266 disasm_flags,
267 how_many, low, high);
268 }