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