From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by sourceware.org (Postfix) with ESMTPS id 3EF2A3858438 for ; Sat, 11 Sep 2021 12:02:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 3EF2A3858438 Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 78EF71FED0 for ; Sat, 11 Sep 2021 12:02:57 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 631F21332A for ; Sat, 11 Sep 2021 12:02:57 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id VJgsF/GaPGHjQQAAGKfGzw (envelope-from ) for ; Sat, 11 Sep 2021 12:02:57 +0000 Date: Sat, 11 Sep 2021 14:02:56 +0200 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH][gdb/testsuite] Reimplement gdb.gdb/python-interrupts.exp as unittest Message-ID: <20210911120254.GA19083@delia.home> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Sat, 11 Sep 2021 12:02:59 -0000 Hi, The test-case gdb.gdb/python-interrupts.exp: - runs to captured_command_loop - sets a breakpoint at set_active_ext_lang - calls a python command - verifies the command triggers the breakpoint - sends a signal and verifies the result The test-case is fragile, because (f.i. with -flto) it cannot be guaranteed that captured_command_loop and set_active_ext_lang are available for setting breakpoints. Reimplement the test-case as unittest, using: - execute_command_to_string to capture the output - try/catch to catch the "Error while executing Python code" exception - a new hook selftests::hook_set_active_ext_lang to raise the signal Tested on x86_64-linux. Any comments? Thanks, - Tom [gdb/testsuite] Reimplement gdb.gdb/python-interrupts.exp as unittest --- gdb/extension.c | 12 +++++++++++ gdb/python/python.c | 62 ++++++++++++++++++++++++++++++++++++++++------------- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/gdb/extension.c b/gdb/extension.c index 27dce9befa0..3edecb21d6b 100644 --- a/gdb/extension.c +++ b/gdb/extension.c @@ -682,6 +682,13 @@ install_gdb_sigint_handler (struct signal_handler *previous) previous->handler_saved = 0; } +#if GDB_SELF_TEST +namespace selftests { +extern void (*hook_set_active_ext_lang) (void); +void (*hook_set_active_ext_lang) (void) = nullptr; +} +#endif + /* Set the currently active extension language to NOW_ACTIVE. The result is a pointer to a malloc'd block of memory to pass to restore_active_ext_lang. @@ -708,6 +715,11 @@ install_gdb_sigint_handler (struct signal_handler *previous) struct active_ext_lang_state * set_active_ext_lang (const struct extension_language_defn *now_active) { +#if GDB_SELF_TEST + if (selftests::hook_set_active_ext_lang) + selftests::hook_set_active_ext_lang (); +#endif + struct active_ext_lang_state *previous = XCNEW (struct active_ext_lang_state); diff --git a/gdb/python/python.c b/gdb/python/python.c index 34c1be76a16..b17b6b69268 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -1883,6 +1883,14 @@ do_start_initialization () #if GDB_SELF_TEST namespace selftests { +extern void (*hook_set_active_ext_lang) (void); + +static void +raise_sigint (void) +{ + raise (SIGINT); +} + /* Entry point for python unit tests. */ static void @@ -1897,21 +1905,45 @@ test_python () output.clear (); bool saw_exception = false; - scoped_restore reset_gdb_python_initialized - = make_scoped_restore (&gdb_python_initialized, 0); - try - { - CMD (output); - } - catch (const gdb_exception &e) - { - saw_exception = true; - SELF_CHECK (e.reason == RETURN_ERROR); - SELF_CHECK (e.error == GENERIC_ERROR); - SELF_CHECK (*e.message == "Python not initialized"); - } - SELF_CHECK (saw_exception); - SELF_CHECK (output.empty ()); + { + scoped_restore reset_gdb_python_initialized + = make_scoped_restore (&gdb_python_initialized, 0); + try + { + CMD (output); + } + catch (const gdb_exception &e) + { + saw_exception = true; + SELF_CHECK (e.reason == RETURN_ERROR); + SELF_CHECK (e.error == GENERIC_ERROR); + SELF_CHECK (*e.message == "Python not initialized"); + } + SELF_CHECK (saw_exception); + SELF_CHECK (output.empty ()); + } + + saw_exception = false; + { + scoped_restore save_hook + = make_scoped_restore (&hook_set_active_ext_lang, raise_sigint); + try + { + CMD (output); + } + catch (const gdb_exception &e) + { + saw_exception = true; + SELF_CHECK (e.reason == RETURN_ERROR); + SELF_CHECK (e.error == GENERIC_ERROR); + SELF_CHECK (*e.message == "Error while executing Python code."); + } + SELF_CHECK (saw_exception); + std::string ref_output("Traceback (most recent call last):\n" + " File \"\", line 1, in \n" + "KeyboardInterrupt\n"); + SELF_CHECK (output == ref_output); + } #undef CMD }