Some cleaning, modified to respect kernel standard.
[lttv.git] / 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>
43ed82b5 35#include <glib/gprintf.h>
3aee1200 36#include <malloc.h>
37#include <sys/mman.h>
dd3a6d39 38#include <string.h>
43ed82b5 39#include <ctype.h>
40#include <inttypes.h>
6cd62ccf 41
ef35d837 42// For realpath
43#include <limits.h>
44#include <stdlib.h>
45
46
a5dcde2f 47#include <ltt/ltt.h>
48#include "ltt-private.h"
963b5f2d 49#include <ltt/trace.h>
c02ea99f 50#include <ltt/event.h>
1a2ceb63 51#include <ltt/ltt-types.h>
bb38a290 52#include <ltt/marker.h>
3aee1200 53
f0241068
MD
54#define DEFAULT_N_BLOCKS 32
55
43ed82b5 56/* from marker.c */
57extern long marker_update_fields_offsets(struct marker_info *info, const char *data);
58
2fc874ab 59/* Tracefile names used in this file */
3aee1200 60
2fc874ab 61GQuark LTT_TRACEFILE_NAME_METADATA;
3aee1200 62
86005ded 63#ifndef g_open
64#define g_open open
65#endif
66
67
51b5991e 68#define __UNUSED__ __attribute__((__unused__))
6cd62ccf 69
a1062ddd 70#define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
3aee1200 71
72#ifndef g_debug
a1062ddd 73#define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
3aee1200 74#endif
a1062ddd 75
45e14832 76#define g_close close
8959a0c8 77
b77d1b57 78/* Those macros must be called from within a function where page_size is a known
79 * variable */
80#define PAGE_MASK (~(page_size-1))
81#define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
82
6cd62ccf 83/* set the offset of the fields belonging to the event,
84 need the information of the archecture */
f104d082 85//void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
eed2ef37 86//size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
6cd62ccf 87
3aee1200 88/* map a fixed size or a block information from the file (fd) */
89static gint map_block(LttTracefile * tf, guint block_num);
90
91/* calculate nsec per cycles for current block */
791dffa6 92#if 0
93static guint32 calc_nsecs_per_cycle(LttTracefile * t);
94static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
95#endif //0
6cd62ccf 96
3aee1200 97/* go to the next event */
98static int ltt_seek_next_event(LttTracefile *tf);
6cd62ccf 99
3c165eaf 100static int open_tracefiles(LttTrace *trace, gchar *root_path,
101 gchar *relative_path);
2fc874ab 102static int ltt_process_metadata_tracefile(LttTracefile *tf);
3c165eaf 103static void ltt_tracefile_time_span_get(LttTracefile *tf,
104 LttTime *start, LttTime *end);
105static void group_time_span_get(GQuark name, gpointer data, gpointer user_data);
106static gint map_block(LttTracefile * tf, guint block_num);
3c165eaf 107static void ltt_update_event_size(LttTracefile *tf);
91f8d488 108
109/* Enable event debugging */
110static int a_event_debug = 0;
111
112void ltt_event_debug(int state)
113{
114 a_event_debug = state;
115}
116
b7576a11 117/* trace can be NULL
118 *
119 * Return value : 0 success, 1 bad tracefile
120 */
64dd41a5 121static int parse_trace_header(ltt_subbuffer_header_t *header,
122 LttTracefile *tf, LttTrace *t)
b7576a11 123{
64dd41a5 124 if (header->magic_number == LTT_MAGIC_NUMBER)
b7576a11 125 tf->reverse_bo = 0;
64dd41a5 126 else if(header->magic_number == LTT_REV_MAGIC_NUMBER)
b7576a11 127 tf->reverse_bo = 1;
128 else /* invalid magic number, bad tracefile ! */
129 return 1;
64dd41a5 130
131 if(t) {
132 t->ltt_major_version = header->major_version;
133 t->ltt_minor_version = header->minor_version;
134 t->arch_size = header->arch_size;
135 }
136 tf->alignment = header->alignment;
137
b7576a11 138 /* Get float byte order : might be different from int byte order
139 * (or is set to 0 if the trace has no float (kernel trace)) */
64dd41a5 140 tf->float_word_order = 0;
b7576a11 141
64dd41a5 142 switch(header->major_version) {
b7576a11 143 case 0:
2fc874ab 144 case 1:
3c165eaf 145 g_warning("Unsupported trace version : %hhu.%hhu",
64dd41a5 146 header->major_version, header->minor_version);
3c165eaf 147 return 1;
148 break;
2fc874ab 149 case 2:
64dd41a5 150 switch(header->minor_version) {
f0241068 151 case 5:
b7576a11 152 {
f0241068 153 struct ltt_subbuffer_header_2_5 *vheader = header;
1550fba6 154 tf->buffer_header_size = ltt_subbuffer_header_size();
64dd41a5 155 tf->tscbits = 27;
156 tf->eventbits = 5;
2fc874ab 157 tf->tsc_mask = ((1ULL << tf->tscbits) - 1);
158 tf->tsc_mask_next_bit = (1ULL << tf->tscbits);
159
e939e5b2 160 if(t) {
161 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
162 &vheader->start_freq);
a3999b49 163 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
164 &vheader->freq_scale);
e939e5b2 165 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 166 &vheader->cycle_count_begin);
167 t->start_monotonic = 0;
e939e5b2 168 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
169 &vheader->start_time_sec);
170 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
171 &vheader->start_time_usec);
172 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
173
9c9bf2d4
BP
174 t->start_time_from_tsc =
175 ltt_time_from_uint64(tsc_to_uint64(t->freq_scale,
176 t->start_freq, t->start_tsc));
e939e5b2 177 }
178 }
179 break;
b7576a11 180 default:
986e2a7c 181 g_warning("Unsupported trace version : %hhu.%hhu",
64dd41a5 182 header->major_version, header->minor_version);
b7576a11 183 return 1;
184 }
185 break;
b7576a11 186 default:
187 g_warning("Unsupported trace version : %hhu.%hhu",
64dd41a5 188 header->major_version, header->minor_version);
b7576a11 189 return 1;
190 }
b7576a11 191 return 0;
192}
193
f0241068
MD
194int get_block_offset_size(LttTracefile *tf, guint block_num,
195 uint64_t *offset, uint32_t *size)
196{
197 uint64_t offa, offb;
198
199 if (unlikely(block_num >= tf->num_blocks))
200 return -1;
b7576a11 201
f0241068
MD
202 offa = g_array_index(tf->buf_index, uint64_t, block_num);
203 if (likely(block_num < tf->num_blocks - 1))
204 offb = g_array_index(tf->buf_index, uint64_t, block_num + 1);
205 else
206 offb = tf->file_size;
207 *offset = offa;
208 *size = offb - offa;
209 return 0;
210}
211
212int ltt_trace_create_block_index(LttTracefile *tf)
213{
214 int page_size = getpagesize();
215 uint64_t offset = 0;
216 unsigned long i = 0;
217 unsigned int header_map_size = PAGE_ALIGN(ltt_subbuffer_header_size());
218
219 tf->buf_index = g_array_sized_new(FALSE, TRUE, sizeof(uint64_t),
220 DEFAULT_N_BLOCKS);
221
222 g_assert(tf->buf_index->len == i);
223
224 while (offset < tf->file_size) {
225 ltt_subbuffer_header_t *header;
226 uint64_t *off;
227
228 tf->buf_index = g_array_set_size(tf->buf_index, i + 1);
229 off = &g_array_index(tf->buf_index, uint64_t, i);
230 *off = offset;
231
232 /* map block header */
233 header = mmap(0, header_map_size, PROT_READ,
93556665 234 MAP_PRIVATE, tf->fd, (off_t)offset);
f0241068
MD
235 if(header == MAP_FAILED) {
236 perror("Error in allocating memory for buffer of tracefile");
237 return -1;
238 }
239
240 /* read len, offset += len */
241 offset += ltt_get_uint32(LTT_GET_BO(tf), &header->sb_size);
242
243 /* unmap block header */
244 if(munmap(header, header_map_size)) {
245 g_warning("unmap size : %u\n", header_map_size);
246 perror("munmap error");
247 return -1;
248 }
249 ++i;
250 }
251 tf->num_blocks = i;
252
253 return 0;
254}
b7576a11 255
6cd62ccf 256/*****************************************************************************
257 *Function name
963b5f2d 258 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 259 *Input params
963b5f2d 260 * t : the trace containing the tracefile
261 * fileName : path name of the trace file
3aee1200 262 * tf : the tracefile structure
6cd62ccf 263 *Return value
3aee1200 264 * : 0 for success, -1 otherwise.
6cd62ccf 265 ****************************************************************************/
266
8655430b 267static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
6cd62ccf 268{
963b5f2d 269 struct stat lTDFStat; /* Trace data file status */
64dd41a5 270 ltt_subbuffer_header_t *header;
b77d1b57 271 int page_size = getpagesize();
6cd62ccf 272
273 //open the file
d3d34f49 274 tf->long_name = g_quark_from_string(fileName);
963b5f2d 275 tf->trace = t;
3865ea09 276 tf->fd = open(fileName, O_RDONLY);
f0241068 277 tf->buf_index = NULL;
6cd62ccf 278 if(tf->fd < 0){
2a74fbf4 279 g_warning("Unable to open input data file %s\n", fileName);
3aee1200 280 goto end;
6cd62ccf 281 }
282
283 // Get the file's status
284 if(fstat(tf->fd, &lTDFStat) < 0){
2a74fbf4 285 g_warning("Unable to get the status of the input data file %s\n", fileName);
3aee1200 286 goto close_file;
6cd62ccf 287 }
288
289 // Is the file large enough to contain a trace
823820eb 290 if(lTDFStat.st_size <
1550fba6 291 (off_t)(ltt_subbuffer_header_size())){
8710c6c7 292 g_print("The input data file %s does not contain a trace\n", fileName);
3aee1200 293 goto close_file;
294 }
295
296 /* Temporarily map the buffer start header to get trace information */
297 /* Multiple of pages aligned head */
b77d1b57 298 tf->buffer.head = mmap(0,
1550fba6 299 PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ,
3aee1200 300 MAP_PRIVATE, tf->fd, 0);
3865ea09 301 if(tf->buffer.head == MAP_FAILED) {
3aee1200 302 perror("Error in allocating memory for buffer of tracefile");
303 goto close_file;
6cd62ccf 304 }
f64fedd7 305 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 306
64dd41a5 307 header = (ltt_subbuffer_header_t *)tf->buffer.head;
6cd62ccf 308
64dd41a5 309 if(parse_trace_header(header, tf, NULL)) {
51551c6f 310 g_warning("parse_trace_header error");
311 goto unmap_file;
312 }
3aee1200 313
6cd62ccf 314 //store the size of the file
315 tf->file_size = lTDFStat.st_size;
426f6149 316 tf->events_lost = 0;
317 tf->subbuf_corrupt = 0;
f628823c 318
319 if(munmap(tf->buffer.head,
1550fba6 320 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
43ed82b5 321 g_warning("unmap size : %zu\n",
1550fba6 322 PAGE_ALIGN(ltt_subbuffer_header_size()));
f628823c 323 perror("munmap error");
324 g_assert(0);
325 }
3aee1200 326 tf->buffer.head = NULL;
6cd62ccf 327
f0241068
MD
328 /* Create block index */
329 ltt_trace_create_block_index(tf);
330
963b5f2d 331 //read the first block
3aee1200 332 if(map_block(tf,0)) {
333 perror("Cannot map block for tracefile");
334 goto close_file;
335 }
336
337 return 0;
6cd62ccf 338
3aee1200 339 /* Error */
340unmap_file:
f628823c 341 if(munmap(tf->buffer.head,
1550fba6 342 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
43ed82b5 343 g_warning("unmap size : %zu\n",
1550fba6 344 PAGE_ALIGN(ltt_subbuffer_header_size()));
f628823c 345 perror("munmap error");
346 g_assert(0);
347 }
3aee1200 348close_file:
3865ea09 349 close(tf->fd);
3aee1200 350end:
f0241068
MD
351 if (tf->buf_index)
352 g_array_free(tf->buf_index, TRUE);
3aee1200 353 return -1;
6cd62ccf 354}
355
6cd62ccf 356
357/*****************************************************************************
358 *Function name
963b5f2d 359 * ltt_tracefile_close: close a trace file,
6cd62ccf 360 *Input params
963b5f2d 361 * t : tracefile which will be closed
6cd62ccf 362 ****************************************************************************/
363
8655430b 364static void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 365{
f628823c 366 int page_size = getpagesize();
367
3aee1200 368 if(t->buffer.head != NULL)
f0241068 369 if(munmap(t->buffer.head, PAGE_ALIGN(t->buffer.size))) {
f628823c 370 g_warning("unmap size : %u\n",
f0241068 371 PAGE_ALIGN(t->buffer.size));
f628823c 372 perror("munmap error");
373 g_assert(0);
374 }
375
3865ea09 376 close(t->fd);
f0241068
MD
377 if (t->buf_index)
378 g_array_free(t->buf_index, TRUE);
963b5f2d 379}
6cd62ccf 380
8d1e6362 381/****************************************************************************
382 * get_absolute_pathname
803229fa 383 *
8d1e6362 384 * return the unique pathname in the system
385 *
ef35d837 386 * MD : Fixed this function so it uses realpath, dealing well with
387 * forgotten cases (.. were not used correctly before).
803229fa 388 *
963b5f2d 389 ****************************************************************************/
45e14832 390void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
9f797243 391{
9f797243 392 abs_pathname[0] = '\0';
803229fa 393
2fc874ab 394 if (realpath(pathname, abs_pathname) != NULL)
ef35d837 395 return;
396 else
397 {
8d1e6362 398 /* error, return the original path unmodified */
ef35d837 399 strcpy(abs_pathname, pathname);
9f797243 400 return;
401 }
ef35d837 402 return;
9f797243 403}
404
3aee1200 405/* Search for something like : .*_.*
406 *
407 * The left side is the name, the right side is the number.
750eb11a 408 * Exclude leading /.
4ad053df 409 * Exclude flight- prefix.
3aee1200 410 */
411
8655430b 412static int get_tracefile_name_number(gchar *raw_name,
3aee1200 413 GQuark *name,
ae3d0f50 414 guint *num,
f64fedd7 415 gulong *tid,
416 gulong *pgid,
62e4e7bf 417 guint64 *creation)
6cd62ccf 418{
3aee1200 419 guint raw_name_len = strlen(raw_name);
420 gchar char_name[PATH_MAX];
8af0138e 421 int i;
3aee1200 422 int underscore_pos;
423 long int cpu_num;
424 gchar *endptr;
62e4e7bf 425 gchar *tmpptr;
3aee1200 426
750eb11a 427 /* skip leading / */
428 for(i = 0; i < raw_name_len-1;i++) {
429 if(raw_name[i] != '/')
430 break;
431 }
432 raw_name = &raw_name[i];
433 raw_name_len = strlen(raw_name);
434
3aee1200 435 for(i=raw_name_len-1;i>=0;i--) {
436 if(raw_name[i] == '_') break;
437 }
ae3d0f50 438 if(i==-1) { /* Either not found or name length is 0 */
62e4e7bf 439 /* This is a userspace tracefile */
440 strncpy(char_name, raw_name, raw_name_len);
441 char_name[raw_name_len] = '\0';
442 *name = g_quark_from_string(char_name);
443 *num = 0; /* unknown cpu */
444 for(i=0;i<raw_name_len;i++) {
445 if(raw_name[i] == '/') {
446 break;
447 }
448 }
449 i++;
450 for(;i<raw_name_len;i++) {
451 if(raw_name[i] == '/') {
452 break;
453 }
454 }
455 i++;
456 for(;i<raw_name_len;i++) {
457 if(raw_name[i] == '-') {
458 break;
459 }
460 }
461 if(i == raw_name_len) return -1;
462 i++;
463 tmpptr = &raw_name[i];
464 for(;i<raw_name_len;i++) {
465 if(raw_name[i] == '.') {
466 raw_name[i] = ' ';
467 break;
468 }
469 }
470 *tid = strtoul(tmpptr, &endptr, 10);
471 if(endptr == tmpptr)
472 return -1; /* No digit */
473 if(*tid == ULONG_MAX)
474 return -1; /* underflow / overflow */
475 i++;
476 tmpptr = &raw_name[i];
477 for(;i<raw_name_len;i++) {
478 if(raw_name[i] == '.') {
479 raw_name[i] = ' ';
480 break;
481 }
482 }
483 *pgid = strtoul(tmpptr, &endptr, 10);
484 if(endptr == tmpptr)
485 return -1; /* No digit */
486 if(*pgid == ULONG_MAX)
487 return -1; /* underflow / overflow */
488 i++;
489 tmpptr = &raw_name[i];
490 *creation = strtoull(tmpptr, &endptr, 10);
491 if(endptr == tmpptr)
492 return -1; /* No digit */
493 if(*creation == G_MAXUINT64)
494 return -1; /* underflow / overflow */
495 } else {
496 underscore_pos = i;
497
498 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
499
500 if(endptr == raw_name+underscore_pos+1)
501 return -1; /* No digit */
502 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
503 return -1; /* underflow / overflow */
504
4ad053df 505 if (!strncmp(raw_name, "flight-", sizeof("flight-") - 1)) {
506 raw_name += sizeof("flight-") - 1;
507 underscore_pos -= sizeof("flight-") - 1;
508 }
62e4e7bf 509 strncpy(char_name, raw_name, underscore_pos);
510 char_name[underscore_pos] = '\0';
62e4e7bf 511 *name = g_quark_from_string(char_name);
512 *num = cpu_num;
513 }
69bd59ed 514
b333a76b 515
3aee1200 516 return 0;
517}
963b5f2d 518
3aee1200 519
3865ea09 520GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
77175651 521{
3865ea09 522 return &trace->tracefiles;
77175651 523}
524
525
3865ea09 526void compute_tracefile_group(GQuark key_id,
527 GArray *group,
528 struct compute_tracefile_group_args *args)
77175651 529{
43ed82b5 530 unsigned int i;
77175651 531 LttTracefile *tf;
532
533 for(i=0; i<group->len; i++) {
534 tf = &g_array_index (group, LttTracefile, i);
535 if(tf->cpu_online)
3865ea09 536 args->func(tf, args->func_args);
77175651 537 }
538}
539
540
8655430b 541static void ltt_tracefile_group_destroy(gpointer data)
3aee1200 542{
543 GArray *group = (GArray *)data;
43ed82b5 544 unsigned int i;
3aee1200 545 LttTracefile *tf;
546
750eb11a 547 if (group->len > 0)
548 destroy_marker_data(g_array_index (group, LttTracefile, 0).mdata);
3aee1200 549 for(i=0; i<group->len; i++) {
550 tf = &g_array_index (group, LttTracefile, i);
551 if(tf->cpu_online)
552 ltt_tracefile_close(tf);
553 }
554 g_array_free(group, TRUE);
6cd62ccf 555}
556
43ed82b5 557static __attribute__ ((__unused__)) gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
49bf71b5 558{
3aee1200 559 GArray *group = (GArray *)data;
43ed82b5 560 unsigned int i;
3aee1200 561 LttTracefile *tf;
562
563 for(i=0; i<group->len; i++) {
564 tf = &g_array_index (group, LttTracefile, i);
3c165eaf 565 if(tf->cpu_online)
566 return 1;
3aee1200 567 }
568 return 0;
49bf71b5 569}
570
571
3aee1200 572/* Open each tracefile under a specific directory. Put them in a
573 * GData : permits to access them using their tracefile group pathname.
574 * i.e. access control/modules tracefile group by index :
575 * "control/module".
3865ea09 576 *
577 * relative path is the path relative to the trace root
578 * root path is the full path
3aee1200 579 *
4a55f63e 580 * A tracefile group is simply an array where all the per cpu tracefiles sit.
3aee1200 581 */
582
8655430b 583static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
f7afe191 584{
62e4e7bf 585 DIR *dir = opendir(root_path);
586 struct dirent *entry;
587 struct stat stat_buf;
750eb11a 588 int ret, i;
589 struct marker_data *mdata;
3865ea09 590
62e4e7bf 591 gchar path[PATH_MAX];
592 int path_len;
593 gchar *path_ptr;
3aee1200 594
3865ea09 595 int rel_path_len;
69bd59ed 596 gchar rel_path[PATH_MAX];
597 gchar *rel_path_ptr;
cb03932a 598 LttTracefile tmp_tf;
3865ea09 599
62e4e7bf 600 if(dir == NULL) {
601 perror(root_path);
602 return ENOENT;
603 }
3aee1200 604
62e4e7bf 605 strncpy(path, root_path, PATH_MAX-1);
606 path_len = strlen(path);
607 path[path_len] = '/';
608 path_len++;
609 path_ptr = path + path_len;
3aee1200 610
3865ea09 611 strncpy(rel_path, relative_path, PATH_MAX-1);
612 rel_path_len = strlen(rel_path);
613 rel_path[rel_path_len] = '/';
614 rel_path_len++;
615 rel_path_ptr = rel_path + rel_path_len;
616
62e4e7bf 617 while((entry = readdir(dir)) != NULL) {
618
619 if(entry->d_name[0] == '.') continue;
620
621 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
622 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
623
624 ret = stat(path, &stat_buf);
625 if(ret == -1) {
626 perror(path);
627 continue;
628 }
629
630 g_debug("Tracefile file or directory : %s\n", path);
631
fe452434 632 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
74a588bb 633
62e4e7bf 634 if(S_ISDIR(stat_buf.st_mode)) {
3aee1200 635
62e4e7bf 636 g_debug("Entering subdirectory...\n");
637 ret = open_tracefiles(trace, path, rel_path);
638 if(ret < 0) continue;
639 } else if(S_ISREG(stat_buf.st_mode)) {
640 GQuark name;
f64fedd7 641 guint num;
642 gulong tid, pgid;
62e4e7bf 643 guint64 creation;
3aee1200 644 GArray *group;
f64fedd7 645 num = 0;
646 tid = pgid = 0;
62e4e7bf 647 creation = 0;
ae3d0f50 648 if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
3aee1200 649 continue; /* invalid name */
3865ea09 650
62e4e7bf 651 g_debug("Opening file.\n");
cb03932a 652 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
653 g_info("Error opening tracefile %s", path);
654
655 continue; /* error opening the tracefile : bad magic number ? */
656 }
657
3865ea09 658 g_debug("Tracefile name is %s and number is %u",
659 g_quark_to_string(name), num);
750eb11a 660
661 mdata = NULL;
cb03932a 662 tmp_tf.cpu_online = 1;
663 tmp_tf.cpu_num = num;
d3d34f49 664 tmp_tf.name = name;
62e4e7bf 665 tmp_tf.tid = tid;
666 tmp_tf.pgid = pgid;
667 tmp_tf.creation = creation;
3865ea09 668 group = g_datalist_id_get_data(&trace->tracefiles, name);
3aee1200 669 if(group == NULL) {
670 /* Elements are automatically cleared when the array is allocated.
671 * It makes the cpu_online variable set to 0 : cpu offline, by default.
672 */
673 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
3865ea09 674 g_datalist_id_set_data_full(&trace->tracefiles, name,
3aee1200 675 group, ltt_tracefile_group_destroy);
750eb11a 676 mdata = allocate_marker_data();
677 if (!mdata)
678 g_error("Error in allocating marker data");
3aee1200 679 }
cb03932a 680
3aee1200 681 /* Add the per cpu tracefile to the named group */
682 unsigned int old_len = group->len;
683 if(num+1 > old_len)
684 group = g_array_set_size(group, num+1);
750eb11a 685
686 g_assert(group->len > 0);
687 if (!mdata)
688 mdata = g_array_index (group, LttTracefile, 0).mdata;
689
cb03932a 690 g_array_index (group, LttTracefile, num) = tmp_tf;
afd57a3c 691 g_array_index (group, LttTracefile, num).event.tracefile =
692 &g_array_index (group, LttTracefile, num);
750eb11a 693 for (i = 0; i < group->len; i++)
694 g_array_index (group, LttTracefile, i).mdata = mdata;
62e4e7bf 695 }
696 }
697
698 closedir(dir);
3aee1200 699
62e4e7bf 700 return 0;
f7afe191 701}
702
3aee1200 703
704/* Presumes the tracefile is already seeked at the beginning. It makes sense,
705 * because it must be done just after the opening */
2fc874ab 706static int ltt_process_metadata_tracefile(LttTracefile *tf)
3aee1200 707{
708 int err;
3aee1200 709
710 while(1) {
711 err = ltt_tracefile_read_seek(tf);
712 if(err == EPERM) goto seek_error;
713 else if(err == ERANGE) break; /* End of tracefile */
714
715 err = ltt_tracefile_read_update_event(tf);
716 if(err) goto update_error;
717
3aee1200 718 /* The rules are :
2fc874ab 719 * It contains only core events :
720 * 0 : set_marker_id
721 * 1 : set_marker_format
3aee1200 722 */
3c165eaf 723 if(tf->event.event_id >= MARKER_CORE_IDS) {
2fc874ab 724 /* Should only contain core events */
725 g_warning("Error in processing metadata file %s, "
3c165eaf 726 "should not contain event id %u.", g_quark_to_string(tf->name),
727 tf->event.event_id);
3aee1200 728 err = EPERM;
3c165eaf 729 goto event_id_error;
d2083cab 730 } else {
2e13d6af 731 char *pos;
750eb11a 732 const char *channel_name, *marker_name, *format;
3c165eaf 733 uint16_t id;
734 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
3aee1200 735
3c165eaf 736 switch((enum marker_id)tf->event.event_id) {
737 case MARKER_ID_SET_MARKER_ID:
750eb11a 738 channel_name = pos = tf->event.data;
739 pos += strlen(channel_name) + 1;
740 marker_name = pos;
741 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s.%s",
742 channel_name, marker_name);
2e13d6af 743 pos += strlen(marker_name) + 1;
2fc874ab 744 pos += ltt_align((size_t)pos, sizeof(guint16), tf->alignment);
3c165eaf 745 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
750eb11a 746 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s.%s id %hu",
747 channel_name, marker_name, id);
3c165eaf 748 pos += sizeof(guint16);
749 int_size = *(guint8*)pos;
750 pos += sizeof(guint8);
751 long_size = *(guint8*)pos;
752 pos += sizeof(guint8);
753 pointer_size = *(guint8*)pos;
754 pos += sizeof(guint8);
755 size_t_size = *(guint8*)pos;
756 pos += sizeof(guint8);
757 alignment = *(guint8*)pos;
758 pos += sizeof(guint8);
750eb11a 759 marker_id_event(tf->trace,
760 g_quark_from_string(channel_name),
761 g_quark_from_string(marker_name),
3c165eaf 762 id, int_size, long_size,
763 pointer_size, size_t_size, alignment);
3aee1200 764 break;
3c165eaf 765 case MARKER_ID_SET_MARKER_FORMAT:
750eb11a 766 channel_name = pos = tf->event.data;
767 pos += strlen(channel_name) + 1;
768 marker_name = pos;
769 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s.%s",
770 channel_name, marker_name);
2e13d6af 771 pos += strlen(marker_name) + 1;
2e13d6af 772 format = pos;
3c165eaf 773 pos += strlen(format) + 1;
750eb11a 774 marker_format_event(tf->trace,
775 g_quark_from_string(channel_name),
776 g_quark_from_string(marker_name),
3c165eaf 777 format);
2fc874ab 778 /* get information from dictionary TODO */
d1bb700c 779 break;
3aee1200 780 default:
2fc874ab 781 g_warning("Error in processing metadata file %s, "
3c165eaf 782 "unknown event id %hhu.",
3aee1200 783 g_quark_to_string(tf->name),
784 tf->event.event_id);
785 err = EPERM;
786 goto event_id_error;
787 }
788 }
963b5f2d 789 }
3aee1200 790 return 0;
963b5f2d 791
3aee1200 792 /* Error handling */
3aee1200 793event_id_error:
3aee1200 794update_error:
795seek_error:
2fc874ab 796 g_warning("An error occured in metadata tracefile parsing");
3aee1200 797 return err;
6cd62ccf 798}
799
e95fe8f7 800/*
801 * Open a trace and return its LttTrace handle.
802 *
803 * pathname must be the directory of the trace
804 */
963b5f2d 805
3aee1200 806LttTrace *ltt_trace_open(const gchar *pathname)
807{
808 gchar abs_path[PATH_MAX];
809 LttTrace * t;
810 LttTracefile *tf;
811 GArray *group;
43ed82b5 812 unsigned int i;
813 int ret;
64dd41a5 814 ltt_subbuffer_header_t *header;
62e4e7bf 815 DIR *dir;
816 struct dirent *entry;
62e4e7bf 817 struct stat stat_buf;
b56dcdf2 818 gchar path[PATH_MAX];
3aee1200 819
820 t = g_new(LttTrace, 1);
821 if(!t) goto alloc_error;
822
823 get_absolute_pathname(pathname, abs_path);
824 t->pathname = g_quark_from_string(abs_path);
825
3aee1200 826 g_datalist_init(&t->tracefiles);
b56dcdf2 827
828 /* Test to see if it looks like a trace */
62e4e7bf 829 dir = opendir(abs_path);
830 if(dir == NULL) {
831 perror(abs_path);
832 goto open_error;
833 }
834 while((entry = readdir(dir)) != NULL) {
b56dcdf2 835 strcpy(path, abs_path);
836 strcat(path, "/");
837 strcat(path, entry->d_name);
62e4e7bf 838 ret = stat(path, &stat_buf);
839 if(ret == -1) {
840 perror(path);
841 continue;
842 }
b56dcdf2 843 }
844 closedir(dir);
845
b56dcdf2 846 /* Open all the tracefiles */
22660d97 847 t->start_freq= 0;
e45551ac 848 if(open_tracefiles(t, abs_path, "")) {
849 g_warning("Error opening tracefile %s", abs_path);
b56dcdf2 850 goto find_error;
e45551ac 851 }
3aee1200 852
750eb11a 853 /* Parse each trace metadata_N files : get runtime fac. info */
2fc874ab 854 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_METADATA);
3aee1200 855 if(group == NULL) {
8af0138e 856 g_warning("Trace %s has no metadata tracefile", abs_path);
750eb11a 857 goto find_error;
3aee1200 858 }
859
e0c7c400 860 /*
750eb11a 861 * Get the trace information for the metadata_0 tracefile.
e0c7c400 862 * Getting a correct trace start_time and start_tsc is insured by the fact
863 * that no subbuffers are supposed to be lost in the metadata channel.
864 * Therefore, the first subbuffer contains the start_tsc timestamp in its
865 * buffer header.
866 */
16fcbb80 867 g_assert(group->len > 0);
868 tf = &g_array_index (group, LttTracefile, 0);
64dd41a5 869 header = (ltt_subbuffer_header_t *)tf->buffer.head;
ac3b1c7d 870 ret = parse_trace_header(header, tf, t);
871 g_assert(!ret);
b56dcdf2 872
873 t->num_cpu = group->len;
9c9bf2d4
BP
874 t->drift = 1.;
875 t->offset = 0.;
16fcbb80 876
750eb11a 877 //ret = allocate_marker_data(t);
878 //if (ret)
879 // g_error("Error in allocating marker data");
d79909d1 880
3aee1200 881 for(i=0; i<group->len; i++) {
882 tf = &g_array_index (group, LttTracefile, i);
e85b0b1b 883 if (tf->cpu_online)
2fc874ab 884 if(ltt_process_metadata_tracefile(tf))
750eb11a 885 goto find_error;
886 // goto metadata_error;
3aee1200 887 }
dd3a6d39 888
3aee1200 889 return t;
890
891 /* Error handling */
750eb11a 892//metadata_error:
893// destroy_marker_data(t);
b56dcdf2 894find_error:
3aee1200 895 g_datalist_clear(&t->tracefiles);
b56dcdf2 896open_error:
3aee1200 897 g_free(t);
898alloc_error:
899 return NULL;
900
901}
6cd62ccf 902
e95fe8f7 903/* Open another, completely independant, instance of a trace.
904 *
905 * A read on this new instance will read the first event of the trace.
906 *
3aee1200 907 * When we copy a trace, we want all the opening actions to happen again :
908 * the trace will be reopened and totally independant from the original.
909 * That's why we call ltt_trace_open.
e95fe8f7 910 */
3aee1200 911LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 912{
3aee1200 913 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 914}
915
e95fe8f7 916/*
917 * Close a trace
918 */
919
3aee1200 920void ltt_trace_close(LttTrace *t)
6cd62ccf 921{
3aee1200 922 g_datalist_clear(&t->tracefiles);
923 g_free(t);
6cd62ccf 924}
925
3aee1200 926
6cd62ccf 927/*****************************************************************************
3aee1200 928 * Get the start time and end time of the trace
6cd62ccf 929 ****************************************************************************/
930
3c165eaf 931void ltt_tracefile_time_span_get(LttTracefile *tf,
3aee1200 932 LttTime *start, LttTime *end)
6cd62ccf 933{
3aee1200 934 int err;
6cd62ccf 935
3aee1200 936 err = map_block(tf, 0);
937 if(unlikely(err)) {
938 g_error("Can not map block");
939 *start = ltt_time_infinite;
940 } else
941 *start = tf->buffer.begin.timestamp;
942
943 err = map_block(tf, tf->num_blocks - 1); /* Last block */
944 if(unlikely(err)) {
945 g_error("Can not map block");
946 *end = ltt_time_zero;
947 } else
948 *end = tf->buffer.end.timestamp;
3729b611
PMF
949
950 g_assert(end->tv_sec <= G_MAXUINT);
6cd62ccf 951}
952
3aee1200 953struct tracefile_time_span_get_args {
954 LttTrace *t;
955 LttTime *start;
956 LttTime *end;
957};
963b5f2d 958
8655430b 959static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 960{
3aee1200 961 struct tracefile_time_span_get_args *args =
962 (struct tracefile_time_span_get_args*)user_data;
963
964 GArray *group = (GArray *)data;
43ed82b5 965 unsigned int i;
3aee1200 966 LttTracefile *tf;
967 LttTime tmp_start;
968 LttTime tmp_end;
969
970 for(i=0; i<group->len; i++) {
971 tf = &g_array_index (group, LttTracefile, i);
972 if(tf->cpu_online) {
973 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
974 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
975 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
976 }
977 }
6cd62ccf 978}
979
8655430b 980/* return the start and end time of a trace */
981
487ad181 982void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
983{
3aee1200 984 LttTime min_start = ltt_time_infinite;
985 LttTime max_end = ltt_time_zero;
986 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 987
3aee1200 988 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
989
990 if(start != NULL) *start = min_start;
991 if(end != NULL) *end = max_end;
992
487ad181 993}
994
995
3aee1200 996/* Seek to the first event in a tracefile that has a time equal or greater than
997 * the time passed in parameter.
998 *
999 * If the time parameter is outside the tracefile time span, seek to the first
27304273 1000 * event or if after, return ERANGE.
3aee1200 1001 *
1002 * If the time parameter is before the first event, we have to seek specially to
1003 * there.
1004 *
27304273 1005 * If the time is after the end of the trace, return ERANGE.
3aee1200 1006 *
1007 * Do a binary search to find the right block, then a sequential search in the
1008 * block to find the event.
1009 *
1010 * In the special case where the time requested fits inside a block that has no
1011 * event corresponding to the requested time, the first event of the next block
1012 * will be seeked.
1013 *
1014 * IMPORTANT NOTE : // FIXME everywhere...
1015 *
1016 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1017 * you will jump over an event if you do.
1018 *
1019 * Return value : 0 : no error, the tf->event can be used
27304273 1020 * ERANGE : time if after the last event of the trace
3aee1200 1021 * otherwise : this is an error.
1022 *
1023 * */
1024
1025int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1026{
1027 int ret = 0;
1028 int err;
1029 unsigned int block_num, high, low;
1030
1031 /* seek at the beginning of trace */
1032 err = map_block(tf, 0); /* First block */
1033 if(unlikely(err)) {
1034 g_error("Can not map block");
1035 goto fail;
caf7a67a 1036 }
1037
3aee1200 1038 /* If the time is lower or equal the beginning of the trace,
1039 * go to the first event. */
1040 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1041 ret = ltt_tracefile_read(tf);
27304273 1042 if(ret == ERANGE) goto range;
1043 else if (ret) goto fail;
3aee1200 1044 goto found; /* There is either no event in the trace or the event points
1045 to the first event in the trace */
1046 }
caf7a67a 1047
3aee1200 1048 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1049 if(unlikely(err)) {
1050 g_error("Can not map block");
1051 goto fail;
caf7a67a 1052 }
6cd62ccf 1053
27304273 1054 /* If the time is after the end of the trace, return ERANGE. */
1055 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1056 goto range;
3aee1200 1057 }
62e55dd6 1058
3aee1200 1059 /* Binary search the block */
1060 high = tf->num_blocks - 1;
1061 low = 0;
1062
1063 while(1) {
1064 block_num = ((high-low) / 2) + low;
1065
1066 err = map_block(tf, block_num);
1067 if(unlikely(err)) {
1068 g_error("Can not map block");
1069 goto fail;
db55eaae 1070 }
3aee1200 1071 if(high == low) {
1072 /* We cannot divide anymore : this is what would happen if the time
1073 * requested was exactly between two consecutive buffers'end and start
1074 * timestamps. This is also what would happend if we didn't deal with out
1075 * of span cases prior in this function. */
1076 /* The event is right in the buffer!
1077 * (or in the next buffer first event) */
1078 while(1) {
1079 ret = ltt_tracefile_read(tf);
27304273 1080 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1081 else if(ret) goto fail;
1082
d9e13a0f 1083 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 1084 goto found;
3aee1200 1085 }
1086
27304273 1087 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 1088 /* go to lower part */
b1369bef 1089 high = block_num - 1;
3aee1200 1090 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1091 /* go to higher part */
b1369bef 1092 low = block_num + 1;
3aee1200 1093 } else {/* The event is right in the buffer!
1094 (or in the next buffer first event) */
1095 while(1) {
27304273 1096 ret = ltt_tracefile_read(tf);
1097 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1098 else if(ret) goto fail;
1099
d9e13a0f 1100 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1101 break;
6cd62ccf 1102 }
3aee1200 1103 goto found;
6cd62ccf 1104 }
6cd62ccf 1105 }
3aee1200 1106
1107found:
1108 return 0;
27304273 1109range:
1110 return ERANGE;
3aee1200 1111
1112 /* Error handling */
1113fail:
1114 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1115 g_quark_to_string(tf->name));
1116 return EPERM;
6cd62ccf 1117}
1118
e95fe8f7 1119/* Seek to a position indicated by an LttEventPosition
1120 */
80da81ad 1121
8655430b 1122int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep)
1123{
3aee1200 1124 int err;
1125
1126 if(ep->tracefile != tf) {
1127 goto fail;
80da81ad 1128 }
1129
3aee1200 1130 err = map_block(tf, ep->block);
1131 if(unlikely(err)) {
1132 g_error("Can not map block");
1133 goto fail;
1134 }
1135
1136 tf->event.offset = ep->offset;
18206708 1137
62e4e7bf 1138 /* Put back the event real tsc */
1139 tf->event.tsc = ep->tsc;
1140 tf->buffer.tsc = ep->tsc;
78f79181 1141
3aee1200 1142 err = ltt_tracefile_read_update_event(tf);
1143 if(err) goto fail;
73faf25a 1144
1145 /* deactivate this, as it does nothing for now
3aee1200 1146 err = ltt_tracefile_read_op(tf);
1147 if(err) goto fail;
73faf25a 1148 */
80da81ad 1149
a0c1f622 1150 return 0;
3aee1200 1151
1152fail:
1153 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1154 g_quark_to_string(tf->name));
a0c1f622 1155 return 1;
3aee1200 1156}
1157
9c9bf2d4
BP
1158/*
1159 * Convert a value in "TSC scale" to a value in nanoseconds
1160 */
1161guint64 tsc_to_uint64(guint32 freq_scale, uint64_t start_freq, guint64 tsc)
1162{
1163 return (double) tsc * NANOSECONDS_PER_SECOND * freq_scale / start_freq;
1164}
1165
e95fe8f7 1166/* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1167 * corresponds to.
1168 */
ae3d0f50 1169LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
3aee1200 1170{
9c9bf2d4
BP
1171 return ltt_time_from_uint64(tsc_to_uint64(tf->trace->freq_scale,
1172 tf->trace->start_freq, tf->trace->drift * tsc +
1173 tf->trace->offset));
80da81ad 1174}
1175
ae3d0f50 1176/* Calculate the real event time based on the buffer boundaries */
1177LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1178{
62e4e7bf 1179 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
ae3d0f50 1180}
1181
eed2ef37 1182
1183/* Get the current event of the tracefile : valid until the next read */
1184LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1185{
1186 return &tf->event;
1187}
1188
1189
1190
6cd62ccf 1191/*****************************************************************************
1192 *Function name
3aee1200 1193 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1194 *Input params
1195 * t : tracefile
1196 *Return value
3aee1200 1197 *
1198 * Returns 0 if an event can be used in tf->event.
d822520b 1199 * Returns ERANGE on end of trace. The event in tf->event still can be used
1200 * (if the last block was not empty).
3aee1200 1201 * Returns EPERM on error.
1202 *
1203 * This function does make the tracefile event structure point to the event
1204 * currently pointed to by the tf->event.
1205 *
1206 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1207 * reinitialize it after an error if you want results to be coherent.
1208 * It would be the case if a end of trace last buffer has no event : the end
1209 * of trace wouldn't be returned, but an error.
1210 * We make the assumption there is at least one event per buffer.
6cd62ccf 1211 ****************************************************************************/
1212
3aee1200 1213int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1214{
963b5f2d 1215 int err;
6cd62ccf 1216
3aee1200 1217 err = ltt_tracefile_read_seek(tf);
1218 if(err) return err;
1219 err = ltt_tracefile_read_update_event(tf);
1220 if(err) return err;
73faf25a 1221
1222 /* deactivate this, as it does nothing for now
3aee1200 1223 err = ltt_tracefile_read_op(tf);
1224 if(err) return err;
73faf25a 1225 */
3aee1200 1226
1227 return 0;
1228}
1229
1230int ltt_tracefile_read_seek(LttTracefile *tf)
1231{
1232 int err;
1233
1234 /* Get next buffer until we finally have an event, or end of trace */
1235 while(1) {
1236 err = ltt_seek_next_event(tf);
1237 if(unlikely(err == ENOPROTOOPT)) {
1238 return EPERM;
bdc36259 1239 }
bdc36259 1240
3aee1200 1241 /* Are we at the end of the buffer ? */
1242 if(err == ERANGE) {
1243 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1244 return ERANGE;
1245 } else {
1246 /* get next block */
1247 err = map_block(tf, tf->buffer.index + 1);
1248 if(unlikely(err)) {
1249 g_error("Can not map block");
1250 return EPERM;
1251 }
1252 }
1253 } else break; /* We found an event ! */
1254 }
2dee981d 1255
3aee1200 1256 return 0;
1257}
18206708 1258
e95fe8f7 1259/* do an operation when reading a new event */
18206708 1260
73faf25a 1261/* This function does nothing for now */
1262#if 0
3aee1200 1263int ltt_tracefile_read_op(LttTracefile *tf)
1264{
3aee1200 1265 LttEvent *event;
1266
1267 event = &tf->event;
18206708 1268
73faf25a 1269 /* do event specific operation */
1270
1271 /* nothing */
40331ba8 1272
3aee1200 1273 return 0;
6cd62ccf 1274}
73faf25a 1275#endif
6cd62ccf 1276
91f8d488 1277static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1278{
1279 unsigned int offset = 0;
1280 int i, j;
1281
43ed82b5 1282 g_printf("Event header (tracefile %s offset %" PRIx64 "):\n",
afd57a3c 1283 g_quark_to_string(ev->tracefile->long_name),
f0241068
MD
1284 (uint64_t)ev->tracefile->buffer.offset +
1285 (long)start_pos - (long)ev->tracefile->buffer.head);
91f8d488 1286
1287 while (offset < (long)end_pos - (long)start_pos) {
1288 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1289 g_printf(" ");
1290
1291 for (i = 0; i < 4 ; i++) {
1292 for (j = 0; j < 4; j++) {
1293 if (offset + ((i * 4) + j) <
1294 (long)end_pos - (long)start_pos)
1295 g_printf("%02hhX",
d3353231 1296 ((char*)start_pos)[offset + ((i * 4) + j)]);
91f8d488 1297 else
1298 g_printf(" ");
1299 g_printf(" ");
1300 }
1301 if (i < 4)
1302 g_printf(" ");
1303 }
1304 offset+=16;
1305 g_printf("\n");
1306 }
1307}
1308
6cd62ccf 1309
3aee1200 1310/* same as ltt_tracefile_read, but does not seek to the next event nor call
1311 * event specific operation. */
1312int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1313{
3aee1200 1314 void * pos;
1315 LttEvent *event;
91f8d488 1316 void *pos_aligned;
551bf485 1317 guint16 packed_evid; /* event id reader from the 5 bits in header */
3aee1200 1318
1319 event = &tf->event;
eed2ef37 1320 pos = tf->buffer.head + event->offset;
3aee1200 1321
1322 /* Read event header */
1323
62e4e7bf 1324 /* Align the head */
2fc874ab 1325 pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment);
91f8d488 1326 pos_aligned = pos;
3aee1200 1327
2fc874ab 1328 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos);
551bf485 1329 event->event_id = packed_evid = event->timestamp >> tf->tscbits;
a9048165 1330 event->timestamp = event->timestamp & tf->tsc_mask;
2fc874ab 1331 pos += sizeof(guint32);
1332
551bf485 1333 switch (packed_evid) {
2fc874ab 1334 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1335 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1336 pos += sizeof(guint16);
1337 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1338 pos += sizeof(guint16);
1339 if (event->event_size == 0xFFFF) {
1340 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1341 pos += sizeof(guint32);
d1bb700c 1342 }
2fc874ab 1343 pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment);
1344 tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
62e4e7bf 1345 pos += sizeof(guint64);
2fc874ab 1346 break;
1347 case 30: /* LTT_RFLAG_ID_SIZE */
f439de06 1348 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
3c165eaf 1349 pos += sizeof(guint16);
d1bb700c 1350 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1351 pos += sizeof(guint16);
2fc874ab 1352 if (event->event_size == 0xFFFF) {
1353 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1354 pos += sizeof(guint32);
1355 }
1356 break;
1357 case 31: /* LTT_RFLAG_ID */
1358 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1359 pos += sizeof(guint16);
1360 event->event_size = G_MAXUINT;
1361 break;
1362 default:
1363 event->event_size = G_MAXUINT;
1364 break;
1365 }
1366
551bf485 1367 if (likely(packed_evid != 29)) {
2fc874ab 1368 /* No extended timestamp */
1369 if (event->timestamp < (tf->buffer.tsc & tf->tsc_mask))
1370 tf->buffer.tsc = ((tf->buffer.tsc & ~tf->tsc_mask) /* overflow */
1371 + tf->tsc_mask_next_bit)
1372 | (guint64)event->timestamp;
1373 else
1374 tf->buffer.tsc = (tf->buffer.tsc & ~tf->tsc_mask) /* no overflow */
1375 | (guint64)event->timestamp;
d1bb700c 1376 }
2fc874ab 1377 event->tsc = tf->buffer.tsc;
1378
1379 event->event_time = ltt_interpolate_time(tf, event);
91f8d488 1380
1381 if (a_event_debug)
1382 print_debug_event_header(event, pos_aligned, pos);
1383
3aee1200 1384 event->data = pos;
1385
2fc874ab 1386 /*
1387 * Let ltt_update_event_size update event->data according to the largest
1388 * alignment within the payload.
1389 * Get the data size and update the event fields with the current
1390 * information. */
eed2ef37 1391 ltt_update_event_size(tf);
77175651 1392
3aee1200 1393 return 0;
6cd62ccf 1394}
1395
507915ee 1396
6cd62ccf 1397/****************************************************************************
1398 *Function name
3aee1200 1399 * map_block : map a block from the file
6cd62ccf 1400 *Input Params
1401 * lttdes : ltt trace file
1402 * whichBlock : the block which will be read
1403 *return value
1404 * 0 : success
1405 * EINVAL : lseek fail
1406 * EIO : can not read from the file
1407 ****************************************************************************/
1408
8655430b 1409static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1410{
b77d1b57 1411 int page_size = getpagesize();
64dd41a5 1412 ltt_subbuffer_header_t *header;
f0241068
MD
1413 uint64_t offset;
1414 uint32_t size;
1415 int ret;
3aee1200 1416
1417 g_assert(block_num < tf->num_blocks);
6cd62ccf 1418
f628823c 1419 if(tf->buffer.head != NULL) {
f0241068 1420 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buffer.size))) {
f628823c 1421 g_warning("unmap size : %u\n",
f0241068 1422 PAGE_ALIGN(tf->buffer.size));
f628823c 1423 perror("munmap error");
1424 g_assert(0);
1425 }
1426 }
f0241068
MD
1427
1428 ret = get_block_offset_size(tf, block_num, &offset, &size);
1429 g_assert(!ret);
1430
93556665
MD
1431 g_debug("Map block %u, offset %llu, size %u\n", block_num,
1432 (unsigned long long)offset, (unsigned int)size);
1433
3aee1200 1434 /* Multiple of pages aligned head */
f0241068
MD
1435 tf->buffer.head = mmap(0, (size_t)size, PROT_READ, MAP_PRIVATE,
1436 tf->fd, (off_t)offset);
3aee1200 1437
3865ea09 1438 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1439 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1440 g_assert(0);
3aee1200 1441 goto map_error;
6cd62ccf 1442 }
f64fedd7 1443 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
6cd62ccf 1444
3aee1200 1445 tf->buffer.index = block_num;
1446
64dd41a5 1447 header = (ltt_subbuffer_header_t *)tf->buffer.head;
3aee1200 1448
3aee1200 1449 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1450 &header->cycle_count_begin);
3aee1200 1451 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1452 &header->cycle_count_end);
f0241068
MD
1453 tf->buffer.offset = offset;
1454 tf->buffer.size = ltt_get_uint32(LTT_GET_BO(tf),
1455 &header->sb_size);
f0241068
MD
1456 tf->buffer.data_size = ltt_get_uint32(LTT_GET_BO(tf),
1457 &header->data_size);
3aee1200 1458 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1459 tf->event.tsc = tf->buffer.tsc;
986e2a7c 1460 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 1461
93556665 1462 g_assert(size == tf->buffer.size);
f0241068
MD
1463 g_assert(tf->buffer.data_size <= tf->buffer.size);
1464
22660d97
BP
1465 if (tf->trace->start_freq)
1466 {
1467 tf->buffer.begin.freq = tf->trace->start_freq;
1468 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
1469 tf->buffer.begin.cycle_count);
1470 tf->buffer.end.freq = tf->trace->start_freq;
1471 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
1472 tf->buffer.end.cycle_count);
1473 }
1474
3aee1200 1475 /* Make the current event point to the beginning of the buffer :
1476 * it means that the event read must get the first event. */
1477 tf->event.tracefile = tf;
1478 tf->event.block = block_num;
eed2ef37 1479 tf->event.offset = 0;
3aee1200 1480
a2bbf2e5 1481 if (header->events_lost) {
1482 g_warning("%d events lost so far in tracefile %s at block %u",
0c2f4984 1483 (guint)header->events_lost,
a2bbf2e5 1484 g_quark_to_string(tf->long_name),
1485 block_num);
426f6149 1486 tf->events_lost = header->events_lost;
1487 }
a2bbf2e5 1488 if (header->subbuf_corrupt) {
1489 g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u",
0c2f4984 1490 (guint)header->subbuf_corrupt,
a2bbf2e5 1491 g_quark_to_string(tf->long_name),
1492 block_num);
426f6149 1493 tf->subbuf_corrupt = header->subbuf_corrupt;
1494 }
1495
3aee1200 1496 return 0;
6cd62ccf 1497
3aee1200 1498map_error:
1499 return -errno;
6cd62ccf 1500}
1501
91f8d488 1502static void print_debug_event_data(LttEvent *ev)
1503{
1504 unsigned int offset = 0;
1505 int i, j;
1506
1507 if (!max(ev->event_size, ev->data_size))
1508 return;
1509
43ed82b5 1510 g_printf("Event data (tracefile %s offset %" PRIx64 "):\n",
1511 g_quark_to_string(ev->tracefile->long_name),
f0241068 1512 (uint64_t)ev->tracefile->buffer.offset
43ed82b5 1513 + (long)ev->data - (long)ev->tracefile->buffer.head);
91f8d488 1514
1515 while (offset < max(ev->event_size, ev->data_size)) {
1516 g_printf("%8lx", (long)ev->data + offset
1517 - (long)ev->tracefile->buffer.head);
1518 g_printf(" ");
1519
1520 for (i = 0; i < 4 ; i++) {
1521 for (j = 0; j < 4; j++) {
1522 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
1523 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
1524 else
1525 g_printf(" ");
1526 g_printf(" ");
1527 }
1528 if (i < 4)
1529 g_printf(" ");
1530 }
1531
1532 g_printf(" ");
1533
1534 for (i = 0; i < 4; i++) {
1535 for (j = 0; j < 4; j++) {
1536 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
1537 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
1538 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
1539 else
1540 g_printf(".");
1541 } else
1542 g_printf(" ");
1543 }
1544 }
1545 offset+=16;
1546 g_printf("\n");
1547 }
1548}
1549
77175651 1550/* It will update the fields offsets too */
1551void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1552{
2312de30 1553 off_t size = 0;
d2007fbd 1554 struct marker_info *info;
750eb11a 1555
1556 if (tf->name == LTT_TRACEFILE_NAME_METADATA) {
1557 switch((enum marker_id)tf->event.event_id) {
1558 case MARKER_ID_SET_MARKER_ID:
1559 size = strlen((char*)tf->event.data) + 1;
1560 g_debug("marker %s id set", (char*)tf->event.data + size);
1561 size += strlen((char*)tf->event.data + size) + 1;
1562 size += ltt_align(size, sizeof(guint16), tf->alignment);
1563 size += sizeof(guint16);
1564 size += sizeof(guint8);
1565 size += sizeof(guint8);
1566 size += sizeof(guint8);
1567 size += sizeof(guint8);
1568 size += sizeof(guint8);
1569 break;
1570 case MARKER_ID_SET_MARKER_FORMAT:
1571 size = strlen((char*)tf->event.data) + 1;
1572 g_debug("marker %s format set", (char*)tf->event.data);
1573 size += strlen((char*)tf->event.data + size) + 1;
1574 size += strlen((char*)tf->event.data + size) + 1;
1575 break;
1576 }
256a5b3a 1577 }
1578
750eb11a 1579 info = marker_get_info_from_id(tf->mdata, tf->event.event_id);
dcf96842 1580
256a5b3a 1581 if (tf->event.event_id >= MARKER_CORE_IDS)
1582 g_assert(info != NULL);
1583
1584 /* Do not update field offsets of core markers when initially reading the
2fc874ab 1585 * metadata tracefile when the infos about these markers do not exist yet.
256a5b3a 1586 */
1587 if (likely(info && info->fields)) {
2fc874ab 1588 /* alignment */
196085f2 1589 tf->event.data += ltt_align((off_t)(unsigned long)tf->event.data,
1590 info->largest_align,
2fc874ab 1591 info->alignment);
1592 /* size, dynamically computed */
256a5b3a 1593 if (info->size != -1)
1594 size = info->size;
1595 else
750eb11a 1596 size = marker_update_fields_offsets(marker_get_info_from_id(tf->mdata,
256a5b3a 1597 tf->event.event_id), tf->event.data);
44f317b7 1598 }
c4afd5d8 1599
d2007fbd 1600 tf->event.data_size = size;
1601
1602 /* Check consistency between kernel and LTTV structure sizes */
2fc874ab 1603 if(tf->event.event_size == G_MAXUINT) {
d2007fbd 1604 /* Event size too big to fit in the event size field */
1605 tf->event.event_size = tf->event.data_size;
1606 }
91f8d488 1607
1608 if (a_event_debug)
1609 print_debug_event_data(&tf->event);
1610
d2007fbd 1611 if (tf->event.data_size != tf->event.event_size) {
750eb11a 1612 struct marker_info *info = marker_get_info_from_id(tf->mdata,
3c165eaf 1613 tf->event.event_id);
750eb11a 1614 if (!info)
1615 g_error("Undescribed event %hhu in channel %s", tf->event.event_id,
1616 g_quark_to_string(tf->name));
3c165eaf 1617 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1618 g_quark_to_string(info->name),
1619 tf->event.event_size, tf->event.data_size);
d2007fbd 1620 exit(-1);
1621 }
6cd62ccf 1622}
1623
6cd62ccf 1624
2fc874ab 1625/* Take the tf current event offset and use the event id to figure out where is
1626 * the next event offset.
3aee1200 1627 *
1628 * This is an internal function not aiming at being used elsewhere : it will
1629 * not jump over the current block limits. Please consider using
1630 * ltt_tracefile_read to do this.
1631 *
1632 * Returns 0 on success
1633 * ERANGE if we are at the end of the buffer.
1634 * ENOPROTOOPT if an error occured when getting the current event size.
1635 */
8655430b 1636static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1637{
3aee1200 1638 int ret = 0;
1639 void *pos;
3aee1200 1640
1641 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1642 if(tf->event.offset == 0) {
51551c6f 1643 tf->event.offset += tf->buffer_header_size;
b77d1b57 1644
f0241068 1645 if(tf->event.offset == tf->buffer.data_size) {
b77d1b57 1646 ret = ERANGE;
1647 }
3aee1200 1648 goto found;
1649 }
2fc874ab 1650
3aee1200 1651 pos = tf->event.data;
1652
77175651 1653 if(tf->event.data_size < 0) goto error;
3aee1200 1654
77175651 1655 pos += (size_t)tf->event.data_size;
3aee1200 1656
eed2ef37 1657 tf->event.offset = pos - tf->buffer.head;
cb03932a 1658
f0241068 1659 if(tf->event.offset == tf->buffer.data_size) {
cb03932a 1660 ret = ERANGE;
1661 goto found;
1662 }
f0241068 1663 g_assert(tf->event.offset < tf->buffer.data_size);
3aee1200 1664
1665found:
1666 return ret;
1667
1668error:
1669 g_error("Error in ltt_seek_next_event for tracefile %s",
1670 g_quark_to_string(tf->name));
1671 return ENOPROTOOPT;
6cd62ccf 1672}
1673
f104d082 1674
6cd62ccf 1675/*****************************************************************************
1676 *Function name
eed2ef37 1677 * ltt_get_int : get an integer number
6cd62ccf 1678 *Input params
3aee1200 1679 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 1680 * size : the size of the integer
3aee1200 1681 * ptr : the data pointer
6cd62ccf 1682 *Return value
cf74a6f1 1683 * gint64 : a 64 bits integer
6cd62ccf 1684 ****************************************************************************/
1685
eed2ef37 1686gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 1687{
3aee1200 1688 gint64 val;
cf74a6f1 1689
1690 switch(size) {
3aee1200 1691 case 1: val = *((gint8*)data); break;
1692 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
1693 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
1694 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
1695 default: val = ltt_get_int64(reverse_byte_order, data);
1696 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 1697 break;
1698 }
1699
3aee1200 1700 return val;
6cd62ccf 1701}
3aee1200 1702
6cd62ccf 1703/*****************************************************************************
1704 *Function name
eed2ef37 1705 * ltt_get_uint : get an unsigned integer number
6cd62ccf 1706 *Input params
3aee1200 1707 * reverse_byte_order: must we reverse the byte order ?
1708 * size : the size of the integer
1709 * ptr : the data pointer
1710 *Return value
1711 * guint64 : a 64 bits unsigned integer
6cd62ccf 1712 ****************************************************************************/
1713
eed2ef37 1714guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 1715{
3aee1200 1716 guint64 val;
1717
1718 switch(size) {
1719 case 1: val = *((gint8*)data); break;
1720 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
1721 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
1722 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
1723 default: val = ltt_get_uint64(reverse_byte_order, data);
1724 g_critical("get_uint : unsigned integer size %d unknown",
1725 size);
1726 break;
1727 }
1728
1729 return val;
6cd62ccf 1730}
3aee1200 1731
1732
a5dcde2f 1733/* get the node name of the system */
1734
1735char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1736{
1737 return s->node_name;
1738}
1739
1740
1741/* get the domain name of the system */
1742
1743char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1744{
1745 return s->domain_name;
1746}
1747
1748
1749/* get the description of the system */
1750
1751char * ltt_trace_system_description_description (LttSystemDescription * s)
1752{
1753 return s->description;
1754}
1755
1756
bf33dd50 1757/* get the NTP corrected start time of the trace */
7bd563ec 1758LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 1759{
7bd563ec 1760 return t->start_time;
a5dcde2f 1761}
1762
bf33dd50 1763/* get the monotonic start time of the trace */
1764LttTime ltt_trace_start_time_monotonic(LttTrace *t)
1765{
1766 return t->start_time_from_tsc;
1767}
1768
43ed82b5 1769static __attribute__ ((__unused__)) LttTracefile *ltt_tracefile_new()
18206708 1770{
afd57a3c 1771 LttTracefile *tf;
1772 tf = g_new(LttTracefile, 1);
1773 tf->event.tracefile = tf;
1774 return tf;
18206708 1775}
1776
43ed82b5 1777static __attribute__ ((__unused__)) void ltt_tracefile_destroy(LttTracefile *tf)
18206708 1778{
1779 g_free(tf);
1780}
1781
43ed82b5 1782static __attribute__ ((__unused__)) void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
18206708 1783{
1784 *dest = *src;
1785}
1786
3aee1200 1787/* Before library loading... */
1788
8655430b 1789static __attribute__((constructor)) void init(void)
3aee1200 1790{
750eb11a 1791 LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("metadata");
3aee1200 1792}
27cc2365
WB
1793
1794/*****************************************************************************
1795 *Function name
1796 * ltt_tracefile_open_header : based on ltt_tracefile_open but it stops
1797 * when it gets the header
1798 *Input params
1799 * fileName : path to the tracefile
1800 * tf : the tracefile (metadata_0) where the header will be read
1801 *Return value
1802 * ltt_subbuffer_header_t : the header containing the version number
1803 ****************************************************************************/
8b73a028 1804static ltt_subbuffer_header_t * ltt_tracefile_open_header(gchar *fileName, LttTracefile *tf)
27cc2365 1805{
8b73a028
WB
1806 struct stat lTDFStat; /* Trace data file status */
1807 ltt_subbuffer_header_t *header;
1808 int page_size = getpagesize();
1809
1810 /* open the file */
1811 tf->long_name = g_quark_from_string(fileName);
1812 tf->fd = open(fileName, O_RDONLY);
1813 if(tf->fd < 0){
1814 g_warning("Unable to open input data file %s\n", fileName);
1815 goto end;
1816 }
1817
1818 /* Get the file's status */
1819 if(fstat(tf->fd, &lTDFStat) < 0){
1820 g_warning("Unable to get the status of the input data file %s\n", fileName);
1821 goto close_file;
1822 }
1823
1824 /* Is the file large enough to contain a trace */
1825 if(lTDFStat.st_size < (off_t)(ltt_subbuffer_header_size())) {
1826 g_print("The input data file %s does not contain a trace\n", fileName);
1827 goto close_file;
1828 }
1829
1830 /* Temporarily map the buffer start header to get trace information */
1831 /* Multiple of pages aligned head */
1832 tf->buffer.head = mmap(0,PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ, MAP_PRIVATE, tf->fd, 0);
1833
1834 if(tf->buffer.head == MAP_FAILED) {
1835 perror("Error in allocating memory for buffer of tracefile");
1836 goto close_file;
1837 }
1838 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
1839
1840 header = (ltt_subbuffer_header_t *)tf->buffer.head;
1841
1842 return header;
1843
1844 close_file:
1845 close(tf->fd);
1846 end:
1847 return 0;
27cc2365
WB
1848}
1849
1850
1851/*****************************************************************************
1852 *Function name
1853 * get_version : get the trace version from a metadata_0 trace file
1854 *Input params
1855 * pathname : path to the trace
1856 * version_number : the struct that will get the version number
1857 *Return value
1858 * int : 1 if succeed, -1 if error
1859 ****************************************************************************/
8b73a028 1860int ltt_get_trace_version(const gchar *pathname, struct LttTraceVersion *version_number)
27cc2365
WB
1861{
1862 gchar abs_path[PATH_MAX];
1863 int ret = 0;
1864 DIR *dir;
1865 struct dirent *entry;
1866 struct stat stat_buf;
1867 gchar path[PATH_MAX];
1868
1869 LttTracefile tmp_tf;
1870 LttTrace * t;
1871 ltt_subbuffer_header_t *header;
1872
1873 t = g_new(LttTrace, 1);
1874
1875 get_absolute_pathname(pathname, abs_path);
8b73a028 1876
27cc2365
WB
1877 /* Test to see if it looks like a trace */
1878 dir = opendir(abs_path);
1879
1880 if(dir == NULL) {
1881 perror(abs_path);
1882 goto open_error;
1883 }
1884
1885 while((entry = readdir(dir)) != NULL) {
1886 strcpy(path, abs_path);
1887 strcat(path, "/");
1888 strcat(path, entry->d_name);
1889 ret = stat(path, &stat_buf);
1890 if(ret == -1) {
1891 perror(path);
1892 continue;
1893 }
1894 }
1895
1896 closedir(dir);
1897 dir = opendir(abs_path);
8b73a028 1898
27cc2365 1899 while((entry = readdir(dir)) != NULL) {
27cc2365
WB
1900 if(entry->d_name[0] == '.') continue;
1901 if(g_strcmp0(entry->d_name, "metadata_0") != 0) continue;
1902
1903 strcpy(path, abs_path);
1904 strcat(path, "/");
1905 strcat(path, entry->d_name);
1906 if(ret == -1) {
1907 perror(path);
1908 continue;
1909 }
1910
1911 header = ltt_tracefile_open_header(path, &tmp_tf);
1912
1913 if(header == NULL) {
1914 g_info("Error getting the header %s", path);
27cc2365
WB
1915 continue; /* error opening the tracefile : bad magic number ? */
1916 }
8b73a028 1917
27cc2365
WB
1918 version_number->ltt_major_version = header->major_version;
1919 version_number->ltt_minor_version = header->minor_version;
27cc2365
WB
1920 }
1921
1922 return 0;
1923
1924 open_error:
8b73a028
WB
1925 g_free(t);
1926 return -1;
1927}
This page took 0.219921 seconds and 4 git commands to generate.