Use scoped_fd in more places

This changes a few more places to use scoped_fd.  This allows the
removal of some cleanups.

Regression tested by the buildbot, though note that I'm not sure
whether the buildbot actually builds anything using all of these
files.

gdb/ChangeLog
2018-03-08  Tom Tromey  <tom@tromey.com>

	* source.c (get_filename_and_charpos): Use scoped_fd.
	* nto-procfs.c (procfs_open_1): Use scoped_fd.
	(procfs_pidlist): Likewise.
	* procfs.c (proc_get_LDT_entry): Use scoped_fd.
	(iterate_over_mappings): Likewise.
This commit is contained in:
Tom Tromey 2018-02-22 15:21:42 -07:00
parent fdf07f3aeb
commit 5dc1a7047a
4 changed files with 37 additions and 46 deletions

View file

@ -1,3 +1,11 @@
2018-03-08 Tom Tromey <tom@tromey.com>
* source.c (get_filename_and_charpos): Use scoped_fd.
* nto-procfs.c (procfs_open_1): Use scoped_fd.
(procfs_pidlist): Likewise.
* procfs.c (proc_get_LDT_entry): Use scoped_fd.
(iterate_over_mappings): Likewise.
2018-03-08 Tom Tromey <tom@tromey.com> 2018-03-08 Tom Tromey <tom@tromey.com>
* infcall.c (struct call_return_meta_info) * infcall.c (struct call_return_meta_info)

View file

@ -43,6 +43,7 @@
#include "solib.h" #include "solib.h"
#include "inf-child.h" #include "inf-child.h"
#include "common/filestuff.h" #include "common/filestuff.h"
#include "common/scoped_fd.h"
#define NULL_PID 0 #define NULL_PID 0
#define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\ #define _DEBUG_FLAG_TRACE (_DEBUG_FLAG_TRACE_EXEC|_DEBUG_FLAG_TRACE_RD|\
@ -113,9 +114,8 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
{ {
char *endstr; char *endstr;
char buffer[50]; char buffer[50];
int fd, total_size; int total_size;
procfs_sysinfo *sysinfo; procfs_sysinfo *sysinfo;
struct cleanup *cleanups;
char nto_procfs_path[PATH_MAX]; char nto_procfs_path[PATH_MAX];
/* Offer to kill previous inferiors before opening this target. */ /* Offer to kill previous inferiors before opening this target. */
@ -158,17 +158,16 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s", snprintf (nto_procfs_path, PATH_MAX - 1, "%s%s",
(nodestr != NULL) ? nodestr : "", "/proc"); (nodestr != NULL) ? nodestr : "", "/proc");
fd = open (nto_procfs_path, O_RDONLY); scoped_fd fd (open (nto_procfs_path, O_RDONLY));
if (fd == -1) if (fd.get () == -1)
{ {
printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno, printf_filtered ("Error opening %s : %d (%s)\n", nto_procfs_path, errno,
safe_strerror (errno)); safe_strerror (errno));
error (_("Invalid procfs arg")); error (_("Invalid procfs arg"));
} }
cleanups = make_cleanup_close (fd);
sysinfo = (void *) buffer; sysinfo = (void *) buffer;
if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK) if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, sizeof buffer, 0) != EOK)
{ {
printf_filtered ("Error getting size: %d (%s)\n", errno, printf_filtered ("Error getting size: %d (%s)\n", errno,
safe_strerror (errno)); safe_strerror (errno));
@ -186,7 +185,8 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
} }
else else
{ {
if (devctl (fd, DCMD_PROC_SYSINFO, sysinfo, total_size, 0) != EOK) if (devctl (fd.get (), DCMD_PROC_SYSINFO, sysinfo, total_size, 0)
!= EOK)
{ {
printf_filtered ("Error getting sysinfo: %d (%s)\n", errno, printf_filtered ("Error getting sysinfo: %d (%s)\n", errno,
safe_strerror (errno)); safe_strerror (errno));
@ -201,7 +201,6 @@ procfs_open_1 (struct target_ops *ops, const char *arg, int from_tty)
} }
} }
} }
do_cleanups (cleanups);
inf_child_open_target (ops, arg, from_tty); inf_child_open_target (ops, arg, from_tty);
printf_filtered ("Debugging using %s\n", nto_procfs_path); printf_filtered ("Debugging using %s\n", nto_procfs_path);
@ -377,9 +376,6 @@ procfs_pidlist (const char *args, int from_tty)
do do
{ {
int fd;
struct cleanup *inner_cleanup;
/* Get the right pid and procfs path for the pid. */ /* Get the right pid and procfs path for the pid. */
do do
{ {
@ -397,17 +393,16 @@ procfs_pidlist (const char *args, int from_tty)
while (pid == 0); while (pid == 0);
/* Open the procfs path. */ /* Open the procfs path. */
fd = open (buf, O_RDONLY); scoped_fd fd (open (buf, O_RDONLY));
if (fd == -1) if (fd.get () == -1)
{ {
fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n", fprintf_unfiltered (gdb_stderr, "failed to open %s - %d (%s)\n",
buf, errno, safe_strerror (errno)); buf, errno, safe_strerror (errno));
continue; continue;
} }
inner_cleanup = make_cleanup_close (fd);
pidinfo = (procfs_info *) buf; pidinfo = (procfs_info *) buf;
if (devctl (fd, DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK) if (devctl (fd.get (), DCMD_PROC_INFO, pidinfo, sizeof (buf), 0) != EOK)
{ {
fprintf_unfiltered (gdb_stderr, fprintf_unfiltered (gdb_stderr,
"devctl DCMD_PROC_INFO failed - %d (%s)\n", "devctl DCMD_PROC_INFO failed - %d (%s)\n",
@ -417,7 +412,8 @@ procfs_pidlist (const char *args, int from_tty)
num_threads = pidinfo->num_threads; num_threads = pidinfo->num_threads;
info = (procfs_debuginfo *) buf; info = (procfs_debuginfo *) buf;
if (devctl (fd, DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0) != EOK) if (devctl (fd.get (), DCMD_PROC_MAPDEBUG_BASE, info, sizeof (buf), 0)
!= EOK)
strcpy (name, "unavailable"); strcpy (name, "unavailable");
else else
strcpy (name, info->path); strcpy (name, info->path);
@ -427,7 +423,7 @@ procfs_pidlist (const char *args, int from_tty)
for (status->tid = 1; status->tid <= num_threads; status->tid++) for (status->tid = 1; status->tid <= num_threads; status->tid++)
{ {
const int err const int err
= devctl (fd, DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0); = devctl (fd.get (), DCMD_PROC_TIDSTATUS, status, sizeof (buf), 0);
printf_filtered ("%s - %d", name, pid); printf_filtered ("%s - %d", name, pid);
if (err == EOK && status->tid != 0) if (err == EOK && status->tid != 0)
printf_filtered ("/%d\n", status->tid); printf_filtered ("/%d\n", status->tid);
@ -437,8 +433,6 @@ procfs_pidlist (const char *args, int from_tty)
break; break;
} }
} }
do_cleanups (inner_cleanup);
} }
while (dirp != NULL); while (dirp != NULL);

View file

@ -46,6 +46,7 @@
#include "auxv.h" #include "auxv.h"
#include "procfs.h" #include "procfs.h"
#include "observer.h" #include "observer.h"
#include "common/scoped_fd.h"
/* This module provides the interface between GDB and the /* This module provides the interface between GDB and the
/proc file system, which is used on many versions of Unix /proc file system, which is used on many versions of Unix
@ -1593,8 +1594,6 @@ proc_get_LDT_entry (procinfo *pi, int key)
{ {
static struct ssd *ldt_entry = NULL; static struct ssd *ldt_entry = NULL;
char pathname[MAX_PROC_NAME_SIZE]; char pathname[MAX_PROC_NAME_SIZE];
struct cleanup *old_chain = NULL;
int fd;
/* Allocate space for one LDT entry. /* Allocate space for one LDT entry.
This alloc must persist, because we return a pointer to it. */ This alloc must persist, because we return a pointer to it. */
@ -1603,16 +1602,16 @@ proc_get_LDT_entry (procinfo *pi, int key)
/* Open the file descriptor for the LDT table. */ /* Open the file descriptor for the LDT table. */
sprintf (pathname, "/proc/%d/ldt", pi->pid); sprintf (pathname, "/proc/%d/ldt", pi->pid);
if ((fd = open_with_retry (pathname, O_RDONLY)) < 0) scoped_fd fd (open_with_retry (pathname, O_RDONLY));
if (fd.get () < 0)
{ {
proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__); proc_warn (pi, "proc_get_LDT_entry (open)", __LINE__);
return NULL; return NULL;
} }
/* Make sure it gets closed again! */
old_chain = make_cleanup_close (fd);
/* Now 'read' thru the table, find a match and return it. */ /* Now 'read' thru the table, find a match and return it. */
while (read (fd, ldt_entry, sizeof (struct ssd)) == sizeof (struct ssd)) while (read (fd.get (), ldt_entry, sizeof (struct ssd))
== sizeof (struct ssd))
{ {
if (ldt_entry->sel == 0 && if (ldt_entry->sel == 0 &&
ldt_entry->bo == 0 && ldt_entry->bo == 0 &&
@ -1627,7 +1626,6 @@ proc_get_LDT_entry (procinfo *pi, int key)
} }
} }
/* Loop ended, match not found. */ /* Loop ended, match not found. */
do_cleanups (old_chain);
return NULL; return NULL;
} }
@ -3426,40 +3424,33 @@ iterate_over_mappings (procinfo *pi, find_memory_region_ftype child_func,
struct prmap *prmaps; struct prmap *prmaps;
struct prmap *prmap; struct prmap *prmap;
int funcstat; int funcstat;
int map_fd;
int nmap; int nmap;
struct cleanup *cleanups = make_cleanup (null_cleanup, NULL);
struct stat sbuf; struct stat sbuf;
/* Get the number of mappings, allocate space, /* Get the number of mappings, allocate space,
and read the mappings into prmaps. */ and read the mappings into prmaps. */
/* Open map fd. */ /* Open map fd. */
sprintf (pathname, "/proc/%d/map", pi->pid); sprintf (pathname, "/proc/%d/map", pi->pid);
if ((map_fd = open (pathname, O_RDONLY)) < 0)
proc_error (pi, "iterate_over_mappings (open)", __LINE__);
/* Make sure it gets closed again. */ scoped_fd map_fd (open (pathname, O_RDONLY));
make_cleanup_close (map_fd); if (map_fd.get () < 0)
proc_error (pi, "iterate_over_mappings (open)", __LINE__);
/* Use stat to determine the file size, and compute /* Use stat to determine the file size, and compute
the number of prmap_t objects it contains. */ the number of prmap_t objects it contains. */
if (fstat (map_fd, &sbuf) != 0) if (fstat (map_fd.get (), &sbuf) != 0)
proc_error (pi, "iterate_over_mappings (fstat)", __LINE__); proc_error (pi, "iterate_over_mappings (fstat)", __LINE__);
nmap = sbuf.st_size / sizeof (prmap_t); nmap = sbuf.st_size / sizeof (prmap_t);
prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps)); prmaps = (struct prmap *) alloca ((nmap + 1) * sizeof (*prmaps));
if (read (map_fd, (char *) prmaps, nmap * sizeof (*prmaps)) if (read (map_fd.get (), (char *) prmaps, nmap * sizeof (*prmaps))
!= (nmap * sizeof (*prmaps))) != (nmap * sizeof (*prmaps)))
proc_error (pi, "iterate_over_mappings (read)", __LINE__); proc_error (pi, "iterate_over_mappings (read)", __LINE__);
for (prmap = prmaps; nmap > 0; prmap++, nmap--) for (prmap = prmaps; nmap > 0; prmap++, nmap--)
if ((funcstat = (*func) (prmap, child_func, data)) != 0) if ((funcstat = (*func) (prmap, child_func, data)) != 0)
{ return funcstat;
do_cleanups (cleanups);
return funcstat;
}
do_cleanups (cleanups);
return 0; return 0;
} }

View file

@ -42,6 +42,7 @@
#include "ui-out.h" #include "ui-out.h"
#include "readline/readline.h" #include "readline/readline.h"
#include "common/enum-flags.h" #include "common/enum-flags.h"
#include "common/scoped_fd.h"
#include <algorithm> #include <algorithm>
#include "common/pathstuff.h" #include "common/pathstuff.h"
@ -1215,24 +1216,21 @@ find_source_lines (struct symtab *s, int desc)
static int static int
get_filename_and_charpos (struct symtab *s, char **fullname) get_filename_and_charpos (struct symtab *s, char **fullname)
{ {
int desc, linenums_changed = 0; int linenums_changed = 0;
struct cleanup *cleanups;
desc = open_source_file (s); scoped_fd desc (open_source_file (s));
if (desc < 0) if (desc.get () < 0)
{ {
if (fullname) if (fullname)
*fullname = NULL; *fullname = NULL;
return 0; return 0;
} }
cleanups = make_cleanup_close (desc);
if (fullname) if (fullname)
*fullname = s->fullname; *fullname = s->fullname;
if (s->line_charpos == 0) if (s->line_charpos == 0)
linenums_changed = 1; linenums_changed = 1;
if (linenums_changed) if (linenums_changed)
find_source_lines (s, desc); find_source_lines (s, desc.get ());
do_cleanups (cleanups);
return linenums_changed; return linenums_changed;
} }