【问题标题】:Unable to source file from .gdbinit无法从 .gdbinit 获取文件
【发布时间】:2019-12-13 06:33:51
【问题描述】:

我有一个包含 GDB 断点的文件 ~/.gdb_bps。我用save breakpoints .gdb_bps 生成了这个文件。我试图在 GDB 启动时通过将此行添加到 ~/.gdbinit 来获取此文件:

source .gdb_bps

当我启动 GDB 时出现错误:

No symbol table is loaded.  Use the "file" command.
Make breakpoint pending on future shared library load? (y or [n]) [answered N; input not from terminal]

但是 GDB 会话期间的采购按预期工作:

(gdb) source .gdb_bps
Breakpoint 1 at 0x401227: file test/varargs2_test.c, line 22.
Breakpoint 2 at 0x4012a3: file test/varargs2_test.c, line 27.
Breakpoint 3 at 0x40117b: file test/varargs2_test.c, line 9.
Breakpoint 4 at 0x401256: file test/varargs2_test.c, line 25.

我的问题是为什么source .gdb_bps~/.gdbinit 中使用时会出错?

【问题讨论】:

    标签: c gdb breakpoints gdbinit


    【解决方案1】:

    ~/.gdbinit 在处理命令行中的目标文件之前运行。

    虽然需要进行一些设置,但将断点保存为 gdb 脚本(可以是 auto-loaded)可能是最简洁的方法。

    首先,创建一个目录来保存脚本,并将适当的行添加到~/.gdbinit

    ~/devel$ mkdir ~/gdbscripts
    ~/devel$ cat ~/.gdbinit
    add-auto-load-safe-path /home/mp/gdbscripts
    add-auto-load-scripts-directory /home/mp/gdbscripts
    set debug auto-load on
    

    我们将在一切正常后删除最后一行。

    现在让我们看看 gdb 在哪里寻找它的脚本文件。

    ~/devel$ gdb -q sigw
    Reading symbols from sigw...done.
    auto-load: Attempted file "/home/mp/devel/sigw-gdb.gdb" does not exist.
    auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load:/home/mp/gdbscripts".
    auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load:/home/mp/gdbscripts".
    auto-load: Attempted file "/usr/lib/debug/home/mp/devel/sigw-gdb.gdb" does not exist.
    auto-load: Attempted file "/usr/share/gdb/auto-load/home/mp/devel/sigw-gdb.gdb" does not exist.
    auto-load: Attempted file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" does not exist.
    auto-load: Attempted file "/home/mp/devel/sigw-gdb.py" does not exist.
    auto-load: Expanded $-variables to "/usr/lib/debug:/usr/share/gdb/auto-load:/home/mp/gdbscripts".
    auto-load: Searching 'set auto-load scripts-directory' path "$debugdir:$datadir/auto-load:/home/mp/gdbscripts".
    auto-load: Attempted file "/usr/lib/debug/home/mp/devel/sigw-gdb.py" does not exist.
    auto-load: Attempted file "/usr/share/gdb/auto-load/home/mp/devel/sigw-gdb.py" does not exist.
    auto-load: Attempted file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.py" does not exist.
    

    那里有几个不错的候选者,其中一些只能由 root 写入,其中一些需要添加到安全路径中。让我们使用~/gdbscripts/$PWD/sigw-gdb.gdb

    (gdb) shell mkdir -p ~/gdbscripts/$PWD
    (gdb) b main
    Breakpoint 1 at 0x84f: file sigw.c, line 15.
    (gdb) save breakpoints ~/gdbscripts/$PWD/sigw-gdb.gdb
    Unable to open file '/home/mp/gdbscripts/$PWD/sigw-gdb.gdb' for saving (No such file or directory)
    

    看起来save 命令扩展了~,但没有扩展环境变量。我们必须使用完整路径。

    (gdb) save breakpoints ~/gdbscripts/home/mp/devel/sigw-gdb.gdb
    Saved to file '/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb'.
    

    测试一下。

    (gdb) quit
    ~/devel$ gdb -q sigw
    Reading symbols from sigw...done.
    ...
    auto-load: Matching file "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" to pattern "/home/mp/gdbscripts"
    auto-load: Matched - file "/home/mp/gdbscripts" to pattern "/home/mp/gdbscripts".
    auto-load: File "/home/mp/gdbscripts/home/mp/devel/sigw-gdb.gdb" matches directory "/home/mp/gdbscripts".
    Breakpoint 1 at 0x84f: file sigw.c, line 15.
    ...
    (gdb) i b
    Num     Type           Disp Enb Address            What
    1       breakpoint     keep y   0x000000000000084f in main at sigw.c:15
    

    【讨论】:

      【解决方案2】:

      可以在处理 .gdbinit 后加载断点。这可以通过-x option 来完成:

      $ gdb -x .gdb_bps program
      Reading symbols from test/varargs2_test...
      Breakpoint 1 at 0x40117b: file test/varargs2_test.c, line 9.
      Breakpoint 2 at 0x4011b2: file test/varargs2_test.c, line 12.
      (gdb) 
      

      为避免键入选项,请定义 bash 函数:

      gdb() (
          [ -f .gdb_bps ] && xopt="-x .gdb_bps" || xopt=
          command gdb -q $@ $xopt
      )
      

      并像gdb program一样使用它

      【讨论】:

      • 将按什么顺序加载:.gdbinit-x 提供的源以及要调试的可执行文件(及其符号)?这或许可以解释 为什么 OP 的尝试不起作用。
      • .gdbinit 在-x 指定的文件之前加载。 GDB 初始化过程描述在这里:sourceware.org/gdb/onlinedocs/gdb/Startup.html
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 2021-02-16
      • 2017-09-19
      • 2015-05-28
      • 2017-01-29
      • 2016-03-10
      相关资源
      最近更新 更多