7211c97f955889a4d6c15e409f2df0a87b4ae275
[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
21 #include <babeltrace/format.h>
22 #include <babeltrace/ctf-text/types.h>
23 #include <babeltrace/ctf/metadata.h>
24 #include <babeltrace/babeltrace-internal.h>
25 #include <babeltrace/ctf/events-internal.h>
26 #include <inttypes.h>
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
37 #define NSEC_PER_SEC 1000000000ULL
38
39 int opt_all_field_names,
40 opt_scope_field_names,
41 opt_header_field_names,
42 opt_context_field_names,
43 opt_payload_field_names,
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_trace_hostname_field,
50 opt_trace_default_fields = 1,
51 opt_loglevel_field,
52 opt_delta_field = 1;
53
54 enum field_item {
55 ITEM_SCOPE,
56 ITEM_HEADER,
57 ITEM_CONTEXT,
58 ITEM_PAYLOAD,
59 };
60
61 enum 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
80 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
81 void (*packet_seek)(struct stream_pos *pos, size_t index,
82 int whence), FILE *metadata_fp);
83 void ctf_text_close_trace(struct trace_descriptor *descriptor);
84
85 static
86 rw_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
97 static
98 struct format ctf_text_format = {
99 .open_trace = ctf_text_open_trace,
100 .close_trace = ctf_text_close_trace,
101 };
102
103 static 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
109 static
110 void __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
119 int 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
140 static
141 void 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
174 static
175 const 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
215 static
216 int ctf_text_write_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
217
218 {
219 struct ctf_text_stream_pos *pos =
220 container_of(ppos, struct ctf_text_stream_pos, parent);
221 struct ctf_stream_declaration *stream_class = stream->stream_class;
222 int field_nr_saved;
223 struct ctf_event_declaration *event_class;
224 struct ctf_event_definition *event;
225 uint64_t id;
226 int ret;
227 int dom_print = 0;
228
229 id = stream->event_id;
230
231 if (id >= stream_class->events_by_id->len) {
232 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
233 return -EINVAL;
234 }
235 event = g_ptr_array_index(stream->events_by_id, id);
236 if (!event) {
237 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
238 return -EINVAL;
239 }
240 event_class = g_ptr_array_index(stream_class->events_by_id, id);
241 if (!event_class) {
242 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
243 return -EINVAL;
244 }
245
246 /* Print events discarded */
247 if (stream->events_discarded) {
248 fflush(pos->fp);
249 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events between [",
250 stream->events_discarded);
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 }
264 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
265 fflush(stderr);
266 stream->events_discarded = 0;
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 || opt_all_fields) && 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->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->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 (dom_print && !pos->print_names)
389 fprintf(pos->fp, " ");
390 set_field_names_print(pos, ITEM_HEADER);
391 if (pos->print_names)
392 fprintf(pos->fp, "name = ");
393 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
394 if (pos->print_names)
395 pos->field_nr++;
396 else
397 fprintf(pos->fp, ":");
398
399 /* print cpuid field from packet context */
400 if (stream->stream_packet_context) {
401 if (pos->field_nr++ != 0)
402 fprintf(pos->fp, ",");
403 set_field_names_print(pos, ITEM_SCOPE);
404 if (pos->print_names)
405 fprintf(pos->fp, " stream.packet.context =");
406 field_nr_saved = pos->field_nr;
407 pos->field_nr = 0;
408 set_field_names_print(pos, ITEM_CONTEXT);
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
415 /* Only show the event header in verbose mode */
416 if (babeltrace_verbose && stream->stream_event_header) {
417 if (pos->field_nr++ != 0)
418 fprintf(pos->fp, ",");
419 set_field_names_print(pos, ITEM_SCOPE);
420 if (pos->print_names)
421 fprintf(pos->fp, " stream.event.header =");
422 field_nr_saved = pos->field_nr;
423 pos->field_nr = 0;
424 set_field_names_print(pos, ITEM_CONTEXT);
425 ret = generic_rw(ppos, &stream->stream_event_header->p);
426 if (ret)
427 goto error;
428 pos->field_nr = field_nr_saved;
429 }
430
431 /* print stream-declared event context */
432 if (stream->stream_event_context) {
433 if (pos->field_nr++ != 0)
434 fprintf(pos->fp, ",");
435 set_field_names_print(pos, ITEM_SCOPE);
436 if (pos->print_names)
437 fprintf(pos->fp, " stream.event.context =");
438 field_nr_saved = pos->field_nr;
439 pos->field_nr = 0;
440 set_field_names_print(pos, ITEM_CONTEXT);
441 ret = generic_rw(ppos, &stream->stream_event_context->p);
442 if (ret)
443 goto error;
444 pos->field_nr = field_nr_saved;
445 }
446
447 /* print event-declared event context */
448 if (event->event_context) {
449 if (pos->field_nr++ != 0)
450 fprintf(pos->fp, ",");
451 set_field_names_print(pos, ITEM_SCOPE);
452 if (pos->print_names)
453 fprintf(pos->fp, " event.context =");
454 field_nr_saved = pos->field_nr;
455 pos->field_nr = 0;
456 set_field_names_print(pos, ITEM_CONTEXT);
457 ret = generic_rw(ppos, &event->event_context->p);
458 if (ret)
459 goto error;
460 pos->field_nr = field_nr_saved;
461 }
462
463 /* Read and print event payload */
464 if (event->event_fields) {
465 if (pos->field_nr++ != 0)
466 fprintf(pos->fp, ",");
467 set_field_names_print(pos, ITEM_SCOPE);
468 if (pos->print_names)
469 fprintf(pos->fp, " event.fields =");
470 field_nr_saved = pos->field_nr;
471 pos->field_nr = 0;
472 set_field_names_print(pos, ITEM_PAYLOAD);
473 ret = generic_rw(ppos, &event->event_fields->p);
474 if (ret)
475 goto error;
476 pos->field_nr = field_nr_saved;
477 }
478 /* newline */
479 fprintf(pos->fp, "\n");
480 pos->field_nr = 0;
481
482 return 0;
483
484 error:
485 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
486 return ret;
487 }
488
489
490 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
491 void (*packet_seek)(struct stream_pos *pos, size_t index,
492 int whence), FILE *metadata_fp)
493 {
494 struct ctf_text_stream_pos *pos;
495 FILE *fp;
496
497 pos = g_new0(struct ctf_text_stream_pos, 1);
498
499 pos->last_real_timestamp = -1ULL;
500 pos->last_cycles_timestamp = -1ULL;
501 switch (flags & O_ACCMODE) {
502 case O_RDWR:
503 if (!path)
504 fp = stdout;
505 else
506 fp = fopen(path, "w");
507 if (!fp)
508 goto error;
509 pos->fp = fp;
510 pos->parent.rw_table = write_dispatch_table;
511 pos->parent.event_cb = ctf_text_write_event;
512 pos->print_names = 0;
513 break;
514 case O_RDONLY:
515 default:
516 fprintf(stderr, "[error] Incorrect open flags.\n");
517 goto error;
518 }
519
520 return &pos->trace_descriptor;
521 error:
522 g_free(pos);
523 return NULL;
524 }
525
526 void 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
534 void __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.062959 seconds and 4 git commands to generate.