Take the 2-minute tour ×

We use strace a lot. We would like to output some text into strace to mark places the code has reached. The way i see people have done it so far is to stat an non-existent file. The filename is just the text they want to see in the strace. Its pretty fast, but I'm sure there is a better way. I worry that there might be a lot of code and maybe kernel locks being hit even though the mount point is bogus. Any ideas?

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

write() to an out-of-range file descriptor shows up well in strace output, and should be much faster - the range check happens early, and it doesn't need to look at the data at all. (You need to pass the length of the data to be written, rather than just a 0-terminated string, but gcc will normally optimise strlen() of a constant string to a constant.)

$ cat hw.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#define STRACE_TRACE(str) write(-1, str, strlen(str))

int main(void)
  printf("Hello world\n");
  return 0;
$ gcc -Wall -o hw hw.c
$ strace ./hw
write(-1, "before"..., 6)               = -1 EBADF (Bad file descriptor)
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 150), ...}) = 0
write(1, "Hello world\n"..., 12Hello world
)        = 12
write(-1, "after"..., 5)                = -1 EBADF (Bad file descriptor)
exit_group(0)                           = ?
share|improve this answer
Thanks! I will do a little perf test just to check, but what you said makes a lot of sense. Thanks again. –  johnnycrash Jan 8 '11 at 4:38
Yeah, or write STRACE_TRACE as a normal, sane C function instead of a nasty preprocessor macro. Make it use printf() style arguments if you like. –  MarkR Jan 9 '11 at 15:20
add comment

Your Answer


