Provide sane defaults
[babeltrace.git] / converter / babeltrace-lib.c
CommitLineData
46322b33
MD
1/*
2 * babeltrace-lib.c
3 *
4 * Babeltrace Trace Converter Library
5 *
6 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining a copy
9 * of this software and associated documentation files (the "Software"), to deal
10 * in the Software without restriction, including without limitation the rights
11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12 * copies of the Software, and to permit persons to whom the Software is
13 * furnished to do so, subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
17 */
18
19#include <stdlib.h>
20#include <errno.h>
21#include <stdio.h>
847bf71a 22#include <inttypes.h>
46322b33
MD
23#include <babeltrace/babeltrace.h>
24#include <babeltrace/format.h>
25#include <babeltrace/ctf/types.h>
26#include <babeltrace/ctf/metadata.h>
27#include <babeltrace/ctf-text/types.h>
28
29static
847bf71a
MD
30int convert_event(struct ctf_text_stream_pos *sout,
31 struct ctf_file_stream *sin)
46322b33
MD
32{
33 struct ctf_stream *stream_class = sin->stream;
34 struct ctf_event *event_class;
35 uint64_t id = 0;
36 int len_index;
37
847bf71a
MD
38 if (sin->pos.offset == -EOF)
39 return -EOF;
40
c8b219a3
MD
41 /* Hide event payload struct brackets */
42 sout->depth = -1;
43
46322b33
MD
44 /* Read and print event header */
45 if (stream_class->event_header) {
847bf71a 46 generic_rw(&sin->pos.parent, &stream_class->event_header->p);
46322b33
MD
47
48 /* lookup event id */
847bf71a 49 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
46322b33
MD
50 g_quark_from_static_string("id"));
51 if (len_index >= 0) {
52 struct definition_integer *defint;
53 struct field *field;
54
55 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
56 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
57 defint = container_of(field->definition, struct definition_integer, p);
58 assert(defint->declaration->signedness == FALSE);
59 id = defint->value._unsigned; /* set id */
60 }
61
847bf71a 62 generic_rw(&sout->parent, &stream_class->event_header->p);
46322b33
MD
63 }
64
65 /* Read and print stream-declared event context */
66 if (stream_class->event_context) {
847bf71a
MD
67 generic_rw(&sin->pos.parent, &stream_class->event_context->p);
68 generic_rw(&sout->parent, &stream_class->event_context->p);
46322b33
MD
69 }
70
71 if (id >= stream_class->events_by_id->len) {
72 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
73 return -EINVAL;
74 }
75 event_class = g_ptr_array_index(stream_class->events_by_id, id);
76 if (!event_class) {
77 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
78 return -EINVAL;
79 }
80
81 /* Read and print event-declared event context */
82 if (event_class->context) {
847bf71a
MD
83 generic_rw(&sin->pos.parent, &event_class->context->p);
84 generic_rw(&sout->parent, &event_class->context->p);
46322b33
MD
85 }
86
87 /* Read and print event payload */
88 if (event_class->fields) {
847bf71a
MD
89 generic_rw(&sin->pos.parent, &event_class->fields->p);
90 generic_rw(&sout->parent, &event_class->fields->p);
46322b33
MD
91 }
92
93 return 0;
94}
95
96static
847bf71a
MD
97int convert_stream(struct ctf_text_stream_pos *sout,
98 struct ctf_file_stream *sin)
46322b33
MD
99{
100 int ret;
101
102 /* For each event, print header, context, payload */
103 /* TODO: order events by timestamps across streams */
104 for (;;) {
847bf71a 105 ret = convert_event(sout, sin);
46322b33
MD
106 if (ret == -EOF)
107 break;
108 else if (ret) {
109 fprintf(stdout, "[error] Printing event failed.\n");
110 goto error;
111 }
112 }
113
114 return 0;
115
116error:
117 return ret;
118}
119
847bf71a
MD
120int convert_trace(struct trace_descriptor *td_write,
121 struct trace_descriptor *td_read)
46322b33
MD
122{
123 struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
847bf71a
MD
124 struct ctf_text_stream_pos *sout =
125 container_of(td_write, struct ctf_text_stream_pos, trace_descriptor);
46322b33
MD
126 int stream_id, filenr;
127 int ret;
128
129 /* For each stream (TODO: order events by timestamp) */
130 for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
131 struct ctf_stream *stream = g_ptr_array_index(tin->streams, stream_id);
132
133 if (!stream)
134 continue;
135 for (filenr = 0; filenr < stream->files->len; filenr++) {
136 struct ctf_file_stream *file_stream = g_ptr_array_index(stream->files, filenr);
847bf71a 137 ret = convert_stream(sout, file_stream);
46322b33
MD
138 if (ret) {
139 fprintf(stdout, "[error] Printing stream %d failed.\n", stream_id);
140 goto error;
141 }
142 }
143 }
144
145 return 0;
146
147error:
148 return ret;
149}
This page took 0.029689 seconds and 4 git commands to generate.