cac080e951da48da8efa6cfa5b4cdca1cec84f04
[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 #define NSEC_PER_SEC 1000000000ULL
38
39 int opt_all_field_names,
40 opt_scope_field_names,
41 opt_header_field_names,
42 opt_context_field_names,
43 opt_payload_field_names,
44 opt_trace_name,
45 opt_trace_domain,
46 opt_trace_procname,
47 opt_trace_vpid,
48 opt_loglevel,
49 opt_delta = 1;
50
51 enum field_item {
52 ITEM_SCOPE,
53 ITEM_HEADER,
54 ITEM_CONTEXT,
55 ITEM_PAYLOAD,
56 };
57
58 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
59 const char *path, int flags,
60 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
61 int whence), FILE *metadata_fp);
62 void ctf_text_close_trace(struct trace_descriptor *descriptor);
63
64 static
65 rw_dispatch write_dispatch_table[] = {
66 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
67 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
68 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
69 [ CTF_TYPE_STRING ] = ctf_text_string_write,
70 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
71 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
72 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
73 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
74 };
75
76 static
77 struct format ctf_text_format = {
78 .open_trace = ctf_text_open_trace,
79 .close_trace = ctf_text_close_trace,
80 };
81
82 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
83 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
84 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
85 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
86 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
87
88 static
89 void __attribute__((constructor)) init_quarks(void)
90 {
91 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
92 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
93 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
94 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
95 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
96 }
97
98 int print_field(struct definition *definition)
99 {
100 /* Print all fields in verbose mode */
101 if (babeltrace_verbose)
102 return 1;
103
104 /* Filter out part of the packet context */
105 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
106 return 0;
107 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
108 return 0;
109 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
110 return 0;
111 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
112 return 0;
113 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
114 return 0;
115
116 return 1;
117 }
118
119 static
120 void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
121 {
122 switch (item) {
123 case ITEM_SCOPE:
124 if (opt_all_field_names || opt_scope_field_names)
125 pos->print_names = 1;
126 else
127 pos->print_names = 0;
128 break;
129 case ITEM_HEADER:
130 if (opt_all_field_names || opt_header_field_names)
131 pos->print_names = 1;
132 else
133 pos->print_names = 0;
134 break;
135 case ITEM_CONTEXT:
136 if (opt_all_field_names || opt_context_field_names)
137 pos->print_names = 1;
138 else
139 pos->print_names = 0;
140 break;
141 case ITEM_PAYLOAD:
142 if (opt_all_field_names || opt_payload_field_names)
143 pos->print_names = 1;
144 else
145 pos->print_names = 0;
146
147 break;
148 default:
149 assert(0);
150 }
151 }
152
153 static
154 int ctf_text_write_event(struct stream_pos *ppos,
155 struct ctf_stream *stream)
156 {
157 struct ctf_text_stream_pos *pos =
158 container_of(ppos, struct ctf_text_stream_pos, parent);
159 struct ctf_stream_class *stream_class = stream->stream_class;
160 int field_nr_saved;
161 struct ctf_event *event_class;
162 struct ctf_stream_event *event;
163 uint64_t id;
164 int ret;
165 int dom_print = 0;
166
167 id = stream->event_id;
168
169 if (id >= stream_class->events_by_id->len) {
170 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
171 return -EINVAL;
172 }
173 event = g_ptr_array_index(stream->events_by_id, id);
174 if (!event) {
175 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
176 return -EINVAL;
177 }
178 event_class = g_ptr_array_index(stream_class->events_by_id, id);
179 if (!event) {
180 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
181 return -EINVAL;
182 }
183
184 if (stream->has_timestamp) {
185 uint64_t ts_sec, ts_nsec;
186
187 ts_sec = stream->timestamp / NSEC_PER_SEC;
188 ts_nsec = stream->timestamp % NSEC_PER_SEC;
189 set_field_names_print(pos, ITEM_HEADER);
190 if (pos->print_names)
191 fprintf(pos->fp, "timestamp = ");
192 else
193 fprintf(pos->fp, "[");
194 fprintf(pos->fp, "%3" PRIu64 ".%09" PRIu64,
195 ts_sec, ts_nsec);
196 if (!pos->print_names)
197 fprintf(pos->fp, "]");
198
199 if (pos->print_names)
200 fprintf(pos->fp, ", ");
201 else
202 fprintf(pos->fp, " ");
203 }
204 if (opt_delta && stream->has_timestamp) {
205 uint64_t delta, delta_sec, delta_nsec;
206
207 set_field_names_print(pos, ITEM_HEADER);
208 if (pos->print_names)
209 fprintf(pos->fp, "delta = ");
210 else
211 fprintf(pos->fp, "(");
212 if (pos->last_timestamp != -1ULL) {
213 delta = stream->timestamp - pos->last_timestamp;
214 delta_sec = delta / NSEC_PER_SEC;
215 delta_nsec = delta % NSEC_PER_SEC;
216 fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64,
217 delta_sec, delta_nsec);
218 } else {
219 fprintf(pos->fp, "+?.?????????");
220 }
221 if (!pos->print_names)
222 fprintf(pos->fp, ")");
223
224 if (pos->print_names)
225 fprintf(pos->fp, ", ");
226 else
227 fprintf(pos->fp, " ");
228 pos->last_timestamp = stream->timestamp;
229 }
230
231 if ((opt_trace_name || opt_all_field_names) && stream_class->trace->path[0] != '\0') {
232 set_field_names_print(pos, ITEM_HEADER);
233 if (pos->print_names) {
234 if (opt_trace_name || opt_all_field_names)
235 fprintf(pos->fp, "trace = ");
236 }
237
238 fprintf(pos->fp, "%s", stream_class->trace->path);
239 if (pos->print_names)
240 fprintf(pos->fp, ", ");
241 else
242 fprintf(pos->fp, " ");
243 }
244 if ((opt_trace_domain) && stream_class->trace->domain[0] != '\0') {
245 set_field_names_print(pos, ITEM_HEADER);
246 if (pos->print_names) {
247 fprintf(pos->fp, "trace:domain = ");
248 }
249 if (opt_trace_domain)
250 fprintf(pos->fp, "%s", stream_class->trace->domain);
251 if (pos->print_names)
252 fprintf(pos->fp, ", ");
253 dom_print = 1;
254 }
255 if ((opt_trace_procname) && stream_class->trace->procname[0] != '\0') {
256 set_field_names_print(pos, ITEM_HEADER);
257 if (pos->print_names) {
258 fprintf(pos->fp, "trace:procname = ");
259 } else if (dom_print) {
260 fprintf(pos->fp, ":");
261 }
262
263 if (opt_trace_procname)
264 fprintf(pos->fp, "%s", stream_class->trace->procname);
265 if (pos->print_names)
266 fprintf(pos->fp, ", ");
267 dom_print = 1;
268 }
269 if ((opt_trace_vpid) && stream_class->trace->vpid[0] != '\0') {
270 set_field_names_print(pos, ITEM_HEADER);
271 if (pos->print_names) {
272 fprintf(pos->fp, "trace:vpid = ");
273 } else if (dom_print) {
274 fprintf(pos->fp, ":");
275 }
276
277 if (opt_trace_vpid)
278 fprintf(pos->fp, "%s", stream_class->trace->vpid);
279 if (pos->print_names)
280 fprintf(pos->fp, ", ");
281 dom_print = 1;
282 }
283 if ((opt_loglevel || opt_all_field_names) && event_class->loglevel_identifier != 0) {
284 set_field_names_print(pos, ITEM_HEADER);
285 if (pos->print_names) {
286 fprintf(pos->fp, "loglevel = ");
287 } else if (dom_print) {
288 fprintf(pos->fp, ":");
289 }
290
291 fprintf(pos->fp, "%s (%lld)",
292 g_quark_to_string(event_class->loglevel_identifier),
293 (long long) event_class->loglevel_value);
294 if (pos->print_names)
295 fprintf(pos->fp, ", ");
296 dom_print = 1;
297 }
298 if (dom_print && !pos->print_names)
299 fprintf(pos->fp, " ");
300 set_field_names_print(pos, ITEM_HEADER);
301 if (pos->print_names)
302 fprintf(pos->fp, "name = ");
303 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
304 if (pos->print_names)
305 pos->field_nr++;
306 else
307 fprintf(pos->fp, ":");
308
309 /* print cpuid field from packet context */
310 if (stream->stream_packet_context) {
311 if (pos->field_nr++ != 0)
312 fprintf(pos->fp, ",");
313 set_field_names_print(pos, ITEM_SCOPE);
314 if (pos->print_names)
315 fprintf(pos->fp, " stream.packet.context =");
316 field_nr_saved = pos->field_nr;
317 pos->field_nr = 0;
318 set_field_names_print(pos, ITEM_CONTEXT);
319 ret = generic_rw(ppos, &stream->stream_packet_context->p);
320 if (ret)
321 goto error;
322 pos->field_nr = field_nr_saved;
323 }
324
325 /* Only show the event header in verbose mode */
326 if (babeltrace_verbose && stream->stream_event_header) {
327 if (pos->field_nr++ != 0)
328 fprintf(pos->fp, ",");
329 set_field_names_print(pos, ITEM_SCOPE);
330 if (pos->print_names)
331 fprintf(pos->fp, " stream.event.header =");
332 field_nr_saved = pos->field_nr;
333 pos->field_nr = 0;
334 set_field_names_print(pos, ITEM_CONTEXT);
335 ret = generic_rw(ppos, &stream->stream_event_header->p);
336 if (ret)
337 goto error;
338 pos->field_nr = field_nr_saved;
339 }
340
341 /* print stream-declared event context */
342 if (stream->stream_event_context) {
343 if (pos->field_nr++ != 0)
344 fprintf(pos->fp, ",");
345 set_field_names_print(pos, ITEM_SCOPE);
346 if (pos->print_names)
347 fprintf(pos->fp, " stream.event.context =");
348 field_nr_saved = pos->field_nr;
349 pos->field_nr = 0;
350 set_field_names_print(pos, ITEM_CONTEXT);
351 ret = generic_rw(ppos, &stream->stream_event_context->p);
352 if (ret)
353 goto error;
354 pos->field_nr = field_nr_saved;
355 }
356
357 /* print event-declared event context */
358 if (event->event_context) {
359 if (pos->field_nr++ != 0)
360 fprintf(pos->fp, ",");
361 set_field_names_print(pos, ITEM_SCOPE);
362 if (pos->print_names)
363 fprintf(pos->fp, " event.context =");
364 field_nr_saved = pos->field_nr;
365 pos->field_nr = 0;
366 set_field_names_print(pos, ITEM_CONTEXT);
367 ret = generic_rw(ppos, &event->event_context->p);
368 if (ret)
369 goto error;
370 pos->field_nr = field_nr_saved;
371 }
372
373 /* Read and print event payload */
374 if (event->event_fields) {
375 if (pos->field_nr++ != 0)
376 fprintf(pos->fp, ",");
377 set_field_names_print(pos, ITEM_SCOPE);
378 if (pos->print_names)
379 fprintf(pos->fp, " event.fields =");
380 field_nr_saved = pos->field_nr;
381 pos->field_nr = 0;
382 set_field_names_print(pos, ITEM_PAYLOAD);
383 ret = generic_rw(ppos, &event->event_fields->p);
384 if (ret)
385 goto error;
386 pos->field_nr = field_nr_saved;
387 }
388 /* newline */
389 fprintf(pos->fp, "\n");
390 pos->field_nr = 0;
391
392 return 0;
393
394 error:
395 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
396 return ret;
397 }
398
399
400 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
401 const char *path, int flags,
402 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
403 int whence), FILE *metadata_fp)
404 {
405 struct ctf_text_stream_pos *pos;
406 FILE *fp;
407
408 pos = g_new0(struct ctf_text_stream_pos, 1);
409
410 pos->last_timestamp = -1ULL;
411 switch (flags & O_ACCMODE) {
412 case O_RDWR:
413 if (!path)
414 fp = stdout;
415 else
416 fp = fopen(path, "w");
417 if (!fp)
418 goto error;
419 pos->fp = fp;
420 pos->parent.rw_table = write_dispatch_table;
421 pos->parent.event_cb = ctf_text_write_event;
422 pos->print_names = 0;
423 break;
424 case O_RDONLY:
425 default:
426 fprintf(stderr, "[error] Incorrect open flags.\n");
427 goto error;
428 }
429
430 return &pos->trace_descriptor;
431 error:
432 g_free(pos);
433 return NULL;
434 }
435
436 void ctf_text_close_trace(struct trace_descriptor *td)
437 {
438 struct ctf_text_stream_pos *pos =
439 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
440 fclose(pos->fp);
441 g_free(pos);
442 }
443
444 void __attribute__((constructor)) ctf_text_init(void)
445 {
446 int ret;
447
448 ctf_text_format.name = g_quark_from_static_string("text");
449 ret = bt_register_format(&ctf_text_format);
450 assert(!ret);
451 }
452
453 /* TODO: finalize */
This page took 0.036822 seconds and 3 git commands to generate.