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