xref: /netbsd/usr.bin/make/trace.c (revision bf9ec67e)
1 /*	$NetBSD: trace.c,v 1.4 2002/01/27 01:50:55 reinoud 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.4 2002/01/27 01:50:55 reinoud Exp $";
42 #else
43 #include <sys/cdefs.h>
44 #ifndef lint
45 __RCSID("$NetBSD: trace.c,v 1.4 2002/01/27 01:50:55 reinoud 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 <stdio.h>
63 #include <unistd.h>
64 #include <sys/time.h>
65 
66 #include "make.h"
67 #include "job.h"
68 #include "trace.h"
69 
70 static FILE *trfile;
71 static pid_t trpid;
72 char *trwd;
73 
74 static const char *evname[] = {
75 	"BEG",
76 	"END",
77 	"ERR",
78 	"JOB",
79 	"DON",
80 	"INT",
81 };
82 
83 void
84 Trace_Init(pathname)
85 	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(event, job)
98 	TrEvent event;
99 	Job *job;
100 {
101 	struct timeval rightnow;
102 
103 	if (trfile == NULL)
104 		return;
105 
106 	gettimeofday(&rightnow, NULL);
107 
108 	fprintf(trfile, "%ld.%06d %d %d %s %d %s",
109 	    rightnow.tv_sec, (int)rightnow.tv_usec,
110 	    jobTokensRunning, jobTokensFree,
111 	    evname[event], trpid, trwd);
112 	if (job != NULL) {
113 		fprintf(trfile, " %s %d %x %x", job->node->name,
114 		    job->pid, job->flags, job->node->type);
115 	}
116 	fputc('\n', trfile);
117 	fflush(trfile);
118 }
119 
120 void
121 Trace_End()
122 {
123 	if (trfile != NULL)
124 		fclose(trfile);
125 }
126