fixes coming from the powerpc port
[lttv.git] / ltt / branches / poly / 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
38 // For realpath
39 #include <limits.h>
40 #include <stdlib.h>
41
42
43 #include "parser.h"
44 #include <ltt/ltt.h>
45 #include "ltt-private.h"
46 #include <ltt/trace.h>
47 #include <ltt/facility.h>
48 #include <ltt/event.h>
49 #include <ltt/type.h>
50 #include <ltt/ltt-types.h>
51
52
53 /* Facility names used in this file */
54
55 GQuark LTT_FACILITY_NAME_HEARTBEAT,
56 LTT_EVENT_NAME_HEARTBEAT;
57 GQuark LTT_TRACEFILE_NAME_FACILITIES;
58
59 #ifndef g_open
60 #define g_open open
61 #endif
62
63
64 #define __UNUSED__ __attribute__((__unused__))
65
66 #define g_info(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_INFO, format)
67
68 #ifndef g_debug
69 #define g_debug(format...) g_log (G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, format)
70 #endif
71
72 #define g_close close
73
74 /* Those macros must be called from within a function where page_size is a known
75 * variable */
76 #define PAGE_MASK (~(page_size-1))
77 #define PAGE_ALIGN(addr) (((addr)+page_size-1)&PAGE_MASK)
78
79 /* set the offset of the fields belonging to the event,
80 need the information of the archecture */
81 //void set_fields_offsets(LttTracefile *tf, LttEventType *event_type);
82 //size_t get_fields_offsets(LttTracefile *tf, LttEventType *event_type, void *data);
83
84 /* get the size of the field type according to
85 * The facility size information. */
86 #if 0
87 static inline void preset_field_type_size(LttTracefile *tf,
88 LttEventType *event_type,
89 off_t offset_root, off_t offset_parent,
90 enum field_status *fixed_root, enum field_status *fixed_parent,
91 LttField *field);
92 #endif //0
93
94 /* map a fixed size or a block information from the file (fd) */
95 static gint map_block(LttTracefile * tf, guint block_num);
96
97 /* calculate nsec per cycles for current block */
98 #if 0
99 static guint32 calc_nsecs_per_cycle(LttTracefile * t);
100 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles);
101 #endif //0
102
103 /* go to the next event */
104 static int ltt_seek_next_event(LttTracefile *tf);
105
106 void ltt_update_event_size(LttTracefile *tf);
107
108
109 void precompute_offsets(LttFacility *fac, LttEventType *event);
110
111 #if 0
112 /* Functions to parse system.xml file (using glib xml parser) */
113 static void parser_start_element (GMarkupParseContext __UNUSED__ *context,
114 const gchar *element_name,
115 const gchar **attribute_names,
116 const gchar **attribute_values,
117 gpointer user_data,
118 GError **error)
119 {
120 int i=0;
121 LttSystemDescription* des = (LttSystemDescription* )user_data;
122 if(strcmp("system", element_name)){
123 *error = g_error_new(G_MARKUP_ERROR,
124 G_LOG_LEVEL_WARNING,
125 "This is not system.xml file");
126 return;
127 }
128
129 while(attribute_names[i]){
130 if(strcmp("node_name", attribute_names[i])==0){
131 des->node_name = g_strdup(attribute_values[i]);
132 }else if(strcmp("domainname", attribute_names[i])==0){
133 des->domain_name = g_strdup(attribute_values[i]);
134 }else if(strcmp("cpu", attribute_names[i])==0){
135 des->nb_cpu = atoi(attribute_values[i]);
136 }else if(strcmp("arch_size", attribute_names[i])==0){
137 if(strcmp(attribute_values[i],"LP32") == 0) des->size = LTT_LP32;
138 else if(strcmp(attribute_values[i],"ILP32") == 0) des->size = LTT_ILP32;
139 else if(strcmp(attribute_values[i],"LP64") == 0) des->size = LTT_LP64;
140 else if(strcmp(attribute_values[i],"ILP64") == 0) des->size = LTT_ILP64;
141 else if(strcmp(attribute_values[i],"UNKNOWN") == 0) des->size = LTT_UNKNOWN;
142 }else if(strcmp("endian", attribute_names[i])==0){
143 if(strcmp(attribute_values[i],"LITTLE_ENDIAN") == 0)
144 des->endian = LTT_LITTLE_ENDIAN;
145 else if(strcmp(attribute_values[i],"BIG_ENDIAN") == 0)
146 des->endian = LTT_BIG_ENDIAN;
147 }else if(strcmp("kernel_name", attribute_names[i])==0){
148 des->kernel_name = g_strdup(attribute_values[i]);
149 }else if(strcmp("kernel_release", attribute_names[i])==0){
150 des->kernel_release = g_strdup(attribute_values[i]);
151 }else if(strcmp("kernel_version", attribute_names[i])==0){
152 des->kernel_version = g_strdup(attribute_values[i]);
153 }else if(strcmp("machine", attribute_names[i])==0){
154 des->machine = g_strdup(attribute_values[i]);
155 }else if(strcmp("processor", attribute_names[i])==0){
156 des->processor = g_strdup(attribute_values[i]);
157 }else if(strcmp("hardware_platform", attribute_names[i])==0){
158 des->hardware_platform = g_strdup(attribute_values[i]);
159 }else if(strcmp("operating_system", attribute_names[i])==0){
160 des->operating_system = g_strdup(attribute_values[i]);
161 }else if(strcmp("ltt_major_version", attribute_names[i])==0){
162 des->ltt_major_version = atoi(attribute_values[i]);
163 }else if(strcmp("ltt_minor_version", attribute_names[i])==0){
164 des->ltt_minor_version = atoi(attribute_values[i]);
165 }else if(strcmp("ltt_block_size", attribute_names[i])==0){
166 des->ltt_block_size = atoi(attribute_values[i]);
167 }else{
168 *error = g_error_new(G_MARKUP_ERROR,
169 G_LOG_LEVEL_WARNING,
170 "Not a valid attribute");
171 return;
172 }
173 i++;
174 }
175 }
176
177 static void parser_characters (GMarkupParseContext __UNUSED__ *context,
178 const gchar *text,
179 gsize __UNUSED__ text_len,
180 gpointer user_data,
181 GError __UNUSED__ **error)
182 {
183 LttSystemDescription* des = (LttSystemDescription* )user_data;
184 des->description = g_strdup(text);
185 }
186 #endif //0
187
188
189 LttFacility *ltt_trace_get_facility_by_num(LttTrace *t,
190 guint num)
191 {
192 g_assert(num < t->facilities_by_num->len);
193
194 return &g_array_index(t->facilities_by_num, LttFacility, num);
195
196 }
197
198 guint ltt_trace_get_num_cpu(LttTrace *t)
199 {
200 return t->num_cpu;
201 }
202
203
204 /* trace can be NULL
205 *
206 * Return value : 0 success, 1 bad tracefile
207 */
208 int parse_trace_header(void *header, LttTracefile *tf, LttTrace *t)
209 {
210 guint32 *magic_number = (guint32*)header;
211 struct ltt_trace_header_any *any = (struct ltt_trace_header_any *)header;
212
213 if(*magic_number == LTT_MAGIC_NUMBER)
214 tf->reverse_bo = 0;
215 else if(*magic_number == LTT_REV_MAGIC_NUMBER)
216 tf->reverse_bo = 1;
217 else /* invalid magic number, bad tracefile ! */
218 return 1;
219
220 /* Get float byte order : might be different from int byte order
221 * (or is set to 0 if the trace has no float (kernel trace)) */
222 tf->float_word_order = any->float_word_order;
223 tf->has_alignment = any->has_alignment;
224
225 if(t) {
226 t->arch_type = ltt_get_uint32(LTT_GET_BO(tf),
227 &any->arch_type);
228 t->arch_variant = ltt_get_uint32(LTT_GET_BO(tf),
229 &any->arch_variant);
230 t->arch_size = any->arch_size;
231 t->ltt_major_version = any->major_version;
232 t->ltt_minor_version = any->minor_version;
233 t->flight_recorder = any->flight_recorder;
234 t->has_heartbeat = any->has_heartbeat;
235 }
236
237
238 switch(any->major_version) {
239
240 case 0:
241 switch(any->minor_version) {
242 case 3:
243 {
244 tf->buffer_header_size =
245 sizeof(struct ltt_block_start_header)
246 + sizeof(struct ltt_trace_header_0_3);
247 g_warning("Unsupported trace version : %hhu.%hhu",
248 any->major_version, any->minor_version);
249 return 1;
250 }
251 break;
252 case 7:
253 {
254 struct ltt_trace_header_0_7 *vheader =
255 (struct ltt_trace_header_0_7 *)header;
256 tf->buffer_header_size =
257 sizeof(struct ltt_block_start_header)
258 + sizeof(struct ltt_trace_header_0_7);
259 if(t) {
260 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
261 &vheader->start_freq);
262 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
263 &vheader->freq_scale);
264 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
265 &vheader->start_tsc);
266 t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
267 &vheader->start_monotonic);
268 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
269 &vheader->start_time_sec);
270 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
271 &vheader->start_time_usec);
272 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
273
274 t->start_time_from_tsc = ltt_time_from_uint64(
275 (double)t->start_tsc * 1000000.0 / (double)t->start_freq);
276 }
277 }
278 break;
279 default:
280 g_warning("Unsupported trace version : %hhu.%hhu",
281 any->major_version, any->minor_version);
282 return 1;
283 }
284 break;
285
286 default:
287 g_warning("Unsupported trace version : %hhu.%hhu",
288 any->major_version, any->minor_version);
289 return 1;
290 }
291
292
293 return 0;
294 }
295
296
297
298 /*****************************************************************************
299 *Function name
300 * ltt_tracefile_open : open a trace file, construct a LttTracefile
301 *Input params
302 * t : the trace containing the tracefile
303 * fileName : path name of the trace file
304 * tf : the tracefile structure
305 *Return value
306 * : 0 for success, -1 otherwise.
307 ****************************************************************************/
308
309 gint ltt_tracefile_open(LttTrace *t, gchar * fileName, LttTracefile *tf)
310 {
311 struct stat lTDFStat; /* Trace data file status */
312 struct ltt_block_start_header *header;
313 int page_size = getpagesize();
314
315 //open the file
316 tf->long_name = g_quark_from_string(fileName);
317 tf->trace = t;
318 tf->fd = open(fileName, O_RDONLY);
319 if(tf->fd < 0){
320 g_warning("Unable to open input data file %s\n", fileName);
321 goto end;
322 }
323
324 // Get the file's status
325 if(fstat(tf->fd, &lTDFStat) < 0){
326 g_warning("Unable to get the status of the input data file %s\n", fileName);
327 goto close_file;
328 }
329
330 // Is the file large enough to contain a trace
331 if(lTDFStat.st_size <
332 (off_t)(sizeof(struct ltt_block_start_header)
333 + sizeof(struct ltt_trace_header_any))){
334 g_print("The input data file %s does not contain a trace\n", fileName);
335 goto close_file;
336 }
337
338 /* Temporarily map the buffer start header to get trace information */
339 /* Multiple of pages aligned head */
340 tf->buffer.head = mmap(0,
341 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
342 + sizeof(struct ltt_trace_header_any)), PROT_READ,
343 MAP_PRIVATE, tf->fd, 0);
344 if(tf->buffer.head == MAP_FAILED) {
345 perror("Error in allocating memory for buffer of tracefile");
346 goto close_file;
347 }
348 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
349
350 header = (struct ltt_block_start_header*)tf->buffer.head;
351
352 if(parse_trace_header(header->trace, tf, NULL)) {
353 g_warning("parse_trace_header error");
354 goto unmap_file;
355 }
356
357 //store the size of the file
358 tf->file_size = lTDFStat.st_size;
359 tf->buf_size = ltt_get_uint32(LTT_GET_BO(tf), &header->buf_size);
360 tf->num_blocks = tf->file_size / tf->buf_size;
361
362 if(munmap(tf->buffer.head,
363 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
364 + sizeof(struct ltt_trace_header_any)))) {
365 g_warning("unmap size : %u\n",
366 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
367 + sizeof(struct ltt_trace_header_any)));
368 perror("munmap error");
369 g_assert(0);
370 }
371 tf->buffer.head = NULL;
372
373 //read the first block
374 if(map_block(tf,0)) {
375 perror("Cannot map block for tracefile");
376 goto close_file;
377 }
378
379 return 0;
380
381 /* Error */
382 unmap_file:
383 if(munmap(tf->buffer.head,
384 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
385 + sizeof(struct ltt_trace_header_any)))) {
386 g_warning("unmap size : %u\n",
387 PAGE_ALIGN(sizeof(struct ltt_block_start_header)
388 + sizeof(struct ltt_trace_header_any)));
389 perror("munmap error");
390 g_assert(0);
391 }
392 close_file:
393 close(tf->fd);
394 end:
395 return -1;
396 }
397
398 LttTrace *ltt_tracefile_get_trace(LttTracefile *tf)
399 {
400 return tf->trace;
401 }
402
403 #if 0
404 /*****************************************************************************
405 *Open control and per cpu tracefiles
406 ****************************************************************************/
407
408 void ltt_tracefile_open_cpu(LttTrace *t, gchar * tracefile_name)
409 {
410 LttTracefile * tf;
411 tf = ltt_tracefile_open(t,tracefile_name);
412 if(!tf) return;
413 t->per_cpu_tracefile_number++;
414 g_ptr_array_add(t->per_cpu_tracefiles, tf);
415 }
416
417 gint ltt_tracefile_open_control(LttTrace *t, gchar * control_name)
418 {
419 LttTracefile * tf;
420 LttEvent ev;
421 LttFacility * f;
422 void * pos;
423 FacilityLoad fLoad;
424 unsigned int i;
425
426 tf = ltt_tracefile_open(t,control_name);
427 if(!tf) {
428 g_warning("ltt_tracefile_open_control : bad file descriptor");
429 return -1;
430 }
431 t->control_tracefile_number++;
432 g_ptr_array_add(t->control_tracefiles,tf);
433
434 //parse facilities tracefile to get base_id
435 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
436 while(1){
437 if(!ltt_tracefile_read(tf,&ev)) return 0; // end of file
438
439 if(ev.event_id == TRACE_FACILITY_LOAD){
440 pos = ev.data;
441 fLoad.name = (gchar*)pos;
442 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
443 fLoad.base_code = *(guint32 *)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
444
445 for(i=0;i<t->facility_number;i++){
446 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
447 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
448 f->base_id = fLoad.base_code;
449 break;
450 }
451 }
452 if(i==t->facility_number) {
453 g_warning("Facility: %s, checksum: %u is not found",
454 fLoad.name,(unsigned int)fLoad.checksum);
455 return -1;
456 }
457 }else if(ev.event_id == TRACE_BLOCK_START){
458 continue;
459 }else if(ev.event_id == TRACE_BLOCK_END){
460 break;
461 }else {
462 g_warning("Not valid facilities trace file");
463 return -1;
464 }
465 }
466 }
467 return 0;
468 }
469 #endif //0
470
471 /*****************************************************************************
472 *Function name
473 * ltt_tracefile_close: close a trace file,
474 *Input params
475 * t : tracefile which will be closed
476 ****************************************************************************/
477
478 void ltt_tracefile_close(LttTracefile *t)
479 {
480 int page_size = getpagesize();
481
482 if(t->buffer.head != NULL)
483 if(munmap(t->buffer.head, PAGE_ALIGN(t->buf_size))) {
484 g_warning("unmap size : %u\n",
485 PAGE_ALIGN(t->buf_size));
486 perror("munmap error");
487 g_assert(0);
488 }
489
490 close(t->fd);
491 }
492
493
494 /*****************************************************************************
495 *Get system information
496 ****************************************************************************/
497 #if 0
498 gint getSystemInfo(LttSystemDescription* des, gchar * pathname)
499 {
500 int fd;
501 GIOChannel *iochan;
502 gchar *buf = NULL;
503 gsize length;
504
505 GMarkupParseContext * context;
506 GError * error = NULL;
507 GMarkupParser markup_parser =
508 {
509 parser_start_element,
510 NULL,
511 parser_characters,
512 NULL, /* passthrough */
513 NULL /* error */
514 };
515
516 fd = g_open(pathname, O_RDONLY, 0);
517 if(fd == -1){
518 g_warning("Can not open file : %s\n", pathname);
519 return -1;
520 }
521
522 iochan = g_io_channel_unix_new(fd);
523
524 context = g_markup_parse_context_new(&markup_parser, 0, des,NULL);
525
526 //while(fgets(buf,DIR_NAME_SIZE, fp) != NULL){
527 while(g_io_channel_read_line(iochan, &buf, &length, NULL, &error)
528 != G_IO_STATUS_EOF) {
529
530 if(error != NULL) {
531 g_warning("Can not read xml file: \n%s\n", error->message);
532 g_error_free(error);
533 }
534 if(!g_markup_parse_context_parse(context, buf, length, &error)){
535 if(error != NULL) {
536 g_warning("Can not parse xml file: \n%s\n", error->message);
537 g_error_free(error);
538 }
539 g_markup_parse_context_free(context);
540
541 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
542 if(error != NULL) {
543 g_warning("Can not close file: \n%s\n", error->message);
544 g_error_free(error);
545 }
546
547 close(fd);
548 return -1;
549 }
550 }
551 g_markup_parse_context_free(context);
552
553 g_io_channel_shutdown(iochan, FALSE, &error); /* No flush */
554 if(error != NULL) {
555 g_warning("Can not close file: \n%s\n", error->message);
556 g_error_free(error);
557 }
558
559 g_close(fd);
560
561 g_free(buf);
562 return 0;
563 }
564 #endif //0
565
566 /*****************************************************************************
567 *The following functions get facility/tracefile information
568 ****************************************************************************/
569 #if 0
570 gint getFacilityInfo(LttTrace *t, gchar* eventdefs)
571 {
572 GDir * dir;
573 const gchar * name;
574 unsigned int i,j;
575 LttFacility * f;
576 LttEventType * et;
577 gchar fullname[DIR_NAME_SIZE];
578 GError * error = NULL;
579
580 dir = g_dir_open(eventdefs, 0, &error);
581
582 if(error != NULL) {
583 g_warning("Can not open directory: %s, %s\n", eventdefs, error->message);
584 g_error_free(error);
585 return -1;
586 }
587
588 while((name = g_dir_read_name(dir)) != NULL){
589 if(!g_pattern_match_simple("*.xml", name)) continue;
590 strcpy(fullname,eventdefs);
591 strcat(fullname,name);
592 ltt_facility_open(t,fullname);
593 }
594 g_dir_close(dir);
595
596 for(j=0;j<t->facility_number;j++){
597 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
598 for(i=0; i<f->event_number; i++){
599 et = f->events[i];
600 setFieldsOffset(NULL, et, NULL, t);
601 }
602 }
603 return 0;
604 }
605 #endif //0
606
607 /*****************************************************************************
608 *A trace is specified as a pathname to the directory containing all the
609 *associated data (control tracefiles, per cpu tracefiles, event
610 *descriptions...).
611 *
612 *When a trace is closed, all the associated facilities, types and fields
613 *are released as well.
614 */
615
616
617 /****************************************************************************
618 * get_absolute_pathname
619 *
620 * return the unique pathname in the system
621 *
622 * MD : Fixed this function so it uses realpath, dealing well with
623 * forgotten cases (.. were not used correctly before).
624 *
625 ****************************************************************************/
626 void get_absolute_pathname(const gchar *pathname, gchar * abs_pathname)
627 {
628 abs_pathname[0] = '\0';
629
630 if ( realpath (pathname, abs_pathname) != NULL)
631 return;
632 else
633 {
634 /* error, return the original path unmodified */
635 strcpy(abs_pathname, pathname);
636 return;
637 }
638 return;
639 }
640
641 /* Search for something like : .*_.*
642 *
643 * The left side is the name, the right side is the number.
644 */
645
646 int get_tracefile_name_number(gchar *raw_name,
647 GQuark *name,
648 guint *num,
649 guint *tid,
650 guint *pgid,
651 guint64 *creation)
652 {
653 guint raw_name_len = strlen(raw_name);
654 gchar char_name[PATH_MAX];
655 int i;
656 int underscore_pos;
657 long int cpu_num;
658 gchar *endptr;
659 gchar *tmpptr;
660
661 for(i=raw_name_len-1;i>=0;i--) {
662 if(raw_name[i] == '_') break;
663 }
664 if(i==-1) { /* Either not found or name length is 0 */
665 /* This is a userspace tracefile */
666 strncpy(char_name, raw_name, raw_name_len);
667 *name = g_quark_from_string(char_name);
668 *num = 0; /* unknown cpu */
669 for(i=0;i<raw_name_len;i++) {
670 if(raw_name[i] == '/') {
671 break;
672 }
673 }
674 i++;
675 for(;i<raw_name_len;i++) {
676 if(raw_name[i] == '/') {
677 break;
678 }
679 }
680 i++;
681 for(;i<raw_name_len;i++) {
682 if(raw_name[i] == '-') {
683 break;
684 }
685 }
686 if(i == raw_name_len) return -1;
687 i++;
688 tmpptr = &raw_name[i];
689 for(;i<raw_name_len;i++) {
690 if(raw_name[i] == '.') {
691 raw_name[i] = ' ';
692 break;
693 }
694 }
695 *tid = strtoul(tmpptr, &endptr, 10);
696 if(endptr == tmpptr)
697 return -1; /* No digit */
698 if(*tid == ULONG_MAX)
699 return -1; /* underflow / overflow */
700 i++;
701 tmpptr = &raw_name[i];
702 for(;i<raw_name_len;i++) {
703 if(raw_name[i] == '.') {
704 raw_name[i] = ' ';
705 break;
706 }
707 }
708 *pgid = strtoul(tmpptr, &endptr, 10);
709 if(endptr == tmpptr)
710 return -1; /* No digit */
711 if(*pgid == ULONG_MAX)
712 return -1; /* underflow / overflow */
713 i++;
714 tmpptr = &raw_name[i];
715 *creation = strtoull(tmpptr, &endptr, 10);
716 if(endptr == tmpptr)
717 return -1; /* No digit */
718 if(*creation == G_MAXUINT64)
719 return -1; /* underflow / overflow */
720 } else {
721 underscore_pos = i;
722
723 cpu_num = strtol(raw_name+underscore_pos+1, &endptr, 10);
724
725 if(endptr == raw_name+underscore_pos+1)
726 return -1; /* No digit */
727 if(cpu_num == LONG_MIN || cpu_num == LONG_MAX)
728 return -1; /* underflow / overflow */
729
730 strncpy(char_name, raw_name, underscore_pos);
731 char_name[underscore_pos] = '\0';
732
733 *name = g_quark_from_string(char_name);
734 *num = cpu_num;
735 }
736
737
738 return 0;
739 }
740
741
742 GData **ltt_trace_get_tracefiles_groups(LttTrace *trace)
743 {
744 return &trace->tracefiles;
745 }
746
747
748 void compute_tracefile_group(GQuark key_id,
749 GArray *group,
750 struct compute_tracefile_group_args *args)
751 {
752 int i;
753 LttTracefile *tf;
754
755 for(i=0; i<group->len; i++) {
756 tf = &g_array_index (group, LttTracefile, i);
757 if(tf->cpu_online)
758 args->func(tf, args->func_args);
759 }
760 }
761
762
763 void ltt_tracefile_group_destroy(gpointer data)
764 {
765 GArray *group = (GArray *)data;
766 int i;
767 LttTracefile *tf;
768
769 for(i=0; i<group->len; i++) {
770 tf = &g_array_index (group, LttTracefile, i);
771 if(tf->cpu_online)
772 ltt_tracefile_close(tf);
773 }
774 g_array_free(group, TRUE);
775 }
776
777 gboolean ltt_tracefile_group_has_cpu_online(gpointer data)
778 {
779 GArray *group = (GArray *)data;
780 int i;
781 LttTracefile *tf;
782
783 for(i=0; i<group->len; i++) {
784 tf = &g_array_index (group, LttTracefile, i);
785 if(tf->cpu_online) return 1;
786 }
787 return 0;
788 }
789
790
791 /* Open each tracefile under a specific directory. Put them in a
792 * GData : permits to access them using their tracefile group pathname.
793 * i.e. access control/modules tracefile group by index :
794 * "control/module".
795 *
796 * relative path is the path relative to the trace root
797 * root path is the full path
798 *
799 * A tracefile group is simply an array where all the per cpu tracefiles sits.
800 */
801
802 static int open_tracefiles(LttTrace *trace, gchar *root_path,
803 gchar *relative_path)
804 {
805 DIR *dir = opendir(root_path);
806 struct dirent *entry;
807 struct stat stat_buf;
808 int ret;
809
810 gchar path[PATH_MAX];
811 int path_len;
812 gchar *path_ptr;
813
814 int rel_path_len;
815 gchar rel_path[PATH_MAX];
816 gchar *rel_path_ptr;
817 LttTracefile tmp_tf;
818
819 if(dir == NULL) {
820 perror(root_path);
821 return ENOENT;
822 }
823
824 strncpy(path, root_path, PATH_MAX-1);
825 path_len = strlen(path);
826 path[path_len] = '/';
827 path_len++;
828 path_ptr = path + path_len;
829
830 strncpy(rel_path, relative_path, PATH_MAX-1);
831 rel_path_len = strlen(rel_path);
832 rel_path[rel_path_len] = '/';
833 rel_path_len++;
834 rel_path_ptr = rel_path + rel_path_len;
835
836 while((entry = readdir(dir)) != NULL) {
837
838 if(entry->d_name[0] == '.') continue;
839
840 strncpy(path_ptr, entry->d_name, PATH_MAX - path_len);
841 strncpy(rel_path_ptr, entry->d_name, PATH_MAX - rel_path_len);
842
843 ret = stat(path, &stat_buf);
844 if(ret == -1) {
845 perror(path);
846 continue;
847 }
848
849 g_debug("Tracefile file or directory : %s\n", path);
850
851 if(strcmp(rel_path, "/eventdefs") == 0) continue;
852
853 if(S_ISDIR(stat_buf.st_mode)) {
854
855 g_debug("Entering subdirectory...\n");
856 ret = open_tracefiles(trace, path, rel_path);
857 if(ret < 0) continue;
858 } else if(S_ISREG(stat_buf.st_mode)) {
859 GQuark name;
860 guint num, tid, pgid;
861 guint64 creation;
862 GArray *group;
863 num = tid = pgid = 0;
864 creation = 0;
865 if(get_tracefile_name_number(rel_path, &name, &num, &tid, &pgid, &creation))
866 continue; /* invalid name */
867
868 g_debug("Opening file.\n");
869 if(ltt_tracefile_open(trace, path, &tmp_tf)) {
870 g_info("Error opening tracefile %s", path);
871
872 continue; /* error opening the tracefile : bad magic number ? */
873 }
874
875 g_debug("Tracefile name is %s and number is %u",
876 g_quark_to_string(name), num);
877
878 tmp_tf.cpu_online = 1;
879 tmp_tf.cpu_num = num;
880 tmp_tf.name = name;
881 tmp_tf.tid = tid;
882 tmp_tf.pgid = pgid;
883 tmp_tf.creation = creation;
884
885 group = g_datalist_id_get_data(&trace->tracefiles, name);
886 if(group == NULL) {
887 /* Elements are automatically cleared when the array is allocated.
888 * It makes the cpu_online variable set to 0 : cpu offline, by default.
889 */
890 group = g_array_sized_new (FALSE, TRUE, sizeof(LttTracefile), 10);
891 g_datalist_id_set_data_full(&trace->tracefiles, name,
892 group, ltt_tracefile_group_destroy);
893 }
894
895 /* Add the per cpu tracefile to the named group */
896 unsigned int old_len = group->len;
897 if(num+1 > old_len)
898 group = g_array_set_size(group, num+1);
899 g_array_index (group, LttTracefile, num) = tmp_tf;
900
901 }
902 }
903
904 closedir(dir);
905
906 return 0;
907 }
908
909 /* ltt_get_facility_description
910 *
911 * Opens the file corresponding to the requested facility (identified by fac_id
912 * and checksum).
913 *
914 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
915 *
916 * Returns 0 on success, or 1 on failure.
917 */
918
919 static int ltt_get_facility_description(LttFacility *f,
920 LttTrace *t,
921 LttTracefile *fac_tf)
922 {
923 char desc_file_name[PATH_MAX];
924 const gchar *text;
925 guint textlen;
926 gint err;
927 gint arch_spec;
928 gint fac_name_len;
929
930 text = g_quark_to_string(t->pathname);
931 textlen = strlen(text);
932
933 if(textlen >= PATH_MAX) goto name_error;
934 strcpy(desc_file_name, text);
935
936 text = "/eventdefs/";
937 textlen+=strlen(text);
938 if(textlen >= PATH_MAX) goto name_error;
939 strcat(desc_file_name, text);
940
941 text = g_quark_to_string(f->name);
942 fac_name_len = strlen(text);
943 textlen+=fac_name_len;
944 if(textlen >= PATH_MAX) goto name_error;
945 strcat(desc_file_name, text);
946
947 /* arch specific facilities are named like this : name_arch */
948 if(fac_name_len+1 < sizeof("_arch"))
949 arch_spec = 0;
950 else {
951 if(!strcmp(&text[fac_name_len+1-sizeof("_arch")], "_arch"))
952 arch_spec = 1;
953 else
954 arch_spec = 0;
955 }
956
957 #if 0
958 text = "_";
959 textlen+=strlen(text);
960 if(textlen >= PATH_MAX) goto name_error;
961 strcat(desc_file_name, text);
962
963 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
964 "%u", f->checksum);
965 if(err < 0) goto name_error;
966
967 textlen=strlen(desc_file_name);
968
969 #endif //0
970
971 if(arch_spec) {
972 switch(t->arch_type) {
973 case LTT_ARCH_TYPE_I386:
974 text = "_i386";
975 break;
976 case LTT_ARCH_TYPE_PPC:
977 text = "_ppc";
978 break;
979 case LTT_ARCH_TYPE_SH:
980 text = "_sh";
981 break;
982 case LTT_ARCH_TYPE_S390:
983 text = "_s390";
984 break;
985 case LTT_ARCH_TYPE_MIPS:
986 text = "_mips";
987 break;
988 case LTT_ARCH_TYPE_ARM:
989 text = "_arm";
990 break;
991 case LTT_ARCH_TYPE_PPC64:
992 text = "_ppc64";
993 break;
994 case LTT_ARCH_TYPE_X86_64:
995 text = "_x86_64";
996 break;
997 case LTT_ARCH_TYPE_C2:
998 text = "_c2";
999 break;
1000 case LTT_ARCH_TYPE_POWERPC:
1001 text = "_powerpc";
1002 break;
1003 default:
1004 g_error("Trace from unsupported architecture.");
1005 }
1006 textlen+=strlen(text);
1007 if(textlen >= PATH_MAX) goto name_error;
1008 strcat(desc_file_name, text);
1009 }
1010
1011 text = ".xml";
1012 textlen+=strlen(text);
1013 if(textlen >= PATH_MAX) goto name_error;
1014 strcat(desc_file_name, text);
1015
1016 err = ltt_facility_open(f, t, desc_file_name);
1017 if(err) goto facility_error;
1018
1019 return 0;
1020
1021 facility_error:
1022 name_error:
1023 return 1;
1024 }
1025
1026 static void ltt_fac_ids_destroy(gpointer data)
1027 {
1028 GArray *fac_ids = (GArray *)data;
1029
1030 g_array_free(fac_ids, TRUE);
1031 }
1032
1033
1034 /* Presumes the tracefile is already seeked at the beginning. It makes sense,
1035 * because it must be done just after the opening */
1036 static int ltt_process_facility_tracefile(LttTracefile *tf)
1037 {
1038 int err;
1039 LttFacility *fac;
1040 GArray *fac_ids;
1041 guint i;
1042 LttEventType *et;
1043
1044 while(1) {
1045 err = ltt_tracefile_read_seek(tf);
1046 if(err == EPERM) goto seek_error;
1047 else if(err == ERANGE) break; /* End of tracefile */
1048
1049 err = ltt_tracefile_read_update_event(tf);
1050 if(err) goto update_error;
1051
1052 /* We are on a facility load/or facility unload/ or heartbeat event */
1053 /* The rules are :
1054 * * facility 0 is hardcoded : this is the core facility. It will be shown
1055 * in the facility array though, and is shown as "loaded builtin" in the
1056 * trace.
1057 * It contains event :
1058 * 0 : facility load
1059 * 1 : facility unload
1060 * 2 : state dump facility load
1061 * 3 : heartbeat
1062 */
1063 if(tf->event.facility_id != LTT_FACILITY_CORE) {
1064 /* Should only contain core facility */
1065 g_warning("Error in processing facility file %s, "
1066 "should not contain facility id %u.", g_quark_to_string(tf->name),
1067 tf->event.facility_id);
1068 err = EPERM;
1069 goto fac_id_error;
1070 } else {
1071
1072 struct LttFacilityLoad *fac_load_data;
1073 struct LttStateDumpFacilityLoad *fac_state_dump_load_data;
1074 char *fac_name;
1075 void *pos;
1076
1077 // FIXME align
1078 switch((enum ltt_core_events)tf->event.event_id) {
1079 case LTT_EVENT_FACILITY_LOAD:
1080 fac_name = (char*)(tf->event.data);
1081 g_debug("Doing LTT_EVENT_FACILITY_LOAD of facility %s",
1082 fac_name);
1083 pos = (tf->event.data + strlen(fac_name) + 1);
1084 pos += ltt_align((size_t)pos, sizeof(guint32), tf->has_alignment);
1085 fac_load_data = (struct LttFacilityLoad *)pos;
1086
1087 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
1088 ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id));
1089 /* facility may already exist if trace is paused/unpaused */
1090 if(fac->exists) continue;
1091 fac->name = g_quark_from_string(fac_name);
1092 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
1093 &fac_load_data->checksum);
1094 fac->id = ltt_get_uint32(LTT_GET_BO(tf), &fac_load_data->id);
1095 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
1096 &fac_load_data->pointer_size);
1097 fac->int_size = ltt_get_uint32(LTT_GET_BO(tf),
1098 &fac_load_data->int_size);
1099 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
1100 &fac_load_data->long_size);
1101 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
1102 &fac_load_data->size_t_size);
1103 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
1104 &fac_load_data->has_alignment);
1105
1106 if(ltt_get_facility_description(fac, tf->trace, tf))
1107 continue; /* error opening description */
1108
1109 fac->trace = tf->trace;
1110
1111 /* Preset the field offsets */
1112 for(i=0; i<fac->events->len; i++){
1113 et = &g_array_index(fac->events, LttEventType, i);
1114 precompute_offsets(fac, et);
1115 }
1116
1117 fac->exists = 1;
1118
1119 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
1120 fac->name);
1121 if(fac_ids == NULL) {
1122 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
1123 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
1124 fac->name,
1125 fac_ids, ltt_fac_ids_destroy);
1126 }
1127 g_array_append_val(fac_ids, fac->id);
1128
1129 break;
1130 case LTT_EVENT_FACILITY_UNLOAD:
1131 g_debug("Doing LTT_EVENT_FACILITY_UNLOAD");
1132 /* We don't care about unload : facilities ID are valid for the whole
1133 * trace. They simply won't be used after the unload. */
1134 break;
1135 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
1136 fac_name = (char*)(tf->event.data);
1137 g_debug("Doing LTT_EVENT_STATE_DUMP_FACILITY_LOAD of facility %s",
1138 fac_name);
1139 pos = (tf->event.data + strlen(fac_name) + 1);
1140 pos += ltt_align((size_t)pos, sizeof(guint32), tf->has_alignment);
1141 fac_state_dump_load_data = (struct LttStateDumpFacilityLoad *)pos;
1142
1143 fac = &g_array_index (tf->trace->facilities_by_num, LttFacility,
1144 ltt_get_uint32(LTT_GET_BO(tf), &fac_state_dump_load_data->id));
1145 /* facility may already exist if trace is paused/unpaused */
1146 if(fac->exists) continue;
1147 fac->name = g_quark_from_string(fac_name);
1148 fac->checksum = ltt_get_uint32(LTT_GET_BO(tf),
1149 &fac_state_dump_load_data->checksum);
1150 fac->id = ltt_get_uint32(LTT_GET_BO(tf),
1151 &fac_state_dump_load_data->id);
1152 fac->pointer_size = ltt_get_uint32(LTT_GET_BO(tf),
1153 &fac_state_dump_load_data->pointer_size);
1154 fac->int_size = ltt_get_uint32(LTT_GET_BO(tf),
1155 &fac_state_dump_load_data->int_size);
1156 fac->long_size = ltt_get_uint32(LTT_GET_BO(tf),
1157 &fac_state_dump_load_data->long_size);
1158 fac->size_t_size = ltt_get_uint32(LTT_GET_BO(tf),
1159 &fac_state_dump_load_data->size_t_size);
1160 fac->alignment = ltt_get_uint32(LTT_GET_BO(tf),
1161 &fac_state_dump_load_data->has_alignment);
1162 if(ltt_get_facility_description(fac, tf->trace, tf))
1163 continue; /* error opening description */
1164
1165 fac->trace = tf->trace;
1166
1167 /* Preset the field offsets */
1168 for(i=0; i<fac->events->len; i++){
1169 et = &g_array_index(fac->events, LttEventType, i);
1170 precompute_offsets(fac, et);
1171 }
1172
1173 fac->exists = 1;
1174
1175 fac_ids = g_datalist_id_get_data(&tf->trace->facilities_by_name,
1176 fac->name);
1177 if(fac_ids == NULL) {
1178 fac_ids = g_array_sized_new (FALSE, TRUE, sizeof(guint), 1);
1179 g_datalist_id_set_data_full(&tf->trace->facilities_by_name,
1180 fac->name,
1181 fac_ids, ltt_fac_ids_destroy);
1182 }
1183 g_array_append_val(fac_ids, fac->id);
1184
1185 break;
1186 case LTT_EVENT_HEARTBEAT:
1187 break;
1188 default:
1189 g_warning("Error in processing facility file %s, "
1190 "unknown event id %hhu in core facility.",
1191 g_quark_to_string(tf->name),
1192 tf->event.event_id);
1193 err = EPERM;
1194 goto event_id_error;
1195 }
1196 }
1197 }
1198 return 0;
1199
1200 /* Error handling */
1201 event_id_error:
1202 fac_id_error:
1203 update_error:
1204 seek_error:
1205 g_warning("An error occured in facility tracefile parsing");
1206 return err;
1207 }
1208
1209
1210 LttTrace *ltt_trace_open(const gchar *pathname)
1211 {
1212 gchar abs_path[PATH_MAX];
1213 LttTrace * t;
1214 LttTracefile *tf;
1215 GArray *group;
1216 int i, ret;
1217 struct ltt_block_start_header *header;
1218 DIR *dir;
1219 struct dirent *entry;
1220 guint control_found = 0;
1221 guint eventdefs_found = 0;
1222 struct stat stat_buf;
1223 gchar path[PATH_MAX];
1224
1225 t = g_new(LttTrace, 1);
1226 if(!t) goto alloc_error;
1227
1228 get_absolute_pathname(pathname, abs_path);
1229 t->pathname = g_quark_from_string(abs_path);
1230
1231 g_datalist_init(&t->tracefiles);
1232
1233 /* Test to see if it looks like a trace */
1234 dir = opendir(abs_path);
1235 if(dir == NULL) {
1236 perror(abs_path);
1237 goto open_error;
1238 }
1239 while((entry = readdir(dir)) != NULL) {
1240 strcpy(path, abs_path);
1241 strcat(path, "/");
1242 strcat(path, entry->d_name);
1243 ret = stat(path, &stat_buf);
1244 if(ret == -1) {
1245 perror(path);
1246 continue;
1247 }
1248 if(S_ISDIR(stat_buf.st_mode)) {
1249 if(strcmp(entry->d_name, "control") == 0) {
1250 control_found = 1;
1251 }
1252 if(strcmp(entry->d_name, "eventdefs") == 0) {
1253 eventdefs_found = 1;
1254 }
1255 }
1256 }
1257 closedir(dir);
1258
1259 if(!control_found || !eventdefs_found) goto find_error;
1260
1261 /* Open all the tracefiles */
1262 if(open_tracefiles(t, abs_path, "")) {
1263 g_warning("Error opening tracefile %s", abs_path);
1264 goto find_error;
1265 }
1266
1267 /* Prepare the facilities containers : array and mapping */
1268 /* Array is zeroed : the "exists" field is set to false by default */
1269 t->facilities_by_num = g_array_sized_new (FALSE,
1270 TRUE, sizeof(LttFacility),
1271 NUM_FACILITIES);
1272 t->facilities_by_num = g_array_set_size(t->facilities_by_num, NUM_FACILITIES);
1273
1274 g_datalist_init(&t->facilities_by_name);
1275
1276 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1277 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1278 if(group == NULL) {
1279 g_error("Trace %s has no facility tracefile", abs_path);
1280 g_assert(0);
1281 goto facilities_error;
1282 }
1283
1284 /* Get the trace information for the control/facility 0 tracefile */
1285 g_assert(group->len > 0);
1286 tf = &g_array_index (group, LttTracefile, 0);
1287 header = (struct ltt_block_start_header*)tf->buffer.head;
1288 g_assert(parse_trace_header(header->trace,
1289 tf, t) == 0);
1290
1291 t->num_cpu = group->len;
1292
1293 for(i=0; i<group->len; i++) {
1294 tf = &g_array_index (group, LttTracefile, i);
1295 if(ltt_process_facility_tracefile(tf))
1296 goto facilities_error;
1297 }
1298
1299 return t;
1300
1301 /* Error handling */
1302 facilities_error:
1303 g_datalist_clear(&t->facilities_by_name);
1304 g_array_free(t->facilities_by_num, TRUE);
1305 find_error:
1306 g_datalist_clear(&t->tracefiles);
1307 open_error:
1308 g_free(t);
1309 alloc_error:
1310 return NULL;
1311
1312 }
1313
1314 GQuark ltt_trace_name(const LttTrace *t)
1315 {
1316 return t->pathname;
1317 }
1318
1319
1320 /******************************************************************************
1321 * When we copy a trace, we want all the opening actions to happen again :
1322 * the trace will be reopened and totally independant from the original.
1323 * That's why we call ltt_trace_open.
1324 *****************************************************************************/
1325 LttTrace *ltt_trace_copy(LttTrace *self)
1326 {
1327 return ltt_trace_open(g_quark_to_string(self->pathname));
1328 }
1329
1330 void ltt_trace_close(LttTrace *t)
1331 {
1332 guint i;
1333 LttFacility *fac;
1334
1335 for(i=0; i<t->facilities_by_num->len; i++) {
1336 fac = &g_array_index (t->facilities_by_num, LttFacility, i);
1337 if(fac->exists)
1338 ltt_facility_close(fac);
1339 }
1340
1341 g_datalist_clear(&t->facilities_by_name);
1342 g_array_free(t->facilities_by_num, TRUE);
1343 g_datalist_clear(&t->tracefiles);
1344 g_free(t);
1345 }
1346
1347
1348 /*****************************************************************************
1349 *Get the system description of the trace
1350 ****************************************************************************/
1351
1352 LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
1353 {
1354 g_assert(id < t->facilities_by_num->len);
1355 return &g_array_index(t->facilities_by_num, LttFacility, id);
1356 }
1357
1358 /* ltt_trace_facility_get_by_name
1359 *
1360 * Returns the GArray of facility indexes. All the fac_ids that matches the
1361 * requested facility name.
1362 *
1363 * If name is not found, returns NULL.
1364 */
1365 GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1366 {
1367 return g_datalist_id_get_data(&t->facilities_by_name, name);
1368 }
1369
1370 /*****************************************************************************
1371 * Functions to discover all the event types in the trace
1372 ****************************************************************************/
1373
1374 #if 0
1375 unsigned ltt_trace_eventtype_number(LttTrace *t)
1376 {
1377 unsigned int i;
1378 unsigned count = 0;
1379 unsigned int num = t->facility_number;
1380 LttFacility * f;
1381
1382 for(i=0;i<num;i++){
1383 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1384 count += f->event_number;
1385 }
1386 return count;
1387 }
1388 #endif //0
1389
1390 #if 0
1391 //use an iteration on all the trace facilities, and inside iteration on all the
1392 //event types in each facilities instead.
1393 LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
1394 {
1395 LttEventType *event_type;
1396
1397 LttFacility * f;
1398 f = ltt_trace_facility_by_id(t,evId);
1399
1400 if(unlikely(!f)) event_type = NULL;
1401 else event_type = f->events[evId - f->base_id];
1402
1403 return event_type;
1404 }
1405 #endif //0
1406
1407 #if 0
1408 /*****************************************************************************
1409 * ltt_trace_find_tracefile
1410 *
1411 * Find a tracefile by name and index in the group.
1412 *
1413 * Returns a pointer to the tracefiles, else NULL.
1414 ****************************************************************************/
1415
1416 LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
1417 {
1418 }
1419 #endif //0
1420
1421 /*****************************************************************************
1422 * Get the start time and end time of the trace
1423 ****************************************************************************/
1424
1425 static void ltt_tracefile_time_span_get(LttTracefile *tf,
1426 LttTime *start, LttTime *end)
1427 {
1428 int err;
1429
1430 err = map_block(tf, 0);
1431 if(unlikely(err)) {
1432 g_error("Can not map block");
1433 *start = ltt_time_infinite;
1434 } else
1435 *start = tf->buffer.begin.timestamp;
1436
1437 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1438 if(unlikely(err)) {
1439 g_error("Can not map block");
1440 *end = ltt_time_zero;
1441 } else
1442 *end = tf->buffer.end.timestamp;
1443 }
1444
1445 struct tracefile_time_span_get_args {
1446 LttTrace *t;
1447 LttTime *start;
1448 LttTime *end;
1449 };
1450
1451 static void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
1452 {
1453 struct tracefile_time_span_get_args *args =
1454 (struct tracefile_time_span_get_args*)user_data;
1455
1456 GArray *group = (GArray *)data;
1457 int i;
1458 LttTracefile *tf;
1459 LttTime tmp_start;
1460 LttTime tmp_end;
1461
1462 for(i=0; i<group->len; i++) {
1463 tf = &g_array_index (group, LttTracefile, i);
1464 if(tf->cpu_online) {
1465 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1466 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1467 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1468 }
1469 }
1470 }
1471
1472 void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1473 {
1474 LttTime min_start = ltt_time_infinite;
1475 LttTime max_end = ltt_time_zero;
1476 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
1477
1478 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1479
1480 if(start != NULL) *start = min_start;
1481 if(end != NULL) *end = max_end;
1482
1483 }
1484
1485
1486 /*****************************************************************************
1487 *Get the name of a tracefile
1488 ****************************************************************************/
1489
1490 GQuark ltt_tracefile_name(const LttTracefile *tf)
1491 {
1492 return tf->name;
1493 }
1494
1495 GQuark ltt_tracefile_long_name(const LttTracefile *tf)
1496 {
1497 return tf->long_name;
1498 }
1499
1500
1501
1502 guint ltt_tracefile_cpu(LttTracefile *tf)
1503 {
1504 return tf->cpu_num;
1505 }
1506
1507 guint ltt_tracefile_tid(LttTracefile *tf)
1508 {
1509 return tf->tid;
1510 }
1511
1512 guint ltt_tracefile_pgid(LttTracefile *tf)
1513 {
1514 return tf->pgid;
1515 }
1516
1517 guint64 ltt_tracefile_creation(LttTracefile *tf)
1518 {
1519 return tf->creation;
1520 }
1521 /*****************************************************************************
1522 * Get the number of blocks in the tracefile
1523 ****************************************************************************/
1524
1525 guint ltt_tracefile_block_number(LttTracefile *tf)
1526 {
1527 return tf->num_blocks;
1528 }
1529
1530
1531 /* Seek to the first event in a tracefile that has a time equal or greater than
1532 * the time passed in parameter.
1533 *
1534 * If the time parameter is outside the tracefile time span, seek to the first
1535 * event or if after, return ERANGE.
1536 *
1537 * If the time parameter is before the first event, we have to seek specially to
1538 * there.
1539 *
1540 * If the time is after the end of the trace, return ERANGE.
1541 *
1542 * Do a binary search to find the right block, then a sequential search in the
1543 * block to find the event.
1544 *
1545 * In the special case where the time requested fits inside a block that has no
1546 * event corresponding to the requested time, the first event of the next block
1547 * will be seeked.
1548 *
1549 * IMPORTANT NOTE : // FIXME everywhere...
1550 *
1551 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1552 * you will jump over an event if you do.
1553 *
1554 * Return value : 0 : no error, the tf->event can be used
1555 * ERANGE : time if after the last event of the trace
1556 * otherwise : this is an error.
1557 *
1558 * */
1559
1560 int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1561 {
1562 int ret = 0;
1563 int err;
1564 unsigned int block_num, high, low;
1565
1566 /* seek at the beginning of trace */
1567 err = map_block(tf, 0); /* First block */
1568 if(unlikely(err)) {
1569 g_error("Can not map block");
1570 goto fail;
1571 }
1572
1573 /* If the time is lower or equal the beginning of the trace,
1574 * go to the first event. */
1575 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1576 ret = ltt_tracefile_read(tf);
1577 if(ret == ERANGE) goto range;
1578 else if (ret) goto fail;
1579 goto found; /* There is either no event in the trace or the event points
1580 to the first event in the trace */
1581 }
1582
1583 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1584 if(unlikely(err)) {
1585 g_error("Can not map block");
1586 goto fail;
1587 }
1588
1589 /* If the time is after the end of the trace, return ERANGE. */
1590 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1591 goto range;
1592 }
1593
1594 /* Binary search the block */
1595 high = tf->num_blocks - 1;
1596 low = 0;
1597
1598 while(1) {
1599 block_num = ((high-low) / 2) + low;
1600
1601 err = map_block(tf, block_num);
1602 if(unlikely(err)) {
1603 g_error("Can not map block");
1604 goto fail;
1605 }
1606 if(high == low) {
1607 /* We cannot divide anymore : this is what would happen if the time
1608 * requested was exactly between two consecutive buffers'end and start
1609 * timestamps. This is also what would happend if we didn't deal with out
1610 * of span cases prior in this function. */
1611 /* The event is right in the buffer!
1612 * (or in the next buffer first event) */
1613 while(1) {
1614 ret = ltt_tracefile_read(tf);
1615 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1616 else if(ret) goto fail;
1617
1618 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1619 goto found;
1620 }
1621
1622 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
1623 /* go to lower part */
1624 high = block_num - 1;
1625 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1626 /* go to higher part */
1627 low = block_num + 1;
1628 } else {/* The event is right in the buffer!
1629 (or in the next buffer first event) */
1630 while(1) {
1631 ret = ltt_tracefile_read(tf);
1632 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
1633 else if(ret) goto fail;
1634
1635 if(ltt_time_compare(time, tf->event.event_time) <= 0)
1636 break;
1637 }
1638 goto found;
1639 }
1640 }
1641
1642 found:
1643 return 0;
1644 range:
1645 return ERANGE;
1646
1647 /* Error handling */
1648 fail:
1649 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1650 g_quark_to_string(tf->name));
1651 return EPERM;
1652 }
1653
1654
1655 int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
1656
1657 int err;
1658
1659 if(ep->tracefile != tf) {
1660 goto fail;
1661 }
1662
1663 err = map_block(tf, ep->block);
1664 if(unlikely(err)) {
1665 g_error("Can not map block");
1666 goto fail;
1667 }
1668
1669 tf->event.offset = ep->offset;
1670
1671 /* Put back the event real tsc */
1672 tf->event.tsc = ep->tsc;
1673 tf->buffer.tsc = ep->tsc;
1674
1675 err = ltt_tracefile_read_update_event(tf);
1676 if(err) goto fail;
1677 err = ltt_tracefile_read_op(tf);
1678 if(err) goto fail;
1679
1680 return 0;
1681
1682 fail:
1683 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1684 g_quark_to_string(tf->name));
1685 return 1;
1686 }
1687
1688 LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
1689 {
1690 LttTime time;
1691
1692 if(tsc > tf->trace->start_tsc) {
1693 time = ltt_time_from_uint64(
1694 (double)(tsc - tf->trace->start_tsc)
1695 * (1000000000.0 / tf->trace->freq_scale)
1696 / (double)tf->trace->start_freq);
1697 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1698 } else {
1699 time = ltt_time_from_uint64(
1700 (double)(tf->trace->start_tsc - tsc)
1701 * (1000000000.0 / tf->trace->freq_scale)
1702 / (double)tf->trace->start_freq);
1703 time = ltt_time_sub(tf->trace->start_time_from_tsc, time);
1704 }
1705 return time;
1706 }
1707
1708 /* Calculate the real event time based on the buffer boundaries */
1709 LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1710 {
1711 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
1712 }
1713
1714
1715 /* Get the current event of the tracefile : valid until the next read */
1716 LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1717 {
1718 return &tf->event;
1719 }
1720
1721
1722
1723 /*****************************************************************************
1724 *Function name
1725 * ltt_tracefile_read : Read the next event in the tracefile
1726 *Input params
1727 * t : tracefile
1728 *Return value
1729 *
1730 * Returns 0 if an event can be used in tf->event.
1731 * Returns ERANGE on end of trace. The event in tf->event still can be used
1732 * (if the last block was not empty).
1733 * Returns EPERM on error.
1734 *
1735 * This function does make the tracefile event structure point to the event
1736 * currently pointed to by the tf->event.
1737 *
1738 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1739 * reinitialize it after an error if you want results to be coherent.
1740 * It would be the case if a end of trace last buffer has no event : the end
1741 * of trace wouldn't be returned, but an error.
1742 * We make the assumption there is at least one event per buffer.
1743 ****************************************************************************/
1744
1745 int ltt_tracefile_read(LttTracefile *tf)
1746 {
1747 int err;
1748
1749 err = ltt_tracefile_read_seek(tf);
1750 if(err) return err;
1751 err = ltt_tracefile_read_update_event(tf);
1752 if(err) return err;
1753 err = ltt_tracefile_read_op(tf);
1754 if(err) return err;
1755
1756 return 0;
1757 }
1758
1759 int ltt_tracefile_read_seek(LttTracefile *tf)
1760 {
1761 int err;
1762
1763 /* Get next buffer until we finally have an event, or end of trace */
1764 while(1) {
1765 err = ltt_seek_next_event(tf);
1766 if(unlikely(err == ENOPROTOOPT)) {
1767 return EPERM;
1768 }
1769
1770 /* Are we at the end of the buffer ? */
1771 if(err == ERANGE) {
1772 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1773 return ERANGE;
1774 } else {
1775 /* get next block */
1776 err = map_block(tf, tf->buffer.index + 1);
1777 if(unlikely(err)) {
1778 g_error("Can not map block");
1779 return EPERM;
1780 }
1781 }
1782 } else break; /* We found an event ! */
1783 }
1784
1785 return 0;
1786 }
1787
1788
1789 /* do specific operation on events */
1790 int ltt_tracefile_read_op(LttTracefile *tf)
1791 {
1792 LttEvent *event;
1793
1794 event = &tf->event;
1795
1796 /* do event specific operation */
1797
1798 /* do something if its an heartbeat event : increment the heartbeat count */
1799 //if(event->facility_id == LTT_FACILITY_CORE)
1800 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1801 // tf->cur_heart_beat_number++;
1802
1803 return 0;
1804 }
1805
1806
1807 /* same as ltt_tracefile_read, but does not seek to the next event nor call
1808 * event specific operation. */
1809 int ltt_tracefile_read_update_event(LttTracefile *tf)
1810 {
1811 void * pos;
1812 LttEvent *event;
1813
1814 event = &tf->event;
1815 pos = tf->buffer.head + event->offset;
1816
1817 /* Read event header */
1818
1819 /* Align the head */
1820 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment);
1821
1822 if(tf->trace->has_heartbeat) {
1823 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1824 pos);
1825 /* 32 bits -> 64 bits tsc */
1826 /* note : still works for seek and non seek cases. */
1827 if(event->timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1828 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1829 + 0x100000000ULL)
1830 | (guint64)event->timestamp;
1831 event->tsc = tf->buffer.tsc;
1832 } else {
1833 /* no overflow */
1834 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1835 | (guint64)event->timestamp;
1836 event->tsc = tf->buffer.tsc;
1837 }
1838 pos += sizeof(guint32);
1839 } else {
1840 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1841 tf->buffer.tsc = event->tsc;
1842 pos += sizeof(guint64);
1843 }
1844 event->event_time = ltt_interpolate_time(tf, event);
1845 event->facility_id = *(guint8*)pos;
1846 pos += sizeof(guint8);
1847
1848 event->event_id = *(guint8*)pos;
1849 pos += sizeof(guint8);
1850
1851 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1852 pos += sizeof(guint16);
1853
1854 /* Align the head */
1855 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->has_alignment);
1856
1857 event->data = pos;
1858
1859 /* get the data size and update the event fields with the current
1860 * information */
1861 ltt_update_event_size(tf);
1862
1863 return 0;
1864 }
1865
1866
1867 /****************************************************************************
1868 *Function name
1869 * map_block : map a block from the file
1870 *Input Params
1871 * lttdes : ltt trace file
1872 * whichBlock : the block which will be read
1873 *return value
1874 * 0 : success
1875 * EINVAL : lseek fail
1876 * EIO : can not read from the file
1877 ****************************************************************************/
1878
1879 static gint map_block(LttTracefile * tf, guint block_num)
1880 {
1881 int page_size = getpagesize();
1882 struct ltt_block_start_header *header;
1883
1884 g_assert(block_num < tf->num_blocks);
1885
1886 if(tf->buffer.head != NULL) {
1887 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1888 g_warning("unmap size : %u\n",
1889 PAGE_ALIGN(tf->buf_size));
1890 perror("munmap error");
1891 g_assert(0);
1892 }
1893 }
1894
1895
1896 /* Multiple of pages aligned head */
1897 tf->buffer.head = mmap(0,
1898 PAGE_ALIGN(tf->buf_size),
1899 PROT_READ, MAP_PRIVATE, tf->fd,
1900 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
1901
1902 if(tf->buffer.head == MAP_FAILED) {
1903 perror("Error in allocating memory for buffer of tracefile");
1904 g_assert(0);
1905 goto map_error;
1906 }
1907 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
1908
1909
1910 tf->buffer.index = block_num;
1911
1912 header = (struct ltt_block_start_header*)tf->buffer.head;
1913
1914 #if 0
1915 tf->buffer.begin.timestamp = ltt_time_add(
1916 ltt_time_from_uint64(
1917 ltt_get_uint64(LTT_GET_BO(tf),
1918 &header->begin.timestamp)
1919 - tf->trace->start_monotonic),
1920 tf->trace->start_time);
1921 #endif //0
1922 //g_debug("block %u begin : %lu.%lu", block_num,
1923 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
1924 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1925 &header->begin.cycle_count);
1926 tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
1927 &header->begin.freq);
1928 if(tf->buffer.begin.freq == 0)
1929 tf->buffer.begin.freq = tf->trace->start_freq;
1930
1931 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
1932 tf->buffer.begin.cycle_count);
1933 #if 0
1934 ltt_time_add(
1935 ltt_time_from_uint64(
1936 (double)(tf->buffer.begin.cycle_count
1937 - tf->trace->start_tsc) * 1000000.0
1938 / (double)tf->trace->start_freq),
1939 tf->trace->start_time_from_tsc);
1940 #endif //0
1941 #if 0
1942
1943 tf->buffer.end.timestamp = ltt_time_add(
1944 ltt_time_from_uint64(
1945 ltt_get_uint64(LTT_GET_BO(tf),
1946 &header->end.timestamp)
1947 - tf->trace->start_monotonic),
1948 tf->trace->start_time);
1949 #endif //0
1950 //g_debug("block %u end : %lu.%lu", block_num,
1951 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
1952 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1953 &header->end.cycle_count);
1954 tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
1955 &header->end.freq);
1956 if(tf->buffer.end.freq == 0)
1957 tf->buffer.end.freq = tf->trace->start_freq;
1958
1959 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
1960 &header->lost_size);
1961 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
1962 tf->buffer.end.cycle_count);
1963 #if 0
1964 ltt_time_add(
1965 ltt_time_from_uint64(
1966 (double)(tf->buffer.end.cycle_count
1967 - tf->trace->start_tsc) * 1000000.0
1968 / (double)tf->trace->start_freq),
1969 tf->trace->start_time_from_tsc);
1970 #endif //0
1971 tf->buffer.tsc = tf->buffer.begin.cycle_count;
1972 tf->event.tsc = tf->buffer.tsc;
1973 tf->buffer.freq = tf->buffer.begin.freq;
1974
1975 /* FIXME
1976 * eventually support variable buffer size : will need a partial pre-read of
1977 * the headers to create an index when we open the trace... eventually. */
1978 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
1979 &header->buf_size));
1980
1981 /* Now that the buffer is mapped, calculate the time interpolation for the
1982 * block. */
1983
1984 // tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
1985 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
1986
1987 /* Make the current event point to the beginning of the buffer :
1988 * it means that the event read must get the first event. */
1989 tf->event.tracefile = tf;
1990 tf->event.block = block_num;
1991 tf->event.offset = 0;
1992
1993 return 0;
1994
1995 map_error:
1996 return -errno;
1997
1998 }
1999
2000 /* It will update the fields offsets too */
2001 void ltt_update_event_size(LttTracefile *tf)
2002 {
2003 off_t size = 0;
2004
2005 /* Specific handling of core events : necessary to read the facility control
2006 * tracefile. */
2007 LttFacility *f = ltt_trace_get_facility_by_num(tf->trace,
2008 tf->event.facility_id);
2009
2010 if(likely(tf->event.facility_id == LTT_FACILITY_CORE)) {
2011 switch((enum ltt_core_events)tf->event.event_id) {
2012 case LTT_EVENT_FACILITY_LOAD:
2013 size = strlen((char*)tf->event.data) + 1;
2014 //g_debug("Update Event facility load of facility %s", (char*)tf->event.data);
2015 size += ltt_align(size, sizeof(guint32), tf->has_alignment);
2016 size += sizeof(struct LttFacilityLoad);
2017 break;
2018 case LTT_EVENT_FACILITY_UNLOAD:
2019 //g_debug("Update Event facility unload");
2020 size = sizeof(struct LttFacilityUnload);
2021 break;
2022 case LTT_EVENT_STATE_DUMP_FACILITY_LOAD:
2023 size = strlen((char*)tf->event.data) + 1;
2024 size += ltt_align(size, sizeof(guint32), tf->has_alignment);
2025 //g_debug("Update Event facility load state dump of facility %s",
2026 // (char*)tf->event.data);
2027 size += sizeof(struct LttStateDumpFacilityLoad);
2028 break;
2029 case LTT_EVENT_HEARTBEAT:
2030 //g_debug("Update Event heartbeat");
2031 size = sizeof(TimeHeartbeat);
2032 break;
2033 default:
2034 g_warning("Error in getting event size : tracefile %s, "
2035 "unknown event id %hhu in core facility.",
2036 g_quark_to_string(tf->name),
2037 tf->event.event_id);
2038 goto event_id_error;
2039
2040 }
2041 } else {
2042 if(!f->exists) {
2043 g_warning("Unknown facility %hhu (0x%hhx) in tracefile %s",
2044 tf->event.facility_id,
2045 tf->event.facility_id,
2046 g_quark_to_string(tf->name));
2047 goto facility_error;
2048 }
2049
2050 LttEventType *event_type =
2051 ltt_facility_eventtype_get(f, tf->event.event_id);
2052
2053 if(!event_type) {
2054 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2055 tf->event.event_id,
2056 g_quark_to_string(f->name),
2057 g_quark_to_string(tf->name));
2058 goto event_type_error;
2059 }
2060
2061 /* Compute the dynamic offsets */
2062 compute_offsets(tf, f, event_type, &size, tf->event.data);
2063
2064 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2065 // tf->event.facility_id,
2066 // tf->event.event_id, size);
2067 }
2068
2069 tf->event.data_size = size;
2070
2071 /* Check consistency between kernel and LTTV structure sizes */
2072 if(tf->event.event_size == 0xFFFF) {
2073 /* Event size too big to fit in the event size field */
2074 tf->event.event_size = tf->event.data_size;
2075 }
2076 g_assert(tf->event.data_size == tf->event.event_size);
2077
2078 return;
2079
2080 facility_error:
2081 event_type_error:
2082 event_id_error:
2083 if(tf->event.event_size == 0xFFFF) {
2084 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2085 }
2086 /* The facility is unknown : use the kernel information about this event
2087 * to jump over it. */
2088 tf->event.data_size = tf->event.event_size;
2089 }
2090
2091
2092 /* Take the tf current event offset and use the event facility id and event id
2093 * to figure out where is the next event offset.
2094 *
2095 * This is an internal function not aiming at being used elsewhere : it will
2096 * not jump over the current block limits. Please consider using
2097 * ltt_tracefile_read to do this.
2098 *
2099 * Returns 0 on success
2100 * ERANGE if we are at the end of the buffer.
2101 * ENOPROTOOPT if an error occured when getting the current event size.
2102 */
2103 static int ltt_seek_next_event(LttTracefile *tf)
2104 {
2105 int ret = 0;
2106 void *pos;
2107
2108 /* seek over the buffer header if we are at the buffer start */
2109 if(tf->event.offset == 0) {
2110 tf->event.offset += tf->buffer_header_size;
2111
2112 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
2113 ret = ERANGE;
2114 }
2115 goto found;
2116 }
2117
2118
2119 pos = tf->event.data;
2120
2121 if(tf->event.data_size < 0) goto error;
2122
2123 pos += (size_t)tf->event.data_size;
2124
2125 tf->event.offset = pos - tf->buffer.head;
2126
2127 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
2128 ret = ERANGE;
2129 goto found;
2130 }
2131 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
2132
2133 found:
2134 return ret;
2135
2136 error:
2137 g_error("Error in ltt_seek_next_event for tracefile %s",
2138 g_quark_to_string(tf->name));
2139 return ENOPROTOOPT;
2140 }
2141
2142 #if 0
2143 /*****************************************************************************
2144 *Function name
2145 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
2146 *
2147 * 1.0 / (freq(khz) *1000) * 1000000000
2148 *Input Params
2149 * t : tracefile
2150 ****************************************************************************/
2151 /* from timer_tsc.c */
2152 #define CYC2NS_SCALE_FACTOR 10
2153 static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
2154 {
2155 //return 1e6 / (double)tf->buffer.freq;
2156 guint32 cpu_mhz = tf->buffer.freq / 1000;
2157 guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
2158
2159 return cyc2ns_scale;
2160 // return 1e6 / (double)tf->buffer.freq;
2161 }
2162
2163 static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
2164 {
2165 return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
2166 }
2167 #endif //0
2168
2169 #if 0
2170 void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
2171 {
2172 LttField * rootFld = evT->root_field;
2173 // rootFld->base_address = evD;
2174
2175 if(likely(rootFld))
2176 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
2177 evT, 0,0,rootFld, evD);
2178 }
2179 #endif //0
2180 #if 0
2181 /*****************************************************************************
2182 *Function name
2183 * set_fields_offsets : set the precomputable offset of the fields
2184 *Input params
2185 * tracefile : opened trace file
2186 * event_type : the event type
2187 ****************************************************************************/
2188
2189 void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
2190 {
2191 LttField *field = event_type->root_field;
2192 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
2193
2194 if(likely(field))
2195 preset_field_type_size(tf, event_type, 0, 0,
2196 &fixed_root, &fixed_parent,
2197 field);
2198
2199 }
2200 #endif //0
2201
2202
2203 /*****************************************************************************
2204 *Function name
2205 * get_alignment : Get the alignment needed for a field.
2206 *Input params
2207 * field : field
2208 *
2209 * returns : The size on which it must be aligned.
2210 *
2211 ****************************************************************************/
2212 off_t get_alignment(LttField *field)
2213 {
2214 LttType *type = &field->field_type;
2215
2216 switch(type->type_class) {
2217 case LTT_INT_FIXED:
2218 case LTT_UINT_FIXED:
2219 case LTT_POINTER:
2220 case LTT_CHAR:
2221 case LTT_UCHAR:
2222 case LTT_SHORT:
2223 case LTT_USHORT:
2224 case LTT_INT:
2225 case LTT_UINT:
2226 case LTT_LONG:
2227 case LTT_ULONG:
2228 case LTT_SIZE_T:
2229 case LTT_SSIZE_T:
2230 case LTT_OFF_T:
2231 case LTT_FLOAT:
2232 case LTT_ENUM:
2233 /* Align offset on type size */
2234 g_assert(field->field_size != 0);
2235 return field->field_size;
2236 break;
2237 case LTT_STRING:
2238 return 1;
2239 break;
2240 case LTT_ARRAY:
2241 g_assert(type->fields->len == 1);
2242 {
2243 LttField *child = &g_array_index(type->fields, LttField, 0);
2244 return get_alignment(child);
2245 }
2246 break;
2247 case LTT_SEQUENCE:
2248 g_assert(type->fields->len == 2);
2249 {
2250 off_t localign = 1;
2251 LttField *child = &g_array_index(type->fields, LttField, 0);
2252
2253 localign = max(localign, get_alignment(child));
2254
2255 child = &g_array_index(type->fields, LttField, 1);
2256 localign = max(localign, get_alignment(child));
2257
2258 return localign;
2259 }
2260 break;
2261 case LTT_STRUCT:
2262 case LTT_UNION:
2263 {
2264 guint i;
2265 off_t localign = 1;
2266
2267 for(i=0; i<type->fields->len; i++) {
2268 LttField *child = &g_array_index(type->fields, LttField, i);
2269 localign = max(localign, get_alignment(child));
2270 }
2271 return localign;
2272 }
2273 break;
2274 case LTT_NONE:
2275 default:
2276 g_error("get_alignment : unknown type");
2277 return -1;
2278 }
2279 }
2280
2281 /*****************************************************************************
2282 *Function name
2283 * field_compute_static_size : Determine the size of fields known by their
2284 * sole definition. Unions, arrays and struct sizes might be known, but
2285 * the parser does not give that information.
2286 *Input params
2287 * tf : tracefile
2288 * field : field
2289 *
2290 ****************************************************************************/
2291
2292 void field_compute_static_size(LttFacility *fac, LttField *field)
2293 {
2294 LttType *type = &field->field_type;
2295
2296 switch(type->type_class) {
2297 case LTT_INT_FIXED:
2298 case LTT_UINT_FIXED:
2299 case LTT_POINTER:
2300 case LTT_CHAR:
2301 case LTT_UCHAR:
2302 case LTT_SHORT:
2303 case LTT_USHORT:
2304 case LTT_INT:
2305 case LTT_UINT:
2306 case LTT_LONG:
2307 case LTT_ULONG:
2308 case LTT_SIZE_T:
2309 case LTT_SSIZE_T:
2310 case LTT_OFF_T:
2311 case LTT_FLOAT:
2312 case LTT_ENUM:
2313 case LTT_STRING:
2314 /* nothing to do */
2315 break;
2316 case LTT_ARRAY:
2317 /* note this : array type size is the number of elements in the array,
2318 * while array field size of the length of the array in bytes */
2319 g_assert(type->fields->len == 1);
2320 {
2321 LttField *child = &g_array_index(type->fields, LttField, 0);
2322 field_compute_static_size(fac, child);
2323
2324 if(child->field_size != 0) {
2325 field->field_size = type->size * child->field_size;
2326 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2327 sizeof(off_t), type->size);
2328 } else {
2329 field->field_size = 0;
2330 }
2331 }
2332 break;
2333 case LTT_SEQUENCE:
2334 g_assert(type->fields->len == 2);
2335 {
2336 off_t local_offset = 0;
2337 LttField *child = &g_array_index(type->fields, LttField, 1);
2338 field_compute_static_size(fac, child);
2339 field->field_size = 0;
2340 type->size = 0;
2341 if(child->field_size != 0) {
2342 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2343 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
2344 }
2345 }
2346 break;
2347 case LTT_STRUCT:
2348 case LTT_UNION:
2349 {
2350 guint i;
2351 for(i=0;i<type->fields->len;i++) {
2352 LttField *child = &g_array_index(type->fields, LttField, i);
2353 field_compute_static_size(fac, child);
2354 if(child->field_size != 0) {
2355 type->size += ltt_align(type->size, get_alignment(child),
2356 fac->alignment);
2357 type->size += child->field_size;
2358 } else {
2359 /* As soon as we find a child with variable size, we have
2360 * a variable size */
2361 type->size = 0;
2362 break;
2363 }
2364 }
2365 field->field_size = type->size;
2366 }
2367 break;
2368 default:
2369 g_error("field_static_size : unknown type");
2370 }
2371
2372 }
2373
2374
2375
2376 /*****************************************************************************
2377 *Function name
2378 * precompute_fields_offsets : set the precomputable offset of the fields
2379 *Input params
2380 * fac : facility
2381 * field : the field
2382 * offset : pointer to the current offset, must be incremented
2383 *
2384 * return : 1 : found a variable length field, stop the processing.
2385 * 0 otherwise.
2386 ****************************************************************************/
2387
2388
2389 gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset)
2390 {
2391 LttType *type = &field->field_type;
2392
2393 switch(type->type_class) {
2394 case LTT_INT_FIXED:
2395 case LTT_UINT_FIXED:
2396 case LTT_POINTER:
2397 case LTT_CHAR:
2398 case LTT_UCHAR:
2399 case LTT_SHORT:
2400 case LTT_USHORT:
2401 case LTT_INT:
2402 case LTT_UINT:
2403 case LTT_LONG:
2404 case LTT_ULONG:
2405 case LTT_SIZE_T:
2406 case LTT_SSIZE_T:
2407 case LTT_OFF_T:
2408 case LTT_FLOAT:
2409 case LTT_ENUM:
2410 g_assert(field->field_size != 0);
2411 /* Align offset on type size */
2412 *offset += ltt_align(*offset, get_alignment(field),
2413 fac->alignment);
2414 /* remember offset */
2415 field->offset_root = *offset;
2416 field->fixed_root = FIELD_FIXED;
2417 /* Increment offset */
2418 *offset += field->field_size;
2419 return 0;
2420 break;
2421 case LTT_STRING:
2422 field->offset_root = *offset;
2423 field->fixed_root = FIELD_FIXED;
2424 return 1;
2425 break;
2426 case LTT_ARRAY:
2427 g_assert(type->fields->len == 1);
2428 {
2429 LttField *child = &g_array_index(type->fields, LttField, 0);
2430
2431 *offset += ltt_align(*offset, get_alignment(field),
2432 fac->alignment);
2433
2434 /* remember offset */
2435 field->offset_root = *offset;
2436 field->array_offset = *offset;
2437 field->fixed_root = FIELD_FIXED;
2438
2439 /* Let the child be variable */
2440 //precompute_fields_offsets(tf, child, offset);
2441
2442 if(field->field_size != 0) {
2443 /* Increment offset */
2444 /* field_size is the array size in bytes */
2445 *offset += field->field_size;
2446 return 0;
2447 } else {
2448 return 1;
2449 }
2450 }
2451 break;
2452 case LTT_SEQUENCE:
2453 g_assert(type->fields->len == 2);
2454 {
2455 LttField *child;
2456 guint ret;
2457
2458 *offset += ltt_align(*offset, get_alignment(field),
2459 fac->alignment);
2460
2461 /* remember offset */
2462 field->offset_root = *offset;
2463 field->fixed_root = FIELD_FIXED;
2464
2465 child = &g_array_index(type->fields, LttField, 0);
2466 ret = precompute_fields_offsets(fac, child, offset);
2467 g_assert(ret == 0); /* Seq len cannot have variable len */
2468
2469 child = &g_array_index(type->fields, LttField, 1);
2470 *offset += ltt_align(*offset, get_alignment(child),
2471 fac->alignment);
2472 field->array_offset = *offset;
2473 /* Let the child be variable. */
2474 //ret = precompute_fields_offsets(fac, child, offset);
2475
2476 /* Cannot precompute fields offsets of sequence members, and has
2477 * variable length. */
2478 return 1;
2479 }
2480 break;
2481 case LTT_STRUCT:
2482 {
2483 LttField *child;
2484 guint i;
2485 gint ret=0;
2486
2487 *offset += ltt_align(*offset, get_alignment(field),
2488 fac->alignment);
2489 /* remember offset */
2490 field->offset_root = *offset;
2491 field->fixed_root = FIELD_FIXED;
2492
2493 for(i=0; i< type->fields->len; i++) {
2494 child = &g_array_index(type->fields, LttField, i);
2495 ret = precompute_fields_offsets(fac, child, offset);
2496
2497 if(ret) break;
2498 }
2499 return ret;
2500 }
2501 break;
2502 case LTT_UNION:
2503 {
2504 LttField *child;
2505 guint i;
2506 gint ret=0;
2507
2508 *offset += ltt_align(*offset, get_alignment(field),
2509 fac->alignment);
2510 /* remember offset */
2511 field->offset_root = *offset;
2512 field->fixed_root = FIELD_FIXED;
2513
2514 for(i=0; i< type->fields->len; i++) {
2515 *offset = field->offset_root;
2516 child = &g_array_index(type->fields, LttField, i);
2517 ret = precompute_fields_offsets(fac, child, offset);
2518
2519 if(ret) break;
2520 }
2521 *offset = field->offset_root + field->field_size;
2522 return ret;
2523 }
2524
2525 break;
2526 case LTT_NONE:
2527 default:
2528 g_error("precompute_fields_offsets : unknown type");
2529 return 1;
2530 }
2531
2532 }
2533
2534
2535 /*****************************************************************************
2536 *Function name
2537 * precompute_offsets : set the precomputable offset of an event type
2538 *Input params
2539 * tf : tracefile
2540 * event : event type
2541 *
2542 ****************************************************************************/
2543 void precompute_offsets(LttFacility *fac, LttEventType *event)
2544 {
2545 guint i;
2546 off_t offset = 0;
2547 gint ret;
2548
2549 /* First, compute the size of fixed size fields. Will determine size for
2550 * arrays, struct and unions, which is not done by the parser */
2551 for(i=0; i<event->fields->len; i++) {
2552 LttField *field = &g_array_index(event->fields, LttField, i);
2553 field_compute_static_size(fac, field);
2554 }
2555
2556 /* Precompute all known offsets */
2557 for(i=0; i<event->fields->len; i++) {
2558 LttField *field = &g_array_index(event->fields, LttField, i);
2559 ret = precompute_fields_offsets(fac, field, &offset);
2560 if(ret) break;
2561 }
2562 }
2563
2564
2565
2566
2567 /*****************************************************************************
2568 *Function name
2569 * preset_field_type_size : set the fixed sizes of the field type
2570 *Input params
2571 * tf : tracefile
2572 * event_type : event type
2573 * offset_root : offset from the root
2574 * offset_parent : offset from the parent
2575 * fixed_root : Do we know a fixed offset to the root ?
2576 * fixed_parent : Do we know a fixed offset to the parent ?
2577 * field : field
2578 ****************************************************************************/
2579
2580
2581
2582 // preset the fixed size offsets. Calculate them just like genevent-new : an
2583 // increment of a *to value that represents the offset from the start of the
2584 // event data.
2585 // The preset information is : offsets up to (and including) the first element
2586 // of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2587 #if 0
2588 void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
2589 off_t offset_root, off_t offset_parent,
2590 enum field_status *fixed_root, enum field_status *fixed_parent,
2591 LttField *field)
2592 {
2593 enum field_status local_fixed_root, local_fixed_parent;
2594 guint i;
2595 LttType *type;
2596
2597 g_assert(field->fixed_root == FIELD_UNKNOWN);
2598 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2599 g_assert(field->fixed_size == FIELD_UNKNOWN);
2600
2601 type = field->field_type;
2602
2603 field->fixed_root = *fixed_root;
2604 if(field->fixed_root == FIELD_FIXED)
2605 field->offset_root = offset_root;
2606 else
2607 field->offset_root = 0;
2608
2609 field->fixed_parent = *fixed_parent;
2610 if(field->fixed_parent == FIELD_FIXED)
2611 field->offset_parent = offset_parent;
2612 else
2613 field->offset_parent = 0;
2614
2615 size_t current_root_offset;
2616 size_t current_offset;
2617 enum field_status current_child_status, final_child_status;
2618 size_t max_size;
2619
2620 switch(type->type_class) {
2621 case LTT_INT_FIXED:
2622 case LTT_UINT_FIXED:
2623 case LTT_CHAR:
2624 case LTT_UCHAR:
2625 case LTT_SHORT:
2626 case LTT_USHORT:
2627 case LTT_INT:
2628 case LTT_UINT:
2629 case LTT_FLOAT:
2630 case LTT_ENUM:
2631 field->field_size = ltt_type_size(tf->trace, type);
2632 field->fixed_size = FIELD_FIXED;
2633 break;
2634 case LTT_POINTER:
2635 field->field_size = (off_t)event_type->facility->pointer_size;
2636 field->fixed_size = FIELD_FIXED;
2637 break;
2638 case LTT_LONG:
2639 case LTT_ULONG:
2640 field->field_size = (off_t)event_type->facility->long_size;
2641 field->fixed_size = FIELD_FIXED;
2642 break;
2643 case LTT_SIZE_T:
2644 case LTT_SSIZE_T:
2645 case LTT_OFF_T:
2646 field->field_size = (off_t)event_type->facility->size_t_size;
2647 field->fixed_size = FIELD_FIXED;
2648 break;
2649 case LTT_SEQUENCE:
2650 local_fixed_root = FIELD_VARIABLE;
2651 local_fixed_parent = FIELD_VARIABLE;
2652 preset_field_type_size(tf, event_type,
2653 0, 0,
2654 &local_fixed_root, &local_fixed_parent,
2655 field->child[0]);
2656 field->fixed_size = FIELD_VARIABLE;
2657 field->field_size = 0;
2658 *fixed_root = FIELD_VARIABLE;
2659 *fixed_parent = FIELD_VARIABLE;
2660 break;
2661 case LTT_STRING:
2662 field->fixed_size = FIELD_VARIABLE;
2663 field->field_size = 0;
2664 *fixed_root = FIELD_VARIABLE;
2665 *fixed_parent = FIELD_VARIABLE;
2666 break;
2667 case LTT_ARRAY:
2668 local_fixed_root = FIELD_VARIABLE;
2669 local_fixed_parent = FIELD_VARIABLE;
2670 preset_field_type_size(tf, event_type,
2671 0, 0,
2672 &local_fixed_root, &local_fixed_parent,
2673 field->child[0]);
2674 field->fixed_size = field->child[0]->fixed_size;
2675 if(field->fixed_size == FIELD_FIXED) {
2676 field->field_size = type->element_number * field->child[0]->field_size;
2677 } else {
2678 field->field_size = 0;
2679 *fixed_root = FIELD_VARIABLE;
2680 *fixed_parent = FIELD_VARIABLE;
2681 }
2682 break;
2683 case LTT_STRUCT:
2684 current_root_offset = field->offset_root;
2685 current_offset = 0;
2686 current_child_status = FIELD_FIXED;
2687 for(i=0;i<type->element_number;i++) {
2688 preset_field_type_size(tf, event_type,
2689 current_root_offset, current_offset,
2690 fixed_root, &current_child_status,
2691 field->child[i]);
2692 if(current_child_status == FIELD_FIXED) {
2693 current_root_offset += field->child[i]->field_size;
2694 current_offset += field->child[i]->field_size;
2695 } else {
2696 current_root_offset = 0;
2697 current_offset = 0;
2698 }
2699 }
2700 if(current_child_status != FIELD_FIXED) {
2701 *fixed_parent = current_child_status;
2702 field->field_size = 0;
2703 field->fixed_size = current_child_status;
2704 } else {
2705 field->field_size = current_offset;
2706 field->fixed_size = FIELD_FIXED;
2707 }
2708 break;
2709 case LTT_UNION:
2710 current_root_offset = field->offset_root;
2711 current_offset = 0;
2712 max_size = 0;
2713 final_child_status = FIELD_FIXED;
2714 for(i=0;i<type->element_number;i++) {
2715 enum field_status current_root_child_status = FIELD_FIXED;
2716 enum field_status current_child_status = FIELD_FIXED;
2717 preset_field_type_size(tf, event_type,
2718 current_root_offset, current_offset,
2719 &current_root_child_status, &current_child_status,
2720 field->child[i]);
2721 if(current_child_status != FIELD_FIXED)
2722 final_child_status = current_child_status;
2723 else
2724 max_size = max(max_size, field->child[i]->field_size);
2725 }
2726 if(final_child_status != FIELD_FIXED) {
2727 g_error("LTTV does not support variable size fields in unions.");
2728 /* This will stop the application. */
2729 *fixed_root = final_child_status;
2730 *fixed_parent = final_child_status;
2731 field->field_size = 0;
2732 field->fixed_size = current_child_status;
2733 } else {
2734 field->field_size = max_size;
2735 field->fixed_size = FIELD_FIXED;
2736 }
2737 break;
2738 case LTT_NONE:
2739 g_error("unexpected type NONE");
2740 break;
2741 }
2742
2743 }
2744 #endif //0
2745
2746 /*****************************************************************************
2747 *Function name
2748 * check_fields_compatibility : Check for compatibility between two fields :
2749 * do they use the same inner structure ?
2750 *Input params
2751 * event_type1 : event type
2752 * event_type2 : event type
2753 * field1 : field
2754 * field2 : field
2755 *Returns : 0 if identical
2756 * 1 if not.
2757 ****************************************************************************/
2758 // this function checks for equality of field types. Therefore, it does not use
2759 // per se offsets. For instance, an aligned version of a structure is
2760 // compatible with an unaligned version of the same structure.
2761 gint check_fields_compatibility(LttEventType *event_type1,
2762 LttEventType *event_type2,
2763 LttField *field1, LttField *field2)
2764 {
2765 guint different = 0;
2766 LttType *type1;
2767 LttType *type2;
2768
2769 if(field1 == NULL) {
2770 if(field2 == NULL) goto end;
2771 else {
2772 different = 1;
2773 goto end;
2774 }
2775 } else if(field2 == NULL) {
2776 different = 1;
2777 goto end;
2778 }
2779
2780 type1 = &field1->field_type;
2781 type2 = &field2->field_type;
2782
2783 if(type1->type_class != type2->type_class) {
2784 different = 1;
2785 goto end;
2786 }
2787 if(type1->network != type2->network) {
2788 different = 1;
2789 goto end;
2790 }
2791
2792 switch(type1->type_class) {
2793 case LTT_INT_FIXED:
2794 case LTT_UINT_FIXED:
2795 case LTT_POINTER:
2796 case LTT_CHAR:
2797 case LTT_UCHAR:
2798 case LTT_SHORT:
2799 case LTT_USHORT:
2800 case LTT_INT:
2801 case LTT_UINT:
2802 case LTT_LONG:
2803 case LTT_ULONG:
2804 case LTT_SIZE_T:
2805 case LTT_SSIZE_T:
2806 case LTT_OFF_T:
2807 case LTT_FLOAT:
2808 case LTT_ENUM:
2809 if(field1->field_size != field2->field_size)
2810 different = 1;
2811 break;
2812 case LTT_STRING:
2813 break;
2814 case LTT_ARRAY:
2815 {
2816 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2817 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2818
2819 if(type1->size != type2->size)
2820 different = 1;
2821 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2822 different = 1;
2823 }
2824 break;
2825 case LTT_SEQUENCE:
2826 {
2827 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2828 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2829
2830 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2831 different = 1;
2832 }
2833 break;
2834 case LTT_STRUCT:
2835 case LTT_UNION:
2836 {
2837 LttField *child;
2838 guint i;
2839
2840 if(type1->fields->len != type2->fields->len) {
2841 different = 1;
2842 goto end;
2843 }
2844
2845 for(i=0; i< type1->fields->len; i++) {
2846 LttField *child1;
2847 LttField *child2;
2848 child1 = &g_array_index(type1->fields, LttField, i);
2849 child2 = &g_array_index(type2->fields, LttField, i);
2850 different = check_fields_compatibility(event_type1,
2851 event_type2, child1, child2);
2852
2853 if(different) break;
2854 }
2855 }
2856 break;
2857 case LTT_NONE:
2858 default:
2859 g_error("precompute_fields_offsets : unknown type");
2860 }
2861
2862 end:
2863 return different;
2864 }
2865
2866
2867 #if 0
2868 gint check_fields_compatibility(LttEventType *event_type1,
2869 LttEventType *event_type2,
2870 LttField *field1, LttField *field2)
2871 {
2872 guint different = 0;
2873 guint i;
2874 LttType *type1;
2875 LttType *type2;
2876
2877 if(field1 == NULL) {
2878 if(field2 == NULL) goto end;
2879 else {
2880 different = 1;
2881 goto end;
2882 }
2883 } else if(field2 == NULL) {
2884 different = 1;
2885 goto end;
2886 }
2887
2888 g_assert(field1->fixed_root != FIELD_UNKNOWN);
2889 g_assert(field2->fixed_root != FIELD_UNKNOWN);
2890 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
2891 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
2892 g_assert(field1->fixed_size != FIELD_UNKNOWN);
2893 g_assert(field2->fixed_size != FIELD_UNKNOWN);
2894
2895 type1 = field1->field_type;
2896 type2 = field2->field_type;
2897
2898 if(type1->type_class != type2->type_class) {
2899 different = 1;
2900 goto end;
2901 }
2902 if(type1->element_name != type2->element_name) {
2903 different = 1;
2904 goto end;
2905 }
2906
2907 switch(type1->type_class) {
2908 case LTT_INT_FIXED:
2909 case LTT_UINT_FIXED:
2910 case LTT_POINTER:
2911 case LTT_CHAR:
2912 case LTT_UCHAR:
2913 case LTT_SHORT:
2914 case LTT_USHORT:
2915 case LTT_INT:
2916 case LTT_UINT:
2917 case LTT_FLOAT:
2918 case LTT_POINTER:
2919 case LTT_LONG:
2920 case LTT_ULONG:
2921 case LTT_SIZE_T:
2922 case LTT_SSIZE_T:
2923 case LTT_OFF_T:
2924 if(field1->field_size != field2->field_size) {
2925 different = 1;
2926 goto end;
2927 }
2928 break;
2929 case LTT_ENUM:
2930 if(type1->element_number != type2->element_number) {
2931 different = 1;
2932 goto end;
2933 }
2934 for(i=0;i<type1->element_number;i++) {
2935 if(type1->enum_strings[i] != type2->enum_strings[i]) {
2936 different = 1;
2937 goto end;
2938 }
2939 }
2940 break;
2941 case LTT_SEQUENCE:
2942 /* Two elements : size and child */
2943 g_assert(type1->element_number != type2->element_number);
2944 for(i=0;i<type1->element_number;i++) {
2945 if(check_fields_compatibility(event_type1, event_type2,
2946 field1->child[0], field2->child[0])) {
2947 different = 1;
2948 goto end;
2949 }
2950 }
2951 break;
2952 case LTT_STRING:
2953 break;
2954 case LTT_ARRAY:
2955 if(field1->field_size != field2->field_size) {
2956 different = 1;
2957 goto end;
2958 }
2959 /* Two elements : size and child */
2960 g_assert(type1->element_number != type2->element_number);
2961 for(i=0;i<type1->element_number;i++) {
2962 if(check_fields_compatibility(event_type1, event_type2,
2963 field1->child[0], field2->child[0])) {
2964 different = 1;
2965 goto end;
2966 }
2967 }
2968 break;
2969 case LTT_STRUCT:
2970 case LTT_UNION:
2971 if(type1->element_number != type2->element_number) {
2972 different = 1;
2973 break;
2974 }
2975 for(i=0;i<type1->element_number;i++) {
2976 if(check_fields_compatibility(event_type1, event_type2,
2977 field1->child[0], field2->child[0])) {
2978 different = 1;
2979 goto end;
2980 }
2981 }
2982 break;
2983 }
2984 end:
2985 return different;
2986 }
2987 #endif //0
2988
2989
2990 /*****************************************************************************
2991 *Function name
2992 * ltt_get_int : get an integer number
2993 *Input params
2994 * reverse_byte_order: must we reverse the byte order ?
2995 * size : the size of the integer
2996 * ptr : the data pointer
2997 *Return value
2998 * gint64 : a 64 bits integer
2999 ****************************************************************************/
3000
3001 gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
3002 {
3003 gint64 val;
3004
3005 switch(size) {
3006 case 1: val = *((gint8*)data); break;
3007 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
3008 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
3009 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
3010 default: val = ltt_get_int64(reverse_byte_order, data);
3011 g_critical("get_int : integer size %d unknown", size);
3012 break;
3013 }
3014
3015 return val;
3016 }
3017
3018 /*****************************************************************************
3019 *Function name
3020 * ltt_get_uint : get an unsigned integer number
3021 *Input params
3022 * reverse_byte_order: must we reverse the byte order ?
3023 * size : the size of the integer
3024 * ptr : the data pointer
3025 *Return value
3026 * guint64 : a 64 bits unsigned integer
3027 ****************************************************************************/
3028
3029 guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
3030 {
3031 guint64 val;
3032
3033 switch(size) {
3034 case 1: val = *((gint8*)data); break;
3035 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
3036 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
3037 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
3038 default: val = ltt_get_uint64(reverse_byte_order, data);
3039 g_critical("get_uint : unsigned integer size %d unknown",
3040 size);
3041 break;
3042 }
3043
3044 return val;
3045 }
3046
3047
3048 /* get the node name of the system */
3049
3050 char * ltt_trace_system_description_node_name (LttSystemDescription * s)
3051 {
3052 return s->node_name;
3053 }
3054
3055
3056 /* get the domain name of the system */
3057
3058 char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
3059 {
3060 return s->domain_name;
3061 }
3062
3063
3064 /* get the description of the system */
3065
3066 char * ltt_trace_system_description_description (LttSystemDescription * s)
3067 {
3068 return s->description;
3069 }
3070
3071
3072 /* get the NTP corrected start time of the trace */
3073 LttTime ltt_trace_start_time(LttTrace *t)
3074 {
3075 return t->start_time;
3076 }
3077
3078 /* get the monotonic start time of the trace */
3079 LttTime ltt_trace_start_time_monotonic(LttTrace *t)
3080 {
3081 return t->start_time_from_tsc;
3082 }
3083
3084 LttTracefile *ltt_tracefile_new()
3085 {
3086 return g_new(LttTracefile, 1);
3087 }
3088
3089 void ltt_tracefile_destroy(LttTracefile *tf)
3090 {
3091 g_free(tf);
3092 }
3093
3094 void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
3095 {
3096 *dest = *src;
3097 }
3098
3099 /* Before library loading... */
3100
3101 static void __attribute__((constructor)) init(void)
3102 {
3103 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
3104 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
3105
3106 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
3107 }
3108
This page took 0.146529 seconds and 5 git commands to generate.