From: Zubin Mithra <[email protected]>

* defs.h (show_fd_path): Change type to unsigned int.
(get_tracee_cwd, print_abspath): New prototypes.
* strace.c (show_fd_path): Update usage to count y flag.
* util.c (get_tracee_cwd): New function.
(printpathn): Update to use get_tracee_cwd and print
  absolute path.

Signed-off-by: Zubin Mithra <[email protected]>
---
 defs.h   |  4 +++-
 strace.c |  4 ++--
 util.c   | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 54 insertions(+), 4 deletions(-)

diff --git a/defs.h b/defs.h
index 1a3b483..62b86cd 100644
--- a/defs.h
+++ b/defs.h
@@ -562,7 +562,7 @@ extern bool iflag;
 extern bool count_wallclock;
 extern unsigned int qflag;
 extern bool not_failing_only;
-extern bool show_fd_path;
+extern unsigned int show_fd_path;
 extern bool hide_log_until_execve;
 /* are we filtering traces based on paths? */
 extern const char **paths_selected;
@@ -646,6 +646,8 @@ extern const char *signame(int);
 extern void pathtrace_select(const char *);
 extern int pathtrace_match(struct tcb *);
 extern int getfdpath(struct tcb *, int, char *, unsigned);
+extern int get_tracee_cwd(struct tcb *, char *);
+extern bool print_abspath(struct tcb *, char *, int);
 
 extern const char *xlookup(const struct xlat *, int);
 
diff --git a/strace.c b/strace.c
index 46c9d63..a17506a 100644
--- a/strace.c
+++ b/strace.c
@@ -129,7 +129,7 @@ static int post_attach_sigstop = TCB_IGNORE_ONE_SIGSTOP;
 bool not_failing_only = 0;
 
 /* Show path associated with fd arguments */
-bool show_fd_path = 0;
+unsigned int show_fd_path = 0;
 
 static bool detach_on_execve = 0;
 /* Are we "strace PROG" and need to skip detach on first execve? */
@@ -1734,7 +1734,7 @@ init(int argc, char *argv[])
                        xflag++;
                        break;
                case 'y':
-                       show_fd_path = 1;
+                       show_fd_path++;
                        break;
                case 'v':
                        qualify("abbrev=none");
diff --git a/util.c b/util.c
index 33482d5..583c395 100644
--- a/util.c
+++ b/util.c
@@ -571,7 +571,9 @@ void
 printpathn(struct tcb *tcp, long addr, int n)
 {
        char path[MAXPATHLEN + 1];
+       char cwd[MAXPATHLEN + 1];
        int nul_seen;
+       int valid_length = 1;
 
        if (!addr) {
                tprints("NULL");
@@ -593,8 +595,28 @@ printpathn(struct tcb *tcp, long addr, int n)
                n++;
                outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
                string_quote(path, outstr, -1, n);
+
+               /* If -yy is used and the path is not absolute, find the cwd of 
the tracee
+                  and print that out first */
+               if (show_fd_path == 2 && outstr[1] != '/') {
+                       /* If the call to get_tracee_cwd succeeds, we 
concatenate the
+                          cwd and outstr and print it out. If the call fails, 
we just
+                          print out the relative path. */
+                       char *outcwd;
+                       int cwd_len;
+                       cwd_len = get_tracee_cwd(tcp, cwd);
+                       if (cwd_len != -1) {
+                               outcwd = alloca(4 * cwd_len);
+                               string_quote(cwd, outcwd, -1, cwd_len+1);
+                               outcwd[strlen(outcwd)-1] = '\0';
+                               tprintf("%s", outcwd);
+                               if (strlen(cwd) + strlen(outstr) > MAXPATHLEN)
+                                       valid_length = 0;
+                       }
+               }
+               if (show_fd_path == 2) outstr += 1;
                tprints(outstr);
-               if (!nul_seen)
+               if (!nul_seen || (show_fd_path==2) && !valid_length)
                        tprints("...");
        }
 }
@@ -1549,3 +1571,29 @@ clearbpt(struct tcb *tcp)
        tcp->flags &= ~TCB_BPTSET;
        return 0;
 }
+
+/* Return the current working directory of the tracee process,
+   with a trailing slash. */
+int
+get_tracee_cwd(struct tcb *tcp, char *cwd)
+{
+       int link_size = sizeof("/proc/%u/cwd") + sizeof(int) * 3;
+       char linkpath[link_size];
+       ssize_t n;
+
+       snprintf(linkpath, link_size, "/proc/%u/cwd", tcp->pid);
+       n = readlink(linkpath, cwd, MAXPATHLEN);
+
+       /* If readlink fails, something is abnormal(path > 4096) about
+          the traced process and its cwd, so return an empty cwd. */
+       if (n == -1) {
+               cwd[0] = '\0';
+               return n;
+       }
+
+       if (n >= 0) {
+               cwd[n] = '/';
+               cwd[n+1] = '\0';
+       }
+       return n+1;
+}
-- 
1.8.4


------------------------------------------------------------------------------
Learn Graph Databases - Download FREE O'Reilly Book
"Graph Databases" is the definitive new guide to graph databases and their 
applications. Written by three acclaimed leaders in the field, 
this first edition is now available. Download your free book today!
http://p.sf.net/sfu/NeoTech
_______________________________________________
Strace-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/strace-devel

Reply via email to