Depurando o Kernel do Linux com o ftrace
De Wiki do Veiga
Tabela de conteúdo |
Introdução
Ftrace é um rastreador (tracer) interno do kernel do Linux projetado para facilitar a depuração e análise de latência e desempenho. Ftrace suporte diferentes tipos de rastreamento, como por exemplo: rastreamento de funções, trocas de contexto, tempo em interrupção desabilitada, entre outros.
Este texto descreve algumas formas bastante úteis de utilizar o ftrace para depurar o kernel. Para informações detalhadas, consulte os texto da seção de referências.
Compilando o Kernel com Suporte ao ftrace
Configuração pelo make menuconfig.
Kernel hacking --->
Tracers --->
-*- Kernel Function Tracer
[*] Kernel Function Graph Tracer
[*] Scheduling Latency Tracer
-*- Trace process context switches
[*] Trace various events in the kernel
[*] Trace boot initcalls
[*] Trace likely/unlikely profiler
[ ] Profile all if conditionals
[*] Trace likely/unlikely instances
[*] Trace max stack
[*] Trace SLAB allocations
[*] Trace workqueues
[*] Support for tracing block io actions
[*] enable/disable ftrace tracepoints dynamically
[*] Perform a startup test on ftrace
Utilizando o ftrace
Ftrace utiliza o debugfs file system para manter os arquivos de controle e de saida dos tracers.
Para montar o debugfs:
mkdir /debug mount -t debugfs nodev /debug
Toda a configuração e visualização de resultado dos tracers são realizadas através dos arquivos contidos em /debug/tracing.
ls /debug/tracing ...
Para verificar se o tracer está rodando:
cat /debug/tracing/tracing_enabled 1
Por default, o tracer está habilitado. Para ligar ou desligar:
# Desabilitar echo 0 > /debug/tracing/tracing_enabled # Habilitar echo 1 > /debug/tracing/tracing_enabled
Exemplos de Rastreamento
Rastrear todas as funções
O ftrace_enabled de estar habilidado, senão este tracer é um nop.
sysctl kernel.ftrace_enabled=1
Exemplo de tracing:
echo function > /debug/tracing/current_tracer echo 1 > /debug/tracing/tracing_enabled usleep 1 echo 0 > /debug/tracing/tracing_enabled cat /debug/tracing/trace
Rastrear funções específicas (filtros)
Através dos arquivos set_ftrace_filter e set_ftrace_notrace é possível utilizar filtros para especificar quais as funções que devem ser rastreadas.
Uma lista de todas as funções que podem ser utilizadas como filtro é disponibilizada no arquivo available_filter_functions.
cat /debug/tracing/available_filter_functions ...
Rastreamento de uma única função
echo function > /debug/tracing/current_tracer echo sys_open > /debug/tracing/set_ftrace_filter echo 1 > /debug/tracing/tracing_enabled ls > /dev/null echo 0 > /debug/tracing/tracing_enabled cat /debug/tracing/trace
Rastreamento de mais de uma função
echo sys_open sys_close > /debug/tracing/set_ftrace_filter
ou
echo sys_open > /debug/tracing/set_ftrace_filter echo sys_close >> /debug/tracing/set_ftrace_filter
Rastreamento de grupos de funções (por exemplo, mesmo módulo)
echo 'sys_*' > /debug/tracing/set_ftrace_filter
- <match>* - irá casar funções que começam com <match>
- *<match> - irá casar funções que terminam com <match>
- *<match>* - irá casar funções que contêm <match>
Rastrear uma função completa (com todos seus filhos)
echo function_graph > /debug/tracing/current_tracer echo __do_fault > /debug/tracing/set_graph_function echo 0 > /debug/tracing/tracing_enabled ... echo 1 > /debug/tracing/tracing_enabled cat /debug/tracing/tracer
Obs: a lista de exemplos vai aumentar em breve.
Referências
- ftrace - Function Tracer: linux-2.6/Documentation/trace/ftrace.txt
- Debugging the kernel using Ftrace - part 1: http://lwn.net/Articles/365835/
- Debugging the kernel using Ftrace - part 2: http://lwn.net/Articles/366796/
- Using the ftrace Utility for Tracing Latencies: http://www.redhat.com/docs/en-US/Red_Hat_Enterprise_MRG/1.0/html/Realtime_Tuning_Guide/sect-Realtime_Tuning_Guide-Realtime_Specific_Tuning-Using_the_ftrace_Utility_for_Tracing_Latencies.html
- Secrets of the Ftrace function tracer: http://lwn.net/Articles/370423/
Marcelo Veiga Neves < marcelo.veiga at gmail.com >
