From patchwork Fri Feb 23 21:11:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 86311 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9ECFD3858287 for ; Fri, 23 Feb 2024 21:13:07 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2d.google.com (mail-io1-xd2d.google.com [IPv6:2607:f8b0:4864:20::d2d]) by sourceware.org (Postfix) with ESMTPS id 492DC3858407 for ; Fri, 23 Feb 2024 21:11:40 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 492DC3858407 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 492DC3858407 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708722704; cv=none; b=utDdWskCd+40bpDp8hxqfpf8o+ZGS9LPzUpG9G9OQJj8y9F+5BcS8cu1sq6BLK8vzStf5W3uGDigoUIwO6HWGmM5+CSW+rCGD7yEHJQ3uifmIJeKahey+iFGUJTR5DMOAfUbidQFx2dGVF6GmgcKuUMkf9CYOlVHTctXHvjov3c= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708722704; c=relaxed/simple; bh=arWxRr0MVpxjQxpR7X381xUYNpw6uFdHG8ytU2ZvaTY=; h=DKIM-Signature:From:Date:Subject:MIME-Version:Message-Id:To; b=rHclSPho0fyx800tZffTv2Q2vKec+DJ/d39q6joxNF1APHmVhj8Jgwexaz9KVmlEKQC4KFdxfLpOrrT9cuqYsp9lPYzGmj14othFhsroSl7kqutUwG0ohWmfZKw5pJkmXkMf7aFbA4VXz7Gs3EYgS5iGPuT7kHGILpMmD7Z6D6U= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2d.google.com with SMTP id ca18e2360f4ac-7c7894b39edso31955739f.3 for ; Fri, 23 Feb 2024 13:11:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1708722699; x=1709327499; darn=sourceware.org; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=U7VScV6pmXTXygJqEHpgN+ls9E6KiA7Xx2zo6LpPWIg=; b=CUpjZlVBZaPGtwsRICKh6Hycqc/DwHF3HYoRBVC7wTy409xJmxrDcyBs57vWcgAR7g ndUGkvZj7l0i36FSz6sJNbSQD5hK1NuppTsm6bm0jl1lW198epQaisYaz3EVbAvtKEd9 nBU4x3wuP7ss3wTCBLaZfnNGF4oVbPwQI24ZPMB4uryV97uEQMTQ+DadKXzm4bss55Vc Ph0EKJAmB/tjYYR6w1YXAVkhi9CFPHtf3Ddog5y1g7MEYxDjdZgZt5nzxKrgG2FfXeb0 ykhy1MbErEKgic/4KMI9RWX5n+x/WG7kfkuTe+D1AFkKKWXQX0258d2Yd/ik8qKiXfId rrxA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708722699; x=1709327499; h=to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=U7VScV6pmXTXygJqEHpgN+ls9E6KiA7Xx2zo6LpPWIg=; b=vwyzoQNmx14gsazSWgDLTVtQs0us07WMMDk3yc7lAHzkHoXjQsufiLxBQcxeeuGe5u F4mK91q9mnj9VoSJNs+j6IPtzkql7uZqpD9AXBd/ehEWiMO5iHoIyz7fNkCCP+SlNHbX YU2OuLj/d/m3wc6LzDnB+tWLfBzRTg8zwGbjToGZMJXZNrZwbatMN+6ArA2MCVXGi7If OqxKbdWQLaoqcu+/75ofofTPRtoX30PgjXWd8jxOgf2q3Jn3lJ5VRNfweSoHop/hsR5y nCKA1c3yfyJswfm+/Ycb7bOp+hzFdtY+c1ABZcfu2w8lcJiGOfukP/8VGy2BTHGP+UdP JMVw== X-Gm-Message-State: AOJu0YxRP7WJ5IzLSBhNLh5gZQ+xI7+H682C1axAabubywNVW5886z1c F7R2hUPYhCUCXsWcnFe72YcVUx+K2nVjKJzmPvF3T3xY7bMFTn/vpnFuMevfkgdXa+tuYf2UBXg = X-Google-Smtp-Source: AGHT+IGoVoUvdf0x79oZPrXM+/YZT75nVE4DVTfj7qOQYexbtDruIa7stys9D1Mba0FR2IFbJhAFVQ== X-Received: by 2002:a6b:f014:0:b0:7c7:97ad:91fc with SMTP id w20-20020a6bf014000000b007c797ad91fcmr1271916ioc.1.1708722699431; Fri, 23 Feb 2024 13:11:39 -0800 (PST) Received: from localhost.localdomain (71-211-170-195.hlrn.qwest.net. [71.211.170.195]) by smtp.gmail.com with ESMTPSA id y14-20020a02c00e000000b004741aec6cdasm3497851jai.25.2024.02.23.13.11.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 23 Feb 2024 13:11:39 -0800 (PST) From: Tom Tromey Date: Fri, 23 Feb 2024 14:11:38 -0700 Subject: [PATCH 2/5] Add extension_language_ops::shutdown MIME-Version: 1.0 Message-Id: <20240223-final-cleanups-v1-2-84d5271e9979@adacore.com> References: <20240223-final-cleanups-v1-0-84d5271e9979@adacore.com> In-Reply-To: <20240223-final-cleanups-v1-0-84d5271e9979@adacore.com> To: gdb-patches@sourceware.org X-Mailer: b4 0.12.4 X-Spam-Status: No, score=-11.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, 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.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces+patchwork=sourceware.org@sourceware.org Right now, Python is shut down via a final cleanup. However, it seems to me that it is better for extension languages to be shut down explicitly, after all the ordinary final cleanups are run. The main reason for this is that a subsequent patch adds another case like finalize_values; and rather than add a series of workarounds for Python shutdown, it seemed better to let these be done via final cleanups, and then have Python shutdown itself be the special case. --- gdb/extension-priv.h | 4 ++++ gdb/extension.c | 12 ++++++++++++ gdb/extension.h | 3 +++ gdb/guile/guile.c | 1 + gdb/python/python.c | 6 +++--- gdb/top.c | 2 ++ 6 files changed, 25 insertions(+), 3 deletions(-) diff --git a/gdb/extension-priv.h b/gdb/extension-priv.h index b709494927d..cb00cb6ff7b 100644 --- a/gdb/extension-priv.h +++ b/gdb/extension-priv.h @@ -119,6 +119,10 @@ struct extension_language_ops This method is required. */ int (*initialized) (const struct extension_language_defn *); + /* Called just before GDB exits. This shuts down the extension + language. This can be NULL. */ + void (*shutdown) (const struct extension_language_defn *); + /* Process a sequence of commands embedded in GDB's own scripting language. E.g., python diff --git a/gdb/extension.c b/gdb/extension.c index 42e05199d2c..9f403500497 100644 --- a/gdb/extension.c +++ b/gdb/extension.c @@ -341,6 +341,18 @@ ext_lang_initialization (void) } } +/* See extension.h. */ + +void +ext_lang_shutdown () +{ + for (const struct extension_language_defn *extlang : extension_languages) + { + if (extlang->ops != nullptr && extlang->ops->shutdown != nullptr) + extlang->ops->shutdown (extlang); + } +} + /* Invoke the appropriate extension_language_ops.eval_from_control_command method to perform CMD, which is a list of commands in an extension language. diff --git a/gdb/extension.h b/gdb/extension.h index 0514d7930a2..5260bcbde00 100644 --- a/gdb/extension.h +++ b/gdb/extension.h @@ -282,6 +282,9 @@ extern bool ext_lang_auto_load_enabled (const struct extension_language_defn *); extern void ext_lang_initialization (void); +/* Shut down all extension languages. */ +extern void ext_lang_shutdown (); + extern void eval_ext_lang_from_control_command (struct command_line *cmd); extern void auto_load_ext_lang_scripts_for_objfile (struct objfile *); diff --git a/gdb/guile/guile.c b/gdb/guile/guile.c index 30d1c72fceb..f0db709e4fe 100644 --- a/gdb/guile/guile.c +++ b/gdb/guile/guile.c @@ -115,6 +115,7 @@ static const struct extension_language_ops guile_extension_ops = { gdbscm_initialize, gdbscm_initialized, + nullptr, gdbscm_eval_from_control_command, diff --git a/gdb/python/python.c b/gdb/python/python.c index 971fc850dbb..dde8afc3c65 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -110,6 +110,7 @@ static objfile_script_sourcer_func gdbpy_source_objfile_script; static objfile_script_executor_func gdbpy_execute_objfile_script; static void gdbpy_initialize (const struct extension_language_defn *); static int gdbpy_initialized (const struct extension_language_defn *); +static void finalize_python (const struct extension_language_defn *); static void gdbpy_eval_from_control_command (const struct extension_language_defn *, struct command_line *cmd); static void gdbpy_start_type_printers (const struct extension_language_defn *, @@ -147,6 +148,7 @@ static const struct extension_language_ops python_extension_ops = { gdbpy_initialize, gdbpy_initialized, + finalize_python, gdbpy_eval_from_control_command, @@ -2070,7 +2072,7 @@ static struct cmd_list_element *user_show_python_list; interpreter. This lets Python's 'atexit' work. */ static void -finalize_python () +finalize_python (const struct extension_language_defn *ignore) { struct active_ext_lang_state *previous_active; @@ -2310,8 +2312,6 @@ do_start_initialization () /* Release the GIL while gdb runs. */ PyEval_SaveThread (); - add_final_cleanup (finalize_python); - /* Only set this when initialization has succeeded. */ gdb_python_initialized = 1; return true; diff --git a/gdb/top.c b/gdb/top.c index 5114713baa4..67d6670cd9c 100644 --- a/gdb/top.c +++ b/gdb/top.c @@ -1819,6 +1819,8 @@ quit_force (int *exit_arg, int from_tty) exception_print (gdb_stderr, ex); } + ext_lang_shutdown (); + exit (exit_code); }