0

我想在静态本地设置一个观察点s_db

static sqlite3*& GetSqliteDb()
{
    static std::once_flag flag;
    static sqlite3* s_db = NULL;

    std::call_once(flag, []() {
        s_db = ...
        ...
    });
}

我试过用 设置观察点file::function::name,但它给我带来了麻烦:

$ gdb
GNU gdb (GDB) 7.6.2
Copyright (C) 2013 Free Software Foundation, Inc.
...
(gdb) file ./bin/Debug/ac-test 
Reading symbols from ...
(gdb) watch ac-sqlite.cpp::GetSqliteDb::s_db
No symbol "ac" in current context.
(gdb) watch "ac-sqlite.cpp::GetSqliteDb::s_db"
Cannot watch constant value `"ac-sqlite.cpp::GetSqliteDb::s_db"'.
(gdb) watch GetSqliteDb()::s_db
A syntax error in expression, near `s_db'.
(gdb) watch GetSqliteDb::s_db
No symbol "s_db" in specified context.
(gdb) watch s_db
No symbol "s_db" in current context.
(gdb) 

为了完整起见,我使用 构建-O0 -g3,因此甚至可以使用符号常量名称。

如何为静态本地上的写入设置观察点s_db

4

2 回答 2

1

似乎如果我设置一个带有损坏的 C++ 名称的观察点,那么一切都很好。如果我设置一个带有解构 C++ 名称的观察点,那么 gdb 在函数中找不到静态变量。这是我在 Windows 上使用 gcc 和 gdb 测试的示例:

void GetSqliteDb()
{
  static int *s_db = 0;
  s_db = new int;
  *s_db = 1;
  *s_db = 2;
   s_db = 0;
}

int main()
{
   GetSqliteDb();
   return 0;
}

这是 nm 输出:

>nm a.exe | grep s_db
00405020 b __ZZ11GetSqliteDbvE4s_db

>nm a.exe | grep s_db | c++filt
00405020 b GetSqliteDb()::s_db

这是一个 gdb 会话:

>gdb -q a.exe
Reading symbols from a.exe...done.
(gdb) watch GetSqliteDb::s_db
No symbol "s_db" in specified context.
(gdb) watch _ZZ11GetSqliteDbvE4s_db
Hardware watchpoint 1: _ZZ11GetSqliteDbvE4s_db
(gdb) start
Temporary breakpoint 2 at 0x4013cb: file main.cpp, line 11.
Starting program: a.exe
[New Thread 1688.0xff8]

(gdb) info watchpoints
Num     Type           Disp Enb Address    What
1       hw watchpoint  keep y              _ZZ11GetSqliteDbvE4s_db

Temporary breakpoint 2, main () at main.cpp:11
11      {
(gdb) c
Continuing.
Hardware watchpoint 1: _ZZ11GetSqliteDbvE4s_db

Old value = 0
New value = 4007328
GetSqliteDb () at main.cpp:5
5         *s_db = 1;
(gdb) c
Continuing.
Hardware watchpoint 1: _ZZ11GetSqliteDbvE4s_db

Old value = 4007328
New value = 0
GetSqliteDb () at main.cpp:8
8       }
(gdb) c
Continuing.
[Inferior 1 (process 1688) exited normally]

更新


我使用 gdb 7.6.2 在 RHEL 6.3 (2.6.32-279.el6.x86_64) 上测试了相同的示例,它正确设置了观察点。所以我无法在这个平台上重现这个问题:

>gdb a.out
GNU gdb (GDB) 7.6.2
Copyright (C) 2013 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-unknown-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/a.out...done.
(gdb) watch GetSqliteDb::s_db
Hardware watchpoint 1: GetSqliteDb::s_db
(gdb) r
Starting program: /home/a.out
Hardware watchpoint 1: GetSqliteDb::s_db

Old value = (int *) 0x0
New value = (int *) 0x601010
GetSqliteDb () at main.cpp:5
5         *s_db = 1;
(gdb) c
Continuing.
Hardware watchpoint 1: GetSqliteDb::s_db

Old value = (int *) 0x601010
New value = (int *) 0x0
GetSqliteDb () at main.cpp:8
8       }
(gdb)
于 2014-01-22T19:51:49.533 回答
1

看来您需要s_db使用运算符取消引用变量的地址*
请参阅文档中的设置观察点。这个观察点应该工作:

(gdb) watch *("ac-sqlite.cpp::GetSqliteDb::s_db")
于 2014-01-22T20:04:58.763 回答