From: Florian Weimer via Gdb <gdb@sourceware.org>
To: gdb@sourceware.org
Subject: Using GDB as a Python interpreter
Date: Sat, 01 Jul 2023 10:53:10 +0200 [thread overview]
Message-ID: <87sfa8vwqx.fsf@oldenburg.str.redhat.com> (raw)
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']
next reply other threads:[~2023-07-01 8:54 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-07-01 8:53 Florian Weimer via Gdb [this message]
2023-07-03 7:11 ` Aktemur, Tankut Baris via Gdb
2023-07-03 8:13 ` Florian Weimer via Gdb
2023-07-03 19:39 ` Tom Tromey
2023-07-03 20:24 ` Mark Wielaard
2023-07-03 23:00 ` Tom Tromey
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87sfa8vwqx.fsf@oldenburg.str.redhat.com \
--to=gdb@sourceware.org \
--cc=fweimer@redhat.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox