{"id":9418,"date":"2021-08-17T13:27:42","date_gmt":"2021-08-17T13:27:42","guid":{"rendered":"https:\/\/linuxways.net\/?p=9418"},"modified":"2021-08-17T13:27:42","modified_gmt":"2021-08-17T13:27:42","slug":"11-strace-command-with-example-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/11-strace-command-with-example-in-linux\/","title":{"rendered":"11 Strace Command with Example in Linux"},"content":{"rendered":"<p>Strace is a very powerful debugging command-line utility tool which helps to troubleshoot the issues by monitoring the system calls and signals of the specific program. In case we do not have source code available, strace can be used to analyze how a program interacts with the system. In this article, we will discuss some important strace commands to troubleshoot and debug programs and processes in Linux.<\/p>\n<h3>Installation<\/h3>\n<p>Installation of strace utility tools is a straightforward and easy process. Use the following command to install the application as per your Linux distribution.<\/p>\n<p><strong>Debian\/Ubuntu<\/strong><\/p>\n<pre>$ sudo apt install strace<\/pre>\n<p><strong>RedHat\/CentOS<\/strong><\/p>\n<pre>$ yum install strace<\/pre>\n<p>Verify the installation by running the following command<\/p>\n<pre>$ strace -V<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"712\" height=\"119\" class=\"wp-image-9419\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-371.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-371.png 712w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-371-300x50.png 300w\" sizes=\"auto, (max-width: 712px) 100vw, 712px\" \/><\/p>\n<h3>1.Trace Linux Command system calls using strace<\/h3>\n<p>Strace is helpful while tracing Linux command system calls. In this example, a simple command <strong>pwd <\/strong>is used for tracing.<\/p>\n<pre>$ strace pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1061\" height=\"712\" class=\"wp-image-9420\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-372.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-372.png 1061w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-372-300x201.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-372-1024x687.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-372-768x515.png 768w\" sizes=\"auto, (max-width: 1061px) 100vw, 1061px\" \/><\/p>\n<h3>2. Redirect trace log in a file<\/h3>\n<p>The system calls trace log can be redirected to a file using strace command as:<\/p>\n<p>$ strace -o pwd-log.txt pwd<\/p>\n<p>Use cat command to find the contents of the file as:<\/p>\n<pre>$ cat pwd-log.txt<\/pre>\n<h3>3. Use strace to print system calls summary instead of regular output.<\/h3>\n<p>Using strace with option -c makes it possible to print the log summary. In this example, a summary of the linux command pwd is presented.<\/p>\n<pre>$ strace -c pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"824\" height=\"325\" class=\"wp-image-9421\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-373.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-373.png 824w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-373-300x118.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-373-768x303.png 768w\" sizes=\"auto, (max-width: 824px) 100vw, 824px\" \/><\/p>\n<h3>4. Trace particular system calls using strace<\/h3>\n<p>Strace command with option -e and trace type (read, write) can be used to trace specific system calls. In this example, the write system call is being traced for the command pwd.<\/p>\n<pre>$ strace -e trace=write pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"834\" height=\"70\" class=\"wp-image-9422\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-374.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-374.png 834w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-374-300x25.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-374-768x64.png 768w\" sizes=\"auto, (max-width: 834px) 100vw, 834px\" \/><\/p>\n<h3>5. Strace command to print timestamp of each system call.<\/h3>\n<p>Strace command with the option -r can be used to print all the relative timestamps of each system call.<\/p>\n<pre>$ strace -r pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1074\" height=\"486\" class=\"wp-image-9423\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-375.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-375.png 1074w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-375-300x136.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-375-1024x463.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-375-768x348.png 768w\" sizes=\"auto, (max-width: 1074px) 100vw, 1074px\" \/><\/p>\n<h3>6. Tracing using process id<\/h3>\n<p>If there is any process already running in the system, the system calls can be traced by using strace command with option -p along with the process id. In this example, the process id of nginx is used for tracing<\/p>\n<p>Syntax<\/p>\n<pre>$ strace -p &lt;process id&gt;<\/pre>\n<pre>$ strace -p 12842<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1351\" height=\"526\" class=\"wp-image-9424\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-376.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-376.png 1351w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-376-300x117.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-376-1024x399.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-376-768x299.png 768w\" sizes=\"auto, (max-width: 1351px) 100vw, 1351px\" \/><\/p>\n<p>Tracing can be stopped by pressing ctrl+c.<\/p>\n<h3>7. Strace command to print debugging output<\/h3>\n<p>Strace command with option -d can be used to print debugging output. In this example, a simple Linux command pwd is used for tracing.<\/p>\n<pre>$ strace -d pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"690\" height=\"696\" class=\"wp-image-9425\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-377.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-377.png 690w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-377-297x300.png 297w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-377-150x150.png 150w\" sizes=\"auto, (max-width: 690px) 100vw, 690px\" \/><\/p>\n<h3>8. Strace command to print time spent on system calls.<\/h3>\n<p>Strace command with option -T gives the time spent on system calls as:<\/p>\n<pre>$ strace -T pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1082\" height=\"711\" class=\"wp-image-9426\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-378.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-378.png 1082w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-378-300x197.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-378-1024x673.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-378-768x505.png 768w\" sizes=\"auto, (max-width: 1082px) 100vw, 1082px\" \/><\/p>\n<h3>9. Strace command to print instruction pointer<\/h3>\n<p>Strace command with the option -i prints the instruction pointer. In this example, a simple Linux command pwd is used for tracing.<\/p>\n<pre>$ strace -i pwd<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1092\" height=\"709\" class=\"wp-image-9427\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-379.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-379.png 1092w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-379-300x195.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-379-1024x665.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-379-768x499.png 768w\" sizes=\"auto, (max-width: 1092px) 100vw, 1092px\" \/><\/p>\n<h3>10. Strace command to trace system calls based on specific condition<\/h3>\n<p>Strace command can be used to trace system calls based on specific conditions such as memory, process, CPU, etc. In this example, system calls related to memory management are being traced for a simple Linux command pwd.<\/p>\n<p>$ strace -q -e memory pwd<\/p>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1032\" height=\"314\" class=\"wp-image-9428\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-380.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-380.png 1032w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-380-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-380-1024x312.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-380-768x234.png 768w\" sizes=\"auto, (max-width: 1032px) 100vw, 1032px\" \/><\/p>\n<h3>11. Strace command to trace signal related system calls<\/h3>\n<p>Signal-related system calls can be traced by defining the trace type in the command. In this example <strong>nc -v -n localhost 80<\/strong> is used for tracing system calls related to the signal.<\/p>\n<pre>$ strace -e trace=signal nc -v -n 127.0.0.1 80<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1352\" height=\"157\" class=\"wp-image-9429\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-381.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-381.png 1352w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-381-300x35.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-381-1024x119.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-381-768x89.png 768w\" sizes=\"auto, (max-width: 1352px) 100vw, 1352px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned what is strace utility tool and when It can be used. Also, I have covered some important strace commands to troubleshoot and debug system calls and processes.<\/p>","protected":false},"excerpt":{"rendered":"<p>Strace is a very powerful debugging command-line utility tool which helps to troubleshoot the issues by monitoring the system calls and signals of the specific program. In case&hellip;<\/p>","protected":false},"author":1,"featured_media":9445,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[493],"class_list":["post-9418","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-strace-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9418","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=9418"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9418\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/9445"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=9418"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=9418"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=9418"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}