| 1 | /*
|
|---|
| 2 | * kern/sys_ps.c - show kernel active processes and threads
|
|---|
| 3 | *
|
|---|
| 4 | * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless
|
|---|
| 5 | * Copyright (c) 2011,2012,2013,2014,2015 UPMC Sorbonne Universites
|
|---|
| 6 | *
|
|---|
| 7 | * This file is part of ALMOS-kernel.
|
|---|
| 8 | *
|
|---|
| 9 | * ALMOS-kernel is free software; you can redistribute it and/or modify it
|
|---|
| 10 | * under the terms of the GNU General Public License as published by
|
|---|
| 11 | * the Free Software Foundation; version 2.0 of the License.
|
|---|
| 12 | *
|
|---|
| 13 | * ALMOS-kernel is distributed in the hope that it will be useful, but
|
|---|
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU General Public License
|
|---|
| 19 | * along with ALMOS-kernel; if not, write to the Free Software Foundation,
|
|---|
| 20 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | #include <types.h>
|
|---|
| 24 | #include <task.h>
|
|---|
| 25 | #include <pid.h>
|
|---|
| 26 | #include <thread.h>
|
|---|
| 27 | #include <vmm.h>
|
|---|
| 28 | #include <errno.h>
|
|---|
| 29 | #include <utils.h>
|
|---|
| 30 | #include <rpc.h>
|
|---|
| 31 | #include <cluster.h>
|
|---|
| 32 |
|
|---|
| 33 | extern error_t ps_func(void *param);
|
|---|
| 34 |
|
|---|
| 35 | /* TODO: add remote support. */
|
|---|
| 36 | static error_t sys_ps_check_thread(pid_t pid, uint_t tid, struct thread_s **th_ptr)
|
|---|
| 37 | {
|
|---|
| 38 | struct task_s *task;
|
|---|
| 39 | struct thread_s *thread;
|
|---|
| 40 | cid_t location;
|
|---|
| 41 |
|
|---|
| 42 | if(pid == PID_MIN_LOCAL)
|
|---|
| 43 | return EINVAL;
|
|---|
| 44 |
|
|---|
| 45 | location = task_whereis(pid);
|
|---|
| 46 |
|
|---|
| 47 | tasks_manager_lock();
|
|---|
| 48 | if ( location == current_cid )
|
|---|
| 49 | {
|
|---|
| 50 | task = task_lookup(pid)->task;
|
|---|
| 51 |
|
|---|
| 52 | if((task == NULL) || (tid > task->max_order))
|
|---|
| 53 | return EINVAL;
|
|---|
| 54 |
|
|---|
| 55 | thread = task->th_tbl[tid];
|
|---|
| 56 |
|
|---|
| 57 | if((thread == NULL) ||
|
|---|
| 58 | (thread->signature != THREAD_ID) ||
|
|---|
| 59 | (thread->info.attr.key != tid))
|
|---|
| 60 | {
|
|---|
| 61 | return ESRCH;
|
|---|
| 62 | }
|
|---|
| 63 |
|
|---|
| 64 | *th_ptr = thread;
|
|---|
| 65 | return 0;
|
|---|
| 66 | }
|
|---|
| 67 | else
|
|---|
| 68 | {
|
|---|
| 69 | printk(WARNING, "%s: cluster %u can't execute this function on remote task (pid %u on cluster %u)\n", \
|
|---|
| 70 | __FUNCTION__, current_cid, pid, location);
|
|---|
| 71 | return ENOSYS;
|
|---|
| 72 | }
|
|---|
| 73 | tasks_manager_unlock();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /*TODO: use RPC_ARG_NULL instead of sending a useless variable.
|
|---|
| 77 | * It's not urgent, it's a minor change.
|
|---|
| 78 | */
|
|---|
| 79 | RPC_DECLARE(__ps_func, \
|
|---|
| 80 | RPC_RET( RPC_RET_PTR(error_t, err) ), \
|
|---|
| 81 | RPC_ARG( RPC_ARG_PTR(error_t, foo) ) \
|
|---|
| 82 | )
|
|---|
| 83 | {
|
|---|
| 84 | *err = ps_func(NULL);
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | int sys_ps(uint_t cmd, pid_t pid, uint_t tid)
|
|---|
| 88 | {
|
|---|
| 89 | cid_t next;
|
|---|
| 90 | error_t err;
|
|---|
| 91 | struct thread_s *thread;
|
|---|
| 92 | struct kernel_iter_s *kernel_iter;
|
|---|
| 93 |
|
|---|
| 94 | err = 0;
|
|---|
| 95 |
|
|---|
| 96 | switch(cmd)
|
|---|
| 97 | {
|
|---|
| 98 | case TASK_PS_TRACE_OFF:
|
|---|
| 99 | err = sys_ps_check_thread(pid, tid, &thread);
|
|---|
| 100 | if(err) goto fail_trace_off;
|
|---|
| 101 | thread->info.isTraced = false;
|
|---|
| 102 | cpu_wbflush();
|
|---|
| 103 | printk(INFO,"INFO: pid %d, tid %x, tracing is turned [OFF]\n", pid, tid);
|
|---|
| 104 | break;
|
|---|
| 105 |
|
|---|
| 106 | case TASK_PS_TRACE_ON:
|
|---|
| 107 | err = sys_ps_check_thread(pid, tid, &thread);
|
|---|
| 108 | if(err) goto fail_trace_on;
|
|---|
| 109 | thread->info.isTraced = true;
|
|---|
| 110 | cpu_wbflush();
|
|---|
| 111 | printk(INFO,"INFO: pid %d, tid %x, tracing is turned [ON]\n", pid, tid);
|
|---|
| 112 | break;
|
|---|
| 113 |
|
|---|
| 114 | case TASK_PS_SHOW:
|
|---|
| 115 | kernel_foreach_backward(kernel_iter, next)
|
|---|
| 116 | {
|
|---|
| 117 | RCPC( next, RPC_PRIO_PS, __ps_func,
|
|---|
| 118 | RPC_RECV( RPC_RECV_OBJ(err) ),
|
|---|
| 119 | RPC_SEND( RPC_SEND_OBJ(err) )
|
|---|
| 120 | );
|
|---|
| 121 | }
|
|---|
| 122 | break;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | fail_trace_on:
|
|---|
| 126 | fail_trace_off:
|
|---|
| 127 | current_thread->info.errno = err;
|
|---|
| 128 | return err;
|
|---|
| 129 | }
|
|---|