strace command
strace (System Trace)
Purpose
strace command in Linux is used to trace system calls and signals made by a process.This command is very useful for:
-
Debugging programs
-
Learning how system calls work
-
Finding out why a program fails or behaves unexpectedly
๐งฉ Basic Syntax
or
⚙️ What It Does
strace intercepts and records these calls, showing what the process is requesting from the operating system.
๐ Examples
1. Trace a Simple Command
This will run the ls command, but instead of showing only the directory listing, it will print all the system calls ls makes — for example:
Here you can see calls like:
-
openat() – opening a directory
-
write() – writing output to the terminal
-
close() – closing file descriptors
2. Trace an Existing Process
This attaches strace to an existing process with process ID (PID) 1234 and shows its system calls in real time.
Use this carefully — the process will pause briefly while being attached.
3. Save Output to a File
This saves all system call logs to a file (trace_output.txt) instead of printing them on the screen.
4. Count System Calls
This summarizes how many times each system call was made and how much time was spent in each call.
Example Output:
This is very useful for teaching how frequently different system calls are made by user-level commands.
5. Trace Only Specific System Calls
This will show only the open(), read(), and write() system calls — filtering out the rest.
6. Trace a Program and Its Child Processes
-f ensures that any processes created using fork() or exec() are also traced.
๐งพ Commonly Seen System Calls
| System Call | Description |
|---|---|
open() | Open a file |
read() | Read from a file descriptor |
write() | Write to a file descriptor |
close() | Close a file descriptor |
execve() | Execute a program |
fork() | Create a new process |
mmap() | Map files or devices into memory |
brk() | Change data segment size (for memory allocation) |
๐ก Useful for Students
-
Helps understand the bridge between user space and kernel space.
-
Demonstrates that even simple commands (like
ls,cat, orecho) rely on many system calls. -
Great for linking theory (system calls, process states, file handling) with practical observation.
๐งช Simple Lab Exercise
Task:
-
Run:
-
Note which system call is invoked most frequently.
-
Then run:
-
Compare the number and types of system calls between
lsandcat.
Comments
Post a Comment