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