本文将记录strace相关笔记。

Introduction

strace is a diagnostic, debugging and instructional userspace utility for Linux. It is used to monitor and tamper with interactions between processes and the Linux kernel, which include system calls, signal deliveries, and changes of process state.

System administrators, diagnosticians and trouble-shooters will find it invaluable for solving problems with programs for which the source is not readily available since they do not need to be recompiled in order to trace them.

The operation of strace is made possible by the kernel feature known as ptrace.

strace解决的问题都是关于寻找程序依赖的文件、找出程序卡住或慢的原因、或者找出程序失败的原因。

资料

man strace
Strace little book

Example

  • Print stack trace of every system call
    -k option is used to print stack trace of every system call.
    Print stack trace of every system call

  • -f跟踪目标进程,以及目标进程创建的所有子进程

  • -t 在输出中的每一行前加上时间信息(-tt 表示微秒级)

  • -T 显示每个系统调用所耗的时间

  • -o filename: Write the trace output to the file filename rather than to stderr.

  • -c Count time, calls, and errors for each system call and report a summary on program exit, suppressing the regular output.

Case

原理


参考资料:

  1. Debug 利器:pstack & strace
  2. Linux strace、pstack 命令 使用详解
  3. 深入理解 Linux 内核–jemalloc 引起的 TLB shootdown 及优化