Home | History | Annotate | Line # | Download | only in make
trace.c revision 1.6
      1  1.6      ross /*	$NetBSD: trace.c,v 1.6 2004/05/07 00:04:40 ross 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  * 3. All advertising materials mentioning features or use of this software
     19  1.1  sommerfe  *    must display the following acknowledgement:
     20  1.1  sommerfe  *        This product includes software developed by the NetBSD
     21  1.1  sommerfe  *        Foundation, Inc. and its contributors.
     22  1.1  sommerfe  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.1  sommerfe  *    contributors may be used to endorse or promote products derived
     24  1.1  sommerfe  *    from this software without specific prior written permission.
     25  1.1  sommerfe  *
     26  1.1  sommerfe  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.1  sommerfe  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.1  sommerfe  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.1  sommerfe  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.1  sommerfe  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.1  sommerfe  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.1  sommerfe  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.1  sommerfe  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.1  sommerfe  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.1  sommerfe  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.1  sommerfe  * POSSIBILITY OF SUCH DAMAGE.
     37  1.1  sommerfe  */
     38  1.1  sommerfe 
     39  1.1  sommerfe 
     40  1.6      ross #ifndef MAKE_NATIVE
     41  1.6      ross static char rcsid[] = "$NetBSD: trace.c,v 1.6 2004/05/07 00:04:40 ross Exp $";
     42  1.1  sommerfe #else
     43  1.1  sommerfe #include <sys/cdefs.h>
     44  1.1  sommerfe #ifndef lint
     45  1.6      ross __RCSID("$NetBSD: trace.c,v 1.6 2004/05/07 00:04:40 ross Exp $");
     46  1.1  sommerfe #endif /* not lint */
     47  1.1  sommerfe #endif
     48  1.1  sommerfe 
     49  1.1  sommerfe /*-
     50  1.1  sommerfe  * trace.c --
     51  1.1  sommerfe  *	handle logging of trace events generated by various parts of make.
     52  1.1  sommerfe  *
     53  1.1  sommerfe  * Interface:
     54  1.1  sommerfe  *	Trace_Init		Initialize tracing (called once during
     55  1.1  sommerfe  *				the lifetime of the process)
     56  1.1  sommerfe  *
     57  1.1  sommerfe  *	Trace_End		Finalize tracing (called before make exits)
     58  1.1  sommerfe  *
     59  1.1  sommerfe  *	Trace_Log		Log an event about a particular make job.
     60  1.1  sommerfe  */
     61  1.1  sommerfe 
     62  1.5       wiz #include <sys/time.h>
     63  1.5       wiz 
     64  1.1  sommerfe #include <stdio.h>
     65  1.1  sommerfe #include <unistd.h>
     66  1.1  sommerfe 
     67  1.1  sommerfe #include "make.h"
     68  1.1  sommerfe #include "job.h"
     69  1.1  sommerfe #include "trace.h"
     70  1.1  sommerfe 
     71  1.1  sommerfe static FILE *trfile;
     72  1.1  sommerfe static pid_t trpid;
     73  1.1  sommerfe char *trwd;
     74  1.1  sommerfe 
     75  1.1  sommerfe static const char *evname[] = {
     76  1.1  sommerfe 	"BEG",
     77  1.1  sommerfe 	"END",
     78  1.1  sommerfe 	"ERR",
     79  1.1  sommerfe 	"JOB",
     80  1.1  sommerfe 	"DON",
     81  1.1  sommerfe 	"INT",
     82  1.1  sommerfe };
     83  1.1  sommerfe 
     84  1.1  sommerfe void
     85  1.5       wiz Trace_Init(const char *pathname)
     86  1.1  sommerfe {
     87  1.1  sommerfe 	char *p1;
     88  1.1  sommerfe 	if (pathname != NULL) {
     89  1.1  sommerfe 		trpid = getpid();
     90  1.1  sommerfe 		trwd = Var_Value(".CURDIR", VAR_GLOBAL, &p1);
     91  1.1  sommerfe 
     92  1.1  sommerfe 		trfile = fopen(pathname, "a");
     93  1.1  sommerfe 	}
     94  1.1  sommerfe }
     95  1.1  sommerfe 
     96  1.1  sommerfe void
     97  1.5       wiz Trace_Log(TrEvent event, Job *job)
     98  1.1  sommerfe {
     99  1.4   reinoud 	struct timeval rightnow;
    100  1.1  sommerfe 
    101  1.1  sommerfe 	if (trfile == NULL)
    102  1.1  sommerfe 		return;
    103  1.1  sommerfe 
    104  1.4   reinoud 	gettimeofday(&rightnow, NULL);
    105  1.1  sommerfe 
    106  1.2  sommerfe 	fprintf(trfile, "%ld.%06d %d %d %s %d %s",
    107  1.4   reinoud 	    rightnow.tv_sec, (int)rightnow.tv_usec,
    108  1.2  sommerfe 	    jobTokensRunning, jobTokensFree,
    109  1.1  sommerfe 	    evname[event], trpid, trwd);
    110  1.1  sommerfe 	if (job != NULL) {
    111  1.1  sommerfe 		fprintf(trfile, " %s %d %x %x", job->node->name,
    112  1.1  sommerfe 		    job->pid, job->flags, job->node->type);
    113  1.1  sommerfe 	}
    114  1.1  sommerfe 	fputc('\n', trfile);
    115  1.1  sommerfe 	fflush(trfile);
    116  1.1  sommerfe }
    117  1.1  sommerfe 
    118  1.1  sommerfe void
    119  1.5       wiz Trace_End(void)
    120  1.1  sommerfe {
    121  1.1  sommerfe 	if (trfile != NULL)
    122  1.1  sommerfe 		fclose(trfile);
    123  1.1  sommerfe }
    124