Logo AND Algorithmique Numérique Distribuée

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