【问题标题】:awatch does not stop on specified addressawatch 不会在指定地址停止
【发布时间】:2015-10-21 15:28:40
【问题描述】:

我了解 GDB 中的 awatch 命令在读/写表达式时会中断。但是,为什么当我将 awatch 命令设置为结构指针的地址时,它只停止了一次。下面是我的 GDB 和代码 sn-p 的屏幕截图。请指教。谢谢。

测试.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;

测试.h

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

【问题讨论】:

  • 你已经取消了指针,所以可能是 0x602010。或者,awatch -l 0x602010。最简单的方法可能是 awatch -l *college
  • awatch -l *college 说“表达式中有语法错误,靠近 {...} *) 0x0000000000602010'." Also, awatch 0x602010 or awatch -l 0x602010 says "Cannot watch constant value 0x602010'。”

标签: c linux ubuntu gdb watch


【解决方案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

【讨论】:

  • 感谢您提供这个示例程序。
  • @Marss 不客气。每当我需要测试我从未使用过的功能(例如语言构造、调试 cmd、库调用等)时,我都会这样做。创建一个做一件事的小型测试/诊断程序。我创建了什么 保证 来引用 glob。在您的程序中,您有两个问题:(1)awatch 有效吗? (2)程序实际上是否引用了内存?测试程序只有 (1) 可以抗衡(按设计)。我肮脏的小秘密:即使在之后,我也不得不尝试 几个 [未显示 ;-)] 的 awatch 变体才能找到 一个。
猜你喜欢
  • 2016-03-24
  • 1970-01-01
  • 2012-04-02
  • 1970-01-01
  • 1970-01-01
  • 2018-05-02
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多