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
,
51 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
52 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
53 int whence
), FILE *metadata_fp
);
54 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
57 rw_dispatch write_dispatch_table
[] = {
58 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
59 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
60 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
61 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
62 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
63 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
64 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
65 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
69 struct format ctf_text_format
= {
70 .open_trace
= ctf_text_open_trace
,
71 .close_trace
= ctf_text_close_trace
,
74 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
75 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
76 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
77 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
78 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
81 void __attribute__((constructor
)) init_quarks(void)
83 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
84 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
85 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
86 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
87 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
90 int print_field(struct definition
*definition
)
92 /* Print all fields in verbose mode */
93 if (babeltrace_verbose
)
96 /* Filter out part of the packet context */
97 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
99 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
101 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
103 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
105 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
112 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
116 if (opt_all_field_names
|| opt_scope_field_names
)
117 pos
->print_names
= 1;
119 pos
->print_names
= 0;
122 if (opt_all_field_names
|| opt_header_field_names
)
123 pos
->print_names
= 1;
125 pos
->print_names
= 0;
128 if (opt_all_field_names
|| opt_context_field_names
)
129 pos
->print_names
= 1;
131 pos
->print_names
= 0;
134 if (opt_all_field_names
|| opt_payload_field_names
)
135 pos
->print_names
= 1;
137 pos
->print_names
= 0;
146 int ctf_text_write_event(struct stream_pos
*ppos
,
147 struct ctf_stream
*stream
)
149 struct ctf_text_stream_pos
*pos
=
150 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
151 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
153 struct ctf_event
*event_class
;
154 struct ctf_stream_event
*event
;
158 id
= stream
->event_id
;
160 if (id
>= stream_class
->events_by_id
->len
) {
161 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
164 event
= g_ptr_array_index(stream
->events_by_id
, id
);
166 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
169 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
171 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
175 if (stream
->has_timestamp
) {
176 set_field_names_print(pos
, ITEM_HEADER
);
177 if (pos
->print_names
)
178 fprintf(pos
->fp
, "timestamp = ");
180 fprintf(pos
->fp
, "[");
181 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
182 if (!pos
->print_names
)
183 fprintf(pos
->fp
, "]");
185 if (pos
->print_names
)
186 fprintf(pos
->fp
, ", ");
188 fprintf(pos
->fp
, " ");
190 if ((opt_trace_name
|| opt_all_field_names
) && stream_class
->trace
->path
[0] != '\0') {
191 set_field_names_print(pos
, ITEM_HEADER
);
192 if (pos
->print_names
)
193 fprintf(pos
->fp
, "trace = ");
195 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
197 if (pos
->print_names
)
198 fprintf(pos
->fp
, ", ");
200 fprintf(pos
->fp
, " ");
202 set_field_names_print(pos
, ITEM_HEADER
);
203 if (pos
->print_names
)
204 fprintf(pos
->fp
, "name = ");
205 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
206 if (pos
->print_names
)
209 fprintf(pos
->fp
, ":");
211 /* print cpuid field from packet context */
212 if (stream
->stream_packet_context
) {
213 if (pos
->field_nr
++ != 0)
214 fprintf(pos
->fp
, ",");
215 set_field_names_print(pos
, ITEM_SCOPE
);
216 if (pos
->print_names
)
217 fprintf(pos
->fp
, " stream.packet.context =");
218 field_nr_saved
= pos
->field_nr
;
220 set_field_names_print(pos
, ITEM_CONTEXT
);
221 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
224 pos
->field_nr
= field_nr_saved
;
227 /* Only show the event header in verbose mode */
228 if (babeltrace_verbose
&& stream
->stream_event_header
) {
229 if (pos
->field_nr
++ != 0)
230 fprintf(pos
->fp
, ",");
231 set_field_names_print(pos
, ITEM_SCOPE
);
232 if (pos
->print_names
)
233 fprintf(pos
->fp
, " stream.event.header =");
234 field_nr_saved
= pos
->field_nr
;
236 set_field_names_print(pos
, ITEM_CONTEXT
);
237 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
240 pos
->field_nr
= field_nr_saved
;
243 /* print stream-declared event context */
244 if (stream
->stream_event_context
) {
245 if (pos
->field_nr
++ != 0)
246 fprintf(pos
->fp
, ",");
247 set_field_names_print(pos
, ITEM_SCOPE
);
248 if (pos
->print_names
)
249 fprintf(pos
->fp
, " stream.event.context =");
250 field_nr_saved
= pos
->field_nr
;
252 set_field_names_print(pos
, ITEM_CONTEXT
);
253 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
256 pos
->field_nr
= field_nr_saved
;
259 /* print event-declared event context */
260 if (event
->event_context
) {
261 if (pos
->field_nr
++ != 0)
262 fprintf(pos
->fp
, ",");
263 set_field_names_print(pos
, ITEM_SCOPE
);
264 if (pos
->print_names
)
265 fprintf(pos
->fp
, " event.context =");
266 field_nr_saved
= pos
->field_nr
;
268 set_field_names_print(pos
, ITEM_CONTEXT
);
269 ret
= generic_rw(ppos
, &event
->event_context
->p
);
272 pos
->field_nr
= field_nr_saved
;
275 /* Read and print event payload */
276 if (event
->event_fields
) {
277 if (pos
->field_nr
++ != 0)
278 fprintf(pos
->fp
, ",");
279 set_field_names_print(pos
, ITEM_SCOPE
);
280 if (pos
->print_names
)
281 fprintf(pos
->fp
, " event.fields =");
282 field_nr_saved
= pos
->field_nr
;
284 set_field_names_print(pos
, ITEM_PAYLOAD
);
285 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
288 pos
->field_nr
= field_nr_saved
;
291 fprintf(pos
->fp
, "\n");
297 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
302 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
303 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
304 int whence
), FILE *metadata_fp
)
306 struct ctf_text_stream_pos
*pos
;
309 pos
= g_new0(struct ctf_text_stream_pos
, 1);
311 switch (flags
& O_ACCMODE
) {
316 fp
= fopen(path
, "w");
320 pos
->parent
.rw_table
= write_dispatch_table
;
321 pos
->parent
.event_cb
= ctf_text_write_event
;
322 pos
->print_names
= 0;
326 fprintf(stdout
, "[error] Incorrect open flags.\n");
330 return &pos
->trace_descriptor
;
336 void ctf_text_close_trace(struct trace_descriptor
*td
)
338 struct ctf_text_stream_pos
*pos
=
339 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
344 void __attribute__((constructor
)) ctf_text_init(void)
348 ctf_text_format
.name
= g_quark_from_static_string("text");
349 ret
= bt_register_format(&ctf_text_format
);