Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does gdb tell me a pointer is 4 bytes on x86-64?

Tags:

linux

gdb

Seen with gdb on openSUSE, CentOS, Fedora, and Ubuntu:

This gdb was configured as "x86_64-unknown-linux-gnu".

(gdb) p sizeof(void *)

$1 = 4

(gdb) p sizeof(long)

$2 = 4

Why is gdb giving me the wrong answers on all of my 64-bit systems?

like image 588
federal Avatar asked Jul 12 '10 21:07

federal


1 Answers

It seems like gdb chooses some surprising defaults when you're not debugging any particular piece of code. If you load up a 64-bit executable as in: gdb /bin/sh you get a less-surprising result:

(gdb) p sizeof(void *)
$1 = 8

You can also specifically tell gdb what to do:

(gdb) show architecture
The target architecture is set automatically (currently i386)
(gdb) p sizeof(void *)
$1 = 4
(gdb) set architecture
Requires an argument. Valid arguments are i386, i386:x86-64, i8086, i386:intel, i386:x86-64:intel, auto.
(gdb) set architecture i386:x86-64
The target architecture is assumed to be i386:x86-64
(gdb) p sizeof(void *)
$2 = 8
like image 138
hobbs Avatar answered Nov 18 '22 12:11

hobbs