From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2126) id 891B638582BE; Fri, 8 Jul 2022 20:28:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 891B638582BE Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable From: Tom Tromey To: gdb-cvs@sourceware.org Subject: [binutils-gdb] Accept gdb.Value in more Python APIs X-Act-Checkin: binutils-gdb X-Git-Author: Tom Tromey X-Git-Refname: refs/heads/master X-Git-Oldrev: fa17a6814113ac22d8059d61514aa2c6e29b0aae X-Git-Newrev: d19ca0b35c9536210c5e8bd30504489b7439f51f Message-Id: <20220708202852.891B638582BE@sourceware.org> Date: Fri, 8 Jul 2022 20:28:52 +0000 (GMT) X-BeenThere: gdb-cvs@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Jul 2022 20:28:52 -0000 https://sourceware.org/git/gitweb.cgi?p=3Dbinutils-gdb.git;h=3Dd19ca0b35c95= 36210c5e8bd30504489b7439f51f commit d19ca0b35c9536210c5e8bd30504489b7439f51f Author: Tom Tromey Date: Sun Jun 5 07:42:12 2022 -0600 Accept gdb.Value in more Python APIs =20 PR python/27000 points out that gdb.block_for_pc will accept a Python integer, but not a gdb.Value. This patch corrects this oversight. =20 I looked at all uses of GDB_PY_LLU_ARG and fixed these up to use get_addr_from_python instead. I also looked at uses of GDB_PY_LL_ARG, but those seemed relatively unlikely to be useful with a gdb.Value, so I didn't change them. My thinking here is that a Value will typically come from inferior memory, and something like a line number is not too likely to be found this way. =20 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=3D27000 Diff: --- gdb/python/py-arch.c | 33 ++++++++++-----------------= ---- gdb/python/py-progspace.c | 24 ++++++++++++++-------- gdb/testsuite/gdb.python/py-arch.exp | 2 +- gdb/testsuite/gdb.python/py-progspace.exp | 3 +++ gdb/testsuite/gdb.python/py-shared.exp | 5 ++++- gdb/testsuite/gdb.python/python.exp | 3 +++ 6 files changed, 37 insertions(+), 33 deletions(-) diff --git a/gdb/python/py-arch.c b/gdb/python/py-arch.c index d8098a2d161..594e4ed4918 100644 --- a/gdb/python/py-arch.c +++ b/gdb/python/py-arch.c @@ -122,39 +122,26 @@ static PyObject * archpy_disassemble (PyObject *self, PyObject *args, PyObject *kw) { static const char *keywords[] =3D { "start_pc", "end_pc", "count", NULL = }; - CORE_ADDR start, end =3D 0; + CORE_ADDR start =3D 0, end =3D 0; CORE_ADDR pc; - gdb_py_ulongest start_temp; long count =3D 0, i; - PyObject *end_obj =3D NULL, *count_obj =3D NULL; + PyObject *start_obj =3D nullptr, *end_obj =3D nullptr, *count_obj =3D nu= llptr; struct gdbarch *gdbarch =3D NULL; =20 ARCHPY_REQUIRE_VALID (self, gdbarch); =20 - if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, GDB_PY_LLU_ARG "|OO", - keywords, &start_temp, &end_obj, + if (!gdb_PyArg_ParseTupleAndKeywords (args, kw, "O|OO", + keywords, &start_obj, &end_obj, &count_obj)) return NULL; =20 - start =3D start_temp; - if (end_obj) - { - /* Make a long logic check first. In Python 3.x, internally, - all integers are represented as longs. In Python 2.x, there - is still a differentiation internally between a PyInt and a - PyLong. Explicitly do this long check conversion first. In - GDB, for Python 3.x, we #ifdef PyInt =3D PyLong. This check has - to be done first to ensure we do not lose information in the - conversion process. */ - if (PyLong_Check (end_obj)) - end =3D PyLong_AsUnsignedLongLong (end_obj); - else - { - PyErr_SetString (PyExc_TypeError, - _("Argument 'end_pc' should be a (long) integer.")); + if (get_addr_from_python (start_obj, &start) < 0) + return nullptr; =20 - return NULL; - } + if (end_obj !=3D nullptr) + { + if (get_addr_from_python (end_obj, &end) < 0) + return nullptr; =20 if (end < start) { diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index a9abfb4c777..5ec5986fce8 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -351,13 +351,17 @@ pspy_get_objfiles (PyObject *self_, PyObject *args) static PyObject * pspy_solib_name (PyObject *o, PyObject *args) { - gdb_py_ulongest pc; + CORE_ADDR pc; + PyObject *pc_obj; + pspace_object *self =3D (pspace_object *) o; =20 PSPY_REQUIRE_VALID (self); =20 - if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) + if (!PyArg_ParseTuple (args, "O", &pc_obj)) return NULL; + if (get_addr_from_python (pc_obj, &pc) < 0) + return nullptr; =20 const char *soname =3D solib_name_from_address (self->pspace, pc); if (soname =3D=3D nullptr) @@ -371,14 +375,17 @@ static PyObject * pspy_block_for_pc (PyObject *o, PyObject *args) { pspace_object *self =3D (pspace_object *) o; - gdb_py_ulongest pc; + CORE_ADDR pc; + PyObject *pc_obj; const struct block *block =3D NULL; struct compunit_symtab *cust =3D NULL; =20 PSPY_REQUIRE_VALID (self); =20 - if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc)) + if (!PyArg_ParseTuple (args, "O", &pc_obj)) return NULL; + if (get_addr_from_python (pc_obj, &pc) < 0) + return nullptr; =20 try { @@ -410,24 +417,25 @@ pspy_block_for_pc (PyObject *o, PyObject *args) static PyObject * pspy_find_pc_line (PyObject *o, PyObject *args) { - gdb_py_ulongest pc_llu; + CORE_ADDR pc; PyObject *result =3D NULL; /* init for gcc -Wall */ + PyObject *pc_obj; pspace_object *self =3D (pspace_object *) o; =20 PSPY_REQUIRE_VALID (self); =20 - if (!PyArg_ParseTuple (args, GDB_PY_LLU_ARG, &pc_llu)) + if (!PyArg_ParseTuple (args, "O", &pc_obj)) return NULL; + if (get_addr_from_python (pc_obj, &pc) < 0) + return nullptr; =20 try { struct symtab_and_line sal; - CORE_ADDR pc; scoped_restore_current_program_space saver; =20 set_current_program_space (self->pspace); =20 - pc =3D (CORE_ADDR) pc_llu; sal =3D find_pc_line (pc, 0); result =3D symtab_and_line_to_sal_object (sal); } diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.pytho= n/py-arch.exp index 58f6cb06b3e..1fbbc47c872 100644 --- a/gdb/testsuite/gdb.python/py-arch.exp +++ b/gdb/testsuite/gdb.python/py-arch.exp @@ -43,7 +43,7 @@ gdb_py_test_silent_cmd "python insn_list2 =3D arch.disass= emble(pc, pc)" \ "disassemble no count" 0 gdb_py_test_silent_cmd "python insn_list3 =3D arch.disassemble(pc, count= =3D1)" \ "disassemble no end" 0 -gdb_py_test_silent_cmd "python insn_list4 =3D arch.disassemble(pc)" \ +gdb_py_test_silent_cmd "python insn_list4 =3D arch.disassemble(gdb.Value(p= c))" \ "disassemble no end no count" 0 =20 gdb_test "python print (len(insn_list1))" "1" "test number of instructions= 1" diff --git a/gdb/testsuite/gdb.python/py-progspace.exp b/gdb/testsuite/gdb.= python/py-progspace.exp index 6c7142c99d8..7c365850f35 100644 --- a/gdb/testsuite/gdb.python/py-progspace.exp +++ b/gdb/testsuite/gdb.python/py-progspace.exp @@ -60,6 +60,9 @@ if {![runto_main]} { set pc_val [get_integer_valueof "\$pc" 0] gdb_py_test_silent_cmd "python blk =3D gdb.current_progspace ().block_for_= pc (${pc_val})" \ "get block for the current \$pc" 1 +gdb_py_test_silent_cmd \ + "python blk =3D gdb.current_progspace ().block_for_pc (gdb.Value(${pc_= val}))" \ + "get block for the current \$pc as value" 1 gdb_test "python print (blk.start <=3D ${pc_val})" "True" \ "block start is before \$pc" gdb_test "python print (blk.end >=3D ${pc_val})" "True" \ diff --git a/gdb/testsuite/gdb.python/py-shared.exp b/gdb/testsuite/gdb.pyt= hon/py-shared.exp index 2d3390284fe..7075bc5ee36 100644 --- a/gdb/testsuite/gdb.python/py-shared.exp +++ b/gdb/testsuite/gdb.python/py-shared.exp @@ -57,7 +57,10 @@ runto [gdb_get_line_number "Break to end."] # Test gdb.solib_name gdb_test "p &func1" "" "func1 address" gdb_py_test_silent_cmd "python func1 =3D gdb.history(0)" "Aquire func1 add= ress" 1 -gdb_test "python print (gdb.solib_name(int(func1)))" "py-shared-sl.sl" "te= st func1 solib location" +gdb_test "python print (gdb.solib_name(int(func1)))" "py-shared-sl.sl" \ + "test func1 solib location" +gdb_test "python print (gdb.solib_name(func1))" "py-shared-sl.sl" \ + "test func1 solib location using Value" =20 gdb_test "p &main" "" "main address" gdb_py_test_silent_cmd "python main =3D gdb.history(0)" "Aquire main addre= ss" 1 diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python= /python.exp index 7e0d4cb0c58..8c0da6daa26 100644 --- a/gdb/testsuite/gdb.python/python.exp +++ b/gdb/testsuite/gdb.python/python.exp @@ -514,6 +514,9 @@ gdb_test "python print (pc_rtn > pc_call)" "True" \ =20 gdb_test "python print (gdb.find_pc_line(pc_rtn).line >=3D line)" "True" \ "test find_pc_line with resume address" +gdb_test "python print (gdb.find_pc_line(pc_rtn).line =3D=3D gdb.find_pc_l= ine(gdb.Value(pc_rtn)).line)" \ + "True" \ + "test find_pc_line using Value" =20 gdb_test_no_output "set variable \$cvar1 =3D 23" "set convenience variable" gdb_test "python print(gdb.convenience_variable('cvar1'))" "23"