]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - tools/perf/builtin-trace.c
perf session: Register the idle thread in perf_session__process_events
[mirror_ubuntu-zesty-kernel.git] / tools / perf / builtin-trace.c
CommitLineData
5f9c39dc
FW
1#include "builtin.h"
2
3#include "util/util.h"
4#include "util/cache.h"
5#include "util/symbol.h"
6#include "util/thread.h"
7#include "util/header.h"
cf72344d
IM
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
94c744b6 10#include "util/session.h"
5f9c39dc 11
956ffd02
TZ
12static char const *script_name;
13static char const *generate_script_lang;
14
15static int default_start_script(const char *script __attribute((unused)))
16{
17 return 0;
18}
19
20static int default_stop_script(void)
21{
22 return 0;
23}
24
25static int default_generate_script(const char *outfile __attribute ((unused)))
26{
27 return 0;
28}
29
30static struct scripting_ops default_scripting_ops = {
31 .start_script = default_start_script,
32 .stop_script = default_stop_script,
33 .process_event = print_event,
34 .generate_script = default_generate_script,
35};
36
37static struct scripting_ops *scripting_ops;
38
39static void setup_scripting(void)
40{
41 /* make sure PERF_EXEC_PATH is set for scripts */
42 perf_set_argv_exec_path(perf_exec_path());
43
16c632de
TZ
44 setup_perl_scripting();
45
956ffd02
TZ
46 scripting_ops = &default_scripting_ops;
47}
48
49static int cleanup_scripting(void)
50{
51 return scripting_ops->stop_script();
52}
53
5f9c39dc
FW
54#include "util/parse-options.h"
55
56#include "perf.h"
57#include "util/debug.h"
58
59#include "util/trace-event.h"
956ffd02 60#include "util/exec_cmd.h"
5f9c39dc 61
956ffd02 62static char const *input_name = "perf.data";
5f9c39dc 63
956ffd02 64static u64 sample_type;
5f9c39dc 65
d8f66248 66static int process_sample_event(event_t *event, struct perf_session *session __used)
5f9c39dc 67{
180f95e2
OH
68 struct sample_data data;
69 struct thread *thread;
6ddf259d 70
180f95e2
OH
71 memset(&data, 0, sizeof(data));
72 data.time = -1;
73 data.cpu = -1;
74 data.period = 1;
cd6feeea 75
180f95e2 76 event__parse_sample(event, sample_type, &data);
5f9c39dc 77
62daacb5 78 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
5f9c39dc 79 event->header.misc,
180f95e2
OH
80 data.pid, data.tid,
81 (void *)(long)data.ip,
82 (long long)data.period);
5f9c39dc 83
180f95e2 84 thread = threads__findnew(event->ip.pid);
5f9c39dc 85 if (thread == NULL) {
6beba7ad
ACM
86 pr_debug("problem processing %d event, skipping it.\n",
87 event->header.type);
5f9c39dc
FW
88 return -1;
89 }
90
5f9c39dc 91 if (sample_type & PERF_SAMPLE_RAW) {
5f9c39dc
FW
92 /*
93 * FIXME: better resolve from pid from the struct trace_entry
94 * field, although it should be the same than this perf
95 * event pid
96 */
180f95e2
OH
97 scripting_ops->process_event(data.cpu, data.raw_data,
98 data.raw_size,
99 data.time, thread->comm);
5f9c39dc 100 }
180f95e2 101 event__stats.total += data.period;
5f9c39dc
FW
102
103 return 0;
104}
105
016e92fb 106static int sample_type_check(u64 type)
5f9c39dc 107{
016e92fb 108 sample_type = type;
5f9c39dc 109
016e92fb
FW
110 if (!(sample_type & PERF_SAMPLE_RAW)) {
111 fprintf(stderr,
112 "No trace sample to read. Did you call perf record "
113 "without -R?");
5f9c39dc
FW
114 return -1;
115 }
116
117 return 0;
118}
119
301a0b02 120static struct perf_event_ops event_ops = {
016e92fb 121 .process_sample_event = process_sample_event,
62daacb5 122 .process_comm_event = event__process_comm,
016e92fb
FW
123 .sample_type_check = sample_type_check,
124};
125
d8f66248 126static int __cmd_trace(struct perf_session *session)
5f9c39dc 127{
301a0b02
ACM
128 return perf_session__process_events(session, &event_ops, 0,
129 &event__cwdlen, &event__cwd);
5f9c39dc
FW
130}
131
956ffd02
TZ
132struct script_spec {
133 struct list_head node;
134 struct scripting_ops *ops;
135 char spec[0];
136};
137
138LIST_HEAD(script_specs);
139
140static struct script_spec *script_spec__new(const char *spec,
141 struct scripting_ops *ops)
142{
143 struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1);
144
145 if (s != NULL) {
146 strcpy(s->spec, spec);
147 s->ops = ops;
148 }
149
150 return s;
151}
152
153static void script_spec__delete(struct script_spec *s)
154{
155 free(s->spec);
156 free(s);
157}
158
159static void script_spec__add(struct script_spec *s)
160{
161 list_add_tail(&s->node, &script_specs);
162}
163
164static struct script_spec *script_spec__find(const char *spec)
165{
166 struct script_spec *s;
167
168 list_for_each_entry(s, &script_specs, node)
169 if (strcasecmp(s->spec, spec) == 0)
170 return s;
171 return NULL;
172}
173
174static struct script_spec *script_spec__findnew(const char *spec,
175 struct scripting_ops *ops)
176{
177 struct script_spec *s = script_spec__find(spec);
178
179 if (s)
180 return s;
181
182 s = script_spec__new(spec, ops);
183 if (!s)
184 goto out_delete_spec;
185
186 script_spec__add(s);
187
188 return s;
189
190out_delete_spec:
191 script_spec__delete(s);
192
193 return NULL;
194}
195
196int script_spec_register(const char *spec, struct scripting_ops *ops)
197{
198 struct script_spec *s;
199
200 s = script_spec__find(spec);
201 if (s)
202 return -1;
203
204 s = script_spec__findnew(spec, ops);
205 if (!s)
206 return -1;
207
208 return 0;
209}
210
211static struct scripting_ops *script_spec__lookup(const char *spec)
212{
213 struct script_spec *s = script_spec__find(spec);
214 if (!s)
215 return NULL;
216
217 return s->ops;
218}
219
220static void list_available_languages(void)
221{
222 struct script_spec *s;
223
224 fprintf(stderr, "\n");
225 fprintf(stderr, "Scripting language extensions (used in "
226 "perf trace -s [spec:]script.[spec]):\n\n");
227
228 list_for_each_entry(s, &script_specs, node)
229 fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name);
230
231 fprintf(stderr, "\n");
232}
233
234static int parse_scriptname(const struct option *opt __used,
235 const char *str, int unset __used)
236{
237 char spec[PATH_MAX];
238 const char *script, *ext;
239 int len;
240
241 if (strcmp(str, "list") == 0) {
242 list_available_languages();
243 return 0;
244 }
245
246 script = strchr(str, ':');
247 if (script) {
248 len = script - str;
249 if (len >= PATH_MAX) {
250 fprintf(stderr, "invalid language specifier");
251 return -1;
252 }
253 strncpy(spec, str, len);
254 spec[len] = '\0';
255 scripting_ops = script_spec__lookup(spec);
256 if (!scripting_ops) {
257 fprintf(stderr, "invalid language specifier");
258 return -1;
259 }
260 script++;
261 } else {
262 script = str;
263 ext = strchr(script, '.');
264 if (!ext) {
265 fprintf(stderr, "invalid script extension");
266 return -1;
267 }
268 scripting_ops = script_spec__lookup(++ext);
269 if (!scripting_ops) {
270 fprintf(stderr, "invalid script extension");
271 return -1;
272 }
273 }
274
275 script_name = strdup(script);
276
277 return 0;
278}
279
5f9c39dc
FW
280static const char * const annotate_usage[] = {
281 "perf trace [<options>] <command>",
282 NULL
283};
284
285static const struct option options[] = {
286 OPT_BOOLEAN('D', "dump-raw-trace", &dump_trace,
287 "dump raw trace in ASCII"),
288 OPT_BOOLEAN('v', "verbose", &verbose,
289 "be more verbose (show symbol address, etc)"),
cda48461
SR
290 OPT_BOOLEAN('l', "latency", &latency_format,
291 "show latency attributes (irqs/preemption disabled, etc)"),
956ffd02
TZ
292 OPT_CALLBACK('s', "script", NULL, "name",
293 "script file name (lang:script name, script name, or *)",
294 parse_scriptname),
295 OPT_STRING('g', "gen-script", &generate_script_lang, "lang",
296 "generate perf-trace.xx script in specified language"),
297
1909629f 298 OPT_END()
5f9c39dc
FW
299};
300
301int cmd_trace(int argc, const char **argv, const char *prefix __used)
302{
956ffd02 303 int err;
d8f66248 304 struct perf_session *session;
956ffd02 305
00a192b3 306 symbol__init(0);
5f9c39dc 307
956ffd02
TZ
308 setup_scripting();
309
5f9c39dc
FW
310 argc = parse_options(argc, argv, options, annotate_usage, 0);
311 if (argc) {
312 /*
313 * Special case: if there's an argument left then assume tha
314 * it's a symbol filter:
315 */
316 if (argc > 1)
317 usage_with_options(annotate_usage, options);
318 }
319
5f9c39dc
FW
320 setup_pager();
321
d8f66248
ACM
322 session = perf_session__new(input_name, O_RDONLY, 0);
323 if (session == NULL)
324 return -ENOMEM;
325
956ffd02
TZ
326 if (generate_script_lang) {
327 struct stat perf_stat;
328
329 int input = open(input_name, O_RDONLY);
330 if (input < 0) {
331 perror("failed to open file");
332 exit(-1);
333 }
334
335 err = fstat(input, &perf_stat);
336 if (err < 0) {
337 perror("failed to stat file");
338 exit(-1);
339 }
340
341 if (!perf_stat.st_size) {
342 fprintf(stderr, "zero-sized file, nothing to do!\n");
343 exit(0);
344 }
345
346 scripting_ops = script_spec__lookup(generate_script_lang);
347 if (!scripting_ops) {
348 fprintf(stderr, "invalid language specifier");
349 return -1;
350 }
351
94c744b6 352 perf_header__read(&session->header, input);
956ffd02
TZ
353 err = scripting_ops->generate_script("perf-trace");
354 goto out;
355 }
356
357 if (script_name) {
358 err = scripting_ops->start_script(script_name);
359 if (err)
360 goto out;
361 }
362
d8f66248 363 err = __cmd_trace(session);
956ffd02 364
d8f66248 365 perf_session__delete(session);
956ffd02
TZ
366 cleanup_scripting();
367out:
368 return err;
5f9c39dc 369}