Changeset 256910 in webkit


Ignore:
Timestamp:
Feb 18, 2020 9:37:45 PM (4 years ago)
Author:
keith_miller@apple.com
Message:

Add an os_log PrintStream
https://bugs.webkit.org/show_bug.cgi?id=207898

Reviewed by Mark Lam.

Source/JavaScriptCore:

Add jsc option to write dataLogs to os_log.

  • runtime/Options.cpp:

(JSC::Options::initialize):

  • runtime/OptionsList.h:

Source/WTF:

When debugging on iOS writing to a file can be hard (thanks
Sandboxing!) so logging our dataLogs to os_log would make things
easier. This patch adds a new subclass of PrintStream,
OSLogPrintStream, that converts our file writes to
os_log. Unfortunately, os_log doesn't buffer writes so
OSLogPrintStream needs to do its own buffering. e.g. if you call
dataLogLn("some text with a ", number, " and a ", string);
os_log will log that as 5 separate logs.

  • WTF.xcodeproj/project.pbxproj:
  • wtf/CMakeLists.txt:
  • wtf/DataLog.cpp:

(WTF::setDataFile):

  • wtf/DataLog.h:
  • wtf/OSLogPrintStream.cpp: Added.

(WTF::OSLogPrintStream::OSLogPrintStream):
(WTF::OSLogPrintStream::~OSLogPrintStream):
(WTF::OSLogPrintStream::open):
(WTF::OSLogPrintStream::vprintf):

  • wtf/OSLogPrintStream.h: Copied from Source/WTF/wtf/DataLog.h.
  • wtf/PrintStream.cpp:

(WTF::printExpectedCStringHelper):
(WTF::printInternal):

  • wtf/text/CString.cpp:

(WTF::CString::grow):

  • wtf/text/CString.h:
Location:
trunk/Source
Files:
1 added
11 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/Source/JavaScriptCore/ChangeLog

    r256850 r256910  
     12020-02-18  Keith Miller  <keith_miller@apple.com>
     2
     3        Add an os_log PrintStream
     4        https://bugs.webkit.org/show_bug.cgi?id=207898
     5
     6        Reviewed by Mark Lam.
     7
     8        Add jsc option to write dataLogs to os_log.
     9
     10        * runtime/Options.cpp:
     11        (JSC::Options::initialize):
     12        * runtime/OptionsList.h:
     13
    1142020-02-18  Paulo Matos  <pmatos@igalia.com>
    215
  • trunk/Source/JavaScriptCore/runtime/Options.cpp

    r256500 r256910  
    4343#include <wtf/NumberOfCores.h>
    4444#include <wtf/Optional.h>
     45#include <wtf/OSLogPrintStream.h>
    4546#include <wtf/PointerPreparations.h>
    4647#include <wtf/StdLibExtras.h>
     
    614615#endif
    615616
     617#if OS(DARWIN)
     618            if (Options::useOSLog()) {
     619                WTF::setDataFile(OSLogPrintStream::open("com.apple.JavaScriptCore", "DataLog", OS_LOG_TYPE_INFO));
     620                // Make sure no one jumped here for nefarious reasons...
     621                RELEASE_ASSERT(useOSLog());
     622            }
     623#endif
     624
    616625#if ASAN_ENABLED && OS(LINUX) && ENABLE(WEBASSEMBLY_FAST_MEMORY)
    617626            if (Options::useWebAssemblyFastMemory()) {
  • trunk/Source/JavaScriptCore/runtime/OptionsList.h

    r256500 r256910  
    120120    v(Unsigned, shadowChickenMaxTailDeletedFramesSize, 128, Normal, nullptr) \
    121121    \
     122    v(Bool, useOSLog, false, Normal, "Log dataLog()s to os_log instead of stderr") \
    122123    /* dumpDisassembly implies dumpDFGDisassembly. */ \
    123124    v(Bool, dumpDisassembly, false, Normal, "dumps disassembly of all JIT compiled code upon compilation") \
  • trunk/Source/WTF/ChangeLog

    r256834 r256910  
     12020-02-18  Keith Miller  <keith_miller@apple.com>
     2
     3        Add an os_log PrintStream
     4        https://bugs.webkit.org/show_bug.cgi?id=207898
     5
     6        Reviewed by Mark Lam.
     7
     8        When debugging on iOS writing to a file can be hard (thanks
     9        Sandboxing!)  so logging our dataLogs to os_log would make things
     10        easier. This patch adds a new subclass of PrintStream,
     11        OSLogPrintStream, that converts our file writes to
     12        os_log. Unfortunately, os_log doesn't buffer writes so
     13        OSLogPrintStream needs to do its own buffering. e.g. if you call
     14        `dataLogLn("some text with a ", number, " and a ", string);`
     15        os_log will log that as 5 separate logs.
     16
     17        * WTF.xcodeproj/project.pbxproj:
     18        * wtf/CMakeLists.txt:
     19        * wtf/DataLog.cpp:
     20        (WTF::setDataFile):
     21        * wtf/DataLog.h:
     22        * wtf/OSLogPrintStream.cpp: Added.
     23        (WTF::OSLogPrintStream::OSLogPrintStream):
     24        (WTF::OSLogPrintStream::~OSLogPrintStream):
     25        (WTF::OSLogPrintStream::open):
     26        (WTF::OSLogPrintStream::vprintf):
     27        * wtf/OSLogPrintStream.h: Copied from Source/WTF/wtf/DataLog.h.
     28        * wtf/PrintStream.cpp:
     29        (WTF::printExpectedCStringHelper):
     30        (WTF::printInternal):
     31        * wtf/text/CString.cpp:
     32        (WTF::CString::grow):
     33        * wtf/text/CString.h:
     34
    1352020-02-18  Simon Fraser  <simon.fraser@apple.com>
    236
  • trunk/Source/WTF/WTF.xcodeproj/project.pbxproj

    r255577 r256910  
    7979                5311BD5C1EA822F900525281 /* ThreadMessage.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5311BD5B1EA822F900525281 /* ThreadMessage.cpp */; };
    8080                53534F2A1EC0E10E00141B2F /* MachExceptions.defs in Sources */ = {isa = PBXBuildFile; fileRef = 53534F291EC0E10E00141B2F /* MachExceptions.defs */; settings = {ATTRIBUTES = (Client, Server, ); }; };
     81                53FC70D023FB950C005B1990 /* OSLogPrintStream.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 53FC70CF23FB950C005B1990 /* OSLogPrintStream.cpp */; };
    8182                5C1F05932164356B0039302C /* CFURLExtras.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5C1F05912164356B0039302C /* CFURLExtras.cpp */; };
    8283                5C1F0595216437B30039302C /* URLCF.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 5C1F0594216437B30039302C /* URLCF.cpp */; };
     
    400401                53F08A1BA39D49A8BAD369A1 /* StdSet.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = StdSet.h; sourceTree = "<group>"; };
    401402                53F1D98620477B9800EBC6BF /* FunctionTraits.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.h; path = FunctionTraits.h; sourceTree = "<group>"; };
     403                53FC70CE23FB950C005B1990 /* OSLogPrintStream.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OSLogPrintStream.h; sourceTree = "<group>"; };
     404                53FC70CF23FB950C005B1990 /* OSLogPrintStream.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = OSLogPrintStream.cpp; sourceTree = "<group>"; };
    402405                553071C91C40427200384898 /* TinyLRUCache.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TinyLRUCache.h; sourceTree = "<group>"; };
    403406                5597F82C1D94B9970066BC21 /* SynchronizedFixedQueue.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SynchronizedFixedQueue.h; sourceTree = "<group>"; };
     
    11131116                                0F9495831C571CC900413A48 /* OrderMaker.h */,
    11141117                                A8A472D7151A825B004123FF /* OSAllocator.h */,
     1118                                53FC70CF23FB950C005B1990 /* OSLogPrintStream.cpp */,
     1119                                53FC70CE23FB950C005B1990 /* OSLogPrintStream.h */,
    11151120                                7CBBA07319BB7FDC00BBF025 /* OSObjectPtr.h */,
    11161121                                A8A472DA151A825B004123FF /* OSRandomSource.cpp */,
     
    16651670                                8348BA0E21FBC0D500FD3054 /* ObjectIdentifier.cpp in Sources */,
    16661671                                A3EE5C3A21FFAC5F00FABD61 /* OSAllocatorPOSIX.cpp in Sources */,
     1672                                53FC70D023FB950C005B1990 /* OSLogPrintStream.cpp in Sources */,
    16671673                                A8A473F9151A825B004123FF /* OSRandomSource.cpp in Sources */,
    16681674                                A8A47402151A825B004123FF /* PageBlock.cpp in Sources */,
  • trunk/Source/WTF/wtf/CMakeLists.txt

    r255577 r256910  
    149149    NumberOfCores.h
    150150    OSAllocator.h
     151    OSLogPrintStream.h
    151152    OSObjectPtr.h
    152153    OSRandomSource.h
     
    401402    MonotonicTime.cpp
    402403    NumberOfCores.cpp
     404    OSLogPrintStream.cpp
    403405    OSRandomSource.cpp
    404406    ObjectIdentifier.cpp
  • trunk/Source/WTF/wtf/DataLog.cpp

    r250005 r256910  
    167167}
    168168
     169void setDataFile(std::unique_ptr<PrintStream>&& file)
     170{
     171    s_file = file.release();
     172}
     173
    169174PrintStream& dataFile()
    170175{
  • trunk/Source/WTF/wtf/DataLog.h

    r254714 r256910  
    3535WTF_EXPORT_PRIVATE PrintStream& dataFile();
    3636WTF_EXPORT_PRIVATE void setDataFile(const char* path);
     37WTF_EXPORT_PRIVATE void setDataFile(std::unique_ptr<PrintStream>&&);
    3738
    3839WTF_EXPORT_PRIVATE void dataLogFV(const char* format, va_list) WTF_ATTRIBUTE_PRINTF(1, 0);
  • trunk/Source/WTF/wtf/OSLogPrintStream.h

    r256909 r256910  
    11/*
    2  * Copyright (C) 2012 Apple Inc. All rights reserved.
     2 * Copyright (C) 2020 Apple Inc. All rights reserved.
    33 *
    44 * Redistribution and use in source and binary forms, with or without
     
    2626#pragma once
    2727
    28 #include <stdarg.h>
    29 #include <stdio.h>
     28#if OS(DARWIN)
     29
     30#include <wtf/Lock.h>
    3031#include <wtf/PrintStream.h>
    31 #include <wtf/StdLibExtras.h>
     32#include <wtf/RecursiveLockAdapter.h>
     33#include <wtf/text/CString.h>
     34#include <wtf/Vector.h>
     35
     36#include <os/log.h>
    3237
    3338namespace WTF {
    3439
    35 WTF_EXPORT_PRIVATE PrintStream& dataFile();
    36 WTF_EXPORT_PRIVATE void setDataFile(const char* path);
     40class WTF_EXPORT_PRIVATE OSLogPrintStream final : public PrintStream {
     41public:
     42    OSLogPrintStream(os_log_t, os_log_type_t);
     43    virtual ~OSLogPrintStream();
     44   
     45    static std::unique_ptr<OSLogPrintStream> open(const char* subsystem, const char* category, os_log_type_t = OS_LOG_TYPE_DEFAULT);
     46   
     47    void vprintf(const char* format, va_list) override WTF_ATTRIBUTE_PRINTF(2, 0);
    3748
    38 WTF_EXPORT_PRIVATE void dataLogFV(const char* format, va_list) WTF_ATTRIBUTE_PRINTF(1, 0);
    39 WTF_EXPORT_PRIVATE void dataLogF(const char* format, ...) WTF_ATTRIBUTE_PRINTF(1, 2);
    40 WTF_EXPORT_PRIVATE void dataLogFString(const char*);
    41 
    42 template<typename... Types>
    43 NEVER_INLINE void dataLog(const Types&... values)
    44 {
    45     dataFile().print(values...);
    46 }
    47 
    48 template<typename... Types>
    49 void dataLogLn(const Types&... values)
    50 {
    51     dataLog(values..., "\n");
    52 }
    53 
    54 template<typename... Types>
    55 ALWAYS_INLINE void dataLogIf(bool shouldLog, const Types&... values)
    56 {
    57     if (UNLIKELY(shouldLog))
    58         dataLog(values...);
    59 }
    60 
    61 template<typename... Types>
    62 ALWAYS_INLINE void dataLogLnIf(bool shouldLog, const Types&... values)
    63 {
    64     if (UNLIKELY(shouldLog))
    65         dataLogLn(values...);
    66 }
     49private:
     50    os_log_t m_log;
     51    os_log_type_t m_logType;
     52    Lock m_stringLock;
     53    // We need a buffer because os_log doesn't wait for a new line to print the characters.
     54    CString m_string;
     55    size_t m_offset { 0 };
     56};
    6757
    6858} // namespace WTF
    6959
    70 using WTF::dataLog;
    71 using WTF::dataLogLn;
    72 using WTF::dataLogIf;
    73 using WTF::dataLogLnIf;
    74 using WTF::dataLogF;
    75 using WTF::dataLogFString;
     60using WTF::OSLogPrintStream;
     61
     62#endif
  • trunk/Source/WTF/wtf/PrintStream.cpp

    r237099 r256910  
    7878{
    7979    if (UNLIKELY(!expectedCString)) {
    80         if (expectedCString.error() == UTF8ConversionError::OutOfMemory)
    81             out.print("(Out of memory while converting ", type, " to utf8)");
    82         else
    83             out.print("(failed to convert ", type, " to utf8)");
     80        if (expectedCString.error() == UTF8ConversionError::OutOfMemory) {
     81            printInternal(out, "(Out of memory while converting ");
     82            printInternal(out, type);
     83            printInternal(out, " to utf8)");
     84        } else {
     85            printInternal(out, "(failed to convert ");
     86            printInternal(out, type);
     87            printInternal(out, " to utf8)");
     88        }
    8489        return;
    8590    }
    86     out.print(expectedCString.value());
     91    printInternal(out, expectedCString.value());
    8792}
    8893
     
    9499void printInternal(PrintStream& out, const CString& string)
    95100{
    96     out.print(string.data());
     101    printInternal(out, string.data());
    97102}
    98103
     
    105110{
    106111    if (!string) {
    107         out.print("(null StringImpl*)");
     112        printInternal(out, "(null StringImpl*)");
    108113        return;
    109114    }
     
    168173void printInternal(PrintStream& out, float value)
    169174{
    170     out.print(static_cast<double>(value));
     175    printInternal(out, static_cast<double>(value));
    171176}
    172177
  • trunk/Source/WTF/wtf/text/CString.cpp

    r253987 r256910  
    107107}
    108108
     109void CString::grow(size_t newLength)
     110{
     111    ASSERT(newLength > length());
     112
     113    auto newBuffer = CStringBuffer::createUninitialized(newLength);
     114    memcpy(newBuffer->mutableData(), m_buffer->data(), length() + 1);
     115    m_buffer = WTFMove(newBuffer);
     116}
     117
    109118bool operator==(const CString& a, const CString& b)
    110119{
  • trunk/Source/WTF/wtf/text/CString.h

    r253987 r256910  
    8585    WTF_EXPORT_PRIVATE unsigned hash() const;
    8686
     87    // Useful if you want your CString to hold dynamic data.
     88    WTF_EXPORT_PRIVATE void grow(size_t newLength);
     89
    8790private:
    8891    void copyBufferIfNeeded();
Note: See TracChangeset for help on using the changeset viewer.