Skip to content

Commit 2e8ce8b

Browse files
authored
feat: add CrashpadClient::Add/RemoveAttachment() for Windows and Linux (#124)
1 parent b265391 commit 2e8ce8b

15 files changed

+207
-7
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) || BUILDFLAG(IS_LINUX) || 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_linux.cc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,16 @@ class RequestCrashDumpHandler : public SignalHandler {
436436
}
437437
#endif
438438

439+
void AddAttachment(const base::FilePath& attachment) {
440+
ExceptionHandlerClient client(sock_to_handler_.get(), true);
441+
client.AddAttachment(attachment);
442+
}
443+
444+
void RemoveAttachment(const base::FilePath& attachment) {
445+
ExceptionHandlerClient client(sock_to_handler_.get(), true);
446+
client.RemoveAttachment(attachment);
447+
}
448+
439449
private:
440450
RequestCrashDumpHandler() = default;
441451

@@ -807,4 +817,14 @@ void CrashpadClient::SetCrashLoopBefore(uint64_t crash_loop_before_time) {
807817
}
808818
#endif
809819

820+
void CrashpadClient::AddAttachment(const base::FilePath& attachment) {
821+
auto signal_handler = RequestCrashDumpHandler::Get();
822+
signal_handler->AddAttachment(attachment);
823+
}
824+
825+
void CrashpadClient::RemoveAttachment(const base::FilePath& attachment) {
826+
auto signal_handler = RequestCrashDumpHandler::Get();
827+
signal_handler->RemoveAttachment(attachment);
828+
}
829+
810830
} // namespace crashpad

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/linux/crash_report_exception_handler.cc

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ CrashReportExceptionHandler::CrashReportExceptionHandler(
111111
: database_(database),
112112
upload_thread_(upload_thread),
113113
process_annotations_(process_annotations),
114-
attachments_(attachments),
114+
attachments_(*attachments),
115115
write_minidump_to_database_(write_minidump_to_database),
116116
write_minidump_to_log_(write_minidump_to_log),
117117
user_stream_data_sources_(user_stream_data_sources),
@@ -210,6 +210,26 @@ bool CrashReportExceptionHandler::HandleExceptionWithConnection(
210210
return result;
211211
}
212212

213+
void CrashReportExceptionHandler::AddAttachment(
214+
const base::FilePath& attachment) {
215+
auto it = std::find(attachments_.begin(), attachments_.end(), attachment);
216+
if (it != attachments_.end()) {
217+
LOG(WARNING) << "ignoring duplicate attachment " << attachment;
218+
return;
219+
}
220+
attachments_.push_back(attachment);
221+
}
222+
223+
void CrashReportExceptionHandler::RemoveAttachment(
224+
const base::FilePath& attachment) {
225+
auto it = std::find(attachments_.begin(), attachments_.end(), attachment);
226+
if (it == attachments_.end()) {
227+
LOG(WARNING) << "ignoring non-existent attachment " << attachment;
228+
return;
229+
}
230+
attachments_.erase(it);
231+
}
232+
213233
bool CrashReportExceptionHandler::WriteMinidumpToDatabase(
214234
ProcessSnapshotLinux* process_snapshot,
215235
ProcessSnapshotSanitized* sanitized_snapshot,
@@ -252,7 +272,7 @@ bool CrashReportExceptionHandler::WriteMinidumpToDatabase(
252272
}
253273
}
254274

255-
for (const auto& attachment : (*attachments_)) {
275+
for (const auto& attachment : attachments_) {
256276
FileReader file_reader;
257277
if (!file_reader.Open(attachment)) {
258278
LOG(ERROR) << "attachment " << attachment.value().c_str()

handler/linux/crash_report_exception_handler.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <map>
1919
#include <string>
2020

21+
#include "base/files/file_path.h"
2122
#include "client/crash_report_database.h"
2223
#include "handler/crash_report_upload_thread.h"
2324
#include "handler/linux/exception_handler_server.h"
@@ -94,6 +95,9 @@ class CrashReportExceptionHandler : public ExceptionHandlerServer::Delegate {
9495
int broker_sock,
9596
UUID* local_report_id = nullptr) override;
9697

98+
void AddAttachment(const base::FilePath& attachment) override;
99+
void RemoveAttachment(const base::FilePath& attachment) override;
100+
97101
private:
98102
bool HandleExceptionWithConnection(
99103
PtraceConnection* connection,
@@ -119,7 +123,7 @@ class CrashReportExceptionHandler : public ExceptionHandlerServer::Delegate {
119123
CrashReportDatabase* database_; // weak
120124
CrashReportUploadThread* upload_thread_; // weak
121125
const std::map<std::string, std::string>* process_annotations_; // weak
122-
const std::vector<base::FilePath>* attachments_; // weak
126+
std::vector<base::FilePath> attachments_;
123127
bool write_minidump_to_database_;
124128
bool write_minidump_to_log_;
125129
const UserStreamDataSources* user_stream_data_sources_; // weak

handler/linux/exception_handler_server.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,14 @@ bool ExceptionHandlerServer::ReceiveClientMessage(Event* event) {
431431
message.requesting_thread_stack_address,
432432
event->fd.get(),
433433
event->type == Event::Type::kSharedSocketMessage);
434+
435+
case ExceptionHandlerProtocol::ClientToServerMessage::kTypeAddAttachment:
436+
delegate_->AddAttachment(base::FilePath(message.attachment_info.path));
437+
return true;
438+
439+
case ExceptionHandlerProtocol::ClientToServerMessage::kTypeRemoveAttachment:
440+
delegate_->RemoveAttachment(base::FilePath(message.attachment_info.path));
441+
return true;
434442
}
435443

436444
DCHECK(false);

handler/linux/exception_handler_server.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class ExceptionHandlerServer {
111111
int broker_sock,
112112
UUID* local_report_id = nullptr) = 0;
113113

114+
//! \brief Called to add an attachment to the crash report.
115+
virtual void AddAttachment(const base::FilePath& attachment) = 0;
116+
117+
//! \brief Called to remove an attachment from the crash report.
118+
virtual void RemoveAttachment(const base::FilePath& attachment) = 0;
119+
114120
virtual ~Delegate() {}
115121
};
116122

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/linux/exception_handler_client.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,23 @@ int ExceptionHandlerClient::WaitForCrashDumpComplete() {
225225
return errno;
226226
}
227227

228+
void ExceptionHandlerClient::AddAttachment(const base::FilePath& attachment) {
229+
ExceptionHandlerProtocol::ClientToServerMessage message;
230+
message.type =
231+
ExceptionHandlerProtocol::ClientToServerMessage::kTypeAddAttachment;
232+
snprintf(
233+
message.attachment_info.path, PATH_MAX, "%s", attachment.value().c_str());
234+
UnixCredentialSocket::SendMsg(server_sock_, &message, sizeof(message));
235+
}
236+
237+
void ExceptionHandlerClient::RemoveAttachment(
238+
const base::FilePath& attachment) {
239+
ExceptionHandlerProtocol::ClientToServerMessage message;
240+
message.type =
241+
ExceptionHandlerProtocol::ClientToServerMessage::kTypeRemoveAttachment;
242+
snprintf(
243+
message.attachment_info.path, PATH_MAX, "%s", attachment.value().c_str());
244+
UnixCredentialSocket::SendMsg(server_sock_, &message, sizeof(message));
245+
}
246+
228247
} // namespace crashpad

0 commit comments

Comments
 (0)