11 #include <sys/mman.h> // PROT_*
17 #include <libunwind.h>
18 #include <libunwind-ptrace.h>
20 #include <xbt/mmalloc.h>
22 #include "mc_process.h"
23 #include "mc_object_info.h"
24 #include "mc_address_space.h"
26 #include "mc_snapshot.h"
27 #include "mc_ignore.h"
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
30 "MC process information");
32 static void MC_process_init_memory_map_info(mc_process_t process);
33 static void MC_process_open_memory_file(mc_process_t process);
35 // ***** Destructor callbacks
37 // ***** mc_address_space methods for mc_process
39 static mc_process_t MC_process_get_process(mc_process_t p) {
43 static const s_mc_address_space_class_t mc_process_class = {
44 .read = (void*) &MC_process_read,
45 .get_process = (void*) MC_process_get_process
48 bool MC_is_process(mc_address_space_t p)
50 return p->address_space_class == &mc_process_class;
55 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
57 process->address_space.address_space_class = &mc_process_class;
58 process->process_flags = MC_PROCESS_NO_FLAG;
59 process->socket = sockfd;
62 process->process_flags |= MC_PROCESS_SELF_FLAG;
63 process->running = true;
65 process->memory_map = MC_get_memory_map(pid);
66 process->memory_file = -1;
67 process->cache_flags = 0;
69 process->heap_info = NULL;
70 MC_process_init_memory_map_info(process);
71 MC_process_open_memory_file(process);
73 // Read std_heap (is a struct mdesc*):
74 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
76 xbt_die("No heap information in the target process");
77 if(!std_heap_var->address)
78 xbt_die("No constant address for this variable");
79 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
80 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
81 MC_PROCESS_INDEX_DISABLED);
83 process->checkpoint_ignore = MC_checkpoint_ignore_new();
85 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
86 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
87 process->unw_underlying_addr_space = unw_local_addr_space;
88 process->unw_underlying_context = NULL;
90 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
91 process->unw_underlying_context = _UPT_create(pid);
95 void MC_process_clear(mc_process_t process)
97 process->address_space.address_space_class = NULL;
98 process->process_flags = MC_PROCESS_NO_FLAG;
101 MC_free_memory_map(process->memory_map);
102 process->memory_map = NULL;
104 process->maestro_stack_start = NULL;
105 process->maestro_stack_end = NULL;
107 xbt_dynar_free(&process->checkpoint_ignore);
110 for (i=0; i!=process->object_infos_size; ++i) {
111 MC_free_object_info(&process->object_infos[i]);
113 free(process->object_infos);
114 process->object_infos = NULL;
115 process->object_infos_size = 0;
116 if (process->memory_file >= 0) {
117 close(process->memory_file);
120 if (process->unw_underlying_addr_space != unw_local_addr_space) {
121 unw_destroy_addr_space(process->unw_underlying_addr_space);
122 _UPT_destroy(process->unw_underlying_context);
124 process->unw_underlying_context = NULL;
125 process->unw_underlying_addr_space = NULL;
127 unw_destroy_addr_space(process->unw_addr_space);
128 process->unw_addr_space = NULL;
130 process->cache_flags = 0;
133 process->heap = NULL;
135 free(process->heap_info);
136 process->heap_info = NULL;
139 void MC_process_refresh_heap(mc_process_t process)
141 assert(!MC_process_is_self(process));
142 // Read/dereference/refresh the std_heap pointer:
143 if (!process->heap) {
144 xbt_mheap_t oldheap = mmalloc_get_current_heap();
146 process->heap = malloc(sizeof(struct mdesc));
147 mmalloc_set_current_heap(oldheap);
149 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
150 process->heap, process->heap_address, sizeof(struct mdesc),
151 MC_PROCESS_INDEX_DISABLED
155 void MC_process_refresh_malloc_info(mc_process_t process)
157 assert(!MC_process_is_self(process));
158 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
159 MC_process_refresh_heap(process);
160 // Refresh process->heapinfo:
161 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
163 xbt_mheap_t oldheap = mmalloc_get_current_heap();
165 process->heap_info = (malloc_info*) realloc(process->heap_info,
166 malloc_info_bytesize);
167 mmalloc_set_current_heap(oldheap);
169 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
171 process->heap->heapinfo, malloc_info_bytesize,
172 MC_PROCESS_INDEX_DISABLED);
175 #define SO_RE "\\.so[\\.0-9]*$"
176 #define VERSION_RE "-[\\.0-9]*$"
178 const char* FILTERED_LIBS[] = {
200 static bool MC_is_simgrid_lib(const char* libname)
202 return !strcmp(libname, "libsimgrid");
205 static bool MC_is_filtered_lib(const char* libname)
207 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
210 if (strcmp(libname, FILTERED_LIBS[i])==0)
215 struct s_mc_memory_map_re {
220 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
221 const char* map_basename = basename((char*) pathname);
224 if(regexec(&res->so_re, map_basename, 1, &match, 0))
227 char* libname = strndup(map_basename, match.rm_so);
229 // Strip the version suffix:
230 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
231 char* temp = libname;
232 libname = strndup(temp, match.rm_so);
239 /** @brief Finds the range of the different memory segments and binary paths */
240 static void MC_process_init_memory_map_info(mc_process_t process)
242 XBT_DEBUG("Get debug information ...");
243 process->maestro_stack_start = NULL;
244 process->maestro_stack_end = NULL;
245 process->object_infos = NULL;
246 process->object_infos_size = 0;
247 process->binary_info = NULL;
248 process->libsimgrid_info = NULL;
250 struct s_mc_memory_map_re res;
252 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
253 xbt_die(".so regexp did not compile");
255 memory_map_t maps = process->memory_map;
257 const char* current_name = NULL;
260 for (i=0; i < maps->mapsize; i++) {
261 map_region_t reg = &(maps->regions[i]);
262 const char* pathname = maps->regions[i].pathname;
265 if (maps->regions[i].pathname == NULL) {
270 // [stack], [vvar], [vsyscall], [vdso] ...
271 if (pathname[0] == '[') {
272 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
273 process->maestro_stack_start = reg->start_addr;
274 process->maestro_stack_end = reg->end_addr;
280 if (current_name && strcmp(current_name, pathname)==0)
283 current_name = pathname;
284 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
287 const bool is_executable = !i;
288 char* libname = NULL;
289 if (!is_executable) {
290 libname = MC_get_lib_name(pathname, &res);
293 if (MC_is_filtered_lib(libname)) {
299 mc_object_info_t info =
300 MC_find_object_info(process->memory_map, pathname, is_executable);
301 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
302 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
303 process->object_infos[process->object_infos_size] = info;
304 process->object_infos_size++;
306 process->binary_info = info;
307 else if (libname && MC_is_simgrid_lib(libname))
308 process->libsimgrid_info = info;
313 regfree(&res.version_re);
315 // Resolve time (including accress differents objects):
316 for (i=0; i!=process->object_infos_size; ++i)
317 MC_post_process_object_info(process, process->object_infos[i]);
319 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
320 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
322 XBT_DEBUG("Get debug information done !");
325 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
328 for (i = 0; i != process->object_infos_size; ++i) {
329 if (addr >= (void *) process->object_infos[i]->start
330 && addr <= (void *) process->object_infos[i]->end) {
331 return process->object_infos[i];
337 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
340 for (i = 0; i != process->object_infos_size; ++i) {
341 if (addr >= (void *) process->object_infos[i]->start_exec
342 && addr <= (void *) process->object_infos[i]->end_exec) {
343 return process->object_infos[i];
349 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
352 for (i = 0; i != process->object_infos_size; ++i) {
353 if (addr >= (void *) process->object_infos[i]->start_rw
354 && addr <= (void *) process->object_infos[i]->end_rw) {
355 return process->object_infos[i];
361 // Functions, variables…
363 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
365 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
369 return MC_file_object_info_find_function(info, ip);
372 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
374 const size_t n = process->object_infos_size;
376 for (i=0; i!=n; ++i) {
377 mc_object_info_t info =process->object_infos[i];
378 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
385 // ***** Memory access
387 int MC_process_vm_open(pid_t pid, int flags)
389 const size_t buffer_size = 30;
390 char buffer[buffer_size];
391 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
392 if (res < 0 || res >= buffer_size) {
393 errno = ENAMETOOLONG;
396 return open(buffer, flags);
399 static void MC_process_open_memory_file(mc_process_t process)
401 if (MC_process_is_self(process) || process->memory_file >= 0)
404 int fd = MC_process_vm_open(process->pid, O_RDWR);
406 xbt_die("Could not open file for process virtual address space");
407 process->memory_file = fd;
410 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
412 char* buffer = (char*) buf;
413 ssize_t real_count = count;
415 ssize_t res = pread(fd, buffer, count, offset);
422 } else if (errno != EINTR) {
429 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
431 const char* buffer = (const char*) buf;
432 ssize_t real_count = count;
434 ssize_t res = pwrite(fd, buffer, count, offset);
441 } else if (errno != EINTR) {
448 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
449 void* local, const void* remote, size_t len,
452 if (process_index != MC_PROCESS_INDEX_DISABLED) {
453 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
454 // Segment overlap is not handled.
455 if (MC_object_info_is_privatized(info)) {
456 if (process_index < 0)
457 xbt_die("Missing process index");
458 // Address translation in the privaization segment:
459 size_t offset = (const char*) remote - info->start_rw;
460 remote = (const char*) remote - offset;
464 if (MC_process_is_self(process)) {
465 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
468 memcpy(local, remote, len);
472 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
473 xbt_die("Read from process %lli failed", (long long) process->pid);
478 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
480 if (MC_process_is_self(process)) {
481 memcpy(remote, local, len);
483 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
484 xbt_die("Write to process %lli failed", (long long) process->pid);
488 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
489 static const void* zero_buffer;
490 static const int zero_buffer_size = 10 * 4096;
492 static void MC_zero_buffer_init(void)
494 int fd = open("/dev/zero", O_RDONLY);
496 xbt_die("Could not open /dev/zero");
497 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
498 if (zero_buffer == MAP_FAILED)
499 xbt_die("Could not map the zero buffer");
503 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
505 if (MC_process_is_self(process)) {
506 memset(remote, 0, len);
508 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
510 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
511 MC_process_write(process, zero_buffer, remote, s);
512 remote = (char*) remote + s;