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 remote_ptr<void> address, int process_index = ProcessIndexAny,
58 ReadMode mode = Normal) override;
59 xbt_mheap_t get_heap()
61 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
65 malloc_info* get_malloc_info()
67 if (!(this->cache_flags & MC_PROCESS_CACHE_FLAG_MALLOC_INFO))
68 this->refresh_malloc_info();
69 return this->heap_info;
73 return this->process_flags & MC_PROCESS_SELF_FLAG;
77 void refresh_malloc_info();
78 public: // to be private
79 mc_process_flags_t process_flags;
84 memory_map_t memory_map;
85 void *maestro_stack_start, *maestro_stack_end;
86 mc_object_info_t libsimgrid_info;
87 mc_object_info_t binary_info;
88 mc_object_info_t* object_infos;
89 size_t object_infos_size;
92 /** Copy of `simix_global->process_list`
96 xbt_dynar_t smx_process_infos;
98 /** Copy of `simix_global->process_to_destroy`
102 xbt_dynar_t smx_old_process_infos;
104 /** State of the cache (which variables are up to date) */
105 mc_process_cache_flags_t cache_flags;
107 /** Address of the heap structure in the MCed process. */
110 /** Copy of the heap structure of the process
112 * This is refreshed with the `MC_process_refresh` call.
113 * This is not used if the process is the current one:
114 * use `get_heap_info()` in order to use it.
118 /** Copy of the allocation info structure
120 * This is refreshed with the `MC_process_refresh` call.
121 * This is not used if the process is the current one:
122 * use `get_malloc_info()` in order to use it.
124 malloc_info* heap_info;
126 // ***** Libunwind-data
128 /** Full-featured MC-aware libunwind address space for the process
130 * This address space is using a mc_unw_context_t
131 * (with mc_process_t/mc_address_space_t and unw_context_t).
133 unw_addr_space_t unw_addr_space;
135 /** Underlying libunwind addres-space
137 * The `find_proc_info`, `put_unwind_info`, `get_dyn_info_list_addr`
138 * operations of the native MC address space is currently delegated
139 * to this address space (either the local or a ptrace unwinder).
141 unw_addr_space_t unw_underlying_addr_space;
143 /** The corresponding context
145 void* unw_underlying_context;
147 xbt_dynar_t checkpoint_ignore;
155 /** Open a FD to a remote process memory (`/dev/$pid/mem`)
157 int MC_process_vm_open(pid_t pid, int flags);
159 /* Process memory access: */
162 const void* MC_process_read(mc_process_t process,
163 simgrid::mc::AddressSpace::ReadMode mode,
164 void* local, const void* remote, size_t len,
167 return process->read_bytes(local, len, (std::uint64_t)remote, process_index, mode);
170 // Simplified versions/wrappers (whould be moved in mc_address_space):
171 static inline const void* MC_process_read_simple(mc_process_t process,
172 void* local, const void* remote, size_t len)
174 return process->read_bytes(local, len, (std::uint64_t)remote);
177 XBT_INTERNAL const void* MC_process_read_dynar_element(mc_process_t process,
178 void* local, const void* remote_dynar, size_t i, size_t len);
179 XBT_INTERNAL unsigned long MC_process_read_dynar_length(mc_process_t process,
180 const void* remote_dynar);
182 /** Write data to a process memory
184 * @param process the process
185 * @param local local memory address (source)
186 * @param remote target process memory address (target)
187 * @param len data size
189 XBT_INTERNAL void MC_process_write(mc_process_t process,
190 const void* local, void* remote, size_t len);
192 XBT_INTERNAL void MC_process_clear_memory(mc_process_t process,
193 void* remote, size_t len);
195 /* Functions, variables of the process: */
197 XBT_INTERNAL mc_object_info_t MC_process_find_object_info(mc_process_t process, const void* addr);
198 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void* addr);
199 XBT_INTERNAL mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void* addr);
201 XBT_INTERNAL dw_frame_t MC_process_find_function(mc_process_t process, const void* ip);
203 XBT_INTERNAL void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size);
204 XBT_INTERNAL char* MC_process_read_string(mc_process_t, void* address);
206 /** Find (one occurence of) the named variable definition
208 XBT_INTERNAL dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name);
210 XBT_INTERNAL void MC_invalidate_cache(void);