How to debug an App on Android with GDBSERVER?

前端 未结 2 776
太阳男子
太阳男子 2020-12-31 17:40

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 lau

2条回答
  •  情深已故
    2020-12-31 18:28

    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] 
            -D: enable debugging
    
        send a broadcast Intent: am broadcast 
    
        start an Instrumentation: am instrument [flags] 
            -r: print raw results (otherwise decode REPORT_KEY_STREAMRESULT)
            -e  : set argument  to 
            -p : write profiling data to 
            -w: wait for instrumentation to finish before returning
    
        start profiling: am profile  start 
        stop profiling: am profile  stop
    
         specifications include these flags:
            [-a ] [-d ] [-t ]
            [-c  [-c ] ...]
            [-e|--es   ...]
            [--ez   ...]
            [-e|--ei   ...]
            [-n ] [-f ] []
    
    
    # am start -n com.android.browser/.BrowserActivity
    Starting: Intent { cmp=com.android.browser/.BrowserActivity }
    #
    

    Once your application is running, use gdbserver --attach 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.

提交回复
热议问题