I want to create a linux shell with overridden malloc() i.e. malloc_hook(). My terminal should take an .exe file as input and run the program as a thread. Whenever there's a malloc() call in the program, terminal's malloc_hook() should be called eventually. How can I achieve this goal. I have gone through a tutorial to write my own Linux terminal link : https://abhijangda.wordpress.com/2013/04/14/creating-a-simple-linux-terminal-in-c/ but that wasn't just enough.
-
This is remarkably similar to http://stackoverflow.com/q/35382048/1566221 – rici Feb 13 '16 at 17:51
-
Yes, the person who posted it is my group member – SEGrad2K13 Feb 13 '16 at 18:11
-
By "terminal" do you really mean "shell"? – John Hascall Feb 13 '16 at 18:11
-
@John Hascall Yes, a shell – SEGrad2K13 Feb 13 '16 at 18:13
-
Hmmm, that link *is* actually about writing a terminal. In a windowed system, the **terminal** is the visible part that shows the characters and takes the keyboard input and so on. The terminal runs a single program. Almost always this single program is a **shell** (like *bash* or similar). The shell, is what interprets your "command lines" and runs other programs. – John Hascall Feb 13 '16 at 18:21
-
Right, I just edited my question. – SEGrad2K13 Feb 13 '16 at 18:23
1 Answers
Using malloc_hook
does not have much to do with the Linux terminal. You can look at the man page for malloc_hook
to try and understand how it works. but it seems to have been deprecated in recent versions of the glibc:
http://man7.org/linux/man-pages/man3/malloc_hook.3.html
You can also read the manual page for mallopt
for other ways to tweak the allocation system:
http://man7.org/linux/man-pages/man3/mallopt.3.html
If the program is yours, use malloc_hook
as documented above.
If you cannot recompile the program, you may achieve your goal by making a library that replaces all of the malloc
family of functions and dynamically link the program with this library before the C library, using LD_PRELOAD
as documented here:
http://man7.org/linux/man-pages/man8/ld.so.8.html
And as discussed in this question: What is the LD_PRELOAD trick?