2 * Copyright (c) 2000-2001 Sendmail, Inc. and its suppliers.
5 * By using this file, you agree to the terms and conditions set
6 * forth in the LICENSE file which can be found at the top level of
7 * the sendmail distribution.
12 SM_RCSID("@(#)$Id: assert.c,v 1.26 2003/12/05 22:45:24 ca Exp $")
15 ** Abnormal program termination and assertion checking.
16 ** For documentation, see assert.html.
23 #include <sm/assert.h>
26 #include <sm/varargs.h>
29 ** Debug categories that are used to guard expensive assertion checks.
32 SM_DEBUG_T SmExpensiveAssert = SM_DEBUG_INITIALIZER("sm_check_assert",
33 "@(#)$Debug: sm_check_assert - check assertions $");
35 SM_DEBUG_T SmExpensiveRequire = SM_DEBUG_INITIALIZER("sm_check_require",
36 "@(#)$Debug: sm_check_require - check function preconditions $");
38 SM_DEBUG_T SmExpensiveEnsure = SM_DEBUG_INITIALIZER("sm_check_ensure",
39 "@(#)$Debug: sm_check_ensure - check function postconditions $");
42 ** Debug category: send self SIGSTOP on fatal error,
43 ** so that you can run a debugger on the stopped process.
46 SM_DEBUG_T SmAbortStop = SM_DEBUG_INITIALIZER("sm_abort_stop",
47 "@(#)$Debug: sm_abort_stop - stop process on fatal error $");
50 ** SM_ABORT_DEFAULTHANDLER -- Default procedure for abnormal program
53 ** The goal is to display an error message without disturbing the
54 ** process state too much, then dump core.
57 ** filename -- filename (can be NULL).
58 ** lineno -- line number.
66 sm_abort_defaulthandler __P((
72 sm_abort_defaulthandler(filename, lineno, msg)
78 sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s:%d: %s\n", filename,
81 sm_io_fprintf(smioerr, SM_TIME_DEFAULT, "%s\n", msg);
82 sm_io_flush(smioerr, SM_TIME_DEFAULT);
84 if (sm_debug_active(&SmAbortStop, 1))
85 kill(getpid(), SIGSTOP);
91 ** This is the action to be taken to cause abnormal program termination.
94 static SM_ABORT_HANDLER_T SmAbortHandler = sm_abort_defaulthandler;
97 ** SM_ABORT_SETHANDLER -- Set handler for SM_ABORT()
99 ** This allows you to set a handler function for causing abnormal
100 ** program termination; it is called when a logic bug is detected.
110 sm_abort_sethandler(f)
111 SM_ABORT_HANDLER_T f;
114 SmAbortHandler = sm_abort_defaulthandler;
120 ** SM_ABORT -- Call it when you have detected a logic bug.
123 ** fmt -- format string.
132 sm_abort(char *fmt, ...)
133 #else /* SM_VA_STD */
134 sm_abort(fmt, va_alist)
137 #endif /* SM_VA_STD */
142 SM_VA_START(ap, fmt);
143 sm_vsnprintf(msg, sizeof msg, fmt, ap);
145 sm_abort_at(NULL, 0, msg);
149 ** SM_ABORT_AT -- Initiate abnormal program termination.
151 ** This is the low level function that is called to initiate abnormal
152 ** program termination. It prints an error message and terminates the
153 ** program. It is called by sm_abort and by the assertion macros.
154 ** If filename != NULL then filename and lineno specify the line of source
155 ** code at which the bug was detected.
158 ** filename -- filename (can be NULL).
159 ** lineno -- line number.
167 sm_abort_at(filename, lineno, msg)
168 const char *filename;
173 (*SmAbortHandler)(filename, lineno, msg);
175 sm_io_fprintf(smioerr, SM_TIME_DEFAULT,
176 "exception raised by abort handler:\n");
177 sm_exc_print(exc, smioerr);
178 sm_io_flush(smioerr, SM_TIME_DEFAULT);
182 ** SmAbortHandler isn't supposed to return.
183 ** Since it has, let's make sure that the program is terminated.