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