1 /* Copyright (c) 2008-2014. The SimGrid Team.
2 * All rights reserved. */
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. */
11 #include <sys/types.h>
13 #include "simgrid_config.h"
14 #include <sys/types.h>
16 #include <xbt/mmalloc.h>
19 #include "xbt/mmalloc/mmprivate.h"
22 #include <simgrid/simix.h>
23 #include "simix/popping_private.h"
24 #include "simix/smx_private.h"
26 #include "mc_forward.h"
28 #include "mc_mmalloc.h" // std_heap
29 #include "mc_memory_map.h"
30 #include "AddressSpace.hpp"
31 #include "mc_protocol.h"
33 typedef int mc_process_flags_t;
34 #define MC_PROCESS_NO_FLAG 0
35 #define MC_PROCESS_SELF_FLAG 1
37 // Those flags are used to track down which cached information
38 // is still up to date and which information needs to be updated.
39 typedef int mc_process_cache_flags_t;
40 #define MC_PROCESS_CACHE_FLAG_NONE 0
41 #define MC_PROCESS_CACHE_FLAG_HEAP 1
42 #define MC_PROCESS_CACHE_FLAG_MALLOC_INFO 2
43 #define MC_PROCESS_CACHE_FLAG_SIMIX_PROCESSES 4
45 typedef struct s_mc_smx_process_info s_mc_smx_process_info_t, *mc_smx_process_info_t;
50 /** Representation of a process
52 class Process : public AddressSpace {
54 Process(pid_t pid, int sockfd);
56 const void* read_bytes(void* buffer, std::size_t size,
57 std::uint64_t address, int process_index = ProcessIndexAny,
58 ReadMode mode = Normal) override;
59 public: // to be private
60 mc_process_flags_t process_flags;
65 memory_map_t memory_map;
66 void *maestro_stack_start, *maestro_stack_end;
67 mc_object_info_t libsimgrid_info;
68 mc_object_info_t binary_info;
69 mc_object_info_t* object_infos;
70 size_t object_infos_size;
73 /** Copy of `simix_global->process_list`
77 xbt_dynar_t smx_process_infos;
79 /** Copy of `simix_global->process_to_destroy`
83 xbt_dynar_t smx_old_process_infos;
85 /** State of the cache (which variables are up to date) */
86 mc_process_cache_flags_t cache_flags;
88 /** Address of the heap structure in the MCed process. */
91 /** Copy of the heap structure of the process
93 * This is refreshed with the `MC_process_refresh` call.
94 * This is not used if the process is the current one:
95 * use `MC_process_get_heap_info` in order to use it.
99 /** Copy of the allocation info structure
101 * This is refreshed with the `MC_process_refresh` call.
102 * This is not used if the process is the current one:
103 * use `MC_process_get_malloc_info` in order to use it.
105 malloc_info* heap_info;
107 // ***** Libunwind-data
109 /** Full-featured MC-aware libunwind address space for the process
111 * This address space is using a mc_unw_context_t
112 * (with mc_process_t/mc_address_space_t and unw_context_t).
114 unw_addr_space_t unw_addr_space;
116 /** Underlying libunwind addres-space
118 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
119 * operations of the native MC address space is currently delegated
120 * to this address space (either the local or a ptrace unwinder).
122 unw_addr_space_t unw_underlying_addr_space;
124 /** The corresponding context
126 void* unw_underlying_context;
128 xbt_dynar_t checkpoint_ignore;
136 int MC_process_vm_open(pid_t pid, int flags);
138 /** Refresh the information about the process
140 * Do not use direclty, this is used by the getters when appropriate
141 * in order to have fresh data.
143 XBT_INTERNAL void MC_process_refresh_heap(mc_process_t process);
145 /** Refresh the information about the process
147 * Do not use direclty, this is used by the getters when appropriate
148 * in order to have fresh data.
150 XBT_INTERNAL void MC_process_refresh_malloc_info(mc_process_t process);
153 bool MC_process_is_self(mc_process_t process)
155 return process->process_flags & MC_PROCESS_SELF_FLAG;
158 /* Process memory access: */
161 const void* MC_process_read(mc_process_t process,
162 simgrid::mc::AddressSpace::ReadMode mode,
163 void* local, const void* remote, size_t len,
166 return process->read_bytes(local, len, (std::uint64_t)remote, process_index, mode);
169 // Simplified versions/wrappers (whould be moved in mc_address_space):
170 XBT_INTERNAL const void* MC_process_read_simple(mc_process_t process,
171 void* local, const void* remote, size_t len);
172 XBT_INTERNAL const void* MC_process_read_dynar_element(mc_process_t process,
173 void* local, const void* remote_dynar, size_t i, size_t len);
174 XBT_INTERNAL unsigned long MC_process_read_dynar_length(mc_process_t process,
175 const void* remote_dynar);
177 /** Write data to a process memory
179 * @param process the process
180 * @param local local memory address (source)
181 * @param remote target process memory address (target)
182 * @param len data size
184 XBT_INTERNAL void MC_process_write(mc_process_t process,
185 const void* local, void* remote, size_t len);
187 XBT_INTERNAL void MC_process_clear_memory(mc_process_t process,
188 void* remote, size_t len);
190 /* Functions, variables of the process: */
192 XBT_INTERNAL mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
193 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
194 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
196 XBT_INTERNAL dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
198 XBT_INTERNAL void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size);
199 XBT_INTERNAL char* MC_process_read_string(mc_process_t, void* address);
201 static inline xbt_mheap_t MC_process_get_heap(mc_process_t process)
203 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
204 MC_process_refresh_heap(process);
205 return process->heap;
208 static inline malloc_info* MC_process_get_malloc_info(mc_process_t process)
210 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
211 MC_process_refresh_malloc_info(process);
212 return process->heap_info;
215 /** Find (one occurence of) the named variable definition
217 XBT_INTERNAL dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
219 XBT_INTERNAL void MC_invalidate_cache(void);