From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from alt-proxy28.mail.unifiedlayer.com (alt-proxy28.mail.unifiedlayer.com [74.220.216.123]) by sourceware.org (Postfix) with ESMTPS id CC1B93857B9E for ; Sun, 5 Jun 2022 16:17:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CC1B93857B9E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=tromey.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tromey.com Received: from cmgw14.mail.unifiedlayer.com (unknown [10.0.90.129]) by progateway1.mail.pro1.eigbox.com (Postfix) with ESMTP id 3C0331003FED2 for ; Sun, 5 Jun 2022 16:17:59 +0000 (UTC) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with ESMTP id xswwnrbYKhWk0xswwnVLt4; Sun, 05 Jun 2022 16:17:59 +0000 X-Authority-Reason: nr=8 X-Authority-Analysis: v=2.4 cv=IrfbzJzg c=1 sm=1 tr=0 ts=629cd737 a=ApxJNpeYhEAb1aAlGBBbmA==:117 a=ApxJNpeYhEAb1aAlGBBbmA==:17 a=dLZJa+xiwSxG16/P+YVxDGlgEgI=:19 a=JPEYwPQDsx4A:10:nop_rcvd_month_year a=Qbun_eYptAEA:10:endurance_base64_authed_username_1 a=CCpqsmhAAAAA:8 a=1f6Wc1VY7HjHiQw_vFQA:9 a=ul9cdbp4aOFLsgKbc677:22 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=Content-Transfer-Encoding:MIME-Version:Message-Id:Date:Subject: Cc:To:From:Sender:Reply-To:Content-Type:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: In-Reply-To:References:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive; bh=gbeuEDnYoYfT0GKMxfx+mrhSQaYtEZyB9dKv4xTzaOc=; b=jpUqyQNre8wpHlM1TtjH7LNLXa o0UdpSE/tcpPWUNng/2mWu/O8pULozovsWTZVaLzDyukHjmfr16jawT2XkINMKwrGYzBB+JC73tb7 52QekSQnkIF4ACF95Wp+5qyRL; Received: from 71-211-171-143.hlrn.qwest.net ([71.211.171.143]:45638 helo=prentzel.Home) by box5379.bluehost.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1nxsww-000WT1-HH; Sun, 05 Jun 2022 10:17:58 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Accept gdb.Value in more Python APIs Date: Sun, 5 Jun 2022 10:17:56 -0600 Message-Id: <20220605161756.1593949-1-tom@tromey.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - box5379.bluehost.com X-AntiAbuse: Original Domain - sourceware.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - tromey.com X-BWhitelist: no X-Source-IP: 71.211.171.143 X-Source-L: No X-Exim-ID: 1nxsww-000WT1-HH X-Source: X-Source-Args: X-Source-Dir: X-Source-Sender: 71-211-171-143.hlrn.qwest.net (prentzel.Home) [71.211.171.143]:45638 X-Source-Auth: tom+tromey.com X-Email-Count: 2 X-Source-Cap: ZWx5bnJvYmk7ZWx5bnJvYmk7Ym94NTM3OS5ibHVlaG9zdC5jb20= X-Local-Domain: yes X-Spam-Status: No, score=-3030.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, JMQ_SPF_NEUTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 05 Jun 2022 16:18:01 -0000 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. 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. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=27000 --- 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[] = { "start_pc", "end_pc", "count", NULL }; - CORE_ADDR start, end = 0; + CORE_ADDR start = 0, end = 0; CORE_ADDR pc; - gdb_py_ulongest start_temp; long count = 0, i; - PyObject *end_obj = NULL, *count_obj = NULL; + PyObject *start_obj = nullptr, *end_obj = nullptr, *count_obj = nullptr; struct gdbarch *gdbarch = NULL; ARCHPY_REQUIRE_VALID (self, gdbarch); - 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; - start = 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 = 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 = 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; - return NULL; - } + if (end_obj != nullptr) + { + if (get_addr_from_python (end_obj, &end) < 0) + return nullptr; if (end < start) { diff --git a/gdb/python/py-progspace.c b/gdb/python/py-progspace.c index 5657ef7756c..b94f39ae80c 100644 --- a/gdb/python/py-progspace.c +++ b/gdb/python/py-progspace.c @@ -352,13 +352,17 @@ static PyObject * pspy_solib_name (PyObject *o, PyObject *args) { char *soname; - gdb_py_ulongest pc; + CORE_ADDR pc; + PyObject *pc_obj; + pspace_object *self = (pspace_object *) o; PSPY_REQUIRE_VALID (self); - 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; soname = solib_name_from_address (self->pspace, pc); if (soname == nullptr) @@ -372,14 +376,17 @@ static PyObject * pspy_block_for_pc (PyObject *o, PyObject *args) { pspace_object *self = (pspace_object *) o; - gdb_py_ulongest pc; + CORE_ADDR pc; + PyObject *pc_obj; const struct block *block = NULL; struct compunit_symtab *cust = NULL; PSPY_REQUIRE_VALID (self); - 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; try { @@ -411,24 +418,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 = NULL; /* init for gcc -Wall */ + PyObject *pc_obj; pspace_object *self = (pspace_object *) o; PSPY_REQUIRE_VALID (self); - 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; try { struct symtab_and_line sal; - CORE_ADDR pc; scoped_restore_current_program_space saver; set_current_program_space (self->pspace); - pc = (CORE_ADDR) pc_llu; sal = find_pc_line (pc, 0); result = symtab_and_line_to_sal_object (sal); } diff --git a/gdb/testsuite/gdb.python/py-arch.exp b/gdb/testsuite/gdb.python/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 = arch.disassemble(pc, pc)" \ "disassemble no count" 0 gdb_py_test_silent_cmd "python insn_list3 = arch.disassemble(pc, count=1)" \ "disassemble no end" 0 -gdb_py_test_silent_cmd "python insn_list4 = arch.disassemble(pc)" \ +gdb_py_test_silent_cmd "python insn_list4 = arch.disassemble(gdb.Value(pc))" \ "disassemble no end no count" 0 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 = gdb.current_progspace ().block_for_pc (${pc_val})" \ "get block for the current \$pc" 1 +gdb_py_test_silent_cmd \ + "python blk = 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 <= ${pc_val})" "True" \ "block start is before \$pc" gdb_test "python print (blk.end >= ${pc_val})" "True" \ diff --git a/gdb/testsuite/gdb.python/py-shared.exp b/gdb/testsuite/gdb.python/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 = gdb.history(0)" "Aquire func1 address" 1 -gdb_test "python print (gdb.solib_name(int(func1)))" "py-shared-sl.sl" "test 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" gdb_test "p &main" "" "main address" gdb_py_test_silent_cmd "python main = gdb.history(0)" "Aquire main address" 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" \ gdb_test "python print (gdb.find_pc_line(pc_rtn).line >= line)" "True" \ "test find_pc_line with resume address" +gdb_test "python print (gdb.find_pc_line(pc_rtn).line == gdb.find_pc_line(gdb.Value(pc_rtn)).line)" \ + "True" \ + "test find_pc_line using Value" gdb_test_no_output "set variable \$cvar1 = 23" "set convenience variable" gdb_test "python print(gdb.convenience_variable('cvar1'))" "23" -- 2.34.1