toolkit/crashreporter/InjectCrashReporter.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/crashreporter/InjectCrashReporter.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,84 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "InjectCrashReporter.h"
    1.10 +#include "nsDirectoryServiceUtils.h"
    1.11 +#include "nsDirectoryServiceDefs.h"
    1.12 +#include "client/windows/crash_generation/crash_generation_client.h"
    1.13 +#include "nsExceptionHandler.h"
    1.14 +#include "LoadLibraryRemote.h"
    1.15 +#include "nsWindowsHelpers.h"
    1.16 +
    1.17 +using google_breakpad::CrashGenerationClient;
    1.18 +using CrashReporter::GetChildNotificationPipe;
    1.19 +
    1.20 +namespace mozilla {
    1.21 +
    1.22 +InjectCrashRunnable::InjectCrashRunnable(DWORD pid)
    1.23 +  : mPID(pid)
    1.24 +{
    1.25 +  nsCOMPtr<nsIFile> dll;
    1.26 +  nsresult rv = NS_GetSpecialDirectory(NS_GRE_DIR, getter_AddRefs(dll));
    1.27 +  if (NS_SUCCEEDED(rv)) {
    1.28 +    dll->Append(NS_LITERAL_STRING("breakpadinjector.dll"));
    1.29 +    dll->GetPath(mInjectorPath);
    1.30 +  }
    1.31 +}
    1.32 +
    1.33 +NS_IMETHODIMP
    1.34 +InjectCrashRunnable::Run()
    1.35 +{
    1.36 +  if (mInjectorPath.IsEmpty())
    1.37 +    return NS_OK;
    1.38 +
    1.39 +  nsAutoHandle hProcess(
    1.40 +    OpenProcess(PROCESS_CREATE_THREAD |
    1.41 +                PROCESS_QUERY_INFORMATION |
    1.42 +                PROCESS_DUP_HANDLE |
    1.43 +                PROCESS_VM_OPERATION |
    1.44 +                PROCESS_VM_WRITE |
    1.45 +                PROCESS_VM_READ, FALSE, mPID));
    1.46 +  if (!hProcess) {
    1.47 +    NS_WARNING("Unable to open remote process handle for crashreporter injection.");
    1.48 +    return NS_OK;
    1.49 +  }
    1.50 +
    1.51 +  void* proc = LoadRemoteLibraryAndGetAddress(hProcess, mInjectorPath.get(),
    1.52 +                                              "Start");
    1.53 +  if (!proc) {
    1.54 +    NS_WARNING("Unable to inject crashreporter DLL.");
    1.55 +    return NS_OK;
    1.56 +  }
    1.57 +
    1.58 +  HANDLE hRemotePipe =
    1.59 +    CrashGenerationClient::DuplicatePipeToClientProcess(
    1.60 +      NS_ConvertASCIItoUTF16(GetChildNotificationPipe()).get(),
    1.61 +      hProcess);
    1.62 +  if (INVALID_HANDLE_VALUE == hRemotePipe) {
    1.63 +    NS_WARNING("Unable to duplicate crash reporter pipe to process.");
    1.64 +    return NS_OK;
    1.65 +  }
    1.66 +
    1.67 +  nsAutoHandle hThread(CreateRemoteThread(hProcess, nullptr, 0,
    1.68 +                                          (LPTHREAD_START_ROUTINE) proc,
    1.69 +                                          (void*) hRemotePipe, 0, nullptr));
    1.70 +  if (!hThread) {
    1.71 +    NS_WARNING("Unable to CreateRemoteThread");
    1.72 +
    1.73 +    // We have to close the remote pipe or else our crash generation client
    1.74 +    // will be stuck unable to accept other remote requests.
    1.75 +    HANDLE toClose = INVALID_HANDLE_VALUE;
    1.76 +    if (DuplicateHandle(hProcess, hRemotePipe, ::GetCurrentProcess(),
    1.77 +                        &toClose, 0, FALSE,
    1.78 +                        DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS)) {
    1.79 +      CloseHandle(toClose);
    1.80 +      return NS_OK;
    1.81 +    }
    1.82 +  }
    1.83 +
    1.84 +  return NS_OK;
    1.85 +}
    1.86 +
    1.87 +} // namespace mozilla

mercurial