Change default printout to add host, process names and vpid
[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>
e4195791 25#include <babeltrace/ctf/events-internal.h>
642ec66d 26#include <inttypes.h>
642ec66d
MD
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,
32cfb8ad 49 opt_trace_hostname_field,
c3815874 50 opt_trace_default_fields = 1,
359d7456 51 opt_loglevel_field,
7d97fad9 52 opt_delta_field = 1;
cba1661c
MD
53
54enum field_item {
55 ITEM_SCOPE,
56 ITEM_HEADER,
57 ITEM_CONTEXT,
58 ITEM_PAYLOAD,
59};
b88d6e85 60
39535929
MD
61enum bt_loglevel {
62 BT_LOGLEVEL_EMERG = 0,
63 BT_LOGLEVEL_ALERT = 1,
64 BT_LOGLEVEL_CRIT = 2,
65 BT_LOGLEVEL_ERR = 3,
66 BT_LOGLEVEL_WARNING = 4,
67 BT_LOGLEVEL_NOTICE = 5,
68 BT_LOGLEVEL_INFO = 6,
69 BT_LOGLEVEL_DEBUG_SYSTEM = 7,
70 BT_LOGLEVEL_DEBUG_PROGRAM = 8,
71 BT_LOGLEVEL_DEBUG_PROCESS = 9,
72 BT_LOGLEVEL_DEBUG_MODULE = 10,
73 BT_LOGLEVEL_DEBUG_UNIT = 11,
74 BT_LOGLEVEL_DEBUG_FUNCTION = 12,
75 BT_LOGLEVEL_DEBUG_LINE = 13,
76 BT_LOGLEVEL_DEBUG = 14,
77};
78
79
5b80ddfb 80struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
20d0dcf9 81 void (*packet_seek)(struct stream_pos *pos, size_t index,
ae23d232 82 int whence), FILE *metadata_fp);
642ec66d
MD
83void ctf_text_close_trace(struct trace_descriptor *descriptor);
84
642ec66d
MD
85static
86rw_dispatch write_dispatch_table[] = {
87 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
88 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
89 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
90 [ CTF_TYPE_STRING ] = ctf_text_string_write,
91 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
92 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
93 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
94 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
95};
96
97static
98struct format ctf_text_format = {
99 .open_trace = ctf_text_open_trace,
100 .close_trace = ctf_text_close_trace,
101};
102
d335f0f7
MD
103static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
104 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
105 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
106 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
107 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE;
108
109static
110void __attribute__((constructor)) init_quarks(void)
111{
112 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
113 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
114 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
115 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
116 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
117}
118
119int print_field(struct definition *definition)
120{
121 /* Print all fields in verbose mode */
122 if (babeltrace_verbose)
123 return 1;
124
125 /* Filter out part of the packet context */
126 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
127 return 0;
128 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
129 return 0;
130 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
131 return 0;
132 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
133 return 0;
134 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
135 return 0;
136
137 return 1;
138}
139
cba1661c
MD
140static
141void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
142{
143 switch (item) {
144 case ITEM_SCOPE:
145 if (opt_all_field_names || opt_scope_field_names)
146 pos->print_names = 1;
147 else
148 pos->print_names = 0;
149 break;
150 case ITEM_HEADER:
151 if (opt_all_field_names || opt_header_field_names)
152 pos->print_names = 1;
153 else
154 pos->print_names = 0;
155 break;
156 case ITEM_CONTEXT:
157 if (opt_all_field_names || opt_context_field_names)
158 pos->print_names = 1;
159 else
160 pos->print_names = 0;
161 break;
162 case ITEM_PAYLOAD:
163 if (opt_all_field_names || opt_payload_field_names)
164 pos->print_names = 1;
165 else
166 pos->print_names = 0;
167
168 break;
169 default:
170 assert(0);
171 }
172}
173
39535929
MD
174static
175const char *print_loglevel(int value)
176{
177 switch (value) {
178 case -1:
179 return "";
180 case BT_LOGLEVEL_EMERG:
181 return "TRACE_EMERG";
182 case BT_LOGLEVEL_ALERT:
183 return "TRACE_ALERT";
184 case BT_LOGLEVEL_CRIT:
185 return "TRACE_CRIT";
186 case BT_LOGLEVEL_ERR:
187 return "TRACE_ERR";
188 case BT_LOGLEVEL_WARNING:
189 return "TRACE_WARNING";
190 case BT_LOGLEVEL_NOTICE:
191 return "TRACE_NOTICE";
192 case BT_LOGLEVEL_INFO:
193 return "TRACE_INFO";
194 case BT_LOGLEVEL_DEBUG_SYSTEM:
195 return "TRACE_DEBUG_SYSTEM";
196 case BT_LOGLEVEL_DEBUG_PROGRAM:
197 return "TRACE_DEBUG_PROGRAM";
198 case BT_LOGLEVEL_DEBUG_PROCESS:
199 return "TRACE_DEBUG_PROCESS";
200 case BT_LOGLEVEL_DEBUG_MODULE:
201 return "TRACE_DEBUG_MODULE";
202 case BT_LOGLEVEL_DEBUG_UNIT:
203 return "TRACE_DEBUG_UNIT";
204 case BT_LOGLEVEL_DEBUG_FUNCTION:
205 return "TRACE_DEBUG_FUNCTION";
206 case BT_LOGLEVEL_DEBUG_LINE:
207 return "TRACE_DEBUG_LINE";
208 case BT_LOGLEVEL_DEBUG:
209 return "TRACE_DEBUG";
210 default:
211 return "<<UNKNOWN>>";
212 }
213}
214
31262354 215static
9e88d150 216int ctf_text_write_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
e4195791 217
31262354
MD
218{
219 struct ctf_text_stream_pos *pos =
220 container_of(ppos, struct ctf_text_stream_pos, parent);
f380e105 221 struct ctf_stream_declaration *stream_class = stream->stream_class;
fd3382e8 222 int field_nr_saved;
4716614a 223 struct ctf_event_declaration *event_class;
c716f83b 224 struct ctf_event_definition *event;
c87a8eb2 225 uint64_t id;
31262354 226 int ret;
8c250d87 227 int dom_print = 0;
31262354 228
c87a8eb2 229 id = stream->event_id;
31262354
MD
230
231 if (id >= stream_class->events_by_id->len) {
3394d22e 232 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
31262354
MD
233 return -EINVAL;
234 }
e28d4618
MD
235 event = g_ptr_array_index(stream->events_by_id, id);
236 if (!event) {
3394d22e 237 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
e28d4618
MD
238 return -EINVAL;
239 }
31262354 240 event_class = g_ptr_array_index(stream_class->events_by_id, id);
9ff2b8ad
FO
241 if (!event_class) {
242 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
31262354
MD
243 return -EINVAL;
244 }
245
fca04958
MD
246 /* Print events discarded */
247 if (stream->events_discarded) {
7d97fad9 248 fflush(pos->fp);
4c4ba021 249 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events between [",
fca04958 250 stream->events_discarded);
03798a93
JD
251 if (opt_clock_cycles) {
252 ctf_print_timestamp(stderr, stream,
253 stream->prev_cycles_timestamp);
254 fprintf(stderr, "] and [");
255 ctf_print_timestamp(stderr, stream,
256 stream->prev_cycles_timestamp_end);
257 } else {
258 ctf_print_timestamp(stderr, stream,
259 stream->prev_real_timestamp);
260 fprintf(stderr, "] and [");
261 ctf_print_timestamp(stderr, stream,
262 stream->prev_real_timestamp_end);
263 }
a0b34fbb 264 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
fca04958
MD
265 fflush(stderr);
266 stream->events_discarded = 0;
267 }
268
5e2eb0ae 269 if (stream->has_timestamp) {
cba1661c 270 set_field_names_print(pos, ITEM_HEADER);
be60c7fb
MD
271 if (pos->print_names)
272 fprintf(pos->fp, "timestamp = ");
273 else
274 fprintf(pos->fp, "[");
03798a93
JD
275 if (opt_clock_cycles) {
276 ctf_print_timestamp(pos->fp, stream, stream->cycles_timestamp);
277 } else {
278 ctf_print_timestamp(pos->fp, stream, stream->real_timestamp);
279 }
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, "(");
03798a93
JD
296 if (pos->last_real_timestamp != -1ULL) {
297 delta = stream->real_timestamp - pos->last_real_timestamp;
8d8ed9af
MD
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, " ");
03798a93
JD
312 pos->last_real_timestamp = stream->real_timestamp;
313 pos->last_cycles_timestamp = stream->cycles_timestamp;
8d8ed9af
MD
314 }
315
359d7456 316 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
82662ad4 317 set_field_names_print(pos, ITEM_HEADER);
8c250d87 318 if (pos->print_names) {
359d7456 319 fprintf(pos->fp, "trace = ");
8c250d87 320 }
82662ad4 321 fprintf(pos->fp, "%s", stream_class->trace->path);
82662ad4
MD
322 if (pos->print_names)
323 fprintf(pos->fp, ", ");
324 else
325 fprintf(pos->fp, " ");
326 }
c3815874
MD
327 if ((opt_trace_hostname_field || opt_all_fields || opt_trace_default_fields)
328 && stream_class->trace->env.hostname[0] != '\0') {
32cfb8ad
MD
329 set_field_names_print(pos, ITEM_HEADER);
330 if (pos->print_names) {
331 fprintf(pos->fp, "trace:hostname = ");
332 }
333 fprintf(pos->fp, "%s", stream_class->trace->env.hostname);
334 if (pos->print_names)
335 fprintf(pos->fp, ", ");
336 dom_print = 1;
337 }
c3815874 338 if ((opt_trace_domain_field || opt_all_fields) && stream_class->trace->env.domain[0] != '\0') {
8c250d87
MD
339 set_field_names_print(pos, ITEM_HEADER);
340 if (pos->print_names) {
341 fprintf(pos->fp, "trace:domain = ");
342 }
1842a4c8 343 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
8c250d87
MD
344 if (pos->print_names)
345 fprintf(pos->fp, ", ");
346 dom_print = 1;
347 }
c3815874
MD
348 if ((opt_trace_procname_field || opt_all_fields || opt_trace_default_fields)
349 && stream_class->trace->env.procname[0] != '\0') {
8c250d87
MD
350 set_field_names_print(pos, ITEM_HEADER);
351 if (pos->print_names) {
352 fprintf(pos->fp, "trace:procname = ");
353 } else if (dom_print) {
354 fprintf(pos->fp, ":");
355 }
1842a4c8 356 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
8c250d87
MD
357 if (pos->print_names)
358 fprintf(pos->fp, ", ");
359 dom_print = 1;
360 }
c3815874
MD
361 if ((opt_trace_vpid_field || opt_all_fields || opt_trace_default_fields)
362 && stream_class->trace->env.vpid != -1) {
8c250d87
MD
363 set_field_names_print(pos, ITEM_HEADER);
364 if (pos->print_names) {
365 fprintf(pos->fp, "trace:vpid = ");
366 } else if (dom_print) {
367 fprintf(pos->fp, ":");
368 }
1842a4c8 369 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
8c250d87
MD
370 if (pos->print_names)
371 fprintf(pos->fp, ", ");
372 dom_print = 1;
373 }
306eeaa6 374 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
d86d62f8
MD
375 set_field_names_print(pos, ITEM_HEADER);
376 if (pos->print_names) {
377 fprintf(pos->fp, "loglevel = ");
378 } else if (dom_print) {
379 fprintf(pos->fp, ":");
380 }
39535929
MD
381 fprintf(pos->fp, "%s (%d)",
382 print_loglevel(event_class->loglevel),
306eeaa6 383 event_class->loglevel);
d86d62f8
MD
384 if (pos->print_names)
385 fprintf(pos->fp, ", ");
386 dom_print = 1;
387 }
8c250d87
MD
388 if (dom_print && !pos->print_names)
389 fprintf(pos->fp, " ");
cba1661c 390 set_field_names_print(pos, ITEM_HEADER);
31262354
MD
391 if (pos->print_names)
392 fprintf(pos->fp, "name = ");
8178fe48
MD
393 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
394 if (pos->print_names)
fd3382e8 395 pos->field_nr++;
8178fe48 396 else
fd3382e8 397 fprintf(pos->fp, ":");
31262354 398
e28d4618
MD
399 /* print cpuid field from packet context */
400 if (stream->stream_packet_context) {
401 if (pos->field_nr++ != 0)
402 fprintf(pos->fp, ",");
cba1661c 403 set_field_names_print(pos, ITEM_SCOPE);
e28d4618
MD
404 if (pos->print_names)
405 fprintf(pos->fp, " stream.packet.context =");
406 field_nr_saved = pos->field_nr;
407 pos->field_nr = 0;
cba1661c 408 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618
MD
409 ret = generic_rw(ppos, &stream->stream_packet_context->p);
410 if (ret)
411 goto error;
412 pos->field_nr = field_nr_saved;
413 }
414
764af3f4 415 /* Only show the event header in verbose mode */
e28d4618 416 if (babeltrace_verbose && stream->stream_event_header) {
fd3382e8
MD
417 if (pos->field_nr++ != 0)
418 fprintf(pos->fp, ",");
cba1661c 419 set_field_names_print(pos, ITEM_SCOPE);
31262354 420 if (pos->print_names)
fd3382e8
MD
421 fprintf(pos->fp, " stream.event.header =");
422 field_nr_saved = pos->field_nr;
423 pos->field_nr = 0;
cba1661c 424 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 425 ret = generic_rw(ppos, &stream->stream_event_header->p);
31262354
MD
426 if (ret)
427 goto error;
fd3382e8 428 pos->field_nr = field_nr_saved;
31262354
MD
429 }
430
431 /* print stream-declared event context */
e28d4618 432 if (stream->stream_event_context) {
fd3382e8
MD
433 if (pos->field_nr++ != 0)
434 fprintf(pos->fp, ",");
cba1661c 435 set_field_names_print(pos, ITEM_SCOPE);
31262354 436 if (pos->print_names)
fd3382e8
MD
437 fprintf(pos->fp, " stream.event.context =");
438 field_nr_saved = pos->field_nr;
439 pos->field_nr = 0;
cba1661c 440 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 441 ret = generic_rw(ppos, &stream->stream_event_context->p);
31262354
MD
442 if (ret)
443 goto error;
fd3382e8 444 pos->field_nr = field_nr_saved;
31262354
MD
445 }
446
447 /* print event-declared event context */
e28d4618 448 if (event->event_context) {
fd3382e8
MD
449 if (pos->field_nr++ != 0)
450 fprintf(pos->fp, ",");
cba1661c 451 set_field_names_print(pos, ITEM_SCOPE);
31262354 452 if (pos->print_names)
fd3382e8
MD
453 fprintf(pos->fp, " event.context =");
454 field_nr_saved = pos->field_nr;
455 pos->field_nr = 0;
cba1661c 456 set_field_names_print(pos, ITEM_CONTEXT);
e28d4618 457 ret = generic_rw(ppos, &event->event_context->p);
31262354
MD
458 if (ret)
459 goto error;
fd3382e8 460 pos->field_nr = field_nr_saved;
31262354
MD
461 }
462
463 /* Read and print event payload */
e28d4618 464 if (event->event_fields) {
fd3382e8
MD
465 if (pos->field_nr++ != 0)
466 fprintf(pos->fp, ",");
cba1661c 467 set_field_names_print(pos, ITEM_SCOPE);
31262354 468 if (pos->print_names)
fd3382e8
MD
469 fprintf(pos->fp, " event.fields =");
470 field_nr_saved = pos->field_nr;
471 pos->field_nr = 0;
cba1661c 472 set_field_names_print(pos, ITEM_PAYLOAD);
e28d4618 473 ret = generic_rw(ppos, &event->event_fields->p);
31262354
MD
474 if (ret)
475 goto error;
fd3382e8 476 pos->field_nr = field_nr_saved;
31262354 477 }
fd3382e8 478 /* newline */
31262354 479 fprintf(pos->fp, "\n");
fd3382e8 480 pos->field_nr = 0;
31262354
MD
481
482 return 0;
483
484error:
3394d22e 485 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
486 return ret;
487}
488
489
5b80ddfb 490struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
20d0dcf9 491 void (*packet_seek)(struct stream_pos *pos, size_t index,
ae23d232 492 int whence), FILE *metadata_fp)
642ec66d
MD
493{
494 struct ctf_text_stream_pos *pos;
495 FILE *fp;
496
497 pos = g_new0(struct ctf_text_stream_pos, 1);
498
03798a93
JD
499 pos->last_real_timestamp = -1ULL;
500 pos->last_cycles_timestamp = -1ULL;
642ec66d 501 switch (flags & O_ACCMODE) {
989c73bc 502 case O_RDWR:
b61922b5 503 if (!path)
6cf7957b
MD
504 fp = stdout;
505 else
506 fp = fopen(path, "w");
642ec66d
MD
507 if (!fp)
508 goto error;
509 pos->fp = fp;
510 pos->parent.rw_table = write_dispatch_table;
31262354 511 pos->parent.event_cb = ctf_text_write_event;
cba1661c 512 pos->print_names = 0;
642ec66d
MD
513 break;
514 case O_RDONLY:
515 default:
3394d22e 516 fprintf(stderr, "[error] Incorrect open flags.\n");
642ec66d
MD
517 goto error;
518 }
519
520 return &pos->trace_descriptor;
521error:
522 g_free(pos);
523 return NULL;
524}
525
526void ctf_text_close_trace(struct trace_descriptor *td)
527{
528 struct ctf_text_stream_pos *pos =
529 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
530 fclose(pos->fp);
531 g_free(pos);
532}
533
534void __attribute__((constructor)) ctf_text_init(void)
535{
536 int ret;
537
538 ctf_text_format.name = g_quark_from_static_string("text");
539 ret = bt_register_format(&ctf_text_format);
540 assert(!ret);
541}
542
543/* TODO: finalize */
This page took 0.051598 seconds and 4 git commands to generate.