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-debuginfo.h>
38 #include <sys/types.h>
46 #define NSEC_PER_SEC 1000000000ULL
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,
73 BT_LOGLEVEL_EMERG
= 0,
74 BT_LOGLEVEL_ALERT
= 1,
77 BT_LOGLEVEL_WARNING
= 4,
78 BT_LOGLEVEL_NOTICE
= 5,
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,
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
);
95 int ctf_text_close_trace(struct bt_trace_descriptor
*descriptor
);
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
,
110 struct bt_format ctf_text_format
= {
111 .open_trace
= ctf_text_open_trace
,
112 .close_trace
= ctf_text_close_trace
,
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
;
123 void __attribute__((constructor
)) init_quarks(void)
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");
134 struct ctf_callsite_dups
*ctf_trace_callsite_lookup(struct ctf_trace
*trace
,
135 GQuark callsite_name
)
137 return g_hash_table_lookup(trace
->callsites
,
138 (gpointer
) (unsigned long) callsite_name
);
141 int print_field(struct bt_definition
*definition
)
143 /* Print all fields in verbose mode */
144 if (babeltrace_verbose
)
147 /* Filter out part of the packet context */
148 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
150 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
152 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
154 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
156 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
158 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM
)
165 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
169 if (opt_all_field_names
|| opt_scope_field_names
)
170 pos
->print_names
= 1;
172 pos
->print_names
= 0;
175 if (opt_all_field_names
|| opt_header_field_names
)
176 pos
->print_names
= 1;
178 pos
->print_names
= 0;
181 if (opt_all_field_names
|| opt_context_field_names
)
182 pos
->print_names
= 1;
184 pos
->print_names
= 0;
187 if (opt_all_field_names
|| opt_payload_field_names
)
188 pos
->print_names
= 1;
190 pos
->print_names
= 0;
199 const char *print_loglevel(int value
)
204 case BT_LOGLEVEL_EMERG
:
205 return "TRACE_EMERG";
206 case BT_LOGLEVEL_ALERT
:
207 return "TRACE_ALERT";
208 case BT_LOGLEVEL_CRIT
:
210 case BT_LOGLEVEL_ERR
:
212 case BT_LOGLEVEL_WARNING
:
213 return "TRACE_WARNING";
214 case BT_LOGLEVEL_NOTICE
:
215 return "TRACE_NOTICE";
216 case BT_LOGLEVEL_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";
235 return "<<UNKNOWN>>";
240 int ctf_text_write_event(struct bt_stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
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
;
246 struct ctf_event_declaration
*event_class
;
247 struct ctf_event_definition
*event
;
252 id
= stream
->event_id
;
254 if (id
>= stream_class
->events_by_id
->len
) {
255 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
258 event
= g_ptr_array_index(stream
->events_by_id
, id
);
260 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
263 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
265 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
269 handle_debug_info_event(stream_class
, event
);
271 if (stream
->has_timestamp
) {
272 set_field_names_print(pos
, ITEM_HEADER
);
273 if (pos
->print_names
)
274 fprintf(pos
->fp
, "timestamp = ");
276 fprintf(pos
->fp
, "[");
277 if (opt_clock_cycles
) {
278 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
280 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
282 if (!pos
->print_names
)
283 fprintf(pos
->fp
, "]");
285 if (pos
->print_names
)
286 fprintf(pos
->fp
, ", ");
288 fprintf(pos
->fp
, " ");
290 if (opt_delta_field
&& stream
->has_timestamp
) {
291 uint64_t delta
, delta_sec
, delta_nsec
;
293 set_field_names_print(pos
, ITEM_HEADER
);
294 if (pos
->print_names
)
295 fprintf(pos
->fp
, "delta = ");
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
);
305 fprintf(pos
->fp
, "+?.?????????");
307 if (!pos
->print_names
)
308 fprintf(pos
->fp
, ")");
310 if (pos
->print_names
)
311 fprintf(pos
->fp
, ", ");
313 fprintf(pos
->fp
, " ");
314 pos
->last_real_timestamp
= stream
->real_timestamp
;
315 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
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 = ");
323 fprintf(pos
->fp
, "%s", stream_class
->trace
->parent
.path
);
324 if (pos
->print_names
)
325 fprintf(pos
->fp
, ", ");
327 fprintf(pos
->fp
, " ");
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 = ");
335 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
336 if (pos
->print_names
)
337 fprintf(pos
->fp
, ", ");
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 = ");
345 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
346 if (pos
->print_names
)
347 fprintf(pos
->fp
, ", ");
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
, ":");
358 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
359 if (pos
->print_names
)
360 fprintf(pos
->fp
, ", ");
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
, ":");
371 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
372 if (pos
->print_names
)
373 fprintf(pos
->fp
, ", ");
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
, ":");
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
, ", ");
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
, ":");
397 fprintf(pos
->fp
, "\"%s\"",
398 g_quark_to_string(event_class
->model_emf_uri
));
399 if (pos
->print_names
)
400 fprintf(pos
->fp
, ", ");
403 if ((opt_callsite_field
|| opt_all_fields
)) {
404 struct ctf_callsite_dups
*cs_dups
;
405 struct ctf_callsite
*callsite
;
407 cs_dups
= ctf_trace_callsite_lookup(stream_class
->trace
,
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
, ":");
418 fprintf(pos
->fp
, "[");
419 bt_list_for_each_entry(callsite
, &cs_dups
->head
, node
) {
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
,
427 fprintf(pos
->fp
, "%s:%s:%" PRIu64
"",
428 callsite
->func
, callsite
->file
,
433 fprintf(pos
->fp
, "]");
434 if (pos
->print_names
)
435 fprintf(pos
->fp
, ", ");
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
)
448 fprintf(pos
->fp
, ":");
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
;
459 set_field_names_print(pos
, ITEM_CONTEXT
);
460 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
463 pos
->field_nr
= field_nr_saved
;
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
;
475 set_field_names_print(pos
, ITEM_CONTEXT
);
476 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
479 pos
->field_nr
= field_nr_saved
;
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
;
491 set_field_names_print(pos
, ITEM_CONTEXT
);
492 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
495 pos
->field_nr
= field_nr_saved
;
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
;
507 set_field_names_print(pos
, ITEM_CONTEXT
);
508 ret
= generic_rw(ppos
, &event
->event_context
->p
);
511 pos
->field_nr
= field_nr_saved
;
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
;
523 set_field_names_print(pos
, ITEM_PAYLOAD
);
524 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
527 pos
->field_nr
= field_nr_saved
;
530 fprintf(pos
->fp
, "\n");
536 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
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
)
545 struct ctf_text_stream_pos
*pos
;
548 pos
= g_new0(struct ctf_text_stream_pos
, 1);
550 pos
->last_real_timestamp
= -1ULL;
551 pos
->last_cycles_timestamp
= -1ULL;
552 switch (flags
& O_ACCMODE
) {
557 fp
= fopen(path
, "w");
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
++;
569 fprintf(stderr
, "[error] Incorrect open flags.\n");
573 return &pos
->trace_descriptor
;
580 int ctf_text_close_trace(struct bt_trace_descriptor
*td
)
583 struct ctf_text_stream_pos
*pos
=
584 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
586 babeltrace_ctf_console_output
--;
587 if (pos
->fp
!= stdout
) {
588 ret
= fclose(pos
->fp
);
590 perror("Error on fclose");
599 void __attribute__((constructor
)) ctf_text_init(void)
603 ctf_text_format
.name
= g_quark_from_static_string("text");
604 ret
= bt_register_format(&ctf_text_format
);
609 void __attribute__((destructor
)) ctf_text_exit(void)
611 bt_unregister_format(&ctf_text_format
);