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