File: dt_printf.c

package info (click to toggle)
dtrace 2.0.5-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 24,408 kB
  • sloc: ansic: 61,247; sh: 17,997; asm: 1,717; lex: 947; awk: 754; yacc: 695; perl: 37; sed: 17; makefile: 15
file content (2524 lines) | stat: -rw-r--r-- 67,851 bytes parent folder | download
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
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
/*
 * Oracle Linux DTrace.
 * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */

#include <string.h>
#include <stdlib.h>
#include <alloca.h>
#include <assert.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <port.h>

#include <dt_impl.h>
#include <dt_aggregate.h>
#include <dt_module.h>
#include <dt_printf.h>
#include <dt_string.h>

/*ARGSUSED*/
static int
pfcheck_addr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return (dt_node_is_pointer(dnp) && !dt_node_is_stack(dnp)) ||
	       dt_node_is_integer(dnp);
}

/*ARGSUSED*/
static int
pfcheck_kaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return dt_node_is_pointer(dnp) || dt_node_is_integer(dnp) ||
	    dt_node_is_symaddr(dnp);
}

/*ARGSUSED*/
static int
pfcheck_uaddr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	dtrace_hdl_t *dtp = pfv->pfv_dtp;
	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");

	if (dt_node_is_usymaddr(dnp))
		return 1;

	if (idp == NULL || idp->di_id == 0)
		return 0;

	return dt_node_is_pointer(dnp) || dt_node_is_integer(dnp);
}

/*ARGSUSED*/
static int
pfcheck_stack(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return dt_node_is_stack(dnp);
}

/*ARGSUSED*/
static int
pfcheck_time(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return dt_node_is_integer(dnp) &&
	    dt_node_type_size(dnp) == sizeof(uint64_t);
}

/*ARGSUSED*/
static int
pfcheck_str(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	ctf_file_t *ctfp;
	ctf_encoding_t e;
	ctf_arinfo_t r;
	ctf_id_t base;
	uint_t kind;

	if (dt_node_is_string(dnp))
		return 1;

	ctfp = dnp->dn_ctfp;
	base = ctf_type_resolve(ctfp, dnp->dn_type);
	kind = ctf_type_kind(ctfp, base);

	return kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
	    ctf_type_encoding(ctfp, base, &e) == 0 && IS_CHAR(e);
}

/*ARGSUSED*/
static int
pfcheck_wstr(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	ctf_file_t *ctfp = dnp->dn_ctfp;
	ctf_id_t base = ctf_type_resolve(ctfp, dnp->dn_type);
	uint_t kind = ctf_type_kind(ctfp, base);

	ctf_encoding_t e;
	ctf_arinfo_t r;

	return kind == CTF_K_ARRAY && ctf_array_info(ctfp, base, &r) == 0 &&
	    (base = ctf_type_resolve(ctfp, r.ctr_contents)) != CTF_ERR &&
	    ctf_type_kind(ctfp, base) == CTF_K_INTEGER &&
	    ctf_type_encoding(ctfp, base, &e) == 0 && e.cte_bits == 32;
}

/*ARGSUSED*/
static int
pfcheck_csi(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return dt_node_is_integer(dnp) &&
	    dt_node_type_size(dnp) <= sizeof(int);
}

/*ARGSUSED*/
static int
pfcheck_fp(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return dt_node_is_float(dnp);
}

/*ARGSUSED*/
static int
pfcheck_xint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return dt_node_is_integer(dnp);
}

/*ARGSUSED*/
static int
pfcheck_dint(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	if (dnp->dn_flags & DT_NF_SIGNED)
		pfd->pfd_flags |= DT_PFCONV_SIGNED;
	else
		pfd->pfd_fmt[strlen(pfd->pfd_fmt) - 1] = 'u';

	return dt_node_is_integer(dnp);
}

/*ARGSUSED*/
static int
pfcheck_xshort(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	ctf_file_t *ctfp = dnp->dn_ctfp;
	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
	char n[DT_TYPE_NAMELEN];

	return ctf_type_name(ctfp, type, n, sizeof(n)) != NULL && (
	    strcmp(n, "short") == 0 || strcmp(n, "signed short") == 0 ||
	    strcmp(n, "unsigned short") == 0);
}

/*ARGSUSED*/
static int
pfcheck_xlong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	ctf_file_t *ctfp = dnp->dn_ctfp;
	ctf_id_t type = ctf_type_resolve(ctfp, dnp->dn_type);
	char n[DT_TYPE_NAMELEN];

	return ctf_type_name(ctfp, type, n, sizeof(n)) != NULL && (
	    strcmp(n, "long") == 0 || strcmp(n, "signed long") == 0 ||
	    strcmp(n, "unsigned long") == 0);
}

/*ARGSUSED*/
static int
pfcheck_xlonglong(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	ctf_file_t *ctfp = dnp->dn_ctfp;
	ctf_id_t type = dnp->dn_type;
	char n[DT_TYPE_NAMELEN];

	if (ctf_type_name(ctfp, ctf_type_resolve(ctfp, type), n,
	    sizeof(n)) != NULL && (strcmp(n, "long long") == 0 ||
	    strcmp(n, "signed long long") == 0 ||
	    strcmp(n, "unsigned long long") == 0))
		return 1;

	/*
	 * If the type used for %llx or %llX is not an [unsigned] long long, we
	 * also permit it to be a [u]int64_t or any typedef thereof.  We know
	 * that these typedefs are guaranteed to work with %ll[xX] in either
	 * compilation environment even though they alias to "long" in LP64.
	 */
	while (ctf_type_kind(ctfp, type) == CTF_K_TYPEDEF) {
		if (ctf_type_name(ctfp, type, n, sizeof(n)) != NULL &&
		    (strcmp(n, "int64_t") == 0 || strcmp(n, "uint64_t") == 0))
			return 1;

		type = ctf_type_reference(ctfp, type);
	}

	return 0;
}

/*ARGSUSED*/
static int
pfcheck_type(dt_pfargv_t *pfv, dt_pfargd_t *pfd, dt_node_t *dnp)
{
	return ctf_type_compat(dnp->dn_ctfp, ctf_type_resolve(dnp->dn_ctfp,
	    dnp->dn_type), pfd->pfd_conv->pfc_dctfp, pfd->pfd_conv->pfc_dtype);
}

/*ARGSUSED*/
static int
pfprint_sint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t unormal, uint64_t sig)
{
	int64_t normal = (int64_t)unormal;
	int32_t n = (int32_t)normal;

	switch (size) {
	case sizeof(int8_t):
		return dt_printf(dtp, fp, format,
		    (int32_t)*((int8_t *)addr) / n);
	case sizeof(int16_t):
		return dt_printf(dtp, fp, format,
		    (int32_t)*((int16_t *)addr) / n);
	case sizeof(int32_t):
		return dt_printf(dtp, fp, format,
		    *((int32_t *)addr) / n);
	case sizeof(int64_t):
		return dt_printf(dtp, fp, format,
		    *((int64_t *)addr) / normal);
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}
}

/*ARGSUSED*/
static int
pfprint_uint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	uint32_t n = (uint32_t)normal;

	switch (size) {
	case sizeof(uint8_t):
		return dt_printf(dtp, fp, format,
		    (uint32_t)*((uint8_t *)addr) / n);
	case sizeof(uint16_t):
		return dt_printf(dtp, fp, format,
		    (uint32_t)*((uint16_t *)addr) / n);
	case sizeof(uint32_t):
		return dt_printf(dtp, fp, format, *((uint32_t *)addr) / n);
	case sizeof(uint64_t):
		return dt_printf(dtp, fp, format, *((uint64_t *)addr) / normal);
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}
}

static int
pfprint_dint(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	if (pfd->pfd_flags & DT_PFCONV_SIGNED)
		return pfprint_sint(dtp, fp, format, pfd, addr, size, normal,
				    sig);
	else
		return pfprint_uint(dtp, fp, format, pfd, addr, size, normal,
				    sig);
}

/*ARGSUSED*/
static int
pfprint_fp(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	   const dt_pfargd_t *pfd, const void *addr, size_t size,
	   uint64_t normal, uint64_t sig)
{
	double n = (double)normal;
	long double ldn = (long double)normal;

	switch (size) {
	case sizeof(float):
		return dt_printf(dtp, fp, format, (double)*((float *)addr) / n);
	case sizeof(double):
		return dt_printf(dtp, fp, format, *((double *)addr) / n);
	case sizeof(long double):
		return dt_printf(dtp, fp, format, *((long double *)addr) / ldn);
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}
}

/*ARGSUSED*/
static int
pfprint_addr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	char *s;
	int n, len = 256;
	uint64_t val;

	switch (size) {
	case sizeof(uint32_t):
		val = *((uint32_t *)addr);
		break;
	case sizeof(uint64_t):
		val = *((uint64_t *)addr);
		break;
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}

	do {
		n = len;
		s = alloca(n);
	} while ((len = dtrace_addr2str(dtp, val, s, n)) > n);

	return dt_printf(dtp, fp, format, s);
}

/*ARGSUSED*/
static int
pfprint_mod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	    const dt_pfargd_t *pfd, const void *addr, size_t size,
	    uint64_t normal, uint64_t sig)
{
	return dt_print_mod(dtp, fp, format, (caddr_t)addr);
}

/*ARGSUSED*/
static int
pfprint_umod(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	return dt_print_umod(dtp, fp, format, (caddr_t)addr);
}

/*ARGSUSED*/
static int
pfprint_uaddr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	      const dt_pfargd_t *pfd, const void *addr, size_t size,
	      uint64_t normal, uint64_t sig)
{
	char *s;
	int n, len = 256;
	uint64_t val, tgid = 0;

	dt_ident_t *idp = dt_idhash_lookup(dtp->dt_macros, "target");

	switch (size) {
	case sizeof(uint32_t):
		val = (unsigned long long)*((uint32_t *)addr);
		break;
	case sizeof(uint64_t):
		val = (unsigned long long)*((uint64_t *)addr);
		break;
	case sizeof(uint64_t) * 3:
		tgid = ((uint64_t *)(uintptr_t)addr)[1];
		val = ((uint64_t *)(uintptr_t)addr)[2];
		break;
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}

	if (tgid == 0 && dtp->dt_vector == NULL && idp != NULL)
		tgid = idp->di_id; /* pretend it is a pid */

	do {
		n = len;
		s = alloca(n);
	} while ((len = dtrace_uaddr2str(dtp, tgid, val, s, n)) > n);

	return dt_printf(dtp, fp, format, s);
}

static int
dt_print_stack_kernel(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		      caddr_t addr, int indent, uint32_t depth)
{
	dtrace_syminfo_t dts;
	GElf_Sym sym;
	int i;
	char c[PATH_MAX * 2];
	uint64_t pc;

	if (format == NULL)
		format = "%s";

	for (i = 0; i < depth; i++) {
		pc = *((uint64_t *)addr);
		if (pc == 0)
			break;

		addr += sizeof(pc);

		if (dt_printf(dtp, fp, "%*s", indent, "") < 0)
			return -1;

		if (dtrace_lookup_by_addr(dtp, pc, &sym, &dts) == 0) {
			if (pc > sym.st_value)
				snprintf(c, sizeof(c), "%s`%s+0x%llx",
					 dts.object, dts.name,
					 (long long unsigned)pc - sym.st_value);
			else
				snprintf(c, sizeof(c), "%s`%s",
					 dts.object, dts.name);
		} else {
			/*
			 * We'll repeat the lookup, but this time we'll specify
			 * a NULL GElf_Sym -- indicating that we're only
			 * interested in the containing module.
			 */
			if (dtrace_lookup_by_addr(dtp, pc, NULL, &dts) == 0)
				snprintf(c, sizeof(c), "%s`0x%llx",
					 dts.object, (long long unsigned)pc);
			else
				snprintf(c, sizeof(c), "0x%llx",
				    (long long unsigned)pc);
		}

		if (dt_printf(dtp, fp, format, c) < 0)
			return -1;

		if (dt_printf(dtp, fp, "\n") < 0)
			return -1;
	}

	return 0;
}

static int
dt_print_stack_user(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		    caddr_t addr, int indent, uint32_t depth, uint32_t strsize)
{
	/* LINTED - alignment */
	uint64_t *pc = ((uint64_t *)addr);
	const char *strbase = addr + (depth + 1) * sizeof(uint64_t);
	const char *str = strsize ? strbase : NULL;
	int err = 0;

	const char *name;
	char objname[PATH_MAX], c[PATH_MAX * 2];
	GElf_Sym sym;
	int i;
	pid_t pid = -1, tgid;

	if (depth == 0)
		return 0;

	tgid = ((uint32_t *)pc)[1];
	pc++;

	if (format == NULL)
		format = "%s";

	/*
	 * Ultimately, we need to add an entry point in the library vector for
	 * determining <symbol, offset> from <tgid, address>.  For now, if
	 * this is a vector open, we just print the raw address or string.
	 */
	if (dtp->dt_vector == NULL)
		pid = dt_proc_grab_lock(dtp, tgid, DTRACE_PROC_WAITING |
		    DTRACE_PROC_SHORTLIVED);

	for (i = 0; i < depth && pc[i] != 0; i++) {
		const prmap_t *map;

		if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
			break;
		if (dtp->dt_options[DTRACEOPT_NORESOLVE] != DTRACEOPT_UNSET
		    && pid >= 0) {
			if (dt_Pobjname(dtp, pid, pc[i], objname,
			    sizeof(objname)) != NULL) {
				const prmap_t *pmap = NULL;
				uint64_t offset = pc[i];

				pmap = dt_Paddr_to_map(dtp, pid, pc[i]);

				if (pmap)
					offset = pc[i] - pmap->pr_vaddr;

				snprintf(c, sizeof(c), "%s:0x%llx",
				    dt_basename(objname), (unsigned long long)offset);

			} else
				snprintf(c, sizeof(c), "0x%llx",
				    (unsigned long long)pc[i]);

		} else if (pid >= 0 && dt_Plookup_by_addr(dtp, pid, pc[i],
							  &name, &sym) == 0) {
			if (dt_Pobjname(dtp, pid, pc[i], objname,
					sizeof(objname)) != NULL) {
				if (pc[i] > sym.st_value)
					snprintf(c, sizeof(c), "%s`%s+0x%llx",
						 dt_basename(objname), name,
						 (unsigned long long)(pc[i] - sym.st_value));
				else
					snprintf(c, sizeof(c), "%s`%s",
						 dt_basename(objname), name);
			} else
				snprintf(c, sizeof(c), "0x%llx",
				    (unsigned long long)pc[i]);
			/* Allocated by Plookup_by_addr. */
			free((char *)name);
		} else if (str != NULL && str[0] != '\0' && str[0] != '@' &&
		    (pid >= 0 &&
			((map = dt_Paddr_to_map(dtp, pid, pc[i])) == NULL ||
			    (map->pr_mflags & MA_WRITE)))) {
			/*
			 * If the current string pointer in the string table
			 * does not point to an empty string _and_ the program
			 * counter falls in a writable region, we'll use the
			 * string from the string table instead of the raw
			 * address.  This last condition is necessary because
			 * some (broken) ustack helpers will return a string
			 * even for a program counter that they can't
			 * identify.  If we have a string for a program
			 * counter that falls in a segment that isn't
			 * writable, we assume that we have fallen into this
			 * case and we refuse to use the string.
			 */
			snprintf(c, sizeof(c), "%s", str);
		} else {
			if (pid >= 0 && dt_Pobjname(dtp, pid, pc[i], objname,
			    sizeof(objname)) != NULL)
				snprintf(c, sizeof(c), "%s`0x%llx",
				    dt_basename(objname), (unsigned long long)pc[i]);
			else
				snprintf(c, sizeof(c), "0x%llx",
				    (unsigned long long)pc[i]);
		}

		if ((err = dt_printf(dtp, fp, format, c)) < 0)
			break;

		if ((err = dt_printf(dtp, fp, "\n")) < 0)
			break;

		if (str != NULL && str[0] == '@') {
			/*
			 * If the first character of the string is an "at" sign,
			 * then the string is inferred to be an annotation --
			 * and it is printed out beneath the frame and offset
			 * with brackets.
			 */
			if ((err = dt_printf(dtp, fp, "%*s", indent, "")) < 0)
				break;

			snprintf(c, sizeof(c), "  [ %s ]", &str[1]);

			if ((err = dt_printf(dtp, fp, format, c)) < 0)
				break;

			if ((err = dt_printf(dtp, fp, "\n")) < 0)
				break;
		}

		if (str != NULL) {
			str += strlen(str) + 1;
			if (str - strbase >= strsize)
				str = NULL;
		}
	}

	if (pid >= 0)
		dt_proc_release_unlock(dtp, pid);

	return err;
}

/*
 * The data at vaddr is structured as follows:
 *	uint32_t	depth
 *	uint32_t	strsz
 *	uint32_t	is_user
 *	uint32_t	pid
 *	uint64_t	addrs[depth]
 *
 * The 'depth' member provides the maximum number of addresses in the stack
 * trace.
 * The 'strsz' member provides the size of the optional string blob that is
 * appended to the stack trace data.
 * The 'is_user' member identifies the stack trace as a userspace stack trace
 * if its value is > 0.
 * The 'pid' member provides the userspace process id for userspace stack
 * traces and otherwise will be 0.
 * The 'addrs' array provides the stack traces addresses.  If the stack trace
 * is shorter than 'depth', remaining addresses will be 0 and can be ignored.
 */
static int
pfprint_stack(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	      const dt_pfargd_t *pfd, const void *vaddr, size_t size,
	      uint64_t normal, uint64_t sig)
{
	int width;
	uint32_t *vals = (uint32_t *)vaddr;
	uint32_t depth = vals[0];
	uint32_t strsz = vals[1];
	uint32_t is_user = vals[2];
	caddr_t addr = (caddr_t)(vals + (is_user ? 2 : 4));
	int err = 0;

	if (depth > dtp->dt_options[DTRACEOPT_MAXFRAMES])
		return dt_set_errno(dtp, EDT_DSIZE);

	if (depth == 0)
		return 0;

	/*
	 * If the field has been specified as left-aligned (i.e. (%-#), we use
	 * the field width as indentation.  This is a slightly odd semantic
	 * but it's useful functionality -- and it's slightly odd to begin with
	 * to be using a single format specifier to be formatting multiple
	 * lines of text...
	 */
	if (pfd->pfd_dynwidth < 0) {
		assert(pfd->pfd_flags & DT_PFCONV_DYNWIDTH);
		width = -pfd->pfd_dynwidth;
	} else if (pfd->pfd_flags & DT_PFCONV_LEFT) {
		width = pfd->pfd_dynwidth ? pfd->pfd_dynwidth : pfd->pfd_width;
	} else {
		width = 0;
	}

	if (dt_printf(dtp, fp, "\n") < 0)
		return -1;

	if (is_user)
		err = dt_print_stack_user(dtp, fp, format, addr, width, depth,
					  strsz);
	else
		err = dt_print_stack_kernel(dtp, fp, format, addr, width, depth);

	return err;
}

/*ARGSUSED*/
static int
pfprint_time(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	char src[32], buf[32], *dst = buf;
	hrtime_t time = *((uint64_t *)addr);
	time_t sec = (time_t)(time / NANOSEC);
	int i;

	/*
	 * ctime(3C) returns a string of the form "Dec  3 17:20:00 1973\n\0".
	 * Below, we turn this into the canonical adb/mdb /[yY] format,
	 * "1973 Dec  3 17:20:00".
	 */
	ctime_r(&sec, src);

	/*
	 * Place the 4-digit year at the head of the string...
	 */
	for (i = 20; i < 24; i++)
		*dst++ = src[i];

	/*
	 * ...and follow it with the remainder (month, day, hh:mm:ss).
	 */
	for (i = 3; i < 19; i++)
		*dst++ = src[i];

	*dst = '\0';
	return dt_printf(dtp, fp, format, buf);
}

/*
 * This prints the time in RFC 822 standard form.  This is useful for emitting
 * notions of time that are consumed by standard tools (e.g., as part of an
 * RSS feed).
 */
/*ARGSUSED*/
static int
pfprint_time822(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		const dt_pfargd_t *pfd, const void *addr, size_t size,
		uint64_t normal, uint64_t sig)
{
	hrtime_t time = *((uint64_t *)addr);
	time_t sec = (time_t)(time / NANOSEC);
	struct tm tm;
	char buf[64];

	localtime_r(&sec, &tm);
	strftime(buf, sizeof(buf), "%a, %d %b %G %T %Z", &tm);
	return dt_printf(dtp, fp, format, buf);
}

/*ARGSUSED*/
static int
pfprint_cstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	char *s = alloca(size + 1);

	memcpy(s, addr, size);
	s[size] = '\0';
	return dt_printf(dtp, fp, format, s);
}

/*ARGSUSED*/
static int
pfprint_wstr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	wchar_t *ws = alloca(size + sizeof(wchar_t));

	memcpy(ws, addr, size);
	ws[size / sizeof(wchar_t)] = L'\0';
	return dt_printf(dtp, fp, format, ws);
}

/*ARGSUSED*/
static int
pfprint_estr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	char *s;
	int n;

	if ((s = strchr2esc(addr, size)) == NULL)
		return dt_set_errno(dtp, EDT_NOMEM);

	n = dt_printf(dtp, fp, format, s);
	free(s);
	return n;
}

static int
pfprint_echr(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	     const dt_pfargd_t *pfd, const void *addr, size_t size,
	     uint64_t normal, uint64_t sig)
{
	char c;

	switch (size) {
	case sizeof(int8_t):
		c = *(int8_t *)addr;
		break;
	case sizeof(int16_t):
		c = *(int16_t *)addr;
		break;
	case sizeof(int32_t):
		c = *(int32_t *)addr;
		break;
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}

	return pfprint_estr(dtp, fp, format, pfd, &c, 1, normal, sig);
}

/*ARGSUSED*/
static int
pfprint_pct(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	    const dt_pfargd_t *pfd, const void *addr, size_t size,
	    uint64_t normal, uint64_t sig)
{
	return dt_printf(dtp, fp, "%%");
}

static const char pfproto_xint[] = "char, short, int, long, or long long";
static const char pfproto_csi[] = "char, short, or int";
static const char pfproto_fp[] = "float, double, or long double";
static const char pfproto_addr[] = "pointer or integer";
static const char pfproto_uaddr[] =
	"pointer or integer (with -p/-c) or _usymaddr (without -p/-c)";
static const char pfproto_cstr[] = "char [] or string (or use stringof)";
static const char pfproto_wstr[] = "wchar_t []";

/*
 * Printf format conversion dictionary.  This table should match the set of
 * conversions offered by printf(3C), as well as some additional extensions.
 * The second parameter is an ASCII string which is either an actual type
 * name we should look up (if pfcheck_type is specified), or just a descriptive
 * string of the types expected for use in error messages.
 */
static const dt_pfconv_t _dtrace_conversions[] = {
{ "a", "s", pfproto_addr, pfcheck_kaddr, pfprint_addr },
{ "A", "s", pfproto_uaddr, pfcheck_uaddr, pfprint_uaddr },
{ "c", "c", pfproto_csi, pfcheck_csi, pfprint_sint },
{ "C", "s", pfproto_csi, pfcheck_csi, pfprint_echr },
{ "d", "d", pfproto_xint, pfcheck_dint, pfprint_dint },
{ "e", "e", pfproto_fp, pfcheck_fp, pfprint_fp },
{ "E", "E", pfproto_fp, pfcheck_fp, pfprint_fp },
{ "f", "f", pfproto_fp, pfcheck_fp, pfprint_fp },
{ "g", "g", pfproto_fp, pfcheck_fp, pfprint_fp },
{ "G", "G", pfproto_fp, pfcheck_fp, pfprint_fp },
{ "hd", "d", "short", pfcheck_type, pfprint_sint },
{ "hi", "i", "short", pfcheck_type, pfprint_sint },
{ "ho", "o", "unsigned short", pfcheck_type, pfprint_uint },
{ "hu", "u", "unsigned short", pfcheck_type, pfprint_uint },
{ "hx", "x", "short", pfcheck_xshort, pfprint_uint },
{ "hX", "X", "short", pfcheck_xshort, pfprint_uint },
{ "i", "i", pfproto_xint, pfcheck_dint, pfprint_dint },
{ "k", "s", "dt_stack_t", pfcheck_stack, pfprint_stack },
{ "lc", "lc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wint_t */
{ "ld",	"d", "long", pfcheck_type, pfprint_sint },
{ "li",	"i", "long", pfcheck_type, pfprint_sint },
{ "lo",	"o", "unsigned long", pfcheck_type, pfprint_uint },
{ "lu", "u", "unsigned long", pfcheck_type, pfprint_uint },
{ "ls",	"ls", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
{ "lx",	"x", "long", pfcheck_xlong, pfprint_uint },
{ "lX",	"X", "long", pfcheck_xlong, pfprint_uint },
{ "lld", "d", "long long", pfcheck_type, pfprint_sint },
{ "lli", "i", "long long", pfcheck_type, pfprint_sint },
{ "llo", "o", "unsigned long long", pfcheck_type, pfprint_uint },
{ "llu", "u", "unsigned long long", pfcheck_type, pfprint_uint },
{ "llx", "x", "long long", pfcheck_xlonglong, pfprint_uint },
{ "llX", "X", "long long", pfcheck_xlonglong, pfprint_uint },
{ "Le",	"e", "long double", pfcheck_type, pfprint_fp },
{ "LE",	"E", "long double", pfcheck_type, pfprint_fp },
{ "Lf",	"f", "long double", pfcheck_type, pfprint_fp },
{ "Lg",	"g", "long double", pfcheck_type, pfprint_fp },
{ "LG",	"G", "long double", pfcheck_type, pfprint_fp },
{ "o", "o", pfproto_xint, pfcheck_xint, pfprint_uint },
{ "p", "x", pfproto_addr, pfcheck_addr, pfprint_uint },
{ "s", "s", "char [] or string (or use stringof)", pfcheck_str, pfprint_cstr },
{ "S", "s", pfproto_cstr, pfcheck_str, pfprint_estr },
{ "T", "s", "int64_t", pfcheck_time, pfprint_time822 },
{ "u", "u", pfproto_xint, pfcheck_xint, pfprint_uint },
{ "wc",	"wc", "int", pfcheck_type, pfprint_sint }, /* a.k.a. wchar_t */
{ "ws", "ws", pfproto_wstr, pfcheck_wstr, pfprint_wstr },
{ "x", "x", pfproto_xint, pfcheck_xint, pfprint_uint },
{ "X", "X", pfproto_xint, pfcheck_xint, pfprint_uint },
{ "Y", "s", "int64_t", pfcheck_time, pfprint_time },
{ "%", "%", "void", pfcheck_type, pfprint_pct },
{ NULL, NULL, NULL, NULL, NULL }
};

int
dt_pfdict_create(dtrace_hdl_t *dtp)
{
	uint_t n = _dtrace_strbuckets;
	const dt_pfconv_t *pfd;
	dt_pfdict_t *pdi;

	if ((pdi = malloc(sizeof(dt_pfdict_t))) == NULL ||
	    (pdi->pdi_buckets = malloc(sizeof(dt_pfconv_t *) * n)) == NULL) {
		free(pdi);
		return dt_set_errno(dtp, EDT_NOMEM);
	}

	dtp->dt_pfdict = pdi;
	memset(pdi->pdi_buckets, 0, sizeof(dt_pfconv_t *) * n);
	pdi->pdi_nbuckets = n;

	for (pfd = _dtrace_conversions; pfd->pfc_name != NULL; pfd++) {
		dtrace_typeinfo_t dtt;
		dt_pfconv_t *pfc;
		uint_t h;

		if ((pfc = malloc(sizeof(dt_pfconv_t))) == NULL) {
			dt_pfdict_destroy(dtp);
			return dt_set_errno(dtp, EDT_NOMEM);
		}

		memcpy(pfc, pfd, sizeof(dt_pfconv_t));
		h = str2hval(pfc->pfc_name, 0) % n;
		pfc->pfc_next = pdi->pdi_buckets[h];
		pdi->pdi_buckets[h] = pfc;

		dtt.dtt_ctfp = NULL;
		dtt.dtt_type = CTF_ERR;

		/*
		 * The "D" container or its parent must contain a definition of
		 * any type referenced by a printf conversion.  If none can be
		 * found, we fail to initialize the printf dictionary.
		 */
		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
		    dtp, DTRACE_OBJ_DDEFS, pfc->pfc_tstr, &dtt) != 0) {
			dt_pfdict_destroy(dtp);
			return dt_set_errno(dtp, EDT_NOCONV);
		}

		pfc->pfc_dctfp = dtt.dtt_ctfp;
		pfc->pfc_dtype = dtt.dtt_type;

		/*
		 * The "C" container may contain an alternate definition of an
		 * explicit conversion type.  If it does, use it; otherwise
		 * just set pfc_ctype to pfc_dtype so it is always valid.
		 */
		if (pfc->pfc_check == &pfcheck_type && dtrace_lookup_by_type(
		    dtp, DTRACE_OBJ_CDEFS, pfc->pfc_tstr, &dtt) == 0) {
			pfc->pfc_cctfp = dtt.dtt_ctfp;
			pfc->pfc_ctype = dtt.dtt_type;
		} else {
			pfc->pfc_cctfp = pfc->pfc_dctfp;
			pfc->pfc_ctype = pfc->pfc_dtype;
		}

		if (pfc->pfc_check == NULL || pfc->pfc_print == NULL ||
		    pfc->pfc_ofmt == NULL || pfc->pfc_tstr == NULL) {
			dt_pfdict_destroy(dtp);
			return dt_set_errno(dtp, EDT_BADCONV);
		}

		dt_dprintf("loaded printf conversion %%%s\n", pfc->pfc_name);
	}

	return 0;
}

void
dt_pfdict_destroy(dtrace_hdl_t *dtp)
{
	dt_pfdict_t *pdi = dtp->dt_pfdict;
	dt_pfconv_t *pfc, *nfc;
	uint_t i;

	if (pdi == NULL)
		return;

	for (i = 0; i < pdi->pdi_nbuckets; i++) {
		for (pfc = pdi->pdi_buckets[i]; pfc != NULL; pfc = nfc) {
			nfc = pfc->pfc_next;
			free(pfc);
		}
	}

	free(pdi->pdi_buckets);
	free(pdi);
	dtp->dt_pfdict = NULL;
}

static const dt_pfconv_t *
dt_pfdict_lookup(dtrace_hdl_t *dtp, const char *name)
{
	dt_pfdict_t *pdi = dtp->dt_pfdict;
	uint_t h = str2hval(name, 0) % pdi->pdi_nbuckets;
	const dt_pfconv_t *pfc;

	for (pfc = pdi->pdi_buckets[h]; pfc != NULL; pfc = pfc->pfc_next) {
		if (strcmp(pfc->pfc_name, name) == 0)
			break;
	}

	return pfc;
}

static dt_pfargv_t *
dt_printf_error(dtrace_hdl_t *dtp, int err)
{
	if (yypcb != NULL)
		longjmp(yypcb->pcb_jmpbuf, err);

	dt_set_errno(dtp, err);
	return NULL;
}

dt_pfargv_t *
dt_printf_create(dtrace_hdl_t *dtp, const char *s)
{
	dt_pfargd_t *pfd, *nfd = NULL;
	dt_pfargv_t *pfv;
	const char *p, *q;
	char *format;

	if ((pfv = malloc(sizeof(dt_pfargv_t))) == NULL ||
	    (format = strdup(s)) == NULL) {
		free(pfv);
		return dt_printf_error(dtp, EDT_NOMEM);
	}

	pfv->pfv_format = format;
	pfv->pfv_argv = NULL;
	pfv->pfv_argc = 0;
	pfv->pfv_flags = 0;
	pfv->pfv_dtp = dtp;

	for (q = format; (p = strchr(q, '%')) != NULL; q = *p ? p + 1 : p) {
		uint_t namelen = 0;
		int digits = 0;
		int dot = 0;

		char name[8];
		char c;
		int n;

		if ((pfd = malloc(sizeof(dt_pfargd_t))) == NULL) {
			dt_printf_destroy(pfv);
			return dt_printf_error(dtp, EDT_NOMEM);
		}

		if (pfv->pfv_argv != NULL)
			nfd->pfd_next = pfd;
		else
			pfv->pfv_argv = pfd;

		memset(pfd, 0, sizeof(dt_pfargd_t));
		pfv->pfv_argc++;
		nfd = pfd;

		if (p > q) {
			pfd->pfd_preflen = (size_t)(p - q);
			pfd->pfd_prefix = q;
		}

		fmt_switch:
		switch (c = *++p) {
		case '0': case '1': case '2': case '3': case '4':
		case '5': case '6': case '7': case '8': case '9':
			if (dot == 0 && digits == 0 && c == '0') {
				pfd->pfd_flags |= DT_PFCONV_ZPAD;
				pfd->pfd_flags &= ~DT_PFCONV_LEFT;
				goto fmt_switch;
			}

			for (n = 0; isdigit(c); c = *++p)
				n = n * 10 + c - '0';

			if (dot)
				pfd->pfd_prec = n;
			else
				pfd->pfd_width = n;

			p--;
			digits++;
			goto fmt_switch;

		case '#':
			pfd->pfd_flags |= DT_PFCONV_ALT;
			goto fmt_switch;

		case '*':
			n = dot ? DT_PFCONV_DYNPREC : DT_PFCONV_DYNWIDTH;

			if (pfd->pfd_flags & n) {
				yywarn("format conversion #%u has more than "
				       "one '*' specified for the output %s\n",
				       pfv->pfv_argc,
				       dot ? "precision" : "width");

				dt_printf_destroy(pfv);
				return dt_printf_error(dtp, EDT_COMPILER);
			}

			pfd->pfd_flags |= n;
			goto fmt_switch;

		case '+':
			pfd->pfd_flags |= DT_PFCONV_SPOS;
			goto fmt_switch;

		case '-':
			pfd->pfd_flags |= DT_PFCONV_LEFT;
			pfd->pfd_flags &= ~DT_PFCONV_ZPAD;
			goto fmt_switch;

		case '.':
			if (dot++ != 0) {
				yywarn("format conversion #%u has more than "
				    "one '.' specified\n", pfv->pfv_argc);

				dt_printf_destroy(pfv);
				return dt_printf_error(dtp, EDT_COMPILER);
			}
			digits = 0;
			goto fmt_switch;

		case '?':
			if (dtp->dt_conf.dtc_ctfmodel == CTF_MODEL_LP64)
				pfd->pfd_width = 16;
			else
				pfd->pfd_width = 8;
			goto fmt_switch;

		case '@':
			pfd->pfd_flags |= DT_PFCONV_AGG;
			goto fmt_switch;

		case '\'':
			pfd->pfd_flags |= DT_PFCONV_GROUP;
			goto fmt_switch;

		case ' ':
			pfd->pfd_flags |= DT_PFCONV_SPACE;
			goto fmt_switch;

		case '$':
			yywarn("format conversion #%u uses unsupported "
			    "positional format (%%n$)\n", pfv->pfv_argc);

			dt_printf_destroy(pfv);
			return dt_printf_error(dtp, EDT_COMPILER);

		case '%':
			if (p[-1] == '%')
				goto default_lbl; /* if %% then use "%" conv */

			yywarn("format conversion #%u cannot be combined "
			    "with other format flags: %%%%\n", pfv->pfv_argc);

			dt_printf_destroy(pfv);
			return dt_printf_error(dtp, EDT_COMPILER);

		case '\0':
			yywarn("format conversion #%u name expected before "
			    "end of format string\n", pfv->pfv_argc);

			dt_printf_destroy(pfv);
			return dt_printf_error(dtp, EDT_COMPILER);

		case 'h':
		case 'l':
		case 'L':
		case 'w':
			if (namelen < sizeof(name) - 2)
				name[namelen++] = c;
			goto fmt_switch;

		default_lbl:
		default:
			name[namelen++] = c;
			name[namelen] = '\0';
		}

		pfd->pfd_conv = dt_pfdict_lookup(dtp, name);

		if (pfd->pfd_conv == NULL) {
			yywarn("format conversion #%u is undefined: %%%s\n",
			    pfv->pfv_argc, name);
			dt_printf_destroy(pfv);
			return dt_printf_error(dtp, EDT_COMPILER);
		}
	}

	if (*q != '\0' || *format == '\0') {
		if ((pfd = malloc(sizeof(dt_pfargd_t))) == NULL) {
			dt_printf_destroy(pfv);
			return dt_printf_error(dtp, EDT_NOMEM);
		}

		if (pfv->pfv_argv != NULL)
			nfd->pfd_next = pfd;
		else
			pfv->pfv_argv = pfd;

		memset(pfd, 0, sizeof(dt_pfargd_t));
		pfv->pfv_argc++;

		pfd->pfd_prefix = q;
		pfd->pfd_preflen = strlen(q);
	}

	return pfv;
}

void
dt_printf_destroy(dt_pfargv_t *pfv)
{
	dt_pfargd_t *pfd, *nfd;

	for (pfd = pfv->pfv_argv; pfd != NULL; pfd = nfd) {
		nfd = pfd->pfd_next;
		free(pfd);
	}

	free(pfv->pfv_format);
	free(pfv);
}

void
dt_printf_validate(dt_pfargv_t *pfv, uint_t flags, dt_ident_t *idp, int foff,
		   dtrace_actkind_t kind, dt_node_t *dnp)
{
	dt_pfargd_t *pfd = pfv->pfv_argv;
	const char *func = idp->di_name;

	char n[DT_TYPE_NAMELEN];
	dtrace_typeinfo_t dtt;
	const char *aggtype;
	dt_node_t aggnode;
	int i, j;

	if (pfv->pfv_format[0] == '\0') {
		xyerror(D_PRINTF_FMT_EMPTY,
		    "%s( ) format string is empty\n", func);
	}

	pfv->pfv_flags = flags;

	memset(&aggnode, 0, sizeof(aggnode));

	if (flags & DT_PRINTF_AGGREGATION) {
		/*
		 * We fake up a parse node representing the type that can be
		 * used with an aggregation result conversion, which -- for all
		 * but count() -- is a signed quantity.
		 */
		if (kind != DT_AGG_COUNT)
			aggtype = "int64_t";
		else
			aggtype = "uint64_t";

		if (dt_type_lookup(aggtype, &dtt) != 0)
			xyerror(D_TYPE_ERR, "failed to lookup agg type %s\n",
				aggtype);

		dt_node_type_assign(&aggnode, dtt.dtt_ctfp, dtt.dtt_type);
	}

	for (i = 0, j = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
		const dt_pfconv_t *pfc = pfd->pfd_conv;
		const char *dyns[2];
		int dync = 0;

		char vname[64];
		dt_node_t *vnp;

		if (pfc == NULL)
			continue; /* no checking if argd is just a prefix */

		if (pfc->pfc_print == &pfprint_pct) {
			strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
			continue;
		}

		if (pfd->pfd_flags & DT_PFCONV_DYNPREC)
			dyns[dync++] = ".*";
		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
			dyns[dync++] = "*";

		for (; dync != 0; dync--) {
			if (dnp == NULL) {
				xyerror(D_PRINTF_DYN_PROTO,
				    "%s( ) prototype mismatch: conversion "
				    "#%d (%%%s) is missing a corresponding "
				    "\"%s\" argument\n", func, i + 1,
				    pfc->pfc_name, dyns[dync - 1]);
			}

			if (dt_node_is_integer(dnp) == 0) {
				xyerror(D_PRINTF_DYN_TYPE,
				    "%s( ) argument #%d is incompatible "
				    "with conversion #%d prototype:\n"
				    "\tconversion: %% %s %s\n"
				    "\t prototype: int\n\t  argument: %s\n",
				    func, j + foff + 1, i + 1,
				    dyns[dync - 1], pfc->pfc_name,
				    dt_node_type_name(dnp, n, sizeof(n)));
			}

			dnp = dnp->dn_list;
			j++;
		}

		/*
		 * If this conversion is consuming the aggregation data, set
		 * the value node pointer (vnp) to a fake node based on the
		 * aggregating function result type.  Otherwise assign vnp to
		 * the next parse node in the argument list, if there is one.
		 */
		if (pfd->pfd_flags & DT_PFCONV_AGG) {
			if (!(flags & DT_PRINTF_AGGREGATION))
				xyerror(D_PRINTF_AGG_CONV,
				    "%%@ conversion requires an aggregation"
				    " and is not for use with %s( )\n", func);
			strlcpy(vname, "aggregating action", sizeof(vname));
			vnp = &aggnode;
		} else if (dnp == NULL) {
			xyerror(D_PRINTF_ARG_PROTO,
			    "%s( ) prototype mismatch: conversion #%d (%%"
			    "%s) is missing a corresponding value argument\n",
			    func, i + 1, pfc->pfc_name);
		} else {
			snprintf(vname, sizeof(vname),
			    "argument #%d", j + foff + 1);
			vnp = dnp;
			dnp = dnp->dn_list;
			j++;
		}

		/*
		 * Fill in the proposed final format string by prepending any
		 * size-related prefixes to the pfconv's format string.  The
		 * pfc_check() function below may optionally modify the format
		 * as part of validating the type of the input argument.
		 */
		if (pfc->pfc_print == &pfprint_sint ||
		    pfc->pfc_print == &pfprint_uint ||
		    pfc->pfc_print == &pfprint_dint) {
			if (dt_node_type_size(vnp) == sizeof(uint64_t))
				strcpy(pfd->pfd_fmt, "ll");
		} else if (pfc->pfc_print == &pfprint_fp) {
			if (dt_node_type_size(vnp) == sizeof(long double))
				strcpy(pfd->pfd_fmt, "L");
		}

		strcat(pfd->pfd_fmt, pfc->pfc_ofmt);

		/*
		 * Validate the format conversion against the value node type.
		 * If the conversion is good, create the descriptor format
		 * string by concatenating together any required printf(3C)
		 * size prefixes with the conversion's native format string.
		 */
		if (pfc->pfc_check(pfv, pfd, vnp) == 0)
			xyerror(D_PRINTF_ARG_TYPE,
			    "%s( ) %s is incompatible with "
			    "conversion #%d prototype:\n\tconversion: %%%s\n"
			    "\t prototype: %s\n\t  argument: %s\n", func,
			    vname, i + 1, pfc->pfc_name, pfc->pfc_tstr,
			    dt_node_type_name(vnp, n, sizeof(n)));
	}

	if ((flags & DT_PRINTF_EXACTLEN) && dnp != NULL)
		xyerror(D_PRINTF_ARG_EXTRA,
		    "%s( ) prototype mismatch: only %d arguments "
		    "required by this format string\n", func, j);
}

void
dt_printa_validate(dt_node_t *lhs, dt_node_t *rhs)
{
	dt_ident_t *lid, *rid;
	dt_node_t *lproto, *rproto;
	int largc, rargc, argn;
	char n1[DT_TYPE_NAMELEN];
	char n2[DT_TYPE_NAMELEN];

	assert(lhs->dn_kind == DT_NODE_AGG);
	assert(rhs->dn_kind == DT_NODE_AGG);

	lid = lhs->dn_ident;
	rid = rhs->dn_ident;

	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;

	/*
	 * First, get an argument count on each side.  These must match.
	 */
	for (largc = 0; lproto != NULL; lproto = lproto->dn_list)
		largc++;

	for (rargc = 0; rproto != NULL; rproto = rproto->dn_list)
		rargc++;

	if (largc != rargc) {
		xyerror(D_PRINTA_AGGKEY, "printa( ): @%s and @%s do not have "
		    "matching key signatures: @%s has %d key%s, @%s has %d "
		    "key%s", lid->di_name, rid->di_name,
		    lid->di_name, largc, largc == 1 ? "" : "s",
		    rid->di_name, rargc, rargc == 1 ? "" : "s");
	}

	/*
	 * Now iterate over the keys to verify that each type matches.
	 */
	lproto = ((dt_idsig_t *)lid->di_data)->dis_args;
	rproto = ((dt_idsig_t *)rid->di_data)->dis_args;

	for (argn = 1; lproto != NULL; argn++, lproto = lproto->dn_list,
	    rproto = rproto->dn_list) {
		assert(rproto != NULL);

		if (dt_node_is_argcompat(lproto, rproto))
			continue;

		xyerror(D_PRINTA_AGGPROTO, "printa( ): @%s[ ] key #%d is "
		    "incompatible with @%s:\n%9s key #%d: %s\n"
		    "%9s key #%d: %s\n",
		    rid->di_name, argn, lid->di_name, lid->di_name, argn,
		    dt_node_type_name(lproto, n1, sizeof(n1)), rid->di_name,
		    argn, dt_node_type_name(rproto, n2, sizeof(n2)));
	}
}

static int
dt_printf_getint(dtrace_hdl_t *dtp, const dtrace_recdesc_t *recp,
    uint_t nrecs, const void *buf, size_t len, int *ip)
{
	uintptr_t addr;

	if (nrecs == 0)
		return dt_set_errno(dtp, EDT_DMISMATCH);

	addr = (uintptr_t)buf + recp->dtrd_offset;

	if (addr + recp->dtrd_size > (uintptr_t)buf + len)
		return dt_set_errno(dtp, EDT_DOFFSET);

	if (addr & (recp->dtrd_alignment - 1))
		return dt_set_errno(dtp, EDT_DALIGN);

	switch (recp->dtrd_size) {
	case sizeof(int8_t):
		*ip = (int)*((int8_t *)addr);
		break;
	case sizeof(int16_t):
		*ip = (int)*((int16_t *)addr);
		break;
	case sizeof(int32_t):
		*ip = (int)*((int32_t *)addr);
		break;
	case sizeof(int64_t):
		*ip = (int)*((int64_t *)addr);
		break;
	default:
		return dt_set_errno(dtp, EDT_DMISMATCH);
	}

	return 0;
}

/*ARGSUSED*/
static int
pfprint_average(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		const dt_pfargd_t *pfd, const void *addr, size_t size,
		uint64_t normal, uint64_t sig)
{
	const uint64_t *data = addr;

	if (size != sizeof(uint64_t) * 2)
		return dt_set_errno(dtp, EDT_DMISMATCH);

	return dt_printf(dtp, fp, format,
			 data[0] ? data[1] / normal / data[0] : 0);
}

/*ARGSUSED*/
static int
pfprint_stddev(dtrace_hdl_t *dtp, FILE *fp, const char *format,
	       const dt_pfargd_t *pfd, const void *addr, size_t size,
	       uint64_t normal, uint64_t sig)
{
	const uint64_t *data = addr;

	if (size != sizeof(uint64_t) * 4)
		return dt_set_errno(dtp, EDT_DMISMATCH);

	return dt_printf(dtp, fp, format, dt_stddev((uint64_t *)data, normal));
}

/*ARGSUSED*/
static int
pfprint_quantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		 const dt_pfargd_t *pfd, const void *addr, size_t size,
		 uint64_t normal, uint64_t sig)
{
	return dt_print_quantize(dtp, fp, addr, size, normal);
}

/*ARGSUSED*/
static int
pfprint_lquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		  const dt_pfargd_t *pfd, const void *addr, size_t size,
		  uint64_t normal, uint64_t sig)
{
	return dt_print_lquantize(dtp, fp, addr, size, normal, sig);
}

static int
pfprint_llquantize(dtrace_hdl_t *dtp, FILE *fp, const char *format,
		   const dt_pfargd_t *pfd, const void *addr, size_t size,
		   uint64_t normal, uint64_t sig)
{
	return dt_print_llquantize(dtp, fp, addr, size, normal, sig);
}

static int
dt_printf_format(dtrace_hdl_t *dtp, FILE *fp, const dt_pfargv_t *pfv,
    const dtrace_recdesc_t *recs, uint_t nrecs, const void *buf,
    size_t len, const dtrace_aggdata_t **aggsdata, int naggvars)
{
	dt_pfargd_t *pfd = pfv->pfv_argv;
	const dtrace_recdesc_t *recp = recs;
	const dtrace_aggdata_t *aggdata = NULL; /* gcc -Wmaybe-uninitialized */
	dtrace_aggdesc_t *agg;
	caddr_t lim = (caddr_t)buf + len, limit;
	char format[64] = "%";
	int i, curagg = -1;
	uint64_t normal, sig;

	/*
	 * FIXME: Comment needs rewrite.
	 *
	 * If we are formatting an aggregation, set 'aggrec' to the index of
	 * the final record description (the aggregation result) so we can use
	 * this record index with any conversion where DT_PFCONV_AGG is set.
	 * (The actual aggregation used will vary as we increment through the
	 * aggregation variables that we have been passed.)  Finally, we
	 * decrement nrecs to prevent this record from being used with any
	 * other conversion.
	 */
	if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
		assert(aggsdata != NULL);
		assert(naggvars > 0);

		curagg = naggvars > 1 ? 1 : 0;
		aggdata = aggsdata[0];
	}

	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
		const dt_pfconv_t *pfc = pfd->pfd_conv;
		int width = pfd->pfd_width;
		int prec = pfd->pfd_prec;
		int rval;

		char *f = format + 1; /* skip initial '%' */
		const dtrace_recdesc_t *rec;
		dt_pfprint_f *func;
		caddr_t addr;
		size_t size;
		uint32_t flags = 0;

		if (pfd->pfd_preflen != 0) {
			char *tmp = alloca(pfd->pfd_preflen + 1);

			memcpy(tmp, pfd->pfd_prefix, pfd->pfd_preflen);
			tmp[pfd->pfd_preflen] = '\0';

			if ((rval = dt_printf(dtp, fp, "%s", tmp)) < 0)
				return rval;

			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
				/*
				 * For printa(), we flush the buffer after each
				 * prefix, setting the flags to indicate that
				 * this is part of the printa() format string.
				 */
				flags = DTRACE_BUFDATA_AGGFORMAT;

				if (pfc == NULL && i == pfv->pfv_argc - 1)
					flags |= DTRACE_BUFDATA_AGGLAST;

				if (dt_buffered_flush(dtp, NULL, NULL,
				    aggdata, flags) < 0)
					return -1;
			}
		}

		if (pfc == NULL) {
			if (pfv->pfv_argc == 1)
				return nrecs != 0;
			continue;
		}

		/*
		 * If the conversion is %%, just invoke the print callback
		 * with no data record and continue; it consumes no record.
		 */
		if (pfc->pfc_print == &pfprint_pct) {
			if (pfc->pfc_print(dtp, fp, NULL, pfd, NULL, 0, 1, 0) >= 0)
				continue;
			return -1;		/* errno is set for us */
		}

		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH) {
			if (dt_printf_getint(dtp, recp++, nrecs--, buf, len,
					     &width) == -1)
				return -1;	/* errno is set for us */
			pfd->pfd_dynwidth = width;
		} else
			pfd->pfd_dynwidth = 0;

		if ((pfd->pfd_flags & DT_PFCONV_DYNPREC) &&
		    dt_printf_getint(dtp, recp++, nrecs--, buf, len, &prec) == -1)
			return -1;		/* errno is set for us */

		if (pfd->pfd_flags & DT_PFCONV_AGG) {
			/*
			 * This should be impossible -- the compiler shouldn't
			 * create a DT_PFCONV_AGG conversion without an
			 * aggregation present.  Still, we'd rather fail
			 * gracefully than blow up...
			 */
			if (aggsdata == NULL)
				return dt_set_errno(dtp, EDT_DMISMATCH);

			aggdata = aggsdata[curagg];
			agg = aggdata->dtada_desc;

			/*
			 * We increment the current aggregation variable, but
			 * not beyond the number of aggregation variables that
			 * we're printing. This has the (desired) effect that
			 * DT_PFCONV_AGG conversions beyond the number of
			 * aggregation variables (re-)convert the aggregation
			 * value of the last aggregation variable.
			 */
			if (curagg < naggvars - 1)
				curagg++;

			rec = &agg->dtagd_drecs[DT_AGGDATA_RECORD];
			addr = aggdata->dtada_data + rec->dtrd_offset;
			limit = addr + rec->dtrd_size;
			normal = agg->dtagd_normal;
			sig = agg->dtagd_sig;
			flags = DTRACE_BUFDATA_AGGVAL;
		} else {
			if (nrecs == 0)
				return dt_set_errno(dtp, EDT_DMISMATCH);

			rec = recp++;
			nrecs--;
			normal = 1;
			sig = 0;

			if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
				/*
				 * When printing aggregation keys, we always
				 * set the aggdata to be the representative
				 * (zeroth) aggregation.  The aggdata isn't
				 * actually used here in this case, but it is
				 * passed to the buffer handler and must
				 * therefore still be correct.
				 */
				aggdata = aggsdata[0];
				flags = DTRACE_BUFDATA_AGGKEY;
				addr = (caddr_t)aggdata->dtada_key + rec->dtrd_offset;
				limit = addr + rec->dtrd_size;
			} else {
				addr = (caddr_t)buf + rec->dtrd_offset;
				limit = lim;
			}
		}

		size = rec->dtrd_size;

		if (addr + size > limit) {
			dt_dprintf("bad size: addr=%p size=0x%x lim=%p\n",
			    (void *)addr, rec->dtrd_size, (void *)limit);
			return dt_set_errno(dtp, EDT_DOFFSET);
		}

		if (rec->dtrd_alignment != 0 &&
		    ((uintptr_t)addr & (rec->dtrd_alignment - 1)) != 0) {
			dt_dprintf("bad align: addr=%p size=0x%x align=0x%x\n",
			    (void *)addr, rec->dtrd_size, rec->dtrd_alignment);
			return dt_set_errno(dtp, EDT_DALIGN);
		}

		switch (rec->dtrd_action) {
		case DT_AGG_AVG:
			func = pfprint_average;
			break;
		case DT_AGG_STDDEV:
			func = pfprint_stddev;
			break;
		case DT_AGG_QUANTIZE:
			func = pfprint_quantize;
			break;
		case DT_AGG_LQUANTIZE:
			func = pfprint_lquantize;
			break;
		case DT_AGG_LLQUANTIZE:
			func = pfprint_llquantize;
			break;
		case DTRACEACT_MOD:
			func = pfprint_mod;
			break;
		case DTRACEACT_UMOD:
			func = pfprint_umod;
			break;
		default:
			func = pfc->pfc_print;
			break;
		}

		if (pfd->pfd_flags & DT_PFCONV_ALT)
			*f++ = '#';
		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
			*f++ = '0';
		if (width < 0 || (pfd->pfd_flags & DT_PFCONV_LEFT))
			*f++ = '-';
		if (pfd->pfd_flags & DT_PFCONV_SPOS)
			*f++ = '+';
		if (pfd->pfd_flags & DT_PFCONV_GROUP)
			*f++ = '\'';
		if (pfd->pfd_flags & DT_PFCONV_SPACE)
			*f++ = ' ';

		/*
		 * If we're printing a stack and DT_PFCONV_LEFT is set, we
		 * don't add the width to the format string.  See the block
		 * comment in pfprint_stack() for a description of the
		 * behavior in this case.
		 */
		if (func == pfprint_stack && (pfd->pfd_flags & DT_PFCONV_LEFT))
			width = 0;

		if (width != 0)
			f += snprintf(f, sizeof(format) - (f - format), "%d",
				      ABS(width));

		if (prec > 0)
			f += snprintf(f, sizeof(format) - (f - format), ".%d",
				      prec);

		strcpy(f, pfd->pfd_fmt);
		pfd->pfd_rec = rec;

		if (func(dtp, fp, format, pfd, addr, size, normal, sig) < 0)
			return -1; /* errno is set for us */

		if (pfv->pfv_flags & DT_PRINTF_AGGREGATION) {
			/*
			 * For printa(), we flush the buffer after each tuple
			 * element, inidicating that this is the last record
			 * as appropriate.
			 */
			if (i == pfv->pfv_argc - 1)
				flags |= DTRACE_BUFDATA_AGGLAST;

			if (dt_buffered_flush(dtp, NULL,
			    rec, aggdata, flags) < 0)
				return -1;
		}
	}

	return (int)(recp - recs);
}

int
dtrace_sprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
    const dtrace_recdesc_t *recp, uint_t nrecs, const void *buf, size_t len)
{
	dtrace_optval_t size;
	int rval;

	rval = dtrace_getopt(dtp, "strsize", &size);
	assert(rval == 0);
	assert(dtp->dt_sprintf_buflen == 0);

	if (dtp->dt_sprintf_buf != NULL)
		free(dtp->dt_sprintf_buf);

	if ((dtp->dt_sprintf_buf = malloc(size)) == NULL)
		return dt_set_errno(dtp, EDT_NOMEM);

	memset(dtp->dt_sprintf_buf, 0, size);
	dtp->dt_sprintf_buflen = size;
	rval = dt_printf_format(dtp, fp, fmtdata, recp, nrecs, buf, len,
	    NULL, 0);
	dtp->dt_sprintf_buflen = 0;

	if (rval == -1)
		free(dtp->dt_sprintf_buf);

	return rval;
}

/*ARGSUSED*/
int
dtrace_system(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
    const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
    uint_t nrecs, const void *buf, size_t len)
{
	int rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);

	if (rval == -1)
		return rval;

	/*
	 * Before we execute the specified command, flush fp to assure that
	 * any prior dt_printf()'s appear before the output of the command
	 * not after it.
	 */
	fflush(fp);

	if (system(dtp->dt_sprintf_buf) == -1)
		return dt_set_errno(dtp, errno);

	return rval;
}

int
dtrace_freopen(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
    const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
    uint_t nrecs, const void *buf, size_t len)
{
	char tmpbuf[40];
	FILE *nfp;
	int rval, errval;
	dt_pfargv_t *pfv = fmtdata;
	dt_pfargd_t *pfd = pfv->pfv_argv;

	rval = dtrace_sprintf(dtp, fp, fmtdata, recp, nrecs, buf, len);

	if (rval == -1 || fp == NULL)
		return rval;

	if (pfd->pfd_preflen != 0 &&
	    strcmp(pfd->pfd_prefix, DT_FREOPEN_RESTORE) == 0) {
		/*
		 * The only way to have the format string set to the value
		 * DT_FREOPEN_RESTORE is via the empty freopen() string --
		 * denoting that we should restore the old stdout.
		 */
		assert(strcmp(dtp->dt_sprintf_buf, DT_FREOPEN_RESTORE) == 0);

		if (dtp->dt_stdout_fd == -1) {
			/*
			 * We could complain here by generating an error,
			 * but it seems like overkill:  it seems that calling
			 * freopen() to restore stdout when freopen() has
			 * never before been called should just be a no-op,
			 * so we just return in this case.
			 */
			return rval;
		}

		snprintf(tmpbuf, sizeof(tmpbuf),
		    "/dev/fd/%d", dtp->dt_stdout_fd);
		free(dtp->dt_freopen_filename);
		dtp->dt_freopen_filename = strndup(tmpbuf, sizeof(tmpbuf));
	} else {
		free(dtp->dt_freopen_filename);
		dtp->dt_freopen_filename = strdup(dtp->dt_sprintf_buf);
	}

	/*
	 * freopen(3C) will always close the specified stream and underlying
	 * file descriptor -- even if the specified file can't be opened.
	 * Even for the semantic cesspool that is standard I/O, this is
	 * surprisingly brain-dead behavior:  it means that any failure to
	 * open the specified file destroys the specified stream in the
	 * process -- which is particularly relevant when the specified stream
	 * happens (or rather, happened) to be stdout.  This could be resolved
	 * were there an "fdreopen()" equivalent of freopen() that allowed one
	 * to pass a file descriptor instead of the name of a file, but there
	 * is no such thing.  However, we can effect this ourselves by first
	 * fopen()'ing the desired file, and then (assuming that that works),
	 * freopen()'ing "/dev/fd/[fileno]", where [fileno] is the underlying
	 * file descriptor for the fopen()'d file.  This way, if the fopen()
	 * fails, we can fail the operation without destroying stdout.
	 */
	if ((nfp = fopen(dtp->dt_freopen_filename, "ace")) == NULL) {
		char *faultstr;

		asprintf(&faultstr, "couldn't freopen() \"%s\": %s",
				dtp->dt_freopen_filename, strerror(errno));

		if ((errval = dt_handle_liberr(dtp, data, faultstr)) == 0)
			return rval;

		free(faultstr);

		return errval;
	}

	snprintf(tmpbuf, sizeof(tmpbuf), "/dev/fd/%d", fileno(nfp));

	if (dtp->dt_stdout_fd == -1) {
		/*
		 * If this is the first time that we're calling freopen(),
		 * we're going to stash away the file descriptor for stdout.
		 * We don't expect the dup(2) to fail, so if it does we must
		 * return failure.
		 */
		if ((dtp->dt_stdout_fd = dup(fileno(fp))) == -1) {
			fclose(nfp);
			return dt_set_errno(dtp, errno);
		}
	}

	if (freopen(tmpbuf, "aF", fp) == NULL) {
		fclose(nfp);
		return dt_set_errno(dtp, errno);
	}

	fclose(nfp);

	return rval;
}

/*ARGSUSED*/
int
dtrace_fprintf(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
    const dtrace_probedata_t *data, const dtrace_recdesc_t *recp,
    uint_t nrecs, const void *buf, size_t len)
{
	return dt_printf_format(dtp, fp, fmtdata,
	    recp, nrecs, buf, len, NULL, 0);
}

void *
dtrace_printf_create(dtrace_hdl_t *dtp, const char *s)
{
	dt_pfargv_t *pfv = dt_printf_create(dtp, s);
	dt_pfargd_t *pfd;
	int i;

	if (pfv == NULL)
		return NULL;		/* errno has been set for us */

	pfd = pfv->pfv_argv;

	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
		const dt_pfconv_t *pfc = pfd->pfd_conv;

		if (pfc == NULL)
			continue;

		/*
		 * If the output format is not %s then we assume that we have
		 * been given a correctly-sized format string, so we copy the
		 * true format name including the size modifier.  If the output
		 * format is %s, then either the input format is %s as well or
		 * it is one of our custom formats (e.g. pfprint_addr), so we
		 * must set pfd_fmt to be the output format conversion "s".
		 */
		if (strcmp(pfc->pfc_ofmt, "s") != 0)
			strcat(pfd->pfd_fmt, pfc->pfc_name);
		else
			strcat(pfd->pfd_fmt, pfc->pfc_ofmt);
	}

	return pfv;
}

void *
dtrace_printa_create(dtrace_hdl_t *dtp, const char *s)
{
	dt_pfargv_t *pfv = dtrace_printf_create(dtp, s);

	if (pfv == NULL)
		return NULL;		/* errno has been set for us */

	pfv->pfv_flags |= DT_PRINTF_AGGREGATION;

	return pfv;
}

/*ARGSUSED*/
size_t
dtrace_printf_format(dtrace_hdl_t *dtp, void *fmtdata, char *s, size_t len)
{
	dt_pfargv_t *pfv = fmtdata;
	dt_pfargd_t *pfd = pfv->pfv_argv;

	/*
	 * An upper bound on the string length is the length of the original
	 * format string, plus three times the number of conversions (each
	 * conversion could add up an additional "ll" and/or pfd_width digit
	 * in the case of converting %? to %16) plus one for a terminating \0.
	 */
	size_t formatlen = strlen(pfv->pfv_format) + 3 * pfv->pfv_argc + 1;
	char *format = alloca(formatlen);
	char *f = format;
	int i, j;

	for (i = 0; i < pfv->pfv_argc; i++, pfd = pfd->pfd_next) {
		const dt_pfconv_t *pfc = pfd->pfd_conv;
		const char *str;
		int width = pfd->pfd_width;
		int prec = pfd->pfd_prec;

		if (pfd->pfd_preflen != 0) {
			for (j = 0; j < pfd->pfd_preflen; j++)
				*f++ = pfd->pfd_prefix[j];
		}

		if (pfc == NULL)
			continue;

		*f++ = '%';

		if (pfd->pfd_flags & DT_PFCONV_ALT)
			*f++ = '#';
		if (pfd->pfd_flags & DT_PFCONV_ZPAD)
			*f++ = '0';
		if (pfd->pfd_flags & DT_PFCONV_LEFT)
			*f++ = '-';
		if (pfd->pfd_flags & DT_PFCONV_SPOS)
			*f++ = '+';
		if (pfd->pfd_flags & DT_PFCONV_DYNWIDTH)
			*f++ = '*';
		if (pfd->pfd_flags & DT_PFCONV_DYNPREC) {
			*f++ = '.';
			*f++ = '*';
		}
		if (pfd->pfd_flags & DT_PFCONV_GROUP)
			*f++ = '\'';
		if (pfd->pfd_flags & DT_PFCONV_SPACE)
			*f++ = ' ';
		if (pfd->pfd_flags & DT_PFCONV_AGG)
			*f++ = '@';

		if (width != 0)
			f += snprintf(f, formatlen - (f - format), "%d",
				      width);

		if (prec != 0)
			f += snprintf(f, formatlen - (f - format), ".%d",
				      prec);

		/*
		 * If the output format is %s, then either %s is the underlying
		 * conversion or the conversion is one of our customized ones,
		 * e.g. pfprint_addr.  In these cases, put the original string
		 * name of the conversion (pfc_name) into the pickled format
		 * string rather than the derived conversion (pfd_fmt).
		 */
		if (strcmp(pfc->pfc_ofmt, "s") == 0)
			str = pfc->pfc_name;
		else
			str = pfd->pfd_fmt;

		for (j = 0; str[j] != '\0'; j++)
			*f++ = str[j];
	}

	*f = '\0'; /* insert nul byte; do not count in return value */

	assert(f < format + formatlen);
	strncpy(s, format, len);

	return (size_t)(f - format);
}

static int
dt_fprinta(const dtrace_aggdata_t *adp, void *arg)
{
	const dtrace_aggdesc_t	*agg = adp->dtada_desc;
	const dtrace_recdesc_t	*rec = &agg->dtagd_krecs[1];
	uint_t			nrecs = agg->dtagd_nkrecs - 1;
	dt_pfwalk_t		*pfw = arg;
	dtrace_hdl_t		*dtp = pfw->pfw_argv->pfv_dtp;

	if (pfw->pfw_aid != agg->dtagd_varid)
		return 0;	/* id does not match */

	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv, rec, nrecs,
			     adp->dtada_key, agg->dtagd_dsize, &adp, 1) == -1) {
		pfw->pfw_err = dtp->dt_errno;
		return DTRACE_AGGWALK_ERROR;
	}

	/*
	 * Cast away the const to set the bit indicating that this aggregation
	 * has been printed.
	 */
	((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;

	return DTRACE_AGGWALK_NEXT;
}

static int
dt_fprintas(const dtrace_aggdata_t **aggsdata, int naggvars, void *arg)
{
	const dtrace_aggdata_t	*aggdata = aggsdata[0];
	const dtrace_aggdesc_t	*agg = aggdata->dtada_desc;
	const dtrace_recdesc_t	*rec = &agg->dtagd_krecs[1];
	uint_t			nrecs = agg->dtagd_nkrecs - 1;
	dt_pfwalk_t		*pfw = arg;
	dtrace_hdl_t		*dtp = pfw->pfw_argv->pfv_dtp;
	int			i;

	if (dt_printf_format(dtp, pfw->pfw_fp, pfw->pfw_argv, rec, nrecs,
			     aggdata->dtada_key, agg->dtagd_dsize,
			     aggsdata, naggvars) == -1) {
		pfw->pfw_err = dtp->dt_errno;
		return DTRACE_AGGWALK_ERROR;
	}

	/*
	 * For each aggregation, indicate that it has been printed, casting
	 * away the const as necessary.
	 */
	for (i = 1; i < naggvars; i++) {
		agg = aggsdata[i]->dtada_desc;
		((dtrace_aggdesc_t *)agg)->dtagd_flags |= DTRACE_AGD_PRINTED;
	}

	return DTRACE_AGGWALK_NEXT;
}

int
dtrace_fprinta(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
	       const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
	       uint_t nrecs, const void *buf, size_t len)
{
	dt_pfwalk_t	pfw;
	int		i, naggvars = 0;
	dtrace_aggid_t	*aggvars;

	aggvars = alloca(nrecs * sizeof(dtrace_aggid_t));

	/*
	 * This might be a printa() with multiple aggregation variables.  We
	 * need to scan forward through the records until we find a record that
	 * does not belong to this printa() statement.
	 */
	for (i = 0; i < nrecs; i++) {
		const dtrace_recdesc_t *nrec = &recs[i];

		if (nrec->dtrd_arg != recs->dtrd_arg)
			break;

		if (nrec->dtrd_action != recs->dtrd_action)
			return dt_set_errno(dtp, EDT_BADAGG);

		aggvars[naggvars++] =
		    /* LINTED - alignment */
		    *((dtrace_aggid_t *)((caddr_t)buf + nrec->dtrd_offset));
	}

	if (naggvars == 0)
		return dt_set_errno(dtp, EDT_BADAGG);

	pfw.pfw_argv = fmtdata;
	pfw.pfw_fp = fp;
	pfw.pfw_err = 0;

	if (naggvars == 1) {
		pfw.pfw_aid = aggvars[0];

		if (dtrace_aggregate_walk_sorted(dtp, dt_fprinta, &pfw) == -1 ||
		    pfw.pfw_err != 0)
			return -1; /* errno is set for us */
	} else {
		if (dtrace_aggregate_walk_joined(dtp, aggvars, naggvars,
						 dt_fprintas, &pfw) == -1 ||
		    pfw.pfw_err != 0)
			return -1; /* errno is set for us */
	}

	return i;
}

#define DT_PRINT_DEPTH		10

struct dt_visit_arg {
	dtrace_hdl_t	*dv_dtp;
	FILE		*dv_fp;
	ctf_file_t	*dv_ctfp;
	void		*dv_data;
	uint64_t	dv_size;
	int		dv_kinds[DT_PRINT_DEPTH];
	int		dv_last_depth;
	int		dv_startindent;
};

#define DT_NAME_LEN		32
#define DT_PRINT_LEN		80

#define DT_PRINT_STARTINDENT	44

/* used to compare nested objects like structs/unions/arrays to see if they are 0. */
static char zeros[65536] = { 0 };

static int
dt_print_close_parens(struct dt_visit_arg *dva, int depth)
{
	int i;

	switch (dva->dv_kinds[depth]) {
	case CTF_K_STRUCT:
	case CTF_K_UNION:
		/* if struct/union contents were 0, we still opened the
		 * struct "{" at the top level, even if we did not
		 * display any members, so need to close it.  An empty
		 * struct looks like this:
		 * (struct foo) {
		 * }
		 *
		 * ...at the top level, but at any other depth of nesting
		 * it is not displayed at all.
		 */
		if (depth == 0 && dva->dv_last_depth == 0)
			dva->dv_last_depth = 1;
		/* add a "}" for each level needed between current depth
		 * and last depth we displayed data at.
		 */
		for (i = dva->dv_last_depth - depth; i > 0; i--) {
			if (dt_printf(dva->dv_dtp, dva->dv_fp, "%*s}%s\n",
				      dva->dv_startindent + depth + i - 1, "",
				      (depth > 0 || i > 1) ? "," : "") < 0)
				return -1;
			dva->dv_last_depth = depth;
		}
		break;
	}
	return 0;
}

static int
dt_print_visit(const char *name, ctf_id_t type, unsigned long offset,
	       int depth, void *arg)
{
	struct dt_visit_arg *dva = arg;
	const void *data = (char *)dva->dv_data + offset/NBBY;
	const char *membername = NULL;
	ssize_t typesize, asize = 0;
	char typename[DT_TYPE_NAMELEN];
	char *datastr = NULL;
	int64_t intdata = 0;
	const char *ename;
	ctf_encoding_t e;
	ctf_arinfo_t a;
	ctf_id_t rtype;
	size_t typelen;
	int issigned;
	int ret = 0;
	int rkind;
	int i;

	/* run out of data to display, or hit depth limit? */
	if ((offset/NBBY) >= dva->dv_size || depth >= DT_PRINT_DEPTH) {
		dt_printf(dva->dv_dtp, dva->dv_fp, "%*s...\n",
			  dva->dv_startindent + depth, "");
		return -1;
	}
	if (ctf_type_name(dva->dv_ctfp, type, typename, sizeof(typename)) == NULL) {
		dt_dprintf("error retrieving type name for [%ld]: %s\n",
			   type, ctf_errmsg(ctf_errno(dva->dv_ctfp)));
		return dt_set_errno(dva->dv_dtp, EDT_CTF);
	}
	rtype = ctf_type_resolve(dva->dv_ctfp, type);
	if (rtype == CTF_ERR) {
		dt_dprintf("error resolving type [%s]: %s\n", typename,
			   ctf_errmsg(ctf_errno(dva->dv_ctfp)));
		ret = dt_set_errno(dva->dv_dtp, EDT_CTF);
		goto err;
	}
	rkind = ctf_type_kind(dva->dv_ctfp, rtype);

	/* we may have just shown the last member in a struct/union
	 * at depth + 1 (or deeper than that).  If so close the "{".
	 * The final parenthesis close (at depth 0) will be done
	 * by the caller.
	 */
	if (dva->dv_last_depth > depth)
		dt_print_close_parens(dva, depth);

	/* zeroed data? if so, do not display it. */
	if (depth > 0) {
		typesize = ctf_type_size(dva->dv_ctfp, rtype);
		if (typesize < 0) {
			dt_dprintf("error retrieving type size for [%s]: %s\n",
				   typename, ctf_errmsg(ctf_errno(dva->dv_ctfp)));
			ret = dt_set_errno(dva->dv_dtp, EDT_CTF);
			goto err;
		}
		if (typesize < sizeof(zeros) && memcmp(data, zeros, typesize) == 0)
			return 0;
	}

	dva->dv_kinds[depth] = rkind;
	dva->dv_last_depth = depth;

	/* anon struct/union/enum will be "struct "; remove the space. */
	typelen = strlen(typename);
	if (typename[typelen - 1] == ' ')
		typename[typelen - 1] = '\0';

	/* print .<member_name> where available */
	if (depth > 0) {
		if (name && strlen(name) > 0)
			membername = name;
	}

	switch (rkind) {
	case CTF_K_STRUCT:
	case CTF_K_UNION:
		ret = asprintf(&datastr, "(%s) {", typename);
		break;
	case CTF_K_ARRAY:
		ret = ctf_array_info(dva->dv_ctfp, rtype, &a);
		if (ret < 0) {
			dt_dprintf("error retrieving array info for [%s]: %s\n",
				   typename, ctf_errmsg(ctf_errno(dva->dv_ctfp)));
			ret = dt_set_errno(dva->dv_dtp, EDT_CTF);
			goto err;
		}
		asize = ctf_type_size(dva->dv_ctfp, a.ctr_contents);
		if (asize < 0) {
			dt_dprintf("error retrieving type size for array contents in [%s]: %s\n",
				   typename, ctf_errmsg(ctf_errno(dva->dv_ctfp)));
			ret = dt_set_errno(dva->dv_dtp, EDT_CTF);
			goto err;
		}
		ret = asprintf(&datastr, "(%s) [%s", typename,
			       a.ctr_nelems == 0 ? "]," : "");
		/* array member processing will be handled below... */
		break;
	case CTF_K_INTEGER:
		ret = ctf_type_encoding(dva->dv_ctfp, rtype, &e);
		if (ret < 0) {
			dt_dprintf("error retrieving type encoding for [%s]: %s\n",
				   typename, ctf_errmsg(ctf_errno(dva->dv_ctfp)));
			ret = dt_set_errno(dva->dv_dtp, EDT_CTF);
			goto err;
		}
		issigned = (e.cte_format & CTF_INT_SIGNED) != 0;
		switch (e.cte_bits) {
		case 8:
			if (issigned)
				intdata = (int64_t)*(char *)data;
			else
				intdata = (int64_t)*(unsigned char *)data;
			break;
		case 16:
			if (issigned)
				intdata = (int64_t)*(int16_t *)data;
			else
				intdata = (int64_t)*(uint16_t *)data;
			break;
		case 32:
			if (issigned)
				intdata = (int64_t)*(int32_t *)data;
			else
				intdata = (int64_t)*(uint32_t *)data;
			break;
		case 64:
			if (issigned) {
				intdata = (int64_t)*(int64_t *)data;
			} else {
				/* cannot use intdata (int64_t) to hold uin64_t;
				 * stringify the cast and data here instead.
				 */
				ret = asprintf(&datastr, "(%s)%lu,",
					       typename, *(uint64_t *)data);
				goto doprint;
			}
			break;
		default:
			if (e.cte_bits <= 64) {
				unsigned int lshift, rshift;
				uint64_t bitdata = *(uint64_t *)((char *)dva->dv_data +
						(offset + e.cte_offset)/NBBY);
				/* shift bitfield data left then right to
				 * eliminate unneeded bits.
				 */
				lshift = 63 - ((offset + e.cte_offset) % 64);
				rshift = 64 - e.cte_bits;
				bitdata = bitdata << lshift;
				bitdata = bitdata >> rshift;
				intdata = (int64_t)bitdata;
				/* zero-value bitfield; do not display */
				if (intdata == 0)
					return 0;
			} else {
				/* > 64 bit integer vals not supported. */
				ret = asprintf(&datastr, "(%s)?(%d bit value),",
					       typename, e.cte_bits);
				goto doprint;
			}
			break;
		}
		if ((e.cte_format & CTF_INT_CHAR) != 0 &&
		    strcmp(typename, "char") == 0) {
			ret = asprintf(&datastr, "(%s)'%c',",
				       typename, (char)intdata);
		} else {
			ret = asprintf(&datastr, "(%s)%ld,",
				       typename, intdata);
		}
		break;
	case CTF_K_ENUM:
		ename = ctf_enum_name(dva->dv_ctfp, rtype, *(int *)data);
		if (ename != NULL) {
			/* exception to printing 0-valued data here;
			 * enum values are often meaningful with 0
			 * value so display the enum value string.
			 */
			ret = asprintf(&datastr, "(%s)%s,",
				       typename, ename);
		} else {
			ret = asprintf(&datastr, "(%s)%d,",
				       typename, *(int *)data);
		}
		break;
	case CTF_K_POINTER:
		ret = asprintf(&datastr, "(%s)0x%lx,",
			 typename, (uintptr_t)*(void **)data);
		break;
	default:
		return 0;
	}
doprint:
	if (ret < 0) {
		/* asprintf() failed... */
		ret = dt_set_errno(dva->dv_dtp, EDT_NOMEM);
		goto err;
	}

	/* format is
	 * [.<membername> = ](<type>)value,
	 * For example
	 * .foo = (bar)1,
	 */
	ret = dt_printf(dva->dv_dtp, dva->dv_fp, "%*s%s%s%s%s\n",
			dva->dv_startindent + depth, "",
			membername != NULL ? "." : "",
			membername != NULL ? membername : "",
			membername != NULL ? " = " : "",
			datastr);
	free(datastr);
	if (ret < 0)
		return ret;

	/* type visit does not recurse into array elements... */
	if (rkind == CTF_K_ARRAY) {
		struct dt_visit_arg dva2;
		int ischar = 0;

		dva2.dv_dtp = dva->dv_dtp;
		dva2.dv_ctfp = dva->dv_ctfp;
		dva2.dv_fp = dva->dv_fp;
		dva2.dv_data = dva->dv_data;
		dva2.dv_startindent = dva->dv_startindent;
		dva2.dv_size = dva->dv_size;
		dva2.dv_last_depth = dva->dv_last_depth;
		if (ctf_type_encoding(dva->dv_ctfp, a.ctr_contents, &e) < 0) {
			dt_dprintf("error retrieving type encoding for array contents in [%ld]: %s\n",
				   a.ctr_contents,
				   ctf_errmsg(ctf_errno(dva->dv_ctfp)));
			return dt_set_errno(dva->dv_dtp, EDT_CTF);
		}
		/* is array all 0s? Then skip... */
		if (memcmp(data, zeros, asize * a.ctr_nelems) == 0)
			return 0;
		/* we check for early '\0' in char arrays... */
		if ((e.cte_format & CTF_INT_CHAR) != 0 && e.cte_bits == 8)
			ischar = 1;

		for (i = 0; i < a.ctr_nelems; i++) {
			int aoffset = (asize * 8) * i;

			if (ischar && *(char *)((char *)data + i) == '\0')
				break;
			if (dt_print_visit(NULL, a.ctr_contents,
					   offset + aoffset,
					   depth + 1, &dva2) < 0)
				return -1;
		}
		if (a.ctr_nelems > 0) {
			if (dt_printf(dva->dv_dtp, dva->dv_fp, "%*s],\n",
				      dva->dv_startindent + depth, "") < 0)
				return -1;
		}
	}
	return 0;
err:
	free(datastr);
	return ret;
}

int
dt_print_type(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
	      const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
	      uint_t nrecs, const void *buf, size_t len)
{
	const char *modname = ((dt_pfargv_t *)fmtdata)->pfv_format;
	size_t max_size = dtp->dt_options[DTRACEOPT_PRINTSIZE];
	ctf_id_t type = (ctf_id_t)recs->dtrd_arg;
	const dtrace_recdesc_t *data_rec = recs + 1;
	size_t size = data_rec->dtrd_size;
	caddr_t addr_data = (caddr_t)buf;
	struct dt_visit_arg dva;
	uint64_t printaddr;
	dt_module_t *dmp;

	if (size > max_size)
		size = max_size;

	if (dt_read_scalar(addr_data, recs, &printaddr) < 0)
		return dt_set_errno(dtp, EDT_PRINT);

	dmp = dt_module_lookup_by_name(dtp, modname);
	if (!dmp) {
		return dt_set_errno(dtp, EDT_PRINT);
	}
	dva.dv_dtp = dtp;
	dva.dv_fp = fp;
	dva.dv_ctfp = dmp->dm_ctfp;
	dva.dv_data = addr_data + data_rec->dtrd_offset;
	dva.dv_size = size;
	dva.dv_last_depth = 0;
	dva.dv_startindent = DT_PRINT_STARTINDENT;

	/* print address as * to type; type display will start on the
	 * next line.
	 */
	if (dt_printf(dtp, fp, "%p = *\n", (void *)printaddr) < 0)
		return -1;

	ctf_type_visit(dva.dv_ctfp, type, dt_print_visit, &dva);

	/* may need to close top-level parenthesis for struct/union. */
	if (dt_print_close_parens(&dva, 0) < 0)
		return -1;

	return 2;
}

int
dt_print_stack(dtrace_hdl_t *dtp, FILE *fp, void *fmtdata,
	       const dtrace_probedata_t *data, const dtrace_recdesc_t *recs,
	        uint_t nrecs, const void *buf, size_t len)
{
	const char	*format;
	dt_pfargd_t	pfd;

	if (fmtdata == NULL)
		format = "%s";
	else
		format = ((dt_pfargv_t *)fmtdata)->pfv_format;

	/* pfprint_stack() uses pfd_flags and pfd_width only */
	memset(&pfd, 0, sizeof(pfd));
	pfd.pfd_flags = DT_PFCONV_LEFT;

	if (dtp->dt_options[DTRACEOPT_STACKINDENT] != DTRACEOPT_UNSET)
		pfd.pfd_width = (int)dtp->dt_options[DTRACEOPT_STACKINDENT];
	else
		pfd.pfd_width = _dtrace_stkindent;

	return pfprint_stack(dtp, fp, format, &pfd, data->dtpda_data, 0, 0, 0);
}