2017-03-10 19:01:57 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2017 Apple Inc. All rights reserved.
|
2017-04-25 02:53:49 +00:00
|
|
|
* Copyright (C) 2017 Yusuke Suzuki <utatane.tea@gmail.com>
|
2017-03-10 19:01:57 +00:00
|
|
|
*
|
|
|
|
* 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"
|
2018-10-15 14:24:49 +00:00
|
|
|
#include <wtf/StackTrace.h>
|
2017-03-10 19:01:57 +00:00
|
|
|
|
|
|
|
#include <wtf/Assertions.h>
|
2017-04-25 02:53:49 +00:00
|
|
|
#include <wtf/PrintStream.h>
|
2017-03-10 19:01:57 +00:00
|
|
|
|
2017-07-14 16:03:47 +00:00
|
|
|
#if HAVE(BACKTRACE_SYMBOLS) || HAVE(BACKTRACE)
|
2017-06-20 02:59:31 +00:00
|
|
|
#include <execinfo.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE(DLADDR)
|
2017-03-10 19:01:57 +00:00
|
|
|
#include <cxxabi.h>
|
|
|
|
#include <dlfcn.h>
|
|
|
|
#endif
|
|
|
|
|
2017-07-14 16:03:47 +00:00
|
|
|
#if OS(WINDOWS)
|
|
|
|
#include <windows.h>
|
2018-10-24 14:27:13 +00:00
|
|
|
#include <wtf/win/DbgHelperWin.h>
|
2017-07-14 16:03:47 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
void WTFGetBacktrace(void** stack, int* size)
|
|
|
|
{
|
|
|
|
#if HAVE(BACKTRACE)
|
|
|
|
*size = backtrace(stack, *size);
|
|
|
|
#elif OS(WINDOWS)
|
2020-05-08 17:31:54 +00:00
|
|
|
*size = RtlCaptureStackBackTrace(0, *size, stack, nullptr);
|
2017-07-14 16:03:47 +00:00
|
|
|
#else
|
2018-12-04 02:30:27 +00:00
|
|
|
UNUSED_PARAM(stack);
|
2017-07-14 16:03:47 +00:00
|
|
|
*size = 0;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:53:49 +00:00
|
|
|
namespace WTF {
|
2017-03-10 19:01:57 +00:00
|
|
|
|
|
|
|
ALWAYS_INLINE size_t StackTrace::instanceSize(int capacity)
|
|
|
|
{
|
|
|
|
ASSERT(capacity >= 1);
|
|
|
|
return sizeof(StackTrace) + (capacity - 1) * sizeof(void*);
|
|
|
|
}
|
|
|
|
|
2017-07-14 16:03:47 +00:00
|
|
|
std::unique_ptr<StackTrace> StackTrace::captureStackTrace(int maxFrames, int framesToSkip)
|
2017-03-10 19:01:57 +00:00
|
|
|
{
|
|
|
|
maxFrames = std::max(1, maxFrames);
|
|
|
|
size_t sizeToAllocate = instanceSize(maxFrames);
|
2017-07-14 16:03:47 +00:00
|
|
|
std::unique_ptr<StackTrace> trace(new (NotNull, fastMalloc(sizeToAllocate)) StackTrace());
|
2017-03-10 19:01:57 +00:00
|
|
|
|
2017-05-08 16:56:32 +00:00
|
|
|
// Skip 2 additional frames i.e. StackTrace::captureStackTrace and WTFGetBacktrace.
|
|
|
|
framesToSkip += 2;
|
2017-03-10 19:01:57 +00:00
|
|
|
int numberOfFrames = maxFrames + framesToSkip;
|
2017-04-25 02:53:49 +00:00
|
|
|
|
2017-03-10 19:01:57 +00:00
|
|
|
WTFGetBacktrace(&trace->m_skippedFrame0, &numberOfFrames);
|
2017-07-01 21:42:43 +00:00
|
|
|
if (numberOfFrames) {
|
|
|
|
RELEASE_ASSERT(numberOfFrames >= framesToSkip);
|
|
|
|
trace->m_size = numberOfFrames - framesToSkip;
|
|
|
|
} else
|
|
|
|
trace->m_size = 0;
|
|
|
|
|
2017-03-10 19:01:57 +00:00
|
|
|
trace->m_capacity = maxFrames;
|
|
|
|
|
|
|
|
return trace;
|
|
|
|
}
|
|
|
|
|
2021-05-30 16:11:40 +00:00
|
|
|
auto StackTrace::demangle(void* pc) -> std::optional<DemangleEntry>
|
2017-04-25 02:53:49 +00:00
|
|
|
{
|
|
|
|
#if HAVE(DLADDR)
|
|
|
|
const char* mangledName = nullptr;
|
|
|
|
const char* cxaDemangled = nullptr;
|
|
|
|
Dl_info info;
|
|
|
|
if (dladdr(pc, &info) && info.dli_sname)
|
|
|
|
mangledName = info.dli_sname;
|
|
|
|
if (mangledName) {
|
|
|
|
int status = 0;
|
|
|
|
cxaDemangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
|
|
|
|
UNUSED_PARAM(status);
|
|
|
|
}
|
|
|
|
if (mangledName || cxaDemangled)
|
|
|
|
return DemangleEntry { mangledName, cxaDemangled };
|
2018-12-04 02:30:27 +00:00
|
|
|
#else
|
|
|
|
UNUSED_PARAM(pc);
|
2017-04-25 02:53:49 +00:00
|
|
|
#endif
|
Next step toward using std::optional directly instead of through WTF::Optional typedef
https://bugs.webkit.org/show_bug.cgi?id=226280
Reviewed by Chris Dumez.
Source/JavaScriptCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* yarr/YarrSyntaxChecker.cpp: Since the style checker complained about this file,
tweaked style to make it happy after the renaming done by do-webcore-rename, and
also hand-updated Optional to std::optional as long as we were touching it.
Source/WebCore:
* <many files>: Accept the renaming done by do-webcore-rename.
* Modules/webauthn/fido/DeviceRequestConverter.h: Since style checker complained
about the names of some arguments, fixed them, and also hand-updated Optional to
std::optional as long as we were touching it.
* loader/EmptyClients.cpp: Since style checker complained about the mix of
WEBCORE_EXPORT and inlined functions, moved them out of line, and
also hand-updated Optional to std::optional as long as we were touching it.
Also removed is<EmptyFrameLoaderClient>().
* loader/EmptyFrameLoaderClient.h: Ditto.
Source/WebCore/PAL:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebDriver:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKit:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy:
* Storage/StorageTracker.cpp:
(WebKit::StorageTracker::diskUsageForOrigin): Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/mac:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WebKitLegacy/win:
* <many files>: Accept the renaming done by do-webcore-rename.
Source/WTF:
* <many files>: Accept the renaming done by do-webcore-rename.
* wtf/Optional.h: Remove WTF::nullopt_t and WTF::makeOptional.
* wtf/URLHelpers.cpp:
(WTF::URLHelpers::mapHostName): Convert from nullopt to std::nullopt.
Tools:
* Scripts/do-webcore-rename: Use script to rename valueOr, WTF::nullopt, WTF::nullopt_t,
WTF::Optional, WTF::makeOptional, and makeOptional. Other renamings can't necessarily
be done by the script and so will be done in later passes.
* <many files>: Accept the renaming done by do-webcore-rename.
Canonical link: https://commits.webkit.org/238228@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@278185 268f45cc-cd09-0410-ab3c-d52691b4dbfc
2021-05-28 01:26:23 +00:00
|
|
|
return std::nullopt;
|
2017-04-25 02:53:49 +00:00
|
|
|
}
|
|
|
|
|
2017-05-08 16:56:32 +00:00
|
|
|
void StackTrace::dump(PrintStream& out, const char* indentString) const
|
2017-03-10 19:01:57 +00:00
|
|
|
{
|
2017-04-25 02:53:49 +00:00
|
|
|
const auto* stack = this->stack();
|
|
|
|
#if HAVE(BACKTRACE_SYMBOLS)
|
|
|
|
char** symbols = backtrace_symbols(stack, m_size);
|
2017-03-10 19:01:57 +00:00
|
|
|
if (!symbols)
|
|
|
|
return;
|
2018-10-24 14:27:13 +00:00
|
|
|
#elif OS(WINDOWS)
|
|
|
|
HANDLE hProc = GetCurrentProcess();
|
|
|
|
uint8_t symbolData[sizeof(SYMBOL_INFO) + MAX_SYM_NAME * sizeof(TCHAR)] = { 0 };
|
|
|
|
auto symbolInfo = reinterpret_cast<SYMBOL_INFO*>(symbolData);
|
|
|
|
|
|
|
|
symbolInfo->SizeOfStruct = sizeof(SYMBOL_INFO);
|
|
|
|
symbolInfo->MaxNameLen = MAX_SYM_NAME;
|
2017-03-10 19:01:57 +00:00
|
|
|
#endif
|
2017-04-25 02:53:49 +00:00
|
|
|
|
2017-05-08 16:56:32 +00:00
|
|
|
if (!indentString)
|
|
|
|
indentString = "";
|
2017-03-10 19:01:57 +00:00
|
|
|
for (int i = 0; i < m_size; ++i) {
|
2017-04-25 02:53:49 +00:00
|
|
|
const char* mangledName = nullptr;
|
|
|
|
const char* cxaDemangled = nullptr;
|
|
|
|
#if HAVE(BACKTRACE_SYMBOLS)
|
2017-03-10 19:01:57 +00:00
|
|
|
mangledName = symbols[i];
|
2020-02-25 09:19:22 +00:00
|
|
|
#elif OS(WINDOWS)
|
2020-05-08 17:31:54 +00:00
|
|
|
if (DbgHelper::SymFromAddress(hProc, reinterpret_cast<DWORD64>(stack[i]), nullptr, symbolInfo))
|
2020-02-25 09:19:22 +00:00
|
|
|
mangledName = symbolInfo->Name;
|
|
|
|
#endif
|
2017-04-25 02:53:49 +00:00
|
|
|
auto demangled = demangle(stack[i]);
|
|
|
|
if (demangled) {
|
|
|
|
mangledName = demangled->mangledName();
|
|
|
|
cxaDemangled = demangled->demangledName();
|
|
|
|
}
|
2017-03-10 19:01:57 +00:00
|
|
|
const int frameNumber = i + 1;
|
|
|
|
if (mangledName || cxaDemangled)
|
2021-06-04 17:58:37 +00:00
|
|
|
out.printf("%s%s%-3d %p %s\n", m_prefix ? m_prefix : "", indentString, frameNumber, stack[i], cxaDemangled ? cxaDemangled : mangledName);
|
2017-03-10 19:01:57 +00:00
|
|
|
else
|
2021-06-04 17:58:37 +00:00
|
|
|
out.printf("%s%s%-3d %p\n", m_prefix ? m_prefix : "", indentString, frameNumber, stack[i]);
|
2017-03-10 19:01:57 +00:00
|
|
|
}
|
2017-04-25 02:53:49 +00:00
|
|
|
|
|
|
|
#if HAVE(BACKTRACE_SYMBOLS)
|
2017-03-10 19:01:57 +00:00
|
|
|
free(symbols);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-04-25 02:53:49 +00:00
|
|
|
} // namespace WTF
|