1 /* Disassemble support for GDB.
3 Copyright (C) 2000-2020 Free Software Foundation, Inc.
5 This file is part of GDB.
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.
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.
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/>. */
21 #include "arch-utils.h"
30 #include "safe-ctype.h"
32 #include "gdbsupport/gdb_optional.h"
34 #include "cli/cli-style.h"
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. */
40 /* This variable is used to hold the prospective disassembler_options value
41 which is set by the "set disassembler_options" command. */
42 static char *prospective_options
= NULL
;
44 /* This structure is used to store line number information for the
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. */
50 struct deprecated_dis_line_entry
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. */
64 struct symtab
*symtab
;
68 /* Hash function for dis_line_entry. */
71 hash_dis_line_entry (const void *item
)
73 const struct dis_line_entry
*dle
= (const struct dis_line_entry
*) item
;
75 return htab_hash_pointer (dle
->symtab
) + dle
->line
;
78 /* Equal function for dis_line_entry. */
81 eq_dis_line_entry (const void *item_lhs
, const void *item_rhs
)
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
;
86 return (lhs
->symtab
== rhs
->symtab
87 && lhs
->line
== rhs
->line
);
90 /* Create the table to manage lines for mixed source/disassembly. */
93 allocate_dis_line_table (void)
95 return htab_create_alloc (41,
96 hash_dis_line_entry
, eq_dis_line_entry
,
97 xfree
, xcalloc
, xfree
);
100 /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
103 add_dis_line_entry (htab_t table
, struct symtab
*symtab
, int line
)
106 struct dis_line_entry dle
, *dlep
;
110 slot
= htab_find_slot (table
, &dle
, INSERT
);
113 dlep
= XNEW (struct dis_line_entry
);
114 dlep
->symtab
= symtab
;
120 /* Return non-zero if SYMTAB, LINE are in TABLE. */
123 line_has_code_p (htab_t table
, struct symtab
*symtab
, int line
)
125 struct dis_line_entry dle
;
129 return htab_find (table
, &dle
) != NULL
;
132 /* Wrapper of target_read_code. */
135 gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
137 struct disassemble_info
*info
)
139 return target_read_code (memaddr
, myaddr
, len
);
142 /* Wrapper of memory_error. */
145 gdb_disassembler::dis_asm_memory_error (int err
, bfd_vma memaddr
,
146 struct disassemble_info
*info
)
148 gdb_disassembler
*self
149 = static_cast<gdb_disassembler
*>(info
->application_data
);
151 self
->m_err_memaddr
= memaddr
;
154 /* Wrapper of print_address. */
157 gdb_disassembler::dis_asm_print_address (bfd_vma addr
,
158 struct disassemble_info
*info
)
160 gdb_disassembler
*self
161 = static_cast<gdb_disassembler
*>(info
->application_data
);
163 print_address (self
->arch (), addr
, self
->stream ());
167 line_is_less_than (const deprecated_dis_line_entry
&mle1
,
168 const deprecated_dis_line_entry
&mle2
)
172 /* End of sequence markers have a line number of 0 but don't want to
173 be sorted to the head of the list, instead sort by PC. */
174 if (mle1
.line
== 0 || mle2
.line
== 0)
176 if (mle1
.start_pc
!= mle2
.start_pc
)
177 val
= mle1
.start_pc
< mle2
.start_pc
;
179 val
= mle1
.line
< mle2
.line
;
183 if (mle1
.line
!= mle2
.line
)
184 val
= mle1
.line
< mle2
.line
;
186 val
= mle1
.start_pc
< mle2
.start_pc
;
194 gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn
*insn
,
195 gdb_disassembly_flags flags
)
197 /* parts of the symbolic representation of the address */
203 struct gdbarch
*gdbarch
= arch ();
206 ui_out_emit_tuple
tuple_emitter (m_uiout
, NULL
);
209 if (insn
->number
!= 0)
211 m_uiout
->field_unsigned ("insn-number", insn
->number
);
212 m_uiout
->text ("\t");
215 if ((flags
& DISASSEMBLY_SPECULATIVE
) != 0)
217 if (insn
->is_speculative
)
219 m_uiout
->field_string ("is-speculative", "?");
221 /* The speculative execution indication overwrites the first
222 character of the PC prefix.
223 We assume a PC prefix length of 3 characters. */
224 if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
225 m_uiout
->text (pc_prefix (pc
) + 1);
229 else if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
230 m_uiout
->text (pc_prefix (pc
));
234 else if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
235 m_uiout
->text (pc_prefix (pc
));
236 m_uiout
->field_core_addr ("address", gdbarch
, pc
);
238 std::string name
, filename
;
239 bool omit_fname
= ((flags
& DISASSEMBLY_OMIT_FNAME
) != 0);
240 if (!build_address_symbolic (gdbarch
, pc
, false, omit_fname
, &name
,
241 &offset
, &filename
, &line
, &unmapped
))
243 /* We don't care now about line, filename and unmapped. But we might in
245 m_uiout
->text (" <");
247 m_uiout
->field_string ("func-name", name
.c_str (),
248 function_name_style
.style ());
249 /* For negative offsets, avoid displaying them as +-N; the sign of
250 the offset takes the place of the "+" here. */
253 m_uiout
->field_signed ("offset", offset
);
254 m_uiout
->text (">:\t");
257 m_uiout
->text (":\t");
261 if (flags
& DISASSEMBLY_RAW_INSN
)
265 const char *spacer
= "";
267 /* Build the opcodes using a temporary stream so we can
268 write them out in a single go for the MI. */
269 m_opcode_stb
.clear ();
271 size
= m_di
.print_insn (pc
);
274 for (;pc
< end_pc
; ++pc
)
276 read_code (pc
, &data
, 1);
277 m_opcode_stb
.printf ("%s%02x", spacer
, (unsigned) data
);
281 m_uiout
->field_stream ("opcodes", m_opcode_stb
);
282 m_uiout
->text ("\t");
285 size
= m_di
.print_insn (pc
);
287 m_uiout
->field_stream ("inst", m_insn_stb
);
289 m_uiout
->text ("\n");
295 dump_insns (struct gdbarch
*gdbarch
,
296 struct ui_out
*uiout
, CORE_ADDR low
, CORE_ADDR high
,
297 int how_many
, gdb_disassembly_flags flags
, CORE_ADDR
*end_pc
)
299 struct disasm_insn insn
;
300 int num_displayed
= 0;
302 memset (&insn
, 0, sizeof (insn
));
305 gdb_pretty_print_disassembler
disasm (gdbarch
, uiout
);
307 while (insn
.addr
< high
&& (how_many
< 0 || num_displayed
< how_many
))
311 size
= disasm
.pretty_print_insn (&insn
, flags
);
318 /* Allow user to bail out with ^C. */
325 return num_displayed
;
328 /* The idea here is to present a source-O-centric view of a
329 function to the user. This means that things are presented
330 in source order, with (possibly) out of order assembly
331 immediately following.
333 N.B. This view is deprecated. */
336 do_mixed_source_and_assembly_deprecated
337 (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
338 struct symtab
*symtab
,
339 CORE_ADDR low
, CORE_ADDR high
,
340 int how_many
, gdb_disassembly_flags flags
)
344 struct linetable_entry
*le
;
345 struct deprecated_dis_line_entry
*mle
;
346 struct symtab_and_line sal
;
348 int out_of_order
= 0;
350 int num_displayed
= 0;
351 print_source_lines_flags psl_flags
= 0;
353 gdb_assert (symtab
!= NULL
&& SYMTAB_LINETABLE (symtab
) != NULL
);
355 nlines
= SYMTAB_LINETABLE (symtab
)->nitems
;
356 le
= SYMTAB_LINETABLE (symtab
)->item
;
358 if (flags
& DISASSEMBLY_FILENAME
)
359 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
361 mle
= (struct deprecated_dis_line_entry
*)
362 alloca (nlines
* sizeof (struct deprecated_dis_line_entry
));
364 /* Copy linetable entries for this function into our data
365 structure, creating end_pc's and setting out_of_order as
368 /* First, skip all the preceding functions. */
370 for (i
= 0; i
< nlines
- 1 && le
[i
].pc
< low
; i
++);
372 /* Now, copy all entries before the end of this function. */
374 for (; i
< nlines
- 1 && le
[i
].pc
< high
; i
++)
376 if (le
[i
].line
== le
[i
+ 1].line
&& le
[i
].pc
== le
[i
+ 1].pc
)
377 continue; /* Ignore duplicates. */
379 /* Skip any end-of-function markers. */
383 mle
[newlines
].line
= le
[i
].line
;
384 if (le
[i
].line
> le
[i
+ 1].line
)
386 mle
[newlines
].start_pc
= le
[i
].pc
;
387 mle
[newlines
].end_pc
= le
[i
+ 1].pc
;
391 /* If we're on the last line, and it's part of the function,
392 then we need to get the end pc in a special way. */
394 if (i
== nlines
- 1 && le
[i
].pc
< high
)
396 mle
[newlines
].line
= le
[i
].line
;
397 mle
[newlines
].start_pc
= le
[i
].pc
;
398 sal
= find_pc_line (le
[i
].pc
, 0);
399 mle
[newlines
].end_pc
= sal
.end
;
403 /* Now, sort mle by line #s (and, then by addresses within lines). */
406 std::sort (mle
, mle
+ newlines
, line_is_less_than
);
408 /* Now, for each line entry, emit the specified lines (unless
409 they have been emitted before), followed by the assembly code
412 ui_out_emit_list
asm_insns_list (uiout
, "asm_insns");
414 gdb::optional
<ui_out_emit_tuple
> outer_tuple_emitter
;
415 gdb::optional
<ui_out_emit_list
> inner_list_emitter
;
417 for (i
= 0; i
< newlines
; i
++)
419 /* Print out everything from next_line to the current line. */
420 if (mle
[i
].line
>= next_line
)
424 /* Just one line to print. */
425 if (next_line
== mle
[i
].line
)
427 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
428 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, psl_flags
);
432 /* Several source lines w/o asm instructions associated. */
433 for (; next_line
< mle
[i
].line
; next_line
++)
435 ui_out_emit_tuple
tuple_emitter (uiout
,
437 print_source_lines (symtab
, next_line
, next_line
+ 1,
439 ui_out_emit_list
temp_list_emitter (uiout
,
442 /* Print the last line and leave list open for
443 asm instructions to be added. */
444 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
445 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, psl_flags
);
450 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
451 print_source_lines (symtab
, mle
[i
].line
, mle
[i
].line
+ 1, psl_flags
);
454 next_line
= mle
[i
].line
+ 1;
455 inner_list_emitter
.emplace (uiout
, "line_asm_insn");
458 num_displayed
+= dump_insns (gdbarch
, uiout
,
459 mle
[i
].start_pc
, mle
[i
].end_pc
,
460 how_many
, flags
, NULL
);
462 /* When we've reached the end of the mle array, or we've seen the last
463 assembly range for this source line, close out the list/tuple. */
464 if (i
== (newlines
- 1) || mle
[i
+ 1].line
> mle
[i
].line
)
466 inner_list_emitter
.reset ();
467 outer_tuple_emitter
.reset ();
470 if (how_many
>= 0 && num_displayed
>= how_many
)
475 /* The idea here is to present a source-O-centric view of a
476 function to the user. This means that things are presented
477 in source order, with (possibly) out of order assembly
478 immediately following. */
481 do_mixed_source_and_assembly (struct gdbarch
*gdbarch
,
482 struct ui_out
*uiout
,
483 struct symtab
*main_symtab
,
484 CORE_ADDR low
, CORE_ADDR high
,
485 int how_many
, gdb_disassembly_flags flags
)
487 const struct linetable_entry
*le
, *first_le
;
489 int num_displayed
= 0;
490 print_source_lines_flags psl_flags
= 0;
492 struct symtab
*last_symtab
;
495 gdb_assert (main_symtab
!= NULL
&& SYMTAB_LINETABLE (main_symtab
) != NULL
);
497 /* First pass: collect the list of all source files and lines.
498 We do this so that we can only print lines containing code once.
499 We try to print the source text leading up to the next instruction,
500 but if that text is for code that will be disassembled later, then
501 we'll want to defer printing it until later with its associated code. */
503 htab_up
dis_line_table (allocate_dis_line_table ());
507 /* The prologue may be empty, but there may still be a line number entry
508 for the opening brace which is distinct from the first line of code.
509 If the prologue has been eliminated find_pc_line may return the source
510 line after the opening brace. We still want to print this opening brace.
511 first_le is used to implement this. */
513 nlines
= SYMTAB_LINETABLE (main_symtab
)->nitems
;
514 le
= SYMTAB_LINETABLE (main_symtab
)->item
;
517 /* Skip all the preceding functions. */
518 for (i
= 0; i
< nlines
&& le
[i
].pc
< low
; i
++)
521 if (i
< nlines
&& le
[i
].pc
< high
)
524 /* Add lines for every pc value. */
527 struct symtab_and_line sal
;
530 sal
= find_pc_line (pc
, 0);
531 length
= gdb_insn_length (gdbarch
, pc
);
534 if (sal
.symtab
!= NULL
)
535 add_dis_line_entry (dis_line_table
.get (), sal
.symtab
, sal
.line
);
538 /* Second pass: print the disassembly.
540 Output format, from an MI perspective:
541 The result is a ui_out list, field name "asm_insns", where elements have
542 name "src_and_asm_line".
543 Each element is a tuple of source line specs (field names line, file,
544 fullname), and field "line_asm_insn" which contains the disassembly.
545 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
548 CLI output works on top of this because MI ignores ui_out_text output,
549 which is where we put file name and source line contents output.
553 Handles the outer "asm_insns" list.
555 The tuples for each group of consecutive disassemblies.
557 List of consecutive source lines or disassembled insns. */
559 if (flags
& DISASSEMBLY_FILENAME
)
560 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
562 ui_out_emit_list
asm_insns_emitter (uiout
, "asm_insns");
564 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
565 gdb::optional
<ui_out_emit_list
> list_emitter
;
573 struct symtab_and_line sal
;
575 int start_preceding_line_to_display
= 0;
576 int end_preceding_line_to_display
= 0;
577 int new_source_line
= 0;
579 sal
= find_pc_line (pc
, 0);
581 if (sal
.symtab
!= last_symtab
)
583 /* New source file. */
586 /* If this is the first line of output, check for any preceding
590 && first_le
->line
< sal
.line
)
592 start_preceding_line_to_display
= first_le
->line
;
593 end_preceding_line_to_display
= sal
.line
;
598 /* Same source file as last time. */
599 if (sal
.symtab
!= NULL
)
601 if (sal
.line
> last_line
+ 1 && last_line
!= 0)
605 /* Several preceding source lines. Print the trailing ones
606 not associated with code that we'll print later. */
607 for (l
= sal
.line
- 1; l
> last_line
; --l
)
609 if (line_has_code_p (dis_line_table
.get (),
613 if (l
< sal
.line
- 1)
615 start_preceding_line_to_display
= l
+ 1;
616 end_preceding_line_to_display
= sal
.line
;
619 if (sal
.line
!= last_line
)
623 /* Same source line as last time. This can happen, depending
624 on the debug info. */
631 /* Skip the newline if this is the first instruction. */
634 if (tuple_emitter
.has_value ())
636 gdb_assert (list_emitter
.has_value ());
637 list_emitter
.reset ();
638 tuple_emitter
.reset ();
640 if (sal
.symtab
!= last_symtab
641 && !(flags
& DISASSEMBLY_FILENAME
))
643 /* Remember MI ignores ui_out_text.
644 We don't have to do anything here for MI because MI
645 output includes the source specs for each line. */
646 if (sal
.symtab
!= NULL
)
648 uiout
->text (symtab_to_filename_for_display (sal
.symtab
));
651 uiout
->text ("unknown");
654 if (start_preceding_line_to_display
> 0)
656 /* Several source lines w/o asm instructions associated.
657 We need to preserve the structure of the output, so output
658 a bunch of line tuples with no asm entries. */
661 gdb_assert (sal
.symtab
!= NULL
);
662 for (l
= start_preceding_line_to_display
;
663 l
< end_preceding_line_to_display
;
666 ui_out_emit_tuple
line_tuple_emitter (uiout
,
668 print_source_lines (sal
.symtab
, l
, l
+ 1, psl_flags
);
669 ui_out_emit_list
chain_line_emitter (uiout
, "line_asm_insn");
672 tuple_emitter
.emplace (uiout
, "src_and_asm_line");
673 if (sal
.symtab
!= NULL
)
674 print_source_lines (sal
.symtab
, sal
.line
, sal
.line
+ 1, psl_flags
);
676 uiout
->text (_("--- no source info for this pc ---\n"));
677 list_emitter
.emplace (uiout
, "line_asm_insn");
681 /* Here we're appending instructions to an existing line.
682 By construction the very first insn will have a symtab
683 and follow the new_source_line path above. */
684 gdb_assert (tuple_emitter
.has_value ());
685 gdb_assert (list_emitter
.has_value ());
689 end_pc
= std::min (sal
.end
, high
);
692 num_displayed
+= dump_insns (gdbarch
, uiout
, pc
, end_pc
,
693 how_many
, flags
, &end_pc
);
696 if (how_many
>= 0 && num_displayed
>= how_many
)
699 last_symtab
= sal
.symtab
;
700 last_line
= sal
.line
;
705 do_assembly_only (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
706 CORE_ADDR low
, CORE_ADDR high
,
707 int how_many
, gdb_disassembly_flags flags
)
709 ui_out_emit_list
list_emitter (uiout
, "asm_insns");
711 dump_insns (gdbarch
, uiout
, low
, high
, how_many
, flags
, NULL
);
714 /* Initialize the disassemble info struct ready for the specified
717 static int ATTRIBUTE_PRINTF (2, 3)
718 fprintf_disasm (void *stream
, const char *format
, ...)
722 va_start (args
, format
);
723 vfprintf_filtered ((struct ui_file
*) stream
, format
, args
);
725 /* Something non -ve. */
729 /* Combine implicit and user disassembler options and return them
730 in a newly-created string. */
733 get_all_disassembler_options (struct gdbarch
*gdbarch
)
735 const char *implicit
= gdbarch_disassembler_options_implicit (gdbarch
);
736 const char *options
= get_disassembler_options (gdbarch
);
737 const char *comma
= ",";
739 if (implicit
== nullptr)
745 if (options
== nullptr)
751 return string_printf ("%s%s%s", implicit
, comma
, options
);
754 gdb_disassembler::gdb_disassembler (struct gdbarch
*gdbarch
,
755 struct ui_file
*file
,
756 di_read_memory_ftype read_memory_func
)
757 : m_gdbarch (gdbarch
),
760 init_disassemble_info (&m_di
, file
, fprintf_disasm
);
761 m_di
.flavour
= bfd_target_unknown_flavour
;
762 m_di
.memory_error_func
= dis_asm_memory_error
;
763 m_di
.print_address_func
= dis_asm_print_address
;
764 /* NOTE: cagney/2003-04-28: The original code, from the old Insight
765 disassembler had a local optimization here. By default it would
766 access the executable file, instead of the target memory (there
767 was a growing list of exceptions though). Unfortunately, the
768 heuristic was flawed. Commands like "disassemble &variable"
769 didn't work as they relied on the access going to the target.
770 Further, it has been superseeded by trust-read-only-sections
771 (although that should be superseeded by target_trust..._p()). */
772 m_di
.read_memory_func
= read_memory_func
;
773 m_di
.arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
774 m_di
.mach
= gdbarch_bfd_arch_info (gdbarch
)->mach
;
775 m_di
.endian
= gdbarch_byte_order (gdbarch
);
776 m_di
.endian_code
= gdbarch_byte_order_for_code (gdbarch
);
777 m_di
.application_data
= this;
778 m_disassembler_options_holder
= get_all_disassembler_options (gdbarch
);
779 if (!m_disassembler_options_holder
.empty ())
780 m_di
.disassembler_options
= m_disassembler_options_holder
.c_str ();
781 disassemble_init_for_target (&m_di
);
784 gdb_disassembler::~gdb_disassembler ()
786 disassemble_free_target (&m_di
);
790 gdb_disassembler::print_insn (CORE_ADDR memaddr
,
791 int *branch_delay_insns
)
795 int length
= gdbarch_print_insn (arch (), memaddr
, &m_di
);
798 memory_error (TARGET_XFER_E_IO
, m_err_memaddr
);
800 if (branch_delay_insns
!= NULL
)
802 if (m_di
.insn_info_valid
)
803 *branch_delay_insns
= m_di
.branch_delay_insns
;
805 *branch_delay_insns
= 0;
811 gdb_disassembly (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
812 gdb_disassembly_flags flags
, int how_many
,
813 CORE_ADDR low
, CORE_ADDR high
)
815 struct symtab
*symtab
;
818 /* Assume symtab is valid for whole PC range. */
819 symtab
= find_pc_line_symtab (low
);
821 if (symtab
!= NULL
&& SYMTAB_LINETABLE (symtab
) != NULL
)
822 nlines
= SYMTAB_LINETABLE (symtab
)->nitems
;
824 if (!(flags
& (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
826 do_assembly_only (gdbarch
, uiout
, low
, high
, how_many
, flags
);
828 else if (flags
& DISASSEMBLY_SOURCE
)
829 do_mixed_source_and_assembly (gdbarch
, uiout
, symtab
, low
, high
,
832 else if (flags
& DISASSEMBLY_SOURCE_DEPRECATED
)
833 do_mixed_source_and_assembly_deprecated (gdbarch
, uiout
, symtab
,
834 low
, high
, how_many
, flags
);
836 gdb_flush (gdb_stdout
);
839 /* Print the instruction at address MEMADDR in debugged memory,
840 on STREAM. Returns the length of the instruction, in bytes,
841 and, if requested, the number of branch delay slot instructions. */
844 gdb_print_insn (struct gdbarch
*gdbarch
, CORE_ADDR memaddr
,
845 struct ui_file
*stream
, int *branch_delay_insns
)
848 gdb_disassembler
di (gdbarch
, stream
);
850 return di
.print_insn (memaddr
, branch_delay_insns
);
853 /* Return the length in bytes of the instruction at address MEMADDR in
857 gdb_insn_length (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
859 return gdb_print_insn (gdbarch
, addr
, &null_stream
, NULL
);
862 /* fprintf-function for gdb_buffered_insn_length. This function is a
863 nop, we don't want to print anything, we just want to compute the
864 length of the insn. */
866 static int ATTRIBUTE_PRINTF (2, 3)
867 gdb_buffered_insn_length_fprintf (void *stream
, const char *format
, ...)
872 /* Initialize a struct disassemble_info for gdb_buffered_insn_length.
873 Upon return, *DISASSEMBLER_OPTIONS_HOLDER owns the string pointed
874 to by DI.DISASSEMBLER_OPTIONS. */
877 gdb_buffered_insn_length_init_dis (struct gdbarch
*gdbarch
,
878 struct disassemble_info
*di
,
879 const gdb_byte
*insn
, int max_len
,
881 std::string
*disassembler_options_holder
)
883 init_disassemble_info (di
, NULL
, gdb_buffered_insn_length_fprintf
);
885 /* init_disassemble_info installs buffer_read_memory, etc.
886 so we don't need to do that here.
887 The cast is necessary until disassemble_info is const-ified. */
888 di
->buffer
= (gdb_byte
*) insn
;
889 di
->buffer_length
= max_len
;
890 di
->buffer_vma
= addr
;
892 di
->arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
893 di
->mach
= gdbarch_bfd_arch_info (gdbarch
)->mach
;
894 di
->endian
= gdbarch_byte_order (gdbarch
);
895 di
->endian_code
= gdbarch_byte_order_for_code (gdbarch
);
897 *disassembler_options_holder
= get_all_disassembler_options (gdbarch
);
898 if (!disassembler_options_holder
->empty ())
899 di
->disassembler_options
= disassembler_options_holder
->c_str ();
900 disassemble_init_for_target (di
);
903 /* Return the length in bytes of INSN. MAX_LEN is the size of the
904 buffer containing INSN. */
907 gdb_buffered_insn_length (struct gdbarch
*gdbarch
,
908 const gdb_byte
*insn
, int max_len
, CORE_ADDR addr
)
910 struct disassemble_info di
;
911 std::string disassembler_options_holder
;
913 gdb_buffered_insn_length_init_dis (gdbarch
, &di
, insn
, max_len
, addr
,
914 &disassembler_options_holder
);
916 int result
= gdbarch_print_insn (gdbarch
, addr
, &di
);
917 disassemble_free_target (&di
);
922 get_disassembler_options (struct gdbarch
*gdbarch
)
924 char **disassembler_options
= gdbarch_disassembler_options (gdbarch
);
925 if (disassembler_options
== NULL
)
927 return *disassembler_options
;
931 set_disassembler_options (char *prospective_options
)
933 struct gdbarch
*gdbarch
= get_current_arch ();
934 char **disassembler_options
= gdbarch_disassembler_options (gdbarch
);
935 const disasm_options_and_args_t
*valid_options_and_args
;
936 const disasm_options_t
*valid_options
;
937 char *options
= remove_whitespace_and_extra_commas (prospective_options
);
940 /* Allow all architectures, even ones that do not support 'set disassembler',
941 to reset their disassembler options to NULL. */
944 if (disassembler_options
!= NULL
)
946 free (*disassembler_options
);
947 *disassembler_options
= NULL
;
952 valid_options_and_args
= gdbarch_valid_disassembler_options (gdbarch
);
953 if (valid_options_and_args
== NULL
)
955 fprintf_filtered (gdb_stderr
, _("\
956 'set disassembler-options ...' is not supported on this architecture.\n"));
960 valid_options
= &valid_options_and_args
->options
;
962 /* Verify we have valid disassembler options. */
963 FOR_EACH_DISASSEMBLER_OPTION (opt
, options
)
966 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
967 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
969 size_t len
= strlen (valid_options
->name
[i
]);
974 if (memcmp (opt
, valid_options
->name
[i
], len
) != 0)
977 for (j
= 0; valid_options
->arg
[i
]->values
[j
] != NULL
; j
++)
978 if (disassembler_options_cmp
979 (arg
, valid_options
->arg
[i
]->values
[j
]) == 0)
987 else if (disassembler_options_cmp (opt
, valid_options
->name
[i
]) == 0)
989 if (valid_options
->name
[i
] == NULL
)
991 fprintf_filtered (gdb_stderr
,
992 _("Invalid disassembler option value: '%s'.\n"),
998 free (*disassembler_options
);
999 *disassembler_options
= xstrdup (options
);
1003 set_disassembler_options_sfunc (const char *args
, int from_tty
,
1004 struct cmd_list_element
*c
)
1006 set_disassembler_options (prospective_options
);
1010 show_disassembler_options_sfunc (struct ui_file
*file
, int from_tty
,
1011 struct cmd_list_element
*c
, const char *value
)
1013 struct gdbarch
*gdbarch
= get_current_arch ();
1014 const disasm_options_and_args_t
*valid_options_and_args
;
1015 const disasm_option_arg_t
*valid_args
;
1016 const disasm_options_t
*valid_options
;
1018 const char *options
= get_disassembler_options (gdbarch
);
1019 if (options
== NULL
)
1022 fprintf_filtered (file
, _("The current disassembler options are '%s'\n\n"),
1025 valid_options_and_args
= gdbarch_valid_disassembler_options (gdbarch
);
1027 if (valid_options_and_args
== NULL
)
1029 fputs_filtered (_("There are no disassembler options available "
1030 "for this architecture.\n"),
1035 valid_options
= &valid_options_and_args
->options
;
1037 fprintf_filtered (file
, _("\
1038 The following disassembler options are supported for use with the\n\
1039 'set disassembler-options OPTION [,OPTION]...' command:\n"));
1041 if (valid_options
->description
!= NULL
)
1043 size_t i
, max_len
= 0;
1045 fprintf_filtered (file
, "\n");
1047 /* Compute the length of the longest option name. */
1048 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
1050 size_t len
= strlen (valid_options
->name
[i
]);
1052 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1053 len
+= strlen (valid_options
->arg
[i
]->name
);
1058 for (i
= 0, max_len
++; valid_options
->name
[i
] != NULL
; i
++)
1060 fprintf_filtered (file
, " %s", valid_options
->name
[i
]);
1061 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1062 fprintf_filtered (file
, "%s", valid_options
->arg
[i
]->name
);
1063 if (valid_options
->description
[i
] != NULL
)
1065 size_t len
= strlen (valid_options
->name
[i
]);
1067 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1068 len
+= strlen (valid_options
->arg
[i
]->name
);
1069 fprintf_filtered (file
, "%*c %s", (int) (max_len
- len
), ' ',
1070 valid_options
->description
[i
]);
1072 fprintf_filtered (file
, "\n");
1078 fprintf_filtered (file
, " ");
1079 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
1081 fprintf_filtered (file
, "%s", valid_options
->name
[i
]);
1082 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1083 fprintf_filtered (file
, "%s", valid_options
->arg
[i
]->name
);
1084 if (valid_options
->name
[i
+ 1] != NULL
)
1085 fprintf_filtered (file
, ", ");
1088 fprintf_filtered (file
, "\n");
1091 valid_args
= valid_options_and_args
->args
;
1092 if (valid_args
!= NULL
)
1096 for (i
= 0; valid_args
[i
].name
!= NULL
; i
++)
1098 fprintf_filtered (file
, _("\n\
1099 For the options above, the following values are supported for \"%s\":\n "),
1100 valid_args
[i
].name
);
1101 for (j
= 0; valid_args
[i
].values
[j
] != NULL
; j
++)
1103 fprintf_filtered (file
, " %s", valid_args
[i
].values
[j
]);
1106 fprintf_filtered (file
, "\n");
1111 /* A completion function for "set disassembler". */
1114 disassembler_options_completer (struct cmd_list_element
*ignore
,
1115 completion_tracker
&tracker
,
1116 const char *text
, const char *word
)
1118 struct gdbarch
*gdbarch
= get_current_arch ();
1119 const disasm_options_and_args_t
*opts_and_args
1120 = gdbarch_valid_disassembler_options (gdbarch
);
1122 if (opts_and_args
!= NULL
)
1124 const disasm_options_t
*opts
= &opts_and_args
->options
;
1126 /* Only attempt to complete on the last option text. */
1127 const char *separator
= strrchr (text
, ',');
1128 if (separator
!= NULL
)
1129 text
= separator
+ 1;
1130 text
= skip_spaces (text
);
1131 complete_on_enum (tracker
, opts
->name
, text
, word
);
1136 /* Initialization code. */
1138 void _initialize_disasm ();
1140 _initialize_disasm ()
1142 struct cmd_list_element
*cmd
;
1144 /* Add the command that controls the disassembler options. */
1145 cmd
= add_setshow_string_noescape_cmd ("disassembler-options", no_class
,
1146 &prospective_options
, _("\
1147 Set the disassembler options.\n\
1148 Usage: set disassembler-options OPTION [,OPTION]...\n\n\
1149 See: 'show disassembler-options' for valid option values."), _("\
1150 Show the disassembler options."), NULL
,
1151 set_disassembler_options_sfunc
,
1152 show_disassembler_options_sfunc
,
1153 &setlist
, &showlist
);
1154 set_cmd_completer (cmd
, disassembler_options_completer
);