aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/api
diff options
context:
space:
mode:
Diffstat (limited to 'tools/lib/api')
-rw-r--r--tools/lib/api/Makefile42
-rw-r--r--tools/lib/api/fs/debugfs.c100
-rw-r--r--tools/lib/api/fs/debugfs.h29
3 files changed, 171 insertions, 0 deletions
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile
new file mode 100644
index 000000000000..ed2f51e11b80
--- /dev/null
+++ b/tools/lib/api/Makefile
@@ -0,0 +1,42 @@
1include ../../scripts/Makefile.include
2include ../../perf/config/utilities.mak # QUIET_CLEAN
3
4CC = $(CROSS_COMPILE)gcc
5AR = $(CROSS_COMPILE)ar
6
7# guard against environment variables
8LIB_H=
9LIB_OBJS=
10
11LIB_H += fs/debugfs.h
12
13LIB_OBJS += $(OUTPUT)fs/debugfs.o
14
15LIBFILE = libapikfs.a
16
17CFLAGS = -ggdb3 -Wall -Wextra -std=gnu99 -Werror -O6 -D_FORTIFY_SOURCE=2 $(EXTRA_WARNINGS) $(EXTRA_CFLAGS) -fPIC
18EXTLIBS = -lelf -lpthread -lrt -lm
19ALL_CFLAGS = $(CFLAGS) $(BASIC_CFLAGS) -D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64
20ALL_LDFLAGS = $(LDFLAGS)
21
22RM = rm -f
23
24$(LIBFILE): $(LIB_OBJS)
25 $(QUIET_AR)$(RM) $@ && $(AR) rcs $(OUTPUT)$@ $(LIB_OBJS)
26
27$(LIB_OBJS): $(LIB_H)
28
29libapi_dirs:
30 $(QUIET_MKDIR)mkdir -p $(OUTPUT)fs/
31
32$(OUTPUT)%.o: %.c libapi_dirs
33 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<
34$(OUTPUT)%.s: %.c libapi_dirs
35 $(QUIET_CC)$(CC) -S $(ALL_CFLAGS) $<
36$(OUTPUT)%.o: %.S libapi_dirs
37 $(QUIET_CC)$(CC) -o $@ -c $(ALL_CFLAGS) $<
38
39clean:
40 $(call QUIET_CLEAN, libapi) $(RM) $(LIB_OBJS) $(LIBFILE)
41
42.PHONY: clean
diff --git a/tools/lib/api/fs/debugfs.c b/tools/lib/api/fs/debugfs.c
new file mode 100644
index 000000000000..7c4347962353
--- /dev/null
+++ b/tools/lib/api/fs/debugfs.c
@@ -0,0 +1,100 @@
1#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <sys/vfs.h>
7#include <sys/mount.h>
8#include <linux/kernel.h>
9
10#include "debugfs.h"
11
12char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
13
14static const char * const debugfs_known_mountpoints[] = {
15 "/sys/kernel/debug/",
16 "/debug/",
17 0,
18};
19
20static bool debugfs_found;
21
22/* find the path to the mounted debugfs */
23const char *debugfs_find_mountpoint(void)
24{
25 const char * const *ptr;
26 char type[100];
27 FILE *fp;
28
29 if (debugfs_found)
30 return (const char *)debugfs_mountpoint;
31
32 ptr = debugfs_known_mountpoints;
33 while (*ptr) {
34 if (debugfs_valid_mountpoint(*ptr) == 0) {
35 debugfs_found = true;
36 strcpy(debugfs_mountpoint, *ptr);
37 return debugfs_mountpoint;
38 }
39 ptr++;
40 }
41
42 /* give up and parse /proc/mounts */
43 fp = fopen("/proc/mounts", "r");
44 if (fp == NULL)
45 return NULL;
46
47 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
48 debugfs_mountpoint, type) == 2) {
49 if (strcmp(type, "debugfs") == 0)
50 break;
51 }
52 fclose(fp);
53
54 if (strcmp(type, "debugfs") != 0)
55 return NULL;
56
57 debugfs_found = true;
58
59 return debugfs_mountpoint;
60}
61
62/* verify that a mountpoint is actually a debugfs instance */
63
64int debugfs_valid_mountpoint(const char *debugfs)
65{
66 struct statfs st_fs;
67
68 if (statfs(debugfs, &st_fs) < 0)
69 return -ENOENT;
70 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
71 return -ENOENT;
72
73 return 0;
74}
75
76/* mount the debugfs somewhere if it's not mounted */
77char *debugfs_mount(const char *mountpoint)
78{
79 /* see if it's already mounted */
80 if (debugfs_find_mountpoint())
81 goto out;
82
83 /* if not mounted and no argument */
84 if (mountpoint == NULL) {
85 /* see if environment variable set */
86 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
87 /* if no environment variable, use default */
88 if (mountpoint == NULL)
89 mountpoint = "/sys/kernel/debug";
90 }
91
92 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
93 return NULL;
94
95 /* save the mountpoint */
96 debugfs_found = true;
97 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
98out:
99 return debugfs_mountpoint;
100}
diff --git a/tools/lib/api/fs/debugfs.h b/tools/lib/api/fs/debugfs.h
new file mode 100644
index 000000000000..f19d3df9609d
--- /dev/null
+++ b/tools/lib/api/fs/debugfs.h
@@ -0,0 +1,29 @@
1#ifndef __API_DEBUGFS_H__
2#define __API_DEBUGFS_H__
3
4#define _STR(x) #x
5#define STR(x) _STR(x)
6
7/*
8 * On most systems <limits.h> would have given us this, but not on some systems
9 * (e.g. GNU/Hurd).
10 */
11#ifndef PATH_MAX
12#define PATH_MAX 4096
13#endif
14
15#ifndef DEBUGFS_MAGIC
16#define DEBUGFS_MAGIC 0x64626720
17#endif
18
19#ifndef PERF_DEBUGFS_ENVIRONMENT
20#define PERF_DEBUGFS_ENVIRONMENT "PERF_DEBUGFS_DIR"
21#endif
22
23const char *debugfs_find_mountpoint(void);
24int debugfs_valid_mountpoint(const char *debugfs);
25char *debugfs_mount(const char *mountpoint);
26
27extern char debugfs_mountpoint[];
28
29#endif /* __API_DEBUGFS_H__ */