From patchwork Sat Jan 4 18:34:03 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 37196 Received: (qmail 14176 invoked by alias); 4 Jan 2020 18:54:22 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 14166 invoked by uid 89); 4 Jan 2020 18:54:21 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.4 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=shut X-HELO: gateway34.websitewelcome.com Received: from gateway34.websitewelcome.com (HELO gateway34.websitewelcome.com) (192.185.148.109) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Sat, 04 Jan 2020 18:54:18 +0000 Received: from cm16.websitewelcome.com (cm16.websitewelcome.com [100.42.49.19]) by gateway34.websitewelcome.com (Postfix) with ESMTP id D500ED1ED4 for ; Sat, 4 Jan 2020 12:34:16 -0600 (CST) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id noFcinbHdOdBHnoFcis7jR; Sat, 04 Jan 2020 12:34:16 -0600 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=nzQP2tX/jaqVJyjaHngF43umRQk0LRc2ZMAV6Ta1q0E=; b=YAwnD4b6swOriZG1dIrqEHVSg/ 97Yn5VCAvww5FPBqHBCocyoknro1/Mlt/XF3bm3D9rF++gSmd0dTHOlduTWIWue9UALee6FO3AKWV Idt0ak1deS0JbiLyNg8VtYQPi; Received: from 75-166-123-50.hlrn.qwest.net ([75.166.123.50]:48942 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1inoFc-0026Lh-MS; Sat, 04 Jan 2020 11:34:16 -0700 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH 17/24] Change how TUI windows are instantiated Date: Sat, 4 Jan 2020 11:34:03 -0700 Message-Id: <20200104183410.17114-18-tom@tromey.com> In-Reply-To: <20200104183410.17114-1-tom@tromey.com> References: <20200104183410.17114-1-tom@tromey.com> This adds a new global that maps from window names to window constructor functions, and then changes tui_get_window_by_name and validate_window_name to use it. This is another step toward user-defined window types. 2020-01-04 Tom Tromey * tui/tui-layout.c (make_standard_window, get_locator_window): New functions. (known_window_types): New global. (tui_get_window_by_name): Reimplement. (initialize_known_windows): New function. (validate_window_name): Rewrite. (_initialize_tui_layout): Call initialize_known_windows. Change-Id: I9037aac550299b9d945899220a30c2d3af9dd0de --- gdb/ChangeLog | 10 +++++ gdb/tui/tui-layout.c | 99 ++++++++++++++++++++++++++++++-------------- 2 files changed, 77 insertions(+), 32 deletions(-) diff --git a/gdb/tui/tui-layout.c b/gdb/tui/tui-layout.c index 898d2f0b8d2..797acc6e8b7 100644 --- a/gdb/tui/tui-layout.c +++ b/gdb/tui/tui-layout.c @@ -29,6 +29,7 @@ #include "cli/cli-decode.h" #include "cli/cli-utils.h" #include +#include #include #include "tui/tui.h" @@ -322,40 +323,74 @@ tui_gen_win_info::resize (int height_, int width_, +/* Helper function to create one of the built-in (non-locator) + windows. */ + +template +static tui_gen_win_info * +make_standard_window (const char *) +{ + if (tui_win_list[V] == nullptr) + tui_win_list[V] = new T (); + return tui_win_list[V]; +} + +/* Helper function to wrap tui_locator_win_info_ptr for + tui_get_window_by_name. */ + +static tui_gen_win_info * +get_locator_window (const char *) +{ + return tui_locator_win_info_ptr (); +} + +/* A map holding all the known window types, keyed by name. Note that + this is heap-allocated and "leaked" at gdb exit. This avoids + ordering issues with destroying elements in the map at shutdown. + In particular, destroying this map can occur after Python has been + shut down, causing crashes if any window destruction requires + running Python code. */ + +static std::unordered_map *known_window_types; + /* Helper function that returns a TUI window, given its name. */ static tui_gen_win_info * tui_get_window_by_name (const std::string &name) { - if (name == "src") - { - if (tui_win_list[SRC_WIN] == nullptr) - tui_win_list[SRC_WIN] = new tui_source_window (); - return tui_win_list[SRC_WIN]; - } - else if (name == "cmd") - { - if (tui_win_list[CMD_WIN] == nullptr) - tui_win_list[CMD_WIN] = new tui_cmd_window (); - return tui_win_list[CMD_WIN]; - } - else if (name == "regs") - { - if (tui_win_list[DATA_WIN] == nullptr) - tui_win_list[DATA_WIN] = new tui_data_window (); - return tui_win_list[DATA_WIN]; - } - else if (name == "asm") - { - if (tui_win_list[DISASSEM_WIN] == nullptr) - tui_win_list[DISASSEM_WIN] = new tui_disasm_window (); - return tui_win_list[DISASSEM_WIN]; - } - else - { - gdb_assert (name == "locator"); - return tui_locator_win_info_ptr (); - } + for (tui_win_info *window : saved_tui_windows) + if (name == window->name ()) + return window; + + auto iter = known_window_types->find (name); + if (iter == known_window_types->end ()) + error (_("Unknown window type \"%s\""), name.c_str ()); + + tui_gen_win_info *result = iter->second (name.c_str ()); + if (result == nullptr) + error (_("Could not create window \"%s\""), name.c_str ()); + return result; +} + +/* Initialize the known window types. */ + +static void +initialize_known_windows () +{ + known_window_types = new std::unordered_map; + + known_window_types->emplace ("src", + make_standard_window); + known_window_types->emplace ("cmd", + make_standard_window); + known_window_types->emplace ("regs", + make_standard_window); + known_window_types->emplace ("asm", + make_standard_window); + known_window_types->emplace ("locator", get_locator_window); } /* See tui-layout.h. */ @@ -886,9 +921,8 @@ initialize_layouts () static bool validate_window_name (const std::string &name) { - return (name == "src" || name == "cmd" - || name == "regs" || name == "asm" - || name == "locator"); + auto iter = known_window_types->find (name); + return iter != known_window_types->end (); } /* Implementation of the "tui new-layout" command. */ @@ -1022,4 +1056,5 @@ to be allocated to the window."), tui_get_cmd_list ()); initialize_layouts (); + initialize_known_windows (); }