1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License Version 2 as
6 * published by the Free Software Foundation;
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23 #include <sys/types.h>
31 #include "ltt-private.h"
32 #include <ltt/facility.h>
40 /* search for the (named) type in the table, if it does not exist
42 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
);
44 /* construct directed acyclic graph for types, and tree for fields */
45 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
48 /* generate the facility according to the events belongin to it */
49 void generateFacility(LttFacility
* f
, facility_t
* fac
,
50 LttChecksum checksum
);
52 /* functions to release the memory occupied by a facility */
53 void freeFacility(LttFacility
* facility
);
54 void freeEventtype(LttEventType
* evType
);
55 void freeLttType(LttType
** type
);
56 void freeLttField(LttField
* fld
);
57 void freeLttNamedType(LttType
* type
);
60 /*****************************************************************************
62 * ltt_facility_open : open facilities
64 * t : the trace containing the facilities
65 * pathname : the path name of the facility
66 ****************************************************************************/
68 void ltt_facility_open(LttTrace
* t
, gchar
* pathname
)
76 GError
* error
= NULL
;
77 gchar buffer
[BUFFER_SIZE
];
79 in
.buffer
= &(buffer
[0]);
81 in
.error
= error_callback
;
84 in
.fd
= g_open(in
.name
, O_RDONLY
, 0);
85 if(in
.fd
< 0 ) in
.error(&in
,"cannot open input file");
87 in
.channel
= g_io_channel_unix_new(in
.fd
);
91 token
= getToken(&in
);
92 if(in
.type
== ENDFILE
) break;
94 if(g_ascii_strcasecmp(token
, "<")) in
.error(&in
,"not a facility file");
97 if(g_ascii_strcasecmp("facility",token
) == 0) {
98 fac
= g_new(facility_t
, 1);
100 fac
->description
= NULL
;
101 sequence_init(&(fac
->events
));
102 table_init(&(fac
->named_types
));
103 sequence_init(&(fac
->unnamed_types
));
105 parseFacility(&in
, fac
);
107 //check if any namedType is not defined
108 g_assert(checkNamedTypesImplemented(&fac
->named_types
) == 0);
110 g_assert(generateChecksum(fac
->name
, &checksum
, &fac
->events
) == 0);
112 f
= g_new(LttFacility
,1);
114 generateFacility(f
, fac
, checksum
);
116 t
->facility_number
++;
117 g_ptr_array_add(t
->facilities
,f
);
120 g_free(fac
->description
);
121 freeEvents(&fac
->events
);
122 sequence_dispose(&fac
->events
);
123 freeNamedType(&fac
->named_types
);
124 table_dispose(&fac
->named_types
);
125 freeTypes(&fac
->unnamed_types
);
126 sequence_dispose(&fac
->unnamed_types
);
129 else in
.error(&in
,"facility token was expected");
132 g_io_channel_shutdown(in
.channel
, FALSE
, &error
); /* No flush */
134 g_warning("Can not close file: \n%s\n", error
->message
);
142 /*****************************************************************************
144 * generateFacility : generate facility, internal function
146 * facility : LttFacilty structure
147 * fac : facility structure
148 * checksum : checksum of the facility
149 ****************************************************************************/
151 void generateFacility(LttFacility
*f
, facility_t
*fac
,LttChecksum checksum
)
153 char * facilityName
= fac
->name
;
154 sequence
* events
= &fac
->events
;
156 LttEventType
* evType
;
160 f
->name
= g_strdup(facilityName
);
161 f
->event_number
= events
->position
;
162 f
->checksum
= checksum
;
164 //initialize inner structures
165 f
->events
= g_new(LttEventType
*,f
->event_number
);
166 f
->named_types_number
= fac
->named_types
.keys
.position
;
167 f
->named_types
= g_new(LttType
*, fac
->named_types
.keys
.position
);
168 for(i
=0;i
<fac
->named_types
.keys
.position
;i
++) f
->named_types
[i
] = NULL
;
170 //for each event, construct field tree and type graph
171 for(i
=0;i
<events
->position
;i
++){
172 evType
= g_new(LttEventType
,1);
173 f
->events
[i
] = evType
;
175 evType
->name
= g_strdup(((event_t
*)(events
->array
[i
]))->name
);
176 evType
->description
=g_strdup(((event_t
*)(events
->array
[i
]))->description
);
178 field
= g_new(LttField
, 1);
179 evType
->root_field
= field
;
180 evType
->facility
= f
;
183 if(((event_t
*)(events
->array
[i
]))->type
!= NULL
){
184 field
->field_pos
= 0;
185 type
= lookup_named_type(f
,((event_t
*)(events
->array
[i
]))->type
);
186 field
->field_type
= type
;
187 field
->offset_root
= 0;
188 field
->fixed_root
= 1;
189 field
->offset_parent
= 0;
190 field
->fixed_parent
= 1;
191 // field->base_address = NULL;
192 field
->field_size
= 0;
193 field
->field_fixed
= -1;
194 field
->parent
= NULL
;
196 field
->current_element
= 0;
198 //construct field tree and type graph
199 constructTypeAndFields(f
,((event_t
*)(events
->array
[i
]))->type
,field
);
201 evType
->root_field
= NULL
;
208 /*****************************************************************************
210 * constructTypeAndFields : construct field tree and type graph,
211 * internal recursion function
213 * fac : facility struct
214 * td : type descriptor
215 * root_field : root field of the event
216 ****************************************************************************/
218 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
222 type_descriptor
* tmpTd
;
224 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
225 // fld->field_size = 0;
226 // else fld->field_size = -1;
228 if(td
->type
== LTT_ENUM
){
229 fld
->field_type
->element_number
= td
->labels
.position
;
230 fld
->field_type
->enum_strings
= g_new(char*,td
->labels
.position
);
231 for(i
=0;i
<td
->labels
.position
;i
++){
232 fld
->field_type
->enum_strings
[i
]
233 = g_strdup(((char*)(td
->labels
.array
[i
])));
235 }else if(td
->type
== LTT_ARRAY
|| td
->type
== LTT_SEQUENCE
){
236 if(td
->type
== LTT_ARRAY
)
237 fld
->field_type
->element_number
= (unsigned)td
->size
;
238 fld
->field_type
->element_type
= g_new(LttType
*,1);
239 tmpTd
= td
->nested_type
;
240 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
241 fld
->child
= g_new(LttField
*, 1);
242 fld
->child
[0] = g_new(LttField
, 1);
244 fld
->child
[0]->field_pos
= 0;
245 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
246 fld
->child
[0]->offset_root
= fld
->offset_root
;
247 fld
->child
[0]->fixed_root
= fld
->fixed_root
;
248 fld
->child
[0]->offset_parent
= 0;
249 fld
->child
[0]->fixed_parent
= 1;
250 // fld->child[0]->base_address = NULL;
251 fld
->child
[0]->field_size
= 0;
252 fld
->child
[0]->field_fixed
= -1;
253 fld
->child
[0]->parent
= fld
;
254 fld
->child
[0]->child
= NULL
;
255 fld
->child
[0]->current_element
= 0;
256 constructTypeAndFields(fac
, tmpTd
, fld
->child
[0]);
257 }else if(td
->type
== LTT_STRUCT
){
258 fld
->field_type
->element_number
= td
->fields
.position
;
260 if(fld
->field_type
->element_type
== NULL
){
261 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
267 fld
->child
= g_new(LttField
*, td
->fields
.position
);
268 for(i
=0;i
<td
->fields
.position
;i
++){
269 tmpTd
= ((type_fields
*)(td
->fields
.array
[i
]))->type
;
272 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
273 fld
->child
[i
] = g_new(LttField
,1);
275 fld
->child
[i
]->field_pos
= i
;
276 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
279 fld
->child
[i
]->field_type
->element_name
280 = g_strdup(((type_fields
*)(td
->fields
.array
[i
]))->name
);
283 fld
->child
[i
]->offset_root
= -1;
284 fld
->child
[i
]->fixed_root
= -1;
285 fld
->child
[i
]->offset_parent
= -1;
286 fld
->child
[i
]->fixed_parent
= -1;
287 // fld->child[i]->base_address = NULL;
288 fld
->child
[i
]->field_size
= 0;
289 fld
->child
[i
]->field_fixed
= -1;
290 fld
->child
[i
]->parent
= fld
;
291 fld
->child
[i
]->child
= NULL
;
292 fld
->child
[i
]->current_element
= 0;
293 constructTypeAndFields(fac
, tmpTd
, fld
->child
[i
]);
299 /*****************************************************************************
301 * lookup_named_type: search named type in the table
304 * fac : facility struct
305 * td : type descriptor
307 * : either find the named type, or create a new LttType
308 ****************************************************************************/
310 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
)
312 LttType
* lttType
= NULL
;
317 for(i
=0;i
<fac
->named_types_number
; i
++){
318 if(fac
->named_types
[i
] == NULL
) break;
319 name
= fac
->named_types
[i
]->type_name
;
320 if(g_ascii_strcasecmp(name
, td
->type_name
)==0){
321 lttType
= fac
->named_types
[i
];
322 // if(lttType->element_name) g_free(lttType->element_name);
323 // lttType->element_name = NULL;
330 lttType
= g_new(LttType
,1);
331 lttType
->type_class
= td
->type
;
332 if(td
->fmt
) lttType
->fmt
= g_strdup(td
->fmt
);
333 else lttType
->fmt
= NULL
;
334 lttType
->size
= td
->size
;
335 lttType
->enum_strings
= NULL
;
336 lttType
->element_type
= NULL
;
337 lttType
->element_number
= 0;
338 lttType
->element_name
= NULL
;
340 lttType
->type_name
= g_strdup(td
->type_name
);
341 fac
->named_types
[i
] = lttType
; /* i is initialized, checked. */
344 lttType
->type_name
= NULL
;
352 /*****************************************************************************
354 * ltt_facility_close : close a facility, decrease its usage count,
355 * if usage count = 0, release the memory
357 * f : facility that will be closed
359 * int : usage count ?? status
360 ****************************************************************************/
362 int ltt_facility_close(LttFacility
*f
)
364 //release the memory it occupied
370 /*****************************************************************************
371 * Functions to release the memory occupied by the facility
372 ****************************************************************************/
374 void freeFacility(LttFacility
* fac
)
377 g_free(fac
->name
); //free facility name
380 for(i
=0;i
<fac
->event_number
;i
++){
381 freeEventtype(fac
->events
[i
]);
385 //free all named types
386 for(i
=0;i
<fac
->named_types_number
;i
++){
387 freeLttNamedType(fac
->named_types
[i
]);
388 fac
->named_types
[i
] = NULL
;
390 g_free(fac
->named_types
);
392 //free the facility itself
396 void freeEventtype(LttEventType
* evType
)
399 g_free(evType
->name
);
400 if(evType
->description
)
401 g_free(evType
->description
);
402 if(evType
->root_field
){
403 root_type
= evType
->root_field
->field_type
;
404 freeLttField(evType
->root_field
);
405 freeLttType(&root_type
);
411 void freeLttNamedType(LttType
* type
)
413 g_free(type
->type_name
);
414 type
->type_name
= NULL
;
418 void freeLttType(LttType
** type
)
421 if(*type
== NULL
) return;
422 if((*type
)->type_name
){
423 return; //this is a named type
425 if((*type
)->element_name
)
426 g_free((*type
)->element_name
);
428 g_free((*type
)->fmt
);
429 if((*type
)->enum_strings
){
430 for(i
=0;i
<(*type
)->element_number
;i
++)
431 g_free((*type
)->enum_strings
[i
]);
432 g_free((*type
)->enum_strings
);
435 if((*type
)->element_type
){
436 for(i
=0;i
<(*type
)->element_number
;i
++)
437 freeLttType(&((*type
)->element_type
[i
]));
438 g_free((*type
)->element_type
);
444 void freeLttField(LttField
* fld
)
450 if(fld
->field_type
->type_class
== LTT_ARRAY
||
451 fld
->field_type
->type_class
== LTT_SEQUENCE
){
453 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
454 size
= fld
->field_type
->element_number
;
459 for(i
=0; i
<size
; i
++){
460 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
467 /*****************************************************************************
469 * ltt_facility_name : obtain the facility's name
471 * f : the facility that will be closed
473 * char * : the facility's name
474 ****************************************************************************/
476 gchar
*ltt_facility_name(LttFacility
*f
)
481 /*****************************************************************************
483 * ltt_facility_checksum : obtain the facility's checksum
485 * f : the facility that will be closed
487 * LttChecksum : the checksum of the facility
488 ****************************************************************************/
490 LttChecksum
ltt_facility_checksum(LttFacility
*f
)
495 /*****************************************************************************
497 * ltt_facility_base_id : obtain the facility base id
501 * : the base id of the facility
502 ****************************************************************************/
504 unsigned ltt_facility_base_id(LttFacility
*f
)
509 /*****************************************************************************
511 * ltt_facility_eventtype_number: obtain the number of the event types
513 * f : the facility that will be closed
515 * unsigned : the number of the event types
516 ****************************************************************************/
518 unsigned ltt_facility_eventtype_number(LttFacility
*f
)
520 return (f
->event_number
);
523 /*****************************************************************************
525 * ltt_facility_eventtype_get: obtain the event type according to event id
526 * from 0 to event_number - 1
528 * f : the facility that will be closed
530 * LttEventType * : the event type required
531 ****************************************************************************/
533 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, unsigned i
)
538 /*****************************************************************************
540 * ltt_facility_eventtype_get_by_name
541 * : obtain the event type according to event name
542 * event name is unique in the facility
545 * name : the name of the event
547 * LttEventType * : the event type required
548 ****************************************************************************/
550 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, gchar
*name
)
553 LttEventType
* ev
= NULL
;
555 for(i
=0;i
<f
->event_number
;i
++){
556 LttEventType
*iter_ev
= f
->events
[i
];
557 if(g_ascii_strcasecmp(iter_ev
->name
, name
) == 0) {
This page took 0.056853 seconds and 4 git commands to generate.