kprobe 内核模块
代码来自于linux内核sample/kprobe
kprobe_example.c
/*
* NOTE: This example is works on x86 and powerpc.
* Here's a sample kernel module showing the use of kprobes to dump a
* stack trace and selected registers when do_fork() is called.
*
* For more information on theory of operation of kprobes, see
* Documentation/kprobes.txt
*
* You will see the trace data in /var/log/messages and on the console
* whenever do_fork() is invoked to create a new process.
*/ #include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h> /* For each probe you need to allocate a kprobe structure */
static struct kprobe kp = {
.symbol_name = "do_fork",
}; /* kprobe pre_handler: called just before the probed instruction is executed */
static int handler_pre(struct kprobe *p, struct pt_regs *regs)
{
#ifdef CONFIG_X86
printk(KERN_INFO "pre_handler: p->addr = 0x%p, ip = %lx,"
" flags = 0x%lx\n",
p->addr, regs->ip, regs->flags);
#endif
#ifdef CONFIG_PPC
printk(KERN_INFO "pre_handler: p->addr = 0x%p, nip = 0x%lx,"
" msr = 0x%lx\n",
p->addr, regs->nip, regs->msr);
#endif
#ifdef CONFIG_MIPS
printk(KERN_INFO "pre_handler: p->addr = 0x%p, epc = 0x%lx,"
" status = 0x%lx\n",
p->addr, regs->cp0_epc, regs->cp0_status);
#endif /* A dump_stack() here will give a stack backtrace */
return ;
} /* kprobe post_handler: called after the probed instruction is executed */
static void handler_post(struct kprobe *p, struct pt_regs *regs,
unsigned long flags)
{
#ifdef CONFIG_X86
printk(KERN_INFO "post_handler: p->addr = 0x%p, flags = 0x%lx\n",
p->addr, regs->flags);
#endif
#ifdef CONFIG_PPC
printk(KERN_INFO "post_handler: p->addr = 0x%p, msr = 0x%lx\n",
p->addr, regs->msr);
#endif
#ifdef CONFIG_MIPS
printk(KERN_INFO "post_handler: p->addr = 0x%p, status = 0x%lx\n",
p->addr, regs->cp0_status);
#endif
} /*
* fault_handler: this is called if an exception is generated for any
* instruction within the pre- or post-handler, or when Kprobes
* single-steps the probed instruction.
*/
static int handler_fault(struct kprobe *p, struct pt_regs *regs, int trapnr)
{
printk(KERN_INFO "fault_handler: p->addr = 0x%p, trap #%dn",
p->addr, trapnr);
/* Return 0 because we don't handle the fault. */
return ;
} static int __init kprobe_init(void)
{
int ret;
kp.pre_handler = handler_pre;
kp.post_handler = handler_post;
kp.fault_handler = handler_fault; ret = register_kprobe(&kp);
if (ret < ) {
printk(KERN_INFO "register_kprobe failed, returned %d\n", ret);
return ret;
}
printk(KERN_INFO "Planted kprobe at %p\n", kp.addr);
return ;
} static void __exit kprobe_exit(void)
{
unregister_kprobe(&kp);
printk(KERN_INFO "kprobe at %p unregistered\n", kp.addr);
} module_init(kprobe_init)
module_exit(kprobe_exit)
MODULE_LICENSE("GPL");
makefile 如下:
obj-m := kprobe-example.o
KDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
rm -f *.mod.c *.ko *.o
su root
insmod kprobe-example.ko
cat /var/log/messages 查看消息
lsmod | grep krpobe
rm kprobe-example.ko
kprobe 内核模块的更多相关文章
- 哎呀,发现自己不会用模块的方式用kprobe啊,弱爆了
在内核外面编译模块,会报warning函数名undefined的错误,解决方法是把函数给export出来:EXPORT_SYMBOL 一直以来,用kprobe比较多的是kprobe event的用法, ...
- Linux 下的一个全新的性能测量和调式诊断工具 Systemtap,第 1 部分: kprobe
kprobe 的原理.编程接口.局限性和使用注意事项 本系列文章详细地介绍了一个Linux下的全新的调式.诊断和性能测量工具Systemtap和它所依赖的基础kprobe以及促使开发该工具的先驱DTr ...
- Linux kprobe调试技术使用
kprobe调试技术是为了便于跟踪内核函数执行状态所设计的一种轻量级内核调试技术. 利用kprobe技术,可以在内核绝大多数函数中动态插入探测点,收集调试状态所需信息而基本不影响原有执行流程. kpr ...
- Linux下 kprobe工具的使用
此处转载: 一.Kprobe简单介绍 kprobe是一个动态地收集调试和性能信息的工具,它从Dprobe项目派生而来,是一种非破坏性工具,用户用它差点儿能够跟踪不论什么函数或被运行的指令以及一些异步事 ...
- Linux内核模块开发基础【转】
本文转载自:http://blog.csdn.net/coding__madman/article/details/51298180 1. 什么是内核模块 内核模块具有以下两个特点:1. 模块本身并不 ...
- linux 使用/proc文件系统 实现用户空间与内核模块之间通信
项目中可能会用到用户态和内核模块之间进行通信的功能.想到linux系统本身很多通信都是通过/proc文件系统来的,比如修改网络中连接跟踪表连接数限制/proc/sys/net/netfilter/nf ...
- Linux内核模块设计
内核的设计有两种方式:单内核和微内核,两者各有优劣,关于两者的比较可以参见wiki.windowds和Solaris采用微内核结构. Linux内核采用单内核结构,设计比较简单,但单内核的理念是把所有 ...
- secure boot(安全启动)下为内核模块签名
上一篇随笔中提到了如何在secure boot下安装Nvidia显卡驱动 >>上一篇随笔 如果不需要安装Nvidia显卡驱动,而且要生成密钥,可以参考>> 这篇文章 这里假设生 ...
- 5.linux内核模块基础,内核模块学习
linux内核模块基础 一.定义 Linux 内核的整体结构非常庞大,其包含的组件也非常多,如何使用这些组件呢: 方法 1:把所有的组件都编译进内核文件,即:zImage 或 bzImage,但这样会 ...
随机推荐
- —教训深刻—SQL Server大约TempDB使用
场景现象 中午查询了流水,因未与业务人员沟通好.忘了删选条件,导致TempDB不能分配空间,SQL Server高负载执行. 错误分析 我们来看看错误日志: 再来看看TempDB自增长记录: 事件 逻 ...
- Java 审查基调
1.& 与 &&的差别 两个都有逻辑与的功能. 可是所不同的是.当&两边的表达式不是boolean类型的时候,&具有位与的功能:&&是短路与,当 ...
- SQL Server系统数据库备份最佳实践
原文:SQL Server系统数据库备份最佳实践 首先了解主要的系统数据库: 系统数据库 master 包含登录信息和其他数据库的核心信息 msdb 存储作业.操作员.警报.备份还原历史.数据库邮件信 ...
- 于Eclipse传导C/C++配置方法开发(20140721新)
Eclipse 它是一个开源.基于Java可扩展的开发平台. 在其自己的.它只是一个框架和一组服务.对于通过插件组件构建开发环境. --从百度百科的短语. 简单的说Eclipse 是免费的开源的Jav ...
- UVA714- Copying Books(最大最小化)
意甲冠军:k手稿的部分成m部分,使每一个和最小 思路:典型最大值最小化问题,使用贪心+二分. 贪心的是每次尽量将元素往右边划分,二分查找最小的x满足m个连续的子序列和S(i)都不超过x. 由于输出的原 ...
- Ubuntu文件的复制、移动和删除命令
先说说cp复制命令 该命令的功能是将给出的文件或文件夹复制到还有一文件或文件夹中,同MSDOS下的copy命令一样,功能十分强大. 语法: cp [选项] 源文件或文件夹 目标文件或文件夹 说明:该命 ...
- ExecutorService invokeAll 实例(转)
10个班级,每个班级20名学生,在指定的时间内查询每个班级学生的集合. package cn.com.ld.study.thread; import java.util.ArrayList; impo ...
- FutureTask解析(转)
站在使用者的角度,future是一个经常在多线程环境下使用的Runnable,使用它的好处有两个:1. 线程执行结果带有返回值2. 提供了一个线程超时的功能,超过超时时间抛出异常后返回. 那,怎么实现 ...
- 2款不同样式的CSS3 Loading加载动画 附源码
原文:2款不同样式的CSS3 Loading加载动画 附源码 我们经常看到的Loading加载很多都是转圈圈的那种,今天我们来换一种有创意的CSS3 Loading加载动画,一种是声波形状的动画,另一 ...
- hdu4453 Looploop 2012年杭州现场赛 Splay
题意:维护一个圈,实现六个功能,给某位置起的一些数增加某值,反转某一段数,添加删除某些数,移动当前所指的位置, 简单的splay,把圈拆成链,对于每种操作,处理一下. #define inf 0x3f ...