2020-07-30 18:38:15 -03:00
|
|
|
/*
|
2021-01-11 05:52:18 -03:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-07-30 18:38:15 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 18:38:15 -03:00
|
|
|
*/
|
|
|
|
|
|
2021-08-22 09:51:04 -03:00
|
|
|
#include <Kernel/Coredump.h>
|
2021-01-11 05:52:18 -03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2021-05-07 05:38:50 -03:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2020-07-30 18:38:15 -03:00
|
|
|
#include <Kernel/Process.h>
|
2021-05-13 17:15:13 -03:00
|
|
|
#include <Kernel/Time/TimeManagement.h>
|
2020-07-30 18:38:15 -03:00
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-03-02 13:19:35 -03:00
|
|
|
bool g_profiling_all_threads;
|
2021-04-25 18:42:36 -03:00
|
|
|
PerformanceEventBuffer* g_global_perf_events;
|
2021-05-14 03:10:43 -03:00
|
|
|
u64 g_profiling_event_mask;
|
2021-03-02 13:19:35 -03:00
|
|
|
|
2021-06-28 15:59:35 -03:00
|
|
|
KResultOr<FlatPtr> Process::sys$profiling_enable(pid_t pid, u64 event_mask)
|
2020-07-30 18:38:15 -03:00
|
|
|
{
|
2021-07-18 15:20:12 -03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2020-07-30 18:38:15 -03:00
|
|
|
REQUIRE_NO_PROMISES;
|
2021-03-02 13:19:35 -03:00
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
if (!is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
ScopedCritical critical;
|
2021-05-30 11:24:53 -03:00
|
|
|
g_profiling_event_mask = PERF_EVENT_PROCESS_CREATE | PERF_EVENT_THREAD_CREATE | PERF_EVENT_MMAP;
|
2021-03-02 13:19:35 -03:00
|
|
|
if (g_global_perf_events)
|
|
|
|
|
g_global_perf_events->clear();
|
|
|
|
|
else
|
|
|
|
|
g_global_perf_events = PerformanceEventBuffer::try_create_with_size(32 * MiB).leak_ptr();
|
2021-05-07 05:38:50 -03:00
|
|
|
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-05-14 05:33:34 -03:00
|
|
|
if (!TimeManagement::the().enable_profile_timer())
|
|
|
|
|
return ENOTSUP;
|
2021-05-15 16:25:54 -03:00
|
|
|
g_profiling_all_threads = true;
|
2021-05-14 03:10:43 -03:00
|
|
|
PerformanceManager::add_process_created_event(*Scheduler::colonel());
|
2021-04-25 18:42:36 -03:00
|
|
|
Process::for_each([](auto& process) {
|
2021-05-07 05:38:50 -03:00
|
|
|
PerformanceManager::add_process_created_event(process);
|
2021-04-25 18:42:36 -03:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-05-30 11:24:53 -03:00
|
|
|
g_profiling_event_mask = event_mask;
|
2021-03-02 13:19:35 -03:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 23:04:56 -03:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 18:38:15 -03:00
|
|
|
if (!process)
|
2021-03-01 09:49:16 -03:00
|
|
|
return ESRCH;
|
2020-07-30 18:38:15 -03:00
|
|
|
if (process->is_dead())
|
2021-03-01 09:49:16 -03:00
|
|
|
return ESRCH;
|
2021-03-10 15:59:46 -03:00
|
|
|
if (!is_superuser() && process->uid() != euid())
|
2021-03-01 09:49:16 -03:00
|
|
|
return EPERM;
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-05-30 11:24:53 -03:00
|
|
|
g_profiling_event_mask = PERF_EVENT_PROCESS_CREATE | PERF_EVENT_THREAD_CREATE | PERF_EVENT_MMAP;
|
2021-05-23 17:16:30 -03:00
|
|
|
process->set_profiling(true);
|
2021-05-24 03:59:30 -03:00
|
|
|
if (!process->create_perf_events_buffer_if_needed()) {
|
|
|
|
|
process->set_profiling(false);
|
2021-03-02 12:55:54 -03:00
|
|
|
return ENOMEM;
|
2021-05-24 03:59:30 -03:00
|
|
|
}
|
2021-05-30 11:24:53 -03:00
|
|
|
g_profiling_event_mask = event_mask;
|
2021-05-24 03:59:30 -03:00
|
|
|
if (!TimeManagement::the().enable_profile_timer()) {
|
|
|
|
|
process->set_profiling(false);
|
2021-05-14 05:33:34 -03:00
|
|
|
return ENOTSUP;
|
2021-05-24 03:59:30 -03:00
|
|
|
}
|
2020-07-30 18:38:15 -03:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 15:59:35 -03:00
|
|
|
KResultOr<FlatPtr> Process::sys$profiling_disable(pid_t pid)
|
2020-07-30 18:38:15 -03:00
|
|
|
{
|
2021-07-18 15:20:12 -03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-04-19 01:10:05 -03:00
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
|
2021-03-02 13:19:35 -03:00
|
|
|
if (pid == -1) {
|
|
|
|
|
if (!is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
ScopedCritical critical;
|
2021-05-14 05:33:34 -03:00
|
|
|
if (!TimeManagement::the().disable_profile_timer())
|
|
|
|
|
return ENOTSUP;
|
2021-03-02 13:19:35 -03:00
|
|
|
g_profiling_all_threads = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 23:04:56 -03:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 18:38:15 -03:00
|
|
|
if (!process)
|
2021-03-01 09:49:16 -03:00
|
|
|
return ESRCH;
|
2021-03-10 15:59:46 -03:00
|
|
|
if (!is_superuser() && process->uid() != euid())
|
2021-03-01 09:49:16 -03:00
|
|
|
return EPERM;
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-01-11 05:52:18 -03:00
|
|
|
if (!process->is_profiling())
|
2021-03-01 09:49:16 -03:00
|
|
|
return EINVAL;
|
2021-05-14 05:33:34 -03:00
|
|
|
// FIXME: If we enabled the profile timer and it's not supported, how do we disable it now?
|
|
|
|
|
if (!TimeManagement::the().disable_profile_timer())
|
|
|
|
|
return ENOTSUP;
|
2020-07-30 18:38:15 -03:00
|
|
|
process->set_profiling(false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-28 15:59:35 -03:00
|
|
|
KResultOr<FlatPtr> Process::sys$profiling_free_buffer(pid_t pid)
|
2021-04-19 01:10:05 -03:00
|
|
|
{
|
2021-07-18 15:20:12 -03:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this)
|
2021-04-19 01:10:05 -03:00
|
|
|
REQUIRE_NO_PROMISES;
|
|
|
|
|
|
|
|
|
|
if (pid == -1) {
|
|
|
|
|
if (!is_superuser())
|
|
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
|
|
OwnPtr<PerformanceEventBuffer> perf_events;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ScopedCritical critical;
|
|
|
|
|
|
2021-05-30 13:39:23 -03:00
|
|
|
perf_events = adopt_own_if_nonnull(g_global_perf_events);
|
2021-04-19 01:10:05 -03:00
|
|
|
g_global_perf_events = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto process = Process::from_pid(pid);
|
|
|
|
|
if (!process)
|
|
|
|
|
return ESRCH;
|
|
|
|
|
if (!is_superuser() && process->uid() != euid())
|
|
|
|
|
return EPERM;
|
2021-08-21 20:49:22 -03:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-04-19 01:10:05 -03:00
|
|
|
if (process->is_profiling())
|
|
|
|
|
return EINVAL;
|
|
|
|
|
process->delete_perf_events_buffer();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-30 18:38:15 -03:00
|
|
|
}
|