From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp.polymtl.ca (smtp.polymtl.ca [132.207.4.11]) by sourceware.org (Postfix) with ESMTPS id A72A838369C9 for ; Fri, 16 Dec 2022 06:01:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A72A838369C9 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=polymtl.ca Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=polymtl.ca Received: from simark.ca (simark.ca [158.69.221.121]) (authenticated bits=0) by smtp.polymtl.ca (8.14.7/8.14.7) with ESMTP id 2BG61njd020737 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 16 Dec 2022 01:01:54 -0500 DKIM-Filter: OpenDKIM Filter v2.11.0 smtp.polymtl.ca 2BG61njd020737 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=polymtl.ca; s=default; t=1671170514; bh=YXU5CoU1hOrJNYYRyQ4mXBkRTb4S9W8q0WjB9s+iwhQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uGBufLK5B8CzDA48wLr0hN3KLojI4j7jBoUfbLek2j97kY76y9eBIuaCAWH0nmCCO p+U97GCrYKwYj2W5o7kLnHR/z96jkm2wcyFrL/VznA3ijTF9hYXdOR8tCS9HOTzBPZ 4qn9epQCw2/YK5Tn6cL3kiK4PXoKjWUQyU46A1HA= Received: from simark.localdomain (unknown [217.28.27.60]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by simark.ca (Postfix) with ESMTPSA id BEE651E11F; Fri, 16 Dec 2022 01:01:49 -0500 (EST) From: Simon Marchi To: gdb-patches@sourceware.org Cc: Simon Marchi Subject: [PATCH 2/2] gdb: convert linux-osdata.c from buffer to std::string Date: Fri, 16 Dec 2022 01:01:48 -0500 Message-Id: <20221216060148.1261366-3-simon.marchi@polymtl.ca> X-Mailer: git-send-email 2.38.1 In-Reply-To: <20221216060148.1261366-1-simon.marchi@polymtl.ca> References: <20221216060148.1261366-1-simon.marchi@polymtl.ca> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Poly-FromMTA: (simark.ca [158.69.221.121]) at Fri, 16 Dec 2022 06:01:50 +0000 X-Spam-Status: No, score=-3189.5 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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, "\n"); + std::string buffer = "\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, "" "%lld" @@ -404,7 +403,9 @@ linux_xfer_osdata_processes (struct buffer *buffer) closedir (dirp); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, "" "%lld" @@ -510,18 +510,19 @@ linux_xfer_osdata_processgroups (struct buffer *buffer) } } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, "" "%lld" @@ -592,17 +593,18 @@ linux_xfer_osdata_threads (struct buffer *buffer) closedir (dirp); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, ""); + buffer += ""; else - buffer_grow_str (buffer, ""); + buffer += ""; first_item = 0; } - buffer_xml_printf (buffer, - "%s", - key, - value); + string_xml_appendf (buffer, + "%s", + key, + value); } } while (!feof (fp.get ())); if (first_item == 0) - buffer_grow_str (buffer, ""); + buffer += ""; } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, "" "%s" @@ -743,7 +746,9 @@ linux_xfer_osdata_fds (struct buffer *buffer) closedir (dirp); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, - "" - "%s" - "%s" - "%s" - "%s" - "%s" - "%s" - "%s" - "%s" - "", - 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, + "" + "%s" + "%s" + "%s" + "%s" + "%s" + "%s" + "%s" + "%s" + "", + 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, "\n"); + std::string buffer = "\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, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, "" "%d" @@ -1081,16 +1088,18 @@ linux_xfer_osdata_shm (struct buffer *buffer) while (!feof (fp.get ())); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, "" "%d" @@ -1159,16 +1168,18 @@ linux_xfer_osdata_sem (struct buffer *buffer) while (!feof (fp.get ())); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, "" "%d" @@ -1251,16 +1262,18 @@ linux_xfer_osdata_msg (struct buffer *buffer) while (!feof (fp.get ())); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, - "" - "%s" - "%u" - "%d" - "%s" - "%s" - "%llx" - "", - name, - size, - uses, - dependencies, - status, - address); + string_xml_appendf (buffer, + "" + "%s" + "%u" + "%d" + "%s" + "%s" + "%llx" + "", + name, + size, + uses, + dependencies, + status, + address); } } while (!feof (fp.get ())); } - buffer_grow_str0 (buffer, "\n"); + buffer += "\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, "\n"); + std::string buffer = "\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, - "" - "%s" - "%s" - "%s" - "", - osdata_table[i].type, - osdata_table[i].description, - osdata_table[i].title); - - buffer_grow_str0 (buffer, "\n"); + string_xml_appendf (buffer, + "" + "%s" + "%s" + "%s" + "", + osdata_table[i].type, + osdata_table[i].description, + osdata_table[i].title); + + buffer += "\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