From patchwork Fri May 24 18:33: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: 90843 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 11E1C3858429 for ; Fri, 24 May 2024 18:34:14 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-io1-xd2a.google.com (mail-io1-xd2a.google.com [IPv6:2607:f8b0:4864:20::d2a]) by sourceware.org (Postfix) with ESMTPS id DB31C3858D29 for ; Fri, 24 May 2024 18:33:49 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DB31C3858D29 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 DB31C3858D29 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::d2a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716575631; cv=none; b=i/rzjFBsBcr7gxPTinCOFOFW54V5k+wXIFsCpLfgDZfjwDO14jK7G+XF48wL5pMeRUi+MehiSqEd3gAVEwFQ2avl9wfkzFP12Lk0EhePNdgraPLPrz05ds+UEmw2WgFGa4hC6jI5H/MrAwRnXLSRXf3diEk58TOblMVgw1AKLTs= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1716575631; c=relaxed/simple; bh=A34aJGw4Zks4p8mzcrY9F4HA841egrl5BNZxPQvEo+w=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=OiOBwH7WBcAMPOpYfNLLRTAp+673CTcaoPRnAJ5vBBES8QPCzWg4cRlAYRhDjgEf9iJQB3Q9uZf0/mb1c7FEPzcEAuSxnTfEfH5rMuAQjukat23iMfdldW/rP+idSzRGuSrmcVf6rPvslEOj/tYO+T3QONt5sD8pWxJWGd9ZfbI= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-io1-xd2a.google.com with SMTP id ca18e2360f4ac-7e8e7b01257so43158039f.3 for ; Fri, 24 May 2024 11:33:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1716575629; x=1717180429; darn=sourceware.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=G+4H+lnn80SV2pwRmD4IWyhPnVaGfG6X8nxYAfdJrd0=; b=F2iXnzYk2r8BR2FlDcb+oXcpDSnG1FPymutehT9bx6wNbu+gJa0BEwXCaLiQg1kaA5 bjgfh5WYDKFxnLR6Rdt5BDtQbxSAMp8/hYcobLjE9yPD28CJiBqhZAKQm0okG1aG3grR 5MnvclV3O9AXPulCnJf5HH4HzBV8PtU/Q69NYTaDwjmMrEURP0jq98GshlOpbhegNBzE jPRsDEyoh4ajE7bi9D9o6F0YTcjiUc2XfH0gPOjkaEJeJ+Svkm7/RGG1+ZKOWf/D2OZB YDrE6JNr2GbYsIsT0NORmpetf199VyWqCtPe7G5CBniO38Z4X1E20kzHeCf2/F7T050t RDhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716575629; x=1717180429; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=G+4H+lnn80SV2pwRmD4IWyhPnVaGfG6X8nxYAfdJrd0=; b=rVuSdRAcaIdu+A/J/bcio8hMEZdC5Xd7fLZdFrhnF0tgcPBfRA5aQUuIN8W4fKK6YN F7OQvoq4WJe5Kz0eSxSaPxm5Unc3Pr0QLo15DqJhkrGkCI7j2om9E0k9InogF/VsVVkn zZjZb9uKgTVMM8CzILJjh9CyTkydtNeB3UVY1OWU3kLfuySh8JZAQ/oG2w902PPvuNAn zXTBsLBoPGeIS3DVAmnhqAHTo2YfoRBjJ5YCqLO6og9zcuYPp9YydC8/FbT7HmgP4hoV 3kduQ/Xc38d9XjnxztZHva83J2yAE1qga8Qmbzay74LWquR9kjj53+uR+SE7SdCbKEX9 EQew== X-Gm-Message-State: AOJu0Yx7JusGETH0xwc9Q3uwapleUQ3LhFO+q4LHWdPFeWgoeK99yOvH kvra0vPrMrqk1vh2c6A3seXkRmuXmnK6w4X/Bec0NuZwqy6Iiqpf5pawm7iCWo/woX6EjVDzVVo = X-Google-Smtp-Source: AGHT+IHy3d7w9Nm6JMItj/BoNbaKZYVABgVwCqYltUOUW3CEO03qgvSx3k8nD4VDhumeZZcUC5vNnQ== X-Received: by 2002:a05:6602:585:b0:7e1:817a:4a3c with SMTP id ca18e2360f4ac-7e8c470c164mr358939839f.4.1716575628913; Fri, 24 May 2024 11:33:48 -0700 (PDT) Received: from localhost.localdomain (75-166-134-4.hlrn.qwest.net. [75.166.134.4]) by smtp.gmail.com with ESMTPSA id ca18e2360f4ac-7e903e0b66esm46310339f.3.2024.05.24.11.33.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 11:33:48 -0700 (PDT) From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH] Make global_symbol_searcher::filenames private Date: Fri, 24 May 2024 12:33:38 -0600 Message-ID: <20240524183338.1996024-1-tromey@adacore.com> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-11.2 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 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 This patch renames global_symbol_searcher::filenames and makes it private, adding a new method to append a filename to the vector. This also cleans up memory management here, removing an alloca from rbreak, and removing a somewhat ugly SCOPE_EXIT from the Python code, in favor of having global_symbol_searcher manage the memory itself. Regression tested on x86-64 Fedora 38. --- gdb/python/python.c | 9 +-------- gdb/symtab.c | 42 +++++++++++++++++++----------------------- gdb/symtab.h | 10 ++++++---- 3 files changed, 26 insertions(+), 35 deletions(-) diff --git a/gdb/python/python.c b/gdb/python/python.c index 8121e5d6f2a..e9092b4d994 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -804,10 +804,6 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw) } global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regex); - SCOPE_EXIT { - for (const char *elem : spec.filenames) - xfree ((void *) elem); - }; /* The "symtabs" keyword is any Python iterable object that returns a gdb.Symtab on each iteration. If specified, iterate through @@ -852,10 +848,7 @@ gdbpy_rbreak (PyObject *self, PyObject *args, PyObject *kw) if (filename == NULL) return NULL; - /* Make sure there is a definite place to store the value of - filename before it is released. */ - spec.filenames.push_back (nullptr); - spec.filenames.back () = filename.release (); + spec.add_filename (std::move (filename)); } } diff --git a/gdb/symtab.c b/gdb/symtab.c index 58648a8779d..8776cb2c84b 100644 --- a/gdb/symtab.c +++ b/gdb/symtab.c @@ -4765,16 +4765,17 @@ info_sources_command (const char *args, int from_tty) true compare only lbasename of FILENAMES. */ static bool -file_matches (const char *file, const std::vector &filenames, +file_matches (const char *file, + const std::vector> &filenames, bool basenames) { if (filenames.empty ()) return true; - for (const char *name : filenames) + for (const auto &name : filenames) { - name = (basenames ? lbasename (name) : name); - if (compare_filenames_for_search (file, name)) + const char *lname = (basenames ? lbasename (name.get ()) : name.get ()); + if (compare_filenames_for_search (file, lname)) return true; } @@ -4867,10 +4868,10 @@ global_symbol_searcher::expand_symtabs auto do_file_match = [&] (const char *filename, bool basenames) { - return file_matches (filename, filenames, basenames); + return file_matches (filename, m_filenames, basenames); }; gdb::function_view file_matcher = nullptr; - if (!filenames.empty ()) + if (!m_filenames.empty ()) file_matcher = do_file_match; objfile->expand_symtabs_matching @@ -4899,7 +4900,7 @@ global_symbol_searcher::expand_symtabs We only search the objfile the msymbol came from, we no longer search all objfiles. In large programs (1000s of shared libs) searching all objfiles is not worth the pain. */ - if (filenames.empty () + if (m_filenames.empty () && (kind & (SEARCH_VAR_DOMAIN | SEARCH_FUNCTION_DOMAIN)) != 0) { for (minimal_symbol *msymbol : objfile->msymbols ()) @@ -4965,12 +4966,12 @@ global_symbol_searcher::add_matching_symbols /* Check first sole REAL_SYMTAB->FILENAME. It does not need to be a substring of symtab_to_fullname as it may contain "./" etc. */ - if (!(file_matches (real_symtab->filename, filenames, false) + if (!(file_matches (real_symtab->filename, m_filenames, false) || ((basenames_may_differ || file_matches (lbasename (real_symtab->filename), - filenames, true)) + m_filenames, true)) && file_matches (symtab_to_fullname (real_symtab), - filenames, false)))) + m_filenames, false)))) continue; if (!sym->matches (kind)) @@ -5147,7 +5148,7 @@ global_symbol_searcher::search () const minimal symbol, as we assume that a minimal symbol does not have a type. */ if ((found_msymbol - || (filenames.empty () && (m_kind & SEARCH_VAR_DOMAIN) != 0)) + || (m_filenames.empty () && (m_kind & SEARCH_VAR_DOMAIN) != 0)) && !m_exclude_minsyms && !treg.has_value ()) { @@ -5556,7 +5557,7 @@ static void rbreak_command (const char *regexp, int from_tty) { std::string string; - const char *file_name = nullptr; + gdb::unique_xmalloc_ptr file_name; if (regexp != nullptr) { @@ -5569,23 +5570,18 @@ rbreak_command (const char *regexp, int from_tty) if (colon && *(colon + 1) != ':') { - int colon_index; - char *local_name; - - colon_index = colon - regexp; - local_name = (char *) alloca (colon_index + 1); - memcpy (local_name, regexp, colon_index); - local_name[colon_index--] = 0; - while (isspace (local_name[colon_index])) - local_name[colon_index--] = 0; - file_name = local_name; + int colon_index = colon - regexp; + while (colon_index > 0 && isspace (regexp[colon_index - 1])) + --colon_index; + + file_name = make_unique_xstrndup (regexp, colon_index); regexp = skip_spaces (colon + 1); } } global_symbol_searcher spec (SEARCH_FUNCTION_DOMAIN, regexp); if (file_name != nullptr) - spec.filenames.push_back (file_name); + spec.add_filename (std::move (file_name)); std::vector symbols = spec.search (); scoped_rbreak_breakpoints finalize; diff --git a/gdb/symtab.h b/gdb/symtab.h index bf9a3cfb79f..d96b3cc1273 100644 --- a/gdb/symtab.h +++ b/gdb/symtab.h @@ -2612,12 +2612,14 @@ class global_symbol_searcher removed. */ std::vector search () const; - /* The set of source files to search in for matching symbols. This is - currently public so that it can be populated after this object has - been constructed. */ - std::vector filenames; + /* Add a filename to the list of file names to search. */ + void add_filename (gdb::unique_xmalloc_ptr filename) + { m_filenames.push_back (std::move (filename)); } private: + /* The set of source files to search in for matching symbols. */ + std::vector> m_filenames; + /* The kind of symbols are we searching for. VARIABLES_DOMAIN - Search all symbols, excluding functions, type names, and constants (enums).