简体   繁体   中英

How to debug an App on Android with GDBSERVER?

I'm trying to debug a native shared library that my App uses through JNI. I can attach to a running app just fine with "gdbserver --attach pid" but i need to actually launch my app when i launch the gdbserver command.

They all say to just type "gdbserver 10.0.2.2:1234 .\/MyProgram" but what exactly is "MyProgram". Is that MyProgram.apk? Is it MyProgram.so? Is it some other file that gets created when the app is installed? If so, what's its path?

While it is possible to develop free standing applications that can be launched directly from the shell as others are describing, it sounds like your code runs within the Android application framework. Therefore, you don't have an executable and instead have an APK that contains your Dalvik class files along with other resources including your native shared object.

Launching an application in an APK involves several steps

  1. The system_server process receives an intent requesting your application.
  2. The zygote process is told to fork off a new process and run a method of your class.
  3. Your application runs in the new process.

While you can't launch an APK directly by passing an executable to gdbserver, its fairly easy to trigger a launch it from the shell using the am command.

$ adb -d shell
# am
usage: am [subcommand] [options]

    start an Activity: am start [-D] <INTENT>
        -D: enable debugging

    send a broadcast Intent: am broadcast <INTENT>

    start an Instrumentation: am instrument [flags] <COMPONENT>
        -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
        -e <NAME> <VALUE>: set argument <NAME> to <VALUE>
        -p <FILE>: write profiling data to <FILE>
        -w: wait for instrumentation to finish before returning

    start profiling: am profile <PROCESS> start <FILE>
    stop profiling: am profile <PROCESS> stop

    <INTENT> specifications include these flags:
        [-a <ACTION>] [-d <DATA_URI>] [-t <MIME_TYPE>]
        [-c <CATEGORY> [-c <CATEGORY>] ...]
        [-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
        [--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
        [-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
        [-n <COMPONENT>] [-f <FLAGS>] [<URI>]


# am start -n com.android.browser/.BrowserActivity
Starting: Intent { cmp=com.android.browser/.BrowserActivity }
#

Once your application is running, use gdbserver --attach <pid> like you have before. If you are lucky your application waits for some user interaction before calling into your native code to give you a chance to attach and set your breakpoints in GDB.

Google provides an official solution to your problem: 'ndk-gdb'

It is included in the NDK. IIRC, it requires that you have a copy of gdbserver bundled up inside your APK; IIRC, this will happen automatically if you built your APK with 'ndk-build', and specified the appropriate arguments.

Please see Google's documentation in $NDK/docs/NDK-BUILD.html and $NDK/docs/NDK-GDB.html

</ryan>

For the gdbserver executable, I just copied it from the NDK folder of the Android SDK:

  1. Download Android NDK from SDK tools tab of Android Studio preferences.
  2. Copy gdbserver to the rooted device: adb push {android-sdk}/ndk/{version}/prebuilt/android-arm64/gdbserver/gdbserver /data/local/tmp

To run gdbserver, with the device plugged in:

  1. adb shell su -c setenforce 0
  2. In the device, accept superuser access rights of shell
  3. Launch the app you want to debug
  4. adb shell ps | grep {package-name}
  5. Copy the process id (2nd column)
  6. adb shell
  7. cd /data/local/tmp
  8. su -c ./gdbserver :<any-port-number> --attach <pid> (app will freeze)

For the gdbclient executable, I had to build it from the gdb source code to configure the correct architecture (arm64) for the Android device. On a separate mac terminal window/tab:

  1. cd gdb-10.1
  2. ./configure --target=aarch64-linux-android && make -j8 && sudo make install
  3. aarch64-linux-android-gdb
  4. set sysroot
  5. target remote <phone-ip-address>:<port-number-above>
  6. continue (to unfreeze app)
  7. Start debugging.
/gdb-9.1/arm64/gdb/../../gdb/python/py-cmd.c:191: undefined reference to `PyUnicodeUCS2_Decode'

@Siklab.ph sorry, In compile i got a python error ,Then I recompile python with option ./configure --enable-unicode=ucs2

But got a same error .

do you have any way to fix it?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM