From f52625f1f29a87e94aa9d40e1b76be2d0ecedc10 Mon Sep 17 00:00:00 2001 From: Bruno Larsen Date: Thu, 15 Jun 2023 11:17:07 +0200 Subject: [PATCH] gdb/cli: Improve UX when using list with no args When using "list" with no arguments, GDB will first print the lines around where the inferior is stopped, then print the next N lines until reaching the end of file, at which point it warns the user "Line X out of range, file Y only has X-1 lines.". This is usually desirable, but if the user can no longer see the original line, they may have forgotten the current line or that a list command was used at all, making GDB's error message look cryptic. It was reported in bugzilla as PR cli/30497. This commit improves the user experience by changing the behavior of "list" slightly when a user passes no arguments. It now prints that the end of the file has been reached and recommends that the user use the command "list ." instead. Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=30497 Reviewed-By: Eli Zaretskii Approved-By: Tom Tromey --- gdb/NEWS | 5 +++++ gdb/cli/cli-cmds.c | 17 +++++++++++++---- gdb/doc/gdb.texinfo | 4 +++- gdb/source.c | 16 ++++++++++++++++ gdb/source.h | 7 +++++++ gdb/testsuite/gdb.base/list.exp | 8 ++++---- 6 files changed, 48 insertions(+), 9 deletions(-) diff --git a/gdb/NEWS b/gdb/NEWS index c5bbd4f1348..ac5dc424d3f 100644 --- a/gdb/NEWS +++ b/gdb/NEWS @@ -90,6 +90,11 @@ print the location where the inferior is stopped. If the inferior hasn't started yet, the command will print around the main function. +* Using the 'list' command with no arguments in a situation where the + command would attempt to list past the end of the file now warns the + user that the end of file has been reached, refers the user to the + newly added '.' argument + * New commands maintenance print record-instruction [ N ] diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c index 44db019c59f..3a1902a0c82 100644 --- a/gdb/cli/cli-cmds.c +++ b/gdb/cli/cli-cmds.c @@ -1246,10 +1246,19 @@ list_command (const char *arg, int from_tty) list_around_line (arg, cursal); } - /* "l" or "l +" lists next ten lines. */ - else if (arg == NULL || arg[0] == '+') - print_source_lines (cursal.symtab, - source_lines_range (cursal.line), 0); + /* "l" and "l +" lists the next few lines, unless we're listing past + the end of the file. */ + else if (arg == nullptr || arg[0] == '+') + { + if (last_symtab_line (cursal.symtab) >= cursal.line) + print_source_lines (cursal.symtab, + source_lines_range (cursal.line), 0); + else + { + error (_("End of the file was already reached, use \"list .\" to" + " list the current location again")); + } + } /* "l -" lists previous ten lines, the ones before the ten just listed. */ diff --git a/gdb/doc/gdb.texinfo b/gdb/doc/gdb.texinfo index 7619efe3de9..20c9b24400d 100644 --- a/gdb/doc/gdb.texinfo +++ b/gdb/doc/gdb.texinfo @@ -9144,7 +9144,9 @@ Print more lines. If the last lines printed were printed with a @code{list} command, this prints lines following the last lines printed; however, if the last line printed was a solitary line printed as part of displaying a stack frame (@pxref{Stack, ,Examining the -Stack}), this prints lines centered around that line. +Stack}), this prints lines centered around that line. If no +@code{list} command has been used and no solitary line was printed, +it prints the lines around the function @code{main}. @item list - Print lines just before the lines last printed. diff --git a/gdb/source.c b/gdb/source.c index 9997cccb31b..08adc6671b7 100644 --- a/gdb/source.c +++ b/gdb/source.c @@ -1484,6 +1484,22 @@ print_source_lines (struct symtab *s, source_lines_range line_range, line_range.stopline (), flags); } +/* See source.h. */ + +int +last_symtab_line (struct symtab *s) +{ + const std::vector *offsets; + + /* Try to get the offsets for the start of each line. */ + if (!g_source_cache.get_line_charpos (s, &offsets)) + return false; + if (offsets == nullptr) + return false; + + return offsets->size (); +} + /* Print info on range of pc's in a specified line. */ diff --git a/gdb/source.h b/gdb/source.h index 8fbc365680d..be80e003890 100644 --- a/gdb/source.h +++ b/gdb/source.h @@ -192,6 +192,13 @@ private: int m_stopline; }; +/* Get the number of the last line in the given symtab. */ +extern int last_symtab_line (struct symtab *s); + +/* Check if the line LINE can be found in the symtab S, so that it can be + printed. */ +extern bool can_print_line (struct symtab *s, int line); + /* Variation of previous print_source_lines that takes a range instead of a start and end line number. */ extern void print_source_lines (struct symtab *s, source_lines_range r, diff --git a/gdb/testsuite/gdb.base/list.exp b/gdb/testsuite/gdb.base/list.exp index ed178a1dd95..582355996b0 100644 --- a/gdb/testsuite/gdb.base/list.exp +++ b/gdb/testsuite/gdb.base/list.exp @@ -175,8 +175,8 @@ proc_with_prefix test_list_forward {} { "list 25-34" gdb_test "list" "35\[ \t\]+foo \\(.*\\);.*${last_line_re}" \ "list 35-42" - gdb_test "list" "Line number 44 out of range; \[^\r\n\]+ has 43 lines\." \ - "end of file error after \"list\" command" + gdb_test "list" "End of the file was already reached, use \"list .\" to list the current location again" \ + "list past end of file" } # Test that repeating the list linenum command doesn't print the same @@ -194,8 +194,8 @@ proc_with_prefix test_repeat_list_command {} { "list 25-34" gdb_test " " "35\[ \t\]+foo \\(.*\\);.*${last_line_re}" \ "list 35-42" - gdb_test "list" "Line number 44 out of range; \[^\r\n\]+ has 43 lines\." \ - "end of file error after using 'return' to repeat the list command" + gdb_test "list" "End of the file was already reached, use \"list .\" to list the current location again" \ + "list past end of file" } proc_with_prefix test_list_backwards {} { -- 2.30.2