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
,
60 BT_LOGLEVEL_EMERG
= 0,
61 BT_LOGLEVEL_ALERT
= 1,
64 BT_LOGLEVEL_WARNING
= 4,
65 BT_LOGLEVEL_NOTICE
= 5,
67 BT_LOGLEVEL_DEBUG_SYSTEM
= 7,
68 BT_LOGLEVEL_DEBUG_PROGRAM
= 8,
69 BT_LOGLEVEL_DEBUG_PROCESS
= 9,
70 BT_LOGLEVEL_DEBUG_MODULE
= 10,
71 BT_LOGLEVEL_DEBUG_UNIT
= 11,
72 BT_LOGLEVEL_DEBUG_FUNCTION
= 12,
73 BT_LOGLEVEL_DEBUG_LINE
= 13,
74 BT_LOGLEVEL_DEBUG
= 14,
78 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
79 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
80 int whence
), FILE *metadata_fp
);
81 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
84 rw_dispatch write_dispatch_table
[] = {
85 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
86 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
87 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
88 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
89 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
90 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
91 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
92 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
96 struct format ctf_text_format
= {
97 .open_trace
= ctf_text_open_trace
,
98 .close_trace
= ctf_text_close_trace
,
101 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
102 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
103 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
104 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
105 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
108 void __attribute__((constructor
)) init_quarks(void)
110 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
111 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
112 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
113 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
114 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
117 int print_field(struct definition
*definition
)
119 /* Print all fields in verbose mode */
120 if (babeltrace_verbose
)
123 /* Filter out part of the packet context */
124 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
126 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
128 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
130 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
132 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
139 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
143 if (opt_all_field_names
|| opt_scope_field_names
)
144 pos
->print_names
= 1;
146 pos
->print_names
= 0;
149 if (opt_all_field_names
|| opt_header_field_names
)
150 pos
->print_names
= 1;
152 pos
->print_names
= 0;
155 if (opt_all_field_names
|| opt_context_field_names
)
156 pos
->print_names
= 1;
158 pos
->print_names
= 0;
161 if (opt_all_field_names
|| opt_payload_field_names
)
162 pos
->print_names
= 1;
164 pos
->print_names
= 0;
173 const char *print_loglevel(int value
)
178 case BT_LOGLEVEL_EMERG
:
179 return "TRACE_EMERG";
180 case BT_LOGLEVEL_ALERT
:
181 return "TRACE_ALERT";
182 case BT_LOGLEVEL_CRIT
:
184 case BT_LOGLEVEL_ERR
:
186 case BT_LOGLEVEL_WARNING
:
187 return "TRACE_WARNING";
188 case BT_LOGLEVEL_NOTICE
:
189 return "TRACE_NOTICE";
190 case BT_LOGLEVEL_INFO
:
192 case BT_LOGLEVEL_DEBUG_SYSTEM
:
193 return "TRACE_DEBUG_SYSTEM";
194 case BT_LOGLEVEL_DEBUG_PROGRAM
:
195 return "TRACE_DEBUG_PROGRAM";
196 case BT_LOGLEVEL_DEBUG_PROCESS
:
197 return "TRACE_DEBUG_PROCESS";
198 case BT_LOGLEVEL_DEBUG_MODULE
:
199 return "TRACE_DEBUG_MODULE";
200 case BT_LOGLEVEL_DEBUG_UNIT
:
201 return "TRACE_DEBUG_UNIT";
202 case BT_LOGLEVEL_DEBUG_FUNCTION
:
203 return "TRACE_DEBUG_FUNCTION";
204 case BT_LOGLEVEL_DEBUG_LINE
:
205 return "TRACE_DEBUG_LINE";
206 case BT_LOGLEVEL_DEBUG
:
207 return "TRACE_DEBUG";
209 return "<<UNKNOWN>>";
214 int ctf_text_write_event(struct stream_pos
*ppos
, struct ctf_stream_definition
*stream
)
217 struct ctf_text_stream_pos
*pos
=
218 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
219 struct ctf_stream_declaration
*stream_class
= stream
->stream_class
;
221 struct ctf_event_declaration
*event_class
;
222 struct ctf_event_definition
*event
;
227 id
= stream
->event_id
;
229 if (id
>= stream_class
->events_by_id
->len
) {
230 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
233 event
= g_ptr_array_index(stream
->events_by_id
, id
);
235 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
238 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
240 fprintf(stderr
, "[error] Event class id %" PRIu64
" is unknown.\n", id
);
244 /* Print events discarded */
245 if (stream
->events_discarded
) {
247 fprintf(stderr
, "[warning] Tracer discarded %" PRIu64
" events between [",
248 stream
->events_discarded
);
249 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp
);
250 fprintf(stderr
, "] and [");
251 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp_end
);
252 fprintf(stderr
, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
254 stream
->events_discarded
= 0;
257 if (stream
->has_timestamp
) {
258 set_field_names_print(pos
, ITEM_HEADER
);
259 if (pos
->print_names
)
260 fprintf(pos
->fp
, "timestamp = ");
262 fprintf(pos
->fp
, "[");
263 ctf_print_timestamp(pos
->fp
, stream
, stream
->timestamp
);
264 if (!pos
->print_names
)
265 fprintf(pos
->fp
, "]");
267 if (pos
->print_names
)
268 fprintf(pos
->fp
, ", ");
270 fprintf(pos
->fp
, " ");
272 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
273 uint64_t delta
, delta_sec
, delta_nsec
;
275 set_field_names_print(pos
, ITEM_HEADER
);
276 if (pos
->print_names
)
277 fprintf(pos
->fp
, "delta = ");
279 fprintf(pos
->fp
, "(");
280 if (pos
->last_timestamp
!= -1ULL) {
281 delta
= stream
->timestamp
- pos
->last_timestamp
;
282 delta_sec
= delta
/ NSEC_PER_SEC
;
283 delta_nsec
= delta
% NSEC_PER_SEC
;
284 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
285 delta_sec
, delta_nsec
);
287 fprintf(pos
->fp
, "+?.?????????");
289 if (!pos
->print_names
)
290 fprintf(pos
->fp
, ")");
292 if (pos
->print_names
)
293 fprintf(pos
->fp
, ", ");
295 fprintf(pos
->fp
, " ");
296 pos
->last_timestamp
= stream
->timestamp
;
299 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
300 set_field_names_print(pos
, ITEM_HEADER
);
301 if (pos
->print_names
) {
302 fprintf(pos
->fp
, "trace = ");
304 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
305 if (pos
->print_names
)
306 fprintf(pos
->fp
, ", ");
308 fprintf(pos
->fp
, " ");
310 if ((opt_trace_domain_field
&& !opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
311 set_field_names_print(pos
, ITEM_HEADER
);
312 if (pos
->print_names
) {
313 fprintf(pos
->fp
, "trace:domain = ");
315 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
316 if (pos
->print_names
)
317 fprintf(pos
->fp
, ", ");
320 if ((opt_trace_procname_field
&& !opt_all_fields
) && stream_class
->trace
->env
.procname
[0] != '\0') {
321 set_field_names_print(pos
, ITEM_HEADER
);
322 if (pos
->print_names
) {
323 fprintf(pos
->fp
, "trace:procname = ");
324 } else if (dom_print
) {
325 fprintf(pos
->fp
, ":");
327 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
328 if (pos
->print_names
)
329 fprintf(pos
->fp
, ", ");
332 if ((opt_trace_vpid_field
&& !opt_all_fields
) && stream_class
->trace
->env
.vpid
!= -1) {
333 set_field_names_print(pos
, ITEM_HEADER
);
334 if (pos
->print_names
) {
335 fprintf(pos
->fp
, "trace:vpid = ");
336 } else if (dom_print
) {
337 fprintf(pos
->fp
, ":");
339 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
340 if (pos
->print_names
)
341 fprintf(pos
->fp
, ", ");
344 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
345 set_field_names_print(pos
, ITEM_HEADER
);
346 if (pos
->print_names
) {
347 fprintf(pos
->fp
, "loglevel = ");
348 } else if (dom_print
) {
349 fprintf(pos
->fp
, ":");
351 fprintf(pos
->fp
, "%s (%d)",
352 print_loglevel(event_class
->loglevel
),
353 event_class
->loglevel
);
354 if (pos
->print_names
)
355 fprintf(pos
->fp
, ", ");
358 if (dom_print
&& !pos
->print_names
)
359 fprintf(pos
->fp
, " ");
360 set_field_names_print(pos
, ITEM_HEADER
);
361 if (pos
->print_names
)
362 fprintf(pos
->fp
, "name = ");
363 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
364 if (pos
->print_names
)
367 fprintf(pos
->fp
, ":");
369 /* print cpuid field from packet context */
370 if (stream
->stream_packet_context
) {
371 if (pos
->field_nr
++ != 0)
372 fprintf(pos
->fp
, ",");
373 set_field_names_print(pos
, ITEM_SCOPE
);
374 if (pos
->print_names
)
375 fprintf(pos
->fp
, " stream.packet.context =");
376 field_nr_saved
= pos
->field_nr
;
378 set_field_names_print(pos
, ITEM_CONTEXT
);
379 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
382 pos
->field_nr
= field_nr_saved
;
385 /* Only show the event header in verbose mode */
386 if (babeltrace_verbose
&& stream
->stream_event_header
) {
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.event.header =");
392 field_nr_saved
= pos
->field_nr
;
394 set_field_names_print(pos
, ITEM_CONTEXT
);
395 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
398 pos
->field_nr
= field_nr_saved
;
401 /* print stream-declared event context */
402 if (stream
->stream_event_context
) {
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.context =");
408 field_nr_saved
= pos
->field_nr
;
410 set_field_names_print(pos
, ITEM_CONTEXT
);
411 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
414 pos
->field_nr
= field_nr_saved
;
417 /* print event-declared event context */
418 if (event
->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
, " event.context =");
424 field_nr_saved
= pos
->field_nr
;
426 set_field_names_print(pos
, ITEM_CONTEXT
);
427 ret
= generic_rw(ppos
, &event
->event_context
->p
);
430 pos
->field_nr
= field_nr_saved
;
433 /* Read and print event payload */
434 if (event
->event_fields
) {
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.fields =");
440 field_nr_saved
= pos
->field_nr
;
442 set_field_names_print(pos
, ITEM_PAYLOAD
);
443 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
446 pos
->field_nr
= field_nr_saved
;
449 fprintf(pos
->fp
, "\n");
451 stream
->consumed
= 1;
456 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
461 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
462 void (*packet_seek
)(struct stream_pos
*pos
, size_t index
,
463 int whence
), FILE *metadata_fp
)
465 struct ctf_text_stream_pos
*pos
;
468 pos
= g_new0(struct ctf_text_stream_pos
, 1);
470 pos
->last_timestamp
= -1ULL;
471 switch (flags
& O_ACCMODE
) {
476 fp
= fopen(path
, "w");
480 pos
->parent
.rw_table
= write_dispatch_table
;
481 pos
->parent
.event_cb
= ctf_text_write_event
;
482 pos
->print_names
= 0;
486 fprintf(stderr
, "[error] Incorrect open flags.\n");
490 return &pos
->trace_descriptor
;
496 void ctf_text_close_trace(struct trace_descriptor
*td
)
498 struct ctf_text_stream_pos
*pos
=
499 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
504 void __attribute__((constructor
)) ctf_text_init(void)
508 ctf_text_format
.name
= g_quark_from_static_string("text");
509 ret
= bt_register_format(&ctf_text_format
);