valgrind fix
[lttv.git] / ltt / branches / poly / ltt / facility.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 * 2005 Mathieu Desnoyers
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License Version 2 as
7 * published by the Free Software Foundation;
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
17 * MA 02111-1307, USA.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <glib.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
30 #include <fcntl.h>
31
32
33
34 #include "parser.h"
35 #include <ltt/ltt.h>
36 #include "ltt-private.h"
37 #include <ltt/facility.h>
38
39 #ifndef g_open
40 #define g_open open
41 #endif
42
43 #define g_close close
44
45 /* search for the (named) type in the table, if it does not exist
46 create a new one */
47 LttType * lookup_named_type(LttFacility *fac, type_descriptor_t * td);
48
49 /* construct directed acyclic graph for types, and tree for fields */
50 void construct_types_and_fields(LttFacility * fac, type_descriptor_t * td,
51 LttField * fld);
52
53 /* generate the facility according to the events belongin to it */
54 void generateFacility(LttFacility * f, facility_t * fac,
55 guint32 checksum);
56
57 /* functions to release the memory occupied by a facility */
58 void freeFacility(LttFacility * facility);
59 void freeEventtype(LttEventType * evType);
60 void freeLttType(LttType ** type);
61 void freeLttField(LttField * fld);
62 void freeLttNamedType(LttType * type);
63
64
65 /*****************************************************************************
66 *Function name
67 * ltt_facility_open : open facilities
68 *Input params
69 * t : the trace containing the facilities
70 * pathname : the path name of the facility
71 *
72 *returns 0 on success, 1 on error.
73 ****************************************************************************/
74
75 int ltt_facility_open(LttFacility *f, LttTrace * t, gchar * pathname)
76 {
77 int ret = 0;
78 gchar *token;
79 parse_file_t in;
80 facility_t * fac;
81 unsigned long checksum;
82 //GError * error = NULL;
83 gchar buffer[BUFFER_SIZE];
84
85 in.buffer = &(buffer[0]);
86 in.lineno = 0;
87 in.error = error_callback;
88 in.name = pathname;
89 in.unget = 0;
90
91 //in.fd = g_open(in.name, O_RDONLY, 0);
92 //if(in.fd < 0 ) {
93 in.fp = fopen(in.name, "r");
94 if(in.fp == NULL) {
95 g_warning("cannot open facility description file %s",
96 in.name);
97 ret = 1;
98 goto open_error;
99 }
100
101 //in.channel = g_io_channel_unix_new(in.fd);
102 //in.pos = 0;
103
104 while(1){
105 token = getToken(&in);
106 if(in.type == ENDFILE) break;
107
108 if(g_ascii_strcasecmp(token, "<")) in.error(&in,"not a facility file");
109 token = getName(&in);
110
111 if(g_ascii_strcasecmp("facility",token) == 0) {
112 fac = g_new(facility_t, 1);
113 fac->name = NULL;
114 fac->description = NULL;
115 sequence_init(&(fac->events));
116 table_init(&(fac->named_types));
117 sequence_init(&(fac->unnamed_types));
118
119 parseFacility(&in, fac);
120
121 //check if any namedType is not defined
122 checkNamedTypesImplemented(&fac->named_types);
123
124 generateChecksum(fac->name, &checksum, &fac->events);
125
126 generateFacility(f, fac, checksum);
127
128 g_free(fac->name);
129 g_free(fac->description);
130 freeEvents(&fac->events);
131 sequence_dispose(&fac->events);
132 freeNamedType(&fac->named_types);
133 table_dispose(&fac->named_types);
134 freeTypes(&fac->unnamed_types);
135 sequence_dispose(&fac->unnamed_types);
136 g_free(fac);
137 }
138 else {
139 g_warning("facility token was expected in file %s", in.name);
140 ret = 1;
141 goto parse_error;
142 }
143 }
144
145 parse_error:
146 //g_io_channel_shutdown(in.channel, FALSE, &error); /* No flush */
147 //if(error != NULL) {
148 fclose(in.fp);
149 open_error:
150 // g_warning("Can not close file: \n%s\n", error->message);
151 // g_error_free(error);
152 //}
153
154 //g_close(in.fd);
155 return ret;
156 }
157
158
159 /*****************************************************************************
160 *Function name
161 * generateFacility : generate facility, internal function
162 *Input params
163 * facility : LttFacilty structure
164 * fac : facility structure
165 * checksum : checksum of the facility
166 ****************************************************************************/
167
168 void generateFacility(LttFacility *f, facility_t *fac, guint32 checksum)
169 {
170 char * facilityName = fac->name;
171 sequence_t * events = &fac->events;
172 int i;
173 //LttEventType * evType;
174 LttEventType * event_type;
175 LttField * field;
176 LttType * type;
177
178 g_assert(f->name == g_quark_from_string(facilityName));
179 g_assert(f->checksum == checksum);
180
181 //f->event_number = events->position;
182
183 //initialize inner structures
184 f->events = g_array_sized_new (FALSE, TRUE, sizeof(LttEventType),
185 events->position);
186 //f->events = g_new(LttEventType*,f->event_number);
187 f->events = g_array_set_size(f->events, events->position);
188
189 g_datalist_init(&f->events_by_name);
190 g_datalist_init(&f->named_types);
191
192 //f->named_types_number = fac->named_types.keys.position;
193 //f->named_types = g_array_sized_new (FALSE, TRUE, sizeof(LttType),
194 // fac->named_types.keys.position);
195 //f->named_types = g_new(LttType*, fac->named_types.keys.position);
196 //f->named_types = g_array_set_size(f->named_types,
197 // fac->named_types.keys.position);
198
199 //for each event, construct field tree and type graph
200 for(i=0;i<events->position;i++){
201 event_type = &g_array_index(f->events, LttEventType, i);
202 //evType = g_new(LttEventType,1);
203 //f->events[i] = evType;
204
205 event_type->name =
206 g_quark_from_string(((event_t*)(events->array[i]))->name);
207
208 g_datalist_id_set_data(&f->events_by_name, event_type->name,
209 event_type);
210
211 event_type->description =
212 g_strdup(((event_t*)(events->array[i]))->description);
213
214 field = g_new(LttField, 1);
215 event_type->root_field = field;
216 event_type->facility = f;
217 event_type->index = i;
218
219 if(((event_t*)(events->array[i]))->type != NULL){
220 // field->field_pos = 0;
221 type = lookup_named_type(f,((event_t*)(events->array[i]))->type);
222 field->field_type = type;
223 field->offset_root = 0;
224 field->fixed_root = FIELD_UNKNOWN;
225 field->offset_parent = 0;
226 field->fixed_parent = FIELD_UNKNOWN;
227 // field->base_address = NULL;
228 field->field_size = 0;
229 field->fixed_size = FIELD_UNKNOWN;
230 field->parent = NULL;
231 field->child = NULL;
232 field->current_element = 0;
233
234 //construct field tree and type graph
235 construct_types_and_fields(f,((event_t*)(events->array[i]))->type,field);
236 }else{
237 event_type->root_field = NULL;
238 g_free(field);
239 }
240 }
241 }
242
243
244 /*****************************************************************************
245 *Function name
246 * construct_types_and_fields : construct field tree and type graph,
247 * internal recursion function
248 *Input params
249 * fac : facility struct
250 * td : type descriptor
251 * root_field : root field of the event
252 ****************************************************************************/
253
254
255 void construct_types_and_fields(LttFacility * fac, type_descriptor_t * td,
256 LttField * fld)
257 {
258 int i;
259 type_descriptor_t * tmpTd;
260
261 switch(td->type) {
262 case INT:
263 case UINT:
264 case FLOAT:
265 fld->field_type->size = td->size;
266 break;
267 case POINTER:
268 case LONG:
269 case ULONG:
270 case SIZE_T:
271 case SSIZE_T:
272 case OFF_T:
273 fld->field_type->size = 0;
274 break;
275 case STRING:
276 fld->field_type->size = 0;
277 break;
278 case ENUM:
279 fld->field_type->element_number = td->labels.position;
280 fld->field_type->enum_strings = g_new(GQuark,td->labels.position);
281 for(i=0;i<td->labels.position;i++){
282 fld->field_type->enum_strings[i]
283 = g_quark_from_string(((char*)(td->labels.array[i])));
284 }
285 fld->field_type->size = td->size;
286 break;
287
288 case ARRAY:
289 fld->field_type->element_number = (unsigned)td->size;
290 case SEQUENCE:
291 fld->field_type->element_type = g_new(LttType*,1);
292 tmpTd = td->nested_type;
293 fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd);
294 fld->child = g_new(LttField*, 1);
295 fld->child[0] = g_new(LttField, 1);
296
297 fld->child[0]->field_type = fld->field_type->element_type[0];
298 fld->child[0]->offset_root = 0;
299 fld->child[0]->fixed_root = FIELD_UNKNOWN;
300 fld->child[0]->offset_parent = 0;
301 fld->child[0]->fixed_parent = FIELD_UNKNOWN;
302 fld->child[0]->field_size = 0;
303 fld->child[0]->fixed_size = FIELD_UNKNOWN;
304 fld->child[0]->parent = fld;
305 fld->child[0]->child = NULL;
306 fld->child[0]->current_element = 0;
307 construct_types_and_fields(fac, tmpTd, fld->child[0]);
308 break;
309
310 case STRUCT:
311 case UNION:
312 fld->field_type->element_number = td->fields.position;
313
314 g_assert(fld->field_type->element_type == NULL);
315 fld->field_type->element_type = g_new(LttType*, td->fields.position);
316
317 fld->child = g_new(LttField*, td->fields.position);
318 for(i=0;i<td->fields.position;i++){
319 tmpTd = ((field_t*)(td->fields.array[i]))->type;
320
321 fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd);
322 fld->child[i] = g_new(LttField,1);
323
324 // fld->child[i]->field_pos = i;
325 fld->child[i]->field_type = fld->field_type->element_type[i];
326
327 fld->child[i]->field_type->element_name
328 = g_quark_from_string(((field_t*)(td->fields.array[i]))->name);
329
330 fld->child[i]->offset_root = 0;
331 fld->child[i]->fixed_root = FIELD_UNKNOWN;
332 fld->child[i]->offset_parent = 0;
333 fld->child[i]->fixed_parent = FIELD_UNKNOWN;
334 fld->child[i]->field_size = 0;
335 fld->child[i]->fixed_size = FIELD_UNKNOWN;
336 fld->child[i]->parent = fld;
337 fld->child[i]->child = NULL;
338 fld->child[i]->current_element = 0;
339 construct_types_and_fields(fac, tmpTd, fld->child[i]);
340 }
341 break;
342
343 default:
344 g_error("construct_types_and_fields : unknown type");
345 }
346
347
348 }
349
350
351
352 #if 0
353 void construct_types_and_fields(LttFacility * fac, type_descriptor * td,
354 LttField * fld)
355 {
356 int i, flag;
357 type_descriptor * tmpTd;
358
359 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
360 // fld->field_size = 0;
361 // else fld->field_size = -1;
362
363 if(td->type == LTT_ENUM){
364 fld->field_type->element_number = td->labels.position;
365 fld->field_type->enum_strings = g_new(GQuark,td->labels.position);
366 for(i=0;i<td->labels.position;i++){
367 fld->field_type->enum_strings[i]
368 = g_quark_from_string(((char*)(td->labels.array[i])));
369 }
370 }else if(td->type == LTT_ARRAY || td->type == LTT_SEQUENCE){
371 if(td->type == LTT_ARRAY)
372 fld->field_type->element_number = (unsigned)td->size;
373 fld->field_type->element_type = g_new(LttType*,1);
374 tmpTd = td->nested_type;
375 fld->field_type->element_type[0] = lookup_named_type(fac, tmpTd);
376 fld->child = g_new(LttField*, 1);
377 fld->child[0] = g_new(LttField, 1);
378
379 // fld->child[0]->field_pos = 0;
380 fld->child[0]->field_type = fld->field_type->element_type[0];
381 fld->child[0]->offset_root = fld->offset_root;
382 fld->child[0]->fixed_root = fld->fixed_root;
383 fld->child[0]->offset_parent = 0;
384 fld->child[0]->fixed_parent = 1;
385 // fld->child[0]->base_address = NULL;
386 fld->child[0]->field_size = 0;
387 fld->child[0]->field_fixed = -1;
388 fld->child[0]->parent = fld;
389 fld->child[0]->child = NULL;
390 fld->child[0]->current_element = 0;
391 construct_types_and_fields(fac, tmpTd, fld->child[0]);
392 }else if(td->type == LTT_STRUCT){
393 fld->field_type->element_number = td->fields.position;
394
395 if(fld->field_type->element_type == NULL){
396 fld->field_type->element_type = g_new(LttType*, td->fields.position);
397 flag = 1;
398 }else{
399 flag = 0;
400 }
401
402 fld->child = g_new(LttField*, td->fields.position);
403 for(i=0;i<td->fields.position;i++){
404 tmpTd = ((type_fields*)(td->fields.array[i]))->type;
405
406 if(flag)
407 fld->field_type->element_type[i] = lookup_named_type(fac, tmpTd);
408 fld->child[i] = g_new(LttField,1);
409
410 fld->child[i]->field_pos = i;
411 fld->child[i]->field_type = fld->field_type->element_type[i];
412
413 if(flag){
414 fld->child[i]->field_type->element_name
415 = g_quark_from_string(((type_fields*)(td->fields.array[i]))->name);
416 }
417
418 fld->child[i]->offset_root = -1;
419 fld->child[i]->fixed_root = -1;
420 fld->child[i]->offset_parent = -1;
421 fld->child[i]->fixed_parent = -1;
422 // fld->child[i]->base_address = NULL;
423 fld->child[i]->field_size = 0;
424 fld->child[i]->field_fixed = -1;
425 fld->child[i]->parent = fld;
426 fld->child[i]->child = NULL;
427 fld->child[i]->current_element = 0;
428 construct_types_and_fields(fac, tmpTd, fld->child[i]);
429 }
430 }
431 }
432 #endif //0
433
434 /*****************************************************************************
435 *Function name
436 * lookup_named_type: search named type in the table
437 * internal function
438 *Input params
439 * fac : facility struct
440 * td : type descriptor
441 *Return value
442 * : either find the named type, or create a new LttType
443 ****************************************************************************/
444
445 LttType * lookup_named_type(LttFacility *fac, type_descriptor_t * td)
446 {
447 LttType *type = NULL;
448 GQuark name = 0;
449
450 if(td->type_name != NULL) {
451 /* Named type */
452 name = g_quark_from_string(td->type_name);
453
454 type = g_datalist_id_get_data(&fac->named_types, name);
455 }
456
457 if(type == NULL){
458 /* Create the type */
459 type = g_new(LttType,1);
460 type->type_name = name;
461 type->type_class = td->type;
462 if(td->fmt) type->fmt = g_strdup(td->fmt);
463 else type->fmt = NULL;
464 type->size = td->size;
465 type->enum_strings = NULL;
466 type->element_type = NULL;
467 type->element_number = 0;
468
469 if(td->type_name != NULL)
470 g_datalist_id_set_data_full(&fac->named_types, name,
471 type, (GDestroyNotify)freeLttNamedType);
472 }
473 return type;
474 }
475
476
477 /*****************************************************************************
478 *Function name
479 * ltt_facility_close : close a facility, decrease its usage count,
480 * if usage count = 0, release the memory
481 *Input params
482 * f : facility that will be closed
483 ****************************************************************************/
484
485 void ltt_facility_close(LttFacility *f)
486 {
487 //release the memory it occupied
488 freeFacility(f);
489 }
490
491 /*****************************************************************************
492 * Functions to release the memory occupied by the facility
493 ****************************************************************************/
494
495 void freeFacility(LttFacility * fac)
496 {
497 guint i;
498 LttEventType *et;
499
500 for(i=0; i<fac->events->len; i++) {
501 et = &g_array_index (fac->events, LttEventType, i);
502 freeEventtype(et);
503 }
504 g_array_free(fac->events, TRUE);
505
506 g_datalist_clear(&fac->named_types);
507
508 }
509
510 void freeEventtype(LttEventType * evType)
511 {
512 LttType * root_type;
513 if(evType->description)
514 g_free(evType->description);
515 if(evType->root_field){
516 root_type = evType->root_field->field_type;
517 freeLttField(evType->root_field);
518 freeLttType(&root_type);
519 }
520 }
521
522 void freeLttNamedType(LttType * type)
523 {
524 freeLttType(&type);
525 }
526
527 void freeLttType(LttType ** type)
528 {
529 unsigned int i;
530 if(*type == NULL) return;
531 if((*type)->type_name != 0) return; //this is a named type.
532 //if((*type)->type_name){
533 // return; //this is a named type
534 //}
535 if((*type)->fmt)
536 g_free((*type)->fmt);
537 if((*type)->enum_strings){
538 g_free((*type)->enum_strings);
539 }
540
541 if((*type)->element_type){
542 for(i=0;i<(*type)->element_number;i++)
543 freeLttType(&((*type)->element_type[i]));
544 g_free((*type)->element_type);
545 }
546 g_free(*type);
547 *type = NULL;
548 }
549
550 void freeLttField(LttField * fld)
551 {
552 int i;
553 int size = 0;
554
555 if(fld->field_type){
556 if(fld->field_type->type_class == LTT_ARRAY ||
557 fld->field_type->type_class == LTT_SEQUENCE){
558 size = 1;
559 }else if(fld->field_type->type_class == LTT_STRUCT){
560 size = fld->field_type->element_number;
561 }
562 }
563
564 if(fld->child){
565 for(i=0; i<size; i++){
566 if(fld->child[i])freeLttField(fld->child[i]);
567 }
568 g_free(fld->child);
569 }
570 g_free(fld);
571 }
572
573 /*****************************************************************************
574 *Function name
575 * ltt_facility_name : obtain the facility's name
576 *Input params
577 * f : the facility
578 *Return value
579 * GQuark : the facility's name
580 ****************************************************************************/
581
582 GQuark ltt_facility_name(LttFacility *f)
583 {
584 return f->name;
585 }
586
587 /*****************************************************************************
588 *Function name
589 * ltt_facility_checksum : obtain the facility's checksum
590 *Input params
591 * f : the facility
592 *Return value
593 * : the checksum of the facility
594 ****************************************************************************/
595
596 guint32 ltt_facility_checksum(LttFacility *f)
597 {
598 return f->checksum;
599 }
600
601 /*****************************************************************************
602 *Function name
603 * ltt_facility_base_id : obtain the facility base id
604 *Input params
605 * f : the facility
606 *Return value
607 * : the base id of the facility
608 ****************************************************************************/
609
610 guint ltt_facility_id(LttFacility *f)
611 {
612 return f->id;
613 }
614
615 /*****************************************************************************
616 *Function name
617 * ltt_facility_eventtype_number: obtain the number of the event types
618 *Input params
619 * f : the facility that will be closed
620 *Return value
621 * : the number of the event types
622 ****************************************************************************/
623
624 guint8 ltt_facility_eventtype_number(LttFacility *f)
625 {
626 return (f->events->len);
627 }
628
629 /*****************************************************************************
630 *Function name
631 * ltt_facility_eventtype_get: obtain the event type according to event id
632 * from 0 to event_number - 1
633 *Input params
634 * f : the facility that will be closed
635 *Return value
636 * LttEventType * : the event type required
637 ****************************************************************************/
638
639 LttEventType *ltt_facility_eventtype_get(LttFacility *f, guint8 i)
640 {
641 if(!f->exists) return NULL;
642
643 g_assert(i < f->events->len);
644 return &g_array_index(f->events, LttEventType, i);
645 }
646
647 /*****************************************************************************
648 *Function name
649 * ltt_facility_eventtype_get_by_name
650 * : obtain the event type according to event name
651 * event name is unique in the facility
652 *Input params
653 * f : the facility
654 * name : the name of the event
655 *Return value
656 * LttEventType * : the event type required
657 ****************************************************************************/
658
659 LttEventType *ltt_facility_eventtype_get_by_name(LttFacility *f, GQuark name)
660 {
661 LttEventType *et = g_datalist_id_get_data(&f->events_by_name, name);
662 return et;
663 }
664
This page took 0.06098 seconds and 4 git commands to generate.