blob: 1e0bb0da5e4f2b4eb1a5345ba27ca50d91846cdf [file] [log] [blame]
Jiri Olsa592d5a62015-09-02 09:56:34 +02001#ifndef _GNU_SOURCE
2# define _GNU_SOURCE
3#endif
4
5#include <stdio.h>
6#include <stdlib.h>
7#include <string.h>
Jiri Olsa988bdb32015-09-02 09:56:35 +02008#include <errno.h>
9#include <unistd.h>
Jiri Olsa592d5a62015-09-02 09:56:34 +020010#include "debugfs.h"
11#include "tracefs.h"
12
13#include "tracing_path.h"
14
15
16char tracing_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing";
17char tracing_events_path[PATH_MAX + 1] = "/sys/kernel/debug/tracing/events";
18
19
20static void __tracing_path_set(const char *tracing, const char *mountpoint)
21{
22 snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
23 mountpoint, tracing);
24 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
25 mountpoint, tracing, "events");
26}
27
28static const char *tracing_path_tracefs_mount(void)
29{
30 const char *mnt;
31
32 mnt = tracefs_mount(NULL);
33 if (!mnt)
34 return NULL;
35
36 __tracing_path_set("", mnt);
37
38 return mnt;
39}
40
41static const char *tracing_path_debugfs_mount(void)
42{
43 const char *mnt;
44
45 mnt = debugfs_mount(NULL);
46 if (!mnt)
47 return NULL;
48
49 __tracing_path_set("tracing/", mnt);
50
51 return mnt;
52}
53
54const char *tracing_path_mount(void)
55{
56 const char *mnt;
57
58 mnt = tracing_path_tracefs_mount();
59 if (mnt)
60 return mnt;
61
62 mnt = tracing_path_debugfs_mount();
63
64 return mnt;
65}
66
67void tracing_path_set(const char *mntpt)
68{
69 __tracing_path_set("tracing/", mntpt);
70}
71
72char *get_tracing_file(const char *name)
73{
74 char *file;
75
76 if (asprintf(&file, "%s/%s", tracing_path, name) < 0)
77 return NULL;
78
79 return file;
80}
81
82void put_tracing_file(char *file)
83{
84 free(file);
85}
Jiri Olsa988bdb32015-09-02 09:56:35 +020086
87static int strerror_open(int err, char *buf, size_t size, const char *filename)
88{
89 char sbuf[128];
90
91 switch (err) {
92 case ENOENT:
Jiri Olsa4f234f02015-09-02 09:56:36 +020093 /*
94 * We will get here if we can't find the tracepoint, but one of
95 * debugfs or tracefs is configured, which means you probably
96 * want some tracepoint which wasn't compiled in your kernel.
97 * - jirka
98 */
99 if (debugfs_configured() || tracefs_configured()) {
Jiri Olsa988bdb32015-09-02 09:56:35 +0200100 snprintf(buf, size,
101 "Error:\tFile %s/%s not found.\n"
102 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
Jiri Olsa4f234f02015-09-02 09:56:36 +0200103 tracing_events_path, filename);
Jiri Olsa988bdb32015-09-02 09:56:35 +0200104 break;
105 }
106 snprintf(buf, size, "%s",
Jiri Olsa4f234f02015-09-02 09:56:36 +0200107 "Error:\tUnable to find debugfs/tracefs\n"
108 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
109 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
Jiri Olsa988bdb32015-09-02 09:56:35 +0200110 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
111 break;
112 case EACCES: {
Jiri Olsa4f234f02015-09-02 09:56:36 +0200113 const char *mountpoint = debugfs_find_mountpoint();
Jiri Olsa988bdb32015-09-02 09:56:35 +0200114
Jiri Olsa4f234f02015-09-02 09:56:36 +0200115 if (!access(mountpoint, R_OK) && strncmp(filename, "tracing/", 8) == 0) {
Jiri Olsa988bdb32015-09-02 09:56:35 +0200116 const char *tracefs_mntpoint = tracefs_find_mountpoint();
117
118 if (tracefs_mntpoint)
Jiri Olsa4f234f02015-09-02 09:56:36 +0200119 mountpoint = tracefs_find_mountpoint();
Jiri Olsa988bdb32015-09-02 09:56:35 +0200120 }
121
122 snprintf(buf, size,
123 "Error:\tNo permissions to read %s/%s\n"
124 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
Jiri Olsa4f234f02015-09-02 09:56:36 +0200125 tracing_events_path, filename, mountpoint);
Jiri Olsa988bdb32015-09-02 09:56:35 +0200126 }
127 break;
128 default:
129 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
130 break;
131 }
132
133 return 0;
134}
135
136int tracing_path__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
137{
138 char path[PATH_MAX];
139
Jiri Olsa4f234f02015-09-02 09:56:36 +0200140 snprintf(path, PATH_MAX, "%s/%s", sys, name ?: "*");
Jiri Olsa988bdb32015-09-02 09:56:35 +0200141
142 return strerror_open(err, buf, size, path);
143}