À partir du 27 mars 2025, nous vous recommandons d'utiliser android-latest-release
au lieu de aosp-main
pour créer et contribuer à AOSP. Pour en savoir plus, consultez la section Modifications apportées à AOSP.
Utiliser strace
Restez organisé à l'aide des collections
Enregistrez et classez les contenus selon vos préférences.
Strace vous permet de voir les appels système effectués par un processus et ce qu'ils renvoient.
Créer strace
Pour créer strace, exécutez la commande suivante:
mmma -j6 external/strace
Associer à un processus en cours d'exécution
Le cas d'utilisation le plus simple et le plus courant de strace consiste à l'associer à un processus en cours d'exécution, ce que vous pouvez faire avec:
adb shell strace -f -p PID
L'indicateur -f
indique à strace de s'attacher à tous les threads du processus, ainsi qu'à tous les nouveaux threads créés ultérieurement.
Un processus typique effectue de nombreux appels système. Nous vous conseillons donc de consulter la page de manuel strace pour apprendre à ne collecter que les données qui vous intéressent.
Utiliser dans une application
Pour utiliser strace sur une application:
- Configurez l'appareil pour pouvoir exécuter strace. Vous devez être root, désactiver SELinux et redémarrer l'environnement d'exécution pour supprimer le filtre seccomp qui empêcherait l'exécution de strace:
adb root
adb shell setenforce 0
adb shell stop
adb shell start
- Configurez un répertoire accessible en écriture à l'échelle mondiale pour les journaux strace, car strace s'exécute sous l'UID de l'application:
adb shell mkdir -m 777 /data/local/tmp/strace
- Sélectionnez le processus à tracer et lancez-le:
adb shell setprop wrap.com.android.calendar '"logwrapper strace -f -o /data/local/tmp/strace/strace.com.android.calendar.txt"'
- Lancez le processus normalement.
Utiliser sur le zygote
Pour utiliser strace sur le zygote, corrigez la ligne zygote init.rc
appropriée (requiert 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
Obtenir des journaux strace au démarrage d'Android
Pour obtenir des journaux strace au démarrage d'Android, apportez les modifications suivantes:
- Étant donné que le nom du processus passe de
zygote
à strace
, le service donné risque de ne pas démarrer en raison de l'absence de file_context
SELinux pour strace
. La solution consiste à ajouter une ligne pour strace dans system/sepolicy/private/file_contexts
et à copier le contexte de fichier d'origine. Exemple :
/dev/socket/zygote u:object_r:zygote_socket:s0
+ /system/bin/strace u:object_r:zygote_socket:s0
- Ajoutez le paramètre kernel ou bootconfig, puis démarrez l'appareil en mode SELinux permissif. Pour ce faire, ajoutez
androidboot.selinux=permissive
à BOARD_KERNEL_CMDLINE
ou à BOARD_BOOTCONFIG
dans Android 12 avec la version de kernel 5.10 ou ultérieure.
(Cette variable devient en lecture seule dans build/core/Makefile
, mais est toujours disponible sous /device/*/BoardConfig
.)
Exemple pour l'appareil Pixel (sailfish) dans /device/google/marlin/sailfish/BoardConfig.mk
:
- BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ...
+BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ... androidboot.selinux=permissive
Une fois la modification effectuée, créez et flashez l'image de démarrage. L'appareil démarrera en mode permissif.
Le contenu et les exemples de code de cette page sont soumis aux licences décrites dans la Licence de contenu. Java et OpenJDK sont des marques ou des marques déposées d'Oracle et/ou de ses sociétés affiliées.
Dernière mise à jour le 2025/07/27 (UTC).
[[["Facile à comprendre","easyToUnderstand","thumb-up"],["J'ai pu résoudre mon problème","solvedMyProblem","thumb-up"],["Autre","otherUp","thumb-up"]],[["Il n'y a pas l'information dont j'ai besoin","missingTheInformationINeed","thumb-down"],["Trop compliqué/Trop d'étapes","tooComplicatedTooManySteps","thumb-down"],["Obsolète","outOfDate","thumb-down"],["Problème de traduction","translationIssue","thumb-down"],["Mauvais exemple/Erreur de code","samplesCodeIssue","thumb-down"],["Autre","otherDown","thumb-down"]],["Dernière mise à jour le 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."]]