7个Debug linux程序的Strace 列子

news/2024/7/7 18:55:48

Strace是一个能帮助你解决问题的debugging工具

Strace监控指定程序系统调用和信号,在你没有源代码又想dubug程序的执行时是会用到的。Strace会以程序的开始到结束来顺序执行的

你可以从这个7个Strace 例子开始起步了解Strace

 1.跟踪可执行程序的执行

你可以使用strace命令的跟踪任何可执行程序,接下来的例子展示linux ls命令的的strace输出

[tony@oc4443573018 python]$ strace ls
execve("/bin/ls", ["ls"], [/* 52 vars */]) = 0
brk(0)                                  = 0x2172000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f5c88a90000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=99825, ...}) = 0
mmap(NULL, 99825, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f5c88a77000
close(3)                                = 0
open("/lib64/libselinux.so.1", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320X\340\366=\0\0\0"..., 832) = 832
fstat(3, {st_mode=S_IFREG|0755, st_size=124624, ...}) = 0
mmap(0x3df6e00000, 2221912, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3df6e00000
mprotect(0x3df6e1d000, 2093056, PROT_NONE) = 0
mmap(0x3df701c000, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x1c000) = 0x3df701c000
mmap(0x3df701e000, 1880, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x3df701e000
close(3)                                = 0
open("/lib64/librt.so.1", O_RDONLY)     = 3.....
.....

2.使用option -e参数来追踪执行指定程序的系统调用

默认情况下,strace展示指定可执行程序的所有系统调用,使用strace -e选项只输出指定系统调用

[tony@oc4443573018 Videos]$ strace -e open ls
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib64/libselinux.so.1", O_RDONLY) = 3
open("/lib64/librt.so.1", O_RDONLY)     = 3
open("/lib64/libcap.so.2", O_RDONLY)    = 3
open("/lib64/libacl.so.1", O_RDONLY)    = 3
open("/lib64/libc.so.6", O_RDONLY)      = 3
open("/lib64/libdl.so.2", O_RDONLY)     = 3
open("/lib64/libpthread.so.0", O_RDONLY) = 3
open("/lib64/libattr.so.1", O_RDONLY)   = 3
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
open(".", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
Webcam
[tony@oc4443573018 Videos]$ 

The above output displays only the open system call of the ls command. At the end of the strace output, it also displays the output of the ls command.

上面只输出指定ls命令的系统调用,在strace输出的最后面同时也输出了ls命令的结果

If you want to trace multiple system calls use the “-e trace=” option. The following example displays both open and read system calls.

如果想trace多个系统调用,可以使用"-e strace="选项,下面展示了open和read命令的系统调用

[tony@oc4443573018 ~]$ strace -e trace=open,read ls /home
open("/etc/ld.so.cache", O_RDONLY)      = 3
open("/lib64/libselinux.so.1", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320X\340\366=\0\0\0"..., 832) = 832
open("/lib64/librt.so.1", O_RDONLY)     = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0@! \366=\0\0\0"..., 832) = 832
open("/lib64/libcap.so.2", O_RDONLY)    = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0000\23`\371=\0\0\0"..., 832) = 832
open("/lib64/libacl.so.1", O_RDONLY)    = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\36`\2>\0\0\0"..., 832) = 832
open("/lib64/libc.so.6", O_RDONLY)      = 3
read(3, "\177ELF\2\1\1\3\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0p\356!\365=\0\0\0"..., 832) = 832
open("/lib64/libdl.so.2", O_RDONLY)     = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340\r\340\365=\0\0\0"..., 832) = 832
open("/lib64/libpthread.so.0", O_RDONLY) = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\340]\240\365=\0\0\0"..., 832) = 832
open("/lib64/libattr.so.1", O_RDONLY)   = 3
read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\200\23 \6>\0\0\0"..., 832) = 832
open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
open("/home", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
interpel  tonyb
[tony@oc4443573018 ~]$ 
View Code

3.使用参数-o 保存strace结果

The following examples stores the strace output to output.txt file.

下面的列子展示strace命令输出的output.txt文件

[tonyboob@oc4443573018 ~]$ strace -o output.txt ls
A                    output.txt
a_b.py                    packages
a.py                    pexpect_test.py
B                    Pictures
bianli.py                Public
C                    putty.log
chefdk-0.10.0-1.el6.x86_64.rpm        python
CROSS_TEAM                Recycle Bin
derby.log                redis
Desktop                    redis_daemon.sh
Documents                rpmbuild
Downloads                SametimeRooms
。。。。。。。。
。。。。。。。。。。
[tony@oc4443573018 ~]$ cat output.txt 
execve("/bin/ls", ["ls"], [/* 53 vars */]) = 0
brk(0)                                  = 0x1dd9000
mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f75f6d17000
access("/etc/ld.so.preload", R_OK)      = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=99825, ...}) = 0
mmap(NULL, 99825, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7f75f6cfe000
close(3)                                = 0
open("/lib64/libselinux.so.1", O_RDONLY) = 3
View Code

4.使用-p 参数来strace一个运行中进程

You could execute strace on a program that is already running using the process id. First, identify the PID of a program using ps command.

可以使用已运行程序PID来strace一个程序,但首先你的使用ps命令来确定PID

For example, if you want to do strace on the firefox program that is currently running, identify the PID of the firefox program.

比如,你想strace运行中的Firefox,先确定Firefox的PID

[tony@oc4443573018 ~]$ ps -C firefoxPID TTY          TIME CMD6970 ?        06:02:51 firefox
[tony@oc4443573018 ~]$ 
View Code

使用-p选项来展示指定PID输出

[tony@oc4443573018 ~]$ strace -p 6970 -o firefox_strace.txt
Process 6970 attached - interrupt to quit^CProcess 6970 detached
[tony@oc4443573018 ~]$ tail -f firefox_strace.txt 
read(28, "\372", 1)   
View Code

firefox_trace中将记录firefox进程的trace记录,可以使用tail 命令的动态的观察firefox的输出,不过这是需要在另一个terminal来tail -f的

Strace会显示下面的错误,当你的user id不匹配给定的进程的UID

strace -p 1725 -o output.txt
attach: ptrace(PTRACE_ATTACH, ...): Operation not permitted
Could not attach to process.  If your uid matches the uid of the target
process, check the setting of /proc/sys/kernel/yama/ptrace_scope, or try
again as the root user.  For more details, see /etc/sysctl.d/10-ptrace.conf
View Code

5.使用-t参数为每行trace输出打印时间戳

tony@oc4443573018 ~]$ strace -t -e open ls /home
10:43:25 open("/etc/ld.so.cache", O_RDONLY) = 3
10:43:25 open("/lib64/libselinux.so.1", O_RDONLY) = 3
10:43:25 open("/lib64/librt.so.1", O_RDONLY) = 3
10:43:25 open("/lib64/libcap.so.2", O_RDONLY) = 3
10:43:25 open("/lib64/libacl.so.1", O_RDONLY) = 3
10:43:25 open("/lib64/libc.so.6", O_RDONLY) = 3
10:43:25 open("/lib64/libdl.so.2", O_RDONLY) = 3
10:43:25 open("/lib64/libpthread.so.0", O_RDONLY) = 3
10:43:25 open("/lib64/libattr.so.1", O_RDONLY) = 3
10:43:25 open("/usr/lib/locale/locale-archive", O_RDONLY) = 3
10:43:25 open("/home", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
interpel  tony
View Code

6.使用-r参数打印相关系统调用次数

[tony@oc4443573018 ~]$ strace -r ls0.000000 execve("/bin/ls", ["ls"], [/* 53 vars */]) = 00.000653 brk(0)                    = 0x1a0b0000.000141 mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7fdecabb80000.000163 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)0.000423 open("/etc/ld.so.cache", O_RDONLY) = 30.000895 fstat(3, {st_mode=S_IFREG|0644, st_size=99825, ...}) = 00.000136 mmap(NULL, 99825, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7fdecab9f0000.000090 close(3)                  = 00.000111 open("/lib64/libselinux.so.1", O_RDONLY) = 30.000816 read(3, "\177ELF\2\1\1\0\0\0\0\0\0\0\0\0\3\0>\0\1\0\0\0\320X\340\366=\0\0\0"..., 832) = 8320.000089 fstat(3, {st_mode=S_IFREG|0755, st_size=124624, ...}) = 00.000115 mmap(0x3df6e00000, 2221912, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x3df6e00000。。。。。。。。。。。。。。。。。
。。。。。。。。。。。。。。。。。
View Code

7.使用-c参数来生成系统调用的统计报告

使用-c选项会提供有用的执行追踪统计报告,下面输出中calls列表明特定的系统被执行了多少次

[tony@oc4443573018 python]$ strace -c ls /home
interpel  tony
% time     seconds  usecs/call     calls    errors syscall
------ ----------- ----------- --------- --------- ----------------98.97    0.000671          61        11           open1.03    0.000007           0        27           mmap0.00    0.000000           0         8           read0.00    0.000000           0         1           write0.00    0.000000           0        13           close0.00    0.000000           0         2           stat0.00    0.000000           0        11           fstat0.00    0.000000           0        16           mprotect0.00    0.000000           0         2           munmap0.00    0.000000           0         3           brk0.00    0.000000           0         2           rt_sigaction0.00    0.000000           0         1           rt_sigprocmask0.00    0.000000           0         2           ioctl0.00    0.000000           0         1         1 access0.00    0.000000           0         1           execve0.00    0.000000           0         1           fcntl0.00    0.000000           0         2           getdents0.00    0.000000           0         1           getrlimit0.00    0.000000           0         2           statfs
View Code

 

转载于:https://www.cnblogs.com/myland/p/5209179.html


http://lihuaxi.xjx100.cn/news/285914.html

相关文章

第2关:利用栈判断字符串括号是否匹配

#ifndef stack__h #define stack__h#include <stdio.h> #include <stdlib.h>typedef char T; // 数据元素的数据类型struct Stack{T* data; // 数据元素存储空间的开始地址int top; // 栈表的当前位置int max; // 栈表的最大长度 };Stack* Stack_Create(int …

Codeforces Round #645 (Div. 2)(D.The Best Vacation)

题目链接&#xff1a;https://codeforces.com/contest/1358/problem/D 思路&#xff1a;双指针前缀和 前缀和主要处理了两组数据&#xff1a;sum[]是某月到某月的天数,ans[] 代表某月到某月能得到得hug数 然后我们通过双指针的方法来遍历整个范围&#xff0c;当sum[r]-sum[l-1]…

如何用最强模型BERT做NLP迁移学习?

作者 | 台湾大学网红教授李宏毅的三名爱徒来源 | 井森堡&#xff0c;不定期更新机器学习技术文并附上质量佳且可读性高的代码。编辑 | Jane谷歌此前发布的NLP模型BERT&#xff0c;在知乎、Reddit上都引起了轰动。其模型效果极好&#xff0c;BERT论文的作者在论文里做的几个实验…

第3关:利用栈判断字符串是否为回文串

#ifndef stack__h #define stack__h#include <stdio.h> #include <stdlib.h>typedef char T; // 数据元素的数据类型struct Stack{T* data; // 数据元素存储空间的开始地址int top; // 栈表的当前位置int max; // 栈表的最大长度 };Stack* Stack_Create(int …

并查集 ---- 扩展域并查集判二分图 + 循环模拟字典树 The 2020 ICPC Asia Macau Regional Contest C. Club Assignment (详解)

题目链接 题目大意&#xff1a; 有n个数&#xff0c;现在要把他们拆分成两个集合&#xff0c;假设S为集合&#xff0c;有如下定义&#xff1a; f(S){min(x⊕y)∣x,y∈S,andx!y}f(S)\{min(x\oplus y)|x,y\in S,and\;x!y\}f(S){min(x⊕y)∣x,y∈S,andx!y} 将n个数拆分为两个集合…

实现Date函数属性中的format方法

js中没有Date.format方法的&#xff0c;所以在date属性中加format方法 //js格式化属性 Date.prototype.format function (format) {   var o {     "M": this.getMonth() 1, //month     "d": this.getDate(), //day     "h": …

网易开源支持图像识别的自动化UI测试工具,零基础亲测好评!

编辑 | Jane出品 | AI科技大本营AI科技大本营给大家推荐了很多有意思、适合开发者们的工具&#xff0c;比如代码修复神器、帮小白快速分析 Error、PDF 翻译工具、变量命名神器等等。今天&#xff0c;营长要专门给测试人员&#xff0c;或者想做测试的小伙伴们推荐一款工具&#…

[CQOI2009]中位数图 详细题解

题目链接&#xff1a; https://ac.nowcoder.com/acm/problem/19913 题目描述&#xff1a; 给出1~n的一个排列&#xff0c;统计该排列有多少个长度为奇数的连续子序列的中位数是b。中位数是指把所有元素从小到大排列后&#xff0c;位于中间的数。 题解&#xff1a; 因为中位数是…