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