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_COMMON_IPC_PROTOCOL_H__
31 #define CLIENT_WINDOWS_COMMON_IPC_PROTOCOL_H__
32 
33 #include <windows.h>
34 #include <dbghelp.h>
35 #include <string>
36 #include <utility>
37 #include "common/windows/string_utils-inl.h"
38 #include "google_breakpad/common/minidump_format.h"
39 
40 namespace google_breakpad {
41 
42 // Name/value pair for custom client information.
43 struct CustomInfoEntry {
44   // Maximum length for name and value for client custom info.
45   static const int kNameMaxLength = 64;
46   static const int kValueMaxLength = 64;
47 
CustomInfoEntryCustomInfoEntry48   CustomInfoEntry() {
49     // Putting name and value in initializer list makes VC++ show warning 4351.
50     set_name(NULL);
51     set_value(NULL);
52   }
53 
CustomInfoEntryCustomInfoEntry54   CustomInfoEntry(const wchar_t* name_arg, const wchar_t* value_arg) {
55     set_name(name_arg);
56     set_value(value_arg);
57   }
58 
set_nameCustomInfoEntry59   void set_name(const wchar_t* name_arg) {
60     if (!name_arg) {
61       name[0] = L'\0';
62       return;
63     }
64     WindowsStringUtils::safe_wcscpy(name, kNameMaxLength, name_arg);
65   }
66 
set_valueCustomInfoEntry67   void set_value(const wchar_t* value_arg) {
68     if (!value_arg) {
69       value[0] = L'\0';
70       return;
71     }
72 
73     WindowsStringUtils::safe_wcscpy(value, kValueMaxLength, value_arg);
74   }
75 
setCustomInfoEntry76   void set(const wchar_t* name_arg, const wchar_t* value_arg) {
77     set_name(name_arg);
78     set_value(value_arg);
79   }
80 
81   wchar_t name[kNameMaxLength];
82   wchar_t value[kValueMaxLength];
83 };
84 
85 // Constants for the protocol between client and the server.
86 
87 // Tags sent with each message indicating the purpose of
88 // the message.
89 enum MessageTag {
90   MESSAGE_TAG_NONE = 0,
91   MESSAGE_TAG_REGISTRATION_REQUEST = 1,
92   MESSAGE_TAG_REGISTRATION_RESPONSE = 2,
93   MESSAGE_TAG_REGISTRATION_ACK = 3,
94   MESSAGE_TAG_UPLOAD_REQUEST = 4
95 };
96 
97 struct CustomClientInfo {
98   const CustomInfoEntry* entries;
99   size_t count;
100 };
101 
102 // Message structure for IPC between crash client and crash server.
103 struct ProtocolMessage {
ProtocolMessageProtocolMessage104   ProtocolMessage()
105       : tag(MESSAGE_TAG_NONE),
106         id(0),
107         dump_type(MiniDumpNormal),
108         thread_id(0),
109         exception_pointers(NULL),
110         assert_info(NULL),
111         custom_client_info(),
112         dump_request_handle(NULL),
113         dump_generated_handle(NULL),
114         server_alive_handle(NULL) {
115   }
116 
117   // Creates an instance with the given parameters.
ProtocolMessageProtocolMessage118   ProtocolMessage(MessageTag arg_tag,
119                   DWORD arg_id,
120                   MINIDUMP_TYPE arg_dump_type,
121                   DWORD* arg_thread_id,
122                   EXCEPTION_POINTERS** arg_exception_pointers,
123                   MDRawAssertionInfo* arg_assert_info,
124                   const CustomClientInfo& custom_info,
125                   HANDLE arg_dump_request_handle,
126                   HANDLE arg_dump_generated_handle,
127                   HANDLE arg_server_alive)
128     : tag(arg_tag),
129       id(arg_id),
130       dump_type(arg_dump_type),
131       thread_id(arg_thread_id),
132       exception_pointers(arg_exception_pointers),
133       assert_info(arg_assert_info),
134       custom_client_info(custom_info),
135       dump_request_handle(arg_dump_request_handle),
136       dump_generated_handle(arg_dump_generated_handle),
137       server_alive_handle(arg_server_alive) {
138   }
139 
140   // Tag in the message.
141   MessageTag tag;
142 
143   // The id for this message. This may be either a process id or a crash id
144   // depending on the type of message.
145   DWORD id;
146 
147   // Dump type requested.
148   MINIDUMP_TYPE dump_type;
149 
150   // Client thread id pointer.
151   DWORD* thread_id;
152 
153   // Exception information.
154   EXCEPTION_POINTERS** exception_pointers;
155 
156   // Assert information in case of an invalid parameter or
157   // pure call failure.
158   MDRawAssertionInfo* assert_info;
159 
160   // Custom client information.
161   CustomClientInfo custom_client_info;
162 
163   // Handle to signal the crash event.
164   HANDLE dump_request_handle;
165 
166   // Handle to check if server is done generating crash.
167   HANDLE dump_generated_handle;
168 
169   // Handle to a mutex that becomes signaled (WAIT_ABANDONED)
170   // if server process goes down.
171   HANDLE server_alive_handle;
172 
173  private:
174   // Disable copy ctor and operator=.
175   ProtocolMessage(const ProtocolMessage& msg);
176   ProtocolMessage& operator=(const ProtocolMessage& msg);
177 };
178 
179 }  // namespace google_breakpad
180 
181 #endif  // CLIENT_WINDOWS_COMMON_IPC_PROTOCOL_H__
182