aboutsummaryrefslogtreecommitdiffstats
path: root/tools/lib/api/fs/debugfs.c
diff options
context:
space:
mode:
authorBorislav Petkov <bp@suse.de>2013-12-09 11:14:23 -0500
committerArnaldo Carvalho de Melo <acme@redhat.com>2013-12-16 14:03:27 -0500
commit553873e1df63a20559ac9c336765dc7055cfc3d4 (patch)
treecdae36b2cbe9818bea7e6987a9e73baf9d79fd53 /tools/lib/api/fs/debugfs.c
parentb283d2f3b74bc98174e8453c0be41dfcda3cae1b (diff)
tools/: Convert to new topic libraries
Move debugfs.* to api/fs/. We have a common tools/lib/api/ place where the Makefile lives and then we place the headers in subdirs. For example, all the fs-related stuff goes to tools/lib/api/fs/ from which we get libapikfs.a (acme got almost the naming he wanted :-)) and we link it into the tools which need it - in this case perf and tools/vm/page-types. acme: "Looking at the implementation, I think some tools can even link directly to the .o files, avoiding the .a file altogether. But that is just an optimization/finer granularity tools/lib/ cherrypicking that toolers can make use of." Fixup documentation cleaning target while at it. Signed-off-by: Borislav Petkov <bp@suse.de> Acked-by: Ingo Molnar <mingo@kernel.org> Cc: Adrian Hunter <adrian.hunter@intel.com> Cc: Andi Kleen <ak@linux.intel.com> Cc: Arjan van de Ven <arjan@linux.intel.com> Cc: David Ahern <dsahern@gmail.com> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ingo Molnar <mingo@kernel.org> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Mike Galbraith <efault@gmx.de> Cc: Namhyung Kim <namhyung@gmail.com> Cc: Paul Mackerras <paulus@samba.org> Cc: Pekka Enberg <penberg@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Robert Richter <rric@kernel.org> Cc: Stanislav Fomichev <stfomichev@yandex-team.ru> Cc: Stephane Eranian <eranian@google.com> Cc: Steven Rostedt <rostedt@goodmis.org> Link: http://lkml.kernel.org/r/1386605664-24041-2-git-send-email-bp@alien8.de Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Diffstat (limited to 'tools/lib/api/fs/debugfs.c')
-rw-r--r--tools/lib/api/fs/debugfs.c100
1 files changed, 100 insertions, 0 deletions
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}