public inbox for gdb@sourceware.org
 help / color / mirror / Atom feed
* Using GDB as a Python interpreter
@ 2023-07-01  8:53 Florian Weimer
  2023-07-03  7:11 ` Aktemur, Tankut Baris
  2023-07-03 19:39 ` Tom Tromey
  0 siblings, 2 replies; 6+ messages in thread
From: Florian Weimer @ 2023-07-01  8:53 UTC (permalink / raw)
  To: gdb

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']


^ permalink raw reply	[flat|nested] 6+ messages in thread

* RE: Using GDB as a Python interpreter
  2023-07-01  8:53 Using GDB as a Python interpreter Florian Weimer
@ 2023-07-03  7:11 ` Aktemur, Tankut Baris
  2023-07-03  8:13   ` Florian Weimer
  2023-07-03 19:39 ` Tom Tromey
  1 sibling, 1 reply; 6+ messages in thread
From: Aktemur, Tankut Baris @ 2023-07-03  7:11 UTC (permalink / raw)
  To: Florian Weimer, gdb

On Saturday, July 1, 2023 10:53 AM, Florian Weimer wrote:
> 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.

Based on this comment, I assume you have write access to /usr.
Would you then consider wrapping your script in a Python function
and then installing it in GDB's data directory
(e.g. /usr/share/gdb/python/gdb/function/)?  The file would be loaded
automatically by GDB at startup.  You can then invoke the Python function
with a single-line GDB command.

-Baris

 
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva  
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using GDB as a Python interpreter
  2023-07-03  7:11 ` Aktemur, Tankut Baris
@ 2023-07-03  8:13   ` Florian Weimer
  0 siblings, 0 replies; 6+ messages in thread
From: Florian Weimer @ 2023-07-03  8:13 UTC (permalink / raw)
  To: Aktemur, Tankut Baris via Gdb; +Cc: Aktemur, Tankut Baris

* Tankut Baris via Gdb Aktemur:

> On Saturday, July 1, 2023 10:53 AM, Florian Weimer wrote:
>> 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.
>
> Based on this comment, I assume you have write access to /usr.
> Would you then consider wrapping your script in a Python function
> and then installing it in GDB's data directory
> (e.g. /usr/share/gdb/python/gdb/function/)?  The file would be loaded
> automatically by GDB at startup.  You can then invoke the Python function
> with a single-line GDB command.

I was aiming for a one-file solution (and something that could be
installed as-is into /usr/bin eventually).  Two-file solutions are sort
of easy. 8-)

Thanks,
Florian


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using GDB as a Python interpreter
  2023-07-01  8:53 Using GDB as a Python interpreter Florian Weimer
  2023-07-03  7:11 ` Aktemur, Tankut Baris
@ 2023-07-03 19:39 ` Tom Tromey
  2023-07-03 20:24   ` Mark Wielaard
  1 sibling, 1 reply; 6+ messages in thread
From: Tom Tromey @ 2023-07-03 19:39 UTC (permalink / raw)
  To: Florian Weimer via Gdb; +Cc: Florian Weimer

>>>>> "Florian" == Florian Weimer via Gdb <gdb@sourceware.org> writes:

Florian> I want to run a Python script with GDB, from the shell command line,
Florian> without having to create two files.

Once upon a time, Red Hat had a patch to add a '-P' option to gdb, which
would put it directly into Python scripting.  I think maybe I wrote it
ages ago, but then when Andrew tried to submit it upstream, I basically
rejected it.

That was a mistake on my part.  IIRC he had a pretty good
counter-argument at the time, I don't recall why I didn't recant.

Another option I looked at, way back when, was to compile gdb with -fPIE
and let it be loaded as a shared library.  The idea here was that, with
a few other tweaks, ordinary Python code could just 'import gdb'.

Anyway, I guess now I'd support either of these.  'import gdb' would be
great to have, even with its various limitations.  However it's a bit
trickier to get working than just resurrecting the old -P patch.

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using GDB as a Python interpreter
  2023-07-03 19:39 ` Tom Tromey
@ 2023-07-03 20:24   ` Mark Wielaard
  2023-07-03 23:00     ` Tom Tromey
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Wielaard @ 2023-07-03 20:24 UTC (permalink / raw)
  To: Tom Tromey; +Cc: Florian Weimer via Gdb, Florian Weimer

On Mon, Jul 03, 2023 at 01:39:34PM -0600, Tom Tromey wrote:
> >>>>> "Florian" == Florian Weimer via Gdb <gdb@sourceware.org> writes:
> 
> Florian> I want to run a Python script with GDB, from the shell command line,
> Florian> without having to create two files.
> 
> Once upon a time, Red Hat had a patch to add a '-P' option to gdb, which
> would put it directly into Python scripting.  I think maybe I wrote it
> ages ago, but then when Andrew tried to submit it upstream, I basically
> rejected it.
> 
> That was a mistake on my part.  IIRC he had a pretty good
> counter-argument at the time, I don't recall why I didn't recant.

Was that this?
https://inbox.sourceware.org/gdb-patches/20190721235427.21893-1-kevinb@redhat.com/

Cheers,

Mark

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Using GDB as a Python interpreter
  2023-07-03 20:24   ` Mark Wielaard
@ 2023-07-03 23:00     ` Tom Tromey
  0 siblings, 0 replies; 6+ messages in thread
From: Tom Tromey @ 2023-07-03 23:00 UTC (permalink / raw)
  To: Mark Wielaard; +Cc: Tom Tromey, Florian Weimer via Gdb, Florian Weimer

Mark> Was that this?
Mark> https://inbox.sourceware.org/gdb-patches/20190721235427.21893-1-kevinb@redhat.com/

Yeah, and I mis-remembered who sent it upstream, sorry about that!

Tom

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-07-03 23:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-01  8:53 Using GDB as a Python interpreter Florian Weimer
2023-07-03  7:11 ` Aktemur, Tankut Baris
2023-07-03  8:13   ` Florian Weimer
2023-07-03 19:39 ` Tom Tromey
2023-07-03 20:24   ` Mark Wielaard
2023-07-03 23:00     ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).