js/src/jscrashreport.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #include "jscrashreport.h"
michael@0 8
michael@0 9 #include <time.h>
michael@0 10
michael@0 11 #include "jsapi.h"
michael@0 12 #include "jscrashformat.h"
michael@0 13 #include "jsutil.h"
michael@0 14
michael@0 15 using namespace js;
michael@0 16 using namespace js::crash;
michael@0 17
michael@0 18 #if defined(XP_WIN)
michael@0 19
michael@0 20 static const int stack_snapshot_max_size = 32768;
michael@0 21
michael@0 22 #include <windows.h>
michael@0 23
michael@0 24 static bool
michael@0 25 GetStack(uint64_t *stack, uint64_t *stack_len, CrashRegisters *regs, char *buffer, size_t size)
michael@0 26 {
michael@0 27 /* Try to figure out how big the stack is. */
michael@0 28 char dummy;
michael@0 29 MEMORY_BASIC_INFORMATION info;
michael@0 30 if (VirtualQuery(reinterpret_cast<LPCVOID>(&dummy), &info, sizeof(info)) == 0)
michael@0 31 return false;
michael@0 32 if (info.State != MEM_COMMIT)
michael@0 33 return false;
michael@0 34
michael@0 35 /* 256 is a fudge factor to account for the rest of GetStack's frame. */
michael@0 36 uint64_t p = uint64_t(&dummy) - 256;
michael@0 37 uint64_t len = stack_snapshot_max_size;
michael@0 38
michael@0 39 if (p + len > uint64_t(info.BaseAddress) + info.RegionSize)
michael@0 40 len = uint64_t(info.BaseAddress) + info.RegionSize - p;
michael@0 41
michael@0 42 if (len > size)
michael@0 43 len = size;
michael@0 44
michael@0 45 *stack = p;
michael@0 46 *stack_len = len;
michael@0 47
michael@0 48 /* Get the register state. */
michael@0 49 #if defined(_MSC_VER) && defined(_M_IX86)
michael@0 50 /* ASM version for win2k that doesn't support RtlCaptureContext */
michael@0 51 uint32_t vip, vsp, vbp;
michael@0 52 __asm {
michael@0 53 MyLabel:
michael@0 54 mov [vbp], ebp;
michael@0 55 mov [vsp], esp;
michael@0 56 mov eax, [MyLabel];
michael@0 57 mov [vip], eax;
michael@0 58 }
michael@0 59 regs->ip = vip;
michael@0 60 regs->sp = vsp;
michael@0 61 regs->bp = vbp;
michael@0 62 #else
michael@0 63 CONTEXT context;
michael@0 64 RtlCaptureContext(&context);
michael@0 65 #if defined(_M_IX86)
michael@0 66 regs->ip = context.Eip;
michael@0 67 regs->sp = context.Esp;
michael@0 68 regs->bp = context.Ebp;
michael@0 69 #elif defined(_M_X64)
michael@0 70 regs->ip = context.Rip;
michael@0 71 regs->sp = context.Rsp;
michael@0 72 regs->bp = context.Rbp;
michael@0 73 #else
michael@0 74 #error unknown cpu architecture
michael@0 75 #endif
michael@0 76 #endif
michael@0 77
michael@0 78 js_memcpy(buffer, (void *)p, len);
michael@0 79
michael@0 80 return true;
michael@0 81 }
michael@0 82
michael@0 83 #elif 0
michael@0 84
michael@0 85 #include <sys/mman.h>
michael@0 86 #include <ucontext.h>
michael@0 87 #include <unistd.h>
michael@0 88
michael@0 89 static bool
michael@0 90 GetStack(uint64_t *stack, uint64_t *stack_len, CrashRegisters *regs, char *buffer, size_t size)
michael@0 91 {
michael@0 92 /* 256 is a fudge factor to account for the rest of GetStack's frame. */
michael@0 93 char dummy;
michael@0 94 uint64_t p = uint64_t(&dummy) - 256;
michael@0 95 uint64_t pgsz = getpagesize();
michael@0 96 uint64_t len = stack_snapshot_max_size;
michael@0 97 p &= ~(pgsz - 1);
michael@0 98
michael@0 99 /* Try to figure out how big the stack is. */
michael@0 100 while (len > 0) {
michael@0 101 if (mlock((const void *)p, len) == 0) {
michael@0 102 munlock((const void *)p, len);
michael@0 103 break;
michael@0 104 }
michael@0 105 len -= pgsz;
michael@0 106 }
michael@0 107
michael@0 108 if (len > size)
michael@0 109 len = size;
michael@0 110
michael@0 111 *stack = p;
michael@0 112 *stack_len = len;
michael@0 113
michael@0 114 /* Get the register state. */
michael@0 115 ucontext_t context;
michael@0 116 if (getcontext(&context) != 0)
michael@0 117 return false;
michael@0 118
michael@0 119 #if defined(__x86_64__)
michael@0 120 regs->sp = (uint64_t)context.uc_mcontext.gregs[REG_RSP];
michael@0 121 regs->bp = (uint64_t)context.uc_mcontext.gregs[REG_RBP];
michael@0 122 regs->ip = (uint64_t)context.uc_mcontext.gregs[REG_RIP];
michael@0 123 #elif defined(__i386__)
michael@0 124 regs->sp = (uint64_t)context.uc_mcontext.gregs[REG_ESP];
michael@0 125 regs->bp = (uint64_t)context.uc_mcontext.gregs[REG_EBP];
michael@0 126 regs->ip = (uint64_t)context.uc_mcontext.gregs[REG_EIP];
michael@0 127 #else
michael@0 128 #error unknown cpu architecture
michael@0 129 #endif
michael@0 130
michael@0 131 js_memcpy(buffer, (void *)p, len);
michael@0 132
michael@0 133 return true;
michael@0 134 }
michael@0 135
michael@0 136 #else
michael@0 137
michael@0 138 static bool
michael@0 139 GetStack(uint64_t *stack, uint64_t *stack_len, CrashRegisters *regs, char *buffer, size_t size)
michael@0 140 {
michael@0 141 return false;
michael@0 142 }
michael@0 143
michael@0 144 #endif
michael@0 145
michael@0 146 namespace js {
michael@0 147 namespace crash {
michael@0 148
michael@0 149 class Stack : private CrashStack
michael@0 150 {
michael@0 151 public:
michael@0 152 Stack(uint64_t id);
michael@0 153
michael@0 154 bool snapshot();
michael@0 155 };
michael@0 156
michael@0 157 Stack::Stack(uint64_t id)
michael@0 158 : CrashStack(id)
michael@0 159 {
michael@0 160 }
michael@0 161
michael@0 162 bool
michael@0 163 Stack::snapshot()
michael@0 164 {
michael@0 165 snaptime = time(nullptr);
michael@0 166 return GetStack(&stack_base, &stack_len, &regs, stack, sizeof(stack));
michael@0 167 }
michael@0 168
michael@0 169 class Ring : private CrashRing
michael@0 170 {
michael@0 171 public:
michael@0 172 Ring(uint64_t id);
michael@0 173
michael@0 174 void push(uint64_t tag, void *data, size_t size);
michael@0 175
michael@0 176 private:
michael@0 177 size_t bufferSize() { return crash_buffer_size; }
michael@0 178 void copyBytes(void *data, size_t size);
michael@0 179 };
michael@0 180
michael@0 181 Ring::Ring(uint64_t id)
michael@0 182 : CrashRing(id)
michael@0 183 {
michael@0 184 }
michael@0 185
michael@0 186 void
michael@0 187 Ring::push(uint64_t tag, void *data, size_t size)
michael@0 188 {
michael@0 189 uint64_t t = time(nullptr);
michael@0 190
michael@0 191 copyBytes(&tag, sizeof(uint64_t));
michael@0 192 copyBytes(&t, sizeof(uint64_t));
michael@0 193 copyBytes(data, size);
michael@0 194 uint64_t mysize = size;
michael@0 195 copyBytes(&mysize, sizeof(uint64_t));
michael@0 196 }
michael@0 197
michael@0 198 void
michael@0 199 Ring::copyBytes(void *data, size_t size)
michael@0 200 {
michael@0 201 if (size >= bufferSize())
michael@0 202 size = bufferSize();
michael@0 203
michael@0 204 if (offset + size > bufferSize()) {
michael@0 205 size_t first = bufferSize() - offset;
michael@0 206 size_t second = size - first;
michael@0 207 js_memcpy(&buffer[offset], data, first);
michael@0 208 js_memcpy(buffer, (char *)data + first, second);
michael@0 209 offset = second;
michael@0 210 } else {
michael@0 211 js_memcpy(&buffer[offset], data, size);
michael@0 212 offset += size;
michael@0 213 }
michael@0 214 }
michael@0 215
michael@0 216 } /* namespace crash */
michael@0 217 } /* namespace js */
michael@0 218
michael@0 219 #ifdef JS_CRASH_DIAGNOSTICS
michael@0 220 static bool gInitialized;
michael@0 221
michael@0 222 static Stack gGCStack(JS_CRASH_STACK_GC);
michael@0 223 static Stack gErrorStack(JS_CRASH_STACK_ERROR);
michael@0 224 static Ring gRingBuffer(JS_CRASH_RING);
michael@0 225 #endif
michael@0 226
michael@0 227 void
michael@0 228 js::crash::SnapshotGCStack()
michael@0 229 {
michael@0 230 #ifdef JS_CRASH_DIAGNOSTICS
michael@0 231 if (gInitialized)
michael@0 232 gGCStack.snapshot();
michael@0 233 #endif
michael@0 234 }
michael@0 235
michael@0 236 void
michael@0 237 js::crash::SnapshotErrorStack()
michael@0 238 {
michael@0 239 #ifdef JS_CRASH_DIAGNOSTICS
michael@0 240 if (gInitialized)
michael@0 241 gErrorStack.snapshot();
michael@0 242 #endif
michael@0 243 }
michael@0 244
michael@0 245 void
michael@0 246 js::crash::SaveCrashData(uint64_t tag, void *ptr, size_t size)
michael@0 247 {
michael@0 248 #ifdef JS_CRASH_DIAGNOSTICS
michael@0 249 if (gInitialized)
michael@0 250 gRingBuffer.push(tag, ptr, size);
michael@0 251 #endif
michael@0 252 }
michael@0 253
michael@0 254 JS_PUBLIC_API(void)
michael@0 255 JS_EnumerateDiagnosticMemoryRegions(JSEnumerateDiagnosticMemoryCallback callback)
michael@0 256 {
michael@0 257 #ifdef JS_CRASH_DIAGNOSTICS
michael@0 258 if (!gInitialized) {
michael@0 259 gInitialized = true;
michael@0 260 (*callback)(&gGCStack, sizeof(gGCStack));
michael@0 261 (*callback)(&gErrorStack, sizeof(gErrorStack));
michael@0 262 (*callback)(&gRingBuffer, sizeof(gRingBuffer));
michael@0 263 }
michael@0 264 #endif
michael@0 265 }
michael@0 266

mercurial