Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 1 | /* |
| 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 Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 25 | /*! \file backtrace.c |
| 26 | * \brief Routines realted to generating call back traces |
| 27 | */ |
| 28 | |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <stdlib.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 31 | #include <osmocom/core/utils.h> |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame^] | 32 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 33 | #include "config.h" |
| 34 | |
| 35 | #ifdef HAVE_EXECINFO_H |
| 36 | #include <execinfo.h> |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 37 | |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame^] | 38 | static void _osmo_backtrace(int use_printf, int subsys, int level) |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 39 | { |
| 40 | int i, nptrs; |
| 41 | void *buffer[100]; |
| 42 | char **strings; |
| 43 | |
| 44 | nptrs = backtrace(buffer, ARRAY_SIZE(buffer)); |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame^] | 45 | 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 Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 50 | |
| 51 | strings = backtrace_symbols(buffer, nptrs); |
| 52 | if (!strings) |
| 53 | return; |
| 54 | |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame^] | 55 | 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 Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 61 | |
| 62 | free(strings); |
| 63 | } |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame^] | 64 | |
| 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. */ |
| 69 | void 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 */ |
| 79 | void osmo_log_backtrace(int subsys, int level) |
| 80 | { |
| 81 | _osmo_backtrace(0, subsys, level); |
| 82 | } |
Holger Hans Peter Freyther | 3ec5047 | 2011-11-09 12:18:55 +0100 | [diff] [blame] | 83 | #else |
| 84 | void osmo_generate_backtrace(void) |
| 85 | { |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame^] | 86 | fprintf(stderr, "This platform has no backtrace function\n"); |
| 87 | } |
| 88 | void osmo_log_backtrace(int subsys, int level) |
| 89 | { |
| 90 | LOGP(subsys, level, "This platform has no backtrace function\n"); |
Holger Hans Peter Freyther | 3ec5047 | 2011-11-09 12:18:55 +0100 | [diff] [blame] | 91 | } |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 92 | #endif |