Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Multiple heap removal (partial)
[simgrid.git] / src / mc / mc_process.cpp
1 /* Copyright (c) 2014-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 #include <assert.h>
8 #include <stddef.h>
9 #include <stdbool.h>
10 #include <stdint.h>
11 #include <errno.h>
12
13 #include <sys/types.h>
14 #include <fcntl.h>
15 #include <unistd.h>
16 #include <regex.h>
17 #include <sys/mman.h> // PROT_*
18
19 #include <pthread.h>
20
21 #include <libgen.h>
22
23 #include <libunwind.h>
24 #include <libunwind-ptrace.h>
25
26 #include <xbt/mmalloc.h>
27
28 #include "mc_process.h"
29 #include "mc_object_info.h"
30 #include "mc_address_space.h"
31 #include "mc_unw.h"
32 #include "mc_snapshot.h"
33 #include "mc_ignore.h"
34 #include "mc_smx.h"
35 #include "mc_server.h"
36
37 extern "C" {
38
39 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
40                                 "MC process information");
41
42 static void MC_process_init_memory_map_info(mc_process_t process);
43 static void MC_process_open_memory_file(mc_process_t process);
44
45 // ***** Destructor callbacks
46
47 // ***** mc_address_space methods for mc_process
48
49 static mc_process_t MC_process_get_process(mc_process_t p) {
50   return p;
51 }
52
53 static const s_mc_address_space_class_t mc_process_class = {
54   .read = (mc_address_space_class_read_callback_t) &MC_process_read,
55   .get_process = (mc_address_space_class_get_process_callback_t) MC_process_get_process
56 };
57
58 bool MC_is_process(mc_address_space_t p)
59 {
60   return p->address_space_class == &mc_process_class;
61 }
62
63 // ***** mc_process
64
65 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
66 {
67   process->address_space.address_space_class = &mc_process_class;
68   process->process_flags = MC_PROCESS_NO_FLAG;
69   process->socket = sockfd;
70   process->pid = pid;
71   if (pid==getpid())
72     process->process_flags |= MC_PROCESS_SELF_FLAG;
73   process->running = true;
74   process->status = 0;
75   process->memory_map = MC_get_memory_map(pid);
76   process->memory_file = -1;
77   process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
78   process->heap = NULL;
79   process->heap_info = NULL;
80   MC_process_init_memory_map_info(process);
81   MC_process_open_memory_file(process);
82
83   // Read std_heap (is a struct mdesc*):
84   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
85   if (!std_heap_var)
86     xbt_die("No heap information in the target process");
87   if(!std_heap_var->address)
88     xbt_die("No constant address for this variable");
89   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
90     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
91     MC_PROCESS_INDEX_DISABLED);
92
93   process->smx_process_infos = MC_smx_process_info_list_new();
94   process->smx_old_process_infos = MC_smx_process_info_list_new();
95
96   process->checkpoint_ignore = MC_checkpoint_ignore_new();
97
98   process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors  , __BYTE_ORDER);
99   if (process->process_flags & MC_PROCESS_SELF_FLAG) {
100     process->unw_underlying_addr_space = unw_local_addr_space;
101     process->unw_underlying_context = NULL;
102   } else {
103     process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
104     process->unw_underlying_context = _UPT_create(pid);
105   }
106 }
107
108 void MC_process_clear(mc_process_t process)
109 {
110   process->address_space.address_space_class = NULL;
111   process->process_flags = MC_PROCESS_NO_FLAG;
112   process->pid = 0;
113
114   MC_free_memory_map(process->memory_map);
115   process->memory_map = NULL;
116
117   process->maestro_stack_start = NULL;
118   process->maestro_stack_end = NULL;
119
120   xbt_dynar_free(&process->checkpoint_ignore);
121
122   xbt_dynar_free(&process->smx_process_infos);
123   xbt_dynar_free(&process->smx_old_process_infos);
124
125   size_t i;
126   for (i=0; i!=process->object_infos_size; ++i) {
127     MC_free_object_info(&process->object_infos[i]);
128   }
129   free(process->object_infos);
130   process->object_infos = NULL;
131   process->object_infos_size = 0;
132   if (process->memory_file >= 0) {
133     close(process->memory_file);
134   }
135
136   if (process->unw_underlying_addr_space != unw_local_addr_space) {
137     unw_destroy_addr_space(process->unw_underlying_addr_space);
138     _UPT_destroy(process->unw_underlying_context);
139   }
140   process->unw_underlying_context = NULL;
141   process->unw_underlying_addr_space = NULL;
142
143   unw_destroy_addr_space(process->unw_addr_space);
144   process->unw_addr_space = NULL;
145
146   process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
147
148   free(process->heap);
149   process->heap = NULL;
150
151   free(process->heap_info);
152   process->heap_info = NULL;
153 }
154
155 void MC_process_refresh_heap(mc_process_t process)
156 {
157   assert(!MC_process_is_self(process));
158   // Read/dereference/refresh the std_heap pointer:
159   if (!process->heap) {
160     process->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
161   }
162   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
163     process->heap, process->heap_address, sizeof(struct mdesc),
164     MC_PROCESS_INDEX_DISABLED
165     );
166 }
167
168 void MC_process_refresh_malloc_info(mc_process_t process)
169 {
170   assert(!MC_process_is_self(process));
171   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
172     MC_process_refresh_heap(process);
173   // Refresh process->heapinfo:
174   size_t malloc_info_bytesize =
175     (process->heap->heaplimit + 1) * sizeof(malloc_info);
176   process->heap_info = (malloc_info*) realloc(process->heap_info, malloc_info_bytesize);
177   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
178     process->heap_info,
179     process->heap->heapinfo, malloc_info_bytesize,
180     MC_PROCESS_INDEX_DISABLED);
181 }
182
183 #define SO_RE "\\.so[\\.0-9]*$"
184 #define VERSION_RE "-[\\.0-9]*$"
185
186 const char* FILTERED_LIBS[] = {
187   "libstdc++",
188   "libc++",
189   "libm",
190   "libgcc_s",
191   "libpthread",
192   "libunwind",
193   "libunwind-x86_64",
194   "libunwind-x86",
195   "libunwind-ptrace",
196   "libdw",
197   "libdl",
198   "librt",
199   "liblzma",
200   "libelf",
201   "libbz2",
202   "libz",
203   "libelf",
204   "libc",
205   "ld"
206 };
207
208 static bool MC_is_simgrid_lib(const char* libname)
209 {
210   return !strcmp(libname, "libsimgrid");
211 }
212
213 static bool MC_is_filtered_lib(const char* libname)
214 {
215   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
216   size_t i;
217   for (i=0; i!=n; ++i)
218     if (strcmp(libname, FILTERED_LIBS[i])==0)
219       return true;
220   return false;
221 }
222
223 struct s_mc_memory_map_re {
224   regex_t so_re;
225   regex_t version_re;
226 };
227
228 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
229   const char* map_basename = basename((char*) pathname);
230
231   regmatch_t match;
232   if(regexec(&res->so_re, map_basename, 1, &match, 0))
233     return NULL;
234
235   char* libname = strndup(map_basename, match.rm_so);
236
237   // Strip the version suffix:
238   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
239     char* temp = libname;
240     libname = strndup(temp, match.rm_so);
241     free(temp);
242   }
243
244   return libname;
245 }
246
247 /** @brief Finds the range of the different memory segments and binary paths */
248 static void MC_process_init_memory_map_info(mc_process_t process)
249 {
250   XBT_DEBUG("Get debug information ...");
251   process->maestro_stack_start = NULL;
252   process->maestro_stack_end = NULL;
253   process->object_infos = NULL;
254   process->object_infos_size = 0;
255   process->binary_info = NULL;
256   process->libsimgrid_info = NULL;
257
258   struct s_mc_memory_map_re res;
259
260   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
261     xbt_die(".so regexp did not compile");
262
263   memory_map_t maps = process->memory_map;
264
265   const char* current_name = NULL;
266
267   for (ssize_t i=0; i < maps->mapsize; i++) {
268     map_region_t reg = &(maps->regions[i]);
269     const char* pathname = maps->regions[i].pathname;
270
271     // Nothing to do
272     if (maps->regions[i].pathname == NULL) {
273       current_name = NULL;
274       continue;
275     }
276
277     // [stack], [vvar], [vsyscall], [vdso] ...
278     if (pathname[0] == '[') {
279       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
280         process->maestro_stack_start = reg->start_addr;
281         process->maestro_stack_end = reg->end_addr;
282       }
283       current_name = NULL;
284       continue;
285     }
286
287     if (current_name && strcmp(current_name, pathname)==0)
288       continue;
289
290     current_name = pathname;
291     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
292       continue;
293
294     const bool is_executable = !i;
295     char* libname = NULL;
296     if (!is_executable) {
297       libname = MC_get_lib_name(pathname, &res);
298       if(!libname)
299         continue;
300       if (MC_is_filtered_lib(libname)) {
301         free(libname);
302         continue;
303       }
304     }
305
306     mc_object_info_t info =
307       MC_find_object_info(process->memory_map, pathname, is_executable);
308     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
309       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
310     process->object_infos[process->object_infos_size] = info;
311     process->object_infos_size++;
312     if (is_executable)
313       process->binary_info = info;
314     else if (libname && MC_is_simgrid_lib(libname))
315       process->libsimgrid_info = info;
316     free(libname);
317   }
318
319   regfree(&res.so_re);
320   regfree(&res.version_re);
321
322   // Resolve time (including accress differents objects):
323   for (size_t i=0; i!=process->object_infos_size; ++i)
324     MC_post_process_object_info(process, process->object_infos[i]);
325
326   xbt_assert(process->maestro_stack_start, "Did not find maestro_stack_start");
327   xbt_assert(process->maestro_stack_end, "Did not find maestro_stack_end");
328
329   XBT_DEBUG("Get debug information done !");
330 }
331
332 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
333 {
334   size_t i;
335   for (i = 0; i != process->object_infos_size; ++i) {
336     if (addr >= (void *) process->object_infos[i]->start
337         && addr <= (void *) process->object_infos[i]->end) {
338       return process->object_infos[i];
339     }
340   }
341   return NULL;
342 }
343
344 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
345 {
346   size_t i;
347   for (i = 0; i != process->object_infos_size; ++i) {
348     if (addr >= (void *) process->object_infos[i]->start_exec
349         && addr <= (void *) process->object_infos[i]->end_exec) {
350       return process->object_infos[i];
351     }
352   }
353   return NULL;
354 }
355
356 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
357 {
358   size_t i;
359   for (i = 0; i != process->object_infos_size; ++i) {
360     if (addr >= (void *) process->object_infos[i]->start_rw
361         && addr <= (void *) process->object_infos[i]->end_rw) {
362       return process->object_infos[i];
363     }
364   }
365   return NULL;
366 }
367
368 // Functions, variables…
369
370 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
371 {
372   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
373   if (info == NULL)
374     return NULL;
375   else
376     return MC_file_object_info_find_function(info, ip);
377 }
378
379 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
380 {
381   const size_t n = process->object_infos_size;
382   size_t i;
383
384   // First lookup the variable in the executable shared object.
385   // A global variable used directly by the executable code from a library
386   // is reinstanciated in the executable memory .data/.bss.
387   // We need to look up the variable in the execvutable first.
388   if (process->binary_info) {
389     mc_object_info_t info = process->binary_info;
390     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
391     if (var)
392       return var;
393   }
394
395   for (i=0; i!=n; ++i) {
396     mc_object_info_t info =process->object_infos[i];
397     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
398     if (var)
399       return var;
400   }
401
402   return NULL;
403 }
404
405 void MC_process_read_variable(mc_process_t process, const char* name, void* target, size_t size)
406 {
407   dw_variable_t var = MC_process_find_variable_by_name(process, name);
408   if (!var->address)
409     xbt_die("No simple location for this variable");
410   if (!var->type->full_type)
411     xbt_die("Partial type for %s, cannot check size", name);
412   if ((size_t) var->type->full_type->byte_size != size)
413     xbt_die("Unexpected size for %s (expected %zi, was %zi)",
414       name, size, (size_t) var->type->full_type->byte_size);
415   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE, target, var->address, size,
416     MC_PROCESS_INDEX_ANY);
417 }
418
419 char* MC_process_read_string(mc_process_t process, void* address)
420 {
421   if (!address)
422     return NULL;
423   if (MC_process_is_self(process))
424     return strdup((char*) address);
425
426   off_t len = 128;
427   char* res = (char*) malloc(len);
428   off_t off = 0;
429
430   while (1) {
431     ssize_t c = pread(process->memory_file, res + off, len - off, (off_t) address + off);
432     if (c == -1) {
433       if (errno == EINTR)
434         continue;
435       else
436         xbt_die("Could not read from from remote process");
437     }
438     if (c==0)
439       xbt_die("Could not read string from remote process");
440
441     void* p = memchr(res + off, '\0', c);
442     if (p)
443       return res;
444
445     off += c;
446     if (off == len) {
447       len *= 2;
448       res = (char*) realloc(res, len);
449     }
450   }
451 }
452
453 // ***** Memory access
454
455 int MC_process_vm_open(pid_t pid, int flags)
456 {
457   const size_t buffer_size = 30;
458   char buffer[buffer_size];
459   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) pid);
460   if (res < 0 || (size_t) res >= buffer_size) {
461     errno = ENAMETOOLONG;
462     return -1;
463   }
464   return open(buffer, flags);
465 }
466
467 static void MC_process_open_memory_file(mc_process_t process)
468 {
469   if (MC_process_is_self(process) || process->memory_file >= 0)
470     return;
471
472   int fd = MC_process_vm_open(process->pid, O_RDWR);
473   if (fd<0)
474     xbt_die("Could not open file for process virtual address space");
475   process->memory_file = fd;
476 }
477
478 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
479 {
480   char* buffer = (char*) buf;
481   ssize_t real_count = count;
482   while (count) {
483     ssize_t res = pread(fd, buffer, count, offset);
484     if (res > 0) {
485       count  -= res;
486       buffer += res;
487       offset += res;
488     } else if (res==0) {
489       return -1;
490     } else if (errno != EINTR) {
491       return -1;
492     }
493   }
494   return real_count;
495 }
496
497 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
498 {
499   const char* buffer = (const char*) buf;
500   ssize_t real_count = count;
501   while (count) {
502     ssize_t res = pwrite(fd, buffer, count, offset);
503     if (res > 0) {
504       count  -= res;
505       buffer += res;
506       offset += res;
507     } else if (res==0) {
508       return -1;
509     } else if (errno != EINTR) {
510       return -1;
511     }
512   }
513   return real_count;
514 }
515
516 const void* MC_process_read(mc_process_t process, adress_space_read_flags_t flags,
517   void* local, const void* remote, size_t len,
518   int process_index)
519 {
520   if (process_index != MC_PROCESS_INDEX_DISABLED) {
521     mc_object_info_t info = MC_process_find_object_info_rw(process, remote);
522     // Segment overlap is not handled.
523     if (MC_object_info_is_privatized(info)) {
524       if (process_index < 0)
525         xbt_die("Missing process index");
526       // Address translation in the privaization segment:
527       size_t offset = (const char*) remote - info->start_rw;
528       remote = (const char*) remote - offset;
529     }
530   }
531
532   if (MC_process_is_self(process)) {
533     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
534       return remote;
535     else {
536       memcpy(local, remote, len);
537       return local;
538     }
539   } else {
540     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
541       xbt_die("Read from process %lli failed", (long long) process->pid);
542     return local;
543   }
544 }
545
546 const void* MC_process_read_simple(mc_process_t process,
547   void* local, const void* remote, size_t len)
548 {
549   adress_space_read_flags_t flags = MC_ADDRESS_SPACE_READ_FLAGS_NONE;
550   int index = MC_PROCESS_INDEX_ANY;
551    MC_process_read(process, flags, local, remote, len, index);
552    return local;
553 }
554
555 const void* MC_process_read_dynar_element(mc_process_t process,
556   void* local, const void* remote_dynar, size_t i, size_t len)
557 {
558   s_xbt_dynar_t d;
559   MC_process_read_simple(process, &d, remote_dynar, sizeof(d));
560   if (i >= d.used)
561     xbt_die("Out of bound index %zi/%zi", i, d.used);
562   if (len != d.elmsize)
563     xbt_die("Bad size in MC_process_read_dynar_element");
564   MC_process_read_simple(process, local, xbt_dynar_get_ptr(&d, i), len);
565   return local;
566 }
567
568 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
569 {
570   if (MC_process_is_self(process)) {
571     memcpy(remote, local, len);
572   } else {
573     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
574       xbt_die("Write to process %lli failed", (long long) process->pid);
575   }
576 }
577
578 unsigned long MC_process_read_dynar_length(mc_process_t process, const void* remote_dynar)
579 {
580   if (!remote_dynar)
581     return 0;
582   unsigned long res;
583   MC_process_read_simple(process, &res,
584     &((xbt_dynar_t)remote_dynar)->used, sizeof(res));
585   return res;
586 }
587
588 static pthread_once_t zero_buffer_flag = PTHREAD_ONCE_INIT;
589 static const void* zero_buffer;
590 static const int zero_buffer_size = 10 * 4096;
591
592 static void MC_zero_buffer_init(void)
593 {
594   int fd = open("/dev/zero", O_RDONLY);
595   if (fd<0)
596     xbt_die("Could not open /dev/zero");
597   zero_buffer = mmap(NULL, zero_buffer_size, PROT_READ, MAP_SHARED, fd, 0);
598   if (zero_buffer == MAP_FAILED)
599     xbt_die("Could not map the zero buffer");
600   close(fd);
601 }
602
603 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
604 {
605   if (MC_process_is_self(process)) {
606     memset(remote, 0, len);
607   } else {
608     pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
609     while (len) {
610       size_t s = len > zero_buffer_size ? zero_buffer_size : len;
611       MC_process_write(process, zero_buffer, remote, s);
612       remote = (char*) remote + s;
613       len -= s;
614     }
615   }
616 }
617
618 }