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 |
104 | * ltt_eventtype_type : get the type of the event type |
105 | *Input params |
106 | * et : an event type |
107 | *Return value |
963b5f2d |
108 | * LttType * : the type of the event type |
6cd62ccf |
109 | ****************************************************************************/ |
110 | |
963b5f2d |
111 | LttType *ltt_eventtype_type(LttEventType *et) |
6cd62ccf |
112 | { |
57df94dd |
113 | if(unlikely(!et->root_field)) return NULL; |
114 | else return et->root_field->field_type; |
6cd62ccf |
115 | } |
116 | |
963b5f2d |
117 | /***************************************************************************** |
118 | *Function name |
119 | * ltt_eventtype_field : get the root filed of the event type |
120 | *Input params |
121 | * et : an event type |
122 | *Return value |
123 | * LttField * : the root filed of the event type |
124 | ****************************************************************************/ |
125 | |
126 | LttField *ltt_eventtype_field(LttEventType *et) |
127 | { |
128 | return et->root_field; |
129 | } |
130 | |
6cd62ccf |
131 | /***************************************************************************** |
132 | *Function name |
133 | * ltt_type_name : get the name of the type |
134 | *Input params |
135 | * t : a type |
136 | *Return value |
137 | * char * : the name of the type |
138 | ****************************************************************************/ |
139 | |
3aee1200 |
140 | GQuark ltt_type_name(LttType *t) |
6cd62ccf |
141 | { |
142 | return t->element_name; |
143 | } |
144 | |
145 | /***************************************************************************** |
146 | *Function name |
147 | * ltt_type_class : get the type class of the type |
148 | *Input params |
149 | * t : a type |
150 | *Return value |
963b5f2d |
151 | * LttTypeEnum : the type class of the type |
6cd62ccf |
152 | ****************************************************************************/ |
153 | |
963b5f2d |
154 | LttTypeEnum ltt_type_class(LttType *t) |
6cd62ccf |
155 | { |
156 | return t->type_class; |
157 | } |
158 | |
159 | /***************************************************************************** |
160 | *Function name |
161 | * ltt_type_size : obtain the type size. The size is the number of bytes |
3aee1200 |
162 | * for primitive types (INT, UINT, FLOAT, ENUM) |
163 | * or the size for the unsigned integer length count for |
164 | * sequences |
6cd62ccf |
165 | *Input params |
166 | * tf : trace file |
167 | * t : a type |
168 | *Return value |
3aee1200 |
169 | * : the type size |
8d1e6362 |
170 | * returns 0 if erroneous, and show a critical warning message. |
6cd62ccf |
171 | ****************************************************************************/ |
172 | |
3aee1200 |
173 | size_t ltt_type_size(LttTrace * trace, LttType *t) |
6cd62ccf |
174 | { |
3aee1200 |
175 | size_t size; |
176 | |
177 | switch(t->type_class) { |
178 | |
179 | case LTT_INT: |
180 | case LTT_UINT: |
181 | case LTT_SEQUENCE: |
182 | case LTT_ENUM: |
57df94dd |
183 | if(likely(t->size < INT_SIZES_NUMBER)) |
184 | size = intSizes[t->size]; |
3aee1200 |
185 | else |
186 | goto error; |
187 | break; |
188 | case LTT_FLOAT: |
189 | if(likely(t->size < FLOAT_SIZES_NUMBER)) |
190 | size = floatSizes[t->size]; |
191 | else |
192 | goto error; |
193 | break; |
194 | case LTT_POINTER: |
195 | case LTT_LONG: |
196 | case LTT_ULONG: |
197 | case LTT_SIZE_T: |
198 | case LTT_SSIZE_T: |
199 | case LTT_OFF_T: |
200 | case LTT_STRING: |
201 | case LTT_ARRAY: |
202 | case LTT_STRUCT: |
203 | case LTT_UNION: |
204 | goto error; |
205 | break; |
6cd62ccf |
206 | } |
57df94dd |
207 | |
208 | return size; |
3aee1200 |
209 | |
210 | |
211 | error: |
212 | g_warning("no size known for the type"); |
213 | return 0; |
6cd62ccf |
214 | } |
215 | |
216 | /***************************************************************************** |
217 | *Function name |
218 | * ltt_type_element_type : obtain the type of nested elements for arrays |
219 | * and sequences |
220 | *Input params |
221 | * t : a type |
222 | *Return value |
963b5f2d |
223 | * LttType : the type of nested element of array or sequence |
6cd62ccf |
224 | ****************************************************************************/ |
225 | |
963b5f2d |
226 | LttType *ltt_type_element_type(LttType *t) |
6cd62ccf |
227 | { |
57df94dd |
228 | LttType *element_type; |
229 | |
230 | if(unlikely(t->type_class != LTT_ARRAY && t->type_class != LTT_SEQUENCE)) |
231 | element_type = NULL; |
232 | else |
233 | element_type = t->element_type[0]; |
234 | |
235 | return element_type; |
6cd62ccf |
236 | } |
237 | |
238 | /***************************************************************************** |
239 | *Function name |
240 | * ltt_type_element_number : obtain the number of elements for arrays |
241 | *Input params |
242 | * t : a type |
243 | *Return value |
244 | * unsigned : the number of elements for arrays |
245 | ****************************************************************************/ |
246 | |
963b5f2d |
247 | unsigned ltt_type_element_number(LttType *t) |
6cd62ccf |
248 | { |
57df94dd |
249 | unsigned ret = 0; |
250 | |
251 | if(likely(t->type_class == LTT_ARRAY)) |
252 | ret = t->element_number; |
253 | |
254 | return ret; |
6cd62ccf |
255 | } |
256 | |
257 | /***************************************************************************** |
258 | *Function name |
259 | * ltt_type_member_number : obtain the number of data members for structure |
260 | *Input params |
261 | * t : a type |
262 | *Return value |
263 | * unsigned : the number of members for structure |
264 | ****************************************************************************/ |
265 | |
963b5f2d |
266 | unsigned ltt_type_member_number(LttType *t) |
6cd62ccf |
267 | { |
57df94dd |
268 | unsigned ret = 0; |
269 | |
270 | if(likely(t->type_class == LTT_STRUCT || t->type_class == LTT_UNION)) |
271 | ret =t->element_number; |
272 | |
273 | return ret; |
6cd62ccf |
274 | } |
275 | |
276 | /***************************************************************************** |
277 | *Function name |
59d7bdf3 |
278 | * ltt_type_member_type : obtain the type of a data member in a structure |
279 | * or union. |
6cd62ccf |
280 | *Input params |
281 | * t : a type |
282 | * i : index of the member |
283 | *Return value |
963b5f2d |
284 | * LttType * : the type of structure member |
6cd62ccf |
285 | ****************************************************************************/ |
286 | |
3aee1200 |
287 | LttType *ltt_type_member_type(LttType *t, unsigned i, GQuark *name) |
6cd62ccf |
288 | { |
57df94dd |
289 | LttType *member_type = NULL; |
290 | |
291 | if(unlikely( (t->type_class != LTT_STRUCT |
292 | && t->type_class != LTT_UNION) |
293 | || |
294 | (i >= t->element_number) |
295 | )) { |
3aee1200 |
296 | *name = 0; |
57df94dd |
297 | } else { |
298 | *name = t->element_type[i]->element_name; |
299 | member_type = t->element_type[i]; |
300 | } |
301 | |
302 | return member_type; |
6cd62ccf |
303 | } |
304 | |
305 | /***************************************************************************** |
306 | *Function name |
307 | * ltt_enum_string_get : for enumerations, obtain the symbolic string |
308 | * associated with a value (0 to n - 1 for an |
309 | * enumeration of n elements) |
310 | *Input params |
311 | * t : a type |
312 | * i : index of the member |
313 | *Return value |
314 | * char * : symbolic string associated with a value |
315 | ****************************************************************************/ |
316 | |
3aee1200 |
317 | GQuark ltt_enum_string_get(LttType *t, unsigned i) |
57df94dd |
318 | { |
57df94dd |
319 | if(likely(t->type_class == LTT_ENUM && i < t->element_number)) |
3aee1200 |
320 | return t->enum_strings[i]; |
321 | else |
322 | return 0; |
6cd62ccf |
323 | } |
324 | |
325 | /***************************************************************************** |
326 | *Function name |
327 | * ltt_field_element : obtain the field of nested elements for arrays and |
328 | * sequence |
329 | *Input params |
330 | * f : a field |
331 | *Return value |
963b5f2d |
332 | * LttField * : the field of the nested element |
6cd62ccf |
333 | ****************************************************************************/ |
334 | |
963b5f2d |
335 | LttField *ltt_field_element(LttField *f) |
6cd62ccf |
336 | { |
57df94dd |
337 | LttField *nest = NULL; |
338 | |
339 | if(likely(f->field_type->type_class == LTT_ARRAY || |
340 | f->field_type->type_class == LTT_SEQUENCE)) |
341 | nest = f->child[0]; |
6cd62ccf |
342 | |
57df94dd |
343 | return nest; |
6cd62ccf |
344 | } |
345 | |
346 | /***************************************************************************** |
347 | *Function name |
59d7bdf3 |
348 | * ltt_field_member : obtain the field of data members for structure |
6cd62ccf |
349 | *Input params |
350 | * f : a field |
351 | * i : index of member field |
352 | *Return value |
963b5f2d |
353 | * LttField * : the field of the nested element |
6cd62ccf |
354 | ****************************************************************************/ |
355 | |
963b5f2d |
356 | LttField *ltt_field_member(LttField *f, unsigned i) |
6cd62ccf |
357 | { |
57df94dd |
358 | LttField *field_member; |
359 | |
9d239bd9 |
360 | g_assert(f->field_type->type_class == LTT_STRUCT || |
361 | f->field_type->type_class == LTT_UNION); |
362 | g_assert(i < f->field_type->element_number); |
363 | #if 0 |
57df94dd |
364 | if(unlikely( f->field_type->type_class != LTT_STRUCT |
365 | && f->field_type->type_class != LTT_UNION) |
366 | || i >= f->field_type->element_number ) |
367 | field_member = NULL; |
368 | else |
9d239bd9 |
369 | #endif //0 |
370 | field_member = f->child[i]; |
57df94dd |
371 | |
372 | return field_member; |
6cd62ccf |
373 | } |
374 | |
375 | /***************************************************************************** |
376 | *Function name |
377 | * ltt_field_type : obtain the type of the field |
378 | *Input params |
379 | * f : a field |
380 | *Return value |
381 | * ltt_tyoe * : the type of field |
382 | ****************************************************************************/ |
383 | |
963b5f2d |
384 | LttType *ltt_field_type(LttField *f) |
6cd62ccf |
385 | { |
57df94dd |
386 | if(unlikely(!f))return NULL; |
6cd62ccf |
387 | return f->field_type; |
388 | } |
389 | |
a5dcde2f |
390 | int ltt_field_size(LttField * f) |
391 | { |
57df94dd |
392 | if(unlikely(!f))return 0; |
a5dcde2f |
393 | return f->field_size; |
394 | } |