2 * BabelTrace - Common Trace Format (CTF)
4 * CTF Text Format registration.
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
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
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>
37 #include <sys/types.h>
45 #define NSEC_PER_SEC 1000000000ULL
47 int opt_all_field_names
,
48 opt_scope_field_names
,
49 opt_header_field_names
,
50 opt_context_field_names
,
51 opt_payload_field_names
,
54 opt_trace_domain_field
,
55 opt_trace_procname_field
,
57 opt_trace_hostname_field
,
58 opt_trace_default_fields
= 1,
72 BT_LOGLEVEL_EMERG
= 0,
73 BT_LOGLEVEL_ALERT
= 1,
76 BT_LOGLEVEL_WARNING
= 4,
77 BT_LOGLEVEL_NOTICE
= 5,
79 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
80 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
81 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
82 BT_LOGLEVEL_DEBUG_MODULE
= 10,
83 BT_LOGLEVEL_DEBUG_UNIT
= 11,
84 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
85 BT_LOGLEVEL_DEBUG_LINE
= 13,
86 BT_LOGLEVEL_DEBUG
= 14,
90 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
91 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
92 int whence
), FILE *metadata_fp
);
94 int ctf_text_close_trace(struct bt_trace_descriptor
*descriptor
);
97 struct bt_trace_descriptor
*ctf_metadata_open_trace(const char *path
, int flags
,
98 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
99 int whence
), FILE *metadata_fp
);
101 int ctf_metadata_close_trace(struct bt_trace_descriptor
*descriptor
);
104 rw_dispatch write_dispatch_table
[] = {
105 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
106 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
107 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
108 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
109 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
110 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
111 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
112 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
116 struct bt_format ctf_text_format
= {
117 .open_trace
= ctf_text_open_trace
,
118 .close_trace
= ctf_text_close_trace
,
122 struct bt_format ctf_metadata_format
= {
123 .open_trace
= ctf_metadata_open_trace
,
124 .close_trace
= ctf_metadata_close_trace
,
127 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
128 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
129 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
130 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
131 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
134 void __attribute__((constructor
)) init_quarks(void)
136 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
137 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
138 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
139 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
140 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
144 struct ctf_callsite_dups
*ctf_trace_callsite_lookup(struct ctf_trace
*trace
,
145 GQuark callsite_name
)
147 return g_hash_table_lookup(trace
->callsites
,
148 (gpointer
) (unsigned long) callsite_name
);
151 int print_field(struct bt_definition
*definition
)
153 /* Print all fields in verbose mode */
154 if (babeltrace_verbose
)
157 /* Filter out part of the packet context */
158 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
160 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
162 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
164 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
166 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
173 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
177 if (opt_all_field_names
|| opt_scope_field_names
)
178 pos
->print_names
= 1;
180 pos
->print_names
= 0;
183 if (opt_all_field_names
|| opt_header_field_names
)
184 pos
->print_names
= 1;
186 pos
->print_names
= 0;
189 if (opt_all_field_names
|| opt_context_field_names
)
190 pos
->print_names
= 1;
192 pos
->print_names
= 0;
195 if (opt_all_field_names
|| opt_payload_field_names
)
196 pos
->print_names
= 1;
198 pos
->print_names
= 0;
207 const char *print_loglevel(int value
)
212 case BT_LOGLEVEL_EMERG
:
213 return "TRACE_EMERG";
214 case BT_LOGLEVEL_ALERT
:
215 return "TRACE_ALERT";
216 case BT_LOGLEVEL_CRIT
:
218 case BT_LOGLEVEL_ERR
:
220 case BT_LOGLEVEL_WARNING
:
221 return "TRACE_WARNING";
222 case BT_LOGLEVEL_NOTICE
:
223 return "TRACE_NOTICE";
224 case BT_LOGLEVEL_INFO
:
226 case BT_LOGLEVEL_DEBUG_SYSTEM
:
227 return "TRACE_DEBUG_SYSTEM";
228 case BT_LOGLEVEL_DEBUG_PROGRAM
:
229 return "TRACE_DEBUG_PROGRAM";
230 case BT_LOGLEVEL_DEBUG_PROCESS
:
231 return "TRACE_DEBUG_PROCESS";
232 case BT_LOGLEVEL_DEBUG_MODULE
:
233 return "TRACE_DEBUG_MODULE";
234 case BT_LOGLEVEL_DEBUG_UNIT
:
235 return "TRACE_DEBUG_UNIT";
236 case BT_LOGLEVEL_DEBUG_FUNCTION
:
237 return "TRACE_DEBUG_FUNCTION";
238 case BT_LOGLEVEL_DEBUG_LINE
:
239 return "TRACE_DEBUG_LINE";
240 case BT_LOGLEVEL_DEBUG
:
241 return "TRACE_DEBUG";
243 return "<<UNKNOWN>>";
248 int ctf_text_write_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
251 struct ctf_text_stream_pos
*pos
=
252 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
253 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
255 struct ctf_event_declaration
*event_class
;
256 struct ctf_event_definition
*event
;
261 id
= stream
->event_id
;
263 if (id
>= stream_class
->events_by_id
->len
) {
264 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
267 event
= g_ptr_array_index(stream
->events_by_id
, id
);
269 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
272 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
274 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
278 /* Print events discarded */
279 if (stream
->events_discarded
) {
281 ctf_print_discarded(stderr
, stream
, 0);
282 stream
->events_discarded
= 0;
285 if (stream
->has_timestamp
) {
286 set_field_names_print(pos
, ITEM_HEADER
);
287 if (pos
->print_names
)
288 fprintf(pos
->fp
, "timestamp = ");
290 fprintf(pos
->fp
, "[");
291 if (opt_clock_cycles
) {
292 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
294 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
296 if (!pos
->print_names
)
297 fprintf(pos
->fp
, "]");
299 if (pos
->print_names
)
300 fprintf(pos
->fp
, ", ");
302 fprintf(pos
->fp
, " ");
304 if (opt_delta_field
&& stream
->has_timestamp
) {
305 uint64_t delta
, delta_sec
, delta_nsec
;
307 set_field_names_print(pos
, ITEM_HEADER
);
308 if (pos
->print_names
)
309 fprintf(pos
->fp
, "delta = ");
311 fprintf(pos
->fp
, "(");
312 if (pos
->last_real_timestamp
!= -1ULL) {
313 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
314 delta_sec
= delta
/ NSEC_PER_SEC
;
315 delta_nsec
= delta
% NSEC_PER_SEC
;
316 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
317 delta_sec
, delta_nsec
);
319 fprintf(pos
->fp
, "+?.?????????");
321 if (!pos
->print_names
)
322 fprintf(pos
->fp
, ")");
324 if (pos
->print_names
)
325 fprintf(pos
->fp
, ", ");
327 fprintf(pos
->fp
, " ");
328 pos
->last_real_timestamp
= stream
->real_timestamp
;
329 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
332 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
333 set_field_names_print(pos
, ITEM_HEADER
);
334 if (pos
->print_names
) {
335 fprintf(pos
->fp
, "trace = ");
337 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
338 if (pos
->print_names
)
339 fprintf(pos
->fp
, ", ");
341 fprintf(pos
->fp
, " ");
343 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
344 && stream_class
->trace
->env
.hostname
[0] != '\0') {
345 set_field_names_print(pos
, ITEM_HEADER
);
346 if (pos
->print_names
) {
347 fprintf(pos
->fp
, "trace:hostname = ");
349 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
350 if (pos
->print_names
)
351 fprintf(pos
->fp
, ", ");
354 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
355 set_field_names_print(pos
, ITEM_HEADER
);
356 if (pos
->print_names
) {
357 fprintf(pos
->fp
, "trace:domain = ");
359 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
360 if (pos
->print_names
)
361 fprintf(pos
->fp
, ", ");
364 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
365 && stream_class
->trace
->env
.procname
[0] != '\0') {
366 set_field_names_print(pos
, ITEM_HEADER
);
367 if (pos
->print_names
) {
368 fprintf(pos
->fp
, "trace:procname = ");
369 } else if (dom_print
) {
370 fprintf(pos
->fp
, ":");
372 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
373 if (pos
->print_names
)
374 fprintf(pos
->fp
, ", ");
377 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
378 && stream_class
->trace
->env
.vpid
!= -1) {
379 set_field_names_print(pos
, ITEM_HEADER
);
380 if (pos
->print_names
) {
381 fprintf(pos
->fp
, "trace:vpid = ");
382 } else if (dom_print
) {
383 fprintf(pos
->fp
, ":");
385 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
386 if (pos
->print_names
)
387 fprintf(pos
->fp
, ", ");
390 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
391 set_field_names_print(pos
, ITEM_HEADER
);
392 if (pos
->print_names
) {
393 fprintf(pos
->fp
, "loglevel = ");
394 } else if (dom_print
) {
395 fprintf(pos
->fp
, ":");
397 fprintf(pos
->fp
, "%s (%d)",
398 print_loglevel(event_class
->loglevel
),
399 event_class
->loglevel
);
400 if (pos
->print_names
)
401 fprintf(pos
->fp
, ", ");
404 if ((opt_emf_field
|| opt_all_fields
) && event_class
->model_emf_uri
) {
405 set_field_names_print(pos
, ITEM_HEADER
);
406 if (pos
->print_names
) {
407 fprintf(pos
->fp
, "model.emf.uri = ");
408 } else if (dom_print
) {
409 fprintf(pos
->fp
, ":");
411 fprintf(pos
->fp
, "\"%s\"",
412 g_quark_to_string(event_class
->model_emf_uri
));
413 if (pos
->print_names
)
414 fprintf(pos
->fp
, ", ");
417 if ((opt_callsite_field
|| opt_all_fields
)) {
418 struct ctf_callsite_dups
*cs_dups
;
419 struct ctf_callsite
*callsite
;
421 cs_dups
= ctf_trace_callsite_lookup(stream_class
->trace
,
426 set_field_names_print(pos
, ITEM_HEADER
);
427 if (pos
->print_names
) {
428 fprintf(pos
->fp
, "callsite = ");
429 } else if (dom_print
) {
430 fprintf(pos
->fp
, ":");
432 fprintf(pos
->fp
, "[");
433 bt_list_for_each_entry(callsite
, &cs_dups
->head
, node
) {
435 fprintf(pos
->fp
, ",");
436 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
437 fprintf(pos
->fp
, "%s@0x%" PRIx64
":%s:%" PRIu64
"",
438 callsite
->func
, callsite
->ip
, callsite
->file
,
441 fprintf(pos
->fp
, "%s:%s:%" PRIu64
"",
442 callsite
->func
, callsite
->file
,
447 fprintf(pos
->fp
, "]");
448 if (pos
->print_names
)
449 fprintf(pos
->fp
, ", ");
453 if (dom_print
&& !pos
->print_names
)
454 fprintf(pos
->fp
, " ");
455 set_field_names_print(pos
, ITEM_HEADER
);
456 if (pos
->print_names
)
457 fprintf(pos
->fp
, "name = ");
458 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
459 if (pos
->print_names
)
462 fprintf(pos
->fp
, ":");
464 /* print cpuid field from packet context */
465 if (stream
->stream_packet_context
) {
466 if (pos
->field_nr
++ != 0)
467 fprintf(pos
->fp
, ",");
468 set_field_names_print(pos
, ITEM_SCOPE
);
469 if (pos
->print_names
)
470 fprintf(pos
->fp
, " stream.packet.context =");
471 field_nr_saved
= pos
->field_nr
;
473 set_field_names_print(pos
, ITEM_CONTEXT
);
474 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
477 pos
->field_nr
= field_nr_saved
;
480 /* Only show the event header in verbose mode */
481 if (babeltrace_verbose
&& stream
->stream_event_header
) {
482 if (pos
->field_nr
++ != 0)
483 fprintf(pos
->fp
, ",");
484 set_field_names_print(pos
, ITEM_SCOPE
);
485 if (pos
->print_names
)
486 fprintf(pos
->fp
, " stream.event.header =");
487 field_nr_saved
= pos
->field_nr
;
489 set_field_names_print(pos
, ITEM_CONTEXT
);
490 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
493 pos
->field_nr
= field_nr_saved
;
496 /* print stream-declared event context */
497 if (stream
->stream_event_context
) {
498 if (pos
->field_nr
++ != 0)
499 fprintf(pos
->fp
, ",");
500 set_field_names_print(pos
, ITEM_SCOPE
);
501 if (pos
->print_names
)
502 fprintf(pos
->fp
, " stream.event.context =");
503 field_nr_saved
= pos
->field_nr
;
505 set_field_names_print(pos
, ITEM_CONTEXT
);
506 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
509 pos
->field_nr
= field_nr_saved
;
512 /* print event-declared event context */
513 if (event
->event_context
) {
514 if (pos
->field_nr
++ != 0)
515 fprintf(pos
->fp
, ",");
516 set_field_names_print(pos
, ITEM_SCOPE
);
517 if (pos
->print_names
)
518 fprintf(pos
->fp
, " event.context =");
519 field_nr_saved
= pos
->field_nr
;
521 set_field_names_print(pos
, ITEM_CONTEXT
);
522 ret
= generic_rw(ppos
, &event
->event_context
->p
);
525 pos
->field_nr
= field_nr_saved
;
528 /* Read and print event payload */
529 if (event
->event_fields
) {
530 if (pos
->field_nr
++ != 0)
531 fprintf(pos
->fp
, ",");
532 set_field_names_print(pos
, ITEM_SCOPE
);
533 if (pos
->print_names
)
534 fprintf(pos
->fp
, " event.fields =");
535 field_nr_saved
= pos
->field_nr
;
537 set_field_names_print(pos
, ITEM_PAYLOAD
);
538 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
541 pos
->field_nr
= field_nr_saved
;
544 fprintf(pos
->fp
, "\n");
550 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
555 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
556 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
557 int whence
), FILE *metadata_fp
)
559 struct ctf_text_stream_pos
*pos
;
562 pos
= g_new0(struct ctf_text_stream_pos
, 1);
564 pos
->last_real_timestamp
= -1ULL;
565 pos
->last_cycles_timestamp
= -1ULL;
566 switch (flags
& O_ACCMODE
) {
571 fp
= fopen(path
, "w");
575 pos
->parent
.rw_table
= write_dispatch_table
;
576 pos
->parent
.event_cb
= ctf_text_write_event
;
577 pos
->print_names
= 0;
581 fprintf(stderr
, "[error] Incorrect open flags.\n");
585 return &pos
->trace_descriptor
;
592 int ctf_text_close_trace(struct bt_trace_descriptor
*td
)
595 struct ctf_text_stream_pos
*pos
=
596 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
597 if (pos
->fp
!= stdout
) {
598 ret
= fclose(pos
->fp
);
600 perror("Error on fclose");
609 int ctf_metadata_trace_pre_handler(struct bt_stream_pos
*ppos
,
610 struct bt_trace_descriptor
*td
)
612 struct ctf_text_stream_pos
*pos
=
613 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
614 struct ctf_trace
*trace
;
616 trace
= container_of(td
, struct ctf_trace
, parent
);
617 if (!trace
->metadata_string
)
619 if (trace
->metadata_packetized
) {
620 fprintf(pos
->fp
, "/* CTF %u.%u */\n",
621 BT_CTF_MAJOR
, BT_CTF_MINOR
);
623 fprintf(pos
->fp
, "%s", trace
->metadata_string
);
628 struct bt_trace_descriptor
*ctf_metadata_open_trace(const char *path
, int flags
,
629 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
630 int whence
), FILE *metadata_fp
)
632 struct ctf_text_stream_pos
*pos
;
635 pos
= g_new0(struct ctf_text_stream_pos
, 1);
637 pos
->last_real_timestamp
= -1ULL;
638 pos
->last_cycles_timestamp
= -1ULL;
639 switch (flags
& O_ACCMODE
) {
644 fp
= fopen(path
, "w");
648 pos
->parent
.pre_trace_cb
= ctf_metadata_trace_pre_handler
;
649 pos
->print_names
= 0;
653 fprintf(stderr
, "[error] Incorrect open flags.\n");
657 return &pos
->trace_descriptor
;
664 int ctf_metadata_close_trace(struct bt_trace_descriptor
*td
)
667 struct ctf_text_stream_pos
*pos
=
668 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
669 if (pos
->fp
!= stdout
) {
670 ret
= fclose(pos
->fp
);
672 perror("Error on fclose");
681 void __attribute__((constructor
)) ctf_text_init(void)
685 ctf_text_format
.name
= g_quark_from_static_string("text");
686 ret
= bt_register_format(&ctf_text_format
);
688 ctf_metadata_format
.name
= g_quark_from_static_string("ctf-metadata");
689 ret
= bt_register_format(&ctf_metadata_format
);
694 void __attribute__((destructor
)) ctf_text_exit(void)
696 bt_unregister_format(&ctf_metadata_format
);
697 bt_unregister_format(&ctf_text_format
);