1 /* Copyright (c) 2008-2013 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;
33 char *_sg_mc_dot_output_file = NULL;
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);
84 void _mc_cfg_cb_dot_output(const char *name, int pos) {
85 if (_sg_init_status && !_sg_do_model_check) {
86 xbt_die("You are specifying a file name for a dot output of graph state 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.");
88 _sg_mc_dot_output_file= xbt_cfg_get_string(_sg_cfg_set, name);
91 /* MC global data structures */
93 mc_state_t mc_current_state = NULL;
94 char mc_replay_mode = FALSE;
95 double *mc_time = NULL;
96 mc_comparison_times_t mc_comp_times = NULL;
97 double mc_snapshot_comparison_time;
98 mc_stats_t mc_stats = NULL;
102 xbt_fifo_t mc_stack_safety = NULL;
103 mc_global_t initial_state_safety = NULL;
107 xbt_fifo_t mc_stack_liveness = NULL;
108 mc_global_t initial_state_liveness = NULL;
112 xbt_dict_t mc_local_variables = NULL;
114 xbt_dynar_t mc_global_variables = NULL;
116 /* Ignore mechanism */
117 xbt_dynar_t mc_stack_comparison_ignore;
118 xbt_dynar_t mc_data_bss_comparison_ignore;
119 extern xbt_dynar_t mc_heap_comparison_ignore;
120 extern xbt_dynar_t stacks_areas;
122 FILE *dot_output = NULL;
123 const char* colors[10];
125 xbt_automaton_t _mc_property_automaton = NULL;
127 /* Static functions */
129 static void MC_assert_pair(int prop);
130 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
131 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
132 static size_t data_bss_ignore_size(void *address);
133 static void MC_get_global_variables(char *elf_file);
135 void MC_do_the_modelcheck_for_real() {
138 mc_comp_times = xbt_new0(s_mc_comparison_times_t, 1);
141 if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
143 MC_init_dot_output();
147 if (!_sg_mc_property_file || _sg_mc_property_file[0]=='\0') {
148 if (mc_reduce_kind==e_mc_reduce_unset)
149 mc_reduce_kind=e_mc_reduce_dpor;
151 XBT_INFO("Check a safety property");
152 MC_modelcheck_safety();
156 if (mc_reduce_kind==e_mc_reduce_unset)
157 mc_reduce_kind=e_mc_reduce_none;
159 XBT_INFO("Check the liveness property %s",_sg_mc_property_file);
160 MC_automaton_load(_sg_mc_property_file);
161 MC_modelcheck_liveness();
166 void MC_compare(void){
172 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
176 /* Initialize the data structures that must be persistent across every
177 iteration of the model-checker (in RAW memory) */
181 MC_init_memory_map_info();
183 mc_local_variables = xbt_dict_new_homogeneous(NULL);
185 /* Get local variables in binary for state equality detection */
186 xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
187 MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
189 /* Get local variables in libsimgrid for state equality detection */
190 xbt_dict_t libsimgrid_location_list = MC_get_location_list(libsimgrid_path);
191 MC_get_local_variables(libsimgrid_path, libsimgrid_location_list, &mc_local_variables);
193 xbt_dict_free(&libsimgrid_location_list);
194 xbt_dict_free(&binary_location_list);
196 /* Get .plt section (start and end addresses) for data libsimgrid and data program comparison */
197 get_libsimgrid_plt_section();
198 get_binary_plt_section();
200 /* Get global variables */
201 MC_get_global_variables(xbt_binary_name);
202 MC_get_global_variables(libsimgrid_path);
206 /* Ignore some variables from xbt/ex.h used by exception e for stacks comparison */
207 MC_ignore_stack("e", "*");
208 MC_ignore_stack("__ex_cleanup", "*");
209 MC_ignore_stack("__ex_mctx_en", "*");
210 MC_ignore_stack("__ex_mctx_me", "*");
211 MC_ignore_stack("__xbt_ex_ctx_ptr", "*");
212 MC_ignore_stack("_log_ev", "*");
213 MC_ignore_stack("_throw_ctx", "*");
214 MC_ignore_stack("ctx", "*");
216 MC_ignore_stack("next_context", "smx_ctx_sysv_suspend_serial");
217 MC_ignore_stack("i", "smx_ctx_sysv_suspend_serial");
219 /* Ignore local variable about time used for tracing */
220 MC_ignore_stack("start_time", "*");
222 MC_ignore_data_bss(&mc_comp_times, sizeof(mc_comp_times));
223 MC_ignore_data_bss(&mc_snapshot_comparison_time, sizeof(mc_snapshot_comparison_time));
224 MC_ignore_data_bss(&mc_time, sizeof(mc_time));
231 void MC_init_dot_output(){
238 colors[5] = "purple";
239 colors[6] = "yellow";
240 colors[7] = "orange";
242 dot_output = fopen(_sg_mc_dot_output_file, "w");
244 if(dot_output == NULL){
245 perror("Error open dot output file");
249 fprintf(dot_output, "digraph graphname{\n fixedsize=true; rankdir=TB; ranksep=.20; edge [fontsize=12]; node [fontsize=10, shape=circle,width=.5 ]; graph [resolution=20, fontsize=10];\n");
253 void MC_modelcheck_safety(void)
255 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
257 /* Check if MC is already initialized */
258 if (initial_state_safety)
261 mc_time = xbt_new0(double, simix_process_maxpid);
263 /* mc_time refers to clock for each process -> ignore it for heap comparison */
264 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
266 /* Initialize the data structures that must be persistent across every
267 iteration of the model-checker (in RAW memory) */
271 /* Initialize statistics */
272 mc_stats = xbt_new0(s_mc_stats_t, 1);
273 mc_stats->state_size = 1;
275 /* Create exploration stack */
276 mc_stack_safety = xbt_fifo_new();
280 if(_sg_mc_visited > 0){
284 MC_init_memory_map_info();
285 get_libsimgrid_plt_section();
286 get_binary_plt_section();
293 /* Save the initial state */
294 initial_state_safety = xbt_new0(s_mc_global_t, 1);
295 initial_state_safety->snapshot = MC_take_snapshot();
306 void MC_modelcheck_liveness(){
308 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
312 mc_time = xbt_new0(double, simix_process_maxpid);
314 /* mc_time refers to clock for each process -> ignore it for heap comparison */
315 MC_ignore_heap(mc_time, simix_process_maxpid * sizeof(double));
319 /* Initialize statistics */
320 mc_stats = xbt_new0(s_mc_stats_t, 1);
321 mc_stats->state_size = 1;
323 /* Create exploration stack */
324 mc_stack_liveness = xbt_fifo_new();
326 /* Create the initial state */
327 initial_state_liveness = xbt_new0(s_mc_global_t, 1);
334 MC_print_statistics(mc_stats);
346 if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
347 fprintf(dot_output, "}\n");
357 int SIMIX_pre_mc_random(smx_simcall_t simcall){
359 return simcall->mc_value;
365 /*FIXME: return mc_current_state->executed_transition->random.value;*/
366 return simcall_mc_random();
370 * \brief Schedules all the process that are ready to run
372 void MC_wait_for_requests(void)
374 smx_process_t process;
378 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
379 SIMIX_process_runall();
380 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
381 req = &process->simcall;
382 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
383 SIMIX_simcall_pre(req, 0);
388 int MC_deadlock_check()
390 int deadlock = FALSE;
391 smx_process_t process;
392 if(xbt_swag_size(simix_global->process_list)){
394 xbt_swag_foreach(process, simix_global->process_list){
395 if(process->simcall.call != SIMCALL_NONE
396 && MC_request_is_enabled(&process->simcall)){
406 * \brief Re-executes from the state at position start all the transitions indicated by
407 * a given model-checker stack.
408 * \param stack The stack with the transitions to execute.
409 * \param start Start index to begin the re-execution.
411 void MC_replay(xbt_fifo_t stack, int start)
413 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
415 int value, i = 1, count = 1;
417 smx_simcall_t req = NULL, saved_req = NULL;
418 xbt_fifo_item_t item, start_item;
420 smx_process_t process = NULL;
422 XBT_DEBUG("**** Begin Replay ****");
425 /* Restore the initial state */
426 MC_restore_snapshot(initial_state_safety->snapshot);
427 /* At the moment of taking the snapshot the raw heap was set, so restoring
428 * it will set it back again, we have to unset it to continue */
432 start_item = xbt_fifo_get_last_item(stack);
435 start_item = xbt_fifo_get_prev_item(start_item);
441 xbt_dict_reset(first_enabled_state);
442 xbt_swag_foreach(process, simix_global->process_list){
443 if(MC_process_is_enabled(process)){
444 char *key = bprintf("%lu", process->pid);
445 char *data = bprintf("%d", count);
446 xbt_dict_set(first_enabled_state, key, data, NULL);
453 /* Traverse the stack from the state at position start and re-execute the transitions */
454 for (item = start_item;
455 item != xbt_fifo_get_first_item(stack);
456 item = xbt_fifo_get_prev_item(item)) {
458 state = (mc_state_t) xbt_fifo_get_item_content(item);
459 saved_req = MC_state_get_executed_request(state, &value);
462 char *key = bprintf("%lu", saved_req->issuer->pid);
463 xbt_dict_remove(first_enabled_state, key);
468 /* because we got a copy of the executed request, we have to fetch the
469 real one, pointed by the request field of the issuer process */
470 req = &saved_req->issuer->simcall;
472 /* Debug information */
473 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
474 req_str = MC_request_to_string(req, value);
475 XBT_DEBUG("Replay: %s (%p)", req_str, state);
480 SIMIX_simcall_pre(req, value);
481 MC_wait_for_requests();
486 /* Insert in dict all enabled processes */
487 xbt_swag_foreach(process, simix_global->process_list){
488 if(MC_process_is_enabled(process) /*&& !MC_state_process_is_done(state, process)*/){
489 char *key = bprintf("%lu", process->pid);
490 if(xbt_dict_get_or_null(first_enabled_state, key) == NULL){
491 char *data = bprintf("%d", count);
492 xbt_dict_set(first_enabled_state, key, data, NULL);
499 /* Update statistics */
500 mc_stats->visited_states++;
501 mc_stats->executed_transitions++;
505 XBT_DEBUG("**** End Replay ****");
515 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
518 initial_state_liveness->raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
522 smx_simcall_t req = NULL, saved_req = NULL;
523 xbt_fifo_item_t item;
528 XBT_DEBUG("**** Begin Replay ****");
530 /* Restore the initial state */
531 MC_restore_snapshot(initial_state_liveness->snapshot);
533 /* At the moment of taking the snapshot the raw heap was set, so restoring
534 * it will set it back again, we have to unset it to continue */
535 if(!initial_state_liveness->raw_mem_set)
540 item = xbt_fifo_get_last_item(stack);
542 while(depth <= xbt_fifo_size(stack)){
544 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
545 state = (mc_state_t) pair->graph_state;
547 if(pair->requests > 0){
549 saved_req = MC_state_get_executed_request(state, &value);
550 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
552 if(saved_req != NULL){
553 /* because we got a copy of the executed request, we have to fetch the
554 real one, pointed by the request field of the issuer process */
555 req = &saved_req->issuer->simcall;
556 //XBT_DEBUG("Req->call %u", req->call);
558 /* Debug information */
559 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
560 req_str = MC_request_to_string(req, value);
561 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
567 SIMIX_simcall_pre(req, value);
568 MC_wait_for_requests();
573 /* Update statistics */
574 mc_stats->visited_pairs++;
575 mc_stats->executed_transitions++;
577 item = xbt_fifo_get_prev_item(item);
582 /* Traverse the stack from the initial state and re-execute the transitions */
583 for (item = xbt_fifo_get_last_item(stack);
584 item != xbt_fifo_get_first_item(stack);
585 item = xbt_fifo_get_prev_item(item)) {
587 pair = (mc_pair_t) xbt_fifo_get_item_content(item);
588 state = (mc_state_t) pair->graph_state;
590 if(pair->requests > 0){
592 saved_req = MC_state_get_executed_request(state, &value);
593 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
595 if(saved_req != NULL){
596 /* because we got a copy of the executed request, we have to fetch the
597 real one, pointed by the request field of the issuer process */
598 req = &saved_req->issuer->simcall;
599 //XBT_DEBUG("Req->call %u", req->call);
601 /* Debug information */
602 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
603 req_str = MC_request_to_string(req, value);
604 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
610 SIMIX_simcall_pre(req, value);
611 MC_wait_for_requests();
616 /* Update statistics */
617 mc_stats->visited_pairs++;
618 mc_stats->executed_transitions++;
622 XBT_DEBUG("**** End Replay ****");
624 if(initial_state_liveness->raw_mem_set)
632 * \brief Dumps the contents of a model-checker's stack and shows the actual
634 * \param stack The stack to dump
636 void MC_dump_stack_safety(xbt_fifo_t stack)
639 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
641 MC_show_stack_safety(stack);
643 if(!_sg_mc_checkpoint){
648 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
649 MC_state_delete(state);
662 void MC_show_stack_safety(xbt_fifo_t stack)
666 xbt_fifo_item_t item;
668 char *req_str = NULL;
670 for (item = xbt_fifo_get_last_item(stack);
671 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
672 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
673 req = MC_state_get_executed_request(state, &value);
675 req_str = MC_request_to_string(req, value);
676 XBT_INFO("%s", req_str);
682 void MC_show_deadlock(smx_simcall_t req)
684 /*char *req_str = NULL;*/
685 XBT_INFO("**************************");
686 XBT_INFO("*** DEAD-LOCK DETECTED ***");
687 XBT_INFO("**************************");
688 XBT_INFO("Locked request:");
689 /*req_str = MC_request_to_string(req);
690 XBT_INFO("%s", req_str);
692 XBT_INFO("Counter-example execution trace:");
693 MC_dump_stack_safety(mc_stack_safety);
697 void MC_show_stack_liveness(xbt_fifo_t stack){
700 xbt_fifo_item_t item;
702 char *req_str = NULL;
704 for (item = xbt_fifo_get_last_item(stack);
705 (item ? (pair = (mc_pair_t) (xbt_fifo_get_item_content(item)))
706 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
707 req = MC_state_get_executed_request(pair->graph_state, &value);
709 if(pair->requests>0){
710 req_str = MC_request_to_string(req, value);
711 XBT_INFO("%s", req_str);
714 XBT_INFO("End of system requests but evolution in Büchi automaton");
720 void MC_dump_stack_liveness(xbt_fifo_t stack){
722 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
727 while ((pair = (mc_pair_t) xbt_fifo_pop(stack)) != NULL)
728 MC_pair_delete(pair);
737 void MC_print_statistics(mc_stats_t stats)
739 if(stats->expanded_pairs == 0){
740 XBT_INFO("Expanded states = %lu", stats->expanded_states);
741 XBT_INFO("Visited states = %lu", stats->visited_states);
743 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
744 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
746 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
749 void MC_assert(int prop)
751 if (MC_is_active() && !prop){
752 XBT_INFO("**************************");
753 XBT_INFO("*** PROPERTY NOT VALID ***");
754 XBT_INFO("**************************");
755 XBT_INFO("Counter-example execution trace:");
756 MC_dump_stack_safety(mc_stack_safety);
757 MC_print_statistics(mc_stats);
759 if((_sg_mc_dot_output_file != NULL) && (_sg_mc_dot_output_file[0]!='\0')){
760 fprintf(dot_output, "}\n");
768 static void MC_assert_pair(int prop){
769 if (MC_is_active() && !prop) {
770 XBT_INFO("**************************");
771 XBT_INFO("*** PROPERTY NOT VALID ***");
772 XBT_INFO("**************************");
773 //XBT_INFO("Counter-example execution trace:");
774 MC_show_stack_liveness(mc_stack_liveness);
775 //MC_dump_snapshot_stack(mc_snapshot_stack);
776 MC_print_statistics(mc_stats);
781 void MC_process_clock_add(smx_process_t process, double amount)
783 mc_time[process->pid] += amount;
786 double MC_process_clock_get(smx_process_t process)
790 return mc_time[process->pid];
798 void MC_automaton_load(const char *file){
800 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
804 if (_mc_property_automaton == NULL)
805 _mc_property_automaton = xbt_automaton_new();
807 xbt_automaton_load(_mc_property_automaton,file);
816 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
818 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
822 if (_mc_property_automaton == NULL)
823 _mc_property_automaton = xbt_automaton_new();
825 xbt_automaton_propositional_symbol_new(_mc_property_automaton,id,fct);
834 /************ MC_ignore ***********/
836 void heap_ignore_region_free(mc_heap_ignore_region_t r){
840 void heap_ignore_region_free_voidp(void *r){
841 heap_ignore_region_free((mc_heap_ignore_region_t) * (void **) r);
844 void MC_ignore_heap(void *address, size_t size){
846 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
850 mc_heap_ignore_region_t region = NULL;
851 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
852 region->address = address;
855 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
857 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
858 region->fragment = -1;
859 ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_block.ignore++;
861 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
862 ((xbt_mheap_t)std_heap)->heapinfo[region->block].busy_frag.ignore[region->fragment]++;
865 if(mc_heap_comparison_ignore == NULL){
866 mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), heap_ignore_region_free_voidp);
867 xbt_dynar_push(mc_heap_comparison_ignore, ®ion);
873 unsigned int cursor = 0;
874 mc_heap_ignore_region_t current_region = NULL;
876 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
879 cursor = (start + end) / 2;
880 current_region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
881 if(current_region->address == address){
882 heap_ignore_region_free(region);
887 if(current_region->address < address)
889 if(current_region->address > address)
893 if(current_region->address < address)
894 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor + 1, ®ion);
896 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
904 void MC_remove_ignore_heap(void *address, size_t size){
906 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
910 unsigned int cursor = 0;
912 int end = xbt_dynar_length(mc_heap_comparison_ignore) - 1;
913 mc_heap_ignore_region_t region;
914 int ignore_found = 0;
917 cursor = (start + end) / 2;
918 region = (mc_heap_ignore_region_t)xbt_dynar_get_as(mc_heap_comparison_ignore, cursor, mc_heap_ignore_region_t);
919 if(region->address == address){
923 if(region->address < address)
925 if(region->address > address){
926 if((char * )region->address <= ((char *)address + size)){
934 if(ignore_found == 1){
935 xbt_dynar_remove_at(mc_heap_comparison_ignore, cursor, NULL);
936 MC_remove_ignore_heap(address, size);
946 void MC_ignore_data_bss(void *address, size_t size){
948 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
952 if(mc_data_bss_comparison_ignore == NULL)
953 mc_data_bss_comparison_ignore = xbt_dynar_new(sizeof(mc_data_bss_ignore_variable_t), NULL);
955 mc_data_bss_ignore_variable_t var = NULL;
956 var = xbt_new0(s_mc_data_bss_ignore_variable_t, 1);
957 var->address = address;
960 if(xbt_dynar_is_empty(mc_data_bss_comparison_ignore)){
962 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, 0, &var);
966 unsigned int cursor = 0;
968 int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
969 mc_data_bss_ignore_variable_t current_var = NULL;
972 cursor = (start + end) / 2;
973 current_var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
974 if(current_var->address == address){
980 if(current_var->address < address)
982 if(current_var->address > address)
986 if(current_var->address < address)
987 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor + 1, &var);
989 xbt_dynar_insert_at(mc_data_bss_comparison_ignore, cursor, &var);
993 /* Remove variable from mc_global_variables */
995 if(mc_global_variables != NULL){
997 unsigned int cursor = 0;
999 int end = xbt_dynar_length(mc_global_variables) - 1;
1000 global_variable_t current_var;
1003 while(start <= end){
1004 cursor = (start + end) / 2;
1005 current_var = (global_variable_t)xbt_dynar_get_as(mc_global_variables, cursor, global_variable_t);
1006 if(current_var->address == var->address){
1010 if(current_var->address < address)
1012 if(current_var->address > address)
1017 xbt_dynar_remove_at(mc_global_variables, cursor, NULL);
1027 static size_t data_bss_ignore_size(void *address){
1028 unsigned int cursor = 0;
1030 int end = xbt_dynar_length(mc_data_bss_comparison_ignore) - 1;
1031 mc_data_bss_ignore_variable_t var;
1033 while(start <= end){
1034 cursor = (start + end) / 2;
1035 var = (mc_data_bss_ignore_variable_t)xbt_dynar_get_as(mc_data_bss_comparison_ignore, cursor, mc_data_bss_ignore_variable_t);
1036 if(var->address == address)
1038 if(var->address < address){
1039 if((void *)((char *)var->address + var->size) > address)
1040 return (char *)var->address + var->size - (char*)address;
1044 if(var->address > address)
1053 void MC_ignore_stack(const char *var_name, const char *frame_name){
1055 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1059 if(mc_stack_comparison_ignore == NULL)
1060 mc_stack_comparison_ignore = xbt_dynar_new(sizeof(mc_stack_ignore_variable_t), NULL);
1062 if(xbt_dynar_is_empty(mc_stack_comparison_ignore)){
1064 mc_stack_ignore_variable_t var = NULL;
1065 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
1066 var->var_name = strdup(var_name);
1067 var->frame = strdup(frame_name);
1069 xbt_dynar_insert_at(mc_stack_comparison_ignore, 0, &var);
1073 unsigned int cursor = 0;
1075 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
1076 mc_stack_ignore_variable_t current_var = NULL;
1078 while(start <= end){
1079 cursor = (start + end) / 2;
1080 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
1081 if(strcmp(current_var->frame, frame_name) == 0){
1082 if(strcmp(current_var->var_name, var_name) == 0){
1088 if(strcmp(current_var->var_name, var_name) < 0)
1090 if(strcmp(current_var->var_name, var_name) > 0)
1093 if(strcmp(current_var->frame, frame_name) < 0)
1095 if(strcmp(current_var->frame, frame_name) > 0)
1099 mc_stack_ignore_variable_t var = NULL;
1100 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
1101 var->var_name = strdup(var_name);
1102 var->frame = strdup(frame_name);
1104 if(strcmp(current_var->frame, frame_name) < 0)
1105 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor + 1, &var);
1107 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor, &var);
1111 /* Remove variable from mc_local_variables */
1113 if(mc_local_variables != NULL){
1115 if(strcmp(frame_name, "*") != 0){
1116 dw_frame_t frame = xbt_dict_get_or_null(mc_local_variables, frame_name);
1118 xbt_dict_remove(frame->variables, var_name);
1130 void MC_new_stack_area(void *stack, char *name, void* context, size_t size){
1132 int raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
1135 if(stacks_areas == NULL)
1136 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
1138 stack_region_t region = NULL;
1139 region = xbt_new0(s_stack_region_t, 1);
1140 region->address = stack;
1141 region->process_name = strdup(name);
1142 region->context = context;
1143 region->size = size;
1144 region->block = ((char*)stack - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
1145 xbt_dynar_push(stacks_areas, ®ion);
1153 /************ DWARF ***********/
1155 xbt_dict_t MC_get_location_list(const char *elf_file){
1157 char *command = bprintf("objdump -Wo %s", elf_file);
1159 FILE *fp = popen(command, "r");
1162 perror("popen for objdump failed");
1166 int debug = 0; /*Detect if the program has been compiled with -g */
1168 xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
1169 char *line = NULL, *loc_expr = NULL;
1173 xbt_dynar_t split = NULL;
1175 while ((read = xbt_getline(&line, &n, fp)) != -1) {
1177 /* Wipeout the new line character */
1178 line[read - 1] = '\0';
1180 xbt_str_trim(line, NULL);
1185 if(strlen(line) == 0)
1190 if(strncmp(line, elf_file, strlen(elf_file)) == 0)
1193 if(strncmp(line, "Contents", 8) == 0)
1196 if(strncmp(line, "Offset", 6) == 0){
1203 XBT_INFO("Your program must be compiled with -g");
1207 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
1209 xbt_str_strip_spaces(line);
1210 split = xbt_str_split(line, " ");
1212 while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
1214 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
1215 new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
1216 new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
1219 while(cursor_remove < 3){
1220 xbt_dynar_remove_at(split, 0, NULL);
1224 loc_expr = xbt_str_join(split, " ");
1225 xbt_str_ltrim(loc_expr, "(");
1226 xbt_str_rtrim(loc_expr, ")");
1227 new_entry->location = get_location(NULL, loc_expr);
1229 xbt_dynar_push(loclist, &new_entry);
1231 xbt_dynar_free(&split);
1234 read = xbt_getline(&line, &n, fp);
1236 line[read - 1] = '\0';
1237 xbt_str_strip_spaces(line);
1238 split = xbt_str_split(line, " ");
1244 char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
1245 xbt_dict_set(location_list, key, loclist, NULL);
1248 xbt_dynar_free(&split);
1256 return location_list;
1259 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
1261 xbt_dict_cursor_t cursor = NULL;
1265 xbt_dict_foreach(all_variables, cursor, name, res) {
1266 if(offset >= res->start && offset < res->end){
1267 xbt_dict_cursor_free(&cursor);
1272 xbt_dict_cursor_free(&cursor);
1277 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
1279 char *command = bprintf("objdump -Wi %s", elf_file);
1281 FILE *fp = popen(command, "r");
1284 perror("popen for objdump failed");
1286 char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
1289 int valid_variable = 1;
1290 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
1291 xbt_dynar_t split = NULL, split2 = NULL;
1293 xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
1294 xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
1295 char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
1296 int new_frame = 0, new_variable = 0;
1297 dw_frame_t variable_frame, subroutine_frame = NULL;
1299 read = xbt_getline(&line, &n, fp);
1301 while (read != -1) {
1304 read = xbt_getline(&line, &n, fp);
1308 /* Wipeout the new line character */
1309 line[read - 1] = '\0';
1311 if(strlen(line) == 0){
1312 read = xbt_getline(&line, &n, fp);
1316 xbt_str_ltrim(line, NULL);
1317 xbt_str_strip_spaces(line);
1320 read = xbt_getline(&line, &n, fp);
1324 xbt_dynar_free(&split);
1325 split = xbt_str_split(line, " ");
1328 node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1330 if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
1332 dw_frame_t frame = NULL;
1334 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1335 subprogram_start = strdup(strtok(NULL, "<"));
1336 xbt_str_rtrim(subprogram_start, ">:");
1338 read = xbt_getline(&line, &n, fp);
1343 read = xbt_getline(&line, &n, fp);
1347 /* Wipeout the new line character */
1348 line[read - 1] = '\0';
1350 if(strlen(line) == 0){
1351 read = xbt_getline(&line, &n, fp);
1355 xbt_dynar_free(&split);
1356 xbt_str_rtrim(line, NULL);
1357 xbt_str_strip_spaces(line);
1358 split = xbt_str_split(line, " ");
1360 node_type = xbt_dynar_get_as(split, 1, char *);
1362 if(strncmp(node_type, "DW_AT_", 6) != 0)
1365 if(strcmp(node_type, "DW_AT_sibling") == 0){
1367 subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
1368 xbt_str_ltrim(subprogram_end, "<0x");
1369 xbt_str_rtrim(subprogram_end, ">");
1371 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
1374 abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
1375 xbt_str_ltrim(abstract_origin, "<0x");
1376 xbt_str_rtrim(abstract_origin, ">");
1377 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
1378 frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1379 xbt_free(abstract_origin);
1381 }else if(strcmp(node_type, "DW_AT_name") == 0){
1384 xbt_free(current_frame);
1385 frame = xbt_new0(s_dw_frame_t, 1);
1386 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1387 frame->variables = xbt_dict_new_homogeneous(NULL);
1388 frame->frame_base = xbt_new0(s_dw_location_t, 1);
1389 current_frame = strdup(frame->name);
1391 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1393 }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
1395 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1397 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1399 frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1403 xbt_str_strip_spaces(line);
1404 split2 = xbt_str_split(line, "(");
1405 xbt_dynar_remove_at(split2, 0, NULL);
1406 loc_expr = xbt_str_join(split2, " ");
1407 xbt_str_rtrim(loc_expr, ")");
1408 frame->frame_base = get_location(NULL, loc_expr);
1409 xbt_dynar_free(&split2);
1414 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1417 frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1419 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1422 frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1424 }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1426 xbt_free(frame->name);
1427 xbt_free(current_frame);
1428 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1429 current_frame = strdup(frame->name);
1430 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1434 read = xbt_getline(&line, &n, fp);
1439 frame->start = strtoul(subprogram_start, NULL, 16);
1440 if(subprogram_end != NULL)
1441 frame->end = strtoul(subprogram_end, NULL, 16);
1442 xbt_dict_set(*all_variables, frame->name, frame, NULL);
1445 xbt_free(subprogram_start);
1446 xbt_free(subprogram_end);
1447 subprogram_end = NULL;
1450 }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1452 dw_local_variable_t var = NULL;
1454 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1455 origin = strdup(strtok(NULL, "<"));
1456 xbt_str_rtrim(origin, ">:");
1458 read = xbt_getline(&line, &n, fp);
1463 read = xbt_getline(&line, &n, fp);
1467 /* Wipeout the new line character */
1468 line[read - 1] = '\0';
1470 if(strlen(line) == 0){
1471 read = xbt_getline(&line, &n, fp);
1475 xbt_dynar_free(&split);
1476 xbt_str_rtrim(line, NULL);
1477 xbt_str_strip_spaces(line);
1478 split = xbt_str_split(line, " ");
1480 node_type = xbt_dynar_get_as(split, 1, char *);
1482 if(strncmp(node_type, "DW_AT_", 6) != 0)
1485 if(strcmp(node_type, "DW_AT_name") == 0){
1488 var = xbt_new0(s_dw_local_variable_t, 1);
1489 var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1491 xbt_dict_set(variables_origin, origin, var->name, NULL);
1493 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1496 abstract_origin = xbt_dynar_get_as(split, 2, char *);
1497 xbt_str_ltrim(abstract_origin, "<0x");
1498 xbt_str_rtrim(abstract_origin, ">");
1500 variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1501 variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1502 var = xbt_dict_get_or_null(variable_frame->variables, variable_name);
1504 }else if(strcmp(node_type, "DW_AT_location") == 0){
1506 if(valid_variable == 1 && var != NULL){
1508 var->location = xbt_new0(s_dw_location_t, 1);
1510 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1512 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1514 var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1518 xbt_str_strip_spaces(line);
1519 split2 = xbt_str_split(line, "(");
1520 xbt_dynar_remove_at(split2, 0, NULL);
1521 loc_expr = xbt_str_join(split2, " ");
1522 xbt_str_rtrim(loc_expr, ")");
1523 var->location = get_location(NULL, loc_expr);
1524 xbt_dynar_free(&split2);
1531 }else if(strcmp(node_type, "DW_AT_external") == 0){
1537 read = xbt_getline(&line, &n, fp);
1541 if(new_variable == 1 && valid_variable == 1){
1543 variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1544 xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1550 }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1552 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1553 origin = strdup(strtok(NULL, "<"));
1554 xbt_str_rtrim(origin, ">:");
1556 read = xbt_getline(&line, &n, fp);
1560 /* Wipeout the new line character */
1561 line[read - 1] = '\0';
1564 read = xbt_getline(&line, &n, fp);
1568 if(strlen(line) == 0){
1569 read = xbt_getline(&line, &n, fp);
1573 xbt_dynar_free(&split);
1574 xbt_str_rtrim(line, NULL);
1575 xbt_str_strip_spaces(line);
1576 split = xbt_str_split(line, " ");
1578 if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1581 node_type = xbt_dynar_get_as(split, 1, char *);
1583 if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1585 origin = xbt_dynar_get_as(split, 2, char *);
1586 xbt_str_ltrim(origin, "<0x");
1587 xbt_str_rtrim(origin, ">");
1589 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1590 subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1592 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1594 subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1596 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1598 subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1601 read = xbt_getline(&line, &n, fp);
1607 read = xbt_getline(&line, &n, fp);
1613 xbt_dynar_free(&split);
1620 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1622 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1624 if(location_list != NULL){
1626 char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1627 loc->type = e_dw_loclist;
1628 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1629 if(loc->location.loclist == NULL)
1630 XBT_INFO("Key not found in loclist");
1637 char *tok = NULL, *tok2 = NULL;
1639 xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1640 xbt_dynar_t tokens2;
1642 loc->type = e_dw_compose;
1643 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1645 while(cursor < xbt_dynar_length(tokens1)){
1647 tok = xbt_dynar_get_as(tokens1, cursor, char*);
1648 tokens2 = xbt_str_split(tok, " ");
1649 tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1651 if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1652 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1653 new_element->type = e_dw_register;
1654 new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1655 xbt_dynar_push(loc->location.compose, &new_element);
1656 }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1657 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1658 new_element->type = e_dw_fbregister_op;
1659 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
1660 xbt_dynar_push(loc->location.compose, &new_element);
1661 }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1662 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1663 new_element->type = e_dw_bregister_op;
1664 new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1665 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
1666 xbt_dynar_push(loc->location.compose, &new_element);
1667 }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1668 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1669 new_element->type = e_dw_lit;
1670 new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1671 xbt_dynar_push(loc->location.compose, &new_element);
1672 }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1673 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1674 new_element->type = e_dw_piece;
1675 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
1676 /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1677 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1679 new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1680 xbt_dynar_push(loc->location.compose, &new_element);
1681 }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1682 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1683 new_element->type = e_dw_plus_uconst;
1684 new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char *));
1685 xbt_dynar_push(loc->location.compose, &new_element);
1686 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1687 strcmp(tok, "DW_OP_and") == 0 ||
1688 strcmp(tok, "DW_OP_div") == 0 ||
1689 strcmp(tok, "DW_OP_minus") == 0 ||
1690 strcmp(tok, "DW_OP_mod") == 0 ||
1691 strcmp(tok, "DW_OP_mul") == 0 ||
1692 strcmp(tok, "DW_OP_neg") == 0 ||
1693 strcmp(tok, "DW_OP_not") == 0 ||
1694 strcmp(tok, "DW_OP_or") == 0 ||
1695 strcmp(tok, "DW_OP_plus") == 0){
1696 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1697 new_element->type = e_dw_arithmetic;
1698 new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1699 xbt_dynar_push(loc->location.compose, &new_element);
1700 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1701 }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1702 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1703 new_element->type = e_dw_deref;
1704 new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*));
1705 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1706 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1708 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1709 xbt_dynar_push(loc->location.compose, &new_element);
1710 }else if(strcmp(tok, "DW_OP_deref") == 0){
1711 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1712 new_element->type = e_dw_deref;
1713 new_element->location.deref_size = sizeof(void *);
1714 xbt_dynar_push(loc->location.compose, &new_element);
1715 }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1716 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1717 new_element->type = e_dw_uconstant;
1718 new_element->location.uconstant.bytes = 1;
1719 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
1720 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1721 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1723 new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1724 xbt_dynar_push(loc->location.compose, &new_element);
1725 }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1726 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1727 new_element->type = e_dw_sconstant;
1728 new_element->location.sconstant.bytes = 1;
1729 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
1730 xbt_dynar_push(loc->location.compose, &new_element);
1731 }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1732 strcmp(tok2, "DW_OP_const2u:") == 0 ||
1733 strcmp(tok2, "DW_OP_const4u:") == 0 ||
1734 strcmp(tok2, "DW_OP_const8u:") == 0){
1735 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1736 new_element->type = e_dw_uconstant;
1737 new_element->location.uconstant.bytes = tok2[11] - '0';
1738 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
1739 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1740 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1742 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1743 xbt_dynar_push(loc->location.compose, &new_element);
1744 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1745 strcmp(tok, "DW_OP_const2s") == 0 ||
1746 strcmp(tok, "DW_OP_const4s") == 0 ||
1747 strcmp(tok, "DW_OP_const8s") == 0){
1748 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1749 new_element->type = e_dw_sconstant;
1750 new_element->location.sconstant.bytes = tok2[11] - '0';
1751 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, xbt_dynar_length(tokens2) - 1, char*)));
1752 xbt_dynar_push(loc->location.compose, &new_element);
1754 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1755 new_element->type = e_dw_unsupported;
1756 xbt_dynar_push(loc->location.compose, &new_element);
1760 xbt_dynar_free(&tokens2);
1764 xbt_dynar_free(&tokens1);
1773 void print_local_variables(xbt_dict_t list){
1775 dw_location_entry_t entry;
1776 dw_location_t location_entry;
1777 unsigned int cursor3 = 0, cursor4 = 0;
1778 xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1780 char *frame_name, *variable_name;
1781 dw_frame_t current_frame;
1782 dw_local_variable_t current_variable;
1784 xbt_dict_foreach(list, cursor, frame_name, current_frame){
1785 fprintf(stderr, "Frame name : %s\n", current_frame->name);
1786 fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1787 xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1788 fprintf(stderr, "Name : %s\n", current_variable->name);
1789 if(current_variable->location == NULL)
1791 fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1792 switch(current_variable->location->type){
1794 xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1795 fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1796 switch(entry->location->type){
1797 case e_dw_register :
1798 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1800 case e_dw_bregister_op:
1801 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1804 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1806 case e_dw_fbregister_op:
1807 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1810 fprintf(stderr, " Location :\n");
1811 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1812 switch(location_entry->type){
1813 case e_dw_register :
1814 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1816 case e_dw_bregister_op:
1817 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);
1820 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1822 case e_dw_fbregister_op:
1823 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1826 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);
1828 case e_dw_arithmetic :
1829 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1832 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1834 case e_dw_uconstant :
1835 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1837 case e_dw_sconstant :
1838 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1841 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1847 fprintf(stderr, "Location type not supported\n");
1854 fprintf(stderr, "Location :\n");
1855 xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1856 switch(location_entry->type){
1857 case e_dw_register :
1858 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1860 case e_dw_bregister_op:
1861 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);
1864 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1866 case e_dw_fbregister_op:
1867 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1870 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);
1872 case e_dw_arithmetic :
1873 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1876 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1878 case e_dw_uconstant :
1879 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1881 case e_dw_sconstant :
1882 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1885 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1891 fprintf(stderr, "Location type not supported\n");
1899 static void MC_get_global_variables(char *elf_file){
1903 char *command = bprintf("objdump -t -j .data -j .bss %s", elf_file);
1905 fp = popen(command, "r");
1908 perror("popen failed");
1912 if(mc_global_variables == NULL)
1913 mc_global_variables = xbt_dynar_new(sizeof(global_variable_t), global_variable_free_voidp);
1919 xbt_dynar_t line_tokens = NULL;
1920 unsigned long offset;
1922 int type = strcmp(elf_file, xbt_binary_name); /* 0 = binary, other = libsimgrid */
1924 while ((read = xbt_getline(&line, &n, fp)) != -1){
1929 /* Wipeout the new line character */
1930 line[read - 1] = '\0';
1932 xbt_str_strip_spaces(line);
1933 xbt_str_ltrim(line, NULL);
1935 line_tokens = xbt_str_split(line, NULL);
1937 if(xbt_dynar_length(line_tokens) <= 4 || strcmp(xbt_dynar_get_as(line_tokens, 0, char *), "SYMBOL") == 0)
1940 if((strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "__gcov", 6) == 0)
1941 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "gcov", 4) == 0)
1942 || (strcmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), ".data") == 0)
1943 || (strcmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), ".bss") == 0)
1944 || (strncmp(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*), "stderr", 6) == 0)
1945 || ((size_t)strtoul(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 2, char*), NULL, 16) == 0))
1948 global_variable_t var = xbt_new0(s_global_variable_t, 1);
1951 var->address = (void *)strtoul(xbt_dynar_get_as(line_tokens, 0, char*), NULL, 16);
1953 offset = strtoul(xbt_dynar_get_as(line_tokens, 0, char*), NULL, 16);
1954 var->address = (char *)start_text_libsimgrid+offset;
1957 var->size = (size_t)strtoul(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 2, char*), NULL, 16);
1958 var->name = strdup(xbt_dynar_get_as(line_tokens, xbt_dynar_length(line_tokens) - 1, char*));
1960 if(data_bss_ignore_size(var->address) > 0){
1961 global_variable_free(var);
1963 if(xbt_dynar_is_empty(mc_global_variables)){
1964 xbt_dynar_push(mc_global_variables, &var);
1966 unsigned int cursor = 0;
1968 int end = xbt_dynar_length(mc_global_variables) - 1;
1969 global_variable_t current_var = NULL;
1971 while(start <= end){
1972 cursor = (start + end) / 2;
1973 current_var = (global_variable_t)xbt_dynar_get_as(mc_global_variables, cursor, global_variable_t);
1974 if(current_var->address == var->address)
1976 if(current_var->address < var->address)
1978 if(current_var->address > var->address)
1982 if(current_var->address < var->address)
1983 xbt_dynar_insert_at(mc_global_variables, cursor + 1, &var);
1985 xbt_dynar_insert_at(mc_global_variables, cursor, &var);
1989 xbt_dynar_free(&line_tokens);
1999 void global_variable_free(global_variable_t v){
2004 void global_variable_free_voidp(void *v){
2005 global_variable_free((global_variable_t) * (void **) v);