Bug 662646 - Add stack snapshotting API for crash reports (r=dmandelin)

This commit is contained in:
Bill McCloskey 2011-07-07 17:31:18 -07:00
parent 33aaa1793d
commit f31cc0f13b
6 changed files with 472 additions and 0 deletions

View File

@ -150,6 +150,7 @@ CPPSRCS = \
jsgcmark.cpp \
jsgcchunk.cpp \
jsgcstats.cpp \
jscrashreport.cpp \
jshash.cpp \
jsinterp.cpp \
jsinvoke.cpp \
@ -206,6 +207,7 @@ INSTALLED_HEADERS = \
jsclone.h \
jscntxt.h \
jscompat.h \
jscrashreport.h \
jsdate.h \
jsdbgapi.h \
jsdhash.h \

View File

@ -1183,6 +1183,16 @@ extern JS_PUBLIC_API(JSBool)
JS_SetCTypesCallbacks(JSContext *cx, JSObject *ctypesObj, JSCTypesCallbacks *callbacks);
#endif
typedef JSBool
(* JSEnumerateDiagnosticMemoryCallback)(void *ptr, size_t length);
/*
* Enumerate memory regions that contain diagnostic information
* intended to be included in crash report minidumps.
*/
extern JS_PUBLIC_API(void)
JS_EnumerateDiagnosticMemoryRegions(JSEnumerateDiagnosticMemoryCallback callback);
/*
* Macros to hide interpreter stack layout details from a JSFastNative using
* its jsval *vp parameter. The stack layout underlying invocation can't change

106
js/src/jscrashformat.h Normal file
View File

@ -0,0 +1,106 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=4 sw=4 et tw=99:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
* May 28, 2008.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef jscrashformat_h___
#define jscrashformat_h___
#include <string.h>
namespace js {
namespace crash {
const static int crash_cookie_len = 16;
const static char crash_cookie[crash_cookie_len] = "*J*S*CRASHDATA*";
/* These values are used for CrashHeader::id. */
enum {
JS_CRASH_STACK_GC = 0x400,
JS_CRASH_STACK_ERROR = 0x401,
JS_CRASH_RING = 0x800
};
/*
* All the data here will be stored directly in the minidump, so we use
* platform-independent types. We also ensure that the size of every field is a
* multiple of 8 bytes, to guarantee that they won't be padded.
*/
struct CrashHeader
{
char cookie[crash_cookie_len];
/* id of the crash data, chosen from the enum above. */
uint64 id;
CrashHeader(uint64 id) : id(id) { memcpy(cookie, crash_cookie, crash_cookie_len); }
};
struct CrashRegisters
{
uint64 ip, sp, bp;
};
const static int crash_buffer_size = 32 * 1024;
struct CrashStack
{
CrashStack(uint64 id) : header(id) {}
CrashHeader header;
uint64 snaptime; /* Unix time when the stack was snapshotted. */
CrashRegisters regs; /* Register contents for the snapshot. */
uint64 stack_base; /* Base address of stack at the time of snapshot. */
uint64 stack_len; /* Extent of the stack. */
char stack[crash_buffer_size]; /* Contents of the stack. */
};
struct CrashRing
{
CrashRing(uint64 id) : header(id), offset(0) { memset(buffer, 0, sizeof(buffer)); }
CrashHeader header;
uint64 offset; /* Next byte to be written in the buffer. */
char buffer[crash_buffer_size];
};
} /* namespace crash */
} /* namespace js */
#endif

280
js/src/jscrashreport.cpp Normal file
View File

@ -0,0 +1,280 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=4 sw=4 et tw=99:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
* May 28, 2008.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#include "jsapi.h"
#include "jscntxt.h"
#include "jscrashreport.h"
#include "jscrashformat.h"
#include <time.h>
namespace js {
namespace crash {
const static int stack_snapshot_max_size = 32768;
#if defined(XP_WIN)
#include <Windows.h>
static bool
GetStack(uint64 *stack, uint64 *stack_len, CrashRegisters *regs, char *buffer, size_t size)
{
/* Try to figure out how big the stack is. */
char dummy;
MEMORY_BASIC_INFORMATION info;
if (VirtualQuery(reinterpret_cast<LPCVOID>(&dummy), &info, sizeof(info)) == 0)
return false;
if (info.State != MEM_COMMIT)
return false;
/* 256 is a fudge factor to account for the rest of GetStack's frame. */
uint64 p = uint64(&dummy) - 256;
uint64 len = stack_snapshot_max_size;
if (p + len > uint64(info.BaseAddress) + info.RegionSize)
len = uint64(info.BaseAddress) + info.RegionSize - p;
if (len > size)
len = size;
*stack = p;
*stack_len = len;
/* Get the register state. */
#if JS_BITS_PER_WORD == 32
uint32 vip, vsp, vbp;
__asm {
Label:
mov [vbp], ebp;
mov [vsp], esp;
mov eax, [Label];
mov [vip], eax;
}
regs->ip = vip;
regs->sp = vsp;
regs->bp = vbp;
#else
CONTEXT context;
RtlCaptureContext(&context);
regs->ip = context.Rip;
regs->sp = context.Rsp;
regs->bp = context.Rbp;
#endif
memcpy(buffer, (void *)p, len);
return true;
}
#elif defined(__linux__) && (defined(__x86_64__) || defined(__i386__))
#include <unistd.h>
#include <ucontext.h>
#include <sys/mman.h>
static bool
GetStack(uint64 *stack, uint64 *stack_len, CrashRegisters *regs, char *buffer, size_t size)
{
/* 256 is a fudge factor to account for the rest of GetStack's frame. */
char dummy;
uint64 p = uint64(&dummy) - 256;
uint64 pgsz = getpagesize();
uint64 len = stack_snapshot_max_size;
p &= ~(pgsz - 1);
/* Try to figure out how big the stack is. */
while (len > 0) {
if (mlock((const void *)p, len) == 0) {
munlock((const void *)p, len);
break;
}
len -= pgsz;
}
if (len > size)
len = size;
*stack = p;
*stack_len = len;
/* Get the register state. */
ucontext_t context;
if (getcontext(&context) != 0)
return false;
#if JS_BITS_PER_WORD == 64
regs->sp = (uint64)context.uc_mcontext.gregs[REG_RSP];
regs->bp = (uint64)context.uc_mcontext.gregs[REG_RBP];
regs->ip = (uint64)context.uc_mcontext.gregs[REG_RIP];
#elif JS_BITS_PER_WORD == 32
regs->sp = (uint64)context.uc_mcontext.gregs[REG_ESP];
regs->bp = (uint64)context.uc_mcontext.gregs[REG_EBP];
regs->ip = (uint64)context.uc_mcontext.gregs[REG_EIP];
#endif
memcpy(buffer, (void *)p, len);
return true;
}
#else
static bool
GetStack(uint64 *stack, uint64 *stack_len, CrashRegisters *regs, char *buffer, size_t size)
{
return false;
}
#endif
class Stack : private CrashStack
{
public:
Stack(uint64 id);
bool snapshot();
};
Stack::Stack(uint64 id)
: CrashStack(id)
{
}
bool
Stack::snapshot()
{
snaptime = time(NULL);
return GetStack(&stack_base, &stack_len, &regs, stack, sizeof(stack));
}
class Ring : private CrashRing
{
public:
Ring(uint64 id);
void push(uint64 tag, void *data, size_t size);
private:
size_t bufferSize() { return crash_buffer_size; }
void copyBytes(void *data, size_t size);
};
Ring::Ring(uint64 id)
: CrashRing(id)
{
}
void
Ring::push(uint64 tag, void *data, size_t size)
{
uint64 t = time(NULL);
copyBytes(&tag, sizeof(uint64));
copyBytes(&t, sizeof(uint64));
copyBytes(data, size);
uint64 mysize = size;
copyBytes(&mysize, sizeof(uint64));
}
void
Ring::copyBytes(void *data, size_t size)
{
if (size >= bufferSize())
size = bufferSize();
if (offset + size > bufferSize()) {
size_t first = bufferSize() - offset;
size_t second = size - first;
memcpy(&buffer[offset], data, first);
memcpy(buffer, (char *)data + first, second);
offset = second;
} else {
memcpy(&buffer[offset], data, size);
offset += size;
}
}
static bool gInitialized;
static Stack gGCStack(JS_CRASH_STACK_GC);
static Stack gErrorStack(JS_CRASH_STACK_ERROR);
static Ring gRingBuffer(JS_CRASH_RING);
} /* namespace crash */
} /* namespace js */
using namespace js;
using namespace js::crash;
JS_FRIEND_API(void)
js_SnapshotGCStack()
{
if (gInitialized)
gGCStack.snapshot();
}
JS_FRIEND_API(void)
js_SnapshotErrorStack()
{
if (gInitialized)
gErrorStack.snapshot();
}
JS_FRIEND_API(void)
js_SaveCrashData(uint64 tag, void *ptr, size_t size)
{
if (gInitialized)
gRingBuffer.push(tag, ptr, size);
}
JS_PUBLIC_API(void)
JS_EnumerateDiagnosticMemoryRegions(JSEnumerateDiagnosticMemoryCallback callback)
{
#if 0
if (!gInitialized) {
gInitialized = true;
(*callback)(&gGCStack, sizeof(gGCStack));
(*callback)(&gErrorStack, sizeof(gErrorStack));
(*callback)(&gRingBuffer, sizeof(gRingBuffer));
}
#endif
}

59
js/src/jscrashreport.h Normal file
View File

@ -0,0 +1,59 @@
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=4 sw=4 et tw=99:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
* May 28, 2008.
*
* The Initial Developer of the Original Code is
* Mozilla Foundation
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
#ifndef jscrashreport_h___
#define jscrashreport_h___
#include "jstypes.h"
JS_BEGIN_EXTERN_C
JS_FRIEND_API(void)
js_SnapshotGCStack();
JS_FRIEND_API(void)
js_SnapshotErrorStack();
JS_FRIEND_API(void)
js_SaveCrashData(uint64 tag, void *ptr, size_t size);
JS_END_EXTERN_C
#endif /* jscrashreport_h___ */

View File

@ -49,6 +49,10 @@
#include "mozilla/FunctionTimer.h"
#include "prsystem.h"
#ifdef MOZ_CRASHREPORTER
#include "nsExceptionHandler.h"
#endif
using namespace mozilla;
/***************************************************************************/
@ -1667,6 +1671,14 @@ NS_IMPL_THREADSAFE_ISUPPORTS1(
, nsIMemoryMultiReporter
)
#ifdef MOZ_CRASHREPORTER
static JSBool
DiagnosticMemoryCallback(void *ptr, size_t size)
{
return CrashReporter::RegisterAppMemory(ptr, size) == NS_OK;
}
#endif
XPCJSRuntime::XPCJSRuntime(nsXPConnect* aXPConnect)
: mXPConnect(aXPConnect),
mJSRuntime(nsnull),
@ -1724,6 +1736,9 @@ XPCJSRuntime::XPCJSRuntime(nsXPConnect* aXPConnect)
JS_SetWrapObjectCallbacks(mJSRuntime,
xpc::WrapperFactory::Rewrap,
xpc::WrapperFactory::PrepareForWrapping);
#ifdef MOZ_CRASHREPORTER
JS_EnumerateDiagnosticMemoryRegions(DiagnosticMemoryCallback);
#endif
mWatchdogWakeup = JS_NEW_CONDVAR(mJSRuntime->gcLock);
if (!mWatchdogWakeup)
NS_RUNTIMEABORT("JS_NEW_CONDVAR failed.");