Skip to content
Advertisement

Why am I not able to view the clone() system call in strace output when typing ‘strace ls’ in terminal?

My understanding is that when we type ls in the terminal:

  1. It invokes fork() library function which invokes system call clone() to create a child process.
  2. Then system call execve() is invoked to replace address space of the new process created with new content.

In that case I was expecting to see system calls clone() and execve() in the strace ls output. But I am seeing only execve() and not clone().

What could be reason?

I tried the following commands:

JavaScript

OS – Redhat

Advertisement

Answer

It’s true, your shell does fork + execve to execute a command, but you are not tracing your shell, so you will not see it!

The strace tool simply creates a child (through fork), attaches to it with ptrace and then does execve of the requested command, so if you do a simple strace ls the first thing that you will see is the execve done by strace to start ls.

If you want to see what your shell does, you can start a shell and then attach to it from another shell with strace.

  1. Start one shell, then get its PID (in bash just echo $$ will get you the current shell PID).
  2. Start a second shell, and run strace -f -p PID_OF_FIRST_SHELL.
  3. Execute ls in the first shell, and whatch the output of strace on the second one.

Note that since strace traces every syscall by default, and shells are usually very complex programs, you will see a lot of syscalls in the output. If you want to just observe a few syscalls, you can filter them with the -e option, for example:

JavaScript

Example on my machine:

  • Shell 1:

    JavaScript
  • Shell 2:

    JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement