builtin-trace.c (709334c87dbdb44150ce436b3d13c814db0dcae9) builtin-trace.c (586bc5cce88be993dad584c3936c49f945368551)
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"
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
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"
8#include "util/exec_cmd.h"
9#include "util/trace-event.h"
10#include "util/session.h"
10
11static char const *script_name;
12static char const *generate_script_lang;
13
11
12static char const *script_name;
13static char const *generate_script_lang;
14
14static int default_start_script(const char *script __attribute((unused)))
15static int default_start_script(const char *script __unused,
16 int argc __unused,
17 const char **argv __unused)
15{
16 return 0;
17}
18
19static int default_stop_script(void)
20{
21 return 0;
22}
23
18{
19 return 0;
20}
21
22static int default_stop_script(void)
23{
24 return 0;
25}
26
24static int default_generate_script(const char *outfile __attribute ((unused)))
27static int default_generate_script(const char *outfile __unused)
25{
26 return 0;
27}
28
29static struct scripting_ops default_scripting_ops = {
30 .start_script = default_start_script,
31 .stop_script = default_stop_script,
32 .process_event = print_event,

--- 18 unchanged lines hidden (view full) ---

51}
52
53#include "util/parse-options.h"
54
55#include "perf.h"
56#include "util/debug.h"
57
58#include "util/trace-event.h"
28{
29 return 0;
30}
31
32static struct scripting_ops default_scripting_ops = {
33 .start_script = default_start_script,
34 .stop_script = default_stop_script,
35 .process_event = print_event,

--- 18 unchanged lines hidden (view full) ---

54}
55
56#include "util/parse-options.h"
57
58#include "perf.h"
59#include "util/debug.h"
60
61#include "util/trace-event.h"
59#include "util/data_map.h"
60#include "util/exec_cmd.h"
61
62static char const *input_name = "perf.data";
63
62#include "util/exec_cmd.h"
63
64static char const *input_name = "perf.data";
65
64static struct perf_header *header;
65static u64 sample_type;
66
67static int process_sample_event(event_t *event)
66static int process_sample_event(event_t *event, struct perf_session *session)
68{
69 struct sample_data data;
70 struct thread *thread;
71
72 memset(&data, 0, sizeof(data));
73 data.time = -1;
74 data.cpu = -1;
75 data.period = 1;
76
67{
68 struct sample_data data;
69 struct thread *thread;
70
71 memset(&data, 0, sizeof(data));
72 data.time = -1;
73 data.cpu = -1;
74 data.period = 1;
75
77 event__parse_sample(event, sample_type, &data);
76 event__parse_sample(event, session->sample_type, &data);
78
79 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
80 event->header.misc,
81 data.pid, data.tid,
82 (void *)(long)data.ip,
83 (long long)data.period);
84
77
78 dump_printf("(IP, %d): %d/%d: %p period: %Ld\n",
79 event->header.misc,
80 data.pid, data.tid,
81 (void *)(long)data.ip,
82 (long long)data.period);
83
85 thread = threads__findnew(event->ip.pid);
84 thread = perf_session__findnew(session, event->ip.pid);
86 if (thread == NULL) {
87 pr_debug("problem processing %d event, skipping it.\n",
88 event->header.type);
89 return -1;
90 }
91
85 if (thread == NULL) {
86 pr_debug("problem processing %d event, skipping it.\n",
87 event->header.type);
88 return -1;
89 }
90
92 if (sample_type & PERF_SAMPLE_RAW) {
91 if (session->sample_type & PERF_SAMPLE_RAW) {
93 /*
94 * FIXME: better resolve from pid from the struct trace_entry
95 * field, although it should be the same than this perf
96 * event pid
97 */
98 scripting_ops->process_event(data.cpu, data.raw_data,
99 data.raw_size,
100 data.time, thread->comm);
101 }
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 */
97 scripting_ops->process_event(data.cpu, data.raw_data,
98 data.raw_size,
99 data.time, thread->comm);
100 }
102 event__stats.total += data.period;
103
101
102 session->events_stats.total += data.period;
104 return 0;
105}
106
103 return 0;
104}
105
107static int sample_type_check(u64 type)
106static int sample_type_check(struct perf_session *session)
108{
107{
109 sample_type = type;
110
111 if (!(sample_type & PERF_SAMPLE_RAW)) {
108 if (!(session->sample_type & PERF_SAMPLE_RAW)) {
112 fprintf(stderr,
113 "No trace sample to read. Did you call perf record "
114 "without -R?");
115 return -1;
116 }
117
118 return 0;
119}
120
109 fprintf(stderr,
110 "No trace sample to read. Did you call perf record "
111 "without -R?");
112 return -1;
113 }
114
115 return 0;
116}
117
121static struct perf_file_handler file_handler = {
118static struct perf_event_ops event_ops = {
122 .process_sample_event = process_sample_event,
123 .process_comm_event = event__process_comm,
124 .sample_type_check = sample_type_check,
125};
126
119 .process_sample_event = process_sample_event,
120 .process_comm_event = event__process_comm,
121 .sample_type_check = sample_type_check,
122};
123
127static int __cmd_trace(void)
124static int __cmd_trace(struct perf_session *session)
128{
125{
129 register_idle_thread();
130 register_perf_file_handler(&file_handler);
131
132 return mmap_dispatch_perf_file(&header, input_name,
133 0, 0, &event__cwdlen, &event__cwd);
126 return perf_session__process_events(session, &event_ops);
134}
135
136struct script_spec {
137 struct list_head node;
138 struct scripting_ops *ops;
139 char spec[0];
140};
141

--- 158 unchanged lines hidden (view full) ---

300 "generate perf-trace.xx script in specified language"),
301
302 OPT_END()
303};
304
305int cmd_trace(int argc, const char **argv, const char *prefix __used)
306{
307 int err;
127}
128
129struct script_spec {
130 struct list_head node;
131 struct scripting_ops *ops;
132 char spec[0];
133};
134

--- 158 unchanged lines hidden (view full) ---

293 "generate perf-trace.xx script in specified language"),
294
295 OPT_END()
296};
297
298int cmd_trace(int argc, const char **argv, const char *prefix __used)
299{
300 int err;
301 struct perf_session *session;
308
309 symbol__init(0);
310
311 setup_scripting();
312
302
303 symbol__init(0);
304
305 setup_scripting();
306
313 argc = parse_options(argc, argv, options, annotate_usage, 0);
314 if (argc) {
315 /*
316 * Special case: if there's an argument left then assume tha
317 * it's a symbol filter:
318 */
319 if (argc > 1)
320 usage_with_options(annotate_usage, options);
321 }
307 argc = parse_options(argc, argv, options, annotate_usage,
308 PARSE_OPT_STOP_AT_NON_OPTION);
322
323 setup_pager();
324
309
310 setup_pager();
311
312 session = perf_session__new(input_name, O_RDONLY, 0, NULL);
313 if (session == NULL)
314 return -ENOMEM;
315
325 if (generate_script_lang) {
326 struct stat perf_stat;
327
328 int input = open(input_name, O_RDONLY);
329 if (input < 0) {
330 perror("failed to open file");
331 exit(-1);
332 }

--- 10 unchanged lines hidden (view full) ---

343 }
344
345 scripting_ops = script_spec__lookup(generate_script_lang);
346 if (!scripting_ops) {
347 fprintf(stderr, "invalid language specifier");
348 return -1;
349 }
350
316 if (generate_script_lang) {
317 struct stat perf_stat;
318
319 int input = open(input_name, O_RDONLY);
320 if (input < 0) {
321 perror("failed to open file");
322 exit(-1);
323 }

--- 10 unchanged lines hidden (view full) ---

334 }
335
336 scripting_ops = script_spec__lookup(generate_script_lang);
337 if (!scripting_ops) {
338 fprintf(stderr, "invalid language specifier");
339 return -1;
340 }
341
351 header = perf_header__new();
352 if (header == NULL)
353 return -1;
354
355 perf_header__read(header, input);
342 perf_header__read(&session->header, input);
356 err = scripting_ops->generate_script("perf-trace");
357 goto out;
358 }
359
360 if (script_name) {
343 err = scripting_ops->generate_script("perf-trace");
344 goto out;
345 }
346
347 if (script_name) {
361 err = scripting_ops->start_script(script_name);
348 err = scripting_ops->start_script(script_name, argc, argv);
362 if (err)
363 goto out;
364 }
365
349 if (err)
350 goto out;
351 }
352
366 err = __cmd_trace();
353 err = __cmd_trace(session);
367
354
355 perf_session__delete(session);
368 cleanup_scripting();
369out:
370 return err;
371}
356 cleanup_scripting();
357out:
358 return err;
359}