1 /* Disassemble support for GDB.
3 Copyright (C) 2000-2019 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 (),
248 ui_out_style_kind::FUNCTION);
250 uiout->field_int ("offset", offset);
251 uiout->text (">:\t");
258 if (flags & DISASSEMBLY_RAW_INSN)
262 const char *spacer = "";
264 /* Build the opcodes using a temporary stream so we can
265 write them out in a single go for the MI. */
266 m_opcode_stb.clear ();
268 size = m_di.print_insn (pc);
271 for (;pc < end_pc; ++pc)
273 read_code (pc, &data, 1);
274 m_opcode_stb.printf ("%s%02x", spacer, (unsigned) data);
278 uiout->field_stream ("opcodes", m_opcode_stb);
282 size = m_di.print_insn (pc);
284 uiout->field_stream ("inst", m_insn_stb);
292 dump_insns (struct gdbarch *gdbarch,
293 struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
294 int how_many, gdb_disassembly_flags flags, CORE_ADDR *end_pc)
296 struct disasm_insn insn;
297 int num_displayed = 0;
299 memset (&insn, 0, sizeof (insn));
302 gdb_pretty_print_disassembler disasm (gdbarch);
304 while (insn.addr < high && (how_many < 0 || num_displayed < how_many))
308 size = disasm.pretty_print_insn (uiout, &insn, flags);
315 /* Allow user to bail out with ^C. */
322 return num_displayed;
325 /* The idea here is to present a source-O-centric view of a
326 function to the user. This means that things are presented
327 in source order, with (possibly) out of order assembly
328 immediately following.
330 N.B. This view is deprecated. */
333 do_mixed_source_and_assembly_deprecated
334 (struct gdbarch *gdbarch, struct ui_out *uiout,
335 struct symtab *symtab,
336 CORE_ADDR low, CORE_ADDR high,
337 int how_many, gdb_disassembly_flags flags)
341 struct linetable_entry *le;
342 struct deprecated_dis_line_entry *mle;
343 struct symtab_and_line sal;
345 int out_of_order = 0;
347 int num_displayed = 0;
348 print_source_lines_flags psl_flags = 0;
350 gdb_assert (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL);
352 nlines = SYMTAB_LINETABLE (symtab)->nitems;
353 le = SYMTAB_LINETABLE (symtab)->item;
355 if (flags & DISASSEMBLY_FILENAME)
356 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
358 mle = (struct deprecated_dis_line_entry *)
359 alloca (nlines * sizeof (struct deprecated_dis_line_entry));
361 /* Copy linetable entries for this function into our data
362 structure, creating end_pc's and setting out_of_order as
365 /* First, skip all the preceding functions. */
367 for (i = 0; i < nlines - 1 && le[i].pc < low; i++);
369 /* Now, copy all entries before the end of this function. */
371 for (; i < nlines - 1 && le[i].pc < high; i++)
373 if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
374 continue; /* Ignore duplicates. */
376 /* Skip any end-of-function markers. */
380 mle[newlines].line = le[i].line;
381 if (le[i].line > le[i + 1].line)
383 mle[newlines].start_pc = le[i].pc;
384 mle[newlines].end_pc = le[i + 1].pc;
388 /* If we're on the last line, and it's part of the function,
389 then we need to get the end pc in a special way. */
391 if (i == nlines - 1 && le[i].pc < high)
393 mle[newlines].line = le[i].line;
394 mle[newlines].start_pc = le[i].pc;
395 sal = find_pc_line (le[i].pc, 0);
396 mle[newlines].end_pc = sal.end;
400 /* Now, sort mle by line #s (and, then by addresses within lines). */
403 qsort (mle, newlines, sizeof (struct deprecated_dis_line_entry),
406 /* Now, for each line entry, emit the specified lines (unless
407 they have been emitted before), followed by the assembly code
410 ui_out_emit_list asm_insns_list (uiout, "asm_insns");
412 gdb::optional<ui_out_emit_tuple> outer_tuple_emitter;
413 gdb::optional<ui_out_emit_list> inner_list_emitter;
415 for (i = 0; i < newlines; i++)
417 /* Print out everything from next_line to the current line. */
418 if (mle[i].line >= next_line)
422 /* Just one line to print. */
423 if (next_line == mle[i].line)
425 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
426 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
430 /* Several source lines w/o asm instructions associated. */
431 for (; next_line < mle[i].line; next_line++)
433 ui_out_emit_tuple tuple_emitter (uiout,
435 print_source_lines (symtab, next_line, next_line + 1,
437 ui_out_emit_list temp_list_emitter (uiout,
440 /* Print the last line and leave list open for
441 asm instructions to be added. */
442 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
443 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
448 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
449 print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
452 next_line = mle[i].line + 1;
453 inner_list_emitter.emplace (uiout, "line_asm_insn");
456 num_displayed += dump_insns (gdbarch, uiout,
457 mle[i].start_pc, mle[i].end_pc,
458 how_many, flags, NULL);
460 /* When we've reached the end of the mle array, or we've seen the last
461 assembly range for this source line, close out the list/tuple. */
462 if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
464 inner_list_emitter.reset ();
465 outer_tuple_emitter.reset ();
468 if (how_many >= 0 && num_displayed >= how_many)
473 /* The idea here is to present a source-O-centric view of a
474 function to the user. This means that things are presented
475 in source order, with (possibly) out of order assembly
476 immediately following. */
479 do_mixed_source_and_assembly (struct gdbarch *gdbarch,
480 struct ui_out *uiout,
481 struct symtab *main_symtab,
482 CORE_ADDR low, CORE_ADDR high,
483 int how_many, gdb_disassembly_flags flags)
485 const struct linetable_entry *le, *first_le;
487 int num_displayed = 0;
488 print_source_lines_flags psl_flags = 0;
490 struct symtab *last_symtab;
493 gdb_assert (main_symtab != NULL && SYMTAB_LINETABLE (main_symtab) != NULL);
495 /* First pass: collect the list of all source files and lines.
496 We do this so that we can only print lines containing code once.
497 We try to print the source text leading up to the next instruction,
498 but if that text is for code that will be disassembled later, then
499 we'll want to defer printing it until later with its associated code. */
501 htab_up dis_line_table (allocate_dis_line_table ());
505 /* The prologue may be empty, but there may still be a line number entry
506 for the opening brace which is distinct from the first line of code.
507 If the prologue has been eliminated find_pc_line may return the source
508 line after the opening brace. We still want to print this opening brace.
509 first_le is used to implement this. */
511 nlines = SYMTAB_LINETABLE (main_symtab)->nitems;
512 le = SYMTAB_LINETABLE (main_symtab)->item;
515 /* Skip all the preceding functions. */
516 for (i = 0; i < nlines && le[i].pc < low; i++)
519 if (i < nlines && le[i].pc < high)
522 /* Add lines for every pc value. */
525 struct symtab_and_line sal;
528 sal = find_pc_line (pc, 0);
529 length = gdb_insn_length (gdbarch, pc);
532 if (sal.symtab != NULL)
533 add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
536 /* Second pass: print the disassembly.
538 Output format, from an MI perspective:
539 The result is a ui_out list, field name "asm_insns", where elements have
540 name "src_and_asm_line".
541 Each element is a tuple of source line specs (field names line, file,
542 fullname), and field "line_asm_insn" which contains the disassembly.
543 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
546 CLI output works on top of this because MI ignores ui_out_text output,
547 which is where we put file name and source line contents output.
551 Handles the outer "asm_insns" list.
553 The tuples for each group of consecutive disassemblies.
555 List of consecutive source lines or disassembled insns. */
557 if (flags & DISASSEMBLY_FILENAME)
558 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
560 ui_out_emit_list asm_insns_emitter (uiout, "asm_insns");
562 gdb::optional<ui_out_emit_tuple> tuple_emitter;
563 gdb::optional<ui_out_emit_list> list_emitter;
571 struct symtab_and_line sal;
573 int start_preceding_line_to_display = 0;
574 int end_preceding_line_to_display = 0;
575 int new_source_line = 0;
577 sal = find_pc_line (pc, 0);
579 if (sal.symtab != last_symtab)
581 /* New source file. */
584 /* If this is the first line of output, check for any preceding
588 && first_le->line < sal.line)
590 start_preceding_line_to_display = first_le->line;
591 end_preceding_line_to_display = sal.line;
596 /* Same source file as last time. */
597 if (sal.symtab != NULL)
599 if (sal.line > last_line + 1 && last_line != 0)
603 /* Several preceding source lines. Print the trailing ones
604 not associated with code that we'll print later. */
605 for (l = sal.line - 1; l > last_line; --l)
607 if (line_has_code_p (dis_line_table.get (),
611 if (l < sal.line - 1)
613 start_preceding_line_to_display = l + 1;
614 end_preceding_line_to_display = sal.line;
617 if (sal.line != last_line)
621 /* Same source line as last time. This can happen, depending
622 on the debug info. */
629 /* Skip the newline if this is the first instruction. */
632 if (tuple_emitter.has_value ())
634 gdb_assert (list_emitter.has_value ());
635 list_emitter.reset ();
636 tuple_emitter.reset ();
638 if (sal.symtab != last_symtab
639 && !(flags & DISASSEMBLY_FILENAME))
641 /* Remember MI ignores ui_out_text.
642 We don't have to do anything here for MI because MI
643 output includes the source specs for each line. */
644 if (sal.symtab != NULL)
646 uiout->text (symtab_to_filename_for_display (sal.symtab));
649 uiout->text ("unknown");
652 if (start_preceding_line_to_display > 0)
654 /* Several source lines w/o asm instructions associated.
655 We need to preserve the structure of the output, so output
656 a bunch of line tuples with no asm entries. */
659 gdb_assert (sal.symtab != NULL);
660 for (l = start_preceding_line_to_display;
661 l < end_preceding_line_to_display;
664 ui_out_emit_tuple line_tuple_emitter (uiout,
666 print_source_lines (sal.symtab, l, l + 1, psl_flags);
667 ui_out_emit_list chain_line_emitter (uiout, "line_asm_insn");
670 tuple_emitter.emplace (uiout, "src_and_asm_line");
671 if (sal.symtab != NULL)
672 print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags);
674 uiout->text (_("--- no source info for this pc ---\n"));
675 list_emitter.emplace (uiout, "line_asm_insn");
679 /* Here we're appending instructions to an existing line.
680 By construction the very first insn will have a symtab
681 and follow the new_source_line path above. */
682 gdb_assert (tuple_emitter.has_value ());
683 gdb_assert (list_emitter.has_value ());
687 end_pc = std::min (sal.end, high);
690 num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
691 how_many, flags, &end_pc);
694 if (how_many >= 0 && num_displayed >= how_many)
697 last_symtab = sal.symtab;
698 last_line = sal.line;
703 do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
704 CORE_ADDR low, CORE_ADDR high,
705 int how_many, gdb_disassembly_flags flags)
707 ui_out_emit_list list_emitter (uiout, "asm_insns");
709 dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
712 /* Initialize the disassemble info struct ready for the specified
715 static int ATTRIBUTE_PRINTF (2, 3)
716 fprintf_disasm (void *stream, const char *format, ...)
720 va_start (args, format);
721 vfprintf_filtered ((struct ui_file *) stream, format, args);
723 /* Something non -ve. */
727 /* Combine implicit and user disassembler options and return them
728 in a newly-created string. */
731 get_all_disassembler_options (struct gdbarch *gdbarch)
733 const char *implicit = gdbarch_disassembler_options_implicit (gdbarch);
734 const char *options = get_disassembler_options (gdbarch);
735 const char *comma = ",";
737 if (implicit == nullptr)
743 if (options == nullptr)
749 return string_printf ("%s%s%s", implicit, comma, options);
752 gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
753 struct ui_file *file,
754 di_read_memory_ftype read_memory_func)
755 : m_gdbarch (gdbarch),
758 init_disassemble_info (&m_di, file, fprintf_disasm);
759 m_di.flavour = bfd_target_unknown_flavour;
760 m_di.memory_error_func = dis_asm_memory_error;
761 m_di.print_address_func = dis_asm_print_address;
762 /* NOTE: cagney/2003-04-28: The original code, from the old Insight
763 disassembler had a local optomization here. By default it would
764 access the executable file, instead of the target memory (there
765 was a growing list of exceptions though). Unfortunately, the
766 heuristic was flawed. Commands like "disassemble &variable"
767 didn't work as they relied on the access going to the target.
768 Further, it has been supperseeded by trust-read-only-sections
769 (although that should be superseeded by target_trust..._p()). */
770 m_di.read_memory_func = read_memory_func;
771 m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
772 m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
773 m_di.endian = gdbarch_byte_order (gdbarch);
774 m_di.endian_code = gdbarch_byte_order_for_code (gdbarch);
775 m_di.application_data = this;
776 m_disassembler_options_holder = get_all_disassembler_options (gdbarch);
777 if (!m_disassembler_options_holder.empty ())
778 m_di.disassembler_options = m_disassembler_options_holder.c_str ();
779 disassemble_init_for_target (&m_di);
783 gdb_disassembler::print_insn (CORE_ADDR memaddr,
784 int *branch_delay_insns)
788 int length = gdbarch_print_insn (arch (), memaddr, &m_di);
791 memory_error (TARGET_XFER_E_IO, m_err_memaddr);
793 if (branch_delay_insns != NULL)
795 if (m_di.insn_info_valid)
796 *branch_delay_insns = m_di.branch_delay_insns;
798 *branch_delay_insns = 0;
804 gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
805 gdb_disassembly_flags flags, int how_many,
806 CORE_ADDR low, CORE_ADDR high)
808 struct symtab *symtab;
811 /* Assume symtab is valid for whole PC range. */
812 symtab = find_pc_line_symtab (low);
814 if (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL)
815 nlines = SYMTAB_LINETABLE (symtab)->nitems;
817 if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
819 do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
821 else if (flags & DISASSEMBLY_SOURCE)
822 do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
825 else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
826 do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
827 low, high, how_many, flags);
829 gdb_flush (gdb_stdout);
832 /* Print the instruction at address MEMADDR in debugged memory,
833 on STREAM. Returns the length of the instruction, in bytes,
834 and, if requested, the number of branch delay slot instructions. */
837 gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
838 struct ui_file *stream, int *branch_delay_insns)
841 gdb_disassembler di (gdbarch, stream);
843 return di.print_insn (memaddr, branch_delay_insns);
846 /* Return the length in bytes of the instruction at address MEMADDR in
850 gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
852 return gdb_print_insn (gdbarch, addr, &null_stream, NULL);
855 /* fprintf-function for gdb_buffered_insn_length. This function is a
856 nop, we don't want to print anything, we just want to compute the
857 length of the insn. */
859 static int ATTRIBUTE_PRINTF (2, 3)
860 gdb_buffered_insn_length_fprintf (void *stream, const char *format, ...)
865 /* Initialize a struct disassemble_info for gdb_buffered_insn_length.
866 Upon return, *DISASSEMBLER_OPTIONS_HOLDER owns the string pointed
867 to by DI.DISASSEMBLER_OPTIONS. */
870 gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch,
871 struct disassemble_info *di,
872 const gdb_byte *insn, int max_len,
874 std::string *disassembler_options_holder)
876 init_disassemble_info (di, NULL, gdb_buffered_insn_length_fprintf);
878 /* init_disassemble_info installs buffer_read_memory, etc.
879 so we don't need to do that here.
880 The cast is necessary until disassemble_info is const-ified. */
881 di->buffer = (gdb_byte *) insn;
882 di->buffer_length = max_len;
883 di->buffer_vma = addr;
885 di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
886 di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
887 di->endian = gdbarch_byte_order (gdbarch);
888 di->endian_code = gdbarch_byte_order_for_code (gdbarch);
890 *disassembler_options_holder = get_all_disassembler_options (gdbarch);
891 if (!disassembler_options_holder->empty ())
892 di->disassembler_options = disassembler_options_holder->c_str ();
893 disassemble_init_for_target (di);
896 /* Return the length in bytes of INSN. MAX_LEN is the size of the
897 buffer containing INSN. */
900 gdb_buffered_insn_length (struct gdbarch *gdbarch,
901 const gdb_byte *insn, int max_len, CORE_ADDR addr)
903 struct disassemble_info di;
904 std::string disassembler_options_holder;
906 gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr,
907 &disassembler_options_holder);
909 return gdbarch_print_insn (gdbarch, addr, &di);
913 get_disassembler_options (struct gdbarch *gdbarch)
915 char **disassembler_options = gdbarch_disassembler_options (gdbarch);
916 if (disassembler_options == NULL)
918 return *disassembler_options;
922 set_disassembler_options (char *prospective_options)
924 struct gdbarch *gdbarch = get_current_arch ();
925 char **disassembler_options = gdbarch_disassembler_options (gdbarch);
926 const disasm_options_and_args_t *valid_options_and_args;
927 const disasm_options_t *valid_options;
928 char *options = remove_whitespace_and_extra_commas (prospective_options);
931 /* Allow all architectures, even ones that do not support 'set disassembler',
932 to reset their disassembler options to NULL. */
935 if (disassembler_options != NULL)
937 free (*disassembler_options);
938 *disassembler_options = NULL;
943 valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
944 if (valid_options_and_args == NULL)
946 fprintf_filtered (gdb_stdlog, _("\
947 'set disassembler-options ...' is not supported on this architecture.\n"));
951 valid_options = &valid_options_and_args->options;
953 /* Verify we have valid disassembler options. */
954 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
957 for (i = 0; valid_options->name[i] != NULL; i++)
958 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
960 size_t len = strlen (valid_options->name[i]);
965 if (memcmp (opt, valid_options->name[i], len) != 0)
968 for (j = 0; valid_options->arg[i]->values[j] != NULL; j++)
969 if (disassembler_options_cmp
970 (arg, valid_options->arg[i]->values[j]) == 0)
978 else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0)
980 if (valid_options->name[i] == NULL)
982 fprintf_filtered (gdb_stdlog,
983 _("Invalid disassembler option value: '%s'.\n"),
989 free (*disassembler_options);
990 *disassembler_options = xstrdup (options);
994 set_disassembler_options_sfunc (const char *args, int from_tty,
995 struct cmd_list_element *c)
997 set_disassembler_options (prospective_options);
1001 show_disassembler_options_sfunc (struct ui_file *file, int from_tty,
1002 struct cmd_list_element *c, const char *value)
1004 struct gdbarch *gdbarch = get_current_arch ();
1005 const disasm_options_and_args_t *valid_options_and_args;
1006 const disasm_option_arg_t *valid_args;
1007 const disasm_options_t *valid_options;
1009 const char *options = get_disassembler_options (gdbarch);
1010 if (options == NULL)
1013 fprintf_filtered (file, _("The current disassembler options are '%s'\n\n"),
1016 valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
1018 if (valid_options_and_args == NULL)
1020 fputs_filtered (_("There are no disassembler options available "
1021 "for this architecture.\n"),
1026 valid_options = &valid_options_and_args->options;
1028 fprintf_filtered (file, _("\
1029 The following disassembler options are supported for use with the\n\
1030 'set disassembler-options OPTION [,OPTION]...' command:\n"));
1032 if (valid_options->description != NULL)
1034 size_t i, max_len = 0;
1036 fprintf_filtered (file, "\n");
1038 /* Compute the length of the longest option name. */
1039 for (i = 0; valid_options->name[i] != NULL; i++)
1041 size_t len = strlen (valid_options->name[i]);
1043 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1044 len += strlen (valid_options->arg[i]->name);
1049 for (i = 0, max_len++; valid_options->name[i] != NULL; i++)
1051 fprintf_filtered (file, " %s", valid_options->name[i]);
1052 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1053 fprintf_filtered (file, "%s", valid_options->arg[i]->name);
1054 if (valid_options->description[i] != NULL)
1056 size_t len = strlen (valid_options->name[i]);
1058 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1059 len += strlen (valid_options->arg[i]->name);
1060 fprintf_filtered (file, "%*c %s", (int) (max_len - len), ' ',
1061 valid_options->description[i]);
1063 fprintf_filtered (file, "\n");
1069 fprintf_filtered (file, " ");
1070 for (i = 0; valid_options->name[i] != NULL; i++)
1072 fprintf_filtered (file, "%s", valid_options->name[i]);
1073 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1074 fprintf_filtered (file, "%s", valid_options->arg[i]->name);
1075 if (valid_options->name[i + 1] != NULL)
1076 fprintf_filtered (file, ", ");
1079 fprintf_filtered (file, "\n");
1082 valid_args = valid_options_and_args->args;
1083 if (valid_args != NULL)
1087 for (i = 0; valid_args[i].name != NULL; i++)
1089 fprintf_filtered (file, _("\n\
1090 For the options above, the following values are supported for \"%s\":\n "),
1091 valid_args[i].name);
1092 for (j = 0; valid_args[i].values[j] != NULL; j++)
1094 fprintf_filtered (file, " %s", valid_args[i].values[j]);
1097 fprintf_filtered (file, "\n");
1102 /* A completion function for "set disassembler". */
1105 disassembler_options_completer (struct cmd_list_element *ignore,
1106 completion_tracker &tracker,
1107 const char *text, const char *word)
1109 struct gdbarch *gdbarch = get_current_arch ();
1110 const disasm_options_and_args_t *opts_and_args
1111 = gdbarch_valid_disassembler_options (gdbarch);
1113 if (opts_and_args != NULL)
1115 const disasm_options_t *opts = &opts_and_args->options;
1117 /* Only attempt to complete on the last option text. */
1118 const char *separator = strrchr (text, ',');
1119 if (separator != NULL)
1120 text = separator + 1;
1121 text = skip_spaces (text);
1122 complete_on_enum (tracker, opts->name, text, word);
1127 /* Initialization code. */
1130 _initialize_disasm (void)
1132 struct cmd_list_element *cmd;
1134 /* Add the command that controls the disassembler options. */
1135 cmd = add_setshow_string_noescape_cmd ("disassembler-options", no_class,
1136 &prospective_options, _("\
1137 Set the disassembler options.\n\
1138 Usage: set disassembler-options OPTION [,OPTION]...\n\n\
1139 See: 'show disassembler-options' for valid option values.\n"), _("\
1140 Show the disassembler options."), NULL,
1141 set_disassembler_options_sfunc,
1142 show_disassembler_options_sfunc,
1143 &setlist, &showlist);
1144 set_cmd_completer (cmd, disassembler_options_completer);