1 // Copyright (c) 2010 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 GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
31 #define GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
32 
33 #include <stdint.h>
34 
35 #include <string>
36 
37 #include "common/mac/MachIPC.h"
38 
39 namespace google_breakpad {
40 
41 class ClientInfo;
42 
43 // Messages the server can read via its mach port
44 enum {
45   kDumpRequestMessage     = 1,
46   kAcknowledgementMessage = 2,
47   kQuitMessage            = 3
48 };
49 
50 // Exception details sent by the client when requesting a dump.
51 struct ExceptionInfo {
52   int32_t exception_type;
53   int32_t exception_code;
54   int32_t exception_subcode;
55 };
56 
57 class CrashGenerationServer {
58  public:
59   // WARNING: callbacks may be invoked on a different thread
60   // than that which creates the CrashGenerationServer.  They must
61   // be thread safe.
62   typedef void (*OnClientDumpRequestCallback)(void *context,
63                                               const ClientInfo &client_info,
64                                               const std::string &file_path);
65 
66   typedef void (*OnClientExitingCallback)(void *context,
67                                           const ClientInfo &client_info);
68 
69   // Create an instance with the given parameters.
70   //
71   // mach_port_name: Named server port to listen on.
72   // dump_callback: Callback for a client crash dump request.
73   // dump_context: Context for client crash dump request callback.
74   // exit_callback: Callback for client process exit.
75   // exit_context: Context for client exit callback.
76   // generate_dumps: Whether to automatically generate dumps.
77   //     Client code of this class might want to generate dumps explicitly
78   //     in the crash dump request callback. In that case, false can be
79   //     passed for this parameter.
80   // dump_path: Path for generating dumps; required only if true is
81   //     passed for generateDumps parameter; NULL can be passed otherwise.
82   CrashGenerationServer(const char *mach_port_name,
83                         OnClientDumpRequestCallback dump_callback,
84                         void *dump_context,
85                         OnClientExitingCallback exit_callback,
86                         void *exit_context,
87                         bool generate_dumps,
88                         const std::string &dump_path);
89 
90   ~CrashGenerationServer();
91 
92   // Perform initialization steps needed to start listening to clients.
93   //
94   // Return true if initialization is successful; false otherwise.
95   bool Start();
96 
97   // Stop the server.
98   bool Stop();
99 
100  private:
101   // Return a unique filename at which a minidump can be written.
102   bool MakeMinidumpFilename(std::string &outFilename);
103 
104   // Loop reading client messages and responding to them until
105   // a quit message is received.
106   static void *WaitForMessages(void *server);
107 
108   // Wait for a single client message and respond to it. Returns false
109   // if a quit message was received or if an error occurred.
110   bool WaitForOneMessage();
111 
112   OnClientDumpRequestCallback dump_callback_;
113   void *dump_context_;
114 
115   OnClientExitingCallback exit_callback_;
116   void *exit_context_;
117 
118   bool generate_dumps_;
119 
120   std::string dump_dir_;
121 
122   bool started_;
123 
124   // The mach port that receives requests to dump from child processes.
125   ReceivePort receive_port_;
126 
127   // The name of the mach port. Stored so the Stop method can message
128   // the background thread to shut it down.
129   std::string mach_port_name_;
130 
131   // The thread that waits on the receive port.
132   pthread_t server_thread_;
133 
134   // Disable copy constructor and operator=.
135   CrashGenerationServer(const CrashGenerationServer&);
136   CrashGenerationServer& operator=(const CrashGenerationServer&);
137 };
138 
139 }  // namespace google_breakpad
140 
141 #endif  // GOOGLE_BREAKPAD_CLIENT_MAC_CRASH_GENERATION_CRASH_GENERATION_SERVER_H_
142