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