diff mbox

[v1,1/1] list actual code around more than one locations

Message ID 1500437120-3473-1-git-send-email-zhouzhouyi@gmail.com
State New
Headers show

Commit Message

Zhouyi Zhou July 19, 2017, 4:05 a.m. UTC
When debugging following C++ code:
int bar() { return 0;}
int bar(int) { return 0; }

GDB behaves as:
(gdb) list bar
 file: "overload.cc", line number: 1
 file: "overload.cc", line number: 2

However, it would be better for gdb to list the actual code around those
two locations, not just print the location.

Under the mentorship of Pedro Alves, I modify the function list_command
so that GDB behaves more instructive:
(gdb) list bar
file: "overload.cc", line number: 1
1       int bar() { return 0;}
2       int bar(int) { return 0; }
file: "overload.cc", line number: 2
1       int bar() { return 0;}
2       int bar(int) { return 0; }

Tested on x86-64 GNU/Linux.  
Signed-off-by: Zhouyi Zhou <zhouzhouyi@gmail.com>

gdb/ChangeLog:
2017-07-19 Zhouyi Zhou <zhouzhouyi@gmail.com>
        * cli-cmds.c (list_commands): list actual code around more than one
	locations.
---
 gdb/cli/cli-cmds.c | 48 ++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 40 insertions(+), 8 deletions(-)
diff mbox

Patch

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index af750d3..87a89e2 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -980,6 +980,7 @@  list_command (char *arg, int from_tty)
   if (!have_full_symbols () && !have_partial_symbols ())
     error (_("No symbol table is loaded.  Use the \"file\" command."));
 
+  sals.nelts = 0;
   arg1 = arg;
   if (*arg1 == ',')
     dummy_beg = 1;
@@ -996,15 +997,8 @@  list_command (char *arg, int from_tty)
 	  /*  C++  */
 	  return;
 	}
-      if (sals.nelts > 1)
-	{
-	  ambiguous_line_spec (&sals);
-	  xfree (sals.sals);
-	  return;
-	}
 
       sal = sals.sals[0];
-      xfree (sals.sals);
     }
 
   /* Record whether the BEG arg is all digits.  */
@@ -1017,6 +1011,12 @@  list_command (char *arg, int from_tty)
   if (*arg1 == ',')
     {
       no_end = 0;
+      if (sals.nelts > 1)
+        {
+          ambiguous_line_spec (&sals);
+          xfree (sals.sals);
+          return;
+        }
       arg1++;
       while (*arg1 == ' ' || *arg1 == '\t')
 	arg1++;
@@ -1035,11 +1035,17 @@  list_command (char *arg, int from_tty)
 
 	  filter_sals (&sals_end);
 	  if (sals_end.nelts == 0)
-	    return;
+	    {
+	      if (sals.nelts)
+		xfree (sals.sals);
+	      return;
+	    }
 	  if (sals_end.nelts > 1)
 	    {
 	      ambiguous_line_spec (&sals_end);
 	      xfree (sals_end.sals);
+	      if (sals.nelts)
+		xfree (sals.sals);
 	      return;
 	    }
 	  sal_end = sals_end.sals[0];
@@ -1106,13 +1112,37 @@  list_command (char *arg, int from_tty)
   else if (no_end)
     {
       int first_line = sal.line - get_lines_to_list () / 2;
+      int i;
 
       if (first_line < 1) first_line = 1;
+      
+      if (sals.nelts > 1)
+        {
+          printf_filtered (_("file: \"%s\", line number: %d\n"),
+			   symtab_to_filename_for_display (sal.symtab),
+			   sal.line);
+        }
 
       print_source_lines (sal.symtab,
 		          first_line,
 			  first_line + get_lines_to_list (),
 			  0);
+      if (sals.nelts > 1)
+        {
+          for (i = 1; i < sals.nelts; i++)
+            {
+              sal = sals.sals[i];
+              first_line = sal.line - get_lines_to_list () / 2;
+              if (first_line < 1) first_line = 1;
+              printf_filtered (_("file: \"%s\", line number: %d\n"),
+			       symtab_to_filename_for_display (sal.symtab),
+			       sal.line);
+              print_source_lines (sal.symtab,
+                                  first_line,
+                                  first_line + get_lines_to_list (),
+                                  0);
+            }
+        }
     }
   else
     print_source_lines (sal.symtab, sal.line,
@@ -1120,6 +1150,8 @@  list_command (char *arg, int from_tty)
 			 ? sal.line + get_lines_to_list ()
 			 : sal_end.line + 1),
 			0);
+  if (sals.nelts)
+    xfree (sals.sals);
 }
 
 /* Subroutine of disassemble_command to simplify it.