Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Shell Scripting
  • Kali Linux
  • Ubuntu
  • Red Hat
  • CentOS
  • Docker in Linux
  • Kubernetes in Linux
  • Linux interview question
  • Python
  • R
  • Java
  • C
  • C++
  • JavaScript
  • DSA
Open In App

Strace command in Linux with Examples

Last Updated : 02 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

'strace' is a powerful tool for monitoring and diagnosing processes in Linux. It is primarily used for debugging programs, troubleshooting issues, intercepting and recording system calls, and tracing running processes. It provides valuable insights into how a program interacts with the system, especially when the source code is not available.

It also acts as a debugging tool that helps in troubleshooting issues. It is majorly used for the following purposes:

  • Debugging Programs
  • Troubleshooting Programs
  • Intercept System calls by a process
  • Record system calls by a process
  • Signals received by a process
  • Trace running processes

In case the source code is not available, 'strace' is used to analyze how a program interacts with the system to debug the executing of program. It returns the name of each system call along with its argument enclosed in parenthesis and its return value to standard error.

Installing 'strace' Process Monitoring Tool

To install the 'strace' tool use the following commands as per your Linux distribution.

1. In case of Debian/Ubuntu

$sudo apt install strace

2. In case of CentOS/RedHat

$yum install strace

Working with Strace Process Monitoring Tool

1. To get the system call, argument, and the result of the call.

$strace ls

Note: Here "ls" is the command whose system call is to be traced.

strace command in Linuxstrace command in Linux

As can be seen, it displays the system call, argument(in parenthesis), and result of the call of ls command. And +++ exited with 0 +++, in the last line states that exit status is 0 which means there was no error. In case of an error, the exit code is -1.

2. To count number of system calls.

$strace -c ls 

Note: Here "ls" is the command whose system call is to be traced.

To count number of system calls

As can be seen, it displays the number of times each system call was made and prints the total and even showing the number and time spent in each call.

3. To trace particular or specific system calls.

$strace -e trace=write ls 

Note: Here "ls" is the command whose system call is to be traced. And the name of the system call which is to be traced is write.

To trace particular or specific system calls

As can be seen, it only displays the name, argument, and result of write system call.

4. To trace network related system calls

$strace -e trace=network nc -v -n 127.0.0.1 801 

Note: Here "nc -v -n 127.0.0.1 801" is the command whose system call is to be traced. And the name of the system call which is to be traced is the network.

To trace network related system calls

As can be seen, it only displays the name, argument, and result of the network system call.

5. To trace signal related system calls

$strace -e trace=signal nc -v -n 127.0.0.1 801 

Note: Here "nc -v -n 127.0.0.1 801" is the command whose system call is to be traced. And the name of the system call which is to be traced is signal.

To trace signal related system calls

As can be seen, it only displays the name, argument, and result of the signal system call.

6. To print timestamp of each call.

$strace -r ls 

Note: Here "ls" is the command whose system call is to be traced.

To print timestamp of each callTo print timestamp of each call

As can be seen, it displays a relative timestamp upon entry to each system call. It records the time difference between the beginning of successive system calls.

7. To print time spent on system calls.

$strace -T ls 

Note: Here "ls" is the command whose system call is to be traced.

To print time spent on system callsTo print time spent on system calls

As can be seen the time spent on each call is printed at the end of each line.

8. To print wall clock time of each system call.

$strace -t ls 

Note: Here "ls" is the command whose system call is to be traced.

To print wall clock time of each system callTo print wall clock time of each system call

As it can be seen that the prefix of each line if the wall clock time itself of the system call.

9. To print instruction pointer.

$strace -i ls 

Note: Here "ls" is the command whose system call is to be traced.

To print wall clock time of each system callTo print wall clock time of each system call

As can be seen, the instruction pointer is printed in each line of output.

10. To print output to a file

$strace -o output.txt ls 

Note: Here "ls" is the command whose system call is to be traced. And output.txt is the name of the file in which output is to be stored.

To print wall clock time of each system callTo print wall clock time of each system call

As can be seen, the output of the command is stored in the 'output.txt' file.

Conclusion

In conclusion, 'strace' is a versatile tool for debugging and monitoring system interactions, providing detailed insights that can help in troubleshooting and improving system performance.


M

manav014
Improve
Article Tags :
  • Technical Scripter
  • Linux-Unix
  • Write From Home
  • linux-command
  • Linux-system-commands

Similar Reads

    Shift command in Linux with examples
    Shift is a built-in command in bash that after getting executed, shifts/moves the command line arguments to one position left. The first argument is lost after using the shift command. This command takes only one integer as an argument. This command is useful when you want to get rid of the command
    3 min read
    showkey command in Linux with Examples
    showkey command in Linux is used to examine the codes sent by the keyboard. showkey prints to standard output either the scan codes or the key code or the 'ASCII' code of each key pressed. The program runs in the first two modes until 10 seconds have elapsed since the last key press or release event
    2 min read
    shred Command in Linux with Examples
    When you delete a file from Linux or from any Operating System, then the file is not deleted permanently from the hard disk. When a file is deleted, it first gets moved to the trash and as soon as you clear off the trash the files get deleted for the file system. But the file is still there on your
    6 min read
    shutdown command in Linux with Examples
    The `shutdown` operation in Linux is a crucial command for managing system power states, allowing administrators to halt safely, power off, or reboot the system. This command provides flexibility in scheduling downtimes, ensuring minimal disruption to users and processes. In this article, we will ex
    5 min read
    sleep Command in Linux with Examples
    sleep command is used to create a dummy job. A dummy job helps in delaying the execution. It takes time in seconds by default but a small suffix(s, m, h, d) can be added at the end to convert it into any other format. This command pauses the execution for an amount of time which is defined by NUMBER
    5 min read
    source Command in Linux with Examples
    If you're new to the world of Linux, you might have heard about commands that do various tasks, but some like the 'source' command might seem a bit confusing at first. Don't worry; let's break it down step by step.What is the Source Command?The source command in Linux is like a magic wand that lets
    7 min read
    How to sort lines in text files in Linux | sort Command
    The sort command in Linux is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.sort is a command that sorts text files alphabetically, numer
    7 min read
    Split Command in Linux with Examples
    Split command in Linux is used to split large files into smaller files. It splits the files into 1000 lines per file(by default) and even allows users to change the number of lines as per requirement. The names of the files are PREFIXaa, PREFIXab, PREFIXac, and so on. By default the PREFIX of files
    6 min read
    How to use SSH to connect to a remote server in Linux | ssh Command
    Secure Shell, commonly known as SSH, is like a super-secure way to talk to faraway computers, called servers. It's like a secret tunnel on the internet that keeps your conversations safe and private. Imagine you're sending a letter, and instead of sending it openly, you put it in a magic envelope th
    8 min read
    Strace command in Linux with Examples
    'strace' is a powerful tool for monitoring and diagnosing processes in Linux. It is primarily used for debugging programs, troubleshooting issues, intercepting and recording system calls, and tracing running processes. It provides valuable insights into how a program interacts with the system, espec
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences