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