Commit | Line | Data |
---|---|---|
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 | ||
29 | static | |
847bf71a MD |
30 | int 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; | |
c5e74408 | 37 | int ret; |
46322b33 | 38 | |
670977d3 MD |
39 | if (sin->pos.offset == EOF) |
40 | return EOF; | |
847bf71a | 41 | |
c8b219a3 MD |
42 | /* Hide event payload struct brackets */ |
43 | sout->depth = -1; | |
44 | ||
46322b33 MD |
45 | /* Read and print event header */ |
46 | if (stream_class->event_header) { | |
c5e74408 MD |
47 | ret = generic_rw(&sin->pos.parent, &stream_class->event_header->p); |
48 | if (ret) | |
49 | return ret; | |
46322b33 | 50 | /* lookup event id */ |
847bf71a | 51 | len_index = struct_declaration_lookup_field_index(stream_class->event_header_decl, |
46322b33 MD |
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 | ||
c5e74408 MD |
64 | ret = generic_rw(&sout->parent, &stream_class->event_header->p); |
65 | if (ret) | |
66 | return ret; | |
46322b33 MD |
67 | } |
68 | ||
69 | /* Read and print stream-declared event context */ | |
70 | if (stream_class->event_context) { | |
c5e74408 MD |
71 | ret = generic_rw(&sin->pos.parent, &stream_class->event_context->p); |
72 | if (ret) | |
73 | return ret; | |
74 | ret = generic_rw(&sout->parent, &stream_class->event_context->p); | |
75 | if (ret) | |
76 | return ret; | |
46322b33 MD |
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) { | |
c5e74408 MD |
91 | ret = generic_rw(&sin->pos.parent, &event_class->context->p); |
92 | if (ret) | |
93 | return ret; | |
94 | ret = generic_rw(&sout->parent, &event_class->context->p); | |
95 | if (ret) | |
96 | return ret; | |
46322b33 MD |
97 | } |
98 | ||
99 | /* Read and print event payload */ | |
100 | if (event_class->fields) { | |
c5e74408 MD |
101 | ret = generic_rw(&sin->pos.parent, &event_class->fields->p); |
102 | if (ret) | |
103 | return ret; | |
104 | ret = generic_rw(&sout->parent, &event_class->fields->p); | |
105 | if (ret) | |
106 | return ret; | |
46322b33 MD |
107 | } |
108 | ||
109 | return 0; | |
110 | } | |
111 | ||
112 | static | |
847bf71a MD |
113 | int convert_stream(struct ctf_text_stream_pos *sout, |
114 | struct ctf_file_stream *sin) | |
46322b33 MD |
115 | { |
116 | int ret; | |
117 | ||
118 | /* For each event, print header, context, payload */ | |
119 | /* TODO: order events by timestamps across streams */ | |
120 | for (;;) { | |
847bf71a | 121 | ret = convert_event(sout, sin); |
670977d3 | 122 | if (ret == EOF) |
46322b33 MD |
123 | break; |
124 | else if (ret) { | |
125 | fprintf(stdout, "[error] Printing event failed.\n"); | |
126 | goto error; | |
127 | } | |
128 | } | |
129 | ||
130 | return 0; | |
131 | ||
132 | error: | |
133 | return ret; | |
134 | } | |
135 | ||
847bf71a MD |
136 | int convert_trace(struct trace_descriptor *td_write, |
137 | struct trace_descriptor *td_read) | |
46322b33 MD |
138 | { |
139 | struct ctf_trace *tin = container_of(td_read, struct ctf_trace, parent); | |
847bf71a MD |
140 | struct ctf_text_stream_pos *sout = |
141 | container_of(td_write, struct ctf_text_stream_pos, trace_descriptor); | |
46322b33 MD |
142 | int stream_id, filenr; |
143 | int ret; | |
144 | ||
145 | /* For each stream (TODO: order events by timestamp) */ | |
146 | for (stream_id = 0; stream_id < tin->streams->len; stream_id++) { | |
147 | struct ctf_stream *stream = g_ptr_array_index(tin->streams, stream_id); | |
148 | ||
149 | if (!stream) | |
150 | continue; | |
151 | for (filenr = 0; filenr < stream->files->len; filenr++) { | |
152 | struct ctf_file_stream *file_stream = g_ptr_array_index(stream->files, filenr); | |
847bf71a | 153 | ret = convert_stream(sout, file_stream); |
46322b33 MD |
154 | if (ret) { |
155 | fprintf(stdout, "[error] Printing stream %d failed.\n", stream_id); | |
156 | goto error; | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | return 0; | |
162 | ||
163 | error: | |
164 | return ret; | |
165 | } |