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.

Header

Mercurial (d38398e5144e)

VCS Links

AsyncMinidumpAnalyzer

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
/* -*- 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/. */

#include "CrashReporterHost.h"
#include "CrashReporterMetadataShmem.h"
#include "mozilla/LazyIdleThread.h"
#include "mozilla/Sprintf.h"
#include "mozilla/SyncRunnable.h"
#include "mozilla/Telemetry.h"
#ifdef MOZ_CRASHREPORTER
# include "nsIAsyncShutdown.h"
# include "nsICrashService.h"
#endif

namespace mozilla {
namespace ipc {

CrashReporterHost::CrashReporterHost(GeckoProcessType aProcessType,
                                     const Shmem& aShmem,
                                     CrashReporter::ThreadId aThreadId)
 : mProcessType(aProcessType),
   mShmem(aShmem),
   mThreadId(aThreadId),
   mStartTime(::time(nullptr)),
   mFinalized(false)
{
}

#ifdef MOZ_CRASHREPORTER
bool
CrashReporterHost::GenerateCrashReport(base::ProcessId aPid)
{
  if (!TakeCrashedChildMinidump(aPid, nullptr)) {
    return false;
  }
  return FinalizeCrashReport();
}

RefPtr<nsIFile>
CrashReporterHost::TakeCrashedChildMinidump(base::ProcessId aPid, uint32_t* aOutSequence)
{
  MOZ_ASSERT(!HasMinidump());

  RefPtr<nsIFile> crashDump;
  if (!XRE_TakeMinidumpForChild(aPid, getter_AddRefs(crashDump), aOutSequence)) {
    return nullptr;
  }
  if (!AdoptMinidump(crashDump)) {
    return nullptr;
  }
  return crashDump.get();
}

bool
CrashReporterHost::AdoptMinidump(nsIFile* aFile)
{
  return CrashReporter::GetIDFromMinidump(aFile, mDumpID);
}

bool
CrashReporterHost::FinalizeCrashReport()
{
  MOZ_ASSERT(!mFinalized);
  MOZ_ASSERT(HasMinidump());

  CrashReporter::AnnotationTable notes;

  nsAutoCString type;
  switch (mProcessType) {
    case GeckoProcessType_Content:
      type = NS_LITERAL_CSTRING("content");
      break;
    case GeckoProcessType_Plugin:
    case GeckoProcessType_GMPlugin:
      type = NS_LITERAL_CSTRING("plugin");
      break;
    case GeckoProcessType_GPU:
      type = NS_LITERAL_CSTRING("gpu");
      break;
    default:
      NS_ERROR("unknown process type");
      break;
  }
  notes.Put(NS_LITERAL_CSTRING("ProcessType"), type);

  char startTime[32];
  SprintfLiteral(startTime, "%lld", static_cast<long long>(mStartTime));
  notes.Put(NS_LITERAL_CSTRING("StartupTime"), nsDependentCString(startTime));

  // We might not have shmem (for example, when running crashreporter tests).
  if (mShmem.IsReadable()) {
    CrashReporterMetadataShmem::ReadAppNotes(mShmem, &notes);
  }
  CrashReporter::AppendExtraData(mDumpID, mExtraNotes);
  CrashReporter::AppendExtraData(mDumpID, notes);

  // Use mExtraNotes, since NotifyCrashService looks for "PluginHang" which is
  // set in the parent process.
  NotifyCrashService(mProcessType, mDumpID, &mExtraNotes);

  mFinalized = true;
  return true;
}

/**
 * Runnable used to execute the minidump analyzer program asynchronously after
 * a crash. This should run on a background thread not to block the main thread
 * over the potentially long minidump analysis. Once analysis is over, the
 * crash information will be relayed to the crash manager via another runnable
 * sent to the main thread. Shutdown will be blocked for the duration of the
 * entire process to ensure this information is sent.
 */
class AsyncMinidumpAnalyzer final : public nsIRunnable
                                  , public nsIAsyncShutdownBlocker
{
public:
  /**
   * Create a new minidump analyzer runnable, this will also block shutdown
   * until the associated crash has been added to the crash manager.
   */
  AsyncMinidumpAnalyzer(int32_t aProcessType,
                        int32_t aCrashType,
                        const nsString& aChildDumpID)
    : mProcessType(aProcessType)
    , mCrashType(aCrashType)
    , mChildDumpID(aChildDumpID)
    , mName(NS_LITERAL_STRING("Crash reporter: blocking on minidump analysis"))
  {
    MOZ_ASSERT(NS_IsMainThread());

    nsresult rv = GetShutdownBarrier()->AddBlocker(
      this, NS_LITERAL_STRING(__FILE__), __LINE__,
      NS_LITERAL_STRING("Minidump analysis"));
    Unused << NS_WARN_IF(NS_FAILED(rv));
  }

  NS_IMETHOD Run() override
  {
    MOZ_ASSERT(!NS_IsMainThread());

    if (mProcessType == nsICrashService::PROCESS_TYPE_CONTENT) {
      CrashReporter::RunMinidumpAnalyzer(mChildDumpID);
    }

    // Make a copy of these so we can copy them into the runnable lambda
    int32_t processType = mProcessType;
    int32_t crashType = mCrashType;
    nsString childDumpID(mChildDumpID);
    nsCOMPtr<nsIAsyncShutdownBlocker> self(this);

    NS_DispatchToMainThread(NS_NewRunnableFunction([
      self, processType, crashType, childDumpID
    ] {
      nsCOMPtr<nsICrashService> crashService =
        do_GetService("@mozilla.org/crashservice;1");
      if (crashService) {
        crashService->AddCrash(processType, crashType, childDumpID);
      }

      nsCOMPtr<nsIAsyncShutdownClient> barrier = GetShutdownBarrier();

      if (barrier) {
        barrier->RemoveBlocker(self);
      }
    }));

    return NS_OK;
  }

  NS_IMETHOD BlockShutdown(nsIAsyncShutdownClient* aBarrierClient) override
  {
    return NS_OK;
  }

  NS_IMETHOD GetName(nsAString& aName) override
  {
    aName = mName;
    return NS_OK;
  }

  NS_IMETHOD GetState(nsIPropertyBag**) override
  {
    return NS_OK;
  }

  NS_DECL_THREADSAFE_ISUPPORTS

private:
  ~AsyncMinidumpAnalyzer() {}

  // Returns the profile-before-change shutdown barrier
  static nsCOMPtr<nsIAsyncShutdownClient> GetShutdownBarrier()
  {
    MOZ_ASSERT(NS_IsMainThread());

    nsCOMPtr<nsIAsyncShutdownService> svc = services::GetAsyncShutdown();
    nsCOMPtr<nsIAsyncShutdownClient> barrier;
    nsresult rv = svc->GetProfileBeforeChange(getter_AddRefs(barrier));

    if (NS_WARN_IF(NS_FAILED(rv))) {
      return nullptr;
    }

    return barrier.forget();
  }

  int32_t mProcessType;
  int32_t mCrashType;
  const nsString mChildDumpID;
  const nsString mName;
};

NS_IMPL_ISUPPORTS(AsyncMinidumpAnalyzer, nsIRunnable, nsIAsyncShutdownBlocker)

/**
 * Add information about a crash to the crash manager. This method runs the
 * various activities required to gather additional information and notify the
 * crash manager asynchronously, since many of them involve I/O or potentially
 * long processing.
 *
 * @param aProcessType The type of process that crashed
 * @param aCrashType The type of crash (crash or hang)
 * @param aChildDumpID A string holding the ID of the dump associated with this
 *        crash
 */
/* static */ void
CrashReporterHost::AsyncAddCrash(int32_t aProcessType,
                                 int32_t aCrashType,
                                 const nsString& aChildDumpID)
{
  MOZ_ASSERT(NS_IsMainThread());

  static StaticRefPtr<LazyIdleThread> sBackgroundThread;

  if (!sBackgroundThread) {
    // Background thread used for running minidump analysis. It will be
    // destroyed immediately after it's done with the task.
    sBackgroundThread =
      new LazyIdleThread(0, NS_LITERAL_CSTRING("CrashReporterHost"));
    ClearOnShutdown(&sBackgroundThread);
  }

  RefPtr<AsyncMinidumpAnalyzer> task =
    new AsyncMinidumpAnalyzer(aProcessType, aCrashType, aChildDumpID);

  Unused << sBackgroundThread->Dispatch(task, NS_DISPATCH_NORMAL);
}

/* static */ void
CrashReporterHost::NotifyCrashService(GeckoProcessType aProcessType,
                                      const nsString& aChildDumpID,
                                      const AnnotationTable* aNotes)
{
  if (!NS_IsMainThread()) {
    RefPtr<Runnable> runnable = NS_NewRunnableFunction([=] () -> void {
      CrashReporterHost::NotifyCrashService(aProcessType, aChildDumpID, aNotes);
    });
    RefPtr<nsIThread> mainThread = do_GetMainThread();
    SyncRunnable::DispatchToThread(mainThread, runnable);
    return;
  }

  MOZ_ASSERT(!aChildDumpID.IsEmpty());

  int32_t processType;
  int32_t crashType = nsICrashService::CRASH_TYPE_CRASH;

  nsCString telemetryKey;

  switch (aProcessType) {
    case GeckoProcessType_Content:
      processType = nsICrashService::PROCESS_TYPE_CONTENT;
      telemetryKey.AssignLiteral("content");
      break;
    case GeckoProcessType_Plugin: {
      processType = nsICrashService::PROCESS_TYPE_PLUGIN;
      telemetryKey.AssignLiteral("plugin");
      nsAutoCString val;
      if (aNotes->Get(NS_LITERAL_CSTRING("PluginHang"), &val) &&
        val.Equals(NS_LITERAL_CSTRING("1"))) {
        crashType = nsICrashService::CRASH_TYPE_HANG;
        telemetryKey.AssignLiteral("pluginhang");
      }
      break;
    }
    case GeckoProcessType_GMPlugin:
      processType = nsICrashService::PROCESS_TYPE_GMPLUGIN;
      telemetryKey.AssignLiteral("gmplugin");
      break;
    case GeckoProcessType_GPU:
      processType = nsICrashService::PROCESS_TYPE_GPU;
      telemetryKey.AssignLiteral("gpu");
      break;
    default:
      NS_ERROR("unknown process type");
      return;
  }

  AsyncAddCrash(processType, crashType, aChildDumpID);
  Telemetry::Accumulate(Telemetry::SUBPROCESS_CRASHES_WITH_DUMP, telemetryKey, 1);
}

void
CrashReporterHost::AddNote(const nsCString& aKey, const nsCString& aValue)
{
  mExtraNotes.Put(aKey, aValue);
}
#endif

} // namespace ipc
} // namespace mozilla