fix lttctl
[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;
08a986f1 277 tf->compact_event_bits = 32 - vheader->compact_data_shift;
dd3a6d39 278 tf->tsc_mask = ((1ULL << (tf->tscbits))-1);
279 tf->tsc_mask = tf->tsc_mask << tf->tsc_lsb_truncate;
62e4e7bf 280 tf->tsc_mask_next_bit = (1ULL<<(tf->tscbits));
281 tf->tsc_mask_next_bit = tf->tsc_mask_next_bit << tf->tsc_lsb_truncate;
e939e5b2 282 if(t) {
283 t->start_freq = ltt_get_uint64(LTT_GET_BO(tf),
284 &vheader->start_freq);
a3999b49 285 t->freq_scale = ltt_get_uint32(LTT_GET_BO(tf),
286 &vheader->freq_scale);
e939e5b2 287 t->start_tsc = ltt_get_uint64(LTT_GET_BO(tf),
288 &vheader->start_tsc);
289 t->start_monotonic = ltt_get_uint64(LTT_GET_BO(tf),
290 &vheader->start_monotonic);
291 t->start_time.tv_sec = ltt_get_uint64(LTT_GET_BO(tf),
292 &vheader->start_time_sec);
293 t->start_time.tv_nsec = ltt_get_uint64(LTT_GET_BO(tf),
294 &vheader->start_time_usec);
295 t->start_time.tv_nsec *= 1000; /* microsec to nanosec */
296
297 t->start_time_from_tsc = ltt_time_from_uint64(
c7f86478 298 (double)t->start_tsc
299 * (1000000000.0 / tf->trace->freq_scale)
62e4e7bf 300 / (double)t->start_freq);
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;
afd57a3c 929 g_array_index (group, LttTracefile, num).event.tracefile =
930 &g_array_index (group, LttTracefile, num);
62e4e7bf 931 }
932 }
933
934 closedir(dir);
3aee1200 935
62e4e7bf 936 return 0;
f7afe191 937}
938
3aee1200 939/* ltt_get_facility_description
940 *
29e7c5b3 941 * Opens the file corresponding to the requested facility (identified by fac_id
3aee1200 942 * and checksum).
943 *
944 * The name searched is : %trace root%/eventdefs/facname_checksum.xml
945 *
946 * Returns 0 on success, or 1 on failure.
947 */
3c165eaf 948#if 0
3aee1200 949static int ltt_get_facility_description(LttFacility *f,
950 LttTrace *t,
951 LttTracefile *fac_tf)
6cd62ccf 952{
3aee1200 953 char desc_file_name[PATH_MAX];
954 const gchar *text;
955 guint textlen;
956 gint err;
62e4e7bf 957 gint arch_spec;
958 gint fac_name_len;
3aee1200 959
960 text = g_quark_to_string(t->pathname);
961 textlen = strlen(text);
962
963 if(textlen >= PATH_MAX) goto name_error;
964 strcpy(desc_file_name, text);
965
966 text = "/eventdefs/";
967 textlen+=strlen(text);
968 if(textlen >= PATH_MAX) goto name_error;
969 strcat(desc_file_name, text);
970
971 text = g_quark_to_string(f->name);
62e4e7bf 972 fac_name_len = strlen(text);
f5d7967f 973 textlen+=fac_name_len;
3aee1200 974 if(textlen >= PATH_MAX) goto name_error;
975 strcat(desc_file_name, text);
f5d7967f 976
62e4e7bf 977 /* arch specific facilities are named like this : name_arch */
978 if(fac_name_len+1 < sizeof("_arch"))
979 arch_spec = 0;
980 else {
981 if(!strcmp(&text[fac_name_len+1-sizeof("_arch")], "_arch"))
982 arch_spec = 1;
983 else
984 arch_spec = 0;
985 }
f5d7967f 986
29e7c5b3 987#if 0
3aee1200 988 text = "_";
989 textlen+=strlen(text);
990 if(textlen >= PATH_MAX) goto name_error;
991 strcat(desc_file_name, text);
963b5f2d 992
3aee1200 993 err = snprintf(desc_file_name+textlen, PATH_MAX-textlen-1,
994 "%u", f->checksum);
d2083cab 995 if(err < 0) goto name_error;
3aee1200 996
997 textlen=strlen(desc_file_name);
998
29e7c5b3 999#endif //0
62e4e7bf 1000
1001 if(arch_spec) {
1002 switch(t->arch_type) {
1003 case LTT_ARCH_TYPE_I386:
1004 text = "_i386";
1005 break;
1006 case LTT_ARCH_TYPE_PPC:
1007 text = "_ppc";
1008 break;
1009 case LTT_ARCH_TYPE_SH:
1010 text = "_sh";
1011 break;
1012 case LTT_ARCH_TYPE_S390:
1013 text = "_s390";
1014 break;
1015 case LTT_ARCH_TYPE_MIPS:
1016 text = "_mips";
1017 break;
1018 case LTT_ARCH_TYPE_ARM:
1019 text = "_arm";
1020 break;
1021 case LTT_ARCH_TYPE_PPC64:
1022 text = "_ppc64";
1023 break;
1024 case LTT_ARCH_TYPE_X86_64:
1025 text = "_x86_64";
1026 break;
1027 case LTT_ARCH_TYPE_C2:
1028 text = "_c2";
1029 break;
1030 case LTT_ARCH_TYPE_POWERPC:
1031 text = "_powerpc";
1032 break;
1033 default:
1034 g_error("Trace from unsupported architecture.");
1035 }
1036 textlen+=strlen(text);
1037 if(textlen >= PATH_MAX) goto name_error;
1038 strcat(desc_file_name, text);
1039 }
1040
3aee1200 1041 text = ".xml";
1042 textlen+=strlen(text);
1043 if(textlen >= PATH_MAX) goto name_error;
1044 strcat(desc_file_name, text);
29e7c5b3 1045
3aee1200 1046 err = ltt_facility_open(f, t, desc_file_name);
1047 if(err) goto facility_error;
3aee1200 1048
1049 return 0;
1050
1051facility_error:
1052name_error:
1053 return 1;
1054}
1055
1056static void ltt_fac_ids_destroy(gpointer data)
1057{
1058 GArray *fac_ids = (GArray *)data;
963b5f2d 1059
3aee1200 1060 g_array_free(fac_ids, TRUE);
1061}
3c165eaf 1062#endif //0
3aee1200 1063
1064/* Presumes the tracefile is already seeked at the beginning. It makes sense,
1065 * because it must be done just after the opening */
3c165eaf 1066int ltt_process_facility_tracefile(LttTracefile *tf)
3aee1200 1067{
1068 int err;
29af7cfd 1069 //LttFacility *fac;
1070 //GArray *fac_ids;
cb03932a 1071 guint i;
29af7cfd 1072 //LttEventType *et;
3aee1200 1073
1074 while(1) {
1075 err = ltt_tracefile_read_seek(tf);
1076 if(err == EPERM) goto seek_error;
1077 else if(err == ERANGE) break; /* End of tracefile */
1078
1079 err = ltt_tracefile_read_update_event(tf);
1080 if(err) goto update_error;
1081
1082 /* We are on a facility load/or facility unload/ or heartbeat event */
1083 /* The rules are :
1084 * * facility 0 is hardcoded : this is the core facility. It will be shown
1085 * in the facility array though, and is shown as "loaded builtin" in the
1086 * trace.
1087 * It contains event :
1088 * 0 : facility load
1089 * 1 : facility unload
1090 * 2 : state dump facility load
d2083cab 1091 * 3 : heartbeat
3aee1200 1092 */
3c165eaf 1093 if(tf->event.event_id >= MARKER_CORE_IDS) {
d2083cab 1094 /* Should only contain core facility */
3aee1200 1095 g_warning("Error in processing facility file %s, "
3c165eaf 1096 "should not contain event id %u.", g_quark_to_string(tf->name),
1097 tf->event.event_id);
3aee1200 1098 err = EPERM;
3c165eaf 1099 goto event_id_error;
d2083cab 1100 } else {
3aee1200 1101
2e13d6af 1102 char *pos;
3c165eaf 1103 const char *marker_name, *format;
1104 uint16_t id;
1105 guint8 int_size, long_size, pointer_size, size_t_size, alignment;
3aee1200 1106
1107 // FIXME align
3c165eaf 1108 switch((enum marker_id)tf->event.event_id) {
1109 case MARKER_ID_SET_MARKER_ID:
2e13d6af 1110 marker_name = pos = tf->event.data;
3c165eaf 1111 g_debug("Doing MARKER_ID_SET_MARKER_ID of marker %s", marker_name);
2e13d6af 1112 pos += strlen(marker_name) + 1;
1113 //remove genevent compatibility
256a5b3a 1114 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
1115 pos += ltt_align((size_t)pos, sizeof(uint16_t), tf->alignment);
3c165eaf 1116 id = ltt_get_uint16(LTT_GET_BO(tf), pos);
c7146c5c 1117 g_debug("In MARKER_ID_SET_MARKER_ID of marker %s id %hu",
1118 marker_name, id);
3c165eaf 1119 pos += sizeof(guint16);
1120 int_size = *(guint8*)pos;
1121 pos += sizeof(guint8);
1122 long_size = *(guint8*)pos;
1123 pos += sizeof(guint8);
1124 pointer_size = *(guint8*)pos;
1125 pos += sizeof(guint8);
1126 size_t_size = *(guint8*)pos;
1127 pos += sizeof(guint8);
1128 alignment = *(guint8*)pos;
1129 pos += sizeof(guint8);
1130 marker_id_event(tf->trace, g_quark_from_string(marker_name),
1131 id, int_size, long_size,
1132 pointer_size, size_t_size, alignment);
3aee1200 1133 break;
3c165eaf 1134 case MARKER_ID_SET_MARKER_FORMAT:
2e13d6af 1135 marker_name = pos = tf->event.data;
3c165eaf 1136 g_debug("Doing MARKER_ID_SET_MARKER_FORMAT of marker %s",
1137 marker_name);
2e13d6af 1138 pos += strlen(marker_name) + 1;
1139 //break genevent.
256a5b3a 1140 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
2e13d6af 1141 format = pos;
3c165eaf 1142 pos += strlen(format) + 1;
2e13d6af 1143 //break genevent
256a5b3a 1144 //pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
3c165eaf 1145 marker_format_event(tf->trace, g_quark_from_string(marker_name),
1146 format);
1147 /* get information from dictionnary TODO */
3aee1200 1148 break;
3c165eaf 1149 case MARKER_ID_HEARTBEAT_32:
1150 case MARKER_ID_HEARTBEAT_64:
d1bb700c 1151 break;
3aee1200 1152 default:
1153 g_warning("Error in processing facility file %s, "
3c165eaf 1154 "unknown event id %hhu.",
3aee1200 1155 g_quark_to_string(tf->name),
1156 tf->event.event_id);
1157 err = EPERM;
1158 goto event_id_error;
1159 }
1160 }
963b5f2d 1161 }
3aee1200 1162 return 0;
963b5f2d 1163
3aee1200 1164 /* Error handling */
3aee1200 1165event_id_error:
3aee1200 1166update_error:
1167seek_error:
d2083cab 1168 g_warning("An error occured in facility tracefile parsing");
3aee1200 1169 return err;
6cd62ccf 1170}
1171
963b5f2d 1172
3aee1200 1173LttTrace *ltt_trace_open(const gchar *pathname)
1174{
1175 gchar abs_path[PATH_MAX];
1176 LttTrace * t;
1177 LttTracefile *tf;
1178 GArray *group;
b56dcdf2 1179 int i, ret;
16fcbb80 1180 struct ltt_block_start_header *header;
62e4e7bf 1181 DIR *dir;
1182 struct dirent *entry;
b56dcdf2 1183 guint control_found = 0;
1184 guint eventdefs_found = 0;
62e4e7bf 1185 struct stat stat_buf;
b56dcdf2 1186 gchar path[PATH_MAX];
3aee1200 1187
1188 t = g_new(LttTrace, 1);
1189 if(!t) goto alloc_error;
1190
1191 get_absolute_pathname(pathname, abs_path);
1192 t->pathname = g_quark_from_string(abs_path);
1193
3aee1200 1194 g_datalist_init(&t->tracefiles);
b56dcdf2 1195
1196 /* Test to see if it looks like a trace */
62e4e7bf 1197 dir = opendir(abs_path);
1198 if(dir == NULL) {
1199 perror(abs_path);
1200 goto open_error;
1201 }
1202 while((entry = readdir(dir)) != NULL) {
b56dcdf2 1203 strcpy(path, abs_path);
1204 strcat(path, "/");
1205 strcat(path, entry->d_name);
62e4e7bf 1206 ret = stat(path, &stat_buf);
1207 if(ret == -1) {
1208 perror(path);
1209 continue;
1210 }
1211 if(S_ISDIR(stat_buf.st_mode)) {
b56dcdf2 1212 if(strcmp(entry->d_name, "control") == 0) {
1213 control_found = 1;
1214 }
1215 if(strcmp(entry->d_name, "eventdefs") == 0) {
1216 eventdefs_found = 1;
1217 }
1218 }
1219 }
1220 closedir(dir);
1221
1222 if(!control_found || !eventdefs_found) goto find_error;
1223
1224 /* Open all the tracefiles */
e45551ac 1225 if(open_tracefiles(t, abs_path, "")) {
1226 g_warning("Error opening tracefile %s", abs_path);
b56dcdf2 1227 goto find_error;
e45551ac 1228 }
3aee1200 1229
3aee1200 1230 /* Parse each trace control/facilitiesN files : get runtime fac. info */
1231 group = g_datalist_id_get_data(&t->tracefiles, LTT_TRACEFILE_NAME_FACILITIES);
1232 if(group == NULL) {
1233 g_error("Trace %s has no facility tracefile", abs_path);
3865ea09 1234 g_assert(0);
3aee1200 1235 goto facilities_error;
1236 }
1237
16fcbb80 1238 /* Get the trace information for the control/facility 0 tracefile */
1239 g_assert(group->len > 0);
1240 tf = &g_array_index (group, LttTracefile, 0);
1241 header = (struct ltt_block_start_header*)tf->buffer.head;
51551c6f 1242 g_assert(parse_trace_header(header->trace,
1243 tf, t) == 0);
b56dcdf2 1244
1245 t->num_cpu = group->len;
16fcbb80 1246
d79909d1 1247 ret = allocate_marker_data(t);
b44ba972 1248 if (ret)
d79909d1 1249 g_error("Error in allocating marker data");
1250
3aee1200 1251 for(i=0; i<group->len; i++) {
1252 tf = &g_array_index (group, LttTracefile, i);
1253 if(ltt_process_facility_tracefile(tf))
1254 goto facilities_error;
1255 }
dd3a6d39 1256
3aee1200 1257 return t;
1258
1259 /* Error handling */
1260facilities_error:
d79909d1 1261 destroy_marker_data(t);
b56dcdf2 1262find_error:
3aee1200 1263 g_datalist_clear(&t->tracefiles);
b56dcdf2 1264open_error:
3aee1200 1265 g_free(t);
1266alloc_error:
1267 return NULL;
1268
1269}
6cd62ccf 1270
826f1ab2 1271GQuark ltt_trace_name(const LttTrace *t)
6cd62ccf 1272{
3aee1200 1273 return t->pathname;
6cd62ccf 1274}
1275
6cd62ccf 1276
3aee1200 1277/******************************************************************************
1278 * When we copy a trace, we want all the opening actions to happen again :
1279 * the trace will be reopened and totally independant from the original.
1280 * That's why we call ltt_trace_open.
1281 *****************************************************************************/
1282LttTrace *ltt_trace_copy(LttTrace *self)
963b5f2d 1283{
3aee1200 1284 return ltt_trace_open(g_quark_to_string(self->pathname));
963b5f2d 1285}
1286
3aee1200 1287void ltt_trace_close(LttTrace *t)
6cd62ccf 1288{
3aee1200 1289 g_datalist_clear(&t->tracefiles);
1290 g_free(t);
6cd62ccf 1291}
1292
3aee1200 1293
6cd62ccf 1294/*****************************************************************************
3aee1200 1295 *Get the system description of the trace
6cd62ccf 1296 ****************************************************************************/
3c165eaf 1297#if 0
3aee1200 1298LttFacility *ltt_trace_facility_by_id(LttTrace *t, guint8 id)
6cd62ccf 1299{
3aee1200 1300 g_assert(id < t->facilities_by_num->len);
1301 return &g_array_index(t->facilities_by_num, LttFacility, id);
1302}
1303
1304/* ltt_trace_facility_get_by_name
1305 *
1306 * Returns the GArray of facility indexes. All the fac_ids that matches the
1307 * requested facility name.
1308 *
1309 * If name is not found, returns NULL.
1310 */
1311GArray *ltt_trace_facility_get_by_name(LttTrace *t, GQuark name)
1312{
1313 return g_datalist_id_get_data(&t->facilities_by_name, name);
6cd62ccf 1314}
3c165eaf 1315#endif //0
6cd62ccf 1316
1317/*****************************************************************************
963b5f2d 1318 * Functions to discover all the event types in the trace
6cd62ccf 1319 ****************************************************************************/
1320
3aee1200 1321#if 0
963b5f2d 1322unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 1323{
8d1e6362 1324 unsigned int i;
963b5f2d 1325 unsigned count = 0;
dc1cad90 1326 unsigned int num = t->facility_number;
963b5f2d 1327 LttFacility * f;
dc1cad90 1328
1329 for(i=0;i<num;i++){
963b5f2d 1330 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
1331 count += f->event_number;
1332 }
1333 return count;
6cd62ccf 1334}
3aee1200 1335#endif //0
6cd62ccf 1336
3aee1200 1337#if 0
1338//use an iteration on all the trace facilities, and inside iteration on all the
1339//event types in each facilities instead.
963b5f2d 1340LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 1341{
6e8c19d4 1342 LttEventType *event_type;
1343
963b5f2d 1344 LttFacility * f;
1345 f = ltt_trace_facility_by_id(t,evId);
6e8c19d4 1346
1347 if(unlikely(!f)) event_type = NULL;
1348 else event_type = f->events[evId - f->base_id];
1349
1350 return event_type;
6cd62ccf 1351}
3aee1200 1352#endif //0
6cd62ccf 1353
3aee1200 1354#if 0
6cd62ccf 1355/*****************************************************************************
3aee1200 1356 * ltt_trace_find_tracefile
1357 *
1358 * Find a tracefile by name and index in the group.
1359 *
1360 * Returns a pointer to the tracefiles, else NULL.
6cd62ccf 1361 ****************************************************************************/
6cd62ccf 1362
3aee1200 1363LttTracefile *ltt_trace_find_tracefile(LttTrace *t, const gchar *name)
6cd62ccf 1364{
6cd62ccf 1365}
3aee1200 1366#endif //0
6cd62ccf 1367
1368/*****************************************************************************
3aee1200 1369 * Get the start time and end time of the trace
6cd62ccf 1370 ****************************************************************************/
1371
3c165eaf 1372void ltt_tracefile_time_span_get(LttTracefile *tf,
3aee1200 1373 LttTime *start, LttTime *end)
6cd62ccf 1374{
3aee1200 1375 int err;
6cd62ccf 1376
3aee1200 1377 err = map_block(tf, 0);
1378 if(unlikely(err)) {
1379 g_error("Can not map block");
1380 *start = ltt_time_infinite;
1381 } else
1382 *start = tf->buffer.begin.timestamp;
1383
1384 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1385 if(unlikely(err)) {
1386 g_error("Can not map block");
1387 *end = ltt_time_zero;
1388 } else
1389 *end = tf->buffer.end.timestamp;
6cd62ccf 1390}
1391
3aee1200 1392struct tracefile_time_span_get_args {
1393 LttTrace *t;
1394 LttTime *start;
1395 LttTime *end;
1396};
963b5f2d 1397
3c165eaf 1398void group_time_span_get(GQuark name, gpointer data, gpointer user_data)
963b5f2d 1399{
3aee1200 1400 struct tracefile_time_span_get_args *args =
1401 (struct tracefile_time_span_get_args*)user_data;
1402
1403 GArray *group = (GArray *)data;
1404 int i;
1405 LttTracefile *tf;
1406 LttTime tmp_start;
1407 LttTime tmp_end;
1408
1409 for(i=0; i<group->len; i++) {
1410 tf = &g_array_index (group, LttTracefile, i);
1411 if(tf->cpu_online) {
1412 ltt_tracefile_time_span_get(tf, &tmp_start, &tmp_end);
1413 if(ltt_time_compare(*args->start, tmp_start)>0) *args->start = tmp_start;
1414 if(ltt_time_compare(*args->end, tmp_end)<0) *args->end = tmp_end;
1415 }
1416 }
6cd62ccf 1417}
1418
487ad181 1419void ltt_trace_time_span_get(LttTrace *t, LttTime *start, LttTime *end)
1420{
3aee1200 1421 LttTime min_start = ltt_time_infinite;
1422 LttTime max_end = ltt_time_zero;
1423 struct tracefile_time_span_get_args args = { t, &min_start, &max_end };
487ad181 1424
3aee1200 1425 g_datalist_foreach(&t->tracefiles, &group_time_span_get, &args);
1426
1427 if(start != NULL) *start = min_start;
1428 if(end != NULL) *end = max_end;
1429
487ad181 1430}
1431
1432
6cd62ccf 1433/*****************************************************************************
963b5f2d 1434 *Get the name of a tracefile
6cd62ccf 1435 ****************************************************************************/
1436
348c6ba8 1437GQuark ltt_tracefile_name(const LttTracefile *tf)
6cd62ccf 1438{
963b5f2d 1439 return tf->name;
6cd62ccf 1440}
1441
348c6ba8 1442GQuark ltt_tracefile_long_name(const LttTracefile *tf)
1443{
1444 return tf->long_name;
1445}
1446
1447
d3d34f49 1448
ae3d0f50 1449guint ltt_tracefile_cpu(LttTracefile *tf)
d3d34f49 1450{
1451 return tf->cpu_num;
1452}
1453
ae3d0f50 1454guint ltt_tracefile_tid(LttTracefile *tf)
1455{
1456 return tf->tid;
1457}
1458
1459guint ltt_tracefile_pgid(LttTracefile *tf)
1460{
1461 return tf->pgid;
1462}
1463
1464guint64 ltt_tracefile_creation(LttTracefile *tf)
1465{
1466 return tf->creation;
1467}
80da81ad 1468/*****************************************************************************
1469 * Get the number of blocks in the tracefile
1470 ****************************************************************************/
1471
3aee1200 1472guint ltt_tracefile_block_number(LttTracefile *tf)
80da81ad 1473{
3aee1200 1474 return tf->num_blocks;
80da81ad 1475}
1476
caf7a67a 1477
3aee1200 1478/* Seek to the first event in a tracefile that has a time equal or greater than
1479 * the time passed in parameter.
1480 *
1481 * If the time parameter is outside the tracefile time span, seek to the first
27304273 1482 * event or if after, return ERANGE.
3aee1200 1483 *
1484 * If the time parameter is before the first event, we have to seek specially to
1485 * there.
1486 *
27304273 1487 * If the time is after the end of the trace, return ERANGE.
3aee1200 1488 *
1489 * Do a binary search to find the right block, then a sequential search in the
1490 * block to find the event.
1491 *
1492 * In the special case where the time requested fits inside a block that has no
1493 * event corresponding to the requested time, the first event of the next block
1494 * will be seeked.
1495 *
1496 * IMPORTANT NOTE : // FIXME everywhere...
1497 *
1498 * You MUST NOT do a ltt_tracefile_read right after a ltt_tracefile_seek_time :
1499 * you will jump over an event if you do.
1500 *
1501 * Return value : 0 : no error, the tf->event can be used
27304273 1502 * ERANGE : time if after the last event of the trace
3aee1200 1503 * otherwise : this is an error.
1504 *
1505 * */
1506
1507int ltt_tracefile_seek_time(LttTracefile *tf, LttTime time)
1508{
1509 int ret = 0;
1510 int err;
1511 unsigned int block_num, high, low;
1512
1513 /* seek at the beginning of trace */
1514 err = map_block(tf, 0); /* First block */
1515 if(unlikely(err)) {
1516 g_error("Can not map block");
1517 goto fail;
caf7a67a 1518 }
1519
3aee1200 1520 /* If the time is lower or equal the beginning of the trace,
1521 * go to the first event. */
1522 if(ltt_time_compare(time, tf->buffer.begin.timestamp) <= 0) {
1523 ret = ltt_tracefile_read(tf);
27304273 1524 if(ret == ERANGE) goto range;
1525 else if (ret) goto fail;
3aee1200 1526 goto found; /* There is either no event in the trace or the event points
1527 to the first event in the trace */
1528 }
caf7a67a 1529
3aee1200 1530 err = map_block(tf, tf->num_blocks - 1); /* Last block */
1531 if(unlikely(err)) {
1532 g_error("Can not map block");
1533 goto fail;
caf7a67a 1534 }
6cd62ccf 1535
27304273 1536 /* If the time is after the end of the trace, return ERANGE. */
1537 if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1538 goto range;
3aee1200 1539 }
62e55dd6 1540
3aee1200 1541 /* Binary search the block */
1542 high = tf->num_blocks - 1;
1543 low = 0;
1544
1545 while(1) {
1546 block_num = ((high-low) / 2) + low;
1547
1548 err = map_block(tf, block_num);
1549 if(unlikely(err)) {
1550 g_error("Can not map block");
1551 goto fail;
db55eaae 1552 }
3aee1200 1553 if(high == low) {
1554 /* We cannot divide anymore : this is what would happen if the time
1555 * requested was exactly between two consecutive buffers'end and start
1556 * timestamps. This is also what would happend if we didn't deal with out
1557 * of span cases prior in this function. */
1558 /* The event is right in the buffer!
1559 * (or in the next buffer first event) */
1560 while(1) {
1561 ret = ltt_tracefile_read(tf);
27304273 1562 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1563 else if(ret) goto fail;
1564
d9e13a0f 1565 if(ltt_time_compare(time, tf->event.event_time) <= 0)
e45551ac 1566 goto found;
3aee1200 1567 }
1568
27304273 1569 } else if(ltt_time_compare(time, tf->buffer.begin.timestamp) < 0) {
3aee1200 1570 /* go to lower part */
b1369bef 1571 high = block_num - 1;
3aee1200 1572 } else if(ltt_time_compare(time, tf->buffer.end.timestamp) > 0) {
1573 /* go to higher part */
b1369bef 1574 low = block_num + 1;
3aee1200 1575 } else {/* The event is right in the buffer!
1576 (or in the next buffer first event) */
1577 while(1) {
27304273 1578 ret = ltt_tracefile_read(tf);
1579 if(ret == ERANGE) goto range; /* ERANGE or EPERM */
3aee1200 1580 else if(ret) goto fail;
1581
d9e13a0f 1582 if(ltt_time_compare(time, tf->event.event_time) <= 0)
3aee1200 1583 break;
6cd62ccf 1584 }
3aee1200 1585 goto found;
6cd62ccf 1586 }
6cd62ccf 1587 }
3aee1200 1588
1589found:
1590 return 0;
27304273 1591range:
1592 return ERANGE;
3aee1200 1593
1594 /* Error handling */
1595fail:
1596 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1597 g_quark_to_string(tf->name));
1598 return EPERM;
6cd62ccf 1599}
1600
80da81ad 1601
3aee1200 1602int ltt_tracefile_seek_position(LttTracefile *tf, const LttEventPosition *ep) {
80da81ad 1603
3aee1200 1604 int err;
1605
1606 if(ep->tracefile != tf) {
1607 goto fail;
80da81ad 1608 }
1609
3aee1200 1610 err = map_block(tf, ep->block);
1611 if(unlikely(err)) {
1612 g_error("Can not map block");
1613 goto fail;
1614 }
1615
1616 tf->event.offset = ep->offset;
18206708 1617
62e4e7bf 1618 /* Put back the event real tsc */
1619 tf->event.tsc = ep->tsc;
1620 tf->buffer.tsc = ep->tsc;
78f79181 1621
3aee1200 1622 err = ltt_tracefile_read_update_event(tf);
1623 if(err) goto fail;
1624 err = ltt_tracefile_read_op(tf);
1625 if(err) goto fail;
80da81ad 1626
a0c1f622 1627 return 0;
3aee1200 1628
1629fail:
1630 g_error("ltt_tracefile_seek_time failed on tracefile %s",
1631 g_quark_to_string(tf->name));
a0c1f622 1632 return 1;
3aee1200 1633}
1634
ae3d0f50 1635LttTime ltt_interpolate_time_from_tsc(LttTracefile *tf, guint64 tsc)
3aee1200 1636{
1637 LttTime time;
62e4e7bf 1638
1639 if(tsc > tf->trace->start_tsc) {
1640 time = ltt_time_from_uint64(
1641 (double)(tsc - tf->trace->start_tsc)
1642 * (1000000000.0 / tf->trace->freq_scale)
1643 / (double)tf->trace->start_freq);
1644 time = ltt_time_add(tf->trace->start_time_from_tsc, time);
1645 } else {
1646 time = ltt_time_from_uint64(
1647 (double)(tf->trace->start_tsc - tsc)
1648 * (1000000000.0 / tf->trace->freq_scale)
1649 / (double)tf->trace->start_freq);
1650 time = ltt_time_sub(tf->trace->start_time_from_tsc, time);
1651 }
3aee1200 1652 return time;
80da81ad 1653}
1654
ae3d0f50 1655/* Calculate the real event time based on the buffer boundaries */
1656LttTime ltt_interpolate_time(LttTracefile *tf, LttEvent *event)
1657{
62e4e7bf 1658 return ltt_interpolate_time_from_tsc(tf, tf->buffer.tsc);
ae3d0f50 1659}
1660
eed2ef37 1661
1662/* Get the current event of the tracefile : valid until the next read */
1663LttEvent *ltt_tracefile_get_event(LttTracefile *tf)
1664{
1665 return &tf->event;
1666}
1667
1668
1669
6cd62ccf 1670/*****************************************************************************
1671 *Function name
3aee1200 1672 * ltt_tracefile_read : Read the next event in the tracefile
6cd62ccf 1673 *Input params
1674 * t : tracefile
1675 *Return value
3aee1200 1676 *
1677 * Returns 0 if an event can be used in tf->event.
d822520b 1678 * Returns ERANGE on end of trace. The event in tf->event still can be used
1679 * (if the last block was not empty).
3aee1200 1680 * Returns EPERM on error.
1681 *
1682 * This function does make the tracefile event structure point to the event
1683 * currently pointed to by the tf->event.
1684 *
1685 * Note : you must call a ltt_tracefile_seek to the beginning of the trace to
1686 * reinitialize it after an error if you want results to be coherent.
1687 * It would be the case if a end of trace last buffer has no event : the end
1688 * of trace wouldn't be returned, but an error.
1689 * We make the assumption there is at least one event per buffer.
6cd62ccf 1690 ****************************************************************************/
1691
3aee1200 1692int ltt_tracefile_read(LttTracefile *tf)
6cd62ccf 1693{
963b5f2d 1694 int err;
6cd62ccf 1695
3aee1200 1696 err = ltt_tracefile_read_seek(tf);
1697 if(err) return err;
1698 err = ltt_tracefile_read_update_event(tf);
1699 if(err) return err;
1700 err = ltt_tracefile_read_op(tf);
1701 if(err) return err;
1702
1703 return 0;
1704}
1705
1706int ltt_tracefile_read_seek(LttTracefile *tf)
1707{
1708 int err;
1709
1710 /* Get next buffer until we finally have an event, or end of trace */
1711 while(1) {
1712 err = ltt_seek_next_event(tf);
1713 if(unlikely(err == ENOPROTOOPT)) {
1714 return EPERM;
bdc36259 1715 }
bdc36259 1716
3aee1200 1717 /* Are we at the end of the buffer ? */
1718 if(err == ERANGE) {
1719 if(unlikely(tf->buffer.index == tf->num_blocks-1)){ /* end of trace ? */
1720 return ERANGE;
1721 } else {
1722 /* get next block */
1723 err = map_block(tf, tf->buffer.index + 1);
1724 if(unlikely(err)) {
1725 g_error("Can not map block");
1726 return EPERM;
1727 }
1728 }
1729 } else break; /* We found an event ! */
1730 }
2dee981d 1731
3aee1200 1732 return 0;
1733}
18206708 1734
1735
3aee1200 1736/* do specific operation on events */
1737int ltt_tracefile_read_op(LttTracefile *tf)
1738{
3aee1200 1739 LttEvent *event;
1740
1741 event = &tf->event;
18206708 1742
3aee1200 1743 /* do event specific operation */
40331ba8 1744
3aee1200 1745 /* do something if its an heartbeat event : increment the heartbeat count */
1746 //if(event->facility_id == LTT_FACILITY_CORE)
1747 // if(event->event_id == LTT_EVENT_HEARTBEAT)
1748 // tf->cur_heart_beat_number++;
1749
1750 return 0;
6cd62ccf 1751}
1752
91f8d488 1753static void print_debug_event_header(LttEvent *ev, void *start_pos, void *end_pos)
1754{
1755 unsigned int offset = 0;
1756 int i, j;
1757
1758 g_printf("Event header (tracefile %s offset %llx):\n",
afd57a3c 1759 g_quark_to_string(ev->tracefile->long_name),
91f8d488 1760 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
1761 + (long)start_pos - (long)ev->tracefile->buffer.head);
1762
1763 while (offset < (long)end_pos - (long)start_pos) {
1764 g_printf("%8lx", (long)start_pos - (long)ev->tracefile->buffer.head + offset);
1765 g_printf(" ");
1766
1767 for (i = 0; i < 4 ; i++) {
1768 for (j = 0; j < 4; j++) {
1769 if (offset + ((i * 4) + j) <
1770 (long)end_pos - (long)start_pos)
1771 g_printf("%02hhX",
d3353231 1772 ((char*)start_pos)[offset + ((i * 4) + j)]);
91f8d488 1773 else
1774 g_printf(" ");
1775 g_printf(" ");
1776 }
1777 if (i < 4)
1778 g_printf(" ");
1779 }
1780 offset+=16;
1781 g_printf("\n");
1782 }
1783}
1784
6cd62ccf 1785
3aee1200 1786/* same as ltt_tracefile_read, but does not seek to the next event nor call
1787 * event specific operation. */
1788int ltt_tracefile_read_update_event(LttTracefile *tf)
6cd62ccf 1789{
3aee1200 1790 void * pos;
1791 LttEvent *event;
91f8d488 1792 void *pos_aligned;
3aee1200 1793
1794 event = &tf->event;
eed2ef37 1795 pos = tf->buffer.head + event->offset;
3aee1200 1796
1797 /* Read event header */
1798
62e4e7bf 1799 /* Align the head */
1800 if(!tf->compact)
256a5b3a 1801 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
62e4e7bf 1802 else {
1803 g_assert(tf->has_heartbeat);
256a5b3a 1804 pos += ltt_align((size_t)pos, sizeof(uint32_t), tf->alignment);
62e4e7bf 1805 }
91f8d488 1806 pos_aligned = pos;
3aee1200 1807
62e4e7bf 1808 if(tf->has_heartbeat) {
1809 event->timestamp = ltt_get_uint32(LTT_GET_BO(tf),
1810 pos);
d1bb700c 1811 if(!tf->compact) {
1812 /* 32 bits -> 64 bits tsc */
1813 /* note : still works for seek and non seek cases. */
1814 if(event->timestamp < (0xFFFFFFFFULL&tf->buffer.tsc)) {
1815 tf->buffer.tsc = ((tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1816 + 0x100000000ULL)
1817 | (guint64)event->timestamp;
1818 event->tsc = tf->buffer.tsc;
1819 } else {
1820 /* no overflow */
1821 tf->buffer.tsc = (tf->buffer.tsc&0xFFFFFFFF00000000ULL)
1822 | (guint64)event->timestamp;
1823 event->tsc = tf->buffer.tsc;
62e4e7bf 1824 event->compact_data = 0;
d1bb700c 1825 }
1826 } else {
1827 /* Compact header */
1828 /* We keep the LSB of the previous timestamp, to make sure
1829 * we never go back */
1830 event->event_id = event->timestamp >> tf->tscbits;
d3353231 1831 event->event_id = event->event_id & ((1 << tf->compact_event_bits) - 1);
dd3a6d39 1832 event->compact_data = event->timestamp >>
d3353231 1833 (tf->compact_event_bits + tf->tscbits);
dd3a6d39 1834 //printf("tsc bits %u, ev bits %u init data %u\n",
62e4e7bf 1835 // tf->tscbits, tf->trace->compact_event_bits, event->compact_data);
dd3a6d39 1836 /* Put the compact data back in original endianness */
1837 event->compact_data = ltt_get_uint32(LTT_GET_BO(tf), &event->compact_data);
d1bb700c 1838 event->event_size = 0xFFFF;
dd3a6d39 1839 //printf("Found compact event %d\n", event->event_id);
1840 //printf("Compact data %d\n", event->compact_data);
d1bb700c 1841 event->timestamp = event->timestamp << tf->tsc_lsb_truncate;
1842 event->timestamp = event->timestamp & tf->tsc_mask;
dd3a6d39 1843 //printf("timestamp 0x%lX\n", event->timestamp);
1844 //printf("mask 0x%llX\n", tf->tsc_mask);
1845 //printf("mask_next 0x%llX\n", tf->tsc_mask_next_bit);
1846 //printf("previous tsc 0x%llX\n", tf->buffer.tsc);
1847 //printf("previous tsc&mask 0x%llX\n", tf->tsc_mask&tf->buffer.tsc);
1848 //printf("previous tsc&(~mask) 0x%llX\n", tf->buffer.tsc&(~tf->tsc_mask));
d1bb700c 1849 if(event->timestamp < (tf->tsc_mask&tf->buffer.tsc)) {
dd3a6d39 1850 //printf("wrap\n");
d1bb700c 1851 tf->buffer.tsc = ((tf->buffer.tsc&(~tf->tsc_mask))
1852 + tf->tsc_mask_next_bit)
1853 | (guint64)event->timestamp;
1854 event->tsc = tf->buffer.tsc;
1855 } else {
dd3a6d39 1856 //printf("no wrap\n");
d1bb700c 1857 /* no overflow */
1858 tf->buffer.tsc = (tf->buffer.tsc&(~tf->tsc_mask))
1859 | (guint64)event->timestamp;
1860 event->tsc = tf->buffer.tsc;
1861 }
dd3a6d39 1862 //printf("current tsc 0x%llX\n", tf->buffer.tsc);
d1bb700c 1863 }
62e4e7bf 1864 pos += sizeof(guint32);
1865 } else {
1866 event->tsc = ltt_get_uint64(LTT_GET_BO(tf), pos);
1867 tf->buffer.tsc = event->tsc;
1868 event->compact_data = 0;
1869 pos += sizeof(guint64);
1870 }
1871 event->event_time = ltt_interpolate_time(tf, event);
3aee1200 1872
d1bb700c 1873 if(!tf->compact) {
3c165eaf 1874 event->event_id = *(guint16*)pos;
1875 pos += sizeof(guint16);
d1bb700c 1876
1877 event->event_size = ltt_get_uint16(LTT_GET_BO(tf), pos);
1878 pos += sizeof(guint16);
1879 } else {
1880 /* Compact event */
1881 }
91f8d488 1882
1883 if (a_event_debug)
1884 print_debug_event_header(event, pos_aligned, pos);
1885
62e4e7bf 1886 /* Align the head */
1887 if(!tf->compact)
256a5b3a 1888 pos += ltt_align((size_t)pos, tf->trace->arch_size, tf->alignment);
f104d082 1889
3aee1200 1890 event->data = pos;
1891
77175651 1892 /* get the data size and update the event fields with the current
d1bb700c 1893 * information. Also update the time if a heartbeat_full event is found. */
eed2ef37 1894 ltt_update_event_size(tf);
77175651 1895
3aee1200 1896 return 0;
6cd62ccf 1897}
1898
507915ee 1899
6cd62ccf 1900/****************************************************************************
1901 *Function name
3aee1200 1902 * map_block : map a block from the file
6cd62ccf 1903 *Input Params
1904 * lttdes : ltt trace file
1905 * whichBlock : the block which will be read
1906 *return value
1907 * 0 : success
1908 * EINVAL : lseek fail
1909 * EIO : can not read from the file
1910 ****************************************************************************/
1911
3c165eaf 1912gint map_block(LttTracefile * tf, guint block_num)
6cd62ccf 1913{
b77d1b57 1914 int page_size = getpagesize();
3aee1200 1915 struct ltt_block_start_header *header;
1916
1917 g_assert(block_num < tf->num_blocks);
6cd62ccf 1918
f628823c 1919 if(tf->buffer.head != NULL) {
1920 if(munmap(tf->buffer.head, PAGE_ALIGN(tf->buf_size))) {
1921 g_warning("unmap size : %u\n",
1922 PAGE_ALIGN(tf->buf_size));
1923 perror("munmap error");
1924 g_assert(0);
1925 }
1926 }
1927
ac849774 1928
3aee1200 1929 /* Multiple of pages aligned head */
b77d1b57 1930 tf->buffer.head = mmap(0,
f628823c 1931 PAGE_ALIGN(tf->buf_size),
b77d1b57 1932 PROT_READ, MAP_PRIVATE, tf->fd,
f628823c 1933 PAGE_ALIGN((off_t)tf->buf_size * (off_t)block_num));
3aee1200 1934
3865ea09 1935 if(tf->buffer.head == MAP_FAILED) {
3aee1200 1936 perror("Error in allocating memory for buffer of tracefile");
3865ea09 1937 g_assert(0);
3aee1200 1938 goto map_error;
6cd62ccf 1939 }
3865ea09 1940 g_assert( ( (guint)tf->buffer.head&(8-1) ) == 0); // make sure it's aligned.
3aee1200 1941
6cd62ccf 1942
3aee1200 1943 tf->buffer.index = block_num;
1944
1945 header = (struct ltt_block_start_header*)tf->buffer.head;
1946
791dffa6 1947#if 0
986e2a7c 1948 tf->buffer.begin.timestamp = ltt_time_add(
1949 ltt_time_from_uint64(
1950 ltt_get_uint64(LTT_GET_BO(tf),
1951 &header->begin.timestamp)
1952 - tf->trace->start_monotonic),
1953 tf->trace->start_time);
791dffa6 1954#endif //0
348c6ba8 1955 //g_debug("block %u begin : %lu.%lu", block_num,
1956 // tf->buffer.begin.timestamp.tv_sec, tf->buffer.begin.timestamp.tv_nsec);
3aee1200 1957 tf->buffer.begin.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1958 &header->begin.cycle_count);
986e2a7c 1959 tf->buffer.begin.freq = ltt_get_uint64(LTT_GET_BO(tf),
1960 &header->begin.freq);
62e4e7bf 1961 if(tf->buffer.begin.freq == 0)
1962 tf->buffer.begin.freq = tf->trace->start_freq;
ae3d0f50 1963
1964 tf->buffer.begin.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1965 tf->buffer.begin.cycle_count);
ae3d0f50 1966#if 0
62e4e7bf 1967 ltt_time_add(
791dffa6 1968 ltt_time_from_uint64(
88612d17 1969 (double)(tf->buffer.begin.cycle_count
1970 - tf->trace->start_tsc) * 1000000.0
791dffa6 1971 / (double)tf->trace->start_freq),
62e4e7bf 1972 tf->trace->start_time_from_tsc);
ae3d0f50 1973#endif //0
791dffa6 1974#if 0
1975
986e2a7c 1976 tf->buffer.end.timestamp = ltt_time_add(
1977 ltt_time_from_uint64(
1978 ltt_get_uint64(LTT_GET_BO(tf),
1979 &header->end.timestamp)
1980 - tf->trace->start_monotonic),
1981 tf->trace->start_time);
791dffa6 1982#endif //0
348c6ba8 1983 //g_debug("block %u end : %lu.%lu", block_num,
1984 // tf->buffer.end.timestamp.tv_sec, tf->buffer.end.timestamp.tv_nsec);
3aee1200 1985 tf->buffer.end.cycle_count = ltt_get_uint64(LTT_GET_BO(tf),
1986 &header->end.cycle_count);
986e2a7c 1987 tf->buffer.end.freq = ltt_get_uint64(LTT_GET_BO(tf),
1988 &header->end.freq);
62e4e7bf 1989 if(tf->buffer.end.freq == 0)
1990 tf->buffer.end.freq = tf->trace->start_freq;
1991
3aee1200 1992 tf->buffer.lost_size = ltt_get_uint32(LTT_GET_BO(tf),
986e2a7c 1993 &header->lost_size);
ae3d0f50 1994 tf->buffer.end.timestamp = ltt_interpolate_time_from_tsc(tf,
62e4e7bf 1995 tf->buffer.end.cycle_count);
ae3d0f50 1996#if 0
62e4e7bf 1997 ltt_time_add(
791dffa6 1998 ltt_time_from_uint64(
88612d17 1999 (double)(tf->buffer.end.cycle_count
2000 - tf->trace->start_tsc) * 1000000.0
791dffa6 2001 / (double)tf->trace->start_freq),
88612d17 2002 tf->trace->start_time_from_tsc);
ae3d0f50 2003#endif //0
3aee1200 2004 tf->buffer.tsc = tf->buffer.begin.cycle_count;
2005 tf->event.tsc = tf->buffer.tsc;
986e2a7c 2006 tf->buffer.freq = tf->buffer.begin.freq;
3aee1200 2007
2008 /* FIXME
2009 * eventually support variable buffer size : will need a partial pre-read of
2010 * the headers to create an index when we open the trace... eventually. */
f628823c 2011 g_assert(tf->buf_size == ltt_get_uint32(LTT_GET_BO(tf),
3aee1200 2012 &header->buf_size));
507915ee 2013
3aee1200 2014 /* Now that the buffer is mapped, calculate the time interpolation for the
2015 * block. */
2016
791dffa6 2017// tf->buffer.nsecs_per_cycle = calc_nsecs_per_cycle(tf);
2018 //tf->buffer.cyc2ns_scale = calc_nsecs_per_cycle(tf);
3aee1200 2019
2020 /* Make the current event point to the beginning of the buffer :
2021 * it means that the event read must get the first event. */
2022 tf->event.tracefile = tf;
2023 tf->event.block = block_num;
eed2ef37 2024 tf->event.offset = 0;
3aee1200 2025
2026 return 0;
6cd62ccf 2027
3aee1200 2028map_error:
2029 return -errno;
40331ba8 2030
6cd62ccf 2031}
2032
91f8d488 2033static void print_debug_event_data(LttEvent *ev)
2034{
2035 unsigned int offset = 0;
2036 int i, j;
2037
2038 if (!max(ev->event_size, ev->data_size))
2039 return;
2040
2041 g_printf("Event data (tracefile %s offset %llx):\n",
afd57a3c 2042 g_quark_to_string(ev->tracefile->long_name),
91f8d488 2043 ((uint64_t)ev->tracefile->buffer.index * ev->tracefile->buf_size)
2044 + (long)ev->data - (long)ev->tracefile->buffer.head);
2045
2046 while (offset < max(ev->event_size, ev->data_size)) {
2047 g_printf("%8lx", (long)ev->data + offset
2048 - (long)ev->tracefile->buffer.head);
2049 g_printf(" ");
2050
2051 for (i = 0; i < 4 ; i++) {
2052 for (j = 0; j < 4; j++) {
2053 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size))
2054 g_printf("%02hhX", ((char*)ev->data)[offset + ((i * 4) + j)]);
2055 else
2056 g_printf(" ");
2057 g_printf(" ");
2058 }
2059 if (i < 4)
2060 g_printf(" ");
2061 }
2062
2063 g_printf(" ");
2064
2065 for (i = 0; i < 4; i++) {
2066 for (j = 0; j < 4; j++) {
2067 if (offset + ((i * 4) + j) < max(ev->event_size, ev->data_size)) {
2068 if (isprint(((char*)ev->data)[offset + ((i * 4) + j)]))
2069 g_printf("%c", ((char*)ev->data)[offset + ((i * 4) + j)]);
2070 else
2071 g_printf(".");
2072 } else
2073 g_printf(" ");
2074 }
2075 }
2076 offset+=16;
2077 g_printf("\n");
2078 }
2079}
2080
77175651 2081/* It will update the fields offsets too */
2082void ltt_update_event_size(LttTracefile *tf)
6cd62ccf 2083{
2312de30 2084 off_t size = 0;
d1bb700c 2085 char *tscdata;
d2007fbd 2086 struct marker_info *info;
44f317b7 2087
d2007fbd 2088 switch((enum marker_id)tf->event.event_id) {
2089 case MARKER_ID_SET_MARKER_ID:
44f317b7 2090 size = strlen((char*)tf->event.data) + 1;
07effdbd 2091 g_debug("marker %s id set", (char*)tf->event.data);
256a5b3a 2092 size += ltt_align(size, sizeof(guint16), tf->alignment);
d2007fbd 2093 size += sizeof(guint16);
3c165eaf 2094 size += sizeof(guint8);
2095 size += sizeof(guint8);
2096 size += sizeof(guint8);
2097 size += sizeof(guint8);
2098 size += sizeof(guint8);
44f317b7 2099 break;
d2007fbd 2100 case MARKER_ID_SET_MARKER_FORMAT:
07effdbd 2101 g_debug("marker %s format set", (char*)tf->event.data);
44f317b7 2102 size = strlen((char*)tf->event.data) + 1;
2e13d6af 2103 size += strlen((char*)tf->event.data + size) + 1;
44f317b7 2104 break;
d2007fbd 2105 case MARKER_ID_HEARTBEAT_32:
07effdbd 2106 g_debug("Update Event heartbeat 32 bits");
44f317b7 2107 break;
d2007fbd 2108 case MARKER_ID_HEARTBEAT_64:
07effdbd 2109 g_debug("Update Event heartbeat 64 bits");
d1bb700c 2110 tscdata = (char*)(tf->event.data);
2111 tf->event.tsc = ltt_get_uint64(LTT_GET_BO(tf), tscdata);
2112 tf->buffer.tsc = tf->event.tsc;
2113 tf->event.event_time = ltt_interpolate_time(tf, &tf->event);
256a5b3a 2114 size = ltt_align(size, sizeof(guint64), tf->alignment);
d2007fbd 2115 size += sizeof(guint64);
d1bb700c 2116 break;
256a5b3a 2117 }
2118
2119 info = marker_get_info_from_id(tf->trace, tf->event.event_id);
2120 if (tf->event.event_id >= MARKER_CORE_IDS)
2121 g_assert(info != NULL);
2122
2123 /* Do not update field offsets of core markers when initially reading the
2124 * facility tracefile when the infos about these markers do not exist yet.
2125 */
2126 if (likely(info && info->fields)) {
2127 if (info->size != -1)
2128 size = info->size;
2129 else
2130 size = marker_update_fields_offsets(marker_get_info_from_id(tf->trace,
2131 tf->event.event_id), tf->event.data);
44f317b7 2132 }
c4afd5d8 2133
d2007fbd 2134 tf->event.data_size = size;
2135
2136 /* Check consistency between kernel and LTTV structure sizes */
2137 if(tf->event.event_size == 0xFFFF) {
2138 /* Event size too big to fit in the event size field */
2139 tf->event.event_size = tf->event.data_size;
2140 }
91f8d488 2141
2142 if (a_event_debug)
2143 print_debug_event_data(&tf->event);
2144
27c089c1 2145 /* Having a marker load or marker format event out of the facilities
2146 * tracefiles is a serious bug. */
2147 switch((enum marker_id)tf->event.event_id) {
2148 case MARKER_ID_SET_MARKER_ID:
2149 case MARKER_ID_SET_MARKER_FORMAT:
2150 if (tf->name != g_quark_from_string("/control/facilities"))
2151 g_error("Trace inconsistency : metadata event found in data "
2152 "tracefile %s", g_quark_to_string(tf->long_name));
2153 }
2154
d2007fbd 2155 if (tf->event.data_size != tf->event.event_size) {
3c165eaf 2156 struct marker_info *info = marker_get_info_from_id(tf->trace,
2157 tf->event.event_id);
2158 g_error("Kernel/LTTV event size differs for event %s: kernel %u, LTTV %u",
2159 g_quark_to_string(info->name),
2160 tf->event.event_size, tf->event.data_size);
d2007fbd 2161 exit(-1);
2162 }
2163
2164#if 0
44f317b7 2165 LttEventType *event_type =
2166 ltt_facility_eventtype_get(f, tf->event.event_id);
c4afd5d8 2167
44f317b7 2168 if(!event_type) {
2169 g_warning("Unknown event id %hhu in facility %s in tracefile %s",
2170 tf->event.event_id,
2171 g_quark_to_string(f->name),
2172 g_quark_to_string(tf->name));
2173 goto event_type_error;
3aee1200 2174 }
3aee1200 2175
44f317b7 2176 /* Compute the dynamic offsets */
2177 compute_offsets(tf, f, event_type, &size, tf->event.data);
2178
2179 //g_debug("Event root field : f.e %hhu.%hhu size %zd",
2180 // tf->event.facility_id,
2181 // tf->event.event_id, size);
7cbcdb89 2182
2183no_offset:
77175651 2184 tf->event.data_size = size;
eed2ef37 2185
b77d1b57 2186 /* Check consistency between kernel and LTTV structure sizes */
62e4e7bf 2187 if(tf->event.event_size == 0xFFFF) {
2188 /* Event size too big to fit in the event size field */
2189 tf->event.event_size = tf->event.data_size;
2190 }
f63ebe51 2191 if (tf->event.data_size != tf->event.event_size) {
2192 g_error("Kernel/LTTV event size differs for event %s.%s: kernel %u, LTTV %u",
62e4e7bf 2193 g_quark_to_string(f->name), g_quark_to_string(event_type->name),
2194 tf->event.event_size, tf->event.data_size);
f63ebe51 2195 exit(-1);
2196 }
2197 //g_assert(tf->event.data_size == tf->event.event_size);
9eb6aa7a 2198
eed2ef37 2199 return;
2200
c4afd5d8 2201event_type_error:
3aee1200 2202event_id_error:
62e4e7bf 2203 if(tf->event.event_size == 0xFFFF) {
2204 g_error("Cannot jump over an unknown event bigger than 0xFFFE bytes");
2205 }
2206 /* The facility is unknown : use the kernel information about this event
2207 * to jump over it. */
9d7e0c25 2208 tf->event.data_size = tf->event.event_size;
d2007fbd 2209#endif //0
6cd62ccf 2210}
2211
6cd62ccf 2212
3aee1200 2213/* Take the tf current event offset and use the event facility id and event id
2214 * to figure out where is the next event offset.
2215 *
2216 * This is an internal function not aiming at being used elsewhere : it will
2217 * not jump over the current block limits. Please consider using
2218 * ltt_tracefile_read to do this.
2219 *
2220 * Returns 0 on success
2221 * ERANGE if we are at the end of the buffer.
2222 * ENOPROTOOPT if an error occured when getting the current event size.
2223 */
3c165eaf 2224int ltt_seek_next_event(LttTracefile *tf)
6cd62ccf 2225{
3aee1200 2226 int ret = 0;
2227 void *pos;
3aee1200 2228
2229 /* seek over the buffer header if we are at the buffer start */
eed2ef37 2230 if(tf->event.offset == 0) {
51551c6f 2231 tf->event.offset += tf->buffer_header_size;
b77d1b57 2232
f628823c 2233 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
b77d1b57 2234 ret = ERANGE;
2235 }
3aee1200 2236 goto found;
2237 }
6cd62ccf 2238
6cd62ccf 2239
3aee1200 2240 pos = tf->event.data;
2241
77175651 2242 if(tf->event.data_size < 0) goto error;
3aee1200 2243
77175651 2244 pos += (size_t)tf->event.data_size;
3aee1200 2245
eed2ef37 2246 tf->event.offset = pos - tf->buffer.head;
cb03932a 2247
f628823c 2248 if(tf->event.offset == tf->buf_size - tf->buffer.lost_size) {
cb03932a 2249 ret = ERANGE;
2250 goto found;
2251 }
36d36c9f 2252 g_assert(tf->event.offset < tf->buf_size - tf->buffer.lost_size);
3aee1200 2253
2254found:
2255 return ret;
2256
2257error:
2258 g_error("Error in ltt_seek_next_event for tracefile %s",
2259 g_quark_to_string(tf->name));
2260 return ENOPROTOOPT;
6cd62ccf 2261}
2262
791dffa6 2263#if 0
6cd62ccf 2264/*****************************************************************************
2265 *Function name
3aee1200 2266 * calc_nsecs_per_cycle : calculate nsecs per cycle for current block
986e2a7c 2267 *
2268 * 1.0 / (freq(khz) *1000) * 1000000000
6cd62ccf 2269 *Input Params
2270 * t : tracefile
2271 ****************************************************************************/
986e2a7c 2272/* from timer_tsc.c */
2273#define CYC2NS_SCALE_FACTOR 10
791dffa6 2274static guint32 calc_nsecs_per_cycle(LttTracefile * tf)
6cd62ccf 2275{
986e2a7c 2276 //return 1e6 / (double)tf->buffer.freq;
791dffa6 2277 guint32 cpu_mhz = tf->buffer.freq / 1000;
2278 guint32 cyc2ns_scale = (1000 << CYC2NS_SCALE_FACTOR)/cpu_mhz;
6cd62ccf 2279
791dffa6 2280 return cyc2ns_scale;
986e2a7c 2281 // return 1e6 / (double)tf->buffer.freq;
3aee1200 2282}
791dffa6 2283
2284static guint64 cycles_2_ns(LttTracefile *tf, guint64 cycles)
2285{
2286 return (cycles * tf->buffer.cyc2ns_scale) >> CYC2NS_SCALE_FACTOR;
2287}
2288#endif //0
2289
3aee1200 2290#if 0
2291void setFieldsOffset(LttTracefile *tf, LttEventType *evT,void *evD)
2292{
2293 LttField * rootFld = evT->root_field;
2294 // rootFld->base_address = evD;
2dee981d 2295
3aee1200 2296 if(likely(rootFld))
2297 rootFld->field_size = getFieldtypeSize(tf, evT->facility,
2298 evT, 0,0,rootFld, evD);
6cd62ccf 2299}
3aee1200 2300#endif //0
f104d082 2301#if 0
3aee1200 2302/*****************************************************************************
6cd62ccf 2303 *Function name
3aee1200 2304 * set_fields_offsets : set the precomputable offset of the fields
6cd62ccf 2305 *Input params
3aee1200 2306 * tracefile : opened trace file
2307 * event_type : the event type
6cd62ccf 2308 ****************************************************************************/
2309
3aee1200 2310void set_fields_offsets(LttTracefile *tf, LttEventType *event_type)
6cd62ccf 2311{
3aee1200 2312 LttField *field = event_type->root_field;
2313 enum field_status fixed_root = FIELD_FIXED, fixed_parent = FIELD_FIXED;
2314
2315 if(likely(field))
2316 preset_field_type_size(tf, event_type, 0, 0,
2317 &fixed_root, &fixed_parent,
2318 field);
2319
2320}
f104d082 2321#endif //0
2322
2323
2324/*****************************************************************************
2325 *Function name
2326 * get_alignment : Get the alignment needed for a field.
2327 *Input params
f104d082 2328 * field : field
2329 *
2330 * returns : The size on which it must be aligned.
2331 *
2332 ****************************************************************************/
3c165eaf 2333#if 0
743e50fd 2334off_t get_alignment(LttField *field)
f104d082 2335{
2312de30 2336 LttType *type = &field->field_type;
f104d082 2337
2338 switch(type->type_class) {
2339 case LTT_INT_FIXED:
2340 case LTT_UINT_FIXED:
2341 case LTT_POINTER:
2342 case LTT_CHAR:
2343 case LTT_UCHAR:
2344 case LTT_SHORT:
2345 case LTT_USHORT:
2346 case LTT_INT:
2347 case LTT_UINT:
2348 case LTT_LONG:
2349 case LTT_ULONG:
2350 case LTT_SIZE_T:
2351 case LTT_SSIZE_T:
2352 case LTT_OFF_T:
2353 case LTT_FLOAT:
2354 case LTT_ENUM:
2355 /* Align offset on type size */
83e160f2 2356 g_assert(field->field_size != 0);
f104d082 2357 return field->field_size;
2358 break;
2359 case LTT_STRING:
83e160f2 2360 return 1;
f104d082 2361 break;
2362 case LTT_ARRAY:
2363 g_assert(type->fields->len == 1);
2364 {
2365 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 2366 return get_alignment(child);
f104d082 2367 }
2368 break;
2369 case LTT_SEQUENCE:
2370 g_assert(type->fields->len == 2);
2371 {
83e160f2 2372 off_t localign = 1;
f104d082 2373 LttField *child = &g_array_index(type->fields, LttField, 0);
2374
743e50fd 2375 localign = max(localign, get_alignment(child));
f104d082 2376
2377 child = &g_array_index(type->fields, LttField, 1);
743e50fd 2378 localign = max(localign, get_alignment(child));
f104d082 2379
2380 return localign;
2381 }
2382 break;
2383 case LTT_STRUCT:
2384 case LTT_UNION:
2385 {
2386 guint i;
83e160f2 2387 off_t localign = 1;
f104d082 2388
2389 for(i=0; i<type->fields->len; i++) {
2390 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 2391 localign = max(localign, get_alignment(child));
f104d082 2392 }
2393 return localign;
2394 }
2395 break;
2396 case LTT_NONE:
2397 default:
2398 g_error("get_alignment : unknown type");
83e160f2 2399 return -1;
f104d082 2400 }
f104d082 2401}
2402
3c165eaf 2403#endif //0
2404
f104d082 2405/*****************************************************************************
2406 *Function name
2407 * field_compute_static_size : Determine the size of fields known by their
2408 * sole definition. Unions, arrays and struct sizes might be known, but
2409 * the parser does not give that information.
2410 *Input params
2411 * tf : tracefile
2412 * field : field
2413 *
2414 ****************************************************************************/
3c165eaf 2415#if 0
743e50fd 2416void field_compute_static_size(LttFacility *fac, LttField *field)
f104d082 2417{
2312de30 2418 LttType *type = &field->field_type;
f104d082 2419
2420 switch(type->type_class) {
2421 case LTT_INT_FIXED:
2422 case LTT_UINT_FIXED:
2423 case LTT_POINTER:
2424 case LTT_CHAR:
2425 case LTT_UCHAR:
2426 case LTT_SHORT:
2427 case LTT_USHORT:
2428 case LTT_INT:
2429 case LTT_UINT:
2430 case LTT_LONG:
2431 case LTT_ULONG:
2432 case LTT_SIZE_T:
2433 case LTT_SSIZE_T:
2434 case LTT_OFF_T:
2435 case LTT_FLOAT:
2436 case LTT_ENUM:
2437 case LTT_STRING:
2438 /* nothing to do */
2439 break;
2440 case LTT_ARRAY:
2441 /* note this : array type size is the number of elements in the array,
2442 * while array field size of the length of the array in bytes */
2443 g_assert(type->fields->len == 1);
2444 {
2445 LttField *child = &g_array_index(type->fields, LttField, 0);
743e50fd 2446 field_compute_static_size(fac, child);
f104d082 2447
2448 if(child->field_size != 0) {
2449 field->field_size = type->size * child->field_size;
2450 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2451 sizeof(off_t), type->size);
2452 } else {
2453 field->field_size = 0;
2454 }
2455 }
2456 break;
2457 case LTT_SEQUENCE:
2458 g_assert(type->fields->len == 2);
2459 {
2312de30 2460 off_t local_offset = 0;
f104d082 2461 LttField *child = &g_array_index(type->fields, LttField, 1);
743e50fd 2462 field_compute_static_size(fac, child);
f104d082 2463 field->field_size = 0;
2464 type->size = 0;
2465 if(child->field_size != 0) {
2466 field->dynamic_offsets = g_array_sized_new(FALSE, TRUE,
2467 sizeof(off_t), SEQUENCE_AVG_ELEMENTS);
2468 }
2469 }
2470 break;
2471 case LTT_STRUCT:
2472 case LTT_UNION:
2473 {
2474 guint i;
2475 for(i=0;i<type->fields->len;i++) {
2476 LttField *child = &g_array_index(type->fields, LttField, i);
743e50fd 2477 field_compute_static_size(fac, child);
f104d082 2478 if(child->field_size != 0) {
743e50fd 2479 type->size += ltt_align(type->size, get_alignment(child),
2480 fac->alignment);
f104d082 2481 type->size += child->field_size;
2482 } else {
2483 /* As soon as we find a child with variable size, we have
2484 * a variable size */
2485 type->size = 0;
2486 break;
2487 }
2488 }
2489 field->field_size = type->size;
2490 }
2491 break;
2492 default:
2493 g_error("field_static_size : unknown type");
2312de30 2494 }
f104d082 2495
2496}
3c165eaf 2497#endif //0
f104d082 2498
2499
2500/*****************************************************************************
2501 *Function name
2502 * precompute_fields_offsets : set the precomputable offset of the fields
2503 *Input params
743e50fd 2504 * fac : facility
f104d082 2505 * field : the field
2506 * offset : pointer to the current offset, must be incremented
2507 *
2508 * return : 1 : found a variable length field, stop the processing.
2509 * 0 otherwise.
2510 ****************************************************************************/
2511
3c165eaf 2512#if 0
dd3a6d39 2513gint precompute_fields_offsets(LttFacility *fac, LttField *field, off_t *offset, gint is_compact)
f104d082 2514{
2312de30 2515 LttType *type = &field->field_type;
dd3a6d39 2516
2517 if(unlikely(is_compact)) {
2518 g_assert(field->field_size != 0);
2519 /* FIXME THIS IS A HUUUUUGE hack :
2520 * offset is between the compact_data field in struct LttEvent
2521 * and the address of the field root in the memory map.
2522 * ark. Both will stay at the same addresses while the event
2523 * is readable, so it's ok.
2524 */
2525 field->offset_root = 0;
2526 field->fixed_root = FIELD_FIXED;
2527 return 0;
2528 }
f104d082 2529
2530 switch(type->type_class) {
2531 case LTT_INT_FIXED:
2532 case LTT_UINT_FIXED:
2533 case LTT_POINTER:
2534 case LTT_CHAR:
2535 case LTT_UCHAR:
2536 case LTT_SHORT:
2537 case LTT_USHORT:
2538 case LTT_INT:
2539 case LTT_UINT:
2540 case LTT_LONG:
2541 case LTT_ULONG:
2542 case LTT_SIZE_T:
2543 case LTT_SSIZE_T:
2544 case LTT_OFF_T:
2545 case LTT_FLOAT:
2546 case LTT_ENUM:
743e50fd 2547 g_assert(field->field_size != 0);
f104d082 2548 /* Align offset on type size */
743e50fd 2549 *offset += ltt_align(*offset, get_alignment(field),
2550 fac->alignment);
f104d082 2551 /* remember offset */
2552 field->offset_root = *offset;
2553 field->fixed_root = FIELD_FIXED;
2554 /* Increment offset */
2555 *offset += field->field_size;
2556 return 0;
2557 break;
2558 case LTT_STRING:
2559 field->offset_root = *offset;
2560 field->fixed_root = FIELD_FIXED;
2561 return 1;
2562 break;
2563 case LTT_ARRAY:
2564 g_assert(type->fields->len == 1);
2565 {
2566 LttField *child = &g_array_index(type->fields, LttField, 0);
2567
743e50fd 2568 *offset += ltt_align(*offset, get_alignment(field),
2569 fac->alignment);
f104d082 2570
2571 /* remember offset */
2572 field->offset_root = *offset;
2573 field->array_offset = *offset;
2574 field->fixed_root = FIELD_FIXED;
2575
2576 /* Let the child be variable */
2577 //precompute_fields_offsets(tf, child, offset);
2578
2579 if(field->field_size != 0) {
2580 /* Increment offset */
2581 /* field_size is the array size in bytes */
2582 *offset += field->field_size;
2583 return 0;
2584 } else {
2585 return 1;
2586 }
2587 }
2588 break;
2589 case LTT_SEQUENCE:
2590 g_assert(type->fields->len == 2);
2591 {
2592 LttField *child;
2593 guint ret;
2594
743e50fd 2595 *offset += ltt_align(*offset, get_alignment(field),
2596 fac->alignment);
f104d082 2597
2598 /* remember offset */
2599 field->offset_root = *offset;
2600 field->fixed_root = FIELD_FIXED;
2601
2602 child = &g_array_index(type->fields, LttField, 0);
dd3a6d39 2603 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 2604 g_assert(ret == 0); /* Seq len cannot have variable len */
2605
2606 child = &g_array_index(type->fields, LttField, 1);
743e50fd 2607 *offset += ltt_align(*offset, get_alignment(child),
2608 fac->alignment);
f104d082 2609 field->array_offset = *offset;
743e50fd 2610 /* Let the child be variable. */
2611 //ret = precompute_fields_offsets(fac, child, offset);
f104d082 2612
2613 /* Cannot precompute fields offsets of sequence members, and has
2614 * variable length. */
2615 return 1;
2616 }
2617 break;
2618 case LTT_STRUCT:
2619 {
2620 LttField *child;
2621 guint i;
2622 gint ret=0;
2623
743e50fd 2624 *offset += ltt_align(*offset, get_alignment(field),
2625 fac->alignment);
f104d082 2626 /* remember offset */
2627 field->offset_root = *offset;
2628 field->fixed_root = FIELD_FIXED;
2629
2630 for(i=0; i< type->fields->len; i++) {
2631 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 2632 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 2633
2634 if(ret) break;
2635 }
2636 return ret;
2637 }
2638 break;
2639 case LTT_UNION:
2640 {
2641 LttField *child;
2642 guint i;
2643 gint ret=0;
2644
743e50fd 2645 *offset += ltt_align(*offset, get_alignment(field),
2646 fac->alignment);
f104d082 2647 /* remember offset */
2648 field->offset_root = *offset;
2649 field->fixed_root = FIELD_FIXED;
2650
2651 for(i=0; i< type->fields->len; i++) {
2652 *offset = field->offset_root;
2653 child = &g_array_index(type->fields, LttField, i);
dd3a6d39 2654 ret = precompute_fields_offsets(fac, child, offset, is_compact);
f104d082 2655
2656 if(ret) break;
2657 }
2658 *offset = field->offset_root + field->field_size;
2659 return ret;
2660 }
2661
2662 break;
2663 case LTT_NONE:
2664 default:
2665 g_error("precompute_fields_offsets : unknown type");
2666 return 1;
2667 }
2668
2669}
2670
3c165eaf 2671#endif //0
f104d082 2672
3c165eaf 2673#if 0
f104d082 2674/*****************************************************************************
2675 *Function name
2676 * precompute_offsets : set the precomputable offset of an event type
2677 *Input params
2678 * tf : tracefile
2679 * event : event type
2680 *
2681 ****************************************************************************/
743e50fd 2682void precompute_offsets(LttFacility *fac, LttEventType *event)
f104d082 2683{
2684 guint i;
2685 off_t offset = 0;
2686 gint ret;
2687
2688 /* First, compute the size of fixed size fields. Will determine size for
2689 * arrays, struct and unions, which is not done by the parser */
2690 for(i=0; i<event->fields->len; i++) {
2691 LttField *field = &g_array_index(event->fields, LttField, i);
743e50fd 2692 field_compute_static_size(fac, field);
f104d082 2693 }
2694
2695 /* Precompute all known offsets */
2696 for(i=0; i<event->fields->len; i++) {
2697 LttField *field = &g_array_index(event->fields, LttField, i);
dd3a6d39 2698 if(event->has_compact_data && i == 0)
2699 ret = precompute_fields_offsets(fac, field, &offset, 1);
2700 else
2701 ret = precompute_fields_offsets(fac, field, &offset, 0);
f104d082 2702 if(ret) break;
2703 }
2704}
3c165eaf 2705#endif //0
f104d082 2706
e4eced0f 2707
bbf28e50 2708
3aee1200 2709/*****************************************************************************
2710 *Function name
2711 * preset_field_type_size : set the fixed sizes of the field type
2712 *Input params
2713 * tf : tracefile
2714 * event_type : event type
2715 * offset_root : offset from the root
2716 * offset_parent : offset from the parent
2717 * fixed_root : Do we know a fixed offset to the root ?
2718 * fixed_parent : Do we know a fixed offset to the parent ?
2719 * field : field
2720 ****************************************************************************/
f104d082 2721
2722
2723
2724// preset the fixed size offsets. Calculate them just like genevent-new : an
2725// increment of a *to value that represents the offset from the start of the
2726// event data.
2727// The preset information is : offsets up to (and including) the first element
2728// of variable size. All subsequent fields must be flagged "VARIABLE OFFSET".
2729#if 0
3aee1200 2730void preset_field_type_size(LttTracefile *tf, LttEventType *event_type,
2731 off_t offset_root, off_t offset_parent,
2732 enum field_status *fixed_root, enum field_status *fixed_parent,
2733 LttField *field)
2734{
2735 enum field_status local_fixed_root, local_fixed_parent;
2736 guint i;
2737 LttType *type;
dfb73233 2738
3aee1200 2739 g_assert(field->fixed_root == FIELD_UNKNOWN);
2740 g_assert(field->fixed_parent == FIELD_UNKNOWN);
2741 g_assert(field->fixed_size == FIELD_UNKNOWN);
dfb73233 2742
3aee1200 2743 type = field->field_type;
2744
2745 field->fixed_root = *fixed_root;
2746 if(field->fixed_root == FIELD_FIXED)
2747 field->offset_root = offset_root;
2748 else
2749 field->offset_root = 0;
2750
2751 field->fixed_parent = *fixed_parent;
2752 if(field->fixed_parent == FIELD_FIXED)
2753 field->offset_parent = offset_parent;
2754 else
2755 field->offset_parent = 0;
2756
2757 size_t current_root_offset;
2758 size_t current_offset;
2759 enum field_status current_child_status, final_child_status;
2760 size_t max_size;
2761
2762 switch(type->type_class) {
83e160f2 2763 case LTT_INT_FIXED:
2764 case LTT_UINT_FIXED:
2765 case LTT_CHAR:
2766 case LTT_UCHAR:
2767 case LTT_SHORT:
2768 case LTT_USHORT:
3aee1200 2769 case LTT_INT:
2770 case LTT_UINT:
2771 case LTT_FLOAT:
2772 case LTT_ENUM:
2773 field->field_size = ltt_type_size(tf->trace, type);
2774 field->fixed_size = FIELD_FIXED;
2775 break;
2776 case LTT_POINTER:
2777 field->field_size = (off_t)event_type->facility->pointer_size;
2778 field->fixed_size = FIELD_FIXED;
2779 break;
2780 case LTT_LONG:
2781 case LTT_ULONG:
cb03932a 2782 field->field_size = (off_t)event_type->facility->long_size;
3aee1200 2783 field->fixed_size = FIELD_FIXED;
2784 break;
2785 case LTT_SIZE_T:
2786 case LTT_SSIZE_T:
2787 case LTT_OFF_T:
2788 field->field_size = (off_t)event_type->facility->size_t_size;
2789 field->fixed_size = FIELD_FIXED;
2790 break;
2791 case LTT_SEQUENCE:
2792 local_fixed_root = FIELD_VARIABLE;
2793 local_fixed_parent = FIELD_VARIABLE;
2794 preset_field_type_size(tf, event_type,
2795 0, 0,
2796 &local_fixed_root, &local_fixed_parent,
2797 field->child[0]);
2798 field->fixed_size = FIELD_VARIABLE;
2799 field->field_size = 0;
27304273 2800 *fixed_root = FIELD_VARIABLE;
2801 *fixed_parent = FIELD_VARIABLE;
3aee1200 2802 break;
2803 case LTT_STRING:
2804 field->fixed_size = FIELD_VARIABLE;
2805 field->field_size = 0;
27304273 2806 *fixed_root = FIELD_VARIABLE;
2807 *fixed_parent = FIELD_VARIABLE;
3aee1200 2808 break;
2809 case LTT_ARRAY:
2810 local_fixed_root = FIELD_VARIABLE;
2811 local_fixed_parent = FIELD_VARIABLE;
2812 preset_field_type_size(tf, event_type,
2813 0, 0,
2814 &local_fixed_root, &local_fixed_parent,
2815 field->child[0]);
2816 field->fixed_size = field->child[0]->fixed_size;
27304273 2817 if(field->fixed_size == FIELD_FIXED) {
3aee1200 2818 field->field_size = type->element_number * field->child[0]->field_size;
27304273 2819 } else {
3aee1200 2820 field->field_size = 0;
27304273 2821 *fixed_root = FIELD_VARIABLE;
2822 *fixed_parent = FIELD_VARIABLE;
2823 }
3aee1200 2824 break;
2825 case LTT_STRUCT:
2826 current_root_offset = field->offset_root;
2827 current_offset = 0;
2828 current_child_status = FIELD_FIXED;
2829 for(i=0;i<type->element_number;i++) {
2830 preset_field_type_size(tf, event_type,
2831 current_root_offset, current_offset,
2832 fixed_root, &current_child_status,
2833 field->child[i]);
2834 if(current_child_status == FIELD_FIXED) {
2835 current_root_offset += field->child[i]->field_size;
2836 current_offset += field->child[i]->field_size;
2837 } else {
2838 current_root_offset = 0;
2839 current_offset = 0;
2840 }
2841 }
2842 if(current_child_status != FIELD_FIXED) {
2843 *fixed_parent = current_child_status;
2844 field->field_size = 0;
2845 field->fixed_size = current_child_status;
2846 } else {
2847 field->field_size = current_offset;
2848 field->fixed_size = FIELD_FIXED;
2849 }
2850 break;
2851 case LTT_UNION:
2852 current_root_offset = field->offset_root;
2853 current_offset = 0;
2854 max_size = 0;
2855 final_child_status = FIELD_FIXED;
2856 for(i=0;i<type->element_number;i++) {
2857 enum field_status current_root_child_status = FIELD_FIXED;
2858 enum field_status current_child_status = FIELD_FIXED;
2859 preset_field_type_size(tf, event_type,
2860 current_root_offset, current_offset,
2861 &current_root_child_status, &current_child_status,
2862 field->child[i]);
2863 if(current_child_status != FIELD_FIXED)
2864 final_child_status = current_child_status;
2865 else
2866 max_size = max(max_size, field->child[i]->field_size);
2867 }
2868 if(final_child_status != FIELD_FIXED) {
62e4e7bf 2869 g_error("LTTV does not support variable size fields in unions.");
2870 /* This will stop the application. */
3aee1200 2871 *fixed_root = final_child_status;
2872 *fixed_parent = final_child_status;
2873 field->field_size = 0;
2874 field->fixed_size = current_child_status;
2875 } else {
2876 field->field_size = max_size;
2877 field->fixed_size = FIELD_FIXED;
2878 }
2879 break;
83e160f2 2880 case LTT_NONE:
2881 g_error("unexpected type NONE");
2882 break;
dfb73233 2883 }
2884
6cd62ccf 2885}
f104d082 2886#endif //0
3aee1200 2887
77175651 2888/*****************************************************************************
2889 *Function name
2890 * check_fields_compatibility : Check for compatibility between two fields :
2891 * do they use the same inner structure ?
2892 *Input params
2893 * event_type1 : event type
2894 * event_type2 : event type
2895 * field1 : field
2896 * field2 : field
2897 *Returns : 0 if identical
2898 * 1 if not.
2899 ****************************************************************************/
f104d082 2900// this function checks for equality of field types. Therefore, it does not use
2901// per se offsets. For instance, an aligned version of a structure is
2902// compatible with an unaligned version of the same structure.
3c165eaf 2903#if 0
f104d082 2904gint check_fields_compatibility(LttEventType *event_type1,
2905 LttEventType *event_type2,
2906 LttField *field1, LttField *field2)
2907{
2908 guint different = 0;
2312de30 2909 LttType *type1;
2910 LttType *type2;
f104d082 2911
2912 if(field1 == NULL) {
2913 if(field2 == NULL) goto end;
2914 else {
2915 different = 1;
2916 goto end;
2917 }
2918 } else if(field2 == NULL) {
2919 different = 1;
2920 goto end;
2921 }
2922
2312de30 2923 type1 = &field1->field_type;
2924 type2 = &field2->field_type;
f104d082 2925
2926 if(type1->type_class != type2->type_class) {
2927 different = 1;
2928 goto end;
2929 }
62e4e7bf 2930 if(type1->network != type2->network) {
2931 different = 1;
2932 goto end;
2933 }
f104d082 2934
2935 switch(type1->type_class) {
2936 case LTT_INT_FIXED:
2937 case LTT_UINT_FIXED:
2938 case LTT_POINTER:
2939 case LTT_CHAR:
2940 case LTT_UCHAR:
2941 case LTT_SHORT:
2942 case LTT_USHORT:
2943 case LTT_INT:
2944 case LTT_UINT:
2945 case LTT_LONG:
2946 case LTT_ULONG:
2947 case LTT_SIZE_T:
2948 case LTT_SSIZE_T:
2949 case LTT_OFF_T:
2950 case LTT_FLOAT:
2951 case LTT_ENUM:
2952 if(field1->field_size != field2->field_size)
2953 different = 1;
2954 break;
2955 case LTT_STRING:
2956 break;
2957 case LTT_ARRAY:
2958 {
2959 LttField *child1 = &g_array_index(type1->fields, LttField, 0);
2960 LttField *child2 = &g_array_index(type2->fields, LttField, 0);
2961
2962 if(type1->size != type2->size)
2963 different = 1;
2964 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2965 different = 1;
2966 }
2967 break;
2968 case LTT_SEQUENCE:
2969 {
2970 LttField *child1 = &g_array_index(type1->fields, LttField, 1);
2971 LttField *child2 = &g_array_index(type2->fields, LttField, 1);
2972
2973 if(check_fields_compatibility(event_type1, event_type2, child1, child2))
2974 different = 1;
2975 }
2976 break;
2977 case LTT_STRUCT:
2978 case LTT_UNION:
2979 {
2980 LttField *child;
2981 guint i;
2982
2983 if(type1->fields->len != type2->fields->len) {
2984 different = 1;
2985 goto end;
2986 }
2987
2988 for(i=0; i< type1->fields->len; i++) {
2312de30 2989 LttField *child1;
2990 LttField *child2;
f104d082 2991 child1 = &g_array_index(type1->fields, LttField, i);
2992 child2 = &g_array_index(type2->fields, LttField, i);
2993 different = check_fields_compatibility(event_type1,
2994 event_type2, child1, child2);
2995
2996 if(different) break;
2997 }
2998 }
2999 break;
3000 case LTT_NONE:
3001 default:
dd3a6d39 3002 g_error("check_fields_compatibility : unknown type");
f104d082 3003 }
3004
3005end:
3006 return different;
3007}
3c165eaf 3008#endif //0
f104d082 3009
3010#if 0
77175651 3011gint check_fields_compatibility(LttEventType *event_type1,
3012 LttEventType *event_type2,
3013 LttField *field1, LttField *field2)
3014{
3015 guint different = 0;
77175651 3016 guint i;
3017 LttType *type1;
3018 LttType *type2;
3019
3020 if(field1 == NULL) {
3021 if(field2 == NULL) goto end;
3022 else {
3023 different = 1;
3024 goto end;
3025 }
3026 } else if(field2 == NULL) {
3027 different = 1;
3028 goto end;
3029 }
3030
3031 g_assert(field1->fixed_root != FIELD_UNKNOWN);
3032 g_assert(field2->fixed_root != FIELD_UNKNOWN);
3033 g_assert(field1->fixed_parent != FIELD_UNKNOWN);
3034 g_assert(field2->fixed_parent != FIELD_UNKNOWN);
3035 g_assert(field1->fixed_size != FIELD_UNKNOWN);
3036 g_assert(field2->fixed_size != FIELD_UNKNOWN);
3037
3038 type1 = field1->field_type;
3039 type2 = field2->field_type;
3040
77175651 3041 if(type1->type_class != type2->type_class) {
3042 different = 1;
3043 goto end;
3044 }
3045 if(type1->element_name != type2->element_name) {
3046 different = 1;
3047 goto end;
3048 }
3049
3050 switch(type1->type_class) {
83e160f2 3051 case LTT_INT_FIXED:
3052 case LTT_UINT_FIXED:
3053 case LTT_POINTER:
3054 case LTT_CHAR:
3055 case LTT_UCHAR:
3056 case LTT_SHORT:
3057 case LTT_USHORT:
77175651 3058 case LTT_INT:
3059 case LTT_UINT:
3060 case LTT_FLOAT:
3061 case LTT_POINTER:
3062 case LTT_LONG:
3063 case LTT_ULONG:
3064 case LTT_SIZE_T:
3065 case LTT_SSIZE_T:
3066 case LTT_OFF_T:
3067 if(field1->field_size != field2->field_size) {
3068 different = 1;
3069 goto end;
3070 }
3071 break;
3072 case LTT_ENUM:
3073 if(type1->element_number != type2->element_number) {
3074 different = 1;
3075 goto end;
3076 }
3077 for(i=0;i<type1->element_number;i++) {
3078 if(type1->enum_strings[i] != type2->enum_strings[i]) {
3079 different = 1;
3080 goto end;
3081 }
3082 }
3083 break;
3084 case LTT_SEQUENCE:
3085 /* Two elements : size and child */
3086 g_assert(type1->element_number != type2->element_number);
3087 for(i=0;i<type1->element_number;i++) {
3088 if(check_fields_compatibility(event_type1, event_type2,
3089 field1->child[0], field2->child[0])) {
3090 different = 1;
3091 goto end;
3092 }
3093 }
3094 break;
3095 case LTT_STRING:
3096 break;
3097 case LTT_ARRAY:
3098 if(field1->field_size != field2->field_size) {
3099 different = 1;
3100 goto end;
3101 }
3102 /* Two elements : size and child */
3103 g_assert(type1->element_number != type2->element_number);
3104 for(i=0;i<type1->element_number;i++) {
3105 if(check_fields_compatibility(event_type1, event_type2,
3106 field1->child[0], field2->child[0])) {
3107 different = 1;
3108 goto end;
3109 }
3110 }
3111 break;
3112 case LTT_STRUCT:
3113 case LTT_UNION:
3114 if(type1->element_number != type2->element_number) {
3115 different = 1;
3116 break;
3117 }
3118 for(i=0;i<type1->element_number;i++) {
3119 if(check_fields_compatibility(event_type1, event_type2,
3120 field1->child[0], field2->child[0])) {
3121 different = 1;
3122 goto end;
3123 }
3124 }
3125 break;
3126 }
3127end:
3128 return different;
3129}
3aee1200 3130#endif //0
6cd62ccf 3131
f104d082 3132
6cd62ccf 3133/*****************************************************************************
3134 *Function name
eed2ef37 3135 * ltt_get_int : get an integer number
6cd62ccf 3136 *Input params
3aee1200 3137 * reverse_byte_order: must we reverse the byte order ?
6cd62ccf 3138 * size : the size of the integer
3aee1200 3139 * ptr : the data pointer
6cd62ccf 3140 *Return value
cf74a6f1 3141 * gint64 : a 64 bits integer
6cd62ccf 3142 ****************************************************************************/
3143
eed2ef37 3144gint64 ltt_get_int(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 3145{
3aee1200 3146 gint64 val;
cf74a6f1 3147
3148 switch(size) {
3aee1200 3149 case 1: val = *((gint8*)data); break;
3150 case 2: val = ltt_get_int16(reverse_byte_order, data); break;
3151 case 4: val = ltt_get_int32(reverse_byte_order, data); break;
3152 case 8: val = ltt_get_int64(reverse_byte_order, data); break;
3153 default: val = ltt_get_int64(reverse_byte_order, data);
3154 g_critical("get_int : integer size %d unknown", size);
cf74a6f1 3155 break;
3156 }
3157
3aee1200 3158 return val;
6cd62ccf 3159}
3aee1200 3160
6cd62ccf 3161/*****************************************************************************
3162 *Function name
eed2ef37 3163 * ltt_get_uint : get an unsigned integer number
6cd62ccf 3164 *Input params
3aee1200 3165 * reverse_byte_order: must we reverse the byte order ?
3166 * size : the size of the integer
3167 * ptr : the data pointer
3168 *Return value
3169 * guint64 : a 64 bits unsigned integer
6cd62ccf 3170 ****************************************************************************/
3171
eed2ef37 3172guint64 ltt_get_uint(gboolean reverse_byte_order, gint size, void *data)
6cd62ccf 3173{
3aee1200 3174 guint64 val;
3175
3176 switch(size) {
3177 case 1: val = *((gint8*)data); break;
3178 case 2: val = ltt_get_uint16(reverse_byte_order, data); break;
3179 case 4: val = ltt_get_uint32(reverse_byte_order, data); break;
3180 case 8: val = ltt_get_uint64(reverse_byte_order, data); break;
3181 default: val = ltt_get_uint64(reverse_byte_order, data);
3182 g_critical("get_uint : unsigned integer size %d unknown",
3183 size);
3184 break;
3185 }
3186
3187 return val;
6cd62ccf 3188}
3aee1200 3189
3190
a5dcde2f 3191/* get the node name of the system */
3192
3193char * ltt_trace_system_description_node_name (LttSystemDescription * s)
3194{
3195 return s->node_name;
3196}
3197
3198
3199/* get the domain name of the system */
3200
3201char * ltt_trace_system_description_domain_name (LttSystemDescription * s)
3202{
3203 return s->domain_name;
3204}
3205
3206
3207/* get the description of the system */
3208
3209char * ltt_trace_system_description_description (LttSystemDescription * s)
3210{
3211 return s->description;
3212}
3213
3214
bf33dd50 3215/* get the NTP corrected start time of the trace */
7bd563ec 3216LttTime ltt_trace_start_time(LttTrace *t)
a5dcde2f 3217{
7bd563ec 3218 return t->start_time;
a5dcde2f 3219}
3220
bf33dd50 3221/* get the monotonic start time of the trace */
3222LttTime ltt_trace_start_time_monotonic(LttTrace *t)
3223{
3224 return t->start_time_from_tsc;
3225}
3226
18206708 3227LttTracefile *ltt_tracefile_new()
3228{
afd57a3c 3229 LttTracefile *tf;
3230 tf = g_new(LttTracefile, 1);
3231 tf->event.tracefile = tf;
3232 return tf;
18206708 3233}
3234
3235void ltt_tracefile_destroy(LttTracefile *tf)
3236{
3237 g_free(tf);
3238}
3239
3240void ltt_tracefile_copy(LttTracefile *dest, const LttTracefile *src)
3241{
3242 *dest = *src;
3243}
3244
3aee1200 3245/* Before library loading... */
3246
3c165eaf 3247void init(void)
3aee1200 3248{
3249 LTT_FACILITY_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
3250 LTT_EVENT_NAME_HEARTBEAT = g_quark_from_string("heartbeat");
d1bb700c 3251 LTT_EVENT_NAME_HEARTBEAT_FULL = g_quark_from_string("heartbeat_full");
3aee1200 3252
3865ea09 3253 LTT_TRACEFILE_NAME_FACILITIES = g_quark_from_string("/control/facilities");
3aee1200 3254}
This page took 0.224549 seconds and 4 git commands to generate.