Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

macos

gdb

arm64

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?

like image 447
scribe Avatar asked Dec 29 '25 06:12

scribe


2 Answers

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.

like image 54
Surge Avatar answered Dec 30 '25 22:12

Surge


I didn't use GDB on macOS before. But I guess the issue you have is that some security technologies on macOS are different than MS Windows. You probably need some extra config for code signing and system integrity protection(SIP) so that GDB can debug your C program. To be honest, it might be complex and I 'm not sure how to set those up.

I reckon there's a more macOS-adopted alternative :

You can try LLDB if you don't need to stick with GDB. LLDB is generally more integrated with macOS, including the ARM64 architecture. It's also the default debugger in Xcode.

There's also tutorials of how to set up and use LLDB

Cheers

like image 30
Jeremiah Avatar answered Dec 30 '25 21:12

Jeremiah



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!