cleanup: protected -> hidden: cleanup symbol table
[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_loglevel_field,
50 opt_delta_field = 1;
51
52 enum field_item {
53 ITEM_SCOPE,
54 ITEM_HEADER,
55 ITEM_CONTEXT,
56 ITEM_PAYLOAD,
57 };
58
59 enum 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
78 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
79 void (*packet_seek)(struct stream_pos *pos, size_t index,
80 int whence), FILE *metadata_fp);
81 void ctf_text_close_trace(struct trace_descriptor *descriptor);
82
83 static
84 rw_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
95 static
96 struct format ctf_text_format = {
97 .open_trace = ctf_text_open_trace,
98 .close_trace = ctf_text_close_trace,
99 };
100
101 static 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
107 static
108 void __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
117 int 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
138 static
139 void 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
172 static
173 const 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
213 static
214 int ctf_text_write_event(struct stream_pos *ppos, struct ctf_stream_definition *stream)
215
216 {
217 struct ctf_text_stream_pos *pos =
218 container_of(ppos, struct ctf_text_stream_pos, parent);
219 struct ctf_stream_declaration *stream_class = stream->stream_class;
220 int field_nr_saved;
221 struct ctf_event_declaration *event_class;
222 struct ctf_event_definition *event;
223 uint64_t id;
224 int ret;
225 int dom_print = 0;
226
227 id = stream->event_id;
228
229 if (id >= stream_class->events_by_id->len) {
230 fprintf(stderr, "[error] Event id %" PRIu64 " is outside range.\n", id);
231 return -EINVAL;
232 }
233 event = g_ptr_array_index(stream->events_by_id, id);
234 if (!event) {
235 fprintf(stderr, "[error] Event id %" PRIu64 " is unknown.\n", id);
236 return -EINVAL;
237 }
238 event_class = g_ptr_array_index(stream_class->events_by_id, id);
239 if (!event_class) {
240 fprintf(stderr, "[error] Event class id %" PRIu64 " is unknown.\n", id);
241 return -EINVAL;
242 }
243
244 /* Print events discarded */
245 if (stream->events_discarded) {
246 fflush(pos->fp);
247 fprintf(stderr, "[warning] Tracer discarded %" PRIu64 " events between [",
248 stream->events_discarded);
249 if (opt_clock_cycles) {
250 ctf_print_timestamp(stderr, stream,
251 stream->prev_cycles_timestamp);
252 fprintf(stderr, "] and [");
253 ctf_print_timestamp(stderr, stream,
254 stream->prev_cycles_timestamp_end);
255 } else {
256 ctf_print_timestamp(stderr, stream,
257 stream->prev_real_timestamp);
258 fprintf(stderr, "] and [");
259 ctf_print_timestamp(stderr, stream,
260 stream->prev_real_timestamp_end);
261 }
262 fprintf(stderr, "]. You should consider recording a new trace with larger buffers or with fewer events enabled.\n");
263 fflush(stderr);
264 stream->events_discarded = 0;
265 }
266
267 if (stream->has_timestamp) {
268 set_field_names_print(pos, ITEM_HEADER);
269 if (pos->print_names)
270 fprintf(pos->fp, "timestamp = ");
271 else
272 fprintf(pos->fp, "[");
273 if (opt_clock_cycles) {
274 ctf_print_timestamp(pos->fp, stream, stream->cycles_timestamp);
275 } else {
276 ctf_print_timestamp(pos->fp, stream, stream->real_timestamp);
277 }
278 if (!pos->print_names)
279 fprintf(pos->fp, "]");
280
281 if (pos->print_names)
282 fprintf(pos->fp, ", ");
283 else
284 fprintf(pos->fp, " ");
285 }
286 if ((opt_delta_field || opt_all_fields) && stream->has_timestamp) {
287 uint64_t delta, delta_sec, delta_nsec;
288
289 set_field_names_print(pos, ITEM_HEADER);
290 if (pos->print_names)
291 fprintf(pos->fp, "delta = ");
292 else
293 fprintf(pos->fp, "(");
294 if (pos->last_real_timestamp != -1ULL) {
295 delta = stream->real_timestamp - pos->last_real_timestamp;
296 delta_sec = delta / NSEC_PER_SEC;
297 delta_nsec = delta % NSEC_PER_SEC;
298 fprintf(pos->fp, "+%" PRIu64 ".%09" PRIu64,
299 delta_sec, delta_nsec);
300 } else {
301 fprintf(pos->fp, "+?.?????????");
302 }
303 if (!pos->print_names)
304 fprintf(pos->fp, ")");
305
306 if (pos->print_names)
307 fprintf(pos->fp, ", ");
308 else
309 fprintf(pos->fp, " ");
310 pos->last_real_timestamp = stream->real_timestamp;
311 pos->last_cycles_timestamp = stream->cycles_timestamp;
312 }
313
314 if ((opt_trace_field || opt_all_fields) && stream_class->trace->path[0] != '\0') {
315 set_field_names_print(pos, ITEM_HEADER);
316 if (pos->print_names) {
317 fprintf(pos->fp, "trace = ");
318 }
319 fprintf(pos->fp, "%s", stream_class->trace->path);
320 if (pos->print_names)
321 fprintf(pos->fp, ", ");
322 else
323 fprintf(pos->fp, " ");
324 }
325 if ((opt_trace_domain_field && !opt_all_fields) && stream_class->trace->env.domain[0] != '\0') {
326 set_field_names_print(pos, ITEM_HEADER);
327 if (pos->print_names) {
328 fprintf(pos->fp, "trace:domain = ");
329 }
330 fprintf(pos->fp, "%s", stream_class->trace->env.domain);
331 if (pos->print_names)
332 fprintf(pos->fp, ", ");
333 dom_print = 1;
334 }
335 if ((opt_trace_procname_field && !opt_all_fields) && stream_class->trace->env.procname[0] != '\0') {
336 set_field_names_print(pos, ITEM_HEADER);
337 if (pos->print_names) {
338 fprintf(pos->fp, "trace:procname = ");
339 } else if (dom_print) {
340 fprintf(pos->fp, ":");
341 }
342 fprintf(pos->fp, "%s", stream_class->trace->env.procname);
343 if (pos->print_names)
344 fprintf(pos->fp, ", ");
345 dom_print = 1;
346 }
347 if ((opt_trace_vpid_field && !opt_all_fields) && stream_class->trace->env.vpid != -1) {
348 set_field_names_print(pos, ITEM_HEADER);
349 if (pos->print_names) {
350 fprintf(pos->fp, "trace:vpid = ");
351 } else if (dom_print) {
352 fprintf(pos->fp, ":");
353 }
354 fprintf(pos->fp, "%d", stream_class->trace->env.vpid);
355 if (pos->print_names)
356 fprintf(pos->fp, ", ");
357 dom_print = 1;
358 }
359 if ((opt_loglevel_field || opt_all_fields) && event_class->loglevel != -1) {
360 set_field_names_print(pos, ITEM_HEADER);
361 if (pos->print_names) {
362 fprintf(pos->fp, "loglevel = ");
363 } else if (dom_print) {
364 fprintf(pos->fp, ":");
365 }
366 fprintf(pos->fp, "%s (%d)",
367 print_loglevel(event_class->loglevel),
368 event_class->loglevel);
369 if (pos->print_names)
370 fprintf(pos->fp, ", ");
371 dom_print = 1;
372 }
373 if (dom_print && !pos->print_names)
374 fprintf(pos->fp, " ");
375 set_field_names_print(pos, ITEM_HEADER);
376 if (pos->print_names)
377 fprintf(pos->fp, "name = ");
378 fprintf(pos->fp, "%s", g_quark_to_string(event_class->name));
379 if (pos->print_names)
380 pos->field_nr++;
381 else
382 fprintf(pos->fp, ":");
383
384 /* print cpuid field from packet context */
385 if (stream->stream_packet_context) {
386 if (pos->field_nr++ != 0)
387 fprintf(pos->fp, ",");
388 set_field_names_print(pos, ITEM_SCOPE);
389 if (pos->print_names)
390 fprintf(pos->fp, " stream.packet.context =");
391 field_nr_saved = pos->field_nr;
392 pos->field_nr = 0;
393 set_field_names_print(pos, ITEM_CONTEXT);
394 ret = generic_rw(ppos, &stream->stream_packet_context->p);
395 if (ret)
396 goto error;
397 pos->field_nr = field_nr_saved;
398 }
399
400 /* Only show the event header in verbose mode */
401 if (babeltrace_verbose && stream->stream_event_header) {
402 if (pos->field_nr++ != 0)
403 fprintf(pos->fp, ",");
404 set_field_names_print(pos, ITEM_SCOPE);
405 if (pos->print_names)
406 fprintf(pos->fp, " stream.event.header =");
407 field_nr_saved = pos->field_nr;
408 pos->field_nr = 0;
409 set_field_names_print(pos, ITEM_CONTEXT);
410 ret = generic_rw(ppos, &stream->stream_event_header->p);
411 if (ret)
412 goto error;
413 pos->field_nr = field_nr_saved;
414 }
415
416 /* print stream-declared event context */
417 if (stream->stream_event_context) {
418 if (pos->field_nr++ != 0)
419 fprintf(pos->fp, ",");
420 set_field_names_print(pos, ITEM_SCOPE);
421 if (pos->print_names)
422 fprintf(pos->fp, " stream.event.context =");
423 field_nr_saved = pos->field_nr;
424 pos->field_nr = 0;
425 set_field_names_print(pos, ITEM_CONTEXT);
426 ret = generic_rw(ppos, &stream->stream_event_context->p);
427 if (ret)
428 goto error;
429 pos->field_nr = field_nr_saved;
430 }
431
432 /* print event-declared event context */
433 if (event->event_context) {
434 if (pos->field_nr++ != 0)
435 fprintf(pos->fp, ",");
436 set_field_names_print(pos, ITEM_SCOPE);
437 if (pos->print_names)
438 fprintf(pos->fp, " event.context =");
439 field_nr_saved = pos->field_nr;
440 pos->field_nr = 0;
441 set_field_names_print(pos, ITEM_CONTEXT);
442 ret = generic_rw(ppos, &event->event_context->p);
443 if (ret)
444 goto error;
445 pos->field_nr = field_nr_saved;
446 }
447
448 /* Read and print event payload */
449 if (event->event_fields) {
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, " event.fields =");
455 field_nr_saved = pos->field_nr;
456 pos->field_nr = 0;
457 set_field_names_print(pos, ITEM_PAYLOAD);
458 ret = generic_rw(ppos, &event->event_fields->p);
459 if (ret)
460 goto error;
461 pos->field_nr = field_nr_saved;
462 }
463 /* newline */
464 fprintf(pos->fp, "\n");
465 pos->field_nr = 0;
466
467 return 0;
468
469 error:
470 fprintf(stderr, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
471 return ret;
472 }
473
474
475 struct trace_descriptor *ctf_text_open_trace(const char *path, int flags,
476 void (*packet_seek)(struct stream_pos *pos, size_t index,
477 int whence), FILE *metadata_fp)
478 {
479 struct ctf_text_stream_pos *pos;
480 FILE *fp;
481
482 pos = g_new0(struct ctf_text_stream_pos, 1);
483
484 pos->last_real_timestamp = -1ULL;
485 pos->last_cycles_timestamp = -1ULL;
486 switch (flags & O_ACCMODE) {
487 case O_RDWR:
488 if (!path)
489 fp = stdout;
490 else
491 fp = fopen(path, "w");
492 if (!fp)
493 goto error;
494 pos->fp = fp;
495 pos->parent.rw_table = write_dispatch_table;
496 pos->parent.event_cb = ctf_text_write_event;
497 pos->print_names = 0;
498 break;
499 case O_RDONLY:
500 default:
501 fprintf(stderr, "[error] Incorrect open flags.\n");
502 goto error;
503 }
504
505 return &pos->trace_descriptor;
506 error:
507 g_free(pos);
508 return NULL;
509 }
510
511 void ctf_text_close_trace(struct trace_descriptor *td)
512 {
513 struct ctf_text_stream_pos *pos =
514 container_of(td, struct ctf_text_stream_pos, trace_descriptor);
515 fclose(pos->fp);
516 g_free(pos);
517 }
518
519 void __attribute__((constructor)) ctf_text_init(void)
520 {
521 int ret;
522
523 ctf_text_format.name = g_quark_from_static_string("text");
524 ret = bt_register_format(&ctf_text_format);
525 assert(!ret);
526 }
527
528 /* TODO: finalize */
This page took 0.038909 seconds and 4 git commands to generate.