Fix: add stricter checks on packet boundaries
[babeltrace.git] / include / babeltrace / ctf / types.h
1 #ifndef _BABELTRACE_CTF_TYPES_H
2 #define _BABELTRACE_CTF_TYPES_H
3
4 /*
5 * Common Trace Format
6 *
7 * Type header
8 *
9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
10 *
11 * Permission is hereby granted, free of charge, to any person obtaining a copy
12 * of this software and associated documentation files (the "Software"), to deal
13 * in the Software without restriction, including without limitation the rights
14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15 * copies of the Software, and to permit persons to whom the Software is
16 * furnished to do so, subject to the following conditions:
17 *
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
20 *
21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 * SOFTWARE.
28 */
29
30 #include <babeltrace/types.h>
31 #include <babeltrace/babeltrace-internal.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
34 #include <fcntl.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <unistd.h>
38 #include <glib.h>
39 #include <stdio.h>
40 #include <inttypes.h>
41 #include <babeltrace/mmap-align.h>
42
43 #define LAST_OFFSET_POISON ((int64_t) ~0ULL)
44
45 struct bt_stream_callbacks;
46
47 struct packet_index {
48 off_t offset; /* offset of the packet in the file, in bytes */
49 int64_t data_offset; /* offset of data within the packet, in bits */
50 uint64_t packet_size; /* packet size, in bits */
51 uint64_t content_size; /* content size, in bits */
52 uint64_t timestamp_begin;
53 uint64_t timestamp_end;
54 uint64_t events_discarded;
55 uint64_t events_discarded_len; /* length of the field, in bits */
56 };
57
58 /*
59 * Always update ctf_stream_pos with ctf_move_pos and ctf_init_pos.
60 */
61 struct ctf_stream_pos {
62 struct bt_stream_pos parent;
63 int fd; /* backing file fd. -1 if unset. */
64 FILE *index_fp; /* backing index file fp. NULL if unset. */
65 GArray *packet_cycles_index; /* contains struct packet_index in cycles */
66 GArray *packet_real_index; /* contains struct packet_index in ns */
67 int prot; /* mmap protection */
68 int flags; /* mmap flags */
69
70 /* Current position */
71 off_t mmap_offset; /* mmap offset in the file, in bytes */
72 off_t mmap_base_offset; /* offset of start of packet in mmap, in bytes */
73 uint64_t packet_size; /* current packet size, in bits */
74 uint64_t content_size; /* current content size, in bits */
75 uint64_t *content_size_loc; /* pointer to current content size */
76 struct mmap_align *base_mma;/* mmap base address */
77 int64_t offset; /* offset from base, in bits. EOF for end of file. */
78 int64_t last_offset; /* offset before the last read_event */
79 uint64_t cur_index; /* current index in packet index */
80 uint64_t last_events_discarded; /* last known amount of event discarded */
81 void (*packet_seek)(struct bt_stream_pos *pos, size_t index,
82 int whence); /* function called to switch packet */
83
84 int dummy; /* dummy position, for length calculation */
85 struct bt_stream_callbacks *cb; /* Callbacks registered for iterator. */
86 };
87
88 static inline
89 struct ctf_stream_pos *ctf_pos(struct bt_stream_pos *pos)
90 {
91 return container_of(pos, struct ctf_stream_pos, parent);
92 }
93
94 BT_HIDDEN
95 int ctf_integer_read(struct bt_stream_pos *pos, struct bt_definition *definition);
96 BT_HIDDEN
97 int ctf_integer_write(struct bt_stream_pos *pos, struct bt_definition *definition);
98 BT_HIDDEN
99 int ctf_float_read(struct bt_stream_pos *pos, struct bt_definition *definition);
100 BT_HIDDEN
101 int ctf_float_write(struct bt_stream_pos *pos, struct bt_definition *definition);
102 BT_HIDDEN
103 int ctf_string_read(struct bt_stream_pos *pos, struct bt_definition *definition);
104 BT_HIDDEN
105 int ctf_string_write(struct bt_stream_pos *pos, struct bt_definition *definition);
106 BT_HIDDEN
107 int ctf_enum_read(struct bt_stream_pos *pos, struct bt_definition *definition);
108 BT_HIDDEN
109 int ctf_enum_write(struct bt_stream_pos *pos, struct bt_definition *definition);
110 BT_HIDDEN
111 int ctf_struct_rw(struct bt_stream_pos *pos, struct bt_definition *definition);
112 BT_HIDDEN
113 int ctf_variant_rw(struct bt_stream_pos *pos, struct bt_definition *definition);
114 BT_HIDDEN
115 int ctf_array_read(struct bt_stream_pos *pos, struct bt_definition *definition);
116 BT_HIDDEN
117 int ctf_array_write(struct bt_stream_pos *pos, struct bt_definition *definition);
118 BT_HIDDEN
119 int ctf_sequence_read(struct bt_stream_pos *pos, struct bt_definition *definition);
120 BT_HIDDEN
121 int ctf_sequence_write(struct bt_stream_pos *pos, struct bt_definition *definition);
122
123 void ctf_packet_seek(struct bt_stream_pos *pos, size_t index, int whence);
124
125 int ctf_init_pos(struct ctf_stream_pos *pos, struct bt_trace_descriptor *trace,
126 int fd, int open_flags);
127 int ctf_fini_pos(struct ctf_stream_pos *pos);
128
129 /*
130 * move_pos - move position of a relative bit offset
131 *
132 * Return 1 if OK, 0 if out-of-bound.
133 *
134 * TODO: allow larger files by updating base too.
135 */
136 static inline
137 int ctf_move_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
138 {
139 uint64_t max_len;
140
141 printf_debug("ctf_move_pos test EOF: %" PRId64 "\n", pos->offset);
142 if (unlikely(pos->offset == EOF))
143 return 0;
144 if (pos->flags & PROT_READ)
145 max_len = pos->content_size;
146 else
147 max_len = pos->packet_size;
148 if (unlikely(pos->offset + bit_offset > max_len))
149 return 0;
150
151 pos->offset += bit_offset;
152 printf_debug("ctf_move_pos after increment: %" PRId64 "\n", pos->offset);
153 return 1;
154 }
155
156 /*
157 * align_pos - align position on a bit offset (> 0)
158 *
159 * Return 1 if OK, 0 if out-of-bound.
160 *
161 * TODO: allow larger files by updating base too.
162 */
163 static inline
164 int ctf_align_pos(struct ctf_stream_pos *pos, uint64_t bit_offset)
165 {
166 return ctf_move_pos(pos, offset_align(pos->offset, bit_offset));
167 }
168
169 static inline
170 char *ctf_get_pos_addr(struct ctf_stream_pos *pos)
171 {
172 /* Only makes sense to get the address after aligning on CHAR_BIT */
173 assert(!(pos->offset % CHAR_BIT));
174 return mmap_align_addr(pos->base_mma) +
175 pos->mmap_base_offset + (pos->offset / CHAR_BIT);
176 }
177
178 static inline
179 void ctf_dummy_pos(struct ctf_stream_pos *pos, struct ctf_stream_pos *dummy)
180 {
181 memcpy(dummy, pos, sizeof(struct ctf_stream_pos));
182 dummy->dummy = 1;
183 dummy->fd = -1;
184 }
185
186 /*
187 * Check if current packet can hold data.
188 * Returns 0 for success, negative error otherwise.
189 */
190 static inline
191 int ctf_pos_packet(struct ctf_stream_pos *dummy)
192 {
193 if (unlikely(dummy->offset > dummy->packet_size))
194 return -ENOSPC;
195 return 0;
196 }
197
198 static inline
199 void ctf_pos_pad_packet(struct ctf_stream_pos *pos)
200 {
201 ctf_packet_seek(&pos->parent, 0, SEEK_CUR);
202 }
203
204 static inline
205 int ctf_pos_access_ok(struct ctf_stream_pos *pos, uint64_t bit_len)
206 {
207 uint64_t max_len;
208
209 if (unlikely(pos->offset == EOF))
210 return 0;
211 if (pos->flags & PROT_READ)
212 max_len = pos->content_size;
213 else
214 max_len = pos->packet_size;
215 if (unlikely(pos->offset + bit_len > max_len))
216 return 0;
217 return 1;
218 }
219
220 /*
221 * Update the stream position to the current event. This moves to
222 * the next packet if we are located at the end of the current packet.
223 */
224 static inline
225 void ctf_pos_get_event(struct ctf_stream_pos *pos)
226 {
227 assert(pos->offset <= pos->content_size);
228 if (pos->offset == pos->content_size) {
229 printf_debug("ctf_packet_seek (before call): %" PRId64 "\n",
230 pos->offset);
231 pos->packet_seek(&pos->parent, 0, SEEK_CUR);
232 printf_debug("ctf_packet_seek (after call): %" PRId64 "\n",
233 pos->offset);
234 }
235 }
236
237 void ctf_print_timestamp(FILE *fp, struct ctf_stream_definition *stream,
238 uint64_t timestamp);
239
240 #endif /* _BABELTRACE_CTF_TYPES_H */
This page took 0.034476 seconds and 5 git commands to generate.