xref: /openbmc/linux/tools/lib/api/fs/tracing_path.c (revision d236d361)
1 #ifndef _GNU_SOURCE
2 # define _GNU_SOURCE
3 #endif
4 
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8 #include <linux/string.h>
9 #include <errno.h>
10 #include <unistd.h>
11 #include "fs.h"
12 
13 #include "tracing_path.h"
14 
15 
16 char tracing_mnt[PATH_MAX]         = "/sys/kernel/debug";
17 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, 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 int tracing_path__strerror_open_tp(int err, char *buf, size_t size,
90 				   const char *sys, const char *name)
91 {
92 	char sbuf[128];
93 	char filename[PATH_MAX];
94 
95 	snprintf(filename, PATH_MAX, "%s/%s", sys, name ?: "*");
96 
97 	switch (err) {
98 	case ENOENT:
99 		/*
100 		 * We will get here if we can't find the tracepoint, but one of
101 		 * debugfs or tracefs is configured, which means you probably
102 		 * want some tracepoint which wasn't compiled in your kernel.
103 		 * - jirka
104 		 */
105 		if (debugfs__configured() || tracefs__configured()) {
106 			/* sdt markers */
107 			if (!strncmp(filename, "sdt_", 4)) {
108 				snprintf(buf, size,
109 					"Error:\tFile %s/%s not found.\n"
110 					"Hint:\tSDT event cannot be directly recorded on.\n"
111 					"\tPlease first use 'perf probe %s:%s' before recording it.\n",
112 					tracing_events_path, filename, sys, name);
113 			} else {
114 				snprintf(buf, size,
115 					 "Error:\tFile %s/%s not found.\n"
116 					 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
117 					 tracing_events_path, filename);
118 			}
119 			break;
120 		}
121 		snprintf(buf, size, "%s",
122 			 "Error:\tUnable to find debugfs/tracefs\n"
123 			 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
124 			 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
125 			 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
126 		break;
127 	case EACCES: {
128 		snprintf(buf, size,
129 			 "Error:\tNo permissions to read %s/%s\n"
130 			 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
131 			 tracing_events_path, filename, tracing_mnt);
132 	}
133 		break;
134 	default:
135 		snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
136 		break;
137 	}
138 
139 	return 0;
140 }
141