gdb: add interp::on_tsv_deleted method
[binutils-gdb.git] / gdb / disasm.c
1 /* Disassemble support for GDB.
2
3 Copyright (C) 2000-2023 Free Software Foundation, Inc.
4
5 This file is part of GDB.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>. */
19
20 #include "defs.h"
21 #include "arch-utils.h"
22 #include "target.h"
23 #include "value.h"
24 #include "ui-out.h"
25 #include "disasm.h"
26 #include "gdbcore.h"
27 #include "gdbcmd.h"
28 #include "dis-asm.h"
29 #include "source.h"
30 #include "gdbsupport/gdb-safe-ctype.h"
31 #include <algorithm>
32 #include "gdbsupport/gdb_optional.h"
33 #include "valprint.h"
34 #include "cli/cli-style.h"
35 #include "objfiles.h"
36
37 /* Disassemble functions.
38 FIXME: We should get rid of all the duplicate code in gdb that does
39 the same thing: disassemble_command() and the gdbtk variation. */
40
41 /* This variable is used to hold the prospective disassembler_options value
42 which is set by the "set disassembler_options" command. */
43 static std::string prospective_options;
44
45 /* When this is true we will try to use libopcodes to provide styling to
46 the disassembler output. */
47
48 static bool use_libopcodes_styling = true;
49
50 /* To support the set_use_libopcodes_styling function we have a second
51 variable which is connected to the actual set/show option. */
52
53 static bool use_libopcodes_styling_option = use_libopcodes_styling;
54
55 /* The "maint show libopcodes-styling enabled" command. */
56
57 static void
58 show_use_libopcodes_styling (struct ui_file *file, int from_tty,
59 struct cmd_list_element *c,
60 const char *value)
61 {
62 gdb_non_printing_memory_disassembler dis (target_gdbarch ());
63 bool supported = dis.disasm_info ()->created_styled_output;
64
65 if (supported || !use_libopcodes_styling)
66 gdb_printf (file, _("Use of libopcodes styling support is \"%s\".\n"),
67 value);
68 else
69 {
70 /* Use of libopcodes styling is not supported, and the user has this
71 turned on! */
72 gdb_printf (file, _("Use of libopcodes styling support is \"off\""
73 " (not supported on architecture \"%s\")\n"),
74 gdbarch_bfd_arch_info (target_gdbarch ())->printable_name);
75 }
76 }
77
78 /* The "maint set libopcodes-styling enabled" command. */
79
80 static void
81 set_use_libopcodes_styling (const char *args, int from_tty,
82 struct cmd_list_element *c)
83 {
84 gdb_non_printing_memory_disassembler dis (target_gdbarch ());
85 bool supported = dis.disasm_info ()->created_styled_output;
86
87 /* If the current architecture doesn't support libopcodes styling then we
88 give an error here, but leave the underlying setting enabled. This
89 means that if the user switches to an architecture that does support
90 libopcodes styling the setting will be enabled. */
91
92 if (use_libopcodes_styling_option && !supported)
93 {
94 use_libopcodes_styling_option = use_libopcodes_styling;
95 error (_("Use of libopcodes styling not supported on architecture \"%s\"."),
96 gdbarch_bfd_arch_info (target_gdbarch ())->printable_name);
97 }
98 else
99 use_libopcodes_styling = use_libopcodes_styling_option;
100 }
101
102 /* This structure is used to store line number information for the
103 deprecated /m option.
104 We need a different sort of line table from the normal one cuz we can't
105 depend upon implicit line-end pc's for lines to do the
106 reordering in this function. */
107
108 struct deprecated_dis_line_entry
109 {
110 int line;
111 CORE_ADDR start_pc;
112 CORE_ADDR end_pc;
113 };
114
115 /* This Structure is used to store line number information.
116 We need a different sort of line table from the normal one cuz we can't
117 depend upon implicit line-end pc's for lines to do the
118 reordering in this function. */
119
120 struct dis_line_entry
121 {
122 struct symtab *symtab;
123 int line;
124 };
125
126 /* Hash function for dis_line_entry. */
127
128 static hashval_t
129 hash_dis_line_entry (const void *item)
130 {
131 const struct dis_line_entry *dle = (const struct dis_line_entry *) item;
132
133 return htab_hash_pointer (dle->symtab) + dle->line;
134 }
135
136 /* Equal function for dis_line_entry. */
137
138 static int
139 eq_dis_line_entry (const void *item_lhs, const void *item_rhs)
140 {
141 const struct dis_line_entry *lhs = (const struct dis_line_entry *) item_lhs;
142 const struct dis_line_entry *rhs = (const struct dis_line_entry *) item_rhs;
143
144 return (lhs->symtab == rhs->symtab
145 && lhs->line == rhs->line);
146 }
147
148 /* Create the table to manage lines for mixed source/disassembly. */
149
150 static htab_t
151 allocate_dis_line_table (void)
152 {
153 return htab_create_alloc (41,
154 hash_dis_line_entry, eq_dis_line_entry,
155 xfree, xcalloc, xfree);
156 }
157
158 /* Add a new dis_line_entry containing SYMTAB and LINE to TABLE. */
159
160 static void
161 add_dis_line_entry (htab_t table, struct symtab *symtab, int line)
162 {
163 void **slot;
164 struct dis_line_entry dle, *dlep;
165
166 dle.symtab = symtab;
167 dle.line = line;
168 slot = htab_find_slot (table, &dle, INSERT);
169 if (*slot == NULL)
170 {
171 dlep = XNEW (struct dis_line_entry);
172 dlep->symtab = symtab;
173 dlep->line = line;
174 *slot = dlep;
175 }
176 }
177
178 /* Return non-zero if SYMTAB, LINE are in TABLE. */
179
180 static int
181 line_has_code_p (htab_t table, struct symtab *symtab, int line)
182 {
183 struct dis_line_entry dle;
184
185 dle.symtab = symtab;
186 dle.line = line;
187 return htab_find (table, &dle) != NULL;
188 }
189
190 /* Wrapper of target_read_code. */
191
192 int
193 gdb_disassembler_memory_reader::dis_asm_read_memory
194 (bfd_vma memaddr, gdb_byte *myaddr, unsigned int len,
195 struct disassemble_info *info) noexcept
196 {
197 return target_read_code (memaddr, myaddr, len);
198 }
199
200 /* Wrapper of memory_error. */
201
202 void
203 gdb_disassembler::dis_asm_memory_error
204 (int err, bfd_vma memaddr, struct disassemble_info *info) noexcept
205 {
206 gdb_disassembler *self
207 = static_cast<gdb_disassembler *>(info->application_data);
208
209 self->m_err_memaddr.emplace (memaddr);
210 }
211
212 /* Wrapper of print_address. */
213
214 void
215 gdb_disassembler::dis_asm_print_address
216 (bfd_vma addr, struct disassemble_info *info) noexcept
217 {
218 gdb_disassembler *self
219 = static_cast<gdb_disassembler *>(info->application_data);
220
221 if (self->in_comment_p ())
222 {
223 /* Calling 'print_address' might add styling to the output (based on
224 the properties of the stream we're writing too). This is usually
225 fine, but if we are in an assembler comment then we'd prefer to
226 have the comment style, rather than the default address style.
227
228 Print the address into a temporary buffer which doesn't support
229 styling, then reprint this unstyled address with the default text
230 style.
231
232 As we are inside a comment right now, the standard print routine
233 will ensure that the comment is printed to the user with a
234 suitable comment style. */
235 string_file tmp;
236 print_address (self->arch (), addr, &tmp);
237 self->fprintf_styled_func (self, dis_style_text, "%s", tmp.c_str ());
238 }
239 else
240 print_address (self->arch (), addr, self->stream ());
241 }
242
243 /* See disasm.h. */
244
245 ui_file *
246 gdb_printing_disassembler::stream_from_gdb_disassemble_info (void *dis_info)
247 {
248 gdb_disassemble_info *di = (gdb_disassemble_info *) dis_info;
249 gdb_printing_disassembler *dis
250 = gdb::checked_static_cast<gdb_printing_disassembler *> (di);
251 ui_file *stream = dis->stream ();
252 gdb_assert (stream != nullptr);
253 return stream;
254 }
255
256 /* Format disassembler output to STREAM. */
257
258 int
259 gdb_printing_disassembler::fprintf_func (void *dis_info,
260 const char *format, ...) noexcept
261 {
262 ui_file *stream = stream_from_gdb_disassemble_info (dis_info);
263
264 va_list args;
265 va_start (args, format);
266 gdb_vprintf (stream, format, args);
267 va_end (args);
268
269 /* Something non -ve. */
270 return 0;
271 }
272
273 /* See disasm.h. */
274
275 int
276 gdb_printing_disassembler::fprintf_styled_func
277 (void *dis_info, enum disassembler_style style,
278 const char *format, ...) noexcept
279 {
280 ui_file *stream = stream_from_gdb_disassemble_info (dis_info);
281 gdb_printing_disassembler *dis = (gdb_printing_disassembler *) dis_info;
282
283 va_list args;
284 va_start (args, format);
285 std::string content = string_vprintf (format, args);
286 va_end (args);
287
288 /* Once in a comment then everything should be styled as a comment. */
289 if (style == dis_style_comment_start)
290 dis->set_in_comment (true);
291 if (dis->in_comment_p ())
292 style = dis_style_comment_start;
293
294 /* Now print the content with the correct style. */
295 const char *txt = content.c_str ();
296 switch (style)
297 {
298 case dis_style_mnemonic:
299 case dis_style_sub_mnemonic:
300 case dis_style_assembler_directive:
301 fputs_styled (txt, disasm_mnemonic_style.style (), stream);
302 break;
303
304 case dis_style_register:
305 fputs_styled (txt, disasm_register_style.style (), stream);
306 break;
307
308 case dis_style_immediate:
309 case dis_style_address_offset:
310 fputs_styled (txt, disasm_immediate_style.style (), stream);
311 break;
312
313 case dis_style_address:
314 fputs_styled (txt, address_style.style (), stream);
315 break;
316
317 case dis_style_symbol:
318 fputs_styled (txt, function_name_style.style (), stream);
319 break;
320
321 case dis_style_comment_start:
322 fputs_styled (txt, disasm_comment_style.style (), stream);
323 break;
324
325 case dis_style_text:
326 gdb_puts (txt, stream);
327 break;
328 }
329
330 /* Something non -ve. */
331 return 0;
332 }
333
334 static bool
335 line_is_less_than (const deprecated_dis_line_entry &mle1,
336 const deprecated_dis_line_entry &mle2)
337 {
338 bool val;
339
340 /* End of sequence markers have a line number of 0 but don't want to
341 be sorted to the head of the list, instead sort by PC. */
342 if (mle1.line == 0 || mle2.line == 0)
343 {
344 if (mle1.start_pc != mle2.start_pc)
345 val = mle1.start_pc < mle2.start_pc;
346 else
347 val = mle1.line < mle2.line;
348 }
349 else
350 {
351 if (mle1.line != mle2.line)
352 val = mle1.line < mle2.line;
353 else
354 val = mle1.start_pc < mle2.start_pc;
355 }
356 return val;
357 }
358
359 /* See disasm.h. */
360
361 int
362 gdb_pretty_print_disassembler::pretty_print_insn (const struct disasm_insn *insn,
363 gdb_disassembly_flags flags)
364 {
365 /* parts of the symbolic representation of the address */
366 int unmapped;
367 int offset;
368 int line;
369 int size;
370 CORE_ADDR pc;
371 struct gdbarch *gdbarch = arch ();
372
373 {
374 ui_out_emit_tuple tuple_emitter (m_uiout, NULL);
375 pc = insn->addr;
376
377 if (insn->number != 0)
378 {
379 m_uiout->field_unsigned ("insn-number", insn->number);
380 m_uiout->text ("\t");
381 }
382
383 if ((flags & DISASSEMBLY_SPECULATIVE) != 0)
384 {
385 if (insn->is_speculative)
386 {
387 m_uiout->field_string ("is-speculative", "?");
388
389 /* The speculative execution indication overwrites the first
390 character of the PC prefix.
391 We assume a PC prefix length of 3 characters. */
392 if ((flags & DISASSEMBLY_OMIT_PC) == 0)
393 m_uiout->text (pc_prefix (pc) + 1);
394 else
395 m_uiout->text (" ");
396 }
397 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
398 m_uiout->text (pc_prefix (pc));
399 else
400 m_uiout->text (" ");
401 }
402 else if ((flags & DISASSEMBLY_OMIT_PC) == 0)
403 m_uiout->text (pc_prefix (pc));
404 m_uiout->field_core_addr ("address", gdbarch, pc);
405
406 std::string name, filename;
407 bool omit_fname = ((flags & DISASSEMBLY_OMIT_FNAME) != 0);
408 if (!build_address_symbolic (gdbarch, pc, false, omit_fname, &name,
409 &offset, &filename, &line, &unmapped))
410 {
411 /* We don't care now about line, filename and unmapped. But we might in
412 the future. */
413 m_uiout->text (" <");
414 if (!omit_fname)
415 m_uiout->field_string ("func-name", name,
416 function_name_style.style ());
417 /* For negative offsets, avoid displaying them as +-N; the sign of
418 the offset takes the place of the "+" here. */
419 if (offset >= 0)
420 m_uiout->text ("+");
421 m_uiout->field_signed ("offset", offset);
422 m_uiout->text (">:\t");
423 }
424 else
425 m_uiout->text (":\t");
426
427 /* Clear the buffer into which we will disassemble the instruction. */
428 m_insn_stb.clear ();
429
430 /* A helper function to write the M_INSN_STB buffer, followed by a
431 newline. This can be called in a couple of situations. */
432 auto write_out_insn_buffer = [&] ()
433 {
434 m_uiout->field_stream ("inst", m_insn_stb);
435 m_uiout->text ("\n");
436 };
437
438 try
439 {
440 /* Now we can disassemble the instruction. If the disassembler
441 returns a negative value this indicates an error and is handled
442 within the print_insn call, resulting in an exception being
443 thrown. Returning zero makes no sense, as this indicates we
444 disassembled something successfully, but it was something of no
445 size? */
446 size = m_di.print_insn (pc);
447 gdb_assert (size > 0);
448 }
449 catch (const gdb_exception &)
450 {
451 /* An exception was thrown while disassembling the instruction.
452 However, the disassembler might still have written something
453 out, so ensure that we flush the instruction buffer before
454 rethrowing the exception. We can't perform this write from an
455 object destructor as the write itself might throw an exception
456 if the pager kicks in, and the user selects quit. */
457 write_out_insn_buffer ();
458 throw;
459 }
460
461 if ((flags & (DISASSEMBLY_RAW_INSN | DISASSEMBLY_RAW_BYTES)) != 0)
462 {
463 /* Build the opcodes using a temporary stream so we can
464 write them out in a single go for the MI. */
465 m_opcode_stb.clear ();
466
467 /* Read the instruction opcode data. */
468 m_opcode_data.resize (size);
469 read_code (pc, m_opcode_data.data (), size);
470
471 /* The disassembler provides information about the best way to
472 display the instruction bytes to the user. We provide some sane
473 defaults in case the disassembler gets it wrong. */
474 const struct disassemble_info *di = m_di.disasm_info ();
475 int bytes_per_line = std::max (di->bytes_per_line, size);
476 int bytes_per_chunk = std::max (di->bytes_per_chunk, 1);
477
478 /* If the user has requested the instruction bytes be displayed
479 byte at a time, then handle that here. Also, if the instruction
480 is not a multiple of the chunk size (which probably indicates a
481 disassembler problem) then avoid that causing display problems
482 by switching to byte at a time mode. */
483 if ((flags & DISASSEMBLY_RAW_BYTES) != 0
484 || (size % bytes_per_chunk) != 0)
485 bytes_per_chunk = 1;
486
487 /* Print the instruction opcodes bytes, grouped into chunks. */
488 for (int i = 0; i < size; i += bytes_per_chunk)
489 {
490 if (i > 0)
491 m_opcode_stb.puts (" ");
492
493 if (di->display_endian == BFD_ENDIAN_LITTLE)
494 {
495 for (int k = bytes_per_chunk; k-- != 0; )
496 m_opcode_stb.printf ("%02x", (unsigned) m_opcode_data[i + k]);
497 }
498 else
499 {
500 for (int k = 0; k < bytes_per_chunk; k++)
501 m_opcode_stb.printf ("%02x", (unsigned) m_opcode_data[i + k]);
502 }
503 }
504
505 /* Calculate required padding. */
506 int nspaces = 0;
507 for (int i = size; i < bytes_per_line; i += bytes_per_chunk)
508 {
509 if (i > size)
510 nspaces++;
511 nspaces += bytes_per_chunk * 2;
512 }
513
514 m_uiout->field_stream ("opcodes", m_opcode_stb);
515 m_uiout->spaces (nspaces);
516 m_uiout->text ("\t");
517 }
518
519 /* Disassembly was a success, write out the instruction buffer. */
520 write_out_insn_buffer ();
521 }
522
523 return size;
524 }
525
526 static int
527 dump_insns (struct gdbarch *gdbarch,
528 struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
529 int how_many, gdb_disassembly_flags flags, CORE_ADDR *end_pc)
530 {
531 struct disasm_insn insn;
532 int num_displayed = 0;
533
534 memset (&insn, 0, sizeof (insn));
535 insn.addr = low;
536
537 gdb_pretty_print_disassembler disasm (gdbarch, uiout);
538
539 while (insn.addr < high && (how_many < 0 || num_displayed < how_many))
540 {
541 int size;
542
543 size = disasm.pretty_print_insn (&insn, flags);
544 if (size <= 0)
545 break;
546
547 ++num_displayed;
548 insn.addr += size;
549
550 /* Allow user to bail out with ^C. */
551 QUIT;
552 }
553
554 if (end_pc != NULL)
555 *end_pc = insn.addr;
556
557 return num_displayed;
558 }
559
560 /* The idea here is to present a source-O-centric view of a
561 function to the user. This means that things are presented
562 in source order, with (possibly) out of order assembly
563 immediately following.
564
565 N.B. This view is deprecated. */
566
567 static void
568 do_mixed_source_and_assembly_deprecated
569 (struct gdbarch *gdbarch, struct ui_out *uiout,
570 struct symtab *symtab,
571 CORE_ADDR low, CORE_ADDR high,
572 int how_many, gdb_disassembly_flags flags)
573 {
574 int newlines = 0;
575 int nlines;
576 const struct linetable_entry *le;
577 struct deprecated_dis_line_entry *mle;
578 struct symtab_and_line sal;
579 int i;
580 int out_of_order = 0;
581 int next_line = 0;
582 int num_displayed = 0;
583 print_source_lines_flags psl_flags = 0;
584
585 gdb_assert (symtab != nullptr && symtab->linetable () != nullptr);
586
587 nlines = symtab->linetable ()->nitems;
588 le = symtab->linetable ()->item;
589
590 if (flags & DISASSEMBLY_FILENAME)
591 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
592
593 mle = (struct deprecated_dis_line_entry *)
594 alloca (nlines * sizeof (struct deprecated_dis_line_entry));
595
596 struct objfile *objfile = symtab->compunit ()->objfile ();
597
598 unrelocated_addr unrel_low
599 = unrelocated_addr (low - objfile->text_section_offset ());
600 unrelocated_addr unrel_high
601 = unrelocated_addr (high - objfile->text_section_offset ());
602
603 /* Copy linetable entries for this function into our data
604 structure, creating end_pc's and setting out_of_order as
605 appropriate. */
606
607 /* First, skip all the preceding functions. */
608
609 for (i = 0; i < nlines - 1 && le[i].raw_pc () < unrel_low; i++);
610
611 /* Now, copy all entries before the end of this function. */
612
613 for (; i < nlines - 1 && le[i].raw_pc () < unrel_high; i++)
614 {
615 if (le[i] == le[i + 1])
616 continue; /* Ignore duplicates. */
617
618 /* Skip any end-of-function markers. */
619 if (le[i].line == 0)
620 continue;
621
622 mle[newlines].line = le[i].line;
623 if (le[i].line > le[i + 1].line)
624 out_of_order = 1;
625 mle[newlines].start_pc = le[i].pc (objfile);
626 mle[newlines].end_pc = le[i + 1].pc (objfile);
627 newlines++;
628 }
629
630 /* If we're on the last line, and it's part of the function,
631 then we need to get the end pc in a special way. */
632
633 if (i == nlines - 1 && le[i].raw_pc () < unrel_high)
634 {
635 mle[newlines].line = le[i].line;
636 mle[newlines].start_pc = le[i].pc (objfile);
637 sal = find_pc_line (le[i].pc (objfile), 0);
638 mle[newlines].end_pc = sal.end;
639 newlines++;
640 }
641
642 /* Now, sort mle by line #s (and, then by addresses within lines). */
643
644 if (out_of_order)
645 std::sort (mle, mle + newlines, line_is_less_than);
646
647 /* Now, for each line entry, emit the specified lines (unless
648 they have been emitted before), followed by the assembly code
649 for that line. */
650
651 ui_out_emit_list asm_insns_list (uiout, "asm_insns");
652
653 gdb::optional<ui_out_emit_tuple> outer_tuple_emitter;
654 gdb::optional<ui_out_emit_list> inner_list_emitter;
655
656 for (i = 0; i < newlines; i++)
657 {
658 /* Print out everything from next_line to the current line. */
659 if (mle[i].line >= next_line)
660 {
661 if (next_line != 0)
662 {
663 /* Just one line to print. */
664 if (next_line == mle[i].line)
665 {
666 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
667 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
668 }
669 else
670 {
671 /* Several source lines w/o asm instructions associated. */
672 for (; next_line < mle[i].line; next_line++)
673 {
674 ui_out_emit_tuple tuple_emitter (uiout,
675 "src_and_asm_line");
676 print_source_lines (symtab, next_line, next_line + 1,
677 psl_flags);
678 ui_out_emit_list temp_list_emitter (uiout,
679 "line_asm_insn");
680 }
681 /* Print the last line and leave list open for
682 asm instructions to be added. */
683 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
684 print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
685 }
686 }
687 else
688 {
689 outer_tuple_emitter.emplace (uiout, "src_and_asm_line");
690 print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
691 }
692
693 next_line = mle[i].line + 1;
694 inner_list_emitter.emplace (uiout, "line_asm_insn");
695 }
696
697 num_displayed += dump_insns (gdbarch, uiout,
698 mle[i].start_pc, mle[i].end_pc,
699 how_many, flags, NULL);
700
701 /* When we've reached the end of the mle array, or we've seen the last
702 assembly range for this source line, close out the list/tuple. */
703 if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
704 {
705 inner_list_emitter.reset ();
706 outer_tuple_emitter.reset ();
707 uiout->text ("\n");
708 }
709 if (how_many >= 0 && num_displayed >= how_many)
710 break;
711 }
712 }
713
714 /* The idea here is to present a source-O-centric view of a
715 function to the user. This means that things are presented
716 in source order, with (possibly) out of order assembly
717 immediately following. */
718
719 static void
720 do_mixed_source_and_assembly (struct gdbarch *gdbarch,
721 struct ui_out *uiout,
722 struct symtab *main_symtab,
723 CORE_ADDR low, CORE_ADDR high,
724 int how_many, gdb_disassembly_flags flags)
725 {
726 const struct linetable_entry *le, *first_le;
727 int i, nlines;
728 int num_displayed = 0;
729 print_source_lines_flags psl_flags = 0;
730 CORE_ADDR pc;
731 struct symtab *last_symtab;
732 int last_line;
733
734 gdb_assert (main_symtab != NULL && main_symtab->linetable () != NULL);
735
736 /* First pass: collect the list of all source files and lines.
737 We do this so that we can only print lines containing code once.
738 We try to print the source text leading up to the next instruction,
739 but if that text is for code that will be disassembled later, then
740 we'll want to defer printing it until later with its associated code. */
741
742 htab_up dis_line_table (allocate_dis_line_table ());
743
744 struct objfile *objfile = main_symtab->compunit ()->objfile ();
745
746 unrelocated_addr unrel_low
747 = unrelocated_addr (low - objfile->text_section_offset ());
748 unrelocated_addr unrel_high
749 = unrelocated_addr (high - objfile->text_section_offset ());
750
751 pc = low;
752
753 /* The prologue may be empty, but there may still be a line number entry
754 for the opening brace which is distinct from the first line of code.
755 If the prologue has been eliminated find_pc_line may return the source
756 line after the opening brace. We still want to print this opening brace.
757 first_le is used to implement this. */
758
759 nlines = main_symtab->linetable ()->nitems;
760 le = main_symtab->linetable ()->item;
761 first_le = NULL;
762
763 /* Skip all the preceding functions. */
764 for (i = 0; i < nlines && le[i].raw_pc () < unrel_low; i++)
765 continue;
766
767 if (i < nlines && le[i].raw_pc () < unrel_high)
768 first_le = &le[i];
769
770 /* Add lines for every pc value. */
771 while (pc < high)
772 {
773 struct symtab_and_line sal;
774 int length;
775
776 sal = find_pc_line (pc, 0);
777 length = gdb_insn_length (gdbarch, pc);
778 pc += length;
779
780 if (sal.symtab != NULL)
781 add_dis_line_entry (dis_line_table.get (), sal.symtab, sal.line);
782 }
783
784 /* Second pass: print the disassembly.
785
786 Output format, from an MI perspective:
787 The result is a ui_out list, field name "asm_insns", where elements have
788 name "src_and_asm_line".
789 Each element is a tuple of source line specs (field names line, file,
790 fullname), and field "line_asm_insn" which contains the disassembly.
791 Field "line_asm_insn" is a list of tuples: address, func-name, offset,
792 opcodes, inst.
793
794 CLI output works on top of this because MI ignores ui_out_text output,
795 which is where we put file name and source line contents output.
796
797 Emitter usage:
798 asm_insns_emitter
799 Handles the outer "asm_insns" list.
800 tuple_emitter
801 The tuples for each group of consecutive disassemblies.
802 list_emitter
803 List of consecutive source lines or disassembled insns. */
804
805 if (flags & DISASSEMBLY_FILENAME)
806 psl_flags |= PRINT_SOURCE_LINES_FILENAME;
807
808 ui_out_emit_list asm_insns_emitter (uiout, "asm_insns");
809
810 gdb::optional<ui_out_emit_tuple> tuple_emitter;
811 gdb::optional<ui_out_emit_list> list_emitter;
812
813 last_symtab = NULL;
814 last_line = 0;
815 pc = low;
816
817 while (pc < high)
818 {
819 struct symtab_and_line sal;
820 CORE_ADDR end_pc;
821 int start_preceding_line_to_display = 0;
822 int end_preceding_line_to_display = 0;
823 int new_source_line = 0;
824
825 sal = find_pc_line (pc, 0);
826
827 if (sal.symtab != last_symtab)
828 {
829 /* New source file. */
830 new_source_line = 1;
831
832 /* If this is the first line of output, check for any preceding
833 lines. */
834 if (last_line == 0
835 && first_le != NULL
836 && first_le->line < sal.line)
837 {
838 start_preceding_line_to_display = first_le->line;
839 end_preceding_line_to_display = sal.line;
840 }
841 }
842 else
843 {
844 /* Same source file as last time. */
845 if (sal.symtab != NULL)
846 {
847 if (sal.line > last_line + 1 && last_line != 0)
848 {
849 int l;
850
851 /* Several preceding source lines. Print the trailing ones
852 not associated with code that we'll print later. */
853 for (l = sal.line - 1; l > last_line; --l)
854 {
855 if (line_has_code_p (dis_line_table.get (),
856 sal.symtab, l))
857 break;
858 }
859 if (l < sal.line - 1)
860 {
861 start_preceding_line_to_display = l + 1;
862 end_preceding_line_to_display = sal.line;
863 }
864 }
865 if (sal.line != last_line)
866 new_source_line = 1;
867 else
868 {
869 /* Same source line as last time. This can happen, depending
870 on the debug info. */
871 }
872 }
873 }
874
875 if (new_source_line)
876 {
877 /* Skip the newline if this is the first instruction. */
878 if (pc > low)
879 uiout->text ("\n");
880 if (tuple_emitter.has_value ())
881 {
882 gdb_assert (list_emitter.has_value ());
883 list_emitter.reset ();
884 tuple_emitter.reset ();
885 }
886 if (sal.symtab != last_symtab
887 && !(flags & DISASSEMBLY_FILENAME))
888 {
889 /* Remember MI ignores ui_out_text.
890 We don't have to do anything here for MI because MI
891 output includes the source specs for each line. */
892 if (sal.symtab != NULL)
893 {
894 uiout->text (symtab_to_filename_for_display (sal.symtab));
895 }
896 else
897 uiout->text ("unknown");
898 uiout->text (":\n");
899 }
900 if (start_preceding_line_to_display > 0)
901 {
902 /* Several source lines w/o asm instructions associated.
903 We need to preserve the structure of the output, so output
904 a bunch of line tuples with no asm entries. */
905 int l;
906
907 gdb_assert (sal.symtab != NULL);
908 for (l = start_preceding_line_to_display;
909 l < end_preceding_line_to_display;
910 ++l)
911 {
912 ui_out_emit_tuple line_tuple_emitter (uiout,
913 "src_and_asm_line");
914 print_source_lines (sal.symtab, l, l + 1, psl_flags);
915 ui_out_emit_list chain_line_emitter (uiout, "line_asm_insn");
916 }
917 }
918 tuple_emitter.emplace (uiout, "src_and_asm_line");
919 if (sal.symtab != NULL)
920 print_source_lines (sal.symtab, sal.line, sal.line + 1, psl_flags);
921 else
922 uiout->text (_("--- no source info for this pc ---\n"));
923 list_emitter.emplace (uiout, "line_asm_insn");
924 }
925 else
926 {
927 /* Here we're appending instructions to an existing line.
928 By construction the very first insn will have a symtab
929 and follow the new_source_line path above. */
930 gdb_assert (tuple_emitter.has_value ());
931 gdb_assert (list_emitter.has_value ());
932 }
933
934 if (sal.end != 0)
935 end_pc = std::min (sal.end, high);
936 else
937 end_pc = pc + 1;
938 num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
939 how_many, flags, &end_pc);
940 pc = end_pc;
941
942 if (how_many >= 0 && num_displayed >= how_many)
943 break;
944
945 last_symtab = sal.symtab;
946 last_line = sal.line;
947 }
948 }
949
950 static void
951 do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
952 CORE_ADDR low, CORE_ADDR high,
953 int how_many, gdb_disassembly_flags flags)
954 {
955 ui_out_emit_list list_emitter (uiout, "asm_insns");
956
957 dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
958 }
959
960 /* Combine implicit and user disassembler options and return them
961 in a newly-created string. */
962
963 static std::string
964 get_all_disassembler_options (struct gdbarch *gdbarch)
965 {
966 const char *implicit = gdbarch_disassembler_options_implicit (gdbarch);
967 const char *options = get_disassembler_options (gdbarch);
968 const char *comma = ",";
969
970 if (implicit == nullptr)
971 {
972 implicit = "";
973 comma = "";
974 }
975
976 if (options == nullptr)
977 {
978 options = "";
979 comma = "";
980 }
981
982 return string_printf ("%s%s%s", implicit, comma, options);
983 }
984
985 gdb_disassembler::gdb_disassembler (struct gdbarch *gdbarch,
986 struct ui_file *file,
987 read_memory_ftype func)
988 : gdb_printing_disassembler (gdbarch, &m_buffer, func,
989 dis_asm_memory_error, dis_asm_print_address),
990 m_dest (file),
991 m_buffer (!use_ext_lang_for_styling () && use_libopcodes_for_styling ())
992 { /* Nothing. */ }
993
994 /* See disasm.h. */
995
996 bool
997 gdb_disassembler::use_ext_lang_for_styling () const
998 {
999 /* The use of m_di.created_styled_output here is a bit of a cheat, but
1000 it works fine for now.
1001
1002 This function is called in situations after m_di has been initialized,
1003 but before the instruction has been disassembled.
1004
1005 Currently, every target that supports libopcodes styling sets the
1006 created_styled_output field in disassemble_init_for_target, which was
1007 called as part of the initialization of gdb_printing_disassembler.
1008
1009 This means that we are OK to check the created_styled_output field
1010 here.
1011
1012 If, in the future, there's ever a target that only sets the
1013 created_styled_output field during the actual instruction disassembly
1014 phase, then we will need to update this code. */
1015 return (disassembler_styling
1016 && (!m_di.created_styled_output || !use_libopcodes_styling)
1017 && use_ext_lang_colorization_p
1018 && m_dest->can_emit_style_escape ());
1019 }
1020
1021 /* See disasm.h. */
1022
1023 bool
1024 gdb_disassembler::use_libopcodes_for_styling () const
1025 {
1026 /* See the comment on the use of m_di.created_styled_output in the
1027 gdb_disassembler::use_ext_lang_for_styling function. */
1028 return (disassembler_styling
1029 && m_di.created_styled_output
1030 && use_libopcodes_styling
1031 && m_dest->can_emit_style_escape ());
1032 }
1033
1034 /* See disasm.h. */
1035
1036 gdb_disassemble_info::gdb_disassemble_info
1037 (struct gdbarch *gdbarch,
1038 read_memory_ftype read_memory_func, memory_error_ftype memory_error_func,
1039 print_address_ftype print_address_func, fprintf_ftype fprintf_func,
1040 fprintf_styled_ftype fprintf_styled_func)
1041 : m_gdbarch (gdbarch)
1042 {
1043 gdb_assert (fprintf_func != nullptr);
1044 gdb_assert (fprintf_styled_func != nullptr);
1045 init_disassemble_info (&m_di, (void *) this, fprintf_func,
1046 fprintf_styled_func);
1047 m_di.flavour = bfd_target_unknown_flavour;
1048
1049 /* The memory_error_func, print_address_func, and read_memory_func are
1050 all initialized to a default (non-nullptr) value by the call to
1051 init_disassemble_info above. If the user is overriding these fields
1052 (by passing non-nullptr values) then do that now, otherwise, leave
1053 these fields as the defaults. */
1054 if (memory_error_func != nullptr)
1055 m_di.memory_error_func = memory_error_func;
1056 if (print_address_func != nullptr)
1057 m_di.print_address_func = print_address_func;
1058 if (read_memory_func != nullptr)
1059 m_di.read_memory_func = read_memory_func;
1060
1061 m_di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
1062 m_di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
1063 m_di.endian = gdbarch_byte_order (gdbarch);
1064 m_di.endian_code = gdbarch_byte_order_for_code (gdbarch);
1065 m_di.application_data = this;
1066 m_disassembler_options_holder = get_all_disassembler_options (gdbarch);
1067 if (!m_disassembler_options_holder.empty ())
1068 m_di.disassembler_options = m_disassembler_options_holder.c_str ();
1069 disassemble_init_for_target (&m_di);
1070 }
1071
1072 /* See disasm.h. */
1073
1074 gdb_disassemble_info::~gdb_disassemble_info ()
1075 {
1076 disassemble_free_target (&m_di);
1077 }
1078
1079 /* Wrapper around calling gdbarch_print_insn. This function takes care of
1080 first calling the extension language hooks for print_insn, and, if none
1081 of the extension languages can print this instruction, calls
1082 gdbarch_print_insn to do the work.
1083
1084 GDBARCH is the architecture to disassemble in, VMA is the address of the
1085 instruction being disassembled, and INFO is the libopcodes disassembler
1086 related information. */
1087
1088 static int
1089 gdb_print_insn_1 (struct gdbarch *gdbarch, CORE_ADDR vma,
1090 struct disassemble_info *info)
1091 {
1092 /* Call into the extension languages to do the disassembly. */
1093 gdb::optional<int> length = ext_lang_print_insn (gdbarch, vma, info);
1094 if (length.has_value ())
1095 return *length;
1096
1097 /* No extension language wanted to do the disassembly, so do it
1098 manually. */
1099 return gdbarch_print_insn (gdbarch, vma, info);
1100 }
1101
1102 /* See disasm.h. */
1103
1104 bool gdb_disassembler::use_ext_lang_colorization_p = true;
1105
1106 /* See disasm.h. */
1107
1108 int
1109 gdb_disassembler::print_insn (CORE_ADDR memaddr,
1110 int *branch_delay_insns)
1111 {
1112 m_err_memaddr.reset ();
1113 m_buffer.clear ();
1114 this->set_in_comment (false);
1115
1116 int length = gdb_print_insn_1 (arch (), memaddr, &m_di);
1117
1118 /* If we have successfully disassembled an instruction, disassembler
1119 styling using the extension language is on, and libopcodes hasn't
1120 already styled the output for us, and, if the destination can support
1121 styling, then lets call into the extension languages in order to style
1122 this output. */
1123 if (length > 0 && use_ext_lang_for_styling ())
1124 {
1125 gdb::optional<std::string> ext_contents;
1126 ext_contents = ext_lang_colorize_disasm (m_buffer.string (), arch ());
1127 if (ext_contents.has_value ())
1128 m_buffer = std::move (*ext_contents);
1129 else
1130 {
1131 /* The extension language failed to add styling to the
1132 disassembly output. Set the static flag so that next time we
1133 disassemble we don't even bother attempting to use the
1134 extension language for styling. */
1135 use_ext_lang_colorization_p = false;
1136
1137 /* We're about to disassemble this instruction again, reset the
1138 in-comment state. */
1139 this->set_in_comment (false);
1140
1141 /* The instruction we just disassembled, and the extension
1142 languages failed to style, might have otherwise had some
1143 minimal styling applied by GDB. To regain that styling we
1144 need to recreate m_buffer, but this time with styling support.
1145
1146 To do this we perform an in-place new, but this time turn on
1147 the styling support, then we can re-disassembly the
1148 instruction, and gain any minimal styling GDB might add. */
1149 gdb_static_assert ((std::is_same<decltype (m_buffer),
1150 string_file>::value));
1151 gdb_assert (!m_buffer.term_out ());
1152 m_buffer.~string_file ();
1153 new (&m_buffer) string_file (use_libopcodes_for_styling ());
1154 length = gdb_print_insn_1 (arch (), memaddr, &m_di);
1155 gdb_assert (length > 0);
1156 }
1157 }
1158
1159 /* Push any disassemble output to the real destination stream. We do
1160 this even if the disassembler reported failure (-1) as the
1161 disassembler may have printed something to its output stream. */
1162 gdb_printf (m_dest, "%s", m_buffer.c_str ());
1163
1164 /* If the disassembler failed then report an appropriate error. */
1165 if (length < 0)
1166 {
1167 if (m_err_memaddr.has_value ())
1168 memory_error (TARGET_XFER_E_IO, *m_err_memaddr);
1169 else
1170 error (_("unknown disassembler error (error = %d)"), length);
1171 }
1172
1173 if (branch_delay_insns != NULL)
1174 {
1175 if (m_di.insn_info_valid)
1176 *branch_delay_insns = m_di.branch_delay_insns;
1177 else
1178 *branch_delay_insns = 0;
1179 }
1180 return length;
1181 }
1182
1183 void
1184 gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
1185 gdb_disassembly_flags flags, int how_many,
1186 CORE_ADDR low, CORE_ADDR high)
1187 {
1188 struct symtab *symtab;
1189 int nlines = -1;
1190
1191 /* Assume symtab is valid for whole PC range. */
1192 symtab = find_pc_line_symtab (low);
1193
1194 if (symtab != NULL && symtab->linetable () != NULL)
1195 nlines = symtab->linetable ()->nitems;
1196
1197 if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
1198 || nlines <= 0)
1199 do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
1200
1201 else if (flags & DISASSEMBLY_SOURCE)
1202 do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
1203 how_many, flags);
1204
1205 else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
1206 do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
1207 low, high, how_many, flags);
1208
1209 gdb_flush (gdb_stdout);
1210 }
1211
1212 /* Print the instruction at address MEMADDR in debugged memory,
1213 on STREAM. Returns the length of the instruction, in bytes,
1214 and, if requested, the number of branch delay slot instructions. */
1215
1216 int
1217 gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
1218 struct ui_file *stream, int *branch_delay_insns)
1219 {
1220
1221 gdb_disassembler di (gdbarch, stream);
1222
1223 return di.print_insn (memaddr, branch_delay_insns);
1224 }
1225
1226 /* Return the length in bytes of the instruction at address MEMADDR in
1227 debugged memory. */
1228
1229 int
1230 gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
1231 {
1232 return gdb_print_insn (gdbarch, addr, &null_stream, NULL);
1233 }
1234
1235 /* See disasm.h. */
1236
1237 int
1238 gdb_non_printing_disassembler::null_fprintf_func
1239 (void *stream, const char *format, ...) noexcept
1240 {
1241 return 0;
1242 }
1243
1244 /* See disasm.h. */
1245
1246 int
1247 gdb_non_printing_disassembler::null_fprintf_styled_func
1248 (void *stream, enum disassembler_style style,
1249 const char *format, ...) noexcept
1250 {
1251 return 0;
1252 }
1253
1254 /* A non-printing disassemble_info management class. The disassemble_info
1255 setup by this class will not print anything to the output stream (there
1256 is no output stream), and the instruction to be disassembled will be
1257 read from a buffer passed to the constructor. */
1258
1259 struct gdb_non_printing_buffer_disassembler
1260 : public gdb_non_printing_disassembler
1261 {
1262 /* Constructor. GDBARCH is the architecture to disassemble for, BUFFER
1263 contains the instruction to disassemble, and INSN_ADDRESS is the
1264 address (in target memory) of the instruction to disassemble. */
1265 gdb_non_printing_buffer_disassembler (struct gdbarch *gdbarch,
1266 gdb::array_view<const gdb_byte> buffer,
1267 CORE_ADDR insn_address)
1268 : gdb_non_printing_disassembler (gdbarch, nullptr)
1269 {
1270 /* The cast is necessary until disassemble_info is const-ified. */
1271 m_di.buffer = (gdb_byte *) buffer.data ();
1272 m_di.buffer_length = buffer.size ();
1273 m_di.buffer_vma = insn_address;
1274 }
1275 };
1276
1277 /* Return the length in bytes of INSN. MAX_LEN is the size of the
1278 buffer containing INSN. */
1279
1280 int
1281 gdb_buffered_insn_length (struct gdbarch *gdbarch,
1282 const gdb_byte *insn, int max_len, CORE_ADDR addr)
1283 {
1284 gdb::array_view<const gdb_byte> buffer
1285 = gdb::make_array_view (insn, max_len);
1286 gdb_non_printing_buffer_disassembler dis (gdbarch, buffer, addr);
1287 int result = gdb_print_insn_1 (gdbarch, addr, dis.disasm_info ());
1288 return result;
1289 }
1290
1291 char *
1292 get_disassembler_options (struct gdbarch *gdbarch)
1293 {
1294 char **disassembler_options = gdbarch_disassembler_options (gdbarch);
1295 if (disassembler_options == NULL)
1296 return NULL;
1297 return *disassembler_options;
1298 }
1299
1300 void
1301 set_disassembler_options (const char *prospective_options)
1302 {
1303 struct gdbarch *gdbarch = get_current_arch ();
1304 char **disassembler_options = gdbarch_disassembler_options (gdbarch);
1305 const disasm_options_and_args_t *valid_options_and_args;
1306 const disasm_options_t *valid_options;
1307 gdb::unique_xmalloc_ptr<char> prospective_options_local
1308 = make_unique_xstrdup (prospective_options);
1309 char *options = remove_whitespace_and_extra_commas
1310 (prospective_options_local.get ());
1311 const char *opt;
1312
1313 /* Allow all architectures, even ones that do not support 'set disassembler',
1314 to reset their disassembler options to NULL. */
1315 if (options == NULL)
1316 {
1317 if (disassembler_options != NULL)
1318 {
1319 free (*disassembler_options);
1320 *disassembler_options = NULL;
1321 }
1322 return;
1323 }
1324
1325 valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
1326 if (valid_options_and_args == NULL)
1327 {
1328 gdb_printf (gdb_stderr, _("\
1329 'set disassembler-options ...' is not supported on this architecture.\n"));
1330 return;
1331 }
1332
1333 valid_options = &valid_options_and_args->options;
1334
1335 /* Verify we have valid disassembler options. */
1336 FOR_EACH_DISASSEMBLER_OPTION (opt, options)
1337 {
1338 size_t i;
1339 for (i = 0; valid_options->name[i] != NULL; i++)
1340 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1341 {
1342 size_t len = strlen (valid_options->name[i]);
1343 bool found = false;
1344 const char *arg;
1345 size_t j;
1346
1347 if (memcmp (opt, valid_options->name[i], len) != 0)
1348 continue;
1349 arg = opt + len;
1350 if (valid_options->arg[i]->values == NULL)
1351 break;
1352 for (j = 0; valid_options->arg[i]->values[j] != NULL; j++)
1353 if (disassembler_options_cmp
1354 (arg, valid_options->arg[i]->values[j]) == 0)
1355 {
1356 found = true;
1357 break;
1358 }
1359 if (found)
1360 break;
1361 }
1362 else if (disassembler_options_cmp (opt, valid_options->name[i]) == 0)
1363 break;
1364 if (valid_options->name[i] == NULL)
1365 {
1366 gdb_printf (gdb_stderr,
1367 _("Invalid disassembler option value: '%s'.\n"),
1368 opt);
1369 return;
1370 }
1371 }
1372
1373 free (*disassembler_options);
1374 *disassembler_options = xstrdup (options);
1375 }
1376
1377 static void
1378 set_disassembler_options_sfunc (const char *args, int from_tty,
1379 struct cmd_list_element *c)
1380 {
1381 set_disassembler_options (prospective_options.c_str ());
1382 }
1383
1384 static void
1385 show_disassembler_options_sfunc (struct ui_file *file, int from_tty,
1386 struct cmd_list_element *c, const char *value)
1387 {
1388 struct gdbarch *gdbarch = get_current_arch ();
1389 const disasm_options_and_args_t *valid_options_and_args;
1390 const disasm_option_arg_t *valid_args;
1391 const disasm_options_t *valid_options;
1392
1393 const char *options = get_disassembler_options (gdbarch);
1394 if (options == NULL)
1395 options = "";
1396
1397 gdb_printf (file, _("The current disassembler options are '%s'\n\n"),
1398 options);
1399
1400 valid_options_and_args = gdbarch_valid_disassembler_options (gdbarch);
1401
1402 if (valid_options_and_args == NULL)
1403 {
1404 gdb_puts (_("There are no disassembler options available "
1405 "for this architecture.\n"),
1406 file);
1407 return;
1408 }
1409
1410 valid_options = &valid_options_and_args->options;
1411
1412 gdb_printf (file, _("\
1413 The following disassembler options are supported for use with the\n\
1414 'set disassembler-options OPTION [,OPTION]...' command:\n"));
1415
1416 if (valid_options->description != NULL)
1417 {
1418 size_t i, max_len = 0;
1419
1420 gdb_printf (file, "\n");
1421
1422 /* Compute the length of the longest option name. */
1423 for (i = 0; valid_options->name[i] != NULL; i++)
1424 {
1425 size_t len = strlen (valid_options->name[i]);
1426
1427 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1428 len += strlen (valid_options->arg[i]->name);
1429 if (max_len < len)
1430 max_len = len;
1431 }
1432
1433 for (i = 0, max_len++; valid_options->name[i] != NULL; i++)
1434 {
1435 gdb_printf (file, " %s", valid_options->name[i]);
1436 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1437 gdb_printf (file, "%s", valid_options->arg[i]->name);
1438 if (valid_options->description[i] != NULL)
1439 {
1440 size_t len = strlen (valid_options->name[i]);
1441
1442 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1443 len += strlen (valid_options->arg[i]->name);
1444 gdb_printf (file, "%*c %s", (int) (max_len - len), ' ',
1445 valid_options->description[i]);
1446 }
1447 gdb_printf (file, "\n");
1448 }
1449 }
1450 else
1451 {
1452 size_t i;
1453 gdb_printf (file, " ");
1454 for (i = 0; valid_options->name[i] != NULL; i++)
1455 {
1456 gdb_printf (file, "%s", valid_options->name[i]);
1457 if (valid_options->arg != NULL && valid_options->arg[i] != NULL)
1458 gdb_printf (file, "%s", valid_options->arg[i]->name);
1459 if (valid_options->name[i + 1] != NULL)
1460 gdb_printf (file, ", ");
1461 file->wrap_here (2);
1462 }
1463 gdb_printf (file, "\n");
1464 }
1465
1466 valid_args = valid_options_and_args->args;
1467 if (valid_args != NULL)
1468 {
1469 size_t i, j;
1470
1471 for (i = 0; valid_args[i].name != NULL; i++)
1472 {
1473 if (valid_args[i].values == NULL)
1474 continue;
1475 gdb_printf (file, _("\n\
1476 For the options above, the following values are supported for \"%s\":\n "),
1477 valid_args[i].name);
1478 for (j = 0; valid_args[i].values[j] != NULL; j++)
1479 {
1480 gdb_printf (file, " %s", valid_args[i].values[j]);
1481 file->wrap_here (3);
1482 }
1483 gdb_printf (file, "\n");
1484 }
1485 }
1486 }
1487
1488 /* A completion function for "set disassembler". */
1489
1490 static void
1491 disassembler_options_completer (struct cmd_list_element *ignore,
1492 completion_tracker &tracker,
1493 const char *text, const char *word)
1494 {
1495 struct gdbarch *gdbarch = get_current_arch ();
1496 const disasm_options_and_args_t *opts_and_args
1497 = gdbarch_valid_disassembler_options (gdbarch);
1498
1499 if (opts_and_args != NULL)
1500 {
1501 const disasm_options_t *opts = &opts_and_args->options;
1502
1503 /* Only attempt to complete on the last option text. */
1504 const char *separator = strrchr (text, ',');
1505 if (separator != NULL)
1506 text = separator + 1;
1507 text = skip_spaces (text);
1508 complete_on_enum (tracker, opts->name, text, word);
1509 }
1510 }
1511
1512
1513 /* Initialization code. */
1514
1515 void _initialize_disasm ();
1516 void
1517 _initialize_disasm ()
1518 {
1519 /* Add the command that controls the disassembler options. */
1520 set_show_commands set_show_disas_opts
1521 = add_setshow_string_noescape_cmd ("disassembler-options", no_class,
1522 &prospective_options, _("\
1523 Set the disassembler options.\n\
1524 Usage: set disassembler-options OPTION [,OPTION]...\n\n\
1525 See: 'show disassembler-options' for valid option values."), _("\
1526 Show the disassembler options."), NULL,
1527 set_disassembler_options_sfunc,
1528 show_disassembler_options_sfunc,
1529 &setlist, &showlist);
1530 set_cmd_completer (set_show_disas_opts.set, disassembler_options_completer);
1531
1532
1533 /* All the 'maint set|show libopcodes-styling' sub-commands. */
1534 static struct cmd_list_element *maint_set_libopcodes_styling_cmdlist;
1535 static struct cmd_list_element *maint_show_libopcodes_styling_cmdlist;
1536
1537 /* Adds 'maint set|show libopcodes-styling'. */
1538 add_setshow_prefix_cmd ("libopcodes-styling", class_maintenance,
1539 _("Set libopcodes-styling specific variables."),
1540 _("Show libopcodes-styling specific variables."),
1541 &maint_set_libopcodes_styling_cmdlist,
1542 &maint_show_libopcodes_styling_cmdlist,
1543 &maintenance_set_cmdlist,
1544 &maintenance_show_cmdlist);
1545
1546 /* Adds 'maint set|show gnu-source-highlight enabled'. */
1547 add_setshow_boolean_cmd ("enabled", class_maintenance,
1548 &use_libopcodes_styling_option, _("\
1549 Set whether the libopcodes styling support should be used."), _("\
1550 Show whether the libopcodes styling support should be used."),_("\
1551 When enabled, GDB will try to make use of the builtin libopcodes styling\n\
1552 support, to style the disassembler output. Not every architecture has\n\
1553 styling support within libopcodes, so enabling this is not a guarantee\n\
1554 that libopcodes styling will be available.\n\
1555 \n\
1556 When this option is disabled, GDB will make use of the Python Pygments\n\
1557 package (if available) to style the disassembler output.\n\
1558 \n\
1559 All disassembler styling can be disabled with:\n\
1560 \n\
1561 set style disassembler enabled off"),
1562 set_use_libopcodes_styling,
1563 show_use_libopcodes_styling,
1564 &maint_set_libopcodes_styling_cmdlist,
1565 &maint_show_libopcodes_styling_cmdlist);
1566 }