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