Fix dynamic update of marker data across same channel tracefiles
[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) {
621ef26f 151 case 6:
b7576a11 152 {
621ef26f 153 struct ltt_subbuffer_header_2_6 *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
547 for(i=0; i<group->len; i++) {
548 tf = &g_array_index (group, LttTracefile, i);
f972ab5e
MD
549 if(tf->cpu_online) {
550 destroy_marker_data(tf->mdata);
3aee1200 551 ltt_tracefile_close(tf);
f972ab5e 552 }
3aee1200 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;
f972ab5e 588 int ret;
3865ea09 589
62e4e7bf 590 gchar path[PATH_MAX];
591 int path_len;
592 gchar *path_ptr;
3aee1200 593
3865ea09 594 int rel_path_len;
69bd59ed 595 gchar rel_path[PATH_MAX];
596 gchar *rel_path_ptr;
cb03932a 597 LttTracefile tmp_tf;
f972ab5e 598 struct marker_data **mdata;
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
cb03932a 661 tmp_tf.cpu_online = 1;
662 tmp_tf.cpu_num = num;
d3d34f49 663 tmp_tf.name = name;
62e4e7bf 664 tmp_tf.tid = tid;
665 tmp_tf.pgid = pgid;
666 tmp_tf.creation = creation;
3865ea09 667 group = g_datalist_id_get_data(&trace->tracefiles, name);
3aee1200 668 if(group == NULL) {
669 /* Elements are automatically cleared when the array is allocated.
670 * It makes the cpu_online variable set to 0 : cpu offline, by default.
671 */
672 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
3865ea09 673 g_datalist_id_set_data_full(&trace->tracefiles, name,
3aee1200 674 group, ltt_tracefile_group_destroy);
675 }
cb03932a 676
3aee1200 677 /* Add the per cpu tracefile to the named group */
678 unsigned int old_len = group->len;
679 if(num+1 > old_len)
680 group = g_array_set_size(group, num+1);
750eb11a 681
cb03932a 682 g_array_index (group, LttTracefile, num) = tmp_tf;
afd57a3c 683 g_array_index (group, LttTracefile, num).event.tracefile =
684 &g_array_index (group, LttTracefile, num);
f972ab5e
MD
685 mdata = &g_array_index (group, LttTracefile, num).mdata;
686 *mdata = allocate_marker_data();
687 if (!*mdata)
688 g_error("Error in allocating marker data");
62e4e7bf 689 }
690 }
691
692 closedir(dir);
3aee1200 693
62e4e7bf 694 return 0;
f7afe191 695}
696
3aee1200 697
698/* Presumes the tracefile is already seeked at the beginning. It makes sense,
699 * because it must be done just after the opening */
2fc874ab 700static int ltt_process_metadata_tracefile(LttTracefile *tf)
3aee1200 701{
702 int err;
3aee1200 703
704 while(1) {
705 err = ltt_tracefile_read_seek(tf);
706 if(err == EPERM) goto seek_error;
707 else if(err == ERANGE) break; /* End of tracefile */
708
709 err = ltt_tracefile_read_update_event(tf);
710 if(err) goto update_error;
711
3aee1200 712 /* The rules are :
2fc874ab 713 * It contains only core events :
714 * 0 : set_marker_id
715 * 1 : set_marker_format
3aee1200 716 */
3c165eaf 717 if(tf->event.event_id >= MARKER_CORE_IDS) {
2fc874ab 718 /* Should only contain core events */
719 g_warning("Error in processing metadata file %s, "
3c165eaf 720 "should not contain event id %u.", g_quark_to_string(tf->name),
721 tf->event.event_id);
3aee1200 722 err = EPERM;
3c165eaf 723 goto event_id_error;
d2083cab 724 } else {
2e13d6af 725 char *pos;
750eb11a 726 const char *channel_name, *marker_name, *format;
3c165eaf 727 uint16_t id;
728 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
3aee1200 729
3c165eaf 730 switch((enum marker_id)tf->event.event_id) {
731 case MARKER_ID_SET_MARKER_ID:
750eb11a 732 channel_name = pos = tf->event.data;
733 pos += strlen(channel_name) + 1;
734 marker_name = pos;
735 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s.%s",
736 channel_name, marker_name);
2e13d6af 737 pos += strlen(marker_name) + 1;
2fc874ab 738 pos += ltt_align((size_t)pos, sizeof(guint16), tf->alignment);
3c165eaf 739 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
750eb11a 740 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s.%s id %hu",
741 channel_name, marker_name, id);
3c165eaf 742 pos += sizeof(guint16);
743 int_size = *(guint8*)pos;
744 pos += sizeof(guint8);
745 long_size = *(guint8*)pos;
746 pos += sizeof(guint8);
747 pointer_size = *(guint8*)pos;
748 pos += sizeof(guint8);
749 size_t_size = *(guint8*)pos;
750 pos += sizeof(guint8);
751 alignment = *(guint8*)pos;
752 pos += sizeof(guint8);
750eb11a 753 marker_id_event(tf->trace,
754 g_quark_from_string(channel_name),
755 g_quark_from_string(marker_name),
3c165eaf 756 id, int_size, long_size,
757 pointer_size, size_t_size, alignment);
3aee1200 758 break;
3c165eaf 759 case MARKER_ID_SET_MARKER_FORMAT:
750eb11a 760 channel_name = pos = tf->event.data;
761 pos += strlen(channel_name) + 1;
762 marker_name = pos;
763 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s.%s",
764 channel_name, marker_name);
2e13d6af 765 pos += strlen(marker_name) + 1;
2e13d6af 766 format = pos;
3c165eaf 767 pos += strlen(format) + 1;
750eb11a 768 marker_format_event(tf->trace,
769 g_quark_from_string(channel_name),
770 g_quark_from_string(marker_name),
3c165eaf 771 format);
2fc874ab 772 /* get information from dictionary TODO */
d1bb700c 773 break;
3aee1200 774 default:
2fc874ab 775 g_warning("Error in processing metadata file %s, "
3c165eaf 776 "unknown event id %hhu.",
3aee1200 777 g_quark_to_string(tf->name),
778 tf->event.event_id);
779 err = EPERM;
780 goto event_id_error;
781 }
782 }
963b5f2d 783 }
3aee1200 784 return 0;
963b5f2d 785
3aee1200 786 /* Error handling */
3aee1200 787event_id_error:
3aee1200 788update_error:
789seek_error:
2fc874ab 790 g_warning("An error occured in metadata tracefile parsing");
3aee1200 791 return err;
6cd62ccf 792}
793
e95fe8f7 794/*
795 * Open a trace and return its LttTrace handle.
796 *
797 * pathname must be the directory of the trace
798 */
963b5f2d 799
3aee1200 800LttTrace *ltt_trace_open(const gchar *pathname)
801{
802 gchar abs_path[PATH_MAX];
803 LttTrace * t;
804 LttTracefile *tf;
805 GArray *group;
43ed82b5 806 unsigned int i;
807 int ret;
64dd41a5 808 ltt_subbuffer_header_t *header;
62e4e7bf 809 DIR *dir;
810 struct dirent *entry;
62e4e7bf 811 struct stat stat_buf;
b56dcdf2 812 gchar path[PATH_MAX];
3aee1200 813
814 t = g_new(LttTrace, 1);
815 if(!t) goto alloc_error;
816
817 get_absolute_pathname(pathname, abs_path);
818 t->pathname = g_quark_from_string(abs_path);
819
3aee1200 820 g_datalist_init(&t->tracefiles);
b56dcdf2 821
822 /* Test to see if it looks like a trace */
62e4e7bf 823 dir = opendir(abs_path);
824 if(dir == NULL) {
825 perror(abs_path);
826 goto open_error;
827 }
828 while((entry = readdir(dir)) != NULL) {
b56dcdf2 829 strcpy(path, abs_path);
830 strcat(path, "/");
831 strcat(path, entry->d_name);
62e4e7bf 832 ret = stat(path, &stat_buf);
833 if(ret == -1) {
834 perror(path);
835 continue;
836 }
b56dcdf2 837 }
838 closedir(dir);
839
b56dcdf2 840 /* Open all the tracefiles */
22660d97 841 t->start_freq= 0;
e45551ac 842 if(open_tracefiles(t, abs_path, "")) {
843 g_warning("Error opening tracefile %s", abs_path);
b56dcdf2 844 goto find_error;
e45551ac 845 }
3aee1200 846
750eb11a 847 /* Parse each trace metadata_N files : get runtime fac. info */
2fc874ab 848 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_METADATA);
3aee1200 849 if(group == NULL) {
8af0138e 850 g_warning("Trace %s has no metadata tracefile", abs_path);
750eb11a 851 goto find_error;
3aee1200 852 }
853
e0c7c400 854 /*
750eb11a 855 * Get the trace information for the metadata_0 tracefile.
e0c7c400 856 * Getting a correct trace start_time and start_tsc is insured by the fact
857 * that no subbuffers are supposed to be lost in the metadata channel.
858 * Therefore, the first subbuffer contains the start_tsc timestamp in its
859 * buffer header.
860 */
16fcbb80 861 g_assert(group->len > 0);
862 tf = &g_array_index (group, LttTracefile, 0);
64dd41a5 863 header = (ltt_subbuffer_header_t *)tf->buffer.head;
ac3b1c7d 864 ret = parse_trace_header(header, tf, t);
865 g_assert(!ret);
b56dcdf2 866
867 t->num_cpu = group->len;
9c9bf2d4
BP
868 t->drift = 1.;
869 t->offset = 0.;
16fcbb80 870
750eb11a 871 //ret = allocate_marker_data(t);
872 //if (ret)
873 // g_error("Error in allocating marker data");
d79909d1 874
3aee1200 875 for(i=0; i<group->len; i++) {
876 tf = &g_array_index (group, LttTracefile, i);
e85b0b1b 877 if (tf->cpu_online)
2fc874ab 878 if(ltt_process_metadata_tracefile(tf))
750eb11a 879 goto find_error;
880 // goto metadata_error;
3aee1200 881 }
dd3a6d39 882
3aee1200 883 return t;
884
885 /* Error handling */
750eb11a 886//metadata_error:
887// destroy_marker_data(t);
b56dcdf2 888find_error:
3aee1200 889 g_datalist_clear(&t->tracefiles);
b56dcdf2 890open_error:
3aee1200 891 g_free(t);
892alloc_error:
893 return NULL;
894
895}
6cd62ccf 896
e95fe8f7 897/* Open another, completely independant, instance of a trace.
898 *
899 * A read on this new instance will read the first event of the trace.
900 *
3aee1200 901 * When we copy a trace, we want all the opening actions to happen again :
902 * the trace will be reopened and totally independant from the original.
903 * That's why we call ltt_trace_open.
e95fe8f7 904 */
3aee1200 905LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 906{
3aee1200 907 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 908}
909
e95fe8f7 910/*
911 * Close a trace
912 */
913
3aee1200 914void ltt_trace_close(LttTrace *t)
6cd62ccf 915{
3aee1200 916 g_datalist_clear(&t->tracefiles);
917 g_free(t);
6cd62ccf 918}
919
3aee1200 920
6cd62ccf 921/*****************************************************************************
3aee1200 922 * Get the start time and end time of the trace
6cd62ccf 923 ****************************************************************************/
924
3c165eaf 925void ltt_tracefile_time_span_get(LttTracefile *tf,
3aee1200 926 LttTime *start, LttTime *end)
6cd62ccf 927{
3aee1200 928 int err;
6cd62ccf 929
3aee1200 930 err = map_block(tf, 0);
931 if(unlikely(err)) {
932 g_error("Can not map block");
933 *start = ltt_time_infinite;
934 } else
935 *start = tf->buffer.begin.timestamp;
936
937 err = map_block(tf, tf->num_blocks - 1); /* Last block */
938 if(unlikely(err)) {
939 g_error("Can not map block");
940 *end = ltt_time_zero;
941 } else
942 *end = tf->buffer.end.timestamp;
3729b611
PMF
943
944 g_assert(end->tv_sec <= G_MAXUINT);
6cd62ccf 945}
946
3aee1200 947struct tracefile_time_span_get_args {
948 LttTrace *t;
949 LttTime *start;
950 LttTime *end;
951};
963b5f2d 952
8655430b 953static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 954{
3aee1200 955 struct tracefile_time_span_get_args *args =
956 (struct tracefile_time_span_get_args*)user_data;
957
958 GArray *group = (GArray *)data;
43ed82b5 959 unsigned int i;
3aee1200 960 LttTracefile *tf;
961 LttTime tmp_start;
962 LttTime tmp_end;
963
964 for(i=0; i<group->len; i++) {
965 tf = &g_array_index (group, LttTracefile, i);
966 if(tf->cpu_online) {
967 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
968 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
969 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
970 }
971 }
6cd62ccf 972}
973
8655430b 974/* return the start and end time of a trace */
975
487ad181 976void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
977{
3aee1200 978 LttTime min_start = ltt_time_infinite;
979 LttTime max_end = ltt_time_zero;
980 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 981
3aee1200 982 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
983
984 if(start != NULL) *start = min_start;
985 if(end != NULL) *end = max_end;
986
487ad181 987}
988
989
3aee1200 990/* Seek to the first event in a tracefile that has a time equal or greater than
991 * the time passed in parameter.
992 *
993 * If the time parameter is outside the tracefile time span, seek to the first
27304273 994 * event or if after, return ERANGE.
3aee1200 995 *
996 * If the time parameter is before the first event, we have to seek specially to
997 * there.
998 *
27304273 999 * If the time is after the end of the trace, return ERANGE.
3aee1200 1000 *
1001 * Do a binary search to find the right block, then a sequential search in the
1002 * block to find the event.
1003 *
1004 * In the special case where the time requested fits inside a block that has no
1005 * event corresponding to the requested time, the first event of the next block
1006 * will be seeked.
1007 *
1008 * IMPORTANT NOTE : // FIXME everywhere...
1009 *
1010 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1011 * you will jump over an event if you do.
1012 *
1013 * Return value : 0 : no error, the tf->event can be used
27304273 1014 * ERANGE : time if after the last event of the trace
3aee1200 1015 * otherwise : this is an error.
1016 *
1017 * */
1018
1019int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1020{
1021 int ret = 0;
1022 int err;
1023 unsigned int block_num, high, low;
1024
1025 /* seek at the beginning of trace */
1026 err = map_block(tf, 0); /* First block */
1027 if(unlikely(err)) {
1028 g_error("Can not map block");
1029 goto fail;
caf7a67a 1030 }
1031
3aee1200 1032 /* If the time is lower or equal the beginning of the trace,
1033 * go to the first event. */
1034 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1035 ret = ltt_tracefile_read(tf);
27304273 1036 if(ret == ERANGE) goto range;
1037 else if (ret) goto fail;
3aee1200 1038 goto found; /* There is either no event in the trace or the event points
1039 to the first event in the trace */
1040 }
caf7a67a 1041
3aee1200 1042 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1043 if(unlikely(err)) {
1044 g_error("Can not map block");
1045 goto fail;
caf7a67a 1046 }
6cd62ccf 1047
27304273 1048 /* If the time is after the end of the trace, return ERANGE. */
1049 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1050 goto range;
3aee1200 1051 }
62e55dd6 1052
3aee1200 1053 /* Binary search the block */
1054 high = tf->num_blocks - 1;
1055 low = 0;
1056
1057 while(1) {
1058 block_num = ((high-low) / 2) + low;
1059
1060 err = map_block(tf, block_num);
1061 if(unlikely(err)) {
1062 g_error("Can not map block");
1063 goto fail;
db55eaae 1064 }
3aee1200 1065 if(high == low) {
1066 /* We cannot divide anymore : this is what would happen if the time
1067 * requested was exactly between two consecutive buffers'end and start
1068 * timestamps. This is also what would happend if we didn't deal with out
1069 * of span cases prior in this function. */
1070 /* The event is right in the buffer!
1071 * (or in the next buffer first event) */
1072 while(1) {
1073 ret = ltt_tracefile_read(tf);
27304273 1074 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1075 else if(ret) goto fail;
1076
d9e13a0f 1077 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 1078 goto found;
3aee1200 1079 }
1080
27304273 1081 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 1082 /* go to lower part */
b1369bef 1083 high = block_num - 1;
3aee1200 1084 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1085 /* go to higher part */
b1369bef 1086 low = block_num + 1;
3aee1200 1087 } else {/* The event is right in the buffer!
1088 (or in the next buffer first event) */
1089 while(1) {
27304273 1090 ret = ltt_tracefile_read(tf);
1091 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1092 else if(ret) goto fail;
1093
d9e13a0f 1094 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1095 break;
6cd62ccf 1096 }
3aee1200 1097 goto found;
6cd62ccf 1098 }
6cd62ccf 1099 }
3aee1200 1100
1101found:
1102 return 0;
27304273 1103range:
1104 return ERANGE;
3aee1200 1105
1106 /* Error handling */
1107fail:
1108 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1109 g_quark_to_string(tf->name));
1110 return EPERM;
6cd62ccf 1111}
1112
e95fe8f7 1113/* Seek to a position indicated by an LttEventPosition
1114 */
80da81ad 1115
8655430b 1116int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep)
1117{
3aee1200 1118 int err;
1119
1120 if(ep->tracefile != tf) {
1121 goto fail;
80da81ad 1122 }
1123
3aee1200 1124 err = map_block(tf, ep->block);
1125 if(unlikely(err)) {
1126 g_error("Can not map block");
1127 goto fail;
1128 }
1129
1130 tf->event.offset = ep->offset;
18206708 1131
62e4e7bf 1132 /* Put back the event real tsc */
1133 tf->event.tsc = ep->tsc;
1134 tf->buffer.tsc = ep->tsc;
78f79181 1135
3aee1200 1136 err = ltt_tracefile_read_update_event(tf);
1137 if(err) goto fail;
73faf25a 1138
1139 /* deactivate this, as it does nothing for now
3aee1200 1140 err = ltt_tracefile_read_op(tf);
1141 if(err) goto fail;
73faf25a 1142 */
80da81ad 1143
a0c1f622 1144 return 0;
3aee1200 1145
1146fail:
1147 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1148 g_quark_to_string(tf->name));
a0c1f622 1149 return 1;
3aee1200 1150}
1151
9c9bf2d4
BP
1152/*
1153 * Convert a value in "TSC scale" to a value in nanoseconds
1154 */
1155guint64 tsc_to_uint64(guint32 freq_scale, uint64_t start_freq, guint64 tsc)
1156{
1157 return (double) tsc * NANOSECONDS_PER_SECOND * freq_scale / start_freq;
1158}
1159
e95fe8f7 1160/* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1161 * corresponds to.
1162 */
ae3d0f50 1163LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
3aee1200 1164{
9c9bf2d4
BP
1165 return ltt_time_from_uint64(tsc_to_uint64(tf->trace->freq_scale,
1166 tf->trace->start_freq, tf->trace->drift * tsc +
1167 tf->trace->offset));
80da81ad 1168}
1169
ae3d0f50 1170/* Calculate the real event time based on the buffer boundaries */
1171LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1172{
62e4e7bf 1173 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
ae3d0f50 1174}
1175
eed2ef37 1176
1177/* Get the current event of the tracefile : valid until the next read */
1178LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1179{
1180 return &tf->event;
1181}
1182
1183
1184
6cd62ccf 1185/*****************************************************************************
1186 *Function name
3aee1200 1187 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1188 *Input params
1189 * t : tracefile
1190 *Return value
3aee1200 1191 *
1192 * Returns 0 if an event can be used in tf->event.
d822520b 1193 * Returns ERANGE on end of trace. The event in tf->event still can be used
1194 * (if the last block was not empty).
3aee1200 1195 * Returns EPERM on error.
1196 *
1197 * This function does make the tracefile event structure point to the event
1198 * currently pointed to by the tf->event.
1199 *
1200 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1201 * reinitialize it after an error if you want results to be coherent.
1202 * It would be the case if a end of trace last buffer has no event : the end
1203 * of trace wouldn't be returned, but an error.
1204 * We make the assumption there is at least one event per buffer.
6cd62ccf 1205 ****************************************************************************/
1206
3aee1200 1207int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1208{
963b5f2d 1209 int err;
6cd62ccf 1210
3aee1200 1211 err = ltt_tracefile_read_seek(tf);
1212 if(err) return err;
1213 err = ltt_tracefile_read_update_event(tf);
1214 if(err) return err;
73faf25a 1215
1216 /* deactivate this, as it does nothing for now
3aee1200 1217 err = ltt_tracefile_read_op(tf);
1218 if(err) return err;
73faf25a 1219 */
3aee1200 1220
1221 return 0;
1222}
1223
1224int ltt_tracefile_read_seek(LttTracefile *tf)
1225{
1226 int err;
1227
1228 /* Get next buffer until we finally have an event, or end of trace */
1229 while(1) {
1230 err = ltt_seek_next_event(tf);
1231 if(unlikely(err == ENOPROTOOPT)) {
1232 return EPERM;
bdc36259 1233 }
bdc36259 1234
3aee1200 1235 /* Are we at the end of the buffer ? */
1236 if(err == ERANGE) {
1237 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1238 return ERANGE;
1239 } else {
1240 /* get next block */
1241 err = map_block(tf, tf->buffer.index + 1);
1242 if(unlikely(err)) {
1243 g_error("Can not map block");
1244 return EPERM;
1245 }
1246 }
1247 } else break; /* We found an event ! */
1248 }
2dee981d 1249
3aee1200 1250 return 0;
1251}
18206708 1252
e95fe8f7 1253/* do an operation when reading a new event */
18206708 1254
73faf25a 1255/* This function does nothing for now */
1256#if 0
3aee1200 1257int ltt_tracefile_read_op(LttTracefile *tf)
1258{
3aee1200 1259 LttEvent *event;
1260
1261 event = &tf->event;
18206708 1262
73faf25a 1263 /* do event specific operation */
1264
1265 /* nothing */
40331ba8 1266
3aee1200 1267 return 0;
6cd62ccf 1268}
73faf25a 1269#endif
6cd62ccf 1270
91f8d488 1271static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1272{
1273 unsigned int offset = 0;
1274 int i, j;
1275
43ed82b5 1276 g_printf("Event header (tracefile %s offset %" PRIx64 "):\n",
afd57a3c 1277 g_quark_to_string(ev->tracefile->long_name),
f0241068
MD
1278 (uint64_t)ev->tracefile->buffer.offset +
1279 (long)start_pos - (long)ev->tracefile->buffer.head);
91f8d488 1280
1281 while (offset < (long)end_pos - (long)start_pos) {
1282 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1283 g_printf(" ");
1284
1285 for (i = 0; i < 4 ; i++) {
1286 for (j = 0; j < 4; j++) {
1287 if (offset + ((i * 4) + j) <
1288 (long)end_pos - (long)start_pos)
1289 g_printf("%02hhX",
d3353231 1290 ((char*)start_pos)[offset + ((i * 4) + j)]);
91f8d488 1291 else
1292 g_printf(" ");
1293 g_printf(" ");
1294 }
1295 if (i < 4)
1296 g_printf(" ");
1297 }
1298 offset+=16;
1299 g_printf("\n");
1300 }
1301}
1302
6cd62ccf 1303
3aee1200 1304/* same as ltt_tracefile_read, but does not seek to the next event nor call
1305 * event specific operation. */
1306int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1307{
3aee1200 1308 void * pos;
1309 LttEvent *event;
91f8d488 1310 void *pos_aligned;
551bf485 1311 guint16 packed_evid; /* event id reader from the 5 bits in header */
3aee1200 1312
1313 event = &tf->event;
eed2ef37 1314 pos = tf->buffer.head + event->offset;
3aee1200 1315
1316 /* Read event header */
1317
62e4e7bf 1318 /* Align the head */
2fc874ab 1319 pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment);
91f8d488 1320 pos_aligned = pos;
3aee1200 1321
2fc874ab 1322 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos);
551bf485 1323 event->event_id = packed_evid = event->timestamp >> tf->tscbits;
a9048165 1324 event->timestamp = event->timestamp & tf->tsc_mask;
2fc874ab 1325 pos += sizeof(guint32);
1326
551bf485 1327 switch (packed_evid) {
2fc874ab 1328 case 29: /* LTT_RFLAG_ID_SIZE_TSC */
1329 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1330 pos += sizeof(guint16);
1331 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1332 pos += sizeof(guint16);
1333 if (event->event_size == 0xFFFF) {
1334 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1335 pos += sizeof(guint32);
d1bb700c 1336 }
2fc874ab 1337 pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment);
1338 tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
62e4e7bf 1339 pos += sizeof(guint64);
2fc874ab 1340 break;
1341 case 30: /* LTT_RFLAG_ID_SIZE */
f439de06 1342 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
3c165eaf 1343 pos += sizeof(guint16);
d1bb700c 1344 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1345 pos += sizeof(guint16);
2fc874ab 1346 if (event->event_size == 0xFFFF) {
1347 event->event_size = ltt_get_uint32(LTT_GET_BO(tf), pos);
1348 pos += sizeof(guint32);
1349 }
1350 break;
1351 case 31: /* LTT_RFLAG_ID */
1352 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1353 pos += sizeof(guint16);
1354 event->event_size = G_MAXUINT;
1355 break;
1356 default:
1357 event->event_size = G_MAXUINT;
1358 break;
1359 }
1360
551bf485 1361 if (likely(packed_evid != 29)) {
2fc874ab 1362 /* No extended timestamp */
1363 if (event->timestamp < (tf->buffer.tsc & tf->tsc_mask))
1364 tf->buffer.tsc = ((tf->buffer.tsc & ~tf->tsc_mask) /* overflow */
1365 + tf->tsc_mask_next_bit)
1366 | (guint64)event->timestamp;
1367 else
1368 tf->buffer.tsc = (tf->buffer.tsc & ~tf->tsc_mask) /* no overflow */
1369 | (guint64)event->timestamp;
d1bb700c 1370 }
2fc874ab 1371 event->tsc = tf->buffer.tsc;
1372
1373 event->event_time = ltt_interpolate_time(tf, event);
91f8d488 1374
1375 if (a_event_debug)
1376 print_debug_event_header(event, pos_aligned, pos);
1377
3aee1200 1378 event->data = pos;
1379
2fc874ab 1380 /*
1381 * Let ltt_update_event_size update event->data according to the largest
1382 * alignment within the payload.
1383 * Get the data size and update the event fields with the current
1384 * information. */
eed2ef37 1385 ltt_update_event_size(tf);
77175651 1386
3aee1200 1387 return 0;
6cd62ccf 1388}
1389
507915ee 1390
6cd62ccf 1391/****************************************************************************
1392 *Function name
3aee1200 1393 * map_block : map a block from the file
6cd62ccf 1394 *Input Params
1395 * lttdes : ltt trace file
1396 * whichBlock : the block which will be read
1397 *return value
1398 * 0 : success
1399 * EINVAL : lseek fail
1400 * EIO : can not read from the file
1401 ****************************************************************************/
1402
8655430b 1403static gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1404{
b77d1b57 1405 int page_size = getpagesize();
64dd41a5 1406 ltt_subbuffer_header_t *header;
f0241068
MD
1407 uint64_t offset;
1408 uint32_t size;
1409 int ret;
3aee1200 1410
1411 g_assert(block_num < tf->num_blocks);
6cd62ccf 1412
f628823c 1413 if(tf->buffer.head != NULL) {
f0241068 1414 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buffer.size))) {
f628823c 1415 g_warning("unmap size : %u\n",
f0241068 1416 PAGE_ALIGN(tf->buffer.size));
f628823c 1417 perror("munmap error");
1418 g_assert(0);
1419 }
1420 }
f0241068
MD
1421
1422 ret = get_block_offset_size(tf, block_num, &offset, &size);
1423 g_assert(!ret);
1424
93556665
MD
1425 g_debug("Map block %u, offset %llu, size %u\n", block_num,
1426 (unsigned long long)offset, (unsigned int)size);
1427
3aee1200 1428 /* Multiple of pages aligned head */
f0241068
MD
1429 tf->buffer.head = mmap(0, (size_t)size, PROT_READ, MAP_PRIVATE,
1430 tf->fd, (off_t)offset);
3aee1200 1431
3865ea09 1432 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1433 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1434 g_assert(0);
3aee1200 1435 goto map_error;
6cd62ccf 1436 }
f64fedd7 1437 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
6cd62ccf 1438
3aee1200 1439 tf->buffer.index = block_num;
1440
64dd41a5 1441 header = (ltt_subbuffer_header_t *)tf->buffer.head;
3aee1200 1442
3aee1200 1443 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1444 &header->cycle_count_begin);
3aee1200 1445 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
64dd41a5 1446 &header->cycle_count_end);
f0241068
MD
1447 tf->buffer.offset = offset;
1448 tf->buffer.size = ltt_get_uint32(LTT_GET_BO(tf),
1449 &header->sb_size);
f0241068
MD
1450 tf->buffer.data_size = ltt_get_uint32(LTT_GET_BO(tf),
1451 &header->data_size);
3aee1200 1452 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1453 tf->event.tsc = tf->buffer.tsc;
986e2a7c 1454 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 1455
93556665 1456 g_assert(size == tf->buffer.size);
f0241068
MD
1457 g_assert(tf->buffer.data_size <= tf->buffer.size);
1458
22660d97
BP
1459 if (tf->trace->start_freq)
1460 {
1461 tf->buffer.begin.freq = tf->trace->start_freq;
1462 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
1463 tf->buffer.begin.cycle_count);
1464 tf->buffer.end.freq = tf->trace->start_freq;
1465 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
1466 tf->buffer.end.cycle_count);
1467 }
1468
3aee1200 1469 /* Make the current event point to the beginning of the buffer :
1470 * it means that the event read must get the first event. */
1471 tf->event.tracefile = tf;
1472 tf->event.block = block_num;
eed2ef37 1473 tf->event.offset = 0;
3aee1200 1474
a2bbf2e5 1475 if (header->events_lost) {
1476 g_warning("%d events lost so far in tracefile %s at block %u",
0c2f4984 1477 (guint)header->events_lost,
a2bbf2e5 1478 g_quark_to_string(tf->long_name),
1479 block_num);
426f6149 1480 tf->events_lost = header->events_lost;
1481 }
a2bbf2e5 1482 if (header->subbuf_corrupt) {
1483 g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u",
0c2f4984 1484 (guint)header->subbuf_corrupt,
a2bbf2e5 1485 g_quark_to_string(tf->long_name),
1486 block_num);
426f6149 1487 tf->subbuf_corrupt = header->subbuf_corrupt;
1488 }
1489
3aee1200 1490 return 0;
6cd62ccf 1491
3aee1200 1492map_error:
1493 return -errno;
6cd62ccf 1494}
1495
91f8d488 1496static void print_debug_event_data(LttEvent *ev)
1497{
1498 unsigned int offset = 0;
1499 int i, j;
1500
1501 if (!max(ev->event_size, ev->data_size))
1502 return;
1503
43ed82b5 1504 g_printf("Event data (tracefile %s offset %" PRIx64 "):\n",
1505 g_quark_to_string(ev->tracefile->long_name),
f0241068 1506 (uint64_t)ev->tracefile->buffer.offset
43ed82b5 1507 + (long)ev->data - (long)ev->tracefile->buffer.head);
91f8d488 1508
1509 while (offset < max(ev->event_size, ev->data_size)) {
1510 g_printf("%8lx", (long)ev->data + offset
1511 - (long)ev->tracefile->buffer.head);
1512 g_printf(" ");
1513
1514 for (i = 0; i < 4 ; i++) {
1515 for (j = 0; j < 4; j++) {
1516 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
1517 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
1518 else
1519 g_printf(" ");
1520 g_printf(" ");
1521 }
1522 if (i < 4)
1523 g_printf(" ");
1524 }
1525
1526 g_printf(" ");
1527
1528 for (i = 0; i < 4; i++) {
1529 for (j = 0; j < 4; j++) {
1530 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
1531 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
1532 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
1533 else
1534 g_printf(".");
1535 } else
1536 g_printf(" ");
1537 }
1538 }
1539 offset+=16;
1540 g_printf("\n");
1541 }
1542}
1543
77175651 1544/* It will update the fields offsets too */
1545void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 1546{
2312de30 1547 off_t size = 0;
d2007fbd 1548 struct marker_info *info;
750eb11a 1549
1550 if (tf->name == LTT_TRACEFILE_NAME_METADATA) {
1551 switch((enum marker_id)tf->event.event_id) {
1552 case MARKER_ID_SET_MARKER_ID:
1553 size = strlen((char*)tf->event.data) + 1;
1554 g_debug("marker %s id set", (char*)tf->event.data + size);
1555 size += strlen((char*)tf->event.data + size) + 1;
1556 size += ltt_align(size, sizeof(guint16), tf->alignment);
1557 size += sizeof(guint16);
1558 size += sizeof(guint8);
1559 size += sizeof(guint8);
1560 size += sizeof(guint8);
1561 size += sizeof(guint8);
1562 size += sizeof(guint8);
1563 break;
1564 case MARKER_ID_SET_MARKER_FORMAT:
1565 size = strlen((char*)tf->event.data) + 1;
1566 g_debug("marker %s format set", (char*)tf->event.data);
1567 size += strlen((char*)tf->event.data + size) + 1;
1568 size += strlen((char*)tf->event.data + size) + 1;
1569 break;
1570 }
256a5b3a 1571 }
1572
750eb11a 1573 info = marker_get_info_from_id(tf->mdata, tf->event.event_id);
dcf96842 1574
256a5b3a 1575 if (tf->event.event_id >= MARKER_CORE_IDS)
1576 g_assert(info != NULL);
1577
1578 /* Do not update field offsets of core markers when initially reading the
2fc874ab 1579 * metadata tracefile when the infos about these markers do not exist yet.
256a5b3a 1580 */
1581 if (likely(info && info->fields)) {
2fc874ab 1582 /* alignment */
196085f2 1583 tf->event.data += ltt_align((off_t)(unsigned long)tf->event.data,
1584 info->largest_align,
2fc874ab 1585 info->alignment);
1586 /* size, dynamically computed */
256a5b3a 1587 if (info->size != -1)
1588 size = info->size;
1589 else
750eb11a 1590 size = marker_update_fields_offsets(marker_get_info_from_id(tf->mdata,
256a5b3a 1591 tf->event.event_id), tf->event.data);
44f317b7 1592 }
c4afd5d8 1593
d2007fbd 1594 tf->event.data_size = size;
1595
1596 /* Check consistency between kernel and LTTV structure sizes */
2fc874ab 1597 if(tf->event.event_size == G_MAXUINT) {
d2007fbd 1598 /* Event size too big to fit in the event size field */
1599 tf->event.event_size = tf->event.data_size;
1600 }
91f8d488 1601
1602 if (a_event_debug)
1603 print_debug_event_data(&tf->event);
1604
d2007fbd 1605 if (tf->event.data_size != tf->event.event_size) {
750eb11a 1606 struct marker_info *info = marker_get_info_from_id(tf->mdata,
3c165eaf 1607 tf->event.event_id);
750eb11a 1608 if (!info)
1609 g_error("Undescribed event %hhu in channel %s", tf->event.event_id,
1610 g_quark_to_string(tf->name));
3c165eaf 1611 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
1612 g_quark_to_string(info->name),
1613 tf->event.event_size, tf->event.data_size);
d2007fbd 1614 exit(-1);
1615 }
6cd62ccf 1616}
1617
6cd62ccf 1618
2fc874ab 1619/* Take the tf current event offset and use the event id to figure out where is
1620 * the next event offset.
3aee1200 1621 *
1622 * This is an internal function not aiming at being used elsewhere : it will
1623 * not jump over the current block limits. Please consider using
1624 * ltt_tracefile_read to do this.
1625 *
1626 * Returns 0 on success
1627 * ERANGE if we are at the end of the buffer.
1628 * ENOPROTOOPT if an error occured when getting the current event size.
1629 */
8655430b 1630static int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 1631{
3aee1200 1632 int ret = 0;
1633 void *pos;
3aee1200 1634
1635 /* seek over the buffer header if we are at the buffer start */
eed2ef37 1636 if(tf->event.offset == 0) {
51551c6f 1637 tf->event.offset += tf->buffer_header_size;
b77d1b57 1638
f0241068 1639 if(tf->event.offset == tf->buffer.data_size) {
b77d1b57 1640 ret = ERANGE;
1641 }
3aee1200 1642 goto found;
1643 }
2fc874ab 1644
3aee1200 1645 pos = tf->event.data;
1646
77175651 1647 if(tf->event.data_size < 0) goto error;
3aee1200 1648
77175651 1649 pos += (size_t)tf->event.data_size;
3aee1200 1650
eed2ef37 1651 tf->event.offset = pos - tf->buffer.head;
cb03932a 1652
f0241068 1653 if(tf->event.offset == tf->buffer.data_size) {
cb03932a 1654 ret = ERANGE;
1655 goto found;
1656 }
f0241068 1657 g_assert(tf->event.offset < tf->buffer.data_size);
3aee1200 1658
1659found:
1660 return ret;
1661
1662error:
1663 g_error("Error in ltt_seek_next_event for tracefile %s",
1664 g_quark_to_string(tf->name));
1665 return ENOPROTOOPT;
6cd62ccf 1666}
1667
f104d082 1668
6cd62ccf 1669/*****************************************************************************
1670 *Function name
eed2ef37 1671 * ltt_get_int : get an integer number
6cd62ccf 1672 *Input params
3aee1200 1673 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 1674 * size : the size of the integer
3aee1200 1675 * ptr : the data pointer
6cd62ccf 1676 *Return value
cf74a6f1 1677 * gint64 : a 64 bits integer
6cd62ccf 1678 ****************************************************************************/
1679
eed2ef37 1680gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 1681{
3aee1200 1682 gint64 val;
cf74a6f1 1683
1684 switch(size) {
3aee1200 1685 case 1: val = *((gint8*)data); break;
1686 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
1687 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
1688 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
1689 default: val = ltt_get_int64(reverse_byte_order, data);
1690 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 1691 break;
1692 }
1693
3aee1200 1694 return val;
6cd62ccf 1695}
3aee1200 1696
6cd62ccf 1697/*****************************************************************************
1698 *Function name
eed2ef37 1699 * ltt_get_uint : get an unsigned integer number
6cd62ccf 1700 *Input params
3aee1200 1701 * reverse_byte_order: must we reverse the byte order ?
1702 * size : the size of the integer
1703 * ptr : the data pointer
1704 *Return value
1705 * guint64 : a 64 bits unsigned integer
6cd62ccf 1706 ****************************************************************************/
1707
eed2ef37 1708guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 1709{
3aee1200 1710 guint64 val;
1711
1712 switch(size) {
1713 case 1: val = *((gint8*)data); break;
1714 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
1715 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
1716 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
1717 default: val = ltt_get_uint64(reverse_byte_order, data);
1718 g_critical("get_uint : unsigned integer size %d unknown",
1719 size);
1720 break;
1721 }
1722
1723 return val;
6cd62ccf 1724}
3aee1200 1725
1726
a5dcde2f 1727/* get the node name of the system */
1728
1729char * ltt_trace_system_description_node_name (LttSystemDescription * s)
1730{
1731 return s->node_name;
1732}
1733
1734
1735/* get the domain name of the system */
1736
1737char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
1738{
1739 return s->domain_name;
1740}
1741
1742
1743/* get the description of the system */
1744
1745char * ltt_trace_system_description_description (LttSystemDescription * s)
1746{
1747 return s->description;
1748}
1749
1750
bf33dd50 1751/* get the NTP corrected start time of the trace */
7bd563ec 1752LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 1753{
7bd563ec 1754 return t->start_time;
a5dcde2f 1755}
1756
bf33dd50 1757/* get the monotonic start time of the trace */
1758LttTime ltt_trace_start_time_monotonic(LttTrace *t)
1759{
1760 return t->start_time_from_tsc;
1761}
1762
43ed82b5 1763static __attribute__ ((__unused__)) LttTracefile *ltt_tracefile_new()
18206708 1764{
afd57a3c 1765 LttTracefile *tf;
1766 tf = g_new(LttTracefile, 1);
1767 tf->event.tracefile = tf;
1768 return tf;
18206708 1769}
1770
43ed82b5 1771static __attribute__ ((__unused__)) void ltt_tracefile_destroy(LttTracefile *tf)
18206708 1772{
1773 g_free(tf);
1774}
1775
43ed82b5 1776static __attribute__ ((__unused__)) void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
18206708 1777{
1778 *dest = *src;
1779}
1780
3aee1200 1781/* Before library loading... */
1782
8655430b 1783static __attribute__((constructor)) void init(void)
3aee1200 1784{
750eb11a 1785 LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("metadata");
3aee1200 1786}
a31d2894
WB
1787
1788/*****************************************************************************
1789 *Function name
1790 * ltt_tracefile_open_header : based on ltt_tracefile_open but it stops
72984157
WB
1791 * when it gets the header
1792 *Input params
a31d2894
WB
1793 * fileName : path to the tracefile
1794 * tf : the tracefile (metadata_0) where the header will be read
1795 *Return value
1796 * ltt_subbuffer_header_t : the header containing the version number
1797 ****************************************************************************/
1798static ltt_subbuffer_header_t * ltt_tracefile_open_header(gchar *fileName, LttTracefile *tf)
1799{
1800 struct stat lTDFStat; /* Trace data file status */
1801 ltt_subbuffer_header_t *header;
1802 int page_size = getpagesize();
72984157 1803
a31d2894
WB
1804 /* open the file */
1805 tf->long_name = g_quark_from_string(fileName);
1806 tf->fd = open(fileName, O_RDONLY);
1807 if(tf->fd < 0){
1808 g_warning("Unable to open input data file %s\n", fileName);
1809 goto end;
1810 }
72984157
WB
1811
1812 /* Get the file's status */
a31d2894
WB
1813 if(fstat(tf->fd, &lTDFStat) < 0){
1814 g_warning("Unable to get the status of the input data file %s\n", fileName);
1815 goto close_file;
1816 }
72984157 1817
a31d2894
WB
1818 /* Is the file large enough to contain a trace */
1819 if(lTDFStat.st_size < (off_t)(ltt_subbuffer_header_size())) {
1820 g_print("The input data file %s does not contain a trace\n", fileName);
1821 goto close_file;
1822 }
72984157 1823
a31d2894
WB
1824 /* Temporarily map the buffer start header to get trace information */
1825 /* Multiple of pages aligned head */
1826 tf->buffer.head = mmap(0,PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ, MAP_PRIVATE, tf->fd, 0);
72984157 1827
a31d2894
WB
1828 if(tf->buffer.head == MAP_FAILED) {
1829 perror("Error in allocating memory for buffer of tracefile");
1830 goto close_file;
1831 }
1832 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
72984157 1833
a31d2894 1834 header = (ltt_subbuffer_header_t *)tf->buffer.head;
72984157 1835
a31d2894 1836 return header;
72984157 1837
a31d2894
WB
1838 close_file:
1839 close(tf->fd);
1840 end:
1841 return 0;
1842}
1843
1844
1845/*****************************************************************************
1846 *Function name
1847 * get_version : get the trace version from a metadata_0 trace file
72984157 1848 *Input params
a31d2894
WB
1849 * pathname : path to the trace
1850 * version_number : the struct that will get the version number
1851 *Return value
1852 * int : 1 if succeed, -1 if error
1853 ****************************************************************************/
72984157 1854int ltt_get_trace_version(const gchar *pathname, struct LttTraceVersion *version_number)
a31d2894
WB
1855{
1856 gchar abs_path[PATH_MAX];
1857 int ret = 0;
1858 DIR *dir;
1859 struct dirent *entry;
1860 struct stat stat_buf;
1861 gchar path[PATH_MAX];
72984157 1862
a31d2894
WB
1863 LttTracefile tmp_tf;
1864 LttTrace * t;
1865 ltt_subbuffer_header_t *header;
72984157 1866
a31d2894 1867 t = g_new(LttTrace, 1);
72984157 1868
a31d2894 1869 get_absolute_pathname(pathname, abs_path);
72984157 1870
a31d2894
WB
1871 /* Test to see if it looks like a trace */
1872 dir = opendir(abs_path);
72984157 1873
a31d2894
WB
1874 if(dir == NULL) {
1875 perror(abs_path);
1876 goto open_error;
1877 }
72984157 1878
a31d2894
WB
1879 while((entry = readdir(dir)) != NULL) {
1880 strcpy(path, abs_path);
1881 strcat(path, "/");
1882 strcat(path, entry->d_name);
1883 ret = stat(path, &stat_buf);
1884 if(ret == -1) {
1885 perror(path);
1886 continue;
1887 }
1888 }
72984157
WB
1889
1890 closedir(dir);
a31d2894 1891 dir = opendir(abs_path);
72984157 1892
a31d2894
WB
1893 while((entry = readdir(dir)) != NULL) {
1894 if(entry->d_name[0] == '.') continue;
1895 if(g_strcmp0(entry->d_name, "metadata_0") != 0) continue;
72984157 1896
a31d2894
WB
1897 strcpy(path, abs_path);
1898 strcat(path, "/");
1899 strcat(path, entry->d_name);
1900 if(ret == -1) {
1901 perror(path);
1902 continue;
1903 }
72984157
WB
1904
1905 header = ltt_tracefile_open_header(path, &tmp_tf);
1906
a31d2894
WB
1907 if(header == NULL) {
1908 g_info("Error getting the header %s", path);
1909 continue; /* error opening the tracefile : bad magic number ? */
1910 }
72984157
WB
1911
1912 version_number->ltt_major_version = header->major_version;
1913 version_number->ltt_minor_version = header->minor_version;
a31d2894 1914 }
72984157 1915
a31d2894 1916 return 0;
72984157 1917
a31d2894
WB
1918 open_error:
1919 g_free(t);
1920 return -1;
1921}
This page took 0.211102 seconds and 4 git commands to generate.