Bug 1023461 - Record filename and line number for chrome JS entries; r=snorp

This commit is contained in:
Jim Chen
2014-06-20 14:28:10 -04:00
parent 0112d5b817
commit d73297430f
2 changed files with 58 additions and 9 deletions

View File

@@ -8,10 +8,15 @@
#include "nsJSPrincipals.h"
#include "nsScriptSecurityManager.h"
#include "jsfriendapi.h"
#include "prprf.h"
#include "js/OldDebugAPI.h"
#include "mozilla/Assertions.h"
#include "mozilla/Move.h"
#include <string.h>
#ifdef XP_LINUX
#include <unistd.h>
#include <sys/syscall.h>
@@ -59,6 +64,7 @@ ThreadStackHelper::ThreadStackHelper()
#endif
mStackToFill(nullptr)
, mMaxStackSize(Stack::sMaxInlineStorage)
, mMaxBufferSize(0)
{
#if defined(XP_LINUX)
mThreadID = ::syscall(SYS_gettid);
@@ -202,7 +208,11 @@ ThreadStackHelper::PrepareStackBuffer(Stack& aStack)
}
#endif
MOZ_ASSERT(mPseudoStack);
MOZ_ALWAYS_TRUE(aStack.reserve(mMaxStackSize));
if (!aStack.reserve(mMaxStackSize) ||
!aStack.reserve(aStack.capacity()) || // reserve up to the capacity
!aStack.EnsureBufferCapacity(mMaxBufferSize)) {
return false;
}
mStackToFill = &aStack;
return true;
#else
@@ -232,21 +242,46 @@ IsChromeJSScript(JSScript* aScript)
const char*
ThreadStackHelper::AppendJSEntry(const volatile StackEntry* aEntry,
intptr_t& aAvailableBufferSize,
const char* aPrevLabel)
{
// May be called from another thread or inside a signal handler.
// We assume querying the script is safe but we must not manupulate it.
// Also we must not allocate any memory from heap.
MOZ_ASSERT(aEntry->isJs());
MOZ_ASSERT(aEntry->script());
const char* label;
if (IsChromeJSScript(aEntry->script())) {
const char* const filename = JS_GetScriptFilename(aEntry->script());
unsigned lineno = JS_PCToLineNumber(nullptr, aEntry->script(),
aEntry->pc());
MOZ_ASSERT(filename);
char buffer[64]; // Enough to fit longest js file name from the tree
const char* const basename = strrchr(filename, '/');
size_t len = PR_snprintf(buffer, sizeof(buffer), "%s:%u",
basename ? basename + 1 : filename, lineno);
if (len < sizeof(buffer)) {
if (mStackToFill->IsSameAsEntry(aPrevLabel, buffer)) {
return aPrevLabel;
}
// Keep track of the required buffer size
aAvailableBufferSize -= (len + 1);
if (aAvailableBufferSize >= 0) {
// Buffer is big enough.
return mStackToFill->InfallibleAppendViaBuffer(buffer, len);
}
// Buffer is not big enough; fall through to using static label below.
}
// snprintf failed or buffer is not big enough.
label = "(chrome script)";
} else {
label = "(content script)";
}
if (label == aPrevLabel) {
if (mStackToFill->IsSameAsEntry(aPrevLabel, label)) {
return aPrevLabel;
}
mStackToFill->infallibleAppend(label);
@@ -258,8 +293,12 @@ ThreadStackHelper::AppendJSEntry(const volatile StackEntry* aEntry,
void
ThreadStackHelper::FillStackBuffer()
{
MOZ_ASSERT(mStackToFill->empty());
#ifdef MOZ_ENABLE_PROFILER_SPS
size_t reservedSize = mMaxStackSize;
size_t reservedSize = mStackToFill->capacity();
size_t reservedBufferSize = mStackToFill->AvailableBufferSize();
intptr_t availableBufferSize = intptr_t(reservedBufferSize);
// Go from front to back
const volatile StackEntry* entry = mPseudoStack->mStack;
@@ -267,24 +306,32 @@ ThreadStackHelper::FillStackBuffer()
// Deduplicate identical, consecutive frames
const char* prevLabel = nullptr;
for (; reservedSize-- && entry != end; entry++) {
/* We only accept non-copy labels, because
we are unable to actually copy labels here */
/* We only accept non-copy labels, including js::RunScript,
because we only want static labels in the hang stack. */
if (entry->isCopyLabel()) {
continue;
}
if (entry->isJs()) {
prevLabel = AppendJSEntry(entry, prevLabel);
prevLabel = AppendJSEntry(entry, availableBufferSize, prevLabel);
continue;
}
const char* const label = entry->label();
if (label == prevLabel) {
if (mStackToFill->IsSameAsEntry(prevLabel, label)) {
continue;
}
mStackToFill->infallibleAppend(label);
prevLabel = label;
}
// If we exited early due to buffer size, expand the buffer for next time
mMaxStackSize += (end - entry);
// end != entry if we exited early due to not enough reserved frames.
// Expand the number of reserved frames for next time.
mMaxStackSize = mStackToFill->capacity() + (end - entry);
// availableBufferSize < 0 if we needed a larger buffer than we reserved.
// Calculate a new reserve size for next time.
if (availableBufferSize < 0) {
mMaxBufferSize = reservedBufferSize - availableBufferSize;
}
#endif
}