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,
62 BT_LOGLEVEL_EMERG
= 0,
63 BT_LOGLEVEL_ALERT
= 1,
66 BT_LOGLEVEL_WARNING
= 4,
67 BT_LOGLEVEL_NOTICE
= 5,
69 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
70 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
71 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
72 BT_LOGLEVEL_DEBUG_MODULE
= 10,
73 BT_LOGLEVEL_DEBUG_UNIT
= 11,
74 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
75 BT_LOGLEVEL_DEBUG_LINE
= 13,
76 BT_LOGLEVEL_DEBUG
= 14,
80 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
81 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
82 int whence
), FILE *metadata_fp
);
83 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
86 rw_dispatch write_dispatch_table
[] = {
87 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
88 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
89 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
90 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
91 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
92 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
93 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
94 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
98 struct format ctf_text_format
= {
99 .open_trace
= ctf_text_open_trace
,
100 .close_trace
= ctf_text_close_trace
,
103 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
104 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
105 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
106 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
107 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
110 void __attribute__((constructor
)) init_quarks(void)
112 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
113 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
114 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
115 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
116 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
119 int print_field(struct definition
*definition
)
121 /* Print all fields in verbose mode */
122 if (babeltrace_verbose
)
125 /* Filter out part of the packet context */
126 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
128 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
130 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
132 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
134 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
141 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
145 if (opt_all_field_names
|| opt_scope_field_names
)
146 pos
->print_names
= 1;
148 pos
->print_names
= 0;
151 if (opt_all_field_names
|| opt_header_field_names
)
152 pos
->print_names
= 1;
154 pos
->print_names
= 0;
157 if (opt_all_field_names
|| opt_context_field_names
)
158 pos
->print_names
= 1;
160 pos
->print_names
= 0;
163 if (opt_all_field_names
|| opt_payload_field_names
)
164 pos
->print_names
= 1;
166 pos
->print_names
= 0;
175 const char *print_loglevel(int value
)
180 case BT_LOGLEVEL_EMERG
:
181 return "TRACE_EMERG";
182 case BT_LOGLEVEL_ALERT
:
183 return "TRACE_ALERT";
184 case BT_LOGLEVEL_CRIT
:
186 case BT_LOGLEVEL_ERR
:
188 case BT_LOGLEVEL_WARNING
:
189 return "TRACE_WARNING";
190 case BT_LOGLEVEL_NOTICE
:
191 return "TRACE_NOTICE";
192 case BT_LOGLEVEL_INFO
:
194 case BT_LOGLEVEL_DEBUG_SYSTEM
:
195 return "TRACE_DEBUG_SYSTEM";
196 case BT_LOGLEVEL_DEBUG_PROGRAM
:
197 return "TRACE_DEBUG_PROGRAM";
198 case BT_LOGLEVEL_DEBUG_PROCESS
:
199 return "TRACE_DEBUG_PROCESS";
200 case BT_LOGLEVEL_DEBUG_MODULE
:
201 return "TRACE_DEBUG_MODULE";
202 case BT_LOGLEVEL_DEBUG_UNIT
:
203 return "TRACE_DEBUG_UNIT";
204 case BT_LOGLEVEL_DEBUG_FUNCTION
:
205 return "TRACE_DEBUG_FUNCTION";
206 case BT_LOGLEVEL_DEBUG_LINE
:
207 return "TRACE_DEBUG_LINE";
208 case BT_LOGLEVEL_DEBUG
:
209 return "TRACE_DEBUG";
211 return "<<UNKNOWN>>";
216 int ctf_text_write_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
219 struct ctf_text_stream_pos
*pos
=
220 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
221 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
223 struct ctf_event_declaration
*event_class
;
224 struct ctf_event_definition
*event
;
229 id
= stream
->event_id
;
231 if (id
>= stream_class
->events_by_id
->len
) {
232 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
235 event
= g_ptr_array_index(stream
->events_by_id
, id
);
237 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
240 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
242 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
246 /* Print events discarded */
247 if (stream
->events_discarded
) {
249 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events between [",
250 stream
->events_discarded
);
251 if (opt_clock_cycles
) {
252 ctf_print_timestamp(stderr
, stream
,
253 stream
->prev_cycles_timestamp
);
254 fprintf(stderr
, "] and [");
255 ctf_print_timestamp(stderr
, stream
,
256 stream
->prev_cycles_timestamp_end
);
258 ctf_print_timestamp(stderr
, stream
,
259 stream
->prev_real_timestamp
);
260 fprintf(stderr
, "] and [");
261 ctf_print_timestamp(stderr
, stream
,
262 stream
->prev_real_timestamp_end
);
264 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
266 stream
->events_discarded
= 0;
269 if (stream
->has_timestamp
) {
270 set_field_names_print(pos
, ITEM_HEADER
);
271 if (pos
->print_names
)
272 fprintf(pos
->fp
, "timestamp = ");
274 fprintf(pos
->fp
, "[");
275 if (opt_clock_cycles
) {
276 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
278 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
280 if (!pos
->print_names
)
281 fprintf(pos
->fp
, "]");
283 if (pos
->print_names
)
284 fprintf(pos
->fp
, ", ");
286 fprintf(pos
->fp
, " ");
288 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
289 uint64_t delta
, delta_sec
, delta_nsec
;
291 set_field_names_print(pos
, ITEM_HEADER
);
292 if (pos
->print_names
)
293 fprintf(pos
->fp
, "delta = ");
295 fprintf(pos
->fp
, "(");
296 if (pos
->last_real_timestamp
!= -1ULL) {
297 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
298 delta_sec
= delta
/ NSEC_PER_SEC
;
299 delta_nsec
= delta
% NSEC_PER_SEC
;
300 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
301 delta_sec
, delta_nsec
);
303 fprintf(pos
->fp
, "+?.?????????");
305 if (!pos
->print_names
)
306 fprintf(pos
->fp
, ")");
308 if (pos
->print_names
)
309 fprintf(pos
->fp
, ", ");
311 fprintf(pos
->fp
, " ");
312 pos
->last_real_timestamp
= stream
->real_timestamp
;
313 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
316 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
317 set_field_names_print(pos
, ITEM_HEADER
);
318 if (pos
->print_names
) {
319 fprintf(pos
->fp
, "trace = ");
321 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
322 if (pos
->print_names
)
323 fprintf(pos
->fp
, ", ");
325 fprintf(pos
->fp
, " ");
327 if ((opt_trace_hostname_field
|| opt_all_fields
|| opt_trace_default_fields
)
328 && stream_class
->trace
->env
.hostname
[0] != '\0') {
329 set_field_names_print(pos
, ITEM_HEADER
);
330 if (pos
->print_names
) {
331 fprintf(pos
->fp
, "trace:hostname = ");
333 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
334 if (pos
->print_names
)
335 fprintf(pos
->fp
, ", ");
338 if ((opt_trace_domain_field
|| opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
339 set_field_names_print(pos
, ITEM_HEADER
);
340 if (pos
->print_names
) {
341 fprintf(pos
->fp
, "trace:domain = ");
343 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
344 if (pos
->print_names
)
345 fprintf(pos
->fp
, ", ");
348 if ((opt_trace_procname_field
|| opt_all_fields
|| opt_trace_default_fields
)
349 && stream_class
->trace
->env
.procname
[0] != '\0') {
350 set_field_names_print(pos
, ITEM_HEADER
);
351 if (pos
->print_names
) {
352 fprintf(pos
->fp
, "trace:procname = ");
353 } else if (dom_print
) {
354 fprintf(pos
->fp
, ":");
356 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
357 if (pos
->print_names
)
358 fprintf(pos
->fp
, ", ");
361 if ((opt_trace_vpid_field
|| opt_all_fields
|| opt_trace_default_fields
)
362 && stream_class
->trace
->env
.vpid
!= -1) {
363 set_field_names_print(pos
, ITEM_HEADER
);
364 if (pos
->print_names
) {
365 fprintf(pos
->fp
, "trace:vpid = ");
366 } else if (dom_print
) {
367 fprintf(pos
->fp
, ":");
369 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
370 if (pos
->print_names
)
371 fprintf(pos
->fp
, ", ");
374 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
375 set_field_names_print(pos
, ITEM_HEADER
);
376 if (pos
->print_names
) {
377 fprintf(pos
->fp
, "loglevel = ");
378 } else if (dom_print
) {
379 fprintf(pos
->fp
, ":");
381 fprintf(pos
->fp
, "%s (%d)",
382 print_loglevel(event_class
->loglevel
),
383 event_class
->loglevel
);
384 if (pos
->print_names
)
385 fprintf(pos
->fp
, ", ");
388 if (dom_print
&& !pos
->print_names
)
389 fprintf(pos
->fp
, " ");
390 set_field_names_print(pos
, ITEM_HEADER
);
391 if (pos
->print_names
)
392 fprintf(pos
->fp
, "name = ");
393 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
394 if (pos
->print_names
)
397 fprintf(pos
->fp
, ":");
399 /* print cpuid field from packet context */
400 if (stream
->stream_packet_context
) {
401 if (pos
->field_nr
++ != 0)
402 fprintf(pos
->fp
, ",");
403 set_field_names_print(pos
, ITEM_SCOPE
);
404 if (pos
->print_names
)
405 fprintf(pos
->fp
, " stream.packet.context =");
406 field_nr_saved
= pos
->field_nr
;
408 set_field_names_print(pos
, ITEM_CONTEXT
);
409 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
412 pos
->field_nr
= field_nr_saved
;
415 /* Only show the event header in verbose mode */
416 if (babeltrace_verbose
&& stream
->stream_event_header
) {
417 if (pos
->field_nr
++ != 0)
418 fprintf(pos
->fp
, ",");
419 set_field_names_print(pos
, ITEM_SCOPE
);
420 if (pos
->print_names
)
421 fprintf(pos
->fp
, " stream.event.header =");
422 field_nr_saved
= pos
->field_nr
;
424 set_field_names_print(pos
, ITEM_CONTEXT
);
425 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
428 pos
->field_nr
= field_nr_saved
;
431 /* print stream-declared event context */
432 if (stream
->stream_event_context
) {
433 if (pos
->field_nr
++ != 0)
434 fprintf(pos
->fp
, ",");
435 set_field_names_print(pos
, ITEM_SCOPE
);
436 if (pos
->print_names
)
437 fprintf(pos
->fp
, " stream.event.context =");
438 field_nr_saved
= pos
->field_nr
;
440 set_field_names_print(pos
, ITEM_CONTEXT
);
441 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
444 pos
->field_nr
= field_nr_saved
;
447 /* print event-declared event context */
448 if (event
->event_context
) {
449 if (pos
->field_nr
++ != 0)
450 fprintf(pos
->fp
, ",");
451 set_field_names_print(pos
, ITEM_SCOPE
);
452 if (pos
->print_names
)
453 fprintf(pos
->fp
, " event.context =");
454 field_nr_saved
= pos
->field_nr
;
456 set_field_names_print(pos
, ITEM_CONTEXT
);
457 ret
= generic_rw(ppos
, &event
->event_context
->p
);
460 pos
->field_nr
= field_nr_saved
;
463 /* Read and print event payload */
464 if (event
->event_fields
) {
465 if (pos
->field_nr
++ != 0)
466 fprintf(pos
->fp
, ",");
467 set_field_names_print(pos
, ITEM_SCOPE
);
468 if (pos
->print_names
)
469 fprintf(pos
->fp
, " event.fields =");
470 field_nr_saved
= pos
->field_nr
;
472 set_field_names_print(pos
, ITEM_PAYLOAD
);
473 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
476 pos
->field_nr
= field_nr_saved
;
479 fprintf(pos
->fp
, "\n");
485 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
490 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
491 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
492 int whence
), FILE *metadata_fp
)
494 struct ctf_text_stream_pos
*pos
;
497 pos
= g_new0(struct ctf_text_stream_pos
, 1);
499 pos
->last_real_timestamp
= -1ULL;
500 pos
->last_cycles_timestamp
= -1ULL;
501 switch (flags
& O_ACCMODE
) {
506 fp
= fopen(path
, "w");
510 pos
->parent
.rw_table
= write_dispatch_table
;
511 pos
->parent
.event_cb
= ctf_text_write_event
;
512 pos
->print_names
= 0;
516 fprintf(stderr
, "[error] Incorrect open flags.\n");
520 return &pos
->trace_descriptor
;
526 void ctf_text_close_trace(struct trace_descriptor
*td
)
528 struct ctf_text_stream_pos
*pos
=
529 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
534 void __attribute__((constructor
)) ctf_text_init(void)
538 ctf_text_format
.name
= g_quark_from_static_string("text");
539 ret
= bt_register_format(&ctf_text_format
);