Planeshift

crash_generation_server.h

Go to the documentation of this file.
00001 // Copyright (c) 2010 Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 
00030 #ifndef GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
00031 #define GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
00032 
00033 #include <stdint.h>
00034 
00035 #include <string>
00036 
00037 #include "common/mac/MachIPC.h"
00038 
00039 namespace google_breakpad {
00040 
00041 class ClientInfo;
00042 
00043 // Messages the server can read via its mach port
00044 enum {
00045   kDumpRequestMessage     = 1,
00046   kAcknowledgementMessage = 2,
00047   kQuitMessage            = 3
00048 };
00049 
00050 // Exception details sent by the client when requesting a dump.
00051 struct ExceptionInfo {
00052   int32_t exception_type;
00053   int32_t exception_code;
00054   int32_t exception_subcode;
00055 };
00056 
00057 class CrashGenerationServer {
00058  public:
00059   // WARNING: callbacks may be invoked on a different thread
00060   // than that which creates the CrashGenerationServer.  They must
00061   // be thread safe.
00062   typedef void (*OnClientDumpRequestCallback)(void *context,
00063                                               const ClientInfo &client_info,
00064                                               const std::string &file_path);
00065 
00066   typedef void (*OnClientExitingCallback)(void *context,
00067                                           const ClientInfo &client_info);
00068   // If a FilterCallback returns false, the dump will not be written.
00069   typedef bool (*FilterCallback)(void *context);
00070 
00071   // Create an instance with the given parameters.
00072   //
00073   // mach_port_name: Named server port to listen on.
00074   // filter: Callback for a client to cancel writing a dump.
00075   // filter_context: Context for the filter callback.
00076   // dump_callback: Callback for a client crash dump request.
00077   // dump_context: Context for client crash dump request callback.
00078   // exit_callback: Callback for client process exit.
00079   // exit_context: Context for client exit callback.
00080   // generate_dumps: Whether to automatically generate dumps.
00081   //     Client code of this class might want to generate dumps explicitly
00082   //     in the crash dump request callback. In that case, false can be
00083   //     passed for this parameter.
00084   // dump_path: Path for generating dumps; required only if true is
00085   //     passed for generateDumps parameter; NULL can be passed otherwise.
00086   CrashGenerationServer(const char *mach_port_name,
00087                         FilterCallback filter,
00088                         void *filter_context,
00089                         OnClientDumpRequestCallback dump_callback,
00090                         void *dump_context,
00091                         OnClientExitingCallback exit_callback,
00092                         void *exit_context,
00093                         bool generate_dumps,
00094                         const std::string &dump_path);
00095 
00096   ~CrashGenerationServer();
00097 
00098   // Perform initialization steps needed to start listening to clients.
00099   //
00100   // Return true if initialization is successful; false otherwise.
00101   bool Start();
00102 
00103   // Stop the server.
00104   bool Stop();
00105 
00106  private:
00107   // Return a unique filename at which a minidump can be written.
00108   bool MakeMinidumpFilename(std::string &outFilename);
00109 
00110   // Loop reading client messages and responding to them until
00111   // a quit message is received.
00112   static void *WaitForMessages(void *server);
00113 
00114   // Wait for a single client message and respond to it. Returns false
00115   // if a quit message was received or if an error occurred.
00116   bool WaitForOneMessage();
00117 
00118   FilterCallback filter_;
00119   void *filter_context_;
00120 
00121   OnClientDumpRequestCallback dump_callback_;
00122   void *dump_context_;
00123 
00124   OnClientExitingCallback exit_callback_;
00125   void *exit_context_;
00126 
00127   bool generate_dumps_;
00128 
00129   std::string dump_dir_;
00130 
00131   bool started_;
00132 
00133   // The mach port that receives requests to dump from child processes.
00134   ReceivePort receive_port_;
00135 
00136   // The name of the mach port. Stored so the Stop method can message
00137   // the background thread to shut it down.
00138   std::string mach_port_name_;
00139 
00140   // The thread that waits on the receive port.
00141   pthread_t server_thread_;
00142 
00143   // Disable copy constructor and operator=.
00144   CrashGenerationServer(const CrashGenerationServer&);
00145   CrashGenerationServer& operator=(const CrashGenerationServer&);
00146 };
00147 
00148 }  // namespace google_breakpad
00149 
00150 #endif  // GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_