Kernel: Make PerformanceEventBuffer::add_process fallible with ErrorOr
This commit is contained in:
parent
d2ffcfb762
commit
8a4654a924
Notes:
sideshowbarker
2024-07-17 21:03:32 +09:00
Author: https://github.com/IdanHo Commit: https://github.com/SerenityOS/serenity/commit/8a4654a9243 Pull-request: https://github.com/SerenityOS/serenity/pull/11817 Reviewed-by: https://github.com/bgianfo ✅
4 changed files with 22 additions and 16 deletions
|
@ -313,29 +313,34 @@ OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size
|
|||
return adopt_own_if_nonnull(new (nothrow) PerformanceEventBuffer(buffer_or_error.release_value()));
|
||||
}
|
||||
|
||||
void PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)
|
||||
ErrorOr<void> PerformanceEventBuffer::add_process(const Process& process, ProcessEventType event_type)
|
||||
{
|
||||
SpinlockLocker locker(process.address_space().get_lock());
|
||||
|
||||
OwnPtr<KString> executable;
|
||||
if (process.executable())
|
||||
executable = MUST(process.executable()->try_serialize_absolute_path());
|
||||
executable = TRY(process.executable()->try_serialize_absolute_path());
|
||||
else
|
||||
executable = MUST(KString::formatted("<{}>", process.name()));
|
||||
executable = TRY(KString::formatted("<{}>", process.name()));
|
||||
|
||||
[[maybe_unused]] auto rc = append_with_ip_and_bp(process.pid(), 0, 0, 0,
|
||||
TRY(append_with_ip_and_bp(process.pid(), 0, 0, 0,
|
||||
event_type == ProcessEventType::Create ? PERF_EVENT_PROCESS_CREATE : PERF_EVENT_PROCESS_EXEC,
|
||||
0, process.pid().value(), 0, executable->view());
|
||||
0, process.pid().value(), 0, executable->view()));
|
||||
|
||||
ErrorOr<void> result;
|
||||
process.for_each_thread([&](auto& thread) {
|
||||
[[maybe_unused]] auto rc = append_with_ip_and_bp(process.pid(), thread.tid().value(),
|
||||
result = append_with_ip_and_bp(process.pid(), thread.tid().value(),
|
||||
0, 0, PERF_EVENT_THREAD_CREATE, 0, 0, 0, nullptr);
|
||||
return result.is_error() ? IterationDecision::Break : IterationDecision::Continue;
|
||||
});
|
||||
TRY(result);
|
||||
|
||||
for (auto const& region : process.address_space().regions()) {
|
||||
[[maybe_unused]] auto rc = append_with_ip_and_bp(process.pid(), 0,
|
||||
0, 0, PERF_EVENT_MMAP, 0, region->range().base().get(), region->range().size(), region->name());
|
||||
TRY(append_with_ip_and_bp(process.pid(), 0,
|
||||
0, 0, PERF_EVENT_MMAP, 0, region->range().base().get(), region->range().size(), region->name()));
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<FlatPtr> PerformanceEventBuffer::register_string(NonnullOwnPtr<KString> string)
|
||||
|
|
|
@ -121,7 +121,7 @@ public:
|
|||
|
||||
ErrorOr<void> to_json(KBufferBuilder&) const;
|
||||
|
||||
void add_process(const Process&, ProcessEventType event_type);
|
||||
ErrorOr<void> add_process(const Process&, ProcessEventType event_type);
|
||||
|
||||
ErrorOr<FlatPtr> register_string(NonnullOwnPtr<KString>);
|
||||
|
||||
|
|
|
@ -18,14 +18,14 @@ public:
|
|||
{
|
||||
if (g_profiling_all_threads) {
|
||||
VERIFY(g_global_perf_events);
|
||||
g_global_perf_events->add_process(process, ProcessEventType::Create);
|
||||
(void)g_global_perf_events->add_process(process, ProcessEventType::Create);
|
||||
}
|
||||
}
|
||||
|
||||
inline static void add_process_exec_event(Process& process)
|
||||
{
|
||||
if (auto* event_buffer = process.current_perf_events_buffer()) {
|
||||
event_buffer->add_process(process, ProcessEventType::Exec);
|
||||
(void)event_buffer->add_process(process, ProcessEventType::Exec);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -788,11 +788,12 @@ void Process::tracer_trap(Thread& thread, const RegisterState& regs)
|
|||
|
||||
bool Process::create_perf_events_buffer_if_needed()
|
||||
{
|
||||
if (!m_perf_event_buffer) {
|
||||
m_perf_event_buffer = PerformanceEventBuffer::try_create_with_size(4 * MiB);
|
||||
m_perf_event_buffer->add_process(*this, ProcessEventType::Create);
|
||||
}
|
||||
return !!m_perf_event_buffer;
|
||||
if (m_perf_event_buffer)
|
||||
return true;
|
||||
m_perf_event_buffer = PerformanceEventBuffer::try_create_with_size(4 * MiB);
|
||||
if (!m_perf_event_buffer)
|
||||
return false;
|
||||
return !m_perf_event_buffer->add_process(*this, ProcessEventType::Create).is_error();
|
||||
}
|
||||
|
||||
void Process::delete_perf_events_buffer()
|
||||
|
|
Loading…
Add table
Reference in a new issue