Linux Ftrace 使用
Ftrace相關的文章
如何利用ftrace精確跟蹤特定進程調度信息
1、Ftrace 是什么東西?
Ftrace是一個直接內置在Linux內核中的跟蹤工具。許多發行版在最近的發行版中已經啟用了各種各樣的Ftrace配置。Ftrace給Linux帶來的好處之一是能夠看到內核中發生了什么。因此,這使得發現問題區域或簡單地跟蹤奇怪的bug變得更容易管理。
上面那段話是直接翻譯過來的,原文如下,我比較推薦大家直接看英文
Ftrace is a tracing utility built directly into the Linux kernel. Many distributions already have various configurations of Ftrace enabled in their most recent releases. One of the benefits that Ftrace brings to Linux is the ability to see what is happening inside the kernel. As such, this makes finding problem areas or simply tracking down that strange bug more manageable.
2、怎么使用Ftrace
我的實驗平臺是MT8167、Linux4.4內核
2.1、配置內核宏
weiqifa@bsp-ubuntu1804:~/is10-sdk$?git?diff?kernel-4.4/ diff?--git?a/kernel-4.4/arch/arm/configs/xxx_defconfig? b/kernel-4.4/arch/arm/configs/xxx_defconfig index?8da5210003..a88e53253f?100755 ---?a/kernel-4.4/arch/arm/configs/xxx_defconfig +++?b/kernel-4.4/arch/arm/configs/xxx_defconfig @@?-400,3?+400,7?@@?CONFIG_USB_RTL8152=yCONFIG_USB_NET_DRIVERS=yCONFIG_GPIO_CONTROL=y#CONFIG_POGO_PIN=y +CONFIG_FUNCTION_TRACER=y +CONFIG_FUNCTION_GRAPH_TRACER=y +CONFIG_STACK_TRACER=y +CONFIG_DYNAMIC_FTRACE=y2.2、燒錄boot.img 重新開機
2.3、查看是否生效
進入ftrace 目錄
xxx:/?#?cd?/sys/kernel/debug/tracing xxx:/sys/kernel/debug/tracing?#?ls README?????????????????????enabled_functions?saved_cmdlines??????set_graph_function?trace_marker available_events???????????events????????????saved_cmdlines_size?set_graph_notrace??trace_options available_filter_functions?free_buffer???????saved_tgids?????????snapshot???????????trace_pipe available_tracers??????????instances?????????set_event???????????stack_max_size?????tracing_cpumask buffer_size_kb?????????????max_graph_depth???set_event_pid???????stack_trace????????tracing_max_latency buffer_total_size_kb???????options???????????set_ftrace_filter???stack_trace_filter?tracing_on current_tracer?????????????per_cpu???????????set_ftrace_notrace??trace??????????????tracing_thresh dyn_ftrace_total_info??????printk_formats????set_ftrace_pid??????trace_clock xxx:/sys/kernel/debug/tracing?#查看內核支持的跟蹤器列表
xxx:/sys/kernel/debug/tracing?#?cat?available_tracers function_graph?function?nop xxx:/sys/kernel/debug/tracing?#Tracer有很多種,主要幾大類:
函數類:function, function_graph, stack
延時類:irqsoff, preemptoff, preemptirqsoff, wakeup, wakeup_rt, waktup_dl
其他類:nop, mmiotrace, blk
使能function_graph跟蹤器
echo?function_graph?>?current_tracer查看當前的跟蹤器
xxx:/sys/kernel/debug/tracing?#?cat?current_tracer function_graph xxx:/sys/kernel/debug/tracing?#使能ftrace 功能
xxx:/sys/kernel/debug/tracing?#?echo?1?>?tracing_on查看ftrace輸出
1|Knowin?inSight10:/sys/kernel/debug/tracing?#?cat?trace?|?head?-40 #?tracer:?function_graph # #?CPU??DURATION??????????????????FUNCTION?CALLS #?|?????|???|?????????????????????|???|???|???|2)???4.000?us????|??????????}?/*?path_init?*/2)???????????????|??????????link_path_walk()?{2)???????????????|????????????inode_permission2()?{2)???????????????|??????????????__inode_permission2()?{2)???????????????|????????????????generic_permission()?{2)???0.384?us????|??????????????????in_group_p();2)???2.154?us????|????????????????}2)???????????????|????????????????security_inode_permission()?{2)???????????????|??????????????????selinux_inode_permission()?{2)???0.231?us????|????????????????????__rcu_read_lock();2)???0.308?us????|????????????????????avc_lookup();2)???0.231?us????|????????????????????__rcu_read_unlock();2)???5.923?us????|??????????????????}2)???7.846?us????|????????????????}2)?+?13.538?us???|??????????????}2)?+?15.308?us???|????????????}2)???????????????|????????????walk_component()?{2)???????????????|??????????????lookup_fast()?{2)???0.307?us????|????????????????__d_lookup_rcu();2)???0.307?us????|????????????????__lookup_mnt();2)???4.231?us????|??????????????}2)???6.077?us????|????????????}2)???????????????|????????????inode_permission2()?{2)???????????????|??????????????__inode_permission2()?{3)???3.923?us????|??????????????????????}?/*?down_trylock?*/2)???????????????|????????????????generic_permission()?{3)???6.538?us????|????????????????????}?/*?console_trylock?*/2)???0.308?us????|??????????????????in_group_p();3)???????????????|????????????????????console_unlock()?{3)???0.231?us????|??????????????????????_raw_spin_lock_irqsave();2)???2.231?us????|????????????????}2)???????????????|????????????????security_inode_permission()?{2)???????????????|??????????????????selinux_inode_permission()?{3)???0.308?us????|??????????????????????_raw_spin_unlock_irqrestore();2)???0.231?us????|????????????????????__rcu_read_lock();3)???0.231?us????|??????????????????????_raw_spin_lock_irqsave(); Knowin?inSight10:/sys/kernel/debug/tracing?#3、Ftrace 其他使用特點
直接cat trace 文件,內容太多了,我們可以通過設置filter來設置過濾函數。
為了方便使用,set_ftrace_filter 文件還支持簡單格式的通配符。
value* 選擇所有名字以 value字串開頭的函數
*value* 選擇所有名字中包含 value字串的函數
*value 選擇所有名字以 value字串結尾的函數
設置filter
xxx:/sys/kernel/debug/tracing?#?echo?raw*?>?set_ftrace_filter xxx:/sys/kernel/debug/tracing?#再查看trace文件
xxx:/sys/kernel/debug/tracing?#?cat?trace #?tracer:?function_graph # #?CPU??DURATION??????????????????FUNCTION?CALLS #?|?????|???|?????????????????????|???|???|???|2)???1.693?us????|??raw_local_deliver();2)???0.308?us????|??raw_local_deliver();2)???1.000?us????|??raw_notifier_call_chain();2)???2.077?us????|??raw_notifier_call_chain();2)???1.923?us????|??raw_local_deliver();------------------------------------------2)??ntloop--1393??=>????<...>-16------------------------------------------2)???0.308?us????|??raw_local_deliver();------------------------------------------2)????<...>-16????=>????<idle>-0------------------------------------------2)???0.307?us????|??raw_notifier_call_chain();2)???0.846?us????|??raw_notifier_call_chain();2)???0.385?us????|??raw_notifier_call_chain();------------------------------------------2)????<idle>-0????=>??droid.b-4348------------------------------------------2)???1.000?us????|??raw_notifier_call_chain();2)???1.384?us????|??raw_notifier_call_chain();------------------------------------------2)??droid.b-4348??=>??Binder:-524------------------------------------------2)???0.923?us????|??raw_notifier_call_chain();2)???1.077?us????|??raw_notifier_call_chain();------------------------------------------2)??Binder:-524???=>??droid.b-4348------------------------------------------2)???1.000?us????|??raw_notifier_call_chain();------------------------------------------2)??droid.b-4348??=>??Binder:-524------------------------------------------2)???1.231?us????|??raw_notifier_call_chain();------------------------------------------2)??Binder:-524???=>??droid.b-4348------------------------------------------2)???1.077?us????|??raw_notifier_call_chain();2)???1.692?us????|??raw_notifier_call_chain();2)???2.000?us????|??raw_notifier_call_chain();2)???1.000?us????|??raw_notifier_call_chain();2)???0.923?us????|??raw_notifier_call_chain();2)???0.462?us????|??raw_notifier_call_chain();2)???1.000?us????|??raw_notifier_call_chain();2)???0.846?us????|??raw_notifier_call_chain();查看中間字符串
xxx:/sys/kernel/debug/tracing?#?echo?*touch*?>?set_ftrace_filter查看trace
xxx:/sys/kernel/debug/tracing?#?cat?trace?|head?-40 #?tracer:?function_graph # #?CPU??DURATION??????????????????FUNCTION?CALLS #?|?????|???|?????????????????????|???|???|???|0)???????????????|??/*?6?[cpu_loading]?not_reset_cpu_loading?*/0)???????????????|??/*?6?[cpu_loading]?update?cpu_loading?*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[0].time:933317139?cur_wall_time[0].time:1008009932*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[1].time:936071150?cur_wall_time[1].time:1008009942*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[2].time:917091836?cur_wall_time[2].time:1008009946*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[3].time:890438730?cur_wall_time[3].time:1008009950*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[0].time:933317139?cur_wall_time[0].time:1008009932*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[1].time:936071150?cur_wall_time[1].time:1008009942*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[2].time:917091836?cur_wall_time[2].time:1008009946*/0)???????????????|??/*?6?[cpu_loading]?cur_idle_time[3].time:890438730?cur_wall_time[3].time:1008009950*/0)???????????????|??/*?6?[cpu_loading]?tmp_cpu_loading:2?prev_cpu_loading:1?previous?state:3*/0)???????????????|??/*?6?[cpu_loading]?sent?uevent?success:lower=2?*/0)???????????????|??/*?6?[cpu_loading]?current?state:3*/0)???????????????|??/*?6?[cpu_loading]?enable?timer?*/1)???1.462?us????|??touch_atime();1)???0.616?us????|??touch_atime();1)???0.385?us????|??touch_atime();1)???0.538?us????|??touch_atime();1)???0.539?us????|??touch_atime();1)???1.461?us????|??touch_atime();1)???0.308?us????|??touch_atime();1)???0.385?us????|??touch_atime();1)???0.385?us????|??touch_atime();1)???2.000?us????|??touch_atime();1)???0.923?us????|??touch_atime();1)???0.615?us????|??touch_atime(); xxx:/sys/kernel/debug/tracing?#通過該文件還可以指定屬于特定模塊的函數,這要用到 mod 指令。指定模塊的格式為:
echo?':mod:[module_name]'?>?set_ftrace_filter查看對應的內核模塊
xxx:/sys/kernel/debug/tracing?#?cat?/proc/devices Character?devices:1?mem2?pty3?ttyp4?ttyS5?/dev/tty5?/dev/console5?/dev/ptmx10?misc13?input14?sound29?fb使用過濾器只針對某個模塊進行跟蹤
xxx:/sys/kernel/debug/tracing?#?echo?':mod:sound'?>?set_ftrace_filter 1|xxx:/sys/kernel/debug/tracing?#需要注意的是,這三種形式不能組合使用,比如“beginmiddleend”實際的效果與“begin”相同。另外,使用通配符表達式時,需要用單引號將其括起來,如果使用雙引號,shell 可能會對字符‘ * ’進行擴展,這樣最終跟蹤的對象可能與目標函數不一樣。
4.參考資料
[1]、 https://lwn.net/Articles/365835/
[2]、https://www.kernel.org/doc/Documentation/trace/ftrace.txt
推薦閱讀:
專輯|Linux文章匯總
專輯|程序人生
專輯|C語言
我的知識小密圈
總結
以上是生活随笔為你收集整理的Linux Ftrace 使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 跟韦老师聊了一个深夜
- 下一篇: Linux-C编程 / 多线程 / 如何