Search code examples
debuggingassemblygdbx86-64apple-silicon

Debug and lookup emulated x86_64 registers on apple silicon?


I currently use a M1 MacBook Air. I want to debug C code, but with x86_64 assembly syntax and registers, on my local computer.

I tried installing gdb, but it was complicated to setup on my machine. I also tried UTM emulation, qemu, they returned error about some non privileged actions related to gdb or debugging itself.

I know there are online free solutions (https://www.onlinegdb.com/). But there is satisfaction doing it on local machine. After a few days of reading docs and tinkering I found an acceptable solution using lldb. Also, onlinegdb is Linux, not macOS. And only GAS, not other assemblers like NASM.

See below for the Solution.


Solution

  • Create a File.

    ❯ vim main.c
    

    Compile it to binary (-arch x86_64). See (How can I create a x86_64 assembly file from C source code in apple silicon?).

    ❯ clang -g -O0 -Wall -arch x86_64 -o main main.c
    

    [ Optional One Time Setup ]: Use this command to view debug assembly code with intel syntax. (https://stackoverflow.com/a/21502943/14668175)

    ❯ echo "settings set target.x86-disassembly-flavor intel" >> ~/.lldbinit
    

    Use lldb to debug. lldb commands (https://lldb.llvm.org/use/map.html).

    ❯ lldb -arch x86_64 main
    

    You are all set.