Blame view

tools/lib/api/fs/debugfs.c 2.07 KB
85c66be10   Borislav Petkov   perf tools: Intro...
1
2
3
4
5
6
  #include <errno.h>
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <stdbool.h>
  #include <sys/vfs.h>
c168fbfb9   Arnaldo Carvalho de Melo   perf tools: Elimi...
7
  #include <sys/mount.h>
85c66be10   Borislav Petkov   perf tools: Intro...
8
9
10
  #include <linux/kernel.h>
  
  #include "debugfs.h"
c168fbfb9   Arnaldo Carvalho de Melo   perf tools: Elimi...
11

ebf294bf4   Arnaldo Carvalho de Melo   perf tools: Simpl...
12
  char debugfs_mountpoint[PATH_MAX + 1] = "/sys/kernel/debug";
afe61f677   Clark Williams   perf tools: Add d...
13

85c66be10   Borislav Petkov   perf tools: Intro...
14
  static const char * const debugfs_known_mountpoints[] = {
603940b6b   Xia Kaixu   perf tools: Remov...
15
16
  	"/sys/kernel/debug",
  	"/debug",
afe61f677   Clark Williams   perf tools: Add d...
17
18
  	0,
  };
fed120884   Borislav Petkov   perf tools: Remov...
19
  static bool debugfs_found;
afe61f677   Clark Williams   perf tools: Add d...
20
21
22
23
  
  /* find the path to the mounted debugfs */
  const char *debugfs_find_mountpoint(void)
  {
85c66be10   Borislav Petkov   perf tools: Intro...
24
  	const char * const *ptr;
afe61f677   Clark Williams   perf tools: Add d...
25
26
27
28
  	char type[100];
  	FILE *fp;
  
  	if (debugfs_found)
85c66be10   Borislav Petkov   perf tools: Intro...
29
  		return (const char *)debugfs_mountpoint;
afe61f677   Clark Williams   perf tools: Add d...
30
31
32
33
  
  	ptr = debugfs_known_mountpoints;
  	while (*ptr) {
  		if (debugfs_valid_mountpoint(*ptr) == 0) {
fed120884   Borislav Petkov   perf tools: Remov...
34
  			debugfs_found = true;
afe61f677   Clark Williams   perf tools: Add d...
35
36
37
38
39
40
41
42
43
  			strcpy(debugfs_mountpoint, *ptr);
  			return debugfs_mountpoint;
  		}
  		ptr++;
  	}
  
  	/* give up and parse /proc/mounts */
  	fp = fopen("/proc/mounts", "r");
  	if (fp == NULL)
ebf294bf4   Arnaldo Carvalho de Melo   perf tools: Simpl...
44
  		return NULL;
afe61f677   Clark Williams   perf tools: Add d...
45

c168fbfb9   Arnaldo Carvalho de Melo   perf tools: Elimi...
46
47
  	while (fscanf(fp, "%*s %" STR(PATH_MAX) "s %99s %*s %*d %*d
  ",
afe61f677   Clark Williams   perf tools: Add d...
48
49
50
51
52
53
54
55
  		      debugfs_mountpoint, type) == 2) {
  		if (strcmp(type, "debugfs") == 0)
  			break;
  	}
  	fclose(fp);
  
  	if (strcmp(type, "debugfs") != 0)
  		return NULL;
fed120884   Borislav Petkov   perf tools: Remov...
56
  	debugfs_found = true;
afe61f677   Clark Williams   perf tools: Add d...
57
58
59
60
61
62
63
64
65
66
67
68
  
  	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;
db1806edc   Alexey Brodkin   perf tools: Fix s...
69
  	else if ((long)st_fs.f_type != (long)DEBUGFS_MAGIC)
afe61f677   Clark Williams   perf tools: Add d...
70
71
72
73
  		return -ENOENT;
  
  	return 0;
  }
29c52aa23   Xiao Guangrong   perf tools: Mount...
74
  /* mount the debugfs somewhere if it's not mounted */
29c52aa23   Xiao Guangrong   perf tools: Mount...
75
  char *debugfs_mount(const char *mountpoint)
afe61f677   Clark Williams   perf tools: Add d...
76
  {
afe61f677   Clark Williams   perf tools: Add d...
77
  	/* see if it's already mounted */
fed120884   Borislav Petkov   perf tools: Remov...
78
  	if (debugfs_find_mountpoint())
ebf294bf4   Arnaldo Carvalho de Melo   perf tools: Simpl...
79
  		goto out;
afe61f677   Clark Williams   perf tools: Add d...
80
81
82
83
84
85
86
87
88
  
  	/* if not mounted and no argument */
  	if (mountpoint == NULL) {
  		/* see if environment variable set */
  		mountpoint = getenv(PERF_DEBUGFS_ENVIRONMENT);
  		/* if no environment variable, use default */
  		if (mountpoint == NULL)
  			mountpoint = "/sys/kernel/debug";
  	}
29c52aa23   Xiao Guangrong   perf tools: Mount...
89
90
  	if (mount(NULL, mountpoint, "debugfs", 0, NULL) < 0)
  		return NULL;
afe61f677   Clark Williams   perf tools: Add d...
91
  	/* save the mountpoint */
fed120884   Borislav Petkov   perf tools: Remov...
92
  	debugfs_found = true;
ebf294bf4   Arnaldo Carvalho de Melo   perf tools: Simpl...
93
94
  	strncpy(debugfs_mountpoint, mountpoint, sizeof(debugfs_mountpoint));
  out:
29c52aa23   Xiao Guangrong   perf tools: Mount...
95
  	return debugfs_mountpoint;
afe61f677   Clark Williams   perf tools: Add d...
96
  }