perf report: Fix leak of resolved callchains array on error path
[safe/jmp/linux-2.6] / tools / perf / builtin-report.c
1 /*
2  * builtin-report.c
3  *
4  * Builtin report command: Analyze the perf.data input file,
5  * look up and read DSOs and symbol information and display
6  * a histogram of results, along various sorting keys.
7  */
8 #include "builtin.h"
9
10 #include "util/util.h"
11
12 #include "util/color.h"
13 #include <linux/list.h>
14 #include "util/cache.h"
15 #include <linux/rbtree.h>
16 #include "util/symbol.h"
17 #include "util/callchain.h"
18 #include "util/strlist.h"
19 #include "util/values.h"
20
21 #include "perf.h"
22 #include "util/debug.h"
23 #include "util/header.h"
24 #include "util/session.h"
25
26 #include "util/parse-options.h"
27 #include "util/parse-events.h"
28
29 #include "util/thread.h"
30 #include "util/sort.h"
31 #include "util/hist.h"
32
33 static char             const *input_name = "perf.data";
34
35 static bool             force;
36 static bool             hide_unresolved;
37 static bool             dont_use_callchains;
38
39 static bool             show_threads;
40 static struct perf_read_values  show_threads_values;
41
42 static char             default_pretty_printing_style[] = "normal";
43 static char             *pretty_printing_style = default_pretty_printing_style;
44
45 static char             callchain_default_opt[] = "fractal,0.5";
46
47 static struct event_stat_id *get_stats(struct perf_session *self,
48                                        u64 event_stream, u32 type, u64 config)
49 {
50         struct rb_node **p = &self->stats_by_id.rb_node;
51         struct rb_node *parent = NULL;
52         struct event_stat_id *iter, *new;
53
54         while (*p != NULL) {
55                 parent = *p;
56                 iter = rb_entry(parent, struct event_stat_id, rb_node);
57                 if (iter->config == config)
58                         return iter;
59
60
61                 if (config > iter->config)
62                         p = &(*p)->rb_right;
63                 else
64                         p = &(*p)->rb_left;
65         }
66
67         new = malloc(sizeof(struct event_stat_id));
68         if (new == NULL)
69                 return NULL;
70         memset(new, 0, sizeof(struct event_stat_id));
71         new->event_stream = event_stream;
72         new->config = config;
73         new->type = type;
74         rb_link_node(&new->rb_node, parent, p);
75         rb_insert_color(&new->rb_node, &self->stats_by_id);
76         return new;
77 }
78
79 static int perf_session__add_hist_entry(struct perf_session *self,
80                                         struct addr_location *al,
81                                         struct sample_data *data)
82 {
83         struct map_symbol *syms = NULL;
84         struct symbol *parent = NULL;
85         bool hit;
86         int err = -ENOMEM;
87         struct hist_entry *he;
88         struct event_stat_id *stats;
89         struct perf_event_attr *attr;
90
91         if ((sort__has_parent || symbol_conf.use_callchain) && data->callchain) {
92                 syms = perf_session__resolve_callchain(self, al->thread,
93                                                        data->callchain, &parent);
94                 if (syms == NULL)
95                         return -ENOMEM;
96         }
97
98         attr = perf_header__find_attr(data->id, &self->header);
99         if (attr)
100                 stats = get_stats(self, data->id, attr->type, attr->config);
101         else
102                 stats = get_stats(self, data->id, 0, 0);
103         if (stats == NULL)
104                 goto out_free_syms;
105         he = __perf_session__add_hist_entry(&stats->hists, al, parent,
106                                             data->period, &hit);
107         if (he == NULL)
108                 goto out_free_syms;
109
110         if (hit)
111                 __perf_session__add_count(he, al,  data->period);
112
113         err = 0;
114         if (symbol_conf.use_callchain) {
115                 if (!hit)
116                         callchain_init(he->callchain);
117                 err = append_chain(he->callchain, data->callchain, syms);
118         }
119 out_free_syms:
120         free(syms);
121         return err;
122 }
123
124 static int add_event_total(struct perf_session *session,
125                            struct sample_data *data,
126                            struct perf_event_attr *attr)
127 {
128         struct event_stat_id *stats;
129
130         if (attr)
131                 stats = get_stats(session, data->id, attr->type, attr->config);
132         else
133                 stats = get_stats(session, data->id, 0, 0);
134
135         if (!stats)
136                 return -ENOMEM;
137
138         stats->stats.total += data->period;
139         session->events_stats.total += data->period;
140         return 0;
141 }
142
143 static int process_sample_event(event_t *event, struct perf_session *session)
144 {
145         struct sample_data data = { .period = 1, };
146         struct addr_location al;
147         struct perf_event_attr *attr;
148
149         event__parse_sample(event, session->sample_type, &data);
150
151         dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc,
152                     data.pid, data.tid, data.ip, data.period);
153
154         if (session->sample_type & PERF_SAMPLE_CALLCHAIN) {
155                 unsigned int i;
156
157                 dump_printf("... chain: nr:%Lu\n", data.callchain->nr);
158
159                 if (!ip_callchain__valid(data.callchain, event)) {
160                         pr_debug("call-chain problem with event, "
161                                  "skipping it.\n");
162                         return 0;
163                 }
164
165                 if (dump_trace) {
166                         for (i = 0; i < data.callchain->nr; i++)
167                                 dump_printf("..... %2d: %016Lx\n",
168                                             i, data.callchain->ips[i]);
169                 }
170         }
171
172         if (event__preprocess_sample(event, session, &al, NULL) < 0) {
173                 fprintf(stderr, "problem processing %d event, skipping it.\n",
174                         event->header.type);
175                 return -1;
176         }
177
178         if (al.filtered || (hide_unresolved && al.sym == NULL))
179                 return 0;
180
181         if (perf_session__add_hist_entry(session, &al, &data)) {
182                 pr_debug("problem incrementing symbol count, skipping event\n");
183                 return -1;
184         }
185
186         attr = perf_header__find_attr(data.id, &session->header);
187
188         if (add_event_total(session, &data, attr)) {
189                 pr_debug("problem adding event count\n");
190                 return -1;
191         }
192
193         return 0;
194 }
195
196 static int process_read_event(event_t *event, struct perf_session *session __used)
197 {
198         struct perf_event_attr *attr;
199
200         attr = perf_header__find_attr(event->read.id, &session->header);
201
202         if (show_threads) {
203                 const char *name = attr ? __event_name(attr->type, attr->config)
204                                    : "unknown";
205                 perf_read_values_add_value(&show_threads_values,
206                                            event->read.pid, event->read.tid,
207                                            event->read.id,
208                                            name,
209                                            event->read.value);
210         }
211
212         dump_printf(": %d %d %s %Lu\n", event->read.pid, event->read.tid,
213                     attr ? __event_name(attr->type, attr->config) : "FAIL",
214                     event->read.value);
215
216         return 0;
217 }
218
219 static int perf_session__setup_sample_type(struct perf_session *self)
220 {
221         if (!(self->sample_type & PERF_SAMPLE_CALLCHAIN)) {
222                 if (sort__has_parent) {
223                         fprintf(stderr, "selected --sort parent, but no"
224                                         " callchain data. Did you call"
225                                         " perf record without -g?\n");
226                         return -EINVAL;
227                 }
228                 if (symbol_conf.use_callchain) {
229                         fprintf(stderr, "selected -g but no callchain data."
230                                         " Did you call perf record without"
231                                         " -g?\n");
232                         return -1;
233                 }
234         } else if (!dont_use_callchains && callchain_param.mode != CHAIN_NONE &&
235                    !symbol_conf.use_callchain) {
236                         symbol_conf.use_callchain = true;
237                         if (register_callchain_param(&callchain_param) < 0) {
238                                 fprintf(stderr, "Can't register callchain"
239                                                 " params\n");
240                                 return -EINVAL;
241                         }
242         }
243
244         return 0;
245 }
246
247 static struct perf_event_ops event_ops = {
248         .sample = process_sample_event,
249         .mmap   = event__process_mmap,
250         .comm   = event__process_comm,
251         .exit   = event__process_task,
252         .fork   = event__process_task,
253         .lost   = event__process_lost,
254         .read   = process_read_event,
255         .attr   = event__process_attr,
256         .event_type = event__process_event_type,
257         .tracing_data = event__process_tracing_data,
258         .build_id = event__process_build_id,
259 };
260
261 extern volatile int session_done;
262
263 static void sig_handler(int sig __attribute__((__unused__)))
264 {
265         session_done = 1;
266 }
267
268 static int __cmd_report(void)
269 {
270         int ret = -EINVAL;
271         struct perf_session *session;
272         struct rb_node *next;
273         const char *help = "For a higher level overview, try: perf report --sort comm,dso";
274
275         signal(SIGINT, sig_handler);
276
277         session = perf_session__new(input_name, O_RDONLY, force, false);
278         if (session == NULL)
279                 return -ENOMEM;
280
281         if (show_threads)
282                 perf_read_values_init(&show_threads_values);
283
284         ret = perf_session__setup_sample_type(session);
285         if (ret)
286                 goto out_delete;
287
288         ret = perf_session__process_events(session, &event_ops);
289         if (ret)
290                 goto out_delete;
291
292         if (dump_trace) {
293                 event__print_totals();
294                 goto out_delete;
295         }
296
297         if (verbose > 3)
298                 perf_session__fprintf(session, stdout);
299
300         if (verbose > 2)
301                 perf_session__fprintf_dsos(session, stdout);
302
303         next = rb_first(&session->stats_by_id);
304         while (next) {
305                 struct event_stat_id *stats;
306                 u64 nr_hists;
307
308                 stats = rb_entry(next, struct event_stat_id, rb_node);
309                 perf_session__collapse_resort(&stats->hists);
310                 nr_hists = perf_session__output_resort(&stats->hists,
311                                                        stats->stats.total);
312                 if (use_browser)
313                         perf_session__browse_hists(&stats->hists, nr_hists,
314                                                    stats->stats.total, help,
315                                                    input_name);
316                 else {
317                         if (rb_first(&session->stats_by_id) ==
318                             rb_last(&session->stats_by_id))
319                                 fprintf(stdout, "# Samples: %Ld\n#\n",
320                                         stats->stats.total);
321                         else
322                                 fprintf(stdout, "# Samples: %Ld %s\n#\n",
323                                         stats->stats.total,
324                                         __event_name(stats->type, stats->config));
325
326                         perf_session__fprintf_hists(&stats->hists, NULL, false, stdout,
327                                             stats->stats.total);
328                         fprintf(stdout, "\n\n");
329                 }
330
331                 next = rb_next(&stats->rb_node);
332         }
333
334         if (!use_browser && sort_order == default_sort_order &&
335             parent_pattern == default_parent_pattern) {
336                 fprintf(stdout, "#\n# (%s)\n#\n", help);
337
338                 if (show_threads) {
339                         bool style = !strcmp(pretty_printing_style, "raw");
340                         perf_read_values_display(stdout, &show_threads_values,
341                                                  style);
342                         perf_read_values_destroy(&show_threads_values);
343                 }
344         }
345 out_delete:
346         perf_session__delete(session);
347         return ret;
348 }
349
350 static int
351 parse_callchain_opt(const struct option *opt __used, const char *arg,
352                     int unset)
353 {
354         char *tok;
355         char *endptr;
356
357         /*
358          * --no-call-graph
359          */
360         if (unset) {
361                 dont_use_callchains = true;
362                 return 0;
363         }
364
365         symbol_conf.use_callchain = true;
366
367         if (!arg)
368                 return 0;
369
370         tok = strtok((char *)arg, ",");
371         if (!tok)
372                 return -1;
373
374         /* get the output mode */
375         if (!strncmp(tok, "graph", strlen(arg)))
376                 callchain_param.mode = CHAIN_GRAPH_ABS;
377
378         else if (!strncmp(tok, "flat", strlen(arg)))
379                 callchain_param.mode = CHAIN_FLAT;
380
381         else if (!strncmp(tok, "fractal", strlen(arg)))
382                 callchain_param.mode = CHAIN_GRAPH_REL;
383
384         else if (!strncmp(tok, "none", strlen(arg))) {
385                 callchain_param.mode = CHAIN_NONE;
386                 symbol_conf.use_callchain = false;
387
388                 return 0;
389         }
390
391         else
392                 return -1;
393
394         /* get the min percentage */
395         tok = strtok(NULL, ",");
396         if (!tok)
397                 goto setup;
398
399         callchain_param.min_percent = strtod(tok, &endptr);
400         if (tok == endptr)
401                 return -1;
402
403 setup:
404         if (register_callchain_param(&callchain_param) < 0) {
405                 fprintf(stderr, "Can't register callchain params\n");
406                 return -1;
407         }
408         return 0;
409 }
410
411 static const char * const report_usage[] = {
412         "perf report [<options>] <command>",
413         NULL
414 };
415
416 static const struct option options[] = {
417         OPT_STRING('i', "input", &input_name, "file",
418                     "input file name"),
419         OPT_INCR('v', "verbose", &verbose,
420                     "be more verbose (show symbol address, etc)"),
421         OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
422                     "dump raw trace in ASCII"),
423         OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
424                    "file", "vmlinux pathname"),
425         OPT_BOOLEAN('f', "force", &force, "don't complain, do it"),
426         OPT_BOOLEAN('m', "modules", &symbol_conf.use_modules,
427                     "load module symbols - WARNING: use only with -k and LIVE kernel"),
428         OPT_BOOLEAN('n', "show-nr-samples", &symbol_conf.show_nr_samples,
429                     "Show a column with the number of samples"),
430         OPT_BOOLEAN('T', "threads", &show_threads,
431                     "Show per-thread event counters"),
432         OPT_STRING(0, "pretty", &pretty_printing_style, "key",
433                    "pretty printing style key: normal raw"),
434         OPT_STRING('s', "sort", &sort_order, "key[,key2...]",
435                    "sort by key(s): pid, comm, dso, symbol, parent"),
436         OPT_BOOLEAN('P', "full-paths", &symbol_conf.full_paths,
437                     "Don't shorten the pathnames taking into account the cwd"),
438         OPT_BOOLEAN(0, "showcpuutilization", &symbol_conf.show_cpu_utilization,
439                     "Show sample percentage for different cpu modes"),
440         OPT_STRING('p', "parent", &parent_pattern, "regex",
441                    "regex filter to identify parent, see: '--sort parent'"),
442         OPT_BOOLEAN('x', "exclude-other", &symbol_conf.exclude_other,
443                     "Only display entries with parent-match"),
444         OPT_CALLBACK_DEFAULT('g', "call-graph", NULL, "output_type,min_percent",
445                      "Display callchains using output_type (graph, flat, fractal, or none) and min percent threshold. "
446                      "Default: fractal,0.5", &parse_callchain_opt, callchain_default_opt),
447         OPT_STRING('d', "dsos", &symbol_conf.dso_list_str, "dso[,dso...]",
448                    "only consider symbols in these dsos"),
449         OPT_STRING('C', "comms", &symbol_conf.comm_list_str, "comm[,comm...]",
450                    "only consider symbols in these comms"),
451         OPT_STRING('S', "symbols", &symbol_conf.sym_list_str, "symbol[,symbol...]",
452                    "only consider these symbols"),
453         OPT_STRING('w', "column-widths", &symbol_conf.col_width_list_str,
454                    "width[,width...]",
455                    "don't try to adjust column width, use these fixed values"),
456         OPT_STRING('t', "field-separator", &symbol_conf.field_sep, "separator",
457                    "separator for columns, no spaces will be added between "
458                    "columns '.' is reserved."),
459         OPT_BOOLEAN('U', "hide-unresolved", &hide_unresolved,
460                     "Only display entries resolved to a symbol"),
461         OPT_END()
462 };
463
464 int cmd_report(int argc, const char **argv, const char *prefix __used)
465 {
466         argc = parse_options(argc, argv, options, report_usage, 0);
467
468         if (strcmp(input_name, "-") != 0)
469                 setup_browser();
470
471         if (symbol__init() < 0)
472                 return -1;
473
474         setup_sorting(report_usage, options);
475
476         if (parent_pattern != default_parent_pattern) {
477                 if (sort_dimension__add("parent") < 0)
478                         return -1;
479                 sort_parent.elide = 1;
480         } else
481                 symbol_conf.exclude_other = false;
482
483         /*
484          * Any (unrecognized) arguments left?
485          */
486         if (argc)
487                 usage_with_options(report_usage, options);
488
489         sort_entry__setup_elide(&sort_dso, symbol_conf.dso_list, "dso", stdout);
490         sort_entry__setup_elide(&sort_comm, symbol_conf.comm_list, "comm", stdout);
491         sort_entry__setup_elide(&sort_sym, symbol_conf.sym_list, "symbol", stdout);
492
493         return __cmd_report();
494 }