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 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
38 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
39 int whence
), FILE *metadata_fp
);
40 void ctf_text_close_trace(struct trace_descriptor
*descriptor
);
43 rw_dispatch write_dispatch_table
[] = {
44 [ CTF_TYPE_INTEGER
] = ctf_text_integer_write
,
45 [ CTF_TYPE_FLOAT
] = ctf_text_float_write
,
46 [ CTF_TYPE_ENUM
] = ctf_text_enum_write
,
47 [ CTF_TYPE_STRING
] = ctf_text_string_write
,
48 [ CTF_TYPE_STRUCT
] = ctf_text_struct_write
,
49 [ CTF_TYPE_VARIANT
] = ctf_text_variant_write
,
50 [ CTF_TYPE_ARRAY
] = ctf_text_array_write
,
51 [ CTF_TYPE_SEQUENCE
] = ctf_text_sequence_write
,
55 struct format ctf_text_format
= {
56 .open_trace
= ctf_text_open_trace
,
57 .close_trace
= ctf_text_close_trace
,
60 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
,
61 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
,
62 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
,
63 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
,
64 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
;
67 void __attribute__((constructor
)) init_quarks(void)
69 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
= g_quark_from_static_string("stream.packet.context.timestamp_begin");
70 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
= g_quark_from_static_string("stream.packet.context.timestamp_end");
71 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
= g_quark_from_static_string("stream.packet.context.events_discarded");
72 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
= g_quark_from_static_string("stream.packet.context.content_size");
73 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
= g_quark_from_static_string("stream.packet.context.packet_size");
76 int print_field(struct definition
*definition
)
78 /* Print all fields in verbose mode */
79 if (babeltrace_verbose
)
82 /* Filter out part of the packet context */
83 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN
)
85 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END
)
87 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED
)
89 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE
)
91 if (definition
->path
== Q_STREAM_PACKET_CONTEXT_PACKET_SIZE
)
98 int ctf_text_write_event(struct stream_pos
*ppos
,
99 struct ctf_stream
*stream
)
101 struct ctf_text_stream_pos
*pos
=
102 container_of(ppos
, struct ctf_text_stream_pos
, parent
);
103 struct ctf_stream_class
*stream_class
= stream
->stream_class
;
105 struct ctf_event
*event_class
;
106 struct ctf_stream_event
*event
;
110 id
= stream
->event_id
;
112 if (id
>= stream_class
->events_by_id
->len
) {
113 fprintf(stdout
, "[error] Event id %" PRIu64
" is outside range.\n", id
);
116 event
= g_ptr_array_index(stream
->events_by_id
, id
);
118 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
121 event_class
= g_ptr_array_index(stream_class
->events_by_id
, id
);
123 fprintf(stdout
, "[error] Event id %" PRIu64
" is unknown.\n", id
);
127 if (stream
->has_timestamp
) {
128 if (pos
->print_names
)
129 fprintf(pos
->fp
, "timestamp = ");
131 fprintf(pos
->fp
, "[");
132 fprintf(pos
->fp
, "%12" PRIu64
, stream
->timestamp
);
133 if (!pos
->print_names
)
134 fprintf(pos
->fp
, "]");
136 if (pos
->print_names
)
137 fprintf(pos
->fp
, ", ");
139 fprintf(pos
->fp
, " ");
141 if (pos
->print_names
)
142 fprintf(pos
->fp
, "name = ");
143 fprintf(pos
->fp
, "%s", g_quark_to_string(event_class
->name
));
144 if (pos
->print_names
)
147 fprintf(pos
->fp
, ":");
149 /* print cpuid field from packet context */
150 if (stream
->stream_packet_context
) {
151 if (pos
->field_nr
++ != 0)
152 fprintf(pos
->fp
, ",");
153 if (pos
->print_names
)
154 fprintf(pos
->fp
, " stream.packet.context =");
155 field_nr_saved
= pos
->field_nr
;
157 ret
= generic_rw(ppos
, &stream
->stream_packet_context
->p
);
160 pos
->field_nr
= field_nr_saved
;
163 /* Only show the event header in verbose mode */
164 if (babeltrace_verbose
&& stream
->stream_event_header
) {
165 if (pos
->field_nr
++ != 0)
166 fprintf(pos
->fp
, ",");
167 if (pos
->print_names
)
168 fprintf(pos
->fp
, " stream.event.header =");
169 field_nr_saved
= pos
->field_nr
;
171 ret
= generic_rw(ppos
, &stream
->stream_event_header
->p
);
174 pos
->field_nr
= field_nr_saved
;
177 /* print stream-declared event context */
178 if (stream
->stream_event_context
) {
179 if (pos
->field_nr
++ != 0)
180 fprintf(pos
->fp
, ",");
181 if (pos
->print_names
)
182 fprintf(pos
->fp
, " stream.event.context =");
183 field_nr_saved
= pos
->field_nr
;
185 ret
= generic_rw(ppos
, &stream
->stream_event_context
->p
);
188 pos
->field_nr
= field_nr_saved
;
191 /* print event-declared event context */
192 if (event
->event_context
) {
193 if (pos
->field_nr
++ != 0)
194 fprintf(pos
->fp
, ",");
195 if (pos
->print_names
)
196 fprintf(pos
->fp
, " event.context =");
197 field_nr_saved
= pos
->field_nr
;
199 ret
= generic_rw(ppos
, &event
->event_context
->p
);
202 pos
->field_nr
= field_nr_saved
;
205 /* Read and print event payload */
206 if (event
->event_fields
) {
207 if (pos
->field_nr
++ != 0)
208 fprintf(pos
->fp
, ",");
209 if (pos
->print_names
)
210 fprintf(pos
->fp
, " event.fields =");
211 field_nr_saved
= pos
->field_nr
;
213 ret
= generic_rw(ppos
, &event
->event_fields
->p
);
216 pos
->field_nr
= field_nr_saved
;
219 fprintf(pos
->fp
, "\n");
225 fprintf(stdout
, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
230 struct trace_descriptor
*ctf_text_open_trace(const char *path
, int flags
,
231 void (*move_pos_slow
)(struct ctf_stream_pos
*pos
, size_t offset
,
232 int whence
), FILE *metadata_fp
)
234 struct ctf_text_stream_pos
*pos
;
237 pos
= g_new0(struct ctf_text_stream_pos
, 1);
239 switch (flags
& O_ACCMODE
) {
244 fp
= fopen(path
, "w");
248 pos
->parent
.rw_table
= write_dispatch_table
;
249 pos
->parent
.event_cb
= ctf_text_write_event
;
250 pos
->print_names
= opt_field_names
;
254 fprintf(stdout
, "[error] Incorrect open flags.\n");
258 return &pos
->trace_descriptor
;
264 void ctf_text_close_trace(struct trace_descriptor
*td
)
266 struct ctf_text_stream_pos
*pos
=
267 container_of(td
, struct ctf_text_stream_pos
, trace_descriptor
);
272 void __attribute__((constructor
)) ctf_text_init(void)
276 ctf_text_format
.name
= g_quark_from_static_string("text");
277 ret
= bt_register_format(&ctf_text_format
);