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