This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Limit objdump -S context lines


There have been a couple of complaints about objdump -S recently, one
being that sometimes multiple copies of source lines are printed.  So
I took a look and it seems to me that we ought to do something about
SHOW_PRECEDING_CONTEXT_LINES.  Presumably this is to show function
args when dumping out the start of a function.  However, showing
context lines is confusing in many cases, an obvious example being
loops.

For instance:
int my_fun (int *arr, int N)
{
  int sum = 0;
  for (int i = 0; i < N; ++i)
    sum += arr[i];
  return sum;
}

gcc -c -O2 -g on x86-64 gives an object file displayed by objdump -dS
0000000000000000 <my_fun>:
int my_fun (int *arr, int N)
{
  int sum = 0;
  for (int i = 0; i < N; ++i)
   0:	85 f6                	test   %esi,%esi
   2:	7e 19                	jle    1d <my_fun+0x1d>
   4:	8d 46 ff             	lea    -0x1(%rsi),%eax
   7:	48 8d 54 87 04       	lea    0x4(%rdi,%rax,4),%rdx
   c:	31 c0                	xor    %eax,%eax
   e:	66 90                	xchg   %ax,%ax
    sum += arr[i];
  10:	03 07                	add    (%rdi),%eax
  12:	48 83 c7 04          	add    $0x4,%rdi
int my_fun (int *arr, int N)
{
  int sum = 0;
  for (int i = 0; i < N; ++i)
  16:	48 39 d7             	cmp    %rdx,%rdi
  19:	75 f5                	jne    10 <my_fun+0x10>
  1b:	f3 c3                	repz retq 
int my_fun (int *arr, int N)
{
  int sum = 0;
  1d:	31 c0                	xor    %eax,%eax
  for (int i = 0; i < N; ++i)
    sum += arr[i];
  return sum;
}
  1f:	c3                   	retq   

Improved with this patch to
0000000000000000 <my_fun>:
int my_fun (int *arr, int N)
{
  int sum = 0;
  for (int i = 0; i < N; ++i)
   0:	85 f6                	test   %esi,%esi
   2:	7e 19                	jle    1d <my_fun+0x1d>
   4:	8d 46 ff             	lea    -0x1(%rsi),%eax
   7:	48 8d 54 87 04       	lea    0x4(%rdi,%rax,4),%rdx
   c:	31 c0                	xor    %eax,%eax
   e:	66 90                	xchg   %ax,%ax
    sum += arr[i];
  10:	03 07                	add    (%rdi),%eax
  12:	48 83 c7 04          	add    $0x4,%rdi
  for (int i = 0; i < N; ++i)
  16:	48 39 d7             	cmp    %rdx,%rdi
  19:	75 f5                	jne    10 <my_fun+0x10>
  1b:	f3 c3                	repz retq 
  int sum = 0;
  1d:	31 c0                	xor    %eax,%eax
  return sum;
}
  1f:	c3                   	retq   


	* objdump.c (struct print_file_list): Add "max_printed".
	(try_print_file_open): Init new field.
	(show_line): Don't show 5 context lines when redisplaying source.

diff --git a/binutils/objdump.c b/binutils/objdump.c
index 5b84801..174596e 100644
--- a/binutils/objdump.c
+++ b/binutils/objdump.c
@@ -1135,6 +1135,7 @@ struct print_file_list
   const char **linemap;
   unsigned maxline;
   unsigned last_line;
+  unsigned max_printed;
   int first;
 };
 
@@ -1260,6 +1261,7 @@ try_print_file_open (const char *origname, const char *modname)
 
   p->linemap = index_file (p->map, p->mapsize, &p->maxline);
   p->last_line = 0;
+  p->max_printed = 0;
   p->filename = origname;
   p->modname = modname;
   p->next = print_files;
@@ -1447,10 +1449,17 @@ show_line (bfd *abfd, asection *section, bfd_vma addr_offset)
 	      l = linenumber - SHOW_PRECEDING_CONTEXT_LINES;
 	      if (l >= linenumber)
 		l = 1;
-	      if (p->last_line >= l && p->last_line <= linenumber)
-		l = p->last_line + 1;
+	      if (p->max_printed >= l)
+		{
+		  if (p->max_printed < linenumber)
+		    l = p->max_printed + 1;
+		  else
+		    l = linenumber;
+		}
 	    }
 	  dump_lines (p, l, linenumber);
+	  if (p->max_printed < linenumber)
+	    p->max_printed = linenumber;
 	  p->last_line = linenumber;
 	  p->first = 0;
 	}

-- 
Alan Modra
Australia Development Lab, IBM


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]