Search code examples
androiddebuggingandroid-ndkadblldb

How do I use lldb to debug C++ code on Android on command line


How can I debug my Android NDK project in C++, using the lldb debugger from the command line?


Solution

  • Probably you can try below: (This example steps are based on macOS)

    run gdb server and attach process

    //Below commands will suspend the execution on the running app, and waits for a debugger to connect to it on port 5045.
    adb shell
    
    // to get pid
    root@generic_x86:/ # ps | grep <your-app-name>
    u0_a54    6510  1196  800157 47442 ffffffff b662df1b S 
    
    <your-app-name>
    
    root@generic_x86:/ # gdbserver :5045 --attach 6510 (PID)
    Attached; pid = 6510
    Listening on port 5045
    //The process is now suspended, and gdbserver is listening for debugging clients on port 5045.
    

    attach gdb debugger

    //open a new terminal, e.g. terminal2, send below commands from this new terminal
    //forward the above port to a local port on the host with the abd forward command
    adb forward tcp:5045 tcp:5045
    //launch gdb client from your android ndk folder
    <your-ndk-home>/android-ndk-r16b/prebuilt/darwin-x86_64/bin/gdb
    //Target the gdb to the remote sever
    (gdb) target remote :5045
    
    //now the process is successfully attached with the application for debugging, you can see below info from terminal 1.
    Remote debugging from host 127.0.0.1