aboutsummaryrefslogtreecommitdiffstats
path: root/tools/perf
diff options
context:
space:
mode:
Diffstat (limited to 'tools/perf')
-rw-r--r--tools/perf/builtin-probe.c70
-rw-r--r--tools/perf/util/probe-event.c615
-rw-r--r--tools/perf/util/probe-event.h24
3 files changed, 480 insertions, 229 deletions
diff --git a/tools/perf/builtin-probe.c b/tools/perf/builtin-probe.c
index daf4668d2de0..64bc11a183bf 100644
--- a/tools/perf/builtin-probe.c
+++ b/tools/perf/builtin-probe.c
@@ -59,23 +59,25 @@ static struct {
59 59
60 60
61/* Parse an event definition. Note that any error must die. */ 61/* Parse an event definition. Note that any error must die. */
62static void parse_probe_event(const char *str) 62static int parse_probe_event(const char *str)
63{ 63{
64 struct perf_probe_event *pev = &params.events[params.nevents]; 64 struct perf_probe_event *pev = &params.events[params.nevents];
65 int ret;
65 66
66 pr_debug("probe-definition(%d): %s\n", params.nevents, str); 67 pr_debug("probe-definition(%d): %s\n", params.nevents, str);
67 if (++params.nevents == MAX_PROBES) 68 if (++params.nevents == MAX_PROBES)
68 die("Too many probes (> %d) are specified.", MAX_PROBES); 69 die("Too many probes (> %d) are specified.", MAX_PROBES);
69 70
70 /* Parse a perf-probe command into event */ 71 /* Parse a perf-probe command into event */
71 parse_perf_probe_command(str, pev); 72 ret = parse_perf_probe_command(str, pev);
72
73 pr_debug("%d arguments\n", pev->nargs); 73 pr_debug("%d arguments\n", pev->nargs);
74
75 return ret;
74} 76}
75 77
76static void parse_probe_event_argv(int argc, const char **argv) 78static int parse_probe_event_argv(int argc, const char **argv)
77{ 79{
78 int i, len; 80 int i, len, ret;
79 char *buf; 81 char *buf;
80 82
81 /* Bind up rest arguments */ 83 /* Bind up rest arguments */
@@ -86,16 +88,18 @@ static void parse_probe_event_argv(int argc, const char **argv)
86 len = 0; 88 len = 0;
87 for (i = 0; i < argc; i++) 89 for (i = 0; i < argc; i++)
88 len += sprintf(&buf[len], "%s ", argv[i]); 90 len += sprintf(&buf[len], "%s ", argv[i]);
89 parse_probe_event(buf); 91 ret = parse_probe_event(buf);
90 free(buf); 92 free(buf);
93 return ret;
91} 94}
92 95
93static int opt_add_probe_event(const struct option *opt __used, 96static int opt_add_probe_event(const struct option *opt __used,
94 const char *str, int unset __used) 97 const char *str, int unset __used)
95{ 98{
96 if (str) 99 if (str)
97 parse_probe_event(str); 100 return parse_probe_event(str);
98 return 0; 101 else
102 return 0;
99} 103}
100 104
101static int opt_del_probe_event(const struct option *opt __used, 105static int opt_del_probe_event(const struct option *opt __used,
@@ -113,11 +117,14 @@ static int opt_del_probe_event(const struct option *opt __used,
113static int opt_show_lines(const struct option *opt __used, 117static int opt_show_lines(const struct option *opt __used,
114 const char *str, int unset __used) 118 const char *str, int unset __used)
115{ 119{
120 int ret = 0;
121
116 if (str) 122 if (str)
117 parse_line_range_desc(str, &params.line_range); 123 ret = parse_line_range_desc(str, &params.line_range);
118 INIT_LIST_HEAD(&params.line_range.line_list); 124 INIT_LIST_HEAD(&params.line_range.line_list);
119 params.show_lines = true; 125 params.show_lines = true;
120 return 0; 126
127 return ret;
121} 128}
122#endif 129#endif
123 130
@@ -178,6 +185,8 @@ static const struct option options[] = {
178 185
179int cmd_probe(int argc, const char **argv, const char *prefix __used) 186int cmd_probe(int argc, const char **argv, const char *prefix __used)
180{ 187{
188 int ret;
189
181 argc = parse_options(argc, argv, options, probe_usage, 190 argc = parse_options(argc, argv, options, probe_usage,
182 PARSE_OPT_STOP_AT_NON_OPTION); 191 PARSE_OPT_STOP_AT_NON_OPTION);
183 if (argc > 0) { 192 if (argc > 0) {
@@ -185,7 +194,11 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
185 pr_warning(" Error: '-' is not supported.\n"); 194 pr_warning(" Error: '-' is not supported.\n");
186 usage_with_options(probe_usage, options); 195 usage_with_options(probe_usage, options);
187 } 196 }
188 parse_probe_event_argv(argc, argv); 197 ret = parse_probe_event_argv(argc, argv);
198 if (ret < 0) {
199 pr_err(" Error: Parse Error. (%d)\n", ret);
200 return ret;
201 }
189 } 202 }
190 203
191 if ((!params.nevents && !params.dellist && !params.list_events && 204 if ((!params.nevents && !params.dellist && !params.list_events &&
@@ -197,16 +210,18 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
197 210
198 if (params.list_events) { 211 if (params.list_events) {
199 if (params.nevents != 0 || params.dellist) { 212 if (params.nevents != 0 || params.dellist) {
200 pr_warning(" Error: Don't use --list with" 213 pr_err(" Error: Don't use --list with --add/--del.\n");
201 " --add/--del.\n");
202 usage_with_options(probe_usage, options); 214 usage_with_options(probe_usage, options);
203 } 215 }
204 if (params.show_lines) { 216 if (params.show_lines) {
205 pr_warning(" Error: Don't use --list with --line.\n"); 217 pr_err(" Error: Don't use --list with --line.\n");
206 usage_with_options(probe_usage, options); 218 usage_with_options(probe_usage, options);
207 } 219 }
208 show_perf_probe_events(); 220 ret = show_perf_probe_events();
209 return 0; 221 if (ret < 0)
222 pr_err(" Error: Failed to show event list. (%d)\n",
223 ret);
224 return ret;
210 } 225 }
211 226
212#ifdef DWARF_SUPPORT 227#ifdef DWARF_SUPPORT
@@ -217,19 +232,30 @@ int cmd_probe(int argc, const char **argv, const char *prefix __used)
217 usage_with_options(probe_usage, options); 232 usage_with_options(probe_usage, options);
218 } 233 }
219 234
220 show_line_range(&params.line_range); 235 ret = show_line_range(&params.line_range);
221 return 0; 236 if (ret < 0)
237 pr_err(" Error: Failed to show lines. (%d)\n", ret);
238 return ret;
222 } 239 }
223#endif 240#endif
224 241
225 if (params.dellist) { 242 if (params.dellist) {
226 del_perf_probe_events(params.dellist); 243 ret = del_perf_probe_events(params.dellist);
227 strlist__delete(params.dellist); 244 strlist__delete(params.dellist);
228 if (params.nevents == 0) 245 if (ret < 0) {
229 return 0; 246 pr_err(" Error: Failed to delete events. (%d)\n", ret);
247 return ret;
248 }
230 } 249 }
231 250
232 add_perf_probe_events(params.events, params.nevents, params.force_add); 251 if (params.nevents) {
252 ret = add_perf_probe_events(params.events, params.nevents,
253 params.force_add);
254 if (ret < 0) {
255 pr_err(" Error: Failed to add events. (%d)\n", ret);
256 return ret;
257 }
258 }
233 return 0; 259 return 0;
234} 260}
235 261
diff --git a/tools/perf/util/probe-event.c b/tools/perf/util/probe-event.c
index 7893b3207dbe..bd68f7b33b21 100644
--- a/tools/perf/util/probe-event.c
+++ b/tools/perf/util/probe-event.c
@@ -53,7 +53,7 @@
53 53
54bool probe_event_dry_run; /* Dry run flag */ 54bool probe_event_dry_run; /* Dry run flag */
55 55
56#define semantic_error(msg ...) die("Semantic error :" msg) 56#define semantic_error(msg ...) pr_err("Semantic error :" msg)
57 57
58/* If there is no space to write, returns -E2BIG. */ 58/* If there is no space to write, returns -E2BIG. */
59static int e_snprintf(char *str, size_t size, const char *format, ...) 59static int e_snprintf(char *str, size_t size, const char *format, ...)
@@ -76,19 +76,30 @@ static struct map_groups kmap_groups;
76static struct map *kmaps[MAP__NR_TYPES]; 76static struct map *kmaps[MAP__NR_TYPES];
77 77
78/* Initialize symbol maps and path of vmlinux */ 78/* Initialize symbol maps and path of vmlinux */
79static void init_vmlinux(void) 79static int init_vmlinux(void)
80{ 80{
81 int ret;
82
81 symbol_conf.sort_by_name = true; 83 symbol_conf.sort_by_name = true;
82 if (symbol_conf.vmlinux_name == NULL) 84 if (symbol_conf.vmlinux_name == NULL)
83 symbol_conf.try_vmlinux_path = true; 85 symbol_conf.try_vmlinux_path = true;
84 else 86 else
85 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name); 87 pr_debug("Use vmlinux: %s\n", symbol_conf.vmlinux_name);
86 if (symbol__init() < 0) 88 ret = symbol__init();
87 die("Failed to init symbol map."); 89 if (ret < 0) {
90 pr_debug("Failed to init symbol map.\n");
91 goto out;
92 }
88 93
89 map_groups__init(&kmap_groups); 94 map_groups__init(&kmap_groups);
90 if (map_groups__create_kernel_maps(&kmap_groups, kmaps) < 0) 95 ret = map_groups__create_kernel_maps(&kmap_groups, kmaps);
91 die("Failed to create kernel maps."); 96 if (ret < 0)
97 pr_debug("Failed to create kernel maps.\n");
98
99out:
100 if (ret < 0)
101 pr_warning("Failed to init vmlinux path.\n");
102 return ret;
92} 103}
93 104
94#ifdef DWARF_SUPPORT 105#ifdef DWARF_SUPPORT
@@ -102,24 +113,32 @@ static int open_vmlinux(void)
102 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY); 113 return open(kmaps[MAP__FUNCTION]->dso->long_name, O_RDONLY);
103} 114}
104 115
105static void convert_to_perf_probe_point(struct kprobe_trace_point *tp, 116/* Convert trace point to probe point with debuginfo */
106 struct perf_probe_point *pp) 117static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
118 struct perf_probe_point *pp)
107{ 119{
108 struct symbol *sym; 120 struct symbol *sym;
109 int fd, ret = 0; 121 int fd, ret = -ENOENT;
110 122
111 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], 123 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
112 tp->symbol, NULL); 124 tp->symbol, NULL);
113 if (sym) { 125 if (sym) {
114 fd = open_vmlinux(); 126 fd = open_vmlinux();
115 ret = find_perf_probe_point(fd, sym->start + tp->offset, pp); 127 if (fd >= 0) {
116 close(fd); 128 ret = find_perf_probe_point(fd,
129 sym->start + tp->offset, pp);
130 close(fd);
131 }
117 } 132 }
118 if (ret <= 0) { 133 if (ret <= 0) {
134 pr_debug("Failed to find corresponding probes from "
135 "debuginfo. Use kprobe event information.\n");
119 pp->function = xstrdup(tp->symbol); 136 pp->function = xstrdup(tp->symbol);
120 pp->offset = tp->offset; 137 pp->offset = tp->offset;
121 } 138 }
122 pp->retprobe = tp->retprobe; 139 pp->retprobe = tp->retprobe;
140
141 return 0;
123} 142}
124 143
125/* Try to find perf_probe_event with debuginfo */ 144/* Try to find perf_probe_event with debuginfo */
@@ -131,9 +150,10 @@ static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
131 150
132 fd = open_vmlinux(); 151 fd = open_vmlinux();
133 if (fd < 0) { 152 if (fd < 0) {
134 if (need_dwarf) 153 if (need_dwarf) {
135 die("Could not open debuginfo file."); 154 pr_warning("Failed to open debuginfo file.\n");
136 155 return fd;
156 }
137 pr_debug("Could not open vmlinux. Try to use symbols.\n"); 157 pr_debug("Could not open vmlinux. Try to use symbols.\n");
138 return 0; 158 return 0;
139 } 159 }
@@ -142,30 +162,32 @@ static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
142 ntevs = find_kprobe_trace_events(fd, pev, tevs); 162 ntevs = find_kprobe_trace_events(fd, pev, tevs);
143 close(fd); 163 close(fd);
144 164
145 if (ntevs > 0) /* Succeeded to find trace events */ 165 if (ntevs > 0) { /* Succeeded to find trace events */
166 pr_debug("find %d kprobe_trace_events.\n", ntevs);
146 return ntevs; 167 return ntevs;
168 }
147 169
148 if (ntevs == 0) /* No error but failed to find probe point. */ 170 if (ntevs == 0) { /* No error but failed to find probe point. */
149 die("Probe point '%s' not found. - probe not added.", 171 pr_warning("Probe point '%s' not found.\n",
150 synthesize_perf_probe_point(&pev->point)); 172 synthesize_perf_probe_point(&pev->point));
151 173 return -ENOENT;
152 /* Error path */ 174 }
175 /* Error path : ntevs < 0 */
153 if (need_dwarf) { 176 if (need_dwarf) {
154 if (ntevs == -EBADF) 177 if (ntevs == -EBADF)
155 pr_warning("No dwarf info found in the vmlinux - " 178 pr_warning("No dwarf info found in the vmlinux - "
156 "please rebuild with CONFIG_DEBUG_INFO=y.\n"); 179 "please rebuild with CONFIG_DEBUG_INFO=y.\n");
157 die("Failed to analyze debuginfo."); 180 return ntevs;
158 } 181 }
159 pr_debug("An error occurred in debuginfo analysis." 182 pr_debug("An error occurred in debuginfo analysis."
160 " Try to use symbols.\n"); 183 " Try to use symbols.\n");
161 return 0; 184 return 0;
162
163} 185}
164 186
165#define LINEBUF_SIZE 256 187#define LINEBUF_SIZE 256
166#define NR_ADDITIONAL_LINES 2 188#define NR_ADDITIONAL_LINES 2
167 189
168static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num) 190static int show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
169{ 191{
170 char buf[LINEBUF_SIZE]; 192 char buf[LINEBUF_SIZE];
171 const char *color = PERF_COLOR_BLUE; 193 const char *color = PERF_COLOR_BLUE;
@@ -190,19 +212,22 @@ static void show_one_line(FILE *fp, unsigned int l, bool skip, bool show_num)
190 color_fprintf(stdout, color, "%s", buf); 212 color_fprintf(stdout, color, "%s", buf);
191 } 213 }
192 } 214 }
193 return; 215
216 return 0;
194error: 217error:
195 if (feof(fp)) 218 if (feof(fp))
196 die("Source file is shorter than expected."); 219 pr_warning("Source file is shorter than expected.\n");
197 else 220 else
198 die("File read error: %s", strerror(errno)); 221 pr_warning("File read error: %s\n", strerror(errno));
222
223 return -1;
199} 224}
200 225
201/* 226/*
202 * Show line-range always requires debuginfo to find source file and 227 * Show line-range always requires debuginfo to find source file and
203 * line number. 228 * line number.
204 */ 229 */
205void show_line_range(struct line_range *lr) 230int show_line_range(struct line_range *lr)
206{ 231{
207 unsigned int l = 1; 232 unsigned int l = 1;
208 struct line_node *ln; 233 struct line_node *ln;
@@ -210,14 +235,25 @@ void show_line_range(struct line_range *lr)
210 int fd, ret; 235 int fd, ret;
211 236
212 /* Search a line range */ 237 /* Search a line range */
213 init_vmlinux(); 238 ret = init_vmlinux();
239 if (ret < 0)
240 return ret;
241
214 fd = open_vmlinux(); 242 fd = open_vmlinux();
215 if (fd < 0) 243 if (fd < 0) {
216 die("Could not open debuginfo file."); 244 pr_warning("Failed to open debuginfo file.\n");
245 return fd;
246 }
247
217 ret = find_line_range(fd, lr); 248 ret = find_line_range(fd, lr);
218 if (ret <= 0)
219 die("Source line is not found.\n");
220 close(fd); 249 close(fd);
250 if (ret == 0) {
251 pr_warning("Specified source line is not found.\n");
252 return -ENOENT;
253 } else if (ret < 0) {
254 pr_warning("Debuginfo analysis failed. (%d)\n", ret);
255 return ret;
256 }
221 257
222 setup_pager(); 258 setup_pager();
223 259
@@ -228,52 +264,68 @@ void show_line_range(struct line_range *lr)
228 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start); 264 fprintf(stdout, "<%s:%d>\n", lr->file, lr->start);
229 265
230 fp = fopen(lr->path, "r"); 266 fp = fopen(lr->path, "r");
231 if (fp == NULL) 267 if (fp == NULL) {
232 die("Failed to open %s: %s", lr->path, strerror(errno)); 268 pr_warning("Failed to open %s: %s\n", lr->path,
269 strerror(errno));
270 return -errno;
271 }
233 /* Skip to starting line number */ 272 /* Skip to starting line number */
234 while (l < lr->start) 273 while (l < lr->start && ret >= 0)
235 show_one_line(fp, l++, true, false); 274 ret = show_one_line(fp, l++, true, false);
275 if (ret < 0)
276 goto end;
236 277
237 list_for_each_entry(ln, &lr->line_list, list) { 278 list_for_each_entry(ln, &lr->line_list, list) {
238 while (ln->line > l) 279 while (ln->line > l && ret >= 0)
239 show_one_line(fp, (l++) - lr->offset, false, false); 280 ret = show_one_line(fp, (l++) - lr->offset,
240 show_one_line(fp, (l++) - lr->offset, false, true); 281 false, false);
282 if (ret >= 0)
283 ret = show_one_line(fp, (l++) - lr->offset,
284 false, true);
285 if (ret < 0)
286 goto end;
241 } 287 }
242 288
243 if (lr->end == INT_MAX) 289 if (lr->end == INT_MAX)
244 lr->end = l + NR_ADDITIONAL_LINES; 290 lr->end = l + NR_ADDITIONAL_LINES;
245 while (l < lr->end && !feof(fp)) 291 while (l < lr->end && !feof(fp) && ret >= 0)
246 show_one_line(fp, (l++) - lr->offset, false, false); 292 ret = show_one_line(fp, (l++) - lr->offset, false, false);
247 293end:
248 fclose(fp); 294 fclose(fp);
295 return ret;
249} 296}
250 297
251#else /* !DWARF_SUPPORT */ 298#else /* !DWARF_SUPPORT */
252 299
253static void convert_to_perf_probe_point(struct kprobe_trace_point *tp, 300static int convert_to_perf_probe_point(struct kprobe_trace_point *tp,
254 struct perf_probe_point *pp) 301 struct perf_probe_point *pp)
255{ 302{
256 pp->function = xstrdup(tp->symbol); 303 pp->function = xstrdup(tp->symbol);
257 pp->offset = tp->offset; 304 pp->offset = tp->offset;
258 pp->retprobe = tp->retprobe; 305 pp->retprobe = tp->retprobe;
306
307 return 0;
259} 308}
260 309
261static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev, 310static int try_to_find_kprobe_trace_events(struct perf_probe_event *pev,
262 struct kprobe_trace_event **tevs __unused) 311 struct kprobe_trace_event **tevs __unused)
263{ 312{
264 if (perf_probe_event_need_dwarf(pev)) 313 if (perf_probe_event_need_dwarf(pev)) {
265 die("Debuginfo-analysis is not supported"); 314 pr_warning("Debuginfo-analysis is not supported.\n");
315 return -ENOSYS;
316 }
266 return 0; 317 return 0;
267} 318}
268 319
269void show_line_range(struct line_range *lr __unused) 320int show_line_range(struct line_range *lr __unused)
270{ 321{
271 die("Debuginfo-analysis is not supported"); 322 pr_warning("Debuginfo-analysis is not supported.\n");
323 return -ENOSYS;
272} 324}
273 325
274#endif 326#endif
275 327
276void parse_line_range_desc(const char *arg, struct line_range *lr) 328int parse_line_range_desc(const char *arg, struct line_range *lr)
277{ 329{
278 const char *ptr; 330 const char *ptr;
279 char *tmp; 331 char *tmp;
@@ -293,12 +345,16 @@ void parse_line_range_desc(const char *arg, struct line_range *lr)
293 else 345 else
294 lr->end = 0; 346 lr->end = 0;
295 pr_debug("Line range is %u to %u\n", lr->start, lr->end); 347 pr_debug("Line range is %u to %u\n", lr->start, lr->end);
296 if (lr->end && lr->start > lr->end) 348 if (lr->end && lr->start > lr->end) {
297 semantic_error("Start line must be smaller" 349 semantic_error("Start line must be smaller"
298 " than end line."); 350 " than end line.\n");
299 if (*tmp != '\0') 351 return -EINVAL;
300 semantic_error("Tailing with invalid character '%d'.", 352 }
353 if (*tmp != '\0') {
354 semantic_error("Tailing with invalid character '%d'.\n",
301 *tmp); 355 *tmp);
356 return -EINVAL;
357 }
302 tmp = xstrndup(arg, (ptr - arg)); 358 tmp = xstrndup(arg, (ptr - arg));
303 } else 359 } else
304 tmp = xstrdup(arg); 360 tmp = xstrdup(arg);
@@ -307,6 +363,8 @@ void parse_line_range_desc(const char *arg, struct line_range *lr)
307 lr->file = tmp; 363 lr->file = tmp;
308 else 364 else
309 lr->function = tmp; 365 lr->function = tmp;
366
367 return 0;
310} 368}
311 369
312/* Check the name is good for event/group */ 370/* Check the name is good for event/group */
@@ -322,7 +380,7 @@ static bool check_event_name(const char *name)
322} 380}
323 381
324/* Parse probepoint definition. */ 382/* Parse probepoint definition. */
325static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev) 383static int parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
326{ 384{
327 struct perf_probe_point *pp = &pev->point; 385 struct perf_probe_point *pp = &pev->point;
328 char *ptr, *tmp; 386 char *ptr, *tmp;
@@ -339,12 +397,15 @@ static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
339 if (ptr && *ptr == '=') { /* Event name */ 397 if (ptr && *ptr == '=') { /* Event name */
340 *ptr = '\0'; 398 *ptr = '\0';
341 tmp = ptr + 1; 399 tmp = ptr + 1;
342 ptr = strchr(arg, ':'); 400 if (strchr(arg, ':')) {
343 if (ptr) /* Group name is not supported yet. */ 401 semantic_error("Group name is not supported yet.\n");
344 semantic_error("Group name is not supported yet."); 402 return -ENOTSUP;
345 if (!check_event_name(arg)) 403 }
404 if (!check_event_name(arg)) {
346 semantic_error("%s is bad for event name -it must " 405 semantic_error("%s is bad for event name -it must "
347 "follow C symbol-naming rule.", arg); 406 "follow C symbol-naming rule.\n", arg);
407 return -EINVAL;
408 }
348 pev->event = xstrdup(arg); 409 pev->event = xstrdup(arg);
349 pev->group = NULL; 410 pev->group = NULL;
350 arg = tmp; 411 arg = tmp;
@@ -378,64 +439,89 @@ static void parse_perf_probe_point(char *arg, struct perf_probe_event *pev)
378 switch (c) { 439 switch (c) {
379 case ':': /* Line number */ 440 case ':': /* Line number */
380 pp->line = strtoul(arg, &tmp, 0); 441 pp->line = strtoul(arg, &tmp, 0);
381 if (*tmp != '\0') 442 if (*tmp != '\0') {
382 semantic_error("There is non-digit char" 443 semantic_error("There is non-digit char"
383 " in line number."); 444 " in line number.\n");
445 return -EINVAL;
446 }
384 break; 447 break;
385 case '+': /* Byte offset from a symbol */ 448 case '+': /* Byte offset from a symbol */
386 pp->offset = strtoul(arg, &tmp, 0); 449 pp->offset = strtoul(arg, &tmp, 0);
387 if (*tmp != '\0') 450 if (*tmp != '\0') {
388 semantic_error("There is non-digit character" 451 semantic_error("There is non-digit character"
389 " in offset."); 452 " in offset.\n");
453 return -EINVAL;
454 }
390 break; 455 break;
391 case '@': /* File name */ 456 case '@': /* File name */
392 if (pp->file) 457 if (pp->file) {
393 semantic_error("SRC@SRC is not allowed."); 458 semantic_error("SRC@SRC is not allowed.\n");
459 return -EINVAL;
460 }
394 pp->file = xstrdup(arg); 461 pp->file = xstrdup(arg);
395 break; 462 break;
396 case '%': /* Probe places */ 463 case '%': /* Probe places */
397 if (strcmp(arg, "return") == 0) { 464 if (strcmp(arg, "return") == 0) {
398 pp->retprobe = 1; 465 pp->retprobe = 1;
399 } else /* Others not supported yet */ 466 } else { /* Others not supported yet */
400 semantic_error("%%%s is not supported.", arg); 467 semantic_error("%%%s is not supported.\n", arg);
468 return -ENOTSUP;
469 }
401 break; 470 break;
402 default: 471 default: /* Buggy case */
403 DIE_IF("Program has a bug."); 472 pr_err("This program has a bug at %s:%d.\n",
473 __FILE__, __LINE__);
474 return -ENOTSUP;
404 break; 475 break;
405 } 476 }
406 } 477 }
407 478
408 /* Exclusion check */ 479 /* Exclusion check */
409 if (pp->lazy_line && pp->line) 480 if (pp->lazy_line && pp->line) {
410 semantic_error("Lazy pattern can't be used with line number."); 481 semantic_error("Lazy pattern can't be used with line number.");
482 return -EINVAL;
483 }
411 484
412 if (pp->lazy_line && pp->offset) 485 if (pp->lazy_line && pp->offset) {
413 semantic_error("Lazy pattern can't be used with offset."); 486 semantic_error("Lazy pattern can't be used with offset.");
487 return -EINVAL;
488 }
414 489
415 if (pp->line && pp->offset) 490 if (pp->line && pp->offset) {
416 semantic_error("Offset can't be used with line number."); 491 semantic_error("Offset can't be used with line number.");
492 return -EINVAL;
493 }
417 494
418 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) 495 if (!pp->line && !pp->lazy_line && pp->file && !pp->function) {
419 semantic_error("File always requires line number or " 496 semantic_error("File always requires line number or "
420 "lazy pattern."); 497 "lazy pattern.");
498 return -EINVAL;
499 }
421 500
422 if (pp->offset && !pp->function) 501 if (pp->offset && !pp->function) {
423 semantic_error("Offset requires an entry function."); 502 semantic_error("Offset requires an entry function.");
503 return -EINVAL;
504 }
424 505
425 if (pp->retprobe && !pp->function) 506 if (pp->retprobe && !pp->function) {
426 semantic_error("Return probe requires an entry function."); 507 semantic_error("Return probe requires an entry function.");
508 return -EINVAL;
509 }
427 510
428 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) 511 if ((pp->offset || pp->line || pp->lazy_line) && pp->retprobe) {
429 semantic_error("Offset/Line/Lazy pattern can't be used with " 512 semantic_error("Offset/Line/Lazy pattern can't be used with "
430 "return probe."); 513 "return probe.");
514 return -EINVAL;
515 }
431 516
432 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n", 517 pr_debug("symbol:%s file:%s line:%d offset:%lu return:%d lazy:%s\n",
433 pp->function, pp->file, pp->line, pp->offset, pp->retprobe, 518 pp->function, pp->file, pp->line, pp->offset, pp->retprobe,
434 pp->lazy_line); 519 pp->lazy_line);
520 return 0;
435} 521}
436 522
437/* Parse perf-probe event argument */ 523/* Parse perf-probe event argument */
438static void parse_perf_probe_arg(char *str, struct perf_probe_arg *arg) 524static int parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
439{ 525{
440 char *tmp; 526 char *tmp;
441 struct perf_probe_arg_field **fieldp; 527 struct perf_probe_arg_field **fieldp;
@@ -461,7 +547,7 @@ static void parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
461 /* A variable, register, symbol or special value */ 547 /* A variable, register, symbol or special value */
462 arg->var = xstrdup(str); 548 arg->var = xstrdup(str);
463 pr_debug("%s\n", arg->var); 549 pr_debug("%s\n", arg->var);
464 return; 550 return 0;
465 } 551 }
466 552
467 /* Structure fields */ 553 /* Structure fields */
@@ -477,8 +563,10 @@ static void parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
477 } else if (tmp[1] == '>') { 563 } else if (tmp[1] == '>') {
478 str = tmp + 2; 564 str = tmp + 2;
479 (*fieldp)->ref = true; 565 (*fieldp)->ref = true;
480 } else 566 } else {
481 semantic_error("Argument parse error: %s", str); 567 semantic_error("Argument parse error: %s\n", str);
568 return -EINVAL;
569 }
482 570
483 tmp = strpbrk(str, "-."); 571 tmp = strpbrk(str, "-.");
484 if (tmp) { 572 if (tmp) {
@@ -493,34 +581,47 @@ static void parse_perf_probe_arg(char *str, struct perf_probe_arg *arg)
493 /* If no name is specified, set the last field name */ 581 /* If no name is specified, set the last field name */
494 if (!arg->name) 582 if (!arg->name)
495 arg->name = xstrdup((*fieldp)->name); 583 arg->name = xstrdup((*fieldp)->name);
584
585 return 0;
496} 586}
497 587
498/* Parse perf-probe event command */ 588/* Parse perf-probe event command */
499void parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev) 589int parse_perf_probe_command(const char *cmd, struct perf_probe_event *pev)
500{ 590{
501 char **argv; 591 char **argv;
502 int argc, i; 592 int argc, i, ret = 0;
503 593
504 argv = argv_split(cmd, &argc); 594 argv = argv_split(cmd, &argc);
505 if (!argv) 595 if (!argv) {
506 die("argv_split failed."); 596 pr_debug("Failed to split arguments.\n");
507 if (argc > MAX_PROBE_ARGS + 1) 597 return -ENOMEM;
508 semantic_error("Too many arguments"); 598 }
509 599 if (argc - 1 > MAX_PROBE_ARGS) {
600 semantic_error("Too many probe arguments (%d).\n", argc - 1);
601 ret = -ERANGE;
602 goto out;
603 }
510 /* Parse probe point */ 604 /* Parse probe point */
511 parse_perf_probe_point(argv[0], pev); 605 ret = parse_perf_probe_point(argv[0], pev);
606 if (ret < 0)
607 goto out;
512 608
513 /* Copy arguments and ensure return probe has no C argument */ 609 /* Copy arguments and ensure return probe has no C argument */
514 pev->nargs = argc - 1; 610 pev->nargs = argc - 1;
515 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs); 611 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
516 for (i = 0; i < pev->nargs; i++) { 612 for (i = 0; i < pev->nargs && ret >= 0; i++) {
517 parse_perf_probe_arg(argv[i + 1], &pev->args[i]); 613 ret = parse_perf_probe_arg(argv[i + 1], &pev->args[i]);
518 if (is_c_varname(pev->args[i].var) && pev->point.retprobe) 614 if (ret >= 0 &&
615 is_c_varname(pev->args[i].var) && pev->point.retprobe) {
519 semantic_error("You can't specify local variable for" 616 semantic_error("You can't specify local variable for"
520 " kretprobe"); 617 " kretprobe.\n");
618 ret = -EINVAL;
619 }
521 } 620 }
522 621out:
523 argv_free(argv); 622 argv_free(argv);
623
624 return ret;
524} 625}
525 626
526/* Return true if this perf_probe_event requires debuginfo */ 627/* Return true if this perf_probe_event requires debuginfo */
@@ -539,7 +640,7 @@ bool perf_probe_event_need_dwarf(struct perf_probe_event *pev)
539} 640}
540 641
541/* Parse kprobe_events event into struct probe_point */ 642/* Parse kprobe_events event into struct probe_point */
542void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev) 643int parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
543{ 644{
544 struct kprobe_trace_point *tp = &tev->point; 645 struct kprobe_trace_point *tp = &tev->point;
545 char pr; 646 char pr;
@@ -549,17 +650,25 @@ void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
549 650
550 pr_debug("Parsing kprobe_events: %s\n", cmd); 651 pr_debug("Parsing kprobe_events: %s\n", cmd);
551 argv = argv_split(cmd, &argc); 652 argv = argv_split(cmd, &argc);
552 if (!argv) 653 if (!argv) {
553 die("argv_split failed."); 654 pr_debug("Failed to split arguments.\n");
554 if (argc < 2) 655 return -ENOMEM;
555 semantic_error("Too less arguments."); 656 }
657 if (argc < 2) {
658 semantic_error("Too few probe arguments.\n");
659 ret = -ERANGE;
660 goto out;
661 }
556 662
557 /* Scan event and group name. */ 663 /* Scan event and group name. */
558 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]", 664 ret = sscanf(argv[0], "%c:%a[^/ \t]/%a[^ \t]",
559 &pr, (float *)(void *)&tev->group, 665 &pr, (float *)(void *)&tev->group,
560 (float *)(void *)&tev->event); 666 (float *)(void *)&tev->event);
561 if (ret != 3) 667 if (ret != 3) {
562 semantic_error("Failed to parse event name: %s", argv[0]); 668 semantic_error("Failed to parse event name: %s\n", argv[0]);
669 ret = -EINVAL;
670 goto out;
671 }
563 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr); 672 pr_debug("Group:%s Event:%s probe:%c\n", tev->group, tev->event, pr);
564 673
565 tp->retprobe = (pr == 'r'); 674 tp->retprobe = (pr == 'r');
@@ -582,8 +691,10 @@ void parse_kprobe_trace_command(const char *cmd, struct kprobe_trace_event *tev)
582 /* TODO: parse regs and offset */ 691 /* TODO: parse regs and offset */
583 tev->args[i].value = xstrdup(p); 692 tev->args[i].value = xstrdup(p);
584 } 693 }
585 694 ret = 0;
695out:
586 argv_free(argv); 696 argv_free(argv);
697 return ret;
587} 698}
588 699
589/* Compose only probe arg */ 700/* Compose only probe arg */
@@ -622,7 +733,9 @@ int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf, size_t len)
622 733
623 return tmp - buf; 734 return tmp - buf;
624error: 735error:
625 die("Failed to synthesize perf probe argument: %s", strerror(-ret)); 736 pr_debug("Failed to synthesize perf probe argument: %s",
737 strerror(-ret));
738 return ret;
626} 739}
627 740
628/* Compose only probe point (not argument) */ 741/* Compose only probe point (not argument) */
@@ -666,7 +779,10 @@ static char *synthesize_perf_probe_point(struct perf_probe_point *pp)
666 779
667 return buf; 780 return buf;
668error: 781error:
669 die("Failed to synthesize perf probe point: %s", strerror(-ret)); 782 pr_debug("Failed to synthesize perf probe point: %s",
783 strerror(-ret));
784 free(buf);
785 return NULL;
670} 786}
671 787
672#if 0 788#if 0
@@ -796,29 +912,37 @@ error:
796 return NULL; 912 return NULL;
797} 913}
798 914
799void convert_to_perf_probe_event(struct kprobe_trace_event *tev, 915int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
800 struct perf_probe_event *pev) 916 struct perf_probe_event *pev)
801{ 917{
802 char buf[64]; 918 char buf[64];
803 int i; 919 int i, ret;
804 920
805 /* Convert event/group name */ 921 /* Convert event/group name */
806 pev->event = xstrdup(tev->event); 922 pev->event = xstrdup(tev->event);
807 pev->group = xstrdup(tev->group); 923 pev->group = xstrdup(tev->group);
808 924
809 /* Convert trace_point to probe_point */ 925 /* Convert trace_point to probe_point */
810 convert_to_perf_probe_point(&tev->point, &pev->point); 926 ret = convert_to_perf_probe_point(&tev->point, &pev->point);
927 if (ret < 0)
928 return ret;
811 929
812 /* Convert trace_arg to probe_arg */ 930 /* Convert trace_arg to probe_arg */
813 pev->nargs = tev->nargs; 931 pev->nargs = tev->nargs;
814 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs); 932 pev->args = xzalloc(sizeof(struct perf_probe_arg) * pev->nargs);
815 for (i = 0; i < tev->nargs; i++) 933 for (i = 0; i < tev->nargs && ret >= 0; i++)
816 if (tev->args[i].name) 934 if (tev->args[i].name)
817 pev->args[i].name = xstrdup(tev->args[i].name); 935 pev->args[i].name = xstrdup(tev->args[i].name);
818 else { 936 else {
819 synthesize_kprobe_trace_arg(&tev->args[i], buf, 64); 937 ret = synthesize_kprobe_trace_arg(&tev->args[i],
938 buf, 64);
820 pev->args[i].name = xstrdup(buf); 939 pev->args[i].name = xstrdup(buf);
821 } 940 }
941
942 if (ret < 0)
943 clear_perf_probe_event(pev);
944
945 return ret;
822} 946}
823 947
824void clear_perf_probe_event(struct perf_probe_event *pev) 948void clear_perf_probe_event(struct perf_probe_event *pev)
@@ -894,21 +1018,20 @@ static int open_kprobe_events(bool readwrite)
894 int ret; 1018 int ret;
895 1019
896 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path); 1020 ret = e_snprintf(buf, PATH_MAX, "%s/../kprobe_events", debugfs_path);
897 if (ret < 0) 1021 if (ret >= 0) {
898 die("Failed to make kprobe_events path."); 1022 if (readwrite && !probe_event_dry_run)
899 1023 ret = open(buf, O_RDWR, O_APPEND);
900 if (readwrite && !probe_event_dry_run) 1024 else
901 ret = open(buf, O_RDWR, O_APPEND); 1025 ret = open(buf, O_RDONLY, 0);
902 else 1026 }
903 ret = open(buf, O_RDONLY, 0);
904 1027
905 if (ret < 0) { 1028 if (ret < 0) {
906 if (errno == ENOENT) 1029 if (errno == ENOENT)
907 die("kprobe_events file does not exist -" 1030 pr_warning("kprobe_events file does not exist - please"
908 " please rebuild with CONFIG_KPROBE_EVENT."); 1031 " rebuild kernel with CONFIG_KPROBE_EVENT.\n");
909 else 1032 else
910 die("Could not open kprobe_events file: %s", 1033 pr_warning("Failed to open kprobe_events file: %s\n",
911 strerror(errno)); 1034 strerror(errno));
912 } 1035 }
913 return ret; 1036 return ret;
914} 1037}
@@ -934,8 +1057,11 @@ static struct strlist *get_kprobe_trace_command_rawlist(int fd)
934 if (p[idx] == '\n') 1057 if (p[idx] == '\n')
935 p[idx] = '\0'; 1058 p[idx] = '\0';
936 ret = strlist__add(sl, buf); 1059 ret = strlist__add(sl, buf);
937 if (ret < 0) 1060 if (ret < 0) {
938 die("strlist__add failed: %s", strerror(-ret)); 1061 pr_debug("strlist__add failed: %s\n", strerror(-ret));
1062 strlist__delete(sl);
1063 return NULL;
1064 }
939 } 1065 }
940 fclose(fp); 1066 fclose(fp);
941 1067
@@ -943,7 +1069,7 @@ static struct strlist *get_kprobe_trace_command_rawlist(int fd)
943} 1069}
944 1070
945/* Show an event */ 1071/* Show an event */
946static void show_perf_probe_event(struct perf_probe_event *pev) 1072static int show_perf_probe_event(struct perf_probe_event *pev)
947{ 1073{
948 int i, ret; 1074 int i, ret;
949 char buf[128]; 1075 char buf[128];
@@ -951,52 +1077,71 @@ static void show_perf_probe_event(struct perf_probe_event *pev)
951 1077
952 /* Synthesize only event probe point */ 1078 /* Synthesize only event probe point */
953 place = synthesize_perf_probe_point(&pev->point); 1079 place = synthesize_perf_probe_point(&pev->point);
1080 if (!place)
1081 return -EINVAL;
954 1082
955 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event); 1083 ret = e_snprintf(buf, 128, "%s:%s", pev->group, pev->event);
956 if (ret < 0) 1084 if (ret < 0)
957 die("Failed to copy event: %s", strerror(-ret)); 1085 return ret;
1086
958 printf(" %-20s (on %s", buf, place); 1087 printf(" %-20s (on %s", buf, place);
959 1088
960 if (pev->nargs > 0) { 1089 if (pev->nargs > 0) {
961 printf(" with"); 1090 printf(" with");
962 for (i = 0; i < pev->nargs; i++) { 1091 for (i = 0; i < pev->nargs; i++) {
963 synthesize_perf_probe_arg(&pev->args[i], buf, 128); 1092 ret = synthesize_perf_probe_arg(&pev->args[i],
1093 buf, 128);
1094 if (ret < 0)
1095 break;
964 printf(" %s", buf); 1096 printf(" %s", buf);
965 } 1097 }
966 } 1098 }
967 printf(")\n"); 1099 printf(")\n");
968 free(place); 1100 free(place);
1101 return ret;
969} 1102}
970 1103
971/* List up current perf-probe events */ 1104/* List up current perf-probe events */
972void show_perf_probe_events(void) 1105int show_perf_probe_events(void)
973{ 1106{
974 int fd; 1107 int fd, ret;
975 struct kprobe_trace_event tev; 1108 struct kprobe_trace_event tev;
976 struct perf_probe_event pev; 1109 struct perf_probe_event pev;
977 struct strlist *rawlist; 1110 struct strlist *rawlist;
978 struct str_node *ent; 1111 struct str_node *ent;
979 1112
980 setup_pager(); 1113 setup_pager();
981 init_vmlinux(); 1114 ret = init_vmlinux();
1115 if (ret < 0)
1116 return ret;
982 1117
983 memset(&tev, 0, sizeof(tev)); 1118 memset(&tev, 0, sizeof(tev));
984 memset(&pev, 0, sizeof(pev)); 1119 memset(&pev, 0, sizeof(pev));
985 1120
986 fd = open_kprobe_events(false); 1121 fd = open_kprobe_events(false);
1122 if (fd < 0)
1123 return fd;
1124
987 rawlist = get_kprobe_trace_command_rawlist(fd); 1125 rawlist = get_kprobe_trace_command_rawlist(fd);
988 close(fd); 1126 close(fd);
1127 if (!rawlist)
1128 return -ENOENT;
989 1129
990 strlist__for_each(ent, rawlist) { 1130 strlist__for_each(ent, rawlist) {
991 parse_kprobe_trace_command(ent->s, &tev); 1131 ret = parse_kprobe_trace_command(ent->s, &tev);
992 convert_to_perf_probe_event(&tev, &pev); 1132 if (ret >= 0) {
993 /* Show an event */ 1133 ret = convert_to_perf_probe_event(&tev, &pev);
994 show_perf_probe_event(&pev); 1134 if (ret >= 0)
1135 ret = show_perf_probe_event(&pev);
1136 }
995 clear_perf_probe_event(&pev); 1137 clear_perf_probe_event(&pev);
996 clear_kprobe_trace_event(&tev); 1138 clear_kprobe_trace_event(&tev);
1139 if (ret < 0)
1140 break;
997 } 1141 }
998
999 strlist__delete(rawlist); 1142 strlist__delete(rawlist);
1143
1144 return ret;
1000} 1145}
1001 1146
1002/* Get current perf-probe event names */ 1147/* Get current perf-probe event names */
@@ -1006,88 +1151,118 @@ static struct strlist *get_kprobe_trace_event_names(int fd, bool include_group)
1006 struct strlist *sl, *rawlist; 1151 struct strlist *sl, *rawlist;
1007 struct str_node *ent; 1152 struct str_node *ent;
1008 struct kprobe_trace_event tev; 1153 struct kprobe_trace_event tev;
1154 int ret = 0;
1009 1155
1010 memset(&tev, 0, sizeof(tev)); 1156 memset(&tev, 0, sizeof(tev));
1011 1157
1012 rawlist = get_kprobe_trace_command_rawlist(fd); 1158 rawlist = get_kprobe_trace_command_rawlist(fd);
1013 sl = strlist__new(true, NULL); 1159 sl = strlist__new(true, NULL);
1014 strlist__for_each(ent, rawlist) { 1160 strlist__for_each(ent, rawlist) {
1015 parse_kprobe_trace_command(ent->s, &tev); 1161 ret = parse_kprobe_trace_command(ent->s, &tev);
1162 if (ret < 0)
1163 break;
1016 if (include_group) { 1164 if (include_group) {
1017 if (e_snprintf(buf, 128, "%s:%s", tev.group, 1165 ret = e_snprintf(buf, 128, "%s:%s", tev.group,
1018 tev.event) < 0) 1166 tev.event);
1019 die("Failed to copy group:event name."); 1167 if (ret >= 0)
1020 strlist__add(sl, buf); 1168 ret = strlist__add(sl, buf);
1021 } else 1169 } else
1022 strlist__add(sl, tev.event); 1170 ret = strlist__add(sl, tev.event);
1023 clear_kprobe_trace_event(&tev); 1171 clear_kprobe_trace_event(&tev);
1172 if (ret < 0)
1173 break;
1024 } 1174 }
1025
1026 strlist__delete(rawlist); 1175 strlist__delete(rawlist);
1027 1176
1177 if (ret < 0) {
1178 strlist__delete(sl);
1179 return NULL;
1180 }
1028 return sl; 1181 return sl;
1029} 1182}
1030 1183
1031static void write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev) 1184static int write_kprobe_trace_event(int fd, struct kprobe_trace_event *tev)
1032{ 1185{
1033 int ret; 1186 int ret;
1034 char *buf = synthesize_kprobe_trace_command(tev); 1187 char *buf = synthesize_kprobe_trace_command(tev);
1035 1188
1189 if (!buf) {
1190 pr_debug("Failed to synthesize kprobe trace event.\n");
1191 return -EINVAL;
1192 }
1193
1036 pr_debug("Writing event: %s\n", buf); 1194 pr_debug("Writing event: %s\n", buf);
1037 if (!probe_event_dry_run) { 1195 if (!probe_event_dry_run) {
1038 ret = write(fd, buf, strlen(buf)); 1196 ret = write(fd, buf, strlen(buf));
1039 if (ret <= 0) 1197 if (ret <= 0)
1040 die("Failed to write event: %s", strerror(errno)); 1198 pr_warning("Failed to write event: %s\n",
1199 strerror(errno));
1041 } 1200 }
1042 free(buf); 1201 free(buf);
1202 return ret;
1043} 1203}
1044 1204
1045static void get_new_event_name(char *buf, size_t len, const char *base, 1205static int get_new_event_name(char *buf, size_t len, const char *base,
1046 struct strlist *namelist, bool allow_suffix) 1206 struct strlist *namelist, bool allow_suffix)
1047{ 1207{
1048 int i, ret; 1208 int i, ret;
1049 1209
1050 /* Try no suffix */ 1210 /* Try no suffix */
1051 ret = e_snprintf(buf, len, "%s", base); 1211 ret = e_snprintf(buf, len, "%s", base);
1052 if (ret < 0) 1212 if (ret < 0) {
1053 die("snprintf() failed: %s", strerror(-ret)); 1213 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1214 return ret;
1215 }
1054 if (!strlist__has_entry(namelist, buf)) 1216 if (!strlist__has_entry(namelist, buf))
1055 return; 1217 return 0;
1056 1218
1057 if (!allow_suffix) { 1219 if (!allow_suffix) {
1058 pr_warning("Error: event \"%s\" already exists. " 1220 pr_warning("Error: event \"%s\" already exists. "
1059 "(Use -f to force duplicates.)\n", base); 1221 "(Use -f to force duplicates.)\n", base);
1060 die("Can't add new event."); 1222 return -EEXIST;
1061 } 1223 }
1062 1224
1063 /* Try to add suffix */ 1225 /* Try to add suffix */
1064 for (i = 1; i < MAX_EVENT_INDEX; i++) { 1226 for (i = 1; i < MAX_EVENT_INDEX; i++) {
1065 ret = e_snprintf(buf, len, "%s_%d", base, i); 1227 ret = e_snprintf(buf, len, "%s_%d", base, i);
1066 if (ret < 0) 1228 if (ret < 0) {
1067 die("snprintf() failed: %s", strerror(-ret)); 1229 pr_debug("snprintf() failed: %s\n", strerror(-ret));
1230 return ret;
1231 }
1068 if (!strlist__has_entry(namelist, buf)) 1232 if (!strlist__has_entry(namelist, buf))
1069 break; 1233 break;
1070 } 1234 }
1071 if (i == MAX_EVENT_INDEX) 1235 if (i == MAX_EVENT_INDEX) {
1072 die("Too many events are on the same function."); 1236 pr_warning("Too many events are on the same function.\n");
1237 ret = -ERANGE;
1238 }
1239
1240 return ret;
1073} 1241}
1074 1242
1075static void __add_kprobe_trace_events(struct perf_probe_event *pev, 1243static int __add_kprobe_trace_events(struct perf_probe_event *pev,
1076 struct kprobe_trace_event *tevs, 1244 struct kprobe_trace_event *tevs,
1077 int ntevs, bool allow_suffix) 1245 int ntevs, bool allow_suffix)
1078{ 1246{
1079 int i, fd; 1247 int i, fd, ret;
1080 struct kprobe_trace_event *tev = NULL; 1248 struct kprobe_trace_event *tev = NULL;
1081 char buf[64]; 1249 char buf[64];
1082 const char *event, *group; 1250 const char *event, *group;
1083 struct strlist *namelist; 1251 struct strlist *namelist;
1084 1252
1085 fd = open_kprobe_events(true); 1253 fd = open_kprobe_events(true);
1254 if (fd < 0)
1255 return fd;
1086 /* Get current event names */ 1256 /* Get current event names */
1087 namelist = get_kprobe_trace_event_names(fd, false); 1257 namelist = get_kprobe_trace_event_names(fd, false);
1258 if (!namelist) {
1259 pr_debug("Failed to get current event list.\n");
1260 return -EIO;
1261 }
1088 1262
1263 ret = 0;
1089 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":"); 1264 printf("Add new event%s\n", (ntevs > 1) ? "s:" : ":");
1090 for (i = 0; i < ntevs; i++) { 1265 for (i = 0; i < ntevs && ret >= 0; i++) {
1091 tev = &tevs[i]; 1266 tev = &tevs[i];
1092 if (pev->event) 1267 if (pev->event)
1093 event = pev->event; 1268 event = pev->event;
@@ -1102,12 +1277,17 @@ static void __add_kprobe_trace_events(struct perf_probe_event *pev,
1102 group = PERFPROBE_GROUP; 1277 group = PERFPROBE_GROUP;
1103 1278
1104 /* Get an unused new event name */ 1279 /* Get an unused new event name */
1105 get_new_event_name(buf, 64, event, namelist, allow_suffix); 1280 ret = get_new_event_name(buf, 64, event,
1281 namelist, allow_suffix);
1282 if (ret < 0)
1283 break;
1106 event = buf; 1284 event = buf;
1107 1285
1108 tev->event = xstrdup(event); 1286 tev->event = xstrdup(event);
1109 tev->group = xstrdup(group); 1287 tev->group = xstrdup(group);
1110 write_kprobe_trace_event(fd, tev); 1288 ret = write_kprobe_trace_event(fd, tev);
1289 if (ret < 0)
1290 break;
1111 /* Add added event name to namelist */ 1291 /* Add added event name to namelist */
1112 strlist__add(namelist, event); 1292 strlist__add(namelist, event);
1113 1293
@@ -1129,12 +1309,17 @@ static void __add_kprobe_trace_events(struct perf_probe_event *pev,
1129 */ 1309 */
1130 allow_suffix = true; 1310 allow_suffix = true;
1131 } 1311 }
1132 /* Show how to use the event. */ 1312
1133 printf("\nYou can now use it on all perf tools, such as:\n\n"); 1313 if (ret >= 0) {
1134 printf("\tperf record -e %s:%s -a sleep 1\n\n", tev->group, tev->event); 1314 /* Show how to use the event. */
1315 printf("\nYou can now use it on all perf tools, such as:\n\n");
1316 printf("\tperf record -e %s:%s -aR sleep 1\n\n", tev->group,
1317 tev->event);
1318 }
1135 1319
1136 strlist__delete(namelist); 1320 strlist__delete(namelist);
1137 close(fd); 1321 close(fd);
1322 return ret;
1138} 1323}
1139 1324
1140static int convert_to_kprobe_trace_events(struct perf_probe_event *pev, 1325static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
@@ -1146,7 +1331,7 @@ static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1146 1331
1147 /* Convert perf_probe_event with debuginfo */ 1332 /* Convert perf_probe_event with debuginfo */
1148 ntevs = try_to_find_kprobe_trace_events(pev, tevs); 1333 ntevs = try_to_find_kprobe_trace_events(pev, tevs);
1149 if (ntevs > 0) 1334 if (ntevs != 0)
1150 return ntevs; 1335 return ntevs;
1151 1336
1152 /* Allocate trace event buffer */ 1337 /* Allocate trace event buffer */
@@ -1172,10 +1357,11 @@ static int convert_to_kprobe_trace_events(struct perf_probe_event *pev,
1172 /* Currently just checking function name from symbol map */ 1357 /* Currently just checking function name from symbol map */
1173 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION], 1358 sym = map__find_symbol_by_name(kmaps[MAP__FUNCTION],
1174 tev->point.symbol, NULL); 1359 tev->point.symbol, NULL);
1175 if (!sym) 1360 if (!sym) {
1176 die("Kernel symbol \'%s\' not found - probe not added.", 1361 pr_warning("Kernel symbol \'%s\' not found.\n",
1177 tev->point.symbol); 1362 tev->point.symbol);
1178 1363 return -ENOENT;
1364 }
1179 return ntevs; 1365 return ntevs;
1180} 1366}
1181 1367
@@ -1185,93 +1371,128 @@ struct __event_package {
1185 int ntevs; 1371 int ntevs;
1186}; 1372};
1187 1373
1188void add_perf_probe_events(struct perf_probe_event *pevs, int npevs, 1374int add_perf_probe_events(struct perf_probe_event *pevs, int npevs,
1189 bool force_add) 1375 bool force_add)
1190{ 1376{
1191 int i; 1377 int i, j, ret;
1192 struct __event_package *pkgs; 1378 struct __event_package *pkgs;
1193 1379
1194 pkgs = xzalloc(sizeof(struct __event_package) * npevs); 1380 pkgs = xzalloc(sizeof(struct __event_package) * npevs);
1195 1381
1196 /* Init vmlinux path */ 1382 /* Init vmlinux path */
1197 init_vmlinux(); 1383 ret = init_vmlinux();
1384 if (ret < 0)
1385 return ret;
1198 1386
1199 /* Loop 1: convert all events */ 1387 /* Loop 1: convert all events */
1200 for (i = 0; i < npevs; i++) { 1388 for (i = 0; i < npevs; i++) {
1201 pkgs[i].pev = &pevs[i]; 1389 pkgs[i].pev = &pevs[i];
1202 /* Convert with or without debuginfo */ 1390 /* Convert with or without debuginfo */
1203 pkgs[i].ntevs = convert_to_kprobe_trace_events(pkgs[i].pev, 1391 ret = convert_to_kprobe_trace_events(pkgs[i].pev,
1204 &pkgs[i].tevs); 1392 &pkgs[i].tevs);
1393 if (ret < 0)
1394 goto end;
1395 pkgs[i].ntevs = ret;
1205 } 1396 }
1206 1397
1207 /* Loop 2: add all events */ 1398 /* Loop 2: add all events */
1399 for (i = 0; i < npevs && ret >= 0; i++)
1400 ret = __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs,
1401 pkgs[i].ntevs, force_add);
1402end:
1403 /* Loop 3: cleanup trace events */
1208 for (i = 0; i < npevs; i++) 1404 for (i = 0; i < npevs; i++)
1209 __add_kprobe_trace_events(pkgs[i].pev, pkgs[i].tevs, 1405 for (j = 0; j < pkgs[i].ntevs; j++)
1210 pkgs[i].ntevs, force_add); 1406 clear_kprobe_trace_event(&pkgs[i].tevs[j]);
1211 /* TODO: cleanup all trace events? */ 1407
1408 return ret;
1212} 1409}
1213 1410
1214static void __del_trace_kprobe_event(int fd, struct str_node *ent) 1411static int __del_trace_kprobe_event(int fd, struct str_node *ent)
1215{ 1412{
1216 char *p; 1413 char *p;
1217 char buf[128]; 1414 char buf[128];
1218 int ret; 1415 int ret;
1219 1416
1220 /* Convert from perf-probe event to trace-kprobe event */ 1417 /* Convert from perf-probe event to trace-kprobe event */
1221 if (e_snprintf(buf, 128, "-:%s", ent->s) < 0) 1418 ret = e_snprintf(buf, 128, "-:%s", ent->s);
1222 die("Failed to copy event."); 1419 if (ret < 0)
1420 goto error;
1421
1223 p = strchr(buf + 2, ':'); 1422 p = strchr(buf + 2, ':');
1224 if (!p) 1423 if (!p) {
1225 die("Internal error: %s should have ':' but not.", ent->s); 1424 pr_debug("Internal error: %s should have ':' but not.\n",
1425 ent->s);
1426 ret = -ENOTSUP;
1427 goto error;
1428 }
1226 *p = '/'; 1429 *p = '/';
1227 1430
1228 pr_debug("Writing event: %s\n", buf); 1431 pr_debug("Writing event: %s\n", buf);
1229 ret = write(fd, buf, strlen(buf)); 1432 ret = write(fd, buf, strlen(buf));
1230 if (ret <= 0) 1433 if (ret < 0)
1231 die("Failed to write event: %s", strerror(errno)); 1434 goto error;
1435
1232 printf("Remove event: %s\n", ent->s); 1436 printf("Remove event: %s\n", ent->s);
1437 return 0;
1438error:
1439 pr_warning("Failed to delete event: %s\n", strerror(-ret));
1440 return ret;
1233} 1441}
1234 1442
1235static void del_trace_kprobe_event(int fd, const char *group, 1443static int del_trace_kprobe_event(int fd, const char *group,
1236 const char *event, struct strlist *namelist) 1444 const char *event, struct strlist *namelist)
1237{ 1445{
1238 char buf[128]; 1446 char buf[128];
1239 struct str_node *ent, *n; 1447 struct str_node *ent, *n;
1240 int found = 0; 1448 int found = 0, ret = 0;
1241 1449
1242 if (e_snprintf(buf, 128, "%s:%s", group, event) < 0) 1450 ret = e_snprintf(buf, 128, "%s:%s", group, event);
1243 die("Failed to copy event."); 1451 if (ret < 0) {
1452 pr_err("Failed to copy event.");
1453 return ret;
1454 }
1244 1455
1245 if (strpbrk(buf, "*?")) { /* Glob-exp */ 1456 if (strpbrk(buf, "*?")) { /* Glob-exp */
1246 strlist__for_each_safe(ent, n, namelist) 1457 strlist__for_each_safe(ent, n, namelist)
1247 if (strglobmatch(ent->s, buf)) { 1458 if (strglobmatch(ent->s, buf)) {
1248 found++; 1459 found++;
1249 __del_trace_kprobe_event(fd, ent); 1460 ret = __del_trace_kprobe_event(fd, ent);
1461 if (ret < 0)
1462 break;
1250 strlist__remove(namelist, ent); 1463 strlist__remove(namelist, ent);
1251 } 1464 }
1252 } else { 1465 } else {
1253 ent = strlist__find(namelist, buf); 1466 ent = strlist__find(namelist, buf);
1254 if (ent) { 1467 if (ent) {
1255 found++; 1468 found++;
1256 __del_trace_kprobe_event(fd, ent); 1469 ret = __del_trace_kprobe_event(fd, ent);
1257 strlist__remove(namelist, ent); 1470 if (ret >= 0)
1471 strlist__remove(namelist, ent);
1258 } 1472 }
1259 } 1473 }
1260 if (found == 0) 1474 if (found == 0 && ret >= 0)
1261 pr_info("Info: event \"%s\" does not exist, could not remove it.\n", buf); 1475 pr_info("Info: Event \"%s\" does not exist.\n", buf);
1476
1477 return ret;
1262} 1478}
1263 1479
1264void del_perf_probe_events(struct strlist *dellist) 1480int del_perf_probe_events(struct strlist *dellist)
1265{ 1481{
1266 int fd; 1482 int fd, ret = 0;
1267 const char *group, *event; 1483 const char *group, *event;
1268 char *p, *str; 1484 char *p, *str;
1269 struct str_node *ent; 1485 struct str_node *ent;
1270 struct strlist *namelist; 1486 struct strlist *namelist;
1271 1487
1272 fd = open_kprobe_events(true); 1488 fd = open_kprobe_events(true);
1489 if (fd < 0)
1490 return fd;
1491
1273 /* Get current event names */ 1492 /* Get current event names */
1274 namelist = get_kprobe_trace_event_names(fd, true); 1493 namelist = get_kprobe_trace_event_names(fd, true);
1494 if (namelist == NULL)
1495 return -EINVAL;
1275 1496
1276 strlist__for_each(ent, dellist) { 1497 strlist__for_each(ent, dellist) {
1277 str = xstrdup(ent->s); 1498 str = xstrdup(ent->s);
@@ -1286,10 +1507,14 @@ void del_perf_probe_events(struct strlist *dellist)
1286 event = str; 1507 event = str;
1287 } 1508 }
1288 pr_debug("Group: %s, Event: %s\n", group, event); 1509 pr_debug("Group: %s, Event: %s\n", group, event);
1289 del_trace_kprobe_event(fd, group, event, namelist); 1510 ret = del_trace_kprobe_event(fd, group, event, namelist);
1290 free(str); 1511 free(str);
1512 if (ret < 0)
1513 break;
1291 } 1514 }
1292 strlist__delete(namelist); 1515 strlist__delete(namelist);
1293 close(fd); 1516 close(fd);
1517
1518 return ret;
1294} 1519}
1295 1520
diff --git a/tools/perf/util/probe-event.h b/tools/perf/util/probe-event.h
index ff2f26b1822c..ab5492901708 100644
--- a/tools/perf/util/probe-event.h
+++ b/tools/perf/util/probe-event.h
@@ -89,10 +89,10 @@ struct line_range {
89}; 89};
90 90
91/* Command string to events */ 91/* Command string to events */
92extern void parse_perf_probe_command(const char *cmd, 92extern int parse_perf_probe_command(const char *cmd,
93 struct perf_probe_event *pev); 93 struct perf_probe_event *pev);
94extern void parse_kprobe_trace_command(const char *cmd, 94extern int parse_kprobe_trace_command(const char *cmd,
95 struct kprobe_trace_event *tev); 95 struct kprobe_trace_event *tev);
96 96
97/* Events to command string */ 97/* Events to command string */
98extern char *synthesize_perf_probe_command(struct perf_probe_event *pev); 98extern char *synthesize_perf_probe_command(struct perf_probe_event *pev);
@@ -104,22 +104,22 @@ extern int synthesize_perf_probe_arg(struct perf_probe_arg *pa, char *buf,
104extern bool perf_probe_event_need_dwarf(struct perf_probe_event *pev); 104extern bool perf_probe_event_need_dwarf(struct perf_probe_event *pev);
105 105
106/* Convert from kprobe_trace_event to perf_probe_event */ 106/* Convert from kprobe_trace_event to perf_probe_event */
107extern void convert_to_perf_probe_event(struct kprobe_trace_event *tev, 107extern int convert_to_perf_probe_event(struct kprobe_trace_event *tev,
108 struct perf_probe_event *pev); 108 struct perf_probe_event *pev);
109 109
110/* Release event contents */ 110/* Release event contents */
111extern void clear_perf_probe_event(struct perf_probe_event *pev); 111extern void clear_perf_probe_event(struct perf_probe_event *pev);
112extern void clear_kprobe_trace_event(struct kprobe_trace_event *tev); 112extern void clear_kprobe_trace_event(struct kprobe_trace_event *tev);
113 113
114/* Command string to line-range */ 114/* Command string to line-range */
115extern void parse_line_range_desc(const char *cmd, struct line_range *lr); 115extern int parse_line_range_desc(const char *cmd, struct line_range *lr);
116 116
117 117
118extern void add_perf_probe_events(struct perf_probe_event *pevs, int ntevs, 118extern int add_perf_probe_events(struct perf_probe_event *pevs, int ntevs,
119 bool force_add); 119 bool force_add);
120extern void del_perf_probe_events(struct strlist *dellist); 120extern int del_perf_probe_events(struct strlist *dellist);
121extern void show_perf_probe_events(void); 121extern int show_perf_probe_events(void);
122extern void show_line_range(struct line_range *lr); 122extern int show_line_range(struct line_range *lr);
123 123
124 124
125/* Maximum index number of event-name postfix */ 125/* Maximum index number of event-name postfix */