Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / tools / lib / api / fs / tracing_path.c
blob5afb11b30fca4e318f88aaf9e7e28b0c2d4107b8
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef _GNU_SOURCE
3 # define _GNU_SOURCE
4 #endif
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"
14 #include "tracing_path.h"
16 static char tracing_mnt[PATH_MAX] = "/sys/kernel/debug";
17 static char tracing_path[PATH_MAX] = "/sys/kernel/debug/tracing";
18 static char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events";
20 static void __tracing_path_set(const char *tracing, const char *mountpoint)
22 snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint);
23 snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
24 mountpoint, tracing);
25 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
26 mountpoint, tracing, "events");
29 static const char *tracing_path_tracefs_mount(void)
31 const char *mnt;
33 mnt = tracefs__mount();
34 if (!mnt)
35 return NULL;
37 __tracing_path_set("", mnt);
39 return tracing_path;
42 static const char *tracing_path_debugfs_mount(void)
44 const char *mnt;
46 mnt = debugfs__mount();
47 if (!mnt)
48 return NULL;
50 __tracing_path_set("tracing/", mnt);
52 return tracing_path;
55 const char *tracing_path_mount(void)
57 const char *mnt;
59 mnt = tracing_path_tracefs_mount();
60 if (mnt)
61 return mnt;
63 mnt = tracing_path_debugfs_mount();
65 return mnt;
68 void tracing_path_set(const char *mntpt)
70 __tracing_path_set("tracing/", mntpt);
73 char *get_tracing_file(const char *name)
75 char *file;
77 if (asprintf(&file, "%s/%s", tracing_path_mount(), name) < 0)
78 return NULL;
80 return file;
83 void put_tracing_file(char *file)
85 free(file);
88 char *get_events_file(const char *name)
90 char *file;
92 if (asprintf(&file, "%s/events/%s", tracing_path_mount(), name) < 0)
93 return NULL;
95 return file;
98 void put_events_file(char *file)
100 free(file);
103 DIR *tracing_events__opendir(void)
105 DIR *dir = NULL;
106 char *path = get_tracing_file("events");
108 if (path) {
109 dir = opendir(path);
110 put_events_file(path);
113 return dir;
116 int tracing_path__strerror_open_tp(int err, char *buf, size_t size,
117 const char *sys, const char *name)
119 char sbuf[128];
120 char filename[PATH_MAX];
122 snprintf(filename, PATH_MAX, "%s/%s", sys, name ?: "*");
124 switch (err) {
125 case ENOENT:
127 * We will get here if we can't find the tracepoint, but one of
128 * debugfs or tracefs is configured, which means you probably
129 * want some tracepoint which wasn't compiled in your kernel.
130 * - jirka
132 if (debugfs__configured() || tracefs__configured()) {
133 /* sdt markers */
134 if (!strncmp(filename, "sdt_", 4)) {
135 snprintf(buf, size,
136 "Error:\tFile %s/%s not found.\n"
137 "Hint:\tSDT event cannot be directly recorded on.\n"
138 "\tPlease first use 'perf probe %s:%s' before recording it.\n",
139 tracing_events_path, filename, sys, name);
140 } else {
141 snprintf(buf, size,
142 "Error:\tFile %s/%s not found.\n"
143 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
144 tracing_events_path, filename);
146 break;
148 snprintf(buf, size, "%s",
149 "Error:\tUnable to find debugfs/tracefs\n"
150 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
151 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
152 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
153 break;
154 case EACCES: {
155 snprintf(buf, size,
156 "Error:\tNo permissions to read %s/%s\n"
157 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
158 tracing_events_path, filename, tracing_path_mount());
160 break;
161 default:
162 snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
163 break;
166 return 0;