Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Do not call malloc_no_memset in mc_snapshot
[simgrid.git] / src / mc / mc_process.c
1 #include <assert.h>
2 #include <stddef.h>
3 #include <stdbool.h>
4 #include <stdint.h>
5 #include <errno.h>
6
7 #include <sys/types.h>
8 #include <fcntl.h>
9 #include <unistd.h>
10 #include <regex.h>
11 #include <sys/mman.h> // PROT_*
12
13 #include <pthread.h>
14
15 #include <libgen.h>
16
17 #include <libunwind.h>
18 #include <libunwind-ptrace.h>
19
20 #include <xbt/mmalloc.h>
21
22 #include "mc_process.h"
23 #include "mc_object_info.h"
24 #include "mc_address_space.h"
25 #include "mc_unw.h"
26 #include "mc_snapshot.h"
27 #include "mc_ignore.h"
28
29 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
30                                 "MC process information");
31
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);
34
35 // ***** Destructor callbacks
36
37 // ***** mc_address_space methods for mc_process
38
39 static mc_process_t MC_process_get_process(mc_process_t p) {
40   return p;
41 }
42
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
46 };
47
48 bool MC_is_process(mc_address_space_t p)
49 {
50   return p->address_space_class == &mc_process_class;
51 }
52
53 // ***** mc_process
54
55 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
56 {
57   process->address_space.address_space_class = &mc_process_class;
58   process->process_flags = MC_PROCESS_NO_FLAG;
59   process->socket = sockfd;
60   process->pid = pid;
61   if (pid==getpid())
62     process->process_flags |= MC_PROCESS_SELF_FLAG;
63   process->running = true;
64   process->status = 0;
65   process->memory_map = MC_get_memory_map(pid);
66   process->memory_file = -1;
67   process->cache_flags = 0;
68   process->heap = NULL;
69   process->heap_info = NULL;
70   MC_process_init_memory_map_info(process);
71   MC_process_open_memory_file(process);
72
73   // Read std_heap (is a struct mdesc*):
74   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
75   if (!std_heap_var)
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);
82
83   process->checkpoint_ignore = MC_checkpoint_ignore_new();
84
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;
89   } else {
90     process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
91     process->unw_underlying_context = _UPT_create(pid);
92   }
93 }
94
95 void MC_process_clear(mc_process_t process)
96 {
97   process->address_space.address_space_class = NULL;
98   process->process_flags = MC_PROCESS_NO_FLAG;
99   process->pid = 0;
100
101   MC_free_memory_map(process->memory_map);
102   process->memory_map = NULL;
103
104   process->maestro_stack_start = NULL;
105   process->maestro_stack_end = NULL;
106
107   xbt_dynar_free(&process->checkpoint_ignore);
108
109   size_t i;
110   for (i=0; i!=process->object_infos_size; ++i) {
111     MC_free_object_info(&process->object_infos[i]);
112   }
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);
118   }
119
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);
123   }
124   process->unw_underlying_context = NULL;
125   process->unw_underlying_addr_space = NULL;
126
127   unw_destroy_addr_space(process->unw_addr_space);
128   process->unw_addr_space = NULL;
129
130   process->cache_flags = 0;
131
132   free(process->heap);
133   process->heap = NULL;
134
135   free(process->heap_info);
136   process->heap_info = NULL;
137 }
138
139 void MC_process_refresh_heap(mc_process_t process)
140 {
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_get_current_heap();
145     MC_SET_MC_HEAP;
146     process->heap = malloc(sizeof(struct mdesc));
147     mmalloc_set_current_heap(oldheap);
148   }
149   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
150     process->heap, process->heap_address, sizeof(struct mdesc),
151     MC_PROCESS_INDEX_DISABLED
152     );
153 }
154
155 void MC_process_refresh_malloc_info(mc_process_t process)
156 {
157   assert(!MC_process_is_self(process));
158   if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
159     MC_process_refresh_heap(process);
160   // Refresh process->heapinfo:
161   size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
162
163   xbt_mheap_t oldheap  = mmalloc_get_current_heap();
164   MC_SET_MC_HEAP;
165   process->heap_info = (malloc_info*) realloc(process->heap_info,
166     malloc_info_bytesize);
167   mmalloc_set_current_heap(oldheap);
168
169   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
170     process->heap_info,
171     process->heap->heapinfo, malloc_info_bytesize,
172     MC_PROCESS_INDEX_DISABLED);
173 }
174
175 #define SO_RE "\\.so[\\.0-9]*$"
176 #define VERSION_RE "-[\\.0-9]*$"
177
178 const char* FILTERED_LIBS[] = {
179   "libstdc++",
180   "libc++",
181   "libm",
182   "libgcc_s",
183   "libpthread",
184   "libunwind",
185   "libunwind-x86_64",
186   "libunwind-x86",
187   "libunwind-ptrace",
188   "libdw",
189   "libdl",
190   "librt",
191   "liblzma",
192   "libelf",
193   "libbz2",
194   "libz",
195   "libelf",
196   "libc",
197   "ld"
198 };
199
200 static bool MC_is_simgrid_lib(const char* libname)
201 {
202   return !strcmp(libname, "libsimgrid");
203 }
204
205 static bool MC_is_filtered_lib(const char* libname)
206 {
207   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
208   size_t i;
209   for (i=0; i!=n; ++i)
210     if (strcmp(libname, FILTERED_LIBS[i])==0)
211       return true;
212   return false;
213 }
214
215 struct s_mc_memory_map_re {
216   regex_t so_re;
217   regex_t version_re;
218 };
219
220 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
221   const char* map_basename = basename((char*) pathname);
222
223   regmatch_t match;
224   if(regexec(&res->so_re, map_basename, 1, &match, 0))
225     return NULL;
226
227   char* libname = strndup(map_basename, match.rm_so);
228
229   // Strip the version suffix:
230   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
231     char* temp = libname;
232     libname = strndup(temp, match.rm_so);
233     free(temp);
234   }
235
236   return libname;
237 }
238
239 /** @brief Finds the range of the different memory segments and binary paths */
240 static void MC_process_init_memory_map_info(mc_process_t process)
241 {
242   XBT_DEBUG("Get debug information ...");
243   process->maestro_stack_start = NULL;
244   process->maestro_stack_end = NULL;
245   process->object_infos = NULL;
246   process->object_infos_size = 0;
247   process->binary_info = NULL;
248   process->libsimgrid_info = NULL;
249
250   struct s_mc_memory_map_re res;
251
252   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
253     xbt_die(".so regexp did not compile");
254
255   memory_map_t maps = process->memory_map;
256
257   const char* current_name = NULL;
258
259   size_t i = 0;
260   for (i=0; i < maps->mapsize; i++) {
261     map_region_t reg = &(maps->regions[i]);
262     const char* pathname = maps->regions[i].pathname;
263
264     // Nothing to do
265     if (maps->regions[i].pathname == NULL) {
266       current_name = NULL;
267       continue;
268     }
269
270     // [stack], [vvar], [vsyscall], [vdso] ...
271     if (pathname[0] == '[') {
272       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
273         process->maestro_stack_start = reg->start_addr;
274         process->maestro_stack_end = reg->end_addr;
275       }
276       current_name = NULL;
277       continue;
278     }
279
280     if (current_name && strcmp(current_name, pathname)==0)
281       continue;
282
283     current_name = pathname;
284     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
285       continue;
286
287     const bool is_executable = !i;
288     char* libname = NULL;
289     if (!is_executable) {
290       libname = MC_get_lib_name(pathname, &res);
291       if(!libname)
292         continue;
293       if (MC_is_filtered_lib(libname)) {
294         free(libname);
295         continue;
296       }
297     }
298
299     mc_object_info_t info =
300       MC_find_object_info(process->memory_map, pathname, is_executable);
301     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
302       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
303     process->object_infos[process->object_infos_size] = info;
304     process->object_infos_size++;
305     if (is_executable)
306       process->binary_info = info;
307     else if (libname && MC_is_simgrid_lib(libname))
308       process->libsimgrid_info = info;
309     free(libname);
310   }
311
312   regfree(&res.so_re);
313   regfree(&res.version_re);
314
315   // Resolve time (including accress differents objects):
316   for (i=0; i!=process->object_infos_size; ++i)
317     MC_post_process_object_info(process, process->object_infos[i]);
318
319   xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
320   xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
321
322   XBT_DEBUG("Get debug information done !");
323 }
324
325 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
326 {
327   size_t i;
328   for (i = 0; i != process->object_infos_size; ++i) {
329     if (addr >= (void *) process->object_infos[i]->start
330         && addr <= (void *) process->object_infos[i]->end) {
331       return process->object_infos[i];
332     }
333   }
334   return NULL;
335 }
336
337 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
338 {
339   size_t i;
340   for (i = 0; i != process->object_infos_size; ++i) {
341     if (addr >= (void *) process->object_infos[i]->start_exec
342         && addr <= (void *) process->object_infos[i]->end_exec) {
343       return process->object_infos[i];
344     }
345   }
346   return NULL;
347 }
348
349 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
350 {
351   size_t i;
352   for (i = 0; i != process->object_infos_size; ++i) {
353     if (addr >= (void *) process->object_infos[i]->start_rw
354         && addr <= (void *) process->object_infos[i]->end_rw) {
355       return process->object_infos[i];
356     }
357   }
358   return NULL;
359 }
360
361 // Functions, variables…
362
363 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
364 {
365   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
366   if (info == NULL)
367     return NULL;
368   else
369     return MC_file_object_info_find_function(info, ip);
370 }
371
372 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
373 {
374   const size_t n = process->object_infos_size;
375   size_t i;
376   for (i=0; i!=n; ++i) {
377     mc_object_info_t info =process->object_infos[i];
378     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
379     if (var)
380       return var;
381   }
382   return NULL;
383 }
384
385 // ***** Memory access
386
387 int MC_process_vm_open(pid_t pid, int flags)
388 {
389   const size_t buffer_size = 30;
390   char buffer[buffer_size];
391   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
392   if (res < 0 || res >= buffer_size) {
393     errno = ENAMETOOLONG;
394     return -1;
395   }
396   return open(buffer, flags);
397 }
398
399 static void MC_process_open_memory_file(mc_process_t process)
400 {
401   if (MC_process_is_self(process) || process->memory_file >= 0)
402     return;
403
404   int fd = MC_process_vm_open(process->pid, O_RDWR);
405   if (fd<0)
406     xbt_die("Could not open file for process virtual address space");
407   process->memory_file = fd;
408 }
409
410 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
411 {
412   char* buffer = (char*) buf;
413   ssize_t real_count = count;
414   while (count) {
415     ssize_t res = pread(fd, buffer, count, offset);
416     if (res > 0) {
417       count  -= res;
418       buffer += res;
419       offset += res;
420     } else if (res==0) {
421       return -1;
422     } else if (errno != EINTR) {
423       return -1;
424     }
425   }
426   return real_count;
427 }
428
429 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
430 {
431   const char* buffer = (const char*) buf;
432   ssize_t real_count = count;
433   while (count) {
434     ssize_t res = pwrite(fd, buffer, count, offset);
435     if (res > 0) {
436       count  -= res;
437       buffer += res;
438       offset += res;
439     } else if (res==0) {
440       return -1;
441     } else if (errno != EINTR) {
442       return -1;
443     }
444   }
445   return real_count;
446 }
447
448 const void* MC_process_read(mc_process_t process, e_adress_space_read_flags_t flags,
449   void* local, const void* remote, size_t len,
450   int process_index)
451 {
452   if (process_index != MC_PROCESS_INDEX_DISABLED) {
453     mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
454     // Segment overlap is not handled.
455     if (MC_object_info_is_privatized(info)) {
456       if (process_index < 0)
457         xbt_die("Missing process index");
458       // Address translation in the privaization segment:
459       size_t offset = (const char*) remote - info->start_rw;
460       remote = (const char*) remote - offset;
461     }
462   }
463
464   if (MC_process_is_self(process)) {
465     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
466       return remote;
467     else {
468       memcpy(local, remote, len);
469       return local;
470     }
471   } else {
472     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
473       xbt_die("Read from process %lli failed", (long long) process->pid);
474     return local;
475   }
476 }
477
478 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
479 {
480   if (MC_process_is_self(process)) {
481     memcpy(remote, local, len);
482   } else {
483     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
484       xbt_die("Write to process %lli failed", (long long) process->pid);
485   }
486 }
487
488 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
489 static const void* zero_buffer;
490 static const int zero_buffer_size = 10 * 4096;
491
492 static void MC_zero_buffer_init(void)
493 {
494   int fd = open("/dev/zero", O_RDONLY);
495   if (fd<0)
496     xbt_die("Could not open /dev/zero");
497   zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
498   if (zero_buffer == MAP_FAILED)
499     xbt_die("Could not map the zero buffer");
500   close(fd);
501 }
502
503 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
504 {
505   if (MC_process_is_self(process)) {
506     memset(remote, 0, len);
507   } else {
508     pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
509     while (len) {
510       size_t s = len > zero_buffer_size ? zero_buffer_size : len;
511       MC_process_write(process, zero_buffer, remote, s);
512       remote = (char*) remote + s;
513       len -= s;
514     }
515   }
516 }