Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Make source.c:search_command_helper use source cache
@ 2024-03-24  1:18 Will Hawkins
  2024-03-25 14:00 ` [PATCH v2] " Will Hawkins
  0 siblings, 1 reply; 9+ messages in thread
From: Will Hawkins @ 2024-03-24  1:18 UTC (permalink / raw)
  To: gdb-patches; +Cc: Will Hawkins

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 <hawkinsw@obs.cr>
---

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<off_t> *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<char> 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];
 	}
     }
 
-- 
2.44.0


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2024-04-19 22:30 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-24  1:18 [PATCH] Make source.c:search_command_helper use source cache Will Hawkins
2024-03-25 14:00 ` [PATCH v2] " Will Hawkins
2024-03-29 14:46   ` Will Hawkins
2024-04-08 18:12     ` Will Hawkins
2024-04-18  1:28       ` Will Hawkins
2024-04-19 19:41     ` Tom Tromey
2024-04-19 22:28       ` Will Hawkins
2024-04-19 19:40   ` Tom Tromey
2024-04-19 22:30     ` Will Hawkins

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox