在stackoverflow上有人問如何用DTrace訪問程序的全局變量(http://stackoverflow.com/questions/11228352/dtrace-accessing-global-variables-from-application),答案也簡單,只需知道全局變量的地址,然後把地址作爲指針,按照變量的類型,解引用指針即可。所以問題來了,如何得到全局變量地址?
還是以stackoverflow上問題的代碼做例子,並假設編譯後生成的可執行文件名爲a:
#include
int global_var;
void change_var(){
global_var++;
}
int main(void){
change_var();
return 0;
}
(2)用gdb調試程序,打印地址,同樣也要執行一次:
bash-3.2# gdb a
GNU gdb (GDB) 7.5
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-solaris2.10".
For bug reporting instructions, please see:
...
Reading symbols from /data1/nan/a...done.
(gdb) start
Temporary breakpoint 1 at 0x8050c12: file a.c, line 9.
Starting program: /data1/nan/a
[Thread debugging using libthread_db enabled]
[New Thread 1 (LWP 1)]
[Switching to Thread 1 (LWP 1)]
Temporary breakpoint 1, main () at a.c:9
9 change_var();
(gdb) p &gl
glcs glob global_gt global_var globfree globit
(gdb) p &global_var
$1 = (int *) 0x8060e78
elfdump a | grep global_var
[21] 0x08060e78 0x00000004 OBJT GLOB D 1 .bss global_var
[89] 0x08060e78 0x00000004 OBJT GLOB D 0 .bss global_var
18 [21] global_var
#!/usr/sbin/dtrace -qs
pid$target:a:change_var:return
{
this->global_var = (int32*)copyin(0x8060e78, sizeof(int32));
printf("global_var = %d\n", *this->global_var);
}
./check.d -c ./a
global_var = 1