summaryrefslogtreecommitdiff
path: root/sysdeps/bsd/prockernel.c
blob: ccd72c278135e958d4bc8150432a0ea5d22e33ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/* Copyright (C) 1998 Joshua Sled
   This file is part of LibGTop 1.0.

   Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.

   LibGTop is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.

   LibGTop is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

   You should have received a copy of the GNU General Public License
   along with LibGTop; see the file COPYING. If not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/prockernel.h>

#include <glibtop_suid.h>

#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#if (!defined __OpenBSD__) && (!defined __bsdi__)
#include <sys/user.h>
#endif
#if !defined(__bsdi__) && !(defined(__FreeBSD__) && defined(__alpha__)) && \
    !defined(__NetBSD__) 
#include <machine/pcb.h>
#endif

#include <unistd.h>
#include <fcntl.h>

#ifdef __FreeBSD__
#include <osreldate.h>
#endif

#ifdef __NetBSD__
#include <machine/vmparam.h>
#include <machine/pmap.h>
#ifdef __arm32__
#define	KERNBASE	KERNEL_BASE
#endif
#endif

#ifdef __NetBSD__
#include <machine/vmparam.h>
#include <machine/pmap.h>
#ifdef __arm32__
#define	KERNBASE	KERNEL_BASE
#endif
#endif


static const unsigned long _glibtop_sysdeps_proc_kernel_pstats =
(1L << GLIBTOP_PROC_KERNEL_MIN_FLT) +
(1L << GLIBTOP_PROC_KERNEL_MAJ_FLT) +
(1L << GLIBTOP_PROC_KERNEL_CMIN_FLT) +
(1L << GLIBTOP_PROC_KERNEL_CMAJ_FLT);

static const unsigned long _glibtop_sysdeps_proc_kernel_pcb =
(1L << GLIBTOP_PROC_KERNEL_KSTK_EIP) +
(1L << GLIBTOP_PROC_KERNEL_KSTK_ESP);

static const unsigned long _glibtop_sysdeps_proc_kernel_wchan =
(1L << GLIBTOP_PROC_KERNEL_NWCHAN) +
(1L << GLIBTOP_PROC_KERNEL_WCHAN);

/* Init function. */

void
glibtop_init_proc_kernel_p (glibtop *server)
{
	server->sysdeps.proc_kernel = _glibtop_sysdeps_proc_kernel_pstats |
		_glibtop_sysdeps_proc_kernel_pcb |
		_glibtop_sysdeps_proc_kernel_wchan;
}

void
glibtop_get_proc_kernel_p (glibtop *server,
			   glibtop_proc_kernel *buf,
			   pid_t pid)
{
	struct kinfo_proc *pinfo;
#if !(defined(__FreeBSD__) || defined(__FreeBSD_kernel__))
	struct user *u_addr = (struct user *)USRSTACK;
	struct pstats pstats;
	struct pcb pcb;
#endif
	int count;

	char filename [BUFSIZ];
	struct stat statb;

	glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_KERNEL), 0);

	memset (buf, 0, sizeof (glibtop_proc_kernel));

	if (server->sysdeps.proc_time == 0)
		return;

	/* It does not work for the swapper task. */
	if (pid == 0) return;

	/* Get the process information */
	pinfo = kvm_getprocs (server->machine.kd, KERN_PROC_PID, pid, &count);
	if ((pinfo == NULL) || (count != 1)) {
		glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid);
		return;
	}

#if (defined(__FreeBSD__) && (__FreeBSD_version >= 500013)) || defined(__FreeBSD_kernel__)

#define	PROC_WCHAN	ki_wchan
#define	PROC_WMESG	ki_wmesg
#define	PROC_WMESG	ki_wmesg

#else

#define	PROC_WCHAN	kp_proc.p_wchan
#define	PROC_WMESG	kp_proc.p_wmesg
#define	PROC_WMESG	kp_eproc.e_wmesg

#endif

#if !defined(__NetBSD__) || !defined(SACTIVE)
	buf->nwchan = (unsigned long) pinfo [0].PROC_WCHAN &~ KERNBASE;

	buf->flags |= (1L << GLIBTOP_PROC_KERNEL_NWCHAN);

	if (pinfo [0].PROC_WCHAN && pinfo [0].PROC_WMESG) {
		g_strlcpy (buf->wchan, pinfo [0].PROC_WMESG,
			 sizeof buf->wchan);
		buf->flags |= (1L << GLIBTOP_PROC_KERNEL_WCHAN);
	} else {
		buf->wchan [0] = 0;
	}
#endif

#if !(defined(__FreeBSD__) || defined(__FreeBSD_kernel__))

	/* Taken from `saveuser ()' in `/usr/src/bin/ps/ps.c'. */

	/* [FIXME]: /usr/include/sys/user.h tells me that the user area
	 *          may or may not be at the same kernel address in all
	 *          processes, but I don't see any way to get that address.
	 *          Since `ps' simply uses its own address, I think it's
	 *          safe to do this here, too. */

	/* NOTE: You need to mount the /proc filesystem to make
	 *       `kvm_uread' work. */

	sprintf (filename, "/proc/%d/mem", (int) pid);
	if (stat (filename, &statb)) return;

	glibtop_suid_enter (server);

#if !defined(__NetBSD__) || !defined(SACTIVE)
#ifdef __NetBSD__
	/* On NetBSD, there is no kvm_uread(), and kvm_read() always reads
	 * from kernel memory.  */

	if (kvm_read (server->machine.kd,
#else

	if ((pinfo [0].kp_proc.p_flag & P_INMEM) &&
	    kvm_uread (server->machine.kd, &(pinfo [0]).kp_proc,
#endif
		       (unsigned long) &u_addr->u_stats,
		       (char *) &pstats, sizeof (pstats)) == sizeof (pstats))
		{
			/*
			 * The u-area might be swapped out, and we can't get
			 * at it because we have a crashdump and no swap.
			 * If it's here fill in these fields, otherwise, just
			 * leave them 0.
			 */

			buf->min_flt = (guint64) pstats.p_ru.ru_minflt;
			buf->maj_flt = (guint64) pstats.p_ru.ru_majflt;
			buf->cmin_flt = (guint64) pstats.p_cru.ru_minflt;
			buf->cmaj_flt = (guint64) pstats.p_cru.ru_majflt;

			buf->flags |= _glibtop_sysdeps_proc_kernel_pstats;
		}

#ifdef __NetBSD__
	if (kvm_read (server->machine.kd,
#else
	if ((pinfo [0].kp_proc.p_flag & P_INMEM) &&
	    kvm_uread (server->machine.kd, &(pinfo [0]).kp_proc,
#endif
		       (unsigned long) &u_addr->u_pcb,
		       (char *) &pcb, sizeof (pcb)) == sizeof (pcb))
		{
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
#ifndef __alpha__
#if (__FreeBSD_version >= 300003) || defined(__FreeBSD_kernel__)
			buf->kstk_esp = (guint64) pcb.pcb_esp;
			buf->kstk_eip = (guint64) pcb.pcb_eip;
#else
			buf->kstk_esp = (guint64) pcb.pcb_ksp;
			buf->kstk_eip = (guint64) pcb.pcb_pc;
#endif
#else
			/*xxx FreeBSD/Alpha? */
#endif
#else
#ifdef __i386__
			buf->kstk_esp = (guint64) pcb.pcb_tss.tss_esp0;
#ifdef __bsdi__
			buf->kstk_eip = (guint64) pcb.pcb_tss.tss_eip;
#else
			buf->kstk_eip = (guint64) pcb.pcb_tss.__tss_eip;
#endif
#else
#if defined(__NetBSD__)
#if defined(__m68k__)
			buf->kstk_esp = (guint64) pcb.pcb_usp;
			buf->kstk_eip = (guint64) 0;
#elif defined(__x86_64__)
			buf->kstk_esp = (guint64) pcb.pcb_usersp;
			buf->kstk_eip = (guint64) 0;
#elif (defined(__arm32__) || defined(__powerpc__))
			buf->kstk_esp = (guint64) pcb.pcb_sp;
			buf->kstk_eip = (guint64) 0;
#elif defined(__mipsel__)
			buf->kstk_esp = (guint64) pcb.pcb_context[8];
			buf->kstk_eip = (guint64) 0;
#elif defined(__sparc__)
			buf->kstk_esp = (guint64) pcb.pcb_sp;
			buf->kstk_eip = (guint64) pcb.pcb_pc;
#elif defined(__alpha__)
			buf->kstk_esp = (guint64) pcb.pcb_context[9];
			buf->kstk_eip = (guint64) pcb.pcb_context[8];
#else
			/* provide some defaults for other platforms */
			buf->kstk_esp = (guint64) 0;
			buf->kstk_eip = (guint64) 0;
#endif /* ${MACHINE_ARCH} */
#endif /* __NetBSD__ */
			buf->flags |= _glibtop_sysdeps_proc_kernel_pcb;
#endif
#endif
		}
#endif

	/* Taken from `wchan ()' in `/usr/src/bin/ps/print.c'. */

	glibtop_suid_leave (server);

#else
	/* XXX: the code here was, quite frankly, junk, and almost
	 * certainly wrong - remove it all, leave these fields
	 * unpopulated, and give up until such time as the right
	 * code is produced for both FreeBSD 4.x and 5.x
	 */
	return;
#endif /* __FreeBSD__ */
}