All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Macros | Functions | Variables
debug.c File Reference

Various functions to aid in debugging. More...

#include <assert.h>
#include <freeradius-devel/libradius.h>
#include <sys/stat.h>
#include <sys/wait.h>
+ Include dependency graph for debug.c:

Go to the source code of this file.

Macros

#define PTHREAD_MUTEX_LOCK(_x)
 
#define PTHREAD_MUTEX_UNLOCK(_x)
 
#define TALLOC_REPORT_MAX_DEPTH   20
 

Functions

static int _fr_disable_null_tracking (UNUSED bool *p)
 
void NEVER_RETURNS _fr_exit (char const *file, int line, int status)
 Exit possibly printing a message about why we're exiting. More...
 
void NEVER_RETURNS _fr_exit_now (char const *file, int line, int status)
 Exit possibly printing a message about why we're exiting. More...
 
static void _fr_talloc_fault (char const *reason) CC_HINT(noreturn)
 Callback executed on fatal talloc error. More...
 
static void _fr_talloc_fault_simple (char const *reason) CC_HINT(noreturn)
 Callback executed on fatal talloc error. More...
 
static void _fr_talloc_log (char const *msg)
 Wrapper to pass talloc log output to our fr_fault_log function. More...
 
static int _panic_on_free (UNUSED char *foo)
 
void backtrace_print (UNUSED fr_cbuff_t *cbuff, UNUSED void *obj)
 
bool fr_assert_cond (char const *file, int line, char const *expr, bool cond)
 A soft assertion which triggers the fault handler in debug builds. More...
 
fr_bt_marker_tfr_backtrace_attach (UNUSED fr_cbuff_t **cbuff, UNUSED TALLOC_CTX *obj)
 
void fr_debug_break (bool always)
 Break in debugger (if were running under a debugger) More...
 
char const * fr_debug_state_to_msg (fr_debug_state_t state)
 Return current value of debug_state. More...
 
NEVER_RETURNS void fr_fault (int sig)
 Prints a simple backtrace (if execinfo is available) and calls panic_action if set. More...
 
static int fr_fault_check_permissions (void)
 Check to see if panic_action file is world writeable. More...
 
void fr_fault_log (char const *msg,...)
 Log output to the fr_fault_log_fd. More...
 
void fr_fault_set_cb (fr_fault_cb_t func)
 Set a callback to be called before fr_fault() More...
 
void fr_fault_set_log_fd (int fd)
 Set a file descriptor to log memory reports to. More...
 
int fr_fault_setup (char const *cmd, char const *program)
 Registers signal handlers to execute panic_action on fatal signal. More...
 
static int fr_get_debug_state (void)
 
static int fr_get_dumpable_flag (void)
 Get the processes dumpable flag. More...
 
int fr_log_talloc_report (TALLOC_CTX *ctx)
 Generate a talloc memory report for a context and print to stderr/stdout. More...
 
void fr_panic_on_free (TALLOC_CTX *ctx)
 Insert memory into the context of another talloc memory chunk which causes a panic when freed. More...
 
int fr_reset_dumpable (void)
 Reset dumpable state to previously configured value. More...
 
int fr_set_dumpable (bool allow_core_dumps)
 Enable or disable core dumps. More...
 
static int fr_set_dumpable_flag (UNUSED bool dumpable)
 Set the dumpable flag, also controls whether processes can PATTACH. More...
 
int fr_set_dumpable_init (void)
 Get the current maximum for core files. More...
 
void fr_store_debug_state (void)
 Should be run before using setuid or setgid to get useful results. More...
 
void fr_talloc_fault_setup (void)
 Register talloc fault handlers. More...
 

Variables

static bool dump_core
 Whether we should drop a core on fatal signals. More...
 
fr_debug_state_t fr_debug_state = DEBUG_STATE_UNKNOWN
 Whether we're attached to by a debugger. More...
 
static int fr_fault_log_fd = STDERR_FILENO
 Where to write debug output. More...
 
static char panic_action [512]
 The command to execute when panicking. More...
 
static fr_fault_cb_t panic_cb = NULL
 Callback to execute whilst panicking, before the panic_action. More...
 
static TALLOC_CTX * talloc_autofree_ctx
 
static TALLOC_CTX * talloc_null_ctx
 

Detailed Description

Various functions to aid in debugging.

Definition in file debug.c.

Macro Definition Documentation

#define PTHREAD_MUTEX_LOCK (   _x)

Definition at line 63 of file debug.c.

#define PTHREAD_MUTEX_UNLOCK (   _x)

Definition at line 64 of file debug.c.

#define TALLOC_REPORT_MAX_DEPTH   20

Function Documentation

static int _fr_disable_null_tracking ( UNUSED bool *  p)
static

Definition at line 861 of file debug.c.

+ Here is the caller graph for this function:

void NEVER_RETURNS _fr_exit ( char const *  file,
int  line,
int  status 
)

Exit possibly printing a message about why we're exiting.

Note
Use the fr_exit(status) macro instead of calling this function directly.
Parameters
filewhere fr_exit() was called.
linewhere fr_exit() was called.
statuswe're exiting with.

Definition at line 1106 of file debug.c.

+ Here is the call graph for this function:

void NEVER_RETURNS _fr_exit_now ( char const *  file,
int  line,
int  status 
)

Exit possibly printing a message about why we're exiting.

Note
Use the fr_exit_now(status) macro instead of calling this function directly.
Parameters
filewhere fr_exit_now() was called.
linewhere fr_exit_now() was called.
statuswe're exiting with.

Definition at line 1130 of file debug.c.

+ Here is the call graph for this function:

static void _fr_talloc_fault ( char const *  reason)
static

Callback executed on fatal talloc error.

Translates a talloc abort into a fr_fault call. Mostly to work around issues with some debuggers not being able to attach after a SIGABRT has been raised.

Parameters
reasonstring provided by talloc.

Definition at line 789 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static void _fr_talloc_fault_simple ( char const *  reason)
static

Callback executed on fatal talloc error.

This is the simple version which mostly behaves the same way as the default one, and will not call panic_action.

Parameters
reasonstring provided by talloc.

Definition at line 763 of file debug.c.

+ Here is the caller graph for this function:

static void _fr_talloc_log ( char const *  msg)
static

Wrapper to pass talloc log output to our fr_fault_log function.

Definition at line 801 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int _panic_on_free ( UNUSED char *  foo)
static

Definition at line 438 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void backtrace_print ( UNUSED fr_cbuff_t cbuff,
UNUSED void *  obj 
)

Definition at line 427 of file debug.c.

bool fr_assert_cond ( char const *  file,
int  line,
char const *  expr,
bool  cond 
)

A soft assertion which triggers the fault handler in debug builds.

Parameters
filethe assertion failed in.
lineof the assertion in the file.
exprthat was evaluated.
condResult of evaluating the expression.
Returns
the value of cond.

Definition at line 1085 of file debug.c.

+ Here is the call graph for this function:

fr_bt_marker_t* fr_backtrace_attach ( UNUSED fr_cbuff_t **  cbuff,
UNUSED TALLOC_CTX *  obj 
)

Definition at line 431 of file debug.c.

void fr_debug_break ( bool  always)

Break in debugger (if were running under a debugger)

If the server is running under a debugger this will raise a SIGTRAP which will pause the running process.

If the server is not running under debugger then this will do nothing.

Definition at line 307 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

char const* fr_debug_state_to_msg ( fr_debug_state_t  state)

Return current value of debug_state.

Parameters
stateto translate into a humanly readable value.
Returns
humanly readable version of debug state.

Definition at line 278 of file debug.c.

+ Here is the caller graph for this function:

NEVER_RETURNS void fr_fault ( int  sig)

Prints a simple backtrace (if execinfo is available) and calls panic_action if set.

Parameters
sigcaught

Definition at line 629 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int fr_fault_check_permissions ( void  )
static

Check to see if panic_action file is world writeable.

Returns
  • 0 if file is OK.
  • -1 if the file is world writeable.

Definition at line 584 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_fault_log ( char const *  msg,
  ... 
)

Log output to the fr_fault_log_fd.

We used to support a user defined callback, which was set to a radlog function. Unfortunately, when logging to syslog, syslog would malloc memory which would result in a deadlock if fr_fault was triggered from within a malloc call.

Now we just write directly to the FD.

Definition at line 1057 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_fault_set_cb ( fr_fault_cb_t  func)

Set a callback to be called before fr_fault()

Parameters
functo execute. If callback returns < 0 fr_fault will exit before running panic_action code.

Definition at line 1043 of file debug.c.

+ Here is the caller graph for this function:

void fr_fault_set_log_fd ( int  fd)

Set a file descriptor to log memory reports to.

Parameters
fdto write output to.

Definition at line 1072 of file debug.c.

+ Here is the caller graph for this function:

int fr_fault_setup ( char const *  cmd,
char const *  program 
)

Registers signal handlers to execute panic_action on fatal signal.

May be called multiple time to change the panic_action/program.

Parameters
cmdto execute on fault. If present p will be substituted for the parent PID before the command is executed, and e will be substituted for the currently running program.
programName of program currently executing (argv[0]).
Returns
  • 0 on success.
  • -1 on failure.

Definition at line 890 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int fr_get_debug_state ( void  )
static

Definition at line 246 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int fr_get_dumpable_flag ( void  )
static

Get the processes dumpable flag.

Definition at line 501 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_log_talloc_report ( TALLOC_CTX *  ctx)

Generate a talloc memory report for a context and print to stderr/stdout.

Parameters
ctxto generate a report for, may be NULL in which case the root context is used.

Definition at line 810 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_panic_on_free ( TALLOC_CTX *  ctx)

Insert memory into the context of another talloc memory chunk which causes a panic when freed.

Parameters
ctxTALLOC_CTX to monitor for frees.

Definition at line 449 of file debug.c.

+ Here is the call graph for this function:

int fr_reset_dumpable ( void  )

Reset dumpable state to previously configured value.

Needed after suid up/down

Returns
  • 0 on success.
  • -1 on failure.

Definition at line 573 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_set_dumpable ( bool  allow_core_dumps)

Enable or disable core dumps.

Parameters
allow_core_dumpswhether to enable or disable core dumps.

Definition at line 528 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

static int fr_set_dumpable_flag ( UNUSED bool  dumpable)
static

Set the dumpable flag, also controls whether processes can PATTACH.

Parameters
dumpablewhether we should allow core dumping

Definition at line 473 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

int fr_set_dumpable_init ( void  )

Get the current maximum for core files.

Do this before anything else so as to ensure it's properly initialized.

Definition at line 513 of file debug.c.

+ Here is the call graph for this function:

void fr_store_debug_state ( void  )

Should be run before using setuid or setgid to get useful results.

Note
sets the fr_debug_state global.

Definition at line 258 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void fr_talloc_fault_setup ( void  )

Register talloc fault handlers.

Just register the fault handlers we need to make talloc produce useful debugging output.

Definition at line 872 of file debug.c.

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Variable Documentation

bool dump_core
static

Whether we should drop a core on fatal signals.

Definition at line 96 of file debug.c.

Whether we're attached to by a debugger.

Definition at line 100 of file debug.c.

int fr_fault_log_fd = STDERR_FILENO
static

Where to write debug output.

Definition at line 98 of file debug.c.

char panic_action[512]
static

The command to execute when panicking.

Definition at line 92 of file debug.c.

fr_fault_cb_t panic_cb = NULL
static

Callback to execute whilst panicking, before the panic_action.

Definition at line 93 of file debug.c.

TALLOC_CTX* talloc_autofree_ctx
static

Definition at line 107 of file debug.c.

TALLOC_CTX* talloc_null_ctx
static

Definition at line 106 of file debug.c.