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 int opt_all_field_names
,
38 opt_scope_field_names
,
39 opt_header_field_names
,
40 opt_context_field_names
,
41 opt_payload_field_names
,
55 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
56 const char *path
, int flags
,
57 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
58 int whence
), FILE *metadata_fp
);
59 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
62 rw_dispatch write_dispatch_table
[] = {
63 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
64 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
65 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
66 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
67 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
68 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
69 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
70 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
74 struct format ctf_text_format
= {
75 .open_trace
= ctf_text_open_trace
,
76 .close_trace
= ctf_text_close_trace
,
79 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
80 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
81 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
82 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
83 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
86 void __attribute__((constructor
)) init_quarks(void)
88 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
89 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
90 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
91 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
92 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
95 int print_field(struct definition
*definition
)
97 /* Print all fields in verbose mode */
98 if (babeltrace_verbose
)
101 /* Filter out part of the packet context */
102 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
104 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
106 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
108 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
110 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
117 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
121 if (opt_all_field_names
|| opt_scope_field_names
)
122 pos
->print_names
= 1;
124 pos
->print_names
= 0;
127 if (opt_all_field_names
|| opt_header_field_names
)
128 pos
->print_names
= 1;
130 pos
->print_names
= 0;
133 if (opt_all_field_names
|| opt_context_field_names
)
134 pos
->print_names
= 1;
136 pos
->print_names
= 0;
139 if (opt_all_field_names
|| opt_payload_field_names
)
140 pos
->print_names
= 1;
142 pos
->print_names
= 0;
151 int ctf_text_write_event(struct stream_pos
*ppos
,
152 struct ctf_stream
*stream
)
154 struct ctf_text_stream_pos
*pos
=
155 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
156 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
158 struct ctf_event
*event_class
;
159 struct ctf_stream_event
*event
;
164 id
= stream
->event_id
;
166 if (id
>= stream_class
->events_by_id
->len
) {
167 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
170 event
= g_ptr_array_index(stream
->events_by_id
, id
);
172 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
175 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
177 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
181 if (stream
->has_timestamp
) {
182 set_field_names_print(pos
, ITEM_HEADER
);
183 if (pos
->print_names
)
184 fprintf(pos
->fp
, "timestamp = ");
186 fprintf(pos
->fp
, "[");
187 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
188 if (!pos
->print_names
)
189 fprintf(pos
->fp
, "]");
191 if (pos
->print_names
)
192 fprintf(pos
->fp
, ", ");
194 fprintf(pos
->fp
, " ");
196 if ((opt_trace_name
|| opt_all_field_names
) && stream_class
->trace
->path
[0] != '\0') {
197 set_field_names_print(pos
, ITEM_HEADER
);
198 if (pos
->print_names
) {
199 if (opt_trace_name
|| opt_all_field_names
)
200 fprintf(pos
->fp
, "trace = ");
203 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
204 if (pos
->print_names
)
205 fprintf(pos
->fp
, ", ");
207 fprintf(pos
->fp
, " ");
209 if ((opt_trace_domain
) && stream_class
->trace
->domain
[0] != '\0') {
210 set_field_names_print(pos
, ITEM_HEADER
);
211 if (pos
->print_names
) {
212 fprintf(pos
->fp
, "trace:domain = ");
214 if (opt_trace_domain
)
215 fprintf(pos
->fp
, "%s", stream_class
->trace
->domain
);
216 if (pos
->print_names
)
217 fprintf(pos
->fp
, ", ");
220 if ((opt_trace_procname
) && stream_class
->trace
->procname
[0] != '\0') {
221 set_field_names_print(pos
, ITEM_HEADER
);
222 if (pos
->print_names
) {
223 fprintf(pos
->fp
, "trace:procname = ");
224 } else if (dom_print
) {
225 fprintf(pos
->fp
, ":");
228 if (opt_trace_procname
)
229 fprintf(pos
->fp
, "%s", stream_class
->trace
->procname
);
230 if (pos
->print_names
)
231 fprintf(pos
->fp
, ", ");
234 if ((opt_trace_vpid
) && stream_class
->trace
->vpid
[0] != '\0') {
235 set_field_names_print(pos
, ITEM_HEADER
);
236 if (pos
->print_names
) {
237 fprintf(pos
->fp
, "trace:vpid = ");
238 } else if (dom_print
) {
239 fprintf(pos
->fp
, ":");
243 fprintf(pos
->fp
, "%s", stream_class
->trace
->vpid
);
244 if (pos
->print_names
)
245 fprintf(pos
->fp
, ", ");
248 if ((opt_loglevel
|| opt_all_field_names
) && event_class
->loglevel_identifier
!= 0) {
249 set_field_names_print(pos
, ITEM_HEADER
);
250 if (pos
->print_names
) {
251 fprintf(pos
->fp
, "loglevel = ");
252 } else if (dom_print
) {
253 fprintf(pos
->fp
, ":");
256 fprintf(pos
->fp
, "%s (%lld)",
257 g_quark_to_string(event_class
->loglevel_identifier
),
258 (long long) event_class
->loglevel_value
);
259 if (pos
->print_names
)
260 fprintf(pos
->fp
, ", ");
263 if (dom_print
&& !pos
->print_names
)
264 fprintf(pos
->fp
, " ");
265 set_field_names_print(pos
, ITEM_HEADER
);
266 if (pos
->print_names
)
267 fprintf(pos
->fp
, "name = ");
268 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
269 if (pos
->print_names
)
272 fprintf(pos
->fp
, ":");
274 /* print cpuid field from packet context */
275 if (stream
->stream_packet_context
) {
276 if (pos
->field_nr
++ != 0)
277 fprintf(pos
->fp
, ",");
278 set_field_names_print(pos
, ITEM_SCOPE
);
279 if (pos
->print_names
)
280 fprintf(pos
->fp
, " stream.packet.context =");
281 field_nr_saved
= pos
->field_nr
;
283 set_field_names_print(pos
, ITEM_CONTEXT
);
284 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
287 pos
->field_nr
= field_nr_saved
;
290 /* Only show the event header in verbose mode */
291 if (babeltrace_verbose
&& stream
->stream_event_header
) {
292 if (pos
->field_nr
++ != 0)
293 fprintf(pos
->fp
, ",");
294 set_field_names_print(pos
, ITEM_SCOPE
);
295 if (pos
->print_names
)
296 fprintf(pos
->fp
, " stream.event.header =");
297 field_nr_saved
= pos
->field_nr
;
299 set_field_names_print(pos
, ITEM_CONTEXT
);
300 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
303 pos
->field_nr
= field_nr_saved
;
306 /* print stream-declared event context */
307 if (stream
->stream_event_context
) {
308 if (pos
->field_nr
++ != 0)
309 fprintf(pos
->fp
, ",");
310 set_field_names_print(pos
, ITEM_SCOPE
);
311 if (pos
->print_names
)
312 fprintf(pos
->fp
, " stream.event.context =");
313 field_nr_saved
= pos
->field_nr
;
315 set_field_names_print(pos
, ITEM_CONTEXT
);
316 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
319 pos
->field_nr
= field_nr_saved
;
322 /* print event-declared event context */
323 if (event
->event_context
) {
324 if (pos
->field_nr
++ != 0)
325 fprintf(pos
->fp
, ",");
326 set_field_names_print(pos
, ITEM_SCOPE
);
327 if (pos
->print_names
)
328 fprintf(pos
->fp
, " event.context =");
329 field_nr_saved
= pos
->field_nr
;
331 set_field_names_print(pos
, ITEM_CONTEXT
);
332 ret
= generic_rw(ppos
, &event
->event_context
->p
);
335 pos
->field_nr
= field_nr_saved
;
338 /* Read and print event payload */
339 if (event
->event_fields
) {
340 if (pos
->field_nr
++ != 0)
341 fprintf(pos
->fp
, ",");
342 set_field_names_print(pos
, ITEM_SCOPE
);
343 if (pos
->print_names
)
344 fprintf(pos
->fp
, " event.fields =");
345 field_nr_saved
= pos
->field_nr
;
347 set_field_names_print(pos
, ITEM_PAYLOAD
);
348 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
351 pos
->field_nr
= field_nr_saved
;
354 fprintf(pos
->fp
, "\n");
360 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
365 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
366 const char *path
, int flags
,
367 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
368 int whence
), FILE *metadata_fp
)
370 struct ctf_text_stream_pos
*pos
;
373 pos
= g_new0(struct ctf_text_stream_pos
, 1);
375 switch (flags
& O_ACCMODE
) {
380 fp
= fopen(path
, "w");
384 pos
->parent
.rw_table
= write_dispatch_table
;
385 pos
->parent
.event_cb
= ctf_text_write_event
;
386 pos
->print_names
= 0;
390 fprintf(stdout
, "[error] Incorrect open flags.\n");
394 return &pos
->trace_descriptor
;
400 void ctf_text_close_trace(struct trace_descriptor
*td
)
402 struct ctf_text_stream_pos
*pos
=
403 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
408 void __attribute__((constructor
)) ctf_text_init(void)
412 ctf_text_format
.name
= g_quark_from_static_string("text");
413 ret
= bt_register_format(&ctf_text_format
);