* [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
@ 2011-07-01 4:00 Taisuke Yamada
[not found] ` <BANLkTimcWKLMOkd0R8bNT-z4yqr0FJ_uog@mail.gmail.com>
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Taisuke Yamada @ 2011-07-01 4:00 UTC (permalink / raw)
To: gdb-patches
[-- Attachment #1: Type: text/plain, Size: 793 bytes --]
Hi.
While working with GDB/Python, I wondered if I can use it
interactively by loading ipython (interactive python shell) on
top of it. However, current GDB/Python fails as it does not
initialize sys.argv properly.
So here is a patch to set sys.argv when initializing embedded
Python interpreter. I have tested it by running ipython with
(gdb) python execfile("/usr/bin/ipython")
Python 2.6.6 (r266:84292, Oct 9 2010, 12:40:51)
...
In [1]: import gdb
In [2]: print gdb.lookup_symbol("main")
(<gdb.Symbol object at 0x7ff6f9815b20>, False)
It is quite nice to be able to inspect debuggee interactively
using full-featured language.
This patch should also improve compatibility with other exsiting
python libraries, as some of those expect existance of sys.argv.
Best Regards,
[-- Attachment #2: gdb-python-ipython.patch --]
[-- Type: text/x-patch, Size: 434 bytes --]
diff --git a/gdb/python/python.c b/gdb/python/python.c
index 90d5dc8..c9f2ce9 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -1010,7 +1010,9 @@ Enables or disables printing of Python stack traces."),
SLASH_STRING, "python", NULL));
#endif
+ char *argv[] = { "gdb", NULL };
Py_Initialize ();
+ PySys_SetArgvEx (1, argv, 0);
PyEval_InitThreads ();
gdb_module = Py_InitModule ("gdb", GdbMethods);
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
[not found] ` <BANLkTimcWKLMOkd0R8bNT-z4yqr0FJ_uog@mail.gmail.com>
@ 2011-07-01 10:31 ` Kevin Pouget
0 siblings, 0 replies; 7+ messages in thread
From: Kevin Pouget @ 2011-07-01 10:31 UTC (permalink / raw)
To: gdb-patches
hello,
just one thing, you're patch doesn't compile on my computer because of this line
+ char *argv[] = { "gdb", NULL };
error: ISO C90 forbigs mixed declarations and code
but ipython looks very intersting, thanks!
Kevin
On Fri, Jul 1, 2011 at 6:00 AM, Taisuke Yamada <tai@rakugaki.org> wrote:
>
> Hi.
>
> While working with GDB/Python, I wondered if I can use it
> interactively by loading ipython (interactive python shell) on
> top of it. However, current GDB/Python fails as it does not
> initialize sys.argv properly.
>
> So here is a patch to set sys.argv when initializing embedded
> Python interpreter. I have tested it by running ipython with
>
> (gdb) python execfile("/usr/bin/ipython")
> Python 2.6.6 (r266:84292, Oct 9 2010, 12:40:51)
> ...
> In [1]: import gdb
> In [2]: print gdb.lookup_symbol("main")
> (<gdb.Symbol object at 0x7ff6f9815b20>, False)
>
> It is quite nice to be able to inspect debuggee interactively
> using full-featured language.
>
> This patch should also improve compatibility with other exsiting
> python libraries, as some of those expect existance of sys.argv.
>
> Best Regards,
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
2011-07-01 4:00 [PATCH] sys.argv and ipython (interactive python) support in GDB/Python Taisuke Yamada
[not found] ` <BANLkTimcWKLMOkd0R8bNT-z4yqr0FJ_uog@mail.gmail.com>
@ 2011-07-01 10:59 ` Phil Muldoon
2011-07-01 14:10 ` Taisuke Yamada
2011-07-01 14:34 ` Tom Tromey
2011-07-01 14:27 ` Tom Tromey
2 siblings, 2 replies; 7+ messages in thread
From: Phil Muldoon @ 2011-07-01 10:59 UTC (permalink / raw)
To: Taisuke Yamada; +Cc: gdb-patches
Taisuke Yamada <tai@rakugaki.org> writes:
Thanks for the patch!
> diff --git a/gdb/python/python.c b/gdb/python/python.c
> index 90d5dc8..c9f2ce9 100644
> --- a/gdb/python/python.c
> +++ b/gdb/python/python.c
> @@ -1010,7 +1010,9 @@ Enables or disables printing of Python stack traces."),
> SLASH_STRING, "python", NULL));
> #endif
>
> + char *argv[] = { "gdb", NULL };
As Kevin noted, this needs to be placed in the declarations part of the
function.
> Py_Initialize ();
> + PySys_SetArgvEx (1, argv, 0);
This can throw an error. Though, it seems unlikely it would. I'm also
a little confused about the documentation about PySys_SetArgvEx.
"These parameters are similar to those passed to the program's
main() function with the difference that the first entry should refer to
the script file to be executed rather than the executable hosting the
Python interpreter. If there isn't a script that will be run,
the first entry in argv can be an empty string. If this function fails
to initialize sys.argv, a fatal condition is signalled using
Py_FatalError()."
Isn't your first argument of argv the executable GDB?
I should know this, but the documentation notes "New in version
2.6.6". I think we support 2.6.6, but one of the maintainers would have
to reply here.
Lastly, even though this patch is about two lines, I believe it is not a
trivial change, and probably needs a ChangeLog entry.
Cheers,
Phil
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
2011-07-01 10:59 ` Phil Muldoon
@ 2011-07-01 14:10 ` Taisuke Yamada
2011-07-01 14:14 ` Phil Muldoon
2011-07-01 14:34 ` Tom Tromey
1 sibling, 1 reply; 7+ messages in thread
From: Taisuke Yamada @ 2011-07-01 14:10 UTC (permalink / raw)
To: pmuldoon; +Cc: gdb-patches
Hi.
>> + char *argv[] = { "gdb", NULL };
>
> As Kevin noted, this needs to be placed in the declarations part of the function.
Yes...I'm now too used to C99 and was too sloppy to move declaration upward :-)
>> Py_Initialize ();
>> + PySys_SetArgvEx (1, argv, 0);
>
> Isn't your first argument of argv the executable GDB?
Not really. I'm just setting "gdb" as a text label to use when generating
log message both in python and python library. Just like in C, it *usually*
points to executing script itself, but after all, there's nothing that
gurantees that.
Alternatively, you can do "PySys_SetArgvEx (0, NULL, 0);", but then
python will automatically generate empty (= "") argv[0]. That means any
logs generated will be somewhat odd, so my idea was to have something
instead. If you're concerned, maybe "python-gdb" would be better.
Best Regards,
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
2011-07-01 14:10 ` Taisuke Yamada
@ 2011-07-01 14:14 ` Phil Muldoon
0 siblings, 0 replies; 7+ messages in thread
From: Phil Muldoon @ 2011-07-01 14:14 UTC (permalink / raw)
To: Taisuke Yamada; +Cc: gdb-patches
Taisuke Yamada <tai@rakugaki.org> writes:
> Not really. I'm just setting "gdb" as a text label to use when generating
> log message both in python and python library. Just like in C, it *usually*
> points to executing script itself, but after all, there's nothing that
> gurantees that.
>
> Alternatively, you can do "PySys_SetArgvEx (0, NULL, 0);", but then
> python will automatically generate empty (= "") argv[0]. That means any
> logs generated will be somewhat odd, so my idea was to have something
> instead.
Ah ok, thanks for the explanation.
> If you're concerned, maybe "python-gdb" would be better.
I'm not, your explanation is good.
Cheers
Phil
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
2011-07-01 4:00 [PATCH] sys.argv and ipython (interactive python) support in GDB/Python Taisuke Yamada
[not found] ` <BANLkTimcWKLMOkd0R8bNT-z4yqr0FJ_uog@mail.gmail.com>
2011-07-01 10:59 ` Phil Muldoon
@ 2011-07-01 14:27 ` Tom Tromey
2 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2011-07-01 14:27 UTC (permalink / raw)
To: Taisuke Yamada; +Cc: gdb-patches
>>>>> "Taisuke" == Taisuke Yamada <tai@rakugaki.org> writes:
Taisuke> So here is a patch to set sys.argv when initializing embedded
Taisuke> Python interpreter.
Taisuke> + char *argv[] = { "gdb", NULL };
It seems to me that this should probably be { "", NULL }.
From the docs:
-- Function: void PySys_SetArgvEx (int argc, char **argv,
int updatepath)
[...]
If there isn't a script that will be run,
the first entry in _argv_ can be an empty string.
Tom
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH] sys.argv and ipython (interactive python) support in GDB/Python
2011-07-01 10:59 ` Phil Muldoon
2011-07-01 14:10 ` Taisuke Yamada
@ 2011-07-01 14:34 ` Tom Tromey
1 sibling, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2011-07-01 14:34 UTC (permalink / raw)
To: pmuldoon; +Cc: Taisuke Yamada, gdb-patches
>>>>> "Phil" == Phil Muldoon <pmuldoon@redhat.com> writes:
Phil> I should know this, but the documentation notes "New in version
Phil> 2.6.6". I think we support 2.6.6, but one of the maintainers would have
Phil> to reply here.
Thanks, I missed this.
We support back to 2.4. So, we can't use this function unconditionally.
Perhaps we can instead call PySys_SetArgv followed by popping sys.path,
as recommended in the manual.
Phil> Lastly, even though this patch is about two lines, I believe it is
Phil> not a trivial change, and probably needs a ChangeLog entry.
I think it is trivial for copyright purposes.
All patches require a ChangeLog entry.
Tom
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2011-07-01 14:34 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-07-01 4:00 [PATCH] sys.argv and ipython (interactive python) support in GDB/Python Taisuke Yamada
[not found] ` <BANLkTimcWKLMOkd0R8bNT-z4yqr0FJ_uog@mail.gmail.com>
2011-07-01 10:31 ` Kevin Pouget
2011-07-01 10:59 ` Phil Muldoon
2011-07-01 14:10 ` Taisuke Yamada
2011-07-01 14:14 ` Phil Muldoon
2011-07-01 14:34 ` Tom Tromey
2011-07-01 14:27 ` Tom Tromey
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox