449cb9d7 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Xiangxiu Yang |
3aee1200 |
3 | * 2005 Mathieu Desnoyers |
449cb9d7 |
4 | * |
1b44b0b5 |
5 | * This library is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU Lesser General Public |
7 | * License Version 2.1 as published by the Free Software Foundation. |
449cb9d7 |
8 | * |
1b44b0b5 |
9 | * This library is distributed in the hope that it will be useful, |
449cb9d7 |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
1b44b0b5 |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 | * Lesser General Public License for more details. |
449cb9d7 |
13 | * |
1b44b0b5 |
14 | * You should have received a copy of the GNU Lesser General Public |
15 | * License along with this library; if not, write to the |
16 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
17 | * Boston, MA 02111-1307, USA. |
449cb9d7 |
18 | */ |
19 | |
4e4d11b3 |
20 | #ifdef HAVE_CONFIG_H |
21 | #include <config.h> |
22 | #endif |
23 | |
6cd62ccf |
24 | #include <stdlib.h> |
25 | #include <string.h> |
26 | #include <stdio.h> |
cdf90f40 |
27 | #include <glib.h> |
45e14832 |
28 | #include <sys/types.h> |
29 | #include <sys/stat.h> |
30 | #include <fcntl.h> |
31 | |
32 | |
6cd62ccf |
33 | |
6cd62ccf |
34 | #include "parser.h" |
a5dcde2f |
35 | #include <ltt/ltt.h> |
36 | #include "ltt-private.h" |
6cd62ccf |
37 | #include <ltt/facility.h> |
38 | |
86005ded |
39 | #ifndef g_open |
40 | #define g_open open |
41 | #endif |
42 | |
45e14832 |
43 | #define g_close close |
44 | |
6cd62ccf |
45 | /* search for the (named) type in the table, if it does not exist |
46 | create a new one */ |
90699b2b |
47 | LttType * lookup_named_type(LttFacility *fac, type_descriptor_t * td); |
6cd62ccf |
48 | |
49 | /* construct directed acyclic graph for types, and tree for fields */ |
2312de30 |
50 | void construct_fields(LttFacility *fac, |
c1161b10 |
51 | LttField *field, |
2312de30 |
52 | field_t *fld); |
6cd62ccf |
53 | |
54 | /* generate the facility according to the events belongin to it */ |
8d1e6362 |
55 | void generateFacility(LttFacility * f, facility_t * fac, |
3aee1200 |
56 | guint32 checksum); |
6cd62ccf |
57 | |
58 | /* functions to release the memory occupied by a facility */ |
963b5f2d |
59 | void freeFacility(LttFacility * facility); |
60 | void freeEventtype(LttEventType * evType); |
2312de30 |
61 | void freeLttType(LttType * type); |
963b5f2d |
62 | void freeLttField(LttField * fld); |
908f42fa |
63 | void freeLttNamedType(LttType * type); |
6cd62ccf |
64 | |
65 | |
66 | /***************************************************************************** |
67 | *Function name |
963b5f2d |
68 | * ltt_facility_open : open facilities |
6cd62ccf |
69 | *Input params |
963b5f2d |
70 | * t : the trace containing the facilities |
6cd62ccf |
71 | * pathname : the path name of the facility |
3aee1200 |
72 | * |
29e7c5b3 |
73 | * Open the facility corresponding to the right checksum. |
74 | * |
3aee1200 |
75 | *returns 0 on success, 1 on error. |
6cd62ccf |
76 | ****************************************************************************/ |
77 | |
3aee1200 |
78 | int ltt_facility_open(LttFacility *f, LttTrace * t, gchar * pathname) |
6cd62ccf |
79 | { |
a0c1f622 |
80 | int ret = 0; |
45e14832 |
81 | gchar *token; |
90699b2b |
82 | parse_file_t in; |
8d1e6362 |
83 | facility_t * fac; |
2312de30 |
84 | unsigned int checksum; |
45e14832 |
85 | gchar buffer[BUFFER_SIZE]; |
29e7c5b3 |
86 | gboolean generated = FALSE; |
6cd62ccf |
87 | |
45e14832 |
88 | in.buffer = &(buffer[0]); |
6cd62ccf |
89 | in.lineno = 0; |
90 | in.error = error_callback; |
963b5f2d |
91 | in.name = pathname; |
ba8a51cd |
92 | in.unget = 0; |
6cd62ccf |
93 | |
90699b2b |
94 | in.fp = fopen(in.name, "r"); |
cb03932a |
95 | if(in.fp == NULL) { |
3aee1200 |
96 | g_warning("cannot open facility description file %s", |
97 | in.name); |
a0c1f622 |
98 | ret = 1; |
99 | goto open_error; |
3aee1200 |
100 | } |
45e14832 |
101 | |
6cd62ccf |
102 | while(1){ |
103 | token = getToken(&in); |
104 | if(in.type == ENDFILE) break; |
e6d018d4 |
105 | |
c1161b10 |
106 | if(g_ascii_strcasecmp(token, "<")) in.error(&in,"not a facility file"); |
107 | token = getName(&in); |
108 | if(g_ascii_strcasecmp(token, "?")) in.error(&in,"not a facility file"); |
109 | token = getName(&in); |
110 | if(g_ascii_strcasecmp(token, "xml")) in.error(&in,"not a facility file"); |
111 | token = getName(&in); |
112 | if(g_ascii_strcasecmp(token, "version")) in.error(&in,"not a facility file"); |
113 | token = getName(&in); |
114 | if(g_ascii_strcasecmp(token, "=")) in.error(&in,"not a facility file"); |
115 | token = getQuotedString(&in); |
116 | if(g_ascii_strcasecmp(token, "1.0")) in.error(&in,"not a facility file"); |
117 | token = getName(&in); |
118 | if(g_ascii_strcasecmp(token, "?")) in.error(&in,"not a facility file"); |
119 | token = getToken(&in); |
120 | if(g_ascii_strcasecmp(token, ">")) in.error(&in,"not a facility file"); |
121 | |
122 | token = getToken(&in); |
123 | |
45e14832 |
124 | if(g_ascii_strcasecmp(token, "<")) in.error(&in,"not a facility file"); |
963b5f2d |
125 | token = getName(&in); |
45e14832 |
126 | |
127 | if(g_ascii_strcasecmp("facility",token) == 0) { |
8d1e6362 |
128 | fac = g_new(facility_t, 1); |
963b5f2d |
129 | fac->name = NULL; |
130 | fac->description = NULL; |
131 | sequence_init(&(fac->events)); |
132 | table_init(&(fac->named_types)); |
133 | sequence_init(&(fac->unnamed_types)); |
134 | |
135 | parseFacility(&in, fac); |
136 | |
137 | //check if any namedType is not defined |
90699b2b |
138 | checkNamedTypesImplemented(&fac->named_types); |
963b5f2d |
139 | |
90699b2b |
140 | generateChecksum(fac->name, &checksum, &fac->events); |
cba098f3 |
141 | // FIXME if(checksum == f->checksum) { |
29e7c5b3 |
142 | generateFacility(f, fac, checksum); |
143 | generated = TRUE; |
cba098f3 |
144 | //} |
145 | if (checksum != f->checksum) |
146 | g_warning("Facility checksum mismatch for facility %s : kernel 0x%X vs " |
147 | "XML 0x%X\n", fac->name, f->checksum, checksum); |
963b5f2d |
148 | |
45e14832 |
149 | g_free(fac->name); |
d2ace3a9 |
150 | free(fac->capname); |
45e14832 |
151 | g_free(fac->description); |
963b5f2d |
152 | freeEvents(&fac->events); |
153 | sequence_dispose(&fac->events); |
154 | freeNamedType(&fac->named_types); |
155 | table_dispose(&fac->named_types); |
156 | freeTypes(&fac->unnamed_types); |
157 | sequence_dispose(&fac->unnamed_types); |
cf1307af |
158 | g_free(fac); |
129fd24a |
159 | if(generated) break; /* use the first good match */ |
6cd62ccf |
160 | } |
3aee1200 |
161 | else { |
162 | g_warning("facility token was expected in file %s", in.name); |
a0c1f622 |
163 | ret = 1; |
3aee1200 |
164 | goto parse_error; |
165 | } |
6cd62ccf |
166 | } |
29e7c5b3 |
167 | |
3aee1200 |
168 | parse_error: |
90699b2b |
169 | fclose(in.fp); |
a0c1f622 |
170 | open_error: |
90699b2b |
171 | |
986e2a7c |
172 | if(!generated) { |
173 | g_warning("Cannot find facility %s, checksum 0x%X", |
174 | g_quark_to_string(f->name), f->checksum); |
175 | ret = 1; |
176 | } |
29e7c5b3 |
177 | |
a0c1f622 |
178 | return ret; |
6cd62ccf |
179 | } |
180 | |
181 | |
182 | /***************************************************************************** |
183 | *Function name |
184 | * generateFacility : generate facility, internal function |
185 | *Input params |
963b5f2d |
186 | * facility : LttFacilty structure |
187 | * fac : facility structure |
6cd62ccf |
188 | * checksum : checksum of the facility |
6cd62ccf |
189 | ****************************************************************************/ |
190 | |
3aee1200 |
191 | void generateFacility(LttFacility *f, facility_t *fac, guint32 checksum) |
6cd62ccf |
192 | { |
963b5f2d |
193 | char * facilityName = fac->name; |
90699b2b |
194 | sequence_t * events = &fac->events; |
f104d082 |
195 | unsigned int i, j; |
963b5f2d |
196 | LttType * type; |
f104d082 |
197 | table_t *named_types = &fac->named_types; |
6cd62ccf |
198 | |
3aee1200 |
199 | g_assert(f->name == g_quark_from_string(facilityName)); |
cba098f3 |
200 | //g_assert(f->checksum == checksum); |
3aee1200 |
201 | |
202 | //f->event_number = events->position; |
6cd62ccf |
203 | |
204 | //initialize inner structures |
3aee1200 |
205 | f->events = g_array_sized_new (FALSE, TRUE, sizeof(LttEventType), |
206 | events->position); |
207 | //f->events = g_new(LttEventType*,f->event_number); |
208 | f->events = g_array_set_size(f->events, events->position); |
209 | |
210 | g_datalist_init(&f->events_by_name); |
2312de30 |
211 | // g_datalist_init(&f->named_types); |
f104d082 |
212 | #if 0 |
213 | /* The first day, he created the named types */ |
214 | |
215 | for(i=0; i<named_types->keys.position; i++) { |
216 | GQuark name = g_quark_from_string((char*)named_types->keys.array[i]); |
217 | type_descriptor_t *td = (type_descriptor_t*)named_types->values.array[i]; |
218 | |
219 | /* Create the type */ |
220 | type = g_new(LttType,1); |
221 | type->type_name = name; |
222 | type->type_class = td->type; |
223 | if(td->fmt) type->fmt = g_strdup(td->fmt); |
224 | else type->fmt = NULL; |
225 | type->size = td->size; |
226 | type->enum_strings = NULL; |
227 | type->element_type = NULL; |
228 | type->element_number = 0; |
229 | |
230 | construct_types_and_fields(type, td, NULL, NULL, ...); |
231 | |
232 | g_datalist_id_set_data_full(&fac->named_types, name, |
233 | type, (GDestroyNotify)freeLttNamedType); |
234 | |
235 | } |
236 | #endif //0 |
237 | /* The second day, he created the event fields and types */ |
238 | //for each event, construct field and type acyclic graph |
6cd62ccf |
239 | for(i=0;i<events->position;i++){ |
c1161b10 |
240 | event_t *parser_event = (event_t*)events->array[i]; |
f104d082 |
241 | LttEventType *event_type = &g_array_index(f->events, LttEventType, i); |
6cd62ccf |
242 | |
3aee1200 |
243 | event_type->name = |
f104d082 |
244 | g_quark_from_string(parser_event->name); |
3aee1200 |
245 | |
246 | g_datalist_id_set_data(&f->events_by_name, event_type->name, |
247 | event_type); |
248 | |
249 | event_type->description = |
f104d082 |
250 | g_strdup(parser_event->description); |
6cd62ccf |
251 | |
3aee1200 |
252 | event_type->index = i; |
f104d082 |
253 | event_type->facility = f; |
6cd62ccf |
254 | |
dd3a6d39 |
255 | event_type->has_compact_data = parser_event->compact_data; |
256 | |
f104d082 |
257 | event_type->fields = g_array_sized_new(FALSE, TRUE, |
258 | sizeof(LttField), parser_event->fields.position); |
259 | event_type->fields = |
260 | g_array_set_size(event_type->fields, parser_event->fields.position); |
261 | g_datalist_init(&event_type->fields_by_name); |
262 | |
263 | for(j=0; j<parser_event->fields.position; j++) { |
264 | LttField *field = &g_array_index(event_type->fields, LttField, j); |
265 | field_t *parser_field = (field_t*)parser_event->fields.array[j]; |
266 | |
2312de30 |
267 | construct_fields(f, field, parser_field); |
f104d082 |
268 | g_datalist_id_set_data(&event_type->fields_by_name, |
269 | field->name, |
270 | field); |
8710c6c7 |
271 | } |
f104d082 |
272 | } |
273 | |
274 | /* What about 2 days weeks ? */ |
6cd62ccf |
275 | } |
276 | |
277 | |
278 | /***************************************************************************** |
279 | *Function name |
3aee1200 |
280 | * construct_types_and_fields : construct field tree and type graph, |
6cd62ccf |
281 | * internal recursion function |
282 | *Input params |
283 | * fac : facility struct |
f104d082 |
284 | * field : destination lttv field |
285 | * fld : source parser field |
6cd62ccf |
286 | ****************************************************************************/ |
287 | |
f104d082 |
288 | //DONE |
289 | //make the change for arrays and sequences |
290 | //no more root field. -> change this for an array of fields. |
291 | // Compute the field size here. |
292 | // Flag fields as "VARIABLE OFFSET" or "FIXED OFFSET" : as soon as |
293 | // a field with a variable size is found, all the following fields must |
294 | // be flagged with "VARIABLE OFFSET", this will be done by the offset |
295 | // precomputation. |
296 | |
3aee1200 |
297 | |
f104d082 |
298 | void construct_fields(LttFacility *fac, |
c1161b10 |
299 | LttField *field, |
f104d082 |
300 | field_t *fld) |
301 | { |
302 | guint len; |
303 | type_descriptor_t *td; |
2312de30 |
304 | LttType *type; |
f104d082 |
305 | |
c1161b10 |
306 | if(fld->name) |
307 | field->name = g_quark_from_string(fld->name); |
308 | else |
309 | fld->name = 0; |
ae3d0f50 |
310 | |
f104d082 |
311 | if(fld->description) { |
312 | len = strlen(fld->description); |
313 | field->description = g_new(gchar, len+1); |
314 | strcpy(field->description, fld->description); |
315 | } |
316 | field->dynamic_offsets = NULL; |
317 | type = &field->field_type; |
318 | td = fld->type; |
319 | |
320 | type->enum_map = NULL; |
321 | type->fields = NULL; |
322 | type->fields_by_name = NULL; |
c1161b10 |
323 | type->network = td->network; |
743e50fd |
324 | |
f104d082 |
325 | switch(td->type) { |
326 | case INT_FIXED: |
327 | type->type_class = LTT_INT_FIXED; |
328 | type->size = td->size; |
329 | break; |
330 | case UINT_FIXED: |
331 | type->type_class = LTT_UINT_FIXED; |
332 | type->size = td->size; |
333 | break; |
334 | case POINTER: |
335 | type->type_class = LTT_POINTER; |
336 | type->size = fac->pointer_size; |
337 | break; |
338 | case CHAR: |
339 | type->type_class = LTT_CHAR; |
340 | type->size = td->size; |
341 | break; |
342 | case UCHAR: |
343 | type->type_class = LTT_UCHAR; |
344 | type->size = td->size; |
743e50fd |
345 | g_assert(type->size != 0); |
f104d082 |
346 | break; |
347 | case SHORT: |
348 | type->type_class = LTT_SHORT; |
349 | type->size = td->size; |
350 | break; |
351 | case USHORT: |
352 | type->type_class = LTT_USHORT; |
353 | type->size = td->size; |
354 | break; |
355 | case INT: |
356 | type->type_class = LTT_INT; |
357 | type->size = fac->int_size; |
358 | break; |
359 | case UINT: |
360 | type->type_class = LTT_UINT; |
361 | type->size = fac->int_size; |
743e50fd |
362 | g_assert(type->size != 0); |
f104d082 |
363 | break; |
364 | case LONG: |
365 | type->type_class = LTT_LONG; |
366 | type->size = fac->long_size; |
367 | break; |
368 | case ULONG: |
369 | type->type_class = LTT_ULONG; |
370 | type->size = fac->long_size; |
371 | break; |
372 | case SIZE_T: |
373 | type->type_class = LTT_SIZE_T; |
374 | type->size = fac->size_t_size; |
375 | break; |
376 | case SSIZE_T: |
377 | type->type_class = LTT_SSIZE_T; |
378 | type->size = fac->size_t_size; |
379 | break; |
380 | case OFF_T: |
381 | type->type_class = LTT_OFF_T; |
382 | type->size = fac->size_t_size; |
383 | break; |
384 | case FLOAT: |
385 | type->type_class = LTT_FLOAT; |
386 | type->size = td->size; |
387 | break; |
388 | case STRING: |
389 | type->type_class = LTT_STRING; |
390 | type->size = 0; |
391 | break; |
392 | case ENUM: |
393 | type->type_class = LTT_ENUM; |
394 | type->size = fac->int_size; |
395 | { |
396 | guint i; |
743e50fd |
397 | type->enum_map = g_hash_table_new(g_direct_hash, g_direct_equal); |
f104d082 |
398 | for(i=0; i<td->labels.position; i++) { |
2312de30 |
399 | GQuark value = g_quark_from_string((char*)td->labels.array[i]); |
400 | gint key = *(int*)td->labels_values.array[i]; |
401 | g_hash_table_insert(type->enum_map, (gpointer)key, (gpointer)value); |
f104d082 |
402 | } |
403 | } |
743e50fd |
404 | g_assert(type->size != 0); |
f104d082 |
405 | break; |
406 | case ARRAY: |
407 | type->type_class = LTT_ARRAY; |
408 | type->size = td->size; |
409 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
410 | td->fields.position); |
411 | type->fields = g_array_set_size(type->fields, td->fields.position); |
412 | { |
413 | guint i; |
414 | |
415 | for(i=0; i<td->fields.position; i++) { |
416 | field_t *schild = (field_t*)td->fields.array[i]; |
417 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
418 | |
419 | construct_fields(fac, dchild, schild); |
420 | } |
421 | } |
422 | break; |
423 | case SEQUENCE: |
424 | type->type_class = LTT_SEQUENCE; |
425 | type->size = 0; |
426 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
427 | td->fields.position); |
428 | type->fields = g_array_set_size(type->fields, td->fields.position); |
429 | { |
430 | guint i; |
431 | |
432 | for(i=0; i<td->fields.position; i++) { |
433 | field_t *schild = (field_t*)td->fields.array[i]; |
434 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
435 | |
436 | construct_fields(fac, dchild, schild); |
437 | } |
438 | } |
439 | break; |
440 | case STRUCT: |
441 | type->type_class = LTT_STRUCT; |
442 | type->size = 0; // Size not calculated by the parser. |
443 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
444 | td->fields.position); |
445 | type->fields = g_array_set_size(type->fields, td->fields.position); |
446 | g_datalist_init(&type->fields_by_name); |
447 | { |
448 | guint i; |
449 | |
450 | for(i=0; i<td->fields.position; i++) { |
451 | field_t *schild = (field_t*)td->fields.array[i]; |
452 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
453 | |
454 | construct_fields(fac, dchild, schild); |
455 | g_datalist_id_set_data(&type->fields_by_name, |
456 | dchild->name, |
457 | dchild); |
458 | } |
459 | } |
460 | break; |
461 | case UNION: |
462 | type->type_class = LTT_UNION; |
463 | type->size = 0; // Size not calculated by the parser. |
464 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
465 | td->fields.position); |
466 | type->fields = g_array_set_size(type->fields, td->fields.position); |
467 | g_datalist_init(&type->fields_by_name); |
468 | { |
469 | guint i; |
470 | |
471 | for(i=0; i<td->fields.position; i++) { |
472 | field_t *schild = (field_t*)td->fields.array[i]; |
473 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
474 | |
475 | construct_fields(fac, dchild, schild); |
476 | g_datalist_id_set_data(&type->fields_by_name, |
477 | dchild->name, |
478 | dchild); |
479 | } |
480 | } |
481 | break; |
482 | case NONE: |
483 | default: |
484 | g_error("construct_fields : unknown type"); |
485 | } |
486 | |
487 | field->field_size = type->size; |
488 | |
489 | /* Put the fields as "variable" offset to root first. Then, |
490 | * the offset precomputation will only have to set the FIELD_FIXED until |
491 | * it reaches the first variable length field, then stop. |
492 | */ |
493 | field->fixed_root = FIELD_VARIABLE; |
494 | |
495 | if(td->fmt) { |
496 | len = strlen(td->fmt); |
497 | type->fmt = g_new(gchar, len+1); |
498 | strcpy(type->fmt, td->fmt); |
499 | } |
500 | } |
501 | |
502 | |
503 | |
504 | #if 0 |
90699b2b |
505 | void construct_types_and_fields(LttFacility * fac, type_descriptor_t * td, |
3aee1200 |
506 | LttField * fld) |
507 | { |
a0c1f622 |
508 | int i; |
90699b2b |
509 | type_descriptor_t * tmpTd; |
3aee1200 |
510 | |
511 | switch(td->type) { |
cb03932a |
512 | case INT: |
513 | case UINT: |
514 | case FLOAT: |
515 | fld->field_type->size = td->size; |
516 | break; |
517 | case POINTER: |
518 | case LONG: |
519 | case ULONG: |
520 | case SIZE_T: |
521 | case SSIZE_T: |
522 | case OFF_T: |
523 | fld->field_type->size = 0; |
524 | break; |
525 | case STRING: |
526 | fld->field_type->size = 0; |
527 | break; |
528 | case ENUM: |
529 | fld->field_type->element_number = td->labels.position; |
530 | fld->field_type->enum_strings = g_new(GQuark,td->labels.position); |
531 | for(i=0;i<td->labels.position;i++){ |
532 | fld->field_type->enum_strings[i] |
533 | = g_quark_from_string(((char*)(td->labels.array[i]))); |
534 | } |
535 | fld->field_type->size = td->size; |
536 | break; |
537 | |
538 | case ARRAY: |
539 | fld->field_type->element_number = (unsigned)td->size; |
540 | case SEQUENCE: |
3aee1200 |
541 | fld->field_type->element_type = g_new(LttType*,1); |
542 | tmpTd = td->nested_type; |
543 | fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd); |
544 | fld->child = g_new(LttField*, 1); |
545 | fld->child[0] = g_new(LttField, 1); |
546 | |
547 | fld->child[0]->field_type = fld->field_type->element_type[0]; |
548 | fld->child[0]->offset_root = 0; |
549 | fld->child[0]->fixed_root = FIELD_UNKNOWN; |
550 | fld->child[0]->offset_parent = 0; |
551 | fld->child[0]->fixed_parent = FIELD_UNKNOWN; |
552 | fld->child[0]->field_size = 0; |
553 | fld->child[0]->fixed_size = FIELD_UNKNOWN; |
554 | fld->child[0]->parent = fld; |
555 | fld->child[0]->child = NULL; |
556 | fld->child[0]->current_element = 0; |
557 | construct_types_and_fields(fac, tmpTd, fld->child[0]); |
558 | break; |
cb03932a |
559 | |
560 | case STRUCT: |
561 | case UNION: |
3aee1200 |
562 | fld->field_type->element_number = td->fields.position; |
563 | |
564 | g_assert(fld->field_type->element_type == NULL); |
565 | fld->field_type->element_type = g_new(LttType*, td->fields.position); |
566 | |
567 | fld->child = g_new(LttField*, td->fields.position); |
568 | for(i=0;i<td->fields.position;i++){ |
90699b2b |
569 | tmpTd = ((field_t*)(td->fields.array[i]))->type; |
3aee1200 |
570 | |
c1161b10 |
571 | fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd); |
3aee1200 |
572 | fld->child[i] = g_new(LttField,1); |
573 | |
574 | // fld->child[i]->field_pos = i; |
575 | fld->child[i]->field_type = fld->field_type->element_type[i]; |
576 | |
577 | fld->child[i]->field_type->element_name |
c1161b10 |
578 | = g_quark_from_string(((field_t*)(td->fields.array[i]))->name); |
3aee1200 |
579 | |
580 | fld->child[i]->offset_root = 0; |
581 | fld->child[i]->fixed_root = FIELD_UNKNOWN; |
582 | fld->child[i]->offset_parent = 0; |
583 | fld->child[i]->fixed_parent = FIELD_UNKNOWN; |
584 | fld->child[i]->field_size = 0; |
585 | fld->child[i]->fixed_size = FIELD_UNKNOWN; |
586 | fld->child[i]->parent = fld; |
587 | fld->child[i]->child = NULL; |
588 | fld->child[i]->current_element = 0; |
589 | construct_types_and_fields(fac, tmpTd, fld->child[i]); |
590 | } |
3aee1200 |
591 | break; |
cb03932a |
592 | |
3aee1200 |
593 | default: |
594 | g_error("construct_types_and_fields : unknown type"); |
595 | } |
596 | |
597 | |
598 | } |
599 | |
f104d082 |
600 | #endif //0 |
3aee1200 |
601 | |
602 | #if 0 |
603 | void construct_types_and_fields(LttFacility * fac, type_descriptor * td, |
963b5f2d |
604 | LttField * fld) |
6cd62ccf |
605 | { |
908f42fa |
606 | int i, flag; |
6cd62ccf |
607 | type_descriptor * tmpTd; |
608 | |
609 | // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE) |
610 | // fld->field_size = 0; |
611 | // else fld->field_size = -1; |
612 | |
613 | if(td->type == LTT_ENUM){ |
614 | fld->field_type->element_number = td->labels.position; |
3aee1200 |
615 | fld->field_type->enum_strings = g_new(GQuark,td->labels.position); |
6cd62ccf |
616 | for(i=0;i<td->labels.position;i++){ |
617 | fld->field_type->enum_strings[i] |
3aee1200 |
618 | = g_quark_from_string(((char*)(td->labels.array[i]))); |
6cd62ccf |
619 | } |
620 | }else if(td->type == LTT_ARRAY || td->type == LTT_SEQUENCE){ |
621 | if(td->type == LTT_ARRAY) |
622 | fld->field_type->element_number = (unsigned)td->size; |
963b5f2d |
623 | fld->field_type->element_type = g_new(LttType*,1); |
6cd62ccf |
624 | tmpTd = td->nested_type; |
625 | fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd); |
963b5f2d |
626 | fld->child = g_new(LttField*, 1); |
627 | fld->child[0] = g_new(LttField, 1); |
6cd62ccf |
628 | |
3aee1200 |
629 | // fld->child[0]->field_pos = 0; |
6cd62ccf |
630 | fld->child[0]->field_type = fld->field_type->element_type[0]; |
631 | fld->child[0]->offset_root = fld->offset_root; |
632 | fld->child[0]->fixed_root = fld->fixed_root; |
633 | fld->child[0]->offset_parent = 0; |
634 | fld->child[0]->fixed_parent = 1; |
635 | // fld->child[0]->base_address = NULL; |
636 | fld->child[0]->field_size = 0; |
637 | fld->child[0]->field_fixed = -1; |
638 | fld->child[0]->parent = fld; |
639 | fld->child[0]->child = NULL; |
640 | fld->child[0]->current_element = 0; |
3aee1200 |
641 | construct_types_and_fields(fac, tmpTd, fld->child[0]); |
6cd62ccf |
642 | }else if(td->type == LTT_STRUCT){ |
643 | fld->field_type->element_number = td->fields.position; |
908f42fa |
644 | |
645 | if(fld->field_type->element_type == NULL){ |
646 | fld->field_type->element_type = g_new(LttType*, td->fields.position); |
647 | flag = 1; |
648 | }else{ |
649 | flag = 0; |
650 | } |
651 | |
963b5f2d |
652 | fld->child = g_new(LttField*, td->fields.position); |
6cd62ccf |
653 | for(i=0;i<td->fields.position;i++){ |
8d1e6362 |
654 | tmpTd = ((type_fields*)(td->fields.array[i]))->type; |
908f42fa |
655 | |
656 | if(flag) |
c1161b10 |
657 | fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd); |
963b5f2d |
658 | fld->child[i] = g_new(LttField,1); |
6cd62ccf |
659 | |
660 | fld->child[i]->field_pos = i; |
661 | fld->child[i]->field_type = fld->field_type->element_type[i]; |
908f42fa |
662 | |
663 | if(flag){ |
c1161b10 |
664 | fld->child[i]->field_type->element_name |
665 | = g_quark_from_string(((type_fields*)(td->fields.array[i]))->name); |
908f42fa |
666 | } |
667 | |
6cd62ccf |
668 | fld->child[i]->offset_root = -1; |
669 | fld->child[i]->fixed_root = -1; |
670 | fld->child[i]->offset_parent = -1; |
671 | fld->child[i]->fixed_parent = -1; |
672 | // fld->child[i]->base_address = NULL; |
673 | fld->child[i]->field_size = 0; |
674 | fld->child[i]->field_fixed = -1; |
675 | fld->child[i]->parent = fld; |
676 | fld->child[i]->child = NULL; |
677 | fld->child[i]->current_element = 0; |
3aee1200 |
678 | construct_types_and_fields(fac, tmpTd, fld->child[i]); |
6cd62ccf |
679 | } |
680 | } |
681 | } |
3aee1200 |
682 | #endif //0 |
6cd62ccf |
683 | |
f104d082 |
684 | #if 0 |
6cd62ccf |
685 | /***************************************************************************** |
686 | *Function name |
687 | * lookup_named_type: search named type in the table |
688 | * internal function |
689 | *Input params |
690 | * fac : facility struct |
f104d082 |
691 | * name : type name |
6cd62ccf |
692 | *Return value |
963b5f2d |
693 | * : either find the named type, or create a new LttType |
6cd62ccf |
694 | ****************************************************************************/ |
695 | |
f104d082 |
696 | LttType * lookup_named_type(LttFacility *fac, GQuark type_name) |
6cd62ccf |
697 | { |
cb03932a |
698 | LttType *type = NULL; |
6cd62ccf |
699 | |
f104d082 |
700 | /* Named type */ |
701 | type = g_datalist_id_get_data(&fac->named_types, name); |
702 | |
703 | g_assert(type != NULL); |
704 | #if 0 |
3aee1200 |
705 | if(type == NULL){ |
cb03932a |
706 | /* Create the type */ |
3aee1200 |
707 | type = g_new(LttType,1); |
708 | type->type_name = name; |
3aee1200 |
709 | type->type_class = td->type; |
710 | if(td->fmt) type->fmt = g_strdup(td->fmt); |
711 | else type->fmt = NULL; |
712 | type->size = td->size; |
713 | type->enum_strings = NULL; |
714 | type->element_type = NULL; |
715 | type->element_number = 0; |
cb03932a |
716 | |
717 | if(td->type_name != NULL) |
718 | g_datalist_id_set_data_full(&fac->named_types, name, |
719 | type, (GDestroyNotify)freeLttNamedType); |
6cd62ccf |
720 | } |
f104d082 |
721 | #endif //0 |
3aee1200 |
722 | return type; |
6cd62ccf |
723 | } |
f104d082 |
724 | #endif //0 |
6cd62ccf |
725 | |
726 | /***************************************************************************** |
727 | *Function name |
728 | * ltt_facility_close : close a facility, decrease its usage count, |
729 | * if usage count = 0, release the memory |
730 | *Input params |
731 | * f : facility that will be closed |
6cd62ccf |
732 | ****************************************************************************/ |
733 | |
3aee1200 |
734 | void ltt_facility_close(LttFacility *f) |
6cd62ccf |
735 | { |
6cd62ccf |
736 | //release the memory it occupied |
737 | freeFacility(f); |
6cd62ccf |
738 | } |
739 | |
740 | /***************************************************************************** |
741 | * Functions to release the memory occupied by the facility |
742 | ****************************************************************************/ |
743 | |
963b5f2d |
744 | void freeFacility(LttFacility * fac) |
6cd62ccf |
745 | { |
3aee1200 |
746 | guint i; |
747 | LttEventType *et; |
6cd62ccf |
748 | |
3aee1200 |
749 | for(i=0; i<fac->events->len; i++) { |
750 | et = &g_array_index (fac->events, LttEventType, i); |
751 | freeEventtype(et); |
6cd62ccf |
752 | } |
3aee1200 |
753 | g_array_free(fac->events, TRUE); |
6cd62ccf |
754 | |
f104d082 |
755 | g_datalist_clear(&fac->events_by_name); |
6cd62ccf |
756 | |
f104d082 |
757 | // g_datalist_clear(&fac->named_types); |
6cd62ccf |
758 | } |
759 | |
963b5f2d |
760 | void freeEventtype(LttEventType * evType) |
6cd62ccf |
761 | { |
f104d082 |
762 | unsigned int i; |
908f42fa |
763 | LttType * root_type; |
6cd62ccf |
764 | if(evType->description) |
f104d082 |
765 | g_free(evType->description); |
766 | |
767 | for(i=0; i<evType->fields->len;i++) { |
2312de30 |
768 | LttField *field = &g_array_index(evType->fields, LttField, i); |
769 | freeLttField(field); |
1417d990 |
770 | } |
f104d082 |
771 | g_array_free(evType->fields, TRUE); |
772 | g_datalist_clear(&evType->fields_by_name); |
773 | } |
774 | |
775 | void freeLttType(LttType * type) |
776 | { |
777 | unsigned int i; |
778 | |
779 | if(type->fmt) |
780 | g_free(type->fmt); |
781 | |
782 | if(type->enum_map) |
2312de30 |
783 | g_hash_table_destroy(type->enum_map); |
f104d082 |
784 | |
785 | if(type->fields) { |
786 | for(i=0; i<type->fields->len; i++) { |
787 | freeLttField(&g_array_index(type->fields, LttField, i)); |
788 | } |
789 | g_array_free(type->fields, TRUE); |
790 | } |
791 | if(type->fields_by_name) |
792 | g_datalist_clear(&type->fields_by_name); |
6cd62ccf |
793 | } |
794 | |
908f42fa |
795 | void freeLttNamedType(LttType * type) |
796 | { |
f104d082 |
797 | freeLttType(type); |
908f42fa |
798 | } |
799 | |
f104d082 |
800 | void freeLttField(LttField * field) |
1417d990 |
801 | { |
f104d082 |
802 | if(field->description) |
803 | g_free(field->description); |
804 | if(field->dynamic_offsets) |
805 | g_array_free(field->dynamic_offsets, TRUE); |
2312de30 |
806 | freeLttType(&field->field_type); |
6cd62ccf |
807 | } |
808 | |
809 | /***************************************************************************** |
810 | *Function name |
811 | * ltt_facility_name : obtain the facility's name |
812 | *Input params |
3aee1200 |
813 | * f : the facility |
6cd62ccf |
814 | *Return value |
3aee1200 |
815 | * GQuark : the facility's name |
6cd62ccf |
816 | ****************************************************************************/ |
817 | |
3aee1200 |
818 | GQuark ltt_facility_name(LttFacility *f) |
6cd62ccf |
819 | { |
820 | return f->name; |
821 | } |
822 | |
823 | /***************************************************************************** |
824 | *Function name |
825 | * ltt_facility_checksum : obtain the facility's checksum |
826 | *Input params |
3aee1200 |
827 | * f : the facility |
6cd62ccf |
828 | *Return value |
3aee1200 |
829 | * : the checksum of the facility |
6cd62ccf |
830 | ****************************************************************************/ |
831 | |
3aee1200 |
832 | guint32 ltt_facility_checksum(LttFacility *f) |
6cd62ccf |
833 | { |
834 | return f->checksum; |
835 | } |
836 | |
963b5f2d |
837 | /***************************************************************************** |
838 | *Function name |
839 | * ltt_facility_base_id : obtain the facility base id |
840 | *Input params |
841 | * f : the facility |
842 | *Return value |
843 | * : the base id of the facility |
844 | ****************************************************************************/ |
845 | |
3aee1200 |
846 | guint ltt_facility_id(LttFacility *f) |
963b5f2d |
847 | { |
3aee1200 |
848 | return f->id; |
963b5f2d |
849 | } |
850 | |
6cd62ccf |
851 | /***************************************************************************** |
852 | *Function name |
853 | * ltt_facility_eventtype_number: obtain the number of the event types |
854 | *Input params |
855 | * f : the facility that will be closed |
856 | *Return value |
3aee1200 |
857 | * : the number of the event types |
6cd62ccf |
858 | ****************************************************************************/ |
859 | |
3aee1200 |
860 | guint8 ltt_facility_eventtype_number(LttFacility *f) |
6cd62ccf |
861 | { |
3aee1200 |
862 | return (f->events->len); |
6cd62ccf |
863 | } |
864 | |
865 | /***************************************************************************** |
866 | *Function name |
867 | * ltt_facility_eventtype_get: obtain the event type according to event id |
868 | * from 0 to event_number - 1 |
869 | *Input params |
870 | * f : the facility that will be closed |
871 | *Return value |
963b5f2d |
872 | * LttEventType * : the event type required |
6cd62ccf |
873 | ****************************************************************************/ |
874 | |
3aee1200 |
875 | LttEventType *ltt_facility_eventtype_get(LttFacility *f, guint8 i) |
6cd62ccf |
876 | { |
c4afd5d8 |
877 | if(!f->exists) return NULL; |
878 | |
3aee1200 |
879 | g_assert(i < f->events->len); |
880 | return &g_array_index(f->events, LttEventType, i); |
6cd62ccf |
881 | } |
882 | |
883 | /***************************************************************************** |
884 | *Function name |
885 | * ltt_facility_eventtype_get_by_name |
886 | * : obtain the event type according to event name |
887 | * event name is unique in the facility |
888 | *Input params |
cf74a6f1 |
889 | * f : the facility |
6cd62ccf |
890 | * name : the name of the event |
891 | *Return value |
963b5f2d |
892 | * LttEventType * : the event type required |
6cd62ccf |
893 | ****************************************************************************/ |
894 | |
3aee1200 |
895 | LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, GQuark name) |
6cd62ccf |
896 | { |
cb03932a |
897 | LttEventType *et = g_datalist_id_get_data(&f->events_by_name, name); |
a0c1f622 |
898 | return et; |
6cd62ccf |
899 | } |
900 | |