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
,
54 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
55 const char *path
, int flags
,
56 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
57 int whence
), FILE *metadata_fp
);
58 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
61 rw_dispatch write_dispatch_table
[] = {
62 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
63 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
64 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
65 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
66 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
67 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
68 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
69 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
73 struct format ctf_text_format
= {
74 .open_trace
= ctf_text_open_trace
,
75 .close_trace
= ctf_text_close_trace
,
78 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
79 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
80 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
81 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
82 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
85 void __attribute__((constructor
)) init_quarks(void)
87 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
88 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
89 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
90 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
91 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
94 int print_field(struct definition
*definition
)
96 /* Print all fields in verbose mode */
97 if (babeltrace_verbose
)
100 /* Filter out part of the packet context */
101 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
103 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
105 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
107 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
109 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
116 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
120 if (opt_all_field_names
|| opt_scope_field_names
)
121 pos
->print_names
= 1;
123 pos
->print_names
= 0;
126 if (opt_all_field_names
|| opt_header_field_names
)
127 pos
->print_names
= 1;
129 pos
->print_names
= 0;
132 if (opt_all_field_names
|| opt_context_field_names
)
133 pos
->print_names
= 1;
135 pos
->print_names
= 0;
138 if (opt_all_field_names
|| opt_payload_field_names
)
139 pos
->print_names
= 1;
141 pos
->print_names
= 0;
150 int ctf_text_write_event(struct stream_pos
*ppos
,
151 struct ctf_stream
*stream
)
153 struct ctf_text_stream_pos
*pos
=
154 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
155 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
157 struct ctf_event
*event_class
;
158 struct ctf_stream_event
*event
;
163 id
= stream
->event_id
;
165 if (id
>= stream_class
->events_by_id
->len
) {
166 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
169 event
= g_ptr_array_index(stream
->events_by_id
, id
);
171 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
174 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
176 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
180 if (stream
->has_timestamp
) {
181 set_field_names_print(pos
, ITEM_HEADER
);
182 if (pos
->print_names
)
183 fprintf(pos
->fp
, "timestamp = ");
185 fprintf(pos
->fp
, "[");
186 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
187 if (!pos
->print_names
)
188 fprintf(pos
->fp
, "]");
190 if (pos
->print_names
)
191 fprintf(pos
->fp
, ", ");
193 fprintf(pos
->fp
, " ");
195 if ((opt_trace_name
|| opt_all_field_names
) && stream_class
->trace
->path
[0] != '\0') {
196 set_field_names_print(pos
, ITEM_HEADER
);
197 if (pos
->print_names
) {
198 if (opt_trace_name
|| opt_all_field_names
)
199 fprintf(pos
->fp
, "trace = ");
202 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
203 if (pos
->print_names
)
204 fprintf(pos
->fp
, ", ");
206 fprintf(pos
->fp
, " ");
208 if ((opt_trace_domain
) && stream_class
->trace
->domain
[0] != '\0') {
209 set_field_names_print(pos
, ITEM_HEADER
);
210 if (pos
->print_names
) {
211 fprintf(pos
->fp
, "trace:domain = ");
213 if (opt_trace_domain
)
214 fprintf(pos
->fp
, "%s", stream_class
->trace
->domain
);
215 if (pos
->print_names
)
216 fprintf(pos
->fp
, ", ");
219 if ((opt_trace_procname
) && stream_class
->trace
->procname
[0] != '\0') {
220 set_field_names_print(pos
, ITEM_HEADER
);
221 if (pos
->print_names
) {
222 fprintf(pos
->fp
, "trace:procname = ");
223 } else if (dom_print
) {
224 fprintf(pos
->fp
, ":");
227 if (opt_trace_procname
)
228 fprintf(pos
->fp
, "%s", stream_class
->trace
->procname
);
229 if (pos
->print_names
)
230 fprintf(pos
->fp
, ", ");
233 if ((opt_trace_vpid
) && stream_class
->trace
->vpid
[0] != '\0') {
234 set_field_names_print(pos
, ITEM_HEADER
);
235 if (pos
->print_names
) {
236 fprintf(pos
->fp
, "trace:vpid = ");
237 } else if (dom_print
) {
238 fprintf(pos
->fp
, ":");
242 fprintf(pos
->fp
, "%s", stream_class
->trace
->vpid
);
243 if (pos
->print_names
)
244 fprintf(pos
->fp
, ", ");
247 if (dom_print
&& !pos
->print_names
)
248 fprintf(pos
->fp
, " ");
249 set_field_names_print(pos
, ITEM_HEADER
);
250 if (pos
->print_names
)
251 fprintf(pos
->fp
, "name = ");
252 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
253 if (pos
->print_names
)
256 fprintf(pos
->fp
, ":");
258 /* print cpuid field from packet context */
259 if (stream
->stream_packet_context
) {
260 if (pos
->field_nr
++ != 0)
261 fprintf(pos
->fp
, ",");
262 set_field_names_print(pos
, ITEM_SCOPE
);
263 if (pos
->print_names
)
264 fprintf(pos
->fp
, " stream.packet.context =");
265 field_nr_saved
= pos
->field_nr
;
267 set_field_names_print(pos
, ITEM_CONTEXT
);
268 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
271 pos
->field_nr
= field_nr_saved
;
274 /* Only show the event header in verbose mode */
275 if (babeltrace_verbose
&& stream
->stream_event_header
) {
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.event.header =");
281 field_nr_saved
= pos
->field_nr
;
283 set_field_names_print(pos
, ITEM_CONTEXT
);
284 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
287 pos
->field_nr
= field_nr_saved
;
290 /* print stream-declared event context */
291 if (stream
->stream_event_context
) {
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.context =");
297 field_nr_saved
= pos
->field_nr
;
299 set_field_names_print(pos
, ITEM_CONTEXT
);
300 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
303 pos
->field_nr
= field_nr_saved
;
306 /* print event-declared event context */
307 if (event
->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
, " event.context =");
313 field_nr_saved
= pos
->field_nr
;
315 set_field_names_print(pos
, ITEM_CONTEXT
);
316 ret
= generic_rw(ppos
, &event
->event_context
->p
);
319 pos
->field_nr
= field_nr_saved
;
322 /* Read and print event payload */
323 if (event
->event_fields
) {
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.fields =");
329 field_nr_saved
= pos
->field_nr
;
331 set_field_names_print(pos
, ITEM_PAYLOAD
);
332 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
335 pos
->field_nr
= field_nr_saved
;
338 fprintf(pos
->fp
, "\n");
344 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
349 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
350 const char *path
, int flags
,
351 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
352 int whence
), FILE *metadata_fp
)
354 struct ctf_text_stream_pos
*pos
;
357 pos
= g_new0(struct ctf_text_stream_pos
, 1);
359 switch (flags
& O_ACCMODE
) {
364 fp
= fopen(path
, "w");
368 pos
->parent
.rw_table
= write_dispatch_table
;
369 pos
->parent
.event_cb
= ctf_text_write_event
;
370 pos
->print_names
= 0;
374 fprintf(stdout
, "[error] Incorrect open flags.\n");
378 return &pos
->trace_descriptor
;
384 void ctf_text_close_trace(struct trace_descriptor
*td
)
386 struct ctf_text_stream_pos
*pos
=
387 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
392 void __attribute__((constructor
)) ctf_text_init(void)
396 ctf_text_format
.name
= g_quark_from_static_string("text");
397 ret
= bt_register_format(&ctf_text_format
);