If you're interested in implementing your own user-level-threads library, I'd suggest looking into the (now deprecated) ucontext
implementation. Specifically, looking at the definitions for the structs used in ucontext.h
will help you see all the stuff you actually need to capture to get a valid snapshot of the thread state.
What you're really trying to capture with the erroneous (address_t)
cast in your example is the current continuation. Unfortunately, C doesn't support first-class continuations, so you're going to be stuck doing something much more low-level, like swapping stacks and dumping registers (hence why I pointed you to ucontext
as a reference—it's going to be kind of complicated if you really want to get this right).