diff options
Diffstat (limited to 'tools/lib')
80 files changed, 5145 insertions, 594 deletions
diff --git a/tools/lib/lk/Makefile b/tools/lib/api/Makefile index 3dba0a4aebb..ce00f7ee645 100644 --- a/tools/lib/lk/Makefile +++ b/tools/lib/api/Makefile @@ -1,4 +1,5 @@  include ../../scripts/Makefile.include +include ../../perf/config/utilities.mak		# QUIET_CLEAN  CC = $(CROSS_COMPILE)gcc  AR = $(CROSS_COMPILE)ar @@ -7,11 +8,13 @@ AR = $(CROSS_COMPILE)ar  LIB_H=  LIB_OBJS= -LIB_H += debugfs.h +LIB_H += fs/debugfs.h +LIB_H += fs/fs.h -LIB_OBJS += $(OUTPUT)debugfs.o +LIB_OBJS += $(OUTPUT)fs/debugfs.o +LIB_OBJS += $(OUTPUT)fs/fs.o -LIBFILE = liblk.a +LIBFILE = libapikfs.a  CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) -fPIC  EXTLIBS = -lelf -lpthread -lrt -lm @@ -25,14 +28,17 @@ $(LIBFILE): $(LIB_OBJS)  $(LIB_OBJS): $(LIB_H) -$(OUTPUT)%.o: %.c +libapi_dirs: +	$(QUIET_MKDIR)mkdir -p $(OUTPUT)fs/ + +$(OUTPUT)%.o: %.c libapi_dirs  	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< -$(OUTPUT)%.s: %.c +$(OUTPUT)%.s: %.c libapi_dirs  	$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $< -$(OUTPUT)%.o: %.S +$(OUTPUT)%.o: %.S libapi_dirs  	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<  clean: -	$(RM) $(LIB_OBJS) $(LIBFILE) +	$(call QUIET_CLEAN, libapi) $(RM) $(LIB_OBJS) $(LIBFILE)  .PHONY: clean diff --git a/tools/lib/lk/debugfs.c b/tools/lib/api/fs/debugfs.c index 099e7cd022e..a74fba6d774 100644 --- a/tools/lib/lk/debugfs.c +++ b/tools/lib/api/fs/debugfs.c @@ -5,7 +5,6 @@  #include <stdbool.h>  #include <sys/vfs.h>  #include <sys/mount.h> -#include <linux/magic.h>  #include <linux/kernel.h>  #include "debugfs.h" @@ -13,8 +12,8 @@  char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";  static const char * const debugfs_known_mountpoints[] = { -	"/sys/kernel/debug/", -	"/debug/", +	"/sys/kernel/debug", +	"/debug",  	0,  }; diff --git a/tools/lib/lk/debugfs.h b/tools/lib/api/fs/debugfs.h index 935c59bdb44..f19d3df9609 100644 --- a/tools/lib/lk/debugfs.h +++ b/tools/lib/api/fs/debugfs.h @@ -1,5 +1,5 @@ -#ifndef __LK_DEBUGFS_H__ -#define __LK_DEBUGFS_H__ +#ifndef __API_DEBUGFS_H__ +#define __API_DEBUGFS_H__  #define _STR(x) #x  #define STR(x) _STR(x) @@ -26,4 +26,4 @@ char *debugfs_mount(const char *mountpoint);  extern char debugfs_mountpoint[]; -#endif /* __LK_DEBUGFS_H__ */ +#endif /* __API_DEBUGFS_H__ */ diff --git a/tools/lib/api/fs/fs.c b/tools/lib/api/fs/fs.c new file mode 100644 index 00000000000..c1b49c36a95 --- /dev/null +++ b/tools/lib/api/fs/fs.c @@ -0,0 +1,165 @@ +/* TODO merge/factor in debugfs.c here */ + +#include <ctype.h> +#include <errno.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/vfs.h> + +#include "debugfs.h" +#include "fs.h" + +static const char * const sysfs__fs_known_mountpoints[] = { +	"/sys", +	0, +}; + +static const char * const procfs__known_mountpoints[] = { +	"/proc", +	0, +}; + +struct fs { +	const char		*name; +	const char * const	*mounts; +	char			 path[PATH_MAX + 1]; +	bool			 found; +	long			 magic; +}; + +enum { +	FS__SYSFS  = 0, +	FS__PROCFS = 1, +}; + +static struct fs fs__entries[] = { +	[FS__SYSFS] = { +		.name	= "sysfs", +		.mounts	= sysfs__fs_known_mountpoints, +		.magic	= SYSFS_MAGIC, +	}, +	[FS__PROCFS] = { +		.name	= "proc", +		.mounts	= procfs__known_mountpoints, +		.magic	= PROC_SUPER_MAGIC, +	}, +}; + +static bool fs__read_mounts(struct fs *fs) +{ +	bool found = false; +	char type[100]; +	FILE *fp; + +	fp = fopen("/proc/mounts", "r"); +	if (fp == NULL) +		return NULL; + +	while (!found && +	       fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", +		      fs->path, type) == 2) { + +		if (strcmp(type, fs->name) == 0) +			found = true; +	} + +	fclose(fp); +	return fs->found = found; +} + +static int fs__valid_mount(const char *fs, long magic) +{ +	struct statfs st_fs; + +	if (statfs(fs, &st_fs) < 0) +		return -ENOENT; +	else if (st_fs.f_type != magic) +		return -ENOENT; + +	return 0; +} + +static bool fs__check_mounts(struct fs *fs) +{ +	const char * const *ptr; + +	ptr = fs->mounts; +	while (*ptr) { +		if (fs__valid_mount(*ptr, fs->magic) == 0) { +			fs->found = true; +			strcpy(fs->path, *ptr); +			return true; +		} +		ptr++; +	} + +	return false; +} + +static void mem_toupper(char *f, size_t len) +{ +	while (len) { +		*f = toupper(*f); +		f++; +		len--; +	} +} + +/* + * Check for "NAME_PATH" environment variable to override fs location (for + * testing). This matches the recommendation in Documentation/sysfs-rules.txt + * for SYSFS_PATH. + */ +static bool fs__env_override(struct fs *fs) +{ +	char *override_path; +	size_t name_len = strlen(fs->name); +	/* name + "_PATH" + '\0' */ +	char upper_name[name_len + 5 + 1]; +	memcpy(upper_name, fs->name, name_len); +	mem_toupper(upper_name, name_len); +	strcpy(&upper_name[name_len], "_PATH"); + +	override_path = getenv(upper_name); +	if (!override_path) +		return false; + +	fs->found = true; +	strncpy(fs->path, override_path, sizeof(fs->path)); +	return true; +} + +static const char *fs__get_mountpoint(struct fs *fs) +{ +	if (fs__env_override(fs)) +		return fs->path; + +	if (fs__check_mounts(fs)) +		return fs->path; + +	if (fs__read_mounts(fs)) +		return fs->path; + +	return NULL; +} + +static const char *fs__mountpoint(int idx) +{ +	struct fs *fs = &fs__entries[idx]; + +	if (fs->found) +		return (const char *)fs->path; + +	return fs__get_mountpoint(fs); +} + +#define FS__MOUNTPOINT(name, idx)	\ +const char *name##__mountpoint(void)	\ +{					\ +	return fs__mountpoint(idx);	\ +} + +FS__MOUNTPOINT(sysfs,  FS__SYSFS); +FS__MOUNTPOINT(procfs, FS__PROCFS); diff --git a/tools/lib/api/fs/fs.h b/tools/lib/api/fs/fs.h new file mode 100644 index 00000000000..cb7049551f3 --- /dev/null +++ b/tools/lib/api/fs/fs.h @@ -0,0 +1,14 @@ +#ifndef __API_FS__ +#define __API_FS__ + +#ifndef SYSFS_MAGIC +#define SYSFS_MAGIC            0x62656572 +#endif + +#ifndef PROC_SUPER_MAGIC +#define PROC_SUPER_MAGIC       0x9fa0 +#endif + +const char *sysfs__mountpoint(void); +const char *procfs__mountpoint(void); +#endif /* __API_FS__ */ diff --git a/tools/lib/lockdep/Makefile b/tools/lib/lockdep/Makefile new file mode 100644 index 00000000000..52f9279c6c1 --- /dev/null +++ b/tools/lib/lockdep/Makefile @@ -0,0 +1,243 @@ +# file format version +FILE_VERSION = 1 + +LIBLOCKDEP_VERSION=$(shell make --no-print-directory -sC ../../.. kernelversion) + +# Makefiles suck: This macro sets a default value of $(2) for the +# variable named by $(1), unless the variable has been set by +# environment or command line. This is necessary for CC and AR +# because make sets default values, so the simpler ?= approach +# won't work as expected. +define allow-override +  $(if $(or $(findstring environment,$(origin $(1))),\ +            $(findstring command line,$(origin $(1)))),,\ +    $(eval $(1) = $(2))) +endef + +# Allow setting CC and AR, or setting CROSS_COMPILE as a prefix. +$(call allow-override,CC,$(CROSS_COMPILE)gcc) +$(call allow-override,AR,$(CROSS_COMPILE)ar) + +INSTALL = install + +# Use DESTDIR for installing into a different root directory. +# This is useful for building a package. The program will be +# installed in this directory as if it was the root directory. +# Then the build tool can move it later. +DESTDIR ?= +DESTDIR_SQ = '$(subst ','\'',$(DESTDIR))' + +prefix ?= /usr/local +libdir_relative = lib +libdir = $(prefix)/$(libdir_relative) +bindir_relative = bin +bindir = $(prefix)/$(bindir_relative) + +export DESTDIR DESTDIR_SQ INSTALL + +# copy a bit from Linux kbuild + +ifeq ("$(origin V)", "command line") +  VERBOSE = $(V) +endif +ifndef VERBOSE +  VERBOSE = 0 +endif + +ifeq ("$(origin O)", "command line") +  BUILD_OUTPUT := $(O) +endif + +ifeq ($(BUILD_SRC),) +ifneq ($(BUILD_OUTPUT),) + +define build_output +	$(if $(VERBOSE:1=),@)$(MAKE) -C $(BUILD_OUTPUT)	\ +	BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1 +endef + +saved-output := $(BUILD_OUTPUT) +BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd) +$(if $(BUILD_OUTPUT),, \ +     $(error output directory "$(saved-output)" does not exist)) + +all: sub-make + +gui: force +	$(call build_output, all_cmd) + +$(filter-out gui,$(MAKECMDGOALS)): sub-make + +sub-make: force +	$(call build_output, $(MAKECMDGOALS)) + + +# Leave processing to above invocation of make +skip-makefile := 1 + +endif # BUILD_OUTPUT +endif # BUILD_SRC + +# We process the rest of the Makefile if this is the final invocation of make +ifeq ($(skip-makefile),) + +srctree		:= $(realpath $(if $(BUILD_SRC),$(BUILD_SRC),$(CURDIR))) +objtree		:= $(realpath $(CURDIR)) +src		:= $(srctree) +obj		:= $(objtree) + +export prefix libdir bindir src obj + +# Shell quotes +libdir_SQ = $(subst ','\'',$(libdir)) +bindir_SQ = $(subst ','\'',$(bindir)) + +LIB_FILE = liblockdep.a liblockdep.so.$(LIBLOCKDEP_VERSION) +BIN_FILE = lockdep + +CONFIG_INCLUDES = +CONFIG_LIBS	= +CONFIG_FLAGS	= + +OBJ		= $@ +N		= + +export Q VERBOSE + +INCLUDES = -I. -I/usr/local/include -I./uinclude -I./include -I../../include $(CONFIG_INCLUDES) + +# Set compile option CFLAGS if not set elsewhere +CFLAGS ?= -g -DCONFIG_LOCKDEP -DCONFIG_STACKTRACE -DCONFIG_PROVE_LOCKING -DBITS_PER_LONG=__WORDSIZE -DLIBLOCKDEP_VERSION='"$(LIBLOCKDEP_VERSION)"' -rdynamic -O0 -g + +override CFLAGS += $(CONFIG_FLAGS) $(INCLUDES) $(PLUGIN_DIR_SQ) + +ifeq ($(VERBOSE),1) +  Q = +  print_compile = +  print_app_build = +  print_fpic_compile = +  print_shared_lib_compile = +  print_install = +else +  Q = @ +  print_compile =		echo '  CC                 '$(OBJ); +  print_app_build =		echo '  BUILD              '$(OBJ); +  print_fpic_compile =		echo '  CC FPIC            '$(OBJ); +  print_shared_lib_compile =	echo '  BUILD SHARED LIB   '$(OBJ); +  print_static_lib_build =	echo '  BUILD STATIC LIB   '$(OBJ); +  print_install =		echo '  INSTALL     '$1'	to	$(DESTDIR_SQ)$2'; +endif + +do_fpic_compile =					\ +	($(print_fpic_compile)				\ +	$(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@) + +do_app_build =						\ +	($(print_app_build)				\ +	$(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS)) + +do_compile_shared_library =			\ +	($(print_shared_lib_compile)		\ +	$(CC) --shared $^ -o $@ -lpthread -ldl -Wl,-soname='"$@"';$(shell ln -s $@ liblockdep.so)) + +do_build_static_lib =				\ +	($(print_static_lib_build)		\ +	$(RM) $@;  $(AR) rcs $@ $^) + + +define do_compile +	$(print_compile)						\ +	$(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@; +endef + +$(obj)/%.o: $(src)/%.c +	$(Q)$(call do_compile) + +%.o: $(src)/%.c +	$(Q)$(call do_compile) + +PEVENT_LIB_OBJS = common.o lockdep.o preload.o rbtree.o + +ALL_OBJS = $(PEVENT_LIB_OBJS) + +CMD_TARGETS = $(LIB_FILE) + +TARGETS = $(CMD_TARGETS) + + +all: all_cmd + +all_cmd: $(CMD_TARGETS) + +liblockdep.so.$(LIBLOCKDEP_VERSION): $(PEVENT_LIB_OBJS) +	$(Q)$(do_compile_shared_library) + +liblockdep.a: $(PEVENT_LIB_OBJS) +	$(Q)$(do_build_static_lib) + +$(PEVENT_LIB_OBJS): %.o: $(src)/%.c +	$(Q)$(do_fpic_compile) + +## make deps + +all_objs := $(sort $(ALL_OBJS)) +all_deps := $(all_objs:%.o=.%.d) + +# let .d file also depends on the source and header files +define check_deps +		@set -e; $(RM) $@; \ +		$(CC) -MM $(CFLAGS) $< > $@.$$$$; \ +		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +		$(RM) $@.$$$$ +endef + +$(all_deps): .%.d: $(src)/%.c +	$(Q)$(call check_deps) + +$(all_objs) : %.o : .%.d + +dep_includes := $(wildcard $(all_deps)) + +ifneq ($(dep_includes),) + include $(dep_includes) +endif + +### Detect environment changes +TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE) + +tags:	force +	$(RM) tags +	find . -name '*.[ch]' | xargs ctags --extra=+f --c-kinds=+px \ +	--regex-c++='/_PE\(([^,)]*).*/PEVENT_ERRNO__\1/' + +TAGS:	force +	$(RM) TAGS +	find . -name '*.[ch]' | xargs etags \ +	--regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/' + +define do_install +	$(print_install)				\ +	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\ +		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\ +	fi;						\ +	$(INSTALL) $1 '$(DESTDIR_SQ)$2' +endef + +install_lib: all_cmd +	$(Q)$(call do_install,$(LIB_FILE),$(libdir_SQ)) +	$(Q)$(call do_install,$(BIN_FILE),$(bindir_SQ)) + +install: install_lib + +clean: +	$(RM) *.o *~ $(TARGETS) *.a *liblockdep*.so* $(VERSION_FILES) .*.d +	$(RM) tags TAGS + +endif # skip-makefile + +PHONY += force +force: + +# Declare the contents of the .PHONY variable as phony.  We keep that +# information in a variable so we can use it in if_changed and friends. +.PHONY: $(PHONY) diff --git a/tools/lib/lockdep/common.c b/tools/lib/lockdep/common.c new file mode 100644 index 00000000000..8ef602f18a3 --- /dev/null +++ b/tools/lib/lockdep/common.c @@ -0,0 +1,33 @@ +#include <stddef.h> +#include <stdbool.h> +#include <linux/compiler.h> +#include <linux/lockdep.h> +#include <unistd.h> +#include <sys/syscall.h> + +static __thread struct task_struct current_obj; + +/* lockdep wants these */ +bool debug_locks = true; +bool debug_locks_silent; + +__attribute__((constructor)) static void liblockdep_init(void) +{ +	lockdep_init(); +} + +__attribute__((destructor)) static void liblockdep_exit(void) +{ +	debug_check_no_locks_held(¤t_obj); +} + +struct task_struct *__curr(void) +{ +	if (current_obj.pid == 0) { +		/* Makes lockdep output pretty */ +		prctl(PR_GET_NAME, current_obj.comm); +		current_obj.pid = syscall(__NR_gettid); +	} + +	return ¤t_obj; +} diff --git a/tools/lib/lockdep/include/liblockdep/common.h b/tools/lib/lockdep/include/liblockdep/common.h new file mode 100644 index 00000000000..0bda630027c --- /dev/null +++ b/tools/lib/lockdep/include/liblockdep/common.h @@ -0,0 +1,50 @@ +#ifndef _LIBLOCKDEP_COMMON_H +#define _LIBLOCKDEP_COMMON_H + +#include <pthread.h> + +#define NR_LOCKDEP_CACHING_CLASSES 2 +#define MAX_LOCKDEP_SUBCLASSES 8UL + +#ifndef CALLER_ADDR0 +#define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) +#endif + +#ifndef _RET_IP_ +#define _RET_IP_ CALLER_ADDR0 +#endif + +#ifndef _THIS_IP_ +#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) +#endif + +struct lockdep_subclass_key { +	char __one_byte; +}; + +struct lock_class_key { +	struct lockdep_subclass_key subkeys[MAX_LOCKDEP_SUBCLASSES]; +}; + +struct lockdep_map { +	struct lock_class_key	*key; +	struct lock_class	*class_cache[NR_LOCKDEP_CACHING_CLASSES]; +	const char		*name; +#ifdef CONFIG_LOCK_STAT +	int			cpu; +	unsigned long		ip; +#endif +}; + +void lockdep_init_map(struct lockdep_map *lock, const char *name, +			struct lock_class_key *key, int subclass); +void lock_acquire(struct lockdep_map *lock, unsigned int subclass, +			int trylock, int read, int check, +			struct lockdep_map *nest_lock, unsigned long ip); +void lock_release(struct lockdep_map *lock, int nested, +			unsigned long ip); + +#define STATIC_LOCKDEP_MAP_INIT(_name, _key) \ +	{ .name = (_name), .key = (void *)(_key), } + +#endif diff --git a/tools/lib/lockdep/include/liblockdep/mutex.h b/tools/lib/lockdep/include/liblockdep/mutex.h new file mode 100644 index 00000000000..ee53a42818c --- /dev/null +++ b/tools/lib/lockdep/include/liblockdep/mutex.h @@ -0,0 +1,70 @@ +#ifndef _LIBLOCKDEP_MUTEX_H +#define _LIBLOCKDEP_MUTEX_H + +#include <pthread.h> +#include "common.h" + +struct liblockdep_pthread_mutex { +	pthread_mutex_t mutex; +	struct lockdep_map dep_map; +}; + +typedef struct liblockdep_pthread_mutex liblockdep_pthread_mutex_t; + +#define LIBLOCKDEP_PTHREAD_MUTEX_INITIALIZER(mtx)			\ +		(const struct liblockdep_pthread_mutex) {		\ +	.mutex = PTHREAD_MUTEX_INITIALIZER,				\ +	.dep_map = STATIC_LOCKDEP_MAP_INIT(#mtx, &((&(mtx))->dep_map)),	\ +} + +static inline int __mutex_init(liblockdep_pthread_mutex_t *lock, +				const char *name, +				struct lock_class_key *key, +				const pthread_mutexattr_t *__mutexattr) +{ +	lockdep_init_map(&lock->dep_map, name, key, 0); +	return pthread_mutex_init(&lock->mutex, __mutexattr); +} + +#define liblockdep_pthread_mutex_init(mutex, mutexattr)		\ +({								\ +	static struct lock_class_key __key;			\ +								\ +	__mutex_init((mutex), #mutex, &__key, (mutexattr));	\ +}) + +static inline int liblockdep_pthread_mutex_lock(liblockdep_pthread_mutex_t *lock) +{ +	lock_acquire(&lock->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); +	return pthread_mutex_lock(&lock->mutex); +} + +static inline int liblockdep_pthread_mutex_unlock(liblockdep_pthread_mutex_t *lock) +{ +	lock_release(&lock->dep_map, 0, (unsigned long)_RET_IP_); +	return pthread_mutex_unlock(&lock->mutex); +} + +static inline int liblockdep_pthread_mutex_trylock(liblockdep_pthread_mutex_t *lock) +{ +	lock_acquire(&lock->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_); +	return pthread_mutex_trylock(&lock->mutex) == 0 ? 1 : 0; +} + +static inline int liblockdep_pthread_mutex_destroy(liblockdep_pthread_mutex_t *lock) +{ +	return pthread_mutex_destroy(&lock->mutex); +} + +#ifdef __USE_LIBLOCKDEP + +#define pthread_mutex_t         liblockdep_pthread_mutex_t +#define pthread_mutex_init      liblockdep_pthread_mutex_init +#define pthread_mutex_lock      liblockdep_pthread_mutex_lock +#define pthread_mutex_unlock    liblockdep_pthread_mutex_unlock +#define pthread_mutex_trylock   liblockdep_pthread_mutex_trylock +#define pthread_mutex_destroy   liblockdep_pthread_mutex_destroy + +#endif + +#endif diff --git a/tools/lib/lockdep/include/liblockdep/rwlock.h b/tools/lib/lockdep/include/liblockdep/rwlock.h new file mode 100644 index 00000000000..4ec03f86155 --- /dev/null +++ b/tools/lib/lockdep/include/liblockdep/rwlock.h @@ -0,0 +1,86 @@ +#ifndef _LIBLOCKDEP_RWLOCK_H +#define _LIBLOCKDEP_RWLOCK_H + +#include <pthread.h> +#include "common.h" + +struct liblockdep_pthread_rwlock { +	pthread_rwlock_t rwlock; +	struct lockdep_map dep_map; +}; + +typedef struct liblockdep_pthread_rwlock liblockdep_pthread_rwlock_t; + +#define LIBLOCKDEP_PTHREAD_RWLOCK_INITIALIZER(rwl)			\ +		(struct liblockdep_pthread_rwlock) {			\ +	.rwlock = PTHREAD_RWLOCK_INITIALIZER,				\ +	.dep_map = STATIC_LOCKDEP_MAP_INIT(#rwl, &((&(rwl))->dep_map)),	\ +} + +static inline int __rwlock_init(liblockdep_pthread_rwlock_t *lock, +				const char *name, +				struct lock_class_key *key, +				const pthread_rwlockattr_t *attr) +{ +	lockdep_init_map(&lock->dep_map, name, key, 0); + +	return pthread_rwlock_init(&lock->rwlock, attr); +} + +#define liblockdep_pthread_rwlock_init(lock, attr)		\ +({							\ +	static struct lock_class_key __key;		\ +							\ +	__rwlock_init((lock), #lock, &__key, (attr));	\ +}) + +static inline int liblockdep_pthread_rwlock_rdlock(liblockdep_pthread_rwlock_t *lock) +{ +	lock_acquire(&lock->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_); +	return pthread_rwlock_rdlock(&lock->rwlock); + +} + +static inline int liblockdep_pthread_rwlock_unlock(liblockdep_pthread_rwlock_t *lock) +{ +	lock_release(&lock->dep_map, 0, (unsigned long)_RET_IP_); +	return pthread_rwlock_unlock(&lock->rwlock); +} + +static inline int liblockdep_pthread_rwlock_wrlock(liblockdep_pthread_rwlock_t *lock) +{ +	lock_acquire(&lock->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); +	return pthread_rwlock_wrlock(&lock->rwlock); +} + +static inline int liblockdep_pthread_rwlock_tryrdlock(liblockdep_pthread_rwlock_t *lock) +{ +	lock_acquire(&lock->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_); +	return pthread_rwlock_tryrdlock(&lock->rwlock) == 0 ? 1 : 0; +} + +static inline int liblockdep_pthread_rwlock_trywlock(liblockdep_pthread_rwlock_t *lock) +{ +	lock_acquire(&lock->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_); +	return pthread_rwlock_trywlock(&lock->rwlock) == 0 ? 1 : 0; +} + +static inline int liblockdep_rwlock_destroy(liblockdep_pthread_rwlock_t *lock) +{ +	return pthread_rwlock_destroy(&lock->rwlock); +} + +#ifdef __USE_LIBLOCKDEP + +#define pthread_rwlock_t		liblockdep_pthread_rwlock_t +#define pthread_rwlock_init		liblockdep_pthread_rwlock_init +#define pthread_rwlock_rdlock		liblockdep_pthread_rwlock_rdlock +#define pthread_rwlock_unlock		liblockdep_pthread_rwlock_unlock +#define pthread_rwlock_wrlock		liblockdep_pthread_rwlock_wrlock +#define pthread_rwlock_tryrdlock	liblockdep_pthread_rwlock_tryrdlock +#define pthread_rwlock_trywlock		liblockdep_pthread_rwlock_trywlock +#define pthread_rwlock_destroy		liblockdep_rwlock_destroy + +#endif + +#endif diff --git a/tools/lib/lockdep/lockdep b/tools/lib/lockdep/lockdep new file mode 100755 index 00000000000..49af9fe19f5 --- /dev/null +++ b/tools/lib/lockdep/lockdep @@ -0,0 +1,3 @@ +#!/bin/bash + +LD_PRELOAD="./liblockdep.so $LD_PRELOAD" "$@" diff --git a/tools/lib/lockdep/lockdep.c b/tools/lib/lockdep/lockdep.c new file mode 100644 index 00000000000..f42b7e9aa48 --- /dev/null +++ b/tools/lib/lockdep/lockdep.c @@ -0,0 +1,2 @@ +#include <linux/lockdep.h> +#include "../../../kernel/locking/lockdep.c" diff --git a/tools/lib/lockdep/lockdep_internals.h b/tools/lib/lockdep/lockdep_internals.h new file mode 100644 index 00000000000..29d0c954cc2 --- /dev/null +++ b/tools/lib/lockdep/lockdep_internals.h @@ -0,0 +1 @@ +#include "../../../kernel/locking/lockdep_internals.h" diff --git a/tools/lib/lockdep/lockdep_states.h b/tools/lib/lockdep/lockdep_states.h new file mode 100644 index 00000000000..248d235efda --- /dev/null +++ b/tools/lib/lockdep/lockdep_states.h @@ -0,0 +1 @@ +#include "../../../kernel/locking/lockdep_states.h" diff --git a/tools/lib/lockdep/preload.c b/tools/lib/lockdep/preload.c new file mode 100644 index 00000000000..6f803609e49 --- /dev/null +++ b/tools/lib/lockdep/preload.c @@ -0,0 +1,445 @@ +#define _GNU_SOURCE +#include <pthread.h> +#include <stdio.h> +#include <dlfcn.h> +#include <stdlib.h> +#include <sysexits.h> +#include "include/liblockdep/mutex.h" +#include "../../../include/linux/rbtree.h" + +/** + * struct lock_lookup - liblockdep's view of a single unique lock + * @orig: pointer to the original pthread lock, used for lookups + * @dep_map: lockdep's dep_map structure + * @key: lockdep's key structure + * @node: rb-tree node used to store the lock in a global tree + * @name: a unique name for the lock + */ +struct lock_lookup { +	void *orig; /* Original pthread lock, used for lookups */ +	struct lockdep_map dep_map; /* Since all locks are dynamic, we need +				     * a dep_map and a key for each lock */ +	/* +	 * Wait, there's no support for key classes? Yup :( +	 * Most big projects wrap the pthread api with their own calls to +	 * be compatible with different locking methods. This means that +	 * "classes" will be brokes since the function that creates all +	 * locks will point to a generic locking function instead of the +	 * actual code that wants to do the locking. +	 */ +	struct lock_class_key key; +	struct rb_node node; +#define LIBLOCKDEP_MAX_LOCK_NAME 22 +	char name[LIBLOCKDEP_MAX_LOCK_NAME]; +}; + +/* This is where we store our locks */ +static struct rb_root locks = RB_ROOT; +static pthread_rwlock_t locks_rwlock = PTHREAD_RWLOCK_INITIALIZER; + +/* pthread mutex API */ + +#ifdef __GLIBC__ +extern int __pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr); +extern int __pthread_mutex_lock(pthread_mutex_t *mutex); +extern int __pthread_mutex_trylock(pthread_mutex_t *mutex); +extern int __pthread_mutex_unlock(pthread_mutex_t *mutex); +extern int __pthread_mutex_destroy(pthread_mutex_t *mutex); +#else +#define __pthread_mutex_init	NULL +#define __pthread_mutex_lock	NULL +#define __pthread_mutex_trylock	NULL +#define __pthread_mutex_unlock	NULL +#define __pthread_mutex_destroy	NULL +#endif +static int (*ll_pthread_mutex_init)(pthread_mutex_t *mutex, +			const pthread_mutexattr_t *attr)	= __pthread_mutex_init; +static int (*ll_pthread_mutex_lock)(pthread_mutex_t *mutex)	= __pthread_mutex_lock; +static int (*ll_pthread_mutex_trylock)(pthread_mutex_t *mutex)	= __pthread_mutex_trylock; +static int (*ll_pthread_mutex_unlock)(pthread_mutex_t *mutex)	= __pthread_mutex_unlock; +static int (*ll_pthread_mutex_destroy)(pthread_mutex_t *mutex)	= __pthread_mutex_destroy; + +/* pthread rwlock API */ + +#ifdef __GLIBC__ +extern int __pthread_rwlock_init(pthread_rwlock_t *rwlock, const pthread_rwlockattr_t *attr); +extern int __pthread_rwlock_destroy(pthread_rwlock_t *rwlock); +extern int __pthread_rwlock_wrlock(pthread_rwlock_t *rwlock); +extern int __pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock); +extern int __pthread_rwlock_rdlock(pthread_rwlock_t *rwlock); +extern int __pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock); +extern int __pthread_rwlock_unlock(pthread_rwlock_t *rwlock); +#else +#define __pthread_rwlock_init		NULL +#define __pthread_rwlock_destroy	NULL +#define __pthread_rwlock_wrlock		NULL +#define __pthread_rwlock_trywrlock	NULL +#define __pthread_rwlock_rdlock		NULL +#define __pthread_rwlock_tryrdlock	NULL +#define __pthread_rwlock_unlock		NULL +#endif + +static int (*ll_pthread_rwlock_init)(pthread_rwlock_t *rwlock, +			const pthread_rwlockattr_t *attr)		= __pthread_rwlock_init; +static int (*ll_pthread_rwlock_destroy)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_destroy; +static int (*ll_pthread_rwlock_rdlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_rdlock; +static int (*ll_pthread_rwlock_tryrdlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_tryrdlock; +static int (*ll_pthread_rwlock_trywrlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_trywrlock; +static int (*ll_pthread_rwlock_wrlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_wrlock; +static int (*ll_pthread_rwlock_unlock)(pthread_rwlock_t *rwlock)	= __pthread_rwlock_unlock; + +enum { none, prepare, done, } __init_state; +static void init_preload(void); +static void try_init_preload(void) +{ +	if (__init_state != done) +		init_preload(); +} + +static struct rb_node **__get_lock_node(void *lock, struct rb_node **parent) +{ +	struct rb_node **node = &locks.rb_node; +	struct lock_lookup *l; + +	*parent = NULL; + +	while (*node) { +		l = rb_entry(*node, struct lock_lookup, node); + +		*parent = *node; +		if (lock < l->orig) +			node = &l->node.rb_left; +		else if (lock > l->orig) +			node = &l->node.rb_right; +		else +			return node; +	} + +	return node; +} + +#ifndef LIBLOCKDEP_STATIC_ENTRIES +#define LIBLOCKDEP_STATIC_ENTRIES	1024 +#endif + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +static struct lock_lookup __locks[LIBLOCKDEP_STATIC_ENTRIES]; +static int __locks_nr; + +static inline bool is_static_lock(struct lock_lookup *lock) +{ +	return lock >= __locks && lock < __locks + ARRAY_SIZE(__locks); +} + +static struct lock_lookup *alloc_lock(void) +{ +	if (__init_state != done) { +		/* +		 * Some programs attempt to initialize and use locks in their +		 * allocation path. This means that a call to malloc() would +		 * result in locks being initialized and locked. +		 * +		 * Why is it an issue for us? dlsym() below will try allocating +		 * to give us the original function. Since this allocation will +		 * result in a locking operations, we have to let pthread deal +		 * with it, but we can't! we don't have the pointer to the +		 * original API since we're inside dlsym() trying to get it +		 */ + +		int idx = __locks_nr++; +		if (idx >= ARRAY_SIZE(__locks)) { +			fprintf(stderr, +		"LOCKDEP error: insufficient LIBLOCKDEP_STATIC_ENTRIES\n"); +			exit(EX_UNAVAILABLE); +		} +		return __locks + idx; +	} + +	return malloc(sizeof(struct lock_lookup)); +} + +static inline void free_lock(struct lock_lookup *lock) +{ +	if (likely(!is_static_lock(lock))) +		free(lock); +} + +/** + * __get_lock - find or create a lock instance + * @lock: pointer to a pthread lock function + * + * Try to find an existing lock in the rbtree using the provided pointer. If + * one wasn't found - create it. + */ +static struct lock_lookup *__get_lock(void *lock) +{ +	struct rb_node **node, *parent; +	struct lock_lookup *l; + +	ll_pthread_rwlock_rdlock(&locks_rwlock); +	node = __get_lock_node(lock, &parent); +	ll_pthread_rwlock_unlock(&locks_rwlock); +	if (*node) { +		return rb_entry(*node, struct lock_lookup, node); +	} + +	/* We didn't find the lock, let's create it */ +	l = alloc_lock(); +	if (l == NULL) +		return NULL; + +	l->orig = lock; +	/* +	 * Currently the name of the lock is the ptr value of the pthread lock, +	 * while not optimal, it makes debugging a bit easier. +	 * +	 * TODO: Get the real name of the lock using libdwarf +	 */ +	sprintf(l->name, "%p", lock); +	lockdep_init_map(&l->dep_map, l->name, &l->key, 0); + +	ll_pthread_rwlock_wrlock(&locks_rwlock); +	/* This might have changed since the last time we fetched it */ +	node = __get_lock_node(lock, &parent); +	rb_link_node(&l->node, parent, node); +	rb_insert_color(&l->node, &locks); +	ll_pthread_rwlock_unlock(&locks_rwlock); + +	return l; +} + +static void __del_lock(struct lock_lookup *lock) +{ +	ll_pthread_rwlock_wrlock(&locks_rwlock); +	rb_erase(&lock->node, &locks); +	ll_pthread_rwlock_unlock(&locks_rwlock); +	free_lock(lock); +} + +int pthread_mutex_init(pthread_mutex_t *mutex, +			const pthread_mutexattr_t *attr) +{ +	int r; + +	/* +	 * We keep trying to init our preload module because there might be +	 * code in init sections that tries to touch locks before we are +	 * initialized, in that case we'll need to manually call preload +	 * to get us going. +	 * +	 * Funny enough, kernel's lockdep had the same issue, and used +	 * (almost) the same solution. See look_up_lock_class() in +	 * kernel/locking/lockdep.c for details. +	 */ +	try_init_preload(); + +	r = ll_pthread_mutex_init(mutex, attr); +	if (r == 0) +		/* +		 * We do a dummy initialization here so that lockdep could +		 * warn us if something fishy is going on - such as +		 * initializing a held lock. +		 */ +		__get_lock(mutex); + +	return r; +} + +int pthread_mutex_lock(pthread_mutex_t *mutex) +{ +	int r; + +	try_init_preload(); + +	lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, +			(unsigned long)_RET_IP_); +	/* +	 * Here's the thing with pthread mutexes: unlike the kernel variant, +	 * they can fail. +	 * +	 * This means that the behaviour here is a bit different from what's +	 * going on in the kernel: there we just tell lockdep that we took the +	 * lock before actually taking it, but here we must deal with the case +	 * that locking failed. +	 * +	 * To do that we'll "release" the lock if locking failed - this way +	 * we'll get lockdep doing the correct checks when we try to take +	 * the lock, and if that fails - we'll be back to the correct +	 * state by releasing it. +	 */ +	r = ll_pthread_mutex_lock(mutex); +	if (r) +		lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_mutex_trylock(pthread_mutex_t *mutex) +{ +	int r; + +	try_init_preload(); + +	lock_acquire(&__get_lock(mutex)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_); +	r = ll_pthread_mutex_trylock(mutex); +	if (r) +		lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_mutex_unlock(pthread_mutex_t *mutex) +{ +	int r; + +	try_init_preload(); + +	lock_release(&__get_lock(mutex)->dep_map, 0, (unsigned long)_RET_IP_); +	/* +	 * Just like taking a lock, only in reverse! +	 * +	 * If we fail releasing the lock, tell lockdep we're holding it again. +	 */ +	r = ll_pthread_mutex_unlock(mutex); +	if (r) +		lock_acquire(&__get_lock(mutex)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_mutex_destroy(pthread_mutex_t *mutex) +{ +	try_init_preload(); + +	/* +	 * Let's see if we're releasing a lock that's held. +	 * +	 * TODO: Hook into free() and add that check there as well. +	 */ +	debug_check_no_locks_freed(mutex, mutex + sizeof(*mutex)); +	__del_lock(__get_lock(mutex)); +	return ll_pthread_mutex_destroy(mutex); +} + +/* This is the rwlock part, very similar to what happened with mutex above */ +int pthread_rwlock_init(pthread_rwlock_t *rwlock, +			const pthread_rwlockattr_t *attr) +{ +	int r; + +	try_init_preload(); + +	r = ll_pthread_rwlock_init(rwlock, attr); +	if (r == 0) +		__get_lock(rwlock); + +	return r; +} + +int pthread_rwlock_destroy(pthread_rwlock_t *rwlock) +{ +	try_init_preload(); + +	debug_check_no_locks_freed(rwlock, rwlock + sizeof(*rwlock)); +	__del_lock(__get_lock(rwlock)); +	return ll_pthread_rwlock_destroy(rwlock); +} + +int pthread_rwlock_rdlock(pthread_rwlock_t *rwlock) +{ +	int r; + +        init_preload(); + +	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 2, 1, NULL, (unsigned long)_RET_IP_); +	r = ll_pthread_rwlock_rdlock(rwlock); +	if (r) +		lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_rwlock_tryrdlock(pthread_rwlock_t *rwlock) +{ +	int r; + +        init_preload(); + +	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 2, 1, NULL, (unsigned long)_RET_IP_); +	r = ll_pthread_rwlock_tryrdlock(rwlock); +	if (r) +		lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_rwlock_trywrlock(pthread_rwlock_t *rwlock) +{ +	int r; + +        init_preload(); + +	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 1, 0, 1, NULL, (unsigned long)_RET_IP_); +	r = ll_pthread_rwlock_trywrlock(rwlock); +	if (r) +                lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_rwlock_wrlock(pthread_rwlock_t *rwlock) +{ +	int r; + +        init_preload(); + +	lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); +	r = ll_pthread_rwlock_wrlock(rwlock); +	if (r) +		lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); + +	return r; +} + +int pthread_rwlock_unlock(pthread_rwlock_t *rwlock) +{ +	int r; + +        init_preload(); + +	lock_release(&__get_lock(rwlock)->dep_map, 0, (unsigned long)_RET_IP_); +	r = ll_pthread_rwlock_unlock(rwlock); +	if (r) +		lock_acquire(&__get_lock(rwlock)->dep_map, 0, 0, 0, 1, NULL, (unsigned long)_RET_IP_); + +	return r; +} + +__attribute__((constructor)) static void init_preload(void) +{ +	if (__init_state == done) +		return; + +#ifndef __GLIBC__ +	__init_state = prepare; + +	ll_pthread_mutex_init = dlsym(RTLD_NEXT, "pthread_mutex_init"); +	ll_pthread_mutex_lock = dlsym(RTLD_NEXT, "pthread_mutex_lock"); +	ll_pthread_mutex_trylock = dlsym(RTLD_NEXT, "pthread_mutex_trylock"); +	ll_pthread_mutex_unlock = dlsym(RTLD_NEXT, "pthread_mutex_unlock"); +	ll_pthread_mutex_destroy = dlsym(RTLD_NEXT, "pthread_mutex_destroy"); + +	ll_pthread_rwlock_init = dlsym(RTLD_NEXT, "pthread_rwlock_init"); +	ll_pthread_rwlock_destroy = dlsym(RTLD_NEXT, "pthread_rwlock_destroy"); +	ll_pthread_rwlock_rdlock = dlsym(RTLD_NEXT, "pthread_rwlock_rdlock"); +	ll_pthread_rwlock_tryrdlock = dlsym(RTLD_NEXT, "pthread_rwlock_tryrdlock"); +	ll_pthread_rwlock_wrlock = dlsym(RTLD_NEXT, "pthread_rwlock_wrlock"); +	ll_pthread_rwlock_trywrlock = dlsym(RTLD_NEXT, "pthread_rwlock_trywrlock"); +	ll_pthread_rwlock_unlock = dlsym(RTLD_NEXT, "pthread_rwlock_unlock"); +#endif + +	lockdep_init(); + +	__init_state = done; +} diff --git a/tools/lib/lockdep/rbtree.c b/tools/lib/lockdep/rbtree.c new file mode 100644 index 00000000000..f7f43033c8b --- /dev/null +++ b/tools/lib/lockdep/rbtree.c @@ -0,0 +1 @@ +#include "../../../lib/rbtree.c" diff --git a/tools/lib/lockdep/run_tests.sh b/tools/lib/lockdep/run_tests.sh new file mode 100755 index 00000000000..5334ad9d39b --- /dev/null +++ b/tools/lib/lockdep/run_tests.sh @@ -0,0 +1,27 @@ +#! /bin/bash + +make &> /dev/null + +for i in `ls tests/*.c`; do +	testname=$(basename -s .c "$i") +	gcc -o tests/$testname -pthread -lpthread $i liblockdep.a -Iinclude -D__USE_LIBLOCKDEP &> /dev/null +	echo -ne "$testname... " +	if [ $(timeout 1 ./tests/$testname | wc -l) -gt 0 ]; then +		echo "PASSED!" +	else +		echo "FAILED!" +	fi +	rm tests/$testname +done + +for i in `ls tests/*.c`; do +	testname=$(basename -s .c "$i") +	gcc -o tests/$testname -pthread -lpthread -Iinclude $i &> /dev/null +	echo -ne "(PRELOAD) $testname... " +	if [ $(timeout 1 ./lockdep ./tests/$testname | wc -l) -gt 0 ]; then +		echo "PASSED!" +	else +		echo "FAILED!" +	fi +	rm tests/$testname +done diff --git a/tools/lib/lockdep/tests/AA.c b/tools/lib/lockdep/tests/AA.c new file mode 100644 index 00000000000..0f782ff404a --- /dev/null +++ b/tools/lib/lockdep/tests/AA.c @@ -0,0 +1,13 @@ +#include <liblockdep/mutex.h> + +void main(void) +{ +	pthread_mutex_t a, b; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); + +	pthread_mutex_lock(&a); +	pthread_mutex_lock(&b); +	pthread_mutex_lock(&a); +} diff --git a/tools/lib/lockdep/tests/ABBA.c b/tools/lib/lockdep/tests/ABBA.c new file mode 100644 index 00000000000..07f0e29d548 --- /dev/null +++ b/tools/lib/lockdep/tests/ABBA.c @@ -0,0 +1,13 @@ +#include <liblockdep/mutex.h> +#include "common.h" + +void main(void) +{ +	pthread_mutex_t a, b; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); + +	LOCK_UNLOCK_2(a, b); +	LOCK_UNLOCK_2(b, a); +} diff --git a/tools/lib/lockdep/tests/ABBCCA.c b/tools/lib/lockdep/tests/ABBCCA.c new file mode 100644 index 00000000000..843db09ac66 --- /dev/null +++ b/tools/lib/lockdep/tests/ABBCCA.c @@ -0,0 +1,15 @@ +#include <liblockdep/mutex.h> +#include "common.h" + +void main(void) +{ +	pthread_mutex_t a, b, c; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); +	pthread_mutex_init(&c, NULL); + +	LOCK_UNLOCK_2(a, b); +	LOCK_UNLOCK_2(b, c); +	LOCK_UNLOCK_2(c, a); +} diff --git a/tools/lib/lockdep/tests/ABBCCDDA.c b/tools/lib/lockdep/tests/ABBCCDDA.c new file mode 100644 index 00000000000..33620e268f8 --- /dev/null +++ b/tools/lib/lockdep/tests/ABBCCDDA.c @@ -0,0 +1,17 @@ +#include <liblockdep/mutex.h> +#include "common.h" + +void main(void) +{ +	pthread_mutex_t a, b, c, d; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); +	pthread_mutex_init(&c, NULL); +	pthread_mutex_init(&d, NULL); + +	LOCK_UNLOCK_2(a, b); +	LOCK_UNLOCK_2(b, c); +	LOCK_UNLOCK_2(c, d); +	LOCK_UNLOCK_2(d, a); +} diff --git a/tools/lib/lockdep/tests/ABCABC.c b/tools/lib/lockdep/tests/ABCABC.c new file mode 100644 index 00000000000..3fee51e3a68 --- /dev/null +++ b/tools/lib/lockdep/tests/ABCABC.c @@ -0,0 +1,15 @@ +#include <liblockdep/mutex.h> +#include "common.h" + +void main(void) +{ +	pthread_mutex_t a, b, c; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); +	pthread_mutex_init(&c, NULL); + +	LOCK_UNLOCK_2(a, b); +	LOCK_UNLOCK_2(c, a); +	LOCK_UNLOCK_2(b, c); +} diff --git a/tools/lib/lockdep/tests/ABCDBCDA.c b/tools/lib/lockdep/tests/ABCDBCDA.c new file mode 100644 index 00000000000..427ba562c75 --- /dev/null +++ b/tools/lib/lockdep/tests/ABCDBCDA.c @@ -0,0 +1,17 @@ +#include <liblockdep/mutex.h> +#include "common.h" + +void main(void) +{ +	pthread_mutex_t a, b, c, d; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); +	pthread_mutex_init(&c, NULL); +	pthread_mutex_init(&d, NULL); + +	LOCK_UNLOCK_2(a, b); +	LOCK_UNLOCK_2(c, d); +	LOCK_UNLOCK_2(b, c); +	LOCK_UNLOCK_2(d, a); +} diff --git a/tools/lib/lockdep/tests/ABCDBDDA.c b/tools/lib/lockdep/tests/ABCDBDDA.c new file mode 100644 index 00000000000..680c6cf3e91 --- /dev/null +++ b/tools/lib/lockdep/tests/ABCDBDDA.c @@ -0,0 +1,17 @@ +#include <liblockdep/mutex.h> +#include "common.h" + +void main(void) +{ +	pthread_mutex_t a, b, c, d; + +	pthread_mutex_init(&a, NULL); +	pthread_mutex_init(&b, NULL); +	pthread_mutex_init(&c, NULL); +	pthread_mutex_init(&d, NULL); + +	LOCK_UNLOCK_2(a, b); +	LOCK_UNLOCK_2(c, d); +	LOCK_UNLOCK_2(b, d); +	LOCK_UNLOCK_2(d, a); +} diff --git a/tools/lib/lockdep/tests/WW.c b/tools/lib/lockdep/tests/WW.c new file mode 100644 index 00000000000..d44f77d7102 --- /dev/null +++ b/tools/lib/lockdep/tests/WW.c @@ -0,0 +1,13 @@ +#include <liblockdep/rwlock.h> + +void main(void) +{ +	pthread_rwlock_t a, b; + +	pthread_rwlock_init(&a, NULL); +	pthread_rwlock_init(&b, NULL); + +	pthread_rwlock_wrlock(&a); +	pthread_rwlock_rdlock(&b); +	pthread_rwlock_wrlock(&a); +} diff --git a/tools/lib/lockdep/tests/common.h b/tools/lib/lockdep/tests/common.h new file mode 100644 index 00000000000..d89e94d47d8 --- /dev/null +++ b/tools/lib/lockdep/tests/common.h @@ -0,0 +1,12 @@ +#ifndef _LIBLOCKDEP_TEST_COMMON_H +#define _LIBLOCKDEP_TEST_COMMON_H + +#define LOCK_UNLOCK_2(a, b)			\ +	do {					\ +		pthread_mutex_lock(&(a));	\ +		pthread_mutex_lock(&(b));	\ +		pthread_mutex_unlock(&(b));	\ +		pthread_mutex_unlock(&(a));	\ +	} while(0) + +#endif diff --git a/tools/lib/lockdep/tests/unlock_balance.c b/tools/lib/lockdep/tests/unlock_balance.c new file mode 100644 index 00000000000..0bc62de686f --- /dev/null +++ b/tools/lib/lockdep/tests/unlock_balance.c @@ -0,0 +1,12 @@ +#include <liblockdep/mutex.h> + +void main(void) +{ +	pthread_mutex_t a; + +	pthread_mutex_init(&a, NULL); + +	pthread_mutex_lock(&a); +	pthread_mutex_unlock(&a); +	pthread_mutex_unlock(&a); +} diff --git a/tools/lib/lockdep/uinclude/asm/hash.h b/tools/lib/lockdep/uinclude/asm/hash.h new file mode 100644 index 00000000000..d82b170bb21 --- /dev/null +++ b/tools/lib/lockdep/uinclude/asm/hash.h @@ -0,0 +1,6 @@ +#ifndef __ASM_GENERIC_HASH_H +#define __ASM_GENERIC_HASH_H + +/* Stub */ + +#endif /* __ASM_GENERIC_HASH_H */ diff --git a/tools/lib/lockdep/uinclude/asm/hweight.h b/tools/lib/lockdep/uinclude/asm/hweight.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/asm/hweight.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/asm/sections.h b/tools/lib/lockdep/uinclude/asm/sections.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/asm/sections.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/bitops.h b/tools/lib/lockdep/uinclude/linux/bitops.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/bitops.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/compiler.h b/tools/lib/lockdep/uinclude/linux/compiler.h new file mode 100644 index 00000000000..7ac838a1f19 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/compiler.h @@ -0,0 +1,7 @@ +#ifndef _LIBLOCKDEP_LINUX_COMPILER_H_ +#define _LIBLOCKDEP_LINUX_COMPILER_H_ + +#define __used		__attribute__((__unused__)) +#define unlikely + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/debug_locks.h b/tools/lib/lockdep/uinclude/linux/debug_locks.h new file mode 100644 index 00000000000..f38eb64df79 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/debug_locks.h @@ -0,0 +1,12 @@ +#ifndef _LIBLOCKDEP_DEBUG_LOCKS_H_ +#define _LIBLOCKDEP_DEBUG_LOCKS_H_ + +#include <stddef.h> +#include <linux/compiler.h> + +#define DEBUG_LOCKS_WARN_ON(x) (x) + +extern bool debug_locks; +extern bool debug_locks_silent; + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/delay.h b/tools/lib/lockdep/uinclude/linux/delay.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/delay.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/ftrace.h b/tools/lib/lockdep/uinclude/linux/ftrace.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/ftrace.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/gfp.h b/tools/lib/lockdep/uinclude/linux/gfp.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/gfp.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/hardirq.h b/tools/lib/lockdep/uinclude/linux/hardirq.h new file mode 100644 index 00000000000..c8f3f8f5872 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/hardirq.h @@ -0,0 +1,11 @@ +#ifndef _LIBLOCKDEP_LINUX_HARDIRQ_H_ +#define _LIBLOCKDEP_LINUX_HARDIRQ_H_ + +#define SOFTIRQ_BITS	0UL +#define HARDIRQ_BITS	0UL +#define SOFTIRQ_SHIFT	0UL +#define HARDIRQ_SHIFT	0UL +#define hardirq_count()	0UL +#define softirq_count()	0UL + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/hash.h b/tools/lib/lockdep/uinclude/linux/hash.h new file mode 100644 index 00000000000..0f8479858dc --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/hash.h @@ -0,0 +1 @@ +#include "../../../include/linux/hash.h" diff --git a/tools/lib/lockdep/uinclude/linux/interrupt.h b/tools/lib/lockdep/uinclude/linux/interrupt.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/interrupt.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/irqflags.h b/tools/lib/lockdep/uinclude/linux/irqflags.h new file mode 100644 index 00000000000..6cc296f0fad --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/irqflags.h @@ -0,0 +1,38 @@ +#ifndef _LIBLOCKDEP_LINUX_TRACE_IRQFLAGS_H_ +#define _LIBLOCKDEP_LINUX_TRACE_IRQFLAGS_H_ + +# define trace_hardirq_context(p)	0 +# define trace_softirq_context(p)	0 +# define trace_hardirqs_enabled(p)	0 +# define trace_softirqs_enabled(p)	0 +# define trace_hardirq_enter()		do { } while (0) +# define trace_hardirq_exit()		do { } while (0) +# define lockdep_softirq_enter()	do { } while (0) +# define lockdep_softirq_exit()		do { } while (0) +# define INIT_TRACE_IRQFLAGS + +# define stop_critical_timings() do { } while (0) +# define start_critical_timings() do { } while (0) + +#define raw_local_irq_disable() do { } while (0) +#define raw_local_irq_enable() do { } while (0) +#define raw_local_irq_save(flags) ((flags) = 0) +#define raw_local_irq_restore(flags) do { } while (0) +#define raw_local_save_flags(flags) ((flags) = 0) +#define raw_irqs_disabled_flags(flags) do { } while (0) +#define raw_irqs_disabled() 0 +#define raw_safe_halt() + +#define local_irq_enable() do { } while (0) +#define local_irq_disable() do { } while (0) +#define local_irq_save(flags) ((flags) = 0) +#define local_irq_restore(flags) do { } while (0) +#define local_save_flags(flags)	((flags) = 0) +#define irqs_disabled() (1) +#define irqs_disabled_flags(flags) (0) +#define safe_halt() do { } while (0) + +#define trace_lock_release(x, y) +#define trace_lock_acquire(a, b, c, d, e, f, g) + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/kallsyms.h b/tools/lib/lockdep/uinclude/linux/kallsyms.h new file mode 100644 index 00000000000..b0f2dbdf1a1 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/kallsyms.h @@ -0,0 +1,32 @@ +#ifndef _LIBLOCKDEP_LINUX_KALLSYMS_H_ +#define _LIBLOCKDEP_LINUX_KALLSYMS_H_ + +#include <linux/kernel.h> +#include <stdio.h> + +#define KSYM_NAME_LEN 128 + +struct module; + +static inline const char *kallsyms_lookup(unsigned long addr, +					  unsigned long *symbolsize, +					  unsigned long *offset, +					  char **modname, char *namebuf) +{ +	return NULL; +} + +#include <execinfo.h> +#include <stdlib.h> +static inline void print_ip_sym(unsigned long ip) +{ +	char **name; + +	name = backtrace_symbols((void **)&ip, 1); + +	printf("%s\n", *name); + +	free(name); +} + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/kern_levels.h b/tools/lib/lockdep/uinclude/linux/kern_levels.h new file mode 100644 index 00000000000..3b9bade2869 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/kern_levels.h @@ -0,0 +1,25 @@ +#ifndef __KERN_LEVELS_H__ +#define __KERN_LEVELS_H__ + +#define KERN_SOH	""		/* ASCII Start Of Header */ +#define KERN_SOH_ASCII	'' + +#define KERN_EMERG	KERN_SOH ""	/* system is unusable */ +#define KERN_ALERT	KERN_SOH ""	/* action must be taken immediately */ +#define KERN_CRIT	KERN_SOH ""	/* critical conditions */ +#define KERN_ERR	KERN_SOH ""	/* error conditions */ +#define KERN_WARNING	KERN_SOH ""	/* warning conditions */ +#define KERN_NOTICE	KERN_SOH ""	/* normal but significant condition */ +#define KERN_INFO	KERN_SOH ""	/* informational */ +#define KERN_DEBUG	KERN_SOH ""	/* debug-level messages */ + +#define KERN_DEFAULT	KERN_SOH ""	/* the default kernel loglevel */ + +/* + * Annotation for a "continued" line of log printout (only done after a + * line that had no enclosing \n). Only to be used by core/arch code + * during early bootup (a continued line is not SMP-safe otherwise). + */ +#define KERN_CONT	"" + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/kernel.h b/tools/lib/lockdep/uinclude/linux/kernel.h new file mode 100644 index 00000000000..a11e3c357be --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/kernel.h @@ -0,0 +1,44 @@ +#ifndef _LIBLOCKDEP_LINUX_KERNEL_H_ +#define _LIBLOCKDEP_LINUX_KERNEL_H_ + +#include <linux/export.h> +#include <linux/types.h> +#include <linux/rcu.h> +#include <linux/hardirq.h> +#include <linux/kern_levels.h> + +#ifndef container_of +#define container_of(ptr, type, member) ({			\ +	const typeof(((type *)0)->member) * __mptr = (ptr);	\ +	(type *)((char *)__mptr - offsetof(type, member)); }) +#endif + +#define max(x, y) ({				\ +	typeof(x) _max1 = (x);			\ +	typeof(y) _max2 = (y);			\ +	(void) (&_max1 == &_max2);		\ +	_max1 > _max2 ? _max1 : _max2; }) + +#define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) +#define WARN_ON(x) (x) +#define WARN_ON_ONCE(x) (x) +#define likely(x) (x) +#define WARN(x, y, z) (x) +#define uninitialized_var(x) x +#define __init +#define noinline +#define list_add_tail_rcu list_add_tail + +#ifndef CALLER_ADDR0 +#define CALLER_ADDR0 ((unsigned long)__builtin_return_address(0)) +#endif + +#ifndef _RET_IP_ +#define _RET_IP_ CALLER_ADDR0 +#endif + +#ifndef _THIS_IP_ +#define _THIS_IP_ ({ __label__ __here; __here: (unsigned long)&&__here; }) +#endif + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/kmemcheck.h b/tools/lib/lockdep/uinclude/linux/kmemcheck.h new file mode 100644 index 00000000000..94d598bc6ab --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/kmemcheck.h @@ -0,0 +1,8 @@ +#ifndef _LIBLOCKDEP_LINUX_KMEMCHECK_H_ +#define _LIBLOCKDEP_LINUX_KMEMCHECK_H_ + +static inline void kmemcheck_mark_initialized(void *address, unsigned int n) +{ +} + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/linkage.h b/tools/lib/lockdep/uinclude/linux/linkage.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/linkage.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/list.h b/tools/lib/lockdep/uinclude/linux/list.h new file mode 100644 index 00000000000..6e9ef31ed82 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/list.h @@ -0,0 +1 @@ +#include "../../../include/linux/list.h" diff --git a/tools/lib/lockdep/uinclude/linux/lockdep.h b/tools/lib/lockdep/uinclude/linux/lockdep.h new file mode 100644 index 00000000000..c1552c28507 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/lockdep.h @@ -0,0 +1,58 @@ +#ifndef _LIBLOCKDEP_LOCKDEP_H_ +#define _LIBLOCKDEP_LOCKDEP_H_ + +#include <sys/prctl.h> +#include <sys/syscall.h> +#include <string.h> +#include <limits.h> +#include <linux/utsname.h> + + +#define MAX_LOCK_DEPTH 2000UL + +#define asmlinkage +#define __visible + +#include "../../../include/linux/lockdep.h" + +struct task_struct { +	u64 curr_chain_key; +	int lockdep_depth; +	unsigned int lockdep_recursion; +	struct held_lock held_locks[MAX_LOCK_DEPTH]; +	gfp_t lockdep_reclaim_gfp; +	int pid; +	char comm[17]; +}; + +extern struct task_struct *__curr(void); + +#define current (__curr()) + +#define debug_locks_off() 1 +#define task_pid_nr(tsk) ((tsk)->pid) + +#define KSYM_NAME_LEN 128 +#define printk printf + +#define list_del_rcu list_del + +#define atomic_t unsigned long +#define atomic_inc(x) ((*(x))++) + +static struct new_utsname *init_utsname(void) +{ +	static struct new_utsname n = (struct new_utsname) { +		.release = "liblockdep", +		.version = LIBLOCKDEP_VERSION, +	}; + +	return &n; +} + +#define print_tainted() "" +#define static_obj(x) 1 + +#define debug_show_all_locks() + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/module.h b/tools/lib/lockdep/uinclude/linux/module.h new file mode 100644 index 00000000000..09c7a7be8cc --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/module.h @@ -0,0 +1,6 @@ +#ifndef _LIBLOCKDEP_LINUX_MODULE_H_ +#define _LIBLOCKDEP_LINUX_MODULE_H_ + +#define module_param(name, type, perm) + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/mutex.h b/tools/lib/lockdep/uinclude/linux/mutex.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/mutex.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/poison.h b/tools/lib/lockdep/uinclude/linux/poison.h new file mode 100644 index 00000000000..0c27bdf1423 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/poison.h @@ -0,0 +1 @@ +#include "../../../include/linux/poison.h" diff --git a/tools/lib/lockdep/uinclude/linux/prefetch.h b/tools/lib/lockdep/uinclude/linux/prefetch.h new file mode 100644 index 00000000000..d73fe6f850a --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/prefetch.h @@ -0,0 +1,6 @@ +#ifndef _LIBLOCKDEP_LINUX_PREFETCH_H_ +#define _LIBLOCKDEP_LINUX_PREFETCH_H + +static inline void prefetch(void *a __attribute__((unused))) { } + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/proc_fs.h b/tools/lib/lockdep/uinclude/linux/proc_fs.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/proc_fs.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/rbtree.h b/tools/lib/lockdep/uinclude/linux/rbtree.h new file mode 100644 index 00000000000..965901db486 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/rbtree.h @@ -0,0 +1 @@ +#include "../../../include/linux/rbtree.h" diff --git a/tools/lib/lockdep/uinclude/linux/rbtree_augmented.h b/tools/lib/lockdep/uinclude/linux/rbtree_augmented.h new file mode 100644 index 00000000000..c3759477379 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/rbtree_augmented.h @@ -0,0 +1,2 @@ +#define __always_inline +#include "../../../include/linux/rbtree_augmented.h" diff --git a/tools/lib/lockdep/uinclude/linux/rcu.h b/tools/lib/lockdep/uinclude/linux/rcu.h new file mode 100644 index 00000000000..042ee8e463c --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/rcu.h @@ -0,0 +1,21 @@ +#ifndef _LIBLOCKDEP_RCU_H_ +#define _LIBLOCKDEP_RCU_H_ + +int rcu_scheduler_active; + +static inline int rcu_lockdep_current_cpu_online(void) +{ +	return 1; +} + +static inline int rcu_is_cpu_idle(void) +{ +	return 1; +} + +static inline bool rcu_is_watching(void) +{ +	return false; +} + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/seq_file.h b/tools/lib/lockdep/uinclude/linux/seq_file.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/seq_file.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/lockdep/uinclude/linux/spinlock.h b/tools/lib/lockdep/uinclude/linux/spinlock.h new file mode 100644 index 00000000000..68c1aa2bcba --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/spinlock.h @@ -0,0 +1,25 @@ +#ifndef _LIBLOCKDEP_SPINLOCK_H_ +#define _LIBLOCKDEP_SPINLOCK_H_ + +#include <pthread.h> +#include <stdbool.h> + +#define arch_spinlock_t pthread_mutex_t +#define __ARCH_SPIN_LOCK_UNLOCKED PTHREAD_MUTEX_INITIALIZER + +static inline void arch_spin_lock(arch_spinlock_t *mutex) +{ +	pthread_mutex_lock(mutex); +} + +static inline void arch_spin_unlock(arch_spinlock_t *mutex) +{ +	pthread_mutex_unlock(mutex); +} + +static inline bool arch_spin_is_locked(arch_spinlock_t *mutex) +{ +	return true; +} + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/stacktrace.h b/tools/lib/lockdep/uinclude/linux/stacktrace.h new file mode 100644 index 00000000000..39aecc6b19d --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/stacktrace.h @@ -0,0 +1,32 @@ +#ifndef _LIBLOCKDEP_LINUX_STACKTRACE_H_ +#define _LIBLOCKDEP_LINUX_STACKTRACE_H_ + +#include <execinfo.h> + +struct stack_trace { +	unsigned int nr_entries, max_entries; +	unsigned long *entries; +	int skip; +}; + +static inline void print_stack_trace(struct stack_trace *trace, int spaces) +{ +	backtrace_symbols_fd((void **)trace->entries, trace->nr_entries, 1); +} + +#define save_stack_trace(trace)	\ +	((trace)->nr_entries =	\ +		backtrace((void **)(trace)->entries, (trace)->max_entries)) + +static inline int dump_stack(void) +{ +	void *array[64]; +	size_t size; + +	size = backtrace(array, 64); +	backtrace_symbols_fd(array, size, 1); + +	return 0; +} + +#endif diff --git a/tools/lib/lockdep/uinclude/linux/stringify.h b/tools/lib/lockdep/uinclude/linux/stringify.h new file mode 100644 index 00000000000..05dfcd1ac11 --- /dev/null +++ b/tools/lib/lockdep/uinclude/linux/stringify.h @@ -0,0 +1,7 @@ +#ifndef _LIBLOCKDEP_LINUX_STRINGIFY_H_ +#define _LIBLOCKDEP_LINUX_STRINGIFY_H_ + +#define __stringify_1(x...)	#x +#define __stringify(x...)	__stringify_1(x) + +#endif diff --git a/tools/lib/lockdep/uinclude/trace/events/lock.h b/tools/lib/lockdep/uinclude/trace/events/lock.h new file mode 100644 index 00000000000..fab00ff936d --- /dev/null +++ b/tools/lib/lockdep/uinclude/trace/events/lock.h @@ -0,0 +1,3 @@ + +/* empty file */ + diff --git a/tools/lib/symbol/kallsyms.c b/tools/lib/symbol/kallsyms.c new file mode 100644 index 00000000000..18bc271a4bb --- /dev/null +++ b/tools/lib/symbol/kallsyms.c @@ -0,0 +1,58 @@ +#include "symbol/kallsyms.h" +#include <stdio.h> +#include <stdlib.h> + +int kallsyms__parse(const char *filename, void *arg, +		    int (*process_symbol)(void *arg, const char *name, +					  char type, u64 start)) +{ +	char *line = NULL; +	size_t n; +	int err = -1; +	FILE *file = fopen(filename, "r"); + +	if (file == NULL) +		goto out_failure; + +	err = 0; + +	while (!feof(file)) { +		u64 start; +		int line_len, len; +		char symbol_type; +		char *symbol_name; + +		line_len = getline(&line, &n, file); +		if (line_len < 0 || !line) +			break; + +		line[--line_len] = '\0'; /* \n */ + +		len = hex2u64(line, &start); + +		len++; +		if (len + 2 >= line_len) +			continue; + +		symbol_type = line[len]; +		len += 2; +		symbol_name = line + len; +		len = line_len - len; + +		if (len >= KSYM_NAME_LEN) { +			err = -1; +			break; +		} + +		err = process_symbol(arg, symbol_name, symbol_type, start); +		if (err) +			break; +	} + +	free(line); +	fclose(file); +	return err; + +out_failure: +	return -1; +} diff --git a/tools/lib/symbol/kallsyms.h b/tools/lib/symbol/kallsyms.h new file mode 100644 index 00000000000..6084f5e18b3 --- /dev/null +++ b/tools/lib/symbol/kallsyms.h @@ -0,0 +1,24 @@ +#ifndef __TOOLS_KALLSYMS_H_ +#define __TOOLS_KALLSYMS_H_ 1 + +#include <elf.h> +#include <linux/ctype.h> +#include <linux/types.h> + +#ifndef KSYM_NAME_LEN +#define KSYM_NAME_LEN 256 +#endif + +static inline u8 kallsyms2elf_type(char type) +{ +	if (type == 'W') +		return STB_WEAK; + +	return isupper(type) ? STB_GLOBAL : STB_LOCAL; +} + +int kallsyms__parse(const char *filename, void *arg, +		    int (*process_symbol)(void *arg, const char *name, +					  char type, u64 start)); + +#endif /* __TOOLS_KALLSYMS_H_ */ diff --git a/tools/lib/traceevent/Makefile b/tools/lib/traceevent/Makefile index ca6cb779876..005c9cc0693 100644 --- a/tools/lib/traceevent/Makefile +++ b/tools/lib/traceevent/Makefile @@ -43,6 +43,32 @@ man_dir_SQ = '$(subst ','\'',$(man_dir))'  export man_dir man_dir_SQ INSTALL  export DESTDIR DESTDIR_SQ +set_plugin_dir := 1 + +# Set plugin_dir to preffered global plugin location +# If we install under $HOME directory we go under +# $(HOME)/.traceevent/plugins +# +# We dont set PLUGIN_DIR in case we install under $HOME +# directory, because by default the code looks under: +# $(HOME)/.traceevent/plugins by default. +# +ifeq ($(plugin_dir),) +ifeq ($(prefix),$(HOME)) +override plugin_dir = $(HOME)/.traceevent/plugins +set_plugin_dir := 0 +else +override plugin_dir = $(prefix)/lib/traceevent/plugins +endif +endif + +ifeq ($(set_plugin_dir),1) +PLUGIN_DIR = -DPLUGIN_DIR="$(plugin_dir)" +PLUGIN_DIR_SQ = '$(subst ','\'',$(PLUGIN_DIR))' +endif + +include $(if $(BUILD_SRC),$(BUILD_SRC)/)../../scripts/Makefile.include +  # copy a bit from Linux kbuild  ifeq ("$(origin V)", "command line") @@ -57,18 +83,13 @@ ifeq ("$(origin O)", "command line")  endif  ifeq ($(BUILD_SRC),) -ifneq ($(BUILD_OUTPUT),) +ifneq ($(OUTPUT),)  define build_output -	$(if $(VERBOSE:1=),@)+$(MAKE) -C $(BUILD_OUTPUT) 	\ -	BUILD_SRC=$(CURDIR) -f $(CURDIR)/Makefile $1 +  $(if $(VERBOSE:1=),@)+$(MAKE) -C $(OUTPUT) \ +  BUILD_SRC=$(CURDIR)/ -f $(CURDIR)/Makefile $1  endef -saved-output := $(BUILD_OUTPUT) -BUILD_OUTPUT := $(shell cd $(BUILD_OUTPUT) && /bin/pwd) -$(if $(BUILD_OUTPUT),, \ -     $(error output directory "$(saved-output)" does not exist)) -  all: sub-make  $(MAKECMDGOALS): sub-make @@ -80,7 +101,7 @@ sub-make: force  # Leave processing to above invocation of make  skip-makefile := 1 -endif # BUILD_OUTPUT +endif # OUTPUT  endif # BUILD_SRC  # We process the rest of the Makefile if this is the final invocation of make @@ -96,6 +117,7 @@ export prefix bindir src obj  # Shell quotes  bindir_SQ = $(subst ','\'',$(bindir))  bindir_relative_SQ = $(subst ','\'',$(bindir_relative)) +plugin_dir_SQ = $(subst ','\'',$(plugin_dir))  LIB_FILE = libtraceevent.a libtraceevent.so @@ -114,7 +136,7 @@ export Q VERBOSE  EVENT_PARSE_VERSION = $(EP_VERSION).$(EP_PATCHLEVEL).$(EP_EXTRAVERSION) -INCLUDES = -I. $(CONFIG_INCLUDES) +INCLUDES = -I. -I $(srctree)/../../include $(CONFIG_INCLUDES)  # Set compile option CFLAGS if not set elsewhere  CFLAGS ?= -g -Wall @@ -125,41 +147,14 @@ override CFLAGS += $(udis86-flags) -D_GNU_SOURCE  ifeq ($(VERBOSE),1)    Q = -  print_compile = -  print_app_build = -  print_fpic_compile = -  print_shared_lib_compile = -  print_plugin_obj_compile = -  print_plugin_build = -  print_install =  else    Q = @ -  print_compile =		echo '  CC                 '$(OBJ); -  print_app_build =		echo '  BUILD              '$(OBJ); -  print_fpic_compile =		echo '  CC FPIC            '$(OBJ); -  print_shared_lib_compile =	echo '  BUILD SHARED LIB   '$(OBJ); -  print_plugin_obj_compile =	echo '  CC PLUGIN OBJ      '$(OBJ); -  print_plugin_build =		echo '  CC PLUGI           '$(OBJ); -  print_static_lib_build =	echo '  BUILD STATIC LIB   '$(OBJ); -  print_install =		echo '  INSTALL     '$1'	to	$(DESTDIR_SQ)$2';  endif -do_fpic_compile =					\ -	($(print_fpic_compile)				\ -	$(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@) - -do_app_build =						\ -	($(print_app_build)				\ -	$(CC) $^ -rdynamic -o $@ $(CONFIG_LIBS) $(LIBS)) -  do_compile_shared_library =			\  	($(print_shared_lib_compile)		\  	$(CC) --shared $^ -o $@) -do_compile_plugin_obj =				\ -	($(print_plugin_obj_compile)		\ -	$(CC) -c $(CFLAGS) -fPIC -o $@ $<) -  do_plugin_build =				\  	($(print_plugin_build)			\  	$(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<) @@ -169,23 +164,37 @@ do_build_static_lib =				\  	$(RM) $@;  $(AR) rcs $@ $^) -define do_compile -	$(print_compile)						\ -	$(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@; -endef +do_compile = $(QUIET_CC)$(CC) -c $(CFLAGS) $(EXT) $< -o $(obj)/$@;  $(obj)/%.o: $(src)/%.c -	$(Q)$(call do_compile) +	$(call do_compile)  %.o: $(src)/%.c -	$(Q)$(call do_compile) +	$(call do_compile) -PEVENT_LIB_OBJS = event-parse.o trace-seq.o parse-filter.o parse-utils.o +PEVENT_LIB_OBJS  = event-parse.o +PEVENT_LIB_OBJS += event-plugin.o +PEVENT_LIB_OBJS += trace-seq.o +PEVENT_LIB_OBJS += parse-filter.o +PEVENT_LIB_OBJS += parse-utils.o  PEVENT_LIB_OBJS += kbuffer-parse.o -ALL_OBJS = $(PEVENT_LIB_OBJS) +PLUGIN_OBJS  = plugin_jbd2.o +PLUGIN_OBJS += plugin_hrtimer.o +PLUGIN_OBJS += plugin_kmem.o +PLUGIN_OBJS += plugin_kvm.o +PLUGIN_OBJS += plugin_mac80211.o +PLUGIN_OBJS += plugin_sched_switch.o +PLUGIN_OBJS += plugin_function.o +PLUGIN_OBJS += plugin_xen.o +PLUGIN_OBJS += plugin_scsi.o +PLUGIN_OBJS += plugin_cfg80211.o + +PLUGINS := $(PLUGIN_OBJS:.o=.so) + +ALL_OBJS = $(PEVENT_LIB_OBJS) $(PLUGIN_OBJS) -CMD_TARGETS = $(LIB_FILE) +CMD_TARGETS = $(LIB_FILE) $(PLUGINS)  TARGETS = $(CMD_TARGETS) @@ -195,32 +204,40 @@ all: all_cmd  all_cmd: $(CMD_TARGETS)  libtraceevent.so: $(PEVENT_LIB_OBJS) -	$(Q)$(do_compile_shared_library) +	$(QUIET_LINK)$(CC) --shared $^ -o $@  libtraceevent.a: $(PEVENT_LIB_OBJS) -	$(Q)$(do_build_static_lib) +	$(QUIET_LINK)$(RM) $@; $(AR) rcs $@ $^ + +plugins: $(PLUGINS)  $(PEVENT_LIB_OBJS): %.o: $(src)/%.c TRACEEVENT-CFLAGS -	$(Q)$(do_fpic_compile) +	$(QUIET_CC_FPIC)$(CC) -c $(CFLAGS) $(EXT) -fPIC $< -o $@ + +$(PLUGIN_OBJS): %.o : $(src)/%.c +	$(QUIET_CC_FPIC)$(CC) -c $(CFLAGS) -fPIC -o $@ $< + +$(PLUGINS): %.so: %.o +	$(QUIET_LINK)$(CC) $(CFLAGS) -shared -nostartfiles -o $@ $<  define make_version.h -	(echo '/* This file is automatically generated. Do not modify. */';		\ -	echo \#define VERSION_CODE $(shell						\ -	expr $(VERSION) \* 256 + $(PATCHLEVEL));					\ -	echo '#define EXTRAVERSION ' $(EXTRAVERSION);					\ -	echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"';	\ -	echo '#define FILE_VERSION '$(FILE_VERSION);					\ -	) > $1 +  (echo '/* This file is automatically generated. Do not modify. */';		\ +   echo \#define VERSION_CODE $(shell						\ +   expr $(VERSION) \* 256 + $(PATCHLEVEL));					\ +   echo '#define EXTRAVERSION ' $(EXTRAVERSION);				\ +   echo '#define VERSION_STRING "'$(VERSION).$(PATCHLEVEL).$(EXTRAVERSION)'"';	\ +   echo '#define FILE_VERSION '$(FILE_VERSION);					\ +  ) > $1  endef  define update_version.h -	($(call make_version.h, $@.tmp);		\ -	if [ -r $@ ] && cmp -s $@ $@.tmp; then		\ -		rm -f $@.tmp;				\ -	else						\ -		echo '  UPDATE                 $@';	\ -		mv -f $@.tmp $@;			\ -	fi); +  ($(call make_version.h, $@.tmp);		\ +    if [ -r $@ ] && cmp -s $@ $@.tmp; then	\ +      rm -f $@.tmp;				\ +    else					\ +      echo '  UPDATE                 $@';	\ +      mv -f $@.tmp $@;				\ +    fi);  endef  ep_version.h: force @@ -229,13 +246,13 @@ ep_version.h: force  VERSION_FILES = ep_version.h  define update_dir -	(echo $1 > $@.tmp;	\ -	if [ -r $@ ] && cmp -s $@ $@.tmp; then		\ -		rm -f $@.tmp;				\ -	else						\ -		echo '  UPDATE                 $@';	\ -		mv -f $@.tmp $@;			\ -	fi); +  (echo $1 > $@.tmp;				\ +   if [ -r $@ ] && cmp -s $@ $@.tmp; then	\ +     rm -f $@.tmp;				\ +   else						\ +     echo '  UPDATE                 $@';	\ +     mv -f $@.tmp $@;				\ +   fi);  endef  ## make deps @@ -245,10 +262,10 @@ all_deps := $(all_objs:%.o=.%.d)  # let .d file also depends on the source and header files  define check_deps -		@set -e; $(RM) $@; \ -		$(CC) -MM $(CFLAGS) $< > $@.$$$$; \ -		sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ -		$(RM) $@.$$$$ +  @set -e; $(RM) $@; \ +  $(CC) -MM $(CFLAGS) $< > $@.$$$$; \ +  sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \ +  $(RM) $@.$$$$  endef  $(all_deps): .%.d: $(src)/%.c @@ -268,7 +285,7 @@ TRACK_CFLAGS = $(subst ','\'',$(CFLAGS)):$(ARCH):$(CROSS_COMPILE)  TRACEEVENT-CFLAGS: force  	@FLAGS='$(TRACK_CFLAGS)'; \  	    if test x"$$FLAGS" != x"`cat TRACEEVENT-CFLAGS 2>/dev/null`" ; then \ -		echo 1>&2 "    * new build flags or cross compiler"; \ +		echo 1>&2 "  FLAGS:   * new build flags or cross compiler"; \  		echo "$$FLAGS" >TRACEEVENT-CFLAGS; \              fi @@ -283,27 +300,41 @@ TAGS:	force  	--regex='/_PE(\([^,)]*\).*/PEVENT_ERRNO__\1/'  define do_install -	$(print_install)				\  	if [ ! -d '$(DESTDIR_SQ)$2' ]; then		\  		$(INSTALL) -d -m 755 '$(DESTDIR_SQ)$2';	\  	fi;						\  	$(INSTALL) $1 '$(DESTDIR_SQ)$2'  endef -install_lib: all_cmd -	$(Q)$(call do_install,$(LIB_FILE),$(bindir_SQ)) +define do_install_plugins +	for plugin in $1; do				\ +	  $(call do_install,$$plugin,$(plugin_dir_SQ));	\ +	done +endef + +install_lib: all_cmd install_plugins +	$(call QUIET_INSTALL, $(LIB_FILE)) \ +		$(call do_install,$(LIB_FILE),$(bindir_SQ)) + +install_plugins: $(PLUGINS) +	$(call QUIET_INSTALL, trace_plugins) \ +		$(call do_install_plugins, $(PLUGINS))  install: install_lib  clean: -	$(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d -	$(RM) TRACEEVENT-CFLAGS tags TAGS +	$(call QUIET_CLEAN, libtraceevent) \ +		$(RM) *.o *~ $(TARGETS) *.a *.so $(VERSION_FILES) .*.d \ +		$(RM) TRACEEVENT-CFLAGS tags TAGS  endif # skip-makefile -PHONY += force +PHONY += force plugins  force: +plugins: +	@echo > /dev/null +  # Declare the contents of the .PHONY variable as phony.  We keep that  # information in a variable so we can use it in if_changed and friends.  .PHONY: $(PHONY) diff --git a/tools/lib/traceevent/event-parse.c b/tools/lib/traceevent/event-parse.c index d1c2a6a4cd3..93825a17dcc 100644 --- a/tools/lib/traceevent/event-parse.c +++ b/tools/lib/traceevent/event-parse.c @@ -50,6 +50,18 @@ static int show_warning = 1;  			warning(fmt, ##__VA_ARGS__);	\  	} while (0) +#define do_warning_event(event, fmt, ...)			\ +	do {							\ +		if (!show_warning)				\ +			continue;				\ +								\ +		if (event)					\ +			warning("[%s:%s] " fmt, event->system,	\ +				event->name, ##__VA_ARGS__);	\ +		else						\ +			warning(fmt, ##__VA_ARGS__);		\ +	} while (0) +  static void init_input_buf(const char *buf, unsigned long long size)  {  	input_buf = buf; @@ -305,6 +317,11 @@ int pevent_register_comm(struct pevent *pevent, const char *comm, int pid)  	return 0;  } +void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock) +{ +	pevent->trace_clock = trace_clock; +} +  struct func_map {  	unsigned long long		addr;  	char				*func; @@ -599,10 +616,11 @@ find_printk(struct pevent *pevent, unsigned long long addr)   * This registers a string by the address it was stored in the kernel.   * The @fmt passed in is duplicated.   */ -int pevent_register_print_string(struct pevent *pevent, char *fmt, +int pevent_register_print_string(struct pevent *pevent, const char *fmt,  				 unsigned long long addr)  {  	struct printk_list *item = malloc(sizeof(*item)); +	char *p;  	if (!item)  		return -1; @@ -610,10 +628,21 @@ int pevent_register_print_string(struct pevent *pevent, char *fmt,  	item->next = pevent->printklist;  	item->addr = addr; +	/* Strip off quotes and '\n' from the end */ +	if (fmt[0] == '"') +		fmt++;  	item->printk = strdup(fmt);  	if (!item->printk)  		goto out_free; +	p = item->printk + strlen(item->printk) - 1; +	if (*p == '"') +		*p = 0; + +	p -= 2; +	if (strcmp(p, "\\n") == 0) +		*p = 0; +  	pevent->printklist = item;  	pevent->printk_count++; @@ -736,6 +765,9 @@ static void free_arg(struct print_arg *arg)  	case PRINT_BSTRING:  		free(arg->string.string);  		break; +	case PRINT_BITMASK: +		free(arg->bitmask.bitmask); +		break;  	case PRINT_DYNAMIC_ARRAY:  		free(arg->dynarray.index);  		break; @@ -1338,7 +1370,7 @@ static int event_read_fields(struct event_format *event, struct format_field **f  		}  		if (!field->type) { -			do_warning("%s: no type found", __func__); +			do_warning_event(event, "%s: no type found", __func__);  			goto fail;  		}  		field->name = last_token; @@ -1385,7 +1417,7 @@ static int event_read_fields(struct event_format *event, struct format_field **f  				free_token(token);  				type = read_token(&token);  				if (type == EVENT_NONE) { -					do_warning("failed to find token"); +					do_warning_event(event, "failed to find token");  					goto fail;  				}  			} @@ -1589,6 +1621,24 @@ process_arg(struct event_format *event, struct print_arg *arg, char **tok)  static enum event_type  process_op(struct event_format *event, struct print_arg *arg, char **tok); +/* + * For __print_symbolic() and __print_flags, we need to completely + * evaluate the first argument, which defines what to print next. + */ +static enum event_type +process_field_arg(struct event_format *event, struct print_arg *arg, char **tok) +{ +	enum event_type type; + +	type = process_arg(event, arg, tok); + +	while (type == EVENT_OP) { +		type = process_op(event, arg, tok); +	} + +	return type; +} +  static enum event_type  process_cond(struct event_format *event, struct print_arg *top, char **tok)  { @@ -1601,7 +1651,7 @@ process_cond(struct event_format *event, struct print_arg *top, char **tok)  	right = alloc_arg();  	if (!arg || !left || !right) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		/* arg will be freed at out_free */  		free_arg(left);  		free_arg(right); @@ -1651,7 +1701,7 @@ process_array(struct event_format *event, struct print_arg *top, char **tok)  	arg = alloc_arg();  	if (!arg) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		/* '*tok' is set to top->op.op.  No need to free. */  		*tok = NULL;  		return EVENT_ERROR; @@ -1757,7 +1807,7 @@ process_op(struct event_format *event, struct print_arg *arg, char **tok)  	if (arg->type == PRINT_OP && !arg->op.left) {  		/* handle single op */  		if (token[1]) { -			do_warning("bad op token %s", token); +			do_warning_event(event, "bad op token %s", token);  			goto out_free;  		}  		switch (token[0]) { @@ -1767,7 +1817,7 @@ process_op(struct event_format *event, struct print_arg *arg, char **tok)  		case '-':  			break;  		default: -			do_warning("bad op token %s", token); +			do_warning_event(event, "bad op token %s", token);  			goto out_free;  		} @@ -1853,7 +1903,7 @@ process_op(struct event_format *event, struct print_arg *arg, char **tok)  			char *new_atom;  			if (left->type != PRINT_ATOM) { -				do_warning("bad pointer type"); +				do_warning_event(event, "bad pointer type");  				goto out_free;  			}  			new_atom = realloc(left->atom.atom, @@ -1895,7 +1945,7 @@ process_op(struct event_format *event, struct print_arg *arg, char **tok)  		type = process_array(event, arg, tok);  	} else { -		do_warning("unknown op '%s'", token); +		do_warning_event(event, "unknown op '%s'", token);  		event->flags |= EVENT_FL_FAILED;  		/* the arg is now the left side */  		goto out_free; @@ -1916,7 +1966,7 @@ process_op(struct event_format *event, struct print_arg *arg, char **tok)  	return type;  out_warn_free: -	do_warning("%s: not enough memory!", __func__); +	do_warning_event(event, "%s: not enough memory!", __func__);  out_free:  	free_token(token);  	*tok = NULL; @@ -2221,6 +2271,7 @@ static int arg_num_eval(struct print_arg *arg, long long *val)  	case PRINT_FIELD ... PRINT_SYMBOL:  	case PRINT_STRING:  	case PRINT_BSTRING: +	case PRINT_BITMASK:  	default:  		do_warning("invalid eval type %d", arg->type);  		ret = 0; @@ -2249,6 +2300,7 @@ static char *arg_eval (struct print_arg *arg)  	case PRINT_FIELD ... PRINT_SYMBOL:  	case PRINT_STRING:  	case PRINT_BSTRING: +	case PRINT_BITMASK:  	default:  		do_warning("invalid eval type %d", arg->type);  		break; @@ -2350,11 +2402,11 @@ process_flags(struct event_format *event, struct print_arg *arg, char **tok)  	field = alloc_arg();  	if (!field) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		goto out_free;  	} -	type = process_arg(event, field, &token); +	type = process_field_arg(event, field, &token);  	/* Handle operations in the first argument */  	while (type == EVENT_OP) @@ -2403,11 +2455,12 @@ process_symbols(struct event_format *event, struct print_arg *arg, char **tok)  	field = alloc_arg();  	if (!field) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		goto out_free;  	} -	type = process_arg(event, field, &token); +	type = process_field_arg(event, field, &token); +  	if (test_type_token(type, token, EVENT_DELIM, ","))  		goto out_free_field; @@ -2441,7 +2494,7 @@ process_hex(struct event_format *event, struct print_arg *arg, char **tok)  	field = alloc_arg();  	if (!field) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		goto out_free;  	} @@ -2456,7 +2509,7 @@ process_hex(struct event_format *event, struct print_arg *arg, char **tok)  	field = alloc_arg();  	if (!field) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		*tok = NULL;  		return EVENT_ERROR;  	} @@ -2519,7 +2572,7 @@ process_dynamic_array(struct event_format *event, struct print_arg *arg, char **  	free_token(token);  	arg = alloc_arg();  	if (!arg) { -		do_warning("%s: not enough memory!", __func__); +		do_warning_event(event, "%s: not enough memory!", __func__);  		*tok = NULL;  		return EVENT_ERROR;  	} @@ -2578,13 +2631,14 @@ process_paren(struct event_format *event, struct print_arg *arg, char **tok)  		/* prevous must be an atom */  		if (arg->type != PRINT_ATOM) { -			do_warning("previous needed to be PRINT_ATOM"); +			do_warning_event(event, "previous needed to be PRINT_ATOM");  			goto out_free;  		}  		item_arg = alloc_arg();  		if (!item_arg) { -			do_warning("%s: not enough memory!", __func__); +			do_warning_event(event, "%s: not enough memory!", +					 __func__);  			goto out_free;  		} @@ -2634,6 +2688,35 @@ process_str(struct event_format *event __maybe_unused, struct print_arg *arg,  	return EVENT_ERROR;  } +static enum event_type +process_bitmask(struct event_format *event __maybe_unused, struct print_arg *arg, +	    char **tok) +{ +	enum event_type type; +	char *token; + +	if (read_expect_type(EVENT_ITEM, &token) < 0) +		goto out_free; + +	arg->type = PRINT_BITMASK; +	arg->bitmask.bitmask = token; +	arg->bitmask.offset = -1; + +	if (read_expected(EVENT_DELIM, ")") < 0) +		goto out_err; + +	type = read_token(&token); +	*tok = token; + +	return type; + + out_free: +	free_token(token); + out_err: +	*tok = NULL; +	return EVENT_ERROR; +} +  static struct pevent_function_handler *  find_func_handler(struct pevent *pevent, char *func_name)  { @@ -2674,7 +2757,6 @@ process_func_handler(struct event_format *event, struct pevent_function_handler  	struct print_arg *farg;  	enum event_type type;  	char *token; -	const char *test;  	int i;  	arg->type = PRINT_FUNC; @@ -2686,20 +2768,27 @@ process_func_handler(struct event_format *event, struct pevent_function_handler  	for (i = 0; i < func->nr_args; i++) {  		farg = alloc_arg();  		if (!farg) { -			do_warning("%s: not enough memory!", __func__); +			do_warning_event(event, "%s: not enough memory!", +					 __func__);  			return EVENT_ERROR;  		}  		type = process_arg(event, farg, &token); -		if (i < (func->nr_args - 1)) -			test = ","; -		else -			test = ")"; - -		if (test_type_token(type, token, EVENT_DELIM, test)) { -			free_arg(farg); -			free_token(token); -			return EVENT_ERROR; +		if (i < (func->nr_args - 1)) { +			if (type != EVENT_DELIM || strcmp(token, ",") != 0) { +				do_warning_event(event, +					"Error: function '%s()' expects %d arguments but event %s only uses %d", +					func->name, func->nr_args, +					event->name, i + 1); +				goto err; +			} +		} else { +			if (type != EVENT_DELIM || strcmp(token, ")") != 0) { +				do_warning_event(event, +					"Error: function '%s()' only expects %d arguments but event %s has more", +					func->name, func->nr_args, event->name); +				goto err; +			}  		}  		*next_arg = farg; @@ -2711,6 +2800,11 @@ process_func_handler(struct event_format *event, struct pevent_function_handler  	*tok = token;  	return type; + +err: +	free_arg(farg); +	free_token(token); +	return EVENT_ERROR;  }  static enum event_type @@ -2737,6 +2831,10 @@ process_function(struct event_format *event, struct print_arg *arg,  		free_token(token);  		return process_str(event, arg, tok);  	} +	if (strcmp(token, "__get_bitmask") == 0) { +		free_token(token); +		return process_bitmask(event, arg, tok); +	}  	if (strcmp(token, "__get_dynamic_array") == 0) {  		free_token(token);  		return process_dynamic_array(event, arg, tok); @@ -2748,7 +2846,7 @@ process_function(struct event_format *event, struct print_arg *arg,  		return process_func_handler(event, func, arg, tok);  	} -	do_warning("function %s not defined", token); +	do_warning_event(event, "function %s not defined", token);  	free_token(token);  	return EVENT_ERROR;  } @@ -2834,7 +2932,7 @@ process_arg_token(struct event_format *event, struct print_arg *arg,  	case EVENT_ERROR ... EVENT_NEWLINE:  	default: -		do_warning("unexpected type %d", type); +		do_warning_event(event, "unexpected type %d", type);  		return EVENT_ERROR;  	}  	*tok = token; @@ -2857,7 +2955,8 @@ static int event_read_print_args(struct event_format *event, struct print_arg **  		arg = alloc_arg();  		if (!arg) { -			do_warning("%s: not enough memory!", __func__); +			do_warning_event(event, "%s: not enough memory!", +					 __func__);  			return -1;  		} @@ -3263,6 +3362,7 @@ eval_num_arg(void *data, int size, struct event_format *event, struct print_arg  		return eval_type(val, arg, 0);  	case PRINT_STRING:  	case PRINT_BSTRING: +	case PRINT_BITMASK:  		return 0;  	case PRINT_FUNC: {  		struct trace_seq s; @@ -3418,17 +3518,31 @@ eval_num_arg(void *data, int size, struct event_format *event, struct print_arg  			goto out_warning_op;  		}  		break; +	case PRINT_DYNAMIC_ARRAY: +		/* Without [], we pass the address to the dynamic data */ +		offset = pevent_read_number(pevent, +					    data + arg->dynarray.field->offset, +					    arg->dynarray.field->size); +		/* +		 * The actual length of the dynamic array is stored +		 * in the top half of the field, and the offset +		 * is in the bottom half of the 32 bit field. +		 */ +		offset &= 0xffff; +		val = (unsigned long long)((unsigned long)data + offset); +		break;  	default: /* not sure what to do there */  		return 0;  	}  	return val;  out_warning_op: -	do_warning("%s: unknown op '%s'", __func__, arg->op.op); +	do_warning_event(event, "%s: unknown op '%s'", __func__, arg->op.op);  	return 0;  out_warning_field: -	do_warning("%s: field %s not found", __func__, arg->field.name); +	do_warning_event(event, "%s: field %s not found", +			 __func__, arg->field.name);  	return 0;  } @@ -3481,6 +3595,60 @@ static void print_str_to_seq(struct trace_seq *s, const char *format,  		trace_seq_printf(s, format, str);  } +static void print_bitmask_to_seq(struct pevent *pevent, +				 struct trace_seq *s, const char *format, +				 int len_arg, const void *data, int size) +{ +	int nr_bits = size * 8; +	int str_size = (nr_bits + 3) / 4; +	int len = 0; +	char buf[3]; +	char *str; +	int index; +	int i; + +	/* +	 * The kernel likes to put in commas every 32 bits, we +	 * can do the same. +	 */ +	str_size += (nr_bits - 1) / 32; + +	str = malloc(str_size + 1); +	if (!str) { +		do_warning("%s: not enough memory!", __func__); +		return; +	} +	str[str_size] = 0; + +	/* Start out with -2 for the two chars per byte */ +	for (i = str_size - 2; i >= 0; i -= 2) { +		/* +		 * data points to a bit mask of size bytes. +		 * In the kernel, this is an array of long words, thus +		 * endianess is very important. +		 */ +		if (pevent->file_bigendian) +			index = size - (len + 1); +		else +			index = len; + +		snprintf(buf, 3, "%02x", *((unsigned char *)data + index)); +		memcpy(str + i, buf, 2); +		len++; +		if (!(len & 3) && i > 0) { +			i--; +			str[i] = ','; +		} +	} + +	if (len_arg >= 0) +		trace_seq_printf(s, format, len_arg, str); +	else +		trace_seq_printf(s, format, str); + +	free(str); +} +  static void print_str_arg(struct trace_seq *s, void *data, int size,  			  struct event_format *event, const char *format,  			  int len_arg, struct print_arg *arg) @@ -3488,6 +3656,7 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,  	struct pevent *pevent = event->pevent;  	struct print_flag_sym *flag;  	struct format_field *field; +	struct printk_map *printk;  	unsigned long long val, fval;  	unsigned long addr;  	char *str; @@ -3523,12 +3692,18 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,  		if (!(field->flags & FIELD_IS_ARRAY) &&  		    field->size == pevent->long_size) {  			addr = *(unsigned long *)(data + field->offset); -			trace_seq_printf(s, "%lx", addr); +			/* Check if it matches a print format */ +			printk = find_printk(pevent, addr); +			if (printk) +				trace_seq_puts(s, printk->printk); +			else +				trace_seq_printf(s, "%lx", addr);  			break;  		}  		str = malloc(len + 1);  		if (!str) { -			do_warning("%s: not enough memory!", __func__); +			do_warning_event(event, "%s: not enough memory!", +					 __func__);  			return;  		}  		memcpy(str, data + field->offset, len); @@ -3565,15 +3740,23 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,  		}  		break;  	case PRINT_HEX: -		field = arg->hex.field->field.field; -		if (!field) { -			str = arg->hex.field->field.name; -			field = pevent_find_any_field(event, str); -			if (!field) -				goto out_warning_field; -			arg->hex.field->field.field = field; +		if (arg->hex.field->type == PRINT_DYNAMIC_ARRAY) { +			unsigned long offset; +			offset = pevent_read_number(pevent, +				data + arg->hex.field->dynarray.field->offset, +				arg->hex.field->dynarray.field->size); +			hex = data + (offset & 0xffff); +		} else { +			field = arg->hex.field->field.field; +			if (!field) { +				str = arg->hex.field->field.name; +				field = pevent_find_any_field(event, str); +				if (!field) +					goto out_warning_field; +				arg->hex.field->field.field = field; +			} +			hex = data + field->offset;  		} -		hex = data + field->offset;  		len = eval_num_arg(data, size, event, arg->hex.size);  		for (i = 0; i < len; i++) {  			if (i) @@ -3601,6 +3784,23 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,  	case PRINT_BSTRING:  		print_str_to_seq(s, format, len_arg, arg->string.string);  		break; +	case PRINT_BITMASK: { +		int bitmask_offset; +		int bitmask_size; + +		if (arg->bitmask.offset == -1) { +			struct format_field *f; + +			f = pevent_find_any_field(event, arg->bitmask.bitmask); +			arg->bitmask.offset = f->offset; +		} +		bitmask_offset = data2host4(pevent, data + arg->bitmask.offset); +		bitmask_size = bitmask_offset >> 16; +		bitmask_offset &= 0xffff; +		print_bitmask_to_seq(pevent, s, format, len_arg, +				     data + bitmask_offset, bitmask_size); +		break; +	}  	case PRINT_OP:  		/*  		 * The only op for string should be ? : @@ -3626,7 +3826,8 @@ static void print_str_arg(struct trace_seq *s, void *data, int size,  	return;  out_warning_field: -	do_warning("%s: field %s not found", __func__, arg->field.name); +	do_warning_event(event, "%s: field %s not found", +			 __func__, arg->field.name);  }  static unsigned long long @@ -3671,14 +3872,16 @@ process_defined_func(struct trace_seq *s, void *data, int size,  			trace_seq_terminate(&str);  			string = malloc(sizeof(*string));  			if (!string) { -				do_warning("%s(%d): malloc str", __func__, __LINE__); +				do_warning_event(event, "%s(%d): malloc str", +						 __func__, __LINE__);  				goto out_free;  			}  			string->next = strings;  			string->str = strdup(str.buffer);  			if (!string->str) {  				free(string); -				do_warning("%s(%d): malloc str", __func__, __LINE__); +				do_warning_event(event, "%s(%d): malloc str", +						 __func__, __LINE__);  				goto out_free;  			}  			args[i] = (uintptr_t)string->str; @@ -3690,7 +3893,7 @@ process_defined_func(struct trace_seq *s, void *data, int size,  			 * Something went totally wrong, this is not  			 * an input error, something in this code broke.  			 */ -			do_warning("Unexpected end of arguments\n"); +			do_warning_event(event, "Unexpected end of arguments\n");  			goto out_free;  		}  		farg = farg->next; @@ -3740,12 +3943,12 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc  	if (!field) {  		field = pevent_find_field(event, "buf");  		if (!field) { -			do_warning("can't find buffer field for binary printk"); +			do_warning_event(event, "can't find buffer field for binary printk");  			return NULL;  		}  		ip_field = pevent_find_field(event, "ip");  		if (!ip_field) { -			do_warning("can't find ip field for binary printk"); +			do_warning_event(event, "can't find ip field for binary printk");  			return NULL;  		}  		pevent->bprint_buf_field = field; @@ -3759,7 +3962,8 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc  	 */  	args = alloc_arg();  	if (!args) { -		do_warning("%s(%d): not enough memory!", __func__, __LINE__); +		do_warning_event(event, "%s(%d): not enough memory!", +				 __func__, __LINE__);  		return NULL;  	}  	arg = args; @@ -3771,8 +3975,8 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc  	if (asprintf(&arg->atom.atom, "%lld", ip) < 0)  		goto out_free; -	/* skip the first "%pf : " */ -	for (ptr = fmt + 6, bptr = data + field->offset; +	/* skip the first "%pf: " */ +	for (ptr = fmt + 5, bptr = data + field->offset;  	     bptr < data + size && *ptr; ptr++) {  		int ls = 0; @@ -3825,7 +4029,7 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc  				bptr += vsize;  				arg = alloc_arg();  				if (!arg) { -					do_warning("%s(%d): not enough memory!", +					do_warning_event(event, "%s(%d): not enough memory!",  						   __func__, __LINE__);  					goto out_free;  				} @@ -3848,7 +4052,7 @@ static struct print_arg *make_bprint_args(char *fmt, void *data, int size, struc  			case 's':  				arg = alloc_arg();  				if (!arg) { -					do_warning("%s(%d): not enough memory!", +					do_warning_event(event, "%s(%d): not enough memory!",  						   __func__, __LINE__);  					goto out_free;  				} @@ -3882,14 +4086,13 @@ get_bprint_format(void *data, int size __maybe_unused,  	struct format_field *field;  	struct printk_map *printk;  	char *format; -	char *p;  	field = pevent->bprint_fmt_field;  	if (!field) {  		field = pevent_find_field(event, "fmt");  		if (!field) { -			do_warning("can't find format field for binary printk"); +			do_warning_event(event, "can't find format field for binary printk");  			return NULL;  		}  		pevent->bprint_fmt_field = field; @@ -3899,25 +4102,13 @@ get_bprint_format(void *data, int size __maybe_unused,  	printk = find_printk(pevent, addr);  	if (!printk) { -		if (asprintf(&format, "%%pf : (NO FORMAT FOUND at %llx)\n", addr) < 0) +		if (asprintf(&format, "%%pf: (NO FORMAT FOUND at %llx)\n", addr) < 0)  			return NULL;  		return format;  	} -	p = printk->printk; -	/* Remove any quotes. */ -	if (*p == '"') -		p++; -	if (asprintf(&format, "%s : %s", "%pf", p) < 0) +	if (asprintf(&format, "%s: %s", "%pf", printk->printk) < 0)  		return NULL; -	/* remove ending quotes and new line since we will add one too */ -	p = format + strlen(format) - 1; -	if (*p == '"') -		*p = 0; - -	p -= 2; -	if (strcmp(p, "\\n") == 0) -		*p = 0;  	return format;  } @@ -3945,8 +4136,8 @@ static void print_mac_arg(struct trace_seq *s, int mac, void *data, int size,  		arg->field.field =  			pevent_find_any_field(event, arg->field.name);  		if (!arg->field.field) { -			do_warning("%s: field %s not found", -				   __func__, arg->field.name); +			do_warning_event(event, "%s: field %s not found", +					 __func__, arg->field.name);  			return;  		}  	} @@ -3963,7 +4154,7 @@ static int is_printable_array(char *p, unsigned int len)  	unsigned int i;  	for (i = 0; i < len && p[i]; i++) -		if (!isprint(p[i])) +		if (!isprint(p[i]) && !isspace(p[i]))  		    return 0;  	return 1;  } @@ -4049,6 +4240,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  	unsigned long long val;  	struct func_map *func;  	const char *saveptr; +	struct trace_seq p;  	char *bprint_fmt = NULL;  	char format[32];  	int show_func; @@ -4117,7 +4309,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  			case '*':  				/* The argument is the length. */  				if (!arg) { -					do_warning("no argument match"); +					do_warning_event(event, "no argument match");  					event->flags |= EVENT_FL_FAILED;  					goto out_failed;  				} @@ -4154,7 +4346,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  			case 'X':  			case 'u':  				if (!arg) { -					do_warning("no argument match"); +					do_warning_event(event, "no argument match");  					event->flags |= EVENT_FL_FAILED;  					goto out_failed;  				} @@ -4164,7 +4356,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  				/* should never happen */  				if (len > 31) { -					do_warning("bad format!"); +					do_warning_event(event, "bad format!");  					event->flags |= EVENT_FL_FAILED;  					len = 31;  				} @@ -4231,13 +4423,13 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  						trace_seq_printf(s, format, (long long)val);  					break;  				default: -					do_warning("bad count (%d)", ls); +					do_warning_event(event, "bad count (%d)", ls);  					event->flags |= EVENT_FL_FAILED;  				}  				break;  			case 's':  				if (!arg) { -					do_warning("no matching argument"); +					do_warning_event(event, "no matching argument");  					event->flags |= EVENT_FL_FAILED;  					goto out_failed;  				} @@ -4247,7 +4439,7 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  				/* should never happen */  				if (len > 31) { -					do_warning("bad format!"); +					do_warning_event(event, "bad format!");  					event->flags |= EVENT_FL_FAILED;  					len = 31;  				} @@ -4256,8 +4448,13 @@ static void pretty_print(struct trace_seq *s, void *data, int size, struct event  				format[len] = 0;  				if (!len_as_arg)  					len_arg = -1; -				print_str_arg(s, data, size, event, +				/* Use helper trace_seq */ +				trace_seq_init(&p); +				print_str_arg(&p, data, size, event,  					      format, len_arg, arg); +				trace_seq_terminate(&p); +				trace_seq_puts(s, p.buffer); +				trace_seq_destroy(&p);  				arg = arg->next;  				break;  			default: @@ -4428,11 +4625,11 @@ void pevent_event_info(struct trace_seq *s, struct event_format *event,  {  	int print_pretty = 1; -	if (event->pevent->print_raw) +	if (event->pevent->print_raw || (event->flags & EVENT_FL_PRINTRAW))  		print_event_fields(s, record->data, record->size, event);  	else { -		if (event->handler) +		if (event->handler && !(event->flags & EVENT_FL_NOHANDLE))  			print_pretty = event->handler(s, record, event,  						      event->context); @@ -4443,8 +4640,21 @@ void pevent_event_info(struct trace_seq *s, struct event_format *event,  	trace_seq_terminate(s);  } +static bool is_timestamp_in_us(char *trace_clock, bool use_trace_clock) +{ +	if (!use_trace_clock) +		return true; + +	if (!strcmp(trace_clock, "local") || !strcmp(trace_clock, "global") +	    || !strcmp(trace_clock, "uptime") || !strcmp(trace_clock, "perf")) +		return true; + +	/* trace_clock is setting in tsc or counter mode */ +	return false; +} +  void pevent_print_event(struct pevent *pevent, struct trace_seq *s, -			struct pevent_record *record) +			struct pevent_record *record, bool use_trace_clock)  {  	static const char *spaces = "                    "; /* 20 spaces */  	struct event_format *event; @@ -4457,9 +4667,14 @@ void pevent_print_event(struct pevent *pevent, struct trace_seq *s,  	int pid;  	int len;  	int p; +	bool use_usec_format; -	secs = record->ts / NSECS_PER_SEC; -	nsecs = record->ts - secs * NSECS_PER_SEC; +	use_usec_format = is_timestamp_in_us(pevent->trace_clock, +							use_trace_clock); +	if (use_usec_format) { +		secs = record->ts / NSECS_PER_SEC; +		nsecs = record->ts - secs * NSECS_PER_SEC; +	}  	if (record->size < 0) {  		do_warning("ug! negative record size %d", record->size); @@ -4484,15 +4699,20 @@ void pevent_print_event(struct pevent *pevent, struct trace_seq *s,  	} else  		trace_seq_printf(s, "%16s-%-5d [%03d]", comm, pid, record->cpu); -	if (pevent->flags & PEVENT_NSEC_OUTPUT) { -		usecs = nsecs; -		p = 9; -	} else { -		usecs = (nsecs + 500) / NSECS_PER_USEC; -		p = 6; -	} +	if (use_usec_format) { +		if (pevent->flags & PEVENT_NSEC_OUTPUT) { +			usecs = nsecs; +			p = 9; +		} else { +			usecs = (nsecs + 500) / NSECS_PER_USEC; +			p = 6; +		} -	trace_seq_printf(s, " %5lu.%0*lu: %s: ", secs, p, usecs, event->name); +		trace_seq_printf(s, " %5lu.%0*lu: %s: ", +					secs, p, usecs, event->name); +	} else +		trace_seq_printf(s, " %12llu: %s: ", +					record->ts, event->name);  	/* Space out the event names evenly. */  	len = strlen(event->name); @@ -4712,6 +4932,9 @@ static void print_args(struct print_arg *args)  	case PRINT_BSTRING:  		printf("__get_str(%s)", args->string.string);  		break; +	case PRINT_BITMASK: +		printf("__get_bitmask(%s)", args->bitmask.bitmask); +		break;  	case PRINT_TYPE:  		printf("(%s)", args->typecast.type);  		print_args(args->typecast.item); @@ -5043,8 +5266,38 @@ enum pevent_errno __pevent_parse_format(struct event_format **eventp,  	return ret;  } +static enum pevent_errno +__pevent_parse_event(struct pevent *pevent, +		     struct event_format **eventp, +		     const char *buf, unsigned long size, +		     const char *sys) +{ +	int ret = __pevent_parse_format(eventp, pevent, buf, size, sys); +	struct event_format *event = *eventp; + +	if (event == NULL) +		return ret; + +	if (pevent && add_event(pevent, event)) { +		ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; +		goto event_add_failed; +	} + +#define PRINT_ARGS 0 +	if (PRINT_ARGS && event->print_fmt.args) +		print_args(event->print_fmt.args); + +	return 0; + +event_add_failed: +	pevent_free_format(event); +	return ret; +} +  /**   * pevent_parse_format - parse the event format + * @pevent: the handle to the pevent + * @eventp: returned format   * @buf: the buffer storing the event format string   * @size: the size of @buf   * @sys: the system the event belongs to @@ -5056,10 +5309,12 @@ enum pevent_errno __pevent_parse_format(struct event_format **eventp,   *   * /sys/kernel/debug/tracing/events/.../.../format   */ -enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf, +enum pevent_errno pevent_parse_format(struct pevent *pevent, +				      struct event_format **eventp, +				      const char *buf,  				      unsigned long size, const char *sys)  { -	return __pevent_parse_format(eventp, NULL, buf, size, sys); +	return __pevent_parse_event(pevent, eventp, buf, size, sys);  }  /** @@ -5080,25 +5335,7 @@ enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,  				     unsigned long size, const char *sys)  {  	struct event_format *event = NULL; -	int ret = __pevent_parse_format(&event, pevent, buf, size, sys); - -	if (event == NULL) -		return ret; - -	if (add_event(pevent, event)) { -		ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; -		goto event_add_failed; -	} - -#define PRINT_ARGS 0 -	if (PRINT_ARGS && event->print_fmt.args) -		print_args(event->print_fmt.args); - -	return 0; - -event_add_failed: -	pevent_free_format(event); -	return ret; +	return __pevent_parse_event(pevent, &event, buf, size, sys);  }  #undef _PE @@ -5130,22 +5367,7 @@ int pevent_strerror(struct pevent *pevent __maybe_unused,  	idx = errnum - __PEVENT_ERRNO__START - 1;  	msg = pevent_error_str[idx]; - -	switch (errnum) { -	case PEVENT_ERRNO__MEM_ALLOC_FAILED: -	case PEVENT_ERRNO__PARSE_EVENT_FAILED: -	case PEVENT_ERRNO__READ_ID_FAILED: -	case PEVENT_ERRNO__READ_FORMAT_FAILED: -	case PEVENT_ERRNO__READ_PRINT_FAILED: -	case PEVENT_ERRNO__OLD_FTRACE_ARG_FAILED: -	case PEVENT_ERRNO__INVALID_ARG_TYPE: -		snprintf(buf, buflen, "%s", msg); -		break; - -	default: -		/* cannot reach here */ -		break; -	} +	snprintf(buf, buflen, "%s", msg);  	return 0;  } @@ -5326,6 +5548,48 @@ int pevent_print_num_field(struct trace_seq *s, const char *fmt,  	return -1;  } +/** + * pevent_print_func_field - print a field and a format for function pointers + * @s: The seq to print to + * @fmt: The printf format to print the field with. + * @event: the event that the field is for + * @name: The name of the field + * @record: The record with the field name. + * @err: print default error if failed. + * + * Returns: 0 on success, -1 field not found, or 1 if buffer is full. + */ +int pevent_print_func_field(struct trace_seq *s, const char *fmt, +			    struct event_format *event, const char *name, +			    struct pevent_record *record, int err) +{ +	struct format_field *field = pevent_find_field(event, name); +	struct pevent *pevent = event->pevent; +	unsigned long long val; +	struct func_map *func; +	char tmp[128]; + +	if (!field) +		goto failed; + +	if (pevent_read_number_field(field, record->data, &val)) +		goto failed; + +	func = find_func(pevent, val); + +	if (func) +		snprintf(tmp, 128, "%s/0x%llx", func->func, func->addr - val); +	else +		sprintf(tmp, "0x%08llx", val); + +	return trace_seq_printf(s, fmt, tmp); + + failed: +	if (err) +		trace_seq_printf(s, "CAN'T FIND FIELD \"%s\"", name); +	return -1; +} +  static void free_func_handle(struct pevent_function_handler *func)  {  	struct pevent_func_params *params; @@ -5434,6 +5698,52 @@ int pevent_register_print_function(struct pevent *pevent,  }  /** + * pevent_unregister_print_function - unregister a helper function + * @pevent: the handle to the pevent + * @func: the function to process the helper function + * @name: the name of the helper function + * + * This function removes existing print handler for function @name. + * + * Returns 0 if the handler was removed successully, -1 otherwise. + */ +int pevent_unregister_print_function(struct pevent *pevent, +				     pevent_func_handler func, char *name) +{ +	struct pevent_function_handler *func_handle; + +	func_handle = find_func_handler(pevent, name); +	if (func_handle && func_handle->func == func) { +		remove_func_handler(pevent, name); +		return 0; +	} +	return -1; +} + +static struct event_format *pevent_search_event(struct pevent *pevent, int id, +						const char *sys_name, +						const char *event_name) +{ +	struct event_format *event; + +	if (id >= 0) { +		/* search by id */ +		event = pevent_find_event(pevent, id); +		if (!event) +			return NULL; +		if (event_name && (strcmp(event_name, event->name) != 0)) +			return NULL; +		if (sys_name && (strcmp(sys_name, event->system) != 0)) +			return NULL; +	} else { +		event = pevent_find_event_by_name(pevent, sys_name, event_name); +		if (!event) +			return NULL; +	} +	return event; +} + +/**   * pevent_register_event_handler - register a way to parse an event   * @pevent: the handle to the pevent   * @id: the id of the event to register @@ -5457,20 +5767,9 @@ int pevent_register_event_handler(struct pevent *pevent, int id,  	struct event_format *event;  	struct event_handler *handle; -	if (id >= 0) { -		/* search by id */ -		event = pevent_find_event(pevent, id); -		if (!event) -			goto not_found; -		if (event_name && (strcmp(event_name, event->name) != 0)) -			goto not_found; -		if (sys_name && (strcmp(sys_name, event->system) != 0)) -			goto not_found; -	} else { -		event = pevent_find_event_by_name(pevent, sys_name, event_name); -		if (!event) -			goto not_found; -	} +	event = pevent_search_event(pevent, id, sys_name, event_name); +	if (event == NULL) +		goto not_found;  	pr_stat("overriding event (%d) %s:%s with new print handler",  		event->id, event->system, event->name); @@ -5510,6 +5809,79 @@ int pevent_register_event_handler(struct pevent *pevent, int id,  	return -1;  } +static int handle_matches(struct event_handler *handler, int id, +			  const char *sys_name, const char *event_name, +			  pevent_event_handler_func func, void *context) +{ +	if (id >= 0 && id != handler->id) +		return 0; + +	if (event_name && (strcmp(event_name, handler->event_name) != 0)) +		return 0; + +	if (sys_name && (strcmp(sys_name, handler->sys_name) != 0)) +		return 0; + +	if (func != handler->func || context != handler->context) +		return 0; + +	return 1; +} + +/** + * pevent_unregister_event_handler - unregister an existing event handler + * @pevent: the handle to the pevent + * @id: the id of the event to unregister + * @sys_name: the system name the handler belongs to + * @event_name: the name of the event handler + * @func: the function to call to parse the event information + * @context: the data to be passed to @func + * + * This function removes existing event handler (parser). + * + * If @id is >= 0, then it is used to find the event. + * else @sys_name and @event_name are used. + * + * Returns 0 if handler was removed successfully, -1 if event was not found. + */ +int pevent_unregister_event_handler(struct pevent *pevent, int id, +				    const char *sys_name, const char *event_name, +				    pevent_event_handler_func func, void *context) +{ +	struct event_format *event; +	struct event_handler *handle; +	struct event_handler **next; + +	event = pevent_search_event(pevent, id, sys_name, event_name); +	if (event == NULL) +		goto not_found; + +	if (event->handler == func && event->context == context) { +		pr_stat("removing override handler for event (%d) %s:%s. Going back to default handler.", +			event->id, event->system, event->name); + +		event->handler = NULL; +		event->context = NULL; +		return 0; +	} + +not_found: +	for (next = &pevent->handlers; *next; next = &(*next)->next) { +		handle = *next; +		if (handle_matches(handle, id, sys_name, event_name, +				   func, context)) +			break; +	} + +	if (!(*next)) +		return -1; + +	*next = handle->next; +	free_handler(handle); + +	return 0; +} +  /**   * pevent_alloc - create a pevent handle   */ diff --git a/tools/lib/traceevent/event-parse.h b/tools/lib/traceevent/event-parse.h index c37b2026d04..7a3873ff9a4 100644 --- a/tools/lib/traceevent/event-parse.h +++ b/tools/lib/traceevent/event-parse.h @@ -20,8 +20,10 @@  #ifndef _PARSE_EVENTS_H  #define _PARSE_EVENTS_H +#include <stdbool.h>  #include <stdarg.h>  #include <regex.h> +#include <string.h>  #ifndef __maybe_unused  #define __maybe_unused __attribute__((unused)) @@ -56,6 +58,12 @@ struct pevent_record {  #endif  }; +enum trace_seq_fail { +	TRACE_SEQ__GOOD, +	TRACE_SEQ__BUFFER_POISONED, +	TRACE_SEQ__MEM_ALLOC_FAILED, +}; +  /*   * Trace sequences are used to allow a function to call several other functions   * to create a string of data to use (up to a max of PAGE_SIZE). @@ -66,6 +74,7 @@ struct trace_seq {  	unsigned int		buffer_size;  	unsigned int		len;  	unsigned int		readpos; +	enum trace_seq_fail	state;  };  void trace_seq_init(struct trace_seq *s); @@ -96,10 +105,10 @@ typedef int (*pevent_event_handler_func)(struct trace_seq *s,  					 void *context);  typedef int (*pevent_plugin_load_func)(struct pevent *pevent); -typedef int (*pevent_plugin_unload_func)(void); +typedef int (*pevent_plugin_unload_func)(struct pevent *pevent); -struct plugin_option { -	struct plugin_option		*next; +struct pevent_plugin_option { +	struct pevent_plugin_option	*next;  	void				*handle;  	char				*file;  	char				*name; @@ -121,12 +130,12 @@ struct plugin_option {   * PEVENT_PLUGIN_UNLOADER:  (optional)   *   The function called just before unloading   * - *   int PEVENT_PLUGIN_UNLOADER(void) + *   int PEVENT_PLUGIN_UNLOADER(struct pevent *pevent)   *   * PEVENT_PLUGIN_OPTIONS:  (optional)   *   Plugin options that can be set before loading   * - *   struct plugin_option PEVENT_PLUGIN_OPTIONS[] = { + *   struct pevent_plugin_option PEVENT_PLUGIN_OPTIONS[] = {   *	{   *		.name = "option-name",   *		.plugin_alias = "overide-file-name", (optional) @@ -199,6 +208,11 @@ struct print_arg_string {  	int			offset;  }; +struct print_arg_bitmask { +	char			*bitmask; +	int			offset; +}; +  struct print_arg_field {  	char			*name;  	struct format_field	*field; @@ -265,6 +279,7 @@ enum print_arg_type {  	PRINT_DYNAMIC_ARRAY,  	PRINT_OP,  	PRINT_FUNC, +	PRINT_BITMASK,  };  struct print_arg { @@ -279,6 +294,7 @@ struct print_arg {  		struct print_arg_hex		hex;  		struct print_arg_func		func;  		struct print_arg_string		string; +		struct print_arg_bitmask	bitmask;  		struct print_arg_op		op;  		struct print_arg_dynarray	dynarray;  	}; @@ -307,6 +323,8 @@ enum {  	EVENT_FL_ISBPRINT	= 0x04,  	EVENT_FL_ISFUNCENT	= 0x10,  	EVENT_FL_ISFUNCRET	= 0x20, +	EVENT_FL_NOHANDLE	= 0x40, +	EVENT_FL_PRINTRAW	= 0x80,  	EVENT_FL_FAILED		= 0x80000000  }; @@ -343,6 +361,8 @@ enum pevent_func_arg_type {  enum pevent_flag {  	PEVENT_NSEC_OUTPUT		= 1,	/* output in NSECS */ +	PEVENT_DISABLE_SYS_PLUGINS	= 1 << 1, +	PEVENT_DISABLE_PLUGINS		= 1 << 2,  };  #define PEVENT_ERRORS 							      \ @@ -352,12 +372,35 @@ enum pevent_flag {  	_PE(READ_FORMAT_FAILED,	"failed to read event format"),		      \  	_PE(READ_PRINT_FAILED,	"failed to read event print fmt"), 	      \  	_PE(OLD_FTRACE_ARG_FAILED,"failed to allocate field name for ftrace"),\ -	_PE(INVALID_ARG_TYPE,	"invalid argument type") +	_PE(INVALID_ARG_TYPE,	"invalid argument type"),		      \ +	_PE(INVALID_EXP_TYPE,	"invalid expression type"),		      \ +	_PE(INVALID_OP_TYPE,	"invalid operator type"),		      \ +	_PE(INVALID_EVENT_NAME,	"invalid event name"),			      \ +	_PE(EVENT_NOT_FOUND,	"no event found"),			      \ +	_PE(SYNTAX_ERROR,	"syntax error"),			      \ +	_PE(ILLEGAL_RVALUE,	"illegal rvalue"),			      \ +	_PE(ILLEGAL_LVALUE,	"illegal lvalue for string comparison"),      \ +	_PE(INVALID_REGEX,	"regex did not compute"),		      \ +	_PE(ILLEGAL_STRING_CMP,	"illegal comparison for string"), 	      \ +	_PE(ILLEGAL_INTEGER_CMP,"illegal comparison for integer"), 	      \ +	_PE(REPARENT_NOT_OP,	"cannot reparent other than OP"),	      \ +	_PE(REPARENT_FAILED,	"failed to reparent filter OP"),	      \ +	_PE(BAD_FILTER_ARG,	"bad arg in filter tree"),		      \ +	_PE(UNEXPECTED_TYPE,	"unexpected type (not a value)"),	      \ +	_PE(ILLEGAL_TOKEN,	"illegal token"),			      \ +	_PE(INVALID_PAREN,	"open parenthesis cannot come here"), 	      \ +	_PE(UNBALANCED_PAREN,	"unbalanced number of parenthesis"),	      \ +	_PE(UNKNOWN_TOKEN,	"unknown token"),			      \ +	_PE(FILTER_NOT_FOUND,	"no filter found"),			      \ +	_PE(NOT_A_NUMBER,	"must have number field"),		      \ +	_PE(NO_FILTER,		"no filters exists"),			      \ +	_PE(FILTER_MISS,	"record does not match to filter")  #undef _PE  #define _PE(__code, __str) PEVENT_ERRNO__ ## __code  enum pevent_errno {  	PEVENT_ERRNO__SUCCESS			= 0, +	PEVENT_ERRNO__FILTER_MATCH		= PEVENT_ERRNO__SUCCESS,  	/*  	 * Choose an arbitrary negative big number not to clash with standard @@ -374,6 +417,22 @@ enum pevent_errno {  };  #undef _PE +struct plugin_list; + +#define INVALID_PLUGIN_LIST_OPTION	((char **)((unsigned long)-1)) + +struct plugin_list *traceevent_load_plugins(struct pevent *pevent); +void traceevent_unload_plugins(struct plugin_list *plugin_list, +			       struct pevent *pevent); +char **traceevent_plugin_list_options(void); +void traceevent_plugin_free_options_list(char **list); +int traceevent_plugin_add_options(const char *name, +				  struct pevent_plugin_option *options); +void traceevent_plugin_remove_options(struct pevent_plugin_option *options); +void traceevent_print_plugins(struct trace_seq *s, +			      const char *prefix, const char *suffix, +			      const struct plugin_list *list); +  struct cmdline;  struct cmdline_list;  struct func_map; @@ -450,6 +509,8 @@ struct pevent {  	/* cache */  	struct event_format *last_event; + +	char *trace_clock;  };  static inline void pevent_set_flag(struct pevent *pevent, int flag) @@ -517,6 +578,15 @@ __data2host8(struct pevent *pevent, unsigned long long data)  	__data2host8(pevent, __val);				\  }) +static inline int traceevent_host_bigendian(void) +{ +	unsigned char str[] = { 0x1, 0x2, 0x3, 0x4 }; +	unsigned int val; + +	memcpy(&val, str, 4); +	return val == 0x01020304; +} +  /* taken from kernel/trace/trace.h */  enum trace_flag_type {  	TRACE_FLAG_IRQS_OFF		= 0x01, @@ -527,21 +597,24 @@ enum trace_flag_type {  };  int pevent_register_comm(struct pevent *pevent, const char *comm, int pid); +void pevent_register_trace_clock(struct pevent *pevent, char *trace_clock);  int pevent_register_function(struct pevent *pevent, char *name,  			     unsigned long long addr, char *mod); -int pevent_register_print_string(struct pevent *pevent, char *fmt, +int pevent_register_print_string(struct pevent *pevent, const char *fmt,  				 unsigned long long addr);  int pevent_pid_is_registered(struct pevent *pevent, int pid);  void pevent_print_event(struct pevent *pevent, struct trace_seq *s, -			struct pevent_record *record); +			struct pevent_record *record, bool use_trace_clock);  int pevent_parse_header_page(struct pevent *pevent, char *buf, unsigned long size,  			     int long_size);  enum pevent_errno pevent_parse_event(struct pevent *pevent, const char *buf,  				     unsigned long size, const char *sys); -enum pevent_errno pevent_parse_format(struct event_format **eventp, const char *buf, +enum pevent_errno pevent_parse_format(struct pevent *pevent, +				      struct event_format **eventp, +				      const char *buf,  				      unsigned long size, const char *sys);  void pevent_free_format(struct event_format *event); @@ -563,13 +636,22 @@ int pevent_print_num_field(struct trace_seq *s, const char *fmt,  			   struct event_format *event, const char *name,  			   struct pevent_record *record, int err); +int pevent_print_func_field(struct trace_seq *s, const char *fmt, +			   struct event_format *event, const char *name, +			   struct pevent_record *record, int err); +  int pevent_register_event_handler(struct pevent *pevent, int id,  				  const char *sys_name, const char *event_name,  				  pevent_event_handler_func func, void *context); +int pevent_unregister_event_handler(struct pevent *pevent, int id, +				    const char *sys_name, const char *event_name, +				    pevent_event_handler_func func, void *context);  int pevent_register_print_function(struct pevent *pevent,  				   pevent_func_handler func,  				   enum pevent_func_arg_type ret_type,  				   char *name, ...); +int pevent_unregister_print_function(struct pevent *pevent, +				     pevent_func_handler func, char *name);  struct format_field *pevent_find_common_field(struct event_format *event, const char *name);  struct format_field *pevent_find_field(struct event_format *event, const char *name); @@ -801,18 +883,22 @@ struct filter_type {  	struct filter_arg	*filter;  }; +#define PEVENT_FILTER_ERROR_BUFSZ  1024 +  struct event_filter {  	struct pevent		*pevent;  	int			filters;  	struct filter_type	*event_filters; +	char			error_buffer[PEVENT_FILTER_ERROR_BUFSZ];  };  struct event_filter *pevent_filter_alloc(struct pevent *pevent); -#define FILTER_NONE		-2 -#define FILTER_NOEXIST		-1 -#define FILTER_MISS		0 -#define FILTER_MATCH		1 +/* for backward compatibility */ +#define FILTER_NONE		PEVENT_ERRNO__NO_FILTER +#define FILTER_NOEXIST		PEVENT_ERRNO__FILTER_NOT_FOUND +#define FILTER_MISS		PEVENT_ERRNO__FILTER_MISS +#define FILTER_MATCH		PEVENT_ERRNO__FILTER_MATCH  enum filter_trivial_type {  	FILTER_TRIVIAL_FALSE, @@ -820,20 +906,21 @@ enum filter_trivial_type {  	FILTER_TRIVIAL_BOTH,  }; -int pevent_filter_add_filter_str(struct event_filter *filter, -				 const char *filter_str, -				 char **error_str); +enum pevent_errno pevent_filter_add_filter_str(struct event_filter *filter, +					       const char *filter_str); +enum pevent_errno pevent_filter_match(struct event_filter *filter, +				      struct pevent_record *record); -int pevent_filter_match(struct event_filter *filter, -			struct pevent_record *record); +int pevent_filter_strerror(struct event_filter *filter, enum pevent_errno err, +			   char *buf, size_t buflen);  int pevent_event_filtered(struct event_filter *filter,  			  int event_id);  void pevent_filter_reset(struct event_filter *filter); -void pevent_filter_clear_trivial(struct event_filter *filter, +int pevent_filter_clear_trivial(struct event_filter *filter,  				 enum filter_trivial_type type);  void pevent_filter_free(struct event_filter *filter); diff --git a/tools/lib/traceevent/event-plugin.c b/tools/lib/traceevent/event-plugin.c new file mode 100644 index 00000000000..136162c03af --- /dev/null +++ b/tools/lib/traceevent/event-plugin.c @@ -0,0 +1,416 @@ +/* + * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ + +#include <stdio.h> +#include <string.h> +#include <dlfcn.h> +#include <stdlib.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <dirent.h> +#include "event-parse.h" +#include "event-utils.h" + +#define LOCAL_PLUGIN_DIR ".traceevent/plugins" + +static struct registered_plugin_options { +	struct registered_plugin_options	*next; +	struct pevent_plugin_option		*options; +} *registered_options; + +static struct trace_plugin_options { +	struct trace_plugin_options	*next; +	char				*plugin; +	char				*option; +	char				*value; +} *trace_plugin_options; + +struct plugin_list { +	struct plugin_list	*next; +	char			*name; +	void			*handle; +}; + +/** + * traceevent_plugin_list_options - get list of plugin options + * + * Returns an array of char strings that list the currently registered + * plugin options in the format of <plugin>:<option>. This list can be + * used by toggling the option. + * + * Returns NULL if there's no options registered. On error it returns + * INVALID_PLUGIN_LIST_OPTION + * + * Must be freed with traceevent_plugin_free_options_list(). + */ +char **traceevent_plugin_list_options(void) +{ +	struct registered_plugin_options *reg; +	struct pevent_plugin_option *op; +	char **list = NULL; +	char *name; +	int count = 0; + +	for (reg = registered_options; reg; reg = reg->next) { +		for (op = reg->options; op->name; op++) { +			char *alias = op->plugin_alias ? op->plugin_alias : op->file; +			char **temp = list; + +			name = malloc(strlen(op->name) + strlen(alias) + 2); +			if (!name) +				goto err; + +			sprintf(name, "%s:%s", alias, op->name); +			list = realloc(list, count + 2); +			if (!list) { +				list = temp; +				free(name); +				goto err; +			} +			list[count++] = name; +			list[count] = NULL; +		} +	} +	return list; + + err: +	while (--count >= 0) +		free(list[count]); +	free(list); + +	return INVALID_PLUGIN_LIST_OPTION; +} + +void traceevent_plugin_free_options_list(char **list) +{ +	int i; + +	if (!list) +		return; + +	if (list == INVALID_PLUGIN_LIST_OPTION) +		return; + +	for (i = 0; list[i]; i++) +		free(list[i]); + +	free(list); +} + +static int +update_option(const char *file, struct pevent_plugin_option *option) +{ +	struct trace_plugin_options *op; +	char *plugin; + +	if (option->plugin_alias) { +		plugin = strdup(option->plugin_alias); +		if (!plugin) +			return -1; +	} else { +		char *p; +		plugin = strdup(file); +		if (!plugin) +			return -1; +		p = strstr(plugin, "."); +		if (p) +			*p = '\0'; +	} + +	/* first look for named options */ +	for (op = trace_plugin_options; op; op = op->next) { +		if (!op->plugin) +			continue; +		if (strcmp(op->plugin, plugin) != 0) +			continue; +		if (strcmp(op->option, option->name) != 0) +			continue; + +		option->value = op->value; +		option->set ^= 1; +		goto out; +	} + +	/* first look for unnamed options */ +	for (op = trace_plugin_options; op; op = op->next) { +		if (op->plugin) +			continue; +		if (strcmp(op->option, option->name) != 0) +			continue; + +		option->value = op->value; +		option->set ^= 1; +		break; +	} + + out: +	free(plugin); +	return 0; +} + +/** + * traceevent_plugin_add_options - Add a set of options by a plugin + * @name: The name of the plugin adding the options + * @options: The set of options being loaded + * + * Sets the options with the values that have been added by user. + */ +int traceevent_plugin_add_options(const char *name, +				  struct pevent_plugin_option *options) +{ +	struct registered_plugin_options *reg; + +	reg = malloc(sizeof(*reg)); +	if (!reg) +		return -1; +	reg->next = registered_options; +	reg->options = options; +	registered_options = reg; + +	while (options->name) { +		update_option(name, options); +		options++; +	} +	return 0; +} + +/** + * traceevent_plugin_remove_options - remove plugin options that were registered + * @options: Options to removed that were registered with traceevent_plugin_add_options + */ +void traceevent_plugin_remove_options(struct pevent_plugin_option *options) +{ +	struct registered_plugin_options **last; +	struct registered_plugin_options *reg; + +	for (last = ®istered_options; *last; last = &(*last)->next) { +		if ((*last)->options == options) { +			reg = *last; +			*last = reg->next; +			free(reg); +			return; +		} +	} +} + +/** + * traceevent_print_plugins - print out the list of plugins loaded + * @s: the trace_seq descripter to write to + * @prefix: The prefix string to add before listing the option name + * @suffix: The suffix string ot append after the option name + * @list: The list of plugins (usually returned by traceevent_load_plugins() + * + * Writes to the trace_seq @s the list of plugins (files) that is + * returned by traceevent_load_plugins(). Use @prefix and @suffix for formating: + * @prefix = "  ", @suffix = "\n". + */ +void traceevent_print_plugins(struct trace_seq *s, +			      const char *prefix, const char *suffix, +			      const struct plugin_list *list) +{ +	while (list) { +		trace_seq_printf(s, "%s%s%s", prefix, list->name, suffix); +		list = list->next; +	} +} + +static void +load_plugin(struct pevent *pevent, const char *path, +	    const char *file, void *data) +{ +	struct plugin_list **plugin_list = data; +	pevent_plugin_load_func func; +	struct plugin_list *list; +	const char *alias; +	char *plugin; +	void *handle; + +	plugin = malloc(strlen(path) + strlen(file) + 2); +	if (!plugin) { +		warning("could not allocate plugin memory\n"); +		return; +	} + +	strcpy(plugin, path); +	strcat(plugin, "/"); +	strcat(plugin, file); + +	handle = dlopen(plugin, RTLD_NOW | RTLD_GLOBAL); +	if (!handle) { +		warning("could not load plugin '%s'\n%s\n", +			plugin, dlerror()); +		goto out_free; +	} + +	alias = dlsym(handle, PEVENT_PLUGIN_ALIAS_NAME); +	if (!alias) +		alias = file; + +	func = dlsym(handle, PEVENT_PLUGIN_LOADER_NAME); +	if (!func) { +		warning("could not find func '%s' in plugin '%s'\n%s\n", +			PEVENT_PLUGIN_LOADER_NAME, plugin, dlerror()); +		goto out_free; +	} + +	list = malloc(sizeof(*list)); +	if (!list) { +		warning("could not allocate plugin memory\n"); +		goto out_free; +	} + +	list->next = *plugin_list; +	list->handle = handle; +	list->name = plugin; +	*plugin_list = list; + +	pr_stat("registering plugin: %s", plugin); +	func(pevent); +	return; + + out_free: +	free(plugin); +} + +static void +load_plugins_dir(struct pevent *pevent, const char *suffix, +		 const char *path, +		 void (*load_plugin)(struct pevent *pevent, +				     const char *path, +				     const char *name, +				     void *data), +		 void *data) +{ +	struct dirent *dent; +	struct stat st; +	DIR *dir; +	int ret; + +	ret = stat(path, &st); +	if (ret < 0) +		return; + +	if (!S_ISDIR(st.st_mode)) +		return; + +	dir = opendir(path); +	if (!dir) +		return; + +	while ((dent = readdir(dir))) { +		const char *name = dent->d_name; + +		if (strcmp(name, ".") == 0 || +		    strcmp(name, "..") == 0) +			continue; + +		/* Only load plugins that end in suffix */ +		if (strcmp(name + (strlen(name) - strlen(suffix)), suffix) != 0) +			continue; + +		load_plugin(pevent, path, name, data); +	} + +	closedir(dir); +} + +static void +load_plugins(struct pevent *pevent, const char *suffix, +	     void (*load_plugin)(struct pevent *pevent, +				 const char *path, +				 const char *name, +				 void *data), +	     void *data) +{ +	char *home; +	char *path; +	char *envdir; + +	if (pevent->flags & PEVENT_DISABLE_PLUGINS) +		return; + +	/* +	 * If a system plugin directory was defined, +	 * check that first. +	 */ +#ifdef PLUGIN_DIR +	if (!(pevent->flags & PEVENT_DISABLE_SYS_PLUGINS)) +		load_plugins_dir(pevent, suffix, PLUGIN_DIR, +				 load_plugin, data); +#endif + +	/* +	 * Next let the environment-set plugin directory +	 * override the system defaults. +	 */ +	envdir = getenv("TRACEEVENT_PLUGIN_DIR"); +	if (envdir) +		load_plugins_dir(pevent, suffix, envdir, load_plugin, data); + +	/* +	 * Now let the home directory override the environment +	 * or system defaults. +	 */ +	home = getenv("HOME"); +	if (!home) +		return; + +	path = malloc(strlen(home) + strlen(LOCAL_PLUGIN_DIR) + 2); +	if (!path) { +		warning("could not allocate plugin memory\n"); +		return; +	} + +	strcpy(path, home); +	strcat(path, "/"); +	strcat(path, LOCAL_PLUGIN_DIR); + +	load_plugins_dir(pevent, suffix, path, load_plugin, data); + +	free(path); +} + +struct plugin_list* +traceevent_load_plugins(struct pevent *pevent) +{ +	struct plugin_list *list = NULL; + +	load_plugins(pevent, ".so", load_plugin, &list); +	return list; +} + +void +traceevent_unload_plugins(struct plugin_list *plugin_list, struct pevent *pevent) +{ +	pevent_plugin_unload_func func; +	struct plugin_list *list; + +	while (plugin_list) { +		list = plugin_list; +		plugin_list = list->next; +		func = dlsym(list->handle, PEVENT_PLUGIN_UNLOADER_NAME); +		if (func) +			func(pevent); +		dlclose(list->handle); +		free(list->name); +		free(list); +	} +} diff --git a/tools/lib/traceevent/event-utils.h b/tools/lib/traceevent/event-utils.h index e76c9acb92c..d1dc2170e40 100644 --- a/tools/lib/traceevent/event-utils.h +++ b/tools/lib/traceevent/event-utils.h @@ -23,18 +23,14 @@  #include <ctype.h>  /* Can be overridden */ -void die(const char *fmt, ...); -void *malloc_or_die(unsigned int size);  void warning(const char *fmt, ...);  void pr_stat(const char *fmt, ...);  void vpr_stat(const char *fmt, va_list ap);  /* Always available */ -void __die(const char *fmt, ...);  void __warning(const char *fmt, ...);  void __pr_stat(const char *fmt, ...); -void __vdie(const char *fmt, ...);  void __vwarning(const char *fmt, ...);  void __vpr_stat(const char *fmt, ...); diff --git a/tools/lib/traceevent/parse-filter.c b/tools/lib/traceevent/parse-filter.c index 2500e75583f..b50234402fc 100644 --- a/tools/lib/traceevent/parse-filter.c +++ b/tools/lib/traceevent/parse-filter.c @@ -38,41 +38,31 @@ struct event_list {  	struct event_format	*event;  }; -#define MAX_ERR_STR_SIZE 256 - -static void show_error(char **error_str, const char *fmt, ...) +static void show_error(char *error_buf, const char *fmt, ...)  {  	unsigned long long index;  	const char *input; -	char *error;  	va_list ap;  	int len;  	int i; -	if (!error_str) -		return; -  	input = pevent_get_input_buf();  	index = pevent_get_input_buf_ptr();  	len = input ? strlen(input) : 0; -	error = malloc_or_die(MAX_ERR_STR_SIZE + (len*2) + 3); -  	if (len) { -		strcpy(error, input); -		error[len] = '\n'; +		strcpy(error_buf, input); +		error_buf[len] = '\n';  		for (i = 1; i < len && i < index; i++) -			error[len+i] = ' '; -		error[len + i] = '^'; -		error[len + i + 1] = '\n'; +			error_buf[len+i] = ' '; +		error_buf[len + i] = '^'; +		error_buf[len + i + 1] = '\n';  		len += i+2;  	}  	va_start(ap, fmt); -	vsnprintf(error + len, MAX_ERR_STR_SIZE, fmt, ap); +	vsnprintf(error_buf + len, PEVENT_FILTER_ERROR_BUFSZ - len, fmt, ap);  	va_end(ap); - -	*error_str = error;  }  static void free_token(char *token) @@ -95,7 +85,11 @@ static enum event_type read_token(char **tok)  	    (strcmp(token, "=") == 0 || strcmp(token, "!") == 0) &&  	    pevent_peek_char() == '~') {  		/* append it */ -		*tok = malloc_or_die(3); +		*tok = malloc(3); +		if (*tok == NULL) { +			free_token(token); +			return EVENT_ERROR; +		}  		sprintf(*tok, "%c%c", *token, '~');  		free_token(token);  		/* Now remove the '~' from the buffer */ @@ -147,11 +141,13 @@ add_filter_type(struct event_filter *filter, int id)  	if (filter_type)  		return filter_type; -	filter->event_filters =	realloc(filter->event_filters, -					sizeof(*filter->event_filters) * -					(filter->filters + 1)); -	if (!filter->event_filters) -		die("Could not allocate filter"); +	filter_type = realloc(filter->event_filters, +			      sizeof(*filter->event_filters) * +			      (filter->filters + 1)); +	if (!filter_type) +		return NULL; + +	filter->event_filters = filter_type;  	for (i = 0; i < filter->filters; i++) {  		if (filter->event_filters[i].event_id > id) @@ -182,7 +178,10 @@ struct event_filter *pevent_filter_alloc(struct pevent *pevent)  {  	struct event_filter *filter; -	filter = malloc_or_die(sizeof(*filter)); +	filter = malloc(sizeof(*filter)); +	if (filter == NULL) +		return NULL; +  	memset(filter, 0, sizeof(*filter));  	filter->pevent = pevent;  	pevent_ref(pevent); @@ -192,12 +191,7 @@ struct event_filter *pevent_filter_alloc(struct pevent *pevent)  static struct filter_arg *allocate_arg(void)  { -	struct filter_arg *arg; - -	arg = malloc_or_die(sizeof(*arg)); -	memset(arg, 0, sizeof(*arg)); - -	return arg; +	return calloc(1, sizeof(struct filter_arg));  }  static void free_arg(struct filter_arg *arg) @@ -242,15 +236,19 @@ static void free_arg(struct filter_arg *arg)  	free(arg);  } -static void add_event(struct event_list **events, +static int add_event(struct event_list **events,  		      struct event_format *event)  {  	struct event_list *list; -	list = malloc_or_die(sizeof(*list)); +	list = malloc(sizeof(*list)); +	if (list == NULL) +		return -1; +  	list->next = *events;  	*events = list;  	list->event = event; +	return 0;  }  static int event_match(struct event_format *event, @@ -265,7 +263,7 @@ static int event_match(struct event_format *event,  		!regexec(ereg, event->name, 0, NULL, 0);  } -static int +static enum pevent_errno  find_event(struct pevent *pevent, struct event_list **events,  	   char *sys_name, char *event_name)  { @@ -273,6 +271,7 @@ find_event(struct pevent *pevent, struct event_list **events,  	regex_t ereg;  	regex_t sreg;  	int match = 0; +	int fail = 0;  	char *reg;  	int ret;  	int i; @@ -283,23 +282,31 @@ find_event(struct pevent *pevent, struct event_list **events,  		sys_name = NULL;  	} -	reg = malloc_or_die(strlen(event_name) + 3); +	reg = malloc(strlen(event_name) + 3); +	if (reg == NULL) +		return PEVENT_ERRNO__MEM_ALLOC_FAILED; +  	sprintf(reg, "^%s$", event_name);  	ret = regcomp(&ereg, reg, REG_ICASE|REG_NOSUB);  	free(reg);  	if (ret) -		return -1; +		return PEVENT_ERRNO__INVALID_EVENT_NAME;  	if (sys_name) { -		reg = malloc_or_die(strlen(sys_name) + 3); +		reg = malloc(strlen(sys_name) + 3); +		if (reg == NULL) { +			regfree(&ereg); +			return PEVENT_ERRNO__MEM_ALLOC_FAILED; +		} +  		sprintf(reg, "^%s$", sys_name);  		ret = regcomp(&sreg, reg, REG_ICASE|REG_NOSUB);  		free(reg);  		if (ret) {  			regfree(&ereg); -			return -1; +			return PEVENT_ERRNO__INVALID_EVENT_NAME;  		}  	} @@ -307,7 +314,10 @@ find_event(struct pevent *pevent, struct event_list **events,  		event = pevent->events[i];  		if (event_match(event, sys_name ? &sreg : NULL, &ereg)) {  			match = 1; -			add_event(events, event); +			if (add_event(events, event) < 0) { +				fail = 1; +				break; +			}  		}  	} @@ -316,7 +326,9 @@ find_event(struct pevent *pevent, struct event_list **events,  		regfree(&sreg);  	if (!match) -		return -1; +		return PEVENT_ERRNO__EVENT_NOT_FOUND; +	if (fail) +		return PEVENT_ERRNO__MEM_ALLOC_FAILED;  	return 0;  } @@ -332,14 +344,18 @@ static void free_events(struct event_list *events)  	}  } -static struct filter_arg * +static enum pevent_errno  create_arg_item(struct event_format *event, const char *token, -		enum event_type type, char **error_str) +		enum event_type type, struct filter_arg **parg, char *error_str)  {  	struct format_field *field;  	struct filter_arg *arg;  	arg = allocate_arg(); +	if (arg == NULL) { +		show_error(error_str, "failed to allocate filter arg"); +		return PEVENT_ERRNO__MEM_ALLOC_FAILED; +	}  	switch (type) { @@ -349,8 +365,11 @@ create_arg_item(struct event_format *event, const char *token,  		arg->value.type =  			type == EVENT_DQUOTE ? FILTER_STRING : FILTER_CHAR;  		arg->value.str = strdup(token); -		if (!arg->value.str) -			die("malloc string"); +		if (!arg->value.str) { +			free_arg(arg); +			show_error(error_str, "failed to allocate string filter arg"); +			return PEVENT_ERRNO__MEM_ALLOC_FAILED; +		}  		break;  	case EVENT_ITEM:  		/* if it is a number, then convert it */ @@ -377,11 +396,11 @@ create_arg_item(struct event_format *event, const char *token,  		break;  	default:  		free_arg(arg); -		show_error(error_str, "expected a value but found %s", -			   token); -		return NULL; +		show_error(error_str, "expected a value but found %s", token); +		return PEVENT_ERRNO__UNEXPECTED_TYPE;  	} -	return arg; +	*parg = arg; +	return 0;  }  static struct filter_arg * @@ -390,6 +409,9 @@ create_arg_op(enum filter_op_type btype)  	struct filter_arg *arg;  	arg = allocate_arg(); +	if (!arg) +		return NULL; +  	arg->type = FILTER_ARG_OP;  	arg->op.type = btype; @@ -402,6 +424,9 @@ create_arg_exp(enum filter_exp_type etype)  	struct filter_arg *arg;  	arg = allocate_arg(); +	if (!arg) +		return NULL; +  	arg->type = FILTER_ARG_EXP;  	arg->op.type = etype; @@ -414,6 +439,9 @@ create_arg_cmp(enum filter_exp_type etype)  	struct filter_arg *arg;  	arg = allocate_arg(); +	if (!arg) +		return NULL; +  	/* Use NUM and change if necessary */  	arg->type = FILTER_ARG_NUM;  	arg->op.type = etype; @@ -421,8 +449,8 @@ create_arg_cmp(enum filter_exp_type etype)  	return arg;  } -static int add_right(struct filter_arg *op, struct filter_arg *arg, -		     char **error_str) +static enum pevent_errno +add_right(struct filter_arg *op, struct filter_arg *arg, char *error_str)  {  	struct filter_arg *left;  	char *str; @@ -453,9 +481,8 @@ static int add_right(struct filter_arg *op, struct filter_arg *arg,  		case FILTER_ARG_FIELD:  			break;  		default: -			show_error(error_str, -				   "Illegal rvalue"); -			return -1; +			show_error(error_str, "Illegal rvalue"); +			return PEVENT_ERRNO__ILLEGAL_RVALUE;  		}  		/* @@ -502,7 +529,7 @@ static int add_right(struct filter_arg *op, struct filter_arg *arg,  			if (left->type != FILTER_ARG_FIELD) {  				show_error(error_str,  					   "Illegal lvalue for string comparison"); -				return -1; +				return PEVENT_ERRNO__ILLEGAL_LVALUE;  			}  			/* Make sure this is a valid string compare */ @@ -521,25 +548,31 @@ static int add_right(struct filter_arg *op, struct filter_arg *arg,  					show_error(error_str,  						   "RegEx '%s' did not compute",  						   str); -					return -1; +					return PEVENT_ERRNO__INVALID_REGEX;  				}  				break;  			default:  				show_error(error_str,  					   "Illegal comparison for string"); -				return -1; +				return PEVENT_ERRNO__ILLEGAL_STRING_CMP;  			}  			op->type = FILTER_ARG_STR;  			op->str.type = op_type;  			op->str.field = left->field.field;  			op->str.val = strdup(str); -			if (!op->str.val) -				die("malloc string"); +			if (!op->str.val) { +				show_error(error_str, "Failed to allocate string filter"); +				return PEVENT_ERRNO__MEM_ALLOC_FAILED; +			}  			/*  			 * Need a buffer to copy data for tests  			 */ -			op->str.buffer = malloc_or_die(op->str.field->size + 1); +			op->str.buffer = malloc(op->str.field->size + 1); +			if (!op->str.buffer) { +				show_error(error_str, "Failed to allocate string filter"); +				return PEVENT_ERRNO__MEM_ALLOC_FAILED; +			}  			/* Null terminate this buffer */  			op->str.buffer[op->str.field->size] = 0; @@ -557,7 +590,7 @@ static int add_right(struct filter_arg *op, struct filter_arg *arg,  			case FILTER_CMP_NOT_REGEX:  				show_error(error_str,  					   "Op not allowed with integers"); -				return -1; +				return PEVENT_ERRNO__ILLEGAL_INTEGER_CMP;  			default:  				break; @@ -577,9 +610,8 @@ static int add_right(struct filter_arg *op, struct filter_arg *arg,  	return 0;   out_fail: -	show_error(error_str, -		   "Syntax error"); -	return -1; +	show_error(error_str, "Syntax error"); +	return PEVENT_ERRNO__SYNTAX_ERROR;  }  static struct filter_arg * @@ -592,7 +624,7 @@ rotate_op_right(struct filter_arg *a, struct filter_arg *b)  	return arg;  } -static int add_left(struct filter_arg *op, struct filter_arg *arg) +static enum pevent_errno add_left(struct filter_arg *op, struct filter_arg *arg)  {  	switch (op->type) {  	case FILTER_ARG_EXP: @@ -611,11 +643,11 @@ static int add_left(struct filter_arg *op, struct filter_arg *arg)  		/* left arg of compares must be a field */  		if (arg->type != FILTER_ARG_FIELD &&  		    arg->type != FILTER_ARG_BOOLEAN) -			return -1; +			return PEVENT_ERRNO__INVALID_ARG_TYPE;  		op->num.left = arg;  		break;  	default: -		return -1; +		return PEVENT_ERRNO__INVALID_ARG_TYPE;  	}  	return 0;  } @@ -728,15 +760,18 @@ enum filter_vals {  	FILTER_VAL_TRUE,  }; -void reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child, -		  struct filter_arg *arg) +static enum pevent_errno +reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child, +		struct filter_arg *arg, char *error_str)  {  	struct filter_arg *other_child;  	struct filter_arg **ptr;  	if (parent->type != FILTER_ARG_OP && -	    arg->type != FILTER_ARG_OP) -		die("can not reparent other than OP"); +	    arg->type != FILTER_ARG_OP) { +		show_error(error_str, "can not reparent other than OP"); +		return PEVENT_ERRNO__REPARENT_NOT_OP; +	}  	/* Get the sibling */  	if (old_child->op.right == arg) { @@ -745,8 +780,10 @@ void reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child,  	} else if (old_child->op.left == arg) {  		ptr = &old_child->op.left;  		other_child = old_child->op.right; -	} else -		die("Error in reparent op, find other child"); +	} else { +		show_error(error_str, "Error in reparent op, find other child"); +		return PEVENT_ERRNO__REPARENT_FAILED; +	}  	/* Detach arg from old_child */  	*ptr = NULL; @@ -757,23 +794,29 @@ void reparent_op_arg(struct filter_arg *parent, struct filter_arg *old_child,  		*parent = *arg;  		/* Free arg without recussion */  		free(arg); -		return; +		return 0;  	}  	if (parent->op.right == old_child)  		ptr = &parent->op.right;  	else if (parent->op.left == old_child)  		ptr = &parent->op.left; -	else -		die("Error in reparent op"); +	else { +		show_error(error_str, "Error in reparent op"); +		return PEVENT_ERRNO__REPARENT_FAILED; +	} +  	*ptr = arg;  	free_arg(old_child); +	return 0;  } -enum filter_vals test_arg(struct filter_arg *parent, struct filter_arg *arg) +/* Returns either filter_vals (success) or pevent_errno (failfure) */ +static int test_arg(struct filter_arg *parent, struct filter_arg *arg, +		    char *error_str)  { -	enum filter_vals lval, rval; +	int lval, rval;  	switch (arg->type) { @@ -788,63 +831,68 @@ enum filter_vals test_arg(struct filter_arg *parent, struct filter_arg *arg)  		return FILTER_VAL_NORM;  	case FILTER_ARG_EXP: -		lval = test_arg(arg, arg->exp.left); +		lval = test_arg(arg, arg->exp.left, error_str);  		if (lval != FILTER_VAL_NORM)  			return lval; -		rval = test_arg(arg, arg->exp.right); +		rval = test_arg(arg, arg->exp.right, error_str);  		if (rval != FILTER_VAL_NORM)  			return rval;  		return FILTER_VAL_NORM;  	case FILTER_ARG_NUM: -		lval = test_arg(arg, arg->num.left); +		lval = test_arg(arg, arg->num.left, error_str);  		if (lval != FILTER_VAL_NORM)  			return lval; -		rval = test_arg(arg, arg->num.right); +		rval = test_arg(arg, arg->num.right, error_str);  		if (rval != FILTER_VAL_NORM)  			return rval;  		return FILTER_VAL_NORM;  	case FILTER_ARG_OP:  		if (arg->op.type != FILTER_OP_NOT) { -			lval = test_arg(arg, arg->op.left); +			lval = test_arg(arg, arg->op.left, error_str);  			switch (lval) {  			case FILTER_VAL_NORM:  				break;  			case FILTER_VAL_TRUE:  				if (arg->op.type == FILTER_OP_OR)  					return FILTER_VAL_TRUE; -				rval = test_arg(arg, arg->op.right); +				rval = test_arg(arg, arg->op.right, error_str);  				if (rval != FILTER_VAL_NORM)  					return rval; -				reparent_op_arg(parent, arg, arg->op.right); -				return FILTER_VAL_NORM; +				return reparent_op_arg(parent, arg, arg->op.right, +						       error_str);  			case FILTER_VAL_FALSE:  				if (arg->op.type == FILTER_OP_AND)  					return FILTER_VAL_FALSE; -				rval = test_arg(arg, arg->op.right); +				rval = test_arg(arg, arg->op.right, error_str);  				if (rval != FILTER_VAL_NORM)  					return rval; -				reparent_op_arg(parent, arg, arg->op.right); -				return FILTER_VAL_NORM; +				return reparent_op_arg(parent, arg, arg->op.right, +						       error_str); + +			default: +				return lval;  			}  		} -		rval = test_arg(arg, arg->op.right); +		rval = test_arg(arg, arg->op.right, error_str);  		switch (rval) {  		case FILTER_VAL_NORM: +		default:  			break; +  		case FILTER_VAL_TRUE:  			if (arg->op.type == FILTER_OP_OR)  				return FILTER_VAL_TRUE;  			if (arg->op.type == FILTER_OP_NOT)  				return FILTER_VAL_FALSE; -			reparent_op_arg(parent, arg, arg->op.left); -			return FILTER_VAL_NORM; +			return reparent_op_arg(parent, arg, arg->op.left, +					       error_str);  		case FILTER_VAL_FALSE:  			if (arg->op.type == FILTER_OP_AND) @@ -852,41 +900,56 @@ enum filter_vals test_arg(struct filter_arg *parent, struct filter_arg *arg)  			if (arg->op.type == FILTER_OP_NOT)  				return FILTER_VAL_TRUE; -			reparent_op_arg(parent, arg, arg->op.left); -			return FILTER_VAL_NORM; +			return reparent_op_arg(parent, arg, arg->op.left, +					       error_str);  		} -		return FILTER_VAL_NORM; +		return rval;  	default: -		die("bad arg in filter tree"); +		show_error(error_str, "bad arg in filter tree"); +		return PEVENT_ERRNO__BAD_FILTER_ARG;  	}  	return FILTER_VAL_NORM;  }  /* Remove any unknown event fields */ -static struct filter_arg *collapse_tree(struct filter_arg *arg) +static int collapse_tree(struct filter_arg *arg, +			 struct filter_arg **arg_collapsed, char *error_str)  { -	enum filter_vals ret; +	int ret; -	ret = test_arg(arg, arg); +	ret = test_arg(arg, arg, error_str);  	switch (ret) {  	case FILTER_VAL_NORM: -		return arg; +		break;  	case FILTER_VAL_TRUE:  	case FILTER_VAL_FALSE:  		free_arg(arg);  		arg = allocate_arg(); -		arg->type = FILTER_ARG_BOOLEAN; -		arg->boolean.value = ret == FILTER_VAL_TRUE; +		if (arg) { +			arg->type = FILTER_ARG_BOOLEAN; +			arg->boolean.value = ret == FILTER_VAL_TRUE; +		} else { +			show_error(error_str, "Failed to allocate filter arg"); +			ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; +		} +		break; + +	default: +		/* test_arg() already set the error_str */ +		free_arg(arg); +		arg = NULL; +		break;  	} -	return arg; +	*arg_collapsed = arg; +	return ret;  } -static int +static enum pevent_errno  process_filter(struct event_format *event, struct filter_arg **parg, -	       char **error_str, int not) +	       char *error_str, int not)  {  	enum event_type type;  	char *token = NULL; @@ -898,7 +961,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,  	enum filter_op_type btype;  	enum filter_exp_type etype;  	enum filter_cmp_type ctype; -	int ret; +	enum pevent_errno ret;  	*parg = NULL; @@ -909,8 +972,8 @@ process_filter(struct event_format *event, struct filter_arg **parg,  		case EVENT_SQUOTE:  		case EVENT_DQUOTE:  		case EVENT_ITEM: -			arg = create_arg_item(event, token, type, error_str); -			if (!arg) +			ret = create_arg_item(event, token, type, &arg, error_str); +			if (ret < 0)  				goto fail;  			if (!left_item)  				left_item = arg; @@ -923,20 +986,20 @@ process_filter(struct event_format *event, struct filter_arg **parg,  				if (not) {  					arg = NULL;  					if (current_op) -						goto fail_print; +						goto fail_syntax;  					free(token);  					*parg = current_exp;  					return 0;  				}  			} else -				goto fail_print; +				goto fail_syntax;  			arg = NULL;  			break;  		case EVENT_DELIM:  			if (*token == ',') { -				show_error(error_str, -					   "Illegal token ','"); +				show_error(error_str, "Illegal token ','"); +				ret = PEVENT_ERRNO__ILLEGAL_TOKEN;  				goto fail;  			} @@ -944,19 +1007,23 @@ process_filter(struct event_format *event, struct filter_arg **parg,  				if (left_item) {  					show_error(error_str,  						   "Open paren can not come after item"); +					ret = PEVENT_ERRNO__INVALID_PAREN;  					goto fail;  				}  				if (current_exp) {  					show_error(error_str,  						   "Open paren can not come after expression"); +					ret = PEVENT_ERRNO__INVALID_PAREN;  					goto fail;  				}  				ret = process_filter(event, &arg, error_str, 0); -				if (ret != 1) { -					if (ret == 0) +				if (ret != PEVENT_ERRNO__UNBALANCED_PAREN) { +					if (ret == 0) {  						show_error(error_str,  							   "Unbalanced number of '('"); +						ret = PEVENT_ERRNO__UNBALANCED_PAREN; +					}  					goto fail;  				}  				ret = 0; @@ -964,7 +1031,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,  				/* A not wants just one expression */  				if (not) {  					if (current_op) -						goto fail_print; +						goto fail_syntax;  					*parg = arg;  					return 0;  				} @@ -979,19 +1046,19 @@ process_filter(struct event_format *event, struct filter_arg **parg,  			} else { /* ')' */  				if (!current_op && !current_exp) -					goto fail_print; +					goto fail_syntax;  				/* Make sure everything is finished at this level */  				if (current_exp && !check_op_done(current_exp)) -					goto fail_print; +					goto fail_syntax;  				if (current_op && !check_op_done(current_op)) -					goto fail_print; +					goto fail_syntax;  				if (current_op)  					*parg = current_op;  				else  					*parg = current_exp; -				return 1; +				return PEVENT_ERRNO__UNBALANCED_PAREN;  			}  			break; @@ -1003,21 +1070,22 @@ process_filter(struct event_format *event, struct filter_arg **parg,  			case OP_BOOL:  				/* Logic ops need a left expression */  				if (!current_exp && !current_op) -					goto fail_print; +					goto fail_syntax;  				/* fall through */  			case OP_NOT:  				/* logic only processes ops and exp */  				if (left_item) -					goto fail_print; +					goto fail_syntax;  				break;  			case OP_EXP:  			case OP_CMP:  				if (!left_item) -					goto fail_print; +					goto fail_syntax;  				break;  			case OP_NONE:  				show_error(error_str,  					   "Unknown op token %s", token); +				ret = PEVENT_ERRNO__UNKNOWN_TOKEN;  				goto fail;  			} @@ -1025,6 +1093,8 @@ process_filter(struct event_format *event, struct filter_arg **parg,  			switch (op_type) {  			case OP_BOOL:  				arg = create_arg_op(btype); +				if (arg == NULL) +					goto fail_alloc;  				if (current_op)  					ret = add_left(arg, current_op);  				else @@ -1035,6 +1105,8 @@ process_filter(struct event_format *event, struct filter_arg **parg,  			case OP_NOT:  				arg = create_arg_op(btype); +				if (arg == NULL) +					goto fail_alloc;  				if (current_op)  					ret = add_right(current_op, arg, error_str);  				if (ret < 0) @@ -1054,6 +1126,8 @@ process_filter(struct event_format *event, struct filter_arg **parg,  					arg = create_arg_exp(etype);  				else  					arg = create_arg_cmp(ctype); +				if (arg == NULL) +					goto fail_alloc;  				if (current_op)  					ret = add_right(current_op, arg, error_str); @@ -1062,7 +1136,7 @@ process_filter(struct event_format *event, struct filter_arg **parg,  				ret = add_left(arg, left_item);  				if (ret < 0) {  					arg = NULL; -					goto fail_print; +					goto fail_syntax;  				}  				current_exp = arg;  				break; @@ -1071,57 +1145,64 @@ process_filter(struct event_format *event, struct filter_arg **parg,  			}  			arg = NULL;  			if (ret < 0) -				goto fail_print; +				goto fail_syntax;  			break;  		case EVENT_NONE:  			break; +		case EVENT_ERROR: +			goto fail_alloc;  		default: -			goto fail_print; +			goto fail_syntax;  		}  	} while (type != EVENT_NONE);  	if (!current_op && !current_exp) -		goto fail_print; +		goto fail_syntax;  	if (!current_op)  		current_op = current_exp; -	current_op = collapse_tree(current_op); +	ret = collapse_tree(current_op, parg, error_str); +	if (ret < 0) +		goto fail;  	*parg = current_op;  	return 0; - fail_print: + fail_alloc: +	show_error(error_str, "failed to allocate filter arg"); +	ret = PEVENT_ERRNO__MEM_ALLOC_FAILED; +	goto fail; + fail_syntax:  	show_error(error_str, "Syntax error"); +	ret = PEVENT_ERRNO__SYNTAX_ERROR;   fail:  	free_arg(current_op);  	free_arg(current_exp);  	free_arg(arg);  	free(token); -	return -1; +	return ret;  } -static int +static enum pevent_errno  process_event(struct event_format *event, const char *filter_str, -	      struct filter_arg **parg, char **error_str) +	      struct filter_arg **parg, char *error_str)  {  	int ret;  	pevent_buffer_init(filter_str, strlen(filter_str));  	ret = process_filter(event, parg, error_str, 0); -	if (ret == 1) { -		show_error(error_str, -			   "Unbalanced number of ')'"); -		return -1; -	}  	if (ret < 0)  		return ret;  	/* If parg is NULL, then make it into FALSE */  	if (!*parg) {  		*parg = allocate_arg(); +		if (*parg == NULL) +			return PEVENT_ERRNO__MEM_ALLOC_FAILED; +  		(*parg)->type = FILTER_ARG_BOOLEAN;  		(*parg)->boolean.value = FILTER_FALSE;  	} @@ -1129,13 +1210,13 @@ process_event(struct event_format *event, const char *filter_str,  	return 0;  } -static int filter_event(struct event_filter *filter, -			struct event_format *event, -			const char *filter_str, char **error_str) +static enum pevent_errno +filter_event(struct event_filter *filter, struct event_format *event, +	     const char *filter_str, char *error_str)  {  	struct filter_type *filter_type;  	struct filter_arg *arg; -	int ret; +	enum pevent_errno ret;  	if (filter_str) {  		ret = process_event(event, filter_str, &arg, error_str); @@ -1145,11 +1226,17 @@ static int filter_event(struct event_filter *filter,  	} else {  		/* just add a TRUE arg */  		arg = allocate_arg(); +		if (arg == NULL) +			return PEVENT_ERRNO__MEM_ALLOC_FAILED; +  		arg->type = FILTER_ARG_BOOLEAN;  		arg->boolean.value = FILTER_TRUE;  	}  	filter_type = add_filter_type(filter, event->id); +	if (filter_type == NULL) +		return PEVENT_ERRNO__MEM_ALLOC_FAILED; +  	if (filter_type->filter)  		free_arg(filter_type->filter);  	filter_type->filter = arg; @@ -1157,22 +1244,24 @@ static int filter_event(struct event_filter *filter,  	return 0;  } +static void filter_init_error_buf(struct event_filter *filter) +{ +	/* clear buffer to reset show error */ +	pevent_buffer_init("", 0); +	filter->error_buffer[0] = '\0'; +} +  /**   * pevent_filter_add_filter_str - add a new filter   * @filter: the event filter to add to   * @filter_str: the filter string that contains the filter - * @error_str: string containing reason for failed filter - * - * Returns 0 if the filter was successfully added - *   -1 if there was an error.   * - * On error, if @error_str points to a string pointer, - * it is set to the reason that the filter failed. - * This string must be freed with "free". + * Returns 0 if the filter was successfully added or a + * negative error code.  Use pevent_filter_strerror() to see + * actual error message in case of error.   */ -int pevent_filter_add_filter_str(struct event_filter *filter, -				 const char *filter_str, -				 char **error_str) +enum pevent_errno pevent_filter_add_filter_str(struct event_filter *filter, +					       const char *filter_str)  {  	struct pevent *pevent = filter->pevent;  	struct event_list *event; @@ -1183,15 +1272,11 @@ int pevent_filter_add_filter_str(struct event_filter *filter,  	char *event_name = NULL;  	char *sys_name = NULL;  	char *sp; -	int rtn = 0; +	enum pevent_errno rtn = 0; /* PEVENT_ERRNO__SUCCESS */  	int len;  	int ret; -	/* clear buffer to reset show error */ -	pevent_buffer_init("", 0); - -	if (error_str) -		*error_str = NULL; +	filter_init_error_buf(filter);  	filter_start = strchr(filter_str, ':');  	if (filter_start) @@ -1199,7 +1284,6 @@ int pevent_filter_add_filter_str(struct event_filter *filter,  	else  		len = strlen(filter_str); -  	do {  		next_event = strchr(filter_str, ',');  		if (next_event && @@ -1210,7 +1294,12 @@ int pevent_filter_add_filter_str(struct event_filter *filter,  		else  			len = strlen(filter_str); -		this_event = malloc_or_die(len + 1); +		this_event = malloc(len + 1); +		if (this_event == NULL) { +			/* This can only happen when events is NULL, but still */ +			free_events(events); +			return PEVENT_ERRNO__MEM_ALLOC_FAILED; +		}  		memcpy(this_event, filter_str, len);  		this_event[len] = 0; @@ -1223,27 +1312,18 @@ int pevent_filter_add_filter_str(struct event_filter *filter,  		event_name = strtok_r(NULL, "/", &sp);  		if (!sys_name) { -			show_error(error_str, "No filter found");  			/* This can only happen when events is NULL, but still */  			free_events(events);  			free(this_event); -			return -1; +			return PEVENT_ERRNO__FILTER_NOT_FOUND;  		}  		/* Find this event */  		ret = find_event(pevent, &events, strim(sys_name), strim(event_name));  		if (ret < 0) { -			if (event_name) -				show_error(error_str, -					   "No event found under '%s.%s'", -					   sys_name, event_name); -			else -				show_error(error_str, -					   "No event found under '%s'", -					   sys_name);  			free_events(events);  			free(this_event); -			return -1; +			return ret;  		}  		free(this_event);  	} while (filter_str); @@ -1255,7 +1335,7 @@ int pevent_filter_add_filter_str(struct event_filter *filter,  	/* filter starts here */  	for (event = events; event; event = event->next) {  		ret = filter_event(filter, event->event, filter_start, -				   error_str); +				   filter->error_buffer);  		/* Failures are returned if a parse error happened */  		if (ret < 0)  			rtn = ret; @@ -1263,8 +1343,10 @@ int pevent_filter_add_filter_str(struct event_filter *filter,  		if (ret >= 0 && pevent->test_filters) {  			char *test;  			test = pevent_filter_make_string(filter, event->event->id); -			printf(" '%s: %s'\n", event->event->name, test); -			free(test); +			if (test) { +				printf(" '%s: %s'\n", event->event->name, test); +				free(test); +			}  		}  	} @@ -1282,6 +1364,32 @@ static void free_filter_type(struct filter_type *filter_type)  }  /** + * pevent_filter_strerror - fill error message in a buffer + * @filter: the event filter contains error + * @err: the error code + * @buf: the buffer to be filled in + * @buflen: the size of the buffer + * + * Returns 0 if message was filled successfully, -1 if error + */ +int pevent_filter_strerror(struct event_filter *filter, enum pevent_errno err, +			   char *buf, size_t buflen) +{ +	if (err <= __PEVENT_ERRNO__START || err >= __PEVENT_ERRNO__END) +		return -1; + +	if (strlen(filter->error_buffer) > 0) { +		size_t len = snprintf(buf, buflen, "%s", filter->error_buffer); + +		if (len > buflen) +			return -1; +		return 0; +	} + +	return pevent_strerror(filter->pevent, err, buf, buflen); +} + +/**   * pevent_filter_remove_event - remove a filter for an event   * @filter: the event filter to remove from   * @event_id: the event to remove a filter for @@ -1374,6 +1482,9 @@ static int copy_filter_type(struct event_filter *filter,  	if (strcmp(str, "TRUE") == 0 || strcmp(str, "FALSE") == 0) {  		/* Add trivial event */  		arg = allocate_arg(); +		if (arg == NULL) +			return -1; +  		arg->type = FILTER_ARG_BOOLEAN;  		if (strcmp(str, "TRUE") == 0)  			arg->boolean.value = 1; @@ -1381,6 +1492,9 @@ static int copy_filter_type(struct event_filter *filter,  			arg->boolean.value = 0;  		filter_type = add_filter_type(filter, event->id); +		if (filter_type == NULL) +			return -1; +  		filter_type->filter = arg;  		free(str); @@ -1482,8 +1596,10 @@ int pevent_update_trivial(struct event_filter *dest, struct event_filter *source   * @type: remove only true, false, or both   *   * Removes filters that only contain a TRUE or FALES boolean arg. + * + * Returns 0 on success and -1 if there was a problem.   */ -void pevent_filter_clear_trivial(struct event_filter *filter, +int pevent_filter_clear_trivial(struct event_filter *filter,  				 enum filter_trivial_type type)  {  	struct filter_type *filter_type; @@ -1492,13 +1608,15 @@ void pevent_filter_clear_trivial(struct event_filter *filter,  	int i;  	if (!filter->filters) -		return; +		return 0;  	/*  	 * Two steps, first get all ids with trivial filters.  	 *  then remove those ids.  	 */  	for (i = 0; i < filter->filters; i++) { +		int *new_ids; +  		filter_type = &filter->event_filters[i];  		if (filter_type->filter->type != FILTER_ARG_BOOLEAN)  			continue; @@ -1513,19 +1631,24 @@ void pevent_filter_clear_trivial(struct event_filter *filter,  			break;  		} -		ids = realloc(ids, sizeof(*ids) * (count + 1)); -		if (!ids) -			die("Can't allocate ids"); +		new_ids = realloc(ids, sizeof(*ids) * (count + 1)); +		if (!new_ids) { +			free(ids); +			return -1; +		} + +		ids = new_ids;  		ids[count++] = filter_type->event_id;  	}  	if (!count) -		return; +		return 0;  	for (i = 0; i < count; i++)  		pevent_filter_remove_event(filter, ids[i]);  	free(ids); +	return 0;  }  /** @@ -1565,8 +1688,8 @@ int pevent_filter_event_has_trivial(struct event_filter *filter,  	}  } -static int test_filter(struct event_format *event, -		       struct filter_arg *arg, struct pevent_record *record); +static int test_filter(struct event_format *event, struct filter_arg *arg, +		       struct pevent_record *record, enum pevent_errno *err);  static const char *  get_comm(struct event_format *event, struct pevent_record *record) @@ -1612,15 +1735,24 @@ get_value(struct event_format *event,  }  static unsigned long long -get_arg_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record); +get_arg_value(struct event_format *event, struct filter_arg *arg, +	      struct pevent_record *record, enum pevent_errno *err);  static unsigned long long -get_exp_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record) +get_exp_value(struct event_format *event, struct filter_arg *arg, +	      struct pevent_record *record, enum pevent_errno *err)  {  	unsigned long long lval, rval; -	lval = get_arg_value(event, arg->exp.left, record); -	rval = get_arg_value(event, arg->exp.right, record); +	lval = get_arg_value(event, arg->exp.left, record, err); +	rval = get_arg_value(event, arg->exp.right, record, err); + +	if (*err) { +		/* +		 * There was an error, no need to process anymore. +		 */ +		return 0; +	}  	switch (arg->exp.type) {  	case FILTER_EXP_ADD: @@ -1655,39 +1787,51 @@ get_exp_value(struct event_format *event, struct filter_arg *arg, struct pevent_  	case FILTER_EXP_NOT:  	default: -		die("error in exp"); +		if (!*err) +			*err = PEVENT_ERRNO__INVALID_EXP_TYPE;  	}  	return 0;  }  static unsigned long long -get_arg_value(struct event_format *event, struct filter_arg *arg, struct pevent_record *record) +get_arg_value(struct event_format *event, struct filter_arg *arg, +	      struct pevent_record *record, enum pevent_errno *err)  {  	switch (arg->type) {  	case FILTER_ARG_FIELD:  		return get_value(event, arg->field.field, record);  	case FILTER_ARG_VALUE: -		if (arg->value.type != FILTER_NUMBER) -			die("must have number field!"); +		if (arg->value.type != FILTER_NUMBER) { +			if (!*err) +				*err = PEVENT_ERRNO__NOT_A_NUMBER; +		}  		return arg->value.val;  	case FILTER_ARG_EXP: -		return get_exp_value(event, arg, record); +		return get_exp_value(event, arg, record, err);  	default: -		die("oops in filter"); +		if (!*err) +			*err = PEVENT_ERRNO__INVALID_ARG_TYPE;  	}  	return 0;  } -static int test_num(struct event_format *event, -		    struct filter_arg *arg, struct pevent_record *record) +static int test_num(struct event_format *event, struct filter_arg *arg, +		    struct pevent_record *record, enum pevent_errno *err)  {  	unsigned long long lval, rval; -	lval = get_arg_value(event, arg->num.left, record); -	rval = get_arg_value(event, arg->num.right, record); +	lval = get_arg_value(event, arg->num.left, record, err); +	rval = get_arg_value(event, arg->num.right, record, err); + +	if (*err) { +		/* +		 * There was an error, no need to process anymore. +		 */ +		return 0; +	}  	switch (arg->num.type) {  	case FILTER_CMP_EQ: @@ -1709,7 +1853,8 @@ static int test_num(struct event_format *event,  		return lval <= rval;  	default: -		/* ?? */ +		if (!*err) +			*err = PEVENT_ERRNO__ILLEGAL_INTEGER_CMP;  		return 0;  	}  } @@ -1756,8 +1901,8 @@ static const char *get_field_str(struct filter_arg *arg, struct pevent_record *r  	return val;  } -static int test_str(struct event_format *event, -		    struct filter_arg *arg, struct pevent_record *record) +static int test_str(struct event_format *event, struct filter_arg *arg, +		    struct pevent_record *record, enum pevent_errno *err)  {  	const char *val; @@ -1781,48 +1926,57 @@ static int test_str(struct event_format *event,  		return regexec(&arg->str.reg, val, 0, NULL, 0);  	default: -		/* ?? */ +		if (!*err) +			*err = PEVENT_ERRNO__ILLEGAL_STRING_CMP;  		return 0;  	}  } -static int test_op(struct event_format *event, -		   struct filter_arg *arg, struct pevent_record *record) +static int test_op(struct event_format *event, struct filter_arg *arg, +		   struct pevent_record *record, enum pevent_errno *err)  {  	switch (arg->op.type) {  	case FILTER_OP_AND: -		return test_filter(event, arg->op.left, record) && -			test_filter(event, arg->op.right, record); +		return test_filter(event, arg->op.left, record, err) && +			test_filter(event, arg->op.right, record, err);  	case FILTER_OP_OR: -		return test_filter(event, arg->op.left, record) || -			test_filter(event, arg->op.right, record); +		return test_filter(event, arg->op.left, record, err) || +			test_filter(event, arg->op.right, record, err);  	case FILTER_OP_NOT: -		return !test_filter(event, arg->op.right, record); +		return !test_filter(event, arg->op.right, record, err);  	default: -		/* ?? */ +		if (!*err) +			*err = PEVENT_ERRNO__INVALID_OP_TYPE;  		return 0;  	}  } -static int test_filter(struct event_format *event, -		       struct filter_arg *arg, struct pevent_record *record) +static int test_filter(struct event_format *event, struct filter_arg *arg, +		       struct pevent_record *record, enum pevent_errno *err)  { +	if (*err) { +		/* +		 * There was an error, no need to process anymore. +		 */ +		return 0; +	} +  	switch (arg->type) {  	case FILTER_ARG_BOOLEAN:  		/* easy case */  		return arg->boolean.value;  	case FILTER_ARG_OP: -		return test_op(event, arg, record); +		return test_op(event, arg, record, err);  	case FILTER_ARG_NUM: -		return test_num(event, arg, record); +		return test_num(event, arg, record, err);  	case FILTER_ARG_STR: -		return test_str(event, arg, record); +		return test_str(event, arg, record, err);  	case FILTER_ARG_EXP:  	case FILTER_ARG_VALUE: @@ -1831,11 +1985,11 @@ static int test_filter(struct event_format *event,  		 * Expressions, fields and values evaluate  		 * to true if they return non zero  		 */ -		return !!get_arg_value(event, arg, record); +		return !!get_arg_value(event, arg, record, err);  	default: -		die("oops!"); -		/* ?? */ +		if (!*err) +			*err = PEVENT_ERRNO__INVALID_ARG_TYPE;  		return 0;  	}  } @@ -1848,8 +2002,7 @@ static int test_filter(struct event_format *event,   * Returns 1 if filter found for @event_id   *   otherwise 0;   */ -int pevent_event_filtered(struct event_filter *filter, -			  int event_id) +int pevent_event_filtered(struct event_filter *filter, int event_id)  {  	struct filter_type *filter_type; @@ -1866,31 +2019,38 @@ int pevent_event_filtered(struct event_filter *filter,   * @filter: filter struct with filter information   * @record: the record to test against the filter   * - * Returns: - *  1 - filter found for event and @record matches - *  0 - filter found for event and @record does not match - * -1 - no filter found for @record's event - * -2 - if no filters exist + * Returns: match result or error code (prefixed with PEVENT_ERRNO__) + * FILTER_MATCH - filter found for event and @record matches + * FILTER_MISS  - filter found for event and @record does not match + * FILTER_NOT_FOUND - no filter found for @record's event + * NO_FILTER - if no filters exist + * otherwise - error occurred during test   */ -int pevent_filter_match(struct event_filter *filter, -			struct pevent_record *record) +enum pevent_errno pevent_filter_match(struct event_filter *filter, +				      struct pevent_record *record)  {  	struct pevent *pevent = filter->pevent;  	struct filter_type *filter_type;  	int event_id; +	int ret; +	enum pevent_errno err = 0; + +	filter_init_error_buf(filter);  	if (!filter->filters) -		return FILTER_NONE; +		return PEVENT_ERRNO__NO_FILTER;  	event_id = pevent_data_type(pevent, record);  	filter_type = find_filter_type(filter, event_id); -  	if (!filter_type) -		return FILTER_NOEXIST; +		return PEVENT_ERRNO__FILTER_NOT_FOUND; + +	ret = test_filter(filter_type->event, filter_type->filter, record, &err); +	if (err) +		return err; -	return test_filter(filter_type->event, filter_type->filter, record) ? -		FILTER_MATCH : FILTER_MISS; +	return ret ? PEVENT_ERRNO__FILTER_MATCH : PEVENT_ERRNO__FILTER_MISS;  }  static char *op_to_str(struct event_filter *filter, struct filter_arg *arg) @@ -1902,7 +2062,6 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)  	int left_val = -1;  	int right_val = -1;  	int val; -	int len;  	switch (arg->op.type) {  	case FILTER_OP_AND: @@ -1949,11 +2108,7 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)  				default:  					break;  				} -				str = malloc_or_die(6); -				if (val) -					strcpy(str, "TRUE"); -				else -					strcpy(str, "FALSE"); +				asprintf(&str, val ? "TRUE" : "FALSE");  				break;  			}  		} @@ -1971,10 +2126,7 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)  			break;  		} -		len = strlen(left) + strlen(right) + strlen(op) + 10; -		str = malloc_or_die(len); -		snprintf(str, len, "(%s) %s (%s)", -			 left, op, right); +		asprintf(&str, "(%s) %s (%s)", left, op, right);  		break;  	case FILTER_OP_NOT: @@ -1990,16 +2142,10 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)  			right_val = 0;  		if (right_val >= 0) {  			/* just return the opposite */ -			str = malloc_or_die(6); -			if (right_val) -				strcpy(str, "FALSE"); -			else -				strcpy(str, "TRUE"); +			asprintf(&str, right_val ? "FALSE" : "TRUE");  			break;  		} -		len = strlen(right) + strlen(op) + 3; -		str = malloc_or_die(len); -		snprintf(str, len, "%s(%s)", op, right); +		asprintf(&str, "%s(%s)", op, right);  		break;  	default: @@ -2013,11 +2159,9 @@ static char *op_to_str(struct event_filter *filter, struct filter_arg *arg)  static char *val_to_str(struct event_filter *filter, struct filter_arg *arg)  { -	char *str; - -	str = malloc_or_die(30); +	char *str = NULL; -	snprintf(str, 30, "%lld", arg->value.val); +	asprintf(&str, "%lld", arg->value.val);  	return str;  } @@ -2033,7 +2177,6 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)  	char *rstr;  	char *op;  	char *str = NULL; -	int len;  	lstr = arg_to_str(filter, arg->exp.left);  	rstr = arg_to_str(filter, arg->exp.right); @@ -2072,12 +2215,11 @@ static char *exp_to_str(struct event_filter *filter, struct filter_arg *arg)  		op = "^";  		break;  	default: -		die("oops in exp"); +		op = "[ERROR IN EXPRESSION TYPE]"; +		break;  	} -	len = strlen(op) + strlen(lstr) + strlen(rstr) + 4; -	str = malloc_or_die(len); -	snprintf(str, len, "%s %s %s", lstr, op, rstr); +	asprintf(&str, "%s %s %s", lstr, op, rstr);  out:  	free(lstr);  	free(rstr); @@ -2091,7 +2233,6 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)  	char *rstr;  	char *str = NULL;  	char *op = NULL; -	int len;  	lstr = arg_to_str(filter, arg->num.left);  	rstr = arg_to_str(filter, arg->num.right); @@ -2122,10 +2263,7 @@ static char *num_to_str(struct event_filter *filter, struct filter_arg *arg)  		if (!op)  			op = "<="; -		len = strlen(lstr) + strlen(op) + strlen(rstr) + 4; -		str = malloc_or_die(len); -		sprintf(str, "%s %s %s", lstr, op, rstr); - +		asprintf(&str, "%s %s %s", lstr, op, rstr);  		break;  	default: @@ -2143,7 +2281,6 @@ static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)  {  	char *str = NULL;  	char *op = NULL; -	int len;  	switch (arg->str.type) {  	case FILTER_CMP_MATCH: @@ -2161,12 +2298,8 @@ static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)  		if (!op)  			op = "!~"; -		len = strlen(arg->str.field->name) + strlen(op) + -			strlen(arg->str.val) + 6; -		str = malloc_or_die(len); -		snprintf(str, len, "%s %s \"%s\"", -			 arg->str.field->name, -			 op, arg->str.val); +		asprintf(&str, "%s %s \"%s\"", +			 arg->str.field->name, op, arg->str.val);  		break;  	default: @@ -2178,15 +2311,11 @@ static char *str_to_str(struct event_filter *filter, struct filter_arg *arg)  static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg)  { -	char *str; +	char *str = NULL;  	switch (arg->type) {  	case FILTER_ARG_BOOLEAN: -		str = malloc_or_die(6); -		if (arg->boolean.value) -			strcpy(str, "TRUE"); -		else -			strcpy(str, "FALSE"); +		asprintf(&str, arg->boolean.value ? "TRUE" : "FALSE");  		return str;  	case FILTER_ARG_OP: @@ -2221,7 +2350,7 @@ static char *arg_to_str(struct event_filter *filter, struct filter_arg *arg)   *   * Returns a string that displays the filter contents.   *  This string must be freed with free(str). - *  NULL is returned if no filter is found. + *  NULL is returned if no filter is found or allocation failed.   */  char *  pevent_filter_make_string(struct event_filter *filter, int event_id) diff --git a/tools/lib/traceevent/parse-utils.c b/tools/lib/traceevent/parse-utils.c index bba701cf10e..eda07fa31dc 100644 --- a/tools/lib/traceevent/parse-utils.c +++ b/tools/lib/traceevent/parse-utils.c @@ -25,40 +25,6 @@  #define __weak __attribute__((weak)) -void __vdie(const char *fmt, va_list ap) -{ -	int ret = errno; - -	if (errno) -		perror("trace-cmd"); -	else -		ret = -1; - -	fprintf(stderr, "  "); -	vfprintf(stderr, fmt, ap); - -	fprintf(stderr, "\n"); -	exit(ret); -} - -void __die(const char *fmt, ...) -{ -	va_list ap; - -	va_start(ap, fmt); -	__vdie(fmt, ap); -	va_end(ap); -} - -void __weak die(const char *fmt, ...) -{ -	va_list ap; - -	va_start(ap, fmt); -	__vdie(fmt, ap); -	va_end(ap); -} -  void __vwarning(const char *fmt, va_list ap)  {  	if (errno) @@ -117,13 +83,3 @@ void __weak pr_stat(const char *fmt, ...)  	__vpr_stat(fmt, ap);  	va_end(ap);  } - -void __weak *malloc_or_die(unsigned int size) -{ -	void *data; - -	data = malloc(size); -	if (!data) -		die("malloc"); -	return data; -} diff --git a/tools/lib/traceevent/plugin_cfg80211.c b/tools/lib/traceevent/plugin_cfg80211.c new file mode 100644 index 00000000000..c066b25905f --- /dev/null +++ b/tools/lib/traceevent/plugin_cfg80211.c @@ -0,0 +1,30 @@ +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include <endian.h> +#include "event-parse.h" + +static unsigned long long +process___le16_to_cpup(struct trace_seq *s, +		       unsigned long long *args) +{ +	uint16_t *val = (uint16_t *) (unsigned long) args[0]; +	return val ? (long long) le16toh(*val) : 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_print_function(pevent, +				       process___le16_to_cpup, +				       PEVENT_FUNC_ARG_INT, +				       "__le16_to_cpup", +				       PEVENT_FUNC_ARG_PTR, +				       PEVENT_FUNC_ARG_VOID); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_print_function(pevent, process___le16_to_cpup, +					 "__le16_to_cpup"); +} diff --git a/tools/lib/traceevent/plugin_function.c b/tools/lib/traceevent/plugin_function.c new file mode 100644 index 00000000000..a00ec190821 --- /dev/null +++ b/tools/lib/traceevent/plugin_function.c @@ -0,0 +1,194 @@ +/* + * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "event-parse.h" +#include "event-utils.h" + +static struct func_stack { +	int size; +	char **stack; +} *fstack; + +static int cpus = -1; + +#define STK_BLK 10 + +struct pevent_plugin_option plugin_options[] = +{ +	{ +		.name = "parent", +		.plugin_alias = "ftrace", +		.description = +		"Print parent of functions for function events", +	}, +	{ +		.name = "indent", +		.plugin_alias = "ftrace", +		.description = +		"Try to show function call indents, based on parents", +		.set = 1, +	}, +	{ +		.name = NULL, +	} +}; + +static struct pevent_plugin_option *ftrace_parent = &plugin_options[0]; +static struct pevent_plugin_option *ftrace_indent = &plugin_options[1]; + +static void add_child(struct func_stack *stack, const char *child, int pos) +{ +	int i; + +	if (!child) +		return; + +	if (pos < stack->size) +		free(stack->stack[pos]); +	else { +		char **ptr; + +		ptr = realloc(stack->stack, sizeof(char *) * +			      (stack->size + STK_BLK)); +		if (!ptr) { +			warning("could not allocate plugin memory\n"); +			return; +		} + +		stack->stack = ptr; + +		for (i = stack->size; i < stack->size + STK_BLK; i++) +			stack->stack[i] = NULL; +		stack->size += STK_BLK; +	} + +	stack->stack[pos] = strdup(child); +} + +static int add_and_get_index(const char *parent, const char *child, int cpu) +{ +	int i; + +	if (cpu < 0) +		return 0; + +	if (cpu > cpus) { +		struct func_stack *ptr; + +		ptr = realloc(fstack, sizeof(*fstack) * (cpu + 1)); +		if (!ptr) { +			warning("could not allocate plugin memory\n"); +			return 0; +		} + +		fstack = ptr; + +		/* Account for holes in the cpu count */ +		for (i = cpus + 1; i <= cpu; i++) +			memset(&fstack[i], 0, sizeof(fstack[i])); +		cpus = cpu; +	} + +	for (i = 0; i < fstack[cpu].size && fstack[cpu].stack[i]; i++) { +		if (strcmp(parent, fstack[cpu].stack[i]) == 0) { +			add_child(&fstack[cpu], child, i+1); +			return i; +		} +	} + +	/* Not found */ +	add_child(&fstack[cpu], parent, 0); +	add_child(&fstack[cpu], child, 1); +	return 0; +} + +static int function_handler(struct trace_seq *s, struct pevent_record *record, +			    struct event_format *event, void *context) +{ +	struct pevent *pevent = event->pevent; +	unsigned long long function; +	unsigned long long pfunction; +	const char *func; +	const char *parent; +	int index; + +	if (pevent_get_field_val(s, event, "ip", record, &function, 1)) +		return trace_seq_putc(s, '!'); + +	func = pevent_find_function(pevent, function); + +	if (pevent_get_field_val(s, event, "parent_ip", record, &pfunction, 1)) +		return trace_seq_putc(s, '!'); + +	parent = pevent_find_function(pevent, pfunction); + +	if (parent && ftrace_indent->set) +		index = add_and_get_index(parent, func, record->cpu); + +	trace_seq_printf(s, "%*s", index*3, ""); + +	if (func) +		trace_seq_printf(s, "%s", func); +	else +		trace_seq_printf(s, "0x%llx", function); + +	if (ftrace_parent->set) { +		trace_seq_printf(s, " <-- "); +		if (parent) +			trace_seq_printf(s, "%s", parent); +		else +			trace_seq_printf(s, "0x%llx", pfunction); +	} + +	return 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_event_handler(pevent, -1, "ftrace", "function", +				      function_handler, NULL); + +	traceevent_plugin_add_options("ftrace", plugin_options); + +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	int i, x; + +	pevent_unregister_event_handler(pevent, -1, "ftrace", "function", +					function_handler, NULL); + +	for (i = 0; i <= cpus; i++) { +		for (x = 0; x < fstack[i].size && fstack[i].stack[x]; x++) +			free(fstack[i].stack[x]); +		free(fstack[i].stack); +	} + +	traceevent_plugin_remove_options(plugin_options); + +	free(fstack); +	fstack = NULL; +	cpus = -1; +} diff --git a/tools/lib/traceevent/plugin_hrtimer.c b/tools/lib/traceevent/plugin_hrtimer.c new file mode 100644 index 00000000000..12bf14cc115 --- /dev/null +++ b/tools/lib/traceevent/plugin_hrtimer.c @@ -0,0 +1,88 @@ +/* + * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * Copyright (C) 2009 Johannes Berg <johannes@sipsolutions.net> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "event-parse.h" + +static int timer_expire_handler(struct trace_seq *s, +				struct pevent_record *record, +				struct event_format *event, void *context) +{ +	trace_seq_printf(s, "hrtimer="); + +	if (pevent_print_num_field(s, "0x%llx", event, "timer", +				   record, 0) == -1) +		pevent_print_num_field(s, "0x%llx", event, "hrtimer", +				       record, 1); + +	trace_seq_printf(s, " now="); + +	pevent_print_num_field(s, "%llu", event, "now", record, 1); + +	pevent_print_func_field(s, " function=%s", event, "function", +				record, 0); +	return 0; +} + +static int timer_start_handler(struct trace_seq *s, +			       struct pevent_record *record, +			       struct event_format *event, void *context) +{ +	trace_seq_printf(s, "hrtimer="); + +	if (pevent_print_num_field(s, "0x%llx", event, "timer", +				   record, 0) == -1) +		pevent_print_num_field(s, "0x%llx", event, "hrtimer", +				       record, 1); + +	pevent_print_func_field(s, " function=%s", event, "function", +				record, 0); + +	trace_seq_printf(s, " expires="); +	pevent_print_num_field(s, "%llu", event, "expires", record, 1); + +	trace_seq_printf(s, " softexpires="); +	pevent_print_num_field(s, "%llu", event, "softexpires", record, 1); +	return 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_event_handler(pevent, -1, +				      "timer", "hrtimer_expire_entry", +				      timer_expire_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "timer", "hrtimer_start", +				      timer_start_handler, NULL); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_event_handler(pevent, -1, +					"timer", "hrtimer_expire_entry", +					timer_expire_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "timer", "hrtimer_start", +					timer_start_handler, NULL); +} diff --git a/tools/lib/traceevent/plugin_jbd2.c b/tools/lib/traceevent/plugin_jbd2.c new file mode 100644 index 00000000000..0db714c721b --- /dev/null +++ b/tools/lib/traceevent/plugin_jbd2.c @@ -0,0 +1,77 @@ +/* + * Copyright (C) 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "event-parse.h" + +#define MINORBITS	20 +#define MINORMASK	((1U << MINORBITS) - 1) + +#define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS)) +#define MINOR(dev)	((unsigned int) ((dev) & MINORMASK)) + +static unsigned long long +process_jbd2_dev_to_name(struct trace_seq *s, +			 unsigned long long *args) +{ +	unsigned int dev = args[0]; + +	trace_seq_printf(s, "%d:%d", MAJOR(dev), MINOR(dev)); +	return 0; +} + +static unsigned long long +process_jiffies_to_msecs(struct trace_seq *s, +			 unsigned long long *args) +{ +	unsigned long long jiffies = args[0]; + +	trace_seq_printf(s, "%lld", jiffies); +	return jiffies; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_print_function(pevent, +				       process_jbd2_dev_to_name, +				       PEVENT_FUNC_ARG_STRING, +				       "jbd2_dev_to_name", +				       PEVENT_FUNC_ARG_INT, +				       PEVENT_FUNC_ARG_VOID); + +	pevent_register_print_function(pevent, +				       process_jiffies_to_msecs, +				       PEVENT_FUNC_ARG_LONG, +				       "jiffies_to_msecs", +				       PEVENT_FUNC_ARG_LONG, +				       PEVENT_FUNC_ARG_VOID); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_print_function(pevent, process_jbd2_dev_to_name, +					 "jbd2_dev_to_name"); + +	pevent_unregister_print_function(pevent, process_jiffies_to_msecs, +					 "jiffies_to_msecs"); +} diff --git a/tools/lib/traceevent/plugin_kmem.c b/tools/lib/traceevent/plugin_kmem.c new file mode 100644 index 00000000000..70650ff48d7 --- /dev/null +++ b/tools/lib/traceevent/plugin_kmem.c @@ -0,0 +1,94 @@ +/* + * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "event-parse.h" + +static int call_site_handler(struct trace_seq *s, struct pevent_record *record, +			     struct event_format *event, void *context) +{ +	struct format_field *field; +	unsigned long long val, addr; +	void *data = record->data; +	const char *func; + +	field = pevent_find_field(event, "call_site"); +	if (!field) +		return 1; + +	if (pevent_read_number_field(field, data, &val)) +		return 1; + +	func = pevent_find_function(event->pevent, val); +	if (!func) +		return 1; + +	addr = pevent_find_function_address(event->pevent, val); + +	trace_seq_printf(s, "(%s+0x%x) ", func, (int)(val - addr)); +	return 1; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_event_handler(pevent, -1, "kmem", "kfree", +				      call_site_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kmem", "kmalloc", +				      call_site_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kmem", "kmalloc_node", +				      call_site_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kmem", "kmem_cache_alloc", +				      call_site_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kmem", +				      "kmem_cache_alloc_node", +				      call_site_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kmem", "kmem_cache_free", +				      call_site_handler, NULL); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_event_handler(pevent, -1, "kmem", "kfree", +					call_site_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kmem", "kmalloc", +					call_site_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kmem", "kmalloc_node", +					call_site_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kmem", "kmem_cache_alloc", +					call_site_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kmem", +					"kmem_cache_alloc_node", +					call_site_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kmem", "kmem_cache_free", +					call_site_handler, NULL); +} diff --git a/tools/lib/traceevent/plugin_kvm.c b/tools/lib/traceevent/plugin_kvm.c new file mode 100644 index 00000000000..9e0e8c61b43 --- /dev/null +++ b/tools/lib/traceevent/plugin_kvm.c @@ -0,0 +1,465 @@ +/* + * Copyright (C) 2009 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdint.h> + +#include "event-parse.h" + +#ifdef HAVE_UDIS86 + +#include <udis86.h> + +static ud_t ud; + +static void init_disassembler(void) +{ +	ud_init(&ud); +	ud_set_syntax(&ud, UD_SYN_ATT); +} + +static const char *disassemble(unsigned char *insn, int len, uint64_t rip, +			       int cr0_pe, int eflags_vm, +			       int cs_d, int cs_l) +{ +	int mode; + +	if (!cr0_pe) +		mode = 16; +	else if (eflags_vm) +		mode = 16; +	else if (cs_l) +		mode = 64; +	else if (cs_d) +		mode = 32; +	else +		mode = 16; + +	ud_set_pc(&ud, rip); +	ud_set_mode(&ud, mode); +	ud_set_input_buffer(&ud, insn, len); +	ud_disassemble(&ud); +	return ud_insn_asm(&ud); +} + +#else + +static void init_disassembler(void) +{ +} + +static const char *disassemble(unsigned char *insn, int len, uint64_t rip, +			       int cr0_pe, int eflags_vm, +			       int cs_d, int cs_l) +{ +	static char out[15*3+1]; +	int i; + +	for (i = 0; i < len; ++i) +		sprintf(out + i * 3, "%02x ", insn[i]); +	out[len*3-1] = '\0'; +	return out; +} + +#endif + + +#define VMX_EXIT_REASONS			\ +	_ER(EXCEPTION_NMI,	 0)		\ +	_ER(EXTERNAL_INTERRUPT,	 1)		\ +	_ER(TRIPLE_FAULT,	 2)		\ +	_ER(PENDING_INTERRUPT,	 7)		\ +	_ER(NMI_WINDOW,		 8)		\ +	_ER(TASK_SWITCH,	 9)		\ +	_ER(CPUID,		 10)		\ +	_ER(HLT,		 12)		\ +	_ER(INVD,		 13)		\ +	_ER(INVLPG,		 14)		\ +	_ER(RDPMC,		 15)		\ +	_ER(RDTSC,		 16)		\ +	_ER(VMCALL,		 18)		\ +	_ER(VMCLEAR,		 19)		\ +	_ER(VMLAUNCH,		 20)		\ +	_ER(VMPTRLD,		 21)		\ +	_ER(VMPTRST,		 22)		\ +	_ER(VMREAD,		 23)		\ +	_ER(VMRESUME,		 24)		\ +	_ER(VMWRITE,		 25)		\ +	_ER(VMOFF,		 26)		\ +	_ER(VMON,		 27)		\ +	_ER(CR_ACCESS,		 28)		\ +	_ER(DR_ACCESS,		 29)		\ +	_ER(IO_INSTRUCTION,	 30)		\ +	_ER(MSR_READ,		 31)		\ +	_ER(MSR_WRITE,		 32)		\ +	_ER(MWAIT_INSTRUCTION,	 36)		\ +	_ER(MONITOR_INSTRUCTION, 39)		\ +	_ER(PAUSE_INSTRUCTION,	 40)		\ +	_ER(MCE_DURING_VMENTRY,	 41)		\ +	_ER(TPR_BELOW_THRESHOLD, 43)		\ +	_ER(APIC_ACCESS,	 44)		\ +	_ER(EOI_INDUCED,	 45)		\ +	_ER(EPT_VIOLATION,	 48)		\ +	_ER(EPT_MISCONFIG,	 49)		\ +	_ER(INVEPT,		 50)		\ +	_ER(PREEMPTION_TIMER,	 52)		\ +	_ER(WBINVD,		 54)		\ +	_ER(XSETBV,		 55)		\ +	_ER(APIC_WRITE,		 56)		\ +	_ER(INVPCID,		 58) + +#define SVM_EXIT_REASONS \ +	_ER(EXIT_READ_CR0,	0x000)		\ +	_ER(EXIT_READ_CR3,	0x003)		\ +	_ER(EXIT_READ_CR4,	0x004)		\ +	_ER(EXIT_READ_CR8,	0x008)		\ +	_ER(EXIT_WRITE_CR0,	0x010)		\ +	_ER(EXIT_WRITE_CR3,	0x013)		\ +	_ER(EXIT_WRITE_CR4,	0x014)		\ +	_ER(EXIT_WRITE_CR8,	0x018)		\ +	_ER(EXIT_READ_DR0,	0x020)		\ +	_ER(EXIT_READ_DR1,	0x021)		\ +	_ER(EXIT_READ_DR2,	0x022)		\ +	_ER(EXIT_READ_DR3,	0x023)		\ +	_ER(EXIT_READ_DR4,	0x024)		\ +	_ER(EXIT_READ_DR5,	0x025)		\ +	_ER(EXIT_READ_DR6,	0x026)		\ +	_ER(EXIT_READ_DR7,	0x027)		\ +	_ER(EXIT_WRITE_DR0,	0x030)		\ +	_ER(EXIT_WRITE_DR1,	0x031)		\ +	_ER(EXIT_WRITE_DR2,	0x032)		\ +	_ER(EXIT_WRITE_DR3,	0x033)		\ +	_ER(EXIT_WRITE_DR4,	0x034)		\ +	_ER(EXIT_WRITE_DR5,	0x035)		\ +	_ER(EXIT_WRITE_DR6,	0x036)		\ +	_ER(EXIT_WRITE_DR7,	0x037)		\ +	_ER(EXIT_EXCP_BASE,     0x040)		\ +	_ER(EXIT_INTR,		0x060)		\ +	_ER(EXIT_NMI,		0x061)		\ +	_ER(EXIT_SMI,		0x062)		\ +	_ER(EXIT_INIT,		0x063)		\ +	_ER(EXIT_VINTR,		0x064)		\ +	_ER(EXIT_CR0_SEL_WRITE,	0x065)		\ +	_ER(EXIT_IDTR_READ,	0x066)		\ +	_ER(EXIT_GDTR_READ,	0x067)		\ +	_ER(EXIT_LDTR_READ,	0x068)		\ +	_ER(EXIT_TR_READ,	0x069)		\ +	_ER(EXIT_IDTR_WRITE,	0x06a)		\ +	_ER(EXIT_GDTR_WRITE,	0x06b)		\ +	_ER(EXIT_LDTR_WRITE,	0x06c)		\ +	_ER(EXIT_TR_WRITE,	0x06d)		\ +	_ER(EXIT_RDTSC,		0x06e)		\ +	_ER(EXIT_RDPMC,		0x06f)		\ +	_ER(EXIT_PUSHF,		0x070)		\ +	_ER(EXIT_POPF,		0x071)		\ +	_ER(EXIT_CPUID,		0x072)		\ +	_ER(EXIT_RSM,		0x073)		\ +	_ER(EXIT_IRET,		0x074)		\ +	_ER(EXIT_SWINT,		0x075)		\ +	_ER(EXIT_INVD,		0x076)		\ +	_ER(EXIT_PAUSE,		0x077)		\ +	_ER(EXIT_HLT,		0x078)		\ +	_ER(EXIT_INVLPG,	0x079)		\ +	_ER(EXIT_INVLPGA,	0x07a)		\ +	_ER(EXIT_IOIO,		0x07b)		\ +	_ER(EXIT_MSR,		0x07c)		\ +	_ER(EXIT_TASK_SWITCH,	0x07d)		\ +	_ER(EXIT_FERR_FREEZE,	0x07e)		\ +	_ER(EXIT_SHUTDOWN,	0x07f)		\ +	_ER(EXIT_VMRUN,		0x080)		\ +	_ER(EXIT_VMMCALL,	0x081)		\ +	_ER(EXIT_VMLOAD,	0x082)		\ +	_ER(EXIT_VMSAVE,	0x083)		\ +	_ER(EXIT_STGI,		0x084)		\ +	_ER(EXIT_CLGI,		0x085)		\ +	_ER(EXIT_SKINIT,	0x086)		\ +	_ER(EXIT_RDTSCP,	0x087)		\ +	_ER(EXIT_ICEBP,		0x088)		\ +	_ER(EXIT_WBINVD,	0x089)		\ +	_ER(EXIT_MONITOR,	0x08a)		\ +	_ER(EXIT_MWAIT,		0x08b)		\ +	_ER(EXIT_MWAIT_COND,	0x08c)		\ +	_ER(EXIT_NPF,		0x400)		\ +	_ER(EXIT_ERR,		-1) + +#define _ER(reason, val)	{ #reason, val }, +struct str_values { +	const char	*str; +	int		val; +}; + +static struct str_values vmx_exit_reasons[] = { +	VMX_EXIT_REASONS +	{ NULL, -1} +}; + +static struct str_values svm_exit_reasons[] = { +	SVM_EXIT_REASONS +	{ NULL, -1} +}; + +static struct isa_exit_reasons { +	unsigned isa; +	struct str_values *strings; +} isa_exit_reasons[] = { +	{ .isa = 1, .strings = vmx_exit_reasons }, +	{ .isa = 2, .strings = svm_exit_reasons }, +	{ } +}; + +static const char *find_exit_reason(unsigned isa, int val) +{ +	struct str_values *strings = NULL; +	int i; + +	for (i = 0; isa_exit_reasons[i].strings; ++i) +		if (isa_exit_reasons[i].isa == isa) { +			strings = isa_exit_reasons[i].strings; +			break; +		} +	if (!strings) +		return "UNKNOWN-ISA"; +	for (i = 0; strings[i].val >= 0; i++) +		if (strings[i].val == val) +			break; +	if (strings[i].str) +		return strings[i].str; +	return "UNKNOWN"; +} + +static int kvm_exit_handler(struct trace_seq *s, struct pevent_record *record, +			    struct event_format *event, void *context) +{ +	unsigned long long isa; +	unsigned long long val; +	unsigned long long info1 = 0, info2 = 0; + +	if (pevent_get_field_val(s, event, "exit_reason", record, &val, 1) < 0) +		return -1; + +	if (pevent_get_field_val(s, event, "isa", record, &isa, 0) < 0) +		isa = 1; + +	trace_seq_printf(s, "reason %s", find_exit_reason(isa, val)); + +	pevent_print_num_field(s, " rip 0x%lx", event, "guest_rip", record, 1); + +	if (pevent_get_field_val(s, event, "info1", record, &info1, 0) >= 0 +	    && pevent_get_field_val(s, event, "info2", record, &info2, 0) >= 0) +		trace_seq_printf(s, " info %llx %llx", info1, info2); + +	return 0; +} + +#define KVM_EMUL_INSN_F_CR0_PE (1 << 0) +#define KVM_EMUL_INSN_F_EFL_VM (1 << 1) +#define KVM_EMUL_INSN_F_CS_D   (1 << 2) +#define KVM_EMUL_INSN_F_CS_L   (1 << 3) + +static int kvm_emulate_insn_handler(struct trace_seq *s, +				    struct pevent_record *record, +				    struct event_format *event, void *context) +{ +	unsigned long long rip, csbase, len, flags, failed; +	int llen; +	uint8_t *insn; +	const char *disasm; + +	if (pevent_get_field_val(s, event, "rip", record, &rip, 1) < 0) +		return -1; + +	if (pevent_get_field_val(s, event, "csbase", record, &csbase, 1) < 0) +		return -1; + +	if (pevent_get_field_val(s, event, "len", record, &len, 1) < 0) +		return -1; + +	if (pevent_get_field_val(s, event, "flags", record, &flags, 1) < 0) +		return -1; + +	if (pevent_get_field_val(s, event, "failed", record, &failed, 1) < 0) +		return -1; + +	insn = pevent_get_field_raw(s, event, "insn", record, &llen, 1); +	if (!insn) +		return -1; + +	disasm = disassemble(insn, len, rip, +			     flags & KVM_EMUL_INSN_F_CR0_PE, +			     flags & KVM_EMUL_INSN_F_EFL_VM, +			     flags & KVM_EMUL_INSN_F_CS_D, +			     flags & KVM_EMUL_INSN_F_CS_L); + +	trace_seq_printf(s, "%llx:%llx: %s%s", csbase, rip, disasm, +			 failed ? " FAIL" : ""); +	return 0; +} + +union kvm_mmu_page_role { +	unsigned word; +	struct { +		unsigned glevels:4; +		unsigned level:4; +		unsigned quadrant:2; +		unsigned pad_for_nice_hex_output:6; +		unsigned direct:1; +		unsigned access:3; +		unsigned invalid:1; +		unsigned cr4_pge:1; +		unsigned nxe:1; +	}; +}; + +static int kvm_mmu_print_role(struct trace_seq *s, struct pevent_record *record, +			      struct event_format *event, void *context) +{ +	unsigned long long val; +	static const char *access_str[] = { +		"---", "--x", "w--", "w-x", "-u-", "-ux", "wu-", "wux" +	}; +	union kvm_mmu_page_role role; + +	if (pevent_get_field_val(s, event, "role", record, &val, 1) < 0) +		return -1; + +	role.word = (int)val; + +	/* +	 * We can only use the structure if file is of the same +	 * endianess. +	 */ +	if (pevent_is_file_bigendian(event->pevent) == +	    pevent_is_host_bigendian(event->pevent)) { + +		trace_seq_printf(s, "%u/%u q%u%s %s%s %spge %snxe", +				 role.level, +				 role.glevels, +				 role.quadrant, +				 role.direct ? " direct" : "", +				 access_str[role.access], +				 role.invalid ? " invalid" : "", +				 role.cr4_pge ? "" : "!", +				 role.nxe ? "" : "!"); +	} else +		trace_seq_printf(s, "WORD: %08x", role.word); + +	pevent_print_num_field(s, " root %u ",  event, +			       "root_count", record, 1); + +	if (pevent_get_field_val(s, event, "unsync", record, &val, 1) < 0) +		return -1; + +	trace_seq_printf(s, "%s%c",  val ? "unsync" : "sync", 0); +	return 0; +} + +static int kvm_mmu_get_page_handler(struct trace_seq *s, +				    struct pevent_record *record, +				    struct event_format *event, void *context) +{ +	unsigned long long val; + +	if (pevent_get_field_val(s, event, "created", record, &val, 1) < 0) +		return -1; + +	trace_seq_printf(s, "%s ", val ? "new" : "existing"); + +	if (pevent_get_field_val(s, event, "gfn", record, &val, 1) < 0) +		return -1; + +	trace_seq_printf(s, "sp gfn %llx ", val); +	return kvm_mmu_print_role(s, record, event, context); +} + +#define PT_WRITABLE_SHIFT 1 +#define PT_WRITABLE_MASK (1ULL << PT_WRITABLE_SHIFT) + +static unsigned long long +process_is_writable_pte(struct trace_seq *s, unsigned long long *args) +{ +	unsigned long pte = args[0]; +	return pte & PT_WRITABLE_MASK; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	init_disassembler(); + +	pevent_register_event_handler(pevent, -1, "kvm", "kvm_exit", +				      kvm_exit_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kvm", "kvm_emulate_insn", +				      kvm_emulate_insn_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_get_page", +				      kvm_mmu_get_page_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_sync_page", +				      kvm_mmu_print_role, NULL); + +	pevent_register_event_handler(pevent, -1, +				      "kvmmmu", "kvm_mmu_unsync_page", +				      kvm_mmu_print_role, NULL); + +	pevent_register_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_zap_page", +				      kvm_mmu_print_role, NULL); + +	pevent_register_event_handler(pevent, -1, "kvmmmu", +			"kvm_mmu_prepare_zap_page", kvm_mmu_print_role, +			NULL); + +	pevent_register_print_function(pevent, +				       process_is_writable_pte, +				       PEVENT_FUNC_ARG_INT, +				       "is_writable_pte", +				       PEVENT_FUNC_ARG_LONG, +				       PEVENT_FUNC_ARG_VOID); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_event_handler(pevent, -1, "kvm", "kvm_exit", +					kvm_exit_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kvm", "kvm_emulate_insn", +					kvm_emulate_insn_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_get_page", +					kvm_mmu_get_page_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_sync_page", +					kvm_mmu_print_role, NULL); + +	pevent_unregister_event_handler(pevent, -1, +					"kvmmmu", "kvm_mmu_unsync_page", +					kvm_mmu_print_role, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kvmmmu", "kvm_mmu_zap_page", +					kvm_mmu_print_role, NULL); + +	pevent_unregister_event_handler(pevent, -1, "kvmmmu", +			"kvm_mmu_prepare_zap_page", kvm_mmu_print_role, +			NULL); + +	pevent_unregister_print_function(pevent, process_is_writable_pte, +					 "is_writable_pte"); +} diff --git a/tools/lib/traceevent/plugin_mac80211.c b/tools/lib/traceevent/plugin_mac80211.c new file mode 100644 index 00000000000..7e15a0f1c2f --- /dev/null +++ b/tools/lib/traceevent/plugin_mac80211.c @@ -0,0 +1,102 @@ +/* + * Copyright (C) 2009 Johannes Berg <johannes@sipsolutions.net> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "event-parse.h" + +#define INDENT 65 + +static void print_string(struct trace_seq *s, struct event_format *event, +			 const char *name, const void *data) +{ +	struct format_field *f = pevent_find_field(event, name); +	int offset; +	int length; + +	if (!f) { +		trace_seq_printf(s, "NOTFOUND:%s", name); +		return; +	} + +	offset = f->offset; +	length = f->size; + +	if (!strncmp(f->type, "__data_loc", 10)) { +		unsigned long long v; +		if (pevent_read_number_field(f, data, &v)) { +			trace_seq_printf(s, "invalid_data_loc"); +			return; +		} +		offset = v & 0xffff; +		length = v >> 16; +	} + +	trace_seq_printf(s, "%.*s", length, (char *)data + offset); +} + +#define SF(fn)	pevent_print_num_field(s, fn ":%d", event, fn, record, 0) +#define SFX(fn)	pevent_print_num_field(s, fn ":%#x", event, fn, record, 0) +#define SP()	trace_seq_putc(s, ' ') + +static int drv_bss_info_changed(struct trace_seq *s, +				struct pevent_record *record, +				struct event_format *event, void *context) +{ +	void *data = record->data; + +	print_string(s, event, "wiphy_name", data); +	trace_seq_printf(s, " vif:"); +	print_string(s, event, "vif_name", data); +	pevent_print_num_field(s, "(%d)", event, "vif_type", record, 1); + +	trace_seq_printf(s, "\n%*s", INDENT, ""); +	SF("assoc"); SP(); +	SF("aid"); SP(); +	SF("cts"); SP(); +	SF("shortpre"); SP(); +	SF("shortslot"); SP(); +	SF("dtimper"); SP(); +	trace_seq_printf(s, "\n%*s", INDENT, ""); +	SF("bcnint"); SP(); +	SFX("assoc_cap"); SP(); +	SFX("basic_rates"); SP(); +	SF("enable_beacon"); +	trace_seq_printf(s, "\n%*s", INDENT, ""); +	SF("ht_operation_mode"); + +	return 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_event_handler(pevent, -1, "mac80211", +				      "drv_bss_info_changed", +				      drv_bss_info_changed, NULL); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_event_handler(pevent, -1, "mac80211", +					"drv_bss_info_changed", +					drv_bss_info_changed, NULL); +} diff --git a/tools/lib/traceevent/plugin_sched_switch.c b/tools/lib/traceevent/plugin_sched_switch.c new file mode 100644 index 00000000000..f1ce6006525 --- /dev/null +++ b/tools/lib/traceevent/plugin_sched_switch.c @@ -0,0 +1,160 @@ +/* + * Copyright (C) 2009, 2010 Red Hat Inc, Steven Rostedt <srostedt@redhat.com> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; + * version 2.1 of the License (not later!) + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not,  see <http://www.gnu.org/licenses> + * + * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + */ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include "event-parse.h" + +static void write_state(struct trace_seq *s, int val) +{ +	const char states[] = "SDTtZXxW"; +	int found = 0; +	int i; + +	for (i = 0; i < (sizeof(states) - 1); i++) { +		if (!(val & (1 << i))) +			continue; + +		if (found) +			trace_seq_putc(s, '|'); + +		found = 1; +		trace_seq_putc(s, states[i]); +	} + +	if (!found) +		trace_seq_putc(s, 'R'); +} + +static void write_and_save_comm(struct format_field *field, +				struct pevent_record *record, +				struct trace_seq *s, int pid) +{ +	const char *comm; +	int len; + +	comm = (char *)(record->data + field->offset); +	len = s->len; +	trace_seq_printf(s, "%.*s", +			 field->size, comm); + +	/* make sure the comm has a \0 at the end. */ +	trace_seq_terminate(s); +	comm = &s->buffer[len]; + +	/* Help out the comm to ids. This will handle dups */ +	pevent_register_comm(field->event->pevent, comm, pid); +} + +static int sched_wakeup_handler(struct trace_seq *s, +				struct pevent_record *record, +				struct event_format *event, void *context) +{ +	struct format_field *field; +	unsigned long long val; + +	if (pevent_get_field_val(s, event, "pid", record, &val, 1)) +		return trace_seq_putc(s, '!'); + +	field = pevent_find_any_field(event, "comm"); +	if (field) { +		write_and_save_comm(field, record, s, val); +		trace_seq_putc(s, ':'); +	} +	trace_seq_printf(s, "%lld", val); + +	if (pevent_get_field_val(s, event, "prio", record, &val, 0) == 0) +		trace_seq_printf(s, " [%lld]", val); + +	if (pevent_get_field_val(s, event, "success", record, &val, 1) == 0) +		trace_seq_printf(s, " success=%lld", val); + +	if (pevent_get_field_val(s, event, "target_cpu", record, &val, 0) == 0) +		trace_seq_printf(s, " CPU:%03llu", val); + +	return 0; +} + +static int sched_switch_handler(struct trace_seq *s, +				struct pevent_record *record, +				struct event_format *event, void *context) +{ +	struct format_field *field; +	unsigned long long val; + +	if (pevent_get_field_val(s, event, "prev_pid", record, &val, 1)) +		return trace_seq_putc(s, '!'); + +	field = pevent_find_any_field(event, "prev_comm"); +	if (field) { +		write_and_save_comm(field, record, s, val); +		trace_seq_putc(s, ':'); +	} +	trace_seq_printf(s, "%lld ", val); + +	if (pevent_get_field_val(s, event, "prev_prio", record, &val, 0) == 0) +		trace_seq_printf(s, "[%lld] ", val); + +	if (pevent_get_field_val(s,  event, "prev_state", record, &val, 0) == 0) +		write_state(s, val); + +	trace_seq_puts(s, " ==> "); + +	if (pevent_get_field_val(s, event, "next_pid", record, &val, 1)) +		return trace_seq_putc(s, '!'); + +	field = pevent_find_any_field(event, "next_comm"); +	if (field) { +		write_and_save_comm(field, record, s, val); +		trace_seq_putc(s, ':'); +	} +	trace_seq_printf(s, "%lld", val); + +	if (pevent_get_field_val(s, event, "next_prio", record, &val, 0) == 0) +		trace_seq_printf(s, " [%lld]", val); + +	return 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_event_handler(pevent, -1, "sched", "sched_switch", +				      sched_switch_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "sched", "sched_wakeup", +				      sched_wakeup_handler, NULL); + +	pevent_register_event_handler(pevent, -1, "sched", "sched_wakeup_new", +				      sched_wakeup_handler, NULL); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_event_handler(pevent, -1, "sched", "sched_switch", +					sched_switch_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "sched", "sched_wakeup", +					sched_wakeup_handler, NULL); + +	pevent_unregister_event_handler(pevent, -1, "sched", "sched_wakeup_new", +					sched_wakeup_handler, NULL); +} diff --git a/tools/lib/traceevent/plugin_scsi.c b/tools/lib/traceevent/plugin_scsi.c new file mode 100644 index 00000000000..eda326fc862 --- /dev/null +++ b/tools/lib/traceevent/plugin_scsi.c @@ -0,0 +1,429 @@ +#include <stdio.h> +#include <string.h> +#include <inttypes.h> +#include "event-parse.h" + +typedef unsigned long sector_t; +typedef uint64_t u64; +typedef unsigned int u32; + +/* + *      SCSI opcodes + */ +#define TEST_UNIT_READY			0x00 +#define REZERO_UNIT			0x01 +#define REQUEST_SENSE			0x03 +#define FORMAT_UNIT			0x04 +#define READ_BLOCK_LIMITS		0x05 +#define REASSIGN_BLOCKS			0x07 +#define INITIALIZE_ELEMENT_STATUS	0x07 +#define READ_6				0x08 +#define WRITE_6				0x0a +#define SEEK_6				0x0b +#define READ_REVERSE			0x0f +#define WRITE_FILEMARKS			0x10 +#define SPACE				0x11 +#define INQUIRY				0x12 +#define RECOVER_BUFFERED_DATA		0x14 +#define MODE_SELECT			0x15 +#define RESERVE				0x16 +#define RELEASE				0x17 +#define COPY				0x18 +#define ERASE				0x19 +#define MODE_SENSE			0x1a +#define START_STOP			0x1b +#define RECEIVE_DIAGNOSTIC		0x1c +#define SEND_DIAGNOSTIC			0x1d +#define ALLOW_MEDIUM_REMOVAL		0x1e + +#define READ_FORMAT_CAPACITIES		0x23 +#define SET_WINDOW			0x24 +#define READ_CAPACITY			0x25 +#define READ_10				0x28 +#define WRITE_10			0x2a +#define SEEK_10				0x2b +#define POSITION_TO_ELEMENT		0x2b +#define WRITE_VERIFY			0x2e +#define VERIFY				0x2f +#define SEARCH_HIGH			0x30 +#define SEARCH_EQUAL			0x31 +#define SEARCH_LOW			0x32 +#define SET_LIMITS			0x33 +#define PRE_FETCH			0x34 +#define READ_POSITION			0x34 +#define SYNCHRONIZE_CACHE		0x35 +#define LOCK_UNLOCK_CACHE		0x36 +#define READ_DEFECT_DATA		0x37 +#define MEDIUM_SCAN			0x38 +#define COMPARE				0x39 +#define COPY_VERIFY			0x3a +#define WRITE_BUFFER			0x3b +#define READ_BUFFER			0x3c +#define UPDATE_BLOCK			0x3d +#define READ_LONG			0x3e +#define WRITE_LONG			0x3f +#define CHANGE_DEFINITION		0x40 +#define WRITE_SAME			0x41 +#define UNMAP				0x42 +#define READ_TOC			0x43 +#define READ_HEADER			0x44 +#define GET_EVENT_STATUS_NOTIFICATION	0x4a +#define LOG_SELECT			0x4c +#define LOG_SENSE			0x4d +#define XDWRITEREAD_10			0x53 +#define MODE_SELECT_10			0x55 +#define RESERVE_10			0x56 +#define RELEASE_10			0x57 +#define MODE_SENSE_10			0x5a +#define PERSISTENT_RESERVE_IN		0x5e +#define PERSISTENT_RESERVE_OUT		0x5f +#define VARIABLE_LENGTH_CMD		0x7f +#define REPORT_LUNS			0xa0 +#define SECURITY_PROTOCOL_IN		0xa2 +#define MAINTENANCE_IN			0xa3 +#define MAINTENANCE_OUT			0xa4 +#define MOVE_MEDIUM			0xa5 +#define EXCHANGE_MEDIUM			0xa6 +#define READ_12				0xa8 +#define WRITE_12			0xaa +#define READ_MEDIA_SERIAL_NUMBER	0xab +#define WRITE_VERIFY_12			0xae +#define VERIFY_12			0xaf +#define SEARCH_HIGH_12			0xb0 +#define SEARCH_EQUAL_12			0xb1 +#define SEARCH_LOW_12			0xb2 +#define SECURITY_PROTOCOL_OUT		0xb5 +#define READ_ELEMENT_STATUS		0xb8 +#define SEND_VOLUME_TAG			0xb6 +#define WRITE_LONG_2			0xea +#define EXTENDED_COPY			0x83 +#define RECEIVE_COPY_RESULTS		0x84 +#define ACCESS_CONTROL_IN		0x86 +#define ACCESS_CONTROL_OUT		0x87 +#define READ_16				0x88 +#define WRITE_16			0x8a +#define READ_ATTRIBUTE			0x8c +#define WRITE_ATTRIBUTE			0x8d +#define VERIFY_16			0x8f +#define SYNCHRONIZE_CACHE_16		0x91 +#define WRITE_SAME_16			0x93 +#define SERVICE_ACTION_IN		0x9e +/* values for service action in */ +#define	SAI_READ_CAPACITY_16		0x10 +#define SAI_GET_LBA_STATUS		0x12 +/* values for VARIABLE_LENGTH_CMD service action codes + * see spc4r17 Section D.3.5, table D.7 and D.8 */ +#define VLC_SA_RECEIVE_CREDENTIAL	0x1800 +/* values for maintenance in */ +#define MI_REPORT_IDENTIFYING_INFORMATION		0x05 +#define MI_REPORT_TARGET_PGS				0x0a +#define MI_REPORT_ALIASES				0x0b +#define MI_REPORT_SUPPORTED_OPERATION_CODES		0x0c +#define MI_REPORT_SUPPORTED_TASK_MANAGEMENT_FUNCTIONS	0x0d +#define MI_REPORT_PRIORITY				0x0e +#define MI_REPORT_TIMESTAMP				0x0f +#define MI_MANAGEMENT_PROTOCOL_IN			0x10 +/* value for MI_REPORT_TARGET_PGS ext header */ +#define MI_EXT_HDR_PARAM_FMT		0x20 +/* values for maintenance out */ +#define MO_SET_IDENTIFYING_INFORMATION	0x06 +#define MO_SET_TARGET_PGS		0x0a +#define MO_CHANGE_ALIASES		0x0b +#define MO_SET_PRIORITY			0x0e +#define MO_SET_TIMESTAMP		0x0f +#define MO_MANAGEMENT_PROTOCOL_OUT	0x10 +/* values for variable length command */ +#define XDREAD_32			0x03 +#define XDWRITE_32			0x04 +#define XPWRITE_32			0x06 +#define XDWRITEREAD_32			0x07 +#define READ_32				0x09 +#define VERIFY_32			0x0a +#define WRITE_32			0x0b +#define WRITE_SAME_32			0x0d + +#define SERVICE_ACTION16(cdb) (cdb[1] & 0x1f) +#define SERVICE_ACTION32(cdb) ((cdb[8] << 8) | cdb[9]) + +static const char * +scsi_trace_misc(struct trace_seq *, unsigned char *, int); + +static const char * +scsi_trace_rw6(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len; +	sector_t lba = 0, txlen = 0; + +	lba |= ((cdb[1] & 0x1F) << 16); +	lba |=  (cdb[2] << 8); +	lba |=   cdb[3]; +	txlen = cdb[4]; + +	trace_seq_printf(p, "lba=%llu txlen=%llu", +			 (unsigned long long)lba, (unsigned long long)txlen); +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_rw10(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len; +	sector_t lba = 0, txlen = 0; + +	lba |= (cdb[2] << 24); +	lba |= (cdb[3] << 16); +	lba |= (cdb[4] << 8); +	lba |=  cdb[5]; +	txlen |= (cdb[7] << 8); +	txlen |=  cdb[8]; + +	trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u", +			 (unsigned long long)lba, (unsigned long long)txlen, +			 cdb[1] >> 5); + +	if (cdb[0] == WRITE_SAME) +		trace_seq_printf(p, " unmap=%u", cdb[1] >> 3 & 1); + +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_rw12(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len; +	sector_t lba = 0, txlen = 0; + +	lba |= (cdb[2] << 24); +	lba |= (cdb[3] << 16); +	lba |= (cdb[4] << 8); +	lba |=  cdb[5]; +	txlen |= (cdb[6] << 24); +	txlen |= (cdb[7] << 16); +	txlen |= (cdb[8] << 8); +	txlen |=  cdb[9]; + +	trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u", +			 (unsigned long long)lba, (unsigned long long)txlen, +			 cdb[1] >> 5); +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_rw16(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len; +	sector_t lba = 0, txlen = 0; + +	lba |= ((u64)cdb[2] << 56); +	lba |= ((u64)cdb[3] << 48); +	lba |= ((u64)cdb[4] << 40); +	lba |= ((u64)cdb[5] << 32); +	lba |= (cdb[6] << 24); +	lba |= (cdb[7] << 16); +	lba |= (cdb[8] << 8); +	lba |=  cdb[9]; +	txlen |= (cdb[10] << 24); +	txlen |= (cdb[11] << 16); +	txlen |= (cdb[12] << 8); +	txlen |=  cdb[13]; + +	trace_seq_printf(p, "lba=%llu txlen=%llu protect=%u", +			 (unsigned long long)lba, (unsigned long long)txlen, +			 cdb[1] >> 5); + +	if (cdb[0] == WRITE_SAME_16) +		trace_seq_printf(p, " unmap=%u", cdb[1] >> 3 & 1); + +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_rw32(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len, *cmd; +	sector_t lba = 0, txlen = 0; +	u32 ei_lbrt = 0; + +	switch (SERVICE_ACTION32(cdb)) { +	case READ_32: +		cmd = "READ"; +		break; +	case VERIFY_32: +		cmd = "VERIFY"; +		break; +	case WRITE_32: +		cmd = "WRITE"; +		break; +	case WRITE_SAME_32: +		cmd = "WRITE_SAME"; +		break; +	default: +		trace_seq_printf(p, "UNKNOWN"); +		goto out; +	} + +	lba |= ((u64)cdb[12] << 56); +	lba |= ((u64)cdb[13] << 48); +	lba |= ((u64)cdb[14] << 40); +	lba |= ((u64)cdb[15] << 32); +	lba |= (cdb[16] << 24); +	lba |= (cdb[17] << 16); +	lba |= (cdb[18] << 8); +	lba |=  cdb[19]; +	ei_lbrt |= (cdb[20] << 24); +	ei_lbrt |= (cdb[21] << 16); +	ei_lbrt |= (cdb[22] << 8); +	ei_lbrt |=  cdb[23]; +	txlen |= (cdb[28] << 24); +	txlen |= (cdb[29] << 16); +	txlen |= (cdb[30] << 8); +	txlen |=  cdb[31]; + +	trace_seq_printf(p, "%s_32 lba=%llu txlen=%llu protect=%u ei_lbrt=%u", +			 cmd, (unsigned long long)lba, +			 (unsigned long long)txlen, cdb[10] >> 5, ei_lbrt); + +	if (SERVICE_ACTION32(cdb) == WRITE_SAME_32) +		trace_seq_printf(p, " unmap=%u", cdb[10] >> 3 & 1); + +out: +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_unmap(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len; +	unsigned int regions = cdb[7] << 8 | cdb[8]; + +	trace_seq_printf(p, "regions=%u", (regions - 8) / 16); +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_service_action_in(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len, *cmd; +	sector_t lba = 0; +	u32 alloc_len = 0; + +	switch (SERVICE_ACTION16(cdb)) { +	case SAI_READ_CAPACITY_16: +		cmd = "READ_CAPACITY_16"; +		break; +	case SAI_GET_LBA_STATUS: +		cmd = "GET_LBA_STATUS"; +		break; +	default: +		trace_seq_printf(p, "UNKNOWN"); +		goto out; +	} + +	lba |= ((u64)cdb[2] << 56); +	lba |= ((u64)cdb[3] << 48); +	lba |= ((u64)cdb[4] << 40); +	lba |= ((u64)cdb[5] << 32); +	lba |= (cdb[6] << 24); +	lba |= (cdb[7] << 16); +	lba |= (cdb[8] << 8); +	lba |=  cdb[9]; +	alloc_len |= (cdb[10] << 24); +	alloc_len |= (cdb[11] << 16); +	alloc_len |= (cdb[12] << 8); +	alloc_len |=  cdb[13]; + +	trace_seq_printf(p, "%s lba=%llu alloc_len=%u", cmd, +			 (unsigned long long)lba, alloc_len); + +out: +	trace_seq_putc(p, 0); +	return ret; +} + +static const char * +scsi_trace_varlen(struct trace_seq *p, unsigned char *cdb, int len) +{ +	switch (SERVICE_ACTION32(cdb)) { +	case READ_32: +	case VERIFY_32: +	case WRITE_32: +	case WRITE_SAME_32: +		return scsi_trace_rw32(p, cdb, len); +	default: +		return scsi_trace_misc(p, cdb, len); +	} +} + +static const char * +scsi_trace_misc(struct trace_seq *p, unsigned char *cdb, int len) +{ +	const char *ret = p->buffer + p->len; + +	trace_seq_printf(p, "-"); +	trace_seq_putc(p, 0); +	return ret; +} + +const char * +scsi_trace_parse_cdb(struct trace_seq *p, unsigned char *cdb, int len) +{ +	switch (cdb[0]) { +	case READ_6: +	case WRITE_6: +		return scsi_trace_rw6(p, cdb, len); +	case READ_10: +	case VERIFY: +	case WRITE_10: +	case WRITE_SAME: +		return scsi_trace_rw10(p, cdb, len); +	case READ_12: +	case VERIFY_12: +	case WRITE_12: +		return scsi_trace_rw12(p, cdb, len); +	case READ_16: +	case VERIFY_16: +	case WRITE_16: +	case WRITE_SAME_16: +		return scsi_trace_rw16(p, cdb, len); +	case UNMAP: +		return scsi_trace_unmap(p, cdb, len); +	case SERVICE_ACTION_IN: +		return scsi_trace_service_action_in(p, cdb, len); +	case VARIABLE_LENGTH_CMD: +		return scsi_trace_varlen(p, cdb, len); +	default: +		return scsi_trace_misc(p, cdb, len); +	} +} + +unsigned long long process_scsi_trace_parse_cdb(struct trace_seq *s, +						unsigned long long *args) +{ +	scsi_trace_parse_cdb(s, (unsigned char *) (unsigned long) args[1], args[2]); +	return 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_print_function(pevent, +				       process_scsi_trace_parse_cdb, +				       PEVENT_FUNC_ARG_STRING, +				       "scsi_trace_parse_cdb", +				       PEVENT_FUNC_ARG_PTR, +				       PEVENT_FUNC_ARG_PTR, +				       PEVENT_FUNC_ARG_INT, +				       PEVENT_FUNC_ARG_VOID); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_print_function(pevent, process_scsi_trace_parse_cdb, +					 "scsi_trace_parse_cdb"); +} diff --git a/tools/lib/traceevent/plugin_xen.c b/tools/lib/traceevent/plugin_xen.c new file mode 100644 index 00000000000..3a413eaada6 --- /dev/null +++ b/tools/lib/traceevent/plugin_xen.c @@ -0,0 +1,136 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include "event-parse.h" + +#define __HYPERVISOR_set_trap_table			0 +#define __HYPERVISOR_mmu_update				1 +#define __HYPERVISOR_set_gdt				2 +#define __HYPERVISOR_stack_switch			3 +#define __HYPERVISOR_set_callbacks			4 +#define __HYPERVISOR_fpu_taskswitch			5 +#define __HYPERVISOR_sched_op_compat			6 +#define __HYPERVISOR_dom0_op				7 +#define __HYPERVISOR_set_debugreg			8 +#define __HYPERVISOR_get_debugreg			9 +#define __HYPERVISOR_update_descriptor			10 +#define __HYPERVISOR_memory_op				12 +#define __HYPERVISOR_multicall				13 +#define __HYPERVISOR_update_va_mapping			14 +#define __HYPERVISOR_set_timer_op			15 +#define __HYPERVISOR_event_channel_op_compat		16 +#define __HYPERVISOR_xen_version			17 +#define __HYPERVISOR_console_io				18 +#define __HYPERVISOR_physdev_op_compat			19 +#define __HYPERVISOR_grant_table_op			20 +#define __HYPERVISOR_vm_assist				21 +#define __HYPERVISOR_update_va_mapping_otherdomain	22 +#define __HYPERVISOR_iret				23 /* x86 only */ +#define __HYPERVISOR_vcpu_op				24 +#define __HYPERVISOR_set_segment_base			25 /* x86/64 only */ +#define __HYPERVISOR_mmuext_op				26 +#define __HYPERVISOR_acm_op				27 +#define __HYPERVISOR_nmi_op				28 +#define __HYPERVISOR_sched_op				29 +#define __HYPERVISOR_callback_op			30 +#define __HYPERVISOR_xenoprof_op			31 +#define __HYPERVISOR_event_channel_op			32 +#define __HYPERVISOR_physdev_op				33 +#define __HYPERVISOR_hvm_op				34 +#define __HYPERVISOR_tmem_op				38 + +/* Architecture-specific hypercall definitions. */ +#define __HYPERVISOR_arch_0				48 +#define __HYPERVISOR_arch_1				49 +#define __HYPERVISOR_arch_2				50 +#define __HYPERVISOR_arch_3				51 +#define __HYPERVISOR_arch_4				52 +#define __HYPERVISOR_arch_5				53 +#define __HYPERVISOR_arch_6				54 +#define __HYPERVISOR_arch_7				55 + +#define N(x)	[__HYPERVISOR_##x] = "("#x")" +static const char *xen_hypercall_names[] = { +	N(set_trap_table), +	N(mmu_update), +	N(set_gdt), +	N(stack_switch), +	N(set_callbacks), +	N(fpu_taskswitch), +	N(sched_op_compat), +	N(dom0_op), +	N(set_debugreg), +	N(get_debugreg), +	N(update_descriptor), +	N(memory_op), +	N(multicall), +	N(update_va_mapping), +	N(set_timer_op), +	N(event_channel_op_compat), +	N(xen_version), +	N(console_io), +	N(physdev_op_compat), +	N(grant_table_op), +	N(vm_assist), +	N(update_va_mapping_otherdomain), +	N(iret), +	N(vcpu_op), +	N(set_segment_base), +	N(mmuext_op), +	N(acm_op), +	N(nmi_op), +	N(sched_op), +	N(callback_op), +	N(xenoprof_op), +	N(event_channel_op), +	N(physdev_op), +	N(hvm_op), + +/* Architecture-specific hypercall definitions. */ +	N(arch_0), +	N(arch_1), +	N(arch_2), +	N(arch_3), +	N(arch_4), +	N(arch_5), +	N(arch_6), +	N(arch_7), +}; +#undef N + +#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0])) + +static const char *xen_hypercall_name(unsigned op) +{ +	if (op < ARRAY_SIZE(xen_hypercall_names) && +	    xen_hypercall_names[op] != NULL) +		return xen_hypercall_names[op]; + +	return ""; +} + +unsigned long long process_xen_hypercall_name(struct trace_seq *s, +					      unsigned long long *args) +{ +	unsigned int op = args[0]; + +	trace_seq_printf(s, "%s", xen_hypercall_name(op)); +	return 0; +} + +int PEVENT_PLUGIN_LOADER(struct pevent *pevent) +{ +	pevent_register_print_function(pevent, +				       process_xen_hypercall_name, +				       PEVENT_FUNC_ARG_STRING, +				       "xen_hypercall_name", +				       PEVENT_FUNC_ARG_INT, +				       PEVENT_FUNC_ARG_VOID); +	return 0; +} + +void PEVENT_PLUGIN_UNLOADER(struct pevent *pevent) +{ +	pevent_unregister_print_function(pevent, process_xen_hypercall_name, +					 "xen_hypercall_name"); +} diff --git a/tools/lib/traceevent/trace-seq.c b/tools/lib/traceevent/trace-seq.c index d7f2e68bc5b..ec3bd16a548 100644 --- a/tools/lib/traceevent/trace-seq.c +++ b/tools/lib/traceevent/trace-seq.c @@ -22,6 +22,7 @@  #include <string.h>  #include <stdarg.h> +#include <asm/bug.h>  #include "event-parse.h"  #include "event-utils.h" @@ -32,10 +33,21 @@  #define TRACE_SEQ_POISON	((void *)0xdeadbeef)  #define TRACE_SEQ_CHECK(s)						\  do {									\ -	if ((s)->buffer == TRACE_SEQ_POISON)			\ -		die("Usage of trace_seq after it was destroyed");	\ +	if (WARN_ONCE((s)->buffer == TRACE_SEQ_POISON,			\ +		      "Usage of trace_seq after it was destroyed"))	\ +		(s)->state = TRACE_SEQ__BUFFER_POISONED;		\  } while (0) +#define TRACE_SEQ_CHECK_RET_N(s, n)		\ +do {						\ +	TRACE_SEQ_CHECK(s);			\ +	if ((s)->state != TRACE_SEQ__GOOD)	\ +		return n; 			\ +} while (0) + +#define TRACE_SEQ_CHECK_RET(s)   TRACE_SEQ_CHECK_RET_N(s, ) +#define TRACE_SEQ_CHECK_RET0(s)  TRACE_SEQ_CHECK_RET_N(s, 0) +  /**   * trace_seq_init - initialize the trace_seq structure   * @s: a pointer to the trace_seq structure to initialize @@ -45,7 +57,11 @@ void trace_seq_init(struct trace_seq *s)  	s->len = 0;  	s->readpos = 0;  	s->buffer_size = TRACE_SEQ_BUF_SIZE; -	s->buffer = malloc_or_die(s->buffer_size); +	s->buffer = malloc(s->buffer_size); +	if (s->buffer != NULL) +		s->state = TRACE_SEQ__GOOD; +	else +		s->state = TRACE_SEQ__MEM_ALLOC_FAILED;  }  /** @@ -71,17 +87,23 @@ void trace_seq_destroy(struct trace_seq *s)  {  	if (!s)  		return; -	TRACE_SEQ_CHECK(s); +	TRACE_SEQ_CHECK_RET(s);  	free(s->buffer);  	s->buffer = TRACE_SEQ_POISON;  }  static void expand_buffer(struct trace_seq *s)  { +	char *buf; + +	buf = realloc(s->buffer, s->buffer_size + TRACE_SEQ_BUF_SIZE); +	if (WARN_ONCE(!buf, "Can't allocate trace_seq buffer memory")) { +		s->state = TRACE_SEQ__MEM_ALLOC_FAILED; +		return; +	} + +	s->buffer = buf;  	s->buffer_size += TRACE_SEQ_BUF_SIZE; -	s->buffer = realloc(s->buffer, s->buffer_size); -	if (!s->buffer) -		die("Can't allocate trace_seq buffer memory");  }  /** @@ -105,9 +127,9 @@ trace_seq_printf(struct trace_seq *s, const char *fmt, ...)  	int len;  	int ret; -	TRACE_SEQ_CHECK(s); -   try_again: +	TRACE_SEQ_CHECK_RET0(s); +  	len = (s->buffer_size - 1) - s->len;  	va_start(ap, fmt); @@ -141,9 +163,9 @@ trace_seq_vprintf(struct trace_seq *s, const char *fmt, va_list args)  	int len;  	int ret; -	TRACE_SEQ_CHECK(s); -   try_again: +	TRACE_SEQ_CHECK_RET0(s); +  	len = (s->buffer_size - 1) - s->len;  	ret = vsnprintf(s->buffer + s->len, len, fmt, args); @@ -172,13 +194,15 @@ int trace_seq_puts(struct trace_seq *s, const char *str)  {  	int len; -	TRACE_SEQ_CHECK(s); +	TRACE_SEQ_CHECK_RET0(s);  	len = strlen(str);  	while (len > ((s->buffer_size - 1) - s->len))  		expand_buffer(s); +	TRACE_SEQ_CHECK_RET0(s); +  	memcpy(s->buffer + s->len, str, len);  	s->len += len; @@ -187,11 +211,13 @@ int trace_seq_puts(struct trace_seq *s, const char *str)  int trace_seq_putc(struct trace_seq *s, unsigned char c)  { -	TRACE_SEQ_CHECK(s); +	TRACE_SEQ_CHECK_RET0(s);  	while (s->len >= (s->buffer_size - 1))  		expand_buffer(s); +	TRACE_SEQ_CHECK_RET0(s); +  	s->buffer[s->len++] = c;  	return 1; @@ -199,7 +225,7 @@ int trace_seq_putc(struct trace_seq *s, unsigned char c)  void trace_seq_terminate(struct trace_seq *s)  { -	TRACE_SEQ_CHECK(s); +	TRACE_SEQ_CHECK_RET(s);  	/* There's always one character left on the buffer */  	s->buffer[s->len] = 0; @@ -208,5 +234,16 @@ void trace_seq_terminate(struct trace_seq *s)  int trace_seq_do_printf(struct trace_seq *s)  {  	TRACE_SEQ_CHECK(s); -	return printf("%.*s", s->len, s->buffer); + +	switch (s->state) { +	case TRACE_SEQ__GOOD: +		return printf("%.*s", s->len, s->buffer); +	case TRACE_SEQ__BUFFER_POISONED: +		puts("Usage of trace_seq after it was destroyed"); +		break; +	case TRACE_SEQ__MEM_ALLOC_FAILED: +		puts("Can't allocate trace_seq buffer memory"); +		break; +	} +	return -1;  }  | 
