1

I'm writing a GDB script in python to print some debug information of the application. The thing is multiple architectures are supported: x86, alpha, aarch64, and probably some more later. The function printing the debug information varies from the architecture.

So in fact I've got the following functions:

def print_info_x86():
   #...


def print_info_aarch64():
   #...


def print_info_alpha():
   #...

And I want to achieve something like the following:

def print_info():
   if arch == 'x86':
      print_info_x86()
   #etc..

Is there a way to do that? There is a GDB command show architecture and it's possible to extract it from objdump -a, but is there a simpler way to understand what architecture the binary was compiled for in GDB?

Some Name
  • 8,555
  • 5
  • 27
  • 77

1 Answers1

2

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

something like this:

f = gdb.selected_frame()
a = f.architecture()
print(a.name())
0___________
  • 60,014
  • 4
  • 34
  • 74
  • 1
    You probably meant `frame = gdb.selected_frame()`, not `f = gdb.selected_frame()` – Some Name Aug 28 '22 at 18:26
  • @SomeName copy-paste copied the invokation form the documentation – 0___________ Aug 28 '22 at 18:42
  • 4
    I would probably use [gdb.selected_inferior().architecture().name()](https://sourceware.org/gdb/onlinedocs/gdb/Inferiors-In-Python.html#index-Inferior_002earchitecture) instead, so I don't need a running process. – ssbssa Aug 28 '22 at 19:09