Reading and writing the Stack

Gordon Cross crossgl at ingr.UUCP
Wed Nov 9 05:16:41 AEST 1988


In article <163 at snipe-lake.UUCP>, anthony at alberta.UUCP (Anthony Mutiso) writes:
> 
> Any one have any ideas how one can do the following in C:-
> 
> 	(1) find the bottom and the top of a processes stack.
> 	(2) read the information on stack.
> 	(3) copy the stack to disk ala core dump, as a way of saving the
> 	    process context.
> 	(4) write to the stack (i.e grow the stack and place return addresses
>           and automatic variables (initialized), etc, on it.

All of these things can be done but you will have to code assembly routines
in all likelyhood.  The top of the stack (assuming your machine uses a stack)
is kept in a special register called the stack pointer.  The "bottom" you may
have some trouble with.

> I would like to prime a processes stack, so a process can begin from
> somewhere in the middle of its address space (in some special function,
> rather than the in the main) and unwind the stack in the usual way when
> the special function returns)

I'm not quite sure what it is you are wanting to do here.  A process does not
actually begin in main but in a special entry point routine usually called
"start".  This routine is responsible for certain initializations required for
your process to run correctly.  It is possible to specify an alternate entry
point using the -e option of ld(1) but then this initialization must be done
by you!  I can't think of any reason for wanting to do this but you never know.
Good luck!


Gordon Cross
Intergraph Corp.  Huntsville, AL



More information about the Comp.lang.c mailing list