From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) by sourceware.org (Postfix) with ESMTPS id 917DD38582A7 for ; Fri, 24 Jun 2022 10:52:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 917DD38582A7 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=palves.net Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f46.google.com with SMTP id e10so2572088wra.11 for ; Fri, 24 Jun 2022 03:52:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:subject:date:message-id:mime-version :content-transfer-encoding; bh=kEO03OjFgNJvbWoZNKoGHBKDoP8U9xVB6YmpVP3FWOI=; b=mAortPacrgPCeBtiy0rgbDsBYTmNPXhUR6mpn3CUOdKkRz/tRpG88+pYJ6qwpM7IqF gzuxsiwYfvvt/6naPDqRiacndZbmZnoPiiFZBdLDSw61+piLBL2m5/2KotdzxCXOV7sA WIubGZxE8Lgy+wMlrFEBdE3jA0zULoa789Lx26Gw9fMcyrFuAWNfZ+JLrv6pT8a7gM2c sIBydcHSBVwpnYuPCQFD7t6rufGF0v/uq/+uF6beucde48lQiVaiDBL/pAwSpJMfzzUS oS2Md2qUEmta/2lNtp6roKUSbmQQpgsi0qV1o14qzJD7j84SI+74D0ajl5SKV1OsQZ16 KjfQ== X-Gm-Message-State: AJIora8004Lnjk4LS8CRen0Ha1KQW1MGeEV7EMhvpAjSWf0xqYgQh60U Ezd23T0D3+5iFmUtNZfmZPIKYXsUOu4= X-Google-Smtp-Source: AGRyM1uBr2CPyN2U6eo1DUOIQnap8LR4Y8ADB2pct5v9XaBPqpqA32GKOwtwfz5SyxKR7jrD+5aQJw== X-Received: by 2002:a05:6000:15c6:b0:216:7615:f00d with SMTP id y6-20020a05600015c600b002167615f00dmr12855106wry.327.1656067966670; Fri, 24 Jun 2022 03:52:46 -0700 (PDT) Received: from localhost ([2001:8a0:f924:2600:209d:85e2:409e:8726]) by smtp.gmail.com with ESMTPSA id f11-20020a05600c4e8b00b0039c4945c753sm7420538wmq.39.2022.06.24.03.52.45 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 24 Jun 2022 03:52:45 -0700 (PDT) From: Pedro Alves To: gdb-patches@sourceware.org Subject: [PATCH] Eliminate TUI/CLI observers duplication Date: Fri, 24 Jun 2022 11:52:44 +0100 Message-Id: <20220624105244.941058-1-pedro@palves.net> X-Mailer: git-send-email 2.36.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-9.8 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, 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: Fri, 24 Jun 2022 10:52:51 -0000 For historical reasons, the CLI and the TUI observers are basically exact duplicates, except for the downcast: cli: struct cli_interp *cli = as_cli_interp (interp); tui: struct interp *tui = as_tui_interp (interp); and how they get at the interpreter's ui_out: cli: cli->cli_uiout tui: tui->interp_ui_out () Since interp_ui_out() is a virtual method that also works for the CLI interpreter, and, both the CLI and the TUI interpreters inherit from the same base class (cli_interp_base), we can convert the CLI observers to cast to cli_interp_base instead and use interp_ui_out() too. With that, the CLI observers will work for the TUI interpreter as well. This lets us completely eliminate the TUI observers. That's what this commit does. Change-Id: Iaf6cf12dfa200ed3ab203a895a72b69dfedbd6e0 --- gdb/cli/cli-interp.c | 127 ++++++++++++++--------------- gdb/tui/tui-interp.c | 190 +------------------------------------------ 2 files changed, 64 insertions(+), 253 deletions(-) diff --git a/gdb/cli/cli-interp.c b/gdb/cli/cli-interp.c index fe9e4b43f14..036bc723b24 100644 --- a/gdb/cli/cli-interp.c +++ b/gdb/cli/cli-interp.c @@ -70,13 +70,13 @@ cli_interp::~cli_interp () /* Suppress notification struct. */ struct cli_suppress_notification cli_suppress_notification; -/* Returns the INTERP's data cast as cli_interp if INTERP is a CLI, - and returns NULL otherwise. */ +/* Returns the INTERP's data cast as cli_interp_base if INTERP is a + console-like interpreter, and returns NULL otherwise. */ -static struct cli_interp * -as_cli_interp (struct interp *interp) +static cli_interp_base * +as_cli_interp_base (interp *interp) { - return dynamic_cast (interp); + return dynamic_cast (interp); } /* Longjmp-safe wrapper for "execute_command". */ @@ -117,12 +117,13 @@ should_print_stop_to_console (struct interp *console_interp, /* Observers for several run control events. If the interpreter is quiet (i.e., another interpreter is being run with - interpreter-exec), print nothing. */ + interpreter-exec), print nothing. These are named "cli_base" as + they print to both CLI interpreters and TUI interpreters. */ /* Observer for the normal_stop notification. */ static void -cli_on_normal_stop (struct bpstat *bs, int print_frame) +cli_base_on_normal_stop (struct bpstat *bs, int print_frame) { if (!print_frame) return; @@ -134,106 +135,98 @@ cli_on_normal_stop (struct bpstat *bs, int print_frame) SWITCH_THRU_ALL_UIS () { struct interp *interp = top_level_interpreter (); - struct cli_interp *cli = as_cli_interp (interp); - struct thread_info *thread; - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (interp); + if (cli == nullptr) continue; - thread = inferior_thread (); + thread_info *thread = inferior_thread (); if (should_print_stop_to_console (interp, thread)) - print_stop_event (cli->cli_uiout); + print_stop_event (cli->interp_ui_out ()); } } /* Observer for the signal_received notification. */ static void -cli_on_signal_received (enum gdb_signal siggnal) +cli_base_on_signal_received (enum gdb_signal siggnal) { SWITCH_THRU_ALL_UIS () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) continue; - print_signal_received_reason (cli->cli_uiout, siggnal); + print_signal_received_reason (cli->interp_ui_out (), siggnal); } } /* Observer for the end_stepping_range notification. */ static void -cli_on_end_stepping_range (void) +cli_base_on_end_stepping_range () { SWITCH_THRU_ALL_UIS () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) continue; - print_end_stepping_range_reason (cli->cli_uiout); + print_end_stepping_range_reason (cli->interp_ui_out ()); } } /* Observer for the signalled notification. */ static void -cli_on_signal_exited (enum gdb_signal siggnal) +cli_base_on_signal_exited (enum gdb_signal siggnal) { SWITCH_THRU_ALL_UIS () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) continue; - print_signal_exited_reason (cli->cli_uiout, siggnal); + print_signal_exited_reason (cli->interp_ui_out (), siggnal); } } /* Observer for the exited notification. */ static void -cli_on_exited (int exitstatus) +cli_base_on_exited (int exitstatus) { SWITCH_THRU_ALL_UIS () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) continue; - print_exited_reason (cli->cli_uiout, exitstatus); + print_exited_reason (cli->interp_ui_out (), exitstatus); } } /* Observer for the no_history notification. */ static void -cli_on_no_history (void) +cli_base_on_no_history () { SWITCH_THRU_ALL_UIS () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) continue; - print_no_history_reason (cli->cli_uiout); + print_no_history_reason (cli->interp_ui_out ()); } } /* Observer for the sync_execution_done notification. */ static void -cli_on_sync_execution_done (void) +cli_base_on_sync_execution_done () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) return; display_gdb_prompt (NULL); @@ -242,11 +235,10 @@ cli_on_sync_execution_done (void) /* Observer for the command_error notification. */ static void -cli_on_command_error (void) +cli_base_on_command_error () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) return; display_gdb_prompt (NULL); @@ -255,27 +247,26 @@ cli_on_command_error (void) /* Observer for the user_selected_context_changed notification. */ static void -cli_on_user_selected_context_changed (user_selected_what selection) +cli_base_on_user_selected_context_changed (user_selected_what selection) { /* This event is suppressed. */ if (cli_suppress_notification.user_selected_context) return; - thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : NULL; + thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : nullptr; SWITCH_THRU_ALL_UIS () { - struct cli_interp *cli = as_cli_interp (top_level_interpreter ()); - - if (cli == NULL) + cli_interp_base *cli = as_cli_interp_base (top_level_interpreter ()); + if (cli == nullptr) continue; if (selection & USER_SELECTED_INFERIOR) - print_selected_inferior (cli->cli_uiout); + print_selected_inferior (cli->interp_ui_out ()); - if (tp != NULL + if (tp != nullptr && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))) - print_selected_thread_frame (cli->cli_uiout, selection); + print_selected_thread_frame (cli->interp_ui_out (), selection); } } @@ -465,17 +456,21 @@ _initialize_cli_interp () { interp_factory_register (INTERP_CONSOLE, cli_interp_factory); - /* If changing this, remember to update tui-interp.c as well. */ - gdb::observers::normal_stop.attach (cli_on_normal_stop, "cli-interp"); - gdb::observers::end_stepping_range.attach (cli_on_end_stepping_range, - "cli-interp"); - gdb::observers::signal_received.attach (cli_on_signal_received, "cli-interp"); - gdb::observers::signal_exited.attach (cli_on_signal_exited, "cli-interp"); - gdb::observers::exited.attach (cli_on_exited, "cli-interp"); - gdb::observers::no_history.attach (cli_on_no_history, "cli-interp"); - gdb::observers::sync_execution_done.attach (cli_on_sync_execution_done, - "cli-interp"); - gdb::observers::command_error.attach (cli_on_command_error, "cli-interp"); + /* Note these all work for both the CLI and TUI interpreters. */ + gdb::observers::normal_stop.attach (cli_base_on_normal_stop, + "cli-interp-base"); + gdb::observers::end_stepping_range.attach (cli_base_on_end_stepping_range, + "cli-interp-base"); + gdb::observers::signal_received.attach (cli_base_on_signal_received, + "cli-interp-base"); + gdb::observers::signal_exited.attach (cli_base_on_signal_exited, + "cli-interp-base"); + gdb::observers::exited.attach (cli_base_on_exited, "cli-interp-base"); + gdb::observers::no_history.attach (cli_base_on_no_history, "cli-interp-base"); + gdb::observers::sync_execution_done.attach (cli_base_on_sync_execution_done, + "cli-interp-base"); + gdb::observers::command_error.attach (cli_base_on_command_error, + "cli-interp-base"); gdb::observers::user_selected_context_changed.attach - (cli_on_user_selected_context_changed, "cli-interp"); + (cli_base_on_user_selected_context_changed, "cli-interp-base"); } diff --git a/gdb/tui/tui-interp.c b/gdb/tui/tui-interp.c index b4faede8ce6..1c4ffbbc3aa 100644 --- a/gdb/tui/tui-interp.c +++ b/gdb/tui/tui-interp.c @@ -53,15 +53,6 @@ class tui_interp final : public cli_interp_base ui_out *interp_ui_out () override; }; -/* Returns the INTERP if the INTERP is a TUI, and returns NULL - otherwise. */ - -static tui_interp * -as_tui_interp (struct interp *interp) -{ - return dynamic_cast (interp); -} - /* Cleanup the tui before exiting. */ static void @@ -72,171 +63,6 @@ tui_exit (void) tui_disable (); } -/* Observers for several run control events. If the interpreter is - quiet (i.e., another interpreter is being run with - interpreter-exec), print nothing. */ - -/* Observer for the normal_stop notification. */ - -static void -tui_on_normal_stop (struct bpstat *bs, int print_frame) -{ - if (!print_frame) - return; - - /* This event is suppressed. */ - if (cli_suppress_notification.normal_stop) - return; - - SWITCH_THRU_ALL_UIS () - { - struct interp *interp = top_level_interpreter (); - struct interp *tui = as_tui_interp (interp); - struct thread_info *thread; - - if (tui == NULL) - continue; - - thread = inferior_thread (); - if (should_print_stop_to_console (interp, thread)) - print_stop_event (tui->interp_ui_out ()); - } -} - -/* Observer for the signal_received notification. */ - -static void -tui_on_signal_received (enum gdb_signal siggnal) -{ - SWITCH_THRU_ALL_UIS () - { - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - continue; - - print_signal_received_reason (tui->interp_ui_out (), siggnal); - } -} - -/* Observer for the end_stepping_range notification. */ - -static void -tui_on_end_stepping_range (void) -{ - SWITCH_THRU_ALL_UIS () - { - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - continue; - - print_end_stepping_range_reason (tui->interp_ui_out ()); - } -} - -/* Observer for the signal_exited notification. */ - -static void -tui_on_signal_exited (enum gdb_signal siggnal) -{ - SWITCH_THRU_ALL_UIS () - { - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - continue; - - print_signal_exited_reason (tui->interp_ui_out (), siggnal); - } -} - -/* Observer for the exited notification. */ - -static void -tui_on_exited (int exitstatus) -{ - SWITCH_THRU_ALL_UIS () - { - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - continue; - - print_exited_reason (tui->interp_ui_out (), exitstatus); - } -} - -/* Observer for the no_history notification. */ - -static void -tui_on_no_history (void) -{ - SWITCH_THRU_ALL_UIS () - { - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - continue; - - print_no_history_reason (tui->interp_ui_out ()); - } -} - -/* Observer for the sync_execution_done notification. */ - -static void -tui_on_sync_execution_done (void) -{ - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - return; - - display_gdb_prompt (NULL); -} - -/* Observer for the command_error notification. */ - -static void -tui_on_command_error (void) -{ - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - return; - - display_gdb_prompt (NULL); -} - -/* Observer for the user_selected_context_changed notification. */ - -static void -tui_on_user_selected_context_changed (user_selected_what selection) -{ - /* This event is suppressed. */ - if (cli_suppress_notification.user_selected_context) - return; - - thread_info *tp = inferior_ptid != null_ptid ? inferior_thread () : NULL; - - SWITCH_THRU_ALL_UIS () - { - struct interp *tui = as_tui_interp (top_level_interpreter ()); - - if (tui == NULL) - continue; - - if (selection & USER_SELECTED_INFERIOR) - print_selected_inferior (tui->interp_ui_out ()); - - if (tp != NULL - && ((selection & (USER_SELECTED_THREAD | USER_SELECTED_FRAME)))) - print_selected_thread_frame (tui->interp_ui_out (), selection); - - } -} - /* These implement the TUI interpreter. */ void @@ -349,17 +175,7 @@ _initialize_tui_interp () if (interpreter_p == INTERP_CONSOLE) interpreter_p = INTERP_TUI; - /* If changing this, remember to update cli-interp.c as well. */ - gdb::observers::normal_stop.attach (tui_on_normal_stop, "tui-interp"); - gdb::observers::signal_received.attach (tui_on_signal_received, "tui-interp"); - gdb::observers::end_stepping_range.attach (tui_on_end_stepping_range, - "tui-interp"); - gdb::observers::signal_exited.attach (tui_on_signal_exited, "tui-interp"); - gdb::observers::exited.attach (tui_on_exited, "tui-interp"); - gdb::observers::no_history.attach (tui_on_no_history, "tui-interp"); - gdb::observers::sync_execution_done.attach (tui_on_sync_execution_done, - "tui-interp"); - gdb::observers::command_error.attach (tui_on_command_error, "tui-interp"); - gdb::observers::user_selected_context_changed.attach - (tui_on_user_selected_context_changed, "tui-interp"); + /* There are no observers here because the CLI interpreter's + observers work for the TUI interpreter as well. See + cli-interp.c. */ } base-commit: 39b8a8090ed7e8967ceca3655aa5f3a2ae91219d -- 2.36.0