From patchwork Mon Mar 25 14:00:52 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Hawkins X-Patchwork-Id: 87632 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 33D333858409 for ; Mon, 25 Mar 2024 14:01:44 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-qk1-x72e.google.com (mail-qk1-x72e.google.com [IPv6:2607:f8b0:4864:20::72e]) by sourceware.org (Postfix) with ESMTPS id A680A3858D3C for ; Mon, 25 Mar 2024 14:01:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A680A3858D3C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=obs.cr Authentication-Results: sourceware.org; spf=none smtp.mailfrom=obs.cr ARC-Filter: OpenARC Filter v1.0.0 sourceware.org A680A3858D3C Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::72e ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711375275; cv=none; b=PNF/OaByvFaxNYhGvbZgtm/unPy9NYDDDW2wSnmzk0+zthOWj3kzBjWtT1kD4lIzZJ1d6eigMUJcmzHySoXl73Q1pmGK6FV4HO2lQPd+i+BP+/coDvvyZ7NufWSbzcAIPnzC1efcfRAlCmXZ+k3X0Fkc2kcCoa/Ml2P+fI5TuMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711375275; c=relaxed/simple; bh=o0G37R4046JL3MqsZ8YY8r+fmTmhV/AvZEeik+f5AAw=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=nQAu/yiaoEarsLNhNJHSCJTTv1SjSOAgdGU+s/Ke/kJfnUf9W9Eqbh5lH1MGKAHnUEYgqiPFU6u4kruETOfl6eJfUPrRE4C7SsKabQ+KBMfisyZc3xymf8lfgM48/xcj+xBpU2RjfgDe5exlXkmYE8IjFO6cAaJ4wcjL77ydveA= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x72e.google.com with SMTP id af79cd13be357-78a3ca01301so181313885a.1 for ; Mon, 25 Mar 2024 07:01:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=obs-cr.20230601.gappssmtp.com; s=20230601; t=1711375266; x=1711980066; darn=sourceware.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=fYDj1iAiXgQcxJ8m78+J1vjc7BSO+UIpED7wsYz/Vv8=; b=1v6b8TTlKXnfnkTbCHRI3he43BU3HEUdWpgJvK5O06XlR7grWcvQbHQJwQb7r4oqsn uSOS2pSalYpT0n/px7+d4pASuYwdPv7QkGtXRm/WZmfROX5P6K1Im1qrAtOMdDcWAqID 6SgiRomneEZTLOg2YO05jz/HyeGwfQiTZY7itYsNXWvfS286R13R04dvGTCWFA4GElVC 7V77AomGRwWDzkfmAG+pYk2rL4yAMeIyoYvG01RBKV/4OOiMCB2KKVhCD5Em3Dyw9HWM BljRhsfi35mjNgbt5LrDkcMf6bEaJ/HcLxXQdTzGo45SiacXeTN72jXPJn9uRwNh9M9C QfjA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711375266; x=1711980066; 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=fYDj1iAiXgQcxJ8m78+J1vjc7BSO+UIpED7wsYz/Vv8=; b=nwnvKakIEpswwKhv8EAmvs0J5H5aus7O5D9Jmc4Rk8XdIScD4+j8D11jI77AYUXMZ2 bIQe2Lla6hguJ7anN6YoeFwnVTFrQKwl717L/yZjHcTw5GQCVNxeju75az1xHwkSZ9Tn M04yjGaLxAtZEQfJj7QWYf9jBlUsP4JCk76TKnMD9sPGWyhjbcRFYAIZ7I4rXwVIHYt/ PMxufrQ5ABdm7UAGK7dgYAt7QGTrWT/Kbt89Wz8mNXRq6e8WrbDOeljNbhZ0JdBNmCkD wtmrROYqUXKJFuZXPKX27XZKLnyEp1AKZV90CgWcrgd8qTv1DMjq2syoilm+z6kifr3r ni3A== X-Gm-Message-State: AOJu0YwyH1fummQ20VHqMcdsfRGBtKVc68ClbuL8sm1tkvOoBN/LtBQ+ YC6Axb0YSgZpa2G4Nuk5aHRk+lYjz/J9T0q94axL2rT/J/sNubXDTusiPS8LuMFp07NtT+kEigq s X-Google-Smtp-Source: AGHT+IFEoxeWIghIpgkrQTw1Bzg9E7wjHRs9J8VVVQb6LDftBGeoi84T576AnMy7YN1UiW9oyeNpbQ== X-Received: by 2002:a05:620a:6108:b0:789:dc20:3a4a with SMTP id oq8-20020a05620a610800b00789dc203a4amr7670465qkn.18.1711375266377; Mon, 25 Mar 2024 07:01:06 -0700 (PDT) Received: from ininer.rhod.uc.edu ([129.137.96.15]) by smtp.gmail.com with ESMTPSA id o6-20020ae9f506000000b0078a4541720csm2053521qkg.79.2024.03.25.07.01.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 07:01:06 -0700 (PDT) From: Will Hawkins To: gdb-patches@sourceware.org Cc: Will Hawkins Subject: [PATCH v2] Make source.c:search_command_helper use source cache Date: Mon, 25 Mar 2024 10:00:52 -0400 Message-ID: <20240325140103.157217-1-hawkinsw@obs.cr> X-Mailer: git-send-email 2.44.0 In-Reply-To: <20240324011828.145140-1-hawkinsw@obs.cr> References: <20240324011828.145140-1-hawkinsw@obs.cr> MIME-Version: 1.0 X-Spam-Status: No, score=-9.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, 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 The current implementation of search_command_helper accesses the line offsets of the current program spaces's source code through the source cache but then accesses its contents through the disk. This PR updates the implementation so that the access of the contents is also through the source cache. Tested on x86_64-redhat-linux. Signed-off-by: Will Hawkins --- v1 -> v2: Remove leftover debugging code. Rationale: Supporting access through the source cache during searching will make it easier to implement the embedded source functionality of DW_LNCT_source. gdb/source.c | 79 +++++++++++++++++++++++++--------------------------- 1 file changed, 38 insertions(+), 41 deletions(-) diff --git a/gdb/source.c b/gdb/source.c index bbeb4154258..ef042b0e96e 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -1617,46 +1617,45 @@ search_command_helper (const char *regex, int from_tty, bool forward) if (!source_open) error (_("source code access disabled")); - scoped_fd desc (open_source_file (loc->symtab ())); - if (desc.get () < 0) - perror_with_name (symtab_to_filename_for_display (loc->symtab ()), - -desc.get ()); - - int line = (forward - ? last_line_listed + 1 - : last_line_listed - 1); - const std::vector *offsets; - if (line < 1 - || !g_source_cache.get_line_charpos (loc->symtab (), &offsets) - || line > offsets->size ()) + if (!g_source_cache.get_line_charpos (loc->symtab (), &offsets)) error (_("Expression not found")); - if (lseek (desc.get (), (*offsets)[line - 1], 0) < 0) - perror_with_name (symtab_to_filename_for_display (loc->symtab ())); + std::string lines; + if (!g_source_cache.get_source_lines (loc->symtab (), 1, offsets->size (), + &lines)) + perror_with_name (symtab_to_filename_for_display (loc->symtab ()), -ENOENT); + + int line_no = (forward + ? last_line_listed + 1 + : last_line_listed - 1); - gdb_file_up stream = desc.to_file (FDOPEN_MODE); - clearerr (stream.get ()); + if (line_no < 1 + || line_no > offsets->size ()) + error (_("Expression not found")); + + size_t current_line_start = (*offsets)[line_no - 1]; - gdb::def_vector buf; - buf.reserve (256); + std::string buf; while (1) { - buf.resize (0); - - int c = fgetc (stream.get ()); - if (c == EOF) + /* Invariant: current_line_start is either the index + of the start of the current line in LINES *or* the + length of the source code (LINES, when there is nothing + else to do). */ + if (current_line_start == lines.length ()) break; - do - { - buf.push_back (c); - } - while (c != '\n' && (c = fgetc (stream.get ())) >= 0); + + size_t current_line_end = ((line_no + 1) > offsets->size () + ? lines.size () - 1 + : (*offsets)[line_no] - 1); + + size_t sz = current_line_end - current_line_start; + buf = lines.substr (current_line_start, sz); /* Remove the \r, if any, at the end of the line, otherwise regular expressions that end with $ or \n won't work. */ - size_t sz = buf.size (); if (sz >= 2 && buf[sz - 2] == '\r') { buf[sz - 2] = '\n'; @@ -1664,29 +1663,27 @@ search_command_helper (const char *regex, int from_tty, bool forward) } /* We now have a source line in buf, null terminate and match. */ - buf.push_back ('\0'); + buf += '\0'; if (re_exec (buf.data ()) > 0) { /* Match! */ - print_source_lines (loc->symtab (), line, line + 1, 0); - set_internalvar_integer (lookup_internalvar ("_"), line); - loc->set (loc->symtab (), std::max (line - lines_to_list / 2, 1)); + print_source_lines (loc->symtab (), line_no, line_no + 1, 0); + set_internalvar_integer (lookup_internalvar ("_"), line_no); + loc->set (loc->symtab (), std::max (line_no - lines_to_list / 2, 1)); return; } if (forward) - line++; + { + line_no++; + current_line_start = current_line_end + 1; + } else { - line--; - if (line < 1) + line_no--; + if (line_no < 1) break; - if (fseek (stream.get (), (*offsets)[line - 1], 0) < 0) - { - const char *filename - = symtab_to_filename_for_display (loc->symtab ()); - perror_with_name (filename); - } + current_line_start = (*offsets)[line_no - 1]; } }