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