cgdbcpu-architecturegdb-python

How to implement a GDB function depending on the architecture


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?


Solution

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

    something like this:

    f = gdb.selected_frame()
    a = f.architecture()
    print(a.name())