2025 年 3 月 27 日より、AOSP のビルドとコントリビューションには aosp-main
ではなく android-latest-release
を使用することをおすすめします。詳細については、AOSP の変更をご覧ください。
strace の使用
コレクションでコンテンツを整理
必要に応じて、コンテンツの保存と分類を行います。
strace を使用すると、プロセスが行うシステムコールと、そのシステムコールが返す結果を確認できます。
strace の作成
strace を作成するには、次のコマンドを実行します。
mmma -j6 external/strace
実行中のプロセスにアタッチする
strace の非常に簡単かつ一般的な使用例は、次のように、実行中のプロセスにアタッチすることです。
adb shell strace -f -p PID
-f
フラグを指定すると、strace はプロセス内のすべてのスレッドと、後で生成される新しいスレッドにアタッチされます。
一般的なプロセスでは大量のシステムコールが行われるため、strace のマニュアル ページを参照して、実際に必要なデータのみを収集する方法を確認することをおすすめします。
アプリで使用する
アプリで strace を使用する手順は次のとおりです。
- strace を実行できるようにデバイスをセットアップします。root 権限で SELinux を無効にし、ランタイムを再起動して、seccomp フィルタを削除する必要があります。削除しないと、strace を実行できません。
adb root
adb shell setenforce 0
adb shell stop
adb shell start
- strace はアプリの uid で実行されるため、strace ログ用に、グローバルに書き込み可能なディレクトリを設定します。
adb shell mkdir -m 777 /data/local/tmp/strace
- トレースするプロセスを選択して起動します。
adb shell setprop wrap.com.android.calendar '"logwrapper strace -f -o /data/local/tmp/strace/strace.com.android.calendar.txt"'
- プロセスを通常どおり起動します。
zygote で使用する
zygote で strace を使用するには、関連する init.rc
の zygote 行を修正します(adb shell setenforce 0
が必要です)。
cd system/core/
patch -p1 <<EOF
--- a/rootdir/init.zygote32.rc
+++ b/rootdir/init.zygote32.rc
@@ -1,4 +1,4 @@
-service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
+service zygote /system/bin/strace -o /data/local/tmp/zygote.strace /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server
class main
socket zygote stream 660 root system
onrestart write /sys/android_power/request_state wake
EOF
Android の起動時に strace ログを取得する
Android の起動時に strace ログを取得するには、次の変更を行います。
- プロセス名が
zygote
から strace
に変更されるため、strace
の SELinux file_context
がないことが原因で、指定されたサービスを開始できない場合があります。この問題を解決するには、system/sepolicy/private/file_contexts
に strace 用の新しい行を追加し、元のファイル コンテキストをコピーします。例:
/dev/socket/zygote u:object_r:zygote_socket:s0
+ /system/bin/strace u:object_r:zygote_socket:s0
- カーネルまたは bootconfig のパラメータを追加し、SELinux の permissive モードでデバイスを起動します。これを行うには、
androidboot.selinux=permissive
を BOARD_KERNEL_CMDLINE
に追加するか、カーネル バージョン 5.10 以降の Android 12 では BOARD_BOOTCONFIG
に追加します。(この変数は、build/core/Makefile
では読み取り専用ですが、/device/*/BoardConfig
の下では常に使用可能です)。
/device/google/marlin/sailfish/BoardConfig.mk
の Google Pixel(sailfish)デバイスの例を次に示します。
- BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ...
+BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ... androidboot.selinux=permissive
変更後、ブートイメージをビルドしてフラッシュすると、デバイスが permissive モードで起動します。
このページのコンテンツやコードサンプルは、コンテンツ ライセンスに記載のライセンスに従います。Java および OpenJDK は Oracle および関連会社の商標または登録商標です。
最終更新日 2025-03-26 UTC。
[[["わかりやすい","easyToUnderstand","thumb-up"],["問題の解決に役立った","solvedMyProblem","thumb-up"],["その他","otherUp","thumb-up"]],[["必要な情報がない","missingTheInformationINeed","thumb-down"],["複雑すぎる / 手順が多すぎる","tooComplicatedTooManySteps","thumb-down"],["最新ではない","outOfDate","thumb-down"],["翻訳に関する問題","translationIssue","thumb-down"],["サンプル / コードに問題がある","samplesCodeIssue","thumb-down"],["その他","otherDown","thumb-down"]],["最終更新日 2025-03-26 UTC。"],[],[],null,["# Use strace\n\n[Strace](https://strace.io) enables you to see the system calls a\nprocess makes and what those system calls return.\n\nBuild strace\n------------\n\nTo build strace, run the following: \n\n```\nmmma -j6 external/strace\n```\n\nAttach to a running process\n---------------------------\n\nThe simplest and most common use case for strace is to attach it to a running\nprocess, which you can do with: \n\n```\nadb shell strace -f -p PID\n```\n\nThe `-f` flag tells strace to attach to all the threads in the\nprocess, plus any new threads spawned later.\n\nA typical process makes a lot of system calls, so you'll want to review the\n[strace man page](http://man7.org/linux/man-pages/man1/strace.1.html)\nto learn how to collect only data you're actually interested in.\n\nUse on an app\n-------------\n\nTo use strace on an app:\n\n1. Set up the device so that you can run strace. You need to be root, disable SELinux, and restart the runtime to remove the seccomp filter that will otherwise prevent strace from running: \n\n adb root\n adb shell setenforce 0\n adb shell stop\n adb shell start\n\n2. Set up a world-writable directory for strace logs, because strace will be running under the app's uid: \n\n adb shell mkdir -m 777 /data/local/tmp/strace\n\n3. Choose the process to trace and launch it: \n\n ```\n adb shell setprop wrap.com.android.calendar '\"logwrapper strace -f -o /data/local/tmp/strace/strace.com.android.calendar.txt\"'\n ```\n4. Launch the process normally.\n\nUse on the zygote\n-----------------\n\nTo use strace on the zygote, fix the relevant `init.rc` zygote\nline (requires `adb shell setenforce 0`): \n\n cd system/core/\n patch -p1 \u003c\u003cEOF\n --- a/rootdir/init.zygote32.rc\n +++ b/rootdir/init.zygote32.rc\n @@ -1,4 +1,4 @@\n -service zygote /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server\n +service zygote /system/bin/strace -o /data/local/tmp/zygote.strace /system/bin/app_process -Xzygote /system/bin --zygote --start-system-server\n class main\n socket zygote stream 660 root system\n onrestart write /sys/android_power/request_state wake\n EOF\n\nGet strace logs during Android boot\n-----------------------------------\n\nTo get strace logs during Android boot, make the following changes:\n\n- Since the process name changes from `zygote` to `strace`, the given service may fail to start due to the missing SELinux `file_context` for `strace`. The solution is to add a new line for strace in `system/sepolicy/private/file_contexts` and copy the original file context over. Example: \n\n ```\n /dev/socket/zygote u:object_r:zygote_socket:s0\n + /system/bin/strace u:object_r:zygote_socket:s0\n ```\n- Add kernel or bootconfig parameter, then boot the device in SELinux permissive mode. You can do this by adding `androidboot.selinux=permissive`to `BOARD_KERNEL_CMDLINE`, or to `BOARD_BOOTCONFIG` in Android 12 with kernel version 5.10 or greater. (This variable becomes read-only in `build/core/Makefile` but is always available under `/device/*/BoardConfig`.) \n\n Example for the Pixel (sailfish) device in `/device/google/marlin/sailfish/BoardConfig.mk`: \n\n ```\n - BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ...\n +BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ... androidboot.selinux=permissive\n ```\n After making the change, build and flash the boot image and the device will boot in permissive mode."]]