git-svn-id: http://ltt.polymtl.ca/svn@157 04897980-b3bd-0310-b5e0-8ef037075253
[lttv.git] / ltt / branches / poly / ltt / tracefile.c
CommitLineData
6cd62ccf 1#include <stdio.h>
2#include <fcntl.h>
3#include <sys/stat.h>
4#include <sys/types.h>
963b5f2d 5#include <dirent.h>
6cd62ccf 6#include <linux/errno.h>
7
fcdf0ec2 8#include <ltt/LTTTypes.h>
6cd62ccf 9#include "parser.h"
963b5f2d 10#include <ltt/trace.h>
6cd62ccf 11
963b5f2d 12#define DIR_NAME_SIZE 256
6cd62ccf 13
14/* set the offset of the fields belonging to the event,
15 need the information of the archecture */
40331ba8 16void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace *t);
6cd62ccf 17
18/* get the size of the field type according to the archtecture's
19 size and endian type(info of the archecture) */
40331ba8 20int getFieldtypeSize(LttTracefile * tf, LttEventType * evT, int offsetRoot,
21 int offsetParent, LttField *fld, void *evD, LttTrace* t);
6cd62ccf 22
23/* read a fixed size or a block information from the file (fd) */
24int readFile(int fd, void * buf, size_t size, char * mesg);
963b5f2d 25int readBlock(LttTracefile * tf, int whichBlock);
6cd62ccf 26
27/* calculate cycles per nsec for current block */
963b5f2d 28void getCyclePerNsec(LttTracefile * t);
6cd62ccf 29
30/* reinitialize the info of the block which is already in the buffer */
963b5f2d 31void updateTracefile(LttTracefile * tf);
6cd62ccf 32
33/* go to the next event */
963b5f2d 34int skipEvent(LttTracefile * t);
6cd62ccf 35
963b5f2d 36/* compare two time (LttTime), 0:t1=t2, -1:t1<t2, 1:t1>t2 */
37int timecmp(LttTime * t1, LttTime * t2);
6cd62ccf 38
6cd62ccf 39
6cd62ccf 40
41/*****************************************************************************
42 *Function name
963b5f2d 43 * ltt_tracefile_open : open a trace file, construct a LttTracefile
6cd62ccf 44 *Input params
963b5f2d 45 * t : the trace containing the tracefile
46 * fileName : path name of the trace file
6cd62ccf 47 *Return value
963b5f2d 48 * : a pointer to a tracefile
6cd62ccf 49 ****************************************************************************/
50
963b5f2d 51LttTracefile* ltt_tracefile_open(LttTrace * t, char * fileName)
6cd62ccf 52{
963b5f2d 53 LttTracefile * tf;
54 struct stat lTDFStat; /* Trace data file status */
55 BlockStart a_block_start;
6cd62ccf 56
963b5f2d 57 tf = g_new(LttTracefile, 1);
6cd62ccf 58
59 //open the file
963b5f2d 60 tf->name = g_strdup(fileName);
61 tf->trace = t;
6cd62ccf 62 tf->fd = open(fileName, O_RDONLY, 0);
63 if(tf->fd < 0){
64 g_error("Unable to open input data file %s\n", fileName);
65 }
66
67 // Get the file's status
68 if(fstat(tf->fd, &lTDFStat) < 0){
69 g_error("Unable to get the status of the input data file %s\n", fileName);
70 }
71
72 // Is the file large enough to contain a trace
963b5f2d 73 if(lTDFStat.st_size < sizeof(BlockStart) + EVENT_HEADER_SIZE){
8710c6c7 74 g_print("The input data file %s does not contain a trace\n", fileName);
663bd9ff 75 g_free(tf->name);
76 close(tf->fd);
77 g_free(tf);
8710c6c7 78 return NULL;
6cd62ccf 79 }
80
81 //store the size of the file
82 tf->file_size = lTDFStat.st_size;
963b5f2d 83 tf->block_size = t->system_description->ltt_block_size;
84 tf->block_number = tf->file_size / tf->block_size;
85 tf->which_block = 0;
6cd62ccf 86
87 //allocate memory to contain the info of a block
963b5f2d 88 tf->buffer = (void *) g_new(char, t->system_description->ltt_block_size);
6cd62ccf 89
963b5f2d 90 //read the first block
91 if(readBlock(tf,1)) exit(1);
6cd62ccf 92
93 return tf;
94}
95
6cd62ccf 96
97/*****************************************************************************
963b5f2d 98 *Open control and per cpu tracefiles
6cd62ccf 99 ****************************************************************************/
100
963b5f2d 101void ltt_tracefile_open_cpu(LttTrace *t, char * tracefile_name)
6cd62ccf 102{
963b5f2d 103 LttTracefile * tf;
104 tf = ltt_tracefile_open(t,tracefile_name);
4a6c8e36 105 if(!tf) return;
963b5f2d 106 t->per_cpu_tracefile_number++;
107 g_ptr_array_add(t->per_cpu_tracefiles, tf);
6cd62ccf 108}
109
963b5f2d 110void ltt_tracefile_open_control(LttTrace *t, char * control_name)
6cd62ccf 111{
963b5f2d 112 LttTracefile * tf;
113 LttEvent * ev;
114 LttFacility * f;
115 uint16_t evId;
116 void * pos;
117 FacilityLoad fLoad;
118 int i;
119
120 tf = ltt_tracefile_open(t,control_name);
8710c6c7 121 if(!tf) return;
963b5f2d 122 t->control_tracefile_number++;
123 g_ptr_array_add(t->control_tracefiles,tf);
124
125 //parse facilities tracefile to get base_id
542ceddd 126 if(strcmp(&control_name[strlen(control_name)-10],"facilities") ==0){
963b5f2d 127 while(1){
40331ba8 128 ev = ltt_tracefile_read(tf);
129 if(!ev)return; // end of file
130
131 if(ev->event_id == TRACE_FACILITY_LOAD){
132 pos = ev->data;
963b5f2d 133 fLoad.name = (char*)pos;
47a166fc 134 fLoad.checksum = *(LttChecksum*)(pos + strlen(fLoad.name));
135 fLoad.base_code = *(uint32_t*)(pos + strlen(fLoad.name) + sizeof(LttChecksum));
963b5f2d 136
137 for(i=0;i<t->facility_number;i++){
138 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
139 if(strcmp(f->name,fLoad.name)==0 && fLoad.checksum==f->checksum){
140 f->base_id = fLoad.base_code;
141 break;
142 }
143 }
144 if(i==t->facility_number)
145 g_error("Facility: %s, checksum: %d is not founded\n",
146 fLoad.name,fLoad.checksum);
40331ba8 147 }else if(ev->event_id == TRACE_BLOCK_START){
148 continue;
149 }else if(ev->event_id == TRACE_BLOCK_END){
150 break;
963b5f2d 151 }else g_error("Not valid facilities trace file\n");
62e55dd6 152 g_free(ev);
963b5f2d 153 }
154 }
6cd62ccf 155}
156
157/*****************************************************************************
158 *Function name
963b5f2d 159 * ltt_tracefile_close: close a trace file,
6cd62ccf 160 *Input params
963b5f2d 161 * t : tracefile which will be closed
6cd62ccf 162 ****************************************************************************/
163
963b5f2d 164void ltt_tracefile_close(LttTracefile *t)
6cd62ccf 165{
963b5f2d 166 g_free(t->name);
167 g_free(t->buffer);
663bd9ff 168 close(t->fd);
963b5f2d 169 g_free(t);
170}
6cd62ccf 171
6cd62ccf 172
963b5f2d 173/*****************************************************************************
174 *Get system information
175 ****************************************************************************/
176void getSystemInfo(LttSystemDescription* des, char * pathname)
177{
178 FILE * fp;
179 int i;
180 int entry_number = 15;
181 char buf[DIR_NAME_SIZE];
182 char description[4*DIR_NAME_SIZE];
183 char * ptr;
184
185 fp = fopen(pathname,"r");
186 if(!fp){
187 g_error("Can not open file : %s\n", pathname);
6cd62ccf 188 }
963b5f2d 189
190 while(fgets(buf,DIR_NAME_SIZE, fp)!= NULL){
191 ptr = buf;
192 while(isspace(*ptr)) ptr++;
193 if(strlen(ptr) == 0) continue;
194 break;
6cd62ccf 195 }
196
963b5f2d 197 if(strlen(ptr) == 0) g_error("Not a valid file: %s\n", pathname);
198 if(strncmp("<system",ptr,7) !=0)g_error("Not a valid file: %s\n", pathname);
199
200 for(i=0;i<entry_number;i++){
201 if(fgets(buf,DIR_NAME_SIZE, fp)== NULL)
202 g_error("Not a valid file: %s\n", pathname);
8710c6c7 203 buf[strlen(buf)-1] = '\0';
963b5f2d 204 ptr = buf;
205 while(isspace(*ptr)) ptr++;
206 switch(i){
207 case 0:
208 if(strncmp("node_name=",ptr,10)!=0)
209 g_error("Not a valid file: %s\n", pathname);
210 des->node_name = g_strdup(ptr+10);
211 break;
212 case 1:
213 if(strncmp("domainname=",ptr,11)!=0)
214 g_error("Not a valid file: %s\n", pathname);
215 des->domain_name = g_strdup(ptr+11);
216 break;
217 case 2:
218 if(strncmp("cpu=",ptr,4)!=0)
219 g_error("Not a valid file: %s\n", pathname);
220 des->nb_cpu = (unsigned)atoi(ptr+4);
221 break;
222 case 3:
223 if(strncmp("arch_size=",ptr,10)!=0)
224 g_error("Not a valid file: %s\n", pathname);
225 if(strcmp(ptr+10,"\"LP32\"") == 0) des->size = LTT_LP32;
226 else if(strcmp(ptr+10,"\"ILP32\"") == 0) des->size = LTT_ILP32;
227 else if(strcmp(ptr+10,"\"LP64\"") == 0) des->size = LTT_LP64;
228 else if(strcmp(ptr+10,"\"ILP64\"") == 0) des->size = LTT_ILP64;
229 else if(strcmp(ptr+10,"\"UNKNOWN\"") == 0) des->size = LTT_UNKNOWN;
230 break;
231 case 4:
232 if(strncmp("endian=",ptr,7)!=0)
233 g_error("Not a valid file: %s\n", pathname);
234 if(strcmp(ptr+7,"\"LITTLE_ENDIAN\"") == 0)
235 des->endian = LTT_LITTLE_ENDIAN;
236 else if(strcmp(ptr+7,"\"BIG_ENDIAN\"") == 0)
237 des->endian = LTT_BIG_ENDIAN;
238 break;
239 case 5:
240 if(strncmp("kernel_name=",ptr,12)!=0)
241 g_error("Not a valid file: %s\n", pathname);
242 des->kernel_name = g_strdup(ptr+12);
243 break;
244 case 6:
245 if(strncmp("kernel_release=",ptr,15)!=0)
246 g_error("Not a valid file: %s\n", pathname);
247 des->kernel_release = g_strdup(ptr+15);
248 break;
249 case 7:
250 if(strncmp("kernel_version=",ptr,15)!=0)
251 g_error("Not a valid file: %s\n", pathname);
252 des->kernel_version = g_strdup(ptr+15);
253 break;
254 case 8:
255 if(strncmp("machine=",ptr,8)!=0)
256 g_error("Not a valid file: %s\n", pathname);
257 des->machine = g_strdup(ptr+8);
258 break;
259 case 9:
260 if(strncmp("processor=",ptr,10)!=0)
261 g_error("Not a valid file: %s\n", pathname);
262 des->processor = g_strdup(ptr+10);
263 break;
264 case 10:
265 if(strncmp("hardware_platform=",ptr,18)!=0)
266 g_error("Not a valid file: %s\n", pathname);
267 des->hardware_platform = g_strdup(ptr+18);
268 break;
269 case 11:
270 if(strncmp("operating_system=",ptr,17)!=0)
271 g_error("Not a valid file: %s\n", pathname);
272 des->operating_system = g_strdup(ptr+17);
273 break;
274 case 12:
275 if(strncmp("ltt_major_version=",ptr,18)!=0)
276 g_error("Not a valid file: %s\n", pathname);
277 ptr += 18;
8710c6c7 278 // ptr++;//skip begining "
279 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
963b5f2d 280 des->ltt_major_version = (unsigned)atoi(ptr);
281 break;
282 case 13:
283 if(strncmp("ltt_minor_version=",ptr,18)!=0)
284 g_error("Not a valid file: %s\n", pathname);
285 ptr += 18;
8710c6c7 286 // ptr++;//skip begining "
287 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
963b5f2d 288 des->ltt_minor_version = (unsigned)atoi(ptr);
289 break;
290 case 14:
291 if(strncmp("ltt_block_size=",ptr,15)!=0)
292 g_error("Not a valid file: %s\n", pathname);
293 ptr += 15;
8710c6c7 294 // ptr++;//skip begining "
295 // ptr[strlen(ptr)-1] = '\0'; //get rid of the ending "
963b5f2d 296 des->ltt_block_size = (unsigned)atoi(ptr);
297 break;
298 default:
299 g_error("Not a valid file: %s\n", pathname);
300 }
6cd62ccf 301 }
302
963b5f2d 303 //get description
304 description[0] = '\0';
305 if(fgets(buf,DIR_NAME_SIZE, fp)== NULL)
306 g_error("Not a valid file: %s\n", pathname);
307 ptr = buf;
308 while(isspace(*ptr)) ptr++;
309 if(*ptr != '>') g_error("Not a valid file: %s\n", pathname);
310 while((ptr=fgets(buf,DIR_NAME_SIZE, fp))!= NULL){
311 ptr = buf;
312 while(isspace(*ptr)) ptr++;
313 if(strncmp("</system>",ptr,9) == 0 )break;
314 strcat(description, buf);
315 }
316 if(!ptr)g_error("Not a valid file: %s\n", pathname);
317 if(description[0] = '\0')des->description = NULL;
318 des->description = g_strdup(description);
6cd62ccf 319
963b5f2d 320 fclose(fp);
6cd62ccf 321}
322
323/*****************************************************************************
963b5f2d 324 *The following functions get facility/tracefile information
6cd62ccf 325 ****************************************************************************/
326
963b5f2d 327void getFacilityInfo(LttTrace *t, char* eventdefs)
6cd62ccf 328{
963b5f2d 329 DIR * dir;
330 struct dirent *entry;
331 char * ptr;
332 int i,j;
333 LttFacility * f;
334 LttEventType * et;
e8bb1a73 335 char name[DIR_NAME_SIZE];
963b5f2d 336
337 dir = opendir(eventdefs);
338 if(!dir) g_error("Can not open directory: %s\n", eventdefs);
339
340 while((entry = readdir(dir)) != NULL){
341 ptr = &entry->d_name[strlen(entry->d_name)-4];
342 if(strcmp(ptr,".xml") != 0) continue;
e8bb1a73 343 strcpy(name,eventdefs);
344 strcat(name,entry->d_name);
345 ltt_facility_open(t,name);
963b5f2d 346 }
347 closedir(dir);
348
963b5f2d 349 for(j=0;j<t->facility_number;j++){
8710c6c7 350 f = (LttFacility*)g_ptr_array_index(t->facilities, j);
963b5f2d 351 for(i=0; i<f->event_number; i++){
352 et = f->events[i];
40331ba8 353 setFieldsOffset(NULL, et, NULL, t);
963b5f2d 354 }
355 }
356}
6cd62ccf 357
963b5f2d 358void getControlFileInfo(LttTrace *t, char* control)
6cd62ccf 359{
963b5f2d 360 DIR * dir;
361 struct dirent *entry;
e8bb1a73 362 char name[DIR_NAME_SIZE];
963b5f2d 363
364 dir = opendir(control);
365 if(!dir) g_error("Can not open directory: %s\n", control);
366
367 while((entry = readdir(dir)) != NULL){
8710c6c7 368 if(strcmp(entry->d_name,"facilities") != 0 &&
369 strcmp(entry->d_name,"interrupts") != 0 &&
963b5f2d 370 strcmp(entry->d_name,"processes") != 0) continue;
371
e8bb1a73 372 strcpy(name,control);
373 strcat(name,entry->d_name);
374 ltt_tracefile_open_control(t,name);
963b5f2d 375 }
376 closedir(dir);
6cd62ccf 377}
378
963b5f2d 379void getCpuFileInfo(LttTrace *t, char* cpu)
6cd62ccf 380{
963b5f2d 381 DIR * dir;
382 struct dirent *entry;
e8bb1a73 383 char name[DIR_NAME_SIZE];
963b5f2d 384
385 dir = opendir(cpu);
386 if(!dir) g_error("Can not open directory: %s\n", cpu);
387
388 while((entry = readdir(dir)) != NULL){
8710c6c7 389 if(strcmp(entry->d_name,".") != 0 &&
963b5f2d 390 strcmp(entry->d_name,"..") != 0 ){
e8bb1a73 391 strcpy(name,cpu);
392 strcat(name,entry->d_name);
393 ltt_tracefile_open_cpu(t,name);
963b5f2d 394 }else continue;
395 }
396 closedir(dir);
6cd62ccf 397}
398
963b5f2d 399/*****************************************************************************
400 *A trace is specified as a pathname to the directory containing all the
401 *associated data (control tracefiles, per cpu tracefiles, event
402 *descriptions...).
403 *
404 *When a trace is closed, all the associated facilities, types and fields
405 *are released as well.
406 ****************************************************************************/
6cd62ccf 407
963b5f2d 408LttTrace *ltt_trace_open(char *pathname)
6cd62ccf 409{
963b5f2d 410 LttTrace * t;
411 LttSystemDescription * sys_description;
412 char eventdefs[DIR_NAME_SIZE];
413 char info[DIR_NAME_SIZE];
414 char control[DIR_NAME_SIZE];
415 char cpu[DIR_NAME_SIZE];
416 char tmp[DIR_NAME_SIZE];
417 gboolean has_slash = FALSE;
418
419 //establish the pathname to different directories
420 if(pathname[strlen(pathname)-1] == '/')has_slash = TRUE;
421 strcpy(eventdefs,pathname);
422 if(!has_slash)strcat(eventdefs,"/");
423 strcat(eventdefs,"eventdefs/");
424
425 strcpy(info,pathname);
426 if(!has_slash)strcat(info,"/");
427 strcat(info,"info/");
428
429 strcpy(control,pathname);
430 if(!has_slash)strcat(control,"/");
431 strcat(control,"control/");
432
433 strcpy(cpu,pathname);
434 if(!has_slash)strcat(cpu,"/");
435 strcat(cpu,"cpu/");
436
437 //new trace
438 t = g_new(LttTrace, 1);
439 sys_description = g_new(LttSystemDescription, 1);
440 t->pathname = g_strdup(pathname);
441 t->facility_number = 0;
442 t->control_tracefile_number = 0;
443 t->per_cpu_tracefile_number = 0;
444 t->system_description = sys_description;
445 t->control_tracefiles = g_ptr_array_new();
446 t->per_cpu_tracefiles = g_ptr_array_new();
447 t->facilities = g_ptr_array_new();
448 getDataEndianType(&(t->my_arch_size), &(t->my_arch_endian));
449
450 //get system description
451 strcpy(tmp,info);
452 strcat(tmp,"system.xml");
453 getSystemInfo(sys_description, tmp);
454
b333a76b 455 //get facilities info
456 getFacilityInfo(t,eventdefs);
457
963b5f2d 458 //get control tracefile info
459 getControlFileInfo(t,control);
460
461 //get cpu tracefile info
462 getCpuFileInfo(t,cpu);
463
963b5f2d 464 return t;
6cd62ccf 465}
466
963b5f2d 467void ltt_trace_close(LttTrace *t)
6cd62ccf 468{
963b5f2d 469 int i;
470 LttTracefile * tf;
471 LttFacility * f;
472
473 g_free(t->pathname);
474
475 //free system_description
476 g_free(t->system_description->description);
477 g_free(t->system_description->node_name);
478 g_free(t->system_description->domain_name);
479 g_free(t->system_description->kernel_name);
480 g_free(t->system_description->kernel_release);
481 g_free(t->system_description->kernel_version);
482 g_free(t->system_description->machine);
483 g_free(t->system_description->processor);
484 g_free(t->system_description->hardware_platform);
485 g_free(t->system_description->operating_system);
486 g_free(t->system_description);
487
488 //free control_tracefiles
489 for(i=0;i<t->control_tracefile_number;i++){
490 tf = (LttTracefile*)g_ptr_array_index(t->control_tracefiles,i);
491 ltt_tracefile_close(tf);
492 }
09ad4797 493 g_ptr_array_free(t->control_tracefiles, TRUE);
963b5f2d 494
495 //free per_cpu_tracefiles
496 for(i=0;i<t->per_cpu_tracefile_number;i++){
497 tf = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles,i);
498 ltt_tracefile_close(tf);
499 }
09ad4797 500 g_ptr_array_free(t->per_cpu_tracefiles, TRUE);
963b5f2d 501
502 //free facilities
503 for(i=0;i<t->facility_number;i++){
504 f = (LttFacility*)g_ptr_array_index(t->facilities,i);
505 ltt_facility_close(f);
506 }
09ad4797 507 g_ptr_array_free(t->facilities, TRUE);
963b5f2d 508
509 g_free(t);
09ad4797 510
511 g_blow_chunks();
6cd62ccf 512}
513
963b5f2d 514
6cd62ccf 515/*****************************************************************************
963b5f2d 516 *Get the system description of the trace
6cd62ccf 517 ****************************************************************************/
518
963b5f2d 519LttSystemDescription *ltt_trace_system_description(LttTrace *t)
6cd62ccf 520{
963b5f2d 521 return t->system_description;
6cd62ccf 522}
523
524/*****************************************************************************
963b5f2d 525 * The following functions discover the facilities of the trace
6cd62ccf 526 ****************************************************************************/
527
963b5f2d 528unsigned ltt_trace_facility_number(LttTrace *t)
529{
530 return (unsigned)(t->facility_number);
531}
532
533LttFacility *ltt_trace_facility_get(LttTrace *t, unsigned i)
6cd62ccf 534{
963b5f2d 535 return (LttFacility*)g_ptr_array_index(t->facilities, i);
6cd62ccf 536}
537
538/*****************************************************************************
539 *Function name
963b5f2d 540 * ltt_trace_facility_find : find facilities in the trace
6cd62ccf 541 *Input params
963b5f2d 542 * t : the trace
543 * name : facility name
544 *Output params
545 * position : position of the facility in the trace
6cd62ccf 546 *Return value
963b5f2d 547 * : the number of facilities
6cd62ccf 548 ****************************************************************************/
549
963b5f2d 550unsigned ltt_trace_facility_find(LttTrace *t, char *name, unsigned *position)
6cd62ccf 551{
963b5f2d 552 int i, count=0;
553 LttFacility * f;
554 for(i=0;i=t->facility_number;i++){
555 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
556 if(strcmp(f->name,name)==0){
557 count++;
558 if(count==1) *position = i;
559 }else{
560 if(count) break;
561 }
562 }
563 return count;
6cd62ccf 564}
565
566/*****************************************************************************
963b5f2d 567 * Functions to discover all the event types in the trace
6cd62ccf 568 ****************************************************************************/
569
963b5f2d 570unsigned ltt_trace_eventtype_number(LttTrace *t)
6cd62ccf 571{
963b5f2d 572 int i;
573 unsigned count = 0;
574 LttFacility * f;
575 for(i=0;i=t->facility_number;i++){
576 f = (LttFacility*)g_ptr_array_index(t->facilities, i);
577 count += f->event_number;
578 }
579 return count;
6cd62ccf 580}
581
963b5f2d 582LttFacility * ltt_trace_facility_by_id(LttTrace * trace, unsigned id)
6cd62ccf 583{
963b5f2d 584 LttFacility * facility;
585 int i;
586 for(i=0;i<trace->facility_number;i++){
587 facility = (LttFacility*) g_ptr_array_index(trace->facilities,i);
588 if(id >= facility->base_id &&
589 id < facility->base_id + facility->event_number)
590 break;
591 }
592 if(i==trace->facility_number) return NULL;
593 else return facility;
6cd62ccf 594}
595
963b5f2d 596LttEventType *ltt_trace_eventtype_get(LttTrace *t, unsigned evId)
6cd62ccf 597{
963b5f2d 598 LttFacility * f;
599 f = ltt_trace_facility_by_id(t,evId);
600 if(!f) return NULL;
601 return f->events[evId - f->base_id];
6cd62ccf 602}
603
604/*****************************************************************************
963b5f2d 605 *There is one "per cpu" tracefile for each CPU, numbered from 0 to
606 *the maximum number of CPU in the system. When the number of CPU installed
607 *is less than the maximum, some positions are unused. There are also a
608 *number of "control" tracefiles (facilities, interrupts...).
6cd62ccf 609 ****************************************************************************/
963b5f2d 610unsigned ltt_trace_control_tracefile_number(LttTrace *t)
6cd62ccf 611{
963b5f2d 612 return t->control_tracefile_number;
6cd62ccf 613}
614
963b5f2d 615unsigned ltt_trace_per_cpu_tracefile_number(LttTrace *t)
6cd62ccf 616{
963b5f2d 617 return t->per_cpu_tracefile_number;
6cd62ccf 618}
619
620/*****************************************************************************
963b5f2d 621 *It is possible to search for the tracefiles by name or by CPU position.
622 *The index within the tracefiles of the same type is returned if found
623 *and a negative value otherwise.
6cd62ccf 624 ****************************************************************************/
625
963b5f2d 626int ltt_trace_control_tracefile_find(LttTrace *t, char *name)
6cd62ccf 627{
963b5f2d 628 LttTracefile * tracefile;
629 int i;
630 for(i=0;i<t->control_tracefile_number;i++){
631 tracefile = (LttTracefile*)g_ptr_array_index(t->control_tracefiles, i);
632 if(strcmp(tracefile->name, name)==0)break;
633 }
634 if(i == t->control_tracefile_number) return -1;
635 return i;
6cd62ccf 636}
637
963b5f2d 638int ltt_trace_per_cpu_tracefile_find(LttTrace *t, unsigned i)
6cd62ccf 639{
963b5f2d 640 LttTracefile * tracefile;
641 int j, name;
642 for(j=0;j<t->per_cpu_tracefile_number;j++){
643 tracefile = (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, j);
644 name = atoi(tracefile->name);
645 if(name == (int)i)break;
646 }
647 if(j == t->per_cpu_tracefile_number) return -1;
648 return j;
6cd62ccf 649}
650
651/*****************************************************************************
963b5f2d 652 *Get a specific tracefile
6cd62ccf 653 ****************************************************************************/
654
963b5f2d 655LttTracefile *ltt_trace_control_tracefile_get(LttTrace *t, unsigned i)
6cd62ccf 656{
963b5f2d 657 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
658}
659
660LttTracefile *ltt_trace_per_cpu_tracefile_get(LttTrace *t, unsigned i)
661{
662 return (LttTracefile*)g_ptr_array_index(t->per_cpu_tracefiles, i);
6cd62ccf 663}
664
665/*****************************************************************************
963b5f2d 666 *Get the name of a tracefile
6cd62ccf 667 ****************************************************************************/
668
963b5f2d 669char *ltt_tracefile_name(LttTracefile *tf)
6cd62ccf 670{
963b5f2d 671 return tf->name;
6cd62ccf 672}
673
674/*****************************************************************************
675 *Function name
676 * ltt_tracefile_seek_time: seek to the first event of the trace with time
677 * larger or equal to time
678 *Input params
679 * t : tracefile
680 * time : criteria of the time
6cd62ccf 681 ****************************************************************************/
682
963b5f2d 683void ltt_tracefile_seek_time(LttTracefile *t, LttTime time)
6cd62ccf 684{
685 int err;
963b5f2d 686 LttTime lttTime;
687 int headTime = timecmp(&(t->a_block_start->time), &time);
688 int tailTime = timecmp(&(t->a_block_end->time), &time);
62e55dd6 689 LttEvent * ev;
690
6cd62ccf 691 if(headTime < 0 && tailTime > 0){
692 lttTime = getEventTime(t);
693 err = timecmp(&lttTime, &time);
963b5f2d 694 if(err > 0){
40331ba8 695 if(t->which_event==2 || timecmp(&t->prev_event_time,&time)<0){
963b5f2d 696 return;
697 }else{
6cd62ccf 698 updateTracefile(t);
699 return ltt_tracefile_seek_time(t, time);
700 }
701 }else if(err < 0){
1a3b8cbd 702 while(1){
703 ev = ltt_tracefile_read(t);
704 if(ev == NULL){
705 g_print("End of file\n");
706 return;
707 }
708 g_free(ev);
709 lttTime = getEventTime(t);
710 err = timecmp(&lttTime, &time);
711 if(err >= 0)return;
963b5f2d 712 }
963b5f2d 713 }else return;
6cd62ccf 714 }else if(headTime > 0){
715 if(t->which_block == 1){
716 updateTracefile(t);
717 }else{
40331ba8 718 if(timecmp(&(t->prev_block_end_time),&time) >= 0 ){
6cd62ccf 719 err=readBlock(t,t->which_block-1);
963b5f2d 720 if(err) g_error("Can not read tracefile: %s\n", t->name);
6cd62ccf 721 return ltt_tracefile_seek_time(t, time) ;
722 }else{
723 updateTracefile(t);
724 }
725 }
40331ba8 726 }else if(tailTime < 0){
6cd62ccf 727 if(t->which_block != t->block_number){
728 err=readBlock(t,t->which_block+1);
963b5f2d 729 if(err) g_error("Can not read tracefile: %s\n", t->name);
730 }else {
25975c93 731 g_print("End of file\n");
963b5f2d 732 return;
733 }
6cd62ccf 734 if(tailTime < 0) return ltt_tracefile_seek_time(t, time);
735 }else if(headTime == 0){
736 updateTracefile(t);
40331ba8 737 }else if(tailTime == 0){
1a3b8cbd 738 t->cur_event_pos = t->a_block_end - EVENT_HEADER_SIZE;
62e55dd6 739 t->current_event_time = time;
740 t->cur_heart_beat_number = 0;
741 t->prev_event_time.tv_sec = 0;
742 t->prev_event_time.tv_nsec = 0;
40331ba8 743 return;
6cd62ccf 744 }
6cd62ccf 745}
746
747/*****************************************************************************
748 *Function name
40331ba8 749 * ltt_tracefile_read : read the current event, set the pointer to the next
6cd62ccf 750 *Input params
751 * t : tracefile
752 *Return value
963b5f2d 753 * LttEvent * : an event to be processed
6cd62ccf 754 ****************************************************************************/
755
963b5f2d 756LttEvent *ltt_tracefile_read(LttTracefile *t)
6cd62ccf 757{
963b5f2d 758 LttEvent * lttEvent = (LttEvent *)g_new(LttEvent, 1);
759 int err;
6cd62ccf 760
963b5f2d 761 lttEvent->event_id = (int)(*(uint16_t *)(t->cur_event_pos));
762 if(lttEvent->event_id == TRACE_TIME_HEARTBEAT)
763 t->cur_heart_beat_number++;
6cd62ccf 764
40331ba8 765 t->prev_event_time = t->current_event_time;
62e55dd6 766 // t->current_event_time = getEventTime(t);
6cd62ccf 767
963b5f2d 768 lttEvent->time_delta = *(uint32_t*)(t->cur_event_pos + EVENT_ID_SIZE);
769 lttEvent->event_time = t->current_event_time;
770
771 lttEvent->event_cycle_count = ((uint64_t)1)<<32 * t->cur_heart_beat_number
772 + lttEvent->time_delta;
773
6cd62ccf 774 lttEvent->tracefile = t;
775 lttEvent->data = t->cur_event_pos + EVENT_HEADER_SIZE;
908f42fa 776 lttEvent->which_block = t->which_block;
777 lttEvent->which_event = t->which_event;
6cd62ccf 778
40331ba8 779 //update the fields of the current event and go to the next event
780 err = skipEvent(t);
663bd9ff 781 if(err == ENOMEM){
782 g_free(lttEvent);
783 return NULL;
784 }
99ff36b1 785 if(err == ENOENT) return lttEvent;
40331ba8 786 if(err == ERANGE) g_error("event id is out of range\n");
787 if(err)g_error("Can not read tracefile\n");
788
6cd62ccf 789 return lttEvent;
790}
791
792/****************************************************************************
793 *Function name
794 * readFile : wrap function to read from a file
795 *Input Params
796 * fd : file descriptor
797 * buf : buf to contain the content
798 * size : number of bytes to be read
799 * mesg : message to be printed if some thing goes wrong
800 *return value
801 * 0 : success
802 * EIO : can not read from the file
803 ****************************************************************************/
804
805int readFile(int fd, void * buf, size_t size, char * mesg)
806{
807 ssize_t nbBytes;
808 nbBytes = read(fd, buf, size);
809 if(nbBytes != size){
810 printf("%s\n",mesg);
811 return EIO;
812 }
813 return 0;
814}
815
816/****************************************************************************
817 *Function name
818 * readBlock : read a block from the file
819 *Input Params
820 * lttdes : ltt trace file
821 * whichBlock : the block which will be read
822 *return value
823 * 0 : success
824 * EINVAL : lseek fail
825 * EIO : can not read from the file
826 ****************************************************************************/
827
963b5f2d 828int readBlock(LttTracefile * tf, int whichBlock)
6cd62ccf 829{
830 off_t nbBytes;
831 uint32_t lostSize;
832
833 if(whichBlock - tf->which_block == 1 && tf->which_block != 0){
963b5f2d 834 tf->prev_block_end_time = tf->a_block_end->time;
40331ba8 835 tf->prev_event_time = tf->a_block_end->time;
6cd62ccf 836 }else{
837 tf->prev_block_end_time.tv_sec = 0;
838 tf->prev_block_end_time.tv_nsec = 0;
40331ba8 839 tf->prev_event_time.tv_sec = 0;
840 tf->prev_event_time.tv_nsec = 0;
6cd62ccf 841 }
6cd62ccf 842
963b5f2d 843 nbBytes=lseek(tf->fd,(off_t)((whichBlock-1)*tf->block_size), SEEK_SET);
6cd62ccf 844 if(nbBytes == -1) return EINVAL;
845
963b5f2d 846 if(readFile(tf->fd,tf->buffer,tf->block_size,"Unable to read a block"))
847 return EIO;
6cd62ccf 848
963b5f2d 849 tf->a_block_start=(BlockStart *) (tf->buffer + EVENT_HEADER_SIZE);
850 lostSize = *(uint32_t*)(tf->buffer + tf->block_size - sizeof(uint32_t));
851 tf->a_block_end=(BlockEnd *)(tf->buffer + tf->block_size -
852 lostSize + EVENT_HEADER_SIZE);
6cd62ccf 853
6cd62ccf 854 tf->which_block = whichBlock;
963b5f2d 855 tf->which_event = 1;
40331ba8 856 tf->cur_event_pos = tf->buffer;//the beginning of the block, block start ev
6cd62ccf 857 tf->cur_heart_beat_number = 0;
963b5f2d 858
6cd62ccf 859 getCyclePerNsec(tf);
860
62e55dd6 861 tf->current_event_time = getEventTime(tf);
40331ba8 862
6cd62ccf 863 return 0;
864}
865
866/*****************************************************************************
867 *Function name
868 * updateTracefile : reinitialize the info of the block which is already
869 * in the buffer
870 *Input params
871 * tf : tracefile
872 ****************************************************************************/
873
963b5f2d 874void updateTracefile(LttTracefile * tf)
6cd62ccf 875{
963b5f2d 876 tf->which_event = 1;
40331ba8 877 tf->cur_event_pos = tf->buffer;
62e55dd6 878 tf->current_event_time = getEventTime(tf);
6cd62ccf 879 tf->cur_heart_beat_number = 0;
880
881 tf->prev_event_time.tv_sec = 0;
882 tf->prev_event_time.tv_nsec = 0;
883}
884
885/*****************************************************************************
886 *Function name
887 * skipEvent : go to the next event, update the fields of the current event
888 *Input params
889 * t : tracefile
890 *return value
891 * 0 : success
892 * EINVAL : lseek fail
893 * EIO : can not read from the file
99ff36b1 894 * ENOMEM : end of file
895 * ENOENT : last event
6cd62ccf 896 * ERANGE : event id is out of range
897 ****************************************************************************/
898
963b5f2d 899int skipEvent(LttTracefile * t)
6cd62ccf 900{
901 int evId, err;
902 void * evData;
963b5f2d 903 LttEventType * evT;
904 LttField * rootFld;
99ff36b1 905 static int evCount = 0;
906
907 if(evCount){
908 if(t->which_block == t->block_number &&
909 evCount == t->which_event){
910 return ENOMEM;
911 }else evCount = 0;
912 }
6cd62ccf 913
963b5f2d 914 evId = (int)(*(uint16_t *)(t->cur_event_pos));
6cd62ccf 915 evData = t->cur_event_pos + EVENT_HEADER_SIZE;
6cd62ccf 916
908f42fa 917 evT = ltt_trace_eventtype_get(t->trace,(unsigned)evId);
47a166fc 918
908f42fa 919 if(evT) rootFld = evT->root_field;
920 else return ERANGE;
6cd62ccf 921
908f42fa 922 if(rootFld){
923 //event has string/sequence or the last event is not the same event
924 if((evT->latest_block!=t->which_block || evT->latest_event!=t->which_event)
925 && rootFld->field_fixed == 0){
926 setFieldsOffset(t, evT, evData, t->trace);
47a166fc 927 }
908f42fa 928 t->cur_event_pos += EVENT_HEADER_SIZE + rootFld->field_size;
929 }else t->cur_event_pos += EVENT_HEADER_SIZE;
930
931 evT->latest_block = t->which_block;
932 evT->latest_event = t->which_event;
933
6cd62ccf 934 //the next event is in the next block
963b5f2d 935 if(evId == TRACE_BLOCK_END){
99ff36b1 936 if(t->which_block == t->block_number){
937 t->which_event++;
938 evCount = t->which_event;
939 return ENOENT;
940 }
6cd62ccf 941 err = readBlock(t, t->which_block + 1);
942 if(err) return err;
943 }else{
944 t->which_event++;
62e55dd6 945 t->current_event_time = getEventTime(t);
6cd62ccf 946 }
947
948 return 0;
949}
950
951/*****************************************************************************
952 *Function name
953 * getCyclePerNsec : calculate cycles per nsec for current block
954 *Input Params
955 * t : tracefile
956 ****************************************************************************/
957
963b5f2d 958void getCyclePerNsec(LttTracefile * t)
6cd62ccf 959{
963b5f2d 960 LttTime lBufTotalTime; /* Total time for this buffer */
961 LttCycleCount lBufTotalNSec; /* Total time for this buffer in nsecs */
962 LttCycleCount lBufTotalCycle;/* Total cycles for this buffer */
6cd62ccf 963
964 /* Calculate the total time for this buffer */
963b5f2d 965 TimeSub(lBufTotalTime,t->a_block_end->time, t->a_block_start->time);
6cd62ccf 966
967 /* Calculate the total cycles for this bufffer */
963b5f2d 968 lBufTotalCycle = t->a_block_end->cycle_count
969 - t->a_block_start->cycle_count;
6cd62ccf 970
971 /* Convert the total time to nsecs */
972 lBufTotalNSec = lBufTotalTime.tv_sec * 1000000000 + lBufTotalTime.tv_nsec;
973
974 t->cycle_per_nsec = (double)lBufTotalCycle / (double)lBufTotalNSec;
975}
976
977/****************************************************************************
978 *Function name
979 * getEventTime : obtain the time of an event
980 *Input params
981 * tf : tracefile
982 *Return value
963b5f2d 983 * LttTime : the time of the event
6cd62ccf 984 ****************************************************************************/
985
963b5f2d 986LttTime getEventTime(LttTracefile * tf)
6cd62ccf 987{
963b5f2d 988 LttTime time;
989 LttCycleCount cycle_count; // cycle count for the current event
990 LttCycleCount lEventTotalCycle; // Total cycles from start for event
991 double lEventNSec; // Total usecs from start for event
992 LttTime lTimeOffset; // Time offset in struct LttTime
40331ba8 993 uint16_t evId;
6cd62ccf 994
40331ba8 995 evId = *(uint16_t*)tf->cur_event_pos;
996 if(evId == TRACE_BLOCK_START)
997 return tf->a_block_start->time;
998 else if(evId == TRACE_BLOCK_END)
999 return tf->a_block_end->time;
1000
1001
963b5f2d 1002 // Calculate total time in cycles from start of buffer for this event
1003 cycle_count = (LttCycleCount)*(uint32_t*)(tf->cur_event_pos + EVENT_ID_SIZE);
6cd62ccf 1004 if(tf->cur_heart_beat_number)
1005 cycle_count += ((uint64_t)1)<<32 * tf->cur_heart_beat_number;
963b5f2d 1006 lEventTotalCycle = cycle_count - tf->a_block_start->cycle_count;
6cd62ccf 1007
963b5f2d 1008 // Convert it to nsecs
6cd62ccf 1009 lEventNSec = lEventTotalCycle / tf->cycle_per_nsec;
1010
963b5f2d 1011 // Determine offset in struct LttTime
6cd62ccf 1012 lTimeOffset.tv_nsec = (long)lEventNSec % 1000000000;
1013 lTimeOffset.tv_sec = (long)lEventNSec / 1000000000;
1014
963b5f2d 1015 TimeAdd(time, tf->a_block_start->time, lTimeOffset);
6cd62ccf 1016
1017 return time;
1018}
1019
1020/*****************************************************************************
1021 *Function name
1022 * setFieldsOffset : set offset of the fields
1023 *Input params
1024 * tracefile : opened trace file
1025 * evT : the event type
1026 * evD : event data, it may be NULL
1027 ****************************************************************************/
1028
40331ba8 1029void setFieldsOffset(LttTracefile *tf,LttEventType *evT,void *evD,LttTrace* t)
6cd62ccf 1030{
963b5f2d 1031 LttField * rootFld = evT->root_field;
6cd62ccf 1032 // rootFld->base_address = evD;
1033
8710c6c7 1034 if(rootFld)
1035 rootFld->field_size = getFieldtypeSize(tf, evT, 0,0,rootFld, evD,t);
6cd62ccf 1036}
1037
1038/*****************************************************************************
1039 *Function name
1040 * getFieldtypeSize: get the size of the field type (primitive type)
1041 *Input params
1042 * tracefile : opened trace file
1043 * evT : event type
1044 * offsetRoot : offset from the root
1045 * offsetParent : offset from the parrent
1046 * fld : field
1047 * evD : event data, it may be NULL
1048 *Return value
1049 * int : size of the field
1050 ****************************************************************************/
1051
963b5f2d 1052int getFieldtypeSize(LttTracefile * t, LttEventType * evT, int offsetRoot,
40331ba8 1053 int offsetParent, LttField * fld, void *evD, LttTrace *trace)
6cd62ccf 1054{
1055 int size, size1, element_number, i, offset1, offset2;
963b5f2d 1056 LttType * type = fld->field_type;
6cd62ccf 1057
8710c6c7 1058 if(t){
963b5f2d 1059 if(evT->latest_block==t->which_block && evT->latest_event==t->which_event){
1060 return fld->field_size;
1061 }
1062 }
6cd62ccf 1063
1064 if(fld->field_fixed == 1){
1065 if(fld == evT->root_field) return fld->field_size;
1066 }
1067
1068 if(type->type_class != LTT_STRUCT && type->type_class != LTT_ARRAY &&
1069 type->type_class != LTT_SEQUENCE && type->type_class != LTT_STRING){
1070 if(fld->field_fixed == -1){
40331ba8 1071 size = (int) ltt_type_size(trace, type);
6cd62ccf 1072 fld->field_fixed = 1;
1073 }else size = fld->field_size;
1074
1075 }else if(type->type_class == LTT_ARRAY){
1076 element_number = (int) type->element_number;
1077 if(fld->field_fixed == -1){
40331ba8 1078 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1079 if(size == 0){ //has string or sequence
1080 fld->field_fixed = 0;
1081 }else{
1082 fld->field_fixed = 1;
1083 size *= element_number;
1084 }
1085 }else if(fld->field_fixed == 0){// has string or sequence
1086 size = 0;
1087 for(i=0;i<element_number;i++){
1088 size += getFieldtypeSize(t, evT, offsetRoot+size,size,
40331ba8 1089 fld->child[0], evD+size, trace);
6cd62ccf 1090 }
1091 }else size = fld->field_size;
1092
1093 }else if(type->type_class == LTT_SEQUENCE){
40331ba8 1094 size1 = (int) ltt_type_size(trace, type);
6cd62ccf 1095 if(fld->field_fixed == -1){
908f42fa 1096 fld->sequ_number_size = size1;
6cd62ccf 1097 fld->field_fixed = 0;
40331ba8 1098 size = getFieldtypeSize(t, evT, offsetRoot,0,fld->child[0], NULL, trace);
6cd62ccf 1099 fld->element_size = size;
1100 }else{//0: sequence
1101 element_number = getIntNumber(size1,evD);
1102 type->element_number = element_number;
1103 if(fld->element_size > 0){
1104 size = element_number * fld->element_size;
1105 }else{//sequence has string or sequence
1106 size = 0;
1107 for(i=0;i<element_number;i++){
1108 size += getFieldtypeSize(t, evT, offsetRoot+size+size1,size+size1,
40331ba8 1109 fld->child[0], evD+size+size1, trace);
6cd62ccf 1110 }
1111 }
1112 size += size1;
1113 }
1114
1115 }else if(type->type_class == LTT_STRING){
1116 size = 0;
1117 if(fld->field_fixed == -1){
1118 fld->field_fixed = 0;
1119 }else{//0: string
47a166fc 1120 size = strlen((char*)evD) + 1; //include end : '\0'
6cd62ccf 1121 }
1122
1123 }else if(type->type_class == LTT_STRUCT){
1124 element_number = (int) type->element_number;
1125 size = 0;
1126 if(fld->field_fixed == -1){
1127 offset1 = offsetRoot;
1128 offset2 = 0;
1129 for(i=0;i<element_number;i++){
40331ba8 1130 size1=getFieldtypeSize(t, evT,offset1,offset2, fld->child[i], NULL, trace);
6cd62ccf 1131 if(size1 > 0 && size >= 0){
1132 size += size1;
1133 if(offset1 >= 0) offset1 += size1;
1134 offset2 += size1;
1135 }else{
1136 size = -1;
1137 offset1 = -1;
1138 offset2 = -1;
1139 }
1140 }
1141 if(size == -1){
1142 fld->field_fixed = 0;
1143 size = 0;
1144 }else fld->field_fixed = 1;
1145 }else if(fld->field_fixed == 0){
1146 offset1 = offsetRoot;
1147 offset2 = 0;
1148 for(i=0;i<element_number;i++){
40331ba8 1149 size=getFieldtypeSize(t,evT,offset1,offset2,fld->child[i],evD+offset2, trace);
6cd62ccf 1150 offset1 += size;
1151 offset2 += size;
1152 }
1153 size = offset2;
1154 }else size = fld->field_size;
1155 }
1156
1157 fld->offset_root = offsetRoot;
1158 fld->offset_parent = offsetParent;
1159 if(!evD){
1160 fld->fixed_root = (offsetRoot==-1) ? 0 : 1;
1161 fld->fixed_parent = (offsetParent==-1) ? 0 : 1;
1162 }
1163 fld->field_size = size;
1164
1165 return size;
1166}
1167
1168/*****************************************************************************
1169 *Function name
1170 * timecmp : compare two time
1171 *Input params
1172 * t1 : first time
1173 * t2 : second time
1174 *Return value
1175 * int : 0: t1 == t2; -1: t1 < t2; 1: t1 > t2
1176 ****************************************************************************/
1177
963b5f2d 1178int timecmp(LttTime * t1, LttTime * t2)
6cd62ccf 1179{
963b5f2d 1180 LttTime T;
6cd62ccf 1181 TimeSub(T, *t1, *t2);
1182 if(T.tv_sec == 0 && T.tv_nsec == 0) return 0;
1183 else if(T.tv_sec > 0 || (T.tv_sec==0 && T.tv_nsec > 0)) return 1;
1184 else return -1;
1185}
1186
1187/*****************************************************************************
1188 *Function name
1189 * getIntNumber : get an integer number
1190 *Input params
1191 * size : the size of the integer
1192 * evD : the event data
1193 *Return value
1194 * int : an integer
1195 ****************************************************************************/
1196
1197int getIntNumber(int size, void *evD)
1198{
1199 int64_t i;
1200 if(size == 1) i = *(int8_t *)evD;
1201 else if(size == 2) i = *(int16_t *)evD;
1202 else if(size == 4) i = *(int32_t *)evD;
1203 else if(size == 8) i = *(int64_t *)evD;
1204
1205 return (int) i;
1206}
1207
1208/*****************************************************************************
1209 *Function name
1210 * getDataEndianType : get the data type size and endian type of the local
1211 * machine
1212 *Input params
1213 * size : size of data type
1214 * endian : endian type, little or big
1215 ****************************************************************************/
1216
963b5f2d 1217void getDataEndianType(LttArchSize * size, LttArchEndian * endian)
6cd62ccf 1218{
1219 int i = 1;
1220 char c = (char) i;
1221 int sizeInt=sizeof(int), sizeLong=sizeof(long), sizePointer=sizeof(void *);
1222
1223 if(c == 1) *endian = LTT_LITTLE_ENDIAN;
1224 else *endian = LTT_BIG_ENDIAN;
1225
1226 if(sizeInt == 2 && sizeLong == 4 && sizePointer == 4)
1227 *size = LTT_LP32;
1228 else if(sizeInt == 4 && sizeLong == 4 && sizePointer == 4)
1229 *size = LTT_ILP32;
1230 else if(sizeInt == 4 && sizeLong == 8 && sizePointer == 8)
1231 *size = LTT_LP64;
1232 else if(sizeInt == 8 && sizeLong == 8 && sizePointer == 8)
1233 *size = LTT_ILP64;
1234 else *size = LTT_UNKNOWN;
1235}
1236
This page took 0.078519 seconds and 4 git commands to generate.