2
* This code relies heavily on the Darwin "ps" command, which is available
3
* from Apple in the adv_cmds portion of the Darwin distribution. The portions
4
* of this code which were included from that source are:
6
* Copyright (c) 1990, 1993, 1994
7
* The Regents of the University of California. All rights reserved.
9
* Redistribution and use in source and binary forms, with or without
10
* modification, are permitted provided that the following conditions
12
* 1. Redistributions of source code must retain the above copyright
13
* notice, this list of conditions and the following disclaimer.
14
* 2. Redistributions in binary form must reproduce the above copyright
15
* notice, this list of conditions and the following disclaimer in the
16
* documentation and/or other materials provided with the distribution.
17
* 3. All advertising materials mentioning features or use of this software
18
* must display the following acknowledgement:
19
* This product includes software developed by the University of
20
* California, Berkeley and its contributors.
21
* 4. Neither the name of the University nor the names of its contributors
22
* may be used to endorse or promote products derived from this software
23
* without specific prior written permission.
25
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37
* The portions of this code which were necessary to tie into the Perl
38
* Proc::ProcessTable module are:
40
* Copyright (c) 2003, Thomas R. Wyant, III
42
* and may be reused under the same terms as Perl itself.
47
#include <sys/types.h>
49
#include <mach/mach_init.h>
50
#include <mach/mach_port.h>
51
#include <mach/mach_traps.h>
52
#include <mach/mach_types.h>
53
#include <mach/shared_memory_server.h>
54
#include <mach/task.h>
55
#include <mach/thread_act.h>
56
#include <mach/time_value.h>
57
#include <mach/vm_map.h>
60
#include <sys/resource.h>
62
#include <sys/sysctl.h>
67
/* these are in sys/sysctl.h */
69
#define KI_PROC(ki) (&(ki)->ki_p->kp_proc)
70
#define KI_EPROC(ki) (&(ki)->ki_p->kp_eproc)
73
typedef struct thread_values {
74
struct thread_basic_info tb;
75
/* struct policy_infos schedinfo; */
77
struct policy_timeshare_info tshare;
78
struct policy_rr_info rr;
79
struct policy_fifo_info fifo;
84
struct timeval u_start;
91
typedef struct kinfo {
92
struct kinfo_proc *ki_p; /* proc structure */
93
struct usave ki_u; /* interesting parts of user */
94
char *ki_args; /* exec args */
95
char *ki_env; /* environment */
102
struct task_basic_info tasks_info;
103
struct task_thread_times_info times;
104
/* struct policy_infos schedinfo; */
106
struct policy_timeshare_info tshare;
107
struct policy_rr_info rr;
108
struct policy_fifo_info fifo;
112
thread_port_array_t thread_list;
113
thread_values_t *thval;
118
#define TIME_IN_MICROSECONDS 1
122
/* Note that FREE_BUFFERS is to be defined appropriately before
123
* DIE_HORRIBLY is used.
125
#define DIE_HORRIBLY(s) {FREE_BUFFERS perror (s); exit (1);}
127
#else /* def TESTING */
129
#define DIE_HORRIBLY(s) {FREE_BUFFERS perror (s); return;}
131
extern void bless_into_proc(char* format, char** fields, ...);
133
#ifdef TIME_IN_MICROSECONDS
134
static char *Format = "iiiiiiiiiiiiiiijjjllslsssss";
135
#else /* def TIME_IN_MICROSECONDS */
136
static char *Format = "iiiiiiiiiiiiiiilllllslsssss";
137
#endif /* def TIME_IN_MICROSECONDS */
139
static char *Fields[] = {
140
"pid", /* Process ID */
142
"ppid", /* Parent pid */
144
"pgrp", /* Process group leader */
146
"uid", /* real uid */
148
"gid", /* real gid */
150
"euid", /* effective uid */
152
"egid", /* effective gid */
154
"suid", /* saved uid */
156
"sgid", /* saved gid */
158
"priority", /* proiority */
159
# define F_PRIORITY 9
160
"size", /* virtual size (Kbytes) */
162
"rss", /* resident set size (Kbytes) */
164
"flags", /* process flags */
166
"nice", /* nice for CPU usage */
168
"sess", /* session pointer */
170
"time", /* total time (system + user, centi- or microseconds, depending) */
171
# define F_CPTICKS 15
172
"stime", /* system time (centi- or micrseconds, depending) */
174
"utime", /* user time (centi- or microseconds, depending) */
176
"start", /* Start time in seconds since epoch */
178
"wchan", /* Wait channel (addr of system call) */
182
"ttynum", /* device number for tty, or -1 */
184
"pctcpu", /* Percent cpu as string representing float */
186
"pctmem", /* Percent memory as string representing float */
188
"state", /* current run state (e.g. sleep, wait ...) */
190
"cmndline", /* entire command line */
191
# define F_CMNDLINE 25
192
"fname", /* filename */
194
# define F_LASTFIELD 26
197
#endif /* def TESTING */
200
static char *States[] = {
201
"", "idle", "run", "sleep", "stop", "zombie"