apply patch by Gabriel Matni to add format string support for event fields
[lttv.git] / ltt / branches / poly / ltt / type.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2003-2004 Xiangxiu Yang
3 * 2005 Mathieu Desnoyers
4 *
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.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
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.
18 */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <glib.h>
26
27 #include "parser.h"
28 #include <ltt/ltt.h>
29 #include "ltt-private.h"
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
36 static unsigned floatSizes[] = {
37 0, 0, sizeof(float), sizeof(double), 0, sizeof(float), sizeof(double) };
38
39
40 typedef enum _intSizesNames { SIZE_INT8, SIZE_INT16, SIZE_INT32,
41 SIZE_INT64, SIZE_SHORT, INT_SIZES_NUMBER }
42 intSizesNames;
43
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" };
48
49
50 #define FLOAT_SIZES_NUMBER 7
51
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
59 * GQuark : the name of the event type
60 ****************************************************************************/
61
62 GQuark ltt_eventtype_name(LttEventType *et)
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
76 gchar *ltt_eventtype_description(LttEventType *et)
77 {
78 return et->description;
79 }
80
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
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
101 * unsigned : the id
102 ****************************************************************************/
103
104 guint8 ltt_eventtype_id(LttEventType *et)
105 {
106 return et->index;
107 }
108
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
123 /*****************************************************************************
124 *Function name
125 * ltt_field_name : get the name of the field
126 *Input params
127 * f : a field
128 *Return value
129 * char * : the name of the type
130 ****************************************************************************/
131
132 GQuark ltt_field_name(LttField *f)
133 {
134 return f->name;
135 }
136
137
138
139 /*****************************************************************************
140 *Function name
141 * ltt_type_class : get the type class of the type
142 *Input params
143 * t : a type
144 *Return value
145 * LttTypeEnum : the type class of the type
146 ****************************************************************************/
147
148 LttTypeEnum ltt_type_class(LttType *t)
149 {
150 return t->type_class;
151 }
152
153 /*****************************************************************************
154 *Function name
155 * ltt_type_size : obtain the type size. The size is the number of bytes
156 * for primitive types (INT, UINT, FLOAT, ENUM)
157 * or the size for the unsigned integer length count for
158 * sequences
159 *Input params
160 * tf : trace file
161 * t : a type
162 *Return value
163 * : the type size
164 * returns 0 if erroneous, and show a critical warning message.
165 ****************************************************************************/
166
167 guint ltt_type_size(LttTrace * trace, LttType *t)
168 {
169 guint size;
170
171 switch(t->type_class) {
172 case LTT_INT_FIXED:
173 case LTT_UINT_FIXED:
174 case LTT_CHAR:
175 case LTT_UCHAR:
176 case LTT_SHORT:
177 case LTT_USHORT:
178 case LTT_INT:
179 case LTT_UINT:
180 case LTT_ENUM:
181 if(likely(t->size < INT_SIZES_NUMBER))
182 size = intSizes[t->size];
183 else
184 goto error;
185 break;
186 case LTT_FLOAT:
187 if(likely(t->size < FLOAT_SIZES_NUMBER))
188 size = floatSizes[t->size];
189 else
190 goto error;
191 break;
192 case LTT_POINTER:
193 case LTT_LONG:
194 case LTT_ULONG:
195 case LTT_SIZE_T:
196 case LTT_SSIZE_T:
197 case LTT_SEQUENCE:
198 case LTT_OFF_T:
199 case LTT_STRING:
200 case LTT_ARRAY:
201 case LTT_STRUCT:
202 case LTT_UNION:
203 case LTT_NONE:
204 goto error;
205 break;
206 }
207
208 return size;
209
210
211 error:
212 g_warning("no size known for the type");
213 return 0;
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 LttField *field;
230
231 if(unlikely(t->type_class != LTT_ARRAY && t->type_class != LTT_SEQUENCE))
232 element_type = NULL;
233 else {
234 if(t->type_class == LTT_ARRAY)
235 field = &g_array_index(t->fields, LttField, 0);
236 else
237 field = &g_array_index(t->fields, LttField, 1);
238 element_type = ltt_field_type(field);
239 }
240
241 return element_type;
242 }
243
244 /*****************************************************************************
245 *Function name
246 * ltt_type_element_number : obtain the number of elements for enums
247 *Input params
248 * t : a type
249 *Return value
250 * unsigned : the number of elements for arrays
251 ****************************************************************************/
252 unsigned ltt_type_element_number(LttType *t)
253 {
254 unsigned ret = 0;
255
256 if(likely(t->type_class == LTT_ENUM))
257 // Permits non full enums ret = g_hash_table_size(t->enum_map);
258 ret = (unsigned)(t->highest_value - t->lowest_value);
259
260 return ret;
261 }
262
263 /*****************************************************************************
264 *Function name
265 * ltt_type_member_number : obtain the number of data members for structure
266 *Input params
267 * t : a type
268 *Return value
269 * unsigned : the number of members for structure
270 ****************************************************************************/
271
272 unsigned ltt_type_member_number(LttType *t)
273 {
274 unsigned ret = 0;
275
276 if(likely(t->type_class == LTT_STRUCT || t->type_class == LTT_UNION))
277 ret = t->fields->len;
278
279 return ret;
280 }
281
282
283 /*****************************************************************************
284 *Function name
285 * ltt_enum_string_get : for enumerations, obtain the symbolic string
286 * associated with a value (0 to n - 1 for an
287 * enumeration of n elements)
288 *Input params
289 * t : a type
290 * i : index of the member
291 *Return value
292 * char * : symbolic string associated with a value
293 ****************************************************************************/
294
295 GQuark ltt_enum_string_get(LttType *t, gulong i)
296 {
297 if(likely(t->type_class == LTT_ENUM))
298 return (GQuark)g_hash_table_lookup(t->enum_map, (gpointer)i);
299 else
300 return 0;
301 }
302 #if 0
303 /*****************************************************************************
304 *Function name
305 * ltt_field_element : obtain the field of nested elements for arrays and
306 * sequence
307 *Input params
308 * f : a field
309 *Return value
310 * LttField * : the field of the nested element
311 ****************************************************************************/
312
313 LttField *ltt_field_element(LttField *f)
314 {
315 LttField *nest = NULL;
316
317 if(likely(f->field_type->type_class == LTT_ARRAY ||
318 f->field_type->type_class == LTT_SEQUENCE))
319 nest = f->child[0];
320
321 return nest;
322 }
323 #endif//0
324
325 /*****************************************************************************
326 *Function name
327 * ltt_field_member_by_name : obtain the field of data members for structure
328 *Input params
329 * f : a field
330 * name : name of the field
331 *Return value
332 * LttField * : the field of the nested element
333 ****************************************************************************/
334
335 LttField *ltt_field_member_by_name(LttField *f, GQuark name)
336 {
337 LttField *field_member;
338
339 g_assert(f->field_type.type_class == LTT_STRUCT ||
340 f->field_type.type_class == LTT_UNION);
341
342 field_member = g_datalist_id_get_data(&f->field_type.fields_by_name, name);
343
344 return field_member;
345 }
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, guint i)
359 {
360 LttField *field_member;
361
362 g_assert(f->field_type.type_class == LTT_STRUCT ||
363 f->field_type.type_class == LTT_UNION);
364 g_assert(i < f->field_type.fields->len);
365
366 field_member = &g_array_index(f->field_type.fields, LttField, i);
367
368 return field_member;
369 }
370
371 /*****************************************************************************
372 *Function name
373 * ltt_field_type : obtain the type of the field
374 *Input params
375 * f : a field
376 *Return value
377 * ltt_tyoe * : the type of field
378 ****************************************************************************/
379
380 LttType *ltt_field_type(LttField *f)
381 {
382 if(unlikely(!f))return NULL;
383 return &f->field_type;
384 }
385
386 int ltt_field_size(LttField * f)
387 {
388 if(unlikely(!f))return 0;
389 return f->field_size;
390 }
391
392
393 /*****************************************************************************
394 *Function name
395 * ltt_eventtype_num_fields : get the number of fields of the event
396 *Input params
397 * e : an instance of an event type
398 *Return value
399 * guint : number of fields
400 ****************************************************************************/
401
402 guint ltt_eventtype_num_fields(LttEventType *event_type)
403 {
404 if(unlikely(!event_type)) return 0;
405
406 return event_type->fields->len;
407
408 }
409 /*****************************************************************************
410 *Function name
411 * ltt_eventtype_field : get the i th field of the event
412 *Input params
413 * e : an instance of an event type
414 * i : field index
415 *Return value
416 * LttField * : The requested field, or NULL
417 ****************************************************************************/
418
419 LttField *ltt_eventtype_field(LttEventType *event_type, guint i)
420 {
421 if(unlikely(!event_type)) return NULL;
422
423 if(i >= event_type->fields->len) return NULL;
424
425 return &g_array_index(event_type->fields, LttField, i);
426
427 }
428
429 /*****************************************************************************
430 *Function name
431 * ltt_eventtype_field_by_name : get a field of the event
432 *Input params
433 * e : an instance of an event type
434 * name : field name
435 *Return value
436 * LttField * : The requested field, or NULL
437 ****************************************************************************/
438
439 LttField *ltt_eventtype_field_by_name(LttEventType *event_type, GQuark name)
440 {
441 if(unlikely(!event_type)) return NULL;
442
443 return (LttField*)g_datalist_id_get_data(&event_type->fields_by_name, name);
444
445 }
446
447
This page took 0.039919 seconds and 4 git commands to generate.