1 // Copyright 2010 Google LLC
2 //
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
5 // met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
12 // distribution.
13 // * Neither the name of Google LLC nor the names of its
14 // contributors may be used to endorse or promote products derived from
15 // this software without specific prior written permission.
16 //
17 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #ifdef HAVE_CONFIG_H
30 #include <config.h> // Must come first
31 #endif
32
33 #include "client/mac/crash_generation/crash_generation_server.h"
34
35 #include <pthread.h>
36
37 #include "client/mac/crash_generation/client_info.h"
38 #include "client/mac/handler/minidump_generator.h"
39 #include "common/mac/scoped_task_suspend-inl.h"
40
41 namespace google_breakpad {
42
CrashGenerationServer(const char * mach_port_name,FilterCallback filter,void * filter_context,OnClientDumpRequestCallback dump_callback,void * dump_context,OnClientExitingCallback exit_callback,void * exit_context,bool generate_dumps,const std::string & dump_path)43 CrashGenerationServer::CrashGenerationServer(
44 const char* mach_port_name,
45 FilterCallback filter,
46 void* filter_context,
47 OnClientDumpRequestCallback dump_callback,
48 void* dump_context,
49 OnClientExitingCallback exit_callback,
50 void* exit_context,
51 bool generate_dumps,
52 const std::string& dump_path)
53 : filter_(filter),
54 filter_context_(filter_context),
55 dump_callback_(dump_callback),
56 dump_context_(dump_context),
57 exit_callback_(exit_callback),
58 exit_context_(exit_context),
59 generate_dumps_(generate_dumps),
60 dump_dir_(dump_path.empty() ? "/tmp" : dump_path),
61 started_(false),
62 receive_port_(mach_port_name),
63 mach_port_name_(mach_port_name) {
64 }
65
~CrashGenerationServer()66 CrashGenerationServer::~CrashGenerationServer() {
67 if (started_)
68 Stop();
69 }
70
Start()71 bool CrashGenerationServer::Start() {
72 int thread_create_result = pthread_create(&server_thread_, NULL,
73 &WaitForMessages, this);
74 started_ = thread_create_result == 0;
75 return started_;
76 }
77
Stop()78 bool CrashGenerationServer::Stop() {
79 if (!started_)
80 return false;
81
82 // Send a quit message to the background thread, and then join it.
83 MachPortSender sender(mach_port_name_.c_str());
84 MachSendMessage quit_message(kQuitMessage);
85 const mach_msg_timeout_t kSendTimeoutMs = 2 * 1000;
86 kern_return_t result = sender.SendMessage(quit_message, kSendTimeoutMs);
87 if (result == KERN_SUCCESS) {
88 int thread_join_result = pthread_join(server_thread_, NULL);
89 started_ = thread_join_result != 0;
90 }
91
92 return !started_;
93 }
94
95 // static
WaitForMessages(void * server)96 void* CrashGenerationServer::WaitForMessages(void* server) {
97 CrashGenerationServer* self =
98 reinterpret_cast<CrashGenerationServer*>(server);
99 while (self->WaitForOneMessage()) {}
100 return NULL;
101 }
102
WaitForOneMessage()103 bool CrashGenerationServer::WaitForOneMessage() {
104 MachReceiveMessage message;
105 kern_return_t result = receive_port_.WaitForMessage(&message,
106 MACH_MSG_TIMEOUT_NONE);
107 if (result == KERN_SUCCESS) {
108 switch (message.GetMessageID()) {
109 case kDumpRequestMessage: {
110 ExceptionInfo& info = (ExceptionInfo&)*message.GetData();
111
112 mach_port_t remote_task = message.GetTranslatedPort(0);
113 mach_port_t crashing_thread = message.GetTranslatedPort(1);
114 mach_port_t handler_thread = message.GetTranslatedPort(2);
115 mach_port_t ack_port = message.GetTranslatedPort(3);
116 pid_t remote_pid = -1;
117 pid_for_task(remote_task, &remote_pid);
118 ClientInfo client(remote_pid);
119
120 bool result;
121 std::string dump_path;
122 if (generate_dumps_ && (!filter_ || filter_(filter_context_))) {
123 ScopedTaskSuspend suspend(remote_task);
124
125 MinidumpGenerator generator(remote_task, handler_thread);
126 dump_path = generator.UniqueNameInDirectory(dump_dir_, NULL);
127
128 if (info.exception_type && info.exception_code) {
129 generator.SetExceptionInformation(info.exception_type,
130 info.exception_code,
131 info.exception_subcode,
132 crashing_thread);
133 }
134 result = generator.Write(dump_path.c_str());
135 } else {
136 result = true;
137 }
138
139 if (result && dump_callback_) {
140 dump_callback_(dump_context_, client, dump_path);
141 }
142
143 // TODO(ted): support a way for the client to send additional data,
144 // perhaps with a callback so users of the server can read the data
145 // themselves?
146
147 if (ack_port != MACH_PORT_DEAD && ack_port != MACH_PORT_NULL) {
148 MachPortSender sender(ack_port);
149 MachSendMessage ack_message(kAcknowledgementMessage);
150 const mach_msg_timeout_t kSendTimeoutMs = 2 * 1000;
151
152 sender.SendMessage(ack_message, kSendTimeoutMs);
153 }
154
155 if (exit_callback_) {
156 exit_callback_(exit_context_, client);
157 }
158 break;
159 }
160 case kQuitMessage:
161 return false;
162 }
163 } else { // result != KERN_SUCCESS
164 return false;
165 }
166 return true;
167 }
168
169 } // namespace google_breakpad
170