1f798b2bc372c522e369f9d2d86e0b66965bcc8b
[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 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 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 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
120 static
121 void __attribute__((constructor)) init_quarks(void)
122 {
123 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN = g_quark_from_static_string("stream.packet.context.timestamp_begin");
124 Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END = g_quark_from_static_string("stream.packet.context.timestamp_end");
125 Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED = g_quark_from_static_string("stream.packet.context.events_discarded");
126 Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE = g_quark_from_static_string("stream.packet.context.content_size");
127 Q_STREAM_PACKET_CONTEXT_PACKET_SIZE = g_quark_from_static_string("stream.packet.context.packet_size");
128 }
129
130 static
131 struct ctf_callsite_dups *ctf_trace_callsite_lookup(struct ctf_trace *trace,
132 GQuark callsite_name)
133 {
134 return g_hash_table_lookup(trace->callsites,
135 (gpointer) (unsigned long) callsite_name);
136 }
137
138 int print_field(struct definition *definition)
139 {
140 /* Print all fields in verbose mode */
141 if (babeltrace_verbose)
142 return 1;
143
144 /* Filter out part of the packet context */
145 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_BEGIN)
146 return 0;
147 if (definition->path == Q_STREAM_PACKET_CONTEXT_TIMESTAMP_END)
148 return 0;
149 if (definition->path == Q_STREAM_PACKET_CONTEXT_EVENTS_DISCARDED)
150 return 0;
151 if (definition->path == Q_STREAM_PACKET_CONTEXT_CONTENT_SIZE)
152 return 0;
153 if (definition->path == Q_STREAM_PACKET_CONTEXT_PACKET_SIZE)
154 return 0;
155
156 return 1;
157 }
158
159 static
160 void set_field_names_print(struct ctf_text_stream_pos *pos, enum field_item item)
161 {
162 switch (item) {
163 case ITEM_SCOPE:
164 if (opt_all_field_names || opt_scope_field_names)
165 pos->print_names = 1;
166 else
167 pos->print_names = 0;
168 break;
169 case ITEM_HEADER:
170 if (opt_all_field_names || opt_header_field_names)
171 pos->print_names = 1;
172 else
173 pos->print_names = 0;
174 break;
175 case ITEM_CONTEXT:
176 if (opt_all_field_names || opt_context_field_names)
177 pos->print_names = 1;
178 else
179 pos->print_names = 0;
180 break;
181 case ITEM_PAYLOAD:
182 if (opt_all_field_names || opt_payload_field_names)
183 pos->print_names = 1;
184 else
185 pos->print_names = 0;
186
187 break;
188 default:
189 assert(0);
190 }
191 }
192
193 static
194 const char *print_loglevel(int value)
195 {
196 switch (value) {
197 case -1:
198 return "";
199 case BT_LOGLEVEL_EMERG:
200 return "TRACE_EMERG";
201 case BT_LOGLEVEL_ALERT:
202 return "TRACE_ALERT";
203 case BT_LOGLEVEL_CRIT:
204 return "TRACE_CRIT";
205 case BT_LOGLEVEL_ERR:
206 return "TRACE_ERR";
207 case BT_LOGLEVEL_WARNING:
208 return "TRACE_WARNING";
209 case BT_LOGLEVEL_NOTICE:
210 return "TRACE_NOTICE";
211 case BT_LOGLEVEL_INFO:
212 return "TRACE_INFO";
213 case BT_LOGLEVEL_DEBUG_SYSTEM:
214 return "TRACE_DEBUG_SYSTEM";
215 case BT_LOGLEVEL_DEBUG_PROGRAM:
216 return "TRACE_DEBUG_PROGRAM";
217 case BT_LOGLEVEL_DEBUG_PROCESS:
218 return "TRACE_DEBUG_PROCESS";
219 case BT_LOGLEVEL_DEBUG_MODULE:
220 return "TRACE_DEBUG_MODULE";
221 case BT_LOGLEVEL_DEBUG_UNIT:
222 return "TRACE_DEBUG_UNIT";
223 case BT_LOGLEVEL_DEBUG_FUNCTION:
224 return "TRACE_DEBUG_FUNCTION";
225 case BT_LOGLEVEL_DEBUG_LINE:
226 return "TRACE_DEBUG_LINE";
227 case BT_LOGLEVEL_DEBUG:
228 return "TRACE_DEBUG";
229 default:
230 return "<<UNKNOWN>>";
231 }
232 }
233
234 static
235 int ctf_text_write_event(struct bt_stream_pos *ppos, struct ctf_stream_definition *stream)
236
237 {
238 struct ctf_text_stream_pos *pos =
239 container_of(ppos, struct ctf_text_stream_pos, parent);
240 struct ctf_stream_declaration *stream_class = stream->stream_class;
241 int field_nr_saved;
242 struct ctf_event_declaration *event_class;
243 struct ctf_event_definition *event;
244 uint64_t id;
245 int ret;
246 int dom_print = 0;
247
248 id = stream->event_id;
249
250 if (id >= stream_class->events_by_id->len) {
251 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
252 return -EINVAL;
253 }
254 event = g_ptr_array_index(stream->events_by_id, id);
255 if (!event) {
256 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
257 return -EINVAL;
258 }
259 event_class = g_ptr_array_index(stream_class->events_by_id, id);
260 if (!event_class) {
261 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
262 return -EINVAL;
263 }
264
265 /* Print events discarded */
266 if (stream->events_discarded) {
267 fflush(pos->fp);
268 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events between [",
269 stream->events_discarded);
270 if (opt_clock_cycles) {
271 ctf_print_timestamp(stderr, stream,
272 stream->prev_cycles_timestamp);
273 fprintf(stderr, "] and [");
274 ctf_print_timestamp(stderr, stream,
275 stream->prev_cycles_timestamp_end);
276 } else {
277 ctf_print_timestamp(stderr, stream,
278 stream->prev_real_timestamp);
279 fprintf(stderr, "] and [");
280 ctf_print_timestamp(stderr, stream,
281 stream->prev_real_timestamp_end);
282 }
283 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
284 fflush(stderr);
285 stream->events_discarded = 0;
286 }
287
288 if (stream->has_timestamp) {
289 set_field_names_print(pos, ITEM_HEADER);
290 if (pos->print_names)
291 fprintf(pos->fp, "timestamp = ");
292 else
293 fprintf(pos->fp, "[");
294 if (opt_clock_cycles) {
295 ctf_print_timestamp(pos->fp, stream, stream->cycles_timestamp);
296 } else {
297 ctf_print_timestamp(pos->fp, stream, stream->real_timestamp);
298 }
299 if (!pos->print_names)
300 fprintf(pos->fp, "]");
301
302 if (pos->print_names)
303 fprintf(pos->fp, ", ");
304 else
305 fprintf(pos->fp, " ");
306 }
307 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
308 uint64_t delta, delta_sec, delta_nsec;
309
310 set_field_names_print(pos, ITEM_HEADER);
311 if (pos->print_names)
312 fprintf(pos->fp, "delta = ");
313 else
314 fprintf(pos->fp, "(");
315 if (pos->last_real_timestamp != -1ULL) {
316 delta = stream->real_timestamp - pos->last_real_timestamp;
317 delta_sec = delta / NSEC_PER_SEC;
318 delta_nsec = delta % NSEC_PER_SEC;
319 fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64,
320 delta_sec, delta_nsec);
321 } else {
322 fprintf(pos->fp, "+?.?????????");
323 }
324 if (!pos->print_names)
325 fprintf(pos->fp, ")");
326
327 if (pos->print_names)
328 fprintf(pos->fp, ", ");
329 else
330 fprintf(pos->fp, " ");
331 pos->last_real_timestamp = stream->real_timestamp;
332 pos->last_cycles_timestamp = stream->cycles_timestamp;
333 }
334
335 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
336 set_field_names_print(pos, ITEM_HEADER);
337 if (pos->print_names) {
338 fprintf(pos->fp, "trace = ");
339 }
340 fprintf(pos->fp, "%s", stream_class->trace->path);
341 if (pos->print_names)
342 fprintf(pos->fp, ", ");
343 else
344 fprintf(pos->fp, " ");
345 }
346 if ((opt_trace_hostname_field || opt_all_fields || opt_trace_default_fields)
347 && stream_class->trace->env.hostname[0] != '\0') {
348 set_field_names_print(pos, ITEM_HEADER);
349 if (pos->print_names) {
350 fprintf(pos->fp, "trace:hostname = ");
351 }
352 fprintf(pos->fp, "%s", stream_class->trace->env.hostname);
353 if (pos->print_names)
354 fprintf(pos->fp, ", ");
355 dom_print = 1;
356 }
357 if ((opt_trace_domain_field || opt_all_fields) && stream_class->trace->env.domain[0] != '\0') {
358 set_field_names_print(pos, ITEM_HEADER);
359 if (pos->print_names) {
360 fprintf(pos->fp, "trace:domain = ");
361 }
362 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
363 if (pos->print_names)
364 fprintf(pos->fp, ", ");
365 dom_print = 1;
366 }
367 if ((opt_trace_procname_field || opt_all_fields || opt_trace_default_fields)
368 && stream_class->trace->env.procname[0] != '\0') {
369 set_field_names_print(pos, ITEM_HEADER);
370 if (pos->print_names) {
371 fprintf(pos->fp, "trace:procname = ");
372 } else if (dom_print) {
373 fprintf(pos->fp, ":");
374 }
375 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
376 if (pos->print_names)
377 fprintf(pos->fp, ", ");
378 dom_print = 1;
379 }
380 if ((opt_trace_vpid_field || opt_all_fields || opt_trace_default_fields)
381 && stream_class->trace->env.vpid != -1) {
382 set_field_names_print(pos, ITEM_HEADER);
383 if (pos->print_names) {
384 fprintf(pos->fp, "trace:vpid = ");
385 } else if (dom_print) {
386 fprintf(pos->fp, ":");
387 }
388 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
389 if (pos->print_names)
390 fprintf(pos->fp, ", ");
391 dom_print = 1;
392 }
393 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
394 set_field_names_print(pos, ITEM_HEADER);
395 if (pos->print_names) {
396 fprintf(pos->fp, "loglevel = ");
397 } else if (dom_print) {
398 fprintf(pos->fp, ":");
399 }
400 fprintf(pos->fp, "%s (%d)",
401 print_loglevel(event_class->loglevel),
402 event_class->loglevel);
403 if (pos->print_names)
404 fprintf(pos->fp, ", ");
405 dom_print = 1;
406 }
407 if ((opt_emf_field || opt_all_fields) && event_class->model_emf_uri) {
408 set_field_names_print(pos, ITEM_HEADER);
409 if (pos->print_names) {
410 fprintf(pos->fp, "model.emf.uri = ");
411 } else if (dom_print) {
412 fprintf(pos->fp, ":");
413 }
414 fprintf(pos->fp, "\"%s\"",
415 g_quark_to_string(event_class->model_emf_uri));
416 if (pos->print_names)
417 fprintf(pos->fp, ", ");
418 dom_print = 1;
419 }
420 if ((opt_callsite_field || opt_all_fields)) {
421 struct ctf_callsite_dups *cs_dups;
422 struct ctf_callsite *callsite;
423
424 cs_dups = ctf_trace_callsite_lookup(stream_class->trace,
425 event_class->name);
426 if (cs_dups) {
427 int i = 0;
428
429 set_field_names_print(pos, ITEM_HEADER);
430 if (pos->print_names) {
431 fprintf(pos->fp, "callsite = ");
432 } else if (dom_print) {
433 fprintf(pos->fp, ":");
434 }
435 fprintf(pos->fp, "[");
436 bt_list_for_each_entry(callsite, &cs_dups->head, node) {
437 if (i != 0)
438 fprintf(pos->fp, ",");
439 if (CTF_CALLSITE_FIELD_IS_SET(callsite, ip)) {
440 fprintf(pos->fp, "%s@0x%" PRIx64 ":%s:%" PRIu64 "",
441 callsite->func, callsite->ip, callsite->file,
442 callsite->line);
443 } else {
444 fprintf(pos->fp, "%s:%s:%" PRIu64 "",
445 callsite->func, callsite->file,
446 callsite->line);
447 }
448 i++;
449 }
450 fprintf(pos->fp, "]");
451 if (pos->print_names)
452 fprintf(pos->fp, ", ");
453 dom_print = 1;
454 }
455 }
456 if (dom_print && !pos->print_names)
457 fprintf(pos->fp, " ");
458 set_field_names_print(pos, ITEM_HEADER);
459 if (pos->print_names)
460 fprintf(pos->fp, "name = ");
461 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
462 if (pos->print_names)
463 pos->field_nr++;
464 else
465 fprintf(pos->fp, ":");
466
467 /* print cpuid field from packet context */
468 if (stream->stream_packet_context) {
469 if (pos->field_nr++ != 0)
470 fprintf(pos->fp, ",");
471 set_field_names_print(pos, ITEM_SCOPE);
472 if (pos->print_names)
473 fprintf(pos->fp, " stream.packet.context =");
474 field_nr_saved = pos->field_nr;
475 pos->field_nr = 0;
476 set_field_names_print(pos, ITEM_CONTEXT);
477 ret = generic_rw(ppos, &stream->stream_packet_context->p);
478 if (ret)
479 goto error;
480 pos->field_nr = field_nr_saved;
481 }
482
483 /* Only show the event header in verbose mode */
484 if (babeltrace_verbose && stream->stream_event_header) {
485 if (pos->field_nr++ != 0)
486 fprintf(pos->fp, ",");
487 set_field_names_print(pos, ITEM_SCOPE);
488 if (pos->print_names)
489 fprintf(pos->fp, " stream.event.header =");
490 field_nr_saved = pos->field_nr;
491 pos->field_nr = 0;
492 set_field_names_print(pos, ITEM_CONTEXT);
493 ret = generic_rw(ppos, &stream->stream_event_header->p);
494 if (ret)
495 goto error;
496 pos->field_nr = field_nr_saved;
497 }
498
499 /* print stream-declared event context */
500 if (stream->stream_event_context) {
501 if (pos->field_nr++ != 0)
502 fprintf(pos->fp, ",");
503 set_field_names_print(pos, ITEM_SCOPE);
504 if (pos->print_names)
505 fprintf(pos->fp, " stream.event.context =");
506 field_nr_saved = pos->field_nr;
507 pos->field_nr = 0;
508 set_field_names_print(pos, ITEM_CONTEXT);
509 ret = generic_rw(ppos, &stream->stream_event_context->p);
510 if (ret)
511 goto error;
512 pos->field_nr = field_nr_saved;
513 }
514
515 /* print event-declared event context */
516 if (event->event_context) {
517 if (pos->field_nr++ != 0)
518 fprintf(pos->fp, ",");
519 set_field_names_print(pos, ITEM_SCOPE);
520 if (pos->print_names)
521 fprintf(pos->fp, " event.context =");
522 field_nr_saved = pos->field_nr;
523 pos->field_nr = 0;
524 set_field_names_print(pos, ITEM_CONTEXT);
525 ret = generic_rw(ppos, &event->event_context->p);
526 if (ret)
527 goto error;
528 pos->field_nr = field_nr_saved;
529 }
530
531 /* Read and print event payload */
532 if (event->event_fields) {
533 if (pos->field_nr++ != 0)
534 fprintf(pos->fp, ",");
535 set_field_names_print(pos, ITEM_SCOPE);
536 if (pos->print_names)
537 fprintf(pos->fp, " event.fields =");
538 field_nr_saved = pos->field_nr;
539 pos->field_nr = 0;
540 set_field_names_print(pos, ITEM_PAYLOAD);
541 ret = generic_rw(ppos, &event->event_fields->p);
542 if (ret)
543 goto error;
544 pos->field_nr = field_nr_saved;
545 }
546 /* newline */
547 fprintf(pos->fp, "\n");
548 pos->field_nr = 0;
549
550 return 0;
551
552 error:
553 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
554 return ret;
555 }
556
557 static
558 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
559 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
560 int whence), FILE *metadata_fp)
561 {
562 struct ctf_text_stream_pos *pos;
563 FILE *fp;
564
565 pos = g_new0(struct ctf_text_stream_pos, 1);
566
567 pos->last_real_timestamp = -1ULL;
568 pos->last_cycles_timestamp = -1ULL;
569 switch (flags & O_ACCMODE) {
570 case O_RDWR:
571 if (!path)
572 fp = stdout;
573 else
574 fp = fopen(path, "w");
575 if (!fp)
576 goto error;
577 pos->fp = fp;
578 pos->parent.rw_table = write_dispatch_table;
579 pos->parent.event_cb = ctf_text_write_event;
580 pos->print_names = 0;
581 break;
582 case O_RDONLY:
583 default:
584 fprintf(stderr, "[error] Incorrect open flags.\n");
585 goto error;
586 }
587
588 return &pos->trace_descriptor;
589 error:
590 g_free(pos);
591 return NULL;
592 }
593
594 static
595 int ctf_text_close_trace(struct trace_descriptor *td)
596 {
597 int ret;
598 struct ctf_text_stream_pos *pos =
599 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
600 ret = fclose(pos->fp);
601 if (ret) {
602 perror("Error on fclose");
603 return -1;
604 }
605 g_free(pos);
606 return 0;
607 }
608
609 static
610 void __attribute__((constructor)) ctf_text_init(void)
611 {
612 int ret;
613
614 ctf_text_format.name = g_quark_from_static_string("text");
615 ret = bt_register_format(&ctf_text_format);
616 assert(!ret);
617 }
618
619 static
620 void __attribute__((destructor)) ctf_text_exit(void)
621 {
622 bt_unregister_format(&ctf_text_format);
623 }
This page took 0.040399 seconds and 3 git commands to generate.