Print your own text in strace. Comments

We use strace a lot. We want to display some text in strace to mark the places that the code reached. The way I see people doing this so far is to put a nonexistent file. A file name is just the text that they want to see in strace. This is pretty fast, but I'm sure there is a better way. I am worried that there may be a lot of code, and possibly kernel locks will be removed, although the mount point is fictitious. Any ideas?

+3
source share
1 answer

write()for a file descriptor out of range it is well displayed in the straceoutputs and should be much faster - the range is checked at an early stage, and there is no need to look at the data at all. (You need to pass the length of the recorded data, not just a line ending with 0, but gcc usually optimizes a strlen()constant line for 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)
{
  STRACE_TRACE("before");
  printf("Hello world\n");
  STRACE_TRACE("after");
  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
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb77da000
write(1, "Hello world\n"..., 12Hello world
)        = 12
write(-1, "after"..., 5)                = -1 EBADF (Bad file descriptor)
exit_group(0)                           = ?
$ 
+6
source

Source: https://habr.com/ru/post/1784194/


All Articles