Commit | Line | Data |
---|---|---|
92df71f0 | 1 | /* Disassemble support for GDB. |
1bac305b | 2 | |
b811d2c2 | 3 | Copyright (C) 2000-2020 Free Software Foundation, Inc. |
92df71f0 FN |
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 | |
a9762ec7 | 9 | the Free Software Foundation; either version 3 of the License, or |
92df71f0 FN |
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 | |
a9762ec7 | 18 | along with this program. If not, see <http://www.gnu.org/licenses/>. */ |
92df71f0 FN |
19 | |
20 | #include "defs.h" | |
65b48a81 | 21 | #include "arch-utils.h" |
4de283e4 TT |
22 | #include "target.h" |
23 | #include "value.h" | |
24 | #include "ui-out.h" | |
92df71f0 | 25 | #include "disasm.h" |
d55e5aa6 | 26 | #include "gdbcore.h" |
4de283e4 TT |
27 | #include "gdbcmd.h" |
28 | #include "dis-asm.h" | |
d55e5aa6 | 29 | #include "source.h" |
4de283e4 TT |
30 | #include "safe-ctype.h" |
31 | #include <algorithm> | |
268a13a5 | 32 | #include "gdbsupport/gdb_optional.h" |
c7110220 | 33 | #include "valprint.h" |
e43b10e1 | 34 | #include "cli/cli-style.h" |
92df71f0 FN |
35 | |
36 | /* Disassemble functions. | |
37 | FIXME: We should get rid of all the duplicate code in gdb that does | |
0963b4bd | 38 | the same thing: disassemble_command() and the gdbtk variation. */ |
92df71f0 | 39 | |
65b48a81 PB |
40 | /* This variable is used to hold the prospective disassembler_options value |
41 | which is set by the "set disassembler_options" command. */ | |
42 | static char *prospective_options = NULL; | |
43 | ||
6ff0ba5f DE |
44 | /* This structure is used to store line number information for the |
45 | deprecated /m option. | |
92df71f0 FN |
46 | We need a different sort of line table from the normal one cuz we can't |
47 | depend upon implicit line-end pc's for lines to do the | |
48 | reordering in this function. */ | |
49 | ||
6ff0ba5f | 50 | struct deprecated_dis_line_entry |
92df71f0 FN |
51 | { |
52 | int line; | |
53 | CORE_ADDR start_pc; | |
54 | CORE_ADDR end_pc; | |
55 | }; | |
56 | ||
6ff0ba5f DE |
57 | /* This Structure is used to store line number information. |
58 | We need a different sort of line table from the normal one cuz we can't | |
59 | depend upon implicit line-end pc's for lines to do the | |
60 | reordering in this function. */ | |
61 | ||
62 | struct dis_line_entry | |
63 | { | |
64 | struct symtab *symtab; | |
65 | int line; | |
66 | }; | |
67 | ||
68 | /* Hash function for dis_line_entry. */ | |
69 | ||
70 | static hashval_t | |
71 | hash_dis_line_entry (const void *item) | |
72 | { | |
9a3c8263 | 73 | const struct dis_line_entry *dle = (const struct dis_line_entry *) item; |
6ff0ba5f DE |
74 | |
75 | return htab_hash_pointer (dle->symtab) + dle->line; | |
76 | } | |
77 | ||
78 | /* Equal function for dis_line_entry. */ | |
79 | ||
80 | static int | |
81 | eq_dis_line_entry (const void *item_lhs, const void *item_rhs) | |
82 | { | |
9a3c8263 SM |
83 | const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs; |
84 | const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs; | |
6ff0ba5f DE |
85 | |
86 | return (lhs->symtab == rhs->symtab | |
87 | && lhs->line == rhs->line); | |
88 | } | |
89 | ||
90 | /* Create the table to manage lines for mixed source/disassembly. */ | |
91 | ||
92 | static htab_t | |
93 | allocate_dis_line_table (void) | |
94 | { | |
95 | return htab_create_alloc (41, | |
96 | hash_dis_line_entry, eq_dis_line_entry, | |
97 | xfree, xcalloc, xfree); | |
98 | } | |
99 | ||
4a099de2 | 100 | /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */ |
6ff0ba5f DE |
101 | |
102 | static void | |
4a099de2 | 103 | add_dis_line_entry (htab_t table, struct symtab *symtab, int line) |
6ff0ba5f DE |
104 | { |
105 | void **slot; | |
106 | struct dis_line_entry dle, *dlep; | |
107 | ||
108 | dle.symtab = symtab; | |
109 | dle.line = line; | |
110 | slot = htab_find_slot (table, &dle, INSERT); | |
111 | if (*slot == NULL) | |
112 | { | |
113 | dlep = XNEW (struct dis_line_entry); | |
114 | dlep->symtab = symtab; | |
115 | dlep->line = line; | |
116 | *slot = dlep; | |
117 | } | |
118 | } | |
119 | ||
120 | /* Return non-zero if SYMTAB, LINE are in TABLE. */ | |
121 | ||
122 | static int | |
123 | line_has_code_p (htab_t table, struct symtab *symtab, int line) | |
124 | { | |
125 | struct dis_line_entry dle; | |
126 | ||
127 | dle.symtab = symtab; | |
128 | dle.line = line; | |
129 | return htab_find (table, &dle) != NULL; | |
130 | } | |
131 | ||
e47ad6c0 YQ |
132 | /* Wrapper of target_read_code. */ |
133 | ||
134 | int | |
135 | gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, | |
136 | unsigned int len, | |
137 | struct disassemble_info *info) | |
810ecf9f | 138 | { |
283f7163 | 139 | return target_read_code (memaddr, myaddr, len); |
810ecf9f AC |
140 | } |
141 | ||
e47ad6c0 YQ |
142 | /* Wrapper of memory_error. */ |
143 | ||
144 | void | |
145 | gdb_disassembler::dis_asm_memory_error (int err, bfd_vma memaddr, | |
146 | struct disassemble_info *info) | |
810ecf9f | 147 | { |
d8b49cf0 YQ |
148 | gdb_disassembler *self |
149 | = static_cast<gdb_disassembler *>(info->application_data); | |
150 | ||
151 | self->m_err_memaddr = memaddr; | |
810ecf9f AC |
152 | } |
153 | ||
e47ad6c0 YQ |
154 | /* Wrapper of print_address. */ |
155 | ||
156 | void | |
157 | gdb_disassembler::dis_asm_print_address (bfd_vma addr, | |
158 | struct disassemble_info *info) | |
810ecf9f | 159 | { |
e47ad6c0 YQ |
160 | gdb_disassembler *self |
161 | = static_cast<gdb_disassembler *>(info->application_data); | |
9a619af0 | 162 | |
e47ad6c0 | 163 | print_address (self->arch (), addr, self->stream ()); |
810ecf9f AC |
164 | } |
165 | ||
39ef2f62 CB |
166 | static bool |
167 | line_is_less_than (const deprecated_dis_line_entry &mle1, | |
168 | const deprecated_dis_line_entry &mle2) | |
92df71f0 | 169 | { |
39ef2f62 | 170 | bool val; |
92df71f0 | 171 | |
9011945e AB |
172 | /* End of sequence markers have a line number of 0 but don't want to |
173 | be sorted to the head of the list, instead sort by PC. */ | |
39ef2f62 | 174 | if (mle1.line == 0 || mle2.line == 0) |
9011945e | 175 | { |
39ef2f62 CB |
176 | if (mle1.start_pc != mle2.start_pc) |
177 | val = mle1.start_pc < mle2.start_pc; | |
178 | else | |
179 | val = mle1.line < mle2.line; | |
9011945e AB |
180 | } |
181 | else | |
182 | { | |
39ef2f62 CB |
183 | if (mle1.line != mle2.line) |
184 | val = mle1.line < mle2.line; | |
185 | else | |
186 | val = mle1.start_pc < mle2.start_pc; | |
9011945e AB |
187 | } |
188 | return val; | |
92df71f0 FN |
189 | } |
190 | ||
a50a4026 | 191 | /* See disasm.h. */ |
af70908d | 192 | |
a50a4026 | 193 | int |
046bebe1 | 194 | gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn *insn, |
9a24775b | 195 | gdb_disassembly_flags flags) |
92df71f0 | 196 | { |
92df71f0 FN |
197 | /* parts of the symbolic representation of the address */ |
198 | int unmapped; | |
92df71f0 FN |
199 | int offset; |
200 | int line; | |
af70908d | 201 | int size; |
a50a4026 | 202 | CORE_ADDR pc; |
8b172ce7 | 203 | struct gdbarch *gdbarch = arch (); |
af70908d | 204 | |
393702cd | 205 | { |
046bebe1 | 206 | ui_out_emit_tuple tuple_emitter (m_uiout, NULL); |
393702cd TT |
207 | pc = insn->addr; |
208 | ||
209 | if (insn->number != 0) | |
210 | { | |
046bebe1 TT |
211 | m_uiout->field_unsigned ("insn-number", insn->number); |
212 | m_uiout->text ("\t"); | |
393702cd TT |
213 | } |
214 | ||
215 | if ((flags & DISASSEMBLY_SPECULATIVE) != 0) | |
216 | { | |
217 | if (insn->is_speculative) | |
218 | { | |
046bebe1 | 219 | m_uiout->field_string ("is-speculative", "?"); |
393702cd TT |
220 | |
221 | /* The speculative execution indication overwrites the first | |
222 | character of the PC prefix. | |
223 | We assume a PC prefix length of 3 characters. */ | |
224 | if ((flags & DISASSEMBLY_OMIT_PC) == 0) | |
046bebe1 | 225 | m_uiout->text (pc_prefix (pc) + 1); |
393702cd | 226 | else |
046bebe1 | 227 | m_uiout->text (" "); |
393702cd TT |
228 | } |
229 | else if ((flags & DISASSEMBLY_OMIT_PC) == 0) | |
046bebe1 | 230 | m_uiout->text (pc_prefix (pc)); |
393702cd | 231 | else |
046bebe1 | 232 | m_uiout->text (" "); |
393702cd TT |
233 | } |
234 | else if ((flags & DISASSEMBLY_OMIT_PC) == 0) | |
046bebe1 TT |
235 | m_uiout->text (pc_prefix (pc)); |
236 | m_uiout->field_core_addr ("address", gdbarch, pc); | |
393702cd | 237 | |
c7110220 | 238 | std::string name, filename; |
2dc80cf8 KB |
239 | bool omit_fname = ((flags & DISASSEMBLY_OMIT_FNAME) != 0); |
240 | if (!build_address_symbolic (gdbarch, pc, false, omit_fname, &name, | |
241 | &offset, &filename, &line, &unmapped)) | |
393702cd TT |
242 | { |
243 | /* We don't care now about line, filename and unmapped. But we might in | |
244 | the future. */ | |
046bebe1 | 245 | m_uiout->text (" <"); |
2dc80cf8 | 246 | if (!omit_fname) |
046bebe1 | 247 | m_uiout->field_string ("func-name", name.c_str (), |
e43b10e1 | 248 | function_name_style.style ()); |
2dc80cf8 KB |
249 | /* For negative offsets, avoid displaying them as +-N; the sign of |
250 | the offset takes the place of the "+" here. */ | |
251 | if (offset >= 0) | |
046bebe1 TT |
252 | m_uiout->text ("+"); |
253 | m_uiout->field_signed ("offset", offset); | |
254 | m_uiout->text (">:\t"); | |
393702cd TT |
255 | } |
256 | else | |
046bebe1 | 257 | m_uiout->text (":\t"); |
393702cd | 258 | |
393702cd TT |
259 | m_insn_stb.clear (); |
260 | ||
261 | if (flags & DISASSEMBLY_RAW_INSN) | |
262 | { | |
263 | CORE_ADDR end_pc; | |
264 | bfd_byte data; | |
393702cd TT |
265 | const char *spacer = ""; |
266 | ||
267 | /* Build the opcodes using a temporary stream so we can | |
268 | write them out in a single go for the MI. */ | |
269 | m_opcode_stb.clear (); | |
270 | ||
271 | size = m_di.print_insn (pc); | |
272 | end_pc = pc + size; | |
273 | ||
274 | for (;pc < end_pc; ++pc) | |
275 | { | |
276 | read_code (pc, &data, 1); | |
277 | m_opcode_stb.printf ("%s%02x", spacer, (unsigned) data); | |
278 | spacer = " "; | |
279 | } | |
280 | ||
046bebe1 TT |
281 | m_uiout->field_stream ("opcodes", m_opcode_stb); |
282 | m_uiout->text ("\t"); | |
393702cd TT |
283 | } |
284 | else | |
8b172ce7 | 285 | size = m_di.print_insn (pc); |
af70908d | 286 | |
046bebe1 | 287 | m_uiout->field_stream ("inst", m_insn_stb); |
393702cd | 288 | } |
046bebe1 | 289 | m_uiout->text ("\n"); |
af70908d MM |
290 | |
291 | return size; | |
292 | } | |
293 | ||
294 | static int | |
187808b0 PA |
295 | dump_insns (struct gdbarch *gdbarch, |
296 | struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high, | |
9a24775b | 297 | int how_many, gdb_disassembly_flags flags, CORE_ADDR *end_pc) |
af70908d | 298 | { |
a50a4026 | 299 | struct disasm_insn insn; |
af70908d MM |
300 | int num_displayed = 0; |
301 | ||
a50a4026 MM |
302 | memset (&insn, 0, sizeof (insn)); |
303 | insn.addr = low; | |
304 | ||
046bebe1 | 305 | gdb_pretty_print_disassembler disasm (gdbarch, uiout); |
8b172ce7 | 306 | |
a50a4026 | 307 | while (insn.addr < high && (how_many < 0 || num_displayed < how_many)) |
af70908d MM |
308 | { |
309 | int size; | |
310 | ||
046bebe1 | 311 | size = disasm.pretty_print_insn (&insn, flags); |
af70908d MM |
312 | if (size <= 0) |
313 | break; | |
314 | ||
315 | ++num_displayed; | |
a50a4026 | 316 | insn.addr += size; |
af70908d MM |
317 | |
318 | /* Allow user to bail out with ^C. */ | |
319 | QUIT; | |
92df71f0 | 320 | } |
6ff0ba5f DE |
321 | |
322 | if (end_pc != NULL) | |
a50a4026 | 323 | *end_pc = insn.addr; |
af70908d | 324 | |
92df71f0 FN |
325 | return num_displayed; |
326 | } | |
327 | ||
328 | /* The idea here is to present a source-O-centric view of a | |
329 | function to the user. This means that things are presented | |
330 | in source order, with (possibly) out of order assembly | |
6ff0ba5f DE |
331 | immediately following. |
332 | ||
333 | N.B. This view is deprecated. */ | |
0963b4bd | 334 | |
92df71f0 | 335 | static void |
6ff0ba5f | 336 | do_mixed_source_and_assembly_deprecated |
187808b0 PA |
337 | (struct gdbarch *gdbarch, struct ui_out *uiout, |
338 | struct symtab *symtab, | |
6ff0ba5f | 339 | CORE_ADDR low, CORE_ADDR high, |
9a24775b | 340 | int how_many, gdb_disassembly_flags flags) |
92df71f0 FN |
341 | { |
342 | int newlines = 0; | |
6ff0ba5f DE |
343 | int nlines; |
344 | struct linetable_entry *le; | |
345 | struct deprecated_dis_line_entry *mle; | |
92df71f0 FN |
346 | struct symtab_and_line sal; |
347 | int i; | |
348 | int out_of_order = 0; | |
349 | int next_line = 0; | |
92df71f0 | 350 | int num_displayed = 0; |
8d297bbf | 351 | print_source_lines_flags psl_flags = 0; |
92df71f0 | 352 | |
6ff0ba5f DE |
353 | gdb_assert (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL); |
354 | ||
355 | nlines = SYMTAB_LINETABLE (symtab)->nitems; | |
356 | le = SYMTAB_LINETABLE (symtab)->item; | |
357 | ||
4cd29721 MM |
358 | if (flags & DISASSEMBLY_FILENAME) |
359 | psl_flags |= PRINT_SOURCE_LINES_FILENAME; | |
360 | ||
6ff0ba5f DE |
361 | mle = (struct deprecated_dis_line_entry *) |
362 | alloca (nlines * sizeof (struct deprecated_dis_line_entry)); | |
92df71f0 FN |
363 | |
364 | /* Copy linetable entries for this function into our data | |
365 | structure, creating end_pc's and setting out_of_order as | |
366 | appropriate. */ | |
367 | ||
368 | /* First, skip all the preceding functions. */ | |
369 | ||
370 | for (i = 0; i < nlines - 1 && le[i].pc < low; i++); | |
371 | ||
372 | /* Now, copy all entries before the end of this function. */ | |
373 | ||
374 | for (; i < nlines - 1 && le[i].pc < high; i++) | |
375 | { | |
376 | if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc) | |
0963b4bd | 377 | continue; /* Ignore duplicates. */ |
92df71f0 FN |
378 | |
379 | /* Skip any end-of-function markers. */ | |
380 | if (le[i].line == 0) | |
381 | continue; | |
382 | ||
383 | mle[newlines].line = le[i].line; | |
384 | if (le[i].line > le[i + 1].line) | |
385 | out_of_order = 1; | |
386 | mle[newlines].start_pc = le[i].pc; | |
387 | mle[newlines].end_pc = le[i + 1].pc; | |
388 | newlines++; | |
389 | } | |
390 | ||
391 | /* If we're on the last line, and it's part of the function, | |
392 | then we need to get the end pc in a special way. */ | |
393 | ||
394 | if (i == nlines - 1 && le[i].pc < high) | |
395 | { | |
396 | mle[newlines].line = le[i].line; | |
397 | mle[newlines].start_pc = le[i].pc; | |
398 | sal = find_pc_line (le[i].pc, 0); | |
399 | mle[newlines].end_pc = sal.end; | |
400 | newlines++; | |
401 | } | |
402 | ||
6ff0ba5f | 403 | /* Now, sort mle by line #s (and, then by addresses within lines). */ |
92df71f0 FN |
404 | |
405 | if (out_of_order) | |
39ef2f62 | 406 | std::sort (mle, mle + newlines, line_is_less_than); |
92df71f0 FN |
407 | |
408 | /* Now, for each line entry, emit the specified lines (unless | |
409 | they have been emitted before), followed by the assembly code | |
410 | for that line. */ | |
411 | ||
30f0b101 TT |
412 | ui_out_emit_list asm_insns_list (uiout, "asm_insns"); |
413 | ||
414 | gdb::optional<ui_out_emit_tuple> outer_tuple_emitter; | |
415 | gdb::optional<ui_out_emit_list> inner_list_emitter; | |
92df71f0 FN |
416 | |
417 | for (i = 0; i < newlines; i++) | |
418 | { | |
92df71f0 FN |
419 | /* Print out everything from next_line to the current line. */ |
420 | if (mle[i].line >= next_line) | |
421 | { | |
422 | if (next_line != 0) | |
423 | { | |
0963b4bd | 424 | /* Just one line to print. */ |
92df71f0 FN |
425 | if (next_line == mle[i].line) |
426 | { | |
30f0b101 | 427 | outer_tuple_emitter.emplace (uiout, "src_and_asm_line"); |
4cd29721 | 428 | print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags); |
92df71f0 FN |
429 | } |
430 | else | |
431 | { | |
0963b4bd | 432 | /* Several source lines w/o asm instructions associated. */ |
92df71f0 FN |
433 | for (; next_line < mle[i].line; next_line++) |
434 | { | |
2e783024 TT |
435 | ui_out_emit_tuple tuple_emitter (uiout, |
436 | "src_and_asm_line"); | |
92df71f0 | 437 | print_source_lines (symtab, next_line, next_line + 1, |
4cd29721 | 438 | psl_flags); |
b926417a TT |
439 | ui_out_emit_list temp_list_emitter (uiout, |
440 | "line_asm_insn"); | |
92df71f0 FN |
441 | } |
442 | /* Print the last line and leave list open for | |
0963b4bd | 443 | asm instructions to be added. */ |
30f0b101 | 444 | outer_tuple_emitter.emplace (uiout, "src_and_asm_line"); |
4cd29721 | 445 | print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags); |
92df71f0 FN |
446 | } |
447 | } | |
448 | else | |
449 | { | |
30f0b101 | 450 | outer_tuple_emitter.emplace (uiout, "src_and_asm_line"); |
4cd29721 | 451 | print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags); |
92df71f0 FN |
452 | } |
453 | ||
454 | next_line = mle[i].line + 1; | |
30f0b101 | 455 | inner_list_emitter.emplace (uiout, "line_asm_insn"); |
92df71f0 FN |
456 | } |
457 | ||
187808b0 | 458 | num_displayed += dump_insns (gdbarch, uiout, |
13274fc3 | 459 | mle[i].start_pc, mle[i].end_pc, |
e47ad6c0 | 460 | how_many, flags, NULL); |
0127c0d3 JJ |
461 | |
462 | /* When we've reached the end of the mle array, or we've seen the last | |
463 | assembly range for this source line, close out the list/tuple. */ | |
464 | if (i == (newlines - 1) || mle[i + 1].line > mle[i].line) | |
92df71f0 | 465 | { |
30f0b101 TT |
466 | inner_list_emitter.reset (); |
467 | outer_tuple_emitter.reset (); | |
112e8700 | 468 | uiout->text ("\n"); |
92df71f0 | 469 | } |
0127c0d3 JJ |
470 | if (how_many >= 0 && num_displayed >= how_many) |
471 | break; | |
92df71f0 | 472 | } |
92df71f0 FN |
473 | } |
474 | ||
6ff0ba5f DE |
475 | /* The idea here is to present a source-O-centric view of a |
476 | function to the user. This means that things are presented | |
477 | in source order, with (possibly) out of order assembly | |
478 | immediately following. */ | |
479 | ||
480 | static void | |
e47ad6c0 YQ |
481 | do_mixed_source_and_assembly (struct gdbarch *gdbarch, |
482 | struct ui_out *uiout, | |
6ff0ba5f DE |
483 | struct symtab *main_symtab, |
484 | CORE_ADDR low, CORE_ADDR high, | |
9a24775b | 485 | int how_many, gdb_disassembly_flags flags) |
6ff0ba5f | 486 | { |
6ff0ba5f | 487 | const struct linetable_entry *le, *first_le; |
6ff0ba5f | 488 | int i, nlines; |
6ff0ba5f | 489 | int num_displayed = 0; |
8d297bbf | 490 | print_source_lines_flags psl_flags = 0; |
6ff0ba5f DE |
491 | CORE_ADDR pc; |
492 | struct symtab *last_symtab; | |
493 | int last_line; | |
6ff0ba5f DE |
494 | |
495 | gdb_assert (main_symtab != NULL && SYMTAB_LINETABLE (main_symtab) != NULL); | |
496 | ||
497 | /* First pass: collect the list of all source files and lines. | |
498 | We do this so that we can only print lines containing code once. | |
499 | We try to print the source text leading up to the next instruction, | |
500 | but if that text is for code that will be disassembled later, then | |
501 | we'll want to defer printing it until later with its associated code. */ | |
502 | ||
fc4007c9 | 503 | htab_up dis_line_table (allocate_dis_line_table ()); |
6ff0ba5f DE |
504 | |
505 | pc = low; | |
506 | ||
507 | /* The prologue may be empty, but there may still be a line number entry | |
508 | for the opening brace which is distinct from the first line of code. | |
509 | If the prologue has been eliminated find_pc_line may return the source | |
510 | line after the opening brace. We still want to print this opening brace. | |
511 | first_le is used to implement this. */ | |
512 | ||
513 | nlines = SYMTAB_LINETABLE (main_symtab)->nitems; | |
514 | le = SYMTAB_LINETABLE (main_symtab)->item; | |
515 | first_le = NULL; | |
516 | ||
517 | /* Skip all the preceding functions. */ | |
518 | for (i = 0; i < nlines && le[i].pc < low; i++) | |
519 | continue; | |
520 | ||
521 | if (i < nlines && le[i].pc < high) | |
522 | first_le = &le[i]; | |
523 | ||
524 | /* Add lines for every pc value. */ | |
525 | while (pc < high) | |
526 | { | |
527 | struct symtab_and_line sal; | |
528 | int length; | |
529 | ||
530 | sal = find_pc_line (pc, 0); | |
531 | length = gdb_insn_length (gdbarch, pc); | |
532 | pc += length; | |
533 | ||
534 | if (sal.symtab != NULL) | |
fc4007c9 | 535 | add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line); |
6ff0ba5f DE |
536 | } |
537 | ||
538 | /* Second pass: print the disassembly. | |
539 | ||
540 | Output format, from an MI perspective: | |
541 | The result is a ui_out list, field name "asm_insns", where elements have | |
542 | name "src_and_asm_line". | |
543 | Each element is a tuple of source line specs (field names line, file, | |
544 | fullname), and field "line_asm_insn" which contains the disassembly. | |
545 | Field "line_asm_insn" is a list of tuples: address, func-name, offset, | |
546 | opcodes, inst. | |
547 | ||
548 | CLI output works on top of this because MI ignores ui_out_text output, | |
549 | which is where we put file name and source line contents output. | |
550 | ||
30f0b101 TT |
551 | Emitter usage: |
552 | asm_insns_emitter | |
6ff0ba5f | 553 | Handles the outer "asm_insns" list. |
30f0b101 | 554 | tuple_emitter |
6ff0ba5f | 555 | The tuples for each group of consecutive disassemblies. |
30f0b101 | 556 | list_emitter |
6ff0ba5f DE |
557 | List of consecutive source lines or disassembled insns. */ |
558 | ||
559 | if (flags & DISASSEMBLY_FILENAME) | |
560 | psl_flags |= PRINT_SOURCE_LINES_FILENAME; | |
561 | ||
30f0b101 | 562 | ui_out_emit_list asm_insns_emitter (uiout, "asm_insns"); |
6ff0ba5f | 563 | |
30f0b101 TT |
564 | gdb::optional<ui_out_emit_tuple> tuple_emitter; |
565 | gdb::optional<ui_out_emit_list> list_emitter; | |
6ff0ba5f DE |
566 | |
567 | last_symtab = NULL; | |
568 | last_line = 0; | |
569 | pc = low; | |
570 | ||
571 | while (pc < high) | |
572 | { | |
6ff0ba5f DE |
573 | struct symtab_and_line sal; |
574 | CORE_ADDR end_pc; | |
575 | int start_preceding_line_to_display = 0; | |
576 | int end_preceding_line_to_display = 0; | |
577 | int new_source_line = 0; | |
578 | ||
579 | sal = find_pc_line (pc, 0); | |
580 | ||
581 | if (sal.symtab != last_symtab) | |
582 | { | |
583 | /* New source file. */ | |
584 | new_source_line = 1; | |
585 | ||
586 | /* If this is the first line of output, check for any preceding | |
587 | lines. */ | |
588 | if (last_line == 0 | |
589 | && first_le != NULL | |
590 | && first_le->line < sal.line) | |
591 | { | |
592 | start_preceding_line_to_display = first_le->line; | |
593 | end_preceding_line_to_display = sal.line; | |
594 | } | |
595 | } | |
596 | else | |
597 | { | |
598 | /* Same source file as last time. */ | |
599 | if (sal.symtab != NULL) | |
600 | { | |
601 | if (sal.line > last_line + 1 && last_line != 0) | |
602 | { | |
603 | int l; | |
604 | ||
605 | /* Several preceding source lines. Print the trailing ones | |
606 | not associated with code that we'll print later. */ | |
607 | for (l = sal.line - 1; l > last_line; --l) | |
608 | { | |
fc4007c9 TT |
609 | if (line_has_code_p (dis_line_table.get (), |
610 | sal.symtab, l)) | |
6ff0ba5f DE |
611 | break; |
612 | } | |
613 | if (l < sal.line - 1) | |
614 | { | |
615 | start_preceding_line_to_display = l + 1; | |
616 | end_preceding_line_to_display = sal.line; | |
617 | } | |
618 | } | |
619 | if (sal.line != last_line) | |
620 | new_source_line = 1; | |
621 | else | |
622 | { | |
623 | /* Same source line as last time. This can happen, depending | |
624 | on the debug info. */ | |
625 | } | |
626 | } | |
627 | } | |
628 | ||
629 | if (new_source_line) | |
630 | { | |
631 | /* Skip the newline if this is the first instruction. */ | |
632 | if (pc > low) | |
112e8700 | 633 | uiout->text ("\n"); |
30f0b101 | 634 | if (tuple_emitter.has_value ()) |
6ff0ba5f | 635 | { |
30f0b101 TT |
636 | gdb_assert (list_emitter.has_value ()); |
637 | list_emitter.reset (); | |
638 | tuple_emitter.reset (); | |
6ff0ba5f DE |
639 | } |
640 | if (sal.symtab != last_symtab | |
641 | && !(flags & DISASSEMBLY_FILENAME)) | |
642 | { | |
643 | /* Remember MI ignores ui_out_text. | |
644 | We don't have to do anything here for MI because MI | |
645 | output includes the source specs for each line. */ | |
646 | if (sal.symtab != NULL) | |
647 | { | |
112e8700 | 648 | uiout->text (symtab_to_filename_for_display (sal.symtab)); |
6ff0ba5f DE |
649 | } |
650 | else | |
112e8700 SM |
651 | uiout->text ("unknown"); |
652 | uiout->text (":\n"); | |
6ff0ba5f DE |
653 | } |
654 | if (start_preceding_line_to_display > 0) | |
655 | { | |
656 | /* Several source lines w/o asm instructions associated. | |
657 | We need to preserve the structure of the output, so output | |
658 | a bunch of line tuples with no asm entries. */ | |
659 | int l; | |
6ff0ba5f DE |
660 | |
661 | gdb_assert (sal.symtab != NULL); | |
662 | for (l = start_preceding_line_to_display; | |
663 | l < end_preceding_line_to_display; | |
664 | ++l) | |
665 | { | |
b926417a TT |
666 | ui_out_emit_tuple line_tuple_emitter (uiout, |
667 | "src_and_asm_line"); | |
6ff0ba5f | 668 | print_source_lines (sal.symtab, l, l + 1, psl_flags); |
30f0b101 | 669 | ui_out_emit_list chain_line_emitter (uiout, "line_asm_insn"); |
6ff0ba5f DE |
670 | } |
671 | } | |
30f0b101 | 672 | tuple_emitter.emplace (uiout, "src_and_asm_line"); |
6ff0ba5f DE |
673 | if (sal.symtab != NULL) |
674 | print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags); | |
675 | else | |
112e8700 | 676 | uiout->text (_("--- no source info for this pc ---\n")); |
30f0b101 | 677 | list_emitter.emplace (uiout, "line_asm_insn"); |
6ff0ba5f DE |
678 | } |
679 | else | |
680 | { | |
681 | /* Here we're appending instructions to an existing line. | |
682 | By construction the very first insn will have a symtab | |
683 | and follow the new_source_line path above. */ | |
30f0b101 TT |
684 | gdb_assert (tuple_emitter.has_value ()); |
685 | gdb_assert (list_emitter.has_value ()); | |
6ff0ba5f DE |
686 | } |
687 | ||
688 | if (sal.end != 0) | |
325fac50 | 689 | end_pc = std::min (sal.end, high); |
6ff0ba5f DE |
690 | else |
691 | end_pc = pc + 1; | |
187808b0 | 692 | num_displayed += dump_insns (gdbarch, uiout, pc, end_pc, |
e47ad6c0 | 693 | how_many, flags, &end_pc); |
6ff0ba5f DE |
694 | pc = end_pc; |
695 | ||
696 | if (how_many >= 0 && num_displayed >= how_many) | |
697 | break; | |
698 | ||
699 | last_symtab = sal.symtab; | |
700 | last_line = sal.line; | |
701 | } | |
6ff0ba5f | 702 | } |
92df71f0 FN |
703 | |
704 | static void | |
187808b0 | 705 | do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout, |
92df71f0 | 706 | CORE_ADDR low, CORE_ADDR high, |
9a24775b | 707 | int how_many, gdb_disassembly_flags flags) |
92df71f0 | 708 | { |
10f489e5 | 709 | ui_out_emit_list list_emitter (uiout, "asm_insns"); |
92df71f0 | 710 | |
187808b0 | 711 | dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL); |
92df71f0 FN |
712 | } |
713 | ||
92bf2b80 AC |
714 | /* Initialize the disassemble info struct ready for the specified |
715 | stream. */ | |
716 | ||
a0b31db1 | 717 | static int ATTRIBUTE_PRINTF (2, 3) |
242e8be5 AC |
718 | fprintf_disasm (void *stream, const char *format, ...) |
719 | { | |
720 | va_list args; | |
9a619af0 | 721 | |
242e8be5 | 722 | va_start (args, format); |
9a3c8263 | 723 | vfprintf_filtered ((struct ui_file *) stream, format, args); |
242e8be5 AC |
724 | va_end (args); |
725 | /* Something non -ve. */ | |
726 | return 0; | |
727 | } | |
728 | ||
471b9d15 MR |
729 | /* Combine implicit and user disassembler options and return them |
730 | in a newly-created string. */ | |
731 | ||
732 | static std::string | |
733 | get_all_disassembler_options (struct gdbarch *gdbarch) | |
734 | { | |
735 | const char *implicit = gdbarch_disassembler_options_implicit (gdbarch); | |
736 | const char *options = get_disassembler_options (gdbarch); | |
737 | const char *comma = ","; | |
738 | ||
739 | if (implicit == nullptr) | |
740 | { | |
741 | implicit = ""; | |
742 | comma = ""; | |
743 | } | |
744 | ||
745 | if (options == nullptr) | |
746 | { | |
747 | options = ""; | |
748 | comma = ""; | |
749 | } | |
750 | ||
751 | return string_printf ("%s%s%s", implicit, comma, options); | |
752 | } | |
753 | ||
e47ad6c0 YQ |
754 | gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch, |
755 | struct ui_file *file, | |
756 | di_read_memory_ftype read_memory_func) | |
d8b49cf0 YQ |
757 | : m_gdbarch (gdbarch), |
758 | m_err_memaddr (0) | |
92df71f0 | 759 | { |
e47ad6c0 YQ |
760 | init_disassemble_info (&m_di, file, fprintf_disasm); |
761 | m_di.flavour = bfd_target_unknown_flavour; | |
762 | m_di.memory_error_func = dis_asm_memory_error; | |
763 | m_di.print_address_func = dis_asm_print_address; | |
2b6fd0d8 | 764 | /* NOTE: cagney/2003-04-28: The original code, from the old Insight |
85102364 | 765 | disassembler had a local optimization here. By default it would |
2b6fd0d8 | 766 | access the executable file, instead of the target memory (there |
ce2826aa | 767 | was a growing list of exceptions though). Unfortunately, the |
2b6fd0d8 AC |
768 | heuristic was flawed. Commands like "disassemble &variable" |
769 | didn't work as they relied on the access going to the target. | |
85102364 | 770 | Further, it has been superseeded by trust-read-only-sections |
2b6fd0d8 | 771 | (although that should be superseeded by target_trust..._p()). */ |
e47ad6c0 YQ |
772 | m_di.read_memory_func = read_memory_func; |
773 | m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
774 | m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach; | |
775 | m_di.endian = gdbarch_byte_order (gdbarch); | |
776 | m_di.endian_code = gdbarch_byte_order_for_code (gdbarch); | |
777 | m_di.application_data = this; | |
471b9d15 MR |
778 | m_disassembler_options_holder = get_all_disassembler_options (gdbarch); |
779 | if (!m_disassembler_options_holder.empty ()) | |
780 | m_di.disassembler_options = m_disassembler_options_holder.c_str (); | |
e47ad6c0 YQ |
781 | disassemble_init_for_target (&m_di); |
782 | } | |
783 | ||
784 | int | |
785 | gdb_disassembler::print_insn (CORE_ADDR memaddr, | |
786 | int *branch_delay_insns) | |
787 | { | |
d8b49cf0 YQ |
788 | m_err_memaddr = 0; |
789 | ||
e47ad6c0 YQ |
790 | int length = gdbarch_print_insn (arch (), memaddr, &m_di); |
791 | ||
d8b49cf0 YQ |
792 | if (length < 0) |
793 | memory_error (TARGET_XFER_E_IO, m_err_memaddr); | |
794 | ||
e47ad6c0 YQ |
795 | if (branch_delay_insns != NULL) |
796 | { | |
797 | if (m_di.insn_info_valid) | |
798 | *branch_delay_insns = m_di.branch_delay_insns; | |
799 | else | |
800 | *branch_delay_insns = 0; | |
801 | } | |
802 | return length; | |
92bf2b80 AC |
803 | } |
804 | ||
805 | void | |
13274fc3 | 806 | gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout, |
9a24775b | 807 | gdb_disassembly_flags flags, int how_many, |
9c419145 | 808 | CORE_ADDR low, CORE_ADDR high) |
92bf2b80 | 809 | { |
34248c3a | 810 | struct symtab *symtab; |
92bf2b80 | 811 | int nlines = -1; |
92df71f0 | 812 | |
0963b4bd | 813 | /* Assume symtab is valid for whole PC range. */ |
34248c3a | 814 | symtab = find_pc_line_symtab (low); |
92df71f0 | 815 | |
8435453b | 816 | if (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL) |
6ff0ba5f | 817 | nlines = SYMTAB_LINETABLE (symtab)->nitems; |
92df71f0 | 818 | |
6ff0ba5f DE |
819 | if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE)) |
820 | || nlines <= 0) | |
187808b0 | 821 | do_assembly_only (gdbarch, uiout, low, high, how_many, flags); |
92df71f0 | 822 | |
e6158f16 | 823 | else if (flags & DISASSEMBLY_SOURCE) |
187808b0 | 824 | do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high, |
e47ad6c0 | 825 | how_many, flags); |
6ff0ba5f DE |
826 | |
827 | else if (flags & DISASSEMBLY_SOURCE_DEPRECATED) | |
187808b0 | 828 | do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab, |
e47ad6c0 | 829 | low, high, how_many, flags); |
92df71f0 FN |
830 | |
831 | gdb_flush (gdb_stdout); | |
832 | } | |
810ecf9f | 833 | |
92bf2b80 | 834 | /* Print the instruction at address MEMADDR in debugged memory, |
a4642986 MR |
835 | on STREAM. Returns the length of the instruction, in bytes, |
836 | and, if requested, the number of branch delay slot instructions. */ | |
92bf2b80 AC |
837 | |
838 | int | |
13274fc3 UW |
839 | gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr, |
840 | struct ui_file *stream, int *branch_delay_insns) | |
92bf2b80 | 841 | { |
a4642986 | 842 | |
e47ad6c0 YQ |
843 | gdb_disassembler di (gdbarch, stream); |
844 | ||
845 | return di.print_insn (memaddr, branch_delay_insns); | |
92bf2b80 | 846 | } |
eda5a4d7 | 847 | |
eda5a4d7 PA |
848 | /* Return the length in bytes of the instruction at address MEMADDR in |
849 | debugged memory. */ | |
850 | ||
851 | int | |
852 | gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr) | |
853 | { | |
d7e74731 | 854 | return gdb_print_insn (gdbarch, addr, &null_stream, NULL); |
eda5a4d7 PA |
855 | } |
856 | ||
857 | /* fprintf-function for gdb_buffered_insn_length. This function is a | |
858 | nop, we don't want to print anything, we just want to compute the | |
859 | length of the insn. */ | |
860 | ||
861 | static int ATTRIBUTE_PRINTF (2, 3) | |
862 | gdb_buffered_insn_length_fprintf (void *stream, const char *format, ...) | |
863 | { | |
864 | return 0; | |
865 | } | |
866 | ||
471b9d15 MR |
867 | /* Initialize a struct disassemble_info for gdb_buffered_insn_length. |
868 | Upon return, *DISASSEMBLER_OPTIONS_HOLDER owns the string pointed | |
869 | to by DI.DISASSEMBLER_OPTIONS. */ | |
eda5a4d7 PA |
870 | |
871 | static void | |
872 | gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch, | |
873 | struct disassemble_info *di, | |
874 | const gdb_byte *insn, int max_len, | |
471b9d15 MR |
875 | CORE_ADDR addr, |
876 | std::string *disassembler_options_holder) | |
eda5a4d7 PA |
877 | { |
878 | init_disassemble_info (di, NULL, gdb_buffered_insn_length_fprintf); | |
879 | ||
880 | /* init_disassemble_info installs buffer_read_memory, etc. | |
881 | so we don't need to do that here. | |
882 | The cast is necessary until disassemble_info is const-ified. */ | |
883 | di->buffer = (gdb_byte *) insn; | |
884 | di->buffer_length = max_len; | |
885 | di->buffer_vma = addr; | |
886 | ||
887 | di->arch = gdbarch_bfd_arch_info (gdbarch)->arch; | |
888 | di->mach = gdbarch_bfd_arch_info (gdbarch)->mach; | |
889 | di->endian = gdbarch_byte_order (gdbarch); | |
890 | di->endian_code = gdbarch_byte_order_for_code (gdbarch); | |
891 | ||
471b9d15 MR |
892 | *disassembler_options_holder = get_all_disassembler_options (gdbarch); |
893 | if (!disassembler_options_holder->empty ()) | |
894 | di->disassembler_options = disassembler_options_holder->c_str (); | |
eda5a4d7 PA |
895 | disassemble_init_for_target (di); |
896 | } | |
897 | ||
898 | /* Return the length in bytes of INSN. MAX_LEN is the size of the | |
899 | buffer containing INSN. */ | |
900 | ||
901 | int | |
902 | gdb_buffered_insn_length (struct gdbarch *gdbarch, | |
903 | const gdb_byte *insn, int max_len, CORE_ADDR addr) | |
904 | { | |
905 | struct disassemble_info di; | |
471b9d15 | 906 | std::string disassembler_options_holder; |
eda5a4d7 | 907 | |
471b9d15 MR |
908 | gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr, |
909 | &disassembler_options_holder); | |
eda5a4d7 PA |
910 | |
911 | return gdbarch_print_insn (gdbarch, addr, &di); | |
912 | } | |
65b48a81 PB |
913 | |
914 | char * | |
915 | get_disassembler_options (struct gdbarch *gdbarch) | |
916 | { | |
917 | char **disassembler_options = gdbarch_disassembler_options (gdbarch); | |
918 | if (disassembler_options == NULL) | |
919 | return NULL; | |
920 | return *disassembler_options; | |
921 | } | |
922 | ||
923 | void | |
924 | set_disassembler_options (char *prospective_options) | |
925 | { | |
926 | struct gdbarch *gdbarch = get_current_arch (); | |
927 | char **disassembler_options = gdbarch_disassembler_options (gdbarch); | |
471b9d15 | 928 | const disasm_options_and_args_t *valid_options_and_args; |
65b48a81 PB |
929 | const disasm_options_t *valid_options; |
930 | char *options = remove_whitespace_and_extra_commas (prospective_options); | |
f995bbe8 | 931 | const char *opt; |
65b48a81 PB |
932 | |
933 | /* Allow all architectures, even ones that do not support 'set disassembler', | |
934 | to reset their disassembler options to NULL. */ | |
935 | if (options == NULL) | |
936 | { | |
937 | if (disassembler_options != NULL) | |
938 | { | |
939 | free (*disassembler_options); | |
940 | *disassembler_options = NULL; | |
941 | } | |
942 | return; | |
943 | } | |
944 | ||
471b9d15 MR |
945 | valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch); |
946 | if (valid_options_and_args == NULL) | |
65b48a81 | 947 | { |
81f47ac2 | 948 | fprintf_filtered (gdb_stderr, _("\ |
65b48a81 PB |
949 | 'set disassembler-options ...' is not supported on this architecture.\n")); |
950 | return; | |
951 | } | |
952 | ||
471b9d15 MR |
953 | valid_options = &valid_options_and_args->options; |
954 | ||
65b48a81 PB |
955 | /* Verify we have valid disassembler options. */ |
956 | FOR_EACH_DISASSEMBLER_OPTION (opt, options) | |
957 | { | |
958 | size_t i; | |
959 | for (i = 0; valid_options->name[i] != NULL; i++) | |
471b9d15 MR |
960 | if (valid_options->arg != NULL && valid_options->arg[i] != NULL) |
961 | { | |
962 | size_t len = strlen (valid_options->name[i]); | |
963 | bool found = false; | |
964 | const char *arg; | |
965 | size_t j; | |
966 | ||
967 | if (memcmp (opt, valid_options->name[i], len) != 0) | |
968 | continue; | |
969 | arg = opt + len; | |
970 | for (j = 0; valid_options->arg[i]->values[j] != NULL; j++) | |
971 | if (disassembler_options_cmp | |
972 | (arg, valid_options->arg[i]->values[j]) == 0) | |
973 | { | |
974 | found = true; | |
975 | break; | |
976 | } | |
977 | if (found) | |
978 | break; | |
979 | } | |
980 | else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0) | |
65b48a81 PB |
981 | break; |
982 | if (valid_options->name[i] == NULL) | |
983 | { | |
81f47ac2 | 984 | fprintf_filtered (gdb_stderr, |
65b48a81 PB |
985 | _("Invalid disassembler option value: '%s'.\n"), |
986 | opt); | |
987 | return; | |
988 | } | |
989 | } | |
990 | ||
991 | free (*disassembler_options); | |
992 | *disassembler_options = xstrdup (options); | |
993 | } | |
994 | ||
995 | static void | |
eb4c3f4a | 996 | set_disassembler_options_sfunc (const char *args, int from_tty, |
65b48a81 PB |
997 | struct cmd_list_element *c) |
998 | { | |
999 | set_disassembler_options (prospective_options); | |
1000 | } | |
1001 | ||
1002 | static void | |
1003 | show_disassembler_options_sfunc (struct ui_file *file, int from_tty, | |
1004 | struct cmd_list_element *c, const char *value) | |
1005 | { | |
1006 | struct gdbarch *gdbarch = get_current_arch (); | |
471b9d15 MR |
1007 | const disasm_options_and_args_t *valid_options_and_args; |
1008 | const disasm_option_arg_t *valid_args; | |
65b48a81 PB |
1009 | const disasm_options_t *valid_options; |
1010 | ||
1011 | const char *options = get_disassembler_options (gdbarch); | |
1012 | if (options == NULL) | |
1013 | options = ""; | |
1014 | ||
f4bab6ff | 1015 | fprintf_filtered (file, _("The current disassembler options are '%s'\n\n"), |
65b48a81 PB |
1016 | options); |
1017 | ||
471b9d15 | 1018 | valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch); |
65b48a81 | 1019 | |
471b9d15 | 1020 | if (valid_options_and_args == NULL) |
f4bab6ff TT |
1021 | { |
1022 | fputs_filtered (_("There are no disassembler options available " | |
1023 | "for this architecture.\n"), | |
1024 | file); | |
1025 | return; | |
1026 | } | |
65b48a81 | 1027 | |
471b9d15 MR |
1028 | valid_options = &valid_options_and_args->options; |
1029 | ||
f4bab6ff | 1030 | fprintf_filtered (file, _("\ |
65b48a81 | 1031 | The following disassembler options are supported for use with the\n\ |
f4bab6ff | 1032 | 'set disassembler-options OPTION [,OPTION]...' command:\n")); |
65b48a81 PB |
1033 | |
1034 | if (valid_options->description != NULL) | |
1035 | { | |
1036 | size_t i, max_len = 0; | |
1037 | ||
471b9d15 MR |
1038 | fprintf_filtered (file, "\n"); |
1039 | ||
65b48a81 PB |
1040 | /* Compute the length of the longest option name. */ |
1041 | for (i = 0; valid_options->name[i] != NULL; i++) | |
1042 | { | |
1043 | size_t len = strlen (valid_options->name[i]); | |
471b9d15 MR |
1044 | |
1045 | if (valid_options->arg != NULL && valid_options->arg[i] != NULL) | |
1046 | len += strlen (valid_options->arg[i]->name); | |
65b48a81 PB |
1047 | if (max_len < len) |
1048 | max_len = len; | |
1049 | } | |
1050 | ||
1051 | for (i = 0, max_len++; valid_options->name[i] != NULL; i++) | |
1052 | { | |
1053 | fprintf_filtered (file, " %s", valid_options->name[i]); | |
471b9d15 MR |
1054 | if (valid_options->arg != NULL && valid_options->arg[i] != NULL) |
1055 | fprintf_filtered (file, "%s", valid_options->arg[i]->name); | |
65b48a81 | 1056 | if (valid_options->description[i] != NULL) |
471b9d15 MR |
1057 | { |
1058 | size_t len = strlen (valid_options->name[i]); | |
1059 | ||
1060 | if (valid_options->arg != NULL && valid_options->arg[i] != NULL) | |
1061 | len += strlen (valid_options->arg[i]->name); | |
1062 | fprintf_filtered (file, "%*c %s", (int) (max_len - len), ' ', | |
1063 | valid_options->description[i]); | |
1064 | } | |
65b48a81 PB |
1065 | fprintf_filtered (file, "\n"); |
1066 | } | |
1067 | } | |
1068 | else | |
1069 | { | |
1070 | size_t i; | |
1071 | fprintf_filtered (file, " "); | |
1072 | for (i = 0; valid_options->name[i] != NULL; i++) | |
1073 | { | |
1074 | fprintf_filtered (file, "%s", valid_options->name[i]); | |
471b9d15 MR |
1075 | if (valid_options->arg != NULL && valid_options->arg[i] != NULL) |
1076 | fprintf_filtered (file, "%s", valid_options->arg[i]->name); | |
65b48a81 PB |
1077 | if (valid_options->name[i + 1] != NULL) |
1078 | fprintf_filtered (file, ", "); | |
1079 | wrap_here (" "); | |
1080 | } | |
1081 | fprintf_filtered (file, "\n"); | |
1082 | } | |
471b9d15 MR |
1083 | |
1084 | valid_args = valid_options_and_args->args; | |
1085 | if (valid_args != NULL) | |
1086 | { | |
1087 | size_t i, j; | |
1088 | ||
1089 | for (i = 0; valid_args[i].name != NULL; i++) | |
1090 | { | |
1091 | fprintf_filtered (file, _("\n\ | |
1092 | For the options above, the following values are supported for \"%s\":\n "), | |
1093 | valid_args[i].name); | |
1094 | for (j = 0; valid_args[i].values[j] != NULL; j++) | |
1095 | { | |
1096 | fprintf_filtered (file, " %s", valid_args[i].values[j]); | |
1097 | wrap_here (" "); | |
1098 | } | |
1099 | fprintf_filtered (file, "\n"); | |
1100 | } | |
1101 | } | |
65b48a81 PB |
1102 | } |
1103 | ||
1104 | /* A completion function for "set disassembler". */ | |
1105 | ||
eb3ff9a5 | 1106 | static void |
65b48a81 | 1107 | disassembler_options_completer (struct cmd_list_element *ignore, |
eb3ff9a5 | 1108 | completion_tracker &tracker, |
65b48a81 PB |
1109 | const char *text, const char *word) |
1110 | { | |
1111 | struct gdbarch *gdbarch = get_current_arch (); | |
471b9d15 MR |
1112 | const disasm_options_and_args_t *opts_and_args |
1113 | = gdbarch_valid_disassembler_options (gdbarch); | |
65b48a81 | 1114 | |
471b9d15 | 1115 | if (opts_and_args != NULL) |
65b48a81 | 1116 | { |
471b9d15 MR |
1117 | const disasm_options_t *opts = &opts_and_args->options; |
1118 | ||
65b48a81 PB |
1119 | /* Only attempt to complete on the last option text. */ |
1120 | const char *separator = strrchr (text, ','); | |
1121 | if (separator != NULL) | |
1122 | text = separator + 1; | |
f1735a53 | 1123 | text = skip_spaces (text); |
eb3ff9a5 | 1124 | complete_on_enum (tracker, opts->name, text, word); |
65b48a81 | 1125 | } |
65b48a81 PB |
1126 | } |
1127 | ||
1128 | ||
1129 | /* Initialization code. */ | |
1130 | ||
65b48a81 PB |
1131 | void |
1132 | _initialize_disasm (void) | |
1133 | { | |
1134 | struct cmd_list_element *cmd; | |
1135 | ||
1136 | /* Add the command that controls the disassembler options. */ | |
1137 | cmd = add_setshow_string_noescape_cmd ("disassembler-options", no_class, | |
1138 | &prospective_options, _("\ | |
1139 | Set the disassembler options.\n\ | |
7c9ee61b | 1140 | Usage: set disassembler-options OPTION [,OPTION]...\n\n\ |
89549d7f | 1141 | See: 'show disassembler-options' for valid option values."), _("\ |
65b48a81 PB |
1142 | Show the disassembler options."), NULL, |
1143 | set_disassembler_options_sfunc, | |
1144 | show_disassembler_options_sfunc, | |
1145 | &setlist, &showlist); | |
1146 | set_cmd_completer (cmd, disassembler_options_completer); | |
1147 | } |