Android Studio+LLDB調試內核Binder

文中所使用的例子:https://github.com/dodola/BinderDebug

最近在研究Binder架構,由于本人比較菜,只分析代碼邏輯無法很清楚的了解其中的數據流向以及數據結構,所以想整理一套簡單的調試工具幫助我來分析,我一共嘗試過三種方法:

  1. 直接使用GDB調試(本人比較菜用的很不順手)
  2. Eclipse+GDB,效果很好,配置起來有點復雜
  3. AndroidStudio+LLDB,這是用著效果最好

先來看一下我們可以調試到什么程度

enter description here

從上圖中可以看到調試IPCThreadState.cpp的過程,我們可以看到參數的值,以及各個變量的結構和對應的值

下面描述一下環境搭建過程.

注:本人系統環境是Mac

編譯系統源碼

進行調試的前提是我們有一套系統源碼,并且已經編譯完成

我編譯了三個版本的代碼:4.4.1_r1(本文使用的版本),5.1.0_r1,6.0.1_r1,經我測試都可以使用這種方法調試

這里不寫編譯過程了,具體可以查看官方文檔

如果沒有更改編譯輸出目錄的話,編譯完成后會在源碼根目錄生成out文件夾

我們需要關注的是out/target/product/generic下的東西:

Android Build

配置Android Studio NDK編譯環境

Build Environment

  • Android Studio 2.1 Preview 4
  • Gradle 2.10
  • Android NDK r10e
  • gradle-experimental 0.7.0-alpha1

基本上都是最新的環境.

編譯Demo例子

Android Studio 1.4以后就支持NDK編譯了,詳細的Gradle配置可以去看The new NDK support in Android Studio這篇文章,這里不在贅述,想嘗試例子的可以去google 官方的github,直接導入運行即可

這里只描述一下如何使用NDK編譯依賴系統庫的應用.

我們都知道NDK里并沒有提供Binder庫的相關頭文件和lib庫,所以無法直接使用NDK編譯Binder C++ 的程序,所以我們需要從編譯好的系統目錄里將需要的共享庫提取出來供我們編譯使用.

下面是我提取出來編譯該Demo所使用的shared libs:

Libs

libbinder.so 是Binder的核心庫,包含ServiceManager和Pracple的代碼,其他三個都是libbinder.so的依賴庫

除此之外還需要這些庫的頭文件,目錄樹如下:

.
├── binder ----> libbinder.so 
├── core ---->別的頭文件里有引用此頭文件
│   └── include
│       ├── android
│       ├── corkscrew
│       ├── ctest
│       ├── cutils
│       ├── diskconfig
│       ├── ion
│       ├── log
│       ├── memtrack
│       ├── mincrypt
│       ├── netutils
│       ├── pixelflinger
│       ├── private
│       │   └── pixelflinger
│       ├── sync
│       ├── system
│       ├── sysutils
│       ├── usbhost
│       ├── utils
│       └── zipfile
├── cutils ---->libcutils.so
├── libc ----->暫時沒用到
│   ├── android
│   ├── arpa
│   ├── machine
│   ├── net
│   ├── netinet
│   ├── netpacket
│   └── sys
├── log
├── system
└── utils ---->libutils.so

下面貼出gradle配置文件里系統庫的配置

apply plugin: 'com.android.model.application'
model {
    repositories {

        libs(PrebuiltLibraries) {
            libc {
                headers.srcDir rootDir.absolutePath + "/binder_lib/include/libc"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(rootDir.absolutePath + "/binder_lib/libs/libc.so")
                }
            }
            binderlib {
                headers.srcDir rootDir.absolutePath + "/binder_lib/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(rootDir.absolutePath + "/binder_lib/libs/libbinder.so")
                }
            }
            libcutils {
                headers.srcDir rootDir.absolutePath + "/binder_lib/include/core/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(rootDir.absolutePath + "/binder_lib/libs/libcutils.so")
                }
            }
            libutils {
                headers.srcDir rootDir.absolutePath + "/binder_lib/include/core/include"
                binaries.withType(SharedLibraryBinary) {
                    sharedLibraryFile = file(rootDir.absolutePath + "/binder_lib/libs/libutils.so")
                }
            }
        }
    }
    android {
        compileSdkVersion = 23
        buildToolsVersion = "23.0.2"
        println(rootDir.absolutePath + "/binder_lib/include/libc")
        defaultConfig {
            applicationId "dodola.binder"
            minSdkVersion.apiLevel 15
            targetSdkVersion.apiLevel 22
            versionCode 1
            versionName "1.0"

            buildConfigFields {
                create() {
                    type "int"
                    name "VALUE"
                    value "1"
                }
            }
        }
    }


    android.ndk {
        moduleName = "binder_ndk"
        cppFlags.addAll(["-Werror", "-fno-rtti", "-fno-exceptions"/*, "-std=c++11"*/])
        CFlags.addAll(["-Werror"])
        ldLibs.addAll(['android', 'log'])
//        stl = "gnustl_static"
        debuggable = true

    }
    android.buildTypes {
        release {
            minifyEnabled = false
            proguardFiles.add(file('proguard-rules.txt'))
        }
    }
    android.sources {
        main {
            jni {
                dependencies {
//                    library "libc" linkage "shared"
                    library "binderlib" linkage "shared" buildType "debug"
                    library "libcutils" linkage "shared" buildType "debug"
                    library "libutils" linkage "shared" buildType "debug"

                }
            }
        }
    }
    android.productFlavors {
        // for detailed abiFilter descriptions, refer to "Supported ABIs" @
        // https://developer.android.com/ndk/guides/abis.html#sa
        create("arm") {
            ndk.abiFilters.add("armeabi")
        }

        create("all")
    }
}

dependencies {

    compile fileTree(dir: 'libs', include: ['*.jar'])
    testCompile 'junit:junit:4.12'
    compile 'com.android.support:appcompat-v7:23.2.1'
}

Android Studio 配置

enter description here

然后添加一個 Android Native 的Configuration

enter description here

Module這里選擇 APP

enter description here

然后在Debugger 里,添加Symbol 文件夾(不確定是否有用)

enter description here

運行應用

需要注意的是這個例子需要在自己編譯好的系統下運行,因為我們使用的so文件在調試的時候需要根據symbol的地址查找源碼,否則就會出現地址找不到的情況.

我沒有真機進行調試這里只演示模擬器的調試.

在已經經過編譯的系統源碼目錄下運行以下命令

> $ . build/envsetup.sh                                                ? 5.7.0 
build/envsetup.sh:537: command not found: complete
WARNING: Only bash is supported, use of other shell would lead to erroneous results
including device/asus/deb/vendorsetup.sh
including device/asus/flo/vendorsetup.sh
including device/asus/grouper/vendorsetup.sh
including device/asus/tilapia/vendorsetup.sh
including device/generic/armv7-a-neon/vendorsetup.sh
including device/generic/mips/vendorsetup.sh
including device/generic/x86/vendorsetup.sh
including device/lge/hammerhead/vendorsetup.sh
including device/lge/mako/vendorsetup.sh
including device/samsung/manta/vendorsetup.sh
                                                                                
> $ lunch                                                              ? 5.7.0 

You're building on Darwin

Lunch menu... pick a combo:
     1. aosp_arm-eng
     2. aosp_x86-eng
     3. aosp_mips-eng
     4. vbox_x86-eng
     5. aosp_deb-userdebug
     6. aosp_flo-userdebug
     7. aosp_grouper-userdebug
     8. aosp_tilapia-userdebug
     9. mini_armv7a_neon-userdebug
     10. mini_mips-userdebug
     11. mini_x86-userdebug
     12. aosp_hammerhead-userdebug
     13. aosp_mako-userdebug
     14. aosp_manta-userdebug

Which would you like? [aosp_arm-eng] 

============================================
PLATFORM_VERSION_CODENAME=REL
PLATFORM_VERSION=4.4.1
TARGET_PRODUCT=aosp_arm
TARGET_BUILD_VARIANT=eng
TARGET_BUILD_TYPE=release
TARGET_BUILD_APPS=
TARGET_ARCH=arm
TARGET_ARCH_VARIANT=armv7-a
TARGET_CPU_VARIANT=generic
HOST_ARCH=x86
HOST_OS=darwin
HOST_OS_EXTRA=Darwin-15.4.0-x86_64-i386-64bit
HOST_BUILD_TYPE=release
BUILD_ID=KOT49E
OUT_DIR=out
============================================

                                                                                
> $ emulator                                                           ? 5.7.0 
emulator: WARNING: system partition size adjusted to match image file (550 MB > 200 MB)


這樣就可以將模擬器啟動起來

然后就可以在Android Studio里運行例子了.

enter description here

調試應用

上面所有步驟配置好以后,則可以調試應用了

  1. 選擇Attach debug to Android process
enter description here
  1. 在彈出的對話框里選擇Native選項
enter description here
  1. 點擊ok以后,debug的控制臺會輸出LLDB的調用命令
enter description here

效果

哈,這樣我們在調試的時候就可以深入到binder內部看一下它的流程了

下面這個圖是斷點到IServiceManager.cpp的情況,以及變量的結構

enter description here
enter description here

下面這個圖是mData里的結構(以前只能靠YY)

enter description here
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,048評論 6 542
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 99,414評論 3 429
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,169評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,722評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 72,465評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,823評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,813評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 43,000評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,554評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 41,295評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,513評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,722評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,125評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,430評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,237評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,482評論 2 379

推薦閱讀更多精彩內容