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