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 "simgrid/sg_config.h"
12 #include "../surf/surf_private.h"
13 #include "../simix/smx_private.h"
14 #include "../xbt/mmalloc/mmprivate.h"
16 #include "mc_private.h"
17 #include "xbt/automaton.h"
20 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
22 "Logging specific to MC (global)");
24 /* Configuration support */
25 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
28 extern int _sg_init_status;
29 void _mc_cfg_cb_reduce(const char *name, int pos) {
30 if (_sg_init_status && !_sg_do_model_check) {
31 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.");
33 char *val= xbt_cfg_get_string(_sg_cfg_set, name);
34 if (!strcasecmp(val,"none")) {
35 mc_reduce_kind = e_mc_reduce_none;
36 } else if (!strcasecmp(val,"dpor")) {
37 mc_reduce_kind = e_mc_reduce_dpor;
39 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
43 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
44 if (_sg_init_status && !_sg_do_model_check) {
45 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.");
47 _surf_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
49 void _mc_cfg_cb_property(const char *name, int pos) {
50 if (_sg_init_status && !_sg_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(_sg_cfg_set, name);
56 void _mc_cfg_cb_timeout(const char *name, int pos) {
57 if (_sg_init_status && !_sg_do_model_check) {
58 xbt_die("You are specifying a value to enable/disable timeout for wait requests 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.");
60 _surf_mc_timeout= xbt_cfg_get_int(_sg_cfg_set, name);
63 void _mc_cfg_cb_max_depth(const char *name, int pos) {
64 if (_sg_init_status && !_sg_do_model_check) {
65 xbt_die("You are specifying a max depth 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.");
67 _surf_mc_max_depth= xbt_cfg_get_int(_sg_cfg_set, name);
70 void _mc_cfg_cb_visited(const char *name, int pos) {
71 if (_sg_init_status && !_sg_do_model_check) {
72 xbt_die("You are specifying a number of stored visited states 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.");
74 _surf_mc_visited= xbt_cfg_get_int(_sg_cfg_set, name);
78 /* MC global data structures */
80 mc_state_t mc_current_state = NULL;
81 char mc_replay_mode = FALSE;
82 double *mc_time = NULL;
86 xbt_fifo_t mc_stack_safety = NULL;
87 mc_stats_t mc_stats = NULL;
88 mc_global_t initial_state_safety = NULL;
92 mc_stats_pair_t mc_stats_pair = NULL;
93 xbt_fifo_t mc_stack_liveness = NULL;
94 mc_global_t initial_state_liveness = NULL;
98 xbt_dict_t mc_local_variables = NULL;
100 xbt_dynar_t mc_global_variables = NULL;
102 /* Ignore mechanism */
103 xbt_dynar_t mc_stack_comparison_ignore;
104 xbt_dynar_t mc_data_bss_comparison_ignore;
105 extern xbt_dynar_t mc_heap_comparison_ignore;
106 extern xbt_dynar_t stacks_areas;
108 xbt_automaton_t _mc_property_automaton = NULL;
110 /* Static functions */
112 static void MC_assert_pair(int prop);
113 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
114 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
115 static size_t data_bss_ignore_size(void *address);
116 static void MC_get_global_variables(char *elf_file);
118 void MC_do_the_modelcheck_for_real() {
119 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
120 if (mc_reduce_kind==e_mc_reduce_unset)
121 mc_reduce_kind=e_mc_reduce_dpor;
123 XBT_INFO("Check a safety property");
124 MC_modelcheck_safety();
128 if (mc_reduce_kind==e_mc_reduce_unset)
129 mc_reduce_kind=e_mc_reduce_none;
131 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
132 MC_automaton_load(_surf_mc_property_file);
133 MC_modelcheck_liveness();
138 void MC_compare(void){
144 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
146 mc_time = xbt_new0(double, simix_process_maxpid);
148 /* mc_time refers to clock for each process -> ignore it for heap comparison */
150 for(i = 0; i<simix_process_maxpid; i++)
151 MC_ignore_heap(&(mc_time[i]), sizeof(double));
155 /* Initialize the data structures that must be persistent across every
156 iteration of the model-checker (in RAW memory) */
160 MC_init_memory_map_info();
162 mc_local_variables = xbt_dict_new_homogeneous(NULL);
164 /* Get local variables in binary for state equality detection */
165 xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
166 MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
168 /* Get local variables in libsimgrid for state equality detection */
169 xbt_dict_t libsimgrid_location_list = MC_get_location_list(libsimgrid_path);
170 MC_get_local_variables(libsimgrid_path, libsimgrid_location_list, &mc_local_variables);
172 /* Get .plt section (start and end addresses) for data libsimgrid and data program comparison */
173 get_libsimgrid_plt_section();
174 get_binary_plt_section();
176 MC_ignore_data_bss(&end_raw_heap, sizeof(end_raw_heap));
178 /* Get global variables */
179 MC_get_global_variables(xbt_binary_name);
180 MC_get_global_variables(libsimgrid_path);
184 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
185 MC_ignore_stack("e", "*");
186 MC_ignore_stack("__ex_cleanup", "*");
187 MC_ignore_stack("__ex_mctx_en", "*");
188 MC_ignore_stack("__ex_mctx_me", "*");
189 MC_ignore_stack("__xbt_ex_ctx_ptr", "*");
190 MC_ignore_stack("_log_ev", "*");
191 MC_ignore_stack("_throw_ctx", "*");
192 MC_ignore_stack("ctx", "*");
199 void MC_modelcheck_safety(void)
201 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
203 /* Check if MC is already initialized */
204 if (initial_state_safety)
207 mc_time = xbt_new0(double, simix_process_maxpid);
209 /* Initialize the data structures that must be persistent across every
210 iteration of the model-checker (in RAW memory) */
214 /* Initialize statistics */
215 mc_stats = xbt_new0(s_mc_stats_t, 1);
216 mc_stats->state_size = 1;
218 /* Create exploration stack */
219 mc_stack_safety = xbt_fifo_new();
223 if(_surf_mc_visited > 0){
227 MC_init_memory_map_info();
228 get_libsimgrid_plt_section();
229 get_binary_plt_section();
236 /* Save the initial state */
237 initial_state_safety = xbt_new0(s_mc_global_t, 1);
238 initial_state_safety->snapshot = MC_take_snapshot();
249 void MC_modelcheck_liveness(){
251 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
257 /* Initialize statistics */
258 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
260 /* Create exploration stack */
261 mc_stack_liveness = xbt_fifo_new();
263 initial_state_liveness = xbt_new0(s_mc_global_t, 1);
270 MC_print_statistics_pairs(mc_stats_pair);
286 int MC_random(int min, int max)
288 /*FIXME: return mc_current_state->executed_transition->random.value;*/
293 * \brief Schedules all the process that are ready to run
295 void MC_wait_for_requests(void)
297 smx_process_t process;
301 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
302 SIMIX_process_runall();
303 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
304 req = &process->simcall;
305 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
306 SIMIX_simcall_pre(req, 0);
311 int MC_deadlock_check()
313 int deadlock = FALSE;
314 smx_process_t process;
315 if(xbt_swag_size(simix_global->process_list)){
317 xbt_swag_foreach(process, simix_global->process_list){
318 if(process->simcall.call != SIMCALL_NONE
319 && MC_request_is_enabled(&process->simcall)){
329 * \brief Re-executes from the state at position start all the transitions indicated by
330 * a given model-checker stack.
331 * \param stack The stack with the transitions to execute.
332 * \param start Start index to begin the re-execution.
334 void MC_replay(xbt_fifo_t stack, int start)
336 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
340 smx_simcall_t req = NULL, saved_req = NULL;
341 xbt_fifo_item_t item, start_item;
344 XBT_DEBUG("**** Begin Replay ****");
347 /* Restore the initial state */
348 MC_restore_snapshot(initial_state_safety->snapshot);
349 /* At the moment of taking the snapshot the raw heap was set, so restoring
350 * it will set it back again, we have to unset it to continue */
354 start_item = xbt_fifo_get_last_item(stack);
357 start_item = xbt_fifo_get_prev_item(start_item);
362 /* Traverse the stack from the state at position start and re-execute the transitions */
363 for (item = start_item;
364 item != xbt_fifo_get_first_item(stack);
365 item = xbt_fifo_get_prev_item(item)) {
367 state = (mc_state_t) xbt_fifo_get_item_content(item);
368 saved_req = MC_state_get_executed_request(state, &value);
371 /* because we got a copy of the executed request, we have to fetch the
372 real one, pointed by the request field of the issuer process */
373 req = &saved_req->issuer->simcall;
375 /* Debug information */
376 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
377 req_str = MC_request_to_string(req, value);
378 XBT_DEBUG("Replay: %s (%p)", req_str, state);
383 SIMIX_simcall_pre(req, value);
384 MC_wait_for_requests();
386 /* Update statistics */
387 mc_stats->visited_states++;
388 mc_stats->executed_transitions++;
390 XBT_DEBUG("**** End Replay ****");
400 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
403 initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
407 smx_simcall_t req = NULL, saved_req = NULL;
408 xbt_fifo_item_t item;
410 mc_pair_stateless_t pair;
413 XBT_DEBUG("**** Begin Replay ****");
415 /* Restore the initial state */
416 MC_restore_snapshot(initial_state_liveness->snapshot);
418 /* At the moment of taking the snapshot the raw heap was set, so restoring
419 * it will set it back again, we have to unset it to continue */
420 if(!initial_state_liveness->raw_mem_set)
425 item = xbt_fifo_get_last_item(stack);
427 while(depth <= xbt_fifo_size(stack)){
429 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
430 state = (mc_state_t) pair->graph_state;
432 if(pair->requests > 0){
434 saved_req = MC_state_get_executed_request(state, &value);
435 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
437 if(saved_req != NULL){
438 /* because we got a copy of the executed request, we have to fetch the
439 real one, pointed by the request field of the issuer process */
440 req = &saved_req->issuer->simcall;
441 //XBT_DEBUG("Req->call %u", req->call);
443 /* Debug information */
444 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
445 req_str = MC_request_to_string(req, value);
446 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
452 SIMIX_simcall_pre(req, value);
453 MC_wait_for_requests();
458 /* Update statistics */
459 mc_stats_pair->visited_pairs++;
461 item = xbt_fifo_get_prev_item(item);
466 /* Traverse the stack from the initial state and re-execute the transitions */
467 for (item = xbt_fifo_get_last_item(stack);
468 item != xbt_fifo_get_first_item(stack);
469 item = xbt_fifo_get_prev_item(item)) {
471 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
472 state = (mc_state_t) pair->graph_state;
474 if(pair->requests > 0){
476 saved_req = MC_state_get_executed_request(state, &value);
477 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
479 if(saved_req != NULL){
480 /* because we got a copy of the executed request, we have to fetch the
481 real one, pointed by the request field of the issuer process */
482 req = &saved_req->issuer->simcall;
483 //XBT_DEBUG("Req->call %u", req->call);
485 /* Debug information */
486 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
487 req_str = MC_request_to_string(req, value);
488 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
494 SIMIX_simcall_pre(req, value);
495 MC_wait_for_requests();
500 /* Update statistics */
501 mc_stats_pair->visited_pairs++;
505 XBT_DEBUG("**** End Replay ****");
507 if(initial_state_liveness->raw_mem_set)
515 * \brief Dumps the contents of a model-checker's stack and shows the actual
517 * \param stack The stack to dump
519 void MC_dump_stack_safety(xbt_fifo_t stack)
522 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
524 MC_show_stack_safety(stack);
526 if(!_surf_mc_checkpoint){
531 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
532 MC_state_delete(state);
545 void MC_show_stack_safety(xbt_fifo_t stack)
549 xbt_fifo_item_t item;
551 char *req_str = NULL;
553 for (item = xbt_fifo_get_last_item(stack);
554 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
555 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
556 req = MC_state_get_executed_request(state, &value);
558 req_str = MC_request_to_string(req, value);
559 XBT_INFO("%s", req_str);
565 void MC_show_deadlock(smx_simcall_t req)
567 /*char *req_str = NULL;*/
568 XBT_INFO("**************************");
569 XBT_INFO("*** DEAD-LOCK DETECTED ***");
570 XBT_INFO("**************************");
571 XBT_INFO("Locked request:");
572 /*req_str = MC_request_to_string(req);
573 XBT_INFO("%s", req_str);
575 XBT_INFO("Counter-example execution trace:");
576 MC_dump_stack_safety(mc_stack_safety);
580 void MC_show_stack_liveness(xbt_fifo_t stack){
582 mc_pair_stateless_t pair;
583 xbt_fifo_item_t item;
585 char *req_str = NULL;
587 for (item = xbt_fifo_get_last_item(stack);
588 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
589 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
590 req = MC_state_get_executed_request(pair->graph_state, &value);
592 if(pair->requests>0){
593 req_str = MC_request_to_string(req, value);
594 XBT_INFO("%s", req_str);
597 XBT_INFO("End of system requests but evolution in Büchi automaton");
603 void MC_dump_stack_liveness(xbt_fifo_t stack){
605 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
607 mc_pair_stateless_t pair;
610 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
611 pair_stateless_free(pair);
620 void MC_print_statistics(mc_stats_t stats)
622 //XBT_INFO("State space size ~= %lu", stats->state_size);
623 XBT_INFO("Expanded states = %lu", stats->expanded_states);
624 XBT_INFO("Visited states = %lu", stats->visited_states);
625 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
626 XBT_INFO("Expanded / Visited = %lf",
627 (double) stats->visited_states / stats->expanded_states);
628 /*XBT_INFO("Exploration coverage = %lf",
629 (double)stats->expanded_states / stats->state_size); */
632 void MC_print_statistics_pairs(mc_stats_pair_t stats)
634 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
635 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
636 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
637 XBT_INFO("Expanded / Visited = %lf",
638 (double) stats->visited_pairs / stats->expanded_pairs);
640 if(mmalloc_get_current_heap() == raw_heap)
644 void MC_assert(int prop)
646 if (MC_is_active() && !prop){
647 XBT_INFO("**************************");
648 XBT_INFO("*** PROPERTY NOT VALID ***");
649 XBT_INFO("**************************");
650 XBT_INFO("Counter-example execution trace:");
651 MC_dump_stack_safety(mc_stack_safety);
652 MC_print_statistics(mc_stats);
657 static void MC_assert_pair(int prop){
658 if (MC_is_active() && !prop) {
659 XBT_INFO("**************************");
660 XBT_INFO("*** PROPERTY NOT VALID ***");
661 XBT_INFO("**************************");
662 //XBT_INFO("Counter-example execution trace:");
663 MC_show_stack_liveness(mc_stack_liveness);
664 //MC_dump_snapshot_stack(mc_snapshot_stack);
665 MC_print_statistics_pairs(mc_stats_pair);
670 void MC_process_clock_add(smx_process_t process, double amount)
672 mc_time[process->pid] += amount;
675 double MC_process_clock_get(smx_process_t process)
679 return mc_time[process->pid];
687 void MC_automaton_load(const char *file){
689 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
693 if (_mc_property_automaton == NULL)
694 _mc_property_automaton = xbt_automaton_new();
696 xbt_automaton_load(_mc_property_automaton,file);
705 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
707 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
711 if (_mc_property_automaton == NULL)
712 _mc_property_automaton = xbt_automaton_new();
714 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
723 /************ MC_ignore ***********/
725 void MC_ignore_heap(void *address, size_t size){
727 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
731 if(mc_heap_comparison_ignore == NULL)
732 mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), NULL);
734 mc_heap_ignore_region_t region = NULL;
735 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
736 region->address = address;
739 if((address >= std_heap) && (address <= (void*)((char *)std_heap + STD_HEAP_SIZE))){
741 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
743 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
744 region->fragment = -1;
746 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
751 unsigned int cursor = 0;
752 mc_heap_ignore_region_t current_region;
753 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
754 if(current_region->address > address)
758 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
766 void MC_ignore_data_bss(void *address, size_t size){
768 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
772 if(mc_data_bss_comparison_ignore == NULL)
773 mc_data_bss_comparison_ignore = xbt_dynar_new(sizeof(mc_data_bss_ignore_variable_t), NULL);
775 if(xbt_dynar_is_empty(mc_data_bss_comparison_ignore)){
777 mc_data_bss_ignore_variable_t var = NULL;
778 var = xbt_new0(s_mc_data_bss_ignore_variable_t, 1);
779 var->address = address;
782 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, 0, &var);
786 unsigned int cursor = 0;
788 int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
789 mc_data_bss_ignore_variable_t current_var = NULL;
792 cursor = (start + end) / 2;
793 current_var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
794 if(current_var->address == address){
800 if(current_var->address < address)
802 if(current_var->address > address)
806 mc_data_bss_ignore_variable_t var = NULL;
807 var = xbt_new0(s_mc_data_bss_ignore_variable_t, 1);
808 var->address = address;
811 if(current_var->address < address)
812 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor + 1, &var);
814 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor, &var);
824 static size_t data_bss_ignore_size(void *address){
825 unsigned int cursor = 0;
827 int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
828 mc_data_bss_ignore_variable_t var;
831 cursor = (start + end) / 2;
832 var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
833 if(var->address == address)
835 if(var->address < address){
836 if((void *)((char *)var->address + var->size) > address)
837 return (char *)var->address + var->size - (char*)address;
841 if(var->address > address)
850 void MC_ignore_stack(const char *var_name, const char *frame){
852 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
856 if(mc_stack_comparison_ignore == NULL)
857 mc_stack_comparison_ignore = xbt_dynar_new(sizeof(mc_stack_ignore_variable_t), NULL);
859 if(xbt_dynar_is_empty(mc_stack_comparison_ignore)){
861 mc_stack_ignore_variable_t var = NULL;
862 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
863 var->var_name = strdup(var_name);
864 var->frame = strdup(frame);
866 xbt_dynar_insert_at(mc_stack_comparison_ignore, 0, &var);
870 unsigned int cursor = 0;
872 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
873 mc_stack_ignore_variable_t current_var = NULL;
876 cursor = (start + end) / 2;
877 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
878 if(strcmp(current_var->frame, frame) == 0){
879 if(strcmp(current_var->var_name, var_name) == 0){
885 if(strcmp(current_var->var_name, var_name) < 0)
887 if(strcmp(current_var->var_name, var_name) > 0)
890 if(strcmp(current_var->frame, frame) < 0)
892 if(strcmp(current_var->frame, frame) > 0)
896 mc_stack_ignore_variable_t var = NULL;
897 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
898 var->var_name = strdup(var_name);
899 var->frame = strdup(frame);
901 if(strcmp(current_var->frame, frame) < 0)
902 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor + 1, &var);
904 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor, &var);
915 void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
917 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
920 if(stacks_areas == NULL)
921 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
923 stack_region_t region = NULL;
924 region = xbt_new0(s_stack_region_t, 1);
925 region->address = stack;
926 region->process_name = strdup(name);
927 region->context = context;
929 xbt_dynar_push(stacks_areas, ®ion);
937 /************ DWARF ***********/
939 xbt_dict_t MC_get_location_list(const char *elf_file){
941 char *command = bprintf("objdump -Wo %s", elf_file);
943 FILE *fp = popen(command, "r");
946 perror("popen for objdump failed");
950 int debug = 0; /*Detect if the program has been compiled with -g */
952 xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
953 char *line = NULL, *loc_expr = NULL;
957 xbt_dynar_t split = NULL;
959 while ((read = getline(&line, &n, fp)) != -1) {
961 /* Wipeout the new line character */
962 line[read - 1] = '\0';
964 xbt_str_trim(line, NULL);
969 if(strlen(line) == 0)
974 if(strncmp(line, elf_file, strlen(elf_file)) == 0)
977 if(strncmp(line, "Contents", 8) == 0)
980 if(strncmp(line, "Offset", 6) == 0){
987 XBT_INFO("Your program must be compiled with -g");
991 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
993 xbt_str_strip_spaces(line);
994 split = xbt_str_split(line, " ");
996 while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
998 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
999 new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
1000 new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
1003 while(cursor_remove < 3){
1004 xbt_dynar_remove_at(split, 0, NULL);
1008 loc_expr = xbt_str_join(split, " ");
1009 xbt_str_ltrim(loc_expr, "(");
1010 xbt_str_rtrim(loc_expr, ")");
1011 new_entry->location = get_location(NULL, loc_expr);
1013 xbt_dynar_push(loclist, &new_entry);
1015 xbt_dynar_free(&split);
1018 read = getline(&line, &n, fp);
1020 line[read - 1] = '\0';
1021 xbt_str_strip_spaces(line);
1022 split = xbt_str_split(line, " ");
1028 char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
1029 xbt_dict_set(location_list, key, loclist, NULL);
1031 xbt_dynar_free(&split);
1039 return location_list;
1042 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
1044 xbt_dict_cursor_t cursor = NULL;
1048 xbt_dict_foreach(all_variables, cursor, name, res) {
1049 if(offset >= res->start && offset < res->end)
1057 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
1059 char *command = bprintf("objdump -Wi %s", elf_file);
1061 FILE *fp = popen(command, "r");
1064 perror("popen for objdump failed");
1066 char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
1069 int valid_variable = 1;
1070 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
1071 xbt_dynar_t split = NULL, split2 = NULL;
1073 xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
1074 xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
1075 char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
1076 int new_frame = 0, new_variable = 0;
1077 dw_frame_t variable_frame, subroutine_frame = NULL;
1079 read = getline(&line, &n, fp);
1081 while (read != -1) {
1084 read = getline(&line, &n, fp);
1088 /* Wipeout the new line character */
1089 line[read - 1] = '\0';
1091 if(strlen(line) == 0){
1092 read = getline(&line, &n, fp);
1096 xbt_str_ltrim(line, NULL);
1097 xbt_str_strip_spaces(line);
1100 read = getline(&line, &n, fp);
1104 xbt_dynar_free(&split);
1105 split = xbt_str_split(line, " ");
1108 node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1110 if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
1112 dw_frame_t frame = NULL;
1114 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1115 subprogram_start = strdup(strtok(NULL, "<"));
1116 xbt_str_rtrim(subprogram_start, ">:");
1118 read = getline(&line, &n, fp);
1123 read = getline(&line, &n, fp);
1127 /* Wipeout the new line character */
1128 line[read - 1] = '\0';
1130 if(strlen(line) == 0){
1131 read = getline(&line, &n, fp);
1135 xbt_dynar_free(&split);
1136 xbt_str_rtrim(line, NULL);
1137 xbt_str_strip_spaces(line);
1138 split = xbt_str_split(line, " ");
1140 node_type = xbt_dynar_get_as(split, 1, char *);
1142 if(strncmp(node_type, "DW_AT_", 6) != 0)
1145 if(strcmp(node_type, "DW_AT_sibling") == 0){
1147 subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
1148 xbt_str_ltrim(subprogram_end, "<0x");
1149 xbt_str_rtrim(subprogram_end, ">");
1151 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
1154 abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
1155 xbt_str_ltrim(abstract_origin, "<0x");
1156 xbt_str_rtrim(abstract_origin, ">");
1157 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
1158 frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1160 }else if(strcmp(node_type, "DW_AT_name") == 0){
1163 free(current_frame);
1164 frame = xbt_new0(s_dw_frame_t, 1);
1165 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1166 frame->variables = xbt_dict_new_homogeneous(NULL);
1167 frame->frame_base = xbt_new0(s_dw_location_t, 1);
1168 current_frame = strdup(frame->name);
1170 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1172 }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
1174 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1176 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1178 frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1182 xbt_str_strip_spaces(line);
1183 split2 = xbt_str_split(line, "(");
1184 xbt_dynar_remove_at(split2, 0, NULL);
1185 loc_expr = xbt_str_join(split2, " ");
1186 xbt_str_rtrim(loc_expr, ")");
1187 frame->frame_base = get_location(NULL, loc_expr);
1188 xbt_dynar_free(&split2);
1192 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1195 frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1197 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1200 frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1202 }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1205 free(current_frame);
1206 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1207 current_frame = strdup(frame->name);
1208 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1212 read = getline(&line, &n, fp);
1217 frame->start = strtoul(subprogram_start, NULL, 16);
1218 if(subprogram_end != NULL)
1219 frame->end = strtoul(subprogram_end, NULL, 16);
1220 xbt_dict_set(*all_variables, frame->name, frame, NULL);
1223 free(subprogram_start);
1224 if(subprogram_end != NULL){
1225 free(subprogram_end);
1226 subprogram_end = NULL;
1230 }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1232 dw_local_variable_t var = NULL;
1234 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1235 origin = strdup(strtok(NULL, "<"));
1236 xbt_str_rtrim(origin, ">:");
1238 read = getline(&line, &n, fp);
1243 read = getline(&line, &n, fp);
1247 /* Wipeout the new line character */
1248 line[read - 1] = '\0';
1250 if(strlen(line) == 0){
1251 read = getline(&line, &n, fp);
1255 xbt_dynar_free(&split);
1256 xbt_str_rtrim(line, NULL);
1257 xbt_str_strip_spaces(line);
1258 split = xbt_str_split(line, " ");
1260 node_type = xbt_dynar_get_as(split, 1, char *);
1262 if(strncmp(node_type, "DW_AT_", 6) != 0)
1265 if(strcmp(node_type, "DW_AT_name") == 0){
1268 var = xbt_new0(s_dw_local_variable_t, 1);
1269 var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1271 xbt_dict_set(variables_origin, origin, var->name, NULL);
1273 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1276 abstract_origin = xbt_dynar_get_as(split, 2, char *);
1277 xbt_str_ltrim(abstract_origin, "<0x");
1278 xbt_str_rtrim(abstract_origin, ">");
1280 variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1281 variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1282 var = xbt_dict_get_or_null(variable_frame->variables, variable_name);
1284 }else if(strcmp(node_type, "DW_AT_location") == 0){
1286 if(valid_variable == 1 && var != NULL){
1288 var->location = xbt_new0(s_dw_location_t, 1);
1290 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1292 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1294 var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1298 xbt_str_strip_spaces(line);
1299 split2 = xbt_str_split(line, "(");
1300 xbt_dynar_remove_at(split2, 0, NULL);
1301 loc_expr = xbt_str_join(split2, " ");
1302 xbt_str_rtrim(loc_expr, ")");
1303 var->location = get_location(NULL, loc_expr);
1304 xbt_dynar_free(&split2);
1310 }else if(strcmp(node_type, "DW_AT_external") == 0){
1316 read = getline(&line, &n, fp);
1320 if(new_variable == 1 && valid_variable == 1){
1322 variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1323 xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1329 }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1331 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1332 origin = strdup(strtok(NULL, "<"));
1333 xbt_str_rtrim(origin, ">:");
1335 read = getline(&line, &n, fp);
1339 /* Wipeout the new line character */
1340 line[read - 1] = '\0';
1343 read = getline(&line, &n, fp);
1347 if(strlen(line) == 0){
1348 read = getline(&line, &n, fp);
1352 xbt_dynar_free(&split);
1353 xbt_str_rtrim(line, NULL);
1354 xbt_str_strip_spaces(line);
1355 split = xbt_str_split(line, " ");
1357 if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1360 node_type = xbt_dynar_get_as(split, 1, char *);
1362 if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1364 origin = xbt_dynar_get_as(split, 2, char *);
1365 xbt_str_ltrim(origin, "<0x");
1366 xbt_str_rtrim(origin, ">");
1368 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1369 subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1371 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1373 subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1375 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1377 subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1380 read = getline(&line, &n, fp);
1386 read = getline(&line, &n, fp);
1392 xbt_dynar_free(&split);
1399 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1401 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1403 if(location_list != NULL){
1405 char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1406 loc->type = e_dw_loclist;
1407 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1409 XBT_INFO("Key not found in loclist");
1415 char *tok = NULL, *tok2 = NULL;
1417 xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1418 xbt_dynar_t tokens2;
1420 loc->type = e_dw_compose;
1421 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1423 while(cursor < xbt_dynar_length(tokens1)){
1425 tok = xbt_dynar_get_as(tokens1, cursor, char*);
1426 tokens2 = xbt_str_split(tok, " ");
1427 tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1429 if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1430 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1431 new_element->type = e_dw_register;
1432 new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1433 xbt_dynar_push(loc->location.compose, &new_element);
1434 }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1435 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1436 new_element->type = e_dw_fbregister_op;
1437 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1438 xbt_dynar_push(loc->location.compose, &new_element);
1439 }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1440 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1441 new_element->type = e_dw_bregister_op;
1442 new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1443 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1444 xbt_dynar_push(loc->location.compose, &new_element);
1445 }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1446 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1447 new_element->type = e_dw_lit;
1448 new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1449 xbt_dynar_push(loc->location.compose, &new_element);
1450 }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1451 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1452 new_element->type = e_dw_piece;
1453 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1454 /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1455 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1457 new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1458 xbt_dynar_push(loc->location.compose, &new_element);
1459 }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1460 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1461 new_element->type = e_dw_plus_uconst;
1462 new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, 1, char *));
1463 xbt_dynar_push(loc->location.compose, &new_element);
1464 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1465 strcmp(tok, "DW_OP_and") == 0 ||
1466 strcmp(tok, "DW_OP_div") == 0 ||
1467 strcmp(tok, "DW_OP_minus") == 0 ||
1468 strcmp(tok, "DW_OP_mod") == 0 ||
1469 strcmp(tok, "DW_OP_mul") == 0 ||
1470 strcmp(tok, "DW_OP_neg") == 0 ||
1471 strcmp(tok, "DW_OP_not") == 0 ||
1472 strcmp(tok, "DW_OP_or") == 0 ||
1473 strcmp(tok, "DW_OP_plus") == 0){
1474 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1475 new_element->type = e_dw_arithmetic;
1476 new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1477 xbt_dynar_push(loc->location.compose, &new_element);
1478 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1479 }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1480 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1481 new_element->type = e_dw_deref;
1482 new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, 1, char*));
1483 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1484 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1486 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1487 xbt_dynar_push(loc->location.compose, &new_element);
1488 }else if(strcmp(tok, "DW_OP_deref") == 0){
1489 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1490 new_element->type = e_dw_deref;
1491 new_element->location.deref_size = sizeof(void *);
1492 xbt_dynar_push(loc->location.compose, &new_element);
1493 }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1494 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1495 new_element->type = e_dw_uconstant;
1496 new_element->location.uconstant.bytes = 1;
1497 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1498 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1499 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1501 new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1502 xbt_dynar_push(loc->location.compose, &new_element);
1503 }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1504 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1505 new_element->type = e_dw_sconstant;
1506 new_element->location.sconstant.bytes = 1;
1507 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1508 xbt_dynar_push(loc->location.compose, &new_element);
1509 }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1510 strcmp(tok2, "DW_OP_const2u:") == 0 ||
1511 strcmp(tok2, "DW_OP_const4u:") == 0 ||
1512 strcmp(tok2, "DW_OP_const8u:") == 0){
1513 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1514 new_element->type = e_dw_uconstant;
1515 new_element->location.uconstant.bytes = tok2[11] - '0';
1516 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1517 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1518 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1520 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1521 xbt_dynar_push(loc->location.compose, &new_element);
1522 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1523 strcmp(tok, "DW_OP_const2s") == 0 ||
1524 strcmp(tok, "DW_OP_const4s") == 0 ||
1525 strcmp(tok, "DW_OP_const8s") == 0){
1526 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1527 new_element->type = e_dw_sconstant;
1528 new_element->location.sconstant.bytes = tok2[11] - '0';
1529 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1530 xbt_dynar_push(loc->location.compose, &new_element);
1532 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1533 new_element->type = e_dw_unsupported;
1534 xbt_dynar_push(loc->location.compose, &new_element);
1538 xbt_dynar_free(&tokens2);
1542 xbt_dynar_free(&tokens1);
1551 void print_local_variables(xbt_dict_t list){
1553 dw_location_entry_t entry;
1554 dw_location_t location_entry;
1555 unsigned int cursor3 = 0, cursor4 = 0;
1556 xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1558 char *frame_name, *variable_name;
1559 dw_frame_t current_frame;
1560 dw_local_variable_t current_variable;
1562 xbt_dict_foreach(list, cursor, frame_name, current_frame){
1563 fprintf(stderr, "Frame name : %s\n", current_frame->name);
1564 fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1565 xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1566 fprintf(stderr, "Name : %s\n", current_variable->name);
1567 if(current_variable->location == NULL)
1569 fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1570 switch(current_variable->location->type){
1572 xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1573 fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1574 switch(entry->location->type){
1575 case e_dw_register :
1576 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1578 case e_dw_bregister_op:
1579 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1582 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1584 case e_dw_fbregister_op:
1585 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1588 fprintf(stderr, " Location :\n");
1589 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1590 switch(location_entry->type){
1591 case e_dw_register :
1592 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1594 case e_dw_bregister_op:
1595 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);
1598 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1600 case e_dw_fbregister_op:
1601 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1604 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);
1606 case e_dw_arithmetic :
1607 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1610 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1612 case e_dw_uconstant :
1613 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1615 case e_dw_sconstant :
1616 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1619 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1625 fprintf(stderr, "Location type not supported\n");
1632 fprintf(stderr, "Location :\n");
1633 xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1634 switch(location_entry->type){
1635 case e_dw_register :
1636 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1638 case e_dw_bregister_op:
1639 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);
1642 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1644 case e_dw_fbregister_op:
1645 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1648 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);
1650 case e_dw_arithmetic :
1651 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1654 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1656 case e_dw_uconstant :
1657 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1659 case e_dw_sconstant :
1660 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1663 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1669 fprintf(stderr, "Location type not supported\n");
1677 static void MC_get_global_variables(char *elf_file){
1681 char *command = bprintf("objdump -t -j .data -j .bss %s", elf_file);
1683 fp = popen(command, "r");
1686 perror("popen failed");
1690 if(mc_global_variables == NULL)
1691 mc_global_variables = xbt_dynar_new(sizeof(global_variable_t), global_variable_free_voidp);
1697 xbt_dynar_t line_tokens = NULL;
1698 unsigned long offset;
1700 int type = strcmp(elf_file, xbt_binary_name); /* 0 = binary, other = libsimgrid */
1702 while ((read = getline(&line, &n, fp)) != -1){
1707 /* Wipeout the new line character */
1708 line[read - 1] = '\0';
1710 xbt_str_strip_spaces(line);
1711 xbt_str_ltrim(line, NULL);
1713 line_tokens = xbt_str_split(line, NULL);
1715 if(xbt_dynar_length(line_tokens) <= 4 || strcmp(xbt_dynar_get_as(line_tokens, 0, char *), "SYMBOL") == 0)
1718 if((strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "__gcov", 6) == 0)
1719 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "gcov", 4) == 0)
1720 || (strcmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), ".data") == 0)
1721 || (strcmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), ".bss") == 0)
1722 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "stderr", 6) == 0)
1723 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "counter", 7) == 0)
1724 || ((size_t)strtoul(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 2, char*), NULL, 16) == 0))
1727 global_variable_t var = xbt_new0(s_global_variable_t, 1);
1730 var->address = (void *)strtoul(xbt_dynar_get_as(line_tokens, 0, char*), NULL, 16);
1732 offset = strtoul(xbt_dynar_get_as(line_tokens, 0, char*), NULL, 16);
1733 var->address = (char *)start_text_libsimgrid+offset;
1736 var->size = (size_t)strtoul(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 2, char*), NULL, 16);
1737 var->name = strdup(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*));
1739 if(data_bss_ignore_size(var->address) > 0)
1740 global_variable_free(var);
1742 xbt_dynar_push(mc_global_variables, &var);
1744 xbt_dynar_free(&line_tokens);
1754 void global_variable_free(global_variable_t v){
1759 void global_variable_free_voidp(void *v){
1760 global_variable_free((global_variable_t) * (void **) v);