FreeTDM: Move custom backtrace code into ftdm_backtrace_walk() and helper functions.

Portability fix for uClibc and other (linux) environments that lack execinfo.h.

ftdm_backtrace_walk() and related return FTDM_NOTIMPL and print a message
if backtraces are not available in the current environment.

Signed-off-by: Stefan Knoblich <stkn@openisdn.net>
This commit is contained in:
Stefan Knoblich 2012-07-11 17:21:49 +02:00
parent 91236c9ad3
commit 61cfafe6c2
6 changed files with 132 additions and 26 deletions

View File

@ -79,7 +79,8 @@ libfreetdm_la_SOURCES = \
$(SRC)/ftdm_buffer.c \
$(SRC)/ftdm_threadmutex.c \
$(SRC)/ftdm_dso.c \
$(SRC)/ftdm_cpu_monitor.c
$(SRC)/ftdm_cpu_monitor.c \
$(SRC)/ftdm_backtrace.c
library_include_HEADERS = \
$(SRC)/include/freetdm.h \

View File

@ -136,7 +136,7 @@ AC_CHECK_LIB([dl], [dlopen])
AC_CHECK_LIB([pthread], [pthread_create])
AC_CHECK_LIB([m], [cos])
AC_CHECK_HEADERS([netdb.h sys/select.h])
AC_CHECK_HEADERS([netdb.h sys/select.h execinfo.h])
AC_CHECK_FUNC([gethostbyname_r],
[], [AC_CHECK_LIB([nsl], [gethostbyname_r])]

View File

@ -35,13 +35,6 @@
#include "freetdm.h"
//#define CUDATEL_DEBUG
#ifdef CUDATEL_DEBUG
#ifndef _BSD_SOURCE
#define _BSD_SOURCE
#endif
#include <execinfo.h>
#include <syscall.h>
#endif
#ifndef __FUNCTION__
#define __FUNCTION__ __SWITCH_FUNC__
@ -499,6 +492,21 @@ static switch_status_t channel_on_destroy(switch_core_session_t *session)
return SWITCH_STATUS_SUCCESS;
}
#ifdef CUDATEL_DEBUG
struct cudatel_trace_priv {
const char *name;
int span_id;
int chan_id;
};
static void cudatel_trace(const int tid, const void *addr, const char *symbol, void *priv)
{
struct cudatel_trace_priv *data = priv;
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "[%d:%d][tid:%d] %s -> %s\n",
data->span_id, data->chan_id, tid, data->name, symbol);
}
#endif
static switch_status_t channel_on_hangup(switch_core_session_t *session)
{
switch_channel_t *channel = NULL;
@ -558,19 +566,11 @@ static switch_status_t channel_on_hangup(switch_core_session_t *session)
#ifdef CUDATEL_DEBUG
{
pid_t tid = 0;
size_t size = 0;
char **symbols = NULL;
void *stacktrace[50];
int si = 0;
size = backtrace(stacktrace, ftdm_array_len(stacktrace));
symbols = backtrace_symbols(stacktrace, size);
tid = syscall(SYS_gettid);
for (si = 0; si < size; si++) {
switch_log_printf(SWITCH_CHANNEL_LOG, SWITCH_LOG_DEBUG, "[%d:%d][tid:%d] %s -> %s\n",
span_id, chan_id, tid, name, symbols[si]);
}
free(symbols);
struct cudatel_trace_priv trace_data;
trace_data.name = name;
trace_data.span_id = span_id;
trace_data.chan_id = chan_id;
ftdm_backtrace_walk(&cudatel_trace, &trace_data);
}
#endif

View File

@ -0,0 +1,75 @@
/*
*
*
*/
#define _BSD_SOURCE
#include "private/ftdm_core.h"
#ifdef HAVE_EXECINFO_H
#include <stdlib.h>
#include <unistd.h>
#include <execinfo.h>
#include <syscall.h>
#define FTDM_BACKTRACE_MAX 50
FT_DECLARE(ftdm_status_t) ftdm_backtrace_walk(void (* callback)(const int tid, const void *addr, const char *symbol, void *priv), void *priv)
{
void *stacktrace[FTDM_BACKTRACE_MAX];
char **symbols = NULL;
size_t size = 0;
pid_t tid = 0;
int si = 0;
if (!callback) {
return FTDM_EINVAL;
}
tid = syscall(SYS_gettid);
size = backtrace(stacktrace, ftdm_array_len(stacktrace));
symbols = backtrace_symbols(stacktrace, size);
for (si = 0; si < size; si++) {
callback(tid, stacktrace[si], symbols[si], priv);
}
free(symbols);
return FTDM_SUCCESS;
}
#else /* !HAVE_EXECINFO_H */
FT_DECLARE(ftdm_status_t) ftdm_backtrace_walk(void (* callback)(const int tid, const void *addr, const char *symbol, void *priv), void *priv)
{
ftdm_log(FTDM_LOG_DEBUG, "Stack traces are not available on this platform!\n");
return FTDM_NOTIMPL;
}
#endif
static void span_backtrace(const int tid, const void *addr, const char *symbol, void *priv)
{
ftdm_span_t *span = priv;
ftdm_log(FTDM_LOG_DEBUG, "[%d][tid:%d] %p -> %s\n",
ftdm_span_get_id(span), tid, addr, symbol);
}
FT_DECLARE(ftdm_status_t) ftdm_backtrace_span(ftdm_span_t *span)
{
return ftdm_backtrace_walk(&span_backtrace, span);
}
static void chan_backtrace(const int tid, const void *addr, const char *symbol, void *priv)
{
ftdm_channel_t *chan = priv;
ftdm_log(FTDM_LOG_DEBUG, "[%d:%d][tid:%d] %p -> %s\n",
ftdm_channel_get_span_id(chan), ftdm_channel_get_id(chan), tid, addr, symbol);
}
FT_DECLARE(ftdm_status_t) ftdm_backtrace_chan(ftdm_channel_t *chan)
{
return ftdm_backtrace_walk(&chan_backtrace, chan);
}

View File

@ -1814,6 +1814,40 @@ FT_DECLARE(void) ftdm_global_set_config_directory(const char *path);
/*! \brief Check if the FTDM library is initialized and running */
FT_DECLARE(ftdm_bool_t) ftdm_running(void);
/**
* Generate a stack trace and invoke a callback function for each entry
* \param[in] callback Callback function, that is invoked for each stack symbol
* \param[in] priv (User-)Private data passed to the callback
* \retval
* FTDM_SUCCESS On success
* FTDM_NOTIMPL Backtraces are not available
* FTDM_EINVAL Invalid arguments (callback was NULL)
*/
FT_DECLARE(ftdm_status_t) ftdm_backtrace_walk(void (* callback)(const int tid, const void *addr, const char *symbol, void *priv), void *priv);
/**
* Convenience function to print a backtrace for a span.
* \note The backtrace is generated with FTDM_LOG_DEBUG log level.
* \param[in] span Span object
* \retval
* FTDM_SUCCESS On success
* FTDM_NOTIMPL Backtraces are not available
* FTDM_EINVAL Invalid arguments (e.g. span was NULL)
*/
FT_DECLARE(ftdm_status_t) ftdm_backtrace_span(ftdm_span_t *span);
/**
* Convenience function to print a backtrace for a channel.
* \note The backtrace is generated with FTDM_LOG_DEBUG log level.
* \param[in] chan Channel object
* \retval
* FTDM_SUCCESS On success
* FTDM_NOTIMPL Backtraces are not available
* FTDM_EINVAL Invalid arguments (e.g. chan was NULL)
*/
FT_DECLARE(ftdm_status_t) ftdm_backtrace_chan(ftdm_channel_t *chan);
FT_DECLARE_DATA extern ftdm_logger_t ftdm_log;
/*! \brief Basic transcoding function prototype */

View File

@ -104,10 +104,6 @@
#include <sys/time.h>
#endif
#ifdef __linux__
#include <execinfo.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>