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