- #include<stdio.h>
- #include<stdlib.h>
- #include<unistd.h>
- #include<sys/types.h>
- #include<sys/wait.h>
- #include<string.h>
- int g_var[102400] = {0};
- int main()
- {
- int l_var[102400] = {0};
- fprintf(stderr,"g_var 's address is %lx\n",(unsigned long)g_var);
- fprintf(stderr,"l_var 's address is %lx\n",(unsigned long)l_var);
- memset(g_var,0,sizeof(g_var));
- memset(l_var,0,sizeof(l_var));
- sleep(15);
- int ret = fork();
- if(ret < 0 )
- {
- fprintf(stderr,"fork failed ,nothing to do now!\n");
- return -1;
- }
- if(ret == 0)
- {
- sleep(10);
- fprintf(stderr, "I begin to write now\n");
- fprintf(stderr,"address at %-10lx value(%-6d) will cause page falut\n",
- (unsigned long)(g_var+2048),g_var[2048]);
- g_var[2048] = 4;
- sleep(6);
- fprintf(stderr,"address at %-10lx value(%-6d) will cause page fault\n",
- (unsigned long)(g_var+10240),g_var[10240]);
- g_var[10240] = 8;
- sleep(4);
- fprintf(stderr,"address at %-10lx value(%-6d) will cause page falut\n",
- (unsigned long)(l_var+2048),l_var[2048]);
- l_var[2048] = 8;
- sleep(4);
- fprintf(stderr,"address at %-10lx value(%-6d) will cause page falut\n",
- (unsigned long)(l_var+10240),l_var[10240]);
- l_var[10240] = 8;
-
- }
- if(ret >0)
- {
- waitpid(-1,NULL,0);
- fprintf(stderr,"child process exit, now check the value\n");
- fprintf(stderr,"g_var[%-6d] = %-4d\ng_var[%-6d] = %-4d\n",
- 2048,g_var[2048],10240,g_var[10240]);
- fprintf(stderr,"l_var[%-6d] = %-4d\nl_var[%-6d] = %-4d\n",
- 2048,l_var[2048],10240,l_var[10240]);
- return 0;
- }
- }
这里面执行了一个fork系统调用,我们调用下systemtap脚本看下他都调用了kernel/fork.c里面的那些函数:systemtap脚本如下:
- probe kernel.function("*@kernel/fork.c")
- {
- if(pid() == target())
- {
- printf("PID(%d) ,execname(%s) probe point:(%s) \n",pid(),execname(),pp());
- }
- }
- probe timer.s(60)
- {
- exit();
- }
- root@libin:~/program/systemtap/process# stap fork_call.stp -x 7192
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("do_fork@/build/buildd/linux-2.6.32/kernel/fork.c:1364"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_process@/build/buildd/linux-2.6.32/kernel/fork.c:978"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("dup_task_struct@/build/buildd/linux-2.6.32/kernel/fork.c:221"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("account_kernel_stack@/build/buildd/linux-2.6.32/kernel/fork.c:141"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("rt_mutex_init_task@/build/buildd/linux-2.6.32/kernel/fork.c:941"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_flags@/build/buildd/linux-2.6.32/kernel/fork.c:923"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("posix_cpu_timers_init@/build/buildd/linux-2.6.32/kernel/fork.c:960"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_files@/build/buildd/linux-2.6.32/kernel/fork.c:747"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_fs@/build/buildd/linux-2.6.32/kernel/fork.c:727"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_sighand@/build/buildd/linux-2.6.32/kernel/fork.c:799"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_signal@/build/buildd/linux-2.6.32/kernel/fork.c:854"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("posix_cpu_timers_init_group@/build/buildd/linux-2.6.32/kernel/fork.c:826"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_mm@/build/buildd/linux-2.6.32/kernel/fork.c:680"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("dup_mm@/build/buildd/linux-2.6.32/kernel/fork.c:624"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("mm_init@/build/buildd/linux-2.6.32/kernel/fork.c:448"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("mm_alloc_pgd@/build/buildd/linux-2.6.32/kernel/fork.c:403"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("mm_init_aio@/build/buildd/linux-2.6.32/kernel/fork.c:440"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("mm_init_owner@/build/buildd/linux-2.6.32/kernel/fork.c:951"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("dup_mmap@/build/buildd/linux-2.6.32/kernel/fork.c:278"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("copy_io@/build/buildd/linux-2.6.32/kernel/fork.c:774"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("__cleanup_sighand@/build/buildd/linux-2.6.32/kernel/fork.c:816"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("__cleanup_signal@/build/buildd/linux-2.6.32/kernel/fork.c:916"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("mm_release@/build/buildd/linux-2.6.32/kernel/fork.c:570"))
- PID(7192) ,execname(fork_cow) probe point:(kernel.function("mmput@/build/buildd/linux-2.6.32/kernel/fork.c:509"))
- if (clone_flags & CLONE_VM) {
- atomic_inc(&oldmm->mm_users);
- mm = oldmm;
- goto good_mm;
- }
- root@libin:~/program/C/process_share# ./pthread_cmp &
- [3] 7787
- root@libin:~/program/C/process_share# thread OUT
- thread IN
- thread OUT
- [2]- Done ./pthread_cmp
- [3]+ Done ./pthread_cmp
- root@libin:~/program/systemtap/process# stap fork_call.stp -x 7787
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("do_fork@/build/buildd/linux-2.6.32/kernel/fork.c:1364"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_process@/build/buildd/linux-2.6.32/kernel/fork.c:978"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("dup_task_struct@/build/buildd/linux-2.6.32/kernel/fork.c:221"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("account_kernel_stack@/build/buildd/linux-2.6.32/kernel/fork.c:141"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("rt_mutex_init_task@/build/buildd/linux-2.6.32/kernel/fork.c:941"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_flags@/build/buildd/linux-2.6.32/kernel/fork.c:923"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("posix_cpu_timers_init@/build/buildd/linux-2.6.32/kernel/fork.c:960"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_files@/build/buildd/linux-2.6.32/kernel/fork.c:747"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_fs@/build/buildd/linux-2.6.32/kernel/fork.c:727"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_sighand@/build/buildd/linux-2.6.32/kernel/fork.c:799"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_signal@/build/buildd/linux-2.6.32/kernel/fork.c:854"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_mm@/build/buildd/linux-2.6.32/kernel/fork.c:680"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("copy_io@/build/buildd/linux-2.6.32/kernel/fork.c:774"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("mm_release@/build/buildd/linux-2.6.32/kernel/fork.c:570"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("mmput@/build/buildd/linux-2.6.32/kernel/fork.c:509"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("__cleanup_sighand@/build/buildd/linux-2.6.32/kernel/fork.c:816"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("mm_release@/build/buildd/linux-2.6.32/kernel/fork.c:570"))
- PID(7787) ,execname(pthread_cmp) probe point:(kernel.function("mmput@/build/buildd/linux-2.6.32/kernel/fork.c:509"))
- #! /usr/bin/env stap
- global fault_entry_time, fault_address, fault_access
- global time_offset
- probe begin { time_offset = gettimeofday_us() }
- probe vm.pagefault {
- if(pid() == target() || ppid() == target())
- {
- t = gettimeofday_us()
- p = pid()
- fault_entry_time[p] = t
- fault_address[p] = address
- fault_access[p] = write_access ? "w" : "r"
- }
- }
-
- probe vm.pagefault.return {
- if(pid() == target() || ppid() == target())
- {
- t=gettimeofday_us()
- p = pid()
- if (!(p in fault_entry_time)) next
- e = t - fault_entry_time[p]
- if (vm_fault_contains(fault_type,VM_FAULT_MINOR)) {
- ftype="minor"
- } else if (vm_fault_contains(fault_type,VM_FAULT_MAJOR)) {
- ftype="major"
- } else {
- next #only want to deal with minor and major page faults
- }
- printf("%d:%d:%p:%s:%s:%d\n",
- t - time_offset, p, fault_address[p], fault_access[p], ftype, e)
-
- #free up memory
- delete fault_entry_time[p]
- delete fault_address[p]
- delete fault_access[p]
- }
- }
- probe timer.s(100){
- exit();
- }
- root@libin:~/program/C/process_share# g_var 's address is 804a060
- l_var 's address is bf8edf0c
- I begin to write now
- address at 804c060 value(0 ) will cause page falut
- address at 8054060 value(0 ) will cause page fault
- address at bf8eff0c value(0 ) will cause page falut
- address at bf8f7f0c value(0 ) will cause page falut
- .....
- root@libin:~/program/systemtap#
- root@libin:~/program/systemtap#
- root@libin:~/program/systemtap# stap pfaults.stp -x 9081
- 4767196:9081:0xb77ec72c:w:minor:35
- 4767230:9092:0xb77ec728:w:minor:23
- 4767239:9081:0xbf8edea8:w:minor:29
- .....
- 14768229:9092:0x0804c060:w:minor:13
- 20768379:9092:0x08054060:w:minor:37
- 24768564:9092:0xbf8eff0c:w:minor:39
- 28768745:9092:0xbf8f7f0c:w:minor:39
- ...