diff options
Diffstat (limited to 'tools/perf/builtin-trace.c')
-rw-r--r-- | tools/perf/builtin-trace.c | 713 |
1 files changed, 525 insertions, 188 deletions
diff --git a/tools/perf/builtin-trace.c b/tools/perf/builtin-trace.c index 0c5e4f72f2ba..407041d20de0 100644 --- a/tools/perf/builtin-trace.c +++ b/tools/perf/builtin-trace.c | |||
@@ -5,259 +5,499 @@ | |||
5 | #include "util/symbol.h" | 5 | #include "util/symbol.h" |
6 | #include "util/thread.h" | 6 | #include "util/thread.h" |
7 | #include "util/header.h" | 7 | #include "util/header.h" |
8 | #include "util/exec_cmd.h" | ||
9 | #include "util/trace-event.h" | ||
10 | #include "util/session.h" | ||
8 | 11 | ||
9 | #include "util/parse-options.h" | 12 | static char const *script_name; |
13 | static char const *generate_script_lang; | ||
10 | 14 | ||
11 | #include "perf.h" | 15 | static int default_start_script(const char *script __unused, |
12 | #include "util/debug.h" | 16 | int argc __unused, |
17 | const char **argv __unused) | ||
18 | { | ||
19 | return 0; | ||
20 | } | ||
13 | 21 | ||
14 | #include "util/trace-event.h" | 22 | static int default_stop_script(void) |
23 | { | ||
24 | return 0; | ||
25 | } | ||
26 | |||
27 | static int default_generate_script(const char *outfile __unused) | ||
28 | { | ||
29 | return 0; | ||
30 | } | ||
31 | |||
32 | static struct scripting_ops default_scripting_ops = { | ||
33 | .start_script = default_start_script, | ||
34 | .stop_script = default_stop_script, | ||
35 | .process_event = print_event, | ||
36 | .generate_script = default_generate_script, | ||
37 | }; | ||
38 | |||
39 | static struct scripting_ops *scripting_ops; | ||
40 | |||
41 | static void setup_scripting(void) | ||
42 | { | ||
43 | /* make sure PERF_EXEC_PATH is set for scripts */ | ||
44 | perf_set_argv_exec_path(perf_exec_path()); | ||
45 | |||
46 | setup_perl_scripting(); | ||
47 | setup_python_scripting(); | ||
15 | 48 | ||
16 | static char const *input_name = "perf.data"; | 49 | scripting_ops = &default_scripting_ops; |
17 | static int input; | 50 | } |
18 | static unsigned long page_size; | 51 | |
19 | static unsigned long mmap_window = 32; | 52 | static int cleanup_scripting(void) |
53 | { | ||
54 | return scripting_ops->stop_script(); | ||
55 | } | ||
20 | 56 | ||
21 | static unsigned long total = 0; | 57 | #include "util/parse-options.h" |
22 | static unsigned long total_comm = 0; | ||
23 | 58 | ||
24 | static struct rb_root threads; | 59 | #include "perf.h" |
25 | static struct thread *last_match; | 60 | #include "util/debug.h" |
26 | 61 | ||
27 | static struct perf_header *header; | 62 | #include "util/trace-event.h" |
28 | static u64 sample_type; | 63 | #include "util/exec_cmd.h" |
29 | 64 | ||
65 | static char const *input_name = "perf.data"; | ||
30 | 66 | ||
31 | static int | 67 | static int process_sample_event(event_t *event, struct perf_session *session) |
32 | process_comm_event(event_t *event, unsigned long offset, unsigned long head) | ||
33 | { | 68 | { |
69 | struct sample_data data; | ||
34 | struct thread *thread; | 70 | struct thread *thread; |
35 | 71 | ||
36 | thread = threads__findnew(event->comm.pid, &threads, &last_match); | 72 | memset(&data, 0, sizeof(data)); |
73 | data.time = -1; | ||
74 | data.cpu = -1; | ||
75 | data.period = 1; | ||
37 | 76 | ||
38 | dump_printf("%p [%p]: PERF_RECORD_COMM: %s:%d\n", | 77 | event__parse_sample(event, session->sample_type, &data); |
39 | (void *)(offset + head), | ||
40 | (void *)(long)(event->header.size), | ||
41 | event->comm.comm, event->comm.pid); | ||
42 | 78 | ||
43 | if (thread == NULL || | 79 | dump_printf("(IP, %d): %d/%d: %#Lx period: %Ld\n", event->header.misc, |
44 | thread__set_comm(thread, event->comm.comm)) { | 80 | data.pid, data.tid, data.ip, data.period); |
45 | dump_printf("problem processing PERF_RECORD_COMM, skipping event.\n"); | 81 | |
82 | thread = perf_session__findnew(session, event->ip.pid); | ||
83 | if (thread == NULL) { | ||
84 | pr_debug("problem processing %d event, skipping it.\n", | ||
85 | event->header.type); | ||
46 | return -1; | 86 | return -1; |
47 | } | 87 | } |
48 | total_comm++; | ||
49 | 88 | ||
89 | if (session->sample_type & PERF_SAMPLE_RAW) { | ||
90 | /* | ||
91 | * FIXME: better resolve from pid from the struct trace_entry | ||
92 | * field, although it should be the same than this perf | ||
93 | * event pid | ||
94 | */ | ||
95 | scripting_ops->process_event(data.cpu, data.raw_data, | ||
96 | data.raw_size, | ||
97 | data.time, thread->comm); | ||
98 | } | ||
99 | |||
100 | session->events_stats.total += data.period; | ||
50 | return 0; | 101 | return 0; |
51 | } | 102 | } |
52 | 103 | ||
53 | static int | 104 | static struct perf_event_ops event_ops = { |
54 | process_sample_event(event_t *event, unsigned long offset, unsigned long head) | 105 | .sample = process_sample_event, |
106 | .comm = event__process_comm, | ||
107 | }; | ||
108 | |||
109 | static int __cmd_trace(struct perf_session *session) | ||
55 | { | 110 | { |
56 | char level; | 111 | return perf_session__process_events(session, &event_ops); |
57 | int show = 0; | 112 | } |
58 | struct dso *dso = NULL; | ||
59 | struct thread *thread; | ||
60 | u64 ip = event->ip.ip; | ||
61 | u64 timestamp = -1; | ||
62 | u32 cpu = -1; | ||
63 | u64 period = 1; | ||
64 | void *more_data = event->ip.__more_data; | ||
65 | int cpumode; | ||
66 | |||
67 | thread = threads__findnew(event->ip.pid, &threads, &last_match); | ||
68 | |||
69 | if (sample_type & PERF_SAMPLE_TIME) { | ||
70 | timestamp = *(u64 *)more_data; | ||
71 | more_data += sizeof(u64); | ||
72 | } | ||
73 | 113 | ||
74 | if (sample_type & PERF_SAMPLE_CPU) { | 114 | struct script_spec { |
75 | cpu = *(u32 *)more_data; | 115 | struct list_head node; |
76 | more_data += sizeof(u32); | 116 | struct scripting_ops *ops; |
77 | more_data += sizeof(u32); /* reserved */ | 117 | char spec[0]; |
78 | } | 118 | }; |
119 | |||
120 | LIST_HEAD(script_specs); | ||
121 | |||
122 | static struct script_spec *script_spec__new(const char *spec, | ||
123 | struct scripting_ops *ops) | ||
124 | { | ||
125 | struct script_spec *s = malloc(sizeof(*s) + strlen(spec) + 1); | ||
79 | 126 | ||
80 | if (sample_type & PERF_SAMPLE_PERIOD) { | 127 | if (s != NULL) { |
81 | period = *(u64 *)more_data; | 128 | strcpy(s->spec, spec); |
82 | more_data += sizeof(u64); | 129 | s->ops = ops; |
83 | } | 130 | } |
84 | 131 | ||
85 | dump_printf("%p [%p]: PERF_RECORD_SAMPLE (IP, %d): %d/%d: %p period: %Ld\n", | 132 | return s; |
86 | (void *)(offset + head), | 133 | } |
87 | (void *)(long)(event->header.size), | ||
88 | event->header.misc, | ||
89 | event->ip.pid, event->ip.tid, | ||
90 | (void *)(long)ip, | ||
91 | (long long)period); | ||
92 | 134 | ||
93 | dump_printf(" ... thread: %s:%d\n", thread->comm, thread->pid); | 135 | static void script_spec__delete(struct script_spec *s) |
136 | { | ||
137 | free(s->spec); | ||
138 | free(s); | ||
139 | } | ||
94 | 140 | ||
95 | if (thread == NULL) { | 141 | static void script_spec__add(struct script_spec *s) |
96 | eprintf("problem processing %d event, skipping it.\n", | 142 | { |
97 | event->header.type); | 143 | list_add_tail(&s->node, &script_specs); |
98 | return -1; | 144 | } |
99 | } | 145 | |
146 | static struct script_spec *script_spec__find(const char *spec) | ||
147 | { | ||
148 | struct script_spec *s; | ||
100 | 149 | ||
101 | cpumode = event->header.misc & PERF_RECORD_MISC_CPUMODE_MASK; | 150 | list_for_each_entry(s, &script_specs, node) |
151 | if (strcasecmp(s->spec, spec) == 0) | ||
152 | return s; | ||
153 | return NULL; | ||
154 | } | ||
102 | 155 | ||
103 | if (cpumode == PERF_RECORD_MISC_KERNEL) { | 156 | static struct script_spec *script_spec__findnew(const char *spec, |
104 | show = SHOW_KERNEL; | 157 | struct scripting_ops *ops) |
105 | level = 'k'; | 158 | { |
159 | struct script_spec *s = script_spec__find(spec); | ||
106 | 160 | ||
107 | dso = kernel_dso; | 161 | if (s) |
162 | return s; | ||
108 | 163 | ||
109 | dump_printf(" ...... dso: %s\n", dso->name); | 164 | s = script_spec__new(spec, ops); |
165 | if (!s) | ||
166 | goto out_delete_spec; | ||
110 | 167 | ||
111 | } else if (cpumode == PERF_RECORD_MISC_USER) { | 168 | script_spec__add(s); |
112 | 169 | ||
113 | show = SHOW_USER; | 170 | return s; |
114 | level = '.'; | ||
115 | 171 | ||
116 | } else { | 172 | out_delete_spec: |
117 | show = SHOW_HV; | 173 | script_spec__delete(s); |
118 | level = 'H'; | ||
119 | 174 | ||
120 | dso = hypervisor_dso; | 175 | return NULL; |
176 | } | ||
121 | 177 | ||
122 | dump_printf(" ...... dso: [hypervisor]\n"); | 178 | int script_spec_register(const char *spec, struct scripting_ops *ops) |
123 | } | 179 | { |
180 | struct script_spec *s; | ||
124 | 181 | ||
125 | if (sample_type & PERF_SAMPLE_RAW) { | 182 | s = script_spec__find(spec); |
126 | struct { | 183 | if (s) |
127 | u32 size; | 184 | return -1; |
128 | char data[0]; | ||
129 | } *raw = more_data; | ||
130 | 185 | ||
131 | /* | 186 | s = script_spec__findnew(spec, ops); |
132 | * FIXME: better resolve from pid from the struct trace_entry | 187 | if (!s) |
133 | * field, although it should be the same than this perf | 188 | return -1; |
134 | * event pid | ||
135 | */ | ||
136 | print_event(cpu, raw->data, raw->size, timestamp, thread->comm); | ||
137 | } | ||
138 | total += period; | ||
139 | 189 | ||
140 | return 0; | 190 | return 0; |
141 | } | 191 | } |
142 | 192 | ||
143 | static int | 193 | static struct scripting_ops *script_spec__lookup(const char *spec) |
144 | process_event(event_t *event, unsigned long offset, unsigned long head) | ||
145 | { | 194 | { |
146 | trace_event(event); | 195 | struct script_spec *s = script_spec__find(spec); |
196 | if (!s) | ||
197 | return NULL; | ||
147 | 198 | ||
148 | switch (event->header.type) { | 199 | return s->ops; |
149 | case PERF_RECORD_MMAP ... PERF_RECORD_LOST: | 200 | } |
150 | return 0; | ||
151 | 201 | ||
152 | case PERF_RECORD_COMM: | 202 | static void list_available_languages(void) |
153 | return process_comm_event(event, offset, head); | 203 | { |
204 | struct script_spec *s; | ||
154 | 205 | ||
155 | case PERF_RECORD_EXIT ... PERF_RECORD_READ: | 206 | fprintf(stderr, "\n"); |
156 | return 0; | 207 | fprintf(stderr, "Scripting language extensions (used in " |
208 | "perf trace -s [spec:]script.[spec]):\n\n"); | ||
157 | 209 | ||
158 | case PERF_RECORD_SAMPLE: | 210 | list_for_each_entry(s, &script_specs, node) |
159 | return process_sample_event(event, offset, head); | 211 | fprintf(stderr, " %-42s [%s]\n", s->spec, s->ops->name); |
160 | 212 | ||
161 | case PERF_RECORD_MAX: | 213 | fprintf(stderr, "\n"); |
162 | default: | 214 | } |
163 | return -1; | 215 | |
216 | static int parse_scriptname(const struct option *opt __used, | ||
217 | const char *str, int unset __used) | ||
218 | { | ||
219 | char spec[PATH_MAX]; | ||
220 | const char *script, *ext; | ||
221 | int len; | ||
222 | |||
223 | if (strcmp(str, "lang") == 0) { | ||
224 | list_available_languages(); | ||
225 | exit(0); | ||
226 | } | ||
227 | |||
228 | script = strchr(str, ':'); | ||
229 | if (script) { | ||
230 | len = script - str; | ||
231 | if (len >= PATH_MAX) { | ||
232 | fprintf(stderr, "invalid language specifier"); | ||
233 | return -1; | ||
234 | } | ||
235 | strncpy(spec, str, len); | ||
236 | spec[len] = '\0'; | ||
237 | scripting_ops = script_spec__lookup(spec); | ||
238 | if (!scripting_ops) { | ||
239 | fprintf(stderr, "invalid language specifier"); | ||
240 | return -1; | ||
241 | } | ||
242 | script++; | ||
243 | } else { | ||
244 | script = str; | ||
245 | ext = strchr(script, '.'); | ||
246 | if (!ext) { | ||
247 | fprintf(stderr, "invalid script extension"); | ||
248 | return -1; | ||
249 | } | ||
250 | scripting_ops = script_spec__lookup(++ext); | ||
251 | if (!scripting_ops) { | ||
252 | fprintf(stderr, "invalid script extension"); | ||
253 | return -1; | ||
254 | } | ||
164 | } | 255 | } |
165 | 256 | ||
257 | script_name = strdup(script); | ||
258 | |||
166 | return 0; | 259 | return 0; |
167 | } | 260 | } |
168 | 261 | ||
169 | static int __cmd_trace(void) | 262 | #define for_each_lang(scripts_dir, lang_dirent, lang_next) \ |
263 | while (!readdir_r(scripts_dir, &lang_dirent, &lang_next) && \ | ||
264 | lang_next) \ | ||
265 | if (lang_dirent.d_type == DT_DIR && \ | ||
266 | (strcmp(lang_dirent.d_name, ".")) && \ | ||
267 | (strcmp(lang_dirent.d_name, ".."))) | ||
268 | |||
269 | #define for_each_script(lang_dir, script_dirent, script_next) \ | ||
270 | while (!readdir_r(lang_dir, &script_dirent, &script_next) && \ | ||
271 | script_next) \ | ||
272 | if (script_dirent.d_type != DT_DIR) | ||
273 | |||
274 | |||
275 | #define RECORD_SUFFIX "-record" | ||
276 | #define REPORT_SUFFIX "-report" | ||
277 | |||
278 | struct script_desc { | ||
279 | struct list_head node; | ||
280 | char *name; | ||
281 | char *half_liner; | ||
282 | char *args; | ||
283 | }; | ||
284 | |||
285 | LIST_HEAD(script_descs); | ||
286 | |||
287 | static struct script_desc *script_desc__new(const char *name) | ||
170 | { | 288 | { |
171 | int ret, rc = EXIT_FAILURE; | 289 | struct script_desc *s = zalloc(sizeof(*s)); |
172 | unsigned long offset = 0; | ||
173 | unsigned long head = 0; | ||
174 | struct stat perf_stat; | ||
175 | event_t *event; | ||
176 | uint32_t size; | ||
177 | char *buf; | ||
178 | |||
179 | trace_report(); | ||
180 | register_idle_thread(&threads, &last_match); | ||
181 | |||
182 | input = open(input_name, O_RDONLY); | ||
183 | if (input < 0) { | ||
184 | perror("failed to open file"); | ||
185 | exit(-1); | ||
186 | } | ||
187 | 290 | ||
188 | ret = fstat(input, &perf_stat); | 291 | if (s != NULL) |
189 | if (ret < 0) { | 292 | s->name = strdup(name); |
190 | perror("failed to stat file"); | ||
191 | exit(-1); | ||
192 | } | ||
193 | 293 | ||
194 | if (!perf_stat.st_size) { | 294 | return s; |
195 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | 295 | } |
196 | exit(0); | 296 | |
197 | } | 297 | static void script_desc__delete(struct script_desc *s) |
198 | header = perf_header__read(input); | 298 | { |
199 | head = header->data_offset; | 299 | free(s->name); |
200 | sample_type = perf_header__sample_type(header); | 300 | free(s); |
301 | } | ||
302 | |||
303 | static void script_desc__add(struct script_desc *s) | ||
304 | { | ||
305 | list_add_tail(&s->node, &script_descs); | ||
306 | } | ||
307 | |||
308 | static struct script_desc *script_desc__find(const char *name) | ||
309 | { | ||
310 | struct script_desc *s; | ||
311 | |||
312 | list_for_each_entry(s, &script_descs, node) | ||
313 | if (strcasecmp(s->name, name) == 0) | ||
314 | return s; | ||
315 | return NULL; | ||
316 | } | ||
317 | |||
318 | static struct script_desc *script_desc__findnew(const char *name) | ||
319 | { | ||
320 | struct script_desc *s = script_desc__find(name); | ||
321 | |||
322 | if (s) | ||
323 | return s; | ||
324 | |||
325 | s = script_desc__new(name); | ||
326 | if (!s) | ||
327 | goto out_delete_desc; | ||
328 | |||
329 | script_desc__add(s); | ||
330 | |||
331 | return s; | ||
201 | 332 | ||
202 | if (!(sample_type & PERF_SAMPLE_RAW)) | 333 | out_delete_desc: |
203 | die("No trace sample to read. Did you call perf record " | 334 | script_desc__delete(s); |
204 | "without -R?"); | ||
205 | 335 | ||
206 | if (load_kernel() < 0) { | 336 | return NULL; |
207 | perror("failed to load kernel symbols"); | 337 | } |
208 | return EXIT_FAILURE; | 338 | |
339 | static char *ends_with(char *str, const char *suffix) | ||
340 | { | ||
341 | size_t suffix_len = strlen(suffix); | ||
342 | char *p = str; | ||
343 | |||
344 | if (strlen(str) > suffix_len) { | ||
345 | p = str + strlen(str) - suffix_len; | ||
346 | if (!strncmp(p, suffix, suffix_len)) | ||
347 | return p; | ||
209 | } | 348 | } |
210 | 349 | ||
211 | remap: | 350 | return NULL; |
212 | buf = (char *)mmap(NULL, page_size * mmap_window, PROT_READ, | 351 | } |
213 | MAP_SHARED, input, offset); | 352 | |
214 | if (buf == MAP_FAILED) { | 353 | static char *ltrim(char *str) |
215 | perror("failed to mmap file"); | 354 | { |
216 | exit(-1); | 355 | int len = strlen(str); |
356 | |||
357 | while (len && isspace(*str)) { | ||
358 | len--; | ||
359 | str++; | ||
217 | } | 360 | } |
218 | 361 | ||
219 | more: | 362 | return str; |
220 | event = (event_t *)(buf + head); | 363 | } |
221 | 364 | ||
222 | if (head + event->header.size >= page_size * mmap_window) { | 365 | static int read_script_info(struct script_desc *desc, const char *filename) |
223 | unsigned long shift = page_size * (head / page_size); | 366 | { |
224 | int res; | 367 | char line[BUFSIZ], *p; |
368 | FILE *fp; | ||
225 | 369 | ||
226 | res = munmap(buf, page_size * mmap_window); | 370 | fp = fopen(filename, "r"); |
227 | assert(res == 0); | 371 | if (!fp) |
372 | return -1; | ||
228 | 373 | ||
229 | offset += shift; | 374 | while (fgets(line, sizeof(line), fp)) { |
230 | head -= shift; | 375 | p = ltrim(line); |
231 | goto remap; | 376 | if (strlen(p) == 0) |
377 | continue; | ||
378 | if (*p != '#') | ||
379 | continue; | ||
380 | p++; | ||
381 | if (strlen(p) && *p == '!') | ||
382 | continue; | ||
383 | |||
384 | p = ltrim(p); | ||
385 | if (strlen(p) && p[strlen(p) - 1] == '\n') | ||
386 | p[strlen(p) - 1] = '\0'; | ||
387 | |||
388 | if (!strncmp(p, "description:", strlen("description:"))) { | ||
389 | p += strlen("description:"); | ||
390 | desc->half_liner = strdup(ltrim(p)); | ||
391 | continue; | ||
392 | } | ||
393 | |||
394 | if (!strncmp(p, "args:", strlen("args:"))) { | ||
395 | p += strlen("args:"); | ||
396 | desc->args = strdup(ltrim(p)); | ||
397 | continue; | ||
398 | } | ||
232 | } | 399 | } |
233 | 400 | ||
234 | size = event->header.size; | 401 | fclose(fp); |
235 | |||
236 | if (!size || process_event(event, offset, head) < 0) { | ||
237 | 402 | ||
238 | /* | 403 | return 0; |
239 | * assume we lost track of the stream, check alignment, and | 404 | } |
240 | * increment a single u64 in the hope to catch on again 'soon'. | ||
241 | */ | ||
242 | 405 | ||
243 | if (unlikely(head & 7)) | 406 | static int list_available_scripts(const struct option *opt __used, |
244 | head &= ~7ULL; | 407 | const char *s __used, int unset __used) |
408 | { | ||
409 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | ||
410 | char scripts_path[MAXPATHLEN]; | ||
411 | DIR *scripts_dir, *lang_dir; | ||
412 | char script_path[MAXPATHLEN]; | ||
413 | char lang_path[MAXPATHLEN]; | ||
414 | struct script_desc *desc; | ||
415 | char first_half[BUFSIZ]; | ||
416 | char *script_root; | ||
417 | char *str; | ||
418 | |||
419 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | ||
420 | |||
421 | scripts_dir = opendir(scripts_path); | ||
422 | if (!scripts_dir) | ||
423 | return -1; | ||
245 | 424 | ||
246 | size = 8; | 425 | for_each_lang(scripts_dir, lang_dirent, lang_next) { |
426 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | ||
427 | lang_dirent.d_name); | ||
428 | lang_dir = opendir(lang_path); | ||
429 | if (!lang_dir) | ||
430 | continue; | ||
431 | |||
432 | for_each_script(lang_dir, script_dirent, script_next) { | ||
433 | script_root = strdup(script_dirent.d_name); | ||
434 | str = ends_with(script_root, REPORT_SUFFIX); | ||
435 | if (str) { | ||
436 | *str = '\0'; | ||
437 | desc = script_desc__findnew(script_root); | ||
438 | snprintf(script_path, MAXPATHLEN, "%s/%s", | ||
439 | lang_path, script_dirent.d_name); | ||
440 | read_script_info(desc, script_path); | ||
441 | } | ||
442 | free(script_root); | ||
443 | } | ||
247 | } | 444 | } |
248 | 445 | ||
249 | head += size; | 446 | fprintf(stdout, "List of available trace scripts:\n"); |
447 | list_for_each_entry(desc, &script_descs, node) { | ||
448 | sprintf(first_half, "%s %s", desc->name, | ||
449 | desc->args ? desc->args : ""); | ||
450 | fprintf(stdout, " %-36s %s\n", first_half, | ||
451 | desc->half_liner ? desc->half_liner : ""); | ||
452 | } | ||
250 | 453 | ||
251 | if (offset + head < (unsigned long)perf_stat.st_size) | 454 | exit(0); |
252 | goto more; | 455 | } |
253 | 456 | ||
254 | rc = EXIT_SUCCESS; | 457 | static char *get_script_path(const char *script_root, const char *suffix) |
255 | close(input); | 458 | { |
459 | struct dirent *script_next, *lang_next, script_dirent, lang_dirent; | ||
460 | char scripts_path[MAXPATHLEN]; | ||
461 | char script_path[MAXPATHLEN]; | ||
462 | DIR *scripts_dir, *lang_dir; | ||
463 | char lang_path[MAXPATHLEN]; | ||
464 | char *str, *__script_root; | ||
465 | char *path = NULL; | ||
466 | |||
467 | snprintf(scripts_path, MAXPATHLEN, "%s/scripts", perf_exec_path()); | ||
468 | |||
469 | scripts_dir = opendir(scripts_path); | ||
470 | if (!scripts_dir) | ||
471 | return NULL; | ||
472 | |||
473 | for_each_lang(scripts_dir, lang_dirent, lang_next) { | ||
474 | snprintf(lang_path, MAXPATHLEN, "%s/%s/bin", scripts_path, | ||
475 | lang_dirent.d_name); | ||
476 | lang_dir = opendir(lang_path); | ||
477 | if (!lang_dir) | ||
478 | continue; | ||
479 | |||
480 | for_each_script(lang_dir, script_dirent, script_next) { | ||
481 | __script_root = strdup(script_dirent.d_name); | ||
482 | str = ends_with(__script_root, suffix); | ||
483 | if (str) { | ||
484 | *str = '\0'; | ||
485 | if (strcmp(__script_root, script_root)) | ||
486 | continue; | ||
487 | snprintf(script_path, MAXPATHLEN, "%s/%s", | ||
488 | lang_path, script_dirent.d_name); | ||
489 | path = strdup(script_path); | ||
490 | free(__script_root); | ||
491 | break; | ||
492 | } | ||
493 | free(__script_root); | ||
494 | } | ||
495 | } | ||
256 | 496 | ||
257 | return rc; | 497 | return path; |
258 | } | 498 | } |
259 | 499 | ||
260 | static const char * const annotate_usage[] = { | 500 | static const char * const trace_usage[] = { |
261 | "perf trace [<options>] <command>", | 501 | "perf trace [<options>] <command>", |
262 | NULL | 502 | NULL |
263 | }; | 503 | }; |
@@ -267,25 +507,122 @@ static const struct option options[] = { | |||
267 | "dump raw trace in ASCII"), | 507 | "dump raw trace in ASCII"), |
268 | OPT_BOOLEAN('v', "verbose", &verbose, | 508 | OPT_BOOLEAN('v', "verbose", &verbose, |
269 | "be more verbose (show symbol address, etc)"), | 509 | "be more verbose (show symbol address, etc)"), |
510 | OPT_BOOLEAN('L', "Latency", &latency_format, | ||
511 | "show latency attributes (irqs/preemption disabled, etc)"), | ||
512 | OPT_CALLBACK_NOOPT('l', "list", NULL, NULL, "list available scripts", | ||
513 | list_available_scripts), | ||
514 | OPT_CALLBACK('s', "script", NULL, "name", | ||
515 | "script file name (lang:script name, script name, or *)", | ||
516 | parse_scriptname), | ||
517 | OPT_STRING('g', "gen-script", &generate_script_lang, "lang", | ||
518 | "generate perf-trace.xx script in specified language"), | ||
519 | OPT_STRING('i', "input", &input_name, "file", | ||
520 | "input file name"), | ||
521 | |||
270 | OPT_END() | 522 | OPT_END() |
271 | }; | 523 | }; |
272 | 524 | ||
273 | int cmd_trace(int argc, const char **argv, const char *prefix __used) | 525 | int cmd_trace(int argc, const char **argv, const char *prefix __used) |
274 | { | 526 | { |
275 | symbol__init(); | 527 | struct perf_session *session; |
276 | page_size = getpagesize(); | 528 | const char *suffix = NULL; |
529 | const char **__argv; | ||
530 | char *script_path; | ||
531 | int i, err; | ||
532 | |||
533 | if (argc >= 2 && strncmp(argv[1], "rec", strlen("rec")) == 0) { | ||
534 | if (argc < 3) { | ||
535 | fprintf(stderr, | ||
536 | "Please specify a record script\n"); | ||
537 | return -1; | ||
538 | } | ||
539 | suffix = RECORD_SUFFIX; | ||
540 | } | ||
277 | 541 | ||
278 | argc = parse_options(argc, argv, options, annotate_usage, 0); | 542 | if (argc >= 2 && strncmp(argv[1], "rep", strlen("rep")) == 0) { |
279 | if (argc) { | 543 | if (argc < 3) { |
280 | /* | 544 | fprintf(stderr, |
281 | * Special case: if there's an argument left then assume tha | 545 | "Please specify a report script\n"); |
282 | * it's a symbol filter: | 546 | return -1; |
283 | */ | 547 | } |
284 | if (argc > 1) | 548 | suffix = REPORT_SUFFIX; |
285 | usage_with_options(annotate_usage, options); | 549 | } |
550 | |||
551 | if (suffix) { | ||
552 | script_path = get_script_path(argv[2], suffix); | ||
553 | if (!script_path) { | ||
554 | fprintf(stderr, "script not found\n"); | ||
555 | return -1; | ||
556 | } | ||
557 | |||
558 | __argv = malloc((argc + 1) * sizeof(const char *)); | ||
559 | __argv[0] = "/bin/sh"; | ||
560 | __argv[1] = script_path; | ||
561 | for (i = 3; i < argc; i++) | ||
562 | __argv[i - 1] = argv[i]; | ||
563 | __argv[argc - 1] = NULL; | ||
564 | |||
565 | execvp("/bin/sh", (char **)__argv); | ||
566 | exit(-1); | ||
567 | } | ||
568 | |||
569 | setup_scripting(); | ||
570 | |||
571 | argc = parse_options(argc, argv, options, trace_usage, | ||
572 | PARSE_OPT_STOP_AT_NON_OPTION); | ||
573 | |||
574 | if (symbol__init() < 0) | ||
575 | return -1; | ||
576 | if (!script_name) | ||
577 | setup_pager(); | ||
578 | |||
579 | session = perf_session__new(input_name, O_RDONLY, 0); | ||
580 | if (session == NULL) | ||
581 | return -ENOMEM; | ||
582 | |||
583 | if (!perf_session__has_traces(session, "record -R")) | ||
584 | return -EINVAL; | ||
585 | |||
586 | if (generate_script_lang) { | ||
587 | struct stat perf_stat; | ||
588 | |||
589 | int input = open(input_name, O_RDONLY); | ||
590 | if (input < 0) { | ||
591 | perror("failed to open file"); | ||
592 | exit(-1); | ||
593 | } | ||
594 | |||
595 | err = fstat(input, &perf_stat); | ||
596 | if (err < 0) { | ||
597 | perror("failed to stat file"); | ||
598 | exit(-1); | ||
599 | } | ||
600 | |||
601 | if (!perf_stat.st_size) { | ||
602 | fprintf(stderr, "zero-sized file, nothing to do!\n"); | ||
603 | exit(0); | ||
604 | } | ||
605 | |||
606 | scripting_ops = script_spec__lookup(generate_script_lang); | ||
607 | if (!scripting_ops) { | ||
608 | fprintf(stderr, "invalid language specifier"); | ||
609 | return -1; | ||
610 | } | ||
611 | |||
612 | err = scripting_ops->generate_script("perf-trace"); | ||
613 | goto out; | ||
614 | } | ||
615 | |||
616 | if (script_name) { | ||
617 | err = scripting_ops->start_script(script_name, argc, argv); | ||
618 | if (err) | ||
619 | goto out; | ||
286 | } | 620 | } |
287 | 621 | ||
288 | setup_pager(); | 622 | err = __cmd_trace(session); |
289 | 623 | ||
290 | return __cmd_trace(); | 624 | perf_session__delete(session); |
625 | cleanup_scripting(); | ||
626 | out: | ||
627 | return err; | ||
291 | } | 628 | } |