Module Name:    src
Committed By:   rillig
Date:           Sat Jan 29 09:38:27 UTC 2022

Modified Files:
        src/usr.bin/make: main.c make.h parse.c
        src/usr.bin/make/unit-tests: var-recursive.exp

Log Message:
make: print stack trace on fatal errors

The only fatal error that occurs while the makefiles are read in is the
one about recursive variables, which didn't give any hint about the
location before.

If a recursive variable is detected while evaluating the commands of a
target to be made, there is no location information, as before.


To generate a diff of this commit:
cvs rdiff -u -r1.576 -r1.577 src/usr.bin/make/main.c
cvs rdiff -u -r1.292 -r1.293 src/usr.bin/make/make.h
cvs rdiff -u -r1.658 -r1.659 src/usr.bin/make/parse.c
cvs rdiff -u -r1.2 -r1.3 src/usr.bin/make/unit-tests/var-recursive.exp

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/main.c
diff -u src/usr.bin/make/main.c:1.576 src/usr.bin/make/main.c:1.577
--- src/usr.bin/make/main.c:1.576	Thu Jan 27 06:02:59 2022
+++ src/usr.bin/make/main.c	Sat Jan 29 09:38:26 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: main.c,v 1.576 2022/01/27 06:02:59 sjg Exp $	*/
+/*	$NetBSD: main.c,v 1.577 2022/01/29 09:38:26 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -111,7 +111,7 @@
 #include "trace.h"
 
 /*	"@(#)main.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: main.c,v 1.576 2022/01/27 06:02:59 sjg Exp $");
+MAKE_RCSID("$NetBSD: main.c,v 1.577 2022/01/29 09:38:26 rillig Exp $");
 #if defined(MAKE_NATIVE) && !defined(lint)
 __COPYRIGHT("@(#) Copyright (c) 1988, 1989, 1990, 1993 "
 	    "The Regents of the University of California.  "
@@ -1828,6 +1828,7 @@ Fatal(const char *fmt, ...)
 	va_end(ap);
 	(void)fprintf(stderr, "\n");
 	(void)fflush(stderr);
+	PrintStackTrace(true);
 
 	PrintOnError(NULL, "\n");
 

Index: src/usr.bin/make/make.h
diff -u src/usr.bin/make/make.h:1.292 src/usr.bin/make/make.h:1.293
--- src/usr.bin/make/make.h:1.292	Sat Jan 29 01:07:31 2022
+++ src/usr.bin/make/make.h	Sat Jan 29 09:38:26 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: make.h,v 1.292 2022/01/29 01:07:31 rillig Exp $	*/
+/*	$NetBSD: make.h,v 1.293 2022/01/29 09:38:26 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -832,6 +832,7 @@ bool GetBooleanExpr(const char *, bool);
 void Parse_Init(void);
 void Parse_End(void);
 
+void PrintStackTrace(bool);
 void Parse_Error(ParseErrorLevel, const char *, ...) MAKE_ATTR_PRINTFLIKE(2, 3);
 bool Parse_VarAssign(const char *, bool, GNode *) MAKE_ATTR_USE;
 void Parse_AddIncludeDir(const char *);

Index: src/usr.bin/make/parse.c
diff -u src/usr.bin/make/parse.c:1.658 src/usr.bin/make/parse.c:1.659
--- src/usr.bin/make/parse.c:1.658	Sat Jan 29 01:07:31 2022
+++ src/usr.bin/make/parse.c	Sat Jan 29 09:38:26 2022
@@ -1,4 +1,4 @@
-/*	$NetBSD: parse.c,v 1.658 2022/01/29 01:07:31 rillig Exp $	*/
+/*	$NetBSD: parse.c,v 1.659 2022/01/29 09:38:26 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -106,7 +106,7 @@
 #include "pathnames.h"
 
 /*	"@(#)parse.c	8.3 (Berkeley) 3/19/94"	*/
-MAKE_RCSID("$NetBSD: parse.c,v 1.658 2022/01/29 01:07:31 rillig Exp $");
+MAKE_RCSID("$NetBSD: parse.c,v 1.659 2022/01/29 09:38:26 rillig Exp $");
 
 /*
  * A file being read.
@@ -348,8 +348,14 @@ loadfile(const char *path, int fd)
 	return buf;		/* may not be null-terminated */
 }
 
-static void
-PrintStackTrace(void)
+/*
+ * Print the current chain of .include and .for directives.  In Parse_Fatal
+ * or other functions that already print the location, includingInnermost
+ * would be redundant, but in other cases like Error or Fatal it needs to be
+ * included.
+ */
+void
+PrintStackTrace(bool includingInnermost)
 {
 	const IncludedFile *entries;
 	size_t i, n;
@@ -359,7 +365,7 @@ PrintStackTrace(void)
 	if (n == 0)
 		return;
 
-	if (entries[n - 1].forLoop == NULL)
+	if (!includingInnermost && entries[n - 1].forLoop == NULL)
 		n--;		/* already in the diagnostic */
 
 	for (i = n; i-- > 0;) {
@@ -484,7 +490,7 @@ ParseVErrorInternal(FILE *f, bool useVar
 	}
 
 	if (DEBUG(PARSE))
-		PrintStackTrace();
+		PrintStackTrace(false);
 }
 
 static void MAKE_ATTR_PRINTFLIKE(4, 5)

Index: src/usr.bin/make/unit-tests/var-recursive.exp
diff -u src/usr.bin/make/unit-tests/var-recursive.exp:1.2 src/usr.bin/make/unit-tests/var-recursive.exp:1.3
--- src/usr.bin/make/unit-tests/var-recursive.exp:1.2	Sat Oct 31 13:45:00 2020
+++ src/usr.bin/make/unit-tests/var-recursive.exp	Sat Jan 29 09:38:27 2022
@@ -1,12 +1,15 @@
 make: "var-recursive.mk" line 20: still there
 Variable DIRECT is recursive.
+	in var-recursive.mk:21
 
 make: stopped in unit-tests
 Variable INDIRECT1 is recursive.
+	in var-recursive.mk:28
 
 make: stopped in unit-tests
 make: "var-recursive.mk" line 35: ok
 Variable V is recursive.
+	in var-recursive.mk:43
 
 make: stopped in unit-tests
 exit status 0

Reply via email to