Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Switch to C++
[simgrid.git] / src / mc / mc_process.cpp
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 #include "mc_smx.h"
29 #include "mc_server.h"
30
31 extern "C" {
32
33 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
34                                 "MC process information");
35
36 static void MC_process_init_memory_map_info(mc_process_t process);
37 static void MC_process_open_memory_file(mc_process_t process);
38
39 // ***** Destructor callbacks
40
41 // ***** mc_address_space methods for mc_process
42
43 static mc_process_t MC_process_get_process(mc_process_t p) {
44   return p;
45 }
46
47 static const s_mc_address_space_class_t mc_process_class = {
48   .read = (mc_address_space_class_read_callback_t) &MC_process_read,
49   .get_process = (mc_address_space_class_get_process_callback_t) MC_process_get_process
50 };
51
52 bool MC_is_process(mc_address_space_t p)
53 {
54   return p->address_space_class == &mc_process_class;
55 }
56
57 // ***** mc_process
58
59 void MC_process_init(mc_process_t process, pid_t pid, int sockfd)
60 {
61   process->address_space.address_space_class = &mc_process_class;
62   process->process_flags = MC_PROCESS_NO_FLAG;
63   process->socket = sockfd;
64   process->pid = pid;
65   if (pid==getpid())
66     process->process_flags |= MC_PROCESS_SELF_FLAG;
67   process->running = true;
68   process->status = 0;
69   process->memory_map = MC_get_memory_map(pid);
70   process->memory_file = -1;
71   process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
72   process->heap = NULL;
73   process->heap_info = NULL;
74   MC_process_init_memory_map_info(process);
75   MC_process_open_memory_file(process);
76
77   // Read std_heap (is a struct mdesc*):
78   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
79   if (!std_heap_var)
80     xbt_die("No heap information in the target process");
81   if(!std_heap_var->address)
82     xbt_die("No constant address for this variable");
83   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
84     &process->heap_address, std_heap_var->address, sizeof(struct mdesc*),
85     MC_PROCESS_INDEX_DISABLED);
86
87   process->smx_process_infos = MC_smx_process_info_list_new();
88   process->smx_old_process_infos = MC_smx_process_info_list_new();
89
90   process->checkpoint_ignore = MC_checkpoint_ignore_new();
91
92   process->unw_addr_space = unw_create_addr_space(&mc_unw_accessors  , __BYTE_ORDER);
93   if (process->process_flags & MC_PROCESS_SELF_FLAG) {
94     process->unw_underlying_addr_space = unw_local_addr_space;
95     process->unw_underlying_context = NULL;
96   } else {
97     process->unw_underlying_addr_space = unw_create_addr_space(&mc_unw_vmread_accessors, __BYTE_ORDER);
98     process->unw_underlying_context = _UPT_create(pid);
99   }
100 }
101
102 void MC_process_clear(mc_process_t process)
103 {
104   process->address_space.address_space_class = NULL;
105   process->process_flags = MC_PROCESS_NO_FLAG;
106   process->pid = 0;
107
108   MC_free_memory_map(process->memory_map);
109   process->memory_map = NULL;
110
111   process->maestro_stack_start = NULL;
112   process->maestro_stack_end = NULL;
113
114   xbt_dynar_free(&process->checkpoint_ignore);
115
116   xbt_dynar_free(&process->smx_process_infos);
117   xbt_dynar_free(&process->smx_old_process_infos);
118
119   size_t i;
120   for (i=0; i!=process->object_infos_size; ++i) {
121     MC_free_object_info(&process->object_infos[i]);
122   }
123   free(process->object_infos);
124   process->object_infos = NULL;
125   process->object_infos_size = 0;
126   if (process->memory_file >= 0) {
127     close(process->memory_file);
128   }
129
130   if (process->unw_underlying_addr_space != unw_local_addr_space) {
131     unw_destroy_addr_space(process->unw_underlying_addr_space);
132     _UPT_destroy(process->unw_underlying_context);
133   }
134   process->unw_underlying_context = NULL;
135   process->unw_underlying_addr_space = NULL;
136
137   unw_destroy_addr_space(process->unw_addr_space);
138   process->unw_addr_space = NULL;
139
140   process->cache_flags = MC_PROCESS_CACHE_FLAG_NONE;
141
142   free(process->heap);
143   process->heap = NULL;
144
145   free(process->heap_info);
146   process->heap_info = NULL;
147 }
148
149 void MC_process_refresh_heap(mc_process_t process)
150 {
151   assert(!MC_process_is_self(process));
152   // Read/dereference/refresh the std_heap pointer:
153   if (!process->heap) {
154     xbt_mheap_t oldheap  = mmalloc_set_current_heap(mc_heap);
155     process->heap = (struct mdesc*) malloc(sizeof(struct mdesc));
156     mmalloc_set_current_heap(oldheap);
157   }
158   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
159     process->heap, process->heap_address, sizeof(struct mdesc),
160     MC_PROCESS_INDEX_DISABLED
161     );
162 }
163
164 void MC_process_refresh_malloc_info(mc_process_t process)
165 {
166   assert(!MC_process_is_self(process));
167   if (!(process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP))
168     MC_process_refresh_heap(process);
169   // Refresh process->heapinfo:
170   size_t malloc_info_bytesize =
171     (process->heap->heaplimit + 1) * sizeof(malloc_info);
172
173   xbt_mheap_t heap  = mmalloc_set_current_heap(mc_heap);
174   process->heap_info = (malloc_info*) realloc(process->heap_info, malloc_info_bytesize);
175   mmalloc_set_current_heap(heap);
176
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 }