Mulai 27 Maret 2025, sebaiknya gunakan android-latest-release
, bukan aosp-main
, untuk mem-build dan berkontribusi pada AOSP. Untuk mengetahui informasi selengkapnya, lihat Perubahan pada AOSP.
Menggunakan strace
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Strace memungkinkan Anda melihat panggilan sistem
yang dibuat proses dan apa yang ditampilkan oleh panggilan sistem tersebut.
Mem-build strace
Untuk mem-build strace, jalankan perintah berikut:
mmma -j6 external/strace
Memasang ke proses yang sedang berjalan
Kasus penggunaan yang paling sederhana dan paling umum untuk strace adalah melampirkan strace ke proses
yang sedang berjalan, yang dapat Anda lakukan dengan:
adb shell strace -f -p PID
Flag -f
memberi tahu strace untuk melampirkan ke semua thread dalam
proses, ditambah thread baru yang dibuat nanti.
Proses standar membuat banyak panggilan sistem, jadi sebaiknya Anda meninjau
halaman man strace
untuk mempelajari cara mengumpulkan hanya data yang benar-benar Anda minati.
Menggunakan di aplikasi
Untuk menggunakan strace di aplikasi:
- Siapkan perangkat agar Anda dapat menjalankan strace. Anda harus memiliki hak akses root, menonaktifkan SELinux, dan memulai ulang
runtime untuk menghapus filter seccomp yang akan mencegah strace berjalan:
adb root
adb shell setenforce 0
adb shell stop
adb shell start
- Siapkan direktori yang dapat ditulis oleh semua orang untuk log strace, karena strace akan berjalan di bawah
uid aplikasi:
adb shell mkdir -m 777 /data/local/tmp/strace
- Pilih proses yang akan dilacak dan luncurkan:
adb shell setprop wrap.com.android.calendar '"logwrapper strace -f -o /data/local/tmp/strace/strace.com.android.calendar.txt"'
- Meluncurkan proses secara normal.
Menggunakan di zygote
Untuk menggunakan strace pada zygote, perbaiki baris zygote init.rc
yang relevan (memerlukan 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
Mendapatkan log strace selama booting Android
Untuk mendapatkan log strace selama booting Android, lakukan perubahan berikut:
- Karena nama proses berubah dari
zygote
menjadi
strace
, layanan tertentu mungkin gagal dimulai karena tidak adanya
file_context
SELinux untuk strace
. Solusinya adalah
menambahkan baris baru untuk strace di system/sepolicy/private/file_contexts
dan menyalin konteks file asli. Contoh:
/dev/socket/zygote u:object_r:zygote_socket:s0
+ /system/bin/strace u:object_r:zygote_socket:s0
- Tambahkan parameter kernel atau bootconfig, lalu booting perangkat dalam mode permisif SELinux. Anda dapat
melakukannya dengan menambahkan
androidboot.selinux=permissive
ke
BOARD_KERNEL_CMDLINE
, atau ke BOARD_BOOTCONFIG
di Android
12 dengan versi kernel 5.10 atau yang lebih baru.
(Variabel ini menjadi hanya baca di build/core/Makefile
, tetapi selalu
tersedia di bagian /device/*/BoardConfig
.)
Contoh untuk perangkat Pixel (sailfish) di
/device/google/marlin/sailfish/BoardConfig.mk
:
- BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ...
+BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ... androidboot.selinux=permissive
Setelah melakukan perubahan, build dan flash image booting, lalu perangkat akan melakukan booting
dalam mode permisif.
Konten dan contoh kode di halaman ini tunduk kepada lisensi yang dijelaskan dalam Lisensi Konten. Java dan OpenJDK adalah merek dagang atau merek dagang terdaftar dari Oracle dan/atau afiliasinya.
Terakhir diperbarui pada 2025-07-27 UTC.
[[["Mudah dipahami","easyToUnderstand","thumb-up"],["Memecahkan masalah saya","solvedMyProblem","thumb-up"],["Lainnya","otherUp","thumb-up"]],[["Informasi yang saya butuhkan tidak ada","missingTheInformationINeed","thumb-down"],["Terlalu rumit/langkahnya terlalu banyak","tooComplicatedTooManySteps","thumb-down"],["Sudah usang","outOfDate","thumb-down"],["Masalah terjemahan","translationIssue","thumb-down"],["Masalah kode / contoh","samplesCodeIssue","thumb-down"],["Lainnya","otherDown","thumb-down"]],["Terakhir diperbarui pada 2025-07-27 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."]]