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>
34 #include <babeltrace/trace-debug-info.h>
38 #include <sys/types.h>
46 #define NSEC_PER_SEC 1000000000LL
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
,
55 opt_trace_domain_field
,
56 opt_trace_procname_field
,
58 opt_trace_hostname_field
,
59 opt_trace_default_fields
= 1,
64 opt_debug_info_full_path
;
74 BT_LOGLEVEL_EMERG
= 0,
75 BT_LOGLEVEL_ALERT
= 1,
78 BT_LOGLEVEL_WARNING
= 4,
79 BT_LOGLEVEL_NOTICE
= 5,
81 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
82 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
83 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
84 BT_LOGLEVEL_DEBUG_MODULE
= 10,
85 BT_LOGLEVEL_DEBUG_UNIT
= 11,
86 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
87 BT_LOGLEVEL_DEBUG_LINE
= 13,
88 BT_LOGLEVEL_DEBUG
= 14,
92 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
93 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
94 int whence
), FILE *metadata_fp
);
96 int ctf_text_close_trace(struct bt_trace_descriptor
*descriptor
);
99 rw_dispatch write_dispatch_table
[] = {
100 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
101 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
102 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
103 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
104 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
105 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
106 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
107 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
111 struct bt_format ctf_text_format
= {
112 .open_trace
= ctf_text_open_trace
,
113 .close_trace
= ctf_text_close_trace
,
116 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
117 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
118 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
119 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
120 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
,
121 Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
;
124 void __attribute__((constructor
)) init_quarks(void)
126 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_string("stream.packet.context.timestamp_begin");
127 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_string("stream.packet.context.timestamp_end");
128 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_string("stream.packet.context.events_discarded");
129 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_string("stream.packet.context.content_size");
130 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_string("stream.packet.context.packet_size");
131 Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
= g_quark_from_string("stream.packet.context.packet_seq_num");
135 struct ctf_callsite_dups
*ctf_trace_callsite_lookup(struct ctf_trace
*trace
,
136 GQuark callsite_name
)
138 return g_hash_table_lookup(trace
->callsites
,
139 (gpointer
) (unsigned long) callsite_name
);
142 void bt_ctf_text_hook(void)
145 * Dummy function to prevent the linker from discarding this format as
146 * "unused" in static builds.
150 int print_field(struct bt_definition
*definition
)
152 /* Print all fields in verbose mode */
153 if (babeltrace_verbose
)
156 /* Filter out part of the packet context */
157 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
159 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
161 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
163 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
165 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
167 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
)
174 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
178 if (opt_all_field_names
|| opt_scope_field_names
)
179 pos
->print_names
= 1;
181 pos
->print_names
= 0;
184 if (opt_all_field_names
|| opt_header_field_names
)
185 pos
->print_names
= 1;
187 pos
->print_names
= 0;
190 if (opt_all_field_names
|| opt_context_field_names
)
191 pos
->print_names
= 1;
193 pos
->print_names
= 0;
196 if (opt_all_field_names
|| opt_payload_field_names
)
197 pos
->print_names
= 1;
199 pos
->print_names
= 0;
208 const char *print_loglevel(int value
)
213 case BT_LOGLEVEL_EMERG
:
214 return "TRACE_EMERG";
215 case BT_LOGLEVEL_ALERT
:
216 return "TRACE_ALERT";
217 case BT_LOGLEVEL_CRIT
:
219 case BT_LOGLEVEL_ERR
:
221 case BT_LOGLEVEL_WARNING
:
222 return "TRACE_WARNING";
223 case BT_LOGLEVEL_NOTICE
:
224 return "TRACE_NOTICE";
225 case BT_LOGLEVEL_INFO
:
227 case BT_LOGLEVEL_DEBUG_SYSTEM
:
228 return "TRACE_DEBUG_SYSTEM";
229 case BT_LOGLEVEL_DEBUG_PROGRAM
:
230 return "TRACE_DEBUG_PROGRAM";
231 case BT_LOGLEVEL_DEBUG_PROCESS
:
232 return "TRACE_DEBUG_PROCESS";
233 case BT_LOGLEVEL_DEBUG_MODULE
:
234 return "TRACE_DEBUG_MODULE";
235 case BT_LOGLEVEL_DEBUG_UNIT
:
236 return "TRACE_DEBUG_UNIT";
237 case BT_LOGLEVEL_DEBUG_FUNCTION
:
238 return "TRACE_DEBUG_FUNCTION";
239 case BT_LOGLEVEL_DEBUG_LINE
:
240 return "TRACE_DEBUG_LINE";
241 case BT_LOGLEVEL_DEBUG
:
242 return "TRACE_DEBUG";
244 return "<<UNKNOWN>>";
249 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 handle_debug_info_event(stream_class
, event
);
280 if (stream
->has_timestamp
) {
281 set_field_names_print(pos
, ITEM_HEADER
);
282 if (pos
->print_names
)
283 fprintf(pos
->fp
, "timestamp = ");
285 fprintf(pos
->fp
, "[");
286 if (opt_clock_cycles
) {
287 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
289 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
291 if (!pos
->print_names
)
292 fprintf(pos
->fp
, "]");
294 if (pos
->print_names
)
295 fprintf(pos
->fp
, ", ");
297 fprintf(pos
->fp
, " ");
299 if (opt_delta_field
&& stream
->has_timestamp
) {
300 uint64_t delta
, delta_sec
, delta_nsec
;
302 set_field_names_print(pos
, ITEM_HEADER
);
303 if (pos
->print_names
)
304 fprintf(pos
->fp
, "delta = ");
306 fprintf(pos
->fp
, "(");
307 if (pos
->last_real_timestamp
!= -1ULL) {
308 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
309 delta_sec
= delta
/ NSEC_PER_SEC
;
310 delta_nsec
= delta
% NSEC_PER_SEC
;
311 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
312 delta_sec
, delta_nsec
);
314 fprintf(pos
->fp
, "+?.?????????");
316 if (!pos
->print_names
)
317 fprintf(pos
->fp
, ")");
319 if (pos
->print_names
)
320 fprintf(pos
->fp
, ", ");
322 fprintf(pos
->fp
, " ");
323 pos
->last_real_timestamp
= stream
->real_timestamp
;
324 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
327 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->parent
.path
[0] != '\0') {
328 set_field_names_print(pos
, ITEM_HEADER
);
329 if (pos
->print_names
) {
330 fprintf(pos
->fp
, "trace = ");
332 fprintf(pos
->fp
, "%s", stream_class
->trace
->parent
.path
);
333 if (pos
->print_names
)
334 fprintf(pos
->fp
, ", ");
336 fprintf(pos
->fp
, " ");
338 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
339 && stream_class
->trace
->env
.hostname
[0] != '\0') {
340 set_field_names_print(pos
, ITEM_HEADER
);
341 if (pos
->print_names
) {
342 fprintf(pos
->fp
, "trace:hostname = ");
344 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
345 if (pos
->print_names
)
346 fprintf(pos
->fp
, ", ");
349 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
350 set_field_names_print(pos
, ITEM_HEADER
);
351 if (pos
->print_names
) {
352 fprintf(pos
->fp
, "trace:domain = ");
354 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
355 if (pos
->print_names
)
356 fprintf(pos
->fp
, ", ");
359 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
360 && stream_class
->trace
->env
.procname
[0] != '\0') {
361 set_field_names_print(pos
, ITEM_HEADER
);
362 if (pos
->print_names
) {
363 fprintf(pos
->fp
, "trace:procname = ");
364 } else if (dom_print
) {
365 fprintf(pos
->fp
, ":");
367 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
368 if (pos
->print_names
)
369 fprintf(pos
->fp
, ", ");
372 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
373 && stream_class
->trace
->env
.vpid
!= -1) {
374 set_field_names_print(pos
, ITEM_HEADER
);
375 if (pos
->print_names
) {
376 fprintf(pos
->fp
, "trace:vpid = ");
377 } else if (dom_print
) {
378 fprintf(pos
->fp
, ":");
380 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
381 if (pos
->print_names
)
382 fprintf(pos
->fp
, ", ");
385 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
386 set_field_names_print(pos
, ITEM_HEADER
);
387 if (pos
->print_names
) {
388 fprintf(pos
->fp
, "loglevel = ");
389 } else if (dom_print
) {
390 fprintf(pos
->fp
, ":");
392 fprintf(pos
->fp
, "%s (%d)",
393 print_loglevel(event_class
->loglevel
),
394 event_class
->loglevel
);
395 if (pos
->print_names
)
396 fprintf(pos
->fp
, ", ");
399 if ((opt_emf_field
|| opt_all_fields
) && event_class
->model_emf_uri
) {
400 set_field_names_print(pos
, ITEM_HEADER
);
401 if (pos
->print_names
) {
402 fprintf(pos
->fp
, "model.emf.uri = ");
403 } else if (dom_print
) {
404 fprintf(pos
->fp
, ":");
406 fprintf(pos
->fp
, "\"%s\"",
407 g_quark_to_string(event_class
->model_emf_uri
));
408 if (pos
->print_names
)
409 fprintf(pos
->fp
, ", ");
412 if ((opt_callsite_field
|| opt_all_fields
)) {
413 struct ctf_callsite_dups
*cs_dups
;
414 struct ctf_callsite
*callsite
;
416 cs_dups
= ctf_trace_callsite_lookup(stream_class
->trace
,
421 set_field_names_print(pos
, ITEM_HEADER
);
422 if (pos
->print_names
) {
423 fprintf(pos
->fp
, "callsite = ");
424 } else if (dom_print
) {
425 fprintf(pos
->fp
, ":");
427 fprintf(pos
->fp
, "[");
428 bt_list_for_each_entry(callsite
, &cs_dups
->head
, node
) {
430 fprintf(pos
->fp
, ",");
431 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
432 fprintf(pos
->fp
, "%s@0x%" PRIx64
":%s:%" PRIu64
"",
433 callsite
->func
, callsite
->ip
, callsite
->file
,
436 fprintf(pos
->fp
, "%s:%s:%" PRIu64
"",
437 callsite
->func
, callsite
->file
,
442 fprintf(pos
->fp
, "]");
443 if (pos
->print_names
)
444 fprintf(pos
->fp
, ", ");
448 if (dom_print
&& !pos
->print_names
)
449 fprintf(pos
->fp
, " ");
450 set_field_names_print(pos
, ITEM_HEADER
);
451 if (pos
->print_names
)
452 fprintf(pos
->fp
, "name = ");
453 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
454 if (pos
->print_names
)
457 fprintf(pos
->fp
, ":");
459 /* print cpuid field from packet context */
460 if (stream
->stream_packet_context
) {
461 if (pos
->field_nr
++ != 0)
462 fprintf(pos
->fp
, ",");
463 set_field_names_print(pos
, ITEM_SCOPE
);
464 if (pos
->print_names
)
465 fprintf(pos
->fp
, " stream.packet.context =");
466 field_nr_saved
= pos
->field_nr
;
468 set_field_names_print(pos
, ITEM_CONTEXT
);
469 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
472 pos
->field_nr
= field_nr_saved
;
475 /* Only show the event header in verbose mode */
476 if (babeltrace_verbose
&& stream
->stream_event_header
) {
477 if (pos
->field_nr
++ != 0)
478 fprintf(pos
->fp
, ",");
479 set_field_names_print(pos
, ITEM_SCOPE
);
480 if (pos
->print_names
)
481 fprintf(pos
->fp
, " stream.event.header =");
482 field_nr_saved
= pos
->field_nr
;
484 set_field_names_print(pos
, ITEM_CONTEXT
);
485 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
488 pos
->field_nr
= field_nr_saved
;
491 /* print stream-declared event context */
492 if (stream
->stream_event_context
) {
493 if (pos
->field_nr
++ != 0)
494 fprintf(pos
->fp
, ",");
495 set_field_names_print(pos
, ITEM_SCOPE
);
496 if (pos
->print_names
)
497 fprintf(pos
->fp
, " stream.event.context =");
498 field_nr_saved
= pos
->field_nr
;
500 set_field_names_print(pos
, ITEM_CONTEXT
);
501 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
504 pos
->field_nr
= field_nr_saved
;
507 /* print event-declared event context */
508 if (event
->event_context
) {
509 if (pos
->field_nr
++ != 0)
510 fprintf(pos
->fp
, ",");
511 set_field_names_print(pos
, ITEM_SCOPE
);
512 if (pos
->print_names
)
513 fprintf(pos
->fp
, " event.context =");
514 field_nr_saved
= pos
->field_nr
;
516 set_field_names_print(pos
, ITEM_CONTEXT
);
517 ret
= generic_rw(ppos
, &event
->event_context
->p
);
520 pos
->field_nr
= field_nr_saved
;
523 /* Read and print event payload */
524 if (event
->event_fields
) {
525 if (pos
->field_nr
++ != 0)
526 fprintf(pos
->fp
, ",");
527 set_field_names_print(pos
, ITEM_SCOPE
);
528 if (pos
->print_names
)
529 fprintf(pos
->fp
, " event.fields =");
530 field_nr_saved
= pos
->field_nr
;
532 set_field_names_print(pos
, ITEM_PAYLOAD
);
533 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
536 pos
->field_nr
= field_nr_saved
;
539 fprintf(pos
->fp
, "\n");
545 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
550 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
551 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
552 int whence
), FILE *metadata_fp
)
554 struct ctf_text_stream_pos
*pos
;
557 pos
= g_new0(struct ctf_text_stream_pos
, 1);
561 init_trace_descriptor(&pos
->trace_descriptor
);
563 pos
->last_real_timestamp
= -1ULL;
564 pos
->last_cycles_timestamp
= -1ULL;
565 switch (flags
& O_ACCMODE
) {
570 fp
= fopen(path
, "w");
574 pos
->parent
.rw_table
= write_dispatch_table
;
575 pos
->parent
.event_cb
= ctf_text_write_event
;
576 pos
->parent
.trace
= &pos
->trace_descriptor
;
577 pos
->print_names
= 0;
578 babeltrace_ctf_console_output
++;
582 fprintf(stderr
, "[error] Incorrect open flags.\n");
586 return &pos
->trace_descriptor
;
593 int ctf_text_close_trace(struct bt_trace_descriptor
*td
)
596 struct ctf_text_stream_pos
*pos
=
597 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
599 babeltrace_ctf_console_output
--;
600 if (pos
->fp
!= stdout
) {
601 ret
= fclose(pos
->fp
);
603 perror("Error on fclose");
612 void __attribute__((constructor
)) ctf_text_init(void)
616 ctf_text_format
.name
= g_quark_from_string("text");
617 ret
= bt_register_format(&ctf_text_format
);
622 void __attribute__((destructor
)) ctf_text_exit(void)
624 bt_unregister_format(&ctf_text_format
);