54ecbf38 |
1 | /* This file is part of the Linux Trace Toolkit viewer |
2 | * Copyright (C) 2003-2004 Xiangxiu Yang, Mathieu Desnoyers |
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 | |
19 | #ifdef HAVE_CONFIG_H |
20 | #include <config.h> |
21 | #endif |
22 | |
23 | #include <stdio.h> |
24 | #include <glib.h> |
25 | |
26 | #include "parser.h" |
27 | #include <ltt/ltt.h> |
28 | #include "ltt-private.h" |
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 | |
35 | typedef enum _intSizesNames { SIZE_INT8, SIZE_INT16, SIZE_INT32, |
36 | SIZE_INT64, SIZE_SHORT, INT_SIZES_NUMBER } |
37 | intSizesNames; |
38 | |
39 | |
40 | static unsigned floatSizes[] = { |
41 | 0, 0, sizeof(float), sizeof(double), 0, sizeof(float), sizeof(double) }; |
42 | |
43 | #define FLOAT_SIZES_NUMBER 7 |
44 | |
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 |
b59cd2f8 |
52 | * GQuark : the name of the event type |
54ecbf38 |
53 | ****************************************************************************/ |
54 | |
b59cd2f8 |
55 | GQuark ltt_eventtype_name(LttEventType *et) |
54ecbf38 |
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 | |
69 | gchar *ltt_eventtype_description(LttEventType *et) |
70 | { |
71 | return et->description; |
72 | } |
73 | |
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 | |
54ecbf38 |
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 |
94 | * unsigned : the id |
95 | ****************************************************************************/ |
96 | |
b59cd2f8 |
97 | guint8 ltt_eventtype_id(LttEventType *et) |
54ecbf38 |
98 | { |
b59cd2f8 |
99 | return et->index; |
54ecbf38 |
100 | } |
101 | |
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 |
108 | * LttType * : the type of the event type |
109 | ****************************************************************************/ |
110 | |
111 | LttType *ltt_eventtype_type(LttEventType *et) |
112 | { |
113 | if(unlikely(!et->root_field)) return NULL; |
114 | else return et->root_field->field_type; |
115 | } |
116 | |
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 | |
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 | |
b59cd2f8 |
140 | GQuark ltt_type_name(LttType *t) |
54ecbf38 |
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 |
151 | * LttTypeEnum : the type class of the type |
152 | ****************************************************************************/ |
153 | |
154 | LttTypeEnum ltt_type_class(LttType *t) |
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 |
b59cd2f8 |
162 | * for primitive types (INT, UINT, FLOAT, ENUM) |
163 | * or the size for the unsigned integer length count for |
164 | * sequences |
54ecbf38 |
165 | *Input params |
166 | * tf : trace file |
167 | * t : a type |
168 | *Return value |
b59cd2f8 |
169 | * : the type size |
54ecbf38 |
170 | * returns 0 if erroneous, and show a critical warning message. |
171 | ****************************************************************************/ |
172 | |
b59cd2f8 |
173 | size_t ltt_type_size(LttTrace * trace, LttType *t) |
54ecbf38 |
174 | { |
b59cd2f8 |
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: |
54ecbf38 |
183 | if(likely(t->size < INT_SIZES_NUMBER)) |
184 | size = intSizes[t->size]; |
b59cd2f8 |
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; |
54ecbf38 |
206 | } |
207 | |
208 | return size; |
b59cd2f8 |
209 | |
210 | |
211 | error: |
212 | g_warning("no size known for the type"); |
213 | return 0; |
54ecbf38 |
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 |
223 | * LttType : the type of nested element of array or sequence |
224 | ****************************************************************************/ |
225 | |
226 | LttType *ltt_type_element_type(LttType *t) |
227 | { |
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; |
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 | |
247 | unsigned ltt_type_element_number(LttType *t) |
248 | { |
249 | unsigned ret = 0; |
250 | |
251 | if(likely(t->type_class == LTT_ARRAY)) |
252 | ret = t->element_number; |
253 | |
254 | return ret; |
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 | |
266 | unsigned ltt_type_member_number(LttType *t) |
267 | { |
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; |
274 | } |
275 | |
276 | /***************************************************************************** |
277 | *Function name |
278 | * ltt_type_member_type : obtain the type of a data member in a structure |
279 | * or union. |
280 | *Input params |
281 | * t : a type |
282 | * i : index of the member |
283 | *Return value |
284 | * LttType * : the type of structure member |
285 | ****************************************************************************/ |
286 | |
287 | LttType *ltt_type_member_type(LttType *t, unsigned i, gchar ** name) |
288 | { |
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 | )) { |
296 | *name = NULL; |
297 | } else { |
298 | *name = t->element_type[i]->element_name; |
299 | member_type = t->element_type[i]; |
300 | } |
301 | |
302 | return member_type; |
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 | |
317 | char *ltt_enum_string_get(LttType *t, unsigned i) |
318 | { |
319 | gchar *string = NULL; |
320 | |
321 | if(likely(t->type_class == LTT_ENUM && i < t->element_number)) |
322 | string = t->enum_strings[i]; |
323 | |
324 | return string; |
325 | } |
326 | |
327 | /***************************************************************************** |
328 | *Function name |
329 | * ltt_field_element : obtain the field of nested elements for arrays and |
330 | * sequence |
331 | *Input params |
332 | * f : a field |
333 | *Return value |
334 | * LttField * : the field of the nested element |
335 | ****************************************************************************/ |
336 | |
337 | LttField *ltt_field_element(LttField *f) |
338 | { |
339 | LttField *nest = NULL; |
340 | |
341 | if(likely(f->field_type->type_class == LTT_ARRAY || |
342 | f->field_type->type_class == LTT_SEQUENCE)) |
343 | nest = f->child[0]; |
344 | |
345 | return nest; |
346 | } |
347 | |
348 | /***************************************************************************** |
349 | *Function name |
350 | * ltt_field_member : obtain the field of data members for structure |
351 | *Input params |
352 | * f : a field |
353 | * i : index of member field |
354 | *Return value |
355 | * LttField * : the field of the nested element |
356 | ****************************************************************************/ |
357 | |
358 | LttField *ltt_field_member(LttField *f, unsigned i) |
359 | { |
360 | LttField *field_member; |
361 | |
362 | if(unlikely( f->field_type->type_class != LTT_STRUCT |
363 | && f->field_type->type_class != LTT_UNION) |
364 | || i >= f->field_type->element_number ) |
365 | field_member = NULL; |
366 | else |
367 | field_member = f->child[i]; |
368 | |
369 | return field_member; |
370 | } |
371 | |
372 | /***************************************************************************** |
373 | *Function name |
374 | * ltt_field_type : obtain the type of the field |
375 | *Input params |
376 | * f : a field |
377 | *Return value |
378 | * ltt_tyoe * : the type of field |
379 | ****************************************************************************/ |
380 | |
381 | LttType *ltt_field_type(LttField *f) |
382 | { |
383 | if(unlikely(!f))return NULL; |
384 | return f->field_type; |
385 | } |
386 | |
387 | int ltt_field_size(LttField * f) |
388 | { |
389 | if(unlikely(!f))return 0; |
390 | return f->field_size; |
391 | } |