2017-06-20 02:35:06 +00:00
|
|
|
/*! \file backtrace.c
|
|
|
|
* Routines related to generating call back traces. */
|
2011-03-23 17:08:08 +00:00
|
|
|
/*
|
|
|
|
* (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
|
2012-07-14 10:31:55 +00:00
|
|
|
* (C) 2012 by Harald Welte <laforge@gnumonks.org>
|
2011-03-23 17:08:08 +00:00
|
|
|
*
|
|
|
|
* All Rights Reserved
|
|
|
|
*
|
2017-11-12 16:00:26 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0+
|
|
|
|
*
|
2011-03-23 17:08:08 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2011-03-22 15:36:13 +00:00
|
|
|
#include <osmocom/core/utils.h>
|
2012-07-14 10:30:53 +00:00
|
|
|
#include <osmocom/core/logging.h>
|
2011-03-23 17:08:08 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
|
|
|
#ifdef HAVE_EXECINFO_H
|
|
|
|
#include <execinfo.h>
|
2011-08-17 10:46:48 +00:00
|
|
|
|
2012-07-14 10:30:53 +00:00
|
|
|
static void _osmo_backtrace(int use_printf, int subsys, int level)
|
2011-03-23 17:08:08 +00:00
|
|
|
{
|
|
|
|
int i, nptrs;
|
|
|
|
void *buffer[100];
|
|
|
|
char **strings;
|
|
|
|
|
|
|
|
nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
|
2012-07-14 10:30:53 +00:00
|
|
|
if (use_printf)
|
|
|
|
printf("backtrace() returned %d addresses\n", nptrs);
|
|
|
|
else
|
|
|
|
LOGP(subsys, level, "backtrace() returned %d addresses\n",
|
|
|
|
nptrs);
|
2011-03-23 17:08:08 +00:00
|
|
|
|
|
|
|
strings = backtrace_symbols(buffer, nptrs);
|
|
|
|
if (!strings)
|
|
|
|
return;
|
|
|
|
|
2012-07-14 10:30:53 +00:00
|
|
|
for (i = 1; i < nptrs; i++) {
|
|
|
|
if (use_printf)
|
|
|
|
printf("%s\n", strings[i]);
|
|
|
|
else
|
|
|
|
LOGP(subsys, level, "\t%s\n", strings[i]);
|
|
|
|
}
|
2011-03-23 17:08:08 +00:00
|
|
|
|
|
|
|
free(strings);
|
|
|
|
}
|
2012-07-14 10:30:53 +00:00
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Generate and print a call back-trace
|
2012-07-14 10:30:53 +00:00
|
|
|
*
|
|
|
|
* This function will generate a function call back-trace of the
|
|
|
|
* current process and print it to stdout. */
|
|
|
|
void osmo_generate_backtrace(void)
|
|
|
|
{
|
|
|
|
_osmo_backtrace(1, 0, 0);
|
|
|
|
}
|
|
|
|
|
2017-06-19 22:17:59 +00:00
|
|
|
/*! Generate and log a call back-trace
|
2016-04-25 10:11:20 +00:00
|
|
|
* \param[in] subsys Logging sub-system
|
|
|
|
* \param[in] level Logging level
|
2012-07-14 10:30:53 +00:00
|
|
|
*
|
|
|
|
* This function will generate a function call back-trace of the
|
|
|
|
* current process and log it to the specified subsystem and
|
|
|
|
* level using the libosmocore logging subsystem */
|
|
|
|
void osmo_log_backtrace(int subsys, int level)
|
|
|
|
{
|
|
|
|
_osmo_backtrace(0, subsys, level);
|
|
|
|
}
|
2011-11-09 11:18:55 +00:00
|
|
|
#else
|
|
|
|
void osmo_generate_backtrace(void)
|
|
|
|
{
|
2012-09-08 20:00:13 +00:00
|
|
|
printf("This platform has no backtrace function\n");
|
2012-07-14 10:30:53 +00:00
|
|
|
}
|
|
|
|
void osmo_log_backtrace(int subsys, int level)
|
|
|
|
{
|
|
|
|
LOGP(subsys, level, "This platform has no backtrace function\n");
|
2011-11-09 11:18:55 +00:00
|
|
|
}
|
2011-03-23 17:08:08 +00:00
|
|
|
#endif
|