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