summaryrefslogtreecommitdiff
path: root/tizen/src/util/error_handler.c
blob: a6348ac54b45aed7424e607f814e10bb9a8b2530 (plain)
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
 * Error handler
 *
 * Copyright (C) 2015 Samsung Electronics Co., Ltd. All rights reserved.
 *
 * Contact:
 * Jinhyung Jo <jinhyung.jo@samsung.com>
 * SeokYeon Hwang <syeon.hwang@samsung.com>
 * GiWoong Kim <giwoong.kim@samsung.com>
 *
 * This program 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 * Contributors:
 * - S-Core Co., Ltd
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#include "qemu/osdep.h"
#include "qemu-common.h"

#ifdef CONFIG_WIN32
#include <windows.h>
#include <dbghelp.h>
#else
#include <execinfo.h>
#endif

#include "qemu/error-report.h"

#include "error_handler.h"
#include "emulator_common.h"
#include "emulator.h"

#ifdef CONFIG_QT
#include "qt5_error_report.h"
#endif

#include "new_debug_ch.h"

DECLARE_DEBUG_CHANNEL(backtrace);

bool print_backtrace_at_normal_exit_enabled;

void enable_print_backtrace_at_normal_exit(void)
{
    print_backtrace_at_normal_exit_enabled = true;
}

#ifdef CONFIG_WIN32
static LPTOP_LEVEL_EXCEPTION_FILTER prevExceptionFilter;

static void dump_backtrace(void *ptr)
{
    STACKFRAME64 frame;
    DWORD image;
    CONTEXT context;
    HANDLE hProcess = GetCurrentProcess();
    HANDLE hThread = GetCurrentThread();

    if (!ptr) {
        ZeroMemory(&context, sizeof(CONTEXT));
        context.ContextFlags = CONTEXT_FULL;
        RtlCaptureContext(&context);
    } else {
        CopyMemory(&context, ptr, sizeof(CONTEXT));
    }

    SymInitialize(hProcess, NULL, TRUE);

    ZeroMemory(&frame, sizeof(STACKFRAME64));
#ifdef _WIN64
    image = IMAGE_FILE_MACHINE_AMD64;
    frame.AddrPC.Offset = context.Rip;
    frame.AddrPC.Mode = AddrModeFlat;
    frame.AddrFrame.Offset = context.Rbp;
    frame.AddrFrame.Mode = AddrModeFlat;
    frame.AddrStack.Offset = context.Rsp;
    frame.AddrStack.Mode = AddrModeFlat;
#else
    image = IMAGE_FILE_MACHINE_I386;
    frame.AddrPC.Offset = context.Eip;
    frame.AddrPC.Mode = AddrModeFlat;
    frame.AddrFrame.Offset = context.Ebp;
    frame.AddrFrame.Mode = AddrModeFlat;
    frame.AddrStack.Offset = context.Esp;
    frame.AddrStack.Mode = AddrModeFlat;
#endif

    int i = 0;
    BOOL result = FALSE;
    while ((result = StackWalk64(image, hProcess, hThread,
                                  &frame, &context, NULL,
                                  SymFunctionTableAccess64,
                                  SymGetModuleBase64, NULL)) == TRUE) {
        TCHAR buffer[sizeof(SYMBOL_INFO) + (MAX_SYM_NAME - 1) * sizeof(TCHAR)];
        PSYMBOL_INFO pSymbol = (PSYMBOL_INFO)buffer;
        pSymbol->SizeOfStruct = sizeof(SYMBOL_INFO);
        pSymbol->MaxNameLen = MAX_SYM_NAME;
        DWORD64 displacement = 0;
        TCHAR pFileName[MAX_PATH] = {0, };
        DWORD64 dwBase = SymGetModuleBase64(hProcess, frame.AddrPC.Offset);
        if (dwBase) {
            if (!GetModuleFileNameA((HMODULE)(intptr_t)dwBase, pFileName,
                        MAX_PATH)) {
                snprintf(pFileName, MAX_PATH, "Unknown Module");
            }
        }
        /* TODO: take the symbols for the static functions
                 without import .pdb */
        if (!SymFromAddr(hProcess,
                    frame.AddrPC.Offset,
                    &displacement, pSymbol)) {
            g_strlcpy(pSymbol->Name, "????????", MAX_SYM_NAME);
        }
#ifdef _WIN64
        LOG_INFO("#%04d 0x%016" PRIx64 " in %s from %s\n",
#else
        LOG_INFO("#%04d 0x%08" PRIx64 " in %s from %s\n",
#endif
                i, frame.AddrPC.Offset, pSymbol->Name, pFileName);
        i++;
    }

    SymCleanup(hProcess);
}

static WINAPI LONG
maru_unhandled_exception_filter(LPEXCEPTION_POINTERS pException)
{
#ifdef _WIN64
    LOG_SEVERE("Exception occurred: Code[0x%x], Address[0x%016I64x]\n",
#else
    LOG_SEVERE("Exception occurred: Code[0x%x], Address[0x%08x]\n",
#endif
               pException->ExceptionRecord->ExceptionCode,
               pException->ExceptionRecord->ExceptionAddress);

    dump_backtrace(pException->ContextRecord);

    return EXCEPTION_CONTINUE_SEARCH;
}

static void register_exception_handler(void)
{
    prevExceptionFilter =
        SetUnhandledExceptionFilter(maru_unhandled_exception_filter);
}

#else /* END FOR WINDOWS, START FOR LINUX & DARWIN */

/* prevent an interrupt by another signal */
QemuMutex siglock;
/* uses in case SIGSEGV */
struct sigaction old_sa;

/* Print 'backtrace' */
static void dump_backtrace(void *ptr)
{
    int i;
    void *trace[1024];
    int ndepth = backtrace(trace, 1024);
    char **syms = backtrace_symbols(trace, ndepth);

    LOG_INFO("Backtrace depth is %d\n", ndepth);
    for (i = 0; i < ndepth; i++) {
        LOG_INFO("#%04d %s\n", i, syms[i]);
    }
    free(syms);
}

static void maru_sighandler(int sig)
{
    LOG_SEVERE("Got signal: %d(%s)\n", sig, strsignal(sig));

    qemu_mutex_lock(&siglock);
    dump_backtrace(NULL);
    qemu_mutex_unlock(&siglock);
    qemu_mutex_destroy(&siglock);
    if (sig == SIGSEGV) {
        sigaction(SIGSEGV, &old_sa, NULL);
        raise(SIGSEGV);
    }
}

static void register_exception_handler(void)
{
    void *trace[1];
    struct sigaction sa;

    /* make dummy call to explicitly load glibc library */
    backtrace(trace, 1);

    qemu_mutex_init(&siglock);
    sa.sa_handler = maru_sighandler;
    sigemptyset(&sa.sa_mask);
    sa.sa_flags = SA_RESTART;
    /* store previous signal action */
    sigaction(SIGSEGV, &sa, &old_sa);
    sigaction(SIGBUS, &sa, NULL);
    sigaction(SIGILL, &sa, NULL);
    sigaction(SIGFPE, &sa, NULL);
    sigaction(SIGABRT, &sa, NULL);
    /* main thread only */
    sigaction(SIGTERM, &sa, NULL);
    sigaction(SIGINT, &sa, NULL);
}
#endif  /* END FOR LINUX & DARWIN */

static void handle_error_at_exit(void)
{
    if (print_backtrace_at_normal_exit_enabled) {
        LOG_INFO("Stack backtrace for tracing...\n");
        LOG_INFO("This is not an error\n");
        dump_backtrace(NULL);
    }
}

#define MAX_MESSAGE_LEN 2048
static size_t message_len;
static char message[MAX_MESSAGE_LEN];

static void report(const char *fmt, va_list ap)
{
    char *new_message = g_strdup_vprintf(fmt, ap);
    message_len = g_strlcat(message, new_message, MAX_MESSAGE_LEN);
    g_free(new_message);

    /* We are waiting for '\n' */
    if (message[message_len - 1] == '\n') {
#ifdef CONFIG_QT
        start_qt5_msgbox(CRITICAL_ICON, message);
#endif

        message[0] = '\0';
        message_len = 0;
    }
}

static ErrorReporter error_reporter = {
    .report = report
};

void init_error_handler(void)
{
    register_exception_handler();
    add_error_reporter(&error_reporter);

    atexit(handle_error_at_exit);
}