PR24065, 32-bit objcopy fails with 64-bit address ... out of range
[deliverable/binutils-gdb.git] / gdb / source.c
index 575e46c2123e66a58eb20f3f227b881601e421cd..ad6c6466b4470dcfa410bdf11a39027acfe4c856 100644 (file)
@@ -1,5 +1,5 @@
 /* List lines of source files for GDB, the GNU debugger.
-   Copyright (C) 1986-2018 Free Software Foundation, Inc.
+   Copyright (C) 1986-2019 Free Software Foundation, Inc.
 
    This file is part of GDB.
 
@@ -45,6 +45,7 @@
 #include "common/scoped_fd.h"
 #include <algorithm>
 #include "common/pathstuff.h"
+#include "source-cache.h"
 
 #define OPEN_MODE (O_RDONLY | O_BINARY)
 #define FDOPEN_MODE FOPEN_RB
@@ -393,6 +394,7 @@ forget_cached_source_info (void)
       forget_cached_source_info_for_objfile (objfile);
     }
 
+  g_source_cache.clear ();
   last_source_visited = NULL;
 }
 
@@ -1344,25 +1346,18 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
 
   last_source_error = 0;
 
-  if (s->line_charpos == 0)
-    find_source_lines (s, desc.get ());
-
-  if (line < 1 || line > s->nlines)
+  std::string lines;
+  if (!g_source_cache.get_source_lines (s, line, stopline - 1, &lines))
     error (_("Line number %d out of range; %s has %d lines."),
           line, symtab_to_filename_for_display (s), s->nlines);
 
-  if (lseek (desc.get (), s->line_charpos[line - 1], 0) < 0)
-    perror_with_name (symtab_to_filename_for_display (s));
-
-  gdb_file_up stream = desc.to_file (FDOPEN_MODE);
-  clearerr (stream.get ());
-
+  const char *iter = lines.c_str ();
   while (nlines-- > 0)
     {
       char buf[20];
 
-      c = fgetc (stream.get ());
-      if (c == EOF)
+      c = *iter++;
+      if (c == '\0')
        break;
       last_line_listed = current_source_line;
       if (flags & PRINT_SOURCE_LINES_FILENAME)
@@ -1374,7 +1369,7 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
       uiout->text (buf);
       do
        {
-         if (c < 040 && c != '\t' && c != '\n' && c != '\r')
+         if (c < 040 && c != '\t' && c != '\n' && c != '\r' && c != '\033')
            {
              xsnprintf (buf, sizeof (buf), "^%c", c + 0100);
              uiout->text (buf);
@@ -1384,12 +1379,13 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
          else if (c == '\r')
            {
              /* Skip a \r character, but only before a \n.  */
-             int c1 = fgetc (stream.get ());
-
-             if (c1 != '\n')
+             if (iter[1] == '\n')
+               {
+                 ++iter;
+                 c = '\n';
+               }
+             else
                printf_filtered ("^%c", c + 0100);
-             if (c1 != EOF)
-               ungetc (c1, stream.get ());
            }
          else
            {
@@ -1397,8 +1393,12 @@ print_source_lines_base (struct symtab *s, int line, int stopline,
              uiout->text (buf);
            }
        }
-      while (c != '\n' && (c = fgetc (stream.get ())) >= 0);
+      while (c != '\n' && (c = *iter++) != '\0');
+      if (c == '\0')
+       break;
     }
+  if (lines.back () != '\n')
+    uiout->text ("\n");
 }
 \f
 /* Show source lines from the file of symtab S, starting with line
@@ -1598,6 +1598,8 @@ search_command_helper (const char *regex, int from_tty, bool forward)
       else
        {
          line--;
+         if (line < 1)
+           break;
          if (fseek (stream.get (),
                     current_source_symtab->line_charpos[line - 1], 0) < 0)
            {
This page took 0.025404 seconds and 4 git commands to generate.