forked from allinurl/goaccess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
181 lines (156 loc) · 4.52 KB
/
error.c
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
/**
* error.c -- error handling
* ______ ___
* / ____/___ / | _____________ __________
* / / __/ __ \/ /| |/ ___/ ___/ _ \/ ___/ ___/
* / /_/ / /_/ / ___ / /__/ /__/ __(__ |__ )
* \____/\____/_/ |_\___/\___/\___/____/____/
*
* The MIT License (MIT)
* Copyright (c) 2009-2016 Gerardo Orellana <hello @ goaccess.io>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#if HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#if defined(__GLIBC__)
#include <execinfo.h>
#endif
#include <sys/types.h>
#include <unistd.h>
#include "error.h"
#include "parser.h"
static FILE *log_file;
static GLog *log_data;
static FILE *log_invalid;
/* Open a debug file whose name is specified in the given path. */
void
dbg_log_open (const char *path)
{
if (path != NULL) {
log_file = fopen (path, "w");
if (log_file == NULL)
return;
}
}
/* Close the debug file. */
void
dbg_log_close (void)
{
if (log_file != NULL)
fclose (log_file);
}
/* Open the invalid requests log file whose name is specified in the
* given path. */
void
invalid_log_open (const char *path)
{
if (path != NULL) {
log_invalid = fopen (path, "w");
if (log_invalid == NULL)
return;
}
}
/* Close the invalid requests log file. */
void
invalid_log_close (void)
{
if (log_invalid != NULL)
fclose (log_invalid);
}
/* Set current overall parsed log data. */
void
set_signal_data (void *p)
{
log_data = p;
}
#if defined(__GLIBC__)
/* Dump to the standard output the values of the overall parsed log
* data. */
static void
dump_struct (FILE * fp)
{
int pid = getpid ();
if (!log_data)
return;
fprintf (fp, "==%d== VALUES AT CRASH POINT\n", pid);
fprintf (fp, "==%d==\n", pid);
fprintf (fp, "==%d== Line number: %u\n", pid, log_data->processed);
fprintf (fp, "==%d== Offset: %u\n", pid, log_data->offset);
fprintf (fp, "==%d== Invalid data: %u\n", pid, log_data->invalid);
fprintf (fp, "==%d== Piping: %d\n", pid, log_data->piping);
fprintf (fp, "==%d== Response size: %llu bytes\n", pid, log_data->resp_size);
fprintf (fp, "==%d==\n", pid);
}
/* Custom SIGSEGV handler. */
void
sigsegv_handler (int sig)
{
char **messages;
FILE *fp = stderr;
int pid = getpid ();
size_t size, i;
void *trace_stack[TRACE_SIZE];
(void) endwin ();
fprintf (fp, "\n==%d== GoAccess %s crashed by Signal %d\n", pid, GO_VERSION,
sig);
fprintf (fp, "==%d==\n", pid);
dump_struct (fp);
size = backtrace (trace_stack, TRACE_SIZE);
messages = backtrace_symbols (trace_stack, size);
fprintf (fp, "==%d== STACK TRACE:\n", pid);
fprintf (fp, "==%d==\n", pid);
for (i = 0; i < size; i++)
fprintf (fp, "==%d== %zu %s\n", pid, i, messages[i]);
fprintf (fp, "==%d==\n", pid);
fprintf (fp, "==%d== Please report it by opening an issue on GitHub:\n", pid);
fprintf (fp, "==%d== https://github.com/allinurl/goaccess/issues\n\n", pid);
exit (EXIT_FAILURE);
}
#endif
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
/* Write formatted debug log data to the logfile. */
void
dbg_fprintf (const char *fmt, ...)
{
va_list args;
if (!log_file)
return;
va_start (args, fmt);
vfprintf (log_file, fmt, args);
fflush (log_file);
va_end (args);
}
/* Write formatted invalid requests log data to the logfile. */
void
invalid_fprintf (const char *fmt, ...)
{
va_list args;
if (!log_invalid)
return;
va_start (args, fmt);
vfprintf (log_invalid, fmt, args);
fflush (log_invalid);
va_end (args);
}
#pragma GCC diagnostic warning "-Wformat-nonliteral"