Bug 25321 - Subtracting two pointers in value history variables gives wrong result
Summary: Subtracting two pointers in value history variables gives wrong result
Status: RESOLVED INVALID
Alias: None
Product: gdb
Classification: Unclassified
Component: gdb (show other bugs)
Version: HEAD
: P2 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2019-12-29 18:54 UTC by Simon Marchi
Modified: 2019-12-31 17:55 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Simon Marchi 2019-12-29 18:54:00 UTC
I found this while trying to find by how much a global variable gets relocated.  I first print the variable address:

(gdb) p &some_global
$1 = (int *) 0x402c <some_global>

Then start the inferior:

(gdb) start
Temporary breakpoint 1 at 0x111d: file test.c, line 9.
Starting program: /home/simark/src/aoc/08/p2/a.out 

Temporary breakpoint 1, main () at test.c:9
9	  for (i = 0; i < 1000; i++) {

Then print the variable again:
(gdb) p &some_global
$2 = (int *) 0x55555555802c <some_global>

This is the difference between the two addresses:

(gdb) p/x 0x55555555802c - 0x402c
$3 = 0x555555554000

Trying to compute the same using the numbered history variables gives a different result:

(gdb) p/x $2 - $1
$4 = 0x155555555000

I haven't investigated at all.  All I know is that the type of the pointers appear to be the same:

(gdb) p sizeof($1)
$5 = 8
(gdb) p sizeof($2)
$6 = 8
(gdb) p sizeof(*$1)
$7 = 4
(gdb) p sizeof(*$2)
$8 = 4
(gdb) ptype $1
type = int *
(gdb) ptype $2
type = int *
Comment 1 Simon Marchi 2019-12-31 17:55:09 UTC
Bah, nevermind, this is expected pointer arithmetic.  Since these are pointers to integers, subtracting the two pointers gives the number of integers between the two pointers:

(0x55555555802c − 0x402c) / 4 = 0x155555555000