11 #include <sys/mman.h> // PROT_*
17 #include "mc_process.h"
18 #include "mc_object_info.h"
19 #include "mc_address_space.h"
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
22 "MC process information");
24 static void MC_process_init_memory_map_info(mc_process_t process);
25 static void MC_process_open_memory_file(mc_process_t process);
27 static s_mc_address_space_class_t mc_process_class = {
28 .read = (void*) &MC_process_read
31 void MC_process_init(mc_process_t process, pid_t pid)
33 process->address_space.address_space_class = &mc_process_class;
34 process->process_flags = MC_PROCESS_NO_FLAG;
37 process->process_flags |= MC_PROCESS_SELF_FLAG;
38 process->memory_map = MC_get_memory_map(pid);
39 process->memory_file = -1;
40 process->cache_flags = 0;
42 process->heap_info = NULL;
43 MC_process_init_memory_map_info(process);
44 MC_process_open_memory_file(process);
46 // Read std_heap (is a struct mdesc*):
47 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
49 xbt_die("No heap information in the target process");
50 if(!std_heap_var->address)
51 xbt_die("No constant address for this variable");
52 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
53 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
54 MC_PROCESS_INDEX_DISABLED);
57 void MC_process_clear(mc_process_t process)
59 process->address_space.address_space_class = NULL;
60 process->process_flags = MC_PROCESS_NO_FLAG;
63 MC_free_memory_map(process->memory_map);
64 process->memory_map = NULL;
66 process->maestro_stack_start = NULL;
67 process->maestro_stack_end = NULL;
70 for (i=0; i!=process->object_infos_size; ++i) {
71 MC_free_object_info(&process->object_infos[i]);
73 free(process->object_infos);
74 process->object_infos = NULL;
75 process->object_infos_size = 0;
76 if (process->memory_file >= 0) {
77 close(process->memory_file);
80 process->cache_flags = 0;
85 free(process->heap_info);
86 process->heap_info = NULL;
89 void MC_process_refresh_heap(mc_process_t process)
91 assert(!MC_process_is_self(process));
92 // Read/dereference/refresh the std_heap pointer:
94 xbt_mheap_t oldheap = mmalloc_get_current_heap();
96 process->heap = malloc(sizeof(struct mdesc));
97 mmalloc_set_current_heap(oldheap);
99 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
100 process->heap, process->heap_address, sizeof(struct mdesc),
101 MC_PROCESS_INDEX_DISABLED
105 void MC_process_refresh_malloc_info(mc_process_t process)
107 assert(!MC_process_is_self(process));
108 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
109 MC_process_refresh_heap(process);
110 // Refresh process->heapinfo:
111 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
113 xbt_mheap_t oldheap = mmalloc_get_current_heap();
115 process->heap_info = (malloc_info*) realloc(process->heap_info,
116 malloc_info_bytesize);
117 mmalloc_set_current_heap(oldheap);
119 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
121 process->heap->heapinfo, malloc_info_bytesize,
122 MC_PROCESS_INDEX_DISABLED);
125 #define SO_RE "\\.so[\\.0-9]*$"
126 #define VERSION_RE "-[\\.0-9]*$"
128 const char* FILTERED_LIBS[] = {
149 static bool MC_is_simgrid_lib(const char* libname)
151 return !strcmp(libname, "libsimgrid");
154 static bool MC_is_filtered_lib(const char* libname)
156 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
159 if (strcmp(libname, FILTERED_LIBS[i])==0)
164 struct s_mc_memory_map_re {
169 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
170 const char* map_basename = basename((char*) pathname);
173 if(regexec(&res->so_re, map_basename, 1, &match, 0))
176 char* libname = strndup(map_basename, match.rm_so);
178 // Strip the version suffix:
179 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
180 char* temp = libname;
181 libname = strndup(temp, match.rm_so);
188 /** @brief Finds the range of the different memory segments and binary paths */
189 static void MC_process_init_memory_map_info(mc_process_t process)
191 XBT_INFO("Get debug information ...");
192 process->maestro_stack_start = NULL;
193 process->maestro_stack_end = NULL;
194 process->object_infos = NULL;
195 process->object_infos_size = 0;
196 process->binary_info = NULL;
197 process->libsimgrid_info = NULL;
199 struct s_mc_memory_map_re res;
201 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
202 xbt_die(".so regexp did not compile");
204 memory_map_t maps = process->memory_map;
206 const char* current_name = NULL;
209 for (i=0; i < maps->mapsize; i++) {
210 map_region_t reg = &(maps->regions[i]);
211 const char* pathname = maps->regions[i].pathname;
214 if (maps->regions[i].pathname == NULL) {
219 // [stack], [vvar], [vsyscall], [vdso] ...
220 if (pathname[0] == '[') {
221 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
222 process->maestro_stack_start = reg->start_addr;
223 process->maestro_stack_end = reg->end_addr;
229 if (current_name && strcmp(current_name, pathname)==0)
232 current_name = pathname;
233 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
236 const bool is_executable = !i;
237 char* libname = NULL;
238 if (!is_executable) {
239 libname = MC_get_lib_name(pathname, &res);
242 if (MC_is_filtered_lib(libname)) {
248 mc_object_info_t info =
249 MC_find_object_info(process->memory_map, pathname, is_executable);
250 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
251 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
252 process->object_infos[process->object_infos_size] = info;
253 process->object_infos_size++;
255 process->binary_info = info;
256 else if (libname && MC_is_simgrid_lib(libname))
257 process->libsimgrid_info = info;
262 regfree(&res.version_re);
264 // Resolve time (including accress differents objects):
265 for (i=0; i!=process->object_infos_size; ++i)
266 MC_post_process_object_info(process, process->object_infos[i]);
268 xbt_assert(process->maestro_stack_start, "maestro_stack_start");
269 xbt_assert(process->maestro_stack_end, "maestro_stack_end");
271 XBT_INFO("Get debug information done !");
274 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
277 for (i = 0; i != process->object_infos_size; ++i) {
278 if (addr >= (void *) process->object_infos[i]->start
279 && addr <= (void *) process->object_infos[i]->end) {
280 return process->object_infos[i];
286 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
289 for (i = 0; i != process->object_infos_size; ++i) {
290 if (addr >= (void *) process->object_infos[i]->start_exec
291 && addr <= (void *) process->object_infos[i]->end_exec) {
292 return process->object_infos[i];
298 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
301 for (i = 0; i != process->object_infos_size; ++i) {
302 if (addr >= (void *) process->object_infos[i]->start_rw
303 && addr <= (void *) process->object_infos[i]->end_rw) {
304 return process->object_infos[i];
310 // Functions, variables…
312 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
314 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
318 return MC_file_object_info_find_function(info, ip);
321 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
323 const size_t n = process->object_infos_size;
325 for (i=0; i!=n; ++i) {
326 mc_object_info_t info =process->object_infos[i];
327 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
334 // ***** Memory access
336 static void MC_process_open_memory_file(mc_process_t process)
338 if (MC_process_is_self(process) || process->memory_file >= 0)
341 const size_t buffer_size = 30;
342 char buffer[buffer_size];
343 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) process->pid);
344 if (res < 0 || res>= buffer_size) {
345 XBT_ERROR("Could not open memory file descriptor for process %lli",
346 (long long) process->pid);
350 int fd = open(buffer, O_RDWR);
352 xbt_die("Could not initialise memory access for remote process");
353 process->memory_file = fd;
356 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
358 char* buffer = (char*) buf;
359 ssize_t real_count = count;
361 ssize_t res = pread(fd, buffer, count, offset);
368 } else if (errno != EINTR) {
375 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
377 const char* buffer = (const char*) buf;
378 ssize_t real_count = count;
380 ssize_t res = pwrite(fd, buffer, count, offset);
387 } else if (errno != EINTR) {
394 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
395 void* local, const void* remote, size_t len,
398 if (process_index != MC_PROCESS_INDEX_DISABLED) {
399 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
400 // Segment overlap is not handled.
401 if (MC_object_info_is_privatized(info)) {
402 if (process_index < 0)
403 xbt_die("Missing process index");
404 // Address translation in the privaization segment:
405 size_t offset = (const char*) remote - info->start_rw;
406 remote = (const char*) remote - offset;
410 if (MC_process_is_self(process)) {
411 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
414 memcpy(local, remote, len);
418 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
419 xbt_die("Read from process %lli failed", (long long) process->pid);
424 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
426 if (MC_process_is_self(process)) {
427 memcpy(remote, local, len);
429 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
430 xbt_die("Write to process %lli failed", (long long) process->pid);
434 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
435 static const void* zero_buffer;
436 static const int zero_buffer_size = 10 * 4096;
438 static void MC_zero_buffer_init(void)
440 int fd = open("/dev/zero", O_RDONLY);
442 xbt_die("Could not open /dev/zero");
443 zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
444 if (zero_buffer == MAP_FAILED)
445 xbt_die("Could not map the zero buffer");
449 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
451 if (MC_process_is_self(process)) {
452 memset(remote, 0, len);
454 pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
456 size_t s = len > zero_buffer_size ? zero_buffer_size : len;
457 MC_process_write(process, zero_buffer, remote, s);
458 remote = (char*) remote + s;