The strace tool

The strace(1) command-line utility allows you to trace system calls and signals. As strace(1) is not available on macOS, this section will use a Debian Linux machine to showcase strace(1).

The strace(1) tool only works on Linux machines.

The output that strace(1) generates looks like the following:

$ strace ls
execve("/bin/ls", ["ls"], [/* 15 vars */]) = 0
brk(0)                                  = 0x186c000
fstat(3, {st_mode=S_IFREG|0644, st_size=35288, ...}) = 0

The strace(1) output displays each system call with its parameters as well as its return value. Note that in the Unix world, a return value of 0 is a good thing!

In order to process a binary file, you will need to put the strace(1) command in front of the executable that you want to process. However, you will need to interpret the output on your own in order to use it to make useful conclusions. The good thing is that tools like grep(1) can get you the output that you are actually seeking:

$ strace find /usr 2>&1 | grep ioctl
ioctl(0, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffe3bc59c50) = -1 ENOTTY (Inappropriate ioctl for device)
ioctl(1, SNDCTL_TMR_TIMEBASE or SNDRV_TIMER_IOCTL_NEXT_DEVICE or TCGETS, 0x7ffe3bc59be0) = -1 ENOTTY (Inappropriate ioctl for device)

The strace(1) tool can count time, calls, and errors for each system call when used with the -c command-line option:

$ strace -c find /usr 1>/dev/null
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- -------------
 82.88    0.063223           2     39228           getdents
 16.60    0.012664           1     19587           newfstatat
  0.16    0.000119           0     19618        13 open

As the normal program output is printed in standard output form, and the output of strace(1) is printed in standard error form, the previous command discards the output of the command that is examined and shows the output of strace(1). As you can see from the last line of the output, the open(2) system call was called 19,618 times, generated 13 errors, and took about 0.16% percent of the execution time of the entire command, or about 0.000119 seconds.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.78.136