blob: a18bde02cf00dc978025bac43c63ff45df566aca [file] [log] [blame]
Neels Hofmeyr17518fe2017-06-20 04:35:06 +02001/*! \file backtrace.c
2 * Routines related to generating call back traces. */
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +01003/*
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +01004 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Weltea9e4a142012-07-14 12:31:55 +02005 * (C) 2012 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +01006 *
7 * All Rights Reserved
8 *
Harald Weltee08da972017-11-13 01:00:26 +09009 * SPDX-License-Identifier: GPL-2.0+
10 *
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010011 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 *
25 */
26
27#include <stdio.h>
28#include <stdlib.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/utils.h>
Harald Welte45ecd042012-07-14 12:30:53 +020030#include <osmocom/core/logging.h>
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010031#include "config.h"
32
33#ifdef HAVE_EXECINFO_H
34#include <execinfo.h>
Harald Welteba6988b2011-08-17 12:46:48 +020035
Harald Welte45ecd042012-07-14 12:30:53 +020036static void _osmo_backtrace(int use_printf, int subsys, int level)
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010037{
38 int i, nptrs;
39 void *buffer[100];
40 char **strings;
41
42 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
Harald Welte45ecd042012-07-14 12:30:53 +020043 if (use_printf)
44 printf("backtrace() returned %d addresses\n", nptrs);
45 else
46 LOGP(subsys, level, "backtrace() returned %d addresses\n",
47 nptrs);
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010048
49 strings = backtrace_symbols(buffer, nptrs);
50 if (!strings)
51 return;
52
Harald Welte45ecd042012-07-14 12:30:53 +020053 for (i = 1; i < nptrs; i++) {
54 if (use_printf)
55 printf("%s\n", strings[i]);
56 else
57 LOGP(subsys, level, "\t%s\n", strings[i]);
58 }
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010059
60 free(strings);
61}
Harald Welte45ecd042012-07-14 12:30:53 +020062
Neels Hofmeyr87e45502017-06-20 00:17:59 +020063/*! Generate and print a call back-trace
Harald Welte45ecd042012-07-14 12:30:53 +020064 *
65 * This function will generate a function call back-trace of the
66 * current process and print it to stdout. */
67void osmo_generate_backtrace(void)
68{
69 _osmo_backtrace(1, 0, 0);
70}
71
Neels Hofmeyr87e45502017-06-20 00:17:59 +020072/*! Generate and log a call back-trace
Harald Welte2d2e2cc2016-04-25 12:11:20 +020073 * \param[in] subsys Logging sub-system
74 * \param[in] level Logging level
Harald Welte45ecd042012-07-14 12:30:53 +020075 *
76 * This function will generate a function call back-trace of the
77 * current process and log it to the specified subsystem and
78 * level using the libosmocore logging subsystem */
79void osmo_log_backtrace(int subsys, int level)
80{
81 _osmo_backtrace(0, subsys, level);
82}
Holger Hans Peter Freyther3ec50472011-11-09 12:18:55 +010083#else
84void osmo_generate_backtrace(void)
85{
Harald Welteaef91752012-09-08 22:00:13 +020086 printf("This platform has no backtrace function\n");
Harald Welte45ecd042012-07-14 12:30:53 +020087}
88void osmo_log_backtrace(int subsys, int level)
89{
90 LOGP(subsys, level, "This platform has no backtrace function\n");
Holger Hans Peter Freyther3ec50472011-11-09 12:18:55 +010091}
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010092#endif