summaryrefslogtreecommitdiff
path: root/usr/src/lib/libkvm/common/test.c
blob: 973c647d5b4718283a9b7a8867af5173c4906b09 (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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright (c) 1996-1998 by Sun Microsystems, Inc.
 * All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>

#include "kvm.h"

#include <nlist.h>
#include <sys/thread.h>
#include <sys/fcntl.h>
#include <sys/param.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/elf.h>

#ifdef __sparc
#include <sys/stack.h>		/* for STACK_BIAS */
#else
#define	STACK_BIAS		0
#endif

kvm_t *cookie;

struct proc *tst_getproc(pid_t);
struct proc *tst_nextproc(void);
struct user *tst_getu(struct proc *);
int tst_setproc(void);
int tst_getcmd(struct proc *, struct user *);
void tst_segkp(void);
void tst_nlist(struct nlist nl[]);
void tst_open(char *, char *, char *, int);
void tst_close(void);
ssize_t tst_read(uintptr_t, void *, size_t);
ssize_t tst_write(uintptr_t, void *, size_t);
int tst_getcmd(struct proc *, struct user *);
void tst_segkvp(void);

char *name;
char *core;
char *swap;
int wflag;

struct nlist nl[] = {
	{"free"},
	{"fragtbl"},
	{"freemem"},
	{"allthreads"},
	{"nbuckets"},
	{"cputype"},
	{0}
};

int
main(int argc, char *argv[], char *envp[])
{
	int c, errflg = 0;
	long xx;
	struct nlist *nlp;
	struct proc *proc;
	struct user *u;
	int envc, ccnt;

	for (envc = 0; *envp++ != NULL; envc++)
		continue;
	envp -= 2;
	ccnt = (*envp - *argv) + strlen(*envp) + 1;
	printf("pid %d:: %d args; %d envs; %d chars (%p - %p)\n",
	    getpid(), argc, envc, ccnt,
	    &argv[0], *envp + strlen(*envp));

	while ((c = getopt(argc, argv, "w")) != EOF)
		switch (c) {
		case 'w':
			wflag++;
			break;
		case '?':
			errflg++;
		}
	if (errflg) {
		fprintf(stderr, "usage: %s [-w] [name] [core] [swap]\n",
		    argv[0]);
		return (2);
	}
	if (optind < argc) {
		name = argv[optind++];
		if (*name == '\0')
			name = NULL;
	} else
		name = NULL;
	if (optind < argc) {
		core = argv[optind++];
		if (*core == '\0')
			core = NULL;
	} else
		core = NULL;
	if (optind < argc) {
		swap = argv[optind++];
		if (*swap == '\0')
			swap = NULL;
	} else
		swap = NULL;

	tst_open(name, core, swap, (wflag ? O_RDWR : O_RDONLY));
	if (cookie == NULL)
		return (1);

	tst_nlist(nl);

	for (nlp = nl; nlp[0].n_type != 0; nlp++)
		tst_read(nlp[0].n_value, &xx, sizeof (xx));

	while ((proc = tst_nextproc()) != NULL) {
		struct pid pid;
		if (kvm_read(cookie, (uintptr_t)proc->p_pidp, &pid,
		    sizeof (pid)) != sizeof (pid)) {
			printf("ERROR: couldn't get pid\n");
			break;
		}
		tst_getproc(pid.pid_id);
	}

	tst_setproc();

	while ((proc = tst_nextproc()) != NULL) {
		if ((u = tst_getu(proc)) != NULL)
			(void) tst_getcmd(proc, u);
	}

	tst_segkp();
	tst_close();

	return (0);
}

void
tst_open(char *namelist, char *corefile, char *swapfile, int flag)
{
	printf("kvm_open(%s, %s, %s, %s)\n",
	    (namelist == NULL) ? "LIVE_KERNEL" : namelist,
	    (corefile == NULL) ? "LIVE_KERNEL" : corefile,
	    (swapfile == NULL) ?
		((corefile == NULL) ? "LIVE_KERNEL" : "(none)") : swapfile,
	    (flag == O_RDONLY) ? "O_RDONLY" : ((flag == O_RDWR) ?
	    "O_RDWR" : "???"));

	if ((cookie = kvm_open(namelist, corefile,
	    swapfile, flag, "libkvm test")) == NULL)
		printf("ERROR: kvm_open returned %p\n", cookie);
}

void
tst_close(void)
{
	int i;

	printf("kvm_close()\n");
	if ((i = kvm_close(cookie)) != 0)
		printf("ERROR: kvm_close returned %d\n", i);
}

void
tst_nlist(struct nlist nl[])
{
	int i;
	char *t, *s;

	printf("kvm_nlist([nl])\n");
	if ((i = kvm_nlist(cookie, nl)) != 0)
		printf("ERROR: kvm_nlist returned %d\n", i);
	for (i = 0; nl[i].n_name != 0 && nl[i].n_name[0] != '\0'; i++) {
		/*
		 * Debug:
		 * n_value gets filled in with st_value,
		 * n_type gets filled in w/ELF32_ST_TYPE(sym->st_info)
		 * n_scnum gets filled in w/st_shndx
		 */
		switch (nl[i].n_type) {
		case STT_NOTYPE:
			t = "NOTYPE";
			break;
		case STT_OBJECT:
			t = "OBJECT";
			break;
		case STT_FUNC:
			t = "FUNC";
			break;
		case STT_SECTION:
			t = "SECTION";
			break;
		case STT_FILE:
			t = "FILE";
			break;
		case STT_NUM:
			t = "NUM";
			break;
		default:
			t = "???";
		}

		switch ((unsigned)nl[i].n_scnum) {
			static char strbuf[40];

		case SHN_UNDEF:
			s = "UNDEF";
			break;
		case SHN_LORESERVE:
			s = "LORESERVE";
			break;
		case SHN_ABS:
			s = "ABS";
			break;
		case SHN_COMMON:
			s = "COMMON";
			break;
		case SHN_HIRESERVE:
			s = "HIRESERVE";
			break;
		default:
			(void) sprintf(strbuf, "unknown (%d)", nl[i].n_scnum);
			s = strbuf;
			break;
		}

		printf("%s: %lx (%s, %s)\n",
		    nl[i].n_name, nl[i].n_value, s, t);
	}
}

ssize_t
tst_read(uintptr_t addr, void *buf, size_t nbytes)
{
	ssize_t e;
	int i;
	char *b;

	printf("kvm_read(%lx, [buf], %lu)\n", addr, nbytes);
	if ((e = kvm_read(cookie, addr, buf, nbytes)) != nbytes)
		printf("ERROR: kvm_read returned %ld instead of %lu\n",
		    e, nbytes);
	for (b = buf, i = 0; i < nbytes; b++, i++)
		printf("%lx: %02x (%04o)\n", addr + i,
		    *b & 0xff, *b & 0xff);

	return (e);
}

ssize_t
tst_write(uintptr_t addr, void *buf, size_t nbytes)
{
	ssize_t e;
	ssize_t i;
	void *b;

	printf("kvm_write(%lx, [buf], %lu)\n", addr, nbytes);
	if ((e = kvm_write(cookie, addr, buf, nbytes)) != nbytes)
		printf("ERROR: kvm_write returned %ld instead of %lu\n",
		    e, nbytes);
	if ((b = malloc(nbytes)) == 0)
		printf("ERROR: malloc for readback failed\n");
	else {
		if ((i = kvm_read(cookie, addr, b, nbytes)) != nbytes)
			printf("ERROR: readback returned %ld\n", i);
		else if (memcmp(b, buf, nbytes))
			printf("ERROR: write check failed!\n");
		(void) free(b);
	}
	return (e);
}

struct proc *
tst_getproc(pid_t pid)
{
	struct proc *proc;
	struct pid pidbuf;

	printf("kvm_getproc(%d)\n", pid);
	if ((proc = kvm_getproc(cookie, pid)) == NULL) {
		printf("ERROR: kvm_getproc returned NULL\n");
		return (proc);
	}

	if (kvm_read(cookie, (uintptr_t)proc->p_pidp, &pidbuf,
	    sizeof (pidbuf)) != sizeof (pidbuf)) {
		printf("ERROR: couldn't get pid\n");
		return (proc);
	}

	printf("p_pid: %d\n", pidbuf.pid_id);
	return (proc);
}

struct proc *
tst_nextproc(void)
{
	struct proc *proc;
	struct pid pidbuf;

	printf("kvm_nextproc()\n");
	if ((proc = kvm_nextproc(cookie)) == NULL) {
		printf("kvm_nextproc returned NULL\n");
		return (proc);
	}

	/*
	 * p_pid is now a macro which turns into a ptr dereference;
	 * must do a kvm_read to get contents.
	 */
	if (kvm_read(cookie, (u_long)proc->p_pidp, (char *)&pidbuf,
	    sizeof (struct pid)) != sizeof (struct pid)) {
		printf("ERROR: couldn't get pid\n");
	}
	printf("p_pid: %d\n", pidbuf.pid_id);

	return (proc);
}

int
tst_setproc(void)
{
	int i;

	printf("kvm_setproc()\n");
	if ((i = kvm_setproc(cookie)) != 0)
		printf("ERROR: kvm_setproc returned %d\n", i);
	return (i);
}

struct user *
tst_getu(struct proc *proc)
{
	register int e;
	struct proc tp;
	struct user *u;
	struct pid pidbuf;

	if (kvm_read(cookie, (uintptr_t)proc->p_pidp, &pidbuf,
	    sizeof (pidbuf)) != sizeof (pidbuf))
		printf("ERROR: couldn't get pid\n");

	printf("kvm_getu(pid:%d)\n", pidbuf.pid_id);
	if ((u = kvm_getu(cookie, proc)) == NULL)
		printf("ERROR: kvm_getu returned NULL\n");
	return (u);
}

static void
safe_printf(const char *s)
{
	char buf[BUFSIZ], *p;

	(void) strncpy(buf, s, BUFSIZ - 1);
	buf[BUFSIZ - 1] = '\0';

	for (p = buf; *p != '\0'; p++) {
		if (!isprint(*p))
			*p = ' ';
	}

	(void) printf("\"%s\"\n", buf);
}

int
tst_getcmd(struct proc *proc, struct user *u)
{
	char **arg;
	char **env;
	int i;
	char **p;
	struct pid pidbuf;

	if (kvm_kread(cookie, (uintptr_t)proc->p_pidp, &pidbuf,
	    sizeof (pidbuf)) != sizeof (pidbuf)) {
		printf("ERROR: couldn't get pid\n");
		return (-1);
	}

	printf("kvm_getcmd(pid:%d, [u], arg, env)\n", pidbuf.pid_id);
	if ((i = kvm_getcmd(cookie, proc, u, &arg, &env)) != 0) {
		printf("kvm_getcmd returned %d\n", i);
		return (i);
	}

	printf("Args:  ");
	for (p = arg; *p != NULL; p++)
		safe_printf(*p);
	printf("Env:  ");
	for (p = env; *p != NULL; p++)
		safe_printf(*p);

	(void) free(arg);
	(void) free(env);

	return (0);
}

void
tst_segkp(void)
{
	kthread_t t;
	caddr_t tp, alltp;
	uintptr_t stk[16];
	int i;

	if (kvm_read(cookie, nl[3].n_value, &alltp, sizeof (alltp))
	    != sizeof (alltp)) {
		printf("ERROR: couldn't read allthread, addr 0x%lx\n",
		    nl[3].n_value);
		return;
	}
	printf("allthreads 0x%lx\n", nl[3].n_value);
	printf("next offset 0x%lx\n",
	    (uintptr_t)&(t.t_next) - (uintptr_t)&t);

	for (tp = alltp; tp; tp = (caddr_t)(t.t_next)) {
		if (kvm_read(cookie,
		    (uintptr_t)tp, &t, sizeof (t)) != sizeof (t)) {
			printf("ERROR: couldn't read thread, addr 0x%p\n", tp);
			return;
		}

		printf("thread 0x%p\n", tp);
		printf("\tstk 0x%p sp 0x%lx tid %d next 0x%p prev 0x%p\n",
		    tp, t.t_stk, t.t_pcb.val[1], t.t_tid, t.t_next, t.t_prev);

		if (kvm_read(cookie, t.t_pcb.val[1] + STACK_BIAS, stk,
		    sizeof (stk)) != sizeof (stk)) {
			printf("ERROR: couldn't read stack, taddr 0x%p\n", tp);
			continue;
		}
		for (i = 0; i < 16; i++) {
			printf("%-16lx ", stk[i]);
			if (((i + 1) % 4) == 0)
				printf("\n");
		}

		if ((caddr_t)(t.t_next) == alltp)
			break;
	}
}