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