mm/migrate: correct failure handling if !hugepage_migration_support()
[firefly-linux-kernel-4.4.55.git] / tools / perf / builtin-script.c
index baf17989a216137064e089d4e2f65fef07a4357f..9e9c91f5b7fae97de166583b46bd801fc4ccc2f1 100644 (file)
@@ -43,6 +43,7 @@ enum perf_output_field {
        PERF_OUTPUT_DSO             = 1U << 9,
        PERF_OUTPUT_ADDR            = 1U << 10,
        PERF_OUTPUT_SYMOFFSET       = 1U << 11,
+       PERF_OUTPUT_SRCLINE         = 1U << 12,
 };
 
 struct output_option {
@@ -61,6 +62,7 @@ struct output_option {
        {.str = "dso",   .field = PERF_OUTPUT_DSO},
        {.str = "addr",  .field = PERF_OUTPUT_ADDR},
        {.str = "symoff", .field = PERF_OUTPUT_SYMOFFSET},
+       {.str = "srcline", .field = PERF_OUTPUT_SRCLINE},
 };
 
 /* default set to maintain compatibility with current format */
@@ -210,6 +212,11 @@ static int perf_evsel__check_attr(struct perf_evsel *evsel,
                       "to DSO.\n");
                return -EINVAL;
        }
+       if (PRINT_FIELD(SRCLINE) && !PRINT_FIELD(IP)) {
+               pr_err("Display of source line number requested but sample IP is not\n"
+                      "selected. Hence, no address to lookup the source line number.\n");
+               return -EINVAL;
+       }
 
        if ((PRINT_FIELD(PID) || PRINT_FIELD(TID)) &&
                perf_evsel__check_stype(evsel, PERF_SAMPLE_TID, "TID",
@@ -245,6 +252,9 @@ static void set_print_ip_opts(struct perf_event_attr *attr)
 
        if (PRINT_FIELD(SYMOFFSET))
                output[type].print_ip_opts |= PRINT_IP_OPT_SYMOFFSET;
+
+       if (PRINT_FIELD(SRCLINE))
+               output[type].print_ip_opts |= PRINT_IP_OPT_SRCLINE;
 }
 
 /*
@@ -280,6 +290,30 @@ static int perf_session__check_output_opt(struct perf_session *session)
                set_print_ip_opts(&evsel->attr);
        }
 
+       /*
+        * set default for tracepoints to print symbols only
+        * if callchains are present
+        */
+       if (symbol_conf.use_callchain &&
+           !output[PERF_TYPE_TRACEPOINT].user_set) {
+               struct perf_event_attr *attr;
+
+               j = PERF_TYPE_TRACEPOINT;
+               evsel = perf_session__find_first_evtype(session, j);
+               if (evsel == NULL)
+                       goto out;
+
+               attr = &evsel->attr;
+
+               if (attr->sample_type & PERF_SAMPLE_CALLCHAIN) {
+                       output[j].fields |= PERF_OUTPUT_IP;
+                       output[j].fields |= PERF_OUTPUT_SYM;
+                       output[j].fields |= PERF_OUTPUT_DSO;
+                       set_print_ip_opts(attr);
+               }
+       }
+
+out:
        return 0;
 }
 
@@ -288,7 +322,6 @@ static void print_sample_start(struct perf_sample *sample,
                               struct perf_evsel *evsel)
 {
        struct perf_event_attr *attr = &evsel->attr;
-       const char *evname = NULL;
        unsigned long secs;
        unsigned long usecs;
        unsigned long long nsecs;
@@ -323,11 +356,6 @@ static void print_sample_start(struct perf_sample *sample,
                usecs = nsecs / NSECS_PER_USEC;
                printf("%5lu.%06lu: ", secs, usecs);
        }
-
-       if (PRINT_FIELD(EVNAME)) {
-               evname = perf_evsel__name(evsel);
-               printf("%s: ", evname ? evname : "[unknown]");
-       }
 }
 
 static bool is_bts_event(struct perf_event_attr *attr)
@@ -395,8 +423,8 @@ static void print_sample_addr(union perf_event *event,
 static void print_sample_bts(union perf_event *event,
                             struct perf_sample *sample,
                             struct perf_evsel *evsel,
-                            struct machine *machine,
-                            struct thread *thread)
+                            struct thread *thread,
+                            struct addr_location *al)
 {
        struct perf_event_attr *attr = &evsel->attr;
 
@@ -406,7 +434,7 @@ static void print_sample_bts(union perf_event *event,
                        printf(" ");
                else
                        printf("\n");
-               perf_evsel__print_ip(evsel, event, sample, machine,
+               perf_evsel__print_ip(evsel, sample, al,
                                     output[attr->type].print_ip_opts,
                                     PERF_MAX_STACK_DEPTH);
        }
@@ -417,15 +445,14 @@ static void print_sample_bts(union perf_event *event,
        if (PRINT_FIELD(ADDR) ||
            ((evsel->attr.sample_type & PERF_SAMPLE_ADDR) &&
             !output[attr->type].user_set))
-               print_sample_addr(event, sample, machine, thread, attr);
+               print_sample_addr(event, sample, al->machine, thread, attr);
 
        printf("\n");
 }
 
 static void process_event(union perf_event *event, struct perf_sample *sample,
-                         struct perf_evsel *evsel, struct machine *machine,
-                         struct thread *thread,
-                         struct addr_location *al __maybe_unused)
+                         struct perf_evsel *evsel, struct thread *thread,
+                         struct addr_location *al)
 {
        struct perf_event_attr *attr = &evsel->attr;
 
@@ -434,8 +461,13 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
 
        print_sample_start(sample, thread, evsel);
 
+       if (PRINT_FIELD(EVNAME)) {
+               const char *evname = perf_evsel__name(evsel);
+               printf("%s: ", evname ? evname : "[unknown]");
+       }
+
        if (is_bts_event(attr)) {
-               print_sample_bts(event, sample, evsel, machine, thread);
+               print_sample_bts(event, sample, evsel, thread, al);
                return;
        }
 
@@ -443,7 +475,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
                event_format__print(evsel->tp_format, sample->cpu,
                                    sample->raw_data, sample->raw_size);
        if (PRINT_FIELD(ADDR))
-               print_sample_addr(event, sample, machine, thread, attr);
+               print_sample_addr(event, sample, al->machine, thread, attr);
 
        if (PRINT_FIELD(IP)) {
                if (!symbol_conf.use_callchain)
@@ -451,7 +483,7 @@ static void process_event(union perf_event *event, struct perf_sample *sample,
                else
                        printf("\n");
 
-               perf_evsel__print_ip(evsel, event, sample, machine,
+               perf_evsel__print_ip(evsel, sample, al,
                                     output[attr->type].print_ip_opts,
                                     PERF_MAX_STACK_DEPTH);
        }
@@ -540,7 +572,7 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
        if (cpu_list && !test_bit(sample->cpu, cpu_bitmap))
                return 0;
 
-       scripting_ops->process_event(event, sample, evsel, machine, thread, &al);
+       scripting_ops->process_event(event, sample, evsel, thread, &al);
 
        evsel->hists.stats.total_period += sample->period;
        return 0;
@@ -549,6 +581,8 @@ static int process_sample_event(struct perf_tool *tool __maybe_unused,
 struct perf_script {
        struct perf_tool        tool;
        struct perf_session     *session;
+       bool                    show_task_events;
+       bool                    show_mmap_events;
 };
 
 static int process_attr(struct perf_tool *tool, union perf_event *event,
@@ -569,7 +603,7 @@ static int process_attr(struct perf_tool *tool, union perf_event *event,
        if (evsel->attr.type >= PERF_TYPE_MAX)
                return 0;
 
-       list_for_each_entry(pos, &evlist->entries, node) {
+       evlist__for_each(evlist, pos) {
                if (pos->attr.type == evsel->attr.type && pos != evsel)
                        return 0;
        }
@@ -579,6 +613,163 @@ static int process_attr(struct perf_tool *tool, union perf_event *event,
        return perf_evsel__check_attr(evsel, scr->session);
 }
 
+static int process_comm_event(struct perf_tool *tool,
+                             union perf_event *event,
+                             struct perf_sample *sample,
+                             struct machine *machine)
+{
+       struct thread *thread;
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       struct perf_session *session = script->session;
+       struct perf_evsel *evsel = perf_evlist__first(session->evlist);
+       int ret = -1;
+
+       thread = machine__findnew_thread(machine, event->comm.pid, event->comm.tid);
+       if (thread == NULL) {
+               pr_debug("problem processing COMM event, skipping it.\n");
+               return -1;
+       }
+
+       if (perf_event__process_comm(tool, event, sample, machine) < 0)
+               goto out;
+
+       if (!evsel->attr.sample_id_all) {
+               sample->cpu = 0;
+               sample->time = 0;
+               sample->tid = event->comm.tid;
+               sample->pid = event->comm.pid;
+       }
+       print_sample_start(sample, thread, evsel);
+       perf_event__fprintf(event, stdout);
+       ret = 0;
+
+out:
+       return ret;
+}
+
+static int process_fork_event(struct perf_tool *tool,
+                             union perf_event *event,
+                             struct perf_sample *sample,
+                             struct machine *machine)
+{
+       struct thread *thread;
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       struct perf_session *session = script->session;
+       struct perf_evsel *evsel = perf_evlist__first(session->evlist);
+
+       if (perf_event__process_fork(tool, event, sample, machine) < 0)
+               return -1;
+
+       thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
+       if (thread == NULL) {
+               pr_debug("problem processing FORK event, skipping it.\n");
+               return -1;
+       }
+
+       if (!evsel->attr.sample_id_all) {
+               sample->cpu = 0;
+               sample->time = event->fork.time;
+               sample->tid = event->fork.tid;
+               sample->pid = event->fork.pid;
+       }
+       print_sample_start(sample, thread, evsel);
+       perf_event__fprintf(event, stdout);
+
+       return 0;
+}
+static int process_exit_event(struct perf_tool *tool,
+                             union perf_event *event,
+                             struct perf_sample *sample,
+                             struct machine *machine)
+{
+       struct thread *thread;
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       struct perf_session *session = script->session;
+       struct perf_evsel *evsel = perf_evlist__first(session->evlist);
+
+       thread = machine__findnew_thread(machine, event->fork.pid, event->fork.tid);
+       if (thread == NULL) {
+               pr_debug("problem processing EXIT event, skipping it.\n");
+               return -1;
+       }
+
+       if (!evsel->attr.sample_id_all) {
+               sample->cpu = 0;
+               sample->time = 0;
+               sample->tid = event->comm.tid;
+               sample->pid = event->comm.pid;
+       }
+       print_sample_start(sample, thread, evsel);
+       perf_event__fprintf(event, stdout);
+
+       if (perf_event__process_exit(tool, event, sample, machine) < 0)
+               return -1;
+
+       return 0;
+}
+
+static int process_mmap_event(struct perf_tool *tool,
+                             union perf_event *event,
+                             struct perf_sample *sample,
+                             struct machine *machine)
+{
+       struct thread *thread;
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       struct perf_session *session = script->session;
+       struct perf_evsel *evsel = perf_evlist__first(session->evlist);
+
+       if (perf_event__process_mmap(tool, event, sample, machine) < 0)
+               return -1;
+
+       thread = machine__findnew_thread(machine, event->mmap.pid, event->mmap.tid);
+       if (thread == NULL) {
+               pr_debug("problem processing MMAP event, skipping it.\n");
+               return -1;
+       }
+
+       if (!evsel->attr.sample_id_all) {
+               sample->cpu = 0;
+               sample->time = 0;
+               sample->tid = event->mmap.tid;
+               sample->pid = event->mmap.pid;
+       }
+       print_sample_start(sample, thread, evsel);
+       perf_event__fprintf(event, stdout);
+
+       return 0;
+}
+
+static int process_mmap2_event(struct perf_tool *tool,
+                             union perf_event *event,
+                             struct perf_sample *sample,
+                             struct machine *machine)
+{
+       struct thread *thread;
+       struct perf_script *script = container_of(tool, struct perf_script, tool);
+       struct perf_session *session = script->session;
+       struct perf_evsel *evsel = perf_evlist__first(session->evlist);
+
+       if (perf_event__process_mmap2(tool, event, sample, machine) < 0)
+               return -1;
+
+       thread = machine__findnew_thread(machine, event->mmap2.pid, event->mmap2.tid);
+       if (thread == NULL) {
+               pr_debug("problem processing MMAP2 event, skipping it.\n");
+               return -1;
+       }
+
+       if (!evsel->attr.sample_id_all) {
+               sample->cpu = 0;
+               sample->time = 0;
+               sample->tid = event->mmap2.tid;
+               sample->pid = event->mmap2.pid;
+       }
+       print_sample_start(sample, thread, evsel);
+       perf_event__fprintf(event, stdout);
+
+       return 0;
+}
+
 static void sig_handler(int sig __maybe_unused)
 {
        session_done = 1;
@@ -590,6 +781,17 @@ static int __cmd_script(struct perf_script *script)
 
        signal(SIGINT, sig_handler);
 
+       /* override event processing functions */
+       if (script->show_task_events) {
+               script->tool.comm = process_comm_event;
+               script->tool.fork = process_fork_event;
+               script->tool.exit = process_exit_event;
+       }
+       if (script->show_mmap_events) {
+               script->tool.mmap = process_mmap_event;
+               script->tool.mmap2 = process_mmap2_event;
+       }
+
        ret = perf_session__process_events(script->session, &script->tool);
 
        if (debug_mode)
@@ -900,9 +1102,9 @@ static struct script_desc *script_desc__new(const char *name)
 
 static void script_desc__delete(struct script_desc *s)
 {
-       free(s->name);
-       free(s->half_liner);
-       free(s->args);
+       zfree(&s->name);
+       zfree(&s->half_liner);
+       zfree(&s->args);
        free(s);
 }
 
@@ -1107,8 +1309,7 @@ static int check_ev_match(char *dir_name, char *scriptname,
                        snprintf(evname, len + 1, "%s", p);
 
                        match = 0;
-                       list_for_each_entry(pos,
-                                       &session->evlist->entries, node) {
+                       evlist__for_each(session->evlist, pos) {
                                if (!strcmp(perf_evsel__name(pos), evname)) {
                                        match = 1;
                                        break;
@@ -1290,6 +1491,8 @@ static int have_cmd(int argc, const char **argv)
 int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
 {
        bool show_full_info = false;
+       bool header = false;
+       bool header_only = false;
        char *rec_script_path = NULL;
        char *rep_script_path = NULL;
        struct perf_session *session;
@@ -1328,6 +1531,8 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
        OPT_STRING('i', "input", &input_name, "file", "input file name"),
        OPT_BOOLEAN('d', "debug-mode", &debug_mode,
                   "do various checks like samples ordering and lost events"),
+       OPT_BOOLEAN(0, "header", &header, "Show data header."),
+       OPT_BOOLEAN(0, "header-only", &header_only, "Show only data header."),
        OPT_STRING('k', "vmlinux", &symbol_conf.vmlinux_name,
                   "file", "vmlinux pathname"),
        OPT_STRING(0, "kallsyms", &symbol_conf.kallsyms_name,
@@ -1352,6 +1557,10 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
                    "display extended information from perf.data file"),
        OPT_BOOLEAN('\0', "show-kernel-path", &symbol_conf.show_kernel_path,
                    "Show the path of [kernel.kallsyms]"),
+       OPT_BOOLEAN('\0', "show-task-events", &script.show_task_events,
+                   "Show the fork/comm/exit events"),
+       OPT_BOOLEAN('\0', "show-mmap-events", &script.show_mmap_events,
+                   "Show the mmap events"),
        OPT_END()
        };
        const char * const script_usage[] = {
@@ -1540,6 +1749,12 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
        if (session == NULL)
                return -ENOMEM;
 
+       if (header || header_only) {
+               perf_session__fprintf_info(session, stdout, show_full_info);
+               if (header_only)
+                       return 0;
+       }
+
        script.session = session;
 
        if (cpu_list) {
@@ -1547,9 +1762,6 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
                        return -1;
        }
 
-       if (!script_name && !generate_script_lang)
-               perf_session__fprintf_info(session, stdout, show_full_info);
-
        if (!no_callchain)
                symbol_conf.use_callchain = true;
        else
@@ -1588,7 +1800,7 @@ int cmd_script(int argc, const char **argv, const char *prefix __maybe_unused)
                        return -1;
                }
 
-               err = scripting_ops->generate_script(session->pevent,
+               err = scripting_ops->generate_script(session->tevent.pevent,
                                                     "perf-script");
                goto out;
        }