*
* Babeltrace Trace Converter
*
- * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
+ * Copyright 2010-2011 EfficiOS Inc. and Linux Foundation
+ *
+ * Author: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
*/
#include <babeltrace/babeltrace.h>
-#include <babeltrace/format.h>
-#include <popt.h>
-#include <errno.h>
+#include <babeltrace/plugin/component-factory.h>
+#include <babeltrace/plugin/plugin.h>
+#include <babeltrace/plugin/component-class.h>
+#include <babeltrace/ref.h>
+#include <babeltrace/values.h>
#include <stdlib.h>
-#include <ctype.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <fcntl.h>
-
-static char *opt_input_format;
-static char *opt_output_format;
-
-static const char *opt_input_path;
-static const char *opt_output_path;
+#include <babeltrace/ctf-ir/metadata.h> /* for clocks */
+#include <popt.h>
+#include <string.h>
+#include <stdio.h>
-int babeltrace_verbose, babeltrace_debug;
-void strlower(char *str)
-{
- while (*str) {
- *str = tolower(*str);
- str++;
- }
-}
+static char *opt_plugin_path;
enum {
OPT_NONE = 0,
- OPT_HELP,
- OPT_LIST,
+ OPT_PLUGIN_PATH,
OPT_VERBOSE,
OPT_DEBUG,
+ OPT_HELP,
};
+/*
+ * We are _not_ using POPT_ARG_STRING's ability to store directly into
+ * variables, because we want to cast the return to non-const, which is
+ * not possible without using poptGetOptArg explicitly. This helps us
+ * controlling memory allocation correctly without making assumptions
+ * about undocumented behaviors. poptGetOptArg is documented as
+ * requiring the returned const char * to be freed by the caller.
+ */
static struct poptOption long_options[] = {
/* longName, shortName, argInfo, argPtr, value, descrip, argDesc */
- { "input-format", 'i', POPT_ARG_STRING, &opt_input_format, OPT_NONE, NULL, NULL },
- { "output-format", 'o', POPT_ARG_STRING, &opt_output_format, OPT_NONE, NULL, NULL },
- { "help", 'h', POPT_ARG_NONE, NULL, OPT_HELP, NULL, NULL },
- { "list", 'l', POPT_ARG_NONE, NULL, OPT_LIST, NULL, NULL },
+ { "plugin-path", 0, POPT_ARG_STRING, NULL, OPT_PLUGIN_PATH, NULL, NULL },
{ "verbose", 'v', POPT_ARG_NONE, NULL, OPT_VERBOSE, NULL, NULL },
{ "debug", 'd', POPT_ARG_NONE, NULL, OPT_DEBUG, NULL, NULL },
{ NULL, 0, 0, NULL, 0, NULL, NULL },
};
-static void list_formats(FILE *fp)
-{
- fprintf(fp, "\n");
- bt_fprintf_format_list(fp);
-}
-
-static void usage(FILE *fp)
-{
- fprintf(fp, "BabelTrace Trace Converter %u.%u\n\n",
- BABELTRACE_VERSION_MAJOR,
- BABELTRACE_VERSION_MINOR);
- fprintf(fp, "usage : babeltrace [OPTIONS] INPUT OUTPUT\n");
- fprintf(fp, "\n");
- fprintf(fp, " INPUT Input trace path\n");
- fprintf(fp, " OUTPUT Output trace path\n");
- fprintf(fp, "\n");
- fprintf(fp, " -i, --input-format Input trace path\n");
- fprintf(fp, " -o, --output-format Input trace path\n");
- fprintf(fp, "\n");
- fprintf(fp, " -h, --help This help message\n");
- fprintf(fp, " -l, --list List available formats\n");
- fprintf(fp, " -v, --verbose Verbose mode\n");
- fprintf(fp, " -d, --debug Debug mode\n");
- list_formats(fp);
- fprintf(fp, "\n");
-}
-
/*
* Return 0 if caller should continue, < 0 if caller should return
* error, > 0 if caller should exit without reporting error.
int opt, ret = 0;
if (argc == 1) {
- usage(stdout);
return 1; /* exit cleanly */
}
pc = poptGetContext(NULL, argc, (const char **) argv, long_options, 0);
poptReadDefaultConfig(pc, 0);
+ /* set default */
+ opt_context_field_names = 1;
+ opt_payload_field_names = 1;
+
while ((opt = poptGetNextOpt(pc)) != -1) {
switch (opt) {
case OPT_HELP:
- usage(stdout);
ret = 1; /* exit cleanly */
goto end;
- case OPT_LIST:
- list_formats(stdout);
- ret = 1;
- goto end;
+ case OPT_PLUGIN_PATH:
+ opt_plugin_path = (char *) poptGetOptArg(pc);
+ if (!opt_plugin_path) {
+ ret = -EINVAL;
+ goto end;
+ } ;
+ break;
case OPT_VERBOSE:
babeltrace_verbose = 1;
break;
}
}
- opt_input_path = poptGetArg(pc);
- if (!opt_input_path) {
- ret = -EINVAL;
- goto end;
- }
- opt_output_path = poptGetArg(pc);
- if (!opt_output_path) {
- ret = -EINVAL;
- goto end;
- }
end:
if (pc) {
poptFreeContext(pc);
return ret;
}
+static
+const char *component_type_str(enum bt_component_type type)
+{
+ switch (type) {
+ case BT_COMPONENT_TYPE_SOURCE:
+ return "source";
+ case BT_COMPONENT_TYPE_SINK:
+ return "sink";
+ case BT_COMPONENT_TYPE_FILTER:
+ return "filter";
+ case BT_COMPONENT_TYPE_UNKNOWN:
+ default:
+ return "unknown";
+ }
+}
+
+static
+void print_detected_component_classes(struct bt_component_factory *factory)
+{
+ int count, i;
+
+ if (!babeltrace_verbose) {
+ return;
+ }
+
+ count = bt_component_factory_get_component_class_count(factory);
+ if (count <= 0) {
+ fprintf(stderr, "No component classes found. Please make sure your plug-in search path is set correctly.");
+ return;
+ }
+
+ printf_verbose("Found %d component classes.\n", count);
+ for (i = 0; i < count; i++) {
+ struct bt_component_class *component_class =
+ bt_component_factory_get_component_class_index(
+ factory, i);
+ struct bt_plugin *plugin = bt_component_class_get_plugin(
+ component_class);
+ const char *plugin_name = bt_plugin_get_name(plugin);
+ const char *component_name = bt_component_class_get_name(
+ component_class);
+ const char *path = bt_plugin_get_path(plugin);
+ const char *author = bt_plugin_get_author(plugin);
+ const char *license = bt_plugin_get_license(plugin);
+ const char *plugin_description = bt_plugin_get_description(
+ plugin);
+ const char *component_description =
+ bt_component_class_get_description(
+ component_class);
+ enum bt_component_type type = bt_component_class_get_type(
+ component_class);
+
+ printf_verbose("[%s - %s (%s)]\n", plugin_name, component_name,
+ component_type_str(type));
+ printf_verbose("\tpath: %s\n", path);
+ printf_verbose("\tauthor: %s\n", author);
+ printf_verbose("\tlicense: %s\n", license);
+ printf_verbose("\tplugin description: %s\n",
+ plugin_description ? plugin_description : "None");
+ printf_verbose("\tcomponent description: %s\n",
+ component_description ? component_description : "None");
+
+ bt_put(plugin);
+ bt_put(component_class);
+ }
+}
+
+static
+void test_sink_notifications(struct bt_component *sink)
+{
+ return;
+}
+
int main(int argc, char **argv)
{
int ret;
- struct format *fmt_read, *fmt_write;
- struct trace_descriptor *td_read, *td_write;
+ struct bt_component_factory *component_factory = NULL;
+ struct bt_component_class *source_class = NULL;
+ struct bt_component_class *sink_class = NULL;
+ struct bt_component *source = NULL, *sink = NULL;
+ struct bt_value *source_params = NULL, *sink_params = NULL;
ret = parse_options(argc, argv);
if (ret < 0) {
- fprintf(stdout, "Error parsing options.\n\n");
- usage(stdout);
+ fprintf(stderr, "Error parsing options.\n\n");
exit(EXIT_FAILURE);
} else if (ret > 0) {
exit(EXIT_SUCCESS);
}
+
printf_verbose("Verbose mode active.\n");
printf_debug("Debug mode active.\n");
- if (opt_input_format)
- strlower(opt_input_format);
- if (opt_output_format)
- strlower(opt_output_format);
-
- printf_verbose("Converting from file: %s\n", opt_input_path);
- printf_verbose("Converting from format: %s\n",
- opt_input_format ? : "<autodetect>");
- printf_verbose("Converting to file: %s\n", opt_output_path);
- printf_verbose("Converting to format: %s\n",
- opt_output_format ? : "ctf");
-
- if (!opt_input_format) {
- fprintf(stdout, "Error: input format autodetection not implemented yet.\n\n");
- usage(stdout);
- exit(EXIT_FAILURE);
- }
- fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
- if (!fmt_read) {
- fprintf(stdout, "Error: format \"%s\" is not supported.\n\n",
- opt_input_format);
- exit(EXIT_FAILURE);
+ if (!opt_plugin_path) {
+ fprintf(stderr, "No plugin path specified, aborting...\n");
+ ret = -1;
+ goto end;
}
- if (!opt_output_format)
- opt_output_format = "ctf";
- fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
- if (!fmt_write) {
- fprintf(stdout, "Error: format \"%s\" is not supported.\n\n",
- opt_output_format);
- exit(EXIT_FAILURE);
+ printf_verbose("Looking-up plugins at %s\n",
+ opt_plugin_path ? opt_plugin_path : "Invalid");
+ component_factory = bt_component_factory_create();
+ if (!component_factory) {
+ fprintf(stderr, "Failed to create component factory.\n");
+ ret = -1;
+ goto end;
}
- td_read = fmt_read->open_trace(opt_input_path, O_RDONLY);
- if (!td_read) {
- fprintf(stdout, "Error opening trace \"%s\" for reading.\n\n",
- opt_input_path);
- goto error_td_read;
+ ret = bt_component_factory_load_recursive(component_factory, opt_plugin_path);
+ if (ret) {
+ fprintf(stderr, "Failed to load plugins.\n");
+ goto end;
}
- td_write = fmt_write->open_trace(opt_output_path, O_WRONLY);
- if (!td_write) {
- fprintf(stdout, "Error opening trace \"%s\" for writing.\n\n",
- opt_output_path);
- goto error_td_write;
+ print_detected_component_classes(component_factory);
+
+ sink_class = bt_component_factory_get_component_class(component_factory,
+ NULL, BT_COMPONENT_TYPE_SINK, "text");
+ if (!sink_class) {
+ fprintf(stderr, "Could not find text output component class. Aborting...\n");
+ ret = -1;
+ goto end;
}
-#if 0
- ret = print_trace(fmt_write, td_write, fmt_read, td_read);
- if (ret) {
- fprintf(stdout, "Error printing trace.\n\n");
- goto error_copy_trace;
+
+ sink = bt_component_create(sink_class, "bt_text_output", sink_params);
+ if (!sink) {
+ fprintf(stderr, "Failed to instanciate text output. Aborting...\n");
+ ret = -1;
+ goto end;
}
-#endif //0
- fmt_write->close_trace(td_write);
- fmt_read->close_trace(td_read);
- exit(EXIT_SUCCESS);
-
- /* Error handling */
-error_copy_trace:
- fmt_write->close_trace(td_write);
-error_td_write:
- fmt_read->close_trace(td_read);
-error_td_read:
- exit(EXIT_FAILURE);
+
+ test_sink_notifications(sink);
+
+ /* teardown and exit */
+end:
+ BT_PUT(component_factory);
+ BT_PUT(sink_class);
+ BT_PUT(source_class);
+ BT_PUT(source);
+ BT_PUT(sink);
+ BT_PUT(source_params);
+ BT_PUT(sink_params);
+ return ret ? 1 : 0;
}