החל מ-27 במרץ 2025, מומלץ להשתמש ב-android-latest-release
במקום ב-aosp-main
כדי ליצור תרומות ל-AOSP. מידע נוסף זמין במאמר שינויים ב-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, כי strace יפעל באמצעות מזהה המשתמש (uid) של האפליקציה:
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
כדי להשתמש ב-strace ב-zygote, צריך לתקן את השורה הרלוונטית של 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
אחזור יומני strace במהלך האתחול של Android
כדי לקבל יומני strace במהלך האתחול של Android, מבצעים את השינויים הבאים:
- מכיוון ששם התהליך משתנה מ-
zygote
ל-strace
, יכול להיות שהשירות לא יופעל בגלל ש-SELinux file_context
חסר ל-strace
. הפתרון הוא להוסיף שורה חדשה ל-strace בקובץ system/sepolicy/private/file_contexts
ולהעתיק את ההקשר של הקובץ המקורי. דוגמה:
/dev/socket/zygote u:object_r:zygote_socket:s0
+ /system/bin/strace u:object_r:zygote_socket:s0
- מוסיפים פרמטר של ליבה או bootconfig, ואז מפעילים את המכשיר במצב הרשאה של SELinux. כדי לעשות זאת, מוסיפים את
androidboot.selinux=permissive
אל BOARD_KERNEL_CMDLINE
או אל BOARD_BOOTCONFIG
ב-Android 12 עם גרסה 5.10 ואילך של הליבה.
(המשתנה הזה הופך לקריאה בלבד ב-build/core/Makefile
, אבל תמיד זמין ב-/device/*/BoardConfig
).
דוגמה למכשיר Pixel (sailfish) ב-/device/google/marlin/sailfish/BoardConfig.mk
:
- BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ...
+BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ... androidboot.selinux=permissive
אחרי ביצוע השינוי, יוצרים את קובץ האימג' לאתחול ומעבירים אותו למכשיר באמצעות הפלאש, והמכשיר יופעל במצב הרשאות מלא.
דוגמאות התוכן והקוד שבדף הזה כפופות לרישיונות המפורטים בקטע רישיון לתוכן. Java ו-OpenJDK הם סימנים מסחריים או סימנים מסחריים רשומים של חברת Oracle ו/או של השותפים העצמאיים שלה.
עדכון אחרון: 2025-07-27 (שעון 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-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."]]