diff options
author | Simon Marchi <simon.marchi@polymtl.ca> | 2022-12-15 22:01:19 -0500 |
---|---|---|
committer | Simon Marchi <simon.marchi@polymtl.ca> | 2022-12-16 08:33:12 -0500 |
commit | e60a615dde5d6674a6488b74afe807a775551407 (patch) | |
tree | b524aa3b2597a15f08b27110d483a66a2bd2a2d4 | |
parent | 65cded61a2219719df46066880ccc80c45112130 (diff) | |
download | gdb-e60a615dde5d6674a6488b74afe807a775551407.zip gdb-e60a615dde5d6674a6488b74afe807a775551407.tar.gz gdb-e60a615dde5d6674a6488b74afe807a775551407.tar.bz2 |
gdb: convert linux-osdata.c from buffer to std::string
Replace the use of struct buffer in linux-osdata.c with std::string.
There is no change in the logic, so there should be no user-visible
change.
Change-Id: I27f53165d401650bbd0bebe8ed88221e25545b3f
Approved-By: Pedro Alves <pedro@palves.net>
-rw-r--r-- | gdb/nat/linux-osdata.c | 287 |
1 files changed, 148 insertions, 139 deletions
diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index 8639f09..73a0525 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; |