"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/lib/isc/assertions.c" (7 Sep 2020, 2963 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "assertions.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 
   13 /*! \file */
   14 
   15 #include <config.h>
   16 
   17 #include <stdio.h>
   18 #include <stdlib.h>
   19 
   20 #include <isc/assertions.h>
   21 #include <isc/backtrace.h>
   22 #include <isc/msgs.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
   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
   48 isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
   49              const char *cond)
   50 {
   51     isc_assertion_failed_cb(file, line, type, cond);
   52     abort();
   53     /* NOTREACHED */
   54 }
   55 
   56 /*% Set callback. */
   57 void
   58 isc_assertion_setcallback(isc_assertioncallback_t cb) {
   59     if (cb == NULL)
   60         isc_assertion_failed_cb = default_callback;
   61     else
   62         isc_assertion_failed_cb = cb;
   63 }
   64 
   65 /*% Type to Text */
   66 const char *
   67 isc_assertion_typetotext(isc_assertiontype_t type) {
   68     const char *result;
   69 
   70     /*
   71      * These strings have purposefully not been internationalized
   72      * because they are considered to essentially be keywords of
   73      * the ISC development environment.
   74      */
   75     switch (type) {
   76     case isc_assertiontype_require:
   77         result = "REQUIRE";
   78         break;
   79     case isc_assertiontype_ensure:
   80         result = "ENSURE";
   81         break;
   82     case isc_assertiontype_insist:
   83         result = "INSIST";
   84         break;
   85     case isc_assertiontype_invariant:
   86         result = "INVARIANT";
   87         break;
   88     default:
   89         result = NULL;
   90     }
   91     return (result);
   92 }
   93 
   94 /*
   95  * Private.
   96  */
   97 
   98 static void
   99 default_callback(const char *file, int line, isc_assertiontype_t type,
  100          const char *cond)
  101 {
  102     void *tracebuf[BACKTRACE_MAXFRAME];
  103     int i, nframes;
  104     const char *logsuffix = ".";
  105     const char *fname;
  106     isc_result_t result;
  107 
  108     result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME, &nframes);
  109         if (result == ISC_R_SUCCESS && nframes > 0)
  110             logsuffix = ", back trace";
  111 
  112     fprintf(stderr, "%s:%d: %s(%s) %s%s\n",
  113         file, line, isc_assertion_typetotext(type), cond,
  114         isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
  115                    ISC_MSG_FAILED, "failed"), logsuffix);
  116     if (result == ISC_R_SUCCESS) {
  117         for (i = 0; i < nframes; i++) {
  118             unsigned long offset;
  119 
  120             fname = NULL;
  121             result = isc_backtrace_getsymbol(tracebuf[i], &fname,
  122                              &offset);
  123             if (result == ISC_R_SUCCESS) {
  124                 fprintf(stderr, "#%d %p in %s()+0x%lx\n", i,
  125                     tracebuf[i], fname, offset);
  126             } else {
  127                 fprintf(stderr, "#%d %p in ??\n", i,
  128                     tracebuf[i]);
  129             }
  130         }
  131     }
  132     fflush(stderr);
  133 }