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.
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf-text/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
29 #include <sys/types.h>
37 #define NSEC_PER_SEC 1000000000ULL
39 int opt_all_field_names
,
40 opt_scope_field_names
,
41 opt_header_field_names
,
42 opt_context_field_names
,
43 opt_payload_field_names
,
46 opt_trace_domain_field
,
47 opt_trace_procname_field
,
49 opt_trace_hostname_field
,
50 opt_trace_default_fields
= 1,
64 BT_LOGLEVEL_EMERG
= 0,
65 BT_LOGLEVEL_ALERT
= 1,
68 BT_LOGLEVEL_WARNING
= 4,
69 BT_LOGLEVEL_NOTICE
= 5,
71 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
72 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
73 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
74 BT_LOGLEVEL_DEBUG_MODULE
= 10,
75 BT_LOGLEVEL_DEBUG_UNIT
= 11,
76 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
77 BT_LOGLEVEL_DEBUG_LINE
= 13,
78 BT_LOGLEVEL_DEBUG
= 14,
82 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
83 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
84 int whence
), FILE *metadata_fp
);
85 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
88 rw_dispatch write_dispatch_table
[] = {
89 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
90 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
91 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
92 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
93 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
94 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
95 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
96 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
100 struct format ctf_text_format
= {
101 .open_trace
= ctf_text_open_trace
,
102 .close_trace
= ctf_text_close_trace
,
105 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
106 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
107 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
108 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
109 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
112 void __attribute__((constructor
)) init_quarks(void)
114 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
115 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
116 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
117 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
118 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
122 struct ctf_callsite_dups
*ctf_trace_callsite_lookup(struct ctf_trace
*trace
,
123 GQuark callsite_name
)
125 return g_hash_table_lookup(trace
->callsites
,
126 (gpointer
) (unsigned long) callsite_name
);
129 int print_field(struct definition
*definition
)
131 /* Print all fields in verbose mode */
132 if (babeltrace_verbose
)
135 /* Filter out part of the packet context */
136 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
138 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
140 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
142 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
144 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
151 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
155 if (opt_all_field_names
|| opt_scope_field_names
)
156 pos
->print_names
= 1;
158 pos
->print_names
= 0;
161 if (opt_all_field_names
|| opt_header_field_names
)
162 pos
->print_names
= 1;
164 pos
->print_names
= 0;
167 if (opt_all_field_names
|| opt_context_field_names
)
168 pos
->print_names
= 1;
170 pos
->print_names
= 0;
173 if (opt_all_field_names
|| opt_payload_field_names
)
174 pos
->print_names
= 1;
176 pos
->print_names
= 0;
185 const char *print_loglevel(int value
)
190 case BT_LOGLEVEL_EMERG
:
191 return "TRACE_EMERG";
192 case BT_LOGLEVEL_ALERT
:
193 return "TRACE_ALERT";
194 case BT_LOGLEVEL_CRIT
:
196 case BT_LOGLEVEL_ERR
:
198 case BT_LOGLEVEL_WARNING
:
199 return "TRACE_WARNING";
200 case BT_LOGLEVEL_NOTICE
:
201 return "TRACE_NOTICE";
202 case BT_LOGLEVEL_INFO
:
204 case BT_LOGLEVEL_DEBUG_SYSTEM
:
205 return "TRACE_DEBUG_SYSTEM";
206 case BT_LOGLEVEL_DEBUG_PROGRAM
:
207 return "TRACE_DEBUG_PROGRAM";
208 case BT_LOGLEVEL_DEBUG_PROCESS
:
209 return "TRACE_DEBUG_PROCESS";
210 case BT_LOGLEVEL_DEBUG_MODULE
:
211 return "TRACE_DEBUG_MODULE";
212 case BT_LOGLEVEL_DEBUG_UNIT
:
213 return "TRACE_DEBUG_UNIT";
214 case BT_LOGLEVEL_DEBUG_FUNCTION
:
215 return "TRACE_DEBUG_FUNCTION";
216 case BT_LOGLEVEL_DEBUG_LINE
:
217 return "TRACE_DEBUG_LINE";
218 case BT_LOGLEVEL_DEBUG
:
219 return "TRACE_DEBUG";
221 return "<<UNKNOWN>>";
226 int ctf_text_write_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
229 struct ctf_text_stream_pos
*pos
=
230 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
231 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
233 struct ctf_event_declaration
*event_class
;
234 struct ctf_event_definition
*event
;
239 id
= stream
->event_id
;
241 if (id
>= stream_class
->events_by_id
->len
) {
242 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
245 event
= g_ptr_array_index(stream
->events_by_id
, id
);
247 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
250 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
252 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
256 /* Print events discarded */
257 if (stream
->events_discarded
) {
259 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events between [",
260 stream
->events_discarded
);
261 if (opt_clock_cycles
) {
262 ctf_print_timestamp(stderr
, stream
,
263 stream
->prev_cycles_timestamp
);
264 fprintf(stderr
, "] and [");
265 ctf_print_timestamp(stderr
, stream
,
266 stream
->prev_cycles_timestamp_end
);
268 ctf_print_timestamp(stderr
, stream
,
269 stream
->prev_real_timestamp
);
270 fprintf(stderr
, "] and [");
271 ctf_print_timestamp(stderr
, stream
,
272 stream
->prev_real_timestamp_end
);
274 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
276 stream
->events_discarded
= 0;
279 if (stream
->has_timestamp
) {
280 set_field_names_print(pos
, ITEM_HEADER
);
281 if (pos
->print_names
)
282 fprintf(pos
->fp
, "timestamp = ");
284 fprintf(pos
->fp
, "[");
285 if (opt_clock_cycles
) {
286 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
288 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
290 if (!pos
->print_names
)
291 fprintf(pos
->fp
, "]");
293 if (pos
->print_names
)
294 fprintf(pos
->fp
, ", ");
296 fprintf(pos
->fp
, " ");
298 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
299 uint64_t delta
, delta_sec
, delta_nsec
;
301 set_field_names_print(pos
, ITEM_HEADER
);
302 if (pos
->print_names
)
303 fprintf(pos
->fp
, "delta = ");
305 fprintf(pos
->fp
, "(");
306 if (pos
->last_real_timestamp
!= -1ULL) {
307 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
308 delta_sec
= delta
/ NSEC_PER_SEC
;
309 delta_nsec
= delta
% NSEC_PER_SEC
;
310 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
311 delta_sec
, delta_nsec
);
313 fprintf(pos
->fp
, "+?.?????????");
315 if (!pos
->print_names
)
316 fprintf(pos
->fp
, ")");
318 if (pos
->print_names
)
319 fprintf(pos
->fp
, ", ");
321 fprintf(pos
->fp
, " ");
322 pos
->last_real_timestamp
= stream
->real_timestamp
;
323 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
326 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
327 set_field_names_print(pos
, ITEM_HEADER
);
328 if (pos
->print_names
) {
329 fprintf(pos
->fp
, "trace = ");
331 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
332 if (pos
->print_names
)
333 fprintf(pos
->fp
, ", ");
335 fprintf(pos
->fp
, " ");
337 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
338 && stream_class
->trace
->env
.hostname
[0] != '\0') {
339 set_field_names_print(pos
, ITEM_HEADER
);
340 if (pos
->print_names
) {
341 fprintf(pos
->fp
, "trace:hostname = ");
343 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
344 if (pos
->print_names
)
345 fprintf(pos
->fp
, ", ");
348 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
349 set_field_names_print(pos
, ITEM_HEADER
);
350 if (pos
->print_names
) {
351 fprintf(pos
->fp
, "trace:domain = ");
353 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
354 if (pos
->print_names
)
355 fprintf(pos
->fp
, ", ");
358 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
359 && stream_class
->trace
->env
.procname
[0] != '\0') {
360 set_field_names_print(pos
, ITEM_HEADER
);
361 if (pos
->print_names
) {
362 fprintf(pos
->fp
, "trace:procname = ");
363 } else if (dom_print
) {
364 fprintf(pos
->fp
, ":");
366 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
367 if (pos
->print_names
)
368 fprintf(pos
->fp
, ", ");
371 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
372 && stream_class
->trace
->env
.vpid
!= -1) {
373 set_field_names_print(pos
, ITEM_HEADER
);
374 if (pos
->print_names
) {
375 fprintf(pos
->fp
, "trace:vpid = ");
376 } else if (dom_print
) {
377 fprintf(pos
->fp
, ":");
379 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
380 if (pos
->print_names
)
381 fprintf(pos
->fp
, ", ");
384 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
385 set_field_names_print(pos
, ITEM_HEADER
);
386 if (pos
->print_names
) {
387 fprintf(pos
->fp
, "loglevel = ");
388 } else if (dom_print
) {
389 fprintf(pos
->fp
, ":");
391 fprintf(pos
->fp
, "%s (%d)",
392 print_loglevel(event_class
->loglevel
),
393 event_class
->loglevel
);
394 if (pos
->print_names
)
395 fprintf(pos
->fp
, ", ");
398 if ((opt_emf_field
|| opt_all_fields
) && event_class
->model_emf_uri
) {
399 set_field_names_print(pos
, ITEM_HEADER
);
400 if (pos
->print_names
) {
401 fprintf(pos
->fp
, "model.emf.uri = ");
402 } else if (dom_print
) {
403 fprintf(pos
->fp
, ":");
405 fprintf(pos
->fp
, "\"%s\"",
406 g_quark_to_string(event_class
->model_emf_uri
));
407 if (pos
->print_names
)
408 fprintf(pos
->fp
, ", ");
411 if ((opt_callsite_field
|| opt_all_fields
)) {
412 struct ctf_callsite_dups
*cs_dups
;
413 struct ctf_callsite
*callsite
;
415 cs_dups
= ctf_trace_callsite_lookup(stream_class
->trace
,
420 set_field_names_print(pos
, ITEM_HEADER
);
421 if (pos
->print_names
) {
422 fprintf(pos
->fp
, "callsite = ");
423 } else if (dom_print
) {
424 fprintf(pos
->fp
, ":");
426 fprintf(pos
->fp
, "[");
427 bt_list_for_each_entry(callsite
, &cs_dups
->head
, node
) {
429 fprintf(pos
->fp
, ",");
430 if (CTF_CALLSITE_FIELD_IS_SET(callsite
, ip
)) {
431 fprintf(pos
->fp
, "%s@0x%" PRIx64
":%s:%" PRIu64
"",
432 callsite
->func
, callsite
->ip
, callsite
->file
,
435 fprintf(pos
->fp
, "%s:%s:%" PRIu64
"",
436 callsite
->func
, callsite
->file
,
441 fprintf(pos
->fp
, "]");
442 if (pos
->print_names
)
443 fprintf(pos
->fp
, ", ");
447 if (dom_print
&& !pos
->print_names
)
448 fprintf(pos
->fp
, " ");
449 set_field_names_print(pos
, ITEM_HEADER
);
450 if (pos
->print_names
)
451 fprintf(pos
->fp
, "name = ");
452 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
453 if (pos
->print_names
)
456 fprintf(pos
->fp
, ":");
458 /* print cpuid field from packet context */
459 if (stream
->stream_packet_context
) {
460 if (pos
->field_nr
++ != 0)
461 fprintf(pos
->fp
, ",");
462 set_field_names_print(pos
, ITEM_SCOPE
);
463 if (pos
->print_names
)
464 fprintf(pos
->fp
, " stream.packet.context =");
465 field_nr_saved
= pos
->field_nr
;
467 set_field_names_print(pos
, ITEM_CONTEXT
);
468 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
471 pos
->field_nr
= field_nr_saved
;
474 /* Only show the event header in verbose mode */
475 if (babeltrace_verbose
&& stream
->stream_event_header
) {
476 if (pos
->field_nr
++ != 0)
477 fprintf(pos
->fp
, ",");
478 set_field_names_print(pos
, ITEM_SCOPE
);
479 if (pos
->print_names
)
480 fprintf(pos
->fp
, " stream.event.header =");
481 field_nr_saved
= pos
->field_nr
;
483 set_field_names_print(pos
, ITEM_CONTEXT
);
484 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
487 pos
->field_nr
= field_nr_saved
;
490 /* print stream-declared event context */
491 if (stream
->stream_event_context
) {
492 if (pos
->field_nr
++ != 0)
493 fprintf(pos
->fp
, ",");
494 set_field_names_print(pos
, ITEM_SCOPE
);
495 if (pos
->print_names
)
496 fprintf(pos
->fp
, " stream.event.context =");
497 field_nr_saved
= pos
->field_nr
;
499 set_field_names_print(pos
, ITEM_CONTEXT
);
500 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
503 pos
->field_nr
= field_nr_saved
;
506 /* print event-declared event context */
507 if (event
->event_context
) {
508 if (pos
->field_nr
++ != 0)
509 fprintf(pos
->fp
, ",");
510 set_field_names_print(pos
, ITEM_SCOPE
);
511 if (pos
->print_names
)
512 fprintf(pos
->fp
, " event.context =");
513 field_nr_saved
= pos
->field_nr
;
515 set_field_names_print(pos
, ITEM_CONTEXT
);
516 ret
= generic_rw(ppos
, &event
->event_context
->p
);
519 pos
->field_nr
= field_nr_saved
;
522 /* Read and print event payload */
523 if (event
->event_fields
) {
524 if (pos
->field_nr
++ != 0)
525 fprintf(pos
->fp
, ",");
526 set_field_names_print(pos
, ITEM_SCOPE
);
527 if (pos
->print_names
)
528 fprintf(pos
->fp
, " event.fields =");
529 field_nr_saved
= pos
->field_nr
;
531 set_field_names_print(pos
, ITEM_PAYLOAD
);
532 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
535 pos
->field_nr
= field_nr_saved
;
538 fprintf(pos
->fp
, "\n");
544 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
549 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
550 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
551 int whence
), FILE *metadata_fp
)
553 struct ctf_text_stream_pos
*pos
;
556 pos
= g_new0(struct ctf_text_stream_pos
, 1);
558 pos
->last_real_timestamp
= -1ULL;
559 pos
->last_cycles_timestamp
= -1ULL;
560 switch (flags
& O_ACCMODE
) {
565 fp
= fopen(path
, "w");
569 pos
->parent
.rw_table
= write_dispatch_table
;
570 pos
->parent
.event_cb
= ctf_text_write_event
;
571 pos
->print_names
= 0;
575 fprintf(stderr
, "[error] Incorrect open flags.\n");
579 return &pos
->trace_descriptor
;
585 void ctf_text_close_trace(struct trace_descriptor
*td
)
587 struct ctf_text_stream_pos
*pos
=
588 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
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
);