有undefined reference to puts, 所以看起来得用-shared
gcc dg.c -c -o dg
ld dg -o dg.so.1 -soname dg.so.1 -shared
export LD_PRELOAD=$PWD/dg.so.1
#include "stdio.h"
void _init(void)
{
printf("welcome\n");
}
void _fini(void)
{
printf("byebye\n");
}
the above can get the preloaded library working properly. very cool, next let me try the mtrace function.
export MALLOC_TRACE=$PWD/mem.txt
need to include "mcheck.h" file
mtrace();
for (int i=50; i<5000; i+=50)
{
p=malloc(i);
memset((void*)p,0,i);
free(p);
}
for (int i=1000; i<5000; i+=1000)
{
p=malloc(i);
memset((void*)p,0,i);
}
muntrace();
this works fine and correctly. the result is like the following:
= Start
@ ./callit:[0x804855c] + 0x84fd378 0x32
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd3b0 0x64
@ ./callit:[0x8048588] - 0x84fd3b0
@ ./callit:[0x804855c] + 0x84fd378 0x96
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd378 0xc8
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd378 0xfa
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd378 0x12c
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd378 0x15e
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd378 0x190
@ ./callit:[0x8048588] - 0x84fd378
@ ./callit:[0x804855c] + 0x84fd378 0x1c2