从 gdb 确定编译器 name/version

Determine compiler name/version from gdb

我在机器 运行 不同版本的 gcc 之间共享我的 .gdbinit 脚本(通过 NFS)。如果我正在调试的代码是使用特定编译器版本编译的,我希望执行一些 gdb 命令。 gdb 可以吗?

我想到了这个:

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

对于我的目的来说已经足够好了,虽然它可能不够通用。