我在运行不同版本.gdbinit
的计算机之间共享我的gcc
脚本(通过NFS)。如果我正在调试的代码是使用特定的编译器版本编译的,我希望执行一些gdb
命令。 gdb
能做到吗?
答案 0 :(得分:2)
我想出了这个:
define hook-run
python
from subprocess import Popen, PIPE
from re import search
# grab the executable filename from gdb
# this is probably not general enough --
# there might be several objfiles around
objfilename = gdb.objfiles()[0].filename
# run readelf
process = Popen(['readelf', '-p', '.comment', objfilename], stdout=PIPE)
output = process.communicate()[0]
# match the version number with a
regex = 'GCC: \(GNU\) ([\d.]+)'
match=search(regex, output)
if match:
compiler_version = match.group(1)
gdb.execute('set $compiler_version="'+str(compiler_version)+'"')
gdb.execute('init-if-undefined $compiler_version="None"')
# do what you want with the python compiler_version variable and/or
# with the $compiler_version convenience variable
# I use it to load version-specific pretty-printers
end
end
这对我的目的来说已经足够了,虽然它可能不够通用。