1 /* Copyright (c) 2014-2015. 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. */
13 #include <sys/types.h>
17 #include <sys/mman.h> // PROT_*
23 #include <libunwind.h>
24 #include <libunwind-ptrace.h>
26 #include <xbt/mmalloc.h>
28 #include "mc_process.h"
29 #include "mc_object_info.h"
30 #include "AddressSpace.hpp"
32 #include "mc_snapshot.h"
33 #include "mc_ignore.h"
35 #include "mc_server.h"
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
40 "MC process information");
42 static void MC_process_init_memory_map_info(mc_process_t process);
43 static void MC_process_open_memory_file(mc_process_t process);
45 // ***** Destructor callbacks
47 // ***** mc_address_space methods for mc_process
49 static mc_process_t MC_process_get_process(mc_process_t p) {
60 Process::Process(pid_t pid, int sockfd)
62 Process* process = this;
64 process->process_flags = MC_PROCESS_NO_FLAG;
65 process->socket = sockfd;
68 process->process_flags |= MC_PROCESS_SELF_FLAG;
69 process->running = true;
71 process->memory_map = MC_get_memory_map(pid);
72 process->memory_file = -1;
73 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
75 process->heap_info = NULL;
76 MC_process_init_memory_map_info(process);
77 MC_process_open_memory_file(process);
79 // Read std_heap (is a struct mdesc*):
80 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process,
81 "__mmalloc_default_mdp");
83 xbt_die("No heap information in the target process");
84 if(!std_heap_var->address)
85 xbt_die("No constant address for this variable");
86 MC_process_read(process, simgrid::mc::AddressSpace::Normal,
87 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
88 simgrid::mc::ProcessIndexDisabled);
90 process->smx_process_infos = MC_smx_process_info_list_new();
91 process->smx_old_process_infos = MC_smx_process_info_list_new();
93 process->checkpoint_ignore = MC_checkpoint_ignore_new();
95 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
96 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
97 process->unw_underlying_addr_space = unw_local_addr_space;
98 process->unw_underlying_context = NULL;
100 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
101 process->unw_underlying_context = _UPT_create(pid);
107 Process* process = this;
109 process->process_flags = MC_PROCESS_NO_FLAG;
112 MC_free_memory_map(process->memory_map);
113 process->memory_map = NULL;
115 process->maestro_stack_start = NULL;
116 process->maestro_stack_end = NULL;
118 xbt_dynar_free(&process->checkpoint_ignore);
120 xbt_dynar_free(&process->smx_process_infos);
121 xbt_dynar_free(&process->smx_old_process_infos);
124 for (i=0; i!=process->object_infos_size; ++i) {
125 MC_free_object_info(&process->object_infos[i]);
127 free(process->object_infos);
128 process->object_infos = NULL;
129 process->object_infos_size = 0;
130 if (process->memory_file >= 0) {
131 close(process->memory_file);
134 if (process->unw_underlying_addr_space != unw_local_addr_space) {
135 unw_destroy_addr_space(process->unw_underlying_addr_space);
136 _UPT_destroy(process->unw_underlying_context);
138 process->unw_underlying_context = NULL;
139 process->unw_underlying_addr_space = NULL;
141 unw_destroy_addr_space(process->unw_addr_space);
142 process->unw_addr_space = NULL;
144 process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
147 process->heap = NULL;
149 free(process->heap_info);
150 process->heap_info = NULL;
158 void MC_process_refresh_heap(mc_process_t process)
160 xbt_assert(mc_mode == MC_MODE_SERVER);
161 xbt_assert(!MC_process_is_self(process));
162 // Read/dereference/refresh the std_heap pointer:
163 if (!process->heap) {
164 process->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
166 MC_process_read(process, simgrid::mc::AddressSpace::Normal,
167 process->heap, process->heap_address, sizeof(struct mdesc),
168 simgrid::mc::ProcessIndexDisabled
170 process->cache_flags |= MC_PROCESS_CACHE_FLAG_HEAP;
173 void MC_process_refresh_malloc_info(mc_process_t process)
175 xbt_assert(mc_mode == MC_MODE_SERVER);
176 xbt_assert(!MC_process_is_self(process));
177 if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
178 MC_process_refresh_heap(process);
179 // Refresh process->heapinfo:
180 size_t malloc_info_bytesize =
181 (process->heap->heaplimit + 1) * sizeof(malloc_info);
182 process->heap_info = (malloc_info*) realloc(process->heap_info, malloc_info_bytesize);
183 MC_process_read(process, simgrid::mc::AddressSpace::Normal,
185 process->heap->heapinfo, malloc_info_bytesize,
186 simgrid::mc::ProcessIndexDisabled);
187 process->cache_flags |= MC_PROCESS_CACHE_FLAG_MALLOC_INFO;
190 #define SO_RE "\\.so[\\.0-9]*$"
191 #define VERSION_RE "-[\\.0-9]*$"
193 const char* FILTERED_LIBS[] = {
215 static bool MC_is_simgrid_lib(const char* libname)
217 return !strcmp(libname, "libsimgrid");
220 static bool MC_is_filtered_lib(const char* libname)
222 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
225 if (strcmp(libname, FILTERED_LIBS[i])==0)
230 struct s_mc_memory_map_re {
235 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
236 const char* map_basename = basename((char*) pathname);
239 if(regexec(&res->so_re, map_basename, 1, &match, 0))
242 char* libname = strndup(map_basename, match.rm_so);
244 // Strip the version suffix:
245 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
246 char* temp = libname;
247 libname = strndup(temp, match.rm_so);
254 /** @brief Finds the range of the different memory segments and binary paths */
255 static void MC_process_init_memory_map_info(mc_process_t process)
257 XBT_DEBUG("Get debug information ...");
258 process->maestro_stack_start = NULL;
259 process->maestro_stack_end = NULL;
260 process->object_infos = NULL;
261 process->object_infos_size = 0;
262 process->binary_info = NULL;
263 process->libsimgrid_info = NULL;
265 struct s_mc_memory_map_re res;
267 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
268 xbt_die(".so regexp did not compile");
270 memory_map_t maps = process->memory_map;
272 const char* current_name = NULL;
274 for (ssize_t i=0; i < maps->mapsize; i++) {
275 map_region_t reg = &(maps->regions[i]);
276 const char* pathname = maps->regions[i].pathname;
279 if (maps->regions[i].pathname == NULL) {
284 // [stack], [vvar], [vsyscall], [vdso] ...
285 if (pathname[0] == '[') {
286 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
287 process->maestro_stack_start = reg->start_addr;
288 process->maestro_stack_end = reg->end_addr;
294 if (current_name && strcmp(current_name, pathname)==0)
297 current_name = pathname;
298 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
301 const bool is_executable = !i;
302 char* libname = NULL;
303 if (!is_executable) {
304 libname = MC_get_lib_name(pathname, &res);
307 if (MC_is_filtered_lib(libname)) {
313 mc_object_info_t info =
314 MC_find_object_info(process->memory_map, pathname, is_executable);
315 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
316 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
317 process->object_infos[process->object_infos_size] = info;
318 process->object_infos_size++;
320 process->binary_info = info;
321 else if (libname && MC_is_simgrid_lib(libname))
322 process->libsimgrid_info = info;
327 regfree(&res.version_re);
329 // Resolve time (including accress differents objects):
330 for (size_t i=0; i!=process->object_infos_size; ++i)
331 MC_post_process_object_info(process, process->object_infos[i]);
333 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
334 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
336 XBT_DEBUG("Get debug information done !");
339 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
342 for (i = 0; i != process->object_infos_size; ++i) {
343 if (addr >= (void *) process->object_infos[i]->start
344 && addr <= (void *) process->object_infos[i]->end) {
345 return process->object_infos[i];
351 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
354 for (i = 0; i != process->object_infos_size; ++i) {
355 if (addr >= (void *) process->object_infos[i]->start_exec
356 && addr <= (void *) process->object_infos[i]->end_exec) {
357 return process->object_infos[i];
363 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
366 for (i = 0; i != process->object_infos_size; ++i) {
367 if (addr >= (void *) process->object_infos[i]->start_rw
368 && addr <= (void *) process->object_infos[i]->end_rw) {
369 return process->object_infos[i];
375 // Functions, variables…
377 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
379 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
383 return MC_file_object_info_find_function(info, ip);
386 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
388 const size_t n = process->object_infos_size;
391 // First lookup the variable in the executable shared object.
392 // A global variable used directly by the executable code from a library
393 // is reinstanciated in the executable memory .data/.bss.
394 // We need to look up the variable in the execvutable first.
395 if (process->binary_info) {
396 mc_object_info_t info = process->binary_info;
397 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
402 for (i=0; i!=n; ++i) {
403 mc_object_info_t info =process->object_infos[i];
404 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
412 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
414 dw_variable_t var = MC_process_find_variable_by_name(process, name);
416 xbt_die("No simple location for this variable");
417 if (!var->type->full_type)
418 xbt_die("Partial type for %s, cannot check size", name);
419 if ((size_t) var->type->full_type->byte_size != size)
420 xbt_die("Unexpected size for %s (expected %zi, was %zi)",
421 name, size, (size_t) var->type->full_type->byte_size);
422 MC_process_read(process, simgrid::mc::AddressSpace::Normal, target, var->address, size,
423 simgrid::mc::ProcessIndexAny);
426 char* MC_process_read_string(mc_process_t process, void* address)
430 if (MC_process_is_self(process))
431 return strdup((char*) address);
434 char* res = (char*) malloc(len);
438 ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off);
443 xbt_die("Could not read from from remote process");
446 xbt_die("Could not read string from remote process");
448 void* p = memchr(res + off, '\0', c);
455 res = (char*) realloc(res, len);
460 // ***** Memory access
462 int MC_process_vm_open(pid_t pid, int flags)
464 const size_t buffer_size = 30;
465 char buffer[buffer_size];
466 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
467 if (res < 0 || (size_t) res >= buffer_size) {
468 errno = ENAMETOOLONG;
471 return open(buffer, flags);
474 static void MC_process_open_memory_file(mc_process_t process)
476 if (MC_process_is_self(process) || process->memory_file >= 0)
479 int fd = MC_process_vm_open(process->pid, O_RDWR);
481 xbt_die("Could not open file for process virtual address space");
482 process->memory_file = fd;
485 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
487 char* buffer = (char*) buf;
488 ssize_t real_count = count;
490 ssize_t res = pread(fd, buffer, count, offset);
497 } else if (errno != EINTR) {
504 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
506 const char* buffer = (const char*) buf;
507 ssize_t real_count = count;
509 ssize_t res = pwrite(fd, buffer, count, offset);
516 } else if (errno != EINTR) {
528 const void *Process::read_bytes(void* buffer, std::size_t size,
529 std::uint64_t address, int process_index,
530 AddressSpace::ReadMode mode)
532 if (process_index != simgrid::mc::ProcessIndexDisabled) {
533 mc_object_info_t info = MC_process_find_object_info_rw(this, (void*)address);
534 // Segment overlap is not handled.
535 if (MC_object_info_is_privatized(info)) {
536 if (process_index < 0)
537 xbt_die("Missing process index");
538 // Address translation in the privaization segment:
539 // TODO, fix me (broken)
540 size_t offset = address - (std::uint64_t)info->start_rw;
541 address = address - offset;
545 if (MC_process_is_self(this)) {
546 if (mode == simgrid::mc::AddressSpace::Lazy)
547 return (void*)address;
549 memcpy(buffer, (void*)address, size);
553 if (pread_whole(this->memory_file, buffer, size, (off_t) address) < 0)
554 xbt_die("Read from process %lli failed", (long long) this->pid);
564 const void* MC_process_read_dynar_element(mc_process_t process,
565 void* local, const void* remote_dynar, size_t i, size_t len)
568 MC_process_read_simple(process, &d, remote_dynar, sizeof(d));
570 xbt_die("Out of bound index %zi/%lu", i, d.used);
571 if (len != d.elmsize)
572 xbt_die("Bad size in MC_process_read_dynar_element");
573 MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), len);
577 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
579 if (MC_process_is_self(process)) {
580 memcpy(remote, local, len);
582 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
583 xbt_die("Write to process %lli failed", (long long) process->pid);
587 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar)
592 MC_process_read_simple(process, &res,
593 &((xbt_dynar_t)remote_dynar)->used, sizeof(res));
597 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
598 static const void* zero_buffer;
599 static const size_t zero_buffer_size = 10 * 4096;
601 static void MC_zero_buffer_init(void)
603 int fd = open("/dev/zero", O_RDONLY);
605 xbt_die("Could not open /dev/zero");
606 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
607 if (zero_buffer == MAP_FAILED)
608 xbt_die("Could not map the zero buffer");
612 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
614 if (MC_process_is_self(process)) {
615 memset(remote, 0, len);
617 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
619 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
620 MC_process_write(process, zero_buffer, remote, s);
621 remote = (char*) remote + s;