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