The program demonstrates how to intercept (“hook”) a C function at runtime with Frida Gum.
install_hook() replaces the original func() with my_func(), while keeping a pointer (orig_func) so the genuine implementation can still be called.
After the test call, remove_hook() cleans everything up and de-initialises Frida.
We need frida-gum-devkit. Please access frida/releases.
Build with gcc:
gcc main.c -I./frida -L./frida -lfrida-gum
| Step | What happens | Key API calls |
|---|---|---|
| 1. Initialise | gum_init_embedded() embeds Frida into the current process and prepares Gum. |
gum_init_embedded |
| 2. Obtain interceptor | A GumInterceptor object manages all hook transactions. |
gum_interceptor_obtain |
| 3. Begin transaction | Hooks are grouped in a transactional block for safety. | gum_interceptor_begin_transaction |
| 4. Replace target | func is replaced with my_func; Frida stores a thunk to the original code in orig_func. |
gum_interceptor_replace |
| 5. Commit | Ends the transaction, activating the hook. | gum_interceptor_end_transaction |
| 6. Call test | func() now executes my_func(); that, in turn, invokes orig_func() so original behaviour still runs. |
— |
| 7. Remove hook | The replacement is reverted, all resources freed, and Frida de-initialises. | gum_interceptor_revert, gum_deinit_embedded |
-
Attach instead of replace Use
gum_interceptor_attach()when you want to run pre- or post-handlers without replacing the function entirely. -
Pass user data The
replacement_dataslot can carry a custom struct for state sharing between the interceptor and its callbacks. -
Hook multiple functions Begin a single transaction, call
gum_interceptor_replace()(orattach()) for each target, then end the transaction; this keeps the program in a consistent state. -
Cross-platform builds Frida Gum works on Linux, macOS, Windows, iOS, and Android. Condition-compile the
gccflags and Frida library path for portability. -
Error handling Instead of
exit(), propagate errors or throw exceptions so that the host program can decide how to continue.
These options let you adapt the basic template to more sophisticated instrumentation tasks while keeping the core hooking logic unchanged.