Skip to content

Instantly share code, notes, and snippets.

@m000
Created February 26, 2014 23:23
Show Gist options
  • Save m000/9240954 to your computer and use it in GitHub Desktop.
Save m000/9240954 to your computer and use it in GitHub Desktop.
This patch makes strace print the address of string buffers in addition to their contents. This could be helpful when using strace to debug how data are copied in a program.
diff --git a/util.c b/util.c
index 85bb94c..9158c64 100644
--- a/util.c
+++ b/util.c
@@ -602,6 +602,7 @@ printpath(struct tcb *tcp, long addr)
printpathn(tcp, addr, MAXPATHLEN);
}
+#define PTR_ADDRESS_BUFSIZE 12
/*
* Print string specified by address `addr' and length `len'.
* If `len' < 0, treat the string as a NUL-terminated string.
@@ -612,6 +613,7 @@ printstr(struct tcb *tcp, long addr, long len)
{
static char *str = NULL;
static char *outstr;
+ int outstr_offset;
int size;
int ellipsis;
@@ -621,9 +623,9 @@ printstr(struct tcb *tcp, long addr, long len)
}
/* Allocate static buffers if they are not allocated yet. */
if (!str) {
- unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3;
+ unsigned int outstr_size = 4 * max_strlen + /*for quotes and NUL:*/ 3 + /* for printing ptr address */ PTR_ADDRESS_BUFSIZE;
- if (outstr_size / 4 != max_strlen)
+ if ((outstr_size-PTR_ADDRESS_BUFSIZE) / 4 != max_strlen )
die_out_of_memory();
str = malloc(max_strlen + 1);
if (!str)
@@ -654,10 +656,14 @@ printstr(struct tcb *tcp, long addr, long len)
}
}
+ /* print address to outstr */
+ outstr_offset = 0;
+ outstr_offset = snprintf(outstr, PTR_ADDRESS_BUFSIZE, "%p:", addr);
+
/* If string_quote didn't see NUL and (it was supposed to be ASCIZ str
* or we were requested to print more than -s NUM chars)...
*/
- ellipsis = (string_quote(str, outstr, len, size) &&
+ ellipsis = (string_quote(str, outstr+outstr_offset, len, size) &&
(len < 0 || len > max_strlen));
tprints(outstr);
@nucleare
Copy link

Please pardon my newb question but how can I go about applying this to my system's installation of strace? If it's not too much trouble would you mind elaborating or would it be right to assume I have to build from source and add this in somewhere prior to building?

I had read from an answer on stack overflow that I could simple add the option -e trace=memory for strace which does just that, where it then prints the memory address of wherever a program accesses the memory but as also mentioned in another comment found there and as I'd come to discover, it doesn't show the other relevant information which requires I compare to outputs of strace to correlate the relevant information. Thus this patch seems perfect for what I'm tryin to do. But, I'm not sure what to do with this file.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment