]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
gdb: adjust the default place of 'list' to main's prologue
authorStephan Rohr <stephan.rohr@intel.com>
Sat, 3 Aug 2024 09:07:42 +0000 (02:07 -0700)
committerAndrew Burgess <aburgess@redhat.com>
Fri, 9 Aug 2024 10:29:51 +0000 (11:29 +0100)
The 'list' command prints around the 'main' function if the current
source location is not set.  The prologue of 'main' is skipped and the
first real line of 'main' is offset by 'lines_to_print - 1'.  This is
incorrect, the location should be defaulted to main's prologue without
applying offsets (similar to 'list main').  Printing around the selected
line is then done in 'list_around_line'.

The patch also fixes an issue if the list command is used before the
program is started.  For example, with the following code:

26 static void attribute ((used)) ambiguous_fun (void) {}
27
28 static int attribute ((used)) ambiguous_var;
29
30
31
32
33
34
35
36
37
38 int
39 main (void)
40 {
41   return 0;
42 }

GDB offsets the relevant line by 'lines_to_print - 1' and then by another
'lines_to_print / 2' and prints:

(gdb) list
27
28 static int attribute ((used)) ambiguous_var;
29
30
31
32
33
34
35
36

With this patch, GDB correctly prints:

37
38      int
39      main (void)
40      {
41        return 0;
42      }

Approved-By: Andrew Burgess <aburgess@redhat.com>
gdb/source.c
gdb/testsuite/gdb.base/cursal.c
gdb/testsuite/gdb.base/cursal.exp
gdb/testsuite/gdb.base/list-ambiguous0.c
gdb/testsuite/gdb.base/list-before-start.exp [new file with mode: 0644]
gdb/testsuite/gdb.mi/mi-file.exp

index 25d6e7183a723df65e22b8f0dfcadb97435c8de7..0c5c2bad81915f6024b6dc2b0b90a79c1d10b11e 100644 (file)
@@ -319,13 +319,13 @@ select_source_symtab ()
                                     SEARCH_FUNCTION_DOMAIN, nullptr);
   if (bsym.symbol != nullptr)
     {
-      symtab_and_line sal = find_function_start_sal (bsym.symbol, true);
+      symtab_and_line sal = find_function_start_sal (bsym.symbol, false);
       if (sal.symtab == NULL)
        /* We couldn't find the location of `main', possibly due to missing
           line number info, fall back to line 1 in the corresponding file.  */
        loc->set (bsym.symbol->symtab (), 1);
       else
-       loc->set (sal.symtab, std::max (sal.line - (lines_to_list - 1), 1));
+       loc->set (sal.symtab, sal.line);
       return;
     }
 
index 8dc713f3541968d488894e611bfb9a2a0bfd176e..bc1e46f0e4c871a045a63f2442c5012e9b557937 100644 (file)
@@ -31,7 +31,7 @@ func1 ()
 
 int
 main ()
-{
+{ /* main prologue */
   int v0 = 0;
   func1 ();
 
index 3acced86e3c75ffb771543220d6d0f31ad0b49db..6c1fe3812befcb96f1b91ba12ba414fda93d63c5 100644 (file)
@@ -25,9 +25,9 @@ clean_restart
 gdb_file_cmd ${binfile}
 gdb_test_no_output "set listsize 1"
 
-# initial sal should be first statement in main
+# initial sal should be main's prologue.
 gdb_test "list" \
-    "v0 = 0;" \
+    "{ /\\* main prologue \\*/" \
     "list before run"
 
 gdb_load ${binfile}
index cff16e583de808fa327b4ab86386ef38634064e1..db11702a5b059dcc766c1d5c19e46f7ba79a0ace 100644 (file)
@@ -37,6 +37,6 @@ static int __attribute__ ((used)) ambiguous_var;
 
 int
 main (void)
-{
+{ /* main prologue */
   return 0;
 }
diff --git a/gdb/testsuite/gdb.base/list-before-start.exp b/gdb/testsuite/gdb.base/list-before-start.exp
new file mode 100644 (file)
index 0000000..5499257
--- /dev/null
@@ -0,0 +1,36 @@
+# Copyright 2024 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 <http://www.gnu.org/licenses/>.
+
+# Test the "list" command to print the location around main before the
+# program is started.
+
+standard_testfile list-ambiguous0.c
+
+if {[prepare_for_testing "failed to prepare" $testfile $srcfile debug]} {
+    return -1
+}
+
+set fill "${decimal}\\s+\[^\n\r\]+"
+
+gdb_test_no_output "set listsize 10"
+
+gdb_test "list" \
+    [multi_line \
+       "${decimal}\\s+" \
+       "${decimal}\\s+int" \
+       "${decimal}\\s+main\[^\n\r\]+" \
+       "${decimal}\\s+\\{ /\\* main prologue \\*/" \
+       "${fill}" \
+       "${fill}" ]
index 8e404f9c9bd38e8fb5218cea5b25cf0c3bb848d9..00d28886252dab340dd1d4c63d1555d1a79eae6b 100644 (file)
@@ -42,16 +42,10 @@ proc test_file_list_exec_source_file {} {
     }
 
     # get the path and absolute path to the current executable
-    #
-    # In gdb 6.2 (at least), the default line number is set by
-    # select_source_symtab to the first line of "main" minus
-    # the value of "lines_to_list" (which defaults to 10) plus one.
-    # --chastain 2004-08-13
 
     set line_main_head [gdb_get_line_number "main ("]
-    set line_main_body [expr $line_main_head + 2]
-    set gdb_lines_to_list 10
-    set line_default [expr $line_main_body - $gdb_lines_to_list + 1]
+    set line_main_prologue [expr $line_main_head + 1]
+    set line_default $line_main_prologue
 
     mi_gdb_test "111-file-list-exec-source-file" \
            "111\\\^done,line=\"$line_default\",file=\"${srcfilepath}\",fullname=\"$fullname_syntax${srcfile}\",macro-info=\"0\"" \