1236769Sobrien/* $NetBSD: trace.c,v 1.11 2008/12/28 18:31:51 christos Exp $ */ 2236769Sobrien 3236769Sobrien/*- 4236769Sobrien * Copyright (c) 2000 The NetBSD Foundation, Inc. 5236769Sobrien * All rights reserved. 6236769Sobrien * 7236769Sobrien * This code is derived from software contributed to The NetBSD Foundation 8236769Sobrien * by Bill Sommerfeld 9236769Sobrien * 10236769Sobrien * Redistribution and use in source and binary forms, with or without 11236769Sobrien * modification, are permitted provided that the following conditions 12236769Sobrien * are met: 13236769Sobrien * 1. Redistributions of source code must retain the above copyright 14236769Sobrien * notice, this list of conditions and the following disclaimer. 15236769Sobrien * 2. Redistributions in binary form must reproduce the above copyright 16236769Sobrien * notice, this list of conditions and the following disclaimer in the 17236769Sobrien * documentation and/or other materials provided with the distribution. 18236769Sobrien * 19236769Sobrien * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20236769Sobrien * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21236769Sobrien * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22236769Sobrien * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23236769Sobrien * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24236769Sobrien * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25236769Sobrien * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26236769Sobrien * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27236769Sobrien * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28236769Sobrien * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29236769Sobrien * POSSIBILITY OF SUCH DAMAGE. 30236769Sobrien */ 31236769Sobrien 32236769Sobrien 33236769Sobrien#ifndef MAKE_NATIVE 34236769Sobrienstatic char rcsid[] = "$NetBSD: trace.c,v 1.11 2008/12/28 18:31:51 christos Exp $"; 35236769Sobrien#else 36236769Sobrien#include <sys/cdefs.h> 37236769Sobrien#ifndef lint 38236769Sobrien__RCSID("$NetBSD: trace.c,v 1.11 2008/12/28 18:31:51 christos Exp $"); 39236769Sobrien#endif /* not lint */ 40236769Sobrien#endif 41236769Sobrien 42236769Sobrien/*- 43236769Sobrien * trace.c -- 44236769Sobrien * handle logging of trace events generated by various parts of make. 45236769Sobrien * 46236769Sobrien * Interface: 47236769Sobrien * Trace_Init Initialize tracing (called once during 48236769Sobrien * the lifetime of the process) 49236769Sobrien * 50236769Sobrien * Trace_End Finalize tracing (called before make exits) 51236769Sobrien * 52236769Sobrien * Trace_Log Log an event about a particular make job. 53236769Sobrien */ 54236769Sobrien 55236769Sobrien#include <sys/time.h> 56236769Sobrien 57236769Sobrien#include <stdio.h> 58236769Sobrien#include <unistd.h> 59236769Sobrien 60236769Sobrien#include "make.h" 61236769Sobrien#include "job.h" 62236769Sobrien#include "trace.h" 63236769Sobrien 64236769Sobrienstatic FILE *trfile; 65236769Sobrienstatic pid_t trpid; 66236769Sobrienchar *trwd; 67236769Sobrien 68236769Sobrienstatic const char *evname[] = { 69236769Sobrien "BEG", 70236769Sobrien "END", 71236769Sobrien "ERR", 72236769Sobrien "JOB", 73236769Sobrien "DON", 74236769Sobrien "INT", 75236769Sobrien}; 76236769Sobrien 77236769Sobrienvoid 78236769SobrienTrace_Init(const char *pathname) 79236769Sobrien{ 80236769Sobrien char *p1; 81236769Sobrien if (pathname != NULL) { 82236769Sobrien trpid = getpid(); 83236769Sobrien trwd = Var_Value(".CURDIR", VAR_GLOBAL, &p1); 84236769Sobrien 85236769Sobrien trfile = fopen(pathname, "a"); 86236769Sobrien } 87236769Sobrien} 88236769Sobrien 89236769Sobrienvoid 90236769SobrienTrace_Log(TrEvent event, Job *job) 91236769Sobrien{ 92236769Sobrien struct timeval rightnow; 93236769Sobrien 94236769Sobrien if (trfile == NULL) 95236769Sobrien return; 96236769Sobrien 97236769Sobrien gettimeofday(&rightnow, NULL); 98236769Sobrien 99236769Sobrien fprintf(trfile, "%lld.%06ld %d %s %d %s", 100236769Sobrien (long long)rightnow.tv_sec, (long)rightnow.tv_usec, 101236769Sobrien jobTokensRunning, 102236769Sobrien evname[event], trpid, trwd); 103236769Sobrien if (job != NULL) { 104236769Sobrien fprintf(trfile, " %s %d %x %x", job->node->name, 105236769Sobrien job->pid, job->flags, job->node->type); 106236769Sobrien } 107236769Sobrien fputc('\n', trfile); 108236769Sobrien fflush(trfile); 109236769Sobrien} 110236769Sobrien 111236769Sobrienvoid 112236769SobrienTrace_End(void) 113236769Sobrien{ 114236769Sobrien if (trfile != NULL) 115236769Sobrien fclose(trfile); 116236769Sobrien} 117