Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* Move amd64_insn_length to common code
@ 2010-04-30 18:54 Pedro Alves
  2010-05-06  0:23 ` Pedro Alves
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2010-04-30 18:54 UTC (permalink / raw)
  To: gdb-patches, Doug Evans

I'll be posting a patch that adds code to i386-tdep.c
that will need to know the length of an instruction.  We already
have code for that in amd64-tdep.c.  I don't suppose you
see a problem with moving it to a more common/reusable place?

-- 
Pedro Alves

2010-04-30  Pedro Alves  <pedro@codesourcery.com>

	gdb/
	* amd64-tdep.c: Include disasm.h.
	(amd64_insn_length_fprintf, amd64_insn_length_init_dis)
	(amd64_insn_length): Moved to disasm.c and renamed.
	(fixup_riprel): Adjust.
	* disasm.c (do_ui_file_delete): New.
	(gdb_insn_length): New.
	(gdb_buffered_insn_length_fprintf)
	(gdb_buffered_insn_length_init_dis)
	(gdb_buffered_insn_length): New, moved from amd64-tdep.c, and
	renamed.
	* disasm.h (gdb_insn_length): Declare.
	(gdb_buffered_insn_length): Declare.
	
---
 gdb/amd64-tdep.c |   56 ++----------------------------------------
 gdb/disasm.c     |   73 +++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdb/disasm.h     |   12 +++++++++
 3 files changed, 88 insertions(+), 53 deletions(-)

Index: src/gdb/amd64-tdep.c
===================================================================
--- src.orig/gdb/amd64-tdep.c	2010-04-30 17:58:22.000000000 +0100
+++ src/gdb/amd64-tdep.c	2010-04-30 18:11:34.000000000 +0100
@@ -36,7 +36,7 @@
 #include "regcache.h"
 #include "regset.h"
 #include "symfile.h"
-
+#include "disasm.h"
 #include "gdb_assert.h"
 
 #include "amd64-tdep.h"
@@ -1022,57 +1022,6 @@ amd64_skip_prefixes (gdb_byte *insn)
   return insn;
 }
 
-/* fprintf-function for amd64_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 ATTR_FORMAT (printf, 2, 3)
-amd64_insn_length_fprintf (void *stream, const char *format, ...)
-{
-  return 0;
-}
-
-/* Initialize a struct disassemble_info for amd64_insn_length.	*/
-
-static void
-amd64_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, amd64_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.
-   libopcodes currently doesn't export a utility to compute the
-   instruction length, so use the disassembler until then.  */
-
-static int
-amd64_insn_length (struct gdbarch *gdbarch,
-		   const gdb_byte *insn, int max_len, CORE_ADDR addr)
-{
-  struct disassemble_info di;
-
-  amd64_insn_length_init_dis (gdbarch, &di, insn, max_len, addr);
-
-  return gdbarch_print_insn (gdbarch, addr, &di);
-}
-
 /* Return an integer register (other than RSP) that is unused as an input
    operand in INSN.
    In order to not require adding a rex prefix if the insn doesn't already
@@ -1238,7 +1187,8 @@ fixup_riprel (struct gdbarch *gdbarch, s
 
   /* Compute the rip-relative address.	*/
   disp = extract_signed_integer (insn, sizeof (int32_t), byte_order);
-  insn_length = amd64_insn_length (gdbarch, dsc->insn_buf, dsc->max_len, from);
+  insn_length = gdb_buffered_insn_length (gdbarch, dsc->insn_buf,
+					  dsc->max_len, from);
   rip_base = from + insn_length;
 
   /* We need a register to hold the address.
Index: src/gdb/disasm.c
===================================================================
--- src.orig/gdb/disasm.c	2010-04-30 17:58:22.000000000 +0100
+++ src/gdb/disasm.c	2010-04-30 19:52:45.000000000 +0100
@@ -429,3 +429,76 @@ gdb_print_insn (struct gdbarch *gdbarch,
     }
   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
+gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR addr)
+{
+  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 ATTR_FORMAT (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);
+}
Index: src/gdb/disasm.h
===================================================================
--- src.orig/gdb/disasm.h	2010-04-30 17:58:22.000000000 +0100
+++ src/gdb/disasm.h	2010-04-30 18:15:20.000000000 +0100
@@ -37,4 +37,16 @@ extern void gdb_disassembly (struct gdba
 extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
 			   struct ui_file *stream, int *branch_delay_insns);
 
+/* Return the length in bytes of the instruction at address MEMADDR in
+   debugged memory.  */
+
+extern int gdb_insn_length (struct gdbarch *gdbarch, CORE_ADDR memaddr);
+
+/* Return the length in bytes of INSN, originally at MEMADDR.  MAX_LEN
+   is the size of the buffer containing INSN.  */
+
+extern int gdb_buffered_insn_length (struct gdbarch *gdbarch,
+				     const gdb_byte *insn, int max_len,
+				     CORE_ADDR memaddr);
+
 #endif


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Move amd64_insn_length to common code
  2010-04-30 18:54 Move amd64_insn_length to common code Pedro Alves
@ 2010-05-06  0:23 ` Pedro Alves
  2010-05-06  6:29   ` Mark Kettenis
  0 siblings, 1 reply; 3+ messages in thread
From: Pedro Alves @ 2010-05-06  0:23 UTC (permalink / raw)
  To: gdb-patches; +Cc: Doug Evans

On Friday 30 April 2010 19:54:22, Pedro Alves wrote:
> I'll be posting a patch that adds code to i386-tdep.c
> that will need to know the length of an instruction.  We already
> have code for that in amd64-tdep.c.  I don't suppose you
> see a problem with moving it to a more common/reusable place?

FYI, I went ahead and applied this.  Easy to revert and address
some other way if need be.

> 2010-04-30  Pedro Alves  <pedro@codesourcery.com>
> 
>         gdb/
>         * amd64-tdep.c: Include disasm.h.
>         (amd64_insn_length_fprintf, amd64_insn_length_init_dis)
>         (amd64_insn_length): Moved to disasm.c and renamed.
>         (fixup_riprel): Adjust.
>         * disasm.c (do_ui_file_delete): New.
>         (gdb_insn_length): New.
>         (gdb_buffered_insn_length_fprintf)
>         (gdb_buffered_insn_length_init_dis)
>         (gdb_buffered_insn_length): New, moved from amd64-tdep.c, and
>         renamed.
>         * disasm.h (gdb_insn_length): Declare.
>         (gdb_buffered_insn_length): Declare.

-- 
Pedro Alves


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Move amd64_insn_length to common code
  2010-05-06  0:23 ` Pedro Alves
@ 2010-05-06  6:29   ` Mark Kettenis
  0 siblings, 0 replies; 3+ messages in thread
From: Mark Kettenis @ 2010-05-06  6:29 UTC (permalink / raw)
  To: pedro; +Cc: gdb-patches, dje

> From: Pedro Alves <pedro@codesourcery.com>
> Date: Thu, 6 May 2010 01:23:29 +0100
> 
> On Friday 30 April 2010 19:54:22, Pedro Alves wrote:
> > I'll be posting a patch that adds code to i386-tdep.c
> > that will need to know the length of an instruction.  We already
> > have code for that in amd64-tdep.c.  I don't suppose you
> > see a problem with moving it to a more common/reusable place?
> 
> FYI, I went ahead and applied this.  Easy to revert and address
> some other way if need be.

Looked very reasonable to me anyway.

> > 2010-04-30  Pedro Alves  <pedro@codesourcery.com>
> > 
> >         gdb/
> >         * amd64-tdep.c: Include disasm.h.
> >         (amd64_insn_length_fprintf, amd64_insn_length_init_dis)
> >         (amd64_insn_length): Moved to disasm.c and renamed.
> >         (fixup_riprel): Adjust.
> >         * disasm.c (do_ui_file_delete): New.
> >         (gdb_insn_length): New.
> >         (gdb_buffered_insn_length_fprintf)
> >         (gdb_buffered_insn_length_init_dis)
> >         (gdb_buffered_insn_length): New, moved from amd64-tdep.c, and
> >         renamed.
> >         * disasm.h (gdb_insn_length): Declare.
> >         (gdb_buffered_insn_length): Declare.


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2010-05-06  6:29 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-04-30 18:54 Move amd64_insn_length to common code Pedro Alves
2010-05-06  0:23 ` Pedro Alves
2010-05-06  6:29   ` Mark Kettenis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox