aboutsummaryrefslogtreecommitdiff
path: root/gdb/nat/linux-osdata.c
diff options
context:
space:
mode:
authorTom Tromey <tom@tromey.com>2017-04-26 21:39:46 -0600
committerTom Tromey <tom@tromey.com>2017-08-03 07:58:52 -0600
commitd419f42dd3f3635fc036413258ed530676998191 (patch)
tree63b46767bc2a7a0211eb9923b6e07a9fc95c7594 /gdb/nat/linux-osdata.c
parent4a2b031d5452226cf7894f313b3aac603f7ec5fb (diff)
downloadgdb-d419f42dd3f3635fc036413258ed530676998191.zip
gdb-d419f42dd3f3635fc036413258ed530676998191.tar.gz
gdb-d419f42dd3f3635fc036413258ed530676998191.tar.bz2
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/linux-osdata.c')
-rw-r--r--gdb/nat/linux-osdata.c78
1 files changed, 25 insertions, 53 deletions
diff --git a/gdb/nat/linux-osdata.c b/gdb/nat/linux-osdata.c
index ba2e5a5..4b40a4d 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");