Fix libadd referring to text plugin
[babeltrace.git] / converter / babeltrace.c
CommitLineData
34ac0e6c
MD
1/*
2 * babeltrace.c
3 *
4 * Babeltrace Trace Converter
5 *
64fa3fec
MD
6 * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
7 *
8 * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
34ac0e6c
MD
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.
c462e188
MD
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.
34ac0e6c 27 */
4c8bfb7e 28
95d36295 29#include <babeltrace/babeltrace.h>
33bceaf8 30#include <babeltrace/plugin/component-factory.h>
7c7c0433
JG
31#include <babeltrace/plugin/plugin.h>
32#include <babeltrace/plugin/component-class.h>
90298357 33#include <babeltrace/plugin/notification/iterator.h>
2e339de1
JG
34#include <babeltrace/ref.h>
35#include <babeltrace/values.h>
34ac0e6c 36#include <stdlib.h>
a44bc4c9 37#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
7f26a816
PP
38#include <popt.h>
39#include <string.h>
40#include <stdio.h>
a44bc4c9 41
34ac0e6c 42
33bceaf8 43static char *opt_plugin_path;
56a1cced 44static char *opt_input_path;
34ac0e6c 45
34ac0e6c
MD
46enum {
47 OPT_NONE = 0,
33bceaf8 48 OPT_PLUGIN_PATH,
56a1cced 49 OPT_INPUT_PATH,
34ac0e6c
MD
50 OPT_VERBOSE,
51 OPT_DEBUG,
7f26a816 52 OPT_HELP,
34ac0e6c
MD
53};
54
9fe26ec7 55/*
33bceaf8 56 * We are _not_ using POPT_ARG_STRING's ability to store directly into
9fe26ec7 57 * variables, because we want to cast the return to non-const, which is
41075e78
MD
58 * not possible without using poptGetOptArg explicitly. This helps us
59 * controlling memory allocation correctly without making assumptions
60 * about undocumented behaviors. poptGetOptArg is documented as
61 * requiring the returned const char * to be freed by the caller.
9fe26ec7 62 */
34ac0e6c
MD
63static struct poptOption long_options[] = {
64 /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
33bceaf8 65 { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
56a1cced 66 { "input-path", 0, POPT_ARG_STRING, NULL, OPT_INPUT_PATH, NULL, NULL },
34ac0e6c
MD
67 { "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
68 { "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
69 { NULL, 0, 0, NULL, 0, NULL, NULL },
70};
71
34ac0e6c
MD
72/*
73 * Return 0 if caller should continue, < 0 if caller should return
74 * error, > 0 if caller should exit without reporting error.
75 */
bbefb8dd 76static int parse_options(int argc, char **argv)
34ac0e6c
MD
77{
78 poptContext pc;
79 int opt, ret = 0;
80
0f980a35 81 if (argc == 1) {
0f980a35
MD
82 return 1; /* exit cleanly */
83 }
84
bbefb8dd 85 pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
34ac0e6c
MD
86 poptReadDefaultConfig(pc, 0);
87
cba1661c 88 /* set default */
e505794f 89 opt_context_field_names = 1;
cba1661c
MD
90 opt_payload_field_names = 1;
91
34ac0e6c
MD
92 while ((opt = poptGetNextOpt(pc)) != -1) {
93 switch (opt) {
94 case OPT_HELP:
34ac0e6c
MD
95 ret = 1; /* exit cleanly */
96 goto end;
33bceaf8
JG
97 case OPT_PLUGIN_PATH:
98 opt_plugin_path = (char *) poptGetOptArg(pc);
99 if (!opt_plugin_path) {
100 ret = -EINVAL;
101 goto end;
56a1cced 102 }
33bceaf8 103 break;
34ac0e6c
MD
104 case OPT_VERBOSE:
105 babeltrace_verbose = 1;
106 break;
107 case OPT_DEBUG:
108 babeltrace_debug = 1;
109 break;
56a1cced
JG
110 case OPT_INPUT_PATH:
111 opt_input_path = (char *) poptGetOptArg(pc);
112 if (!opt_input_path) {
113 ret = -EINVAL;
114 goto end;
115 }
116 break;
34ac0e6c
MD
117 default:
118 ret = -EINVAL;
119 goto end;
120 }
121 }
122
34ac0e6c
MD
123end:
124 if (pc) {
125 poptFreeContext(pc);
126 }
127 return ret;
128}
129
7c7c0433
JG
130static
131const char *component_type_str(enum bt_component_type type)
132{
133 switch (type) {
134 case BT_COMPONENT_TYPE_SOURCE:
135 return "source";
136 case BT_COMPONENT_TYPE_SINK:
137 return "sink";
138 case BT_COMPONENT_TYPE_FILTER:
139 return "filter";
140 case BT_COMPONENT_TYPE_UNKNOWN:
141 default:
142 return "unknown";
143 }
144}
145
146static
56a1cced 147void print_found_component_classes(struct bt_component_factory *factory)
7c7c0433
JG
148{
149 int count, i;
150
151 if (!babeltrace_verbose) {
152 return;
153 }
154
155 count = bt_component_factory_get_component_class_count(factory);
156 if (count <= 0) {
157 fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.");
158 return;
159 }
160
161 printf_verbose("Found %d component classes.\n", count);
162 for (i = 0; i < count; i++) {
163 struct bt_component_class *component_class =
164 bt_component_factory_get_component_class_index(
165 factory, i);
166 struct bt_plugin *plugin = bt_component_class_get_plugin(
167 component_class);
168 const char *plugin_name = bt_plugin_get_name(plugin);
169 const char *component_name = bt_component_class_get_name(
170 component_class);
171 const char *path = bt_plugin_get_path(plugin);
172 const char *author = bt_plugin_get_author(plugin);
173 const char *license = bt_plugin_get_license(plugin);
174 const char *plugin_description = bt_plugin_get_description(
175 plugin);
176 const char *component_description =
177 bt_component_class_get_description(
178 component_class);
179 enum bt_component_type type = bt_component_class_get_type(
180 component_class);
181
182 printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
183 component_type_str(type));
184 printf_verbose("\tpath: %s\n", path);
185 printf_verbose("\tauthor: %s\n", author);
186 printf_verbose("\tlicense: %s\n", license);
187 printf_verbose("\tplugin description: %s\n",
188 plugin_description ? plugin_description : "None");
189 printf_verbose("\tcomponent description: %s\n",
190 component_description ? component_description : "None");
0cc9e945
JG
191
192 bt_put(plugin);
193 bt_put(component_class);
7c7c0433
JG
194 }
195}
196
bbefb8dd 197int main(int argc, char **argv)
34ac0e6c 198{
7f26a816 199 int ret;
56a1cced 200 enum bt_value_status value_status;
7c7c0433 201 struct bt_component_factory *component_factory = NULL;
56a1cced 202 struct bt_component_class *source_class = NULL, *sink_class = NULL;
7c7c0433
JG
203 struct bt_component *source = NULL, *sink = NULL;
204 struct bt_value *source_params = NULL, *sink_params = NULL;
90298357 205 struct bt_notification_iterator *it = NULL;
fec2a9f2 206 enum bt_component_status sink_status;
34ac0e6c
MD
207
208 ret = parse_options(argc, argv);
209 if (ret < 0) {
3394d22e 210 fprintf(stderr, "Error parsing options.\n\n");
34ac0e6c
MD
211 exit(EXIT_FAILURE);
212 } else if (ret > 0) {
213 exit(EXIT_SUCCESS);
214 }
7f26a816 215
56a1cced
JG
216 source_params = bt_value_map_create();
217 if (!source_params) {
218 fprintf(stderr, "Failed to create source parameters map, aborting...\n");
219 ret = -1;
220 goto end;
221 }
222
223 value_status = bt_value_map_insert_string(source_params, "path",
224 opt_input_path);
225 if (value_status != BT_VALUE_STATUS_OK) {
226 ret = -1;
227 goto end;
228 }
229
34ac0e6c
MD
230 printf_verbose("Verbose mode active.\n");
231 printf_debug("Debug mode active.\n");
232
33bceaf8
JG
233 if (!opt_plugin_path) {
234 fprintf(stderr, "No plugin path specified, aborting...\n");
235 ret = -1;
236 goto end;
237 }
7c7c0433 238 printf_verbose("Looking-up plugins at %s\n",
33bceaf8
JG
239 opt_plugin_path ? opt_plugin_path : "Invalid");
240 component_factory = bt_component_factory_create();
241 if (!component_factory) {
242 fprintf(stderr, "Failed to create component factory.\n");
243 ret = -1;
244 goto end;
245 }
246
01c34eb9 247 ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path);
33bceaf8
JG
248 if (ret) {
249 fprintf(stderr, "Failed to load plugins.\n");
250 goto end;
251 }
252
56a1cced
JG
253 print_found_component_classes(component_factory);
254
255 source_class = bt_component_factory_get_component_class(
256 component_factory, "ctf", BT_COMPONENT_TYPE_SOURCE,
257 "fs");
258 if (!source_class) {
043e2020 259 fprintf(stderr, "Could not find ctf-fs source component class. Aborting...\n");
56a1cced
JG
260 ret = -1;
261 goto end;
262 }
7c7c0433
JG
263
264 sink_class = bt_component_factory_get_component_class(component_factory,
56a1cced 265 "text", BT_COMPONENT_TYPE_SINK, "text");
7c7c0433 266 if (!sink_class) {
043e2020 267 fprintf(stderr, "Could not find text sink component class. Aborting...\n");
7c7c0433
JG
268 ret = -1;
269 goto end;
270 }
271
56a1cced
JG
272 source = bt_component_create(source_class, "ctf-fs", source_params);
273 if (!source) {
274 fprintf(stderr, "Failed to instantiate source component. Aborting...\n");
275 ret = -1;
276 goto end;
277 }
278
7c7c0433
JG
279 sink = bt_component_create(sink_class, "bt_text_output", sink_params);
280 if (!sink) {
56a1cced 281 fprintf(stderr, "Failed to instantiate output component. Aborting...\n");
7c7c0433 282 ret = -1;
2e339de1
JG
283 goto end;
284 }
285
90298357
JG
286 it = bt_component_source_create_iterator(source);
287 if (!it) {
288 fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n");
289 ret = -1;
290 goto end;
291 }
292
fec2a9f2
JG
293 sink_status = bt_component_sink_add_iterator(sink, it);
294 if (sink_status != BT_COMPONENT_STATUS_OK) {
295 ret = -1;
296 goto end;
297 }
78586d8a 298
fec2a9f2
JG
299 while (true) {
300 sink_status = bt_component_sink_consume(sink);
301
302 switch (sink_status) {
303 case BT_COMPONENT_STATUS_AGAIN:
304 /* Wait for an arbitraty 500 ms. */
305 usleep(500000);
78586d8a 306 break;
fec2a9f2
JG
307 case BT_COMPONENT_STATUS_OK:
308 break;
309 case BT_COMPONENT_STATUS_END:
310 goto end;
311 default:
312 fprintf(stderr, "Sink component returned an error, aborting...\n");
313 ret = -1;
314 goto end;
78586d8a 315 }
fec2a9f2 316 }
11e1d048
MD
317 /* teardown and exit */
318end:
2e339de1 319 BT_PUT(component_factory);
7c7c0433
JG
320 BT_PUT(sink_class);
321 BT_PUT(source_class);
322 BT_PUT(source);
323 BT_PUT(sink);
324 BT_PUT(source_params);
325 BT_PUT(sink_params);
90298357 326 BT_PUT(it);
7f26a816 327 return ret ? 1 : 0;
4c8bfb7e 328}
This page took 0.054061 seconds and 4 git commands to generate.