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.
11 #pragma ident "%Z%%M% %I% %E% SMI"
14 SM_RCSID("@(#)$Id: assert.c,v 1.25.2.1 2003/12/05 22:44:17 ca Exp $")
17 ** Abnormal program termination and assertion checking.
18 ** For documentation, see assert.html.
25 #include <sm/assert.h>
28 #include <sm/varargs.h>
31 ** Debug categories that are used to guard expensive assertion checks.
34 SM_DEBUG_T SmExpensiveAssert
= SM_DEBUG_INITIALIZER("sm_check_assert",
35 "@(#)$Debug: sm_check_assert - check assertions $");
37 SM_DEBUG_T SmExpensiveRequire
= SM_DEBUG_INITIALIZER("sm_check_require",
38 "@(#)$Debug: sm_check_require - check function preconditions $");
40 SM_DEBUG_T SmExpensiveEnsure
= SM_DEBUG_INITIALIZER("sm_check_ensure",
41 "@(#)$Debug: sm_check_ensure - check function postconditions $");
44 ** Debug category: send self SIGSTOP on fatal error,
45 ** so that you can run a debugger on the stopped process.
48 SM_DEBUG_T SmAbortStop
= SM_DEBUG_INITIALIZER("sm_abort_stop",
49 "@(#)$Debug: sm_abort_stop - stop process on fatal error $");
52 ** SM_ABORT_DEFAULTHANDLER -- Default procedure for abnormal program
55 ** The goal is to display an error message without disturbing the
56 ** process state too much, then dump core.
59 ** filename -- filename (can be NULL).
60 ** lineno -- line number.
68 sm_abort_defaulthandler
__P((
74 sm_abort_defaulthandler(filename
, lineno
, msg
)
80 sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
, "%s:%d: %s\n", filename
,
83 sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
, "%s\n", msg
);
84 sm_io_flush(smioerr
, SM_TIME_DEFAULT
);
86 if (sm_debug_active(&SmAbortStop
, 1))
87 kill(getpid(), SIGSTOP
);
93 ** This is the action to be taken to cause abnormal program termination.
96 static SM_ABORT_HANDLER_T SmAbortHandler
= sm_abort_defaulthandler
;
99 ** SM_ABORT_SETHANDLER -- Set handler for SM_ABORT()
101 ** This allows you to set a handler function for causing abnormal
102 ** program termination; it is called when a logic bug is detected.
112 sm_abort_sethandler(f
)
113 SM_ABORT_HANDLER_T f
;
116 SmAbortHandler
= sm_abort_defaulthandler
;
122 ** SM_ABORT -- Call it when you have detected a logic bug.
125 ** fmt -- format string.
134 sm_abort(char *fmt
, ...)
135 #else /* SM_VA_STD */
136 sm_abort(fmt
, va_alist
)
139 #endif /* SM_VA_STD */
144 SM_VA_START(ap
, fmt
);
145 sm_vsnprintf(msg
, sizeof msg
, fmt
, ap
);
147 sm_abort_at(NULL
, 0, msg
);
151 ** SM_ABORT_AT -- Initiate abnormal program termination.
153 ** This is the low level function that is called to initiate abnormal
154 ** program termination. It prints an error message and terminates the
155 ** program. It is called by sm_abort and by the assertion macros.
156 ** If filename != NULL then filename and lineno specify the line of source
157 ** code at which the bug was detected.
160 ** filename -- filename (can be NULL).
161 ** lineno -- line number.
169 sm_abort_at(filename
, lineno
, msg
)
170 const char *filename
;
175 (*SmAbortHandler
)(filename
, lineno
, msg
);
177 sm_io_fprintf(smioerr
, SM_TIME_DEFAULT
,
178 "exception raised by abort handler:\n");
179 sm_exc_print(exc
, smioerr
);
180 sm_io_flush(smioerr
, SM_TIME_DEFAULT
);
184 ** SmAbortHandler isn't supposed to return.
185 ** Since it has, let's make sure that the program is terminated.