67400f513c7fffad32ac89ea96050679e4df3fa0
[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 fprintf(pos->fp, "%s@%s:%" PRIu64 "",
431 callsite->func, callsite->file,
432 callsite->line);
433 i++;
434 }
435 fprintf(pos->fp, "]");
436 if (pos->print_names)
437 fprintf(pos->fp, ", ");
438 dom_print = 1;
439 }
440 }
441 if (dom_print && !pos->print_names)
442 fprintf(pos->fp, " ");
443 set_field_names_print(pos, ITEM_HEADER);
444 if (pos->print_names)
445 fprintf(pos->fp, "name = ");
446 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
447 if (pos->print_names)
448 pos->field_nr++;
449 else
450 fprintf(pos->fp, ":");
451
452 /* print cpuid field from packet context */
453 if (stream->stream_packet_context) {
454 if (pos->field_nr++ != 0)
455 fprintf(pos->fp, ",");
456 set_field_names_print(pos, ITEM_SCOPE);
457 if (pos->print_names)
458 fprintf(pos->fp, " stream.packet.context =");
459 field_nr_saved = pos->field_nr;
460 pos->field_nr = 0;
461 set_field_names_print(pos, ITEM_CONTEXT);
462 ret = generic_rw(ppos, &stream->stream_packet_context->p);
463 if (ret)
464 goto error;
465 pos->field_nr = field_nr_saved;
466 }
467
468 /* Only show the event header in verbose mode */
469 if (babeltrace_verbose && stream->stream_event_header) {
470 if (pos->field_nr++ != 0)
471 fprintf(pos->fp, ",");
472 set_field_names_print(pos, ITEM_SCOPE);
473 if (pos->print_names)
474 fprintf(pos->fp, " stream.event.header =");
475 field_nr_saved = pos->field_nr;
476 pos->field_nr = 0;
477 set_field_names_print(pos, ITEM_CONTEXT);
478 ret = generic_rw(ppos, &stream->stream_event_header->p);
479 if (ret)
480 goto error;
481 pos->field_nr = field_nr_saved;
482 }
483
484 /* print stream-declared event context */
485 if (stream->stream_event_context) {
486 if (pos->field_nr++ != 0)
487 fprintf(pos->fp, ",");
488 set_field_names_print(pos, ITEM_SCOPE);
489 if (pos->print_names)
490 fprintf(pos->fp, " stream.event.context =");
491 field_nr_saved = pos->field_nr;
492 pos->field_nr = 0;
493 set_field_names_print(pos, ITEM_CONTEXT);
494 ret = generic_rw(ppos, &stream->stream_event_context->p);
495 if (ret)
496 goto error;
497 pos->field_nr = field_nr_saved;
498 }
499
500 /* print event-declared event context */
501 if (event->event_context) {
502 if (pos->field_nr++ != 0)
503 fprintf(pos->fp, ",");
504 set_field_names_print(pos, ITEM_SCOPE);
505 if (pos->print_names)
506 fprintf(pos->fp, " event.context =");
507 field_nr_saved = pos->field_nr;
508 pos->field_nr = 0;
509 set_field_names_print(pos, ITEM_CONTEXT);
510 ret = generic_rw(ppos, &event->event_context->p);
511 if (ret)
512 goto error;
513 pos->field_nr = field_nr_saved;
514 }
515
516 /* Read and print event payload */
517 if (event->event_fields) {
518 if (pos->field_nr++ != 0)
519 fprintf(pos->fp, ",");
520 set_field_names_print(pos, ITEM_SCOPE);
521 if (pos->print_names)
522 fprintf(pos->fp, " event.fields =");
523 field_nr_saved = pos->field_nr;
524 pos->field_nr = 0;
525 set_field_names_print(pos, ITEM_PAYLOAD);
526 ret = generic_rw(ppos, &event->event_fields->p);
527 if (ret)
528 goto error;
529 pos->field_nr = field_nr_saved;
530 }
531 /* newline */
532 fprintf(pos->fp, "\n");
533 pos->field_nr = 0;
534
535 return 0;
536
537 error:
538 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
539 return ret;
540 }
541
542
543 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
544 void (*packet_seek)(struct stream_pos *pos, size_t index,
545 int whence), FILE *metadata_fp)
546 {
547 struct ctf_text_stream_pos *pos;
548 FILE *fp;
549
550 pos = g_new0(struct ctf_text_stream_pos, 1);
551
552 pos->last_real_timestamp = -1ULL;
553 pos->last_cycles_timestamp = -1ULL;
554 switch (flags & O_ACCMODE) {
555 case O_RDWR:
556 if (!path)
557 fp = stdout;
558 else
559 fp = fopen(path, "w");
560 if (!fp)
561 goto error;
562 pos->fp = fp;
563 pos->parent.rw_table = write_dispatch_table;
564 pos->parent.event_cb = ctf_text_write_event;
565 pos->print_names = 0;
566 break;
567 case O_RDONLY:
568 default:
569 fprintf(stderr, "[error] Incorrect open flags.\n");
570 goto error;
571 }
572
573 return &pos->trace_descriptor;
574 error:
575 g_free(pos);
576 return NULL;
577 }
578
579 void ctf_text_close_trace(struct trace_descriptor *td)
580 {
581 struct ctf_text_stream_pos *pos =
582 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
583 fclose(pos->fp);
584 g_free(pos);
585 }
586
587 void __attribute__((constructor)) ctf_text_init(void)
588 {
589 int ret;
590
591 ctf_text_format.name = g_quark_from_static_string("text");
592 ret = bt_register_format(&ctf_text_format);
593 assert(!ret);
594 }
595
596 /* TODO: finalize */
This page took 0.040175 seconds and 4 git commands to generate.