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"
18 XBT_LOG_NEW_CATEGORY(mc, "All MC categories");
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_global, mc,
20 "Logging specific to MC (global)");
22 /* Configuration support */
23 e_mc_reduce_t mc_reduce_kind=e_mc_reduce_unset;
25 extern int _surf_init_status;
26 void _mc_cfg_cb_reduce(const char *name, int pos) {
27 if (_surf_init_status && !_surf_do_model_check) {
28 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.");
30 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
31 if (!strcasecmp(val,"none")) {
32 mc_reduce_kind = e_mc_reduce_none;
33 } else if (!strcasecmp(val,"dpor")) {
34 mc_reduce_kind = e_mc_reduce_dpor;
36 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
38 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
41 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
42 if (_surf_init_status && !_surf_do_model_check) {
43 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.");
45 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
46 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
48 void _mc_cfg_cb_property(const char *name, int pos) {
49 if (_surf_init_status && !_surf_do_model_check) {
50 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.");
52 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
53 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
57 /* MC global data structures */
59 mc_state_t mc_current_state = NULL;
60 char mc_replay_mode = FALSE;
61 double *mc_time = NULL;
62 mc_snapshot_t initial_snapshot = NULL;
67 xbt_fifo_t mc_stack_safety = NULL;
68 mc_stats_t mc_stats = NULL;
72 mc_stats_pair_t mc_stats_pair = NULL;
73 xbt_fifo_t mc_stack_liveness = NULL;
74 mc_snapshot_t initial_snapshot_liveness = NULL;
76 xbt_dynar_t mc_binary_local_variables = NULL;
78 extern xbt_dynar_t mmalloc_ignore;
79 extern xbt_dynar_t stacks_areas;
81 xbt_automaton_t _mc_property_automaton = NULL;
83 static void MC_assert_pair(int prop);
85 void MC_do_the_modelcheck_for_real() {
86 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
87 if (mc_reduce_kind==e_mc_reduce_unset)
88 mc_reduce_kind=e_mc_reduce_dpor;
90 XBT_INFO("Check a safety property");
95 if (mc_reduce_kind==e_mc_reduce_unset)
96 mc_reduce_kind=e_mc_reduce_none;
98 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
99 MC_automaton_load(_surf_mc_property_file);
100 MC_modelcheck_liveness();
105 * \brief Initialize the model-checker data structures
107 void MC_init_safety(void)
110 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
112 /* Check if MC is already initialized */
113 if (initial_snapshot)
116 mc_time = xbt_new0(double, simix_process_maxpid);
118 /* Initialize the data structures that must be persistent across every
119 iteration of the model-checker (in RAW memory) */
123 /* Initialize statistics */
124 mc_stats = xbt_new0(s_mc_stats_t, 1);
125 mc_stats->state_size = 1;
127 /* Create exploration stack */
128 mc_stack_safety = xbt_fifo_new();
135 /* Save the initial state */
136 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
137 MC_take_snapshot(initial_snapshot);
148 void MC_compare(void){
153 void MC_modelcheck(void)
160 void MC_modelcheck_liveness(){
162 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
165 XBT_DEBUG("Start init mc");
167 mc_time = xbt_new0(double, simix_process_maxpid);
169 /* mc_time refers to clock for each process -> ignore it for heap comparison */
171 for(i = 0; i<simix_process_maxpid; i++)
172 MC_ignore(&(mc_time[i]), sizeof(double));
176 /* Initialize the data structures that must be persistent across every
177 iteration of the model-checker (in RAW memory) */
181 mc_binary_local_variables = xbt_dynar_new(sizeof(dw_frame_t), NULL);
183 /* Initialize statistics */
184 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
186 XBT_DEBUG("Creating stack");
188 /* Create exploration stack */
189 mc_stack_liveness = xbt_fifo_new();
193 /* Get local variables in binary for state equality detection */
194 MC_get_binary_local_variables();
199 MC_print_statistics_pairs(mc_stats_pair);
208 MC_print_statistics(mc_stats);
214 int MC_random(int min, int max)
216 /*FIXME: return mc_current_state->executed_transition->random.value;*/
221 * \brief Schedules all the process that are ready to run
223 void MC_wait_for_requests(void)
225 smx_process_t process;
229 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
230 SIMIX_process_runall();
231 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
232 req = &process->simcall;
233 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
234 SIMIX_simcall_pre(req, 0);
239 int MC_deadlock_check()
241 int deadlock = FALSE;
242 smx_process_t process;
243 if(xbt_swag_size(simix_global->process_list)){
245 xbt_swag_foreach(process, simix_global->process_list){
246 if(process->simcall.call != SIMCALL_NONE
247 && MC_request_is_enabled(&process->simcall)){
257 * \brief Re-executes from the state at position start all the transitions indicated by
258 * a given model-checker stack.
259 * \param stack The stack with the transitions to execute.
260 * \param start Start index to begin the re-execution.
262 void MC_replay(xbt_fifo_t stack, int start)
264 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
268 smx_simcall_t req = NULL, saved_req = NULL;
269 xbt_fifo_item_t item, start_item;
272 XBT_DEBUG("**** Begin Replay ****");
275 /* Restore the initial state */
276 MC_restore_snapshot(initial_snapshot);
277 /* At the moment of taking the snapshot the raw heap was set, so restoring
278 * it will set it back again, we have to unset it to continue */
282 start_item = xbt_fifo_get_last_item(stack);
285 start_item = xbt_fifo_get_prev_item(start_item);
290 /* Traverse the stack from the state at position start and re-execute the transitions */
291 for (item = start_item;
292 item != xbt_fifo_get_first_item(stack);
293 item = xbt_fifo_get_prev_item(item)) {
295 state = (mc_state_t) xbt_fifo_get_item_content(item);
296 saved_req = MC_state_get_executed_request(state, &value);
299 /* because we got a copy of the executed request, we have to fetch the
300 real one, pointed by the request field of the issuer process */
301 req = &saved_req->issuer->simcall;
303 /* Debug information */
304 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
305 req_str = MC_request_to_string(req, value);
306 XBT_DEBUG("Replay: %s (%p)", req_str, state);
311 SIMIX_simcall_pre(req, value);
312 MC_wait_for_requests();
314 /* Update statistics */
315 mc_stats->visited_states++;
316 mc_stats->executed_transitions++;
318 XBT_DEBUG("**** End Replay ****");
328 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
331 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
335 smx_simcall_t req = NULL, saved_req = NULL;
336 xbt_fifo_item_t item;
338 mc_pair_stateless_t pair;
341 XBT_DEBUG("**** Begin Replay ****");
343 /* Restore the initial state */
344 MC_restore_snapshot(initial_snapshot_liveness);
345 /* At the moment of taking the snapshot the raw heap was set, so restoring
346 * it will set it back again, we have to unset it to continue */
351 item = xbt_fifo_get_last_item(stack);
353 while(depth <= xbt_fifo_size(stack)){
355 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
356 state = (mc_state_t) pair->graph_state;
358 if(pair->requests > 0){
360 saved_req = MC_state_get_executed_request(state, &value);
361 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
363 if(saved_req != NULL){
364 /* because we got a copy of the executed request, we have to fetch the
365 real one, pointed by the request field of the issuer process */
366 req = &saved_req->issuer->simcall;
367 //XBT_DEBUG("Req->call %u", req->call);
369 /* Debug information */
370 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
371 req_str = MC_request_to_string(req, value);
372 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
378 SIMIX_simcall_pre(req, value);
379 MC_wait_for_requests();
384 /* Update statistics */
385 mc_stats_pair->visited_pairs++;
387 item = xbt_fifo_get_prev_item(item);
392 /* Traverse the stack from the initial state and re-execute the transitions */
393 for (item = xbt_fifo_get_last_item(stack);
394 item != xbt_fifo_get_first_item(stack);
395 item = xbt_fifo_get_prev_item(item)) {
397 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
398 state = (mc_state_t) pair->graph_state;
400 if(pair->requests > 0){
402 saved_req = MC_state_get_executed_request(state, &value);
403 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
405 if(saved_req != NULL){
406 /* because we got a copy of the executed request, we have to fetch the
407 real one, pointed by the request field of the issuer process */
408 req = &saved_req->issuer->simcall;
409 //XBT_DEBUG("Req->call %u", req->call);
411 /* Debug information */
412 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
413 req_str = MC_request_to_string(req, value);
414 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
420 SIMIX_simcall_pre(req, value);
421 MC_wait_for_requests();
426 /* Update statistics */
427 mc_stats_pair->visited_pairs++;
431 XBT_DEBUG("**** End Replay ****");
441 * \brief Dumps the contents of a model-checker's stack and shows the actual
443 * \param stack The stack to dump
445 void MC_dump_stack_safety(xbt_fifo_t stack)
448 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
450 MC_show_stack_safety(stack);
452 if(!_surf_mc_checkpoint){
457 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
458 MC_state_delete(state);
471 void MC_show_stack_safety(xbt_fifo_t stack)
475 xbt_fifo_item_t item;
477 char *req_str = NULL;
479 for (item = xbt_fifo_get_last_item(stack);
480 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
481 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
482 req = MC_state_get_executed_request(state, &value);
484 req_str = MC_request_to_string(req, value);
485 XBT_INFO("%s", req_str);
491 void MC_show_deadlock(smx_simcall_t req)
493 /*char *req_str = NULL;*/
494 XBT_INFO("**************************");
495 XBT_INFO("*** DEAD-LOCK DETECTED ***");
496 XBT_INFO("**************************");
497 XBT_INFO("Locked request:");
498 /*req_str = MC_request_to_string(req);
499 XBT_INFO("%s", req_str);
501 XBT_INFO("Counter-example execution trace:");
502 MC_dump_stack_safety(mc_stack_safety);
506 void MC_show_stack_liveness(xbt_fifo_t stack){
508 mc_pair_stateless_t pair;
509 xbt_fifo_item_t item;
511 char *req_str = NULL;
513 for (item = xbt_fifo_get_last_item(stack);
514 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
515 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
516 req = MC_state_get_executed_request(pair->graph_state, &value);
518 if(pair->requests>0){
519 req_str = MC_request_to_string(req, value);
520 XBT_INFO("%s", req_str);
523 XBT_INFO("End of system requests but evolution in Büchi automaton");
529 void MC_dump_stack_liveness(xbt_fifo_t stack){
531 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
533 mc_pair_stateless_t pair;
536 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
537 MC_pair_stateless_delete(pair);
548 void MC_print_statistics(mc_stats_t stats)
550 //XBT_INFO("State space size ~= %lu", stats->state_size);
551 XBT_INFO("Expanded states = %lu", stats->expanded_states);
552 XBT_INFO("Visited states = %lu", stats->visited_states);
553 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
554 XBT_INFO("Expanded / Visited = %lf",
555 (double) stats->visited_states / stats->expanded_states);
556 /*XBT_INFO("Exploration coverage = %lf",
557 (double)stats->expanded_states / stats->state_size); */
560 void MC_print_statistics_pairs(mc_stats_pair_t stats)
562 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
563 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
564 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
565 XBT_INFO("Expanded / Visited = %lf",
566 (double) stats->visited_pairs / stats->expanded_pairs);
567 /*XBT_INFO("Exploration coverage = %lf",
568 (double)stats->expanded_states / stats->state_size); */
571 void MC_assert(int prop)
573 if (MC_IS_ENABLED && !prop){
574 XBT_INFO("**************************");
575 XBT_INFO("*** PROPERTY NOT VALID ***");
576 XBT_INFO("**************************");
577 XBT_INFO("Counter-example execution trace:");
578 MC_dump_stack_safety(mc_stack_safety);
579 MC_print_statistics(mc_stats);
584 static void MC_assert_pair(int prop){
585 if (MC_IS_ENABLED && !prop) {
586 XBT_INFO("**************************");
587 XBT_INFO("*** PROPERTY NOT VALID ***");
588 XBT_INFO("**************************");
589 //XBT_INFO("Counter-example execution trace:");
590 MC_show_stack_liveness(mc_stack_liveness);
591 //MC_dump_snapshot_stack(mc_snapshot_stack);
592 MC_print_statistics_pairs(mc_stats_pair);
597 void MC_process_clock_add(smx_process_t process, double amount)
599 mc_time[process->pid] += amount;
602 double MC_process_clock_get(smx_process_t process)
605 return mc_time[process->pid];
610 void MC_automaton_load(const char *file){
612 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
616 if (_mc_property_automaton == NULL)
617 _mc_property_automaton = xbt_automaton_new();
619 xbt_automaton_load(_mc_property_automaton,file);
630 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
632 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
636 if (_mc_property_automaton == NULL)
637 _mc_property_automaton = xbt_automaton_new();
639 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
650 /************ MC_ignore ***********/
652 void MC_ignore_init(){
654 mmalloc_ignore = xbt_dynar_new(sizeof(mc_ignore_region_t), NULL);
655 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
659 void MC_ignore(void *address, size_t size){
663 mc_ignore_region_t region = NULL;
664 region = xbt_new0(s_mc_ignore_region_t, 1);
665 region->address = address;
667 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
669 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
670 region->fragment = -1;
672 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
675 unsigned int cursor = 0;
676 mc_ignore_region_t current_region;
677 xbt_dynar_foreach(mmalloc_ignore, cursor, current_region){
678 if(current_region->address > address)
682 xbt_dynar_insert_at(mmalloc_ignore, cursor, ®ion);
687 void MC_new_stack_area(void *stack, char *name){
689 stack_region_t region = NULL;
690 region = xbt_new0(s_stack_region_t, 1);
691 region->address = stack;
692 region->process_name = strdup(name);
693 xbt_dynar_push(stacks_areas, ®ion);
697 /************ DWARF ***********/
699 static e_dw_location_type get_location(char *expr, dw_location_t entry);
701 void MC_get_binary_local_variables(){
703 char *command = bprintf("dwarfdump -i %s", xbt_binary_name);
705 FILE* fp = popen(command, "r");
708 perror("popen failed");
710 char *line = NULL, *tmp_line = NULL, *tmp_location = NULL, *frame_name = NULL;
713 int valid_variable = 1, valid_frame = 1;
714 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *lowpc = NULL, *highpc = NULL;
715 xbt_dynar_t split = NULL;
717 read = getline(&line, &n, fp);
724 /* Wipeout the new line character */
725 line[read - 1] = '\0';
732 node_type = strtok(NULL, " ");
734 if(strcmp(node_type, "DW_TAG_subprogram") == 0){ /* New frame */
736 read = getline(&line, &n, fp);
738 while(read != -1 && line[0] != '<'){
743 node_type = strtok(line, " ");
745 if(node_type != NULL && strcmp(node_type, "DW_AT_name") == 0){
747 frame_name = strdup(strtok(NULL, " "));
748 read = getline(&line, &n, fp);
750 }else if(node_type != NULL && strcmp(node_type, "DW_AT_frame_base") == 0){
752 if(valid_frame == 1){
754 dw_frame_t frame = xbt_new0(s_dw_frame_t, 1);
755 frame->name = strdup(frame_name);
756 frame->variables = xbt_dynar_new(sizeof(dw_local_variable_t), NULL);
757 frame->location = xbt_new0(s_dw_location_t, 1);
759 location_type = strtok(NULL, " ");
761 if(strcmp(location_type, "<loclist") == 0){
763 frame->location->type = e_dw_loclist;
764 frame->location->location.loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
766 read = getline(&line, &n, fp);
767 xbt_str_ltrim(line, NULL);
769 while(read != -1 && line[0] == '['){
772 lowpc = strdup(strtok(NULL, "<"));
773 highpc = strdup(strtok(NULL, ">"));
774 tmp_location = strdup(strtok(NULL, ">"));
775 lowpc[strlen(lowpc) - 1] = '\0'; /* Remove last character '>' */
777 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
780 new_entry->lowpc = (void *) strtoul(strtok(NULL, "="), NULL, 16);
782 new_entry->highpc = (void *) strtoul(strtok(NULL, "="), NULL, 16);
784 new_entry->location = xbt_new0(s_dw_location_t, 1);
786 get_location(tmp_location, new_entry->location);
788 xbt_dynar_push(frame->location->location.loclist, &new_entry);
790 read = getline(&line, &n, fp);
791 xbt_str_ltrim(line, NULL);
795 read = getline(&line, &n, fp);
796 frame->location->type = get_location(location_type, frame->location);
800 xbt_dynar_push(mc_binary_local_variables, &frame);
804 read = getline(&line, &n, fp);
808 }else if(node_type != NULL && (strcmp(node_type, "DW_AT_declaration") == 0 || strcmp(node_type, "DW_AT_abstract_origin") == 0 || strcmp(node_type, "DW_AT_artificial") == 0)){
810 read = getline(&line, &n, fp);
815 read = getline(&line, &n, fp);
823 }else if(strcmp(node_type, "DW_TAG_variable") == 0){ /* New variable */
825 variable_name = NULL;
826 location_type = NULL;
828 read = getline(&line, &n, fp);
830 while(read != -1 && line[0] != '<'){
835 tmp_line = strdup(line);
837 node_type = strtok(line, " ");
839 if(node_type != NULL && strcmp(node_type, "DW_AT_name") == 0){
841 variable_name = strdup(strtok(NULL, " "));
842 read = getline(&line, &n, fp);
844 }else if(node_type != NULL && strcmp(node_type, "DW_AT_location") == 0){
846 if(valid_variable == 1){
848 location_type = strdup(strtok(NULL, " "));
850 dw_local_variable_t variable = xbt_new0(s_dw_local_variable_t, 1);
851 variable->name = strdup(variable_name);
852 variable->location = xbt_new0(s_dw_location_t, 1);
854 if(strcmp(location_type, "<loclist") == 0){
856 variable->location->type = e_dw_loclist;
857 variable->location->location.loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
859 read = getline(&line, &n, fp);
860 xbt_str_ltrim(line, NULL);
862 while(read != -1 && line[0] == '['){
865 lowpc = strdup(strtok(NULL, "<"));
866 highpc = strdup(strtok(NULL, ">"));
867 tmp_location = strdup(strtok(NULL, ">"));
868 lowpc[strlen(lowpc) - 1] = '\0'; /* Remove last character '>' */
870 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
873 new_entry->lowpc = (void *) strtoul(strtok(NULL, "="), NULL, 16);
875 new_entry->highpc = (void *) strtoul(strtok(NULL, "="), NULL, 16);
877 new_entry->location = xbt_new0(s_dw_location_t, 1);
879 get_location(tmp_location, new_entry->location);
881 xbt_dynar_push(variable->location->location.loclist, &new_entry);
883 read = getline(&line, &n, fp);
884 xbt_str_ltrim(line, NULL);
889 xbt_str_strip_spaces(tmp_line);
890 split = xbt_str_split(tmp_line, " ");
891 xbt_dynar_remove_at(split, 0, NULL);
892 location_type = xbt_str_join(split, " ");
894 variable->location->type = get_location(location_type, variable->location);
895 read = getline(&line, &n, fp);
899 xbt_dynar_push(((dw_frame_t)xbt_dynar_get_as(mc_binary_local_variables, xbt_dynar_length(mc_binary_local_variables) - 1, dw_frame_t))->variables, &variable);
903 read = getline(&line, &n, fp);
907 }else if(node_type != NULL && (strcmp(node_type, "DW_AT_artificial") == 0 || strcmp(node_type, "DW_AT_external") == 0)){
910 read = getline(&line, &n, fp);
914 read = getline(&line, &n, fp);
924 read = getline(&line, &n, fp);
930 read = getline(&line, &n, fp);
936 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug))
937 print_local_variables(mc_binary_local_variables);
939 free(line); free(tmp_line); free(tmp_location); free(frame_name);
940 free(node_type); free(location_type); free(variable_name); free(lowpc); free(highpc);
945 void print_local_variables(xbt_dynar_t list){
948 dw_local_variable_t variable;
949 dw_location_entry_t entry;
950 dw_location_t location_entry;
951 unsigned int cursor = 0, cursor2 = 0, cursor3 = 0, cursor4 = 0;
953 xbt_dynar_foreach(list, cursor, frame){
954 fprintf(stderr, "Frame name : %s", frame->name);
955 fprintf(stderr, "Location type : %d\n", frame->location->type);
956 fprintf(stderr, "Variables : (%lu)\n", xbt_dynar_length(frame->variables));
957 xbt_dynar_foreach(frame->variables, cursor2, variable){
958 fprintf(stderr, "Name : %s", variable->name);
959 fprintf(stderr, "Location type : %d\n", variable->location->type);
960 switch(variable->location->type){
962 xbt_dynar_foreach(variable->location->location.loclist, cursor3, entry){
963 fprintf(stderr, "Lowpc : %p, Highpc : %p,", entry->lowpc, entry->highpc);
964 switch(entry->location->type){
966 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
968 case e_dw_bregister_op:
969 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
972 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
974 case e_dw_fbregister_op:
975 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
978 fprintf(stderr, " Location :\n");
979 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
980 switch(location_entry->type){
982 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
984 case e_dw_bregister_op:
985 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);
988 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
990 case e_dw_fbregister_op:
991 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
994 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);
996 case e_dw_arithmetic :
997 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1000 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1002 case e_dw_constant :
1003 fprintf(stderr, "%d) Constant %d\n", cursor4 + 1, location_entry->location.constant.value);
1006 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1012 fprintf(stderr, "Location type not supported\n");
1019 fprintf(stderr, "Location :\n");
1020 xbt_dynar_foreach(variable->location->location.compose, cursor4, location_entry){
1021 switch(location_entry->type){
1022 case e_dw_register :
1023 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1025 case e_dw_bregister_op:
1026 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);
1029 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1031 case e_dw_fbregister_op:
1032 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1035 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);
1037 case e_dw_arithmetic :
1038 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1041 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1043 case e_dw_constant :
1044 fprintf(stderr, "%d) Constant %d\n", cursor4 + 1, location_entry->location.constant.value);
1047 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1053 fprintf(stderr, "Location type not supported\n");
1061 static e_dw_location_type get_location(char *expr, dw_location_t entry){
1064 char *tok = NULL, *tmp_tok = NULL;
1066 xbt_dynar_t tokens = xbt_str_split(expr, NULL);
1067 xbt_dynar_remove_at(tokens, xbt_dynar_length(tokens) - 1, NULL);
1069 if(xbt_dynar_length(tokens) > 1){
1071 entry->type = e_dw_compose;
1072 entry->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1074 while(cursor < xbt_dynar_length(tokens)){
1076 tok = xbt_dynar_get_as(tokens, cursor, char*);
1078 if(strncmp(tok, "DW_OP_reg", 9) == 0){
1079 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1080 new_element->type = e_dw_register;
1082 new_element->location.reg = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1084 new_element->location.reg = atoi(strtok(tok, "DW_OP_reg"));
1086 xbt_dynar_push(entry->location.compose, &new_element);
1087 }else if(strcmp(tok, "DW_OP_fbreg") == 0){
1088 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1089 new_element->type = e_dw_fbregister_op;
1090 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1091 xbt_dynar_push(entry->location.compose, &new_element);
1092 }else if(strncmp(tok, "DW_OP_breg", 10) == 0){
1093 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1094 new_element->type = e_dw_bregister_op;
1096 new_element->location.breg_op.reg = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1097 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1099 if(strchr(tok,'+') != NULL){
1100 tmp_tok = strtok(tok,"DW_OP_breg");
1101 new_element->location.breg_op.reg = atoi(strtok(tmp_tok,"+"));
1102 new_element->location.breg_op.offset = atoi(strtok(NULL,"+"));
1104 new_element->location.breg_op.reg = atoi(strtok(tok, "DW_OP_breg"));
1105 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1108 xbt_dynar_push(entry->location.compose, &new_element);
1109 }else if(strncmp(tok, "DW_OP_lit", 9) == 0){
1110 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1111 new_element->type = e_dw_lit;
1112 new_element->location.lit = atoi(strtok(tok, "DW_OP_lit"));
1113 xbt_dynar_push(entry->location.compose, &new_element);
1114 }else if(strcmp(tok, "DW_OP_piece") == 0){
1115 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1116 new_element->type = e_dw_piece;
1117 if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1118 new_element->location.piece = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1120 new_element->location.piece = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';
1121 xbt_dynar_push(entry->location.compose, &new_element);
1123 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1124 strcmp(tok, "DW_OP_and") == 0 ||
1125 strcmp(tok, "DW_OP_div") == 0 ||
1126 strcmp(tok, "DW_OP_minus") == 0 ||
1127 strcmp(tok, "DW_OP_mod") == 0 ||
1128 strcmp(tok, "DW_OP_mul") == 0 ||
1129 strcmp(tok, "DW_OP_neg") == 0 ||
1130 strcmp(tok, "DW_OP_not") == 0 ||
1131 strcmp(tok, "DW_OP_or") == 0 ||
1132 strcmp(tok, "DW_OP_plus") == 0 ||
1133 strcmp(tok, "DW_OP_plus_uconst") == 0){
1134 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1135 new_element->type = e_dw_arithmetic;
1136 new_element->location.arithmetic = strdup(strtok(tok, "DW_OP_"));
1137 xbt_dynar_push(entry->location.compose, &new_element);
1138 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1140 }else if(strcmp(tok, "DW_OP_deref_size") == 0){
1141 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1142 new_element->type = e_dw_deref;
1143 if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1144 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1146 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';
1147 xbt_dynar_push(entry->location.compose, &new_element);
1148 }else if(strcmp(tok, "DW_OP_deref") == 0){
1149 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1150 new_element->type = e_dw_deref;
1151 new_element->location.deref_size = sizeof(void *);
1152 xbt_dynar_push(entry->location.compose, &new_element);
1153 }else if(strcmp(tok, "DW_OP_constu") == 0){
1154 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1155 new_element->type = e_dw_constant;
1156 new_element->location.constant.is_signed = 0;
1157 new_element->location.constant.bytes = 1;
1158 if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1159 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1161 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';
1162 xbt_dynar_push(entry->location.compose, &new_element);
1163 }else if(strcmp(tok, "DW_OP_consts") == 0){
1164 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1165 new_element->type = e_dw_constant;
1166 new_element->location.constant.is_signed = 1;
1167 new_element->location.constant.bytes = 1;
1168 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1169 xbt_dynar_push(entry->location.compose, &new_element);
1170 }else if(strcmp(tok, "DW_OP_const1u") == 0 ||
1171 strcmp(tok, "DW_OP_const2u") == 0 ||
1172 strcmp(tok, "DW_OP_const4u") == 0 ||
1173 strcmp(tok, "DW_OP_const8u") == 0){
1174 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1175 new_element->type = e_dw_constant;
1176 new_element->location.constant.is_signed = 0;
1177 new_element->location.constant.bytes = tok[11] - '0';
1178 if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1179 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1181 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';
1182 xbt_dynar_push(entry->location.compose, &new_element);
1183 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1184 strcmp(tok, "DW_OP_const2s") == 0 ||
1185 strcmp(tok, "DW_OP_const4s") == 0 ||
1186 strcmp(tok, "DW_OP_const8s") == 0){
1187 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1188 new_element->type = e_dw_constant;
1189 new_element->location.constant.is_signed = 1;
1190 new_element->location.constant.bytes = tok[11] - '0';
1191 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, ++cursor, char*));
1192 xbt_dynar_push(entry->location.compose, &new_element);
1194 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1195 new_element->type = e_dw_unsupported;
1196 xbt_dynar_push(entry->location.compose, &new_element);
1205 /*xbt_dynar_free(&tokens);*/
1207 return e_dw_compose;
1211 if(strncmp(expr, "DW_OP_reg", 9) == 0){
1212 entry->type = e_dw_register;
1213 entry->location.reg = atoi(strtok(expr,"DW_OP_reg"));
1214 }else if(strncmp(expr, "DW_OP_breg", 10) == 0){
1215 entry->type = e_dw_bregister_op;
1216 tok = strtok(expr, "+");
1217 entry->location.breg_op.offset = atoi(strtok(NULL, "+"));
1218 entry->location.breg_op.reg = atoi(strtok(tok, "DW_OP_breg"));
1220 entry->type = e_dw_unsupported;