[PATCH v2] Print absolute paths in printpathn when -yy is used

zubin.mithra at gmail.com zubin.mithra at gmail.com
Thu Jun 12 10:05:10 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.
* util.c (print_tracee_cwd): New function.
(printpathn): Update to use print_tracee_cwd and print
 absolute path.

Signed-off-by: Zubin Mithra <zubin.mithra at gmail.com>
---
 defs.h   |  2 +-
 strace.c |  4 ++--
 util.c   | 34 ++++++++++++++++++++++++++++++++++
 3 files changed, 37 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.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..9cb5f28 100644
--- a/util.c
+++ b/util.c
@@ -588,11 +588,18 @@ printpathn(struct tcb *tcp, long addr, int n)
 		tprintf("%#lx", addr);
 	else {
 		char *outstr;
+		int retval = -1;
 
 		path[n] = '\0';
+		if (show_fd_path > 1 && *path && *path != '/')
+			retval = 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 (retval != -1)
+			outstr += 1;
 		tprints(outstr);
 		if (!nul_seen)
 			tprints("...");
@@ -1549,3 +1556,30 @@ clearbpt(struct tcb *tcp)
 	tcp->flags &= ~TCB_BPTSET;
 	return 0;
 }
+
+int
+print_tracee_cwd(struct tcb *tcp)
+{
+	int link_size = sizeof("/proc/%u/cwd") + sizeof(int) * 3;
+	char linkpath[link_size];
+	char cwd[MAXPATHLEN+2];
+	ssize_t n;
+
+	snprintf(linkpath, link_size, "/proc/%u/cwd", tcp->pid);
+	n = readlink(linkpath, cwd, MAXPATHLEN);
+
+	if (n > 0) {
+		char *outstr;
+
+		cwd[n] = '/';
+		cwd[n+1] = '\0';
+
+		/* cwd has n+1 characters followed by a null */
+		outstr = alloca(4 * n + 7); /* 4*(n+1) + 3 for quotes and NUL */
+		string_quote(cwd, outstr, -1, n+2); /* Quote n+1 bytes */
+		outstr[strlen(outstr)-1] = '\0'; /* Don't print the closing quotes */
+		tprints(outstr);
+		return 0;
+	}
+	return -1;
+}
-- 
1.8.4





More information about the Strace-devel mailing list