使用 strace 与 ltrace 跟踪系统调用
通过跟踪进程发起的系统调用和库调用,进一步深入排查问题,准确找出系统停滞或失败的位置
使用 strace 与 ltrace 跟踪系统调用 是 CoddyKit 上的免费 Linux Command Line Mastery 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Linux Command Line Mastery 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Linux Command Line Mastery 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
When Metrics Are Not Enough
CPU, memory, and I/O tools tell you that something is slow. To learn why, you trace what a process actually asks the kernel to do. strace and ltrace open that window.
What strace Shows
strace records every system call: file opens, reads, network sends, and their return values, including errors.
strace ls /tmpAttaching to a Running Process
Use -p PID to trace something already running, such as a stuck service.
sudo strace -p 4567Filtering Specific Calls
-e trace= limits output to calls you care about, e.g. only file-related ones.
strace -e trace=openat,read,write cat /etc/hostnameFinding Missing Files
A classic use: see which file a program fails to open. Look for ENOENT (no such file) in the output.
strace -e trace=openat myapp 2>&1 | grep ENOENTSummarizing with -c
-c prints a profile: which syscalls consumed the most time and how often each was called.
strace -c ls -R /usr/shareTiming Calls
-T shows how long each syscall took, and -t adds timestamps, pinpointing where a process blocks.
strace -T -e trace=read myappFollowing Child Processes
-f follows forks so you trace child processes too, essential for servers that spawn workers.
strace -f -p 4567Saving Output to a File
Traces are noisy; -o writes them to a file you can grep later without cluttering the terminal.
strace -o trace.log -f myappltrace for Library Calls
While strace shows kernel calls, ltrace shows library calls like malloc or strcmp, useful for logic-level debugging.
ltrace ./myprogramA Troubleshooting Recipe
When a service hangs: attach with strace -f -p, watch for a syscall that never returns (often a blocked read or connect), and you have found the stall.
Quick Check
What kind of activity does strace primarily reveal?
Recap
You can now trace a process at the syscall level:
strace cmdor-p PIDto attach-e trace=filters,-cprofiles,-T/-ttime-ffollows children,-osaves outputltraceshows library calls
Watch for errors like ENOENT or a syscall that never returns.
用 AI 导师学习 Linux Command Line Mastery — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 12
- 课程
- 48
常见问题解答
「使用 strace 与 ltrace 跟踪系统调用」课时是免费的吗?
是的 — 「使用 strace 与 ltrace 跟踪系统调用」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Linux Command Line Mastery 课程的其余内容,请升级到 CoddyKit PRO。 Linux Command Line Mastery 课程共包含 4 节课。
「使用 strace 与 ltrace 跟踪系统调用」这节课中我会学到什么?
通过跟踪进程发起的系统调用和库调用,进一步深入排查问题,准确找出系统停滞或失败的位置 你通过在浏览器中直接运行的动手代码来练习 Linux Command Line Mastery,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Linux Command Line Mastery 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Linux Command Line Mastery 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。
「使用 strace 与 ltrace 跟踪系统调用」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Linux Command Line Mastery 课中编写并运行代码吗?
能。每节 Linux Command Line Mastery 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 磁盘输入输出监控:`iostat`、`iotop`
- 内存与 CPU 性能工具
- 高级日志记录与故障排查技术
- 使用 strace 与 ltrace 跟踪系统调用