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