Monday, March 3, 2014

Strace -Making tracing system calls a breeze...

Linux/Unix has a nice system call tracer/debugging tool which prints out a trace of all system calls made by a program during execution. For example if we need to check which system calls are being made by netcat during connection with google we can use the following command

%strace -e connect nc google.com 80

( strace is looking for connect calls during connection with google.com on port 80. The output will look something like this


connect(3, {sa_family=AF_UNSPEC, sa_data="\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16) = 0
connect(3, {sa_family=AF_INET, sin_port=htons(80), sin_addr=inet_addr("170.167.97.9")}, 16) = 0
connect(3, {sa_family=AF_UNSPEC, sa_data="\0\0\0\0\0\0\0\0\0\0\0\0\0\0"}, 16) = 0

Or if we need to look at what times does the write statements in our program execute we could also put in relative times into the strace by writing

%strace -r -e write ./tst2 34 54 

 (34 and 54 are the command line arguments to our executable tst2)
And we need to look at the write calls. The output will look like

 0.000000 write(1, "\n", 1)         = 1
  0.000052 write(1, "\n", 1)         = 1
 0.000024 write(1, " Welcome to simple add and subtr"..., 44 Welcome to simple add and subtract program) = 44
 0.000029 write(1, "\n", 1)         = 1
 0.000023 write(1, "\n", 1)         = 1
 0.000030 write(1, "Sum of 34 + 54 = 88\n\n", 21Sum of 34 + 54 = 88) = 21
 0.000030 write(1, "Difference of 34 - 54 = -20\n\n", 29  Difference of 34 - 54 = -20

Notice the relative time stamps as the first column above.

No comments:

Post a Comment