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.

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

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!

1 Comment

Very nice, thanks. It also answers the general question of how to process gdb output.
1

Actually, I found an extremely simple answer:

if sizeof(uintptr_t) == 4
  set $arch = 32
else 
  set $arch = 64
end

2 Comments

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.
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 <stdint.h> included.
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.

1 Comment

Well, how about an example in python?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.