perf report: Fix -T/--threads option to work again
[firefly-linux-kernel-4.4.55.git] / tools / perf / builtin-trace.c
index 5cd8497445fe7e694d733b3e7af940ed0cb8b1f7..e122970361f21af6d07c321480aefa2cb90bf31d 100644 (file)
@@ -52,7 +52,9 @@ struct tp_field {
 #define TP_UINT_FIELD(bits) \
 static u64 tp_field__u##bits(struct tp_field *field, struct perf_sample *sample) \
 { \
-       return *(u##bits *)(sample->raw_data + field->offset); \
+       u##bits value; \
+       memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
+       return value;  \
 }
 
 TP_UINT_FIELD(8);
@@ -63,7 +65,8 @@ TP_UINT_FIELD(64);
 #define TP_UINT_FIELD__SWAPPED(bits) \
 static u64 tp_field__swapped_u##bits(struct tp_field *field, struct perf_sample *sample) \
 { \
-       u##bits value = *(u##bits *)(sample->raw_data + field->offset); \
+       u##bits value; \
+       memcpy(&value, sample->raw_data + field->offset, sizeof(value)); \
        return bswap_##bits(value);\
 }
 
@@ -1132,6 +1135,8 @@ static struct syscall_fmt *syscall_fmt__find(const char *name)
 
 struct syscall {
        struct event_format *tp_format;
+       int                 nr_args;
+       struct format_field *args;
        const char          *name;
        bool                filtered;
        bool                is_exit;
@@ -1249,6 +1254,7 @@ struct trace {
        bool                    show_comm;
        bool                    show_tool_stats;
        bool                    trace_syscalls;
+       bool                    force;
        int                     trace_pgfaults;
 };
 
@@ -1439,14 +1445,14 @@ static int syscall__set_arg_fmts(struct syscall *sc)
        struct format_field *field;
        int idx = 0;
 
-       sc->arg_scnprintf = calloc(sc->tp_format->format.nr_fields - 1, sizeof(void *));
+       sc->arg_scnprintf = calloc(sc->nr_args, sizeof(void *));
        if (sc->arg_scnprintf == NULL)
                return -1;
 
        if (sc->fmt)
                sc->arg_parm = sc->fmt->arg_parm;
 
-       for (field = sc->tp_format->format.fields->next; field; field = field->next) {
+       for (field = sc->args; field; field = field->next) {
                if (sc->fmt && sc->fmt->arg_scnprintf[idx])
                        sc->arg_scnprintf[idx] = sc->fmt->arg_scnprintf[idx];
                else if (field->flags & FIELD_IS_POINTER)
@@ -1512,18 +1518,37 @@ static int trace__read_syscall_info(struct trace *trace, int id)
        if (sc->tp_format == NULL)
                return -1;
 
+       sc->args = sc->tp_format->format.fields;
+       sc->nr_args = sc->tp_format->format.nr_fields;
+       /* drop nr field - not relevant here; does not exist on older kernels */
+       if (sc->args && strcmp(sc->args->name, "nr") == 0) {
+               sc->args = sc->args->next;
+               --sc->nr_args;
+       }
+
        sc->is_exit = !strcmp(name, "exit_group") || !strcmp(name, "exit");
 
        return syscall__set_arg_fmts(sc);
 }
 
+/*
+ * args is to be interpreted as a series of longs but we need to handle
+ * 8-byte unaligned accesses. args points to raw_data within the event
+ * and raw_data is guaranteed to be 8-byte unaligned because it is
+ * preceded by raw_size which is a u32. So we need to copy args to a temp
+ * variable to read it. Most notably this avoids extended load instructions
+ * on unaligned addresses
+ */
+
 static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
-                                     unsigned long *args, struct trace *trace,
+                                     unsigned char *args, struct trace *trace,
                                      struct thread *thread)
 {
        size_t printed = 0;
+       unsigned char *p;
+       unsigned long val;
 
-       if (sc->tp_format != NULL) {
+       if (sc->args != NULL) {
                struct format_field *field;
                u8 bit = 1;
                struct syscall_arg arg = {
@@ -1533,16 +1558,21 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
                        .thread = thread,
                };
 
-               for (field = sc->tp_format->format.fields->next; field;
+               for (field = sc->args; field;
                     field = field->next, ++arg.idx, bit <<= 1) {
                        if (arg.mask & bit)
                                continue;
+
+                       /* special care for unaligned accesses */
+                       p = args + sizeof(unsigned long) * arg.idx;
+                       memcpy(&val, p, sizeof(val));
+
                        /*
                         * Suppress this argument if its value is zero and
                         * and we don't have a string associated in an
                         * strarray for it.
                         */
-                       if (args[arg.idx] == 0 &&
+                       if (val == 0 &&
                            !(sc->arg_scnprintf &&
                              sc->arg_scnprintf[arg.idx] == SCA_STRARRAY &&
                              sc->arg_parm[arg.idx]))
@@ -1551,23 +1581,26 @@ static size_t syscall__scnprintf_args(struct syscall *sc, char *bf, size_t size,
                        printed += scnprintf(bf + printed, size - printed,
                                             "%s%s: ", printed ? ", " : "", field->name);
                        if (sc->arg_scnprintf && sc->arg_scnprintf[arg.idx]) {
-                               arg.val = args[arg.idx];
+                               arg.val = val;
                                if (sc->arg_parm)
                                        arg.parm = sc->arg_parm[arg.idx];
                                printed += sc->arg_scnprintf[arg.idx](bf + printed,
                                                                      size - printed, &arg);
                        } else {
                                printed += scnprintf(bf + printed, size - printed,
-                                                    "%ld", args[arg.idx]);
+                                                    "%ld", val);
                        }
                }
        } else {
                int i = 0;
 
                while (i < 6) {
+                       /* special care for unaligned accesses */
+                       p = args + sizeof(unsigned long) * i;
+                       memcpy(&val, p, sizeof(val));
                        printed += scnprintf(bf + printed, size - printed,
                                             "%sarg%d: %ld",
-                                            printed ? ", " : "", i, args[i]);
+                                            printed ? ", " : "", i, val);
                        ++i;
                }
        }
@@ -1702,7 +1735,8 @@ static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
                        return -1;
        }
 
-       printed += trace__printf_interrupted_entry(trace, sample);
+       if (!trace->summary_only)
+               trace__printf_interrupted_entry(trace, sample);
 
        ttrace->entry_time = sample->time;
        msg = ttrace->entry_str;
@@ -1719,7 +1753,10 @@ static int trace__sys_enter(struct trace *trace, struct perf_evsel *evsel,
        } else
                ttrace->entry_pending = true;
 
-       trace->current = thread;
+       if (trace->current != thread) {
+               thread__put(trace->current);
+               trace->current = thread__get(thread);
+       }
 
        return 0;
 }
@@ -2204,10 +2241,11 @@ static int trace__run(struct trace *trace, int argc, const char **argv)
        if (err < 0)
                goto out_error_mmap;
 
+       if (!target__none(&trace->opts.target))
+               perf_evlist__enable(evlist);
+
        if (forks)
                perf_evlist__start_workload(evlist);
-       else
-               perf_evlist__enable(evlist);
 
        trace->multiple_threads = evlist->threads->map[0] == -1 ||
                                  evlist->threads->nr > 1 ||
@@ -2235,6 +2273,11 @@ next_event:
 
                        if (interrupted)
                                goto out_disable;
+
+                       if (done && !draining) {
+                               perf_evlist__disable(evlist);
+                               draining = true;
+                       }
                }
        }
 
@@ -2252,6 +2295,8 @@ next_event:
        }
 
 out_disable:
+       thread__zput(trace->current);
+
        perf_evlist__disable(evlist);
 
        if (!err) {
@@ -2307,6 +2352,7 @@ static int trace__replay(struct trace *trace)
        struct perf_data_file file = {
                .path  = input_name,
                .mode  = PERF_DATA_MODE_READ,
+               .force = trace->force,
        };
        struct perf_session *session;
        struct perf_evsel *evsel;
@@ -2381,7 +2427,7 @@ static int trace__replay(struct trace *trace)
 
        setup_pager();
 
-       err = perf_session__process_events(session, &trace->tool);
+       err = perf_session__process_events(session);
        if (err)
                pr_err("Failed to process events, error %d", err);
 
@@ -2582,7 +2628,7 @@ static void evlist__set_evsel_handler(struct perf_evlist *evlist, void *handler)
 
 int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
 {
-       const char * const trace_usage[] = {
+       const char *trace_usage[] = {
                "perf trace [<options>] [<command>]",
                "perf trace [<options>] -- <command> [<options>]",
                "perf trace record [<options>] [<command>]",
@@ -2655,8 +2701,10 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
        OPT_CALLBACK_DEFAULT('F', "pf", &trace.trace_pgfaults, "all|maj|min",
                     "Trace pagefaults", parse_pagefaults, "maj"),
        OPT_BOOLEAN(0, "syscalls", &trace.trace_syscalls, "Trace syscalls"),
+       OPT_BOOLEAN('f', "force", &trace.force, "don't complain, do it"),
        OPT_END()
        };
+       const char * const trace_subcommands[] = { "record", NULL };
        int err;
        char bf[BUFSIZ];
 
@@ -2672,8 +2720,8 @@ int cmd_trace(int argc, const char **argv, const char *prefix __maybe_unused)
                goto out;
        }
 
-       argc = parse_options(argc, argv, trace_options, trace_usage,
-                            PARSE_OPT_STOP_AT_NON_OPTION);
+       argc = parse_options_subcommand(argc, argv, trace_options, trace_subcommands,
+                                trace_usage, PARSE_OPT_STOP_AT_NON_OPTION);
 
        if (trace.trace_pgfaults) {
                trace.opts.sample_address = true;