/contrib/bind9/lib/isc/assertions.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 139 lines · 81 code · 18 blank · 40 comment · 12 complexity · 65047286e487cfd8dc92bfe9e63b4455 MD5 · raw file

  1. /*
  2. * Copyright (C) 2004, 2005, 2007-2009 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1997-2001 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: assertions.c,v 1.26 2009/09/29 15:06:07 fdupont Exp $ */
  18. /*! \file */
  19. #include <config.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <isc/assertions.h>
  23. #include <isc/backtrace.h>
  24. #include <isc/msgs.h>
  25. #include <isc/result.h>
  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. * Forward.
  34. */
  35. static void
  36. default_callback(const char *, int, isc_assertiontype_t, const char *);
  37. static isc_assertioncallback_t isc_assertion_failed_cb = default_callback;
  38. /*%
  39. * Public.
  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. {
  47. isc_assertion_failed_cb(file, line, type, cond);
  48. abort();
  49. /* NOTREACHED */
  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. /*% Type to Text */
  60. const char *
  61. isc_assertion_typetotext(isc_assertiontype_t type) {
  62. const char *result;
  63. /*
  64. * These strings have purposefully not been internationalized
  65. * because they are considered to essentially be keywords of
  66. * the ISC development environment.
  67. */
  68. switch (type) {
  69. case isc_assertiontype_require:
  70. result = "REQUIRE";
  71. break;
  72. case isc_assertiontype_ensure:
  73. result = "ENSURE";
  74. break;
  75. case isc_assertiontype_insist:
  76. result = "INSIST";
  77. break;
  78. case isc_assertiontype_invariant:
  79. result = "INVARIANT";
  80. break;
  81. default:
  82. result = NULL;
  83. }
  84. return (result);
  85. }
  86. /*
  87. * Private.
  88. */
  89. static void
  90. default_callback(const char *file, int line, isc_assertiontype_t type,
  91. const char *cond)
  92. {
  93. void *tracebuf[BACKTRACE_MAXFRAME];
  94. int i, nframes;
  95. const char *logsuffix = ".";
  96. const char *fname;
  97. isc_result_t result;
  98. result = isc_backtrace_gettrace(tracebuf, BACKTRACE_MAXFRAME, &nframes);
  99. if (result == ISC_R_SUCCESS && nframes > 0)
  100. logsuffix = ", back trace";
  101. fprintf(stderr, "%s:%d: %s(%s) %s%s\n",
  102. file, line, isc_assertion_typetotext(type), cond,
  103. isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
  104. ISC_MSG_FAILED, "failed"), logsuffix);
  105. if (result == ISC_R_SUCCESS) {
  106. for (i = 0; i < nframes; i++) {
  107. unsigned long offset;
  108. fname = NULL;
  109. result = isc_backtrace_getsymbol(tracebuf[i], &fname,
  110. &offset);
  111. if (result == ISC_R_SUCCESS) {
  112. fprintf(stderr, "#%d %p in %s()+0x%lx\n", i,
  113. tracebuf[i], fname, offset);
  114. } else {
  115. fprintf(stderr, "#%d %p in ??\n", i,
  116. tracebuf[i]);
  117. }
  118. }
  119. }
  120. fflush(stderr);
  121. }