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