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