栈trace(kprobe)

最近在看CPU/GPU的调用,于是就有了:

与事件跟踪器类似,不需要通过 current_tracer 激活。相反,通过 /sys/kernel/tracing/kprobe_events 添加探测点,并通过 /sys/kernel/tracing/events/kprobes//enable 启用它。

这是一个大佬的sh 脚本

#!/system/bin/sh

CURRENT_DIR=$(pwd)
TRACE_DIR='/sys/kernel/debug/tracing/'

set stack_fun=try_to_wake_up

cd ${TRACE_DIR}
echo nop > current_tracer
echo 0 > ./tracing_on && echo > ./trace
echo 'p trigger_load_balance' > ./kprobe_events
echo 1 > ./options/stacktrace
# or
# echo 'stacktrace' > ./events/kprobes/p_vfs_open_0/trigger

echo 1 > ./events/kprobes/p_trigger_load_balance_0/enable # turn on
echo 1 > ./tracing_on
sleep 10
#${SCRIPT_DIR}/run_test.sh && cp ./trace ${SCRIPT_DIR}/trace_test.log
echo 0 > ./tracing_on
echo 0 > ./events/kprobes/p_trigger_load_balance_0/enable # turn off

# echo '!stacktrace' >  ./events/kprobes/p_vfs_open_0/trigger
echo 0 > ./options/stacktrace
echo '-:p_trigger_load_balance_0' >> kprobe_events
cat /sys/kernel/tracing/trace_pipe >> /data/1.txt

cd /sys/kernel/tracing

echo 'p dev_pm_qos_update_request' > ./kprobe_events

echo 1 > ./options/stacktrace

echo 1 > events/kprobes/p_dev_pm_qos_update_request_0/enable

echo 1 > tracing_on

触发条件后取消设置

echo 0 > ./tracing_on

echo 0 > events/kprobes/p_dev_pm_qos_update_request_0/enable

echo '-:p_dev_pm_qos_update_request_0' >>kprobe_events 

输出结果:

 cat /sys/kernel/tracing/trace_pipe >> /data/1.txt 

栈trace(kprobe)_第1张图片

或者 清除所有探测点

echo > /sys/kernel/tracing/kprobe_events


网上另外的方法:

cd /sys/kernel/debug/tracing
# echo 'p:myopen1 do_sys_open' >> kprobe_events 
# echo 'p:myopen2 do_sys_open' >> kprobe_events 
**cd instances**
mkdir myopen1
mkdir myopen2
echo 1 > myopen1/events/kprobes/myopen1/enable
echo 1 > myopen2/events/kprobes/myopen2/enable
cat myopen1/trace_pipe
cat myopen2/trace_pipe


遇见的问题:

1.

栈trace(kprobe)_第2张图片

kprobe 无法清除。关机重启也不行,问过大佬说是我打得包有问题,有人做了usb的特殊处理

2.如何选定函数

打印栈,是对函数流程有一个大致的理解,然后选定函数。而且不要选择static 的函数

参考链接:

Kprobe-based Event Tracing — The Linux Kernel documentation

你可能感兴趣的:(linux)