Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Remove remaining bits on hardcoded object list
[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 "mc_process.h"
18 #include "mc_object_info.h"
19 #include "mc_address_space.h"
20
21 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
22                                 "MC process information");
23
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);
26
27 static s_mc_address_space_class_t mc_process_class = {
28   .read = (void*) &MC_process_read
29 };
30
31 void MC_process_init(mc_process_t process, pid_t pid)
32 {
33   process->address_space.address_space_class = &mc_process_class;
34   process->process_flags = MC_PROCESS_NO_FLAG;
35   process->pid = pid;
36   if (pid==getpid())
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;
41   process->heap = NULL;
42   process->heap_info = NULL;
43   MC_process_init_memory_map_info(process);
44   MC_process_open_memory_file(process);
45
46   // Read std_heap (is a struct mdesc*):
47   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
48   if (!std_heap_var)
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);
55 }
56
57 void MC_process_clear(mc_process_t process)
58 {
59   process->address_space.address_space_class = NULL;
60   process->process_flags = MC_PROCESS_NO_FLAG;
61   process->pid = 0;
62
63   MC_free_memory_map(process->memory_map);
64   process->memory_map = NULL;
65
66   process->maestro_stack_start = NULL;
67   process->maestro_stack_end = NULL;
68
69   size_t i;
70   for (i=0; i!=process->object_infos_size; ++i) {
71     MC_free_object_info(&process->object_infos[i]);
72   }
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);
78   }
79
80   process->cache_flags = 0;
81
82   free(process->heap);
83   process->heap = NULL;
84
85   free(process->heap_info);
86   process->heap_info = NULL;
87 }
88
89 void MC_process_refresh_heap(mc_process_t process)
90 {
91   assert(!MC_process_is_self(process));
92   // Read/dereference/refresh the std_heap pointer:
93   if (!process->heap) {
94     xbt_mheap_t oldheap  = mmalloc_get_current_heap();
95     MC_SET_MC_HEAP;
96     process->heap = malloc(sizeof(struct mdesc));
97     mmalloc_set_current_heap(oldheap);
98   }
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
102     );
103 }
104
105 void MC_process_refresh_malloc_info(mc_process_t process)
106 {
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);
112
113   xbt_mheap_t oldheap  = mmalloc_get_current_heap();
114   MC_SET_MC_HEAP;
115   process->heap_info = (malloc_info*) realloc(process->heap_info,
116     malloc_info_bytesize);
117   mmalloc_set_current_heap(oldheap);
118
119   MC_process_read(process, MC_ADDRESS_SPACE_READ_FLAGS_NONE,
120     process->heap_info,
121     process->heap->heapinfo, malloc_info_bytesize,
122     MC_PROCESS_INDEX_DISABLED);
123 }
124
125 #define SO_RE "\\.so[\\.0-9]*$"
126 #define VERSION_RE "-[\\.0-9]*$"
127
128 const char* FILTERED_LIBS[] = {
129   "libstdc++",
130   "libc++",
131   "libm",
132   "libgcc_s",
133   "libpthread",
134   "libunwind",
135   "libunwind-x86_64",
136   "libunwind-x86",
137   "libdw",
138   "libdl",
139   "librt",
140   "liblzma",
141   "libelf",
142   "libbz2",
143   "libz",
144   "libelf",
145   "libc",
146   "ld"
147 };
148
149 static bool MC_is_simgrid_lib(const char* libname)
150 {
151   return !strcmp(libname, "libsimgrid");
152 }
153
154 static bool MC_is_filtered_lib(const char* libname)
155 {
156   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
157   size_t i;
158   for (i=0; i!=n; ++i)
159     if (strcmp(libname, FILTERED_LIBS[i])==0)
160       return true;
161   return false;
162 }
163
164 struct s_mc_memory_map_re {
165   regex_t so_re;
166   regex_t version_re;
167 };
168
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);
171
172   regmatch_t match;
173   if(regexec(&res->so_re, map_basename, 1, &match, 0))
174     return NULL;
175
176   char* libname = strndup(map_basename, match.rm_so);
177
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);
182     free(temp);
183   }
184
185   return libname;
186 }
187
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)
190 {
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;
198
199   struct s_mc_memory_map_re res;
200
201   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
202     xbt_die(".so regexp did not compile");
203
204   memory_map_t maps = process->memory_map;
205
206   const char* current_name = NULL;
207
208   size_t i = 0;
209   for (i=0; i < maps->mapsize; i++) {
210     map_region_t reg = &(maps->regions[i]);
211     const char* pathname = maps->regions[i].pathname;
212
213     // Nothing to do
214     if (maps->regions[i].pathname == NULL) {
215       current_name = NULL;
216       continue;
217     }
218
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;
224       }
225       current_name = NULL;
226       continue;
227     }
228
229     if (current_name && strcmp(current_name, pathname)==0)
230       continue;
231
232     current_name = pathname;
233     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
234       continue;
235
236     const bool is_executable = !i;
237     char* libname = NULL;
238     if (!is_executable) {
239       libname = MC_get_lib_name(pathname, &res);
240       if(!libname)
241         continue;
242       if (MC_is_filtered_lib(libname)) {
243         free(libname);
244         continue;
245       }
246     }
247
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++;
254     if (is_executable)
255       process->binary_info = info;
256     else if (libname && MC_is_simgrid_lib(libname))
257       process->libsimgrid_info = info;
258     free(libname);
259   }
260
261   regfree(&res.so_re);
262   regfree(&res.version_re);
263
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]);
267
268   xbt_assert(process->maestro_stack_start, "maestro_stack_start");
269   xbt_assert(process->maestro_stack_end, "maestro_stack_end");
270
271   XBT_INFO("Get debug information done !");
272 }
273
274 mc_object_info_t MC_process_find_object_info(mc_process_t process, const void *addr)
275 {
276   size_t i;
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];
281     }
282   }
283   return NULL;
284 }
285
286 mc_object_info_t MC_process_find_object_info_exec(mc_process_t process, const void *addr)
287 {
288   size_t i;
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];
293     }
294   }
295   return NULL;
296 }
297
298 mc_object_info_t MC_process_find_object_info_rw(mc_process_t process, const void *addr)
299 {
300   size_t i;
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];
305     }
306   }
307   return NULL;
308 }
309
310 // Functions, variables…
311
312 dw_frame_t MC_process_find_function(mc_process_t process, const void *ip)
313 {
314   mc_object_info_t info = MC_process_find_object_info_exec(process, ip);
315   if (info == NULL)
316     return NULL;
317   else
318     return MC_file_object_info_find_function(info, ip);
319 }
320
321 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
322 {
323   const size_t n = process->object_infos_size;
324   size_t i;
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);
328     if (var)
329       return var;
330   }
331   return NULL;
332 }
333
334 // ***** Memory access
335
336 static void MC_process_open_memory_file(mc_process_t process)
337 {
338   if (MC_process_is_self(process) || process->memory_file >= 0)
339     return;
340
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);
347     return;
348   }
349
350   int fd = open(buffer, O_RDWR);
351   if (fd<0)
352     xbt_die("Could not initialise memory access for remote process");
353   process->memory_file = fd;
354 }
355
356 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
357 {
358   char* buffer = (char*) buf;
359   ssize_t real_count = count;
360   while (count) {
361     ssize_t res = pread(fd, buffer, count, offset);
362     if (res > 0) {
363       count  -= res;
364       buffer += res;
365       offset += res;
366     } else if (res==0) {
367       return -1;
368     } else if (errno != EINTR) {
369       return -1;
370     }
371   }
372   return real_count;
373 }
374
375 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
376 {
377   const char* buffer = (const char*) buf;
378   ssize_t real_count = count;
379   while (count) {
380     ssize_t res = pwrite(fd, buffer, count, offset);
381     if (res > 0) {
382       count  -= res;
383       buffer += res;
384       offset += res;
385     } else if (res==0) {
386       return -1;
387     } else if (errno != EINTR) {
388       return -1;
389     }
390   }
391   return real_count;
392 }
393
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,
396   int process_index)
397 {
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;
407     }
408   }
409
410   if (MC_process_is_self(process)) {
411     if (flags & MC_ADDRESS_SPACE_READ_FLAGS_LAZY)
412       return remote;
413     else {
414       memcpy(local, remote, len);
415       return local;
416     }
417   } else {
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);
420     return local;
421   }
422 }
423
424 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
425 {
426   if (MC_process_is_self(process)) {
427     memcpy(remote, local, len);
428   } else {
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);
431   }
432 }
433
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;
437
438 static void MC_zero_buffer_init(void)
439 {
440   int fd = open("/dev/zero", O_RDONLY);
441   if (fd<0)
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");
446   close(fd);
447 }
448
449 void MC_process_clear_memory(mc_process_t process, void* remote, size_t len)
450 {
451   if (MC_process_is_self(process)) {
452     memset(remote, 0, len);
453   } else {
454     pthread_once(&zero_buffer_flag, MC_zero_buffer_init);
455     while (len) {
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;
459       len -= s;
460     }
461   }
462 }