gdb/disasm.c - gdb

Data types defined

Functions defined

Source code

  1. /* Disassemble support for GDB.

  2.    Copyright (C) 2000-2015 Free Software Foundation, Inc.

  3.    This file is part of GDB.

  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 3 of the License, or
  7.    (at your option) any later version.

  8.    This program is distributed in the hope that it will be useful,
  9.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.    GNU General Public License for more details.

  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  14. #include "defs.h"
  15. #include "target.h"
  16. #include "value.h"
  17. #include "ui-out.h"
  18. #include "disasm.h"
  19. #include "gdbcore.h"
  20. #include "dis-asm.h"

  21. /* Disassemble functions.
  22.    FIXME: We should get rid of all the duplicate code in gdb that does
  23.    the same thing: disassemble_command() and the gdbtk variation.  */

  24. /* This Structure is used to store line number information.
  25.    We need a different sort of line table from the normal one cuz we can't
  26.    depend upon implicit line-end pc's for lines to do the
  27.    reordering in this function.  */

  28. struct dis_line_entry
  29. {
  30.   int line;
  31.   CORE_ADDR start_pc;
  32.   CORE_ADDR end_pc;
  33. };

  34. /* Like target_read_memory, but slightly different parameters.  */
  35. static int
  36. dis_asm_read_memory (bfd_vma memaddr, gdb_byte *myaddr, unsigned int len,
  37.                      struct disassemble_info *info)
  38. {
  39.   return target_read_code (memaddr, myaddr, len);
  40. }

  41. /* Like memory_error with slightly different parameters.  */
  42. static void
  43. dis_asm_memory_error (int status, bfd_vma memaddr,
  44.                       struct disassemble_info *info)
  45. {
  46.   memory_error (status, memaddr);
  47. }

  48. /* Like print_address with slightly different parameters.  */
  49. static void
  50. dis_asm_print_address (bfd_vma addr, struct disassemble_info *info)
  51. {
  52.   struct gdbarch *gdbarch = info->application_data;

  53.   print_address (gdbarch, addr, info->stream);
  54. }

  55. static int
  56. compare_lines (const void *mle1p, const void *mle2p)
  57. {
  58.   struct dis_line_entry *mle1, *mle2;
  59.   int val;

  60.   mle1 = (struct dis_line_entry *) mle1p;
  61.   mle2 = (struct dis_line_entry *) mle2p;

  62.   /* End of sequence markers have a line number of 0 but don't want to
  63.      be sorted to the head of the list, instead sort by PC.  */
  64.   if (mle1->line == 0 || mle2->line == 0)
  65.     {
  66.       val = mle1->start_pc - mle2->start_pc;
  67.       if (val == 0)
  68.         val = mle1->line - mle2->line;
  69.     }
  70.   else
  71.     {
  72.       val = mle1->line - mle2->line;
  73.       if (val == 0)
  74.         val = mle1->start_pc - mle2->start_pc;
  75.     }
  76.   return val;
  77. }

  78. static int
  79. dump_insns (struct gdbarch *gdbarch, struct ui_out *uiout,
  80.             struct disassemble_info * di,
  81.             CORE_ADDR low, CORE_ADDR high,
  82.             int how_many, int flags, struct ui_file *stb)
  83. {
  84.   int num_displayed = 0;
  85.   CORE_ADDR pc;

  86.   /* parts of the symbolic representation of the address */
  87.   int unmapped;
  88.   int offset;
  89.   int line;
  90.   struct cleanup *ui_out_chain;

  91.   for (pc = low; pc < high;)
  92.     {
  93.       char *filename = NULL;
  94.       char *name = NULL;

  95.       QUIT;
  96.       if (how_many >= 0)
  97.         {
  98.           if (num_displayed >= how_many)
  99.             break;
  100.           else
  101.             num_displayed++;
  102.         }
  103.       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);

  104.       if ((flags & DISASSEMBLY_OMIT_PC) == 0)
  105.         ui_out_text (uiout, pc_prefix (pc));
  106.       ui_out_field_core_addr (uiout, "address", gdbarch, pc);

  107.       if (!build_address_symbolic (gdbarch, pc, 0, &name, &offset, &filename,
  108.                                    &line, &unmapped))
  109.         {
  110.           /* We don't care now about line, filename and
  111.              unmapped. But we might in the future.  */
  112.           ui_out_text (uiout, " <");
  113.           if ((flags & DISASSEMBLY_OMIT_FNAME) == 0)
  114.             ui_out_field_string (uiout, "func-name", name);
  115.           ui_out_text (uiout, "+");
  116.           ui_out_field_int (uiout, "offset", offset);
  117.           ui_out_text (uiout, ">:\t");
  118.         }
  119.       else
  120.         ui_out_text (uiout, ":\t");

  121.       if (filename != NULL)
  122.         xfree (filename);
  123.       if (name != NULL)
  124.         xfree (name);

  125.       ui_file_rewind (stb);
  126.       if (flags & DISASSEMBLY_RAW_INSN)
  127.         {
  128.           CORE_ADDR old_pc = pc;
  129.           bfd_byte data;
  130.           int status;
  131.           const char *spacer = "";

  132.           /* Build the opcodes using a temporary stream so we can
  133.              write them out in a single go for the MI.  */
  134.           struct ui_file *opcode_stream = mem_fileopen ();
  135.           struct cleanup *cleanups =
  136.             make_cleanup_ui_file_delete (opcode_stream);

  137.           pc += gdbarch_print_insn (gdbarch, pc, di);
  138.           for (;old_pc < pc; old_pc++)
  139.             {
  140.               status = (*di->read_memory_func) (old_pc, &data, 1, di);
  141.               if (status != 0)
  142.                 (*di->memory_error_func) (status, old_pc, di);
  143.               fprintf_filtered (opcode_stream, "%s%02x",
  144.                                 spacer, (unsigned) data);
  145.               spacer = " ";
  146.             }
  147.           ui_out_field_stream (uiout, "opcodes", opcode_stream);
  148.           ui_out_text (uiout, "\t");

  149.           do_cleanups (cleanups);
  150.         }
  151.       else
  152.         pc += gdbarch_print_insn (gdbarch, pc, di);
  153.       ui_out_field_stream (uiout, "inst", stb);
  154.       ui_file_rewind (stb);
  155.       do_cleanups (ui_out_chain);
  156.       ui_out_text (uiout, "\n");
  157.     }
  158.   return num_displayed;
  159. }

  160. /* The idea here is to present a source-O-centric view of a
  161.    function to the user.  This means that things are presented
  162.    in source order, with (possibly) out of order assembly
  163.    immediately following.  */

  164. static void
  165. do_mixed_source_and_assembly (struct gdbarch *gdbarch, struct ui_out *uiout,
  166.                               struct disassemble_info *di, int nlines,
  167.                               struct linetable_entry *le,
  168.                               CORE_ADDR low, CORE_ADDR high,
  169.                               struct symtab *symtab,
  170.                               int how_many, int flags, struct ui_file *stb)
  171. {
  172.   int newlines = 0;
  173.   struct dis_line_entry *mle;
  174.   struct symtab_and_line sal;
  175.   int i;
  176.   int out_of_order = 0;
  177.   int next_line = 0;
  178.   int num_displayed = 0;
  179.   enum print_source_lines_flags psl_flags = 0;
  180.   struct cleanup *ui_out_chain;
  181.   struct cleanup *ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
  182.   struct cleanup *ui_out_list_chain = make_cleanup (null_cleanup, 0);

  183.   if (flags & DISASSEMBLY_FILENAME)
  184.     psl_flags |= PRINT_SOURCE_LINES_FILENAME;

  185.   mle = (struct dis_line_entry *) alloca (nlines
  186.                                           * sizeof (struct dis_line_entry));

  187.   /* Copy linetable entries for this function into our data
  188.      structure, creating end_pc's and setting out_of_order as
  189.      appropriate.  */

  190.   /* First, skip all the preceding functions.  */

  191.   for (i = 0; i < nlines - 1 && le[i].pc < low; i++);

  192.   /* Now, copy all entries before the end of this function.  */

  193.   for (; i < nlines - 1 && le[i].pc < high; i++)
  194.     {
  195.       if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
  196.         continue;                /* Ignore duplicates.  */

  197.       /* Skip any end-of-function markers.  */
  198.       if (le[i].line == 0)
  199.         continue;

  200.       mle[newlines].line = le[i].line;
  201.       if (le[i].line > le[i + 1].line)
  202.         out_of_order = 1;
  203.       mle[newlines].start_pc = le[i].pc;
  204.       mle[newlines].end_pc = le[i + 1].pc;
  205.       newlines++;
  206.     }

  207.   /* If we're on the last line, and it's part of the function,
  208.      then we need to get the end pc in a special way.  */

  209.   if (i == nlines - 1 && le[i].pc < high)
  210.     {
  211.       mle[newlines].line = le[i].line;
  212.       mle[newlines].start_pc = le[i].pc;
  213.       sal = find_pc_line (le[i].pc, 0);
  214.       mle[newlines].end_pc = sal.end;
  215.       newlines++;
  216.     }

  217.   /* Now, sort mle by line #s (and, then by addresses within
  218.      lines).  */

  219.   if (out_of_order)
  220.     qsort (mle, newlines, sizeof (struct dis_line_entry), compare_lines);

  221.   /* Now, for each line entry, emit the specified lines (unless
  222.      they have been emitted before), followed by the assembly code
  223.      for that line.  */

  224.   ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");

  225.   for (i = 0; i < newlines; i++)
  226.     {
  227.       /* Print out everything from next_line to the current line.  */
  228.       if (mle[i].line >= next_line)
  229.         {
  230.           if (next_line != 0)
  231.             {
  232.               /* Just one line to print.  */
  233.               if (next_line == mle[i].line)
  234.                 {
  235.                   ui_out_tuple_chain
  236.                     = make_cleanup_ui_out_tuple_begin_end (uiout,
  237.                                                            "src_and_asm_line");
  238.                   print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
  239.                 }
  240.               else
  241.                 {
  242.                   /* Several source lines w/o asm instructions associated.  */
  243.                   for (; next_line < mle[i].line; next_line++)
  244.                     {
  245.                       struct cleanup *ui_out_list_chain_line;
  246.                       struct cleanup *ui_out_tuple_chain_line;

  247.                       ui_out_tuple_chain_line
  248.                         = make_cleanup_ui_out_tuple_begin_end (uiout,
  249.                                                                "src_and_asm_line");
  250.                       print_source_lines (symtab, next_line, next_line + 1,
  251.                                           psl_flags);
  252.                       ui_out_list_chain_line
  253.                         = make_cleanup_ui_out_list_begin_end (uiout,
  254.                                                               "line_asm_insn");
  255.                       do_cleanups (ui_out_list_chain_line);
  256.                       do_cleanups (ui_out_tuple_chain_line);
  257.                     }
  258.                   /* Print the last line and leave list open for
  259.                      asm instructions to be added.  */
  260.                   ui_out_tuple_chain
  261.                     = make_cleanup_ui_out_tuple_begin_end (uiout,
  262.                                                            "src_and_asm_line");
  263.                   print_source_lines (symtab, next_line, mle[i].line + 1, psl_flags);
  264.                 }
  265.             }
  266.           else
  267.             {
  268.               ui_out_tuple_chain
  269.                 = make_cleanup_ui_out_tuple_begin_end (uiout,
  270.                                                        "src_and_asm_line");
  271.               print_source_lines (symtab, mle[i].line, mle[i].line + 1, psl_flags);
  272.             }

  273.           next_line = mle[i].line + 1;
  274.           ui_out_list_chain
  275.             = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
  276.         }

  277.       num_displayed += dump_insns (gdbarch, uiout, di,
  278.                                    mle[i].start_pc, mle[i].end_pc,
  279.                                    how_many, flags, stb);

  280.       /* When we've reached the end of the mle array, or we've seen the last
  281.          assembly range for this source line, close out the list/tuple.  */
  282.       if (i == (newlines - 1) || mle[i + 1].line > mle[i].line)
  283.         {
  284.           do_cleanups (ui_out_list_chain);
  285.           do_cleanups (ui_out_tuple_chain);
  286.           ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
  287.           ui_out_list_chain = make_cleanup (null_cleanup, 0);
  288.           ui_out_text (uiout, "\n");
  289.         }
  290.       if (how_many >= 0 && num_displayed >= how_many)
  291.         break;
  292.     }
  293.   do_cleanups (ui_out_chain);
  294. }


  295. static void
  296. do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
  297.                   struct disassemble_info * di,
  298.                   CORE_ADDR low, CORE_ADDR high,
  299.                   int how_many, int flags, struct ui_file *stb)
  300. {
  301.   int num_displayed = 0;
  302.   struct cleanup *ui_out_chain;

  303.   ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");

  304.   num_displayed = dump_insns (gdbarch, uiout, di, low, high, how_many,
  305.                               flags, stb);

  306.   do_cleanups (ui_out_chain);
  307. }

  308. /* Initialize the disassemble info struct ready for the specified
  309.    stream.  */

  310. static int ATTRIBUTE_PRINTF (2, 3)
  311. fprintf_disasm (void *stream, const char *format, ...)
  312. {
  313.   va_list args;

  314.   va_start (args, format);
  315.   vfprintf_filtered (stream, format, args);
  316.   va_end (args);
  317.   /* Something non -ve.  */
  318.   return 0;
  319. }

  320. struct disassemble_info
  321. gdb_disassemble_info (struct gdbarch *gdbarch, struct ui_file *file)
  322. {
  323.   struct disassemble_info di;

  324.   init_disassemble_info (&di, file, fprintf_disasm);
  325.   di.flavour = bfd_target_unknown_flavour;
  326.   di.memory_error_func = dis_asm_memory_error;
  327.   di.print_address_func = dis_asm_print_address;
  328.   /* NOTE: cagney/2003-04-28: The original code, from the old Insight
  329.      disassembler had a local optomization here.  By default it would
  330.      access the executable file, instead of the target memory (there
  331.      was a growing list of exceptions though).  Unfortunately, the
  332.      heuristic was flawed.  Commands like "disassemble &variable"
  333.      didn't work as they relied on the access going to the target.
  334.      Further, it has been supperseeded by trust-read-only-sections
  335.      (although that should be superseeded by target_trust..._p()).  */
  336.   di.read_memory_func = dis_asm_read_memory;
  337.   di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  338.   di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  339.   di.endian = gdbarch_byte_order (gdbarch);
  340.   di.endian_code = gdbarch_byte_order_for_code (gdbarch);
  341.   di.application_data = gdbarch;
  342.   disassemble_init_for_target (&di);
  343.   return di;
  344. }

  345. void
  346. gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
  347.                  char *file_string, int flags, int how_many,
  348.                  CORE_ADDR low, CORE_ADDR high)
  349. {
  350.   struct ui_file *stb = mem_fileopen ();
  351.   struct cleanup *cleanups = make_cleanup_ui_file_delete (stb);
  352.   struct disassemble_info di = gdb_disassemble_info (gdbarch, stb);
  353.   struct symtab *symtab;
  354.   struct linetable_entry *le = NULL;
  355.   int nlines = -1;

  356.   /* Assume symtab is valid for whole PC range.  */
  357.   symtab = find_pc_line_symtab (low);

  358.   if (symtab != NULL && SYMTAB_LINETABLE (symtab) != NULL)
  359.     {
  360.       /* Convert the linetable to a bunch of my_line_entry's.  */
  361.       le = SYMTAB_LINETABLE (symtab)->item;
  362.       nlines = SYMTAB_LINETABLE (symtab)->nitems;
  363.     }

  364.   if (!(flags & DISASSEMBLY_SOURCE) || nlines <= 0
  365.       || symtab == NULL || SYMTAB_LINETABLE (symtab) == NULL)
  366.     do_assembly_only (gdbarch, uiout, &di, low, high, how_many, flags, stb);

  367.   else if (flags & DISASSEMBLY_SOURCE)
  368.     do_mixed_source_and_assembly (gdbarch, uiout, &di, nlines, le, low,
  369.                                   high, symtab, how_many, flags, stb);

  370.   do_cleanups (cleanups);
  371.   gdb_flush (gdb_stdout);
  372. }

  373. /* Print the instruction at address MEMADDR in debugged memory,
  374.    on STREAM.  Returns the length of the instruction, in bytes,
  375.    and, if requested, the number of branch delay slot instructions.  */

  376. int
  377. gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
  378.                 struct ui_file *stream, int *branch_delay_insns)
  379. {
  380.   struct disassemble_info di;
  381.   int length;

  382.   di = gdb_disassemble_info (gdbarch, stream);
  383.   length = gdbarch_print_insn (gdbarch, memaddr, &di);
  384.   if (branch_delay_insns)
  385.     {
  386.       if (di.insn_info_valid)
  387.         *branch_delay_insns = di.branch_delay_insns;
  388.       else
  389.         *branch_delay_insns = 0;
  390.     }
  391.   return length;
  392. }

  393. static void
  394. do_ui_file_delete (void *arg)
  395. {
  396.   ui_file_delete (arg);
  397. }

  398. /* Return the length in bytes of the instruction at address MEMADDR in
  399.    debugged memory.  */

  400. int
  401. gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
  402. {
  403.   static struct ui_file *null_stream = NULL;

  404.   /* Dummy file descriptor for the disassembler.  */
  405.   if (!null_stream)
  406.     {
  407.       null_stream = ui_file_new ();
  408.       make_final_cleanup (do_ui_file_delete, null_stream);
  409.     }

  410.   return gdb_print_insn (gdbarch, addr, null_stream, NULL);
  411. }

  412. /* fprintf-function for gdb_buffered_insn_length.  This function is a
  413.    nop, we don't want to print anything, we just want to compute the
  414.    length of the insn.  */

  415. static int ATTRIBUTE_PRINTF (2, 3)
  416. gdb_buffered_insn_length_fprintf (void *stream, const char *format, ...)
  417. {
  418.   return 0;
  419. }

  420. /* Initialize a struct disassemble_info for gdb_buffered_insn_length.  */

  421. static void
  422. gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch,
  423.                                    struct disassemble_info *di,
  424.                                    const gdb_byte *insn, int max_len,
  425.                                    CORE_ADDR addr)
  426. {
  427.   init_disassemble_info (di, NULL, gdb_buffered_insn_length_fprintf);

  428.   /* init_disassemble_info installs buffer_read_memory, etc.
  429.      so we don't need to do that here.
  430.      The cast is necessary until disassemble_info is const-ified.  */
  431.   di->buffer = (gdb_byte *) insn;
  432.   di->buffer_length = max_len;
  433.   di->buffer_vma = addr;

  434.   di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
  435.   di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
  436.   di->endian = gdbarch_byte_order (gdbarch);
  437.   di->endian_code = gdbarch_byte_order_for_code (gdbarch);

  438.   disassemble_init_for_target (di);
  439. }

  440. /* Return the length in bytes of INSN.  MAX_LEN is the size of the
  441.    buffer containing INSN.  */

  442. int
  443. gdb_buffered_insn_length (struct gdbarch *gdbarch,
  444.                           const gdb_byte *insn, int max_len, CORE_ADDR addr)
  445. {
  446.   struct disassemble_info di;

  447.   gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr);

  448.   return gdbarch_print_insn (gdbarch, addr, &di);
  449. }