From patchwork Sun Mar 24 01:18:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Will Hawkins X-Patchwork-Id: 87601 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 8D2383858D3C for ; Sun, 24 Mar 2024 01:19:55 +0000 (GMT) X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from mail-qk1-x730.google.com (mail-qk1-x730.google.com [IPv6:2607:f8b0:4864:20::730]) by sourceware.org (Postfix) with ESMTPS id DF2473858D28 for ; Sun, 24 Mar 2024 01:18:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org DF2473858D28 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 DF2473858D28 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::730 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711243119; cv=none; b=jJMzw6qe8ywAClEVGH7LSVoHvTh2+jlkwlRAM+hKv12q3hB7lWyM3AHnu2ZWhjd421Tkbfjd1tosUPYiRl9G6mTa1RjSeyY1wGnWo2J4p5H33PlnJFYpQNL6HGBBUE5fI/msO3ulNMuR+lzY4GlDfSvoCRA9VUSJLhvl3Mi2IHA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1711243119; c=relaxed/simple; bh=pEPdcBjyTQHXS4tNmCMwLKNpYH3IkIFfmXafyWXttag=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=mbYYBPfP59eADGdU84N07txTYAWk5vzHqrVD/ietNiqxmyCMmsWbZmgveyjs+MMByv3stvWZAoq9xjmhcAiqBJL0JFI5j2whh/T8qSdMz7SbEY9wlIt1fHll1cefdVih6bJGpzZVPnGvxDEKksK80DJMhn22Nixi6ZzAyOQWqKg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-qk1-x730.google.com with SMTP id af79cd13be357-78a16114b69so307790385a.0 for ; Sat, 23 Mar 2024 18:18:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=obs-cr.20230601.gappssmtp.com; s=20230601; t=1711243112; x=1711847912; 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=FfsHlEuYRyy3Rf9GVlhChkC6QRVQVzCOHaV/QhBMgS8=; b=FAAhqu2nULSMIcdWqCuvK9lvoA529er2jxIJusnKyhUEgCs/MwBGIxpiBVUqFs0TJY 0oXkaPjbEjkxRGHSZsBmU+EUuhkAXCR0+cm+FZj0x2p0l/xE9oucIFLHnF2EyqJyuvVw nzvrDX3IIEF9QtQacY9lBTfR7lQBv8XnrGuH+xc3BCqwOQrxfzmnyanDmGvH2F19lTCk ySSzNtT2lzWirfJ6Kegd1apiku2kmIUyYmJfrC0UFte1niA/tkJrGZSEinJ7hk1EPmso MhqzzxO1blCBYhpV3WovOzdvnyCKE2mP6ubaY7H11fzL+Vcxwr1WCKdPvvBy+9oJWdcl Fl0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711243112; x=1711847912; 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=FfsHlEuYRyy3Rf9GVlhChkC6QRVQVzCOHaV/QhBMgS8=; b=GslSKSuwZdf2on4XLosCLjMdG8DyizIJh8LkAV+KVxI4IbxfNhVK2UBWfKsk3bnHPM FTRUh/VUkUdvgguJFMMC5njry9br6DaryZls/aZWREtU6i4NOaIhBr2jRIQXVnG3NHvt DytVaZxllzLEJHRrAL/Cma0pV+EKYhmXSmxxa+TgOHBTxhrZ588geKWdvB6BQOaj7MhY eFbF6PR8XUozpWwD4z8XfWtvIA2Ng0T/1yPSjXXoOOhzeiN3whAYQcS6eEKk7BXvrymr og5Hf9vXn02ZVNGgFnXjpQsOP91EtLx3yEnUOJLsdb1+I8uuB17LfLVYzDQpms4Og7Il JRJQ== X-Gm-Message-State: AOJu0Yxs7wAMliHa2uKwOl0q9XTpqIIjBCeb6+OteCOXOreMBxW1rcWk mLm8GWp3efQWk/p1MnGWZ82w469NvCPdDtaEboOnbsU9jhRvcHawRzZDBGGSCyUTPZMEicNxfr1 h X-Google-Smtp-Source: AGHT+IEe7XQn29E0yn7v8xuS09N9MjUQU/WV8VYSI259i4tFgy3ViIAXbLaGT4vw8j91j9AJWWCcgw== X-Received: by 2002:a05:620a:8109:b0:78a:3524:11f with SMTP id os9-20020a05620a810900b0078a3524011fmr4740488qkn.10.1711243112071; Sat, 23 Mar 2024 18:18:32 -0700 (PDT) Received: from ininer.rhod.uc.edu ([129.137.96.15]) by smtp.gmail.com with ESMTPSA id e10-20020a05620a12ca00b007885e3275e9sm1029009qkl.132.2024.03.23.18.18.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 Mar 2024 18:18:31 -0700 (PDT) From: Will Hawkins To: gdb-patches@sourceware.org Cc: Will Hawkins Subject: [PATCH] Make source.c:search_command_helper use source cache Date: Sat, 23 Mar 2024 21:18:26 -0400 Message-ID: <20240324011828.145140-1-hawkinsw@obs.cr> X-Mailer: git-send-email 2.44.0 MIME-Version: 1.0 X-Spam-Status: No, score=-9.3 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 --- Notes: 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 | 87 +++++++++++++++++++++++++++------------------------- 1 file changed, 46 insertions(+), 41 deletions(-) diff --git a/gdb/source.c b/gdb/source.c index bbeb4154258..81432c7b527 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -1617,46 +1617,53 @@ 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); +#if 0 + gdb_printf (_("sz: %lu\n"), sz); + gdb_printf (_("line: %d\n"), line); + gdb_printf (_("current_line_start: %lu\n"), current_line_start); + gdb_printf (_("current_line_end: %lu\n"), current_line_end); + gdb_printf (_("lines.size (): %lu\n"), lines.size ()); + gdb_printf (_("buf: -%s-\n"), buf.data ()); +#endif /* 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 +1671,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]; } }