blob: 5ce74825579e0a40397c020148410307fcd1d3d8 [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 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 *
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/utils.h>
Harald Welte45ecd042012-07-14 12:30:53 +020028#include <osmocom/core/logging.h>
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010029#include "config.h"
30
31#ifdef HAVE_EXECINFO_H
32#include <execinfo.h>
Harald Welteba6988b2011-08-17 12:46:48 +020033
Harald Welte45ecd042012-07-14 12:30:53 +020034static void _osmo_backtrace(int use_printf, int subsys, int level)
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010035{
36 int i, nptrs;
37 void *buffer[100];
38 char **strings;
39
40 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
Harald Welte45ecd042012-07-14 12:30:53 +020041 if (use_printf)
42 printf("backtrace() returned %d addresses\n", nptrs);
43 else
44 LOGP(subsys, level, "backtrace() returned %d addresses\n",
45 nptrs);
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010046
47 strings = backtrace_symbols(buffer, nptrs);
48 if (!strings)
49 return;
50
Harald Welte45ecd042012-07-14 12:30:53 +020051 for (i = 1; i < nptrs; i++) {
52 if (use_printf)
53 printf("%s\n", strings[i]);
54 else
55 LOGP(subsys, level, "\t%s\n", strings[i]);
56 }
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010057
58 free(strings);
59}
Harald Welte45ecd042012-07-14 12:30:53 +020060
Neels Hofmeyr87e45502017-06-20 00:17:59 +020061/*! Generate and print a call back-trace
Harald Welte45ecd042012-07-14 12:30:53 +020062 *
63 * This function will generate a function call back-trace of the
64 * current process and print it to stdout. */
65void osmo_generate_backtrace(void)
66{
67 _osmo_backtrace(1, 0, 0);
68}
69
Neels Hofmeyr87e45502017-06-20 00:17:59 +020070/*! Generate and log a call back-trace
Harald Welte2d2e2cc2016-04-25 12:11:20 +020071 * \param[in] subsys Logging sub-system
72 * \param[in] level Logging level
Harald Welte45ecd042012-07-14 12:30:53 +020073 *
74 * This function will generate a function call back-trace of the
75 * current process and log it to the specified subsystem and
76 * level using the libosmocore logging subsystem */
77void osmo_log_backtrace(int subsys, int level)
78{
79 _osmo_backtrace(0, subsys, level);
80}
Holger Hans Peter Freyther3ec50472011-11-09 12:18:55 +010081#else
82void osmo_generate_backtrace(void)
83{
Harald Welteaef91752012-09-08 22:00:13 +020084 printf("This platform has no backtrace function\n");
Harald Welte45ecd042012-07-14 12:30:53 +020085}
86void osmo_log_backtrace(int subsys, int level)
87{
88 LOGP(subsys, level, "This platform has no backtrace function\n");
Holger Hans Peter Freyther3ec50472011-11-09 12:18:55 +010089}
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010090#endif