strace是一个集诊断、调试、统计与一体的工具,我们可以使用strace对应用的系统调用和信号传递的跟踪结果来对应用进行分析,以达到解决问题或者是了解应用工作过程的目的。
在软件部署中,我们经常会遇到各种各样的问题,比如程序运行速度慢、内存泄漏、死锁等,这些问题往往是由于程序在运行过程中出现了异常,导致程序无法正常执行,为了定位这些问题,我们需要对程序进行调试,而strace就是一个非常实用的工具,它可以帮助我们跟踪程序的系统调用和信号,从而找到问题的根源,本文将详细介绍如何在软件部署中使用strace进行调试。

创新互联长期为上1000+客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为永川企业提供专业的成都网站设计、做网站,永川网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
什么是strace?
strace(System Trap)是一个Linux下的系统调用跟踪工具,它可以记录进程运行时系统调用的发生情况,以便我们分析程序的行为,通过strace,我们可以了解程序在运行过程中到底做了哪些系统调用,从而找到程序的问题所在。
strace的基本用法
1、跟踪指定进程
要使用strace跟踪指定进程,我们可以使用以下命令:
strace -p [options]
是目标进程的进程ID,[options]是可选的参数,要跟踪进程ID为1234的进程,我们可以使用以下命令:
strace -p 1234 [options]
2、输出系统调用信息到文件
我们还可以将strace的输出信息保存到文件中,以便后续分析,要实现这一点,我们可以在命令行中添加-o参数,指定输出文件名,将输出信息保存到名为output.txt的文件中,我们可以使用以下命令:
strace -p 1234 -o output.txt [options]
3、显示实时输出
默认情况下,strace会将输出信息缓存起来,直到所有系统调用都被记录下来,如果我们希望实时查看输出信息,可以使用-f参数。
strace -p 1234 -f [options]
strace的应用场景
1、性能分析
通过strace,我们可以了解到程序在运行过程中到底做了哪些系统调用,从而找到程序的性能瓶颈,我们可以通过strace来查看程序在IO操作、网络通信等方面的耗时情况。
2、调试内核模块
对于内核模块开发人员来说,strace是一个非常有用的工具,通过strace,我们可以跟踪内核模块中的系统调用和信号,从而找到程序的问题所在,strace还可以帮助我们了解内核模块在运行过程中的资源消耗情况。
3、跟踪用户态程序
虽然我们在开发应用程序时通常会使用调试器(如gdb)来调试程序,但有时候我们还需要在不使用调试器的情况下对程序进行调试,这时,strace就可以发挥作用了,通过strace,我们可以跟踪用户态程序的系统调用和信号,从而找到程序的问题所在。
相关问题与解答
1、如何使用strace查看某个函数被调用的情况?
答:要查看某个函数被调用的情况,我们可以在命令行中添加-e trace=functions:参数,要查看函数foo被调用的情况,我们可以使用以下命令:
strace -p -e trace=functions:foo [options]
2、如何使用strace查看某个符号对应的函数?
答:要查看某个符号对应的函数,我们可以在命令行中添加-O参数,要查看符号foo对应的函数,我们可以使用以下命令:
nm -D --defined-only | grep foo | cut -d ' ' -f 1 | xargs readelf -WsF | grep foo | cut -d ' ' -f 5 | xargs pdb python -m pdb
基本
文件
流程
错误
SQL
调试
- 请求信息 : 2026-02-16 15:36:30 HTTP/1.1 GET : /article/cdegpej.html
- 运行时间 : 0.0976s ( Load:0.0047s Init:0.0303s Exec:0.0583s Template:0.0043s )
- 吞吐率 : 10.25req/s
- 内存开销 : 2,217.13 kb
- 查询信息 : 12 queries 5 writes
- 文件加载 : 36
- 缓存信息 : 0 gets 2 writes
- 配置加载 : 130
- 会话信息 : SESSION_ID=g1nm2n0dr16an0f8u6tucts5k5
- /home/wwwroot/jxjierui.cn/index.php ( 1.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/ThinkPHP.php ( 4.61 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Think.class.php ( 12.26 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage.class.php ( 1.37 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Storage/Driver/File.class.php ( 3.52 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Mode/common.php ( 2.82 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Common/functions.php ( 53.56 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Hook.class.php ( 4.01 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/App.class.php ( 13.49 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Dispatcher.class.php ( 14.79 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Route.class.php ( 13.36 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Controller.class.php ( 11.23 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/View.class.php ( 7.59 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/BuildLiteBehavior.class.php ( 3.68 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ParseTemplateBehavior.class.php ( 3.88 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ContentReplaceBehavior.class.php ( 1.91 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/convention.php ( 11.15 KB )
- /home/wwwroot/jxjierui.cn/App/Common/Conf/config.php ( 2.12 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Lang/zh-cn.php ( 2.55 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Conf/debug.php ( 1.48 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Conf/config.php ( 0.32 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Common/function.php ( 3.33 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ReadHtmlCacheBehavior.class.php ( 5.62 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php ( 6.11 KB )
- /home/wwwroot/jxjierui.cn/App/Home/Controller/CommController.class.php ( 1.60 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Model.class.php ( 60.11 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php ( 32.43 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db/Driver/Pdo.class.php ( 16.74 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache.class.php ( 3.83 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php ( 5.87 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template.class.php ( 28.16 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib/Cx.class.php ( 22.40 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Template/TagLib.class.php ( 9.16 KB )
- /home/wwwroot/jxjierui.cn/App/Runtime/Cache/Home/7540f392f42b28b481b30614275e4e55.php ( 13.96 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/WriteHtmlCacheBehavior.class.php ( 0.97 KB )
- /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Behavior/ShowPageTraceBehavior.class.php ( 5.24 KB )
- [ app_init ] --START--
- Run Behavior\BuildLiteBehavior [ RunTime:0.000005s ]
- [ app_init ] --END-- [ RunTime:0.000029s ]
- [ app_begin ] --START--
- Run Behavior\ReadHtmlCacheBehavior [ RunTime:0.000188s ]
- [ app_begin ] --END-- [ RunTime:0.000216s ]
- [ view_parse ] --START--
- [ template_filter ] --START--
- Run Behavior\ContentReplaceBehavior [ RunTime:0.000063s ]
- [ template_filter ] --END-- [ RunTime:0.000080s ]
- Run Behavior\ParseTemplateBehavior [ RunTime:0.003389s ]
- [ view_parse ] --END-- [ RunTime:0.003404s ]
- [ view_filter ] --START--
- Run Behavior\WriteHtmlCacheBehavior [ RunTime:0.000060s ]
- [ view_filter ] --END-- [ RunTime:0.000069s ]
- [ app_end ] --START--
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`pid`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ') LIMIT 1' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( id= ) LIMIT 1
- 1064:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1
[ SQL语句 ] : SELECT `id`,`navname` FROM `cx_nav` WHERE ( pid= )
- [8] Undefined index: pid /home/wwwroot/jxjierui.cn/App/Home/Controller/ArticleController.class.php 第 47 行.
- [2] file_put_contents(./App/Runtime/Temp/d93595930d51473a70d38ea7356d1389.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.
- [8] Undefined index: db_host /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 120 行.
- [8] Undefined index: db_port /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 121 行.
- [8] Undefined index: db_name /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Db.class.php 第 122 行.
- [2] file_put_contents(./App/Runtime/Temp/e73e59d8f37fab90124e30a4bb5eb82a.php): failed to open stream: Permission denied /home/wwwroot/jxjierui.cn/ThinkPHP/Library/Think/Cache/Driver/File.class.php 第 132 行.

0.0976s
