Home | History | Annotate | Line # | Download | only in make
trace.c revision 1.20
      1  1.20    rillig /*	$NetBSD: trace.c,v 1.20 2020/10/30 20:30:44 rillig Exp $	*/
      2   1.1  sommerfe 
      3   1.1  sommerfe /*-
      4   1.1  sommerfe  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5   1.1  sommerfe  * All rights reserved.
      6   1.1  sommerfe  *
      7   1.1  sommerfe  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1  sommerfe  * by Bill Sommerfeld
      9   1.1  sommerfe  *
     10   1.1  sommerfe  * Redistribution and use in source and binary forms, with or without
     11   1.1  sommerfe  * modification, are permitted provided that the following conditions
     12   1.1  sommerfe  * are met:
     13   1.1  sommerfe  * 1. Redistributions of source code must retain the above copyright
     14   1.1  sommerfe  *    notice, this list of conditions and the following disclaimer.
     15   1.1  sommerfe  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1  sommerfe  *    notice, this list of conditions and the following disclaimer in the
     17   1.1  sommerfe  *    documentation and/or other materials provided with the distribution.
     18   1.1  sommerfe  *
     19   1.1  sommerfe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1  sommerfe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1  sommerfe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1  sommerfe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1  sommerfe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1  sommerfe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1  sommerfe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1  sommerfe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1  sommerfe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1  sommerfe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1  sommerfe  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1  sommerfe  */
     31   1.1  sommerfe 
     32   1.1  sommerfe /*-
     33   1.1  sommerfe  * trace.c --
     34   1.1  sommerfe  *	handle logging of trace events generated by various parts of make.
     35   1.1  sommerfe  *
     36   1.1  sommerfe  * Interface:
     37   1.1  sommerfe  *	Trace_Init		Initialize tracing (called once during
     38   1.1  sommerfe  *				the lifetime of the process)
     39   1.1  sommerfe  *
     40   1.1  sommerfe  *	Trace_End		Finalize tracing (called before make exits)
     41   1.1  sommerfe  *
     42   1.1  sommerfe  *	Trace_Log		Log an event about a particular make job.
     43   1.1  sommerfe  */
     44   1.1  sommerfe 
     45   1.5       wiz #include <sys/time.h>
     46   1.5       wiz 
     47   1.1  sommerfe #include "make.h"
     48   1.1  sommerfe #include "job.h"
     49   1.1  sommerfe #include "trace.h"
     50   1.1  sommerfe 
     51  1.20    rillig MAKE_RCSID("$NetBSD: trace.c,v 1.20 2020/10/30 20:30:44 rillig Exp $");
     52  1.16    rillig 
     53   1.1  sommerfe static FILE *trfile;
     54   1.1  sommerfe static pid_t trpid;
     55  1.13    rillig const char *trwd;
     56   1.1  sommerfe 
     57   1.1  sommerfe static const char *evname[] = {
     58   1.1  sommerfe 	"BEG",
     59   1.1  sommerfe 	"END",
     60   1.1  sommerfe 	"ERR",
     61   1.1  sommerfe 	"JOB",
     62   1.1  sommerfe 	"DON",
     63   1.1  sommerfe 	"INT",
     64   1.1  sommerfe };
     65   1.1  sommerfe 
     66   1.1  sommerfe void
     67   1.5       wiz Trace_Init(const char *pathname)
     68   1.1  sommerfe {
     69   1.1  sommerfe 	if (pathname != NULL) {
     70  1.20    rillig 		void *dontFreeIt;
     71   1.1  sommerfe 		trpid = getpid();
     72  1.14    rillig 		trwd = Var_Value(".CURDIR", VAR_GLOBAL, &dontFreeIt);
     73   1.1  sommerfe 
     74   1.1  sommerfe 		trfile = fopen(pathname, "a");
     75   1.1  sommerfe 	}
     76   1.1  sommerfe }
     77   1.1  sommerfe 
     78   1.1  sommerfe void
     79   1.5       wiz Trace_Log(TrEvent event, Job *job)
     80   1.1  sommerfe {
     81   1.4   reinoud 	struct timeval rightnow;
     82  1.12    rillig 
     83   1.1  sommerfe 	if (trfile == NULL)
     84   1.1  sommerfe 		return;
     85   1.1  sommerfe 
     86   1.9  christos 	gettimeofday(&rightnow, NULL);
     87   1.1  sommerfe 
     88  1.11  christos 	fprintf(trfile, "%lld.%06ld %d %s %d %s",
     89  1.11  christos 	    (long long)rightnow.tv_sec, (long)rightnow.tv_usec,
     90   1.7       dsl 	    jobTokensRunning,
     91   1.1  sommerfe 	    evname[event], trpid, trwd);
     92   1.1  sommerfe 	if (job != NULL) {
     93   1.9  christos 		fprintf(trfile, " %s %d %x %x", job->node->name,
     94   1.9  christos 		    job->pid, job->flags, job->node->type);
     95   1.1  sommerfe 	}
     96   1.9  christos 	fputc('\n', trfile);
     97   1.9  christos 	fflush(trfile);
     98   1.1  sommerfe }
     99   1.1  sommerfe 
    100   1.1  sommerfe void
    101   1.5       wiz Trace_End(void)
    102   1.1  sommerfe {
    103   1.1  sommerfe 	if (trfile != NULL)
    104   1.9  christos 		fclose(trfile);
    105   1.1  sommerfe }
    106