if (realTime) { sigaction(SIGALRM, &action, NULL); } else { sigaction(SIGPROF, &action, NULL); }
void setupProfilingStuff(void){    static int gFirstTime = 1;    if(gFirstTime && !(gFirstTime=0)) {	int  startTimer = 1;	int  doNotStart = 1;	int  firstDelay = 0;        int  append = O_TRUNC;        char *tst  = getenv("JPROF_FLAGS");	/* Options from JPROF_FLAGS environment variable:	 *   JP_DEFER  -> Wait for a SIGPROF (or SIGALRM, if JP_REALTIME	 *               is set) from userland before starting	 *               to generate them internally	 *   JP_START  -> Install the signal handler	 *   JP_PERIOD -> Time between profiler ticks	 *   JP_FIRST  -> Extra delay before starting	 *   JP_REALTIME -> Take stack traces in intervals of real time	 *               rather than time used by the process (and the	 *               system for the process).  This is useful for	 *               finding time spent by the X server.         *   JP_APPEND -> Append to jprof-log rather than overwriting it.         *               This is somewhat risky since it depends on the         *               address map staying constant across multiple runs.	*/	if(tst) {	    if(strstr(tst, "JP_DEFER"))	    {		doNotStart = 0;		startTimer = 0;	    }	    if(strstr(tst, "JP_START")) doNotStart = 0;	    if(strstr(tst, "JP_REALTIME")) realTime = 1;	    if(strstr(tst, "JP_APPEND")) append = O_APPEND;	    char *delay = strstr(tst,"JP_PERIOD=");	    if(delay) {	        double tmp = strtod(delay+10, NULL);		if(tmp>1e-3) {		    timerMiliSec = static_cast<unsigned long>(1000 * tmp);		}	    }	    char *first = strstr(tst, "JP_FIRST=");	    if(first) {	        firstDelay = atol(first+9);	    }	}	if(!doNotStart) {	    if(gLogFD<0) {		gLogFD = open(M_LOGFILE, O_CREAT | O_WRONLY | append, 0666);		if(gLogFD<0) {		    fprintf(stderr, "Unable to create " M_LOGFILE);		    perror(":");		} else {		    struct sigaction action;		    sigset_t mset;		    char buffer[1024];		    // Dump out the address map when we terminate		    atexit(DumpAddressMap);		    sigemptyset(&mset);		    action.sa_handler = NULL;		    action.sa_sigaction = StackHook;		    action.sa_mask  = mset;		    action.sa_flags = SA_RESTART | SA_SIGINFO;		    if (realTime) {		    	sigaction(SIGALRM, &action, NULL);		    } else {		    	sigaction(SIGPROF, &action, NULL);		    }		    // make it so a SIGUSR1 will stop the profiling		    // Note:  It currently does not close the logfile.		    // This could be configurable (so that it could		    // later be reopened).		    struct sigaction stop_action;		    stop_action.sa_handler = EndProfilingHook;		    stop_action.sa_mask  = mset;		    stop_action.sa_flags = SA_RESTART;		    sigaction(SIGUSR1, &stop_action, NULL);		    sprintf(buffer, "Jprof: Initialized signal hander and set "		    "timer for %lu mili-seconds with %d second initial delay\n",		    timerMiliSec, firstDelay);		    writeStrStdout(buffer);		    if(startTimer) {			writeStrStdout("Jprof: started timer\n");			startSignalCounter(firstDelay*1000 + timerMiliSec);		    }		}	    }	}    } else {        printf("setupProfilingStuff() called multiple times\n");    }}