1b44b0b5 |
1 | /* |
2 | * parser.h: Generate helper declarations and functions to trace events |
3 | * from an event description file. |
4 | * |
5 | * Copyright (C) 2005, Mathieu Desnoyers |
6 | * Copyright (C) 2002, Xianxiu Yang |
7 | * Copyright (C) 2002, Michel Dagenais |
8 | * |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public |
11 | * License Version 2.1 as published by the Free Software Foundation. |
12 | * |
13 | * This library is distributed in the hope that it will be useful, |
14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 | * Lesser General Public License for more details. |
17 | * |
18 | * You should have received a copy of the GNU Lesser General Public |
19 | * License along with this library; if not, write to the |
20 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
21 | * Boston, MA 02111-1307, USA. |
22 | */ |
23 | |
90395b4b |
24 | #ifndef PARSER_H |
25 | #define PARSER_H |
26 | |
27 | /* Extensible array container */ |
28 | |
29 | typedef struct _sequence { |
30 | int size; |
31 | int position; |
32 | void **array; |
90699b2b |
33 | } sequence_t; |
90395b4b |
34 | |
90699b2b |
35 | void sequence_init(sequence_t *t); |
36 | void sequence_dispose(sequence_t *t); |
37 | void sequence_push(sequence_t *t, void *elem); |
38 | void *sequence_pop(sequence_t *t); |
90395b4b |
39 | |
40 | |
41 | /* Hash table */ |
42 | |
43 | typedef struct _table { |
90699b2b |
44 | sequence_t keys; |
45 | sequence_t values; |
46 | } table_t; |
90395b4b |
47 | |
90699b2b |
48 | void table_init(table_t *t); |
49 | void table_dispose(table_t *t); |
50 | void table_insert(table_t *t, char *key, void *value); |
51 | void *table_find(table_t *t, char *key); |
52 | void table_insert_int(table_t *t, int *key, void *value); |
53 | void *table_find_int(table_t *t, int *key); |
90395b4b |
54 | |
55 | |
56 | /* Token types */ |
57 | |
58 | typedef enum _token_type { |
59 | ENDFILE, |
60 | FORWARDSLASH, |
61 | LANGLEBRACKET, |
62 | RANGLEBRACKET, |
63 | EQUAL, |
64 | QUOTEDSTRING, |
65 | NUMBER, |
66 | NAME |
90699b2b |
67 | } token_type_t; |
90395b4b |
68 | |
69 | |
70 | /* State associated with a file being parsed */ |
71 | typedef struct _parse_file { |
72 | char *name; |
73 | FILE * fp; |
74 | int lineno; |
75 | char *buffer; |
90699b2b |
76 | token_type_t type; |
90395b4b |
77 | int unget; |
78 | void (*error) (struct _parse_file *, char *); |
90699b2b |
79 | } parse_file_t; |
80 | |
81 | void ungetToken(parse_file_t * in); |
82 | char *getToken(parse_file_t *in); |
83 | char *getForwardslash(parse_file_t *in); |
84 | char *getLAnglebracket(parse_file_t *in); |
85 | char *getRAnglebracket(parse_file_t *in); |
86 | char *getQuotedString(parse_file_t *in); |
87 | char *getName(parse_file_t *in); |
88 | int getNumber(parse_file_t *in); |
89 | char *getEqual(parse_file_t *in); |
90 | char seekNextChar(parse_file_t *in); |
91 | |
92 | void skipComment(parse_file_t * in); |
93 | void skipEOL(parse_file_t * in); |
90395b4b |
94 | |
95 | /* Some constants */ |
96 | |
97 | static const int BUFFER_SIZE = 1024; |
98 | |
99 | |
100 | /* Events data types */ |
101 | |
102 | typedef enum _data_type { |
f104d082 |
103 | INT_FIXED, |
104 | UINT_FIXED, |
105 | POINTER, |
106 | CHAR, |
107 | UCHAR, |
108 | SHORT, |
109 | USHORT, |
90395b4b |
110 | INT, |
111 | UINT, |
90395b4b |
112 | LONG, |
113 | ULONG, |
114 | SIZE_T, |
115 | SSIZE_T, |
116 | OFF_T, |
117 | FLOAT, |
118 | STRING, |
119 | ENUM, |
120 | ARRAY, |
121 | SEQUENCE, |
122 | STRUCT, |
123 | UNION, |
124 | NONE |
90699b2b |
125 | } data_type_t; |
90395b4b |
126 | |
90395b4b |
127 | typedef struct _type_descriptor { |
128 | char * type_name; //used for named type |
90699b2b |
129 | data_type_t type; |
90395b4b |
130 | char *fmt; |
f104d082 |
131 | size_t size; |
90699b2b |
132 | sequence_t labels; // for enumeration |
f104d082 |
133 | sequence_t labels_values; // for enumeration |
90699b2b |
134 | sequence_t labels_description; |
90395b4b |
135 | int already_printed; |
f104d082 |
136 | sequence_t fields; // for structure, array and sequence (field_t type) |
7df766d3 |
137 | int custom_write; /* Should we use a custom write function ? */ |
90699b2b |
138 | } type_descriptor_t; |
90395b4b |
139 | |
140 | |
90395b4b |
141 | |
f104d082 |
142 | /* Fields within types or events */ |
90395b4b |
143 | typedef struct _field{ |
144 | char *name; |
145 | char *description; |
90699b2b |
146 | type_descriptor_t *type; |
147 | } field_t; |
90395b4b |
148 | |
149 | |
150 | /* Events definitions */ |
151 | |
152 | typedef struct _event { |
153 | char *name; |
154 | char *description; |
f104d082 |
155 | //type_descriptor_t *type; |
156 | sequence_t fields; /* event fields */ |
90395b4b |
157 | int per_trace; /* Is the event able to be logged to a specific trace ? */ |
158 | int per_tracefile; /* Must we log this event in a specific tracefile ? */ |
90699b2b |
159 | } event_t; |
90395b4b |
160 | |
161 | typedef struct _facility { |
162 | char * name; |
163 | char * capname; |
f5d7967f |
164 | char * arch; |
90395b4b |
165 | char * description; |
90699b2b |
166 | sequence_t events; |
f104d082 |
167 | sequence_t unnamed_types; //FIXME : remove |
90699b2b |
168 | table_t named_types; |
f104d082 |
169 | unsigned int checksum; |
90699b2b |
170 | } facility_t; |
171 | |
f104d082 |
172 | int getSizeindex(unsigned int value); |
173 | unsigned long long int getSize(parse_file_t *in); |
90699b2b |
174 | unsigned long getTypeChecksum(unsigned long aCrc, type_descriptor_t * type); |
175 | |
176 | void parseFacility(parse_file_t *in, facility_t * fac); |
177 | void parseEvent(parse_file_t *in, event_t *ev, sequence_t * unnamed_types, |
178 | table_t * named_types); |
179 | void parseTypeDefinition(parse_file_t *in, |
180 | sequence_t * unnamed_types, table_t * named_types); |
181 | type_descriptor_t *parseType(parse_file_t *in, |
182 | type_descriptor_t *t, sequence_t * unnamed_types, table_t * named_types); |
f104d082 |
183 | void parseFields(parse_file_t *in, field_t *f, |
184 | sequence_t * unnamed_types, |
185 | table_t * named_types, |
186 | int tag); |
90699b2b |
187 | void checkNamedTypesImplemented(table_t * namedTypes); |
188 | type_descriptor_t * find_named_type(char *name, table_t * named_types); |
189 | void generateChecksum(char * facName, |
f104d082 |
190 | unsigned int * checksum, sequence_t * events); |
90395b4b |
191 | |
192 | |
193 | /* get attributes */ |
90699b2b |
194 | char * getNameAttribute(parse_file_t *in); |
195 | char * getFormatAttribute(parse_file_t *in); |
196 | int getSizeAttribute(parse_file_t *in); |
197 | int getValueAttribute(parse_file_t *in); |
198 | char * getValueStrAttribute(parse_file_t *in); |
90395b4b |
199 | |
90699b2b |
200 | char * getDescription(parse_file_t *in); |
90395b4b |
201 | |
202 | |
90395b4b |
203 | /* Dynamic memory allocation and freeing */ |
204 | |
205 | void * memAlloc(int size); |
206 | char *allocAndCopy(char * str); |
207 | char *appendString(char *s, char *suffix); |
90699b2b |
208 | void freeTypes(sequence_t *t); |
209 | void freeType(type_descriptor_t * td); |
210 | void freeEvents(sequence_t *t); |
211 | void freeNamedType(table_t * t); |
212 | void error_callback(parse_file_t *in, char *msg); |
90395b4b |
213 | |
214 | |
215 | //checksum part |
216 | static const unsigned int crctab32[] = |
217 | { |
218 | #include "crc32.tab" |
219 | }; |
220 | |
221 | static inline unsigned long |
222 | partial_crc32_one(unsigned char c, unsigned long crc) |
223 | { |
224 | return crctab32[(crc ^ c) & 0xff] ^ (crc >> 8); |
225 | } |
226 | |
227 | static inline unsigned long |
228 | partial_crc32(const char *s, unsigned long crc) |
229 | { |
230 | while (*s) |
231 | crc = partial_crc32_one(*s++, crc); |
232 | return crc; |
233 | } |
234 | |
235 | static inline unsigned long |
236 | crc32(const char *s) |
237 | { |
238 | return partial_crc32(s, 0xffffffff) ^ 0xffffffff; |
239 | } |
240 | |
241 | |
f104d082 |
242 | extern char *intOutputTypes[]; |
243 | |
244 | extern char *uintOutputTypes[]; |
245 | |
246 | extern char *floatOutputTypes[]; |
247 | |
248 | |
249 | |
250 | |
90395b4b |
251 | #endif // PARSER_H |