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