2020-07-30 23:38:15 +02:00
|
|
|
/*
|
2021-01-11 09:52:18 +01:00
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
2020-07-30 23:38:15 +02:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-07-30 23:38:15 +02:00
|
|
|
*/
|
|
|
|
|
|
2021-08-22 14:51:04 +02:00
|
|
|
#include <Kernel/Coredump.h>
|
2021-05-07 01:38:50 -07:00
|
|
|
#include <Kernel/PerformanceManager.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
#include <Kernel/Process.h>
|
2022-01-29 13:08:37 +01:00
|
|
|
#include <Kernel/Scheduler.h>
|
2021-05-13 22:15:13 +02:00
|
|
|
#include <Kernel/Time/TimeManagement.h>
|
2020-07-30 23:38:15 +02:00
|
|
|
|
|
|
|
|
namespace Kernel {
|
|
|
|
|
|
2021-03-02 17:19:35 +01:00
|
|
|
bool g_profiling_all_threads;
|
2021-04-25 23:42:36 +02:00
|
|
|
PerformanceEventBuffer* g_global_perf_events;
|
2021-05-14 08:10:43 +02:00
|
|
|
u64 g_profiling_event_mask;
|
2021-03-02 17:19:35 +01:00
|
|
|
|
2022-02-18 22:12:35 +01:00
|
|
|
// NOTE: event_mask needs to be passed as a pointer as u64
|
|
|
|
|
// does not fit into a register on 32bit architectures.
|
|
|
|
|
ErrorOr<FlatPtr> Process::sys$profiling_enable(pid_t pid, Userspace<u64 const*> userspace_event_mask)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_no_promises());
|
2021-03-02 17:19:35 +01:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const event_mask = TRY(copy_typed_from_user(userspace_event_mask));
|
2022-08-20 21:55:55 -04:00
|
|
|
return profiling_enable(pid, event_mask);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NOTE: This second entrypoint exists to allow the kernel to invoke the syscall to enable boot profiling.
|
|
|
|
|
ErrorOr<FlatPtr> Process::profiling_enable(pid_t pid, u64 event_mask)
|
|
|
|
|
{
|
|
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2022-02-18 22:12:35 +01:00
|
|
|
|
2021-03-02 17:19:35 +01:00
|
|
|
if (pid == -1) {
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
2021-03-02 17:19:35 +01:00
|
|
|
return EPERM;
|
|
|
|
|
ScopedCritical critical;
|
2021-05-30 16:24:53 +02:00
|
|
|
g_profiling_event_mask = PERF_EVENT_PROCESS_CREATE | PERF_EVENT_THREAD_CREATE | PERF_EVENT_MMAP;
|
2022-02-06 01:25:32 +01:00
|
|
|
if (g_global_perf_events) {
|
2021-03-02 17:19:35 +01:00
|
|
|
g_global_perf_events->clear();
|
2022-02-06 01:25:32 +01:00
|
|
|
} else {
|
2021-03-02 17:19:35 +01:00
|
|
|
g_global_perf_events = PerformanceEventBuffer::try_create_with_size(32 * MiB).leak_ptr();
|
2022-02-06 01:25:32 +01:00
|
|
|
if (!g_global_perf_events) {
|
|
|
|
|
g_profiling_event_mask = 0;
|
|
|
|
|
return ENOMEM;
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-07 01:38:50 -07:00
|
|
|
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-05-14 11:33:34 +03:00
|
|
|
if (!TimeManagement::the().enable_profile_timer())
|
|
|
|
|
return ENOTSUP;
|
2021-05-15 21:25:54 +02:00
|
|
|
g_profiling_all_threads = true;
|
2021-05-14 08:10:43 +02:00
|
|
|
PerformanceManager::add_process_created_event(*Scheduler::colonel());
|
2021-04-25 23:42:36 +02:00
|
|
|
Process::for_each([](auto& process) {
|
2021-05-07 01:38:50 -07:00
|
|
|
PerformanceManager::add_process_created_event(process);
|
2021-04-25 23:42:36 +02:00
|
|
|
return IterationDecision::Continue;
|
|
|
|
|
});
|
2021-05-30 16:24:53 +02:00
|
|
|
g_profiling_event_mask = event_mask;
|
2021-03-02 17:19:35 +01:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 20:04:56 -06:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!process)
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
2020-07-30 23:38:15 +02:00
|
|
|
if (process->is_dead())
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
auto profile_process_credentials = process->credentials();
|
|
|
|
|
if (!credentials->is_superuser() && profile_process_credentials->uid() != credentials->euid())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-05-30 16:24:53 +02:00
|
|
|
g_profiling_event_mask = PERF_EVENT_PROCESS_CREATE | PERF_EVENT_THREAD_CREATE | PERF_EVENT_MMAP;
|
2021-05-23 22:16:30 +02:00
|
|
|
process->set_profiling(true);
|
2021-05-24 08:59:30 +02:00
|
|
|
if (!process->create_perf_events_buffer_if_needed()) {
|
|
|
|
|
process->set_profiling(false);
|
2021-03-02 16:55:54 +01:00
|
|
|
return ENOMEM;
|
2021-05-24 08:59:30 +02:00
|
|
|
}
|
2021-05-30 16:24:53 +02:00
|
|
|
g_profiling_event_mask = event_mask;
|
2021-05-24 08:59:30 +02:00
|
|
|
if (!TimeManagement::the().enable_profile_timer()) {
|
|
|
|
|
process->set_profiling(false);
|
2021-05-14 11:33:34 +03:00
|
|
|
return ENOTSUP;
|
2021-05-24 08:59:30 +02:00
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$profiling_disable(pid_t pid)
|
2020-07-30 23:38:15 +02:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_no_promises());
|
2021-04-18 21:10:05 -07:00
|
|
|
|
2021-03-02 17:19:35 +01:00
|
|
|
if (pid == -1) {
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
2021-03-02 17:19:35 +01:00
|
|
|
return EPERM;
|
|
|
|
|
ScopedCritical critical;
|
2021-05-14 11:33:34 +03:00
|
|
|
if (!TimeManagement::the().disable_profile_timer())
|
|
|
|
|
return ENOTSUP;
|
2021-03-02 17:19:35 +01:00
|
|
|
g_profiling_all_threads = false;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-01 20:04:56 -06:00
|
|
|
auto process = Process::from_pid(pid);
|
2020-07-30 23:38:15 +02:00
|
|
|
if (!process)
|
2021-03-01 13:49:16 +01:00
|
|
|
return ESRCH;
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
auto profile_process_credentials = process->credentials();
|
|
|
|
|
if (!credentials->is_superuser() && profile_process_credentials->uid() != credentials->euid())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EPERM;
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-01-11 09:52:18 +01:00
|
|
|
if (!process->is_profiling())
|
2021-03-01 13:49:16 +01:00
|
|
|
return EINVAL;
|
2021-05-14 11: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 23:38:15 +02:00
|
|
|
process->set_profiling(false);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-08 00:51:39 +01:00
|
|
|
ErrorOr<FlatPtr> Process::sys$profiling_free_buffer(pid_t pid)
|
2021-04-18 21:10:05 -07:00
|
|
|
{
|
2022-08-17 21:03:04 +01:00
|
|
|
VERIFY_PROCESS_BIG_LOCK_ACQUIRED(this);
|
2021-12-29 01:11:45 -08:00
|
|
|
TRY(require_no_promises());
|
2021-04-18 21:10:05 -07:00
|
|
|
|
|
|
|
|
if (pid == -1) {
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
if (!credentials->is_superuser())
|
2021-04-18 21:10:05 -07:00
|
|
|
return EPERM;
|
|
|
|
|
|
|
|
|
|
OwnPtr<PerformanceEventBuffer> perf_events;
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
|
ScopedCritical critical;
|
|
|
|
|
|
2021-05-30 21:09:23 +04:30
|
|
|
perf_events = adopt_own_if_nonnull(g_global_perf_events);
|
2021-04-18 21:10:05 -07:00
|
|
|
g_global_perf_events = nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
auto process = Process::from_pid(pid);
|
|
|
|
|
if (!process)
|
|
|
|
|
return ESRCH;
|
2022-08-20 18:21:01 -04:00
|
|
|
auto credentials = this->credentials();
|
|
|
|
|
auto profile_process_credentials = process->credentials();
|
|
|
|
|
if (!credentials->is_superuser() && profile_process_credentials->uid() != credentials->euid())
|
2021-04-18 21:10:05 -07:00
|
|
|
return EPERM;
|
2021-08-22 01:49:22 +02:00
|
|
|
SpinlockLocker lock(g_profiling_lock);
|
2021-04-18 21:10:05 -07:00
|
|
|
if (process->is_profiling())
|
|
|
|
|
return EINVAL;
|
|
|
|
|
process->delete_perf_events_buffer();
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2020-07-30 23:38:15 +02:00
|
|
|
}
|