Title: [213718] trunk/Source/_javascript_Core
Revision
213718
Author
mark....@apple.com
Date
2017-03-10 11:01:57 -0800 (Fri, 10 Mar 2017)

Log Message

[Re-landing] Implement a StackTrace utility object that can capture stack traces for debugging.
https://bugs.webkit.org/show_bug.cgi?id=169454

Reviewed by Michael Saboff.

The underlying implementation is hoisted right out of Assertions.cpp from the
implementations of WTFPrintBacktrace().

The reason we need this StackTrace object is because during heap debugging, we
sometimes want to capture the stack trace that allocated the objects of interest.
Dumping the stack trace directly to stdout (using WTFReportBacktrace()) may
perturb the execution profile sufficiently that an issue may not reproduce,
while alternatively, just capturing the stack trace and deferring printing it
till we actually need it later perturbs the execution profile less.

In addition, just capturing the stack traces (instead of printing them
immediately at each capture site) allows us to avoid polluting stdout with tons
of stack traces that may be irrelevant.

For now, we only capture the native stack trace.  We'll leave capturing and
integrating the JS stack trace as an exercise for the future if we need it then.

Here's an example of how to use this StackTrace utility:

    // Capture a stack trace of the top 10 frames.
    std::unique_ptr<StackTrace> trace(StackTrace::captureStackTrace(10));
    // Print the trace.
    dataLog(*trace);

* CMakeLists.txt:
* _javascript_Core.xcodeproj/project.pbxproj:
* tools/StackTrace.cpp: Added.
(JSC::StackTrace::instanceSize):
(JSC::StackTrace::captureStackTrace):
(JSC::StackTrace::dump):
* tools/StackTrace.h: Added.
(JSC::StackTrace::size):
(JSC::StackTrace::StackTrace):

Modified Paths

Added Paths

Diff

Modified: trunk/Source/_javascript_Core/CMakeLists.txt (213717 => 213718)


--- trunk/Source/_javascript_Core/CMakeLists.txt	2017-03-10 18:32:20 UTC (rev 213717)
+++ trunk/Source/_javascript_Core/CMakeLists.txt	2017-03-10 19:01:57 UTC (rev 213718)
@@ -924,6 +924,7 @@
     tools/JSDollarVM.cpp
     tools/JSDollarVMPrototype.cpp
     tools/SigillCrashAnalyzer.cpp
+    tools/StackTrace.cpp
     tools/VMInspector.cpp
 
     wasm/JSWebAssembly.cpp

Modified: trunk/Source/_javascript_Core/ChangeLog (213717 => 213718)


--- trunk/Source/_javascript_Core/ChangeLog	2017-03-10 18:32:20 UTC (rev 213717)
+++ trunk/Source/_javascript_Core/ChangeLog	2017-03-10 19:01:57 UTC (rev 213718)
@@ -1,3 +1,44 @@
+2017-03-10  Mark Lam  <mark....@apple.com>
+
+        [Re-landing] Implement a StackTrace utility object that can capture stack traces for debugging.
+        https://bugs.webkit.org/show_bug.cgi?id=169454
+
+        Reviewed by Michael Saboff.
+
+        The underlying implementation is hoisted right out of Assertions.cpp from the
+        implementations of WTFPrintBacktrace().
+
+        The reason we need this StackTrace object is because during heap debugging, we
+        sometimes want to capture the stack trace that allocated the objects of interest.
+        Dumping the stack trace directly to stdout (using WTFReportBacktrace()) may
+        perturb the execution profile sufficiently that an issue may not reproduce,
+        while alternatively, just capturing the stack trace and deferring printing it
+        till we actually need it later perturbs the execution profile less.
+
+        In addition, just capturing the stack traces (instead of printing them
+        immediately at each capture site) allows us to avoid polluting stdout with tons
+        of stack traces that may be irrelevant.
+
+        For now, we only capture the native stack trace.  We'll leave capturing and
+        integrating the JS stack trace as an exercise for the future if we need it then.
+
+        Here's an example of how to use this StackTrace utility:
+
+            // Capture a stack trace of the top 10 frames.
+            std::unique_ptr<StackTrace> trace(StackTrace::captureStackTrace(10));
+            // Print the trace.
+            dataLog(*trace);
+
+        * CMakeLists.txt:
+        * _javascript_Core.xcodeproj/project.pbxproj:
+        * tools/StackTrace.cpp: Added.
+        (JSC::StackTrace::instanceSize):
+        (JSC::StackTrace::captureStackTrace):
+        (JSC::StackTrace::dump):
+        * tools/StackTrace.h: Added.
+        (JSC::StackTrace::size):
+        (JSC::StackTrace::StackTrace):
+
 2017-03-04  Filip Pizlo  <fpi...@apple.com>
 
         B3 should have comprehensive support for atomic operations

Modified: trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj (213717 => 213718)


--- trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj	2017-03-10 18:32:20 UTC (rev 213717)
+++ trunk/Source/_javascript_Core/_javascript_Core.xcodeproj/project.pbxproj	2017-03-10 19:01:57 UTC (rev 213718)
@@ -2371,6 +2371,8 @@
 		FE1BD0211E72027900134BC9 /* CellProfile.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1BD0201E72027000134BC9 /* CellProfile.h */; };
 		FE1BD0241E72053800134BC9 /* HeapVerifier.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1BD0221E72052F00134BC9 /* HeapVerifier.cpp */; };
 		FE1BD0251E72053800134BC9 /* HeapVerifier.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1BD0231E72052F00134BC9 /* HeapVerifier.h */; };
+		FE1BD02B1E721B4C00134BC9 /* StackTrace.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1BD02A1E721B3700134BC9 /* StackTrace.h */; };
+		FE1BD02C1E721B5100134BC9 /* StackTrace.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1BD0291E721B3700134BC9 /* StackTrace.cpp */; };
 		FE1C0FFD1B193E9800B53FCA /* Exception.h in Headers */ = {isa = PBXBuildFile; fileRef = FE1C0FFC1B193E9800B53FCA /* Exception.h */; settings = {ATTRIBUTES = (Private, ); }; };
 		FE1C0FFF1B194FD100B53FCA /* Exception.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE1C0FFE1B194FD100B53FCA /* Exception.cpp */; };
 		FE20CE9D15F04A9500DF3430 /* LLIntCLoop.cpp in Sources */ = {isa = PBXBuildFile; fileRef = FE20CE9B15F04A9500DF3430 /* LLIntCLoop.cpp */; };
@@ -4957,6 +4959,8 @@
 		FE1BD0201E72027000134BC9 /* CellProfile.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CellProfile.h; sourceTree = "<group>"; };
 		FE1BD0221E72052F00134BC9 /* HeapVerifier.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = HeapVerifier.cpp; sourceTree = "<group>"; };
 		FE1BD0231E72052F00134BC9 /* HeapVerifier.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = HeapVerifier.h; sourceTree = "<group>"; };
+		FE1BD0291E721B3700134BC9 /* StackTrace.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = StackTrace.cpp; sourceTree = "<group>"; };
+		FE1BD02A1E721B3700134BC9 /* StackTrace.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StackTrace.h; sourceTree = "<group>"; };
 		FE1C0FFC1B193E9800B53FCA /* Exception.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Exception.h; sourceTree = "<group>"; };
 		FE1C0FFE1B194FD100B53FCA /* Exception.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Exception.cpp; sourceTree = "<group>"; };
 		FE20CE9B15F04A9500DF3430 /* LLIntCLoop.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = LLIntCLoop.cpp; path = llint/LLIntCLoop.cpp; sourceTree = "<group>"; };
@@ -6955,6 +6959,8 @@
 				86B5822C14D22F5F00A9C306 /* ProfileTreeNode.h */,
 				FE3022D01E3D739600BAC493 /* SigillCrashAnalyzer.cpp */,
 				FE3022D11E3D739600BAC493 /* SigillCrashAnalyzer.h */,
+				FE1BD0291E721B3700134BC9 /* StackTrace.cpp */,
+				FE1BD02A1E721B3700134BC9 /* StackTrace.h */,
 				86B5826A14D35D5100A9C306 /* TieredMMapArray.h */,
 				FE3022D41E42856700BAC493 /* VMInspector.cpp */,
 				FE3022D51E42856700BAC493 /* VMInspector.h */,
@@ -8759,6 +8765,7 @@
 				FE187A0D1C030D5C0038BBCA /* JITDivGenerator.h in Headers */,
 				0F46808214BA572D00BFE272 /* JITExceptions.h in Headers */,
 				0FB14E1F18124ACE009B6B4D /* JITInlineCacheGenerator.h in Headers */,
+				FE1BD02B1E721B4C00134BC9 /* StackTrace.h in Headers */,
 				86CC85A10EE79A4700288682 /* JITInlines.h in Headers */,
 				FE3A06BE1C11041200390FDD /* JITLeftShiftGenerator.h in Headers */,
 				79233C2B1D34715700C5A834 /* JITMathIC.h in Headers */,
@@ -10200,6 +10207,7 @@
 				0FC97F4118202119002C9B26 /* DFGWatchpointCollectionPhase.cpp in Sources */,
 				0FDB2CE7174830A2007B3C1B /* DFGWorklist.cpp in Sources */,
 				0FE050171AA9091100D33B33 /* DirectArguments.cpp in Sources */,
+				FE1BD02C1E721B5100134BC9 /* StackTrace.cpp in Sources */,
 				0FE050151AA9091100D33B33 /* DirectArgumentsOffset.cpp in Sources */,
 				0F2EBBAB1DEDF95000990369 /* DirectEvalCodeCache.cpp in Sources */,
 				14386A741DD69895008652C4 /* DirectEvalExecutable.cpp in Sources */,

Added: trunk/Source/_javascript_Core/tools/StackTrace.cpp (0 => 213718)


--- trunk/Source/_javascript_Core/tools/StackTrace.cpp	                        (rev 0)
+++ trunk/Source/_javascript_Core/tools/StackTrace.cpp	2017-03-10 19:01:57 UTC (rev 213718)
@@ -0,0 +1,105 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "config.h"
+#include "StackTrace.h"
+
+#include <wtf/Assertions.h>
+
+#if OS(DARWIN) || (OS(LINUX) && defined(__GLIBC__) && !defined(__UCLIBC__))
+#include <cxxabi.h>
+#include <dlfcn.h>
+#include <execinfo.h>
+#endif
+
+#if OS(DARWIN) || OS(LINUX)
+#  if PLATFORM(GTK)
+#    if defined(__GLIBC__) && !defined(__UCLIBC__)
+#      define USE_BACKTRACE_SYMBOLS 1
+#    endif
+#  else
+#    define USE_DLADDR 1
+#  endif
+#endif
+
+namespace JSC {
+
+ALWAYS_INLINE size_t StackTrace::instanceSize(int capacity)
+{
+    ASSERT(capacity >= 1);
+    return sizeof(StackTrace) + (capacity - 1) * sizeof(void*);
+}
+
+StackTrace* StackTrace::captureStackTrace(int maxFrames)
+{
+    maxFrames = std::max(1, maxFrames);
+    size_t sizeToAllocate = instanceSize(maxFrames);
+    StackTrace* trace = new (NotNull, fastMalloc(sizeToAllocate)) StackTrace();
+
+    static const int framesToSkip = 2;
+    int numberOfFrames = maxFrames + framesToSkip;
+    
+    WTFGetBacktrace(&trace->m_skippedFrame0, &numberOfFrames);
+    ASSERT(numberOfFrames > framesToSkip);
+    trace->m_size = numberOfFrames - framesToSkip;
+    trace->m_capacity = maxFrames;
+
+    return trace;
+}
+
+void StackTrace::dump(PrintStream& out) const
+{
+#if USE(BACKTRACE_SYMBOLS)
+    char** symbols = backtrace_symbols(m_stack, m_size);
+    if (!symbols)
+        return;
+#endif
+    
+    for (int i = 0; i < m_size; ++i) {
+        const char* mangledName = 0;
+        char* cxaDemangled = 0;
+#if USE(BACKTRACE_SYMBOLS)
+        mangledName = symbols[i];
+#elif USE(DLADDR)
+        Dl_info info;
+        if (dladdr(m_stack[i], &info) && info.dli_sname)
+            mangledName = info.dli_sname;
+        if (mangledName)
+            cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
+#endif
+        const int frameNumber = i + 1;
+        if (mangledName || cxaDemangled)
+            out.printf("%-3d %p %s\n", frameNumber, m_stack[i], cxaDemangled ? cxaDemangled : mangledName);
+        else
+            out.printf("%-3d %p\n", frameNumber, m_stack[i]);
+        free(cxaDemangled);
+    }
+    
+#if USE(BACKTRACE_SYMBOLS)
+    free(symbols);
+#endif
+}
+
+} // namespace JSC

Added: trunk/Source/_javascript_Core/tools/StackTrace.h (0 => 213718)


--- trunk/Source/_javascript_Core/tools/StackTrace.h	                        (rev 0)
+++ trunk/Source/_javascript_Core/tools/StackTrace.h	2017-03-10 19:01:57 UTC (rev 213718)
@@ -0,0 +1,65 @@
+/*
+ * Copyright (C) 2017 Apple Inc. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
+ * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE INC. OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
+ * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
+ * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#pragma once
+
+#include <wtf/PrintStream.h>
+
+namespace JSC {
+
+class StackTrace {
+    WTF_MAKE_FAST_ALLOCATED;
+public:
+    static StackTrace* captureStackTrace(int maxFrames);
+
+    int size() const { return m_size; }
+
+    void dump(PrintStream&) const;
+
+private:
+    inline static size_t instanceSize(int capacity);
+
+    StackTrace()
+        : m_size(0)
+    { }
+
+    // We structure the top fields this way because the underlying stack capture
+    // facility will capture from the top of the stack, and we'll need to skip the
+    // top 2 frame which is of no interest. Setting up the fields layout this way
+    // allows us to capture the stack in place and minimize space wastage.
+    union {
+        struct {
+            int m_size;
+            int m_capacity;
+        };
+        struct {
+            void* m_skippedFrame0;
+            void* m_skippedFrame1;
+        };
+    };
+    void* m_stack[1];
+};
+
+} // namespace JSC
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to