public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
From: Simon Marchi <simon.marchi@polymtl.ca>
To: gdb-patches@sourceware.org
Cc: Simon Marchi <simon.marchi@polymtl.ca>
Subject: [PATCH 2/2] gdb: convert linux-osdata.c from buffer to std::string
Date: Fri, 16 Dec 2022 01:01:48 -0500	[thread overview]
Message-ID: <20221216060148.1261366-3-simon.marchi@polymtl.ca> (raw)
In-Reply-To: <20221216060148.1261366-1-simon.marchi@polymtl.ca>

Replace the use of struct buffer in linux-osdata.c with std::string.
There is not change in the logic, so there should be no user-visible
change.

Change-Id: I27f53165d401650bbd0bebe8ed88221e25545b3f
---
 gdb/nat/linux-osdata.c | 287 +++++++++++++++++++++--------------------
 1 file changed, 148 insertions(+), 139 deletions(-)

diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index 8639f090910a..73a052560264 100644
--- a/gdb/nat/linux-osdata.c
+++ b/gdb/nat/linux-osdata.c
@@ -333,12 +333,11 @@ get_core_array_size ()
   return sysconf (_SC_NPROCESSORS_ONLN);
 }
 
-static void
-linux_xfer_osdata_processes (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_processes ()
 {
   DIR *dirp;
-
-  buffer_grow_str (buffer, "<osdata type=\"processes\">\n");
+  std::string buffer = "<osdata type=\"processes\">\n";
 
   dirp = opendir ("/proc");
   if (dirp)
@@ -385,7 +384,7 @@ linux_xfer_osdata_processes (struct buffer *buffer)
 
 	  xfree (cores);
 
-	  buffer_xml_printf
+	  string_xml_appendf
 	    (buffer,
 	     "<item>"
 	     "<column name=\"pid\">%lld</column>"
@@ -404,7 +403,9 @@ linux_xfer_osdata_processes (struct buffer *buffer)
       closedir (dirp);
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* A simple PID/PGID pair.  */
@@ -446,12 +447,11 @@ struct pid_pgid_entry
 
 /* Collect all process groups from /proc in BUFFER.  */
 
-static void
-linux_xfer_osdata_processgroups (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_processgroups ()
 {
   DIR *dirp;
-
-  buffer_grow_str (buffer, "<osdata type=\"process groups\">\n");
+  std::string buffer = "<osdata type=\"process groups\">\n";
 
   dirp = opendir ("/proc");
   if (dirp)
@@ -493,7 +493,7 @@ linux_xfer_osdata_processgroups (struct buffer *buffer)
 	  command_from_pid (leader_command, sizeof (leader_command), pgid);
 	  command_line = commandline_from_pid (pid);
 
-	  buffer_xml_printf
+	  string_xml_appendf
 	    (buffer,
 	     "<item>"
 	     "<column name=\"pgid\">%lld</column>"
@@ -510,18 +510,19 @@ linux_xfer_osdata_processgroups (struct buffer *buffer)
 	}
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Collect all the threads in /proc by iterating through processes and
-   then tasks within each process in BUFFER.  */
+   then tasks within each process.  */
 
-static void
-linux_xfer_osdata_threads (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_threads ()
 {
   DIR *dirp;
-
-  buffer_grow_str (buffer, "<osdata type=\"threads\">\n");
+  std::string buffer = "<osdata type=\"threads\">\n";
 
   dirp = opendir ("/proc");
   if (dirp)
@@ -570,7 +571,7 @@ linux_xfer_osdata_threads (struct buffer *buffer)
 		      tid = atoi (dp2->d_name);
 		      core = linux_common_core_of_thread (ptid_t (pid, tid));
 
-		      buffer_xml_printf
+		      string_xml_appendf
 			(buffer,
 			 "<item>"
 			 "<column name=\"pid\">%lld</column>"
@@ -592,17 +593,18 @@ linux_xfer_osdata_threads (struct buffer *buffer)
       closedir (dirp);
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Collect data about the cpus/cores on the system in BUFFER.  */
 
-static void
-linux_xfer_osdata_cpus (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_cpus ()
 {
   int first_item = 1;
-
-  buffer_grow_str (buffer, "<osdata type=\"cpus\">\n");
+  std::string buffer = "<osdata type=\"cpus\">\n";
 
   gdb_file_up fp = gdb_fopen_cloexec ("/proc/cpuinfo", "r");
   if (fp != NULL)
@@ -639,37 +641,38 @@ linux_xfer_osdata_cpus (struct buffer *buffer)
 	      if (strcmp (key, "processor") == 0)
 		{
 		  if (first_item)
-		    buffer_grow_str (buffer, "<item>");
+		    buffer += "<item>";
 		  else
-		    buffer_grow_str (buffer, "</item><item>");
+		    buffer += "</item><item>";
 
 		  first_item = 0;
 		}
 
-	      buffer_xml_printf (buffer,
-				 "<column name=\"%s\">%s</column>",
-				 key,
-				 value);
+	      string_xml_appendf (buffer,
+				  "<column name=\"%s\">%s</column>",
+				  key,
+				  value);
 	    }
 	}
       while (!feof (fp.get ()));
 
       if (first_item == 0)
-	buffer_grow_str (buffer, "</item>");
+	buffer += "</item>";
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Collect all the open file descriptors found in /proc and put the details
    found about them into BUFFER.  */
 
-static void
-linux_xfer_osdata_fds (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_fds ()
 {
   DIR *dirp;
-
-  buffer_grow_str (buffer, "<osdata type=\"files\">\n");
+  std::string buffer = "<osdata type=\"files\">\n";
 
   dirp = opendir ("/proc");
   if (dirp)
@@ -721,7 +724,7 @@ linux_xfer_osdata_fds (struct buffer *buffer)
 		      if (rslt >= 0)
 			buf[rslt] = '\0';
 
-		      buffer_xml_printf
+		      string_xml_appendf
 			(buffer,
 			 "<item>"
 			 "<column name=\"pid\">%s</column>"
@@ -743,7 +746,9 @@ linux_xfer_osdata_fds (struct buffer *buffer)
       closedir (dirp);
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Returns the socket state STATE in textual form.  */
@@ -808,7 +813,7 @@ union socket_addr
    otherwise only UDP sockets are processed.  */
 
 static void
-print_sockets (unsigned short family, int tcp, struct buffer *buffer)
+print_sockets (unsigned short family, int tcp, std::string &buffer)
 {
   const char *proc_file;
 
@@ -909,26 +914,26 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer)
 
 		  user_from_uid (user, sizeof (user), uid);
 
-		  buffer_xml_printf (
-		      buffer,
-		      "<item>"
-		      "<column name=\"local address\">%s</column>"
-		      "<column name=\"local port\">%s</column>"
-		      "<column name=\"remote address\">%s</column>"
-		      "<column name=\"remote port\">%s</column>"
-		      "<column name=\"state\">%s</column>"
-		      "<column name=\"user\">%s</column>"
-		      "<column name=\"family\">%s</column>"
-		      "<column name=\"protocol\">%s</column>"
-		      "</item>",
-		      local_address,
-		      local_service,
-		      remote_address,
-		      remote_service,
-		      format_socket_state (state),
-		      user,
-		      (family == AF_INET) ? "INET" : "INET6",
-		      tcp ? "STREAM" : "DGRAM");
+		  string_xml_appendf
+		    (buffer,
+		     "<item>"
+		     "<column name=\"local address\">%s</column>"
+		     "<column name=\"local port\">%s</column>"
+		     "<column name=\"remote address\">%s</column>"
+		     "<column name=\"remote port\">%s</column>"
+		     "<column name=\"state\">%s</column>"
+		     "<column name=\"user\">%s</column>"
+		     "<column name=\"family\">%s</column>"
+		     "<column name=\"protocol\">%s</column>"
+		     "</item>",
+		     local_address,
+		     local_service,
+		     remote_address,
+		     remote_service,
+		     format_socket_state (state),
+		     user,
+		     (family == AF_INET) ? "INET" : "INET6",
+		     tcp ? "STREAM" : "DGRAM");
 		}
 	    }
 	}
@@ -938,17 +943,19 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer)
 
 /* Collect data about internet sockets and write it into BUFFER.  */
 
-static void
-linux_xfer_osdata_isockets (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_isockets ()
 {
-  buffer_grow_str (buffer, "<osdata type=\"I sockets\">\n");
+  std::string buffer = "<osdata type=\"I sockets\">\n";
 
   print_sockets (AF_INET, 1, buffer);
   print_sockets (AF_INET, 0, buffer);
   print_sockets (AF_INET6, 1, buffer);
   print_sockets (AF_INET6, 0, buffer);
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Converts the time SECONDS into textual form and copies it into a
@@ -993,10 +1000,10 @@ group_from_gid (char *group, int maxlen, gid_t gid)
 /* Collect data about shared memory recorded in /proc and write it
    into BUFFER.  */
 
-static void
-linux_xfer_osdata_shm (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_shm ()
 {
-  buffer_grow_str (buffer, "<osdata type=\"shared memory\">\n");
+  std::string buffer = "<osdata type=\"shared memory\">\n";
 
   gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r");
   if (fp)
@@ -1043,7 +1050,7 @@ linux_xfer_osdata_shm (struct buffer *buffer)
 		  time_from_time_t (dtime_str, sizeof (dtime_str), dtime);
 		  time_from_time_t (ctime_str, sizeof (ctime_str), ctime);
 
-		  buffer_xml_printf
+		  string_xml_appendf
 		    (buffer,
 		     "<item>"
 		     "<column name=\"key\">%d</column>"
@@ -1081,16 +1088,18 @@ linux_xfer_osdata_shm (struct buffer *buffer)
       while (!feof (fp.get ()));
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Collect data about semaphores recorded in /proc and write it
    into BUFFER.  */
 
-static void
-linux_xfer_osdata_sem (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_sem ()
 {
-  buffer_grow_str (buffer, "<osdata type=\"semaphores\">\n");
+  std::string buffer = "<osdata type=\"semaphores\">\n";
 
   gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r");
   if (fp)
@@ -1129,7 +1138,7 @@ linux_xfer_osdata_sem (struct buffer *buffer)
 		  time_from_time_t (otime_str, sizeof (otime_str), otime);
 		  time_from_time_t (ctime_str, sizeof (ctime_str), ctime);
 
-		  buffer_xml_printf
+		  string_xml_appendf
 		    (buffer,
 		     "<item>"
 		     "<column name=\"key\">%d</column>"
@@ -1159,16 +1168,18 @@ linux_xfer_osdata_sem (struct buffer *buffer)
       while (!feof (fp.get ()));
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Collect data about message queues recorded in /proc and write it
    into BUFFER.  */
 
-static void
-linux_xfer_osdata_msg (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_msg ()
 {
-  buffer_grow_str (buffer, "<osdata type=\"message queues\">\n");
+  std::string buffer = "<osdata type=\"message queues\">\n";
 
   gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r");
   if (fp)
@@ -1213,7 +1224,7 @@ linux_xfer_osdata_msg (struct buffer *buffer)
 		  time_from_time_t (rtime_str, sizeof (rtime_str), rtime);
 		  time_from_time_t (ctime_str, sizeof (ctime_str), ctime);
 
-		  buffer_xml_printf
+		  string_xml_appendf
 		    (buffer,
 		     "<item>"
 		     "<column name=\"key\">%d</column>"
@@ -1251,16 +1262,18 @@ linux_xfer_osdata_msg (struct buffer *buffer)
       while (!feof (fp.get ()));
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 /* Collect data about loaded kernel modules and write it into
    BUFFER.  */
 
-static void
-linux_xfer_osdata_modules (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_modules ()
 {
-  buffer_grow_str (buffer, "<osdata type=\"modules\">\n");
+  std::string buffer = "<osdata type=\"modules\">\n";
 
   gdb_file_up fp = gdb_fopen_cloexec ("/proc/modules", "r");
   if (fp)
@@ -1306,84 +1319,87 @@ linux_xfer_osdata_modules (struct buffer *buffer)
 	      if (sscanf (tmp, "%llx", &address) != 1)
 		continue;
 
-	      buffer_xml_printf (buffer,
-				 "<item>"
-				 "<column name=\"name\">%s</column>"
-				 "<column name=\"size\">%u</column>"
-				 "<column name=\"num uses\">%d</column>"
-				 "<column name=\"dependencies\">%s</column>"
-				 "<column name=\"status\">%s</column>"
-				 "<column name=\"address\">%llx</column>"
-				 "</item>",
-				 name,
-				 size,
-				 uses,
-				 dependencies,
-				 status,
-				 address);
+	      string_xml_appendf (buffer,
+				  "<item>"
+				  "<column name=\"name\">%s</column>"
+				  "<column name=\"size\">%u</column>"
+				  "<column name=\"num uses\">%d</column>"
+				  "<column name=\"dependencies\">%s</column>"
+				  "<column name=\"status\">%s</column>"
+				  "<column name=\"address\">%llx</column>"
+				  "</item>",
+				  name,
+				  size,
+				  uses,
+				  dependencies,
+				  status,
+				  address);
 	    }
 	}
       while (!feof (fp.get ()));
     }
 
-  buffer_grow_str0 (buffer, "</osdata>\n");
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
-static void linux_xfer_osdata_info_os_types (struct buffer *buffer);
+static std::string linux_xfer_osdata_info_os_types ();
 
 static struct osdata_type {
   const char *type;
   const char *title;
   const char *description;
-  void (*take_snapshot) (struct buffer *buffer);
-  LONGEST len_avail;
-  struct buffer buffer;
+  std::string (*take_snapshot) ();
+  std::string buffer;
 } osdata_table[] = {
   { "types", "Types", "Listing of info os types you can list",
-    linux_xfer_osdata_info_os_types, -1 },
+    linux_xfer_osdata_info_os_types },
   { "cpus", "CPUs", "Listing of all cpus/cores on the system",
-    linux_xfer_osdata_cpus, -1 },
+    linux_xfer_osdata_cpus },
   { "files", "File descriptors", "Listing of all file descriptors",
-    linux_xfer_osdata_fds, -1 },
+    linux_xfer_osdata_fds },
   { "modules", "Kernel modules", "Listing of all loaded kernel modules",
-    linux_xfer_osdata_modules, -1 },
+    linux_xfer_osdata_modules },
   { "msg", "Message queues", "Listing of all message queues",
-    linux_xfer_osdata_msg, -1 },
+    linux_xfer_osdata_msg },
   { "processes", "Processes", "Listing of all processes",
-    linux_xfer_osdata_processes, -1 },
+    linux_xfer_osdata_processes },
   { "procgroups", "Process groups", "Listing of all process groups",
-    linux_xfer_osdata_processgroups, -1 },
+    linux_xfer_osdata_processgroups },
   { "semaphores", "Semaphores", "Listing of all semaphores",
-    linux_xfer_osdata_sem, -1 },
+    linux_xfer_osdata_sem },
   { "shm", "Shared-memory regions", "Listing of all shared-memory regions",
-    linux_xfer_osdata_shm, -1 },
+    linux_xfer_osdata_shm },
   { "sockets", "Sockets", "Listing of all internet-domain sockets",
-    linux_xfer_osdata_isockets, -1 },
+    linux_xfer_osdata_isockets },
   { "threads", "Threads", "Listing of all threads",
-  linux_xfer_osdata_threads, -1 },
+    linux_xfer_osdata_threads },
   { NULL, NULL, NULL }
 };
 
 /* Collect data about all types info os can show in BUFFER.  */
 
-static void
-linux_xfer_osdata_info_os_types (struct buffer *buffer)
+static std::string
+linux_xfer_osdata_info_os_types ()
 {
-  buffer_grow_str (buffer, "<osdata type=\"types\">\n");
+  std::string buffer = "<osdata type=\"types\">\n";
 
   /* Start the below loop at 1, as we do not want to list ourselves.  */
   for (int i = 1; osdata_table[i].type; ++i)
-    buffer_xml_printf (buffer,
-		       "<item>"
-		       "<column name=\"Type\">%s</column>"
-		       "<column name=\"Description\">%s</column>"
-		       "<column name=\"Title\">%s</column>"
-		       "</item>",
-		       osdata_table[i].type,
-		       osdata_table[i].description,
-		       osdata_table[i].title);
-
-  buffer_grow_str0 (buffer, "</osdata>\n");
+    string_xml_appendf (buffer,
+			"<item>"
+			"<column name=\"Type\">%s</column>"
+			"<column name=\"Description\">%s</column>"
+			"<column name=\"Title\">%s</column>"
+			"</item>",
+			osdata_table[i].type,
+			osdata_table[i].description,
+			osdata_table[i].title);
+
+  buffer += "</osdata>\n";
+
+  return buffer;
 }
 
 
@@ -1397,24 +1413,17 @@ common_getter (struct osdata_type *osd,
   gdb_assert (readbuf);
 
   if (offset == 0)
-    {
-      if (osd->len_avail != -1 && osd->len_avail != 0)
-	buffer_free (&osd->buffer);
-      osd->len_avail = 0;
-      buffer_init (&osd->buffer);
-      (osd->take_snapshot) (&osd->buffer);
-      osd->len_avail = strlen (osd->buffer.buffer);
-    }
-  if (offset >= osd->len_avail)
+    osd->buffer = osd->take_snapshot ();
+
+  if (offset >= osd->buffer.size ())
     {
       /* Done.  Get rid of the buffer.  */
-      buffer_free (&osd->buffer);
-      osd->len_avail = 0;
+      osd->buffer.clear ();
       return 0;
     }
-  if (len > osd->len_avail - offset)
-    len = osd->len_avail - offset;
-  memcpy (readbuf, osd->buffer.buffer + offset, len);
+
+  len = std::min (len, osd->buffer.size () - offset);
+  memcpy (readbuf, &osd->buffer[offset], len);
 
   return len;
 
-- 
2.38.1


  parent reply	other threads:[~2022-12-16  6:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-16  6:01 [PATCH 0/2] Remove one use of struct buffer Simon Marchi
2022-12-16  6:01 ` [PATCH 1/2] gdbsupport: add string_xml_appendf Simon Marchi
2022-12-16  6:01 ` Simon Marchi [this message]
2022-12-16 13:09 ` [PATCH 0/2] Remove one use of struct buffer Pedro Alves
2022-12-16 13:32   ` Simon Marchi
2022-12-16 15:04 ` Tom Tromey

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=20221216060148.1261366-3-simon.marchi@polymtl.ca \
    --to=simon.marchi@polymtl.ca \
    --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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).