https://github.com/teemperor/libmemintercept
Library for intercepting memory allocation functions (malloc, free, ...)
https://github.com/teemperor/libmemintercept
cpp free malloc malloc-free memory-allocation
Last synced: 10 months ago
JSON representation
Library for intercepting memory allocation functions (malloc, free, ...)
- Host: GitHub
- URL: https://github.com/teemperor/libmemintercept
- Owner: Teemperor
- Created: 2022-01-28T10:51:10.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2022-01-28T17:54:57.000Z (about 4 years ago)
- Last Synced: 2025-02-12T01:55:36.230Z (12 months ago)
- Topics: cpp, free, malloc, malloc-free, memory-allocation
- Language: C++
- Homepage:
- Size: 12.7 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# libmemintercept
Library for intercepting memory allocation functions (malloc, free, ...).
## Build instructions
```bash
# Create build directory.
mkdir build ; cd build
# Run CMake to setup build directory.
cmake .. -GNinja
# Build.
ninja
# Build and run tests.
ninja check
```
## How to use
See the `examples/` directory for compiling examples.
A minimal example is:
```cpp
#include "libmemintercept.h"
#include
// Your backend. See Memcallback's documentation for available hooks.
struct PrintFrees : libmemintercept::MemCallback {
void memoryAllocated(void *addr, std::size_t size) {}
void memoryReallocated(void *old_addr, void *new_addr,
std::size_t new_size) override {}
void memoryFreed(void *addr) override {
fprintf(stderr, "free(%p)\n", addr);
}
};
// Sets up hook.
LIBMEMINTERCEPT_ADD_CB(PrintFrees)
```
This needs to link against `libmemintercept` to work and should be compiled into a shared objetc.
To actually intercept a process do:
```bash
$ LD_PRELOAD=path/to/your.so binary_you_want_to_intercept
```