callsite: support instruction pointer field
[babeltrace.git] / formats / ctf-text / ctf-text.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * CTF Text Format registration.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a copy
11 * of this software and associated documentation files (the "Software"), to deal
12 * in the Software without restriction, including without limitation the rights
13 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14 * copies of the Software, and to permit persons to whom the Software is
15 * furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf-text/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
26 #include <inttypes.h>
27 #include <sys/mman.h>
28 #include <errno.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <fcntl.h>
32 #include <dirent.h>
33 #include <glib.h>
34 #include <unistd.h>
35 #include <stdlib.h>
36
37 #define NSEC_PER_SEC 1000000000ULL
38
39 int opt_all_field_names,
40 opt_scope_field_names,
41 opt_header_field_names,
42 opt_context_field_names,
43 opt_payload_field_names,
44 opt_all_fields,
45 opt_trace_field,
46 opt_trace_domain_field,
47 opt_trace_procname_field,
48 opt_trace_vpid_field,
49 opt_trace_hostname_field,
50 opt_trace_default_fields = 1,
51 opt_loglevel_field,
52 opt_emf_field,
53 opt_callsite_field,
54 opt_delta_field = 1;
55
56 enum field_item {
57 ITEM_SCOPE,
58 ITEM_HEADER,
59 ITEM_CONTEXT,
60 ITEM_PAYLOAD,
61 };
62
63 enum bt_loglevel {
64 BT_LOGLEVEL_EMERG = 0,
65 BT_LOGLEVEL_ALERT = 1,
66 BT_LOGLEVEL_CRIT = 2,
67 BT_LOGLEVEL_ERR = 3,
68 BT_LOGLEVEL_WARNING = 4,
69 BT_LOGLEVEL_NOTICE = 5,
70 BT_LOGLEVEL_INFO = 6,
71 BT_LOGLEVEL_DEBUG_SYSTEM = 7,
72 BT_LOGLEVEL_DEBUG_PROGRAM = 8,
73 BT_LOGLEVEL_DEBUG_PROCESS = 9,
74 BT_LOGLEVEL_DEBUG_MODULE = 10,
75 BT_LOGLEVEL_DEBUG_UNIT = 11,
76 BT_LOGLEVEL_DEBUG_FUNCTION = 12,
77 BT_LOGLEVEL_DEBUG_LINE = 13,
78 BT_LOGLEVEL_DEBUG = 14,
79 };
80
81
82 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
83 void (*packet_seek)(struct stream_pos *pos, size_t index,
84 int whence), FILE *metadata_fp);
85 void ctf_text_close_trace(struct trace_descriptor *descriptor);
86
87 static
88 rw_dispatch write_dispatch_table[] = {
89 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
90 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
91 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
92 [ CTF_TYPE_STRING ] = ctf_text_string_write,
93 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
94 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
95 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
96 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
97 };
98
99 static
100 struct format ctf_text_format = {
101 .open_trace = ctf_text_open_trace,
102 .close_trace = ctf_text_close_trace,
103 };
104
105 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
106 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
107 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
108 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
109 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
110
111 static
112 void __attribute__((constructor)) init_quarks(void)
113 {
114 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
115 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
116 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
117 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
118 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
119 }
120
121 static
122 struct ctf_callsite_dups *ctf_trace_callsite_lookup(struct ctf_trace *trace,
123 GQuark callsite_name)
124 {
125 return g_hash_table_lookup(trace->callsites,
126 (gpointer) (unsigned long) callsite_name);
127 }
128
129 int print_field(struct definition *definition)
130 {
131 /* Print all fields in verbose mode */
132 if (babeltrace_verbose)
133 return 1;
134
135 /* Filter out part of the packet context */
136 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
137 return 0;
138 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
139 return 0;
140 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
141 return 0;
142 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
143 return 0;
144 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
145 return 0;
146
147 return 1;
148 }
149
150 static
151 void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
152 {
153 switch (item) {
154 case ITEM_SCOPE:
155 if (opt_all_field_names || opt_scope_field_names)
156 pos->print_names = 1;
157 else
158 pos->print_names = 0;
159 break;
160 case ITEM_HEADER:
161 if (opt_all_field_names || opt_header_field_names)
162 pos->print_names = 1;
163 else
164 pos->print_names = 0;
165 break;
166 case ITEM_CONTEXT:
167 if (opt_all_field_names || opt_context_field_names)
168 pos->print_names = 1;
169 else
170 pos->print_names = 0;
171 break;
172 case ITEM_PAYLOAD:
173 if (opt_all_field_names || opt_payload_field_names)
174 pos->print_names = 1;
175 else
176 pos->print_names = 0;
177
178 break;
179 default:
180 assert(0);
181 }
182 }
183
184 static
185 const char *print_loglevel(int value)
186 {
187 switch (value) {
188 case -1:
189 return "";
190 case BT_LOGLEVEL_EMERG:
191 return "TRACE_EMERG";
192 case BT_LOGLEVEL_ALERT:
193 return "TRACE_ALERT";
194 case BT_LOGLEVEL_CRIT:
195 return "TRACE_CRIT";
196 case BT_LOGLEVEL_ERR:
197 return "TRACE_ERR";
198 case BT_LOGLEVEL_WARNING:
199 return "TRACE_WARNING";
200 case BT_LOGLEVEL_NOTICE:
201 return "TRACE_NOTICE";
202 case BT_LOGLEVEL_INFO:
203 return "TRACE_INFO";
204 case BT_LOGLEVEL_DEBUG_SYSTEM:
205 return "TRACE_DEBUG_SYSTEM";
206 case BT_LOGLEVEL_DEBUG_PROGRAM:
207 return "TRACE_DEBUG_PROGRAM";
208 case BT_LOGLEVEL_DEBUG_PROCESS:
209 return "TRACE_DEBUG_PROCESS";
210 case BT_LOGLEVEL_DEBUG_MODULE:
211 return "TRACE_DEBUG_MODULE";
212 case BT_LOGLEVEL_DEBUG_UNIT:
213 return "TRACE_DEBUG_UNIT";
214 case BT_LOGLEVEL_DEBUG_FUNCTION:
215 return "TRACE_DEBUG_FUNCTION";
216 case BT_LOGLEVEL_DEBUG_LINE:
217 return "TRACE_DEBUG_LINE";
218 case BT_LOGLEVEL_DEBUG:
219 return "TRACE_DEBUG";
220 default:
221 return "<<UNKNOWN>>";
222 }
223 }
224
225 static
226 int ctf_text_write_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
227
228 {
229 struct ctf_text_stream_pos *pos =
230 container_of(ppos, struct ctf_text_stream_pos, parent);
231 struct ctf_stream_declaration *stream_class = stream->stream_class;
232 int field_nr_saved;
233 struct ctf_event_declaration *event_class;
234 struct ctf_event_definition *event;
235 uint64_t id;
236 int ret;
237 int dom_print = 0;
238
239 id = stream->event_id;
240
241 if (id >= stream_class->events_by_id->len) {
242 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
243 return -EINVAL;
244 }
245 event = g_ptr_array_index(stream->events_by_id, id);
246 if (!event) {
247 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
248 return -EINVAL;
249 }
250 event_class = g_ptr_array_index(stream_class->events_by_id, id);
251 if (!event_class) {
252 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
253 return -EINVAL;
254 }
255
256 /* Print events discarded */
257 if (stream->events_discarded) {
258 fflush(pos->fp);
259 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events between [",
260 stream->events_discarded);
261 if (opt_clock_cycles) {
262 ctf_print_timestamp(stderr, stream,
263 stream->prev_cycles_timestamp);
264 fprintf(stderr, "] and [");
265 ctf_print_timestamp(stderr, stream,
266 stream->prev_cycles_timestamp_end);
267 } else {
268 ctf_print_timestamp(stderr, stream,
269 stream->prev_real_timestamp);
270 fprintf(stderr, "] and [");
271 ctf_print_timestamp(stderr, stream,
272 stream->prev_real_timestamp_end);
273 }
274 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
275 fflush(stderr);
276 stream->events_discarded = 0;
277 }
278
279 if (stream->has_timestamp) {
280 set_field_names_print(pos, ITEM_HEADER);
281 if (pos->print_names)
282 fprintf(pos->fp, "timestamp = ");
283 else
284 fprintf(pos->fp, "[");
285 if (opt_clock_cycles) {
286 ctf_print_timestamp(pos->fp, stream, stream->cycles_timestamp);
287 } else {
288 ctf_print_timestamp(pos->fp, stream, stream->real_timestamp);
289 }
290 if (!pos->print_names)
291 fprintf(pos->fp, "]");
292
293 if (pos->print_names)
294 fprintf(pos->fp, ", ");
295 else
296 fprintf(pos->fp, " ");
297 }
298 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
299 uint64_t delta, delta_sec, delta_nsec;
300
301 set_field_names_print(pos, ITEM_HEADER);
302 if (pos->print_names)
303 fprintf(pos->fp, "delta = ");
304 else
305 fprintf(pos->fp, "(");
306 if (pos->last_real_timestamp != -1ULL) {
307 delta = stream->real_timestamp - pos->last_real_timestamp;
308 delta_sec = delta / NSEC_PER_SEC;
309 delta_nsec = delta % NSEC_PER_SEC;
310 fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64,
311 delta_sec, delta_nsec);
312 } else {
313 fprintf(pos->fp, "+?.?????????");
314 }
315 if (!pos->print_names)
316 fprintf(pos->fp, ")");
317
318 if (pos->print_names)
319 fprintf(pos->fp, ", ");
320 else
321 fprintf(pos->fp, " ");
322 pos->last_real_timestamp = stream->real_timestamp;
323 pos->last_cycles_timestamp = stream->cycles_timestamp;
324 }
325
326 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
327 set_field_names_print(pos, ITEM_HEADER);
328 if (pos->print_names) {
329 fprintf(pos->fp, "trace = ");
330 }
331 fprintf(pos->fp, "%s", stream_class->trace->path);
332 if (pos->print_names)
333 fprintf(pos->fp, ", ");
334 else
335 fprintf(pos->fp, " ");
336 }
337 if ((opt_trace_hostname_field || opt_all_fields || opt_trace_default_fields)
338 && stream_class->trace->env.hostname[0] != '\0') {
339 set_field_names_print(pos, ITEM_HEADER);
340 if (pos->print_names) {
341 fprintf(pos->fp, "trace:hostname = ");
342 }
343 fprintf(pos->fp, "%s", stream_class->trace->env.hostname);
344 if (pos->print_names)
345 fprintf(pos->fp, ", ");
346 dom_print = 1;
347 }
348 if ((opt_trace_domain_field || opt_all_fields) && stream_class->trace->env.domain[0] != '\0') {
349 set_field_names_print(pos, ITEM_HEADER);
350 if (pos->print_names) {
351 fprintf(pos->fp, "trace:domain = ");
352 }
353 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
354 if (pos->print_names)
355 fprintf(pos->fp, ", ");
356 dom_print = 1;
357 }
358 if ((opt_trace_procname_field || opt_all_fields || opt_trace_default_fields)
359 && stream_class->trace->env.procname[0] != '\0') {
360 set_field_names_print(pos, ITEM_HEADER);
361 if (pos->print_names) {
362 fprintf(pos->fp, "trace:procname = ");
363 } else if (dom_print) {
364 fprintf(pos->fp, ":");
365 }
366 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
367 if (pos->print_names)
368 fprintf(pos->fp, ", ");
369 dom_print = 1;
370 }
371 if ((opt_trace_vpid_field || opt_all_fields || opt_trace_default_fields)
372 && stream_class->trace->env.vpid != -1) {
373 set_field_names_print(pos, ITEM_HEADER);
374 if (pos->print_names) {
375 fprintf(pos->fp, "trace:vpid = ");
376 } else if (dom_print) {
377 fprintf(pos->fp, ":");
378 }
379 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
380 if (pos->print_names)
381 fprintf(pos->fp, ", ");
382 dom_print = 1;
383 }
384 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
385 set_field_names_print(pos, ITEM_HEADER);
386 if (pos->print_names) {
387 fprintf(pos->fp, "loglevel = ");
388 } else if (dom_print) {
389 fprintf(pos->fp, ":");
390 }
391 fprintf(pos->fp, "%s (%d)",
392 print_loglevel(event_class->loglevel),
393 event_class->loglevel);
394 if (pos->print_names)
395 fprintf(pos->fp, ", ");
396 dom_print = 1;
397 }
398 if ((opt_emf_field || opt_all_fields) && event_class->model_emf_uri) {
399 set_field_names_print(pos, ITEM_HEADER);
400 if (pos->print_names) {
401 fprintf(pos->fp, "model.emf.uri = ");
402 } else if (dom_print) {
403 fprintf(pos->fp, ":");
404 }
405 fprintf(pos->fp, "\"%s\"",
406 g_quark_to_string(event_class->model_emf_uri));
407 if (pos->print_names)
408 fprintf(pos->fp, ", ");
409 dom_print = 1;
410 }
411 if ((opt_callsite_field || opt_all_fields)) {
412 struct ctf_callsite_dups *cs_dups;
413 struct ctf_callsite *callsite;
414
415 cs_dups = ctf_trace_callsite_lookup(stream_class->trace,
416 event_class->name);
417 if (cs_dups) {
418 int i = 0;
419
420 set_field_names_print(pos, ITEM_HEADER);
421 if (pos->print_names) {
422 fprintf(pos->fp, "callsite = ");
423 } else if (dom_print) {
424 fprintf(pos->fp, ":");
425 }
426 fprintf(pos->fp, "[");
427 bt_list_for_each_entry(callsite, &cs_dups->head, node) {
428 if (i != 0)
429 fprintf(pos->fp, ",");
430 if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
431 fprintf(pos->fp, "%s@0x%" PRIx64 ":%s:%" PRIu64 "",
432 callsite->func, callsite->ip, callsite->file,
433 callsite->line);
434 } else {
435 fprintf(pos->fp, "%s:%s:%" PRIu64 "",
436 callsite->func, callsite->file,
437 callsite->line);
438 }
439 i++;
440 }
441 fprintf(pos->fp, "]");
442 if (pos->print_names)
443 fprintf(pos->fp, ", ");
444 dom_print = 1;
445 }
446 }
447 if (dom_print && !pos->print_names)
448 fprintf(pos->fp, " ");
449 set_field_names_print(pos, ITEM_HEADER);
450 if (pos->print_names)
451 fprintf(pos->fp, "name = ");
452 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
453 if (pos->print_names)
454 pos->field_nr++;
455 else
456 fprintf(pos->fp, ":");
457
458 /* print cpuid field from packet context */
459 if (stream->stream_packet_context) {
460 if (pos->field_nr++ != 0)
461 fprintf(pos->fp, ",");
462 set_field_names_print(pos, ITEM_SCOPE);
463 if (pos->print_names)
464 fprintf(pos->fp, " stream.packet.context =");
465 field_nr_saved = pos->field_nr;
466 pos->field_nr = 0;
467 set_field_names_print(pos, ITEM_CONTEXT);
468 ret = generic_rw(ppos, &stream->stream_packet_context->p);
469 if (ret)
470 goto error;
471 pos->field_nr = field_nr_saved;
472 }
473
474 /* Only show the event header in verbose mode */
475 if (babeltrace_verbose && stream->stream_event_header) {
476 if (pos->field_nr++ != 0)
477 fprintf(pos->fp, ",");
478 set_field_names_print(pos, ITEM_SCOPE);
479 if (pos->print_names)
480 fprintf(pos->fp, " stream.event.header =");
481 field_nr_saved = pos->field_nr;
482 pos->field_nr = 0;
483 set_field_names_print(pos, ITEM_CONTEXT);
484 ret = generic_rw(ppos, &stream->stream_event_header->p);
485 if (ret)
486 goto error;
487 pos->field_nr = field_nr_saved;
488 }
489
490 /* print stream-declared event context */
491 if (stream->stream_event_context) {
492 if (pos->field_nr++ != 0)
493 fprintf(pos->fp, ",");
494 set_field_names_print(pos, ITEM_SCOPE);
495 if (pos->print_names)
496 fprintf(pos->fp, " stream.event.context =");
497 field_nr_saved = pos->field_nr;
498 pos->field_nr = 0;
499 set_field_names_print(pos, ITEM_CONTEXT);
500 ret = generic_rw(ppos, &stream->stream_event_context->p);
501 if (ret)
502 goto error;
503 pos->field_nr = field_nr_saved;
504 }
505
506 /* print event-declared event context */
507 if (event->event_context) {
508 if (pos->field_nr++ != 0)
509 fprintf(pos->fp, ",");
510 set_field_names_print(pos, ITEM_SCOPE);
511 if (pos->print_names)
512 fprintf(pos->fp, " event.context =");
513 field_nr_saved = pos->field_nr;
514 pos->field_nr = 0;
515 set_field_names_print(pos, ITEM_CONTEXT);
516 ret = generic_rw(ppos, &event->event_context->p);
517 if (ret)
518 goto error;
519 pos->field_nr = field_nr_saved;
520 }
521
522 /* Read and print event payload */
523 if (event->event_fields) {
524 if (pos->field_nr++ != 0)
525 fprintf(pos->fp, ",");
526 set_field_names_print(pos, ITEM_SCOPE);
527 if (pos->print_names)
528 fprintf(pos->fp, " event.fields =");
529 field_nr_saved = pos->field_nr;
530 pos->field_nr = 0;
531 set_field_names_print(pos, ITEM_PAYLOAD);
532 ret = generic_rw(ppos, &event->event_fields->p);
533 if (ret)
534 goto error;
535 pos->field_nr = field_nr_saved;
536 }
537 /* newline */
538 fprintf(pos->fp, "\n");
539 pos->field_nr = 0;
540
541 return 0;
542
543 error:
544 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
545 return ret;
546 }
547
548
549 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
550 void (*packet_seek)(struct stream_pos *pos, size_t index,
551 int whence), FILE *metadata_fp)
552 {
553 struct ctf_text_stream_pos *pos;
554 FILE *fp;
555
556 pos = g_new0(struct ctf_text_stream_pos, 1);
557
558 pos->last_real_timestamp = -1ULL;
559 pos->last_cycles_timestamp = -1ULL;
560 switch (flags & O_ACCMODE) {
561 case O_RDWR:
562 if (!path)
563 fp = stdout;
564 else
565 fp = fopen(path, "w");
566 if (!fp)
567 goto error;
568 pos->fp = fp;
569 pos->parent.rw_table = write_dispatch_table;
570 pos->parent.event_cb = ctf_text_write_event;
571 pos->print_names = 0;
572 break;
573 case O_RDONLY:
574 default:
575 fprintf(stderr, "[error] Incorrect open flags.\n");
576 goto error;
577 }
578
579 return &pos->trace_descriptor;
580 error:
581 g_free(pos);
582 return NULL;
583 }
584
585 void ctf_text_close_trace(struct trace_descriptor *td)
586 {
587 struct ctf_text_stream_pos *pos =
588 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
589 fclose(pos->fp);
590 g_free(pos);
591 }
592
593 void __attribute__((constructor)) ctf_text_init(void)
594 {
595 int ret;
596
597 ctf_text_format.name = g_quark_from_static_string("text");
598 ret = bt_register_format(&ctf_text_format);
599 assert(!ret);
600 }
601
602 /* TODO: finalize */
This page took 0.065448 seconds and 4 git commands to generate.