xref: /linux/tools/lib/api/fs/tracing_path.c (revision 7014e0e3bf9b0d0b6221eb7d2f8a1f690423dd73)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef _GNU_SOURCE
3 # define _GNU_SOURCE
4 #endif
5 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <linux/string.h>
10 #include <errno.h>
11 #include <unistd.h>
12 #include "fs.h"
13 
14 #include "tracing_path.h"
15 
16 static char tracing_mnt[PATH_MAX]  = "/sys/kernel/debug";
17 static char tracing_path[PATH_MAX]        = "/sys/kernel/debug/tracing";
18 char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events";
19 
20 
21 static void __tracing_path_set(const char *tracing, const char *mountpoint)
22 {
23 	snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint);
24 	snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
25 		 mountpoint, tracing);
26 	snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
27 		 mountpoint, tracing, "events");
28 }
29 
30 static const char *tracing_path_tracefs_mount(void)
31 {
32 	const char *mnt;
33 
34 	mnt = tracefs__mount();
35 	if (!mnt)
36 		return NULL;
37 
38 	__tracing_path_set("", mnt);
39 
40 	return mnt;
41 }
42 
43 static const char *tracing_path_debugfs_mount(void)
44 {
45 	const char *mnt;
46 
47 	mnt = debugfs__mount();
48 	if (!mnt)
49 		return NULL;
50 
51 	__tracing_path_set("tracing/", mnt);
52 
53 	return mnt;
54 }
55 
56 const char *tracing_path_mount(void)
57 {
58 	const char *mnt;
59 
60 	mnt = tracing_path_tracefs_mount();
61 	if (mnt)
62 		return mnt;
63 
64 	mnt = tracing_path_debugfs_mount();
65 
66 	return mnt;
67 }
68 
69 void tracing_path_set(const char *mntpt)
70 {
71 	__tracing_path_set("tracing/", mntpt);
72 }
73 
74 char *get_tracing_file(const char *name)
75 {
76 	char *file;
77 
78 	if (asprintf(&file, "%s/%s", tracing_path_mount(), name) < 0)
79 		return NULL;
80 
81 	return file;
82 }
83 
84 void put_tracing_file(char *file)
85 {
86 	free(file);
87 }
88 
89 char *get_events_file(const char *name)
90 {
91 	char *file;
92 
93 	if (asprintf(&file, "%s/events/%s", tracing_path_mount(), name) < 0)
94 		return NULL;
95 
96 	return file;
97 }
98 
99 void put_events_file(char *file)
100 {
101 	free(file);
102 }
103 
104 DIR *tracing_events__opendir(void)
105 {
106 	DIR *dir = NULL;
107 	char *path = get_tracing_file("events");
108 
109 	if (path) {
110 		dir = opendir(path);
111 		put_events_file(path);
112 	}
113 
114 	return dir;
115 }
116 
117 int tracing_path__strerror_open_tp(int err, char *buf, size_t size,
118 				   const char *sys, const char *name)
119 {
120 	char sbuf[128];
121 	char filename[PATH_MAX];
122 
123 	snprintf(filename, PATH_MAX, "%s/%s", sys, name ?: "*");
124 
125 	switch (err) {
126 	case ENOENT:
127 		/*
128 		 * We will get here if we can't find the tracepoint, but one of
129 		 * debugfs or tracefs is configured, which means you probably
130 		 * want some tracepoint which wasn't compiled in your kernel.
131 		 * - jirka
132 		 */
133 		if (debugfs__configured() || tracefs__configured()) {
134 			/* sdt markers */
135 			if (!strncmp(filename, "sdt_", 4)) {
136 				snprintf(buf, size,
137 					"Error:\tFile %s/%s not found.\n"
138 					"Hint:\tSDT event cannot be directly recorded on.\n"
139 					"\tPlease first use 'perf probe %s:%s' before recording it.\n",
140 					tracing_events_path, filename, sys, name);
141 			} else {
142 				snprintf(buf, size,
143 					 "Error:\tFile %s/%s not found.\n"
144 					 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
145 					 tracing_events_path, filename);
146 			}
147 			break;
148 		}
149 		snprintf(buf, size, "%s",
150 			 "Error:\tUnable to find debugfs/tracefs\n"
151 			 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
152 			 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
153 			 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
154 		break;
155 	case EACCES: {
156 		snprintf(buf, size,
157 			 "Error:\tNo permissions to read %s/%s\n"
158 			 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
159 			 tracing_events_path, filename, tracing_path_mount());
160 	}
161 		break;
162 	default:
163 		snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
164 		break;
165 	}
166 
167 	return 0;
168 }
169