"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/isc/assertions.c" (4 Sep 2020, 2887 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.tar.xz:


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 /*! \file */
   13 
   14 #include <stdio.h>
   15 #include <stdlib.h>
   16 
   17 #include <isc/assertions.h>
   18 #include <isc/backtrace.h>
   19 #include <isc/print.h>
   20 #include <isc/result.h>
   21 
   22 /*
   23  * The maximum number of stack frames to dump on assertion failure.
   24  */
   25 #ifndef BACKTRACE_MAXFRAME
   26 #define BACKTRACE_MAXFRAME 128
   27 #endif /* ifndef BACKTRACE_MAXFRAME */
   28 
   29 /*%
   30  * Forward.
   31  */
   32 static void
   33 default_callback(const char *, int, isc_assertiontype_t, const char *);
   34 
   35 static isc_assertioncallback_t isc_assertion_failed_cb = default_callback;
   36 
   37 /*%
   38  * Public.
   39  */
   40 
   41 /*% assertion failed handler */
   42 /* coverity[+kill] */
   43 void
   44 isc_assertion_failed(const char *file, int line, isc_assertiontype_t type,
   45              const char *cond) {
   46     isc_assertion_failed_cb(file, line, type, cond);
   47     abort();
   48     /* NOTREACHED */
   49 }
   50 
   51 /*% Set callback. */
   52 void
   53 isc_assertion_setcallback(isc_assertioncallback_t cb) {
   54     if (cb == NULL) {
   55         isc_assertion_failed_cb = default_callback;
   56     } else {
   57         isc_assertion_failed_cb = cb;
   58     }
   59 }
   60 
   61 /*% Type to Text */
   62 const char *
   63 isc_assertion_typetotext(isc_assertiontype_t type) {
   64     const char *result;
   65 
   66     /*
   67      * These strings have purposefully not been internationalized
   68      * because they are considered to essentially be keywords of
   69      * the ISC development environment.
   70      */
   71     switch (type) {
   72     case isc_assertiontype_require:
   73         result = "REQUIRE";
   74         break;
   75     case isc_assertiontype_ensure:
   76         result = "ENSURE";
   77         break;
   78     case isc_assertiontype_insist:
   79         result = "INSIST";
   80         break;
   81     case isc_assertiontype_invariant:
   82         result = "INVARIANT";
   83         break;
   84     default:
   85         result = "UNKNOWN";
   86     }
   87     return (result);
   88 }
   89 
   90 /*
   91  * Private.
   92  */
   93 
   94 static void
   95 default_callback(const char *file, int line, isc_assertiontype_t type,
   96          const char *cond) {
   97     void *tracebuf[BACKTRACE_MAXFRAME];
   98     int i, nframes;
   99     const char *logsuffix = ".";
  100     const char *fname;
  101     isc_result_t result;
  102 
  103     result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME, &nframes);
  104     if (result == ISC_R_SUCCESS && nframes > 0) {
  105         logsuffix = ", back trace";
  106     }
  107 
  108     fprintf(stderr, "%s:%d: %s(%s) failed%s\n", file, line,
  109         isc_assertion_typetotext(type), cond, logsuffix);
  110 
  111     if (result == ISC_R_SUCCESS) {
  112         for (i = 0; i < nframes; i++) {
  113             unsigned long offset;
  114 
  115             fname = NULL;
  116             result = isc_backtrace_getsymbol(tracebuf[i], &fname,
  117                              &offset);
  118             if (result == ISC_R_SUCCESS) {
  119                 fprintf(stderr, "#%d %p in %s()+0x%lx\n", i,
  120                     tracebuf[i], fname, offset);
  121             } else {
  122                 fprintf(stderr, "#%d %p in ??\n", i,
  123                     tracebuf[i]);
  124             }
  125         }
  126     }
  127     fflush(stderr);
  128 }