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