2ba082303c50941a9beda71ee30cf6ce9fdfcee6
[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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * SOFTWARE.
27 */
28
29 #include <babeltrace/format.h>
30 #include <babeltrace/ctf-text/types.h>
31 #include <babeltrace/ctf/metadata.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/ctf/events-internal.h>
34 #include <inttypes.h>
35 #include <sys/mman.h>
36 #include <errno.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #include <fcntl.h>
40 #include <dirent.h>
41 #include <glib.h>
42 #include <unistd.h>
43 #include <stdlib.h>
44
45 #define NSEC_PER_SEC 1000000000ULL
46
47 int opt_all_field_names,
48 opt_scope_field_names,
49 opt_header_field_names,
50 opt_context_field_names,
51 opt_payload_field_names,
52 opt_all_fields,
53 opt_trace_field,
54 opt_trace_domain_field,
55 opt_trace_procname_field,
56 opt_trace_vpid_field,
57 opt_trace_hostname_field,
58 opt_trace_default_fields = 1,
59 opt_loglevel_field,
60 opt_emf_field,
61 opt_callsite_field,
62 opt_delta_field = 1;
63
64 enum field_item {
65 ITEM_SCOPE,
66 ITEM_HEADER,
67 ITEM_CONTEXT,
68 ITEM_PAYLOAD,
69 };
70
71 enum bt_loglevel {
72 BT_LOGLEVEL_EMERG = 0,
73 BT_LOGLEVEL_ALERT = 1,
74 BT_LOGLEVEL_CRIT = 2,
75 BT_LOGLEVEL_ERR = 3,
76 BT_LOGLEVEL_WARNING = 4,
77 BT_LOGLEVEL_NOTICE = 5,
78 BT_LOGLEVEL_INFO = 6,
79 BT_LOGLEVEL_DEBUG_SYSTEM = 7,
80 BT_LOGLEVEL_DEBUG_PROGRAM = 8,
81 BT_LOGLEVEL_DEBUG_PROCESS = 9,
82 BT_LOGLEVEL_DEBUG_MODULE = 10,
83 BT_LOGLEVEL_DEBUG_UNIT = 11,
84 BT_LOGLEVEL_DEBUG_FUNCTION = 12,
85 BT_LOGLEVEL_DEBUG_LINE = 13,
86 BT_LOGLEVEL_DEBUG = 14,
87 };
88
89 static
90 struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags,
91 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
92 int whence), FILE *metadata_fp);
93 static
94 int ctf_text_close_trace(struct bt_trace_descriptor *descriptor);
95
96 static
97 rw_dispatch write_dispatch_table[] = {
98 [ CTF_TYPE_INTEGER ] = ctf_text_integer_write,
99 [ CTF_TYPE_FLOAT ] = ctf_text_float_write,
100 [ CTF_TYPE_ENUM ] = ctf_text_enum_write,
101 [ CTF_TYPE_STRING ] = ctf_text_string_write,
102 [ CTF_TYPE_STRUCT ] = ctf_text_struct_write,
103 [ CTF_TYPE_VARIANT ] = ctf_text_variant_write,
104 [ CTF_TYPE_ARRAY ] = ctf_text_array_write,
105 [ CTF_TYPE_SEQUENCE ] = ctf_text_sequence_write,
106 };
107
108 static
109 struct bt_format ctf_text_format = {
110 .open_trace = ctf_text_open_trace,
111 .close_trace = ctf_text_close_trace,
112 };
113
114 static GQuark Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN,
115 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END,
116 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED,
117 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE,
118 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE,
119 Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM;
120
121 static
122 void __attribute__((constructor)) init_quarks(void)
123 {
124 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
125 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
126 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
127 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
128 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
129 Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM = g_quark_from_static_string("stream.packet.context.packet_seq_num");
130 }
131
132 static
133 struct ctf_callsite_dups *ctf_trace_callsite_lookup(struct ctf_trace *trace,
134 GQuark callsite_name)
135 {
136 return g_hash_table_lookup(trace->callsites,
137 (gpointer) (unsigned long) callsite_name);
138 }
139
140 int print_field(struct bt_definition *definition)
141 {
142 /* Print all fields in verbose mode */
143 if (babeltrace_verbose)
144 return 1;
145
146 /* Filter out part of the packet context */
147 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
148 return 0;
149 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
150 return 0;
151 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
152 return 0;
153 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
154 return 0;
155 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
156 return 0;
157 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SEQ_NUM)
158 return 0;
159
160 return 1;
161 }
162
163 static
164 void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
165 {
166 switch (item) {
167 case ITEM_SCOPE:
168 if (opt_all_field_names || opt_scope_field_names)
169 pos->print_names = 1;
170 else
171 pos->print_names = 0;
172 break;
173 case ITEM_HEADER:
174 if (opt_all_field_names || opt_header_field_names)
175 pos->print_names = 1;
176 else
177 pos->print_names = 0;
178 break;
179 case ITEM_CONTEXT:
180 if (opt_all_field_names || opt_context_field_names)
181 pos->print_names = 1;
182 else
183 pos->print_names = 0;
184 break;
185 case ITEM_PAYLOAD:
186 if (opt_all_field_names || opt_payload_field_names)
187 pos->print_names = 1;
188 else
189 pos->print_names = 0;
190
191 break;
192 default:
193 assert(0);
194 }
195 }
196
197 static
198 const char *print_loglevel(int value)
199 {
200 switch (value) {
201 case -1:
202 return "";
203 case BT_LOGLEVEL_EMERG:
204 return "TRACE_EMERG";
205 case BT_LOGLEVEL_ALERT:
206 return "TRACE_ALERT";
207 case BT_LOGLEVEL_CRIT:
208 return "TRACE_CRIT";
209 case BT_LOGLEVEL_ERR:
210 return "TRACE_ERR";
211 case BT_LOGLEVEL_WARNING:
212 return "TRACE_WARNING";
213 case BT_LOGLEVEL_NOTICE:
214 return "TRACE_NOTICE";
215 case BT_LOGLEVEL_INFO:
216 return "TRACE_INFO";
217 case BT_LOGLEVEL_DEBUG_SYSTEM:
218 return "TRACE_DEBUG_SYSTEM";
219 case BT_LOGLEVEL_DEBUG_PROGRAM:
220 return "TRACE_DEBUG_PROGRAM";
221 case BT_LOGLEVEL_DEBUG_PROCESS:
222 return "TRACE_DEBUG_PROCESS";
223 case BT_LOGLEVEL_DEBUG_MODULE:
224 return "TRACE_DEBUG_MODULE";
225 case BT_LOGLEVEL_DEBUG_UNIT:
226 return "TRACE_DEBUG_UNIT";
227 case BT_LOGLEVEL_DEBUG_FUNCTION:
228 return "TRACE_DEBUG_FUNCTION";
229 case BT_LOGLEVEL_DEBUG_LINE:
230 return "TRACE_DEBUG_LINE";
231 case BT_LOGLEVEL_DEBUG:
232 return "TRACE_DEBUG";
233 default:
234 return "<<UNKNOWN>>";
235 }
236 }
237
238 static
239 int ctf_text_write_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
240
241 {
242 struct ctf_text_stream_pos *pos =
243 container_of(ppos, struct ctf_text_stream_pos, parent);
244 struct ctf_stream_declaration *stream_class = stream->stream_class;
245 int field_nr_saved;
246 struct ctf_event_declaration *event_class;
247 struct ctf_event_definition *event;
248 uint64_t id;
249 int ret;
250 int dom_print = 0;
251
252 id = stream->event_id;
253
254 if (id >= stream_class->events_by_id->len) {
255 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
256 return -EINVAL;
257 }
258 event = g_ptr_array_index(stream->events_by_id, id);
259 if (!event) {
260 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
261 return -EINVAL;
262 }
263 event_class = g_ptr_array_index(stream_class->events_by_id, id);
264 if (!event_class) {
265 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
266 return -EINVAL;
267 }
268
269 if (stream->has_timestamp) {
270 set_field_names_print(pos, ITEM_HEADER);
271 if (pos->print_names)
272 fprintf(pos->fp, "timestamp = ");
273 else
274 fprintf(pos->fp, "[");
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 }
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 && 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_real_timestamp != -1ULL) {
297 delta = stream->real_timestamp - pos->last_real_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_real_timestamp = stream->real_timestamp;
313 pos->last_cycles_timestamp = stream->cycles_timestamp;
314 }
315
316 if ((opt_trace_field || opt_all_fields) && stream_class->trace->parent.path[0] != '\0') {
317 set_field_names_print(pos, ITEM_HEADER);
318 if (pos->print_names) {
319 fprintf(pos->fp, "trace = ");
320 }
321 fprintf(pos->fp, "%s", stream_class->trace->parent.path);
322 if (pos->print_names)
323 fprintf(pos->fp, ", ");
324 else
325 fprintf(pos->fp, " ");
326 }
327 if ((opt_trace_hostname_field || opt_all_fields || opt_trace_default_fields)
328 && stream_class->trace->env.hostname[0] != '\0') {
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 }
338 if ((opt_trace_domain_field || opt_all_fields) && stream_class->trace->env.domain[0] != '\0') {
339 set_field_names_print(pos, ITEM_HEADER);
340 if (pos->print_names) {
341 fprintf(pos->fp, "trace:domain = ");
342 }
343 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
344 if (pos->print_names)
345 fprintf(pos->fp, ", ");
346 dom_print = 1;
347 }
348 if ((opt_trace_procname_field || opt_all_fields || opt_trace_default_fields)
349 && stream_class->trace->env.procname[0] != '\0') {
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 }
356 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
357 if (pos->print_names)
358 fprintf(pos->fp, ", ");
359 dom_print = 1;
360 }
361 if ((opt_trace_vpid_field || opt_all_fields || opt_trace_default_fields)
362 && stream_class->trace->env.vpid != -1) {
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 }
369 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
370 if (pos->print_names)
371 fprintf(pos->fp, ", ");
372 dom_print = 1;
373 }
374 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
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 }
381 fprintf(pos->fp, "%s (%d)",
382 print_loglevel(event_class->loglevel),
383 event_class->loglevel);
384 if (pos->print_names)
385 fprintf(pos->fp, ", ");
386 dom_print = 1;
387 }
388 if ((opt_emf_field || opt_all_fields) && event_class->model_emf_uri) {
389 set_field_names_print(pos, ITEM_HEADER);
390 if (pos->print_names) {
391 fprintf(pos->fp, "model.emf.uri = ");
392 } else if (dom_print) {
393 fprintf(pos->fp, ":");
394 }
395 fprintf(pos->fp, "\"%s\"",
396 g_quark_to_string(event_class->model_emf_uri));
397 if (pos->print_names)
398 fprintf(pos->fp, ", ");
399 dom_print = 1;
400 }
401 if ((opt_callsite_field || opt_all_fields)) {
402 struct ctf_callsite_dups *cs_dups;
403 struct ctf_callsite *callsite;
404
405 cs_dups = ctf_trace_callsite_lookup(stream_class->trace,
406 event_class->name);
407 if (cs_dups) {
408 int i = 0;
409
410 set_field_names_print(pos, ITEM_HEADER);
411 if (pos->print_names) {
412 fprintf(pos->fp, "callsite = ");
413 } else if (dom_print) {
414 fprintf(pos->fp, ":");
415 }
416 fprintf(pos->fp, "[");
417 bt_list_for_each_entry(callsite, &cs_dups->head, node) {
418 if (i != 0)
419 fprintf(pos->fp, ",");
420 if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
421 fprintf(pos->fp, "%s@0x%" PRIx64 ":%s:%" PRIu64 "",
422 callsite->func, callsite->ip, callsite->file,
423 callsite->line);
424 } else {
425 fprintf(pos->fp, "%s:%s:%" PRIu64 "",
426 callsite->func, callsite->file,
427 callsite->line);
428 }
429 i++;
430 }
431 fprintf(pos->fp, "]");
432 if (pos->print_names)
433 fprintf(pos->fp, ", ");
434 dom_print = 1;
435 }
436 }
437 if (dom_print && !pos->print_names)
438 fprintf(pos->fp, " ");
439 set_field_names_print(pos, ITEM_HEADER);
440 if (pos->print_names)
441 fprintf(pos->fp, "name = ");
442 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
443 if (pos->print_names)
444 pos->field_nr++;
445 else
446 fprintf(pos->fp, ":");
447
448 /* print cpuid field from packet context */
449 if (stream->stream_packet_context) {
450 if (pos->field_nr++ != 0)
451 fprintf(pos->fp, ",");
452 set_field_names_print(pos, ITEM_SCOPE);
453 if (pos->print_names)
454 fprintf(pos->fp, " stream.packet.context =");
455 field_nr_saved = pos->field_nr;
456 pos->field_nr = 0;
457 set_field_names_print(pos, ITEM_CONTEXT);
458 ret = generic_rw(ppos, &stream->stream_packet_context->p);
459 if (ret)
460 goto error;
461 pos->field_nr = field_nr_saved;
462 }
463
464 /* Only show the event header in verbose mode */
465 if (babeltrace_verbose && stream->stream_event_header) {
466 if (pos->field_nr++ != 0)
467 fprintf(pos->fp, ",");
468 set_field_names_print(pos, ITEM_SCOPE);
469 if (pos->print_names)
470 fprintf(pos->fp, " stream.event.header =");
471 field_nr_saved = pos->field_nr;
472 pos->field_nr = 0;
473 set_field_names_print(pos, ITEM_CONTEXT);
474 ret = generic_rw(ppos, &stream->stream_event_header->p);
475 if (ret)
476 goto error;
477 pos->field_nr = field_nr_saved;
478 }
479
480 /* print stream-declared event context */
481 if (stream->stream_event_context) {
482 if (pos->field_nr++ != 0)
483 fprintf(pos->fp, ",");
484 set_field_names_print(pos, ITEM_SCOPE);
485 if (pos->print_names)
486 fprintf(pos->fp, " stream.event.context =");
487 field_nr_saved = pos->field_nr;
488 pos->field_nr = 0;
489 set_field_names_print(pos, ITEM_CONTEXT);
490 ret = generic_rw(ppos, &stream->stream_event_context->p);
491 if (ret)
492 goto error;
493 pos->field_nr = field_nr_saved;
494 }
495
496 /* print event-declared event context */
497 if (event->event_context) {
498 if (pos->field_nr++ != 0)
499 fprintf(pos->fp, ",");
500 set_field_names_print(pos, ITEM_SCOPE);
501 if (pos->print_names)
502 fprintf(pos->fp, " event.context =");
503 field_nr_saved = pos->field_nr;
504 pos->field_nr = 0;
505 set_field_names_print(pos, ITEM_CONTEXT);
506 ret = generic_rw(ppos, &event->event_context->p);
507 if (ret)
508 goto error;
509 pos->field_nr = field_nr_saved;
510 }
511
512 /* Read and print event payload */
513 if (event->event_fields) {
514 if (pos->field_nr++ != 0)
515 fprintf(pos->fp, ",");
516 set_field_names_print(pos, ITEM_SCOPE);
517 if (pos->print_names)
518 fprintf(pos->fp, " event.fields =");
519 field_nr_saved = pos->field_nr;
520 pos->field_nr = 0;
521 set_field_names_print(pos, ITEM_PAYLOAD);
522 ret = generic_rw(ppos, &event->event_fields->p);
523 if (ret)
524 goto error;
525 pos->field_nr = field_nr_saved;
526 }
527 /* newline */
528 fprintf(pos->fp, "\n");
529 pos->field_nr = 0;
530
531 return 0;
532
533 error:
534 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
535 return ret;
536 }
537
538 static
539 struct bt_trace_descriptor *ctf_text_open_trace(const char *path, int flags,
540 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
541 int whence), FILE *metadata_fp)
542 {
543 struct ctf_text_stream_pos *pos;
544 FILE *fp;
545
546 pos = g_new0(struct ctf_text_stream_pos, 1);
547
548 pos->last_real_timestamp = -1ULL;
549 pos->last_cycles_timestamp = -1ULL;
550 switch (flags & O_ACCMODE) {
551 case O_RDWR:
552 if (!path)
553 fp = stdout;
554 else
555 fp = fopen(path, "w");
556 if (!fp)
557 goto error;
558 pos->fp = fp;
559 pos->parent.rw_table = write_dispatch_table;
560 pos->parent.event_cb = ctf_text_write_event;
561 pos->parent.trace = &pos->trace_descriptor;
562 pos->print_names = 0;
563 babeltrace_ctf_console_output++;
564 break;
565 case O_RDONLY:
566 default:
567 fprintf(stderr, "[error] Incorrect open flags.\n");
568 goto error;
569 }
570
571 return &pos->trace_descriptor;
572 error:
573 g_free(pos);
574 return NULL;
575 }
576
577 static
578 int ctf_text_close_trace(struct bt_trace_descriptor *td)
579 {
580 int ret;
581 struct ctf_text_stream_pos *pos =
582 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
583
584 babeltrace_ctf_console_output--;
585 if (pos->fp != stdout) {
586 ret = fclose(pos->fp);
587 if (ret) {
588 perror("Error on fclose");
589 return -1;
590 }
591 }
592 g_free(pos);
593 return 0;
594 }
595
596 static
597 void __attribute__((constructor)) ctf_text_init(void)
598 {
599 int ret;
600
601 ctf_text_format.name = g_quark_from_static_string("text");
602 ret = bt_register_format(&ctf_text_format);
603 assert(!ret);
604 }
605
606 static
607 void __attribute__((destructor)) ctf_text_exit(void)
608 {
609 bt_unregister_format(&ctf_text_format);
610 }
This page took 0.039999 seconds and 3 git commands to generate.