From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2111) id 14F133825FDC; Mon, 3 Jun 2024 15:19:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 14F133825FDC DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1717427945; bh=V6QtIOGk/qOX+o12COfGiLoHA67hOLwNm+HFyXPuT5o=; h=From:To:Subject:Date:From; b=bu0Yf8tyU1S9Au9u+/XfdzqzOy0ej6yM0h/pILY1bxyB3MRc7xLHQE2Qk/AEohKrV vPbscP5aOQkVltbpCQgPxPUC0aMZiDW/1fuSDcRtYH6faSgFBiL2pS01C0G+v9cSaT CdNs8SmCItZFC2XCl93Fgq0dy+6zivNAHJBdnxGo= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Hannes Domani To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Allow calling of convenience functions with python X-Act-Checkin: binutils-gdb X-Git-Author: Hannes Domani X-Git-Refname: refs/heads/master X-Git-Oldrev: 8e8b2ab688b3d7e6035e6c782a2b42501d57c667 X-Git-Newrev: f74da7b8b3d6f14ba9ad21b380f743e4bdc4e952 Message-Id: <20240603151905.14F133825FDC@sourceware.org> Date: Mon, 3 Jun 2024 15:19:05 +0000 (GMT) List-Id: https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Df74da7b8b3d6= f14ba9ad21b380f743e4bdc4e952 commit f74da7b8b3d6f14ba9ad21b380f743e4bdc4e952 Author: Hannes Domani Date: Mon Jun 3 17:18:30 2024 +0200 Allow calling of convenience functions with python =20 As mentioned in PR13326, currently when you try to call a convenience function with python, you get this error: =20 (gdb) py print(gdb.convenience_variable("_isvoid")(3)) Traceback (most recent call last): File "", line 1, in RuntimeError: Value is not callable (not TYPE_CODE_FUNC or TYPE_CODE_ME= THOD). Error while executing Python code. =20 So this extends valpy_call to handle TYPE_CODE_INTERNAL_FUNCTION as well, making this possible: =20 (gdb) py print(gdb.convenience_variable("_isvoid")(3)) 0 =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D13326 Approved-By: Tom Tromey Diff: --- gdb/python/py-value.c | 18 +++++++++++++----- gdb/testsuite/gdb.python/py-value.exp | 11 +++++++++++ 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c index dd17420b0b5..1ae26d73261 100644 --- a/gdb/python/py-value.c +++ b/gdb/python/py-value.c @@ -1210,11 +1210,13 @@ valpy_call (PyObject *self, PyObject *args, PyObjec= t *keywords) GDB_PY_HANDLE_EXCEPTION (except); } =20 - if (ftype->code () !=3D TYPE_CODE_FUNC && ftype->code () !=3D TYPE_CODE_= METHOD) + if (ftype->code () !=3D TYPE_CODE_FUNC && ftype->code () !=3D TYPE_CODE_= METHOD + && ftype->code () !=3D TYPE_CODE_INTERNAL_FUNCTION) { PyErr_SetString (PyExc_RuntimeError, _("Value is not callable (not TYPE_CODE_FUNC" - " or TYPE_CODE_METHOD).")); + " or TYPE_CODE_METHOD" + " or TYPE_CODE_INTERNAL_FUNCTION).")); return NULL; } =20 @@ -1248,9 +1250,15 @@ valpy_call (PyObject *self, PyObject *args, PyObject= *keywords) { scoped_value_mark free_values; =20 - value *return_value - =3D call_function_by_hand (function, NULL, - gdb::make_array_view (vargs, args_count)); + value *return_value; + if (ftype->code () =3D=3D TYPE_CODE_INTERNAL_FUNCTION) + return_value =3D call_internal_function (gdbpy_enter::get_gdbarch (), + current_language, + function, args_count, vargs); + else + return_value + =3D call_function_by_hand (function, NULL, + gdb::make_array_view (vargs, args_count)); result =3D value_to_value_object (return_value); } catch (const gdb_exception &except) diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.pyth= on/py-value.exp index aa674e8aa0d..8ab867a7088 100644 --- a/gdb/testsuite/gdb.python/py-value.exp +++ b/gdb/testsuite/gdb.python/py-value.exp @@ -377,6 +377,16 @@ proc test_inferior_function_call {} { gdb_test "python result2 =3D fp3(10)" ".*Too few arguments in function= call.*" } =20 +proc test_convenience_function_call {} { + # Get convenience function with gdb.convenience_variable. + gdb_test "python print(gdb.convenience_variable('_isvoid')(2))" "0" + gdb_test "python print(gdb.convenience_variable('_strlen')('two'))" "3" + + # Get convenience function with gdb.parse_and_eval. + gdb_test "python print(gdb.parse_and_eval('\$_isvoid')(3))" "0" + gdb_test "python print(gdb.parse_and_eval('\$_strlen')('three'))" "5" +} + # A few objfile tests. proc test_objfiles {} { gdb_test "python\nok=3DFalse\nfor file in gdb.objfiles():\n if 'py-va= lue' in file.filename:\n ok=3DTrue\nprint (ok)\nend" "True" \ @@ -782,6 +792,7 @@ test_value_in_inferior test_value_from_buffer test_value_sub_classes test_inferior_function_call +test_convenience_function_call test_assign test_value_bytes test_value_after_death