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