Piotr Krysik | 93154d1 | 2018-02-27 12:16:25 +0100 | [diff] [blame^] | 1 | /*! \file panic.c |
| 2 | * Routines for panic handling. */ |
| 3 | /* |
| 4 | * (C) 2010 by Sylvain Munaut <tnt@246tNt.com> |
| 5 | * |
| 6 | * All Rights Reserved |
| 7 | * |
| 8 | * SPDX-License-Identifier: GPL-2.0+ |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation; either version 2 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License along |
| 21 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 22 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | /*! \addtogroup utils |
| 27 | * @{ |
| 28 | * \file panic.c */ |
| 29 | |
| 30 | #include <osmocom/core/panic.h> |
| 31 | //#include <osmocom/core/backtrace.h> |
| 32 | |
| 33 | //#include "../config.h" |
| 34 | |
| 35 | |
| 36 | static osmo_panic_handler_t osmo_panic_handler = (void*)0; |
| 37 | |
| 38 | |
| 39 | #ifndef PANIC_INFLOOP |
| 40 | |
| 41 | #include <stdio.h> |
| 42 | #include <stdlib.h> |
| 43 | |
| 44 | static void osmo_panic_default(const char *fmt, va_list args) |
| 45 | { |
| 46 | vfprintf(stderr, fmt, args); |
| 47 | //osmo_generate_backtrace(); |
| 48 | abort(); |
| 49 | } |
| 50 | |
| 51 | #else |
| 52 | |
| 53 | static void osmo_panic_default(const char *fmt, va_list args) |
| 54 | { |
| 55 | while (1); |
| 56 | } |
| 57 | |
| 58 | #endif |
| 59 | |
| 60 | |
| 61 | /*! Terminate the current program with a panic |
| 62 | * |
| 63 | * You can call this function in case some severely unexpected situation |
| 64 | * is detected and the program is supposed to terminate in a way that |
| 65 | * reports the fact that it terminates. |
| 66 | * |
| 67 | * The application can register a panic handler function using \ref |
| 68 | * osmo_set_panic_handler. If it doesn't, a default panic handler |
| 69 | * function is called automatically. |
| 70 | * |
| 71 | * The default function on most systems will generate a backtrace and |
| 72 | * then abort() the process. |
| 73 | */ |
| 74 | void osmo_panic(const char *fmt, ...) |
| 75 | { |
| 76 | va_list args; |
| 77 | |
| 78 | va_start(args, fmt); |
| 79 | |
| 80 | if (osmo_panic_handler) |
| 81 | osmo_panic_handler(fmt, args); |
| 82 | else |
| 83 | osmo_panic_default(fmt, args); |
| 84 | |
| 85 | va_end(args); |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /*! Set the panic handler |
| 90 | * \param[in] h New panic handler function |
| 91 | * |
| 92 | * This changes the panic handling function from the currently active |
| 93 | * function to a new call-back function supplied by the caller. |
| 94 | */ |
| 95 | void osmo_set_panic_handler(osmo_panic_handler_t h) |
| 96 | { |
| 97 | osmo_panic_handler = h; |
| 98 | } |
| 99 | |
| 100 | /*! @} */ |