awatch不会停在指定地址

时间:2015-10-21 05:30:07

标签: c linux ubuntu gdb watch

据我所知,当读取/写入expresssion时,GDB中的awatch命令会中断。但是,为什么当我将awatch命令设置为我的结构指针的地址时,它只停止一次。下面是我的GDB和代码片段的屏幕截图。请指教。谢谢。

enter image description here

Testing.c

College_Record *college = NULL;
college = malloc(sizeof(College_Record));
printf("college %p\n", college);
free(college);
printf("college %p\n", college);
college = NULL;
printf("college %p\n", college);

printf("***************************\n");
printf("\tDONE OKAY\n");
printf("***************************\n");

return 0;

Testing.h

  typedef struct {
    int college_id;
    char school[20];
} College_Record;

1 个答案:

答案 0 :(得分:1)

这是一个示例程序:

struct college {
    int x;
    int y;
};

struct college college_glob;

struct college *college;

void
setwatch(void)
{

    college = &college_glob;
}

void
brkgdb(void)
{
}

int
main(void)
{

    setwatch();
    brkgdb();

    return college_glob.x;
}

这是gdb会话:

> gdb /tmp/watch
GNU gdb (GDB) Fedora 7.9.1-19.fc22
Copyright (C) 2015 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-redhat-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from /tmp/watch...done.
(gdb) b brkgdb
Breakpoint 1 at 0x40050c: file /tmp/watch.c, line 20.
(gdb) run
Starting program: /tmp/watch 

Breakpoint 1, brkgdb () at /tmp/watch.c:20
20  }
(gdb) awatch *college
Hardware access (read/write) watchpoint 2: *college
(gdb) c
Continuing.
Hardware access (read/write) watchpoint 2: *college

Value = {x = 0, y = 0}
Hardware access (read/write) watchpoint 2: *college

Value = {x = 0, y = 0}
main () at /tmp/watch.c:30
30  }
(gdb) list
25  
26      setwatch();
27      brkgdb();
28  
29      return college_glob.x;
30  }
(gdb) q
A debugging session is active.

    Inferior 1 [process 15761] will be killed.

Quit anyway? (y or n) y