[PATCH v3] Print absolute paths in printpathn when -yy is used
Mike Frysinger
vapier at gentoo.org
Tue Jun 17 02:47:05 UTC 2014
On Sun 15 Jun 2014 09:14:05 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.
> * strace.1: Add description of -yy option
you should also update the usage string so `strace -h` documents -yy
> +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];
there should be spaces around that +
> + ssize_t n;
> +
> + snprintf(linkpath, link_size, "/proc/%u/cwd", tcp->pid);
> + n = readlink(linkpath, cwd, MAXPATHLEN);
anytime you start using magic constant lengths for paths, you know you're
probably doing something wrong. you have to remember that some systems don't
have a max path length restriction so these constants won't even be defined.
if you lstat() the path, the st_size will tell you the length, however that
has a TOCTOU race (another thread could change the working dir), so i guess
that's not really viable :(.
you could use an alloca() buffer starting at a "large enough" value to cover
most common cases (like 256) and put it into a loop -- when the return value
of readlink is equal to bufsiz, double the buffer length and try again.
i would also highlight that some systems (really just nommu) really do not
want large buffer stacks because the stack size is fixed. by putting large
values there, you can easily blow it (which this code is doing). if this
particular func matters to performance, we can use a cached malloc buffer
(that you can realloc() to increase over time).
-mike
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: This is a digitally signed message part.
URL: <http://lists.strace.io/pipermail/strace-devel/attachments/20140616/b99955bb/attachment.bin>
More information about the Strace-devel
mailing list