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;
27 int _sg_do_model_check = 0;
28 int _sg_mc_checkpoint=0;
29 char* _sg_mc_property_file=NULL;
31 int _sg_mc_max_depth=1000;
35 extern int _sg_init_status;
36 void _mc_cfg_cb_reduce(const char *name, int pos) {
37 if (_sg_init_status && !_sg_do_model_check) {
38 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.");
40 char *val= xbt_cfg_get_string(_sg_cfg_set, name);
41 if (!strcasecmp(val,"none")) {
42 mc_reduce_kind = e_mc_reduce_none;
43 } else if (!strcasecmp(val,"dpor")) {
44 mc_reduce_kind = e_mc_reduce_dpor;
46 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
50 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
51 if (_sg_init_status && !_sg_do_model_check) {
52 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.");
54 _sg_mc_checkpoint = xbt_cfg_get_int(_sg_cfg_set, name);
56 void _mc_cfg_cb_property(const char *name, int pos) {
57 if (_sg_init_status && !_sg_do_model_check) {
58 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.");
60 _sg_mc_property_file= xbt_cfg_get_string(_sg_cfg_set, name);
63 void _mc_cfg_cb_timeout(const char *name, int pos) {
64 if (_sg_init_status && !_sg_do_model_check) {
65 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.");
67 _sg_mc_timeout= xbt_cfg_get_int(_sg_cfg_set, name);
70 void _mc_cfg_cb_max_depth(const char *name, int pos) {
71 if (_sg_init_status && !_sg_do_model_check) {
72 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.");
74 _sg_mc_max_depth= xbt_cfg_get_int(_sg_cfg_set, name);
77 void _mc_cfg_cb_visited(const char *name, int pos) {
78 if (_sg_init_status && !_sg_do_model_check) {
79 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.");
81 _sg_mc_visited= xbt_cfg_get_int(_sg_cfg_set, name);
85 /* MC global data structures */
87 mc_state_t mc_current_state = NULL;
88 char mc_replay_mode = FALSE;
89 double *mc_time = NULL;
93 xbt_fifo_t mc_stack_safety = NULL;
94 mc_stats_t mc_stats = NULL;
95 mc_global_t initial_state_safety = NULL;
99 mc_stats_pair_t mc_stats_pair = NULL;
100 xbt_fifo_t mc_stack_liveness = NULL;
101 mc_global_t initial_state_liveness = NULL;
105 xbt_dict_t mc_local_variables = NULL;
107 xbt_dynar_t mc_global_variables = NULL;
109 /* Ignore mechanism */
110 xbt_dynar_t mc_stack_comparison_ignore;
111 xbt_dynar_t mc_data_bss_comparison_ignore;
112 extern xbt_dynar_t mc_heap_comparison_ignore;
113 extern xbt_dynar_t stacks_areas;
115 xbt_automaton_t _mc_property_automaton = NULL;
117 /* Static functions */
119 static void MC_assert_pair(int prop);
120 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
121 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
122 static size_t data_bss_ignore_size(void *address);
123 static void MC_get_global_variables(char *elf_file);
125 void MC_do_the_modelcheck_for_real() {
126 if (!_sg_mc_property_file || _sg_mc_property_file[0]=='\0') {
127 if (mc_reduce_kind==e_mc_reduce_unset)
128 mc_reduce_kind=e_mc_reduce_dpor;
130 XBT_INFO("Check a safety property");
131 MC_modelcheck_safety();
135 if (mc_reduce_kind==e_mc_reduce_unset)
136 mc_reduce_kind=e_mc_reduce_none;
138 XBT_INFO("Check the liveness property %s",_sg_mc_property_file);
139 MC_automaton_load(_sg_mc_property_file);
140 MC_modelcheck_liveness();
145 void MC_compare(void){
151 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
153 mc_time = xbt_new0(double, simix_process_maxpid);
155 /* mc_time refers to clock for each process -> ignore it for heap comparison */
157 for(i = 0; i<simix_process_maxpid; i++)
158 MC_ignore_heap(&(mc_time[i]), sizeof(double));
162 /* Initialize the data structures that must be persistent across every
163 iteration of the model-checker (in RAW memory) */
167 MC_init_memory_map_info();
169 mc_local_variables = xbt_dict_new_homogeneous(NULL);
171 /* Get local variables in binary for state equality detection */
172 xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
173 MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
175 /* Get local variables in libsimgrid for state equality detection */
176 xbt_dict_t libsimgrid_location_list = MC_get_location_list(libsimgrid_path);
177 MC_get_local_variables(libsimgrid_path, libsimgrid_location_list, &mc_local_variables);
179 /* Get .plt section (start and end addresses) for data libsimgrid and data program comparison */
180 get_libsimgrid_plt_section();
181 get_binary_plt_section();
183 MC_ignore_data_bss(&end_raw_heap, sizeof(end_raw_heap));
185 /* Get global variables */
186 MC_get_global_variables(xbt_binary_name);
187 MC_get_global_variables(libsimgrid_path);
191 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
192 MC_ignore_stack("e", "*");
193 MC_ignore_stack("__ex_cleanup", "*");
194 MC_ignore_stack("__ex_mctx_en", "*");
195 MC_ignore_stack("__ex_mctx_me", "*");
196 MC_ignore_stack("__xbt_ex_ctx_ptr", "*");
197 MC_ignore_stack("_log_ev", "*");
198 MC_ignore_stack("_throw_ctx", "*");
199 MC_ignore_stack("ctx", "*");
206 void MC_modelcheck_safety(void)
208 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
210 /* Check if MC is already initialized */
211 if (initial_state_safety)
214 mc_time = xbt_new0(double, simix_process_maxpid);
216 /* Initialize the data structures that must be persistent across every
217 iteration of the model-checker (in RAW memory) */
221 /* Initialize statistics */
222 mc_stats = xbt_new0(s_mc_stats_t, 1);
223 mc_stats->state_size = 1;
225 /* Create exploration stack */
226 mc_stack_safety = xbt_fifo_new();
230 if(_sg_mc_visited > 0){
234 MC_init_memory_map_info();
235 get_libsimgrid_plt_section();
236 get_binary_plt_section();
243 /* Save the initial state */
244 initial_state_safety = xbt_new0(s_mc_global_t, 1);
245 initial_state_safety->snapshot = MC_take_snapshot();
256 void MC_modelcheck_liveness(){
258 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
264 /* Initialize statistics */
265 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
267 /* Create exploration stack */
268 mc_stack_liveness = xbt_fifo_new();
270 initial_state_liveness = xbt_new0(s_mc_global_t, 1);
277 MC_print_statistics_pairs(mc_stats_pair);
293 int MC_random(int min, int max)
295 /*FIXME: return mc_current_state->executed_transition->random.value;*/
300 * \brief Schedules all the process that are ready to run
302 void MC_wait_for_requests(void)
304 smx_process_t process;
308 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
309 SIMIX_process_runall();
310 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
311 req = &process->simcall;
312 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
313 SIMIX_simcall_pre(req, 0);
318 int MC_deadlock_check()
320 int deadlock = FALSE;
321 smx_process_t process;
322 if(xbt_swag_size(simix_global->process_list)){
324 xbt_swag_foreach(process, simix_global->process_list){
325 if(process->simcall.call != SIMCALL_NONE
326 && MC_request_is_enabled(&process->simcall)){
336 * \brief Re-executes from the state at position start all the transitions indicated by
337 * a given model-checker stack.
338 * \param stack The stack with the transitions to execute.
339 * \param start Start index to begin the re-execution.
341 void MC_replay(xbt_fifo_t stack, int start)
343 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
347 smx_simcall_t req = NULL, saved_req = NULL;
348 xbt_fifo_item_t item, start_item;
351 XBT_DEBUG("**** Begin Replay ****");
354 /* Restore the initial state */
355 MC_restore_snapshot(initial_state_safety->snapshot);
356 /* At the moment of taking the snapshot the raw heap was set, so restoring
357 * it will set it back again, we have to unset it to continue */
361 start_item = xbt_fifo_get_last_item(stack);
364 start_item = xbt_fifo_get_prev_item(start_item);
369 /* Traverse the stack from the state at position start and re-execute the transitions */
370 for (item = start_item;
371 item != xbt_fifo_get_first_item(stack);
372 item = xbt_fifo_get_prev_item(item)) {
374 state = (mc_state_t) xbt_fifo_get_item_content(item);
375 saved_req = MC_state_get_executed_request(state, &value);
378 /* because we got a copy of the executed request, we have to fetch the
379 real one, pointed by the request field of the issuer process */
380 req = &saved_req->issuer->simcall;
382 /* Debug information */
383 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
384 req_str = MC_request_to_string(req, value);
385 XBT_DEBUG("Replay: %s (%p)", req_str, state);
390 SIMIX_simcall_pre(req, value);
391 MC_wait_for_requests();
393 /* Update statistics */
394 mc_stats->visited_states++;
395 mc_stats->executed_transitions++;
397 XBT_DEBUG("**** End Replay ****");
407 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
410 initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
414 smx_simcall_t req = NULL, saved_req = NULL;
415 xbt_fifo_item_t item;
417 mc_pair_stateless_t pair;
420 XBT_DEBUG("**** Begin Replay ****");
422 /* Restore the initial state */
423 MC_restore_snapshot(initial_state_liveness->snapshot);
425 /* At the moment of taking the snapshot the raw heap was set, so restoring
426 * it will set it back again, we have to unset it to continue */
427 if(!initial_state_liveness->raw_mem_set)
432 item = xbt_fifo_get_last_item(stack);
434 while(depth <= xbt_fifo_size(stack)){
436 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
437 state = (mc_state_t) pair->graph_state;
439 if(pair->requests > 0){
441 saved_req = MC_state_get_executed_request(state, &value);
442 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
444 if(saved_req != NULL){
445 /* because we got a copy of the executed request, we have to fetch the
446 real one, pointed by the request field of the issuer process */
447 req = &saved_req->issuer->simcall;
448 //XBT_DEBUG("Req->call %u", req->call);
450 /* Debug information */
451 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
452 req_str = MC_request_to_string(req, value);
453 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
459 SIMIX_simcall_pre(req, value);
460 MC_wait_for_requests();
465 /* Update statistics */
466 mc_stats_pair->visited_pairs++;
468 item = xbt_fifo_get_prev_item(item);
473 /* Traverse the stack from the initial state and re-execute the transitions */
474 for (item = xbt_fifo_get_last_item(stack);
475 item != xbt_fifo_get_first_item(stack);
476 item = xbt_fifo_get_prev_item(item)) {
478 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
479 state = (mc_state_t) pair->graph_state;
481 if(pair->requests > 0){
483 saved_req = MC_state_get_executed_request(state, &value);
484 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
486 if(saved_req != NULL){
487 /* because we got a copy of the executed request, we have to fetch the
488 real one, pointed by the request field of the issuer process */
489 req = &saved_req->issuer->simcall;
490 //XBT_DEBUG("Req->call %u", req->call);
492 /* Debug information */
493 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
494 req_str = MC_request_to_string(req, value);
495 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
501 SIMIX_simcall_pre(req, value);
502 MC_wait_for_requests();
507 /* Update statistics */
508 mc_stats_pair->visited_pairs++;
512 XBT_DEBUG("**** End Replay ****");
514 if(initial_state_liveness->raw_mem_set)
522 * \brief Dumps the contents of a model-checker's stack and shows the actual
524 * \param stack The stack to dump
526 void MC_dump_stack_safety(xbt_fifo_t stack)
529 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
531 MC_show_stack_safety(stack);
533 if(!_sg_mc_checkpoint){
538 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
539 MC_state_delete(state);
552 void MC_show_stack_safety(xbt_fifo_t stack)
556 xbt_fifo_item_t item;
558 char *req_str = NULL;
560 for (item = xbt_fifo_get_last_item(stack);
561 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
562 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
563 req = MC_state_get_executed_request(state, &value);
565 req_str = MC_request_to_string(req, value);
566 XBT_INFO("%s", req_str);
572 void MC_show_deadlock(smx_simcall_t req)
574 /*char *req_str = NULL;*/
575 XBT_INFO("**************************");
576 XBT_INFO("*** DEAD-LOCK DETECTED ***");
577 XBT_INFO("**************************");
578 XBT_INFO("Locked request:");
579 /*req_str = MC_request_to_string(req);
580 XBT_INFO("%s", req_str);
582 XBT_INFO("Counter-example execution trace:");
583 MC_dump_stack_safety(mc_stack_safety);
587 void MC_show_stack_liveness(xbt_fifo_t stack){
589 mc_pair_stateless_t pair;
590 xbt_fifo_item_t item;
592 char *req_str = NULL;
594 for (item = xbt_fifo_get_last_item(stack);
595 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
596 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
597 req = MC_state_get_executed_request(pair->graph_state, &value);
599 if(pair->requests>0){
600 req_str = MC_request_to_string(req, value);
601 XBT_INFO("%s", req_str);
604 XBT_INFO("End of system requests but evolution in Büchi automaton");
610 void MC_dump_stack_liveness(xbt_fifo_t stack){
612 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
614 mc_pair_stateless_t pair;
617 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
618 pair_stateless_free(pair);
627 void MC_print_statistics(mc_stats_t stats)
629 //XBT_INFO("State space size ~= %lu", stats->state_size);
630 XBT_INFO("Expanded states = %lu", stats->expanded_states);
631 XBT_INFO("Visited states = %lu", stats->visited_states);
632 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
633 XBT_INFO("Expanded / Visited = %lf",
634 (double) stats->visited_states / stats->expanded_states);
635 /*XBT_INFO("Exploration coverage = %lf",
636 (double)stats->expanded_states / stats->state_size); */
639 void MC_print_statistics_pairs(mc_stats_pair_t stats)
641 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
642 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
643 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
644 XBT_INFO("Expanded / Visited = %lf",
645 (double) stats->visited_pairs / stats->expanded_pairs);
647 if(mmalloc_get_current_heap() == raw_heap)
651 void MC_assert(int prop)
653 if (MC_is_active() && !prop){
654 XBT_INFO("**************************");
655 XBT_INFO("*** PROPERTY NOT VALID ***");
656 XBT_INFO("**************************");
657 XBT_INFO("Counter-example execution trace:");
658 MC_dump_stack_safety(mc_stack_safety);
659 MC_print_statistics(mc_stats);
664 static void MC_assert_pair(int prop){
665 if (MC_is_active() && !prop) {
666 XBT_INFO("**************************");
667 XBT_INFO("*** PROPERTY NOT VALID ***");
668 XBT_INFO("**************************");
669 //XBT_INFO("Counter-example execution trace:");
670 MC_show_stack_liveness(mc_stack_liveness);
671 //MC_dump_snapshot_stack(mc_snapshot_stack);
672 MC_print_statistics_pairs(mc_stats_pair);
677 void MC_process_clock_add(smx_process_t process, double amount)
679 mc_time[process->pid] += amount;
682 double MC_process_clock_get(smx_process_t process)
686 return mc_time[process->pid];
694 void MC_automaton_load(const char *file){
696 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
700 if (_mc_property_automaton == NULL)
701 _mc_property_automaton = xbt_automaton_new();
703 xbt_automaton_load(_mc_property_automaton,file);
712 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
714 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
718 if (_mc_property_automaton == NULL)
719 _mc_property_automaton = xbt_automaton_new();
721 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
730 /************ MC_ignore ***********/
732 void MC_ignore_heap(void *address, size_t size){
734 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
738 if(mc_heap_comparison_ignore == NULL)
739 mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), NULL);
741 mc_heap_ignore_region_t region = NULL;
742 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
743 region->address = address;
746 if((address >= std_heap) && (address <= (void*)((char *)std_heap + STD_HEAP_SIZE))){
748 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
750 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
751 region->fragment = -1;
753 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
758 unsigned int cursor = 0;
759 mc_heap_ignore_region_t current_region;
760 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
761 if(current_region->address > address)
765 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
773 void MC_ignore_data_bss(void *address, size_t size){
775 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
779 if(mc_data_bss_comparison_ignore == NULL)
780 mc_data_bss_comparison_ignore = xbt_dynar_new(sizeof(mc_data_bss_ignore_variable_t), NULL);
782 if(xbt_dynar_is_empty(mc_data_bss_comparison_ignore)){
784 mc_data_bss_ignore_variable_t var = NULL;
785 var = xbt_new0(s_mc_data_bss_ignore_variable_t, 1);
786 var->address = address;
789 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, 0, &var);
793 unsigned int cursor = 0;
795 int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
796 mc_data_bss_ignore_variable_t current_var = NULL;
799 cursor = (start + end) / 2;
800 current_var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
801 if(current_var->address == address){
807 if(current_var->address < address)
809 if(current_var->address > address)
813 mc_data_bss_ignore_variable_t var = NULL;
814 var = xbt_new0(s_mc_data_bss_ignore_variable_t, 1);
815 var->address = address;
818 if(current_var->address < address)
819 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor + 1, &var);
821 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor, &var);
831 static size_t data_bss_ignore_size(void *address){
832 unsigned int cursor = 0;
834 int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
835 mc_data_bss_ignore_variable_t var;
838 cursor = (start + end) / 2;
839 var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
840 if(var->address == address)
842 if(var->address < address){
843 if((void *)((char *)var->address + var->size) > address)
844 return (char *)var->address + var->size - (char*)address;
848 if(var->address > address)
857 void MC_ignore_stack(const char *var_name, const char *frame){
859 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
863 if(mc_stack_comparison_ignore == NULL)
864 mc_stack_comparison_ignore = xbt_dynar_new(sizeof(mc_stack_ignore_variable_t), NULL);
866 if(xbt_dynar_is_empty(mc_stack_comparison_ignore)){
868 mc_stack_ignore_variable_t var = NULL;
869 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
870 var->var_name = strdup(var_name);
871 var->frame = strdup(frame);
873 xbt_dynar_insert_at(mc_stack_comparison_ignore, 0, &var);
877 unsigned int cursor = 0;
879 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
880 mc_stack_ignore_variable_t current_var = NULL;
883 cursor = (start + end) / 2;
884 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
885 if(strcmp(current_var->frame, frame) == 0){
886 if(strcmp(current_var->var_name, var_name) == 0){
892 if(strcmp(current_var->var_name, var_name) < 0)
894 if(strcmp(current_var->var_name, var_name) > 0)
897 if(strcmp(current_var->frame, frame) < 0)
899 if(strcmp(current_var->frame, frame) > 0)
903 mc_stack_ignore_variable_t var = NULL;
904 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
905 var->var_name = strdup(var_name);
906 var->frame = strdup(frame);
908 if(strcmp(current_var->frame, frame) < 0)
909 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor + 1, &var);
911 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor, &var);
922 void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
924 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
927 if(stacks_areas == NULL)
928 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
930 stack_region_t region = NULL;
931 region = xbt_new0(s_stack_region_t, 1);
932 region->address = stack;
933 region->process_name = strdup(name);
934 region->context = context;
936 xbt_dynar_push(stacks_areas, ®ion);
944 /************ DWARF ***********/
946 xbt_dict_t MC_get_location_list(const char *elf_file){
948 char *command = bprintf("objdump -Wo %s", elf_file);
950 FILE *fp = popen(command, "r");
953 perror("popen for objdump failed");
957 int debug = 0; /*Detect if the program has been compiled with -g */
959 xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
960 char *line = NULL, *loc_expr = NULL;
964 xbt_dynar_t split = NULL;
966 while ((read = getline(&line, &n, fp)) != -1) {
968 /* Wipeout the new line character */
969 line[read - 1] = '\0';
971 xbt_str_trim(line, NULL);
976 if(strlen(line) == 0)
981 if(strncmp(line, elf_file, strlen(elf_file)) == 0)
984 if(strncmp(line, "Contents", 8) == 0)
987 if(strncmp(line, "Offset", 6) == 0){
994 XBT_INFO("Your program must be compiled with -g");
998 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
1000 xbt_str_strip_spaces(line);
1001 split = xbt_str_split(line, " ");
1003 while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
1005 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
1006 new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
1007 new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
1010 while(cursor_remove < 3){
1011 xbt_dynar_remove_at(split, 0, NULL);
1015 loc_expr = xbt_str_join(split, " ");
1016 xbt_str_ltrim(loc_expr, "(");
1017 xbt_str_rtrim(loc_expr, ")");
1018 new_entry->location = get_location(NULL, loc_expr);
1020 xbt_dynar_push(loclist, &new_entry);
1022 xbt_dynar_free(&split);
1025 read = getline(&line, &n, fp);
1027 line[read - 1] = '\0';
1028 xbt_str_strip_spaces(line);
1029 split = xbt_str_split(line, " ");
1035 char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
1036 xbt_dict_set(location_list, key, loclist, NULL);
1038 xbt_dynar_free(&split);
1046 return location_list;
1049 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
1051 xbt_dict_cursor_t cursor = NULL;
1055 xbt_dict_foreach(all_variables, cursor, name, res) {
1056 if(offset >= res->start && offset < res->end)
1064 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
1066 char *command = bprintf("objdump -Wi %s", elf_file);
1068 FILE *fp = popen(command, "r");
1071 perror("popen for objdump failed");
1073 char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
1076 int valid_variable = 1;
1077 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
1078 xbt_dynar_t split = NULL, split2 = NULL;
1080 xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
1081 xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
1082 char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
1083 int new_frame = 0, new_variable = 0;
1084 dw_frame_t variable_frame, subroutine_frame = NULL;
1086 read = getline(&line, &n, fp);
1088 while (read != -1) {
1091 read = getline(&line, &n, fp);
1095 /* Wipeout the new line character */
1096 line[read - 1] = '\0';
1098 if(strlen(line) == 0){
1099 read = getline(&line, &n, fp);
1103 xbt_str_ltrim(line, NULL);
1104 xbt_str_strip_spaces(line);
1107 read = getline(&line, &n, fp);
1111 xbt_dynar_free(&split);
1112 split = xbt_str_split(line, " ");
1115 node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1117 if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
1119 dw_frame_t frame = NULL;
1121 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1122 subprogram_start = strdup(strtok(NULL, "<"));
1123 xbt_str_rtrim(subprogram_start, ">:");
1125 read = getline(&line, &n, fp);
1130 read = getline(&line, &n, fp);
1134 /* Wipeout the new line character */
1135 line[read - 1] = '\0';
1137 if(strlen(line) == 0){
1138 read = getline(&line, &n, fp);
1142 xbt_dynar_free(&split);
1143 xbt_str_rtrim(line, NULL);
1144 xbt_str_strip_spaces(line);
1145 split = xbt_str_split(line, " ");
1147 node_type = xbt_dynar_get_as(split, 1, char *);
1149 if(strncmp(node_type, "DW_AT_", 6) != 0)
1152 if(strcmp(node_type, "DW_AT_sibling") == 0){
1154 subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
1155 xbt_str_ltrim(subprogram_end, "<0x");
1156 xbt_str_rtrim(subprogram_end, ">");
1158 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
1161 abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
1162 xbt_str_ltrim(abstract_origin, "<0x");
1163 xbt_str_rtrim(abstract_origin, ">");
1164 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
1165 frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1167 }else if(strcmp(node_type, "DW_AT_name") == 0){
1170 free(current_frame);
1171 frame = xbt_new0(s_dw_frame_t, 1);
1172 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1173 frame->variables = xbt_dict_new_homogeneous(NULL);
1174 frame->frame_base = xbt_new0(s_dw_location_t, 1);
1175 current_frame = strdup(frame->name);
1177 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1179 }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
1181 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1183 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1185 frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1189 xbt_str_strip_spaces(line);
1190 split2 = xbt_str_split(line, "(");
1191 xbt_dynar_remove_at(split2, 0, NULL);
1192 loc_expr = xbt_str_join(split2, " ");
1193 xbt_str_rtrim(loc_expr, ")");
1194 frame->frame_base = get_location(NULL, loc_expr);
1195 xbt_dynar_free(&split2);
1199 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1202 frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1204 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1207 frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1209 }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1212 free(current_frame);
1213 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1214 current_frame = strdup(frame->name);
1215 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1219 read = getline(&line, &n, fp);
1224 frame->start = strtoul(subprogram_start, NULL, 16);
1225 if(subprogram_end != NULL)
1226 frame->end = strtoul(subprogram_end, NULL, 16);
1227 xbt_dict_set(*all_variables, frame->name, frame, NULL);
1230 free(subprogram_start);
1231 if(subprogram_end != NULL){
1232 free(subprogram_end);
1233 subprogram_end = NULL;
1237 }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1239 dw_local_variable_t var = NULL;
1241 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1242 origin = strdup(strtok(NULL, "<"));
1243 xbt_str_rtrim(origin, ">:");
1245 read = getline(&line, &n, fp);
1250 read = getline(&line, &n, fp);
1254 /* Wipeout the new line character */
1255 line[read - 1] = '\0';
1257 if(strlen(line) == 0){
1258 read = getline(&line, &n, fp);
1262 xbt_dynar_free(&split);
1263 xbt_str_rtrim(line, NULL);
1264 xbt_str_strip_spaces(line);
1265 split = xbt_str_split(line, " ");
1267 node_type = xbt_dynar_get_as(split, 1, char *);
1269 if(strncmp(node_type, "DW_AT_", 6) != 0)
1272 if(strcmp(node_type, "DW_AT_name") == 0){
1275 var = xbt_new0(s_dw_local_variable_t, 1);
1276 var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1278 xbt_dict_set(variables_origin, origin, var->name, NULL);
1280 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1283 abstract_origin = xbt_dynar_get_as(split, 2, char *);
1284 xbt_str_ltrim(abstract_origin, "<0x");
1285 xbt_str_rtrim(abstract_origin, ">");
1287 variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1288 variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1289 var = xbt_dict_get_or_null(variable_frame->variables, variable_name);
1291 }else if(strcmp(node_type, "DW_AT_location") == 0){
1293 if(valid_variable == 1 && var != NULL){
1295 var->location = xbt_new0(s_dw_location_t, 1);
1297 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1299 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1301 var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1305 xbt_str_strip_spaces(line);
1306 split2 = xbt_str_split(line, "(");
1307 xbt_dynar_remove_at(split2, 0, NULL);
1308 loc_expr = xbt_str_join(split2, " ");
1309 xbt_str_rtrim(loc_expr, ")");
1310 var->location = get_location(NULL, loc_expr);
1311 xbt_dynar_free(&split2);
1317 }else if(strcmp(node_type, "DW_AT_external") == 0){
1323 read = getline(&line, &n, fp);
1327 if(new_variable == 1 && valid_variable == 1){
1329 variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1330 xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1336 }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1338 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1339 origin = strdup(strtok(NULL, "<"));
1340 xbt_str_rtrim(origin, ">:");
1342 read = getline(&line, &n, fp);
1346 /* Wipeout the new line character */
1347 line[read - 1] = '\0';
1350 read = getline(&line, &n, fp);
1354 if(strlen(line) == 0){
1355 read = getline(&line, &n, fp);
1359 xbt_dynar_free(&split);
1360 xbt_str_rtrim(line, NULL);
1361 xbt_str_strip_spaces(line);
1362 split = xbt_str_split(line, " ");
1364 if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1367 node_type = xbt_dynar_get_as(split, 1, char *);
1369 if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1371 origin = xbt_dynar_get_as(split, 2, char *);
1372 xbt_str_ltrim(origin, "<0x");
1373 xbt_str_rtrim(origin, ">");
1375 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1376 subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1378 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1380 subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1382 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1384 subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1387 read = getline(&line, &n, fp);
1393 read = getline(&line, &n, fp);
1399 xbt_dynar_free(&split);
1406 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1408 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1410 if(location_list != NULL){
1412 char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1413 loc->type = e_dw_loclist;
1414 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1416 XBT_INFO("Key not found in loclist");
1422 char *tok = NULL, *tok2 = NULL;
1424 xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1425 xbt_dynar_t tokens2;
1427 loc->type = e_dw_compose;
1428 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1430 while(cursor < xbt_dynar_length(tokens1)){
1432 tok = xbt_dynar_get_as(tokens1, cursor, char*);
1433 tokens2 = xbt_str_split(tok, " ");
1434 tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1436 if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1437 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1438 new_element->type = e_dw_register;
1439 new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1440 xbt_dynar_push(loc->location.compose, &new_element);
1441 }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1442 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1443 new_element->type = e_dw_fbregister_op;
1444 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1445 xbt_dynar_push(loc->location.compose, &new_element);
1446 }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1447 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1448 new_element->type = e_dw_bregister_op;
1449 new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1450 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1451 xbt_dynar_push(loc->location.compose, &new_element);
1452 }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1453 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1454 new_element->type = e_dw_lit;
1455 new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1456 xbt_dynar_push(loc->location.compose, &new_element);
1457 }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1458 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1459 new_element->type = e_dw_piece;
1460 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1461 /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1462 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1464 new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1465 xbt_dynar_push(loc->location.compose, &new_element);
1466 }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1467 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1468 new_element->type = e_dw_plus_uconst;
1469 new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, 1, char *));
1470 xbt_dynar_push(loc->location.compose, &new_element);
1471 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1472 strcmp(tok, "DW_OP_and") == 0 ||
1473 strcmp(tok, "DW_OP_div") == 0 ||
1474 strcmp(tok, "DW_OP_minus") == 0 ||
1475 strcmp(tok, "DW_OP_mod") == 0 ||
1476 strcmp(tok, "DW_OP_mul") == 0 ||
1477 strcmp(tok, "DW_OP_neg") == 0 ||
1478 strcmp(tok, "DW_OP_not") == 0 ||
1479 strcmp(tok, "DW_OP_or") == 0 ||
1480 strcmp(tok, "DW_OP_plus") == 0){
1481 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1482 new_element->type = e_dw_arithmetic;
1483 new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1484 xbt_dynar_push(loc->location.compose, &new_element);
1485 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1486 }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1487 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1488 new_element->type = e_dw_deref;
1489 new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, 1, char*));
1490 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1491 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1493 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1494 xbt_dynar_push(loc->location.compose, &new_element);
1495 }else if(strcmp(tok, "DW_OP_deref") == 0){
1496 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1497 new_element->type = e_dw_deref;
1498 new_element->location.deref_size = sizeof(void *);
1499 xbt_dynar_push(loc->location.compose, &new_element);
1500 }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1501 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1502 new_element->type = e_dw_uconstant;
1503 new_element->location.uconstant.bytes = 1;
1504 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1505 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1506 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1508 new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1509 xbt_dynar_push(loc->location.compose, &new_element);
1510 }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1511 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1512 new_element->type = e_dw_sconstant;
1513 new_element->location.sconstant.bytes = 1;
1514 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1515 xbt_dynar_push(loc->location.compose, &new_element);
1516 }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1517 strcmp(tok2, "DW_OP_const2u:") == 0 ||
1518 strcmp(tok2, "DW_OP_const4u:") == 0 ||
1519 strcmp(tok2, "DW_OP_const8u:") == 0){
1520 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1521 new_element->type = e_dw_uconstant;
1522 new_element->location.uconstant.bytes = tok2[11] - '0';
1523 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1524 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1525 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1527 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1528 xbt_dynar_push(loc->location.compose, &new_element);
1529 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1530 strcmp(tok, "DW_OP_const2s") == 0 ||
1531 strcmp(tok, "DW_OP_const4s") == 0 ||
1532 strcmp(tok, "DW_OP_const8s") == 0){
1533 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1534 new_element->type = e_dw_sconstant;
1535 new_element->location.sconstant.bytes = tok2[11] - '0';
1536 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1537 xbt_dynar_push(loc->location.compose, &new_element);
1539 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1540 new_element->type = e_dw_unsupported;
1541 xbt_dynar_push(loc->location.compose, &new_element);
1545 xbt_dynar_free(&tokens2);
1549 xbt_dynar_free(&tokens1);
1558 void print_local_variables(xbt_dict_t list){
1560 dw_location_entry_t entry;
1561 dw_location_t location_entry;
1562 unsigned int cursor3 = 0, cursor4 = 0;
1563 xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1565 char *frame_name, *variable_name;
1566 dw_frame_t current_frame;
1567 dw_local_variable_t current_variable;
1569 xbt_dict_foreach(list, cursor, frame_name, current_frame){
1570 fprintf(stderr, "Frame name : %s\n", current_frame->name);
1571 fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1572 xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1573 fprintf(stderr, "Name : %s\n", current_variable->name);
1574 if(current_variable->location == NULL)
1576 fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1577 switch(current_variable->location->type){
1579 xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1580 fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1581 switch(entry->location->type){
1582 case e_dw_register :
1583 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1585 case e_dw_bregister_op:
1586 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1589 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1591 case e_dw_fbregister_op:
1592 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1595 fprintf(stderr, " Location :\n");
1596 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1597 switch(location_entry->type){
1598 case e_dw_register :
1599 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1601 case e_dw_bregister_op:
1602 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);
1605 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1607 case e_dw_fbregister_op:
1608 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1611 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);
1613 case e_dw_arithmetic :
1614 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1617 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1619 case e_dw_uconstant :
1620 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1622 case e_dw_sconstant :
1623 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1626 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1632 fprintf(stderr, "Location type not supported\n");
1639 fprintf(stderr, "Location :\n");
1640 xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1641 switch(location_entry->type){
1642 case e_dw_register :
1643 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1645 case e_dw_bregister_op:
1646 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);
1649 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1651 case e_dw_fbregister_op:
1652 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1655 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);
1657 case e_dw_arithmetic :
1658 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1661 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1663 case e_dw_uconstant :
1664 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1666 case e_dw_sconstant :
1667 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1670 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1676 fprintf(stderr, "Location type not supported\n");
1684 static void MC_get_global_variables(char *elf_file){
1688 char *command = bprintf("objdump -t -j .data -j .bss %s", elf_file);
1690 fp = popen(command, "r");
1693 perror("popen failed");
1697 if(mc_global_variables == NULL)
1698 mc_global_variables = xbt_dynar_new(sizeof(global_variable_t), global_variable_free_voidp);
1704 xbt_dynar_t line_tokens = NULL;
1705 unsigned long offset;
1707 int type = strcmp(elf_file, xbt_binary_name); /* 0 = binary, other = libsimgrid */
1709 while ((read = getline(&line, &n, fp)) != -1){
1714 /* Wipeout the new line character */
1715 line[read - 1] = '\0';
1717 xbt_str_strip_spaces(line);
1718 xbt_str_ltrim(line, NULL);
1720 line_tokens = xbt_str_split(line, NULL);
1722 if(xbt_dynar_length(line_tokens) <= 4 || strcmp(xbt_dynar_get_as(line_tokens, 0, char *), "SYMBOL") == 0)
1725 if((strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "__gcov", 6) == 0)
1726 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "gcov", 4) == 0)
1727 || (strcmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), ".data") == 0)
1728 || (strcmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), ".bss") == 0)
1729 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "stderr", 6) == 0)
1730 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "counter", 7) == 0)
1731 || ((size_t)strtoul(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 2, char*), NULL, 16) == 0))
1734 global_variable_t var = xbt_new0(s_global_variable_t, 1);
1737 var->address = (void *)strtoul(xbt_dynar_get_as(line_tokens, 0, char*), NULL, 16);
1739 offset = strtoul(xbt_dynar_get_as(line_tokens, 0, char*), NULL, 16);
1740 var->address = (char *)start_text_libsimgrid+offset;
1743 var->size = (size_t)strtoul(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 2, char*), NULL, 16);
1744 var->name = strdup(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*));
1746 if(data_bss_ignore_size(var->address) > 0)
1747 global_variable_free(var);
1749 xbt_dynar_push(mc_global_variables, &var);
1751 xbt_dynar_free(&line_tokens);
1761 void global_variable_free(global_variable_t v){
1766 void global_variable_free_voidp(void *v){
1767 global_variable_free((global_variable_t) * (void **) v);