Logo AND Algorithmique Numérique Distribuée

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