This is the mail archive of the gdb@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]

Re: Python Scripting Question


Thanks, I'll check it out. In a pinch that should meet what I need.

-Arjun


On 11/12/2009 12:53 PM, scott snyder wrote:
> 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 @@
>  
>  
>  
> +/* 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;
> +}
> +
> +
> +
>  /* 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" },
>  
>
>   


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