Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 1 | /* |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 2 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | a9e4a14 | 2012-07-14 12:31:55 +0200 | [diff] [blame] | 3 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 4 | * |
| 5 | * All Rights Reserved |
| 6 | * |
| 7 | * This program is free software; you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation; either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 19 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | */ |
| 22 | |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 23 | /*! \file backtrace.c |
| 24 | * \brief Routines realted to generating call back traces |
| 25 | */ |
| 26 | |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <stdlib.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 29 | #include <osmocom/core/utils.h> |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 30 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 31 | #include "config.h" |
| 32 | |
| 33 | #ifdef HAVE_EXECINFO_H |
| 34 | #include <execinfo.h> |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 35 | |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 36 | static void _osmo_backtrace(int use_printf, int subsys, int level) |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 37 | { |
| 38 | int i, nptrs; |
| 39 | void *buffer[100]; |
| 40 | char **strings; |
| 41 | |
| 42 | nptrs = backtrace(buffer, ARRAY_SIZE(buffer)); |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 43 | 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 Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 48 | |
| 49 | strings = backtrace_symbols(buffer, nptrs); |
| 50 | if (!strings) |
| 51 | return; |
| 52 | |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 53 | 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 Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 59 | |
| 60 | free(strings); |
| 61 | } |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 62 | |
| 63 | /*! \brief Generate and print a call back-trace |
| 64 | * |
| 65 | * This function will generate a function call back-trace of the |
| 66 | * current process and print it to stdout. */ |
| 67 | void osmo_generate_backtrace(void) |
| 68 | { |
| 69 | _osmo_backtrace(1, 0, 0); |
| 70 | } |
| 71 | |
| 72 | /*! \brief Generate and log a call back-trace |
| 73 | * |
| 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 */ |
| 77 | void osmo_log_backtrace(int subsys, int level) |
| 78 | { |
| 79 | _osmo_backtrace(0, subsys, level); |
| 80 | } |
Holger Hans Peter Freyther | 3ec5047 | 2011-11-09 12:18:55 +0100 | [diff] [blame] | 81 | #else |
| 82 | void osmo_generate_backtrace(void) |
| 83 | { |
Harald Welte | aef9175 | 2012-09-08 22:00:13 +0200 | [diff] [blame] | 84 | printf("This platform has no backtrace function\n"); |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 85 | } |
| 86 | void osmo_log_backtrace(int subsys, int level) |
| 87 | { |
| 88 | LOGP(subsys, level, "This platform has no backtrace function\n"); |
Holger Hans Peter Freyther | 3ec5047 | 2011-11-09 12:18:55 +0100 | [diff] [blame] | 89 | } |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 90 | #endif |