我在程序中调用了“getpid()”多次(以测试系统调用的效率),但是当我使用strace获取跟踪时,只捕获到一个getpid调用。为什么使用strace只能捕获一次系统调用(getpid)?
代码很简单:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
void print_usage(){
printf("Usage: program count\n");
exit(-1);
}
int main(int argc, char** argv){
if(argc != 2)
print_usage();
int cnt = atoi(argv[1]);
int i = 0;
while(i++<cnt)
getpid();
return 0;
}
我用gdb,并得到这个:
(gdb) disasse
Dump of assembler code for function getpid:
0xb76faac0 <getpid+0>: mov %gs:0x4c,%edx
0xb76faac7 <getpid+7>: cmp $0x0,%edx
0xb76faaca <getpid+10>: mov %edx,%eax
0xb76faacc <getpid+12>: jle 0xb76faad0 <getpid+16>
0xb76faace <getpid+14>: repz ret
0xb76faad0 <getpid+16>: jne 0xb76faadc <getpid+28>
0xb76faad2 <getpid+18>: mov %gs:0x48,%eax
0xb76faad8 <getpid+24>: test %eax,%eax
0xb76faada <getpid+26>: jne 0xb76faace <getpid+14>
0xb76faadc <getpid+28>: mov $0x14,%eax
0xb76faae1 <getpid+33>: call *%gs:0x10
0xb76faae8 <getpid+40>: test %edx,%edx
0xb76faaea <getpid+42>: mov %eax,%ecx
0xb76faaec <getpid+44>: jne 0xb76faace <getpid+14>
0xb76faaee <getpid+46>: mov %ecx,%gs:0x48
0xb76faaf5 <getpid+53>: ret
我不太懂汇编代码。如果有人能够对此进行一些详细的解释,那也是有帮助的。根据我的观察,除了第一个getpid()调用之外,“调用*%gs:0x10”(跳转到vdso)不会执行,这可能是其他getpid调用未捕获的原因。但我不知道为什么。
Linux内核:2.6.24-29 海合会(GCC)4.2.4 的libc 2.7,
谢谢!
太好了。我还有一个问题;它可能不相关。我想知道gettimeofday是否通过利用vdso来实现第一个getpid()调用不会触发用户内核模式切换? – Infinite 2011-04-23 18:59:08
另一个问题是如何使用gdb单步进入“call *%gs:0x10”? – Infinite 2011-04-23 19:00:42
我不确定这有一个单一的答案。根据平台不同,系统调用的处理方式也不同(即使32位x86和64位x86_64也有不同的系统调用机制)。但也许我错了 - 你应该为此发布一个单独的问题,并确定你感兴趣的体系结构,以及如果你有一些特别感兴趣的系统调用。根本不了解gdb) – Mat 2011-04-23 19:02:14