Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / tools / lib / api / fs / tracing_path.c
blob7b7fd0b1855105bac16583a19a0474d26d3c3234
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"
17 char tracing_mnt[PATH_MAX] = "/sys/kernel/debug";
18 char tracing_path[PATH_MAX] = "/sys/kernel/debug/tracing";
19 char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events";
22 static void __tracing_path_set(const char *tracing, const char *mountpoint)
24 snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint);
25 snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
26 mountpoint, tracing);
27 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
28 mountpoint, tracing, "events");
31 static const char *tracing_path_tracefs_mount(void)
33 const char *mnt;
35 mnt = tracefs__mount();
36 if (!mnt)
37 return NULL;
39 __tracing_path_set("", mnt);
41 return mnt;
44 static const char *tracing_path_debugfs_mount(void)
46 const char *mnt;
48 mnt = debugfs__mount();
49 if (!mnt)
50 return NULL;
52 __tracing_path_set("tracing/", mnt);
54 return mnt;
57 const char *tracing_path_mount(void)
59 const char *mnt;
61 mnt = tracing_path_tracefs_mount();
62 if (mnt)
63 return mnt;
65 mnt = tracing_path_debugfs_mount();
67 return mnt;
70 void tracing_path_set(const char *mntpt)
72 __tracing_path_set("tracing/", mntpt);
75 char *get_tracing_file(const char *name)
77 char *file;
79 if (asprintf(&file, "%s/%s", tracing_path, name) < 0)
80 return NULL;
82 return file;
85 void put_tracing_file(char *file)
87 free(file);
90 int tracing_path__strerror_open_tp(int err, char *buf, size_t size,
91 const char *sys, const char *name)
93 char sbuf[128];
94 char filename[PATH_MAX];
96 snprintf(filename, PATH_MAX, "%s/%s", sys, name ?: "*");
98 switch (err) {
99 case ENOENT:
101 * We will get here if we can't find the tracepoint, but one of
102 * debugfs or tracefs is configured, which means you probably
103 * want some tracepoint which wasn't compiled in your kernel.
104 * - jirka
106 if (debugfs__configured() || tracefs__configured()) {
107 /* sdt markers */
108 if (!strncmp(filename, "sdt_", 4)) {
109 snprintf(buf, size,
110 "Error:\tFile %s/%s not found.\n"
111 "Hint:\tSDT event cannot be directly recorded on.\n"
112 "\tPlease first use 'perf probe %s:%s' before recording it.\n",
113 tracing_events_path, filename, sys, name);
114 } else {
115 snprintf(buf, size,
116 "Error:\tFile %s/%s not found.\n"
117 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
118 tracing_events_path, filename);
120 break;
122 snprintf(buf, size, "%s",
123 "Error:\tUnable to find debugfs/tracefs\n"
124 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
125 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
126 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
127 break;
128 case EACCES: {
129 snprintf(buf, size,
130 "Error:\tNo permissions to read %s/%s\n"
131 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
132 tracing_events_path, filename, tracing_mnt);
134 break;
135 default:
136 snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
137 break;
140 return 0;