Mirror of the gdb mailing list
 help / color / mirror / Atom feed
From: scott snyder <snyder@fnal.gov>
To: Tom Tromey <tromey@redhat.com>
Cc: Arjun Roy <roy.arjun@gmail.com>, gdb@sourceware.org
Subject: Re: Python Scripting Question
Date: Fri, 13 Nov 2009 14:36:00 -0000	[thread overview]
Message-ID: <200911122053.nACKrgkk023638@d0mino02.fnal.gov> (raw)
In-Reply-To: <m3bpjbfo2s.fsf@fleche.redhat.com>


>From: Tom Tromey <tromey@redhat.com>
>>>>>> "Arjun" == Arjun Roy <roy.arjun@gmail.com> writes:
>Arjun> It looks as though the two main things this would need would be to:
>Arjun> 1. Be able to access the output one gets from executing a GDB command.
>Arjun> Ideally one would get an object with various fields filled in based on
>Arjun> the type of command, but for my purposes even a string would do - as
>Arjun> long as the results can be accessed somehow.
>Yeah, this has been on our wish-list for a while.

FWIW, i've been using this patch.  This adds a function
`gdb.execute_getoutput', which returns the output as a string.

This probably isn't really the right way to do this ---
I don't think this does the right thing with MI, and it has an ugly
special case for TUI, but it's been working for me so far.

sss


=== modified file 'gdb/python/python.c'
--- gdb/python/python.c	2009-10-14 16:48:13 +0000
+++ gdb/python/python.c	2009-10-14 22:44:59 +0000
@@ -27,6 +27,7 @@
 #include "observer.h"
 #include "value.h"
 #include "language.h"
+#include "tui/tui-io.h"
 
 #include <ctype.h>
 
@@ -321,6 +322,86 @@
 
 \f
 
+/* A Python function which evaluates a string using the gdb CLI
+   and returns the output as a string.  */
+
+static
+void pyexecute_file_delete (struct ui_file* stream)
+{
+}
+
+static
+void pyexecute_file_write (struct ui_file* stream,
+                           const char* buf,
+                           long length_buf)
+{
+  PyObject* out = (PyObject*)ui_file_data (stream);
+  PyObject* snew = PyString_FromStringAndSize (buf, length_buf);
+  PyString_ConcatAndDel (&out, snew);
+  set_ui_file_data (stream, out, pyexecute_file_delete);
+}
+
+static PyObject *
+execute_gdb_command_getoutput (PyObject *self, PyObject *args)
+{
+  struct cmd_list_element *alias, *prefix, *cmd;
+  char *arg, *newarg;
+  PyObject *from_tty_obj = NULL;
+  int from_tty;
+  int cmp;
+  volatile struct gdb_exception except;
+  struct ui_file* fout;
+  PyObject* out;
+  struct ui_file* old_stdout;
+  struct ui_out* old_uiout;
+
+  if (! PyArg_ParseTuple (args, "s|O!", &arg, &PyBool_Type, &from_tty_obj))
+    return NULL;
+
+  from_tty = 0;
+  if (from_tty_obj)
+    {
+      cmp = PyObject_IsTrue (from_tty_obj);
+      if (cmp < 0)
+	  return NULL;
+      from_tty = cmp;
+    }
+
+  fout = ui_file_new();
+  out = PyString_FromString ("");
+  set_ui_file_data (fout, out, pyexecute_file_delete);
+  set_ui_file_write (fout, pyexecute_file_write);
+  old_stdout = gdb_stdout;
+  gdb_stdout = fout;
+  old_uiout = uiout;
+  if (uiout == tui_out)
+    uiout = tui_old_uiout;
+  if (ui_out_redirect (uiout, gdb_stdout) < 0)
+    warning (_("Current output protocol does not support redirection"));
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      execute_command (arg, from_tty);
+    }
+  ui_out_redirect (uiout, 0);
+  uiout = old_uiout;
+  gdb_stdout = old_stdout;
+  out = ui_file_data (fout);
+  ui_file_delete (fout);
+  if (except.reason < 0) {
+    Py_DECREF (out);
+    out = 0;
+  }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  /* Do any commands attached to breakpoint we stopped at.  */
+  bpstat_do_actions ();
+
+  return out;
+}
+
+\f
+
 /* Printing.  */
 
 /* A python function to write a single string using gdb's filtered
@@ -653,6 +734,8 @@
     "Get a value from history" },
   { "execute", execute_gdb_command, METH_VARARGS,
     "Execute a gdb command" },
+  { "execute_getoutput", execute_gdb_command_getoutput, METH_VARARGS,
+    "Execute a gdb command, returning the output as a string" },
   { "parameter", gdbpy_parameter, METH_VARARGS,
     "Return a gdb parameter's value" },
 


  reply	other threads:[~2009-11-12 20:53 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-11-08 15:08 Arjun Roy
2009-11-09 17:16 ` Tom Tromey
2009-11-09 17:23   ` Tom Tromey
2009-11-09 17:44     ` Daniel Jacobowitz
2009-11-09 18:12       ` Tom Tromey
2009-11-09 20:22   ` Arjun Roy
2009-11-09 21:12     ` Tom Tromey
2009-11-13 14:36       ` scott snyder [this message]
2009-11-13 23:06         ` Arjun Roy
2009-11-18 20:43         ` 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=200911122053.nACKrgkk023638@d0mino02.fnal.gov \
    --to=snyder@fnal.gov \
    --cc=gdb@sourceware.org \
    --cc=roy.arjun@gmail.com \
    --cc=tromey@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