xref: /illumos-gate/usr/src/lib/libkvm/common/test.c (revision 1da57d55)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1996-1998 by Sun Microsystems, Inc.
24*7c478bd9Sstevel@tonic-gate  * All rights reserved.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #include <unistd.h>
28*7c478bd9Sstevel@tonic-gate #include <string.h>
29*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <ctype.h>
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate #include "kvm.h"
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <nlist.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/fcntl.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/user.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/elf.h>
42*7c478bd9Sstevel@tonic-gate 
43*7c478bd9Sstevel@tonic-gate #ifdef __sparc
44*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>		/* for STACK_BIAS */
45*7c478bd9Sstevel@tonic-gate #else
46*7c478bd9Sstevel@tonic-gate #define	STACK_BIAS		0
47*7c478bd9Sstevel@tonic-gate #endif
48*7c478bd9Sstevel@tonic-gate 
49*7c478bd9Sstevel@tonic-gate kvm_t *cookie;
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate struct proc *tst_getproc(pid_t);
52*7c478bd9Sstevel@tonic-gate struct proc *tst_nextproc(void);
53*7c478bd9Sstevel@tonic-gate struct user *tst_getu(struct proc *);
54*7c478bd9Sstevel@tonic-gate int tst_setproc(void);
55*7c478bd9Sstevel@tonic-gate int tst_getcmd(struct proc *, struct user *);
56*7c478bd9Sstevel@tonic-gate void tst_segkp(void);
57*7c478bd9Sstevel@tonic-gate void tst_nlist(struct nlist nl[]);
58*7c478bd9Sstevel@tonic-gate void tst_open(char *, char *, char *, int);
59*7c478bd9Sstevel@tonic-gate void tst_close(void);
60*7c478bd9Sstevel@tonic-gate ssize_t tst_read(uintptr_t, void *, size_t);
61*7c478bd9Sstevel@tonic-gate ssize_t tst_write(uintptr_t, void *, size_t);
62*7c478bd9Sstevel@tonic-gate int tst_getcmd(struct proc *, struct user *);
63*7c478bd9Sstevel@tonic-gate void tst_segkvp(void);
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate char *name;
66*7c478bd9Sstevel@tonic-gate char *core;
67*7c478bd9Sstevel@tonic-gate char *swap;
68*7c478bd9Sstevel@tonic-gate int wflag;
69*7c478bd9Sstevel@tonic-gate 
70*7c478bd9Sstevel@tonic-gate struct nlist nl[] = {
71*7c478bd9Sstevel@tonic-gate 	{"free"},
72*7c478bd9Sstevel@tonic-gate 	{"fragtbl"},
73*7c478bd9Sstevel@tonic-gate 	{"freemem"},
74*7c478bd9Sstevel@tonic-gate 	{"allthreads"},
75*7c478bd9Sstevel@tonic-gate 	{"nbuckets"},
76*7c478bd9Sstevel@tonic-gate 	{"cputype"},
77*7c478bd9Sstevel@tonic-gate 	{0}
78*7c478bd9Sstevel@tonic-gate };
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate int
main(int argc,char * argv[],char * envp[])81*7c478bd9Sstevel@tonic-gate main(int argc, char *argv[], char *envp[])
82*7c478bd9Sstevel@tonic-gate {
83*7c478bd9Sstevel@tonic-gate 	int c, errflg = 0;
84*7c478bd9Sstevel@tonic-gate 	long xx;
85*7c478bd9Sstevel@tonic-gate 	struct nlist *nlp;
86*7c478bd9Sstevel@tonic-gate 	struct proc *proc;
87*7c478bd9Sstevel@tonic-gate 	struct user *u;
88*7c478bd9Sstevel@tonic-gate 	int envc, ccnt;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	for (envc = 0; *envp++ != NULL; envc++)
91*7c478bd9Sstevel@tonic-gate 		continue;
92*7c478bd9Sstevel@tonic-gate 	envp -= 2;
93*7c478bd9Sstevel@tonic-gate 	ccnt = (*envp - *argv) + strlen(*envp) + 1;
94*7c478bd9Sstevel@tonic-gate 	printf("pid %d:: %d args; %d envs; %d chars (%p - %p)\n",
95*7c478bd9Sstevel@tonic-gate 	    getpid(), argc, envc, ccnt,
96*7c478bd9Sstevel@tonic-gate 	    &argv[0], *envp + strlen(*envp));
97*7c478bd9Sstevel@tonic-gate 
98*7c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "w")) != EOF)
99*7c478bd9Sstevel@tonic-gate 		switch (c) {
100*7c478bd9Sstevel@tonic-gate 		case 'w':
101*7c478bd9Sstevel@tonic-gate 			wflag++;
102*7c478bd9Sstevel@tonic-gate 			break;
103*7c478bd9Sstevel@tonic-gate 		case '?':
104*7c478bd9Sstevel@tonic-gate 			errflg++;
105*7c478bd9Sstevel@tonic-gate 		}
106*7c478bd9Sstevel@tonic-gate 	if (errflg) {
107*7c478bd9Sstevel@tonic-gate 		fprintf(stderr, "usage: %s [-w] [name] [core] [swap]\n",
108*7c478bd9Sstevel@tonic-gate 		    argv[0]);
109*7c478bd9Sstevel@tonic-gate 		return (2);
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
112*7c478bd9Sstevel@tonic-gate 		name = argv[optind++];
113*7c478bd9Sstevel@tonic-gate 		if (*name == '\0')
114*7c478bd9Sstevel@tonic-gate 			name = NULL;
115*7c478bd9Sstevel@tonic-gate 	} else
116*7c478bd9Sstevel@tonic-gate 		name = NULL;
117*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
118*7c478bd9Sstevel@tonic-gate 		core = argv[optind++];
119*7c478bd9Sstevel@tonic-gate 		if (*core == '\0')
120*7c478bd9Sstevel@tonic-gate 			core = NULL;
121*7c478bd9Sstevel@tonic-gate 	} else
122*7c478bd9Sstevel@tonic-gate 		core = NULL;
123*7c478bd9Sstevel@tonic-gate 	if (optind < argc) {
124*7c478bd9Sstevel@tonic-gate 		swap = argv[optind++];
125*7c478bd9Sstevel@tonic-gate 		if (*swap == '\0')
126*7c478bd9Sstevel@tonic-gate 			swap = NULL;
127*7c478bd9Sstevel@tonic-gate 	} else
128*7c478bd9Sstevel@tonic-gate 		swap = NULL;
129*7c478bd9Sstevel@tonic-gate 
130*7c478bd9Sstevel@tonic-gate 	tst_open(name, core, swap, (wflag ? O_RDWR : O_RDONLY));
131*7c478bd9Sstevel@tonic-gate 	if (cookie == NULL)
132*7c478bd9Sstevel@tonic-gate 		return (1);
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate 	tst_nlist(nl);
135*7c478bd9Sstevel@tonic-gate 
136*7c478bd9Sstevel@tonic-gate 	for (nlp = nl; nlp[0].n_type != 0; nlp++)
137*7c478bd9Sstevel@tonic-gate 		tst_read(nlp[0].n_value, &xx, sizeof (xx));
138*7c478bd9Sstevel@tonic-gate 
139*7c478bd9Sstevel@tonic-gate 	while ((proc = tst_nextproc()) != NULL) {
140*7c478bd9Sstevel@tonic-gate 		struct pid pid;
141*7c478bd9Sstevel@tonic-gate 		if (kvm_read(cookie, (uintptr_t)proc->p_pidp, &pid,
142*7c478bd9Sstevel@tonic-gate 		    sizeof (pid)) != sizeof (pid)) {
143*7c478bd9Sstevel@tonic-gate 			printf("ERROR: couldn't get pid\n");
144*7c478bd9Sstevel@tonic-gate 			break;
145*7c478bd9Sstevel@tonic-gate 		}
146*7c478bd9Sstevel@tonic-gate 		tst_getproc(pid.pid_id);
147*7c478bd9Sstevel@tonic-gate 	}
148*7c478bd9Sstevel@tonic-gate 
149*7c478bd9Sstevel@tonic-gate 	tst_setproc();
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	while ((proc = tst_nextproc()) != NULL) {
152*7c478bd9Sstevel@tonic-gate 		if ((u = tst_getu(proc)) != NULL)
153*7c478bd9Sstevel@tonic-gate 			(void) tst_getcmd(proc, u);
154*7c478bd9Sstevel@tonic-gate 	}
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	tst_segkp();
157*7c478bd9Sstevel@tonic-gate 	tst_close();
158*7c478bd9Sstevel@tonic-gate 
159*7c478bd9Sstevel@tonic-gate 	return (0);
160*7c478bd9Sstevel@tonic-gate }
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate void
tst_open(char * namelist,char * corefile,char * swapfile,int flag)163*7c478bd9Sstevel@tonic-gate tst_open(char *namelist, char *corefile, char *swapfile, int flag)
164*7c478bd9Sstevel@tonic-gate {
165*7c478bd9Sstevel@tonic-gate 	printf("kvm_open(%s, %s, %s, %s)\n",
166*7c478bd9Sstevel@tonic-gate 	    (namelist == NULL) ? "LIVE_KERNEL" : namelist,
167*7c478bd9Sstevel@tonic-gate 	    (corefile == NULL) ? "LIVE_KERNEL" : corefile,
168*7c478bd9Sstevel@tonic-gate 	    (swapfile == NULL) ?
169*7c478bd9Sstevel@tonic-gate 		((corefile == NULL) ? "LIVE_KERNEL" : "(none)") : swapfile,
170*7c478bd9Sstevel@tonic-gate 	    (flag == O_RDONLY) ? "O_RDONLY" : ((flag == O_RDWR) ?
171*7c478bd9Sstevel@tonic-gate 	    "O_RDWR" : "???"));
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	if ((cookie = kvm_open(namelist, corefile,
174*7c478bd9Sstevel@tonic-gate 	    swapfile, flag, "libkvm test")) == NULL)
175*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_open returned %p\n", cookie);
176*7c478bd9Sstevel@tonic-gate }
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate void
tst_close(void)179*7c478bd9Sstevel@tonic-gate tst_close(void)
180*7c478bd9Sstevel@tonic-gate {
181*7c478bd9Sstevel@tonic-gate 	int i;
182*7c478bd9Sstevel@tonic-gate 
183*7c478bd9Sstevel@tonic-gate 	printf("kvm_close()\n");
184*7c478bd9Sstevel@tonic-gate 	if ((i = kvm_close(cookie)) != 0)
185*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_close returned %d\n", i);
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate void
tst_nlist(struct nlist nl[])189*7c478bd9Sstevel@tonic-gate tst_nlist(struct nlist nl[])
190*7c478bd9Sstevel@tonic-gate {
191*7c478bd9Sstevel@tonic-gate 	int i;
192*7c478bd9Sstevel@tonic-gate 	char *t, *s;
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	printf("kvm_nlist([nl])\n");
195*7c478bd9Sstevel@tonic-gate 	if ((i = kvm_nlist(cookie, nl)) != 0)
196*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_nlist returned %d\n", i);
197*7c478bd9Sstevel@tonic-gate 	for (i = 0; nl[i].n_name != 0 && nl[i].n_name[0] != '\0'; i++) {
198*7c478bd9Sstevel@tonic-gate 		/*
199*7c478bd9Sstevel@tonic-gate 		 * Debug:
200*7c478bd9Sstevel@tonic-gate 		 * n_value gets filled in with st_value,
201*7c478bd9Sstevel@tonic-gate 		 * n_type gets filled in w/ELF32_ST_TYPE(sym->st_info)
202*7c478bd9Sstevel@tonic-gate 		 * n_scnum gets filled in w/st_shndx
203*7c478bd9Sstevel@tonic-gate 		 */
204*7c478bd9Sstevel@tonic-gate 		switch (nl[i].n_type) {
205*7c478bd9Sstevel@tonic-gate 		case STT_NOTYPE:
206*7c478bd9Sstevel@tonic-gate 			t = "NOTYPE";
207*7c478bd9Sstevel@tonic-gate 			break;
208*7c478bd9Sstevel@tonic-gate 		case STT_OBJECT:
209*7c478bd9Sstevel@tonic-gate 			t = "OBJECT";
210*7c478bd9Sstevel@tonic-gate 			break;
211*7c478bd9Sstevel@tonic-gate 		case STT_FUNC:
212*7c478bd9Sstevel@tonic-gate 			t = "FUNC";
213*7c478bd9Sstevel@tonic-gate 			break;
214*7c478bd9Sstevel@tonic-gate 		case STT_SECTION:
215*7c478bd9Sstevel@tonic-gate 			t = "SECTION";
216*7c478bd9Sstevel@tonic-gate 			break;
217*7c478bd9Sstevel@tonic-gate 		case STT_FILE:
218*7c478bd9Sstevel@tonic-gate 			t = "FILE";
219*7c478bd9Sstevel@tonic-gate 			break;
220*7c478bd9Sstevel@tonic-gate 		case STT_NUM:
221*7c478bd9Sstevel@tonic-gate 			t = "NUM";
222*7c478bd9Sstevel@tonic-gate 			break;
223*7c478bd9Sstevel@tonic-gate 		default:
224*7c478bd9Sstevel@tonic-gate 			t = "???";
225*7c478bd9Sstevel@tonic-gate 		}
226*7c478bd9Sstevel@tonic-gate 
227*7c478bd9Sstevel@tonic-gate 		switch ((unsigned)nl[i].n_scnum) {
228*7c478bd9Sstevel@tonic-gate 			static char strbuf[40];
229*7c478bd9Sstevel@tonic-gate 
230*7c478bd9Sstevel@tonic-gate 		case SHN_UNDEF:
231*7c478bd9Sstevel@tonic-gate 			s = "UNDEF";
232*7c478bd9Sstevel@tonic-gate 			break;
233*7c478bd9Sstevel@tonic-gate 		case SHN_LORESERVE:
234*7c478bd9Sstevel@tonic-gate 			s = "LORESERVE";
235*7c478bd9Sstevel@tonic-gate 			break;
236*7c478bd9Sstevel@tonic-gate 		case SHN_ABS:
237*7c478bd9Sstevel@tonic-gate 			s = "ABS";
238*7c478bd9Sstevel@tonic-gate 			break;
239*7c478bd9Sstevel@tonic-gate 		case SHN_COMMON:
240*7c478bd9Sstevel@tonic-gate 			s = "COMMON";
241*7c478bd9Sstevel@tonic-gate 			break;
242*7c478bd9Sstevel@tonic-gate 		case SHN_HIRESERVE:
243*7c478bd9Sstevel@tonic-gate 			s = "HIRESERVE";
244*7c478bd9Sstevel@tonic-gate 			break;
245*7c478bd9Sstevel@tonic-gate 		default:
246*7c478bd9Sstevel@tonic-gate 			(void) sprintf(strbuf, "unknown (%d)", nl[i].n_scnum);
247*7c478bd9Sstevel@tonic-gate 			s = strbuf;
248*7c478bd9Sstevel@tonic-gate 			break;
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 
251*7c478bd9Sstevel@tonic-gate 		printf("%s: %lx (%s, %s)\n",
252*7c478bd9Sstevel@tonic-gate 		    nl[i].n_name, nl[i].n_value, s, t);
253*7c478bd9Sstevel@tonic-gate 	}
254*7c478bd9Sstevel@tonic-gate }
255*7c478bd9Sstevel@tonic-gate 
256*7c478bd9Sstevel@tonic-gate ssize_t
tst_read(uintptr_t addr,void * buf,size_t nbytes)257*7c478bd9Sstevel@tonic-gate tst_read(uintptr_t addr, void *buf, size_t nbytes)
258*7c478bd9Sstevel@tonic-gate {
259*7c478bd9Sstevel@tonic-gate 	ssize_t e;
260*7c478bd9Sstevel@tonic-gate 	int i;
261*7c478bd9Sstevel@tonic-gate 	char *b;
262*7c478bd9Sstevel@tonic-gate 
263*7c478bd9Sstevel@tonic-gate 	printf("kvm_read(%lx, [buf], %lu)\n", addr, nbytes);
264*7c478bd9Sstevel@tonic-gate 	if ((e = kvm_read(cookie, addr, buf, nbytes)) != nbytes)
265*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_read returned %ld instead of %lu\n",
266*7c478bd9Sstevel@tonic-gate 		    e, nbytes);
267*7c478bd9Sstevel@tonic-gate 	for (b = buf, i = 0; i < nbytes; b++, i++)
268*7c478bd9Sstevel@tonic-gate 		printf("%lx: %02x (%04o)\n", addr + i,
269*7c478bd9Sstevel@tonic-gate 		    *b & 0xff, *b & 0xff);
270*7c478bd9Sstevel@tonic-gate 
271*7c478bd9Sstevel@tonic-gate 	return (e);
272*7c478bd9Sstevel@tonic-gate }
273*7c478bd9Sstevel@tonic-gate 
274*7c478bd9Sstevel@tonic-gate ssize_t
tst_write(uintptr_t addr,void * buf,size_t nbytes)275*7c478bd9Sstevel@tonic-gate tst_write(uintptr_t addr, void *buf, size_t nbytes)
276*7c478bd9Sstevel@tonic-gate {
277*7c478bd9Sstevel@tonic-gate 	ssize_t e;
278*7c478bd9Sstevel@tonic-gate 	ssize_t i;
279*7c478bd9Sstevel@tonic-gate 	void *b;
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	printf("kvm_write(%lx, [buf], %lu)\n", addr, nbytes);
282*7c478bd9Sstevel@tonic-gate 	if ((e = kvm_write(cookie, addr, buf, nbytes)) != nbytes)
283*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_write returned %ld instead of %lu\n",
284*7c478bd9Sstevel@tonic-gate 		    e, nbytes);
285*7c478bd9Sstevel@tonic-gate 	if ((b = malloc(nbytes)) == 0)
286*7c478bd9Sstevel@tonic-gate 		printf("ERROR: malloc for readback failed\n");
287*7c478bd9Sstevel@tonic-gate 	else {
288*7c478bd9Sstevel@tonic-gate 		if ((i = kvm_read(cookie, addr, b, nbytes)) != nbytes)
289*7c478bd9Sstevel@tonic-gate 			printf("ERROR: readback returned %ld\n", i);
290*7c478bd9Sstevel@tonic-gate 		else if (memcmp(b, buf, nbytes))
291*7c478bd9Sstevel@tonic-gate 			printf("ERROR: write check failed!\n");
292*7c478bd9Sstevel@tonic-gate 		(void) free(b);
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 	return (e);
295*7c478bd9Sstevel@tonic-gate }
296*7c478bd9Sstevel@tonic-gate 
297*7c478bd9Sstevel@tonic-gate struct proc *
tst_getproc(pid_t pid)298*7c478bd9Sstevel@tonic-gate tst_getproc(pid_t pid)
299*7c478bd9Sstevel@tonic-gate {
300*7c478bd9Sstevel@tonic-gate 	struct proc *proc;
301*7c478bd9Sstevel@tonic-gate 	struct pid pidbuf;
302*7c478bd9Sstevel@tonic-gate 
303*7c478bd9Sstevel@tonic-gate 	printf("kvm_getproc(%d)\n", pid);
304*7c478bd9Sstevel@tonic-gate 	if ((proc = kvm_getproc(cookie, pid)) == NULL) {
305*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_getproc returned NULL\n");
306*7c478bd9Sstevel@tonic-gate 		return (proc);
307*7c478bd9Sstevel@tonic-gate 	}
308*7c478bd9Sstevel@tonic-gate 
309*7c478bd9Sstevel@tonic-gate 	if (kvm_read(cookie, (uintptr_t)proc->p_pidp, &pidbuf,
310*7c478bd9Sstevel@tonic-gate 	    sizeof (pidbuf)) != sizeof (pidbuf)) {
311*7c478bd9Sstevel@tonic-gate 		printf("ERROR: couldn't get pid\n");
312*7c478bd9Sstevel@tonic-gate 		return (proc);
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 
315*7c478bd9Sstevel@tonic-gate 	printf("p_pid: %d\n", pidbuf.pid_id);
316*7c478bd9Sstevel@tonic-gate 	return (proc);
317*7c478bd9Sstevel@tonic-gate }
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate struct proc *
tst_nextproc(void)320*7c478bd9Sstevel@tonic-gate tst_nextproc(void)
321*7c478bd9Sstevel@tonic-gate {
322*7c478bd9Sstevel@tonic-gate 	struct proc *proc;
323*7c478bd9Sstevel@tonic-gate 	struct pid pidbuf;
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate 	printf("kvm_nextproc()\n");
326*7c478bd9Sstevel@tonic-gate 	if ((proc = kvm_nextproc(cookie)) == NULL) {
327*7c478bd9Sstevel@tonic-gate 		printf("kvm_nextproc returned NULL\n");
328*7c478bd9Sstevel@tonic-gate 		return (proc);
329*7c478bd9Sstevel@tonic-gate 	}
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	/*
332*7c478bd9Sstevel@tonic-gate 	 * p_pid is now a macro which turns into a ptr dereference;
333*7c478bd9Sstevel@tonic-gate 	 * must do a kvm_read to get contents.
334*7c478bd9Sstevel@tonic-gate 	 */
335*7c478bd9Sstevel@tonic-gate 	if (kvm_read(cookie, (u_long)proc->p_pidp, (char *)&pidbuf,
336*7c478bd9Sstevel@tonic-gate 	    sizeof (struct pid)) != sizeof (struct pid)) {
337*7c478bd9Sstevel@tonic-gate 		printf("ERROR: couldn't get pid\n");
338*7c478bd9Sstevel@tonic-gate 	}
339*7c478bd9Sstevel@tonic-gate 	printf("p_pid: %d\n", pidbuf.pid_id);
340*7c478bd9Sstevel@tonic-gate 
341*7c478bd9Sstevel@tonic-gate 	return (proc);
342*7c478bd9Sstevel@tonic-gate }
343*7c478bd9Sstevel@tonic-gate 
344*7c478bd9Sstevel@tonic-gate int
tst_setproc(void)345*7c478bd9Sstevel@tonic-gate tst_setproc(void)
346*7c478bd9Sstevel@tonic-gate {
347*7c478bd9Sstevel@tonic-gate 	int i;
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	printf("kvm_setproc()\n");
350*7c478bd9Sstevel@tonic-gate 	if ((i = kvm_setproc(cookie)) != 0)
351*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_setproc returned %d\n", i);
352*7c478bd9Sstevel@tonic-gate 	return (i);
353*7c478bd9Sstevel@tonic-gate }
354*7c478bd9Sstevel@tonic-gate 
355*7c478bd9Sstevel@tonic-gate struct user *
tst_getu(struct proc * proc)356*7c478bd9Sstevel@tonic-gate tst_getu(struct proc *proc)
357*7c478bd9Sstevel@tonic-gate {
358*7c478bd9Sstevel@tonic-gate 	register int e;
359*7c478bd9Sstevel@tonic-gate 	struct proc tp;
360*7c478bd9Sstevel@tonic-gate 	struct user *u;
361*7c478bd9Sstevel@tonic-gate 	struct pid pidbuf;
362*7c478bd9Sstevel@tonic-gate 
363*7c478bd9Sstevel@tonic-gate 	if (kvm_read(cookie, (uintptr_t)proc->p_pidp, &pidbuf,
364*7c478bd9Sstevel@tonic-gate 	    sizeof (pidbuf)) != sizeof (pidbuf))
365*7c478bd9Sstevel@tonic-gate 		printf("ERROR: couldn't get pid\n");
366*7c478bd9Sstevel@tonic-gate 
367*7c478bd9Sstevel@tonic-gate 	printf("kvm_getu(pid:%d)\n", pidbuf.pid_id);
368*7c478bd9Sstevel@tonic-gate 	if ((u = kvm_getu(cookie, proc)) == NULL)
369*7c478bd9Sstevel@tonic-gate 		printf("ERROR: kvm_getu returned NULL\n");
370*7c478bd9Sstevel@tonic-gate 	return (u);
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate static void
safe_printf(const char * s)374*7c478bd9Sstevel@tonic-gate safe_printf(const char *s)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ], *p;
377*7c478bd9Sstevel@tonic-gate 
378*7c478bd9Sstevel@tonic-gate 	(void) strncpy(buf, s, BUFSIZ - 1);
379*7c478bd9Sstevel@tonic-gate 	buf[BUFSIZ - 1] = '\0';
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	for (p = buf; *p != '\0'; p++) {
382*7c478bd9Sstevel@tonic-gate 		if (!isprint(*p))
383*7c478bd9Sstevel@tonic-gate 			*p = ' ';
384*7c478bd9Sstevel@tonic-gate 	}
385*7c478bd9Sstevel@tonic-gate 
386*7c478bd9Sstevel@tonic-gate 	(void) printf("\"%s\"\n", buf);
387*7c478bd9Sstevel@tonic-gate }
388*7c478bd9Sstevel@tonic-gate 
389*7c478bd9Sstevel@tonic-gate int
tst_getcmd(struct proc * proc,struct user * u)390*7c478bd9Sstevel@tonic-gate tst_getcmd(struct proc *proc, struct user *u)
391*7c478bd9Sstevel@tonic-gate {
392*7c478bd9Sstevel@tonic-gate 	char **arg;
393*7c478bd9Sstevel@tonic-gate 	char **env;
394*7c478bd9Sstevel@tonic-gate 	int i;
395*7c478bd9Sstevel@tonic-gate 	char **p;
396*7c478bd9Sstevel@tonic-gate 	struct pid pidbuf;
397*7c478bd9Sstevel@tonic-gate 
398*7c478bd9Sstevel@tonic-gate 	if (kvm_kread(cookie, (uintptr_t)proc->p_pidp, &pidbuf,
399*7c478bd9Sstevel@tonic-gate 	    sizeof (pidbuf)) != sizeof (pidbuf)) {
400*7c478bd9Sstevel@tonic-gate 		printf("ERROR: couldn't get pid\n");
401*7c478bd9Sstevel@tonic-gate 		return (-1);
402*7c478bd9Sstevel@tonic-gate 	}
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	printf("kvm_getcmd(pid:%d, [u], arg, env)\n", pidbuf.pid_id);
405*7c478bd9Sstevel@tonic-gate 	if ((i = kvm_getcmd(cookie, proc, u, &arg, &env)) != 0) {
406*7c478bd9Sstevel@tonic-gate 		printf("kvm_getcmd returned %d\n", i);
407*7c478bd9Sstevel@tonic-gate 		return (i);
408*7c478bd9Sstevel@tonic-gate 	}
409*7c478bd9Sstevel@tonic-gate 
410*7c478bd9Sstevel@tonic-gate 	printf("Args:  ");
411*7c478bd9Sstevel@tonic-gate 	for (p = arg; *p != NULL; p++)
412*7c478bd9Sstevel@tonic-gate 		safe_printf(*p);
413*7c478bd9Sstevel@tonic-gate 	printf("Env:  ");
414*7c478bd9Sstevel@tonic-gate 	for (p = env; *p != NULL; p++)
415*7c478bd9Sstevel@tonic-gate 		safe_printf(*p);
416*7c478bd9Sstevel@tonic-gate 
417*7c478bd9Sstevel@tonic-gate 	(void) free(arg);
418*7c478bd9Sstevel@tonic-gate 	(void) free(env);
419*7c478bd9Sstevel@tonic-gate 
420*7c478bd9Sstevel@tonic-gate 	return (0);
421*7c478bd9Sstevel@tonic-gate }
422*7c478bd9Sstevel@tonic-gate 
423*7c478bd9Sstevel@tonic-gate void
tst_segkp(void)424*7c478bd9Sstevel@tonic-gate tst_segkp(void)
425*7c478bd9Sstevel@tonic-gate {
426*7c478bd9Sstevel@tonic-gate 	kthread_t t;
427*7c478bd9Sstevel@tonic-gate 	caddr_t tp, alltp;
428*7c478bd9Sstevel@tonic-gate 	uintptr_t stk[16];
429*7c478bd9Sstevel@tonic-gate 	int i;
430*7c478bd9Sstevel@tonic-gate 
431*7c478bd9Sstevel@tonic-gate 	if (kvm_read(cookie, nl[3].n_value, &alltp, sizeof (alltp))
432*7c478bd9Sstevel@tonic-gate 	    != sizeof (alltp)) {
433*7c478bd9Sstevel@tonic-gate 		printf("ERROR: couldn't read allthread, addr 0x%lx\n",
434*7c478bd9Sstevel@tonic-gate 		    nl[3].n_value);
435*7c478bd9Sstevel@tonic-gate 		return;
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 	printf("allthreads 0x%lx\n", nl[3].n_value);
438*7c478bd9Sstevel@tonic-gate 	printf("next offset 0x%lx\n",
439*7c478bd9Sstevel@tonic-gate 	    (uintptr_t)&(t.t_next) - (uintptr_t)&t);
440*7c478bd9Sstevel@tonic-gate 
441*7c478bd9Sstevel@tonic-gate 	for (tp = alltp; tp; tp = (caddr_t)(t.t_next)) {
442*7c478bd9Sstevel@tonic-gate 		if (kvm_read(cookie,
443*7c478bd9Sstevel@tonic-gate 		    (uintptr_t)tp, &t, sizeof (t)) != sizeof (t)) {
444*7c478bd9Sstevel@tonic-gate 			printf("ERROR: couldn't read thread, addr 0x%p\n", tp);
445*7c478bd9Sstevel@tonic-gate 			return;
446*7c478bd9Sstevel@tonic-gate 		}
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 		printf("thread 0x%p\n", tp);
449*7c478bd9Sstevel@tonic-gate 		printf("\tstk 0x%p sp 0x%lx tid %d next 0x%p prev 0x%p\n",
450*7c478bd9Sstevel@tonic-gate 		    tp, t.t_stk, t.t_pcb.val[1], t.t_tid, t.t_next, t.t_prev);
451*7c478bd9Sstevel@tonic-gate 
452*7c478bd9Sstevel@tonic-gate 		if (kvm_read(cookie, t.t_pcb.val[1] + STACK_BIAS, stk,
453*7c478bd9Sstevel@tonic-gate 		    sizeof (stk)) != sizeof (stk)) {
454*7c478bd9Sstevel@tonic-gate 			printf("ERROR: couldn't read stack, taddr 0x%p\n", tp);
455*7c478bd9Sstevel@tonic-gate 			continue;
456*7c478bd9Sstevel@tonic-gate 		}
457*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 16; i++) {
458*7c478bd9Sstevel@tonic-gate 			printf("%-16lx ", stk[i]);
459*7c478bd9Sstevel@tonic-gate 			if (((i + 1) % 4) == 0)
460*7c478bd9Sstevel@tonic-gate 				printf("\n");
461*7c478bd9Sstevel@tonic-gate 		}
462*7c478bd9Sstevel@tonic-gate 
463*7c478bd9Sstevel@tonic-gate 		if ((caddr_t)(t.t_next) == alltp)
464*7c478bd9Sstevel@tonic-gate 			break;
465*7c478bd9Sstevel@tonic-gate 	}
466*7c478bd9Sstevel@tonic-gate }
467