11 #include <sys/mman.h> // PROT_*
17 #include <libunwind.h>
18 #include <libunwind-ptrace.h>
20 #include "mc_process.h"
21 #include "mc_object_info.h"
22 #include "mc_address_space.h"
25 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
26 "MC process information");
28 static void MC_process_init_memory_map_info(mc_process_t process);
29 static void MC_process_open_memory_file(mc_process_t process);
31 static mc_process_t MC_process_get_process(mc_process_t p) {
35 static const s_mc_address_space_class_t mc_process_class = {
36 .read = (void*) &MC_process_read,
37 .get_process = (void*) MC_process_get_process
40 bool MC_is_process(mc_address_space_t p)
42 return p->address_space_class == &mc_process_class;
45 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
47 process->address_space.address_space_class = &mc_process_class;
48 process->process_flags = MC_PROCESS_NO_FLAG;
49 process->socket = sockfd;
52 process->process_flags |= MC_PROCESS_SELF_FLAG;
53 process->running = true;
55 process->memory_map = MC_get_memory_map(pid);
56 process->memory_file = -1;
57 process->cache_flags = 0;
59 process->heap_info = NULL;
60 MC_process_init_memory_map_info(process);
61 MC_process_open_memory_file(process);
63 // Read std_heap (is a struct mdesc*):
64 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
66 xbt_die("No heap information in the target process");
67 if(!std_heap_var->address)
68 xbt_die("No constant address for this variable");
69 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
70 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
71 MC_PROCESS_INDEX_DISABLED);
73 process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors , __BYTE_ORDER);
74 if (process->process_flags & MC_PROCESS_SELF_FLAG) {
75 process->unw_underlying_addr_space = unw_local_addr_space;
76 process->unw_underlying_context = NULL;
78 process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
79 process->unw_underlying_context = _UPT_create(pid);
83 void MC_process_clear(mc_process_t process)
85 process->address_space.address_space_class = NULL;
86 process->process_flags = MC_PROCESS_NO_FLAG;
89 MC_free_memory_map(process->memory_map);
90 process->memory_map = NULL;
92 process->maestro_stack_start = NULL;
93 process->maestro_stack_end = NULL;
96 for (i=0; i!=process->object_infos_size; ++i) {
97 MC_free_object_info(&process->object_infos[i]);
99 free(process->object_infos);
100 process->object_infos = NULL;
101 process->object_infos_size = 0;
102 if (process->memory_file >= 0) {
103 close(process->memory_file);
106 if (process->unw_underlying_addr_space != unw_local_addr_space) {
107 unw_destroy_addr_space(process->unw_underlying_addr_space);
108 _UPT_destroy(process->unw_underlying_context);
110 process->unw_underlying_context = NULL;
111 process->unw_underlying_addr_space = NULL;
113 unw_destroy_addr_space(process->unw_addr_space);
114 process->unw_addr_space = NULL;
116 process->cache_flags = 0;
119 process->heap = NULL;
121 free(process->heap_info);
122 process->heap_info = NULL;
125 void MC_process_refresh_heap(mc_process_t process)
127 assert(!MC_process_is_self(process));
128 // Read/dereference/refresh the std_heap pointer:
129 if (!process->heap) {
130 xbt_mheap_t oldheap = mmalloc_get_current_heap();
132 process->heap = malloc(sizeof(struct mdesc));
133 mmalloc_set_current_heap(oldheap);
135 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
136 process->heap, process->heap_address, sizeof(struct mdesc),
137 MC_PROCESS_INDEX_DISABLED
141 void MC_process_refresh_malloc_info(mc_process_t process)
143 assert(!MC_process_is_self(process));
144 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
145 MC_process_refresh_heap(process);
146 // Refresh process->heapinfo:
147 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
149 xbt_mheap_t oldheap = mmalloc_get_current_heap();
151 process->heap_info = (malloc_info*) realloc(process->heap_info,
152 malloc_info_bytesize);
153 mmalloc_set_current_heap(oldheap);
155 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
157 process->heap->heapinfo, malloc_info_bytesize,
158 MC_PROCESS_INDEX_DISABLED);
161 #define SO_RE "\\.so[\\.0-9]*$"
162 #define VERSION_RE "-[\\.0-9]*$"
164 const char* FILTERED_LIBS[] = {
186 static bool MC_is_simgrid_lib(const char* libname)
188 return !strcmp(libname, "libsimgrid");
191 static bool MC_is_filtered_lib(const char* libname)
193 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
196 if (strcmp(libname, FILTERED_LIBS[i])==0)
201 struct s_mc_memory_map_re {
206 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
207 const char* map_basename = basename((char*) pathname);
210 if(regexec(&res->so_re, map_basename, 1, &match, 0))
213 char* libname = strndup(map_basename, match.rm_so);
215 // Strip the version suffix:
216 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
217 char* temp = libname;
218 libname = strndup(temp, match.rm_so);
225 /** @brief Finds the range of the different memory segments and binary paths */
226 static void MC_process_init_memory_map_info(mc_process_t process)
228 XBT_DEBUG("Get debug information ...");
229 process->maestro_stack_start = NULL;
230 process->maestro_stack_end = NULL;
231 process->object_infos = NULL;
232 process->object_infos_size = 0;
233 process->binary_info = NULL;
234 process->libsimgrid_info = NULL;
236 struct s_mc_memory_map_re res;
238 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
239 xbt_die(".so regexp did not compile");
241 memory_map_t maps = process->memory_map;
243 const char* current_name = NULL;
246 for (i=0; i < maps->mapsize; i++) {
247 map_region_t reg = &(maps->regions[i]);
248 const char* pathname = maps->regions[i].pathname;
251 if (maps->regions[i].pathname == NULL) {
256 // [stack], [vvar], [vsyscall], [vdso] ...
257 if (pathname[0] == '[') {
258 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
259 process->maestro_stack_start = reg->start_addr;
260 process->maestro_stack_end = reg->end_addr;
266 if (current_name && strcmp(current_name, pathname)==0)
269 current_name = pathname;
270 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
273 const bool is_executable = !i;
274 char* libname = NULL;
275 if (!is_executable) {
276 libname = MC_get_lib_name(pathname, &res);
279 if (MC_is_filtered_lib(libname)) {
285 mc_object_info_t info =
286 MC_find_object_info(process->memory_map, pathname, is_executable);
287 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
288 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
289 process->object_infos[process->object_infos_size] = info;
290 process->object_infos_size++;
292 process->binary_info = info;
293 else if (libname && MC_is_simgrid_lib(libname))
294 process->libsimgrid_info = info;
299 regfree(&res.version_re);
301 // Resolve time (including accress differents objects):
302 for (i=0; i!=process->object_infos_size; ++i)
303 MC_post_process_object_info(process, process->object_infos[i]);
305 xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
306 xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
308 XBT_DEBUG("Get debug information done !");
311 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
314 for (i = 0; i != process->object_infos_size; ++i) {
315 if (addr >= (void *) process->object_infos[i]->start
316 && addr <= (void *) process->object_infos[i]->end) {
317 return process->object_infos[i];
323 mc_object_info_t MC_process_find_object_info_exec(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_exec
328 && addr <= (void *) process->object_infos[i]->end_exec) {
329 return process->object_infos[i];
335 mc_object_info_t MC_process_find_object_info_rw(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_rw
340 && addr <= (void *) process->object_infos[i]->end_rw) {
341 return process->object_infos[i];
347 // Functions, variables…
349 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
351 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
355 return MC_file_object_info_find_function(info, ip);
358 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
360 const size_t n = process->object_infos_size;
362 for (i=0; i!=n; ++i) {
363 mc_object_info_t info =process->object_infos[i];
364 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
371 // ***** Memory access
373 int MC_process_vm_open(pid_t pid, int flags)
375 const size_t buffer_size = 30;
376 char buffer[buffer_size];
377 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
378 if (res < 0 || res >= buffer_size) {
379 errno = ENAMETOOLONG;
382 return open(buffer, flags);
385 static void MC_process_open_memory_file(mc_process_t process)
387 if (MC_process_is_self(process) || process->memory_file >= 0)
390 int fd = MC_process_vm_open(process->pid, O_RDWR);
392 xbt_die("Could not open file for process virtual address space");
393 process->memory_file = fd;
396 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
398 char* buffer = (char*) buf;
399 ssize_t real_count = count;
401 ssize_t res = pread(fd, buffer, count, offset);
408 } else if (errno != EINTR) {
415 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
417 const char* buffer = (const char*) buf;
418 ssize_t real_count = count;
420 ssize_t res = pwrite(fd, buffer, count, offset);
427 } else if (errno != EINTR) {
434 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
435 void* local, const void* remote, size_t len,
438 if (process_index != MC_PROCESS_INDEX_DISABLED) {
439 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
440 // Segment overlap is not handled.
441 if (MC_object_info_is_privatized(info)) {
442 if (process_index < 0)
443 xbt_die("Missing process index");
444 // Address translation in the privaization segment:
445 size_t offset = (const char*) remote - info->start_rw;
446 remote = (const char*) remote - offset;
450 if (MC_process_is_self(process)) {
451 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
454 memcpy(local, remote, len);
458 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
459 xbt_die("Read from process %lli failed", (long long) process->pid);
464 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
466 if (MC_process_is_self(process)) {
467 memcpy(remote, local, len);
469 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
470 xbt_die("Write to process %lli failed", (long long) process->pid);
474 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
475 static const void* zero_buffer;
476 static const int zero_buffer_size = 10 * 4096;
478 static void MC_zero_buffer_init(void)
480 int fd = open("/dev/zero", O_RDONLY);
482 xbt_die("Could not open /dev/zero");
483 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
484 if (zero_buffer == MAP_FAILED)
485 xbt_die("Could not map the zero buffer");
489 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
491 if (MC_process_is_self(process)) {
492 memset(remote, 0, len);
494 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
496 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
497 MC_process_write(process, zero_buffer, remote, s);
498 remote = (char*) remote + s;