内核uprobes使用介绍

本文介绍了内核中的uprobe事件追踪工具,它是kprobe的用户态版本,用于在用户空间代码中设置探针。内容包括uprobe的启用、命令格式、如何查看事件监控文件及设置和清除检测点的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

uprobe 事件tracer工具是在内核3.5开发期间何入内核主线版本的,虽然uprobe已经存在很久了。uprobe是和kprobe类似的调试方法。编译内核时通过打开CONFIG_UPROBE_EVENT=y来时能该特性。和kprobe类似,使用时不需要通过current_tracer来激活,而是检测点通过/sys/kernel/debug/tracing/uprobe_events设置,通过/sys/kernel/debug/tracing/events/uprobes/<EVENT>/enabled来使能。

然而,和kprobe不同的是,使用时需要用户自己计算探测点在用户态文件中的偏移,可以通过nm等工具,这还是有点麻烦的。

命令格式

  p[:[GRP/]EVENT] PATH:SYMBOL[+offs] [FETCHARGS]    : Set a probe

 GRP        : Group name. If omitted, use "uprobes" for it.
 EVENT        : Event name. If omitted, the event name is generated
          based on SYMBOL+offs.
 PATH        : path to an executable or a library.
 SYMBOL[+offs]    : Symbol+offset where the probe is inserted.

 FETCHARGS    : Arguments. Each probe can have up to 128 args.
  %REG        : Fetch register REG

查看事件监控文件

可以通过/sys/kernel/debug/tracing/uprobe_profile来查看某一检测事件命中的总数和没有命中的总数。第一列是事件名称,第二列是事件命中的次数,第三列是事件miss-hits的次数。

使用举例

如下定义一个新的检测事件:

    echo 'p: /bin/bash:0x4245c0' > /sys/kernel/debug/tracing/uprobe_events

这样就在可执行文件/bin/bash的偏移0x4245c0处设置了检测点。

要清空所有的检测点,如下:

    echo > /sys/kernel/debug/tracing/uprobe_events


下面的例子给出了获得某可执行文件符号地址的方法:

    # cd /sys/kernel/debug/tracing/
    # cat /proc/`pgrep  bash`/maps | grep /bin/bash | grep r-xp
   00400000-004e1000 r-xp 00000000 08:01 786439                             /bin/bash
    # objdump -T /bin/zsh | grep -w free
    00000000004ab500 g    DF .text    0000000000000009  Base        free

0x4ab500是可执行文件/bin/zsh中哦给你free函数的偏移,可以看出/bin/bash的加载地址是0x00400000,因此设置free处为检测点的命令如下:

 echo 'p /bin/bash:0x4ab500 %ip %ax' > uprobe_events

可以通过uprobe_events来查看注册的事件:

    # cat uprobe_events
    p:uprobes/p_bash_0x4ab500 /bin/bash:0x00000000004ab500 arg1=%ip arg2=%ax


在正确的注册后,每个检测点事件都是禁止的,要检测这个事件,需要手动去激活它:

# echo 1 > events/uprobes/enable

在程序执行了一段事件后,禁止它,然后可以查看监控到的事件:

    # sleep 20
    # echo 0 > events/uprobes/enable

    # cat trace

    # tracer: nop
    #
    #           TASK-PID    CPU#    TIMESTAMP  FUNCTION
    #              | |       |          |         |
                 zsh-24842 [006] 258544.995456: p_bash_0x46420: (0x446420) arg1=446421 arg2=79
                 zsh-24842 [007] 258545.000270: p_bash_0x46420: (0x446420) arg1=446421 arg2=79
                 zsh-24842 [002] 258545.043929: p_bash_0x46420: (0x446420) arg1=446421 arg2=79
                 zsh-24842 [004] 258547.046129: p_bash_0x46420: (0x446420) arg1=446421 arg2=79

BPF and related observability tools give software professionals unprecedented visibility into software, helping them analyze operating system and application performance, troubleshoot code, and strengthen security. BPF Performance Tools: Linux System and Application Observability is the industry’s most comprehensive guide to using these tools for observability. Brendan Gregg, author of the industry’s definitive guide to system performance, introduces powerful new methods and tools for doing analysis that leads to more robust, reliable, and safer code. This authoritative guide: Explores a wide spectrum of software and hardware targets Thoroughly covers open source BPF tools from the Linux Foundation iovisor project’s bcc and bpftrace repositories Summarizes performance engineering and kernel internals you need to understand Provides and discusses 150+ bpftrace tools, including 80 written specifically for this book: tools you can run as-is, without programming — or customize and develop further, using diverse interfaces and the bpftrace front-end You’ll learn how to use BPF (eBPF) tracing tools to analyze CPUs, memory, disks, file systems, networking, languages, applications, containers, hypervisors, security, and the Linux kernel. You’ll move from basic to advanced tools and techniques, producing new metrics, stack traces, custom latency histograms, and more. It’s like having a superpower: with Gregg’s guidance and tools, you can analyze virtually everything that impacts system performance, so you can improve virtually any Linux operating system or application.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值