ericherman / ld-preload-example Goto Github PK
View Code? Open in Web Editor NEWHow to use dynamic loading to wrap library functions like "malloc" and "free"
License: GNU Lesser General Public License v3.0
How to use dynamic loading to wrap library functions like "malloc" and "free"
License: GNU Lesser General Public License v3.0
ld-preload-example This example demostrates how to use dynamic loading to wrap library functions. demo-dlsym.c shows a simple usage of the "dlsym" function to obtain references to functions and then call them. leak.c: is a small program which calls malloc and free, and leaks memory. tracking-malloc.c: implements wrapper versions of malloc and free, and tracks the number of bytes allocated and freed. The leak program can be build directly using the tracking-malloc.o file: gcc -c -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc.o \ tracking-malloc.c -ldl gcc -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc-leak \ tracking-malloc.o leak.c -ldl ./tracking-malloc-leak Alternatively, on linux we can use LD_LIBRARY_PATH and LD_PRELOAD to make the leak program call the wrapper versions of malloc/free: gcc -std=c89 -g -Wall -Wextra -Werror -o leak leak.c gcc -std=c89 -g -Wall -Wextra -Werror -rdynamic -fPIC -shared \ -o libtracking-malloc.so tracking-malloc.c -ldl TRACKING_MALLOC_ENABLE=1 \ LD_LIBRARY_PATH="." \ LD_PRELOAD=libtracking-malloc.so \ ./leak ============================================================================= eric@dione:~/src/ld-preload-example$ make leak gcc -std=c89 -g -Wall -Wextra -Werror -o leak leak.c eric@dione:~/src/ld-preload-example$ eric@dione:~/src/ld-preload-example$ ./leak eric@dione:~/src/ld-preload-example$ eric@dione:~/src/ld-preload-example$ valgrind ./leak ==2425== Memcheck, a memory error detector ==2425== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al. ==2425== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info ==2425== Command: ./leak ==2425== ==2425== ==2425== HEAP SUMMARY: ==2425== in use at exit: 42 bytes in 1 blocks ==2425== total heap usage: 6 allocs, 5 frees, 380 bytes allocated ==2425== ==2425== LEAK SUMMARY: ==2425== definitely lost: 42 bytes in 1 blocks ==2425== indirectly lost: 0 bytes in 0 blocks ==2425== possibly lost: 0 bytes in 0 blocks ==2425== still reachable: 0 bytes in 0 blocks ==2425== suppressed: 0 bytes in 0 blocks ==2425== Rerun with --leak-check=full to see details of leaked memory ==2425== ==2425== For counts of detected and suppressed errors, rerun with: -v ==2425== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0) eric@dione:~/src/ld-preload-example$ eric@dione:~/src/ld-preload-example$ make tracking-malloc-leak gcc -c -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc.o tracking-malloc.c -ldl gcc -std=c89 -g -Wall -Wextra -Werror -o tracking-malloc-leak tracking-malloc.o leak.c -ldl eric@dione:~/src/ld-preload-example$ ./tracking-malloc-leak eric@dione:~/src/ld-preload-example$ TRACKING_MALLOC_ENABLE=1 ./tracking-malloc-leak + 42 - 42 + 126 - 126 + 42 + 42 - 42 + 43 - 43 eric@dione:~/src/ld-preload-example$ eric@dione:~/src/ld-preload-example$ make libtracking-malloc.so gcc -std=c89 -g -Wall -Wextra -Werror -rdynamic -fPIC -shared -o libtracking-malloc.so tracking-malloc.c -ldl eric@dione:~/src/ld-preload-example$ LD_LIBRARY_PATH="." LD_PRELOAD=libtracking-malloc.so ./leak eric@dione:~/src/ld-preload-example$ TRACKING_MALLOC_ENABLE=1 \ > LD_LIBRARY_PATH="." LD_PRELOAD=libtracking-malloc.so ./leak + 42 - 42 + 126 - 126 + 42 + 42 - 42 + 43 - 43 eric@dione:~/src/ld-preload-example$
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.