| 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/ref.h> |
| 34 | #include <babeltrace/values.h> |
| 35 | #include <stdlib.h> |
| 36 | #include <babeltrace/ctf-ir/metadata.h> /* for clocks */ |
| 37 | #include <popt.h> |
| 38 | #include <string.h> |
| 39 | #include <stdio.h> |
| 40 | |
| 41 | |
| 42 | static char *opt_plugin_path; |
| 43 | static char *opt_input_path; |
| 44 | |
| 45 | enum { |
| 46 | OPT_NONE = 0, |
| 47 | OPT_PLUGIN_PATH, |
| 48 | OPT_INPUT_PATH, |
| 49 | OPT_VERBOSE, |
| 50 | OPT_DEBUG, |
| 51 | OPT_HELP, |
| 52 | }; |
| 53 | |
| 54 | /* |
| 55 | * We are _not_ using POPT_ARG_STRING's ability to store directly into |
| 56 | * variables, because we want to cast the return to non-const, which is |
| 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. |
| 61 | */ |
| 62 | static struct poptOption long_options[] = { |
| 63 | /* longName, shortName, argInfo, argPtr, value, descrip, argDesc */ |
| 64 | { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL }, |
| 65 | { "input-path", 0, POPT_ARG_STRING, NULL, OPT_INPUT_PATH, NULL, NULL }, |
| 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 | |
| 71 | /* |
| 72 | * Return 0 if caller should continue, < 0 if caller should return |
| 73 | * error, > 0 if caller should exit without reporting error. |
| 74 | */ |
| 75 | static int parse_options(int argc, char **argv) |
| 76 | { |
| 77 | poptContext pc; |
| 78 | int opt, ret = 0; |
| 79 | |
| 80 | if (argc == 1) { |
| 81 | return 1; /* exit cleanly */ |
| 82 | } |
| 83 | |
| 84 | pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0); |
| 85 | poptReadDefaultConfig(pc, 0); |
| 86 | |
| 87 | /* set default */ |
| 88 | opt_context_field_names = 1; |
| 89 | opt_payload_field_names = 1; |
| 90 | |
| 91 | while ((opt = poptGetNextOpt(pc)) != -1) { |
| 92 | switch (opt) { |
| 93 | case OPT_HELP: |
| 94 | ret = 1; /* exit cleanly */ |
| 95 | goto end; |
| 96 | case OPT_PLUGIN_PATH: |
| 97 | opt_plugin_path = (char *) poptGetOptArg(pc); |
| 98 | if (!opt_plugin_path) { |
| 99 | ret = -EINVAL; |
| 100 | goto end; |
| 101 | } |
| 102 | break; |
| 103 | case OPT_VERBOSE: |
| 104 | babeltrace_verbose = 1; |
| 105 | break; |
| 106 | case OPT_DEBUG: |
| 107 | babeltrace_debug = 1; |
| 108 | break; |
| 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; |
| 116 | default: |
| 117 | ret = -EINVAL; |
| 118 | goto end; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | end: |
| 123 | if (pc) { |
| 124 | poptFreeContext(pc); |
| 125 | } |
| 126 | return ret; |
| 127 | } |
| 128 | |
| 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 |
| 146 | void print_found_component_classes(struct bt_component_factory *factory) |
| 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"); |
| 190 | |
| 191 | bt_put(plugin); |
| 192 | bt_put(component_class); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | int main(int argc, char **argv) |
| 197 | { |
| 198 | int ret; |
| 199 | enum bt_value_status value_status; |
| 200 | struct bt_component_factory *component_factory = NULL; |
| 201 | struct bt_component_class *source_class = NULL, *sink_class = NULL; |
| 202 | struct bt_component *source = NULL, *sink = NULL; |
| 203 | struct bt_value *source_params = NULL, *sink_params = NULL; |
| 204 | |
| 205 | ret = parse_options(argc, argv); |
| 206 | if (ret < 0) { |
| 207 | fprintf(stderr, "Error parsing options.\n\n"); |
| 208 | exit(EXIT_FAILURE); |
| 209 | } else if (ret > 0) { |
| 210 | exit(EXIT_SUCCESS); |
| 211 | } |
| 212 | |
| 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 | |
| 227 | printf_verbose("Verbose mode active.\n"); |
| 228 | printf_debug("Debug mode active.\n"); |
| 229 | |
| 230 | if (!opt_plugin_path) { |
| 231 | fprintf(stderr, "No plugin path specified, aborting...\n"); |
| 232 | ret = -1; |
| 233 | goto end; |
| 234 | } |
| 235 | printf_verbose("Looking-up plugins at %s\n", |
| 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 | |
| 244 | ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path); |
| 245 | if (ret) { |
| 246 | fprintf(stderr, "Failed to load plugins.\n"); |
| 247 | goto end; |
| 248 | } |
| 249 | |
| 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 | } |
| 260 | |
| 261 | sink_class = bt_component_factory_get_component_class(component_factory, |
| 262 | "text", BT_COMPONENT_TYPE_SINK, "text"); |
| 263 | if (!sink_class) { |
| 264 | fprintf(stderr, "Could not find text output component class. Aborting...\n"); |
| 265 | ret = -1; |
| 266 | goto end; |
| 267 | } |
| 268 | |
| 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 | |
| 276 | sink = bt_component_create(sink_class, "bt_text_output", sink_params); |
| 277 | if (!sink) { |
| 278 | fprintf(stderr, "Failed to instantiate output component. Aborting...\n"); |
| 279 | ret = -1; |
| 280 | goto end; |
| 281 | } |
| 282 | |
| 283 | /* teardown and exit */ |
| 284 | end: |
| 285 | BT_PUT(component_factory); |
| 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); |
| 292 | return ret ? 1 : 0; |
| 293 | } |