Saving gmon.out before killing a process

前端 未结 2 1190
日久生厌
日久生厌 2020-12-15 05:22

I would like to use gprof to profile a daemon. My daemon uses a 3rd party library, with which it registers some callbacks, then calls a main function, that neve

相关标签:
2条回答
  • 2020-12-15 05:47

    You could add a signal handler for a signal the third party library doesn't catch or ignore. Probably SIGUSR1 is good enough, but will either have to experiment or read the library's documentation—if it is thorough enough.

    Your signal handler can simply call exit().

    0 讨论(0)
  • 2020-12-15 05:51

    First, I would like to thank @wallyk for giving me good initial pointers. I solved my issue as follows. Apparently, libc's gprof exit handler is called _mcleanup. So, I registered a signal handler for SIGUSR1 (unused by the 3rd party library) and called _mcleanup and _exit. Works perfectly! The code looks as follows:

    #include <dlfcn.h>
    #include <stdio.h>
    #include <unistd.h>
    
    void sigUsr1Handler(int sig)
    {
        fprintf(stderr, "Exiting on SIGUSR1\n");
        void (*_mcleanup)(void);
        _mcleanup = (void (*)(void))dlsym(RTLD_DEFAULT, "_mcleanup");
        if (_mcleanup == NULL)
             fprintf(stderr, "Unable to find gprof exit hook\n");
        else _mcleanup();
        _exit(0);
    }
    
    int main(int argc, char* argv[])
    {
        signal(SIGUSR1, sigUsr1Handler);
        neverReturningLibraryFunction();
    }
    
    0 讨论(0)
提交回复
热议问题