llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[lldb] Inaccurate mapping between program location and memory address

Open iamanonymouscs opened this issue 4 years ago • 4 comments

$ clang --version

Ubuntu clang version 15.0.0-++20220520091548+a61835b1e3f5-1~exp1~20220520091641.498
Target: x86_64-pc-linux-gnu
Thread model: posix
InstalledDir: /usr/bin

$ cat small.c

int v;

void foo (char *p, char *q)
{
  v = p > q;
}

int main ()
{
  char *heap1 = (char *)__builtin_malloc(42);
  char *heap2 = (char *)__builtin_malloc(42);

  foo (heap1, heap2);
  __builtin_free(heap1); __builtin_free(heap2);

  return 0;
}

$ clang -g -O1 small.c; lldb a.out

(lldb) target create "a.out"
Current executable set to '/root/a.out' (x86_64).
(lldb) b 11
Breakpoint 1: where = a.out`main + 17 at small.c:11:25, address = 0x00000000000006b1
(lldb) r
Process 7693 launched: '/root/a.out' (x86_64)
Process 7693 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
    frame #0: 0x00005555555546b1 a.out`main at small.c:11:25
   8   	int main ()
   9   	{
   10  	  char *heap1 = (char *)__builtin_malloc(42);
-> 11  	  char *heap2 = (char *)__builtin_malloc(42);
   12  	
   13  	  foo (heap1, heap2);
   14  	  __builtin_free(heap1); __builtin_free(heap2);
(lldb) s
Process 7693 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x00005555555546be a.out`main at small.c:13:3
   10  	  char *heap1 = (char *)__builtin_malloc(42);
   11  	  char *heap2 = (char *)__builtin_malloc(42);
   12  	
-> 13  	  foo (heap1, heap2);
   14  	  __builtin_free(heap1); __builtin_free(heap2);
   15  	
   16  	  return 0;
(lldb) s
Process 7693 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x00005555555546be a.out`main [inlined] foo(p="", q="") at small.c:5:9
   2   	
   3   	void foo (char *p, char *q)
   4   	{
-> 5   	  v = p > q;
   6   	}
   7   	
   8   	int main ()
(lldb)

We can find that:

  1. small.c:5:9 and small.c:13:3 both mapped to the address 0x00005555555546be.

$ clang -g -O1 small.c; lldb a.out

(lldb) target create "a.out"
Current executable set to '/root/a.out' (x86_64).
(lldb) b 11
Breakpoint 1: where = a.out`main + 17 at small.c:11:25, address = 0x00000000000006b1
(lldb) run
Process 7750 launched: '/root/a.out' (x86_64)
Process 7750 stopped
* thread #1, name = 'a.out', stop reason = breakpoint 1.1
    frame #0: 0x00005555555546b1 a.out`main at small.c:11:25
   8   	int main ()
   9   	{
   10  	  char *heap1 = (char *)__builtin_malloc(42);
-> 11  	  char *heap2 = (char *)__builtin_malloc(42);
   12  	
   13  	  foo (heap1, heap2);
   14  	  __builtin_free(heap1); __builtin_free(heap2);
(lldb) s
Process 7750 stopped
* thread #1, name = 'a.out', stop reason = step in
    frame #0: 0x00005555555546be a.out`main at small.c:13:3
   10  	  char *heap1 = (char *)__builtin_malloc(42);
   11  	  char *heap2 = (char *)__builtin_malloc(42);
   12  	
-> 13  	  foo (heap1, heap2);
   14  	  __builtin_free(heap1); __builtin_free(heap2);
   15  	
   16  	  return 0;
(lldb) si
Process 7750 stopped
* thread #1, name = 'a.out', stop reason = instruction step into
    frame #0: 0x00005555555546c0 a.out`main [inlined] foo(p="", q="") at small.c:5:9
   2   	
   3   	void foo (char *p, char *q)
   4   	{
-> 5   	  v = p > q;
   6   	}
   7   	
   8   	int main ()
(lldb) 

At this time, we can find that:

  1. small.c:13:3 is mapped to 0x00005555555546be.
  2. small.c:5:9 is mapped to the address 0x00005555555546c0.

iamanonymouscs avatar May 24 '22 05:05 iamanonymouscs

@llvm/issue-subscribers-lldb

llvmbot avatar May 24 '22 13:05 llvmbot

From the lldb output, it turns out that main inlined foo. In your first try, the last command you use is s which stands for step. After executing this command, lldb steps into function foo. Because main inlined foo, stepping into foo doesn't change the address. One thing to note is that step doesn't necessary change code address/actually execute instructions.

In you second try, the last command you use is si which always does an instruction level single step. That's why you see different address after si.

Also see https://github.com/llvm/llvm-project/issues/54448#issuecomment-1072898439 where Jim gives detailed explanation for this.

ZequanWu avatar May 24 '22 20:05 ZequanWu

Thanks, Zequan. I have read Jim's explanation on this.

I was wondering whether we can do better for this. At instruction level stepping, line 13 and line 5 in this program map to different addresses. While at source level stepping, it is a little confusing that these two different source lines becomes mapping to the same address.

If the source line does not have an address/instructions, it's ok that lldb step to line 5 and does not change code address/actually execute instructions. But from instruction level stepping, we observe that line 5 indeed has a different address. Thus, I was wondering that lldb could do better as it has the basis for giving more accurate mappings.

iamanonymouscs avatar May 25 '22 01:05 iamanonymouscs

When doing line table analysis, be sure to look not only at the line table but the DW_AT_inlined_subroutine DIEs in the debug_info. The information in the line table is incomplete, in particular it can't represent overlapping lines at the same address since it's required to be strictly increasing in address.

What you found by instruction level stepping is that line 5 in the line table starts at the next instruction, but if you look at the inlined_subroutine info it will say that the inlining starts where lldb says it does, an instruction before your stepi.

The debug_info records say that the same address belongs to the line where the function was inlined, and the first line of the inlined function. In that case, the actual instruction at that address belongs to the deepest inlined function in the inlined function stack there. So if, on source step, we ran that instruction as part of the step, you wouldn't be able to look at the inlined function's state before executing its first instruction, which would be wrong.

Seems to me that step is doing the right thing.

The only thing that seems odd here is that step-instruction doesn't work the same way that step does. But expecting that is not reasonable. source stepping is there to produce the illusion of moving through your code from line to line, following the flow of the original source to the greatest extent possible. step-instruction is there so that regardless of what the debugger thinks it knows about source code, frames, etc, users always have a way to move the process forward one instruction. So there's no way it will be 100% compatible with the results of source line stepping.

But I don't think that's a problem, it's just that source stepping and step-instruction have different jobs.

Jim

On May 24, 2022, at 6:20 PM, yangyibiao @.***> wrote:

Thanks, Zequan. I have read Jim's explanation on this. 

I was wondering whether we can do better for this.  At instruction level stepping, line 13 and line 5 in this program mapping to different addresses. While at source level stepping, it is a little confusing that these two different source lines becomes mapping to the same address. 

If the source line does not have an address/instructions, it's ok that ``doesn't necessary change code address/actually execute instructions. But from instruction level stepping, we observe that line 5 indeed has a different address. Thus, I think lldb has a basis for giving more accurate mappings.      ------------------ Original ------------------ From: @.>; Date:  Wed, May 25, 2022 04:18 AM To: @.>; Cc: @.>; @.>; Subject:  Re: [llvm/llvm-project] [lldb] Inaccurate mapping between program location and memory address (Issue #55670)

 

From the lldb output, it turns out that main inlined foo. In your first try, the last command you use is s which stands for step. After executing this command, lldb steps into function foo. Because main inlined foo, stepping into foo doesn't change the address. One thing to note is that step doesn't necessary change code address/actually execute instructions.

In you second try, the last command you use is si which always does an instruction level single step. That's why you see different address after si.

Also see #54448 (comment) where Jim gives detailed explanation for this.

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***> — Reply to this email directly, view it on GitHub https://github.com/llvm/llvm-project/issues/55670#issuecomment-1136599183, or unsubscribe https://github.com/notifications/unsubscribe-auth/ADUPVWZWYXPYJWWPLF2MCJTVLV563ANCNFSM5WYCUZDA. You are receiving this because you are on a team that was mentioned.

jimingham avatar May 25 '22 01:05 jimingham