blob: 9cda7a6f591746a67a251f2331d905b5f4a5be79 [file] [log] [blame]
Borislav Petkov85c66be2013-02-20 16:32:30 +01001#include <errno.h>
2#include <stdio.h>
3#include <stdlib.h>
4#include <string.h>
5#include <stdbool.h>
6#include <sys/vfs.h>
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -02007#include <sys/mount.h>
Borislav Petkov85c66be2013-02-20 16:32:30 +01008#include <linux/magic.h>
9#include <linux/kernel.h>
10
11#include "debugfs.h"
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020012
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020013char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
14char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
Clark Williamsafe61f62009-11-08 09:01:37 -060015
Borislav Petkov85c66be2013-02-20 16:32:30 +010016static const char * const debugfs_known_mountpoints[] = {
Clark Williamsafe61f62009-11-08 09:01:37 -060017 "/sys/kernel/debug/",
18 "/debug/",
19 0,
20};
21
Borislav Petkovfed12082013-02-20 16:32:27 +010022static bool debugfs_found;
Clark Williamsafe61f62009-11-08 09:01:37 -060023
24/* find the path to the mounted debugfs */
25const char *debugfs_find_mountpoint(void)
26{
Borislav Petkov85c66be2013-02-20 16:32:30 +010027 const char * const *ptr;
Clark Williamsafe61f62009-11-08 09:01:37 -060028 char type[100];
29 FILE *fp;
30
31 if (debugfs_found)
Borislav Petkov85c66be2013-02-20 16:32:30 +010032 return (const char *)debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -060033
34 ptr = debugfs_known_mountpoints;
35 while (*ptr) {
36 if (debugfs_valid_mountpoint(*ptr) == 0) {
Borislav Petkovfed12082013-02-20 16:32:27 +010037 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060038 strcpy(debugfs_mountpoint, *ptr);
39 return debugfs_mountpoint;
40 }
41 ptr++;
42 }
43
44 /* give up and parse /proc/mounts */
45 fp = fopen("/proc/mounts", "r");
46 if (fp == NULL)
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020047 return NULL;
Clark Williamsafe61f62009-11-08 09:01:37 -060048
Arnaldo Carvalho de Meloc168fbf2011-11-16 12:55:59 -020049 while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d\n",
Clark Williamsafe61f62009-11-08 09:01:37 -060050 debugfs_mountpoint, type) == 2) {
51 if (strcmp(type, "debugfs") == 0)
52 break;
53 }
54 fclose(fp);
55
56 if (strcmp(type, "debugfs") != 0)
57 return NULL;
58
Borislav Petkovfed12082013-02-20 16:32:27 +010059 debugfs_found = true;
Clark Williamsafe61f62009-11-08 09:01:37 -060060
61 return debugfs_mountpoint;
62}
63
64/* verify that a mountpoint is actually a debugfs instance */
65
66int debugfs_valid_mountpoint(const char *debugfs)
67{
68 struct statfs st_fs;
69
70 if (statfs(debugfs, &st_fs) < 0)
71 return -ENOENT;
72 else if (st_fs.f_type != (long) DEBUGFS_MAGIC)
73 return -ENOENT;
74
75 return 0;
76}
77
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020078static void debugfs_set_tracing_events_path(const char *mountpoint)
79{
80 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s",
81 mountpoint, "tracing/events");
82}
83
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080084/* mount the debugfs somewhere if it's not mounted */
Clark Williamsafe61f62009-11-08 09:01:37 -060085
Xiao Guangrong29c52aa2009-12-28 16:47:12 +080086char *debugfs_mount(const char *mountpoint)
Clark Williamsafe61f62009-11-08 09:01:37 -060087{
Clark Williamsafe61f62009-11-08 09:01:37 -060088 /* see if it's already mounted */
Borislav Petkovfed12082013-02-20 16:32:27 +010089 if (debugfs_find_mountpoint())
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -020090 goto out;
Clark Williamsafe61f62009-11-08 09:01:37 -060091
92 /* if not mounted and no argument */
93 if (mountpoint == NULL) {
94 /* see if environment variable set */
95 mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
96 /* if no environment variable, use default */
97 if (mountpoint == NULL)
98 mountpoint = "/sys/kernel/debug";
99 }
100
Xiao Guangrong29c52aa2009-12-28 16:47:12 +0800101 if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
102 return NULL;
103
Clark Williamsafe61f62009-11-08 09:01:37 -0600104 /* save the mountpoint */
Borislav Petkovfed12082013-02-20 16:32:27 +0100105 debugfs_found = true;
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200106 strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
107out:
108 debugfs_set_tracing_events_path(debugfs_mountpoint);
Xiao Guangrong29c52aa2009-12-28 16:47:12 +0800109 return debugfs_mountpoint;
Clark Williamsafe61f62009-11-08 09:01:37 -0600110}
111
Arnaldo Carvalho de Meloebf294b2011-11-16 14:03:07 -0200112void debugfs_set_path(const char *mountpoint)
113{
114 snprintf(debugfs_mountpoint, sizeof(debugfs_mountpoint), "%s", mountpoint);
115 debugfs_set_tracing_events_path(mountpoint);
116}