diff options
author | Tom Tromey <tom@tromey.com> | 2017-04-26 21:39:46 -0600 |
---|---|---|
committer | Tom Tromey <tom@tromey.com> | 2017-08-03 07:58:52 -0600 |
commit | d419f42dd3f3635fc036413258ed530676998191 (patch) | |
tree | 63b46767bc2a7a0211eb9923b6e07a9fc95c7594 /gdb/nat | |
parent | 4a2b031d5452226cf7894f313b3aac603f7ec5fb (diff) | |
download | binutils-gdb-d419f42dd3f3635fc036413258ed530676998191.tar.gz |
Introduce and use gdb_file_up
This introduces gdb_file_up, a unique pointer holding a FILE*, and
then changes some code in gdb to use it. In particular
gdb_fopen_cloexec now returns a gdb_file_up. This allow removing some
cleanups.
ChangeLog
2017-08-03 Tom Tromey <tom@tromey.com>
* xml-support.c (xml_fetch_content_from_file): Update.
* ui-file.c (stdio_file::open): Update.
* tracefile-tfile.c (tfile_start): Update.
* remote.c (remote_file_put, remote_file_get): Update.
* nat/linux-procfs.c (linux_proc_get_int)
(linux_proc_pid_get_state, linux_proc_tid_get_name): Update.
* nat/linux-osdata.c (linux_common_core_of_thread): Update.
(command_from_pid, commandline_from_pid, linux_xfer_osdata_cpus)
(print_sockets, linux_xfer_osdata_shm, linux_xfer_osdata_sem)
(linux_xfer_osdata_msg, linux_xfer_osdata_modules): Update.
* nat/linux-btrace.c (linux_determine_kernel_start): Update.
* linux-nat.c (linux_proc_pending_signals): Update.
* dwarf2read.c (write_psymtabs_to_index): Use gdb_file_up.
(file_closer): Remove.
* compile/compile.c (compile_to_object): Update.
* common/filestuff.h (struct gdb_file_deleter): New.
(gdb_file_up): New typedef.
(gdb_fopen_cloexec): Change return type.
* common/filestuff.c (gdb_fopen_cloexec): Return gdb_file_up.
* cli/cli-dump.c (fopen_with_cleanup): Remove.
(dump_binary_file, restore_binary_file): Update.
* auto-load.c (auto_load_objfile_script_1): Update.
Diffstat (limited to 'gdb/nat')
-rw-r--r-- | gdb/nat/linux-btrace.c | 9 | ||||
-rw-r--r-- | gdb/nat/linux-osdata.c | 78 | ||||
-rw-r--r-- | gdb/nat/linux-procfs.c | 18 |
3 files changed, 34 insertions, 71 deletions
diff --git a/gdb/nat/linux-btrace.c b/gdb/nat/linux-btrace.c index b2c84c17081..1a383b740c7 100644 --- a/gdb/nat/linux-btrace.c +++ b/gdb/nat/linux-btrace.c @@ -204,24 +204,23 @@ linux_determine_kernel_start (void) { static uint64_t kernel_start; static int cached; - FILE *file; if (cached != 0) return kernel_start; cached = 1; - file = gdb_fopen_cloexec ("/proc/kallsyms", "r"); + gdb_file_up file = gdb_fopen_cloexec ("/proc/kallsyms", "r"); if (file == NULL) return kernel_start; - while (!feof (file)) + while (!feof (file.get ())) { char buffer[1024], symbol[8], *line; uint64_t addr; int match; - line = fgets (buffer, sizeof (buffer), file); + line = fgets (buffer, sizeof (buffer), file.get ()); if (line == NULL) break; @@ -236,8 +235,6 @@ linux_determine_kernel_start (void) } } - fclose (file); - return kernel_start; } diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c index ba2e5a5d8fe..4b40a4db0e0 100644 --- a/gdb/nat/linux-osdata.c +++ b/gdb/nat/linux-osdata.c @@ -61,7 +61,6 @@ int linux_common_core_of_thread (ptid_t ptid) { char filename[sizeof ("/proc//task//stat") + 2 * MAX_PID_T_STRLEN]; - FILE *f; char *content = NULL; char *p; char *ts = 0; @@ -71,7 +70,7 @@ linux_common_core_of_thread (ptid_t ptid) sprintf (filename, "/proc/%lld/task/%lld/stat", (PID_T) ptid_get_pid (ptid), (PID_T) ptid_get_lwp (ptid)); - f = gdb_fopen_cloexec (filename, "r"); + gdb_file_up f = gdb_fopen_cloexec (filename, "r"); if (!f) return -1; @@ -79,7 +78,7 @@ linux_common_core_of_thread (ptid_t ptid) { int n; content = (char *) xrealloc (content, content_read + 1024); - n = fread (content + content_read, 1, 1024, f); + n = fread (content + content_read, 1, 1024, f.get ()); content_read += n; if (n < 1024) { @@ -104,7 +103,6 @@ linux_common_core_of_thread (ptid_t ptid) core = -1; xfree (content); - fclose (f); return core; } @@ -117,7 +115,7 @@ static void command_from_pid (char *command, int maxlen, PID_T pid) { char *stat_path = xstrprintf ("/proc/%lld/stat", pid); - FILE *fp = gdb_fopen_cloexec (stat_path, "r"); + gdb_file_up fp = gdb_fopen_cloexec (stat_path, "r"); command[0] = '\0'; @@ -128,15 +126,13 @@ command_from_pid (char *command, int maxlen, PID_T pid) (for the brackets). */ char cmd[18]; PID_T stat_pid; - int items_read = fscanf (fp, "%lld %17s", &stat_pid, cmd); + int items_read = fscanf (fp.get (), "%lld %17s", &stat_pid, cmd); if (items_read == 2 && pid == stat_pid) { cmd[strlen (cmd) - 1] = '\0'; /* Remove trailing parenthesis. */ strncpy (command, cmd + 1, maxlen); /* Ignore leading parenthesis. */ } - - fclose (fp); } else { @@ -157,16 +153,16 @@ commandline_from_pid (PID_T pid) { char *pathname = xstrprintf ("/proc/%lld/cmdline", pid); char *commandline = NULL; - FILE *f = gdb_fopen_cloexec (pathname, "r"); + gdb_file_up f = gdb_fopen_cloexec (pathname, "r"); if (f) { size_t len = 0; - while (!feof (f)) + while (!feof (f.get ())) { char buf[1024]; - size_t read_bytes = fread (buf, 1, sizeof (buf), f); + size_t read_bytes = fread (buf, 1, sizeof (buf), f.get ()); if (read_bytes) { @@ -176,8 +172,6 @@ commandline_from_pid (PID_T pid) } } - fclose (f); - if (commandline) { size_t i; @@ -675,7 +669,6 @@ linux_xfer_osdata_cpus (gdb_byte *readbuf, if (offset == 0) { - FILE *fp; int first_item = 1; if (len_avail != -1 && len_avail != 0) @@ -685,14 +678,14 @@ linux_xfer_osdata_cpus (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"cpus\">\n"); - fp = gdb_fopen_cloexec ("/proc/cpuinfo", "r"); + gdb_file_up fp = gdb_fopen_cloexec ("/proc/cpuinfo", "r"); if (fp != NULL) { char buf[8192]; do { - if (fgets (buf, sizeof (buf), fp)) + if (fgets (buf, sizeof (buf), fp.get ())) { char *key, *value; int i = 0; @@ -732,12 +725,10 @@ linux_xfer_osdata_cpus (gdb_byte *readbuf, value); } } - while (!feof (fp)); + while (!feof (fp.get ())); if (first_item == 0) buffer_grow_str (&buffer, "</item>"); - - fclose (fp); } buffer_grow_str0 (&buffer, "</osdata>\n"); @@ -942,7 +933,6 @@ static void print_sockets (unsigned short family, int tcp, struct buffer *buffer) { const char *proc_file; - FILE *fp; if (family == AF_INET) proc_file = tcp ? "/proc/net/tcp" : "/proc/net/udp"; @@ -951,14 +941,14 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer) else return; - fp = gdb_fopen_cloexec (proc_file, "r"); + gdb_file_up fp = gdb_fopen_cloexec (proc_file, "r"); if (fp) { char buf[8192]; do { - if (fgets (buf, sizeof (buf), fp)) + if (fgets (buf, sizeof (buf), fp.get ())) { uid_t uid; unsigned int local_port, remote_port, state; @@ -1064,9 +1054,7 @@ print_sockets (unsigned short family, int tcp, struct buffer *buffer) } } } - while (!feof (fp)); - - fclose (fp); + while (!feof (fp.get ())); } } @@ -1163,8 +1151,6 @@ linux_xfer_osdata_shm (gdb_byte *readbuf, if (offset == 0) { - FILE *fp; - if (len_avail != -1 && len_avail != 0) buffer_free (&buffer); len_avail = 0; @@ -1172,14 +1158,14 @@ linux_xfer_osdata_shm (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"shared memory\">\n"); - fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r"); + gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/shm", "r"); if (fp) { char buf[8192]; do { - if (fgets (buf, sizeof (buf), fp)) + if (fgets (buf, sizeof (buf), fp.get ())) { key_t key; uid_t uid, cuid; @@ -1252,9 +1238,7 @@ linux_xfer_osdata_shm (gdb_byte *readbuf, } } } - while (!feof (fp)); - - fclose (fp); + while (!feof (fp.get ())); } buffer_grow_str0 (&buffer, "</osdata>\n"); @@ -1291,8 +1275,6 @@ linux_xfer_osdata_sem (gdb_byte *readbuf, if (offset == 0) { - FILE *fp; - if (len_avail != -1 && len_avail != 0) buffer_free (&buffer); len_avail = 0; @@ -1300,14 +1282,14 @@ linux_xfer_osdata_sem (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"semaphores\">\n"); - fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r"); + gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/sem", "r"); if (fp) { char buf[8192]; do { - if (fgets (buf, sizeof (buf), fp)) + if (fgets (buf, sizeof (buf), fp.get ())) { key_t key; uid_t uid, cuid; @@ -1364,9 +1346,7 @@ linux_xfer_osdata_sem (gdb_byte *readbuf, } } } - while (!feof (fp)); - - fclose (fp); + while (!feof (fp.get ())); } buffer_grow_str0 (&buffer, "</osdata>\n"); @@ -1403,8 +1383,6 @@ linux_xfer_osdata_msg (gdb_byte *readbuf, if (offset == 0) { - FILE *fp; - if (len_avail != -1 && len_avail != 0) buffer_free (&buffer); len_avail = 0; @@ -1412,14 +1390,14 @@ linux_xfer_osdata_msg (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"message queues\">\n"); - fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r"); + gdb_file_up fp = gdb_fopen_cloexec ("/proc/sysvipc/msg", "r"); if (fp) { char buf[8192]; do { - if (fgets (buf, sizeof (buf), fp)) + if (fgets (buf, sizeof (buf), fp.get ())) { key_t key; PID_T lspid, lrpid; @@ -1490,9 +1468,7 @@ linux_xfer_osdata_msg (gdb_byte *readbuf, } } } - while (!feof (fp)); - - fclose (fp); + while (!feof (fp.get ())); } buffer_grow_str0 (&buffer, "</osdata>\n"); @@ -1529,8 +1505,6 @@ linux_xfer_osdata_modules (gdb_byte *readbuf, if (offset == 0) { - FILE *fp; - if (len_avail != -1 && len_avail != 0) buffer_free (&buffer); len_avail = 0; @@ -1538,14 +1512,14 @@ linux_xfer_osdata_modules (gdb_byte *readbuf, buffer_init (&buffer); buffer_grow_str (&buffer, "<osdata type=\"modules\">\n"); - fp = gdb_fopen_cloexec ("/proc/modules", "r"); + gdb_file_up fp = gdb_fopen_cloexec ("/proc/modules", "r"); if (fp) { char buf[8192]; do { - if (fgets (buf, sizeof (buf), fp)) + if (fgets (buf, sizeof (buf), fp.get ())) { char *name, *dependencies, *status, *tmp; unsigned int size; @@ -1600,9 +1574,7 @@ linux_xfer_osdata_modules (gdb_byte *readbuf, address); } } - while (!feof (fp)); - - fclose (fp); + while (!feof (fp.get ())); } buffer_grow_str0 (&buffer, "</osdata>\n"); diff --git a/gdb/nat/linux-procfs.c b/gdb/nat/linux-procfs.c index 5290045cc3f..a12f6228cba 100644 --- a/gdb/nat/linux-procfs.c +++ b/gdb/nat/linux-procfs.c @@ -29,12 +29,11 @@ static int linux_proc_get_int (pid_t lwpid, const char *field, int warn) { size_t field_len = strlen (field); - FILE *status_file; char buf[100]; int retval = -1; snprintf (buf, sizeof (buf), "/proc/%d/status", (int) lwpid); - status_file = gdb_fopen_cloexec (buf, "r"); + gdb_file_up status_file = gdb_fopen_cloexec (buf, "r"); if (status_file == NULL) { if (warn) @@ -42,14 +41,13 @@ linux_proc_get_int (pid_t lwpid, const char *field, int warn) return -1; } - while (fgets (buf, sizeof (buf), status_file)) + while (fgets (buf, sizeof (buf), status_file.get ())) if (strncmp (buf, field, field_len) == 0 && buf[field_len] == ':') { retval = strtol (&buf[field_len + 1], NULL, 10); break; } - fclose (status_file); return retval; } @@ -128,12 +126,11 @@ parse_proc_status_state (const char *state) static int linux_proc_pid_get_state (pid_t pid, int warn, enum proc_state *state) { - FILE *procfile; int have_state; char buffer[100]; xsnprintf (buffer, sizeof (buffer), "/proc/%d/status", (int) pid); - procfile = gdb_fopen_cloexec (buffer, "r"); + gdb_file_up procfile = gdb_fopen_cloexec (buffer, "r"); if (procfile == NULL) { if (warn) @@ -142,14 +139,13 @@ linux_proc_pid_get_state (pid_t pid, int warn, enum proc_state *state) } have_state = 0; - while (fgets (buffer, sizeof (buffer), procfile) != NULL) + while (fgets (buffer, sizeof (buffer), procfile.get ()) != NULL) if (startswith (buffer, "State:")) { have_state = 1; *state = parse_proc_status_state (buffer + sizeof ("State:") - 1); break; } - fclose (procfile); return have_state; } @@ -242,7 +238,6 @@ linux_proc_tid_get_name (ptid_t ptid) static char comm_buf[TASK_COMM_LEN]; char comm_path[100]; - FILE *comm_file; const char *comm_val; pid_t pid = ptid_get_pid (ptid); pid_t tid = ptid_lwp_p (ptid) ? ptid_get_lwp (ptid) : ptid_get_pid (ptid); @@ -250,12 +245,11 @@ linux_proc_tid_get_name (ptid_t ptid) xsnprintf (comm_path, sizeof (comm_path), "/proc/%ld/task/%ld/comm", (long) pid, (long) tid); - comm_file = gdb_fopen_cloexec (comm_path, "r"); + gdb_file_up comm_file = gdb_fopen_cloexec (comm_path, "r"); if (comm_file == NULL) return NULL; - comm_val = fgets (comm_buf, sizeof (comm_buf), comm_file); - fclose (comm_file); + comm_val = fgets (comm_buf, sizeof (comm_buf), comm_file.get ()); if (comm_val != NULL) { |