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); |
29e7c5b3 |
141 | if(checksum == f->checksum) { |
142 | generateFacility(f, fac, checksum); |
143 | generated = TRUE; |
144 | } |
963b5f2d |
145 | |
45e14832 |
146 | g_free(fac->name); |
d2ace3a9 |
147 | free(fac->capname); |
45e14832 |
148 | g_free(fac->description); |
963b5f2d |
149 | freeEvents(&fac->events); |
150 | sequence_dispose(&fac->events); |
151 | freeNamedType(&fac->named_types); |
152 | table_dispose(&fac->named_types); |
153 | freeTypes(&fac->unnamed_types); |
154 | sequence_dispose(&fac->unnamed_types); |
cf1307af |
155 | g_free(fac); |
129fd24a |
156 | if(generated) break; /* use the first good match */ |
6cd62ccf |
157 | } |
3aee1200 |
158 | else { |
159 | g_warning("facility token was expected in file %s", in.name); |
a0c1f622 |
160 | ret = 1; |
3aee1200 |
161 | goto parse_error; |
162 | } |
6cd62ccf |
163 | } |
29e7c5b3 |
164 | |
3aee1200 |
165 | parse_error: |
90699b2b |
166 | fclose(in.fp); |
a0c1f622 |
167 | open_error: |
90699b2b |
168 | |
986e2a7c |
169 | if(!generated) { |
170 | g_warning("Cannot find facility %s, checksum 0x%X", |
171 | g_quark_to_string(f->name), f->checksum); |
172 | ret = 1; |
173 | } |
29e7c5b3 |
174 | |
a0c1f622 |
175 | return ret; |
6cd62ccf |
176 | } |
177 | |
178 | |
179 | /***************************************************************************** |
180 | *Function name |
181 | * generateFacility : generate facility, internal function |
182 | *Input params |
963b5f2d |
183 | * facility : LttFacilty structure |
184 | * fac : facility structure |
6cd62ccf |
185 | * checksum : checksum of the facility |
6cd62ccf |
186 | ****************************************************************************/ |
187 | |
3aee1200 |
188 | void generateFacility(LttFacility *f, facility_t *fac, guint32 checksum) |
6cd62ccf |
189 | { |
963b5f2d |
190 | char * facilityName = fac->name; |
90699b2b |
191 | sequence_t * events = &fac->events; |
f104d082 |
192 | unsigned int i, j; |
963b5f2d |
193 | LttType * type; |
f104d082 |
194 | table_t *named_types = &fac->named_types; |
6cd62ccf |
195 | |
3aee1200 |
196 | g_assert(f->name == g_quark_from_string(facilityName)); |
197 | g_assert(f->checksum == checksum); |
198 | |
199 | //f->event_number = events->position; |
6cd62ccf |
200 | |
201 | //initialize inner structures |
3aee1200 |
202 | f->events = g_array_sized_new (FALSE, TRUE, sizeof(LttEventType), |
203 | events->position); |
204 | //f->events = g_new(LttEventType*,f->event_number); |
205 | f->events = g_array_set_size(f->events, events->position); |
206 | |
207 | g_datalist_init(&f->events_by_name); |
2312de30 |
208 | // g_datalist_init(&f->named_types); |
f104d082 |
209 | #if 0 |
210 | /* The first day, he created the named types */ |
211 | |
212 | for(i=0; i<named_types->keys.position; i++) { |
213 | GQuark name = g_quark_from_string((char*)named_types->keys.array[i]); |
214 | type_descriptor_t *td = (type_descriptor_t*)named_types->values.array[i]; |
215 | |
216 | /* Create the type */ |
217 | type = g_new(LttType,1); |
218 | type->type_name = name; |
219 | type->type_class = td->type; |
220 | if(td->fmt) type->fmt = g_strdup(td->fmt); |
221 | else type->fmt = NULL; |
222 | type->size = td->size; |
223 | type->enum_strings = NULL; |
224 | type->element_type = NULL; |
225 | type->element_number = 0; |
226 | |
227 | construct_types_and_fields(type, td, NULL, NULL, ...); |
228 | |
229 | g_datalist_id_set_data_full(&fac->named_types, name, |
230 | type, (GDestroyNotify)freeLttNamedType); |
231 | |
232 | } |
233 | #endif //0 |
234 | /* The second day, he created the event fields and types */ |
235 | //for each event, construct field and type acyclic graph |
6cd62ccf |
236 | for(i=0;i<events->position;i++){ |
c1161b10 |
237 | event_t *parser_event = (event_t*)events->array[i]; |
f104d082 |
238 | LttEventType *event_type = &g_array_index(f->events, LttEventType, i); |
6cd62ccf |
239 | |
3aee1200 |
240 | event_type->name = |
f104d082 |
241 | g_quark_from_string(parser_event->name); |
3aee1200 |
242 | |
243 | g_datalist_id_set_data(&f->events_by_name, event_type->name, |
244 | event_type); |
245 | |
246 | event_type->description = |
f104d082 |
247 | g_strdup(parser_event->description); |
6cd62ccf |
248 | |
3aee1200 |
249 | event_type->index = i; |
f104d082 |
250 | event_type->facility = f; |
6cd62ccf |
251 | |
dd3a6d39 |
252 | event_type->has_compact_data = parser_event->compact_data; |
253 | |
f104d082 |
254 | event_type->fields = g_array_sized_new(FALSE, TRUE, |
255 | sizeof(LttField), parser_event->fields.position); |
256 | event_type->fields = |
257 | g_array_set_size(event_type->fields, parser_event->fields.position); |
258 | g_datalist_init(&event_type->fields_by_name); |
259 | |
260 | for(j=0; j<parser_event->fields.position; j++) { |
261 | LttField *field = &g_array_index(event_type->fields, LttField, j); |
262 | field_t *parser_field = (field_t*)parser_event->fields.array[j]; |
263 | |
2312de30 |
264 | construct_fields(f, field, parser_field); |
f104d082 |
265 | g_datalist_id_set_data(&event_type->fields_by_name, |
266 | field->name, |
267 | field); |
8710c6c7 |
268 | } |
f104d082 |
269 | } |
270 | |
271 | /* What about 2 days weeks ? */ |
6cd62ccf |
272 | } |
273 | |
274 | |
275 | /***************************************************************************** |
276 | *Function name |
3aee1200 |
277 | * construct_types_and_fields : construct field tree and type graph, |
6cd62ccf |
278 | * internal recursion function |
279 | *Input params |
280 | * fac : facility struct |
f104d082 |
281 | * field : destination lttv field |
282 | * fld : source parser field |
6cd62ccf |
283 | ****************************************************************************/ |
284 | |
f104d082 |
285 | //DONE |
286 | //make the change for arrays and sequences |
287 | //no more root field. -> change this for an array of fields. |
288 | // Compute the field size here. |
289 | // Flag fields as "VARIABLE OFFSET" or "FIXED OFFSET" : as soon as |
290 | // a field with a variable size is found, all the following fields must |
291 | // be flagged with "VARIABLE OFFSET", this will be done by the offset |
292 | // precomputation. |
293 | |
3aee1200 |
294 | |
f104d082 |
295 | void construct_fields(LttFacility *fac, |
c1161b10 |
296 | LttField *field, |
f104d082 |
297 | field_t *fld) |
298 | { |
299 | guint len; |
300 | type_descriptor_t *td; |
2312de30 |
301 | LttType *type; |
f104d082 |
302 | |
c1161b10 |
303 | if(fld->name) |
304 | field->name = g_quark_from_string(fld->name); |
305 | else |
306 | fld->name = 0; |
ae3d0f50 |
307 | |
f104d082 |
308 | if(fld->description) { |
309 | len = strlen(fld->description); |
310 | field->description = g_new(gchar, len+1); |
311 | strcpy(field->description, fld->description); |
312 | } |
313 | field->dynamic_offsets = NULL; |
314 | type = &field->field_type; |
315 | td = fld->type; |
316 | |
317 | type->enum_map = NULL; |
318 | type->fields = NULL; |
319 | type->fields_by_name = NULL; |
c1161b10 |
320 | type->network = td->network; |
743e50fd |
321 | |
f104d082 |
322 | switch(td->type) { |
323 | case INT_FIXED: |
324 | type->type_class = LTT_INT_FIXED; |
325 | type->size = td->size; |
326 | break; |
327 | case UINT_FIXED: |
328 | type->type_class = LTT_UINT_FIXED; |
329 | type->size = td->size; |
330 | break; |
331 | case POINTER: |
332 | type->type_class = LTT_POINTER; |
333 | type->size = fac->pointer_size; |
334 | break; |
335 | case CHAR: |
336 | type->type_class = LTT_CHAR; |
337 | type->size = td->size; |
338 | break; |
339 | case UCHAR: |
340 | type->type_class = LTT_UCHAR; |
341 | type->size = td->size; |
743e50fd |
342 | g_assert(type->size != 0); |
f104d082 |
343 | break; |
344 | case SHORT: |
345 | type->type_class = LTT_SHORT; |
346 | type->size = td->size; |
347 | break; |
348 | case USHORT: |
349 | type->type_class = LTT_USHORT; |
350 | type->size = td->size; |
351 | break; |
352 | case INT: |
353 | type->type_class = LTT_INT; |
354 | type->size = fac->int_size; |
355 | break; |
356 | case UINT: |
357 | type->type_class = LTT_UINT; |
358 | type->size = fac->int_size; |
743e50fd |
359 | g_assert(type->size != 0); |
f104d082 |
360 | break; |
361 | case LONG: |
362 | type->type_class = LTT_LONG; |
363 | type->size = fac->long_size; |
364 | break; |
365 | case ULONG: |
366 | type->type_class = LTT_ULONG; |
367 | type->size = fac->long_size; |
368 | break; |
369 | case SIZE_T: |
370 | type->type_class = LTT_SIZE_T; |
371 | type->size = fac->size_t_size; |
372 | break; |
373 | case SSIZE_T: |
374 | type->type_class = LTT_SSIZE_T; |
375 | type->size = fac->size_t_size; |
376 | break; |
377 | case OFF_T: |
378 | type->type_class = LTT_OFF_T; |
379 | type->size = fac->size_t_size; |
380 | break; |
381 | case FLOAT: |
382 | type->type_class = LTT_FLOAT; |
383 | type->size = td->size; |
384 | break; |
385 | case STRING: |
386 | type->type_class = LTT_STRING; |
387 | type->size = 0; |
388 | break; |
389 | case ENUM: |
390 | type->type_class = LTT_ENUM; |
391 | type->size = fac->int_size; |
392 | { |
393 | guint i; |
743e50fd |
394 | type->enum_map = g_hash_table_new(g_direct_hash, g_direct_equal); |
f104d082 |
395 | for(i=0; i<td->labels.position; i++) { |
2312de30 |
396 | GQuark value = g_quark_from_string((char*)td->labels.array[i]); |
397 | gint key = *(int*)td->labels_values.array[i]; |
398 | g_hash_table_insert(type->enum_map, (gpointer)key, (gpointer)value); |
f104d082 |
399 | } |
400 | } |
743e50fd |
401 | g_assert(type->size != 0); |
f104d082 |
402 | break; |
403 | case ARRAY: |
404 | type->type_class = LTT_ARRAY; |
405 | type->size = td->size; |
406 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
407 | td->fields.position); |
408 | type->fields = g_array_set_size(type->fields, td->fields.position); |
409 | { |
410 | guint i; |
411 | |
412 | for(i=0; i<td->fields.position; i++) { |
413 | field_t *schild = (field_t*)td->fields.array[i]; |
414 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
415 | |
416 | construct_fields(fac, dchild, schild); |
417 | } |
418 | } |
419 | break; |
420 | case SEQUENCE: |
421 | type->type_class = LTT_SEQUENCE; |
422 | type->size = 0; |
423 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
424 | td->fields.position); |
425 | type->fields = g_array_set_size(type->fields, td->fields.position); |
426 | { |
427 | guint i; |
428 | |
429 | for(i=0; i<td->fields.position; i++) { |
430 | field_t *schild = (field_t*)td->fields.array[i]; |
431 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
432 | |
433 | construct_fields(fac, dchild, schild); |
434 | } |
435 | } |
436 | break; |
437 | case STRUCT: |
438 | type->type_class = LTT_STRUCT; |
439 | type->size = 0; // Size not calculated by the parser. |
440 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
441 | td->fields.position); |
442 | type->fields = g_array_set_size(type->fields, td->fields.position); |
443 | g_datalist_init(&type->fields_by_name); |
444 | { |
445 | guint i; |
446 | |
447 | for(i=0; i<td->fields.position; i++) { |
448 | field_t *schild = (field_t*)td->fields.array[i]; |
449 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
450 | |
451 | construct_fields(fac, dchild, schild); |
452 | g_datalist_id_set_data(&type->fields_by_name, |
453 | dchild->name, |
454 | dchild); |
455 | } |
456 | } |
457 | break; |
458 | case UNION: |
459 | type->type_class = LTT_UNION; |
460 | type->size = 0; // Size not calculated by the parser. |
461 | type->fields = g_array_sized_new(FALSE, TRUE, sizeof(LttField), |
462 | td->fields.position); |
463 | type->fields = g_array_set_size(type->fields, td->fields.position); |
464 | g_datalist_init(&type->fields_by_name); |
465 | { |
466 | guint i; |
467 | |
468 | for(i=0; i<td->fields.position; i++) { |
469 | field_t *schild = (field_t*)td->fields.array[i]; |
470 | LttField *dchild = &g_array_index(type->fields, LttField, i); |
471 | |
472 | construct_fields(fac, dchild, schild); |
473 | g_datalist_id_set_data(&type->fields_by_name, |
474 | dchild->name, |
475 | dchild); |
476 | } |
477 | } |
478 | break; |
479 | case NONE: |
480 | default: |
481 | g_error("construct_fields : unknown type"); |
482 | } |
483 | |
484 | field->field_size = type->size; |
485 | |
486 | /* Put the fields as "variable" offset to root first. Then, |
487 | * the offset precomputation will only have to set the FIELD_FIXED until |
488 | * it reaches the first variable length field, then stop. |
489 | */ |
490 | field->fixed_root = FIELD_VARIABLE; |
491 | |
492 | if(td->fmt) { |
493 | len = strlen(td->fmt); |
494 | type->fmt = g_new(gchar, len+1); |
495 | strcpy(type->fmt, td->fmt); |
496 | } |
497 | } |
498 | |
499 | |
500 | |
501 | #if 0 |
90699b2b |
502 | void construct_types_and_fields(LttFacility * fac, type_descriptor_t * td, |
3aee1200 |
503 | LttField * fld) |
504 | { |
a0c1f622 |
505 | int i; |
90699b2b |
506 | type_descriptor_t * tmpTd; |
3aee1200 |
507 | |
508 | switch(td->type) { |
cb03932a |
509 | case INT: |
510 | case UINT: |
511 | case FLOAT: |
512 | fld->field_type->size = td->size; |
513 | break; |
514 | case POINTER: |
515 | case LONG: |
516 | case ULONG: |
517 | case SIZE_T: |
518 | case SSIZE_T: |
519 | case OFF_T: |
520 | fld->field_type->size = 0; |
521 | break; |
522 | case STRING: |
523 | fld->field_type->size = 0; |
524 | break; |
525 | case ENUM: |
526 | fld->field_type->element_number = td->labels.position; |
527 | fld->field_type->enum_strings = g_new(GQuark,td->labels.position); |
528 | for(i=0;i<td->labels.position;i++){ |
529 | fld->field_type->enum_strings[i] |
530 | = g_quark_from_string(((char*)(td->labels.array[i]))); |
531 | } |
532 | fld->field_type->size = td->size; |
533 | break; |
534 | |
535 | case ARRAY: |
536 | fld->field_type->element_number = (unsigned)td->size; |
537 | case SEQUENCE: |
3aee1200 |
538 | fld->field_type->element_type = g_new(LttType*,1); |
539 | tmpTd = td->nested_type; |
540 | fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd); |
541 | fld->child = g_new(LttField*, 1); |
542 | fld->child[0] = g_new(LttField, 1); |
543 | |
544 | fld->child[0]->field_type = fld->field_type->element_type[0]; |
545 | fld->child[0]->offset_root = 0; |
546 | fld->child[0]->fixed_root = FIELD_UNKNOWN; |
547 | fld->child[0]->offset_parent = 0; |
548 | fld->child[0]->fixed_parent = FIELD_UNKNOWN; |
549 | fld->child[0]->field_size = 0; |
550 | fld->child[0]->fixed_size = FIELD_UNKNOWN; |
551 | fld->child[0]->parent = fld; |
552 | fld->child[0]->child = NULL; |
553 | fld->child[0]->current_element = 0; |
554 | construct_types_and_fields(fac, tmpTd, fld->child[0]); |
555 | break; |
cb03932a |
556 | |
557 | case STRUCT: |
558 | case UNION: |
3aee1200 |
559 | fld->field_type->element_number = td->fields.position; |
560 | |
561 | g_assert(fld->field_type->element_type == NULL); |
562 | fld->field_type->element_type = g_new(LttType*, td->fields.position); |
563 | |
564 | fld->child = g_new(LttField*, td->fields.position); |
565 | for(i=0;i<td->fields.position;i++){ |
90699b2b |
566 | tmpTd = ((field_t*)(td->fields.array[i]))->type; |
3aee1200 |
567 | |
c1161b10 |
568 | fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd); |
3aee1200 |
569 | fld->child[i] = g_new(LttField,1); |
570 | |
571 | // fld->child[i]->field_pos = i; |
572 | fld->child[i]->field_type = fld->field_type->element_type[i]; |
573 | |
574 | fld->child[i]->field_type->element_name |
c1161b10 |
575 | = g_quark_from_string(((field_t*)(td->fields.array[i]))->name); |
3aee1200 |
576 | |
577 | fld->child[i]->offset_root = 0; |
578 | fld->child[i]->fixed_root = FIELD_UNKNOWN; |
579 | fld->child[i]->offset_parent = 0; |
580 | fld->child[i]->fixed_parent = FIELD_UNKNOWN; |
581 | fld->child[i]->field_size = 0; |
582 | fld->child[i]->fixed_size = FIELD_UNKNOWN; |
583 | fld->child[i]->parent = fld; |
584 | fld->child[i]->child = NULL; |
585 | fld->child[i]->current_element = 0; |
586 | construct_types_and_fields(fac, tmpTd, fld->child[i]); |
587 | } |
3aee1200 |
588 | break; |
cb03932a |
589 | |
3aee1200 |
590 | default: |
591 | g_error("construct_types_and_fields : unknown type"); |
592 | } |
593 | |
594 | |
595 | } |
596 | |
f104d082 |
597 | #endif //0 |
3aee1200 |
598 | |
599 | #if 0 |
600 | void construct_types_and_fields(LttFacility * fac, type_descriptor * td, |
963b5f2d |
601 | LttField * fld) |
6cd62ccf |
602 | { |
908f42fa |
603 | int i, flag; |
6cd62ccf |
604 | type_descriptor * tmpTd; |
605 | |
606 | // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE) |
607 | // fld->field_size = 0; |
608 | // else fld->field_size = -1; |
609 | |
610 | if(td->type == LTT_ENUM){ |
611 | fld->field_type->element_number = td->labels.position; |
3aee1200 |
612 | fld->field_type->enum_strings = g_new(GQuark,td->labels.position); |
6cd62ccf |
613 | for(i=0;i<td->labels.position;i++){ |
614 | fld->field_type->enum_strings[i] |
3aee1200 |
615 | = g_quark_from_string(((char*)(td->labels.array[i]))); |
6cd62ccf |
616 | } |
617 | }else if(td->type == LTT_ARRAY || td->type == LTT_SEQUENCE){ |
618 | if(td->type == LTT_ARRAY) |
619 | fld->field_type->element_number = (unsigned)td->size; |
963b5f2d |
620 | fld->field_type->element_type = g_new(LttType*,1); |
6cd62ccf |
621 | tmpTd = td->nested_type; |
622 | fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd); |
963b5f2d |
623 | fld->child = g_new(LttField*, 1); |
624 | fld->child[0] = g_new(LttField, 1); |
6cd62ccf |
625 | |
3aee1200 |
626 | // fld->child[0]->field_pos = 0; |
6cd62ccf |
627 | fld->child[0]->field_type = fld->field_type->element_type[0]; |
628 | fld->child[0]->offset_root = fld->offset_root; |
629 | fld->child[0]->fixed_root = fld->fixed_root; |
630 | fld->child[0]->offset_parent = 0; |
631 | fld->child[0]->fixed_parent = 1; |
632 | // fld->child[0]->base_address = NULL; |
633 | fld->child[0]->field_size = 0; |
634 | fld->child[0]->field_fixed = -1; |
635 | fld->child[0]->parent = fld; |
636 | fld->child[0]->child = NULL; |
637 | fld->child[0]->current_element = 0; |
3aee1200 |
638 | construct_types_and_fields(fac, tmpTd, fld->child[0]); |
6cd62ccf |
639 | }else if(td->type == LTT_STRUCT){ |
640 | fld->field_type->element_number = td->fields.position; |
908f42fa |
641 | |
642 | if(fld->field_type->element_type == NULL){ |
643 | fld->field_type->element_type = g_new(LttType*, td->fields.position); |
644 | flag = 1; |
645 | }else{ |
646 | flag = 0; |
647 | } |
648 | |
963b5f2d |
649 | fld->child = g_new(LttField*, td->fields.position); |
6cd62ccf |
650 | for(i=0;i<td->fields.position;i++){ |
8d1e6362 |
651 | tmpTd = ((type_fields*)(td->fields.array[i]))->type; |
908f42fa |
652 | |
653 | if(flag) |
c1161b10 |
654 | fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd); |
963b5f2d |
655 | fld->child[i] = g_new(LttField,1); |
6cd62ccf |
656 | |
657 | fld->child[i]->field_pos = i; |
658 | fld->child[i]->field_type = fld->field_type->element_type[i]; |
908f42fa |
659 | |
660 | if(flag){ |
c1161b10 |
661 | fld->child[i]->field_type->element_name |
662 | = g_quark_from_string(((type_fields*)(td->fields.array[i]))->name); |
908f42fa |
663 | } |
664 | |
6cd62ccf |
665 | fld->child[i]->offset_root = -1; |
666 | fld->child[i]->fixed_root = -1; |
667 | fld->child[i]->offset_parent = -1; |
668 | fld->child[i]->fixed_parent = -1; |
669 | // fld->child[i]->base_address = NULL; |
670 | fld->child[i]->field_size = 0; |
671 | fld->child[i]->field_fixed = -1; |
672 | fld->child[i]->parent = fld; |
673 | fld->child[i]->child = NULL; |
674 | fld->child[i]->current_element = 0; |
3aee1200 |
675 | construct_types_and_fields(fac, tmpTd, fld->child[i]); |
6cd62ccf |
676 | } |
677 | } |
678 | } |
3aee1200 |
679 | #endif //0 |
6cd62ccf |
680 | |
f104d082 |
681 | #if 0 |
6cd62ccf |
682 | /***************************************************************************** |
683 | *Function name |
684 | * lookup_named_type: search named type in the table |
685 | * internal function |
686 | *Input params |
687 | * fac : facility struct |
f104d082 |
688 | * name : type name |
6cd62ccf |
689 | *Return value |
963b5f2d |
690 | * : either find the named type, or create a new LttType |
6cd62ccf |
691 | ****************************************************************************/ |
692 | |
f104d082 |
693 | LttType * lookup_named_type(LttFacility *fac, GQuark type_name) |
6cd62ccf |
694 | { |
cb03932a |
695 | LttType *type = NULL; |
6cd62ccf |
696 | |
f104d082 |
697 | /* Named type */ |
698 | type = g_datalist_id_get_data(&fac->named_types, name); |
699 | |
700 | g_assert(type != NULL); |
701 | #if 0 |
3aee1200 |
702 | if(type == NULL){ |
cb03932a |
703 | /* Create the type */ |
3aee1200 |
704 | type = g_new(LttType,1); |
705 | type->type_name = name; |
3aee1200 |
706 | type->type_class = td->type; |
707 | if(td->fmt) type->fmt = g_strdup(td->fmt); |
708 | else type->fmt = NULL; |
709 | type->size = td->size; |
710 | type->enum_strings = NULL; |
711 | type->element_type = NULL; |
712 | type->element_number = 0; |
cb03932a |
713 | |
714 | if(td->type_name != NULL) |
715 | g_datalist_id_set_data_full(&fac->named_types, name, |
716 | type, (GDestroyNotify)freeLttNamedType); |
6cd62ccf |
717 | } |
f104d082 |
718 | #endif //0 |
3aee1200 |
719 | return type; |
6cd62ccf |
720 | } |
f104d082 |
721 | #endif //0 |
6cd62ccf |
722 | |
723 | /***************************************************************************** |
724 | *Function name |
725 | * ltt_facility_close : close a facility, decrease its usage count, |
726 | * if usage count = 0, release the memory |
727 | *Input params |
728 | * f : facility that will be closed |
6cd62ccf |
729 | ****************************************************************************/ |
730 | |
3aee1200 |
731 | void ltt_facility_close(LttFacility *f) |
6cd62ccf |
732 | { |
6cd62ccf |
733 | //release the memory it occupied |
734 | freeFacility(f); |
6cd62ccf |
735 | } |
736 | |
737 | /***************************************************************************** |
738 | * Functions to release the memory occupied by the facility |
739 | ****************************************************************************/ |
740 | |
963b5f2d |
741 | void freeFacility(LttFacility * fac) |
6cd62ccf |
742 | { |
3aee1200 |
743 | guint i; |
744 | LttEventType *et; |
6cd62ccf |
745 | |
3aee1200 |
746 | for(i=0; i<fac->events->len; i++) { |
747 | et = &g_array_index (fac->events, LttEventType, i); |
748 | freeEventtype(et); |
6cd62ccf |
749 | } |
3aee1200 |
750 | g_array_free(fac->events, TRUE); |
6cd62ccf |
751 | |
f104d082 |
752 | g_datalist_clear(&fac->events_by_name); |
6cd62ccf |
753 | |
f104d082 |
754 | // g_datalist_clear(&fac->named_types); |
6cd62ccf |
755 | } |
756 | |
963b5f2d |
757 | void freeEventtype(LttEventType * evType) |
6cd62ccf |
758 | { |
f104d082 |
759 | unsigned int i; |
908f42fa |
760 | LttType * root_type; |
6cd62ccf |
761 | if(evType->description) |
f104d082 |
762 | g_free(evType->description); |
763 | |
764 | for(i=0; i<evType->fields->len;i++) { |
2312de30 |
765 | LttField *field = &g_array_index(evType->fields, LttField, i); |
766 | freeLttField(field); |
1417d990 |
767 | } |
f104d082 |
768 | g_array_free(evType->fields, TRUE); |
769 | g_datalist_clear(&evType->fields_by_name); |
770 | } |
771 | |
772 | void freeLttType(LttType * type) |
773 | { |
774 | unsigned int i; |
775 | |
776 | if(type->fmt) |
777 | g_free(type->fmt); |
778 | |
779 | if(type->enum_map) |
2312de30 |
780 | g_hash_table_destroy(type->enum_map); |
f104d082 |
781 | |
782 | if(type->fields) { |
783 | for(i=0; i<type->fields->len; i++) { |
784 | freeLttField(&g_array_index(type->fields, LttField, i)); |
785 | } |
786 | g_array_free(type->fields, TRUE); |
787 | } |
788 | if(type->fields_by_name) |
789 | g_datalist_clear(&type->fields_by_name); |
6cd62ccf |
790 | } |
791 | |
908f42fa |
792 | void freeLttNamedType(LttType * type) |
793 | { |
f104d082 |
794 | freeLttType(type); |
908f42fa |
795 | } |
796 | |
f104d082 |
797 | void freeLttField(LttField * field) |
1417d990 |
798 | { |
f104d082 |
799 | if(field->description) |
800 | g_free(field->description); |
801 | if(field->dynamic_offsets) |
802 | g_array_free(field->dynamic_offsets, TRUE); |
2312de30 |
803 | freeLttType(&field->field_type); |
6cd62ccf |
804 | } |
805 | |
806 | /***************************************************************************** |
807 | *Function name |
808 | * ltt_facility_name : obtain the facility's name |
809 | *Input params |
3aee1200 |
810 | * f : the facility |
6cd62ccf |
811 | *Return value |
3aee1200 |
812 | * GQuark : the facility's name |
6cd62ccf |
813 | ****************************************************************************/ |
814 | |
3aee1200 |
815 | GQuark ltt_facility_name(LttFacility *f) |
6cd62ccf |
816 | { |
817 | return f->name; |
818 | } |
819 | |
820 | /***************************************************************************** |
821 | *Function name |
822 | * ltt_facility_checksum : obtain the facility's checksum |
823 | *Input params |
3aee1200 |
824 | * f : the facility |
6cd62ccf |
825 | *Return value |
3aee1200 |
826 | * : the checksum of the facility |
6cd62ccf |
827 | ****************************************************************************/ |
828 | |
3aee1200 |
829 | guint32 ltt_facility_checksum(LttFacility *f) |
6cd62ccf |
830 | { |
831 | return f->checksum; |
832 | } |
833 | |
963b5f2d |
834 | /***************************************************************************** |
835 | *Function name |
836 | * ltt_facility_base_id : obtain the facility base id |
837 | *Input params |
838 | * f : the facility |
839 | *Return value |
840 | * : the base id of the facility |
841 | ****************************************************************************/ |
842 | |
3aee1200 |
843 | guint ltt_facility_id(LttFacility *f) |
963b5f2d |
844 | { |
3aee1200 |
845 | return f->id; |
963b5f2d |
846 | } |
847 | |
6cd62ccf |
848 | /***************************************************************************** |
849 | *Function name |
850 | * ltt_facility_eventtype_number: obtain the number of the event types |
851 | *Input params |
852 | * f : the facility that will be closed |
853 | *Return value |
3aee1200 |
854 | * : the number of the event types |
6cd62ccf |
855 | ****************************************************************************/ |
856 | |
3aee1200 |
857 | guint8 ltt_facility_eventtype_number(LttFacility *f) |
6cd62ccf |
858 | { |
3aee1200 |
859 | return (f->events->len); |
6cd62ccf |
860 | } |
861 | |
862 | /***************************************************************************** |
863 | *Function name |
864 | * ltt_facility_eventtype_get: obtain the event type according to event id |
865 | * from 0 to event_number - 1 |
866 | *Input params |
867 | * f : the facility that will be closed |
868 | *Return value |
963b5f2d |
869 | * LttEventType * : the event type required |
6cd62ccf |
870 | ****************************************************************************/ |
871 | |
3aee1200 |
872 | LttEventType *ltt_facility_eventtype_get(LttFacility *f, guint8 i) |
6cd62ccf |
873 | { |
c4afd5d8 |
874 | if(!f->exists) return NULL; |
875 | |
3aee1200 |
876 | g_assert(i < f->events->len); |
877 | return &g_array_index(f->events, LttEventType, i); |
6cd62ccf |
878 | } |
879 | |
880 | /***************************************************************************** |
881 | *Function name |
882 | * ltt_facility_eventtype_get_by_name |
883 | * : obtain the event type according to event name |
884 | * event name is unique in the facility |
885 | *Input params |
cf74a6f1 |
886 | * f : the facility |
6cd62ccf |
887 | * name : the name of the event |
888 | *Return value |
963b5f2d |
889 | * LttEventType * : the event type required |
6cd62ccf |
890 | ****************************************************************************/ |
891 | |
3aee1200 |
892 | LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, GQuark name) |
6cd62ccf |
893 | { |
cb03932a |
894 | LttEventType *et = g_datalist_id_get_data(&f->events_by_name, name); |
a0c1f622 |
895 | return et; |
6cd62ccf |
896 | } |
897 | |