Commit | Line | Data |
---|---|---|
8c572eba MD |
1 | /* |
2 | * text-to-ctf.c | |
3 | * | |
4 | * BabelTrace - Convert Text to CTF | |
5 | * | |
6 | * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com> | |
7 | * | |
8 | * Permission is hereby granted, free of charge, to any person obtaining a copy | |
9 | * of this software and associated documentation files (the "Software"), to deal | |
10 | * in the Software without restriction, including without limitation the rights | |
11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
12 | * copies of the Software, and to permit persons to whom the Software is | |
13 | * furnished to do so, subject to the following conditions: | |
14 | * | |
15 | * The above copyright notice and this permission notice shall be included in | |
16 | * all copies or substantial portions of the Software. | |
17 | * | |
18 | * Depends on glibc 2.10 for getline(). | |
19 | */ | |
20 | ||
90219914 MD |
21 | #include <sys/types.h> |
22 | #include <sys/stat.h> | |
23 | #include <fcntl.h> | |
24 | #include <sys/mman.h> | |
25 | #include <stdio.h> | |
26 | #include <stdint.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
29 | #include <uuid/uuid.h> | |
30 | #include <string.h> | |
31 | ||
8c572eba MD |
32 | #include <babeltrace/babeltrace.h> |
33 | #include <babeltrace/ctf/types.h> | |
34 | ||
c8b219a3 | 35 | int babeltrace_debug, babeltrace_verbose; |
8c572eba MD |
36 | |
37 | static uuid_t s_uuid; | |
38 | ||
39 | static | |
46322b33 | 40 | void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid) |
8c572eba | 41 | { |
46322b33 | 42 | struct ctf_stream_pos dummy; |
8c572eba MD |
43 | |
44 | /* magic */ | |
46322b33 MD |
45 | ctf_dummy_pos(pos, &dummy); |
46 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
47 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
48 | assert(!ctf_pos_packet(&dummy)); | |
8c572eba | 49 | |
46322b33 MD |
50 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
51 | *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1; | |
52 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba MD |
53 | |
54 | /* trace_uuid */ | |
46322b33 MD |
55 | ctf_dummy_pos(pos, &dummy); |
56 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
57 | ctf_move_pos(&dummy, 16 * CHAR_BIT); | |
58 | assert(!ctf_pos_packet(&dummy)); | |
59 | ||
60 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); | |
61 | memcpy(ctf_get_pos_addr(pos), uuid, 16); | |
62 | ctf_move_pos(pos, 16 * CHAR_BIT); | |
8c572eba MD |
63 | } |
64 | ||
65 | static | |
46322b33 | 66 | void write_packet_context(struct ctf_stream_pos *pos) |
8c572eba | 67 | { |
46322b33 | 68 | struct ctf_stream_pos dummy; |
8c572eba MD |
69 | |
70 | /* content_size */ | |
46322b33 MD |
71 | ctf_dummy_pos(pos, &dummy); |
72 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
73 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
74 | assert(!ctf_pos_packet(&dummy)); | |
8c572eba | 75 | |
46322b33 MD |
76 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
77 | *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */ | |
78 | pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos); | |
79 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba MD |
80 | |
81 | /* packet_size */ | |
46322b33 MD |
82 | ctf_dummy_pos(pos, &dummy); |
83 | ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
84 | ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
85 | assert(!ctf_pos_packet(&dummy)); | |
8c572eba | 86 | |
46322b33 MD |
87 | ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT); |
88 | *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size; | |
89 | ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
8c572eba MD |
90 | } |
91 | ||
92 | static | |
46322b33 | 93 | void trace_string(char *line, struct ctf_stream_pos *pos, size_t len) |
8c572eba | 94 | { |
46322b33 | 95 | struct ctf_stream_pos dummy; |
8c572eba MD |
96 | int attempt = 0; |
97 | ||
98 | printf_debug("read: %s\n", line); | |
99 | retry: | |
46322b33 MD |
100 | ctf_dummy_pos(pos, &dummy); |
101 | ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
102 | ctf_move_pos(&dummy, len * CHAR_BIT); | |
103 | if (ctf_pos_packet(&dummy)) { | |
8c572eba | 104 | /* TODO write content size */ |
46322b33 | 105 | ctf_pos_pad_packet(pos); |
8c572eba MD |
106 | write_packet_header(pos, s_uuid); |
107 | write_packet_context(pos); | |
108 | if (attempt++ == 1) { | |
109 | fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n", | |
110 | pos->packet_size / CHAR_BIT / 1024); | |
111 | return; | |
112 | } | |
113 | goto retry; | |
114 | } | |
115 | ||
46322b33 MD |
116 | ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT); |
117 | memcpy(ctf_get_pos_addr(pos), line, len); | |
118 | ctf_move_pos(pos, len * CHAR_BIT); | |
8c572eba MD |
119 | } |
120 | ||
121 | static | |
122 | void trace_text(FILE *input, int output) | |
123 | { | |
46322b33 | 124 | struct ctf_stream_pos pos; |
8c572eba MD |
125 | ssize_t len; |
126 | char *line = NULL, *nl; | |
127 | size_t linesize; | |
128 | ||
8563e754 | 129 | ctf_init_pos(&pos, output, O_WRONLY); |
8c572eba MD |
130 | |
131 | write_packet_header(&pos, s_uuid); | |
132 | write_packet_context(&pos); | |
133 | for (;;) { | |
134 | len = getline(&line, &linesize, input); | |
135 | if (len < 0) | |
136 | break; | |
137 | nl = strrchr(line, '\n'); | |
138 | if (nl) | |
139 | *nl = '\0'; | |
140 | trace_string(line, &pos, nl - line + 1); | |
141 | } | |
46322b33 | 142 | ctf_fini_pos(&pos); |
8c572eba | 143 | } |
90219914 MD |
144 | |
145 | int main(int argc, char **argv) | |
146 | { | |
90219914 | 147 | int fd, ret; |
90219914 | 148 | |
8c572eba MD |
149 | ret = unlink("dummystream"); |
150 | if (ret < 0) { | |
151 | perror("unlink"); | |
152 | return -1; | |
153 | } | |
90219914 MD |
154 | fd = open("dummystream", O_RDWR|O_CREAT, S_IRUSR|S_IWUSR); |
155 | if (fd < 0) { | |
156 | perror("open"); | |
157 | return -1; | |
158 | } | |
90219914 | 159 | |
8c572eba | 160 | ret = uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid); |
90219914 MD |
161 | if (ret) { |
162 | printf("uuid parse error\n"); | |
8c572eba | 163 | close(fd); |
90219914 MD |
164 | return -1; |
165 | } | |
90219914 | 166 | |
8c572eba | 167 | trace_text(stdin, fd); |
90219914 MD |
168 | close(fd); |
169 | return 0; | |
170 | } |