gdb/
[deliverable/binutils-gdb.git] / gdb / disasm.c
index 1c050c93324fb17ced4f148aa516bcf0ae41ea79..e3d3349200e64455ceed3aeac44b696ce201f417 100644 (file)
@@ -1,13 +1,12 @@
 /* Disassemble support for GDB.
 
 /* Disassemble support for GDB.
 
-   Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2007
-   Free Software Foundation, Inc.
+   Copyright (C) 2000-2005, 2007-2012 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
 
    This file is part of GDB.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 2 of the License, or
+   the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
    (at your option) any later version.
 
    This program is distributed in the hope that it will be useful,
@@ -16,9 +15,7 @@
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street, Fifth Floor,
-   Boston, MA 02110-1301, USA.  */
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
 #include "defs.h"
 #include "target.h"
 
 #include "defs.h"
 #include "target.h"
@@ -31,7 +28,7 @@
 
 /* Disassemble functions.
    FIXME: We should get rid of all the duplicate code in gdb that does
 
 /* Disassemble functions.
    FIXME: We should get rid of all the duplicate code in gdb that does
-   the same thing: disassemble_command() and the gdbtk variation. */
+   the same thing: disassemble_command() and the gdbtk variation.  */
 
 /* This Structure is used to store line number information.
    We need a different sort of line table from the normal one cuz we can't
 
 /* This Structure is used to store line number information.
    We need a different sort of line table from the normal one cuz we can't
@@ -65,7 +62,9 @@ dis_asm_memory_error (int status, bfd_vma memaddr,
 static void
 dis_asm_print_address (bfd_vma addr, struct disassemble_info *info)
 {
 static void
 dis_asm_print_address (bfd_vma addr, struct disassemble_info *info)
 {
-  print_address (addr, info->stream);
+  struct gdbarch *gdbarch = info->application_data;
+
+  print_address (gdbarch, addr, info->stream);
 }
 
 static int
 }
 
 static int
@@ -77,18 +76,28 @@ compare_lines (const void *mle1p, const void *mle2p)
   mle1 = (struct dis_line_entry *) mle1p;
   mle2 = (struct dis_line_entry *) mle2p;
 
   mle1 = (struct dis_line_entry *) mle1p;
   mle2 = (struct dis_line_entry *) mle2p;
 
-  val = mle1->line - mle2->line;
-
-  if (val != 0)
-    return val;
-
-  return mle1->start_pc - mle2->start_pc;
+  /* End of sequence markers have a line number of 0 but don't want to
+     be sorted to the head of the list, instead sort by PC.  */
+  if (mle1->line == 0 || mle2->line == 0)
+    {
+      val = mle1->start_pc - mle2->start_pc;
+      if (val == 0)
+        val = mle1->line - mle2->line;
+    }
+  else
+    {
+      val = mle1->line - mle2->line;
+      if (val == 0)
+        val = mle1->start_pc - mle2->start_pc;
+    }
+  return val;
 }
 
 static int
 }
 
 static int
-dump_insns (struct ui_out *uiout, struct disassemble_info * di,
+dump_insns (struct gdbarch *gdbarch, struct ui_out *uiout,
+           struct disassemble_info * di,
            CORE_ADDR low, CORE_ADDR high,
            CORE_ADDR low, CORE_ADDR high,
-           int how_many, struct ui_stream *stb)
+           int how_many, int flags, struct ui_file *stb)
 {
   int num_displayed = 0;
   CORE_ADDR pc;
 {
   int num_displayed = 0;
   CORE_ADDR pc;
@@ -113,15 +122,17 @@ dump_insns (struct ui_out *uiout, struct disassemble_info * di,
            num_displayed++;
        }
       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
            num_displayed++;
        }
       ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
-      ui_out_field_core_addr (uiout, "address", pc);
+      ui_out_text (uiout, pc_prefix (pc));
+      ui_out_field_core_addr (uiout, "address", gdbarch, pc);
 
 
-      if (!build_address_symbolic (pc, 0, &name, &offset, &filename,
+      if (!build_address_symbolic (gdbarch, pc, 0, &name, &offset, &filename,
                                   &line, &unmapped))
        {
          /* We don't care now about line, filename and
                                   &line, &unmapped))
        {
          /* We don't care now about line, filename and
-            unmapped. But we might in the future. */
+            unmapped. But we might in the future.  */
          ui_out_text (uiout, " <");
          ui_out_text (uiout, " <");
-         ui_out_field_string (uiout, "func-name", name);
+         if ((flags & DISASSEMBLY_OMIT_FNAME) == 0)
+           ui_out_field_string (uiout, "func-name", name);
          ui_out_text (uiout, "+");
          ui_out_field_int (uiout, "offset", offset);
          ui_out_text (uiout, ">:\t");
          ui_out_text (uiout, "+");
          ui_out_field_int (uiout, "offset", offset);
          ui_out_text (uiout, ">:\t");
@@ -134,10 +145,39 @@ dump_insns (struct ui_out *uiout, struct disassemble_info * di,
       if (name != NULL)
        xfree (name);
 
       if (name != NULL)
        xfree (name);
 
-      ui_file_rewind (stb->stream);
-      pc += TARGET_PRINT_INSN (pc, di);
+      ui_file_rewind (stb);
+      if (flags & DISASSEMBLY_RAW_INSN)
+        {
+          CORE_ADDR old_pc = pc;
+          bfd_byte data;
+          int status;
+          const char *spacer = "";
+
+          /* Build the opcodes using a temporary stream so we can
+             write them out in a single go for the MI.  */
+          struct ui_file *opcode_stream = mem_fileopen ();
+          struct cleanup *cleanups =
+            make_cleanup_ui_file_delete (opcode_stream);
+
+          pc += gdbarch_print_insn (gdbarch, pc, di);
+          for (;old_pc < pc; old_pc++)
+            {
+              status = (*di->read_memory_func) (old_pc, &data, 1, di);
+              if (status != 0)
+                (*di->memory_error_func) (status, old_pc, di);
+              fprintf_filtered (opcode_stream, "%s%02x",
+                                spacer, (unsigned) data);
+              spacer = " ";
+            }
+          ui_out_field_stream (uiout, "opcodes", opcode_stream);
+          ui_out_text (uiout, "\t");
+
+          do_cleanups (cleanups);
+        }
+      else
+        pc += gdbarch_print_insn (gdbarch, pc, di);
       ui_out_field_stream (uiout, "inst", stb);
       ui_out_field_stream (uiout, "inst", stb);
-      ui_file_rewind (stb->stream);
+      ui_file_rewind (stb);
       do_cleanups (ui_out_chain);
       ui_out_text (uiout, "\n");
     }
       do_cleanups (ui_out_chain);
       ui_out_text (uiout, "\n");
     }
@@ -148,13 +188,14 @@ dump_insns (struct ui_out *uiout, struct disassemble_info * di,
    function to the user.  This means that things are presented
    in source order, with (possibly) out of order assembly
    immediately following.  */
    function to the user.  This means that things are presented
    in source order, with (possibly) out of order assembly
    immediately following.  */
+
 static void
 static void
-do_mixed_source_and_assembly (struct ui_out *uiout,
+do_mixed_source_and_assembly (struct gdbarch *gdbarch, struct ui_out *uiout,
                              struct disassemble_info *di, int nlines,
                              struct linetable_entry *le,
                              CORE_ADDR low, CORE_ADDR high,
                              struct symtab *symtab,
                              struct disassemble_info *di, int nlines,
                              struct linetable_entry *le,
                              CORE_ADDR low, CORE_ADDR high,
                              struct symtab *symtab,
-                             int how_many, struct ui_stream *stb)
+                             int how_many, int flags, struct ui_file *stb)
 {
   int newlines = 0;
   struct dis_line_entry *mle;
 {
   int newlines = 0;
   struct dis_line_entry *mle;
@@ -162,7 +203,6 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
   int i;
   int out_of_order = 0;
   int next_line = 0;
   int i;
   int out_of_order = 0;
   int next_line = 0;
-  CORE_ADDR pc;
   int num_displayed = 0;
   struct cleanup *ui_out_chain;
   struct cleanup *ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
   int num_displayed = 0;
   struct cleanup *ui_out_chain;
   struct cleanup *ui_out_tuple_chain = make_cleanup (null_cleanup, 0);
@@ -184,7 +224,7 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
   for (; i < nlines - 1 && le[i].pc < high; i++)
     {
       if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
   for (; i < nlines - 1 && le[i].pc < high; i++)
     {
       if (le[i].line == le[i + 1].line && le[i].pc == le[i + 1].pc)
-       continue;               /* Ignore duplicates */
+       continue;               /* Ignore duplicates */
 
       /* Skip any end-of-function markers.  */
       if (le[i].line == 0)
 
       /* Skip any end-of-function markers.  */
       if (le[i].line == 0)
@@ -211,7 +251,7 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
     }
 
   /* Now, sort mle by line #s (and, then by addresses within
     }
 
   /* Now, sort mle by line #s (and, then by addresses within
-     lines). */
+     lines).  */
 
   if (out_of_order)
     qsort (mle, newlines, sizeof (struct dis_line_entry), compare_lines);
 
   if (out_of_order)
     qsort (mle, newlines, sizeof (struct dis_line_entry), compare_lines);
@@ -229,7 +269,7 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
        {
          if (next_line != 0)
            {
        {
          if (next_line != 0)
            {
-             /* Just one line to print. */
+             /* Just one line to print.  */
              if (next_line == mle[i].line)
                {
                  ui_out_tuple_chain
              if (next_line == mle[i].line)
                {
                  ui_out_tuple_chain
@@ -239,7 +279,7 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
                }
              else
                {
                }
              else
                {
-                 /* Several source lines w/o asm instructions associated. */
+                 /* Several source lines w/o asm instructions associated.  */
                  for (; next_line < mle[i].line; next_line++)
                    {
                      struct cleanup *ui_out_list_chain_line;
                  for (; next_line < mle[i].line; next_line++)
                    {
                      struct cleanup *ui_out_list_chain_line;
@@ -257,7 +297,7 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
                      do_cleanups (ui_out_tuple_chain_line);
                    }
                  /* Print the last line and leave list open for
                      do_cleanups (ui_out_tuple_chain_line);
                    }
                  /* Print the last line and leave list open for
-                    asm instructions to be added. */
+                    asm instructions to be added.  */
                  ui_out_tuple_chain
                    = make_cleanup_ui_out_tuple_begin_end (uiout,
                                                           "src_and_asm_line");
                  ui_out_tuple_chain
                    = make_cleanup_ui_out_tuple_begin_end (uiout,
                                                           "src_and_asm_line");
@@ -267,7 +307,8 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
          else
            {
              ui_out_tuple_chain
          else
            {
              ui_out_tuple_chain
-               = make_cleanup_ui_out_tuple_begin_end (uiout, "src_and_asm_line");
+               = make_cleanup_ui_out_tuple_begin_end (uiout,
+                                                      "src_and_asm_line");
              print_source_lines (symtab, mle[i].line, mle[i].line + 1, 0);
            }
 
              print_source_lines (symtab, mle[i].line, mle[i].line + 1, 0);
            }
 
@@ -276,8 +317,9 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
            = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
        }
 
            = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
        }
 
-      num_displayed += dump_insns (uiout, di, mle[i].start_pc, mle[i].end_pc,
-                                  how_many, stb);
+      num_displayed += dump_insns (gdbarch, uiout, di,
+                                  mle[i].start_pc, mle[i].end_pc,
+                                  how_many, flags, stb);
 
       /* When we've reached the end of the mle array, or we've seen the last
          assembly range for this source line, close out the list/tuple.  */
 
       /* When we've reached the end of the mle array, or we've seen the last
          assembly range for this source line, close out the list/tuple.  */
@@ -297,16 +339,18 @@ do_mixed_source_and_assembly (struct ui_out *uiout,
 
 
 static void
 
 
 static void
-do_assembly_only (struct ui_out *uiout, struct disassemble_info * di,
+do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
+                 struct disassemble_info * di,
                  CORE_ADDR low, CORE_ADDR high,
                  CORE_ADDR low, CORE_ADDR high,
-                 int how_many, struct ui_stream *stb)
+                 int how_many, int flags, struct ui_file *stb)
 {
   int num_displayed = 0;
   struct cleanup *ui_out_chain;
 
   ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
 
 {
   int num_displayed = 0;
   struct cleanup *ui_out_chain;
 
   ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
 
-  num_displayed = dump_insns (uiout, di, low, high, how_many, stb);
+  num_displayed = dump_insns (gdbarch, uiout, di, low, high, how_many,
+                              flags, stb);
 
   do_cleanups (ui_out_chain);
 }
 
   do_cleanups (ui_out_chain);
 }
@@ -314,10 +358,11 @@ do_assembly_only (struct ui_out *uiout, struct disassemble_info * di,
 /* Initialize the disassemble info struct ready for the specified
    stream.  */
 
 /* Initialize the disassemble info struct ready for the specified
    stream.  */
 
-static int ATTR_FORMAT (printf, 2, 3)
+static int ATTRIBUTE_PRINTF (2, 3)
 fprintf_disasm (void *stream, const char *format, ...)
 {
   va_list args;
 fprintf_disasm (void *stream, const char *format, ...)
 {
   va_list args;
+
   va_start (args, format);
   vfprintf_filtered (stream, format, args);
   va_end (args);
   va_start (args, format);
   vfprintf_filtered (stream, format, args);
   va_end (args);
@@ -329,6 +374,7 @@ static struct disassemble_info
 gdb_disassemble_info (struct gdbarch *gdbarch, struct ui_file *file)
 {
   struct disassemble_info di;
 gdb_disassemble_info (struct gdbarch *gdbarch, struct ui_file *file)
 {
   struct disassemble_info di;
+
   init_disassemble_info (&di, file, fprintf_disasm);
   di.flavour = bfd_target_unknown_flavour;
   di.memory_error_func = dis_asm_memory_error;
   init_disassemble_info (&di, file, fprintf_disasm);
   di.flavour = bfd_target_unknown_flavour;
   di.memory_error_func = dis_asm_memory_error;
@@ -345,26 +391,26 @@ gdb_disassemble_info (struct gdbarch *gdbarch, struct ui_file *file)
   di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
   di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
   di.endian = gdbarch_byte_order (gdbarch);
   di.arch = gdbarch_bfd_arch_info (gdbarch)->arch;
   di.mach = gdbarch_bfd_arch_info (gdbarch)->mach;
   di.endian = gdbarch_byte_order (gdbarch);
+  di.endian_code = gdbarch_byte_order_for_code (gdbarch);
+  di.application_data = gdbarch;
   disassemble_init_for_target (&di);
   return di;
 }
 
 void
   disassemble_init_for_target (&di);
   return di;
 }
 
 void
-gdb_disassembly (struct ui_out *uiout,
-               char *file_string,
-               int line_num,
-               int mixed_source_and_assembly,
-               int how_many, CORE_ADDR low, CORE_ADDR high)
+gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
+                char *file_string, int flags, int how_many,
+                CORE_ADDR low, CORE_ADDR high)
 {
 {
-  struct ui_stream *stb = ui_out_stream_new (uiout);
-  struct cleanup *cleanups = make_cleanup_ui_out_stream_delete (stb);
-  struct disassemble_info di = gdb_disassemble_info (current_gdbarch, stb->stream);
-  /* To collect the instruction outputted from opcodes. */
+  struct ui_file *stb = mem_fileopen ();
+  struct cleanup *cleanups = make_cleanup_ui_file_delete (stb);
+  struct disassemble_info di = gdb_disassemble_info (gdbarch, stb);
+  /* To collect the instruction outputted from opcodes.  */
   struct symtab *symtab = NULL;
   struct linetable_entry *le = NULL;
   int nlines = -1;
 
   struct symtab *symtab = NULL;
   struct linetable_entry *le = NULL;
   int nlines = -1;
 
-  /* Assume symtab is valid for whole PC range */
+  /* Assume symtab is valid for whole PC range */
   symtab = find_pc_symtab (low);
 
   if (symtab != NULL && symtab->linetable != NULL)
   symtab = find_pc_symtab (low);
 
   if (symtab != NULL && symtab->linetable != NULL)
@@ -374,24 +420,110 @@ gdb_disassembly (struct ui_out *uiout,
       nlines = symtab->linetable->nitems;
     }
 
       nlines = symtab->linetable->nitems;
     }
 
-  if (!mixed_source_and_assembly || nlines <= 0
+  if (!(flags & DISASSEMBLY_SOURCE) || nlines <= 0
       || symtab == NULL || symtab->linetable == NULL)
       || symtab == NULL || symtab->linetable == NULL)
-    do_assembly_only (uiout, &di, low, high, how_many, stb);
+    do_assembly_only (gdbarch, uiout, &di, low, high, how_many, flags, stb);
 
 
-  else if (mixed_source_and_assembly)
-    do_mixed_source_and_assembly (uiout, &di, nlines, le, low,
-                                 high, symtab, how_many, stb);
+  else if (flags & DISASSEMBLY_SOURCE)
+    do_mixed_source_and_assembly (gdbarch, uiout, &di, nlines, le, low,
+                                 high, symtab, how_many, flags, stb);
 
   do_cleanups (cleanups);
   gdb_flush (gdb_stdout);
 }
 
 /* Print the instruction at address MEMADDR in debugged memory,
 
   do_cleanups (cleanups);
   gdb_flush (gdb_stdout);
 }
 
 /* Print the instruction at address MEMADDR in debugged memory,
-   on STREAM.  Returns length of the instruction, in bytes.  */
+   on STREAM.  Returns the length of the instruction, in bytes,
+   and, if requested, the number of branch delay slot instructions.  */
+
+int
+gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
+               struct ui_file *stream, int *branch_delay_insns)
+{
+  struct disassemble_info di;
+  int length;
+
+  di = gdb_disassemble_info (gdbarch, stream);
+  length = gdbarch_print_insn (gdbarch, memaddr, &di);
+  if (branch_delay_insns)
+    {
+      if (di.insn_info_valid)
+       *branch_delay_insns = di.branch_delay_insns;
+      else
+       *branch_delay_insns = 0;
+    }
+  return length;
+}
+
+static void
+do_ui_file_delete (void *arg)
+{
+  ui_file_delete (arg);
+}
+
+/* Return the length in bytes of the instruction at address MEMADDR in
+   debugged memory.  */
 
 int
 
 int
-gdb_print_insn (CORE_ADDR memaddr, struct ui_file *stream)
+gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
 {
 {
-  struct disassemble_info di = gdb_disassemble_info (current_gdbarch, stream);
-  return TARGET_PRINT_INSN (memaddr, &di);
+  static struct ui_file *null_stream = NULL;
+
+  /* Dummy file descriptor for the disassembler.  */
+  if (!null_stream)
+    {
+      null_stream = ui_file_new ();
+      make_final_cleanup (do_ui_file_delete, null_stream);
+    }
+
+  return gdb_print_insn (gdbarch, addr, null_stream, NULL);
+}
+
+/* fprintf-function for gdb_buffered_insn_length.  This function is a
+   nop, we don't want to print anything, we just want to compute the
+   length of the insn.  */
+
+static int ATTRIBUTE_PRINTF (2, 3)
+gdb_buffered_insn_length_fprintf (void *stream, const char *format, ...)
+{
+  return 0;
+}
+
+/* Initialize a struct disassemble_info for gdb_buffered_insn_length.  */
+
+static void
+gdb_buffered_insn_length_init_dis (struct gdbarch *gdbarch,
+                                  struct disassemble_info *di,
+                                  const gdb_byte *insn, int max_len,
+                                  CORE_ADDR addr)
+{
+  init_disassemble_info (di, NULL, gdb_buffered_insn_length_fprintf);
+
+  /* init_disassemble_info installs buffer_read_memory, etc.
+     so we don't need to do that here.
+     The cast is necessary until disassemble_info is const-ified.  */
+  di->buffer = (gdb_byte *) insn;
+  di->buffer_length = max_len;
+  di->buffer_vma = addr;
+
+  di->arch = gdbarch_bfd_arch_info (gdbarch)->arch;
+  di->mach = gdbarch_bfd_arch_info (gdbarch)->mach;
+  di->endian = gdbarch_byte_order (gdbarch);
+  di->endian_code = gdbarch_byte_order_for_code (gdbarch);
+
+  disassemble_init_for_target (di);
+}
+
+/* Return the length in bytes of INSN.  MAX_LEN is the size of the
+   buffer containing INSN.  */
+
+int
+gdb_buffered_insn_length (struct gdbarch *gdbarch,
+                         const gdb_byte *insn, int max_len, CORE_ADDR addr)
+{
+  struct disassemble_info di;
+
+  gdb_buffered_insn_length_init_dis (gdbarch, &di, insn, max_len, addr);
+
+  return gdbarch_print_insn (gdbarch, addr, &di);
 }
 }
This page took 0.031615 seconds and 4 git commands to generate.