近来学习了下systemtap,忽然想了个问题,怎么在linux下面监控一个用户对那些文件进行操作?准备用它来学习一下内核。
文件操作主要是open,read、write,是不是监控这3个系统调用就可以了?说干就干,上代码。
下面是程序filemon.stp
%{
#include <linux/file.h>
#include <linux/dcache.h>
#include <linux/mount.h>
#include <linux/fs.h>
#include <linux/fdtable.h>
#include <linux/err.h>
%}
probe kernel.function("sys_open"){
if(uid()==$1) /*监控特定用户,$1为命令行传入的参数,数字型格式*/
printf("(%d)execname:(%s)-----openfile:(%s)\n",pid(),execname(),user_string($filename))
}
function fd_to_name:string(fd:long)
%{
struct file *file;
struct files_struct *files;
struct inode *inode;
struct task_struct *task;
const unsigned char *name = NULL;
char *buffer;
buffer = (char*) kmalloc(PATH_MAX,GFP_KERNEL);
if(!buffer)
return ;
task_lock(current);
files = current->files;
if (files)
atomic_inc(&files->count);
task_unlock(current);
if (!files)
return ;
spin_lock(&files->file_lock);
file = fcheck_files(files, (int)STAP_ARG_fd); /*STAP_ARG_fd为systemtap传给嵌入c的参数*/
if (!file)
goto out_unlock;
name = d_path(&(file->f_path), buffer,PATH_MAX);
if (IS_ERR(name)) {
goto out_unlock;
printk("d_path did not work!\n");
}
strlcpy (THIS->__retvalue, name, PATH_MAX);
kfree(buffer);
out_unlock:
spin_unlock(&files->file_lock);
atomic_dec(&files->count);
%}
probe kernel.function("sys_read"){
if(uid()==$1) /*监控特定用户*/
printf("(%d)readfile:%s\n",pid(),fd_to_name($fd));
}
probe kernel.function("sys_write"){
if(uid()==$1)
printf("(%d)writefile:%s\n",pid(),fd_to_name($fd));
}
简单说明:上面这一段嵌入c的代码主要是用d_path将文件描述符fd转换为路径名
用stap -g --vp filemon.stp 501 运行一下,结果如下图
总结一下:要想用好systemtap,得对内核非常熟悉。
相关推荐
使用动态跟踪技术SystemTap监控Oracle和mysql性能——吕海波@美创科技.pdf
使用动态跟踪技术SystemTap监控Oracle和mysql性能——吕海波@美创科技
nginx-systemtap-toolkit 是一个基于 SystemTap 的实时 Nginx 分析和诊断工具。
本文主要介绍在ubuntu平台 + 自定义内核上如何安装systemtap工具包及解决编译加载运行过程中的一些问题,如何利用systemtap工具监测分析内核函数,内核模块及用户态程序。
systemtap是什么?如何通过systemtap对应用程序内核程序进行调试?如何编写运行systemtap脚本?
SystemTap_Beginners_Guide
很全面详细的讲解了systemtap脚本编写基础语法,很好的入门文档
systemtap script examples , cross compile from x86 to arm.
systemTap英文文档集合.7z
SystemTap_Beginners_Guide.pdf
systemtap的入门指南,包含tracing/analysis/tapsets等,入门必备
systemtap交叉编译,运行于android平台。OMAP项目的实例,很有借鉴价值。
systemtap_tutorial
SystemTap是一个全新的工具,但已经表现出了强大的功能和广泛的适用性。...随着越来越多用户的体验,越来越多的bug会被报告和修正,越来越多的新功能会被添加,SystemTap也会变得 越来越稳定和完善。
Language Reference.pdf // 详细说明systemtap脚本的语法规则 Tapset Refernce Manual.pdf // 脚本库,详细说明每个function的功能 tutorial.pdf // systemtap 脚本初级教材,介绍脚本的一些用途 Beginners_Guide....
systemtap使用说明,英文版,包括简单用例,和实现方式,探测实现方式
linux 调试神器,linux 调试神器,linux 调试神器,linux 调试神器
学习Linux性能优化必不可少的工具sytemtap。想学习的同学可以下载。
用systemtap研究内核 用systemtap研究内核 用systemtap研究内核 用systemtap研究内核 用systemtap研究内核