mirror of
https://github.com/signalwire/freeswitch.git
synced 2025-08-13 01:26:58 +00:00
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:
75
libs/freetdm/src/ftdm_backtrace.c
Normal file
75
libs/freetdm/src/ftdm_backtrace.c
Normal 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);
|
||||
}
|
@@ -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 */
|
||||
|
@@ -104,10 +104,6 @@
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#include <execinfo.h>
|
||||
#endif
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
Reference in New Issue
Block a user