Fix dynamic update of marker data across same channel tracefiles
[lttv.git] / ltt / tracefile.c
1 /* This file is part of the Linux Trace Toolkit viewer
2 * Copyright (C) 2005 Mathieu Desnoyers
3 *
4 * Complete rewrite from the original version made by XangXiu Yang.
5 *
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.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
24
25 #include <stdio.h>
26 #include <fcntl.h>
27 #include <string.h>
28 #include <dirent.h>
29 #include <sys/stat.h>
30 #include <sys/types.h>
31 #include <errno.h>
32 #include <unistd.h>
33 #include <math.h>
34 #include <glib.h>
35 #include <glib/gprintf.h>
36 #include <malloc.h>
37 #include <sys/mman.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <inttypes.h>
41
42 // For realpath
43 #include <limits.h>
44 #include <stdlib.h>
45
46
47 #include <ltt/ltt.h>
48 #include "ltt-private.h"
49 #include <ltt/trace.h>
50 #include <ltt/event.h>
51 #include <ltt/ltt-types.h>
52 #include <ltt/marker.h>
53
54 #define DEFAULT_N_BLOCKS 32
55
56 /* from marker.c */
57 extern long marker_update_fields_offsets(struct marker_info *info, const char *data);
58
59 /* Tracefile names used in this file */
60
61 GQuark LTT_TRACEFILE_NAME_METADATA;
62
63 #ifndef g_open
64 #define g_open open
65 #endif
66
67
68 #define __UNUSED__ __attribute__((__unused__))
69
70 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
71
72 #ifndef g_debug
73 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
74 #endif
75
76 #define g_close close
77
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
83 /* set the offset of the fields belonging to the event,
84 need the information of the archecture */
85 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
86 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
87
88 /* map a fixed size or a block information from the file (fd) */
89 static gint map_block(LttTracefile * tf, guint block_num);
90
91 /* calculate nsec per cycles for current block */
92 #if 0
93 static guint32 calc_nsecs_per_cycle(LttTracefile * t);
94 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
95 #endif //0
96
97 /* go to the next event */
98 static int ltt_seek_next_event(LttTracefile *tf);
99
100 static int open_tracefiles(LttTrace *trace, gchar *root_path,
101 gchar *relative_path);
102 static int ltt_process_metadata_tracefile(LttTracefile *tf);
103 static void ltt_tracefile_time_span_get(LttTracefile *tf,
104 LttTime *start, LttTime *end);
105 static void group_time_span_get(GQuark name, gpointer data, gpointer user_data);
106 static gint map_block(LttTracefile * tf, guint block_num);
107 static void ltt_update_event_size(LttTracefile *tf);
108
109 /* Enable event debugging */
110 static int a_event_debug = 0;
111
112 void ltt_event_debug(int state)
113 {
114 a_event_debug = state;
115 }
116
117 /* trace can be NULL
118 *
119 * Return value : 0 success, 1 bad tracefile
120 */
121 static int parse_trace_header(ltt_subbuffer_header_t *header,
122 LttTracefile *tf, LttTrace *t)
123 {
124 if (header->magic_number == LTT_MAGIC_NUMBER)
125 tf->reverse_bo = 0;
126 else if(header->magic_number == LTT_REV_MAGIC_NUMBER)
127 tf->reverse_bo = 1;
128 else /* invalid magic number, bad tracefile ! */
129 return 1;
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
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)) */
140 tf->float_word_order = 0;
141
142 switch(header->major_version) {
143 case 0:
144 case 1:
145 g_warning("Unsupported trace version : %hhu.%hhu",
146 header->major_version, header->minor_version);
147 return 1;
148 break;
149 case 2:
150 switch(header->minor_version) {
151 case 6:
152 {
153 struct ltt_subbuffer_header_2_6 *vheader = header;
154 tf->buffer_header_size = ltt_subbuffer_header_size();
155 tf->tscbits = 27;
156 tf->eventbits = 5;
157 tf->tsc_mask = ((1ULL << tf->tscbits) - 1);
158 tf->tsc_mask_next_bit = (1ULL << tf->tscbits);
159
160 if(t) {
161 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
162 &vheader->start_freq);
163 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
164 &vheader->freq_scale);
165 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
166 &vheader->cycle_count_begin);
167 t->start_monotonic = 0;
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
174 t->start_time_from_tsc =
175 ltt_time_from_uint64(tsc_to_uint64(t->freq_scale,
176 t->start_freq, t->start_tsc));
177 }
178 }
179 break;
180 default:
181 g_warning("Unsupported trace version : %hhu.%hhu",
182 header->major_version, header->minor_version);
183 return 1;
184 }
185 break;
186 default:
187 g_warning("Unsupported trace version : %hhu.%hhu",
188 header->major_version, header->minor_version);
189 return 1;
190 }
191 return 0;
192 }
193
194 int 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;
201
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
212 int 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,
234 MAP_PRIVATE, tf->fd, (off_t)offset);
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 }
255
256 /*****************************************************************************
257 *Function name
258 * ltt_tracefile_open : open a trace file, construct a LttTracefile
259 *Input params
260 * t : the trace containing the tracefile
261 * fileName : path name of the trace file
262 * tf : the tracefile structure
263 *Return value
264 * : 0 for success, -1 otherwise.
265 ****************************************************************************/
266
267 static gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
268 {
269 struct stat lTDFStat; /* Trace data file status */
270 ltt_subbuffer_header_t *header;
271 int page_size = getpagesize();
272
273 //open the file
274 tf->long_name = g_quark_from_string(fileName);
275 tf->trace = t;
276 tf->fd = open(fileName, O_RDONLY);
277 tf->buf_index = NULL;
278 if(tf->fd < 0){
279 g_warning("Unable to open input data file %s\n", fileName);
280 goto end;
281 }
282
283 // Get the file's status
284 if(fstat(tf->fd, &lTDFStat) < 0){
285 g_warning("Unable to get the status of the input data file %s\n", fileName);
286 goto close_file;
287 }
288
289 // Is the file large enough to contain a trace
290 if(lTDFStat.st_size <
291 (off_t)(ltt_subbuffer_header_size())){
292 g_print("The input data file %s does not contain a trace\n", fileName);
293 goto close_file;
294 }
295
296 /* Temporarily map the buffer start header to get trace information */
297 /* Multiple of pages aligned head */
298 tf->buffer.head = mmap(0,
299 PAGE_ALIGN(ltt_subbuffer_header_size()), PROT_READ,
300 MAP_PRIVATE, tf->fd, 0);
301 if(tf->buffer.head == MAP_FAILED) {
302 perror("Error in allocating memory for buffer of tracefile");
303 goto close_file;
304 }
305 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
306
307 header = (ltt_subbuffer_header_t *)tf->buffer.head;
308
309 if(parse_trace_header(header, tf, NULL)) {
310 g_warning("parse_trace_header error");
311 goto unmap_file;
312 }
313
314 //store the size of the file
315 tf->file_size = lTDFStat.st_size;
316 tf->events_lost = 0;
317 tf->subbuf_corrupt = 0;
318
319 if(munmap(tf->buffer.head,
320 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
321 g_warning("unmap size : %zu\n",
322 PAGE_ALIGN(ltt_subbuffer_header_size()));
323 perror("munmap error");
324 g_assert(0);
325 }
326 tf->buffer.head = NULL;
327
328 /* Create block index */
329 ltt_trace_create_block_index(tf);
330
331 //read the first block
332 if(map_block(tf,0)) {
333 perror("Cannot map block for tracefile");
334 goto close_file;
335 }
336
337 return 0;
338
339 /* Error */
340 unmap_file:
341 if(munmap(tf->buffer.head,
342 PAGE_ALIGN(ltt_subbuffer_header_size()))) {
343 g_warning("unmap size : %zu\n",
344 PAGE_ALIGN(ltt_subbuffer_header_size()));
345 perror("munmap error");
346 g_assert(0);
347 }
348 close_file:
349 close(tf->fd);
350 end:
351 if (tf->buf_index)
352 g_array_free(tf->buf_index, TRUE);
353 return -1;
354 }
355
356
357 /*****************************************************************************
358 *Function name
359 * ltt_tracefile_close: close a trace file,
360 *Input params
361 * t : tracefile which will be closed
362 ****************************************************************************/
363
364 static void ltt_tracefile_close(LttTracefile *t)
365 {
366 int page_size = getpagesize();
367
368 if(t->buffer.head != NULL)
369 if(munmap(t->buffer.head, PAGE_ALIGN(t->buffer.size))) {
370 g_warning("unmap size : %u\n",
371 PAGE_ALIGN(t->buffer.size));
372 perror("munmap error");
373 g_assert(0);
374 }
375
376 close(t->fd);
377 if (t->buf_index)
378 g_array_free(t->buf_index, TRUE);
379 }
380
381 /****************************************************************************
382 * get_absolute_pathname
383 *
384 * return the unique pathname in the system
385 *
386 * MD : Fixed this function so it uses realpath, dealing well with
387 * forgotten cases (.. were not used correctly before).
388 *
389 ****************************************************************************/
390 void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
391 {
392 abs_pathname[0] = '\0';
393
394 if (realpath(pathname, abs_pathname) != NULL)
395 return;
396 else
397 {
398 /* error, return the original path unmodified */
399 strcpy(abs_pathname, pathname);
400 return;
401 }
402 return;
403 }
404
405 /* Search for something like : .*_.*
406 *
407 * The left side is the name, the right side is the number.
408 * Exclude leading /.
409 * Exclude flight- prefix.
410 */
411
412 static int get_tracefile_name_number(gchar *raw_name,
413 GQuark *name,
414 guint *num,
415 gulong *tid,
416 gulong *pgid,
417 guint64 *creation)
418 {
419 guint raw_name_len = strlen(raw_name);
420 gchar char_name[PATH_MAX];
421 int i;
422 int underscore_pos;
423 long int cpu_num;
424 gchar *endptr;
425 gchar *tmpptr;
426
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
435 for(i=raw_name_len-1;i>=0;i--) {
436 if(raw_name[i] == '_') break;
437 }
438 if(i==-1) { /* Either not found or name length is 0 */
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
505 if (!strncmp(raw_name, "flight-", sizeof("flight-") - 1)) {
506 raw_name += sizeof("flight-") - 1;
507 underscore_pos -= sizeof("flight-") - 1;
508 }
509 strncpy(char_name, raw_name, underscore_pos);
510 char_name[underscore_pos] = '\0';
511 *name = g_quark_from_string(char_name);
512 *num = cpu_num;
513 }
514
515
516 return 0;
517 }
518
519
520 GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
521 {
522 return &trace->tracefiles;
523 }
524
525
526 void compute_tracefile_group(GQuark key_id,
527 GArray *group,
528 struct compute_tracefile_group_args *args)
529 {
530 unsigned int i;
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)
536 args->func(tf, args->func_args);
537 }
538 }
539
540
541 static void ltt_tracefile_group_destroy(gpointer data)
542 {
543 GArray *group = (GArray *)data;
544 unsigned int i;
545 LttTracefile *tf;
546
547 for(i=0; i<group->len; i++) {
548 tf = &g_array_index (group, LttTracefile, i);
549 if(tf->cpu_online) {
550 destroy_marker_data(tf->mdata);
551 ltt_tracefile_close(tf);
552 }
553 }
554 g_array_free(group, TRUE);
555 }
556
557 static __attribute__ ((__unused__)) gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
558 {
559 GArray *group = (GArray *)data;
560 unsigned int i;
561 LttTracefile *tf;
562
563 for(i=0; i<group->len; i++) {
564 tf = &g_array_index (group, LttTracefile, i);
565 if(tf->cpu_online)
566 return 1;
567 }
568 return 0;
569 }
570
571
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".
576 *
577 * relative path is the path relative to the trace root
578 * root path is the full path
579 *
580 * A tracefile group is simply an array where all the per cpu tracefiles sit.
581 */
582
583 static int open_tracefiles(LttTrace *trace, gchar *root_path, gchar *relative_path)
584 {
585 DIR *dir = opendir(root_path);
586 struct dirent *entry;
587 struct stat stat_buf;
588 int ret;
589
590 gchar path[PATH_MAX];
591 int path_len;
592 gchar *path_ptr;
593
594 int rel_path_len;
595 gchar rel_path[PATH_MAX];
596 gchar *rel_path_ptr;
597 LttTracefile tmp_tf;
598 struct marker_data **mdata;
599
600 if(dir == NULL) {
601 perror(root_path);
602 return ENOENT;
603 }
604
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;
610
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
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
632 // if(strcmp(rel_path, "/eventdefs") == 0) continue;
633
634 if(S_ISDIR(stat_buf.st_mode)) {
635
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;
641 guint num;
642 gulong tid, pgid;
643 guint64 creation;
644 GArray *group;
645 num = 0;
646 tid = pgid = 0;
647 creation = 0;
648 if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
649 continue; /* invalid name */
650
651 g_debug("Opening file.\n");
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
658 g_debug("Tracefile name is %s and number is %u",
659 g_quark_to_string(name), num);
660
661 tmp_tf.cpu_online = 1;
662 tmp_tf.cpu_num = num;
663 tmp_tf.name = name;
664 tmp_tf.tid = tid;
665 tmp_tf.pgid = pgid;
666 tmp_tf.creation = creation;
667 group = g_datalist_id_get_data(&trace->tracefiles, name);
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);
673 g_datalist_id_set_data_full(&trace->tracefiles, name,
674 group, ltt_tracefile_group_destroy);
675 }
676
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);
681
682 g_array_index (group, LttTracefile, num) = tmp_tf;
683 g_array_index (group, LttTracefile, num).event.tracefile =
684 &g_array_index (group, LttTracefile, num);
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");
689 }
690 }
691
692 closedir(dir);
693
694 return 0;
695 }
696
697
698 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
699 * because it must be done just after the opening */
700 static int ltt_process_metadata_tracefile(LttTracefile *tf)
701 {
702 int err;
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
712 /* The rules are :
713 * It contains only core events :
714 * 0 : set_marker_id
715 * 1 : set_marker_format
716 */
717 if(tf->event.event_id >= MARKER_CORE_IDS) {
718 /* Should only contain core events */
719 g_warning("Error in processing metadata file %s, "
720 "should not contain event id %u.", g_quark_to_string(tf->name),
721 tf->event.event_id);
722 err = EPERM;
723 goto event_id_error;
724 } else {
725 char *pos;
726 const char *channel_name, *marker_name, *format;
727 uint16_t id;
728 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
729
730 switch((enum marker_id)tf->event.event_id) {
731 case MARKER_ID_SET_MARKER_ID:
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);
737 pos += strlen(marker_name) + 1;
738 pos += ltt_align((size_t)pos, sizeof(guint16), tf->alignment);
739 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
740 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s.%s id %hu",
741 channel_name, marker_name, id);
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);
753 marker_id_event(tf->trace,
754 g_quark_from_string(channel_name),
755 g_quark_from_string(marker_name),
756 id, int_size, long_size,
757 pointer_size, size_t_size, alignment);
758 break;
759 case MARKER_ID_SET_MARKER_FORMAT:
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);
765 pos += strlen(marker_name) + 1;
766 format = pos;
767 pos += strlen(format) + 1;
768 marker_format_event(tf->trace,
769 g_quark_from_string(channel_name),
770 g_quark_from_string(marker_name),
771 format);
772 /* get information from dictionary TODO */
773 break;
774 default:
775 g_warning("Error in processing metadata file %s, "
776 "unknown event id %hhu.",
777 g_quark_to_string(tf->name),
778 tf->event.event_id);
779 err = EPERM;
780 goto event_id_error;
781 }
782 }
783 }
784 return 0;
785
786 /* Error handling */
787 event_id_error:
788 update_error:
789 seek_error:
790 g_warning("An error occured in metadata tracefile parsing");
791 return err;
792 }
793
794 /*
795 * Open a trace and return its LttTrace handle.
796 *
797 * pathname must be the directory of the trace
798 */
799
800 LttTrace *ltt_trace_open(const gchar *pathname)
801 {
802 gchar abs_path[PATH_MAX];
803 LttTrace * t;
804 LttTracefile *tf;
805 GArray *group;
806 unsigned int i;
807 int ret;
808 ltt_subbuffer_header_t *header;
809 DIR *dir;
810 struct dirent *entry;
811 struct stat stat_buf;
812 gchar path[PATH_MAX];
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
820 g_datalist_init(&t->tracefiles);
821
822 /* Test to see if it looks like a trace */
823 dir = opendir(abs_path);
824 if(dir == NULL) {
825 perror(abs_path);
826 goto open_error;
827 }
828 while((entry = readdir(dir)) != NULL) {
829 strcpy(path, abs_path);
830 strcat(path, "/");
831 strcat(path, entry->d_name);
832 ret = stat(path, &stat_buf);
833 if(ret == -1) {
834 perror(path);
835 continue;
836 }
837 }
838 closedir(dir);
839
840 /* Open all the tracefiles */
841 t->start_freq= 0;
842 if(open_tracefiles(t, abs_path, "")) {
843 g_warning("Error opening tracefile %s", abs_path);
844 goto find_error;
845 }
846
847 /* Parse each trace metadata_N files : get runtime fac. info */
848 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_METADATA);
849 if(group == NULL) {
850 g_warning("Trace %s has no metadata tracefile", abs_path);
851 goto find_error;
852 }
853
854 /*
855 * Get the trace information for the metadata_0 tracefile.
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 */
861 g_assert(group->len > 0);
862 tf = &g_array_index (group, LttTracefile, 0);
863 header = (ltt_subbuffer_header_t *)tf->buffer.head;
864 ret = parse_trace_header(header, tf, t);
865 g_assert(!ret);
866
867 t->num_cpu = group->len;
868 t->drift = 1.;
869 t->offset = 0.;
870
871 //ret = allocate_marker_data(t);
872 //if (ret)
873 // g_error("Error in allocating marker data");
874
875 for(i=0; i<group->len; i++) {
876 tf = &g_array_index (group, LttTracefile, i);
877 if (tf->cpu_online)
878 if(ltt_process_metadata_tracefile(tf))
879 goto find_error;
880 // goto metadata_error;
881 }
882
883 return t;
884
885 /* Error handling */
886 //metadata_error:
887 // destroy_marker_data(t);
888 find_error:
889 g_datalist_clear(&t->tracefiles);
890 open_error:
891 g_free(t);
892 alloc_error:
893 return NULL;
894
895 }
896
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 *
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.
904 */
905 LttTrace *ltt_trace_copy(LttTrace *self)
906 {
907 return ltt_trace_open(g_quark_to_string(self->pathname));
908 }
909
910 /*
911 * Close a trace
912 */
913
914 void ltt_trace_close(LttTrace *t)
915 {
916 g_datalist_clear(&t->tracefiles);
917 g_free(t);
918 }
919
920
921 /*****************************************************************************
922 * Get the start time and end time of the trace
923 ****************************************************************************/
924
925 void ltt_tracefile_time_span_get(LttTracefile *tf,
926 LttTime *start, LttTime *end)
927 {
928 int err;
929
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;
943
944 g_assert(end->tv_sec <= G_MAXUINT);
945 }
946
947 struct tracefile_time_span_get_args {
948 LttTrace *t;
949 LttTime *start;
950 LttTime *end;
951 };
952
953 static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
954 {
955 struct tracefile_time_span_get_args *args =
956 (struct tracefile_time_span_get_args*)user_data;
957
958 GArray *group = (GArray *)data;
959 unsigned int i;
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 }
972 }
973
974 /* return the start and end time of a trace */
975
976 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
977 {
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 };
981
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
987 }
988
989
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
994 * event or if after, return ERANGE.
995 *
996 * If the time parameter is before the first event, we have to seek specially to
997 * there.
998 *
999 * If the time is after the end of the trace, return ERANGE.
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
1014 * ERANGE : time if after the last event of the trace
1015 * otherwise : this is an error.
1016 *
1017 * */
1018
1019 int 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;
1030 }
1031
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);
1036 if(ret == ERANGE) goto range;
1037 else if (ret) goto fail;
1038 goto found; /* There is either no event in the trace or the event points
1039 to the first event in the trace */
1040 }
1041
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;
1046 }
1047
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;
1051 }
1052
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;
1064 }
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);
1074 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1075 else if(ret) goto fail;
1076
1077 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1078 goto found;
1079 }
1080
1081 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
1082 /* go to lower part */
1083 high = block_num - 1;
1084 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1085 /* go to higher part */
1086 low = block_num + 1;
1087 } else {/* The event is right in the buffer!
1088 (or in the next buffer first event) */
1089 while(1) {
1090 ret = ltt_tracefile_read(tf);
1091 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1092 else if(ret) goto fail;
1093
1094 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1095 break;
1096 }
1097 goto found;
1098 }
1099 }
1100
1101 found:
1102 return 0;
1103 range:
1104 return ERANGE;
1105
1106 /* Error handling */
1107 fail:
1108 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1109 g_quark_to_string(tf->name));
1110 return EPERM;
1111 }
1112
1113 /* Seek to a position indicated by an LttEventPosition
1114 */
1115
1116 int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep)
1117 {
1118 int err;
1119
1120 if(ep->tracefile != tf) {
1121 goto fail;
1122 }
1123
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;
1131
1132 /* Put back the event real tsc */
1133 tf->event.tsc = ep->tsc;
1134 tf->buffer.tsc = ep->tsc;
1135
1136 err = ltt_tracefile_read_update_event(tf);
1137 if(err) goto fail;
1138
1139 /* deactivate this, as it does nothing for now
1140 err = ltt_tracefile_read_op(tf);
1141 if(err) goto fail;
1142 */
1143
1144 return 0;
1145
1146 fail:
1147 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1148 g_quark_to_string(tf->name));
1149 return 1;
1150 }
1151
1152 /*
1153 * Convert a value in "TSC scale" to a value in nanoseconds
1154 */
1155 guint64 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
1160 /* Given a TSC value, return the LttTime (seconds,nanoseconds) it
1161 * corresponds to.
1162 */
1163 LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
1164 {
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));
1168 }
1169
1170 /* Calculate the real event time based on the buffer boundaries */
1171 LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1172 {
1173 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
1174 }
1175
1176
1177 /* Get the current event of the tracefile : valid until the next read */
1178 LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1179 {
1180 return &tf->event;
1181 }
1182
1183
1184
1185 /*****************************************************************************
1186 *Function name
1187 * ltt_tracefile_read : Read the next event in the tracefile
1188 *Input params
1189 * t : tracefile
1190 *Return value
1191 *
1192 * Returns 0 if an event can be used in tf->event.
1193 * Returns ERANGE on end of trace. The event in tf->event still can be used
1194 * (if the last block was not empty).
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.
1205 ****************************************************************************/
1206
1207 int ltt_tracefile_read(LttTracefile *tf)
1208 {
1209 int err;
1210
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;
1215
1216 /* deactivate this, as it does nothing for now
1217 err = ltt_tracefile_read_op(tf);
1218 if(err) return err;
1219 */
1220
1221 return 0;
1222 }
1223
1224 int 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;
1233 }
1234
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 }
1249
1250 return 0;
1251 }
1252
1253 /* do an operation when reading a new event */
1254
1255 /* This function does nothing for now */
1256 #if 0
1257 int ltt_tracefile_read_op(LttTracefile *tf)
1258 {
1259 LttEvent *event;
1260
1261 event = &tf->event;
1262
1263 /* do event specific operation */
1264
1265 /* nothing */
1266
1267 return 0;
1268 }
1269 #endif
1270
1271 static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1272 {
1273 unsigned int offset = 0;
1274 int i, j;
1275
1276 g_printf("Event header (tracefile %s offset %" PRIx64 "):\n",
1277 g_quark_to_string(ev->tracefile->long_name),
1278 (uint64_t)ev->tracefile->buffer.offset +
1279 (long)start_pos - (long)ev->tracefile->buffer.head);
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",
1290 ((char*)start_pos)[offset + ((i * 4) + j)]);
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
1303
1304 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1305 * event specific operation. */
1306 int ltt_tracefile_read_update_event(LttTracefile *tf)
1307 {
1308 void * pos;
1309 LttEvent *event;
1310 void *pos_aligned;
1311 guint16 packed_evid; /* event id reader from the 5 bits in header */
1312
1313 event = &tf->event;
1314 pos = tf->buffer.head + event->offset;
1315
1316 /* Read event header */
1317
1318 /* Align the head */
1319 pos += ltt_align((size_t)pos, sizeof(guint32), tf->alignment);
1320 pos_aligned = pos;
1321
1322 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf), pos);
1323 event->event_id = packed_evid = event->timestamp >> tf->tscbits;
1324 event->timestamp = event->timestamp & tf->tsc_mask;
1325 pos += sizeof(guint32);
1326
1327 switch (packed_evid) {
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);
1336 }
1337 pos += ltt_align((size_t)pos, sizeof(guint64), tf->alignment);
1338 tf->buffer.tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1339 pos += sizeof(guint64);
1340 break;
1341 case 30: /* LTT_RFLAG_ID_SIZE */
1342 event->event_id = ltt_get_uint16(LTT_GET_BO(tf), pos);
1343 pos += sizeof(guint16);
1344 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1345 pos += sizeof(guint16);
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
1361 if (likely(packed_evid != 29)) {
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;
1370 }
1371 event->tsc = tf->buffer.tsc;
1372
1373 event->event_time = ltt_interpolate_time(tf, event);
1374
1375 if (a_event_debug)
1376 print_debug_event_header(event, pos_aligned, pos);
1377
1378 event->data = pos;
1379
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. */
1385 ltt_update_event_size(tf);
1386
1387 return 0;
1388 }
1389
1390
1391 /****************************************************************************
1392 *Function name
1393 * map_block : map a block from the file
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
1403 static gint map_block(LttTracefile * tf, guint block_num)
1404 {
1405 int page_size = getpagesize();
1406 ltt_subbuffer_header_t *header;
1407 uint64_t offset;
1408 uint32_t size;
1409 int ret;
1410
1411 g_assert(block_num < tf->num_blocks);
1412
1413 if(tf->buffer.head != NULL) {
1414 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buffer.size))) {
1415 g_warning("unmap size : %u\n",
1416 PAGE_ALIGN(tf->buffer.size));
1417 perror("munmap error");
1418 g_assert(0);
1419 }
1420 }
1421
1422 ret = get_block_offset_size(tf, block_num, &offset, &size);
1423 g_assert(!ret);
1424
1425 g_debug("Map block %u, offset %llu, size %u\n", block_num,
1426 (unsigned long long)offset, (unsigned int)size);
1427
1428 /* Multiple of pages aligned head */
1429 tf->buffer.head = mmap(0, (size_t)size, PROT_READ, MAP_PRIVATE,
1430 tf->fd, (off_t)offset);
1431
1432 if(tf->buffer.head == MAP_FAILED) {
1433 perror("Error in allocating memory for buffer of tracefile");
1434 g_assert(0);
1435 goto map_error;
1436 }
1437 g_assert( ( (gulong)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
1438
1439 tf->buffer.index = block_num;
1440
1441 header = (ltt_subbuffer_header_t *)tf->buffer.head;
1442
1443 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1444 &header->cycle_count_begin);
1445 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1446 &header->cycle_count_end);
1447 tf->buffer.offset = offset;
1448 tf->buffer.size = ltt_get_uint32(LTT_GET_BO(tf),
1449 &header->sb_size);
1450 tf->buffer.data_size = ltt_get_uint32(LTT_GET_BO(tf),
1451 &header->data_size);
1452 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1453 tf->event.tsc = tf->buffer.tsc;
1454 tf->buffer.freq = tf->buffer.begin.freq;
1455
1456 g_assert(size == tf->buffer.size);
1457 g_assert(tf->buffer.data_size <= tf->buffer.size);
1458
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
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;
1473 tf->event.offset = 0;
1474
1475 if (header->events_lost) {
1476 g_warning("%d events lost so far in tracefile %s at block %u",
1477 (guint)header->events_lost,
1478 g_quark_to_string(tf->long_name),
1479 block_num);
1480 tf->events_lost = header->events_lost;
1481 }
1482 if (header->subbuf_corrupt) {
1483 g_warning("%d subbuffer(s) corrupted so far in tracefile %s at block %u",
1484 (guint)header->subbuf_corrupt,
1485 g_quark_to_string(tf->long_name),
1486 block_num);
1487 tf->subbuf_corrupt = header->subbuf_corrupt;
1488 }
1489
1490 return 0;
1491
1492 map_error:
1493 return -errno;
1494 }
1495
1496 static 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
1504 g_printf("Event data (tracefile %s offset %" PRIx64 "):\n",
1505 g_quark_to_string(ev->tracefile->long_name),
1506 (uint64_t)ev->tracefile->buffer.offset
1507 + (long)ev->data - (long)ev->tracefile->buffer.head);
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
1544 /* It will update the fields offsets too */
1545 void ltt_update_event_size(LttTracefile *tf)
1546 {
1547 off_t size = 0;
1548 struct marker_info *info;
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 }
1571 }
1572
1573 info = marker_get_info_from_id(tf->mdata, tf->event.event_id);
1574
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
1579 * metadata tracefile when the infos about these markers do not exist yet.
1580 */
1581 if (likely(info && info->fields)) {
1582 /* alignment */
1583 tf->event.data += ltt_align((off_t)(unsigned long)tf->event.data,
1584 info->largest_align,
1585 info->alignment);
1586 /* size, dynamically computed */
1587 if (info->size != -1)
1588 size = info->size;
1589 else
1590 size = marker_update_fields_offsets(marker_get_info_from_id(tf->mdata,
1591 tf->event.event_id), tf->event.data);
1592 }
1593
1594 tf->event.data_size = size;
1595
1596 /* Check consistency between kernel and LTTV structure sizes */
1597 if(tf->event.event_size == G_MAXUINT) {
1598 /* Event size too big to fit in the event size field */
1599 tf->event.event_size = tf->event.data_size;
1600 }
1601
1602 if (a_event_debug)
1603 print_debug_event_data(&tf->event);
1604
1605 if (tf->event.data_size != tf->event.event_size) {
1606 struct marker_info *info = marker_get_info_from_id(tf->mdata,
1607 tf->event.event_id);
1608 if (!info)
1609 g_error("Undescribed event %hhu in channel %s", tf->event.event_id,
1610 g_quark_to_string(tf->name));
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);
1614 exit(-1);
1615 }
1616 }
1617
1618
1619 /* Take the tf current event offset and use the event id to figure out where is
1620 * the next event offset.
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 */
1630 static int ltt_seek_next_event(LttTracefile *tf)
1631 {
1632 int ret = 0;
1633 void *pos;
1634
1635 /* seek over the buffer header if we are at the buffer start */
1636 if(tf->event.offset == 0) {
1637 tf->event.offset += tf->buffer_header_size;
1638
1639 if(tf->event.offset == tf->buffer.data_size) {
1640 ret = ERANGE;
1641 }
1642 goto found;
1643 }
1644
1645 pos = tf->event.data;
1646
1647 if(tf->event.data_size < 0) goto error;
1648
1649 pos += (size_t)tf->event.data_size;
1650
1651 tf->event.offset = pos - tf->buffer.head;
1652
1653 if(tf->event.offset == tf->buffer.data_size) {
1654 ret = ERANGE;
1655 goto found;
1656 }
1657 g_assert(tf->event.offset < tf->buffer.data_size);
1658
1659 found:
1660 return ret;
1661
1662 error:
1663 g_error("Error in ltt_seek_next_event for tracefile %s",
1664 g_quark_to_string(tf->name));
1665 return ENOPROTOOPT;
1666 }
1667
1668
1669 /*****************************************************************************
1670 *Function name
1671 * ltt_get_int : get an integer number
1672 *Input params
1673 * reverse_byte_order: must we reverse the byte order ?
1674 * size : the size of the integer
1675 * ptr : the data pointer
1676 *Return value
1677 * gint64 : a 64 bits integer
1678 ****************************************************************************/
1679
1680 gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
1681 {
1682 gint64 val;
1683
1684 switch(size) {
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);
1691 break;
1692 }
1693
1694 return val;
1695 }
1696
1697 /*****************************************************************************
1698 *Function name
1699 * ltt_get_uint : get an unsigned integer number
1700 *Input params
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
1706 ****************************************************************************/
1707
1708 guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
1709 {
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;
1724 }
1725
1726
1727 /* get the node name of the system */
1728
1729 char * 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
1737 char * 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
1745 char * ltt_trace_system_description_description (LttSystemDescription * s)
1746 {
1747 return s->description;
1748 }
1749
1750
1751 /* get the NTP corrected start time of the trace */
1752 LttTime ltt_trace_start_time(LttTrace *t)
1753 {
1754 return t->start_time;
1755 }
1756
1757 /* get the monotonic start time of the trace */
1758 LttTime ltt_trace_start_time_monotonic(LttTrace *t)
1759 {
1760 return t->start_time_from_tsc;
1761 }
1762
1763 static __attribute__ ((__unused__)) LttTracefile *ltt_tracefile_new()
1764 {
1765 LttTracefile *tf;
1766 tf = g_new(LttTracefile, 1);
1767 tf->event.tracefile = tf;
1768 return tf;
1769 }
1770
1771 static __attribute__ ((__unused__)) void ltt_tracefile_destroy(LttTracefile *tf)
1772 {
1773 g_free(tf);
1774 }
1775
1776 static __attribute__ ((__unused__)) void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
1777 {
1778 *dest = *src;
1779 }
1780
1781 /* Before library loading... */
1782
1783 static __attribute__((constructor)) void init(void)
1784 {
1785 LTT_TRACEFILE_NAME_METADATA = g_quark_from_string("metadata");
1786 }
1787
1788 /*****************************************************************************
1789 *Function name
1790 * ltt_tracefile_open_header : based on ltt_tracefile_open but it stops
1791 * when it gets the header
1792 *Input params
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 ****************************************************************************/
1798 static 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();
1803
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 }
1811
1812 /* Get the file's status */
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 }
1817
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 }
1823
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);
1827
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.
1833
1834 header = (ltt_subbuffer_header_t *)tf->buffer.head;
1835
1836 return header;
1837
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
1848 *Input params
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 ****************************************************************************/
1854 int ltt_get_trace_version(const gchar *pathname, struct LttTraceVersion *version_number)
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];
1862
1863 LttTracefile tmp_tf;
1864 LttTrace * t;
1865 ltt_subbuffer_header_t *header;
1866
1867 t = g_new(LttTrace, 1);
1868
1869 get_absolute_pathname(pathname, abs_path);
1870
1871 /* Test to see if it looks like a trace */
1872 dir = opendir(abs_path);
1873
1874 if(dir == NULL) {
1875 perror(abs_path);
1876 goto open_error;
1877 }
1878
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 }
1889
1890 closedir(dir);
1891 dir = opendir(abs_path);
1892
1893 while((entry = readdir(dir)) != NULL) {
1894 if(entry->d_name[0] == '.') continue;
1895 if(g_strcmp0(entry->d_name, "metadata_0") != 0) continue;
1896
1897 strcpy(path, abs_path);
1898 strcat(path, "/");
1899 strcat(path, entry->d_name);
1900 if(ret == -1) {
1901 perror(path);
1902 continue;
1903 }
1904
1905 header = ltt_tracefile_open_header(path, &tmp_tf);
1906
1907 if(header == NULL) {
1908 g_info("Error getting the header %s", path);
1909 continue; /* error opening the tracefile : bad magic number ? */
1910 }
1911
1912 version_number->ltt_major_version = header->major_version;
1913 version_number->ltt_minor_version = header->minor_version;
1914 }
1915
1916 return 0;
1917
1918 open_error:
1919 g_free(t);
1920 return -1;
1921 }
This page took 0.120646 seconds and 4 git commands to generate.