6

I need to write some gdb macros that need to different between 32 and 64 bit architectures. I'm looking for a way to determine in gdb whether the debugged executable is 32 or 64 bit.

info target includes info about file type

e.g. file type elf32-i386

but this is embedded in a longer output.

Being new to gdb macros, I don't know how to process that output, or find another way to get this.

Please, no python gdb for the time being.

dbbd
  • 864
  • 1
  • 8
  • 23

4 Answers4

4

Python API example

frame = gdb.selected_frame()
arch = frame.architecture()
print(arch.name())

Sample outputs:

  • i386 for a 32 bit ELF
  • i386:x86-64 for a 64 bit ELF

Docs: https://sourceware.org/gdb/onlinedocs/gdb/Architectures-In-Python.html

Tested on GDB 7.7.1, Ubuntu 14.04 AMD64.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
3

Here is your solution, not 'contaminated' with python:

define set-program-arch
    set logging file tmp.gdb
    set logging overwrite on
    set logging redirect on
    set logging on
    set pagination off
    info target
    set pagination on
    set logging off
    set logging redirect off
    set logging overwrite off
    shell echo -n 'set $program_arch="' > tmp2.gdb
    shell grep 'file type' tmp.gdb | sed "s/\.$//g" | cut -d ' ' -f 4 | tr -d '\n' >> tmp2.gdb
    shell echo '"' >> tmp2.gdb
    source tmp2.gdb
    shell rm -f tmp2.tmp tmp.gdb
end

This sets variable program_arch to ELF type of the binary being debugged (e.g. elf64-x86-64). Enjoy!

sirgeorge
  • 6,331
  • 1
  • 28
  • 33
  • Very nice, thanks. It also answers the general question of how to process gdb output. – dbbd Sep 17 '12 at 12:28
1

Actually, I found an extremely simple answer:

if sizeof(uintptr_t) == 4
  set $arch = 32
else 
  set $arch = 64
end
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
dbbd
  • 864
  • 1
  • 8
  • 23
  • 1
    You've found the answer to a different question from the one you asked. Good luck using this method distinguishing between elf32-i386 and elf32-ppc. – Employed Russian Sep 13 '12 at 22:51
  • 1
    Isn't `unitptr_t` a typo? Shouldn't that be `uint` not `unit`? Also, why do you use `sizeof(uintptr_t)` instead of `sizeof(void *)` or even `sizeof(unsigned int *)`? The latter might work better for C, C++, and Objective-C, not just C++11 and C99 with `` included. – mormegil Aug 20 '13 at 22:50
0

Please, no python gdb for the time being.

I don't believe you can achieve what you want without python-gdb, and it is trivial to achieve what you want with it. So consider relaxing your restriction.

Employed Russian
  • 199,314
  • 34
  • 295
  • 362