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