Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mono / utils / mono-threads-android.c
1 /**
2  * \file
3  */
4
5 #include <config.h>
6
7 #if defined(HOST_ANDROID)
8
9 #include <pthread.h>
10 #include <stdio.h>
11 #include <inttypes.h>
12 #include "glib.h"
13
14 static void
15 slow_get_thread_bounds (guint8 *current, guint8 **staddr, size_t *stsize)
16 {
17         char buff [1024];
18         FILE *f = fopen ("/proc/self/maps", "r");
19         if (!f)
20                 g_error ("Could not determine thread bounds, failed to open /proc/self/maps");
21
22         while (fgets (buff, sizeof (buff), f)) {
23                 intmax_t low, high;
24                 char *ptr = buff;
25                 char *end = NULL;
26                 //each line starts with the range we want: f7648000-f7709000
27                 low = strtoimax (ptr, &end, 16);
28                 if (end) {
29                         ptr = end + 1; //skip the dash to make sure we don't get a negative number
30                         end = NULL;
31                         high = strtoimax (ptr, &end, 16);
32                 }
33                 if (end && low <= (intmax_t)(size_t)current && high > (intmax_t)(size_t)current) {
34                         *staddr = (guint8 *)(size_t)low;
35                         *stsize = (size_t)(high - low);
36                         fclose (f);
37                         return;
38                 }
39         }
40         g_error ("Could not determine thread bounds, failed to find current stack pointer in /proc/self/maps");
41 }
42
43 void
44 mono_threads_platform_get_stack_bounds (guint8 **staddr, size_t *stsize)
45 {
46         pthread_attr_t attr;
47         guint8 *current = (guint8*)&attr;
48
49         *staddr = NULL;
50         *stsize = (size_t)-1;
51
52         pthread_getattr_np (pthread_self (), &attr);
53         pthread_attr_getstack (&attr, (void**)staddr, stsize);
54         pthread_attr_destroy (&attr);
55
56         if (*staddr && ((current <= *staddr) || (current > *staddr + *stsize)))
57                 slow_get_thread_bounds (current, staddr, stsize);
58 }
59
60 #endif