1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "../surf/surf_private.h"
12 #include "../simix/smx_private.h"
13 #include "../xbt/mmalloc/mmprivate.h"
15 #include "mc_private.h"
16 #include "xbt/automaton.h"
19 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
21 "Logging specific to MC (global)");
23 /* Configuration support */
24 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
26 extern int _surf_init_status;
27 void _mc_cfg_cb_reduce(const char *name, int pos) {
28 if (_surf_init_status && !_surf_do_model_check) {
29 xbt_die("You are specifying a reduction strategy after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
31 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
32 if (!strcasecmp(val,"none")) {
33 mc_reduce_kind = e_mc_reduce_none;
34 } else if (!strcasecmp(val,"dpor")) {
35 mc_reduce_kind = e_mc_reduce_dpor;
37 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
39 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
42 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
43 if (_surf_init_status && !_surf_do_model_check) {
44 xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
46 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
47 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
49 void _mc_cfg_cb_property(const char *name, int pos) {
50 if (_surf_init_status && !_surf_do_model_check) {
51 xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
53 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
54 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
58 /* MC global data structures */
60 mc_state_t mc_current_state = NULL;
61 char mc_replay_mode = FALSE;
62 double *mc_time = NULL;
63 mc_snapshot_t initial_snapshot = NULL;
68 xbt_fifo_t mc_stack_safety = NULL;
69 mc_stats_t mc_stats = NULL;
73 mc_stats_pair_t mc_stats_pair = NULL;
74 xbt_fifo_t mc_stack_liveness = NULL;
75 mc_snapshot_t initial_snapshot_liveness = NULL;
79 xbt_dict_t mc_local_variables = NULL;
81 /* Ignore mechanism */
82 extern xbt_dynar_t mc_comparison_ignore;
83 extern xbt_dynar_t stacks_areas;
85 xbt_automaton_t _mc_property_automaton = NULL;
87 /* Static functions */
89 static void MC_assert_pair(int prop);
90 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
91 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
93 void MC_do_the_modelcheck_for_real() {
94 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
95 if (mc_reduce_kind==e_mc_reduce_unset)
96 mc_reduce_kind=e_mc_reduce_dpor;
98 XBT_INFO("Check a safety property");
103 if (mc_reduce_kind==e_mc_reduce_unset)
104 mc_reduce_kind=e_mc_reduce_none;
106 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
107 MC_automaton_load(_surf_mc_property_file);
108 MC_modelcheck_liveness();
113 * \brief Initialize the model-checker data structures
115 void MC_init_safety(void)
118 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
120 /* Check if MC is already initialized */
121 if (initial_snapshot)
124 mc_time = xbt_new0(double, simix_process_maxpid);
126 /* Initialize the data structures that must be persistent across every
127 iteration of the model-checker (in RAW memory) */
131 /* Initialize statistics */
132 mc_stats = xbt_new0(s_mc_stats_t, 1);
133 mc_stats->state_size = 1;
135 /* Create exploration stack */
136 mc_stack_safety = xbt_fifo_new();
143 /* Save the initial state */
144 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
145 MC_take_snapshot(initial_snapshot);
156 void MC_compare(void){
161 void MC_modelcheck(void)
168 void MC_modelcheck_liveness(){
170 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
173 XBT_INFO("Start init mc");
175 mc_time = xbt_new0(double, simix_process_maxpid);
177 /* mc_time refers to clock for each process -> ignore it for heap comparison */
179 for(i = 0; i<simix_process_maxpid; i++)
180 MC_ignore(&(mc_time[i]), sizeof(double));
184 /* Initialize the data structures that must be persistent across every
185 iteration of the model-checker (in RAW memory) */
189 char *ls_path = get_libsimgrid_path();
191 mc_local_variables = xbt_dict_new_homogeneous(NULL);
193 /* Get local variables in binary for state equality detection */
194 xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
195 MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
197 /* Get local variables in libsimgrid for state equality detection */
198 xbt_dict_t libsimgrid_location_list = MC_get_location_list(ls_path);
199 MC_get_local_variables(ls_path, libsimgrid_location_list, &mc_local_variables);
201 /* Initialize statistics */
202 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
204 XBT_DEBUG("Creating stack");
206 /* Create exploration stack */
207 mc_stack_liveness = xbt_fifo_new();
214 MC_print_statistics_pairs(mc_stats_pair);
223 MC_print_statistics(mc_stats);
229 int MC_random(int min, int max)
231 /*FIXME: return mc_current_state->executed_transition->random.value;*/
236 * \brief Schedules all the process that are ready to run
238 void MC_wait_for_requests(void)
240 smx_process_t process;
244 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
245 SIMIX_process_runall();
246 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
247 req = &process->simcall;
248 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
249 SIMIX_simcall_pre(req, 0);
254 int MC_deadlock_check()
256 int deadlock = FALSE;
257 smx_process_t process;
258 if(xbt_swag_size(simix_global->process_list)){
260 xbt_swag_foreach(process, simix_global->process_list){
261 if(process->simcall.call != SIMCALL_NONE
262 && MC_request_is_enabled(&process->simcall)){
272 * \brief Re-executes from the state at position start all the transitions indicated by
273 * a given model-checker stack.
274 * \param stack The stack with the transitions to execute.
275 * \param start Start index to begin the re-execution.
277 void MC_replay(xbt_fifo_t stack, int start)
279 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
283 smx_simcall_t req = NULL, saved_req = NULL;
284 xbt_fifo_item_t item, start_item;
287 XBT_DEBUG("**** Begin Replay ****");
290 /* Restore the initial state */
291 MC_restore_snapshot(initial_snapshot);
292 /* At the moment of taking the snapshot the raw heap was set, so restoring
293 * it will set it back again, we have to unset it to continue */
297 start_item = xbt_fifo_get_last_item(stack);
300 start_item = xbt_fifo_get_prev_item(start_item);
305 /* Traverse the stack from the state at position start and re-execute the transitions */
306 for (item = start_item;
307 item != xbt_fifo_get_first_item(stack);
308 item = xbt_fifo_get_prev_item(item)) {
310 state = (mc_state_t) xbt_fifo_get_item_content(item);
311 saved_req = MC_state_get_executed_request(state, &value);
314 /* because we got a copy of the executed request, we have to fetch the
315 real one, pointed by the request field of the issuer process */
316 req = &saved_req->issuer->simcall;
318 /* Debug information */
319 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
320 req_str = MC_request_to_string(req, value);
321 XBT_DEBUG("Replay: %s (%p)", req_str, state);
326 SIMIX_simcall_pre(req, value);
327 MC_wait_for_requests();
329 /* Update statistics */
330 mc_stats->visited_states++;
331 mc_stats->executed_transitions++;
333 XBT_DEBUG("**** End Replay ****");
343 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
346 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
350 smx_simcall_t req = NULL, saved_req = NULL;
351 xbt_fifo_item_t item;
353 mc_pair_stateless_t pair;
356 XBT_DEBUG("**** Begin Replay ****");
358 /* Restore the initial state */
359 MC_restore_snapshot(initial_snapshot_liveness);
360 /* At the moment of taking the snapshot the raw heap was set, so restoring
361 * it will set it back again, we have to unset it to continue */
366 item = xbt_fifo_get_last_item(stack);
368 while(depth <= xbt_fifo_size(stack)){
370 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
371 state = (mc_state_t) pair->graph_state;
373 if(pair->requests > 0){
375 saved_req = MC_state_get_executed_request(state, &value);
376 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
378 if(saved_req != NULL){
379 /* because we got a copy of the executed request, we have to fetch the
380 real one, pointed by the request field of the issuer process */
381 req = &saved_req->issuer->simcall;
382 //XBT_DEBUG("Req->call %u", req->call);
384 /* Debug information */
385 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
386 req_str = MC_request_to_string(req, value);
387 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
393 SIMIX_simcall_pre(req, value);
394 MC_wait_for_requests();
399 /* Update statistics */
400 mc_stats_pair->visited_pairs++;
402 item = xbt_fifo_get_prev_item(item);
407 /* Traverse the stack from the initial state and re-execute the transitions */
408 for (item = xbt_fifo_get_last_item(stack);
409 item != xbt_fifo_get_first_item(stack);
410 item = xbt_fifo_get_prev_item(item)) {
412 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
413 state = (mc_state_t) pair->graph_state;
415 if(pair->requests > 0){
417 saved_req = MC_state_get_executed_request(state, &value);
418 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
420 if(saved_req != NULL){
421 /* because we got a copy of the executed request, we have to fetch the
422 real one, pointed by the request field of the issuer process */
423 req = &saved_req->issuer->simcall;
424 //XBT_DEBUG("Req->call %u", req->call);
426 /* Debug information */
427 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
428 req_str = MC_request_to_string(req, value);
429 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
435 SIMIX_simcall_pre(req, value);
436 MC_wait_for_requests();
441 /* Update statistics */
442 mc_stats_pair->visited_pairs++;
446 XBT_DEBUG("**** End Replay ****");
456 * \brief Dumps the contents of a model-checker's stack and shows the actual
458 * \param stack The stack to dump
460 void MC_dump_stack_safety(xbt_fifo_t stack)
463 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
465 MC_show_stack_safety(stack);
467 if(!_surf_mc_checkpoint){
472 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
473 MC_state_delete(state);
486 void MC_show_stack_safety(xbt_fifo_t stack)
490 xbt_fifo_item_t item;
492 char *req_str = NULL;
494 for (item = xbt_fifo_get_last_item(stack);
495 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
496 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
497 req = MC_state_get_executed_request(state, &value);
499 req_str = MC_request_to_string(req, value);
500 XBT_INFO("%s", req_str);
506 void MC_show_deadlock(smx_simcall_t req)
508 /*char *req_str = NULL;*/
509 XBT_INFO("**************************");
510 XBT_INFO("*** DEAD-LOCK DETECTED ***");
511 XBT_INFO("**************************");
512 XBT_INFO("Locked request:");
513 /*req_str = MC_request_to_string(req);
514 XBT_INFO("%s", req_str);
516 XBT_INFO("Counter-example execution trace:");
517 MC_dump_stack_safety(mc_stack_safety);
521 void MC_show_stack_liveness(xbt_fifo_t stack){
523 mc_pair_stateless_t pair;
524 xbt_fifo_item_t item;
526 char *req_str = NULL;
528 for (item = xbt_fifo_get_last_item(stack);
529 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
530 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
531 req = MC_state_get_executed_request(pair->graph_state, &value);
533 if(pair->requests>0){
534 req_str = MC_request_to_string(req, value);
535 XBT_INFO("%s", req_str);
538 XBT_INFO("End of system requests but evolution in Büchi automaton");
544 void MC_dump_stack_liveness(xbt_fifo_t stack){
546 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
548 mc_pair_stateless_t pair;
551 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
552 MC_pair_stateless_delete(pair);
563 void MC_print_statistics(mc_stats_t stats)
565 //XBT_INFO("State space size ~= %lu", stats->state_size);
566 XBT_INFO("Expanded states = %lu", stats->expanded_states);
567 XBT_INFO("Visited states = %lu", stats->visited_states);
568 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
569 XBT_INFO("Expanded / Visited = %lf",
570 (double) stats->visited_states / stats->expanded_states);
571 /*XBT_INFO("Exploration coverage = %lf",
572 (double)stats->expanded_states / stats->state_size); */
575 void MC_print_statistics_pairs(mc_stats_pair_t stats)
577 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
578 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
579 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
580 XBT_INFO("Expanded / Visited = %lf",
581 (double) stats->visited_pairs / stats->expanded_pairs);
582 /*XBT_INFO("Exploration coverage = %lf",
583 (double)stats->expanded_states / stats->state_size); */
586 void MC_assert(int prop)
588 if (MC_IS_ENABLED && !prop){
589 XBT_INFO("**************************");
590 XBT_INFO("*** PROPERTY NOT VALID ***");
591 XBT_INFO("**************************");
592 XBT_INFO("Counter-example execution trace:");
593 MC_dump_stack_safety(mc_stack_safety);
594 MC_print_statistics(mc_stats);
599 static void MC_assert_pair(int prop){
600 if (MC_IS_ENABLED && !prop) {
601 XBT_INFO("**************************");
602 XBT_INFO("*** PROPERTY NOT VALID ***");
603 XBT_INFO("**************************");
604 //XBT_INFO("Counter-example execution trace:");
605 MC_show_stack_liveness(mc_stack_liveness);
606 //MC_dump_snapshot_stack(mc_snapshot_stack);
607 MC_print_statistics_pairs(mc_stats_pair);
612 void MC_process_clock_add(smx_process_t process, double amount)
614 mc_time[process->pid] += amount;
617 double MC_process_clock_get(smx_process_t process)
620 return mc_time[process->pid];
625 void MC_automaton_load(const char *file){
627 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
631 if (_mc_property_automaton == NULL)
632 _mc_property_automaton = xbt_automaton_new();
634 xbt_automaton_load(_mc_property_automaton,file);
645 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
647 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
651 if (_mc_property_automaton == NULL)
652 _mc_property_automaton = xbt_automaton_new();
654 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
665 /************ MC_ignore ***********/
667 void MC_ignore(void *address, size_t size){
671 if(mc_comparison_ignore == NULL)
672 mc_comparison_ignore = xbt_dynar_new(sizeof(mc_ignore_region_t), NULL);
674 mc_ignore_region_t region = NULL;
675 region = xbt_new0(s_mc_ignore_region_t, 1);
676 region->address = address;
679 if((address >= std_heap) && (address <= (void*)((char *)std_heap + STD_HEAP_SIZE))){
681 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
683 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
684 region->fragment = -1;
686 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
691 unsigned int cursor = 0;
692 mc_ignore_region_t current_region;
693 xbt_dynar_foreach(mc_comparison_ignore, cursor, current_region){
694 if(current_region->address > address)
698 xbt_dynar_insert_at(mc_comparison_ignore, cursor, ®ion);
703 void MC_new_stack_area(void *stack, char *name, void* context){
705 if(stacks_areas == NULL)
706 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
709 stack_region_t region = NULL;
710 region = xbt_new0(s_stack_region_t, 1);
711 region->address = stack;
712 region->process_name = strdup(name);
713 region->context = context;
714 xbt_dynar_push(stacks_areas, ®ion);
718 /************ DWARF ***********/
720 xbt_dict_t MC_get_location_list(const char *elf_file){
722 char *command = bprintf("objdump -Wo %s", elf_file);
724 FILE *fp = popen(command, "r");
727 perror("popen for objdump failed");
729 int debug = 0; /*Detect if the program has been compiled with -g */
731 xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
732 char *line = NULL, *loc_expr = NULL;
736 xbt_dynar_t split = NULL;
738 while ((read = getline(&line, &n, fp)) != -1) {
740 /* Wipeout the new line character */
741 line[read - 1] = '\0';
743 xbt_str_trim(line, NULL);
748 if(strlen(line) == 0)
753 if(strncmp(line, elf_file, strlen(elf_file)) == 0)
756 if(strncmp(line, "Contents", 8) == 0)
759 if(strncmp(line, "Offset", 6) == 0){
766 XBT_INFO("Your program must be compiled with -g");
770 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
772 xbt_str_strip_spaces(line);
773 split = xbt_str_split(line, " ");
775 while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
777 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
778 new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
779 new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
782 while(cursor_remove < 3){
783 xbt_dynar_remove_at(split, 0, NULL);
787 loc_expr = xbt_str_join(split, " ");
788 xbt_str_ltrim(loc_expr, "(");
789 xbt_str_rtrim(loc_expr, ")");
790 new_entry->location = get_location(NULL, loc_expr);
792 xbt_dynar_push(loclist, &new_entry);
794 xbt_dynar_free(&split);
797 read = getline(&line, &n, fp);
799 line[read - 1] = '\0';
800 xbt_str_strip_spaces(line);
801 split = xbt_str_split(line, " ");
807 char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
808 xbt_dict_set(location_list, key, loclist, NULL);
810 xbt_dynar_free(&split);
818 return location_list;
821 char *get_libsimgrid_path(){
823 char *command = bprintf("ldd %s", xbt_binary_name);
825 FILE *fp = popen(command, "r");
827 perror("popen for ldd failed");
834 while((read = getline(&line, &n, fp)) != -1){
839 /* Wipeout the new line character */
840 line[read - 1] = '\0';
842 xbt_str_strip_spaces(line);
843 xbt_str_ltrim(line, NULL);
844 split = xbt_str_split(line, " ");
846 if(strncmp((char *)xbt_dynar_get_as(split, 0, char *), "libsimgrid.so", 13) == 0){
850 return ((char *)xbt_dynar_get_as(split, 2, char *));
853 xbt_dynar_free(&split);
865 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
867 xbt_dict_cursor_t cursor = NULL;
871 xbt_dict_foreach(all_variables, cursor, name, res) {
872 if(offset >= res->start && offset < res->end)
880 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
882 char *command = bprintf("objdump -Wi %s", elf_file);
884 FILE *fp = popen(command, "r");
887 perror("popen for objdump failed");
889 char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
892 int valid_variable = 1;
893 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
894 xbt_dynar_t split = NULL, split2 = NULL;
896 xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
897 xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
898 char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
899 int new_frame = 0, new_variable = 0;
900 dw_frame_t variable_frame, subroutine_frame = NULL;
902 read = getline(&line, &n, fp);
907 read = getline(&line, &n, fp);
911 /* Wipeout the new line character */
912 line[read - 1] = '\0';
914 if(strlen(line) == 0){
915 read = getline(&line, &n, fp);
919 xbt_str_ltrim(line, NULL);
920 xbt_str_strip_spaces(line);
923 read = getline(&line, &n, fp);
927 xbt_dynar_free(&split);
928 split = xbt_str_split(line, " ");
931 node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
933 if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
935 dw_frame_t frame = NULL;
937 strtok(xbt_dynar_get_as(split, 0, char *), "<");
938 subprogram_start = strdup(strtok(NULL, "<"));
939 xbt_str_rtrim(subprogram_start, ">:");
941 read = getline(&line, &n, fp);
946 read = getline(&line, &n, fp);
950 /* Wipeout the new line character */
951 line[read - 1] = '\0';
953 if(strlen(line) == 0){
954 read = getline(&line, &n, fp);
958 xbt_dynar_free(&split);
959 xbt_str_rtrim(line, NULL);
960 xbt_str_strip_spaces(line);
961 split = xbt_str_split(line, " ");
963 node_type = xbt_dynar_get_as(split, 1, char *);
965 if(strncmp(node_type, "DW_AT_", 6) != 0)
968 if(strcmp(node_type, "DW_AT_sibling") == 0){
970 subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
971 xbt_str_ltrim(subprogram_end, "<0x");
972 xbt_str_rtrim(subprogram_end, ">");
974 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
977 abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
978 xbt_str_ltrim(abstract_origin, "<0x");
979 xbt_str_rtrim(abstract_origin, ">");
980 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
981 frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
983 }else if(strcmp(node_type, "DW_AT_name") == 0){
987 frame = xbt_new0(s_dw_frame_t, 1);
988 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
989 frame->variables = xbt_dict_new_homogeneous(NULL);
990 frame->frame_base = xbt_new0(s_dw_location_t, 1);
991 current_frame = strdup(frame->name);
993 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
995 }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
997 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
999 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1001 frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1005 xbt_str_strip_spaces(line);
1006 split2 = xbt_str_split(line, "(");
1007 xbt_dynar_remove_at(split2, 0, NULL);
1008 loc_expr = xbt_str_join(split2, " ");
1009 xbt_str_rtrim(loc_expr, ")");
1010 frame->frame_base = get_location(NULL, loc_expr);
1011 xbt_dynar_free(&split2);
1015 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1018 frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1020 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1023 frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1025 }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1028 free(current_frame);
1029 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1030 current_frame = strdup(frame->name);
1031 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1035 read = getline(&line, &n, fp);
1040 frame->start = strtoul(subprogram_start, NULL, 16);
1041 if(subprogram_end != NULL)
1042 frame->end = strtoul(subprogram_end, NULL, 16);
1043 xbt_dict_set(*all_variables, frame->name, frame, NULL);
1046 free(subprogram_start);
1047 if(subprogram_end != NULL){
1048 free(subprogram_end);
1049 subprogram_end = NULL;
1053 }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1055 dw_local_variable_t var = NULL;
1057 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1058 origin = strdup(strtok(NULL, "<"));
1059 xbt_str_rtrim(origin, ">:");
1061 read = getline(&line, &n, fp);
1066 read = getline(&line, &n, fp);
1070 /* Wipeout the new line character */
1071 line[read - 1] = '\0';
1073 if(strlen(line) == 0){
1074 read = getline(&line, &n, fp);
1078 xbt_dynar_free(&split);
1079 xbt_str_rtrim(line, NULL);
1080 xbt_str_strip_spaces(line);
1081 split = xbt_str_split(line, " ");
1083 node_type = xbt_dynar_get_as(split, 1, char *);
1085 if(strncmp(node_type, "DW_AT_", 6) != 0)
1088 if(strcmp(node_type, "DW_AT_name") == 0){
1091 var = xbt_new0(s_dw_local_variable_t, 1);
1092 var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1094 xbt_dict_set(variables_origin, origin, var->name, NULL);
1096 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1099 abstract_origin = xbt_dynar_get_as(split, 2, char *);
1100 xbt_str_ltrim(abstract_origin, "<0x");
1101 xbt_str_rtrim(abstract_origin, ">");
1103 variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1104 variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1105 var = xbt_dict_get_or_null(variable_frame->variables, variable_name);
1107 }else if(strcmp(node_type, "DW_AT_location") == 0){
1109 if(valid_variable == 1 && var != NULL){
1111 var->location = xbt_new0(s_dw_location_t, 1);
1113 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1115 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1117 var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1121 xbt_str_strip_spaces(line);
1122 split2 = xbt_str_split(line, "(");
1123 xbt_dynar_remove_at(split2, 0, NULL);
1124 loc_expr = xbt_str_join(split2, " ");
1125 xbt_str_rtrim(loc_expr, ")");
1126 var->location = get_location(NULL, loc_expr);
1127 xbt_dynar_free(&split2);
1133 }else if(strcmp(node_type, "DW_AT_external") == 0){
1139 read = getline(&line, &n, fp);
1143 if(new_variable == 1 && valid_variable == 1){
1145 variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1146 xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1152 }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1154 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1155 origin = strdup(strtok(NULL, "<"));
1156 xbt_str_rtrim(origin, ">:");
1158 read = getline(&line, &n, fp);
1162 /* Wipeout the new line character */
1163 line[read - 1] = '\0';
1166 read = getline(&line, &n, fp);
1170 if(strlen(line) == 0){
1171 read = getline(&line, &n, fp);
1175 xbt_dynar_free(&split);
1176 xbt_str_rtrim(line, NULL);
1177 xbt_str_strip_spaces(line);
1178 split = xbt_str_split(line, " ");
1180 if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1183 node_type = xbt_dynar_get_as(split, 1, char *);
1185 if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1187 origin = xbt_dynar_get_as(split, 2, char *);
1188 xbt_str_ltrim(origin, "<0x");
1189 xbt_str_rtrim(origin, ">");
1191 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1192 subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1194 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1196 subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1198 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1200 subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1203 read = getline(&line, &n, fp);
1209 read = getline(&line, &n, fp);
1215 xbt_dynar_free(&split);
1222 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1224 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1226 if(location_list != NULL){
1228 char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1229 loc->type = e_dw_loclist;
1230 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1232 XBT_INFO("Key not found in loclist");
1238 char *tok = NULL, *tok2 = NULL;
1240 xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1241 xbt_dynar_t tokens2;
1243 loc->type = e_dw_compose;
1244 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1246 while(cursor < xbt_dynar_length(tokens1)){
1248 tok = xbt_dynar_get_as(tokens1, cursor, char*);
1249 tokens2 = xbt_str_split(tok, " ");
1250 tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1252 if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1253 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1254 new_element->type = e_dw_register;
1255 new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1256 xbt_dynar_push(loc->location.compose, &new_element);
1257 }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1258 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1259 new_element->type = e_dw_fbregister_op;
1260 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1261 xbt_dynar_push(loc->location.compose, &new_element);
1262 }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1263 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1264 new_element->type = e_dw_bregister_op;
1265 new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1266 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, 2, char*));
1267 xbt_dynar_push(loc->location.compose, &new_element);
1268 }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1269 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1270 new_element->type = e_dw_lit;
1271 new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1272 xbt_dynar_push(loc->location.compose, &new_element);
1273 }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1274 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1275 new_element->type = e_dw_piece;
1276 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1277 /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1278 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1280 new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1281 xbt_dynar_push(loc->location.compose, &new_element);
1282 }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1283 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1284 new_element->type = e_dw_plus_uconst;
1285 new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, 1, char *));
1286 xbt_dynar_push(loc->location.compose, &new_element);
1287 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1288 strcmp(tok, "DW_OP_and") == 0 ||
1289 strcmp(tok, "DW_OP_div") == 0 ||
1290 strcmp(tok, "DW_OP_minus") == 0 ||
1291 strcmp(tok, "DW_OP_mod") == 0 ||
1292 strcmp(tok, "DW_OP_mul") == 0 ||
1293 strcmp(tok, "DW_OP_neg") == 0 ||
1294 strcmp(tok, "DW_OP_not") == 0 ||
1295 strcmp(tok, "DW_OP_or") == 0 ||
1296 strcmp(tok, "DW_OP_plus") == 0){
1297 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1298 new_element->type = e_dw_arithmetic;
1299 new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1300 xbt_dynar_push(loc->location.compose, &new_element);
1301 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1302 }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1303 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1304 new_element->type = e_dw_deref;
1305 new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, 1, char*));
1306 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1307 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1309 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1310 xbt_dynar_push(loc->location.compose, &new_element);
1311 }else if(strcmp(tok, "DW_OP_deref") == 0){
1312 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1313 new_element->type = e_dw_deref;
1314 new_element->location.deref_size = sizeof(void *);
1315 xbt_dynar_push(loc->location.compose, &new_element);
1316 }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1317 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1318 new_element->type = e_dw_uconstant;
1319 new_element->location.uconstant.bytes = 1;
1320 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1321 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1322 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1324 new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1325 xbt_dynar_push(loc->location.compose, &new_element);
1326 }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1327 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1328 new_element->type = e_dw_sconstant;
1329 new_element->location.sconstant.bytes = 1;
1330 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1331 xbt_dynar_push(loc->location.compose, &new_element);
1332 }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1333 strcmp(tok2, "DW_OP_const2u:") == 0 ||
1334 strcmp(tok2, "DW_OP_const4u:") == 0 ||
1335 strcmp(tok2, "DW_OP_const8u:") == 0){
1336 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1337 new_element->type = e_dw_uconstant;
1338 new_element->location.uconstant.bytes = tok2[11] - '0';
1339 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1340 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1341 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1343 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1344 xbt_dynar_push(loc->location.compose, &new_element);
1345 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1346 strcmp(tok, "DW_OP_const2s") == 0 ||
1347 strcmp(tok, "DW_OP_const4s") == 0 ||
1348 strcmp(tok, "DW_OP_const8s") == 0){
1349 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1350 new_element->type = e_dw_sconstant;
1351 new_element->location.sconstant.bytes = tok2[11] - '0';
1352 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1353 xbt_dynar_push(loc->location.compose, &new_element);
1355 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1356 new_element->type = e_dw_unsupported;
1357 xbt_dynar_push(loc->location.compose, &new_element);
1361 xbt_dynar_free(&tokens2);
1365 xbt_dynar_free(&tokens1);
1374 void print_local_variables(xbt_dict_t list){
1376 dw_location_entry_t entry;
1377 dw_location_t location_entry;
1378 unsigned int cursor3 = 0, cursor4 = 0;
1379 xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1381 char *frame_name, *variable_name;
1382 dw_frame_t current_frame;
1383 dw_local_variable_t current_variable;
1385 xbt_dict_foreach(list, cursor, frame_name, current_frame){
1386 fprintf(stderr, "Frame name : %s\n", current_frame->name);
1387 fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1388 xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1389 fprintf(stderr, "Name : %s\n", current_variable->name);
1390 if(current_variable->location == NULL)
1392 fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1393 switch(current_variable->location->type){
1395 xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1396 fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1397 switch(entry->location->type){
1398 case e_dw_register :
1399 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1401 case e_dw_bregister_op:
1402 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1405 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1407 case e_dw_fbregister_op:
1408 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1411 fprintf(stderr, " Location :\n");
1412 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1413 switch(location_entry->type){
1414 case e_dw_register :
1415 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1417 case e_dw_bregister_op:
1418 fprintf(stderr, " %d) add %d to the value in register %d\n", cursor4 + 1, location_entry->location.breg_op.offset, location_entry->location.breg_op.reg);
1421 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1423 case e_dw_fbregister_op:
1424 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1427 fprintf(stderr, " %d) Pop the stack entry and treats it as an address (size of data %d)\n", cursor4 + 1, location_entry->location.deref_size);
1429 case e_dw_arithmetic :
1430 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1433 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1435 case e_dw_uconstant :
1436 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1438 case e_dw_sconstant :
1439 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1442 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1448 fprintf(stderr, "Location type not supported\n");
1455 fprintf(stderr, "Location :\n");
1456 xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1457 switch(location_entry->type){
1458 case e_dw_register :
1459 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1461 case e_dw_bregister_op:
1462 fprintf(stderr, " %d) add %d to the value in register %d\n", cursor4 + 1, location_entry->location.breg_op.offset, location_entry->location.breg_op.reg);
1465 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1467 case e_dw_fbregister_op:
1468 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1471 fprintf(stderr, " %d) Pop the stack entry and treats it as an address (size of data %d)\n", cursor4 + 1, location_entry->location.deref_size);
1473 case e_dw_arithmetic :
1474 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1477 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1479 case e_dw_uconstant :
1480 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1482 case e_dw_sconstant :
1483 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1486 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1492 fprintf(stderr, "Location type not supported\n");