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

Dmitry V. Levin ldv at altlinux.org
Thu Jun 12 22:54:08 UTC 2014


On Thu, Jun 12, 2014 at 03:35:10PM +0530, zubin.mithra at gmail.com wrote:
> 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 */

I suppose incremented "n" would make the code a bit simpler, e.g.

		cwd[n++] = '/';
		cwd[n++] = '\0';
		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);
> +		return 0;
> +	}
> +	return -1;
> +}

Now it doesn't compile for me:
util.c: In function 'printpathn':
util.c:595:4: error: implicit declaration of function 'print_tracee_cwd' [-Werror=implicit-function-declaration]

Yes, I configure strace build using --enable-gcc-Werror, and I recommend
everybody who is going to submit patches to do the same.

By the way, -yy is a user visible feature, so it needs to be documented.


-- 
ldv
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 181 bytes
Desc: not available
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20140613/0207a135/attachment.bin>


More information about the Strace-devel mailing list