You need to look into ptrace.
ptrace() provides tracing and debugging facilities. It allows one process (the tracing process) to control another (the traced process).
You can use PTRACE_ATTACH
to start tracing, PTRACE_GETREGS
(SETREGS) to get/set registers, PTRACE_PEEKUSER
(POKEUSER) to read/write user data and PTRACE_PEEKDATA
(POKEDATA) to read/write data/code sections.