A commit which is almost all trivial change.

- m_main: if --log-file-qualifier applies, do not add ".pid"
  at the end of the name

- Fix the logic which detected whether the just-devised name
  already existed.  This was broken (by me) because it could not
  distinguish the reasons for failing to open the logfile.

  Doing this required changing the return type of VG_(open)
  from Int to SysRes (to make failure reasons visible) and 
  that's the cause of most of the changes.



git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4228
This commit is contained in:
Julian Seward 2005-07-23 09:18:34 +00:00
parent a74aa1e74d
commit 6789f6542e
9 changed files with 120 additions and 91 deletions

View File

@ -807,6 +807,7 @@ static void fprint_lineCC(Int fd, lineCC* n)
static void fprint_CC_table_and_calc_totals(void)
{
Int fd;
SysRes sres;
Char buf[512];
fileCC *curr_fileCC;
fnCC *curr_fnCC;
@ -815,9 +816,9 @@ static void fprint_CC_table_and_calc_totals(void)
VGP_PUSHCC(VgpCacheResults);
fd = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
VKI_S_IRUSR|VKI_S_IWUSR);
if (fd < 0) {
sres = VG_(open)(cachegrind_out_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
VKI_S_IRUSR|VKI_S_IWUSR);
if (sres.isError) {
// If the file can't be opened for whatever reason (conflict
// between multiple cachegrinded processes?), give up now.
VG_(message)(Vg_UserMsg,
@ -826,6 +827,8 @@ static void fprint_CC_table_and_calc_totals(void)
VG_(message)(Vg_UserMsg,
" ... so simulation results will be missing.");
return;
} else {
fd = sres.val;
}
// "desc:" lines (giving I1/D1/L2 cache configuration). The spaces after

View File

@ -98,21 +98,22 @@ static Int readdec ( const Char* buf, UInt* val )
static void read_procselfmaps ( void )
{
Int n_chunk, fd;
Int n_chunk;
SysRes fd;
/* Read the initial memory mapping from the /proc filesystem. */
fd = VG_(open) ( "/proc/self/maps", VKI_O_RDONLY, 0 );
if (fd < 0) {
if (fd.isError) {
VG_(message)(Vg_UserMsg, "FATAL: can't open /proc/self/maps");
VG_(exit)(1);
}
buf_n_tot = 0;
do {
n_chunk = VG_(read) ( fd, &procmap_buf[buf_n_tot],
n_chunk = VG_(read) ( fd.val, &procmap_buf[buf_n_tot],
M_PROCMAP_BUF - buf_n_tot );
buf_n_tot += n_chunk;
} while ( n_chunk > 0 && buf_n_tot < M_PROCMAP_BUF );
VG_(close)(fd);
VG_(close)(fd.val);
if (buf_n_tot >= M_PROCMAP_BUF-5) {
VG_(message)(Vg_UserMsg, "FATAL: M_PROCMAP_BUF is too small; "
"increase it and recompile");

View File

@ -1145,16 +1145,17 @@ calc_gnu_debuglink_crc32(UInt crc, const UChar *buf, Int len)
static
Addr open_debug_file( Char* name, UInt crc, UInt* size )
{
Int fd;
SysRes fd;
struct vki_stat stat_buf;
Addr addr;
UInt calccrc;
if ((fd = VG_(open)(name, VKI_O_RDONLY, 0)) < 0)
fd = VG_(open)(name, VKI_O_RDONLY, 0);
if (fd.isError)
return 0;
if (VG_(fstat)(fd, &stat_buf) != 0) {
VG_(close)(fd);
if (VG_(fstat)(fd.val, &stat_buf) != 0) {
VG_(close)(fd.val);
return 0;
}
@ -1165,13 +1166,13 @@ Addr open_debug_file( Char* name, UInt crc, UInt* size )
if ((addr = (Addr)VG_(mmap)(NULL, *size, VKI_PROT_READ,
VKI_MAP_PRIVATE|VKI_MAP_NOSYMS,
0, fd, 0)) == (Addr)-1)
0, fd.val, 0)) == (Addr)-1)
{
VG_(close)(fd);
VG_(close)(fd.val);
return 0;
}
VG_(close)(fd);
VG_(close)(fd.val);
calccrc = calc_gnu_debuglink_crc32(0, (UChar*)addr, *size);
if (calccrc != crc) {
@ -1226,7 +1227,7 @@ Bool read_lib_symbols ( SegInfo* si )
ElfXX_Ehdr* ehdr; /* The ELF header */
ElfXX_Shdr* shdr; /* The section table */
UChar* sh_strtab; /* The section table's string table */
Int fd;
SysRes fd;
Int i;
Bool ok;
Addr oimage;
@ -1251,16 +1252,16 @@ Bool read_lib_symbols ( SegInfo* si )
n_oimage = stat_buf.st_size;
fd = VG_(open)(si->filename, VKI_O_RDONLY, 0);
if (fd < 0) {
if (fd.isError) {
ML_(symerr)("Can't open .so/.exe to read symbols?!");
return False;
}
oimage = (Addr)VG_(mmap)( NULL, n_oimage,
VKI_PROT_READ, VKI_MAP_PRIVATE|VKI_MAP_NOSYMS,
0, fd, 0 );
0, fd.val, 0 );
VG_(close)(fd);
VG_(close)(fd.val);
if (oimage == ((Addr)(-1))) {
VG_(message)(Vg_UserMsg, "warning: mmap failed on %s", si->filename );

View File

@ -895,22 +895,25 @@ Bool tool_name_present(Char *name, Char *names)
static void load_one_suppressions_file ( Char* filename )
{
# define N_BUF 200
Int fd, i;
Bool eof;
Char buf[N_BUF+1];
Char* tool_names;
Char* supp_name;
Char* err_str = NULL;
SysRes sres;
Int fd, i;
Bool eof;
Char buf[N_BUF+1];
Char* tool_names;
Char* supp_name;
Char* err_str = NULL;
SuppLoc tmp_callers[VG_MAX_SUPP_CALLERS];
fd = VG_(open)( filename, VKI_O_RDONLY, 0 );
if (fd < 0) {
fd = -1;
sres = VG_(open)( filename, VKI_O_RDONLY, 0 );
if (sres.isError) {
VG_(message)(Vg_UserMsg, "FATAL: can't open suppressions file '%s'",
filename );
VG_(exit)(1);
}
fd = sres.val;
#define BOMB(S) { err_str = S; goto syntax_error; }
# define BOMB(S) { err_str = S; goto syntax_error; }
while (True) {
/* Assign and initialise the two suppression halves (core and tool) */

View File

@ -84,11 +84,10 @@ Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf )
return False;
}
/* Returns -1 on failure. */
Int VG_(open) ( const Char* pathname, Int flags, Int mode )
SysRes VG_(open) ( const Char* pathname, Int flags, Int mode )
{
SysRes res = VG_(do_syscall3)(__NR_open, (UWord)pathname, flags, mode);
return res.isError ? -1 : res.val;
return res;
}
void VG_(close) ( Int fd )

View File

@ -297,22 +297,23 @@ static void layout_remaining_space(Addr argc_addr, float ratio)
static char* get_file_clo(char* dir)
{
# define FLEN 512
Int fd, n;
Int n;
SysRes fd;
struct vki_stat s1;
char* f_clo = NULL;
char filename[FLEN];
Char* f_clo = NULL;
Char filename[FLEN];
snprintf(filename, FLEN, "%s/.valgrindrc", ( NULL == dir ? "" : dir ) );
fd = VG_(open)(filename, 0, VKI_S_IRUSR);
if ( fd > 0 ) {
if ( 0 == VG_(fstat)(fd, &s1) ) {
if ( !fd.isError ) {
if ( 0 == VG_(fstat)(fd.val, &s1) ) {
f_clo = malloc(s1.st_size+1);
vg_assert(f_clo);
n = VG_(read)(fd, f_clo, s1.st_size);
n = VG_(read)(fd.val, f_clo, s1.st_size);
if (n == -1) n = 0;
f_clo[n] = '\0';
}
VG_(close)(fd);
VG_(close)(fd.val);
}
return f_clo;
# undef FLEN
@ -1160,7 +1161,9 @@ static void load_client(char* cl_argv[], const char* exec, Int need_help,
VG_(memset)(info, 0, sizeof(*info));
} else {
Int ret;
VG_(clexecfd) = VG_(open)(exec, VKI_O_RDONLY, VKI_S_IRUSR);
/* HACK: assumes VG_(open) always succeeds */
VG_(clexecfd) = VG_(open)(exec, VKI_O_RDONLY, VKI_S_IRUSR)
.val;
ret = VG_(do_exec)(exec, info);
if (ret != 0) {
fprintf(stderr, "valgrind: do_exec(%s) failed: %s\n",
@ -1407,8 +1410,9 @@ static void pre_process_cmd_line_options
static void process_cmd_line_options( UInt* client_auxv, const char* toolname )
{
Int i, eventually_log_fd;
Int toolname_len = VG_(strlen)(toolname);
SysRes sres;
Int i, eventually_log_fd;
Int toolname_len = VG_(strlen)(toolname);
enum {
VgLogTo_Fd,
VgLogTo_File,
@ -1724,30 +1728,40 @@ static void process_cmd_line_options( UInt* client_auxv, const char* toolname )
}
for (;;) {
if (seq == 0)
VG_(sprintf)( logfilename, "%s%s%s.pid%d",
VG_(clo_log_name),
qual ? "." : "", qual ? qual : "",
pid );
else
VG_(sprintf)( logfilename, "%s%s%s.pid%d.%d",
VG_(clo_log_name),
qual ? "." : "", qual ? qual : "",
pid, seq );
HChar pidtxt[20], seqtxt[20];
VG_(sprintf)(pidtxt, "%d", pid);
if (seq == 0)
seqtxt[0] = 0;
else
VG_(sprintf)(seqtxt, ".%d", seq);
seq++;
/* Result:
if (qual) base_name ++ "." ++ qual ++ seqtxt
if (not qual) base_name ++ "." ++ pid ++ seqtxt
*/
VG_(sprintf)( logfilename,
"%s.%s%s",
VG_(clo_log_name),
qual ? qual : pidtxt,
seqtxt );
// EXCL: it will fail with EEXIST if the file already exists.
eventually_log_fd
sres
= VG_(open)(logfilename,
VKI_O_CREAT|VKI_O_WRONLY|VKI_O_EXCL|VKI_O_TRUNC,
VKI_S_IRUSR|VKI_S_IWUSR);
if (eventually_log_fd >= 0) {
if (!sres.isError) {
eventually_log_fd = sres.val;
VG_(clo_log_fd) = VG_(safe_fd)(eventually_log_fd);
break; /* for (;;) */
} else {
// If the file already existed, we try the next name. If it
// was some other file error, we give up.
if (eventually_log_fd != -VKI_EEXIST) {
if (sres.val != VKI_EEXIST) {
VG_(message)(Vg_UserMsg,
"Can't create/open log file '%s.pid%d'; giving up!",
VG_(clo_log_name), pid);
@ -1764,11 +1778,12 @@ static void process_cmd_line_options( UInt* client_auxv, const char* toolname )
vg_assert(VG_(clo_log_name) != NULL);
vg_assert(VG_(strlen)(VG_(clo_log_name)) <= 900); /* paranoia */
eventually_log_fd
sres
= VG_(open)(VG_(clo_log_name),
VKI_O_CREAT|VKI_O_WRONLY|VKI_O_TRUNC,
VKI_S_IRUSR|VKI_S_IWUSR);
if (eventually_log_fd >= 0) {
if (!sres.isError) {
eventually_log_fd = sres.val;
VG_(clo_log_fd) = VG_(safe_fd)(eventually_log_fd);
} else {
VG_(message)(Vg_UserMsg,
@ -1937,7 +1952,7 @@ static void process_cmd_line_options( UInt* client_auxv, const char* toolname )
}
if (VG_(clo_verbosity) > 1) {
Int fd;
SysRes fd;
if (log_to != VgLogTo_Fd)
VG_(message)(Vg_DebugMsg, "");
VG_(message)(Vg_DebugMsg, "Valgrind library directory: %s", VG_(libdir));
@ -1952,12 +1967,12 @@ static void process_cmd_line_options( UInt* client_auxv, const char* toolname )
VG_(message)(Vg_DebugMsg, "Contents of /proc/version:");
fd = VG_(open) ( "/proc/version", VKI_O_RDONLY, 0 );
if (fd < 0) {
if (fd.isError) {
VG_(message)(Vg_DebugMsg, " can't open /proc/version");
} else {
# define BUF_LEN 256
Char version_buf[BUF_LEN];
Int n = VG_(read) ( fd, version_buf, BUF_LEN );
Int n = VG_(read) ( fd.val, version_buf, BUF_LEN );
vg_assert(n <= BUF_LEN);
if (n > 0) {
version_buf[n-1] = '\0';
@ -1965,7 +1980,7 @@ static void process_cmd_line_options( UInt* client_auxv, const char* toolname )
} else {
VG_(message)(Vg_DebugMsg, " (empty?)");
}
VG_(close)(fd);
VG_(close)(fd.val);
# undef BUF_LEN
}
}

View File

@ -547,32 +547,33 @@ void do_hacky_preopened()
void VG_(init_preopened_fds)()
{
int f, ret;
int ret;
struct vki_dirent d;
SysRes f;
f = VG_(open)("/proc/self/fd", VKI_O_RDONLY, 0);
if(f == -1) {
if (f.isError) {
do_hacky_preopened();
return;
}
while((ret = VG_(getdents)(f, &d, sizeof(d))) != 0) {
while ((ret = VG_(getdents)(f.val, &d, sizeof(d))) != 0) {
if (ret == -1)
goto out;
if (VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) {
int fno = VG_(atoll)(d.d_name);
if (fno != f)
if (fno != f.val)
if (VG_(clo_track_fds))
record_fd_open_named(-1, fno);
}
VG_(lseek)(f, d.d_off, VKI_SEEK_SET);
VG_(lseek)(f.val, d.d_off, VKI_SEEK_SET);
}
out:
VG_(close)(f);
out:
VG_(close)(f.val);
}
static

View File

@ -35,18 +35,18 @@
File-related functions.
------------------------------------------------------------------ */
extern Int VG_(open) ( const Char* pathname, Int flags, Int mode );
extern void VG_(close) ( Int fd );
extern Int VG_(read) ( Int fd, void* buf, Int count);
extern Int VG_(write) ( Int fd, const void* buf, Int count);
extern Int VG_(pipe) ( Int fd[2] );
extern OffT VG_(lseek) ( Int fd, OffT offset, Int whence);
extern SysRes VG_(open) ( const Char* pathname, Int flags, Int mode );
extern void VG_(close) ( Int fd );
extern Int VG_(read) ( Int fd, void* buf, Int count);
extern Int VG_(write) ( Int fd, const void* buf, Int count);
extern Int VG_(pipe) ( Int fd[2] );
extern OffT VG_(lseek) ( Int fd, OffT offset, Int whence);
extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
extern Int VG_(fstat) ( Int fd, struct vki_stat* buf );
extern Int VG_(dup2) ( Int oldfd, Int newfd );
extern Int VG_(rename) ( Char* old_name, Char* new_name );
extern Int VG_(unlink) ( Char* file_name );
extern Int VG_(stat) ( Char* file_name, struct vki_stat* buf );
extern Int VG_(fstat) ( Int fd, struct vki_stat* buf );
extern Int VG_(dup2) ( Int oldfd, Int newfd );
extern Int VG_(rename) ( Char* old_name, Char* new_name );
extern Int VG_(unlink) ( Char* file_name );
// Returns False on failure (eg. if the buffer isn't big enough).
extern Bool VG_(getcwd) ( Char* buf, SizeT size );

View File

@ -1345,12 +1345,13 @@ static void file_err ( Char* file )
*/
static void write_hp_file(void)
{
Int i, j;
Int fd, res;
Char *hp_file, *ps_file, *aux_file;
Char* cmdfmt;
Char* cmdbuf;
Int cmdlen;
Int i, j;
Int fd, res;
SysRes sres;
Char *hp_file, *ps_file, *aux_file;
Char* cmdfmt;
Char* cmdbuf;
Int cmdlen;
VGP_PUSHCC(VgpPrintHp);
@ -1358,12 +1359,14 @@ static void write_hp_file(void)
hp_file = make_filename( base_dir, ".hp" );
ps_file = make_filename( base_dir, ".ps" );
aux_file = make_filename( base_dir, ".aux" );
fd = VG_(open)(hp_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
VKI_S_IRUSR|VKI_S_IWUSR);
if (fd < 0) {
sres = VG_(open)(hp_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
VKI_S_IRUSR|VKI_S_IWUSR);
if (sres.isError) {
file_err( hp_file );
VGP_POPCC(VgpPrintHp);
return;
} else {
fd = sres.val;
}
// File header, including command line
@ -1658,9 +1661,10 @@ static void pp_all_XPts(Int fd, XPt* xpt, ULong heap_spacetime,
static void
write_text_file(ULong total_ST, ULong heap_ST)
{
Int fd, i;
Char* text_file;
Char* maybe_p = ( XHTML == clo_format ? "<p>" : "" );
SysRes sres;
Int fd, i;
Char* text_file;
Char* maybe_p = ( XHTML == clo_format ? "<p>" : "" );
VGP_PUSHCC(VgpPrintXPts);
@ -1668,12 +1672,14 @@ write_text_file(ULong total_ST, ULong heap_ST)
text_file = make_filename( base_dir,
( XText == clo_format ? ".txt" : ".html" ) );
fd = VG_(open)(text_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
sres = VG_(open)(text_file, VKI_O_CREAT|VKI_O_TRUNC|VKI_O_WRONLY,
VKI_S_IRUSR|VKI_S_IWUSR);
if (fd < 0) {
if (sres.isError) {
file_err( text_file );
VGP_POPCC(VgpPrintXPts);
return;
} else {
fd = sres.val;
}
// Header