2010-07-27 Phil Muldoon <pmuldoon@redhat.com>
authorPhil Muldoon <pmuldoon@redhat.com>
Tue, 27 Jul 2010 12:40:42 +0000 (12:40 +0000)
committerPhil Muldoon <pmuldoon@redhat.com>
Tue, 27 Jul 2010 12:40:42 +0000 (12:40 +0000)
* python/py-value.c (valpy_call): New Function.

2010-07-27  Phil Muldoon  <pmuldoon@redhat.com>

* gdb.python/py-value.exp (test_inferior_function_call): New function.
* gdb.python/py-value.c (func1): New function.
(func2): Likewise.

2010-07-27  Phil Muldoon  <pmuldoon@redhat.com>

* gdb.texinfo (Values From Inferior): Add value inferior function
call description.

gdb/ChangeLog
gdb/doc/ChangeLog
gdb/doc/gdb.texinfo
gdb/python/py-value.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-value.c
gdb/testsuite/gdb.python/py-value.exp

index d1fedab37359f5b9fe5dd84a199f53dd538ecf5f..c865a02241f76c6019e6516b9b4cc6ea72b2fe77 100644 (file)
@@ -1,3 +1,7 @@
+2010-07-27  Phil Muldoon  <pmuldoon@redhat.com>
+
+       * python/py-value.c (valpy_call): New Function.
+
 2010-07-27  Ken Werner  <ken.werner@de.ibm.com>
 
        * valops.c (dwarf2_read_index): Initialize the types_list and 
index 0f52851bd21cac1014292acd83f41d6c1956207b..8c249ef74dd380b99caab4aaf772e4db9eb21243 100644 (file)
@@ -1,3 +1,8 @@
+2010-07-27  Phil Muldoon  <pmuldoon@redhat.com>
+
+       * gdb.texinfo (Values From Inferior): Add value inferior function
+       call description.
+
 2010-07-19  Jan Kratochvil  <jan.kratochvil@redhat.com>
 
        * gdb.texinfo (Active Targets): Fix wrong comma placement.
index be6cd3d8ceda781f5af9787025a080d1b5705742..fa583aaf7ed81a07cbdb683c4a72fb6ed05f4160 100644 (file)
@@ -20608,6 +20608,22 @@ bar = some_val['foo']
 
 Again, @code{bar} will also be a @code{gdb.Value} object.
 
+A @code{gdb.Value} that represents a function can be executed via
+inferior function call.  Any arguments provided to the call must match
+the function's prototype, and must be provided in the order specified
+by that prototype.
+
+For example, @code{some_val} is a @code{gdb.Value} instance
+representing a function that takes two integers as arguments.  To
+execute this function, call it like so:
+
+@smallexample
+result = some_val (10,20)
+@end smallexample
+
+Any values returned from a function call will be stored as a
+@code{gdb.Value}.
+
 The following attributes are provided:
 
 @table @code
index 2024021ac11f5a68066736c7d78de1e69283c5f8..d547ed1b172a387563a8fa62923ce517be548c85 100644 (file)
@@ -25,6 +25,7 @@
 #include "language.h"
 #include "dfp.h"
 #include "valprint.h"
+#include "infcall.h"
 
 #ifdef HAVE_PYTHON
 
@@ -393,6 +394,53 @@ valpy_setitem (PyObject *self, PyObject *key, PyObject *value)
   return -1;
 }
 
+/* Called by the Python interpreter to perform an inferior function
+   call on the value.  */
+static PyObject *
+valpy_call (PyObject *self, PyObject *args, PyObject *keywords)
+{
+  struct value *return_value = NULL;
+  Py_ssize_t args_count;
+  volatile struct gdb_exception except;
+  struct value *function = ((value_object *) self)->value;
+  struct value **vargs = NULL;
+  struct type *ftype = check_typedef (value_type (function));
+
+  if (TYPE_CODE (ftype) != TYPE_CODE_FUNC)
+    {
+      PyErr_SetString (PyExc_RuntimeError,
+                      _("Value is not callable (not TYPE_CODE_FUNC)."));
+      return NULL;
+    }
+
+  args_count = PyTuple_Size (args);
+  if (args_count > 0)
+    {
+      int i;
+
+      vargs = alloca (sizeof (struct value *) * args_count);
+      for (i = 0; i < args_count; i++)
+       {
+         PyObject *item = PyTuple_GetItem (args, i);
+
+         if (item == NULL)
+           return NULL;
+
+         vargs[i] = convert_value_from_python (item);
+         if (vargs[i] == NULL)
+           return NULL;
+       }
+    }
+
+  TRY_CATCH (except, RETURN_MASK_ALL)
+    {
+      return_value = call_function_by_hand (function, args_count, vargs);
+    }
+  GDB_PY_HANDLE_EXCEPTION (except);
+
+  return value_to_value_object (return_value);
+}
+
 /* Called by the Python interpreter to obtain string representation
    of the object.  */
 static PyObject *
@@ -1151,7 +1199,7 @@ PyTypeObject value_object_type = {
   0,                             /*tp_as_sequence*/
   &value_object_as_mapping,      /*tp_as_mapping*/
   valpy_hash,                    /*tp_hash*/
-  0,                             /*tp_call*/
+  valpy_call,                    /*tp_call*/
   valpy_str,                     /*tp_str*/
   0,                             /*tp_getattro*/
   0,                             /*tp_setattro*/
index ae6dfa6e93987a3bcbcb75996ba8c20ffba52a6e..1e2ecd6982e33907a5e66f4e14e8d93d214d15d6 100644 (file)
@@ -1,3 +1,9 @@
+2010-07-27  Phil Muldoon  <pmuldoon@redhat.com>
+
+       * gdb.python/py-value.exp (test_inferior_function_call): New function.
+       * gdb.python/py-value.c (func1): New function.
+       (func2): Likewise.
+
 2010-07-26  Corinna Vinschen  <vinschen@redhat.com>
 
        * gdb.asm/xstormy16.inc (gdbasm_startup): Fix beginning of stack so
index be933b314894ef364e66ea6dadc4c3d88b120f7b..7481bd528104138bb6d4018936c5bafae940042d 100644 (file)
@@ -15,6 +15,8 @@
    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
 
+#include <stdio.h>
+
 struct s
 {
   int a;
@@ -44,6 +46,16 @@ void ptr_ref(int*& rptr_int)
 }
 #endif
 
+void func1 ()
+{
+  printf ("void function called\n");
+}
+
+int func2 (int arg1, int arg2)
+{
+  return arg1 + arg2;
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -53,6 +65,8 @@ main (int argc, char *argv[])
   PTR x = &s;
   char st[17] = "divide et impera";
   char nullst[17] = "divide\0et\0impera";
+  void (*fp1) (void)  = &func1;
+  int  (*fp2) (int, int) = &func2;
   const char *sptr = "pointer";
   const char *embed = "embedded x\201\202\203\204";
   int a[3] = {1,2,3};
@@ -63,6 +77,8 @@ main (int argc, char *argv[])
   s.a = 3;
   s.b = 5;
   u.a = 7;
+  (*fp1) ();
+  (*fp2) (10,20);
 
 #ifdef __cplusplus
   ptr_ref(ptr_i);
index a24bc11b0f03dc479f6d8126bcc1adec775d0f28..eaed9c7a6b41754554c8e345fb97f7f85c1e0621 100644 (file)
@@ -276,6 +276,35 @@ proc test_lazy_strings {} {
 }
 
 
+proc test_inferior_function_call {} {
+    global gdb_prompt hex decimal
+
+    # Correct inferior call without arguments.
+    gdb_test "p/x fp1" " = $hex.*"
+    gdb_py_test_silent_cmd "python fp1 = gdb.history (0)" "get value from history" 1
+    gdb_test "python fp1 = fp1.dereference()" ""
+    gdb_test "python result = fp1()" ""
+    gdb_test "python print result" "void"
+
+    # Correct inferior call with arguments.
+    gdb_test "p/x fp2" " = $hex.*"
+    gdb_py_test_silent_cmd "python fp2 = gdb.history (0)" "get value from history" 1
+    gdb_test "python fp2 = fp2.dereference()" ""
+    gdb_test "python result2 = fp2(10,20)" ""
+    gdb_test "python print result2" "30"
+
+    # Incorrect to call an int value.
+    gdb_test "p i" " = $decimal.*"
+    gdb_py_test_silent_cmd "python i = gdb.history (0)" "get value from history" 1
+    gdb_test "python result3 = i()" ".*Value is not callable.*"
+
+    # Incorrect number of arguments.
+    gdb_test "p/x fp2" " = $hex.*"
+    gdb_py_test_silent_cmd "python fp3 = gdb.history (0)" "get value from history" 1
+    gdb_test "python fp3 = fp3.dereference()" ""
+    gdb_test "python result2 = fp3(10)" ".*Too few arguments in function call.*"
+}
+
 # A few objfile tests.
 proc test_objfiles {} {
     gdb_test "python\nok=False\nfor file in gdb.objfiles():\n  if 'py-value' in file.filename:\n    ok=True\nprint ok\nend" "True"
@@ -435,6 +464,7 @@ if ![runto_main] then {
 }
 
 test_value_in_inferior
+test_inferior_function_call
 test_lazy_strings
 test_value_after_death
 
This page took 0.067021 seconds and 4 git commands to generate.