1 1.7 joerg /* $NetBSD: assertions.c,v 1.7 2011/09/16 16:05:58 joerg Exp $ */ 2 1.1 christos 3 1.1 christos /* 4 1.6 christos * Copyright (C) 2004, 2005, 2008 Internet Systems Consortium, Inc. ("ISC") 5 1.6 christos * Copyright (C) 1997, 1999, 2001 Internet Software Consortium. 6 1.1 christos * 7 1.6 christos * Permission to use, copy, modify, and/or distribute this software for any 8 1.1 christos * purpose with or without fee is hereby granted, provided that the above 9 1.1 christos * copyright notice and this permission notice appear in all copies. 10 1.1 christos * 11 1.6 christos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH 12 1.6 christos * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY 13 1.6 christos * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, 14 1.6 christos * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM 15 1.6 christos * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE 16 1.6 christos * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 17 1.6 christos * PERFORMANCE OF THIS SOFTWARE. 18 1.1 christos */ 19 1.1 christos 20 1.2 christos #include <sys/cdefs.h> 21 1.2 christos #if !defined(LINT) && !defined(CODECENTER) && !defined(lint) 22 1.2 christos #ifdef notdef 23 1.6 christos static const char rcsid[] = "Id: assertions.c,v 1.5 2008/11/14 02:36:51 marka Exp"; 24 1.2 christos #else 25 1.7 joerg __RCSID("$NetBSD: assertions.c,v 1.7 2011/09/16 16:05:58 joerg Exp $"); 26 1.2 christos #endif 27 1.1 christos #endif 28 1.1 christos 29 1.1 christos #include "port_before.h" 30 1.1 christos 31 1.1 christos #include <errno.h> 32 1.1 christos #include <stdio.h> 33 1.1 christos #include <stdlib.h> 34 1.1 christos #include <string.h> 35 1.1 christos 36 1.1 christos #include <isc/assertions.h> 37 1.1 christos 38 1.1 christos #include "port_after.h" 39 1.1 christos 40 1.1 christos /* 41 1.1 christos * Forward. 42 1.1 christos */ 43 1.1 christos 44 1.7 joerg __dead static void default_assertion_failed(const char *, int, assertion_type, 45 1.1 christos const char *, int); 46 1.1 christos 47 1.1 christos /* 48 1.1 christos * Public. 49 1.1 christos */ 50 1.1 christos 51 1.1 christos assertion_failure_callback __assertion_failed = default_assertion_failed; 52 1.1 christos 53 1.1 christos void 54 1.1 christos set_assertion_failure_callback(assertion_failure_callback f) { 55 1.1 christos if (f == NULL) 56 1.1 christos __assertion_failed = default_assertion_failed; 57 1.1 christos else 58 1.1 christos __assertion_failed = f; 59 1.1 christos } 60 1.1 christos 61 1.1 christos const char * 62 1.1 christos assertion_type_to_text(assertion_type type) { 63 1.1 christos const char *result; 64 1.1 christos 65 1.1 christos switch (type) { 66 1.1 christos case assert_require: 67 1.1 christos result = "REQUIRE"; 68 1.1 christos break; 69 1.1 christos case assert_ensure: 70 1.1 christos result = "ENSURE"; 71 1.1 christos break; 72 1.1 christos case assert_insist: 73 1.1 christos result = "INSIST"; 74 1.1 christos break; 75 1.1 christos case assert_invariant: 76 1.1 christos result = "INVARIANT"; 77 1.1 christos break; 78 1.1 christos default: 79 1.1 christos result = NULL; 80 1.1 christos } 81 1.1 christos return (result); 82 1.1 christos } 83 1.1 christos 84 1.1 christos /* 85 1.1 christos * Private. 86 1.1 christos */ 87 1.1 christos 88 1.6 christos /* coverity[+kill] */ 89 1.1 christos static void 90 1.1 christos default_assertion_failed(const char *file, int line, assertion_type type, 91 1.1 christos const char *cond, int print_errno) 92 1.1 christos { 93 1.1 christos fprintf(stderr, "%s:%d: %s(%s)%s%s failed.\n", 94 1.1 christos file, line, assertion_type_to_text(type), cond, 95 1.1 christos (print_errno) ? ": " : "", 96 1.1 christos (print_errno) ? strerror(errno) : ""); 97 1.1 christos abort(); 98 1.1 christos /* NOTREACHED */ 99 1.1 christos } 100 1.3 christos 101 1.3 christos /*! \file */ 102