Use O_RDWR for open write mode (for mmap)
[babeltrace.git] / tests / test-dummytrace.c
CommitLineData
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>
989c73bc 25#include <dirent.h>
90219914 26#include <stdio.h>
989c73bc 27#include <stdlib.h>
90219914
MD
28#include <stdint.h>
29#include <unistd.h>
30#include <errno.h>
31#include <uuid/uuid.h>
32#include <string.h>
989c73bc 33#include <endian.h>
90219914 34
8c572eba
MD
35#include <babeltrace/babeltrace.h>
36#include <babeltrace/ctf/types.h>
37
989c73bc
MD
38static const char metadata[] =
39"typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
40"typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
41"\n"
42"trace {\n"
43" major = %s;\n" /* major (e.g. 0) */
44" minor = %s;\n" /* minor (e.g. 1) */
45" uuid = %s;\n" /* UUID */
46" byte_order = %s;\n" /* be or le */
47" packet.header := struct {\n"
48" uint32_t magic;\n"
49" uint8_t trace_uuid[16];\n"
50" };\n"
51"};\n"
52"\n"
53"stream {\n"
54" packet.context := struct {\n"
55" uint32_t content_size;\n"
56" uint32_t packet_size;\n"
57" };\n"
58"};\n"
59"\n"
60"event {\n"
61" name = string;\n"
62" fields := struct { string str; };\n"
63"};\n";
64
c8b219a3 65int babeltrace_debug, babeltrace_verbose;
8c572eba
MD
66
67static uuid_t s_uuid;
68
69static
46322b33 70void write_packet_header(struct ctf_stream_pos *pos, uuid_t uuid)
8c572eba 71{
46322b33 72 struct ctf_stream_pos dummy;
8c572eba
MD
73
74 /* magic */
46322b33
MD
75 ctf_dummy_pos(pos, &dummy);
76 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
77 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
78 assert(!ctf_pos_packet(&dummy));
8c572eba 79
46322b33
MD
80 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
81 *(uint32_t *) ctf_get_pos_addr(pos) = 0xC1FC1FC1;
82 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
83
84 /* trace_uuid */
46322b33
MD
85 ctf_dummy_pos(pos, &dummy);
86 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
87 ctf_move_pos(&dummy, 16 * CHAR_BIT);
88 assert(!ctf_pos_packet(&dummy));
89
90 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
91 memcpy(ctf_get_pos_addr(pos), uuid, 16);
92 ctf_move_pos(pos, 16 * CHAR_BIT);
8c572eba
MD
93}
94
95static
46322b33 96void write_packet_context(struct ctf_stream_pos *pos)
8c572eba 97{
46322b33 98 struct ctf_stream_pos dummy;
8c572eba
MD
99
100 /* content_size */
46322b33
MD
101 ctf_dummy_pos(pos, &dummy);
102 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
103 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
104 assert(!ctf_pos_packet(&dummy));
8c572eba 105
46322b33
MD
106 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
107 *(uint32_t *) ctf_get_pos_addr(pos) = -1U; /* Not known yet */
108 pos->content_size_loc = (uint32_t *) ctf_get_pos_addr(pos);
109 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
110
111 /* packet_size */
46322b33
MD
112 ctf_dummy_pos(pos, &dummy);
113 ctf_align_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
114 ctf_move_pos(&dummy, sizeof(uint32_t) * CHAR_BIT);
115 assert(!ctf_pos_packet(&dummy));
8c572eba 116
46322b33
MD
117 ctf_align_pos(pos, sizeof(uint32_t) * CHAR_BIT);
118 *(uint32_t *) ctf_get_pos_addr(pos) = pos->packet_size;
119 ctf_move_pos(pos, sizeof(uint32_t) * CHAR_BIT);
8c572eba
MD
120}
121
122static
46322b33 123void trace_string(char *line, struct ctf_stream_pos *pos, size_t len)
8c572eba 124{
46322b33 125 struct ctf_stream_pos dummy;
8c572eba
MD
126 int attempt = 0;
127
128 printf_debug("read: %s\n", line);
129retry:
46322b33
MD
130 ctf_dummy_pos(pos, &dummy);
131 ctf_align_pos(&dummy, sizeof(uint8_t) * CHAR_BIT);
132 ctf_move_pos(&dummy, len * CHAR_BIT);
133 if (ctf_pos_packet(&dummy)) {
8c572eba 134 /* TODO write content size */
46322b33 135 ctf_pos_pad_packet(pos);
8c572eba
MD
136 write_packet_header(pos, s_uuid);
137 write_packet_context(pos);
138 if (attempt++ == 1) {
139 fprintf(stdout, "[Error] Line too large for packet size (%zukB) (discarded)\n",
140 pos->packet_size / CHAR_BIT / 1024);
141 return;
142 }
143 goto retry;
144 }
145
46322b33
MD
146 ctf_align_pos(pos, sizeof(uint8_t) * CHAR_BIT);
147 memcpy(ctf_get_pos_addr(pos), line, len);
148 ctf_move_pos(pos, len * CHAR_BIT);
8c572eba
MD
149}
150
151static
152void trace_text(FILE *input, int output)
153{
46322b33 154 struct ctf_stream_pos pos;
8c572eba
MD
155 ssize_t len;
156 char *line = NULL, *nl;
157 size_t linesize;
158
989c73bc 159 ctf_init_pos(&pos, output, O_RDWR);
8c572eba
MD
160
161 write_packet_header(&pos, s_uuid);
162 write_packet_context(&pos);
163 for (;;) {
164 len = getline(&line, &linesize, input);
165 if (len < 0)
166 break;
167 nl = strrchr(line, '\n');
168 if (nl)
169 *nl = '\0';
170 trace_string(line, &pos, nl - line + 1);
171 }
46322b33 172 ctf_fini_pos(&pos);
8c572eba 173}
90219914 174
989c73bc
MD
175static void usage(FILE *fp)
176{
177 fprintf(fp, "BabelTrace Log Converter %u.%u\n",
178 BABELTRACE_VERSION_MAJOR,
179 BABELTRACE_VERSION_MINOR);
180 fprintf(fp, "\n");
181 fprintf(fp, "Convert for a text log (read from standard input) to CTF.\n");
182 fprintf(fp, "\n");
183 fprintf(fp, "usage : babeltrace-log OUTPUT\n");
184 fprintf(fp, "\n");
185 fprintf(fp, " OUTPUT Output trace path\n");
186 fprintf(fp, "\n");
187}
188
90219914
MD
189int main(int argc, char **argv)
190{
90219914 191 int fd, ret;
989c73bc
MD
192 char *outputname;
193 DIR *dir;
194 int dir_fd;
90219914 195
989c73bc
MD
196 if (argc < 2) {
197 usage(stdout);
198 goto error;
90219914 199 }
989c73bc 200 outputname = argv[1];
90219914 201
989c73bc 202 ret = mkdir(outputname, S_IRWXU|S_IRWXG);
90219914 203 if (ret) {
989c73bc
MD
204 perror("mkdir");
205 goto error;
206 }
207
208 dir = opendir(outputname);
209 if (!dir) {
210 perror("opendir");
211 goto error_rmdir;
212 }
213 dir_fd = dirfd(dir);
214 if (dir_fd < 0) {
215 perror("dirfd");
216 goto error_closedir;
217 }
218
219 fd = openat(dir_fd, "datastream", O_RDWR|O_CREAT,
220 S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
221 if (fd < 0) {
222 perror("openat");
223 goto error_closedirfd;
90219914 224 }
90219914 225
8c572eba 226 trace_text(stdin, fd);
989c73bc 227
90219914 228 close(fd);
989c73bc
MD
229 exit(EXIT_SUCCESS);
230
231 /* error handling */
232error_closedirfd:
233 ret = close(dir_fd);
234 if (ret)
235 perror("close");
236error_closedir:
237 ret = closedir(dir);
238 if (ret)
239 perror("closedir");
240error_rmdir:
241 ret = rmdir(outputname);
242 if (ret)
243 perror("rmdir");
244error:
245 exit(EXIT_FAILURE);
90219914 246}
This page took 0.032611 seconds and 4 git commands to generate.