Home | History | Annotate | Download | only in crash_generation
      1 // Copyright (c) 2008, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 #ifndef CLIENT_WINDOWS_CRASH_GENERATION_CRASH_GENERATION_CLIENT_H_
     31 #define CLIENT_WINDOWS_CRASH_GENERATION_CRASH_GENERATION_CLIENT_H_
     32 
     33 #include <windows.h>
     34 #include <dbghelp.h>
     35 #include <string>
     36 #include <utility>
     37 #include "client/windows/common/ipc_protocol.h"
     38 #include "common/scoped_ptr.h"
     39 
     40 namespace google_breakpad {
     41 
     42 struct CustomClientInfo;
     43 
     44 // Abstraction of client-side implementation of out of process
     45 // crash generation.
     46 //
     47 // The process that desires to have out-of-process crash dump
     48 // generation service can use this class in the following way:
     49 //
     50 // * Create an instance.
     51 // * Call Register method so that the client tries to register
     52 //   with the server process and check the return value. If
     53 //   registration is not successful, out-of-process crash dump
     54 //   generation will not be available
     55 // * Request dump generation by calling either of the two
     56 //   overloaded RequestDump methods - one in case of exceptions
     57 //   and the other in case of assertion failures
     58 //
     59 // Note that it is the responsibility of the client code of
     60 // this class to set the unhandled exception filter with the
     61 // system by calling the SetUnhandledExceptionFilter function
     62 // and the client code should explicitly request dump generation.
     63 class CrashGenerationClient {
     64  public:
     65   CrashGenerationClient(const wchar_t* pipe_name,
     66                         MINIDUMP_TYPE dump_type,
     67                         const CustomClientInfo* custom_info);
     68 
     69   CrashGenerationClient(HANDLE pipe_handle,
     70                         MINIDUMP_TYPE dump_type,
     71                         const CustomClientInfo* custom_info);
     72 
     73   ~CrashGenerationClient();
     74 
     75   // Registers the client process with the crash server.
     76   //
     77   // Returns true if the registration is successful; false otherwise.
     78   bool Register();
     79 
     80   // Requests the crash server to upload a previous dump with the
     81   // given crash id.
     82   bool RequestUpload(DWORD crash_id);
     83 
     84   bool RequestDump(EXCEPTION_POINTERS* ex_info,
     85                    MDRawAssertionInfo* assert_info);
     86 
     87   // Requests the crash server to generate a dump with the given
     88   // exception information.
     89   //
     90   // Returns true if the dump was successful; false otherwise. Note that
     91   // if the registration step was not performed or it was not successful,
     92   // false will be returned.
     93   bool RequestDump(EXCEPTION_POINTERS* ex_info);
     94 
     95   // Requests the crash server to generate a dump with the given
     96   // assertion information.
     97   //
     98   // Returns true if the dump was successful; false otherwise. Note that
     99   // if the registration step was not performed or it was not successful,
    100   // false will be returned.
    101   bool RequestDump(MDRawAssertionInfo* assert_info);
    102 
    103   // If the crash generation client is running in a sandbox that prevents it
    104   // from opening the named pipe directly, the server process may open the
    105   // handle and duplicate it into the client process with this helper method.
    106   // Returns INVALID_HANDLE_VALUE on failure. The process must have been opened
    107   // with the PROCESS_DUP_HANDLE access right.
    108   static HANDLE DuplicatePipeToClientProcess(const wchar_t* pipe_name,
    109                                              HANDLE hProcess);
    110 
    111  private:
    112   // Connects to the appropriate pipe and sets the pipe handle state.
    113   //
    114   // Returns the pipe handle if everything goes well; otherwise Returns NULL.
    115   HANDLE ConnectToServer();
    116 
    117   // Performs a handshake with the server over the given pipe which should be
    118   // already connected to the server.
    119   //
    120   // Returns true if handshake with the server was successful; false otherwise.
    121   bool RegisterClient(HANDLE pipe);
    122 
    123   // Validates the given server response.
    124   bool ValidateResponse(const ProtocolMessage& msg) const;
    125 
    126   // Returns true if the registration step succeeded; false otherwise.
    127   bool IsRegistered() const;
    128 
    129   // Connects to the given named pipe with given parameters.
    130   //
    131   // Returns true if the connection is successful; false otherwise.
    132   HANDLE ConnectToPipe(const wchar_t* pipe_name,
    133                        DWORD pipe_access,
    134                        DWORD flags_attrs);
    135 
    136   // Signals the crash event and wait for the server to generate crash.
    137   bool SignalCrashEventAndWait();
    138 
    139   // Pipe name to use to talk to server.
    140   std::wstring pipe_name_;
    141 
    142   // Pipe handle duplicated from server process. Only valid before
    143   // Register is called.
    144   HANDLE pipe_handle_;
    145 
    146   // Custom client information
    147   CustomClientInfo custom_info_;
    148 
    149   // Type of dump to generate.
    150   MINIDUMP_TYPE dump_type_;
    151 
    152   // Event to signal in case of a crash.
    153   HANDLE crash_event_;
    154 
    155   // Handle to wait on after signaling a crash for the server
    156   // to finish generating crash dump.
    157   HANDLE crash_generated_;
    158 
    159   // Handle to a mutex that will become signaled with WAIT_ABANDONED
    160   // if the server process goes down.
    161   HANDLE server_alive_;
    162 
    163   // Server process id.
    164   DWORD server_process_id_;
    165 
    166   // Id of the thread that caused the crash.
    167   DWORD thread_id_;
    168 
    169   // Exception pointers for an exception crash.
    170   EXCEPTION_POINTERS* exception_pointers_;
    171 
    172   // Assertion info for an invalid parameter or pure call crash.
    173   MDRawAssertionInfo assert_info_;
    174 
    175   // Disable copy ctor and operator=.
    176   CrashGenerationClient(const CrashGenerationClient& crash_client);
    177   CrashGenerationClient& operator=(const CrashGenerationClient& crash_client);
    178 };
    179 
    180 }  // namespace google_breakpad
    181 
    182 #endif  // CLIENT_WINDOWS_CRASH_GENERATION_CRASH_GENERATION_CLIENT_H_
    183