Open In App

Decoding information from the strace output

When a program is executed, it toggles multiple times into user mode and kernel mode. In user mode, a process has limited access to resources, while in kernel mode it has access to privileged hardware resources and its data. A process toggles from user mode to kernel mode using System Calls.

Strace is a tool to analyze system call activities of a process. It gives us information about:



Analyzing system calls helps a lot when you do not have access to source code and debugging is done using executable binary only. This article is not about How To use Strace tool, its more about analyzing the output of Strace tool because while executing Strace on a process, it dumps a lot of information related to system calls. At first instance, it looks very scary and analyzing each system call would be a very time-consuming task. Moreover, it may not be required because most of the starting system calls are for the housekeeping purpose and do not add much value to debugging. Once system call flow is understood for one process, then it can be easily identified and remove housekeeping system calls and concentrate on important one for debugging our actual problem.

Program:






// C program to print Hello World!
// filename: hello.c
#include <stdio.h>
  
// Driver Code
int main(int argc, char* argv[])
{
    // Print Hello World
    printf(" geeksforgeeks: hello world !! \n");
  
    return 0;
}

Output:
geeksforgeeks: hello world !!

Compile the above program using the below command:

$ gcc hello.c

Find the Strace out of the above-compiled program using the below command:

$ strace ./a.out

Now, the Strace output for the above program is:

Before starting analyzing system calls, let’s briefly talk about program execution w.r.t. system calls:

Decoding Strace Output:

Now break the Strace output into meaningful chunks for better understanding:

Below is the Strace output for the “Hello World” program built using “-static” option:

It can see that statically built executable does not call open(), mmap(), close() etc, which was done to map shared libraries. Now we have enough understanding about system calls to analyze Strace tool output and to filter out interested system calls for debugging. For any system call detail, best place would be its man page. Which can be accessed using below command.

$ man 2 <System Call>

Article Tags :