| 1 | /* |
| 2 | * babeltrace.c |
| 3 | * |
| 4 | * Babeltrace Trace Converter |
| 5 | * |
| 6 | * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation |
| 7 | * |
| 8 | * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
| 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. |
| 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. |
| 27 | */ |
| 28 | |
| 29 | #include <babeltrace/babeltrace.h> |
| 30 | #include <babeltrace/plugin/component-factory.h> |
| 31 | #include <babeltrace/plugin/plugin.h> |
| 32 | #include <babeltrace/plugin/component-class.h> |
| 33 | #include <babeltrace/plugin/notification/iterator.h> |
| 34 | #include <babeltrace/ref.h> |
| 35 | #include <babeltrace/values.h> |
| 36 | #include <stdlib.h> |
| 37 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
| 38 | #include <popt.h> |
| 39 | #include <string.h> |
| 40 | #include <stdio.h> |
| 41 | |
| 42 | |
| 43 | static char *opt_plugin_path; |
| 44 | static char *opt_input_path; |
| 45 | |
| 46 | enum { |
| 47 | OPT_NONE = 0, |
| 48 | OPT_PLUGIN_PATH, |
| 49 | OPT_INPUT_PATH, |
| 50 | OPT_VERBOSE, |
| 51 | OPT_DEBUG, |
| 52 | OPT_HELP, |
| 53 | }; |
| 54 | |
| 55 | /* |
| 56 | * We are _not_ using POPT_ARG_STRING's ability to store directly into |
| 57 | * variables, because we want to cast the return to non-const, which is |
| 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. |
| 62 | */ |
| 63 | static struct poptOption long_options[] = { |
| 64 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 65 | { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL }, |
| 66 | { "input-path", 0, POPT_ARG_STRING, NULL, OPT_INPUT_PATH, NULL, NULL }, |
| 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 | |
| 72 | /* |
| 73 | * Return 0 if caller should continue, < 0 if caller should return |
| 74 | * error, > 0 if caller should exit without reporting error. |
| 75 | */ |
| 76 | static int parse_options(int argc, char **argv) |
| 77 | { |
| 78 | poptContext pc; |
| 79 | int opt, ret = 0; |
| 80 | |
| 81 | if (argc == 1) { |
| 82 | return 1; /* exit cleanly */ |
| 83 | } |
| 84 | |
| 85 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 86 | poptReadDefaultConfig(pc, 0); |
| 87 | |
| 88 | /* set default */ |
| 89 | opt_context_field_names = 1; |
| 90 | opt_payload_field_names = 1; |
| 91 | |
| 92 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 93 | switch (opt) { |
| 94 | case OPT_HELP: |
| 95 | ret = 1; /* exit cleanly */ |
| 96 | goto end; |
| 97 | case OPT_PLUGIN_PATH: |
| 98 | opt_plugin_path = (char *) poptGetOptArg(pc); |
| 99 | if (!opt_plugin_path) { |
| 100 | ret = -EINVAL; |
| 101 | goto end; |
| 102 | } |
| 103 | break; |
| 104 | case OPT_VERBOSE: |
| 105 | babeltrace_verbose = 1; |
| 106 | break; |
| 107 | case OPT_DEBUG: |
| 108 | babeltrace_debug = 1; |
| 109 | break; |
| 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; |
| 117 | default: |
| 118 | ret = -EINVAL; |
| 119 | goto end; |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | end: |
| 124 | if (pc) { |
| 125 | poptFreeContext(pc); |
| 126 | } |
| 127 | return ret; |
| 128 | } |
| 129 | |
| 130 | static |
| 131 | const 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 | |
| 146 | static |
| 147 | void print_found_component_classes(struct bt_component_factory *factory) |
| 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"); |
| 191 | |
| 192 | bt_put(plugin); |
| 193 | bt_put(component_class); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | int main(int argc, char **argv) |
| 198 | { |
| 199 | int ret; |
| 200 | enum bt_value_status value_status; |
| 201 | struct bt_component_factory *component_factory = NULL; |
| 202 | struct bt_component_class *source_class = NULL, *sink_class = NULL; |
| 203 | struct bt_component *source = NULL, *sink = NULL; |
| 204 | struct bt_value *source_params = NULL, *sink_params = NULL; |
| 205 | struct bt_notification_iterator *it = NULL; |
| 206 | |
| 207 | ret = parse_options(argc, argv); |
| 208 | if (ret < 0) { |
| 209 | fprintf(stderr, "Error parsing options.\n\n"); |
| 210 | exit(EXIT_FAILURE); |
| 211 | } else if (ret > 0) { |
| 212 | exit(EXIT_SUCCESS); |
| 213 | } |
| 214 | |
| 215 | source_params = bt_value_map_create(); |
| 216 | if (!source_params) { |
| 217 | fprintf(stderr, "Failed to create source parameters map, aborting...\n"); |
| 218 | ret = -1; |
| 219 | goto end; |
| 220 | } |
| 221 | |
| 222 | value_status = bt_value_map_insert_string(source_params, "path", |
| 223 | opt_input_path); |
| 224 | if (value_status != BT_VALUE_STATUS_OK) { |
| 225 | ret = -1; |
| 226 | goto end; |
| 227 | } |
| 228 | |
| 229 | printf_verbose("Verbose mode active.\n"); |
| 230 | printf_debug("Debug mode active.\n"); |
| 231 | |
| 232 | if (!opt_plugin_path) { |
| 233 | fprintf(stderr, "No plugin path specified, aborting...\n"); |
| 234 | ret = -1; |
| 235 | goto end; |
| 236 | } |
| 237 | printf_verbose("Looking-up plugins at %s\n", |
| 238 | opt_plugin_path ? opt_plugin_path : "Invalid"); |
| 239 | component_factory = bt_component_factory_create(); |
| 240 | if (!component_factory) { |
| 241 | fprintf(stderr, "Failed to create component factory.\n"); |
| 242 | ret = -1; |
| 243 | goto end; |
| 244 | } |
| 245 | |
| 246 | ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path); |
| 247 | if (ret) { |
| 248 | fprintf(stderr, "Failed to load plugins.\n"); |
| 249 | goto end; |
| 250 | } |
| 251 | |
| 252 | print_found_component_classes(component_factory); |
| 253 | |
| 254 | source_class = bt_component_factory_get_component_class( |
| 255 | component_factory, "ctf", BT_COMPONENT_TYPE_SOURCE, |
| 256 | "fs"); |
| 257 | if (!source_class) { |
| 258 | fprintf(stderr, "Could not find ctf-fs output component class. Aborting...\n"); |
| 259 | ret = -1; |
| 260 | goto end; |
| 261 | } |
| 262 | |
| 263 | sink_class = bt_component_factory_get_component_class(component_factory, |
| 264 | "text", BT_COMPONENT_TYPE_SINK, "text"); |
| 265 | if (!sink_class) { |
| 266 | fprintf(stderr, "Could not find text output component class. Aborting...\n"); |
| 267 | ret = -1; |
| 268 | goto end; |
| 269 | } |
| 270 | |
| 271 | source = bt_component_create(source_class, "ctf-fs", source_params); |
| 272 | if (!source) { |
| 273 | fprintf(stderr, "Failed to instantiate source component. Aborting...\n"); |
| 274 | ret = -1; |
| 275 | goto end; |
| 276 | } |
| 277 | |
| 278 | sink = bt_component_create(sink_class, "bt_text_output", sink_params); |
| 279 | if (!sink) { |
| 280 | fprintf(stderr, "Failed to instantiate output component. Aborting...\n"); |
| 281 | ret = -1; |
| 282 | goto end; |
| 283 | } |
| 284 | |
| 285 | it = bt_component_source_create_iterator(source); |
| 286 | if (!it) { |
| 287 | fprintf(stderr, "Failed to instantiate source iterator. Aborting...\n"); |
| 288 | ret = -1; |
| 289 | goto end; |
| 290 | } |
| 291 | |
| 292 | do { |
| 293 | enum bt_component_status sink_status; |
| 294 | struct bt_notification *notification = |
| 295 | bt_notification_iterator_get_notification(it); |
| 296 | |
| 297 | if (!notification) { |
| 298 | /* |
| 299 | * Should never happen in final code except after next |
| 300 | * has returned BT_NOTIFICATION_ITERATOR_STATUS_END. |
| 301 | * |
| 302 | * Right now it happens at the first event since the |
| 303 | * iterator is not completely initialized and we don't |
| 304 | * have a notification "heap" in place. |
| 305 | */ |
| 306 | continue; |
| 307 | } |
| 308 | |
| 309 | sink_status = bt_component_sink_handle_notification(sink, |
| 310 | notification); |
| 311 | BT_PUT(notification); |
| 312 | if (sink_status != BT_COMPONENT_STATUS_OK) { |
| 313 | fprintf(stderr, "Sink component returned an error, aborting...\n"); |
| 314 | break; |
| 315 | } |
| 316 | } while (bt_notification_iterator_next(it) == |
| 317 | BT_NOTIFICATION_ITERATOR_STATUS_OK); |
| 318 | /* teardown and exit */ |
| 319 | end: |
| 320 | BT_PUT(component_factory); |
| 321 | BT_PUT(sink_class); |
| 322 | BT_PUT(source_class); |
| 323 | BT_PUT(source); |
| 324 | BT_PUT(sink); |
| 325 | BT_PUT(source_params); |
| 326 | BT_PUT(sink_params); |
| 327 | BT_PUT(it); |
| 328 | return ret ? 1 : 0; |
| 329 | } |