From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mgamail.intel.com (mgamail.intel.com [192.55.52.151]) by sourceware.org (Postfix) with ESMTPS id 1472E3858C52 for ; Tue, 14 Nov 2023 11:07:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1472E3858C52 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1472E3858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=192.55.52.151 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699960041; cv=none; b=un33G5a0BkwF2fkBT8bGZEyBLVsXBdrKSPCS13bHmrFln/mZNx8RoT3fOMWsrG+9DID+P9H+bcJ42pPj3NTJATuB9BiHBHjQk9hJWHoh4hZFj0KBoMYU8r2NZEfLVhjM2LbpLmQaHWob2Qdos5TiFrj70B+6eWov4QqbRlsdZJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1699960041; c=relaxed/simple; bh=8RmjEraGO+sIW8HvRuE8Wj63ldr8ZKvWrvm+de0a/cw=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=bgeN13HQPJQoSkPRl8DUQBXcob3csnZy+2iNtS2Pq4iYz4jakdeNqCN3jutAV6REjiNdS1mRqjzUMyzPigqW2F2ZsW2Y5xAADYJq+5xdI0XlDY40oRr3vFBBH7BREF3SVl7Egn/KQlNJIVLF96nnB8TZmvSzX2Fdl9RSgcSHHGE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1699960038; x=1731496038; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=8RmjEraGO+sIW8HvRuE8Wj63ldr8ZKvWrvm+de0a/cw=; b=SOj66KACiEaV58ADu5Y9S7ElDziIKOZn3OPomIDWjRnHHKLbefAoxUre dd7xc750bG7jtAiFB3Aco/LZ2d4ScIyb2UESFj3kn5AqotSpZ6FsTk/Kh u38ed84JDsyBd7gXZ3iG7BlaVpnNnrPjCMMBKDs3dKagEyBkc4st+T611 Bf1OwNFPcx1aei4VigbmqvF/FIwm9w2bay91gyzdxGfTOsfkWpG5HTir1 TtWVW0O/maUzlHGMClp/EVOwKiewDF8DyekLs4BVNgrefAwQnFObQXAIL KkAUtkvdRhxMfX7a258kvS8yi5E31ax8/zupogf3wkVE+z2fYQLMtfI+I Q==; X-IronPort-AV: E=McAfee;i="6600,9927,10893"; a="370830003" X-IronPort-AV: E=Sophos;i="6.03,301,1694761200"; d="scan'208";a="370830003" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga107.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Nov 2023 03:07:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10893"; a="793757165" X-IronPort-AV: E=Sophos;i="6.03,301,1694761200"; d="scan'208";a="793757165" Received: from gkldtt-dev-004.igk.intel.com (HELO localhost) ([10.123.221.202]) by orsmga008-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 14 Nov 2023 03:07:01 -0800 From: Tankut Baris Aktemur To: gdb-patches@sourceware.org Cc: blarsen@redhat.com, tom@tromey.com, aburgess@redhat.com Subject: [PATCH v4] gdb, python: selectively omit enabling stdin in gdb.execute Date: Tue, 14 Nov 2023 12:06:35 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230331081114.1319992-1-tankut.baris.aktemur@intel.com> References: <20230331081114.1319992-1-tankut.baris.aktemur@intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-10.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,SPF_HELO_NONE,SPF_NONE,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 List-Id: ========= Hi, The previous revision (v3) of this patch can be found at https://sourceware.org/pipermail/gdb-patches/2023-March/198508.html Changes in this version: * Rebased on the current master. * Added a new test scenario in gdb.python/py-cmd-prompt.exp. * The new scenario made me add a new boolean field to `struct ui` in ui.h. Regards, Baris ========= >From the Python API, we can execute GDB commands via gdb.execute. If the command gives an exception, however, we need to recover the GDB prompt and enable stdin, because the exception does not reach top-level GDB or normal_stop. This was done in commit commit 1ba1ac88011703abcd0271e4f5d00927dc69a09a Author: Andrew Burgess Date: Tue Nov 19 11:17:20 2019 +0000 gdb: Enable stdin on exception in execute_gdb_command However, we face a glitch if the Python side executes the command in a context where GDB had already disabled stdin, because it was running a synchronous execution command such as "continue" or "run". As an example, suppose we have the following objfile event listener, specified in a file named file.py: ~~~ import gdb class MyListener: def __init__(self): gdb.events.new_objfile.connect(self.handle_new_objfile_event) self.processed_objfile = False def handle_new_objfile_event(self, event): if self.processed_objfile: return print("loading " + event.new_objfile.filename) self.processed_objfile = True gdb.execute("print a") the_listener = MyListener() ~~~ The executed command "print a", gives an error because "a" is not defined. We use the listener as follows: $ gdb -q -ex "source file.py" -ex "run" --args a.out Reading symbols from /tmp/a.out... Starting program: /tmp/a.out loading /lib64/ld-linux-x86-64.so.2 Python Exception : No symbol "a" in current context. (gdb) [Inferior 1 (process 3980401) exited normally] Note how the GDB prompt comes inbetween the exception message and the inferior's exit message. We have this obscure behavior, because GDB continues to execute its flow after emitting the Python event. In this case, GDB would enable stdin in the normal way. Hence, we do not need to explicitly enable stdin in execute_gdb_command when an exception occurs. A similar problem occurs also when the command completes without exception, but if it enables stdin upon completion. The "target remote" command is an example for such case. For more details of the scenario, see the test case added by this patch. As a solution, we track whether the prompt was already blocked. If so, we leave enabling stdin to GDB. With this patch, we see $ gdb -q -ex "source file.py" -ex "run" --args a.out Reading symbols from /tmp/a.out... Starting program: /tmp/a.out loading /lib64/ld-linux-x86-64.so.2 Python Exception : No symbol "a" in current context. [Inferior 1 (process 3984511) exited normally] (gdb) Regression-tested on X86_64 Linux using the default board file (i.e. unix). Co-Authored-By: Oguzhan Karakaya Reviewed-By: Guinevere Larsen --- gdb/event-top.c | 3 +- gdb/python/python.c | 29 ++++++++++ gdb/testsuite/gdb.python/py-cmd-exception.c | 22 ++++++++ gdb/testsuite/gdb.python/py-cmd-exception.exp | 43 +++++++++++++++ gdb/testsuite/gdb.python/py-cmd-exception.py | 33 +++++++++++ gdb/testsuite/gdb.python/py-cmd-prompt.c | 22 ++++++++ gdb/testsuite/gdb.python/py-cmd-prompt.exp | 55 +++++++++++++++++++ gdb/testsuite/gdb.python/py-cmd-prompt.py | 36 ++++++++++++ gdb/ui.h | 5 ++ 9 files changed, 247 insertions(+), 1 deletion(-) create mode 100644 gdb/testsuite/gdb.python/py-cmd-exception.c create mode 100644 gdb/testsuite/gdb.python/py-cmd-exception.exp create mode 100644 gdb/testsuite/gdb.python/py-cmd-exception.py create mode 100644 gdb/testsuite/gdb.python/py-cmd-prompt.c create mode 100644 gdb/testsuite/gdb.python/py-cmd-prompt.exp create mode 100644 gdb/testsuite/gdb.python/py-cmd-prompt.py diff --git a/gdb/event-top.c b/gdb/event-top.c index 9886ca46e7b..c24717eb2f0 100644 --- a/gdb/event-top.c +++ b/gdb/event-top.c @@ -508,7 +508,8 @@ async_enable_stdin (void) { struct ui *ui = current_ui; - if (ui->prompt_state == PROMPT_BLOCKED) + if (ui->prompt_state == PROMPT_BLOCKED + && !ui->keep_prompt_blocked) { target_terminal::ours (); ui->register_file_handler (); diff --git a/gdb/python/python.c b/gdb/python/python.c index d569fb5a3e4..eef0017e407 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -658,6 +658,35 @@ execute_gdb_command (PyObject *self, PyObject *args, PyObject *kw) scoped_restore preventer = prevent_dont_repeat (); + /* If the executed command raises an exception, we may have to + enable stdin and recover the GDB prompt. + + Stdin should not be re-enabled if it is already blocked because, + for example, we are running a command in the context of a + synchronous execution command ("run", "continue", etc.). Like + this: + + User runs "continue" + --> command blocks the prompt + --> Python API is invoked, e.g. via events + --> gdb.execute(C) invoked inside Python + --> command C raises an exception + + In this case case, GDB would go back to the top "continue" command + and move on with its normal course of execution. That is, it + would enable stdin in the way it normally does. + + Similarly, if the command we are about to execute enables the + stdin while we are still in the context of a synchronous + execution command, we would be displaying the prompt too early, + before the surrounding command completes. + + For these reasons, we keep the prompt blocked, if it already is. */ + bool prompt_was_blocked = (current_ui->prompt_state == PROMPT_BLOCKED); + scoped_restore save_prompt_state + = make_scoped_restore (¤t_ui->keep_prompt_blocked, + prompt_was_blocked); + try { gdbpy_allow_threads allow_threads; diff --git a/gdb/testsuite/gdb.python/py-cmd-exception.c b/gdb/testsuite/gdb.python/py-cmd-exception.c new file mode 100644 index 00000000000..f151960ce3a --- /dev/null +++ b/gdb/testsuite/gdb.python/py-cmd-exception.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int +main () +{ + return 0; +} diff --git a/gdb/testsuite/gdb.python/py-cmd-exception.exp b/gdb/testsuite/gdb.python/py-cmd-exception.exp new file mode 100644 index 00000000000..3b81131ee08 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-cmd-exception.exp @@ -0,0 +1,43 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. It tests a corner case where +# the executed GDB command gives an exception and enabling the stdin would +# cause the GDB prompt to be displayed prematurely. + +load_lib gdb-python.exp + +require !use_gdb_stub allow_python_tests + +standard_testfile + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return -1 +} + +set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py] +gdb_test_no_output "source $pyfile" "source the script" + +gdb_start_cmd + +gdb_test_multiple "" "check the prompt" { + -re "breakpoint $decimal, main .*\r\n$gdb_prompt $" { + # The prompt is positioned correctly. + pass $gdb_test_name + } + -re "No symbol \"a\" in current context.\r\n$gdb_prompt " { + fail $gdb_test_name + } +} diff --git a/gdb/testsuite/gdb.python/py-cmd-exception.py b/gdb/testsuite/gdb.python/py-cmd-exception.py new file mode 100644 index 00000000000..51199bd3fe3 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-cmd-exception.py @@ -0,0 +1,33 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import gdb + +class MyListener: + def __init__(self): + gdb.events.new_objfile.connect(self.handle_new_objfile_event) + self.processed_objfile = False + + def handle_new_objfile_event(self, event): + if self.processed_objfile: + return + + print('loading ' + event.new_objfile.filename) + self.processed_objfile = True + + # There is no variable 'a'. The command raises an exception. + gdb.execute('print a') + +the_listener = MyListener() diff --git a/gdb/testsuite/gdb.python/py-cmd-prompt.c b/gdb/testsuite/gdb.python/py-cmd-prompt.c new file mode 100644 index 00000000000..6956ba4858d --- /dev/null +++ b/gdb/testsuite/gdb.python/py-cmd-prompt.c @@ -0,0 +1,22 @@ +/* This testcase is part of GDB, the GNU debugger. + + Copyright 2023 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . */ + +int +main () +{ + return 0; /* break-here */ +} diff --git a/gdb/testsuite/gdb.python/py-cmd-prompt.exp b/gdb/testsuite/gdb.python/py-cmd-prompt.exp new file mode 100644 index 00000000000..968f5603fc1 --- /dev/null +++ b/gdb/testsuite/gdb.python/py-cmd-prompt.exp @@ -0,0 +1,55 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# This file is part of the GDB testsuite. It tests a corner case +# where the executed GDB command enables the stdin while running +# inside a synchronous command, causing the GDB prompt to be displayed +# prematurely. + +load_lib gdb-python.exp +load_lib gdbserver-support.exp + +# We use the start command. +require !use_gdb_stub +require allow_python_tests allow_gdbserver_tests + +standard_testfile + +if { [prepare_for_testing "failed to prepare" ${testfile} ${srcfile}] } { + return -1 +} + +set bp_line [gdb_get_line_number "break-here"] +gdb_breakpoint $bp_line + +set pyfile [gdb_remote_download host ${srcdir}/${subdir}/${testfile}.py] +gdb_test_no_output "source $pyfile" "source the script" + +set gdbserver [gdbserver_start "" [standard_output_file $binfile]] +set gdbserver_gdbport [lindex $gdbserver 1] +gdb_test_no_output "python the_listener.port = '${gdbserver_gdbport}'" + +gdb_run_cmd + +gdb_test_multiple "" "prompt is positioned correctly" { + -re -wrap "break-here \[^\r\n\]+" { + pass $gdb_test_name + } +} + +# Clean up the gdbserver. +gdb_test "inferior 2" "Switching to inferior 2.*" \ + "switch to gdbserver for clean up" +gdbserver_exit 0 diff --git a/gdb/testsuite/gdb.python/py-cmd-prompt.py b/gdb/testsuite/gdb.python/py-cmd-prompt.py new file mode 100644 index 00000000000..a920309d39e --- /dev/null +++ b/gdb/testsuite/gdb.python/py-cmd-prompt.py @@ -0,0 +1,36 @@ +# Copyright (C) 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import gdb + +class MyListener: + def __init__(self): + gdb.events.new_objfile.connect(self.handle_new_objfile_event) + self.processed_objfile = False + self.port = "uninitialized" + + def handle_new_objfile_event(self, event): + if self.processed_objfile: + return + + print('loading ' + event.new_objfile.filename) + self.processed_objfile = True + + gdb.execute('add-inferior -no-connection') + gdb.execute('inferior 2') + gdb.execute('target remote ' + self.port) + gdb.execute('inferior 1') + +the_listener = MyListener() diff --git a/gdb/ui.h b/gdb/ui.h index ed75e041e5f..4303d11c58c 100644 --- a/gdb/ui.h +++ b/gdb/ui.h @@ -135,6 +135,11 @@ struct ui /* See enum prompt_state's description. */ enum prompt_state prompt_state = PROMPT_NEEDED; + /* Whether the prompt should be kept blocked. This is useful to not + recover the prompt too early in the context of nested command + execution. */ + bool keep_prompt_blocked = false; + /* The fields below that start with "m_" are "private". They're meant to be accessed through wrapper macros that make them look like globals. */ -- 2.34.1 Intel Deutschland GmbH Registered Address: Am Campeon 10, 85579 Neubiberg, Germany Tel: +49 89 99 8853-0, www.intel.de Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva Chairperson of the Supervisory Board: Nicole Lau Registered Office: Munich Commercial Register: Amtsgericht Muenchen HRB 186928