blob: f0c3489ac19eca260eef80f2ba4577347e0d6328 [file] [log] [blame]
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +01001/*
2 * (C) 2008 by Daniel Willmann <daniel@totalueberwachung.de>
3 * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org>
4 * (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
5 * (C) 2010 by Nico Golde <nico@ngolde.de>
6 *
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
Harald Welteba6988b2011-08-17 12:46:48 +020025/*! \file backtrace.c
26 * \brief Routines realted to generating call back traces
27 */
28
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010029#include <stdio.h>
30#include <stdlib.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/utils.h>
Harald Welte45ecd042012-07-14 12:30:53 +020032#include <osmocom/core/logging.h>
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010033#include "config.h"
34
35#ifdef HAVE_EXECINFO_H
36#include <execinfo.h>
Harald Welteba6988b2011-08-17 12:46:48 +020037
Harald Welte45ecd042012-07-14 12:30:53 +020038static void _osmo_backtrace(int use_printf, int subsys, int level)
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010039{
40 int i, nptrs;
41 void *buffer[100];
42 char **strings;
43
44 nptrs = backtrace(buffer, ARRAY_SIZE(buffer));
Harald Welte45ecd042012-07-14 12:30:53 +020045 if (use_printf)
46 printf("backtrace() returned %d addresses\n", nptrs);
47 else
48 LOGP(subsys, level, "backtrace() returned %d addresses\n",
49 nptrs);
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010050
51 strings = backtrace_symbols(buffer, nptrs);
52 if (!strings)
53 return;
54
Harald Welte45ecd042012-07-14 12:30:53 +020055 for (i = 1; i < nptrs; i++) {
56 if (use_printf)
57 printf("%s\n", strings[i]);
58 else
59 LOGP(subsys, level, "\t%s\n", strings[i]);
60 }
Pablo Neira Ayusofba495e2011-03-23 18:08:08 +010061
62 free(strings);
63}
Harald Welte45ecd042012-07-14 12:30:53 +020064
65/*! \brief Generate and print a call back-trace
66 *
67 * This function will generate a function call back-trace of the
68 * current process and print it to stdout. */
69void osmo_generate_backtrace(void)
70{
71 _osmo_backtrace(1, 0, 0);
72}
73
74/*! \brief Generate and log a call back-trace
75 *
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 Welte45ecd042012-07-14 12:30:53 +020086 fprintf(stderr, "This platform has no backtrace function\n");
87}
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