Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
c06613ededb71c8c85e6751f6bb7ee84e9ed10e7
[simgrid.git] / src / mc / mc_private.h
1 /* Copyright (c) 2007-2014. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #ifndef MC_PRIVATE_H
8 #define MC_PRIVATE_H
9
10 #include "simgrid_config.h"
11 #include <stdio.h>
12 #include <stdbool.h>
13 #ifndef WIN32
14 #include <sys/mman.h>
15 #endif
16 #include <elfutils/libdw.h>
17
18 #include "mc/mc.h"
19 #include "mc/datatypes.h"
20 #include "xbt/fifo.h"
21 #include "xbt/config.h"
22 #include "xbt/function_types.h"
23 #include "xbt/mmalloc.h"
24 #include "../simix/smx_private.h"
25 #include "../xbt/mmalloc/mmprivate.h"
26 #include "xbt/automaton.h"
27 #include "xbt/hash.h"
28 #include "msg/msg.h"
29 #include "msg/datatypes.h"
30 #include "xbt/strbuff.h"
31 #include "xbt/parmap.h"
32 #include "mc_mmu.h"
33 #include "mc_page_store.h"
34
35 SG_BEGIN_DECL()
36
37 typedef struct s_dw_frame s_dw_frame_t, *dw_frame_t;
38 typedef struct s_mc_function_index_item s_mc_function_index_item_t, *mc_function_index_item_t;
39
40 /****************************** Snapshots ***********************************/
41
42 #define NB_REGIONS 3 /* binary data (data + BSS) (type = 2), libsimgrid data (data + BSS) (type = 1), std_heap (type = 0)*/ 
43
44 /** @brief Copy/snapshot of a given memory region
45  *
46  *  Two types of region snapshots exist:
47  *  <ul>
48  *    <li>flat/dense snapshots are a simple copy of the region;</li>
49  *    <li>sparse/per-page snapshots are snaapshots which shared
50  *    identical pages.</li>
51  *  </ul>
52  */
53 typedef struct s_mc_mem_region{
54   /** @brief  Virtual address of the region in the simulated process */
55   void *start_addr;
56
57   /** @brief Permanent virtual address of the region
58    *
59    * This is usually the same address as the simuilated process address.
60    * However, when using SMPI privatization of global variables,
61    * each SMPI process has its own set of global variables stored
62    * at a different virtual address. The scheduler maps those region
63    * on the region of the global variables.
64    *
65    * */
66   void *permanent_addr;
67
68   /** @brief Copy of the snapshot for flat snapshots regions (NULL otherwise) */
69   void *data;
70
71   /** @brief Size of the data region in bytes */
72   size_t size;
73
74   /** @brief Pages indices in the page store for per-page snapshots (NULL otherwise) */
75   size_t* page_numbers;
76
77 } s_mc_mem_region_t, *mc_mem_region_t;
78
79 static inline  __attribute__ ((always_inline))
80 bool mc_region_contain(mc_mem_region_t region, void* p)
81 {
82   return p >= region->start_addr &&
83     p < (void*)((char*) region->start_addr + region->size);
84 }
85
86 /** Ignored data
87  *
88  *  Some parts of the snapshot are ignored by zeroing them out: the real
89  *  values is stored here.
90  * */
91 typedef struct s_mc_snapshot_ignored_data {
92   void* start;
93   size_t size;
94   void* data;
95 } s_mc_snapshot_ignored_data_t, *mc_snapshot_ignored_data_t;
96
97 typedef struct s_mc_snapshot{
98   size_t heap_bytes_used;
99   mc_mem_region_t regions[NB_REGIONS];
100   xbt_dynar_t enabled_processes;
101   mc_mem_region_t* privatization_regions;
102   int privatization_index;
103   size_t *stack_sizes;
104   xbt_dynar_t stacks;
105   xbt_dynar_t to_ignore;
106   uint64_t hash;
107   xbt_dynar_t ignored_data;
108 } s_mc_snapshot_t, *mc_snapshot_t;
109
110 /** @brief Process index used when no process is available
111  *
112  *  The expected behaviour is that if a process index is needed it will fail.
113  * */
114 #define MC_NO_PROCESS_INDEX -1
115
116 /** @brief Process index when any process is suitable
117  *
118  * We could use a special negative value in the future.
119  */
120 #define MC_ANY_PROCESS_INDEX 0
121
122 mc_mem_region_t mc_get_snapshot_region(void* addr, mc_snapshot_t snapshot, int process_index);
123
124 static inline __attribute__ ((always_inline))
125 mc_mem_region_t mc_get_region_hinted(void* addr, mc_snapshot_t snapshot, int process_index, mc_mem_region_t region)
126 {
127   if (mc_region_contain(region, addr))
128     return region;
129   else
130     return mc_get_snapshot_region(addr, snapshot, process_index);
131 }
132
133 /** Information about a given stack frame
134  *
135  */
136 typedef struct s_mc_stack_frame {
137   /** Instruction pointer */
138   unw_word_t ip;
139   /** Stack pointer */
140   unw_word_t sp;
141   unw_word_t frame_base;
142   dw_frame_t frame;
143   char* frame_name;
144   unw_cursor_t unw_cursor;
145 } s_mc_stack_frame_t, *mc_stack_frame_t;
146
147 typedef struct s_mc_snapshot_stack{
148   xbt_dynar_t local_variables;
149   xbt_dynar_t stack_frames; // mc_stack_frame_t
150   int process_index;
151 }s_mc_snapshot_stack_t, *mc_snapshot_stack_t;
152
153 typedef struct s_mc_global_t{
154   mc_snapshot_t snapshot;
155   int raw_mem_set;
156   int prev_pair;
157   char *prev_req;
158   int initial_communications_pattern_done;
159   int comm_deterministic;
160   int send_deterministic;
161 }s_mc_global_t, *mc_global_t;
162
163 typedef struct s_mc_checkpoint_ignore_region{
164   void *addr;
165   size_t size;
166 }s_mc_checkpoint_ignore_region_t, *mc_checkpoint_ignore_region_t;
167
168 static void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot);
169
170 mc_snapshot_t SIMIX_pre_mc_snapshot(smx_simcall_t simcall);
171 mc_snapshot_t MC_take_snapshot(int num_state);
172 void MC_restore_snapshot(mc_snapshot_t);
173 void MC_free_snapshot(mc_snapshot_t);
174
175 int mc_important_snapshot(mc_snapshot_t snapshot);
176
177 size_t* mc_take_page_snapshot_region(void* data, size_t page_count, uint64_t* pagemap, size_t* reference_pages);
178 void mc_free_page_snapshot_region(size_t* pagenos, size_t page_count);
179 void mc_restore_page_snapshot_region(void* start_addr, size_t page_count, size_t* pagenos, uint64_t* pagemap, size_t* reference_pagenos);
180
181 mc_mem_region_t mc_region_new_sparse(int type, void *start_addr, void* data_addr, size_t size, mc_mem_region_t ref_reg);
182 void MC_region_destroy(mc_mem_region_t reg);
183 void mc_region_restore_sparse(mc_mem_region_t reg, mc_mem_region_t ref_reg);
184 void mc_softdirty_reset();
185
186 static inline __attribute__((always_inline))
187 bool mc_snapshot_region_linear(mc_mem_region_t region) {
188   return !region || !region->data;
189 }
190
191 void* mc_snapshot_read_fragmented(void* addr, mc_mem_region_t region, void* target, size_t size);
192
193 void* mc_snapshot_read(void* addr, mc_snapshot_t snapshot, int process_index, void* target, size_t size);
194 int mc_snapshot_region_memcmp(
195   void* addr1, mc_mem_region_t region1,
196   void* addr2, mc_mem_region_t region2, size_t size);
197 int mc_snapshot_memcmp(
198   void* addr1, mc_snapshot_t snapshot1,
199   void* addr2, mc_snapshot_t snapshot2, int process_index, size_t size);
200
201 static void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot, int process_index);
202
203 /** @brief State of the model-checker (global variables for the model checker)
204  *
205  *  Each part of the state of the model chercker represented as a global
206  *  variable prevents some sharing between snapshots and must be ignored.
207  *  By moving as much state as possible in this structure allocated
208  *  on the model-chercker heap, we avoid those issues.
209  */
210 typedef struct s_mc_model_checker {
211   // This is the parent snapshot of the current state:
212   mc_snapshot_t parent_snapshot;
213   mc_pages_store_t pages;
214   int fd_clear_refs;
215   int fd_pagemap;
216 } s_mc_model_checker_t, *mc_model_checker_t;
217
218 extern mc_model_checker_t mc_model_checker;
219
220 extern xbt_dynar_t mc_checkpoint_ignore;
221
222 /********************************* MC Global **********************************/
223
224 extern double *mc_time;
225 extern FILE *dot_output;
226 extern const char* colors[13];
227 extern xbt_parmap_t parmap;
228
229 extern int user_max_depth_reached;
230
231 int MC_deadlock_check(void);
232 void MC_replay(xbt_fifo_t stack, int start);
233 void MC_replay_liveness(xbt_fifo_t stack, int all_stack);
234 void MC_wait_for_requests(void);
235 void MC_show_deadlock(smx_simcall_t req);
236 void MC_show_stack_safety(xbt_fifo_t stack);
237 void MC_dump_stack_safety(xbt_fifo_t stack);
238 int SIMIX_pre_mc_random(smx_simcall_t simcall, int min, int max);
239
240 extern xbt_fifo_t mc_stack;
241 int get_search_interval(xbt_dynar_t list, void *ref, int *min, int *max);
242
243
244 /********************************* Requests ***********************************/
245
246 int MC_request_depend(smx_simcall_t req1, smx_simcall_t req2);
247 char* MC_request_to_string(smx_simcall_t req, int value);
248 unsigned int MC_request_testany_fail(smx_simcall_t req);
249 /*int MC_waitany_is_enabled_by_comm(smx_req_t req, unsigned int comm);*/
250 int MC_request_is_visible(smx_simcall_t req);
251 int MC_request_is_enabled(smx_simcall_t req);
252 int MC_request_is_enabled_by_idx(smx_simcall_t req, unsigned int idx);
253 int MC_process_is_enabled(smx_process_t process);
254 char *MC_request_get_dot_output(smx_simcall_t req, int value);
255
256
257 /******************************** States **************************************/
258
259 extern mc_global_t initial_global_state;
260
261 /* Possible exploration status of a process in a state */
262 typedef enum {
263   MC_NOT_INTERLEAVE=0,      /* Do not interleave (do not execute) */
264   MC_INTERLEAVE,            /* Interleave the process (one or more request) */
265   MC_MORE_INTERLEAVE,       /* Interleave twice the process (for mc_random simcall) */
266   MC_DONE                   /* Already interleaved */
267 } e_mc_process_state_t;
268
269 /* On every state, each process has an entry of the following type */
270 typedef struct mc_procstate{
271   e_mc_process_state_t state;       /* Exploration control information */
272   unsigned int interleave_count;    /* Number of times that the process was
273                                        interleaved */
274 } s_mc_procstate_t, *mc_procstate_t;
275
276 /* An exploration state is composed of: */
277 typedef struct mc_state {
278   unsigned long max_pid;            /* Maximum pid at state's creation time */
279   mc_procstate_t proc_status;       /* State's exploration status by process */
280   s_smx_action_t internal_comm;     /* To be referenced by the internal_req */
281   s_smx_simcall_t internal_req;         /* Internal translation of request */
282   s_smx_simcall_t executed_req;         /* The executed request of the state */
283   int req_num;                      /* The request number (in the case of a
284                                        multi-request like waitany ) */
285   mc_snapshot_t system_state;      /* Snapshot of system state */
286   int num;
287 } s_mc_state_t, *mc_state_t;
288
289 mc_state_t MC_state_new(void);
290 void MC_state_delete(mc_state_t state);
291 void MC_state_interleave_process(mc_state_t state, smx_process_t process);
292 unsigned int MC_state_interleave_size(mc_state_t state);
293 int MC_state_process_is_done(mc_state_t state, smx_process_t process);
294 void MC_state_set_executed_request(mc_state_t state, smx_simcall_t req, int value);
295 smx_simcall_t MC_state_get_executed_request(mc_state_t state, int *value);
296 smx_simcall_t MC_state_get_internal_request(mc_state_t state);
297 smx_simcall_t MC_state_get_request(mc_state_t state, int *value);
298 void MC_state_remove_interleave_process(mc_state_t state, smx_process_t process);
299
300
301 /****************************** Statistics ************************************/
302
303 typedef struct mc_stats {
304   unsigned long state_size;
305   unsigned long visited_states;
306   unsigned long visited_pairs;
307   unsigned long expanded_states;
308   unsigned long expanded_pairs;
309   unsigned long executed_transitions;
310 } s_mc_stats_t, *mc_stats_t;
311
312 extern mc_stats_t mc_stats;
313
314 void MC_print_statistics(mc_stats_t);
315
316
317 /********************************** MEMORY ******************************/
318 /* The possible memory modes for the modelchecker are standard and raw. */
319 /* Normally the system should operate in std, for switching to raw mode */
320 /* you must wrap the code between MC_SET_RAW_MODE and MC_UNSET_RAW_MODE */
321
322 extern void *std_heap;
323 extern void *mc_heap;
324
325
326 /* FIXME: Horrible hack! because the mmalloc library doesn't provide yet of */
327 /* an API to query about the status of a heap, we simply call mmstats and */
328 /* because I now how does structure looks like, then I redefine it here */
329
330 /* struct mstats { */
331 /*   size_t bytes_total;           /\* Total size of the heap. *\/ */
332 /*   size_t chunks_used;           /\* Chunks allocated by the user. *\/ */
333 /*   size_t bytes_used;            /\* Byte total of user-allocated chunks. *\/ */
334 /*   size_t chunks_free;           /\* Chunks in the free list. *\/ */
335 /*   size_t bytes_free;            /\* Byte total of chunks in the free list. *\/ */
336 /* }; */
337
338 #define MC_SET_MC_HEAP    mmalloc_set_current_heap(mc_heap)
339 #define MC_SET_STD_HEAP  mmalloc_set_current_heap(std_heap)
340
341
342 /******************************* MEMORY MAPPINGS ***************************/
343 /* These functions and data structures implements a binary interface for   */
344 /* the proc maps ascii interface                                           */
345
346 /* Each field is defined as documented in proc's manual page  */
347 typedef struct s_map_region {
348
349   void *start_addr;             /* Start address of the map */
350   void *end_addr;               /* End address of the map */
351   int prot;                     /* Memory protection */
352   int flags;                    /* Additional memory flags */
353   void *offset;                 /* Offset in the file/whatever */
354   char dev_major;               /* Major of the device */
355   char dev_minor;               /* Minor of the device */
356   unsigned long inode;          /* Inode in the device */
357   char *pathname;               /* Path name of the mapped file */
358
359 } s_map_region_t;
360
361 typedef struct s_memory_map {
362
363   s_map_region_t *regions;      /* Pointer to an array of regions */
364   int mapsize;                  /* Number of regions in the memory */
365
366 } s_memory_map_t, *memory_map_t;
367
368
369 void MC_init_memory_map_info(void);
370 memory_map_t MC_get_memory_map(void);
371 void MC_free_memory_map(memory_map_t map);
372
373 extern char *libsimgrid_path;
374
375 /********************************** Snapshot comparison **********************************/
376
377 typedef struct s_mc_comparison_times{
378   double nb_processes_comparison_time;
379   double bytes_used_comparison_time;
380   double stacks_sizes_comparison_time;
381   double binary_global_variables_comparison_time;
382   double libsimgrid_global_variables_comparison_time;
383   double heap_comparison_time;
384   double stacks_comparison_time;
385 }s_mc_comparison_times_t, *mc_comparison_times_t;
386
387 extern __thread mc_comparison_times_t mc_comp_times;
388 extern __thread double mc_snapshot_comparison_time;
389
390 int snapshot_compare(void *state1, void *state2);
391 int SIMIX_pre_mc_compare_snapshots(smx_simcall_t simcall, mc_snapshot_t s1, mc_snapshot_t s2);
392 void print_comparison_times(void);
393
394 //#define MC_DEBUG 1
395 #define MC_VERBOSE 1
396
397 /********************************** Safety verification **************************************/
398
399 typedef enum {
400   e_mc_reduce_unset,
401   e_mc_reduce_none,
402   e_mc_reduce_dpor
403 } e_mc_reduce_t;
404
405 extern e_mc_reduce_t mc_reduce_kind;
406 extern xbt_dict_t first_enabled_state;
407
408 void MC_pre_modelcheck_safety(void);
409 void MC_modelcheck_safety(void);
410
411 typedef struct s_mc_visited_state{
412   mc_snapshot_t system_state;
413   size_t heap_bytes_used;
414   int nb_processes;
415   int num;
416   int other_num; // dot_output for
417 }s_mc_visited_state_t, *mc_visited_state_t;
418
419 extern xbt_dynar_t visited_states;
420 mc_visited_state_t is_visited_state(void);
421 void visited_state_free(mc_visited_state_t state);
422 void visited_state_free_voidp(void *s);
423
424 /********************************** Liveness verification **************************************/
425
426 extern xbt_automaton_t _mc_property_automaton;
427
428 typedef struct s_mc_pair{
429   int num;
430   int search_cycle;
431   mc_state_t graph_state; /* System state included */
432   xbt_automaton_state_t automaton_state;
433   xbt_dynar_t atomic_propositions;
434   int requests;
435 }s_mc_pair_t, *mc_pair_t;
436
437 typedef struct s_mc_visited_pair{
438   int num;
439   int other_num; /* Dot output for */
440   int acceptance_pair;
441   mc_state_t graph_state; /* System state included */
442   xbt_automaton_state_t automaton_state;
443   xbt_dynar_t atomic_propositions;
444   size_t heap_bytes_used;
445   int nb_processes;
446   int acceptance_removed;
447   int visited_removed;
448 }s_mc_visited_pair_t, *mc_visited_pair_t;
449
450 mc_pair_t MC_pair_new(void);
451 void MC_pair_delete(mc_pair_t);
452 void mc_pair_free_voidp(void *p);
453 mc_visited_pair_t MC_visited_pair_new(int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
454 void MC_visited_pair_delete(mc_visited_pair_t p);
455
456 void MC_pre_modelcheck_liveness(void);
457 void MC_modelcheck_liveness(void);
458 void MC_show_stack_liveness(xbt_fifo_t stack);
459 void MC_dump_stack_liveness(xbt_fifo_t stack);
460
461 extern xbt_dynar_t visited_pairs;
462 int is_visited_pair(mc_visited_pair_t pair, int pair_num, xbt_automaton_state_t automaton_state, xbt_dynar_t atomic_propositions);
463
464
465 /********************************** Variables with DWARF **********************************/
466
467 #define MC_OBJECT_INFO_EXECUTABLE 1
468
469 struct s_mc_object_info {
470   size_t flags;
471   char* file_name;
472   char *start_exec, *end_exec; // Executable segment
473   char *start_rw, *end_rw; // Read-write segment
474   char *start_ro, *end_ro; // read-only segment
475   xbt_dict_t subprograms; // xbt_dict_t<origin as hexadecimal string, dw_frame_t>
476   xbt_dynar_t global_variables; // xbt_dynar_t<dw_variable_t>
477   xbt_dict_t types; // xbt_dict_t<origin as hexadecimal string, dw_type_t>
478   xbt_dict_t full_types_by_name; // xbt_dict_t<name, dw_type_t> (full defined type only)
479
480   // Here we sort the minimal information for an efficient (and cache-efficient)
481   // lookup of a function given an instruction pointer.
482   // The entries are sorted by low_pc and a binary search can be used to look them up.
483   xbt_dynar_t functions_index;
484 };
485
486 mc_object_info_t MC_new_object_info(void);
487 mc_object_info_t MC_find_object_info(memory_map_t maps, char* name, int executable);
488 void MC_free_object_info(mc_object_info_t* p);
489
490 void MC_dwarf_get_variables(mc_object_info_t info);
491 void MC_dwarf_get_variables_libdw(mc_object_info_t info);
492 const char* MC_dwarf_attrname(int attr);
493 const char* MC_dwarf_tagname(int tag);
494
495 dw_frame_t MC_find_function_by_ip(void* ip);
496 mc_object_info_t MC_ip_find_object_info(void* ip);
497
498 extern mc_object_info_t mc_libsimgrid_info;
499 extern mc_object_info_t mc_binary_info;
500 extern mc_object_info_t mc_object_infos[2];
501 extern size_t mc_object_infos_size;
502
503 void MC_find_object_address(memory_map_t maps, mc_object_info_t result);
504 void MC_post_process_types(mc_object_info_t info);
505 void MC_post_process_object_info(mc_object_info_t info);
506
507 // ***** Expressions
508
509 /** \brief a DWARF expression with optional validity contraints */
510 typedef struct s_mc_expression {
511   size_t size;
512   Dwarf_Op* ops;
513   // Optional validity:
514   void* lowpc, *highpc;
515 } s_mc_expression_t, *mc_expression_t;
516
517 /** A location list (list of location expressions) */
518 typedef struct s_mc_location_list {
519   size_t size;
520   mc_expression_t locations;
521 } s_mc_location_list_t, *mc_location_list_t;
522
523 uintptr_t mc_dwarf_resolve_location(mc_expression_t expression, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot, int process_index);
524 uintptr_t mc_dwarf_resolve_locations(mc_location_list_t locations, mc_object_info_t object_info, unw_cursor_t* c, void* frame_pointer_address, mc_snapshot_t snapshot, int process_index);
525
526 void mc_dwarf_expression_clear(mc_expression_t expression);
527 void mc_dwarf_expression_init(mc_expression_t expression, size_t len, Dwarf_Op* ops);
528
529 void mc_dwarf_location_list_clear(mc_location_list_t list);
530
531 void mc_dwarf_location_list_init_from_expression(mc_location_list_t target, size_t len, Dwarf_Op* ops);
532 void mc_dwarf_location_list_init(mc_location_list_t target, mc_object_info_t info, Dwarf_Die* die, Dwarf_Attribute* attr);
533
534 // ***** Variables and functions
535
536 struct s_dw_type{
537   e_dw_type_type type;
538   Dwarf_Off id; /* Offset in the section (in hexadecimal form) */
539   char *name; /* Name of the type */
540   int byte_size; /* Size in bytes */
541   int element_count; /* Number of elements for array type */
542   char *dw_type_id; /* DW_AT_type id */
543   xbt_dynar_t members; /* if DW_TAG_structure_type, DW_TAG_class_type, DW_TAG_union_type*/
544   int is_pointer_type;
545
546   // Location (for members) is either of:
547   struct s_mc_expression location;
548   int offset;
549
550   dw_type_t subtype; // DW_AT_type
551   dw_type_t full_type; // The same (but more complete) type
552 };
553
554 void* mc_member_resolve(const void* base, dw_type_t type, dw_type_t member, mc_snapshot_t snapshot, int process_index);
555
556 typedef struct s_dw_variable{
557   Dwarf_Off dwarf_offset; /* Global offset of the field. */
558   int global;
559   char *name;
560   char *type_origin;
561   dw_type_t type;
562
563   // Use either of:
564   s_mc_location_list_t locations;
565   void* address;
566
567   size_t start_scope;
568   mc_object_info_t object_info;
569
570 }s_dw_variable_t, *dw_variable_t;
571
572 struct s_dw_frame{
573   int tag;
574   char *name;
575   void *low_pc;
576   void *high_pc;
577   s_mc_location_list_t frame_base;
578   xbt_dynar_t /* <dw_variable_t> */ variables; /* Cannot use dict, there may be several variables with the same name (in different lexical blocks)*/
579   unsigned long int id; /* DWARF offset of the subprogram */
580   xbt_dynar_t /* <dw_frame_t> */ scopes;
581   Dwarf_Off abstract_origin_id;
582   mc_object_info_t object_info;
583 };
584
585 struct s_mc_function_index_item {
586   void* low_pc, *high_pc;
587   dw_frame_t function;
588 };
589
590 void mc_frame_free(dw_frame_t freme);
591
592 void dw_type_free(dw_type_t t);
593 void dw_variable_free(dw_variable_t v);
594 void dw_variable_free_voidp(void *t);
595
596 void MC_dwarf_register_global_variable(mc_object_info_t info, dw_variable_t variable);
597 void MC_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
598 void MC_dwarf_register_non_global_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
599 void MC_dwarf_register_variable(mc_object_info_t info, dw_frame_t frame, dw_variable_t variable);
600
601 /** Find the DWARF offset for this ELF object
602  *
603  *  An offset is applied to address found in DWARF:
604  *
605  *  <ul>
606  *    <li>for an executable obejct, addresses are virtual address
607  *        (there is no offset) i.e. \f$\text{virtual address} = \{dwarf address}\f$;</li>
608  *    <li>for a shared object, the addreses are offset from the begining
609  *        of the shared object (the base address of the mapped shared
610  *        object must be used as offset
611  *        i.e. \f$\text{virtual address} = \text{shared object base address}
612  *             + \text{dwarf address}\f$.</li>
613  *
614  */
615 void* MC_object_base_address(mc_object_info_t info);
616
617 /********************************** DWARF **********************************/
618
619 #define MC_EXPRESSION_STACK_SIZE 64
620
621 #define MC_EXPRESSION_OK 0
622 #define MC_EXPRESSION_E_UNSUPPORTED_OPERATION 1
623 #define MC_EXPRESSION_E_STACK_OVERFLOW 2
624 #define MC_EXPRESSION_E_STACK_UNDERFLOW 3
625 #define MC_EXPRESSION_E_MISSING_STACK_CONTEXT 4
626 #define MC_EXPRESSION_E_MISSING_FRAME_BASE 5
627 #define MC_EXPRESSION_E_NO_BASE_ADDRESS 6
628
629 typedef struct s_mc_expression_state {
630   uintptr_t stack[MC_EXPRESSION_STACK_SIZE];
631   size_t stack_size;
632
633   unw_cursor_t* cursor;
634   void* frame_base;
635   mc_snapshot_t snapshot;
636   mc_object_info_t object_info;
637   int process_index;
638 } s_mc_expression_state_t, *mc_expression_state_t;
639
640 int mc_dwarf_execute_expression(size_t n, const Dwarf_Op* ops, mc_expression_state_t state);
641
642 void* mc_find_frame_base(dw_frame_t frame, mc_object_info_t object_info, unw_cursor_t* unw_cursor);
643
644 /********************************** Miscellaneous **********************************/
645
646 typedef struct s_local_variable{
647   dw_frame_t subprogram;
648   unsigned long ip;
649   char *name;
650   dw_type_t type;
651   void *address;
652   int region;
653 }s_local_variable_t, *local_variable_t;
654
655 /********************************* Communications pattern ***************************/
656
657 typedef struct s_mc_comm_pattern{
658   int num;
659   smx_action_t comm;
660   e_smx_comm_type_t type;
661   unsigned long src_proc;
662   unsigned long dst_proc;
663   const char *src_host;
664   const char *dst_host;
665   char *rdv;
666   ssize_t data_size;
667   void *data;
668 }s_mc_comm_pattern_t, *mc_comm_pattern_t;
669
670 extern xbt_dynar_t initial_communications_pattern;
671 extern xbt_dynar_t communications_pattern;
672 extern xbt_dynar_t incomplete_communications_pattern;
673
674 void get_comm_pattern(xbt_dynar_t communications_pattern, smx_simcall_t request, int call);
675 void complete_comm_pattern(xbt_dynar_t list, smx_action_t comm);
676 void MC_pre_modelcheck_comm_determinism(void);
677 void MC_modelcheck_comm_determinism(void);
678
679 /* *********** Sets *********** */
680
681 typedef struct s_mc_address_set *mc_address_set_t;
682
683 mc_address_set_t mc_address_set_new();
684 void mc_address_set_free(mc_address_set_t* p);
685 void mc_address_add(mc_address_set_t p, const void* value);
686 bool mc_address_test(mc_address_set_t p, const void* value);
687
688 /* *********** Hash *********** */
689
690 /** \brief Hash the current state
691  *  \param num_state number of states
692  *  \param stacks stacks (mc_snapshot_stak_t) used fot the stack unwinding informations
693  *  \result resulting hash
694  * */
695 uint64_t mc_hash_processes_state(int num_state, xbt_dynar_t stacks);
696
697 /* *********** Snapshot *********** */
698
699 static inline __attribute__((always_inline))
700 void* mc_translate_address_region(uintptr_t addr, mc_mem_region_t region)
701 {
702     size_t pageno = mc_page_number(region->start_addr, (void*) addr);
703     size_t snapshot_pageno = region->page_numbers[pageno];
704     const void* snapshot_page = mc_page_store_get_page(mc_model_checker->pages, snapshot_pageno);
705     return (char*) snapshot_page + mc_page_offset((void*) addr);
706 }
707
708 /** \brief Translate a pointer from process address space to snapshot address space
709  *
710  *  The address space contains snapshot of the main/application memory:
711  *  this function finds the address in a given snaphot for a given
712  *  real/application address.
713  *
714  *  For read only memory regions and other regions which are not int the
715  *  snapshot, the address is not changed.
716  *
717  *  \param addr     Application address
718  *  \param snapshot The snapshot of interest (if NULL no translation is done)
719  *  \return         Translated address in the snapshot address space
720  * */
721 static inline __attribute__((always_inline))
722 void* mc_translate_address(uintptr_t addr, mc_snapshot_t snapshot, int process_index)
723 {
724
725   // If not in a process state/clone:
726   if (!snapshot) {
727     return (uintptr_t *) addr;
728   }
729
730   mc_mem_region_t region = mc_get_snapshot_region((void*) addr, snapshot, process_index);
731
732   xbt_assert(mc_region_contain(region, (void*) addr), "Trying to read out of the region boundary.");
733
734   if (!region) {
735     return (void *) addr;
736   }
737
738   // Flat snapshot:
739   else if (region->data) {
740     uintptr_t offset = addr - (uintptr_t) region->start_addr;
741     return (void *) ((uintptr_t) region->data + offset);
742   }
743
744   // Per-page snapshot:
745   else if (region->page_numbers) {
746     return mc_translate_address_region(addr, region);
747   }
748
749   else {
750     xbt_die("No data for this memory region");
751   }
752 }
753
754 static inline __attribute__ ((always_inline))
755   void* mc_snapshot_get_heap_end(mc_snapshot_t snapshot) {
756   if(snapshot==NULL)
757       xbt_die("snapshot is NULL");
758   void** addr = &((xbt_mheap_t)std_heap)->breakval;
759   return mc_snapshot_read_pointer(addr, snapshot, MC_ANY_PROCESS_INDEX);
760 }
761
762 static inline __attribute__ ((always_inline))
763 void* mc_snapshot_read_pointer(void* addr, mc_snapshot_t snapshot, int process_index)
764 {
765   void* res;
766   return *(void**) mc_snapshot_read(addr, snapshot, process_index, &res, sizeof(void*));
767 }
768
769 /** @brief Read memory from a snapshot region
770  *
771  *  @param addr    Process (non-snapshot) address of the data
772  *  @param region  Snapshot memory region where the data is located
773  *  @param target  Buffer to store the value
774  *  @param size    Size of the data to read in bytes
775  *  @return Pointer where the data is located (target buffer of original location)
776  */
777 static inline __attribute__((always_inline))
778 void* mc_snapshot_read_region(void* addr, mc_mem_region_t region, void* target, size_t size)
779 {
780   if (region==NULL)
781     return addr;
782
783   uintptr_t offset = (char*) addr - (char*) region->start_addr;
784
785   xbt_assert(mc_region_contain(region, addr),
786     "Trying to read out of the region boundary.");
787
788   // Linear memory region:
789   if (region->data) {
790     return (char*) region->data + offset;
791   }
792
793   // Fragmented memory region:
794   else if (region->page_numbers) {
795     // Last byte of the region:
796     void* end = (char*) addr + size - 1;
797     if( mc_same_page(addr, end) ) {
798       // The memory is contained in a single page:
799       return mc_translate_address_region((uintptr_t) addr, region);
800     } else {
801       // The memory spans several pages:
802       return mc_snapshot_read_fragmented(addr, region, target, size);
803     }
804   }
805
806   else {
807     xbt_die("No data available for this region");
808   }
809 }
810
811 static inline __attribute__ ((always_inline))
812 void* mc_snapshot_read_pointer_region(void* addr, mc_mem_region_t region)
813 {
814   void* res;
815   return *(void**) mc_snapshot_read_region(addr, region, &res, sizeof(void*));
816 }
817
818 SG_END_DECL()
819
820 #endif
821