| 1 | /* This file is part of the Linux Trace Toolkit viewer |
| 2 | * Copyright (C) 2005 Mathieu Desnoyers |
| 3 | * |
| 4 | * Complete rewrite from the original version made by XangXiu Yang. |
| 5 | * |
| 6 | * This library is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU Lesser General Public |
| 8 | * License Version 2.1 as published by the Free Software Foundation. |
| 9 | * |
| 10 | * This library is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU Lesser General Public |
| 16 | * License along with this library; if not, write to the |
| 17 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 18 | * Boston, MA 02111-1307, USA. |
| 19 | */ |
| 20 | |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include <config.h> |
| 23 | #endif |
| 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <fcntl.h> |
| 27 | #include <string.h> |
| 28 | #include <dirent.h> |
| 29 | #include <sys/stat.h> |
| 30 | #include <sys/types.h> |
| 31 | #include <errno.h> |
| 32 | #include <unistd.h> |
| 33 | #include <math.h> |
| 34 | #include <glib.h> |
| 35 | #include <malloc.h> |
| 36 | #include <sys/mman.h> |
| 37 | |
| 38 | // For realpath |
| 39 | #include <limits.h> |
| 40 | #include <stdlib.h> |
| 41 | |
| 42 | |
| 43 | #include "parser.h" |
| 44 | #include <ltt/ltt.h> |
| 45 | #include "ltt-private.h" |
| 46 | #include <ltt/trace.h> |
| 47 | #include <ltt/facility.h> |
| 48 | #include <ltt/event.h> |
| 49 | #include <ltt/type.h> |
| 50 | #include <ltt/ltt-types.h> |
| 51 | |
| 52 | |
| 53 | /* Facility names used in this file */ |
| 54 | |
| 55 | GQuark LTT_FACILITY_NAME_HEARTBEAT, |
| 56 | LTT_EVENT_NAME_HEARTBEAT; |
| 57 | GQuark LTT_TRACEFILE_NAME_FACILITIES; |
| 58 | |
| 59 | #ifndef g_open |
| 60 | #define g_open open |
| 61 | #endif |
| 62 | |
| 63 | |
| 64 | #define __UNUSED__ __attribute__((__unused__)) |
| 65 | |
| 66 | #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format) |
| 67 | |
| 68 | #ifndef g_debug |
| 69 | #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format) |
| 70 | #endif |
| 71 | |
| 72 | #define g_close close |
| 73 | |
| 74 | /* Those macros must be called from within a function where page_size is a known |
| 75 | * variable */ |
| 76 | #define PAGE_MASK (~(page_size-1)) |
| 77 | #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK) |
| 78 | |
| 79 | /* set the offset of the fields belonging to the event, |
| 80 | need the information of the archecture */ |
| 81 | //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type); |
| 82 | //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data); |
| 83 | |
| 84 | /* get the size of the field type according to |
| 85 | * The facility size information. */ |
| 86 | #if 0 |
| 87 | static inline void preset_field_type_size(LttTracefile *tf, |
| 88 | LttEventType *event_type, |
| 89 | off_t offset_root, off_t offset_parent, |
| 90 | enum field_status *fixed_root, enum field_status *fixed_parent, |
| 91 | LttField *field); |
| 92 | #endif //0 |
| 93 | |
| 94 | /* map a fixed size or a block information from the file (fd) */ |
| 95 | static gint map_block(LttTracefile * tf, guint block_num); |
| 96 | |
| 97 | /* calculate nsec per cycles for current block */ |
| 98 | #if 0 |
| 99 | static guint32 calc_nsecs_per_cycle(LttTracefile * t); |
| 100 | static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles); |
| 101 | #endif //0 |
| 102 | |
| 103 | /* go to the next event */ |
| 104 | static int ltt_seek_next_event(LttTracefile *tf); |
| 105 | |
| 106 | void ltt_update_event_size(LttTracefile *tf); |
| 107 | |
| 108 | |
| 109 | void precompute_offsets(LttFacility *fac, LttEventType *event); |
| 110 | |
| 111 | #if 0 |
| 112 | /* Functions to parse system.xml file (using glib xml parser) */ |
| 113 | static void parser_start_element (GMarkupParseContext __UNUSED__ *context, |
| 114 | const gchar *element_name, |
| 115 | const gchar **attribute_names, |
| 116 | const gchar **attribute_values, |
| 117 | gpointer user_data, |
| 118 | GError **error) |
| 119 | { |
| 120 | int i=0; |
| 121 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
| 122 | if(strcmp("system", element_name)){ |
| 123 | *error = g_error_new(G_MARKUP_ERROR, |
| 124 | G_LOG_LEVEL_WARNING, |
| 125 | "This is not system.xml file"); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | while(attribute_names[i]){ |
| 130 | if(strcmp("node_name", attribute_names[i])==0){ |
| 131 | des->node_name = g_strdup(attribute_values[i]); |
| 132 | }else if(strcmp("domainname", attribute_names[i])==0){ |
| 133 | des->domain_name = g_strdup(attribute_values[i]); |
| 134 | }else if(strcmp("cpu", attribute_names[i])==0){ |
| 135 | des->nb_cpu = atoi(attribute_values[i]); |
| 136 | }else if(strcmp("arch_size", attribute_names[i])==0){ |
| 137 | if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32; |
| 138 | else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32; |
| 139 | else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64; |
| 140 | else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64; |
| 141 | else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN; |
| 142 | }else if(strcmp("endian", attribute_names[i])==0){ |
| 143 | if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0) |
| 144 | des->endian = LTT_LITTLE_ENDIAN; |
| 145 | else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0) |
| 146 | des->endian = LTT_BIG_ENDIAN; |
| 147 | }else if(strcmp("kernel_name", attribute_names[i])==0){ |
| 148 | des->kernel_name = g_strdup(attribute_values[i]); |
| 149 | }else if(strcmp("kernel_release", attribute_names[i])==0){ |
| 150 | des->kernel_release = g_strdup(attribute_values[i]); |
| 151 | }else if(strcmp("kernel_version", attribute_names[i])==0){ |
| 152 | des->kernel_version = g_strdup(attribute_values[i]); |
| 153 | }else if(strcmp("machine", attribute_names[i])==0){ |
| 154 | des->machine = g_strdup(attribute_values[i]); |
| 155 | }else if(strcmp("processor", attribute_names[i])==0){ |
| 156 | des->processor = g_strdup(attribute_values[i]); |
| 157 | }else if(strcmp("hardware_platform", attribute_names[i])==0){ |
| 158 | des->hardware_platform = g_strdup(attribute_values[i]); |
| 159 | }else if(strcmp("operating_system", attribute_names[i])==0){ |
| 160 | des->operating_system = g_strdup(attribute_values[i]); |
| 161 | }else if(strcmp("ltt_major_version", attribute_names[i])==0){ |
| 162 | des->ltt_major_version = atoi(attribute_values[i]); |
| 163 | }else if(strcmp("ltt_minor_version", attribute_names[i])==0){ |
| 164 | des->ltt_minor_version = atoi(attribute_values[i]); |
| 165 | }else if(strcmp("ltt_block_size", attribute_names[i])==0){ |
| 166 | des->ltt_block_size = atoi(attribute_values[i]); |
| 167 | }else{ |
| 168 | *error = g_error_new(G_MARKUP_ERROR, |
| 169 | G_LOG_LEVEL_WARNING, |
| 170 | "Not a valid attribute"); |
| 171 | return; |
| 172 | } |
| 173 | i++; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | static void parser_characters (GMarkupParseContext __UNUSED__ *context, |
| 178 | const gchar *text, |
| 179 | gsize __UNUSED__ text_len, |
| 180 | gpointer user_data, |
| 181 | GError __UNUSED__ **error) |
| 182 | { |
| 183 | LttSystemDescription* des = (LttSystemDescription* )user_data; |
| 184 | des->description = g_strdup(text); |
| 185 | } |
| 186 | #endif //0 |
| 187 | |
| 188 | |
| 189 | LttFacility *ltt_trace_get_facility_by_num(LttTrace *t, |
| 190 | guint num) |
| 191 | { |
| 192 | g_assert(num < t->facilities_by_num->len); |
| 193 | |
| 194 | return &g_array_index(t->facilities_by_num, LttFacility, num); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | guint ltt_trace_get_num_cpu(LttTrace *t) |
| 199 | { |
| 200 | return t->num_cpu; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | /* trace can be NULL |
| 205 | * |
| 206 | * Return value : 0 success, 1 bad tracefile |
| 207 | */ |
| 208 | int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t) |
| 209 | { |
| 210 | guint32 *magic_number = (guint32*)header; |
| 211 | struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header; |
| 212 | |
| 213 | if(*magic_number == LTT_MAGIC_NUMBER) |
| 214 | tf->reverse_bo = 0; |
| 215 | else if(*magic_number == LTT_REV_MAGIC_NUMBER) |
| 216 | tf->reverse_bo = 1; |
| 217 | else /* invalid magic number, bad tracefile ! */ |
| 218 | return 1; |
| 219 | |
| 220 | /* Get float byte order : might be different from int byte order |
| 221 | * (or is set to 0 if the trace has no float (kernel trace)) */ |
| 222 | tf->float_word_order = any->float_word_order; |
| 223 | tf->has_alignment = any->has_alignment; |
| 224 | |
| 225 | if(t) { |
| 226 | t->arch_type = ltt_get_uint32(LTT_GET_BO(tf), |
| 227 | &any->arch_type); |
| 228 | t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf), |
| 229 | &any->arch_variant); |
| 230 | t->arch_size = any->arch_size; |
| 231 | t->ltt_major_version = any->major_version; |
| 232 | t->ltt_minor_version = any->minor_version; |
| 233 | t->flight_recorder = any->flight_recorder; |
| 234 | t->has_heartbeat = any->has_heartbeat; |
| 235 | t->freq_scale = any->freq_scale; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | switch(any->major_version) { |
| 240 | |
| 241 | case 0: |
| 242 | switch(any->minor_version) { |
| 243 | case 3: |
| 244 | { |
| 245 | tf->buffer_header_size = |
| 246 | sizeof(struct ltt_block_start_header) |
| 247 | + sizeof(struct ltt_trace_header_0_3); |
| 248 | g_warning("Unsupported trace version : %hhu.%hhu", |
| 249 | any->major_version, any->minor_version); |
| 250 | return 1; |
| 251 | } |
| 252 | break; |
| 253 | case 7: |
| 254 | { |
| 255 | struct ltt_trace_header_0_7 *vheader = |
| 256 | (struct ltt_trace_header_0_7 *)header; |
| 257 | tf->buffer_header_size = |
| 258 | sizeof(struct ltt_block_start_header) |
| 259 | + sizeof(struct ltt_trace_header_0_7); |
| 260 | if(t) { |
| 261 | t->start_freq = ltt_get_uint64(LTT_GET_BO(tf), |
| 262 | &vheader->start_freq); |
| 263 | t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf), |
| 264 | &vheader->start_tsc); |
| 265 | t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf), |
| 266 | &vheader->start_monotonic); |
| 267 | t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf), |
| 268 | &vheader->start_time_sec); |
| 269 | t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf), |
| 270 | &vheader->start_time_usec); |
| 271 | t->start_time.tv_nsec *= 1000; /* microsec to nanosec */ |
| 272 | |
| 273 | t->start_time_from_tsc = ltt_time_from_uint64( |
| 274 | (double)t->start_tsc * 1000000.0 / (double)t->start_freq); |
| 275 | } |
| 276 | } |
| 277 | break; |
| 278 | default: |
| 279 | g_warning("Unsupported trace version : %hhu.%hhu", |
| 280 | any->major_version, any->minor_version); |
| 281 | return 1; |
| 282 | } |
| 283 | break; |
| 284 | |
| 285 | default: |
| 286 | g_warning("Unsupported trace version : %hhu.%hhu", |
| 287 | any->major_version, any->minor_version); |
| 288 | return 1; |
| 289 | } |
| 290 | |
| 291 | |
| 292 | return 0; |
| 293 | } |
| 294 | |
| 295 | |
| 296 | |
| 297 | /***************************************************************************** |
| 298 | *Function name |
| 299 | * ltt_tracefile_open : open a trace file, construct a LttTracefile |
| 300 | *Input params |
| 301 | * t : the trace containing the tracefile |
| 302 | * fileName : path name of the trace file |
| 303 | * tf : the tracefile structure |
| 304 | *Return value |
| 305 | * : 0 for success, -1 otherwise. |
| 306 | ****************************************************************************/ |
| 307 | |
| 308 | gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf) |
| 309 | { |
| 310 | struct stat lTDFStat; /* Trace data file status */ |
| 311 | struct ltt_block_start_header *header; |
| 312 | int page_size = getpagesize(); |
| 313 | |
| 314 | //open the file |
| 315 | tf->long_name = g_quark_from_string(fileName); |
| 316 | tf->trace = t; |
| 317 | tf->fd = open(fileName, O_RDONLY); |
| 318 | if(tf->fd < 0){ |
| 319 | g_warning("Unable to open input data file %s\n", fileName); |
| 320 | goto end; |
| 321 | } |
| 322 | |
| 323 | // Get the file's status |
| 324 | if(fstat(tf->fd, &lTDFStat) < 0){ |
| 325 | g_warning("Unable to get the status of the input data file %s\n", fileName); |
| 326 | goto close_file; |
| 327 | } |
| 328 | |
| 329 | // Is the file large enough to contain a trace |
| 330 | if(lTDFStat.st_size < |
| 331 | (off_t)(sizeof(struct ltt_block_start_header) |
| 332 | + sizeof(struct ltt_trace_header_any))){ |
| 333 | g_print("The input data file %s does not contain a trace\n", fileName); |
| 334 | goto close_file; |
| 335 | } |
| 336 | |
| 337 | /* Temporarily map the buffer start header to get trace information */ |
| 338 | /* Multiple of pages aligned head */ |
| 339 | tf->buffer.head = mmap(0, |
| 340 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
| 341 | + sizeof(struct ltt_trace_header_any)), PROT_READ, |
| 342 | MAP_PRIVATE, tf->fd, 0); |
| 343 | if(tf->buffer.head == MAP_FAILED) { |
| 344 | perror("Error in allocating memory for buffer of tracefile"); |
| 345 | goto close_file; |
| 346 | } |
| 347 | g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
| 348 | |
| 349 | header = (struct ltt_block_start_header*)tf->buffer.head; |
| 350 | |
| 351 | if(parse_trace_header(header->trace, tf, NULL)) { |
| 352 | g_warning("parse_trace_header error"); |
| 353 | goto unmap_file; |
| 354 | } |
| 355 | |
| 356 | //store the size of the file |
| 357 | tf->file_size = lTDFStat.st_size; |
| 358 | tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size); |
| 359 | tf->num_blocks = tf->file_size / tf->buf_size; |
| 360 | |
| 361 | if(munmap(tf->buffer.head, |
| 362 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
| 363 | + sizeof(struct ltt_trace_header_any)))) { |
| 364 | g_warning("unmap size : %u\n", |
| 365 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
| 366 | + sizeof(struct ltt_trace_header_any))); |
| 367 | perror("munmap error"); |
| 368 | g_assert(0); |
| 369 | } |
| 370 | tf->buffer.head = NULL; |
| 371 | |
| 372 | //read the first block |
| 373 | if(map_block(tf,0)) { |
| 374 | perror("Cannot map block for tracefile"); |
| 375 | goto close_file; |
| 376 | } |
| 377 | |
| 378 | return 0; |
| 379 | |
| 380 | /* Error */ |
| 381 | unmap_file: |
| 382 | if(munmap(tf->buffer.head, |
| 383 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
| 384 | + sizeof(struct ltt_trace_header_any)))) { |
| 385 | g_warning("unmap size : %u\n", |
| 386 | PAGE_ALIGN(sizeof(struct ltt_block_start_header) |
| 387 | + sizeof(struct ltt_trace_header_any))); |
| 388 | perror("munmap error"); |
| 389 | g_assert(0); |
| 390 | } |
| 391 | close_file: |
| 392 | close(tf->fd); |
| 393 | end: |
| 394 | return -1; |
| 395 | } |
| 396 | |
| 397 | LttTrace *ltt_tracefile_get_trace(LttTracefile *tf) |
| 398 | { |
| 399 | return tf->trace; |
| 400 | } |
| 401 | |
| 402 | #if 0 |
| 403 | /***************************************************************************** |
| 404 | *Open control and per cpu tracefiles |
| 405 | ****************************************************************************/ |
| 406 | |
| 407 | void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name) |
| 408 | { |
| 409 | LttTracefile * tf; |
| 410 | tf = ltt_tracefile_open(t,tracefile_name); |
| 411 | if(!tf) return; |
| 412 | t->per_cpu_tracefile_number++; |
| 413 | g_ptr_array_add(t->per_cpu_tracefiles, tf); |
| 414 | } |
| 415 | |
| 416 | gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name) |
| 417 | { |
| 418 | LttTracefile * tf; |
| 419 | LttEvent ev; |
| 420 | LttFacility * f; |
| 421 | void * pos; |
| 422 | FacilityLoad fLoad; |
| 423 | unsigned int i; |
| 424 | |
| 425 | tf = ltt_tracefile_open(t,control_name); |
| 426 | if(!tf) { |
| 427 | g_warning("ltt_tracefile_open_control : bad file descriptor"); |
| 428 | return -1; |
| 429 | } |
| 430 | t->control_tracefile_number++; |
| 431 | g_ptr_array_add(t->control_tracefiles,tf); |
| 432 | |
| 433 | //parse facilities tracefile to get base_id |
| 434 | if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){ |
| 435 | while(1){ |
| 436 | if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file |
| 437 | |
| 438 | if(ev.event_id == TRACE_FACILITY_LOAD){ |
| 439 | pos = ev.data; |
| 440 | fLoad.name = (gchar*)pos; |
| 441 | fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name)); |
| 442 | fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum)); |
| 443 | |
| 444 | for(i=0;i<t->facility_number;i++){ |
| 445 | f = (LttFacility*)g_ptr_array_index(t->facilities,i); |
| 446 | if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){ |
| 447 | f->base_id = fLoad.base_code; |
| 448 | break; |
| 449 | } |
| 450 | } |
| 451 | if(i==t->facility_number) { |
| 452 | g_warning("Facility: %s, checksum: %u is not found", |
| 453 | fLoad.name,(unsigned int)fLoad.checksum); |
| 454 | return -1; |
| 455 | } |
| 456 | }else if(ev.event_id == TRACE_BLOCK_START){ |
| 457 | continue; |
| 458 | }else if(ev.event_id == TRACE_BLOCK_END){ |
| 459 | break; |
| 460 | }else { |
| 461 | g_warning("Not valid facilities trace file"); |
| 462 | return -1; |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | return 0; |
| 467 | } |
| 468 | #endif //0 |
| 469 | |
| 470 | /***************************************************************************** |
| 471 | *Function name |
| 472 | * ltt_tracefile_close: close a trace file, |
| 473 | *Input params |
| 474 | * t : tracefile which will be closed |
| 475 | ****************************************************************************/ |
| 476 | |
| 477 | void ltt_tracefile_close(LttTracefile *t) |
| 478 | { |
| 479 | int page_size = getpagesize(); |
| 480 | |
| 481 | if(t->buffer.head != NULL) |
| 482 | if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) { |
| 483 | g_warning("unmap size : %u\n", |
| 484 | PAGE_ALIGN(t->buf_size)); |
| 485 | perror("munmap error"); |
| 486 | g_assert(0); |
| 487 | } |
| 488 | |
| 489 | close(t->fd); |
| 490 | } |
| 491 | |
| 492 | |
| 493 | /***************************************************************************** |
| 494 | *Get system information |
| 495 | ****************************************************************************/ |
| 496 | #if 0 |
| 497 | gint getSystemInfo(LttSystemDescription* des, gchar * pathname) |
| 498 | { |
| 499 | int fd; |
| 500 | GIOChannel *iochan; |
| 501 | gchar *buf = NULL; |
| 502 | gsize length; |
| 503 | |
| 504 | GMarkupParseContext * context; |
| 505 | GError * error = NULL; |
| 506 | GMarkupParser markup_parser = |
| 507 | { |
| 508 | parser_start_element, |
| 509 | NULL, |
| 510 | parser_characters, |
| 511 | NULL, /* passthrough */ |
| 512 | NULL /* error */ |
| 513 | }; |
| 514 | |
| 515 | fd = g_open(pathname, O_RDONLY, 0); |
| 516 | if(fd == -1){ |
| 517 | g_warning("Can not open file : %s\n", pathname); |
| 518 | return -1; |
| 519 | } |
| 520 | |
| 521 | iochan = g_io_channel_unix_new(fd); |
| 522 | |
| 523 | context = g_markup_parse_context_new(&markup_parser, 0, des,NULL); |
| 524 | |
| 525 | //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){ |
| 526 | while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error) |
| 527 | != G_IO_STATUS_EOF) { |
| 528 | |
| 529 | if(error != NULL) { |
| 530 | g_warning("Can not read xml file: \n%s\n", error->message); |
| 531 | g_error_free(error); |
| 532 | } |
| 533 | if(!g_markup_parse_context_parse(context, buf, length, &error)){ |
| 534 | if(error != NULL) { |
| 535 | g_warning("Can not parse xml file: \n%s\n", error->message); |
| 536 | g_error_free(error); |
| 537 | } |
| 538 | g_markup_parse_context_free(context); |
| 539 | |
| 540 | g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */ |
| 541 | if(error != NULL) { |
| 542 | g_warning("Can not close file: \n%s\n", error->message); |
| 543 | g_error_free(error); |
| 544 | } |
| 545 | |
| 546 | close(fd); |
| 547 | return -1; |
| 548 | } |
| 549 | } |
| 550 | g_markup_parse_context_free(context); |
| 551 | |
| 552 | g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */ |
| 553 | if(error != NULL) { |
| 554 | g_warning("Can not close file: \n%s\n", error->message); |
| 555 | g_error_free(error); |
| 556 | } |
| 557 | |
| 558 | g_close(fd); |
| 559 | |
| 560 | g_free(buf); |
| 561 | return 0; |
| 562 | } |
| 563 | #endif //0 |
| 564 | |
| 565 | /***************************************************************************** |
| 566 | *The following functions get facility/tracefile information |
| 567 | ****************************************************************************/ |
| 568 | #if 0 |
| 569 | gint getFacilityInfo(LttTrace *t, gchar* eventdefs) |
| 570 | { |
| 571 | GDir * dir; |
| 572 | const gchar * name; |
| 573 | unsigned int i,j; |
| 574 | LttFacility * f; |
| 575 | LttEventType * et; |
| 576 | gchar fullname[DIR_NAME_SIZE]; |
| 577 | GError * error = NULL; |
| 578 | |
| 579 | dir = g_dir_open(eventdefs, 0, &error); |
| 580 | |
| 581 | if(error != NULL) { |
| 582 | g_warning("Can not open directory: %s, %s\n", eventdefs, error->message); |
| 583 | g_error_free(error); |
| 584 | return -1; |
| 585 | } |
| 586 | |
| 587 | while((name = g_dir_read_name(dir)) != NULL){ |
| 588 | if(!g_pattern_match_simple("*.xml", name)) continue; |
| 589 | strcpy(fullname,eventdefs); |
| 590 | strcat(fullname,name); |
| 591 | ltt_facility_open(t,fullname); |
| 592 | } |
| 593 | g_dir_close(dir); |
| 594 | |
| 595 | for(j=0;j<t->facility_number;j++){ |
| 596 | f = (LttFacility*)g_ptr_array_index(t->facilities, j); |
| 597 | for(i=0; i<f->event_number; i++){ |
| 598 | et = f->events[i]; |
| 599 | setFieldsOffset(NULL, et, NULL, t); |
| 600 | } |
| 601 | } |
| 602 | return 0; |
| 603 | } |
| 604 | #endif //0 |
| 605 | |
| 606 | /***************************************************************************** |
| 607 | *A trace is specified as a pathname to the directory containing all the |
| 608 | *associated data (control tracefiles, per cpu tracefiles, event |
| 609 | *descriptions...). |
| 610 | * |
| 611 | *When a trace is closed, all the associated facilities, types and fields |
| 612 | *are released as well. |
| 613 | */ |
| 614 | |
| 615 | |
| 616 | /**************************************************************************** |
| 617 | * get_absolute_pathname |
| 618 | * |
| 619 | * return the unique pathname in the system |
| 620 | * |
| 621 | * MD : Fixed this function so it uses realpath, dealing well with |
| 622 | * forgotten cases (.. were not used correctly before). |
| 623 | * |
| 624 | ****************************************************************************/ |
| 625 | void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname) |
| 626 | { |
| 627 | abs_pathname[0] = '\0'; |
| 628 | |
| 629 | if ( realpath (pathname, abs_pathname) != NULL) |
| 630 | return; |
| 631 | else |
| 632 | { |
| 633 | /* error, return the original path unmodified */ |
| 634 | strcpy(abs_pathname, pathname); |
| 635 | return; |
| 636 | } |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | /* Search for something like : .*_.* |
| 641 | * |
| 642 | * The left side is the name, the right side is the number. |
| 643 | */ |
| 644 | |
| 645 | int get_tracefile_name_number(gchar *raw_name, |
| 646 | GQuark *name, |
| 647 | guint *num, |
| 648 | guint *tid, |
| 649 | guint *pgid, |
| 650 | guint64 *creation) |
| 651 | { |
| 652 | guint raw_name_len = strlen(raw_name); |
| 653 | gchar char_name[PATH_MAX]; |
| 654 | int i; |
| 655 | int underscore_pos; |
| 656 | long int cpu_num; |
| 657 | gchar *endptr; |
| 658 | gchar *tmpptr; |
| 659 | |
| 660 | for(i=raw_name_len-1;i>=0;i--) { |
| 661 | if(raw_name[i] == '_') break; |
| 662 | } |
| 663 | if(i==-1) { /* Either not found or name length is 0 */ |
| 664 | /* This is a userspace tracefile */ |
| 665 | strncpy(char_name, raw_name, raw_name_len); |
| 666 | *name = g_quark_from_string(char_name); |
| 667 | *num = 0; /* unknown cpu */ |
| 668 | for(i=0;i<raw_name_len;i++) { |
| 669 | if(raw_name[i] == '/') { |
| 670 | break; |
| 671 | } |
| 672 | } |
| 673 | i++; |
| 674 | for(;i<raw_name_len;i++) { |
| 675 | if(raw_name[i] == '/') { |
| 676 | break; |
| 677 | } |
| 678 | } |
| 679 | i++; |
| 680 | for(;i<raw_name_len;i++) { |
| 681 | if(raw_name[i] == '-') { |
| 682 | break; |
| 683 | } |
| 684 | } |
| 685 | if(i == raw_name_len) return -1; |
| 686 | i++; |
| 687 | tmpptr = &raw_name[i]; |
| 688 | for(;i<raw_name_len;i++) { |
| 689 | if(raw_name[i] == '.') { |
| 690 | raw_name[i] = ' '; |
| 691 | break; |
| 692 | } |
| 693 | } |
| 694 | *tid = strtoul(tmpptr, &endptr, 10); |
| 695 | if(endptr == tmpptr) |
| 696 | return -1; /* No digit */ |
| 697 | if(*tid == ULONG_MAX) |
| 698 | return -1; /* underflow / overflow */ |
| 699 | i++; |
| 700 | tmpptr = &raw_name[i]; |
| 701 | for(;i<raw_name_len;i++) { |
| 702 | if(raw_name[i] == '.') { |
| 703 | raw_name[i] = ' '; |
| 704 | break; |
| 705 | } |
| 706 | } |
| 707 | *pgid = strtoul(tmpptr, &endptr, 10); |
| 708 | if(endptr == tmpptr) |
| 709 | return -1; /* No digit */ |
| 710 | if(*pgid == ULONG_MAX) |
| 711 | return -1; /* underflow / overflow */ |
| 712 | i++; |
| 713 | tmpptr = &raw_name[i]; |
| 714 | *creation = strtoull(tmpptr, &endptr, 10); |
| 715 | if(endptr == tmpptr) |
| 716 | return -1; /* No digit */ |
| 717 | if(*creation == G_MAXUINT64) |
| 718 | return -1; /* underflow / overflow */ |
| 719 | } else { |
| 720 | underscore_pos = i; |
| 721 | |
| 722 | cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10); |
| 723 | |
| 724 | if(endptr == raw_name+underscore_pos+1) |
| 725 | return -1; /* No digit */ |
| 726 | if(cpu_num == LONG_MIN || cpu_num == LONG_MAX) |
| 727 | return -1; /* underflow / overflow */ |
| 728 | |
| 729 | strncpy(char_name, raw_name, underscore_pos); |
| 730 | char_name[underscore_pos] = '\0'; |
| 731 | |
| 732 | *name = g_quark_from_string(char_name); |
| 733 | *num = cpu_num; |
| 734 | } |
| 735 | |
| 736 | |
| 737 | return 0; |
| 738 | } |
| 739 | |
| 740 | |
| 741 | GData **ltt_trace_get_tracefiles_groups(LttTrace *trace) |
| 742 | { |
| 743 | return &trace->tracefiles; |
| 744 | } |
| 745 | |
| 746 | |
| 747 | void compute_tracefile_group(GQuark key_id, |
| 748 | GArray *group, |
| 749 | struct compute_tracefile_group_args *args) |
| 750 | { |
| 751 | int i; |
| 752 | LttTracefile *tf; |
| 753 | |
| 754 | for(i=0; i<group->len; i++) { |
| 755 | tf = &g_array_index (group, LttTracefile, i); |
| 756 | if(tf->cpu_online) |
| 757 | args->func(tf, args->func_args); |
| 758 | } |
| 759 | } |
| 760 | |
| 761 | |
| 762 | void ltt_tracefile_group_destroy(gpointer data) |
| 763 | { |
| 764 | GArray *group = (GArray *)data; |
| 765 | int i; |
| 766 | LttTracefile *tf; |
| 767 | |
| 768 | for(i=0; i<group->len; i++) { |
| 769 | tf = &g_array_index (group, LttTracefile, i); |
| 770 | if(tf->cpu_online) |
| 771 | ltt_tracefile_close(tf); |
| 772 | } |
| 773 | g_array_free(group, TRUE); |
| 774 | } |
| 775 | |
| 776 | gboolean ltt_tracefile_group_has_cpu_online(gpointer data) |
| 777 | { |
| 778 | GArray *group = (GArray *)data; |
| 779 | int i; |
| 780 | LttTracefile *tf; |
| 781 | |
| 782 | for(i=0; i<group->len; i++) { |
| 783 | tf = &g_array_index (group, LttTracefile, i); |
| 784 | if(tf->cpu_online) return 1; |
| 785 | } |
| 786 | return 0; |
| 787 | } |
| 788 | |
| 789 | |
| 790 | /* Open each tracefile under a specific directory. Put them in a |
| 791 | * GData : permits to access them using their tracefile group pathname. |
| 792 | * i.e. access control/modules tracefile group by index : |
| 793 | * "control/module". |
| 794 | * |
| 795 | * relative path is the path relative to the trace root |
| 796 | * root path is the full path |
| 797 | * |
| 798 | * A tracefile group is simply an array where all the per cpu tracefiles sits. |
| 799 | */ |
| 800 | |
| 801 | static int open_tracefiles(LttTrace *trace, gchar *root_path, |
| 802 | gchar *relative_path) |
| 803 | { |
| 804 | DIR *dir = opendir(root_path); |
| 805 | struct dirent *entry; |
| 806 | struct stat stat_buf; |
| 807 | int ret; |
| 808 | |
| 809 | gchar path[PATH_MAX]; |
| 810 | int path_len; |
| 811 | gchar *path_ptr; |
| 812 | |
| 813 | int rel_path_len; |
| 814 | gchar rel_path[PATH_MAX]; |
| 815 | gchar *rel_path_ptr; |
| 816 | LttTracefile tmp_tf; |
| 817 | |
| 818 | if(dir == NULL) { |
| 819 | perror(root_path); |
| 820 | return ENOENT; |
| 821 | } |
| 822 | |
| 823 | strncpy(path, root_path, PATH_MAX-1); |
| 824 | path_len = strlen(path); |
| 825 | path[path_len] = '/'; |
| 826 | path_len++; |
| 827 | path_ptr = path + path_len; |
| 828 | |
| 829 | strncpy(rel_path, relative_path, PATH_MAX-1); |
| 830 | rel_path_len = strlen(rel_path); |
| 831 | rel_path[rel_path_len] = '/'; |
| 832 | rel_path_len++; |
| 833 | rel_path_ptr = rel_path + rel_path_len; |
| 834 | |
| 835 | while((entry = readdir(dir)) != NULL) { |
| 836 | |
| 837 | if(entry->d_name[0] == '.') continue; |
| 838 | |
| 839 | strncpy(path_ptr, entry->d_name, PATH_MAX - path_len); |
| 840 | strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len); |
| 841 | |
| 842 | ret = stat(path, &stat_buf); |
| 843 | if(ret == -1) { |
| 844 | perror(path); |
| 845 | continue; |
| 846 | } |
| 847 | |
| 848 | g_debug("Tracefile file or directory : %s\n", path); |
| 849 | |
| 850 | if(strcmp(rel_path, "/eventdefs") == 0) continue; |
| 851 | |
| 852 | if(S_ISDIR(stat_buf.st_mode)) { |
| 853 | |
| 854 | g_debug("Entering subdirectory...\n"); |
| 855 | ret = open_tracefiles(trace, path, rel_path); |
| 856 | if(ret < 0) continue; |
| 857 | } else if(S_ISREG(stat_buf.st_mode)) { |
| 858 | GQuark name; |
| 859 | guint num, tid, pgid; |
| 860 | guint64 creation; |
| 861 | GArray *group; |
| 862 | num = tid = pgid = 0; |
| 863 | creation = 0; |
| 864 | if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation)) |
| 865 | continue; /* invalid name */ |
| 866 | |
| 867 | g_debug("Opening file.\n"); |
| 868 | if(ltt_tracefile_open(trace, path, &tmp_tf)) { |
| 869 | g_info("Error opening tracefile %s", path); |
| 870 | |
| 871 | continue; /* error opening the tracefile : bad magic number ? */ |
| 872 | } |
| 873 | |
| 874 | g_debug("Tracefile name is %s and number is %u", |
| 875 | g_quark_to_string(name), num); |
| 876 | |
| 877 | tmp_tf.cpu_online = 1; |
| 878 | tmp_tf.cpu_num = num; |
| 879 | tmp_tf.name = name; |
| 880 | tmp_tf.tid = tid; |
| 881 | tmp_tf.pgid = pgid; |
| 882 | tmp_tf.creation = creation; |
| 883 | |
| 884 | group = g_datalist_id_get_data(&trace->tracefiles, name); |
| 885 | if(group == NULL) { |
| 886 | /* Elements are automatically cleared when the array is allocated. |
| 887 | * It makes the cpu_online variable set to 0 : cpu offline, by default. |
| 888 | */ |
| 889 | group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10); |
| 890 | g_datalist_id_set_data_full(&trace->tracefiles, name, |
| 891 | group, ltt_tracefile_group_destroy); |
| 892 | } |
| 893 | |
| 894 | /* Add the per cpu tracefile to the named group */ |
| 895 | unsigned int old_len = group->len; |
| 896 | if(num+1 > old_len) |
| 897 | group = g_array_set_size(group, num+1); |
| 898 | g_array_index (group, LttTracefile, num) = tmp_tf; |
| 899 | |
| 900 | } |
| 901 | } |
| 902 | |
| 903 | closedir(dir); |
| 904 | |
| 905 | return 0; |
| 906 | } |
| 907 | |
| 908 | /* ltt_get_facility_description |
| 909 | * |
| 910 | * Opens the file corresponding to the requested facility (identified by fac_id |
| 911 | * and checksum). |
| 912 | * |
| 913 | * The name searched is : %trace root%/eventdefs/facname_checksum.xml |
| 914 | * |
| 915 | * Returns 0 on success, or 1 on failure. |
| 916 | */ |
| 917 | |
| 918 | static int ltt_get_facility_description(LttFacility *f, |
| 919 | LttTrace *t, |
| 920 | LttTracefile *fac_tf) |
| 921 | { |
| 922 | char desc_file_name[PATH_MAX]; |
| 923 | const gchar *text; |
| 924 | guint textlen; |
| 925 | gint err; |
| 926 | gint arch_spec; |
| 927 | gint fac_name_len; |
| 928 | |
| 929 | text = g_quark_to_string(t->pathname); |
| 930 | textlen = strlen(text); |
| 931 | |
| 932 | if(textlen >= PATH_MAX) goto name_error; |
| 933 | strcpy(desc_file_name, text); |
| 934 | |
| 935 | text = "/eventdefs/"; |
| 936 | textlen+=strlen(text); |
| 937 | if(textlen >= PATH_MAX) goto name_error; |
| 938 | strcat(desc_file_name, text); |
| 939 | |
| 940 | text = g_quark_to_string(f->name); |
| 941 | fac_name_len = strlen(text); |
| 942 | textlen+=fac_name_len; |
| 943 | if(textlen >= PATH_MAX) goto name_error; |
| 944 | strcat(desc_file_name, text); |
| 945 | |
| 946 | /* arch specific facilities are named like this : name_arch */ |
| 947 | if(fac_name_len+1 < sizeof("_arch")) |
| 948 | arch_spec = 0; |
| 949 | else { |
| 950 | if(!strcmp(&text[fac_name_len+1-sizeof("_arch")], "_arch")) |
| 951 | arch_spec = 1; |
| 952 | else |
| 953 | arch_spec = 0; |
| 954 | } |
| 955 | |
| 956 | #if 0 |
| 957 | text = "_"; |
| 958 | textlen+=strlen(text); |
| 959 | if(textlen >= PATH_MAX) goto name_error; |
| 960 | strcat(desc_file_name, text); |
| 961 | |
| 962 | err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1, |
| 963 | "%u", f->checksum); |
| 964 | if(err < 0) goto name_error; |
| 965 | |
| 966 | textlen=strlen(desc_file_name); |
| 967 | |
| 968 | #endif //0 |
| 969 | |
| 970 | if(arch_spec) { |
| 971 | switch(t->arch_type) { |
| 972 | case LTT_ARCH_TYPE_I386: |
| 973 | text = "_i386"; |
| 974 | break; |
| 975 | case LTT_ARCH_TYPE_PPC: |
| 976 | text = "_ppc"; |
| 977 | break; |
| 978 | case LTT_ARCH_TYPE_SH: |
| 979 | text = "_sh"; |
| 980 | break; |
| 981 | case LTT_ARCH_TYPE_S390: |
| 982 | text = "_s390"; |
| 983 | break; |
| 984 | case LTT_ARCH_TYPE_MIPS: |
| 985 | text = "_mips"; |
| 986 | break; |
| 987 | case LTT_ARCH_TYPE_ARM: |
| 988 | text = "_arm"; |
| 989 | break; |
| 990 | case LTT_ARCH_TYPE_PPC64: |
| 991 | text = "_ppc64"; |
| 992 | break; |
| 993 | case LTT_ARCH_TYPE_X86_64: |
| 994 | text = "_x86_64"; |
| 995 | break; |
| 996 | default: |
| 997 | g_error("Trace from unsupported architecture."); |
| 998 | } |
| 999 | textlen+=strlen(text); |
| 1000 | if(textlen >= PATH_MAX) goto name_error; |
| 1001 | strcat(desc_file_name, text); |
| 1002 | } |
| 1003 | |
| 1004 | text = ".xml"; |
| 1005 | textlen+=strlen(text); |
| 1006 | if(textlen >= PATH_MAX) goto name_error; |
| 1007 | strcat(desc_file_name, text); |
| 1008 | |
| 1009 | err = ltt_facility_open(f, t, desc_file_name); |
| 1010 | if(err) goto facility_error; |
| 1011 | |
| 1012 | return 0; |
| 1013 | |
| 1014 | facility_error: |
| 1015 | name_error: |
| 1016 | return 1; |
| 1017 | } |
| 1018 | |
| 1019 | static void ltt_fac_ids_destroy(gpointer data) |
| 1020 | { |
| 1021 | GArray *fac_ids = (GArray *)data; |
| 1022 | |
| 1023 | g_array_free(fac_ids, TRUE); |
| 1024 | } |
| 1025 | |
| 1026 | |
| 1027 | /* Presumes the tracefile is already seeked at the beginning. It makes sense, |
| 1028 | * because it must be done just after the opening */ |
| 1029 | static int ltt_process_facility_tracefile(LttTracefile *tf) |
| 1030 | { |
| 1031 | int err; |
| 1032 | LttFacility *fac; |
| 1033 | GArray *fac_ids; |
| 1034 | guint i; |
| 1035 | LttEventType *et; |
| 1036 | |
| 1037 | while(1) { |
| 1038 | err = ltt_tracefile_read_seek(tf); |
| 1039 | if(err == EPERM) goto seek_error; |
| 1040 | else if(err == ERANGE) break; /* End of tracefile */ |
| 1041 | |
| 1042 | err = ltt_tracefile_read_update_event(tf); |
| 1043 | if(err) goto update_error; |
| 1044 | |
| 1045 | /* We are on a facility load/or facility unload/ or heartbeat event */ |
| 1046 | /* The rules are : |
| 1047 | * * facility 0 is hardcoded : this is the core facility. It will be shown |
| 1048 | * in the facility array though, and is shown as "loaded builtin" in the |
| 1049 | * trace. |
| 1050 | * It contains event : |
| 1051 | * 0 : facility load |
| 1052 | * 1 : facility unload |
| 1053 | * 2 : state dump facility load |
| 1054 | * 3 : heartbeat |
| 1055 | */ |
| 1056 | if(tf->event.facility_id != LTT_FACILITY_CORE) { |
| 1057 | /* Should only contain core facility */ |
| 1058 | g_warning("Error in processing facility file %s, " |
| 1059 | "should not contain facility id %u.", g_quark_to_string(tf->name), |
| 1060 | tf->event.facility_id); |
| 1061 | err = EPERM; |
| 1062 | goto fac_id_error; |
| 1063 | } else { |
| 1064 | |
| 1065 | struct LttFacilityLoad *fac_load_data; |
| 1066 | struct LttStateDumpFacilityLoad *fac_state_dump_load_data; |
| 1067 | char *fac_name; |
| 1068 | void *pos; |
| 1069 | |
| 1070 | // FIXME align |
| 1071 | switch((enum ltt_core_events)tf->event.event_id) { |
| 1072 | case LTT_EVENT_FACILITY_LOAD: |
| 1073 | fac_name = (char*)(tf->event.data); |
| 1074 | g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s", |
| 1075 | fac_name); |
| 1076 | pos = (tf->event.data + strlen(fac_name) + 1); |
| 1077 | pos += ltt_align((size_t)pos, sizeof(guint32), tf->has_alignment); |
| 1078 | fac_load_data = (struct LttFacilityLoad *)pos; |
| 1079 | |
| 1080 | fac = &g_array_index (tf->trace->facilities_by_num, LttFacility, |
| 1081 | ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id)); |
| 1082 | /* facility may already exist if trace is paused/unpaused */ |
| 1083 | if(fac->exists) continue; |
| 1084 | fac->name = g_quark_from_string(fac_name); |
| 1085 | fac->checksum = ltt_get_uint32(LTT_GET_BO(tf), |
| 1086 | &fac_load_data->checksum); |
| 1087 | fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id); |
| 1088 | fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1089 | &fac_load_data->pointer_size); |
| 1090 | fac->int_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1091 | &fac_load_data->int_size); |
| 1092 | fac->long_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1093 | &fac_load_data->long_size); |
| 1094 | fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1095 | &fac_load_data->size_t_size); |
| 1096 | fac->alignment = ltt_get_uint32(LTT_GET_BO(tf), |
| 1097 | &fac_load_data->has_alignment); |
| 1098 | |
| 1099 | if(ltt_get_facility_description(fac, tf->trace, tf)) |
| 1100 | continue; /* error opening description */ |
| 1101 | |
| 1102 | fac->trace = tf->trace; |
| 1103 | |
| 1104 | /* Preset the field offsets */ |
| 1105 | for(i=0; i<fac->events->len; i++){ |
| 1106 | et = &g_array_index(fac->events, LttEventType, i); |
| 1107 | precompute_offsets(fac, et); |
| 1108 | } |
| 1109 | |
| 1110 | fac->exists = 1; |
| 1111 | |
| 1112 | fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name, |
| 1113 | fac->name); |
| 1114 | if(fac_ids == NULL) { |
| 1115 | fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1); |
| 1116 | g_datalist_id_set_data_full(&tf->trace->facilities_by_name, |
| 1117 | fac->name, |
| 1118 | fac_ids, ltt_fac_ids_destroy); |
| 1119 | } |
| 1120 | g_array_append_val(fac_ids, fac->id); |
| 1121 | |
| 1122 | break; |
| 1123 | case LTT_EVENT_FACILITY_UNLOAD: |
| 1124 | g_debug("Doing LTT_EVENT_FACILITY_UNLOAD"); |
| 1125 | /* We don't care about unload : facilities ID are valid for the whole |
| 1126 | * trace. They simply won't be used after the unload. */ |
| 1127 | break; |
| 1128 | case LTT_EVENT_STATE_DUMP_FACILITY_LOAD: |
| 1129 | fac_name = (char*)(tf->event.data); |
| 1130 | g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s", |
| 1131 | fac_name); |
| 1132 | pos = (tf->event.data + strlen(fac_name) + 1); |
| 1133 | pos += ltt_align((size_t)pos, sizeof(guint32), tf->has_alignment); |
| 1134 | fac_state_dump_load_data = (struct LttStateDumpFacilityLoad *)pos; |
| 1135 | |
| 1136 | fac = &g_array_index (tf->trace->facilities_by_num, LttFacility, |
| 1137 | ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id)); |
| 1138 | /* facility may already exist if trace is paused/unpaused */ |
| 1139 | if(fac->exists) continue; |
| 1140 | fac->name = g_quark_from_string(fac_name); |
| 1141 | fac->checksum = ltt_get_uint32(LTT_GET_BO(tf), |
| 1142 | &fac_state_dump_load_data->checksum); |
| 1143 | fac->id = ltt_get_uint32(LTT_GET_BO(tf), |
| 1144 | &fac_state_dump_load_data->id); |
| 1145 | fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1146 | &fac_state_dump_load_data->pointer_size); |
| 1147 | fac->int_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1148 | &fac_state_dump_load_data->int_size); |
| 1149 | fac->long_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1150 | &fac_state_dump_load_data->long_size); |
| 1151 | fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1152 | &fac_state_dump_load_data->size_t_size); |
| 1153 | fac->alignment = ltt_get_uint32(LTT_GET_BO(tf), |
| 1154 | &fac_state_dump_load_data->has_alignment); |
| 1155 | if(ltt_get_facility_description(fac, tf->trace, tf)) |
| 1156 | continue; /* error opening description */ |
| 1157 | |
| 1158 | fac->trace = tf->trace; |
| 1159 | |
| 1160 | /* Preset the field offsets */ |
| 1161 | for(i=0; i<fac->events->len; i++){ |
| 1162 | et = &g_array_index(fac->events, LttEventType, i); |
| 1163 | precompute_offsets(fac, et); |
| 1164 | } |
| 1165 | |
| 1166 | fac->exists = 1; |
| 1167 | |
| 1168 | fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name, |
| 1169 | fac->name); |
| 1170 | if(fac_ids == NULL) { |
| 1171 | fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1); |
| 1172 | g_datalist_id_set_data_full(&tf->trace->facilities_by_name, |
| 1173 | fac->name, |
| 1174 | fac_ids, ltt_fac_ids_destroy); |
| 1175 | } |
| 1176 | g_array_append_val(fac_ids, fac->id); |
| 1177 | |
| 1178 | break; |
| 1179 | case LTT_EVENT_HEARTBEAT: |
| 1180 | break; |
| 1181 | default: |
| 1182 | g_warning("Error in processing facility file %s, " |
| 1183 | "unknown event id %hhu in core facility.", |
| 1184 | g_quark_to_string(tf->name), |
| 1185 | tf->event.event_id); |
| 1186 | err = EPERM; |
| 1187 | goto event_id_error; |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | return 0; |
| 1192 | |
| 1193 | /* Error handling */ |
| 1194 | event_id_error: |
| 1195 | fac_id_error: |
| 1196 | update_error: |
| 1197 | seek_error: |
| 1198 | g_warning("An error occured in facility tracefile parsing"); |
| 1199 | return err; |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | LttTrace *ltt_trace_open(const gchar *pathname) |
| 1204 | { |
| 1205 | gchar abs_path[PATH_MAX]; |
| 1206 | LttTrace * t; |
| 1207 | LttTracefile *tf; |
| 1208 | GArray *group; |
| 1209 | int i, ret; |
| 1210 | struct ltt_block_start_header *header; |
| 1211 | DIR *dir; |
| 1212 | struct dirent *entry; |
| 1213 | guint control_found = 0; |
| 1214 | guint eventdefs_found = 0; |
| 1215 | struct stat stat_buf; |
| 1216 | gchar path[PATH_MAX]; |
| 1217 | |
| 1218 | t = g_new(LttTrace, 1); |
| 1219 | if(!t) goto alloc_error; |
| 1220 | |
| 1221 | get_absolute_pathname(pathname, abs_path); |
| 1222 | t->pathname = g_quark_from_string(abs_path); |
| 1223 | |
| 1224 | g_datalist_init(&t->tracefiles); |
| 1225 | |
| 1226 | /* Test to see if it looks like a trace */ |
| 1227 | dir = opendir(abs_path); |
| 1228 | if(dir == NULL) { |
| 1229 | perror(abs_path); |
| 1230 | goto open_error; |
| 1231 | } |
| 1232 | while((entry = readdir(dir)) != NULL) { |
| 1233 | strcpy(path, abs_path); |
| 1234 | strcat(path, "/"); |
| 1235 | strcat(path, entry->d_name); |
| 1236 | ret = stat(path, &stat_buf); |
| 1237 | if(ret == -1) { |
| 1238 | perror(path); |
| 1239 | continue; |
| 1240 | } |
| 1241 | if(S_ISDIR(stat_buf.st_mode)) { |
| 1242 | if(strcmp(entry->d_name, "control") == 0) { |
| 1243 | control_found = 1; |
| 1244 | } |
| 1245 | if(strcmp(entry->d_name, "eventdefs") == 0) { |
| 1246 | eventdefs_found = 1; |
| 1247 | } |
| 1248 | } |
| 1249 | } |
| 1250 | closedir(dir); |
| 1251 | |
| 1252 | if(!control_found || !eventdefs_found) goto find_error; |
| 1253 | |
| 1254 | /* Open all the tracefiles */ |
| 1255 | if(open_tracefiles(t, abs_path, "")) { |
| 1256 | g_warning("Error opening tracefile %s", abs_path); |
| 1257 | goto find_error; |
| 1258 | } |
| 1259 | |
| 1260 | /* Prepare the facilities containers : array and mapping */ |
| 1261 | /* Array is zeroed : the "exists" field is set to false by default */ |
| 1262 | t->facilities_by_num = g_array_sized_new (FALSE, |
| 1263 | TRUE, sizeof(LttFacility), |
| 1264 | NUM_FACILITIES); |
| 1265 | t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES); |
| 1266 | |
| 1267 | g_datalist_init(&t->facilities_by_name); |
| 1268 | |
| 1269 | /* Parse each trace control/facilitiesN files : get runtime fac. info */ |
| 1270 | group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES); |
| 1271 | if(group == NULL) { |
| 1272 | g_error("Trace %s has no facility tracefile", abs_path); |
| 1273 | g_assert(0); |
| 1274 | goto facilities_error; |
| 1275 | } |
| 1276 | |
| 1277 | /* Get the trace information for the control/facility 0 tracefile */ |
| 1278 | g_assert(group->len > 0); |
| 1279 | tf = &g_array_index (group, LttTracefile, 0); |
| 1280 | header = (struct ltt_block_start_header*)tf->buffer.head; |
| 1281 | g_assert(parse_trace_header(header->trace, |
| 1282 | tf, t) == 0); |
| 1283 | |
| 1284 | t->num_cpu = group->len; |
| 1285 | |
| 1286 | for(i=0; i<group->len; i++) { |
| 1287 | tf = &g_array_index (group, LttTracefile, i); |
| 1288 | if(ltt_process_facility_tracefile(tf)) |
| 1289 | goto facilities_error; |
| 1290 | } |
| 1291 | |
| 1292 | return t; |
| 1293 | |
| 1294 | /* Error handling */ |
| 1295 | facilities_error: |
| 1296 | g_datalist_clear(&t->facilities_by_name); |
| 1297 | g_array_free(t->facilities_by_num, TRUE); |
| 1298 | find_error: |
| 1299 | g_datalist_clear(&t->tracefiles); |
| 1300 | open_error: |
| 1301 | g_free(t); |
| 1302 | alloc_error: |
| 1303 | return NULL; |
| 1304 | |
| 1305 | } |
| 1306 | |
| 1307 | GQuark ltt_trace_name(const LttTrace *t) |
| 1308 | { |
| 1309 | return t->pathname; |
| 1310 | } |
| 1311 | |
| 1312 | |
| 1313 | /****************************************************************************** |
| 1314 | * When we copy a trace, we want all the opening actions to happen again : |
| 1315 | * the trace will be reopened and totally independant from the original. |
| 1316 | * That's why we call ltt_trace_open. |
| 1317 | *****************************************************************************/ |
| 1318 | LttTrace *ltt_trace_copy(LttTrace *self) |
| 1319 | { |
| 1320 | return ltt_trace_open(g_quark_to_string(self->pathname)); |
| 1321 | } |
| 1322 | |
| 1323 | void ltt_trace_close(LttTrace *t) |
| 1324 | { |
| 1325 | guint i; |
| 1326 | LttFacility *fac; |
| 1327 | |
| 1328 | for(i=0; i<t->facilities_by_num->len; i++) { |
| 1329 | fac = &g_array_index (t->facilities_by_num, LttFacility, i); |
| 1330 | if(fac->exists) |
| 1331 | ltt_facility_close(fac); |
| 1332 | } |
| 1333 | |
| 1334 | g_datalist_clear(&t->facilities_by_name); |
| 1335 | g_array_free(t->facilities_by_num, TRUE); |
| 1336 | g_datalist_clear(&t->tracefiles); |
| 1337 | g_free(t); |
| 1338 | } |
| 1339 | |
| 1340 | |
| 1341 | /***************************************************************************** |
| 1342 | *Get the system description of the trace |
| 1343 | ****************************************************************************/ |
| 1344 | |
| 1345 | LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id) |
| 1346 | { |
| 1347 | g_assert(id < t->facilities_by_num->len); |
| 1348 | return &g_array_index(t->facilities_by_num, LttFacility, id); |
| 1349 | } |
| 1350 | |
| 1351 | /* ltt_trace_facility_get_by_name |
| 1352 | * |
| 1353 | * Returns the GArray of facility indexes. All the fac_ids that matches the |
| 1354 | * requested facility name. |
| 1355 | * |
| 1356 | * If name is not found, returns NULL. |
| 1357 | */ |
| 1358 | GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name) |
| 1359 | { |
| 1360 | return g_datalist_id_get_data(&t->facilities_by_name, name); |
| 1361 | } |
| 1362 | |
| 1363 | /***************************************************************************** |
| 1364 | * Functions to discover all the event types in the trace |
| 1365 | ****************************************************************************/ |
| 1366 | |
| 1367 | #if 0 |
| 1368 | unsigned ltt_trace_eventtype_number(LttTrace *t) |
| 1369 | { |
| 1370 | unsigned int i; |
| 1371 | unsigned count = 0; |
| 1372 | unsigned int num = t->facility_number; |
| 1373 | LttFacility * f; |
| 1374 | |
| 1375 | for(i=0;i<num;i++){ |
| 1376 | f = (LttFacility*)g_ptr_array_index(t->facilities, i); |
| 1377 | count += f->event_number; |
| 1378 | } |
| 1379 | return count; |
| 1380 | } |
| 1381 | #endif //0 |
| 1382 | |
| 1383 | #if 0 |
| 1384 | //use an iteration on all the trace facilities, and inside iteration on all the |
| 1385 | //event types in each facilities instead. |
| 1386 | LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId) |
| 1387 | { |
| 1388 | LttEventType *event_type; |
| 1389 | |
| 1390 | LttFacility * f; |
| 1391 | f = ltt_trace_facility_by_id(t,evId); |
| 1392 | |
| 1393 | if(unlikely(!f)) event_type = NULL; |
| 1394 | else event_type = f->events[evId - f->base_id]; |
| 1395 | |
| 1396 | return event_type; |
| 1397 | } |
| 1398 | #endif //0 |
| 1399 | |
| 1400 | #if 0 |
| 1401 | /***************************************************************************** |
| 1402 | * ltt_trace_find_tracefile |
| 1403 | * |
| 1404 | * Find a tracefile by name and index in the group. |
| 1405 | * |
| 1406 | * Returns a pointer to the tracefiles, else NULL. |
| 1407 | ****************************************************************************/ |
| 1408 | |
| 1409 | LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name) |
| 1410 | { |
| 1411 | } |
| 1412 | #endif //0 |
| 1413 | |
| 1414 | /***************************************************************************** |
| 1415 | * Get the start time and end time of the trace |
| 1416 | ****************************************************************************/ |
| 1417 | |
| 1418 | static void ltt_tracefile_time_span_get(LttTracefile *tf, |
| 1419 | LttTime *start, LttTime *end) |
| 1420 | { |
| 1421 | int err; |
| 1422 | |
| 1423 | err = map_block(tf, 0); |
| 1424 | if(unlikely(err)) { |
| 1425 | g_error("Can not map block"); |
| 1426 | *start = ltt_time_infinite; |
| 1427 | } else |
| 1428 | *start = tf->buffer.begin.timestamp; |
| 1429 | |
| 1430 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
| 1431 | if(unlikely(err)) { |
| 1432 | g_error("Can not map block"); |
| 1433 | *end = ltt_time_zero; |
| 1434 | } else |
| 1435 | *end = tf->buffer.end.timestamp; |
| 1436 | } |
| 1437 | |
| 1438 | struct tracefile_time_span_get_args { |
| 1439 | LttTrace *t; |
| 1440 | LttTime *start; |
| 1441 | LttTime *end; |
| 1442 | }; |
| 1443 | |
| 1444 | static void group_time_span_get(GQuark name, gpointer data, gpointer user_data) |
| 1445 | { |
| 1446 | struct tracefile_time_span_get_args *args = |
| 1447 | (struct tracefile_time_span_get_args*)user_data; |
| 1448 | |
| 1449 | GArray *group = (GArray *)data; |
| 1450 | int i; |
| 1451 | LttTracefile *tf; |
| 1452 | LttTime tmp_start; |
| 1453 | LttTime tmp_end; |
| 1454 | |
| 1455 | for(i=0; i<group->len; i++) { |
| 1456 | tf = &g_array_index (group, LttTracefile, i); |
| 1457 | if(tf->cpu_online) { |
| 1458 | ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end); |
| 1459 | if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start; |
| 1460 | if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end; |
| 1461 | } |
| 1462 | } |
| 1463 | } |
| 1464 | |
| 1465 | void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end) |
| 1466 | { |
| 1467 | LttTime min_start = ltt_time_infinite; |
| 1468 | LttTime max_end = ltt_time_zero; |
| 1469 | struct tracefile_time_span_get_args args = { t, &min_start, &max_end }; |
| 1470 | |
| 1471 | g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args); |
| 1472 | |
| 1473 | if(start != NULL) *start = min_start; |
| 1474 | if(end != NULL) *end = max_end; |
| 1475 | |
| 1476 | } |
| 1477 | |
| 1478 | |
| 1479 | /***************************************************************************** |
| 1480 | *Get the name of a tracefile |
| 1481 | ****************************************************************************/ |
| 1482 | |
| 1483 | GQuark ltt_tracefile_name(const LttTracefile *tf) |
| 1484 | { |
| 1485 | return tf->name; |
| 1486 | } |
| 1487 | |
| 1488 | GQuark ltt_tracefile_long_name(const LttTracefile *tf) |
| 1489 | { |
| 1490 | return tf->long_name; |
| 1491 | } |
| 1492 | |
| 1493 | |
| 1494 | |
| 1495 | guint ltt_tracefile_cpu(LttTracefile *tf) |
| 1496 | { |
| 1497 | return tf->cpu_num; |
| 1498 | } |
| 1499 | |
| 1500 | guint ltt_tracefile_tid(LttTracefile *tf) |
| 1501 | { |
| 1502 | return tf->tid; |
| 1503 | } |
| 1504 | |
| 1505 | guint ltt_tracefile_pgid(LttTracefile *tf) |
| 1506 | { |
| 1507 | return tf->pgid; |
| 1508 | } |
| 1509 | |
| 1510 | guint64 ltt_tracefile_creation(LttTracefile *tf) |
| 1511 | { |
| 1512 | return tf->creation; |
| 1513 | } |
| 1514 | /***************************************************************************** |
| 1515 | * Get the number of blocks in the tracefile |
| 1516 | ****************************************************************************/ |
| 1517 | |
| 1518 | guint ltt_tracefile_block_number(LttTracefile *tf) |
| 1519 | { |
| 1520 | return tf->num_blocks; |
| 1521 | } |
| 1522 | |
| 1523 | |
| 1524 | /* Seek to the first event in a tracefile that has a time equal or greater than |
| 1525 | * the time passed in parameter. |
| 1526 | * |
| 1527 | * If the time parameter is outside the tracefile time span, seek to the first |
| 1528 | * event or if after, return ERANGE. |
| 1529 | * |
| 1530 | * If the time parameter is before the first event, we have to seek specially to |
| 1531 | * there. |
| 1532 | * |
| 1533 | * If the time is after the end of the trace, return ERANGE. |
| 1534 | * |
| 1535 | * Do a binary search to find the right block, then a sequential search in the |
| 1536 | * block to find the event. |
| 1537 | * |
| 1538 | * In the special case where the time requested fits inside a block that has no |
| 1539 | * event corresponding to the requested time, the first event of the next block |
| 1540 | * will be seeked. |
| 1541 | * |
| 1542 | * IMPORTANT NOTE : // FIXME everywhere... |
| 1543 | * |
| 1544 | * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time : |
| 1545 | * you will jump over an event if you do. |
| 1546 | * |
| 1547 | * Return value : 0 : no error, the tf->event can be used |
| 1548 | * ERANGE : time if after the last event of the trace |
| 1549 | * otherwise : this is an error. |
| 1550 | * |
| 1551 | * */ |
| 1552 | |
| 1553 | int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time) |
| 1554 | { |
| 1555 | int ret = 0; |
| 1556 | int err; |
| 1557 | unsigned int block_num, high, low; |
| 1558 | |
| 1559 | /* seek at the beginning of trace */ |
| 1560 | err = map_block(tf, 0); /* First block */ |
| 1561 | if(unlikely(err)) { |
| 1562 | g_error("Can not map block"); |
| 1563 | goto fail; |
| 1564 | } |
| 1565 | |
| 1566 | /* If the time is lower or equal the beginning of the trace, |
| 1567 | * go to the first event. */ |
| 1568 | if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) { |
| 1569 | ret = ltt_tracefile_read(tf); |
| 1570 | if(ret == ERANGE) goto range; |
| 1571 | else if (ret) goto fail; |
| 1572 | goto found; /* There is either no event in the trace or the event points |
| 1573 | to the first event in the trace */ |
| 1574 | } |
| 1575 | |
| 1576 | err = map_block(tf, tf->num_blocks - 1); /* Last block */ |
| 1577 | if(unlikely(err)) { |
| 1578 | g_error("Can not map block"); |
| 1579 | goto fail; |
| 1580 | } |
| 1581 | |
| 1582 | /* If the time is after the end of the trace, return ERANGE. */ |
| 1583 | if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
| 1584 | goto range; |
| 1585 | } |
| 1586 | |
| 1587 | /* Binary search the block */ |
| 1588 | high = tf->num_blocks - 1; |
| 1589 | low = 0; |
| 1590 | |
| 1591 | while(1) { |
| 1592 | block_num = ((high-low) / 2) + low; |
| 1593 | |
| 1594 | err = map_block(tf, block_num); |
| 1595 | if(unlikely(err)) { |
| 1596 | g_error("Can not map block"); |
| 1597 | goto fail; |
| 1598 | } |
| 1599 | if(high == low) { |
| 1600 | /* We cannot divide anymore : this is what would happen if the time |
| 1601 | * requested was exactly between two consecutive buffers'end and start |
| 1602 | * timestamps. This is also what would happend if we didn't deal with out |
| 1603 | * of span cases prior in this function. */ |
| 1604 | /* The event is right in the buffer! |
| 1605 | * (or in the next buffer first event) */ |
| 1606 | while(1) { |
| 1607 | ret = ltt_tracefile_read(tf); |
| 1608 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
| 1609 | else if(ret) goto fail; |
| 1610 | |
| 1611 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
| 1612 | goto found; |
| 1613 | } |
| 1614 | |
| 1615 | } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) { |
| 1616 | /* go to lower part */ |
| 1617 | high = block_num - 1; |
| 1618 | } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) { |
| 1619 | /* go to higher part */ |
| 1620 | low = block_num + 1; |
| 1621 | } else {/* The event is right in the buffer! |
| 1622 | (or in the next buffer first event) */ |
| 1623 | while(1) { |
| 1624 | ret = ltt_tracefile_read(tf); |
| 1625 | if(ret == ERANGE) goto range; /* ERANGE or EPERM */ |
| 1626 | else if(ret) goto fail; |
| 1627 | |
| 1628 | if(ltt_time_compare(time, tf->event.event_time) <= 0) |
| 1629 | break; |
| 1630 | } |
| 1631 | goto found; |
| 1632 | } |
| 1633 | } |
| 1634 | |
| 1635 | found: |
| 1636 | return 0; |
| 1637 | range: |
| 1638 | return ERANGE; |
| 1639 | |
| 1640 | /* Error handling */ |
| 1641 | fail: |
| 1642 | g_error("ltt_tracefile_seek_time failed on tracefile %s", |
| 1643 | g_quark_to_string(tf->name)); |
| 1644 | return EPERM; |
| 1645 | } |
| 1646 | |
| 1647 | |
| 1648 | int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) { |
| 1649 | |
| 1650 | int err; |
| 1651 | |
| 1652 | if(ep->tracefile != tf) { |
| 1653 | goto fail; |
| 1654 | } |
| 1655 | |
| 1656 | err = map_block(tf, ep->block); |
| 1657 | if(unlikely(err)) { |
| 1658 | g_error("Can not map block"); |
| 1659 | goto fail; |
| 1660 | } |
| 1661 | |
| 1662 | tf->event.offset = ep->offset; |
| 1663 | |
| 1664 | /* Put back the event real tsc */ |
| 1665 | tf->event.tsc = ep->tsc; |
| 1666 | tf->buffer.tsc = ep->tsc; |
| 1667 | |
| 1668 | err = ltt_tracefile_read_update_event(tf); |
| 1669 | if(err) goto fail; |
| 1670 | err = ltt_tracefile_read_op(tf); |
| 1671 | if(err) goto fail; |
| 1672 | |
| 1673 | return 0; |
| 1674 | |
| 1675 | fail: |
| 1676 | g_error("ltt_tracefile_seek_time failed on tracefile %s", |
| 1677 | g_quark_to_string(tf->name)); |
| 1678 | return 1; |
| 1679 | } |
| 1680 | |
| 1681 | LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc) |
| 1682 | { |
| 1683 | LttTime time; |
| 1684 | |
| 1685 | if(tsc > tf->trace->start_tsc) { |
| 1686 | time = ltt_time_from_uint64( |
| 1687 | (double)(tsc - tf->trace->start_tsc) |
| 1688 | * (1000000000.0 / tf->trace->freq_scale) |
| 1689 | / (double)tf->trace->start_freq); |
| 1690 | time = ltt_time_add(tf->trace->start_time_from_tsc, time); |
| 1691 | } else { |
| 1692 | time = ltt_time_from_uint64( |
| 1693 | (double)(tf->trace->start_tsc - tsc) |
| 1694 | * (1000000000.0 / tf->trace->freq_scale) |
| 1695 | / (double)tf->trace->start_freq); |
| 1696 | time = ltt_time_sub(tf->trace->start_time_from_tsc, time); |
| 1697 | } |
| 1698 | return time; |
| 1699 | } |
| 1700 | |
| 1701 | /* Calculate the real event time based on the buffer boundaries */ |
| 1702 | LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event) |
| 1703 | { |
| 1704 | return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc); |
| 1705 | } |
| 1706 | |
| 1707 | |
| 1708 | /* Get the current event of the tracefile : valid until the next read */ |
| 1709 | LttEvent *ltt_tracefile_get_event(LttTracefile *tf) |
| 1710 | { |
| 1711 | return &tf->event; |
| 1712 | } |
| 1713 | |
| 1714 | |
| 1715 | |
| 1716 | /***************************************************************************** |
| 1717 | *Function name |
| 1718 | * ltt_tracefile_read : Read the next event in the tracefile |
| 1719 | *Input params |
| 1720 | * t : tracefile |
| 1721 | *Return value |
| 1722 | * |
| 1723 | * Returns 0 if an event can be used in tf->event. |
| 1724 | * Returns ERANGE on end of trace. The event in tf->event still can be used |
| 1725 | * (if the last block was not empty). |
| 1726 | * Returns EPERM on error. |
| 1727 | * |
| 1728 | * This function does make the tracefile event structure point to the event |
| 1729 | * currently pointed to by the tf->event. |
| 1730 | * |
| 1731 | * Note : you must call a ltt_tracefile_seek to the beginning of the trace to |
| 1732 | * reinitialize it after an error if you want results to be coherent. |
| 1733 | * It would be the case if a end of trace last buffer has no event : the end |
| 1734 | * of trace wouldn't be returned, but an error. |
| 1735 | * We make the assumption there is at least one event per buffer. |
| 1736 | ****************************************************************************/ |
| 1737 | |
| 1738 | int ltt_tracefile_read(LttTracefile *tf) |
| 1739 | { |
| 1740 | int err; |
| 1741 | |
| 1742 | err = ltt_tracefile_read_seek(tf); |
| 1743 | if(err) return err; |
| 1744 | err = ltt_tracefile_read_update_event(tf); |
| 1745 | if(err) return err; |
| 1746 | err = ltt_tracefile_read_op(tf); |
| 1747 | if(err) return err; |
| 1748 | |
| 1749 | return 0; |
| 1750 | } |
| 1751 | |
| 1752 | int ltt_tracefile_read_seek(LttTracefile *tf) |
| 1753 | { |
| 1754 | int err; |
| 1755 | |
| 1756 | /* Get next buffer until we finally have an event, or end of trace */ |
| 1757 | while(1) { |
| 1758 | err = ltt_seek_next_event(tf); |
| 1759 | if(unlikely(err == ENOPROTOOPT)) { |
| 1760 | return EPERM; |
| 1761 | } |
| 1762 | |
| 1763 | /* Are we at the end of the buffer ? */ |
| 1764 | if(err == ERANGE) { |
| 1765 | if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */ |
| 1766 | return ERANGE; |
| 1767 | } else { |
| 1768 | /* get next block */ |
| 1769 | err = map_block(tf, tf->buffer.index + 1); |
| 1770 | if(unlikely(err)) { |
| 1771 | g_error("Can not map block"); |
| 1772 | return EPERM; |
| 1773 | } |
| 1774 | } |
| 1775 | } else break; /* We found an event ! */ |
| 1776 | } |
| 1777 | |
| 1778 | return 0; |
| 1779 | } |
| 1780 | |
| 1781 | |
| 1782 | /* do specific operation on events */ |
| 1783 | int ltt_tracefile_read_op(LttTracefile *tf) |
| 1784 | { |
| 1785 | LttEvent *event; |
| 1786 | |
| 1787 | event = &tf->event; |
| 1788 | |
| 1789 | /* do event specific operation */ |
| 1790 | |
| 1791 | /* do something if its an heartbeat event : increment the heartbeat count */ |
| 1792 | //if(event->facility_id == LTT_FACILITY_CORE) |
| 1793 | // if(event->event_id == LTT_EVENT_HEARTBEAT) |
| 1794 | // tf->cur_heart_beat_number++; |
| 1795 | |
| 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | |
| 1800 | /* same as ltt_tracefile_read, but does not seek to the next event nor call |
| 1801 | * event specific operation. */ |
| 1802 | int ltt_tracefile_read_update_event(LttTracefile *tf) |
| 1803 | { |
| 1804 | void * pos; |
| 1805 | LttEvent *event; |
| 1806 | |
| 1807 | event = &tf->event; |
| 1808 | pos = tf->buffer.head + event->offset; |
| 1809 | |
| 1810 | /* Read event header */ |
| 1811 | |
| 1812 | /* Align the head */ |
| 1813 | pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment); |
| 1814 | |
| 1815 | if(tf->trace->has_heartbeat) { |
| 1816 | event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), |
| 1817 | pos); |
| 1818 | /* 32 bits -> 64 bits tsc */ |
| 1819 | /* note : still works for seek and non seek cases. */ |
| 1820 | if(event->timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) { |
| 1821 | tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL) |
| 1822 | + 0x100000000ULL) |
| 1823 | | (guint64)event->timestamp; |
| 1824 | event->tsc = tf->buffer.tsc; |
| 1825 | } else { |
| 1826 | /* no overflow */ |
| 1827 | tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL) |
| 1828 | | (guint64)event->timestamp; |
| 1829 | event->tsc = tf->buffer.tsc; |
| 1830 | } |
| 1831 | pos += sizeof(guint32); |
| 1832 | } else { |
| 1833 | event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos); |
| 1834 | tf->buffer.tsc = event->tsc; |
| 1835 | pos += sizeof(guint64); |
| 1836 | } |
| 1837 | event->event_time = ltt_interpolate_time(tf, event); |
| 1838 | event->facility_id = *(guint8*)pos; |
| 1839 | pos += sizeof(guint8); |
| 1840 | |
| 1841 | event->event_id = *(guint8*)pos; |
| 1842 | pos += sizeof(guint8); |
| 1843 | |
| 1844 | event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos); |
| 1845 | pos += sizeof(guint16); |
| 1846 | |
| 1847 | /* Align the head */ |
| 1848 | pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment); |
| 1849 | |
| 1850 | event->data = pos; |
| 1851 | |
| 1852 | /* get the data size and update the event fields with the current |
| 1853 | * information */ |
| 1854 | ltt_update_event_size(tf); |
| 1855 | |
| 1856 | return 0; |
| 1857 | } |
| 1858 | |
| 1859 | |
| 1860 | /**************************************************************************** |
| 1861 | *Function name |
| 1862 | * map_block : map a block from the file |
| 1863 | *Input Params |
| 1864 | * lttdes : ltt trace file |
| 1865 | * whichBlock : the block which will be read |
| 1866 | *return value |
| 1867 | * 0 : success |
| 1868 | * EINVAL : lseek fail |
| 1869 | * EIO : can not read from the file |
| 1870 | ****************************************************************************/ |
| 1871 | |
| 1872 | static gint map_block(LttTracefile * tf, guint block_num) |
| 1873 | { |
| 1874 | int page_size = getpagesize(); |
| 1875 | struct ltt_block_start_header *header; |
| 1876 | |
| 1877 | g_assert(block_num < tf->num_blocks); |
| 1878 | |
| 1879 | if(tf->buffer.head != NULL) { |
| 1880 | if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) { |
| 1881 | g_warning("unmap size : %u\n", |
| 1882 | PAGE_ALIGN(tf->buf_size)); |
| 1883 | perror("munmap error"); |
| 1884 | g_assert(0); |
| 1885 | } |
| 1886 | } |
| 1887 | |
| 1888 | |
| 1889 | /* Multiple of pages aligned head */ |
| 1890 | tf->buffer.head = mmap(0, |
| 1891 | PAGE_ALIGN(tf->buf_size), |
| 1892 | PROT_READ, MAP_PRIVATE, tf->fd, |
| 1893 | PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num)); |
| 1894 | |
| 1895 | if(tf->buffer.head == MAP_FAILED) { |
| 1896 | perror("Error in allocating memory for buffer of tracefile"); |
| 1897 | g_assert(0); |
| 1898 | goto map_error; |
| 1899 | } |
| 1900 | g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned. |
| 1901 | |
| 1902 | |
| 1903 | tf->buffer.index = block_num; |
| 1904 | |
| 1905 | header = (struct ltt_block_start_header*)tf->buffer.head; |
| 1906 | |
| 1907 | #if 0 |
| 1908 | tf->buffer.begin.timestamp = ltt_time_add( |
| 1909 | ltt_time_from_uint64( |
| 1910 | ltt_get_uint64(LTT_GET_BO(tf), |
| 1911 | &header->begin.timestamp) |
| 1912 | - tf->trace->start_monotonic), |
| 1913 | tf->trace->start_time); |
| 1914 | #endif //0 |
| 1915 | //g_debug("block %u begin : %lu.%lu", block_num, |
| 1916 | // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec); |
| 1917 | tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
| 1918 | &header->begin.cycle_count); |
| 1919 | tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf), |
| 1920 | &header->begin.freq); |
| 1921 | if(tf->buffer.begin.freq == 0) |
| 1922 | tf->buffer.begin.freq = tf->trace->start_freq; |
| 1923 | |
| 1924 | tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf, |
| 1925 | tf->buffer.begin.cycle_count); |
| 1926 | #if 0 |
| 1927 | ltt_time_add( |
| 1928 | ltt_time_from_uint64( |
| 1929 | (double)(tf->buffer.begin.cycle_count |
| 1930 | - tf->trace->start_tsc) * 1000000.0 |
| 1931 | / (double)tf->trace->start_freq), |
| 1932 | tf->trace->start_time_from_tsc); |
| 1933 | #endif //0 |
| 1934 | #if 0 |
| 1935 | |
| 1936 | tf->buffer.end.timestamp = ltt_time_add( |
| 1937 | ltt_time_from_uint64( |
| 1938 | ltt_get_uint64(LTT_GET_BO(tf), |
| 1939 | &header->end.timestamp) |
| 1940 | - tf->trace->start_monotonic), |
| 1941 | tf->trace->start_time); |
| 1942 | #endif //0 |
| 1943 | //g_debug("block %u end : %lu.%lu", block_num, |
| 1944 | // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec); |
| 1945 | tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf), |
| 1946 | &header->end.cycle_count); |
| 1947 | tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf), |
| 1948 | &header->end.freq); |
| 1949 | if(tf->buffer.end.freq == 0) |
| 1950 | tf->buffer.end.freq = tf->trace->start_freq; |
| 1951 | |
| 1952 | tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf), |
| 1953 | &header->lost_size); |
| 1954 | tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf, |
| 1955 | tf->buffer.end.cycle_count); |
| 1956 | #if 0 |
| 1957 | ltt_time_add( |
| 1958 | ltt_time_from_uint64( |
| 1959 | (double)(tf->buffer.end.cycle_count |
| 1960 | - tf->trace->start_tsc) * 1000000.0 |
| 1961 | / (double)tf->trace->start_freq), |
| 1962 | tf->trace->start_time_from_tsc); |
| 1963 | #endif //0 |
| 1964 | tf->buffer.tsc = tf->buffer.begin.cycle_count; |
| 1965 | tf->event.tsc = tf->buffer.tsc; |
| 1966 | tf->buffer.freq = tf->buffer.begin.freq; |
| 1967 | |
| 1968 | /* FIXME |
| 1969 | * eventually support variable buffer size : will need a partial pre-read of |
| 1970 | * the headers to create an index when we open the trace... eventually. */ |
| 1971 | g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf), |
| 1972 | &header->buf_size)); |
| 1973 | |
| 1974 | /* Now that the buffer is mapped, calculate the time interpolation for the |
| 1975 | * block. */ |
| 1976 | |
| 1977 | // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf); |
| 1978 | //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf); |
| 1979 | |
| 1980 | /* Make the current event point to the beginning of the buffer : |
| 1981 | * it means that the event read must get the first event. */ |
| 1982 | tf->event.tracefile = tf; |
| 1983 | tf->event.block = block_num; |
| 1984 | tf->event.offset = 0; |
| 1985 | |
| 1986 | return 0; |
| 1987 | |
| 1988 | map_error: |
| 1989 | return -errno; |
| 1990 | |
| 1991 | } |
| 1992 | |
| 1993 | /* It will update the fields offsets too */ |
| 1994 | void ltt_update_event_size(LttTracefile *tf) |
| 1995 | { |
| 1996 | off_t size = 0; |
| 1997 | |
| 1998 | /* Specific handling of core events : necessary to read the facility control |
| 1999 | * tracefile. */ |
| 2000 | LttFacility *f = ltt_trace_get_facility_by_num(tf->trace, |
| 2001 | tf->event.facility_id); |
| 2002 | |
| 2003 | if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) { |
| 2004 | switch((enum ltt_core_events)tf->event.event_id) { |
| 2005 | case LTT_EVENT_FACILITY_LOAD: |
| 2006 | size = strlen((char*)tf->event.data) + 1; |
| 2007 | //g_debug("Update Event facility load of facility %s", (char*)tf->event.data); |
| 2008 | size += ltt_align(size, sizeof(guint32), tf->has_alignment); |
| 2009 | size += sizeof(struct LttFacilityLoad); |
| 2010 | break; |
| 2011 | case LTT_EVENT_FACILITY_UNLOAD: |
| 2012 | //g_debug("Update Event facility unload"); |
| 2013 | size = sizeof(struct LttFacilityUnload); |
| 2014 | break; |
| 2015 | case LTT_EVENT_STATE_DUMP_FACILITY_LOAD: |
| 2016 | size = strlen((char*)tf->event.data) + 1; |
| 2017 | size += ltt_align(size, sizeof(guint32), tf->has_alignment); |
| 2018 | //g_debug("Update Event facility load state dump of facility %s", |
| 2019 | // (char*)tf->event.data); |
| 2020 | size += sizeof(struct LttStateDumpFacilityLoad); |
| 2021 | break; |
| 2022 | case LTT_EVENT_HEARTBEAT: |
| 2023 | //g_debug("Update Event heartbeat"); |
| 2024 | size = sizeof(TimeHeartbeat); |
| 2025 | break; |
| 2026 | default: |
| 2027 | g_warning("Error in getting event size : tracefile %s, " |
| 2028 | "unknown event id %hhu in core facility.", |
| 2029 | g_quark_to_string(tf->name), |
| 2030 | tf->event.event_id); |
| 2031 | goto event_id_error; |
| 2032 | |
| 2033 | } |
| 2034 | } else { |
| 2035 | if(!f->exists) { |
| 2036 | g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s", |
| 2037 | tf->event.facility_id, |
| 2038 | tf->event.facility_id, |
| 2039 | g_quark_to_string(tf->name)); |
| 2040 | goto facility_error; |
| 2041 | } |
| 2042 | |
| 2043 | LttEventType *event_type = |
| 2044 | ltt_facility_eventtype_get(f, tf->event.event_id); |
| 2045 | |
| 2046 | if(!event_type) { |
| 2047 | g_warning("Unknown event id %hhu in facility %s in tracefile %s", |
| 2048 | tf->event.event_id, |
| 2049 | g_quark_to_string(f->name), |
| 2050 | g_quark_to_string(tf->name)); |
| 2051 | goto event_type_error; |
| 2052 | } |
| 2053 | |
| 2054 | /* Compute the dynamic offsets */ |
| 2055 | compute_offsets(tf, f, event_type, &size, tf->event.data); |
| 2056 | |
| 2057 | //g_debug("Event root field : f.e %hhu.%hhu size %zd", |
| 2058 | // tf->event.facility_id, |
| 2059 | // tf->event.event_id, size); |
| 2060 | } |
| 2061 | |
| 2062 | tf->event.data_size = size; |
| 2063 | |
| 2064 | /* Check consistency between kernel and LTTV structure sizes */ |
| 2065 | if(tf->event.event_size == 0xFFFF) { |
| 2066 | /* Event size too big to fit in the event size field */ |
| 2067 | tf->event.event_size = tf->event.data_size; |
| 2068 | } |
| 2069 | g_assert(tf->event.data_size == tf->event.event_size); |
| 2070 | |
| 2071 | return; |
| 2072 | |
| 2073 | facility_error: |
| 2074 | event_type_error: |
| 2075 | event_id_error: |
| 2076 | if(tf->event.event_size == 0xFFFF) { |
| 2077 | g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes"); |
| 2078 | } |
| 2079 | /* The facility is unknown : use the kernel information about this event |
| 2080 | * to jump over it. */ |
| 2081 | tf->event.data_size = tf->event.event_size; |
| 2082 | } |
| 2083 | |
| 2084 | |
| 2085 | /* Take the tf current event offset and use the event facility id and event id |
| 2086 | * to figure out where is the next event offset. |
| 2087 | * |
| 2088 | * This is an internal function not aiming at being used elsewhere : it will |
| 2089 | * not jump over the current block limits. Please consider using |
| 2090 | * ltt_tracefile_read to do this. |
| 2091 | * |
| 2092 | * Returns 0 on success |
| 2093 | * ERANGE if we are at the end of the buffer. |
| 2094 | * ENOPROTOOPT if an error occured when getting the current event size. |
| 2095 | */ |
| 2096 | static int ltt_seek_next_event(LttTracefile *tf) |
| 2097 | { |
| 2098 | int ret = 0; |
| 2099 | void *pos; |
| 2100 | |
| 2101 | /* seek over the buffer header if we are at the buffer start */ |
| 2102 | if(tf->event.offset == 0) { |
| 2103 | tf->event.offset += tf->buffer_header_size; |
| 2104 | |
| 2105 | if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) { |
| 2106 | ret = ERANGE; |
| 2107 | } |
| 2108 | goto found; |
| 2109 | } |
| 2110 | |
| 2111 | |
| 2112 | pos = tf->event.data; |
| 2113 | |
| 2114 | if(tf->event.data_size < 0) goto error; |
| 2115 | |
| 2116 | pos += (size_t)tf->event.data_size; |
| 2117 | |
| 2118 | tf->event.offset = pos - tf->buffer.head; |
| 2119 | |
| 2120 | if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) { |
| 2121 | ret = ERANGE; |
| 2122 | goto found; |
| 2123 | } |
| 2124 | g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size); |
| 2125 | |
| 2126 | found: |
| 2127 | return ret; |
| 2128 | |
| 2129 | error: |
| 2130 | g_error("Error in ltt_seek_next_event for tracefile %s", |
| 2131 | g_quark_to_string(tf->name)); |
| 2132 | return ENOPROTOOPT; |
| 2133 | } |
| 2134 | |
| 2135 | #if 0 |
| 2136 | /***************************************************************************** |
| 2137 | *Function name |
| 2138 | * calc_nsecs_per_cycle : calculate nsecs per cycle for current block |
| 2139 | * |
| 2140 | * 1.0 / (freq(khz) *1000) * 1000000000 |
| 2141 | *Input Params |
| 2142 | * t : tracefile |
| 2143 | ****************************************************************************/ |
| 2144 | /* from timer_tsc.c */ |
| 2145 | #define CYC2NS_SCALE_FACTOR 10 |
| 2146 | static guint32 calc_nsecs_per_cycle(LttTracefile * tf) |
| 2147 | { |
| 2148 | //return 1e6 / (double)tf->buffer.freq; |
| 2149 | guint32 cpu_mhz = tf->buffer.freq / 1000; |
| 2150 | guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz; |
| 2151 | |
| 2152 | return cyc2ns_scale; |
| 2153 | // return 1e6 / (double)tf->buffer.freq; |
| 2154 | } |
| 2155 | |
| 2156 | static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles) |
| 2157 | { |
| 2158 | return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR; |
| 2159 | } |
| 2160 | #endif //0 |
| 2161 | |
| 2162 | #if 0 |
| 2163 | void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD) |
| 2164 | { |
| 2165 | LttField * rootFld = evT->root_field; |
| 2166 | // rootFld->base_address = evD; |
| 2167 | |
| 2168 | if(likely(rootFld)) |
| 2169 | rootFld->field_size = getFieldtypeSize(tf, evT->facility, |
| 2170 | evT, 0,0,rootFld, evD); |
| 2171 | } |
| 2172 | #endif //0 |
| 2173 | #if 0 |
| 2174 | /***************************************************************************** |
| 2175 | *Function name |
| 2176 | * set_fields_offsets : set the precomputable offset of the fields |
| 2177 | *Input params |
| 2178 | * tracefile : opened trace file |
| 2179 | * event_type : the event type |
| 2180 | ****************************************************************************/ |
| 2181 | |
| 2182 | void set_fields_offsets(LttTracefile *tf, LttEventType *event_type) |
| 2183 | { |
| 2184 | LttField *field = event_type->root_field; |
| 2185 | enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED; |
| 2186 | |
| 2187 | if(likely(field)) |
| 2188 | preset_field_type_size(tf, event_type, 0, 0, |
| 2189 | &fixed_root, &fixed_parent, |
| 2190 | field); |
| 2191 | |
| 2192 | } |
| 2193 | #endif //0 |
| 2194 | |
| 2195 | |
| 2196 | /***************************************************************************** |
| 2197 | *Function name |
| 2198 | * get_alignment : Get the alignment needed for a field. |
| 2199 | *Input params |
| 2200 | * field : field |
| 2201 | * |
| 2202 | * returns : The size on which it must be aligned. |
| 2203 | * |
| 2204 | ****************************************************************************/ |
| 2205 | off_t get_alignment(LttField *field) |
| 2206 | { |
| 2207 | LttType *type = &field->field_type; |
| 2208 | |
| 2209 | switch(type->type_class) { |
| 2210 | case LTT_INT_FIXED: |
| 2211 | case LTT_UINT_FIXED: |
| 2212 | case LTT_POINTER: |
| 2213 | case LTT_CHAR: |
| 2214 | case LTT_UCHAR: |
| 2215 | case LTT_SHORT: |
| 2216 | case LTT_USHORT: |
| 2217 | case LTT_INT: |
| 2218 | case LTT_UINT: |
| 2219 | case LTT_LONG: |
| 2220 | case LTT_ULONG: |
| 2221 | case LTT_SIZE_T: |
| 2222 | case LTT_SSIZE_T: |
| 2223 | case LTT_OFF_T: |
| 2224 | case LTT_FLOAT: |
| 2225 | case LTT_ENUM: |
| 2226 | /* Align offset on type size */ |
| 2227 | return field->field_size; |
| 2228 | break; |
| 2229 | case LTT_STRING: |
| 2230 | return 0; |
| 2231 | break; |
| 2232 | case LTT_ARRAY: |
| 2233 | g_assert(type->fields->len == 1); |
| 2234 | { |
| 2235 | LttField *child = &g_array_index(type->fields, LttField, 0); |
| 2236 | return get_alignment(child); |
| 2237 | } |
| 2238 | break; |
| 2239 | case LTT_SEQUENCE: |
| 2240 | g_assert(type->fields->len == 2); |
| 2241 | { |
| 2242 | off_t localign = 0; |
| 2243 | LttField *child = &g_array_index(type->fields, LttField, 0); |
| 2244 | |
| 2245 | localign = max(localign, get_alignment(child)); |
| 2246 | |
| 2247 | child = &g_array_index(type->fields, LttField, 1); |
| 2248 | localign = max(localign, get_alignment(child)); |
| 2249 | |
| 2250 | return localign; |
| 2251 | } |
| 2252 | break; |
| 2253 | case LTT_STRUCT: |
| 2254 | case LTT_UNION: |
| 2255 | { |
| 2256 | guint i; |
| 2257 | off_t localign = 0; |
| 2258 | |
| 2259 | for(i=0; i<type->fields->len; i++) { |
| 2260 | LttField *child = &g_array_index(type->fields, LttField, i); |
| 2261 | localign = max(localign, get_alignment(child)); |
| 2262 | } |
| 2263 | return localign; |
| 2264 | } |
| 2265 | break; |
| 2266 | case LTT_NONE: |
| 2267 | default: |
| 2268 | g_error("get_alignment : unknown type"); |
| 2269 | } |
| 2270 | |
| 2271 | } |
| 2272 | |
| 2273 | /***************************************************************************** |
| 2274 | *Function name |
| 2275 | * field_compute_static_size : Determine the size of fields known by their |
| 2276 | * sole definition. Unions, arrays and struct sizes might be known, but |
| 2277 | * the parser does not give that information. |
| 2278 | *Input params |
| 2279 | * tf : tracefile |
| 2280 | * field : field |
| 2281 | * |
| 2282 | ****************************************************************************/ |
| 2283 | |
| 2284 | void field_compute_static_size(LttFacility *fac, LttField *field) |
| 2285 | { |
| 2286 | LttType *type = &field->field_type; |
| 2287 | |
| 2288 | switch(type->type_class) { |
| 2289 | case LTT_INT_FIXED: |
| 2290 | case LTT_UINT_FIXED: |
| 2291 | case LTT_POINTER: |
| 2292 | case LTT_CHAR: |
| 2293 | case LTT_UCHAR: |
| 2294 | case LTT_SHORT: |
| 2295 | case LTT_USHORT: |
| 2296 | case LTT_INT: |
| 2297 | case LTT_UINT: |
| 2298 | case LTT_LONG: |
| 2299 | case LTT_ULONG: |
| 2300 | case LTT_SIZE_T: |
| 2301 | case LTT_SSIZE_T: |
| 2302 | case LTT_OFF_T: |
| 2303 | case LTT_FLOAT: |
| 2304 | case LTT_ENUM: |
| 2305 | case LTT_STRING: |
| 2306 | /* nothing to do */ |
| 2307 | break; |
| 2308 | case LTT_ARRAY: |
| 2309 | /* note this : array type size is the number of elements in the array, |
| 2310 | * while array field size of the length of the array in bytes */ |
| 2311 | g_assert(type->fields->len == 1); |
| 2312 | { |
| 2313 | LttField *child = &g_array_index(type->fields, LttField, 0); |
| 2314 | field_compute_static_size(fac, child); |
| 2315 | |
| 2316 | if(child->field_size != 0) { |
| 2317 | field->field_size = type->size * child->field_size; |
| 2318 | field->dynamic_offsets = g_array_sized_new(FALSE, TRUE, |
| 2319 | sizeof(off_t), type->size); |
| 2320 | } else { |
| 2321 | field->field_size = 0; |
| 2322 | } |
| 2323 | } |
| 2324 | break; |
| 2325 | case LTT_SEQUENCE: |
| 2326 | g_assert(type->fields->len == 2); |
| 2327 | { |
| 2328 | off_t local_offset = 0; |
| 2329 | LttField *child = &g_array_index(type->fields, LttField, 1); |
| 2330 | field_compute_static_size(fac, child); |
| 2331 | field->field_size = 0; |
| 2332 | type->size = 0; |
| 2333 | if(child->field_size != 0) { |
| 2334 | field->dynamic_offsets = g_array_sized_new(FALSE, TRUE, |
| 2335 | sizeof(off_t), SEQUENCE_AVG_ELEMENTS); |
| 2336 | } |
| 2337 | } |
| 2338 | break; |
| 2339 | case LTT_STRUCT: |
| 2340 | case LTT_UNION: |
| 2341 | { |
| 2342 | guint i; |
| 2343 | for(i=0;i<type->fields->len;i++) { |
| 2344 | LttField *child = &g_array_index(type->fields, LttField, i); |
| 2345 | field_compute_static_size(fac, child); |
| 2346 | if(child->field_size != 0) { |
| 2347 | type->size += ltt_align(type->size, get_alignment(child), |
| 2348 | fac->alignment); |
| 2349 | type->size += child->field_size; |
| 2350 | } else { |
| 2351 | /* As soon as we find a child with variable size, we have |
| 2352 | * a variable size */ |
| 2353 | type->size = 0; |
| 2354 | break; |
| 2355 | } |
| 2356 | } |
| 2357 | field->field_size = type->size; |
| 2358 | } |
| 2359 | break; |
| 2360 | default: |
| 2361 | g_error("field_static_size : unknown type"); |
| 2362 | } |
| 2363 | |
| 2364 | } |
| 2365 | |
| 2366 | |
| 2367 | |
| 2368 | /***************************************************************************** |
| 2369 | *Function name |
| 2370 | * precompute_fields_offsets : set the precomputable offset of the fields |
| 2371 | *Input params |
| 2372 | * fac : facility |
| 2373 | * field : the field |
| 2374 | * offset : pointer to the current offset, must be incremented |
| 2375 | * |
| 2376 | * return : 1 : found a variable length field, stop the processing. |
| 2377 | * 0 otherwise. |
| 2378 | ****************************************************************************/ |
| 2379 | |
| 2380 | |
| 2381 | gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset) |
| 2382 | { |
| 2383 | LttType *type = &field->field_type; |
| 2384 | |
| 2385 | switch(type->type_class) { |
| 2386 | case LTT_INT_FIXED: |
| 2387 | case LTT_UINT_FIXED: |
| 2388 | case LTT_POINTER: |
| 2389 | case LTT_CHAR: |
| 2390 | case LTT_UCHAR: |
| 2391 | case LTT_SHORT: |
| 2392 | case LTT_USHORT: |
| 2393 | case LTT_INT: |
| 2394 | case LTT_UINT: |
| 2395 | case LTT_LONG: |
| 2396 | case LTT_ULONG: |
| 2397 | case LTT_SIZE_T: |
| 2398 | case LTT_SSIZE_T: |
| 2399 | case LTT_OFF_T: |
| 2400 | case LTT_FLOAT: |
| 2401 | case LTT_ENUM: |
| 2402 | g_assert(field->field_size != 0); |
| 2403 | /* Align offset on type size */ |
| 2404 | *offset += ltt_align(*offset, get_alignment(field), |
| 2405 | fac->alignment); |
| 2406 | /* remember offset */ |
| 2407 | field->offset_root = *offset; |
| 2408 | field->fixed_root = FIELD_FIXED; |
| 2409 | /* Increment offset */ |
| 2410 | *offset += field->field_size; |
| 2411 | return 0; |
| 2412 | break; |
| 2413 | case LTT_STRING: |
| 2414 | field->offset_root = *offset; |
| 2415 | field->fixed_root = FIELD_FIXED; |
| 2416 | return 1; |
| 2417 | break; |
| 2418 | case LTT_ARRAY: |
| 2419 | g_assert(type->fields->len == 1); |
| 2420 | { |
| 2421 | LttField *child = &g_array_index(type->fields, LttField, 0); |
| 2422 | |
| 2423 | *offset += ltt_align(*offset, get_alignment(field), |
| 2424 | fac->alignment); |
| 2425 | |
| 2426 | /* remember offset */ |
| 2427 | field->offset_root = *offset; |
| 2428 | field->array_offset = *offset; |
| 2429 | field->fixed_root = FIELD_FIXED; |
| 2430 | |
| 2431 | /* Let the child be variable */ |
| 2432 | //precompute_fields_offsets(tf, child, offset); |
| 2433 | |
| 2434 | if(field->field_size != 0) { |
| 2435 | /* Increment offset */ |
| 2436 | /* field_size is the array size in bytes */ |
| 2437 | *offset += field->field_size; |
| 2438 | return 0; |
| 2439 | } else { |
| 2440 | return 1; |
| 2441 | } |
| 2442 | } |
| 2443 | break; |
| 2444 | case LTT_SEQUENCE: |
| 2445 | g_assert(type->fields->len == 2); |
| 2446 | { |
| 2447 | LttField *child; |
| 2448 | guint ret; |
| 2449 | |
| 2450 | *offset += ltt_align(*offset, get_alignment(field), |
| 2451 | fac->alignment); |
| 2452 | |
| 2453 | /* remember offset */ |
| 2454 | field->offset_root = *offset; |
| 2455 | field->fixed_root = FIELD_FIXED; |
| 2456 | |
| 2457 | child = &g_array_index(type->fields, LttField, 0); |
| 2458 | ret = precompute_fields_offsets(fac, child, offset); |
| 2459 | g_assert(ret == 0); /* Seq len cannot have variable len */ |
| 2460 | |
| 2461 | child = &g_array_index(type->fields, LttField, 1); |
| 2462 | *offset += ltt_align(*offset, get_alignment(child), |
| 2463 | fac->alignment); |
| 2464 | field->array_offset = *offset; |
| 2465 | /* Let the child be variable. */ |
| 2466 | //ret = precompute_fields_offsets(fac, child, offset); |
| 2467 | |
| 2468 | /* Cannot precompute fields offsets of sequence members, and has |
| 2469 | * variable length. */ |
| 2470 | return 1; |
| 2471 | } |
| 2472 | break; |
| 2473 | case LTT_STRUCT: |
| 2474 | { |
| 2475 | LttField *child; |
| 2476 | guint i; |
| 2477 | gint ret=0; |
| 2478 | |
| 2479 | *offset += ltt_align(*offset, get_alignment(field), |
| 2480 | fac->alignment); |
| 2481 | /* remember offset */ |
| 2482 | field->offset_root = *offset; |
| 2483 | field->fixed_root = FIELD_FIXED; |
| 2484 | |
| 2485 | for(i=0; i< type->fields->len; i++) { |
| 2486 | child = &g_array_index(type->fields, LttField, i); |
| 2487 | ret = precompute_fields_offsets(fac, child, offset); |
| 2488 | |
| 2489 | if(ret) break; |
| 2490 | } |
| 2491 | return ret; |
| 2492 | } |
| 2493 | break; |
| 2494 | case LTT_UNION: |
| 2495 | { |
| 2496 | LttField *child; |
| 2497 | guint i; |
| 2498 | gint ret=0; |
| 2499 | |
| 2500 | *offset += ltt_align(*offset, get_alignment(field), |
| 2501 | fac->alignment); |
| 2502 | /* remember offset */ |
| 2503 | field->offset_root = *offset; |
| 2504 | field->fixed_root = FIELD_FIXED; |
| 2505 | |
| 2506 | for(i=0; i< type->fields->len; i++) { |
| 2507 | *offset = field->offset_root; |
| 2508 | child = &g_array_index(type->fields, LttField, i); |
| 2509 | ret = precompute_fields_offsets(fac, child, offset); |
| 2510 | |
| 2511 | if(ret) break; |
| 2512 | } |
| 2513 | *offset = field->offset_root + field->field_size; |
| 2514 | return ret; |
| 2515 | } |
| 2516 | |
| 2517 | break; |
| 2518 | case LTT_NONE: |
| 2519 | default: |
| 2520 | g_error("precompute_fields_offsets : unknown type"); |
| 2521 | return 1; |
| 2522 | } |
| 2523 | |
| 2524 | } |
| 2525 | |
| 2526 | |
| 2527 | /***************************************************************************** |
| 2528 | *Function name |
| 2529 | * precompute_offsets : set the precomputable offset of an event type |
| 2530 | *Input params |
| 2531 | * tf : tracefile |
| 2532 | * event : event type |
| 2533 | * |
| 2534 | ****************************************************************************/ |
| 2535 | void precompute_offsets(LttFacility *fac, LttEventType *event) |
| 2536 | { |
| 2537 | guint i; |
| 2538 | off_t offset = 0; |
| 2539 | gint ret; |
| 2540 | |
| 2541 | /* First, compute the size of fixed size fields. Will determine size for |
| 2542 | * arrays, struct and unions, which is not done by the parser */ |
| 2543 | for(i=0; i<event->fields->len; i++) { |
| 2544 | LttField *field = &g_array_index(event->fields, LttField, i); |
| 2545 | field_compute_static_size(fac, field); |
| 2546 | } |
| 2547 | |
| 2548 | /* Precompute all known offsets */ |
| 2549 | for(i=0; i<event->fields->len; i++) { |
| 2550 | LttField *field = &g_array_index(event->fields, LttField, i); |
| 2551 | ret = precompute_fields_offsets(fac, field, &offset); |
| 2552 | if(ret) break; |
| 2553 | } |
| 2554 | } |
| 2555 | |
| 2556 | |
| 2557 | |
| 2558 | |
| 2559 | /***************************************************************************** |
| 2560 | *Function name |
| 2561 | * preset_field_type_size : set the fixed sizes of the field type |
| 2562 | *Input params |
| 2563 | * tf : tracefile |
| 2564 | * event_type : event type |
| 2565 | * offset_root : offset from the root |
| 2566 | * offset_parent : offset from the parent |
| 2567 | * fixed_root : Do we know a fixed offset to the root ? |
| 2568 | * fixed_parent : Do we know a fixed offset to the parent ? |
| 2569 | * field : field |
| 2570 | ****************************************************************************/ |
| 2571 | |
| 2572 | |
| 2573 | |
| 2574 | // preset the fixed size offsets. Calculate them just like genevent-new : an |
| 2575 | // increment of a *to value that represents the offset from the start of the |
| 2576 | // event data. |
| 2577 | // The preset information is : offsets up to (and including) the first element |
| 2578 | // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET". |
| 2579 | #if 0 |
| 2580 | void preset_field_type_size(LttTracefile *tf, LttEventType *event_type, |
| 2581 | off_t offset_root, off_t offset_parent, |
| 2582 | enum field_status *fixed_root, enum field_status *fixed_parent, |
| 2583 | LttField *field) |
| 2584 | { |
| 2585 | enum field_status local_fixed_root, local_fixed_parent; |
| 2586 | guint i; |
| 2587 | LttType *type; |
| 2588 | |
| 2589 | g_assert(field->fixed_root == FIELD_UNKNOWN); |
| 2590 | g_assert(field->fixed_parent == FIELD_UNKNOWN); |
| 2591 | g_assert(field->fixed_size == FIELD_UNKNOWN); |
| 2592 | |
| 2593 | type = field->field_type; |
| 2594 | |
| 2595 | field->fixed_root = *fixed_root; |
| 2596 | if(field->fixed_root == FIELD_FIXED) |
| 2597 | field->offset_root = offset_root; |
| 2598 | else |
| 2599 | field->offset_root = 0; |
| 2600 | |
| 2601 | field->fixed_parent = *fixed_parent; |
| 2602 | if(field->fixed_parent == FIELD_FIXED) |
| 2603 | field->offset_parent = offset_parent; |
| 2604 | else |
| 2605 | field->offset_parent = 0; |
| 2606 | |
| 2607 | size_t current_root_offset; |
| 2608 | size_t current_offset; |
| 2609 | enum field_status current_child_status, final_child_status; |
| 2610 | size_t max_size; |
| 2611 | |
| 2612 | switch(type->type_class) { |
| 2613 | case LTT_INT: |
| 2614 | case LTT_UINT: |
| 2615 | case LTT_FLOAT: |
| 2616 | case LTT_ENUM: |
| 2617 | field->field_size = ltt_type_size(tf->trace, type); |
| 2618 | field->fixed_size = FIELD_FIXED; |
| 2619 | break; |
| 2620 | case LTT_POINTER: |
| 2621 | field->field_size = (off_t)event_type->facility->pointer_size; |
| 2622 | field->fixed_size = FIELD_FIXED; |
| 2623 | break; |
| 2624 | case LTT_LONG: |
| 2625 | case LTT_ULONG: |
| 2626 | field->field_size = (off_t)event_type->facility->long_size; |
| 2627 | field->fixed_size = FIELD_FIXED; |
| 2628 | break; |
| 2629 | case LTT_SIZE_T: |
| 2630 | case LTT_SSIZE_T: |
| 2631 | case LTT_OFF_T: |
| 2632 | field->field_size = (off_t)event_type->facility->size_t_size; |
| 2633 | field->fixed_size = FIELD_FIXED; |
| 2634 | break; |
| 2635 | case LTT_SEQUENCE: |
| 2636 | local_fixed_root = FIELD_VARIABLE; |
| 2637 | local_fixed_parent = FIELD_VARIABLE; |
| 2638 | preset_field_type_size(tf, event_type, |
| 2639 | 0, 0, |
| 2640 | &local_fixed_root, &local_fixed_parent, |
| 2641 | field->child[0]); |
| 2642 | field->fixed_size = FIELD_VARIABLE; |
| 2643 | field->field_size = 0; |
| 2644 | *fixed_root = FIELD_VARIABLE; |
| 2645 | *fixed_parent = FIELD_VARIABLE; |
| 2646 | break; |
| 2647 | case LTT_STRING: |
| 2648 | field->fixed_size = FIELD_VARIABLE; |
| 2649 | field->field_size = 0; |
| 2650 | *fixed_root = FIELD_VARIABLE; |
| 2651 | *fixed_parent = FIELD_VARIABLE; |
| 2652 | break; |
| 2653 | case LTT_ARRAY: |
| 2654 | local_fixed_root = FIELD_VARIABLE; |
| 2655 | local_fixed_parent = FIELD_VARIABLE; |
| 2656 | preset_field_type_size(tf, event_type, |
| 2657 | 0, 0, |
| 2658 | &local_fixed_root, &local_fixed_parent, |
| 2659 | field->child[0]); |
| 2660 | field->fixed_size = field->child[0]->fixed_size; |
| 2661 | if(field->fixed_size == FIELD_FIXED) { |
| 2662 | field->field_size = type->element_number * field->child[0]->field_size; |
| 2663 | } else { |
| 2664 | field->field_size = 0; |
| 2665 | *fixed_root = FIELD_VARIABLE; |
| 2666 | *fixed_parent = FIELD_VARIABLE; |
| 2667 | } |
| 2668 | break; |
| 2669 | case LTT_STRUCT: |
| 2670 | current_root_offset = field->offset_root; |
| 2671 | current_offset = 0; |
| 2672 | current_child_status = FIELD_FIXED; |
| 2673 | for(i=0;i<type->element_number;i++) { |
| 2674 | preset_field_type_size(tf, event_type, |
| 2675 | current_root_offset, current_offset, |
| 2676 | fixed_root, ¤t_child_status, |
| 2677 | field->child[i]); |
| 2678 | if(current_child_status == FIELD_FIXED) { |
| 2679 | current_root_offset += field->child[i]->field_size; |
| 2680 | current_offset += field->child[i]->field_size; |
| 2681 | } else { |
| 2682 | current_root_offset = 0; |
| 2683 | current_offset = 0; |
| 2684 | } |
| 2685 | } |
| 2686 | if(current_child_status != FIELD_FIXED) { |
| 2687 | *fixed_parent = current_child_status; |
| 2688 | field->field_size = 0; |
| 2689 | field->fixed_size = current_child_status; |
| 2690 | } else { |
| 2691 | field->field_size = current_offset; |
| 2692 | field->fixed_size = FIELD_FIXED; |
| 2693 | } |
| 2694 | break; |
| 2695 | case LTT_UNION: |
| 2696 | current_root_offset = field->offset_root; |
| 2697 | current_offset = 0; |
| 2698 | max_size = 0; |
| 2699 | final_child_status = FIELD_FIXED; |
| 2700 | for(i=0;i<type->element_number;i++) { |
| 2701 | enum field_status current_root_child_status = FIELD_FIXED; |
| 2702 | enum field_status current_child_status = FIELD_FIXED; |
| 2703 | preset_field_type_size(tf, event_type, |
| 2704 | current_root_offset, current_offset, |
| 2705 | ¤t_root_child_status, ¤t_child_status, |
| 2706 | field->child[i]); |
| 2707 | if(current_child_status != FIELD_FIXED) |
| 2708 | final_child_status = current_child_status; |
| 2709 | else |
| 2710 | max_size = max(max_size, field->child[i]->field_size); |
| 2711 | } |
| 2712 | if(final_child_status != FIELD_FIXED) { |
| 2713 | g_error("LTTV does not support variable size fields in unions."); |
| 2714 | /* This will stop the application. */ |
| 2715 | *fixed_root = final_child_status; |
| 2716 | *fixed_parent = final_child_status; |
| 2717 | field->field_size = 0; |
| 2718 | field->fixed_size = current_child_status; |
| 2719 | } else { |
| 2720 | field->field_size = max_size; |
| 2721 | field->fixed_size = FIELD_FIXED; |
| 2722 | } |
| 2723 | break; |
| 2724 | } |
| 2725 | |
| 2726 | } |
| 2727 | #endif //0 |
| 2728 | |
| 2729 | /***************************************************************************** |
| 2730 | *Function name |
| 2731 | * check_fields_compatibility : Check for compatibility between two fields : |
| 2732 | * do they use the same inner structure ? |
| 2733 | *Input params |
| 2734 | * event_type1 : event type |
| 2735 | * event_type2 : event type |
| 2736 | * field1 : field |
| 2737 | * field2 : field |
| 2738 | *Returns : 0 if identical |
| 2739 | * 1 if not. |
| 2740 | ****************************************************************************/ |
| 2741 | // this function checks for equality of field types. Therefore, it does not use |
| 2742 | // per se offsets. For instance, an aligned version of a structure is |
| 2743 | // compatible with an unaligned version of the same structure. |
| 2744 | gint check_fields_compatibility(LttEventType *event_type1, |
| 2745 | LttEventType *event_type2, |
| 2746 | LttField *field1, LttField *field2) |
| 2747 | { |
| 2748 | guint different = 0; |
| 2749 | LttType *type1; |
| 2750 | LttType *type2; |
| 2751 | |
| 2752 | if(field1 == NULL) { |
| 2753 | if(field2 == NULL) goto end; |
| 2754 | else { |
| 2755 | different = 1; |
| 2756 | goto end; |
| 2757 | } |
| 2758 | } else if(field2 == NULL) { |
| 2759 | different = 1; |
| 2760 | goto end; |
| 2761 | } |
| 2762 | |
| 2763 | type1 = &field1->field_type; |
| 2764 | type2 = &field2->field_type; |
| 2765 | |
| 2766 | if(type1->type_class != type2->type_class) { |
| 2767 | different = 1; |
| 2768 | goto end; |
| 2769 | } |
| 2770 | if(type1->network != type2->network) { |
| 2771 | different = 1; |
| 2772 | goto end; |
| 2773 | } |
| 2774 | |
| 2775 | switch(type1->type_class) { |
| 2776 | case LTT_INT_FIXED: |
| 2777 | case LTT_UINT_FIXED: |
| 2778 | case LTT_POINTER: |
| 2779 | case LTT_CHAR: |
| 2780 | case LTT_UCHAR: |
| 2781 | case LTT_SHORT: |
| 2782 | case LTT_USHORT: |
| 2783 | case LTT_INT: |
| 2784 | case LTT_UINT: |
| 2785 | case LTT_LONG: |
| 2786 | case LTT_ULONG: |
| 2787 | case LTT_SIZE_T: |
| 2788 | case LTT_SSIZE_T: |
| 2789 | case LTT_OFF_T: |
| 2790 | case LTT_FLOAT: |
| 2791 | case LTT_ENUM: |
| 2792 | if(field1->field_size != field2->field_size) |
| 2793 | different = 1; |
| 2794 | break; |
| 2795 | case LTT_STRING: |
| 2796 | break; |
| 2797 | case LTT_ARRAY: |
| 2798 | { |
| 2799 | LttField *child1 = &g_array_index(type1->fields, LttField, 0); |
| 2800 | LttField *child2 = &g_array_index(type2->fields, LttField, 0); |
| 2801 | |
| 2802 | if(type1->size != type2->size) |
| 2803 | different = 1; |
| 2804 | if(check_fields_compatibility(event_type1, event_type2, child1, child2)) |
| 2805 | different = 1; |
| 2806 | } |
| 2807 | break; |
| 2808 | case LTT_SEQUENCE: |
| 2809 | { |
| 2810 | LttField *child1 = &g_array_index(type1->fields, LttField, 1); |
| 2811 | LttField *child2 = &g_array_index(type2->fields, LttField, 1); |
| 2812 | |
| 2813 | if(check_fields_compatibility(event_type1, event_type2, child1, child2)) |
| 2814 | different = 1; |
| 2815 | } |
| 2816 | break; |
| 2817 | case LTT_STRUCT: |
| 2818 | case LTT_UNION: |
| 2819 | { |
| 2820 | LttField *child; |
| 2821 | guint i; |
| 2822 | |
| 2823 | if(type1->fields->len != type2->fields->len) { |
| 2824 | different = 1; |
| 2825 | goto end; |
| 2826 | } |
| 2827 | |
| 2828 | for(i=0; i< type1->fields->len; i++) { |
| 2829 | LttField *child1; |
| 2830 | LttField *child2; |
| 2831 | child1 = &g_array_index(type1->fields, LttField, i); |
| 2832 | child2 = &g_array_index(type2->fields, LttField, i); |
| 2833 | different = check_fields_compatibility(event_type1, |
| 2834 | event_type2, child1, child2); |
| 2835 | |
| 2836 | if(different) break; |
| 2837 | } |
| 2838 | } |
| 2839 | break; |
| 2840 | case LTT_NONE: |
| 2841 | default: |
| 2842 | g_error("precompute_fields_offsets : unknown type"); |
| 2843 | } |
| 2844 | |
| 2845 | end: |
| 2846 | return different; |
| 2847 | } |
| 2848 | |
| 2849 | |
| 2850 | #if 0 |
| 2851 | gint check_fields_compatibility(LttEventType *event_type1, |
| 2852 | LttEventType *event_type2, |
| 2853 | LttField *field1, LttField *field2) |
| 2854 | { |
| 2855 | guint different = 0; |
| 2856 | guint i; |
| 2857 | LttType *type1; |
| 2858 | LttType *type2; |
| 2859 | |
| 2860 | if(field1 == NULL) { |
| 2861 | if(field2 == NULL) goto end; |
| 2862 | else { |
| 2863 | different = 1; |
| 2864 | goto end; |
| 2865 | } |
| 2866 | } else if(field2 == NULL) { |
| 2867 | different = 1; |
| 2868 | goto end; |
| 2869 | } |
| 2870 | |
| 2871 | g_assert(field1->fixed_root != FIELD_UNKNOWN); |
| 2872 | g_assert(field2->fixed_root != FIELD_UNKNOWN); |
| 2873 | g_assert(field1->fixed_parent != FIELD_UNKNOWN); |
| 2874 | g_assert(field2->fixed_parent != FIELD_UNKNOWN); |
| 2875 | g_assert(field1->fixed_size != FIELD_UNKNOWN); |
| 2876 | g_assert(field2->fixed_size != FIELD_UNKNOWN); |
| 2877 | |
| 2878 | type1 = field1->field_type; |
| 2879 | type2 = field2->field_type; |
| 2880 | |
| 2881 | if(type1->type_class != type2->type_class) { |
| 2882 | different = 1; |
| 2883 | goto end; |
| 2884 | } |
| 2885 | if(type1->element_name != type2->element_name) { |
| 2886 | different = 1; |
| 2887 | goto end; |
| 2888 | } |
| 2889 | |
| 2890 | switch(type1->type_class) { |
| 2891 | case LTT_INT: |
| 2892 | case LTT_UINT: |
| 2893 | case LTT_FLOAT: |
| 2894 | case LTT_POINTER: |
| 2895 | case LTT_LONG: |
| 2896 | case LTT_ULONG: |
| 2897 | case LTT_SIZE_T: |
| 2898 | case LTT_SSIZE_T: |
| 2899 | case LTT_OFF_T: |
| 2900 | if(field1->field_size != field2->field_size) { |
| 2901 | different = 1; |
| 2902 | goto end; |
| 2903 | } |
| 2904 | break; |
| 2905 | case LTT_ENUM: |
| 2906 | if(type1->element_number != type2->element_number) { |
| 2907 | different = 1; |
| 2908 | goto end; |
| 2909 | } |
| 2910 | for(i=0;i<type1->element_number;i++) { |
| 2911 | if(type1->enum_strings[i] != type2->enum_strings[i]) { |
| 2912 | different = 1; |
| 2913 | goto end; |
| 2914 | } |
| 2915 | } |
| 2916 | break; |
| 2917 | case LTT_SEQUENCE: |
| 2918 | /* Two elements : size and child */ |
| 2919 | g_assert(type1->element_number != type2->element_number); |
| 2920 | for(i=0;i<type1->element_number;i++) { |
| 2921 | if(check_fields_compatibility(event_type1, event_type2, |
| 2922 | field1->child[0], field2->child[0])) { |
| 2923 | different = 1; |
| 2924 | goto end; |
| 2925 | } |
| 2926 | } |
| 2927 | break; |
| 2928 | case LTT_STRING: |
| 2929 | break; |
| 2930 | case LTT_ARRAY: |
| 2931 | if(field1->field_size != field2->field_size) { |
| 2932 | different = 1; |
| 2933 | goto end; |
| 2934 | } |
| 2935 | /* Two elements : size and child */ |
| 2936 | g_assert(type1->element_number != type2->element_number); |
| 2937 | for(i=0;i<type1->element_number;i++) { |
| 2938 | if(check_fields_compatibility(event_type1, event_type2, |
| 2939 | field1->child[0], field2->child[0])) { |
| 2940 | different = 1; |
| 2941 | goto end; |
| 2942 | } |
| 2943 | } |
| 2944 | break; |
| 2945 | case LTT_STRUCT: |
| 2946 | case LTT_UNION: |
| 2947 | if(type1->element_number != type2->element_number) { |
| 2948 | different = 1; |
| 2949 | break; |
| 2950 | } |
| 2951 | for(i=0;i<type1->element_number;i++) { |
| 2952 | if(check_fields_compatibility(event_type1, event_type2, |
| 2953 | field1->child[0], field2->child[0])) { |
| 2954 | different = 1; |
| 2955 | goto end; |
| 2956 | } |
| 2957 | } |
| 2958 | break; |
| 2959 | } |
| 2960 | end: |
| 2961 | return different; |
| 2962 | } |
| 2963 | #endif //0 |
| 2964 | |
| 2965 | |
| 2966 | /***************************************************************************** |
| 2967 | *Function name |
| 2968 | * ltt_get_int : get an integer number |
| 2969 | *Input params |
| 2970 | * reverse_byte_order: must we reverse the byte order ? |
| 2971 | * size : the size of the integer |
| 2972 | * ptr : the data pointer |
| 2973 | *Return value |
| 2974 | * gint64 : a 64 bits integer |
| 2975 | ****************************************************************************/ |
| 2976 | |
| 2977 | gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data) |
| 2978 | { |
| 2979 | gint64 val; |
| 2980 | |
| 2981 | switch(size) { |
| 2982 | case 1: val = *((gint8*)data); break; |
| 2983 | case 2: val = ltt_get_int16(reverse_byte_order, data); break; |
| 2984 | case 4: val = ltt_get_int32(reverse_byte_order, data); break; |
| 2985 | case 8: val = ltt_get_int64(reverse_byte_order, data); break; |
| 2986 | default: val = ltt_get_int64(reverse_byte_order, data); |
| 2987 | g_critical("get_int : integer size %d unknown", size); |
| 2988 | break; |
| 2989 | } |
| 2990 | |
| 2991 | return val; |
| 2992 | } |
| 2993 | |
| 2994 | /***************************************************************************** |
| 2995 | *Function name |
| 2996 | * ltt_get_uint : get an unsigned integer number |
| 2997 | *Input params |
| 2998 | * reverse_byte_order: must we reverse the byte order ? |
| 2999 | * size : the size of the integer |
| 3000 | * ptr : the data pointer |
| 3001 | *Return value |
| 3002 | * guint64 : a 64 bits unsigned integer |
| 3003 | ****************************************************************************/ |
| 3004 | |
| 3005 | guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data) |
| 3006 | { |
| 3007 | guint64 val; |
| 3008 | |
| 3009 | switch(size) { |
| 3010 | case 1: val = *((gint8*)data); break; |
| 3011 | case 2: val = ltt_get_uint16(reverse_byte_order, data); break; |
| 3012 | case 4: val = ltt_get_uint32(reverse_byte_order, data); break; |
| 3013 | case 8: val = ltt_get_uint64(reverse_byte_order, data); break; |
| 3014 | default: val = ltt_get_uint64(reverse_byte_order, data); |
| 3015 | g_critical("get_uint : unsigned integer size %d unknown", |
| 3016 | size); |
| 3017 | break; |
| 3018 | } |
| 3019 | |
| 3020 | return val; |
| 3021 | } |
| 3022 | |
| 3023 | |
| 3024 | /* get the node name of the system */ |
| 3025 | |
| 3026 | char * ltt_trace_system_description_node_name (LttSystemDescription * s) |
| 3027 | { |
| 3028 | return s->node_name; |
| 3029 | } |
| 3030 | |
| 3031 | |
| 3032 | /* get the domain name of the system */ |
| 3033 | |
| 3034 | char * ltt_trace_system_description_domain_name (LttSystemDescription * s) |
| 3035 | { |
| 3036 | return s->domain_name; |
| 3037 | } |
| 3038 | |
| 3039 | |
| 3040 | /* get the description of the system */ |
| 3041 | |
| 3042 | char * ltt_trace_system_description_description (LttSystemDescription * s) |
| 3043 | { |
| 3044 | return s->description; |
| 3045 | } |
| 3046 | |
| 3047 | |
| 3048 | /* get the NTP corrected start time of the trace */ |
| 3049 | LttTime ltt_trace_start_time(LttTrace *t) |
| 3050 | { |
| 3051 | return t->start_time; |
| 3052 | } |
| 3053 | |
| 3054 | /* get the monotonic start time of the trace */ |
| 3055 | LttTime ltt_trace_start_time_monotonic(LttTrace *t) |
| 3056 | { |
| 3057 | return t->start_time_from_tsc; |
| 3058 | } |
| 3059 | |
| 3060 | LttTracefile *ltt_tracefile_new() |
| 3061 | { |
| 3062 | return g_new(LttTracefile, 1); |
| 3063 | } |
| 3064 | |
| 3065 | void ltt_tracefile_destroy(LttTracefile *tf) |
| 3066 | { |
| 3067 | g_free(tf); |
| 3068 | } |
| 3069 | |
| 3070 | void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src) |
| 3071 | { |
| 3072 | *dest = *src; |
| 3073 | } |
| 3074 | |
| 3075 | /* Before library loading... */ |
| 3076 | |
| 3077 | static void __attribute__((constructor)) init(void) |
| 3078 | { |
| 3079 | LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat"); |
| 3080 | LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat"); |
| 3081 | |
| 3082 | LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities"); |
| 3083 | } |
| 3084 | |