mm: rename alloc_pages_exact_node() to __alloc_pages_node()
[linux/fpc-iii.git] / tools / perf / util / probe-file.c
blobbbb243717ec834f0548e9d5bf8a3ac0ff48f8c4b
1 /*
2 * probe-file.c : operate ftrace k/uprobe events files
4 * Written by Masami Hiramatsu <masami.hiramatsu.pt@hitachi.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
17 #include "util.h"
18 #include "event.h"
19 #include "strlist.h"
20 #include "debug.h"
21 #include "cache.h"
22 #include "color.h"
23 #include "symbol.h"
24 #include "thread.h"
25 #include <api/fs/debugfs.h>
26 #include <api/fs/tracefs.h>
27 #include "probe-event.h"
28 #include "probe-file.h"
29 #include "session.h"
31 #define MAX_CMDLEN 256
33 static void print_open_warning(int err, bool uprobe)
35 char sbuf[STRERR_BUFSIZE];
37 if (err == -ENOENT) {
38 const char *config;
40 if (uprobe)
41 config = "CONFIG_UPROBE_EVENTS";
42 else
43 config = "CONFIG_KPROBE_EVENTS";
45 pr_warning("%cprobe_events file does not exist"
46 " - please rebuild kernel with %s.\n",
47 uprobe ? 'u' : 'k', config);
48 } else if (err == -ENOTSUP)
49 pr_warning("Tracefs or debugfs is not mounted.\n");
50 else
51 pr_warning("Failed to open %cprobe_events: %s\n",
52 uprobe ? 'u' : 'k',
53 strerror_r(-err, sbuf, sizeof(sbuf)));
56 static void print_both_open_warning(int kerr, int uerr)
58 /* Both kprobes and uprobes are disabled, warn it. */
59 if (kerr == -ENOTSUP && uerr == -ENOTSUP)
60 pr_warning("Tracefs or debugfs is not mounted.\n");
61 else if (kerr == -ENOENT && uerr == -ENOENT)
62 pr_warning("Please rebuild kernel with CONFIG_KPROBE_EVENTS "
63 "or/and CONFIG_UPROBE_EVENTS.\n");
64 else {
65 char sbuf[STRERR_BUFSIZE];
66 pr_warning("Failed to open kprobe events: %s.\n",
67 strerror_r(-kerr, sbuf, sizeof(sbuf)));
68 pr_warning("Failed to open uprobe events: %s.\n",
69 strerror_r(-uerr, sbuf, sizeof(sbuf)));
73 static int open_probe_events(const char *trace_file, bool readwrite)
75 char buf[PATH_MAX];
76 const char *__debugfs;
77 const char *tracing_dir = "";
78 int ret;
80 __debugfs = tracefs_find_mountpoint();
81 if (__debugfs == NULL) {
82 tracing_dir = "tracing/";
84 __debugfs = debugfs_find_mountpoint();
85 if (__debugfs == NULL)
86 return -ENOTSUP;
89 ret = e_snprintf(buf, PATH_MAX, "%s/%s%s",
90 __debugfs, tracing_dir, trace_file);
91 if (ret >= 0) {
92 pr_debug("Opening %s write=%d\n", buf, readwrite);
93 if (readwrite && !probe_event_dry_run)
94 ret = open(buf, O_RDWR | O_APPEND, 0);
95 else
96 ret = open(buf, O_RDONLY, 0);
98 if (ret < 0)
99 ret = -errno;
101 return ret;
104 static int open_kprobe_events(bool readwrite)
106 return open_probe_events("kprobe_events", readwrite);
109 static int open_uprobe_events(bool readwrite)
111 return open_probe_events("uprobe_events", readwrite);
114 int probe_file__open(int flag)
116 int fd;
118 if (flag & PF_FL_UPROBE)
119 fd = open_uprobe_events(flag & PF_FL_RW);
120 else
121 fd = open_kprobe_events(flag & PF_FL_RW);
122 if (fd < 0)
123 print_open_warning(fd, flag & PF_FL_UPROBE);
125 return fd;
128 int probe_file__open_both(int *kfd, int *ufd, int flag)
130 if (!kfd || !ufd)
131 return -EINVAL;
133 *kfd = open_kprobe_events(flag & PF_FL_RW);
134 *ufd = open_uprobe_events(flag & PF_FL_RW);
135 if (*kfd < 0 && *ufd < 0) {
136 print_both_open_warning(*kfd, *ufd);
137 return *kfd;
140 return 0;
143 /* Get raw string list of current kprobe_events or uprobe_events */
144 struct strlist *probe_file__get_rawlist(int fd)
146 int ret, idx;
147 FILE *fp;
148 char buf[MAX_CMDLEN];
149 char *p;
150 struct strlist *sl;
152 sl = strlist__new(NULL, NULL);
154 fp = fdopen(dup(fd), "r");
155 while (!feof(fp)) {
156 p = fgets(buf, MAX_CMDLEN, fp);
157 if (!p)
158 break;
160 idx = strlen(p) - 1;
161 if (p[idx] == '\n')
162 p[idx] = '\0';
163 ret = strlist__add(sl, buf);
164 if (ret < 0) {
165 pr_debug("strlist__add failed (%d)\n", ret);
166 strlist__delete(sl);
167 return NULL;
170 fclose(fp);
172 return sl;
175 static struct strlist *__probe_file__get_namelist(int fd, bool include_group)
177 char buf[128];
178 struct strlist *sl, *rawlist;
179 struct str_node *ent;
180 struct probe_trace_event tev;
181 int ret = 0;
183 memset(&tev, 0, sizeof(tev));
184 rawlist = probe_file__get_rawlist(fd);
185 if (!rawlist)
186 return NULL;
187 sl = strlist__new(NULL, NULL);
188 strlist__for_each(ent, rawlist) {
189 ret = parse_probe_trace_command(ent->s, &tev);
190 if (ret < 0)
191 break;
192 if (include_group) {
193 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
194 tev.event);
195 if (ret >= 0)
196 ret = strlist__add(sl, buf);
197 } else
198 ret = strlist__add(sl, tev.event);
199 clear_probe_trace_event(&tev);
200 if (ret < 0)
201 break;
203 strlist__delete(rawlist);
205 if (ret < 0) {
206 strlist__delete(sl);
207 return NULL;
209 return sl;
212 /* Get current perf-probe event names */
213 struct strlist *probe_file__get_namelist(int fd)
215 return __probe_file__get_namelist(fd, false);
218 int probe_file__add_event(int fd, struct probe_trace_event *tev)
220 int ret = 0;
221 char *buf = synthesize_probe_trace_command(tev);
222 char sbuf[STRERR_BUFSIZE];
224 if (!buf) {
225 pr_debug("Failed to synthesize probe trace event.\n");
226 return -EINVAL;
229 pr_debug("Writing event: %s\n", buf);
230 if (!probe_event_dry_run) {
231 ret = write(fd, buf, strlen(buf));
232 if (ret <= 0) {
233 ret = -errno;
234 pr_warning("Failed to write event: %s\n",
235 strerror_r(errno, sbuf, sizeof(sbuf)));
238 free(buf);
240 return ret;
243 static int __del_trace_probe_event(int fd, struct str_node *ent)
245 char *p;
246 char buf[128];
247 int ret;
249 /* Convert from perf-probe event to trace-probe event */
250 ret = e_snprintf(buf, 128, "-:%s", ent->s);
251 if (ret < 0)
252 goto error;
254 p = strchr(buf + 2, ':');
255 if (!p) {
256 pr_debug("Internal error: %s should have ':' but not.\n",
257 ent->s);
258 ret = -ENOTSUP;
259 goto error;
261 *p = '/';
263 pr_debug("Writing event: %s\n", buf);
264 ret = write(fd, buf, strlen(buf));
265 if (ret < 0) {
266 ret = -errno;
267 goto error;
270 pr_info("Removed event: %s\n", ent->s);
271 return 0;
272 error:
273 pr_warning("Failed to delete event: %s\n",
274 strerror_r(-ret, buf, sizeof(buf)));
275 return ret;
278 int probe_file__del_events(int fd, struct strfilter *filter)
280 struct strlist *namelist;
281 struct str_node *ent;
282 const char *p;
283 int ret = -ENOENT;
285 namelist = __probe_file__get_namelist(fd, true);
286 if (!namelist)
287 return -ENOENT;
289 strlist__for_each(ent, namelist) {
290 p = strchr(ent->s, ':');
291 if ((p && strfilter__compare(filter, p + 1)) ||
292 strfilter__compare(filter, ent->s)) {
293 ret = __del_trace_probe_event(fd, ent);
294 if (ret < 0)
295 break;
298 strlist__delete(namelist);
300 return ret;