ubuntu下ftrace使用手记

Trace Events (UserspaceAPI)

We’re in userspacenow, so you can leave the kernel source directory, but you do need to be rootand you may need to mount a filesystem. This is because some distributions(like Ubuntu) have an allergy to debugging (seriously, they even disable thingslike gdb -p).

Try and change intothe /sys/kernel/debug/tracingdirectory.

# cd /sys/kernel/debug/tracing

If this fails, you’llneed to mount the debugfsfilesystem and try again.

# mount -t debugfs none /sys/kernel/debug
# cd /sys/kernel/debug/tracing

With that done, weshould make sure tracing is enabled.

# cat tracing_enabled
1

If that’s 0, enableit:

# echo 1 > tracing_enabled

So we’ve enabledtracing, but what can we trace? Trace events are exposed in the eventssub-directory in two levels, the first is the subsystem and the second are thetrace events themselves. Since in my last blog post we were looking at tracing forks, it would begreat if there were trace events for doing just that. This is where it helps tobe able to git greparound the kernel source and recognise trace events, so you at least know theright subsystem name; and it turns out that the sched subsystem has exactly the events wewanted.

deathspank tracing# ls events/sched
enable                   sched_process_exit/  sched_stat_sleep/
filter                   sched_process_fork/  sched_stat_wait/
sched_kthread_stop/      sched_process_free/  sched_switch/
sched_kthread_stop_ret/  sched_process_wait/  sched_wait_task/
sched_migrate_task/      sched_stat_iowait/   sched_wakeup/
sched_pi_setprio/        sched_stat_runtime/  sched_wakeup_new/

sched_process_fork sounds exactly right, if you look at it, it’s a directory thatcontains four files: enable,filter, format and id. I bet you canguess how to enable fork tracing, but if not:

# cat events/sched/sched_process_fork/enable
0
# echo 1 > events/sched/sched_process_fork/enable

Pretty painless, sogo ahead and run a few things, and turn the tracing off again when you’re done.

# echo 0 > events/sched/sched_process_fork/enable

Now let’s look at theresult of our trace; recall that every trace event comes with a free printk() of formattedoutput? We can find the output from those in the top-level trace file.

# tracer: nop
#
#           TASK-PID    CPU#    TIMESTAMP  FUNCTION
#              | |       |          |         |
             zsh-2667  [001]  6658.716936: sched_process_fork: comm=zsh pid=2667 child_comm=zsh child_pid=2748
So for each processfork, we get the parent and child process ids along with the process name.Pretty much exactly what we want!
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值