public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/3] Use ui-out tables in a few spots
@ 2023-08-09 16:40 Tom Tromey
  2023-08-09 16:40 ` [PATCH 1/3] Use ui-out in maintenance_print_user_registers Tom Tromey
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Tom Tromey @ 2023-08-09 16:40 UTC (permalink / raw)
  To: gdb-patches

This series changes a few spots to use ui-out tables rather than
hand-aligned printf-based ones.  I think ui-out tables should
generally be preferred:

* They are more flexible and easier to use

* They sometimes provide automatic styling (like addresses in this
  series)

* They allow the possibility of structured reuse via MI

There are still a few such tables left in gdb after this series, but
they are all in code I can't readily test: fbsd-tdep.c,
darwin-nat-info.c, and netbsd-tdep.c.

Regression tested on x86-64 Fedora 38.

---
Tom Tromey (3):
      Use ui-out in maintenance_print_user_registers
      Use ui-out in core_target::info_proc_mappings
      Use ui-out tables in linux-tdep.c

 gdb/corelow.c    |  54 +++++++++++---------------
 gdb/linux-tdep.c | 114 ++++++++++++++++++++++---------------------------------
 gdb/user-regs.c  |  14 ++++++-
 3 files changed, 80 insertions(+), 102 deletions(-)
---
base-commit: 3cdc2d7e66ab6a48014dcd425c88cfd42a964321
change-id: 20230809-info-proc-ui-out-ce83b44285ec

Best regards,
-- 
Tom Tromey <tom@tromey.com>


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

* [PATCH 1/3] Use ui-out in maintenance_print_user_registers
  2023-08-09 16:40 [PATCH 0/3] Use ui-out tables in a few spots Tom Tromey
@ 2023-08-09 16:40 ` Tom Tromey
  2023-08-11  9:53   ` Christophe Lyon
  2023-08-09 16:40 ` [PATCH 2/3] Use ui-out in core_target::info_proc_mappings Tom Tromey
  2023-08-09 16:41 ` [PATCH 3/3] Use ui-out tables in linux-tdep.c Tom Tromey
  2 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2023-08-09 16:40 UTC (permalink / raw)
  To: gdb-patches

This changes maintenance_print_user_registers to use a ui-out table.
---
 gdb/user-regs.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/gdb/user-regs.c b/gdb/user-regs.c
index 25c57d4886c..503a41162c6 100644
--- a/gdb/user-regs.c
+++ b/gdb/user-regs.c
@@ -223,9 +223,19 @@ maintenance_print_user_registers (const char *args, int from_tty)
   struct gdb_user_regs *regs = get_user_regs (gdbarch);
   regnum = gdbarch_num_cooked_regs (gdbarch);
 
-  gdb_printf (" %-11s %3s\n", "Name", "Nr");
+  struct ui_out *uiout = current_uiout;
+  ui_out_emit_table table_emitter (uiout, 2, -1, "user-registers");
+  uiout->table_header (11, ui_left, "name", "Name");
+  uiout->table_header (3, ui_right, "number", "Nr");
+
+  uiout->table_body ();
   for (reg = regs->first; reg != NULL; reg = reg->next, ++regnum)
-    gdb_printf (" %-11s %3d\n", reg->name, regnum);
+    {
+      ui_out_emit_tuple tuple_emitter (uiout, nullptr);
+      uiout->field_string ("name", reg->name);
+      uiout->field_signed ("number", regnum);
+      uiout->text ("\n");
+    }
 }
 
 void _initialize_user_regs ();

-- 
2.41.0


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

* [PATCH 2/3] Use ui-out in core_target::info_proc_mappings
  2023-08-09 16:40 [PATCH 0/3] Use ui-out tables in a few spots Tom Tromey
  2023-08-09 16:40 ` [PATCH 1/3] Use ui-out in maintenance_print_user_registers Tom Tromey
@ 2023-08-09 16:40 ` Tom Tromey
  2023-08-09 16:41 ` [PATCH 3/3] Use ui-out tables in linux-tdep.c Tom Tromey
  2 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2023-08-09 16:40 UTC (permalink / raw)
  To: gdb-patches

This changes core_target::info_proc_mappings to use a ui-out table.
It also changes the output to style the file names.
---
 gdb/corelow.c | 54 ++++++++++++++++++++++--------------------------------
 1 file changed, 22 insertions(+), 32 deletions(-)

diff --git a/gdb/corelow.c b/gdb/corelow.c
index 46bb1077b6d..b8132ccc380 100644
--- a/gdb/corelow.c
+++ b/gdb/corelow.c
@@ -53,6 +53,7 @@
 #include "gdbcmd.h"
 #include "xml-tdesc.h"
 #include "memtag.h"
+#include "cli/cli-style.h"
 
 #ifndef O_LARGEFILE
 #define O_LARGEFILE 0
@@ -1402,25 +1403,21 @@ get_current_core_target ()
 void
 core_target::info_proc_mappings (struct gdbarch *gdbarch)
 {
-  if (!m_core_file_mappings.empty ())
-    {
-      gdb_printf (_("Mapped address spaces:\n\n"));
-      if (gdbarch_addr_bit (gdbarch) == 32)
-	{
-	  gdb_printf ("\t%10s %10s %10s %10s %s\n",
-		      "Start Addr",
-		      "  End Addr",
-		      "      Size", "    Offset", "objfile");
-	}
-      else
-	{
-	  gdb_printf ("  %18s %18s %10s %10s %s\n",
-		      "Start Addr",
-		      "  End Addr",
-		      "      Size", "    Offset", "objfile");
-	}
-    }
+  if (m_core_file_mappings.empty ())
+    return;
 
+  struct ui_out *uiout = current_uiout;
+  uiout->text (_("Mapped address spaces:\n\n"));
+  ui_out_emit_table table_emitter (uiout, 5, -1, "mappings");
+  int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
+  uiout->table_header (width, ui_right, "start", "Start Addr");
+  uiout->table_header (width, ui_right, "end", "End Addr");
+  uiout->table_header (10, ui_right, "size", "Size");
+  uiout->table_header (10, ui_right, "offset", "Offset");
+  uiout->table_header (10, ui_left, "objfile", "objfile");
+
+  uiout->table_body ();
+  ui_file_style filenames = file_name_style.style ();
   for (const target_section &tsp : m_core_file_mappings)
     {
       ULONGEST start = tsp.addr;
@@ -1428,20 +1425,13 @@ core_target::info_proc_mappings (struct gdbarch *gdbarch)
       ULONGEST file_ofs = tsp.the_bfd_section->filepos;
       const char *filename = bfd_get_filename (tsp.the_bfd_section->owner);
 
-      if (gdbarch_addr_bit (gdbarch) == 32)
-	gdb_printf ("\t%10s %10s %10s %10s %s\n",
-		    paddress (gdbarch, start),
-		    paddress (gdbarch, end),
-		    hex_string (end - start),
-		    hex_string (file_ofs),
-		    filename);
-      else
-	gdb_printf ("  %18s %18s %10s %10s %s\n",
-		    paddress (gdbarch, start),
-		    paddress (gdbarch, end),
-		    hex_string (end - start),
-		    hex_string (file_ofs),
-		    filename);
+      ui_out_emit_tuple tuple_emitter (uiout, nullptr);
+      uiout->field_core_addr ("start", gdbarch, start);
+      uiout->field_core_addr ("end", gdbarch, end);
+      uiout->field_string ("size", hex_string (end - start));
+      uiout->field_string ("offset", hex_string (file_ofs));
+      uiout->field_string ("objfile", filename, filenames);
+      uiout->text ("\n");
     }
 }
 

-- 
2.41.0


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

* [PATCH 3/3] Use ui-out tables in linux-tdep.c
  2023-08-09 16:40 [PATCH 0/3] Use ui-out tables in a few spots Tom Tromey
  2023-08-09 16:40 ` [PATCH 1/3] Use ui-out in maintenance_print_user_registers Tom Tromey
  2023-08-09 16:40 ` [PATCH 2/3] Use ui-out in core_target::info_proc_mappings Tom Tromey
@ 2023-08-09 16:41 ` Tom Tromey
  2023-08-11  7:12   ` Alexandra Petlanova Hajkova
  2 siblings, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2023-08-09 16:41 UTC (permalink / raw)
  To: gdb-patches

This changes a couple of spots in linux-tdep.c to use a ui-out table.
It also changes the output to style the file names.
---
 gdb/linux-tdep.c | 114 ++++++++++++++++++++++---------------------------------
 1 file changed, 46 insertions(+), 68 deletions(-)

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index b5eee5e108c..b1f181f6604 100644
--- a/gdb/linux-tdep.c
+++ b/gdb/linux-tdep.c
@@ -43,6 +43,7 @@
 #include "gcore-elf.h"
 #include "solib-svr4.h"
 #include "memtag.h"
+#include "cli/cli-style.h"
 
 #include <ctype.h>
 #include <unordered_map>
@@ -457,7 +458,7 @@ struct mapping
 {
   ULONGEST addr;
   ULONGEST endaddr;
-  gdb::string_view permissions;
+  std::string permissions;
   ULONGEST offset;
   gdb::string_view device;
   ULONGEST inode;
@@ -899,20 +900,19 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
 	{
 	  char *line;
 
-	  gdb_printf (_("Mapped address spaces:\n\n"));
-	  if (gdbarch_addr_bit (gdbarch) == 32)
-	    {
-	      gdb_printf ("\t%10s %10s %10s %10s  %s %s\n",
-			  "Start Addr", "  End Addr", "      Size",
-			  "    Offset", "Perms  ", "objfile");
-	    }
-	  else
-	    {
-	      gdb_printf ("  %18s %18s %10s %10s  %s %s\n",
-			  "Start Addr", "  End Addr", "      Size",
-			  "    Offset", "Perms ", "objfile");
-	    }
-
+	  struct ui_out *uiout = current_uiout;
+	  uiout->text (_("Mapped address spaces:\n\n"));
+	  ui_out_emit_table table_emitter (uiout, 6, -1, "mappings");
+	  int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
+	  uiout->table_header (width, ui_right, "start", "Start Addr");
+	  uiout->table_header (width, ui_right, "end", "End Addr");
+	  uiout->table_header (10, ui_right, "size", "Size");
+	  uiout->table_header (10, ui_right, "offset", "Offset");
+	  uiout->table_header (5, ui_right, "permissions", "Perms");
+	  uiout->table_header (10, ui_left, "objfile", "objfile");
+
+	  uiout->table_body ();
+	  ui_file_style filenames = file_name_style.style ();
 	  char *saveptr;
 	  for (line = strtok_r (map.get (), "\n", &saveptr);
 	       line;
@@ -920,28 +920,14 @@ linux_info_proc (struct gdbarch *gdbarch, const char *args,
 	    {
 	      struct mapping m = read_mapping (line);
 
-	      if (gdbarch_addr_bit (gdbarch) == 32)
-		{
-		  gdb_printf ("\t%10s %10s %10s %10s  %-5.*s  %s\n",
-			      paddress (gdbarch, m.addr),
-			      paddress (gdbarch, m.endaddr),
-			      hex_string (m.endaddr - m.addr),
-			      hex_string (m.offset),
-			      (int) m.permissions.size (),
-			      m.permissions.data (),
-			      m.filename);
-		}
-	      else
-		{
-		  gdb_printf ("  %18s %18s %10s %10s  %-5.*s  %s\n",
-			      paddress (gdbarch, m.addr),
-			      paddress (gdbarch, m.endaddr),
-			      hex_string (m.endaddr - m.addr),
-			      hex_string (m.offset),
-			      (int) m.permissions.size (),
-			      m.permissions.data (),
-			      m.filename);
-		}
+	      ui_out_emit_tuple tuple_emitter (uiout, nullptr);
+	      uiout->field_core_addr ("start", gdbarch, m.addr);
+	      uiout->field_core_addr ("end", gdbarch, m.endaddr);
+	      uiout->field_string ("size", hex_string (m.endaddr - m.addr));
+	      uiout->field_string ("offset", hex_string (m.offset));
+	      uiout->field_string ("permissions", m.permissions);
+	      uiout->field_string ("objfile", m.filename, filenames);
+	      uiout->text ("\n");
 	    }
 	}
       else
@@ -1239,42 +1225,34 @@ linux_read_core_file_mappings
 static void
 linux_core_info_proc_mappings (struct gdbarch *gdbarch, const char *args)
 {
+  struct ui_out *uiout = current_uiout;
+  gdb::optional<ui_out_emit_table> table_emitter;
+  ui_file_style filenames = file_name_style.style ();
+
   linux_read_core_file_mappings (gdbarch, core_bfd,
-    [=] (ULONGEST count)
+    [&] (ULONGEST count)
       {
 	gdb_printf (_("Mapped address spaces:\n\n"));
-	if (gdbarch_addr_bit (gdbarch) == 32)
-	  {
-	    gdb_printf ("\t%10s %10s %10s %10s %s\n",
-			"Start Addr",
-			"  End Addr",
-			"      Size", "    Offset", "objfile");
-	  }
-	else
-	  {
-	    gdb_printf ("  %18s %18s %10s %10s %s\n",
-			"Start Addr",
-			"  End Addr",
-			"      Size", "    Offset", "objfile");
-	  }
+	table_emitter.emplace (uiout, 5, -1, "mappings");
+	int width = gdbarch_addr_bit (gdbarch) == 32 ? 10 : 18;
+	uiout->table_header (width, ui_right, "start", "Start Addr");
+	uiout->table_header (width, ui_right, "end", "End Addr");
+	uiout->table_header (10, ui_right, "size", "Size");
+	uiout->table_header (10, ui_right, "offset", "Offset");
+	uiout->table_header (10, ui_left, "objfile", "objfile");
+
+	uiout->table_body ();
       },
-    [=] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
+    [&] (int num, ULONGEST start, ULONGEST end, ULONGEST file_ofs,
 	 const char *filename, const bfd_build_id *build_id)
       {
-	if (gdbarch_addr_bit (gdbarch) == 32)
-	  gdb_printf ("\t%10s %10s %10s %10s %s\n",
-		      paddress (gdbarch, start),
-		      paddress (gdbarch, end),
-		      hex_string (end - start),
-		      hex_string (file_ofs),
-		      filename);
-	else
-	  gdb_printf ("  %18s %18s %10s %10s %s\n",
-		      paddress (gdbarch, start),
-		      paddress (gdbarch, end),
-		      hex_string (end - start),
-		      hex_string (file_ofs),
-		      filename);
+	ui_out_emit_tuple tuple_emitter (uiout, nullptr);
+	uiout->field_core_addr ("start", gdbarch, start);
+	uiout->field_core_addr ("end", gdbarch, end);
+	uiout->field_string ("size", hex_string (end - start));
+	uiout->field_string ("offset", hex_string (file_ofs));
+	uiout->field_string ("objfile", filename, filenames);
+	uiout->text ("\n");
       });
 }
 
@@ -1384,7 +1362,7 @@ parse_smaps_data (const char *data,
 
       /* Decode permissions.  */
       auto has_perm = [&m] (char c)
-	{ return m.permissions.find (c) != gdb::string_view::npos; };
+	{ return m.permissions.find (c) != std::string::npos; };
       read = has_perm ('r');
       write = has_perm ('w');
       exec = has_perm ('x');

-- 
2.41.0


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

* Re: [PATCH 3/3] Use ui-out tables in linux-tdep.c
  2023-08-09 16:41 ` [PATCH 3/3] Use ui-out tables in linux-tdep.c Tom Tromey
@ 2023-08-11  7:12   ` Alexandra Petlanova Hajkova
  0 siblings, 0 replies; 6+ messages in thread
From: Alexandra Petlanova Hajkova @ 2023-08-11  7:12 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 672 bytes --]

On Wed, Aug 9, 2023 at 6:42 PM Tom Tromey <tom@tromey.com> wrote:

> This changes a couple of spots in linux-tdep.c to use a ui-out table.
> It also changes the output to style the file names.
> ---
>  gdb/linux-tdep.c | 114
> ++++++++++++++++++++++---------------------------------
>  1 file changed, 46 insertions(+), 68 deletions(-)
>
> Hi,


I regression tested this patch set on aarch64 and there seems to be a
regression - one failure for gdb.base/completion.exp:
(gdb) FAIL: gdb.base/completion.exp: complete 'info registers '

When I run this test on top of the master it passes:
(gdb) PASS: gdb.base/completion.exp: complete 'info registers '

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

* Re: [PATCH 1/3] Use ui-out in maintenance_print_user_registers
  2023-08-09 16:40 ` [PATCH 1/3] Use ui-out in maintenance_print_user_registers Tom Tromey
@ 2023-08-11  9:53   ` Christophe Lyon
  0 siblings, 0 replies; 6+ messages in thread
From: Christophe Lyon @ 2023-08-11  9:53 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

[-- Attachment #1: Type: text/plain, Size: 2463 bytes --]

Hi Tom,


On Wed, 9 Aug 2023 at 18:41, Tom Tromey <tom@tromey.com> wrote:

> This changes maintenance_print_user_registers to use a ui-out table.
> ---
>  gdb/user-regs.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
>
> diff --git a/gdb/user-regs.c b/gdb/user-regs.c
> index 25c57d4886c..503a41162c6 100644
> --- a/gdb/user-regs.c
> +++ b/gdb/user-regs.c
> @@ -223,9 +223,19 @@ maintenance_print_user_registers (const char *args,
> int from_tty)
>    struct gdb_user_regs *regs = get_user_regs (gdbarch);
>    regnum = gdbarch_num_cooked_regs (gdbarch);
>
> -  gdb_printf (" %-11s %3s\n", "Name", "Nr");
> +  struct ui_out *uiout = current_uiout;
> +  ui_out_emit_table table_emitter (uiout, 2, -1, "user-registers");
> +  uiout->table_header (11, ui_left, "name", "Name");
> +  uiout->table_header (3, ui_right, "number", "Nr");
> +
> +  uiout->table_body ();
>    for (reg = regs->first; reg != NULL; reg = reg->next, ++regnum)
> -    gdb_printf (" %-11s %3d\n", reg->name, regnum);
> +    {
> +      ui_out_emit_tuple tuple_emitter (uiout, nullptr);
> +      uiout->field_string ("name", reg->name);
> +      uiout->field_signed ("number", regnum);
> +      uiout->text ("\n");
> +    }
>  }
>
>  void _initialize_user_regs ();
>
> --
> 2.41.0
>
>
Our pre-commit CI has noticed that this causes a regression on aarch64 in
gdb.base/completion.exp:
 FAIL: gdb.base/completion.exp: complete 'info registers '

I digged a bit, and it seems related to the fact that there's a slight
spacing difference.
Before your patch mt print user-registers prints:
 fp          260
(with a leading space)
after your patch, it prints:
fp          260
(with a trailing space)

Accepting no space before the register name in completion.exp is enough to
make the test pass again, but I guess you probably want to fix the actual
output instead?
diff --git a/gdb/testsuite/gdb.base/completion.exp
b/gdb/testsuite/gdb.base/completion.exp
index 4686e6f8f34..07a91c90f73 100644






--- a/gdb/testsuite/gdb.base/completion.exp






+++ b/gdb/testsuite/gdb.base/completion.exp



@@ -145,7 +145,7 @@ append regs_output "\n"

 append regs_output [capture_command_output "mt print user-registers" \

                     ".*Name.*Nr\[^\n]*\n"]
 set all_regs {}

-foreach {- reg} [regexp -all -inline -line {^\s+(\w+)} $regs_output] {

+foreach {- reg} [regexp -all -inline -line {^\s*(\w+)} $regs_output] {

     lappend all_regs $reg

 }

Thanks,

Christophe

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

end of thread, other threads:[~2023-08-11  9:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-09 16:40 [PATCH 0/3] Use ui-out tables in a few spots Tom Tromey
2023-08-09 16:40 ` [PATCH 1/3] Use ui-out in maintenance_print_user_registers Tom Tromey
2023-08-11  9:53   ` Christophe Lyon
2023-08-09 16:40 ` [PATCH 2/3] Use ui-out in core_target::info_proc_mappings Tom Tromey
2023-08-09 16:41 ` [PATCH 3/3] Use ui-out tables in linux-tdep.c Tom Tromey
2023-08-11  7:12   ` Alexandra Petlanova Hajkova

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).