Arek' Fu
Arek' Fu

Reputation: 857

Determine compiler name/version from gdb

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?

Upvotes: 0

Views: 1291

Answers (1)

Arek' Fu
Arek' Fu

Reputation: 857

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.

Upvotes: 2

Related Questions