DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Mercurial (cdf352f02ac4)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef mozilla_dom_indexeddb_profilerhelpers_h__
#define mozilla_dom_indexeddb_profilerhelpers_h__

// This file is not exported and is only meant to be included in IndexedDB
// source files.

#include "BackgroundChildImpl.h"
#include "GeckoProfiler.h"
#include "IDBCursorType.h"
#include "IDBDatabase.h"
#include "IDBIndex.h"
#include "IDBKeyRange.h"
#include "IDBObjectStore.h"
#include "IDBTransaction.h"
#include "IndexedDatabaseManager.h"
#include "Key.h"
#include "mozilla/Assertions.h"
#include "mozilla/Attributes.h"
#include "mozilla/dom/BindingDeclarations.h"
#include "mozilla/dom/Event.h"
#include "nsDebug.h"
#include "nsID.h"
#include "nsString.h"
#include "mozilla/Logging.h"

// Include this last to avoid path problems on Windows.
#include "ActorsChild.h"

namespace mozilla {
namespace dom {
namespace indexedDB {

class MOZ_STACK_CLASS LoggingIdString final
    : public nsAutoCStringN<NSID_LENGTH> {
 public:
  LoggingIdString() {
    using mozilla::ipc::BackgroundChildImpl;

    if (IndexedDatabaseManager::GetLoggingMode() !=
        IndexedDatabaseManager::Logging_Disabled) {
      const BackgroundChildImpl::ThreadLocal* threadLocal =
          BackgroundChildImpl::GetThreadLocalForCurrentThread();
      if (threadLocal) {
        const auto& idbThreadLocal = threadLocal->mIndexedDBThreadLocal;
        if (idbThreadLocal) {
          Assign(idbThreadLocal->IdString());
        }
      }
    }
  }

  explicit LoggingIdString(const nsID& aID) {
    static_assert(NSID_LENGTH > 1, "NSID_LENGTH is set incorrectly!");
    static_assert(NSID_LENGTH <= kStorageSize,
                  "nsID string won't fit in our storage!");
    // Capacity() excludes the null terminator; NSID_LENGTH includes it.
    MOZ_ASSERT(Capacity() + 1 == NSID_LENGTH);

    if (IndexedDatabaseManager::GetLoggingMode() !=
        IndexedDatabaseManager::Logging_Disabled) {
      // NSID_LENGTH counts the null terminator, SetLength() does not.
      SetLength(NSID_LENGTH - 1);

      aID.ToProvidedString(
          *reinterpret_cast<char(*)[NSID_LENGTH]>(BeginWriting()));
    }
  }
};

class MOZ_STACK_CLASS LoggingString final : public nsAutoCString {
  static const char kQuote = '\"';
  static const char kOpenBracket = '[';
  static const char kCloseBracket = ']';
  static const char kOpenParen = '(';
  static const char kCloseParen = ')';

 public:
  explicit LoggingString(IDBDatabase* aDatabase) : nsAutoCString(kQuote) {
    MOZ_ASSERT(aDatabase);

    AppendUTF16toUTF8(aDatabase->Name(), *this);
    Append(kQuote);
  }

  explicit LoggingString(IDBTransaction* aTransaction)
      : nsAutoCString(kOpenBracket) {
    MOZ_ASSERT(aTransaction);

    NS_NAMED_LITERAL_CSTRING(kCommaSpace, ", ");

    const nsTArray<nsString>& stores = aTransaction->ObjectStoreNamesInternal();

    for (uint32_t count = stores.Length(), index = 0; index < count; index++) {
      Append(kQuote);
      AppendUTF16toUTF8(stores[index], *this);
      Append(kQuote);

      if (index != count - 1) {
        Append(kCommaSpace);
      }
    }

    Append(kCloseBracket);
    Append(kCommaSpace);

    switch (aTransaction->GetMode()) {
      case IDBTransaction::Mode::ReadOnly:
        AppendLiteral("\"readonly\"");
        break;
      case IDBTransaction::Mode::ReadWrite:
        AppendLiteral("\"readwrite\"");
        break;
      case IDBTransaction::Mode::ReadWriteFlush:
        AppendLiteral("\"readwriteflush\"");
        break;
      case IDBTransaction::Mode::Cleanup:
        AppendLiteral("\"cleanup\"");
        break;
      case IDBTransaction::Mode::VersionChange:
        AppendLiteral("\"versionchange\"");
        break;
      default:
        MOZ_CRASH("Unknown mode!");
    };
  }

  explicit LoggingString(IDBObjectStore* aObjectStore) : nsAutoCString(kQuote) {
    MOZ_ASSERT(aObjectStore);

    AppendUTF16toUTF8(aObjectStore->Name(), *this);
    Append(kQuote);
  }

  explicit LoggingString(IDBIndex* aIndex) : nsAutoCString(kQuote) {
    MOZ_ASSERT(aIndex);

    AppendUTF16toUTF8(aIndex->Name(), *this);
    Append(kQuote);
  }

  explicit LoggingString(IDBKeyRange* aKeyRange) {
    if (aKeyRange) {
      if (aKeyRange->IsOnly()) {
        Assign(LoggingString(aKeyRange->Lower()));
      } else {
        if (aKeyRange->LowerOpen()) {
          Assign(kOpenParen);
        } else {
          Assign(kOpenBracket);
        }

        Append(LoggingString(aKeyRange->Lower()));
        AppendLiteral(", ");
        Append(LoggingString(aKeyRange->Upper()));

        if (aKeyRange->UpperOpen()) {
          Append(kCloseParen);
        } else {
          Append(kCloseBracket);
        }
      }
    } else {
      AssignLiteral("<undefined>");
    }
  }

  explicit LoggingString(const Key& aKey) {
    if (aKey.IsUnset()) {
      AssignLiteral("<undefined>");
    } else if (aKey.IsFloat()) {
      AppendPrintf("%g", aKey.ToFloat());
    } else if (aKey.IsDate()) {
      AppendPrintf("<Date %g>", aKey.ToDateMsec());
    } else if (aKey.IsString()) {
      nsAutoString str;
      aKey.ToString(str);
      AppendPrintf("\"%s\"", NS_ConvertUTF16toUTF8(str).get());
    } else if (aKey.IsBinary()) {
      AssignLiteral("[object ArrayBuffer]");
    } else {
      MOZ_ASSERT(aKey.IsArray());
      AssignLiteral("[...]");
    }
  }

  explicit LoggingString(const IDBCursorDirection aDirection) {
    switch (aDirection) {
      case IDBCursorDirection::Next:
        AssignLiteral("\"next\"");
        break;
      case IDBCursorDirection::Nextunique:
        AssignLiteral("\"nextunique\"");
        break;
      case IDBCursorDirection::Prev:
        AssignLiteral("\"prev\"");
        break;
      case IDBCursorDirection::Prevunique:
        AssignLiteral("\"prevunique\"");
        break;
      default:
        MOZ_CRASH("Unknown direction!");
    };
  }

  explicit LoggingString(const Optional<uint64_t>& aVersion) {
    if (aVersion.WasPassed()) {
      AppendInt(aVersion.Value());
    } else {
      AssignLiteral("<undefined>");
    }
  }

  explicit LoggingString(const Optional<uint32_t>& aLimit) {
    if (aLimit.WasPassed()) {
      AppendInt(aLimit.Value());
    } else {
      AssignLiteral("<undefined>");
    }
  }

  LoggingString(IDBObjectStore* aObjectStore, const Key& aKey) {
    MOZ_ASSERT(aObjectStore);

    if (!aObjectStore->HasValidKeyPath()) {
      Append(LoggingString(aKey));
    }
  }

  LoggingString(Event* aEvent, const char16_t* aDefault)
      : nsAutoCString(kQuote) {
    MOZ_ASSERT(aDefault);

    nsAutoString eventType;

    if (aEvent) {
      aEvent->GetType(eventType);
    } else {
      eventType = nsDependentString(aDefault);
    }

    AppendUTF16toUTF8(eventType, *this);
    Append(kQuote);
  }
};

inline void MOZ_FORMAT_PRINTF(2, 3)
    LoggingHelper(bool aUseProfiler, const char* aFmt, ...) {
  MOZ_ASSERT(IndexedDatabaseManager::GetLoggingMode() !=
             IndexedDatabaseManager::Logging_Disabled);
  MOZ_ASSERT(aFmt);

  mozilla::LogModule* logModule = IndexedDatabaseManager::GetLoggingModule();
  MOZ_ASSERT(logModule);

  static const mozilla::LogLevel logLevel = LogLevel::Warning;

  if (MOZ_LOG_TEST(logModule, logLevel) ||
#ifdef MOZ_GECKO_PROFILER
      (aUseProfiler && profiler_thread_is_being_profiled())
#else
      false
#endif
  ) {
    nsAutoCString message;

    {
      va_list args;
      va_start(args, aFmt);

      message.AppendPrintf(aFmt, args);

      va_end(args);
    }

    MOZ_LOG(logModule, logLevel, ("%s", message.get()));

    if (aUseProfiler) {
      PROFILER_ADD_MARKER(message.get(), DOM);
    }
  }
}

}  // namespace indexedDB
}  // namespace dom
}  // namespace mozilla

#define IDB_LOG_MARK2(_detailedFmt, _conciseFmt, ...)                          \
  do {                                                                         \
    using namespace mozilla::dom::indexedDB;                                   \
                                                                               \
    const IndexedDatabaseManager::LoggingMode mode =                           \
        IndexedDatabaseManager::GetLoggingMode();                              \
                                                                               \
    if (mode != IndexedDatabaseManager::Logging_Disabled) {                    \
      const char* _fmt;                                                        \
      if (mode == IndexedDatabaseManager::Logging_Concise ||                   \
          mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks) {      \
        _fmt = _conciseFmt;                                                    \
      } else {                                                                 \
        MOZ_ASSERT(mode == IndexedDatabaseManager::Logging_Detailed ||         \
                   mode ==                                                     \
                       IndexedDatabaseManager::Logging_DetailedProfilerMarks); \
        _fmt = _detailedFmt;                                                   \
      }                                                                        \
                                                                               \
      const bool _useProfiler =                                                \
          mode == IndexedDatabaseManager::Logging_ConciseProfilerMarks ||      \
          mode == IndexedDatabaseManager::Logging_DetailedProfilerMarks;       \
                                                                               \
      LoggingHelper(_useProfiler, _fmt, ##__VA_ARGS__);                        \
    }                                                                          \
  } while (0)

#define IDB_LOG_MARK(_detailedFmt, _conciseFmt, _loggingId, ...)             \
  IDB_LOG_MARK2("IndexedDB %s: " _detailedFmt, "IndexedDB %s: " _conciseFmt, \
                _loggingId, ##__VA_ARGS__)

#define IDB_LOG_ID_STRING(...) \
  mozilla::dom::indexedDB::LoggingIdString(__VA_ARGS__).get()

#define IDB_LOG_STRINGIFY(...) \
  mozilla::dom::indexedDB::LoggingString(__VA_ARGS__).get()

// IDB_LOG_MARK_DETAILED_PARENT and IDB_LOG_MARK_DETAILED_CHILD should have the
// same width.
#define IDB_LOG_MARK_DETAILED_PARENT "Parent"
#define IDB_LOG_MARK_DETAILED_CHILD "Child "

#define IDB_LOG_MARK_CONCISE_PARENT "P"
#define IDB_LOG_MARK_CONCISE_CHILD "C"

#define IDB_LOG_MARK_DETAILED_TRANSACTION "Transaction[%lld]"
#define IDB_LOG_MARK_DETAILED_REQUEST "Request[%llu]"

#define IDB_LOG_MARK_CONCISE_TRANSACTION "T[%lld]"
#define IDB_LOG_MARK_CONCISE_REQUEST "R[%llu]"

#define IDB_LOG_MARK_TRANSACTION_REQUEST(                                      \
    _detailedPeer, _concisePeer, _detailedFmt, _conciseFmt, _loggingId,        \
    _transactionSerialNumber, _requestSerialNumber, ...)                       \
  IDB_LOG_MARK(_detailedPeer " " IDB_LOG_MARK_DETAILED_TRANSACTION             \
                             " " IDB_LOG_MARK_DETAILED_REQUEST                 \
                             ": " _detailedFmt,                                \
               _concisePeer " " IDB_LOG_MARK_CONCISE_TRANSACTION               \
                            " " IDB_LOG_MARK_CONCISE_REQUEST ": " _conciseFmt, \
               _loggingId, _transactionSerialNumber, _requestSerialNumber,     \
               ##__VA_ARGS__)

#define IDB_LOG_MARK_PARENT_TRANSACTION_REQUEST(                               \
    _detailedFmt, _conciseFmt, _loggingId, _transactionSerialNumber,           \
    _requestSerialNumber, ...)                                                 \
  IDB_LOG_MARK_TRANSACTION_REQUEST(                                            \
      IDB_LOG_MARK_DETAILED_PARENT, IDB_LOG_MARK_CONCISE_PARENT, _detailedFmt, \
      _conciseFmt, _loggingId, _transactionSerialNumber, _requestSerialNumber, \
      ##__VA_ARGS__)

#define IDB_LOG_MARK_CHILD_TRANSACTION_REQUEST(_detailedFmt, _conciseFmt,    \
                                               _transactionSerialNumber,     \
                                               _requestSerialNumber, ...)    \
  IDB_LOG_MARK_TRANSACTION_REQUEST(                                          \
      IDB_LOG_MARK_DETAILED_CHILD, IDB_LOG_MARK_CONCISE_CHILD, _detailedFmt, \
      _conciseFmt, IDB_LOG_ID_STRING(), _transactionSerialNumber,            \
      _requestSerialNumber, ##__VA_ARGS__)

#define IDB_LOG_MARK_CHILD_REQUEST(_detailedFmt, _conciseFmt,                \
                                   _requestSerialNumber, ...)                \
  IDB_LOG_MARK(IDB_LOG_MARK_DETAILED_CHILD " " IDB_LOG_MARK_DETAILED_REQUEST \
                                           ": " _detailedFmt,                \
               IDB_LOG_MARK_CONCISE_CHILD " " IDB_LOG_MARK_CONCISE_REQUEST   \
                                          ": " _conciseFmt,                  \
               IDB_LOG_ID_STRING(), _requestSerialNumber, ##__VA_ARGS__)

#define IDB_LOG_MARK_TRANSACTION(_detailedPeer, _concisePeer, _detailedFmt,  \
                                 _conciseFmt, _loggingId,                    \
                                 _transactionSerialNumber, ...)              \
  IDB_LOG_MARK(                                                              \
      _detailedPeer " " IDB_LOG_MARK_DETAILED_TRANSACTION ": " _detailedFmt, \
      _concisePeer " " IDB_LOG_MARK_CONCISE_TRANSACTION ": " _conciseFmt,    \
      _loggingId, _transactionSerialNumber, ##__VA_ARGS__)

#define IDB_LOG_MARK_PARENT_TRANSACTION(_detailedFmt, _conciseFmt, _loggingId, \
                                        _transactionSerialNumber, ...)         \
  IDB_LOG_MARK_TRANSACTION(                                                    \
      IDB_LOG_MARK_DETAILED_PARENT, IDB_LOG_MARK_CONCISE_PARENT, _detailedFmt, \
      _conciseFmt, _loggingId, _transactionSerialNumber, ##__VA_ARGS__)

#define IDB_LOG_MARK_CHILD_TRANSACTION(_detailedFmt, _conciseFmt,     \
                                       _transactionSerialNumber, ...) \
  IDB_LOG_MARK_TRANSACTION(IDB_LOG_MARK_DETAILED_CHILD,               \
                           IDB_LOG_MARK_CONCISE_CHILD, _detailedFmt,  \
                           _conciseFmt, IDB_LOG_ID_STRING(),          \
                           _transactionSerialNumber, ##__VA_ARGS__)

#endif  // mozilla_dom_indexeddb_profilerhelpers_h__