Skip to content

Commit 1f1021b

Browse files
committed
feat: add CrashpadClient::Add/RemoveAttachment() for Windows
1 parent b265391 commit 1f1021b

File tree

7 files changed

+106
-3
lines changed

7 files changed

+106
-3
lines changed

client/crashpad_client.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -840,6 +840,20 @@ class CrashpadClient {
840840
static void SetCrashLoopBefore(uint64_t crash_loop_before_time);
841841
#endif
842842

843+
#if BUILDFLAG(IS_WIN) || DOXYGEN
844+
//! \brief Adds a file to the list of files to be attached to the crash
845+
//! report.
846+
//!
847+
//! \param[in] attachment The path to the file to be added.
848+
void AddAttachment(const base::FilePath& attachment);
849+
850+
//! \brief Removes a file from the list of files to be attached to the crash
851+
//! report.
852+
//!
853+
//! \param[in] attachment The path to the file to be removed.
854+
void RemoveAttachment(const base::FilePath& attachment);
855+
#endif
856+
843857
private:
844858
#if BUILDFLAG(IS_WIN)
845859
//! \brief Registers process handlers for the client.

client/crashpad_client_win.cc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,4 +1183,22 @@ void CrashpadClient::SetFirstChanceExceptionHandler(
11831183
first_chance_handler_ = handler;
11841184
}
11851185

1186+
void CrashpadClient::AddAttachment(const base::FilePath& attachment) {
1187+
ClientToServerMessage message = {};
1188+
message.type = ClientToServerMessage::kAddAttachment;
1189+
swprintf_s(
1190+
message.attachment.path, MAX_PATH, L"%ls", attachment.value().c_str());
1191+
ServerToClientMessage response = {};
1192+
SendToCrashHandlerServer(ipc_pipe_, message, &response);
1193+
}
1194+
1195+
void CrashpadClient::RemoveAttachment(const base::FilePath& attachment) {
1196+
ClientToServerMessage message = {};
1197+
message.type = ClientToServerMessage::kRemoveAttachment;
1198+
swprintf_s(
1199+
message.attachment.path, MAX_PATH, L"%ls", attachment.value().c_str());
1200+
ServerToClientMessage response = {};
1201+
SendToCrashHandlerServer(ipc_pipe_, message, &response);
1202+
}
1203+
11861204
} // namespace crashpad

handler/win/crash_report_exception_handler.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ CrashReportExceptionHandler::CrashReportExceptionHandler(
4545
: database_(database),
4646
upload_thread_(upload_thread),
4747
process_annotations_(process_annotations),
48-
attachments_(attachments),
48+
attachments_(*attachments),
4949
screenshot_(screenshot),
5050
wait_for_upload_(wait_for_upload),
5151
user_stream_data_sources_(user_stream_data_sources) {}
@@ -117,7 +117,7 @@ unsigned int CrashReportExceptionHandler::ExceptionHandlerServerException(
117117
return termination_code;
118118
}
119119

120-
for (const auto& attachment : (*attachments_)) {
120+
for (const auto& attachment : attachments_) {
121121
FileReader file_reader;
122122
if (!file_reader.Open(attachment)) {
123123
LOG(ERROR) << "attachment " << attachment
@@ -175,4 +175,24 @@ unsigned int CrashReportExceptionHandler::ExceptionHandlerServerException(
175175
return termination_code;
176176
}
177177

178+
void CrashReportExceptionHandler::ExceptionHandlerServerAttachmentAdded(
179+
const base::FilePath& attachment) {
180+
auto it = std::find(attachments_.begin(), attachments_.end(), attachment);
181+
if (it != attachments_.end()) {
182+
LOG(WARNING) << "ignoring duplicate attachment " << attachment;
183+
return;
184+
}
185+
attachments_.push_back(attachment);
186+
}
187+
188+
void CrashReportExceptionHandler::ExceptionHandlerServerAttachmentRemoved(
189+
const base::FilePath& attachment) {
190+
auto it = std::find(attachments_.begin(), attachments_.end(), attachment);
191+
if (it == attachments_.end()) {
192+
LOG(WARNING) << "ignoring non-existent attachment " << attachment;
193+
return;
194+
}
195+
attachments_.erase(it);
196+
}
197+
178198
} // namespace crashpad

handler/win/crash_report_exception_handler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,16 @@ class CrashReportExceptionHandler final
7979
HANDLE process,
8080
WinVMAddress exception_information_address,
8181
WinVMAddress debug_critical_section_address) override;
82+
void ExceptionHandlerServerAttachmentAdded(
83+
const base::FilePath& attachment) override;
84+
void ExceptionHandlerServerAttachmentRemoved(
85+
const base::FilePath& attachment) override;
8286

8387
private:
8488
CrashReportDatabase* database_; // weak
8589
CrashReportUploadThread* upload_thread_; // weak
8690
const std::map<std::string, std::string>* process_annotations_; // weak
87-
const std::vector<base::FilePath>* attachments_; // weak
91+
std::vector<base::FilePath> attachments_;
8892
const base::FilePath* screenshot_; // weak
8993
const bool wait_for_upload_;
9094
const UserStreamDataSources* user_stream_data_sources_; // weak

util/win/exception_handler_server.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,26 @@ bool ExceptionHandlerServer::ServiceClientConnection(
455455
// Handled below.
456456
break;
457457

458+
case ClientToServerMessage::kAddAttachment: {
459+
ServerToClientMessage shutdown_response = {};
460+
service_context.delegate()->ExceptionHandlerServerAttachmentAdded(
461+
base::FilePath(message.attachment.path));
462+
LoggingWriteFile(service_context.pipe(),
463+
&shutdown_response,
464+
sizeof(shutdown_response));
465+
return false;
466+
}
467+
468+
case ClientToServerMessage::kRemoveAttachment: {
469+
ServerToClientMessage shutdown_response = {};
470+
service_context.delegate()->ExceptionHandlerServerAttachmentRemoved(
471+
base::FilePath(message.attachment.path));
472+
LoggingWriteFile(service_context.pipe(),
473+
&shutdown_response,
474+
sizeof(shutdown_response));
475+
return false;
476+
}
477+
458478
default:
459479
LOG(ERROR) << "unhandled message type: " << message.type;
460480
return false;

util/win/exception_handler_server.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ class ExceptionHandlerServer {
5858
WinVMAddress exception_information_address,
5959
WinVMAddress debug_critical_section_address) = 0;
6060

61+
//! \brief Called when the server has received a request to add an
62+
//! attachment.
63+
//!
64+
//! \param[in] attachment The path of the attachment.
65+
virtual void ExceptionHandlerServerAttachmentAdded(
66+
const base::FilePath& attachment) = 0;
67+
68+
//! \brief Called when the server has received a request to remove an
69+
//! attachment.
70+
//!
71+
//! \param[in] attachment The path of the attachment.
72+
virtual void ExceptionHandlerServerAttachmentRemoved(
73+
const base::FilePath& attachment) = 0;
74+
6175
protected:
6276
~Delegate();
6377
};

util/win/registration_protocol_win_structs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ struct ShutdownRequest {
116116
uint64_t token;
117117
};
118118

119+
//! \brief A file attachment request.
120+
struct AttachmentRequest {
121+
//! \brief The path of the attachment.
122+
wchar_t path[MAX_PATH];
123+
};
124+
119125
//! \brief The message passed from client to server by
120126
//! SendToCrashHandlerServer().
121127
struct ClientToServerMessage {
@@ -127,6 +133,12 @@ struct ClientToServerMessage {
127133
//! \brief For ShutdownRequest.
128134
kShutdown,
129135

136+
//! \brief For AttachmentRequest.
137+
kAddAttachment,
138+
139+
//! \brief For AttachmentRequest.
140+
kRemoveAttachment,
141+
130142
//! \brief An empty message sent by the initial client in asynchronous mode.
131143
//! No data is required, this just confirms that the server is ready to
132144
//! accept client registrations.
@@ -136,6 +148,7 @@ struct ClientToServerMessage {
136148
union {
137149
RegistrationRequest registration;
138150
ShutdownRequest shutdown;
151+
AttachmentRequest attachment;
139152
};
140153
};
141154

0 commit comments

Comments
 (0)