diff options
Diffstat (limited to 'tools/lib/api')
| -rw-r--r-- | tools/lib/api/Build | 2 | ||||
| -rw-r--r-- | tools/lib/api/Makefile | 58 | ||||
| -rw-r--r-- | tools/lib/api/fd/Build | 1 | ||||
| -rw-r--r-- | tools/lib/api/fs/Build | 4 | ||||
| -rw-r--r-- | tools/lib/api/fs/debugfs.c | 69 | ||||
| -rw-r--r-- | tools/lib/api/fs/debugfs.h | 13 | ||||
| -rw-r--r-- | tools/lib/api/fs/findfs.c | 63 | ||||
| -rw-r--r-- | tools/lib/api/fs/findfs.h | 23 | ||||
| -rw-r--r-- | tools/lib/api/fs/tracefs.c | 78 | ||||
| -rw-r--r-- | tools/lib/api/fs/tracefs.h | 21 | 
10 files changed, 242 insertions, 90 deletions
diff --git a/tools/lib/api/Build b/tools/lib/api/Build new file mode 100644 index 000000000000..3653965cf481 --- /dev/null +++ b/tools/lib/api/Build @@ -0,0 +1,2 @@ +libapi-y += fd/ +libapi-y += fs/ diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile index 36c08b1f4afb..d8fe29fc19a4 100644 --- a/tools/lib/api/Makefile +++ b/tools/lib/api/Makefile @@ -1,49 +1,43 @@  include ../../scripts/Makefile.include  include ../../perf/config/utilities.mak		# QUIET_CLEAN +ifeq ($(srctree),) +srctree := $(patsubst %/,%,$(dir $(shell pwd))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +srctree := $(patsubst %/,%,$(dir $(srctree))) +#$(info Determined 'srctree' to be $(srctree)) +endif +  CC = $(CROSS_COMPILE)gcc  AR = $(CROSS_COMPILE)ar -# guard against environment variables -LIB_H= -LIB_OBJS= - -LIB_H += fs/debugfs.h -LIB_H += fs/fs.h -# See comment below about piggybacking... -LIB_H += fd/array.h - -LIB_OBJS += $(OUTPUT)fs/debugfs.o -LIB_OBJS += $(OUTPUT)fs/fs.o -# XXX piggybacking here, need to introduce libapikfd, or rename this -# to plain libapik.a and make it have it all api goodies -LIB_OBJS += $(OUTPUT)fd/array.o +MAKEFLAGS += --no-print-directory -LIBFILE = libapikfs.a +LIBFILE = $(OUTPUT)libapi.a -CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) -fPIC -EXTLIBS = -lelf -lpthread -lrt -lm -ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64 -ALL_LDFLAGS = $(LDFLAGS) +CFLAGS := $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) +CFLAGS += -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 -fPIC +CFLAGS += -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64  RM = rm -f -$(LIBFILE): $(LIB_OBJS) -	$(QUIET_AR)$(RM) $@ && $(AR) rcs $(OUTPUT)$@ $(LIB_OBJS) +build  := -f $(srctree)/tools/build/Makefile.build dir=. obj +API_IN := $(OUTPUT)libapi-in.o -$(LIB_OBJS): $(LIB_H) +export srctree OUTPUT CC LD CFLAGS V -libapi_dirs: -	$(QUIET_MKDIR)mkdir -p $(OUTPUT)fd $(OUTPUT)fs +all: $(LIBFILE) -$(OUTPUT)%.o: %.c libapi_dirs -	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< -$(OUTPUT)%.s: %.c libapi_dirs -	$(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $< -$(OUTPUT)%.o: %.S libapi_dirs -	$(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $< +$(API_IN): FORCE +	@$(MAKE) $(build)=libapi + +$(LIBFILE): $(API_IN) +	$(QUIET_AR)$(RM) $@ && $(AR) rcs $@ $(API_IN)  clean: -	$(call QUIET_CLEAN, libapi) $(RM) $(LIB_OBJS) $(LIBFILE) +	$(call QUIET_CLEAN, libapi) $(RM) $(LIBFILE); \ +	find $(if $(OUTPUT),$(OUTPUT),.) -name \*.o | xargs $(RM) + +FORCE: -.PHONY: clean +.PHONY: clean FORCE diff --git a/tools/lib/api/fd/Build b/tools/lib/api/fd/Build new file mode 100644 index 000000000000..605d99f6d71a --- /dev/null +++ b/tools/lib/api/fd/Build @@ -0,0 +1 @@ +libapi-y += array.o diff --git a/tools/lib/api/fs/Build b/tools/lib/api/fs/Build new file mode 100644 index 000000000000..6de5a4f0b501 --- /dev/null +++ b/tools/lib/api/fs/Build @@ -0,0 +1,4 @@ +libapi-y += fs.o +libapi-y += debugfs.o +libapi-y += findfs.o +libapi-y += tracefs.o diff --git a/tools/lib/api/fs/debugfs.c b/tools/lib/api/fs/debugfs.c index d2b18e887071..8305b3e9d48e 100644 --- a/tools/lib/api/fs/debugfs.c +++ b/tools/lib/api/fs/debugfs.c @@ -3,75 +3,50 @@  #include <stdio.h>  #include <stdlib.h>  #include <string.h> +#include <unistd.h>  #include <stdbool.h>  #include <sys/vfs.h> +#include <sys/types.h> +#include <sys/stat.h>  #include <sys/mount.h>  #include <linux/kernel.h>  #include "debugfs.h" -char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug"; +#ifndef DEBUGFS_DEFAULT_PATH +#define DEBUGFS_DEFAULT_PATH		"/sys/kernel/debug" +#endif + +char debugfs_mountpoint[PATH_MAX + 1] = DEBUGFS_DEFAULT_PATH;  static const char * const debugfs_known_mountpoints[] = { -	"/sys/kernel/debug", +	DEBUGFS_DEFAULT_PATH,  	"/debug",  	0,  };  static bool debugfs_found; +bool debugfs_configured(void) +{ +	return debugfs_find_mountpoint() != NULL; +} +  /* find the path to the mounted debugfs */  const char *debugfs_find_mountpoint(void)  { -	const char * const *ptr; -	char type[100]; -	FILE *fp; +	const char *ret;  	if (debugfs_found)  		return (const char *)debugfs_mountpoint; -	ptr = debugfs_known_mountpoints; -	while (*ptr) { -		if (debugfs_valid_mountpoint(*ptr) == 0) { -			debugfs_found = true; -			strcpy(debugfs_mountpoint, *ptr); -			return debugfs_mountpoint; -		} -		ptr++; -	} - -	/* give up and parse /proc/mounts */ -	fp = fopen("/proc/mounts", "r"); -	if (fp == NULL) -		return NULL; - -	while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n", -		      debugfs_mountpoint, type) == 2) { -		if (strcmp(type, "debugfs") == 0) -			break; -	} -	fclose(fp); +	ret = find_mountpoint("debugfs", (long) DEBUGFS_MAGIC, +			      debugfs_mountpoint, PATH_MAX + 1, +			      debugfs_known_mountpoints); +	if (ret) +		debugfs_found = true; -	if (strcmp(type, "debugfs") != 0) -		return NULL; - -	debugfs_found = true; - -	return debugfs_mountpoint; -} - -/* verify that a mountpoint is actually a debugfs instance */ - -int debugfs_valid_mountpoint(const char *debugfs) -{ -	struct statfs st_fs; - -	if (statfs(debugfs, &st_fs) < 0) -		return -ENOENT; -	else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC) -		return -ENOENT; - -	return 0; +	return ret;  }  /* mount the debugfs somewhere if it's not mounted */ @@ -87,7 +62,7 @@ char *debugfs_mount(const char *mountpoint)  		mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);  		/* if no environment variable, use default */  		if (mountpoint == NULL) -			mountpoint = "/sys/kernel/debug"; +			mountpoint = DEBUGFS_DEFAULT_PATH;  	}  	if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0) diff --git a/tools/lib/api/fs/debugfs.h b/tools/lib/api/fs/debugfs.h index 0739881a9897..455023698d2b 100644 --- a/tools/lib/api/fs/debugfs.h +++ b/tools/lib/api/fs/debugfs.h @@ -1,16 +1,7 @@  #ifndef __API_DEBUGFS_H__  #define __API_DEBUGFS_H__ -#define _STR(x) #x -#define STR(x) _STR(x) - -/* - * On most systems <limits.h> would have given us this, but  not on some systems - * (e.g. GNU/Hurd). - */ -#ifndef PATH_MAX -#define PATH_MAX 4096 -#endif +#include "findfs.h"  #ifndef DEBUGFS_MAGIC  #define DEBUGFS_MAGIC          0x64626720 @@ -20,8 +11,8 @@  #define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"  #endif +bool debugfs_configured(void);  const char *debugfs_find_mountpoint(void); -int debugfs_valid_mountpoint(const char *debugfs);  char *debugfs_mount(const char *mountpoint);  extern char debugfs_mountpoint[]; diff --git a/tools/lib/api/fs/findfs.c b/tools/lib/api/fs/findfs.c new file mode 100644 index 000000000000..49946cb6d7af --- /dev/null +++ b/tools/lib/api/fs/findfs.c @@ -0,0 +1,63 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <stdbool.h> +#include <sys/vfs.h> + +#include "findfs.h" + +/* verify that a mountpoint is actually the type we want */ + +int valid_mountpoint(const char *mount, long magic) +{ +	struct statfs st_fs; + +	if (statfs(mount, &st_fs) < 0) +		return -ENOENT; +	else if ((long)st_fs.f_type != magic) +		return -ENOENT; + +	return 0; +} + +/* find the path to a mounted file system */ +const char *find_mountpoint(const char *fstype, long magic, +			    char *mountpoint, int len, +			    const char * const *known_mountpoints) +{ +	const char * const *ptr; +	char format[128]; +	char type[100]; +	FILE *fp; + +	if (known_mountpoints) { +		ptr = known_mountpoints; +		while (*ptr) { +			if (valid_mountpoint(*ptr, magic) == 0) { +				strncpy(mountpoint, *ptr, len - 1); +				mountpoint[len-1] = 0; +				return mountpoint; +			} +			ptr++; +		} +	} + +	/* give up and parse /proc/mounts */ +	fp = fopen("/proc/mounts", "r"); +	if (fp == NULL) +		return NULL; + +	snprintf(format, 128, "%%*s %%%ds %%99s %%*s %%*d %%*d\n", len); + +	while (fscanf(fp, format, mountpoint, type) == 2) { +		if (strcmp(type, fstype) == 0) +			break; +	} +	fclose(fp); + +	if (strcmp(type, fstype) != 0) +		return NULL; + +	return mountpoint; +} diff --git a/tools/lib/api/fs/findfs.h b/tools/lib/api/fs/findfs.h new file mode 100644 index 000000000000..b6f5d05acc42 --- /dev/null +++ b/tools/lib/api/fs/findfs.h @@ -0,0 +1,23 @@ +#ifndef __API_FINDFS_H__ +#define __API_FINDFS_H__ + +#include <stdbool.h> + +#define _STR(x) #x +#define STR(x) _STR(x) + +/* + * On most systems <limits.h> would have given us this, but  not on some systems + * (e.g. GNU/Hurd). + */ +#ifndef PATH_MAX +#define PATH_MAX 4096 +#endif + +const char *find_mountpoint(const char *fstype, long magic, +			    char *mountpoint, int len, +			    const char * const *known_mountpoints); + +int valid_mountpoint(const char *mount, long magic); + +#endif /* __API_FINDFS_H__ */ diff --git a/tools/lib/api/fs/tracefs.c b/tools/lib/api/fs/tracefs.c new file mode 100644 index 000000000000..e4aa9688b71e --- /dev/null +++ b/tools/lib/api/fs/tracefs.c @@ -0,0 +1,78 @@ +#include <errno.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> +#include <stdbool.h> +#include <sys/vfs.h> +#include <sys/types.h> +#include <sys/stat.h> +#include <sys/mount.h> +#include <linux/kernel.h> + +#include "tracefs.h" + +#ifndef TRACEFS_DEFAULT_PATH +#define TRACEFS_DEFAULT_PATH		"/sys/kernel/tracing" +#endif + +char tracefs_mountpoint[PATH_MAX + 1] = TRACEFS_DEFAULT_PATH; + +static const char * const tracefs_known_mountpoints[] = { +	TRACEFS_DEFAULT_PATH, +	"/sys/kernel/debug/tracing", +	"/tracing", +	"/trace", +	0, +}; + +static bool tracefs_found; + +bool tracefs_configured(void) +{ +	return tracefs_find_mountpoint() != NULL; +} + +/* find the path to the mounted tracefs */ +const char *tracefs_find_mountpoint(void) +{ +	const char *ret; + +	if (tracefs_found) +		return (const char *)tracefs_mountpoint; + +	ret = find_mountpoint("tracefs", (long) TRACEFS_MAGIC, +			      tracefs_mountpoint, PATH_MAX + 1, +			      tracefs_known_mountpoints); + +	if (ret) +		tracefs_found = true; + +	return ret; +} + +/* mount the tracefs somewhere if it's not mounted */ +char *tracefs_mount(const char *mountpoint) +{ +	/* see if it's already mounted */ +	if (tracefs_find_mountpoint()) +		goto out; + +	/* if not mounted and no argument */ +	if (mountpoint == NULL) { +		/* see if environment variable set */ +		mountpoint = getenv(PERF_TRACEFS_ENVIRONMENT); +		/* if no environment variable, use default */ +		if (mountpoint == NULL) +			mountpoint = TRACEFS_DEFAULT_PATH; +	} + +	if (mount(NULL, mountpoint, "tracefs", 0, NULL) < 0) +		return NULL; + +	/* save the mountpoint */ +	tracefs_found = true; +	strncpy(tracefs_mountpoint, mountpoint, sizeof(tracefs_mountpoint)); +out: +	return tracefs_mountpoint; +} diff --git a/tools/lib/api/fs/tracefs.h b/tools/lib/api/fs/tracefs.h new file mode 100644 index 000000000000..da780ac49acb --- /dev/null +++ b/tools/lib/api/fs/tracefs.h @@ -0,0 +1,21 @@ +#ifndef __API_TRACEFS_H__ +#define __API_TRACEFS_H__ + +#include "findfs.h" + +#ifndef TRACEFS_MAGIC +#define TRACEFS_MAGIC          0x74726163 +#endif + +#ifndef PERF_TRACEFS_ENVIRONMENT +#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR" +#endif + +bool tracefs_configured(void); +const char *tracefs_find_mountpoint(void); +int tracefs_valid_mountpoint(const char *debugfs); +char *tracefs_mount(const char *mountpoint); + +extern char tracefs_mountpoint[]; + +#endif /* __API_DEBUGFS_H__ */  |