static void fprint_CC_table_and_calc_totals(void)
{
Int fd;
+ SysRes sres;
Char buf[512];
fileCC *curr_fileCC;
fnCC *curr_fnCC;
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,
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
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");
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;
}
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) {
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;
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 );
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) */
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 )
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
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",
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,
}
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);
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,
}
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));
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';
} else {
VG_(message)(Vg_DebugMsg, " (empty?)");
}
- VG_(close)(fd);
+ VG_(close)(fd.val);
# undef BUF_LEN
}
}
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
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 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 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 );
// Returns False on failure (eg. if the buffer isn't big enough).
extern Bool VG_(getcwd) ( Char* buf, SizeT size );
*/
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);
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
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);
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