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