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

[lldb] Print command reports incorrect struct field value

Open OCHyams opened this issue 7 years ago • 4 comments

Bugzilla Link 43126
Version unspecified
OS Windows NT
Attachments bug reproducer
CC @dwblaikie,@JDevlieghere,@jmorse,@jimingham,@OCHyams

Extended Description

Summary: lldb is reporting a local struct field as '0' when the correct value (200) has not been described by any DWARF after compiling the attachment 'bug.cpp' at O2.

This bug was found using dexter0. llvm (lldb and clang) version this bug was reported with*: git 91824678868 svn 369769

*not necessarily when it was introduced.

With the attached test case, variable basket.bananas is incorrectly reported as '0' between lines [22, 25] when compiled at O2:

$ clang -g -O2 bug.cpp
$ lldb a.out

(lldb) target create "a.out"
Current executable set to 'a.out' (x86_64).
(lldb) b foo
Breakpoint 1: where = a.out`foo(Box const&) + 1 at bug.cpp:18:19, address = 0x0000000000400491
(lldb) b 22
Breakpoint 2: where = a.out`foo(Box const&) + 16 at bug.cpp:22:3, address = 0x00000000004004a0
(lldb) b 25
Breakpoint 3: where = a.out`foo(Box const&) + 26 at bug.cpp:25:3, address = 0x00000000004004aa
(lldb) process launch
Process 22427 launched: 'a.out' (x86_64)
Process 22427 stopped
* thread #​1, name = 'a.out', stop reason = breakpoint 1.1
    frame #​0: 0x0000000000400491 a.out`foo(box=0x00007fffffffe038) at bug.cpp:18:19
   15  	__attribute__((noinline))
   16  	void foo(const Box& box)
   17  	{
-> 18  	  Basket basket = box.baskets[2];
   19  	  do_something(basket.apples); // DexLabel('a')
   20  	
   21  	  basket.apples = 55;
(lldb) p box
(const Box) $0 = {
  baskets = {
    [0] = (apples = 1, bananas = 2)
    [1] = (apples = 10, bananas = 20)
    [2] = (apples = 100, bananas = 200)
  }
}
(lldb) c
Process 22427 resuming
Process 22427 stopped
* thread #​1, name = 'a.out', stop reason = breakpoint 2.1
    frame #&#8203;0: 0x00000000004004a0 a.out`foo(box=<unavailable>) at bug.cpp:22:3
   19  	  do_something(basket.apples); // DexLabel('a')
   20  	
   21  	  basket.apples = 55;
-> 22  	  do_something(basket.apples); // DexLabel('b')
   23  	
   24  	  basket.apples = basket.bananas;
   25  	  do_something(basket.apples); // DexLabel('c')
(lldb) p basket
(Basket) $1 = (apples = 55, bananas = 0)
(lldb) c
Process 22427 resuming
Process 22427 stopped
* thread #&#8203;1, name = 'a.out', stop reason = breakpoint 3.1
    frame #&#8203;0: 0x00000000004004aa a.out`foo(box=<unavailable>) at bug.cpp:25:3
   22  	  do_something(basket.apples); // DexLabel('b')
   23  	
   24  	  basket.apples = basket.bananas;
-> 25  	  do_something(basket.apples); // DexLabel('c')
   26  	}
   27  	
   28  	
(lldb) p basket
(Basket) $2 = (apples = 55, bananas = 0)

The DWARF doesn't describe basket.bananas at all. The only reference to basket is as follows:

$ llvm-dwarfdump --name=basket a.out
a.out:	file format ELF64-x86-64

0x00000087: DW_TAG_variable
              DW_AT_location	(0x00000023
                 [0x00000000004004a0,  0x00000000004004af): DW_OP_constu 0x37, DW_OP_stack_value, DW_OP_piece 0x2)
              DW_AT_name	("basket")
              DW_AT_decl_file	("bug.cpp")
              DW_AT_decl_line	(18)
              DW_AT_type	(0x000000ff "Basket")

The actual value of basket.bananas is 200 (as shown in the lldb session above with command 'p box').

Given that the dwarf doesn't describe the basket.bananas at all I would expect basket.bananas to be shown as 'optimized away' or something similar. However it is handled it should not be showing '0'.

gdb says that basket.bananas is a 'synthetic pointer'. I'm not sure exactly what this means or if it is the correct message for this scenario, but I thought it was worth mentioning.

OCHyams avatar Aug 27 '19 16:08 OCHyams

DWARF wouldn't mention the fields of a structure in the DW_TAG_variable for a variable of that type. Instead, the location expression for the variable would describe the layout of the different pieces of the variable. In this case, the location expression says that only the first two bytes (presumably the first field) of the structure are available, and their value is 0x37 - i.e. 55.

So the DWARF is right, and your compiler chose only to copy over the first field when you did:

-> 18 Basket basket = box.baskets[2];

because the second one wasn't used. So it doesn't actually have a correct value. LLDB should report fields of a structure as "optimized out" to handle this case. But it doesn't do that at present.

jimingham avatar Aug 27 '19 17:08 jimingham

Hi - I just wanted to highlight that as mentioned in this RFC this bug causes issues when used with the assignment tracking compiler feature. We're hoping to turn it on soon, so for now I'm proposing that assignment tracking is disabled when LLDB debugger tuning is requested (see the more recent RFC and some additional discussion in https://reviews.llvm.org/D146615).

Does that sound reasonable and/or do you have any thoughts on how we can move forward?

Thanks, Orlando

OCHyams avatar Mar 28 '23 13:03 OCHyams

While on something unrelated, I was trying to use AssignmentTrackingPass on macOS, when I realized that is not enabled when under LLDB. Is there any follow up on this by any chance? With latest clang, options -g -gfull -O2 -Xclang -fexperimental-assignment-tracking=forced did not show up any issues to me on this. I think we might enable it (regardless of the bug here), and revert should it lead to issues. cc/ @OCHyams @JDevlieghere

antoniofrighetto avatar Sep 11 '23 14:09 antoniofrighetto

Hi @antoniofrighetto, sorry for the delay. I agree with you, I think it would be good to remove the restriction. This bug isn't show-stopping and we won't know if there are more without turning it on. I've got reduce time to work on toolchain things at the moment; is this something you'd be able to do? No worries if not, I can put it on my list.

We should wait a bit before committing the patch either way to see if @JDevlieghere has any objections.

OCHyams avatar Sep 21 '23 09:09 OCHyams