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