]> git.karo-electronics.de Git - karo-tx-linux.git/blobdiff - tools/perf/builtin-trace.c
perf trace: Fixup thread refcounting
[karo-tx-linux.git] / tools / perf / builtin-trace.c
index 206bf72b77fcb9c795486ec9fba184b9d6321b7b..60053d49539bd98767f787796eba807eeba89576 100644 (file)
@@ -40,6 +40,7 @@
 
 #include <libaudit.h> /* FIXME: Still needed for audit_errno_to_name */
 #include <stdlib.h>
+#include <string.h>
 #include <linux/err.h>
 #include <linux/filter.h>
 #include <linux/audit.h>
@@ -1398,7 +1399,7 @@ static struct syscall *trace__syscall_info(struct trace *trace,
        return &trace->syscalls.table[id];
 
 out_cant_read:
-       if (verbose) {
+       if (verbose > 0) {
                fprintf(trace->output, "Problems reading syscall %d", id);
                if (id <= trace->syscalls.max && trace->syscalls.table[id].name != NULL)
                        fprintf(trace->output, "(%s)", trace->syscalls.table[id].name);
@@ -1652,15 +1653,17 @@ static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
 
        ttrace = thread__priv(thread);
        if (!ttrace)
-               goto out;
+               goto out_put;
 
        filename_len = strlen(filename);
+       if (filename_len == 0)
+               goto out_put;
 
        if (ttrace->filename.namelen < filename_len) {
                char *f = realloc(ttrace->filename.name, filename_len + 1);
 
                if (f == NULL)
-                               goto out;
+                       goto out_put;
 
                ttrace->filename.namelen = filename_len;
                ttrace->filename.name = f;
@@ -1670,12 +1673,12 @@ static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
        ttrace->filename.pending_open = true;
 
        if (!ttrace->filename.ptr)
-               goto out;
+               goto out_put;
 
        entry_str_len = strlen(ttrace->entry_str);
        remaining_space = trace__entry_str_size - entry_str_len - 1; /* \0 */
        if (remaining_space <= 0)
-               goto out;
+               goto out_put;
 
        if (filename_len > (size_t)remaining_space) {
                filename += filename_len - remaining_space;
@@ -1689,6 +1692,8 @@ static int trace__vfs_getname(struct trace *trace, struct perf_evsel *evsel,
 
        ttrace->filename.ptr = 0;
        ttrace->filename.entry_str_pos = 0;
+out_put:
+       thread__put(thread);
 out:
        return 0;
 }
@@ -1709,6 +1714,7 @@ static int trace__sched_stat_runtime(struct trace *trace, struct perf_evsel *evs
 
        ttrace->runtime_ms += runtime_ms;
        trace->runtime_ms += runtime_ms;
+out_put:
        thread__put(thread);
        return 0;
 
@@ -1719,8 +1725,7 @@ out_dump:
               (pid_t)perf_evsel__intval(evsel, sample, "pid"),
               runtime,
               perf_evsel__intval(evsel, sample, "vruntime"));
-       thread__put(thread);
-       return 0;
+       goto out_put;
 }
 
 static void bpf_output__printer(enum binary_printer_ops op,
@@ -1800,10 +1805,10 @@ static void print_location(FILE *f, struct perf_sample *sample,
                           bool print_dso, bool print_sym)
 {
 
-       if ((verbose || print_dso) && al->map)
+       if ((verbose > 0 || print_dso) && al->map)
                fprintf(f, "%s@", al->map->dso->long_name);
 
-       if ((verbose || print_sym) && al->sym)
+       if ((verbose > 0 || print_sym) && al->sym)
                fprintf(f, "%s+0x%" PRIx64, al->sym->name,
                        al->addr - al->sym->start);
        else if (al->map)
@@ -1919,7 +1924,7 @@ static int trace__process_sample(struct perf_tool *tool,
 
        thread = machine__findnew_thread(trace->host, sample->pid, sample->tid);
        if (thread && thread__is_filtered(thread))
-               return 0;
+               goto out;
 
        trace__set_base_time(trace, evsel, sample);
 
@@ -1927,7 +1932,8 @@ static int trace__process_sample(struct perf_tool *tool,
                ++trace->nr_events;
                handler(trace, evsel, event, sample);
        }
-
+out:
+       thread__put(thread);
        return err;
 }
 
@@ -2414,8 +2420,9 @@ static int trace__replay(struct trace *trace)
        trace->tool.exit          = perf_event__process_exit;
        trace->tool.fork          = perf_event__process_fork;
        trace->tool.attr          = perf_event__process_attr;
-       trace->tool.tracing_data = perf_event__process_tracing_data;
+       trace->tool.tracing_data  = perf_event__process_tracing_data;
        trace->tool.build_id      = perf_event__process_build_id;
+       trace->tool.namespaces    = perf_event__process_namespaces;
 
        trace->tool.ordered_events = true;
        trace->tool.ordering_requires_timestamps = true;
@@ -2699,6 +2706,91 @@ static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
                evsel->handler = handler;
 }
 
+/*
+ * XXX: Hackish, just splitting the combined -e+--event (syscalls
+ * (raw_syscalls:{sys_{enter,exit}} + events (tracepoints, HW, SW, etc) to use
+ * existing facilities unchanged (trace->ev_qualifier + parse_options()).
+ *
+ * It'd be better to introduce a parse_options() variant that would return a
+ * list with the terms it didn't match to an event...
+ */
+static int trace__parse_events_option(const struct option *opt, const char *str,
+                                     int unset __maybe_unused)
+{
+       struct trace *trace = (struct trace *)opt->value;
+       const char *s = str;
+       char *sep = NULL, *lists[2] = { NULL, NULL, };
+       int len = strlen(str), err = -1, list;
+       char *strace_groups_dir = system_path(STRACE_GROUPS_DIR);
+       char group_name[PATH_MAX];
+
+       if (strace_groups_dir == NULL)
+               return -1;
+
+       if (*s == '!') {
+               ++s;
+               trace->not_ev_qualifier = true;
+       }
+
+       while (1) {
+               if ((sep = strchr(s, ',')) != NULL)
+                       *sep = '\0';
+
+               list = 0;
+               if (syscalltbl__id(trace->sctbl, s) >= 0) {
+                       list = 1;
+               } else {
+                       path__join(group_name, sizeof(group_name), strace_groups_dir, s);
+                       if (access(group_name, R_OK) == 0)
+                               list = 1;
+               }
+
+               if (lists[list]) {
+                       sprintf(lists[list] + strlen(lists[list]), ",%s", s);
+               } else {
+                       lists[list] = malloc(len);
+                       if (lists[list] == NULL)
+                               goto out;
+                       strcpy(lists[list], s);
+               }
+
+               if (!sep)
+                       break;
+
+               *sep = ',';
+               s = sep + 1;
+       }
+
+       if (lists[1] != NULL) {
+               struct strlist_config slist_config = {
+                       .dirname = strace_groups_dir,
+               };
+
+               trace->ev_qualifier = strlist__new(lists[1], &slist_config);
+               if (trace->ev_qualifier == NULL) {
+                       fputs("Not enough memory to parse event qualifier", trace->output);
+                       goto out;
+               }
+
+               if (trace__validate_ev_qualifier(trace))
+                       goto out;
+       }
+
+       err = 0;
+
+       if (lists[0]) {
+               struct option o = OPT_CALLBACK('e', "event", &trace->evlist, "event",
+                                              "event selector. use 'perf list' to list available events",
+                                              parse_events_option);
+               err = parse_events_option(&o, lists[0], 0);
+       }
+out:
+       if (sep)
+               *sep = ',';
+
+       return err;
+}
+
 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
 {
        const char *trace_usage[] = {
@@ -2730,15 +2822,15 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
                .max_stack = UINT_MAX,
        };
        const char *output_name = NULL;
-       const char *ev_qualifier_str = NULL;
        const struct option trace_options[] = {
-       OPT_CALLBACK(0, "event", &trace.evlist, "event",
-                    "event selector. use 'perf list' to list available events",
-                    parse_events_option),
+       OPT_CALLBACK('e', "event", &trace, "event",
+                    "event/syscall selector. use 'perf list' to list available events",
+                    trace__parse_events_option),
        OPT_BOOLEAN(0, "comm", &trace.show_comm,
                    "show the thread COMM next to its id"),
        OPT_BOOLEAN(0, "tool_stats", &trace.show_tool_stats, "show tool stats"),
-       OPT_STRING('e', "expr", &ev_qualifier_str, "expr", "list of syscalls to trace"),
+       OPT_CALLBACK(0, "expr", &trace, "expr", "list of syscalls/events to trace",
+                    trace__parse_events_option),
        OPT_STRING('o', "output", &output_name, "file", "output file name"),
        OPT_STRING('i', "input", &input_name, "file", "Analyze events in file"),
        OPT_STRING('p', "pid", &trace.opts.target.pid, "pid",
@@ -2863,7 +2955,7 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
                return -1;
        }
 
-       if (!trace.trace_syscalls && ev_qualifier_str) {
+       if (!trace.trace_syscalls && trace.ev_qualifier) {
                pr_err("The -e option can't be used with --no-syscalls.\n");
                goto out;
        }
@@ -2878,28 +2970,6 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
 
        trace.open_id = syscalltbl__id(trace.sctbl, "open");
 
-       if (ev_qualifier_str != NULL) {
-               const char *s = ev_qualifier_str;
-               struct strlist_config slist_config = {
-                       .dirname = system_path(STRACE_GROUPS_DIR),
-               };
-
-               trace.not_ev_qualifier = *s == '!';
-               if (trace.not_ev_qualifier)
-                       ++s;
-               trace.ev_qualifier = strlist__new(s, &slist_config);
-               if (trace.ev_qualifier == NULL) {
-                       fputs("Not enough memory to parse event qualifier",
-                             trace.output);
-                       err = -ENOMEM;
-                       goto out_close;
-               }
-
-               err = trace__validate_ev_qualifier(&trace);
-               if (err)
-                       goto out_close;
-       }
-
        err = target__validate(&trace.opts.target);
        if (err) {
                target__strerror(&trace.opts.target, err, bf, sizeof(bf));