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>
26 #include <uuid/uuid.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
,
56 uint64_t opt_clock_offset
;
65 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
66 const char *path
, int flags
,
67 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
68 int whence
), FILE *metadata_fp
);
69 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
72 rw_dispatch write_dispatch_table
[] = {
73 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
74 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
75 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
76 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
77 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
78 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
79 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
80 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
84 struct format ctf_text_format
= {
85 .open_trace
= ctf_text_open_trace
,
86 .close_trace
= ctf_text_close_trace
,
89 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
90 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
91 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
92 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
93 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
96 void __attribute__((constructor
)) init_quarks(void)
98 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
99 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
100 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
101 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
102 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
105 int print_field(struct definition
*definition
)
107 /* Print all fields in verbose mode */
108 if (babeltrace_verbose
)
111 /* Filter out part of the packet context */
112 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
114 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
116 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
118 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
120 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
127 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
131 if (opt_all_field_names
|| opt_scope_field_names
)
132 pos
->print_names
= 1;
134 pos
->print_names
= 0;
137 if (opt_all_field_names
|| opt_header_field_names
)
138 pos
->print_names
= 1;
140 pos
->print_names
= 0;
143 if (opt_all_field_names
|| opt_context_field_names
)
144 pos
->print_names
= 1;
146 pos
->print_names
= 0;
149 if (opt_all_field_names
|| opt_payload_field_names
)
150 pos
->print_names
= 1;
152 pos
->print_names
= 0;
161 void ctf_text_print_timestamp(FILE *fp
, struct ctf_text_stream_pos
*pos
,
162 struct ctf_stream
*stream
,
165 uint64_t ts_sec
= 0, ts_nsec
;
166 struct ctf_trace
*trace
= stream
->stream_class
->trace
;
167 struct trace_collection
*tc
= trace
->collection
;
168 struct ctf_clock
*clock
= tc
->single_clock
;
173 if (!opt_clock_raw
&& clock
) {
174 ts_sec
+= clock
->offset_s
;
175 ts_nsec
+= clock
->offset
;
177 ts_sec
+= opt_clock_offset
;
179 ts_sec
+= ts_nsec
/ NSEC_PER_SEC
;
180 ts_nsec
= ts_nsec
% NSEC_PER_SEC
;
182 if (!opt_clock_seconds
) {
184 time_t time_s
= (time_t) ts_sec
;
186 if (!opt_clock_gmt
) {
189 res
= localtime_r(&time_s
, &tm
);
191 fprintf(stderr
, "[warning] Unable to get localtime.\n");
197 res
= gmtime_r(&time_s
, &tm
);
199 fprintf(stderr
, "[warning] Unable to get gmtime.\n");
203 if (opt_clock_date
) {
207 /* Print date and time */
208 res
= strftime(timestr
, sizeof(timestr
),
211 fprintf(stderr
, "[warning] Unable to print ascii time.\n");
214 fprintf(fp
, "%s", timestr
);
216 /* Print time in HH:MM:SS.ns */
217 fprintf(fp
, "%02d:%02d:%02d.%09" PRIu64
,
218 tm
.tm_hour
, tm
.tm_min
, tm
.tm_sec
, ts_nsec
);
222 fprintf(fp
, "%3" PRIu64
".%09" PRIu64
,
230 int ctf_text_write_event(struct stream_pos
*ppos
,
231 struct ctf_stream
*stream
)
233 struct ctf_text_stream_pos
*pos
=
234 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
235 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
237 struct ctf_event
*event_class
;
238 struct ctf_stream_event
*event
;
243 id
= stream
->event_id
;
245 if (id
>= stream_class
->events_by_id
->len
) {
246 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
249 event
= g_ptr_array_index(stream
->events_by_id
, id
);
251 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
254 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
256 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
260 /* Print events discarded */
261 if (stream
->events_discarded
) {
263 fprintf(stderr
, "[warning] Tracer discarded %d events between [",
264 stream
->events_discarded
);
265 ctf_text_print_timestamp(stderr
, pos
, stream
, stream
->prev_timestamp
);
266 fprintf(stderr
, "] and [");
267 ctf_text_print_timestamp(stderr
, pos
, stream
, stream
->timestamp
);
268 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
270 stream
->events_discarded
= 0;
273 if (stream
->has_timestamp
) {
274 set_field_names_print(pos
, ITEM_HEADER
);
275 if (pos
->print_names
)
276 fprintf(pos
->fp
, "timestamp = ");
278 fprintf(pos
->fp
, "[");
279 ctf_text_print_timestamp(pos
->fp
, pos
, stream
, stream
->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_timestamp
!= -1ULL) {
297 delta
= stream
->timestamp
- pos
->last_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_timestamp
= stream
->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_domain_field
&& !opt_all_fields
) && stream_class
->trace
->domain
[0] != '\0') {
327 set_field_names_print(pos
, ITEM_HEADER
);
328 if (pos
->print_names
) {
329 fprintf(pos
->fp
, "trace:domain = ");
331 fprintf(pos
->fp
, "%s", stream_class
->trace
->domain
);
332 if (pos
->print_names
)
333 fprintf(pos
->fp
, ", ");
336 if ((opt_trace_procname_field
&& !opt_all_fields
) && stream_class
->trace
->procname
[0] != '\0') {
337 set_field_names_print(pos
, ITEM_HEADER
);
338 if (pos
->print_names
) {
339 fprintf(pos
->fp
, "trace:procname = ");
340 } else if (dom_print
) {
341 fprintf(pos
->fp
, ":");
343 fprintf(pos
->fp
, "%s", stream_class
->trace
->procname
);
344 if (pos
->print_names
)
345 fprintf(pos
->fp
, ", ");
348 if ((opt_trace_vpid_field
&& !opt_all_fields
) && stream_class
->trace
->vpid
[0] != '\0') {
349 set_field_names_print(pos
, ITEM_HEADER
);
350 if (pos
->print_names
) {
351 fprintf(pos
->fp
, "trace:vpid = ");
352 } else if (dom_print
) {
353 fprintf(pos
->fp
, ":");
355 fprintf(pos
->fp
, "%s", stream_class
->trace
->vpid
);
356 if (pos
->print_names
)
357 fprintf(pos
->fp
, ", ");
360 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel_identifier
!= 0) {
361 set_field_names_print(pos
, ITEM_HEADER
);
362 if (pos
->print_names
) {
363 fprintf(pos
->fp
, "loglevel = ");
364 } else if (dom_print
) {
365 fprintf(pos
->fp
, ":");
367 fprintf(pos
->fp
, "%s (%lld)",
368 g_quark_to_string(event_class
->loglevel_identifier
),
369 (long long) event_class
->loglevel_value
);
370 if (pos
->print_names
)
371 fprintf(pos
->fp
, ", ");
374 if (dom_print
&& !pos
->print_names
)
375 fprintf(pos
->fp
, " ");
376 set_field_names_print(pos
, ITEM_HEADER
);
377 if (pos
->print_names
)
378 fprintf(pos
->fp
, "name = ");
379 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
380 if (pos
->print_names
)
383 fprintf(pos
->fp
, ":");
385 /* print cpuid field from packet context */
386 if (stream
->stream_packet_context
) {
387 if (pos
->field_nr
++ != 0)
388 fprintf(pos
->fp
, ",");
389 set_field_names_print(pos
, ITEM_SCOPE
);
390 if (pos
->print_names
)
391 fprintf(pos
->fp
, " stream.packet.context =");
392 field_nr_saved
= pos
->field_nr
;
394 set_field_names_print(pos
, ITEM_CONTEXT
);
395 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
398 pos
->field_nr
= field_nr_saved
;
401 /* Only show the event header in verbose mode */
402 if (babeltrace_verbose
&& stream
->stream_event_header
) {
403 if (pos
->field_nr
++ != 0)
404 fprintf(pos
->fp
, ",");
405 set_field_names_print(pos
, ITEM_SCOPE
);
406 if (pos
->print_names
)
407 fprintf(pos
->fp
, " stream.event.header =");
408 field_nr_saved
= pos
->field_nr
;
410 set_field_names_print(pos
, ITEM_CONTEXT
);
411 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
414 pos
->field_nr
= field_nr_saved
;
417 /* print stream-declared event context */
418 if (stream
->stream_event_context
) {
419 if (pos
->field_nr
++ != 0)
420 fprintf(pos
->fp
, ",");
421 set_field_names_print(pos
, ITEM_SCOPE
);
422 if (pos
->print_names
)
423 fprintf(pos
->fp
, " stream.event.context =");
424 field_nr_saved
= pos
->field_nr
;
426 set_field_names_print(pos
, ITEM_CONTEXT
);
427 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
430 pos
->field_nr
= field_nr_saved
;
433 /* print event-declared event context */
434 if (event
->event_context
) {
435 if (pos
->field_nr
++ != 0)
436 fprintf(pos
->fp
, ",");
437 set_field_names_print(pos
, ITEM_SCOPE
);
438 if (pos
->print_names
)
439 fprintf(pos
->fp
, " event.context =");
440 field_nr_saved
= pos
->field_nr
;
442 set_field_names_print(pos
, ITEM_CONTEXT
);
443 ret
= generic_rw(ppos
, &event
->event_context
->p
);
446 pos
->field_nr
= field_nr_saved
;
449 /* Read and print event payload */
450 if (event
->event_fields
) {
451 if (pos
->field_nr
++ != 0)
452 fprintf(pos
->fp
, ",");
453 set_field_names_print(pos
, ITEM_SCOPE
);
454 if (pos
->print_names
)
455 fprintf(pos
->fp
, " event.fields =");
456 field_nr_saved
= pos
->field_nr
;
458 set_field_names_print(pos
, ITEM_PAYLOAD
);
459 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
462 pos
->field_nr
= field_nr_saved
;
465 fprintf(pos
->fp
, "\n");
471 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
476 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
477 const char *path
, int flags
,
478 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
479 int whence
), FILE *metadata_fp
)
481 struct ctf_text_stream_pos
*pos
;
484 pos
= g_new0(struct ctf_text_stream_pos
, 1);
486 pos
->last_timestamp
= -1ULL;
487 switch (flags
& O_ACCMODE
) {
492 fp
= fopen(path
, "w");
496 pos
->parent
.rw_table
= write_dispatch_table
;
497 pos
->parent
.event_cb
= ctf_text_write_event
;
498 pos
->print_names
= 0;
502 fprintf(stderr
, "[error] Incorrect open flags.\n");
506 return &pos
->trace_descriptor
;
512 void ctf_text_close_trace(struct trace_descriptor
*td
)
514 struct ctf_text_stream_pos
*pos
=
515 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
520 void __attribute__((constructor
)) ctf_text_init(void)
524 ctf_text_format
.name
= g_quark_from_static_string("text");
525 ret
= bt_register_format(&ctf_text_format
);