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_set_current_heap(mc_heap);
145 process->heap = malloc(sizeof(struct mdesc));
146 mmalloc_set_current_heap(oldheap);
148 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
149 process->heap, process->heap_address, sizeof(struct mdesc),
150 MC_PROCESS_INDEX_DISABLED
154 void MC_process_refresh_malloc_info(mc_process_t process)
156 assert(!MC_process_is_self(process));
157 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
158 MC_process_refresh_heap(process);
159 // Refresh process->heapinfo:
160 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
162 xbt_mheap_t heap = mmalloc_set_current_heap(mc_heap);
163 process->heap_info = (malloc_info*) realloc(process->heap_info,
164 malloc_info_bytesize);
165 mmalloc_set_current_heap(heap);
167 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
169 process->heap->heapinfo, malloc_info_bytesize,
170 MC_PROCESS_INDEX_DISABLED);
173 #define SO_RE "\\.so[\\.0-9]*$"
174 #define VERSION_RE "-[\\.0-9]*$"
176 const char* FILTERED_LIBS[] = {
198 static bool MC_is_simgrid_lib(const char* libname)
200 return !strcmp(libname, "libsimgrid");
203 static bool MC_is_filtered_lib(const char* libname)
205 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
208 if (strcmp(libname, FILTERED_LIBS[i])==0)
213 struct s_mc_memory_map_re {
218 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
219 const char* map_basename = basename((char*) pathname);
222 if(regexec(&res->so_re, map_basename, 1, &match, 0))
225 char* libname = strndup(map_basename, match.rm_so);
227 // Strip the version suffix:
228 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
229 char* temp = libname;
230 libname = strndup(temp, match.rm_so);
237 /** @brief Finds the range of the different memory segments and binary paths */
238 static void MC_process_init_memory_map_info(mc_process_t process)
240 XBT_DEBUG("Get debug information ...");
241 process->maestro_stack_start = NULL;
242 process->maestro_stack_end = NULL;
243 process->object_infos = NULL;
244 process->object_infos_size = 0;
245 process->binary_info = NULL;
246 process->libsimgrid_info = NULL;
248 struct s_mc_memory_map_re res;
250 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
251 xbt_die(".so regexp did not compile");
253 memory_map_t maps = process->memory_map;
255 const char* current_name = NULL;
258 for (i=0; i < maps->mapsize; i++) {
259 map_region_t reg = &(maps->regions[i]);
260 const char* pathname = maps->regions[i].pathname;
263 if (maps->regions[i].pathname == NULL) {
268 // [stack], [vvar], [vsyscall], [vdso] ...
269 if (pathname[0] == '[') {
270 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
271 process->maestro_stack_start = reg->start_addr;
272 process->maestro_stack_end = reg->end_addr;
278 if (current_name && strcmp(current_name, pathname)==0)
281 current_name = pathname;
282 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
285 const bool is_executable = !i;
286 char* libname = NULL;
287 if (!is_executable) {
288 libname = MC_get_lib_name(pathname, &res);
291 if (MC_is_filtered_lib(libname)) {
297 mc_object_info_t info =
298 MC_find_object_info(process->memory_map, pathname, is_executable);
299 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
300 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
301 process->object_infos[process->object_infos_size] = info;
302 process->object_infos_size++;
304 process->binary_info = info;
305 else if (libname && MC_is_simgrid_lib(libname))
306 process->libsimgrid_info = info;
311 regfree(&res.version_re);
313 // Resolve time (including accress differents objects):
314 for (i=0; i!=process->object_infos_size; ++i)
315 MC_post_process_object_info(process, process->object_infos[i]);
317 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
318 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
320 XBT_DEBUG("Get debug information done !");
323 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
326 for (i = 0; i != process->object_infos_size; ++i) {
327 if (addr >= (void *) process->object_infos[i]->start
328 && addr <= (void *) process->object_infos[i]->end) {
329 return process->object_infos[i];
335 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
338 for (i = 0; i != process->object_infos_size; ++i) {
339 if (addr >= (void *) process->object_infos[i]->start_exec
340 && addr <= (void *) process->object_infos[i]->end_exec) {
341 return process->object_infos[i];
347 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
350 for (i = 0; i != process->object_infos_size; ++i) {
351 if (addr >= (void *) process->object_infos[i]->start_rw
352 && addr <= (void *) process->object_infos[i]->end_rw) {
353 return process->object_infos[i];
359 // Functions, variables…
361 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
363 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
367 return MC_file_object_info_find_function(info, ip);
370 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
372 const size_t n = process->object_infos_size;
374 for (i=0; i!=n; ++i) {
375 mc_object_info_t info =process->object_infos[i];
376 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
383 // ***** Memory access
385 int MC_process_vm_open(pid_t pid, int flags)
387 const size_t buffer_size = 30;
388 char buffer[buffer_size];
389 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
390 if (res < 0 || res >= buffer_size) {
391 errno = ENAMETOOLONG;
394 return open(buffer, flags);
397 static void MC_process_open_memory_file(mc_process_t process)
399 if (MC_process_is_self(process) || process->memory_file >= 0)
402 int fd = MC_process_vm_open(process->pid, O_RDWR);
404 xbt_die("Could not open file for process virtual address space");
405 process->memory_file = fd;
408 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
410 char* buffer = (char*) buf;
411 ssize_t real_count = count;
413 ssize_t res = pread(fd, buffer, count, offset);
420 } else if (errno != EINTR) {
427 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
429 const char* buffer = (const char*) buf;
430 ssize_t real_count = count;
432 ssize_t res = pwrite(fd, buffer, count, offset);
439 } else if (errno != EINTR) {
446 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
447 void* local, const void* remote, size_t len,
450 if (process_index != MC_PROCESS_INDEX_DISABLED) {
451 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
452 // Segment overlap is not handled.
453 if (MC_object_info_is_privatized(info)) {
454 if (process_index < 0)
455 xbt_die("Missing process index");
456 // Address translation in the privaization segment:
457 size_t offset = (const char*) remote - info->start_rw;
458 remote = (const char*) remote - offset;
462 if (MC_process_is_self(process)) {
463 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
466 memcpy(local, remote, len);
470 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
471 xbt_die("Read from process %lli failed", (long long) process->pid);
476 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
478 if (MC_process_is_self(process)) {
479 memcpy(remote, local, len);
481 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
482 xbt_die("Write to process %lli failed", (long long) process->pid);
486 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
487 static const void* zero_buffer;
488 static const int zero_buffer_size = 10 * 4096;
490 static void MC_zero_buffer_init(void)
492 int fd = open("/dev/zero", O_RDONLY);
494 xbt_die("Could not open /dev/zero");
495 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
496 if (zero_buffer == MAP_FAILED)
497 xbt_die("Could not map the zero buffer");
501 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
503 if (MC_process_is_self(process)) {
504 memset(remote, 0, len);
506 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
508 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
509 MC_process_write(process, zero_buffer, remote, s);
510 remote = (char*) remote + s;