[PATCH v4] Print absolute paths in printpathn when -yy is used
zubin.mithra at gmail.com
zubin.mithra at gmail.com
Fri Jun 20 05:34:15 UTC 2014
From: Zubin Mithra <zubin.mithra at gmail.com>
* defs.h (show_fd_path): Change type to unsigned int.
* strace.c (show_fd_path): Update usage to count y flag.
(usage): Update to add -yy option.
* util.c (print_tracee_cwd): New function.
(printpathn): Update to use print_tracee_cwd and print
absolute path.
* strace.1: Add description of -yy option.
Signed-off-by: Zubin Mithra <zubin.mithra at gmail.com>
---
defs.h | 2 +-
strace.1 | 3 +++
strace.c | 5 +++--
util.c | 43 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 50 insertions(+), 3 deletions(-)
diff --git a/defs.h b/defs.h
index 1a3b483..050341d 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;
diff --git a/strace.1 b/strace.1
index 2a24c38..4504359 100644
--- a/strace.1
+++ b/strace.1
@@ -321,6 +321,9 @@ Print all strings in hexadecimal string format.
.B \-y
Print paths associated with file descriptor arguments.
.TP
+.B \-yy
+Print absolute paths instead of relative paths everywhere.
+.TP
.BI "\-a " column
Align return values in a specific column (default column 40).
.TP
diff --git a/strace.c b/strace.c
index 4154cde..28e2488 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? */
@@ -216,6 +216,7 @@ usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...\n\
-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args\n\
-x -- print non-ascii strings in hex, -xx -- print all strings in hex\n\
-y -- print paths associated with file descriptor arguments\n\
+-yy -- print absolute paths instead of relative paths everywhere\n\
-h -- print help message, -V -- print version\n\
-a column -- alignment COLUMN for printing syscall results (default %d)\n\
-b execve -- detach on this syscall\n\
@@ -1734,7 +1735,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..1308d4f 100644
--- a/util.c
+++ b/util.c
@@ -563,6 +563,42 @@ string_quote(const char *instr, char *outstr, long len, int size)
return 0;
}
+
+/*
+ * Print the current working directory of the tracee process
+ */
+static bool
+print_tracee_cwd(struct tcb *tcp)
+{
+ char *linkpath = NULL;
+ char cwd[MAXPATHLEN + 2];
+ int retval;
+ ssize_t n;
+
+ retval = asprintf(&linkpath, "/proc/%u/cwd", tcp->pid);
+ if (retval == -1)
+ return false;
+
+ n = readlink(linkpath, cwd, MAXPATHLEN);
+
+ if (n > 0) {
+ char *outstr;
+
+ cwd[n++] = '/';
+ cwd[n++] = '\0';
+
+ /* cwd has n-1 characters followed by a null */
+ outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
+ string_quote(cwd, outstr, -1, n);
+ outstr[strlen(outstr)-1] = '\0'; /* Don't print the closing quotes */
+ tprints(outstr);
+ free(linkpath);
+ return true;
+ }
+ free(linkpath);
+ return false;
+}
+
/*
* Print path string specified by address `addr' and length `n'.
* If path length exceeds `n', append `...' to the output.
@@ -588,11 +624,18 @@ printpathn(struct tcb *tcp, long addr, int n)
tprintf("%#lx", addr);
else {
char *outstr;
+ bool prefix_printed = false;
path[n] = '\0';
+ if (show_fd_path > 1 && *path && *path != '/')
+ prefix_printed = print_tracee_cwd(tcp);
n++;
outstr = alloca(4 * n); /* 4*(n-1) + 3 for quotes and NUL */
string_quote(path, outstr, -1, n);
+
+ /* Dont print opening quotes if cwd is printed */
+ if (prefix_printed)
+ outstr += 1;
tprints(outstr);
if (!nul_seen)
tprints("...");
--
1.8.4
More information about the Strace-devel
mailing list