A partir del 27 de marzo de 2025, te recomendamos que uses android-latest-release
en lugar de aosp-main
para compilar y contribuir a AOSP. Para obtener más información, consulta Cambios en AOSP.
Usa strace
Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Strace te permite ver las llamadas al sistema que realiza un proceso y lo que devuelven esas llamadas.
Compila strace
Para compilar strace, ejecuta lo siguiente:
mmma -j6 external/strace
Cómo adjuntar el depurador a un proceso en ejecución
El caso de uso más simple y común de strace es conectarlo a un proceso en ejecución, lo que puedes hacer con lo siguiente:
adb shell strace -f -p PID
La marca -f
le indica a strace que se una a todos los subprocesos del proceso, además de los subprocesos nuevos que se generen más adelante.
Un proceso típico realiza muchas llamadas al sistema, por lo que te recomendamos que revises la
página de man de strace
para aprender a recopilar solo los datos que te interesan.
Uso en una app
Para usar strace en una app, haz lo siguiente:
- Configura el dispositivo para que puedas ejecutar strace. Debes tener permisos de root, inhabilitar SELinux y reiniciar el entorno de ejecución para quitar el filtro seccomp que, de lo contrario, impedirá que se ejecute strace:
adb root
adb shell setenforce 0
adb shell stop
adb shell start
- Configura un directorio de escritura pública para los registros de strace, ya que strace se ejecutará con el UID de la app:
adb shell mkdir -m 777 /data/local/tmp/strace
- Elige el proceso que quieres rastrear y, luego, inícialo:
adb shell setprop wrap.com.android.calendar '"logwrapper strace -f -o /data/local/tmp/strace/strace.com.android.calendar.txt"'
- Inicia el proceso de forma normal.
Cómo usar en el zigoto
Para usar strace en el zygote, corrige la línea init.rc
relevante del zygote (requiere 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
Cómo obtener registros de strace durante el inicio de Android
Para obtener registros de strace durante el inicio de Android, realiza los siguientes cambios:
- Dado que el nombre del proceso cambia de
zygote
a strace
, es posible que el servicio determinado no se inicie debido a que falta file_context
de SELinux para strace
. La solución es agregar una línea nueva para strace en system/sepolicy/private/file_contexts
y copiar el contexto del archivo original. Ejemplo:
/dev/socket/zygote u:object_r:zygote_socket:s0
+ /system/bin/strace u:object_r:zygote_socket:s0
- Agrega el parámetro kernel o bootconfig y, luego, inicia el dispositivo en el modo permisivo de SELinux. Para ello, agrega
androidboot.selinux=permissive
a BOARD_KERNEL_CMDLINE
o a BOARD_BOOTCONFIG
en Android 12 con la versión de kernel 5.10 o posterior.
(Esta variable se convierte en de solo lectura en build/core/Makefile
, pero siempre está disponible en /device/*/BoardConfig
).
Ejemplo para el dispositivo Pixel (sailfish) en /device/google/marlin/sailfish/BoardConfig.mk
:
- BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ...
+BOARD_KERNEL_CMDLINE := .... androidboot.hardware=sailfish ... androidboot.selinux=permissive
Después de realizar el cambio, compila y escribe en la memoria flash la imagen de inicio para que el dispositivo se inicie en modo permisivo.
El contenido y las muestras de código que aparecen en esta página están sujetas a las licencias que se describen en la Licencia de Contenido. Java y OpenJDK son marcas registradas de Oracle o sus afiliados.
Última actualización: 2025-07-27 (UTC)
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Falta la información que necesito","missingTheInformationINeed","thumb-down"],["Muy complicado o demasiados pasos","tooComplicatedTooManySteps","thumb-down"],["Desactualizado","outOfDate","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Problema con las muestras o los códigos","samplesCodeIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 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."]]