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
,
59 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
60 const char *path
, int flags
,
61 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
62 int whence
), FILE *metadata_fp
);
63 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
66 rw_dispatch write_dispatch_table
[] = {
67 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
68 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
69 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
70 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
71 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
72 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
73 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
74 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
78 struct format ctf_text_format
= {
79 .open_trace
= ctf_text_open_trace
,
80 .close_trace
= ctf_text_close_trace
,
83 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
84 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
85 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
86 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
87 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
90 void __attribute__((constructor
)) init_quarks(void)
92 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
93 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
94 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
95 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
96 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
99 int print_field(struct definition
*definition
)
101 /* Print all fields in verbose mode */
102 if (babeltrace_verbose
)
105 /* Filter out part of the packet context */
106 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
108 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
110 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
112 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
114 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
121 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
125 if (opt_all_field_names
|| opt_scope_field_names
)
126 pos
->print_names
= 1;
128 pos
->print_names
= 0;
131 if (opt_all_field_names
|| opt_header_field_names
)
132 pos
->print_names
= 1;
134 pos
->print_names
= 0;
137 if (opt_all_field_names
|| opt_context_field_names
)
138 pos
->print_names
= 1;
140 pos
->print_names
= 0;
143 if (opt_all_field_names
|| opt_payload_field_names
)
144 pos
->print_names
= 1;
146 pos
->print_names
= 0;
155 int ctf_text_write_event(struct stream_pos
*ppos
,
156 struct ctf_stream
*stream
)
158 struct ctf_text_stream_pos
*pos
=
159 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
160 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
162 struct ctf_event
*event_class
;
163 struct ctf_stream_event
*event
;
168 id
= stream
->event_id
;
170 if (id
>= stream_class
->events_by_id
->len
) {
171 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
174 event
= g_ptr_array_index(stream
->events_by_id
, id
);
176 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
179 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
181 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
185 /* Print events discarded */
186 if (stream
->events_discarded
) {
188 fprintf(stderr
, "[warning] Tracer discarded %d events between [",
189 stream
->events_discarded
);
190 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp
);
191 fprintf(stderr
, "] and [");
192 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp_end
);
193 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
195 stream
->events_discarded
= 0;
198 if (stream
->has_timestamp
) {
199 set_field_names_print(pos
, ITEM_HEADER
);
200 if (pos
->print_names
)
201 fprintf(pos
->fp
, "timestamp = ");
203 fprintf(pos
->fp
, "[");
204 ctf_print_timestamp(pos
->fp
, stream
, stream
->timestamp
);
205 if (!pos
->print_names
)
206 fprintf(pos
->fp
, "]");
208 if (pos
->print_names
)
209 fprintf(pos
->fp
, ", ");
211 fprintf(pos
->fp
, " ");
213 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
214 uint64_t delta
, delta_sec
, delta_nsec
;
216 set_field_names_print(pos
, ITEM_HEADER
);
217 if (pos
->print_names
)
218 fprintf(pos
->fp
, "delta = ");
220 fprintf(pos
->fp
, "(");
221 if (pos
->last_timestamp
!= -1ULL) {
222 delta
= stream
->timestamp
- pos
->last_timestamp
;
223 delta_sec
= delta
/ NSEC_PER_SEC
;
224 delta_nsec
= delta
% NSEC_PER_SEC
;
225 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
226 delta_sec
, delta_nsec
);
228 fprintf(pos
->fp
, "+?.?????????");
230 if (!pos
->print_names
)
231 fprintf(pos
->fp
, ")");
233 if (pos
->print_names
)
234 fprintf(pos
->fp
, ", ");
236 fprintf(pos
->fp
, " ");
237 pos
->last_timestamp
= stream
->timestamp
;
240 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
241 set_field_names_print(pos
, ITEM_HEADER
);
242 if (pos
->print_names
) {
243 fprintf(pos
->fp
, "trace = ");
245 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
246 if (pos
->print_names
)
247 fprintf(pos
->fp
, ", ");
249 fprintf(pos
->fp
, " ");
251 if ((opt_trace_domain_field
&& !opt_all_fields
) && stream_class
->trace
->domain
[0] != '\0') {
252 set_field_names_print(pos
, ITEM_HEADER
);
253 if (pos
->print_names
) {
254 fprintf(pos
->fp
, "trace:domain = ");
256 fprintf(pos
->fp
, "%s", stream_class
->trace
->domain
);
257 if (pos
->print_names
)
258 fprintf(pos
->fp
, ", ");
261 if ((opt_trace_procname_field
&& !opt_all_fields
) && stream_class
->trace
->procname
[0] != '\0') {
262 set_field_names_print(pos
, ITEM_HEADER
);
263 if (pos
->print_names
) {
264 fprintf(pos
->fp
, "trace:procname = ");
265 } else if (dom_print
) {
266 fprintf(pos
->fp
, ":");
268 fprintf(pos
->fp
, "%s", stream_class
->trace
->procname
);
269 if (pos
->print_names
)
270 fprintf(pos
->fp
, ", ");
273 if ((opt_trace_vpid_field
&& !opt_all_fields
) && stream_class
->trace
->vpid
[0] != '\0') {
274 set_field_names_print(pos
, ITEM_HEADER
);
275 if (pos
->print_names
) {
276 fprintf(pos
->fp
, "trace:vpid = ");
277 } else if (dom_print
) {
278 fprintf(pos
->fp
, ":");
280 fprintf(pos
->fp
, "%s", stream_class
->trace
->vpid
);
281 if (pos
->print_names
)
282 fprintf(pos
->fp
, ", ");
285 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel_identifier
!= 0) {
286 set_field_names_print(pos
, ITEM_HEADER
);
287 if (pos
->print_names
) {
288 fprintf(pos
->fp
, "loglevel = ");
289 } else if (dom_print
) {
290 fprintf(pos
->fp
, ":");
292 fprintf(pos
->fp
, "%s (%lld)",
293 g_quark_to_string(event_class
->loglevel_identifier
),
294 (long long) event_class
->loglevel_value
);
295 if (pos
->print_names
)
296 fprintf(pos
->fp
, ", ");
299 if (dom_print
&& !pos
->print_names
)
300 fprintf(pos
->fp
, " ");
301 set_field_names_print(pos
, ITEM_HEADER
);
302 if (pos
->print_names
)
303 fprintf(pos
->fp
, "name = ");
304 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
305 if (pos
->print_names
)
308 fprintf(pos
->fp
, ":");
310 /* print cpuid field from packet context */
311 if (stream
->stream_packet_context
) {
312 if (pos
->field_nr
++ != 0)
313 fprintf(pos
->fp
, ",");
314 set_field_names_print(pos
, ITEM_SCOPE
);
315 if (pos
->print_names
)
316 fprintf(pos
->fp
, " stream.packet.context =");
317 field_nr_saved
= pos
->field_nr
;
319 set_field_names_print(pos
, ITEM_CONTEXT
);
320 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
323 pos
->field_nr
= field_nr_saved
;
326 /* Only show the event header in verbose mode */
327 if (babeltrace_verbose
&& stream
->stream_event_header
) {
328 if (pos
->field_nr
++ != 0)
329 fprintf(pos
->fp
, ",");
330 set_field_names_print(pos
, ITEM_SCOPE
);
331 if (pos
->print_names
)
332 fprintf(pos
->fp
, " stream.event.header =");
333 field_nr_saved
= pos
->field_nr
;
335 set_field_names_print(pos
, ITEM_CONTEXT
);
336 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
339 pos
->field_nr
= field_nr_saved
;
342 /* print stream-declared event context */
343 if (stream
->stream_event_context
) {
344 if (pos
->field_nr
++ != 0)
345 fprintf(pos
->fp
, ",");
346 set_field_names_print(pos
, ITEM_SCOPE
);
347 if (pos
->print_names
)
348 fprintf(pos
->fp
, " stream.event.context =");
349 field_nr_saved
= pos
->field_nr
;
351 set_field_names_print(pos
, ITEM_CONTEXT
);
352 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
355 pos
->field_nr
= field_nr_saved
;
358 /* print event-declared event context */
359 if (event
->event_context
) {
360 if (pos
->field_nr
++ != 0)
361 fprintf(pos
->fp
, ",");
362 set_field_names_print(pos
, ITEM_SCOPE
);
363 if (pos
->print_names
)
364 fprintf(pos
->fp
, " event.context =");
365 field_nr_saved
= pos
->field_nr
;
367 set_field_names_print(pos
, ITEM_CONTEXT
);
368 ret
= generic_rw(ppos
, &event
->event_context
->p
);
371 pos
->field_nr
= field_nr_saved
;
374 /* Read and print event payload */
375 if (event
->event_fields
) {
376 if (pos
->field_nr
++ != 0)
377 fprintf(pos
->fp
, ",");
378 set_field_names_print(pos
, ITEM_SCOPE
);
379 if (pos
->print_names
)
380 fprintf(pos
->fp
, " event.fields =");
381 field_nr_saved
= pos
->field_nr
;
383 set_field_names_print(pos
, ITEM_PAYLOAD
);
384 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
387 pos
->field_nr
= field_nr_saved
;
390 fprintf(pos
->fp
, "\n");
396 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
401 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
402 const char *path
, int flags
,
403 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
404 int whence
), FILE *metadata_fp
)
406 struct ctf_text_stream_pos
*pos
;
409 pos
= g_new0(struct ctf_text_stream_pos
, 1);
411 pos
->last_timestamp
= -1ULL;
412 switch (flags
& O_ACCMODE
) {
417 fp
= fopen(path
, "w");
421 pos
->parent
.rw_table
= write_dispatch_table
;
422 pos
->parent
.event_cb
= ctf_text_write_event
;
423 pos
->print_names
= 0;
427 fprintf(stderr
, "[error] Incorrect open flags.\n");
431 return &pos
->trace_descriptor
;
437 void ctf_text_close_trace(struct trace_descriptor
*td
)
439 struct ctf_text_stream_pos
*pos
=
440 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
445 void __attribute__((constructor
)) ctf_text_init(void)
449 ctf_text_format
.name
= g_quark_from_static_string("text");
450 ret
= bt_register_format(&ctf_text_format
);