449cb9d7 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
57df94dd |
2 | * Copyright (C) 2003-2004 Xiangxiu Yang, Mathieu Desnoyers |
449cb9d7 |
3 | * |
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; |
7 | * |
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. |
12 | * |
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, |
16 | * MA 02111-1307, USA. |
17 | */ |
18 | |
4e4d11b3 |
19 | #ifdef HAVE_CONFIG_H |
20 | #include <config.h> |
21 | #endif |
22 | |
6cd62ccf |
23 | #include <stdio.h> |
57df94dd |
24 | #include <glib.h> |
6cd62ccf |
25 | |
6cd62ccf |
26 | #include "parser.h" |
a5dcde2f |
27 | #include <ltt/ltt.h> |
28 | #include "ltt-private.h" |
6cd62ccf |
29 | #include <ltt/type.h> |
30 | |
31 | static unsigned intSizes[] = { |
32 | sizeof(int8_t), sizeof(int16_t), sizeof(int32_t), sizeof(int64_t), |
33 | sizeof(short) }; |
34 | |
57df94dd |
35 | typedef enum _intSizesNames { SIZE_INT8, SIZE_INT16, SIZE_INT32, |
36 | SIZE_INT64, SIZE_SHORT, INT_SIZES_NUMBER } |
37 | intSizesNames; |
38 | |
39 | |
6cd62ccf |
40 | static unsigned floatSizes[] = { |
41 | 0, 0, sizeof(float), sizeof(double), 0, sizeof(float), sizeof(double) }; |
42 | |
57df94dd |
43 | #define FLOAT_SIZES_NUMBER 7 |
44 | |
6cd62ccf |
45 | |
46 | /***************************************************************************** |
47 | *Function name |
48 | * ltt_eventtype_name : get the name of the event type |
49 | *Input params |
50 | * et : an event type |
51 | *Return value |
3aee1200 |
52 | * GQuark : the name of the event type |
6cd62ccf |
53 | ****************************************************************************/ |
54 | |
3aee1200 |
55 | GQuark ltt_eventtype_name(LttEventType *et) |
6cd62ccf |
56 | { |
57 | return et->name; |
58 | } |
59 | |
60 | /***************************************************************************** |
61 | *Function name |
62 | * ltt_eventtype_description : get the description of the event type |
63 | *Input params |
64 | * et : an event type |
65 | *Return value |
66 | * char * : the description of the event type |
67 | ****************************************************************************/ |
68 | |
45e14832 |
69 | gchar *ltt_eventtype_description(LttEventType *et) |
6cd62ccf |
70 | { |
71 | return et->description; |
72 | } |
73 | |
963b5f2d |
74 | /***************************************************************************** |
75 | *Function name |
76 | * ltt_eventtype_facility : get the facility which contains the event type |
77 | *Input params |
78 | * et : an event type |
79 | *Return value |
80 | * LttFacility * : the facility |
81 | ****************************************************************************/ |
82 | |
83 | LttFacility *ltt_eventtype_facility(LttEventType *et) |
84 | { |
85 | return et->facility; |
86 | } |
87 | |
963b5f2d |
88 | /***************************************************************************** |
89 | *Function name |
90 | * ltt_eventtype_id : get the id of the event type |
91 | *Input params |
92 | * et : an event type |
93 | *Return value |
33690006 |
94 | * unsigned : the id |
963b5f2d |
95 | ****************************************************************************/ |
96 | |
3aee1200 |
97 | guint8 ltt_eventtype_id(LttEventType *et) |
963b5f2d |
98 | { |
3aee1200 |
99 | return et->index; |
963b5f2d |
100 | } |
101 | |
6cd62ccf |
102 | /***************************************************************************** |
103 | *Function name |
2312de30 |
104 | * ltt_field_name : get the name of the field |
6cd62ccf |
105 | *Input params |
2312de30 |
106 | * f : a field |
6cd62ccf |
107 | *Return value |
108 | * char * : the name of the type |
109 | ****************************************************************************/ |
110 | |
2312de30 |
111 | GQuark ltt_field_name(LttField *f) |
6cd62ccf |
112 | { |
2312de30 |
113 | return f->name; |
6cd62ccf |
114 | } |
6cd62ccf |
115 | /***************************************************************************** |
116 | *Function name |
117 | * ltt_type_class : get the type class of the type |
118 | *Input params |
119 | * t : a type |
120 | *Return value |
963b5f2d |
121 | * LttTypeEnum : the type class of the type |
6cd62ccf |
122 | ****************************************************************************/ |
123 | |
963b5f2d |
124 | LttTypeEnum ltt_type_class(LttType *t) |
6cd62ccf |
125 | { |
126 | return t->type_class; |
127 | } |
128 | |
129 | /***************************************************************************** |
130 | *Function name |
131 | * ltt_type_size : obtain the type size. The size is the number of bytes |
3aee1200 |
132 | * for primitive types (INT, UINT, FLOAT, ENUM) |
133 | * or the size for the unsigned integer length count for |
134 | * sequences |
6cd62ccf |
135 | *Input params |
136 | * tf : trace file |
137 | * t : a type |
138 | *Return value |
3aee1200 |
139 | * : the type size |
8d1e6362 |
140 | * returns 0 if erroneous, and show a critical warning message. |
6cd62ccf |
141 | ****************************************************************************/ |
142 | |
3aee1200 |
143 | size_t ltt_type_size(LttTrace * trace, LttType *t) |
6cd62ccf |
144 | { |
3aee1200 |
145 | size_t size; |
146 | |
147 | switch(t->type_class) { |
148 | |
149 | case LTT_INT: |
150 | case LTT_UINT: |
151 | case LTT_SEQUENCE: |
152 | case LTT_ENUM: |
57df94dd |
153 | if(likely(t->size < INT_SIZES_NUMBER)) |
154 | size = intSizes[t->size]; |
3aee1200 |
155 | else |
156 | goto error; |
157 | break; |
158 | case LTT_FLOAT: |
159 | if(likely(t->size < FLOAT_SIZES_NUMBER)) |
160 | size = floatSizes[t->size]; |
161 | else |
162 | goto error; |
163 | break; |
164 | case LTT_POINTER: |
165 | case LTT_LONG: |
166 | case LTT_ULONG: |
167 | case LTT_SIZE_T: |
168 | case LTT_SSIZE_T: |
169 | case LTT_OFF_T: |
170 | case LTT_STRING: |
171 | case LTT_ARRAY: |
172 | case LTT_STRUCT: |
173 | case LTT_UNION: |
174 | goto error; |
175 | break; |
6cd62ccf |
176 | } |
57df94dd |
177 | |
178 | return size; |
3aee1200 |
179 | |
180 | |
181 | error: |
182 | g_warning("no size known for the type"); |
183 | return 0; |
6cd62ccf |
184 | } |
185 | |
186 | /***************************************************************************** |
187 | *Function name |
188 | * ltt_type_element_type : obtain the type of nested elements for arrays |
189 | * and sequences |
190 | *Input params |
191 | * t : a type |
192 | *Return value |
963b5f2d |
193 | * LttType : the type of nested element of array or sequence |
6cd62ccf |
194 | ****************************************************************************/ |
195 | |
963b5f2d |
196 | LttType *ltt_type_element_type(LttType *t) |
6cd62ccf |
197 | { |
57df94dd |
198 | LttType *element_type; |
2312de30 |
199 | LttField *field; |
57df94dd |
200 | |
201 | if(unlikely(t->type_class != LTT_ARRAY && t->type_class != LTT_SEQUENCE)) |
202 | element_type = NULL; |
2312de30 |
203 | else { |
204 | if(t->type_class == LTT_ARRAY) |
205 | field = &g_array_index(t->fields, LttField, 0); |
206 | else |
207 | field = &g_array_index(t->fields, LttField, 1); |
208 | element_type = ltt_field_type(field); |
209 | } |
57df94dd |
210 | |
211 | return element_type; |
6cd62ccf |
212 | } |
213 | |
214 | /***************************************************************************** |
215 | *Function name |
d3cd9e86 |
216 | * ltt_type_element_number : obtain the number of elements for enums |
6cd62ccf |
217 | *Input params |
d3cd9e86 |
218 | * t : a type |
6cd62ccf |
219 | *Return value |
220 | * unsigned : the number of elements for arrays |
221 | ****************************************************************************/ |
963b5f2d |
222 | unsigned ltt_type_element_number(LttType *t) |
6cd62ccf |
223 | { |
57df94dd |
224 | unsigned ret = 0; |
225 | |
d3cd9e86 |
226 | if(likely(t->type_class == LTT_ENUM)) |
227 | ret = g_hash_table_size(t->enum_map); |
57df94dd |
228 | |
229 | return ret; |
6cd62ccf |
230 | } |
d3cd9e86 |
231 | |
6cd62ccf |
232 | /***************************************************************************** |
233 | *Function name |
234 | * ltt_type_member_number : obtain the number of data members for structure |
235 | *Input params |
236 | * t : a type |
237 | *Return value |
238 | * unsigned : the number of members for structure |
239 | ****************************************************************************/ |
240 | |
963b5f2d |
241 | unsigned ltt_type_member_number(LttType *t) |
6cd62ccf |
242 | { |
57df94dd |
243 | unsigned ret = 0; |
244 | |
245 | if(likely(t->type_class == LTT_STRUCT || t->type_class == LTT_UNION)) |
2312de30 |
246 | ret = t->fields->len; |
57df94dd |
247 | |
248 | return ret; |
6cd62ccf |
249 | } |
250 | |
6cd62ccf |
251 | |
252 | /***************************************************************************** |
253 | *Function name |
254 | * ltt_enum_string_get : for enumerations, obtain the symbolic string |
255 | * associated with a value (0 to n - 1 for an |
256 | * enumeration of n elements) |
257 | *Input params |
258 | * t : a type |
259 | * i : index of the member |
260 | *Return value |
261 | * char * : symbolic string associated with a value |
262 | ****************************************************************************/ |
263 | |
2312de30 |
264 | GQuark ltt_enum_string_get(LttType *t, gulong i) |
57df94dd |
265 | { |
2312de30 |
266 | if(likely(t->type_class == LTT_ENUM)) |
267 | return (GQuark)g_hash_table_lookup(t->enum_map, (gpointer)i); |
3aee1200 |
268 | else |
269 | return 0; |
6cd62ccf |
270 | } |
2312de30 |
271 | #if 0 |
6cd62ccf |
272 | /***************************************************************************** |
273 | *Function name |
274 | * ltt_field_element : obtain the field of nested elements for arrays and |
275 | * sequence |
276 | *Input params |
277 | * f : a field |
278 | *Return value |
963b5f2d |
279 | * LttField * : the field of the nested element |
6cd62ccf |
280 | ****************************************************************************/ |
281 | |
963b5f2d |
282 | LttField *ltt_field_element(LttField *f) |
6cd62ccf |
283 | { |
57df94dd |
284 | LttField *nest = NULL; |
285 | |
286 | if(likely(f->field_type->type_class == LTT_ARRAY || |
287 | f->field_type->type_class == LTT_SEQUENCE)) |
288 | nest = f->child[0]; |
6cd62ccf |
289 | |
57df94dd |
290 | return nest; |
6cd62ccf |
291 | } |
2312de30 |
292 | #endif//0 |
293 | |
294 | /***************************************************************************** |
295 | *Function name |
296 | * ltt_field_member_by_name : obtain the field of data members for structure |
297 | *Input params |
298 | * f : a field |
299 | * name : name of the field |
300 | *Return value |
301 | * LttField * : the field of the nested element |
302 | ****************************************************************************/ |
303 | |
304 | LttField *ltt_field_member_by_name(LttField *f, GQuark name) |
305 | { |
306 | LttField *field_member; |
307 | |
308 | g_assert(f->field_type.type_class == LTT_STRUCT || |
309 | f->field_type.type_class == LTT_UNION); |
310 | |
311 | field_member = g_datalist_id_get_data(&f->field_type.fields_by_name, name); |
312 | |
313 | return field_member; |
314 | } |
315 | |
6cd62ccf |
316 | |
317 | /***************************************************************************** |
318 | *Function name |
59d7bdf3 |
319 | * ltt_field_member : obtain the field of data members for structure |
6cd62ccf |
320 | *Input params |
321 | * f : a field |
322 | * i : index of member field |
323 | *Return value |
963b5f2d |
324 | * LttField * : the field of the nested element |
6cd62ccf |
325 | ****************************************************************************/ |
326 | |
2312de30 |
327 | LttField *ltt_field_member(LttField *f, guint i) |
6cd62ccf |
328 | { |
57df94dd |
329 | LttField *field_member; |
330 | |
2312de30 |
331 | g_assert(f->field_type.type_class == LTT_STRUCT || |
332 | f->field_type.type_class == LTT_UNION); |
333 | g_assert(i < f->field_type.fields->len); |
334 | |
335 | field_member = &g_array_index(f->field_type.fields, LttField, i); |
57df94dd |
336 | |
337 | return field_member; |
6cd62ccf |
338 | } |
339 | |
340 | /***************************************************************************** |
341 | *Function name |
342 | * ltt_field_type : obtain the type of the field |
343 | *Input params |
344 | * f : a field |
345 | *Return value |
346 | * ltt_tyoe * : the type of field |
347 | ****************************************************************************/ |
348 | |
963b5f2d |
349 | LttType *ltt_field_type(LttField *f) |
6cd62ccf |
350 | { |
57df94dd |
351 | if(unlikely(!f))return NULL; |
2312de30 |
352 | return &f->field_type; |
6cd62ccf |
353 | } |
354 | |
a5dcde2f |
355 | int ltt_field_size(LttField * f) |
356 | { |
57df94dd |
357 | if(unlikely(!f))return 0; |
a5dcde2f |
358 | return f->field_size; |
359 | } |
2312de30 |
360 | |
361 | |
362 | /***************************************************************************** |
363 | *Function name |
364 | * ltt_eventtype_num_fields : get the number of fields of the event |
365 | *Input params |
366 | * e : an instance of an event type |
367 | *Return value |
368 | * guint : number of fields |
369 | ****************************************************************************/ |
370 | |
371 | guint ltt_eventtype_num_fields(LttEventType *event_type) |
372 | { |
373 | if(unlikely(!event_type)) return NULL; |
374 | |
375 | return event_type->fields->len; |
376 | |
377 | } |
378 | /***************************************************************************** |
379 | *Function name |
380 | * ltt_eventtype_field : get the i th field of the event |
381 | *Input params |
382 | * e : an instance of an event type |
383 | * i : field index |
384 | *Return value |
385 | * LttField * : The requested field, or NULL |
386 | ****************************************************************************/ |
387 | |
388 | LttField *ltt_eventtype_field(LttEventType *event_type, guint i) |
389 | { |
390 | if(unlikely(!event_type)) return NULL; |
391 | |
392 | if(i >= event_type->fields->len) return NULL; |
393 | |
394 | return &g_array_index(event_type->fields, LttField, i); |
395 | |
396 | } |
397 | |
398 | /***************************************************************************** |
399 | *Function name |
400 | * ltt_eventtype_field_by_name : get a field of the event |
401 | *Input params |
402 | * e : an instance of an event type |
403 | * name : field name |
404 | *Return value |
405 | * LttField * : The requested field, or NULL |
406 | ****************************************************************************/ |
407 | |
408 | LttField *ltt_eventtype_field_by_name(LttEventType *event_type, GQuark name) |
409 | { |
410 | if(unlikely(!event_type)) return NULL; |
411 | |
412 | return (LttField*)g_datalist_id_get_data(&event_type->fields_by_name, name); |
413 | |
414 | } |
415 | |
416 | |