Mirror of the gdb mailing list
 help / color / mirror / Atom feed
* gdb python and 'complete'
@ 2010-03-24 15:24 Jeroen Dobbelaere
  2010-03-24 15:54 ` Michel METZGER
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jeroen Dobbelaere @ 2010-03-24 15:24 UTC (permalink / raw)
  To: gdb

Hi,

I am trying to create some helper commands using python with gdb-7.1.

One of the things I want to do, is to retrieve the members of a c++
class, do some filtering and
present the result in a nice way.

I tried doing this, by delegating part of the request to the
'complete' command, like in :

class ShowNiceRepresentation(gdb.Command):
...

   def retrieveFunctionSummary(self):
      member_list=gdb.execute("complete b 'MyClass::").split("\n")
      ....

Now, the 'complete' command is indeed retrieving all the member
functions, but it will output them in the terminal in stead of feeding
it back to 'member_list'.

Is there a way to redirect the output, so that the python program can
investigate it ?
Or is there a more appropriate way to access the members of a c++
class form within python ?

Greetings,

Jeroen Dobbelaere


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

* Re: gdb python and 'complete'
  2010-03-24 15:24 gdb python and 'complete' Jeroen Dobbelaere
@ 2010-03-24 15:54 ` Michel METZGER
  2010-03-24 16:35   ` Jeroen Dobbelaere
  2010-03-24 17:16 ` Daniel Jacobowitz
  2010-03-24 17:59 ` Tom Tromey
  2 siblings, 1 reply; 7+ messages in thread
From: Michel METZGER @ 2010-03-24 15:54 UTC (permalink / raw)
  To: Jeroen Dobbelaere; +Cc: gdb

Hi,

You could used the lookup_type method.
Something like:

type = gdb.lookup_type("MyClass")

and then iterate over the type.field() collection.
Take a look at section 23.2.2.5 of the GDB doc to see what is available 
in the Field object.

Regards,

Michel Metzger.


Jeroen Dobbelaere wrote:
> Hi,
> 
> I am trying to create some helper commands using python with gdb-7.1.
> 
> One of the things I want to do, is to retrieve the members of a c++
> class, do some filtering and
> present the result in a nice way.
> 
> I tried doing this, by delegating part of the request to the
> 'complete' command, like in :
> 
> class ShowNiceRepresentation(gdb.Command):
> ...
> 
>    def retrieveFunctionSummary(self):
>       member_list=gdb.execute("complete b 'MyClass::").split("\n")
>       ....
> 
> Now, the 'complete' command is indeed retrieving all the member
> functions, but it will output them in the terminal in stead of feeding
> it back to 'member_list'.
> 
> Is there a way to redirect the output, so that the python program can
> investigate it ?
> Or is there a more appropriate way to access the members of a c++
> class form within python ?
> 
> Greetings,
> 
> Jeroen Dobbelaere
> 


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

* Re: gdb python and 'complete'
  2010-03-24 15:54 ` Michel METZGER
@ 2010-03-24 16:35   ` Jeroen Dobbelaere
  0 siblings, 0 replies; 7+ messages in thread
From: Jeroen Dobbelaere @ 2010-03-24 16:35 UTC (permalink / raw)
  To: Michel METZGER; +Cc: gdb

Hi,

that will indeed give me the data members.
Is there also a way to retrieve the available methods ?

Thanks,

Jeroen

On Wed, Mar 24, 2010 at 4:54 PM, Michel METZGER <michel.metzger@st.com> wrote:
> Hi,
>
> You could used the lookup_type method.
> Something like:
>
> type = gdb.lookup_type("MyClass")
>
> and then iterate over the type.field() collection.
> Take a look at section 23.2.2.5 of the GDB doc to see what is available in
> the Field object.
>
> Regards,
>
> Michel Metzger.
>
>
> Jeroen Dobbelaere wrote:
>>
>> Hi,
>>
>> I am trying to create some helper commands using python with gdb-7.1.
>>
>> One of the things I want to do, is to retrieve the members of a c++
>> class, do some filtering and
>> present the result in a nice way.
>>
>> I tried doing this, by delegating part of the request to the
>> 'complete' command, like in :
>>
>> class ShowNiceRepresentation(gdb.Command):
>> ...
>>
>>   def retrieveFunctionSummary(self):
>>      member_list=gdb.execute("complete b 'MyClass::").split("\n")
>>      ....
>>
>> Now, the 'complete' command is indeed retrieving all the member
>> functions, but it will output them in the terminal in stead of feeding
>> it back to 'member_list'.
>>
>> Is there a way to redirect the output, so that the python program can
>> investigate it ?
>> Or is there a more appropriate way to access the members of a c++
>> class form within python ?
>>
>> Greetings,
>>
>> Jeroen Dobbelaere
>>
>
>



-- 
Jeroen Dobbelaere


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

* Re: gdb python and 'complete'
  2010-03-24 15:24 gdb python and 'complete' Jeroen Dobbelaere
  2010-03-24 15:54 ` Michel METZGER
@ 2010-03-24 17:16 ` Daniel Jacobowitz
  2010-03-24 17:59 ` Tom Tromey
  2 siblings, 0 replies; 7+ messages in thread
From: Daniel Jacobowitz @ 2010-03-24 17:16 UTC (permalink / raw)
  To: Jeroen Dobbelaere; +Cc: gdb

On Wed, Mar 24, 2010 at 04:24:16PM +0100, Jeroen Dobbelaere wrote:
> Now, the 'complete' command is indeed retrieving all the member
> functions, but it will output them in the terminal in stead of feeding
> it back to 'member_list'.
> 
> Is there a way to redirect the output, so that the python program can
> investigate it ?

I ended up having to log gdb output to a file and parse the file from
Python.  But I think there's a clear argument that there should
be a generic way to get CLI output - and maybe more specific Python
routines for each command, e.g. a direct function for "complete".

-- 
Daniel Jacobowitz
CodeSourcery


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

* Re: gdb python and 'complete'
  2010-03-24 15:24 gdb python and 'complete' Jeroen Dobbelaere
  2010-03-24 15:54 ` Michel METZGER
  2010-03-24 17:16 ` Daniel Jacobowitz
@ 2010-03-24 17:59 ` Tom Tromey
  2010-03-24 22:02   ` Jeroen Dobbelaere
  2 siblings, 1 reply; 7+ messages in thread
From: Tom Tromey @ 2010-03-24 17:59 UTC (permalink / raw)
  To: Jeroen Dobbelaere; +Cc: gdb

>>>>> "Jeroen" == Jeroen Dobbelaere <jeroen.dobbelaere@gmail.com> writes:

Jeroen> Now, the 'complete' command is indeed retrieving all the member
Jeroen> functions, but it will output them in the terminal in stead of feeding
Jeroen> it back to 'member_list'.

Jeroen> Is there a way to redirect the output, so that the python program can
Jeroen> investigate it ?

This is http://sourceware.org/bugzilla/show_bug.cgi?id=10808
We'll implement this eventually, it is just that nobody has found the
time yet.

I've also been thinking recently that we should have a "gdb.execute_mi"
method, which could be used to call MI commands, but which would then
return the results as Python objects instead of strings.

Tom


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

* Re: gdb python and 'complete'
  2010-03-24 17:59 ` Tom Tromey
@ 2010-03-24 22:02   ` Jeroen Dobbelaere
  2010-03-24 22:55     ` Tom Tromey
  0 siblings, 1 reply; 7+ messages in thread
From: Jeroen Dobbelaere @ 2010-03-24 22:02 UTC (permalink / raw)
  To: tromey; +Cc: gdb

Would it help if we modify gdb.execute to do the redirection when we
call it with a 'from_tty==false' ?
Or do we need a new command for it ?

Following patch implements the redirection in gdb.execute :

diff --git a/gdb/python/python.c b/gdb/python/python.c
index 9a89eed..9280038 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -313,6 +313,9 @@ execute_gdb_command (PyObject *self, PyObject *args)
   int cmp;
   volatile struct gdb_exception except;

+  char* strresult=0;
+  long  strlength=0;
+
   if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj))
     return NULL;

@@ -330,7 +333,28 @@ execute_gdb_command (PyObject *self, PyObject *args)
       /* Copy the argument text in case the command modifies it.  */
       char *copy = xstrdup (arg);
       struct cleanup *cleanup = make_cleanup (xfree, copy);
-      execute_command (copy, from_tty);
+
+      if (from_tty)
+       {
+         execute_command (copy, from_tty);
+       }
+      else
+       {
+         struct cleanup *io_cleanup;
+         struct ui_file *io_out;
+         struct ui_file *io_backup;
+         io_out = mem_fileopen ();
+         io_cleanup = make_cleanup_ui_file_delete (io_out);
+         io_backup = gdb_stdout;
+         gdb_stdout = io_out;
+
+         execute_command (copy, from_tty);
+
+         gdb_stdout = io_backup;
+         strresult = ui_file_xstrdup (io_out, &strlength);
+         do_cleanups (io_cleanup);
+       }
+
       do_cleanups (cleanup);
     }
   GDB_PY_HANDLE_EXCEPTION (except);
@@ -338,7 +362,17 @@ execute_gdb_command (PyObject *self, PyObject *args)
   /* Do any commands attached to breakpoint we stopped at.  */
   bpstat_do_actions ();

-  Py_RETURN_NONE;
+  if (strresult)
+    {
+      PyObject* result;
+      result = PyUnicode_Decode (strresult, strlength, host_charset (), NULL);
+      xfree (strresult);
+      return result;
+    }
+  else
+    {
+      Py_RETURN_NONE;
+    }
 }

 /* Parse a string and evaluate it as an expression.  */

----
It does have as a drawback that a
  python gdb.execute("bt")
will not display anything any more.
This should now become a :
  python print gdb.execute("bt")

Maybe, in that case, the 'python foo' should be handle as a :

result=foo
if result != None:
  print result

Greetings,

Jeroen

[..]


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

* Re: gdb python and 'complete'
  2010-03-24 22:02   ` Jeroen Dobbelaere
@ 2010-03-24 22:55     ` Tom Tromey
  0 siblings, 0 replies; 7+ messages in thread
From: Tom Tromey @ 2010-03-24 22:55 UTC (permalink / raw)
  To: Jeroen Dobbelaere; +Cc: gdb

>>>>> "Jeroen" == Jeroen Dobbelaere <jeroen.dobbelaere@gmail.com> writes:

Jeroen> Would it help if we modify gdb.execute to do the redirection when we
Jeroen> call it with a 'from_tty==false' ?
Jeroen> Or do we need a new command for it ?

I think the best route is to add a new optional argument to gdb.execute,
which defaults to False and which, when True, means to return the output
as a string.

This approach is backward compatible, and also not too ugly.

I noticed, thanks to your patch, that execute_gdb_command is not using
keyword arguments.  I think it should.

Jeroen> Following patch implements the redirection in gdb.execute :

If you intend to implement this, you'll need copyright assignment
paperwork on file.  If you don't have this already, let me know and I
can get you started.

Tom


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

end of thread, other threads:[~2010-03-24 22:55 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-03-24 15:24 gdb python and 'complete' Jeroen Dobbelaere
2010-03-24 15:54 ` Michel METZGER
2010-03-24 16:35   ` Jeroen Dobbelaere
2010-03-24 17:16 ` Daniel Jacobowitz
2010-03-24 17:59 ` Tom Tromey
2010-03-24 22:02   ` Jeroen Dobbelaere
2010-03-24 22:55     ` Tom Tromey

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox