From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 29933 invoked by alias); 12 Nov 2009 20:53:53 -0000 Received: (qmail 29921 invoked by uid 22791); 12 Nov 2009 20:53:51 -0000 X-SWARE-Spam-Status: No, hits=-2.6 required=5.0 tests=BAYES_00,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mailgw2.fnal.gov (HELO mailgw2.fnal.gov) (131.225.111.12) by sourceware.org (qpsmtpd/0.43rc1) with ESMTP; Thu, 12 Nov 2009 20:53:46 +0000 Received: from mailav2.fnal.gov (mailav2.fnal.gov [131.225.111.20]) by mailgw2.fnal.gov (iPlanet Messaging Server 5.2 HotFix 2.06 (built Mar 28 2005)) with SMTP id <0KT00053CJTZUS@mailgw2.fnal.gov> for gdb@sourceware.org; Thu, 12 Nov 2009 14:53:44 -0600 (CST) Received: from mailgw1.fnal.gov ([131.225.111.11]) by mailav2.fnal.gov (SAVSMTP 3.1.7.47) with SMTP id M2009111214534431261 for ; Thu, 12 Nov 2009 14:53:44 -0600 Received: from conversion-daemon.mailgw1.fnal.gov by mailgw1.fnal.gov (iPlanet Messaging Server 5.2 HotFix 2.06 (built Mar 28 2005)) id <0KT000F01K5QQS@mailgw1.fnal.gov> (original mail from snyder@fnal.gov) for gdb@sourceware.org; Thu, 12 Nov 2009 14:53:44 -0600 (CST) Received: from d0mino02.fnal.gov (d0mino02.fnal.gov [131.225.219.201]) by mailgw1.fnal.gov (iPlanet Messaging Server 5.2 HotFix 2.06 (built Mar 28 2005)) with ESMTPS id <0KT000GY1KPJ10@mailgw1.fnal.gov>; Thu, 12 Nov 2009 14:53:43 -0600 (CST) Received: from d0mino02.fnal.gov (localhost.localdomain [127.0.0.1]) by d0mino02.fnal.gov (8.12.11.20060308/8.12.11) with ESMTP id nACKrhZm023643; Thu, 12 Nov 2009 14:53:43 -0600 Received: from d0mino02.fnal.gov (snyder@localhost) by d0mino02.fnal.gov (8.12.11.20060308/8.12.11/Submit) with ESMTP id nACKrgkk023638; Thu, 12 Nov 2009 14:53:42 -0600 Date: Fri, 13 Nov 2009 14:36:00 -0000 From: scott snyder Subject: Re: Python Scripting Question In-reply-to: To: Tom Tromey Cc: Arjun Roy , gdb@sourceware.org Reply-to: scott snyder Message-id: <200911122053.nACKrgkk023638@d0mino02.fnal.gov> MIME-version: 1.0 Content-type: TEXT/PLAIN Content-transfer-encoding: 7BIT References: <4AF63479.7010602@gmail.com> <4AF87557.8020701@stanford.edu> Comments: In-reply-to Tom Tromey message dated "Mon, 09 Nov 2009 13:19:39 -0700." X-IsSubscribed: yes Mailing-List: contact gdb-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-owner@sourceware.org X-SW-Source: 2009-11/txt/msg00136.txt.bz2 >From: Tom Tromey >>>>>> "Arjun" == Arjun Roy 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 @@ -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" },