summaryrefslogtreecommitdiff
path: root/src/common/nullpo.c
blob: 11741745c09ab0fbfd15eae5b054a0abf089bc4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/**
 * This file is part of Hercules.
 * http://herc.ws - http://github.com/HerculesWS/Hercules
 *
 * Copyright (C) 2012-2020 Hercules Dev Team
 * Copyright (C) Athena Dev Teams
 *
 * Hercules is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#define HERCULES_CORE

#include "nullpo.h"

#include "common/showmsg.h"
#include "common/strlib.h"

#include <stdio.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#if defined(HAVE_LIBBACKTRACE)
#include "libbacktrace/backtrace.h"
#include "libbacktrace/backtrace-supported.h"
#  if defined(WIN32)
#    include <windows.h>
#  elif defined(__sun)
#    include <limits.h>
#  elif defined(__linux) || defined(__linux__)
#    include <unistd.h>
#    include <limits.h>
#  elif defined(__APPLE__) && defined(__MACH__)
#    include <mach-o/dyld.h>
#  elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
#    include <sys/types.h>
#    include <sys/sysctl.h>
#  endif
#elif defined(HAVE_EXECINFO)
#include <execinfo.h>
#endif // HAVE_LIBBACKTRACE


static struct nullpo_interface nullpo_s;
struct nullpo_interface *nullpo;

#ifdef HAVE_LIBBACKTRACE
static char executable_path[PATH_MAX];

static void nullpo_error_callback(void *data, const char *msg, int errnum)
{
	ShowError("Error: %s (%d)", msg, errnum);
}

static int nullpo_print_callback(void *data, uintptr_t pc, const char *filename, int lineno, const char *function)
{
	ShowError("0x%lx %s\n",
		(unsigned long) pc,
		function == NULL ? "???" : function);
	ShowError("\t%s:%d\n",
		filename == NULL ? "???" : filename,
		lineno);
	return 0;
}

static void nullpo_backtrace_print(struct backtrace_state *state)
{
	backtrace_full(state, 0, nullpo_print_callback, nullpo_error_callback, state);
}

static bool nullpo_backtrace_get_executable_path(char *buf, size_t length)
{
#if defined(WIN32)
	char *exe_path = NULL;
	if (_get_pgmptr(&exe_path) != 0)
		return false;
	safestrncpy(buf, exe_path, length);
	return true;
#elif defined(__sun)
	if (length < MAX_PATH)
		return false;
	if (realpath(getexecname(), buf) == NULL)
		return false;
	buf[length - 1] = '\0';
	return true;
#elif defined(__linux) || defined(__linux__)
	ssize_t len = readlink("/proc/self/exe", buf, length);
	if (len <= 0 || len == length)
		return false;
	buf[len] = '\0';
	return true;
#elif defined(__APPLE__) && defined(__MACH__)
	uint32_t len = (uint32_t)length;
	if (_NSGetExecutablePath(buf, &len) != 0)
		return false; // buffer too small (!)
	// resolve symlinks, ., .. if possible
	char *canonical_path = realpath(buf, NULL);
	if (canonical_path != NULL) {
		safestrncpy(buf, canonical_path, length);
		free(canonical_path);
	}
	return true;
#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__bsdi__) || defined(__DragonFly__)
	int mib[4];
	mib[0] = CTL_KERN;
	mib[1] = KERN_PROC;
	mib[2] = KERN_PROC_PATHNAME;
	mib[3] = -1;
	if (sysctl(mib, 4, buf, &length, NULL, 0) != 0)
		return false;
	return true;
#endif
	return false;
}
#endif  // HAVE_LIBBACKTRACE

/**
 * Reports failed assertions or NULL pointers
 *
 * @param file       Source file where the error was detected
 * @param line       Line
 * @param func       Function
 * @param targetname Name of the checked symbol
 * @param title      Message title to display (i.e. failed assertion or nullpo info)
 */
static void assert_report(const char *file, int line, const char *func, const char *targetname, const char *title)
{
	if (file == NULL)
		file = "??";

	if (func == NULL || *func == '\0')
		func = "unknown";

	ShowError("--- %s --------------------------------------------\n", title);
	ShowError("%s:%d: '%s' in function `%s'\n", file, line, targetname, func);
#ifdef HAVE_LIBBACKTRACE
	if (nullpo->backtrace_state != NULL)
		nullpo_backtrace_print(nullpo->backtrace_state);
#elif defined(HAVE_EXECINFO)
	void *array[10];
	int size = (int)backtrace(array, 10);
	char **strings = backtrace_symbols(array, size);
	for (int i = 0; i < size; i++)
		ShowError("%s\n", strings[i]);
	free(strings);
#endif // HAVE_LIBBACKTRACE
	ShowError("--- end %s ----------------------------------------\n", title);
}

static void nullpo_init(void)
{
#ifdef HAVE_LIBBACKTRACE
	if (!nullpo_backtrace_get_executable_path(executable_path, sizeof executable_path)) {
		safestrncpy(executable_path, "hercules", sizeof executable_path);
	}
	nullpo->backtrace_state = backtrace_create_state(executable_path, BACKTRACE_SUPPORTS_THREADS, nullpo_error_callback, NULL);
#endif
}

static void nullpo_final(void)
{
	// FIXME: libbacktrace doesn't provide a backtrace_free_state, and it's unsafe to pass the state to
	// backtrace_free (the function itself uses the state internally). For the time being, we'll leave the state
	// allocated until program termination as shown in their examples.
}

/**
 *
 **/
void nullpo_defaults(void)
{
	nullpo = &nullpo_s;
	nullpo->init = nullpo_init;
	nullpo->final = nullpo_final;
	nullpo->assert_report = assert_report;

	nullpo->backtrace_state = NULL;
}