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
,
57 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
58 const char *path
, int flags
,
59 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
60 int whence
), FILE *metadata_fp
);
61 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
64 rw_dispatch write_dispatch_table
[] = {
65 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
66 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
67 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
68 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
69 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
70 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
71 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
72 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
76 struct format ctf_text_format
= {
77 .open_trace
= ctf_text_open_trace
,
78 .close_trace
= ctf_text_close_trace
,
81 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
82 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
83 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
84 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
85 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
88 void __attribute__((constructor
)) init_quarks(void)
90 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
91 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
92 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
93 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
94 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
97 int print_field(struct definition
*definition
)
99 /* Print all fields in verbose mode */
100 if (babeltrace_verbose
)
103 /* Filter out part of the packet context */
104 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
106 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
108 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
110 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
112 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
119 void set_field_names_print(struct ctf_text_stream_pos
*pos
, enum field_item item
)
123 if (opt_all_field_names
|| opt_scope_field_names
)
124 pos
->print_names
= 1;
126 pos
->print_names
= 0;
129 if (opt_all_field_names
|| opt_header_field_names
)
130 pos
->print_names
= 1;
132 pos
->print_names
= 0;
135 if (opt_all_field_names
|| opt_context_field_names
)
136 pos
->print_names
= 1;
138 pos
->print_names
= 0;
141 if (opt_all_field_names
|| opt_payload_field_names
)
142 pos
->print_names
= 1;
144 pos
->print_names
= 0;
153 int ctf_text_write_event(struct stream_pos
*ppos
,
154 struct ctf_stream
*stream
)
156 struct ctf_text_stream_pos
*pos
=
157 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
158 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
160 struct ctf_event
*event_class
;
161 struct ctf_stream_event
*event
;
166 id
= stream
->event_id
;
168 if (id
>= stream_class
->events_by_id
->len
) {
169 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
172 event
= g_ptr_array_index(stream
->events_by_id
, id
);
174 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
177 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
179 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
183 if (stream
->has_timestamp
) {
184 uint64_t ts_sec
, ts_nsec
;
186 ts_sec
= stream
->timestamp
/ NSEC_PER_SEC
;
187 ts_nsec
= stream
->timestamp
% NSEC_PER_SEC
;
188 set_field_names_print(pos
, ITEM_HEADER
);
189 if (pos
->print_names
)
190 fprintf(pos
->fp
, "timestamp = ");
192 fprintf(pos
->fp
, "[");
193 fprintf(pos
->fp
, "%3" PRIu64
".%09" PRIu64
,
195 if (!pos
->print_names
)
196 fprintf(pos
->fp
, "]");
198 if (pos
->print_names
)
199 fprintf(pos
->fp
, ", ");
201 fprintf(pos
->fp
, " ");
203 if ((opt_trace_name
|| opt_all_field_names
) && stream_class
->trace
->path
[0] != '\0') {
204 set_field_names_print(pos
, ITEM_HEADER
);
205 if (pos
->print_names
) {
206 if (opt_trace_name
|| opt_all_field_names
)
207 fprintf(pos
->fp
, "trace = ");
210 fprintf(pos
->fp
, "%s", stream_class
->trace
->path
);
211 if (pos
->print_names
)
212 fprintf(pos
->fp
, ", ");
214 fprintf(pos
->fp
, " ");
216 if ((opt_trace_domain
) && stream_class
->trace
->domain
[0] != '\0') {
217 set_field_names_print(pos
, ITEM_HEADER
);
218 if (pos
->print_names
) {
219 fprintf(pos
->fp
, "trace:domain = ");
221 if (opt_trace_domain
)
222 fprintf(pos
->fp
, "%s", stream_class
->trace
->domain
);
223 if (pos
->print_names
)
224 fprintf(pos
->fp
, ", ");
227 if ((opt_trace_procname
) && stream_class
->trace
->procname
[0] != '\0') {
228 set_field_names_print(pos
, ITEM_HEADER
);
229 if (pos
->print_names
) {
230 fprintf(pos
->fp
, "trace:procname = ");
231 } else if (dom_print
) {
232 fprintf(pos
->fp
, ":");
235 if (opt_trace_procname
)
236 fprintf(pos
->fp
, "%s", stream_class
->trace
->procname
);
237 if (pos
->print_names
)
238 fprintf(pos
->fp
, ", ");
241 if ((opt_trace_vpid
) && stream_class
->trace
->vpid
[0] != '\0') {
242 set_field_names_print(pos
, ITEM_HEADER
);
243 if (pos
->print_names
) {
244 fprintf(pos
->fp
, "trace:vpid = ");
245 } else if (dom_print
) {
246 fprintf(pos
->fp
, ":");
250 fprintf(pos
->fp
, "%s", stream_class
->trace
->vpid
);
251 if (pos
->print_names
)
252 fprintf(pos
->fp
, ", ");
255 if ((opt_loglevel
|| opt_all_field_names
) && event_class
->loglevel_identifier
!= 0) {
256 set_field_names_print(pos
, ITEM_HEADER
);
257 if (pos
->print_names
) {
258 fprintf(pos
->fp
, "loglevel = ");
259 } else if (dom_print
) {
260 fprintf(pos
->fp
, ":");
263 fprintf(pos
->fp
, "%s (%lld)",
264 g_quark_to_string(event_class
->loglevel_identifier
),
265 (long long) event_class
->loglevel_value
);
266 if (pos
->print_names
)
267 fprintf(pos
->fp
, ", ");
270 if (dom_print
&& !pos
->print_names
)
271 fprintf(pos
->fp
, " ");
272 set_field_names_print(pos
, ITEM_HEADER
);
273 if (pos
->print_names
)
274 fprintf(pos
->fp
, "name = ");
275 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
276 if (pos
->print_names
)
279 fprintf(pos
->fp
, ":");
281 /* print cpuid field from packet context */
282 if (stream
->stream_packet_context
) {
283 if (pos
->field_nr
++ != 0)
284 fprintf(pos
->fp
, ",");
285 set_field_names_print(pos
, ITEM_SCOPE
);
286 if (pos
->print_names
)
287 fprintf(pos
->fp
, " stream.packet.context =");
288 field_nr_saved
= pos
->field_nr
;
290 set_field_names_print(pos
, ITEM_CONTEXT
);
291 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
294 pos
->field_nr
= field_nr_saved
;
297 /* Only show the event header in verbose mode */
298 if (babeltrace_verbose
&& stream
->stream_event_header
) {
299 if (pos
->field_nr
++ != 0)
300 fprintf(pos
->fp
, ",");
301 set_field_names_print(pos
, ITEM_SCOPE
);
302 if (pos
->print_names
)
303 fprintf(pos
->fp
, " stream.event.header =");
304 field_nr_saved
= pos
->field_nr
;
306 set_field_names_print(pos
, ITEM_CONTEXT
);
307 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
310 pos
->field_nr
= field_nr_saved
;
313 /* print stream-declared event context */
314 if (stream
->stream_event_context
) {
315 if (pos
->field_nr
++ != 0)
316 fprintf(pos
->fp
, ",");
317 set_field_names_print(pos
, ITEM_SCOPE
);
318 if (pos
->print_names
)
319 fprintf(pos
->fp
, " stream.event.context =");
320 field_nr_saved
= pos
->field_nr
;
322 set_field_names_print(pos
, ITEM_CONTEXT
);
323 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
326 pos
->field_nr
= field_nr_saved
;
329 /* print event-declared event context */
330 if (event
->event_context
) {
331 if (pos
->field_nr
++ != 0)
332 fprintf(pos
->fp
, ",");
333 set_field_names_print(pos
, ITEM_SCOPE
);
334 if (pos
->print_names
)
335 fprintf(pos
->fp
, " event.context =");
336 field_nr_saved
= pos
->field_nr
;
338 set_field_names_print(pos
, ITEM_CONTEXT
);
339 ret
= generic_rw(ppos
, &event
->event_context
->p
);
342 pos
->field_nr
= field_nr_saved
;
345 /* Read and print event payload */
346 if (event
->event_fields
) {
347 if (pos
->field_nr
++ != 0)
348 fprintf(pos
->fp
, ",");
349 set_field_names_print(pos
, ITEM_SCOPE
);
350 if (pos
->print_names
)
351 fprintf(pos
->fp
, " event.fields =");
352 field_nr_saved
= pos
->field_nr
;
354 set_field_names_print(pos
, ITEM_PAYLOAD
);
355 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
358 pos
->field_nr
= field_nr_saved
;
361 fprintf(pos
->fp
, "\n");
367 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
372 struct trace_descriptor
*ctf_text_open_trace(const char *collection_path
,
373 const char *path
, int flags
,
374 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
375 int whence
), FILE *metadata_fp
)
377 struct ctf_text_stream_pos
*pos
;
380 pos
= g_new0(struct ctf_text_stream_pos
, 1);
382 switch (flags
& O_ACCMODE
) {
387 fp
= fopen(path
, "w");
391 pos
->parent
.rw_table
= write_dispatch_table
;
392 pos
->parent
.event_cb
= ctf_text_write_event
;
393 pos
->print_names
= 0;
397 fprintf(stdout
, "[error] Incorrect open flags.\n");
401 return &pos
->trace_descriptor
;
407 void ctf_text_close_trace(struct trace_descriptor
*td
)
409 struct ctf_text_stream_pos
*pos
=
410 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
415 void __attribute__((constructor
)) ctf_text_init(void)
419 ctf_text_format
.name
= g_quark_from_static_string("text");
420 ret
= bt_register_format(&ctf_text_format
);