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