5be268ccf4371b22e94a2e28e3d1898ecc009835
[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
38 if (sin->pos.offset == -EOF)
39 return -EOF;
40
41 /* Read and print event header */
42 if (stream_class->event_header) {
43 generic_rw(&sin->pos.parent, &stream_class->event_header->p);
44
45 /* lookup event id */
46 len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl,
47 g_quark_from_static_string("id"));
48 if (len_index >= 0) {
49 struct definition_integer *defint;
50 struct field *field;
51
52 field = struct_definition_get_field_from_index(stream_class->event_header, len_index);
53 assert(field->definition->declaration->id == CTF_TYPE_INTEGER);
54 defint = container_of(field->definition, struct definition_integer, p);
55 assert(defint->declaration->signedness == FALSE);
56 id = defint->value._unsigned; /* set id */
57 }
58
59 generic_rw(&sout->parent, &stream_class->event_header->p);
60 }
61
62 /* Read and print stream-declared event context */
63 if (stream_class->event_context) {
64 generic_rw(&sin->pos.parent, &stream_class->event_context->p);
65 generic_rw(&sout->parent, &stream_class->event_context->p);
66 }
67
68 if (id >= stream_class->events_by_id->len) {
69 fprintf(stdout, "[error] Event id %" PRIu64 " is outside range.\n", id);
70 return -EINVAL;
71 }
72 event_class = g_ptr_array_index(stream_class->events_by_id, id);
73 if (!event_class) {
74 fprintf(stdout, "[error] Event id %" PRIu64 " is unknown.\n", id);
75 return -EINVAL;
76 }
77
78 /* Read and print event-declared event context */
79 if (event_class->context) {
80 generic_rw(&sin->pos.parent, &event_class->context->p);
81 generic_rw(&sout->parent, &event_class->context->p);
82 }
83
84 /* Read and print event payload */
85 if (event_class->fields) {
86 generic_rw(&sin->pos.parent, &event_class->fields->p);
87 generic_rw(&sout->parent, &event_class->fields->p);
88 }
89
90 return 0;
91 }
92
93 static
94 int convert_stream(struct ctf_text_stream_pos *sout,
95 struct ctf_file_stream *sin)
96 {
97 int ret;
98
99 /* For each event, print header, context, payload */
100 /* TODO: order events by timestamps across streams */
101 for (;;) {
102 ret = convert_event(sout, sin);
103 if (ret == -EOF)
104 break;
105 else if (ret) {
106 fprintf(stdout, "[error] Printing event failed.\n");
107 goto error;
108 }
109 }
110
111 return 0;
112
113 error:
114 return ret;
115 }
116
117 int convert_trace(struct trace_descriptor *td_write,
118 struct trace_descriptor *td_read)
119 {
120 struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent);
121 struct ctf_text_stream_pos *sout =
122 container_of(td_write, struct ctf_text_stream_pos, trace_descriptor);
123 int stream_id, filenr;
124 int ret;
125
126 /* For each stream (TODO: order events by timestamp) */
127 for (stream_id = 0; stream_id < tin->streams->len; stream_id++) {
128 struct ctf_stream *stream = g_ptr_array_index(tin->streams, stream_id);
129
130 if (!stream)
131 continue;
132 for (filenr = 0; filenr < stream->files->len; filenr++) {
133 struct ctf_file_stream *file_stream = g_ptr_array_index(stream->files, filenr);
134 ret = convert_stream(sout, file_stream);
135 if (ret) {
136 fprintf(stdout, "[error] Printing stream %d failed.\n", stream_id);
137 goto error;
138 }
139 }
140 }
141
142 return 0;
143
144 error:
145 return ret;
146 }
This page took 0.046523 seconds and 3 git commands to generate.