1 Star2 Stars3 Stars4 Stars5 Stars (还没有评分)

DTrace tricks and tips (3) – 访问程序全局变量



#include <stdio.h>
int global_var;

void change_var(){

int main(void){
    return 0;

(1)用printf打印变量地址,这个最简单:“printf(”%p\n”, &global_var);”,但是这需要执行代码一次。

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 <http://gnu.org/licenses/gpl.html>
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 <global_var>


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
        this->global_var = (int32*)copyin(0x8060e78, sizeof(int32));
        printf("global_var = %d\n", *this->global_var);


./check.d -c ./a
global_var = 1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.