trace.c revision 1.22 1 1.22 rillig /* $NetBSD: trace.c,v 1.22 2020/12/10 20:49:11 rillig 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 *
19 1.1 sommerfe * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 sommerfe * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 sommerfe * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 sommerfe * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 sommerfe * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 sommerfe * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 sommerfe * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 sommerfe * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 sommerfe * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 sommerfe * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 sommerfe * POSSIBILITY OF SUCH DAMAGE.
30 1.1 sommerfe */
31 1.1 sommerfe
32 1.1 sommerfe /*-
33 1.1 sommerfe * trace.c --
34 1.1 sommerfe * handle logging of trace events generated by various parts of make.
35 1.1 sommerfe *
36 1.1 sommerfe * Interface:
37 1.1 sommerfe * Trace_Init Initialize tracing (called once during
38 1.1 sommerfe * the lifetime of the process)
39 1.1 sommerfe *
40 1.1 sommerfe * Trace_End Finalize tracing (called before make exits)
41 1.1 sommerfe *
42 1.1 sommerfe * Trace_Log Log an event about a particular make job.
43 1.1 sommerfe */
44 1.1 sommerfe
45 1.5 wiz #include <sys/time.h>
46 1.5 wiz
47 1.1 sommerfe #include "make.h"
48 1.1 sommerfe #include "job.h"
49 1.1 sommerfe #include "trace.h"
50 1.1 sommerfe
51 1.22 rillig MAKE_RCSID("$NetBSD: trace.c,v 1.22 2020/12/10 20:49:11 rillig Exp $");
52 1.16 rillig
53 1.1 sommerfe static FILE *trfile;
54 1.1 sommerfe static pid_t trpid;
55 1.13 rillig const char *trwd;
56 1.1 sommerfe
57 1.1 sommerfe static const char *evname[] = {
58 1.1 sommerfe "BEG",
59 1.1 sommerfe "END",
60 1.1 sommerfe "ERR",
61 1.1 sommerfe "JOB",
62 1.1 sommerfe "DON",
63 1.1 sommerfe "INT",
64 1.1 sommerfe };
65 1.1 sommerfe
66 1.1 sommerfe void
67 1.5 wiz Trace_Init(const char *pathname)
68 1.1 sommerfe {
69 1.1 sommerfe if (pathname != NULL) {
70 1.20 rillig void *dontFreeIt;
71 1.1 sommerfe trpid = getpid();
72 1.21 rillig /* XXX: This variable may get overwritten later, which
73 1.21 rillig * would make trwd point to undefined behavior. */
74 1.14 rillig trwd = Var_Value(".CURDIR", VAR_GLOBAL, &dontFreeIt);
75 1.1 sommerfe
76 1.1 sommerfe trfile = fopen(pathname, "a");
77 1.1 sommerfe }
78 1.1 sommerfe }
79 1.1 sommerfe
80 1.1 sommerfe void
81 1.5 wiz Trace_Log(TrEvent event, Job *job)
82 1.1 sommerfe {
83 1.4 reinoud struct timeval rightnow;
84 1.12 rillig
85 1.1 sommerfe if (trfile == NULL)
86 1.1 sommerfe return;
87 1.1 sommerfe
88 1.9 christos gettimeofday(&rightnow, NULL);
89 1.1 sommerfe
90 1.11 christos fprintf(trfile, "%lld.%06ld %d %s %d %s",
91 1.11 christos (long long)rightnow.tv_sec, (long)rightnow.tv_usec,
92 1.7 dsl jobTokensRunning,
93 1.1 sommerfe evname[event], trpid, trwd);
94 1.1 sommerfe if (job != NULL) {
95 1.22 rillig char flags[5];
96 1.22 rillig
97 1.22 rillig Job_FlagsToString(flags, sizeof flags, &job->flags);
98 1.22 rillig fprintf(trfile, " %s %d %s %x", job->node->name,
99 1.22 rillig job->pid, flags, job->node->type);
100 1.1 sommerfe }
101 1.9 christos fputc('\n', trfile);
102 1.9 christos fflush(trfile);
103 1.1 sommerfe }
104 1.1 sommerfe
105 1.1 sommerfe void
106 1.5 wiz Trace_End(void)
107 1.1 sommerfe {
108 1.1 sommerfe if (trfile != NULL)
109 1.9 christos fclose(trfile);
110 1.1 sommerfe }
111