I share my .gdbinit
script (via NFS) across machines running different versions of gcc
. I would like some gdb
commands to be executed if the code I am debugging has been compiled with a specific compiler version. Can gdb
do that?
Asked
Active
Viewed 1,036 times
0

Arek' Fu
- 826
- 8
- 24
-
1Probably not. Related: http://stackoverflow.com/questions/12112338/get-the-compiler-options-from-a-compiled-executable – Paul R Feb 03 '15 at 09:15
-
@PaulR Though, the compiler version is normally embedded by default: `readelf -p .comment the_program` , I don't know whether you can access that from gdb though. – nos Feb 03 '15 at 09:56
-
@nos: I'm not sure that works reliably - I just tried it with an executable built (mainly) with ICC and it claims it's built with gcc - I'm guessing the info comes from a library rather than being directly related to the compiler. There's also the issue of code built with multiple compilers, e.g. some modules built with gcc, some with ICC - how would that be handled/reported ? – Paul R Feb 03 '15 at 10:02
-
It might be icc doesn't follow the convention of adding a .comment section. If I build a binary with clang, it reports both gcc and clang versions , presumably because of library versions- as does it if I link together object files built with g++ and clang++ – nos Feb 03 '15 at 10:11
-
@nos: I think you are right, `readelf` is the way to go. See my answer below. – Arek' Fu Feb 03 '15 at 10:24
1 Answers
2
I came up with this:
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
It is good enough for my purpose, although it is probably not general enough.

Arek' Fu
- 826
- 8
- 24