Logo AND Algorithmique Numérique Distribuée

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