Home | History | Annotate | Line # | Download | only in make
trace.c revision 1.33.2.1
      1  1.33.2.1  perseant /*	$NetBSD: trace.c,v 1.33.2.1 2025/08/02 05:58:30 perseant Exp $	*/
      2       1.1  sommerfe 
      3      1.26    rillig /*
      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.26    rillig /*
     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.33.2.1  perseant MAKE_RCSID("$NetBSD: trace.c,v 1.33.2.1 2025/08/02 05:58:30 perseant Exp $");
     52      1.16    rillig 
     53       1.1  sommerfe static FILE *trfile;
     54       1.1  sommerfe static pid_t trpid;
     55      1.33    rillig static const char *trwd;
     56       1.1  sommerfe 
     57      1.29    rillig static const char evname[][4] = {
     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.25    rillig 		FStr curDir;
     71       1.1  sommerfe 		trpid = getpid();
     72      1.30    rillig 		/*
     73      1.30    rillig 		 * XXX: This variable may get overwritten later, which would
     74      1.30    rillig 		 * make trwd point to undefined behavior.
     75      1.30    rillig 		 */
     76      1.28    rillig 		curDir = Var_Value(SCOPE_GLOBAL, ".CURDIR");
     77      1.25    rillig 		trwd = curDir.str;
     78       1.1  sommerfe 
     79       1.1  sommerfe 		trfile = fopen(pathname, "a");
     80       1.1  sommerfe 	}
     81       1.1  sommerfe }
     82       1.1  sommerfe 
     83       1.1  sommerfe void
     84       1.5       wiz Trace_Log(TrEvent event, Job *job)
     85       1.1  sommerfe {
     86       1.4   reinoud 	struct timeval rightnow;
     87      1.12    rillig 
     88       1.1  sommerfe 	if (trfile == NULL)
     89       1.1  sommerfe 		return;
     90       1.1  sommerfe 
     91       1.9  christos 	gettimeofday(&rightnow, NULL);
     92       1.1  sommerfe 
     93      1.32    rillig #if __STDC_VERSION__ >= 199901L
     94      1.11  christos 	fprintf(trfile, "%lld.%06ld %d %s %d %s",
     95      1.11  christos 	    (long long)rightnow.tv_sec, (long)rightnow.tv_usec,
     96       1.7       dsl 	    jobTokensRunning,
     97       1.1  sommerfe 	    evname[event], trpid, trwd);
     98      1.31    rillig #else
     99      1.31    rillig 	fprintf(trfile, "%ld.%06ld %d %s %d %s",
    100      1.31    rillig 	    (long)rightnow.tv_sec, (long)rightnow.tv_usec,
    101      1.31    rillig 	    jobTokensRunning,
    102      1.31    rillig 	    evname[event], trpid, trwd);
    103      1.31    rillig #endif
    104       1.1  sommerfe 	if (job != NULL) {
    105  1.33.2.1  perseant 		GNode *gn = Job_Node(job);
    106  1.33.2.1  perseant 		char *type = GNodeType_ToString(gn->type);
    107      1.24    rillig 		char flags[4];
    108      1.23    rillig 		Job_FlagsToString(job, flags, sizeof flags);
    109  1.33.2.1  perseant 		fprintf(trfile, " %s %d %s %s",
    110  1.33.2.1  perseant 		    gn->name, Job_Pid(job), flags, type);
    111  1.33.2.1  perseant 		free(type);
    112       1.1  sommerfe 	}
    113       1.9  christos 	fputc('\n', trfile);
    114       1.9  christos 	fflush(trfile);
    115       1.1  sommerfe }
    116       1.1  sommerfe 
    117       1.1  sommerfe void
    118       1.5       wiz Trace_End(void)
    119       1.1  sommerfe {
    120       1.1  sommerfe 	if (trfile != NULL)
    121       1.9  christos 		fclose(trfile);
    122       1.1  sommerfe }
    123