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
;
50 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
51 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
52 int whence
), FILE *metadata_fp
);
53 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
56 rw_dispatch write_dispatch_table
[] = {
57 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
58 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
59 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
60 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
61 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
62 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
63 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
64 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
68 struct format ctf_text_format
= {
69 .open_trace
= ctf_text_open_trace
,
70 .close_trace
= ctf_text_close_trace
,
73 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
74 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
75 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
76 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
77 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
80 void __attribute__((constructor
)) init_quarks(void)
82 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
83 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
84 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
85 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
86 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
89 int print_field(struct definition
*definition
)
91 /* Print all fields in verbose mode */
92 if (babeltrace_verbose
)
95 /* Filter out part of the packet context */
96 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
98 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
100 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
102 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
104 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
111 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
115 if (opt_all_field_names
|| opt_scope_field_names
)
116 pos
->print_names
= 1;
118 pos
->print_names
= 0;
121 if (opt_all_field_names
|| opt_header_field_names
)
122 pos
->print_names
= 1;
124 pos
->print_names
= 0;
127 if (opt_all_field_names
|| opt_context_field_names
)
128 pos
->print_names
= 1;
130 pos
->print_names
= 0;
133 if (opt_all_field_names
|| opt_payload_field_names
)
134 pos
->print_names
= 1;
136 pos
->print_names
= 0;
145 int ctf_text_write_event(struct stream_pos
*ppos
,
146 struct ctf_stream
*stream
)
148 struct ctf_text_stream_pos
*pos
=
149 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
150 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
152 struct ctf_event
*event_class
;
153 struct ctf_stream_event
*event
;
157 id
= stream
->event_id
;
159 if (id
>= stream_class
->events_by_id
->len
) {
160 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
163 event
= g_ptr_array_index(stream
->events_by_id
, id
);
165 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
168 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
170 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
174 if (stream
->has_timestamp
) {
175 set_field_names_print(pos
, ITEM_HEADER
);
176 if (pos
->print_names
)
177 fprintf(pos
->fp
, "timestamp = ");
179 fprintf(pos
->fp
, "[");
180 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
181 if (!pos
->print_names
)
182 fprintf(pos
->fp
, "]");
184 if (pos
->print_names
)
185 fprintf(pos
->fp
, ", ");
187 fprintf(pos
->fp
, " ");
189 set_field_names_print(pos
, ITEM_HEADER
);
190 if (pos
->print_names
)
191 fprintf(pos
->fp
, "name = ");
192 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
193 if (pos
->print_names
)
196 fprintf(pos
->fp
, ":");
198 /* print cpuid field from packet context */
199 if (stream
->stream_packet_context
) {
200 if (pos
->field_nr
++ != 0)
201 fprintf(pos
->fp
, ",");
202 set_field_names_print(pos
, ITEM_SCOPE
);
203 if (pos
->print_names
)
204 fprintf(pos
->fp
, " stream.packet.context =");
205 field_nr_saved
= pos
->field_nr
;
207 set_field_names_print(pos
, ITEM_CONTEXT
);
208 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
211 pos
->field_nr
= field_nr_saved
;
214 /* Only show the event header in verbose mode */
215 if (babeltrace_verbose
&& stream
->stream_event_header
) {
216 if (pos
->field_nr
++ != 0)
217 fprintf(pos
->fp
, ",");
218 set_field_names_print(pos
, ITEM_SCOPE
);
219 if (pos
->print_names
)
220 fprintf(pos
->fp
, " stream.event.header =");
221 field_nr_saved
= pos
->field_nr
;
223 set_field_names_print(pos
, ITEM_CONTEXT
);
224 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
227 pos
->field_nr
= field_nr_saved
;
230 /* print stream-declared event context */
231 if (stream
->stream_event_context
) {
232 if (pos
->field_nr
++ != 0)
233 fprintf(pos
->fp
, ",");
234 set_field_names_print(pos
, ITEM_SCOPE
);
235 if (pos
->print_names
)
236 fprintf(pos
->fp
, " stream.event.context =");
237 field_nr_saved
= pos
->field_nr
;
239 set_field_names_print(pos
, ITEM_CONTEXT
);
240 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
243 pos
->field_nr
= field_nr_saved
;
246 /* print event-declared event context */
247 if (event
->event_context
) {
248 if (pos
->field_nr
++ != 0)
249 fprintf(pos
->fp
, ",");
250 set_field_names_print(pos
, ITEM_SCOPE
);
251 if (pos
->print_names
)
252 fprintf(pos
->fp
, " event.context =");
253 field_nr_saved
= pos
->field_nr
;
255 set_field_names_print(pos
, ITEM_CONTEXT
);
256 ret
= generic_rw(ppos
, &event
->event_context
->p
);
259 pos
->field_nr
= field_nr_saved
;
262 /* Read and print event payload */
263 if (event
->event_fields
) {
264 if (pos
->field_nr
++ != 0)
265 fprintf(pos
->fp
, ",");
266 set_field_names_print(pos
, ITEM_SCOPE
);
267 if (pos
->print_names
)
268 fprintf(pos
->fp
, " event.fields =");
269 field_nr_saved
= pos
->field_nr
;
271 set_field_names_print(pos
, ITEM_PAYLOAD
);
272 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
275 pos
->field_nr
= field_nr_saved
;
278 fprintf(pos
->fp
, "\n");
284 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
289 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
290 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
291 int whence
), FILE *metadata_fp
)
293 struct ctf_text_stream_pos
*pos
;
296 pos
= g_new0(struct ctf_text_stream_pos
, 1);
298 switch (flags
& O_ACCMODE
) {
303 fp
= fopen(path
, "w");
307 pos
->parent
.rw_table
= write_dispatch_table
;
308 pos
->parent
.event_cb
= ctf_text_write_event
;
309 pos
->print_names
= 0;
313 fprintf(stdout
, "[error] Incorrect open flags.\n");
317 return &pos
->trace_descriptor
;
323 void ctf_text_close_trace(struct trace_descriptor
*td
)
325 struct ctf_text_stream_pos
*pos
=
326 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
331 void __attribute__((constructor
)) ctf_text_init(void)
335 ctf_text_format
.name
= g_quark_from_static_string("text");
336 ret
= bt_register_format(&ctf_text_format
);