Using GDB as a Python interpreter

Florian Weimer via Gdb <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <[email protected]>
I want to run a Python script with GDB, from the shell command line,
without having to create two files.

I came up with the hack included below, but it's not particularly nice.
The downside is that the script file name must end in “.py”, which would
not allow to install the script in /usr/bin for most distributions.  I
don't see a way to override that.

Thanks,
Florian

#!/usr/bin/python

if 'gdb' not in globals():
    import sys
    import os

    pid, = sys.argv[1:]
    os.execvp('gdb', [
        'gdb', '--batch',
        '-ex', 'set debuginfod enabled off',
        '-ex', 'attach ' + pid,
        '-x', __file__,
    ])

dl_ns = gdb.lookup_global_symbol('_rtld_global').value()['_dl_ns']
nsid_min, nsid_max = dl_ns.type.range()
for nsid in range(nsid_min, nsid_max + 1):
    l = dl_ns[nsid]['_ns_loaded']
    if not l:
        continue
    print('namespace', nsid)
    while l:
        print('  {!r}'.format(l['l_name'].string()))
        ln = l['l_libname']
        while ln:
            print('    alias {!r}'.format(ln['name'].string()))
            ln = ln['next']
        l = l['l_next']
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.