2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 * @brief Various functions to aid in debugging
21 * @copyright 2013 The FreeRADIUS server project
22 * @copyright 2013 Arran Cudbard-Bell <a.cudbardb@freeradius.org>
25 #include <freeradius-devel/libradius.h>
29 #if defined(HAVE_MALLOPT) && defined(HAVE_MALLOC_H)
34 * runtime backtrace functions are not POSIX but are included in
35 * glibc, OSX >= 10.5 and various BSDs
38 # include <execinfo.h>
41 #ifdef HAVE_SYS_PRCTL_H
42 # include <sys/prctl.h>
45 #ifdef HAVE_SYS_PTRACE_H
46 # include <sys/ptrace.h>
47 # if !defined(PTRACE_ATTACH) && defined(PT_ATTACH)
48 # define PTRACE_ATTACH PT_ATTACH
50 # if !defined(PTRACE_CONT) && defined(PT_CONTINUE)
51 # define PTRACE_CONT PT_CONTINUE
53 # if !defined(PTRACE_DETACH) && defined(PT_DETACH)
54 # define PTRACE_DETACH PT_DETACH
58 #ifdef HAVE_SYS_RESOURCE_H
59 # include <sys/resource.h>
63 # define PTHREAD_MUTEX_LOCK pthread_mutex_lock
64 # define PTHREAD_MUTEX_UNLOCK pthread_mutex_unlock
66 # define PTHREAD_MUTEX_LOCK(_x)
67 # define PTHREAD_MUTEX_UNLOCK(_x)
71 # ifndef MAX_BT_FRAMES
72 # define MAX_BT_FRAMES 128
75 # define MAX_BT_CBUFF 1048576 //!< Should be a power of 2
78 # ifdef HAVE_PTHREAD_H
79 static pthread_mutex_t fr_debug_init = PTHREAD_MUTEX_INITIALIZER;
82 typedef struct fr_bt_info {
83 void *obj; //!< Memory address of the block of allocated memory.
84 void *frames[MAX_BT_FRAMES]; //!< Backtrace frame data
85 int count; //!< Number of frames stored
89 void *obj; //!< Pointer to the parent object, this is our needle
90 //!< when we iterate over the contents of the circular buffer.
91 fr_cbuff_t *cbuff; //!< Where we temporarily store the backtraces
95 static char panic_action[512]; //!< The command to execute when panicking.
96 static fr_fault_cb_t panic_cb = NULL; //!< Callback to execute whilst panicking, before the
98 static fr_fault_log_t fr_fault_log = NULL; //!< Function to use to process logging output.
99 static int fr_fault_log_fd = STDERR_FILENO; //!< Where to write debug output.
101 static int debugger_attached = -1; //!< Whether were attached to by a debugger.
103 #ifdef HAVE_SYS_RESOURCE_H
104 static struct rlimit core_limits;
107 static TALLOC_CTX *talloc_null_ctx;
108 static TALLOC_CTX *talloc_autofree_ctx;
110 #define FR_FAULT_LOG(fmt, ...) fr_fault_log(fmt "\n", ## __VA_ARGS__)
112 #ifdef HAVE_SYS_PTRACE_H
114 # define _PTRACE(_x, _y) ptrace(_x, _y, NULL, NULL)
116 # define _PTRACE(_x, _y) ptrace(_x, _y, NULL, 0)
119 /** Determine if we're running under a debugger by attempting to attach using pattach
121 * @return 0 if we're not, 1 if we are, -1 if we can't tell.
123 static int fr_debugger_attached(void)
127 int from_child[2] = {-1, -1};
129 if (pipe(from_child) < 0) {
130 fr_strerror_printf("Debugger check failed: Error opening internal pipe: %s", fr_syserror(errno));
136 fr_strerror_printf("Debugger check failed: Error forking: %s", fr_syserror(errno));
143 int ppid = getppid();
145 /* Close parent's side */
146 close(from_child[0]);
148 if (_PTRACE(PTRACE_ATTACH, ppid) == 0) {
149 /* If we attached then we're not running under a debugger */
150 write(from_child[1], &ret, sizeof(ret));
152 /* Wait for the parent to stop and continue it */
153 waitpid(ppid, NULL, 0);
154 _PTRACE(PTRACE_CONT, ppid);
157 _PTRACE(PTRACE_DETACH, ppid);
162 /* Something is already attached */
163 write(from_child[1], &ret, 1);
171 * The child writes a 1 if pattach failed else 0.
173 * This read may be interrupted by pattach,
174 * which is why we need the loop.
176 while ((read(from_child[0], &ret, 1) < 0) && (errno == EINTR));
178 /* Ret not updated */
180 fr_strerror_printf("Debugger check failed: Error getting status from child: %s",
184 /* Close the pipes here (if we did it above, it might race with pattach) */
185 close(from_child[1]);
186 close(from_child[0]);
188 /* Collect the status of the child */
189 waitpid(pid, NULL, 0);
195 static int fr_debugger_attached(void)
197 fr_strerror_printf("Debugger check failed: PTRACE not available");
203 /** Break in debugger (if were running under a debugger)
205 * If the server is running under a debugger this will raise a
206 * SIGTRAP which will pause the running process.
208 * If the server is not running under debugger then this will do nothing.
210 void fr_debug_break(void)
212 if (debugger_attached == -1) {
213 debugger_attached = fr_debugger_attached();
216 if (debugger_attached == 1) {
217 fprintf(stderr, "Debugger detected, raising SIGTRAP\n");
225 /** Print backtrace entry for a given object
227 * @param cbuff to search in.
228 * @param obj pointer to original object
230 void backtrace_print(fr_cbuff_t *cbuff, void *obj)
235 while ((p = fr_cbuff_rp_next(cbuff, NULL))) {
236 if ((p->obj == obj) || !obj) {
239 fprintf(stderr, "Stacktrace for: %p\n", p->obj);
240 backtrace_symbols_fd(p->frames, p->count, STDERR_FILENO);
245 fprintf(stderr, "No backtrace available for %p", obj);
249 /** Generate a backtrace for an object
251 * If this is the first entry being inserted
253 int fr_backtrace_do(fr_bt_marker_t *marker)
257 if (!fr_assert(marker->obj) || !fr_assert(marker->cbuff)) return -1;
259 bt = talloc_zero(NULL, fr_bt_info_t);
262 bt->obj = marker->obj;
263 bt->count = backtrace(bt->frames, MAX_BT_FRAMES);
265 fr_cbuff_rp_insert(marker->cbuff, bt);
270 /** Inserts a backtrace marker into the provided context
272 * Allows for maximum laziness and will initialise a circular buffer if one has not already been created.
274 * Code augmentation should look something like:
276 // Create a static cbuffer pointer, the first call to backtrace_attach will initialise it
277 static fr_cbuff_t *my_obj_bt;
279 my_obj_t *alloc_my_obj(TALLOC_CTX *ctx) {
282 this = talloc(ctx, my_obj_t);
284 // Attach backtrace marker to object
285 backtrace_attach(&my_obj_bt, this);
291 * Then, later when a double free occurs:
293 (gdb) call backtrace_print(&my_obj_bt, <pointer to double freed memory>)
296 * which should print a limited backtrace to stderr. Note, this backtrace will not include any argument
297 * values, but should at least show the code path taken.
299 * @param cbuff this should be a pointer to a static *fr_cbuff.
300 * @param obj we want to generate a backtrace for.
302 fr_bt_marker_t *fr_backtrace_attach(fr_cbuff_t **cbuff, TALLOC_CTX *obj)
304 fr_bt_marker_t *marker;
306 if (*cbuff == NULL) {
307 PTHREAD_MUTEX_LOCK(&fr_debug_init);
308 /* Check again now we hold the mutex - eww*/
309 if (*cbuff == NULL) *cbuff = fr_cbuff_alloc(NULL, MAX_BT_CBUFF, true);
310 PTHREAD_MUTEX_UNLOCK(&fr_debug_init);
313 marker = talloc(obj, fr_bt_marker_t);
318 marker->obj = (void *) obj;
319 marker->cbuff = *cbuff;
321 fprintf(stderr, "Backtrace attached to %s %p\n", talloc_get_name(obj), obj);
323 * Generate the backtrace for memory allocation
325 fr_backtrace_do(marker);
326 talloc_set_destructor(marker, fr_backtrace_do);
331 void backtrace_print(UNUSED fr_cbuff_t *cbuff, UNUSED void *obj)
333 fprintf(stderr, "Server built without fr_backtrace_* support, requires execinfo.h and possibly -lexecinfo\n");
335 fr_bt_marker_t *fr_backtrace_attach(UNUSED fr_cbuff_t **cbuff, UNUSED TALLOC_CTX *obj)
337 fprintf(stderr, "Server built without fr_backtrace_* support, requires execinfo.h and possibly -lexecinfo\n");
340 #endif /* ifdef HAVE_EXECINFO */
342 static int _panic_on_free(UNUSED char *foo)
345 return -1; /* this should make the free fail */
348 /** Insert memory into the context of another talloc memory chunk which
349 * causes a panic when freed.
351 * @param ctx TALLOC_CTX to monitor for frees.
353 void fr_panic_on_free(TALLOC_CTX *ctx)
357 ptr = talloc(ctx, char);
358 talloc_set_destructor(ptr, _panic_on_free);
361 /** Set the dumpable flag, also controls whether processes can PATTACH
363 * @param dumpable whether we should allow core dumping
365 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_SET_DUMPABLE)
366 static int fr_set_dumpable_flag(bool dumpable)
368 if (prctl(PR_SET_DUMPABLE, dumpable ? 1 : 0) < 0) {
369 fr_strerror_printf("Cannot re-enable core dumps: prctl(PR_SET_DUMPABLE) failed: %s",
377 static int fr_set_dumpable_flag(UNUSED bool dumpable)
379 fr_strerror_printf("Changing value of PR_DUMPABLE not supported on this system");
384 /** Get the processes dumpable flag
387 #if defined(HAVE_SYS_PRCTL_H) && defined(PR_GET_DUMPABLE)
388 static int fr_get_dumpable_flag(void)
392 ret = prctl(PR_GET_DUMPABLE);
394 fr_strerror_printf("Cannot get dumpable flag: %s", fr_syserror(errno));
399 * Linux is crazy and prctl sometimes returns 2 for disabled
401 if (ret != 1) return 0;
405 static int fr_get_dumpable_flag(void)
407 fr_strerror_printf("Getting value of PR_DUMPABLE not supported on this system");
413 /** Get the current maximum for core files
415 * Do this before anything else so as to ensure it's properly initialized.
417 int fr_set_dumpable_init(void)
419 #ifdef HAVE_SYS_RESOURCE_H
420 if (getrlimit(RLIMIT_CORE, &core_limits) < 0) {
421 fr_strerror_printf("Failed to get current core limit: %s", fr_syserror(errno));
428 /** Enable or disable core dumps
430 * @param allow_core_dumps whether to enable or disable core dumps.
432 int fr_set_dumpable(bool allow_core_dumps)
435 * If configured, turn core dumps off.
437 if (!allow_core_dumps) {
438 #ifdef HAVE_SYS_RESOURCE_H
439 struct rlimit no_core;
441 no_core.rlim_cur = 0;
442 no_core.rlim_max = 0;
444 if (setrlimit(RLIMIT_CORE, &no_core) < 0) {
445 fr_strerror_printf("Failed disabling core dumps: %s", fr_syserror(errno));
453 if (fr_set_dumpable_flag(true) < 0) return -1;
456 * Reset the core dump limits to their original value.
458 #ifdef HAVE_SYS_RESOURCE_H
459 if (setrlimit(RLIMIT_CORE, &core_limits) < 0) {
460 fr_strerror_printf("Cannot update core dump limit: %s", fr_syserror(errno));
468 /** Check to see if panic_action file is world writeable
470 * @return 0 if file is OK, else -1.
472 static int fr_fault_check_permissions(void)
480 * Try and guess which part of the command is the binary, and check to see if
481 * it's world writeable, to try and save the admin from their own stupidity.
483 * @fixme we should do this properly and take into account single and double
486 if ((q = strchr(panic_action, ' '))) {
488 * need to use a static buffer, because mallocing memory in a signal handler
489 * is a bad idea and can result in deadlock.
491 len = snprintf(filename, sizeof(filename), "%.*s", (int)(q - panic_action), panic_action);
492 if (is_truncated(len, sizeof(filename))) {
493 fr_strerror_printf("Failed writing panic_action to temporary buffer (truncated)");
501 if (stat(p, &statbuf) == 0) {
503 if ((statbuf.st_mode & S_IWOTH) != 0) {
504 fr_strerror_printf("panic_action file \"%s\" is globally writable", p);
513 /** Prints a simple backtrace (if execinfo is available) and calls panic_action if set.
517 void fr_fault(int sig)
519 char cmd[sizeof(panic_action) + 20];
521 size_t left = sizeof(cmd), ret;
523 char const *p = panic_action;
529 * Makes the backtraces slightly cleaner
531 memset(cmd, 0, sizeof(cmd));
533 FR_FAULT_LOG("CAUGHT SIGNAL: %s", strsignal(sig));
536 * Check for administrator sanity.
538 if (fr_fault_check_permissions() < 0) {
539 FR_FAULT_LOG("Refusing to execute panic action: %s", fr_strerror());
544 * Run the callback if one was registered
546 if (panic_cb && (panic_cb(sig) < 0)) goto finish;
549 * Produce a simple backtrace - They've very basic but at least give us an
550 * idea of the area of the code we hit the issue in.
552 * See below in fr_fault_setup() and
553 * https://sourceware.org/bugzilla/show_bug.cgi?id=16159
554 * for why we only print backtraces in debug builds if we're using GLIBC.
556 #if defined(HAVE_EXECINFO) && (!defined(NDEBUG) || !defined(__GNUC__))
558 size_t frame_count, i;
559 void *stack[MAX_BT_FRAMES];
562 frame_count = backtrace(stack, MAX_BT_FRAMES);
564 FR_FAULT_LOG("Backtrace of last %zu frames:", frame_count);
567 * Only use backtrace_symbols() if we don't have a logging fd.
568 * If the server has experienced memory corruption, there's
569 * a high probability that calling backtrace_symbols() which
570 * mallocs more memory, will fail.
572 if (fr_fault_log_fd < 0) {
573 strings = backtrace_symbols(stack, frame_count);
574 for (i = 0; i < frame_count; i++) {
575 FR_FAULT_LOG("%s", strings[i]);
579 backtrace_symbols_fd(stack, frame_count, fr_fault_log_fd);
584 /* No panic action set... */
585 if (panic_action[0] == '\0') {
586 FR_FAULT_LOG("No panic action set");
590 /* Substitute %p for the current PID (useful for attaching a debugger) */
591 while ((q = strstr(p, "%p"))) {
592 out += ret = snprintf(out, left, "%.*s%d", (int) (q - p), p, (int) getpid());
595 FR_FAULT_LOG("Panic action too long");
601 if (strlen(p) >= left) goto oob;
602 strlcpy(out, p, left);
604 FR_FAULT_LOG("Calling: %s", cmd);
607 bool disable = false;
610 * Here we temporarily enable the dumpable flag so if GBD or LLDB
611 * is called in the panic_action, they can pattach to the running
614 if (fr_get_dumpable_flag() == 0) {
615 if ((fr_set_dumpable_flag(true) < 0) || !fr_get_dumpable_flag()) {
616 FR_FAULT_LOG("Failed setting dumpable flag, pattach may not work: %s", fr_strerror());
620 FR_FAULT_LOG("Temporarily setting PR_DUMPABLE to 1");
626 * We only want to error out here, if dumpable was originally disabled
627 * and we managed to change the value to enabled, but failed
628 * setting it back to disabled.
631 FR_FAULT_LOG("Resetting PR_DUMPABLE to 0");
632 if (fr_set_dumpable_flag(false) < 0) {
633 FR_FAULT_LOG("Failed reseting dumpable flag to off: %s", fr_strerror());
634 FR_FAULT_LOG("Exiting due to insecure process state");
640 FR_FAULT_LOG("Panic action exited with %i", code);
644 if (sig == SIGUSR1) {
652 /** Work around debuggers which can't backtrace past the signal handler
654 * At least this provides us some information when we get talloc errors.
656 static void _fr_talloc_fault(char const *reason)
658 fr_fault_log("talloc abort: %s\n", reason);
663 /** Wrapper to pass talloc log output to our fr_fault_log function
666 static void _fr_talloc_log(char const *msg)
668 fr_fault_log("%s\n", msg);
671 /** Generate a talloc memory report for a context and print to stderr/stdout
673 * @param ctx to generate a report for, may be NULL in which case the root context is used.
675 int fr_log_talloc_report(TALLOC_CTX *ctx)
681 fd = dup(fr_fault_log_fd);
683 fr_strerror_printf("Couldn't write memory report, failed to dup log fd: %s", fr_syserror(errno));
686 log = fdopen(fd, "w");
689 fr_strerror_printf("Couldn't write memory report, fdopen failed: %s", fr_syserror(errno));
694 fprintf(log, "Current state of talloced memory:\n");
695 talloc_report_full(talloc_null_ctx, log);
697 fprintf(log, "Talloc chunk lineage:\n");
698 fprintf(log, "%p (%s)", ctx, talloc_get_name(ctx));
699 while ((ctx = talloc_parent(ctx))) fprintf(log, " < %p (%s)", ctx, talloc_get_name(ctx));
703 fprintf(log, "Talloc context level %i:\n", i++);
704 talloc_report_full(ctx, log);
705 } while ((ctx = talloc_parent(ctx)) &&
706 (talloc_parent(ctx) != talloc_autofree_ctx) && /* Stop before we hit the autofree ctx */
707 (talloc_parent(ctx) != talloc_null_ctx)); /* Stop before we hit NULL ctx */
715 /** Signal handler to print out a talloc memory report
719 static void _fr_fault_mem_report(int sig)
721 FR_FAULT_LOG("CAUGHT SIGNAL: %s", strsignal(sig));
723 if (fr_log_talloc_report(NULL) < 0) fr_perror("memreport");
726 static int _fr_disable_null_tracking(UNUSED bool *p)
728 talloc_disable_null_tracking();
732 /** Registers signal handlers to execute panic_action on fatal signal
734 * May be called multiple time to change the panic_action/program.
736 * @param cmd to execute on fault. If present %p will be substituted
737 * for the parent PID before the command is executed, and %e
738 * will be substituted for the currently running program.
739 * @param program Name of program currently executing (argv[0]).
740 * @return 0 on success -1 on failure.
742 int fr_fault_setup(char const *cmd, char const *program)
744 static bool setup = false;
746 char *out = panic_action;
747 size_t left = sizeof(panic_action), ret;
753 /* Substitute %e for the current program */
754 while ((q = strstr(p, "%e"))) {
755 out += ret = snprintf(out, left, "%.*s%s", (int) (q - p), p, program ? program : "");
758 fr_strerror_printf("Panic action too long");
764 if (strlen(p) >= left) goto oob;
765 strlcpy(out, p, left);
767 *panic_action = '\0';
771 * Check for administrator sanity.
773 if (fr_fault_check_permissions() < 0) return -1;
775 /* Unsure what the side effects of changing the signal handler mid execution might be */
777 debugger_attached = fr_debugger_attached();
780 * These signals can't be properly dealt with in the debugger
781 * if we set our own signal handlers
783 if (debugger_attached == 0) {
785 if (fr_set_signal(SIGSEGV, fr_fault) < 0) return -1;
788 if (fr_set_signal(SIGBUS, fr_fault) < 0) return -1;
791 if (fr_set_signal(SIGFPE, fr_fault) < 0) return -1;
795 if (fr_set_signal(SIGABRT, fr_fault) < 0) return -1;
798 * Use this instead of abort so we get a
799 * full backtrace with broken versions of LLDB
801 talloc_set_abort_fn(_fr_talloc_fault);
805 if (fr_set_signal(SIGUSR1, fr_fault) < 0) return -1;
809 if (fr_set_signal(SIGUSR2, _fr_fault_mem_report) < 0) return -1;
813 * Setup the default logger
815 if (!fr_fault_log) fr_fault_set_log_fn(NULL);
816 talloc_set_log_fn(_fr_talloc_log);
819 * Needed for memory reports
825 tmp = talloc(NULL, bool);
826 talloc_null_ctx = talloc_parent(tmp);
830 * Disable null tracking on exit, else valgrind complains
832 talloc_autofree_ctx = talloc_autofree_context();
833 marker = talloc(talloc_autofree_ctx, bool);
834 talloc_set_destructor(marker, _fr_disable_null_tracking);
837 #if defined(HAVE_MALLOPT) && !defined(NDEBUG)
839 * If were using glibc malloc > 2.4 this scribbles over
840 * uninitialised and freed memory, to make memory issues easier
843 if (!getenv("TALLOC_FREE_FILL")) mallopt(M_PERTURB, 0x42);
844 mallopt(M_CHECK_ACTION, 3);
847 #if defined(HAVE_EXECINFO) && defined(__GNUC__) && !defined(NDEBUG)
849 * We need to pre-load lgcc_s, else we can get into a deadlock
850 * in fr_fault, as backtrace() attempts to dlopen it.
852 * Apparently there's a performance impact of loading lgcc_s,
853 * so only do it if this is a debug build.
855 * See: https://sourceware.org/bugzilla/show_bug.cgi?id=16159
860 backtrace(stack, 10);
869 /** Set a callback to be called before fr_fault()
871 * @param func to execute. If callback returns < 0
872 * fr_fault will exit before running panic_action code.
874 void fr_fault_set_cb(fr_fault_cb_t func)
879 /** Default logger, logs output to stderr
882 static void CC_HINT(format (printf, 1, 2)) _fr_fault_log(char const *msg, ...)
887 vfprintf(stderr, msg, ap);
892 /** Set a file descriptor to log panic_action output to.
894 * @param func to call to output log messages.
896 void fr_fault_set_log_fn(fr_fault_log_t func)
898 fr_fault_log = func ? func : _fr_fault_log;
901 /** Set a file descriptor to log memory reports to.
903 * @param fd to write output to.
905 void fr_fault_set_log_fd(int fd)
907 fr_fault_log_fd = fd;
911 #ifdef WITH_VERIFY_PTR
914 * Verify a VALUE_PAIR
916 inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp)
919 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line);
924 (void) talloc_get_type_abort(vp, VALUE_PAIR);
926 if (vp->data.ptr) switch (vp->da->type) {
933 if (!talloc_get_type(vp->data.ptr, uint8_t)) {
934 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
935 "uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
936 (void) talloc_get_type_abort(vp->data.ptr, uint8_t);
939 len = talloc_array_length(vp->vp_octets);
940 if (vp->length > len) {
941 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
942 "uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->length, len);
947 parent = talloc_parent(vp->data.ptr);
949 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
950 "parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
951 file, line, vp->da->name,
952 vp, parent, parent ? talloc_get_name(parent) : "NULL");
964 if (!talloc_get_type(vp->data.ptr, char)) {
965 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
966 "char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
967 (void) talloc_get_type_abort(vp->data.ptr, char);
970 len = (talloc_array_length(vp->vp_strvalue) - 1);
971 if (vp->length > len) {
972 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
973 "char buffer length %zu\n", file, line, vp->da->name, vp->length, len);
978 if (vp->vp_strvalue[vp->length] != '\0') {
979 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 "
980 "terminated\n", file, line, vp->da->name);
985 parent = talloc_parent(vp->data.ptr);
987 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" uint8_t buffer is not "
988 "parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
989 file, line, vp->da->name,
990 vp, parent, parent ? talloc_get_name(parent) : "NULL");
1003 * Verify a pair list
1005 void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps)
1011 for (vp = fr_cursor_init(&cursor, &vps);
1013 vp = fr_cursor_next(&cursor)) {
1016 parent = talloc_parent(vp);
1017 if (expected && (parent != expected)) {
1018 FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented "
1019 "by %p (%s), instead parented by %p (%s)\n",
1020 file, line, vp->da->name,
1021 expected, talloc_get_name(expected),
1022 parent, parent ? talloc_get_name(parent) : "NULL");
1024 fr_log_talloc_report(expected);
1025 if (parent) fr_log_talloc_report(parent);
1034 bool fr_assert_cond(char const *file, int line, char const *expr, bool cond)
1037 FR_FAULT_LOG("SOFT ASSERT FAILED %s[%u]: %s", file, line, expr);
1038 #if !defined(NDEBUG) && defined(SIGUSR1)
1047 /** Exit possibly printing a message about why we're exiting.
1049 * Use the fr_exit(status) macro instead of calling this function
1052 * @param file where fr_exit() was called.
1053 * @param line where fr_exit() was called.
1054 * @param status we're exiting with.
1056 void NEVER_RETURNS _fr_exit(char const *file, int line, int status)
1059 char const *error = fr_strerror();
1061 if (error && (status != 0)) {
1062 FR_FAULT_LOG("EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error);
1064 FR_FAULT_LOG("EXIT(%i) CALLED %s[%u]", status, file, line);
1067 fr_debug_break(); /* If running under GDB we'll break here */
1072 /** Exit possibly printing a message about why we're exiting.
1074 * Use the fr_exit_now(status) macro instead of calling this function
1077 * @param file where fr_exit_now() was called.
1078 * @param line where fr_exit_now() was called.
1079 * @param status we're exiting with.
1081 void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status)
1084 char const *error = fr_strerror();
1086 if (error && (status != 0)) {
1087 FR_FAULT_LOG("_EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error);
1089 FR_FAULT_LOG("_EXIT(%i) CALLED %s[%u]", status, file, line);
1092 fr_debug_break(); /* If running under GDB we'll break here */