Skip to content

Commit b97109c

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

9 files changed

+102
-5
lines changed

client/crashpad_client.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -840,7 +840,7 @@ class CrashpadClient {
840840
static void SetCrashLoopBefore(uint64_t crash_loop_before_time);
841841
#endif
842842

843-
#if BUILDFLAG(IS_WIN) || DOXYGEN
843+
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_LINUX) || DOXYGEN
844844
//! \brief Adds a file to the list of files to be attached to the crash
845845
//! report.
846846
//!

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

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

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

util/linux/exception_handler_client.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ class ExceptionHandlerClient {
6868
//! \param[in] can_set_ptracer Whether SetPtracer should be enabled.
6969
void SetCanSetPtracer(bool can_set_ptracer);
7070

71+
//! \brief Adds an attachment to the crash report.
72+
void AddAttachment(const base::FilePath& attachment);
73+
74+
//! \brief Removes an attachment from the crash report.
75+
void RemoveAttachment(const base::FilePath& attachment);
76+
7177
private:
7278
int SendCrashDumpRequest(
7379
const ExceptionHandlerProtocol::ClientInformation& info,

util/linux/exception_handler_protocol.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#define CRASHPAD_UTIL_LINUX_EXCEPTION_HANDLER_PROTOCOL_H_
1717

1818
#include <errno.h>
19+
#include <linux/limits.h>
1920
#include <signal.h>
2021
#include <stdint.h>
2122
#include <sys/types.h>
@@ -59,6 +60,10 @@ class ExceptionHandlerProtocol {
5960
#endif
6061
};
6162

63+
struct AttachmentInformation {
64+
char path[PATH_MAX];
65+
};
66+
6267
//! \brief The signal used to indicate a crash dump is complete.
6368
//!
6469
//! When multiple clients share a single socket connection with the handler,
@@ -81,7 +86,13 @@ class ExceptionHandlerProtocol {
8186
kTypeCheckCredentials,
8287

8388
//! \brief Used to request a crash dump for the sending client.
84-
kTypeCrashDumpRequest
89+
kTypeCrashDumpRequest,
90+
91+
//! \brief Request that the server add an attachment.
92+
kTypeAddAttachment,
93+
94+
//! \brief Request that the server remove an attachment.
95+
kTypeRemoveAttachment,
8596
};
8697

8798
Type type;
@@ -92,6 +103,9 @@ class ExceptionHandlerProtocol {
92103
union {
93104
//! \brief Valid for type == kCrashDumpRequest
94105
ClientInformation client_info;
106+
107+
//! \brief Valid for type == kAddAttachment || type == kRemoveAttachment
108+
AttachmentInformation attachment_info;
95109
};
96110
};
97111

0 commit comments

Comments
 (0)