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