4 * BabelTrace - Convert Text Log 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>
39 #define UUID_STR_LEN 37 /* With \0 */
42 /* Metadata format string */
43 static const char metadata_fmt
[] =
44 "typealias integer { size = 8; align = 8; signed = false; } := uint8_t;\n"
45 "typealias integer { size = 32; align = 32; signed = false; } := uint32_t;\n"
48 " major = %s;\n" /* major (e.g. 0) */
49 " minor = %s;\n" /* minor (e.g. 1) */
50 " uuid = \"%s\";\n" /* UUID */
51 " byte_order = %s;\n" /* be or le */
52 " packet.header := struct {\n"
54 " uint8_t trace_uuid[16];\n"
59 " packet.context := struct {\n"
60 " uint32_t content_size;\n"
61 " uint32_t packet_size;\n"
67 " fields := struct { string str; };\n"
70 int babeltrace_debug
, babeltrace_verbose
;
75 void print_metadata(FILE *fp
)
77 char uuid_str
[UUID_STR_LEN
];
79 uuid_unparse(s_uuid
, uuid_str
);
80 fprintf(fp
, metadata_fmt
,
81 __stringify(BABELTRACE_VERSION_MAJOR
),
82 __stringify(BABELTRACE_VERSION_MINOR
),
84 BYTE_ORDER
== LITTLE_ENDIAN
? "le" : "be");
89 void write_packet_header(struct ctf_stream_pos
*pos
, uuid_t uuid
)
91 struct ctf_stream_pos dummy
;
94 ctf_dummy_pos(pos
, &dummy
);
95 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
96 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
97 assert(!ctf_pos_packet(&dummy
));
99 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
100 *(uint32_t *) ctf_get_pos_addr(pos
) = 0xC1FC1FC1;
101 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
104 ctf_dummy_pos(pos
, &dummy
);
105 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
106 ctf_move_pos(&dummy
, 16 * CHAR_BIT
);
107 assert(!ctf_pos_packet(&dummy
));
109 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
110 memcpy(ctf_get_pos_addr(pos
), uuid
, 16);
111 ctf_move_pos(pos
, 16 * CHAR_BIT
);
115 void write_packet_context(struct ctf_stream_pos
*pos
)
117 struct ctf_stream_pos dummy
;
120 ctf_dummy_pos(pos
, &dummy
);
121 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
122 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
123 assert(!ctf_pos_packet(&dummy
));
125 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
126 *(uint32_t *) ctf_get_pos_addr(pos
) = -1U; /* Not known yet */
127 pos
->content_size_loc
= (uint32_t *) ctf_get_pos_addr(pos
);
128 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
131 ctf_dummy_pos(pos
, &dummy
);
132 ctf_align_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
133 ctf_move_pos(&dummy
, sizeof(uint32_t) * CHAR_BIT
);
134 assert(!ctf_pos_packet(&dummy
));
136 ctf_align_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
137 *(uint32_t *) ctf_get_pos_addr(pos
) = pos
->packet_size
;
138 ctf_move_pos(pos
, sizeof(uint32_t) * CHAR_BIT
);
142 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
144 struct ctf_stream_pos dummy
;
147 printf_debug("read: %s\n", line
);
149 ctf_dummy_pos(pos
, &dummy
);
150 ctf_align_pos(&dummy
, sizeof(uint8_t) * CHAR_BIT
);
151 ctf_move_pos(&dummy
, len
* CHAR_BIT
);
152 if (ctf_pos_packet(&dummy
)) {
153 ctf_pos_pad_packet(pos
);
154 write_packet_header(pos
, s_uuid
);
155 write_packet_context(pos
);
156 if (attempt
++ == 1) {
157 fprintf(stdout
, "[Error] Line too large for packet size (%zukB) (discarded)\n",
158 pos
->packet_size
/ CHAR_BIT
/ 1024);
164 ctf_align_pos(pos
, sizeof(uint8_t) * CHAR_BIT
);
165 memcpy(ctf_get_pos_addr(pos
), line
, len
);
166 ctf_move_pos(pos
, len
* CHAR_BIT
);
170 void trace_text(FILE *input
, int output
)
172 struct ctf_stream_pos pos
;
174 char *line
= NULL
, *nl
;
177 ctf_init_pos(&pos
, output
, O_RDWR
);
179 write_packet_header(&pos
, s_uuid
);
180 write_packet_context(&pos
);
182 len
= getline(&line
, &linesize
, input
);
185 nl
= strrchr(line
, '\n');
188 trace_string(line
, &pos
, nl
- line
+ 1);
193 static void usage(FILE *fp
)
195 fprintf(fp
, "BabelTrace Log Converter %u.%u\n",
196 BABELTRACE_VERSION_MAJOR
,
197 BABELTRACE_VERSION_MINOR
);
199 fprintf(fp
, "Convert for a text log (read from standard input) to CTF.\n");
201 fprintf(fp
, "usage : babeltrace-log OUTPUT\n");
203 fprintf(fp
, " OUTPUT Output trace path\n");
207 int main(int argc
, char **argv
)
209 int fd
, metadata_fd
, ret
;
219 outputname
= argv
[1];
221 ret
= mkdir(outputname
, S_IRWXU
|S_IRWXG
);
227 dir
= opendir(outputname
);
238 fd
= openat(dir_fd
, "datastream", O_RDWR
|O_CREAT
,
239 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
242 goto error_closedirfd
;
245 metadata_fd
= openat(dir_fd
, "metadata", O_RDWR
|O_CREAT
,
246 S_IRUSR
|S_IWUSR
|S_IRGRP
|S_IWGRP
);
249 goto error_closedatastream
;
251 metadata_fp
= fdopen(metadata_fd
, "w");
254 goto error_closemetadatafd
;
257 uuid_generate(s_uuid
);
258 print_metadata(metadata_fp
);
259 trace_text(stdin
, fd
);
265 error_closemetadatafd
:
266 ret
= close(metadata_fd
);
269 error_closedatastream
:
282 ret
= rmdir(outputname
);
This page took 0.035267 seconds and 4 git commands to generate.