xref: /netbsd-src/external/mpl/dhcp/bind/dist/lib/isc/assertions.c (revision 4afad4b7fa6d4a0d3dedf41d1587a7250710ae54)
1 /*	$NetBSD: assertions.c,v 1.1 2024/02/18 20:57:48 christos Exp $	*/
2 
3 /*
4  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5  *
6  * SPDX-License-Identifier: MPL-2.0
7  *
8  * This Source Code Form is subject to the terms of the Mozilla Public
9  * License, v. 2.0. If a copy of the MPL was not distributed with this
10  * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11  *
12  * See the COPYRIGHT file distributed with this work for additional
13  * information regarding copyright ownership.
14  */
15 
16 /*! \file */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 
21 #include <isc/assertions.h>
22 #include <isc/backtrace.h>
23 #include <isc/print.h>
24 #include <isc/result.h>
25 
26 /*
27  * The maximum number of stack frames to dump on assertion failure.
28  */
29 #ifndef BACKTRACE_MAXFRAME
30 #define BACKTRACE_MAXFRAME 128
31 #endif /* ifndef BACKTRACE_MAXFRAME */
32 
33 /*%
34  * Forward.
35  */
36 static void
37 default_callback(const char *, int, isc_assertiontype_t, const char *);
38 
39 static isc_assertioncallback_t isc_assertion_failed_cb = default_callback;
40 
41 /*%
42  * Public.
43  */
44 
45 /*% assertion failed handler */
46 /* coverity[+kill] */
47 void
isc_assertion_failed(const char * file,int line,isc_assertiontype_t type,const char * cond)48 isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
49 		     const char *cond) {
50 	isc_assertion_failed_cb(file, line, type, cond);
51 	abort();
52 }
53 
54 /*% Set callback. */
55 void
isc_assertion_setcallback(isc_assertioncallback_t cb)56 isc_assertion_setcallback(isc_assertioncallback_t cb) {
57 	if (cb == NULL) {
58 		isc_assertion_failed_cb = default_callback;
59 	} else {
60 		isc_assertion_failed_cb = cb;
61 	}
62 }
63 
64 /*% Type to Text */
65 const char *
isc_assertion_typetotext(isc_assertiontype_t type)66 isc_assertion_typetotext(isc_assertiontype_t type) {
67 	const char *result;
68 
69 	/*
70 	 * These strings have purposefully not been internationalized
71 	 * because they are considered to essentially be keywords of
72 	 * the ISC development environment.
73 	 */
74 	switch (type) {
75 	case isc_assertiontype_require:
76 		result = "REQUIRE";
77 		break;
78 	case isc_assertiontype_ensure:
79 		result = "ENSURE";
80 		break;
81 	case isc_assertiontype_insist:
82 		result = "INSIST";
83 		break;
84 	case isc_assertiontype_invariant:
85 		result = "INVARIANT";
86 		break;
87 	default:
88 		result = "UNKNOWN";
89 	}
90 	return (result);
91 }
92 
93 /*
94  * Private.
95  */
96 
97 /* coverity[+kill] */
98 static void
default_callback(const char * file,int line,isc_assertiontype_t type,const char * cond)99 default_callback(const char *file, int line, isc_assertiontype_t type,
100 		 const char *cond) {
101 	void *tracebuf[BACKTRACE_MAXFRAME];
102 	int i, nframes;
103 	const char *logsuffix = ".";
104 	const char *fname;
105 	isc_result_t result;
106 
107 	result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME, &nframes);
108 	if (result == ISC_R_SUCCESS && nframes > 0) {
109 		logsuffix = ", back trace";
110 	}
111 
112 	fprintf(stderr, "%s:%d: %s(%s) failed%s\n", file, line,
113 		isc_assertion_typetotext(type), cond, logsuffix);
114 
115 	if (result == ISC_R_SUCCESS) {
116 		for (i = 0; i < nframes; i++) {
117 			unsigned long offset;
118 
119 			fname = NULL;
120 			result = isc_backtrace_getsymbol(tracebuf[i], &fname,
121 							 &offset);
122 			if (result == ISC_R_SUCCESS) {
123 				fprintf(stderr, "#%d %p in %s()+0x%lx\n", i,
124 					tracebuf[i], fname, offset);
125 			} else {
126 				fprintf(stderr, "#%d %p in ??\n", i,
127 					tracebuf[i]);
128 			}
129 		}
130 	}
131 	fflush(stderr);
132 }
133