Print time range during which events have been dropped
[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(FILE *fp, struct ctf_text_stream_pos *pos,
162 struct ctf_stream *stream,
163 uint64_t timestamp)
164 {
165 uint64_t ts_sec = 0, ts_nsec;
166 struct ctf_trace *trace = stream->stream_class->trace;
167 struct trace_collection *tc = trace->collection;
168 struct ctf_clock *clock = tc->single_clock;
169
170 ts_nsec = timestamp;
171
172 /* Add offsets */
173 if (!opt_clock_raw && clock) {
174 ts_sec += clock->offset_s;
175 ts_nsec += clock->offset;
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 }
214 fprintf(fp, "%s", timestr);
215 }
216 /* Print time in HH:MM:SS.ns */
217 fprintf(fp, "%02d:%02d:%02d.%09" PRIu64,
218 tm.tm_hour, tm.tm_min, tm.tm_sec, ts_nsec);
219 goto end;
220 }
221 seconds:
222 fprintf(fp, "%3" PRIu64 ".%09" PRIu64,
223 ts_sec, ts_nsec);
224
225 end:
226 return;
227 }
228
229 static
230 int ctf_text_write_event(struct stream_pos *ppos,
231 struct ctf_stream *stream)
232 {
233 struct ctf_text_stream_pos *pos =
234 container_of(ppos, struct ctf_text_stream_pos, parent);
235 struct ctf_stream_class *stream_class = stream->stream_class;
236 int field_nr_saved;
237 struct ctf_event *event_class;
238 struct ctf_stream_event *event;
239 uint64_t id;
240 int ret;
241 int dom_print = 0;
242
243 id = stream->event_id;
244
245 if (id >= stream_class->events_by_id->len) {
246 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
247 return -EINVAL;
248 }
249 event = g_ptr_array_index(stream->events_by_id, id);
250 if (!event) {
251 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
252 return -EINVAL;
253 }
254 event_class = g_ptr_array_index(stream_class->events_by_id, id);
255 if (!event) {
256 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
257 return -EINVAL;
258 }
259
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 [");
267 ctf_text_print_timestamp(stderr, pos, stream, stream->timestamp);
268 fprintf(stderr, "]. You should consider increasing the buffer size.\n");
269 fflush(stderr);
270 stream->events_discarded = 0;
271 }
272
273 if (stream->has_timestamp) {
274 set_field_names_print(pos, ITEM_HEADER);
275 if (pos->print_names)
276 fprintf(pos->fp, "timestamp = ");
277 else
278 fprintf(pos->fp, "[");
279 ctf_text_print_timestamp(pos->fp, pos, stream, stream->timestamp);
280 if (!pos->print_names)
281 fprintf(pos->fp, "]");
282
283 if (pos->print_names)
284 fprintf(pos->fp, ", ");
285 else
286 fprintf(pos->fp, " ");
287 }
288 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
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
315 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
316 set_field_names_print(pos, ITEM_HEADER);
317 if (pos->print_names) {
318 fprintf(pos->fp, "trace = ");
319 }
320 fprintf(pos->fp, "%s", stream_class->trace->path);
321 if (pos->print_names)
322 fprintf(pos->fp, ", ");
323 else
324 fprintf(pos->fp, " ");
325 }
326 if ((opt_trace_domain_field && !opt_all_fields) && stream_class->trace->domain[0] != '\0') {
327 set_field_names_print(pos, ITEM_HEADER);
328 if (pos->print_names) {
329 fprintf(pos->fp, "trace:domain = ");
330 }
331 fprintf(pos->fp, "%s", stream_class->trace->domain);
332 if (pos->print_names)
333 fprintf(pos->fp, ", ");
334 dom_print = 1;
335 }
336 if ((opt_trace_procname_field && !opt_all_fields) && stream_class->trace->procname[0] != '\0') {
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 }
343 fprintf(pos->fp, "%s", stream_class->trace->procname);
344 if (pos->print_names)
345 fprintf(pos->fp, ", ");
346 dom_print = 1;
347 }
348 if ((opt_trace_vpid_field && !opt_all_fields) && stream_class->trace->vpid[0] != '\0') {
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 }
355 fprintf(pos->fp, "%s", stream_class->trace->vpid);
356 if (pos->print_names)
357 fprintf(pos->fp, ", ");
358 dom_print = 1;
359 }
360 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel_identifier != 0) {
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 }
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 }
374 if (dom_print && !pos->print_names)
375 fprintf(pos->fp, " ");
376 set_field_names_print(pos, ITEM_HEADER);
377 if (pos->print_names)
378 fprintf(pos->fp, "name = ");
379 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
380 if (pos->print_names)
381 pos->field_nr++;
382 else
383 fprintf(pos->fp, ":");
384
385 /* print cpuid field from packet context */
386 if (stream->stream_packet_context) {
387 if (pos->field_nr++ != 0)
388 fprintf(pos->fp, ",");
389 set_field_names_print(pos, ITEM_SCOPE);
390 if (pos->print_names)
391 fprintf(pos->fp, " stream.packet.context =");
392 field_nr_saved = pos->field_nr;
393 pos->field_nr = 0;
394 set_field_names_print(pos, ITEM_CONTEXT);
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
401 /* Only show the event header in verbose mode */
402 if (babeltrace_verbose && stream->stream_event_header) {
403 if (pos->field_nr++ != 0)
404 fprintf(pos->fp, ",");
405 set_field_names_print(pos, ITEM_SCOPE);
406 if (pos->print_names)
407 fprintf(pos->fp, " stream.event.header =");
408 field_nr_saved = pos->field_nr;
409 pos->field_nr = 0;
410 set_field_names_print(pos, ITEM_CONTEXT);
411 ret = generic_rw(ppos, &stream->stream_event_header->p);
412 if (ret)
413 goto error;
414 pos->field_nr = field_nr_saved;
415 }
416
417 /* print stream-declared event context */
418 if (stream->stream_event_context) {
419 if (pos->field_nr++ != 0)
420 fprintf(pos->fp, ",");
421 set_field_names_print(pos, ITEM_SCOPE);
422 if (pos->print_names)
423 fprintf(pos->fp, " stream.event.context =");
424 field_nr_saved = pos->field_nr;
425 pos->field_nr = 0;
426 set_field_names_print(pos, ITEM_CONTEXT);
427 ret = generic_rw(ppos, &stream->stream_event_context->p);
428 if (ret)
429 goto error;
430 pos->field_nr = field_nr_saved;
431 }
432
433 /* print event-declared event context */
434 if (event->event_context) {
435 if (pos->field_nr++ != 0)
436 fprintf(pos->fp, ",");
437 set_field_names_print(pos, ITEM_SCOPE);
438 if (pos->print_names)
439 fprintf(pos->fp, " event.context =");
440 field_nr_saved = pos->field_nr;
441 pos->field_nr = 0;
442 set_field_names_print(pos, ITEM_CONTEXT);
443 ret = generic_rw(ppos, &event->event_context->p);
444 if (ret)
445 goto error;
446 pos->field_nr = field_nr_saved;
447 }
448
449 /* Read and print event payload */
450 if (event->event_fields) {
451 if (pos->field_nr++ != 0)
452 fprintf(pos->fp, ",");
453 set_field_names_print(pos, ITEM_SCOPE);
454 if (pos->print_names)
455 fprintf(pos->fp, " event.fields =");
456 field_nr_saved = pos->field_nr;
457 pos->field_nr = 0;
458 set_field_names_print(pos, ITEM_PAYLOAD);
459 ret = generic_rw(ppos, &event->event_fields->p);
460 if (ret)
461 goto error;
462 pos->field_nr = field_nr_saved;
463 }
464 /* newline */
465 fprintf(pos->fp, "\n");
466 pos->field_nr = 0;
467
468 return 0;
469
470 error:
471 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
472 return ret;
473 }
474
475
476 struct trace_descriptor *ctf_text_open_trace(const char *collection_path,
477 const char *path, int flags,
478 void (*move_pos_slow)(struct ctf_stream_pos *pos, size_t offset,
479 int whence), FILE *metadata_fp)
480 {
481 struct ctf_text_stream_pos *pos;
482 FILE *fp;
483
484 pos = g_new0(struct ctf_text_stream_pos, 1);
485
486 pos->last_timestamp = -1ULL;
487 switch (flags & O_ACCMODE) {
488 case O_RDWR:
489 if (!path)
490 fp = stdout;
491 else
492 fp = fopen(path, "w");
493 if (!fp)
494 goto error;
495 pos->fp = fp;
496 pos->parent.rw_table = write_dispatch_table;
497 pos->parent.event_cb = ctf_text_write_event;
498 pos->print_names = 0;
499 break;
500 case O_RDONLY:
501 default:
502 fprintf(stderr, "[error] Incorrect open flags.\n");
503 goto error;
504 }
505
506 return &pos->trace_descriptor;
507 error:
508 g_free(pos);
509 return NULL;
510 }
511
512 void 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
520 void __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.055979 seconds and 4 git commands to generate.