CTF: open trace read
[babeltrace.git] / include / babeltrace / ctf / types.h
CommitLineData
d79865b9
MD
1#ifndef _BABELTRACE_CTF_TYPES_H
2#define _BABELTRACE_CTF_TYPES_H
6dc2ca62
MD
3
4/*
5 * Common Trace Format
6 *
7 * Type header
8 *
ccd7e1c8 9 * Copyright 2010 - Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
6dc2ca62 10 *
ccd7e1c8
MD
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:
de0ba614 17 *
ccd7e1c8
MD
18 * The above copyright notice and this permission notice shall be included in
19 * all copies or substantial portions of the Software.
6dc2ca62
MD
20 */
21
4c8bfb7e 22#include <babeltrace/types.h>
8c572eba
MD
23#include <sys/mman.h>
24#include <errno.h>
6dc2ca62 25#include <stdint.h>
0f980a35 26#include <unistd.h>
6dc2ca62
MD
27#include <glib.h>
28
0f980a35
MD
29struct packet_index {
30 off_t offset; /* offset of the packet in the file, in bytes */
31 size_t packet_size; /* packet size, in bits */
32 size_t content_size; /* content size, in bits */
dd2544fd
MD
33};
34
dd2544fd 35/*
0f980a35 36 * Always update stream_pos with move_pos and init_pos.
dd2544fd 37 */
0f980a35
MD
38struct stream_pos {
39 int fd; /* backing file fd. -1 if unset. */
40 GArray *packet_index; /* contains struct packet_index */
8c572eba
MD
41 int prot; /* mmap protection */
42 int flags; /* mmap flags */
dd2544fd 43
0f980a35
MD
44 /* Current position */
45 off_t mmap_offset; /* mmap offset in the file, in bytes */
46 size_t packet_size; /* current packet size, in bits */
47 size_t content_size; /* current content size, in bits */
8c572eba 48 uint32_t *content_size_loc; /* pointer to current content size */
0f980a35
MD
49 char *base; /* mmap base address */
50 size_t offset; /* offset from base, in bits */
8c572eba 51 size_t cur_index; /* current index in packet index */
dd2544fd 52
0f980a35
MD
53 int dummy; /* dummy position, for length calculation */
54};
dd2544fd 55
6dc2ca62 56/*
de0ba614
MD
57 * IMPORTANT: All lengths (len) and offsets (start, end) are expressed in bits,
58 * *not* in bytes.
59 *
60 * All write primitives, as well as read for dynamically sized entities, can
6dc2ca62
MD
61 * receive a NULL ptr/dest parameter. In this case, no write is performed, but
62 * the size is returned.
63 */
64
bed864a7 65uint64_t ctf_uint_read(struct stream_pos *pos,
f6625916 66 const struct declaration_integer *integer_declaration);
bed864a7 67int64_t ctf_int_read(struct stream_pos *pos,
f6625916 68 const struct declaration_integer *integer_declaration);
bed864a7 69void ctf_uint_write(struct stream_pos *pos,
f6625916 70 const struct declaration_integer *integer_declaration,
bed864a7
MD
71 uint64_t v);
72void ctf_int_write(struct stream_pos *pos,
f6625916 73 const struct declaration_integer *integer_declaration,
bed864a7 74 int64_t v);
6dc2ca62 75
bed864a7 76double ctf_double_read(struct stream_pos *pos,
f6625916 77 const struct declaration_float *src);
bed864a7 78void ctf_double_write(struct stream_pos *pos,
f6625916 79 const struct declaration_float *dest,
bed864a7
MD
80 double v);
81long double ctf_ldouble_read(struct stream_pos *pos,
f6625916 82 const struct declaration_float *src);
bed864a7 83void ctf_ldouble_write(struct stream_pos *pos,
f6625916 84 const struct declaration_float *dest,
bed864a7 85 long double v);
4c8bfb7e
MD
86void ctf_float_copy(struct stream_pos *destp,
87 struct stream_pos *srcp,
f6625916 88 const struct declaration_float *float_declaration);
6dc2ca62 89
bed864a7 90void ctf_string_copy(struct stream_pos *dest, struct stream_pos *src,
f6625916 91 const struct declaration_string *string_declaration);
47e0f2e2 92void ctf_string_read(char **dest, struct stream_pos *src,
f6625916 93 const struct declaration_string *string_declaration);
47e0f2e2 94void ctf_string_write(struct stream_pos *dest, const char *src,
f6625916 95 const struct declaration_string *string_declaration);
47e0f2e2 96void ctf_string_free_temp(char *string);
6dc2ca62 97
47e0f2e2 98GArray *ctf_enum_read(struct stream_pos *pos,
f6625916 99 const struct declaration_enum *src);
bed864a7 100void ctf_enum_write(struct stream_pos *pos,
f6625916 101 const struct declaration_enum *dest,
bed864a7 102 GQuark q);
11796b96 103void ctf_struct_begin(struct stream_pos *pos,
f6625916 104 const struct declaration_struct *struct_declaration);
11796b96 105void ctf_struct_end(struct stream_pos *pos,
f6625916 106 const struct declaration_struct *struct_declaration);
6b71274a 107void ctf_variant_begin(struct stream_pos *pos,
f6625916 108 const struct declaration_variant *variant_declaration);
6b71274a 109void ctf_variant_end(struct stream_pos *pos,
f6625916 110 const struct declaration_variant *variant_declaration);
d06d03db 111void ctf_array_begin(struct stream_pos *pos,
f6625916 112 const struct declaration_array *array_declaration);
d06d03db 113void ctf_array_end(struct stream_pos *pos,
f6625916 114 const struct declaration_array *array_declaration);
d06d03db 115void ctf_sequence_begin(struct stream_pos *pos,
f6625916 116 const struct declaration_sequence *sequence_declaration);
d06d03db 117void ctf_sequence_end(struct stream_pos *pos,
f6625916 118 const struct declaration_sequence *sequence_declaration);
6dc2ca62 119
0f980a35
MD
120void move_pos_slow(struct stream_pos *pos, size_t offset);
121
8c572eba
MD
122void init_pos(struct stream_pos *pos, int fd);
123void fini_pos(struct stream_pos *pos);
0f980a35
MD
124
125/*
126 * move_pos - move position of a relative bit offset
127 *
128 * TODO: allow larger files by updating base too.
129 */
130static inline
8c572eba 131void move_pos(struct stream_pos *pos, size_t bit_offset)
0f980a35 132{
8c572eba
MD
133 if (pos->fd >= 0) {
134 if (((pos->prot == PROT_READ)
135 && (pos->offset + bit_offset >= pos->content_size))
136 || ((pos->prot == PROT_WRITE)
137 && (pos->offset + bit_offset >= pos->packet_size))) {
138 move_pos_slow(pos, bit_offset);
139 return;
140 }
141 }
142 pos->offset += bit_offset;
0f980a35
MD
143}
144
145/*
146 * align_pos - align position on a bit offset (> 0)
147 *
148 * TODO: allow larger files by updating base too.
149 */
150static inline
8c572eba 151void align_pos(struct stream_pos *pos, size_t bit_offset)
0f980a35 152{
8c572eba 153 move_pos(pos, offset_align(pos->offset, bit_offset));
0f980a35
MD
154}
155
156static inline
157char *get_pos_addr(struct stream_pos *pos)
158{
159 /* Only makes sense to get the address after aligning on CHAR_BIT */
160 assert(!(pos->offset % CHAR_BIT));
161 return pos->base + (pos->offset / CHAR_BIT);
162}
163
8c572eba
MD
164static inline
165void dummy_pos(struct stream_pos *pos, struct stream_pos *dummy)
166{
167 memcpy(dummy, pos, sizeof(struct stream_pos));
168 dummy->dummy = 1;
169 dummy->fd = -1;
170}
171
172/*
173 * Check if current packet can hold data.
174 * Returns 0 for success, negative error otherwise.
175 */
176static inline
177int pos_packet(struct stream_pos *dummy)
178{
179 if (dummy->offset > dummy->packet_size)
180 return -ENOSPC;
181 return 0;
182}
183
184static inline
185void pos_pad_packet(struct stream_pos *pos)
186{
187 move_pos(pos, pos->packet_size - pos->offset);
188}
189
d79865b9 190#endif /* _BABELTRACE_CTF_TYPES_H */
This page took 0.032247 seconds and 4 git commands to generate.