tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

platform.h (6842B)


      1 // Copyright (c) 2006-2011 The Chromium Authors. All rights reserved.
      2 //
      3 // Redistribution and use in source and binary forms, with or without
      4 // modification, are permitted provided that the following conditions
      5 // are met:
      6 //  * Redistributions of source code must retain the above copyright
      7 //    notice, this list of conditions and the following disclaimer.
      8 //  * Redistributions in binary form must reproduce the above copyright
      9 //    notice, this list of conditions and the following disclaimer in
     10 //    the documentation and/or other materials provided with the
     11 //    distribution.
     12 //  * Neither the name of Google, Inc. nor the names of its contributors
     13 //    may be used to endorse or promote products derived from this
     14 //    software without specific prior written permission.
     15 //
     16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     19 // FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     20 // COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     21 // INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     22 // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     23 // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     24 // AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     25 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     26 // OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     27 // SUCH DAMAGE.
     28 
     29 #ifndef TOOLS_PLATFORM_H_
     30 #define TOOLS_PLATFORM_H_
     31 
     32 #include "PlatformMacros.h"
     33 
     34 #include "mozilla/BaseProfiler.h"
     35 
     36 #include "mozilla/Atomics.h"
     37 #include "mozilla/Logging.h"
     38 #include "mozilla/UniquePtr.h"
     39 #include "mozilla/Vector.h"
     40 
     41 #include <functional>
     42 #include <stdint.h>
     43 #include <string>
     44 
     45 namespace mozilla {
     46 namespace baseprofiler {
     47 bool LogTest(int aLevelToTest);
     48 void PrintToConsole(const char* aFmt, ...) MOZ_FORMAT_PRINTF(1, 2);
     49 }  // namespace baseprofiler
     50 }  // namespace mozilla
     51 
     52 // These are for MOZ_BASE_PROFILER_LOGGING and above. It's the default logging
     53 // level for the profiler, and should be used sparingly.
     54 #define LOG_TEST ::mozilla::baseprofiler::LogTest(3)
     55 #define LOG(arg, ...)                                                \
     56  do {                                                               \
     57    if (LOG_TEST) {                                                  \
     58      ::mozilla::baseprofiler::PrintToConsole(                       \
     59          "[I %d/%d] " arg "\n",                                     \
     60          int(::mozilla::baseprofiler::profiler_current_process_id() \
     61                  .ToNumber()),                                      \
     62          int(::mozilla::baseprofiler::profiler_current_thread_id()  \
     63                  .ToNumber()),                                      \
     64          ##__VA_ARGS__);                                            \
     65    }                                                                \
     66  } while (0)
     67 
     68 // These are for MOZ_BASE_PROFILER_DEBUG_LOGGING. It should be used for logging
     69 // that is somewhat more verbose than LOG.
     70 #define DEBUG_LOG_TEST ::mozilla::baseprofiler::LogTest(4)
     71 #define DEBUG_LOG(arg, ...)                                          \
     72  do {                                                               \
     73    if (DEBUG_LOG_TEST) {                                            \
     74      ::mozilla::baseprofiler::PrintToConsole(                       \
     75          "[D %d/%d] " arg "\n",                                     \
     76          int(::mozilla::baseprofiler::profiler_current_process_id() \
     77                  .ToNumber()),                                      \
     78          int(::mozilla::baseprofiler::profiler_current_thread_id()  \
     79                  .ToNumber()),                                      \
     80          ##__VA_ARGS__);                                            \
     81    }                                                                \
     82  } while (0)
     83 
     84 // These are for MOZ_BASE_PROFILER_VERBOSE_LOGGING. It should be used for
     85 // logging that is somewhat more verbose than DEBUG_LOG.
     86 #define VERBOSE_LOG_TEST ::mozilla::baseprofiler::LogTest(5)
     87 #define VERBOSE_LOG(arg, ...)                                        \
     88  do {                                                               \
     89    if (VERBOSE_LOG_TEST) {                                          \
     90      ::mozilla::baseprofiler::PrintToConsole(                       \
     91          "[V %d/%d] " arg "\n",                                     \
     92          int(::mozilla::baseprofiler::profiler_current_process_id() \
     93                  .ToNumber()),                                      \
     94          int(::mozilla::baseprofiler::profiler_current_thread_id()  \
     95                  .ToNumber()),                                      \
     96          ##__VA_ARGS__);                                            \
     97    }                                                                \
     98  } while (0)
     99 
    100 namespace mozilla {
    101 
    102 class JSONWriter;
    103 
    104 namespace baseprofiler {
    105 
    106 // If positive, skip stack-sampling in the sampler thread loop.
    107 // Users should increment it atomically when samplings should be avoided, and
    108 // later decrement it back. Multiple uses can overlap.
    109 // There could be a sampling in progress when this is first incremented, so if
    110 // it is critical to prevent any sampling, lock the profiler mutex instead.
    111 // Relaxed ordering, because it's used to request that the profiler pause
    112 // future sampling; this is not time critical, nor dependent on anything else.
    113 extern mozilla::Atomic<int, mozilla::MemoryOrdering::Relaxed> gSkipSampling;
    114 
    115 typedef uint8_t* Address;
    116 
    117 class PlatformData;
    118 
    119 // We can't new/delete the type safely without defining it
    120 // (-Wdelete-incomplete).  Use these to hide the details from clients.
    121 struct PlatformDataDestructor {
    122  void operator()(PlatformData*);
    123 };
    124 
    125 typedef UniquePtr<PlatformData, PlatformDataDestructor> UniquePlatformData;
    126 UniquePlatformData AllocPlatformData(BaseProfilerThreadId aThreadId);
    127 
    128 // Convert the array of strings to a bitfield.
    129 uint32_t ParseFeaturesFromStringArray(const char** aFeatures,
    130                                      uint32_t aFeatureCount,
    131                                      bool aIsStartup = false);
    132 
    133 // Flags to conveniently track various JS instrumentations.
    134 enum class JSInstrumentationFlags {
    135  StackSampling = 0x1,
    136  Allocations = 0x2,
    137 };
    138 
    139 // Record an exit profile from a child process.
    140 void profiler_received_exit_profile(const std::string& aExitProfile);
    141 
    142 // Extract all received exit profiles that have not yet expired (i.e., they
    143 // still intersect with this process' buffer range).
    144 Vector<std::string> profiler_move_exit_profiles();
    145 
    146 }  // namespace baseprofiler
    147 }  // namespace mozilla
    148 
    149 #endif /* ndef TOOLS_PLATFORM_H_ */