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