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>
27 #include <uuid/uuid.h>
30 #include <sys/types.h>
38 #define NSEC_PER_SEC 1000000000ULL
40 int opt_all_field_names
,
41 opt_scope_field_names
,
42 opt_header_field_names
,
43 opt_context_field_names
,
44 opt_payload_field_names
,
47 opt_trace_domain_field
,
48 opt_trace_procname_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
*stream
)
218 struct ctf_text_stream_pos
*pos
=
219 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
220 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
222 struct ctf_event
*event_class
;
223 struct ctf_stream_event
*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 id %" PRIu64
" is unknown.\n", id
);
245 /* Print events discarded */
246 if (stream
->events_discarded
) {
248 fprintf(stderr
, "[warning] Tracer discarded %d events between [",
249 stream
->events_discarded
);
250 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp
);
251 fprintf(stderr
, "] and [");
252 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp_end
);
253 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
255 stream
->events_discarded
= 0;
258 if (stream
->has_timestamp
) {
259 set_field_names_print(pos
, ITEM_HEADER
);
260 if (pos
->print_names
)
261 fprintf(pos
->fp
, "timestamp = ");
263 fprintf(pos
->fp
, "[");
264 ctf_print_timestamp(pos
->fp
, stream
, stream
->timestamp
);
265 if (!pos
->print_names
)
266 fprintf(pos
->fp
, "]");
268 if (pos
->print_names
)
269 fprintf(pos
->fp
, ", ");
271 fprintf(pos
->fp
, " ");
273 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
274 uint64_t delta
, delta_sec
, delta_nsec
;
276 set_field_names_print(pos
, ITEM_HEADER
);
277 if (pos
->print_names
)
278 fprintf(pos
->fp
, "delta = ");
280 fprintf(pos
->fp
, "(");
281 if (pos
->last_timestamp
!= -1ULL) {
282 delta
= stream
->timestamp
- pos
->last_timestamp
;
283 delta_sec
= delta
/ NSEC_PER_SEC
;
284 delta_nsec
= delta
% NSEC_PER_SEC
;
285 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
286 delta_sec
, delta_nsec
);
288 fprintf(pos
->fp
, "+?.?????????");
290 if (!pos
->print_names
)
291 fprintf(pos
->fp
, ")");
293 if (pos
->print_names
)
294 fprintf(pos
->fp
, ", ");
296 fprintf(pos
->fp
, " ");
297 pos
->last_timestamp
= stream
->timestamp
;
300 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
301 set_field_names_print(pos
, ITEM_HEADER
);
302 if (pos
->print_names
) {
303 fprintf(pos
->fp
, "trace = ");
305 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
306 if (pos
->print_names
)
307 fprintf(pos
->fp
, ", ");
309 fprintf(pos
->fp
, " ");
311 if ((opt_trace_domain_field
&& !opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
312 set_field_names_print(pos
, ITEM_HEADER
);
313 if (pos
->print_names
) {
314 fprintf(pos
->fp
, "trace:domain = ");
316 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
317 if (pos
->print_names
)
318 fprintf(pos
->fp
, ", ");
321 if ((opt_trace_procname_field
&& !opt_all_fields
) && stream_class
->trace
->env
.procname
[0] != '\0') {
322 set_field_names_print(pos
, ITEM_HEADER
);
323 if (pos
->print_names
) {
324 fprintf(pos
->fp
, "trace:procname = ");
325 } else if (dom_print
) {
326 fprintf(pos
->fp
, ":");
328 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
329 if (pos
->print_names
)
330 fprintf(pos
->fp
, ", ");
333 if ((opt_trace_vpid_field
&& !opt_all_fields
) && stream_class
->trace
->env
.vpid
!= -1) {
334 set_field_names_print(pos
, ITEM_HEADER
);
335 if (pos
->print_names
) {
336 fprintf(pos
->fp
, "trace:vpid = ");
337 } else if (dom_print
) {
338 fprintf(pos
->fp
, ":");
340 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
341 if (pos
->print_names
)
342 fprintf(pos
->fp
, ", ");
345 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
346 set_field_names_print(pos
, ITEM_HEADER
);
347 if (pos
->print_names
) {
348 fprintf(pos
->fp
, "loglevel = ");
349 } else if (dom_print
) {
350 fprintf(pos
->fp
, ":");
352 fprintf(pos
->fp
, "%s (%d)",
353 print_loglevel(event_class
->loglevel
),
354 event_class
->loglevel
);
355 if (pos
->print_names
)
356 fprintf(pos
->fp
, ", ");
359 if (dom_print
&& !pos
->print_names
)
360 fprintf(pos
->fp
, " ");
361 set_field_names_print(pos
, ITEM_HEADER
);
362 if (pos
->print_names
)
363 fprintf(pos
->fp
, "name = ");
364 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
365 if (pos
->print_names
)
368 fprintf(pos
->fp
, ":");
370 /* print cpuid field from packet context */
371 if (stream
->stream_packet_context
) {
372 if (pos
->field_nr
++ != 0)
373 fprintf(pos
->fp
, ",");
374 set_field_names_print(pos
, ITEM_SCOPE
);
375 if (pos
->print_names
)
376 fprintf(pos
->fp
, " stream.packet.context =");
377 field_nr_saved
= pos
->field_nr
;
379 set_field_names_print(pos
, ITEM_CONTEXT
);
380 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
383 pos
->field_nr
= field_nr_saved
;
386 /* Only show the event header in verbose mode */
387 if (babeltrace_verbose
&& stream
->stream_event_header
) {
388 if (pos
->field_nr
++ != 0)
389 fprintf(pos
->fp
, ",");
390 set_field_names_print(pos
, ITEM_SCOPE
);
391 if (pos
->print_names
)
392 fprintf(pos
->fp
, " stream.event.header =");
393 field_nr_saved
= pos
->field_nr
;
395 set_field_names_print(pos
, ITEM_CONTEXT
);
396 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
399 pos
->field_nr
= field_nr_saved
;
402 /* print stream-declared event context */
403 if (stream
->stream_event_context
) {
404 if (pos
->field_nr
++ != 0)
405 fprintf(pos
->fp
, ",");
406 set_field_names_print(pos
, ITEM_SCOPE
);
407 if (pos
->print_names
)
408 fprintf(pos
->fp
, " stream.event.context =");
409 field_nr_saved
= pos
->field_nr
;
411 set_field_names_print(pos
, ITEM_CONTEXT
);
412 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
415 pos
->field_nr
= field_nr_saved
;
418 /* print event-declared event context */
419 if (event
->event_context
) {
420 if (pos
->field_nr
++ != 0)
421 fprintf(pos
->fp
, ",");
422 set_field_names_print(pos
, ITEM_SCOPE
);
423 if (pos
->print_names
)
424 fprintf(pos
->fp
, " event.context =");
425 field_nr_saved
= pos
->field_nr
;
427 set_field_names_print(pos
, ITEM_CONTEXT
);
428 ret
= generic_rw(ppos
, &event
->event_context
->p
);
431 pos
->field_nr
= field_nr_saved
;
434 /* Read and print event payload */
435 if (event
->event_fields
) {
436 if (pos
->field_nr
++ != 0)
437 fprintf(pos
->fp
, ",");
438 set_field_names_print(pos
, ITEM_SCOPE
);
439 if (pos
->print_names
)
440 fprintf(pos
->fp
, " event.fields =");
441 field_nr_saved
= pos
->field_nr
;
443 set_field_names_print(pos
, ITEM_PAYLOAD
);
444 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
447 pos
->field_nr
= field_nr_saved
;
450 fprintf(pos
->fp
, "\n");
452 stream
->consumed
= 1;
457 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
462 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
463 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
464 int whence
), FILE *metadata_fp
)
466 struct ctf_text_stream_pos
*pos
;
469 pos
= g_new0(struct ctf_text_stream_pos
, 1);
471 pos
->last_timestamp
= -1ULL;
472 switch (flags
& O_ACCMODE
) {
477 fp
= fopen(path
, "w");
481 pos
->parent
.rw_table
= write_dispatch_table
;
482 pos
->parent
.event_cb
= ctf_text_write_event
;
483 pos
->print_names
= 0;
487 fprintf(stderr
, "[error] Incorrect open flags.\n");
491 return &pos
->trace_descriptor
;
497 void ctf_text_close_trace(struct trace_descriptor
*td
)
499 struct ctf_text_stream_pos
*pos
=
500 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
505 void __attribute__((constructor
)) ctf_text_init(void)
509 ctf_text_format
.name
= g_quark_from_static_string("text");
510 ret
= bt_register_format(&ctf_text_format
);