【问题标题】:Determine compiler name/version from gdb从 gdb 确定编译器名称/版本
【发布时间】:2015-02-03 09:09:49
【问题描述】:

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

【问题讨论】:

  • @PaulR 虽然编译器版本通常默认嵌入:readelf -p .comment the_program,但我不知道您是否可以从 gdb 访问它。
  • @nos:我不确定它是否可靠 - 我只是尝试使用(主要)用 ICC 构建的可执行文件,它声称它是用 gcc 构建的 - 我猜信息来自库而不是直接与编译器相关。还有使用多个编译器构建的代码的问题,例如一些使用 gcc 构建的模块,一些使用 ICC - 如何处理/报告?
  • 可能是 icc 不遵循添加 .comment 部分的约定。如果我用 clang 构建一个二进制文件,它会报告 gcc 和 clang 版本,大概是因为库版本 - 如果我将使用 g++ 和 clang++ 构建的目标文件链接在一起,也会如此
  • @nos:我认为你是对的,readelf 是要走的路。请参阅下面的答案。

标签: gcc gdb


【解决方案1】:

我想出了这个:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多