This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] sys.argv and ipython (interactive python) support in GDB/Python


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,
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);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]