[v2,3/3] Use ui-out tables in linux-tdep.c

Message ID 20240303-info-proc-ui-out-v2-3-cf3d2013b33e@tromey.com
State New
Headers
Series Use ui-out tables in a few spots |

Commit Message

Tom Tromey March 3, 2024, 6:43 p.m. UTC
  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(-)
  

Patch

diff --git a/gdb/linux-tdep.c b/gdb/linux-tdep.c
index 701e3df7a27..4bac4d81abd 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;
-  std::string_view permissions;
+  std::string permissions;
   ULONGEST offset;
   std::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
@@ -1242,42 +1228,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;
+  std::optional<ui_out_emit_table> table_emitter;
+  ui_file_style filenames = file_name_style.style ();
+
   linux_read_core_file_mappings (gdbarch, current_program_space->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");
       });
 }
 
@@ -1390,7 +1368,7 @@  parse_smaps_data (const char *data,
 
       /* Decode permissions.  */
       auto has_perm = [&m] (char c)
-	{ return m.permissions.find (c) != std::string_view::npos; };
+	{ return m.permissions.find (c) != std::string::npos; };
       read = has_perm ('r');
       write = has_perm ('w');
       exec = has_perm ('x');