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>
29 #include <uuid/uuid.h>
32 #include <babeltrace/babeltrace.h>
33 #include <babeltrace/ctf/types.h>
35 int babeltrace_debug
= 0;
40 void write_packet_header(struct ctf_stream_pos
*pos
, uuid_t uuid
)
42 struct ctf_stream_pos dummy
;
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
));
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
);
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
));
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
);
66 void write_packet_context(struct ctf_stream_pos
*pos
)
68 struct ctf_stream_pos dummy
;
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
));
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
);
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
));
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
);
93 void trace_string(char *line
, struct ctf_stream_pos
*pos
, size_t len
)
95 struct ctf_stream_pos dummy
;
98 printf_debug("read: %s\n", line
);
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
)) {
104 /* TODO write content size */
105 ctf_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);
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
);
122 void trace_text(FILE *input
, int output
)
124 struct ctf_stream_pos pos
;
126 char *line
= NULL
, *nl
;
129 ctf_init_pos(&pos
, output
, O_WRONLY
);
131 write_packet_header(&pos
, s_uuid
);
132 write_packet_context(&pos
);
134 len
= getline(&line
, &linesize
, input
);
137 nl
= strrchr(line
, '\n');
140 trace_string(line
, &pos
, nl
- line
+ 1);
145 int main(int argc
, char **argv
)
149 ret
= unlink("dummystream");
154 fd
= open("dummystream", O_RDWR
|O_CREAT
, S_IRUSR
|S_IWUSR
);
160 ret
= uuid_parse("2a6422d0-6cee-11e0-8c08-cb07d7b3a564", s_uuid
);
162 printf("uuid parse error\n");
167 trace_text(stdin
, fd
);
This page took 0.033088 seconds and 5 git commands to generate.