Merge branch 'master' of ssh://efficios.com/home/efficios/git/babeltrace
[babeltrace.git] / formats / ctf-text / ctf-text.c
CommitLineData
642ec66d
MD
1/*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * CTF Text Format registration.
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
642ec66d
MD
9 *
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:
16 *
17 * The above copyright notice and this permission notice shall be included in
18 * all copies or substantial portions of the Software.
19 */
20
21#include <babeltrace/format.h>
22#include <babeltrace/ctf-text/types.h>
31262354 23#include <babeltrace/ctf/metadata.h>
642ec66d
MD
24#include <babeltrace/babeltrace.h>
25#include <inttypes.h>
26#include <uuid/uuid.h>
27#include <sys/mman.h>
28#include <errno.h>
29#include <sys/types.h>
30#include <sys/stat.h>
31#include <fcntl.h>
32#include <dirent.h>
33#include <glib.h>
34#include <unistd.h>
35#include <stdlib.h>
36
37struct trace_descriptor *ctf_text_open_trace(const char *path, int flags);
38void ctf_text_close_trace(struct trace_descriptor *descriptor);
39
642ec66d
MD
40static
41rw_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,
50};
51
52static
53struct format ctf_text_format = {
54 .open_trace = ctf_text_open_trace,
55 .close_trace = ctf_text_close_trace,
56};
57
d335f0f7
MD
58static 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;
63
64static
65void __attribute__((constructor)) init_quarks(void)
66{
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");
72}
73
74int print_field(struct definition *definition)
75{
76 /* Print all fields in verbose mode */
77 if (babeltrace_verbose)
78 return 1;
79
80 /* Filter out part of the packet context */
81 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
82 return 0;
83 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
84 return 0;
85 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
86 return 0;
87 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
88 return 0;
89 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
90 return 0;
91
92 return 1;
93}
94
31262354
MD
95static
96int ctf_text_write_event(struct stream_pos *ppos,
764af3f4 97 struct ctf_stream *stream)
31262354
MD
98{
99 struct ctf_text_stream_pos *pos =
100 container_of(ppos, struct ctf_text_stream_pos, parent);
764af3f4 101 struct ctf_stream_class *stream_class = stream->stream_class;
fd3382e8 102 int field_nr_saved;
31262354 103 struct ctf_event *event_class;
42dc00b7 104 struct ctf_stream_event *event;
31262354 105 uint64_t id = 0;
31262354
MD
106 int ret;
107
108 /* print event header */
e28d4618 109 if (stream->stream_event_header) {
ccdb988e
MD
110 struct definition_integer *integer_definition;
111 struct definition *variant;
112
31262354 113 /* lookup event id */
e28d4618 114 integer_definition = lookup_integer(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
115 if (integer_definition) {
116 id = integer_definition->value._unsigned;
117 } else {
118 struct definition_enum *enum_definition;
119
e28d4618 120 enum_definition = lookup_enum(&stream->stream_event_header->p, "id", FALSE);
ccdb988e
MD
121 if (enum_definition) {
122 id = enum_definition->integer->value._unsigned;
123 }
124 }
31262354 125
e28d4618 126 variant = lookup_variant(&stream->stream_event_header->p, "v");
ccdb988e
MD
127 if (variant) {
128 integer_definition = lookup_integer(variant, "id", FALSE);
129 if (integer_definition) {
130 id = integer_definition->value._unsigned;
131 }
31262354
MD
132 }
133 }
134
135 if (id >= stream_class->events_by_id->len) {
136 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
137 return -EINVAL;
138 }
e28d4618
MD
139 event = g_ptr_array_index(stream->events_by_id, id);
140 if (!event) {
141 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
142 return -EINVAL;
143 }
31262354 144 event_class = g_ptr_array_index(stream_class->events_by_id, id);
e28d4618 145 if (!event) {
31262354
MD
146 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
147 return -EINVAL;
148 }
149
5e2eb0ae 150 if (stream->has_timestamp) {
be60c7fb
MD
151 if (pos->print_names)
152 fprintf(pos->fp, "timestamp = ");
153 else
154 fprintf(pos->fp, "[");
155 fprintf(pos->fp, "%12" PRIu64, stream->timestamp);
156 if (!pos->print_names)
157 fprintf(pos->fp, "]");
aa6bffae 158
be60c7fb
MD
159 if (pos->print_names)
160 fprintf(pos->fp, ", ");
161 else
162 fprintf(pos->fp, " ");
163 }
31262354
MD
164 if (pos->print_names)
165 fprintf(pos->fp, "name = ");
8178fe48
MD
166 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
167 if (pos->print_names)
fd3382e8 168 pos->field_nr++;
8178fe48 169 else
fd3382e8 170 fprintf(pos->fp, ":");
31262354 171
e28d4618
MD
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;
179 pos->field_nr = 0;
180 ret = generic_rw(ppos, &stream->stream_packet_context->p);
181 if (ret)
182 goto error;
183 pos->field_nr = field_nr_saved;
184 }
185
764af3f4 186 /* Only show the event header in verbose mode */
e28d4618 187 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
188 if (pos->field_nr++ != 0)
189 fprintf(pos->fp, ",");
31262354 190 if (pos->print_names)
fd3382e8
MD
191 fprintf(pos->fp, " stream.event.header =");
192 field_nr_saved = pos->field_nr;
193 pos->field_nr = 0;
e28d4618 194 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
195 if (ret)
196 goto error;
fd3382e8 197 pos->field_nr = field_nr_saved;
31262354
MD
198 }
199
200 /* print stream-declared event context */
e28d4618 201 if (stream->stream_event_context) {
fd3382e8
MD
202 if (pos->field_nr++ != 0)
203 fprintf(pos->fp, ",");
31262354 204 if (pos->print_names)
fd3382e8
MD
205 fprintf(pos->fp, " stream.event.context =");
206 field_nr_saved = pos->field_nr;
207 pos->field_nr = 0;
e28d4618 208 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
209 if (ret)
210 goto error;
fd3382e8 211 pos->field_nr = field_nr_saved;
31262354
MD
212 }
213
214 /* print event-declared event context */
e28d4618 215 if (event->event_context) {
fd3382e8
MD
216 if (pos->field_nr++ != 0)
217 fprintf(pos->fp, ",");
31262354 218 if (pos->print_names)
fd3382e8
MD
219 fprintf(pos->fp, " event.context =");
220 field_nr_saved = pos->field_nr;
221 pos->field_nr = 0;
e28d4618 222 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
223 if (ret)
224 goto error;
fd3382e8 225 pos->field_nr = field_nr_saved;
31262354
MD
226 }
227
228 /* Read and print event payload */
e28d4618 229 if (event->event_fields) {
fd3382e8
MD
230 if (pos->field_nr++ != 0)
231 fprintf(pos->fp, ",");
31262354 232 if (pos->print_names)
fd3382e8
MD
233 fprintf(pos->fp, " event.fields =");
234 field_nr_saved = pos->field_nr;
235 pos->field_nr = 0;
e28d4618 236 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
237 if (ret)
238 goto error;
fd3382e8 239 pos->field_nr = field_nr_saved;
31262354 240 }
fd3382e8 241 /* newline */
31262354 242 fprintf(pos->fp, "\n");
fd3382e8 243 pos->field_nr = 0;
31262354
MD
244
245 return 0;
246
247error:
248 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
249 return ret;
250}
251
252
642ec66d
MD
253struct trace_descriptor *ctf_text_open_trace(const char *path, int flags)
254{
255 struct ctf_text_stream_pos *pos;
256 FILE *fp;
257
258 pos = g_new0(struct ctf_text_stream_pos, 1);
259
260 switch (flags & O_ACCMODE) {
989c73bc 261 case O_RDWR:
b61922b5 262 if (!path)
6cf7957b
MD
263 fp = stdout;
264 else
265 fp = fopen(path, "w");
642ec66d
MD
266 if (!fp)
267 goto error;
268 pos->fp = fp;
269 pos->parent.rw_table = write_dispatch_table;
31262354 270 pos->parent.event_cb = ctf_text_write_event;
d63ca2cd 271 pos->print_names = opt_field_names;
642ec66d
MD
272 break;
273 case O_RDONLY:
274 default:
275 fprintf(stdout, "[error] Incorrect open flags.\n");
276 goto error;
277 }
278
279 return &pos->trace_descriptor;
280error:
281 g_free(pos);
282 return NULL;
283}
284
285void ctf_text_close_trace(struct trace_descriptor *td)
286{
287 struct ctf_text_stream_pos *pos =
288 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
289 fclose(pos->fp);
290 g_free(pos);
291}
292
293void __attribute__((constructor)) ctf_text_init(void)
294{
295 int ret;
296
297 ctf_text_format.name = g_quark_from_static_string("text");
298 ret = bt_register_format(&ctf_text_format);
299 assert(!ret);
300}
301
302/* TODO: finalize */
This page took 0.036372 seconds and 4 git commands to generate.