aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--tools/lib/api/Makefile2
-rw-r--r--tools/lib/api/fs/tracefs.c70
-rw-r--r--tools/lib/api/fs/tracefs.h20
3 files changed, 92 insertions, 0 deletions
diff --git a/tools/lib/api/Makefile b/tools/lib/api/Makefile
index 22b2f15d7255..212aa4fd65a0 100644
--- a/tools/lib/api/Makefile
+++ b/tools/lib/api/Makefile
@@ -9,12 +9,14 @@ LIB_H=
9LIB_OBJS= 9LIB_OBJS=
10 10
11LIB_H += fs/debugfs.h 11LIB_H += fs/debugfs.h
12LIB_H += fs/tracefs.h
12LIB_H += fs/findfs.h 13LIB_H += fs/findfs.h
13LIB_H += fs/fs.h 14LIB_H += fs/fs.h
14# See comment below about piggybacking... 15# See comment below about piggybacking...
15LIB_H += fd/array.h 16LIB_H += fd/array.h
16 17
17LIB_OBJS += $(OUTPUT)fs/debugfs.o 18LIB_OBJS += $(OUTPUT)fs/debugfs.o
19LIB_OBJS += $(OUTPUT)fs/tracefs.o
18LIB_OBJS += $(OUTPUT)fs/findfs.o 20LIB_OBJS += $(OUTPUT)fs/findfs.o
19LIB_OBJS += $(OUTPUT)fs/fs.o 21LIB_OBJS += $(OUTPUT)fs/fs.o
20# XXX piggybacking here, need to introduce libapikfd, or rename this 22# XXX piggybacking here, need to introduce libapikfd, or rename this
diff --git a/tools/lib/api/fs/tracefs.c b/tools/lib/api/fs/tracefs.c
new file mode 100644
index 000000000000..ef40d15821e9
--- /dev/null
+++ b/tools/lib/api/fs/tracefs.c
@@ -0,0 +1,70 @@
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 "tracefs.h"
11
12#ifndef TRACEFS_DEFAULT_PATH
13#define TRACEFS_DEFAULT_PATH "/sys/kernel/tracing"
14#endif
15
16char tracefs_mountpoint[PATH_MAX + 1] = TRACEFS_DEFAULT_PATH;
17
18static const char * const tracefs_known_mountpoints[] = {
19 TRACEFS_DEFAULT_PATH,
20 "/sys/kernel/debug/tracing",
21 "/tracing",
22 "/trace",
23 0,
24};
25
26static bool tracefs_found;
27
28/* find the path to the mounted tracefs */
29const char *tracefs_find_mountpoint(void)
30{
31 const char *ret;
32
33 if (tracefs_found)
34 return (const char *)tracefs_mountpoint;
35
36 ret = find_mountpoint("tracefs", (long) TRACEFS_MAGIC,
37 tracefs_mountpoint, PATH_MAX + 1,
38 tracefs_known_mountpoints);
39
40 if (ret)
41 tracefs_found = true;
42
43 return ret;
44}
45
46/* mount the tracefs somewhere if it's not mounted */
47char *tracefs_mount(const char *mountpoint)
48{
49 /* see if it's already mounted */
50 if (tracefs_find_mountpoint())
51 goto out;
52
53 /* if not mounted and no argument */
54 if (mountpoint == NULL) {
55 /* see if environment variable set */
56 mountpoint = getenv(PERF_TRACEFS_ENVIRONMENT);
57 /* if no environment variable, use default */
58 if (mountpoint == NULL)
59 mountpoint = TRACEFS_DEFAULT_PATH;
60 }
61
62 if (mount(NULL, mountpoint, "tracefs", 0, NULL) < 0)
63 return NULL;
64
65 /* save the mountpoint */
66 tracefs_found = true;
67 strncpy(tracefs_mountpoint, mountpoint, sizeof(tracefs_mountpoint));
68out:
69 return tracefs_mountpoint;
70}
diff --git a/tools/lib/api/fs/tracefs.h b/tools/lib/api/fs/tracefs.h
new file mode 100644
index 000000000000..e6f7f5183c87
--- /dev/null
+++ b/tools/lib/api/fs/tracefs.h
@@ -0,0 +1,20 @@
1#ifndef __API_TRACEFS_H__
2#define __API_TRACEFS_H__
3
4#include "findfs.h"
5
6#ifndef TRACEFS_MAGIC
7#define TRACEFS_MAGIC 0x74726163
8#endif
9
10#ifndef PERF_TRACEFS_ENVIRONMENT
11#define PERF_TRACEFS_ENVIRONMENT "PERF_TRACEFS_DIR"
12#endif
13
14const char *tracefs_find_mountpoint(void);
15int tracefs_valid_mountpoint(const char *debugfs);
16char *tracefs_mount(const char *mountpoint);
17
18extern char tracefs_mountpoint[];
19
20#endif /* __API_DEBUGFS_H__ */