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