From patchwork Mon Nov 27 17:55:55 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80833 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 869723861814 for ; Mon, 27 Nov 2023 17:56:27 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 1E7DC3858C2D for ; Mon, 27 Nov 2023 17:56:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1E7DC3858C2D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 1E7DC3858C2D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107773; cv=none; b=AKrsYycrUsvjYgdYIL0U/wCMF28Zu8GFENZwRl3MAGIVxECF/8zqi80j/ylhKzqudfm28+vmkBm3Ut8/nq/jw8bJnete3suADWRep63gc6+9ftYNMBA9Vu1Yt6SHX0tnm8HFDknt3ZEkaiAf+UYAzMxT+tyc0ykmJWrgU9LZdgM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107773; c=relaxed/simple; bh=ruaBwHt3fQAwVrANCXrtIEQMGunhB16OzjIkWITalIc=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=pLDp2vXlNO6InoLBHpLQQWmTr3q+DIqc4rwG+PNcWM5/WumM32H9sZBXgKc9k96BXWgj4VULLObzkoYXQ6tl58IoiOKbdR7M0mlwUhucIWkd+/Ds3/WMkuO0eshoNC4gvf96RqaLBHhk+m+8zpB37OCs0IFFwj3P+ng7zL9KcTM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107767; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=mG8FaJ0CGlWPfBeSHOUFggkFtvkSBEZsmBKWvlzRliM=; b=BXa0aNRsU15N4m5pV82E5Xsube1dI1chi/aAC5QKwV8WCpjvGhsJBJr4gDPL0s0lysXPfP zYx7kkabzkCFPJXzh0ZwKsYaPoxv7sP9GvhxAKkfSB+W8LS6CigHy4FLFM9lx0RF7KlndW S95JF4LBNSFrepkcS+wnFXKHji6++mU= Received: from mail-wm1-f70.google.com (mail-wm1-f70.google.com [209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-609-UTCrOK9ZNTivdRmy5YdEvA-1; Mon, 27 Nov 2023 12:56:06 -0500 X-MC-Unique: UTCrOK9ZNTivdRmy5YdEvA-1 Received: by mail-wm1-f70.google.com with SMTP id 5b1f17b1804b1-40b32faeb7eso25736455e9.1 for ; Mon, 27 Nov 2023 09:56:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107765; x=1701712565; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=mG8FaJ0CGlWPfBeSHOUFggkFtvkSBEZsmBKWvlzRliM=; b=QWvqSpWpeNpgf4NXS6F4xN7U1tfb7D4vSrCRiL1r6Jyh5LVvHg63pCEmBBIyRmLK+F +Oi033Oq06ZaqMbA0fyIMqbs0hja7thH2mnA1LY0X0FBmKByAHd4/3fWqIvSOHaO/Hr3 ieU8SAHABkdWMw8JcGK47vZFCxILeiz4D0ixUQ5BExQ+8okmwJVX8SkLSg6C6AXL7FX2 C0f1XCPlV966G5dzR8RZWsMUvC5AbIh6RBuHAAkewogyp7BEQ9Vj6WwI4/lbjb0Xx1TA YiKmUtoS2LscsvveP5LSnU7M68T9/NEIaMTVcx9YB/IpmeDjRSIc4p9tqkhcvWyaXZx9 H7Yg== X-Gm-Message-State: AOJu0YyHrQY1ffXANRUwr3ipimYpQ7PukLyUUYF3mdq1L4wIXcgHQhkI 1JSvNJhTD7k8wDY7L1rX52HIuP+3q4+8Kp+0DB6C7Y0OKPqjTpnHWqBeF7/MjPWzOHFTeJbisup ZrJdmI7nJQXSdR39p+TkFhd5UaufOa2XU7oJ9GBQMlTd9jwjJF6CLoDmcNzw7pOqi1xcuvaKZcY cp8bx9pQ== X-Received: by 2002:a5d:4ac6:0:b0:332:caa2:fd30 with SMTP id y6-20020a5d4ac6000000b00332caa2fd30mr8768826wrs.40.1701107765039; Mon, 27 Nov 2023 09:56:05 -0800 (PST) X-Google-Smtp-Source: AGHT+IHYEca803sTRTSfxhCk7OJa1ZiEKrkRZSKIRh0BVN8xQa+YaqSICekg6YEw5ZsrXO2Y78lT9w== X-Received: by 2002:a5d:4ac6:0:b0:332:caa2:fd30 with SMTP id y6-20020a5d4ac6000000b00332caa2fd30mr8768808wrs.40.1701107764592; Mon, 27 Nov 2023 09:56:04 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id e11-20020a5d500b000000b00332cba50ac3sm12506964wrt.45.2023.11.27.09.56.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:04 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 1/7] gdb: allow use of ~ in 'save gdb-index' command Date: Mon, 27 Nov 2023 17:55:55 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 Add a call to gdb_tilde_expand in the save_gdb_index_command function, this means that we can now do: (gdb) save gdb-index ~/blah/ Previous this wouldn't work. --- gdb/dwarf2/index-write.c | 7 +- gdb/testsuite/gdb.dwarf2/gdb-index-tilde.exp | 91 ++++++++++++++++++++ 2 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 gdb/testsuite/gdb.dwarf2/gdb-index-tilde.exp diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index d1b10a28823..8ee5e420936 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -39,6 +39,7 @@ #include "objfiles.h" #include "ada-lang.h" #include "dwarf2/tag.h" +#include "gdbsupport/gdb_tilde_expand.h" #include #include @@ -1548,6 +1549,8 @@ save_gdb_index_command (const char *arg, int from_tty) if (!*arg) error (_("usage: save gdb-index [-dwarf-5] DIRECTORY")); + std::string directory (gdb_tilde_expand (arg)); + for (objfile *objfile : current_program_space->objfiles ()) { /* If the objfile does not correspond to an actual file, skip it. */ @@ -1567,8 +1570,8 @@ save_gdb_index_command (const char *arg, int from_tty) if (dwz != NULL) dwz_basename = lbasename (dwz->filename ()); - write_dwarf_index (per_objfile->per_bfd, arg, basename, - dwz_basename, index_kind); + write_dwarf_index (per_objfile->per_bfd, directory.c_str (), + basename, dwz_basename, index_kind); } catch (const gdb_exception_error &except) { diff --git a/gdb/testsuite/gdb.dwarf2/gdb-index-tilde.exp b/gdb/testsuite/gdb.dwarf2/gdb-index-tilde.exp new file mode 100644 index 00000000000..d30d0e86cf2 --- /dev/null +++ b/gdb/testsuite/gdb.dwarf2/gdb-index-tilde.exp @@ -0,0 +1,91 @@ +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Test that tilde expansion works for the 'save gdb-index' command. + +# This test relies on using the $HOME directory. We could make this +# work for remote hosts, but right now, this isn't supported. +require {!is_remote host} + +# Can't save an index with readnow. +require !readnow + +standard_testfile main.c + +# Create a directory to generate an index file into. +set full_dir [standard_output_file "index_files"] +remote_exec host "mkdir -p ${full_dir}" + +# The users home directory. +set home $::env(HOME) + +# Check that FULL_DIR is within the $HOME directory. If it's not, then +# that's fine, but we can't test tilde expansion in this case. +if { [string compare -length [string length $home] $full_dir $home] != 0 } { + unsupported "test not run within home directory" + return -1 +} + +# Convert the $HOME prefix in to ~. +set dir "~[string range $full_dir [string length $home] end]" + +# Build the test executable. +if { [prepare_for_testing "failed to prepare" "${testfile}" ${srcfile}] } { + return -1 +} + +# Start GDB and load in the executable. +clean_restart ${binfile} + +# If the executable was built with an index, or lacks the debug +# information required to create an index, then we'll not be able to +# generate an index, so lets not even try. +set has_index false +set can_dump_index false +gdb_test_multiple "maint print objfile $binfile" "check we can generate an index" { + -re "\r\n\\.gdb_index: version ${decimal}(?=\r\n)" { + set has_index true + gdb_test_lines "" $gdb_test_name ".*" + } + -re "\r\n\\.debug_names: exists(?=\r\n)" { + set has_index true + gdb_test_lines "" $gdb_test_name ".*" + } + -re "\r\n(Cooked index in use:|Psymtabs)(?=\r\n)" { + set can_dump_index true + gdb_test_lines "" $gdb_test_name ".*" + } + -re -wrap "" { + } +} + +if { $has_index } { + unsupported "already have an index" + return -1 +} + +if { !$can_dump_index } { + unsupported "lacks debug information needed to dump index" + return -1 +} + +# Generate an index file. +gdb_test_no_output "save gdb-index $dir" +gdb_exit + +# Confirm that the index file exists. +set index_filename "${full_dir}/${gdb_test_file_name}.gdb-index" +gdb_assert { [remote_file host exists $index_filename] } \ + "confirm the index file exists" From patchwork Mon Nov 27 17:55:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80832 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 A2D22384F98B for ; Mon, 27 Nov 2023 17:56:26 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id B79CA385802E for ; Mon, 27 Nov 2023 17:56:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B79CA385802E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B79CA385802E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107773; cv=none; b=NYH+zxw6JRI7CYNaX2xOzBhkeBtJt0SieA2hpPSWFercjdEplJHMh5K5jZLDcW8Lytj8Ms+aLZV4ix6TmILp6lAJ76L4cBI0pjB2N9JJtUu1PBDTLHVE/HiVb1Ec0AQtEtb6OuLL23/v7N7vfdrcHhFbvaornwy6yKOQVWA8boY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107773; c=relaxed/simple; bh=SVWAtogAney9IjuUd2wDZJ6zGevBmlAqcmTECh1OTew=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Il8OvhpbB1JzNhLxSxUR7vPJjuADdpaft2Iova+ktRiWGdHIEG27YC4D/ooyqMSiWhbShjSzn1r+/oNSloABS3HUqglBbpJy9nTD2hp2tR4/IgGTQPTPByc22C8m38CzI7tA70tMlbqAHvDxz39FFReG5bt8iY9Fs1AVnuv+g1M= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=V6XOR18/ywhiFjjwyffQB3JvXw7txJ8GkO+YiR7z9LQ=; b=DxRSqfosS/grfvkNxl2mUEv75+3xKCEMfMJ01Wnnzaf8s6c0XSy1x6sWSLRkdNkwyc0o/R vDyiCK8LF4/ZLBsDBK80HmneasbuuA0oazxiurSqwQwOL53xX4GSia5WGQ+cBF1KWdRyhV xZMa9jKxY5XWbthvJU6fP3FR/PInPdw= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-601-zdIP-7HPOgOgVyRvbY9UsQ-1; Mon, 27 Nov 2023 12:56:07 -0500 X-MC-Unique: zdIP-7HPOgOgVyRvbY9UsQ-1 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-332fab597afso1169871f8f.3 for ; Mon, 27 Nov 2023 09:56:07 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107766; x=1701712566; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=V6XOR18/ywhiFjjwyffQB3JvXw7txJ8GkO+YiR7z9LQ=; b=qw91ANvmwHWUlg6F5nBy4Fi0rAFcUUQwXtYexp1f/lc2BFJQvfygbwy/1J3f6SYi6S k1MmuYZqJSRu2VC7qt2c+sB/pQdZ9dLMUWmXhECNCIHFktmgBu1i2zT8ICEpriWJLVh0 5avEhOBbvQvrWsMoAb+tyMxwQptSNzlD/IUqIPAhPtZwDK7iMyhZriWiq4LQ2c4WqSNi 9Bprm9n9q/hpGw6QRgUklbWVl1vqiyldbLJ7Wp2dmvwYWj0++g0wB+1J2NH/Sjzg9QVP SK59slNtgUKjb1hRaUze7Ef9/uJ0vOA3KOwYwT6AgXTCYwmwlxCXspg7ocdpFD1Em6NT 5AnQ== X-Gm-Message-State: AOJu0YzlvDiAukYrYELkKZhwgkK5IyAC8I4kuh2mh5bXMF9zX9Pio8Ej ct8hVgyDLrZCPA3ci46Psy32j2uYp1tuT6QfVzLrTZnTfINW/6UFJ27ajBEFpgxWRC/G85NCUfN QVVknvJcu684R1R+ERJarc4fLvjeuBgllWyqK1OyysGfBf2OprqqY/XcpK9dGEJmpOFTAmnDwCu HcN0X3nA== X-Received: by 2002:a5d:4942:0:b0:332:e1e6:eeda with SMTP id r2-20020a5d4942000000b00332e1e6eedamr8101271wrs.69.1701107766336; Mon, 27 Nov 2023 09:56:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IGJAzI5hBi15iKjn622frk3QtlqYD4t93uG2CVNsRwlms2tutEIBsP0JrOamavo/3FQupafrQ== X-Received: by 2002:a5d:4942:0:b0:332:e1e6:eeda with SMTP id r2-20020a5d4942000000b00332e1e6eedamr8101258wrs.69.1701107765926; Mon, 27 Nov 2023 09:56:05 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id m4-20020adffe44000000b00332f74ffde5sm6709009wrs.5.2023.11.27.09.56.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:05 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 2/7] gdb: option completion for 'save gdb-index' command Date: Mon, 27 Nov 2023 17:55:56 +0000 Message-Id: <523e35f23ec81debf21af6a42b148968c73e2ecd.1701107594.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 Add proper support for option completion to the 'save gdb-index' command. Update save_gdb_index_command function to make use of the new option_def data structures for parsing the '-dwarf-5' option. --- gdb/dwarf2/index-write.c | 69 ++++++++++++++++++++++++++++++---------- 1 file changed, 52 insertions(+), 17 deletions(-) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 8ee5e420936..c0867799f6d 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -1523,6 +1523,48 @@ write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir, dwz_index_wip->finalize (); } +/* Options structure for the 'save gdb-index' command. */ + +struct save_gdb_index_options +{ + bool dwarf_5 = false; +}; + +/* The option_def list for the 'save gdb-index' command. */ + +static const gdb::option::option_def save_gdb_index_options_defs[] = { + gdb::option::boolean_option_def { + "dwarf-5", + [] (save_gdb_index_options *opt) { return &opt->dwarf_5; }, + nullptr, /* show_cmd_cb */ + nullptr /* set_doc */ + } +}; + +/* Create an options_def_group for the 'save gdb-index' command. */ + +static gdb::option::option_def_group +make_gdb_save_index_options_def_group (save_gdb_index_options *opts) +{ + return {{save_gdb_index_options_defs}, opts}; +} + +/* Completer for the "save gdb-index" command. */ + +static void +gdb_save_index_cmd_completer (struct cmd_list_element *ignore, + completion_tracker &tracker, + const char *text, const char *word) +{ + auto grp = make_gdb_save_index_options_def_group (nullptr); + if (gdb::option::complete_options + (tracker, &text, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, grp)) + return; + + word = advance_to_filename_complete_word_point (tracker, text); + filename_completer (ignore, tracker, text, word); +} + /* Implementation of the `save gdb-index' command. Note that the .gdb_index file format used by this command is @@ -1530,26 +1572,19 @@ write_dwarf_index (dwarf2_per_bfd *per_bfd, const char *dir, there. */ static void -save_gdb_index_command (const char *arg, int from_tty) +save_gdb_index_command (const char *args, int from_tty) { - const char dwarf5space[] = "-dwarf-5 "; - dw_index_kind index_kind = dw_index_kind::GDB_INDEX; - - if (!arg) - arg = ""; - - arg = skip_spaces (arg); - if (strncmp (arg, dwarf5space, strlen (dwarf5space)) == 0) - { - index_kind = dw_index_kind::DEBUG_NAMES; - arg += strlen (dwarf5space); - arg = skip_spaces (arg); - } + save_gdb_index_options opts; + const auto group = make_gdb_save_index_options_def_group (&opts); + gdb::option::process_options + (&args, gdb::option::PROCESS_OPTIONS_UNKNOWN_IS_OPERAND, group); - if (!*arg) + if (args == nullptr || *args == '\0') error (_("usage: save gdb-index [-dwarf-5] DIRECTORY")); - std::string directory (gdb_tilde_expand (arg)); + std::string directory (gdb_tilde_expand (args)); + dw_index_kind index_kind + = (opts.dwarf_5 ? dw_index_kind::DEBUG_NAMES : dw_index_kind::GDB_INDEX); for (objfile *objfile : current_program_space->objfiles ()) { @@ -1675,5 +1710,5 @@ No options create one file with .gdb-index extension for pre-DWARF-5\n\ compatible .gdb_index section. With -dwarf-5 creates two files with\n\ extension .debug_names and .debug_str for DWARF-5 .debug_names section."), &save_cmdlist); - set_cmd_completer (c, filename_completer); + set_cmd_completer_handle_brkchars (c, gdb_save_index_cmd_completer); } From patchwork Mon Nov 27 17:55:57 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80834 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 A326C385AC23 for ; Mon, 27 Nov 2023 17:56:49 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 4CD0A385770A for ; Mon, 27 Nov 2023 17:56:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4CD0A385770A Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4CD0A385770A Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107774; cv=none; b=gIt52jSDYkPyFqpNPGehojpCdY2qf7CzeTIbVMvDXOu814ExyGXNdHL9a5kLcrMcAwavHyBPsWy3bgoJOgN0ScOHFeo7HymXTGC5dDvEeJ3DjnfPXOEeZHlclOb9s+hyoGN6odWV1VCl4kRMyfMP46NAF5NbsBa4p2nD8H8t5cg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107774; c=relaxed/simple; bh=UwXYKmzPgvHWgn7s9dHs/W5TTETy22aAhwXr7meHjP4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=rMJGRk/SQlG6ihTdRXY4xJzsnY/4YS4enlSDW1gIUNWQAflBUHR5YQkUFJyD8gbsFVzcV9A7Ll2ytcGw35K0Xq8HJ1OXuN9OlYmCwPoWU2xcoYog37/aSqlnAzQ55qCaJiijtEc+R92MZnN2jJDAhYLffjN2wAXd+shC88k9+dQ= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107770; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=SBGCDW5OhANMsEBJWec5+QMhpb63ydRNpGhvTDqT9uQ=; b=Hbb1TTAwBWNqfjELB7Pk+Z6PaNEcqtcFh41+MEzbn9eDWjZfzqrsbWct3t1SxkO77+PO3V Rl/ChrQT0fX+bgCISkwM3h+JVpGrnfIKe2yjj7Na004O5EwYAliQXOhtdZl0rMGabRR3QP ALonpqHGSZYqXwffwzJgjRzEEFz+Yj4= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-32-bqFe5JZvO_q_62C9_3L_WA-1; Mon, 27 Nov 2023 12:56:09 -0500 X-MC-Unique: bqFe5JZvO_q_62C9_3L_WA-1 Received: by mail-wm1-f72.google.com with SMTP id 5b1f17b1804b1-40b310b5453so30521815e9.1 for ; Mon, 27 Nov 2023 09:56:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107767; x=1701712567; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=SBGCDW5OhANMsEBJWec5+QMhpb63ydRNpGhvTDqT9uQ=; b=o9pWPqEeTvTblQ5bpdXr75kDSUt+qrdO/0PqWvrdsoftIg3MpVxa/WJ8vkiwdEvqej KFN3cHqeX4yw23VhML9Jor0sQ+XL24ihfCogZuCn9fEclJ4nJN+AZYSBoipujQakK93h D8uq8KcFjbpIjwTcOt7M3XUT9gTFFGkcIU8Dc44UrBsBi22BO1KPsOccvuWfKtGqKk4a Afob1oIW3bP+aDCU+r+WOOWNBbA85L89C4mhs2motgQHmpEQdNYfgEzDXXNL5EWwIbp9 0+JK1sMg1O3pm6HDG8odBTZYEHf7wgXcUFRVg/gmBPTqq/3BOB9rOeTFkA5hy8pFAjwL xkFQ== X-Gm-Message-State: AOJu0YzVQVdWrOdOayrW+87I5QjzEbDn3alrSrBKx27GGpu3l4ky9v6x qr8wsP7D+/JJ2rc6KZ4efFaWwU5kiA7taYDHJj6xXfQqLBQsKMixSu6be6hUKNtlNN2O+UxpCsS nyHbg4ngaCw+dITSUNBXU7KBajZdKDQqioL88m1QLTUhvKnzmNlm3MgYnKw1qf9NNH8xjS3sgs1 3GPkjLXw== X-Received: by 2002:a05:600c:4fcc:b0:408:fe93:a2f7 with SMTP id o12-20020a05600c4fcc00b00408fe93a2f7mr6009715wmq.37.1701107767751; Mon, 27 Nov 2023 09:56:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGE/eMnJ17NyUQlB1WxvhkMcLt5bgLIl4gyoGiZHQS1+elW6VVm/vfJ0j4ZM8AAAhfz/JDklw== X-Received: by 2002:a05:600c:4fcc:b0:408:fe93:a2f7 with SMTP id o12-20020a05600c4fcc00b00408fe93a2f7mr6009692wmq.37.1701107767244; Mon, 27 Nov 2023 09:56:07 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id r4-20020a05600c458400b0040648217f4fsm15258312wmo.39.2023.11.27.09.56.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:06 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 3/7] gdb/testsuite: small refactor in selftest-support.exp Date: Mon, 27 Nov 2023 17:55:57 +0000 Message-Id: <01785d65d4a04b1b05030ef7569103c4c5afd9de.1701107594.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 Split out the code that makes a copy of the GDB executable ready for self testing into a new proc. A later commit in this series wants to load the GDB executable into GDB (for creating an on-disk debug index), but doesn't need to make use of the full do_self_tests proc. There should be no changes in what is tested after this commit. --- gdb/testsuite/lib/selftest-support.exp | 32 ++++++++++++++++++-------- 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/gdb/testsuite/lib/selftest-support.exp b/gdb/testsuite/lib/selftest-support.exp index 1e7a2cea8b7..abb1f43f1b0 100644 --- a/gdb/testsuite/lib/selftest-support.exp +++ b/gdb/testsuite/lib/selftest-support.exp @@ -92,11 +92,13 @@ proc selftest_setup { executable function } { return 0 } -# A simple way to run some self-tests. - -proc do_self_tests {function body} { - global GDB tool - +# Prepare for running a self-test by moving the GDB executable to a +# location where we can use it as the inferior. Return the filename +# of the new location. +# +# If the current testing setup is not suitable for running a +# self-test, then return an empty string. +proc selftest_prepare {} { # Are we testing with a remote board? In that case, the target # won't have access to the GDB's auxilliary data files # (data-directory, etc.). It's simpler to just skip. @@ -120,19 +122,31 @@ proc do_self_tests {function body} { # Run the test with self. Copy the file executable file in case # this OS doesn't like to edit its own text space. - set GDB_FULLPATH [find_gdb $GDB] + set gdb_fullpath [find_gdb $::GDB] if {[is_remote host]} { - set xgdb x$tool + set xgdb x$::tool } else { - set xgdb [standard_output_file x$tool] + set xgdb [standard_output_file x$::tool] } # Remove any old copy lying around. remote_file host delete $xgdb + set filename [remote_download host $gdb_fullpath $xgdb] + + return $filename +} + +# A simple way to run some self-tests. + +proc do_self_tests {function body} { + set file [selftest_prepare] + if { $file eq "" } { + return + } + gdb_start - set file [remote_download host $GDB_FULLPATH $xgdb] # When debugging GDB with GDB, some operations can take a relatively long # time, especially if the build is non-optimized. Bump the timeout for the From patchwork Mon Nov 27 17:55:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80837 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 D9555385AC1F for ; Mon, 27 Nov 2023 17:57:10 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id F35BD3857723 for ; Mon, 27 Nov 2023 17:56:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org F35BD3857723 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org F35BD3857723 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107776; cv=none; b=M8ZaSvfNL+OJNfyPWXsQYBc83Wjc0zSNYTAn671zN0yy/dXxY+GA8C5ychGAfJizO7tWPyDW7A5HBUhoCZJi4Q8ngFRxWHYFytAH+NY5NKxD0YXdMoM6eIXD4P93OGN7z+mCU64QTzv5ycx05jhC0nmcSbHEi0cKBr5ctjodsJ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107776; c=relaxed/simple; bh=bKChCrVcymm1sOWCjGJ1HTTmLY6mIS4Lv4fvBmfbU4M=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fS6xmizEe9Lm7ytN10mOhfL0buNX3P0KlWCZOFU+KF6tV4tJxKm1suQ7kD6epihurZiL8DLWSz9oW+X+It8ta4JVsaHVjoPSum6qx3kaEQ0M2zonK4tgUai1HBX6Qdf9MVH9UCreYro6s5gaoy+JOWwwzUkXZcYrVkD+Oold1Go= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=hzeGzuUfHE39DY3rsd/4OEiW4XPM/BR06Iv2zxiK+OM=; b=daKy8zWCsaWCPQHSBMVLLmElXOdqjnrXctgceij3m7y53Rcxbsy9L17iP6ZICS+sLeKU9e fyWTX+scXLIgbwzeQ60c8zoRH+LY1tQEMmtv67fRD9G4FFEMZfwiPP8sS6PPQ5HZQ8Q3gH yiuDiXO9MtkbuPeJi7lBrDN7Fis3ERA= Received: from mail-wm1-f71.google.com (mail-wm1-f71.google.com [209.85.128.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-110-3BIajJfNMoK1aGBpsL8xcg-1; Mon, 27 Nov 2023 12:56:10 -0500 X-MC-Unique: 3BIajJfNMoK1aGBpsL8xcg-1 Received: by mail-wm1-f71.google.com with SMTP id 5b1f17b1804b1-40b3d81399dso15570885e9.1 for ; Mon, 27 Nov 2023 09:56:10 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107769; x=1701712569; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=hzeGzuUfHE39DY3rsd/4OEiW4XPM/BR06Iv2zxiK+OM=; b=MOrg9xAQRjNnwV6svfDM6coBOj5aWLju7v2Rkj1X3DEzVGriZvYW8kquXLwNOxNSJH CwilvbEUGGHdYTT3g1U+0HMFxOUiIDREPcedCVVWa3XVpMYU5THfiZrveWyLpMRpyBlt nv4JWyrTm16VvWwBqxpwVViYhgLgoUzh/deWmUCZ/q7QiXMmkoeQEB75PfB9Ye9BLNnp LnLFTpyOIB7Q7/s5aQSHHDXzsxX2bNS8fLUWxSOOTzNzl0Ve+E1WcVOFDXlew3B9FWp8 M8xlTLkSdS2e9G61Y263/O7mZ8WEe4dMB98xnrR6bvvByOJqC250Zx18BZuxCaXbcSBK c4qQ== X-Gm-Message-State: AOJu0YzN37GE1BeMliB5NtzPy1xClHl7lyeGv59wB088b7GVJvu2Si4P wVPw0rE6BL3ZiM5uVR6BhBwxkcOprYEigJwEtY2Ci1GKVa9Dvw5fgEBFv9HzVilcStUkYqJbMJH RDl+rGwz+YrNPrI4fBM11YeiACA5C7XlqRvTc3BYC6i9Qg349Mh+DGN3Cr7nI7I3KJkH4DxFhB5 /rDEcChw== X-Received: by 2002:a05:600c:1912:b0:40b:47f3:589f with SMTP id j18-20020a05600c191200b0040b47f3589fmr2154095wmq.37.1701107769051; Mon, 27 Nov 2023 09:56:09 -0800 (PST) X-Google-Smtp-Source: AGHT+IFd+/SHIlqYKjWUTGmWwuk8qGivF5ZQ8YsfujBdpKy9Xze3RVzoTlcGakDjAuHnLksA1tilQg== X-Received: by 2002:a05:600c:1912:b0:40b:47f3:589f with SMTP id j18-20020a05600c191200b0040b47f3589fmr2154080wmq.37.1701107768631; Mon, 27 Nov 2023 09:56:08 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id l38-20020a05600c1d2600b0040b45282f88sm5265373wms.36.2023.11.27.09.56.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:08 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 4/7] gdb: reduce size of generated gdb-index file Date: Mon, 27 Nov 2023 17:55:58 +0000 Message-Id: <7700d3eccfdbd6105da398309ea8898fb658781b.1701107594.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 I noticed in passing that out algorithm for generating the gdb-index file is incorrect. When building the hash table in add_index_entry we count every incoming entry rehash when the number of entries gets too large. However, some of the incoming entries will be duplicates, which don't actually result in new items being added to the hash table. As a result, we grow the gdb-index hash table far too often. With an unmodified GDB, generating a gdb-index for GDB, I see a file size of 90M, with a hash usage (in the generated index file) of just 2.6%. With a patched GDB, generating a gdb-index for the _same_ GDB binary, I now see a gdb-index file size of 30M, with a hash usage of 41.9%. This is a 67% reduction in gdb-index file size. Obviously, not every gdb-index file is going to see such big savings, however, the larger a program, and the more symbols that are duplicated between compilation units, the more GDB would over count, and so, over-grow the index. The gdb-index hash table we create has a minimum size of 1024, and then we grow the hash when it is 75% full, doubling the hash table at that time. Given this, then we expect that either: a. The hash table is size 1024, and less than 75% full, or b. The hash table is between 37.5% and 75% full. I've include a test that checks some of these constraints -- I've not bothered to check the upper limit, and over full hash table isn't really a problem here, but if the fill percentage is less than 37.5% then this indicates that we've done something wrong (obviously, I also check for the 1024 minimum size). --- gdb/dwarf2/index-write.c | 29 ++++--- gdb/testsuite/gdb.gdb/index-file.exp | 115 +++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 10 deletions(-) create mode 100644 gdb/testsuite/gdb.gdb/index-file.exp diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index c0867799f6d..5960bacf8fb 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -256,20 +256,29 @@ add_index_entry (struct mapped_symtab *symtab, const char *name, int is_static, gdb_index_symbol_kind kind, offset_type cu_index) { - offset_type cu_index_and_attrs; + symtab_index_entry *slot = &find_slot (symtab, name); + if (slot->name == NULL) + { + /* This is a new element in the hash table. */ + ++symtab->n_elements; - ++symtab->n_elements; - if (4 * symtab->n_elements / 3 >= symtab->data.size ()) - hash_expand (symtab); + /* We might need to grow the hash table. */ + if (4 * symtab->n_elements / 3 >= symtab->data.size ()) + { + hash_expand (symtab); - symtab_index_entry &slot = find_slot (symtab, name); - if (slot.name == NULL) - { - slot.name = name; + /* This element will have a different slot in the new table. */ + slot = &find_slot (symtab, name); + + /* But it should still be a new element in the hash table. */ + gdb_assert (slot->name == nullptr); + } + + slot->name = name; /* index_offset is set later. */ } - cu_index_and_attrs = 0; + offset_type cu_index_and_attrs = 0; DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index); DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static); DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind); @@ -281,7 +290,7 @@ add_index_entry (struct mapped_symtab *symtab, const char *name, the last entry pushed), but a symbol could have multiple kinds in one CU. To keep things simple we don't worry about the duplication here and sort and uniquify the list after we've processed all symbols. */ - slot.cu_indices.push_back (cu_index_and_attrs); + slot->cu_indices.push_back (cu_index_and_attrs); } /* See symtab_index_entry. */ diff --git a/gdb/testsuite/gdb.gdb/index-file.exp b/gdb/testsuite/gdb.gdb/index-file.exp new file mode 100644 index 00000000000..c6edd286fb9 --- /dev/null +++ b/gdb/testsuite/gdb.gdb/index-file.exp @@ -0,0 +1,115 @@ +# Copyright 2023 Free Software Foundation, Inc. + +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +# Load the GDB executable, and then 'save gdb-index', and make some +# checks of the generated index file. + +load_lib selftest-support.exp + +# Can't save an index with readnow. +require !readnow + +# A multiplier used to ensure slow tasks are less likely to timeout. +set timeout_factor 20 + +set filename [selftest_prepare] +if { $filename eq "" } { + unsupported "${gdb_test_file_name}.exp" + return -1 +} + +with_timeout_factor $timeout_factor { + # Start GDB, load FILENAME. + clean_restart $filename +} + +# Generate an index file. +set dir1 [standard_output_file "index_1"] +remote_exec host "mkdir -p ${dir1}" +with_timeout_factor $timeout_factor { + gdb_test_no_output "save gdb-index $dir1" \ + "create gdb-index file" +} + +# Close GDB. +gdb_exit + +# Validate that the index-file FILENAME has made efficient use of its +# symbol hash table. Calculate the number of symbols in the hash +# table and the total hash table size. The hash table starts with +# 1024 entries, and then doubles each time it is filled to 75%. At +# 75% filled, doubling the size takes it to 37.5% filled. +# +# Thus, the hash table is correctly filled if: +# 1. Its size is 1024 (i.e. it has not yet had its first doubling), or +# 2. Its filled percentage is over 37% +# +# We could check that it is not over filled, but I don't as that's not +# really an issue. But we did once have a bug where the table was +# doubled incorrectly, in which case we'd see a filled percentage of +# around 2% in some cases, which is a huge waste of disk space. +proc check_symbol_table_usage { filename } { + # Open the file in binary mode and read-only mode. + set fp [open $filename rb] + + # Configure the channel to use binary translation. + fconfigure $fp -translation binary + + # Read the first 8 bytes of the file, which contain the header of + # the index section. + set header [read $fp [expr 7 * 4]] + + # Scan the header to get the version, the CU list offset, and the + # types CU list offset. + binary scan $header iiiiii version \ + _ _ _ symbol_table_offset shortcut_offset + + # The length of the symbol hash table (in entries). + set len [expr ($shortcut_offset - $symbol_table_offset) / 8] + + # Now walk the hash table and count how many entries are in use. + set offset $symbol_table_offset + set count 0 + while { $offset < $shortcut_offset } { + seek $fp $offset + set entry [read $fp 8] + binary scan $entry ii name_ptr flags + if { $name_ptr != 0 } { + incr count + } + + incr offset 8 + } + + # Close the file. + close $fp + + # Calculate how full the cache is. + set pct [expr (100 * double($count)) / $len] + + # Write our results out to the gdb.log. + verbose -log "Hash table size: $len" + verbose -log "Hash table entries: $count" + verbose -log "Percentage usage: $pct%" + + # The minimum fill percentage is actually 37.5%, but we give TCL a + # little flexibility in case the FP maths give a result a little + # off. + gdb_assert { $len == 1024 || $pct > 37 } \ + "symbol hash table usage" +} + +set index_filename_base [file tail $filename] +check_symbol_table_usage "$dir1/${index_filename_base}.gdb-index" From patchwork Mon Nov 27 17:55:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80835 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 A45903861855 for ; Mon, 27 Nov 2023 17:56:54 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id D0CF53858C35 for ; Mon, 27 Nov 2023 17:56:13 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D0CF53858C35 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D0CF53858C35 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107775; cv=none; b=Yjqp3TBAxgtTx0ID4NSm68WDKkxQwzJL6p4CUWVvKJqh+Tfh10meH9/qPt+qlDZT6uId9G39qX9ZUBNJ+D71g44PBAHaQFDJbeTYPpgXWGOaj2lPHdBFYKe9nTziaqiMRGrHtBdeM9tUtcAn33mJnivxCv0tSsDS9uODXPeylEQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107775; c=relaxed/simple; bh=T3S0fJkJWdzGf+YB2k5tMlVlc9C7Icy/vd0jyRDRCr8=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=sC5ohWa0hHobMQ1t5mYGxZEyD6EP+8edfftHbLrG6eGPLXq00sCpbNN969MiS8kWoic1tHTFFrlERK+F5Y/key4ybfNctKc6yH3hf3c7Kh3RPwHb702FzLh7RZY9koLXX3fqcCw6xG0F0714rMyvoe6z1C1TuxSWHKanuniOSyo= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107773; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=LS4gD/MR0++A77ZZ1qD66qfQwiU7+rWWYI1kivIpwlw=; b=YMM72TK2emRscrAKbV0sQl3uYZ4Pke3xkazfOdF1zrqPY7cqC5KGGOaj1jAk+sYYm1rWjG BqVsbRsMSib7WlaXq8BBXF4I1rSoFieReoO5qlxLyZFRtF26Yx+9zclitmdikbXvoZDT3G pDdUYDbf06d3oT7aWAAQzzSAPKAxHxs= Received: from mail-lf1-f72.google.com (mail-lf1-f72.google.com [209.85.167.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-500-tzzUjd0-MmKmBmEUk4lUnw-1; Mon, 27 Nov 2023 12:56:12 -0500 X-MC-Unique: tzzUjd0-MmKmBmEUk4lUnw-1 Received: by mail-lf1-f72.google.com with SMTP id 2adb3069b0e04-5091368e043so5321707e87.2 for ; Mon, 27 Nov 2023 09:56:11 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107770; x=1701712570; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LS4gD/MR0++A77ZZ1qD66qfQwiU7+rWWYI1kivIpwlw=; b=eNEK3xUxnxyDmPRdFDomR6EVlFPAh+U6aVMTHUxzofCSDBsLeexn5c9FS0Frj0cP5z laDaF5L19GeExxwzDKnl+e8Ef0Kq+PgRQrv59FBgvtiaVL8d/TArvKNK4gGXBvpRIYl4 PeUO3w4zOBk/zS1VSte1aUHEeM9Dn+G9rNTOmtYK3efRYcUUypcM/+2pZcIps8Eq1NC1 mhi3b108e/HqyvEFsmZ8bn9YrAThmadfHOoxfJn4/mjP/HspRIXIMkKncYOR2uRMTYDZ qSWAlZ6fX6bAAtaFYcObhyPi2NNw0KD0SbkKfa8E7v6aONEwg7rfoqE67OL3tcxZ1CaL KBsg== X-Gm-Message-State: AOJu0YxMkMJc42uQUSeDDKKr3u4aIFqhOdFTZ8jjsAsreiCb1cL4gCer 9dnvM94U8Udtdrm9q3tckrRY+0ZyOBKzOBPv3XcCoIjfTRp7V/ayF/7pYgYQtGNMdEDGKd8wo86 9cBHHHX/iFBbBMiQjnAmkiNxwlRY9KKeicI6oYJzAwKsX2ta5FlXOKkeJY5PEIZ5Ldyljvqd19n isajubFA== X-Received: by 2002:a05:651c:200d:b0:2c9:a1fd:ae35 with SMTP id s13-20020a05651c200d00b002c9a1fdae35mr2644619ljo.16.1701107770300; Mon, 27 Nov 2023 09:56:10 -0800 (PST) X-Google-Smtp-Source: AGHT+IFJ7+C3RgXjr+jI03zuM0e9jvWWFPr3jJ+K9ocO3cql7Zdkksv469EyyU2DK3QHDq6N+pWLKg== X-Received: by 2002:a05:651c:200d:b0:2c9:a1fd:ae35 with SMTP id s13-20020a05651c200d00b002c9a1fdae35mr2644606ljo.16.1701107769924; Mon, 27 Nov 2023 09:56:09 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id i12-20020a05600c354c00b004060f0a0fd5sm14609149wmq.13.2023.11.27.09.56.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:09 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 5/7] gdb: C++-ify mapped_symtab from dwarf2/index-write.c Date: Mon, 27 Nov 2023 17:55:59 +0000 Message-Id: X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_STOCKGEN, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 Make static the functions add_index_entry, find_slot, and hash_expand, member functions of the mapped_symtab class. Fold an additional snippet of code from write_gdbindex into mapped_symtab::minimize, this code relates to minimisation, so this seems like a good home for it. Make the n_elements, data, and m_string_obstack member variables of mapped_symtab private. Provide a new obstack() member function to provide access to the obstack when needed, and also add member functions begin(), end(), cbegin(), and cend() so that the mapped_symtab class can be treated like a contained and iterated over. I've also taken this opportunity to split out the logic for whether the hash table (m_data) needs expanding, this is the new function hash_needs_expanding. This will be useful in a later commit. There should be no user visible changes after this commit. --- gdb/dwarf2/index-write.c | 138 ++++++++++++++++++++++++++------------- 1 file changed, 92 insertions(+), 46 deletions(-) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index 5960bacf8fb..e1c343e8790 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -189,86 +189,135 @@ struct mapped_symtab { mapped_symtab () { - data.resize (1024); + m_data.resize (1024); } - /* Minimize each entry in the symbol table, removing duplicates. */ + /* If there are no elements in the symbol table, then reduce the table + size to zero. Otherwise call symtab_index_entry::minimize each entry + in the symbol table. */ + void minimize () { - for (symtab_index_entry &item : data) + if (m_element_count == 0) + m_data.resize (0); + + for (symtab_index_entry &item : m_data) item.minimize (); } - offset_type n_elements = 0; - std::vector data; + /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX is + the index of the CU in which the symbol appears. IS_STATIC is one if + the symbol is static, otherwise zero (global). */ + + void add_index_entry (const char *name, int is_static, + gdb_index_symbol_kind kind, offset_type cu_index); + + /* Access the obstack. */ + auto_obstack *obstack () + { return &m_string_obstack; } + +private: + + /* Find a slot in SYMTAB for the symbol NAME. Returns a reference to + the slot. + + Function is used only during write_hash_table so no index format + backward compatibility is needed. */ + + symtab_index_entry &find_slot (const char *name); + + /* Expand SYMTAB's hash table. */ + + void hash_expand (); + + /* Return true if the hash table in data needs to grow. */ + + bool hash_needs_expanding () const + { return 4 * m_element_count / 3 >= m_data.size (); } + + /* A vector that is used as a hash table. */ + std::vector m_data; + + /* The number of elements stored in the m_data hash. */ + offset_type m_element_count = 0; /* Temporary storage for names. */ auto_obstack m_string_obstack; -}; -/* Find a slot in SYMTAB for the symbol NAME. Returns a reference to - the slot. +public: + using iterator = decltype (m_data)::iterator; + using const_iterator = decltype (m_data)::const_iterator; - Function is used only during write_hash_table so no index format backward - compatibility is needed. */ + iterator begin () + { return m_data.begin (); } -static symtab_index_entry & -find_slot (struct mapped_symtab *symtab, const char *name) + iterator end () + { return m_data.end (); } + + const_iterator cbegin () + { return m_data.cbegin (); } + + const_iterator cend () + { return m_data.cend (); } +}; + +/* See class definition. */ + +symtab_index_entry & +mapped_symtab::find_slot (const char *name) { offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name); - index = hash & (symtab->data.size () - 1); - step = ((hash * 17) & (symtab->data.size () - 1)) | 1; + index = hash & (m_data.size () - 1); + step = ((hash * 17) & (m_data.size () - 1)) | 1; for (;;) { - if (symtab->data[index].name == NULL - || strcmp (name, symtab->data[index].name) == 0) - return symtab->data[index]; - index = (index + step) & (symtab->data.size () - 1); + if (m_data[index].name == NULL + || strcmp (name, m_data[index].name) == 0) + return m_data[index]; + index = (index + step) & (m_data.size () - 1); } } -/* Expand SYMTAB's hash table. */ +/* See class definition. */ -static void -hash_expand (struct mapped_symtab *symtab) +void +mapped_symtab::hash_expand () { - auto old_entries = std::move (symtab->data); + auto old_entries = std::move (m_data); - symtab->data.clear (); - symtab->data.resize (old_entries.size () * 2); + gdb_assert (m_data.size () == 0); + m_data.resize (old_entries.size () * 2); for (auto &it : old_entries) if (it.name != NULL) { - auto &ref = find_slot (symtab, it.name); + auto &ref = this->find_slot (it.name); ref = std::move (it); } } -/* Add an entry to SYMTAB. NAME is the name of the symbol. - CU_INDEX is the index of the CU in which the symbol appears. - IS_STATIC is one if the symbol is static, otherwise zero (global). */ +/* See class definition. */ -static void -add_index_entry (struct mapped_symtab *symtab, const char *name, - int is_static, gdb_index_symbol_kind kind, - offset_type cu_index) +void +mapped_symtab::add_index_entry (const char *name, int is_static, + gdb_index_symbol_kind kind, + offset_type cu_index) { - symtab_index_entry *slot = &find_slot (symtab, name); + symtab_index_entry *slot = &this->find_slot (name); if (slot->name == NULL) { /* This is a new element in the hash table. */ - ++symtab->n_elements; + ++this->m_element_count; /* We might need to grow the hash table. */ - if (4 * symtab->n_elements / 3 >= symtab->data.size ()) + if (this->hash_needs_expanding ()) { - hash_expand (symtab); + this->hash_expand (); /* This element will have a different slot in the new table. */ - slot = &find_slot (symtab, name); + slot = &this->find_slot (name); /* But it should still be a new element in the hash table. */ gdb_assert (slot->name == nullptr); @@ -389,7 +438,7 @@ write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool) /* We add all the index vectors to the constant pool first, to ensure alignment is ok. */ - for (symtab_index_entry &entry : symtab->data) + for (symtab_index_entry &entry : *symtab) { if (entry.name == NULL) continue; @@ -418,7 +467,7 @@ write_hash_table (mapped_symtab *symtab, data_buf &output, data_buf &cpool) /* Now write out the hash table. */ std::unordered_map str_table; - for (const auto &entry : symtab->data) + for (const auto &entry : *symtab) { offset_type str_off, vec_off; @@ -1159,7 +1208,7 @@ write_cooked_index (cooked_index *table, const auto it = cu_index_htab.find (entry->per_cu); gdb_assert (it != cu_index_htab.cend ()); - const char *name = entry->full_name (&symtab->m_string_obstack); + const char *name = entry->full_name (symtab->obstack ()); if (entry->per_cu->lang () == language_ada) { @@ -1167,8 +1216,7 @@ write_cooked_index (cooked_index *table, gdb, it has to use the encoded name, with any suffixes stripped. */ std::string encoded = ada_encode (name, false); - name = obstack_strdup (&symtab->m_string_obstack, - encoded.c_str ()); + name = obstack_strdup (symtab->obstack (), encoded.c_str ()); } else if (entry->per_cu->lang () == language_cplus && (entry->flags & IS_LINKAGE) != 0) @@ -1199,8 +1247,8 @@ write_cooked_index (cooked_index *table, else kind = GDB_INDEX_SYMBOL_KIND_TYPE; - add_index_entry (symtab, name, (entry->flags & IS_STATIC) != 0, - kind, it->second); + symtab->add_index_entry (name, (entry->flags & IS_STATIC) != 0, + kind, it->second); } } @@ -1303,8 +1351,6 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table, symtab.minimize (); data_buf symtab_vec, constant_pool; - if (symtab.n_elements == 0) - symtab.data.resize (0); write_hash_table (&symtab, symtab_vec, constant_pool); From patchwork Mon Nov 27 17:56:00 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80836 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 DE3A73861894 for ; Mon, 27 Nov 2023 17:57:01 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id C62203857BBD for ; Mon, 27 Nov 2023 17:56:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C62203857BBD Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org C62203857BBD Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107776; cv=none; b=x4D552msSHkFpArynTx3pzOhXBSE3dG2EAWlVjksE3fX8YAbRHEhLu6VMpmx1H7ddcXxyBHQ8QZV4nTEoIvhftGGPyacZS6dTNh3X7/lPWzG6T4WyPi7qQEwXaF+lU6R1kPgWhsPvDgX5IoxB1SIdBFWjlhsKP3P2BXqc9UdCGg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107776; c=relaxed/simple; bh=N9VSrfn8rmdqm2H9xYWMNjLXB41BgcKhIuWhq5zL8yI=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=fYqlP62eUhdCUK+RfOvVijRYSDyNU9KV/YtTvtkMYeYDx5DzEt28rt4kuIyujm/ffEzAbrJ4p21Dc+SBMbuWawEFCHn9beDRtcv7KHs8eVq3hNCX28Pnb9c/gDdDthBbZNLm4uoXyX0TGpgnpwikTBiiBVU1maTZvdxXWYPAh6s= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107774; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=a803Y+7TrO9OmdYOMRffjnb9tGwEID5OP6M8yiz4ce8=; b=KxLQKPHTJQ5zhby+fsUrYw+Ov/3x5mxVfqX61JONpfG3gdZdBb/AjYovrBmcTgEI4xhmhM OeikV7xlZlRvquVZs/XkKmcfge3ggpn0e7Q2YupMXp/jaj/QHVRn5ut2PFIcqFxSpoEg1m jkpvQkO0HdH51LKC8gnDLNmqYbr9j4E= Received: from mail-ej1-f72.google.com (mail-ej1-f72.google.com [209.85.218.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-391-7COxaWEKNf6kSLDonEuYEA-1; Mon, 27 Nov 2023 12:56:13 -0500 X-MC-Unique: 7COxaWEKNf6kSLDonEuYEA-1 Received: by mail-ej1-f72.google.com with SMTP id a640c23a62f3a-a01efffd9e4so335713566b.0 for ; Mon, 27 Nov 2023 09:56:12 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107772; x=1701712572; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=a803Y+7TrO9OmdYOMRffjnb9tGwEID5OP6M8yiz4ce8=; b=V2vxQ+jRivauAed41MfVfsld1o5mnBwSCWNbnxSnS35r2RoIy3RM0wIgNjL8uD9rYI HfLDOicAfTeoy/hmyQz3r/Y3OijGqNYw6iETdweDyHIUjSgOGJ+mKgzwDEiLgcTKVmvw lQx2Jv8wBG/JpHpVu38p35C0chsm4reDevT5Q13MCh0EUTW+znljCYPiGv+4wFQ+2Uqp VUOWQLE3sATRqqRHph0m/kUncAQp/B9+DO24Lpn+3N6hhkDUgAbF/1Hv8iTR6RGsOWRx hNu2GwCvaUxPQbFeQlMqUdctg3Oy/tA8ondTploAuXGfI4lZ3BBrMK/+gQj91GmZvJDm g5dg== X-Gm-Message-State: AOJu0YwLoQKEHOl+zToFa+oAYyv5UheLuK0YhWjLEQ3y+MhWui72btJh S1KRFhuIUDWwX61LQEL0oS2hhGjgSy0FQ9ZAZ6t2cp52bO2cuEQw01dgRS6cXOwpiXJWXHaIDUg 8klN4ETZgXUn01ycYbe6PDsgIZpRHFeMBA5/35xqMeIFk+tGxvaQ6T6z2FQMvomcVFM4hpo3nhB o7BQ2JJw== X-Received: by 2002:a17:907:c18:b0:9fd:cc84:78c0 with SMTP id ga24-20020a1709070c1800b009fdcc8478c0mr11161484ejc.72.1701107771716; Mon, 27 Nov 2023 09:56:11 -0800 (PST) X-Google-Smtp-Source: AGHT+IFDeggBsN9UgwR2hsN1v5nyEOVrDY5EXOe6XKHn+KuxWE/SfJRHpW7xu/LQiNZasvEk+kMX4Q== X-Received: by 2002:a17:907:c18:b0:9fd:cc84:78c0 with SMTP id ga24-20020a1709070c1800b009fdcc8478c0mr11161462ejc.72.1701107771312; Mon, 27 Nov 2023 09:56:11 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id mb22-20020a170906eb1600b009fc0c42098csm5961587ejb.173.2023.11.27.09.56.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:10 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 6/7] gdb: generate gdb-index identically regardless of work thread count Date: Mon, 27 Nov 2023 17:56:00 +0000 Message-Id: <07b6a4d980be1f86db37101e371731be3dce3ecf.1701107594.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 It was observed that changing the number of worker threads that GDB uses (maintenance set worker-threads NUM) would have an impact on the layout of the generated gdb-index. The cause seems to be how the CU are distributed between threads, and then symbols that appear in multiple CU can be encountered earlier or later depending on whether a particular CU moves between threads. I certainly found this behaviour was reproducible when generating an index for GDB itself, like: gdb -q -nx -nh -batch \ -eiex 'maint set worker-threads NUM' \ -ex 'save gdb-index /tmp/' And then setting different values for NUM will change the generated index. Now, the question is: does this matter? I would like to suggest that yes, this does matter. At Red Hat we generate a gdb-index as part of the build process, and we would ideally like to have reproducible builds: for the same source, compiled with the same tool-chain, we should get the exact same output binary. And we do .... except for the index. Now we could simply force GDB to only use a single worker thread when we build the index, but, I don't think the idea of reproducible builds is that strange, so I think we should ensure that our generated indexes are always reproducible. To achieve this, I propose that we add an extra step when building the gdb-index file. After constructing the initial symbol hash table contents, we will pull all the symbols out of the hash, sort them, then re-insert them in sorted order. This will ensure that the structure of the generated hash will remain consistent (given the same set of symbols). I've extended the existing index-file test to check that the generated index doesn't change if we adjust the number of worker threads used. Given that this test is already rather slow, I've only made one change to the worker-thread count. Maybe this test should be changed to use a smaller binary, which is quicker to load, and for which we could then try many different worker thread counts. --- gdb/dwarf2/index-write.c | 69 ++++++++++++++++++++++++++++ gdb/testsuite/gdb.gdb/index-file.exp | 41 +++++++++++++++++ gdb/testsuite/lib/gdb.exp | 15 ++++++ 3 files changed, 125 insertions(+) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index e1c343e8790..bc07bcb8f4c 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -212,6 +212,13 @@ struct mapped_symtab void add_index_entry (const char *name, int is_static, gdb_index_symbol_kind kind, offset_type cu_index); + /* When entries are originally added into the data hash the order will + vary based on the number of worker threads GDB is configured to use. + This function will rebuild the hash such that the final layout will be + deterministic regardless of the number of worker threads used. */ + + void sort (); + /* Access the obstack. */ auto_obstack *obstack () { return &m_string_obstack; } @@ -298,6 +305,65 @@ mapped_symtab::hash_expand () } } +/* See mapped_symtab class declaration. */ + +void mapped_symtab::sort () +{ + /* Move contents out of this->data vector. */ + std::vector original_data = std::move (m_data); + + /* Restore the size of m_data, this will avoid having to expand the hash + table (and rehash all elements) when we reinsert after sorting. + However, we do reset the element count, this allows for some sanity + checking asserts during the reinsert phase. */ + gdb_assert (m_data.size () == 0); + m_data.resize (original_data.size ()); + m_element_count = 0; + + /* Remove empty entries from ORIGINAL_DATA, this makes sorting quicker. */ + auto it = std::remove_if (original_data.begin (), original_data.end (), + [] (const symtab_index_entry &entry) -> bool + { + return entry.name == nullptr; + }); + original_data.erase (it, original_data.end ()); + + /* Sort the existing contents. */ + std::sort (original_data.begin (), original_data.end (), + [] (const symtab_index_entry &a, + const symtab_index_entry &b) -> bool + { + /* Return true if A is before B. */ + gdb_assert (a.name != nullptr); + gdb_assert (b.name != nullptr); + + return strcmp (a.name, b.name) < 0; + }); + + /* Re-insert each item from the sorted list. */ + for (auto &entry : original_data) + { + /* We know that ORIGINAL_DATA contains no duplicates, this data was + taken from a hash table that de-duplicated entries for us, so + count this as a new item. + + As we retained the original size of m_data (see above) then we + should never need to grow m_data_ during this re-insertion phase, + assert that now. */ + ++m_element_count; + gdb_assert (!this->hash_needs_expanding ()); + + /* Lookup a slot. */ + symtab_index_entry &slot = this->find_slot (entry.name); + + /* As discussed above, we should not find duplicates. */ + gdb_assert (slot.name == nullptr); + + /* Move this item into the slot we found. */ + slot = std::move (entry); + } +} + /* See class definition. */ void @@ -1346,6 +1412,9 @@ write_gdbindex (dwarf2_per_bfd *per_bfd, cooked_index *table, for (auto map : table->get_addrmaps ()) write_address_map (map, addr_vec, cu_index_htab); + /* Ensure symbol hash is built domestically. */ + symtab.sort (); + /* Now that we've processed all symbols we can shrink their cu_indices lists. */ symtab.minimize (); diff --git a/gdb/testsuite/gdb.gdb/index-file.exp b/gdb/testsuite/gdb.gdb/index-file.exp index c6edd286fb9..08415920061 100644 --- a/gdb/testsuite/gdb.gdb/index-file.exp +++ b/gdb/testsuite/gdb.gdb/index-file.exp @@ -35,6 +35,9 @@ with_timeout_factor $timeout_factor { clean_restart $filename } +# Record how many worker threads GDB is using. +set worker_threads [gdb_get_worker_threads] + # Generate an index file. set dir1 [standard_output_file "index_1"] remote_exec host "mkdir -p ${dir1}" @@ -113,3 +116,41 @@ proc check_symbol_table_usage { filename } { set index_filename_base [file tail $filename] check_symbol_table_usage "$dir1/${index_filename_base}.gdb-index" + +# If GDB is using more than 1 worker thread then reduce the number of +# worker threads, regenerate the index, and check that we get the same +# index file back. At one point the layout of the index would vary +# based on the number of worker threads used. +if { $worker_threads > 1 } { + # Start GDB, but don't load a file yet. + clean_restart + + # Adjust the number of threads to use. + set reduced_threads [expr $worker_threads / 2] + gdb_test_no_output "maint set worker-threads $reduced_threads" + + with_timeout_factor $timeout_factor { + # Now load the test binary. + gdb_file_cmd $filename + } + + # Generate an index file. + set dir2 [standard_output_file "index_2"] + remote_exec host "mkdir -p ${dir2}" + with_timeout_factor $timeout_factor { + gdb_test_no_output "save gdb-index $dir2" \ + "create second gdb-index file" + } + + # Close GDB. + gdb_exit + + # Now check that the index files are identical. + foreach suffix { gdb-index } { + set result \ + [remote_exec host \ + "cmp -s \"$dir1/${index_filename_base}.${suffix}\" \"$dir2/${index_filename_base}.${suffix}\""] + gdb_assert { [lindex $result 0] == 0 } \ + "$suffix files are identical" + } +} diff --git a/gdb/testsuite/lib/gdb.exp b/gdb/testsuite/lib/gdb.exp index 63885860795..b534a61d066 100644 --- a/gdb/testsuite/lib/gdb.exp +++ b/gdb/testsuite/lib/gdb.exp @@ -10026,6 +10026,21 @@ proc is_target_non_stop { {testname ""} } { return $is_non_stop } +# Return the number of worker threads that GDB is currently using. + +proc gdb_get_worker_threads { {testname ""} } { + set worker_threads "UNKNOWN" + gdb_test_multiple "maintenance show worker-threads" $testname { + -wrap -re "^The number of worker threads GDB can use is unlimited \\(currently ($::decimal)\\)\\." { + set worker_threads $expect_out(1,string) + } + -wrap -re "The number of worker threads GDB can use is ($::decimal)\\." { + set worker_threads $expect_out(1,string) + } + } + return $worker_threads +} + # Check if the compiler emits epilogue information associated # with the closing brace or with the last statement line. # From patchwork Mon Nov 27 17:56:01 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Burgess X-Patchwork-Id: 80838 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 D3D7C3858004 for ; Mon, 27 Nov 2023 17:57:26 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id 8EC36386186D for ; Mon, 27 Nov 2023 17:56:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8EC36386186D Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8EC36386186D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107782; cv=none; b=RUP6Zu8UkxbO8wWhpxYm83cxcv8b1yYi6JGiV/RgrtwUeQP40CdZI+yFP/A7GhCMOvfkdyLHCyW46UC90T4aLVDIRNTl8otFuT0j+xa1PM5BIgUJ8rsLiHgFnxpCiwcFa/djec+7Jm9y1DAuFcGLRukZCpKnPD/C/DPHWG1gXI4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1701107782; c=relaxed/simple; bh=2O5xRD6HL9Ddz8HN9XPMSnjGVvSi3aVbvmlkG5fyn4c=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=Q2ukAKrkD2beWTS0BFb2md+ZgRl2eeGdefXrsT7vSJuQ2Hv8ypzFzyFPrcD2DTlpqIvBmXXT0MTV5uv1tly6GlMBWqU02KKQtXi7WacD6w2I3TL4YHdsjQFTue6vKhs1rdgXDAAory9Sjgy+IrExG0djJuM1icT5YY/BWBHZFVI= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701107780; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dPZIjdABIZddpTNSfTlxpxAKUKtwyx1yxFnwVeAxVeY=; b=bReDDgI61LfXN+dT2/4GhpGk/ybb1esv1yhI4U81W4pi+neEoiLCSYcq1KD4tSS4HbwITt xcq940+pBbgjm5LwXjMmPYs2/saAaUUtM3zKgwl2pUioU8UO6GKwCtY78OLmATYCAfpuTk KYI00qdmFVPBUW6WecrvMK/QziM7h4o= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-15-rZRJq3VsM1uAEcQP7rBMtw-1; Mon, 27 Nov 2023 12:56:14 -0500 X-MC-Unique: rZRJq3VsM1uAEcQP7rBMtw-1 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-332f6a34782so1705782f8f.2 for ; Mon, 27 Nov 2023 09:56:14 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1701107773; x=1701712573; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dPZIjdABIZddpTNSfTlxpxAKUKtwyx1yxFnwVeAxVeY=; b=wtUsKVM3NDD0ynTiHpQTqTHGZexrrMpWPgoP3cZe3tWSt5srEYgukIEHXolRIcW2gr +UeUTucD3MjAuGG2dvpA7hI0opmLH++q8/ruMXKeYz02lT0ADUlYlOuxEWMo55y7MLgc U3qYqVa8ix1b0AimlmC8KfSxsmz2fgKBqvgj7d+/g464tLlt/TGA141G1pgHiHA6SGn3 cZ7aBTifS4rSeBC/k4ORjI4YK/p/7rIwFCqn/YKss2nlPMgWiN1ToNW3reuHMnixdE1s OKtv3KP6YGSZGpjXTsysG9ecRiRUEySQkSbTplv42OZ5ufGZYbBXgA/QBQGPAk3bRqhu QEjQ== X-Gm-Message-State: AOJu0YxTcvOVtelGnG8kv/fhzQkxCuiuT4/fx/uw+alKQ5d8Ts83kB1a XevoTfU5CK6Hi+6s1tOYIvrDuxdJP6hK0Xx/S9cYRYcvvfwaPFl/yc2Tl6IPrOdehjtRGt/gPhZ zx6wwrh/AInFMccdNwHg5Jvb58RgWuE7SF1o0PT7ZfBTcx/PzAfokmqM9gWXgrb6cfrpaP70WIh J2+RqXjg== X-Received: by 2002:a5d:594f:0:b0:332:f9e8:ce14 with SMTP id e15-20020a5d594f000000b00332f9e8ce14mr3831004wri.29.1701107773529; Mon, 27 Nov 2023 09:56:13 -0800 (PST) X-Google-Smtp-Source: AGHT+IF17lGJa+BCgSk9Yi0/s6llmv/lMqM1yVR01p7FAoDS5vP98NKd9keiU4tYqDEs159lJ1O3hw== X-Received: by 2002:a5d:594f:0:b0:332:f9e8:ce14 with SMTP id e15-20020a5d594f000000b00332f9e8ce14mr3830992wri.29.1701107773100; Mon, 27 Nov 2023 09:56:13 -0800 (PST) Received: from localhost (105.226.159.143.dyn.plus.net. [143.159.226.105]) by smtp.gmail.com with ESMTPSA id t2-20020a5d4602000000b0032da4c98ab2sm12860609wrq.35.2023.11.27.09.56.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Nov 2023 09:56:12 -0800 (PST) From: Andrew Burgess To: gdb-patches@sourceware.org Cc: Andrew Burgess Subject: [PATCH 7/7] gdb: generate dwarf-5 index identically as worker-thread count changes Date: Mon, 27 Nov 2023 17:56:01 +0000 Message-Id: <00faec4e7a5d52052639e6f23c3a0ffc670278f5.1701107594.git.aburgess@redhat.com> X-Mailer: git-send-email 2.25.4 In-Reply-To: References: MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.8 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 Similar to the previous commit, this commit ensures that the dwarf-5 index files are generated identically as the number of worker-threads changes. Building the dwarf-5 index makes use of a closed hash table, the bucket_hash local within debug_names::build(). Entries are added to bucket_hash from m_name_to_value_set, which, in turn, is populated by calls to debug_names::insert() in write_debug_names. The insert calls are ordered based on the entries within the cooked_index, and the ordering within cooked_index depends on the number of worker threads that GDB is using. My proposal is to sort each chain within the bucket_hash closed hash table prior to using this to build the dwarf-5 index. The buckets within bucket_hash will always have the same ordering (for a given GDB build with a given executable), and by sorting the chains within each bucket, we can be sure that GDB will see each entry in a deterministic order. I've extended the index creation test to cover this case. --- gdb/dwarf2/index-write.c | 17 +++++++++++++++-- gdb/testsuite/gdb.gdb/index-file.exp | 8 +++++++- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/gdb/dwarf2/index-write.c b/gdb/dwarf2/index-write.c index bc07bcb8f4c..1bc0c8ab57e 100644 --- a/gdb/dwarf2/index-write.c +++ b/gdb/dwarf2/index-write.c @@ -454,6 +454,11 @@ class c_str_view return strcmp (m_cstr, other.m_cstr) == 0; } + bool operator< (const c_str_view &other) const + { + return strcmp (m_cstr, other.m_cstr) < 0; + } + /* Return the underlying C string. Note, the returned string is only a reference with lifetime of this object. */ const char *c_str () const @@ -773,10 +778,18 @@ class debug_names } for (size_t bucket_ix = 0; bucket_ix < bucket_hash.size (); ++bucket_ix) { - const std::forward_list &hashitlist - = bucket_hash[bucket_ix]; + std::forward_list &hashitlist = bucket_hash[bucket_ix]; if (hashitlist.empty ()) continue; + + /* Sort the items within each bucket. This ensures that the + generated index files will be the same no matter the order in + which symbols were added into the index. */ + hashitlist.sort ([] (const hash_it_pair &a, const hash_it_pair &b) + { + return a.it->first < b.it->first; + }); + uint32_t &bucket_slot = m_bucket_table[bucket_ix]; /* The hashes array is indexed starting at 1. */ store_unsigned_integer (reinterpret_cast (&bucket_slot), diff --git a/gdb/testsuite/gdb.gdb/index-file.exp b/gdb/testsuite/gdb.gdb/index-file.exp index 08415920061..7154d234dd5 100644 --- a/gdb/testsuite/gdb.gdb/index-file.exp +++ b/gdb/testsuite/gdb.gdb/index-file.exp @@ -44,6 +44,9 @@ remote_exec host "mkdir -p ${dir1}" with_timeout_factor $timeout_factor { gdb_test_no_output "save gdb-index $dir1" \ "create gdb-index file" + + gdb_test_no_output "save gdb-index -dwarf-5 $dir1" \ + "create dwarf-index files" } # Close GDB. @@ -140,13 +143,16 @@ if { $worker_threads > 1 } { with_timeout_factor $timeout_factor { gdb_test_no_output "save gdb-index $dir2" \ "create second gdb-index file" + + gdb_test_no_output "save gdb-index -dwarf-5 $dir2" \ + "create second dwarf-index files" } # Close GDB. gdb_exit # Now check that the index files are identical. - foreach suffix { gdb-index } { + foreach suffix { gdb-index debug_names debug_str } { set result \ [remote_exec host \ "cmp -s \"$dir1/${index_filename_base}.${suffix}\" \"$dir2/${index_filename_base}.${suffix}\""]