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 rw_dispatch write_dispatch_table
[] = {
98 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
99 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
100 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
101 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
102 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
103 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
104 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
105 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
109 struct bt_format ctf_text_format
= {
110 .open_trace
= ctf_text_open_trace
,
111 .close_trace
= ctf_text_close_trace
,
114 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
115 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
116 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
117 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
118 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
121 void __attribute__((constructor
)) init_quarks(void)
123 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
124 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
125 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
126 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
127 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
131 struct ctf_callsite_dups
*ctf_trace_callsite_lookup(struct ctf_trace
*trace
,
132 GQuark callsite_name
)
134 return g_hash_table_lookup(trace
->callsites
,
135 (gpointer
) (unsigned long) callsite_name
);
138 int print_field(struct bt_definition
*definition
)
140 /* Print all fields in verbose mode */
141 if (babeltrace_verbose
)
144 /* Filter out part of the packet context */
145 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
147 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
149 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
151 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
153 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
160 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
164 if (opt_all_field_names
|| opt_scope_field_names
)
165 pos
->print_names
= 1;
167 pos
->print_names
= 0;
170 if (opt_all_field_names
|| opt_header_field_names
)
171 pos
->print_names
= 1;
173 pos
->print_names
= 0;
176 if (opt_all_field_names
|| opt_context_field_names
)
177 pos
->print_names
= 1;
179 pos
->print_names
= 0;
182 if (opt_all_field_names
|| opt_payload_field_names
)
183 pos
->print_names
= 1;
185 pos
->print_names
= 0;
194 const char *print_loglevel(int value
)
199 case BT_LOGLEVEL_EMERG
:
200 return "TRACE_EMERG";
201 case BT_LOGLEVEL_ALERT
:
202 return "TRACE_ALERT";
203 case BT_LOGLEVEL_CRIT
:
205 case BT_LOGLEVEL_ERR
:
207 case BT_LOGLEVEL_WARNING
:
208 return "TRACE_WARNING";
209 case BT_LOGLEVEL_NOTICE
:
210 return "TRACE_NOTICE";
211 case BT_LOGLEVEL_INFO
:
213 case BT_LOGLEVEL_DEBUG_SYSTEM
:
214 return "TRACE_DEBUG_SYSTEM";
215 case BT_LOGLEVEL_DEBUG_PROGRAM
:
216 return "TRACE_DEBUG_PROGRAM";
217 case BT_LOGLEVEL_DEBUG_PROCESS
:
218 return "TRACE_DEBUG_PROCESS";
219 case BT_LOGLEVEL_DEBUG_MODULE
:
220 return "TRACE_DEBUG_MODULE";
221 case BT_LOGLEVEL_DEBUG_UNIT
:
222 return "TRACE_DEBUG_UNIT";
223 case BT_LOGLEVEL_DEBUG_FUNCTION
:
224 return "TRACE_DEBUG_FUNCTION";
225 case BT_LOGLEVEL_DEBUG_LINE
:
226 return "TRACE_DEBUG_LINE";
227 case BT_LOGLEVEL_DEBUG
:
228 return "TRACE_DEBUG";
230 return "<<UNKNOWN>>";
235 int ctf_text_write_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
238 struct ctf_text_stream_pos
*pos
=
239 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
240 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
242 struct ctf_event_declaration
*event_class
;
243 struct ctf_event_definition
*event
;
248 id
= stream
->event_id
;
250 if (id
>= stream_class
->events_by_id
->len
) {
251 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
254 event
= g_ptr_array_index(stream
->events_by_id
, id
);
256 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
259 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
261 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
265 if (stream
->has_timestamp
) {
266 set_field_names_print(pos
, ITEM_HEADER
);
267 if (pos
->print_names
)
268 fprintf(pos
->fp
, "timestamp = ");
270 fprintf(pos
->fp
, "[");
271 if (opt_clock_cycles
) {
272 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
274 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
276 if (!pos
->print_names
)
277 fprintf(pos
->fp
, "]");
279 if (pos
->print_names
)
280 fprintf(pos
->fp
, ", ");
282 fprintf(pos
->fp
, " ");
284 if (opt_delta_field
&& stream
->has_timestamp
) {
285 uint64_t delta
, delta_sec
, delta_nsec
;
287 set_field_names_print(pos
, ITEM_HEADER
);
288 if (pos
->print_names
)
289 fprintf(pos
->fp
, "delta = ");
291 fprintf(pos
->fp
, "(");
292 if (pos
->last_real_timestamp
!= -1ULL) {
293 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
294 delta_sec
= delta
/ NSEC_PER_SEC
;
295 delta_nsec
= delta
% NSEC_PER_SEC
;
296 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
297 delta_sec
, delta_nsec
);
299 fprintf(pos
->fp
, "+?.?????????");
301 if (!pos
->print_names
)
302 fprintf(pos
->fp
, ")");
304 if (pos
->print_names
)
305 fprintf(pos
->fp
, ", ");
307 fprintf(pos
->fp
, " ");
308 pos
->last_real_timestamp
= stream
->real_timestamp
;
309 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
312 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->parent
.path
[0] != '\0') {
313 set_field_names_print(pos
, ITEM_HEADER
);
314 if (pos
->print_names
) {
315 fprintf(pos
->fp
, "trace = ");
317 fprintf(pos
->fp
, "%s", stream_class
->trace
->parent
.path
);
318 if (pos
->print_names
)
319 fprintf(pos
->fp
, ", ");
321 fprintf(pos
->fp
, " ");
323 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
324 && stream_class
->trace
->env
.hostname
[0] != '\0') {
325 set_field_names_print(pos
, ITEM_HEADER
);
326 if (pos
->print_names
) {
327 fprintf(pos
->fp
, "trace:hostname = ");
329 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
330 if (pos
->print_names
)
331 fprintf(pos
->fp
, ", ");
334 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
335 set_field_names_print(pos
, ITEM_HEADER
);
336 if (pos
->print_names
) {
337 fprintf(pos
->fp
, "trace:domain = ");
339 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
340 if (pos
->print_names
)
341 fprintf(pos
->fp
, ", ");
344 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
345 && stream_class
->trace
->env
.procname
[0] != '\0') {
346 set_field_names_print(pos
, ITEM_HEADER
);
347 if (pos
->print_names
) {
348 fprintf(pos
->fp
, "trace:procname = ");
349 } else if (dom_print
) {
350 fprintf(pos
->fp
, ":");
352 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
353 if (pos
->print_names
)
354 fprintf(pos
->fp
, ", ");
357 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
358 && stream_class
->trace
->env
.vpid
!= -1) {
359 set_field_names_print(pos
, ITEM_HEADER
);
360 if (pos
->print_names
) {
361 fprintf(pos
->fp
, "trace:vpid = ");
362 } else if (dom_print
) {
363 fprintf(pos
->fp
, ":");
365 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
366 if (pos
->print_names
)
367 fprintf(pos
->fp
, ", ");
370 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
371 set_field_names_print(pos
, ITEM_HEADER
);
372 if (pos
->print_names
) {
373 fprintf(pos
->fp
, "loglevel = ");
374 } else if (dom_print
) {
375 fprintf(pos
->fp
, ":");
377 fprintf(pos
->fp
, "%s (%d)",
378 print_loglevel(event_class
->loglevel
),
379 event_class
->loglevel
);
380 if (pos
->print_names
)
381 fprintf(pos
->fp
, ", ");
384 if ((opt_emf_field
|| opt_all_fields
) && event_class
->model_emf_uri
) {
385 set_field_names_print(pos
, ITEM_HEADER
);
386 if (pos
->print_names
) {
387 fprintf(pos
->fp
, "model.emf.uri = ");
388 } else if (dom_print
) {
389 fprintf(pos
->fp
, ":");
391 fprintf(pos
->fp
, "\"%s\"",
392 g_quark_to_string(event_class
->model_emf_uri
));
393 if (pos
->print_names
)
394 fprintf(pos
->fp
, ", ");
397 if ((opt_callsite_field
|| opt_all_fields
)) {
398 struct ctf_callsite_dups
*cs_dups
;
399 struct ctf_callsite
*callsite
;
401 cs_dups
= ctf_trace_callsite_lookup(stream_class
->trace
,
406 set_field_names_print(pos
, ITEM_HEADER
);
407 if (pos
->print_names
) {
408 fprintf(pos
->fp
, "callsite = ");
409 } else if (dom_print
) {
410 fprintf(pos
->fp
, ":");
412 fprintf(pos
->fp
, "[");
413 bt_list_for_each_entry(callsite
, &cs_dups
->head
, node
) {
415 fprintf(pos
->fp
, ",");
416 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
417 fprintf(pos
->fp
, "%s@0x%" PRIx64
":%s:%" PRIu64
"",
418 callsite
->func
, callsite
->ip
, callsite
->file
,
421 fprintf(pos
->fp
, "%s:%s:%" PRIu64
"",
422 callsite
->func
, callsite
->file
,
427 fprintf(pos
->fp
, "]");
428 if (pos
->print_names
)
429 fprintf(pos
->fp
, ", ");
433 if (dom_print
&& !pos
->print_names
)
434 fprintf(pos
->fp
, " ");
435 set_field_names_print(pos
, ITEM_HEADER
);
436 if (pos
->print_names
)
437 fprintf(pos
->fp
, "name = ");
438 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
439 if (pos
->print_names
)
442 fprintf(pos
->fp
, ":");
444 /* print cpuid field from packet context */
445 if (stream
->stream_packet_context
) {
446 if (pos
->field_nr
++ != 0)
447 fprintf(pos
->fp
, ",");
448 set_field_names_print(pos
, ITEM_SCOPE
);
449 if (pos
->print_names
)
450 fprintf(pos
->fp
, " stream.packet.context =");
451 field_nr_saved
= pos
->field_nr
;
453 set_field_names_print(pos
, ITEM_CONTEXT
);
454 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
457 pos
->field_nr
= field_nr_saved
;
460 /* Only show the event header in verbose mode */
461 if (babeltrace_verbose
&& stream
->stream_event_header
) {
462 if (pos
->field_nr
++ != 0)
463 fprintf(pos
->fp
, ",");
464 set_field_names_print(pos
, ITEM_SCOPE
);
465 if (pos
->print_names
)
466 fprintf(pos
->fp
, " stream.event.header =");
467 field_nr_saved
= pos
->field_nr
;
469 set_field_names_print(pos
, ITEM_CONTEXT
);
470 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
473 pos
->field_nr
= field_nr_saved
;
476 /* print stream-declared event context */
477 if (stream
->stream_event_context
) {
478 if (pos
->field_nr
++ != 0)
479 fprintf(pos
->fp
, ",");
480 set_field_names_print(pos
, ITEM_SCOPE
);
481 if (pos
->print_names
)
482 fprintf(pos
->fp
, " stream.event.context =");
483 field_nr_saved
= pos
->field_nr
;
485 set_field_names_print(pos
, ITEM_CONTEXT
);
486 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
489 pos
->field_nr
= field_nr_saved
;
492 /* print event-declared event context */
493 if (event
->event_context
) {
494 if (pos
->field_nr
++ != 0)
495 fprintf(pos
->fp
, ",");
496 set_field_names_print(pos
, ITEM_SCOPE
);
497 if (pos
->print_names
)
498 fprintf(pos
->fp
, " event.context =");
499 field_nr_saved
= pos
->field_nr
;
501 set_field_names_print(pos
, ITEM_CONTEXT
);
502 ret
= generic_rw(ppos
, &event
->event_context
->p
);
505 pos
->field_nr
= field_nr_saved
;
508 /* Read and print event payload */
509 if (event
->event_fields
) {
510 if (pos
->field_nr
++ != 0)
511 fprintf(pos
->fp
, ",");
512 set_field_names_print(pos
, ITEM_SCOPE
);
513 if (pos
->print_names
)
514 fprintf(pos
->fp
, " event.fields =");
515 field_nr_saved
= pos
->field_nr
;
517 set_field_names_print(pos
, ITEM_PAYLOAD
);
518 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
521 pos
->field_nr
= field_nr_saved
;
524 fprintf(pos
->fp
, "\n");
530 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
535 struct bt_trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
536 void (*packet_seek
)(struct bt_stream_pos
*pos
, size_t index
,
537 int whence
), FILE *metadata_fp
)
539 struct ctf_text_stream_pos
*pos
;
542 pos
= g_new0(struct ctf_text_stream_pos
, 1);
544 pos
->last_real_timestamp
= -1ULL;
545 pos
->last_cycles_timestamp
= -1ULL;
546 switch (flags
& O_ACCMODE
) {
551 fp
= fopen(path
, "w");
555 pos
->parent
.rw_table
= write_dispatch_table
;
556 pos
->parent
.event_cb
= ctf_text_write_event
;
557 pos
->parent
.trace
= &pos
->trace_descriptor
;
558 pos
->print_names
= 0;
559 babeltrace_ctf_console_output
++;
563 fprintf(stderr
, "[error] Incorrect open flags.\n");
567 return &pos
->trace_descriptor
;
574 int ctf_text_close_trace(struct bt_trace_descriptor
*td
)
577 struct ctf_text_stream_pos
*pos
=
578 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
580 babeltrace_ctf_console_output
--;
581 if (pos
->fp
!= stdout
) {
582 ret
= fclose(pos
->fp
);
584 perror("Error on fclose");
593 void __attribute__((constructor
)) ctf_text_init(void)
597 ctf_text_format
.name
= g_quark_from_static_string("text");
598 ret
= bt_register_format(&ctf_text_format
);
603 void __attribute__((destructor
)) ctf_text_exit(void)
605 bt_unregister_format(&ctf_text_format
);