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.h>
26 #include <uuid/uuid.h>
29 #include <sys/types.h>
37 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
);
38 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
41 rw_dispatch write_dispatch_table
[] = {
42 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
43 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
44 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
45 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
46 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
47 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
48 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
49 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
53 struct format ctf_text_format
= {
54 .open_trace
= ctf_text_open_trace
,
55 .close_trace
= ctf_text_close_trace
,
58 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
59 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
60 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
61 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
62 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
65 void __attribute__((constructor
)) init_quarks(void)
67 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
68 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
69 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
70 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
71 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
74 int print_field(struct definition
*definition
)
76 /* Print all fields in verbose mode */
77 if (babeltrace_verbose
)
80 /* Filter out part of the packet context */
81 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
83 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
85 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
87 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
89 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
96 int ctf_text_write_event(struct stream_pos
*ppos
,
97 struct ctf_stream
*stream
)
99 struct ctf_text_stream_pos
*pos
=
100 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
101 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
103 struct ctf_event
*event_class
;
104 struct ctf_stream_event
*event
;
108 /* print event header */
109 if (stream
->stream_event_header
) {
110 struct definition_integer
*integer_definition
;
111 struct definition
*variant
;
113 /* lookup event id */
114 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
115 if (integer_definition
) {
116 id
= integer_definition
->value
._unsigned
;
118 struct definition_enum
*enum_definition
;
120 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
121 if (enum_definition
) {
122 id
= enum_definition
->integer
->value
._unsigned
;
126 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
128 integer_definition
= lookup_integer(variant
, "id", FALSE
);
129 if (integer_definition
) {
130 id
= integer_definition
->value
._unsigned
;
135 if (id
>= stream_class
->events_by_id
->len
) {
136 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
139 event
= g_ptr_array_index(stream
->events_by_id
, id
);
141 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
144 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
146 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
150 if (stream
->has_timestamp
) {
151 if (pos
->print_names
)
152 fprintf(pos
->fp
, "timestamp = ");
154 fprintf(pos
->fp
, "[");
155 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
156 if (!pos
->print_names
)
157 fprintf(pos
->fp
, "]");
159 if (pos
->print_names
)
160 fprintf(pos
->fp
, ", ");
162 fprintf(pos
->fp
, " ");
164 if (pos
->print_names
)
165 fprintf(pos
->fp
, "name = ");
166 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
167 if (pos
->print_names
)
170 fprintf(pos
->fp
, ":");
172 /* print cpuid field from packet context */
173 if (stream
->stream_packet_context
) {
174 if (pos
->field_nr
++ != 0)
175 fprintf(pos
->fp
, ",");
176 if (pos
->print_names
)
177 fprintf(pos
->fp
, " stream.packet.context =");
178 field_nr_saved
= pos
->field_nr
;
180 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
183 pos
->field_nr
= field_nr_saved
;
186 /* Only show the event header in verbose mode */
187 if (babeltrace_verbose
&& stream
->stream_event_header
) {
188 if (pos
->field_nr
++ != 0)
189 fprintf(pos
->fp
, ",");
190 if (pos
->print_names
)
191 fprintf(pos
->fp
, " stream.event.header =");
192 field_nr_saved
= pos
->field_nr
;
194 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
197 pos
->field_nr
= field_nr_saved
;
200 /* print stream-declared event context */
201 if (stream
->stream_event_context
) {
202 if (pos
->field_nr
++ != 0)
203 fprintf(pos
->fp
, ",");
204 if (pos
->print_names
)
205 fprintf(pos
->fp
, " stream.event.context =");
206 field_nr_saved
= pos
->field_nr
;
208 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
211 pos
->field_nr
= field_nr_saved
;
214 /* print event-declared event context */
215 if (event
->event_context
) {
216 if (pos
->field_nr
++ != 0)
217 fprintf(pos
->fp
, ",");
218 if (pos
->print_names
)
219 fprintf(pos
->fp
, " event.context =");
220 field_nr_saved
= pos
->field_nr
;
222 ret
= generic_rw(ppos
, &event
->event_context
->p
);
225 pos
->field_nr
= field_nr_saved
;
228 /* Read and print event payload */
229 if (event
->event_fields
) {
230 if (pos
->field_nr
++ != 0)
231 fprintf(pos
->fp
, ",");
232 if (pos
->print_names
)
233 fprintf(pos
->fp
, " event.fields =");
234 field_nr_saved
= pos
->field_nr
;
236 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
239 pos
->field_nr
= field_nr_saved
;
242 fprintf(pos
->fp
, "\n");
248 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
253 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
)
255 struct ctf_text_stream_pos
*pos
;
258 pos
= g_new0(struct ctf_text_stream_pos
, 1);
260 switch (flags
& O_ACCMODE
) {
265 fp
= fopen(path
, "w");
269 pos
->parent
.rw_table
= write_dispatch_table
;
270 pos
->parent
.event_cb
= ctf_text_write_event
;
271 pos
->print_names
= opt_field_names
;
275 fprintf(stdout
, "[error] Incorrect open flags.\n");
279 return &pos
->trace_descriptor
;
285 void ctf_text_close_trace(struct trace_descriptor
*td
)
287 struct ctf_text_stream_pos
*pos
=
288 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
293 void __attribute__((constructor
)) ctf_text_init(void)
297 ctf_text_format
.name
= g_quark_from_static_string("text");
298 ret
= bt_register_format(&ctf_text_format
);