Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Pedro Alves <palves@redhat.com>
To: gdb-patches@sourceware.org
Subject: [PATCH v4 1/2] Add back gdb_pretty_print_insn
Date: Wed, 01 Feb 2017 00:30:00 -0000	[thread overview]
Message-ID: <1485909045-30285-2-git-send-email-palves@redhat.com> (raw)
In-Reply-To: <1485909045-30285-1-git-send-email-palves@redhat.com>

ui_file_rewind is a ui_file method that only really works with mem
buffer files, and is a nop on other ui_file types.  It'd be desirable
to eliminate it from the base ui_file interface, and move it to the
"mem_fileopen" subclass of ui_file instead.  A following patch does
just that.

Unfortunately, there are a couple references to ui_file_rewind inside
gdb_disassembler::pretty_print_insn that were made harder to eliminate
with the recent addition of the gdb_disassembler wrapper.

Before the gdb_disassembler wrapper was added, in commit
e47ad6c0bd7aa3 ("Refactor disassembly code"), gdb_pretty_print_insn
used to be passed a ui_file pointer as argument, and it was simple to
adjust that pointer be a "mem_fileopen" ui_file pointer instead, since
there's only one gdb_pretty_print_insn caller.

That commit made gdb_pretty_print_insn be a method of
gdb_disassembler, and removed the method's ui_file parameter at the
same time, replaced by referencing the gdb_disassembler's stream
instead.  The trouble is that a gdb_disassembler can be instantiated
with a pointer any kind of ui_file.  Casting the gdb_disassembler's
stream to a mem_fileopen ui_file inside
gdb_disassembler::pretty_print_insn in order to call the reset method
would be gross hack.

The fix here is to:

 - make gdb_disassembler::pretty_print_insn a be free function again
   instead of a method of gdb_disassembler.  I.e., bring back
   gdb_pretty_print_insn.

 - but, don't add back the ui_file * parameter.  We'd always be
   passing in a fresh mem_fileopen anyway, so move the mem_fileopen
   allocation inside.  That is a better interface, given that the
   ui_file is only ever used as temporary scratch buffer as an
   implementation detail of gdb_pretty_print_insn.  The function's
   real "where to send output" parameter is the ui_out pointer.

 - don't add back a disassemble_info pointer either.  That used to be
   necessary for this bit:

	  err = m_di.read_memory_func (pc, &data, 1, &m_di);
	  if (err != 0)
	    m_di.memory_error_func (err, pc, &m_di);

   ... but AFAIK, it's not really necessary.  We can replace those
   three lines with a call to read_code.  This seems to fix a
   regression even, because before commit d8b49cf0c891d0 ("Don't throw
   exception in dis_asm_memory_error"), that memory_error_func call
   would throw an error/exception, but now it only records the error
   in the gdb_disassembler's m_err_memaddr field.  (read_code throws
   on error.)

With all these, gdb_pretty_print_insn is completely layered on top of
gdb_disassembler only using the latter's public API.

gdb/ChangeLog:
yyyy-mm-dd  Pedro Alves  <palves@redhat.com>

	* disasm.c (gdb_disassembler::pretty_print_insn): Rename to
	...  (gdb_pretty_print_insn): ... this.  Now a free function.
	Add back a 'gdbarch' parameter.  Allocate a mem_fileopen
	stream here.  Adjust to call gdb_print_insn instead of
	gdb_disassembler::print_insn.
	(dump_insns, do_mixed_source_and_assembly_deprecated)
	(do_mixed_source_and_assembly, do_assembly_only): Add back a
	'gdbarch' parameter.  Remove gdb_disassembler parameter.
	(gdb_disassembly): Don't allocate a gdb_disassembler here.
	* disasm.h (gdb_disassembler::pretty_print_insn): Delete
	declaration.
	(gdb_pretty_print_insn): Re-add declaration.
	* record-btrace.c (btrace_insn_history): Don't allocate a
	gdb_disassembler here.  Adjust to call gdb_pretty_print_insn.
---
 gdb/disasm.c        | 54 ++++++++++++++++++++++-------------------------------
 gdb/disasm.h        | 11 ++++++-----
 gdb/record-btrace.c |  8 ++------
 3 files changed, 30 insertions(+), 43 deletions(-)

diff --git a/gdb/disasm.c b/gdb/disasm.c
index 897f2f1..59ebef0 100644
--- a/gdb/disasm.c
+++ b/gdb/disasm.c
@@ -182,9 +182,9 @@ compare_lines (const void *mle1p, const void *mle2p)
 /* See disasm.h.  */
 
 int
-gdb_disassembler::pretty_print_insn (struct ui_out *uiout,
-				     const struct disasm_insn *insn,
-				     int flags)
+gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout,
+		       const struct disasm_insn *insn,
+		       int flags)
 {
   /* parts of the symbolic representation of the address */
   int unmapped;
@@ -195,8 +195,6 @@ gdb_disassembler::pretty_print_insn (struct ui_out *uiout,
   char *filename = NULL;
   char *name = NULL;
   CORE_ADDR pc;
-  struct ui_file *stb = stream ();
-  struct gdbarch *gdbarch = arch ();
 
   ui_out_chain = make_cleanup_ui_out_tuple_begin_end (uiout, NULL);
   pc = insn->addr;
@@ -250,7 +248,9 @@ gdb_disassembler::pretty_print_insn (struct ui_out *uiout,
   if (name != NULL)
     xfree (name);
 
-  ui_file_rewind (stb);
+  struct ui_file *stb = mem_fileopen ();
+  make_cleanup_ui_file_delete (stb);
+
   if (flags & DISASSEMBLY_RAW_INSN)
     {
       CORE_ADDR end_pc;
@@ -264,14 +264,12 @@ gdb_disassembler::pretty_print_insn (struct ui_out *uiout,
       struct cleanup *cleanups =
 	make_cleanup_ui_file_delete (opcode_stream);
 
-      size = print_insn (pc);
+      size = gdb_print_insn (gdbarch, pc, stb, NULL);
       end_pc = pc + size;
 
       for (;pc < end_pc; ++pc)
 	{
-	  err = m_di.read_memory_func (pc, &data, 1, &m_di);
-	  if (err != 0)
-	    m_di.memory_error_func (err, pc, &m_di);
+	  read_code (pc, &data, 1);
 	  fprintf_filtered (opcode_stream, "%s%02x",
 			    spacer, (unsigned) data);
 	  spacer = " ";
@@ -283,10 +281,9 @@ gdb_disassembler::pretty_print_insn (struct ui_out *uiout,
       do_cleanups (cleanups);
     }
   else
-    size = print_insn (pc);
+    size = gdb_print_insn (gdbarch, pc, stb, NULL);
 
   uiout->field_stream ("inst", stb);
-  ui_file_rewind (stb);
   do_cleanups (ui_out_chain);
   uiout->text ("\n");
 
@@ -294,10 +291,9 @@ gdb_disassembler::pretty_print_insn (struct ui_out *uiout,
 }
 
 static int
-dump_insns (struct ui_out *uiout, gdb_disassembler *di,
-	    CORE_ADDR low, CORE_ADDR high,
-	    int how_many, int flags,
-	    CORE_ADDR *end_pc)
+dump_insns (struct gdbarch *gdbarch,
+	    struct ui_out *uiout, CORE_ADDR low, CORE_ADDR high,
+	    int how_many, int flags, CORE_ADDR *end_pc)
 {
   struct disasm_insn insn;
   int num_displayed = 0;
@@ -309,7 +305,7 @@ dump_insns (struct ui_out *uiout, gdb_disassembler *di,
     {
       int size;
 
-      size = di->pretty_print_insn (uiout, &insn, flags);
+      size = gdb_pretty_print_insn (gdbarch, uiout, &insn, flags);
       if (size <= 0)
 	break;
 
@@ -335,8 +331,8 @@ dump_insns (struct ui_out *uiout, gdb_disassembler *di,
 
 static void
 do_mixed_source_and_assembly_deprecated
-  (struct ui_out *uiout,
-   gdb_disassembler *di, struct symtab *symtab,
+  (struct gdbarch *gdbarch, struct ui_out *uiout,
+   struct symtab *symtab,
    CORE_ADDR low, CORE_ADDR high,
    int how_many, int flags)
 {
@@ -471,7 +467,7 @@ do_mixed_source_and_assembly_deprecated
 	    = make_cleanup_ui_out_list_begin_end (uiout, "line_asm_insn");
 	}
 
-      num_displayed += dump_insns (uiout, di,
+      num_displayed += dump_insns (gdbarch, uiout,
 				   mle[i].start_pc, mle[i].end_pc,
 				   how_many, flags, NULL);
 
@@ -499,7 +495,6 @@ do_mixed_source_and_assembly_deprecated
 static void
 do_mixed_source_and_assembly (struct gdbarch *gdbarch,
 			      struct ui_out *uiout,
-			      gdb_disassembler *di,
 			      struct symtab *main_symtab,
 			      CORE_ADDR low, CORE_ADDR high,
 			      int how_many, int flags)
@@ -721,7 +716,7 @@ do_mixed_source_and_assembly (struct gdbarch *gdbarch,
 	end_pc = std::min (sal.end, high);
       else
 	end_pc = pc + 1;
-      num_displayed += dump_insns (uiout, di, pc, end_pc,
+      num_displayed += dump_insns (gdbarch, uiout, pc, end_pc,
 				   how_many, flags, &end_pc);
       pc = end_pc;
 
@@ -736,8 +731,7 @@ do_mixed_source_and_assembly (struct gdbarch *gdbarch,
 }
 
 static void
-do_assembly_only (struct ui_out *uiout,
-		  gdb_disassembler *di,
+do_assembly_only (struct gdbarch *gdbarch, struct ui_out *uiout,
 		  CORE_ADDR low, CORE_ADDR high,
 		  int how_many, int flags)
 {
@@ -745,7 +739,7 @@ do_assembly_only (struct ui_out *uiout,
 
   ui_out_chain = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
 
-  dump_insns (uiout, di, low, high, how_many, flags, NULL);
+  dump_insns (gdbarch, uiout, low, high, how_many, flags, NULL);
 
   do_cleanups (ui_out_chain);
 }
@@ -818,9 +812,6 @@ 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_file *stb = mem_fileopen ();
-  struct cleanup *cleanups = make_cleanup_ui_file_delete (stb);
-  gdb_disassembler di (gdbarch, stb);
   struct symtab *symtab;
   int nlines = -1;
 
@@ -832,17 +823,16 @@ gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
 
   if (!(flags & (DISASSEMBLY_SOURCE_DEPRECATED | DISASSEMBLY_SOURCE))
       || nlines <= 0)
-    do_assembly_only (uiout, &di, low, high, how_many, flags);
+    do_assembly_only (gdbarch, uiout, low, high, how_many, flags);
 
   else if (flags & DISASSEMBLY_SOURCE)
-    do_mixed_source_and_assembly (gdbarch, uiout, &di, symtab, low, high,
+    do_mixed_source_and_assembly (gdbarch, uiout, symtab, low, high,
 				  how_many, flags);
 
   else if (flags & DISASSEMBLY_SOURCE_DEPRECATED)
-    do_mixed_source_and_assembly_deprecated (uiout, &di, symtab,
+    do_mixed_source_and_assembly_deprecated (gdbarch, uiout, symtab,
 					     low, high, how_many, flags);
 
-  do_cleanups (cleanups);
   gdb_flush (gdb_stdout);
 }
 
diff --git a/gdb/disasm.h b/gdb/disasm.h
index 7bbfa31..dffe9cd 100644
--- a/gdb/disasm.h
+++ b/gdb/disasm.h
@@ -44,11 +44,6 @@ public:
 
   int print_insn (CORE_ADDR memaddr, int *branch_delay_insns = NULL);
 
-  /* Prints the instruction INSN into UIOUT and returns the length of
-     the printed instruction in bytes.  */
-  int pretty_print_insn (struct ui_out *uiout,
-			 const struct disasm_insn *insn, int flags);
-
   /* Return the gdbarch of gdb_disassembler.  */
   struct gdbarch *arch ()
   { return m_gdbarch; }
@@ -102,6 +97,12 @@ extern void gdb_disassembly (struct gdbarch *gdbarch, struct ui_out *uiout,
 extern int gdb_print_insn (struct gdbarch *gdbarch, CORE_ADDR memaddr,
 			   struct ui_file *stream, int *branch_delay_insns);
 
+/* Prints the instruction INSN into UIOUT and returns the length of
+   the printed instruction in bytes.  */
+
+extern int gdb_pretty_print_insn (struct gdbarch *gdbarch, struct ui_out *uiout,
+				  const struct disasm_insn *insn, int flags);
+
 /* Return the length in bytes of the instruction at address MEMADDR in
    debugged memory.  */
 
diff --git a/gdb/record-btrace.c b/gdb/record-btrace.c
index 8896241..80b8aff 100644
--- a/gdb/record-btrace.c
+++ b/gdb/record-btrace.c
@@ -696,7 +696,6 @@ btrace_insn_history (struct ui_out *uiout,
 		     const struct btrace_insn_iterator *begin,
 		     const struct btrace_insn_iterator *end, int flags)
 {
-  struct ui_file *stb;
   struct cleanup *cleanups, *ui_item_chain;
   struct gdbarch *gdbarch;
   struct btrace_insn_iterator it;
@@ -708,12 +707,9 @@ btrace_insn_history (struct ui_out *uiout,
   flags |= DISASSEMBLY_SPECULATIVE;
 
   gdbarch = target_gdbarch ();
-  stb = mem_fileopen ();
-  cleanups = make_cleanup_ui_file_delete (stb);
-  gdb_disassembler di (gdbarch, stb);
   last_lines = btrace_mk_line_range (NULL, 0, 0);
 
-  make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
+  cleanups = make_cleanup_ui_out_list_begin_end (uiout, "asm_insns");
 
   /* UI_ITEM_CHAIN is a cleanup chain for the last source line and the
      instructions corresponding to that line.  */
@@ -772,7 +768,7 @@ btrace_insn_history (struct ui_out *uiout,
 	  if ((insn->flags & BTRACE_INSN_FLAG_SPECULATIVE) != 0)
 	    dinsn.is_speculative = 1;
 
-	  di.pretty_print_insn (uiout, &dinsn, flags);
+	  gdb_pretty_print_insn (gdbarch, uiout, &dinsn, flags);
 	}
     }
 
-- 
2.5.5


  reply	other threads:[~2017-02-01  0:30 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-02-01  0:30 [PATCH v4 0/2] Eliminate cleanups & make ui_file a C++ class hierarchy Pedro Alves
2017-02-01  0:30 ` Pedro Alves [this message]
2017-02-01 17:01   ` [PATCH v4 1/2] Add back gdb_pretty_print_insn Luis Machado
2017-02-01 18:10   ` Simon Marchi
2017-02-01 18:26     ` Simon Marchi
2017-02-02  0:00       ` Pedro Alves
2017-02-01 20:02     ` Pedro Alves
2017-02-01 20:31       ` Pedro Alves
2017-02-01 23:50         ` Pedro Alves
2017-02-02  1:20           ` Simon Marchi
2017-02-02 11:37             ` [pushed] Reuse buffers across gdb_pretty_print_insn calls (Re: [PATCH v4 1/2] Add back gdb_pretty_print_insn) Pedro Alves
2017-02-01  0:31 ` [PATCH v4 2/2] Eliminate make_cleanup_ui_file_delete / make ui_file a class hierarchy Pedro Alves
2017-02-01 17:37   ` Luis Machado
2017-02-01 22:49     ` Pedro Alves
2017-02-01 23:24       ` Luis Machado
2017-02-02  0:02         ` Pedro Alves
2017-02-27 19:43           ` Edjunior Barbosa Machado
2017-03-07 14:02             ` Pedro Alves

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1485909045-30285-2-git-send-email-palves@redhat.com \
    --to=palves@redhat.com \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox