Warn user of clock offsets do not match
[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
MD
160static
161void ctf_text_print_timestamp(struct ctf_text_stream_pos *pos,
162 struct ctf_stream *stream)
163{
164 uint64_t ts_sec = 0, ts_nsec;
165 //need to get the clock offset at trace collection level. TODO
166 //struct ctf_trace *trace = stream->stream_class->trace;
167
168 ts_nsec = stream->timestamp;
169
170 /* Add offsets */
171 if (!opt_clock_raw) {
172 //ts_sec += pos->clock_offset_s;
173 //ts_nsec += pos->clock_offset;
174 }
175 ts_sec += opt_clock_offset;
176
177 ts_sec += ts_nsec / NSEC_PER_SEC;
178 ts_nsec = ts_nsec % NSEC_PER_SEC;
179
180 if (!opt_clock_seconds) {
181 struct tm tm;
182 time_t time_s = (time_t) ts_sec;
183
184 if (!opt_clock_gmt) {
185 struct tm *res;
186
187 res = localtime_r(&time_s, &tm);
188 if (!res) {
189 fprintf(stderr, "[warning] Unable to get localtime.\n");
190 goto seconds;
191 }
192 } else {
193 struct tm *res;
194
195 res = gmtime_r(&time_s, &tm);
196 if (!res) {
197 fprintf(stderr, "[warning] Unable to get gmtime.\n");
198 goto seconds;
199 }
200 }
201 if (opt_clock_date) {
202 char timestr[26];
203 size_t res;
204
205 /* Print date and time */
206 res = strftime(timestr, sizeof(timestr),
207 "%F ", &tm);
208 if (!res) {
209 fprintf(stderr, "[warning] Unable to print ascii time.\n");
210 goto seconds;
211 }
212 fprintf(pos->fp, "%s", timestr);
213 }
214 /* Print time in HH:MM:SS.ns */
215 fprintf(pos->fp, "%02d:%02d:%02d.%09" PRIu64,
216 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
217 goto end;
218 }
219seconds:
220 fprintf(pos->fp, "%3" PRIu64 ".%09" PRIu64,
221 ts_sec, ts_nsec);
222
223end:
224 return;
225}
226
31262354
MD
227static
228int ctf_text_write_event(struct stream_pos *ppos,
764af3f4 229 struct ctf_stream *stream)
31262354
MD
230{
231 struct ctf_text_stream_pos *pos =
232 container_of(ppos, struct ctf_text_stream_pos, parent);
764af3f4 233 struct ctf_stream_class *stream_class = stream->stream_class;
fd3382e8 234 int field_nr_saved;
31262354 235 struct ctf_event *event_class;
42dc00b7 236 struct ctf_stream_event *event;
c87a8eb2 237 uint64_t id;
31262354 238 int ret;
8c250d87 239 int dom_print = 0;
31262354 240
c87a8eb2 241 id = stream->event_id;
31262354
MD
242
243 if (id >= stream_class->events_by_id->len) {
3394d22e 244 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
245 return -EINVAL;
246 }
e28d4618
MD
247 event = g_ptr_array_index(stream->events_by_id, id);
248 if (!event) {
3394d22e 249 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
e28d4618
MD
250 return -EINVAL;
251 }
31262354 252 event_class = g_ptr_array_index(stream_class->events_by_id, id);
e28d4618 253 if (!event) {
3394d22e 254 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
255 return -EINVAL;
256 }
257
5e2eb0ae 258 if (stream->has_timestamp) {
cba1661c 259 set_field_names_print(pos, ITEM_HEADER);
be60c7fb
MD
260 if (pos->print_names)
261 fprintf(pos->fp, "timestamp = ");
262 else
263 fprintf(pos->fp, "[");
11ac6674 264 ctf_text_print_timestamp(pos, stream);
be60c7fb
MD
265 if (!pos->print_names)
266 fprintf(pos->fp, "]");
aa6bffae 267
be60c7fb
MD
268 if (pos->print_names)
269 fprintf(pos->fp, ", ");
270 else
271 fprintf(pos->fp, " ");
272 }
359d7456 273 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
8d8ed9af
MD
274 uint64_t delta, delta_sec, delta_nsec;
275
276 set_field_names_print(pos, ITEM_HEADER);
277 if (pos->print_names)
278 fprintf(pos->fp, "delta = ");
279 else
280 fprintf(pos->fp, "(");
281 if (pos->last_timestamp != -1ULL) {
282 delta = stream->timestamp - pos->last_timestamp;
283 delta_sec = delta / NSEC_PER_SEC;
284 delta_nsec = delta % NSEC_PER_SEC;
285 fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64,
286 delta_sec, delta_nsec);
287 } else {
288 fprintf(pos->fp, "+?.?????????");
289 }
290 if (!pos->print_names)
291 fprintf(pos->fp, ")");
292
293 if (pos->print_names)
294 fprintf(pos->fp, ", ");
295 else
296 fprintf(pos->fp, " ");
297 pos->last_timestamp = stream->timestamp;
298 }
299
359d7456 300 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
82662ad4 301 set_field_names_print(pos, ITEM_HEADER);
8c250d87 302 if (pos->print_names) {
359d7456 303 fprintf(pos->fp, "trace = ");
8c250d87 304 }
82662ad4 305 fprintf(pos->fp, "%s", stream_class->trace->path);
82662ad4
MD
306 if (pos->print_names)
307 fprintf(pos->fp, ", ");
308 else
309 fprintf(pos->fp, " ");
310 }
359d7456 311 if ((opt_trace_domain_field && !opt_all_fields) && stream_class->trace->domain[0] != '\0') {
8c250d87
MD
312 set_field_names_print(pos, ITEM_HEADER);
313 if (pos->print_names) {
314 fprintf(pos->fp, "trace:domain = ");
315 }
359d7456 316 fprintf(pos->fp, "%s", stream_class->trace->domain);
8c250d87
MD
317 if (pos->print_names)
318 fprintf(pos->fp, ", ");
319 dom_print = 1;
320 }
359d7456 321 if ((opt_trace_procname_field && !opt_all_fields) && stream_class->trace->procname[0] != '\0') {
8c250d87
MD
322 set_field_names_print(pos, ITEM_HEADER);
323 if (pos->print_names) {
324 fprintf(pos->fp, "trace:procname = ");
325 } else if (dom_print) {
326 fprintf(pos->fp, ":");
327 }
359d7456 328 fprintf(pos->fp, "%s", stream_class->trace->procname);
8c250d87
MD
329 if (pos->print_names)
330 fprintf(pos->fp, ", ");
331 dom_print = 1;
332 }
359d7456 333 if ((opt_trace_vpid_field && !opt_all_fields) && stream_class->trace->vpid[0] != '\0') {
8c250d87
MD
334 set_field_names_print(pos, ITEM_HEADER);
335 if (pos->print_names) {
336 fprintf(pos->fp, "trace:vpid = ");
337 } else if (dom_print) {
338 fprintf(pos->fp, ":");
339 }
359d7456 340 fprintf(pos->fp, "%s", stream_class->trace->vpid);
8c250d87
MD
341 if (pos->print_names)
342 fprintf(pos->fp, ", ");
343 dom_print = 1;
344 }
359d7456 345 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel_identifier != 0) {
d86d62f8
MD
346 set_field_names_print(pos, ITEM_HEADER);
347 if (pos->print_names) {
348 fprintf(pos->fp, "loglevel = ");
349 } else if (dom_print) {
350 fprintf(pos->fp, ":");
351 }
d86d62f8
MD
352 fprintf(pos->fp, "%s (%lld)",
353 g_quark_to_string(event_class->loglevel_identifier),
354 (long long) event_class->loglevel_value);
355 if (pos->print_names)
356 fprintf(pos->fp, ", ");
357 dom_print = 1;
358 }
8c250d87
MD
359 if (dom_print && !pos->print_names)
360 fprintf(pos->fp, " ");
cba1661c 361 set_field_names_print(pos, ITEM_HEADER);
31262354
MD
362 if (pos->print_names)
363 fprintf(pos->fp, "name = ");
8178fe48
MD
364 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
365 if (pos->print_names)
fd3382e8 366 pos->field_nr++;
8178fe48 367 else
fd3382e8 368 fprintf(pos->fp, ":");
31262354 369
e28d4618
MD
370 /* print cpuid field from packet context */
371 if (stream->stream_packet_context) {
372 if (pos->field_nr++ != 0)
373 fprintf(pos->fp, ",");
cba1661c 374 set_field_names_print(pos, ITEM_SCOPE);
e28d4618
MD
375 if (pos->print_names)
376 fprintf(pos->fp, " stream.packet.context =");
377 field_nr_saved = pos->field_nr;
378 pos->field_nr = 0;
cba1661c 379 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618
MD
380 ret = generic_rw(ppos, &stream->stream_packet_context->p);
381 if (ret)
382 goto error;
383 pos->field_nr = field_nr_saved;
384 }
385
764af3f4 386 /* Only show the event header in verbose mode */
e28d4618 387 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
388 if (pos->field_nr++ != 0)
389 fprintf(pos->fp, ",");
cba1661c 390 set_field_names_print(pos, ITEM_SCOPE);
31262354 391 if (pos->print_names)
fd3382e8
MD
392 fprintf(pos->fp, " stream.event.header =");
393 field_nr_saved = pos->field_nr;
394 pos->field_nr = 0;
cba1661c 395 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 396 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
397 if (ret)
398 goto error;
fd3382e8 399 pos->field_nr = field_nr_saved;
31262354
MD
400 }
401
402 /* print stream-declared event context */
e28d4618 403 if (stream->stream_event_context) {
fd3382e8
MD
404 if (pos->field_nr++ != 0)
405 fprintf(pos->fp, ",");
cba1661c 406 set_field_names_print(pos, ITEM_SCOPE);
31262354 407 if (pos->print_names)
fd3382e8
MD
408 fprintf(pos->fp, " stream.event.context =");
409 field_nr_saved = pos->field_nr;
410 pos->field_nr = 0;
cba1661c 411 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 412 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
413 if (ret)
414 goto error;
fd3382e8 415 pos->field_nr = field_nr_saved;
31262354
MD
416 }
417
418 /* print event-declared event context */
e28d4618 419 if (event->event_context) {
fd3382e8
MD
420 if (pos->field_nr++ != 0)
421 fprintf(pos->fp, ",");
cba1661c 422 set_field_names_print(pos, ITEM_SCOPE);
31262354 423 if (pos->print_names)
fd3382e8
MD
424 fprintf(pos->fp, " event.context =");
425 field_nr_saved = pos->field_nr;
426 pos->field_nr = 0;
cba1661c 427 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 428 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
429 if (ret)
430 goto error;
fd3382e8 431 pos->field_nr = field_nr_saved;
31262354
MD
432 }
433
434 /* Read and print event payload */
e28d4618 435 if (event->event_fields) {
fd3382e8
MD
436 if (pos->field_nr++ != 0)
437 fprintf(pos->fp, ",");
cba1661c 438 set_field_names_print(pos, ITEM_SCOPE);
31262354 439 if (pos->print_names)
fd3382e8
MD
440 fprintf(pos->fp, " event.fields =");
441 field_nr_saved = pos->field_nr;
442 pos->field_nr = 0;
cba1661c 443 set_field_names_print(pos, ITEM_PAYLOAD);
e28d4618 444 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
445 if (ret)
446 goto error;
fd3382e8 447 pos->field_nr = field_nr_saved;
31262354 448 }
fd3382e8 449 /* newline */
31262354 450 fprintf(pos->fp, "\n");
fd3382e8 451 pos->field_nr = 0;
31262354
MD
452
453 return 0;
454
455error:
3394d22e 456 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
457 return ret;
458}
459
460
8c250d87
MD
461struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
462 const char *path, int flags,
b086c01a 463 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 464 int whence), FILE *metadata_fp)
642ec66d
MD
465{
466 struct ctf_text_stream_pos *pos;
467 FILE *fp;
468
469 pos = g_new0(struct ctf_text_stream_pos, 1);
470
8d8ed9af 471 pos->last_timestamp = -1ULL;
642ec66d 472 switch (flags & O_ACCMODE) {
989c73bc 473 case O_RDWR:
b61922b5 474 if (!path)
6cf7957b
MD
475 fp = stdout;
476 else
477 fp = fopen(path, "w");
642ec66d
MD
478 if (!fp)
479 goto error;
480 pos->fp = fp;
481 pos->parent.rw_table = write_dispatch_table;
31262354 482 pos->parent.event_cb = ctf_text_write_event;
cba1661c 483 pos->print_names = 0;
642ec66d
MD
484 break;
485 case O_RDONLY:
486 default:
3394d22e 487 fprintf(stderr, "[error] Incorrect open flags.\n");
642ec66d
MD
488 goto error;
489 }
490
491 return &pos->trace_descriptor;
492error:
493 g_free(pos);
494 return NULL;
495}
496
497void ctf_text_close_trace(struct trace_descriptor *td)
498{
499 struct ctf_text_stream_pos *pos =
500 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
501 fclose(pos->fp);
502 g_free(pos);
503}
504
505void __attribute__((constructor)) ctf_text_init(void)
506{
507 int ret;
508
509 ctf_text_format.name = g_quark_from_static_string("text");
510 ret = bt_register_format(&ctf_text_format);
511 assert(!ret);
512}
513
514/* TODO: finalize */
This page took 0.0474 seconds and 4 git commands to generate.