Я пришел с этим:
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
Это достаточно хорошо для моей цели, хотя это, вероятно, не достаточно общее.
Наверное, нет. Связано: http://stackoverflow.com/questions/12112338/get-the-compiler-options-from-a-compiled-executable –
@PaulR Хотя версия компилятора обычно встроена по умолчанию: 'readelf -p .comment the_program ', Я не знаю, можете ли вы получить доступ к этому из gdb. – nos
@nos: Я не уверен, что работает надежно - я просто попробовал его с исполняемым файлом, созданным (в основном) с ICC, и он утверждает, что он построен с помощью gcc. Я предполагаю, что информация поступает из библиотеки, а не напрямую связана с компилятор. Также существует проблема с кодом, созданным с несколькими компиляторами, например. некоторые модули, построенные с помощью gcc, некоторые с ICC - как это будет обрабатываться/сообщаться? –