Skip to content

Commit

Permalink
Move exit functions to debug.c
Browse files Browse the repository at this point in the history
  • Loading branch information
arr2036 committed Jun 24, 2014
1 parent 5fe27ca commit c4250d1
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 98 deletions.
23 changes: 12 additions & 11 deletions src/include/libradius.h
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,6 @@ DICT_VENDOR *dict_vendorbyvalue(int vendor);
#endif

/* md5.c */

void fr_md5_calc(uint8_t *, uint8_t const *, unsigned int);

/* radius.c */
Expand Down Expand Up @@ -621,14 +620,6 @@ int readvp2(VALUE_PAIR **out, TALLOC_CTX *ctx, FILE *fp, bool *pfiledone);
void fr_strerror_printf(char const *, ...) CC_HINT(format (printf, 1, 2));
void fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2));

extern bool fr_assert_cond(char const *file, int line, char const *expr, bool cond);
#define fr_assert(_x) fr_assert_cond(__FILE__, __LINE__, #_x, (_x))

extern void NEVER_RETURNS _fr_exit(char const *file, int line, int status);
#define fr_exit(_x) _fr_exit(__FILE__, __LINE__, (_x))

extern void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
#define fr_exit_now(_x) _fr_exit_now(__FILE__, __LINE__, (_x))

extern char const *fr_strerror(void);
extern char const *fr_syserror(int num);
Expand Down Expand Up @@ -725,6 +716,7 @@ void fr_rand_seed(void const *, size_t ); /* seed the random pool */
int fr_crypt_check(char const *key, char const *salt);

/* cbuff.c */

typedef struct fr_cbuff fr_cbuff_t;

fr_cbuff_t *fr_cbuff_alloc(TALLOC_CTX *ctx, uint32_t size, bool lock);
Expand Down Expand Up @@ -762,10 +754,19 @@ void fr_fault_set_cb(fr_fault_cb_t func);
void fr_fault_set_log_fn(fr_fault_log_t func);
void fr_fault_set_log_fd(int fd);

#ifdef WITH_VERIFY_PTR
# ifdef WITH_VERIFY_PTR
void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp);
void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR *vps);
#endif
# endif

bool fr_assert_cond(char const *file, int line, char const *expr, bool cond);
# define fr_assert(_x) fr_assert_cond(__FILE__, __LINE__, #_x, (_x))

void NEVER_RETURNS _fr_exit(char const *file, int line, int status);
# define fr_exit(_x) _fr_exit(__FILE__, __LINE__, (_x))

void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status);
# define fr_exit_now(_x) _fr_exit_now(__FILE__, __LINE__, (_x))

/* rbtree.c */
typedef struct rbtree_t rbtree_t;
Expand Down
121 changes: 95 additions & 26 deletions src/lib/debug.c
Original file line number Diff line number Diff line change
Expand Up @@ -510,7 +510,7 @@ void fr_fault(int sig)

/*
* Here we temporarily enable the dumpable flag so if GBD or LLDB
* is called in the panic_action, they can pattach tot he running
* is called in the panic_action, they can pattach to the running
* process.
*/
if (fr_get_dumpable_flag() == 0) {
Expand Down Expand Up @@ -620,7 +620,7 @@ int fr_log_talloc_report(TALLOC_CTX *ctx)
*/
static void _fr_fault_mem_report(int sig)
{
fr_fault_log("CAUGHT SIGNAL: %s\n", strsignal(sig));
FR_FAULT_LOG("CAUGHT SIGNAL: %s", strsignal(sig));

if (fr_log_talloc_report(NULL) < 0) fr_perror("memreport");
}
Expand Down Expand Up @@ -809,7 +809,7 @@ void fr_fault_set_log_fd(int fd)
inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp)
{
if (!vp) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line);
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR pointer was NULL", file, line);
fr_assert(0);
fr_exit_now(0);
}
Expand All @@ -824,25 +824,25 @@ inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp)
TALLOC_CTX *parent;

if (!talloc_get_type(vp->data.ptr, uint8_t)) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
"uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
"uint8_t but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
(void) talloc_get_type_abort(vp->data.ptr, uint8_t);
}

len = talloc_array_length(vp->vp_octets);
if (vp->length > len) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
"uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->length, len);
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
"uint8_t data buffer length %zu\n", file, line, vp->da->name, vp->length, len);
fr_assert(0);
fr_exit_now(1);
}

parent = talloc_parent(vp->data.ptr);
if (parent != vp) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
"parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
file, line, vp->da->name,
vp, parent, parent ? talloc_get_name(parent) : "NULL");
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer is not "
"parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
file, line, vp->da->name,
vp, parent, parent ? talloc_get_name(parent) : "NULL");
fr_assert(0);
fr_exit_now(1);
}
Expand All @@ -855,32 +855,32 @@ inline void fr_verify_vp(char const *file, int line, VALUE_PAIR const *vp)
TALLOC_CTX *parent;

if (!talloc_get_type(vp->data.ptr, char)) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
"char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" data buffer type should be "
"char but is %s\n", file, line, vp->da->name, talloc_get_name(vp->data.ptr));
(void) talloc_get_type_abort(vp->data.ptr, char);
}

len = (talloc_array_length(vp->vp_strvalue) - 1);
if (vp->length > len) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
"char buffer length %zu\n", file, line, vp->da->name, vp->length, len);
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" length %zu is greater than "
"char buffer length %zu\n", file, line, vp->da->name, vp->length, len);
fr_assert(0);
fr_exit_now(1);
}

if (vp->vp_strvalue[vp->length] != '\0') {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 "
"terminated\n", file, line, vp->da->name);
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" char buffer not \\0 "
"terminated\n", file, line, vp->da->name);
fr_assert(0);
fr_exit_now(1);
}

parent = talloc_parent(vp->data.ptr);
if (parent != vp) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" uint8_t buffer is not "
"parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
file, line, vp->da->name,
vp, parent, parent ? talloc_get_name(parent) : "NULL");
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: VALUE_PAIR \"%s\" uint8_t buffer is not "
"parented by VALUE_PAIR %p, instead parented by %p (%s)\n",
file, line, vp->da->name,
vp, parent, parent ? talloc_get_name(parent) : "NULL");
fr_assert(0);
fr_exit_now(1);
}
Expand Down Expand Up @@ -908,11 +908,11 @@ void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR

parent = talloc_parent(vp);
if (expected && (parent != expected)) {
fprintf(stderr, "CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented "
"by %p (%s), instead parented by %p (%s)\n",
file, line, vp->da->name,
expected, talloc_get_name(expected),
parent, parent ? talloc_get_name(parent) : "NULL");
FR_FAULT_LOG("CONSISTENCY CHECK FAILED %s[%u]: Expected VALUE_PAIR \"%s\" to be parented "
"by %p (%s), instead parented by %p (%s)\n",
file, line, vp->da->name,
expected, talloc_get_name(expected),
parent, parent ? talloc_get_name(parent) : "NULL");

fr_log_talloc_report(expected);
if (parent) fr_log_talloc_report(parent);
Expand All @@ -923,3 +923,72 @@ void fr_verify_list(char const *file, int line, TALLOC_CTX *expected, VALUE_PAIR
}
}
#endif

bool fr_assert_cond(char const *file, int line, char const *expr, bool cond)
{
if (!cond) {
FR_FAULT_LOG("SOFT ASSERT FAILED %s[%u]: %s", file, line, expr);
#if !defined(NDEBUG) && defined(SIGUSR1)
fr_fault(SIGUSR1);
#endif
return false;
}

return cond;
}

/** Exit possibly printing a message about why we're exiting.
*
* Use the fr_exit(status) macro instead of calling this function
* directly.
*
* @param file where fr_exit() was called.
* @param line where fr_exit() was called.
* @param status we're exiting with.
*/
void NEVER_RETURNS _fr_exit(char const *file, int line, int status)
{
#ifndef NDEBUG
char const *error = fr_strerror();

if (error && (status != 0)) {
FR_FAULT_LOG("EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error);
} else {
FR_FAULT_LOG("EXIT(%i) CALLED %s[%u]", status, file, line);
}
#endif
fprintf(stderr, "If running under a debugger it should break <<here>>");
fflush(stderr);

fr_debug_break(); /* If running under GDB we'll break here */

exit(status);
}

/** Exit possibly printing a message about why we're exiting.
*
* Use the fr_exit_now(status) macro instead of calling this function
* directly.
*
* @param file where fr_exit_now() was called.
* @param line where fr_exit_now() was called.
* @param status we're exiting with.
*/
void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status)
{
#ifndef NDEBUG
char const *error = fr_strerror();

if (error && (status != 0)) {
FR_FAULT_LOG("_EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error);
} else {
FR_FAULT_LOG("_EXIT(%i) CALLED %s[%u]", status, file, line);
}
#endif
fprintf(stderr, "If running under a debugger it should break <<here>>\n");
fflush(stderr);

fr_debug_break(); /* If running under GDB we'll break here */

_exit(status);
}
11 changes: 1 addition & 10 deletions src/lib/heap.c
Original file line number Diff line number Diff line change
Expand Up @@ -263,15 +263,6 @@ static int heap_cmp(void const *one, void const *two)

#define ARRAY_SIZE (1024)

void NEVER_RETURNS _fr_exit(char const *file, int line, int status)
{
#ifndef NDEBUG
fprintf(stderr, "EXIT CALLED %s[%u]: %i: ", file, line, status);
#endif
fflush(stderr);
exit(status);
}

int main(int argc, char **argv)
{
fr_heap_t *hp;
Expand All @@ -296,7 +287,7 @@ int main(int argc, char **argv)
fprintf(stderr, "Failed inserting %d\n", i);
fr_exit(1);
}

if (!fr_heap_check(hp, &array[i])) {
fprintf(stderr, "Inserted but not in heap %d\n", i);
fr_exit(1);
Expand Down
51 changes: 0 additions & 51 deletions src/lib/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -198,54 +198,3 @@ void fr_perror(char const *fmt, ...)

va_end(ap);
}

bool fr_assert_cond(char const *file, int line, char const *expr, bool cond)
{
if (!cond) {
fr_perror("SOFT ASSERT FAILED %s[%u]: %s", file, line, expr);
#if !defined(NDEBUG) && defined(SIGUSR1)
fr_fault(SIGUSR1);
#endif
return false;
}

return cond;
}

void NEVER_RETURNS _fr_exit(char const *file, int line, int status)
{
#ifndef NDEBUG
char const *error = fr_strerror();

if (error && (status != 0)) {
fr_perror("EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error);
} else {
fr_perror("EXIT(%i) CALLED %s[%u]", status, file, line);
}
#endif
fr_perror("If running under a debugger it should break <<here>>");
fflush(stderr);

fr_debug_break(); /* If running under GDB we'll break here */

exit(status);
}

void NEVER_RETURNS _fr_exit_now(char const *file, int line, int status)
{
#ifndef NDEBUG
char const *error = fr_strerror();

if (error && (status != 0)) {
fr_perror("_EXIT(%i) CALLED %s[%u]. Last error was: %s", status, file, line, error);
} else {
fr_perror("_EXIT(%i) CALLED %s[%u]", status, file, line);
}
#endif
fr_perror("If running under a debugger it should break <<here>>");
fflush(stderr);

fr_debug_break(); /* If running under GDB we'll break here */

_exit(status);
}

0 comments on commit c4250d1

Please sign in to comment.