2 * BabelTrace - Common Trace Format (CTF)
4 * CTF Text Format registration.
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
19 #include <babeltrace/format.h>
20 #include <babeltrace/ctf-text/types.h>
21 #include <babeltrace/ctf/metadata.h>
22 #include <babeltrace/babeltrace.h>
24 #include <uuid/uuid.h>
27 #include <sys/types.h>
35 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
);
36 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
39 rw_dispatch write_dispatch_table
[] = {
40 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
41 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
42 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
43 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
44 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
45 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
46 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
47 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
51 struct format ctf_text_format
= {
52 .open_trace
= ctf_text_open_trace
,
53 .close_trace
= ctf_text_close_trace
,
56 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
57 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
58 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
59 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
60 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
63 void __attribute__((constructor
)) init_quarks(void)
65 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
66 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
67 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
68 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
69 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
72 int print_field(struct definition
*definition
)
74 /* Print all fields in verbose mode */
75 if (babeltrace_verbose
)
78 /* Filter out part of the packet context */
79 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
81 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
83 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
85 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
87 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
94 int ctf_text_write_event(struct stream_pos
*ppos
,
95 struct ctf_stream
*stream
)
97 struct ctf_text_stream_pos
*pos
=
98 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
99 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
101 struct ctf_event
*event_class
;
102 struct ctf_stream_event
*event
;
106 /* print event header */
107 if (stream
->stream_event_header
) {
108 struct definition_integer
*integer_definition
;
109 struct definition
*variant
;
111 /* lookup event id */
112 integer_definition
= lookup_integer(&stream
->stream_event_header
->p
, "id", FALSE
);
113 if (integer_definition
) {
114 id
= integer_definition
->value
._unsigned
;
116 struct definition_enum
*enum_definition
;
118 enum_definition
= lookup_enum(&stream
->stream_event_header
->p
, "id", FALSE
);
119 if (enum_definition
) {
120 id
= enum_definition
->integer
->value
._unsigned
;
124 variant
= lookup_variant(&stream
->stream_event_header
->p
, "v");
126 integer_definition
= lookup_integer(variant
, "id", FALSE
);
127 if (integer_definition
) {
128 id
= integer_definition
->value
._unsigned
;
133 if (id
>= stream_class
->events_by_id
->len
) {
134 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
137 event
= g_ptr_array_index(stream
->events_by_id
, id
);
139 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
142 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
144 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
148 if (stream
->timestamp
) {
149 if (pos
->print_names
)
150 fprintf(pos
->fp
, "timestamp = ");
152 fprintf(pos
->fp
, "[");
153 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
154 if (!pos
->print_names
)
155 fprintf(pos
->fp
, "]");
157 if (pos
->print_names
)
158 fprintf(pos
->fp
, ", ");
160 fprintf(pos
->fp
, " ");
162 if (pos
->print_names
)
163 fprintf(pos
->fp
, "name = ");
164 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
165 if (pos
->print_names
)
168 fprintf(pos
->fp
, ":");
170 /* print cpuid field from packet context */
171 if (stream
->stream_packet_context
) {
172 if (pos
->field_nr
++ != 0)
173 fprintf(pos
->fp
, ",");
174 if (pos
->print_names
)
175 fprintf(pos
->fp
, " stream.packet.context =");
176 field_nr_saved
= pos
->field_nr
;
178 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
181 pos
->field_nr
= field_nr_saved
;
184 /* Only show the event header in verbose mode */
185 if (babeltrace_verbose
&& stream
->stream_event_header
) {
186 if (pos
->field_nr
++ != 0)
187 fprintf(pos
->fp
, ",");
188 if (pos
->print_names
)
189 fprintf(pos
->fp
, " stream.event.header =");
190 field_nr_saved
= pos
->field_nr
;
192 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
195 pos
->field_nr
= field_nr_saved
;
198 /* print stream-declared event context */
199 if (stream
->stream_event_context
) {
200 if (pos
->field_nr
++ != 0)
201 fprintf(pos
->fp
, ",");
202 if (pos
->print_names
)
203 fprintf(pos
->fp
, " stream.event.context =");
204 field_nr_saved
= pos
->field_nr
;
206 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
209 pos
->field_nr
= field_nr_saved
;
212 /* print event-declared event context */
213 if (event
->event_context
) {
214 if (pos
->field_nr
++ != 0)
215 fprintf(pos
->fp
, ",");
216 if (pos
->print_names
)
217 fprintf(pos
->fp
, " event.context =");
218 field_nr_saved
= pos
->field_nr
;
220 ret
= generic_rw(ppos
, &event
->event_context
->p
);
223 pos
->field_nr
= field_nr_saved
;
226 /* Read and print event payload */
227 if (event
->event_fields
) {
228 if (pos
->field_nr
++ != 0)
229 fprintf(pos
->fp
, ",");
230 if (pos
->print_names
)
231 fprintf(pos
->fp
, " event.fields =");
232 field_nr_saved
= pos
->field_nr
;
234 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
237 pos
->field_nr
= field_nr_saved
;
240 fprintf(pos
->fp
, "\n");
246 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
251 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
)
253 struct ctf_text_stream_pos
*pos
;
256 pos
= g_new0(struct ctf_text_stream_pos
, 1);
258 switch (flags
& O_ACCMODE
) {
263 fp
= fopen(path
, "w");
267 pos
->parent
.rw_table
= write_dispatch_table
;
268 pos
->parent
.event_cb
= ctf_text_write_event
;
269 pos
->print_names
= opt_field_names
;
273 fprintf(stdout
, "[error] Incorrect open flags.\n");
277 return &pos
->trace_descriptor
;
283 void ctf_text_close_trace(struct trace_descriptor
*td
)
285 struct ctf_text_stream_pos
*pos
=
286 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
291 void __attribute__((constructor
)) ctf_text_init(void)
295 ctf_text_format
.name
= g_quark_from_static_string("text");
296 ret
= bt_register_format(&ctf_text_format
);