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