1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 * 2005 Mathieu Desnoyers
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;
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.
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,
28 #include <sys/types.h>
36 #include "ltt-private.h"
37 #include <ltt/facility.h>
45 /* search for the (named) type in the table, if it does not exist
47 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor_t
* td
);
49 /* construct directed acyclic graph for types, and tree for fields */
50 void construct_types_and_fields(LttFacility
* fac
, type_descriptor_t
* td
,
53 /* generate the facility according to the events belongin to it */
54 void generateFacility(LttFacility
* f
, facility_t
* fac
,
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
);
65 /*****************************************************************************
67 * ltt_facility_open : open facilities
69 * t : the trace containing the facilities
70 * pathname : the path name of the facility
72 *returns 0 on success, 1 on error.
73 ****************************************************************************/
75 int ltt_facility_open(LttFacility
*f
, LttTrace
* t
, gchar
* pathname
)
81 unsigned long checksum
;
82 //GError * error = NULL;
83 gchar buffer
[BUFFER_SIZE
];
85 in
.buffer
= &(buffer
[0]);
87 in
.error
= error_callback
;
90 //in.fd = g_open(in.name, O_RDONLY, 0);
92 in
.fp
= fopen(in
.name
, "r");
94 g_warning("cannot open facility description file %s",
100 //in.channel = g_io_channel_unix_new(in.fd);
104 token
= getToken(&in
);
105 if(in
.type
== ENDFILE
) break;
107 if(g_ascii_strcasecmp(token
, "<")) in
.error(&in
,"not a facility file");
108 token
= getName(&in
);
110 if(g_ascii_strcasecmp("facility",token
) == 0) {
111 fac
= g_new(facility_t
, 1);
113 fac
->description
= NULL
;
114 sequence_init(&(fac
->events
));
115 table_init(&(fac
->named_types
));
116 sequence_init(&(fac
->unnamed_types
));
118 parseFacility(&in
, fac
);
120 //check if any namedType is not defined
121 checkNamedTypesImplemented(&fac
->named_types
);
123 generateChecksum(fac
->name
, &checksum
, &fac
->events
);
125 generateFacility(f
, fac
, checksum
);
128 g_free(fac
->description
);
129 freeEvents(&fac
->events
);
130 sequence_dispose(&fac
->events
);
131 freeNamedType(&fac
->named_types
);
132 table_dispose(&fac
->named_types
);
133 freeTypes(&fac
->unnamed_types
);
134 sequence_dispose(&fac
->unnamed_types
);
138 g_warning("facility token was expected in file %s", in
.name
);
145 //g_io_channel_shutdown(in.channel, FALSE, &error); /* No flush */
146 //if(error != NULL) {
149 // g_warning("Can not close file: \n%s\n", error->message);
150 // g_error_free(error);
158 /*****************************************************************************
160 * generateFacility : generate facility, internal function
162 * facility : LttFacilty structure
163 * fac : facility structure
164 * checksum : checksum of the facility
165 ****************************************************************************/
167 void generateFacility(LttFacility
*f
, facility_t
*fac
, guint32 checksum
)
169 char * facilityName
= fac
->name
;
170 sequence_t
* events
= &fac
->events
;
172 //LttEventType * evType;
173 LttEventType
* event_type
;
177 g_assert(f
->name
== g_quark_from_string(facilityName
));
178 g_assert(f
->checksum
== checksum
);
180 //f->event_number = events->position;
182 //initialize inner structures
183 f
->events
= g_array_sized_new (FALSE
, TRUE
, sizeof(LttEventType
),
185 //f->events = g_new(LttEventType*,f->event_number);
186 f
->events
= g_array_set_size(f
->events
, events
->position
);
188 g_datalist_init(&f
->events_by_name
);
189 g_datalist_init(&f
->named_types
);
191 //f->named_types_number = fac->named_types.keys.position;
192 //f->named_types = g_array_sized_new (FALSE, TRUE, sizeof(LttType),
193 // fac->named_types.keys.position);
194 //f->named_types = g_new(LttType*, fac->named_types.keys.position);
195 //f->named_types = g_array_set_size(f->named_types,
196 // fac->named_types.keys.position);
198 //for each event, construct field tree and type graph
199 for(i
=0;i
<events
->position
;i
++){
200 event_type
= &g_array_index(f
->events
, LttEventType
, i
);
201 //evType = g_new(LttEventType,1);
202 //f->events[i] = evType;
205 g_quark_from_string(((event_t
*)(events
->array
[i
]))->name
);
207 g_datalist_id_set_data(&f
->events_by_name
, event_type
->name
,
210 event_type
->description
=
211 g_strdup(((event_t
*)(events
->array
[i
]))->description
);
213 field
= g_new(LttField
, 1);
214 event_type
->root_field
= field
;
215 event_type
->facility
= f
;
216 event_type
->index
= i
;
218 if(((event_t
*)(events
->array
[i
]))->type
!= NULL
){
219 // field->field_pos = 0;
220 type
= lookup_named_type(f
,((event_t
*)(events
->array
[i
]))->type
);
221 field
->field_type
= type
;
222 field
->offset_root
= 0;
223 field
->fixed_root
= FIELD_UNKNOWN
;
224 field
->offset_parent
= 0;
225 field
->fixed_parent
= FIELD_UNKNOWN
;
226 // field->base_address = NULL;
227 field
->field_size
= 0;
228 field
->fixed_size
= FIELD_UNKNOWN
;
229 field
->parent
= NULL
;
231 field
->current_element
= 0;
233 //construct field tree and type graph
234 construct_types_and_fields(f
,((event_t
*)(events
->array
[i
]))->type
,field
);
236 event_type
->root_field
= NULL
;
243 /*****************************************************************************
245 * construct_types_and_fields : construct field tree and type graph,
246 * internal recursion function
248 * fac : facility struct
249 * td : type descriptor
250 * root_field : root field of the event
251 ****************************************************************************/
254 void construct_types_and_fields(LttFacility
* fac
, type_descriptor_t
* td
,
258 type_descriptor_t
* tmpTd
;
264 fld
->field_type
->size
= td
->size
;
272 fld
->field_type
->size
= 0;
275 fld
->field_type
->size
= 0;
278 fld
->field_type
->element_number
= td
->labels
.position
;
279 fld
->field_type
->enum_strings
= g_new(GQuark
,td
->labels
.position
);
280 for(i
=0;i
<td
->labels
.position
;i
++){
281 fld
->field_type
->enum_strings
[i
]
282 = g_quark_from_string(((char*)(td
->labels
.array
[i
])));
284 fld
->field_type
->size
= td
->size
;
288 fld
->field_type
->element_number
= (unsigned)td
->size
;
290 fld
->field_type
->element_type
= g_new(LttType
*,1);
291 tmpTd
= td
->nested_type
;
292 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
293 fld
->child
= g_new(LttField
*, 1);
294 fld
->child
[0] = g_new(LttField
, 1);
296 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
297 fld
->child
[0]->offset_root
= 0;
298 fld
->child
[0]->fixed_root
= FIELD_UNKNOWN
;
299 fld
->child
[0]->offset_parent
= 0;
300 fld
->child
[0]->fixed_parent
= FIELD_UNKNOWN
;
301 fld
->child
[0]->field_size
= 0;
302 fld
->child
[0]->fixed_size
= FIELD_UNKNOWN
;
303 fld
->child
[0]->parent
= fld
;
304 fld
->child
[0]->child
= NULL
;
305 fld
->child
[0]->current_element
= 0;
306 construct_types_and_fields(fac
, tmpTd
, fld
->child
[0]);
311 fld
->field_type
->element_number
= td
->fields
.position
;
313 g_assert(fld
->field_type
->element_type
== NULL
);
314 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
316 fld
->child
= g_new(LttField
*, td
->fields
.position
);
317 for(i
=0;i
<td
->fields
.position
;i
++){
318 tmpTd
= ((field_t
*)(td
->fields
.array
[i
]))->type
;
320 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
321 fld
->child
[i
] = g_new(LttField
,1);
323 // fld->child[i]->field_pos = i;
324 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
326 fld
->child
[i
]->field_type
->element_name
327 = g_quark_from_string(((field_t
*)(td
->fields
.array
[i
]))->name
);
329 fld
->child
[i
]->offset_root
= 0;
330 fld
->child
[i
]->fixed_root
= FIELD_UNKNOWN
;
331 fld
->child
[i
]->offset_parent
= 0;
332 fld
->child
[i
]->fixed_parent
= FIELD_UNKNOWN
;
333 fld
->child
[i
]->field_size
= 0;
334 fld
->child
[i
]->fixed_size
= FIELD_UNKNOWN
;
335 fld
->child
[i
]->parent
= fld
;
336 fld
->child
[i
]->child
= NULL
;
337 fld
->child
[i
]->current_element
= 0;
338 construct_types_and_fields(fac
, tmpTd
, fld
->child
[i
]);
343 g_error("construct_types_and_fields : unknown type");
352 void construct_types_and_fields(LttFacility
* fac
, type_descriptor
* td
,
356 type_descriptor
* tmpTd
;
358 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
359 // fld->field_size = 0;
360 // else fld->field_size = -1;
362 if(td
->type
== LTT_ENUM
){
363 fld
->field_type
->element_number
= td
->labels
.position
;
364 fld
->field_type
->enum_strings
= g_new(GQuark
,td
->labels
.position
);
365 for(i
=0;i
<td
->labels
.position
;i
++){
366 fld
->field_type
->enum_strings
[i
]
367 = g_quark_from_string(((char*)(td
->labels
.array
[i
])));
369 }else if(td
->type
== LTT_ARRAY
|| td
->type
== LTT_SEQUENCE
){
370 if(td
->type
== LTT_ARRAY
)
371 fld
->field_type
->element_number
= (unsigned)td
->size
;
372 fld
->field_type
->element_type
= g_new(LttType
*,1);
373 tmpTd
= td
->nested_type
;
374 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
375 fld
->child
= g_new(LttField
*, 1);
376 fld
->child
[0] = g_new(LttField
, 1);
378 // fld->child[0]->field_pos = 0;
379 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
380 fld
->child
[0]->offset_root
= fld
->offset_root
;
381 fld
->child
[0]->fixed_root
= fld
->fixed_root
;
382 fld
->child
[0]->offset_parent
= 0;
383 fld
->child
[0]->fixed_parent
= 1;
384 // fld->child[0]->base_address = NULL;
385 fld
->child
[0]->field_size
= 0;
386 fld
->child
[0]->field_fixed
= -1;
387 fld
->child
[0]->parent
= fld
;
388 fld
->child
[0]->child
= NULL
;
389 fld
->child
[0]->current_element
= 0;
390 construct_types_and_fields(fac
, tmpTd
, fld
->child
[0]);
391 }else if(td
->type
== LTT_STRUCT
){
392 fld
->field_type
->element_number
= td
->fields
.position
;
394 if(fld
->field_type
->element_type
== NULL
){
395 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
401 fld
->child
= g_new(LttField
*, td
->fields
.position
);
402 for(i
=0;i
<td
->fields
.position
;i
++){
403 tmpTd
= ((type_fields
*)(td
->fields
.array
[i
]))->type
;
406 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
407 fld
->child
[i
] = g_new(LttField
,1);
409 fld
->child
[i
]->field_pos
= i
;
410 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
413 fld
->child
[i
]->field_type
->element_name
414 = g_quark_from_string(((type_fields
*)(td
->fields
.array
[i
]))->name
);
417 fld
->child
[i
]->offset_root
= -1;
418 fld
->child
[i
]->fixed_root
= -1;
419 fld
->child
[i
]->offset_parent
= -1;
420 fld
->child
[i
]->fixed_parent
= -1;
421 // fld->child[i]->base_address = NULL;
422 fld
->child
[i
]->field_size
= 0;
423 fld
->child
[i
]->field_fixed
= -1;
424 fld
->child
[i
]->parent
= fld
;
425 fld
->child
[i
]->child
= NULL
;
426 fld
->child
[i
]->current_element
= 0;
427 construct_types_and_fields(fac
, tmpTd
, fld
->child
[i
]);
433 /*****************************************************************************
435 * lookup_named_type: search named type in the table
438 * fac : facility struct
439 * td : type descriptor
441 * : either find the named type, or create a new LttType
442 ****************************************************************************/
444 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor_t
* td
)
446 LttType
*type
= NULL
;
449 if(td
->type_name
!= NULL
) {
451 name
= g_quark_from_string(td
->type_name
);
453 type
= g_datalist_id_get_data(&fac
->named_types
, name
);
457 /* Create the type */
458 type
= g_new(LttType
,1);
459 type
->type_name
= name
;
460 type
->type_class
= td
->type
;
461 if(td
->fmt
) type
->fmt
= g_strdup(td
->fmt
);
462 else type
->fmt
= NULL
;
463 type
->size
= td
->size
;
464 type
->enum_strings
= NULL
;
465 type
->element_type
= NULL
;
466 type
->element_number
= 0;
468 if(td
->type_name
!= NULL
)
469 g_datalist_id_set_data_full(&fac
->named_types
, name
,
470 type
, (GDestroyNotify
)freeLttNamedType
);
476 /*****************************************************************************
478 * ltt_facility_close : close a facility, decrease its usage count,
479 * if usage count = 0, release the memory
481 * f : facility that will be closed
482 ****************************************************************************/
484 void ltt_facility_close(LttFacility
*f
)
486 //release the memory it occupied
490 /*****************************************************************************
491 * Functions to release the memory occupied by the facility
492 ****************************************************************************/
494 void freeFacility(LttFacility
* fac
)
499 for(i
=0; i
<fac
->events
->len
; i
++) {
500 et
= &g_array_index (fac
->events
, LttEventType
, i
);
503 g_array_free(fac
->events
, TRUE
);
505 g_datalist_clear(&fac
->named_types
);
509 void freeEventtype(LttEventType
* evType
)
512 if(evType
->description
)
513 g_free(evType
->description
);
514 if(evType
->root_field
){
515 root_type
= evType
->root_field
->field_type
;
516 freeLttField(evType
->root_field
);
517 freeLttType(&root_type
);
521 void freeLttNamedType(LttType
* type
)
526 void freeLttType(LttType
** type
)
529 if(*type
== NULL
) return;
530 if((*type
)->type_name
!= 0) return; //this is a named type.
531 //if((*type)->type_name){
532 // return; //this is a named type
535 g_free((*type
)->fmt
);
536 if((*type
)->enum_strings
){
537 g_free((*type
)->enum_strings
);
540 if((*type
)->element_type
){
541 for(i
=0;i
<(*type
)->element_number
;i
++)
542 freeLttType(&((*type
)->element_type
[i
]));
543 g_free((*type
)->element_type
);
549 void freeLttField(LttField
* fld
)
555 if(fld
->field_type
->type_class
== LTT_ARRAY
||
556 fld
->field_type
->type_class
== LTT_SEQUENCE
){
558 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
559 size
= fld
->field_type
->element_number
;
564 for(i
=0; i
<size
; i
++){
565 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
572 /*****************************************************************************
574 * ltt_facility_name : obtain the facility's name
578 * GQuark : the facility's name
579 ****************************************************************************/
581 GQuark
ltt_facility_name(LttFacility
*f
)
586 /*****************************************************************************
588 * ltt_facility_checksum : obtain the facility's checksum
592 * : the checksum of the facility
593 ****************************************************************************/
595 guint32
ltt_facility_checksum(LttFacility
*f
)
600 /*****************************************************************************
602 * ltt_facility_base_id : obtain the facility base id
606 * : the base id of the facility
607 ****************************************************************************/
609 guint
ltt_facility_id(LttFacility
*f
)
614 /*****************************************************************************
616 * ltt_facility_eventtype_number: obtain the number of the event types
618 * f : the facility that will be closed
620 * : the number of the event types
621 ****************************************************************************/
623 guint8
ltt_facility_eventtype_number(LttFacility
*f
)
625 return (f
->events
->len
);
628 /*****************************************************************************
630 * ltt_facility_eventtype_get: obtain the event type according to event id
631 * from 0 to event_number - 1
633 * f : the facility that will be closed
635 * LttEventType * : the event type required
636 ****************************************************************************/
638 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, guint8 i
)
640 if(!f
->exists
) return NULL
;
642 g_assert(i
< f
->events
->len
);
643 return &g_array_index(f
->events
, LttEventType
, i
);
646 /*****************************************************************************
648 * ltt_facility_eventtype_get_by_name
649 * : obtain the event type according to event name
650 * event name is unique in the facility
653 * name : the name of the event
655 * LttEventType * : the event type required
656 ****************************************************************************/
658 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, GQuark name
)
660 LttEventType
*et
= g_datalist_id_get_data(&f
->events_by_name
, name
);
This page took 0.070547 seconds and 4 git commands to generate.