linux下信号跟踪:jprobe_signal

问题描述:在linux环境下,有时候进程会异常退出,这个时候可以用strace 跟踪进程的运行情况。如果是进程内部错误,则strace日志可以看到进程的异常错误点。但如果是被其它人或进程杀掉(kill), 则strace 日志里只能看到被杀信息:"+++ killed by SIGKILL +++"。 此时要找到“杀手”进程,就需要跟踪信号量,找出是谁发送了信号量给目标进程。

/*jprobe_signal.c, 基于jprobe,注册钩子函数,在信号发送的地方,根据需要,打印发送信号的进程,发送的信号量,信号接收的目标进程。

*/

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
#include <linux/sched.h>

static int g_pid=0; //-1=all process
static int g_sig=9; //SIGKILL, 0=all signal 
module_param_named(pid,g_pid,int,S_IRUGO|S_IWUSR);
module_param_named(signal,g_sig,int,S_IRUGO|S_IWUSR);
MODULE_PARM_DESC(pid, "The specified process PID,-1 Reprensent all processes. 0 Represent no processes be traced. default is 0");
MODULE_PARM_DESC(signal, "The signal that needs to be tracked,0 represent all signal. default is 9");

static int jsend_signal(int sig,struct siginfo *info,struct task_struct *t,int group)
{
        struct task_struct *parent=NULL;
        int prev_pid=0;
        if(((g_pid < 0) ||(t->pid==g_pid))&&((g_sig==0)||(sig==g_sig))){
                printk(KERN_INFO "jprobe_signal:PID %d name:%s send SIG %d to PID=%d,name=%s\n",current->pid,current->comm,sig,t->pid,t->comm);
                parent=current->parent;
                prev_pid=current->pid;
                while(parent&&(parent->pid >= 1)){
                        printk(KERN_INFO"jprobe_signal:PID %d create by Parent Pid:%d,name:%s\n",prev_pid,parent->pid,parent->comm);
                        prev_pid=parent->pid;
                        parent=parent->parent;
                }
        }
        /* Always end with a call to jprobe_return(). */
        jprobe_return();
        return 0;
}
static struct jprobe my_jprobe = {
        .entry                  = jsend_signal,
        .kp = {
                .symbol_name    = "send_signal",
        },
};

static int __init jprobe_init(void)
{
        int ret;

        ret = register_jprobe(&my_jprobe);
        if (ret < 0) {
                printk(KERN_INFO "register_jprobe failed, returned %d\n", ret);
                return -1;
        }
        printk(KERN_INFO "Planted jprobe at %p, handler addr %p\n",
               my_jprobe.kp.addr, my_jprobe.entry);
        return 0;
}

static void __exit jprobe_exit(void)
{
        unregister_jprobe(&my_jprobe);
        printk(KERN_INFO "jprobe at %p unregistered\n", my_jprobe.kp.addr);
}

module_init(jprobe_init)
module_exit(jprobe_exit)
MODULE_LICENSE("Dual BSD/GPL");

Makefile 文件:

ifneq ($(KERNELRELEASE),)
        obj-m := jprobe_signal.o
else
  KERNELDIR ?=/usr/src/$(shell uname -r)/
  PWD := $(shell pwd)
default:   
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules   
endif

.PHONY: clean
clean:   
    -rm -rf *.mod.c *.o *.order *.sym .j* .tmp*

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值