Gdb macro to determine the architecture of a debugged program

I need to write some gdb macros which should be different between 32 and 64 bit architectures. I am looking for a way to determine in gdb whether a debugged executable is 32 or 64 bit.

info target includes file type information

eg. file type elf32-i386

but it is embedded in a longer output.

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

There is no python gdb yet.

+4
source share
4 answers

Here is your solution, not "infected" 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 the program_arch variable for the ELF type of the binary being debugged (for example, elf64-x86-64 ). Enjoy it!

+3
source

Actually, I found out a very simple answer.

 if sizeof(unitptr_t) == 4 set $arch = 32 else set $arch = 64 end 
+1
source

Python API Example

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

Selective Outputs:

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

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

Tested on GDB 7.7.1, Ubuntu 14.04 AMD64.

+1
source

There is no python gdb yet.

I do not believe that you can achieve what you want without python-gdb, and it is trivial to achieve what you want with it. Therefore, consider relaxing your restriction.

-1
source

Source: https://habr.com/ru/post/1433474/


All Articles