49a650e46ac7ef897fd697e529c83294f0f67732
4 * BabelTrace - Convert Text to CTF
6 * Copyright 2010, 2011 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
18 * Depends on glibc 2.10 for getline().
21 #include <sys/types.h>
31 #include <uuid/uuid.h>
35 #include <babeltrace/babeltrace.h>
36 #include <babeltrace/ctf/types.h>
38 static 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"
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"
49 " uint8_t trace_uuid[16];\n"
54 " packet.context := struct {\n"
55 " uint32_t content_size;\n"
56 " uint32_t packet_size;\n"
62 " fields := struct { string str; };\n"
65 int babeltrace_debug
, babeltrace_verbose
;
70 void write_packet_header(struct ctf_stream_pos
*pos
, uuid_t uuid
)
72 struct ctf_stream_pos dummy
;
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
));
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
);
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
));
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
);
96 void write_packet_context(struct ctf_stream_pos
*pos
)
98 struct ctf_stream_pos dummy
;
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
));
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
);
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
));
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
);
123 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
125 struct ctf_stream_pos dummy
;
128 printf_debug("read: %s\n", line
);
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
)) {
134 /* TODO write content size */
135 ctf_pos_pad_packet(pos
);
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);
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
);
152 void trace_text(FILE *input
, int output
)
154 struct ctf_stream_pos pos
;
156 char *line
= NULL
, *nl
;
159 ctf_init_pos(&pos
, output
, O_RDWR
);
161 write_packet_header(&pos
, s_uuid
);
162 write_packet_context(&pos
);
164 len
= getline(&line
, &linesize
, input
);
167 nl
= strrchr(line
, '\n');
170 trace_string(line
, &pos
, nl
- line
+ 1);
175 static void usage(FILE *fp
)
177 fprintf(fp
, "BabelTrace Log Converter %u.%u\n",
178 BABELTRACE_VERSION_MAJOR
,
179 BABELTRACE_VERSION_MINOR
);
181 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
183 fprintf(fp
, "usage : babeltrace-log OUTPUT\n");
185 fprintf(fp
, " OUTPUT Output trace path\n");
189 int main(int argc
, char **argv
)
200 outputname
= argv
[1];
202 ret
= mkdir(outputname
, S_IRWXU
|S_IRWXG
);
208 dir
= opendir(outputname
);
219 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
220 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
223 goto error_closedirfd
;
226 trace_text(stdin
, fd
);
241 ret
= rmdir(outputname
);
This page took 0.032793 seconds and 3 git commands to generate.