Ignore:
Timestamp:
Oct 4, 2011, 8:39:46 AM (14 years ago)
Author:
[email protected]
Message:

add more stack dumping methods
https://bugs.webkit.org/show_bug.cgi?id=69018

In addition to WTFReportBacktrace, this adds the cross-platform WTFGetBacktrace, which lets
WebKit programmatically retrieve the current stack. This is useful if you need to add more
reporting to field crash report uploads, if you're tracking down an irreproducable bug,
for instance.

Reviewed by Darin Adler.

  • wtf/Assertions.cpp:
  • wtf/Assertions.h:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/wtf/Assertions.cpp

    r95555 r96595  
    5050#endif
    5151
    52 #if PLATFORM(MAC)
     52#if OS(DARWIN) || OS(LINUX)
    5353#include <cxxabi.h>
    5454#include <dlfcn.h>
     
    167167}
    168168
     169void WTFGetBacktrace(void** stack, int* size)
     170{
     171#if OS(DARWIN) || OS(LINUX)
     172    *size = backtrace(stack, *size);
     173#elif OS(WINDOWS)
     174    // The CaptureStackBackTrace function is available in XP, but it is not defined
     175    // in the Windows Server 2003 R2 Platform SDK. So, we'll grab the function
     176    // through GetProcAddress.
     177    typedef WORD (NTAPI* RtlCaptureStackBackTraceFunc)(DWORD, DWORD, PVOID*, PDWORD);
     178    HMODULE kernel32 = ::GetModuleHandleW(L"Kernel32.dll");
     179    if (!kernel32) {
     180        *size = 0;
     181        return;
     182    }
     183    RtlCaptureStackBackTraceFunc captureStackBackTraceFunc = reinterpret_cast<RtlCaptureStackBackTraceFunc>(
     184        ::GetProcAddress(kernel32, "RtlCaptureStackBackTrace"));
     185    if (captureStackBackTraceFunc)
     186        *size = captureStackBackTraceFunc(1, *size, stack, 0);
     187    else
     188        *size = 0;
     189#else
     190    *size = 0;
     191#endif
     192}
     193
    169194void WTFReportBacktrace()
    170195{
    171 #if PLATFORM(MAC)
    172196    static const int maxFrames = 32;
    173197    void* samples[maxFrames];
    174     int frames = backtrace(samples, maxFrames);
     198    int frames = maxFrames;
     199
     200    WTFGetBacktrace(samples, &frames);
    175201
    176202    for (int i = 1; i < frames; ++i) {
    177         void* pointer = samples[i];
    178 
    179         // Try to get a symbol name from the dynamic linker.
     203        const char* mangledName = 0;
     204        char* cxaDemangled = 0;
     205
     206#if !PLATFORM(QT) && (OS(DARWIN) || OS(LINUX))
    180207        Dl_info info;
    181         if (dladdr(pointer, &info) && info.dli_sname) {
    182             const char* mangledName = info.dli_sname;
    183 
    184             // Assume c++ & try to demangle the name.
    185             char* demangledName = abi::__cxa_demangle(mangledName, 0, 0, 0);
    186             if (demangledName) {
    187                 fprintf(stderr, "%-3d %s\n", i, demangledName);
    188                 free(demangledName);
    189             } else
    190                 fprintf(stderr, "%-3d %s\n", i, mangledName);
    191         } else
    192             fprintf(stderr, "%-3d %p\n", i, pointer);
    193     }
    194 #endif
     208        if (dladdr(samples[i], &info) && info.dli_sname)
     209            mangledName = info.dli_sname;
     210        if (mangledName)
     211            cxaDemangled = abi::__cxa_demangle(mangledName, 0, 0, 0);
     212#endif
     213        if (mangledName || cxaDemangled)
     214            fprintf(stderr, "%-3d %p %s\n", i, samples[i], cxaDemangled ? cxaDemangled : mangledName);
     215        else
     216            fprintf(stderr, "%-3d %p\n", i, samples[i]);
     217        free(cxaDemangled);
     218    }
    195219}
    196220
Note: See TracChangeset for help on using the changeset viewer.