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