47db6b80c29c7108fc1e1b0e95162814b339bc39
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,
22 #include <glib/gstdio.h>
23 #include <sys/types.h>
31 #include "ltt-private.h"
32 #include <ltt/facility.h>
36 /* search for the (named) type in the table, if it does not exist
38 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
);
40 /* construct directed acyclic graph for types, and tree for fields */
41 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
44 /* generate the facility according to the events belongin to it */
45 void generateFacility(LttFacility
* f
, facility_t
* fac
,
46 LttChecksum checksum
);
48 /* functions to release the memory occupied by a facility */
49 void freeFacility(LttFacility
* facility
);
50 void freeEventtype(LttEventType
* evType
);
51 void freeLttType(LttType
** type
);
52 void freeLttField(LttField
* fld
);
53 void freeLttNamedType(LttType
* type
);
56 /*****************************************************************************
58 * ltt_facility_open : open facilities
60 * t : the trace containing the facilities
61 * pathname : the path name of the facility
62 ****************************************************************************/
64 void ltt_facility_open(LttTrace
* t
, gchar
* pathname
)
72 GError
* error
= NULL
;
73 gchar buffer
[BUFFER_SIZE
];
75 in
.buffer
= &(buffer
[0]);
77 in
.error
= error_callback
;
80 in
.fd
= g_open(in
.name
, O_RDONLY
, 0);
81 if(in
.fd
< 0 ) in
.error(&in
,"cannot open input file");
83 in
.channel
= g_io_channel_unix_new(in
.fd
);
87 token
= getToken(&in
);
88 if(in
.type
== ENDFILE
) break;
90 if(g_ascii_strcasecmp(token
, "<")) in
.error(&in
,"not a facility file");
93 if(g_ascii_strcasecmp("facility",token
) == 0) {
94 fac
= g_new(facility_t
, 1);
96 fac
->description
= NULL
;
97 sequence_init(&(fac
->events
));
98 table_init(&(fac
->named_types
));
99 sequence_init(&(fac
->unnamed_types
));
101 parseFacility(&in
, fac
);
103 //check if any namedType is not defined
104 g_assert(checkNamedTypesImplemented(&fac
->named_types
) == 0);
106 g_assert(generateChecksum(fac
->name
, &checksum
, &fac
->events
) == 0);
108 f
= g_new(LttFacility
,1);
110 generateFacility(f
, fac
, checksum
);
112 t
->facility_number
++;
113 g_ptr_array_add(t
->facilities
,f
);
116 g_free(fac
->description
);
117 freeEvents(&fac
->events
);
118 sequence_dispose(&fac
->events
);
119 freeNamedType(&fac
->named_types
);
120 table_dispose(&fac
->named_types
);
121 freeTypes(&fac
->unnamed_types
);
122 sequence_dispose(&fac
->unnamed_types
);
125 else in
.error(&in
,"facility token was expected");
128 g_io_channel_shutdown(in
.channel
, FALSE
, &error
); /* No flush */
130 g_warning("Can not close file: \n%s\n", error
->message
);
138 /*****************************************************************************
140 * generateFacility : generate facility, internal function
142 * facility : LttFacilty structure
143 * fac : facility structure
144 * checksum : checksum of the facility
145 ****************************************************************************/
147 void generateFacility(LttFacility
*f
, facility_t
*fac
,LttChecksum checksum
)
149 char * facilityName
= fac
->name
;
150 sequence
* events
= &fac
->events
;
152 LttEventType
* evType
;
156 f
->name
= g_strdup(facilityName
);
157 f
->event_number
= events
->position
;
158 f
->checksum
= checksum
;
160 //initialize inner structures
161 f
->events
= g_new(LttEventType
*,f
->event_number
);
162 f
->named_types_number
= fac
->named_types
.keys
.position
;
163 f
->named_types
= g_new(LttType
*, fac
->named_types
.keys
.position
);
164 for(i
=0;i
<fac
->named_types
.keys
.position
;i
++) f
->named_types
[i
] = NULL
;
166 //for each event, construct field tree and type graph
167 for(i
=0;i
<events
->position
;i
++){
168 evType
= g_new(LttEventType
,1);
169 f
->events
[i
] = evType
;
171 evType
->name
= g_strdup(((event_t
*)(events
->array
[i
]))->name
);
172 evType
->description
=g_strdup(((event_t
*)(events
->array
[i
]))->description
);
174 field
= g_new(LttField
, 1);
175 evType
->root_field
= field
;
176 evType
->facility
= f
;
179 if(((event_t
*)(events
->array
[i
]))->type
!= NULL
){
180 field
->field_pos
= 0;
181 type
= lookup_named_type(f
,((event_t
*)(events
->array
[i
]))->type
);
182 field
->field_type
= type
;
183 field
->offset_root
= 0;
184 field
->fixed_root
= 1;
185 field
->offset_parent
= 0;
186 field
->fixed_parent
= 1;
187 // field->base_address = NULL;
188 field
->field_size
= 0;
189 field
->field_fixed
= -1;
190 field
->parent
= NULL
;
192 field
->current_element
= 0;
194 //construct field tree and type graph
195 constructTypeAndFields(f
,((event_t
*)(events
->array
[i
]))->type
,field
);
197 evType
->root_field
= NULL
;
204 /*****************************************************************************
206 * constructTypeAndFields : construct field tree and type graph,
207 * internal recursion function
209 * fac : facility struct
210 * td : type descriptor
211 * root_field : root field of the event
212 ****************************************************************************/
214 void constructTypeAndFields(LttFacility
* fac
,type_descriptor
* td
,
218 type_descriptor
* tmpTd
;
220 // if(td->type == LTT_STRING || td->type == LTT_SEQUENCE)
221 // fld->field_size = 0;
222 // else fld->field_size = -1;
224 if(td
->type
== LTT_ENUM
){
225 fld
->field_type
->element_number
= td
->labels
.position
;
226 fld
->field_type
->enum_strings
= g_new(char*,td
->labels
.position
);
227 for(i
=0;i
<td
->labels
.position
;i
++){
228 fld
->field_type
->enum_strings
[i
]
229 = g_strdup(((char*)(td
->labels
.array
[i
])));
231 }else if(td
->type
== LTT_ARRAY
|| td
->type
== LTT_SEQUENCE
){
232 if(td
->type
== LTT_ARRAY
)
233 fld
->field_type
->element_number
= (unsigned)td
->size
;
234 fld
->field_type
->element_type
= g_new(LttType
*,1);
235 tmpTd
= td
->nested_type
;
236 fld
->field_type
->element_type
[0] = lookup_named_type(fac
, tmpTd
);
237 fld
->child
= g_new(LttField
*, 1);
238 fld
->child
[0] = g_new(LttField
, 1);
240 fld
->child
[0]->field_pos
= 0;
241 fld
->child
[0]->field_type
= fld
->field_type
->element_type
[0];
242 fld
->child
[0]->offset_root
= fld
->offset_root
;
243 fld
->child
[0]->fixed_root
= fld
->fixed_root
;
244 fld
->child
[0]->offset_parent
= 0;
245 fld
->child
[0]->fixed_parent
= 1;
246 // fld->child[0]->base_address = NULL;
247 fld
->child
[0]->field_size
= 0;
248 fld
->child
[0]->field_fixed
= -1;
249 fld
->child
[0]->parent
= fld
;
250 fld
->child
[0]->child
= NULL
;
251 fld
->child
[0]->current_element
= 0;
252 constructTypeAndFields(fac
, tmpTd
, fld
->child
[0]);
253 }else if(td
->type
== LTT_STRUCT
){
254 fld
->field_type
->element_number
= td
->fields
.position
;
256 if(fld
->field_type
->element_type
== NULL
){
257 fld
->field_type
->element_type
= g_new(LttType
*, td
->fields
.position
);
263 fld
->child
= g_new(LttField
*, td
->fields
.position
);
264 for(i
=0;i
<td
->fields
.position
;i
++){
265 tmpTd
= ((type_fields
*)(td
->fields
.array
[i
]))->type
;
268 fld
->field_type
->element_type
[i
] = lookup_named_type(fac
, tmpTd
);
269 fld
->child
[i
] = g_new(LttField
,1);
271 fld
->child
[i
]->field_pos
= i
;
272 fld
->child
[i
]->field_type
= fld
->field_type
->element_type
[i
];
275 fld
->child
[i
]->field_type
->element_name
276 = g_strdup(((type_fields
*)(td
->fields
.array
[i
]))->name
);
279 fld
->child
[i
]->offset_root
= -1;
280 fld
->child
[i
]->fixed_root
= -1;
281 fld
->child
[i
]->offset_parent
= -1;
282 fld
->child
[i
]->fixed_parent
= -1;
283 // fld->child[i]->base_address = NULL;
284 fld
->child
[i
]->field_size
= 0;
285 fld
->child
[i
]->field_fixed
= -1;
286 fld
->child
[i
]->parent
= fld
;
287 fld
->child
[i
]->child
= NULL
;
288 fld
->child
[i
]->current_element
= 0;
289 constructTypeAndFields(fac
, tmpTd
, fld
->child
[i
]);
295 /*****************************************************************************
297 * lookup_named_type: search named type in the table
300 * fac : facility struct
301 * td : type descriptor
303 * : either find the named type, or create a new LttType
304 ****************************************************************************/
306 LttType
* lookup_named_type(LttFacility
*fac
, type_descriptor
* td
)
308 LttType
* lttType
= NULL
;
313 for(i
=0;i
<fac
->named_types_number
; i
++){
314 if(fac
->named_types
[i
] == NULL
) break;
315 name
= fac
->named_types
[i
]->type_name
;
316 if(g_ascii_strcasecmp(name
, td
->type_name
)==0){
317 lttType
= fac
->named_types
[i
];
318 // if(lttType->element_name) g_free(lttType->element_name);
319 // lttType->element_name = NULL;
326 lttType
= g_new(LttType
,1);
327 lttType
->type_class
= td
->type
;
328 if(td
->fmt
) lttType
->fmt
= g_strdup(td
->fmt
);
329 else lttType
->fmt
= NULL
;
330 lttType
->size
= td
->size
;
331 lttType
->enum_strings
= NULL
;
332 lttType
->element_type
= NULL
;
333 lttType
->element_number
= 0;
334 lttType
->element_name
= NULL
;
336 lttType
->type_name
= g_strdup(td
->type_name
);
337 fac
->named_types
[i
] = lttType
; /* i is initialized, checked. */
340 lttType
->type_name
= NULL
;
348 /*****************************************************************************
350 * ltt_facility_close : close a facility, decrease its usage count,
351 * if usage count = 0, release the memory
353 * f : facility that will be closed
355 * int : usage count ?? status
356 ****************************************************************************/
358 int ltt_facility_close(LttFacility
*f
)
360 //release the memory it occupied
366 /*****************************************************************************
367 * Functions to release the memory occupied by the facility
368 ****************************************************************************/
370 void freeFacility(LttFacility
* fac
)
373 g_free(fac
->name
); //free facility name
376 for(i
=0;i
<fac
->event_number
;i
++){
377 freeEventtype(fac
->events
[i
]);
381 //free all named types
382 for(i
=0;i
<fac
->named_types_number
;i
++){
383 freeLttNamedType(fac
->named_types
[i
]);
384 fac
->named_types
[i
] = NULL
;
386 g_free(fac
->named_types
);
388 //free the facility itself
392 void freeEventtype(LttEventType
* evType
)
395 g_free(evType
->name
);
396 if(evType
->description
)
397 g_free(evType
->description
);
398 if(evType
->root_field
){
399 root_type
= evType
->root_field
->field_type
;
400 freeLttField(evType
->root_field
);
401 freeLttType(&root_type
);
407 void freeLttNamedType(LttType
* type
)
409 g_free(type
->type_name
);
410 type
->type_name
= NULL
;
414 void freeLttType(LttType
** type
)
417 if(*type
== NULL
) return;
418 if((*type
)->type_name
){
419 return; //this is a named type
421 if((*type
)->element_name
)
422 g_free((*type
)->element_name
);
424 g_free((*type
)->fmt
);
425 if((*type
)->enum_strings
){
426 for(i
=0;i
<(*type
)->element_number
;i
++)
427 g_free((*type
)->enum_strings
[i
]);
428 g_free((*type
)->enum_strings
);
431 if((*type
)->element_type
){
432 for(i
=0;i
<(*type
)->element_number
;i
++)
433 freeLttType(&((*type
)->element_type
[i
]));
434 g_free((*type
)->element_type
);
440 void freeLttField(LttField
* fld
)
446 if(fld
->field_type
->type_class
== LTT_ARRAY
||
447 fld
->field_type
->type_class
== LTT_SEQUENCE
){
449 }else if(fld
->field_type
->type_class
== LTT_STRUCT
){
450 size
= fld
->field_type
->element_number
;
455 for(i
=0; i
<size
; i
++){
456 if(fld
->child
[i
])freeLttField(fld
->child
[i
]);
463 /*****************************************************************************
465 * ltt_facility_name : obtain the facility's name
467 * f : the facility that will be closed
469 * char * : the facility's name
470 ****************************************************************************/
472 gchar
*ltt_facility_name(LttFacility
*f
)
477 /*****************************************************************************
479 * ltt_facility_checksum : obtain the facility's checksum
481 * f : the facility that will be closed
483 * LttChecksum : the checksum of the facility
484 ****************************************************************************/
486 LttChecksum
ltt_facility_checksum(LttFacility
*f
)
491 /*****************************************************************************
493 * ltt_facility_base_id : obtain the facility base id
497 * : the base id of the facility
498 ****************************************************************************/
500 unsigned ltt_facility_base_id(LttFacility
*f
)
505 /*****************************************************************************
507 * ltt_facility_eventtype_number: obtain the number of the event types
509 * f : the facility that will be closed
511 * unsigned : the number of the event types
512 ****************************************************************************/
514 unsigned ltt_facility_eventtype_number(LttFacility
*f
)
516 return (f
->event_number
);
519 /*****************************************************************************
521 * ltt_facility_eventtype_get: obtain the event type according to event id
522 * from 0 to event_number - 1
524 * f : the facility that will be closed
526 * LttEventType * : the event type required
527 ****************************************************************************/
529 LttEventType
*ltt_facility_eventtype_get(LttFacility
*f
, unsigned i
)
534 /*****************************************************************************
536 * ltt_facility_eventtype_get_by_name
537 * : obtain the event type according to event name
538 * event name is unique in the facility
541 * name : the name of the event
543 * LttEventType * : the event type required
544 ****************************************************************************/
546 LttEventType
*ltt_facility_eventtype_get_by_name(LttFacility
*f
, gchar
*name
)
549 LttEventType
* ev
= NULL
;
551 for(i
=0;i
<f
->event_number
;i
++){
552 LttEventType
*iter_ev
= f
->events
[i
];
553 if(g_ascii_strcasecmp(iter_ev
->name
, name
) == 0) {
This page took 0.091237 seconds and 4 git commands to generate.