1 /* Copyright (c) 2008-2012 Da SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
11 #include "../surf/surf_private.h"
12 #include "../simix/smx_private.h"
13 #include "../xbt/mmalloc/mmprivate.h"
15 #include "mc_private.h"
16 #include "xbt/automaton.h"
19 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
21 "Logging specific to MC (global)");
23 /* Configuration support */
24 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
27 extern int _surf_init_status;
28 void _mc_cfg_cb_reduce(const char *name, int pos) {
29 if (_surf_init_status && !_surf_do_model_check) {
30 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.");
32 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
33 if (!strcasecmp(val,"none")) {
34 mc_reduce_kind = e_mc_reduce_none;
35 } else if (!strcasecmp(val,"dpor")) {
36 mc_reduce_kind = e_mc_reduce_dpor;
38 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
40 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
43 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
44 if (_surf_init_status && !_surf_do_model_check) {
45 xbt_die("You are specifying a checkpointing value after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
47 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
48 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
50 void _mc_cfg_cb_property(const char *name, int pos) {
51 if (_surf_init_status && !_surf_do_model_check) {
52 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.");
54 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
55 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
59 /* MC global data structures */
61 mc_state_t mc_current_state = NULL;
62 char mc_replay_mode = FALSE;
63 double *mc_time = NULL;
64 mc_snapshot_t initial_snapshot = NULL;
69 xbt_fifo_t mc_stack_safety = NULL;
70 mc_stats_t mc_stats = NULL;
74 mc_stats_pair_t mc_stats_pair = NULL;
75 xbt_fifo_t mc_stack_liveness = NULL;
76 mc_global_t initial_state_liveness = NULL;
80 xbt_dict_t mc_local_variables = NULL;
82 /* Ignore mechanism */
83 xbt_dynar_t mc_stack_comparison_ignore;
84 extern xbt_dynar_t mc_heap_comparison_ignore;
85 extern xbt_dynar_t stacks_areas;
87 xbt_automaton_t _mc_property_automaton = NULL;
89 /* Static functions */
91 static void MC_assert_pair(int prop);
92 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
93 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
95 void MC_do_the_modelcheck_for_real() {
96 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
97 if (mc_reduce_kind==e_mc_reduce_unset)
98 mc_reduce_kind=e_mc_reduce_dpor;
100 XBT_INFO("Check a safety property");
105 if (mc_reduce_kind==e_mc_reduce_unset)
106 mc_reduce_kind=e_mc_reduce_none;
108 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
109 MC_automaton_load(_surf_mc_property_file);
110 MC_modelcheck_liveness();
115 * \brief Initialize the model-checker data structures
117 void MC_init_safety(void)
120 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
122 /* Check if MC is already initialized */
123 if (initial_snapshot)
126 mc_time = xbt_new0(double, simix_process_maxpid);
128 /* Initialize the data structures that must be persistent across every
129 iteration of the model-checker (in RAW memory) */
133 /* Initialize statistics */
134 mc_stats = xbt_new0(s_mc_stats_t, 1);
135 mc_stats->state_size = 1;
137 /* Create exploration stack */
138 mc_stack_safety = xbt_fifo_new();
145 /* Save the initial state */
146 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
147 MC_take_snapshot(initial_snapshot);
155 void MC_compare(void){
160 void MC_modelcheck(void)
167 void MC_init_liveness(){
169 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
171 mc_time = xbt_new0(double, simix_process_maxpid);
173 /* mc_time refers to clock for each process -> ignore it for heap comparison */
175 for(i = 0; i<simix_process_maxpid; i++)
176 MC_ignore_heap(&(mc_time[i]), sizeof(double));
180 /* Initialize the data structures that must be persistent across every
181 iteration of the model-checker (in RAW memory) */
185 char *ls_path = get_libsimgrid_path();
187 mc_local_variables = xbt_dict_new_homogeneous(NULL);
189 /* Get local variables in binary for state equality detection */
190 xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
191 MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
193 /* Get local variables in libsimgrid for state equality detection */
194 xbt_dict_t libsimgrid_location_list = MC_get_location_list(ls_path);
195 MC_get_local_variables(ls_path, libsimgrid_location_list, &mc_local_variables);
199 MC_init_memory_map_info();
201 /* Get .plt section (start and end addresses) for data libsimgrid and data program comparison */
202 get_libsimgrid_plt_section();
203 get_binary_plt_section();
210 void MC_modelcheck_liveness(){
212 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
218 /* Initialize statistics */
219 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
221 XBT_DEBUG("Creating stack");
223 /* Create exploration stack */
224 mc_stack_liveness = xbt_fifo_new();
231 MC_print_statistics_pairs(mc_stats_pair);
239 MC_print_statistics(mc_stats);
245 int MC_random(int min, int max)
247 /*FIXME: return mc_current_state->executed_transition->random.value;*/
252 * \brief Schedules all the process that are ready to run
254 void MC_wait_for_requests(void)
256 smx_process_t process;
260 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
261 SIMIX_process_runall();
262 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
263 req = &process->simcall;
264 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
265 SIMIX_simcall_pre(req, 0);
270 int MC_deadlock_check()
272 int deadlock = FALSE;
273 smx_process_t process;
274 if(xbt_swag_size(simix_global->process_list)){
276 xbt_swag_foreach(process, simix_global->process_list){
277 if(process->simcall.call != SIMCALL_NONE
278 && MC_request_is_enabled(&process->simcall)){
288 * \brief Re-executes from the state at position start all the transitions indicated by
289 * a given model-checker stack.
290 * \param stack The stack with the transitions to execute.
291 * \param start Start index to begin the re-execution.
293 void MC_replay(xbt_fifo_t stack, int start)
295 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
299 smx_simcall_t req = NULL, saved_req = NULL;
300 xbt_fifo_item_t item, start_item;
303 XBT_DEBUG("**** Begin Replay ****");
306 /* Restore the initial state */
307 MC_restore_snapshot(initial_snapshot);
308 /* At the moment of taking the snapshot the raw heap was set, so restoring
309 * it will set it back again, we have to unset it to continue */
313 start_item = xbt_fifo_get_last_item(stack);
316 start_item = xbt_fifo_get_prev_item(start_item);
321 /* Traverse the stack from the state at position start and re-execute the transitions */
322 for (item = start_item;
323 item != xbt_fifo_get_first_item(stack);
324 item = xbt_fifo_get_prev_item(item)) {
326 state = (mc_state_t) xbt_fifo_get_item_content(item);
327 saved_req = MC_state_get_executed_request(state, &value);
330 /* because we got a copy of the executed request, we have to fetch the
331 real one, pointed by the request field of the issuer process */
332 req = &saved_req->issuer->simcall;
334 /* Debug information */
335 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
336 req_str = MC_request_to_string(req, value);
337 XBT_DEBUG("Replay: %s (%p)", req_str, state);
342 SIMIX_simcall_pre(req, value);
343 MC_wait_for_requests();
345 /* Update statistics */
346 mc_stats->visited_states++;
347 mc_stats->executed_transitions++;
349 XBT_DEBUG("**** End Replay ****");
359 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
362 int raw_mem = (mmalloc_get_current_heap() == raw_heap);
366 smx_simcall_t req = NULL, saved_req = NULL;
367 xbt_fifo_item_t item;
369 mc_pair_stateless_t pair;
372 XBT_DEBUG("**** Begin Replay ****");
374 /* Restore the initial state */
375 MC_restore_snapshot(initial_state_liveness->initial_snapshot);
376 /* At the moment of taking the snapshot the raw heap was set, so restoring
377 * it will set it back again, we have to unset it to continue */
383 item = xbt_fifo_get_last_item(stack);
385 while(depth <= xbt_fifo_size(stack)){
387 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
388 state = (mc_state_t) pair->graph_state;
390 if(pair->requests > 0){
392 saved_req = MC_state_get_executed_request(state, &value);
393 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
395 if(saved_req != NULL){
396 /* because we got a copy of the executed request, we have to fetch the
397 real one, pointed by the request field of the issuer process */
398 req = &saved_req->issuer->simcall;
399 //XBT_DEBUG("Req->call %u", req->call);
401 /* Debug information */
402 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
403 req_str = MC_request_to_string(req, value);
404 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
410 SIMIX_simcall_pre(req, value);
411 MC_wait_for_requests();
416 /* Update statistics */
417 mc_stats_pair->visited_pairs++;
419 item = xbt_fifo_get_prev_item(item);
424 /* Traverse the stack from the initial state and re-execute the transitions */
425 for (item = xbt_fifo_get_last_item(stack);
426 item != xbt_fifo_get_first_item(stack);
427 item = xbt_fifo_get_prev_item(item)) {
429 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
430 state = (mc_state_t) pair->graph_state;
432 if(pair->requests > 0){
434 saved_req = MC_state_get_executed_request(state, &value);
435 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
437 if(saved_req != NULL){
438 /* because we got a copy of the executed request, we have to fetch the
439 real one, pointed by the request field of the issuer process */
440 req = &saved_req->issuer->simcall;
441 //XBT_DEBUG("Req->call %u", req->call);
443 /* Debug information */
444 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
445 req_str = MC_request_to_string(req, value);
446 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
452 SIMIX_simcall_pre(req, value);
453 MC_wait_for_requests();
458 /* Update statistics */
459 mc_stats_pair->visited_pairs++;
463 XBT_DEBUG("**** End Replay ****");
473 * \brief Dumps the contents of a model-checker's stack and shows the actual
475 * \param stack The stack to dump
477 void MC_dump_stack_safety(xbt_fifo_t stack)
480 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
482 MC_show_stack_safety(stack);
484 if(!_surf_mc_checkpoint){
489 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
490 MC_state_delete(state);
503 void MC_show_stack_safety(xbt_fifo_t stack)
507 xbt_fifo_item_t item;
509 char *req_str = NULL;
511 for (item = xbt_fifo_get_last_item(stack);
512 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
513 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
514 req = MC_state_get_executed_request(state, &value);
516 req_str = MC_request_to_string(req, value);
517 XBT_INFO("%s", req_str);
523 void MC_show_deadlock(smx_simcall_t req)
525 /*char *req_str = NULL;*/
526 XBT_INFO("**************************");
527 XBT_INFO("*** DEAD-LOCK DETECTED ***");
528 XBT_INFO("**************************");
529 XBT_INFO("Locked request:");
530 /*req_str = MC_request_to_string(req);
531 XBT_INFO("%s", req_str);
533 XBT_INFO("Counter-example execution trace:");
534 MC_dump_stack_safety(mc_stack_safety);
538 void MC_show_stack_liveness(xbt_fifo_t stack){
540 mc_pair_stateless_t pair;
541 xbt_fifo_item_t item;
543 char *req_str = NULL;
545 for (item = xbt_fifo_get_last_item(stack);
546 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
547 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
548 req = MC_state_get_executed_request(pair->graph_state, &value);
550 if(pair->requests>0){
551 req_str = MC_request_to_string(req, value);
552 XBT_INFO("%s", req_str);
555 XBT_INFO("End of system requests but evolution in Büchi automaton");
561 void MC_dump_stack_liveness(xbt_fifo_t stack){
563 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
565 mc_pair_stateless_t pair;
568 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
569 pair_stateless_free(pair);
578 void MC_print_statistics(mc_stats_t stats)
580 //XBT_INFO("State space size ~= %lu", stats->state_size);
581 XBT_INFO("Expanded states = %lu", stats->expanded_states);
582 XBT_INFO("Visited states = %lu", stats->visited_states);
583 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
584 XBT_INFO("Expanded / Visited = %lf",
585 (double) stats->visited_states / stats->expanded_states);
586 /*XBT_INFO("Exploration coverage = %lf",
587 (double)stats->expanded_states / stats->state_size); */
590 void MC_print_statistics_pairs(mc_stats_pair_t stats)
592 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
593 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
594 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
595 XBT_INFO("Expanded / Visited = %lf",
596 (double) stats->visited_pairs / stats->expanded_pairs);
597 /*XBT_INFO("Exploration coverage = %lf",
598 (double)stats->expanded_states / stats->state_size); */
601 void MC_assert(int prop)
603 if (MC_is_active() && !prop){
604 XBT_INFO("**************************");
605 XBT_INFO("*** PROPERTY NOT VALID ***");
606 XBT_INFO("**************************");
607 XBT_INFO("Counter-example execution trace:");
608 MC_dump_stack_safety(mc_stack_safety);
609 MC_print_statistics(mc_stats);
614 static void MC_assert_pair(int prop){
615 if (MC_is_active() && !prop) {
616 XBT_INFO("**************************");
617 XBT_INFO("*** PROPERTY NOT VALID ***");
618 XBT_INFO("**************************");
619 //XBT_INFO("Counter-example execution trace:");
620 MC_show_stack_liveness(mc_stack_liveness);
621 //MC_dump_snapshot_stack(mc_snapshot_stack);
622 MC_print_statistics_pairs(mc_stats_pair);
627 void MC_process_clock_add(smx_process_t process, double amount)
629 mc_time[process->pid] += amount;
632 double MC_process_clock_get(smx_process_t process)
636 return mc_time[process->pid];
644 void MC_automaton_load(const char *file){
646 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
650 if (_mc_property_automaton == NULL)
651 _mc_property_automaton = xbt_automaton_new();
653 xbt_automaton_load(_mc_property_automaton,file);
662 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
664 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
668 if (_mc_property_automaton == NULL)
669 _mc_property_automaton = xbt_automaton_new();
671 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
680 /************ MC_ignore ***********/
682 void MC_ignore_heap(void *address, size_t size){
684 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
688 if(mc_heap_comparison_ignore == NULL)
689 mc_heap_comparison_ignore = xbt_dynar_new(sizeof(mc_heap_ignore_region_t), NULL);
691 mc_heap_ignore_region_t region = NULL;
692 region = xbt_new0(s_mc_heap_ignore_region_t, 1);
693 region->address = address;
696 if((address >= std_heap) && (address <= (void*)((char *)std_heap + STD_HEAP_SIZE))){
698 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
700 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
701 region->fragment = -1;
703 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
708 unsigned int cursor = 0;
709 mc_heap_ignore_region_t current_region;
710 xbt_dynar_foreach(mc_heap_comparison_ignore, cursor, current_region){
711 if(current_region->address > address)
715 xbt_dynar_insert_at(mc_heap_comparison_ignore, cursor, ®ion);
723 void MC_ignore_stack(const char *var_name, const char *frame){
725 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
729 if(mc_stack_comparison_ignore == NULL)
730 mc_stack_comparison_ignore = xbt_dynar_new(sizeof(mc_stack_ignore_variable_t), NULL);
732 if(xbt_dynar_is_empty(mc_stack_comparison_ignore)){
734 mc_stack_ignore_variable_t var = NULL;
735 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
736 var->var_name = strdup(var_name);
737 var->frame = strdup(frame);
739 xbt_dynar_insert_at(mc_stack_comparison_ignore, 0, &var);
743 unsigned int cursor = 0;
745 int end = xbt_dynar_length(mc_stack_comparison_ignore) - 1;
746 mc_stack_ignore_variable_t current_var = NULL;
749 cursor = (start + end) / 2;
750 current_var = (mc_stack_ignore_variable_t)xbt_dynar_get_as(mc_stack_comparison_ignore, cursor, mc_stack_ignore_variable_t);
751 if(strcmp(current_var->frame, frame) == 0){
752 if(strcmp(current_var->var_name, var_name) == 0){
758 if(strcmp(current_var->var_name, var_name) < 0)
760 if(strcmp(current_var->var_name, var_name) > 0)
763 if(strcmp(current_var->frame, frame) < 0)
765 if(strcmp(current_var->frame, frame) > 0)
769 mc_stack_ignore_variable_t var = NULL;
770 var = xbt_new0(s_mc_stack_ignore_variable_t, 1);
771 var->var_name = strdup(var_name);
772 var->frame = strdup(frame);
774 if(strcmp(current_var->frame, frame) < 0)
775 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor + 1, &var);
777 xbt_dynar_insert_at(mc_stack_comparison_ignore, cursor, &var);
788 void MC_new_stack_area(void *stack, char *name, void* context){
790 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
793 if(stacks_areas == NULL)
794 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
796 stack_region_t region = NULL;
797 region = xbt_new0(s_stack_region_t, 1);
798 region->address = stack;
799 region->process_name = strdup(name);
800 region->context = context;
801 xbt_dynar_push(stacks_areas, ®ion);
809 /************ DWARF ***********/
811 xbt_dict_t MC_get_location_list(const char *elf_file){
813 char *command = bprintf("objdump -Wo %s", elf_file);
815 FILE *fp = popen(command, "r");
818 perror("popen for objdump failed");
820 int debug = 0; /*Detect if the program has been compiled with -g */
822 xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
823 char *line = NULL, *loc_expr = NULL;
827 xbt_dynar_t split = NULL;
829 while ((read = getline(&line, &n, fp)) != -1) {
831 /* Wipeout the new line character */
832 line[read - 1] = '\0';
834 xbt_str_trim(line, NULL);
839 if(strlen(line) == 0)
844 if(strncmp(line, elf_file, strlen(elf_file)) == 0)
847 if(strncmp(line, "Contents", 8) == 0)
850 if(strncmp(line, "Offset", 6) == 0){
857 XBT_INFO("Your program must be compiled with -g");
861 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
863 xbt_str_strip_spaces(line);
864 split = xbt_str_split(line, " ");
866 while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
868 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
869 new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
870 new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
873 while(cursor_remove < 3){
874 xbt_dynar_remove_at(split, 0, NULL);
878 loc_expr = xbt_str_join(split, " ");
879 xbt_str_ltrim(loc_expr, "(");
880 xbt_str_rtrim(loc_expr, ")");
881 new_entry->location = get_location(NULL, loc_expr);
883 xbt_dynar_push(loclist, &new_entry);
885 xbt_dynar_free(&split);
888 read = getline(&line, &n, fp);
890 line[read - 1] = '\0';
891 xbt_str_strip_spaces(line);
892 split = xbt_str_split(line, " ");
898 char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
899 xbt_dict_set(location_list, key, loclist, NULL);
901 xbt_dynar_free(&split);
909 return location_list;
912 char *get_libsimgrid_path(){
914 char *command = bprintf("ldd %s", xbt_binary_name);
916 FILE *fp = popen(command, "r");
918 perror("popen for ldd failed");
925 while((read = getline(&line, &n, fp)) != -1){
930 /* Wipeout the new line character */
931 line[read - 1] = '\0';
933 xbt_str_strip_spaces(line);
934 xbt_str_ltrim(line, NULL);
935 split = xbt_str_split(line, " ");
937 if(strncmp((char *)xbt_dynar_get_as(split, 0, char *), "libsimgrid.so", 13) == 0){
941 return ((char *)xbt_dynar_get_as(split, 2, char *));
944 xbt_dynar_free(&split);
956 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
958 xbt_dict_cursor_t cursor = NULL;
962 xbt_dict_foreach(all_variables, cursor, name, res) {
963 if(offset >= res->start && offset < res->end)
971 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
973 char *command = bprintf("objdump -Wi %s", elf_file);
975 FILE *fp = popen(command, "r");
978 perror("popen for objdump failed");
980 char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
983 int valid_variable = 1;
984 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
985 xbt_dynar_t split = NULL, split2 = NULL;
987 xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
988 xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
989 char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
990 int new_frame = 0, new_variable = 0;
991 dw_frame_t variable_frame, subroutine_frame = NULL;
993 read = getline(&line, &n, fp);
998 read = getline(&line, &n, fp);
1002 /* Wipeout the new line character */
1003 line[read - 1] = '\0';
1005 if(strlen(line) == 0){
1006 read = getline(&line, &n, fp);
1010 xbt_str_ltrim(line, NULL);
1011 xbt_str_strip_spaces(line);
1014 read = getline(&line, &n, fp);
1018 xbt_dynar_free(&split);
1019 split = xbt_str_split(line, " ");
1022 node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1024 if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
1026 dw_frame_t frame = NULL;
1028 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1029 subprogram_start = strdup(strtok(NULL, "<"));
1030 xbt_str_rtrim(subprogram_start, ">:");
1032 read = getline(&line, &n, fp);
1037 read = getline(&line, &n, fp);
1041 /* Wipeout the new line character */
1042 line[read - 1] = '\0';
1044 if(strlen(line) == 0){
1045 read = getline(&line, &n, fp);
1049 xbt_dynar_free(&split);
1050 xbt_str_rtrim(line, NULL);
1051 xbt_str_strip_spaces(line);
1052 split = xbt_str_split(line, " ");
1054 node_type = xbt_dynar_get_as(split, 1, char *);
1056 if(strncmp(node_type, "DW_AT_", 6) != 0)
1059 if(strcmp(node_type, "DW_AT_sibling") == 0){
1061 subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
1062 xbt_str_ltrim(subprogram_end, "<0x");
1063 xbt_str_rtrim(subprogram_end, ">");
1065 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
1068 abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
1069 xbt_str_ltrim(abstract_origin, "<0x");
1070 xbt_str_rtrim(abstract_origin, ">");
1071 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
1072 frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1074 }else if(strcmp(node_type, "DW_AT_name") == 0){
1077 free(current_frame);
1078 frame = xbt_new0(s_dw_frame_t, 1);
1079 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1080 frame->variables = xbt_dict_new_homogeneous(NULL);
1081 frame->frame_base = xbt_new0(s_dw_location_t, 1);
1082 current_frame = strdup(frame->name);
1084 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1086 }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
1088 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1090 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1092 frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1096 xbt_str_strip_spaces(line);
1097 split2 = xbt_str_split(line, "(");
1098 xbt_dynar_remove_at(split2, 0, NULL);
1099 loc_expr = xbt_str_join(split2, " ");
1100 xbt_str_rtrim(loc_expr, ")");
1101 frame->frame_base = get_location(NULL, loc_expr);
1102 xbt_dynar_free(&split2);
1106 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1109 frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1111 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1114 frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1116 }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1119 free(current_frame);
1120 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1121 current_frame = strdup(frame->name);
1122 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1126 read = getline(&line, &n, fp);
1131 frame->start = strtoul(subprogram_start, NULL, 16);
1132 if(subprogram_end != NULL)
1133 frame->end = strtoul(subprogram_end, NULL, 16);
1134 xbt_dict_set(*all_variables, frame->name, frame, NULL);
1137 free(subprogram_start);
1138 if(subprogram_end != NULL){
1139 free(subprogram_end);
1140 subprogram_end = NULL;
1144 }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1146 dw_local_variable_t var = NULL;
1148 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1149 origin = strdup(strtok(NULL, "<"));
1150 xbt_str_rtrim(origin, ">:");
1152 read = getline(&line, &n, fp);
1157 read = getline(&line, &n, fp);
1161 /* Wipeout the new line character */
1162 line[read - 1] = '\0';
1164 if(strlen(line) == 0){
1165 read = getline(&line, &n, fp);
1169 xbt_dynar_free(&split);
1170 xbt_str_rtrim(line, NULL);
1171 xbt_str_strip_spaces(line);
1172 split = xbt_str_split(line, " ");
1174 node_type = xbt_dynar_get_as(split, 1, char *);
1176 if(strncmp(node_type, "DW_AT_", 6) != 0)
1179 if(strcmp(node_type, "DW_AT_name") == 0){
1182 var = xbt_new0(s_dw_local_variable_t, 1);
1183 var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1185 xbt_dict_set(variables_origin, origin, var->name, NULL);
1187 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1190 abstract_origin = xbt_dynar_get_as(split, 2, char *);
1191 xbt_str_ltrim(abstract_origin, "<0x");
1192 xbt_str_rtrim(abstract_origin, ">");
1194 variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1195 variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1196 var = xbt_dict_get_or_null(variable_frame->variables, variable_name);
1198 }else if(strcmp(node_type, "DW_AT_location") == 0){
1200 if(valid_variable == 1 && var != NULL){
1202 var->location = xbt_new0(s_dw_location_t, 1);
1204 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1206 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1208 var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1212 xbt_str_strip_spaces(line);
1213 split2 = xbt_str_split(line, "(");
1214 xbt_dynar_remove_at(split2, 0, NULL);
1215 loc_expr = xbt_str_join(split2, " ");
1216 xbt_str_rtrim(loc_expr, ")");
1217 var->location = get_location(NULL, loc_expr);
1218 xbt_dynar_free(&split2);
1224 }else if(strcmp(node_type, "DW_AT_external") == 0){
1230 read = getline(&line, &n, fp);
1234 if(new_variable == 1 && valid_variable == 1){
1236 variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1237 xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1243 }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1245 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1246 origin = strdup(strtok(NULL, "<"));
1247 xbt_str_rtrim(origin, ">:");
1249 read = getline(&line, &n, fp);
1253 /* Wipeout the new line character */
1254 line[read - 1] = '\0';
1257 read = getline(&line, &n, fp);
1261 if(strlen(line) == 0){
1262 read = getline(&line, &n, fp);
1266 xbt_dynar_free(&split);
1267 xbt_str_rtrim(line, NULL);
1268 xbt_str_strip_spaces(line);
1269 split = xbt_str_split(line, " ");
1271 if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1274 node_type = xbt_dynar_get_as(split, 1, char *);
1276 if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1278 origin = xbt_dynar_get_as(split, 2, char *);
1279 xbt_str_ltrim(origin, "<0x");
1280 xbt_str_rtrim(origin, ">");
1282 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1283 subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1285 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1287 subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1289 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1291 subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1294 read = getline(&line, &n, fp);
1300 read = getline(&line, &n, fp);
1306 xbt_dynar_free(&split);
1313 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1315 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1317 if(location_list != NULL){
1319 char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1320 loc->type = e_dw_loclist;
1321 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1323 XBT_INFO("Key not found in loclist");
1329 char *tok = NULL, *tok2 = NULL;
1331 xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1332 xbt_dynar_t tokens2;
1334 loc->type = e_dw_compose;
1335 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1337 while(cursor < xbt_dynar_length(tokens1)){
1339 tok = xbt_dynar_get_as(tokens1, cursor, char*);
1340 tokens2 = xbt_str_split(tok, " ");
1341 tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1343 if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1344 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1345 new_element->type = e_dw_register;
1346 new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1347 xbt_dynar_push(loc->location.compose, &new_element);
1348 }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1349 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1350 new_element->type = e_dw_fbregister_op;
1351 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1352 xbt_dynar_push(loc->location.compose, &new_element);
1353 }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1354 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1355 new_element->type = e_dw_bregister_op;
1356 new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1357 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1358 xbt_dynar_push(loc->location.compose, &new_element);
1359 }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1360 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1361 new_element->type = e_dw_lit;
1362 new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1363 xbt_dynar_push(loc->location.compose, &new_element);
1364 }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1365 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1366 new_element->type = e_dw_piece;
1367 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1368 /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1369 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1371 new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1372 xbt_dynar_push(loc->location.compose, &new_element);
1373 }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1374 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1375 new_element->type = e_dw_plus_uconst;
1376 new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, 1, char *));
1377 xbt_dynar_push(loc->location.compose, &new_element);
1378 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1379 strcmp(tok, "DW_OP_and") == 0 ||
1380 strcmp(tok, "DW_OP_div") == 0 ||
1381 strcmp(tok, "DW_OP_minus") == 0 ||
1382 strcmp(tok, "DW_OP_mod") == 0 ||
1383 strcmp(tok, "DW_OP_mul") == 0 ||
1384 strcmp(tok, "DW_OP_neg") == 0 ||
1385 strcmp(tok, "DW_OP_not") == 0 ||
1386 strcmp(tok, "DW_OP_or") == 0 ||
1387 strcmp(tok, "DW_OP_plus") == 0){
1388 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1389 new_element->type = e_dw_arithmetic;
1390 new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1391 xbt_dynar_push(loc->location.compose, &new_element);
1392 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1393 }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1394 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1395 new_element->type = e_dw_deref;
1396 new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, 1, char*));
1397 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1398 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1400 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1401 xbt_dynar_push(loc->location.compose, &new_element);
1402 }else if(strcmp(tok, "DW_OP_deref") == 0){
1403 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1404 new_element->type = e_dw_deref;
1405 new_element->location.deref_size = sizeof(void *);
1406 xbt_dynar_push(loc->location.compose, &new_element);
1407 }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1408 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1409 new_element->type = e_dw_uconstant;
1410 new_element->location.uconstant.bytes = 1;
1411 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1412 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1413 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1415 new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1416 xbt_dynar_push(loc->location.compose, &new_element);
1417 }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1418 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1419 new_element->type = e_dw_sconstant;
1420 new_element->location.sconstant.bytes = 1;
1421 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1422 xbt_dynar_push(loc->location.compose, &new_element);
1423 }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1424 strcmp(tok2, "DW_OP_const2u:") == 0 ||
1425 strcmp(tok2, "DW_OP_const4u:") == 0 ||
1426 strcmp(tok2, "DW_OP_const8u:") == 0){
1427 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1428 new_element->type = e_dw_uconstant;
1429 new_element->location.uconstant.bytes = tok2[11] - '0';
1430 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1431 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1432 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1434 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1435 xbt_dynar_push(loc->location.compose, &new_element);
1436 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1437 strcmp(tok, "DW_OP_const2s") == 0 ||
1438 strcmp(tok, "DW_OP_const4s") == 0 ||
1439 strcmp(tok, "DW_OP_const8s") == 0){
1440 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1441 new_element->type = e_dw_sconstant;
1442 new_element->location.sconstant.bytes = tok2[11] - '0';
1443 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1444 xbt_dynar_push(loc->location.compose, &new_element);
1446 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1447 new_element->type = e_dw_unsupported;
1448 xbt_dynar_push(loc->location.compose, &new_element);
1452 xbt_dynar_free(&tokens2);
1456 xbt_dynar_free(&tokens1);
1465 void print_local_variables(xbt_dict_t list){
1467 dw_location_entry_t entry;
1468 dw_location_t location_entry;
1469 unsigned int cursor3 = 0, cursor4 = 0;
1470 xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1472 char *frame_name, *variable_name;
1473 dw_frame_t current_frame;
1474 dw_local_variable_t current_variable;
1476 xbt_dict_foreach(list, cursor, frame_name, current_frame){
1477 fprintf(stderr, "Frame name : %s\n", current_frame->name);
1478 fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1479 xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1480 fprintf(stderr, "Name : %s\n", current_variable->name);
1481 if(current_variable->location == NULL)
1483 fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1484 switch(current_variable->location->type){
1486 xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1487 fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1488 switch(entry->location->type){
1489 case e_dw_register :
1490 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1492 case e_dw_bregister_op:
1493 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1496 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1498 case e_dw_fbregister_op:
1499 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1502 fprintf(stderr, " Location :\n");
1503 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1504 switch(location_entry->type){
1505 case e_dw_register :
1506 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1508 case e_dw_bregister_op:
1509 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);
1512 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1514 case e_dw_fbregister_op:
1515 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1518 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);
1520 case e_dw_arithmetic :
1521 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1524 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1526 case e_dw_uconstant :
1527 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1529 case e_dw_sconstant :
1530 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1533 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1539 fprintf(stderr, "Location type not supported\n");
1546 fprintf(stderr, "Location :\n");
1547 xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1548 switch(location_entry->type){
1549 case e_dw_register :
1550 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1552 case e_dw_bregister_op:
1553 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);
1556 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1558 case e_dw_fbregister_op:
1559 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1562 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);
1564 case e_dw_arithmetic :
1565 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1568 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1570 case e_dw_uconstant :
1571 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1573 case e_dw_sconstant :
1574 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1577 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1583 fprintf(stderr, "Location type not supported\n");