blob: a26bb5ea828307358803bc369bbb3c200d266b3d [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 Olsa4605eab2015-09-02 09:56:43 +020010#include "fs.h"
Jiri Olsa592d5a62015-09-02 09:56:34 +020011
12#include "tracing_path.h"
13
14
Jiri Olsaccb55972015-10-05 20:06:01 +020015char tracing_mnt[PATH_MAX] = "/sys/kernel/debug";
16char tracing_path[PATH_MAX] = "/sys/kernel/debug/tracing";
17char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events";
Jiri Olsa592d5a62015-09-02 09:56:34 +020018
19
20static void __tracing_path_set(const char *tracing, const char *mountpoint)
21{
Jiri Olsadc240c52015-09-19 16:47:07 +020022 snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint);
Jiri Olsa592d5a62015-09-02 09:56:34 +020023 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");
27}
28
29static const char *tracing_path_tracefs_mount(void)
30{
31 const char *mnt;
32
Jiri Olsa4605eab2015-09-02 09:56:43 +020033 mnt = tracefs__mount();
Jiri Olsa592d5a62015-09-02 09:56:34 +020034 if (!mnt)
35 return NULL;
36
37 __tracing_path_set("", mnt);
38
39 return mnt;
40}
41
42static const char *tracing_path_debugfs_mount(void)
43{
44 const char *mnt;
45
Jiri Olsa4605eab2015-09-02 09:56:43 +020046 mnt = debugfs__mount();
Jiri Olsa592d5a62015-09-02 09:56:34 +020047 if (!mnt)
48 return NULL;
49
50 __tracing_path_set("tracing/", mnt);
51
52 return mnt;
53}
54
55const char *tracing_path_mount(void)
56{
57 const char *mnt;
58
59 mnt = tracing_path_tracefs_mount();
60 if (mnt)
61 return mnt;
62
63 mnt = tracing_path_debugfs_mount();
64
65 return mnt;
66}
67
68void tracing_path_set(const char *mntpt)
69{
70 __tracing_path_set("tracing/", mntpt);
71}
72
73char *get_tracing_file(const char *name)
74{
75 char *file;
76
77 if (asprintf(&file, "%s/%s", tracing_path, name) < 0)
78 return NULL;
79
80 return file;
81}
82
83void put_tracing_file(char *file)
84{
85 free(file);
86}
Jiri Olsa988bdb32015-09-02 09:56:35 +020087
88static int strerror_open(int err, char *buf, size_t size, const char *filename)
89{
90 char sbuf[128];
91
92 switch (err) {
93 case ENOENT:
Jiri Olsa4f234f02015-09-02 09:56:36 +020094 /*
95 * We will get here if we can't find the tracepoint, but one of
96 * debugfs or tracefs is configured, which means you probably
97 * want some tracepoint which wasn't compiled in your kernel.
98 * - jirka
99 */
Jiri Olsa4605eab2015-09-02 09:56:43 +0200100 if (debugfs__configured() || tracefs__configured()) {
Jiri Olsa988bdb32015-09-02 09:56:35 +0200101 snprintf(buf, size,
102 "Error:\tFile %s/%s not found.\n"
103 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
Jiri Olsa4f234f02015-09-02 09:56:36 +0200104 tracing_events_path, filename);
Jiri Olsa988bdb32015-09-02 09:56:35 +0200105 break;
106 }
107 snprintf(buf, size, "%s",
Jiri Olsa4f234f02015-09-02 09:56:36 +0200108 "Error:\tUnable to find debugfs/tracefs\n"
109 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
110 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
Jiri Olsa988bdb32015-09-02 09:56:35 +0200111 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
112 break;
113 case EACCES: {
Jiri Olsa988bdb32015-09-02 09:56:35 +0200114 snprintf(buf, size,
115 "Error:\tNo permissions to read %s/%s\n"
116 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
Jiri Olsadc240c52015-09-19 16:47:07 +0200117 tracing_events_path, filename, tracing_mnt);
Jiri Olsa988bdb32015-09-02 09:56:35 +0200118 }
119 break;
120 default:
121 snprintf(buf, size, "%s", strerror_r(err, sbuf, sizeof(sbuf)));
122 break;
123 }
124
125 return 0;
126}
127
128int tracing_path__strerror_open_tp(int err, char *buf, size_t size, const char *sys, const char *name)
129{
130 char path[PATH_MAX];
131
Jiri Olsa4f234f02015-09-02 09:56:36 +0200132 snprintf(path, PATH_MAX, "%s/%s", sys, name ?: "*");
Jiri Olsa988bdb32015-09-02 09:56:35 +0200133
134 return strerror_open(err, buf, size, path);
135}