应用调试(一)strace

编译

#tar -xjf strace-4.5.15.tar.bz2
#cd strace-4.5.15/
#patch -p1 <../strace-fix-arm-bad-syscall.patch
#./configure --host=arm-linux  CC=arm-linux-gcc         
#make 
复制到单板的 /usr/bin
#cp  strace  /usr/bin/ 

使用

#insmod dri.ko                 
# strace -o log.txt ./test /dev/xyz0
first_drv_open=0
first_drv_write=0
# 也可以直接跟踪模块
# strace  -o log.txt  rmmod dri

可以看到log里面有记录

....
close(3)                                = 0
open("/dev/xyz0", O_RDWR)               = 3
write(3, "\1\0\0\0", 4)                 = 0
exit_group(0)                           = ?
....

可以加入-t加入秒的时标,-tt为ms的时标3


04:13:18 execve("./test", ["./test", "/dev/xyz0"], [/* 7 vars */]) = 0
04:13:18 uname({sys="Linux", node="(none)", ...}) = 0
04:13:18 brk(0)                         = 0x11000
04:13:18 access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)
04:13:18 open("/etc/ld.so.cache", O_RDONLY) = -1 ENOENT (No such file or directory)
04:13:18 open("/lib/v4l/half/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
04:13:18 stat64("/lib/v4l/half", 0xbe83d4e0) = -1 ENOENT (No such file or directory)
04:13:18 open("/lib/v4l/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
04:13:18 stat64("/lib/v4l", 0xbe83d4e0) = -1 ENOENT (No such file or directory)
04:13:18 open("/lib/half/libc.so.6", O_RDONLY) = -1 ENOENT (No such file or directory)
04:13:18 stat64("/lib/half", 0xbe83d4e0) = -1 ENOENT (No such file or directory)
04:13:18 open("/lib/libc.so.6", O_RDONLY) = 3
04:13:18 read(3, "\177ELF\1\1\1a\0\0\0\0\0\0\0\0\3\0(\0\1\0\0\0\330o\1\000"..., 512) = 512
04:13:18 fstat64(3, {st_mode=S_IFREG|0755, st_size=1435660, ...}) = 0
04:13:18 mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40015000
04:13:18 mmap2(NULL, 1150612, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0x4001e000
04:13:18 mprotect(0x40129000, 56980, PROT_NONE) = 0
04:13:18 mmap2(0x40131000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x10b) = 0x40131000
04:13:18 mmap2(0x40135000, 7828, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x40135000
04:13:18 close(3)                       = 0
04:13:18 open("/dev/xyz0", O_RDWR)      = 3
04:13:18 write(3, "\1\0\0\0", 4)        = 0
04:13:18 exit_group(0)                  = ?

原理

starce是主进程,他去创建一个子进程去执行我们实际需要的应用程序,应用程序的open等C库的函数调用实际上会触发swi异常,在swi异常程序中,会判断是否是被跟踪的,如果有跟踪的标记,会先告诉父进程,等待父进程记录后再去通知我们实的App

# \arch\arm\kernel\entry-armv.S

/*
 * SVC mode handlers
 */
	.macro	svc_entry
....
#ifdef CONFIG_TRACE_IRQFLAGS
	bl	trace_hardirqs_off
#endif
...
	irq_handler
#ifdef CONFIG_TRACE_IRQFLAGS
	tst	r0, #PSR_I_BIT
	bleq	trace_hardirqs_on
#endif

深入文档

https://gitee.com/layty/Jz2440/tree/master/Driver/code/33th-strace/ptrace

posted @ 2019-01-17 11:56  zongzi10010  阅读(218)  评论(0编辑  收藏  举报