trace.c revision 1.13 1 1.13 rillig /* $NetBSD: trace.c,v 1.13 2020/08/01 09:25:36 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.6 ross #ifndef MAKE_NATIVE
34 1.13 rillig static char rcsid[] = "$NetBSD: trace.c,v 1.13 2020/08/01 09:25:36 rillig Exp $";
35 1.1 sommerfe #else
36 1.1 sommerfe #include <sys/cdefs.h>
37 1.1 sommerfe #ifndef lint
38 1.13 rillig __RCSID("$NetBSD: trace.c,v 1.13 2020/08/01 09:25:36 rillig Exp $");
39 1.1 sommerfe #endif /* not lint */
40 1.1 sommerfe #endif
41 1.1 sommerfe
42 1.1 sommerfe /*-
43 1.1 sommerfe * trace.c --
44 1.1 sommerfe * handle logging of trace events generated by various parts of make.
45 1.1 sommerfe *
46 1.1 sommerfe * Interface:
47 1.1 sommerfe * Trace_Init Initialize tracing (called once during
48 1.1 sommerfe * the lifetime of the process)
49 1.1 sommerfe *
50 1.1 sommerfe * Trace_End Finalize tracing (called before make exits)
51 1.1 sommerfe *
52 1.1 sommerfe * Trace_Log Log an event about a particular make job.
53 1.1 sommerfe */
54 1.1 sommerfe
55 1.5 wiz #include <sys/time.h>
56 1.5 wiz
57 1.1 sommerfe #include <stdio.h>
58 1.1 sommerfe #include <unistd.h>
59 1.1 sommerfe
60 1.1 sommerfe #include "make.h"
61 1.1 sommerfe #include "job.h"
62 1.1 sommerfe #include "trace.h"
63 1.1 sommerfe
64 1.1 sommerfe static FILE *trfile;
65 1.1 sommerfe static pid_t trpid;
66 1.13 rillig const char *trwd;
67 1.1 sommerfe
68 1.1 sommerfe static const char *evname[] = {
69 1.1 sommerfe "BEG",
70 1.1 sommerfe "END",
71 1.1 sommerfe "ERR",
72 1.1 sommerfe "JOB",
73 1.1 sommerfe "DON",
74 1.1 sommerfe "INT",
75 1.1 sommerfe };
76 1.1 sommerfe
77 1.1 sommerfe void
78 1.5 wiz Trace_Init(const char *pathname)
79 1.1 sommerfe {
80 1.1 sommerfe char *p1;
81 1.1 sommerfe if (pathname != NULL) {
82 1.1 sommerfe trpid = getpid();
83 1.1 sommerfe trwd = Var_Value(".CURDIR", VAR_GLOBAL, &p1);
84 1.1 sommerfe
85 1.1 sommerfe trfile = fopen(pathname, "a");
86 1.1 sommerfe }
87 1.1 sommerfe }
88 1.1 sommerfe
89 1.1 sommerfe void
90 1.5 wiz Trace_Log(TrEvent event, Job *job)
91 1.1 sommerfe {
92 1.4 reinoud struct timeval rightnow;
93 1.12 rillig
94 1.1 sommerfe if (trfile == NULL)
95 1.1 sommerfe return;
96 1.1 sommerfe
97 1.9 christos gettimeofday(&rightnow, NULL);
98 1.1 sommerfe
99 1.11 christos fprintf(trfile, "%lld.%06ld %d %s %d %s",
100 1.11 christos (long long)rightnow.tv_sec, (long)rightnow.tv_usec,
101 1.7 dsl jobTokensRunning,
102 1.1 sommerfe evname[event], trpid, trwd);
103 1.1 sommerfe if (job != NULL) {
104 1.9 christos fprintf(trfile, " %s %d %x %x", job->node->name,
105 1.9 christos job->pid, job->flags, job->node->type);
106 1.1 sommerfe }
107 1.9 christos fputc('\n', trfile);
108 1.9 christos fflush(trfile);
109 1.1 sommerfe }
110 1.1 sommerfe
111 1.1 sommerfe void
112 1.5 wiz Trace_End(void)
113 1.1 sommerfe {
114 1.1 sommerfe if (trfile != NULL)
115 1.9 christos fclose(trfile);
116 1.1 sommerfe }
117