Skip to content

Instantly share code, notes, and snippets.

@dzeban
Last active November 5, 2018 23:43
Show Gist options
  • Save dzeban/a19c711d6b6b1d72e594 to your computer and use it in GitHub Desktop.
Save dzeban/a19c711d6b6b1d72e594 to your computer and use it in GitHub Desktop.
jprobes example
/*
* Here's a sample kernel module showing the use of jprobes to dump
* the arguments of third-party network driver for Bercut ETN.
*
* For more information on theory of operation of jprobes, see
* Documentation/kprobes.txt
*
* Build and insert the kernel module as done in the kprobe example.
* You will see the trace data in /var/log/messages and on the
* console whenever do_fork() is invoked to create a new process.
* (Some messages may be suppressed if syslogd is configured to
* eliminate duplicate messages.)
*/
#define pr_fmt(fmt) "ETN JPROBE: %s:%d: " fmt, __FUNCTION__, __LINE__
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/kprobes.h>
static ssize_t trace_etn_write(struct file *filp, const char __user *buf,
size_t count, loff_t *f_pos)
{
pr_info("Writing %zu bytes at offset %lld\n", count, *f_pos);
/* Always end with a call to jprobe_return(). */
jprobe_return();
return 0;
}
static struct jprobe etn_write_jprobe = {
.entry = trace_etn_write,
.kp = {
.symbol_name = "etn_write",
},
};
static int __init jprobe_init(void)
{
int ret;
ret = register_jprobe(&etn_write_jprobe);
if (ret < 0) {
pr_err(KERN_INFO "register_jprobe failed, returned %d\n", ret);
return -1;
}
pr_info("Planted jprobe at %p, handler addr %p\n",
etn_write_jprobe.kp.addr, etn_write_jprobe.entry);
return 0;
}
static void __exit jprobe_exit(void)
{
unregister_jprobe(&etn_write_jprobe);
pr_info("jprobe at %p unregistered\n", etn_write_jprobe.kp.addr);
}
module_init(jprobe_init)
module_exit(jprobe_exit)
MODULE_LICENSE("GPL");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment