alexandermerritt/malloc-trace

Can time functions be used in your code?

Closed this issue · 2 comments

FIrst of all I'd like to say that the code is amazing and works perfectly! However, I'd like to print time of each malloc operation. I tried calling this function from inside your malloc implementation:

static void printTime()
{
  time_t timer;
    char buffer[26];
    struct tm* tm_info;

    time(&timer);
    tm_info = localtime(&timer);

    strftime(buffer, 26, "%Y-%m-%d %H:%M:%S", tm_info);
    fprintf(stderr, "%s\n", buffer);
}

however when I execute my executable with your code compiled as .so using LD_PRELOAD, my executable (/bin/ps utility for example) freezes and doesn't work.

I suspect that time functions call malloc under the hood. Do you think this could be the problem?

Indeed the issue was that time functions also use malloc. So I had recursive calls to malloc which probably resulted in stack overflow. One approach to fix that is to maintain some kind of boolean variable which tells us whether it's the malloc call from time or from our executable. This is explained here.

Yep that's indeed the issue. Printing within the trace capture I found mostly useful for debugging, but not for actually running the trace capture, because string formatting and such carry a lot of overhead. That's why it is separated into two phases - capture / record, then read out + convert to text.