Print events discarded
[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;
fa709ab2
MD
165 struct ctf_trace *trace = stream->stream_class->trace;
166 struct trace_collection *tc = trace->collection;
167 struct ctf_clock *clock = tc->single_clock;
11ac6674
MD
168
169 ts_nsec = stream->timestamp;
170
171 /* Add offsets */
fa709ab2
MD
172 if (!opt_clock_raw && clock) {
173 ts_sec += clock->offset_s;
174 ts_nsec += clock->offset;
11ac6674
MD
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 }
220seconds:
221 fprintf(pos->fp, "%3" PRIu64 ".%09" PRIu64,
222 ts_sec, ts_nsec);
223
224end:
225 return;
226}
227
31262354
MD
228static
229int ctf_text_write_event(struct stream_pos *ppos,
764af3f4 230 struct ctf_stream *stream)
31262354
MD
231{
232 struct ctf_text_stream_pos *pos =
233 container_of(ppos, struct ctf_text_stream_pos, parent);
764af3f4 234 struct ctf_stream_class *stream_class = stream->stream_class;
fd3382e8 235 int field_nr_saved;
31262354 236 struct ctf_event *event_class;
42dc00b7 237 struct ctf_stream_event *event;
c87a8eb2 238 uint64_t id;
31262354 239 int ret;
8c250d87 240 int dom_print = 0;
31262354 241
c87a8eb2 242 id = stream->event_id;
31262354
MD
243
244 if (id >= stream_class->events_by_id->len) {
3394d22e 245 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
246 return -EINVAL;
247 }
e28d4618
MD
248 event = g_ptr_array_index(stream->events_by_id, id);
249 if (!event) {
3394d22e 250 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
e28d4618
MD
251 return -EINVAL;
252 }
31262354 253 event_class = g_ptr_array_index(stream_class->events_by_id, id);
e28d4618 254 if (!event) {
3394d22e 255 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
31262354
MD
256 return -EINVAL;
257 }
258
5e2eb0ae 259 if (stream->has_timestamp) {
cba1661c 260 set_field_names_print(pos, ITEM_HEADER);
be60c7fb
MD
261 if (pos->print_names)
262 fprintf(pos->fp, "timestamp = ");
263 else
264 fprintf(pos->fp, "[");
11ac6674 265 ctf_text_print_timestamp(pos, stream);
be60c7fb
MD
266 if (!pos->print_names)
267 fprintf(pos->fp, "]");
aa6bffae 268
be60c7fb
MD
269 if (pos->print_names)
270 fprintf(pos->fp, ", ");
271 else
272 fprintf(pos->fp, " ");
273 }
359d7456 274 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
8d8ed9af
MD
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
359d7456 301 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
82662ad4 302 set_field_names_print(pos, ITEM_HEADER);
8c250d87 303 if (pos->print_names) {
359d7456 304 fprintf(pos->fp, "trace = ");
8c250d87 305 }
82662ad4 306 fprintf(pos->fp, "%s", stream_class->trace->path);
82662ad4
MD
307 if (pos->print_names)
308 fprintf(pos->fp, ", ");
309 else
310 fprintf(pos->fp, " ");
311 }
359d7456 312 if ((opt_trace_domain_field && !opt_all_fields) && stream_class->trace->domain[0] != '\0') {
8c250d87
MD
313 set_field_names_print(pos, ITEM_HEADER);
314 if (pos->print_names) {
315 fprintf(pos->fp, "trace:domain = ");
316 }
359d7456 317 fprintf(pos->fp, "%s", stream_class->trace->domain);
8c250d87
MD
318 if (pos->print_names)
319 fprintf(pos->fp, ", ");
320 dom_print = 1;
321 }
359d7456 322 if ((opt_trace_procname_field && !opt_all_fields) && stream_class->trace->procname[0] != '\0') {
8c250d87
MD
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 }
359d7456 329 fprintf(pos->fp, "%s", stream_class->trace->procname);
8c250d87
MD
330 if (pos->print_names)
331 fprintf(pos->fp, ", ");
332 dom_print = 1;
333 }
359d7456 334 if ((opt_trace_vpid_field && !opt_all_fields) && stream_class->trace->vpid[0] != '\0') {
8c250d87
MD
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 }
359d7456 341 fprintf(pos->fp, "%s", stream_class->trace->vpid);
8c250d87
MD
342 if (pos->print_names)
343 fprintf(pos->fp, ", ");
344 dom_print = 1;
345 }
359d7456 346 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel_identifier != 0) {
d86d62f8
MD
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 }
d86d62f8
MD
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 }
8c250d87
MD
360 if (dom_print && !pos->print_names)
361 fprintf(pos->fp, " ");
cba1661c 362 set_field_names_print(pos, ITEM_HEADER);
31262354
MD
363 if (pos->print_names)
364 fprintf(pos->fp, "name = ");
8178fe48
MD
365 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
366 if (pos->print_names)
fd3382e8 367 pos->field_nr++;
8178fe48 368 else
fd3382e8 369 fprintf(pos->fp, ":");
31262354 370
e28d4618
MD
371 /* print cpuid field from packet context */
372 if (stream->stream_packet_context) {
373 if (pos->field_nr++ != 0)
374 fprintf(pos->fp, ",");
cba1661c 375 set_field_names_print(pos, ITEM_SCOPE);
e28d4618
MD
376 if (pos->print_names)
377 fprintf(pos->fp, " stream.packet.context =");
378 field_nr_saved = pos->field_nr;
379 pos->field_nr = 0;
cba1661c 380 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618
MD
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
764af3f4 387 /* Only show the event header in verbose mode */
e28d4618 388 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
389 if (pos->field_nr++ != 0)
390 fprintf(pos->fp, ",");
cba1661c 391 set_field_names_print(pos, ITEM_SCOPE);
31262354 392 if (pos->print_names)
fd3382e8
MD
393 fprintf(pos->fp, " stream.event.header =");
394 field_nr_saved = pos->field_nr;
395 pos->field_nr = 0;
cba1661c 396 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 397 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
398 if (ret)
399 goto error;
fd3382e8 400 pos->field_nr = field_nr_saved;
31262354
MD
401 }
402
403 /* print stream-declared event context */
e28d4618 404 if (stream->stream_event_context) {
fd3382e8
MD
405 if (pos->field_nr++ != 0)
406 fprintf(pos->fp, ",");
cba1661c 407 set_field_names_print(pos, ITEM_SCOPE);
31262354 408 if (pos->print_names)
fd3382e8
MD
409 fprintf(pos->fp, " stream.event.context =");
410 field_nr_saved = pos->field_nr;
411 pos->field_nr = 0;
cba1661c 412 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 413 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
414 if (ret)
415 goto error;
fd3382e8 416 pos->field_nr = field_nr_saved;
31262354
MD
417 }
418
419 /* print event-declared event context */
e28d4618 420 if (event->event_context) {
fd3382e8
MD
421 if (pos->field_nr++ != 0)
422 fprintf(pos->fp, ",");
cba1661c 423 set_field_names_print(pos, ITEM_SCOPE);
31262354 424 if (pos->print_names)
fd3382e8
MD
425 fprintf(pos->fp, " event.context =");
426 field_nr_saved = pos->field_nr;
427 pos->field_nr = 0;
cba1661c 428 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 429 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
430 if (ret)
431 goto error;
fd3382e8 432 pos->field_nr = field_nr_saved;
31262354
MD
433 }
434
435 /* Read and print event payload */
e28d4618 436 if (event->event_fields) {
fd3382e8
MD
437 if (pos->field_nr++ != 0)
438 fprintf(pos->fp, ",");
cba1661c 439 set_field_names_print(pos, ITEM_SCOPE);
31262354 440 if (pos->print_names)
fd3382e8
MD
441 fprintf(pos->fp, " event.fields =");
442 field_nr_saved = pos->field_nr;
443 pos->field_nr = 0;
cba1661c 444 set_field_names_print(pos, ITEM_PAYLOAD);
e28d4618 445 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
446 if (ret)
447 goto error;
fd3382e8 448 pos->field_nr = field_nr_saved;
31262354 449 }
fd3382e8 450 /* newline */
31262354 451 fprintf(pos->fp, "\n");
fd3382e8 452 pos->field_nr = 0;
31262354
MD
453
454 return 0;
455
456error:
3394d22e 457 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
458 return ret;
459}
460
461
8c250d87
MD
462struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
463 const char *path, int flags,
b086c01a 464 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
ae23d232 465 int whence), FILE *metadata_fp)
642ec66d
MD
466{
467 struct ctf_text_stream_pos *pos;
468 FILE *fp;
469
470 pos = g_new0(struct ctf_text_stream_pos, 1);
471
8d8ed9af 472 pos->last_timestamp = -1ULL;
642ec66d 473 switch (flags & O_ACCMODE) {
989c73bc 474 case O_RDWR:
b61922b5 475 if (!path)
6cf7957b
MD
476 fp = stdout;
477 else
478 fp = fopen(path, "w");
642ec66d
MD
479 if (!fp)
480 goto error;
481 pos->fp = fp;
482 pos->parent.rw_table = write_dispatch_table;
31262354 483 pos->parent.event_cb = ctf_text_write_event;
cba1661c 484 pos->print_names = 0;
642ec66d
MD
485 break;
486 case O_RDONLY:
487 default:
3394d22e 488 fprintf(stderr, "[error] Incorrect open flags.\n");
642ec66d
MD
489 goto error;
490 }
491
492 return &pos->trace_descriptor;
493error:
494 g_free(pos);
495 return NULL;
496}
497
498void 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
506void __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.073745 seconds and 4 git commands to generate.