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,
63 BT_LOGLEVEL_EMERG
= 0,
64 BT_LOGLEVEL_ALERT
= 1,
67 BT_LOGLEVEL_WARNING
= 4,
68 BT_LOGLEVEL_NOTICE
= 5,
70 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
71 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
72 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
73 BT_LOGLEVEL_DEBUG_MODULE
= 10,
74 BT_LOGLEVEL_DEBUG_UNIT
= 11,
75 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
76 BT_LOGLEVEL_DEBUG_LINE
= 13,
77 BT_LOGLEVEL_DEBUG
= 14,
81 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
82 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
83 int whence
), FILE *metadata_fp
);
84 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
87 rw_dispatch write_dispatch_table
[] = {
88 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
89 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
90 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
91 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
92 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
93 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
94 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
95 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
99 struct format ctf_text_format
= {
100 .open_trace
= ctf_text_open_trace
,
101 .close_trace
= ctf_text_close_trace
,
104 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
105 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
106 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
107 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
108 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
111 void __attribute__((constructor
)) init_quarks(void)
113 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
114 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
115 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
116 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
117 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
120 int print_field(struct definition
*definition
)
122 /* Print all fields in verbose mode */
123 if (babeltrace_verbose
)
126 /* Filter out part of the packet context */
127 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
129 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
131 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
133 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
135 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
142 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
146 if (opt_all_field_names
|| opt_scope_field_names
)
147 pos
->print_names
= 1;
149 pos
->print_names
= 0;
152 if (opt_all_field_names
|| opt_header_field_names
)
153 pos
->print_names
= 1;
155 pos
->print_names
= 0;
158 if (opt_all_field_names
|| opt_context_field_names
)
159 pos
->print_names
= 1;
161 pos
->print_names
= 0;
164 if (opt_all_field_names
|| opt_payload_field_names
)
165 pos
->print_names
= 1;
167 pos
->print_names
= 0;
176 const char *print_loglevel(int value
)
181 case BT_LOGLEVEL_EMERG
:
182 return "TRACE_EMERG";
183 case BT_LOGLEVEL_ALERT
:
184 return "TRACE_ALERT";
185 case BT_LOGLEVEL_CRIT
:
187 case BT_LOGLEVEL_ERR
:
189 case BT_LOGLEVEL_WARNING
:
190 return "TRACE_WARNING";
191 case BT_LOGLEVEL_NOTICE
:
192 return "TRACE_NOTICE";
193 case BT_LOGLEVEL_INFO
:
195 case BT_LOGLEVEL_DEBUG_SYSTEM
:
196 return "TRACE_DEBUG_SYSTEM";
197 case BT_LOGLEVEL_DEBUG_PROGRAM
:
198 return "TRACE_DEBUG_PROGRAM";
199 case BT_LOGLEVEL_DEBUG_PROCESS
:
200 return "TRACE_DEBUG_PROCESS";
201 case BT_LOGLEVEL_DEBUG_MODULE
:
202 return "TRACE_DEBUG_MODULE";
203 case BT_LOGLEVEL_DEBUG_UNIT
:
204 return "TRACE_DEBUG_UNIT";
205 case BT_LOGLEVEL_DEBUG_FUNCTION
:
206 return "TRACE_DEBUG_FUNCTION";
207 case BT_LOGLEVEL_DEBUG_LINE
:
208 return "TRACE_DEBUG_LINE";
209 case BT_LOGLEVEL_DEBUG
:
210 return "TRACE_DEBUG";
212 return "<<UNKNOWN>>";
217 int ctf_text_write_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
220 struct ctf_text_stream_pos
*pos
=
221 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
222 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
224 struct ctf_event_declaration
*event_class
;
225 struct ctf_event_definition
*event
;
230 id
= stream
->event_id
;
232 if (id
>= stream_class
->events_by_id
->len
) {
233 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
236 event
= g_ptr_array_index(stream
->events_by_id
, id
);
238 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
241 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
243 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
247 /* Print events discarded */
248 if (stream
->events_discarded
) {
250 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events between [",
251 stream
->events_discarded
);
252 if (opt_clock_cycles
) {
253 ctf_print_timestamp(stderr
, stream
,
254 stream
->prev_cycles_timestamp
);
255 fprintf(stderr
, "] and [");
256 ctf_print_timestamp(stderr
, stream
,
257 stream
->prev_cycles_timestamp_end
);
259 ctf_print_timestamp(stderr
, stream
,
260 stream
->prev_real_timestamp
);
261 fprintf(stderr
, "] and [");
262 ctf_print_timestamp(stderr
, stream
,
263 stream
->prev_real_timestamp_end
);
265 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
267 stream
->events_discarded
= 0;
270 if (stream
->has_timestamp
) {
271 set_field_names_print(pos
, ITEM_HEADER
);
272 if (pos
->print_names
)
273 fprintf(pos
->fp
, "timestamp = ");
275 fprintf(pos
->fp
, "[");
276 if (opt_clock_cycles
) {
277 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
279 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
281 if (!pos
->print_names
)
282 fprintf(pos
->fp
, "]");
284 if (pos
->print_names
)
285 fprintf(pos
->fp
, ", ");
287 fprintf(pos
->fp
, " ");
289 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
290 uint64_t delta
, delta_sec
, delta_nsec
;
292 set_field_names_print(pos
, ITEM_HEADER
);
293 if (pos
->print_names
)
294 fprintf(pos
->fp
, "delta = ");
296 fprintf(pos
->fp
, "(");
297 if (pos
->last_real_timestamp
!= -1ULL) {
298 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
299 delta_sec
= delta
/ NSEC_PER_SEC
;
300 delta_nsec
= delta
% NSEC_PER_SEC
;
301 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
302 delta_sec
, delta_nsec
);
304 fprintf(pos
->fp
, "+?.?????????");
306 if (!pos
->print_names
)
307 fprintf(pos
->fp
, ")");
309 if (pos
->print_names
)
310 fprintf(pos
->fp
, ", ");
312 fprintf(pos
->fp
, " ");
313 pos
->last_real_timestamp
= stream
->real_timestamp
;
314 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
317 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
318 set_field_names_print(pos
, ITEM_HEADER
);
319 if (pos
->print_names
) {
320 fprintf(pos
->fp
, "trace = ");
322 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
323 if (pos
->print_names
)
324 fprintf(pos
->fp
, ", ");
326 fprintf(pos
->fp
, " ");
328 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
329 && stream_class
->trace
->env
.hostname
[0] != '\0') {
330 set_field_names_print(pos
, ITEM_HEADER
);
331 if (pos
->print_names
) {
332 fprintf(pos
->fp
, "trace:hostname = ");
334 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
335 if (pos
->print_names
)
336 fprintf(pos
->fp
, ", ");
339 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
340 set_field_names_print(pos
, ITEM_HEADER
);
341 if (pos
->print_names
) {
342 fprintf(pos
->fp
, "trace:domain = ");
344 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
345 if (pos
->print_names
)
346 fprintf(pos
->fp
, ", ");
349 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
350 && stream_class
->trace
->env
.procname
[0] != '\0') {
351 set_field_names_print(pos
, ITEM_HEADER
);
352 if (pos
->print_names
) {
353 fprintf(pos
->fp
, "trace:procname = ");
354 } else if (dom_print
) {
355 fprintf(pos
->fp
, ":");
357 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
358 if (pos
->print_names
)
359 fprintf(pos
->fp
, ", ");
362 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
363 && stream_class
->trace
->env
.vpid
!= -1) {
364 set_field_names_print(pos
, ITEM_HEADER
);
365 if (pos
->print_names
) {
366 fprintf(pos
->fp
, "trace:vpid = ");
367 } else if (dom_print
) {
368 fprintf(pos
->fp
, ":");
370 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
371 if (pos
->print_names
)
372 fprintf(pos
->fp
, ", ");
375 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
376 set_field_names_print(pos
, ITEM_HEADER
);
377 if (pos
->print_names
) {
378 fprintf(pos
->fp
, "loglevel = ");
379 } else if (dom_print
) {
380 fprintf(pos
->fp
, ":");
382 fprintf(pos
->fp
, "%s (%d)",
383 print_loglevel(event_class
->loglevel
),
384 event_class
->loglevel
);
385 if (pos
->print_names
)
386 fprintf(pos
->fp
, ", ");
389 if ((opt_emf_field
|| opt_all_fields
) && event_class
->model_emf_uri
) {
390 set_field_names_print(pos
, ITEM_HEADER
);
391 if (pos
->print_names
) {
392 fprintf(pos
->fp
, "model.emf.uri = ");
393 } else if (dom_print
) {
394 fprintf(pos
->fp
, ":");
396 fprintf(pos
->fp
, "\"%s\"",
397 g_quark_to_string(event_class
->model_emf_uri
));
398 if (pos
->print_names
)
399 fprintf(pos
->fp
, ", ");
402 if (dom_print
&& !pos
->print_names
)
403 fprintf(pos
->fp
, " ");
404 set_field_names_print(pos
, ITEM_HEADER
);
405 if (pos
->print_names
)
406 fprintf(pos
->fp
, "name = ");
407 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
408 if (pos
->print_names
)
411 fprintf(pos
->fp
, ":");
413 /* print cpuid field from packet context */
414 if (stream
->stream_packet_context
) {
415 if (pos
->field_nr
++ != 0)
416 fprintf(pos
->fp
, ",");
417 set_field_names_print(pos
, ITEM_SCOPE
);
418 if (pos
->print_names
)
419 fprintf(pos
->fp
, " stream.packet.context =");
420 field_nr_saved
= pos
->field_nr
;
422 set_field_names_print(pos
, ITEM_CONTEXT
);
423 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
426 pos
->field_nr
= field_nr_saved
;
429 /* Only show the event header in verbose mode */
430 if (babeltrace_verbose
&& stream
->stream_event_header
) {
431 if (pos
->field_nr
++ != 0)
432 fprintf(pos
->fp
, ",");
433 set_field_names_print(pos
, ITEM_SCOPE
);
434 if (pos
->print_names
)
435 fprintf(pos
->fp
, " stream.event.header =");
436 field_nr_saved
= pos
->field_nr
;
438 set_field_names_print(pos
, ITEM_CONTEXT
);
439 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
442 pos
->field_nr
= field_nr_saved
;
445 /* print stream-declared event context */
446 if (stream
->stream_event_context
) {
447 if (pos
->field_nr
++ != 0)
448 fprintf(pos
->fp
, ",");
449 set_field_names_print(pos
, ITEM_SCOPE
);
450 if (pos
->print_names
)
451 fprintf(pos
->fp
, " stream.event.context =");
452 field_nr_saved
= pos
->field_nr
;
454 set_field_names_print(pos
, ITEM_CONTEXT
);
455 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
458 pos
->field_nr
= field_nr_saved
;
461 /* print event-declared event context */
462 if (event
->event_context
) {
463 if (pos
->field_nr
++ != 0)
464 fprintf(pos
->fp
, ",");
465 set_field_names_print(pos
, ITEM_SCOPE
);
466 if (pos
->print_names
)
467 fprintf(pos
->fp
, " event.context =");
468 field_nr_saved
= pos
->field_nr
;
470 set_field_names_print(pos
, ITEM_CONTEXT
);
471 ret
= generic_rw(ppos
, &event
->event_context
->p
);
474 pos
->field_nr
= field_nr_saved
;
477 /* Read and print event payload */
478 if (event
->event_fields
) {
479 if (pos
->field_nr
++ != 0)
480 fprintf(pos
->fp
, ",");
481 set_field_names_print(pos
, ITEM_SCOPE
);
482 if (pos
->print_names
)
483 fprintf(pos
->fp
, " event.fields =");
484 field_nr_saved
= pos
->field_nr
;
486 set_field_names_print(pos
, ITEM_PAYLOAD
);
487 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
490 pos
->field_nr
= field_nr_saved
;
493 fprintf(pos
->fp
, "\n");
499 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
504 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
505 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
506 int whence
), FILE *metadata_fp
)
508 struct ctf_text_stream_pos
*pos
;
511 pos
= g_new0(struct ctf_text_stream_pos
, 1);
513 pos
->last_real_timestamp
= -1ULL;
514 pos
->last_cycles_timestamp
= -1ULL;
515 switch (flags
& O_ACCMODE
) {
520 fp
= fopen(path
, "w");
524 pos
->parent
.rw_table
= write_dispatch_table
;
525 pos
->parent
.event_cb
= ctf_text_write_event
;
526 pos
->print_names
= 0;
530 fprintf(stderr
, "[error] Incorrect open flags.\n");
534 return &pos
->trace_descriptor
;
540 void ctf_text_close_trace(struct trace_descriptor
*td
)
542 struct ctf_text_stream_pos
*pos
=
543 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
548 void __attribute__((constructor
)) ctf_text_init(void)
552 ctf_text_format
.name
= g_quark_from_static_string("text");
553 ret
= bt_register_format(&ctf_text_format
);