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