Machine code monkey patching
yosefk.com
yosefk.com
Note that I also figure out, at runtime, how to call the real version of pthread_create using the dlsym interface. I make sure that my pthread_create is the first one in the call chain by putting that object file before -lpthread when I link.
The LD_PRELOAD method is for when you are not compiling the code yourself. That is, the function you want to hijack is in a binary, and you can't re-compile that binary. Then you compile a shared library with your function, and use LD_PRELOAD to force the runtime linker to load your library first.
For example, have header files for functions? Want to know when they're called? Monkey patch it in!
Also, from what I've heard monkey patching a function is calling adding a detour, and monkey patching a virtual function is called hooking. "Monkey patching" usually refers to injecting code into dynamic languages, not into compiled languages.