public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: remove maint_print_section_data
@ 2020-08-27 13:18 Simon Marchi
  2020-09-02 14:53 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Simon Marchi @ 2020-08-27 13:18 UTC (permalink / raw)
  To: gdb-patches

Since the "maintenance info sections" helper functions are not used
through a callback with a void* parameter anymore, the
maint_print_section_data is not needed anymore.  Remove it, replace it
with regular parameters.

Break out the index digits computation in its own function.

gdb/ChangeLog:

	* maint.c (index_digits): New function.
	(struct maint_print_section_data): Remove.
	(print_bfd_section_info): Remove print_data parameter, add arg
	and index_digits.
	(print_objfile_section_info): Likewise.
	(print_bfd_section_info_maybe_relocated): Likewise (plus
	objfile).
	(maintenance_info_sections): Adjust calls.

Change-Id: Idfeca5e7e0a95e72fade15cb1488058865c0258e
---
 gdb/maint.c | 101 ++++++++++++++++++++++------------------------------
 1 file changed, 42 insertions(+), 59 deletions(-)

diff --git a/gdb/maint.c b/gdb/maint.c
index be0b1605159a..46dd99f6ccf6 100644
--- a/gdb/maint.c
+++ b/gdb/maint.c
@@ -245,37 +245,16 @@ maint_print_section_info (const char *name, flagword flags,
   printf_filtered ("\n");
 }
 
-/* Information passed between the "maintenance info sections" command, and
-   the worker function that prints each section.  */
-struct maint_print_section_data
+/* Return the number of digits required to display COUNT in decimal.
+
+   Used when pretty printing index numbers to ensure all of the indexes line
+   up.*/
+
+static int
+index_digits (int count)
 {
-  /* The GDB objfile we're printing this section for.  */
-  struct objfile *objfile;
-
-  /* The argument string passed by the user to the top level maintenance
-     info sections command.  Used for filtering which sections are
-     printed.  */
-  const char *arg;
-
-  /* The number of digits in the highest section index for all sections
-     from the bfd object associated with OBJFILE.  Used when pretty
-     printing the index number to ensure all of the indexes line up.  */
-  int index_digits;
-
-  /* Constructor.  */
-  maint_print_section_data (struct objfile *objfile, const char *arg,
-			    bfd *abfd)
-    : objfile (objfile),
-      arg (arg)
-  {
-    int section_count = gdb_bfd_count_sections (abfd);
-    index_digits = ((int) log10 ((float) section_count)) + 1;
-  }
-
-private:
-  maint_print_section_data () = delete;
-  maint_print_section_data (const maint_print_section_data &) = delete;
-};
+  return ((int) log10 ((float) count)) + 1;
+}
 
 /* Helper function to pretty-print the section index of ASECT from ABFD.
    The INDEX_DIGITS is the number of digits in the largest index that will
@@ -295,16 +274,17 @@ print_section_index (bfd *abfd,
 /* Print information about ASECT from ABFD.  The section will be printed using
    the VMA's from the bfd, which will not be the relocated addresses for bfds
    that should be relocated.  The information must be printed with the same
-   layout as PRINT_OBJFILE_SECTION_INFO below.  */
+   layout as PRINT_OBJFILE_SECTION_INFO below.
+
+   ARG is the argument string passed by the user to the top level maintenance
+   info sections command.  Used for filtering which sections are printed.  */
 
 static void
-print_bfd_section_info (bfd *abfd,
-			asection *asect,
-			const maint_print_section_data &print_data)
+print_bfd_section_info (bfd *abfd, asection *asect, const char *arg,
+			int index_digits)
 {
   flagword flags = bfd_section_flags (asect);
   const char *name = bfd_section_name (asect);
-  const char *arg = print_data.arg;
 
   if (arg == NULL || *arg == '\0'
       || match_substring (arg, name)
@@ -316,7 +296,7 @@ print_bfd_section_info (bfd *abfd,
 
       addr = bfd_section_vma (asect);
       endaddr = addr + bfd_section_size (asect);
-      print_section_index (abfd, asect, print_data.index_digits);
+      print_section_index (abfd, asect, index_digits);
       maint_print_section_info (name, flags, addr, endaddr,
 				asect->filepos, addr_size);
     }
@@ -325,26 +305,26 @@ print_bfd_section_info (bfd *abfd,
 /* Print information about ASECT which is GDB's wrapper around a section
    from ABFD.  The information must be printed with the same layout as
    PRINT_BFD_SECTION_INFO above.  PRINT_DATA holds information used to
-   filter which sections are printed, and for formatting the output.  */
+   filter which sections are printed, and for formatting the output.
+
+   ARG is the argument string passed by the user to the top level maintenance
+   info sections command.  Used for filtering which sections are printed.  */
 
 static void
-print_objfile_section_info (bfd *abfd,
-			    struct obj_section *asect,
-			    const maint_print_section_data &print_data)
+print_objfile_section_info (bfd *abfd, struct obj_section *asect,
+			    const char *arg, int index_digits)
 {
   flagword flags = bfd_section_flags (asect->the_bfd_section);
   const char *name = bfd_section_name (asect->the_bfd_section);
-  const char *string = print_data.arg;
 
-  if (string == NULL || *string == '\0'
-      || match_substring (string, name)
-      || match_bfd_flags (string, flags))
+  if (arg == NULL || *arg == '\0'
+      || match_substring (arg, name)
+      || match_bfd_flags (arg, flags))
     {
       struct gdbarch *gdbarch = gdbarch_from_bfd (abfd);
       int addr_size = gdbarch_addr_bit (gdbarch) / 8;
 
-      print_section_index (abfd, asect->the_bfd_section,
-			   print_data.index_digits);
+      print_section_index (abfd, asect->the_bfd_section, index_digits);
       maint_print_section_info (name, flags,
 				obj_section_addr (asect),
 				obj_section_endaddr (asect),
@@ -375,22 +355,24 @@ maint_obj_section_from_bfd_section (bfd *abfd,
 }
 
 /* Print information about ASECT from ABFD.  Where possible the information for
-   ASECT will print the relocated addresses of the section.  */
+   ASECT will print the relocated addresses of the section.
+
+   ARG is the argument string passed by the user to the top level maintenance
+   info sections command.  Used for filtering which sections are printed.  */
 
 static void
-print_bfd_section_info_maybe_relocated
-  (bfd *abfd, asection *asect, const maint_print_section_data &print_data)
+print_bfd_section_info_maybe_relocated (bfd *abfd, asection *asect,
+					objfile *objfile, const char *arg,
+					int index_digits)
 {
-  objfile *objfile = print_data.objfile;
-
   gdb_assert (objfile->sections != NULL);
   obj_section *osect
     = maint_obj_section_from_bfd_section (abfd, asect, objfile);
 
   if (osect->the_bfd_section == NULL)
-    print_bfd_section_info (abfd, asect, print_data);
+    print_bfd_section_info (abfd, asect, arg, index_digits);
   else
-    print_objfile_section_info (abfd, osect, print_data);
+    print_objfile_section_info (abfd, osect, arg, index_digits);
 }
 
 /* Implement the "maintenance info sections" command.  */
@@ -425,25 +407,26 @@ maintenance_info_sections (const char *arg, int from_tty)
 	  else if (ofile->obfd != exec_bfd)
 	    continue;
 
-	  maint_print_section_data print_data (ofile, arg, ofile->obfd);
+	  int section_count = gdb_bfd_count_sections (ofile->obfd);
 
 	  for (asection *sect : gdb_bfd_sections (ofile->obfd))
-	    print_bfd_section_info_maybe_relocated (ofile->obfd, sect,
-						    print_data);
+	    print_bfd_section_info_maybe_relocated
+	      (ofile->obfd, sect, ofile, arg, index_digits (section_count));
 	}
     }
 
   if (core_bfd)
     {
-      maint_print_section_data print_data (nullptr, arg, core_bfd);
-
       printf_filtered (_("Core file:\n"));
       printf_filtered ("    `%s', ", bfd_get_filename (core_bfd));
       wrap_here ("        ");
       printf_filtered (_("file type %s.\n"), bfd_get_target (core_bfd));
 
+      int section_count = gdb_bfd_count_sections (core_bfd);
+
       for (asection *sect : gdb_bfd_sections (core_bfd))
-	print_bfd_section_info (core_bfd, sect, print_data);
+	print_bfd_section_info (core_bfd, sect, arg,
+				index_digits (section_count));
     }
 }
 
-- 
2.28.0


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

* Re: [PATCH] gdb: remove maint_print_section_data
  2020-08-27 13:18 [PATCH] gdb: remove maint_print_section_data Simon Marchi
@ 2020-09-02 14:53 ` Tom Tromey
  2020-09-02 17:57   ` Simon Marchi
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2020-09-02 14:53 UTC (permalink / raw)
  To: Simon Marchi via Gdb-patches

>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:

Simon> Break out the index digits computation in its own function.

This is also done in the TUI; I guess we could have an inline function
in a header somewhere.  (Not needed for this patch, just an observation,
though.)

Simon> 	* maint.c (index_digits): New function.
Simon> 	(struct maint_print_section_data): Remove.
Simon> 	(print_bfd_section_info): Remove print_data parameter, add arg
Simon> 	and index_digits.
Simon> 	(print_objfile_section_info): Likewise.
Simon> 	(print_bfd_section_info_maybe_relocated): Likewise (plus
Simon> 	objfile).
Simon> 	(maintenance_info_sections): Adjust calls.

Looks good.

Tom

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

* Re: [PATCH] gdb: remove maint_print_section_data
  2020-09-02 14:53 ` Tom Tromey
@ 2020-09-02 17:57   ` Simon Marchi
  0 siblings, 0 replies; 3+ messages in thread
From: Simon Marchi @ 2020-09-02 17:57 UTC (permalink / raw)
  To: Tom Tromey, Simon Marchi via Gdb-patches

On 2020-09-02 10:53 a.m., Tom Tromey wrote:
>>>>>> "Simon" == Simon Marchi via Gdb-patches <gdb-patches@sourceware.org> writes:
> 
> Simon> Break out the index digits computation in its own function.
> 
> This is also done in the TUI; I guess we could have an inline function
> in a header somewhere.  (Not needed for this patch, just an observation,
> though.)

Good to know, I'll look into making another patch to share it with the TUI then.

> 
> Simon> 	* maint.c (index_digits): New function.
> Simon> 	(struct maint_print_section_data): Remove.
> Simon> 	(print_bfd_section_info): Remove print_data parameter, add arg
> Simon> 	and index_digits.
> Simon> 	(print_objfile_section_info): Likewise.
> Simon> 	(print_bfd_section_info_maybe_relocated): Likewise (plus
> Simon> 	objfile).
> Simon> 	(maintenance_info_sections): Adjust calls.
> 
> Looks good.

Thanks, I'll push it.

Simon

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

end of thread, other threads:[~2020-09-02 17:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-27 13:18 [PATCH] gdb: remove maint_print_section_data Simon Marchi
2020-09-02 14:53 ` Tom Tromey
2020-09-02 17:57   ` Simon Marchi

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).