From: Nicholas Nethercote Date: Thu, 23 Jun 2005 02:26:47 +0000 (+0000) Subject: Removed some repetition in the way VG_(record_fd_open)() is called. X-Git-Tag: svn/VALGRIND_3_0_0~317 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d35537f51f8f89423bcaec5990d58373f98fac5e;p=thirdparty%2Fvalgrind.git Removed some repetition in the way VG_(record_fd_open)() is called. As part of this, VG_(resolve_filename)() no longer calls VG_(malloc)() and so m_libcfile no longer depends on m_mallocfree. git-svn-id: svn://svn.valgrind.org/valgrind/trunk@4001 --- diff --git a/coregrind/m_aspacemgr/aspacemgr.c b/coregrind/m_aspacemgr/aspacemgr.c index 2cdd6469a7..2a708235d8 100644 --- a/coregrind/m_aspacemgr/aspacemgr.c +++ b/coregrind/m_aspacemgr/aspacemgr.c @@ -785,8 +785,8 @@ VG_(map_file_segment)( Addr addr, SizeT len, void VG_(map_fd_segment)(Addr addr, SizeT len, UInt prot, UInt flags, Int fd, ULong off, const Char *filename) { + Char buf[VKI_PATH_MAX]; struct vki_stat st; - Char *name = NULL; st.st_dev = 0; st.st_ino = 0; @@ -799,10 +799,8 @@ void VG_(map_fd_segment)(Addr addr, SizeT len, UInt prot, UInt flags, } if ((flags & SF_FILE) && filename == NULL && fd != -1) - name = VG_(resolve_filename_nodup)(fd); - - if (filename == NULL) - filename = name; + if (VG_(resolve_filename)(fd, buf, VKI_PATH_MAX)) + filename = buf; VG_(map_file_segment)(addr, len, prot, flags, st.st_dev, st.st_ino, off, filename); diff --git a/coregrind/m_libcfile.c b/coregrind/m_libcfile.c index edf9b82eb6..c543b77f11 100644 --- a/coregrind/m_libcfile.c +++ b/coregrind/m_libcfile.c @@ -33,7 +33,6 @@ #include "pub_core_libcassert.h" #include "pub_core_libcfile.h" #include "pub_core_libcprint.h" // For VG_(sprintf)() -#include "pub_core_mallocfree.h" #include "pub_core_syscall.h" #include "vki_unistd.h" @@ -71,32 +70,18 @@ Int VG_(safe_fd)(Int oldfd) /* Given a file descriptor, attempt to deduce its filename. To do this, we use /proc/self/fd/. If this doesn't point to a file, - or if it doesn't exist, we just return NULL. The caller is - responsible for copying the contents of buf out immediately. */ -static HChar resolve_filename_buf[VKI_PATH_MAX]; -HChar* VG_(resolve_filename_nodup) ( Int fd ) + or if it doesn't exist, we return False. */ +Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf ) { HChar tmp[64]; VG_(sprintf)(tmp, "/proc/self/fd/%d", fd); - VG_(memset)(resolve_filename_buf, 0, VKI_PATH_MAX); + VG_(memset)(buf, 0, n_buf); - if (VG_(readlink)(tmp, resolve_filename_buf, VKI_PATH_MAX) == -1) - return NULL; - - return (resolve_filename_buf[0] == '/') - ? resolve_filename_buf - : NULL; -} - -/* Same as resolve_filename_nodup, except that the result is copied - into new memory which the caller is responsible for freeing. */ -HChar* VG_(resolve_filename) ( Int fd ) -{ - HChar* transient = VG_(resolve_filename_nodup)(fd); - return transient - ? VG_(arena_strdup)(VG_AR_CORE, transient) - : NULL; + if (VG_(readlink)(tmp, buf, VKI_PATH_MAX) > 0 && buf[0] == '/') + return True; + else + return False; } /* Returns -1 on failure. */ diff --git a/coregrind/m_signals.c b/coregrind/m_signals.c index e751e7c008..7f7223c6ba 100644 --- a/coregrind/m_signals.c +++ b/coregrind/m_signals.c @@ -1022,7 +1022,8 @@ static void write_note(Int fd, const struct note *n) static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsinfo) { - Char *name; + static Char name[VKI_PATH_MAX]; + Bool res; VG_(memset)(prpsinfo, 0, sizeof(*prpsinfo)); @@ -1049,12 +1050,10 @@ static void fill_prpsinfo(const ThreadState *tst, struct vki_elf_prpsinfo *prpsi prpsinfo->pr_uid = 0; prpsinfo->pr_gid = 0; - name = VG_(resolve_filename)(VG_(clexecfd)); - - if (name != NULL) { + if (VG_(resolve_filename)(VG_(clexecfd), name, VKI_PATH_MAX)) { Char *n = name+VG_(strlen)(name)-1; - while(n > name && *n != '/') + while (n > name && *n != '/') n--; if (n != name) n++; diff --git a/coregrind/m_syswrap/priv_syswrap-generic.h b/coregrind/m_syswrap/priv_syswrap-generic.h index eed37b4f49..6d31ad7ea7 100644 --- a/coregrind/m_syswrap/priv_syswrap-generic.h +++ b/coregrind/m_syswrap/priv_syswrap-generic.h @@ -48,7 +48,7 @@ extern Bool ML_(fd_allowed)(Int fd, const Char *syscallname, ThreadId tid, Bool soft); extern -void ML_(record_fd_open)(ThreadId tid, Int fd, char *pathname); +void ML_(record_fd_open_nameless)(ThreadId tid, Int fd); // Used when killing threads -- we must not kill a thread if it's the thread // that would do Valgrind's final cleanup and output. diff --git a/coregrind/m_syswrap/syswrap-generic.c b/coregrind/m_syswrap/syswrap-generic.c index db09e002bd..e3a1db13d1 100644 --- a/coregrind/m_syswrap/syswrap-generic.c +++ b/coregrind/m_syswrap/syswrap-generic.c @@ -345,7 +345,7 @@ void record_fd_close(ThreadId tid, Int fd) some such thing) or that we don't know the filename. If the fd is already open, then we're probably doing a dup2() to an existing fd, so just overwrite the existing one. */ -void ML_(record_fd_open)(ThreadId tid, Int fd, char *pathname) +static void record_fd_open_with_given_name(ThreadId tid, Int fd, char *pathname) { OpenFd *i; @@ -374,10 +374,29 @@ void ML_(record_fd_open)(ThreadId tid, Int fd, char *pathname) } i->fd = fd; - i->pathname = pathname; + i->pathname = VG_(arena_strdup)(VG_AR_CORE, pathname); i->where = (tid == -1) ? NULL : VG_(record_ExeContext)(tid); } +// Record opening of an fd, and find its name. +static void record_fd_open_named(ThreadId tid, Int fd) +{ + static HChar buf[VKI_PATH_MAX]; + Char* name; + if (VG_(resolve_filename)(fd, buf, VKI_PATH_MAX)) + name = buf; + else + name = NULL; + + record_fd_open_with_given_name(tid, fd, name); +} + +// Record opening of a nameless fd. +void ML_(record_fd_open_nameless)(ThreadId tid, Int fd) +{ + record_fd_open_with_given_name(tid, fd, NULL); +} + static Char *unix2name(struct vki_sockaddr_un *sa, UInt len, Char *name) { @@ -521,7 +540,7 @@ void do_hacky_preopened() for (i = 0; i < count; i++) if(VG_(fcntl)(i, VKI_F_GETFL, 0) != -1) - ML_(record_fd_open)(-1, i, NULL); + ML_(record_fd_open_nameless)(-1, i); } /* Initialize the list of open file descriptors with the file descriptors @@ -539,15 +558,15 @@ void VG_(init_preopened_fds)() } while((ret = VG_(getdents)(f, &d, sizeof(d))) != 0) { - if(ret == -1) + if (ret == -1) goto out; - if(VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) { + if (VG_(strcmp)(d.d_name, ".") && VG_(strcmp)(d.d_name, "..")) { int fno = VG_(atoll)(d.d_name); - if(fno != f) - if(VG_(clo_track_fds)) - ML_(record_fd_open)(-1, fno, VG_(resolve_filename)(fno)); + if (fno != f) + if (VG_(clo_track_fds)) + record_fd_open_named(-1, fno); } VG_(lseek)(f, d.d_off, VKI_SEEK_SET); @@ -646,7 +665,7 @@ static void check_cmsg_for_fds(ThreadId tid, struct vki_msghdr *msg) if(VG_(clo_track_fds)) // XXX: must we check the range on these fds with // VG_(fd_allowed)()? - ML_(record_fd_open) (tid, fds[i], VG_(resolve_filename)(fds[i])); + record_fd_open_named(tid, fds[i]); } cm = VKI_CMSG_NXTHDR(msg, cm); @@ -901,8 +920,8 @@ ML_(generic_POST_sys_socketpair) ( ThreadId tid, } else { POST_MEM_WRITE( arg3, 2*sizeof(int) ); if (VG_(clo_track_fds)) { - ML_(record_fd_open)(tid, fd1, NULL); - ML_(record_fd_open)(tid, fd2, NULL); + ML_(record_fd_open_nameless)(tid, fd1); + ML_(record_fd_open_nameless)(tid, fd2); } } return r; @@ -920,7 +939,7 @@ ML_(generic_POST_sys_socket) ( ThreadId tid, SysRes res ) r = VG_(mk_SysRes_Error)( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, res.val, NULL); + ML_(record_fd_open_nameless)(tid, res.val); } return r; } @@ -971,7 +990,7 @@ ML_(generic_POST_sys_accept) ( ThreadId tid, buf_and_len_post_check ( tid, res, addr_p, addrlen_p, "socketcall.accept(addrlen_out)" ); if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, res.val, NULL); + ML_(record_fd_open_nameless)(tid, res.val); } return r; } @@ -2511,7 +2530,7 @@ POST(sys_dup) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, VG_(resolve_filename)(RES)); + record_fd_open_named(tid, RES); } } @@ -2527,7 +2546,7 @@ POST(sys_dup2) { vg_assert(SUCCESS); if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, VG_(resolve_filename)(RES)); + record_fd_open_named(tid, RES); } PRE(sys_fchdir) @@ -2612,7 +2631,7 @@ POST(sys_fcntl) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, VG_(resolve_filename)(RES)); + record_fd_open_named(tid, RES); } } } @@ -2678,8 +2697,7 @@ POST(sys_fcntl64) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, - VG_(resolve_filename)(RES)); + record_fd_open_named(tid, RES); } } } @@ -4564,8 +4582,7 @@ POST(sys_open) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, - VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1)); + record_fd_open_with_given_name(tid, RES, (Char*)ARG1); } } @@ -4616,7 +4633,7 @@ POST(sys_creat) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1)); + record_fd_open_with_given_name(tid, RES, (Char*)ARG1); } } @@ -4640,8 +4657,8 @@ POST(sys_pipe) } else { POST_MEM_WRITE( ARG1, 2*sizeof(int) ); if (VG_(clo_track_fds)) { - ML_(record_fd_open)(tid, p[0], NULL); - ML_(record_fd_open)(tid, p[1], NULL); + ML_(record_fd_open_nameless)(tid, p[0]); + ML_(record_fd_open_nameless)(tid, p[1]); } } } @@ -5412,7 +5429,7 @@ POST(sys_mq_open) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1)); + record_fd_open_with_given_name(tid, RES, (Char*)ARG1); } } diff --git a/coregrind/m_syswrap/syswrap-linux.c b/coregrind/m_syswrap/syswrap-linux.c index 5a6c0eb7cd..36fb9c8101 100644 --- a/coregrind/m_syswrap/syswrap-linux.c +++ b/coregrind/m_syswrap/syswrap-linux.c @@ -535,7 +535,7 @@ POST(sys_futex) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open)(tid, RES, VG_(arena_strdup)(VG_AR_CORE, (Char*)ARG1)); + ML_(record_fd_open_nameless)(tid, RES); } } } @@ -553,7 +553,7 @@ POST(sys_epoll_create) SET_STATUS_Failure( VKI_EMFILE ); } else { if (VG_(clo_track_fds)) - ML_(record_fd_open) (tid, RES, NULL); + ML_(record_fd_open_nameless) (tid, RES); } } diff --git a/coregrind/pub_core_libcfile.h b/coregrind/pub_core_libcfile.h index b0457dcf7c..1480da7923 100644 --- a/coregrind/pub_core_libcfile.h +++ b/coregrind/pub_core_libcfile.h @@ -47,8 +47,7 @@ extern Int VG_(safe_fd) ( Int oldfd ); extern Int VG_(fcntl) ( Int fd, Int cmd, Int arg ); /* Convert an fd into a filename */ -extern HChar* VG_(resolve_filename_nodup) ( Int fd ); -extern HChar* VG_(resolve_filename) ( Int fd ); +extern Bool VG_(resolve_filename) ( Int fd, HChar* buf, Int n_buf ); /* Default destination port to be used in logging over a network, if none specified. */