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