update compat
[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) {
3c165eaf 155 case 0:
b7576a11 156 {
64dd41a5 157 struct ltt_subbuffer_header_2_0 *vheader = header;
158 tf->buffer_header_size = sizeof(struct ltt_subbuffer_header_2_0) ;
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 <
64dd41a5 241 (off_t)(sizeof(ltt_subbuffer_header_t))){
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,
64dd41a5 249 PAGE_ALIGN(sizeof(ltt_subbuffer_header_t)), 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,
64dd41a5 270 PAGE_ALIGN(sizeof(ltt_subbuffer_header_t)))) {
f628823c 271 g_warning("unmap size : %u\n",
64dd41a5 272 PAGE_ALIGN(sizeof(ltt_subbuffer_header_t)));
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,
64dd41a5 289 PAGE_ALIGN(sizeof(ltt_subbuffer_header_t)))) {
f628823c 290 g_warning("unmap size : %u\n",
64dd41a5 291 PAGE_ALIGN(sizeof(ltt_subbuffer_header_t)));
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;
779 g_assert(parse_trace_header(header, tf, t) == 0);
b56dcdf2 780
781 t->num_cpu = group->len;
16fcbb80 782
d79909d1 783 ret = allocate_marker_data(t);
b44ba972 784 if (ret)
d79909d1 785 g_error("Error in allocating marker data");
786
3aee1200 787 for(i=0; i<group->len; i++) {
788 tf = &g_array_index (group, LttTracefile, i);
e85b0b1b 789 if (tf->cpu_online)
2fc874ab 790 if(ltt_process_metadata_tracefile(tf))
791 goto metadata_error;
3aee1200 792 }
dd3a6d39 793
3aee1200 794 return t;
795
796 /* Error handling */
2fc874ab 797metadata_error:
d79909d1 798 destroy_marker_data(t);
b56dcdf2 799find_error:
3aee1200 800 g_datalist_clear(&t->tracefiles);
b56dcdf2 801open_error:
3aee1200 802 g_free(t);
803alloc_error:
804 return NULL;
805
806}
6cd62ccf 807
e95fe8f7 808/* Open another, completely independant, instance of a trace.
809 *
810 * A read on this new instance will read the first event of the trace.
811 *
3aee1200 812 * When we copy a trace, we want all the opening actions to happen again :
813 * the trace will be reopened and totally independant from the original.
814 * That's why we call ltt_trace_open.
e95fe8f7 815 */
3aee1200 816LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 817{
3aee1200 818 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 819}
820
e95fe8f7 821/*
822 * Close a trace
823 */
824
3aee1200 825void ltt_trace_close(LttTrace *t)
6cd62ccf 826{
3aee1200 827 g_datalist_clear(&t->tracefiles);
828 g_free(t);
6cd62ccf 829}
830
3aee1200 831
6cd62ccf 832/*****************************************************************************
3aee1200 833 * Get the start time and end time of the trace
6cd62ccf 834 ****************************************************************************/
835
3c165eaf 836void ltt_tracefile_time_span_get(LttTracefile *tf,
3aee1200 837 LttTime *start, LttTime *end)
6cd62ccf 838{
3aee1200 839 int err;
6cd62ccf 840
3aee1200 841 err = map_block(tf, 0);
842 if(unlikely(err)) {
843 g_error("Can not map block");
844 *start = ltt_time_infinite;
845 } else
846 *start = tf->buffer.begin.timestamp;
847
848 err = map_block(tf, tf->num_blocks - 1); /* Last block */
849 if(unlikely(err)) {
850 g_error("Can not map block");
851 *end = ltt_time_zero;
852 } else
853 *end = tf->buffer.end.timestamp;
6cd62ccf 854}
855
3aee1200 856struct tracefile_time_span_get_args {
857 LttTrace *t;
858 LttTime *start;
859 LttTime *end;
860};
963b5f2d 861
8655430b 862static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 863{
3aee1200 864 struct tracefile_time_span_get_args *args =
865 (struct tracefile_time_span_get_args*)user_data;
866
867 GArray *group = (GArray *)data;
868 int i;
869 LttTracefile *tf;
870 LttTime tmp_start;
871 LttTime tmp_end;
872
873 for(i=0; i<group->len; i++) {
874 tf = &g_array_index (group, LttTracefile, i);
875 if(tf->cpu_online) {
876 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
877 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
878 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
879 }
880 }
6cd62ccf 881}
882
8655430b 883/* return the start and end time of a trace */
884
487ad181 885void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
886{
3aee1200 887 LttTime min_start = ltt_time_infinite;
888 LttTime max_end = ltt_time_zero;
889 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 890
3aee1200 891 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
892
893 if(start != NULL) *start = min_start;
894 if(end != NULL) *end = max_end;
895
487ad181 896}
897
898
3aee1200 899/* Seek to the first event in a tracefile that has a time equal or greater than
900 * the time passed in parameter.
901 *
902 * If the time parameter is outside the tracefile time span, seek to the first
27304273 903 * event or if after, return ERANGE.
3aee1200 904 *
905 * If the time parameter is before the first event, we have to seek specially to
906 * there.
907 *
27304273 908 * If the time is after the end of the trace, return ERANGE.
3aee1200 909 *
910 * Do a binary search to find the right block, then a sequential search in the
911 * block to find the event.
912 *
913 * In the special case where the time requested fits inside a block that has no
914 * event corresponding to the requested time, the first event of the next block
915 * will be seeked.
916 *
917 * IMPORTANT NOTE : // FIXME everywhere...
918 *
919 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
920 * you will jump over an event if you do.
921 *
922 * Return value : 0 : no error, the tf->event can be used
27304273 923 * ERANGE : time if after the last event of the trace
3aee1200 924 * otherwise : this is an error.
925 *
926 * */
927
928int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
929{
930 int ret = 0;
931 int err;
932 unsigned int block_num, high, low;
933
934 /* seek at the beginning of trace */
935 err = map_block(tf, 0); /* First block */
936 if(unlikely(err)) {
937 g_error("Can not map block");
938 goto fail;
caf7a67a 939 }
940
3aee1200 941 /* If the time is lower or equal the beginning of the trace,
942 * go to the first event. */
943 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
944 ret = ltt_tracefile_read(tf);
27304273 945 if(ret == ERANGE) goto range;
946 else if (ret) goto fail;
3aee1200 947 goto found; /* There is either no event in the trace or the event points
948 to the first event in the trace */
949 }
caf7a67a 950
3aee1200 951 err = map_block(tf, tf->num_blocks - 1); /* Last block */
952 if(unlikely(err)) {
953 g_error("Can not map block");
954 goto fail;
caf7a67a 955 }
6cd62ccf 956
27304273 957 /* If the time is after the end of the trace, return ERANGE. */
958 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
959 goto range;
3aee1200 960 }
62e55dd6 961
3aee1200 962 /* Binary search the block */
963 high = tf->num_blocks - 1;
964 low = 0;
965
966 while(1) {
967 block_num = ((high-low) / 2) + low;
968
969 err = map_block(tf, block_num);
970 if(unlikely(err)) {
971 g_error("Can not map block");
972 goto fail;
db55eaae 973 }
3aee1200 974 if(high == low) {
975 /* We cannot divide anymore : this is what would happen if the time
976 * requested was exactly between two consecutive buffers'end and start
977 * timestamps. This is also what would happend if we didn't deal with out
978 * of span cases prior in this function. */
979 /* The event is right in the buffer!
980 * (or in the next buffer first event) */
981 while(1) {
982 ret = ltt_tracefile_read(tf);
27304273 983 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 984 else if(ret) goto fail;
985
d9e13a0f 986 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 987 goto found;
3aee1200 988 }
989
27304273 990 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 991 /* go to lower part */
b1369bef 992 high = block_num - 1;
3aee1200 993 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
994 /* go to higher part */
b1369bef 995 low = block_num + 1;
3aee1200 996 } else {/* The event is right in the buffer!
997 (or in the next buffer first event) */
998 while(1) {
27304273 999 ret = ltt_tracefile_read(tf);
1000 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1001 else if(ret) goto fail;
1002
d9e13a0f 1003 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1004 break;
6cd62ccf 1005 }
3aee1200 1006 goto found;
6cd62ccf 1007 }
6cd62ccf 1008 }
3aee1200 1009
1010found:
1011 return 0;
27304273 1012range:
1013 return ERANGE;
3aee1200 1014
1015 /* Error handling */
1016fail:
1017 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1018 g_quark_to_string(tf->name));
1019 return EPERM;
6cd62ccf 1020}
1021
e95fe8f7 1022/* Seek to a position indicated by an LttEventPosition
1023 */
80da81ad 1024
8655430b 1025int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep)
1026{
3aee1200 1027 int err;
1028
1029 if(ep->tracefile != tf) {
1030 goto fail;
80da81ad 1031 }
1032
3aee1200 1033 err = map_block(tf, ep->block);
1034 if(unlikely(err)) {
1035 g_error("Can not map block");
1036 goto fail;
1037 }
1038
1039 tf->event.offset = ep->offset;
18206708 1040
62e4e7bf 1041 /* Put back the event real tsc */
1042 tf->event.tsc = ep->tsc;
1043 tf->buffer.tsc = ep->tsc;
78f79181 1044
3aee1200 1045 err = ltt_tracefile_read_update_event(tf);
1046 if(err) goto fail;
73faf25a 1047
1048 /* deactivate this, as it does nothing for now
3aee1200 1049 err = ltt_tracefile_read_op(tf);
1050 if(err) goto fail;
73faf25a 1051 */
80da81ad 1052
a0c1f622 1053 return 0;
3aee1200 1054
1055fail:
1056 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1057 g_quark_to_string(tf->name));
a0c1f622 1058 return 1;
3aee1200 1059}
1060
e95fe8f7 1061/* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1062 * corresponds to.
1063 */
1064
ae3d0f50 1065LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
3aee1200 1066{
1067 LttTime time;
62e4e7bf 1068
1069 if(tsc > tf->trace->start_tsc) {
1070 time = ltt_time_from_uint64(
1071 (double)(tsc - tf->trace->start_tsc)
1072 * (1000000000.0 / tf->trace->freq_scale)
1073 / (double)tf->trace->start_freq);
1074 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1075 } else {
1076 time = ltt_time_from_uint64(
1077 (double)(tf->trace->start_tsc - tsc)
1078 * (1000000000.0 / tf->trace->freq_scale)
1079 / (double)tf->trace->start_freq);
1080 time = ltt_time_sub(tf->trace->start_time_from_tsc, time);
1081 }
3aee1200 1082 return time;
80da81ad 1083}
1084
ae3d0f50 1085/* Calculate the real event time based on the buffer boundaries */
1086LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1087{
62e4e7bf 1088 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
ae3d0f50 1089}
1090
eed2ef37 1091
1092/* Get the current event of the tracefile : valid until the next read */
1093LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1094{
1095 return &tf->event;
1096}
1097
1098
1099
6cd62ccf 1100/*****************************************************************************
1101 *Function name
3aee1200 1102 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1103 *Input params
1104 * t : tracefile
1105 *Return value
3aee1200 1106 *
1107 * Returns 0 if an event can be used in tf->event.
d822520b 1108 * Returns ERANGE on end of trace. The event in tf->event still can be used
1109 * (if the last block was not empty).
3aee1200 1110 * Returns EPERM on error.
1111 *
1112 * This function does make the tracefile event structure point to the event
1113 * currently pointed to by the tf->event.
1114 *
1115 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1116 * reinitialize it after an error if you want results to be coherent.
1117 * It would be the case if a end of trace last buffer has no event : the end
1118 * of trace wouldn't be returned, but an error.
1119 * We make the assumption there is at least one event per buffer.
6cd62ccf 1120 ****************************************************************************/
1121
3aee1200 1122int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1123{
963b5f2d 1124 int err;
6cd62ccf 1125
3aee1200 1126 err = ltt_tracefile_read_seek(tf);
1127 if(err) return err;
1128 err = ltt_tracefile_read_update_event(tf);
1129 if(err) return err;
73faf25a 1130
1131 /* deactivate this, as it does nothing for now
3aee1200 1132 err = ltt_tracefile_read_op(tf);
1133 if(err) return err;
73faf25a 1134 */
3aee1200 1135
1136 return 0;
1137}
1138
1139int ltt_tracefile_read_seek(LttTracefile *tf)
1140{
1141 int err;
1142
1143 /* Get next buffer until we finally have an event, or end of trace */
1144 while(1) {
1145 err = ltt_seek_next_event(tf);
1146 if(unlikely(err == ENOPROTOOPT)) {
1147 return EPERM;
bdc36259 1148 }
bdc36259 1149
3aee1200 1150 /* Are we at the end of the buffer ? */
1151 if(err == ERANGE) {
1152 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1153 return ERANGE;
1154 } else {
1155 /* get next block */
1156 err = map_block(tf, tf->buffer.index + 1);
1157 if(unlikely(err)) {
1158 g_error("Can not map block");
1159 return EPERM;
1160 }
1161 }
1162 } else break; /* We found an event ! */
1163 }
2dee981d 1164
3aee1200 1165 return 0;
1166}
18206708 1167
e95fe8f7 1168/* do an operation when reading a new event */
18206708 1169
73faf25a 1170/* This function does nothing for now */
1171#if 0
3aee1200 1172int ltt_tracefile_read_op(LttTracefile *tf)
1173{
3aee1200 1174 LttEvent *event;
1175
1176 event = &tf->event;
18206708 1177
73faf25a 1178 /* do event specific operation */
1179
1180 /* nothing */
40331ba8 1181
3aee1200 1182 return 0;
6cd62ccf 1183}
73faf25a 1184#endif
6cd62ccf 1185
91f8d488 1186static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1187{
1188 unsigned int offset = 0;
1189 int i, j;
1190
1191 g_printf("Event header (tracefile %s offset %llx):\n",
afd57a3c 1192 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1193 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1194 + (long)start_pos - (long)ev->tracefile->buffer.head);
1195
1196 while (offset < (long)end_pos - (long)start_pos) {
1197 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1198 g_printf(" ");
1199
1200 for (i = 0; i < 4 ; i++) {
1201 for (j = 0; j < 4; j++) {
1202 if (offset + ((i * 4) + j) <
1203 (long)end_pos - (long)start_pos)
1204 g_printf("%02hhX",
d3353231 1205 ((char*)start_pos)[offset + ((i * 4) + j)]);
91f8d488 1206 else
1207 g_printf(" ");
1208 g_printf(" ");
1209 }
1210 if (i < 4)
1211 g_printf(" ");
1212 }
1213 offset+=16;
1214 g_printf("\n");
1215 }
1216}
1217
6cd62ccf 1218
3aee1200 1219/* same as ltt_tracefile_read, but does not seek to the next event nor call
1220 * event specific operation. */
1221int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1222{
3aee1200 1223 void * pos;
1224 LttEvent *event;
91f8d488 1225 void *pos_aligned;
3aee1200 1226
1227 event = &tf->event;
eed2ef37 1228 pos = tf->buffer.head + event->offset;
3aee1200 1229
1230 /* Read event header */
1231
62e4e7bf 1232 /* Align the head */
2fc874ab 1233 pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment);
91f8d488 1234 pos_aligned = pos;
3aee1200 1235
2fc874ab 1236 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos);
1237 event->event_id = event->timestamp >> tf->tscbits;
a9048165 1238 event->timestamp = event->timestamp & tf->tsc_mask;
2fc874ab 1239 pos += sizeof(guint32);
1240
1241 switch (event->event_id) {
1242 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1243 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1244 pos += sizeof(guint16);
1245 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1246 pos += sizeof(guint16);
1247 if (event->event_size == 0xFFFF) {
1248 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1249 pos += sizeof(guint32);
d1bb700c 1250 }
2fc874ab 1251 pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment);
1252 tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
62e4e7bf 1253 pos += sizeof(guint64);
2fc874ab 1254 break;
1255 case 30: /* LTT_RFLAG_ID_SIZE */
f439de06 1256 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
3c165eaf 1257 pos += sizeof(guint16);
d1bb700c 1258 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1259 pos += sizeof(guint16);
2fc874ab 1260 if (event->event_size == 0xFFFF) {
1261 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1262 pos += sizeof(guint32);
1263 }
1264 break;
1265 case 31: /* LTT_RFLAG_ID */
1266 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1267 pos += sizeof(guint16);
1268 event->event_size = G_MAXUINT;
1269 break;
1270 default:
1271 event->event_size = G_MAXUINT;
1272 break;
1273 }
1274
1275 if (likely(event->event_id != 29)) {
1276 /* No extended timestamp */
1277 if (event->timestamp < (tf->buffer.tsc & tf->tsc_mask))
1278 tf->buffer.tsc = ((tf->buffer.tsc & ~tf->tsc_mask) /* overflow */
1279 + tf->tsc_mask_next_bit)
1280 | (guint64)event->timestamp;
1281 else
1282 tf->buffer.tsc = (tf->buffer.tsc & ~tf->tsc_mask) /* no overflow */
1283 | (guint64)event->timestamp;
d1bb700c 1284 }
2fc874ab 1285 event->tsc = tf->buffer.tsc;
1286
1287 event->event_time = ltt_interpolate_time(tf, event);
91f8d488 1288
1289 if (a_event_debug)
1290 print_debug_event_header(event, pos_aligned, pos);
1291
3aee1200 1292 event->data = pos;
1293
2fc874ab 1294 /*
1295 * Let ltt_update_event_size update event->data according to the largest
1296 * alignment within the payload.
1297 * Get the data size and update the event fields with the current
1298 * information. */
eed2ef37 1299 ltt_update_event_size(tf);
77175651 1300
3aee1200 1301 return 0;
6cd62ccf 1302}
1303
507915ee 1304
6cd62ccf 1305/****************************************************************************
1306 *Function name
3aee1200 1307 * map_block : map a block from the file
6cd62ccf 1308 *Input Params
1309 * lttdes : ltt trace file
1310 * whichBlock : the block which will be read
1311 *return value
1312 * 0 : success
1313 * EINVAL : lseek fail
1314 * EIO : can not read from the file
1315 ****************************************************************************/
1316
8655430b 1317static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1318{
b77d1b57 1319 int page_size = getpagesize();
64dd41a5 1320 ltt_subbuffer_header_t *header;
3aee1200 1321
1322 g_assert(block_num < tf->num_blocks);
6cd62ccf 1323
f628823c 1324 if(tf->buffer.head != NULL) {
1325 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1326 g_warning("unmap size : %u\n",
1327 PAGE_ALIGN(tf->buf_size));
1328 perror("munmap error");
1329 g_assert(0);
1330 }
1331 }
ac849774 1332
3aee1200 1333 /* Multiple of pages aligned head */
b77d1b57 1334 tf->buffer.head = mmap(0,
f628823c 1335 PAGE_ALIGN(tf->buf_size),
b77d1b57 1336 PROT_READ, MAP_PRIVATE, tf->fd,
f628823c 1337 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
3aee1200 1338
3865ea09 1339 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1340 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1341 g_assert(0);
3aee1200 1342 goto map_error;
6cd62ccf 1343 }
f64fedd7 1344 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 1345
6cd62ccf 1346
3aee1200 1347 tf->buffer.index = block_num;
1348
64dd41a5 1349 header = (ltt_subbuffer_header_t *)tf->buffer.head;
3aee1200 1350
3aee1200 1351 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1352 &header->cycle_count_begin);
1353 tf->buffer.begin.freq = tf->trace->start_freq;
ae3d0f50 1354
1355 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1356 tf->buffer.begin.cycle_count);
3aee1200 1357 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1358 &header->cycle_count_end);
1359 tf->buffer.end.freq = tf->trace->start_freq;
62e4e7bf 1360
3aee1200 1361 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
986e2a7c 1362 &header->lost_size);
ae3d0f50 1363 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1364 tf->buffer.end.cycle_count);
3aee1200 1365 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1366 tf->event.tsc = tf->buffer.tsc;
986e2a7c 1367 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 1368
1369 /* FIXME
1370 * eventually support variable buffer size : will need a partial pre-read of
1371 * the headers to create an index when we open the trace... eventually. */
f628823c 1372 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 1373 &header->buf_size));
507915ee 1374
3aee1200 1375 /* Make the current event point to the beginning of the buffer :
1376 * it means that the event read must get the first event. */
1377 tf->event.tracefile = tf;
1378 tf->event.block = block_num;
eed2ef37 1379 tf->event.offset = 0;
3aee1200 1380
1381 return 0;
6cd62ccf 1382
3aee1200 1383map_error:
1384 return -errno;
6cd62ccf 1385}
1386
91f8d488 1387static void print_debug_event_data(LttEvent *ev)
1388{
1389 unsigned int offset = 0;
1390 int i, j;
1391
1392 if (!max(ev->event_size, ev->data_size))
1393 return;
1394
1395 g_printf("Event data (tracefile %s offset %llx):\n",
afd57a3c 1396 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1397 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1398 + (long)ev->data - (long)ev->tracefile->buffer.head);
1399
1400 while (offset < max(ev->event_size, ev->data_size)) {
1401 g_printf("%8lx", (long)ev->data + offset
1402 - (long)ev->tracefile->buffer.head);
1403 g_printf(" ");
1404
1405 for (i = 0; i < 4 ; i++) {
1406 for (j = 0; j < 4; j++) {
1407 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
1408 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
1409 else
1410 g_printf(" ");
1411 g_printf(" ");
1412 }
1413 if (i < 4)
1414 g_printf(" ");
1415 }
1416
1417 g_printf(" ");
1418
1419 for (i = 0; i < 4; i++) {
1420 for (j = 0; j < 4; j++) {
1421 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
1422 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
1423 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
1424 else
1425 g_printf(".");
1426 } else
1427 g_printf(" ");
1428 }
1429 }
1430 offset+=16;
1431 g_printf("\n");
1432 }
1433}
1434
77175651 1435/* It will update the fields offsets too */
1436void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1437{
2312de30 1438 off_t size = 0;
d1bb700c 1439 char *tscdata;
d2007fbd 1440 struct marker_info *info;
44f317b7 1441
d2007fbd 1442 switch((enum marker_id)tf->event.event_id) {
1443 case MARKER_ID_SET_MARKER_ID:
44f317b7 1444 size = strlen((char*)tf->event.data) + 1;
07effdbd 1445 g_debug("marker %s id set", (char*)tf->event.data);
256a5b3a 1446 size += ltt_align(size, sizeof(guint16), tf->alignment);
d2007fbd 1447 size += sizeof(guint16);
3c165eaf 1448 size += sizeof(guint8);
1449 size += sizeof(guint8);
1450 size += sizeof(guint8);
1451 size += sizeof(guint8);
1452 size += sizeof(guint8);
44f317b7 1453 break;
d2007fbd 1454 case MARKER_ID_SET_MARKER_FORMAT:
07effdbd 1455 g_debug("marker %s format set", (char*)tf->event.data);
44f317b7 1456 size = strlen((char*)tf->event.data) + 1;
2e13d6af 1457 size += strlen((char*)tf->event.data + size) + 1;
44f317b7 1458 break;
256a5b3a 1459 }
1460
1461 info = marker_get_info_from_id(tf->trace, tf->event.event_id);
dcf96842 1462
256a5b3a 1463 if (tf->event.event_id >= MARKER_CORE_IDS)
1464 g_assert(info != NULL);
1465
1466 /* Do not update field offsets of core markers when initially reading the
2fc874ab 1467 * metadata tracefile when the infos about these markers do not exist yet.
256a5b3a 1468 */
1469 if (likely(info && info->fields)) {
2fc874ab 1470 /* alignment */
a9048165 1471 tf->event.data += ltt_align((off_t)tf->event.data, info->largest_align,
2fc874ab 1472 info->alignment);
1473 /* size, dynamically computed */
256a5b3a 1474 if (info->size != -1)
1475 size = info->size;
1476 else
1477 size = marker_update_fields_offsets(marker_get_info_from_id(tf->trace,
1478 tf->event.event_id), tf->event.data);
44f317b7 1479 }
c4afd5d8 1480
d2007fbd 1481 tf->event.data_size = size;
1482
1483 /* Check consistency between kernel and LTTV structure sizes */
2fc874ab 1484 if(tf->event.event_size == G_MAXUINT) {
d2007fbd 1485 /* Event size too big to fit in the event size field */
1486 tf->event.event_size = tf->event.data_size;
1487 }
91f8d488 1488
1489 if (a_event_debug)
1490 print_debug_event_data(&tf->event);
1491
2fc874ab 1492 /* Having a marker load or marker format event out of the metadata
27c089c1 1493 * tracefiles is a serious bug. */
1494 switch((enum marker_id)tf->event.event_id) {
1495 case MARKER_ID_SET_MARKER_ID:
1496 case MARKER_ID_SET_MARKER_FORMAT:
2fc874ab 1497 if (tf->name != g_quark_from_string("/control/metadata"))
27c089c1 1498 g_error("Trace inconsistency : metadata event found in data "
1499 "tracefile %s", g_quark_to_string(tf->long_name));
1500 }
1501
d2007fbd 1502 if (tf->event.data_size != tf->event.event_size) {
3c165eaf 1503 struct marker_info *info = marker_get_info_from_id(tf->trace,
1504 tf->event.event_id);
1505 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1506 g_quark_to_string(info->name),
1507 tf->event.event_size, tf->event.data_size);
d2007fbd 1508 exit(-1);
1509 }
6cd62ccf 1510}
1511
6cd62ccf 1512
2fc874ab 1513/* Take the tf current event offset and use the event id to figure out where is
1514 * the next event offset.
3aee1200 1515 *
1516 * This is an internal function not aiming at being used elsewhere : it will
1517 * not jump over the current block limits. Please consider using
1518 * ltt_tracefile_read to do this.
1519 *
1520 * Returns 0 on success
1521 * ERANGE if we are at the end of the buffer.
1522 * ENOPROTOOPT if an error occured when getting the current event size.
1523 */
8655430b 1524static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1525{
3aee1200 1526 int ret = 0;
1527 void *pos;
3aee1200 1528
1529 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1530 if(tf->event.offset == 0) {
51551c6f 1531 tf->event.offset += tf->buffer_header_size;
b77d1b57 1532
f628823c 1533 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
b77d1b57 1534 ret = ERANGE;
1535 }
3aee1200 1536 goto found;
1537 }
2fc874ab 1538
3aee1200 1539 pos = tf->event.data;
1540
77175651 1541 if(tf->event.data_size < 0) goto error;
3aee1200 1542
77175651 1543 pos += (size_t)tf->event.data_size;
3aee1200 1544
eed2ef37 1545 tf->event.offset = pos - tf->buffer.head;
cb03932a 1546
f628823c 1547 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
cb03932a 1548 ret = ERANGE;
1549 goto found;
1550 }
36d36c9f 1551 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
3aee1200 1552
1553found:
1554 return ret;
1555
1556error:
1557 g_error("Error in ltt_seek_next_event for tracefile %s",
1558 g_quark_to_string(tf->name));
1559 return ENOPROTOOPT;
6cd62ccf 1560}
1561
f104d082 1562#if 0
3aee1200 1563/*****************************************************************************
6cd62ccf 1564 *Function name
3aee1200 1565 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 1566 *Input params
3aee1200 1567 * tracefile : opened trace file
1568 * event_type : the event type
6cd62ccf 1569 ****************************************************************************/
1570
3aee1200 1571void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 1572{
3aee1200 1573 LttField *field = event_type->root_field;
1574 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
1575
1576 if(likely(field))
1577 preset_field_type_size(tf, event_type, 0, 0,
1578 &fixed_root, &fixed_parent,
1579 field);
1580
1581}
f104d082 1582#endif //0
1583
1584
1585/*****************************************************************************
1586 *Function name
1587 * get_alignment : Get the alignment needed for a field.
1588 *Input params
f104d082 1589 * field : field
1590 *
1591 * returns : The size on which it must be aligned.
1592 *
1593 ****************************************************************************/
3c165eaf 1594#if 0
743e50fd 1595off_t get_alignment(LttField *field)
f104d082 1596{
2312de30 1597 LttType *type = &field->field_type;
f104d082 1598
1599 switch(type->type_class) {
1600 case LTT_INT_FIXED:
1601 case LTT_UINT_FIXED:
1602 case LTT_POINTER:
1603 case LTT_CHAR:
1604 case LTT_UCHAR:
1605 case LTT_SHORT:
1606 case LTT_USHORT:
1607 case LTT_INT:
1608 case LTT_UINT:
1609 case LTT_LONG:
1610 case LTT_ULONG:
1611 case LTT_SIZE_T:
1612 case LTT_SSIZE_T:
1613 case LTT_OFF_T:
1614 case LTT_FLOAT:
1615 case LTT_ENUM:
1616 /* Align offset on type size */
83e160f2 1617 g_assert(field->field_size != 0);
f104d082 1618 return field->field_size;
1619 break;
1620 case LTT_STRING:
83e160f2 1621 return 1;
f104d082 1622 break;
1623 case LTT_ARRAY:
1624 g_assert(type->fields->len == 1);
1625 {
1626 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 1627 return get_alignment(child);
f104d082 1628 }
1629 break;
1630 case LTT_SEQUENCE:
1631 g_assert(type->fields->len == 2);
1632 {
83e160f2 1633 off_t localign = 1;
f104d082 1634 LttField *child = &g_array_index(type->fields, LttField, 0);
1635
743e50fd 1636 localign = max(localign, get_alignment(child));
f104d082 1637
1638 child = &g_array_index(type->fields, LttField, 1);
743e50fd 1639 localign = max(localign, get_alignment(child));
f104d082 1640
1641 return localign;
1642 }
1643 break;
1644 case LTT_STRUCT:
1645 case LTT_UNION:
1646 {
1647 guint i;
83e160f2 1648 off_t localign = 1;
f104d082 1649
1650 for(i=0; i<type->fields->len; i++) {
1651 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 1652 localign = max(localign, get_alignment(child));
f104d082 1653 }
1654 return localign;
1655 }
1656 break;
1657 case LTT_NONE:
1658 default:
1659 g_error("get_alignment : unknown type");
83e160f2 1660 return -1;
f104d082 1661 }
f104d082 1662}
1663
3c165eaf 1664#endif //0
1665
f104d082 1666/*****************************************************************************
1667 *Function name
1668 * field_compute_static_size : Determine the size of fields known by their
1669 * sole definition. Unions, arrays and struct sizes might be known, but
1670 * the parser does not give that information.
1671 *Input params
1672 * tf : tracefile
1673 * field : field
1674 *
1675 ****************************************************************************/
3c165eaf 1676#if 0
743e50fd 1677void field_compute_static_size(LttFacility *fac, LttField *field)
f104d082 1678{
2312de30 1679 LttType *type = &field->field_type;
f104d082 1680
1681 switch(type->type_class) {
1682 case LTT_INT_FIXED:
1683 case LTT_UINT_FIXED:
1684 case LTT_POINTER:
1685 case LTT_CHAR:
1686 case LTT_UCHAR:
1687 case LTT_SHORT:
1688 case LTT_USHORT:
1689 case LTT_INT:
1690 case LTT_UINT:
1691 case LTT_LONG:
1692 case LTT_ULONG:
1693 case LTT_SIZE_T:
1694 case LTT_SSIZE_T:
1695 case LTT_OFF_T:
1696 case LTT_FLOAT:
1697 case LTT_ENUM:
1698 case LTT_STRING:
1699 /* nothing to do */
1700 break;
1701 case LTT_ARRAY:
1702 /* note this : array type size is the number of elements in the array,
1703 * while array field size of the length of the array in bytes */
1704 g_assert(type->fields->len == 1);
1705 {
1706 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 1707 field_compute_static_size(fac, child);
f104d082 1708
1709 if(child->field_size != 0) {
1710 field->field_size = type->size * child->field_size;
1711 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
1712 sizeof(off_t), type->size);
1713 } else {
1714 field->field_size = 0;
1715 }
1716 }
1717 break;
1718 case LTT_SEQUENCE:
1719 g_assert(type->fields->len == 2);
1720 {
2312de30 1721 off_t local_offset = 0;
f104d082 1722 LttField *child = &g_array_index(type->fields, LttField, 1);
743e50fd 1723 field_compute_static_size(fac, child);
f104d082 1724 field->field_size = 0;
1725 type->size = 0;
1726 if(child->field_size != 0) {
1727 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
1728 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
1729 }
1730 }
1731 break;
1732 case LTT_STRUCT:
1733 case LTT_UNION:
1734 {
1735 guint i;
1736 for(i=0;i<type->fields->len;i++) {
1737 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 1738 field_compute_static_size(fac, child);
f104d082 1739 if(child->field_size != 0) {
743e50fd 1740 type->size += ltt_align(type->size, get_alignment(child),
1741 fac->alignment);
f104d082 1742 type->size += child->field_size;
1743 } else {
1744 /* As soon as we find a child with variable size, we have
1745 * a variable size */
1746 type->size = 0;
1747 break;
1748 }
1749 }
1750 field->field_size = type->size;
1751 }
1752 break;
1753 default:
1754 g_error("field_static_size : unknown type");
2312de30 1755 }
f104d082 1756
1757}
3c165eaf 1758#endif //0
f104d082 1759
1760
1761/*****************************************************************************
1762 *Function name
1763 * precompute_fields_offsets : set the precomputable offset of the fields
1764 *Input params
743e50fd 1765 * fac : facility
f104d082 1766 * field : the field
1767 * offset : pointer to the current offset, must be incremented
1768 *
1769 * return : 1 : found a variable length field, stop the processing.
1770 * 0 otherwise.
1771 ****************************************************************************/
1772
3c165eaf 1773#if 0
dd3a6d39 1774gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset, gint is_compact)
f104d082 1775{
2312de30 1776 LttType *type = &field->field_type;
dd3a6d39 1777
1778 if(unlikely(is_compact)) {
1779 g_assert(field->field_size != 0);
1780 /* FIXME THIS IS A HUUUUUGE hack :
1781 * offset is between the compact_data field in struct LttEvent
1782 * and the address of the field root in the memory map.
1783 * ark. Both will stay at the same addresses while the event
1784 * is readable, so it's ok.
1785 */
1786 field->offset_root = 0;
1787 field->fixed_root = FIELD_FIXED;
1788 return 0;
1789 }
f104d082 1790
1791 switch(type->type_class) {
1792 case LTT_INT_FIXED:
1793 case LTT_UINT_FIXED:
1794 case LTT_POINTER:
1795 case LTT_CHAR:
1796 case LTT_UCHAR:
1797 case LTT_SHORT:
1798 case LTT_USHORT:
1799 case LTT_INT:
1800 case LTT_UINT:
1801 case LTT_LONG:
1802 case LTT_ULONG:
1803 case LTT_SIZE_T:
1804 case LTT_SSIZE_T:
1805 case LTT_OFF_T:
1806 case LTT_FLOAT:
1807 case LTT_ENUM:
743e50fd 1808 g_assert(field->field_size != 0);
f104d082 1809 /* Align offset on type size */
743e50fd 1810 *offset += ltt_align(*offset, get_alignment(field),
1811 fac->alignment);
f104d082 1812 /* remember offset */
1813 field->offset_root = *offset;
1814 field->fixed_root = FIELD_FIXED;
1815 /* Increment offset */
1816 *offset += field->field_size;
1817 return 0;
1818 break;
1819 case LTT_STRING:
1820 field->offset_root = *offset;
1821 field->fixed_root = FIELD_FIXED;
1822 return 1;
1823 break;
1824 case LTT_ARRAY:
1825 g_assert(type->fields->len == 1);
1826 {
1827 LttField *child = &g_array_index(type->fields, LttField, 0);
1828
743e50fd 1829 *offset += ltt_align(*offset, get_alignment(field),
1830 fac->alignment);
f104d082 1831
1832 /* remember offset */
1833 field->offset_root = *offset;
1834 field->array_offset = *offset;
1835 field->fixed_root = FIELD_FIXED;
1836
1837 /* Let the child be variable */
1838 //precompute_fields_offsets(tf, child, offset);
1839
1840 if(field->field_size != 0) {
1841 /* Increment offset */
1842 /* field_size is the array size in bytes */
1843 *offset += field->field_size;
1844 return 0;
1845 } else {
1846 return 1;
1847 }
1848 }
1849 break;
1850 case LTT_SEQUENCE:
1851 g_assert(type->fields->len == 2);
1852 {
1853 LttField *child;
1854 guint ret;
1855
743e50fd 1856 *offset += ltt_align(*offset, get_alignment(field),
1857 fac->alignment);
f104d082 1858
1859 /* remember offset */
1860 field->offset_root = *offset;
1861 field->fixed_root = FIELD_FIXED;
1862
1863 child = &g_array_index(type->fields, LttField, 0);
dd3a6d39 1864 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 1865 g_assert(ret == 0); /* Seq len cannot have variable len */
1866
1867 child = &g_array_index(type->fields, LttField, 1);
743e50fd 1868 *offset += ltt_align(*offset, get_alignment(child),
1869 fac->alignment);
f104d082 1870 field->array_offset = *offset;
743e50fd 1871 /* Let the child be variable. */
1872 //ret = precompute_fields_offsets(fac, child, offset);
f104d082 1873
1874 /* Cannot precompute fields offsets of sequence members, and has
1875 * variable length. */
1876 return 1;
1877 }
1878 break;
1879 case LTT_STRUCT:
1880 {
1881 LttField *child;
1882 guint i;
1883 gint ret=0;
1884
743e50fd 1885 *offset += ltt_align(*offset, get_alignment(field),
1886 fac->alignment);
f104d082 1887 /* remember offset */
1888 field->offset_root = *offset;
1889 field->fixed_root = FIELD_FIXED;
1890
1891 for(i=0; i< type->fields->len; i++) {
1892 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 1893 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 1894
1895 if(ret) break;
1896 }
1897 return ret;
1898 }
1899 break;
1900 case LTT_UNION:
1901 {
1902 LttField *child;
1903 guint i;
1904 gint ret=0;
1905
743e50fd 1906 *offset += ltt_align(*offset, get_alignment(field),
1907 fac->alignment);
f104d082 1908 /* remember offset */
1909 field->offset_root = *offset;
1910 field->fixed_root = FIELD_FIXED;
1911
1912 for(i=0; i< type->fields->len; i++) {
1913 *offset = field->offset_root;
1914 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 1915 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 1916
1917 if(ret) break;
1918 }
1919 *offset = field->offset_root + field->field_size;
1920 return ret;
1921 }
1922
1923 break;
1924 case LTT_NONE:
1925 default:
1926 g_error("precompute_fields_offsets : unknown type");
1927 return 1;
1928 }
1929
1930}
1931
3c165eaf 1932#endif //0
f104d082 1933
3c165eaf 1934#if 0
f104d082 1935/*****************************************************************************
1936 *Function name
1937 * precompute_offsets : set the precomputable offset of an event type
1938 *Input params
1939 * tf : tracefile
1940 * event : event type
1941 *
1942 ****************************************************************************/
743e50fd 1943void precompute_offsets(LttFacility *fac, LttEventType *event)
f104d082 1944{
1945 guint i;
1946 off_t offset = 0;
1947 gint ret;
1948
1949 /* First, compute the size of fixed size fields. Will determine size for
1950 * arrays, struct and unions, which is not done by the parser */
1951 for(i=0; i<event->fields->len; i++) {
1952 LttField *field = &g_array_index(event->fields, LttField, i);
743e50fd 1953 field_compute_static_size(fac, field);
f104d082 1954 }
1955
1956 /* Precompute all known offsets */
1957 for(i=0; i<event->fields->len; i++) {
1958 LttField *field = &g_array_index(event->fields, LttField, i);
dd3a6d39 1959 if(event->has_compact_data && i == 0)
1960 ret = precompute_fields_offsets(fac, field, &offset, 1);
1961 else
1962 ret = precompute_fields_offsets(fac, field, &offset, 0);
f104d082 1963 if(ret) break;
1964 }
1965}
3c165eaf 1966#endif //0
f104d082 1967
e4eced0f 1968
bbf28e50 1969
3aee1200 1970/*****************************************************************************
1971 *Function name
1972 * preset_field_type_size : set the fixed sizes of the field type
1973 *Input params
1974 * tf : tracefile
1975 * event_type : event type
1976 * offset_root : offset from the root
1977 * offset_parent : offset from the parent
1978 * fixed_root : Do we know a fixed offset to the root ?
1979 * fixed_parent : Do we know a fixed offset to the parent ?
1980 * field : field
1981 ****************************************************************************/
f104d082 1982
1983
1984
1985// preset the fixed size offsets. Calculate them just like genevent-new : an
1986// increment of a *to value that represents the offset from the start of the
1987// event data.
1988// The preset information is : offsets up to (and including) the first element
1989// of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
1990#if 0
3aee1200 1991void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
1992 off_t offset_root, off_t offset_parent,
1993 enum field_status *fixed_root, enum field_status *fixed_parent,
1994 LttField *field)
1995{
1996 enum field_status local_fixed_root, local_fixed_parent;
1997 guint i;
1998 LttType *type;
dfb73233 1999
3aee1200 2000 g_assert(field->fixed_root == FIELD_UNKNOWN);
2001 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2002 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 2003
3aee1200 2004 type = field->field_type;
2005
2006 field->fixed_root = *fixed_root;
2007 if(field->fixed_root == FIELD_FIXED)
2008 field->offset_root = offset_root;
2009 else
2010 field->offset_root = 0;
2011
2012 field->fixed_parent = *fixed_parent;
2013 if(field->fixed_parent == FIELD_FIXED)
2014 field->offset_parent = offset_parent;
2015 else
2016 field->offset_parent = 0;
2017
2018 size_t current_root_offset;
2019 size_t current_offset;
2020 enum field_status current_child_status, final_child_status;
2021 size_t max_size;
2022
2023 switch(type->type_class) {
83e160f2 2024 case LTT_INT_FIXED:
2025 case LTT_UINT_FIXED:
2026 case LTT_CHAR:
2027 case LTT_UCHAR:
2028 case LTT_SHORT:
2029 case LTT_USHORT:
3aee1200 2030 case LTT_INT:
2031 case LTT_UINT:
2032 case LTT_FLOAT:
2033 case LTT_ENUM:
2034 field->field_size = ltt_type_size(tf->trace, type);
2035 field->fixed_size = FIELD_FIXED;
2036 break;
2037 case LTT_POINTER:
2038 field->field_size = (off_t)event_type->facility->pointer_size;
2039 field->fixed_size = FIELD_FIXED;
2040 break;
2041 case LTT_LONG:
2042 case LTT_ULONG:
cb03932a 2043 field->field_size = (off_t)event_type->facility->long_size;
3aee1200 2044 field->fixed_size = FIELD_FIXED;
2045 break;
2046 case LTT_SIZE_T:
2047 case LTT_SSIZE_T:
2048 case LTT_OFF_T:
2049 field->field_size = (off_t)event_type->facility->size_t_size;
2050 field->fixed_size = FIELD_FIXED;
2051 break;
2052 case LTT_SEQUENCE:
2053 local_fixed_root = FIELD_VARIABLE;
2054 local_fixed_parent = FIELD_VARIABLE;
2055 preset_field_type_size(tf, event_type,
2056 0, 0,
2057 &local_fixed_root, &local_fixed_parent,
2058 field->child[0]);
2059 field->fixed_size = FIELD_VARIABLE;
2060 field->field_size = 0;
27304273 2061 *fixed_root = FIELD_VARIABLE;
2062 *fixed_parent = FIELD_VARIABLE;
3aee1200 2063 break;
2064 case LTT_STRING:
2065 field->fixed_size = FIELD_VARIABLE;
2066 field->field_size = 0;
27304273 2067 *fixed_root = FIELD_VARIABLE;
2068 *fixed_parent = FIELD_VARIABLE;
3aee1200 2069 break;
2070 case LTT_ARRAY:
2071 local_fixed_root = FIELD_VARIABLE;
2072 local_fixed_parent = FIELD_VARIABLE;
2073 preset_field_type_size(tf, event_type,
2074 0, 0,
2075 &local_fixed_root, &local_fixed_parent,
2076 field->child[0]);
2077 field->fixed_size = field->child[0]->fixed_size;
27304273 2078 if(field->fixed_size == FIELD_FIXED) {
3aee1200 2079 field->field_size = type->element_number * field->child[0]->field_size;
27304273 2080 } else {
3aee1200 2081 field->field_size = 0;
27304273 2082 *fixed_root = FIELD_VARIABLE;
2083 *fixed_parent = FIELD_VARIABLE;
2084 }
3aee1200 2085 break;
2086 case LTT_STRUCT:
2087 current_root_offset = field->offset_root;
2088 current_offset = 0;
2089 current_child_status = FIELD_FIXED;
2090 for(i=0;i<type->element_number;i++) {
2091 preset_field_type_size(tf, event_type,
2092 current_root_offset, current_offset,
2093 fixed_root, &current_child_status,
2094 field->child[i]);
2095 if(current_child_status == FIELD_FIXED) {
2096 current_root_offset += field->child[i]->field_size;
2097 current_offset += field->child[i]->field_size;
2098 } else {
2099 current_root_offset = 0;
2100 current_offset = 0;
2101 }
2102 }
2103 if(current_child_status != FIELD_FIXED) {
2104 *fixed_parent = current_child_status;
2105 field->field_size = 0;
2106 field->fixed_size = current_child_status;
2107 } else {
2108 field->field_size = current_offset;
2109 field->fixed_size = FIELD_FIXED;
2110 }
2111 break;
2112 case LTT_UNION:
2113 current_root_offset = field->offset_root;
2114 current_offset = 0;
2115 max_size = 0;
2116 final_child_status = FIELD_FIXED;
2117 for(i=0;i<type->element_number;i++) {
2118 enum field_status current_root_child_status = FIELD_FIXED;
2119 enum field_status current_child_status = FIELD_FIXED;
2120 preset_field_type_size(tf, event_type,
2121 current_root_offset, current_offset,
2122 &current_root_child_status, &current_child_status,
2123 field->child[i]);
2124 if(current_child_status != FIELD_FIXED)
2125 final_child_status = current_child_status;
2126 else
2127 max_size = max(max_size, field->child[i]->field_size);
2128 }
2129 if(final_child_status != FIELD_FIXED) {
62e4e7bf 2130 g_error("LTTV does not support variable size fields in unions.");
2131 /* This will stop the application. */
3aee1200 2132 *fixed_root = final_child_status;
2133 *fixed_parent = final_child_status;
2134 field->field_size = 0;
2135 field->fixed_size = current_child_status;
2136 } else {
2137 field->field_size = max_size;
2138 field->fixed_size = FIELD_FIXED;
2139 }
2140 break;
83e160f2 2141 case LTT_NONE:
2142 g_error("unexpected type NONE");
2143 break;
dfb73233 2144 }
2145
6cd62ccf 2146}
f104d082 2147#endif //0
3aee1200 2148
77175651 2149/*****************************************************************************
2150 *Function name
2151 * check_fields_compatibility : Check for compatibility between two fields :
2152 * do they use the same inner structure ?
2153 *Input params
2154 * event_type1 : event type
2155 * event_type2 : event type
2156 * field1 : field
2157 * field2 : field
2158 *Returns : 0 if identical
2159 * 1 if not.
2160 ****************************************************************************/
f104d082 2161// this function checks for equality of field types. Therefore, it does not use
2162// per se offsets. For instance, an aligned version of a structure is
2163// compatible with an unaligned version of the same structure.
3c165eaf 2164#if 0
f104d082 2165gint check_fields_compatibility(LttEventType *event_type1,
2166 LttEventType *event_type2,
2167 LttField *field1, LttField *field2)
2168{
2169 guint different = 0;
2312de30 2170 LttType *type1;
2171 LttType *type2;
f104d082 2172
2173 if(field1 == NULL) {
2174 if(field2 == NULL) goto end;
2175 else {
2176 different = 1;
2177 goto end;
2178 }
2179 } else if(field2 == NULL) {
2180 different = 1;
2181 goto end;
2182 }
2183
2312de30 2184 type1 = &field1->field_type;
2185 type2 = &field2->field_type;
f104d082 2186
2187 if(type1->type_class != type2->type_class) {
2188 different = 1;
2189 goto end;
2190 }
62e4e7bf 2191 if(type1->network != type2->network) {
2192 different = 1;
2193 goto end;
2194 }
f104d082 2195
2196 switch(type1->type_class) {
2197 case LTT_INT_FIXED:
2198 case LTT_UINT_FIXED:
2199 case LTT_POINTER:
2200 case LTT_CHAR:
2201 case LTT_UCHAR:
2202 case LTT_SHORT:
2203 case LTT_USHORT:
2204 case LTT_INT:
2205 case LTT_UINT:
2206 case LTT_LONG:
2207 case LTT_ULONG:
2208 case LTT_SIZE_T:
2209 case LTT_SSIZE_T:
2210 case LTT_OFF_T:
2211 case LTT_FLOAT:
2212 case LTT_ENUM:
2213 if(field1->field_size != field2->field_size)
2214 different = 1;
2215 break;
2216 case LTT_STRING:
2217 break;
2218 case LTT_ARRAY:
2219 {
2220 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2221 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2222
2223 if(type1->size != type2->size)
2224 different = 1;
2225 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2226 different = 1;
2227 }
2228 break;
2229 case LTT_SEQUENCE:
2230 {
2231 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2232 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2233
2234 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2235 different = 1;
2236 }
2237 break;
2238 case LTT_STRUCT:
2239 case LTT_UNION:
2240 {
2241 LttField *child;
2242 guint i;
2243
2244 if(type1->fields->len != type2->fields->len) {
2245 different = 1;
2246 goto end;
2247 }
2248
2249 for(i=0; i< type1->fields->len; i++) {
2312de30 2250 LttField *child1;
2251 LttField *child2;
f104d082 2252 child1 = &g_array_index(type1->fields, LttField, i);
2253 child2 = &g_array_index(type2->fields, LttField, i);
2254 different = check_fields_compatibility(event_type1,
2255 event_type2, child1, child2);
2256
2257 if(different) break;
2258 }
2259 }
2260 break;
2261 case LTT_NONE:
2262 default:
dd3a6d39 2263 g_error("check_fields_compatibility : unknown type");
f104d082 2264 }
2265
2266end:
2267 return different;
2268}
3c165eaf 2269#endif //0
f104d082 2270
2271#if 0
77175651 2272gint check_fields_compatibility(LttEventType *event_type1,
2273 LttEventType *event_type2,
2274 LttField *field1, LttField *field2)
2275{
2276 guint different = 0;
77175651 2277 guint i;
2278 LttType *type1;
2279 LttType *type2;
2280
2281 if(field1 == NULL) {
2282 if(field2 == NULL) goto end;
2283 else {
2284 different = 1;
2285 goto end;
2286 }
2287 } else if(field2 == NULL) {
2288 different = 1;
2289 goto end;
2290 }
2291
2292 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2293 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2294 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2295 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2296 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2297 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2298
2299 type1 = field1->field_type;
2300 type2 = field2->field_type;
2301
77175651 2302 if(type1->type_class != type2->type_class) {
2303 different = 1;
2304 goto end;
2305 }
2306 if(type1->element_name != type2->element_name) {
2307 different = 1;
2308 goto end;
2309 }
2310
2311 switch(type1->type_class) {
83e160f2 2312 case LTT_INT_FIXED:
2313 case LTT_UINT_FIXED:
2314 case LTT_POINTER:
2315 case LTT_CHAR:
2316 case LTT_UCHAR:
2317 case LTT_SHORT:
2318 case LTT_USHORT:
77175651 2319 case LTT_INT:
2320 case LTT_UINT:
2321 case LTT_FLOAT:
2322 case LTT_POINTER:
2323 case LTT_LONG:
2324 case LTT_ULONG:
2325 case LTT_SIZE_T:
2326 case LTT_SSIZE_T:
2327 case LTT_OFF_T:
2328 if(field1->field_size != field2->field_size) {
2329 different = 1;
2330 goto end;
2331 }
2332 break;
2333 case LTT_ENUM:
2334 if(type1->element_number != type2->element_number) {
2335 different = 1;
2336 goto end;
2337 }
2338 for(i=0;i<type1->element_number;i++) {
2339 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2340 different = 1;
2341 goto end;
2342 }
2343 }
2344 break;
2345 case LTT_SEQUENCE:
2346 /* Two elements : size and child */
2347 g_assert(type1->element_number != type2->element_number);
2348 for(i=0;i<type1->element_number;i++) {
2349 if(check_fields_compatibility(event_type1, event_type2,
2350 field1->child[0], field2->child[0])) {
2351 different = 1;
2352 goto end;
2353 }
2354 }
2355 break;
2356 case LTT_STRING:
2357 break;
2358 case LTT_ARRAY:
2359 if(field1->field_size != field2->field_size) {
2360 different = 1;
2361 goto end;
2362 }
2363 /* Two elements : size and child */
2364 g_assert(type1->element_number != type2->element_number);
2365 for(i=0;i<type1->element_number;i++) {
2366 if(check_fields_compatibility(event_type1, event_type2,
2367 field1->child[0], field2->child[0])) {
2368 different = 1;
2369 goto end;
2370 }
2371 }
2372 break;
2373 case LTT_STRUCT:
2374 case LTT_UNION:
2375 if(type1->element_number != type2->element_number) {
2376 different = 1;
2377 break;
2378 }
2379 for(i=0;i<type1->element_number;i++) {
2380 if(check_fields_compatibility(event_type1, event_type2,
2381 field1->child[0], field2->child[0])) {
2382 different = 1;
2383 goto end;
2384 }
2385 }
2386 break;
2387 }
2388end:
2389 return different;
2390}
3aee1200 2391#endif //0
6cd62ccf 2392
f104d082 2393
6cd62ccf 2394/*****************************************************************************
2395 *Function name
eed2ef37 2396 * ltt_get_int : get an integer number
6cd62ccf 2397 *Input params
3aee1200 2398 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 2399 * size : the size of the integer
3aee1200 2400 * ptr : the data pointer
6cd62ccf 2401 *Return value
cf74a6f1 2402 * gint64 : a 64 bits integer
6cd62ccf 2403 ****************************************************************************/
2404
eed2ef37 2405gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2406{
3aee1200 2407 gint64 val;
cf74a6f1 2408
2409 switch(size) {
3aee1200 2410 case 1: val = *((gint8*)data); break;
2411 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
2412 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
2413 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
2414 default: val = ltt_get_int64(reverse_byte_order, data);
2415 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 2416 break;
2417 }
2418
3aee1200 2419 return val;
6cd62ccf 2420}
3aee1200 2421
6cd62ccf 2422/*****************************************************************************
2423 *Function name
eed2ef37 2424 * ltt_get_uint : get an unsigned integer number
6cd62ccf 2425 *Input params
3aee1200 2426 * reverse_byte_order: must we reverse the byte order ?
2427 * size : the size of the integer
2428 * ptr : the data pointer
2429 *Return value
2430 * guint64 : a 64 bits unsigned integer
6cd62ccf 2431 ****************************************************************************/
2432
eed2ef37 2433guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 2434{
3aee1200 2435 guint64 val;
2436
2437 switch(size) {
2438 case 1: val = *((gint8*)data); break;
2439 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
2440 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
2441 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
2442 default: val = ltt_get_uint64(reverse_byte_order, data);
2443 g_critical("get_uint : unsigned integer size %d unknown",
2444 size);
2445 break;
2446 }
2447
2448 return val;
6cd62ccf 2449}
3aee1200 2450
2451
a5dcde2f 2452/* get the node name of the system */
2453
2454char * ltt_trace_system_description_node_name (LttSystemDescription * s)
2455{
2456 return s->node_name;
2457}
2458
2459
2460/* get the domain name of the system */
2461
2462char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
2463{
2464 return s->domain_name;
2465}
2466
2467
2468/* get the description of the system */
2469
2470char * ltt_trace_system_description_description (LttSystemDescription * s)
2471{
2472 return s->description;
2473}
2474
2475
bf33dd50 2476/* get the NTP corrected start time of the trace */
7bd563ec 2477LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 2478{
7bd563ec 2479 return t->start_time;
a5dcde2f 2480}
2481
bf33dd50 2482/* get the monotonic start time of the trace */
2483LttTime ltt_trace_start_time_monotonic(LttTrace *t)
2484{
2485 return t->start_time_from_tsc;
2486}
2487
8655430b 2488static LttTracefile *ltt_tracefile_new()
18206708 2489{
afd57a3c 2490 LttTracefile *tf;
2491 tf = g_new(LttTracefile, 1);
2492 tf->event.tracefile = tf;
2493 return tf;
18206708 2494}
2495
8655430b 2496static void ltt_tracefile_destroy(LttTracefile *tf)
18206708 2497{
2498 g_free(tf);
2499}
2500
8655430b 2501static void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
18206708 2502{
2503 *dest = *src;
2504}
2505
3aee1200 2506/* Before library loading... */
2507
8655430b 2508static __attribute__((constructor)) void init(void)
3aee1200 2509{
2fc874ab 2510 LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("/control/metadata");
3aee1200 2511}
This page took 0.199803 seconds and 4 git commands to generate.