5879cf4f8dfbb2a1f02216332e3dc817217a88c0
[babeltrace.git] / formats / ctf-text / ctf-text.c
1 /*
2 * BabelTrace - Common Trace Format (CTF)
3 *
4 * CTF Text Format registration.
5 *
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.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
37 int opt_all_field_names,
38 opt_scope_field_names,
39 opt_header_field_names,
40 opt_context_field_names,
41 opt_payload_field_names;
42
43 enum field_item {
44 ITEM_SCOPE,
45 ITEM_HEADER,
46 ITEM_CONTEXT,
47 ITEM_PAYLOAD,
48 };
49
50 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
51 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
52 int whence), FILE *metadata_fp);
53 void ctf_text_close_trace(struct trace_descriptor *descriptor);
54
55 static
56 rw_dispatch write_dispatch_table[] = {
57 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
58 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
59 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
60 [ CTF_TYPE_STRING ] = ctf_text_string_write,
61 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
62 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
63 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
64 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
65 };
66
67 static
68 struct format ctf_text_format = {
69 .open_trace = ctf_text_open_trace,
70 .close_trace = ctf_text_close_trace,
71 };
72
73 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
74 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
75 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
76 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
77 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
78
79 static
80 void __attribute__((constructor)) init_quarks(void)
81 {
82 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
83 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
84 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
85 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
86 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
87 }
88
89 int print_field(struct definition *definition)
90 {
91 /* Print all fields in verbose mode */
92 if (babeltrace_verbose)
93 return 1;
94
95 /* Filter out part of the packet context */
96 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
97 return 0;
98 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
99 return 0;
100 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
101 return 0;
102 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
103 return 0;
104 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
105 return 0;
106
107 return 1;
108 }
109
110 static
111 void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
112 {
113 switch (item) {
114 case ITEM_SCOPE:
115 if (opt_all_field_names || opt_scope_field_names)
116 pos->print_names = 1;
117 else
118 pos->print_names = 0;
119 break;
120 case ITEM_HEADER:
121 if (opt_all_field_names || opt_header_field_names)
122 pos->print_names = 1;
123 else
124 pos->print_names = 0;
125 break;
126 case ITEM_CONTEXT:
127 if (opt_all_field_names || opt_context_field_names)
128 pos->print_names = 1;
129 else
130 pos->print_names = 0;
131 break;
132 case ITEM_PAYLOAD:
133 if (opt_all_field_names || opt_payload_field_names)
134 pos->print_names = 1;
135 else
136 pos->print_names = 0;
137
138 break;
139 default:
140 assert(0);
141 }
142 }
143
144 static
145 int ctf_text_write_event(struct stream_pos *ppos,
146 struct ctf_stream *stream)
147 {
148 struct ctf_text_stream_pos *pos =
149 container_of(ppos, struct ctf_text_stream_pos, parent);
150 struct ctf_stream_class *stream_class = stream->stream_class;
151 int field_nr_saved;
152 struct ctf_event *event_class;
153 struct ctf_stream_event *event;
154 uint64_t id;
155 int ret;
156
157 id = stream->event_id;
158
159 if (id >= stream_class->events_by_id->len) {
160 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
161 return -EINVAL;
162 }
163 event = g_ptr_array_index(stream->events_by_id, id);
164 if (!event) {
165 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
166 return -EINVAL;
167 }
168 event_class = g_ptr_array_index(stream_class->events_by_id, id);
169 if (!event) {
170 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
171 return -EINVAL;
172 }
173
174 if (stream->has_timestamp) {
175 set_field_names_print(pos, ITEM_HEADER);
176 if (pos->print_names)
177 fprintf(pos->fp, "timestamp = ");
178 else
179 fprintf(pos->fp, "[");
180 fprintf(pos->fp, "%12" PRIu64, stream->timestamp);
181 if (!pos->print_names)
182 fprintf(pos->fp, "]");
183
184 if (pos->print_names)
185 fprintf(pos->fp, ", ");
186 else
187 fprintf(pos->fp, " ");
188 }
189 set_field_names_print(pos, ITEM_HEADER);
190 if (pos->print_names)
191 fprintf(pos->fp, "name = ");
192 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
193 if (pos->print_names)
194 pos->field_nr++;
195 else
196 fprintf(pos->fp, ":");
197
198 /* print cpuid field from packet context */
199 if (stream->stream_packet_context) {
200 if (pos->field_nr++ != 0)
201 fprintf(pos->fp, ",");
202 set_field_names_print(pos, ITEM_SCOPE);
203 if (pos->print_names)
204 fprintf(pos->fp, " stream.packet.context =");
205 field_nr_saved = pos->field_nr;
206 pos->field_nr = 0;
207 set_field_names_print(pos, ITEM_CONTEXT);
208 ret = generic_rw(ppos, &stream->stream_packet_context->p);
209 if (ret)
210 goto error;
211 pos->field_nr = field_nr_saved;
212 }
213
214 /* Only show the event header in verbose mode */
215 if (babeltrace_verbose && stream->stream_event_header) {
216 if (pos->field_nr++ != 0)
217 fprintf(pos->fp, ",");
218 set_field_names_print(pos, ITEM_SCOPE);
219 if (pos->print_names)
220 fprintf(pos->fp, " stream.event.header =");
221 field_nr_saved = pos->field_nr;
222 pos->field_nr = 0;
223 set_field_names_print(pos, ITEM_CONTEXT);
224 ret = generic_rw(ppos, &stream->stream_event_header->p);
225 if (ret)
226 goto error;
227 pos->field_nr = field_nr_saved;
228 }
229
230 /* print stream-declared event context */
231 if (stream->stream_event_context) {
232 if (pos->field_nr++ != 0)
233 fprintf(pos->fp, ",");
234 set_field_names_print(pos, ITEM_SCOPE);
235 if (pos->print_names)
236 fprintf(pos->fp, " stream.event.context =");
237 field_nr_saved = pos->field_nr;
238 pos->field_nr = 0;
239 set_field_names_print(pos, ITEM_CONTEXT);
240 ret = generic_rw(ppos, &stream->stream_event_context->p);
241 if (ret)
242 goto error;
243 pos->field_nr = field_nr_saved;
244 }
245
246 /* print event-declared event context */
247 if (event->event_context) {
248 if (pos->field_nr++ != 0)
249 fprintf(pos->fp, ",");
250 set_field_names_print(pos, ITEM_SCOPE);
251 if (pos->print_names)
252 fprintf(pos->fp, " event.context =");
253 field_nr_saved = pos->field_nr;
254 pos->field_nr = 0;
255 set_field_names_print(pos, ITEM_CONTEXT);
256 ret = generic_rw(ppos, &event->event_context->p);
257 if (ret)
258 goto error;
259 pos->field_nr = field_nr_saved;
260 }
261
262 /* Read and print event payload */
263 if (event->event_fields) {
264 if (pos->field_nr++ != 0)
265 fprintf(pos->fp, ",");
266 set_field_names_print(pos, ITEM_SCOPE);
267 if (pos->print_names)
268 fprintf(pos->fp, " event.fields =");
269 field_nr_saved = pos->field_nr;
270 pos->field_nr = 0;
271 set_field_names_print(pos, ITEM_PAYLOAD);
272 ret = generic_rw(ppos, &event->event_fields->p);
273 if (ret)
274 goto error;
275 pos->field_nr = field_nr_saved;
276 }
277 /* newline */
278 fprintf(pos->fp, "\n");
279 pos->field_nr = 0;
280
281 return 0;
282
283 error:
284 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
285 return ret;
286 }
287
288
289 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
290 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
291 int whence), FILE *metadata_fp)
292 {
293 struct ctf_text_stream_pos *pos;
294 FILE *fp;
295
296 pos = g_new0(struct ctf_text_stream_pos, 1);
297
298 switch (flags & O_ACCMODE) {
299 case O_RDWR:
300 if (!path)
301 fp = stdout;
302 else
303 fp = fopen(path, "w");
304 if (!fp)
305 goto error;
306 pos->fp = fp;
307 pos->parent.rw_table = write_dispatch_table;
308 pos->parent.event_cb = ctf_text_write_event;
309 pos->print_names = 0;
310 break;
311 case O_RDONLY:
312 default:
313 fprintf(stdout, "[error] Incorrect open flags.\n");
314 goto error;
315 }
316
317 return &pos->trace_descriptor;
318 error:
319 g_free(pos);
320 return NULL;
321 }
322
323 void ctf_text_close_trace(struct trace_descriptor *td)
324 {
325 struct ctf_text_stream_pos *pos =
326 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
327 fclose(pos->fp);
328 g_free(pos);
329 }
330
331 void __attribute__((constructor)) ctf_text_init(void)
332 {
333 int ret;
334
335 ctf_text_format.name = g_quark_from_static_string("text");
336 ret = bt_register_format(&ctf_text_format);
337 assert(!ret);
338 }
339
340 /* TODO: finalize */
This page took 0.035855 seconds and 3 git commands to generate.