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 | ||
35 | int babeltrace_debug = 0; | |
36 | ||
37 | static uuid_t s_uuid; | |
38 | ||
39 | static | |
40 | void write_packet_header(struct stream_pos *pos, uuid_t uuid) | |
41 | { | |
42 | struct stream_pos dummy; | |
43 | ||
44 | /* magic */ | |
45 | dummy_pos(pos, &dummy); | |
46 | align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
47 | move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
48 | assert(!pos_packet(&dummy)); | |
49 | ||
50 | align_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
51 | *(uint32_t *) get_pos_addr(pos) = 0xC1FC1FC1; | |
52 | move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
53 | ||
54 | /* trace_uuid */ | |
55 | dummy_pos(pos, &dummy); | |
56 | align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
57 | move_pos(&dummy, 16 * CHAR_BIT); | |
58 | assert(!pos_packet(&dummy)); | |
59 | ||
60 | align_pos(pos, sizeof(uint8_t) * CHAR_BIT); | |
61 | memcpy(get_pos_addr(pos), uuid, 16); | |
62 | move_pos(pos, 16 * CHAR_BIT); | |
63 | } | |
64 | ||
65 | static | |
66 | void write_packet_context(struct stream_pos *pos) | |
67 | { | |
68 | struct stream_pos dummy; | |
69 | ||
70 | /* content_size */ | |
71 | dummy_pos(pos, &dummy); | |
72 | align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
73 | move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
74 | assert(!pos_packet(&dummy)); | |
75 | ||
76 | align_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
77 | *(uint32_t *) get_pos_addr(pos) = -1U; /* Not known yet */ | |
78 | pos->content_size_loc = (uint32_t *) get_pos_addr(pos); | |
79 | move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
80 | ||
81 | /* packet_size */ | |
82 | dummy_pos(pos, &dummy); | |
83 | align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
84 | move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT); | |
85 | assert(!pos_packet(&dummy)); | |
86 | ||
87 | align_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
88 | *(uint32_t *) get_pos_addr(pos) = pos->packet_size; | |
89 | move_pos(pos, sizeof(uint32_t) * CHAR_BIT); | |
90 | } | |
91 | ||
92 | static | |
93 | void trace_string(char *line, struct stream_pos *pos, size_t len) | |
94 | { | |
95 | struct stream_pos dummy; | |
96 | int attempt = 0; | |
97 | ||
98 | printf_debug("read: %s\n", line); | |
99 | retry: | |
100 | dummy_pos(pos, &dummy); | |
101 | align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT); | |
102 | move_pos(&dummy, len * CHAR_BIT); | |
103 | if (pos_packet(&dummy)) { | |
104 | /* TODO write content size */ | |
105 | pos_pad_packet(pos); | |
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 | ||
116 | align_pos(pos, sizeof(uint8_t) * CHAR_BIT); | |
117 | memcpy(get_pos_addr(pos), line, len); | |
118 | move_pos(pos, len * CHAR_BIT); | |
119 | } | |
120 | ||
121 | static | |
122 | void trace_text(FILE *input, int output) | |
123 | { | |
124 | struct stream_pos pos; | |
125 | ssize_t len; | |
126 | char *line = NULL, *nl; | |
127 | size_t linesize; | |
128 | ||
129 | init_pos(&pos, output); | |
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 | } | |
142 | fini_pos(&pos); | |
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 | } |