1430 lines · 32.6 KB · C
RawDownload
// SPDX-License-Identifier: GPL-2.0/* * uprobes-based tracing events * * Copyright (C) IBM Corporation, 2010-2012 * Author:	Srikar Dronamraju <srikar@linux.vnet.ibm.com> */#define pr_fmt(fmt)	"trace_uprobe: " fmt #include <linux/ctype.h>#include <linux/module.h>#include <linux/uaccess.h>#include <linux/uprobes.h>#include <linux/namei.h>#include <linux/string.h>#include <linux/rculist.h> #include "trace_dynevent.h"#include "trace_probe.h"#include "trace_probe_tmpl.h" #define UPROBE_EVENT_SYSTEM	"uprobes" struct uprobe_trace_entry_head {	struct trace_entry	ent;	unsigned long		vaddr[];}; #define SIZEOF_TRACE_ENTRY(is_return)			\	(sizeof(struct uprobe_trace_entry_head) +	\	 sizeof(unsigned long) * (is_return ? 2 : 1)) #define DATAOF_TRACE_ENTRY(entry, is_return)		\	((void*)(entry) + SIZEOF_TRACE_ENTRY(is_return)) struct trace_uprobe_filter {	rwlock_t		rwlock;	int			nr_systemwide;	struct list_head	perf_events;}; static int trace_uprobe_create(int argc, const char **argv);static int trace_uprobe_show(struct seq_file *m, struct dyn_event *ev);static int trace_uprobe_release(struct dyn_event *ev);static bool trace_uprobe_is_busy(struct dyn_event *ev);static bool trace_uprobe_match(const char *system, const char *event,			       struct dyn_event *ev); static struct dyn_event_operations trace_uprobe_ops = {	.create = trace_uprobe_create,	.show = trace_uprobe_show,	.is_busy = trace_uprobe_is_busy,	.free = trace_uprobe_release,	.match = trace_uprobe_match,}; /* * uprobe event core functions */struct trace_uprobe {	struct dyn_event		devent;	struct trace_uprobe_filter	filter;	struct uprobe_consumer		consumer;	struct path			path;	struct inode			*inode;	char				*filename;	unsigned long			offset;	unsigned long			ref_ctr_offset;	unsigned long			nhit;	struct trace_probe		tp;}; static bool is_trace_uprobe(struct dyn_event *ev){	return ev->ops == &trace_uprobe_ops;} static struct trace_uprobe *to_trace_uprobe(struct dyn_event *ev){	return container_of(ev, struct trace_uprobe, devent);} /** * for_each_trace_uprobe - iterate over the trace_uprobe list * @pos:	the struct trace_uprobe * for each entry * @dpos:	the struct dyn_event * to use as a loop cursor */#define for_each_trace_uprobe(pos, dpos)	\	for_each_dyn_event(dpos)		\		if (is_trace_uprobe(dpos) && (pos = to_trace_uprobe(dpos))) #define SIZEOF_TRACE_UPROBE(n)				\	(offsetof(struct trace_uprobe, tp.args) +	\	(sizeof(struct probe_arg) * (n))) static int register_uprobe_event(struct trace_uprobe *tu);static int unregister_uprobe_event(struct trace_uprobe *tu); struct uprobe_dispatch_data {	struct trace_uprobe	*tu;	unsigned long		bp_addr;}; static int uprobe_dispatcher(struct uprobe_consumer *con, struct pt_regs *regs);static int uretprobe_dispatcher(struct uprobe_consumer *con,				unsigned long func, struct pt_regs *regs); #ifdef CONFIG_STACK_GROWSUPstatic unsigned long adjust_stack_addr(unsigned long addr, unsigned int n){	return addr - (n * sizeof(long));}#elsestatic unsigned long adjust_stack_addr(unsigned long addr, unsigned int n){	return addr + (n * sizeof(long));}#endif static unsigned long get_user_stack_nth(struct pt_regs *regs, unsigned int n){	unsigned long ret;	unsigned long addr = user_stack_pointer(regs); 	addr = adjust_stack_addr(addr, n); 	if (copy_from_user(&ret, (void __force __user *) addr, sizeof(ret)))		return 0; 	return ret;} /* * Uprobes-specific fetch functions */static nokprobe_inline intprobe_mem_read(void *dest, void *src, size_t size){	void __user *vaddr = (void __force __user *)src; 	return copy_from_user(dest, vaddr, size) ? -EFAULT : 0;} static nokprobe_inline intprobe_mem_read_user(void *dest, void *src, size_t size){	return probe_mem_read(dest, src, size);} /* * Fetch a null-terminated string. Caller MUST set *(u32 *)dest with max * length and relative data location. */static nokprobe_inline intfetch_store_string(unsigned long addr, void *dest, void *base){	long ret;	u32 loc = *(u32 *)dest;	int maxlen  = get_loc_len(loc);	u8 *dst = get_loc_data(dest, base);	void __user *src = (void __force __user *) addr; 	if (unlikely(!maxlen))		return -ENOMEM; 	if (addr == FETCH_TOKEN_COMM)		ret = strlcpy(dst, current->comm, maxlen);	else		ret = strncpy_from_user(dst, src, maxlen);	if (ret >= 0) {		if (ret == maxlen)			dst[ret - 1] = '\0';		else			/*			 * Include the terminating null byte. In this case it			 * was copied by strncpy_from_user but not accounted			 * for in ret.			 */			ret++;		*(u32 *)dest = make_data_loc(ret, (void *)dst - base);	} 	return ret;} static nokprobe_inline intfetch_store_string_user(unsigned long addr, void *dest, void *base){	return fetch_store_string(addr, dest, base);} /* Return the length of string -- including null terminal byte */static nokprobe_inline intfetch_store_strlen(unsigned long addr){	int len;	void __user *vaddr = (void __force __user *) addr; 	if (addr == FETCH_TOKEN_COMM)		len = strlen(current->comm) + 1;	else		len = strnlen_user(vaddr, MAX_STRING_SIZE); 	return (len > MAX_STRING_SIZE) ? 0 : len;} static nokprobe_inline intfetch_store_strlen_user(unsigned long addr){	return fetch_store_strlen(addr);} static unsigned long translate_user_vaddr(unsigned long file_offset){	unsigned long base_addr;	struct uprobe_dispatch_data *udd; 	udd = (void *) current->utask->vaddr; 	base_addr = udd->bp_addr - udd->tu->offset;	return base_addr + file_offset;} /* Note that we don't verify it, since the code does not come from user space */static intprocess_fetch_insn(struct fetch_insn *code, struct pt_regs *regs, void *dest,		   void *base){	unsigned long val; 	/* 1st stage: get value from context */	switch (code->op) {	case FETCH_OP_REG:		val = regs_get_register(regs, code->param);		break;	case FETCH_OP_STACK:		val = get_user_stack_nth(regs, code->param);		break;	case FETCH_OP_STACKP:		val = user_stack_pointer(regs);		break;	case FETCH_OP_RETVAL:		val = regs_return_value(regs);		break;	case FETCH_OP_IMM:		val = code->immediate;		break;	case FETCH_OP_COMM:		val = FETCH_TOKEN_COMM;		break;	case FETCH_OP_FOFFS:		val = translate_user_vaddr(code->immediate);		break;	default:		return -EILSEQ;	}	code++; 	return process_fetch_insn_bottom(code, val, dest, base);}NOKPROBE_SYMBOL(process_fetch_insn) static inline void init_trace_uprobe_filter(struct trace_uprobe_filter *filter){	rwlock_init(&filter->rwlock);	filter->nr_systemwide = 0;	INIT_LIST_HEAD(&filter->perf_events);} static inline bool uprobe_filter_is_empty(struct trace_uprobe_filter *filter){	return !filter->nr_systemwide && list_empty(&filter->perf_events);} static inline bool is_ret_probe(struct trace_uprobe *tu){	return tu->consumer.ret_handler != NULL;} static bool trace_uprobe_is_busy(struct dyn_event *ev){	struct trace_uprobe *tu = to_trace_uprobe(ev); 	return trace_probe_is_enabled(&tu->tp);} static bool trace_uprobe_match(const char *system, const char *event,			       struct dyn_event *ev){	struct trace_uprobe *tu = to_trace_uprobe(ev); 	return strcmp(trace_probe_name(&tu->tp), event) == 0 &&	    (!system || strcmp(trace_probe_group_name(&tu->tp), system) == 0);} /* * Allocate new trace_uprobe and initialize it (including uprobes). */static struct trace_uprobe *alloc_trace_uprobe(const char *group, const char *event, int nargs, bool is_ret){	struct trace_uprobe *tu;	int ret; 	tu = kzalloc(SIZEOF_TRACE_UPROBE(nargs), GFP_KERNEL);	if (!tu)		return ERR_PTR(-ENOMEM); 	ret = trace_probe_init(&tu->tp, event, group);	if (ret < 0)		goto error; 	dyn_event_init(&tu->devent, &trace_uprobe_ops);	tu->consumer.handler = uprobe_dispatcher;	if (is_ret)		tu->consumer.ret_handler = uretprobe_dispatcher;	init_trace_uprobe_filter(&tu->filter);	return tu; error:	kfree(tu); 	return ERR_PTR(ret);} static void free_trace_uprobe(struct trace_uprobe *tu){	if (!tu)		return; 	path_put(&tu->path);	trace_probe_cleanup(&tu->tp);	kfree(tu->filename);	kfree(tu);} static struct trace_uprobe *find_probe_event(const char *event, const char *group){	struct dyn_event *pos;	struct trace_uprobe *tu; 	for_each_trace_uprobe(tu, pos)		if (strcmp(trace_probe_name(&tu->tp), event) == 0 &&		    strcmp(trace_probe_group_name(&tu->tp), group) == 0)			return tu; 	return NULL;} /* Unregister a trace_uprobe and probe_event */static int unregister_trace_uprobe(struct trace_uprobe *tu){	int ret; 	ret = unregister_uprobe_event(tu);	if (ret)		return ret; 	dyn_event_remove(&tu->devent);	free_trace_uprobe(tu);	return 0;} /* * Uprobe with multiple reference counter is not allowed. i.e. * If inode and offset matches, reference counter offset *must* * match as well. Though, there is one exception: If user is * replacing old trace_uprobe with new one(same group/event), * then we allow same uprobe with new reference counter as far * as the new one does not conflict with any other existing * ones. */static struct trace_uprobe *find_old_trace_uprobe(struct trace_uprobe *new){	struct dyn_event *pos;	struct trace_uprobe *tmp, *old = NULL;	struct inode *new_inode = d_real_inode(new->path.dentry); 	old = find_probe_event(trace_probe_name(&new->tp),				trace_probe_group_name(&new->tp)); 	for_each_trace_uprobe(tmp, pos) {		if ((old ? old != tmp : true) &&		    new_inode == d_real_inode(tmp->path.dentry) &&		    new->offset == tmp->offset &&		    new->ref_ctr_offset != tmp->ref_ctr_offset) {			pr_warn("Reference counter offset mismatch.");			return ERR_PTR(-EINVAL);		}	}	return old;} /* Register a trace_uprobe and probe_event */static int register_trace_uprobe(struct trace_uprobe *tu){	struct trace_uprobe *old_tu;	int ret; 	mutex_lock(&event_mutex); 	/* register as an event */	old_tu = find_old_trace_uprobe(tu);	if (IS_ERR(old_tu)) {		ret = PTR_ERR(old_tu);		goto end;	} 	if (old_tu) {		/* delete old event */		ret = unregister_trace_uprobe(old_tu);		if (ret)			goto end;	} 	ret = register_uprobe_event(tu);	if (ret) {		pr_warn("Failed to register probe event(%d)\n", ret);		goto end;	} 	dyn_event_add(&tu->devent); end:	mutex_unlock(&event_mutex); 	return ret;} /* * Argument syntax: *  - Add uprobe: p|r[:[GRP/]EVENT] PATH:OFFSET [FETCHARGS] */static int trace_uprobe_create(int argc, const char **argv){	struct trace_uprobe *tu;	const char *event = NULL, *group = UPROBE_EVENT_SYSTEM;	char *arg, *filename, *rctr, *rctr_end, *tmp;	char buf[MAX_EVENT_NAME_LEN];	struct path path;	unsigned long offset, ref_ctr_offset;	bool is_return = false;	int i, ret; 	ret = 0;	ref_ctr_offset = 0; 	switch (argv[0][0]) {	case 'r':		is_return = true;		break;	case 'p':		break;	default:		return -ECANCELED;	} 	if (argc < 2)		return -ECANCELED; 	if (argv[0][1] == ':')		event = &argv[0][2]; 	if (!strchr(argv[1], '/'))		return -ECANCELED; 	filename = kstrdup(argv[1], GFP_KERNEL);	if (!filename)		return -ENOMEM; 	/* Find the last occurrence, in case the path contains ':' too. */	arg = strrchr(filename, ':');	if (!arg || !isdigit(arg[1])) {		kfree(filename);		return -ECANCELED;	} 	trace_probe_log_init("trace_uprobe", argc, argv);	trace_probe_log_set_index(1);	/* filename is the 2nd argument */ 	*arg++ = '\0';	ret = kern_path(filename, LOOKUP_FOLLOW, &path);	if (ret) {		trace_probe_log_err(0, FILE_NOT_FOUND);		kfree(filename);		trace_probe_log_clear();		return ret;	}	if (!d_is_reg(path.dentry)) {		trace_probe_log_err(0, NO_REGULAR_FILE);		ret = -EINVAL;		goto fail_address_parse;	} 	/* Parse reference counter offset if specified. */	rctr = strchr(arg, '(');	if (rctr) {		rctr_end = strchr(rctr, ')');		if (!rctr_end) {			ret = -EINVAL;

Lines 1–500 of 1430.