trace.c revision 267654
1139749Simp/*	$NetBSD: trace.c,v 1.11 2008/12/28 18:31:51 christos Exp $	*/
2113584Ssimokawa
3103285Sikob/*-
4103285Sikob * Copyright (c) 2000 The NetBSD Foundation, Inc.
5103285Sikob * All rights reserved.
6103285Sikob *
7103285Sikob * This code is derived from software contributed to The NetBSD Foundation
8103285Sikob * by Bill Sommerfeld
9103285Sikob *
10103285Sikob * Redistribution and use in source and binary forms, with or without
11103285Sikob * modification, are permitted provided that the following conditions
12103285Sikob * are met:
13103285Sikob * 1. Redistributions of source code must retain the above copyright
14103285Sikob *    notice, this list of conditions and the following disclaimer.
15103285Sikob * 2. Redistributions in binary form must reproduce the above copyright
16103285Sikob *    notice, this list of conditions and the following disclaimer in the
17103285Sikob *    documentation and/or other materials provided with the distribution.
18103285Sikob *
19103285Sikob * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20103285Sikob * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21103285Sikob * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22103285Sikob * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23103285Sikob * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24103285Sikob * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25103285Sikob * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26103285Sikob * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27103285Sikob * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28103285Sikob * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29103285Sikob * POSSIBILITY OF SUCH DAMAGE.
30103285Sikob */
31103285Sikob
32103285Sikob
33103285Sikob#ifndef MAKE_NATIVE
34103285Sikobstatic char rcsid[] = "$NetBSD: trace.c,v 1.11 2008/12/28 18:31:51 christos Exp $";
35103285Sikob#else
36103285Sikob#include <sys/cdefs.h>
37113584Ssimokawa#ifndef lint
38127468Ssimokawa__RCSID("$NetBSD: trace.c,v 1.11 2008/12/28 18:31:51 christos Exp $");
39127468Ssimokawa#endif /* not lint */
40127468Ssimokawa#endif
41113584Ssimokawa
42113584Ssimokawa/*-
43113584Ssimokawa * trace.c --
44113584Ssimokawa *	handle logging of trace events generated by various parts of make.
45113584Ssimokawa *
46113584Ssimokawa * Interface:
47103285Sikob *	Trace_Init		Initialize tracing (called once during
48103285Sikob *				the lifetime of the process)
49103285Sikob *
50103285Sikob *	Trace_End		Finalize tracing (called before make exits)
51103285Sikob *
52127468Ssimokawa *	Trace_Log		Log an event about a particular make job.
53103285Sikob */
54121506Ssimokawa
55103285Sikob#include <sys/time.h>
56103285Sikob
57103285Sikob#include <stdio.h>
58103285Sikob#include <unistd.h>
59103285Sikob
60103285Sikob#include "make.h"
61103285Sikob#include "job.h"
62103285Sikob#include "trace.h"
63103285Sikob
64103285Sikobstatic FILE *trfile;
65108527Ssimokawastatic pid_t trpid;
66103285Sikobchar *trwd;
67113584Ssimokawa
68113584Ssimokawastatic const char *evname[] = {
69109379Ssimokawa	"BEG",
70113584Ssimokawa	"END",
71113584Ssimokawa	"ERR",
72113584Ssimokawa	"JOB",
73108276Ssimokawa	"DON",
74103285Sikob	"INT",
75129585Sdfr};
76113584Ssimokawa
77113584Ssimokawavoid
78113584SsimokawaTrace_Init(const char *pathname)
79129585Sdfr{
80113584Ssimokawa	char *p1;
81129585Sdfr	if (pathname != NULL) {
82113584Ssimokawa		trpid = getpid();
83113584Ssimokawa		trwd = Var_Value(".CURDIR", VAR_GLOBAL, &p1);
84167628Ssimokawa
85103285Sikob		trfile = fopen(pathname, "a");
86113584Ssimokawa	}
87124169Ssimokawa}
88124169Ssimokawa
89124169Ssimokawavoid
90124169SsimokawaTrace_Log(TrEvent event, Job *job)
91124169Ssimokawa{
92124169Ssimokawa	struct timeval rightnow;
93124169Ssimokawa
94	if (trfile == NULL)
95		return;
96
97	gettimeofday(&rightnow, NULL);
98
99	fprintf(trfile, "%lld.%06ld %d %s %d %s",
100	    (long long)rightnow.tv_sec, (long)rightnow.tv_usec,
101	    jobTokensRunning,
102	    evname[event], trpid, trwd);
103	if (job != NULL) {
104		fprintf(trfile, " %s %d %x %x", job->node->name,
105		    job->pid, job->flags, job->node->type);
106	}
107	fputc('\n', trfile);
108	fflush(trfile);
109}
110
111void
112Trace_End(void)
113{
114	if (trfile != NULL)
115		fclose(trfile);
116}
117