Search code examples
cmacosgdbarm64

How to run GDB on an M2 Macbook (Air)?


Okay so I installed GDB as,

brew install aarch64-elf-gdb

Which installed aarch64-elf-gdb. Then I made an alias for aarch64-elf-gdb as alias gdb="aarch64-elf-gdb" for convenience.

Why not just brew install gdb? Because, I did and it said,

gdb: The x86_64 architecture is required for this software.

I compile the following file test.c,

int main(int argc, char *argv[]) {
  return 0;
}

With the command gcc -g -o test test.c and run the debugger via gdb ./test. This results in,

[snip]./test": not in executable format: file format not recognized

Here is a screenshot of the whole thing happening.

Is this a configuration issue, maybe I am missing a flag? Or, is it so that it is just not possible to run GDB on M2 machines?


Solution

  • That's because gdb is not yet out for Apple silicon (ARM architecture). In fact, neither is gcc. If you run gcc --version, you will see that in fact your mac is aliasing gcc to clang (Apple clang).

    What brew is telling you is that you cannot use gdb for natively compiled code. What you are using, that is aarch64-elf-gdb, is a debugger for cross-platform development. In other words, Apple mac users developing for Linux, writing elf binaries. Obviously, you won't be able to use those binaries on your mac.

    Addendum: The other answer is correct about one thing. Your best bet now for native binaries is LLDB. But you will have to learn slightly different syntax.