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