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 *path
, int flags
,
60 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
61 int whence
), FILE *metadata_fp
);
62 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
65 rw_dispatch write_dispatch_table
[] = {
66 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
67 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
68 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
69 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
70 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
71 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
72 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
73 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
77 struct format ctf_text_format
= {
78 .open_trace
= ctf_text_open_trace
,
79 .close_trace
= ctf_text_close_trace
,
82 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
83 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
84 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
85 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
86 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
89 void __attribute__((constructor
)) init_quarks(void)
91 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
92 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
93 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
94 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
95 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
98 int print_field(struct definition
*definition
)
100 /* Print all fields in verbose mode */
101 if (babeltrace_verbose
)
104 /* Filter out part of the packet context */
105 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
107 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
109 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
111 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
113 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
120 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
124 if (opt_all_field_names
|| opt_scope_field_names
)
125 pos
->print_names
= 1;
127 pos
->print_names
= 0;
130 if (opt_all_field_names
|| opt_header_field_names
)
131 pos
->print_names
= 1;
133 pos
->print_names
= 0;
136 if (opt_all_field_names
|| opt_context_field_names
)
137 pos
->print_names
= 1;
139 pos
->print_names
= 0;
142 if (opt_all_field_names
|| opt_payload_field_names
)
143 pos
->print_names
= 1;
145 pos
->print_names
= 0;
154 int ctf_text_write_event(struct stream_pos
*ppos
,
155 struct ctf_stream
*stream
)
157 struct ctf_text_stream_pos
*pos
=
158 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
159 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
161 struct ctf_event
*event_class
;
162 struct ctf_stream_event
*event
;
167 id
= stream
->event_id
;
169 if (id
>= stream_class
->events_by_id
->len
) {
170 fprintf(stderr
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
173 event
= g_ptr_array_index(stream
->events_by_id
, id
);
175 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
178 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
180 fprintf(stderr
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
184 /* Print events discarded */
185 if (stream
->events_discarded
) {
187 fprintf(stderr
, "[warning] Tracer discarded %d events between [",
188 stream
->events_discarded
);
189 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp
);
190 fprintf(stderr
, "] and [");
191 ctf_print_timestamp(stderr
, stream
, stream
->prev_timestamp_end
);
192 fprintf(stderr
, "]. You should consider increasing the buffer size.\n");
194 stream
->events_discarded
= 0;
197 if (stream
->has_timestamp
) {
198 set_field_names_print(pos
, ITEM_HEADER
);
199 if (pos
->print_names
)
200 fprintf(pos
->fp
, "timestamp = ");
202 fprintf(pos
->fp
, "[");
203 ctf_print_timestamp(pos
->fp
, stream
, stream
->timestamp
);
204 if (!pos
->print_names
)
205 fprintf(pos
->fp
, "]");
207 if (pos
->print_names
)
208 fprintf(pos
->fp
, ", ");
210 fprintf(pos
->fp
, " ");
212 if ((opt_delta_field
|| opt_all_fields
) && stream
->has_timestamp
) {
213 uint64_t delta
, delta_sec
, delta_nsec
;
215 set_field_names_print(pos
, ITEM_HEADER
);
216 if (pos
->print_names
)
217 fprintf(pos
->fp
, "delta = ");
219 fprintf(pos
->fp
, "(");
220 if (pos
->last_timestamp
!= -1ULL) {
221 delta
= stream
->timestamp
- pos
->last_timestamp
;
222 delta_sec
= delta
/ NSEC_PER_SEC
;
223 delta_nsec
= delta
% NSEC_PER_SEC
;
224 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
225 delta_sec
, delta_nsec
);
227 fprintf(pos
->fp
, "+?.?????????");
229 if (!pos
->print_names
)
230 fprintf(pos
->fp
, ")");
232 if (pos
->print_names
)
233 fprintf(pos
->fp
, ", ");
235 fprintf(pos
->fp
, " ");
236 pos
->last_timestamp
= stream
->timestamp
;
239 if ((opt_trace_field
|| opt_all_fields
) && stream_class
->trace
->path
[0] != '\0') {
240 set_field_names_print(pos
, ITEM_HEADER
);
241 if (pos
->print_names
) {
242 fprintf(pos
->fp
, "trace = ");
244 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
245 if (pos
->print_names
)
246 fprintf(pos
->fp
, ", ");
248 fprintf(pos
->fp
, " ");
250 if ((opt_trace_domain_field
&& !opt_all_fields
) && stream_class
->trace
->env
.domain
[0] != '\0') {
251 set_field_names_print(pos
, ITEM_HEADER
);
252 if (pos
->print_names
) {
253 fprintf(pos
->fp
, "trace:domain = ");
255 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.domain
);
256 if (pos
->print_names
)
257 fprintf(pos
->fp
, ", ");
260 if ((opt_trace_procname_field
&& !opt_all_fields
) && stream_class
->trace
->env
.procname
[0] != '\0') {
261 set_field_names_print(pos
, ITEM_HEADER
);
262 if (pos
->print_names
) {
263 fprintf(pos
->fp
, "trace:procname = ");
264 } else if (dom_print
) {
265 fprintf(pos
->fp
, ":");
267 fprintf(pos
->fp
, "%s", stream_class
->trace
->env
.procname
);
268 if (pos
->print_names
)
269 fprintf(pos
->fp
, ", ");
272 if ((opt_trace_vpid_field
&& !opt_all_fields
) && stream_class
->trace
->env
.vpid
!= -1) {
273 set_field_names_print(pos
, ITEM_HEADER
);
274 if (pos
->print_names
) {
275 fprintf(pos
->fp
, "trace:vpid = ");
276 } else if (dom_print
) {
277 fprintf(pos
->fp
, ":");
279 fprintf(pos
->fp
, "%d", stream_class
->trace
->env
.vpid
);
280 if (pos
->print_names
)
281 fprintf(pos
->fp
, ", ");
284 if ((opt_loglevel_field
|| opt_all_fields
) && event_class
->loglevel
!= -1) {
285 set_field_names_print(pos
, ITEM_HEADER
);
286 if (pos
->print_names
) {
287 fprintf(pos
->fp
, "loglevel = ");
288 } else if (dom_print
) {
289 fprintf(pos
->fp
, ":");
291 fprintf(pos
->fp
, "(%d)",
292 event_class
->loglevel
);
293 if (pos
->print_names
)
294 fprintf(pos
->fp
, ", ");
297 if (dom_print
&& !pos
->print_names
)
298 fprintf(pos
->fp
, " ");
299 set_field_names_print(pos
, ITEM_HEADER
);
300 if (pos
->print_names
)
301 fprintf(pos
->fp
, "name = ");
302 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
303 if (pos
->print_names
)
306 fprintf(pos
->fp
, ":");
308 /* print cpuid field from packet context */
309 if (stream
->stream_packet_context
) {
310 if (pos
->field_nr
++ != 0)
311 fprintf(pos
->fp
, ",");
312 set_field_names_print(pos
, ITEM_SCOPE
);
313 if (pos
->print_names
)
314 fprintf(pos
->fp
, " stream.packet.context =");
315 field_nr_saved
= pos
->field_nr
;
317 set_field_names_print(pos
, ITEM_CONTEXT
);
318 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
321 pos
->field_nr
= field_nr_saved
;
324 /* Only show the event header in verbose mode */
325 if (babeltrace_verbose
&& stream
->stream_event_header
) {
326 if (pos
->field_nr
++ != 0)
327 fprintf(pos
->fp
, ",");
328 set_field_names_print(pos
, ITEM_SCOPE
);
329 if (pos
->print_names
)
330 fprintf(pos
->fp
, " stream.event.header =");
331 field_nr_saved
= pos
->field_nr
;
333 set_field_names_print(pos
, ITEM_CONTEXT
);
334 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
337 pos
->field_nr
= field_nr_saved
;
340 /* print stream-declared event context */
341 if (stream
->stream_event_context
) {
342 if (pos
->field_nr
++ != 0)
343 fprintf(pos
->fp
, ",");
344 set_field_names_print(pos
, ITEM_SCOPE
);
345 if (pos
->print_names
)
346 fprintf(pos
->fp
, " stream.event.context =");
347 field_nr_saved
= pos
->field_nr
;
349 set_field_names_print(pos
, ITEM_CONTEXT
);
350 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
353 pos
->field_nr
= field_nr_saved
;
356 /* print event-declared event context */
357 if (event
->event_context
) {
358 if (pos
->field_nr
++ != 0)
359 fprintf(pos
->fp
, ",");
360 set_field_names_print(pos
, ITEM_SCOPE
);
361 if (pos
->print_names
)
362 fprintf(pos
->fp
, " event.context =");
363 field_nr_saved
= pos
->field_nr
;
365 set_field_names_print(pos
, ITEM_CONTEXT
);
366 ret
= generic_rw(ppos
, &event
->event_context
->p
);
369 pos
->field_nr
= field_nr_saved
;
372 /* Read and print event payload */
373 if (event
->event_fields
) {
374 if (pos
->field_nr
++ != 0)
375 fprintf(pos
->fp
, ",");
376 set_field_names_print(pos
, ITEM_SCOPE
);
377 if (pos
->print_names
)
378 fprintf(pos
->fp
, " event.fields =");
379 field_nr_saved
= pos
->field_nr
;
381 set_field_names_print(pos
, ITEM_PAYLOAD
);
382 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
385 pos
->field_nr
= field_nr_saved
;
388 fprintf(pos
->fp
, "\n");
394 fprintf(stderr
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
399 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
400 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
401 int whence
), FILE *metadata_fp
)
403 struct ctf_text_stream_pos
*pos
;
406 pos
= g_new0(struct ctf_text_stream_pos
, 1);
408 pos
->last_timestamp
= -1ULL;
409 switch (flags
& O_ACCMODE
) {
414 fp
= fopen(path
, "w");
418 pos
->parent
.rw_table
= write_dispatch_table
;
419 pos
->parent
.event_cb
= ctf_text_write_event
;
420 pos
->print_names
= 0;
424 fprintf(stderr
, "[error] Incorrect open flags.\n");
428 return &pos
->trace_descriptor
;
434 void ctf_text_close_trace(struct trace_descriptor
*td
)
436 struct ctf_text_stream_pos
*pos
=
437 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
442 void __attribute__((constructor
)) ctf_text_init(void)
446 ctf_text_format
.name
= g_quark_from_static_string("text");
447 ret
= bt_register_format(&ctf_text_format
);