1 /* Disassemble support for GDB.
3 Copyright (C) 2000-2018 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 "common/gdb_optional.h"
35 /* Disassemble functions.
36 FIXME: We should get rid of all the duplicate code in gdb that does
37 the same thing: disassemble_command() and the gdbtk variation. */
39 /* This variable is used to hold the prospective disassembler_options value
40 which is set by the "set disassembler_options" command. */
41 static char *prospective_options
= NULL
;
43 /* This structure is used to store line number information for the
45 We need a different sort of line table from the normal one cuz we can't
46 depend upon implicit line-end pc's for lines to do the
47 reordering in this function. */
49 struct deprecated_dis_line_entry
56 /* This Structure is used to store line number information.
57 We need a different sort of line table from the normal one cuz we can't
58 depend upon implicit line-end pc's for lines to do the
59 reordering in this function. */
63 struct symtab
*symtab
;
67 /* Hash function for dis_line_entry. */
70 hash_dis_line_entry (const void *item
)
72 const struct dis_line_entry
*dle
= (const struct dis_line_entry
*) item
;
74 return htab_hash_pointer (dle
->symtab
) + dle
->line
;
77 /* Equal function for dis_line_entry. */
80 eq_dis_line_entry (const void *item_lhs
, const void *item_rhs
)
82 const struct dis_line_entry
*lhs
= (const struct dis_line_entry
*) item_lhs
;
83 const struct dis_line_entry
*rhs
= (const struct dis_line_entry
*) item_rhs
;
85 return (lhs
->symtab
== rhs
->symtab
86 && lhs
->line
== rhs
->line
);
89 /* Create the table to manage lines for mixed source/disassembly. */
92 allocate_dis_line_table (void)
94 return htab_create_alloc (41,
95 hash_dis_line_entry
, eq_dis_line_entry
,
96 xfree
, xcalloc
, xfree
);
99 /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
102 add_dis_line_entry (htab_t table
, struct symtab
*symtab
, int line
)
105 struct dis_line_entry dle
, *dlep
;
109 slot
= htab_find_slot (table
, &dle
, INSERT
);
112 dlep
= XNEW (struct dis_line_entry
);
113 dlep
->symtab
= symtab
;
119 /* Return non-zero if SYMTAB, LINE are in TABLE. */
122 line_has_code_p (htab_t table
, struct symtab
*symtab
, int line
)
124 struct dis_line_entry dle
;
128 return htab_find (table
, &dle
) != NULL
;
131 /* Wrapper of target_read_code. */
134 gdb_disassembler::dis_asm_read_memory (bfd_vma memaddr
, gdb_byte
*myaddr
,
136 struct disassemble_info
*info
)
138 return target_read_code (memaddr
, myaddr
, len
);
141 /* Wrapper of memory_error. */
144 gdb_disassembler::dis_asm_memory_error (int err
, bfd_vma memaddr
,
145 struct disassemble_info
*info
)
147 gdb_disassembler
*self
148 = static_cast<gdb_disassembler
*>(info
->application_data
);
150 self
->m_err_memaddr
= memaddr
;
153 /* Wrapper of print_address. */
156 gdb_disassembler::dis_asm_print_address (bfd_vma addr
,
157 struct disassemble_info
*info
)
159 gdb_disassembler
*self
160 = static_cast<gdb_disassembler
*>(info
->application_data
);
162 print_address (self
->arch (), addr
, self
->stream ());
166 compare_lines (const void *mle1p
, const void *mle2p
)
168 struct deprecated_dis_line_entry
*mle1
, *mle2
;
171 mle1
= (struct deprecated_dis_line_entry
*) mle1p
;
172 mle2
= (struct deprecated_dis_line_entry
*) mle2p
;
174 /* End of sequence markers have a line number of 0 but don't want to
175 be sorted to the head of the list, instead sort by PC. */
176 if (mle1
->line
== 0 || mle2
->line
== 0)
178 val
= mle1
->start_pc
- mle2
->start_pc
;
180 val
= 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 (struct ui_out
*uiout
,
195 const struct disasm_insn
*insn
,
196 gdb_disassembly_flags flags
)
198 /* parts of the symbolic representation of the address */
204 struct gdbarch
*gdbarch
= arch ();
207 ui_out_emit_tuple
tuple_emitter (uiout
, NULL
);
210 if (insn
->number
!= 0)
212 uiout
->field_fmt ("insn-number", "%u", insn
->number
);
216 if ((flags
& DISASSEMBLY_SPECULATIVE
) != 0)
218 if (insn
->is_speculative
)
220 uiout
->field_string ("is-speculative", "?");
222 /* The speculative execution indication overwrites the first
223 character of the PC prefix.
224 We assume a PC prefix length of 3 characters. */
225 if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
226 uiout
->text (pc_prefix (pc
) + 1);
230 else if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
231 uiout
->text (pc_prefix (pc
));
235 else if ((flags
& DISASSEMBLY_OMIT_PC
) == 0)
236 uiout
->text (pc_prefix (pc
));
237 uiout
->field_core_addr ("address", gdbarch
, pc
);
239 std::string name
, filename
;
240 if (!build_address_symbolic (gdbarch
, pc
, 0, &name
, &offset
, &filename
,
243 /* We don't care now about line, filename and unmapped. But we might in
246 if ((flags
& DISASSEMBLY_OMIT_FNAME
) == 0)
247 uiout
->field_string ("func-name", name
.c_str ());
249 uiout
->field_int ("offset", offset
);
250 uiout
->text (">:\t");
257 if (flags
& DISASSEMBLY_RAW_INSN
)
261 const char *spacer
= "";
263 /* Build the opcodes using a temporary stream so we can
264 write them out in a single go for the MI. */
265 m_opcode_stb
.clear ();
267 size
= m_di
.print_insn (pc
);
270 for (;pc
< end_pc
; ++pc
)
272 read_code (pc
, &data
, 1);
273 m_opcode_stb
.printf ("%s%02x", spacer
, (unsigned) data
);
277 uiout
->field_stream ("opcodes", m_opcode_stb
);
281 size
= m_di
.print_insn (pc
);
283 uiout
->field_stream ("inst", m_insn_stb
);
291 dump_insns (struct gdbarch
*gdbarch
,
292 struct ui_out
*uiout
, CORE_ADDR low
, CORE_ADDR high
,
293 int how_many
, gdb_disassembly_flags flags
, CORE_ADDR
*end_pc
)
295 struct disasm_insn insn
;
296 int num_displayed
= 0;
298 memset (&insn
, 0, sizeof (insn
));
301 gdb_pretty_print_disassembler
disasm (gdbarch
);
303 while (insn
.addr
< high
&& (how_many
< 0 || num_displayed
< how_many
))
307 size
= disasm
.pretty_print_insn (uiout
, &insn
, flags
);
314 /* Allow user to bail out with ^C. */
321 return num_displayed
;
324 /* The idea here is to present a source-O-centric view of a
325 function to the user. This means that things are presented
326 in source order, with (possibly) out of order assembly
327 immediately following.
329 N.B. This view is deprecated. */
332 do_mixed_source_and_assembly_deprecated
333 (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
334 struct symtab
*symtab
,
335 CORE_ADDR low
, CORE_ADDR high
,
336 int how_many
, gdb_disassembly_flags flags
)
340 struct linetable_entry
*le
;
341 struct deprecated_dis_line_entry
*mle
;
342 struct symtab_and_line sal
;
344 int out_of_order
= 0;
346 int num_displayed
= 0;
347 print_source_lines_flags psl_flags
= 0;
349 gdb_assert (symtab
!= NULL
&& SYMTAB_LINETABLE (symtab
) != NULL
);
351 nlines
= SYMTAB_LINETABLE (symtab
)->nitems
;
352 le
= SYMTAB_LINETABLE (symtab
)->item
;
354 if (flags
& DISASSEMBLY_FILENAME
)
355 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
357 mle
= (struct deprecated_dis_line_entry
*)
358 alloca (nlines
* sizeof (struct deprecated_dis_line_entry
));
360 /* Copy linetable entries for this function into our data
361 structure, creating end_pc's and setting out_of_order as
364 /* First, skip all the preceding functions. */
366 for (i
= 0; i
< nlines
- 1 && le
[i
].pc
< low
; i
++);
368 /* Now, copy all entries before the end of this function. */
370 for (; i
< nlines
- 1 && le
[i
].pc
< high
; i
++)
372 if (le
[i
].line
== le
[i
+ 1].line
&& le
[i
].pc
== le
[i
+ 1].pc
)
373 continue; /* Ignore duplicates. */
375 /* Skip any end-of-function markers. */
379 mle
[newlines
].line
= le
[i
].line
;
380 if (le
[i
].line
> le
[i
+ 1].line
)
382 mle
[newlines
].start_pc
= le
[i
].pc
;
383 mle
[newlines
].end_pc
= le
[i
+ 1].pc
;
387 /* If we're on the last line, and it's part of the function,
388 then we need to get the end pc in a special way. */
390 if (i
== nlines
- 1 && le
[i
].pc
< high
)
392 mle
[newlines
].line
= le
[i
].line
;
393 mle
[newlines
].start_pc
= le
[i
].pc
;
394 sal
= find_pc_line (le
[i
].pc
, 0);
395 mle
[newlines
].end_pc
= sal
.end
;
399 /* Now, sort mle by line #s (and, then by addresses within lines). */
402 qsort (mle
, newlines
, sizeof (struct deprecated_dis_line_entry
),
405 /* Now, for each line entry, emit the specified lines (unless
406 they have been emitted before), followed by the assembly code
409 ui_out_emit_list
asm_insns_list (uiout
, "asm_insns");
411 gdb::optional
<ui_out_emit_tuple
> outer_tuple_emitter
;
412 gdb::optional
<ui_out_emit_list
> inner_list_emitter
;
414 for (i
= 0; i
< newlines
; i
++)
416 /* Print out everything from next_line to the current line. */
417 if (mle
[i
].line
>= next_line
)
421 /* Just one line to print. */
422 if (next_line
== mle
[i
].line
)
424 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
425 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, psl_flags
);
429 /* Several source lines w/o asm instructions associated. */
430 for (; next_line
< mle
[i
].line
; next_line
++)
432 ui_out_emit_tuple
tuple_emitter (uiout
,
434 print_source_lines (symtab
, next_line
, next_line
+ 1,
436 ui_out_emit_list
inner_list_emitter (uiout
,
439 /* Print the last line and leave list open for
440 asm instructions to be added. */
441 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
442 print_source_lines (symtab
, next_line
, mle
[i
].line
+ 1, psl_flags
);
447 outer_tuple_emitter
.emplace (uiout
, "src_and_asm_line");
448 print_source_lines (symtab
, mle
[i
].line
, mle
[i
].line
+ 1, psl_flags
);
451 next_line
= mle
[i
].line
+ 1;
452 inner_list_emitter
.emplace (uiout
, "line_asm_insn");
455 num_displayed
+= dump_insns (gdbarch
, uiout
,
456 mle
[i
].start_pc
, mle
[i
].end_pc
,
457 how_many
, flags
, NULL
);
459 /* When we've reached the end of the mle array, or we've seen the last
460 assembly range for this source line, close out the list/tuple. */
461 if (i
== (newlines
- 1) || mle
[i
+ 1].line
> mle
[i
].line
)
463 inner_list_emitter
.reset ();
464 outer_tuple_emitter
.reset ();
467 if (how_many
>= 0 && num_displayed
>= how_many
)
472 /* The idea here is to present a source-O-centric view of a
473 function to the user. This means that things are presented
474 in source order, with (possibly) out of order assembly
475 immediately following. */
478 do_mixed_source_and_assembly (struct gdbarch
*gdbarch
,
479 struct ui_out
*uiout
,
480 struct symtab
*main_symtab
,
481 CORE_ADDR low
, CORE_ADDR high
,
482 int how_many
, gdb_disassembly_flags flags
)
484 const struct linetable_entry
*le
, *first_le
;
486 int num_displayed
= 0;
487 print_source_lines_flags psl_flags
= 0;
489 struct symtab
*last_symtab
;
492 gdb_assert (main_symtab
!= NULL
&& SYMTAB_LINETABLE (main_symtab
) != NULL
);
494 /* First pass: collect the list of all source files and lines.
495 We do this so that we can only print lines containing code once.
496 We try to print the source text leading up to the next instruction,
497 but if that text is for code that will be disassembled later, then
498 we'll want to defer printing it until later with its associated code. */
500 htab_up
dis_line_table (allocate_dis_line_table ());
504 /* The prologue may be empty, but there may still be a line number entry
505 for the opening brace which is distinct from the first line of code.
506 If the prologue has been eliminated find_pc_line may return the source
507 line after the opening brace. We still want to print this opening brace.
508 first_le is used to implement this. */
510 nlines
= SYMTAB_LINETABLE (main_symtab
)->nitems
;
511 le
= SYMTAB_LINETABLE (main_symtab
)->item
;
514 /* Skip all the preceding functions. */
515 for (i
= 0; i
< nlines
&& le
[i
].pc
< low
; i
++)
518 if (i
< nlines
&& le
[i
].pc
< high
)
521 /* Add lines for every pc value. */
524 struct symtab_and_line sal
;
527 sal
= find_pc_line (pc
, 0);
528 length
= gdb_insn_length (gdbarch
, pc
);
531 if (sal
.symtab
!= NULL
)
532 add_dis_line_entry (dis_line_table
.get (), sal
.symtab
, sal
.line
);
535 /* Second pass: print the disassembly.
537 Output format, from an MI perspective:
538 The result is a ui_out list, field name "asm_insns", where elements have
539 name "src_and_asm_line".
540 Each element is a tuple of source line specs (field names line, file,
541 fullname), and field "line_asm_insn" which contains the disassembly.
542 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
545 CLI output works on top of this because MI ignores ui_out_text output,
546 which is where we put file name and source line contents output.
550 Handles the outer "asm_insns" list.
552 The tuples for each group of consecutive disassemblies.
554 List of consecutive source lines or disassembled insns. */
556 if (flags
& DISASSEMBLY_FILENAME
)
557 psl_flags
|= PRINT_SOURCE_LINES_FILENAME
;
559 ui_out_emit_list
asm_insns_emitter (uiout
, "asm_insns");
561 gdb::optional
<ui_out_emit_tuple
> tuple_emitter
;
562 gdb::optional
<ui_out_emit_list
> list_emitter
;
570 struct symtab_and_line sal
;
572 int start_preceding_line_to_display
= 0;
573 int end_preceding_line_to_display
= 0;
574 int new_source_line
= 0;
576 sal
= find_pc_line (pc
, 0);
578 if (sal
.symtab
!= last_symtab
)
580 /* New source file. */
583 /* If this is the first line of output, check for any preceding
587 && first_le
->line
< sal
.line
)
589 start_preceding_line_to_display
= first_le
->line
;
590 end_preceding_line_to_display
= sal
.line
;
595 /* Same source file as last time. */
596 if (sal
.symtab
!= NULL
)
598 if (sal
.line
> last_line
+ 1 && last_line
!= 0)
602 /* Several preceding source lines. Print the trailing ones
603 not associated with code that we'll print later. */
604 for (l
= sal
.line
- 1; l
> last_line
; --l
)
606 if (line_has_code_p (dis_line_table
.get (),
610 if (l
< sal
.line
- 1)
612 start_preceding_line_to_display
= l
+ 1;
613 end_preceding_line_to_display
= sal
.line
;
616 if (sal
.line
!= last_line
)
620 /* Same source line as last time. This can happen, depending
621 on the debug info. */
628 /* Skip the newline if this is the first instruction. */
631 if (tuple_emitter
.has_value ())
633 gdb_assert (list_emitter
.has_value ());
634 list_emitter
.reset ();
635 tuple_emitter
.reset ();
637 if (sal
.symtab
!= last_symtab
638 && !(flags
& DISASSEMBLY_FILENAME
))
640 /* Remember MI ignores ui_out_text.
641 We don't have to do anything here for MI because MI
642 output includes the source specs for each line. */
643 if (sal
.symtab
!= NULL
)
645 uiout
->text (symtab_to_filename_for_display (sal
.symtab
));
648 uiout
->text ("unknown");
651 if (start_preceding_line_to_display
> 0)
653 /* Several source lines w/o asm instructions associated.
654 We need to preserve the structure of the output, so output
655 a bunch of line tuples with no asm entries. */
658 gdb_assert (sal
.symtab
!= NULL
);
659 for (l
= start_preceding_line_to_display
;
660 l
< end_preceding_line_to_display
;
663 ui_out_emit_tuple
tuple_emitter (uiout
, "src_and_asm_line");
664 print_source_lines (sal
.symtab
, l
, l
+ 1, psl_flags
);
665 ui_out_emit_list
chain_line_emitter (uiout
, "line_asm_insn");
668 tuple_emitter
.emplace (uiout
, "src_and_asm_line");
669 if (sal
.symtab
!= NULL
)
670 print_source_lines (sal
.symtab
, sal
.line
, sal
.line
+ 1, psl_flags
);
672 uiout
->text (_("--- no source info for this pc ---\n"));
673 list_emitter
.emplace (uiout
, "line_asm_insn");
677 /* Here we're appending instructions to an existing line.
678 By construction the very first insn will have a symtab
679 and follow the new_source_line path above. */
680 gdb_assert (tuple_emitter
.has_value ());
681 gdb_assert (list_emitter
.has_value ());
685 end_pc
= std::min (sal
.end
, high
);
688 num_displayed
+= dump_insns (gdbarch
, uiout
, pc
, end_pc
,
689 how_many
, flags
, &end_pc
);
692 if (how_many
>= 0 && num_displayed
>= how_many
)
695 last_symtab
= sal
.symtab
;
696 last_line
= sal
.line
;
701 do_assembly_only (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
702 CORE_ADDR low
, CORE_ADDR high
,
703 int how_many
, gdb_disassembly_flags flags
)
705 ui_out_emit_list
list_emitter (uiout
, "asm_insns");
707 dump_insns (gdbarch
, uiout
, low
, high
, how_many
, flags
, NULL
);
710 /* Initialize the disassemble info struct ready for the specified
713 static int ATTRIBUTE_PRINTF (2, 3)
714 fprintf_disasm (void *stream
, const char *format
, ...)
718 va_start (args
, format
);
719 vfprintf_filtered ((struct ui_file
*) stream
, format
, args
);
721 /* Something non -ve. */
725 gdb_disassembler::gdb_disassembler (struct gdbarch
*gdbarch
,
726 struct ui_file
*file
,
727 di_read_memory_ftype read_memory_func
)
728 : m_gdbarch (gdbarch
),
731 init_disassemble_info (&m_di
, file
, fprintf_disasm
);
732 m_di
.flavour
= bfd_target_unknown_flavour
;
733 m_di
.memory_error_func
= dis_asm_memory_error
;
734 m_di
.print_address_func
= dis_asm_print_address
;
735 /* NOTE: cagney/2003-04-28: The original code, from the old Insight
736 disassembler had a local optomization here. By default it would
737 access the executable file, instead of the target memory (there
738 was a growing list of exceptions though). Unfortunately, the
739 heuristic was flawed. Commands like "disassemble &variable"
740 didn't work as they relied on the access going to the target.
741 Further, it has been supperseeded by trust-read-only-sections
742 (although that should be superseeded by target_trust..._p()). */
743 m_di
.read_memory_func
= read_memory_func
;
744 m_di
.arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
745 m_di
.mach
= gdbarch_bfd_arch_info (gdbarch
)->mach
;
746 m_di
.endian
= gdbarch_byte_order (gdbarch
);
747 m_di
.endian_code
= gdbarch_byte_order_for_code (gdbarch
);
748 m_di
.application_data
= this;
749 m_di
.disassembler_options
= get_disassembler_options (gdbarch
);
750 disassemble_init_for_target (&m_di
);
754 gdb_disassembler::print_insn (CORE_ADDR memaddr
,
755 int *branch_delay_insns
)
759 int length
= gdbarch_print_insn (arch (), memaddr
, &m_di
);
762 memory_error (TARGET_XFER_E_IO
, m_err_memaddr
);
764 if (branch_delay_insns
!= NULL
)
766 if (m_di
.insn_info_valid
)
767 *branch_delay_insns
= m_di
.branch_delay_insns
;
769 *branch_delay_insns
= 0;
775 gdb_disassembly (struct gdbarch
*gdbarch
, struct ui_out
*uiout
,
776 gdb_disassembly_flags flags
, int how_many
,
777 CORE_ADDR low
, CORE_ADDR high
)
779 struct symtab
*symtab
;
782 /* Assume symtab is valid for whole PC range. */
783 symtab
= find_pc_line_symtab (low
);
785 if (symtab
!= NULL
&& SYMTAB_LINETABLE (symtab
) != NULL
)
786 nlines
= SYMTAB_LINETABLE (symtab
)->nitems
;
788 if (!(flags
& (DISASSEMBLY_SOURCE_DEPRECATED
| DISASSEMBLY_SOURCE
))
790 do_assembly_only (gdbarch
, uiout
, low
, high
, how_many
, flags
);
792 else if (flags
& DISASSEMBLY_SOURCE
)
793 do_mixed_source_and_assembly (gdbarch
, uiout
, symtab
, low
, high
,
796 else if (flags
& DISASSEMBLY_SOURCE_DEPRECATED
)
797 do_mixed_source_and_assembly_deprecated (gdbarch
, uiout
, symtab
,
798 low
, high
, how_many
, flags
);
800 gdb_flush (gdb_stdout
);
803 /* Print the instruction at address MEMADDR in debugged memory,
804 on STREAM. Returns the length of the instruction, in bytes,
805 and, if requested, the number of branch delay slot instructions. */
808 gdb_print_insn (struct gdbarch
*gdbarch
, CORE_ADDR memaddr
,
809 struct ui_file
*stream
, int *branch_delay_insns
)
812 gdb_disassembler
di (gdbarch
, stream
);
814 return di
.print_insn (memaddr
, branch_delay_insns
);
817 /* Return the length in bytes of the instruction at address MEMADDR in
821 gdb_insn_length (struct gdbarch
*gdbarch
, CORE_ADDR addr
)
823 return gdb_print_insn (gdbarch
, addr
, &null_stream
, NULL
);
826 /* fprintf-function for gdb_buffered_insn_length. This function is a
827 nop, we don't want to print anything, we just want to compute the
828 length of the insn. */
830 static int ATTRIBUTE_PRINTF (2, 3)
831 gdb_buffered_insn_length_fprintf (void *stream
, const char *format
, ...)
836 /* Initialize a struct disassemble_info for gdb_buffered_insn_length. */
839 gdb_buffered_insn_length_init_dis (struct gdbarch
*gdbarch
,
840 struct disassemble_info
*di
,
841 const gdb_byte
*insn
, int max_len
,
844 init_disassemble_info (di
, NULL
, gdb_buffered_insn_length_fprintf
);
846 /* init_disassemble_info installs buffer_read_memory, etc.
847 so we don't need to do that here.
848 The cast is necessary until disassemble_info is const-ified. */
849 di
->buffer
= (gdb_byte
*) insn
;
850 di
->buffer_length
= max_len
;
851 di
->buffer_vma
= addr
;
853 di
->arch
= gdbarch_bfd_arch_info (gdbarch
)->arch
;
854 di
->mach
= gdbarch_bfd_arch_info (gdbarch
)->mach
;
855 di
->endian
= gdbarch_byte_order (gdbarch
);
856 di
->endian_code
= gdbarch_byte_order_for_code (gdbarch
);
858 di
->disassembler_options
= get_disassembler_options (gdbarch
);
859 disassemble_init_for_target (di
);
862 /* Return the length in bytes of INSN. MAX_LEN is the size of the
863 buffer containing INSN. */
866 gdb_buffered_insn_length (struct gdbarch
*gdbarch
,
867 const gdb_byte
*insn
, int max_len
, CORE_ADDR addr
)
869 struct disassemble_info di
;
871 gdb_buffered_insn_length_init_dis (gdbarch
, &di
, insn
, max_len
, addr
);
873 return gdbarch_print_insn (gdbarch
, addr
, &di
);
877 get_disassembler_options (struct gdbarch
*gdbarch
)
879 char **disassembler_options
= gdbarch_disassembler_options (gdbarch
);
880 if (disassembler_options
== NULL
)
882 return *disassembler_options
;
886 set_disassembler_options (char *prospective_options
)
888 struct gdbarch
*gdbarch
= get_current_arch ();
889 char **disassembler_options
= gdbarch_disassembler_options (gdbarch
);
890 const disasm_options_t
*valid_options
;
891 char *options
= remove_whitespace_and_extra_commas (prospective_options
);
894 /* Allow all architectures, even ones that do not support 'set disassembler',
895 to reset their disassembler options to NULL. */
898 if (disassembler_options
!= NULL
)
900 free (*disassembler_options
);
901 *disassembler_options
= NULL
;
906 valid_options
= gdbarch_valid_disassembler_options (gdbarch
);
907 if (valid_options
== NULL
)
909 fprintf_filtered (gdb_stdlog
, _("\
910 'set disassembler-options ...' is not supported on this architecture.\n"));
914 /* Verify we have valid disassembler options. */
915 FOR_EACH_DISASSEMBLER_OPTION (opt
, options
)
918 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
919 if (disassembler_options_cmp (opt
, valid_options
->name
[i
]) == 0)
921 if (valid_options
->name
[i
] == NULL
)
923 fprintf_filtered (gdb_stdlog
,
924 _("Invalid disassembler option value: '%s'.\n"),
930 free (*disassembler_options
);
931 *disassembler_options
= xstrdup (options
);
935 set_disassembler_options_sfunc (const char *args
, int from_tty
,
936 struct cmd_list_element
*c
)
938 set_disassembler_options (prospective_options
);
942 show_disassembler_options_sfunc (struct ui_file
*file
, int from_tty
,
943 struct cmd_list_element
*c
, const char *value
)
945 struct gdbarch
*gdbarch
= get_current_arch ();
946 const disasm_options_t
*valid_options
;
948 const char *options
= get_disassembler_options (gdbarch
);
952 fprintf_filtered (file
, _("The current disassembler options are '%s'\n"),
955 valid_options
= gdbarch_valid_disassembler_options (gdbarch
);
957 if (valid_options
== NULL
)
960 fprintf_filtered (file
, _("\n\
961 The following disassembler options are supported for use with the\n\
962 'set disassembler-options <option>[,<option>...]' command:\n"));
964 if (valid_options
->description
!= NULL
)
966 size_t i
, max_len
= 0;
968 /* Compute the length of the longest option name. */
969 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
971 size_t len
= strlen (valid_options
->name
[i
]);
976 for (i
= 0, max_len
++; valid_options
->name
[i
] != NULL
; i
++)
978 fprintf_filtered (file
, " %s", valid_options
->name
[i
]);
979 if (valid_options
->description
[i
] != NULL
)
980 fprintf_filtered (file
, "%*c %s",
981 (int)(max_len
- strlen (valid_options
->name
[i
])), ' ',
982 valid_options
->description
[i
]);
983 fprintf_filtered (file
, "\n");
989 fprintf_filtered (file
, " ");
990 for (i
= 0; valid_options
->name
[i
] != NULL
; i
++)
992 fprintf_filtered (file
, "%s", valid_options
->name
[i
]);
993 if (valid_options
->name
[i
+ 1] != NULL
)
994 fprintf_filtered (file
, ", ");
997 fprintf_filtered (file
, "\n");
1001 /* A completion function for "set disassembler". */
1004 disassembler_options_completer (struct cmd_list_element
*ignore
,
1005 completion_tracker
&tracker
,
1006 const char *text
, const char *word
)
1008 struct gdbarch
*gdbarch
= get_current_arch ();
1009 const disasm_options_t
*opts
= gdbarch_valid_disassembler_options (gdbarch
);
1013 /* Only attempt to complete on the last option text. */
1014 const char *separator
= strrchr (text
, ',');
1015 if (separator
!= NULL
)
1016 text
= separator
+ 1;
1017 text
= skip_spaces (text
);
1018 complete_on_enum (tracker
, opts
->name
, text
, word
);
1023 /* Initialization code. */
1026 _initialize_disasm (void)
1028 struct cmd_list_element
*cmd
;
1030 /* Add the command that controls the disassembler options. */
1031 cmd
= add_setshow_string_noescape_cmd ("disassembler-options", no_class
,
1032 &prospective_options
, _("\
1033 Set the disassembler options.\n\
1034 Usage: set disassembler-options OPTION [,OPTION]...\n\n\
1035 See: 'show disassembler-options' for valid option values.\n"), _("\
1036 Show the disassembler options."), NULL
,
1037 set_disassembler_options_sfunc
,
1038 show_disassembler_options_sfunc
,
1039 &setlist
, &showlist
);
1040 set_cmd_completer (cmd
, disassembler_options_completer
);