ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Arguments print incorrectly in gdb

Open yazd opened this issue 3 years ago • 0 comments

Test case:

auto fun(ushort a1, uint a2) {
  return a1 + a2;
}

int main() {
  ushort s = 0xC00D;
  uint i = 0xDEAD_BEAF;
  fun(s, i);
  return 0;
}

Compile using ldc2 -g simple.d Run gdb and add a breakpoint at fun. Gdb prints Breakpoint 1, simple.fun(ushort, uint) (a1=0, a2=1432094960) at simple.d:1 As you can see, the values for a1 and a2 are wrong.

I checked the debug info and they are identical to a similar C program. The only difference is that the breakpoint is set at a different point.

In the D program:

000000000001a7b0 <_D6simple3funFNaNbNiNftkZk>:
   1a7b0:	55                   	push   %rbp
   1a7b1:	48 89 e5             	mov    %rsp,%rbp
   1a7b4:	66 89 f8             	mov    %di,%ax
   1a7b7:	66 89 45 fe          	mov    %ax,-0x2(%rbp) # BREAKPOINT IS SET HERE
   1a7bb:	89 75 f8             	mov    %esi,-0x8(%rbp)
   1a7be:	0f b7 45 fe          	movzwl -0x2(%rbp),%eax
   1a7c2:	03 45 f8             	add    -0x8(%rbp),%eax
   1a7c5:	5d                   	pop    %rbp
   1a7c6:	c3                   	ret    
   1a7c7:	66 0f 1f 84 00 00 00 	nopw   0x0(%rax,%rax,1)
   1a7ce:	00 00 

In the C program:

0000000000401110 <fun>:
  401110:	55                   	push   %rbp
  401111:	48 89 e5             	mov    %rsp,%rbp
  401114:	66 89 f8             	mov    %di,%ax
  401117:	66 89 45 fe          	mov    %ax,-0x2(%rbp)
  40111b:	89 75 f8             	mov    %esi,-0x8(%rbp)
  40111e:	0f b7 45 fe          	movzwl -0x2(%rbp),%eax  # BREAKPOINT IS SET HERE
  401122:	03 45 f8             	add    -0x8(%rbp),%eax
  401125:	5d                   	pop    %rbp
  401126:	c3                   	ret    
  401127:	66 0f 1f 84 00 00 00 	nopw   0x0(%rax,%rax,1)
  40112e:	00 00 

If, in the D program, I use si in gdb to step into the movzwl instruction, and then use info args, the expected output is printed.

Tested using ldc 1.29.0 and older releases on Linux x86-64.

Edited to show assembly from ldc 1.29 where the calling convention is similar to C.

yazd avatar Apr 15 '22 13:04 yazd