本章的我们来学习uprobe ,顾名思义,相对于内核函数/地址的监控,主要用于用户态函数/地址的监控。听起来是不是有点神奇,内核怎么监控用户态函数的调用呢?本章的内容包括:

  • 如何使用uprobe
  • 内核是如何通过uprobe监控用户态的调用,其原理是如何的

1 如何使用uprobe

站在用户视角,我们先看个简单的例子,假设有这么个一个用户程序:

// test.c
#include <stdio.h>
void foo() {
printf("hello, uprobe!\n");
}
int main() {
foo();
return 0;
}

编译好之后,查看某个符号的地址,然后告诉内核我要监控这个地址的调用:

#gcc test.c -o test
#readelf -s test | grep foo
56: 000000000000115a 19 FUNC GLOBAL DEFAULT 14 foo
#echo 1 > /sys/kernel/debug/tracing/events/uprobes/p_test_0x115a/enable
#echo 1 > /sys/kernel/debug/tracing/events/uprobes/p_test_0x115a/enable
#echo 1 > /sys/kernel/debug/tracing/tracing_on

然后运行用户程序并检查内核的监控返回:

$ ./test && ./test
hello, uprobe!
hello, uprobe!

对于内核如何使用uprobe,请参考内核文档uprobetracer.html,其使用基本跟kprobe类似

2 实现原理

上面的接口是基于 tracefs,即读写文件的方式去与内核交互实现 uprobe 监控。

其中写入 uprobe_events 时会经过一系列内核调用,最终会调用到create_or_delete_trace_uprobe

对于__trace_uprobe_create跟使用kprobe类似,也是大家使用的三板斧

  • alloc_trace_uprobe:分配 uprobe 结构体

  • **register_trace_uprobe:**注册 uprobe:

  • regiseter_uprobe_event: 将 probe 添加到全局列表中,并创建对应的 uprobe debugfs 目录,即上文示例中的 p_test_0x115a

对于uprobe其实整个流程跟kprobe基本类似,我们重点关注于uprobe_register的函数做了些什么

当已经注册了 uprobe 的 ELF 程序被执行时,可执行文件会被 mmap 映射到进程的地址空间,同时内核会将该进程虚拟地址空间中对应的 uprobe 地址替换成断点指令。

与 kprobe 类似,我们可以在触发 uprobe 时候根据对应寄存器去提取当前执行的上下文信息,比如函数的调用参数等。同时 uprobe 也有类似的同族: uretprobe。

指定位置上的指令,头部修改为软件中断指令(同时原指令存档他处):

  1. 当执行到该位置时,触发软件中断,陷入内核
  2. 在内核,执行以 注入的 Handler
  3. 单步执行原指令
  4. 修正寄存器和栈,回到原有指令流

3 uprobe内核模块验证

我们以ubuntu为试验环境,使用uprobe一般都是编写内核驱动,在模块中定义uprobe_consumer ,然后调用uprobe的API(uprobe_register)来进行注册uprobe

#include <linux/module.h>
#include <linux/ptrace.h>
#include <linux/uprobes.h>
#include <linux/namei.h>
#include <linux/moduleparam.h> MODULE_AUTHOR("john doe");
MODULE_LICENSE("GPL v2"); static char *filename;
module_param(filename, charp, S_IRUGO); static long offset;
module_param(offset, long, S_IRUGO); static int handler_pre(struct uprobe_consumer *self, struct pt_regs *regs){
pr_info("handler: arg0 = %d arg1 =%d \n", (int)regs->di, (int)regs->si);
return 0;
} static int handler_ret(struct uprobe_consumer *self,
unsigned long func,
struct pt_regs *regs){
pr_info("ret_handler ret = %d \n", (int)regs->ax);
return 0;
} static struct uprobe_consumer uc = {
.handler = handler_pre,
.ret_handler = handler_ret,
}; static struct inode *inode; static int __init uprobe_init(void) {
struct path path;
int ret; ret = kern_path(filename, LOOKUP_FOLLOW, &path);
if (ret < 0) {
pr_err("kern_path failed, returned %d\n", ret);
return ret;
} inode = igrab(path.dentry->d_inode);
path_put(&path); ret = uprobe_register(inode, offset, &uc);
if (ret < 0) {
pr_err("register_uprobe failed, returned %d\n", ret);
return ret;
} return 0;
} static void __exit uprobe_exit(void) {
uprobe_unregister(inode, offset, &uc);
} module_init(uprobe_init);
module_exit(uprobe_exit);

此外,没有像 Kprobes 那样提供 uretprobe_register,如果 ret_handler 设置为值,则设置uretprobe,生成Makefile

obj-m := hello-uprobe-world.o
KDIR := /lib/modules/$(shell uname -r)/build
VERBOSE = 0 all:
$(MAKE) -C $(KDIR) M=$(PWD) KBUILD_VERBOSE=$(VERBOSE) CONFIG_DEBUG_INFO=y modules
clean:
rm -f *.o *.ko *.mod.c Module.symvers modules.order

编译生成ko文件:

准备要跟踪的程序:

#include <stdio.h>

int add(int a, int b) {
return a + b;
} int main(void) {
add(1, 2);
}

安装ko文件,并执行该文件,我们跟踪add函数,其offset可以通过获取

4 uprobe_event验证

uprobe_events 是一种无需创建内核模块即可使用 Uprobe 的机制。 您可以在与 Ftrace 相同的界面中动态创建探测

root@rlk:/sys/kernel/tracing# echo 'p:sample_uprobe /root/Make/main:0x114a %di %si' >  /sys/kernel/debug/tracing/uprobe_events
root@rlk:/sys/kernel/tracing# echo 'r:sample_uretprobe /root/Make/main:0x114a %ax' >> /sys/kernel/debug/tracing/uprobe_events
root@rlk:/sys/kernel/tracing# echo 1 > /sys/kernel/tracing/events/uprobes/sample_uprobe/enable
root@rlk:/sys/kernel/tracing# echo 1 > /sys/kernel/tracing/events/uprobes/sample_uretprobe/enable

5 perf_event_open

perf_event_open系统调用将 BPF 程序附加到 uprobe 事件。 直接编写 BPF 程序可能比较痛苦,因此需要通过 bpftrace 来使用它。

bpftrace -e ‘uretprobe:/root/work/uprobe/main:add {printf(“%d\n”, retval); exit(); }’

检查perf_event_open的使用方式

uprobe的更多相关文章

  1. Python脚本传參和Python中调用mysqldump

    Python脚本传參和Python中调用mysqldump<pre name="code" class="python">#coding=utf-8 ...

  2. Debug Hacks中文版——深入调试的技术和工具

    关键词:gdb.strace.kprobe.uprobe.objdump.meminfo.valgrind.backtrace等. <Debugs Hacks中文版——深入调试的技术和工具> ...

  3. gdb 调试入门,大牛写的高质量指南

    引用自:http://blog.jobbole.com/107759/ gdb 调试 ncurses 全过程: 发现网上的“gdb 示例”只有命令而没有对应的输出,我有点不满意.gdb 是 GNU 调 ...

  4. BPF+XDP比较全的资料都在这里

    Dive into BPF: a list of reading material Sep 1, 2016 • Quentin Monnet◀Table of contents What is BPF ...

  5. systemtap 2.8 news

    * What's new in version 2.8, 2015-06-17 - SystemTap has improved support for probing golang programs ...

  6. linux工具大全

    Linux Performance hi-res: observability + static + perf-tools/bcc (svg)slides: observabilityslides: ...

  7. 再看perf是如何通过dwarf处理栈帧的

    从结构体stack_dump入手, util/unwind-libunwind-local.c 中有函数access_mem #0 access_mem (as=0x1f65bd0, addr=140 ...

  8. gdb 调试 ncurses 全过程:

    转载地址: http://blog.jobbole.com/107759/ gdb 调试 ncurses 全过程: 发现网上的“gdb 示例”只有命令而没有对应的输出,我有点不满意.gdb 是 GNU ...

  9. Linux内核调试的方式以及工具集锦【转】

    转自:https://blog.csdn.net/gatieme/article/details/68948080 版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原 ...

  10. Linux性能优化实战学习笔记:第五十讲

    一.上节回顾 上一节,我以 ksoftirqd CPU 使用率高的问题为例,带你一起学习了内核线程 CPU 使用率高时的分析方法.先简单回顾一下. 当碰到内核线程的资源使用异常时,很多常用的进程级性能 ...

随机推荐

  1. WordPress基础之主题和插件安装

    本篇文章学习WordPress如何安装主题.插件.同时推荐几个我常用的主题.插件及其设置方法. WordPress有海量的主题和插件,有付费的,也有免费的.每个主题都有自己的优缺点,当然,你可以在WP ...

  2. 机器学习:详解是否要使用端到端的深度学习?(Whether to use end-to-end learning?)

    详解是否要使用端到端的深度学习? 假设正在搭建一个机器学习系统,要决定是否使用端对端方法,来看看端到端深度学习的一些优缺点,这样就可以根据一些准则,判断的应用程序是否有希望使用端到端方法. 这里是应用 ...

  3. Jmeter函数助手27-urlencode

    urlencode函数用于将字符串进行application/x-www-form-urlencoded编码格式化. String to encode in URL encoded chars:填入字 ...

  4. Jmeter大小断言

    Jmeter大小断言是用来判断返回的消息体大小的,组件路径[HTTP请求右键添加->断言->大小断言] 我们来了解一下大小断言组件里面包含什么内容 1.Apply to: Main sam ...

  5. 10、Git之国内项目托管平台(Gitee码云)

    10.1.简介 众所周知,GitHub 服务器在国外,如果网络不好的话,严重影响使用体验,甚至会出现登录不上的情况. 针对这个情况,可以使用国内的项目托管平台-- Gitee 码云,来替代 Githu ...

  6. Ubuntu系统下python模块graphviz运行报错:graphviz.backend.execute.ExecutableNotFound: failed to execute PosixPath(‘dot‘)

    代码中需要运行python模块graphviz,安装: pip install graphviz 运行后报错: graphviz.backend.execute.ExecutableNotFound: ...

  7. ubuntu23.04/22.04下安装docker engine

    官方网址: https://docs.docker.com/engine/install/ubuntu/ 2023年12月1日更新 -- Ubuntu 23.04 # Add Docker's off ...

  8. DebugView使用

    操作说明 要知道怎么操作debugview,首先得下载下来.https://docs.microsoft.com/en-us/sysinternals/downloads/debugview 配置过滤 ...

  9. spring之事务详解

    1.背景 该博客要解决的重要问题如下: spring的3种安全性问题,4种事务特性,5种隔离级别,7种传播行为 spring的3种安全性问题,4种事务特性,5种隔离级别,7种传播行为 spring事务 ...

  10. vue中使用better-scroll

    1.创建vue-cli3项目   指令 vue create 项目名 2.要想使用better-scroll 需要先引入 better-scroll的插件 这里采用 npm的方式    指令 npm ...