本文介绍了在终止进程之前保存gmon.out的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用gprof来分析守护程序.我的守护程序使用第3方库,通过它注册一些回调,然后调用main函数,该函数永不返回.我需要调用kill(SIGTERM或SIGKILL)来终止守护程序.不幸的是,gprof的手册页显示以下内容:

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 never returns. I need to call kill (either SIGTERM or SIGKILL) to terminate the daemon. Unfortunately, gprof's manual page says the following:

是否可以为被SIGTERM或SIGKILL杀死的进程保存性能分析信息?

Is there is way to save profiling information for processes which are killed with SIGTERM or SIGKILL ?

推荐答案

首先,我要感谢@wallyk为我提供了很好的初始指针.我如下解决了我的问题.显然,libc的gprof退出处理程序称为_mcleanup.因此,我为SIGUSR1注册了一个信号处理程序(未由第三方库使用),并分别命名为_mcleanup_exit.完美的作品!代码如下:

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();
}

这篇关于在终止进程之前保存gmon.out的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-27 12:53