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