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