summaryrefslogtreecommitdiff
path: root/minix/usr.bin/trace/service/mib.c
blob: 9734a0e034006083d4a5426211d8a87747924163 (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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090

#include "inc.h"

#include <sys/time.h>
#include <sys/sysctl.h>
#include <sys/sched.h>
#include <sys/resource.h>

struct sysctl_tab {
	int id;
	size_t size;
	const struct sysctl_tab *tab;
	int (*proc)(struct trace_proc *, const char *, int, const void *,
	    vir_bytes, size_t);
};
#define NODE(i,t) { .id = i, .size = __arraycount(t), .tab = t }
#define PROC(i,s,p) { .id = i, .size = s, .proc = p }

/*
 * Print CTL_KERN KERN_CLOCKRATE.
 */
static int
put_kern_clockrate(struct trace_proc * proc, const char * name,
	int type __unused, const void * ptr, vir_bytes addr __unused,
	size_t size __unused)
{
	const struct clockinfo *ci;

	ci = (const struct clockinfo *)ptr;

	put_value(proc, "hz", "%d", ci->hz);
	put_value(proc, "tick", "%d", ci->tick);
	if (verbose > 0) {
		put_value(proc, "tickadj", "%d", ci->tickadj);
		put_value(proc, "stathz", "%d", ci->stathz);
		put_value(proc, "profhz", "%d", ci->profhz);
		return TRUE;
	} else
		return FALSE;
}

/*
 * Print CTL_KERN KERN_PROC2.
 */
static int
put_kern_proc2(struct trace_proc * proc, const char * name, int type,
	const void * ptr, vir_bytes addr, size_t size)
{
	const int *mib;
	const char *text;
	unsigned int i;

	if (type == ST_NAME) {
		mib = (const int *)ptr;

		for (i = 0; i < size; i++) {
			text = NULL;

			if (i == 0) {
				switch (mib[i]) {
				case KERN_PROC_ALL: text = "<all>"; break;
				case KERN_PROC_PID: text = "<pid>"; break;
				case KERN_PROC_PGRP: text = "<pgrp>"; break;
				case KERN_PROC_SESSION:
					text = "<session>"; break;
				case KERN_PROC_TTY: text = "<tty>"; break;
				case KERN_PROC_UID: text = "<uid>"; break;
				case KERN_PROC_RUID: text = "<ruid>"; break;
				case KERN_PROC_GID: text = "<gid>"; break;
				case KERN_PROC_RGID: text = "<rgid>"; break;
				}
			} else if (i == 1 && mib[0] == KERN_PROC_TTY) {
				switch ((dev_t)mib[i]) {
				case KERN_PROC_TTY_NODEV:
					text = "<nodev>"; break;
				case KERN_PROC_TTY_REVOKE:
					text = "<revoke>"; break;
				}
			}

			if (!valuesonly && text != NULL)
				put_field(proc, NULL, text);
			else
				put_value(proc, NULL, "%d", mib[i]);
		}

		/*
		 * Save the requested structure length, so that we can later
		 * determine how many elements were returned (see below).
		 */
		proc->sctl_arg = (size == 4) ? mib[2] : 0;

		return 0;
	}

	if (proc->sctl_arg > 0) {
		/* TODO: optionally dump struct kinfo_drivers array */
		put_open(proc, name, 0, "[", ", ");
		if (size > 0)
			put_tail(proc, size / proc->sctl_arg, 0);
		put_close(proc, "]");
	} else
		put_ptr(proc, name, addr);

	return TRUE;
}

/*
 * Print CTL_KERN KERN_PROC_ARGS.
 */
static int
put_kern_proc_args(struct trace_proc * proc, const char * name, int type,
	const void * ptr, vir_bytes addr, size_t size)
{
	const int *mib;
	const char *text;
	unsigned int i;
	int v;

	if (type == ST_NAME) {
		mib = (const int *)ptr;

		for (i = 0; i < size; i++) {
			text = NULL;

			if (i == 1) {
				switch (mib[i]) {
				case KERN_PROC_ARGV: text = "<argv>"; break;
				case KERN_PROC_ENV: text = "<env>"; break;
				case KERN_PROC_NARGV: text = "<nargv>"; break;
				case KERN_PROC_NENV: text = "<nenv>"; break;
				}
			}

			if (!valuesonly && text != NULL)
				put_field(proc, NULL, text);
			else
				put_value(proc, NULL, "%d", mib[i]);
		}

		/* Save the subrequest, so that we can later print data. */
		proc->sctl_arg = (size == 2) ? mib[1] : -999;

		return 0;
	}

	if ((proc->sctl_arg == KERN_PROC_NARGV ||
	    proc->sctl_arg == KERN_PROC_NENV) && size == sizeof(v) &&
	    mem_get_data(proc->pid, addr, &v, sizeof(v)) >= 0) {
		put_open(proc, name, PF_NONAME, "{", ", ");

		put_value(proc, NULL, "%d", v);

		put_close(proc, "}");
	} else
		put_ptr(proc, name, addr);

	return TRUE;
}

/*
 * Print CTL_KERN KERN_CP_TIME.
 */
static int
put_kern_cp_time(struct trace_proc * proc, const char * name __unused,
	int type, const void * ptr, vir_bytes addr __unused, size_t size)
{
	const uint64_t *p;
	unsigned int i;
	const int *mib;

	if (type == ST_NAME) {
		mib = (const int *)ptr;
		for (i = 0; i < size; i++)
			put_value(proc, NULL, "%d", mib[i]);

		return 0;
	}

	p = (const uint64_t *)ptr;

	/* TODO: support for multi-CPU results */
	for (i = 0; i < CPUSTATES; i++)
		put_value(proc, NULL, "%"PRIu64, p[i]);

	return TRUE;
}

/*
 * Print CTL_KERN KERN_CONSDEV.
 */
static int
put_kern_consdev(struct trace_proc * proc, const char * name,
	int type __unused, const void * ptr, vir_bytes addr __unused,
	size_t size __unused)
{

	put_dev(proc, NULL, *(const dev_t *)ptr);

	return TRUE;
}

/*
 * Print CTL_KERN KERN_DRIVERS.
 */
static int
put_kern_drivers(struct trace_proc * proc, const char * name,
	int type __unused, const void * ptr __unused, vir_bytes addr __unused,
	size_t size)
{

	/* TODO: optionally dump struct kinfo_drivers array */
	put_open(proc, name, 0, "[", ", ");
	if (size > 0)
		put_tail(proc, size / sizeof(struct kinfo_drivers), 0);
	put_close(proc, "]");

	return TRUE;
}

/*
 * Print CTL_KERN KERN_BOOTTIME.
 */
static int
put_kern_boottime(struct trace_proc * proc, const char * name,
	int type __unused, const void * ptr __unused, vir_bytes addr,
	size_t size)
{

	if (size == sizeof(struct timeval))
		put_struct_timeval(proc, name, 0, addr);
	else
		put_ptr(proc, name, addr);

	return TRUE;
}

/*
 * Print CTL_KERN KERN_SYSVIPC KERN_SYSVIPC_INFO.
 */
static int
put_kern_sysvipc_info(struct trace_proc * proc, const char * name,
	int type, const void * ptr, vir_bytes addr, size_t size)
{
	const int *mib;
	const char *text;
	unsigned int i;

	/*
	 * TODO: print the obtained structure(s).  For now we are just
	 * concerned with the name components.
	 */
	if (type != ST_NAME) {
		put_ptr(proc, name, addr);

		return TRUE;
	}

	mib = (const int *)ptr;

	for (i = 0; i < size; i++) {
		text = NULL;

		if (i == 0) {
			switch (mib[i]) {
			case KERN_SYSVIPC_SEM_INFO: text = "<sem>"; break;
			case KERN_SYSVIPC_SHM_INFO: text = "<shm>"; break;
			case KERN_SYSVIPC_MSG_INFO: text = "<msg>"; break;
			}
		}

		if (!valuesonly && text != NULL)
			put_field(proc, NULL, text);
		else
			put_value(proc, NULL, "%d", mib[i]);
	}

	return 0;
}

/* The CTL_KERN KERN_SYSVIPC table. */
static const struct sysctl_tab kern_sysvipc_tab[] = {
	PROC(KERN_SYSVIPC_INFO, 0, put_kern_sysvipc_info),
};

/* The CTL_KERN table. */
static const struct sysctl_tab kern_tab[] = {
	PROC(KERN_CLOCKRATE, sizeof(struct clockinfo), put_kern_clockrate),
	PROC(KERN_PROC2, 0, put_kern_proc2),
	PROC(KERN_PROC_ARGS, 0, put_kern_proc_args),
	PROC(KERN_CP_TIME, sizeof(uint64_t) * CPUSTATES, put_kern_cp_time),
	PROC(KERN_CONSDEV, sizeof(dev_t), put_kern_consdev),
	PROC(KERN_DRIVERS, 0, put_kern_drivers),
	NODE(KERN_SYSVIPC, kern_sysvipc_tab),
	PROC(KERN_BOOTTIME, 0, put_kern_boottime),
};

/*
 * Print CTL_VM VM_LOADAVG.
 */
static int
put_vm_loadavg(struct trace_proc * proc, const char * name __unused,
	int type __unused, const void * ptr, vir_bytes addr __unused,
	size_t size __unused)
{
	const struct loadavg *loadavg;
	unsigned int i;

	loadavg = (const struct loadavg *)ptr;

	put_open(proc, "ldavg", 0, "{", ", ");

	for (i = 0; i < __arraycount(loadavg->ldavg); i++)
		put_value(proc, NULL, "%"PRIu32, loadavg->ldavg[i]);

	put_close(proc, "}");

	if (verbose > 0) {
		put_value(proc, "fscale", "%ld", loadavg->fscale);

		return TRUE;
	} else
		return FALSE;
}

/* The CTL_VM table. */
static const struct sysctl_tab vm_tab[] = {
	PROC(VM_LOADAVG, sizeof(struct loadavg), put_vm_loadavg),
};

/*
 * Print CTL_NET PF_ROUTE 0.
 */
static int
put_net_route_rtable(struct trace_proc * proc, const char * name,
	int type, const void * ptr, vir_bytes addr, size_t size)
{
	const int *mib;
	const char *text;
	unsigned int i;

	/*
	 * TODO: print the obtained structure(s).  For now we are just
	 * concerned with the name components.
	 */
	if (type != ST_NAME) {
		put_ptr(proc, name, addr);

		return TRUE;
	}

	mib = (const int *)ptr;

	for (i = 0; i < size; i++) {
		text = NULL;

		switch (i) {
		case 0:
			switch (mib[i]) {
			case AF_UNSPEC: text = "<all>"; break;
			case AF_LINK: text = "<link>"; break;
			case AF_INET: text = "<inet>"; break;
			case AF_INET6: text = "<inet6>"; break;
			/* TODO: add more address families here */
			}
			break;
		case 1:
			switch (mib[i]) {
			case NET_RT_DUMP: text = "<dump>"; break;
			case NET_RT_FLAGS: text = "<flags>"; break;
			case NET_RT_IFLIST: text = "<iflist>"; break;
			}
			break;
		case 2:
			if (mib[1] == NET_RT_IFLIST && mib[i] == 0)
				text = "<all>";
		}

		if (!valuesonly && text != NULL)
			put_field(proc, NULL, text);
		else
			put_value(proc, NULL, "%d", mib[i]);
	}

	return 0;
}

/* The CTL_NET PF_ROUTE table. */
static const struct sysctl_tab net_route_tab[] = {
	PROC(0, 0, put_net_route_rtable),
};

/* The CTL_NET table. */
static const struct sysctl_tab net_tab[] = {
	NODE(PF_ROUTE, net_route_tab),
};

/* The top-level table, which is indexed by identifier. */
static const struct sysctl_tab root_tab[] = {
	[CTL_KERN]	= NODE(0, kern_tab),
	[CTL_VM]	= NODE(0, vm_tab),
	[CTL_NET]	= NODE(0, net_tab),
};

/*
 * This buffer should be large enough to avoid having to perform dynamic
 * allocation in all but highly exceptional cases.  The CTL_KERN subtree is
 * currently the largest, so we base the buffer size on its length.
 * TODO: merge this buffer with ioctlbuf.
 */
static char sysctlbuf[sizeof(struct sysctlnode) * KERN_MAXID];

static const struct flags sysctl_flags[] = {
	FLAG_MASK(SYSCTL_VERS_MASK, SYSCTL_VERS_0),
	FLAG_MASK(SYSCTL_VERS_MASK, SYSCTL_VERSION),
#define SYSCTL_VER_ENTRIES 2 /* the first N entries are for SYSCTL_VERS_MASK */
	FLAG(CTLFLAG_UNSIGNED),
	FLAG(CTLFLAG_OWNDESC),
	FLAG(CTLFLAG_MMAP),
	FLAG(CTLFLAG_ALIAS),
	FLAG(CTLFLAG_ANYNUMBER),
	FLAG(CTLFLAG_ROOT),
	FLAG(CTLFLAG_HEX),
	FLAG(CTLFLAG_IMMEDIATE),
	FLAG(CTLFLAG_OWNDATA),
	FLAG(CTLFLAG_HIDDEN),
	FLAG(CTLFLAG_PERMANENT),
	FLAG(CTLFLAG_PRIVATE),
	FLAG(CTLFLAG_ANYWRITE),
	FLAG_MASK(CTLFLAG_READWRITE, CTLFLAG_READONLY),
	FLAG_MASK(CTLFLAG_READWRITE, CTLFLAG_READWRITE),
	FLAG_MASK(SYSCTL_TYPEMASK, CTLTYPE_NODE),
	FLAG_MASK(SYSCTL_TYPEMASK, CTLTYPE_INT),
	FLAG_MASK(SYSCTL_TYPEMASK, CTLTYPE_STRING),
	FLAG_MASK(SYSCTL_TYPEMASK, CTLTYPE_QUAD),
	FLAG_MASK(SYSCTL_TYPEMASK, CTLTYPE_STRUCT),
	FLAG_MASK(SYSCTL_TYPEMASK, CTLTYPE_BOOL),
};

/*
 * Print the immediate value of a sysctl node.
 */
static void
put_sysctl_imm(struct trace_proc * proc, struct sysctlnode * scn, int use_name)
{
	const char *name;

	name = NULL;

	switch (SYSCTL_TYPE(scn->sysctl_flags)) {
	case CTLTYPE_INT:
		if (use_name)
			name = "sysctl_idata";
		if (scn->sysctl_flags & CTLFLAG_HEX)
			put_value(proc, name, "0x%x", scn->sysctl_idata);
		else if (scn->sysctl_flags & CTLFLAG_UNSIGNED)
			put_value(proc, name, "%u", scn->sysctl_idata);
		else
			put_value(proc, name, "%d", scn->sysctl_idata);
		break;
	case CTLTYPE_BOOL:
		if (use_name)
			name = "sysctl_bdata";
		put_field(proc, name, (scn->sysctl_bdata) ? "true" : "false");
		break;
	case CTLTYPE_QUAD:
		if (use_name)
			name = "sysctl_qdata";
		if (scn->sysctl_flags & CTLFLAG_HEX)
			put_value(proc, name, "0x%"PRIx64, scn->sysctl_qdata);
		else
			put_value(proc, name, "%"PRIu64, scn->sysctl_qdata);
		break;
	}
}

/*
 * Printer for CTL_QUERY data.
 */
static int
put_sysctl_query(struct trace_proc * proc, const char * name, int type,
	const void * data __unused, vir_bytes addr, size_t size)
{
	struct sysctlnode scn;

	if (type == ST_NEWP) {
		if (!put_open_struct(proc, name, 0, addr, &scn, sizeof(scn)))
			return TRUE;

		/* Print just the protocol version, that's all there is. */
		if (verbose > 1)
			put_flags(proc, "sysctl_flags", sysctl_flags,
			    SYSCTL_VER_ENTRIES, "0x%x", scn.sysctl_flags);

		put_close_struct(proc, FALSE /*all*/);
	} else {
		/* TODO: optionally dump struct sysctlnode array */
		put_open(proc, name, 0, "[", ", ");
		if (size > 0)
			put_tail(proc, size / sizeof(scn), 0);
		put_close(proc, "]");
	}

	return TRUE;
}

/*
 * Printer for CTL_CREATE data.
 */
static int
put_sysctl_create(struct trace_proc * proc, const char * name, int type,
	const void * data __unused, vir_bytes addr, size_t size)
{
	struct sysctlnode scn;

	if (!put_open_struct(proc, name, 0, addr, &scn, sizeof(scn)))
		return TRUE;

	if (type == ST_NEWP)
		put_flags(proc, "sysctl_flags", sysctl_flags,
		    COUNT(sysctl_flags), "0x%x", scn.sysctl_flags);

	if (scn.sysctl_num == CTL_CREATE && type == ST_NEWP && !valuesonly)
		put_field(proc, "sysctl_num", "CTL_CREATE");
	else
		put_value(proc, "sysctl_num", "%d", scn.sysctl_num);

	if (type == ST_NEWP) {
		put_buf(proc, "sysctl_name", PF_LOCADDR | PF_STRING,
		    (vir_bytes)scn.sysctl_name, sizeof(scn.sysctl_name));
	}
	if (scn.sysctl_ver != 0 && verbose > 0)
		put_value(proc, "sysctl_ver", "%u", scn.sysctl_ver);

	if (type == ST_NEWP) {
		if (scn.sysctl_flags & CTLFLAG_IMMEDIATE)
			put_sysctl_imm(proc, &scn, TRUE /*use_name*/);

		switch (SYSCTL_TYPE(scn.sysctl_flags)) {
		case CTLTYPE_NODE:
			break;
		case CTLTYPE_STRING:
			if (scn.sysctl_data != NULL)
				put_buf(proc, "sysctl_data", PF_STRING,
				    (vir_bytes)scn.sysctl_data,
				    (scn.sysctl_size > 0) ? scn.sysctl_size :
				    SSIZE_MAX /* hopefully it stops early */);
			if (scn.sysctl_data != NULL || verbose == 0)
				break;
			/* FALLTHROUGH */
		default:
			if (!(scn.sysctl_flags & CTLFLAG_IMMEDIATE) &&
			    verbose > 0)
				put_ptr(proc, "sysctl_data",
				    (vir_bytes)scn.sysctl_data);
			break;
		}

		if (SYSCTL_TYPE(scn.sysctl_flags) == CTLTYPE_STRUCT ||
		    verbose > 0)
			put_value(proc, "sysctl_size", "%zu", scn.sysctl_size);
	}

	put_close_struct(proc, FALSE /*all*/);

	return TRUE;
}

/*
 * Printer for CTL_DESTROY data.
 */
static int
put_sysctl_destroy(struct trace_proc * proc, const char * name, int type,
	const void * data __unused, vir_bytes addr, size_t size)
{
	struct sysctlnode scn;

	if (!put_open_struct(proc, name, 0, addr, &scn, sizeof(scn)))
		return TRUE;

	if (type == ST_NEWP) {
		put_value(proc, "sysctl_num", "%d", scn.sysctl_num);
		if (scn.sysctl_name[0] != '\0')
			put_buf(proc, "sysctl_name", PF_LOCADDR | PF_STRING,
			    (vir_bytes)scn.sysctl_name,
			    sizeof(scn.sysctl_name));
		if (scn.sysctl_ver != 0 && verbose > 0)
			put_value(proc, "sysctl_ver", "%u", scn.sysctl_ver);
	}

	put_close_struct(proc, FALSE /*all*/);

	return TRUE;
}

/*
 * Printer for CTL_CREATE data.
 */
static int
put_sysctl_describe(struct trace_proc * proc, const char * name, int type,
	const void * data __unused, vir_bytes addr, size_t size)
{
	struct sysctlnode scn;

	if (type == ST_NEWP) {
		if (!put_open_struct(proc, name, 0, addr, &scn, sizeof(scn)))
			return TRUE;

		/* Print just the protocol version, that's all there is. */
		if (verbose > 1)
			put_flags(proc, "sysctl_flags", sysctl_flags,
			    SYSCTL_VER_ENTRIES, "0x%x", scn.sysctl_flags);

		put_value(proc, "sysctl_num", "%d", scn.sysctl_num);

		if (scn.sysctl_desc != NULL)
			put_buf(proc, "sysctl_desc", PF_STRING,
			    (vir_bytes)scn.sysctl_desc, 1024 /*no constant!*/);
		else if (verbose > 0)
			put_ptr(proc, "sysctl_desc",
			    (vir_bytes)scn.sysctl_desc);

		put_close_struct(proc, FALSE /*all*/);
	} else {
		/* TODO: optionally dump struct sysctldesc array */
		put_field(proc, name, (size == 0) ? "[]" : "[..]");
	}

	return TRUE;
}

/*
 * Printer for generic data, using the node flags stored in proc->sysctl_flags.
 */
static int
put_sysctl_generic(struct trace_proc * proc, const char * name, int type,
	const void * data __unused, vir_bytes addr, size_t size)
{
	struct sysctlnode scn;
	void *ptr;
	size_t len;

	switch (SYSCTL_TYPE(proc->sctl_flags)) {
	case CTLTYPE_STRING:
		put_buf(proc, name, PF_STRING, addr, size);
		return TRUE;
	case CTLTYPE_INT:
		ptr = &scn.sysctl_idata;
		len = sizeof(scn.sysctl_idata);
		break;
	case CTLTYPE_BOOL:
		ptr = &scn.sysctl_bdata;
		len = sizeof(scn.sysctl_bdata);
		break;
	case CTLTYPE_QUAD:
		ptr = &scn.sysctl_qdata;
		len = sizeof(scn.sysctl_qdata);
		break;
	case CTLTYPE_STRUCT:
	default:
		ptr = NULL;
		len = 0;
		break;
	}

	if (ptr == NULL || len != size ||
	    mem_get_data(proc->pid, addr, ptr, len) < 0) {
		put_ptr(proc, name, addr);
		return TRUE;
	}

	put_open(proc, name, PF_NONAME, "{", ", ");

	scn.sysctl_flags = proc->sctl_flags;

	put_sysctl_imm(proc, &scn, FALSE);

	put_close(proc, "}");

	return TRUE;
}

/*
 * Obtain information about a particular node 'id' in the node directory
 * identified by the MIB path 'name' (length 'namelen').  Return TRUE if the
 * node was found, in which case it is copied into 'scnp'.  Return FALSE if the
 * node was not found or another error occurred.
 */
static int
get_sysctl_node(const int * name, unsigned int namelen, int id,
	struct sysctlnode * scnp)
{
	struct sysctlnode *scn, *escn, *fscn;
	char *buf;
	size_t len, elen;
	int r, mib[CTL_MAXNAME];

	assert(namelen < CTL_MAXNAME);
	assert(id >= 0);

	/* Query the parent, first using our static buffer for the results. */
	memcpy(mib, name, sizeof(mib[0]) * namelen);
	mib[namelen] = CTL_QUERY;
	len = sizeof(sysctlbuf);
	r = sysctl(mib, namelen + 1, sysctlbuf, &len, NULL, 0);
	if (r == -1 && (errno != ENOMEM || len == 0))
		return FALSE;

	/* Even with partial results, check if we already found the node. */
	elen = MIN(len, sizeof(sysctlbuf));
	scn = (struct sysctlnode *)sysctlbuf;
	escn = (struct sysctlnode *)&sysctlbuf[elen];
	fscn = NULL; /* pointer to the node once found, NULL until then */
	for (; scn < escn && fscn == NULL; scn++)
		if (scn->sysctl_num == id)
			fscn = scn;

	/* If our buffer was too small, use a temporary buffer. */
	if (fscn == NULL && r == -1) {
		if ((buf = malloc(len)) == NULL)
			return FALSE;
		if (sysctl(mib, namelen, buf, &len, NULL, 0) == 0) {
			scn = (struct sysctlnode *)sysctlbuf;
			escn = (struct sysctlnode *)&sysctlbuf[len];
			for (; scn < escn && fscn != NULL; scn++)
				if (scn->sysctl_num == id)
					fscn = scn;
		}
		free(buf);
	}

	if (fscn != NULL) {
		memcpy(scnp, fscn, sizeof(*scnp));
		return TRUE;
	} else
		return FALSE;
}

/*
 * Print the name string of one level of a sysctl(2) name, while also gathering
 * information about the target node.  Return 1 if name interpretation should
 * continue as before, meaning this function will also be called for the next
 * name component (if any).  Return 0 if the rest of the name should be printed
 * as numbers, without interpretation.  Return -1 if printing the name is now
 * complete.
 */
static int
put_sysctl_namestr(struct trace_proc * proc, const int * name,
	unsigned int namelen, unsigned int n, int all,
	const struct sysctl_tab ** sctp)
{
	const struct sysctl_tab *sct;
	struct sysctlnode scn;
	const char *namestr;
	int i, r, id, is_last;

	assert(n < namelen);

	id = name[n];
	is_last = (n == namelen - 1 && all);
	namestr = NULL;

	/* Negative identifiers are meta-identifiers. */
	if (id < 0) {
		switch (id) {
		case CTL_EOL:		namestr = "<eol>";		break;
		case CTL_QUERY:		namestr = "<query>";		break;
		case CTL_CREATE:	namestr = "<create>";		break;
		case CTL_CREATESYM:	namestr = "<createsym>";	break;
		case CTL_DESTROY:	namestr = "<destroy>";		break;
		case CTL_MMAP:		namestr = "<mmap>";		break;
		case CTL_DESCRIBE:	namestr = "<describe>";		break;
		}

		/* For some of them, we can print their parameters. */
		if (is_last) {
			switch (id) {
			case CTL_QUERY:
				proc->sctl_proc = put_sysctl_query;
				break;
			case CTL_CREATE:
				proc->sctl_proc = put_sysctl_create;
				break;
			case CTL_DESTROY:
				proc->sctl_proc = put_sysctl_destroy;
				break;
			case CTL_DESCRIBE:
				proc->sctl_proc = put_sysctl_describe;
				break;
			}
		}

		/*
		 * Meta-identifiers are allowed only at the very end of a name,
		 * so if anything follows a meta-identifier, there is no good
		 * way to interpret it.  We just print numbers.
		 */
		r = 0;
	} else if (get_sysctl_node(name, n, id, &scn)) {
		/*
		 * For regular identifiers, first see if we have a callback
		 * function that does the interpretation.  The use of the
		 * callback function depends on whether the current node is of
		 * type CTLTYPE_NODE: if it is, the callback function is
		 * responsible for printing the rest of the name (and we return
		 * -1 here after we are done, #1); if it isn't, then we just
		 * use the callback function to interpret the node value (#2).
		 * If we do not have a callback function, but the current node
		 * is of type CTLTYPE_NODE *and* has a non-NULL callback
		 * function registered in the MIB service, the remote callback
		 * function would interpret the rest of the name, so we simply
		 * print the rest of the name as numbers (returning 0 once we
		 * are done, #3).  Without a MIB-service callback function,
		 * such nodes are just taken as path components and thus we
		 * return 1 to continue resolution (#4).  Finally, if we do not
		 * have a callback function, and the current node is a data
		 * node (i.e., *not* of type CTLTYPE_NODE), we try to interpret
		 * it generically if it is the last component (#5), or we give
		 * up and just print numbers otherwise (#6).
		 */

		/* Okay, so start by looking up the node in our own tables. */
		sct = NULL;
		if (n == 0) {
			/* The top level is ID-indexed for performance. */
			if ((unsigned int)id < __arraycount(root_tab))
				*sctp = &root_tab[id];
			else
				*sctp = NULL;
		} else if (*sctp != NULL) {
			/* Other levels are searched, because of sparseness. */
			sct = (*sctp)->tab; /* NULL if missing or leaf */
			for (i = (int)(*sctp)->size; sct != NULL && i > 0;
			    i--, sct++)
				if (sct->id == id)
					break;
			if (i == 0)
				sct = NULL;
			*sctp = sct;
		}

		/* Now determine what to do. */
		if (SYSCTL_TYPE(scn.sysctl_flags) == CTLTYPE_NODE) {
			if (sct != NULL && sct->proc != NULL) {
				proc->sctl_size = sct->size;
				proc->sctl_proc = sct->proc;
				r = -1; /* #1 */
			} else if (scn.sysctl_func != NULL)
				r = 0; /* #3 */
			else
				r = 1; /* #4 */
		} else {
			if (!is_last)
				r = 0; /* #6 */
			else if (sct != NULL && sct->proc != NULL) {
				/* A nonzero size must match the node size. */
				if (sct->size == 0 ||
				    sct->size == scn.sysctl_size) {
					proc->sctl_size = sct->size;
					proc->sctl_proc = sct->proc;
				}
				r = 0; /* #2 */
			} else {
				proc->sctl_flags = scn.sysctl_flags;
				proc->sctl_proc = put_sysctl_generic;
				r = 0; /* #5 */
			}
		}

		namestr = scn.sysctl_name;
	} else {
		/*
		 * The node was not found.  This basically means that we will
		 * not be able to get any information about deeper nodes
		 * either.  We do not even try: just print numbers.
		 */
		r = 0;
	}

	if (!valuesonly && namestr != NULL)
		put_field(proc, NULL, namestr);
	else
		put_value(proc, NULL, "%d", id);

	/*
	 * Did we determine that the rest of the name should be printed by the
	 * callback function?  Then we might as well make that happen.  The
	 * abuse of the parameter types is not great, oh well.
	 */
	if (r == -1)
		(void)proc->sctl_proc(proc, NULL, ST_NAME, &name[n + 1], 0,
		    namelen - n - 1);

	return r;
}

/*
 * Print the sysctl(2) name parameter, and gather information needed to print
 * the oldp and newp parameters later.
 */
static void
put_sysctl_name(struct trace_proc * proc, const char * name, int flags,
	vir_bytes addr, unsigned int namelen)
{
	const struct sysctl_tab *sct = NULL;
	int r, all, namebuf[CTL_MAXNAME];
	unsigned int n;

	if (namelen > CTL_MAXNAME) {
		namelen = CTL_MAXNAME;
		all = 0;
	} else
		all = 1;

	if ((flags & PF_FAILED) || valuesonly > 1 || namelen > CTL_MAXNAME ||
	    (namelen > 0 && !(flags & PF_LOCADDR) &&
	    mem_get_data(proc->pid, addr, namebuf,
	    namelen * sizeof(namebuf[0])) < 0)) {
		if (flags & PF_LOCADDR)
			put_field(proc, name, "&..");
		else
			put_ptr(proc, name, addr);
		return;
	} else if (namelen > 0 && (flags & PF_LOCADDR))
		memcpy(namebuf, (void *)addr, sizeof(namebuf[0]) * namelen);

	/*
	 * Print the path name of the node as possible, and find information
	 * about the target node as we go along.  See put_sysctl_namestr() for
	 * the meaning of 'r'.
	 */
	put_open(proc, name, PF_NONAME, "[", ".");
	for (n = 0, r = 1; n < namelen; n++) {
		if (r == 1) {
			if ((r = put_sysctl_namestr(proc, namebuf, namelen, n,
			    all, &sct)) < 0)
				break;
		} else
			put_value(proc, NULL, "%d", namebuf[n]);
	}
	if (!all)
		put_field(proc, NULL, "..");
	put_close(proc, "]");
}

/*
 * Print the sysctl(2) oldp or newp parameter.  PF_ALT means that the given
 * parameter is newp rather than oldp, in which case PF_FAILED will not be set.
 */
static void
put_sysctl_data(struct trace_proc * proc, const char * name, int flags,
	vir_bytes addr, size_t len)
{
	char *ptr;
	int type, all;

	if ((flags & PF_FAILED) || addr == 0 || valuesonly > 1 ||
	    proc->sctl_proc == NULL || proc->sctl_size > sizeof(sysctlbuf) ||
	    (proc->sctl_size > 0 && (proc->sctl_size != len ||
	    mem_get_data(proc->pid, addr, sysctlbuf, proc->sctl_size) < 0))) {
		put_ptr(proc, name, addr);
		return;
	}

	type = (flags & PF_ALT) ? ST_NEWP : ST_OLDP;
	ptr = (proc->sctl_size > 0) ? sysctlbuf : NULL;

	/*
	 * The rough idea here: we have a "simple" mode and a "flexible" mode,
	 * depending on whether a size was specified in our table.  For the
	 * simple mode, we only call the callback function when we have been
	 * able to copy in the data.  A surrounding {} block will be printed
	 * automatically, the callback function only has to print the data
	 * fields.  The simple mode is basically for structures.  In contrast,
	 * the flexible mode leaves both the copying and the printing entirely
	 * to the callback function, which thus may print the pointer on copy
	 * failure (in which case the surrounding {}s would get in the way).
	 */
	if (ptr != NULL)
		put_open(proc, name, 0, "{", ", ");

	all = proc->sctl_proc(proc, name, type, ptr, addr, len);

	if (ptr != NULL) {
		if (all == FALSE)
			put_field(proc, NULL, "..");
		put_close(proc, "}");
	}
}

static int
mib_sysctl_out(struct trace_proc * proc, const message * m_out)
{
	unsigned int namelen;

	/* Reset the sysctl-related state. */
	proc->sctl_flags = 0;
	proc->sctl_size = 0;
	proc->sctl_proc = NULL;
	proc->sctl_arg = 0;

	namelen = m_out->m_lc_mib_sysctl.namelen;

	/* As part of processing the name, we initialize the state. */
	if (namelen <= CTL_SHORTNAME)
		put_sysctl_name(proc, "name", PF_LOCADDR,
		    (vir_bytes)&m_out->m_lc_mib_sysctl.name, namelen);
	else
		put_sysctl_name(proc, "name", 0, m_out->m_lc_mib_sysctl.namep,
		    namelen);

	put_value(proc, "namelen", "%u", namelen);

	if (m_out->m_lc_mib_sysctl.oldp == 0 || valuesonly > 1) {
		put_sysctl_data(proc, "oldp", 0,
		    m_out->m_lc_mib_sysctl.oldp,
		    m_out->m_lc_mib_sysctl.oldlen);
		/* If oldp is NULL, oldlen may contain garbage; don't print. */
		if (m_out->m_lc_mib_sysctl.oldp != 0)
			put_value(proc, "oldlen", "%zu",    /* {%zu} is more */
			    m_out->m_lc_mib_sysctl.oldlen); /* correct..     */
		else
			put_value(proc, "oldlen", "%d", 0);
		put_sysctl_data(proc, "newp", PF_ALT,
		    m_out->m_lc_mib_sysctl.newp,
		    m_out->m_lc_mib_sysctl.newlen);
		put_value(proc, "newlen", "%zu",
		    m_out->m_lc_mib_sysctl.newlen);
		return CT_DONE;
	} else
		return CT_NOTDONE;
}

static void
mib_sysctl_in(struct trace_proc * proc, const message * m_out,
	const message * m_in, int failed)
{
	int err;

	if (m_out->m_lc_mib_sysctl.oldp != 0 && valuesonly <= 1) {
		put_sysctl_data(proc, "oldp", failed,
		    m_out->m_lc_mib_sysctl.oldp,
		    m_in->m_mib_lc_sysctl.oldlen /* the returned length */);
		put_value(proc, "oldlen", "%zu", /* {%zu} is more correct.. */
		    m_out->m_lc_mib_sysctl.oldlen);
		put_sysctl_data(proc, "newp", PF_ALT,
		    m_out->m_lc_mib_sysctl.newp,
		    m_out->m_lc_mib_sysctl.newlen);
		put_value(proc, "newlen", "%zu",
		    m_out->m_lc_mib_sysctl.newlen);
		put_equals(proc);
	}

	put_result(proc);

	/*
	 * We want to print the returned old length in the following cases:
	 * 1. the call succeeded, the old pointer was NULL, and no new data was
	 *    supplied;
	 * 2. the call succeeded, the old pointer was not NULL, and the
	 *    returned old length is different from the supplied old length.
	 * 3. the call failed with ENOMEM or EEXIST, and the old pointer was
	 *    not NULL (an undocumented NetBSD feature, used by sysctl(8)).
	 */
	if (/*#1*/ (!failed && m_out->m_lc_mib_sysctl.oldp == 0 &&
	    (m_out->m_lc_mib_sysctl.newp == 0 ||
	    m_out->m_lc_mib_sysctl.newlen == 0)) ||
	    /*#2*/ (!failed && m_out->m_lc_mib_sysctl.oldp != 0 &&
	    m_out->m_lc_mib_sysctl.oldlen != m_in->m_mib_lc_sysctl.oldlen) ||
	    /*#3*/ (failed && call_errno(proc, &err) &&
	    (err == ENOMEM || err == EEXIST) &&
	    m_out->m_lc_mib_sysctl.oldp != 0)) {
		put_open(proc, NULL, 0, "(", ", ");
		put_value(proc, "oldlen", "%zu", m_in->m_mib_lc_sysctl.oldlen);
		put_close(proc, ")");
	}
}

#define MIB_CALL(c) [((MIB_ ## c) - MIB_BASE)]

static const struct call_handler mib_map[] = {
	MIB_CALL(SYSCTL) = HANDLER("sysctl", mib_sysctl_out, mib_sysctl_in),
};

const struct calls mib_calls = {
	.endpt = MIB_PROC_NR,
	.base = MIB_BASE,
	.map = mib_map,
	.count = COUNT(mib_map)
};