update verif
[lttv.git] / trunk / lttv / ltt / tracefile.c
CommitLineData
449cb9d7 1/* This file is part of the Linux Trace Toolkit viewer
3aee1200 2 * Copyright (C) 2005 Mathieu Desnoyers
449cb9d7 3 *
3aee1200 4 * Complete rewrite from the original version made by XangXiu Yang.
5 *
1b44b0b5 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.
449cb9d7 9 *
1b44b0b5 10 * This library is distributed in the hope that it will be useful,
449cb9d7 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
1b44b0b5 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
449cb9d7 14 *
1b44b0b5 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.
449cb9d7 19 */
20
4e4d11b3 21#ifdef HAVE_CONFIG_H
22#include <config.h>
23#endif
24
6cd62ccf 25#include <stdio.h>
26#include <fcntl.h>
8d1e6362 27#include <string.h>
28#include <dirent.h>
6cd62ccf 29#include <sys/stat.h>
30#include <sys/types.h>
8d1e6362 31#include <errno.h>
32#include <unistd.h>
42db9bf1 33#include <math.h>
cdf90f40 34#include <glib.h>
3aee1200 35#include <malloc.h>
36#include <sys/mman.h>
dd3a6d39 37#include <string.h>
6cd62ccf 38
ef35d837 39// For realpath
40#include <limits.h>
41#include <stdlib.h>
42
43
a5dcde2f 44#include <ltt/ltt.h>
45#include "ltt-private.h"
963b5f2d 46#include <ltt/trace.h>
c02ea99f 47#include <ltt/event.h>
1a2ceb63 48#include <ltt/ltt-types.h>
bb38a290 49#include <ltt/marker.h>
3aee1200 50
2fc874ab 51/* Tracefile names used in this file */
3aee1200 52
2fc874ab 53GQuark LTT_TRACEFILE_NAME_METADATA;
3aee1200 54
86005ded 55#ifndef g_open
56#define g_open open
57#endif
58
59
51b5991e 60#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 61
a1062ddd 62#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
3aee1200 63
64#ifndef g_debug
a1062ddd 65#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
3aee1200 66#endif
a1062ddd 67
45e14832 68#define g_close close
8959a0c8 69
b77d1b57 70/* Those macros must be called from within a function where page_size is a known
71 * variable */
72#define PAGE_MASK (~(page_size-1))
73#define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
74
9c731a50 75LttTrace *father_trace = NULL;
76
6cd62ccf 77/* set the offset of the fields belonging to the event,
78 need the information of the archecture */
f104d082 79//void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
eed2ef37 80//size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
6cd62ccf 81
2fc874ab 82#if 0
3aee1200 83/* get the size of the field type according to
84 * The facility size information. */
85static inline void preset_field_type_size(LttTracefile *tf,
86 LttEventType *event_type,
87 off_t offset_root, off_t offset_parent,
88 enum field_status *fixed_root, enum field_status *fixed_parent,
89 LttField *field);
f104d082 90#endif //0
6cd62ccf 91
3aee1200 92/* map a fixed size or a block information from the file (fd) */
93static gint map_block(LttTracefile * tf, guint block_num);
94
95/* calculate nsec per cycles for current block */
791dffa6 96#if 0
97static guint32 calc_nsecs_per_cycle(LttTracefile * t);
98static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
99#endif //0
6cd62ccf 100
3aee1200 101/* go to the next event */
102static int ltt_seek_next_event(LttTracefile *tf);
6cd62ccf 103
3c165eaf 104static int open_tracefiles(LttTrace *trace, gchar *root_path,
105 gchar *relative_path);
2fc874ab 106static int ltt_process_metadata_tracefile(LttTracefile *tf);
3c165eaf 107static void ltt_tracefile_time_span_get(LttTracefile *tf,
108 LttTime *start, LttTime *end);
109static void group_time_span_get(GQuark name, gpointer data, gpointer user_data);
110static gint map_block(LttTracefile * tf, guint block_num);
3c165eaf 111static void ltt_update_event_size(LttTracefile *tf);
91f8d488 112
113/* Enable event debugging */
114static int a_event_debug = 0;
115
116void ltt_event_debug(int state)
117{
118 a_event_debug = state;
119}
120
b7576a11 121/* trace can be NULL
122 *
123 * Return value : 0 success, 1 bad tracefile
124 */
64dd41a5 125static int parse_trace_header(ltt_subbuffer_header_t *header,
126 LttTracefile *tf, LttTrace *t)
b7576a11 127{
64dd41a5 128 if (header->magic_number == LTT_MAGIC_NUMBER)
b7576a11 129 tf->reverse_bo = 0;
64dd41a5 130 else if(header->magic_number == LTT_REV_MAGIC_NUMBER)
b7576a11 131 tf->reverse_bo = 1;
132 else /* invalid magic number, bad tracefile ! */
133 return 1;
64dd41a5 134
135 if(t) {
136 t->ltt_major_version = header->major_version;
137 t->ltt_minor_version = header->minor_version;
138 t->arch_size = header->arch_size;
139 }
140 tf->alignment = header->alignment;
141
b7576a11 142 /* Get float byte order : might be different from int byte order
143 * (or is set to 0 if the trace has no float (kernel trace)) */
64dd41a5 144 tf->float_word_order = 0;
b7576a11 145
64dd41a5 146 switch(header->major_version) {
b7576a11 147 case 0:
2fc874ab 148 case 1:
3c165eaf 149 g_warning("Unsupported trace version : %hhu.%hhu",
64dd41a5 150 header->major_version, header->minor_version);
3c165eaf 151 return 1;
152 break;
2fc874ab 153 case 2:
64dd41a5 154 switch(header->minor_version) {
1550fba6 155 case 1:
b7576a11 156 {
1550fba6 157 struct ltt_subbuffer_header_2_1 *vheader = header;
158 tf->buffer_header_size = ltt_subbuffer_header_size();
64dd41a5 159 tf->tscbits = 27;
160 tf->eventbits = 5;
2fc874ab 161 tf->tsc_mask = ((1ULL << tf->tscbits) - 1);
162 tf->tsc_mask_next_bit = (1ULL << tf->tscbits);
163
e939e5b2 164 if(t) {
165 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
166 &vheader->start_freq);
a3999b49 167 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
168 &vheader->freq_scale);
9c731a50 169 if(father_trace) {
170 t->start_freq = father_trace->start_freq;
171 t->freq_scale = father_trace->freq_scale;
64dd41a5 172 } else {
9c731a50 173 father_trace = t;
174 }
e939e5b2 175 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 176 &vheader->cycle_count_begin);
177 t->start_monotonic = 0;
e939e5b2 178 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
179 &vheader->start_time_sec);
180 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
181 &vheader->start_time_usec);
182 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
183
184 t->start_time_from_tsc = ltt_time_from_uint64(
c7f86478 185 (double)t->start_tsc
186 * (1000000000.0 / tf->trace->freq_scale)
62e4e7bf 187 / (double)t->start_freq);
e939e5b2 188 }
189 }
190 break;
b7576a11 191 default:
986e2a7c 192 g_warning("Unsupported trace version : %hhu.%hhu",
64dd41a5 193 header->major_version, header->minor_version);
b7576a11 194 return 1;
195 }
196 break;
b7576a11 197 default:
198 g_warning("Unsupported trace version : %hhu.%hhu",
64dd41a5 199 header->major_version, header->minor_version);
b7576a11 200 return 1;
201 }
b7576a11 202 return 0;
203}
204
205
206
6cd62ccf 207/*****************************************************************************
208 *Function name
963b5f2d 209 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 210 *Input params
963b5f2d 211 * t : the trace containing the tracefile
212 * fileName : path name of the trace file
3aee1200 213 * tf : the tracefile structure
6cd62ccf 214 *Return value
3aee1200 215 * : 0 for success, -1 otherwise.
6cd62ccf 216 ****************************************************************************/
217
8655430b 218static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
6cd62ccf 219{
963b5f2d 220 struct stat lTDFStat; /* Trace data file status */
64dd41a5 221 ltt_subbuffer_header_t *header;
b77d1b57 222 int page_size = getpagesize();
6cd62ccf 223
224 //open the file
d3d34f49 225 tf->long_name = g_quark_from_string(fileName);
963b5f2d 226 tf->trace = t;
3865ea09 227 tf->fd = open(fileName, O_RDONLY);
6cd62ccf 228 if(tf->fd < 0){
2a74fbf4 229 g_warning("Unable to open input data file %s\n", fileName);
3aee1200 230 goto end;
6cd62ccf 231 }
232
233 // Get the file's status
234 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 235 g_warning("Unable to get the status of the input data file %s\n", fileName);
3aee1200 236 goto close_file;
6cd62ccf 237 }
238
239 // Is the file large enough to contain a trace
823820eb 240 if(lTDFStat.st_size <
1550fba6 241 (off_t)(ltt_subbuffer_header_size())){
8710c6c7 242 g_print("The input data file %s does not contain a trace\n", fileName);
3aee1200 243 goto close_file;
244 }
245
246 /* Temporarily map the buffer start header to get trace information */
247 /* Multiple of pages aligned head */
b77d1b57 248 tf->buffer.head = mmap(0,
1550fba6 249 PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ,
3aee1200 250 MAP_PRIVATE, tf->fd, 0);
3865ea09 251 if(tf->buffer.head == MAP_FAILED) {
3aee1200 252 perror("Error in allocating memory for buffer of tracefile");
253 goto close_file;
6cd62ccf 254 }
f64fedd7 255 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 256
64dd41a5 257 header = (ltt_subbuffer_header_t *)tf->buffer.head;
6cd62ccf 258
64dd41a5 259 if(parse_trace_header(header, tf, NULL)) {
51551c6f 260 g_warning("parse_trace_header error");
261 goto unmap_file;
262 }
3aee1200 263
6cd62ccf 264 //store the size of the file
265 tf->file_size = lTDFStat.st_size;
f628823c 266 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
267 tf->num_blocks = tf->file_size / tf->buf_size;
268
269 if(munmap(tf->buffer.head,
1550fba6 270 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
f628823c 271 g_warning("unmap size : %u\n",
1550fba6 272 PAGE_ALIGN(ltt_subbuffer_header_size()));
f628823c 273 perror("munmap error");
274 g_assert(0);
275 }
3aee1200 276 tf->buffer.head = NULL;
6cd62ccf 277
963b5f2d 278 //read the first block
3aee1200 279 if(map_block(tf,0)) {
280 perror("Cannot map block for tracefile");
281 goto close_file;
282 }
283
284 return 0;
6cd62ccf 285
3aee1200 286 /* Error */
287unmap_file:
f628823c 288 if(munmap(tf->buffer.head,
1550fba6 289 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
f628823c 290 g_warning("unmap size : %u\n",
1550fba6 291 PAGE_ALIGN(ltt_subbuffer_header_size()));
f628823c 292 perror("munmap error");
293 g_assert(0);
294 }
3aee1200 295close_file:
3865ea09 296 close(tf->fd);
3aee1200 297end:
298 return -1;
6cd62ccf 299}
300
6cd62ccf 301
302/*****************************************************************************
303 *Function name
963b5f2d 304 * ltt_tracefile_close: close a trace file,
6cd62ccf 305 *Input params
963b5f2d 306 * t : tracefile which will be closed
6cd62ccf 307 ****************************************************************************/
308
8655430b 309static void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 310{
f628823c 311 int page_size = getpagesize();
312
3aee1200 313 if(t->buffer.head != NULL)
f628823c 314 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
315 g_warning("unmap size : %u\n",
316 PAGE_ALIGN(t->buf_size));
317 perror("munmap error");
318 g_assert(0);
319 }
320
3865ea09 321 close(t->fd);
963b5f2d 322}
6cd62ccf 323
8d1e6362 324/****************************************************************************
325 * get_absolute_pathname
803229fa 326 *
8d1e6362 327 * return the unique pathname in the system
328 *
ef35d837 329 * MD : Fixed this function so it uses realpath, dealing well with
330 * forgotten cases (.. were not used correctly before).
803229fa 331 *
963b5f2d 332 ****************************************************************************/
45e14832 333void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
9f797243 334{
9f797243 335 abs_pathname[0] = '\0';
803229fa 336
2fc874ab 337 if (realpath(pathname, abs_pathname) != NULL)
ef35d837 338 return;
339 else
340 {
8d1e6362 341 /* error, return the original path unmodified */
ef35d837 342 strcpy(abs_pathname, pathname);
9f797243 343 return;
344 }
ef35d837 345 return;
9f797243 346}
347
3aee1200 348/* Search for something like : .*_.*
349 *
350 * The left side is the name, the right side is the number.
351 */
352
8655430b 353static int get_tracefile_name_number(gchar *raw_name,
3aee1200 354 GQuark *name,
ae3d0f50 355 guint *num,
f64fedd7 356 gulong *tid,
357 gulong *pgid,
62e4e7bf 358 guint64 *creation)
6cd62ccf 359{
3aee1200 360 guint raw_name_len = strlen(raw_name);
361 gchar char_name[PATH_MAX];
3aee1200 362 int i;
363 int underscore_pos;
364 long int cpu_num;
365 gchar *endptr;
62e4e7bf 366 gchar *tmpptr;
3aee1200 367
368 for(i=raw_name_len-1;i>=0;i--) {
369 if(raw_name[i] == '_') break;
370 }
ae3d0f50 371 if(i==-1) { /* Either not found or name length is 0 */
62e4e7bf 372 /* This is a userspace tracefile */
373 strncpy(char_name, raw_name, raw_name_len);
374 char_name[raw_name_len] = '\0';
375 *name = g_quark_from_string(char_name);
376 *num = 0; /* unknown cpu */
377 for(i=0;i<raw_name_len;i++) {
378 if(raw_name[i] == '/') {
379 break;
380 }
381 }
382 i++;
383 for(;i<raw_name_len;i++) {
384 if(raw_name[i] == '/') {
385 break;
386 }
387 }
388 i++;
389 for(;i<raw_name_len;i++) {
390 if(raw_name[i] == '-') {
391 break;
392 }
393 }
394 if(i == raw_name_len) return -1;
395 i++;
396 tmpptr = &raw_name[i];
397 for(;i<raw_name_len;i++) {
398 if(raw_name[i] == '.') {
399 raw_name[i] = ' ';
400 break;
401 }
402 }
403 *tid = strtoul(tmpptr, &endptr, 10);
404 if(endptr == tmpptr)
405 return -1; /* No digit */
406 if(*tid == ULONG_MAX)
407 return -1; /* underflow / overflow */
408 i++;
409 tmpptr = &raw_name[i];
410 for(;i<raw_name_len;i++) {
411 if(raw_name[i] == '.') {
412 raw_name[i] = ' ';
413 break;
414 }
415 }
416 *pgid = strtoul(tmpptr, &endptr, 10);
417 if(endptr == tmpptr)
418 return -1; /* No digit */
419 if(*pgid == ULONG_MAX)
420 return -1; /* underflow / overflow */
421 i++;
422 tmpptr = &raw_name[i];
423 *creation = strtoull(tmpptr, &endptr, 10);
424 if(endptr == tmpptr)
425 return -1; /* No digit */
426 if(*creation == G_MAXUINT64)
427 return -1; /* underflow / overflow */
428 } else {
429 underscore_pos = i;
430
431 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
432
433 if(endptr == raw_name+underscore_pos+1)
434 return -1; /* No digit */
435 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
436 return -1; /* underflow / overflow */
437
438 strncpy(char_name, raw_name, underscore_pos);
439 char_name[underscore_pos] = '\0';
440
441 *name = g_quark_from_string(char_name);
442 *num = cpu_num;
443 }
69bd59ed 444
b333a76b 445
3aee1200 446 return 0;
447}
963b5f2d 448
3aee1200 449
3865ea09 450GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
77175651 451{
3865ea09 452 return &trace->tracefiles;
77175651 453}
454
455
3865ea09 456void compute_tracefile_group(GQuark key_id,
457 GArray *group,
458 struct compute_tracefile_group_args *args)
77175651 459{
460 int i;
461 LttTracefile *tf;
462
463 for(i=0; i<group->len; i++) {
464 tf = &g_array_index (group, LttTracefile, i);
465 if(tf->cpu_online)
3865ea09 466 args->func(tf, args->func_args);
77175651 467 }
468}
469
470
8655430b 471static void ltt_tracefile_group_destroy(gpointer data)
3aee1200 472{
473 GArray *group = (GArray *)data;
474 int i;
475 LttTracefile *tf;
476
477 for(i=0; i<group->len; i++) {
478 tf = &g_array_index (group, LttTracefile, i);
479 if(tf->cpu_online)
480 ltt_tracefile_close(tf);
481 }
482 g_array_free(group, TRUE);
6cd62ccf 483}
484
8655430b 485static gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 486{
3aee1200 487 GArray *group = (GArray *)data;
488 int i;
489 LttTracefile *tf;
490
491 for(i=0; i<group->len; i++) {
492 tf = &g_array_index (group, LttTracefile, i);
3c165eaf 493 if(tf->cpu_online)
494 return 1;
3aee1200 495 }
496 return 0;
49bf71b5 497}
498
499
3aee1200 500/* Open each tracefile under a specific directory. Put them in a
501 * GData : permits to access them using their tracefile group pathname.
502 * i.e. access control/modules tracefile group by index :
503 * "control/module".
3865ea09 504 *
505 * relative path is the path relative to the trace root
506 * root path is the full path
3aee1200 507 *
4a55f63e 508 * A tracefile group is simply an array where all the per cpu tracefiles sit.
3aee1200 509 */
510
8655430b 511static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
f7afe191 512{
62e4e7bf 513 DIR *dir = opendir(root_path);
514 struct dirent *entry;
515 struct stat stat_buf;
516 int ret;
3865ea09 517
62e4e7bf 518 gchar path[PATH_MAX];
519 int path_len;
520 gchar *path_ptr;
3aee1200 521
3865ea09 522 int rel_path_len;
69bd59ed 523 gchar rel_path[PATH_MAX];
524 gchar *rel_path_ptr;
cb03932a 525 LttTracefile tmp_tf;
3865ea09 526
62e4e7bf 527 if(dir == NULL) {
528 perror(root_path);
529 return ENOENT;
530 }
3aee1200 531
62e4e7bf 532 strncpy(path, root_path, PATH_MAX-1);
533 path_len = strlen(path);
534 path[path_len] = '/';
535 path_len++;
536 path_ptr = path + path_len;
3aee1200 537
3865ea09 538 strncpy(rel_path, relative_path, PATH_MAX-1);
539 rel_path_len = strlen(rel_path);
540 rel_path[rel_path_len] = '/';
541 rel_path_len++;
542 rel_path_ptr = rel_path + rel_path_len;
543
62e4e7bf 544 while((entry = readdir(dir)) != NULL) {
545
546 if(entry->d_name[0] == '.') continue;
547
548 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
549 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
550
551 ret = stat(path, &stat_buf);
552 if(ret == -1) {
553 perror(path);
554 continue;
555 }
556
557 g_debug("Tracefile file or directory : %s\n", path);
558
fe452434 559 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
74a588bb 560
62e4e7bf 561 if(S_ISDIR(stat_buf.st_mode)) {
3aee1200 562
62e4e7bf 563 g_debug("Entering subdirectory...\n");
564 ret = open_tracefiles(trace, path, rel_path);
565 if(ret < 0) continue;
566 } else if(S_ISREG(stat_buf.st_mode)) {
567 GQuark name;
f64fedd7 568 guint num;
569 gulong tid, pgid;
62e4e7bf 570 guint64 creation;
3aee1200 571 GArray *group;
f64fedd7 572 num = 0;
573 tid = pgid = 0;
62e4e7bf 574 creation = 0;
ae3d0f50 575 if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
3aee1200 576 continue; /* invalid name */
3865ea09 577
62e4e7bf 578 g_debug("Opening file.\n");
cb03932a 579 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
580 g_info("Error opening tracefile %s", path);
581
582 continue; /* error opening the tracefile : bad magic number ? */
583 }
584
3865ea09 585 g_debug("Tracefile name is %s and number is %u",
586 g_quark_to_string(name), num);
587
cb03932a 588 tmp_tf.cpu_online = 1;
589 tmp_tf.cpu_num = num;
d3d34f49 590 tmp_tf.name = name;
62e4e7bf 591 tmp_tf.tid = tid;
592 tmp_tf.pgid = pgid;
593 tmp_tf.creation = creation;
3865ea09 594 group = g_datalist_id_get_data(&trace->tracefiles, name);
3aee1200 595 if(group == NULL) {
596 /* Elements are automatically cleared when the array is allocated.
597 * It makes the cpu_online variable set to 0 : cpu offline, by default.
598 */
599 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
3865ea09 600 g_datalist_id_set_data_full(&trace->tracefiles, name,
3aee1200 601 group, ltt_tracefile_group_destroy);
602 }
cb03932a 603
3aee1200 604 /* Add the per cpu tracefile to the named group */
605 unsigned int old_len = group->len;
606 if(num+1 > old_len)
607 group = g_array_set_size(group, num+1);
cb03932a 608 g_array_index (group, LttTracefile, num) = tmp_tf;
afd57a3c 609 g_array_index (group, LttTracefile, num).event.tracefile =
610 &g_array_index (group, LttTracefile, num);
62e4e7bf 611 }
612 }
613
614 closedir(dir);
3aee1200 615
62e4e7bf 616 return 0;
f7afe191 617}
618
3aee1200 619
620/* Presumes the tracefile is already seeked at the beginning. It makes sense,
621 * because it must be done just after the opening */
2fc874ab 622static int ltt_process_metadata_tracefile(LttTracefile *tf)
3aee1200 623{
624 int err;
cb03932a 625 guint i;
3aee1200 626
627 while(1) {
628 err = ltt_tracefile_read_seek(tf);
629 if(err == EPERM) goto seek_error;
630 else if(err == ERANGE) break; /* End of tracefile */
631
632 err = ltt_tracefile_read_update_event(tf);
633 if(err) goto update_error;
634
3aee1200 635 /* The rules are :
2fc874ab 636 * It contains only core events :
637 * 0 : set_marker_id
638 * 1 : set_marker_format
3aee1200 639 */
3c165eaf 640 if(tf->event.event_id >= MARKER_CORE_IDS) {
2fc874ab 641 /* Should only contain core events */
642 g_warning("Error in processing metadata file %s, "
3c165eaf 643 "should not contain event id %u.", g_quark_to_string(tf->name),
644 tf->event.event_id);
3aee1200 645 err = EPERM;
3c165eaf 646 goto event_id_error;
d2083cab 647 } else {
2e13d6af 648 char *pos;
3c165eaf 649 const char *marker_name, *format;
650 uint16_t id;
651 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
3aee1200 652
3c165eaf 653 switch((enum marker_id)tf->event.event_id) {
654 case MARKER_ID_SET_MARKER_ID:
2e13d6af 655 marker_name = pos = tf->event.data;
3c165eaf 656 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name);
2e13d6af 657 pos += strlen(marker_name) + 1;
2fc874ab 658 pos += ltt_align((size_t)pos, sizeof(guint16), tf->alignment);
3c165eaf 659 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
c7146c5c 660 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
661 marker_name, id);
3c165eaf 662 pos += sizeof(guint16);
663 int_size = *(guint8*)pos;
664 pos += sizeof(guint8);
665 long_size = *(guint8*)pos;
666 pos += sizeof(guint8);
667 pointer_size = *(guint8*)pos;
668 pos += sizeof(guint8);
669 size_t_size = *(guint8*)pos;
670 pos += sizeof(guint8);
671 alignment = *(guint8*)pos;
672 pos += sizeof(guint8);
673 marker_id_event(tf->trace, g_quark_from_string(marker_name),
674 id, int_size, long_size,
675 pointer_size, size_t_size, alignment);
3aee1200 676 break;
3c165eaf 677 case MARKER_ID_SET_MARKER_FORMAT:
2e13d6af 678 marker_name = pos = tf->event.data;
3c165eaf 679 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
680 marker_name);
2e13d6af 681 pos += strlen(marker_name) + 1;
2e13d6af 682 format = pos;
3c165eaf 683 pos += strlen(format) + 1;
3c165eaf 684 marker_format_event(tf->trace, g_quark_from_string(marker_name),
685 format);
2fc874ab 686 /* get information from dictionary TODO */
d1bb700c 687 break;
3aee1200 688 default:
2fc874ab 689 g_warning("Error in processing metadata file %s, "
3c165eaf 690 "unknown event id %hhu.",
3aee1200 691 g_quark_to_string(tf->name),
692 tf->event.event_id);
693 err = EPERM;
694 goto event_id_error;
695 }
696 }
963b5f2d 697 }
3aee1200 698 return 0;
963b5f2d 699
3aee1200 700 /* Error handling */
3aee1200 701event_id_error:
3aee1200 702update_error:
703seek_error:
2fc874ab 704 g_warning("An error occured in metadata tracefile parsing");
3aee1200 705 return err;
6cd62ccf 706}
707
e95fe8f7 708/*
709 * Open a trace and return its LttTrace handle.
710 *
711 * pathname must be the directory of the trace
712 */
963b5f2d 713
3aee1200 714LttTrace *ltt_trace_open(const gchar *pathname)
715{
716 gchar abs_path[PATH_MAX];
717 LttTrace * t;
718 LttTracefile *tf;
719 GArray *group;
b56dcdf2 720 int i, ret;
64dd41a5 721 ltt_subbuffer_header_t *header;
62e4e7bf 722 DIR *dir;
723 struct dirent *entry;
b56dcdf2 724 guint control_found = 0;
62e4e7bf 725 struct stat stat_buf;
b56dcdf2 726 gchar path[PATH_MAX];
3aee1200 727
728 t = g_new(LttTrace, 1);
729 if(!t) goto alloc_error;
730
731 get_absolute_pathname(pathname, abs_path);
732 t->pathname = g_quark_from_string(abs_path);
733
3aee1200 734 g_datalist_init(&t->tracefiles);
b56dcdf2 735
736 /* Test to see if it looks like a trace */
62e4e7bf 737 dir = opendir(abs_path);
738 if(dir == NULL) {
739 perror(abs_path);
740 goto open_error;
741 }
742 while((entry = readdir(dir)) != NULL) {
b56dcdf2 743 strcpy(path, abs_path);
744 strcat(path, "/");
745 strcat(path, entry->d_name);
62e4e7bf 746 ret = stat(path, &stat_buf);
747 if(ret == -1) {
748 perror(path);
749 continue;
750 }
751 if(S_ISDIR(stat_buf.st_mode)) {
b56dcdf2 752 if(strcmp(entry->d_name, "control") == 0) {
753 control_found = 1;
754 }
b56dcdf2 755 }
756 }
757 closedir(dir);
758
fe452434 759 if(!control_found) goto find_error;
b56dcdf2 760
761 /* Open all the tracefiles */
e45551ac 762 if(open_tracefiles(t, abs_path, "")) {
763 g_warning("Error opening tracefile %s", abs_path);
b56dcdf2 764 goto find_error;
e45551ac 765 }
3aee1200 766
2fc874ab 767 /* Parse each trace control/metadata_N files : get runtime fac. info */
768 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_METADATA);
3aee1200 769 if(group == NULL) {
2fc874ab 770 g_error("Trace %s has no metadata tracefile", abs_path);
3865ea09 771 g_assert(0);
2fc874ab 772 goto metadata_error;
3aee1200 773 }
774
2fc874ab 775 /* Get the trace information for the control/metadata_0 tracefile */
16fcbb80 776 g_assert(group->len > 0);
777 tf = &g_array_index (group, LttTracefile, 0);
64dd41a5 778 header = (ltt_subbuffer_header_t *)tf->buffer.head;
ac3b1c7d 779 ret = parse_trace_header(header, tf, t);
780 g_assert(!ret);
b56dcdf2 781
782 t->num_cpu = group->len;
16fcbb80 783
d79909d1 784 ret = allocate_marker_data(t);
b44ba972 785 if (ret)
d79909d1 786 g_error("Error in allocating marker data");
787
3aee1200 788 for(i=0; i<group->len; i++) {
789 tf = &g_array_index (group, LttTracefile, i);
e85b0b1b 790 if (tf->cpu_online)
2fc874ab 791 if(ltt_process_metadata_tracefile(tf))
792 goto metadata_error;
3aee1200 793 }
dd3a6d39 794
3aee1200 795 return t;
796
797 /* Error handling */
2fc874ab 798metadata_error:
d79909d1 799 destroy_marker_data(t);
b56dcdf2 800find_error:
3aee1200 801 g_datalist_clear(&t->tracefiles);
b56dcdf2 802open_error:
3aee1200 803 g_free(t);
804alloc_error:
805 return NULL;
806
807}
6cd62ccf 808
e95fe8f7 809/* Open another, completely independant, instance of a trace.
810 *
811 * A read on this new instance will read the first event of the trace.
812 *
3aee1200 813 * When we copy a trace, we want all the opening actions to happen again :
814 * the trace will be reopened and totally independant from the original.
815 * That's why we call ltt_trace_open.
e95fe8f7 816 */
3aee1200 817LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 818{
3aee1200 819 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 820}
821
e95fe8f7 822/*
823 * Close a trace
824 */
825
3aee1200 826void ltt_trace_close(LttTrace *t)
6cd62ccf 827{
3aee1200 828 g_datalist_clear(&t->tracefiles);
829 g_free(t);
6cd62ccf 830}
831
3aee1200 832
6cd62ccf 833/*****************************************************************************
3aee1200 834 * Get the start time and end time of the trace
6cd62ccf 835 ****************************************************************************/
836
3c165eaf 837void ltt_tracefile_time_span_get(LttTracefile *tf,
3aee1200 838 LttTime *start, LttTime *end)
6cd62ccf 839{
3aee1200 840 int err;
6cd62ccf 841
3aee1200 842 err = map_block(tf, 0);
843 if(unlikely(err)) {
844 g_error("Can not map block");
845 *start = ltt_time_infinite;
846 } else
847 *start = tf->buffer.begin.timestamp;
848
849 err = map_block(tf, tf->num_blocks - 1); /* Last block */
850 if(unlikely(err)) {
851 g_error("Can not map block");
852 *end = ltt_time_zero;
853 } else
854 *end = tf->buffer.end.timestamp;
6cd62ccf 855}
856
3aee1200 857struct tracefile_time_span_get_args {
858 LttTrace *t;
859 LttTime *start;
860 LttTime *end;
861};
963b5f2d 862
8655430b 863static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 864{
3aee1200 865 struct tracefile_time_span_get_args *args =
866 (struct tracefile_time_span_get_args*)user_data;
867
868 GArray *group = (GArray *)data;
869 int i;
870 LttTracefile *tf;
871 LttTime tmp_start;
872 LttTime tmp_end;
873
874 for(i=0; i<group->len; i++) {
875 tf = &g_array_index (group, LttTracefile, i);
876 if(tf->cpu_online) {
877 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
878 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
879 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
880 }
881 }
6cd62ccf 882}
883
8655430b 884/* return the start and end time of a trace */
885
487ad181 886void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
887{
3aee1200 888 LttTime min_start = ltt_time_infinite;
889 LttTime max_end = ltt_time_zero;
890 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 891
3aee1200 892 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
893
894 if(start != NULL) *start = min_start;
895 if(end != NULL) *end = max_end;
896
487ad181 897}
898
899
3aee1200 900/* Seek to the first event in a tracefile that has a time equal or greater than
901 * the time passed in parameter.
902 *
903 * If the time parameter is outside the tracefile time span, seek to the first
27304273 904 * event or if after, return ERANGE.
3aee1200 905 *
906 * If the time parameter is before the first event, we have to seek specially to
907 * there.
908 *
27304273 909 * If the time is after the end of the trace, return ERANGE.
3aee1200 910 *
911 * Do a binary search to find the right block, then a sequential search in the
912 * block to find the event.
913 *
914 * In the special case where the time requested fits inside a block that has no
915 * event corresponding to the requested time, the first event of the next block
916 * will be seeked.
917 *
918 * IMPORTANT NOTE : // FIXME everywhere...
919 *
920 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
921 * you will jump over an event if you do.
922 *
923 * Return value : 0 : no error, the tf->event can be used
27304273 924 * ERANGE : time if after the last event of the trace
3aee1200 925 * otherwise : this is an error.
926 *
927 * */
928
929int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
930{
931 int ret = 0;
932 int err;
933 unsigned int block_num, high, low;
934
935 /* seek at the beginning of trace */
936 err = map_block(tf, 0); /* First block */
937 if(unlikely(err)) {
938 g_error("Can not map block");
939 goto fail;
caf7a67a 940 }
941
3aee1200 942 /* If the time is lower or equal the beginning of the trace,
943 * go to the first event. */
944 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
945 ret = ltt_tracefile_read(tf);
27304273 946 if(ret == ERANGE) goto range;
947 else if (ret) goto fail;
3aee1200 948 goto found; /* There is either no event in the trace or the event points
949 to the first event in the trace */
950 }
caf7a67a 951
3aee1200 952 err = map_block(tf, tf->num_blocks - 1); /* Last block */
953 if(unlikely(err)) {
954 g_error("Can not map block");
955 goto fail;
caf7a67a 956 }
6cd62ccf 957
27304273 958 /* If the time is after the end of the trace, return ERANGE. */
959 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
960 goto range;
3aee1200 961 }
62e55dd6 962
3aee1200 963 /* Binary search the block */
964 high = tf->num_blocks - 1;
965 low = 0;
966
967 while(1) {
968 block_num = ((high-low) / 2) + low;
969
970 err = map_block(tf, block_num);
971 if(unlikely(err)) {
972 g_error("Can not map block");
973 goto fail;
db55eaae 974 }
3aee1200 975 if(high == low) {
976 /* We cannot divide anymore : this is what would happen if the time
977 * requested was exactly between two consecutive buffers'end and start
978 * timestamps. This is also what would happend if we didn't deal with out
979 * of span cases prior in this function. */
980 /* The event is right in the buffer!
981 * (or in the next buffer first event) */
982 while(1) {
983 ret = ltt_tracefile_read(tf);
27304273 984 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 985 else if(ret) goto fail;
986
d9e13a0f 987 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 988 goto found;
3aee1200 989 }
990
27304273 991 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 992 /* go to lower part */
b1369bef 993 high = block_num - 1;
3aee1200 994 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
995 /* go to higher part */
b1369bef 996 low = block_num + 1;
3aee1200 997 } else {/* The event is right in the buffer!
998 (or in the next buffer first event) */
999 while(1) {
27304273 1000 ret = ltt_tracefile_read(tf);
1001 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1002 else if(ret) goto fail;
1003
d9e13a0f 1004 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1005 break;
6cd62ccf 1006 }
3aee1200 1007 goto found;
6cd62ccf 1008 }
6cd62ccf 1009 }
3aee1200 1010
1011found:
1012 return 0;
27304273 1013range:
1014 return ERANGE;
3aee1200 1015
1016 /* Error handling */
1017fail:
1018 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1019 g_quark_to_string(tf->name));
1020 return EPERM;
6cd62ccf 1021}
1022
e95fe8f7 1023/* Seek to a position indicated by an LttEventPosition
1024 */
80da81ad 1025
8655430b 1026int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep)
1027{
3aee1200 1028 int err;
1029
1030 if(ep->tracefile != tf) {
1031 goto fail;
80da81ad 1032 }
1033
3aee1200 1034 err = map_block(tf, ep->block);
1035 if(unlikely(err)) {
1036 g_error("Can not map block");
1037 goto fail;
1038 }
1039
1040 tf->event.offset = ep->offset;
18206708 1041
62e4e7bf 1042 /* Put back the event real tsc */
1043 tf->event.tsc = ep->tsc;
1044 tf->buffer.tsc = ep->tsc;
78f79181 1045
3aee1200 1046 err = ltt_tracefile_read_update_event(tf);
1047 if(err) goto fail;
73faf25a 1048
1049 /* deactivate this, as it does nothing for now
3aee1200 1050 err = ltt_tracefile_read_op(tf);
1051 if(err) goto fail;
73faf25a 1052 */
80da81ad 1053
a0c1f622 1054 return 0;
3aee1200 1055
1056fail:
1057 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1058 g_quark_to_string(tf->name));
a0c1f622 1059 return 1;
3aee1200 1060}
1061
e95fe8f7 1062/* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1063 * corresponds to.
1064 */
1065
ae3d0f50 1066LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
3aee1200 1067{
1068 LttTime time;
62e4e7bf 1069
1070 if(tsc > tf->trace->start_tsc) {
1071 time = ltt_time_from_uint64(
1072 (double)(tsc - tf->trace->start_tsc)
1073 * (1000000000.0 / tf->trace->freq_scale)
1074 / (double)tf->trace->start_freq);
1075 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1076 } else {
1077 time = ltt_time_from_uint64(
1078 (double)(tf->trace->start_tsc - tsc)
1079 * (1000000000.0 / tf->trace->freq_scale)
1080 / (double)tf->trace->start_freq);
1081 time = ltt_time_sub(tf->trace->start_time_from_tsc, time);
1082 }
3aee1200 1083 return time;
80da81ad 1084}
1085
ae3d0f50 1086/* Calculate the real event time based on the buffer boundaries */
1087LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1088{
62e4e7bf 1089 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
ae3d0f50 1090}
1091
eed2ef37 1092
1093/* Get the current event of the tracefile : valid until the next read */
1094LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1095{
1096 return &tf->event;
1097}
1098
1099
1100
6cd62ccf 1101/*****************************************************************************
1102 *Function name
3aee1200 1103 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1104 *Input params
1105 * t : tracefile
1106 *Return value
3aee1200 1107 *
1108 * Returns 0 if an event can be used in tf->event.
d822520b 1109 * Returns ERANGE on end of trace. The event in tf->event still can be used
1110 * (if the last block was not empty).
3aee1200 1111 * Returns EPERM on error.
1112 *
1113 * This function does make the tracefile event structure point to the event
1114 * currently pointed to by the tf->event.
1115 *
1116 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1117 * reinitialize it after an error if you want results to be coherent.
1118 * It would be the case if a end of trace last buffer has no event : the end
1119 * of trace wouldn't be returned, but an error.
1120 * We make the assumption there is at least one event per buffer.
6cd62ccf 1121 ****************************************************************************/
1122
3aee1200 1123int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1124{
963b5f2d 1125 int err;
6cd62ccf 1126
3aee1200 1127 err = ltt_tracefile_read_seek(tf);
1128 if(err) return err;
1129 err = ltt_tracefile_read_update_event(tf);
1130 if(err) return err;
73faf25a 1131
1132 /* deactivate this, as it does nothing for now
3aee1200 1133 err = ltt_tracefile_read_op(tf);
1134 if(err) return err;
73faf25a 1135 */
3aee1200 1136
1137 return 0;
1138}
1139
1140int ltt_tracefile_read_seek(LttTracefile *tf)
1141{
1142 int err;
1143
1144 /* Get next buffer until we finally have an event, or end of trace */
1145 while(1) {
1146 err = ltt_seek_next_event(tf);
1147 if(unlikely(err == ENOPROTOOPT)) {
1148 return EPERM;
bdc36259 1149 }
bdc36259 1150
3aee1200 1151 /* Are we at the end of the buffer ? */
1152 if(err == ERANGE) {
1153 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1154 return ERANGE;
1155 } else {
1156 /* get next block */
1157 err = map_block(tf, tf->buffer.index + 1);
1158 if(unlikely(err)) {
1159 g_error("Can not map block");
1160 return EPERM;
1161 }
1162 }
1163 } else break; /* We found an event ! */
1164 }
2dee981d 1165
3aee1200 1166 return 0;
1167}
18206708 1168
e95fe8f7 1169/* do an operation when reading a new event */
18206708 1170
73faf25a 1171/* This function does nothing for now */
1172#if 0
3aee1200 1173int ltt_tracefile_read_op(LttTracefile *tf)
1174{
3aee1200 1175 LttEvent *event;
1176
1177 event = &tf->event;
18206708 1178
73faf25a 1179 /* do event specific operation */
1180
1181 /* nothing */
40331ba8 1182
3aee1200 1183 return 0;
6cd62ccf 1184}
73faf25a 1185#endif
6cd62ccf 1186
91f8d488 1187static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1188{
1189 unsigned int offset = 0;
1190 int i, j;
1191
1192 g_printf("Event header (tracefile %s offset %llx):\n",
afd57a3c 1193 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1194 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1195 + (long)start_pos - (long)ev->tracefile->buffer.head);
1196
1197 while (offset < (long)end_pos - (long)start_pos) {
1198 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1199 g_printf(" ");
1200
1201 for (i = 0; i < 4 ; i++) {
1202 for (j = 0; j < 4; j++) {
1203 if (offset + ((i * 4) + j) <
1204 (long)end_pos - (long)start_pos)
1205 g_printf("%02hhX",
d3353231 1206 ((char*)start_pos)[offset + ((i * 4) + j)]);
91f8d488 1207 else
1208 g_printf(" ");
1209 g_printf(" ");
1210 }
1211 if (i < 4)
1212 g_printf(" ");
1213 }
1214 offset+=16;
1215 g_printf("\n");
1216 }
1217}
1218
6cd62ccf 1219
3aee1200 1220/* same as ltt_tracefile_read, but does not seek to the next event nor call
1221 * event specific operation. */
1222int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1223{
3aee1200 1224 void * pos;
1225 LttEvent *event;
91f8d488 1226 void *pos_aligned;
3aee1200 1227
1228 event = &tf->event;
eed2ef37 1229 pos = tf->buffer.head + event->offset;
3aee1200 1230
1231 /* Read event header */
1232
62e4e7bf 1233 /* Align the head */
2fc874ab 1234 pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment);
91f8d488 1235 pos_aligned = pos;
3aee1200 1236
2fc874ab 1237 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos);
1238 event->event_id = event->timestamp >> tf->tscbits;
a9048165 1239 event->timestamp = event->timestamp & tf->tsc_mask;
2fc874ab 1240 pos += sizeof(guint32);
1241
1242 switch (event->event_id) {
1243 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1244 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1245 pos += sizeof(guint16);
1246 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1247 pos += sizeof(guint16);
1248 if (event->event_size == 0xFFFF) {
1249 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1250 pos += sizeof(guint32);
d1bb700c 1251 }
2fc874ab 1252 pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment);
1253 tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
62e4e7bf 1254 pos += sizeof(guint64);
2fc874ab 1255 break;
1256 case 30: /* LTT_RFLAG_ID_SIZE */
f439de06 1257 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
3c165eaf 1258 pos += sizeof(guint16);
d1bb700c 1259 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1260 pos += sizeof(guint16);
2fc874ab 1261 if (event->event_size == 0xFFFF) {
1262 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1263 pos += sizeof(guint32);
1264 }
1265 break;
1266 case 31: /* LTT_RFLAG_ID */
1267 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1268 pos += sizeof(guint16);
1269 event->event_size = G_MAXUINT;
1270 break;
1271 default:
1272 event->event_size = G_MAXUINT;
1273 break;
1274 }
1275
1276 if (likely(event->event_id != 29)) {
1277 /* No extended timestamp */
1278 if (event->timestamp < (tf->buffer.tsc & tf->tsc_mask))
1279 tf->buffer.tsc = ((tf->buffer.tsc & ~tf->tsc_mask) /* overflow */
1280 + tf->tsc_mask_next_bit)
1281 | (guint64)event->timestamp;
1282 else
1283 tf->buffer.tsc = (tf->buffer.tsc & ~tf->tsc_mask) /* no overflow */
1284 | (guint64)event->timestamp;
d1bb700c 1285 }
2fc874ab 1286 event->tsc = tf->buffer.tsc;
1287
1288 event->event_time = ltt_interpolate_time(tf, event);
91f8d488 1289
1290 if (a_event_debug)
1291 print_debug_event_header(event, pos_aligned, pos);
1292
3aee1200 1293 event->data = pos;
1294
2fc874ab 1295 /*
1296 * Let ltt_update_event_size update event->data according to the largest
1297 * alignment within the payload.
1298 * Get the data size and update the event fields with the current
1299 * information. */
eed2ef37 1300 ltt_update_event_size(tf);
77175651 1301
3aee1200 1302 return 0;
6cd62ccf 1303}
1304
507915ee 1305
6cd62ccf 1306/****************************************************************************
1307 *Function name
3aee1200 1308 * map_block : map a block from the file
6cd62ccf 1309 *Input Params
1310 * lttdes : ltt trace file
1311 * whichBlock : the block which will be read
1312 *return value
1313 * 0 : success
1314 * EINVAL : lseek fail
1315 * EIO : can not read from the file
1316 ****************************************************************************/
1317
8655430b 1318static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1319{
b77d1b57 1320 int page_size = getpagesize();
64dd41a5 1321 ltt_subbuffer_header_t *header;
3aee1200 1322
1323 g_assert(block_num < tf->num_blocks);
6cd62ccf 1324
f628823c 1325 if(tf->buffer.head != NULL) {
1326 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1327 g_warning("unmap size : %u\n",
1328 PAGE_ALIGN(tf->buf_size));
1329 perror("munmap error");
1330 g_assert(0);
1331 }
1332 }
ac849774 1333
3aee1200 1334 /* Multiple of pages aligned head */
b77d1b57 1335 tf->buffer.head = mmap(0,
f628823c 1336 PAGE_ALIGN(tf->buf_size),
b77d1b57 1337 PROT_READ, MAP_PRIVATE, tf->fd,
f628823c 1338 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
3aee1200 1339
3865ea09 1340 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1341 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1342 g_assert(0);
3aee1200 1343 goto map_error;
6cd62ccf 1344 }
f64fedd7 1345 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 1346
6cd62ccf 1347
3aee1200 1348 tf->buffer.index = block_num;
1349
64dd41a5 1350 header = (ltt_subbuffer_header_t *)tf->buffer.head;
3aee1200 1351
3aee1200 1352 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1353 &header->cycle_count_begin);
1354 tf->buffer.begin.freq = tf->trace->start_freq;
ae3d0f50 1355
1356 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1357 tf->buffer.begin.cycle_count);
3aee1200 1358 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1359 &header->cycle_count_end);
1360 tf->buffer.end.freq = tf->trace->start_freq;
62e4e7bf 1361
3aee1200 1362 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
986e2a7c 1363 &header->lost_size);
ae3d0f50 1364 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1365 tf->buffer.end.cycle_count);
3aee1200 1366 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1367 tf->event.tsc = tf->buffer.tsc;
986e2a7c 1368 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 1369
1370 /* FIXME
1371 * eventually support variable buffer size : will need a partial pre-read of
1372 * the headers to create an index when we open the trace... eventually. */
f628823c 1373 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 1374 &header->buf_size));
507915ee 1375
3aee1200 1376 /* Make the current event point to the beginning of the buffer :
1377 * it means that the event read must get the first event. */
1378 tf->event.tracefile = tf;
1379 tf->event.block = block_num;
eed2ef37 1380 tf->event.offset = 0;
3aee1200 1381
1382 return 0;
6cd62ccf 1383
3aee1200 1384map_error:
1385 return -errno;
6cd62ccf 1386}
1387
91f8d488 1388static void print_debug_event_data(LttEvent *ev)
1389{
1390 unsigned int offset = 0;
1391 int i, j;
1392
1393 if (!max(ev->event_size, ev->data_size))
1394 return;
1395
1396 g_printf("Event data (tracefile %s offset %llx):\n",
afd57a3c 1397 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1398 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1399 + (long)ev->data - (long)ev->tracefile->buffer.head);
1400
1401 while (offset < max(ev->event_size, ev->data_size)) {
1402 g_printf("%8lx", (long)ev->data + offset
1403 - (long)ev->tracefile->buffer.head);
1404 g_printf(" ");
1405
1406 for (i = 0; i < 4 ; i++) {
1407 for (j = 0; j < 4; j++) {
1408 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
1409 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
1410 else
1411 g_printf(" ");
1412 g_printf(" ");
1413 }
1414 if (i < 4)
1415 g_printf(" ");
1416 }
1417
1418 g_printf(" ");
1419
1420 for (i = 0; i < 4; i++) {
1421 for (j = 0; j < 4; j++) {
1422 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
1423 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
1424 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
1425 else
1426 g_printf(".");
1427 } else
1428 g_printf(" ");
1429 }
1430 }
1431 offset+=16;
1432 g_printf("\n");
1433 }
1434}
1435
77175651 1436/* It will update the fields offsets too */
1437void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1438{
2312de30 1439 off_t size = 0;
d1bb700c 1440 char *tscdata;
d2007fbd 1441 struct marker_info *info;
44f317b7 1442
d2007fbd 1443 switch((enum marker_id)tf->event.event_id) {
1444 case MARKER_ID_SET_MARKER_ID:
44f317b7 1445 size = strlen((char*)tf->event.data) + 1;
07effdbd 1446 g_debug("marker %s id set", (char*)tf->event.data);
256a5b3a 1447 size += ltt_align(size, sizeof(guint16), tf->alignment);
d2007fbd 1448 size += sizeof(guint16);
3c165eaf 1449 size += sizeof(guint8);
1450 size += sizeof(guint8);
1451 size += sizeof(guint8);
1452 size += sizeof(guint8);
1453 size += sizeof(guint8);
44f317b7 1454 break;
d2007fbd 1455 case MARKER_ID_SET_MARKER_FORMAT:
07effdbd 1456 g_debug("marker %s format set", (char*)tf->event.data);
44f317b7 1457 size = strlen((char*)tf->event.data) + 1;
2e13d6af 1458 size += strlen((char*)tf->event.data + size) + 1;
44f317b7 1459 break;
256a5b3a 1460 }
1461
1462 info = marker_get_info_from_id(tf->trace, tf->event.event_id);
dcf96842 1463
256a5b3a 1464 if (tf->event.event_id >= MARKER_CORE_IDS)
1465 g_assert(info != NULL);
1466
1467 /* Do not update field offsets of core markers when initially reading the
2fc874ab 1468 * metadata tracefile when the infos about these markers do not exist yet.
256a5b3a 1469 */
1470 if (likely(info && info->fields)) {
2fc874ab 1471 /* alignment */
196085f2 1472 tf->event.data += ltt_align((off_t)(unsigned long)tf->event.data,
1473 info->largest_align,
2fc874ab 1474 info->alignment);
1475 /* size, dynamically computed */
256a5b3a 1476 if (info->size != -1)
1477 size = info->size;
1478 else
1479 size = marker_update_fields_offsets(marker_get_info_from_id(tf->trace,
1480 tf->event.event_id), tf->event.data);
44f317b7 1481 }
c4afd5d8 1482
d2007fbd 1483 tf->event.data_size = size;
1484
1485 /* Check consistency between kernel and LTTV structure sizes */
2fc874ab 1486 if(tf->event.event_size == G_MAXUINT) {
d2007fbd 1487 /* Event size too big to fit in the event size field */
1488 tf->event.event_size = tf->event.data_size;
1489 }
91f8d488 1490
1491 if (a_event_debug)
1492 print_debug_event_data(&tf->event);
1493
2fc874ab 1494 /* Having a marker load or marker format event out of the metadata
27c089c1 1495 * tracefiles is a serious bug. */
1496 switch((enum marker_id)tf->event.event_id) {
1497 case MARKER_ID_SET_MARKER_ID:
1498 case MARKER_ID_SET_MARKER_FORMAT:
2fc874ab 1499 if (tf->name != g_quark_from_string("/control/metadata"))
27c089c1 1500 g_error("Trace inconsistency : metadata event found in data "
1501 "tracefile %s", g_quark_to_string(tf->long_name));
1502 }
1503
d2007fbd 1504 if (tf->event.data_size != tf->event.event_size) {
3c165eaf 1505 struct marker_info *info = marker_get_info_from_id(tf->trace,
1506 tf->event.event_id);
1507 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1508 g_quark_to_string(info->name),
1509 tf->event.event_size, tf->event.data_size);
d2007fbd 1510 exit(-1);
1511 }
6cd62ccf 1512}
1513
6cd62ccf 1514
2fc874ab 1515/* Take the tf current event offset and use the event id to figure out where is
1516 * the next event offset.
3aee1200 1517 *
1518 * This is an internal function not aiming at being used elsewhere : it will
1519 * not jump over the current block limits. Please consider using
1520 * ltt_tracefile_read to do this.
1521 *
1522 * Returns 0 on success
1523 * ERANGE if we are at the end of the buffer.
1524 * ENOPROTOOPT if an error occured when getting the current event size.
1525 */
8655430b 1526static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1527{
3aee1200 1528 int ret = 0;
1529 void *pos;
3aee1200 1530
1531 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1532 if(tf->event.offset == 0) {
51551c6f 1533 tf->event.offset += tf->buffer_header_size;
b77d1b57 1534
f628823c 1535 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
b77d1b57 1536 ret = ERANGE;
1537 }
3aee1200 1538 goto found;
1539 }
2fc874ab 1540
3aee1200 1541 pos = tf->event.data;
1542
77175651 1543 if(tf->event.data_size < 0) goto error;
3aee1200 1544
77175651 1545 pos += (size_t)tf->event.data_size;
3aee1200 1546
eed2ef37 1547 tf->event.offset = pos - tf->buffer.head;
cb03932a 1548
f628823c 1549 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
cb03932a 1550 ret = ERANGE;
1551 goto found;
1552 }
36d36c9f 1553 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
3aee1200 1554
1555found:
1556 return ret;
1557
1558error:
1559 g_error("Error in ltt_seek_next_event for tracefile %s",
1560 g_quark_to_string(tf->name));
1561 return ENOPROTOOPT;
6cd62ccf 1562}
1563
f104d082 1564#if 0
3aee1200 1565/*****************************************************************************
6cd62ccf 1566 *Function name
3aee1200 1567 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 1568 *Input params
3aee1200 1569 * tracefile : opened trace file
1570 * event_type : the event type
6cd62ccf 1571 ****************************************************************************/
1572
3aee1200 1573void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 1574{
3aee1200 1575 LttField *field = event_type->root_field;
1576 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
1577
1578 if(likely(field))
1579 preset_field_type_size(tf, event_type, 0, 0,
1580 &fixed_root, &fixed_parent,
1581 field);
1582
1583}
f104d082 1584#endif //0
1585
1586
1587/*****************************************************************************
1588 *Function name
1589 * get_alignment : Get the alignment needed for a field.
1590 *Input params
f104d082 1591 * field : field
1592 *
1593 * returns : The size on which it must be aligned.
1594 *
1595 ****************************************************************************/
3c165eaf 1596#if 0
743e50fd 1597off_t get_alignment(LttField *field)
f104d082 1598{
2312de30 1599 LttType *type = &field->field_type;
f104d082 1600
1601 switch(type->type_class) {
1602 case LTT_INT_FIXED:
1603 case LTT_UINT_FIXED:
1604 case LTT_POINTER:
1605 case LTT_CHAR:
1606 case LTT_UCHAR:
1607 case LTT_SHORT:
1608 case LTT_USHORT:
1609 case LTT_INT:
1610 case LTT_UINT:
1611 case LTT_LONG:
1612 case LTT_ULONG:
1613 case LTT_SIZE_T:
1614 case LTT_SSIZE_T:
1615 case LTT_OFF_T:
1616 case LTT_FLOAT:
1617 case LTT_ENUM:
1618 /* Align offset on type size */
83e160f2 1619 g_assert(field->field_size != 0);
f104d082 1620 return field->field_size;
1621 break;
1622 case LTT_STRING:
83e160f2 1623 return 1;
f104d082 1624 break;
1625 case LTT_ARRAY:
1626 g_assert(type->fields->len == 1);
1627 {
1628 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 1629 return get_alignment(child);
f104d082 1630 }
1631 break;
1632 case LTT_SEQUENCE:
1633 g_assert(type->fields->len == 2);
1634 {
83e160f2 1635 off_t localign = 1;
f104d082 1636 LttField *child = &g_array_index(type->fields, LttField, 0);
1637
743e50fd 1638 localign = max(localign, get_alignment(child));
f104d082 1639
1640 child = &g_array_index(type->fields, LttField, 1);
743e50fd 1641 localign = max(localign, get_alignment(child));
f104d082 1642
1643 return localign;
1644 }
1645 break;
1646 case LTT_STRUCT:
1647 case LTT_UNION:
1648 {
1649 guint i;
83e160f2 1650 off_t localign = 1;
f104d082 1651
1652 for(i=0; i<type->fields->len; i++) {
1653 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 1654 localign = max(localign, get_alignment(child));
f104d082 1655 }
1656 return localign;
1657 }
1658 break;
1659 case LTT_NONE:
1660 default:
1661 g_error("get_alignment : unknown type");
83e160f2 1662 return -1;
f104d082 1663 }
f104d082 1664}
1665
3c165eaf 1666#endif //0
1667
f104d082 1668/*****************************************************************************
1669 *Function name
1670 * field_compute_static_size : Determine the size of fields known by their
1671 * sole definition. Unions, arrays and struct sizes might be known, but
1672 * the parser does not give that information.
1673 *Input params
1674 * tf : tracefile
1675 * field : field
1676 *
1677 ****************************************************************************/
3c165eaf 1678#if 0
743e50fd 1679void field_compute_static_size(LttFacility *fac, LttField *field)
f104d082 1680{
2312de30 1681 LttType *type = &field->field_type;
f104d082 1682
1683 switch(type->type_class) {
1684 case LTT_INT_FIXED:
1685 case LTT_UINT_FIXED:
1686 case LTT_POINTER:
1687 case LTT_CHAR:
1688 case LTT_UCHAR:
1689 case LTT_SHORT:
1690 case LTT_USHORT:
1691 case LTT_INT:
1692 case LTT_UINT:
1693 case LTT_LONG:
1694 case LTT_ULONG:
1695 case LTT_SIZE_T:
1696 case LTT_SSIZE_T:
1697 case LTT_OFF_T:
1698 case LTT_FLOAT:
1699 case LTT_ENUM:
1700 case LTT_STRING:
1701 /* nothing to do */
1702 break;
1703 case LTT_ARRAY:
1704 /* note this : array type size is the number of elements in the array,
1705 * while array field size of the length of the array in bytes */
1706 g_assert(type->fields->len == 1);
1707 {
1708 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 1709 field_compute_static_size(fac, child);
f104d082 1710
1711 if(child->field_size != 0) {
1712 field->field_size = type->size * child->field_size;
1713 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
1714 sizeof(off_t), type->size);
1715 } else {
1716 field->field_size = 0;
1717 }
1718 }
1719 break;
1720 case LTT_SEQUENCE:
1721 g_assert(type->fields->len == 2);
1722 {
2312de30 1723 off_t local_offset = 0;
f104d082 1724 LttField *child = &g_array_index(type->fields, LttField, 1);
743e50fd 1725 field_compute_static_size(fac, child);
f104d082 1726 field->field_size = 0;
1727 type->size = 0;
1728 if(child->field_size != 0) {
1729 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
1730 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
1731 }
1732 }
1733 break;
1734 case LTT_STRUCT:
1735 case LTT_UNION:
1736 {
1737 guint i;
1738 for(i=0;i<type->fields->len;i++) {
1739 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 1740 field_compute_static_size(fac, child);
f104d082 1741 if(child->field_size != 0) {
743e50fd 1742 type->size += ltt_align(type->size, get_alignment(child),
1743 fac->alignment);
f104d082 1744 type->size += child->field_size;
1745 } else {
1746 /* As soon as we find a child with variable size, we have
1747 * a variable size */
1748 type->size = 0;
1749 break;
1750 }
1751 }
1752 field->field_size = type->size;
1753 }
1754 break;
1755 default:
1756 g_error("field_static_size : unknown type");
2312de30 1757 }
f104d082 1758
1759}
3c165eaf 1760#endif //0
f104d082 1761
1762
1763/*****************************************************************************
1764 *Function name
1765 * precompute_fields_offsets : set the precomputable offset of the fields
1766 *Input params
743e50fd 1767 * fac : facility
f104d082 1768 * field : the field
1769 * offset : pointer to the current offset, must be incremented
1770 *
1771 * return : 1 : found a variable length field, stop the processing.
1772 * 0 otherwise.
1773 ****************************************************************************/
1774
3c165eaf 1775#if 0
dd3a6d39 1776gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset, gint is_compact)
f104d082 1777{
2312de30 1778 LttType *type = &field->field_type;
dd3a6d39 1779
1780 if(unlikely(is_compact)) {
1781 g_assert(field->field_size != 0);
1782 /* FIXME THIS IS A HUUUUUGE hack :
1783 * offset is between the compact_data field in struct LttEvent
1784 * and the address of the field root in the memory map.
1785 * ark. Both will stay at the same addresses while the event
1786 * is readable, so it's ok.
1787 */
1788 field->offset_root = 0;
1789 field->fixed_root = FIELD_FIXED;
1790 return 0;
1791 }
f104d082 1792
1793 switch(type->type_class) {
1794 case LTT_INT_FIXED:
1795 case LTT_UINT_FIXED:
1796 case LTT_POINTER:
1797 case LTT_CHAR:
1798 case LTT_UCHAR:
1799 case LTT_SHORT:
1800 case LTT_USHORT:
1801 case LTT_INT:
1802 case LTT_UINT:
1803 case LTT_LONG:
1804 case LTT_ULONG:
1805 case LTT_SIZE_T:
1806 case LTT_SSIZE_T:
1807 case LTT_OFF_T:
1808 case LTT_FLOAT:
1809 case LTT_ENUM:
743e50fd 1810 g_assert(field->field_size != 0);
f104d082 1811 /* Align offset on type size */
743e50fd 1812 *offset += ltt_align(*offset, get_alignment(field),
1813 fac->alignment);
f104d082 1814 /* remember offset */
1815 field->offset_root = *offset;
1816 field->fixed_root = FIELD_FIXED;
1817 /* Increment offset */
1818 *offset += field->field_size;
1819 return 0;
1820 break;
1821 case LTT_STRING:
1822 field->offset_root = *offset;
1823 field->fixed_root = FIELD_FIXED;
1824 return 1;
1825 break;
1826 case LTT_ARRAY:
1827 g_assert(type->fields->len == 1);
1828 {
1829 LttField *child = &g_array_index(type->fields, LttField, 0);
1830
743e50fd 1831 *offset += ltt_align(*offset, get_alignment(field),
1832 fac->alignment);
f104d082 1833
1834 /* remember offset */
1835 field->offset_root = *offset;
1836 field->array_offset = *offset;
1837 field->fixed_root = FIELD_FIXED;
1838
1839 /* Let the child be variable */
1840 //precompute_fields_offsets(tf, child, offset);
1841
1842 if(field->field_size != 0) {
1843 /* Increment offset */
1844 /* field_size is the array size in bytes */
1845 *offset += field->field_size;
1846 return 0;
1847 } else {
1848 return 1;
1849 }
1850 }
1851 break;
1852 case LTT_SEQUENCE:
1853 g_assert(type->fields->len == 2);
1854 {
1855 LttField *child;
1856 guint ret;
1857
743e50fd 1858 *offset += ltt_align(*offset, get_alignment(field),
1859 fac->alignment);
f104d082 1860
1861 /* remember offset */
1862 field->offset_root = *offset;
1863 field->fixed_root = FIELD_FIXED;
1864
1865 child = &g_array_index(type->fields, LttField, 0);
dd3a6d39 1866 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 1867 g_assert(ret == 0); /* Seq len cannot have variable len */
1868
1869 child = &g_array_index(type->fields, LttField, 1);
743e50fd 1870 *offset += ltt_align(*offset, get_alignment(child),
1871 fac->alignment);
f104d082 1872 field->array_offset = *offset;
743e50fd 1873 /* Let the child be variable. */
1874 //ret = precompute_fields_offsets(fac, child, offset);
f104d082 1875
1876 /* Cannot precompute fields offsets of sequence members, and has
1877 * variable length. */
1878 return 1;
1879 }
1880 break;
1881 case LTT_STRUCT:
1882 {
1883 LttField *child;
1884 guint i;
1885 gint ret=0;
1886
743e50fd 1887 *offset += ltt_align(*offset, get_alignment(field),
1888 fac->alignment);
f104d082 1889 /* remember offset */
1890 field->offset_root = *offset;
1891 field->fixed_root = FIELD_FIXED;
1892
1893 for(i=0; i< type->fields->len; i++) {
1894 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 1895 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 1896
1897 if(ret) break;
1898 }
1899 return ret;
1900 }
1901 break;
1902 case LTT_UNION:
1903 {
1904 LttField *child;
1905 guint i;
1906 gint ret=0;
1907
743e50fd 1908 *offset += ltt_align(*offset, get_alignment(field),
1909 fac->alignment);
f104d082 1910 /* remember offset */
1911 field->offset_root = *offset;
1912 field->fixed_root = FIELD_FIXED;
1913
1914 for(i=0; i< type->fields->len; i++) {
1915 *offset = field->offset_root;
1916 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 1917 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 1918
1919 if(ret) break;
1920 }
1921 *offset = field->offset_root + field->field_size;
1922 return ret;
1923 }
1924
1925 break;
1926 case LTT_NONE:
1927 default:
1928 g_error("precompute_fields_offsets : unknown type");
1929 return 1;
1930 }
1931
1932}
1933
3c165eaf 1934#endif //0
f104d082 1935
3c165eaf 1936#if 0
f104d082 1937/*****************************************************************************
1938 *Function name
1939 * precompute_offsets : set the precomputable offset of an event type
1940 *Input params
1941 * tf : tracefile
1942 * event : event type
1943 *
1944 ****************************************************************************/
743e50fd 1945void precompute_offsets(LttFacility *fac, LttEventType *event)
f104d082 1946{
1947 guint i;
1948 off_t offset = 0;
1949 gint ret;
1950
1951 /* First, compute the size of fixed size fields. Will determine size for
1952 * arrays, struct and unions, which is not done by the parser */
1953 for(i=0; i<event->fields->len; i++) {
1954 LttField *field = &g_array_index(event->fields, LttField, i);
743e50fd 1955 field_compute_static_size(fac, field);
f104d082 1956 }
1957
1958 /* Precompute all known offsets */
1959 for(i=0; i<event->fields->len; i++) {
1960 LttField *field = &g_array_index(event->fields, LttField, i);
dd3a6d39 1961 if(event->has_compact_data && i == 0)
1962 ret = precompute_fields_offsets(fac, field, &offset, 1);
1963 else
1964 ret = precompute_fields_offsets(fac, field, &offset, 0);
f104d082 1965 if(ret) break;
1966 }
1967}
3c165eaf 1968#endif //0
f104d082 1969
e4eced0f 1970
bbf28e50 1971
3aee1200 1972/*****************************************************************************
1973 *Function name
1974 * preset_field_type_size : set the fixed sizes of the field type
1975 *Input params
1976 * tf : tracefile
1977 * event_type : event type
1978 * offset_root : offset from the root
1979 * offset_parent : offset from the parent
1980 * fixed_root : Do we know a fixed offset to the root ?
1981 * fixed_parent : Do we know a fixed offset to the parent ?
1982 * field : field
1983 ****************************************************************************/
f104d082 1984
1985
1986
1987// preset the fixed size offsets. Calculate them just like genevent-new : an
1988// increment of a *to value that represents the offset from the start of the
1989// event data.
1990// The preset information is : offsets up to (and including) the first element
1991// of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
1992#if 0
3aee1200 1993void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
1994 off_t offset_root, off_t offset_parent,
1995 enum field_status *fixed_root, enum field_status *fixed_parent,
1996 LttField *field)
1997{
1998 enum field_status local_fixed_root, local_fixed_parent;
1999 guint i;
2000 LttType *type;
dfb73233 2001
3aee1200 2002 g_assert(field->fixed_root == FIELD_UNKNOWN);
2003 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2004 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 2005
3aee1200 2006 type = field->field_type;
2007
2008 field->fixed_root = *fixed_root;
2009 if(field->fixed_root == FIELD_FIXED)
2010 field->offset_root = offset_root;
2011 else
2012 field->offset_root = 0;
2013
2014 field->fixed_parent = *fixed_parent;
2015 if(field->fixed_parent == FIELD_FIXED)
2016 field->offset_parent = offset_parent;
2017 else
2018 field->offset_parent = 0;
2019
2020 size_t current_root_offset;
2021 size_t current_offset;
2022 enum field_status current_child_status, final_child_status;
2023 size_t max_size;
2024
2025 switch(type->type_class) {
83e160f2 2026 case LTT_INT_FIXED:
2027 case LTT_UINT_FIXED:
2028 case LTT_CHAR:
2029 case LTT_UCHAR:
2030 case LTT_SHORT:
2031 case LTT_USHORT:
3aee1200 2032 case LTT_INT:
2033 case LTT_UINT:
2034 case LTT_FLOAT:
2035 case LTT_ENUM:
2036 field->field_size = ltt_type_size(tf->trace, type);
2037 field->fixed_size = FIELD_FIXED;
2038 break;
2039 case LTT_POINTER:
2040 field->field_size = (off_t)event_type->facility->pointer_size;
2041 field->fixed_size = FIELD_FIXED;
2042 break;
2043 case LTT_LONG:
2044 case LTT_ULONG:
cb03932a 2045 field->field_size = (off_t)event_type->facility->long_size;
3aee1200 2046 field->fixed_size = FIELD_FIXED;
2047 break;
2048 case LTT_SIZE_T:
2049 case LTT_SSIZE_T:
2050 case LTT_OFF_T:
2051 field->field_size = (off_t)event_type->facility->size_t_size;
2052 field->fixed_size = FIELD_FIXED;
2053 break;
2054 case LTT_SEQUENCE:
2055 local_fixed_root = FIELD_VARIABLE;
2056 local_fixed_parent = FIELD_VARIABLE;
2057 preset_field_type_size(tf, event_type,
2058 0, 0,
2059 &local_fixed_root, &local_fixed_parent,
2060 field->child[0]);
2061 field->fixed_size = FIELD_VARIABLE;
2062 field->field_size = 0;
27304273 2063 *fixed_root = FIELD_VARIABLE;
2064 *fixed_parent = FIELD_VARIABLE;
3aee1200 2065 break;
2066 case LTT_STRING:
2067 field->fixed_size = FIELD_VARIABLE;
2068 field->field_size = 0;
27304273 2069 *fixed_root = FIELD_VARIABLE;
2070 *fixed_parent = FIELD_VARIABLE;
3aee1200 2071 break;
2072 case LTT_ARRAY:
2073 local_fixed_root = FIELD_VARIABLE;
2074 local_fixed_parent = FIELD_VARIABLE;
2075 preset_field_type_size(tf, event_type,
2076 0, 0,
2077 &local_fixed_root, &local_fixed_parent,
2078 field->child[0]);
2079 field->fixed_size = field->child[0]->fixed_size;
27304273 2080 if(field->fixed_size == FIELD_FIXED) {
3aee1200 2081 field->field_size = type->element_number * field->child[0]->field_size;
27304273 2082 } else {
3aee1200 2083 field->field_size = 0;
27304273 2084 *fixed_root = FIELD_VARIABLE;
2085 *fixed_parent = FIELD_VARIABLE;
2086 }
3aee1200 2087 break;
2088 case LTT_STRUCT:
2089 current_root_offset = field->offset_root;
2090 current_offset = 0;
2091 current_child_status = FIELD_FIXED;
2092 for(i=0;i<type->element_number;i++) {
2093 preset_field_type_size(tf, event_type,
2094 current_root_offset, current_offset,
2095 fixed_root, &current_child_status,
2096 field->child[i]);
2097 if(current_child_status == FIELD_FIXED) {
2098 current_root_offset += field->child[i]->field_size;
2099 current_offset += field->child[i]->field_size;
2100 } else {
2101 current_root_offset = 0;
2102 current_offset = 0;
2103 }
2104 }
2105 if(current_child_status != FIELD_FIXED) {
2106 *fixed_parent = current_child_status;
2107 field->field_size = 0;
2108 field->fixed_size = current_child_status;
2109 } else {
2110 field->field_size = current_offset;
2111 field->fixed_size = FIELD_FIXED;
2112 }
2113 break;
2114 case LTT_UNION:
2115 current_root_offset = field->offset_root;
2116 current_offset = 0;
2117 max_size = 0;
2118 final_child_status = FIELD_FIXED;
2119 for(i=0;i<type->element_number;i++) {
2120 enum field_status current_root_child_status = FIELD_FIXED;
2121 enum field_status current_child_status = FIELD_FIXED;
2122 preset_field_type_size(tf, event_type,
2123 current_root_offset, current_offset,
2124 &current_root_child_status, &current_child_status,
2125 field->child[i]);
2126 if(current_child_status != FIELD_FIXED)
2127 final_child_status = current_child_status;
2128 else
2129 max_size = max(max_size, field->child[i]->field_size);
2130 }
2131 if(final_child_status != FIELD_FIXED) {
62e4e7bf 2132 g_error("LTTV does not support variable size fields in unions.");
2133 /* This will stop the application. */
3aee1200 2134 *fixed_root = final_child_status;
2135 *fixed_parent = final_child_status;
2136 field->field_size = 0;
2137 field->fixed_size = current_child_status;
2138 } else {
2139 field->field_size = max_size;
2140 field->fixed_size = FIELD_FIXED;
2141 }
2142 break;
83e160f2 2143 case LTT_NONE:
2144 g_error("unexpected type NONE");
2145 break;
dfb73233 2146 }
2147
6cd62ccf 2148}
f104d082 2149#endif //0
3aee1200 2150
77175651 2151/*****************************************************************************
2152 *Function name
2153 * check_fields_compatibility : Check for compatibility between two fields :
2154 * do they use the same inner structure ?
2155 *Input params
2156 * event_type1 : event type
2157 * event_type2 : event type
2158 * field1 : field
2159 * field2 : field
2160 *Returns : 0 if identical
2161 * 1 if not.
2162 ****************************************************************************/
f104d082 2163// this function checks for equality of field types. Therefore, it does not use
2164// per se offsets. For instance, an aligned version of a structure is
2165// compatible with an unaligned version of the same structure.
3c165eaf 2166#if 0
f104d082 2167gint check_fields_compatibility(LttEventType *event_type1,
2168 LttEventType *event_type2,
2169 LttField *field1, LttField *field2)
2170{
2171 guint different = 0;
2312de30 2172 LttType *type1;
2173 LttType *type2;
f104d082 2174
2175 if(field1 == NULL) {
2176 if(field2 == NULL) goto end;
2177 else {
2178 different = 1;
2179 goto end;
2180 }
2181 } else if(field2 == NULL) {
2182 different = 1;
2183 goto end;
2184 }
2185
2312de30 2186 type1 = &field1->field_type;
2187 type2 = &field2->field_type;
f104d082 2188
2189 if(type1->type_class != type2->type_class) {
2190 different = 1;
2191 goto end;
2192 }
62e4e7bf 2193 if(type1->network != type2->network) {
2194 different = 1;
2195 goto end;
2196 }
f104d082 2197
2198 switch(type1->type_class) {
2199 case LTT_INT_FIXED:
2200 case LTT_UINT_FIXED:
2201 case LTT_POINTER:
2202 case LTT_CHAR:
2203 case LTT_UCHAR:
2204 case LTT_SHORT:
2205 case LTT_USHORT:
2206 case LTT_INT:
2207 case LTT_UINT:
2208 case LTT_LONG:
2209 case LTT_ULONG:
2210 case LTT_SIZE_T:
2211 case LTT_SSIZE_T:
2212 case LTT_OFF_T:
2213 case LTT_FLOAT:
2214 case LTT_ENUM:
2215 if(field1->field_size != field2->field_size)
2216 different = 1;
2217 break;
2218 case LTT_STRING:
2219 break;
2220 case LTT_ARRAY:
2221 {
2222 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2223 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2224
2225 if(type1->size != type2->size)
2226 different = 1;
2227 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2228 different = 1;
2229 }
2230 break;
2231 case LTT_SEQUENCE:
2232 {
2233 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2234 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2235
2236 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2237 different = 1;
2238 }
2239 break;
2240 case LTT_STRUCT:
2241 case LTT_UNION:
2242 {
2243 LttField *child;
2244 guint i;
2245
2246 if(type1->fields->len != type2->fields->len) {
2247 different = 1;
2248 goto end;
2249 }
2250
2251 for(i=0; i< type1->fields->len; i++) {
2312de30 2252 LttField *child1;
2253 LttField *child2;
f104d082 2254 child1 = &g_array_index(type1->fields, LttField, i);
2255 child2 = &g_array_index(type2->fields, LttField, i);
2256 different = check_fields_compatibility(event_type1,
2257 event_type2, child1, child2);
2258
2259 if(different) break;
2260 }
2261 }
2262 break;
2263 case LTT_NONE:
2264 default:
dd3a6d39 2265 g_error("check_fields_compatibility : unknown type");
f104d082 2266 }
2267
2268end:
2269 return different;
2270}
3c165eaf 2271#endif //0
f104d082 2272
2273#if 0
77175651 2274gint check_fields_compatibility(LttEventType *event_type1,
2275 LttEventType *event_type2,
2276 LttField *field1, LttField *field2)
2277{
2278 guint different = 0;
77175651 2279 guint i;
2280 LttType *type1;
2281 LttType *type2;
2282
2283 if(field1 == NULL) {
2284 if(field2 == NULL) goto end;
2285 else {
2286 different = 1;
2287 goto end;
2288 }
2289 } else if(field2 == NULL) {
2290 different = 1;
2291 goto end;
2292 }
2293
2294 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2295 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2296 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2297 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2298 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2299 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2300
2301 type1 = field1->field_type;
2302 type2 = field2->field_type;
2303
77175651 2304 if(type1->type_class != type2->type_class) {
2305 different = 1;
2306 goto end;
2307 }
2308 if(type1->element_name != type2->element_name) {
2309 different = 1;
2310 goto end;
2311 }
2312
2313 switch(type1->type_class) {
83e160f2 2314 case LTT_INT_FIXED:
2315 case LTT_UINT_FIXED:
2316 case LTT_POINTER:
2317 case LTT_CHAR:
2318 case LTT_UCHAR:
2319 case LTT_SHORT:
2320 case LTT_USHORT:
77175651 2321 case LTT_INT:
2322 case LTT_UINT:
2323 case LTT_FLOAT:
2324 case LTT_POINTER:
2325 case LTT_LONG:
2326 case LTT_ULONG:
2327 case LTT_SIZE_T:
2328 case LTT_SSIZE_T:
2329 case LTT_OFF_T:
2330 if(field1->field_size != field2->field_size) {
2331 different = 1;
2332 goto end;
2333 }
2334 break;
2335 case LTT_ENUM:
2336 if(type1->element_number != type2->element_number) {
2337 different = 1;
2338 goto end;
2339 }
2340 for(i=0;i<type1->element_number;i++) {
2341 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2342 different = 1;
2343 goto end;
2344 }
2345 }
2346 break;
2347 case LTT_SEQUENCE:
2348 /* Two elements : size and child */
2349 g_assert(type1->element_number != type2->element_number);
2350 for(i=0;i<type1->element_number;i++) {
2351 if(check_fields_compatibility(event_type1, event_type2,
2352 field1->child[0], field2->child[0])) {
2353 different = 1;
2354 goto end;
2355 }
2356 }
2357 break;
2358 case LTT_STRING:
2359 break;
2360 case LTT_ARRAY:
2361 if(field1->field_size != field2->field_size) {
2362 different = 1;
2363 goto end;
2364 }
2365 /* Two elements : size and child */
2366 g_assert(type1->element_number != type2->element_number);
2367 for(i=0;i<type1->element_number;i++) {
2368 if(check_fields_compatibility(event_type1, event_type2,
2369 field1->child[0], field2->child[0])) {
2370 different = 1;
2371 goto end;
2372 }
2373 }
2374 break;
2375 case LTT_STRUCT:
2376 case LTT_UNION:
2377 if(type1->element_number != type2->element_number) {
2378 different = 1;
2379 break;
2380 }
2381 for(i=0;i<type1->element_number;i++) {
2382 if(check_fields_compatibility(event_type1, event_type2,
2383 field1->child[0], field2->child[0])) {
2384 different = 1;
2385 goto end;
2386 }
2387 }
2388 break;
2389 }
2390end:
2391 return different;
2392}
3aee1200 2393#endif //0
6cd62ccf 2394
f104d082 2395
6cd62ccf 2396/*****************************************************************************
2397 *Function name
eed2ef37 2398 * ltt_get_int : get an integer number
6cd62ccf 2399 *Input params
3aee1200 2400 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2401 * size : the size of the integer
3aee1200 2402 * ptr : the data pointer
6cd62ccf 2403 *Return value
cf74a6f1 2404 * gint64 : a 64 bits integer
6cd62ccf 2405 ****************************************************************************/
2406
eed2ef37 2407gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2408{
3aee1200 2409 gint64 val;
cf74a6f1 2410
2411 switch(size) {
3aee1200 2412 case 1: val = *((gint8*)data); break;
2413 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2414 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2415 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2416 default: val = ltt_get_int64(reverse_byte_order, data);
2417 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2418 break;
2419 }
2420
3aee1200 2421 return val;
6cd62ccf 2422}
3aee1200 2423
6cd62ccf 2424/*****************************************************************************
2425 *Function name
eed2ef37 2426 * ltt_get_uint : get an unsigned integer number
6cd62ccf 2427 *Input params
3aee1200 2428 * reverse_byte_order: must we reverse the byte order ?
2429 * size : the size of the integer
2430 * ptr : the data pointer
2431 *Return value
2432 * guint64 : a 64 bits unsigned integer
6cd62ccf 2433 ****************************************************************************/
2434
eed2ef37 2435guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2436{
3aee1200 2437 guint64 val;
2438
2439 switch(size) {
2440 case 1: val = *((gint8*)data); break;
2441 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2442 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2443 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2444 default: val = ltt_get_uint64(reverse_byte_order, data);
2445 g_critical("get_uint : unsigned integer size %d unknown",
2446 size);
2447 break;
2448 }
2449
2450 return val;
6cd62ccf 2451}
3aee1200 2452
2453
a5dcde2f 2454/* get the node name of the system */
2455
2456char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2457{
2458 return s->node_name;
2459}
2460
2461
2462/* get the domain name of the system */
2463
2464char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2465{
2466 return s->domain_name;
2467}
2468
2469
2470/* get the description of the system */
2471
2472char * ltt_trace_system_description_description (LttSystemDescription * s)
2473{
2474 return s->description;
2475}
2476
2477
bf33dd50 2478/* get the NTP corrected start time of the trace */
7bd563ec 2479LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 2480{
7bd563ec 2481 return t->start_time;
a5dcde2f 2482}
2483
bf33dd50 2484/* get the monotonic start time of the trace */
2485LttTime ltt_trace_start_time_monotonic(LttTrace *t)
2486{
2487 return t->start_time_from_tsc;
2488}
2489
8655430b 2490static LttTracefile *ltt_tracefile_new()
18206708 2491{
afd57a3c 2492 LttTracefile *tf;
2493 tf = g_new(LttTracefile, 1);
2494 tf->event.tracefile = tf;
2495 return tf;
18206708 2496}
2497
8655430b 2498static void ltt_tracefile_destroy(LttTracefile *tf)
18206708 2499{
2500 g_free(tf);
2501}
2502
8655430b 2503static void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
18206708 2504{
2505 *dest = *src;
2506}
2507
3aee1200 2508/* Before library loading... */
2509
8655430b 2510static __attribute__((constructor)) void init(void)
3aee1200 2511{
2fc874ab 2512 LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("/control/metadata");
3aee1200 2513}
This page took 0.195413 seconds and 4 git commands to generate.