int main(int argc, char **argv)
{
	int ret, partial_error = 0, open_success = 0;
	struct bt_format *fmt_write;
	struct bt_trace_descriptor *td_write;
	struct bt_context *ctx;
	int i;

	opt_input_paths = g_ptr_array_new();

	ret = parse_options(argc, argv);
	if (ret < 0) {
		fprintf(stderr, "Error parsing options.\n\n");
		usage(stderr);
		g_ptr_array_free(opt_input_paths, TRUE);
		exit(EXIT_FAILURE);
	} else if (ret > 0) {
		g_ptr_array_free(opt_input_paths, TRUE);
		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 directory(ies):\n");
	for (i = 0; i < opt_input_paths->len; i++) {
		const char *ipath = g_ptr_array_index(opt_input_paths, i);
		printf_verbose("    %s\n", ipath);
	}
	printf_verbose("Converting from format: %s\n",
		opt_input_format ? : "ctf <default>");
	printf_verbose("Converting to target: %s\n",
		opt_output_path ? : "<stdout>");
	printf_verbose("Converting to format: %s\n",
		opt_output_format ? : "text <default>");

	if (!opt_input_format) {
		opt_input_format = strdup("ctf");
		if (!opt_input_format) {
			partial_error = 1;
			goto end;
		}
	}
	if (!opt_output_format) {
		opt_output_format = strdup("text");
		if (!opt_output_format) {
			partial_error = 1;
			goto end;
		}
	}
	fmt_read = bt_lookup_format(g_quark_from_static_string(opt_input_format));
	if (!fmt_read) {
		fprintf(stderr, "[error] Format \"%s\" is not supported.\n\n",
			opt_input_format);
		partial_error = 1;
		goto end;
	}
	fmt_write = bt_lookup_format(g_quark_from_static_string(opt_output_format));
	if (!fmt_write) {
		fprintf(stderr, "[error] format \"%s\" is not supported.\n\n",
			opt_output_format);
		partial_error = 1;
		goto end;
	}

	ctx = bt_context_create();
	if (!ctx) {
		goto error_td_read;
	}

	for (i = 0; i < opt_input_paths->len; i++) {
		const char *ipath = g_ptr_array_index(opt_input_paths, i);
		ret = bt_context_add_traces_recursive(ctx, ipath,
				opt_input_format, NULL);
		if (ret < 0) {
			fprintf(stderr, "[error] opening trace \"%s\" for reading.\n\n",
				ipath);
		} else if (ret > 0) {
			fprintf(stderr, "[warning] errors occurred when opening trace \"%s\" for reading, continuing anyway.\n\n",
				ipath);
			open_success = 1;	/* some traces were OK */
			partial_error = 1;
		} else {
			open_success = 1;	/* all traces were OK */
		}
	}
	if (!open_success) {
		fprintf(stderr, "[error] none of the specified trace paths could be opened.\n\n");
		goto error_td_read;
	}

	td_write = fmt_write->open_trace(opt_output_path, O_RDWR, NULL, NULL);
	if (!td_write) {
		fprintf(stderr, "Error opening trace \"%s\" for writing.\n\n",
			opt_output_path ? : "<none>");
		goto error_td_write;
	}
Beispiel #2
0
int bt_context_add_trace(struct bt_context *ctx, const char *path,
		const char *format_name,
		void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
			int whence),
		struct bt_mmap_stream_list *stream_list,
		FILE *metadata)
{
	struct bt_trace_descriptor *td;
	struct bt_format *fmt;
	struct bt_trace_handle *handle;
	int ret, closeret;

	if (!ctx || !format_name || (!path && !stream_list))
		return -EINVAL;

	fmt = bt_lookup_format(g_quark_from_string(format_name));
	if (!fmt) {
		fprintf(stderr, "[error] [Context] Format \"%s\" unknown.\n\n",
			format_name);
		ret = -1;
		goto end;
	}
	if (path) {
		td = fmt->open_trace(path, O_RDONLY, packet_seek, NULL);
		if (!td) {
			fprintf(stderr, "[warning] [Context] Cannot open_trace of format %s at path %s.\n",
					format_name, path);
			ret = -1;
			goto end;
		}
	} else {
		td = fmt->open_mmap_trace(stream_list, packet_seek, metadata);
		if (!td) {
			fprintf(stderr, "[error] [Context] Cannot open_mmap_trace of format %s.\n\n",
					format_name);
			ret = -1;
			goto end;
		}
	}

	/* Create an handle for the trace */
	handle = bt_trace_handle_create(ctx);
	if (!handle) {
		fprintf(stderr, "[error] [Context] Creating trace handle %s .\n\n",
				path);
		ret = -1;
		goto error;
	}
	handle->format = fmt;
	handle->td = td;
	if (path) {
		strncpy(handle->path, path, PATH_MAX);
		handle->path[PATH_MAX - 1] = '\0';
	}

	if (fmt->set_handle)
		fmt->set_handle(td, handle);
	if (fmt->set_context)
		fmt->set_context(td, ctx);

	/* Add new handle to container */
	g_hash_table_insert(ctx->trace_handles,
		(gpointer) (unsigned long) handle->id,
		handle);
	ret = bt_trace_collection_add(ctx->tc, td);
	if (ret != 0)
		goto error;

	ret = fmt->convert_index_timestamp(td);
	if (ret < 0)
		goto error;

	handle->real_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_REAL);
	handle->real_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_REAL);
	handle->cycles_timestamp_begin = fmt->timestamp_begin(td, handle, BT_CLOCK_CYCLES);
	handle->cycles_timestamp_end = fmt->timestamp_end(td, handle, BT_CLOCK_CYCLES);

	return handle->id;

error:
	closeret = fmt->close_trace(td);
	if (closeret) {
		fprintf(stderr, "Error in close_trace callback\n");
	}
end:
	return ret;
}