11 #include <sys/mman.h> // PROT_*
15 #include "mc_process.h"
16 #include "mc_object_info.h"
17 #include "mc_address_space.h"
19 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
20 "MC process information");
22 static void MC_process_init_memory_map_info(mc_process_t process);
23 static void MC_process_open_memory_file(mc_process_t process);
25 static s_mc_address_space_class_t mc_process_class = {
26 .read = (void*) &MC_process_read
29 void MC_process_init(mc_process_t process, pid_t pid)
31 process->address_space.address_space_class = &mc_process_class;
32 process->process_flags = MC_PROCESS_NO_FLAG;
35 process->process_flags |= MC_PROCESS_SELF_FLAG;
36 process->memory_map = MC_get_memory_map(pid);
37 process->memory_file = -1;
38 process->cache_flags = 0;
40 process->heap_info = NULL;
41 MC_process_init_memory_map_info(process);
42 MC_process_open_memory_file(process);
44 // Read std_heap (is a struct mdesc*):
45 dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
47 xbt_die("No heap information in the target process");
48 if(!std_heap_var->address)
49 xbt_die("No constant address for this variable");
50 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
51 &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
52 MC_PROCESS_INDEX_DISABLED);
55 void MC_process_clear(mc_process_t process)
57 process->address_space.address_space_class = NULL;
58 process->process_flags = MC_PROCESS_NO_FLAG;
61 MC_free_memory_map(process->memory_map);
62 process->memory_map = NULL;
64 process->maestro_stack_start = NULL;
65 process->maestro_stack_end = NULL;
68 for (i=0; i!=process->object_infos_size; ++i) {
69 MC_free_object_info(&process->object_infos[i]);
71 free(process->object_infos);
72 process->object_infos = NULL;
73 process->object_infos_size = 0;
74 if (process->memory_file >= 0) {
75 close(process->memory_file);
78 process->cache_flags = 0;
83 free(process->heap_info);
84 process->heap_info = NULL;
87 void MC_process_refresh_heap(mc_process_t process)
89 assert(!MC_process_is_self(process));
90 // Read/dereference/refresh the std_heap pointer:
92 xbt_mheap_t oldheap = mmalloc_get_current_heap();
94 process->heap = malloc(sizeof(struct mdesc));
95 mmalloc_set_current_heap(oldheap);
97 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
98 process->heap, process->heap_address, sizeof(struct mdesc),
99 MC_PROCESS_INDEX_DISABLED
103 void MC_process_refresh_malloc_info(mc_process_t process)
105 assert(!MC_process_is_self(process));
106 if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
107 MC_process_refresh_heap(process);
108 // Refresh process->heapinfo:
109 size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
111 xbt_mheap_t oldheap = mmalloc_get_current_heap();
113 process->heap_info = (malloc_info*) realloc(process->heap_info,
114 malloc_info_bytesize);
115 mmalloc_set_current_heap(oldheap);
117 MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
119 process->heap->heapinfo, malloc_info_bytesize,
120 MC_PROCESS_INDEX_DISABLED);
123 #define SO_RE "\\.so[\\.0-9]*$"
124 #define VERSION_RE "-[\\.0-9]*$"
126 const char* FILTERED_LIBS[] = {
147 static bool MC_is_simgrid_lib(const char* libname)
149 return !strcmp(libname, "libsimgrid");
152 static bool MC_is_filtered_lib(const char* libname)
154 const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
157 if (strcmp(libname, FILTERED_LIBS[i])==0)
162 struct s_mc_memory_map_re {
167 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
168 const char* map_basename = basename((char*) pathname);
171 if(regexec(&res->so_re, map_basename, 1, &match, 0))
174 char* libname = strndup(map_basename, match.rm_so);
176 // Strip the version suffix:
177 if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
178 char* temp = libname;
179 libname = strndup(temp, match.rm_so);
186 /** @brief Finds the range of the different memory segments and binary paths */
187 static void MC_process_init_memory_map_info(mc_process_t process)
189 XBT_INFO("Get debug information ...");
190 process->maestro_stack_start = NULL;
191 process->maestro_stack_end = NULL;
192 process->object_infos = NULL;
193 process->object_infos_size = 0;
194 process->binary_info = NULL;
195 process->libsimgrid_info = NULL;
197 struct s_mc_memory_map_re res;
199 if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
200 xbt_die(".so regexp did not compile");
202 memory_map_t maps = process->memory_map;
204 const char* current_name = NULL;
207 for (i=0; i < maps->mapsize; i++) {
208 map_region_t reg = &(maps->regions[i]);
209 const char* pathname = maps->regions[i].pathname;
212 if (maps->regions[i].pathname == NULL) {
217 // [stack], [vvar], [vsyscall], [vdso] ...
218 if (pathname[0] == '[') {
219 if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
220 process->maestro_stack_start = reg->start_addr;
221 process->maestro_stack_end = reg->end_addr;
227 if (current_name && strcmp(current_name, pathname)==0)
230 current_name = pathname;
231 if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
234 const bool is_executable = !i;
235 char* libname = NULL;
236 if (!is_executable) {
237 libname = MC_get_lib_name(pathname, &res);
240 if (MC_is_filtered_lib(libname)) {
246 mc_object_info_t info =
247 MC_find_object_info(process->memory_map, pathname, is_executable);
248 process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
249 (process->object_infos_size+1) * sizeof(mc_object_info_t*));
250 process->object_infos[process->object_infos_size] = info;
251 process->object_infos_size++;
253 process->binary_info = info;
254 else if (libname && MC_is_simgrid_lib(libname))
255 process->libsimgrid_info = info;
260 regfree(&res.version_re);
262 // Resolve time (including accress differents objects):
263 for (i=0; i!=process->object_infos_size; ++i)
264 MC_post_process_object_info(process, process->object_infos[i]);
266 xbt_assert(process->maestro_stack_start, "maestro_stack_start");
267 xbt_assert(process->maestro_stack_end, "maestro_stack_end");
269 XBT_INFO("Get debug information done !");
272 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
275 for (i = 0; i != process->object_infos_size; ++i) {
276 if (addr >= (void *) process->object_infos[i]->start
277 && addr <= (void *) process->object_infos[i]->end) {
278 return process->object_infos[i];
284 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
287 for (i = 0; i != process->object_infos_size; ++i) {
288 if (addr >= (void *) process->object_infos[i]->start_exec
289 && addr <= (void *) process->object_infos[i]->end_exec) {
290 return process->object_infos[i];
296 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
299 for (i = 0; i != process->object_infos_size; ++i) {
300 if (addr >= (void *) process->object_infos[i]->start_rw
301 && addr <= (void *) process->object_infos[i]->end_rw) {
302 return process->object_infos[i];
308 // Functions, variables…
310 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
312 mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
316 return MC_file_object_info_find_function(info, ip);
319 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
321 const size_t n = process->object_infos_size;
323 for (i=0; i!=n; ++i) {
324 mc_object_info_t info =process->object_infos[i];
325 dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
332 // ***** Memory access
334 static void MC_process_open_memory_file(mc_process_t process)
336 if (MC_process_is_self(process) || process->memory_file >= 0)
339 const size_t buffer_size = 30;
340 char buffer[buffer_size];
341 int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) process->pid);
342 if (res < 0 || res>= buffer_size) {
343 XBT_ERROR("Could not open memory file descriptor for process %lli",
344 (long long) process->pid);
348 int fd = open(buffer, O_RDWR);
350 xbt_die("Could not initialise memory access for remote process");
351 process->memory_file = fd;
354 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
356 char* buffer = (char*) buf;
357 ssize_t real_count = count;
359 ssize_t res = pread(fd, buffer, count, offset);
366 } else if (errno != EINTR) {
373 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
375 const char* buffer = (const char*) buf;
376 ssize_t real_count = count;
378 ssize_t res = pwrite(fd, buffer, count, offset);
385 } else if (errno != EINTR) {
392 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
393 void* local, const void* remote, size_t len,
396 if (process_index != MC_PROCESS_INDEX_DISABLED) {
397 mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
398 // Segment overlap is not handled.
399 if (MC_object_info_is_privatized(info)) {
400 if (process_index < 0)
401 xbt_die("Missing process index");
402 // Address translation in the privaization segment:
403 size_t offset = (const char*) remote - info->start_rw;
404 remote = (const char*) remote - offset;
408 if (MC_process_is_self(process)) {
409 if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
412 memcpy(local, remote, len);
416 if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
417 xbt_die("Read from process %lli failed", (long long) process->pid);
422 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
424 if (MC_process_is_self(process)) {
425 memcpy(remote, local, len);
427 if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
428 xbt_die("Write to process %lli failed", (long long) process->pid);