分类: LINUX
2016-04-13 09:44:20
一.Strace简介
用户态的Strace工具跟踪进程的系统调用,注意是只能跟踪系统调用,不能跟踪用户程序和库
内核也自带一个strace调试工具,内核的这个strace跟踪内核函数调用过程,与这个strace有区别。
二.Strace编译
以编译t1020的strace工具为例子
(1)下载源码,进入源码目录,运行configure进行配置
. ./configure
--prefix=/home/workspace/hbx/strace/strace-4.5.14
--host=powerpc-linux
--target=powerpc-linux
CC=/opt/ppc8540_gcc4.1.2_glibc2.5.0/bin/powerpc-8540-linux-gnu-gcc LD=/opt/ppc8540_gcc4.1.2_glibc2.5.0/bin/powerpc-8540-linux-gnu-ld
RANLIB=/opt/ ppc8540_gcc4.1.2_glibc2.5.0/bin/powerpc-8540-linux-gnu-ranlib
(2)静态编译
make CFLAGS+="-static"
(3)make install
静态编译的strace工具如下,下载到板子上即可运行调试
三.Strace 用法
1.Strace –p (进程pid)
显示进程系统调用
2.Strace –tt –p (进程pid)
显示进程系统调用,带时间
3.Strace系统调用统计:
# strace -c -p 3975 Process 3975 attached ^CProcess 3975 detached % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 42.13 0.108059 43 2530 1241 open 23.54 0.060374 45 1335 read 17.05 0.043741 35 1241 stat 11.09 0.028432 20 1420 close 2.90 0.007426 28 262 getdents 2.21 0.005660 43 131 openat 0.89 0.002284 48 48 write 0.07 0.000174 58 3 munmap 0.04 0.000103 103 1 brk 0.04 0.000097 97 1 poll 0.03 0.000072 24 3 mmap 0.01 0.000036 12 3 fstat 0.01 0.000015 15 1 ioctl ------ ----------- ----------- --------- --------- ---------------- 100.00 0.256473 6979 1241 total # |
4.加-T,统计系统调用耗时
# strace -T -p 3975 Process 3975 attached restart_syscall(<... resuming interrupted call ...>) = 0 <2.157076> //方括号内即调用耗时 ioctl(1, _IOC(_IOC_READ, 0x74, 0x68, 0x08), {ws_row=48, ws_col=177, ws_xpixel=0, ws_ypixel=0}) = 0 <0.000030> openat(AT_FDCWD, "/proc", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3 <0.000062> getdents(3, /* 226 entries */, 32768) = 5864 <0.001486> stat("/proc/1", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0 <0.000054> open("/proc/1/intr", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000060> open("/proc/1/stat", O_RDONLY) = 4 <0.000237> read(4, "1 (init) S 0 1 1 0 -1 4194560 22"..., 1023) = 302 <0.000063> close(4) = 0 <0.000037> openat(AT_FDCWD, "/proc/1/task", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 4 <0.000055> getdents(4, /* 3 entries */, 32768) = 72 <0.000034> stat("/proc/1/task/1", {st_mode=S_IFDIR|0555, st_size=0, ...}) = 0 <0.000104> open("/proc/1/task/1/intr", O_RDONLY) = -1 ENOENT (No such file or directory) <0.000058> open("/proc/1/task/1/stat", O_RDONLY) = 5 <0.000102> read(5, "1 (init) S 0 1 1 0 -1 4194560 22"..., 1023) = 302 <0.000085> |