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;
26 extern int _surf_init_status;
27 void _mc_cfg_cb_reduce(const char *name, int pos) {
28 if (_surf_init_status && !_surf_do_model_check) {
29 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.");
31 char *val= xbt_cfg_get_string(_surf_cfg_set, name);
32 if (!strcasecmp(val,"none")) {
33 mc_reduce_kind = e_mc_reduce_none;
34 } else if (!strcasecmp(val,"dpor")) {
35 mc_reduce_kind = e_mc_reduce_dpor;
37 xbt_die("configuration option %s can only take 'none' or 'dpor' as a value",name);
39 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
42 void _mc_cfg_cb_checkpoint(const char *name, int pos) {
43 if (_surf_init_status && !_surf_do_model_check) {
44 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.");
46 _surf_mc_checkpoint = xbt_cfg_get_int(_surf_cfg_set, name);
47 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
49 void _mc_cfg_cb_property(const char *name, int pos) {
50 if (_surf_init_status && !_surf_do_model_check) {
51 xbt_die("You are specifying a property after the initialization (through MSG_config?), but model-checking was not activated at config time (through --cfg=model-check:1). This won't work, sorry.");
53 _surf_mc_property_file= xbt_cfg_get_string(_surf_cfg_set, name);
54 xbt_cfg_set_int(_surf_cfg_set,"model-check",1);
58 /* MC global data structures */
60 mc_state_t mc_current_state = NULL;
61 char mc_replay_mode = FALSE;
62 double *mc_time = NULL;
63 mc_snapshot_t initial_snapshot = NULL;
68 xbt_fifo_t mc_stack_safety = NULL;
69 mc_stats_t mc_stats = NULL;
73 mc_stats_pair_t mc_stats_pair = NULL;
74 xbt_fifo_t mc_stack_liveness = NULL;
75 mc_snapshot_t initial_snapshot_liveness = NULL;
79 xbt_dict_t mc_local_variables = NULL;
81 /* Ignore mechanism */
82 extern xbt_dynar_t mc_comparison_ignore;
83 extern xbt_dynar_t stacks_areas;
85 xbt_automaton_t _mc_property_automaton = NULL;
87 /* Static functions */
89 static void MC_assert_pair(int prop);
90 static dw_location_t get_location(xbt_dict_t location_list, char *expr);
91 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset);
93 void MC_do_the_modelcheck_for_real() {
94 if (!_surf_mc_property_file || _surf_mc_property_file[0]=='\0') {
95 if (mc_reduce_kind==e_mc_reduce_unset)
96 mc_reduce_kind=e_mc_reduce_dpor;
98 XBT_INFO("Check a safety property");
103 if (mc_reduce_kind==e_mc_reduce_unset)
104 mc_reduce_kind=e_mc_reduce_none;
106 XBT_INFO("Check the liveness property %s",_surf_mc_property_file);
107 MC_automaton_load(_surf_mc_property_file);
108 MC_modelcheck_liveness();
113 * \brief Initialize the model-checker data structures
115 void MC_init_safety(void)
118 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
120 /* Check if MC is already initialized */
121 if (initial_snapshot)
124 mc_time = xbt_new0(double, simix_process_maxpid);
126 /* Initialize the data structures that must be persistent across every
127 iteration of the model-checker (in RAW memory) */
131 /* Initialize statistics */
132 mc_stats = xbt_new0(s_mc_stats_t, 1);
133 mc_stats->state_size = 1;
135 /* Create exploration stack */
136 mc_stack_safety = xbt_fifo_new();
143 /* Save the initial state */
144 initial_snapshot = xbt_new0(s_mc_snapshot_t, 1);
145 MC_take_snapshot(initial_snapshot);
156 void MC_compare(void){
161 void MC_modelcheck(void)
168 void MC_modelcheck_liveness(){
170 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
173 XBT_INFO("Start init mc");
175 mc_time = xbt_new0(double, simix_process_maxpid);
177 /* mc_time refers to clock for each process -> ignore it for heap comparison */
179 for(i = 0; i<simix_process_maxpid; i++)
180 MC_ignore(&(mc_time[i]), sizeof(double));
184 /* Initialize the data structures that must be persistent across every
185 iteration of the model-checker (in RAW memory) */
189 char *ls_path = get_libsimgrid_path();
191 mc_local_variables = xbt_dict_new_homogeneous(NULL);
193 /* Get local variables in binary for state equality detection */
194 xbt_dict_t binary_location_list = MC_get_location_list(xbt_binary_name);
195 MC_get_local_variables(xbt_binary_name, binary_location_list, &mc_local_variables);
197 /* Get local variables in libsimgrid for state equality detection */
198 xbt_dict_t libsimgrid_location_list = MC_get_location_list(ls_path);
199 MC_get_local_variables(ls_path, libsimgrid_location_list, &mc_local_variables);
201 /* Initialize statistics */
202 mc_stats_pair = xbt_new0(s_mc_stats_pair_t, 1);
204 XBT_DEBUG("Creating stack");
206 /* Create exploration stack */
207 mc_stack_liveness = xbt_fifo_new();
214 MC_print_statistics_pairs(mc_stats_pair);
223 MC_print_statistics(mc_stats);
229 int MC_random(int min, int max)
231 /*FIXME: return mc_current_state->executed_transition->random.value;*/
236 * \brief Schedules all the process that are ready to run
238 void MC_wait_for_requests(void)
240 smx_process_t process;
244 while (!xbt_dynar_is_empty(simix_global->process_to_run)) {
245 SIMIX_process_runall();
246 xbt_dynar_foreach(simix_global->process_that_ran, iter, process) {
247 req = &process->simcall;
248 if (req->call != SIMCALL_NONE && !MC_request_is_visible(req))
249 SIMIX_simcall_pre(req, 0);
254 int MC_deadlock_check()
256 int deadlock = FALSE;
257 smx_process_t process;
258 if(xbt_swag_size(simix_global->process_list)){
260 xbt_swag_foreach(process, simix_global->process_list){
261 if(process->simcall.call != SIMCALL_NONE
262 && MC_request_is_enabled(&process->simcall)){
272 * \brief Re-executes from the state at position start all the transitions indicated by
273 * a given model-checker stack.
274 * \param stack The stack with the transitions to execute.
275 * \param start Start index to begin the re-execution.
277 void MC_replay(xbt_fifo_t stack, int start)
279 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
283 smx_simcall_t req = NULL, saved_req = NULL;
284 xbt_fifo_item_t item, start_item;
287 XBT_DEBUG("**** Begin Replay ****");
290 /* Restore the initial state */
291 MC_restore_snapshot(initial_snapshot);
292 /* At the moment of taking the snapshot the raw heap was set, so restoring
293 * it will set it back again, we have to unset it to continue */
297 start_item = xbt_fifo_get_last_item(stack);
300 start_item = xbt_fifo_get_prev_item(start_item);
305 /* Traverse the stack from the state at position start and re-execute the transitions */
306 for (item = start_item;
307 item != xbt_fifo_get_first_item(stack);
308 item = xbt_fifo_get_prev_item(item)) {
310 state = (mc_state_t) xbt_fifo_get_item_content(item);
311 saved_req = MC_state_get_executed_request(state, &value);
314 /* because we got a copy of the executed request, we have to fetch the
315 real one, pointed by the request field of the issuer process */
316 req = &saved_req->issuer->simcall;
318 /* Debug information */
319 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
320 req_str = MC_request_to_string(req, value);
321 XBT_DEBUG("Replay: %s (%p)", req_str, state);
326 SIMIX_simcall_pre(req, value);
327 MC_wait_for_requests();
329 /* Update statistics */
330 mc_stats->visited_states++;
331 mc_stats->executed_transitions++;
333 XBT_DEBUG("**** End Replay ****");
343 void MC_replay_liveness(xbt_fifo_t stack, int all_stack)
346 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
350 smx_simcall_t req = NULL, saved_req = NULL;
351 xbt_fifo_item_t item;
353 mc_pair_stateless_t pair;
356 XBT_DEBUG("**** Begin Replay ****");
358 /* Restore the initial state */
359 MC_restore_snapshot(initial_snapshot_liveness);
360 /* At the moment of taking the snapshot the raw heap was set, so restoring
361 * it will set it back again, we have to unset it to continue */
366 item = xbt_fifo_get_last_item(stack);
368 while(depth <= xbt_fifo_size(stack)){
370 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
371 state = (mc_state_t) pair->graph_state;
373 if(pair->requests > 0){
375 saved_req = MC_state_get_executed_request(state, &value);
376 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
378 if(saved_req != NULL){
379 /* because we got a copy of the executed request, we have to fetch the
380 real one, pointed by the request field of the issuer process */
381 req = &saved_req->issuer->simcall;
382 //XBT_DEBUG("Req->call %u", req->call);
384 /* Debug information */
385 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
386 req_str = MC_request_to_string(req, value);
387 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
393 SIMIX_simcall_pre(req, value);
394 MC_wait_for_requests();
399 /* Update statistics */
400 mc_stats_pair->visited_pairs++;
402 item = xbt_fifo_get_prev_item(item);
407 /* Traverse the stack from the initial state and re-execute the transitions */
408 for (item = xbt_fifo_get_last_item(stack);
409 item != xbt_fifo_get_first_item(stack);
410 item = xbt_fifo_get_prev_item(item)) {
412 pair = (mc_pair_stateless_t) xbt_fifo_get_item_content(item);
413 state = (mc_state_t) pair->graph_state;
415 if(pair->requests > 0){
417 saved_req = MC_state_get_executed_request(state, &value);
418 //XBT_DEBUG("SavedReq->call %u", saved_req->call);
420 if(saved_req != NULL){
421 /* because we got a copy of the executed request, we have to fetch the
422 real one, pointed by the request field of the issuer process */
423 req = &saved_req->issuer->simcall;
424 //XBT_DEBUG("Req->call %u", req->call);
426 /* Debug information */
427 if(XBT_LOG_ISENABLED(mc_global, xbt_log_priority_debug)){
428 req_str = MC_request_to_string(req, value);
429 XBT_DEBUG("Replay (depth = %d) : %s (%p)", depth, req_str, state);
435 SIMIX_simcall_pre(req, value);
436 MC_wait_for_requests();
441 /* Update statistics */
442 mc_stats_pair->visited_pairs++;
446 XBT_DEBUG("**** End Replay ****");
456 * \brief Dumps the contents of a model-checker's stack and shows the actual
458 * \param stack The stack to dump
460 void MC_dump_stack_safety(xbt_fifo_t stack)
463 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
465 MC_show_stack_safety(stack);
467 if(!_surf_mc_checkpoint){
472 while ((state = (mc_state_t) xbt_fifo_pop(stack)) != NULL)
473 MC_state_delete(state);
486 void MC_show_stack_safety(xbt_fifo_t stack)
490 xbt_fifo_item_t item;
492 char *req_str = NULL;
494 for (item = xbt_fifo_get_last_item(stack);
495 (item ? (state = (mc_state_t) (xbt_fifo_get_item_content(item)))
496 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
497 req = MC_state_get_executed_request(state, &value);
499 req_str = MC_request_to_string(req, value);
500 XBT_INFO("%s", req_str);
506 void MC_show_deadlock(smx_simcall_t req)
508 /*char *req_str = NULL;*/
509 XBT_INFO("**************************");
510 XBT_INFO("*** DEAD-LOCK DETECTED ***");
511 XBT_INFO("**************************");
512 XBT_INFO("Locked request:");
513 /*req_str = MC_request_to_string(req);
514 XBT_INFO("%s", req_str);
516 XBT_INFO("Counter-example execution trace:");
517 MC_dump_stack_safety(mc_stack_safety);
521 void MC_show_stack_liveness(xbt_fifo_t stack){
523 mc_pair_stateless_t pair;
524 xbt_fifo_item_t item;
526 char *req_str = NULL;
528 for (item = xbt_fifo_get_last_item(stack);
529 (item ? (pair = (mc_pair_stateless_t) (xbt_fifo_get_item_content(item)))
530 : (NULL)); item = xbt_fifo_get_prev_item(item)) {
531 req = MC_state_get_executed_request(pair->graph_state, &value);
533 if(pair->requests>0){
534 req_str = MC_request_to_string(req, value);
535 XBT_INFO("%s", req_str);
538 XBT_INFO("End of system requests but evolution in Büchi automaton");
544 void MC_dump_stack_liveness(xbt_fifo_t stack){
546 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
548 mc_pair_stateless_t pair;
551 while ((pair = (mc_pair_stateless_t) xbt_fifo_pop(stack)) != NULL)
552 MC_pair_stateless_delete(pair);
563 void MC_print_statistics(mc_stats_t stats)
565 //XBT_INFO("State space size ~= %lu", stats->state_size);
566 XBT_INFO("Expanded states = %lu", stats->expanded_states);
567 XBT_INFO("Visited states = %lu", stats->visited_states);
568 XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
569 XBT_INFO("Expanded / Visited = %lf",
570 (double) stats->visited_states / stats->expanded_states);
571 /*XBT_INFO("Exploration coverage = %lf",
572 (double)stats->expanded_states / stats->state_size); */
575 void MC_print_statistics_pairs(mc_stats_pair_t stats)
577 XBT_INFO("Expanded pairs = %lu", stats->expanded_pairs);
578 XBT_INFO("Visited pairs = %lu", stats->visited_pairs);
579 //XBT_INFO("Executed transitions = %lu", stats->executed_transitions);
580 XBT_INFO("Expanded / Visited = %lf",
581 (double) stats->visited_pairs / stats->expanded_pairs);
582 /*XBT_INFO("Exploration coverage = %lf",
583 (double)stats->expanded_states / stats->state_size); */
586 void MC_assert(int prop)
588 if (MC_IS_ENABLED && !prop){
589 XBT_INFO("**************************");
590 XBT_INFO("*** PROPERTY NOT VALID ***");
591 XBT_INFO("**************************");
592 XBT_INFO("Counter-example execution trace:");
593 MC_dump_stack_safety(mc_stack_safety);
594 MC_print_statistics(mc_stats);
599 static void MC_assert_pair(int prop){
600 if (MC_IS_ENABLED && !prop) {
601 XBT_INFO("**************************");
602 XBT_INFO("*** PROPERTY NOT VALID ***");
603 XBT_INFO("**************************");
604 //XBT_INFO("Counter-example execution trace:");
605 MC_show_stack_liveness(mc_stack_liveness);
606 //MC_dump_snapshot_stack(mc_snapshot_stack);
607 MC_print_statistics_pairs(mc_stats_pair);
612 void MC_process_clock_add(smx_process_t process, double amount)
614 mc_time[process->pid] += amount;
617 double MC_process_clock_get(smx_process_t process)
620 return mc_time[process->pid];
625 void MC_automaton_load(const char *file){
627 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
631 if (_mc_property_automaton == NULL)
632 _mc_property_automaton = xbt_automaton_new();
634 xbt_automaton_load(_mc_property_automaton,file);
645 void MC_automaton_new_propositional_symbol(const char* id, void* fct) {
647 raw_mem_set = (mmalloc_get_current_heap() == raw_heap);
651 if (_mc_property_automaton == NULL)
652 _mc_property_automaton = xbt_automaton_new();
654 xbt_new_propositional_symbol(_mc_property_automaton,id,fct);
665 /************ MC_ignore ***********/
667 void MC_ignore(void *address, size_t size){
671 if(mc_comparison_ignore == NULL)
672 mc_comparison_ignore = xbt_dynar_new(sizeof(mc_ignore_region_t), NULL);
674 mc_ignore_region_t region = NULL;
675 region = xbt_new0(s_mc_ignore_region_t, 1);
676 region->address = address;
679 if((address >= std_heap) && (address <= (void*)((char *)std_heap + STD_HEAP_SIZE))){
681 region->block = ((char*)address - (char*)((xbt_mheap_t)std_heap)->heapbase) / BLOCKSIZE + 1;
683 if(((xbt_mheap_t)std_heap)->heapinfo[region->block].type == 0){
684 region->fragment = -1;
686 region->fragment = ((uintptr_t) (ADDR2UINT (address) % (BLOCKSIZE))) >> ((xbt_mheap_t)std_heap)->heapinfo[region->block].type;
691 unsigned int cursor = 0;
692 mc_ignore_region_t current_region;
693 xbt_dynar_foreach(mc_comparison_ignore, cursor, current_region){
694 if(current_region->address > address)
698 xbt_dynar_insert_at(mc_comparison_ignore, cursor, ®ion);
703 void MC_new_stack_area(void *stack, char *name){
705 if(stacks_areas == NULL)
706 stacks_areas = xbt_dynar_new(sizeof(stack_region_t), NULL);
709 stack_region_t region = NULL;
710 region = xbt_new0(s_stack_region_t, 1);
711 region->address = stack;
712 region->process_name = strdup(name);
713 xbt_dynar_push(stacks_areas, ®ion);
717 /************ DWARF ***********/
719 xbt_dict_t MC_get_location_list(const char *elf_file){
721 char *command = bprintf("objdump -Wo %s", elf_file);
723 FILE *fp = popen(command, "r");
726 perror("popen for objdump failed");
728 int debug = 0; /*Detect if the program has been compiled with -g */
730 xbt_dict_t location_list = xbt_dict_new_homogeneous(NULL);
731 char *line = NULL, *loc_expr = NULL;
735 xbt_dynar_t split = NULL;
737 while ((read = getline(&line, &n, fp)) != -1) {
739 /* Wipeout the new line character */
740 line[read - 1] = '\0';
742 xbt_str_trim(line, NULL);
747 if(strlen(line) == 0)
752 if(strncmp(line, elf_file, strlen(elf_file)) == 0)
755 if(strncmp(line, "Contents", 8) == 0)
758 if(strncmp(line, "Offset", 6) == 0){
765 XBT_INFO("Your program must be compiled with -g");
769 xbt_dynar_t loclist = xbt_dynar_new(sizeof(dw_location_entry_t), NULL);
771 xbt_str_strip_spaces(line);
772 split = xbt_str_split(line, " ");
774 while(read != -1 && strcmp("<End", (char *)xbt_dynar_get_as(split, 1, char *)) != 0){
776 dw_location_entry_t new_entry = xbt_new0(s_dw_location_entry_t, 1);
777 new_entry->lowpc = strtoul((char *)xbt_dynar_get_as(split, 1, char *), NULL, 16);
778 new_entry->highpc = strtoul((char *)xbt_dynar_get_as(split, 2, char *), NULL, 16);
781 while(cursor_remove < 3){
782 xbt_dynar_remove_at(split, 0, NULL);
786 loc_expr = xbt_str_join(split, " ");
787 xbt_str_ltrim(loc_expr, "(");
788 xbt_str_rtrim(loc_expr, ")");
789 new_entry->location = get_location(NULL, loc_expr);
791 xbt_dynar_push(loclist, &new_entry);
793 xbt_dynar_free(&split);
796 read = getline(&line, &n, fp);
798 line[read - 1] = '\0';
799 xbt_str_strip_spaces(line);
800 split = xbt_str_split(line, " ");
806 char *key = bprintf("%d", (int)strtoul((char *)xbt_dynar_get_as(split, 0, char *), NULL, 16));
807 xbt_dict_set(location_list, key, loclist, NULL);
809 xbt_dynar_free(&split);
817 return location_list;
820 char *get_libsimgrid_path(){
822 char *command = bprintf("ldd %s", xbt_binary_name);
824 FILE *fp = popen(command, "r");
826 perror("popen for ldd failed");
833 while((read = getline(&line, &n, fp)) != -1){
838 /* Wipeout the new line character */
839 line[read - 1] = '\0';
841 xbt_str_strip_spaces(line);
842 xbt_str_ltrim(line, NULL);
843 split = xbt_str_split(line, " ");
845 if(strncmp((char *)xbt_dynar_get_as(split, 0, char *), "libsimgrid.so", 13) == 0){
849 return ((char *)xbt_dynar_get_as(split, 2, char *));
852 xbt_dynar_free(&split);
864 static dw_frame_t get_frame_by_offset(xbt_dict_t all_variables, unsigned long int offset){
866 xbt_dict_cursor_t cursor = NULL;
870 xbt_dict_foreach(all_variables, cursor, name, res) {
871 if(offset >= res->start && offset < res->end)
879 void MC_get_local_variables(const char *elf_file, xbt_dict_t location_list, xbt_dict_t *all_variables){
881 char *command = bprintf("objdump -Wi %s", elf_file);
883 FILE *fp = popen(command, "r");
886 perror("popen for objdump failed");
888 char *line = NULL, *origin, *abstract_origin, *current_frame = NULL;
891 int valid_variable = 1;
892 char *node_type = NULL, *location_type = NULL, *variable_name = NULL, *loc_expr = NULL;
893 xbt_dynar_t split = NULL, split2 = NULL;
895 xbt_dict_t variables_origin = xbt_dict_new_homogeneous(NULL);
896 xbt_dict_t subprograms_origin = xbt_dict_new_homogeneous(NULL);
897 char *subprogram_name = NULL, *subprogram_start = NULL, *subprogram_end = NULL;
898 int new_frame = 0, new_variable = 0;
899 dw_frame_t variable_frame, subroutine_frame = NULL;
901 read = getline(&line, &n, fp);
906 read = getline(&line, &n, fp);
910 /* Wipeout the new line character */
911 line[read - 1] = '\0';
913 if(strlen(line) == 0){
914 read = getline(&line, &n, fp);
918 xbt_str_ltrim(line, NULL);
919 xbt_str_strip_spaces(line);
922 read = getline(&line, &n, fp);
926 xbt_dynar_free(&split);
927 split = xbt_str_split(line, " ");
930 node_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
932 if(strcmp(node_type, "(DW_TAG_subprogram)") == 0){ /* New frame */
934 dw_frame_t frame = NULL;
936 strtok(xbt_dynar_get_as(split, 0, char *), "<");
937 subprogram_start = strdup(strtok(NULL, "<"));
938 xbt_str_rtrim(subprogram_start, ">:");
940 read = getline(&line, &n, fp);
945 read = getline(&line, &n, fp);
949 /* Wipeout the new line character */
950 line[read - 1] = '\0';
952 if(strlen(line) == 0){
953 read = getline(&line, &n, fp);
957 xbt_dynar_free(&split);
958 xbt_str_rtrim(line, NULL);
959 xbt_str_strip_spaces(line);
960 split = xbt_str_split(line, " ");
962 node_type = xbt_dynar_get_as(split, 1, char *);
964 if(strncmp(node_type, "DW_AT_", 6) != 0)
967 if(strcmp(node_type, "DW_AT_sibling") == 0){
969 subprogram_end = strdup(xbt_dynar_get_as(split, 3, char*));
970 xbt_str_ltrim(subprogram_end, "<0x");
971 xbt_str_rtrim(subprogram_end, ">");
973 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){ /* Frame already in dict */
976 abstract_origin = strdup(xbt_dynar_get_as(split, 2, char*));
977 xbt_str_ltrim(abstract_origin, "<0x");
978 xbt_str_rtrim(abstract_origin, ">");
979 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, abstract_origin);
980 frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
982 }else if(strcmp(node_type, "DW_AT_name") == 0){
986 frame = xbt_new0(s_dw_frame_t, 1);
987 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
988 frame->variables = xbt_dict_new_homogeneous(NULL);
989 frame->frame_base = xbt_new0(s_dw_location_t, 1);
990 current_frame = strdup(frame->name);
992 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
994 }else if(strcmp(node_type, "DW_AT_frame_base") == 0){
996 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
998 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1000 frame->frame_base = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1004 xbt_str_strip_spaces(line);
1005 split2 = xbt_str_split(line, "(");
1006 xbt_dynar_remove_at(split2, 0, NULL);
1007 loc_expr = xbt_str_join(split2, " ");
1008 xbt_str_rtrim(loc_expr, ")");
1009 frame->frame_base = get_location(NULL, loc_expr);
1010 xbt_dynar_free(&split2);
1014 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1017 frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1019 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1022 frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1024 }else if(strcmp(node_type, "DW_AT_MIPS_linkage_name:") == 0){
1027 free(current_frame);
1028 frame->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1029 current_frame = strdup(frame->name);
1030 xbt_dict_set(subprograms_origin, subprogram_start, frame->name, NULL);
1034 read = getline(&line, &n, fp);
1039 frame->start = strtoul(subprogram_start, NULL, 16);
1040 if(subprogram_end != NULL)
1041 frame->end = strtoul(subprogram_end, NULL, 16);
1042 xbt_dict_set(*all_variables, frame->name, frame, NULL);
1045 free(subprogram_start);
1046 if(subprogram_end != NULL){
1047 free(subprogram_end);
1048 subprogram_end = NULL;
1052 }else if(strcmp(node_type, "(DW_TAG_variable)") == 0){ /* New variable */
1054 dw_local_variable_t var = NULL;
1056 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1057 origin = strdup(strtok(NULL, "<"));
1058 xbt_str_rtrim(origin, ">:");
1060 read = getline(&line, &n, fp);
1065 read = getline(&line, &n, fp);
1069 /* Wipeout the new line character */
1070 line[read - 1] = '\0';
1072 if(strlen(line) == 0){
1073 read = getline(&line, &n, fp);
1077 xbt_dynar_free(&split);
1078 xbt_str_rtrim(line, NULL);
1079 xbt_str_strip_spaces(line);
1080 split = xbt_str_split(line, " ");
1082 node_type = xbt_dynar_get_as(split, 1, char *);
1084 if(strncmp(node_type, "DW_AT_", 6) != 0)
1087 if(strcmp(node_type, "DW_AT_name") == 0){
1090 var = xbt_new0(s_dw_local_variable_t, 1);
1091 var->name = strdup(xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *));
1093 xbt_dict_set(variables_origin, origin, var->name, NULL);
1095 }else if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1098 abstract_origin = xbt_dynar_get_as(split, 2, char *);
1099 xbt_str_ltrim(abstract_origin, "<0x");
1100 xbt_str_rtrim(abstract_origin, ">");
1102 variable_name = (char *)xbt_dict_get_or_null(variables_origin, abstract_origin);
1103 variable_frame = get_frame_by_offset(*all_variables, strtoul(abstract_origin, NULL, 16));
1104 var = xbt_dict_get_or_null(variable_frame->variables, variable_name);
1106 }else if(strcmp(node_type, "DW_AT_location") == 0){
1108 if(valid_variable == 1 && var != NULL){
1110 var->location = xbt_new0(s_dw_location_t, 1);
1112 location_type = xbt_dynar_get_as(split, xbt_dynar_length(split) - 1, char *);
1114 if(strcmp(location_type, "list)") == 0){ /* Search location in location list */
1116 var->location = get_location(location_list, xbt_dynar_get_as(split, 3, char *));
1120 xbt_str_strip_spaces(line);
1121 split2 = xbt_str_split(line, "(");
1122 xbt_dynar_remove_at(split2, 0, NULL);
1123 loc_expr = xbt_str_join(split2, " ");
1124 xbt_str_rtrim(loc_expr, ")");
1125 var->location = get_location(NULL, loc_expr);
1126 xbt_dynar_free(&split2);
1132 }else if(strcmp(node_type, "DW_AT_external") == 0){
1138 read = getline(&line, &n, fp);
1142 if(new_variable == 1 && valid_variable == 1){
1144 variable_frame = xbt_dict_get_or_null(*all_variables, current_frame);
1145 xbt_dict_set(variable_frame->variables, var->name, var, NULL);
1151 }else if(strcmp(node_type, "(DW_TAG_inlined_subroutine)") == 0){
1153 strtok(xbt_dynar_get_as(split, 0, char *), "<");
1154 origin = strdup(strtok(NULL, "<"));
1155 xbt_str_rtrim(origin, ">:");
1157 read = getline(&line, &n, fp);
1161 /* Wipeout the new line character */
1162 line[read - 1] = '\0';
1165 read = getline(&line, &n, fp);
1169 if(strlen(line) == 0){
1170 read = getline(&line, &n, fp);
1174 xbt_dynar_free(&split);
1175 xbt_str_rtrim(line, NULL);
1176 xbt_str_strip_spaces(line);
1177 split = xbt_str_split(line, " ");
1179 if(strncmp(xbt_dynar_get_as(split, 1, char *), "DW_AT_", 6) != 0)
1182 node_type = xbt_dynar_get_as(split, 1, char *);
1184 if(strcmp(node_type, "DW_AT_abstract_origin:") == 0){
1186 origin = xbt_dynar_get_as(split, 2, char *);
1187 xbt_str_ltrim(origin, "<0x");
1188 xbt_str_rtrim(origin, ">");
1190 subprogram_name = (char *)xbt_dict_get_or_null(subprograms_origin, origin);
1191 subroutine_frame = xbt_dict_get_or_null(*all_variables, subprogram_name);
1193 }else if(strcmp(node_type, "DW_AT_low_pc") == 0){
1195 subroutine_frame->low_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1197 }else if(strcmp(node_type, "DW_AT_high_pc") == 0){
1199 subroutine_frame->high_pc = (void *)strtoul(xbt_dynar_get_as(split, 3, char *), NULL, 16);
1202 read = getline(&line, &n, fp);
1208 read = getline(&line, &n, fp);
1214 xbt_dynar_free(&split);
1221 static dw_location_t get_location(xbt_dict_t location_list, char *expr){
1223 dw_location_t loc = xbt_new0(s_dw_location_t, 1);
1225 if(location_list != NULL){
1227 char *key = bprintf("%d", (int)strtoul(expr, NULL, 16));
1228 loc->type = e_dw_loclist;
1229 loc->location.loclist = (xbt_dynar_t)xbt_dict_get_or_null(location_list, key);
1231 XBT_INFO("Key not found in loclist");
1237 char *tok = NULL, *tok2 = NULL;
1239 xbt_dynar_t tokens1 = xbt_str_split(expr, ";");
1240 xbt_dynar_t tokens2;
1242 loc->type = e_dw_compose;
1243 loc->location.compose = xbt_dynar_new(sizeof(dw_location_t), NULL);
1245 while(cursor < xbt_dynar_length(tokens1)){
1247 tok = xbt_dynar_get_as(tokens1, cursor, char*);
1248 tokens2 = xbt_str_split(tok, " ");
1249 tok2 = xbt_dynar_get_as(tokens2, 0, char*);
1251 if(strncmp(tok2, "DW_OP_reg", 9) == 0){
1252 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1253 new_element->type = e_dw_register;
1254 new_element->location.reg = atoi(strtok(tok2, "DW_OP_reg"));
1255 xbt_dynar_push(loc->location.compose, &new_element);
1256 }else if(strcmp(tok2, "DW_OP_fbreg:") == 0){
1257 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1258 new_element->type = e_dw_fbregister_op;
1259 new_element->location.fbreg_op = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1260 xbt_dynar_push(loc->location.compose, &new_element);
1261 }else if(strncmp(tok2, "DW_OP_breg", 10) == 0){
1262 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1263 new_element->type = e_dw_bregister_op;
1264 new_element->location.breg_op.reg = atoi(strtok(tok2, "DW_OP_breg"));
1265 new_element->location.breg_op.offset = atoi(xbt_dynar_get_as(tokens2, 2, char*));
1266 xbt_dynar_push(loc->location.compose, &new_element);
1267 }else if(strncmp(tok2, "DW_OP_lit", 9) == 0){
1268 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1269 new_element->type = e_dw_lit;
1270 new_element->location.lit = atoi(strtok(tok2, "DW_OP_lit"));
1271 xbt_dynar_push(loc->location.compose, &new_element);
1272 }else if(strcmp(tok2, "DW_OP_piece:") == 0){
1273 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1274 new_element->type = e_dw_piece;
1275 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1276 /*if(strlen(xbt_dynar_get_as(tokens2, 1, char*)) > 1)
1277 new_element->location.piece = atoi(xbt_dynar_get_as(tokens2, 1, char*));
1279 new_element->location.piece = xbt_dynar_get_as(tokens2, 1, char*)[0] - '0';*/
1280 xbt_dynar_push(loc->location.compose, &new_element);
1281 }else if(strcmp(tok2, "DW_OP_plus_uconst:") == 0){
1282 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1283 new_element->type = e_dw_plus_uconst;
1284 new_element->location.plus_uconst = atoi(xbt_dynar_get_as(tokens2, 1, char *));
1285 xbt_dynar_push(loc->location.compose, &new_element);
1286 }else if(strcmp(tok, "DW_OP_abs") == 0 ||
1287 strcmp(tok, "DW_OP_and") == 0 ||
1288 strcmp(tok, "DW_OP_div") == 0 ||
1289 strcmp(tok, "DW_OP_minus") == 0 ||
1290 strcmp(tok, "DW_OP_mod") == 0 ||
1291 strcmp(tok, "DW_OP_mul") == 0 ||
1292 strcmp(tok, "DW_OP_neg") == 0 ||
1293 strcmp(tok, "DW_OP_not") == 0 ||
1294 strcmp(tok, "DW_OP_or") == 0 ||
1295 strcmp(tok, "DW_OP_plus") == 0){
1296 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1297 new_element->type = e_dw_arithmetic;
1298 new_element->location.arithmetic = strdup(strtok(tok2, "DW_OP_"));
1299 xbt_dynar_push(loc->location.compose, &new_element);
1300 }else if(strcmp(tok, "DW_OP_stack_value") == 0){
1301 }else if(strcmp(tok2, "DW_OP_deref_size:") == 0){
1302 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1303 new_element->type = e_dw_deref;
1304 new_element->location.deref_size = (unsigned int short) atoi(xbt_dynar_get_as(tokens2, 1, char*));
1305 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1306 new_element->location.deref_size = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1308 new_element->location.deref_size = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1309 xbt_dynar_push(loc->location.compose, &new_element);
1310 }else if(strcmp(tok, "DW_OP_deref") == 0){
1311 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1312 new_element->type = e_dw_deref;
1313 new_element->location.deref_size = sizeof(void *);
1314 xbt_dynar_push(loc->location.compose, &new_element);
1315 }else if(strcmp(tok2, "DW_OP_constu:") == 0){
1316 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1317 new_element->type = e_dw_uconstant;
1318 new_element->location.uconstant.bytes = 1;
1319 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1320 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1321 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens, cursor, char*)));
1323 new_element->location.uconstant.value = (unsigned long int)(xbt_dynar_get_as(tokens, cursor, char*)[0] - '0');*/
1324 xbt_dynar_push(loc->location.compose, &new_element);
1325 }else if(strcmp(tok2, "DW_OP_consts:") == 0){
1326 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1327 new_element->type = e_dw_sconstant;
1328 new_element->location.sconstant.bytes = 1;
1329 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1330 xbt_dynar_push(loc->location.compose, &new_element);
1331 }else if(strcmp(tok2, "DW_OP_const1u:") == 0 ||
1332 strcmp(tok2, "DW_OP_const2u:") == 0 ||
1333 strcmp(tok2, "DW_OP_const4u:") == 0 ||
1334 strcmp(tok2, "DW_OP_const8u:") == 0){
1335 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1336 new_element->type = e_dw_uconstant;
1337 new_element->location.uconstant.bytes = tok2[11] - '0';
1338 new_element->location.uconstant.value = (unsigned long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1339 /*if(strlen(xbt_dynar_get_as(tokens, ++cursor, char*)) > 1)
1340 new_element->location.constant.value = atoi(xbt_dynar_get_as(tokens, cursor, char*));
1342 new_element->location.constant.value = xbt_dynar_get_as(tokens, cursor, char*)[0] - '0';*/
1343 xbt_dynar_push(loc->location.compose, &new_element);
1344 }else if(strcmp(tok, "DW_OP_const1s") == 0 ||
1345 strcmp(tok, "DW_OP_const2s") == 0 ||
1346 strcmp(tok, "DW_OP_const4s") == 0 ||
1347 strcmp(tok, "DW_OP_const8s") == 0){
1348 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1349 new_element->type = e_dw_sconstant;
1350 new_element->location.sconstant.bytes = tok2[11] - '0';
1351 new_element->location.sconstant.value = (long int)(atoi(xbt_dynar_get_as(tokens2, 1, char*)));
1352 xbt_dynar_push(loc->location.compose, &new_element);
1354 dw_location_t new_element = xbt_new0(s_dw_location_t, 1);
1355 new_element->type = e_dw_unsupported;
1356 xbt_dynar_push(loc->location.compose, &new_element);
1360 xbt_dynar_free(&tokens2);
1364 xbt_dynar_free(&tokens1);
1373 void print_local_variables(xbt_dict_t list){
1375 dw_location_entry_t entry;
1376 dw_location_t location_entry;
1377 unsigned int cursor3 = 0, cursor4 = 0;
1378 xbt_dict_cursor_t cursor = 0, cursor2 = 0;
1380 char *frame_name, *variable_name;
1381 dw_frame_t current_frame;
1382 dw_local_variable_t current_variable;
1384 xbt_dict_foreach(list, cursor, frame_name, current_frame){
1385 fprintf(stderr, "Frame name : %s\n", current_frame->name);
1386 fprintf(stderr, "Location type : %d\n", current_frame->frame_base->type);
1387 xbt_dict_foreach((xbt_dict_t)current_frame->variables, cursor2, variable_name, current_variable){
1388 fprintf(stderr, "Name : %s\n", current_variable->name);
1389 if(current_variable->location == NULL)
1391 fprintf(stderr, "Location type : %d\n", current_variable->location->type);
1392 switch(current_variable->location->type){
1394 xbt_dynar_foreach(current_variable->location->location.loclist, cursor3, entry){
1395 fprintf(stderr, "Lowpc : %lx, Highpc : %lx,", entry->lowpc, entry->highpc);
1396 switch(entry->location->type){
1397 case e_dw_register :
1398 fprintf(stderr, " Location : in register %d\n", entry->location->location.reg);
1400 case e_dw_bregister_op:
1401 fprintf(stderr, " Location : Add %d to the value in register %d\n", entry->location->location.breg_op.offset, entry->location->location.breg_op.reg);
1404 fprintf(stderr, "Value already kwnown : %d\n", entry->location->location.lit);
1406 case e_dw_fbregister_op:
1407 fprintf(stderr, " Location : %d bytes from logical frame pointer\n", entry->location->location.fbreg_op);
1410 fprintf(stderr, " Location :\n");
1411 xbt_dynar_foreach(entry->location->location.compose, cursor4, location_entry){
1412 switch(location_entry->type){
1413 case e_dw_register :
1414 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1416 case e_dw_bregister_op:
1417 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);
1420 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1422 case e_dw_fbregister_op:
1423 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1426 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);
1428 case e_dw_arithmetic :
1429 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1432 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1434 case e_dw_uconstant :
1435 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1437 case e_dw_sconstant :
1438 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1441 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1447 fprintf(stderr, "Location type not supported\n");
1454 fprintf(stderr, "Location :\n");
1455 xbt_dynar_foreach(current_variable->location->location.compose, cursor4, location_entry){
1456 switch(location_entry->type){
1457 case e_dw_register :
1458 fprintf(stderr, " %d) in register %d\n", cursor4 + 1, location_entry->location.reg);
1460 case e_dw_bregister_op:
1461 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);
1464 fprintf(stderr, "%d) Value already kwnown : %d\n", cursor4 + 1, location_entry->location.lit);
1466 case e_dw_fbregister_op:
1467 fprintf(stderr, " %d) %d bytes from logical frame pointer\n", cursor4 + 1, location_entry->location.fbreg_op);
1470 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);
1472 case e_dw_arithmetic :
1473 fprintf(stderr, "%d) arithmetic operation : %s\n", cursor4 + 1, location_entry->location.arithmetic);
1476 fprintf(stderr, "%d) The %d byte(s) previous value\n", cursor4 + 1, location_entry->location.piece);
1478 case e_dw_uconstant :
1479 fprintf(stderr, "%d) Unsigned constant %lu\n", cursor4 + 1, location_entry->location.uconstant.value);
1481 case e_dw_sconstant :
1482 fprintf(stderr, "%d) Signed constant %lu\n", cursor4 + 1, location_entry->location.sconstant.value);
1485 fprintf(stderr, "%d) Location type not supported\n", cursor4 + 1);
1491 fprintf(stderr, "Location type not supported\n");