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
,
58 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
59 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(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
173 event
= g_ptr_array_index(stream
->events_by_id
, id
);
175 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
178 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
180 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
184 if (stream
->has_timestamp
) {
185 uint64_t ts_sec
, ts_nsec
;
187 ts_sec
= stream
->timestamp
/ NSEC_PER_SEC
;
188 ts_nsec
= stream
->timestamp
% NSEC_PER_SEC
;
189 set_field_names_print(pos
, ITEM_HEADER
);
190 if (pos
->print_names
)
191 fprintf(pos
->fp
, "timestamp = ");
193 fprintf(pos
->fp
, "[");
194 fprintf(pos
->fp
, "%3" PRIu64
".%09" PRIu64
,
196 if (!pos
->print_names
)
197 fprintf(pos
->fp
, "]");
199 if (pos
->print_names
)
200 fprintf(pos
->fp
, ", ");
202 fprintf(pos
->fp
, " ");
204 if (opt_delta
&& stream
->has_timestamp
) {
205 uint64_t delta
, delta_sec
, delta_nsec
;
207 set_field_names_print(pos
, ITEM_HEADER
);
208 if (pos
->print_names
)
209 fprintf(pos
->fp
, "delta = ");
211 fprintf(pos
->fp
, "(");
212 if (pos
->last_timestamp
!= -1ULL) {
213 delta
= stream
->timestamp
- pos
->last_timestamp
;
214 delta_sec
= delta
/ NSEC_PER_SEC
;
215 delta_nsec
= delta
% NSEC_PER_SEC
;
216 fprintf(pos
->fp
, "+%" PRIu64
".%09" PRIu64
,
217 delta_sec
, delta_nsec
);
219 fprintf(pos
->fp
, "+?.?????????");
221 if (!pos
->print_names
)
222 fprintf(pos
->fp
, ")");
224 if (pos
->print_names
)
225 fprintf(pos
->fp
, ", ");
227 fprintf(pos
->fp
, " ");
228 pos
->last_timestamp
= stream
->timestamp
;
231 if ((opt_trace_name
|| opt_all_field_names
) && stream_class
->trace
->path
[0] != '\0') {
232 set_field_names_print(pos
, ITEM_HEADER
);
233 if (pos
->print_names
) {
234 if (opt_trace_name
|| opt_all_field_names
)
235 fprintf(pos
->fp
, "trace = ");
238 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
239 if (pos
->print_names
)
240 fprintf(pos
->fp
, ", ");
242 fprintf(pos
->fp
, " ");
244 if ((opt_trace_domain
) && stream_class
->trace
->domain
[0] != '\0') {
245 set_field_names_print(pos
, ITEM_HEADER
);
246 if (pos
->print_names
) {
247 fprintf(pos
->fp
, "trace:domain = ");
249 if (opt_trace_domain
)
250 fprintf(pos
->fp
, "%s", stream_class
->trace
->domain
);
251 if (pos
->print_names
)
252 fprintf(pos
->fp
, ", ");
255 if ((opt_trace_procname
) && stream_class
->trace
->procname
[0] != '\0') {
256 set_field_names_print(pos
, ITEM_HEADER
);
257 if (pos
->print_names
) {
258 fprintf(pos
->fp
, "trace:procname = ");
259 } else if (dom_print
) {
260 fprintf(pos
->fp
, ":");
263 if (opt_trace_procname
)
264 fprintf(pos
->fp
, "%s", stream_class
->trace
->procname
);
265 if (pos
->print_names
)
266 fprintf(pos
->fp
, ", ");
269 if ((opt_trace_vpid
) && stream_class
->trace
->vpid
[0] != '\0') {
270 set_field_names_print(pos
, ITEM_HEADER
);
271 if (pos
->print_names
) {
272 fprintf(pos
->fp
, "trace:vpid = ");
273 } else if (dom_print
) {
274 fprintf(pos
->fp
, ":");
278 fprintf(pos
->fp
, "%s", stream_class
->trace
->vpid
);
279 if (pos
->print_names
)
280 fprintf(pos
->fp
, ", ");
283 if ((opt_loglevel
|| opt_all_field_names
) && event_class
->loglevel_identifier
!= 0) {
284 set_field_names_print(pos
, ITEM_HEADER
);
285 if (pos
->print_names
) {
286 fprintf(pos
->fp
, "loglevel = ");
287 } else if (dom_print
) {
288 fprintf(pos
->fp
, ":");
291 fprintf(pos
->fp
, "%s (%lld)",
292 g_quark_to_string(event_class
->loglevel_identifier
),
293 (long long) event_class
->loglevel_value
);
294 if (pos
->print_names
)
295 fprintf(pos
->fp
, ", ");
298 if (dom_print
&& !pos
->print_names
)
299 fprintf(pos
->fp
, " ");
300 set_field_names_print(pos
, ITEM_HEADER
);
301 if (pos
->print_names
)
302 fprintf(pos
->fp
, "name = ");
303 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
304 if (pos
->print_names
)
307 fprintf(pos
->fp
, ":");
309 /* print cpuid field from packet context */
310 if (stream
->stream_packet_context
) {
311 if (pos
->field_nr
++ != 0)
312 fprintf(pos
->fp
, ",");
313 set_field_names_print(pos
, ITEM_SCOPE
);
314 if (pos
->print_names
)
315 fprintf(pos
->fp
, " stream.packet.context =");
316 field_nr_saved
= pos
->field_nr
;
318 set_field_names_print(pos
, ITEM_CONTEXT
);
319 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
322 pos
->field_nr
= field_nr_saved
;
325 /* Only show the event header in verbose mode */
326 if (babeltrace_verbose
&& stream
->stream_event_header
) {
327 if (pos
->field_nr
++ != 0)
328 fprintf(pos
->fp
, ",");
329 set_field_names_print(pos
, ITEM_SCOPE
);
330 if (pos
->print_names
)
331 fprintf(pos
->fp
, " stream.event.header =");
332 field_nr_saved
= pos
->field_nr
;
334 set_field_names_print(pos
, ITEM_CONTEXT
);
335 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
338 pos
->field_nr
= field_nr_saved
;
341 /* print stream-declared event context */
342 if (stream
->stream_event_context
) {
343 if (pos
->field_nr
++ != 0)
344 fprintf(pos
->fp
, ",");
345 set_field_names_print(pos
, ITEM_SCOPE
);
346 if (pos
->print_names
)
347 fprintf(pos
->fp
, " stream.event.context =");
348 field_nr_saved
= pos
->field_nr
;
350 set_field_names_print(pos
, ITEM_CONTEXT
);
351 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
354 pos
->field_nr
= field_nr_saved
;
357 /* print event-declared event context */
358 if (event
->event_context
) {
359 if (pos
->field_nr
++ != 0)
360 fprintf(pos
->fp
, ",");
361 set_field_names_print(pos
, ITEM_SCOPE
);
362 if (pos
->print_names
)
363 fprintf(pos
->fp
, " event.context =");
364 field_nr_saved
= pos
->field_nr
;
366 set_field_names_print(pos
, ITEM_CONTEXT
);
367 ret
= generic_rw(ppos
, &event
->event_context
->p
);
370 pos
->field_nr
= field_nr_saved
;
373 /* Read and print event payload */
374 if (event
->event_fields
) {
375 if (pos
->field_nr
++ != 0)
376 fprintf(pos
->fp
, ",");
377 set_field_names_print(pos
, ITEM_SCOPE
);
378 if (pos
->print_names
)
379 fprintf(pos
->fp
, " event.fields =");
380 field_nr_saved
= pos
->field_nr
;
382 set_field_names_print(pos
, ITEM_PAYLOAD
);
383 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
386 pos
->field_nr
= field_nr_saved
;
389 fprintf(pos
->fp
, "\n");
395 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
400 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
401 const char *path
, int flags
,
402 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
403 int whence
), FILE *metadata_fp
)
405 struct ctf_text_stream_pos
*pos
;
408 pos
= g_new0(struct ctf_text_stream_pos
, 1);
410 pos
->last_timestamp
= -1ULL;
411 switch (flags
& O_ACCMODE
) {
416 fp
= fopen(path
, "w");
420 pos
->parent
.rw_table
= write_dispatch_table
;
421 pos
->parent
.event_cb
= ctf_text_write_event
;
422 pos
->print_names
= 0;
426 fprintf(stdout
, "[error] Incorrect open flags.\n");
430 return &pos
->trace_descriptor
;
436 void ctf_text_close_trace(struct trace_descriptor
*td
)
438 struct ctf_text_stream_pos
*pos
=
439 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
444 void __attribute__((constructor
)) ctf_text_init(void)
448 ctf_text_format
.name
= g_quark_from_static_string("text");
449 ret
= bt_register_format(&ctf_text_format
);