blob: 251fdad8f207b1073f0253c7d2a383d85b6d735e [file] [log] [blame]
Sylvain Munautac3e61a2010-07-25 18:08:54 +02001/* Panic handling */
2/*
3 * (C) 2010 by Sylvain Munaut <tnt@246tNt.com>
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 Weltea523d142011-08-17 16:09:19 +020023/*! \addtogroup utils
24 * @{
25 */
26
Harald Welte2d2e2cc2016-04-25 12:11:20 +020027/*! \file panic.c
Neels Hofmeyr87e45502017-06-20 00:17:59 +020028 * Routines for panic handling
Harald Welte2d2e2cc2016-04-25 12:11:20 +020029 */
Harald Weltea523d142011-08-17 16:09:19 +020030
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010031#include <osmocom/core/panic.h>
32#include <osmocom/core/backtrace.h>
Sylvain Munautac3e61a2010-07-25 18:08:54 +020033
34#include "../config.h"
35
36
37static osmo_panic_handler_t osmo_panic_handler = (void*)0;
38
39
40#ifndef PANIC_INFLOOP
41
42#include <stdio.h>
43#include <stdlib.h>
44
45static void osmo_panic_default(const char *fmt, va_list args)
46{
47 vfprintf(stderr, fmt, args);
Pablo Neira Ayuso619b8b32011-05-07 12:45:47 +020048 osmo_generate_backtrace();
Sylvain Munautac3e61a2010-07-25 18:08:54 +020049 abort();
50}
51
52#else
53
54static void osmo_panic_default(const char *fmt, va_list args)
55{
56 while (1);
57}
58
59#endif
60
61
Neels Hofmeyr87e45502017-06-20 00:17:59 +020062/*! Terminate the current program with a panic
Harald Welte2d2e2cc2016-04-25 12:11:20 +020063 *
64 * You can call this function in case some severely unexpected situation
65 * is detected and the program is supposed to terminate in a way that
66 * reports the fact that it terminates.
67 *
68 * The application can register a panic handler function using \ref
69 * osmo_set_panic_handler. If it doesn't, a default panic handler
70 * function is called automatically.
71 *
72 * The default function on most systems will generate a backtrace and
73 * then abort() the process.
74 */
Sylvain Munautac3e61a2010-07-25 18:08:54 +020075void osmo_panic(const char *fmt, ...)
76{
77 va_list args;
78
79 va_start(args, fmt);
80
81 if (osmo_panic_handler)
82 osmo_panic_handler(fmt, args);
83 else
84 osmo_panic_default(fmt, args);
85
86 va_end(args);
87}
88
89
Neels Hofmeyr87e45502017-06-20 00:17:59 +020090/*! Set the panic handler
Harald Welte2d2e2cc2016-04-25 12:11:20 +020091 * \param[in] h New panic handler function
92 *
93 * This changes the panic handling function from the currently active
94 * function to a new call-back function supplied by the caller.
95 */
Sylvain Munautc91d17b2010-11-13 18:00:25 +010096void osmo_set_panic_handler(osmo_panic_handler_t h)
Sylvain Munautac3e61a2010-07-25 18:08:54 +020097{
98 osmo_panic_handler = h;
99}
100
Harald Weltede6e4982012-12-06 21:25:27 +0100101/*! @} */