Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[mc] Support for reading heap state from another process
[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 <libgen.h>
14
15 #include "mc_process.h"
16 #include "mc_object_info.h"
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(mc_process, mc,
19                                 "MC process information");
20
21 static void MC_process_init_memory_map_info(mc_process_t process);
22 static void MC_process_open_memory_file(mc_process_t process);
23
24
25 void MC_process_init(mc_process_t process, pid_t pid)
26 {
27   process->process_flags = MC_PROCESS_NO_FLAG;
28   process->pid = pid;
29   if (pid==getpid())
30     process->process_flags |= MC_PROCESS_SELF_FLAG;
31   process->memory_map = MC_get_memory_map(pid);
32   process->memory_file = -1;
33   process->cache_flags = 0;
34   process->heap = NULL;
35   process->heap_info = NULL;
36   MC_process_init_memory_map_info(process);
37   MC_process_open_memory_file(process);
38
39   // Read std_heap (is a struct mdesc*):
40   dw_variable_t std_heap_var = MC_process_find_variable_by_name(process, "std_heap");
41   if (!std_heap_var)
42     xbt_die("No heap information in the target process");
43   if(!std_heap_var->address)
44     xbt_die("No constant address for this variable");
45   MC_process_read(process, &process->heap_address,
46     std_heap_var->address, sizeof(struct mdesc*));
47 }
48
49 void MC_process_clear(mc_process_t process)
50 {
51   process->process_flags = MC_PROCESS_NO_FLAG;
52   process->pid = 0;
53
54   MC_free_memory_map(process->memory_map);
55   process->memory_map = NULL;
56
57   process->maestro_stack_start = NULL;
58   process->maestro_stack_end = NULL;
59
60   size_t i;
61   for (i=0; i!=process->object_infos_size; ++i) {
62     MC_free_object_info(&process->object_infos[i]);
63   }
64   free(process->object_infos);
65   process->object_infos = NULL;
66   process->object_infos_size = 0;
67   if (process->memory_file >= 0) {
68     close(process->memory_file);
69   }
70
71   process->cache_flags = 0;
72
73   free(process->heap);
74   process->heap = NULL;
75
76   free(process->heap_info);
77   process->heap_info = NULL;
78 }
79
80 void MC_process_refresh_heap(mc_process_t process)
81 {
82   assert(!MC_process_is_self(process));
83   // Read/dereference/refresh the std_heap pointer:
84   if (!process->heap) {
85     xbt_mheap_t oldheap  = mmalloc_get_current_heap();
86     MC_SET_MC_HEAP;
87     process->heap = malloc(sizeof(struct mdesc));
88     mmalloc_set_current_heap(oldheap);
89   }
90   MC_process_read(process, process->heap,
91     process->heap_address, sizeof(struct mdesc));
92 }
93
94 void MC_process_refresh_malloc_info(mc_process_t process)
95 {
96   assert(!MC_process_is_self(process));
97   if (!process->cache_flags & MC_PROCESS_CACHE_FLAG_HEAP)
98     MC_process_refresh_heap(process);
99   // Refresh process->heapinfo:
100   size_t malloc_info_bytesize = process->heap->heaplimit * sizeof(malloc_info);
101
102   xbt_mheap_t oldheap  = mmalloc_get_current_heap();
103   MC_SET_MC_HEAP;
104   process->heap_info = (malloc_info*) realloc(process->heap_info,
105     malloc_info_bytesize);
106   mmalloc_set_current_heap(oldheap);
107
108   MC_process_read(process, process->heap_info,
109     process->heap->heapinfo, malloc_info_bytesize);
110 }
111
112 #define SO_RE "\\.so[\\.0-9]*$"
113 #define VERSION_RE "-[\\.0-9]*$"
114
115 const char* FILTERED_LIBS[] = {
116   "libstdc++",
117   "libc++",
118   "libm",
119   "libgcc_s",
120   "libpthread",
121   "libunwind",
122   "libunwind-x86_64",
123   "libunwind-x86",
124   "libdw",
125   "libdl",
126   "librt",
127   "liblzma",
128   "libelf",
129   "libbz2",
130   "libz",
131   "libelf",
132   "libc",
133   "ld"
134 };
135
136 static bool MC_is_simgrid_lib(const char* libname)
137 {
138   return !strcmp(libname, "libsimgrid");
139 }
140
141 static bool MC_is_filtered_lib(const char* libname)
142 {
143   const size_t n = sizeof(FILTERED_LIBS) / sizeof(const char*);
144   size_t i;
145   for (i=0; i!=n; ++i)
146     if (strcmp(libname, FILTERED_LIBS[i])==0)
147       return true;
148   return false;
149 }
150
151 struct s_mc_memory_map_re {
152   regex_t so_re;
153   regex_t version_re;
154 };
155
156 static char* MC_get_lib_name(const char* pathname, struct s_mc_memory_map_re* res) {
157   const char* map_basename = basename((char*) pathname);
158
159   regmatch_t match;
160   if(regexec(&res->so_re, map_basename, 1, &match, 0))
161     return NULL;
162
163   char* libname = strndup(map_basename, match.rm_so);
164
165   // Strip the version suffix:
166   if(libname && !regexec(&res->version_re, libname, 1, &match, 0)) {
167     char* temp = libname;
168     libname = strndup(temp, match.rm_so);
169     free(temp);
170   }
171
172   return libname;
173 }
174
175 /** @brief Finds the range of the different memory segments and binary paths */
176 static void MC_process_init_memory_map_info(mc_process_t process)
177 {
178   XBT_INFO("Get debug information ...");
179   process->maestro_stack_start = NULL;
180   process->maestro_stack_end = NULL;
181   process->object_infos = NULL;
182   process->object_infos_size = 0;
183   process->binary_info = NULL;
184   process->libsimgrid_info = NULL;
185
186   struct s_mc_memory_map_re res;
187
188   if(regcomp(&res.so_re, SO_RE, 0) || regcomp(&res.version_re, VERSION_RE, 0))
189     xbt_die(".so regexp did not compile");
190
191   memory_map_t maps = process->memory_map;
192
193   const char* current_name = NULL;
194
195   size_t i = 0;
196   for (i=0; i < maps->mapsize; i++) {
197     map_region_t reg = &(maps->regions[i]);
198     const char* pathname = maps->regions[i].pathname;
199
200     // Nothing to do
201     if (maps->regions[i].pathname == NULL) {
202       current_name = NULL;
203       continue;
204     }
205
206     // [stack], [vvar], [vsyscall], [vdso] ...
207     if (pathname[0] == '[') {
208       if ((reg->prot & PROT_WRITE) && !memcmp(pathname, "[stack]", 7)) {
209         process->maestro_stack_start = reg->start_addr;
210         process->maestro_stack_end = reg->end_addr;
211       }
212       current_name = NULL;
213       continue;
214     }
215
216     if (current_name && strcmp(current_name, pathname)==0)
217       continue;
218
219     current_name = pathname;
220     if (!(reg->prot & PROT_READ) && (reg->prot & PROT_EXEC))
221       continue;
222
223     const bool is_executable = !i;
224     char* libname = NULL;
225     if (!is_executable) {
226       libname = MC_get_lib_name(pathname, &res);
227       if(!libname)
228         continue;
229       if (MC_is_filtered_lib(libname)) {
230         free(libname);
231         continue;
232       }
233     }
234
235     mc_object_info_t info =
236       MC_find_object_info(process->memory_map, pathname, is_executable);
237     process->object_infos = (mc_object_info_t*) realloc(process->object_infos,
238       (process->object_infos_size+1) * sizeof(mc_object_info_t*));
239     process->object_infos[process->object_infos_size] = info;
240     process->object_infos_size++;
241     if (is_executable)
242       process->binary_info = info;
243     else if (libname && MC_is_simgrid_lib(libname))
244       process->libsimgrid_info = info;
245     free(libname);
246   }
247
248   regfree(&res.so_re);
249   regfree(&res.version_re);
250
251   // Resolve time (including accress differents objects):
252   for (i=0; i!=process->object_infos_size; ++i)
253     MC_post_process_object_info(process, process->object_infos[i]);
254
255   xbt_assert(process->maestro_stack_start, "maestro_stack_start");
256   xbt_assert(process->maestro_stack_end, "maestro_stack_end");
257
258   XBT_INFO("Get debug information done !");
259 }
260
261 mc_object_info_t MC_process_find_object_info(mc_process_t process, void *ip)
262 {
263   size_t i;
264   for (i = 0; i != process->object_infos_size; ++i) {
265     if (ip >= (void *) process->object_infos[i]->start_exec
266         && ip <= (void *) process->object_infos[i]->end_exec) {
267       return process->object_infos[i];
268     }
269   }
270   return NULL;
271 }
272
273 // Functions, variables…
274
275 dw_frame_t MC_process_find_function(mc_process_t process, void *ip)
276 {
277   mc_object_info_t info = MC_process_find_object_info(process, ip);
278   if (info == NULL)
279     return NULL;
280   else
281     return MC_file_object_info_find_function(info, ip);
282 }
283
284 dw_variable_t MC_process_find_variable_by_name(mc_process_t process, const char* name)
285 {
286   const size_t n = process->object_infos_size;
287   size_t i;
288   for (i=0; i!=n; ++i) {
289     mc_object_info_t info =process->object_infos[i];
290     dw_variable_t var = MC_file_object_info_find_variable_by_name(info, name);
291     if (var)
292       return var;
293   }
294   return NULL;
295 }
296
297 // ***** Memory access
298
299 static void MC_process_open_memory_file(mc_process_t process)
300 {
301   if (MC_process_is_self(process) || process->memory_file >= 0)
302     return;
303
304   const size_t buffer_size = 30;
305   char buffer[buffer_size];
306   int res = snprintf(buffer, buffer_size, "/proc/%lli/mem", (long long) process->pid);
307   if (res < 0 || res>= buffer_size) {
308     XBT_ERROR("Could not open memory file descriptor for process %lli",
309       (long long) process->pid);
310     return;
311   }
312
313   int fd = open(buffer, O_RDWR);
314   if (fd<0)
315     xbt_die("Could not initialise memory access for remote process");
316   process->memory_file = fd;
317 }
318
319 static ssize_t pread_whole(int fd, void *buf, size_t count, off_t offset)
320 {
321   char* buffer = (char*) buf;
322   ssize_t real_count = count;
323   while (count) {
324     ssize_t res = pread(fd, buffer, count, offset);
325     if (res > 0) {
326       count  -= res;
327       buffer += res;
328       offset += res;
329     } else if (res==0) {
330       return -1;
331     } else if (errno != EINTR) {
332       return -1;
333     }
334   }
335   return real_count;
336 }
337
338 static ssize_t pwrite_whole(int fd, const void *buf, size_t count, off_t offset)
339 {
340   const char* buffer = (const char*) buf;
341   ssize_t real_count = count;
342   while (count) {
343     ssize_t res = pwrite(fd, buffer, count, offset);
344     if (res > 0) {
345       count  -= res;
346       buffer += res;
347       offset += res;
348     } else if (res==0) {
349       return -1;
350     } else if (errno != EINTR) {
351       return -1;
352     }
353   }
354   return real_count;
355 }
356
357 void MC_process_read(mc_process_t process, void* local, const void* remote, size_t len)
358 {
359   if (MC_process_is_self(process)) {
360     memcpy(local, remote, len);
361   } else {
362     if (pread_whole(process->memory_file, local, len, (off_t) remote) < 0)
363       xbt_die("Read from process %lli failed", (long long) process->pid);
364   }
365 }
366
367 void MC_process_write(mc_process_t process, const void* local, void* remote, size_t len)
368 {
369   if (MC_process_is_self(process)) {
370     memcpy(remote, local, len);
371   } else {
372     if (pwrite_whole(process->memory_file, local, len, (off_t) remote) < 0)
373       xbt_die("Write to process %lli failed", (long long) process->pid);
374   }
375 }