0001
0002 #ifndef _GNU_SOURCE
0003 # define _GNU_SOURCE
0004 #endif
0005
0006 #include <stdio.h>
0007 #include <stdlib.h>
0008 #include <string.h>
0009 #include <linux/string.h>
0010 #include <errno.h>
0011 #include <unistd.h>
0012 #include "fs.h"
0013
0014 #include "tracing_path.h"
0015
0016 static char tracing_mnt[PATH_MAX] = "/sys/kernel/debug";
0017 static char tracing_path[PATH_MAX] = "/sys/kernel/debug/tracing";
0018 static char tracing_events_path[PATH_MAX] = "/sys/kernel/debug/tracing/events";
0019
0020 static void __tracing_path_set(const char *tracing, const char *mountpoint)
0021 {
0022 snprintf(tracing_mnt, sizeof(tracing_mnt), "%s", mountpoint);
0023 snprintf(tracing_path, sizeof(tracing_path), "%s/%s",
0024 mountpoint, tracing);
0025 snprintf(tracing_events_path, sizeof(tracing_events_path), "%s/%s%s",
0026 mountpoint, tracing, "events");
0027 }
0028
0029 static const char *tracing_path_tracefs_mount(void)
0030 {
0031 const char *mnt;
0032
0033 mnt = tracefs__mount();
0034 if (!mnt)
0035 return NULL;
0036
0037 __tracing_path_set("", mnt);
0038
0039 return tracing_path;
0040 }
0041
0042 static const char *tracing_path_debugfs_mount(void)
0043 {
0044 const char *mnt;
0045
0046 mnt = debugfs__mount();
0047 if (!mnt)
0048 return NULL;
0049
0050 __tracing_path_set("tracing/", mnt);
0051
0052 return tracing_path;
0053 }
0054
0055 const char *tracing_path_mount(void)
0056 {
0057 const char *mnt;
0058
0059 mnt = tracing_path_tracefs_mount();
0060 if (mnt)
0061 return mnt;
0062
0063 mnt = tracing_path_debugfs_mount();
0064
0065 return mnt;
0066 }
0067
0068 void tracing_path_set(const char *mntpt)
0069 {
0070 __tracing_path_set("tracing/", mntpt);
0071 }
0072
0073 char *get_tracing_file(const char *name)
0074 {
0075 char *file;
0076
0077 if (asprintf(&file, "%s/%s", tracing_path_mount(), name) < 0)
0078 return NULL;
0079
0080 return file;
0081 }
0082
0083 void put_tracing_file(char *file)
0084 {
0085 free(file);
0086 }
0087
0088 char *get_events_file(const char *name)
0089 {
0090 char *file;
0091
0092 if (asprintf(&file, "%s/events/%s", tracing_path_mount(), name) < 0)
0093 return NULL;
0094
0095 return file;
0096 }
0097
0098 void put_events_file(char *file)
0099 {
0100 free(file);
0101 }
0102
0103 DIR *tracing_events__opendir(void)
0104 {
0105 DIR *dir = NULL;
0106 char *path = get_tracing_file("events");
0107
0108 if (path) {
0109 dir = opendir(path);
0110 put_events_file(path);
0111 }
0112
0113 return dir;
0114 }
0115
0116 int tracing_path__strerror_open_tp(int err, char *buf, size_t size,
0117 const char *sys, const char *name)
0118 {
0119 char sbuf[128];
0120 char filename[PATH_MAX];
0121
0122 snprintf(filename, PATH_MAX, "%s/%s", sys, name ?: "*");
0123
0124 switch (err) {
0125 case ENOENT:
0126
0127
0128
0129
0130
0131
0132 if (debugfs__configured() || tracefs__configured()) {
0133
0134 if (!strncmp(filename, "sdt_", 4)) {
0135 snprintf(buf, size,
0136 "Error:\tFile %s/%s not found.\n"
0137 "Hint:\tSDT event cannot be directly recorded on.\n"
0138 "\tPlease first use 'perf probe %s:%s' before recording it.\n",
0139 tracing_events_path, filename, sys, name);
0140 } else {
0141 snprintf(buf, size,
0142 "Error:\tFile %s/%s not found.\n"
0143 "Hint:\tPerhaps this kernel misses some CONFIG_ setting to enable this feature?.\n",
0144 tracing_events_path, filename);
0145 }
0146 break;
0147 }
0148 snprintf(buf, size, "%s",
0149 "Error:\tUnable to find debugfs/tracefs\n"
0150 "Hint:\tWas your kernel compiled with debugfs/tracefs support?\n"
0151 "Hint:\tIs the debugfs/tracefs filesystem mounted?\n"
0152 "Hint:\tTry 'sudo mount -t debugfs nodev /sys/kernel/debug'");
0153 break;
0154 case EACCES: {
0155 snprintf(buf, size,
0156 "Error:\tNo permissions to read %s/%s\n"
0157 "Hint:\tTry 'sudo mount -o remount,mode=755 %s'\n",
0158 tracing_events_path, filename, tracing_path_mount());
0159 }
0160 break;
0161 default:
0162 snprintf(buf, size, "%s", str_error_r(err, sbuf, sizeof(sbuf)));
0163 break;
0164 }
0165
0166 return 0;
0167 }