[System] Authorization from referencesource
[mono-project.git] / mono / utils / mono-threads-android.c
blobfa82387f26ff8e01f26dfc12874eeea0a91d56ab
1 #include <config.h>
3 #if defined(PLATFORM_ANDROID)
5 #include <pthread.h>
6 #include <stdio.h>
7 #include <inttypes.h>
8 #include "glib.h"
10 static void
11 slow_get_thread_bounds (guint8 *current, guint8 **staddr, size_t *stsize)
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");
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);
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;
36 g_error ("Could not determine thread bounds, failed to find current stack pointer in /proc/self/maps");
39 void
40 mono_threads_core_get_stack_bounds (guint8 **staddr, size_t *stsize)
42 pthread_attr_t attr;
43 guint8 *current = (guint8*)&attr;
45 *staddr = NULL;
46 *stsize = (size_t)-1;
48 pthread_getattr_np (pthread_self (), &attr);
49 pthread_attr_getstack (&attr, (void**)staddr, stsize);
50 pthread_attr_destroy (&attr);
52 if (*staddr && ((current <= *staddr) || (current > *staddr + *stsize)))
53 slow_get_thread_bounds (current, staddr, stsize);
56 #endif