2020-02-02 19:26:27 +00:00
|
|
|
/*
|
2021-01-11 08:52:18 +00:00
|
|
|
* Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
|
2020-02-02 19:26:27 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-02-02 19:26:27 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/JsonArraySerializer.h>
|
2020-03-22 00:12:45 +00:00
|
|
|
#include <AK/JsonObjectSerializer.h>
|
2021-05-18 09:26:11 +00:00
|
|
|
#include <AK/ScopeGuard.h>
|
2021-10-14 22:01:29 +00:00
|
|
|
#include <Kernel/Arch/RegisterState.h>
|
2022-05-02 20:46:43 +00:00
|
|
|
#include <Kernel/Arch/SafeMem.h>
|
2021-10-14 21:53:48 +00:00
|
|
|
#include <Kernel/Arch/SmapDisabler.h>
|
2021-03-02 18:01:02 +00:00
|
|
|
#include <Kernel/FileSystem/Custody.h>
|
2020-02-02 19:26:27 +00:00
|
|
|
#include <Kernel/KBufferBuilder.h>
|
|
|
|
#include <Kernel/PerformanceEventBuffer.h>
|
2021-01-11 08:52:18 +00:00
|
|
|
#include <Kernel/Process.h>
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
namespace Kernel {
|
|
|
|
|
2021-03-02 15:55:54 +00:00
|
|
|
PerformanceEventBuffer::PerformanceEventBuffer(NonnullOwnPtr<KBuffer> buffer)
|
|
|
|
: m_buffer(move(buffer))
|
2020-02-02 19:26:27 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-12-04 12:26:13 +00:00
|
|
|
NEVER_INLINE ErrorOr<void> PerformanceEventBuffer::append(int type, FlatPtr arg1, FlatPtr arg2, StringView arg3, Thread* current_thread, FlatPtr arg4, u64 arg5, ErrorOr<FlatPtr> arg6)
|
2021-01-17 09:53:18 +00:00
|
|
|
{
|
2022-05-02 20:22:33 +00:00
|
|
|
FlatPtr base_pointer = (FlatPtr)__builtin_frame_address(0);
|
2021-12-04 12:26:13 +00:00
|
|
|
return append_with_ip_and_bp(current_thread->pid(), current_thread->tid(), 0, base_pointer, type, 0, arg1, arg2, arg3, arg4, arg5, arg6);
|
2021-01-17 09:53:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 23:54:45 +00:00
|
|
|
static Vector<FlatPtr, PerformanceEvent::max_stack_frame_count> raw_backtrace(FlatPtr bp, FlatPtr ip)
|
2021-02-03 10:51:13 +00:00
|
|
|
{
|
|
|
|
Vector<FlatPtr, PerformanceEvent::max_stack_frame_count> backtrace;
|
2021-07-18 23:54:45 +00:00
|
|
|
if (ip != 0)
|
2022-01-03 11:33:47 +00:00
|
|
|
backtrace.unchecked_append(ip);
|
2021-02-03 10:51:13 +00:00
|
|
|
FlatPtr stack_ptr_copy;
|
2021-07-18 23:54:45 +00:00
|
|
|
FlatPtr stack_ptr = bp;
|
2021-02-03 10:51:13 +00:00
|
|
|
// FIXME: Figure out how to remove this SmapDisabler without breaking profile stacks.
|
|
|
|
SmapDisabler disabler;
|
2021-12-19 15:38:46 +00:00
|
|
|
// NOTE: The stack should always have kernel frames first, followed by userspace frames.
|
|
|
|
// If a userspace frame points back into kernel memory, something is afoot.
|
|
|
|
bool is_walking_userspace_stack = false;
|
2021-02-03 10:51:13 +00:00
|
|
|
while (stack_ptr) {
|
|
|
|
void* fault_at;
|
|
|
|
if (!safe_memcpy(&stack_ptr_copy, (void*)stack_ptr, sizeof(FlatPtr), fault_at))
|
|
|
|
break;
|
2021-12-19 15:38:46 +00:00
|
|
|
if (!Memory::is_user_address(VirtualAddress { stack_ptr })) {
|
|
|
|
if (is_walking_userspace_stack) {
|
|
|
|
dbgln("SHENANIGANS! Userspace stack points back into kernel memory");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
is_walking_userspace_stack = true;
|
|
|
|
}
|
2021-02-03 10:51:13 +00:00
|
|
|
FlatPtr retaddr;
|
|
|
|
if (!safe_memcpy(&retaddr, (void*)(stack_ptr + sizeof(FlatPtr)), sizeof(FlatPtr), fault_at))
|
|
|
|
break;
|
2021-04-25 11:39:11 +00:00
|
|
|
if (retaddr == 0)
|
|
|
|
break;
|
2022-01-03 11:33:47 +00:00
|
|
|
backtrace.unchecked_append(retaddr);
|
2021-02-03 10:51:13 +00:00
|
|
|
if (backtrace.size() == PerformanceEvent::max_stack_frame_count)
|
|
|
|
break;
|
|
|
|
stack_ptr = stack_ptr_copy;
|
|
|
|
}
|
|
|
|
return backtrace;
|
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid, RegisterState const& regs,
|
2021-12-04 12:26:13 +00:00
|
|
|
int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FlatPtr arg4, u64 arg5, ErrorOr<FlatPtr> arg6)
|
2021-10-11 22:52:03 +00:00
|
|
|
{
|
2021-12-04 12:26:13 +00:00
|
|
|
return append_with_ip_and_bp(pid, tid, regs.ip(), regs.bp(), type, lost_samples, arg1, arg2, arg3, arg4, arg5, arg6);
|
2021-10-11 22:52:03 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> PerformanceEventBuffer::append_with_ip_and_bp(ProcessID pid, ThreadID tid,
|
2021-12-04 12:26:13 +00:00
|
|
|
FlatPtr ip, FlatPtr bp, int type, u32 lost_samples, FlatPtr arg1, FlatPtr arg2, StringView arg3, FlatPtr arg4, u64 arg5, ErrorOr<FlatPtr> arg6)
|
2020-02-02 19:26:27 +00:00
|
|
|
{
|
|
|
|
if (count() >= capacity())
|
2021-01-20 22:11:17 +00:00
|
|
|
return ENOBUFS;
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2021-05-14 06:10:43 +00:00
|
|
|
if ((g_profiling_event_mask & type) == 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
2021-12-29 00:01:27 +00:00
|
|
|
auto* current_thread = Thread::current();
|
2021-05-18 09:26:11 +00:00
|
|
|
u32 enter_count = 0;
|
|
|
|
if (current_thread)
|
|
|
|
enter_count = current_thread->enter_profiler();
|
|
|
|
ScopeGuard leave_profiler([&] {
|
|
|
|
if (current_thread)
|
|
|
|
current_thread->leave_profiler();
|
|
|
|
});
|
|
|
|
if (enter_count > 0)
|
|
|
|
return EINVAL;
|
|
|
|
|
2020-02-02 19:26:27 +00:00
|
|
|
PerformanceEvent event;
|
|
|
|
event.type = type;
|
2021-05-13 20:14:07 +00:00
|
|
|
event.lost_samples = lost_samples;
|
2020-02-02 19:26:27 +00:00
|
|
|
|
|
|
|
switch (type) {
|
2021-01-11 08:52:18 +00:00
|
|
|
case PERF_EVENT_SAMPLE:
|
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
case PERF_EVENT_MALLOC:
|
|
|
|
event.data.malloc.size = arg1;
|
|
|
|
event.data.malloc.ptr = arg2;
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_FREE:
|
|
|
|
event.data.free.ptr = arg1;
|
|
|
|
break;
|
2021-04-25 21:42:36 +00:00
|
|
|
case PERF_EVENT_MMAP:
|
|
|
|
event.data.mmap.ptr = arg1;
|
|
|
|
event.data.mmap.size = arg2;
|
|
|
|
memset(event.data.mmap.name, 0, sizeof(event.data.mmap.name));
|
|
|
|
if (!arg3.is_empty())
|
|
|
|
memcpy(event.data.mmap.name, arg3.characters_without_null_termination(), min(arg3.length(), sizeof(event.data.mmap.name) - 1));
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_MUNMAP:
|
2021-04-26 21:05:59 +00:00
|
|
|
event.data.munmap.ptr = arg1;
|
|
|
|
event.data.munmap.size = arg2;
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_CREATE:
|
|
|
|
event.data.process_create.parent_pid = arg1;
|
|
|
|
memset(event.data.process_create.executable, 0, sizeof(event.data.process_create.executable));
|
|
|
|
if (!arg3.is_empty()) {
|
|
|
|
memcpy(event.data.process_create.executable, arg3.characters_without_null_termination(),
|
|
|
|
min(arg3.length(), sizeof(event.data.process_create.executable) - 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXEC:
|
|
|
|
memset(event.data.process_exec.executable, 0, sizeof(event.data.process_exec.executable));
|
|
|
|
if (!arg3.is_empty()) {
|
|
|
|
memcpy(event.data.process_exec.executable, arg3.characters_without_null_termination(),
|
|
|
|
min(arg3.length(), sizeof(event.data.process_exec.executable) - 1));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXIT:
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_CREATE:
|
|
|
|
event.data.thread_create.parent_tid = arg1;
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_EXIT:
|
|
|
|
break;
|
2021-05-14 05:48:53 +00:00
|
|
|
case PERF_EVENT_CONTEXT_SWITCH:
|
|
|
|
event.data.context_switch.next_pid = arg1;
|
|
|
|
event.data.context_switch.next_tid = arg2;
|
|
|
|
break;
|
2021-05-13 11:09:00 +00:00
|
|
|
case PERF_EVENT_KMALLOC:
|
|
|
|
event.data.kmalloc.size = arg1;
|
|
|
|
event.data.kmalloc.ptr = arg2;
|
|
|
|
break;
|
|
|
|
case PERF_EVENT_KFREE:
|
|
|
|
event.data.kfree.size = arg1;
|
|
|
|
event.data.kfree.ptr = arg2;
|
|
|
|
break;
|
2021-05-18 09:26:11 +00:00
|
|
|
case PERF_EVENT_PAGE_FAULT:
|
|
|
|
break;
|
2021-08-10 19:02:59 +00:00
|
|
|
case PERF_EVENT_SYSCALL:
|
|
|
|
break;
|
2021-08-11 18:04:03 +00:00
|
|
|
case PERF_EVENT_SIGNPOST:
|
|
|
|
event.data.signpost.arg1 = arg1;
|
|
|
|
event.data.signpost.arg2 = arg2;
|
|
|
|
break;
|
2021-12-04 12:26:13 +00:00
|
|
|
case PERF_EVENT_READ:
|
|
|
|
event.data.read.fd = arg1;
|
|
|
|
event.data.read.size = arg2;
|
|
|
|
event.data.read.filename_index = arg4;
|
|
|
|
event.data.read.start_timestamp = arg5;
|
|
|
|
event.data.read.success = !arg6.is_error();
|
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
default:
|
2021-01-20 22:11:17 +00:00
|
|
|
return EINVAL;
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
|
|
|
|
2021-07-18 23:54:45 +00:00
|
|
|
auto backtrace = raw_backtrace(bp, ip);
|
2020-03-08 09:36:51 +00:00
|
|
|
event.stack_size = min(sizeof(event.stack) / sizeof(FlatPtr), static_cast<size_t>(backtrace.size()));
|
|
|
|
memcpy(event.stack, backtrace.data(), event.stack_size * sizeof(FlatPtr));
|
2020-02-02 19:26:27 +00:00
|
|
|
|
2021-04-25 21:42:36 +00:00
|
|
|
event.pid = pid.value();
|
|
|
|
event.tid = tid.value();
|
2020-11-15 18:58:19 +00:00
|
|
|
event.timestamp = TimeManagement::the().uptime_ms();
|
2020-02-02 19:26:27 +00:00
|
|
|
at(m_count++) = event;
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PerformanceEvent& PerformanceEventBuffer::at(size_t index)
|
|
|
|
{
|
2021-02-23 19:42:32 +00:00
|
|
|
VERIFY(index < capacity());
|
2020-12-18 19:43:39 +00:00
|
|
|
auto* events = reinterpret_cast<PerformanceEvent*>(m_buffer->data());
|
2020-02-02 19:26:27 +00:00
|
|
|
return events[index];
|
|
|
|
}
|
|
|
|
|
2021-03-02 16:19:35 +00:00
|
|
|
template<typename Serializer>
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> PerformanceEventBuffer::to_json_impl(Serializer& object) const
|
2021-01-11 08:52:18 +00:00
|
|
|
{
|
2021-08-11 18:28:29 +00:00
|
|
|
{
|
2022-02-24 18:08:48 +00:00
|
|
|
auto strings = TRY(object.add_array("strings"));
|
2022-02-26 21:59:26 +00:00
|
|
|
Vector<KString*> strings_sorted_by_index;
|
|
|
|
TRY(strings_sorted_by_index.try_resize(m_strings.size()));
|
|
|
|
|
|
|
|
for (auto& entry : m_strings) {
|
|
|
|
strings_sorted_by_index[entry.value] = const_cast<Kernel::KString*>(entry.key.ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_strings.size(); i++) {
|
|
|
|
TRY(strings.add(strings_sorted_by_index[i]->view()));
|
|
|
|
}
|
|
|
|
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(strings.finish());
|
2021-08-11 18:28:29 +00:00
|
|
|
}
|
|
|
|
|
2021-12-19 15:40:11 +00:00
|
|
|
bool show_kernel_addresses = Process::current().is_superuser();
|
2022-02-24 18:08:48 +00:00
|
|
|
auto array = TRY(object.add_array("events"));
|
2021-05-13 22:43:08 +00:00
|
|
|
bool seen_first_sample = false;
|
2020-02-02 19:26:27 +00:00
|
|
|
for (size_t i = 0; i < m_count; ++i) {
|
2021-12-19 15:40:11 +00:00
|
|
|
auto const& event = at(i);
|
|
|
|
|
|
|
|
if (!show_kernel_addresses) {
|
|
|
|
if (event.type == PERF_EVENT_KMALLOC || event.type == PERF_EVENT_KFREE)
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-02-24 18:08:48 +00:00
|
|
|
auto event_object = TRY(array.add_object());
|
2020-02-02 19:26:27 +00:00
|
|
|
switch (event.type) {
|
2021-01-11 08:52:18 +00:00
|
|
|
case PERF_EVENT_SAMPLE:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "sample"));
|
2021-01-11 08:52:18 +00:00
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
case PERF_EVENT_MALLOC:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "malloc"));
|
|
|
|
TRY(event_object.add("ptr", static_cast<u64>(event.data.malloc.ptr)));
|
|
|
|
TRY(event_object.add("size", static_cast<u64>(event.data.malloc.size)));
|
2020-02-02 19:26:27 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_FREE:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "free"));
|
|
|
|
TRY(event_object.add("ptr", static_cast<u64>(event.data.free.ptr)));
|
2020-02-02 19:26:27 +00:00
|
|
|
break;
|
2021-04-25 21:42:36 +00:00
|
|
|
case PERF_EVENT_MMAP:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "mmap"));
|
|
|
|
TRY(event_object.add("ptr", static_cast<u64>(event.data.mmap.ptr)));
|
|
|
|
TRY(event_object.add("size", static_cast<u64>(event.data.mmap.size)));
|
|
|
|
TRY(event_object.add("name", event.data.mmap.name));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_MUNMAP:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "munmap"));
|
|
|
|
TRY(event_object.add("ptr", static_cast<u64>(event.data.munmap.ptr)));
|
|
|
|
TRY(event_object.add("size", static_cast<u64>(event.data.munmap.size)));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_CREATE:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "process_create"));
|
|
|
|
TRY(event_object.add("parent_pid", static_cast<u64>(event.data.process_create.parent_pid)));
|
|
|
|
TRY(event_object.add("executable", event.data.process_create.executable));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXEC:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "process_exec"));
|
|
|
|
TRY(event_object.add("executable", event.data.process_exec.executable));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_PROCESS_EXIT:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "process_exit"));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_CREATE:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "thread_create"));
|
|
|
|
TRY(event_object.add("parent_tid", static_cast<u64>(event.data.thread_create.parent_tid)));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_THREAD_EXIT:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "thread_exit"));
|
2021-04-25 21:42:36 +00:00
|
|
|
break;
|
2021-05-14 05:48:53 +00:00
|
|
|
case PERF_EVENT_CONTEXT_SWITCH:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "context_switch"));
|
|
|
|
TRY(event_object.add("next_pid", static_cast<u64>(event.data.context_switch.next_pid)));
|
|
|
|
TRY(event_object.add("next_tid", static_cast<u64>(event.data.context_switch.next_tid)));
|
2021-05-14 05:48:53 +00:00
|
|
|
break;
|
2021-05-13 11:09:00 +00:00
|
|
|
case PERF_EVENT_KMALLOC:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "kmalloc"));
|
|
|
|
TRY(event_object.add("ptr", static_cast<u64>(event.data.kmalloc.ptr)));
|
|
|
|
TRY(event_object.add("size", static_cast<u64>(event.data.kmalloc.size)));
|
2021-05-13 11:09:00 +00:00
|
|
|
break;
|
|
|
|
case PERF_EVENT_KFREE:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "kfree"));
|
|
|
|
TRY(event_object.add("ptr", static_cast<u64>(event.data.kfree.ptr)));
|
|
|
|
TRY(event_object.add("size", static_cast<u64>(event.data.kfree.size)));
|
2021-05-13 11:09:00 +00:00
|
|
|
break;
|
2021-05-18 09:26:11 +00:00
|
|
|
case PERF_EVENT_PAGE_FAULT:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "page_fault"));
|
2021-05-18 09:26:11 +00:00
|
|
|
break;
|
2021-08-10 19:02:59 +00:00
|
|
|
case PERF_EVENT_SYSCALL:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "syscall"));
|
2021-08-10 19:02:59 +00:00
|
|
|
break;
|
2021-08-11 18:04:03 +00:00
|
|
|
case PERF_EVENT_SIGNPOST:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type"sv, "signpost"sv));
|
|
|
|
TRY(event_object.add("arg1"sv, event.data.signpost.arg1));
|
|
|
|
TRY(event_object.add("arg2"sv, event.data.signpost.arg2));
|
2021-08-11 18:04:03 +00:00
|
|
|
break;
|
2021-12-04 12:26:13 +00:00
|
|
|
case PERF_EVENT_READ:
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("type", "read"));
|
|
|
|
TRY(event_object.add("fd", event.data.read.fd));
|
|
|
|
TRY(event_object.add("size"sv, event.data.read.size));
|
|
|
|
TRY(event_object.add("filename_index"sv, event.data.read.filename_index));
|
|
|
|
TRY(event_object.add("start_timestamp"sv, event.data.read.start_timestamp));
|
|
|
|
TRY(event_object.add("success"sv, event.data.read.success));
|
2021-12-04 12:26:13 +00:00
|
|
|
break;
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(event_object.add("pid", event.pid));
|
|
|
|
TRY(event_object.add("tid", event.tid));
|
|
|
|
TRY(event_object.add("timestamp", event.timestamp));
|
|
|
|
TRY(event_object.add("lost_samples", seen_first_sample ? event.lost_samples : 0));
|
2021-05-13 22:43:08 +00:00
|
|
|
if (event.type == PERF_EVENT_SAMPLE)
|
|
|
|
seen_first_sample = true;
|
2022-02-24 18:08:48 +00:00
|
|
|
auto stack_array = TRY(event_object.add_array("stack"));
|
2020-02-02 19:26:27 +00:00
|
|
|
for (size_t j = 0; j < event.stack_size; ++j) {
|
2021-12-19 15:41:07 +00:00
|
|
|
auto address = event.stack[j];
|
|
|
|
if (!show_kernel_addresses && !Memory::is_user_address(VirtualAddress { address }))
|
|
|
|
address = 0xdeadc0de;
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(stack_array.add(address));
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(stack_array.finish());
|
|
|
|
TRY(event_object.finish());
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
2022-02-24 18:08:48 +00:00
|
|
|
TRY(array.finish());
|
|
|
|
TRY(object.finish());
|
2021-11-07 23:51:39 +00:00
|
|
|
return {};
|
2020-02-02 19:26:27 +00:00
|
|
|
}
|
2020-02-16 00:27:42 +00:00
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<void> PerformanceEventBuffer::to_json(KBufferBuilder& builder) const
|
2021-03-02 16:19:35 +00:00
|
|
|
{
|
2022-02-24 18:08:48 +00:00
|
|
|
auto object = TRY(JsonObjectSerializer<>::try_create(builder));
|
2021-03-02 16:19:35 +00:00
|
|
|
return to_json_impl(object);
|
|
|
|
}
|
|
|
|
|
2021-03-02 15:55:54 +00:00
|
|
|
OwnPtr<PerformanceEventBuffer> PerformanceEventBuffer::try_create_with_size(size_t buffer_size)
|
|
|
|
{
|
2022-04-10 22:08:07 +00:00
|
|
|
auto buffer_or_error = KBuffer::try_create_with_size("Performance events"sv, buffer_size, Memory::Region::Access::ReadWrite, AllocationStrategy::AllocateNow);
|
2021-09-07 13:15:08 +00:00
|
|
|
if (buffer_or_error.is_error())
|
2021-03-02 15:55:54 +00:00
|
|
|
return {};
|
2021-09-07 13:15:08 +00:00
|
|
|
return adopt_own_if_nonnull(new (nothrow) PerformanceEventBuffer(buffer_or_error.release_value()));
|
2021-03-02 15:55:54 +00:00
|
|
|
}
|
|
|
|
|
2022-04-01 17:58:27 +00:00
|
|
|
ErrorOr<void> PerformanceEventBuffer::add_process(Process const& process, ProcessEventType event_type)
|
2021-03-02 18:01:02 +00:00
|
|
|
{
|
2021-08-21 23:49:22 +00:00
|
|
|
SpinlockLocker locker(process.address_space().get_lock());
|
2021-03-02 18:01:02 +00:00
|
|
|
|
2022-01-11 19:34:16 +00:00
|
|
|
OwnPtr<KString> executable;
|
2021-03-02 18:01:02 +00:00
|
|
|
if (process.executable())
|
2022-01-11 19:50:12 +00:00
|
|
|
executable = TRY(process.executable()->try_serialize_absolute_path());
|
2021-04-25 21:42:36 +00:00
|
|
|
else
|
2022-01-11 19:50:12 +00:00
|
|
|
executable = TRY(KString::formatted("<{}>", process.name()));
|
2021-04-25 21:42:36 +00:00
|
|
|
|
2022-01-11 19:50:12 +00:00
|
|
|
TRY(append_with_ip_and_bp(process.pid(), 0, 0, 0,
|
2021-04-25 21:42:36 +00:00
|
|
|
event_type == ProcessEventType::Create ? PERF_EVENT_PROCESS_CREATE : PERF_EVENT_PROCESS_EXEC,
|
2022-01-11 19:50:12 +00:00
|
|
|
0, process.pid().value(), 0, executable->view()));
|
2021-03-02 18:01:02 +00:00
|
|
|
|
2022-01-11 19:50:12 +00:00
|
|
|
ErrorOr<void> result;
|
2021-03-02 18:01:02 +00:00
|
|
|
process.for_each_thread([&](auto& thread) {
|
2022-01-11 19:50:12 +00:00
|
|
|
result = append_with_ip_and_bp(process.pid(), thread.tid().value(),
|
2021-05-13 20:14:07 +00:00
|
|
|
0, 0, PERF_EVENT_THREAD_CREATE, 0, 0, 0, nullptr);
|
2022-01-11 19:50:12 +00:00
|
|
|
return result.is_error() ? IterationDecision::Break : IterationDecision::Continue;
|
2021-03-02 18:01:02 +00:00
|
|
|
});
|
2022-01-11 19:50:12 +00:00
|
|
|
TRY(result);
|
2021-03-02 18:01:02 +00:00
|
|
|
|
2021-12-29 00:01:27 +00:00
|
|
|
for (auto const& region : process.address_space().regions()) {
|
2022-01-11 19:50:12 +00:00
|
|
|
TRY(append_with_ip_and_bp(process.pid(), 0,
|
2022-04-01 23:28:01 +00:00
|
|
|
0, 0, PERF_EVENT_MMAP, 0, region.range().base().get(), region.range().size(), region.name()));
|
2021-03-02 18:01:02 +00:00
|
|
|
}
|
2022-01-11 19:50:12 +00:00
|
|
|
|
|
|
|
return {};
|
2021-03-02 18:01:02 +00:00
|
|
|
}
|
|
|
|
|
2021-11-07 23:51:39 +00:00
|
|
|
ErrorOr<FlatPtr> PerformanceEventBuffer::register_string(NonnullOwnPtr<KString> string)
|
2021-08-11 18:28:29 +00:00
|
|
|
{
|
2022-02-26 21:59:26 +00:00
|
|
|
auto it = m_strings.find(string);
|
|
|
|
if (it != m_strings.end()) {
|
|
|
|
return it->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto new_index = m_strings.size();
|
|
|
|
TRY(m_strings.try_set(move(string), move(new_index)));
|
|
|
|
return new_index;
|
2021-08-11 18:28:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-16 00:27:42 +00:00
|
|
|
}
|