Neels Hofmeyr | 17518fe | 2017-06-20 04:35:06 +0200 | [diff] [blame] | 1 | /*! \file backtrace.c |
| 2 | * Routines related to generating call back traces. */ |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 3 | /* |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 4 | * (C) 2009 by Holger Hans Peter Freyther <zecke@selfish.org> |
Harald Welte | a9e4a14 | 2012-07-14 12:31:55 +0200 | [diff] [blame] | 5 | * (C) 2012 by Harald Welte <laforge@gnumonks.org> |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 6 | * |
| 7 | * All Rights Reserved |
| 8 | * |
Harald Welte | e08da97 | 2017-11-13 01:00:26 +0900 | [diff] [blame] | 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | * |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 11 | * 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 | * |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
Pablo Neira Ayuso | 8341934 | 2011-03-22 16:36:13 +0100 | [diff] [blame] | 25 | #include <osmocom/core/utils.h> |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 26 | #include <osmocom/core/logging.h> |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 27 | #include "config.h" |
| 28 | |
| 29 | #ifdef HAVE_EXECINFO_H |
| 30 | #include <execinfo.h> |
Harald Welte | ba6988b | 2011-08-17 12:46:48 +0200 | [diff] [blame] | 31 | |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 32 | static void _osmo_backtrace(int use_printf, int subsys, int level) |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 33 | { |
| 34 | int i, nptrs; |
| 35 | void *buffer[100]; |
| 36 | char **strings; |
| 37 | |
| 38 | nptrs = backtrace(buffer, ARRAY_SIZE(buffer)); |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 39 | if (use_printf) |
| 40 | printf("backtrace() returned %d addresses\n", nptrs); |
| 41 | else |
| 42 | LOGP(subsys, level, "backtrace() returned %d addresses\n", |
| 43 | nptrs); |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 44 | |
| 45 | strings = backtrace_symbols(buffer, nptrs); |
| 46 | if (!strings) |
| 47 | return; |
| 48 | |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 49 | for (i = 1; i < nptrs; i++) { |
| 50 | if (use_printf) |
| 51 | printf("%s\n", strings[i]); |
| 52 | else |
| 53 | LOGP(subsys, level, "\t%s\n", strings[i]); |
| 54 | } |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 55 | |
| 56 | free(strings); |
| 57 | } |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 58 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 59 | /*! Generate and print a call back-trace |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 60 | * |
| 61 | * This function will generate a function call back-trace of the |
| 62 | * current process and print it to stdout. */ |
| 63 | void osmo_generate_backtrace(void) |
| 64 | { |
| 65 | _osmo_backtrace(1, 0, 0); |
| 66 | } |
| 67 | |
Neels Hofmeyr | 87e4550 | 2017-06-20 00:17:59 +0200 | [diff] [blame] | 68 | /*! Generate and log a call back-trace |
Harald Welte | 2d2e2cc | 2016-04-25 12:11:20 +0200 | [diff] [blame] | 69 | * \param[in] subsys Logging sub-system |
| 70 | * \param[in] level Logging level |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 71 | * |
| 72 | * This function will generate a function call back-trace of the |
| 73 | * current process and log it to the specified subsystem and |
| 74 | * level using the libosmocore logging subsystem */ |
| 75 | void osmo_log_backtrace(int subsys, int level) |
| 76 | { |
| 77 | _osmo_backtrace(0, subsys, level); |
| 78 | } |
Holger Hans Peter Freyther | 3ec5047 | 2011-11-09 12:18:55 +0100 | [diff] [blame] | 79 | #else |
| 80 | void osmo_generate_backtrace(void) |
| 81 | { |
Harald Welte | aef9175 | 2012-09-08 22:00:13 +0200 | [diff] [blame] | 82 | printf("This platform has no backtrace function\n"); |
Harald Welte | 45ecd04 | 2012-07-14 12:30:53 +0200 | [diff] [blame] | 83 | } |
| 84 | void osmo_log_backtrace(int subsys, int level) |
| 85 | { |
| 86 | LOGP(subsys, level, "This platform has no backtrace function\n"); |
Holger Hans Peter Freyther | 3ec5047 | 2011-11-09 12:18:55 +0100 | [diff] [blame] | 87 | } |
Pablo Neira Ayuso | fba495e | 2011-03-23 18:08:08 +0100 | [diff] [blame] | 88 | #endif |