1 /* Disassemble support for GDB.
3 Copyright (C) 2000-2024 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/>. */
20 #include "arch-utils.h"
21 #include "event-top.h"
27 #include "cli/cli-cmds.h"
30 #include "gdbsupport/gdb-safe-ctype.h"
34 #include "cli/cli-style.h"
38 /* Disassemble functions.
39 FIXME: We should get rid of all the duplicate code in gdb that does
40 the same thing: disassemble_command() and the gdbtk variation. */
42 /* This variable is used to hold the prospective disassembler_options value
43 which is set by the "set disassembler_options" command. */
44 static std::string prospective_options
;
46 /* When this is true we will try to use libopcodes to provide styling to
47 the disassembler output. */
49 static bool use_libopcodes_styling
= true;
51 /* To support the set_use_libopcodes_styling function we have a second
52 variable which is connected to the actual set/show option. */
54 static bool use_libopcodes_styling_option
= use_libopcodes_styling
;
56 /* The "maint show libopcodes-styling enabled" command. */
59 show_use_libopcodes_styling (struct ui_file
*file
, int from_tty
,
60 struct cmd_list_element
*c
,
63 gdbarch
*arch
= current_inferior ()->arch ();
64 gdb_non_printing_memory_disassembler
dis (arch
);
65 bool supported
= dis
.disasm_info ()->created_styled_output
;
67 if (supported
|| !use_libopcodes_styling
)
68 gdb_printf (file
, _("Use of libopcodes styling support is \"%s\".\n"),
72 /* Use of libopcodes styling is not supported, and the user has this
74 gdb_printf (file
, _("Use of libopcodes styling support is \"off\""
75 " (not supported on architecture \"%s\")\n"),
76 gdbarch_bfd_arch_info (arch
)->printable_name
);
80 /* The "maint set libopcodes-styling enabled" command. */
83 set_use_libopcodes_styling (const char *args
, int from_tty
,
84 struct cmd_list_element
*c
)
86 gdbarch
*arch
= current_inferior ()->arch ();
87 gdb_non_printing_memory_disassembler
dis (arch
);
88 bool supported
= dis
.disasm_info ()->created_styled_output
;
90 /* If the current architecture doesn't support libopcodes styling then we
91 give an error here, but leave the underlying setting enabled. This
92 means that if the user switches to an architecture that does support
93 libopcodes styling the setting will be enabled. */
95 if (use_libopcodes_styling_option
&& !supported
)
97 use_libopcodes_styling_option
= use_libopcodes_styling
;
98 error (_("Use of libopcodes styling not supported on architecture \"%s\"."),
99 gdbarch_bfd_arch_info (arch
)->printable_name
);
102 use_libopcodes_styling
= use_libopcodes_styling_option
;
105 /* This structure is used to store line number information for the
106 deprecated /m option.
107 We need a different sort of line table from the normal one cuz we can't
108 depend upon implicit line-end pc's for lines to do the
109 reordering in this function. */
111 struct deprecated_dis_line_entry
118 /* This Structure is used to store line number information.
119 We need a different sort of line table from the normal one cuz we can't
120 depend upon implicit line-end pc's for lines to do the
121 reordering in this function. */
123 struct dis_line_entry
125 struct symtab
*symtab
;
129 /* Hash function for dis_line_entry. */
132 hash_dis_line_entry (const void *item
)
134 const struct dis_line_entry
*dle
= (const struct dis_line_entry
*) item
;
136 return htab_hash_pointer (dle
->symtab
) + dle
->line
;
139 /* Equal function for dis_line_entry. */
142 eq_dis_line_entry (const void *item_lhs
, const void *item_rhs
)
144 const struct dis_line_entry
*lhs
= (const struct dis_line_entry
*) item_lhs
;
145 const struct dis_line_entry
*rhs
= (const struct dis_line_entry
*) item_rhs
;
147 return (lhs
->symtab
== rhs
->symtab
148 && lhs
->line
== rhs
->line
);
151 /* Create the table to manage lines for mixed source/disassembly. */
154 allocate_dis_line_table (void)
156 return htab_create_alloc (41,
157 hash_dis_line_entry
, eq_dis_line_entry
,
158 xfree
, xcalloc
, xfree
);
161 /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
164 add_dis_line_entry (htab_t table
, struct symtab
*symtab
, int line
)
167 struct dis_line_entry dle
, *dlep
;
171 slot
= htab_find_slot (table
, &dle
, INSERT
);
174 dlep
= XNEW (struct dis_line_entry
);
175 dlep
->symtab
= symtab
;
181 /* Return non-zero if SYMTAB, LINE are in TABLE. */
184 line_has_code_p (htab_t table
, struct symtab
*symtab
, int line
)
186 struct dis_line_entry dle
;
190 return htab_find (table
, &dle
) != NULL
;
193 /* Wrapper of target_read_code. */
196 gdb_disassembler_memory_reader::dis_asm_read_memory
197 (bfd_vma memaddr
, gdb_byte
*myaddr
, unsigned int len
,
198 struct disassemble_info
*info
) noexcept
200 auto res
= catch_exceptions
<int, -1> ([&]
202 return target_read_code (memaddr
, myaddr
, len
);
208 /* Wrapper of memory_error. */
211 gdb_disassembler::dis_asm_memory_error
212 (int err
, bfd_vma memaddr
, struct disassemble_info
*info
) noexcept
214 gdb_disassembler
*self
215 = static_cast<gdb_disassembler
*>(info
->application_data
);
217 self
->m_err_memaddr
.emplace (memaddr
);
220 /* Wrapper of print_address. */
223 gdb_disassembler::dis_asm_print_address
224 (bfd_vma addr
, struct disassemble_info
*info
) noexcept
226 gdb_disassembler
*self
227 = static_cast<gdb_disassembler
*>(info
->application_data
);
229 if (self
->in_comment_p ())
231 /* Calling 'print_address' might add styling to the output (based on
232 the properties of the stream we're writing too). This is usually
233 fine, but if we are in an assembler comment then we'd prefer to
234 have the comment style, rather than the default address style.
236 Print the address into a temporary buffer which doesn't support
237 styling, then reprint this unstyled address with the default text
240 As we are inside a comment right now, the standard print routine
241 will ensure that the comment is printed to the user with a
242 suitable comment style. */
244 print_address (self
->arch (), addr
, &tmp
);
245 self
->fprintf_styled_func (self
, dis_style_text
, "%s", tmp
.c_str ());
248 print_address (self
->arch (), addr
, self
->stream ());
254 gdb_printing_disassembler::stream_from_gdb_disassemble_info (void *dis_info
)
256 gdb_disassemble_info
*di
= (gdb_disassemble_info
*) dis_info
;
257 gdb_printing_disassembler
*dis
258 = gdb::checked_static_cast
<gdb_printing_disassembler
*> (di
);
259 ui_file
*stream
= dis
->stream ();
260 gdb_assert (stream
!= nullptr);
264 /* Format disassembler output to STREAM. */
267 gdb_printing_disassembler::fprintf_func (void *dis_info
,
268 const char *format
, ...) noexcept
270 ui_file
*stream
= stream_from_gdb_disassemble_info (dis_info
);
273 va_start (args
, format
);
274 gdb_vprintf (stream
, format
, args
);
277 /* Something non -ve. */
284 gdb_printing_disassembler::fprintf_styled_func
285 (void *dis_info
, enum disassembler_style style
,
286 const char *format
, ...) noexcept
288 ui_file
*stream
= stream_from_gdb_disassemble_info (dis_info
);
289 gdb_printing_disassembler
*dis
= (gdb_printing_disassembler
*) dis_info
;
292 va_start (args
, format
);
293 std::string content
= string_vprintf (format
, args
);
296 /* Once in a comment then everything should be styled as a comment. */
297 if (style
== dis_style_comment_start
)
298 dis
->set_in_comment (true);
299 if (dis
->in_comment_p ())
300 style
= dis_style_comment_start
;
302 /* Now print the content with the correct style. */
303 const char *txt
= content
.c_str ();
306 case dis_style_mnemonic
:
307 case dis_style_sub_mnemonic
:
308 case dis_style_assembler_directive
:
309 fputs_styled (txt
, disasm_mnemonic_style
.style (), stream
);
312 case dis_style_register
:
313 fputs_styled (txt
, disasm_register_style
.style (), stream
);
316 case dis_style_immediate
:
317 case dis_style_address_offset
:
318 fputs_styled (txt
, disasm_immediate_style
.style (), stream
);
321 case dis_style_address
:
322 fputs_styled (txt
, address_style
.style (), stream
);
325 case dis_style_symbol
:
326 fputs_styled (txt
, function_name_style
.style (), stream
);
329 case dis_style_comment_start
:
330 fputs_styled (txt
, disasm_comment_style
.style (), stream
);
334 gdb_puts (txt
, stream
);
338 /* Something non -ve. */
343 line_is_less_than (const deprecated_dis_line_entry
&mle1
,
344 const deprecated_dis_line_entry
&mle2
)
348 /* End of sequence markers have a line number of 0 but don't want to
349 be sorted to the head of the list, instead sort by PC. */
350 if (mle1
.line
== 0 || mle2
.line
== 0)
352 if (mle1
.start_pc
!= mle2
.start_pc
)
353 val
= mle1
.start_pc
< mle2
.start_pc
;
355 val
= mle1
.line
< mle2
.line
;
359 if (mle1
.line
!= mle2
.line
)
360 val
= mle1
.line
< mle2
.line
;
362 val
= mle1
.start_pc
< mle2
.start_pc
;
370 gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn
*insn
,
371 gdb_disassembly_flags flags
)
373 /* parts of the symbolic representation of the address */
379 struct gdbarch
*gdbarch
= arch ();
382 ui_out_emit_tuple
tuple_emitter (m_uiout
, NULL
);
385 if (insn
->number
!= 0)
387 m_uiout
->field_unsigned ("insn-number", insn
->number
);
388 m_uiout
->text ("\t");
391 if ((flags
& DISASSEMBLY_SPECULATIVE
) != 0)
393 if (insn
->is_speculative
)
395 m_uiout
->field_string ("is-speculative", "?");
397 /* The speculative execution indication overwrites the first
398 character of the PC prefix.
399 We assume a PC prefix length of 3 characters. */
400 if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
401 m_uiout
->text (pc_prefix (pc
) + 1);
405 else if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
406 m_uiout
->text (pc_prefix (pc
));
410 else if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
411 m_uiout
->text (pc_prefix (pc
));
412 m_uiout
->field_core_addr ("address", gdbarch
, pc
);
414 std::string name
, filename
;
415 bool omit_fname
= ((flags
& DISASSEMBLY_OMIT_FNAME
) != 0);
416 if (!build_address_symbolic (gdbarch
, pc
, false, omit_fname
, &name
,
417 &offset
, &filename
, &line
, &unmapped
))
419 /* We don't care now about line, filename and unmapped. But we might in
421 m_uiout
->text (" <");
423 m_uiout
->field_string ("func-name", name
,
424 function_name_style
.style ());
425 /* For negative offsets, avoid displaying them as +-N; the sign of
426 the offset takes the place of the "+" here. */
429 m_uiout
->field_signed ("offset", offset
);
430 m_uiout
->text (">:\t");
433 m_uiout
->text (":\t");
435 /* Clear the buffer into which we will disassemble the instruction. */
438 /* A helper function to write the M_INSN_STB buffer, followed by a
439 newline. This can be called in a couple of situations. */
440 auto write_out_insn_buffer
= [&] ()
442 m_uiout
->field_stream ("inst", m_insn_stb
);
443 m_uiout
->text ("\n");
448 /* Now we can disassemble the instruction. If the disassembler
449 returns a negative value this indicates an error and is handled
450 within the print_insn call, resulting in an exception being
451 thrown. Returning zero makes no sense, as this indicates we
452 disassembled something successfully, but it was something of no
454 size
= m_di
.print_insn (pc
);
455 gdb_assert (size
> 0);
457 catch (const gdb_exception
&)
459 /* An exception was thrown while disassembling the instruction.
460 However, the disassembler might still have written something
461 out, so ensure that we flush the instruction buffer before
462 rethrowing the exception. We can't perform this write from an
463 object destructor as the write itself might throw an exception
464 if the pager kicks in, and the user selects quit. */
465 write_out_insn_buffer ();
469 if ((flags
& (DISASSEMBLY_RAW_INSN
| DISASSEMBLY_RAW_BYTES
)) != 0)
471 /* Build the opcodes using a temporary stream so we can
472 write them out in a single go for the MI. */
473 m_opcode_stb
.clear ();
475 /* Read the instruction opcode data. */
476 m_opcode_data
.resize (size
);
477 read_code (pc
, m_opcode_data
.data (), size
);
479 /* The disassembler provides information about the best way to
480 display the instruction bytes to the user. We provide some sane
481 defaults in case the disassembler gets it wrong. */
482 const struct disassemble_info
*di
= m_di
.disasm_info ();
483 int bytes_per_line
= std::max (di
->bytes_per_line
, size
);
484 int bytes_per_chunk
= std::max (di
->bytes_per_chunk
, 1);
486 /* If the user has requested the instruction bytes be displayed
487 byte at a time, then handle that here. Also, if the instruction
488 is not a multiple of the chunk size (which probably indicates a
489 disassembler problem) then avoid that causing display problems
490 by switching to byte at a time mode. */
491 if ((flags
& DISASSEMBLY_RAW_BYTES
) != 0
492 || (size
% bytes_per_chunk
) != 0)
495 /* Print the instruction opcodes bytes, grouped into chunks. */
496 for (int i
= 0; i
< size
; i
+= bytes_per_chunk
)
499 m_opcode_stb
.puts (" ");
501 if (di
->display_endian
== BFD_ENDIAN_LITTLE
)
503 for (int k
= bytes_per_chunk
; k
-- != 0; )
504 m_opcode_stb
.printf ("%02x", (unsigned) m_opcode_data
[i
+ k
]);
508 for (int k
= 0; k
< bytes_per_chunk
; k
++)
509 m_opcode_stb
.printf ("%02x", (unsigned) m_opcode_data
[i
+ k
]);
513 /* Calculate required padding. */
515 for (int i
= size
; i
< bytes_per_line
; i
+= bytes_per_chunk
)
519 nspaces
+= bytes_per_chunk
* 2;
522 m_uiout
->field_stream ("opcodes", m_opcode_stb
);
523 m_uiout
->spaces (nspaces
);
524 m_uiout
->text ("\t");
527 /* Disassembly was a success, write out the instruction buffer. */
528 write_out_insn_buffer ();
535 dump_insns (struct gdbarch
*gdbarch
,
536 struct ui_out
*uiout
, CORE_ADDR low
, CORE_ADDR high
,
537 int how_many
, gdb_disassembly_flags flags
, CORE_ADDR
*end_pc
)
539 struct disasm_insn insn
;
540 int num_displayed
= 0;
542 memset (&insn
, 0, sizeof (insn
));
545 gdb_pretty_print_disassembler
disasm (gdbarch
, uiout
);
547 while (insn
.addr
< high
&& (how_many
< 0 || num_displayed
< how_many
))
551 size
= disasm
.pretty_print_insn (&insn
, flags
);
558 /* Allow user to bail out with ^C. */
565 return num_displayed
;
568 /* The idea here is to present a source-O-centric view of a
569 function to the user. This means that things are presented
570 in source order, with (possibly) out of order assembly
571 immediately following.
573 N.B. This view is deprecated. */
576 do_mixed_source_and_assembly_deprecated
577 (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
578 struct symtab
*symtab
,
579 CORE_ADDR low
, CORE_ADDR high
,
580 int how_many
, gdb_disassembly_flags flags
)
584 const struct linetable_entry
*le
;
585 struct deprecated_dis_line_entry
*mle
;
586 struct symtab_and_line sal
;
588 int out_of_order
= 0;
590 int num_displayed
= 0;
591 print_source_lines_flags psl_flags
= 0;
593 gdb_assert (symtab
!= nullptr && symtab
->linetable () != nullptr);
595 nlines
= symtab
->linetable ()->nitems
;
596 le
= symtab
->linetable ()->item
;
598 if (flags
& DISASSEMBLY_FILENAME
)
599 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
601 mle
= (struct deprecated_dis_line_entry
*)
602 alloca (nlines
* sizeof (struct deprecated_dis_line_entry
));
604 struct objfile
*objfile
= symtab
->compunit ()->objfile ();
606 unrelocated_addr unrel_low
607 = unrelocated_addr (low
- objfile
->text_section_offset ());
608 unrelocated_addr unrel_high
609 = unrelocated_addr (high
- objfile
->text_section_offset ());
611 /* Copy linetable entries for this function into our data
612 structure, creating end_pc's and setting out_of_order as
615 /* First, skip all the preceding functions. */
617 for (i
= 0; i
< nlines
- 1 && le
[i
].unrelocated_pc () < unrel_low
; i
++);
619 /* Now, copy all entries before the end of this function. */
621 for (; i
< nlines
- 1 && le
[i
].unrelocated_pc () < unrel_high
; i
++)
623 if (le
[i
] == le
[i
+ 1])
624 continue; /* Ignore duplicates. */
626 /* Skip any end-of-function markers. */
630 mle
[newlines
].line
= le
[i
].line
;
631 if (le
[i
].line
> le
[i
+ 1].line
)
633 mle
[newlines
].start_pc
= le
[i
].pc (objfile
);
634 mle
[newlines
].end_pc
= le
[i
+ 1].pc (objfile
);
638 /* If we're on the last line, and it's part of the function,
639 then we need to get the end pc in a special way. */
641 if (i
== nlines
- 1 && le
[i
].unrelocated_pc () < unrel_high
)
643 mle
[newlines
].line
= le
[i
].line
;
644 mle
[newlines
].start_pc
= le
[i
].pc (objfile
);
645 sal
= find_pc_line (le
[i
].pc (objfile
), 0);
646 mle
[newlines
].end_pc
= sal
.end
;
650 /* Now, sort mle by line #s (and, then by addresses within lines). */
653 std::sort (mle
, mle
+ newlines
, line_is_less_than
);
655 /* Now, for each line entry, emit the specified lines (unless
656 they have been emitted before), followed by the assembly code
659 ui_out_emit_list
asm_insns_list (uiout
, "asm_insns");
661 std::optional
<ui_out_emit_tuple
> outer_tuple_emitter
;
662 std::optional
<ui_out_emit_list
> inner_list_emitter
;
664 for (i
= 0; i
< newlines
; i
++)
666 /* Print out everything from next_line to the current line. */
667 if (mle
[i
].line
>= next_line
)
671 /* Just one line to print. */
672 if (next_line
== mle
[i
].line
)
674 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
675 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, psl_flags
);
679 /* Several source lines w/o asm instructions associated. */
680 for (; next_line
< mle
[i
].line
; next_line
++)
682 ui_out_emit_tuple
tuple_emitter (uiout
,
684 print_source_lines (symtab
, next_line
, next_line
+ 1,
686 ui_out_emit_list
temp_list_emitter (uiout
,
689 /* Print the last line and leave list open for
690 asm instructions to be added. */
691 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
692 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, psl_flags
);
697 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
698 print_source_lines (symtab
, mle
[i
].line
, mle
[i
].line
+ 1, psl_flags
);
701 next_line
= mle
[i
].line
+ 1;
702 inner_list_emitter
.emplace (uiout
, "line_asm_insn");
705 num_displayed
+= dump_insns (gdbarch
, uiout
,
706 mle
[i
].start_pc
, mle
[i
].end_pc
,
707 how_many
, flags
, NULL
);
709 /* When we've reached the end of the mle array, or we've seen the last
710 assembly range for this source line, close out the list/tuple. */
711 if (i
== (newlines
- 1) || mle
[i
+ 1].line
> mle
[i
].line
)
713 inner_list_emitter
.reset ();
714 outer_tuple_emitter
.reset ();
717 if (how_many
>= 0 && num_displayed
>= how_many
)
722 /* The idea here is to present a source-O-centric view of a
723 function to the user. This means that things are presented
724 in source order, with (possibly) out of order assembly
725 immediately following. */
728 do_mixed_source_and_assembly (struct gdbarch
*gdbarch
,
729 struct ui_out
*uiout
,
730 struct symtab
*main_symtab
,
731 CORE_ADDR low
, CORE_ADDR high
,
732 int how_many
, gdb_disassembly_flags flags
)
734 const struct linetable_entry
*le
, *first_le
;
736 int num_displayed
= 0;
737 print_source_lines_flags psl_flags
= 0;
739 struct symtab
*last_symtab
;
742 gdb_assert (main_symtab
!= NULL
&& main_symtab
->linetable () != NULL
);
744 /* First pass: collect the list of all source files and lines.
745 We do this so that we can only print lines containing code once.
746 We try to print the source text leading up to the next instruction,
747 but if that text is for code that will be disassembled later, then
748 we'll want to defer printing it until later with its associated code. */
750 htab_up
dis_line_table (allocate_dis_line_table ());
752 struct objfile
*objfile
= main_symtab
->compunit ()->objfile ();
754 unrelocated_addr unrel_low
755 = unrelocated_addr (low
- objfile
->text_section_offset ());
756 unrelocated_addr unrel_high
757 = unrelocated_addr (high
- objfile
->text_section_offset ());
761 /* The prologue may be empty, but there may still be a line number entry
762 for the opening brace which is distinct from the first line of code.
763 If the prologue has been eliminated find_pc_line may return the source
764 line after the opening brace. We still want to print this opening brace.
765 first_le is used to implement this. */
767 nlines
= main_symtab
->linetable ()->nitems
;
768 le
= main_symtab
->linetable ()->item
;
771 /* Skip all the preceding functions. */
772 for (i
= 0; i
< nlines
&& le
[i
].unrelocated_pc () < unrel_low
; i
++)
775 if (i
< nlines
&& le
[i
].unrelocated_pc () < unrel_high
)
778 /* Add lines for every pc value. */
781 struct symtab_and_line sal
;
784 sal
= find_pc_line (pc
, 0);
785 length
= gdb_insn_length (gdbarch
, pc
);
788 if (sal
.symtab
!= NULL
)
789 add_dis_line_entry (dis_line_table
.get (), sal
.symtab
, sal
.line
);
792 /* Second pass: print the disassembly.
794 Output format, from an MI perspective:
795 The result is a ui_out list, field name "asm_insns", where elements have
796 name "src_and_asm_line".
797 Each element is a tuple of source line specs (field names line, file,
798 fullname), and field "line_asm_insn" which contains the disassembly.
799 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
802 CLI output works on top of this because MI ignores ui_out_text output,
803 which is where we put file name and source line contents output.
807 Handles the outer "asm_insns" list.
809 The tuples for each group of consecutive disassemblies.
811 List of consecutive source lines or disassembled insns. */
813 if (flags
& DISASSEMBLY_FILENAME
)
814 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
816 ui_out_emit_list
asm_insns_emitter (uiout
, "asm_insns");
818 std::optional
<ui_out_emit_tuple
> tuple_emitter
;
819 std::optional
<ui_out_emit_list
> list_emitter
;
827 struct symtab_and_line sal
;
829 int start_preceding_line_to_display
= 0;
830 int end_preceding_line_to_display
= 0;
831 int new_source_line
= 0;
833 sal
= find_pc_line (pc
, 0);
835 if (sal
.symtab
!= last_symtab
)
837 /* New source file. */
840 /* If this is the first line of output, check for any preceding
844 && first_le
->line
< sal
.line
)
846 start_preceding_line_to_display
= first_le
->line
;
847 end_preceding_line_to_display
= sal
.line
;
852 /* Same source file as last time. */
853 if (sal
.symtab
!= NULL
)
855 if (sal
.line
> last_line
+ 1 && last_line
!= 0)
859 /* Several preceding source lines. Print the trailing ones
860 not associated with code that we'll print later. */
861 for (l
= sal
.line
- 1; l
> last_line
; --l
)
863 if (line_has_code_p (dis_line_table
.get (),
867 if (l
< sal
.line
- 1)
869 start_preceding_line_to_display
= l
+ 1;
870 end_preceding_line_to_display
= sal
.line
;
873 if (sal
.line
!= last_line
)
877 /* Same source line as last time. This can happen, depending
878 on the debug info. */
885 /* Skip the newline if this is the first instruction. */
888 if (tuple_emitter
.has_value ())
890 gdb_assert (list_emitter
.has_value ());
891 list_emitter
.reset ();
892 tuple_emitter
.reset ();
894 if (sal
.symtab
!= last_symtab
895 && !(flags
& DISASSEMBLY_FILENAME
))
897 /* Remember MI ignores ui_out_text.
898 We don't have to do anything here for MI because MI
899 output includes the source specs for each line. */
900 if (sal
.symtab
!= NULL
)
902 auto filename
= symtab_to_filename_for_display (sal
.symtab
);
903 uiout
->message ("%ps",
904 styled_string (file_name_style
.style (),
908 uiout
->text ("unknown");
911 if (start_preceding_line_to_display
> 0)
913 /* Several source lines w/o asm instructions associated.
914 We need to preserve the structure of the output, so output
915 a bunch of line tuples with no asm entries. */
918 gdb_assert (sal
.symtab
!= NULL
);
919 for (l
= start_preceding_line_to_display
;
920 l
< end_preceding_line_to_display
;
923 ui_out_emit_tuple
line_tuple_emitter (uiout
,
925 print_source_lines (sal
.symtab
, l
, l
+ 1, psl_flags
);
926 ui_out_emit_list
chain_line_emitter (uiout
, "line_asm_insn");
929 tuple_emitter
.emplace (uiout
, "src_and_asm_line");
930 if (sal
.symtab
!= NULL
)
931 print_source_lines (sal
.symtab
, sal
.line
, sal
.line
+ 1, psl_flags
);
933 uiout
->text (_("--- no source info for this pc ---\n"));
934 list_emitter
.emplace (uiout
, "line_asm_insn");
938 /* Here we're appending instructions to an existing line.
939 By construction the very first insn will have a symtab
940 and follow the new_source_line path above. */
941 gdb_assert (tuple_emitter
.has_value ());
942 gdb_assert (list_emitter
.has_value ());
946 end_pc
= std::min (sal
.end
, high
);
949 num_displayed
+= dump_insns (gdbarch
, uiout
, pc
, end_pc
,
950 how_many
, flags
, &end_pc
);
953 if (how_many
>= 0 && num_displayed
>= how_many
)
956 last_symtab
= sal
.symtab
;
957 last_line
= sal
.line
;
962 do_assembly_only (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
963 CORE_ADDR low
, CORE_ADDR high
,
964 int how_many
, gdb_disassembly_flags flags
)
966 ui_out_emit_list
list_emitter (uiout
, "asm_insns");
968 dump_insns (gdbarch
, uiout
, low
, high
, how_many
, flags
, NULL
);
971 /* Combine implicit and user disassembler options and return them
972 in a newly-created string. */
975 get_all_disassembler_options (struct gdbarch
*gdbarch
)
977 const char *implicit
= gdbarch_disassembler_options_implicit (gdbarch
);
978 const char *options
= get_disassembler_options (gdbarch
);
979 const char *comma
= ",";
981 if (implicit
== nullptr)
987 if (options
== nullptr)
993 return string_printf ("%s%s%s", implicit
, comma
, options
);
996 gdb_disassembler::gdb_disassembler (struct gdbarch
*gdbarch
,
997 struct ui_file
*file
,
998 read_memory_ftype func
)
999 : gdb_printing_disassembler (gdbarch
, &m_buffer
, func
,
1000 dis_asm_memory_error
, dis_asm_print_address
),
1002 m_buffer (!use_ext_lang_for_styling () && use_libopcodes_for_styling ())
1008 gdb_disassembler::use_ext_lang_for_styling () const
1010 /* The use of m_di.created_styled_output here is a bit of a cheat, but
1011 it works fine for now.
1013 This function is called in situations after m_di has been initialized,
1014 but before the instruction has been disassembled.
1016 Currently, every target that supports libopcodes styling sets the
1017 created_styled_output field in disassemble_init_for_target, which was
1018 called as part of the initialization of gdb_printing_disassembler.
1020 This means that we are OK to check the created_styled_output field
1023 If, in the future, there's ever a target that only sets the
1024 created_styled_output field during the actual instruction disassembly
1025 phase, then we will need to update this code. */
1026 return (disassembler_styling
1027 && (!m_di
.created_styled_output
|| !use_libopcodes_styling
)
1028 && use_ext_lang_colorization_p
1029 && m_dest
->can_emit_style_escape ());
1035 gdb_disassembler::use_libopcodes_for_styling () const
1037 /* See the comment on the use of m_di.created_styled_output in the
1038 gdb_disassembler::use_ext_lang_for_styling function. */
1039 return (disassembler_styling
1040 && m_di
.created_styled_output
1041 && use_libopcodes_styling
1042 && m_dest
->can_emit_style_escape ());
1047 gdb_disassemble_info::gdb_disassemble_info
1048 (struct gdbarch
*gdbarch
,
1049 read_memory_ftype read_memory_func
, memory_error_ftype memory_error_func
,
1050 print_address_ftype print_address_func
, fprintf_ftype fprintf_func
,
1051 fprintf_styled_ftype fprintf_styled_func
)
1052 : m_gdbarch (gdbarch
)
1054 gdb_assert (fprintf_func
!= nullptr);
1055 gdb_assert (fprintf_styled_func
!= nullptr);
1056 init_disassemble_info (&m_di
, (void *) this, fprintf_func
,
1057 fprintf_styled_func
);
1058 m_di
.flavour
= bfd_target_unknown_flavour
;
1060 /* The memory_error_func, print_address_func, and read_memory_func are
1061 all initialized to a default (non-nullptr) value by the call to
1062 init_disassemble_info above. If the user is overriding these fields
1063 (by passing non-nullptr values) then do that now, otherwise, leave
1064 these fields as the defaults. */
1065 if (memory_error_func
!= nullptr)
1066 m_di
.memory_error_func
= memory_error_func
;
1067 if (print_address_func
!= nullptr)
1068 m_di
.print_address_func
= print_address_func
;
1069 if (read_memory_func
!= nullptr)
1070 m_di
.read_memory_func
= read_memory_func
;
1072 m_di
.arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
1073 m_di
.mach
= gdbarch_bfd_arch_info (gdbarch
)->mach
;
1074 m_di
.endian
= gdbarch_byte_order (gdbarch
);
1075 m_di
.endian_code
= gdbarch_byte_order_for_code (gdbarch
);
1076 m_di
.application_data
= this;
1077 m_disassembler_options_holder
= get_all_disassembler_options (gdbarch
);
1078 if (!m_disassembler_options_holder
.empty ())
1079 m_di
.disassembler_options
= m_disassembler_options_holder
.c_str ();
1080 disassemble_init_for_target (&m_di
);
1085 gdb_disassemble_info::~gdb_disassemble_info ()
1087 disassemble_free_target (&m_di
);
1090 /* Wrapper around calling gdbarch_print_insn. This function takes care of
1091 first calling the extension language hooks for print_insn, and, if none
1092 of the extension languages can print this instruction, calls
1093 gdbarch_print_insn to do the work.
1095 GDBARCH is the architecture to disassemble in, VMA is the address of the
1096 instruction being disassembled, and INFO is the libopcodes disassembler
1097 related information. */
1100 gdb_print_insn_1 (struct gdbarch
*gdbarch
, CORE_ADDR vma
,
1101 struct disassemble_info
*info
)
1103 /* Call into the extension languages to do the disassembly. */
1104 std::optional
<int> length
= ext_lang_print_insn (gdbarch
, vma
, info
);
1105 if (length
.has_value ())
1108 /* No extension language wanted to do the disassembly, so do it
1110 return gdbarch_print_insn (gdbarch
, vma
, info
);
1115 bool gdb_disassembler::use_ext_lang_colorization_p
= true;
1120 gdb_disassembler::print_insn (CORE_ADDR memaddr
,
1121 int *branch_delay_insns
)
1123 m_err_memaddr
.reset ();
1125 this->set_in_comment (false);
1127 int length
= gdb_print_insn_1 (arch (), memaddr
, &m_di
);
1129 /* If we have successfully disassembled an instruction, disassembler
1130 styling using the extension language is on, and libopcodes hasn't
1131 already styled the output for us, and, if the destination can support
1132 styling, then lets call into the extension languages in order to style
1134 if (length
> 0 && use_ext_lang_for_styling ())
1136 std::optional
<std::string
> ext_contents
;
1137 ext_contents
= ext_lang_colorize_disasm (m_buffer
.string (), arch ());
1138 if (ext_contents
.has_value ())
1139 m_buffer
= std::move (*ext_contents
);
1142 /* The extension language failed to add styling to the
1143 disassembly output. Set the static flag so that next time we
1144 disassemble we don't even bother attempting to use the
1145 extension language for styling. */
1146 use_ext_lang_colorization_p
= false;
1148 /* We're about to disassemble this instruction again, reset the
1149 in-comment state. */
1150 this->set_in_comment (false);
1152 /* The instruction we just disassembled, and the extension
1153 languages failed to style, might have otherwise had some
1154 minimal styling applied by GDB. To regain that styling we
1155 need to recreate m_buffer, but this time with styling support.
1157 To do this we perform an in-place new, but this time turn on
1158 the styling support, then we can re-disassembly the
1159 instruction, and gain any minimal styling GDB might add. */
1160 static_assert ((std::is_same
<decltype (m_buffer
),
1161 string_file
>::value
));
1162 gdb_assert (!m_buffer
.term_out ());
1163 m_buffer
.~string_file ();
1164 new (&m_buffer
) string_file (use_libopcodes_for_styling ());
1165 length
= gdb_print_insn_1 (arch (), memaddr
, &m_di
);
1166 gdb_assert (length
> 0);
1170 /* Push any disassemble output to the real destination stream. We do
1171 this even if the disassembler reported failure (-1) as the
1172 disassembler may have printed something to its output stream. */
1173 gdb_printf (m_dest
, "%s", m_buffer
.c_str ());
1175 /* If the disassembler failed then report an appropriate error. */
1178 if (m_err_memaddr
.has_value ())
1179 memory_error (TARGET_XFER_E_IO
, *m_err_memaddr
);
1181 error (_("unknown disassembler error (error = %d)"), length
);
1184 if (branch_delay_insns
!= NULL
)
1186 if (m_di
.insn_info_valid
)
1187 *branch_delay_insns
= m_di
.branch_delay_insns
;
1189 *branch_delay_insns
= 0;
1195 gdb_disassembly (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
1196 gdb_disassembly_flags flags
, int how_many
,
1197 CORE_ADDR low
, CORE_ADDR high
)
1199 struct symtab
*symtab
;
1202 /* Assume symtab is valid for whole PC range. */
1203 symtab
= find_pc_line_symtab (low
);
1205 if (symtab
!= NULL
&& symtab
->linetable () != NULL
)
1206 nlines
= symtab
->linetable ()->nitems
;
1208 if (!(flags
& (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
1210 do_assembly_only (gdbarch
, uiout
, low
, high
, how_many
, flags
);
1212 else if (flags
& DISASSEMBLY_SOURCE
)
1213 do_mixed_source_and_assembly (gdbarch
, uiout
, symtab
, low
, high
,
1216 else if (flags
& DISASSEMBLY_SOURCE_DEPRECATED
)
1217 do_mixed_source_and_assembly_deprecated (gdbarch
, uiout
, symtab
,
1218 low
, high
, how_many
, flags
);
1220 gdb_flush (gdb_stdout
);
1223 /* Print the instruction at address MEMADDR in debugged memory,
1224 on STREAM. Returns the length of the instruction, in bytes,
1225 and, if requested, the number of branch delay slot instructions. */
1228 gdb_print_insn (struct gdbarch
*gdbarch
, CORE_ADDR memaddr
,
1229 struct ui_file
*stream
, int *branch_delay_insns
)
1232 gdb_disassembler
di (gdbarch
, stream
);
1234 return di
.print_insn (memaddr
, branch_delay_insns
);
1237 /* Return the length in bytes of the instruction at address MEMADDR in
1241 gdb_insn_length (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
1243 return gdb_print_insn (gdbarch
, addr
, &null_stream
, NULL
);
1249 gdb_non_printing_disassembler::null_fprintf_func
1250 (void *stream
, const char *format
, ...) noexcept
1258 gdb_non_printing_disassembler::null_fprintf_styled_func
1259 (void *stream
, enum disassembler_style style
,
1260 const char *format
, ...) noexcept
1265 /* A non-printing disassemble_info management class. The disassemble_info
1266 setup by this class will not print anything to the output stream (there
1267 is no output stream), and the instruction to be disassembled will be
1268 read from a buffer passed to the constructor. */
1270 struct gdb_non_printing_buffer_disassembler
1271 : public gdb_non_printing_disassembler
1273 /* Constructor. GDBARCH is the architecture to disassemble for, BUFFER
1274 contains the instruction to disassemble, and INSN_ADDRESS is the
1275 address (in target memory) of the instruction to disassemble. */
1276 gdb_non_printing_buffer_disassembler (struct gdbarch
*gdbarch
,
1277 gdb::array_view
<const gdb_byte
> buffer
,
1278 CORE_ADDR insn_address
)
1279 : gdb_non_printing_disassembler (gdbarch
, nullptr)
1281 /* The cast is necessary until disassemble_info is const-ified. */
1282 m_di
.buffer
= (gdb_byte
*) buffer
.data ();
1283 m_di
.buffer_length
= buffer
.size ();
1284 m_di
.buffer_vma
= insn_address
;
1288 /* Return the length in bytes of INSN. MAX_LEN is the size of the
1289 buffer containing INSN. */
1292 gdb_buffered_insn_length (struct gdbarch
*gdbarch
,
1293 const gdb_byte
*insn
, int max_len
, CORE_ADDR addr
)
1295 gdb::array_view
<const gdb_byte
> buffer
1296 = gdb::make_array_view (insn
, max_len
);
1297 gdb_non_printing_buffer_disassembler
dis (gdbarch
, buffer
, addr
);
1298 int result
= gdb_print_insn_1 (gdbarch
, addr
, dis
.disasm_info ());
1303 get_disassembler_options (struct gdbarch
*gdbarch
)
1305 std::string
*disassembler_options
= gdbarch_disassembler_options (gdbarch
);
1306 if (disassembler_options
== nullptr || disassembler_options
->empty ())
1308 return disassembler_options
->c_str ();
1312 set_disassembler_options (const char *prospective_options
)
1314 struct gdbarch
*gdbarch
= get_current_arch ();
1315 std::string
*disassembler_options
= gdbarch_disassembler_options (gdbarch
);
1316 const disasm_options_and_args_t
*valid_options_and_args
;
1317 const disasm_options_t
*valid_options
;
1318 gdb::unique_xmalloc_ptr
<char> prospective_options_local
1319 = make_unique_xstrdup (prospective_options
);
1320 char *options
= remove_whitespace_and_extra_commas
1321 (prospective_options_local
.get ());
1324 /* Allow all architectures, even ones that do not support 'set disassembler',
1325 to reset their disassembler options to NULL. */
1326 if (options
== NULL
)
1328 if (disassembler_options
!= nullptr)
1329 disassembler_options
->clear ();
1333 valid_options_and_args
= gdbarch_valid_disassembler_options (gdbarch
);
1334 if (valid_options_and_args
== NULL
)
1336 gdb_printf (gdb_stderr
, _("\
1337 'set disassembler-options ...' is not supported on this architecture.\n"));
1341 valid_options
= &valid_options_and_args
->options
;
1343 /* Verify we have valid disassembler options. */
1344 FOR_EACH_DISASSEMBLER_OPTION (opt
, options
)
1347 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
1348 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1350 size_t len
= strlen (valid_options
->name
[i
]);
1355 if (memcmp (opt
, valid_options
->name
[i
], len
) != 0)
1358 if (valid_options
->arg
[i
]->values
== NULL
)
1360 for (j
= 0; valid_options
->arg
[i
]->values
[j
] != NULL
; j
++)
1361 if (disassembler_options_cmp
1362 (arg
, valid_options
->arg
[i
]->values
[j
]) == 0)
1370 else if (disassembler_options_cmp (opt
, valid_options
->name
[i
]) == 0)
1372 if (valid_options
->name
[i
] == NULL
)
1374 gdb_printf (gdb_stderr
,
1375 _("Invalid disassembler option value: '%s'.\n"),
1381 *disassembler_options
= options
;
1385 set_disassembler_options_sfunc (const char *args
, int from_tty
,
1386 struct cmd_list_element
*c
)
1388 set_disassembler_options (prospective_options
.c_str ());
1392 show_disassembler_options_sfunc (struct ui_file
*file
, int from_tty
,
1393 struct cmd_list_element
*c
, const char *value
)
1395 struct gdbarch
*gdbarch
= get_current_arch ();
1396 const disasm_options_and_args_t
*valid_options_and_args
;
1397 const disasm_option_arg_t
*valid_args
;
1398 const disasm_options_t
*valid_options
;
1400 const char *options
= get_disassembler_options (gdbarch
);
1401 if (options
== NULL
)
1404 gdb_printf (file
, _("The current disassembler options are '%s'\n\n"),
1407 valid_options_and_args
= gdbarch_valid_disassembler_options (gdbarch
);
1409 if (valid_options_and_args
== NULL
)
1411 gdb_puts (_("There are no disassembler options available "
1412 "for this architecture.\n"),
1417 valid_options
= &valid_options_and_args
->options
;
1419 gdb_printf (file
, _("\
1420 The following disassembler options are supported for use with the\n\
1421 'set disassembler-options OPTION [,OPTION]...' command:\n"));
1423 if (valid_options
->description
!= NULL
)
1425 size_t i
, max_len
= 0;
1427 gdb_printf (file
, "\n");
1429 /* Compute the length of the longest option name. */
1430 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
1432 size_t len
= strlen (valid_options
->name
[i
]);
1434 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1435 len
+= strlen (valid_options
->arg
[i
]->name
);
1440 for (i
= 0, max_len
++; valid_options
->name
[i
] != NULL
; i
++)
1442 gdb_printf (file
, " %s", valid_options
->name
[i
]);
1443 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1444 gdb_printf (file
, "%s", valid_options
->arg
[i
]->name
);
1445 if (valid_options
->description
[i
] != NULL
)
1447 size_t len
= strlen (valid_options
->name
[i
]);
1449 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1450 len
+= strlen (valid_options
->arg
[i
]->name
);
1451 gdb_printf (file
, "%*c %s", (int) (max_len
- len
), ' ',
1452 valid_options
->description
[i
]);
1454 gdb_printf (file
, "\n");
1460 gdb_printf (file
, " ");
1461 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
1463 gdb_printf (file
, "%s", valid_options
->name
[i
]);
1464 if (valid_options
->arg
!= NULL
&& valid_options
->arg
[i
] != NULL
)
1465 gdb_printf (file
, "%s", valid_options
->arg
[i
]->name
);
1466 if (valid_options
->name
[i
+ 1] != NULL
)
1467 gdb_printf (file
, ", ");
1468 file
->wrap_here (2);
1470 gdb_printf (file
, "\n");
1473 valid_args
= valid_options_and_args
->args
;
1474 if (valid_args
!= NULL
)
1478 for (i
= 0; valid_args
[i
].name
!= NULL
; i
++)
1480 if (valid_args
[i
].values
== NULL
)
1482 gdb_printf (file
, _("\n\
1483 For the options above, the following values are supported for \"%s\":\n "),
1484 valid_args
[i
].name
);
1485 for (j
= 0; valid_args
[i
].values
[j
] != NULL
; j
++)
1487 gdb_printf (file
, " %s", valid_args
[i
].values
[j
]);
1488 file
->wrap_here (3);
1490 gdb_printf (file
, "\n");
1495 /* A completion function for "set disassembler". */
1498 disassembler_options_completer (struct cmd_list_element
*ignore
,
1499 completion_tracker
&tracker
,
1500 const char *text
, const char *word
)
1502 struct gdbarch
*gdbarch
= get_current_arch ();
1503 const disasm_options_and_args_t
*opts_and_args
1504 = gdbarch_valid_disassembler_options (gdbarch
);
1506 if (opts_and_args
!= NULL
)
1508 const disasm_options_t
*opts
= &opts_and_args
->options
;
1510 /* Only attempt to complete on the last option text. */
1511 const char *separator
= strrchr (text
, ',');
1512 if (separator
!= NULL
)
1513 text
= separator
+ 1;
1514 text
= skip_spaces (text
);
1515 complete_on_enum (tracker
, opts
->name
, text
, word
);
1520 /* Initialization code. */
1522 void _initialize_disasm ();
1524 _initialize_disasm ()
1526 /* Add the command that controls the disassembler options. */
1527 set_show_commands set_show_disas_opts
1528 = add_setshow_string_noescape_cmd ("disassembler-options", no_class
,
1529 &prospective_options
, _("\
1530 Set the disassembler options.\n\
1531 Usage: set disassembler-options OPTION [,OPTION]...\n\n\
1532 See: 'show disassembler-options' for valid option values."), _("\
1533 Show the disassembler options."), NULL
,
1534 set_disassembler_options_sfunc
,
1535 show_disassembler_options_sfunc
,
1536 &setlist
, &showlist
);
1537 set_cmd_completer (set_show_disas_opts
.set
, disassembler_options_completer
);
1540 /* All the 'maint set|show libopcodes-styling' sub-commands. */
1541 static struct cmd_list_element
*maint_set_libopcodes_styling_cmdlist
;
1542 static struct cmd_list_element
*maint_show_libopcodes_styling_cmdlist
;
1544 /* Adds 'maint set|show libopcodes-styling'. */
1545 add_setshow_prefix_cmd ("libopcodes-styling", class_maintenance
,
1546 _("Set libopcodes-styling specific variables."),
1547 _("Show libopcodes-styling specific variables."),
1548 &maint_set_libopcodes_styling_cmdlist
,
1549 &maint_show_libopcodes_styling_cmdlist
,
1550 &maintenance_set_cmdlist
,
1551 &maintenance_show_cmdlist
);
1553 /* Adds 'maint set|show gnu-source-highlight enabled'. */
1554 add_setshow_boolean_cmd ("enabled", class_maintenance
,
1555 &use_libopcodes_styling_option
, _("\
1556 Set whether the libopcodes styling support should be used."), _("\
1557 Show whether the libopcodes styling support should be used."),_("\
1558 When enabled, GDB will try to make use of the builtin libopcodes styling\n\
1559 support, to style the disassembler output. Not every architecture has\n\
1560 styling support within libopcodes, so enabling this is not a guarantee\n\
1561 that libopcodes styling will be available.\n\
1563 When this option is disabled, GDB will make use of the Python Pygments\n\
1564 package (if available) to style the disassembler output.\n\
1566 All disassembler styling can be disabled with:\n\
1568 set style disassembler enabled off"),
1569 set_use_libopcodes_styling
,
1570 show_use_libopcodes_styling
,
1571 &maint_set_libopcodes_styling_cmdlist
,
1572 &maint_show_libopcodes_styling_cmdlist
);