X-Git-Url: http://git.efficios.com/?p=babeltrace.git;a=blobdiff_plain;f=tests%2Ftest-dummytrace.c;h=aa855cc7cd95a2a9a62430e995a20399364d49fd;hp=ed00f34b0a1aa75b8e9dc6d5df0cbdfed40a89e8;hb=b522ac189ce3ed368594d466461d2eb33025f75d;hpb=8c572eba034b95716a4bbd019dfbe0827605ba90 diff --git a/tests/test-dummytrace.c b/tests/test-dummytrace.c index ed00f34b..aa855cc7 100644 --- a/tests/test-dummytrace.c +++ b/tests/test-dummytrace.c @@ -1,7 +1,7 @@ /* - * text-to-ctf.c + * babeltrace-log.c * - * BabelTrace - Convert Text to CTF + * BabelTrace - Convert Text Log to CTF * * Copyright 2010, 2011 - Mathieu Desnoyers * @@ -22,87 +22,136 @@ #include #include #include +#include #include +#include #include #include #include #include #include +#include #include #include -int babeltrace_debug = 0; +#ifndef UUID_STR_LEN +#define UUID_STR_LEN 37 /* With \0 */ +#endif + +/* Metadata format string */ +static const char metadata_fmt[] = +"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n" +"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n" +"\n" +"trace {\n" +" major = %s;\n" /* major (e.g. 0) */ +" minor = %s;\n" /* minor (e.g. 1) */ +" uuid = \"%s\";\n" /* UUID */ +" byte_order = %s;\n" /* be or le */ +" packet.header := struct {\n" +" uint32_t magic;\n" +" uint8_t trace_uuid[16];\n" +" };\n" +"};\n" +"\n" +"stream {\n" +" packet.context := struct {\n" +" uint32_t content_size;\n" +" uint32_t packet_size;\n" +" };\n" +"};\n" +"\n" +"event {\n" +" name = string;\n" +" fields := struct { string str; };\n" +"};\n"; + +int babeltrace_debug, babeltrace_verbose; static uuid_t s_uuid; static -void write_packet_header(struct stream_pos *pos, uuid_t uuid) +void print_metadata(FILE *fp) +{ + char uuid_str[UUID_STR_LEN]; + + uuid_unparse(s_uuid, uuid_str); + fprintf(fp, metadata_fmt, + __stringify(BABELTRACE_VERSION_MAJOR), + __stringify(BABELTRACE_VERSION_MINOR), + uuid_str, + BYTE_ORDER == LITTLE_ENDIAN ? "le" : "be"); +} + + +static +void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid) { - struct stream_pos dummy; + struct ctf_stream_pos dummy; /* magic */ - dummy_pos(pos, &dummy); - align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); - move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); - assert(!pos_packet(&dummy)); + ctf_dummy_pos(pos, &dummy); + ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); + ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); + assert(!ctf_pos_packet(&dummy)); - align_pos(pos, sizeof(uint32_t) * CHAR_BIT); - *(uint32_t *) get_pos_addr(pos) = 0xC1FC1FC1; - move_pos(pos, sizeof(uint32_t) * CHAR_BIT); + ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); + *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1; + ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); /* trace_uuid */ - dummy_pos(pos, &dummy); - align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); - move_pos(&dummy, 16 * CHAR_BIT); - assert(!pos_packet(&dummy)); - - align_pos(pos, sizeof(uint8_t) * CHAR_BIT); - memcpy(get_pos_addr(pos), uuid, 16); - move_pos(pos, 16 * CHAR_BIT); + ctf_dummy_pos(pos, &dummy); + ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); + ctf_move_pos(&dummy, 16 * CHAR_BIT); + assert(!ctf_pos_packet(&dummy)); + + ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); + memcpy(ctf_get_pos_addr(pos), uuid, 16); + ctf_move_pos(pos, 16 * CHAR_BIT); } static -void write_packet_context(struct stream_pos *pos) +void write_packet_context(struct ctf_stream_pos *pos) { - struct stream_pos dummy; + struct ctf_stream_pos dummy; /* content_size */ - dummy_pos(pos, &dummy); - align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); - move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); - assert(!pos_packet(&dummy)); + ctf_dummy_pos(pos, &dummy); + ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); + ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); + assert(!ctf_pos_packet(&dummy)); - align_pos(pos, sizeof(uint32_t) * CHAR_BIT); - *(uint32_t *) get_pos_addr(pos) = -1U; /* Not known yet */ - pos->content_size_loc = (uint32_t *) get_pos_addr(pos); - move_pos(pos, sizeof(uint32_t) * CHAR_BIT); + ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); + *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */ + pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos); + ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); /* packet_size */ - dummy_pos(pos, &dummy); - align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); - move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); - assert(!pos_packet(&dummy)); + ctf_dummy_pos(pos, &dummy); + ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); + ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); + assert(!ctf_pos_packet(&dummy)); - align_pos(pos, sizeof(uint32_t) * CHAR_BIT); - *(uint32_t *) get_pos_addr(pos) = pos->packet_size; - move_pos(pos, sizeof(uint32_t) * CHAR_BIT); + ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); + *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size; + ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); } static -void trace_string(char *line, struct stream_pos *pos, size_t len) +void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) { - struct stream_pos dummy; + struct ctf_stream_pos dummy; int attempt = 0; printf_debug("read: %s\n", line); retry: - dummy_pos(pos, &dummy); - align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); - move_pos(&dummy, len * CHAR_BIT); - if (pos_packet(&dummy)) { + ctf_dummy_pos(pos, &dummy); + ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); + ctf_move_pos(&dummy, len * CHAR_BIT); + if (ctf_pos_packet(&dummy)) { /* TODO write content size */ - pos_pad_packet(pos); + ctf_pos_pad_packet(pos); write_packet_header(pos, s_uuid); write_packet_context(pos); if (attempt++ == 1) { @@ -113,20 +162,20 @@ retry: goto retry; } - align_pos(pos, sizeof(uint8_t) * CHAR_BIT); - memcpy(get_pos_addr(pos), line, len); - move_pos(pos, len * CHAR_BIT); + ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); + memcpy(ctf_get_pos_addr(pos), line, len); + ctf_move_pos(pos, len * CHAR_BIT); } static void trace_text(FILE *input, int output) { - struct stream_pos pos; + struct ctf_stream_pos pos; ssize_t len; char *line = NULL, *nl; size_t linesize; - init_pos(&pos, output); + ctf_init_pos(&pos, output, O_RDWR); write_packet_header(&pos, s_uuid); write_packet_context(&pos); @@ -139,32 +188,101 @@ void trace_text(FILE *input, int output) *nl = '\0'; trace_string(line, &pos, nl - line + 1); } - fini_pos(&pos); + ctf_fini_pos(&pos); +} + +static void usage(FILE *fp) +{ + fprintf(fp, "BabelTrace Log Converter %u.%u\n", + BABELTRACE_VERSION_MAJOR, + BABELTRACE_VERSION_MINOR); + fprintf(fp, "\n"); + fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n"); + fprintf(fp, "\n"); + fprintf(fp, "usage : babeltrace-log OUTPUT\n"); + fprintf(fp, "\n"); + fprintf(fp, " OUTPUT Output trace path\n"); + fprintf(fp, "\n"); } int main(int argc, char **argv) { - int fd, ret; + int fd, metadata_fd, ret; + char *outputname; + DIR *dir; + int dir_fd; + FILE *metadata_fp; - ret = unlink("dummystream"); - if (ret < 0) { - perror("unlink"); - return -1; + if (argc < 2) { + usage(stdout); + goto error; } - fd = open("dummystream", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); + outputname = argv[1]; + + ret = mkdir(outputname, S_IRWXU|S_IRWXG); + if (ret) { + perror("mkdir"); + goto error; + } + + dir = opendir(outputname); + if (!dir) { + perror("opendir"); + goto error_rmdir; + } + dir_fd = dirfd(dir); + if (dir_fd < 0) { + perror("dirfd"); + goto error_closedir; + } + + fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT, + S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); if (fd < 0) { - perror("open"); - return -1; + perror("openat"); + goto error_closedirfd; } - ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid); - if (ret) { - printf("uuid parse error\n"); - close(fd); - return -1; + metadata_fd = openat(dir_fd, "metadata", O_RDWR|O_CREAT, + S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP); + if (fd < 0) { + perror("openat"); + goto error_closedatastream; + } + metadata_fp = fdopen(metadata_fd, "w"); + if (!metadata_fp) { + perror("fdopen"); + goto error_closemetadatafd; } + uuid_generate(s_uuid); + print_metadata(metadata_fp); trace_text(stdin, fd); + close(fd); - return 0; + exit(EXIT_SUCCESS); + + /* error handling */ +error_closemetadatafd: + ret = close(metadata_fd); + if (ret) + perror("close"); +error_closedatastream: + ret = close(fd); + if (ret) + perror("close"); +error_closedirfd: + ret = close(dir_fd); + if (ret) + perror("close"); +error_closedir: + ret = closedir(dir); + if (ret) + perror("closedir"); +error_rmdir: + ret = rmdir(outputname); + if (ret) + perror("rmdir"); +error: + exit(EXIT_FAILURE); }