1/*
2 * Copyright (c) 2004,2005 Voltaire Inc.  All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses.  You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 *     Redistribution and use in source and binary forms, with or
11 *     without modification, are permitted provided that the following
12 *     conditions are met:
13 *
14 *      - Redistributions of source code must retain the above
15 *        copyright notice, this list of conditions and the following
16 *        disclaimer.
17 *
18 *      - Redistributions in binary form must reproduce the above
19 *        copyright notice, this list of conditions and the following
20 *        disclaimer in the documentation and/or other materials
21 *        provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 *
32 */
33
34#define _GNU_SOURCE
35
36#if HAVE_CONFIG_H
37#  include <config.h>
38#endif /* HAVE_CONFIG_H */
39
40#include <inttypes.h>
41#include <string.h>
42#include <errno.h>
43#include <stdio.h>
44#include <stdlib.h>
45#include <unistd.h>
46#include <stdarg.h>
47#include <sys/types.h>
48#include <sys/stat.h>
49#include <fcntl.h>
50#include <sys/ioctl.h>
51#include <unistd.h>
52#include <string.h>
53#include <endian.h>
54#include <byteswap.h>
55#include <sys/poll.h>
56#include <syslog.h>
57#include <time.h>
58#include <signal.h>
59
60#include "common.h"
61
62static int loop_on_panic;
63
64void
65stack_dump(void)
66{
67	if (!__builtin_frame_address(1))
68		return
69	syslog(LOG_ALERT, "#1 %p\n", __builtin_return_address(1));
70
71	if (!__builtin_frame_address(2))
72		return
73	syslog(LOG_ALERT, "#2 %p\n", __builtin_return_address(2));
74
75	if (!__builtin_frame_address(3))
76		return
77	syslog(LOG_ALERT, "#3 %p\n", __builtin_return_address(3));
78
79	if (!__builtin_frame_address(4))
80		return
81	syslog(LOG_ALERT, "#4 %p\n", __builtin_return_address(4));
82
83	if (!__builtin_frame_address(5))
84		return
85	syslog(LOG_ALERT, "#5 %p\n", __builtin_return_address(5));
86
87	if (!__builtin_frame_address(6))
88		return
89	syslog(LOG_ALERT, "#6 %p\n", __builtin_return_address(6));
90
91	if (!__builtin_frame_address(7))
92		return
93	syslog(LOG_ALERT, "#7 %p\n", __builtin_return_address(7));
94
95	if (!__builtin_frame_address(8))
96		return
97	syslog(LOG_ALERT, "#8 %p\n", __builtin_return_address(8));
98
99	if (!__builtin_frame_address(9))
100		return
101	syslog(LOG_ALERT, "#9 %p\n", __builtin_return_address(9));
102
103	if (!__builtin_frame_address(10))
104		return
105	syslog(LOG_ALERT, "#10 %p\n", __builtin_return_address(10));
106
107	if (!__builtin_frame_address(11))
108		return
109	syslog(LOG_ALERT, "#11 %p\n", __builtin_return_address(11));
110
111	if (!__builtin_frame_address(12))
112		return
113	syslog(LOG_ALERT, "#12 %p\n", __builtin_return_address(12));
114
115	if (!__builtin_frame_address(13))
116		return
117	syslog(LOG_ALERT, "#13 %p\n", __builtin_return_address(13));
118
119	if (!__builtin_frame_address(14))
120		return
121	syslog(LOG_ALERT, "#14 %p\n", __builtin_return_address(14));
122
123	if (!__builtin_frame_address(15))
124		return
125	syslog(LOG_ALERT, "#15 %p\n", __builtin_return_address(15));
126
127	if (!__builtin_frame_address(16))
128		return
129	syslog(LOG_ALERT, "#16 %p\n", __builtin_return_address(16));
130
131	if (!__builtin_frame_address(17))
132		return
133	syslog(LOG_ALERT, "#17 %p\n", __builtin_return_address(17));
134
135	if (!__builtin_frame_address(18))
136		return
137	syslog(LOG_ALERT, "#18 %p\n", __builtin_return_address(18));
138}
139
140static void
141handler(int x)
142{
143	static int in;
144	time_t  tm;
145
146	if (!in) {
147		in++;
148
149		syslog(LOG_ALERT, "*** exception handler: died with signal %d", x);
150		stack_dump();
151
152		fflush(NULL);
153
154		tm = time(0);
155		fprintf(stderr, "%s *** exception handler: died with signal %d pid %d\n",
156				ctime(&tm), x, getpid());
157
158		fflush(NULL);
159	}
160
161	if (loop_on_panic) {
162		fprintf(stderr, "exception handler: entering tight loop ... pid %d\n",getpid());
163		for (; ; )
164			;
165	}
166
167	signal(x, SIG_DFL);
168}
169
170void
171enable_stack_dump(int loop)
172{
173	loop_on_panic = loop;
174	signal(SIGILL, handler);
175	signal(SIGBUS, handler);
176	signal(SIGSEGV, handler);
177	signal(SIGABRT, handler);
178}
179