如何在Emacs中通过Python脚本获取gdb输入

1 投票
2 回答
827 浏览
提问于 2025-04-16 15:37

我想用Python写一个可以和gdb互动的脚本。在gdb里面运行得很好,但如果我在emacs中调用gdb,就不行了。

比如说,我的Python脚本(test.py)长这样。它只是简单地打印出它接收到的内容。

def testInput():
    n = raw_input('(gdb) ')
    print n

在gdb中运行没问题:

% gdb
GNU gdb (GDB) 7.2.50.20110217
Copyright (C) 2011 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/>.
(gdb) source test.py
(gdb) python testInput()
(gdb) 1
1
(gdb) q

但是在emacs里,它会在“raw_input”那卡住,根本无法获取输入:

(gdb) source test.py
(gdb) python testInput()
(gdb) 1
2
...

有没有办法让它在emacs中也像在gdb里一样正常工作呢?

2 个回答

0

在使用 Emacs 的一个交互式外部程序时,模式需要能够从缓冲区中识别出外部程序在请求用户输入,这样它才能知道需要让用户输入一些东西。否则,Emacs 会一直等待外部程序输出它能识别的内容,而外部程序则会一直等待输入。由于你的 Python 脚本使用了不同的提示符,Emacs 的 gdb 模式就无法识别出需要输入的情况。

你可以尝试修改 gdb-prompt-name-regexp 的值。下面是来自 gdb-mi.el 的默认值:

(defvar gdb-prompt-name-regexp "value=\"\\(.*?\\)\"")
1

我用 M-x gud-gdb 来启动 gdb,这个方法解决了我的问题。如果想了解更多细节,可以参考 Emacs 手册的第27章。

希望这两年后仍然对你有帮助...

撰写回答