This is the mail archive of the
guile@sources.redhat.com
mailing list for the Guile project.
finding the top of the stack in linux
- To: guile at sourceware dot cygnus dot com
- Subject: finding the top of the stack in linux
- From: Chris Cramer <crayc at kiwi dot pyro dot net>
- Date: Mon, 17 Jul 2000 00:07:54 -0500
In a fit of boredom, I figured out how to find the top of the stack in
Linux. Maybe later I will integrate this into Guile to eliminate the "Guile
takes over main()" problem, for Linux users at least. Maybe someone
will beat me to it, as I have very little time lately. Commuting over
Lake Washington every morning sucks...
Uses the getline() function, so if you're still using libc5, you lose.
You need to #define STACK_GROWS_DOWN or STACK_GROWS_UP... actually, duh,
you can figure it out at runtime.. but, you get the idea.
--
C. Ray C. aka Christopher Cramer
crayc@pyro.net
http://www.pyro.net/~crayc/
#define _GNU_SOURCE
#include <stdio.h>
#include <string.h>
#define STACK_GROWS_DOWN /* i386 */
static void * find_stack(int dummy);
int
main(int argc, char **argv)
{
void *p;
printf("stack pointer is %p\n", &argc);
p = find_stack(0);
if (p == NULL) {
printf("stack not found\n");
} else {
printf("stack is at %p\n", p);
}
return 0;
}
static void *
find_stack(int dummy)
{
FILE *input;
char *line;
char *s;
size_t len;
char hex[9];
void *start;
void *end;
input = fopen("/proc/self/maps", "r");
if (input == NULL) return NULL;
len = 0;
line = NULL;
while (getline(&line, &len, input) != -1) {
s = strchr(line, '-');
if (s == NULL) return NULL;
*s++ = '\0';
start = (void *)strtoul(line, NULL, 16);
end = (void *)strtoul(s, NULL, 16);
if ((void *)&dummy >= start && (void *)&dummy <= end) {
free(line);
fclose(input);
#ifdef STACK_GROWS_DOWN
return end;
#else
# ifdef STACK_GROWS_UP
return start;
# else
# error "you need to #define either STACK_GROWS_DOWN or STACK_GROWS_UP"
# endif /* STACK_GROWS_UP */
#endif /* STACK_GROWS_DOWN */
}
}
free(line);
fclose(input);
return NULL; /* not found =^P */
}