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
,
61 BT_LOGLEVEL_EMERG
= 0,
62 BT_LOGLEVEL_ALERT
= 1,
65 BT_LOGLEVEL_WARNING
= 4,
66 BT_LOGLEVEL_NOTICE
= 5,
68 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
69 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
70 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
71 BT_LOGLEVEL_DEBUG_MODULE
= 10,
72 BT_LOGLEVEL_DEBUG_UNIT
= 11,
73 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
74 BT_LOGLEVEL_DEBUG_LINE
= 13,
75 BT_LOGLEVEL_DEBUG
= 14,
79 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
80 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
81 int whence
), FILE *metadata_fp
);
82 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
85 rw_dispatch write_dispatch_table
[] = {
86 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
87 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
88 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
89 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
90 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
91 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
92 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
93 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
97 struct format ctf_text_format
= {
98 .open_trace
= ctf_text_open_trace
,
99 .close_trace
= ctf_text_close_trace
,
102 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
103 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
104 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
105 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
106 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
109 void __attribute__((constructor
)) init_quarks(void)
111 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
112 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
113 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
114 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
115 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
118 int print_field(struct definition
*definition
)
120 /* Print all fields in verbose mode */
121 if (babeltrace_verbose
)
124 /* Filter out part of the packet context */
125 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
127 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
129 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
131 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
133 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
140 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
144 if (opt_all_field_names
|| opt_scope_field_names
)
145 pos
->print_names
= 1;
147 pos
->print_names
= 0;
150 if (opt_all_field_names
|| opt_header_field_names
)
151 pos
->print_names
= 1;
153 pos
->print_names
= 0;
156 if (opt_all_field_names
|| opt_context_field_names
)
157 pos
->print_names
= 1;
159 pos
->print_names
= 0;
162 if (opt_all_field_names
|| opt_payload_field_names
)
163 pos
->print_names
= 1;
165 pos
->print_names
= 0;
174 const char *print_loglevel(int value
)
179 case BT_LOGLEVEL_EMERG
:
180 return "TRACE_EMERG";
181 case BT_LOGLEVEL_ALERT
:
182 return "TRACE_ALERT";
183 case BT_LOGLEVEL_CRIT
:
185 case BT_LOGLEVEL_ERR
:
187 case BT_LOGLEVEL_WARNING
:
188 return "TRACE_WARNING";
189 case BT_LOGLEVEL_NOTICE
:
190 return "TRACE_NOTICE";
191 case BT_LOGLEVEL_INFO
:
193 case BT_LOGLEVEL_DEBUG_SYSTEM
:
194 return "TRACE_DEBUG_SYSTEM";
195 case BT_LOGLEVEL_DEBUG_PROGRAM
:
196 return "TRACE_DEBUG_PROGRAM";
197 case BT_LOGLEVEL_DEBUG_PROCESS
:
198 return "TRACE_DEBUG_PROCESS";
199 case BT_LOGLEVEL_DEBUG_MODULE
:
200 return "TRACE_DEBUG_MODULE";
201 case BT_LOGLEVEL_DEBUG_UNIT
:
202 return "TRACE_DEBUG_UNIT";
203 case BT_LOGLEVEL_DEBUG_FUNCTION
:
204 return "TRACE_DEBUG_FUNCTION";
205 case BT_LOGLEVEL_DEBUG_LINE
:
206 return "TRACE_DEBUG_LINE";
207 case BT_LOGLEVEL_DEBUG
:
208 return "TRACE_DEBUG";
210 return "<<UNKNOWN>>";
215 int ctf_text_write_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
218 struct ctf_text_stream_pos
*pos
=
219 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
220 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
222 struct ctf_event_declaration
*event_class
;
223 struct ctf_event_definition
*event
;
228 id
= stream
->event_id
;
230 if (id
>= stream_class
->events_by_id
->len
) {
231 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
234 event
= g_ptr_array_index(stream
->events_by_id
, id
);
236 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
239 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
241 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
245 /* Print events discarded */
246 if (stream
->events_discarded
) {
248 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events between [",
249 stream
->events_discarded
);
250 if (opt_clock_cycles
) {
251 ctf_print_timestamp(stderr
, stream
,
252 stream
->prev_cycles_timestamp
);
253 fprintf(stderr
, "] and [");
254 ctf_print_timestamp(stderr
, stream
,
255 stream
->prev_cycles_timestamp_end
);
257 ctf_print_timestamp(stderr
, stream
,
258 stream
->prev_real_timestamp
);
259 fprintf(stderr
, "] and [");
260 ctf_print_timestamp(stderr
, stream
,
261 stream
->prev_real_timestamp_end
);
263 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
265 stream
->events_discarded
= 0;
268 if (stream
->has_timestamp
) {
269 set_field_names_print(pos
, ITEM_HEADER
);
270 if (pos
->print_names
)
271 fprintf(pos
->fp
, "timestamp = ");
273 fprintf(pos
->fp
, "[");
274 if (opt_clock_cycles
) {
275 ctf_print_timestamp(pos
->fp
, stream
, stream
->cycles_timestamp
);
277 ctf_print_timestamp(pos
->fp
, stream
, stream
->real_timestamp
);
279 if (!pos
->print_names
)
280 fprintf(pos
->fp
, "]");
282 if (pos
->print_names
)
283 fprintf(pos
->fp
, ", ");
285 fprintf(pos
->fp
, " ");
287 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
288 uint64_t delta
, delta_sec
, delta_nsec
;
290 set_field_names_print(pos
, ITEM_HEADER
);
291 if (pos
->print_names
)
292 fprintf(pos
->fp
, "delta = ");
294 fprintf(pos
->fp
, "(");
295 if (pos
->last_real_timestamp
!= -1ULL) {
296 delta
= stream
->real_timestamp
- pos
->last_real_timestamp
;
297 delta_sec
= delta
/ NSEC_PER_SEC
;
298 delta_nsec
= delta
% NSEC_PER_SEC
;
299 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
300 delta_sec
, delta_nsec
);
302 fprintf(pos
->fp
, "+?.?????????");
304 if (!pos
->print_names
)
305 fprintf(pos
->fp
, ")");
307 if (pos
->print_names
)
308 fprintf(pos
->fp
, ", ");
310 fprintf(pos
->fp
, " ");
311 pos
->last_real_timestamp
= stream
->real_timestamp
;
312 pos
->last_cycles_timestamp
= stream
->cycles_timestamp
;
315 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
316 set_field_names_print(pos
, ITEM_HEADER
);
317 if (pos
->print_names
) {
318 fprintf(pos
->fp
, "trace = ");
320 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
321 if (pos
->print_names
)
322 fprintf(pos
->fp
, ", ");
324 fprintf(pos
->fp
, " ");
326 if ((opt_trace_hostname_field
&& !opt_all_fields
) && stream_class
->trace
->env
.hostname
[0] != '\0') {
327 set_field_names_print(pos
, ITEM_HEADER
);
328 if (pos
->print_names
) {
329 fprintf(pos
->fp
, "trace:hostname = ");
331 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.hostname
);
332 if (pos
->print_names
)
333 fprintf(pos
->fp
, ", ");
336 if ((opt_trace_domain_field
&& !opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
337 set_field_names_print(pos
, ITEM_HEADER
);
338 if (pos
->print_names
) {
339 fprintf(pos
->fp
, "trace:domain = ");
341 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
342 if (pos
->print_names
)
343 fprintf(pos
->fp
, ", ");
346 if ((opt_trace_procname_field
&& !opt_all_fields
) && stream_class
->trace
->env
.procname
[0] != '\0') {
347 set_field_names_print(pos
, ITEM_HEADER
);
348 if (pos
->print_names
) {
349 fprintf(pos
->fp
, "trace:procname = ");
350 } else if (dom_print
) {
351 fprintf(pos
->fp
, ":");
353 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
354 if (pos
->print_names
)
355 fprintf(pos
->fp
, ", ");
358 if ((opt_trace_vpid_field
&& !opt_all_fields
) && 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 (dom_print
&& !pos
->print_names
)
385 fprintf(pos
->fp
, " ");
386 set_field_names_print(pos
, ITEM_HEADER
);
387 if (pos
->print_names
)
388 fprintf(pos
->fp
, "name = ");
389 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
390 if (pos
->print_names
)
393 fprintf(pos
->fp
, ":");
395 /* print cpuid field from packet context */
396 if (stream
->stream_packet_context
) {
397 if (pos
->field_nr
++ != 0)
398 fprintf(pos
->fp
, ",");
399 set_field_names_print(pos
, ITEM_SCOPE
);
400 if (pos
->print_names
)
401 fprintf(pos
->fp
, " stream.packet.context =");
402 field_nr_saved
= pos
->field_nr
;
404 set_field_names_print(pos
, ITEM_CONTEXT
);
405 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
408 pos
->field_nr
= field_nr_saved
;
411 /* Only show the event header in verbose mode */
412 if (babeltrace_verbose
&& stream
->stream_event_header
) {
413 if (pos
->field_nr
++ != 0)
414 fprintf(pos
->fp
, ",");
415 set_field_names_print(pos
, ITEM_SCOPE
);
416 if (pos
->print_names
)
417 fprintf(pos
->fp
, " stream.event.header =");
418 field_nr_saved
= pos
->field_nr
;
420 set_field_names_print(pos
, ITEM_CONTEXT
);
421 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
424 pos
->field_nr
= field_nr_saved
;
427 /* print stream-declared event context */
428 if (stream
->stream_event_context
) {
429 if (pos
->field_nr
++ != 0)
430 fprintf(pos
->fp
, ",");
431 set_field_names_print(pos
, ITEM_SCOPE
);
432 if (pos
->print_names
)
433 fprintf(pos
->fp
, " stream.event.context =");
434 field_nr_saved
= pos
->field_nr
;
436 set_field_names_print(pos
, ITEM_CONTEXT
);
437 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
440 pos
->field_nr
= field_nr_saved
;
443 /* print event-declared event context */
444 if (event
->event_context
) {
445 if (pos
->field_nr
++ != 0)
446 fprintf(pos
->fp
, ",");
447 set_field_names_print(pos
, ITEM_SCOPE
);
448 if (pos
->print_names
)
449 fprintf(pos
->fp
, " event.context =");
450 field_nr_saved
= pos
->field_nr
;
452 set_field_names_print(pos
, ITEM_CONTEXT
);
453 ret
= generic_rw(ppos
, &event
->event_context
->p
);
456 pos
->field_nr
= field_nr_saved
;
459 /* Read and print event payload */
460 if (event
->event_fields
) {
461 if (pos
->field_nr
++ != 0)
462 fprintf(pos
->fp
, ",");
463 set_field_names_print(pos
, ITEM_SCOPE
);
464 if (pos
->print_names
)
465 fprintf(pos
->fp
, " event.fields =");
466 field_nr_saved
= pos
->field_nr
;
468 set_field_names_print(pos
, ITEM_PAYLOAD
);
469 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
472 pos
->field_nr
= field_nr_saved
;
475 fprintf(pos
->fp
, "\n");
481 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
486 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
487 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
488 int whence
), FILE *metadata_fp
)
490 struct ctf_text_stream_pos
*pos
;
493 pos
= g_new0(struct ctf_text_stream_pos
, 1);
495 pos
->last_real_timestamp
= -1ULL;
496 pos
->last_cycles_timestamp
= -1ULL;
497 switch (flags
& O_ACCMODE
) {
502 fp
= fopen(path
, "w");
506 pos
->parent
.rw_table
= write_dispatch_table
;
507 pos
->parent
.event_cb
= ctf_text_write_event
;
508 pos
->print_names
= 0;
512 fprintf(stderr
, "[error] Incorrect open flags.\n");
516 return &pos
->trace_descriptor
;
522 void ctf_text_close_trace(struct trace_descriptor
*td
)
524 struct ctf_text_stream_pos
*pos
=
525 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
530 void __attribute__((constructor
)) ctf_text_init(void)
534 ctf_text_format
.name
= g_quark_from_static_string("text");
535 ret
= bt_register_format(&ctf_text_format
);