Be more verbose went encountering unexpected EOF
[babeltrace.git] / converter / babeltrace-lib.c
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>
22 #include <inttypes.h>
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
29 static
30 int convert_event(struct ctf_text_stream_pos *sout,
31 struct ctf_file_stream *sin)
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 int ret;
38
39 if (sin->pos.offset == EOF)
40 return EOF;
41
42 /* Hide event payload struct brackets */
43 sout->depth = -1;
44
45 /* Read and print event header */
46 if (stream_class->event_header) {
47 ret = generic_rw(&sin->pos.parent, &stream_class->event_header->p);
48 if (ret)
49 goto error;
50 /* lookup event id */
51 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
52 g_quark_from_static_string("id"));
53 if (len_index >= 0) {
54 struct definition_integer *defint;
55 struct field *field;
56
57 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
58 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
59 defint = container_of(field->definition, struct definition_integer, p);
60 assert(defint->declaration->signedness == FALSE);
61 id = defint->value._unsigned; /* set id */
62 }
63
64 ret = generic_rw(&sout->parent, &stream_class->event_header->p);
65 if (ret)
66 goto error;
67 }
68
69 /* Read and print stream-declared event context */
70 if (stream_class->event_context) {
71 ret = generic_rw(&sin->pos.parent, &stream_class->event_context->p);
72 if (ret)
73 goto error;
74 ret = generic_rw(&sout->parent, &stream_class->event_context->p);
75 if (ret)
76 goto error;
77 }
78
79 if (id >= stream_class->events_by_id->len) {
80 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
81 return -EINVAL;
82 }
83 event_class = g_ptr_array_index(stream_class->events_by_id, id);
84 if (!event_class) {
85 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
86 return -EINVAL;
87 }
88
89 /* Read and print event-declared event context */
90 if (event_class->context) {
91 ret = generic_rw(&sin->pos.parent, &event_class->context->p);
92 if (ret)
93 goto error;
94 ret = generic_rw(&sout->parent, &event_class->context->p);
95 if (ret)
96 goto error;
97 }
98
99 /* Read and print event payload */
100 if (event_class->fields) {
101 ret = generic_rw(&sin->pos.parent, &event_class->fields->p);
102 if (ret)
103 goto error;
104 ret = generic_rw(&sout->parent, &event_class->fields->p);
105 if (ret)
106 goto error;
107 }
108
109 return 0;
110
111 error:
112 fprintf(stdout, "[error] Unexpected end of stream. Either the trace data stream is corrupted or metadata description does not match data layout.\n");
113 return ret;
114 }
115
116 static
117 int convert_stream(struct ctf_text_stream_pos *sout,
118 struct ctf_file_stream *sin)
119 {
120 int ret;
121
122 /* For each event, print header, context, payload */
123 /* TODO: order events by timestamps across streams */
124 for (;;) {
125 ret = convert_event(sout, sin);
126 if (ret == EOF)
127 break;
128 else if (ret) {
129 fprintf(stdout, "[error] Printing event failed.\n");
130 goto error;
131 }
132 }
133
134 return 0;
135
136 error:
137 return ret;
138 }
139
140 int convert_trace(struct trace_descriptor *td_write,
141 struct trace_descriptor *td_read)
142 {
143 struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
144 struct ctf_text_stream_pos *sout =
145 container_of(td_write, struct ctf_text_stream_pos, trace_descriptor);
146 int stream_id, filenr;
147 int ret;
148
149 /* For each stream (TODO: order events by timestamp) */
150 for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
151 struct ctf_stream *stream = g_ptr_array_index(tin->streams, stream_id);
152
153 if (!stream)
154 continue;
155 for (filenr = 0; filenr < stream->files->len; filenr++) {
156 struct ctf_file_stream *file_stream = g_ptr_array_index(stream->files, filenr);
157 ret = convert_stream(sout, file_stream);
158 if (ret) {
159 fprintf(stdout, "[error] Printing stream %d failed.\n", stream_id);
160 goto error;
161 }
162 }
163 }
164
165 return 0;
166
167 error:
168 return ret;
169 }
This page took 0.046515 seconds and 4 git commands to generate.