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