Loading...
    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
 2525
 2526
 2527
 2528
 2529
 2530
 2531
 2532
 2533
 2534
 2535
 2536
 2537
 2538
 2539
 2540
 2541
 2542
 2543
 2544
 2545
 2546
 2547
 2548
 2549
 2550
 2551
 2552
 2553
 2554
 2555
 2556
 2557
 2558
 2559
 2560
 2561
 2562
 2563
 2564
 2565
 2566
 2567
 2568
 2569
 2570
 2571
 2572
 2573
 2574
 2575
 2576
 2577
 2578
 2579
 2580
 2581
 2582
 2583
 2584
 2585
 2586
 2587
 2588
 2589
 2590
 2591
 2592
 2593
 2594
 2595
 2596
 2597
 2598
 2599
 2600
 2601
 2602
 2603
 2604
 2605
 2606
 2607
 2608
 2609
 2610
 2611
 2612
 2613
 2614
 2615
 2616
 2617
 2618
 2619
 2620
 2621
 2622
 2623
 2624
 2625
 2626
 2627
 2628
 2629
 2630
 2631
 2632
 2633
 2634
 2635
 2636
 2637
 2638
 2639
 2640
 2641
 2642
 2643
 2644
 2645
 2646
 2647
 2648
 2649
 2650
 2651
 2652
 2653
 2654
 2655
 2656
 2657
 2658
 2659
 2660
 2661
 2662
 2663
 2664
 2665
 2666
 2667
 2668
 2669
 2670
 2671
 2672
 2673
 2674
 2675
 2676
 2677
 2678
 2679
 2680
 2681
 2682
 2683
 2684
 2685
 2686
 2687
 2688
 2689
 2690
 2691
 2692
 2693
 2694
 2695
 2696
 2697
 2698
 2699
 2700
 2701
 2702
 2703
 2704
 2705
 2706
 2707
 2708
 2709
 2710
 2711
 2712
 2713
 2714
 2715
 2716
 2717
 2718
 2719
 2720
 2721
 2722
 2723
 2724
 2725
 2726
 2727
 2728
 2729
 2730
 2731
 2732
 2733
 2734
 2735
 2736
 2737
 2738
 2739
 2740
 2741
 2742
 2743
 2744
 2745
 2746
 2747
 2748
 2749
 2750
 2751
 2752
 2753
 2754
 2755
 2756
 2757
 2758
 2759
 2760
 2761
 2762
 2763
 2764
 2765
 2766
 2767
 2768
 2769
 2770
 2771
 2772
 2773
 2774
 2775
 2776
 2777
 2778
 2779
 2780
 2781
 2782
 2783
 2784
 2785
 2786
 2787
 2788
 2789
 2790
 2791
 2792
 2793
 2794
 2795
 2796
 2797
 2798
 2799
 2800
 2801
 2802
 2803
 2804
 2805
 2806
 2807
 2808
 2809
 2810
 2811
 2812
 2813
 2814
 2815
 2816
 2817
 2818
 2819
 2820
 2821
 2822
 2823
 2824
 2825
 2826
 2827
 2828
 2829
 2830
 2831
 2832
 2833
 2834
 2835
 2836
 2837
 2838
 2839
 2840
 2841
 2842
 2843
 2844
 2845
 2846
 2847
 2848
 2849
 2850
 2851
 2852
 2853
 2854
 2855
 2856
 2857
 2858
 2859
 2860
 2861
 2862
 2863
 2864
 2865
 2866
 2867
 2868
 2869
 2870
 2871
 2872
 2873
 2874
 2875
 2876
 2877
 2878
 2879
 2880
 2881
 2882
 2883
 2884
 2885
 2886
 2887
 2888
 2889
 2890
 2891
 2892
 2893
 2894
 2895
 2896
 2897
 2898
 2899
 2900
 2901
 2902
 2903
 2904
 2905
 2906
 2907
 2908
 2909
 2910
 2911
 2912
 2913
 2914
 2915
 2916
 2917
 2918
 2919
 2920
 2921
 2922
 2923
 2924
 2925
 2926
 2927
 2928
 2929
 2930
 2931
 2932
 2933
 2934
 2935
 2936
 2937
 2938
 2939
 2940
 2941
 2942
 2943
 2944
 2945
 2946
 2947
 2948
 2949
 2950
 2951
 2952
 2953
 2954
 2955
 2956
 2957
 2958
 2959
 2960
 2961
 2962
 2963
 2964
 2965
 2966
 2967
 2968
 2969
 2970
 2971
 2972
 2973
 2974
 2975
 2976
 2977
 2978
 2979
 2980
 2981
 2982
 2983
 2984
 2985
 2986
 2987
 2988
 2989
 2990
 2991
 2992
 2993
 2994
 2995
 2996
 2997
 2998
 2999
 3000
 3001
 3002
 3003
 3004
 3005
 3006
 3007
 3008
 3009
 3010
 3011
 3012
 3013
 3014
 3015
 3016
 3017
 3018
 3019
 3020
 3021
 3022
 3023
 3024
 3025
 3026
 3027
 3028
 3029
 3030
 3031
 3032
 3033
 3034
 3035
 3036
 3037
 3038
 3039
 3040
 3041
 3042
 3043
 3044
 3045
 3046
 3047
 3048
 3049
 3050
 3051
 3052
 3053
 3054
 3055
 3056
 3057
 3058
 3059
 3060
 3061
 3062
 3063
 3064
 3065
 3066
 3067
 3068
 3069
 3070
 3071
 3072
 3073
 3074
 3075
 3076
 3077
 3078
 3079
 3080
 3081
 3082
 3083
 3084
 3085
 3086
 3087
 3088
 3089
 3090
 3091
 3092
 3093
 3094
 3095
 3096
 3097
 3098
 3099
 3100
 3101
 3102
 3103
 3104
 3105
 3106
 3107
 3108
 3109
 3110
 3111
 3112
 3113
 3114
 3115
 3116
 3117
 3118
 3119
 3120
 3121
 3122
 3123
 3124
 3125
 3126
 3127
 3128
 3129
 3130
 3131
 3132
 3133
 3134
 3135
 3136
 3137
 3138
 3139
 3140
 3141
 3142
 3143
 3144
 3145
 3146
 3147
 3148
 3149
 3150
 3151
 3152
 3153
 3154
 3155
 3156
 3157
 3158
 3159
 3160
 3161
 3162
 3163
 3164
 3165
 3166
 3167
 3168
 3169
 3170
 3171
 3172
 3173
 3174
 3175
 3176
 3177
 3178
 3179
 3180
 3181
 3182
 3183
 3184
 3185
 3186
 3187
 3188
 3189
 3190
 3191
 3192
 3193
 3194
 3195
 3196
 3197
 3198
 3199
 3200
 3201
 3202
 3203
 3204
 3205
 3206
 3207
 3208
 3209
 3210
 3211
 3212
 3213
 3214
 3215
 3216
 3217
 3218
 3219
 3220
 3221
 3222
 3223
 3224
 3225
 3226
 3227
 3228
 3229
 3230
 3231
 3232
 3233
 3234
 3235
 3236
 3237
 3238
 3239
 3240
 3241
 3242
 3243
 3244
 3245
 3246
 3247
 3248
 3249
 3250
 3251
 3252
 3253
 3254
 3255
 3256
 3257
 3258
 3259
 3260
 3261
 3262
 3263
 3264
 3265
 3266
 3267
 3268
 3269
 3270
 3271
 3272
 3273
 3274
 3275
 3276
 3277
 3278
 3279
 3280
 3281
 3282
 3283
 3284
 3285
 3286
 3287
 3288
 3289
 3290
 3291
 3292
 3293
 3294
 3295
 3296
 3297
 3298
 3299
 3300
 3301
 3302
 3303
 3304
 3305
 3306
 3307
 3308
 3309
 3310
 3311
 3312
 3313
 3314
 3315
 3316
 3317
 3318
 3319
 3320
 3321
 3322
 3323
 3324
 3325
 3326
 3327
 3328
 3329
 3330
 3331
 3332
 3333
 3334
 3335
 3336
 3337
 3338
 3339
 3340
 3341
 3342
 3343
 3344
 3345
 3346
 3347
 3348
 3349
 3350
 3351
 3352
 3353
 3354
 3355
 3356
 3357
 3358
 3359
 3360
 3361
 3362
 3363
 3364
 3365
 3366
 3367
 3368
 3369
 3370
 3371
 3372
 3373
 3374
 3375
 3376
 3377
 3378
 3379
 3380
 3381
 3382
 3383
 3384
 3385
 3386
 3387
 3388
 3389
 3390
 3391
 3392
 3393
 3394
 3395
 3396
 3397
 3398
 3399
 3400
 3401
 3402
 3403
 3404
 3405
 3406
 3407
 3408
 3409
 3410
 3411
 3412
 3413
 3414
 3415
 3416
 3417
 3418
 3419
 3420
 3421
 3422
 3423
 3424
 3425
 3426
 3427
 3428
 3429
 3430
 3431
 3432
 3433
 3434
 3435
 3436
 3437
 3438
 3439
 3440
 3441
 3442
 3443
 3444
 3445
 3446
 3447
 3448
 3449
 3450
 3451
 3452
 3453
 3454
 3455
 3456
 3457
 3458
 3459
 3460
 3461
 3462
 3463
 3464
 3465
 3466
 3467
 3468
 3469
 3470
 3471
 3472
 3473
 3474
 3475
 3476
 3477
 3478
 3479
 3480
 3481
 3482
 3483
 3484
 3485
 3486
 3487
 3488
 3489
 3490
 3491
 3492
 3493
 3494
 3495
 3496
 3497
 3498
 3499
 3500
 3501
 3502
 3503
 3504
 3505
 3506
 3507
 3508
 3509
 3510
 3511
 3512
 3513
 3514
 3515
 3516
 3517
 3518
 3519
 3520
 3521
 3522
 3523
 3524
 3525
 3526
 3527
 3528
 3529
 3530
 3531
 3532
 3533
 3534
 3535
 3536
 3537
 3538
 3539
 3540
 3541
 3542
 3543
 3544
 3545
 3546
 3547
 3548
 3549
 3550
 3551
 3552
 3553
 3554
 3555
 3556
 3557
 3558
 3559
 3560
 3561
 3562
 3563
 3564
 3565
 3566
 3567
 3568
 3569
 3570
 3571
 3572
 3573
 3574
 3575
 3576
 3577
 3578
 3579
 3580
 3581
 3582
 3583
 3584
 3585
 3586
 3587
 3588
 3589
 3590
 3591
 3592
 3593
 3594
 3595
 3596
 3597
 3598
 3599
 3600
 3601
 3602
 3603
 3604
 3605
 3606
 3607
 3608
 3609
 3610
 3611
 3612
 3613
 3614
 3615
 3616
 3617
 3618
 3619
 3620
 3621
 3622
 3623
 3624
 3625
 3626
 3627
 3628
 3629
 3630
 3631
 3632
 3633
 3634
 3635
 3636
 3637
 3638
 3639
 3640
 3641
 3642
 3643
 3644
 3645
 3646
 3647
 3648
 3649
 3650
 3651
 3652
 3653
 3654
 3655
 3656
 3657
 3658
 3659
 3660
 3661
 3662
 3663
 3664
 3665
 3666
 3667
 3668
 3669
 3670
 3671
 3672
 3673
 3674
 3675
 3676
 3677
 3678
 3679
 3680
 3681
 3682
 3683
 3684
 3685
 3686
 3687
 3688
 3689
 3690
 3691
 3692
 3693
 3694
 3695
 3696
 3697
 3698
 3699
 3700
 3701
 3702
 3703
 3704
 3705
 3706
 3707
 3708
 3709
 3710
 3711
 3712
 3713
 3714
 3715
 3716
 3717
 3718
 3719
 3720
 3721
 3722
 3723
 3724
 3725
 3726
 3727
 3728
 3729
 3730
 3731
 3732
 3733
 3734
 3735
 3736
 3737
 3738
 3739
 3740
 3741
 3742
 3743
 3744
 3745
 3746
 3747
 3748
 3749
 3750
 3751
 3752
 3753
 3754
 3755
 3756
 3757
 3758
 3759
 3760
 3761
 3762
 3763
 3764
 3765
 3766
 3767
 3768
 3769
 3770
 3771
 3772
 3773
 3774
 3775
 3776
 3777
 3778
 3779
 3780
 3781
 3782
 3783
 3784
 3785
 3786
 3787
 3788
 3789
 3790
 3791
 3792
 3793
 3794
 3795
 3796
 3797
 3798
 3799
 3800
 3801
 3802
 3803
 3804
 3805
 3806
 3807
 3808
 3809
 3810
 3811
 3812
 3813
 3814
 3815
 3816
 3817
 3818
 3819
 3820
 3821
 3822
 3823
 3824
 3825
 3826
 3827
 3828
 3829
 3830
 3831
 3832
 3833
 3834
 3835
 3836
 3837
 3838
 3839
 3840
 3841
 3842
 3843
 3844
 3845
 3846
 3847
 3848
 3849
 3850
 3851
 3852
 3853
 3854
 3855
 3856
 3857
 3858
 3859
 3860
 3861
 3862
 3863
 3864
 3865
 3866
 3867
 3868
 3869
 3870
 3871
 3872
 3873
 3874
 3875
 3876
 3877
 3878
 3879
 3880
 3881
 3882
 3883
 3884
 3885
 3886
 3887
 3888
 3889
 3890
 3891
 3892
 3893
 3894
 3895
 3896
 3897
 3898
 3899
 3900
 3901
 3902
 3903
 3904
 3905
 3906
 3907
 3908
 3909
 3910
 3911
 3912
 3913
 3914
 3915
 3916
 3917
 3918
 3919
 3920
 3921
 3922
 3923
 3924
 3925
 3926
 3927
 3928
 3929
 3930
 3931
 3932
 3933
 3934
 3935
 3936
 3937
 3938
 3939
 3940
 3941
 3942
 3943
 3944
 3945
 3946
 3947
 3948
 3949
 3950
 3951
 3952
 3953
 3954
 3955
 3956
 3957
 3958
 3959
 3960
 3961
 3962
 3963
 3964
 3965
 3966
 3967
 3968
 3969
 3970
 3971
 3972
 3973
 3974
 3975
 3976
 3977
 3978
 3979
 3980
 3981
 3982
 3983
 3984
 3985
 3986
 3987
 3988
 3989
 3990
 3991
 3992
 3993
 3994
 3995
 3996
 3997
 3998
 3999
 4000
 4001
 4002
 4003
 4004
 4005
 4006
 4007
 4008
 4009
 4010
 4011
 4012
 4013
 4014
 4015
 4016
 4017
 4018
 4019
 4020
 4021
 4022
 4023
 4024
 4025
 4026
 4027
 4028
 4029
 4030
 4031
 4032
 4033
 4034
 4035
 4036
 4037
 4038
 4039
 4040
 4041
 4042
 4043
 4044
 4045
 4046
 4047
 4048
 4049
 4050
 4051
 4052
 4053
 4054
 4055
 4056
 4057
 4058
 4059
 4060
 4061
 4062
 4063
 4064
 4065
 4066
 4067
 4068
 4069
 4070
 4071
 4072
 4073
 4074
 4075
 4076
 4077
 4078
 4079
 4080
 4081
 4082
 4083
 4084
 4085
 4086
 4087
 4088
 4089
 4090
 4091
 4092
 4093
 4094
 4095
 4096
 4097
 4098
 4099
 4100
 4101
 4102
 4103
 4104
 4105
 4106
 4107
 4108
 4109
 4110
 4111
 4112
 4113
 4114
 4115
 4116
 4117
 4118
 4119
 4120
 4121
 4122
 4123
 4124
 4125
 4126
 4127
 4128
 4129
 4130
 4131
 4132
 4133
 4134
 4135
 4136
 4137
 4138
 4139
 4140
 4141
 4142
 4143
 4144
 4145
 4146
 4147
 4148
 4149
 4150
 4151
 4152
 4153
 4154
 4155
 4156
 4157
 4158
 4159
 4160
 4161
 4162
 4163
 4164
 4165
 4166
 4167
 4168
 4169
 4170
 4171
 4172
 4173
 4174
 4175
 4176
 4177
 4178
 4179
 4180
 4181
 4182
 4183
 4184
 4185
 4186
 4187
 4188
 4189
 4190
 4191
 4192
 4193
 4194
 4195
 4196
 4197
 4198
 4199
 4200
 4201
 4202
 4203
 4204
 4205
 4206
 4207
 4208
 4209
 4210
 4211
 4212
 4213
 4214
 4215
 4216
 4217
 4218
 4219
 4220
 4221
 4222
 4223
 4224
 4225
 4226
 4227
 4228
 4229
 4230
 4231
 4232
 4233
 4234
 4235
 4236
 4237
 4238
 4239
 4240
 4241
 4242
 4243
 4244
 4245
 4246
 4247
 4248
 4249
 4250
 4251
 4252
 4253
 4254
 4255
 4256
 4257
 4258
 4259
 4260
 4261
 4262
 4263
 4264
 4265
 4266
 4267
 4268
 4269
 4270
 4271
 4272
 4273
 4274
 4275
 4276
 4277
 4278
 4279
 4280
 4281
 4282
 4283
 4284
 4285
 4286
 4287
 4288
 4289
 4290
 4291
 4292
 4293
 4294
 4295
 4296
 4297
 4298
 4299
 4300
 4301
 4302
 4303
 4304
 4305
 4306
 4307
 4308
 4309
 4310
 4311
 4312
 4313
 4314
 4315
 4316
 4317
 4318
 4319
 4320
 4321
 4322
 4323
 4324
 4325
 4326
 4327
 4328
 4329
 4330
 4331
 4332
 4333
 4334
 4335
 4336
 4337
 4338
 4339
 4340
 4341
 4342
 4343
 4344
 4345
 4346
 4347
 4348
 4349
 4350
 4351
 4352
 4353
 4354
 4355
 4356
 4357
 4358
 4359
 4360
 4361
 4362
 4363
 4364
 4365
 4366
 4367
 4368
 4369
 4370
 4371
 4372
 4373
 4374
 4375
 4376
 4377
 4378
 4379
 4380
 4381
 4382
 4383
 4384
 4385
 4386
 4387
 4388
 4389
 4390
 4391
 4392
 4393
 4394
 4395
 4396
 4397
 4398
 4399
 4400
 4401
 4402
 4403
 4404
 4405
 4406
 4407
 4408
 4409
 4410
 4411
 4412
 4413
 4414
 4415
 4416
 4417
 4418
 4419
 4420
 4421
 4422
 4423
 4424
 4425
 4426
 4427
 4428
 4429
 4430
 4431
 4432
 4433
 4434
 4435
 4436
 4437
 4438
 4439
 4440
 4441
 4442
 4443
 4444
 4445
 4446
 4447
 4448
 4449
 4450
 4451
 4452
 4453
 4454
 4455
 4456
 4457
 4458
 4459
 4460
 4461
 4462
 4463
 4464
 4465
 4466
 4467
 4468
 4469
 4470
 4471
 4472
 4473
 4474
 4475
 4476
 4477
 4478
 4479
 4480
 4481
 4482
 4483
 4484
 4485
 4486
 4487
 4488
 4489
 4490
 4491
 4492
 4493
 4494
 4495
 4496
 4497
 4498
 4499
 4500
 4501
 4502
 4503
 4504
 4505
 4506
 4507
 4508
 4509
 4510
 4511
 4512
 4513
 4514
 4515
 4516
 4517
 4518
 4519
 4520
 4521
 4522
 4523
 4524
 4525
 4526
 4527
 4528
 4529
 4530
 4531
 4532
 4533
 4534
 4535
 4536
 4537
 4538
 4539
 4540
 4541
 4542
 4543
 4544
 4545
 4546
 4547
 4548
 4549
 4550
 4551
 4552
 4553
 4554
 4555
 4556
 4557
 4558
 4559
 4560
 4561
 4562
 4563
 4564
 4565
 4566
 4567
 4568
 4569
 4570
 4571
 4572
 4573
 4574
 4575
 4576
 4577
 4578
 4579
 4580
 4581
 4582
 4583
 4584
 4585
 4586
 4587
 4588
 4589
 4590
 4591
 4592
 4593
 4594
 4595
 4596
 4597
 4598
 4599
 4600
 4601
 4602
 4603
 4604
 4605
 4606
 4607
 4608
 4609
 4610
 4611
 4612
 4613
 4614
 4615
 4616
 4617
 4618
 4619
 4620
 4621
 4622
 4623
 4624
 4625
 4626
 4627
 4628
 4629
 4630
 4631
 4632
 4633
 4634
 4635
 4636
 4637
 4638
 4639
 4640
 4641
 4642
 4643
 4644
 4645
 4646
 4647
 4648
 4649
 4650
 4651
 4652
 4653
 4654
 4655
 4656
 4657
 4658
 4659
 4660
 4661
 4662
 4663
 4664
 4665
 4666
 4667
 4668
 4669
 4670
 4671
 4672
 4673
 4674
 4675
 4676
 4677
 4678
 4679
 4680
 4681
 4682
 4683
 4684
 4685
 4686
 4687
 4688
 4689
 4690
 4691
 4692
 4693
 4694
 4695
 4696
 4697
 4698
 4699
 4700
 4701
 4702
 4703
 4704
 4705
 4706
 4707
 4708
 4709
 4710
 4711
 4712
 4713
 4714
 4715
 4716
 4717
 4718
 4719
 4720
 4721
 4722
 4723
 4724
 4725
 4726
 4727
 4728
 4729
 4730
 4731
 4732
 4733
 4734
 4735
 4736
 4737
 4738
 4739
 4740
 4741
 4742
 4743
 4744
 4745
 4746
 4747
 4748
 4749
 4750
 4751
 4752
 4753
 4754
 4755
 4756
 4757
 4758
 4759
 4760
 4761
 4762
 4763
 4764
 4765
 4766
 4767
 4768
 4769
 4770
 4771
 4772
 4773
 4774
 4775
 4776
 4777
 4778
 4779
 4780
 4781
 4782
 4783
 4784
 4785
 4786
 4787
 4788
 4789
 4790
 4791
 4792
 4793
 4794
 4795
 4796
 4797
 4798
 4799
 4800
 4801
 4802
 4803
 4804
 4805
 4806
 4807
 4808
 4809
 4810
 4811
 4812
 4813
 4814
 4815
 4816
 4817
 4818
 4819
 4820
 4821
 4822
 4823
 4824
 4825
 4826
 4827
 4828
 4829
 4830
 4831
 4832
 4833
 4834
 4835
 4836
 4837
 4838
 4839
 4840
 4841
 4842
 4843
 4844
 4845
 4846
 4847
 4848
 4849
 4850
 4851
 4852
 4853
 4854
 4855
 4856
 4857
 4858
 4859
 4860
 4861
 4862
 4863
 4864
 4865
 4866
 4867
 4868
 4869
 4870
 4871
 4872
 4873
 4874
 4875
 4876
 4877
 4878
 4879
 4880
 4881
 4882
 4883
 4884
 4885
 4886
 4887
 4888
 4889
 4890
 4891
 4892
 4893
 4894
 4895
 4896
 4897
 4898
 4899
 4900
 4901
 4902
 4903
 4904
 4905
 4906
 4907
 4908
 4909
 4910
 4911
 4912
 4913
 4914
 4915
 4916
 4917
 4918
 4919
 4920
 4921
 4922
 4923
 4924
 4925
 4926
 4927
 4928
 4929
 4930
 4931
 4932
 4933
 4934
 4935
 4936
 4937
 4938
 4939
 4940
 4941
 4942
 4943
 4944
 4945
 4946
 4947
 4948
 4949
 4950
 4951
 4952
 4953
 4954
 4955
 4956
 4957
 4958
 4959
 4960
 4961
 4962
 4963
 4964
 4965
 4966
 4967
 4968
 4969
 4970
 4971
 4972
 4973
 4974
 4975
 4976
 4977
 4978
 4979
 4980
 4981
 4982
 4983
 4984
 4985
 4986
 4987
 4988
 4989
 4990
 4991
 4992
 4993
 4994
 4995
 4996
 4997
 4998
 4999
 5000
 5001
 5002
 5003
 5004
 5005
 5006
 5007
 5008
 5009
 5010
 5011
 5012
 5013
 5014
 5015
 5016
 5017
 5018
 5019
 5020
 5021
 5022
 5023
 5024
 5025
 5026
 5027
 5028
 5029
 5030
 5031
 5032
 5033
 5034
 5035
 5036
 5037
 5038
 5039
 5040
 5041
 5042
 5043
 5044
 5045
 5046
 5047
 5048
 5049
 5050
 5051
 5052
 5053
 5054
 5055
 5056
 5057
 5058
 5059
 5060
 5061
 5062
 5063
 5064
 5065
 5066
 5067
 5068
 5069
 5070
 5071
 5072
 5073
 5074
 5075
 5076
 5077
 5078
 5079
 5080
 5081
 5082
 5083
 5084
 5085
 5086
 5087
 5088
 5089
 5090
 5091
 5092
 5093
 5094
 5095
 5096
 5097
 5098
 5099
 5100
 5101
 5102
 5103
 5104
 5105
 5106
 5107
 5108
 5109
 5110
 5111
 5112
 5113
 5114
 5115
 5116
 5117
 5118
 5119
 5120
 5121
 5122
 5123
 5124
 5125
 5126
 5127
 5128
 5129
 5130
 5131
 5132
 5133
 5134
 5135
 5136
 5137
 5138
 5139
 5140
 5141
 5142
 5143
 5144
 5145
 5146
 5147
 5148
 5149
 5150
 5151
 5152
 5153
 5154
 5155
 5156
 5157
 5158
 5159
 5160
 5161
 5162
 5163
 5164
 5165
 5166
 5167
 5168
 5169
 5170
 5171
 5172
 5173
 5174
 5175
 5176
 5177
 5178
 5179
 5180
 5181
 5182
 5183
 5184
 5185
 5186
 5187
 5188
 5189
 5190
 5191
 5192
 5193
 5194
 5195
 5196
 5197
 5198
 5199
 5200
 5201
 5202
 5203
 5204
 5205
 5206
 5207
 5208
 5209
 5210
 5211
 5212
 5213
 5214
 5215
 5216
 5217
 5218
 5219
 5220
 5221
 5222
 5223
 5224
 5225
 5226
 5227
 5228
 5229
 5230
 5231
 5232
 5233
 5234
 5235
 5236
 5237
 5238
 5239
 5240
 5241
 5242
 5243
 5244
 5245
 5246
 5247
 5248
 5249
 5250
 5251
 5252
 5253
 5254
 5255
 5256
 5257
 5258
 5259
 5260
 5261
 5262
 5263
 5264
 5265
 5266
 5267
 5268
 5269
 5270
 5271
 5272
 5273
 5274
 5275
 5276
 5277
 5278
 5279
 5280
 5281
 5282
 5283
 5284
 5285
 5286
 5287
 5288
 5289
 5290
 5291
 5292
 5293
 5294
 5295
 5296
 5297
 5298
 5299
 5300
 5301
 5302
 5303
 5304
 5305
 5306
 5307
 5308
 5309
 5310
 5311
 5312
 5313
 5314
 5315
 5316
 5317
 5318
 5319
 5320
 5321
 5322
 5323
 5324
 5325
 5326
 5327
 5328
 5329
 5330
 5331
 5332
 5333
 5334
 5335
 5336
 5337
 5338
 5339
 5340
 5341
 5342
 5343
 5344
 5345
 5346
 5347
 5348
 5349
 5350
 5351
 5352
 5353
 5354
 5355
 5356
 5357
 5358
 5359
 5360
 5361
 5362
 5363
 5364
 5365
 5366
 5367
 5368
 5369
 5370
 5371
 5372
 5373
 5374
 5375
 5376
 5377
 5378
 5379
 5380
 5381
 5382
 5383
 5384
 5385
 5386
 5387
 5388
 5389
 5390
 5391
 5392
 5393
 5394
 5395
 5396
 5397
 5398
 5399
 5400
 5401
 5402
 5403
 5404
 5405
 5406
 5407
 5408
 5409
 5410
 5411
 5412
 5413
 5414
 5415
 5416
 5417
 5418
 5419
 5420
 5421
 5422
 5423
 5424
 5425
 5426
 5427
 5428
 5429
 5430
 5431
 5432
 5433
 5434
 5435
 5436
 5437
 5438
 5439
 5440
 5441
 5442
 5443
 5444
 5445
 5446
 5447
 5448
 5449
 5450
 5451
 5452
 5453
 5454
 5455
 5456
 5457
 5458
 5459
 5460
 5461
 5462
 5463
 5464
 5465
 5466
 5467
 5468
 5469
 5470
 5471
 5472
 5473
 5474
 5475
 5476
 5477
 5478
 5479
 5480
 5481
 5482
 5483
 5484
 5485
 5486
 5487
 5488
 5489
 5490
 5491
 5492
 5493
 5494
 5495
 5496
 5497
 5498
 5499
 5500
 5501
 5502
 5503
 5504
 5505
 5506
 5507
 5508
 5509
 5510
 5511
 5512
 5513
 5514
 5515
 5516
 5517
 5518
 5519
 5520
 5521
 5522
 5523
 5524
 5525
 5526
 5527
 5528
 5529
 5530
 5531
 5532
 5533
 5534
 5535
 5536
 5537
 5538
 5539
 5540
 5541
 5542
 5543
 5544
 5545
 5546
 5547
 5548
 5549
 5550
 5551
 5552
 5553
 5554
 5555
 5556
 5557
 5558
 5559
 5560
 5561
 5562
 5563
 5564
 5565
 5566
 5567
 5568
 5569
 5570
 5571
 5572
 5573
 5574
 5575
 5576
 5577
 5578
 5579
 5580
 5581
 5582
 5583
 5584
 5585
 5586
 5587
 5588
 5589
 5590
 5591
 5592
 5593
 5594
 5595
 5596
 5597
 5598
 5599
 5600
 5601
 5602
 5603
 5604
 5605
 5606
 5607
 5608
 5609
 5610
 5611
 5612
 5613
 5614
 5615
 5616
 5617
 5618
 5619
 5620
 5621
 5622
 5623
 5624
 5625
 5626
 5627
 5628
 5629
 5630
 5631
 5632
 5633
 5634
 5635
 5636
 5637
 5638
 5639
 5640
 5641
 5642
 5643
 5644
 5645
 5646
 5647
 5648
 5649
 5650
 5651
 5652
 5653
 5654
 5655
 5656
 5657
 5658
 5659
 5660
 5661
 5662
 5663
 5664
 5665
 5666
 5667
 5668
 5669
 5670
 5671
 5672
 5673
 5674
 5675
 5676
 5677
 5678
 5679
 5680
 5681
 5682
 5683
 5684
 5685
 5686
 5687
 5688
 5689
 5690
 5691
 5692
 5693
 5694
 5695
 5696
 5697
 5698
 5699
 5700
 5701
 5702
 5703
 5704
 5705
 5706
 5707
 5708
 5709
 5710
 5711
 5712
 5713
 5714
 5715
 5716
 5717
 5718
 5719
 5720
 5721
 5722
 5723
 5724
 5725
 5726
 5727
 5728
 5729
 5730
 5731
 5732
 5733
 5734
 5735
 5736
 5737
 5738
 5739
 5740
 5741
 5742
 5743
 5744
 5745
 5746
 5747
 5748
 5749
 5750
 5751
 5752
 5753
 5754
 5755
 5756
 5757
 5758
 5759
 5760
 5761
 5762
 5763
 5764
 5765
 5766
 5767
 5768
 5769
 5770
 5771
 5772
 5773
 5774
 5775
 5776
 5777
 5778
 5779
 5780
 5781
 5782
 5783
 5784
 5785
 5786
 5787
 5788
 5789
 5790
 5791
 5792
 5793
 5794
 5795
 5796
 5797
 5798
 5799
 5800
 5801
 5802
 5803
 5804
 5805
 5806
 5807
 5808
 5809
 5810
 5811
 5812
 5813
 5814
 5815
 5816
 5817
 5818
 5819
 5820
 5821
 5822
 5823
 5824
 5825
 5826
 5827
 5828
 5829
 5830
 5831
 5832
 5833
 5834
 5835
 5836
 5837
 5838
 5839
 5840
 5841
 5842
 5843
 5844
 5845
 5846
 5847
 5848
 5849
 5850
 5851
 5852
 5853
 5854
 5855
 5856
 5857
 5858
 5859
 5860
 5861
 5862
 5863
 5864
 5865
 5866
 5867
 5868
 5869
 5870
 5871
 5872
 5873
 5874
 5875
 5876
 5877
 5878
 5879
 5880
 5881
 5882
 5883
 5884
 5885
 5886
 5887
 5888
 5889
 5890
 5891
 5892
 5893
 5894
 5895
 5896
 5897
 5898
 5899
 5900
 5901
 5902
 5903
 5904
 5905
 5906
 5907
 5908
 5909
 5910
 5911
 5912
 5913
 5914
 5915
 5916
 5917
 5918
 5919
 5920
 5921
 5922
 5923
 5924
 5925
 5926
 5927
 5928
 5929
 5930
 5931
 5932
 5933
 5934
 5935
 5936
 5937
 5938
 5939
 5940
 5941
 5942
 5943
 5944
 5945
 5946
 5947
 5948
 5949
 5950
 5951
 5952
 5953
 5954
 5955
 5956
 5957
 5958
 5959
 5960
 5961
 5962
 5963
 5964
 5965
 5966
 5967
 5968
 5969
 5970
 5971
 5972
 5973
 5974
 5975
 5976
 5977
 5978
 5979
 5980
 5981
 5982
 5983
 5984
 5985
 5986
 5987
 5988
 5989
 5990
 5991
 5992
 5993
 5994
 5995
 5996
 5997
 5998
 5999
 6000
 6001
 6002
 6003
 6004
 6005
 6006
 6007
 6008
 6009
 6010
 6011
 6012
 6013
 6014
 6015
 6016
 6017
 6018
 6019
 6020
 6021
 6022
 6023
 6024
 6025
 6026
 6027
 6028
 6029
 6030
 6031
 6032
 6033
 6034
 6035
 6036
 6037
 6038
 6039
 6040
 6041
 6042
 6043
 6044
 6045
 6046
 6047
 6048
 6049
 6050
 6051
 6052
 6053
 6054
 6055
 6056
 6057
 6058
 6059
 6060
 6061
 6062
 6063
 6064
 6065
 6066
 6067
 6068
 6069
 6070
 6071
 6072
 6073
 6074
 6075
 6076
 6077
 6078
 6079
 6080
 6081
 6082
 6083
 6084
 6085
 6086
 6087
 6088
 6089
 6090
 6091
 6092
 6093
 6094
 6095
 6096
 6097
 6098
 6099
 6100
 6101
 6102
 6103
 6104
 6105
 6106
 6107
 6108
 6109
 6110
 6111
 6112
 6113
 6114
 6115
 6116
 6117
 6118
 6119
 6120
 6121
 6122
 6123
 6124
 6125
 6126
 6127
 6128
 6129
 6130
 6131
 6132
 6133
 6134
 6135
 6136
 6137
 6138
 6139
 6140
 6141
 6142
 6143
 6144
 6145
 6146
 6147
 6148
 6149
 6150
 6151
 6152
 6153
 6154
 6155
 6156
 6157
 6158
 6159
 6160
 6161
 6162
 6163
 6164
 6165
 6166
 6167
 6168
 6169
 6170
 6171
 6172
 6173
 6174
 6175
 6176
 6177
 6178
 6179
 6180
 6181
 6182
 6183
 6184
 6185
 6186
 6187
 6188
 6189
 6190
 6191
 6192
 6193
 6194
 6195
 6196
 6197
 6198
 6199
 6200
 6201
 6202
 6203
 6204
 6205
 6206
 6207
 6208
 6209
 6210
 6211
 6212
 6213
 6214
 6215
 6216
 6217
 6218
 6219
 6220
 6221
 6222
 6223
 6224
 6225
 6226
 6227
 6228
 6229
 6230
 6231
 6232
 6233
 6234
 6235
 6236
 6237
 6238
 6239
 6240
 6241
 6242
 6243
 6244
 6245
 6246
 6247
 6248
 6249
 6250
 6251
 6252
 6253
 6254
 6255
 6256
 6257
 6258
 6259
 6260
 6261
 6262
 6263
 6264
 6265
 6266
 6267
 6268
 6269
 6270
 6271
 6272
 6273
 6274
 6275
 6276
 6277
 6278
 6279
 6280
 6281
 6282
 6283
 6284
 6285
 6286
 6287
 6288
 6289
 6290
 6291
 6292
 6293
 6294
 6295
 6296
 6297
 6298
 6299
 6300
 6301
 6302
 6303
 6304
 6305
 6306
 6307
 6308
 6309
 6310
 6311
 6312
 6313
 6314
 6315
 6316
 6317
 6318
 6319
 6320
 6321
 6322
 6323
 6324
 6325
 6326
 6327
 6328
 6329
 6330
 6331
 6332
 6333
 6334
 6335
 6336
 6337
 6338
 6339
 6340
 6341
 6342
 6343
 6344
 6345
 6346
 6347
 6348
 6349
 6350
 6351
 6352
 6353
 6354
 6355
 6356
 6357
 6358
 6359
 6360
 6361
 6362
 6363
 6364
 6365
 6366
 6367
 6368
 6369
 6370
 6371
 6372
 6373
 6374
 6375
 6376
 6377
 6378
 6379
 6380
 6381
 6382
 6383
 6384
 6385
 6386
 6387
 6388
 6389
 6390
 6391
 6392
 6393
 6394
 6395
 6396
 6397
 6398
 6399
 6400
 6401
 6402
 6403
 6404
 6405
 6406
 6407
 6408
 6409
 6410
 6411
 6412
 6413
 6414
 6415
 6416
 6417
 6418
 6419
 6420
 6421
 6422
 6423
 6424
 6425
 6426
 6427
 6428
 6429
 6430
 6431
 6432
 6433
 6434
 6435
 6436
 6437
 6438
 6439
 6440
 6441
 6442
 6443
 6444
 6445
 6446
 6447
 6448
 6449
 6450
 6451
 6452
 6453
 6454
 6455
 6456
 6457
 6458
 6459
 6460
 6461
 6462
 6463
 6464
 6465
 6466
 6467
 6468
 6469
 6470
 6471
 6472
 6473
 6474
 6475
 6476
 6477
 6478
 6479
 6480
 6481
 6482
 6483
 6484
 6485
 6486
 6487
 6488
 6489
 6490
 6491
 6492
 6493
 6494
 6495
 6496
 6497
 6498
 6499
 6500
 6501
 6502
 6503
 6504
 6505
 6506
 6507
 6508
 6509
 6510
 6511
 6512
 6513
 6514
 6515
 6516
 6517
 6518
 6519
 6520
 6521
 6522
 6523
 6524
 6525
 6526
 6527
 6528
 6529
 6530
 6531
 6532
 6533
 6534
 6535
 6536
 6537
 6538
 6539
 6540
 6541
 6542
 6543
 6544
 6545
 6546
 6547
 6548
 6549
 6550
 6551
 6552
 6553
 6554
 6555
 6556
 6557
 6558
 6559
 6560
 6561
 6562
 6563
 6564
 6565
 6566
 6567
 6568
 6569
 6570
 6571
 6572
 6573
 6574
 6575
 6576
 6577
 6578
 6579
 6580
 6581
 6582
 6583
 6584
 6585
 6586
 6587
 6588
 6589
 6590
 6591
 6592
 6593
 6594
 6595
 6596
 6597
 6598
 6599
 6600
 6601
 6602
 6603
 6604
 6605
 6606
 6607
 6608
 6609
 6610
 6611
 6612
 6613
 6614
 6615
 6616
 6617
 6618
 6619
 6620
 6621
 6622
 6623
 6624
 6625
 6626
 6627
 6628
 6629
 6630
 6631
 6632
 6633
 6634
 6635
 6636
 6637
 6638
 6639
 6640
 6641
 6642
 6643
 6644
 6645
 6646
 6647
 6648
 6649
 6650
 6651
 6652
 6653
 6654
 6655
 6656
 6657
 6658
 6659
 6660
 6661
 6662
 6663
 6664
 6665
 6666
 6667
 6668
 6669
 6670
 6671
 6672
 6673
 6674
 6675
 6676
 6677
 6678
 6679
 6680
 6681
 6682
 6683
 6684
 6685
 6686
 6687
 6688
 6689
 6690
 6691
 6692
 6693
 6694
 6695
 6696
 6697
 6698
 6699
 6700
 6701
 6702
 6703
 6704
 6705
 6706
 6707
 6708
 6709
 6710
 6711
 6712
 6713
 6714
 6715
 6716
 6717
 6718
 6719
 6720
 6721
 6722
 6723
 6724
 6725
 6726
 6727
 6728
 6729
 6730
 6731
 6732
 6733
 6734
 6735
 6736
 6737
 6738
 6739
 6740
 6741
 6742
 6743
 6744
 6745
 6746
 6747
 6748
 6749
 6750
 6751
 6752
 6753
 6754
 6755
 6756
 6757
 6758
 6759
 6760
 6761
 6762
 6763
 6764
 6765
 6766
 6767
 6768
 6769
 6770
 6771
 6772
 6773
 6774
 6775
 6776
 6777
 6778
 6779
 6780
 6781
 6782
 6783
 6784
 6785
 6786
 6787
 6788
 6789
 6790
 6791
 6792
 6793
 6794
 6795
 6796
 6797
 6798
 6799
 6800
 6801
 6802
 6803
 6804
 6805
 6806
 6807
 6808
 6809
 6810
 6811
 6812
 6813
 6814
 6815
 6816
 6817
 6818
 6819
 6820
 6821
 6822
 6823
 6824
 6825
 6826
 6827
 6828
 6829
 6830
 6831
 6832
 6833
 6834
 6835
 6836
 6837
 6838
 6839
 6840
 6841
 6842
 6843
 6844
 6845
 6846
 6847
 6848
 6849
 6850
 6851
 6852
 6853
 6854
 6855
 6856
 6857
 6858
 6859
 6860
 6861
 6862
 6863
 6864
 6865
 6866
 6867
 6868
 6869
 6870
 6871
 6872
 6873
 6874
 6875
 6876
 6877
 6878
 6879
 6880
 6881
 6882
 6883
 6884
 6885
 6886
 6887
 6888
 6889
 6890
 6891
 6892
 6893
 6894
 6895
 6896
 6897
 6898
 6899
 6900
 6901
 6902
 6903
 6904
 6905
 6906
 6907
 6908
 6909
 6910
 6911
 6912
 6913
 6914
 6915
 6916
 6917
 6918
 6919
 6920
 6921
 6922
 6923
 6924
 6925
 6926
 6927
 6928
 6929
 6930
 6931
 6932
 6933
 6934
 6935
 6936
 6937
 6938
 6939
 6940
 6941
 6942
 6943
 6944
 6945
 6946
 6947
 6948
 6949
 6950
 6951
 6952
 6953
 6954
 6955
 6956
 6957
 6958
 6959
 6960
 6961
 6962
 6963
 6964
 6965
 6966
 6967
 6968
 6969
 6970
 6971
 6972
 6973
 6974
 6975
 6976
 6977
 6978
 6979
 6980
 6981
 6982
 6983
 6984
 6985
 6986
 6987
 6988
 6989
 6990
 6991
 6992
 6993
 6994
 6995
 6996
 6997
 6998
 6999
 7000
 7001
 7002
 7003
 7004
 7005
 7006
 7007
 7008
 7009
 7010
 7011
 7012
 7013
 7014
 7015
 7016
 7017
 7018
 7019
 7020
 7021
 7022
 7023
 7024
 7025
 7026
 7027
 7028
 7029
 7030
 7031
 7032
 7033
 7034
 7035
 7036
 7037
 7038
 7039
 7040
 7041
 7042
 7043
 7044
 7045
 7046
 7047
 7048
 7049
 7050
 7051
 7052
 7053
 7054
 7055
 7056
 7057
 7058
 7059
 7060
 7061
 7062
 7063
 7064
 7065
 7066
 7067
 7068
 7069
 7070
 7071
 7072
 7073
 7074
 7075
 7076
 7077
 7078
 7079
 7080
 7081
 7082
 7083
 7084
 7085
 7086
 7087
 7088
 7089
 7090
 7091
 7092
 7093
 7094
 7095
 7096
 7097
 7098
 7099
 7100
 7101
 7102
 7103
 7104
 7105
 7106
 7107
 7108
 7109
 7110
 7111
 7112
 7113
 7114
 7115
 7116
 7117
 7118
 7119
 7120
 7121
 7122
 7123
 7124
 7125
 7126
 7127
 7128
 7129
 7130
 7131
 7132
 7133
 7134
 7135
 7136
 7137
 7138
 7139
 7140
 7141
 7142
 7143
 7144
 7145
 7146
 7147
 7148
 7149
 7150
 7151
 7152
 7153
 7154
 7155
 7156
 7157
 7158
 7159
 7160
 7161
 7162
 7163
 7164
 7165
 7166
 7167
 7168
 7169
 7170
 7171
 7172
 7173
 7174
 7175
 7176
 7177
 7178
 7179
 7180
 7181
 7182
 7183
 7184
 7185
 7186
 7187
 7188
 7189
 7190
 7191
 7192
 7193
 7194
 7195
 7196
 7197
 7198
 7199
 7200
 7201
 7202
 7203
 7204
 7205
 7206
 7207
 7208
 7209
 7210
 7211
 7212
 7213
 7214
 7215
 7216
 7217
 7218
 7219
 7220
 7221
 7222
 7223
 7224
 7225
 7226
 7227
 7228
 7229
 7230
 7231
 7232
 7233
 7234
 7235
 7236
 7237
 7238
 7239
 7240
 7241
 7242
 7243
 7244
 7245
 7246
 7247
 7248
 7249
 7250
 7251
 7252
 7253
 7254
 7255
 7256
 7257
 7258
 7259
 7260
 7261
 7262
 7263
 7264
 7265
 7266
 7267
 7268
 7269
 7270
 7271
 7272
 7273
 7274
 7275
 7276
 7277
 7278
 7279
 7280
 7281
 7282
 7283
 7284
 7285
 7286
 7287
 7288
 7289
 7290
 7291
 7292
 7293
 7294
 7295
 7296
 7297
 7298
 7299
 7300
 7301
 7302
 7303
 7304
 7305
 7306
 7307
 7308
 7309
 7310
 7311
 7312
 7313
 7314
 7315
 7316
 7317
 7318
 7319
 7320
 7321
 7322
 7323
 7324
 7325
 7326
 7327
 7328
 7329
 7330
 7331
 7332
 7333
 7334
 7335
 7336
 7337
 7338
 7339
 7340
 7341
 7342
 7343
 7344
 7345
 7346
 7347
 7348
 7349
 7350
 7351
 7352
 7353
 7354
 7355
 7356
 7357
 7358
 7359
 7360
 7361
 7362
 7363
 7364
 7365
 7366
 7367
 7368
 7369
 7370
 7371
 7372
 7373
 7374
 7375
 7376
 7377
 7378
 7379
 7380
 7381
 7382
 7383
 7384
 7385
 7386
 7387
 7388
 7389
 7390
 7391
 7392
 7393
 7394
 7395
 7396
 7397
 7398
 7399
 7400
 7401
 7402
 7403
 7404
 7405
 7406
 7407
 7408
 7409
 7410
 7411
 7412
 7413
 7414
 7415
 7416
 7417
 7418
 7419
 7420
 7421
 7422
 7423
 7424
 7425
 7426
 7427
 7428
 7429
 7430
 7431
 7432
 7433
 7434
 7435
 7436
 7437
 7438
 7439
 7440
 7441
 7442
 7443
 7444
 7445
 7446
 7447
 7448
 7449
 7450
 7451
 7452
 7453
 7454
 7455
 7456
 7457
 7458
 7459
 7460
 7461
 7462
 7463
 7464
 7465
 7466
 7467
 7468
 7469
 7470
 7471
 7472
 7473
 7474
 7475
 7476
 7477
 7478
 7479
 7480
 7481
 7482
 7483
 7484
 7485
 7486
 7487
 7488
 7489
 7490
 7491
 7492
 7493
 7494
 7495
 7496
 7497
 7498
 7499
 7500
 7501
 7502
 7503
 7504
 7505
 7506
 7507
 7508
 7509
 7510
 7511
 7512
 7513
 7514
 7515
 7516
 7517
 7518
 7519
 7520
 7521
 7522
 7523
 7524
 7525
 7526
 7527
 7528
 7529
 7530
 7531
 7532
 7533
 7534
 7535
 7536
 7537
 7538
 7539
 7540
 7541
 7542
 7543
 7544
 7545
 7546
 7547
 7548
 7549
 7550
 7551
 7552
 7553
 7554
 7555
 7556
 7557
 7558
 7559
 7560
 7561
 7562
 7563
 7564
 7565
 7566
 7567
 7568
 7569
 7570
 7571
 7572
 7573
 7574
 7575
 7576
 7577
 7578
 7579
 7580
 7581
 7582
 7583
 7584
 7585
 7586
 7587
 7588
 7589
 7590
 7591
 7592
 7593
 7594
 7595
 7596
 7597
 7598
 7599
 7600
 7601
 7602
 7603
 7604
 7605
 7606
 7607
 7608
 7609
 7610
 7611
 7612
 7613
 7614
 7615
 7616
 7617
 7618
 7619
 7620
 7621
 7622
 7623
 7624
 7625
 7626
 7627
 7628
 7629
 7630
 7631
 7632
 7633
 7634
 7635
 7636
 7637
 7638
 7639
 7640
 7641
 7642
 7643
 7644
 7645
 7646
 7647
 7648
 7649
 7650
 7651
 7652
 7653
 7654
 7655
 7656
 7657
 7658
 7659
 7660
 7661
 7662
 7663
 7664
 7665
 7666
 7667
 7668
 7669
 7670
 7671
 7672
 7673
 7674
 7675
 7676
 7677
 7678
 7679
 7680
 7681
 7682
 7683
 7684
 7685
 7686
 7687
 7688
 7689
 7690
 7691
 7692
 7693
 7694
 7695
 7696
 7697
 7698
 7699
 7700
 7701
 7702
 7703
 7704
 7705
 7706
 7707
 7708
 7709
 7710
 7711
 7712
 7713
 7714
 7715
 7716
 7717
 7718
 7719
 7720
 7721
 7722
 7723
 7724
 7725
 7726
 7727
 7728
 7729
 7730
 7731
 7732
 7733
 7734
 7735
 7736
 7737
 7738
 7739
 7740
 7741
 7742
 7743
 7744
 7745
 7746
 7747
 7748
 7749
 7750
 7751
 7752
 7753
 7754
 7755
 7756
 7757
 7758
 7759
 7760
 7761
 7762
 7763
 7764
 7765
 7766
 7767
 7768
 7769
 7770
 7771
 7772
 7773
 7774
 7775
 7776
 7777
 7778
 7779
 7780
 7781
 7782
 7783
 7784
 7785
 7786
 7787
 7788
 7789
 7790
 7791
 7792
 7793
 7794
 7795
 7796
 7797
 7798
 7799
 7800
 7801
 7802
 7803
 7804
 7805
 7806
 7807
 7808
 7809
 7810
 7811
 7812
 7813
 7814
 7815
 7816
 7817
 7818
 7819
 7820
 7821
 7822
 7823
 7824
 7825
 7826
 7827
 7828
 7829
 7830
 7831
 7832
 7833
 7834
 7835
 7836
 7837
 7838
 7839
 7840
 7841
 7842
 7843
 7844
 7845
 7846
 7847
 7848
 7849
 7850
 7851
 7852
 7853
 7854
 7855
 7856
 7857
 7858
 7859
 7860
 7861
 7862
 7863
 7864
 7865
 7866
 7867
 7868
 7869
 7870
 7871
 7872
 7873
 7874
 7875
 7876
 7877
 7878
 7879
 7880
 7881
 7882
 7883
 7884
 7885
 7886
 7887
 7888
 7889
 7890
 7891
 7892
 7893
 7894
 7895
 7896
 7897
 7898
 7899
 7900
 7901
 7902
 7903
 7904
 7905
 7906
 7907
 7908
 7909
 7910
 7911
 7912
 7913
 7914
 7915
 7916
 7917
 7918
 7919
 7920
 7921
 7922
 7923
 7924
 7925
 7926
 7927
 7928
 7929
 7930
 7931
 7932
 7933
 7934
 7935
 7936
 7937
 7938
 7939
 7940
 7941
 7942
 7943
 7944
 7945
 7946
 7947
 7948
 7949
 7950
 7951
 7952
 7953
 7954
 7955
 7956
 7957
 7958
 7959
 7960
 7961
 7962
 7963
 7964
 7965
 7966
 7967
 7968
 7969
 7970
 7971
 7972
 7973
 7974
 7975
 7976
 7977
 7978
 7979
 7980
 7981
 7982
 7983
 7984
 7985
 7986
 7987
 7988
 7989
 7990
 7991
 7992
 7993
 7994
 7995
 7996
 7997
 7998
 7999
 8000
 8001
 8002
 8003
 8004
 8005
 8006
 8007
 8008
 8009
 8010
 8011
 8012
 8013
 8014
 8015
 8016
 8017
 8018
 8019
 8020
 8021
 8022
 8023
 8024
 8025
 8026
 8027
 8028
 8029
 8030
 8031
 8032
 8033
 8034
 8035
 8036
 8037
 8038
 8039
 8040
 8041
 8042
 8043
 8044
 8045
 8046
 8047
 8048
 8049
 8050
 8051
 8052
 8053
 8054
 8055
 8056
 8057
 8058
 8059
 8060
 8061
 8062
 8063
 8064
 8065
 8066
 8067
 8068
 8069
 8070
 8071
 8072
 8073
 8074
 8075
 8076
 8077
 8078
 8079
 8080
 8081
 8082
 8083
 8084
 8085
 8086
 8087
 8088
 8089
 8090
 8091
 8092
 8093
 8094
 8095
 8096
 8097
 8098
 8099
 8100
 8101
 8102
 8103
 8104
 8105
 8106
 8107
 8108
 8109
 8110
 8111
 8112
 8113
 8114
 8115
 8116
 8117
 8118
 8119
 8120
 8121
 8122
 8123
 8124
 8125
 8126
 8127
 8128
 8129
 8130
 8131
 8132
 8133
 8134
 8135
 8136
 8137
 8138
 8139
 8140
 8141
 8142
 8143
 8144
 8145
 8146
 8147
 8148
 8149
 8150
 8151
 8152
 8153
 8154
 8155
 8156
 8157
 8158
 8159
 8160
 8161
 8162
 8163
 8164
 8165
 8166
 8167
 8168
 8169
 8170
 8171
 8172
 8173
 8174
 8175
 8176
 8177
 8178
 8179
 8180
 8181
 8182
 8183
 8184
 8185
 8186
 8187
 8188
 8189
 8190
 8191
 8192
 8193
 8194
 8195
 8196
 8197
 8198
 8199
 8200
 8201
 8202
 8203
 8204
 8205
 8206
 8207
 8208
 8209
 8210
 8211
 8212
 8213
 8214
 8215
 8216
 8217
 8218
 8219
 8220
 8221
 8222
 8223
 8224
 8225
 8226
 8227
 8228
 8229
 8230
 8231
 8232
 8233
 8234
 8235
 8236
 8237
 8238
 8239
 8240
 8241
 8242
 8243
 8244
 8245
 8246
 8247
 8248
 8249
 8250
 8251
 8252
 8253
 8254
 8255
 8256
 8257
 8258
 8259
 8260
 8261
 8262
 8263
 8264
 8265
 8266
 8267
 8268
 8269
 8270
 8271
 8272
 8273
 8274
 8275
 8276
 8277
 8278
 8279
 8280
 8281
 8282
 8283
 8284
 8285
 8286
 8287
 8288
 8289
 8290
 8291
 8292
 8293
 8294
 8295
 8296
 8297
 8298
 8299
 8300
 8301
 8302
 8303
 8304
 8305
 8306
 8307
 8308
 8309
 8310
 8311
 8312
 8313
 8314
 8315
 8316
 8317
 8318
 8319
 8320
 8321
 8322
 8323
 8324
 8325
 8326
 8327
 8328
 8329
 8330
 8331
 8332
 8333
 8334
 8335
 8336
 8337
 8338
 8339
 8340
 8341
 8342
 8343
 8344
 8345
 8346
 8347
 8348
 8349
 8350
 8351
 8352
 8353
 8354
 8355
 8356
 8357
 8358
 8359
 8360
 8361
 8362
 8363
 8364
 8365
 8366
 8367
 8368
 8369
 8370
 8371
 8372
 8373
 8374
 8375
 8376
 8377
 8378
 8379
 8380
 8381
 8382
 8383
 8384
 8385
 8386
 8387
 8388
 8389
 8390
 8391
 8392
 8393
 8394
 8395
 8396
 8397
 8398
 8399
 8400
 8401
 8402
 8403
 8404
 8405
 8406
 8407
 8408
 8409
 8410
 8411
 8412
 8413
 8414
 8415
 8416
 8417
 8418
 8419
 8420
 8421
 8422
 8423
 8424
 8425
 8426
 8427
 8428
 8429
 8430
 8431
 8432
 8433
 8434
 8435
 8436
 8437
 8438
 8439
 8440
 8441
 8442
 8443
 8444
 8445
 8446
 8447
 8448
 8449
 8450
 8451
 8452
 8453
 8454
 8455
 8456
 8457
 8458
 8459
 8460
 8461
 8462
 8463
 8464
 8465
 8466
 8467
 8468
 8469
 8470
 8471
 8472
 8473
 8474
 8475
 8476
 8477
 8478
 8479
 8480
 8481
 8482
 8483
 8484
 8485
 8486
 8487
 8488
 8489
 8490
 8491
 8492
 8493
 8494
 8495
 8496
 8497
 8498
 8499
 8500
 8501
 8502
 8503
 8504
 8505
 8506
 8507
 8508
 8509
 8510
 8511
 8512
 8513
 8514
 8515
 8516
 8517
 8518
 8519
 8520
 8521
 8522
 8523
 8524
 8525
 8526
 8527
 8528
 8529
 8530
 8531
 8532
 8533
 8534
 8535
 8536
 8537
 8538
 8539
 8540
 8541
 8542
 8543
 8544
 8545
 8546
 8547
 8548
 8549
 8550
 8551
 8552
 8553
 8554
 8555
 8556
 8557
 8558
 8559
 8560
 8561
 8562
 8563
 8564
 8565
 8566
 8567
 8568
 8569
 8570
 8571
 8572
 8573
 8574
 8575
 8576
 8577
 8578
 8579
 8580
 8581
 8582
 8583
 8584
 8585
 8586
 8587
 8588
 8589
 8590
 8591
 8592
 8593
 8594
 8595
 8596
 8597
 8598
 8599
 8600
 8601
 8602
 8603
 8604
 8605
 8606
 8607
 8608
 8609
 8610
 8611
 8612
 8613
 8614
 8615
 8616
 8617
 8618
 8619
 8620
 8621
 8622
 8623
 8624
 8625
 8626
 8627
 8628
 8629
 8630
 8631
 8632
 8633
 8634
 8635
 8636
 8637
 8638
 8639
 8640
 8641
 8642
 8643
 8644
 8645
 8646
 8647
 8648
 8649
 8650
 8651
 8652
 8653
 8654
 8655
 8656
 8657
 8658
 8659
 8660
 8661
 8662
 8663
 8664
 8665
 8666
 8667
 8668
 8669
 8670
 8671
 8672
 8673
 8674
 8675
 8676
 8677
 8678
 8679
 8680
 8681
 8682
 8683
 8684
 8685
 8686
 8687
 8688
 8689
 8690
 8691
 8692
 8693
 8694
 8695
 8696
 8697
 8698
 8699
 8700
 8701
 8702
 8703
 8704
 8705
 8706
 8707
 8708
 8709
 8710
 8711
 8712
 8713
 8714
 8715
 8716
 8717
 8718
 8719
 8720
 8721
 8722
 8723
 8724
 8725
 8726
 8727
 8728
 8729
 8730
 8731
 8732
 8733
 8734
 8735
 8736
 8737
 8738
 8739
 8740
 8741
 8742
 8743
 8744
 8745
 8746
 8747
 8748
 8749
 8750
 8751
 8752
 8753
 8754
 8755
 8756
 8757
 8758
 8759
 8760
 8761
 8762
 8763
 8764
 8765
 8766
 8767
 8768
 8769
 8770
 8771
 8772
 8773
 8774
 8775
 8776
 8777
 8778
 8779
 8780
 8781
 8782
 8783
 8784
 8785
 8786
 8787
 8788
 8789
 8790
 8791
 8792
 8793
 8794
 8795
 8796
 8797
 8798
 8799
 8800
 8801
 8802
 8803
 8804
 8805
 8806
 8807
 8808
 8809
 8810
 8811
 8812
 8813
 8814
 8815
 8816
 8817
 8818
 8819
 8820
 8821
 8822
 8823
 8824
 8825
 8826
 8827
 8828
 8829
 8830
 8831
 8832
 8833
 8834
 8835
 8836
 8837
 8838
 8839
 8840
 8841
 8842
 8843
 8844
 8845
 8846
 8847
 8848
 8849
 8850
 8851
 8852
 8853
 8854
 8855
 8856
 8857
 8858
 8859
 8860
 8861
 8862
 8863
 8864
 8865
 8866
 8867
 8868
 8869
 8870
 8871
 8872
 8873
 8874
 8875
 8876
 8877
 8878
 8879
 8880
 8881
 8882
 8883
 8884
 8885
 8886
 8887
 8888
 8889
 8890
 8891
 8892
 8893
 8894
 8895
 8896
 8897
 8898
 8899
 8900
 8901
 8902
 8903
 8904
 8905
 8906
 8907
 8908
 8909
 8910
 8911
 8912
 8913
 8914
 8915
 8916
 8917
 8918
 8919
 8920
 8921
 8922
 8923
 8924
 8925
 8926
 8927
 8928
 8929
 8930
 8931
 8932
 8933
 8934
 8935
 8936
 8937
 8938
 8939
 8940
 8941
 8942
 8943
 8944
 8945
 8946
 8947
 8948
 8949
 8950
 8951
 8952
 8953
 8954
 8955
 8956
 8957
 8958
 8959
 8960
 8961
 8962
 8963
 8964
 8965
 8966
 8967
 8968
 8969
 8970
 8971
 8972
 8973
 8974
 8975
 8976
 8977
 8978
 8979
 8980
 8981
 8982
 8983
 8984
 8985
 8986
 8987
 8988
 8989
 8990
 8991
 8992
 8993
 8994
 8995
 8996
 8997
 8998
 8999
 9000
 9001
 9002
 9003
 9004
 9005
 9006
 9007
 9008
 9009
 9010
 9011
 9012
 9013
 9014
 9015
 9016
 9017
 9018
 9019
 9020
 9021
 9022
 9023
 9024
 9025
 9026
 9027
 9028
 9029
 9030
 9031
 9032
 9033
 9034
 9035
 9036
 9037
 9038
 9039
 9040
 9041
 9042
 9043
 9044
 9045
 9046
 9047
 9048
 9049
 9050
 9051
 9052
 9053
 9054
 9055
 9056
 9057
 9058
 9059
 9060
 9061
 9062
 9063
 9064
 9065
 9066
 9067
 9068
 9069
 9070
 9071
 9072
 9073
 9074
 9075
 9076
 9077
 9078
 9079
 9080
 9081
 9082
 9083
 9084
 9085
 9086
 9087
 9088
 9089
 9090
 9091
 9092
 9093
 9094
 9095
 9096
 9097
 9098
 9099
 9100
 9101
 9102
 9103
 9104
 9105
 9106
 9107
 9108
 9109
 9110
 9111
 9112
 9113
 9114
 9115
 9116
 9117
 9118
 9119
 9120
 9121
 9122
 9123
 9124
 9125
 9126
 9127
 9128
 9129
 9130
 9131
 9132
 9133
 9134
 9135
 9136
 9137
 9138
 9139
 9140
 9141
 9142
 9143
 9144
 9145
 9146
 9147
 9148
 9149
 9150
 9151
 9152
 9153
 9154
 9155
 9156
 9157
 9158
 9159
 9160
 9161
 9162
 9163
 9164
 9165
 9166
 9167
 9168
 9169
 9170
 9171
 9172
 9173
 9174
 9175
 9176
 9177
 9178
 9179
 9180
 9181
 9182
 9183
 9184
 9185
 9186
 9187
 9188
 9189
 9190
 9191
 9192
 9193
 9194
 9195
 9196
 9197
 9198
 9199
 9200
 9201
 9202
 9203
 9204
 9205
 9206
 9207
 9208
 9209
 9210
 9211
 9212
 9213
 9214
 9215
 9216
 9217
 9218
 9219
 9220
 9221
 9222
 9223
 9224
 9225
 9226
 9227
 9228
 9229
 9230
 9231
 9232
 9233
 9234
 9235
 9236
 9237
 9238
 9239
 9240
 9241
 9242
 9243
 9244
 9245
 9246
 9247
 9248
 9249
 9250
 9251
 9252
 9253
 9254
 9255
 9256
 9257
 9258
 9259
 9260
 9261
 9262
 9263
 9264
 9265
 9266
 9267
 9268
 9269
 9270
 9271
 9272
 9273
 9274
 9275
 9276
 9277
 9278
 9279
 9280
 9281
 9282
 9283
 9284
 9285
 9286
 9287
 9288
 9289
 9290
 9291
 9292
 9293
 9294
 9295
 9296
 9297
 9298
 9299
 9300
 9301
 9302
 9303
 9304
 9305
 9306
 9307
 9308
 9309
 9310
 9311
 9312
 9313
 9314
 9315
 9316
 9317
 9318
 9319
 9320
 9321
 9322
 9323
 9324
 9325
 9326
 9327
 9328
 9329
 9330
 9331
 9332
 9333
 9334
 9335
 9336
 9337
 9338
 9339
 9340
 9341
 9342
 9343
 9344
 9345
 9346
 9347
 9348
 9349
 9350
 9351
 9352
 9353
 9354
 9355
 9356
 9357
 9358
 9359
 9360
 9361
 9362
 9363
 9364
 9365
 9366
 9367
 9368
 9369
 9370
 9371
 9372
 9373
 9374
 9375
 9376
 9377
 9378
 9379
 9380
 9381
 9382
 9383
 9384
 9385
 9386
 9387
 9388
 9389
 9390
 9391
 9392
 9393
 9394
 9395
 9396
 9397
 9398
 9399
 9400
 9401
 9402
 9403
 9404
 9405
 9406
 9407
 9408
 9409
 9410
 9411
 9412
 9413
 9414
 9415
 9416
 9417
 9418
 9419
 9420
 9421
 9422
 9423
 9424
 9425
 9426
 9427
 9428
 9429
 9430
 9431
 9432
 9433
 9434
 9435
 9436
 9437
 9438
 9439
 9440
 9441
 9442
 9443
 9444
 9445
 9446
 9447
 9448
 9449
 9450
 9451
 9452
 9453
 9454
 9455
 9456
 9457
 9458
 9459
 9460
 9461
 9462
 9463
 9464
 9465
 9466
 9467
 9468
 9469
 9470
 9471
 9472
 9473
 9474
 9475
 9476
 9477
 9478
 9479
 9480
 9481
 9482
 9483
 9484
 9485
 9486
 9487
 9488
 9489
 9490
 9491
 9492
 9493
 9494
 9495
 9496
 9497
 9498
 9499
 9500
 9501
 9502
 9503
 9504
 9505
 9506
 9507
 9508
 9509
 9510
 9511
 9512
 9513
 9514
 9515
 9516
 9517
 9518
 9519
 9520
 9521
 9522
 9523
 9524
 9525
 9526
 9527
 9528
 9529
 9530
 9531
 9532
 9533
 9534
 9535
 9536
 9537
 9538
 9539
 9540
 9541
 9542
 9543
 9544
 9545
 9546
 9547
 9548
 9549
 9550
 9551
 9552
 9553
 9554
 9555
 9556
 9557
 9558
 9559
 9560
 9561
 9562
 9563
 9564
 9565
 9566
 9567
 9568
 9569
 9570
 9571
 9572
 9573
 9574
 9575
 9576
 9577
 9578
 9579
 9580
 9581
 9582
 9583
 9584
 9585
 9586
 9587
 9588
 9589
 9590
 9591
 9592
 9593
 9594
 9595
 9596
 9597
 9598
 9599
 9600
 9601
 9602
 9603
 9604
 9605
 9606
 9607
 9608
 9609
 9610
 9611
 9612
 9613
 9614
 9615
 9616
 9617
 9618
 9619
 9620
 9621
 9622
 9623
 9624
 9625
 9626
 9627
 9628
 9629
 9630
 9631
 9632
 9633
 9634
 9635
 9636
 9637
 9638
 9639
 9640
 9641
 9642
 9643
 9644
 9645
 9646
 9647
 9648
 9649
 9650
 9651
 9652
 9653
 9654
 9655
 9656
 9657
 9658
 9659
 9660
 9661
 9662
 9663
 9664
 9665
 9666
 9667
 9668
 9669
 9670
 9671
 9672
 9673
 9674
 9675
 9676
 9677
 9678
 9679
 9680
 9681
 9682
 9683
 9684
 9685
 9686
 9687
 9688
 9689
 9690
 9691
 9692
 9693
 9694
 9695
 9696
 9697
 9698
 9699
 9700
 9701
 9702
 9703
 9704
 9705
 9706
 9707
 9708
 9709
 9710
 9711
 9712
 9713
 9714
 9715
 9716
 9717
 9718
 9719
 9720
 9721
 9722
 9723
 9724
 9725
 9726
 9727
 9728
 9729
 9730
 9731
 9732
 9733
 9734
 9735
 9736
 9737
 9738
 9739
 9740
 9741
 9742
 9743
 9744
 9745
 9746
 9747
 9748
 9749
 9750
 9751
 9752
 9753
 9754
 9755
 9756
 9757
 9758
 9759
 9760
 9761
 9762
 9763
 9764
 9765
 9766
 9767
 9768
 9769
 9770
 9771
 9772
 9773
 9774
 9775
 9776
 9777
 9778
 9779
 9780
 9781
 9782
 9783
 9784
 9785
 9786
 9787
 9788
 9789
 9790
 9791
 9792
 9793
 9794
 9795
 9796
 9797
 9798
 9799
 9800
 9801
 9802
 9803
 9804
 9805
 9806
 9807
 9808
 9809
 9810
 9811
 9812
 9813
 9814
 9815
 9816
 9817
 9818
 9819
 9820
 9821
 9822
 9823
 9824
 9825
 9826
 9827
 9828
 9829
 9830
 9831
 9832
 9833
 9834
 9835
 9836
 9837
 9838
 9839
 9840
 9841
 9842
 9843
 9844
 9845
 9846
 9847
 9848
 9849
 9850
 9851
 9852
 9853
 9854
 9855
 9856
 9857
 9858
 9859
 9860
 9861
 9862
 9863
 9864
 9865
 9866
 9867
 9868
 9869
 9870
 9871
 9872
 9873
 9874
 9875
 9876
 9877
 9878
 9879
 9880
 9881
 9882
 9883
 9884
 9885
 9886
 9887
 9888
 9889
 9890
 9891
 9892
 9893
 9894
 9895
 9896
 9897
 9898
 9899
 9900
 9901
 9902
 9903
 9904
 9905
 9906
 9907
 9908
 9909
 9910
 9911
 9912
 9913
 9914
 9915
 9916
 9917
 9918
 9919
 9920
 9921
 9922
 9923
 9924
 9925
 9926
 9927
 9928
 9929
 9930
 9931
 9932
 9933
 9934
 9935
 9936
 9937
 9938
 9939
 9940
 9941
 9942
 9943
 9944
 9945
 9946
 9947
 9948
 9949
 9950
 9951
 9952
 9953
 9954
 9955
 9956
 9957
 9958
 9959
 9960
 9961
 9962
 9963
 9964
 9965
 9966
 9967
 9968
 9969
 9970
 9971
 9972
 9973
 9974
 9975
 9976
 9977
 9978
 9979
 9980
 9981
 9982
 9983
 9984
 9985
 9986
 9987
 9988
 9989
 9990
 9991
 9992
 9993
 9994
 9995
 9996
 9997
 9998
 9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282

# Kernel gdb macros
#
#  These gdb macros should be useful during kernel development in
#  determining what's going on in the kernel.
#
#  All the convenience variables used by these macros begin with $kgm_

set print asm-demangle on
set cp-abi gnu-v2

# This option tells gdb to relax its stack tracing heuristics
# Useful for debugging across stack switches
# (to the interrupt stack, for instance). Requires gdb-675 or greater.
set backtrace sanity-checks off

echo Loading Kernel GDB Macros package.  Type "help kgm" for more info.\n

define kgm
printf ""
echo  These are the gdb macros for kernel debugging.  Type "help kgm" for more info.\n
end

document kgm
| These are the kernel gdb macros.  These gdb macros are intended to be
| used when debugging a remote kernel via the kdp protocol.  Typically, you
| would connect to your remote target like so:
| 		 (gdb) target remote-kdp
| 		 (gdb) attach <name-of-remote-host>
|
| The following macros are available in this package:
|     showversion    Displays a string describing the remote kernel version
|
|     showalltasks   Display a summary listing of all tasks
|     showallthreads Display info about all threads in the system
|     showallstacks  Display the stack for each thread in the system
|     showcurrentthreads   Display info about the thread running on each cpu
|     showcurrentstacks    Display the stack for the thread running on each cpu
|     showallvm      Display a summary listing of all the vm maps
|     showallvme     Display a summary listing of all the vm map entries
|     showallipc     Display a summary listing of all the ipc spaces
|     showipcsummary     Display a summary listing of the ipc spaces of all tasks
|     showallrights  Display a summary listing of all the ipc rights
|     showallkexts   Display a summary listing of all loaded kexts (alias: showallkmods)
|     showallknownkexts   Display a summary listing of all kexts, loaded or not
|     showallbusyports    Display a listing of all ports with unread messages
|     showallprocessors   Display a listing of all psets and processors
|
|     showallclasses    Display info about all OSObject subclasses in the system
|     showobject        Show info about an OSObject - its vtable ptr and retain count, & more info for simple container classes.
|     showregistry      Show info about all registry entries in the current plane
|     showregistryprops Show info about all registry entries in the current plane, and their properties
|     showregistryentry Show info about a registry entry; its properties and descendants in the current plane
|     setregistryplane  Set the plane to be used for the iokit registry macros (pass zero for list)
|
|     setfindregistrystr  Set the encoded string for matching with
|                         findregistryentry or findregistryprop (created from
|                         strcmp_arg_pack64)
|     findregistryentry   Find a registry entry that matches the encoded string
|     findregistryentries Find all the registry entries that match the encoded string
|     findregistryprop    Search the registry entry for a property that matches
|                         the encoded string
|
|     showtask       Display info about the specified task
|     showtaskthreads      Display info about the threads in the task
|     showtaskstacks Display the stack for each thread in the task
|     showtaskvm     Display info about the specified task's vm_map
|     showtaskvme    Display info about the task's vm_map entries
|     showtaskipc    Display info about the specified task's ipc space
|     showtaskrights Display info about the task's ipc space entries
|     showtaskrightsbt Display info about the task's ipc space entries with back traces
|     showtaskbusyports    Display all of the task's ports with unread messages
|
|     showact	     Display info about a thread specified by activation
|     showactstack   Display the stack for a thread specified by activation
|
|     showmap	     Display info about the specified vm_map
|     showmapvme     Display a summary list of the specified vm_map's entries
|
|     showipc        Display info about the specified ipc space
|     showrights     Display a summary list of all the rights in an ipc space
|
|     showpid        Display info about the process identified by pid
|     showproc       Display info about the process identified by proc struct
|     showprocinfo   Display detailed info about the process identified by proc struct
|     showprocfiles  Given a proc_t pointer, display the list of open file descriptors
|     showproclocks  Given a proc_t pointer, display the list of advisory file locks
|     zombproc       Print out all procs in the zombie list
|     showproctree   Show all the processes in a hierarchical tree form
|     allproc        Print out all process in the system not in the zombie list
|     zombstacks     Print out all stacks of tasks that are exiting
|
|     showinitchild  Print out all processes in the system which are children of init process
|
|     showkext	     Display info about a kext (alias: showkmod)
|     showkextaddr   Given an address, display the kext and offset (alias: showkmodaddr)
|
|     dumpcallqueue  Dump out all the entries given a queue head
|
|     showallmtx     Display info about mutexes usage
|     showallrwlck   Display info about reader/writer locks usage
|
|     zprint         Display info about the memory zones
|     showioalloc    Display info about iokit allocations
|     paniclog       Display the panic log info
|
|     switchtoact    Switch to different context specified by activation
|     switchtoctx    Switch to different context
|     showuserstack  Display numeric backtrace of the user stack for an 
|     		     activation
|     showtaskuserstacks	 Display user stacks for a specified task
|     showuserregisters		 Display user registers for the specified thread
|     showtaskuserregisters		 Display user registers for the specified task
|
|     switchtouserthread Switch to the user context of the specified thread
|     resetstacks    Return to the original kernel context
|
|     resetctx       Reset context
|     resume_on      Resume when detaching from gdb
|     resume_off     Don't resume when detaching from gdb 
|
|     sendcore	     Configure kernel to send a coredump to the specified IP
|     sendsyslog     Configure kernel to send a system log to the specified IP
|     sendpaniclog   Configure kernel to send a panic log to the specified IP
|     disablecore    Configure the kernel to disable coredump transmission
|     getdumpinfo    Retrieve the current remote dump parameters
|     setdumpinfo    Configure the remote dump parameters
|
|     switchtocorethread Corefile version of "switchtoact"
|     resetcorectx   Corefile version of "resetctx"
|
|     readphys8      Reads the specified untranslated address (8-bit read)
|     readphys16     Reads the specified untranslated address (16-bit read)
|     readphys32     Reads the specified untranslated address (32-bit read)
|     readphys64     Reads the specified untranslated address (64-bit read)
|     writephys8     Writes to the specified untranslated address (8-bit write)
|     writephys16    Writes to the specified untranslated address (16-bit write)
|     writephys32    Writes to the specified untranslated address (32-bit write)
|     writephys64    Writes to the specified untranslated address (64-bit write)
|
|     readioport8    Read 8-bits from the specified I/O Port
|     readioport16   Read 16-bits from the specified I/O Port
|     readioport32   Read 32-bits from the specified I/O Port
|     writeioport8   Write 8-bits into the specified I/O Port
|     writeioport16  Write 16-bits into the specified I/O Port
|     writeioport32  Write 32-bits into the specified I/O Port
|
|     readmsr64      Read 64-bits from the specified MSR
|     writemsr64     Write 64-bits into the specified MSR
|
|     rtentry_showdbg Print the debug information of a route entry
|     rtentry_trash  Walk the list of trash route entries
|
|     inifa_showdbg  Print the debug information of an IPv4 interface address
|     in6ifa_showdbg Print the debug information of an IPv6 interface address
|     inm_showdbg    Print the debug information of an IPv4 multicast address
|     ifma_showdbg   Print the debug information of a link multicast address
|     ifpref_showdbg Print the debug information of an interface ref count
|
|     ndpr_showdbg   Print the debug information of a nd_prefix structure
|     nddr_showdbg   Print the debug information of a nd_defrouter structure
|
|     imo_showdbg    Print the debug information of a ip_moptions structure
|     im6o_showdbg   Print the debug information of a ip6_moptions structure
|
|     inifa_trash    Walk the list of trash in_ifaddr entries
|     in6ifa_trash   Walk the list of trash in6_ifaddr entries
|     inm_trash      Walk the list of trash in_multi entries
|     in6m_trash     Walk the list of trash in6_multi entries
|     ifma_trash     Walk the list of trash ifmultiaddr entries
|
|     mbuf_walkpkt   Walk the mbuf packet chain (m_nextpkt)
|     mbuf_walk      Walk the mbuf chain (m_next)
|     mbuf_buf2slab  Find the slab structure of the corresponding buffer
|     mbuf_buf2mca   Find the mcache audit structure of the corresponding mbuf
|     mbuf_showmca   Print the contents of an mbuf mcache audit structure
|     mbuf_showactive   Print all active/in-use mbuf objects
|     mbuf_showinactive Print all freed/in-cache mbuf objects 
|     mbuf_showall   Print all mbuf objects
|     mbuf_slabs     Print all slabs in the group
|     mbuf_slabstbl  Print slabs table
|     mbuf_stat      Print extended mbuf allocator statistics
|     mbuf_countchain   Count the length of an mbuf chain
|     mbuf_topleak   Print the top suspected mbuf leakers
|     mbuf_traceleak Print the leak information for a given leak address
|
|     mcache_walkobj     Walk the mcache object chain (obj_next)
|     mcache_stat        Print all mcaches in the system
|     mcache_showcache   Display the number of objects in the cache
|
|     showbootargs       Display boot arguments passed to the target kernel
|     showbootermemorymap Dump phys memory map from EFI
|
|     systemlog          Display the kernel's printf ring buffer
|
|     hexdump            Show the contents of memory as a hex/ASCII dump
|
|     showvnodepath      Print the path for a vnode
|     showvnodelocks     Display list of advisory locks held/blocked on a vnode
|     showvnodedev       Display information about a device vnode
|     showtty            Display information about a struct tty
|     showallvols        Display a summary of mounted volumes
|     showvnode          Display info about one vnode
|     showvolvnodes      Display info about all vnodes of a given volume
|     showvolbusyvnodes  Display info about busy (iocount!=0) vnodes of a given volume
|     showallbusyvnodes  Display info about all busy (iocount!=0) vnodes
|     showallvnodes      Display info about all vnodes
|     print_vnode        Print out the fields of a vnode struct
|     showprocvnodes     Print out all the open fds which are vnodes in a process
|     showallprocvnodes  Print out all the open fds which are vnodes in any process
|     showmountvnodes    Print the vnode list
|     showmountallvnodes Print the vnode inactive list
|     showworkqvnodes    Print the vnode worker list
|     shownewvnodes      Print the new vnode list
|
|     ifconfig           display ifconfig-like output
|     showifnets         show the list of attached and detached interfaces
|     showifaddrs        show the list of addresses for the given ifp
|     showifmultiaddrs   show the list of multicast addresses for the given ifp
|     showinmultiaddrs   show the list of IPv4 multicast addresses records 
|     showin6multiaddrs  show the list of IPv6 multicast addresses records
|
|     showsocket         Display information about a socket
|     showprocsockets    Given a proc_t pointer, display information about its sockets
|     showallprocsockets Display information about the sockets of all the processes
|
|     show_tcp_pcbinfo   Display the list of the TCP protocol control blocks
|     show_tcp_timewaitslots Display the list of the TCP protocol control blocks in TIMEWAIT
|     show_udp_pcbinfo   Display the list of UDP protocol control blocks
|
|     show_rt_inet       Display the IPv4 routing table
|     show_rt_inet6      Display the IPv6 routing table
|
|     showpmworkqueue       Display the IOPMWorkQueue object
|     showregistrypmstate   Display power management state for all IOPower registry entries
|     showioservicepm       Display the IOServicePM object
|     showstacksaftertask   showallstacks starting after a given task
|     showstacksafterthread showallstacks starting after a given thread
|
|     showMCAstate	Print machine-check register state after MC exception.
|
|     showallgdbstacks	Cause GDB to trace all thread stacks
|     showallgdbcorestacks Corefile equivalent of "showallgdbstacks"
|     kdp-reenter	Schedule reentry into the debugger and continue.
|     kdp-reboot	Restart remote target
|     kdp-version       Get KDP version number
|
|     zstack		Print zalloc caller stack (zone leak debugging)
|     findoldest	Find oldest zone leak debugging record
|     countpcs		Print how often a pc occurs in the zone leak log
|
|     showtopztrace     Print the ztrace with the most outstanding allocated memory
|     showztrace		Print a backtrace record given its index
|     showzalloc	    Print an allocation record + stacktrace at index
|     showztraceaddr    Print a backtrace record given its address
|     showztracesabove  Print all the backtrace records with a size bigger than X
|     showzstacktrace   Symbolicate and print a stored OSBacktrace
|     
|     showztraces       Finds all in-use traces in the ztraces table
|     showzallocs       Finds all in-use allocations in the zallocs table
|     showzstats        Shows the statistics gathered about the hash tables
|
|     showzallocsfortrace   Print all the allocations that refer to a trace
|     showztracehistogram   Prints a histogram of the ztraces table
|     showzallochistogram   Prints a histogram of the zallocs table	
|
|     pmap_walk     Perform a page-table walk
|     pmap_vtop     Translate a virtual address to physical address
|
|     showuserdyldinfo       Show dyld information and error messages
|                            in the target task
|     showuserlibraries      Show binary images known by dyld in the
|                            target task
|     showallvmstats	Prints a summary of vm statistics in a table format
|     memstats			Displays memory statistics in a table format
|
|     showthreadfortid	Displays the address of the thread structure
|                       for a given thread_id value. 
|     
|     strcmp_nomalloc   A version of strcmp that avoids the use of malloc
|                       through the use of encoded strings created via
|                       strcmp_arg_pack64.
|     strcmp_arg_pack64 Pack a string into a 64-bit quantity for use by
|                       strcmp_nomalloc
|
|     pci_cfg_read8     Read 8-bits from a PCI config space register
|     pci_cfg_read16    Read 16-bits from a PCI config space register
|     pci_cfg_read32    Read 32-bits from a PCI config space register
|     pci_cfg_write8    Write 8-bits into a PCI config space register
|     pci_cfg_write16   Write 16-bits into a PCI config space register
|     pci_cfg_write32   Write 32-bits into a PCI config space register
|     pci_cfg_dump      Dump entire config space for a PCI device
|     pci_cfg_scan      Perform a scan for PCI devices
|     pci_cfg_dump_all  Dump config spaces for all detected PCI devices
|
|     lapic_read32      Read APIC entry
|     lapic_write32     Write APIC entry
|     lapic_dump        Dump APIC entries
|
|     ioapic_read32     Read IOAPIC entry
|     ioapic_write32    Write IOAPIC entry
|     ioapic_dump       Dump IOAPIC entries
|
|     showallproviders  Display summary listing of all dtrace_providers
|     showallmodctls    Display summary listing of all dtrace modctls
|     showmodctl        Display info about a dtrace modctl
|     showfbtprobe      Display info about an fbt probe given an id (traverses fbt_probetab)
|     processortimers   Display all processor timers, noting any inconsistencies
|
| Type "help <macro>" for more specific help on a particular macro.
| Type "show user <macro>" to see what the macro is really doing.
end

# This macro should appear before any symbol references, to facilitate
# a gdb "source" without a loaded symbol file.
define showversion
    kdp-kernelversion
end

document showversion
Syntax: showversion
| Read the kernel version string from a fixed address in low
| memory. Useful if you don't know which kernel is on the other end,
| and need to find the appropriate symbols. Beware that if you've
| loaded a symbol file, but aren't connected to a remote target,
| the version string from the symbol file will be displayed instead.
| This macro expects to be connected to the remote kernel to function
| correctly.
end

set $kgm_mtype_ppc      = 0x00000012
set $kgm_mtype_arm      = 0x0000000C

set $kgm_mtype_i386     = 0x00000007
set $kgm_mtype_x86_64   = 0x01000007
set $kgm_mtype_x86_any  = $kgm_mtype_i386
set $kgm_mtype_x86_mask = 0xFEFFFFFF

set $kgm_mtype = ((unsigned int *)&_mh_execute_header)[1]
set $kgm_lp64 = $kgm_mtype & 0x01000000

set $kgm_manual_pkt_ppc    = 0x549C
set $kgm_manual_pkt_i386   = 0x249C
set $kgm_manual_pkt_x86_64 = 0xFFFFFF8000002930
set $kgm_manual_pkt_arm    = 0xFFFF04A0

set $kgm_kdp_pkt_data_len   = 128

# part of data packet
set $kgm_kdp_pkt_hdr_req_off = 0
set $kgm_kdp_pkt_hdr_seq_off = 1
set $kgm_kdp_pkt_hdr_len_off = 2
set $kgm_kdp_pkt_hdr_key_off = 4

# after data packet
set $kgm_kdp_pkt_len_off     = $kgm_kdp_pkt_data_len
set $kgm_kdp_pkt_input_off   = $kgm_kdp_pkt_data_len + 4

set $kgm_kdp_pkt_hostreboot = 0x13
set $kgm_kdp_pkt_hdr_size   = 8


set $kgm_readphys_force_kdp     = 0
set $kgm_readphys_force_physmap = 0

set $kgm_lcpu_self      = 0xFFFE

set $kgm_reg_depth = 0
set $kgm_reg_depth_max = 0xFFFF
set $kgm_reg_plane = (IORegistryPlane *) gIOServicePlane
set $kgm_namekey = (OSSymbol *) 0
set $kgm_childkey = (OSSymbol *) 0

set $kgm_show_object_addrs = 0
set $kgm_show_object_retain = 0
set $kgm_show_props = 0
set $kgm_show_data_alwaysbytes = 0

set $kgm_show_kmod_syms = 0

# send a manual packet header that doesn't require knowing the location
# of everything.
define manualhdrint
       set $req = $arg0

       set $hdrp = (uint32_t *) $kgm_manual_pkt_i386
       if ($kgm_mtype == $kgm_mtype_ppc)
          set $hdrp = (uint32_t *) $kgm_manual_pkt_ppc
          set $req = $req << 1 # shift to deal with endiannness
       end
       if ($kgm_mtype == $kgm_mtype_x86_64)
          set $hdrp = (uint64_t *) $kgm_manual_pkt_x86_64
       end
       if ($kgm_mtype == $kgm_mtype_arm)
          set $hdrp = (uint32_t *) $kgm_manual_pkt_arm
       end

       set $pkt_hdr = *$hdrp
       set *((uint8_t *) ($pkt_hdr + $kgm_kdp_pkt_input_off))    = 0
       set *((uint32_t *) ($pkt_hdr + $kgm_kdp_pkt_len_off))     = $kgm_kdp_pkt_hdr_size

       set *((uint8_t *) ($pkt_hdr + $kgm_kdp_pkt_hdr_req_off))  = $req
       set *((uint8_t *) ($pkt_hdr + $kgm_kdp_pkt_hdr_seq_off))  = 0
       set *((uint16_t *) ($pkt_hdr + $kgm_kdp_pkt_hdr_len_off)) = $kgm_kdp_pkt_hdr_size
       set *((uint32_t *) ($pkt_hdr + $kgm_kdp_pkt_hdr_key_off)) = 0
       set *((uint8_t *) ($pkt_hdr + $kgm_kdp_pkt_input_off))    = 1

       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
end

# Print a pointer
define showptr
    if $kgm_lp64
        printf "0x%016llx", $arg0
    else
        printf "0x%08x", $arg0
    end
end

# for headers, leave 8 chars for LP64 pointers
define showptrhdrpad
    if $kgm_lp64
        printf "        "
    end
end

# Print a userspace pointer, using $kgm_tasp
define showuserptr
    set $kgm_userptr_task_64 = ( $kgm_taskp->taskFeatures[0] & 0x80000000)
    if $kgm_userptr_task_64
        printf "0x%016llx", $arg0
    else
        printf "0x%08x", $arg0
    end
end

define showkmodheader
    printf   "kmod_info "
    showptrhdrpad
    printf "  address   "
    showptrhdrpad
    printf "  size      "
    showptrhdrpad
    printf "  id    refs     version  name\n"
end

define showkmodint
    set $kgm_kmodp = (struct kmod_info *)$arg0
    showptr $kgm_kmodp
    printf "  "
    showptr $kgm_kmodp->address
    printf "  "
    showptr $kgm_kmodp->size
    printf "  "
    printf "%3d  ", $kgm_kmodp->id
    printf "%5d  ", $kgm_kmodp->reference_count
    printf "%10s  ", $kgm_kmodp->version
    printf "%s\n", $kgm_kmodp->name
end

# cached info of the last kext found, to speed up subsequent lookups
set $kgm_pkmod = 0
set $kgm_pkmodst = 0
set $kgm_pkmoden = 0

define showkmodaddrint
    showptr $arg0
    if ((unsigned long)$arg0 >= (unsigned long)$kgm_pkmodst) && ((unsigned long)$arg0 < (unsigned long)$kgm_pkmoden)
	set $kgm_off = ((unsigned long)$arg0 - (unsigned long)$kgm_pkmodst)
	printf " <%s + 0x%x>", $kgm_pkmod->name, $kgm_off
    else
    	set $kgm_kmodp = (struct kmod_info *)kmod
	if ($kgm_mtype == $kgm_mtype_x86_64) && ($arg0 >= (unsigned long)&_mh_execute_header)
	    # kexts are loaded below the kernel for x86_64
	    set $kgm_kmodp = 0
	end
	while $kgm_kmodp
	    set $kgm_off = ((unsigned long)$arg0 - (unsigned long)$kgm_kmodp->address)
	    if ($kgm_kmodp->address <= $arg0) && ($kgm_off < $kgm_kmodp->size)
		printf " <%s + 0x%x>", $kgm_kmodp->name, $kgm_off
		set $kgm_pkmod = $kgm_kmodp
		set $kgm_pkmodst = $kgm_kmodp->address
		set $kgm_pkmoden = $kgm_pkmodst + $kgm_kmodp->size
	    	set $kgm_kmodp = 0
	    else
		set $kgm_kmodp = $kgm_kmodp->next
	    end
	end
    end
end

define showkmodaddr
    showkmodaddrint $arg0
end
document showkmodaddr
Syntax: (gdb) showkmodaddr <addr>
| Given an address, print the offset and name for the kmod containing it
end

define showkmod
    showkmodheader
    showkmodint $arg0
end
document showkmod
Syntax: (gdb) showkmod <kmod>
| Routine to print info about a kext
end

define showkext
    showkmod $arg0
end
document showkext
Syntax: (gdb) showkext <kmod_info_address>
| Routine to print info about a kext
end

define showallkmods
    showkmodheader
    set $kgm_kmodp = (struct kmod_info *)kmod
    while $kgm_kmodp
	showkmodint $kgm_kmodp
    	set $kgm_kmodp = $kgm_kmodp->next
    end
end
document showallkmods
Syntax: (gdb) showallkmods
| Routine to print a summary listing of all loaded kexts
end

define showallkexts
    showallkmods
end
document showallkexts
Syntax: (gdb) showallkexts
| Routine to print a summary listing of all loaded kexts
end

# See OSKextVersion.c for the C code this is based on
#
set $KGM_OSKEXT_VERS_MAJ_MULT   = 100000000
set $KGM_OSKEXT_VERS_MIN_MULT   = 1000000
set $KGM_OSKEXT_VERS_REV_MULT   = 10000
set $KGM_OSKEXT_VERS_STAGE_MULT = 1000

define printoskextversion
    set $vers_scratch = $arg0

    if ($vers_scratch == -1)
        printf "(invalid)"
    else
     
        set $vers_major =  $vers_scratch / $KGM_OSKEXT_VERS_MAJ_MULT

        set $vers_scratch = $vers_scratch - ($vers_major * $KGM_OSKEXT_VERS_MAJ_MULT)
        set $vers_minor = $vers_scratch / $KGM_OSKEXT_VERS_MIN_MULT

        set $vers_scratch = $vers_scratch - ( $vers_minor * $KGM_OSKEXT_VERS_MIN_MULT)
        set $vers_revision =  $vers_scratch / $KGM_OSKEXT_VERS_REV_MULT

        set $vers_scratch = $vers_scratch - ( $vers_revision * $KGM_OSKEXT_VERS_REV_MULT)
        set $vers_stage =  $vers_scratch / $KGM_OSKEXT_VERS_STAGE_MULT

        set $vers_scratch = $vers_scratch - ( $vers_stage * $KGM_OSKEXT_VERS_STAGE_MULT)
        set $vers_stagelevel =  $vers_scratch

        printf "%d.%d", $vers_major, $vers_minor
        if ($vers_revision > 0)
            printf ".%d", $vers_revision
        end
        
        if ($vers_stage == 1)
            printf "d"
        end
        if ($vers_stage == 3)
            printf "a"
        end
        if ($vers_stage == 5)
            printf "b"
        end
        if ($vers_stage == 7)
            printf "fc"
        end
        if ($vers_stage == 1 || $vers_stage == 3 || $vers_stage == 5 || $vers_stage == 7)
            printf "%d", $vers_stagelevel
        end
    end
end

define showallknownkexts
   set $kext_count = sKextsByID->count
   set $kext_index = 0
   printf "%d kexts in sKextsByID:\n", $kext_count

   printf "OSKext *    "
   showptrhdrpad
   printf "load_addr   "
   showptrhdrpad
   
   printf " id  name (version)\n"

   while $kext_index < $kext_count
       set $kext_id = sKextsByID->dictionary[$kext_index].key->string
       set $oskext = (OSKext *)sKextsByID->dictionary[$kext_index].value

       showptr $oskext
       printf "  "

       if ($oskext->flags.loaded)
           showptr $oskext->kmod_info
           printf "  "
           printf "%3d", $oskext->loadTag
       else
           showptrhdrpad
           printf " -------- "
           printf "  "
           printf " --"
        end
        printf "  "

       printf "%.64s (", $kext_id
       printoskextversion (uint64_t)$oskext->version
       printf ")\n"
       set $kext_index = $kext_index + 1
   end
end
document showallknownkexts
Syntax: (gdb) showallknownkexts
| Routine to print a summary listing of all kexts, loaded or not
end

define showactheader
    printf "          "
    showptrhdrpad
    printf "  thread    "
    showptrhdrpad
    printf "    thread_id "
    showptrhdrpad
    printf "  processor "
    showptrhdrpad
    printf "   pri io_policy   state    wait_queue"
    showptrhdrpad
    printf "  wait_event\n"
end


define showactint
	printf "            "
	showptrhdrpad
	set $kgm_thread = *(struct thread *)$arg0
        showptr $arg0
	if ($kgm_thread.static_param)
	   printf "[WQ]"
	else
	   printf "    "
	end
	printf "  0x%llx   ", $kgm_thread.thread_id
	showptr $kgm_thread.last_processor
	printf "   %3d ", $kgm_thread.sched_pri
	if ($kgm_thread.uthread != 0)
	   set $kgm_printed = 0
	   set $kgm_uthread = (struct uthread *)$kgm_thread.uthread
	   if ($kgm_uthread->uu_flag & 0x400)
	      printf "RAGE "
	   else
	      printf "     "
	   end
	   set $diskpolicy = 0
	   if ($kgm_thread->ext_actionstate.hw_disk != 0)
		set $diskpolicy = $kgm_thread->ext_actionstate.hw_disk
	   else 
		if ($kgm_thread->actionstate.hw_disk != 0)
			set $diskpolicy = $kgm_thread->actionstate.hw_disk
		end
	   end
	   if ($kgm_thread->ext_actionstate.hw_bg != 0)
		set $diskpolicy = 5
	   end
	   if ($kgm_thread->actionstate.hw_bg != 0)
		set $diskpolicy = 4
	   end
	   if ($diskpolicy == 2)
	      printf "PASS    "
	      set $kgm_printed = 1
           end
	   if ($diskpolicy == 3)
	      printf "THROT   "
	      set $kgm_printed = 1
	   end
	   if ($diskpolicy == 4)
	      printf "BG_THRT "
	      set $kgm_printed = 1
           end
	   if ($diskpolicy == 5)
	      printf "EBG_THRT"
	      set $kgm_printed = 1
           end
	   if ($kgm_printed == 0)
	      printf "        "
	   end
	end
	set $kgm_state = $kgm_thread.state
	if $kgm_state & 0x80
	    printf "I" 
	end
	if $kgm_state & 0x40
	    printf "P" 
	end
	if $kgm_state & 0x20
	    printf "A" 
	end
	if $kgm_state & 0x10
	    printf "H" 
	end
	if $kgm_state & 0x08
	    printf "U" 
	end
	if $kgm_state & 0x04
	    printf "R" 
	end
	if $kgm_state & 0x02
	    printf "S" 
	end
   	if $kgm_state & 0x01
	    printf "W"
	    printf "\t    " 
	    showptr $kgm_thread.wait_queue
            printf "  "
	    	if (((unsigned long)$kgm_thread.wait_event > (unsigned long)&last_kernel_symbol) \
		    && ($arg1 != 2) && ($kgm_show_kmod_syms == 0))
			showkmodaddr $kgm_thread.wait_event
	        else
			output /a $kgm_thread.wait_event
		end
		if ($kgm_thread.uthread != 0)
		    set $kgm_uthread = (struct uthread *)$kgm_thread.uthread
		    if ($kgm_uthread->uu_wmesg != 0)
			printf "\t \"%s\"", $kgm_uthread->uu_wmesg
		    end
		end
	end
	if ($kgm_thread.uthread != 0)
	   set $kgm_uthread = (struct uthread *)$kgm_thread.uthread
	   if ($kgm_uthread->pth_name && $kgm_uthread->pth_name[0])
	   	   printf "\n\t\tThread Name: %s", $kgm_uthread->pth_name
	   end
	end
	if $arg1 != 0
	    if ($kgm_thread.kernel_stack != 0)
		if ($kgm_thread.uthread != 0)
			printf "\n          "
			set $kgm_uthread = (struct uthread *)$kgm_thread.uthread
			if ($kgm_uthread->uu_kwe.kwe_kwqqueue != 0)
				set $kwq = (ksyn_wait_queue_t)$kgm_uthread->uu_kwe.kwe_kwqqueue
				printf "              kwq_lockcount:0x%x; kwq_retval:0x%x", $kgm_uthread->uu_kwe.kwe_lockseq, $kgm_uthread->uu_kwe.kwe_psynchretval
				printf "\n                "
				show_kwq $kwq
				printf "          "
			end
		end
		if ($kgm_thread.reserved_stack != 0)
			printf "\n          "
			showptrhdrpad
			printf "      reserved_stack="
                   	showptr $kgm_thread.reserved_stack
		end
		printf "\n          "
		showptrhdrpad
		printf "      kernel_stack="
		showptr $kgm_thread.kernel_stack
		if ($kgm_mtype == $kgm_mtype_ppc)
			set $mysp = $kgm_thread.machine.pcb->save_r1
		end
		if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
			set $kgm_statep = (struct x86_kernel_state *) \
				($kgm_thread->kernel_stack + kernel_stack_size \
				 - sizeof(struct x86_kernel_state))
			if ($kgm_mtype == $kgm_mtype_i386)
				set $mysp = $kgm_statep->k_ebp
			else
				set $mysp = $kgm_statep->k_rbp
			end
		end
		if ($kgm_mtype == $kgm_mtype_arm)
			if (((unsigned long)$r7 < ((unsigned long) ($kgm_thread->kernel_stack+kernel_stack_size))) \
                      && ((unsigned long)$r7 > (unsigned long) ($kgm_thread->kernel_stack)))
				set $mysp = $r7
			else
				set $kgm_statep = (struct arm_saved_state *)$kgm_thread.machine.kstackptr
				set $mysp = $kgm_statep->r[7]
			end
		end
		set $prevsp = $mysp - 16
		printf "\n          "
		showptrhdrpad
		printf "      stacktop="
 		showptr $mysp
		if ($kgm_mtype == $kgm_mtype_ppc)
			set $stkmask = 0xf
		else
			set $stkmask = 0x3
		end
		set $kgm_return = 0
		set $kgm_actint_framecount = 0
	    	while ($mysp != 0) && (($mysp & $stkmask) == 0) \
		      && ($mysp != $prevsp) \
		      && ((((unsigned long) $mysp ^ (unsigned long) $prevsp) < 0x2000) \
		      || (((unsigned long)$mysp < ((unsigned long) ($kgm_thread->kernel_stack+kernel_stack_size))) \
		      && ((unsigned long)$mysp > (unsigned long) ($kgm_thread->kernel_stack)))) \
		      && ($kgm_actint_framecount < 128)
			printf "\n          "
			set $kgm_actint_framecount = $kgm_actint_framecount + 1
			showptrhdrpad
			printf "      "
 			showptr $mysp
			printf "  "
			if ($kgm_mtype == $kgm_mtype_ppc)
				set $kgm_return = *($mysp + 8)
			end
			if ($kgm_mtype == $kgm_mtype_i386)
				set $kgm_return = *($mysp + 4)
			end
			if ($kgm_mtype == $kgm_mtype_x86_64)
				set $kgm_return = *(unsigned long *)($mysp + 8)
			end
			if ($kgm_mtype == $kgm_mtype_arm)
				set $kgm_return = *($mysp + 4)
			end
			if (((unsigned long) $kgm_return < (unsigned long) &_mh_execute_header || \
			     (unsigned long) $kgm_return >= (unsigned long) &last_kernel_symbol ) \
			    && ($kgm_show_kmod_syms == 0))
				showkmodaddr $kgm_return
			else
				output /a $kgm_return
			end
			set $prevsp = $mysp
			set $mysp = *(unsigned long *)$mysp
		end
		set $kgm_return = 0
		printf "\n          "
		showptrhdrpad
		printf "      stackbottom="
		showptr $prevsp
	    else
		printf "\n          "
		showptrhdrpad
		printf "      continuation="
		output /a $kgm_thread.continuation
	    end
	    printf "\n"
	else
	    printf "\n"
	end
end	    

define showact
    showactheader
    showactint $arg0 0
end
document showact
Syntax: (gdb) showact <activation> 
| Routine to print out the state of a specific thread.
end


define showactstack
    showactheader
    showactint $arg0 1
end
document showactstack
Syntax: (gdb) showactstack <activation> 
| Routine to print out the stack of a specific thread.
end


define showallthreads
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
        showtaskheader
	showtaskint $kgm_taskp
	showactheader
	set $kgm_head_actp = &($kgm_taskp->threads)
        set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
	while $kgm_actp != $kgm_head_actp
	    showactint $kgm_actp 0
  	    set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
        end
	printf "\n"
    	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
end
document showallthreads
Syntax: (gdb) showallthreads
| Routine to print out info about all threads in the system.
end

define showprocessorint
    set $kgm_processor_int = (struct processor *)$arg0
    printf "Processor "
    showptr $kgm_processor_int
    printf " State %d (cpu_id 0x%x)\n", ($kgm_processor_int)->state, ($kgm_processor_int)->cpu_id
end

define showcurrentthreads
    set $kgm_prp = (struct processor *)processor_list
    while $kgm_prp != 0
        showprocessorint $kgm_prp
	if ($kgm_prp)->active_thread != 0
	    set $kgm_actp = ($kgm_prp)->active_thread
	    showtaskheader
	    showtaskint ($kgm_actp)->task
	    showactheader
	    showactint $kgm_actp 0
	    printf "\n"
	end
	set $kgm_prp = ($kgm_prp)->processor_list
    end
end
document showcurrentthreads
Syntax: (gdb) showcurrentthreads
| Routine to print out info about the thread running on each cpu.
end


define _showrunqint
	set $kgm_runq = (struct run_queue *)$arg0
	
	printf "    Priority Run Queue Info: Count %d\n", $kgm_runq->count
	set $kgm_runq_queue_i = 0
	set $kgm_runq_queue_count = sizeof($kgm_runq->queues)/sizeof($kgm_runq->queues[0])
	while $kgm_runq->count && $kgm_runq_queue_i < $kgm_runq_queue_count
		set $kgm_runq_queue_head = &$kgm_runq->queues[$kgm_runq_queue_i]
		set $kgm_runq_queue_p = $kgm_runq_queue_head->next
		if $kgm_runq_queue_p != $kgm_runq_queue_head
			set $kgm_runq_queue_this_count = 0
			while $kgm_runq_queue_p != $kgm_runq_queue_head
				set $kgm_runq_queue_this_count = $kgm_runq_queue_this_count + 1
				showtask ((thread_t)$kgm_runq_queue_p)->task
				showactstack $kgm_runq_queue_p
				set $kgm_runq_queue_p = $kgm_runq_queue_p->next
			end
			printf "      Queue Priority %3d [", $kgm_runq_queue_i
			showptr $kgm_runq_queue_head
			printf "] Count %d\n", $kgm_runq_queue_this_count
		end
		set $kgm_runq_queue_i = $kgm_runq_queue_i + 1
	end

end

define _showgrrrint
	set $kgm_grrr_runq = $arg0
	
	printf "    GRRR Info: Count %d Weight %d Current Group ", $kgm_grrr_runq->count, $kgm_grrr_runq->weight
	showptr $kgm_grrr_runq->current_group
	printf "\n"
	set $kgm_grrr_group_i = 0
	set $kgm_grrr_group_count = sizeof($kgm_grrr_runq->groups)/sizeof($kgm_grrr_runq->groups[0])
	while $kgm_grrr_runq->count && $kgm_grrr_group_i < $kgm_grrr_group_count
		set $kgm_grrr_group = &$kgm_grrr_runq->groups[$kgm_grrr_group_i]
		if $kgm_grrr_group->count > 0
			printf "      Group %3d [", $kgm_grrr_group->index
			showptr $kgm_grrr_group
			printf "] Count %d Weight %d\n", $kgm_grrr_group->count, $kgm_grrr_group->weight
			set $kgm_grrr_group_client_head = &$kgm_grrr_group->clients
			set $kgm_grrr_group_client = $kgm_grrr_group_client_head->next
			while $kgm_grrr_group_client != $kgm_grrr_group_client_head
				# showtask ((thread_t)$kgm_grrr_group_client)->task
				# showactstack $kgm_grrr_group_client
				set $kgm_grrr_group_client = $kgm_grrr_group_client->next
			end
		end
		set $kgm_grrr_group_i = $kgm_grrr_group_i + 1
	end
end

define showallprocessors
	set $kgm_pset = &pset0

	set $kgm_show_grrr = 0
	set $kgm_show_priority_runq = 0
	set $kgm_show_priority_pset_runq = 0
	set $kgm_show_fairshare_grrr = 0
	set $kgm_show_fairshare_list = 0

	if _sched_enum == 1
		set $kgm_show_priority_runq = 1
		set $kgm_show_fairshare_list = 1
	end
	if _sched_enum == 2
		set $kgm_show_priority_pset_runq = 1
		set $kgm_show_fairshare_list = 1
	end
	if _sched_enum == 4
		set $kgm_show_grrr = 1
		set $kgm_show_fairshare_grrr = 1
	end
	if _sched_enum == 5
		set $kgm_show_priority_runq = 1
		set $kgm_show_fairshare_list = 1
	end
	if _sched_enum == 6
		set $kgm_show_priority_pset_runq = 1
		set $kgm_show_fairshare_list = 1
	end

	while $kgm_pset != 0
		printf "Processor Set "
		showptr $kgm_pset
		printf " Count %d (cpu_id 0x%x-0x%x)\n", ($kgm_pset)->cpu_set_count, ($kgm_pset)->cpu_set_low, ($kgm_pset)->cpu_set_hi
		printf "  Active Processors:\n"
		set $kgm_active_queue_head = &($kgm_pset)->active_queue
		set $kgm_active_elt = $kgm_active_queue_head->next
		while $kgm_active_elt != $kgm_active_queue_head
			set $kgm_processor = (processor_t)$kgm_active_elt
			printf "    "
			showprocessorint $kgm_processor

			if $kgm_show_priority_runq
				set $kgm_runq = &$kgm_processor->runq
				_showrunqint $kgm_runq
			end
			if $kgm_show_grrr
				set $kgm_grrr_runq = &$kgm_processor->grrr_runq
				_showgrrrint $kgm_grrr_runq
			end
			
			if $kgm_processor->processor_meta != 0 && $kgm_processor->processor_meta->primary == $kgm_processor
				set $kgm_processor_meta_idle_head = &$kgm_processor->processor_meta->idle_queue
				set $kgm_processor_meta_idle = $kgm_processor_meta_idle_head->next
				while $kgm_processor_meta_idle != $kgm_processor_meta_idle_head
					printf "      Idle Meta Processor: "
					showprocessorint $kgm_processor_meta_idle
					set $kgm_processor_meta_idle = $kgm_processor_meta_idle->next
				end
			end
			
			set $kgm_active_elt = $kgm_active_elt->next
		end
		printf "  Idle Processors:\n"
		set $kgm_idle_queue_head = &($kgm_pset)->idle_queue
		set $kgm_idle_elt = $kgm_idle_queue_head->next
		while $kgm_idle_elt != $kgm_idle_queue_head
			set $kgm_processor = (processor_t)$kgm_idle_elt
			printf "    "
			showprocessorint $kgm_processor
			
			if $kgm_processor->processor_meta != 0 && $kgm_processor->processor_meta->primary == $kgm_processor
				set $kgm_processor_meta_idle_head = &$kgm_processor->processor_meta->idle_queue
				set $kgm_processor_meta_idle = $kgm_processor_meta_idle_head->next
				while $kgm_processor_meta_idle != $kgm_processor_meta_idle_head
					printf "      Idle Meta Processor: "
					showprocessorint $kgm_processor_meta_idle
					set $kgm_processor_meta_idle = $kgm_processor_meta_idle->next
				end
			end

			set $kgm_idle_elt = $kgm_idle_elt->next
		end

		if $kgm_show_priority_pset_runq
			set $kgm_runq = &$kgm_pset->pset_runq
			printf "\n"
			_showrunqint $kgm_runq
		end
		set $kgm_pset = ($kgm_pset)->pset_list
	end
	
	printf "\n"
	printf "Realtime Queue Count %d\n", rt_runq.count
	set $kgm_rt_runq_head = &rt_runq.queue
	set $kgm_rt_runq = $kgm_rt_runq_head->next
	while $kgm_rt_runq != $kgm_rt_runq_head
		showtask ((thread_t)$kgm_rt_runq)->task
		showact $kgm_rt_runq
		set $kgm_rt_runq = $kgm_rt_runq->next
	end
	
	printf "\n"
	if $kgm_show_fairshare_list
		printf "Fair Share Queue Count %d\n", fs_runq.count
		set $kgm_fs_runq_head = &fs_runq.queue
		set $kgm_fs_runq = $kgm_fs_runq_head->next
		while $kgm_fs_runq != $kgm_fs_runq_head
			showtask ((thread_t)$kgm_fs_runq)->task
			showact $kgm_fs_runq
			set $kgm_fs_runq = $kgm_fs_runq->next
		end
	end
	if $kgm_show_fairshare_grrr
		printf "Fair Share Queue Count %d\n", fs_grrr_runq.count
		set $kgm_fs_grrr = &fs_grrr_runq
		_showgrrrint $kgm_fs_grrr
	end
end
document showallprocessors
Syntax: (gdb) showallprocessors
| Routine to print out info about all psets and processors
end

set $decode_wait_events = 0
define showallstacks
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
	showtaskheader
	showtaskint $kgm_taskp
	set $kgm_head_actp = &($kgm_taskp->threads)
	set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
	while $kgm_actp != $kgm_head_actp
	    showactheader
	    if ($decode_wait_events > 0)
	       showactint $kgm_actp 1
	    else
	       showactint $kgm_actp 2
	    end
	    set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
	end
	printf "\n"
	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end

    printf "\nZombie Processes:\n" 
    zombstacks
end

document showallstacks
Syntax: (gdb) showallstacks
| Routine to print out the stack for each thread in the system.
| If the variable $decode_wait_events is non-zero, the routine attempts to
| interpret thread wait_events as kernel module offsets, which can add to
| processing time.
end

define showcurrentstacks
    set $kgm_prp = processor_list
    while $kgm_prp != 0
    	showprocessorint $kgm_prp
	if ($kgm_prp)->active_thread != 0
	    set $kgm_actp = ($kgm_prp)->active_thread
	    showtaskheader
	    showtaskint ($kgm_actp)->task
	    showactheader
	    showactint $kgm_actp 1
	    printf "\n"
	end
	set $kgm_prp = ($kgm_prp)->processor_list
    end
end

document showcurrentstacks
Syntax: (gdb) showcurrentstacks
| Routine to print out the thread running on each cpu (incl. its stack)
end

define showwaiterheader
    printf "waiters     thread      "
    printf "processor   pri  state  wait_queue  wait_event\n"
end

define showwaitqwaiters
    set $kgm_w_waitqp = (WaitQueue*)$arg0
    set $kgm_w_linksp = &($kgm_w_waitqp->wq_queue)
    set $kgm_w_wqe = (WaitQueueElement *)$kgm_w_linksp->next
    set $kgm_w_found = 0
    while ( (queue_entry_t)$kgm_w_wqe != (queue_entry_t)$kgm_w_linksp)
	if ($kgm_w_wqe->wqe_type != &_wait_queue_link)
		if !$kgm_w_found
			set $kgm_w_found = 1
			showwaiterheader
		end
		set $kgm_w_shuttle = (struct thread *)$kgm_w_wqe
		showactint $kgm_w_shuttle 0
	end	
	set $kgm_w_wqe = (WaitQueueElement *)$kgm_w_wqe->wqe_links.next
    end
end

define showwaitqwaitercount
    set $kgm_wc_waitqp = (WaitQueue*)$arg0
    set $kgm_wc_linksp = &($kgm_wc_waitqp->wq_queue)
    set $kgm_wc_wqe = (WaitQueueElement *)$kgm_wc_linksp->next
    set $kgm_wc_count = 0
    while ( (queue_entry_t)$kgm_wc_wqe != (queue_entry_t)$kgm_wc_linksp)
	if ($kgm_wc_wqe->wqe_type != &_wait_queue_link)
        	set $kgm_wc_count = $kgm_wc_count + 1
	end
        set $kgm_wc_wqe = (WaitQueueElement *)$kgm_wc_wqe->wqe_links.next
    end
    printf "0x%08x  ", $kgm_wc_count
end

define showwaitqmembercount
    set $kgm_mc_waitqsetp = (WaitQueueSet*)$arg0
    set $kgm_mc_setlinksp = &($kgm_mc_waitqsetp->wqs_setlinks)
    set $kgm_mc_wql = (WaitQueueLink *)$kgm_mc_setlinksp->next
    set $kgm_mc_count = 0
    while ( (queue_entry_t)$kgm_mc_wql != (queue_entry_t)$kgm_mc_setlinksp)
        set $kgm_mc_count = $kgm_mc_count + 1
        set $kgm_mc_wql = (WaitQueueLink *)$kgm_mc_wql->wql_setlinks.next
    end
    printf "0x%08x  ", $kgm_mc_count
end

    
define showwaitqmemberheader
    printf "set-members wait_queue  interlock   "
    printf "pol  type   member_cnt  waiter_cnt\n"
end

define showwaitqmemberint
    set $kgm_m_waitqp = (WaitQueue*)$arg0
    printf "            0x%08x  ", $kgm_m_waitqp
    printf "0x%08x  ", $kgm_m_waitqp->wq_interlock.lock_data
    if ($kgm_m_waitqp->wq_fifo)
        printf "Fifo "
    else
	printf "Prio "
    end
    if ($kgm_m_waitqp->wq_type == 0xf1d1)
	printf "Set    "
	showwaitqmembercount $kgm_m_waitqp
    else
	printf "Que    0x00000000  "
    end
    showwaitqwaitercount $kgm_m_waitqp
    printf "\n"
end


define showwaitqmemberofheader
    printf "member-of   wait_queue  interlock   "
    printf "pol  type   member_cnt  waiter_cnt\n"
end

define showwaitqmemberof
    set $kgm_mo_waitqp = (WaitQueue*)$arg0
    set $kgm_mo_linksp = &($kgm_mo_waitqp->wq_queue)
    set $kgm_mo_wqe = (WaitQueueElement *)$kgm_mo_linksp->next
    set $kgm_mo_found = 0
    while ( (queue_entry_t)$kgm_mo_wqe != (queue_entry_t)$kgm_mo_linksp)
	if ($kgm_mo_wqe->wqe_type == &_wait_queue_link)
		if !$kgm_mo_found
			set $kgm_mo_found = 1
			showwaitqmemberofheader
		end
		set $kgm_mo_wqlp = (WaitQueueLink *)$kgm_mo_wqe
		set $kgm_mo_wqsetp = (WaitQueue*)($kgm_mo_wqlp->wql_setqueue)
		showwaitqmemberint $kgm_mo_wqsetp
	end	
	set $kgm_mo_wqe = (WaitQueueElement *)$kgm_mo_wqe->wqe_links.next
    end
end

define showwaitqmembers
    set $kgm_ms_waitqsetp = (WaitQueueSet*)$arg0
    set $kgm_ms_setlinksp = &($kgm_ms_waitqsetp->wqs_setlinks)
    set $kgm_ms_wql = (WaitQueueLink *)$kgm_ms_setlinksp->next
    set $kgm_ms_found = 0
    while ( (queue_entry_t)$kgm_ms_wql != (queue_entry_t)$kgm_ms_setlinksp)
        set $kgm_ms_waitqp = $kgm_ms_wql->wql_element.wqe_queue
        if !$kgm_ms_found  
	    showwaitqmemberheader
	    set $kgm_ms_found = 1
        end
        showwaitqmemberint $kgm_ms_waitqp
	set $kgm_ms_wql = (WaitQueueLink *)$kgm_ms_wql->wql_setlinks.next
    end
end

define showwaitqheader
    printf "wait_queue  ref_count   interlock   "
    printf "pol  type   member_cnt  waiter_cnt\n"
end

define showwaitqint
    set $kgm_waitqp = (WaitQueue*)$arg0
    printf "0x%08x  ", $kgm_waitqp
    if ($kgm_waitqp->wq_type == 0xf1d1)
	printf "0x%08x  ", ((WaitQueueSet*)$kgm_waitqp)->wqs_refcount
    else
	printf "0x00000000  "
    end
    printf "0x%08x  ", $kgm_waitqp->wq_interlock.lock_data
    if ($kgm_waitqp->wq_fifo)
        printf "Fifo "
    else
	printf "Prio "
    end
    if ($kgm_waitqp->wq_type == 0xf1d1)
	printf "Set    "
	showwaitqmembercount $kgm_waitqp
    else
	printf "Que    0x00000000  "
    end
    showwaitqwaitercount $kgm_waitqp
    printf "\n"
end

define showwaitq
    set $kgm_waitq1p = (WaitQueue*)$arg0
    showwaitqheader
    showwaitqint $kgm_waitq1p	
    if ($kgm_waitq1p->wq_type == 0xf1d1)
	showwaitqmembers $kgm_waitq1p
    else
    	showwaitqmemberof $kgm_waitq1p
    end
    showwaitqwaiters $kgm_waitq1p
end

define showmapheader
    printf "vm_map    "
    showptrhdrpad
    printf "  pmap      "
    showptrhdrpad
    printf "  vm_size   "
    showptrhdrpad
    printf " #ents rpage  hint      "
    showptrhdrpad
    printf "  first_free\n"
end

define showvmeheader
    printf "    entry     "
    showptrhdrpad
    printf "  start               prot  #page  object    "
    showptrhdrpad
    printf "  offset\n"
end

define showvmint
    set $kgm_mapp = (vm_map_t)$arg0
    set $kgm_map = *$kgm_mapp
    showptr $arg0
    printf "  "
    showptr $kgm_map.pmap
    printf "  "
    showptr $kgm_map.size
    printf "   %3d ", $kgm_map.hdr.nentries
    if $kgm_map.pmap
	printf "%5d  ", $kgm_map.pmap->stats.resident_count
    else
	printf "<n/a>  "
    end
    showptr $kgm_map.hint
    printf "  "
    showptr $kgm_map.first_free
    printf "\n"
    if $arg1 != 0
        showvmeheader	
        set $kgm_head_vmep = &($kgm_mapp->hdr.links)
        set $kgm_vmep = $kgm_map.hdr.links.next
        while (($kgm_vmep != 0) && ($kgm_vmep != $kgm_head_vmep))
            set $kgm_vme = *$kgm_vmep
            printf "    "
	    showptr $kgm_vmep
            printf "  0x%016llx  ", $kgm_vme.links.start
            printf "%1x", $kgm_vme.protection
            printf "%1x", $kgm_vme.max_protection
            if $kgm_vme.inheritance == 0x0
                printf "S"
            end
            if $kgm_vme.inheritance == 0x1
                printf "C"
            end
            if $kgm_vme.inheritance == 0x2
                printf "-"
            end
            if $kgm_vme.inheritance == 0x3
                printf "D"
            end
            if $kgm_vme.is_sub_map
                printf "s "
            else
                if $kgm_vme.needs_copy
                    printf "n "
                else
                    printf "  "
                end
            end
            printf "%6d  ",($kgm_vme.links.end - $kgm_vme.links.start) >> 12
            showptr $kgm_vme.object.vm_object
            printf "  0x%016llx\n", $kgm_vme.offset
            set $kgm_vmep = $kgm_vme.links.next
        end
    end
    printf "\n"
end


define showmapwiredp
    set $kgm_mapp = (vm_map_t)$arg0
    set $kgm_map = *$kgm_mapp
    set $kgm_head_vmep = &($kgm_mapp->hdr.links)
    set $kgm_vmep = $kgm_map.hdr.links.next
    set $kgm_objp_prev = (struct vm_object *)0
    if $arg1 == 0
        set $kgm_saw_kernel_obj = 0
	set $kgm_wired_count = 0
	set $kgm_objp_print_space = 1
    else
	set $kgm_objp_print_space = 0
    end
    while (($kgm_vmep != 0) && ($kgm_vmep != $kgm_head_vmep))
        set $kgm_vme = *$kgm_vmep
	set $kgm_objp = $kgm_vme.object.vm_object
	if $kgm_vme.is_sub_map
	    if $arg1 == 0
	        set $kgm_mapp_orig = $kgm_mapp
		set $kgm_vmep_orig = $kgm_vmep
		set $kgm_vme_orig = $kgm_vme
		set $kgm_head_vmep_orig = $kgm_head_vmep
		printf "\n****"
		showptr $kgm_objp
		showmapwiredp $kgm_objp 1
		set $kgm_vme = $kgm_vme_orig
		set $kgm_vmep = $kgm_vmep_orig
		set $kgm_mapp = $kgm_mapp_orig
		set $kgm_head_vmep = $kgm_head_vmep_orig
		set $kgm_objp = (struct vm_object *)0
	    else
	        printf "\n????"	    
		showptr $kgm_mapp
	        printf "    "
	        showptr $kgm_vmep
		set $kgm_objp = (struct vm_object *)0
		printf "\n"
	    end
	end
	if ($kgm_objp == $kgm_objp_prev)
	    set $kgm_objp = (struct vm_object *)0
	end
	if $kgm_objp == kernel_object
	   if $kgm_saw_kernel_obj
	       set $kgm_objp = (struct vm_object *)0
	   end
	   set $kgm_saw_kernel_obj = 1
	end
	if $kgm_objp && $kgm_objp->wired_page_count
	    if $kgm_objp_print_space == 1
	        printf "    "
		showptr $kgm_mapp
	    end
	    set $kgm_objp_print_space = 1
	    printf "    "
	    showptr $kgm_vmep
	    printf "  0x%016llx  ", $kgm_vme.links.start
	    printf "%5d", $kgm_vme.alias
            printf "%6d  ",($kgm_vme.links.end - $kgm_vme.links.start) >> 12
	    showptr $kgm_objp
	    printf "[%3d]", $kgm_objp->ref_count
	    printf "%7d\n", $kgm_objp->wired_page_count
	    set $kgm_wired_count = $kgm_wired_count + $kgm_objp->wired_page_count
	    set $kgm_objp_prev = $kgm_objp
	end
        set $kgm_vmep = $kgm_vme.links.next
    end
    if $arg1 == 0
        printf "total wired count = %d\n", $kgm_wired_count
    end
end

define showmapwired
    printf "    map       "
    showptrhdrpad
    printf "    entry     "
    showptrhdrpad
    printf "  start               alias  #page  object    "
    showptrhdrpad
    printf "       wired\n"
    showmapwiredp $arg0 0
end
document showmapwired
Syntax: (gdb) showmapwired <vm_map>
| Routine to print out a summary listing of all the entries with wired pages in a vm_map
end

define showmapvme
	showmapheader
	showvmint $arg0 1
end
document showmapvme
Syntax: (gdb) showmapvme <vm_map>
| Routine to print out a summary listing of all the entries in a vm_map
end


define showmap
	showmapheader
	showvmint $arg0 0
end
document showmap
Syntax: (gdb) showmap <vm_map>
| Routine to print out info about the specified vm_map
end

define showallvm
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
        showtaskheader
	showmapheader
	showtaskint $kgm_taskp
	showvmint $kgm_taskp->map 0
    	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
end
document showallvm
Syntax: (gdb) showallvm
| Routine to print a summary listing of all the vm maps
end


define showallvme
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
        showtaskheader
	showmapheader
	showtaskint $kgm_taskp
	showvmint $kgm_taskp->map 1
    	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
end
document showallvme
Syntax: (gdb) showallvme
| Routine to print a summary listing of all the vm map entries
end


define showipcheader
    printf "ipc_space "
    showptrhdrpad
    printf "  is_table  "
    showptrhdrpad
    printf "  table_next"
    showptrhdrpad
    printf " flags ports  splaysize   "
    showptrhdrpad
    printf "splaybase\n"
end

define showipceheader
    printf "            "
    showptrhdrpad
    printf "object      "
    showptrhdrpad
    showptrhdrpad
    printf "name        rite urefs  destname    "
    showptrhdrpad
    printf "destination\n"
end

define showipceint
    set $kgm_ie = *(ipc_entry_t)$arg0
    printf "            "
    showptrhdrpad
    showptr $kgm_ie.ie_object
    showptrhdrpad
    printf "  0x%08x  ", $arg1
    if $kgm_ie.ie_bits & 0x00100000
        printf "Dead "
        printf "%5d\n", $kgm_ie.ie_bits & 0xffff
    else
        if $kgm_ie.ie_bits & 0x00080000
            printf "SET  "
            printf "%5d\n", $kgm_ie.ie_bits & 0xffff
        else
            if $kgm_ie.ie_bits & 0x00010000
                if $kgm_ie.ie_bits & 0x00020000
                    printf " SR"
                else
                    printf "  S"
                end
            else
                if $kgm_ie.ie_bits & 0x00020000
                   printf "  R"
                end
            end
            if $kgm_ie.ie_bits & 0x00040000
                printf "  O"
            end
            if $kgm_ie.index.request
                set $kgm_port = (ipc_port_t)$kgm_ie.ie_object
		set $kgm_requests = $kgm_port->ip_requests
		set $kgm_req_soright = $kgm_requests[$kgm_ie.index.request].notify.port
		if $kgm_req_soright
#                   Armed send-possible notification?
		    if (uintptr_t)$kgm_req_soright & 0x1
		        printf "s"
                    else
#                       Delayed send-possible notification?
		        if (uintptr_t)$kgm_req_soright & 0x2
			    printf "d"
			else
#                           Dead-name notification
			    printf "n"
			end
		    end     
		else
		    printf " "
         	end
            else
                printf " "
            end
#           Collision (with tree)?
            if $kgm_ie.ie_bits & 0x00800000
                printf "c"
    	    else
                printf " "
    	    end
            printf "%5d  ", $kgm_ie.ie_bits & 0xffff
            showportdest $kgm_ie.ie_object
        end
    end
end

define showipcint
    set $kgm_isp = (ipc_space_t)$arg0
    set $kgm_is = *$kgm_isp
    showptr $arg0
    printf "  "
    showptr $kgm_is.is_table
    printf "  "
    showptr $kgm_is.is_table_next
    printf "  "
    if $kgm_is.is_growing != 0
        printf "G"
    else
        printf " "
    end
    if $kgm_is.is_fast != 0
        printf "F"
    else
        printf " "
    end
    if $kgm_is.is_active != 0
        printf "A  "
    else
        printf "   "
    end
    printf "%5d  ", $kgm_is.is_table_size + $kgm_is.is_tree_total
    showptr $kgm_is.is_tree_total
    printf "  "
    showptr &$kgm_isp->is_tree
    printf "\n"
    if $arg1 != 0
        showipceheader
        set $kgm_iindex = 0
        set $kgm_iep = $kgm_is.is_table
        set $kgm_destspacep = (ipc_space_t)0
        while ( $kgm_iindex < $kgm_is.is_table_size )
            set $kgm_ie = *$kgm_iep
            if $kgm_ie.ie_bits & 0x001f0000
                set $kgm_name = (($kgm_iindex << 8)|($kgm_ie.ie_bits >> 24))
                showipceint $kgm_iep $kgm_name
                if $arg2 != 0
		   if $kgm_ie.ie_object != 0 && ($kgm_ie.ie_bits & 0x00070000) && ((ipc_port_t) $kgm_ie.ie_object)->ip_callstack[0] != 0
                   	printf "              user bt: "
                   	showportbt $kgm_ie.ie_object $kgm_is.is_task
                   end
		end
            end
            set $kgm_iindex = $kgm_iindex + 1
            set $kgm_iep = &($kgm_is.is_table[$kgm_iindex])
        end
        if $kgm_is.is_tree_total
            printf "Still need to write tree traversal\n"
        end
    end
    printf "\n"
end


define showipc
    set $kgm_isp = (ipc_space_t)$arg0
    showipcheader
    showipcint $kgm_isp 0 0
end
document showipc
Syntax: (gdb) showipc <ipc_space>
| Routine to print the status of the specified ipc space
end

define showrights
	set $kgm_isp = (ipc_space_t)$arg0
    showipcheader
	showipcint $kgm_isp 1 0
end
document showrights
Syntax: (gdb) showrights <ipc_space>
| Routine to print a summary list of all the rights in a specified ipc space
end


define showtaskipc
	set $kgm_taskp = (task_t)$arg0
	showtaskheader
	showtaskint $kgm_taskp
        showipcheader
	showipcint $kgm_taskp->itk_space 0 0
end
document showtaskipc
Syntax: (gdb) showtaskipc <task>
| Routine to print info about the ipc space for a task
end


define showtaskrights
	set $kgm_taskp = (task_t)$arg0
	showtaskheader
	showtaskint $kgm_taskp
	showipcheader
	showipcint $kgm_taskp->itk_space 1 0
end
document showtaskrights
Syntax: (gdb) showtaskrights <task>
| Routine to print info about the ipc rights for a task
end

define showtaskrightsbt
	set $kgm_taskp = (task_t)$arg0
	showtaskheader
	showtaskint $kgm_taskp
  	showipcheader
	showipcint $kgm_taskp->itk_space 1 1
end
document showtaskrightsbt
Syntax: (gdb) showtaskrightsbt <task>
| Routine to print info about the ipc rights for a task with backtraces
end

define showallipc
    set $kgm_head_taskp = &tasks
    set $kgm_cur_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_cur_taskp != $kgm_head_taskp
        showtaskheader
        showtaskint $kgm_cur_taskp
        showipcheader
        showipcint $kgm_cur_taskp->itk_space 0 0
    	set $kgm_cur_taskp = (struct task *)($kgm_cur_taskp->tasks.next)
    end
end
document showallipc
Syntax: (gdb) showallipc
| Routine to print a summary listing of all the ipc spaces
end

define showipcsumheader
    printf "task         "
    showptrhdrpad
    printf " pid         "
    printf " #acts         "
    printf " tsize "
    printf "command\n"
end

define showipcsummaryint
    set $kgm_taskp = (struct task *)$arg0
    showptr $arg0
    printf "%7d", ((struct proc *)$kgm_taskp->bsd_info)->p_pid
    printf "%15d", $kgm_taskp->thread_count
    printf "%15d", $kgm_cur_taskp->itk_space.is_table_size	
	printf " %s\n", ((struct proc *)$kgm_taskp->bsd_info)->p_comm
end

define showipcsummary
    showipcsumheader
    set $kgm_head_taskp = &tasks
    set $kgm_cur_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_cur_taskp != $kgm_head_taskp
        showipcsummaryint $kgm_cur_taskp
    	set $kgm_cur_taskp = (struct task *)($kgm_cur_taskp->tasks.next)
    end
end

document showipcsummary
Syntax: (gdb) showipcsummary 
| Summarizes the IPC state of all tasks. This is a convenient way to dump
| some basic clues about IPC messaging. You can use the output to determine
| tasks that are candidates for further investigation.
end


define showallrights
    set $kgm_head_taskp = &tasks
    set $kgm_cur_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_cur_taskp != $kgm_head_taskp
        showtaskheader
        showtaskint $kgm_cur_taskp
        showipcheader
        showipcint $kgm_cur_taskp->itk_space 1 0
    	set $kgm_cur_taskp = (struct task *)($kgm_cur_taskp->tasks.next)
    end
end
document showallrights
Syntax: (gdb) showallrights
| Routine to print a summary listing of all the ipc rights
end


define showtaskvm
	set $kgm_taskp = (task_t)$arg0
	showtaskheader
	showmapheader
	showtaskint $kgm_taskp
	showvmint $kgm_taskp->map 0
end
document showtaskvm
Syntax: (gdb) showtaskvm <task>
| Routine to print out info about a task's vm_map
end

define showtaskvme
	set $kgm_taskp = (task_t)$arg0
	showtaskheader
	showtaskint $kgm_taskp
	showmapheader
	showvmint $kgm_taskp->map 1
end
document showtaskvme
Syntax: (gdb) showtaskvme <task>
| Routine to print out info about a task's vm_map_entries
end


define showtaskheader
    printf "task      "
    showptrhdrpad
    printf "  vm_map    "
    showptrhdrpad
    printf "  ipc_space "
    showptrhdrpad
    printf " #acts "
    showprocheader
end


define showtaskint
    set $kgm_taskp = (struct task *)$arg0
    showptr $arg0
    printf "  "
    showptr $kgm_taskp->map
    printf "  "
    showptr $kgm_taskp->itk_space
    printf " %5d ", $kgm_taskp->thread_count
    showprocint $kgm_taskp->bsd_info
end

define showtask
    showtaskheader
    showtaskint $arg0
end
document showtask
Syntax (gdb) showtask <task>
| Routine to print out info about a task.
end


define showtaskthreads
    showtaskheader
    set $kgm_taskp = (struct task *)$arg0
    showtaskint $kgm_taskp
    showactheader
    set $kgm_head_actp = &($kgm_taskp->threads)
    set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
    while $kgm_actp != $kgm_head_actp
	showactint $kgm_actp 0
    	set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
    end
end
document showtaskthreads
Syntax: (gdb) showtaskthreads <task>
| Routine to print info about the threads in a task.
end


define showtaskstacks
    showtaskheader
    set $kgm_taskp = (struct task *)$arg0
    showtaskint $kgm_taskp
    set $kgm_head_actp = &($kgm_taskp->threads)
    set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
    while $kgm_actp != $kgm_head_actp
        showactheader
	showactint $kgm_actp 1
    	set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
    end
end
document showtaskstacks
Syntax: (gdb) showtaskstacks <task>
| Routine to print out the stack for each thread in a task.
end

define showqueue_elems
	set $queue_head = (struct queue_entry *)($arg0)
	set $queue = (struct queue_entry *)($queue_head->next)
    while $queue != $queue_head
		showptr $queue
		printf " "
		set $thread = (struct thread *)$queue
		set $task = (struct task *)$thread->task
		set $bsd = (struct proc *)$task->bsd_info
		set $guy = (char *)$bsd->p_comm
		showptr $thread
		printf " "
		showptr $task
		printf " "
		showptr $bsd
		printf " "
		showptr $guy
		#printf "  %s\n", $kgm_procp->p_comm
		printf "\n"
    	set $queue = (struct queue_entry *)($queue->next)
    end
end

define showalltasks
    showtaskheader
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
	showtaskint $kgm_taskp
    	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
end
document showalltasks
Syntax: (gdb) showalltasks
| Routine to print a summary listing of all the tasks
| wq_state -> reports "number of workq threads", "number of scheduled workq threads", "number of pending work items"
|   if "number of pending work items" seems stuck at non-zero, it may indicate that the workqueue mechanism is hung
| io_policy -> RAGE  - rapid aging of vnodes requested
|              NORM  - normal I/O explicitly requested (this is the default)
|              PASS  - passive I/O requested (i.e. I/Os do not affect throttling decisions)
|	       THROT - throttled I/O requested (i.e. thread/task may be throttled after each I/O completes)
end

define showprocheader
    printf "  pid  process     "
    showptrhdrpad
    printf "io_policy    wq_state   command\n"
end

define showprocint
    set $kgm_procp = (struct proc *)$arg0
    if $kgm_procp != 0
    	set $kgm_printed = 0
        printf "%5d  ", $kgm_procp->p_pid
	showptr $kgm_procp
	if ($kgm_procp->p_lflag & 0x400000)
	   printf "  RAGE "
	else
	   printf "       "
	end
	set $ptask = (struct task *)$kgm_procp->task
	set $diskpolicy = 0
	if ($ptask->ext_actionstate.hw_disk != 0)
		set $diskpolicy = $ptask->ext_actionstate.hw_disk
	else 
		if ($ptask->actionstate.hw_disk != 0)
			set $diskpolicy = $ptask->actionstate.hw_disk
		end
	end
	if ($ptask->ext_actionstate.hw_bg != 0)
		set $diskpolicy = 5
	end
	if ($ptask->actionstate.hw_bg != 0)
		set $diskpolicy = 4
	end
	if ($diskpolicy == 2)
		printf "PASS    "
		set $kgm_printed = 1
	end
	if ($diskpolicy == 3)
		printf "THROT   "
		set $kgm_printed = 1
	end
	if ($diskpolicy == 4)
		printf "BG_THRT "
		set $kgm_printed = 1
	end
	if ($diskpolicy == 5)
		printf "EBG_THRT"
		set $kgm_printed = 1
	end
	if ($kgm_printed == 0)
	   printf "      "
	end
	set $kgm_wqp = (struct workqueue *)$kgm_procp->p_wqptr
	if $kgm_wqp != 0
	   printf "  %2d %2d %2d ", $kgm_wqp->wq_nthreads, $kgm_wqp->wq_thidlecount, $kgm_wqp->wq_itemcount
	else
	   printf "           "
	end
	printf "  %s\n", $kgm_procp->p_comm
    else
	printf "  *0*  "
        showptr 0
        printf "  --\n"
    end
end

define showpid
    showtaskheader
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
	set $kgm_procp = (struct proc *)$kgm_taskp->bsd_info
	if (($kgm_procp != 0) && ($kgm_procp->p_pid == $arg0))
	    showtaskint $kgm_taskp
	    set $kgm_taskp = $kgm_head_taskp
	else
    	    set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
	end
    end
end
document showpid
Syntax: (gdb) showpid <pid>
| Routine to print a single process by pid
end

define showproc
    showtaskheader
    set $kgm_procp = (struct proc *)$arg0
    showtaskint $kgm_procp->task
end


define kdb
    set switch_debugger=1
    continue
end
document kdb
| kdb - Switch to the inline kernel debugger
|
| usage: kdb
|
| The kdb macro allows you to invoke the inline kernel debugger.
end

define showpsetheader
    printf "portset     "
    showptrhdrpad
    printf "waitqueue   "
    showptrhdrpad
    showptrhdrpad
    printf "recvname    flags refs  recvname    "
    showptrhdrpad
    printf "process\n"
end

define showportheader
    printf "port        "
    showptrhdrpad
    printf "mqueue      "
    showptrhdrpad
    showptrhdrpad
    printf "recvname    flags refs  recvname    "
    showptrhdrpad
    printf "dest\n"
end

define showportmemberheader
    printf "members     "
    showptrhdrpad
    printf "port        "
    showptrhdrpad
    showptrhdrpad
    printf "recvname    "
    printf "flags refs  mqueue      "
    showptrhdrpad
    printf "msgcount\n"
end

define showkmsgheader
    printf "dest-port   "
    showptrhdrpad
    printf "kmsg        "
    showptrhdrpad
    showptrhdrpad
    printf "msgid       "
    printf "disp  size  "
    printf "reply-port  "
    showptrhdrpad
    printf "source\n"
end

define showkmsgsrcint
    set $kgm_kmsgsrchp = ((ipc_kmsg_t)$arg0)->ikm_header
#    set $kgm_kmsgsrctp = (mach_msg_audit_trailer_t *)((uintptr_t)$kgm_kmsgsrchp + $kgm_kmsgsrchp->msgh_size)
#    set $kgm_kmsgpid = $kgm_kmsgsrctp->msgh_audit.val[5]
    set $kgm_kmsgpid = (pid_t)((uint *)((uintptr_t)$kgm_kmsgsrchp + $kgm_kmsgsrchp->msgh_size))[10]
# compare against a well-known or cached value as this may be slow
    if ($kgm_kmsgpid == 0)
       set $kgm_kmsgsrcpid = (pid_t)0
       set $kgm_kmsgsrcprocp = (struct proc *)kernel_task->bsd_info
    else 
       if ($kgm_kmsgpid != $kgm_kmsgsrcpid)
	  set $kgm_kmsgsrchead_taskp = &tasks
          set $kgm_kmsgsrctaskp = (struct task *)($kgm_kmsgsrchead_taskp->next)
          while $kgm_kmsgsrctaskp != $kgm_kmsgsrchead_taskp
	      set $kgm_kmsgsrcprocp = (struct proc *)$kgm_kmsgsrctaskp->bsd_info
	      set $kgm_kmsgsrcpid = $kgm_kmsgsrcprocp->p_pid
	      if (($kgm_kmsgsrcprocp != 0) && ($kgm_kmsgsrcprocp->p_pid == $kgm_kmsgpid))
	          set $kgm_kmsgsrctaskp = $kgm_kmsgsrchead_taskp
	      else
    	          set $kgm_kmsgsrctaskp = (struct task *)($kgm_kmsgsrctaskp->tasks.next)
	      end
          end
       end
    end
    if ($kgm_kmsgsrcprocp->p_pid == $kgm_kmsgpid)
    	printf "%s(%d)\n", $kgm_kmsgsrcprocp->p_comm, $kgm_kmsgpid
    else
        printf "unknown(%d)\n", $kgm_kmsgpid
    end
end

define showkmsgint
    set $kgm_kmsghp = ((ipc_kmsg_t)$arg0)->ikm_header
    set $kgm_kmsgh = *$kgm_kmsghp
    if ($arg1 != 0)
        printf "            "
        showptrhdrpad
    else
        showptr $kgm_kmsgh.msgh_remote_port
    end
    showptr $arg0
    showptrhdrpad
    printf "  0x%08x  ", $kgm_kmsgh.msgh_id
    if (($kgm_kmsgh.msgh_bits & 0xff) == 19)
	printf "rC"
    else
	printf "rM"
    end
    if (($kgm_kmsgh.msgh_bits & 0xff00) == (19 << 8))
	printf "lC"
    else
	printf "lM"
    end
    if ($kgm_kmsgh.msgh_bits & 0xf0000000)
	printf "c"
    else
	printf "s"
    end
    printf "%5d  ", $kgm_kmsgh.msgh_size
    showptr $kgm_kmsgh.msgh_local_port
    printf "  "
    set $kgm_kmsgsrcpid = (pid_t)0
    showkmsgsrcint $arg0
end

define showkmsg
    showkmsgint $arg0 0
end

define showkobject
    set $kgm_portp = (struct ipc_port *)$arg0
    showptr $kgm_portp->ip_kobject
    printf "  kobject("
    set $kgm_kotype = ($kgm_portp->ip_object.io_bits & 0x00000fff)
    if ($kgm_kotype == 1)
	printf "THREAD"
    end
    if ($kgm_kotype == 2)
	printf "TASK"
    end
    if ($kgm_kotype == 3)
	printf "HOST"
    end
    if ($kgm_kotype == 4)
	printf "HOST_PRIV"
    end
    if ($kgm_kotype == 5)
	printf "PROCESSOR"
    end
    if ($kgm_kotype == 6)
	printf "PSET"
    end
    if ($kgm_kotype == 7)
	printf "PSET_NAME"
    end
    if ($kgm_kotype == 8)
	printf "TIMER"
    end
    if ($kgm_kotype == 9)
	printf "PAGER_REQ"
    end
    if ($kgm_kotype == 10)
	printf "DEVICE"
    end
    if ($kgm_kotype == 11)
	printf "XMM_OBJECT"
    end
    if ($kgm_kotype == 12)
	printf "XMM_PAGER"
    end
    if ($kgm_kotype == 13)
	printf "XMM_KERNEL"
    end
    if ($kgm_kotype == 14)
	printf "XMM_REPLY"
    end
    if ($kgm_kotype == 15)
	printf "NOTDEF 15"
    end
    if ($kgm_kotype == 16)
	printf "NOTDEF 16"
    end
    if ($kgm_kotype == 17)
	printf "HOST_SEC"
    end
    if ($kgm_kotype == 18)
	printf "LEDGER"
    end
    if ($kgm_kotype == 19)
	printf "MASTER_DEV"
    end
    if ($kgm_kotype == 20)
	printf "ACTIVATION"
    end
    if ($kgm_kotype == 21)
	printf "SUBSYSTEM"
    end
    if ($kgm_kotype == 22)
	printf "IO_DONE_QUE"
    end
    if ($kgm_kotype == 23)
	printf "SEMAPHORE"
    end
    if ($kgm_kotype == 24)
	printf "LOCK_SET"
    end
    if ($kgm_kotype == 25)
	printf "CLOCK"
    end
    if ($kgm_kotype == 26)
	printf "CLOCK_CTRL"
    end
    if ($kgm_kotype == 27)
	printf "IOKIT_SPARE"
    end
    if ($kgm_kotype == 28)
	printf "NAMED_MEM"
    end
    if ($kgm_kotype == 29)
	printf "IOKIT_CON"
    end
    if ($kgm_kotype == 30)
	printf "IOKIT_OBJ"
    end
    if ($kgm_kotype == 31)
	printf "UPL"
    end
    if ($kgm_kotype == 34)
	printf "FD"
    end
    printf ")\n"
end

define showportdestproc
    set $kgm_portp = (struct ipc_port *)$arg0
    set $kgm_spacep = $kgm_portp->data.receiver
#   check against the previous cached value - this is slow
    if ($kgm_spacep != $kgm_destspacep)
	set $kgm_destprocp = (struct proc *)0
        set $kgm_head_taskp = &tasks
        set $kgm_desttaskp = (struct task *)($kgm_head_taskp->next)
        while (($kgm_destprocp == 0) && ($kgm_desttaskp != $kgm_head_taskp))
	    set $kgm_destspacep = $kgm_desttaskp->itk_space
	    if ($kgm_destspacep == $kgm_spacep)
	       set $kgm_destprocp = (struct proc *)$kgm_desttaskp->bsd_info
	    else
    	       set $kgm_desttaskp = (struct task *)($kgm_desttaskp->tasks.next)
            end
        end
    end
    if $kgm_destprocp != 0
       printf "%s(%d)\n", $kgm_destprocp->p_comm, $kgm_destprocp->p_pid
    else
       printf "task "
       showptr $kgm_desttaskp
       printf "\n"
    end
end

define showportdest
    set $kgm_portp = (struct ipc_port *)$arg0
    set $kgm_spacep = $kgm_portp->data.receiver
    if ((uintptr_t)$kgm_spacep == (uintptr_t)ipc_space_kernel)
	showkobject $kgm_portp
    else
	if ($kgm_portp->ip_object.io_bits & 0x80000000)
	    showptr $kgm_portp->ip_messages.data.port.receiver_name
        printf "  "
	    showportdestproc $kgm_portp
	else
        showptr $kgm_portp
	    printf "  inactive-port\n"
	end
    end
end

define showportmember
    printf "            "
    showptrhdrpad
    showptr $arg0
    showptrhdrpad
    set $kgm_portp = (struct ipc_port *)$arg0
    printf "  0x%08x  ", $kgm_portp->ip_messages.data.port.receiver_name
    if ($kgm_portp->ip_object.io_bits & 0x80000000)
	printf "A"
    else
	printf " "
    end
    printf "Port"
    printf "%5d  ", $kgm_portp->ip_object.io_references
    showptr &($kgm_portp->ip_messages)
    printf "  0x%08x\n", $kgm_portp->ip_messages.data.port.msgcount
end

define showportbt
    set $kgm_iebt = ((ipc_port_t) $arg0)->ip_callstack
    set $kgm_iepid = ((ipc_port_t) $arg0)->ip_spares[0]
    set $kgm_procpid = ((proc_t) (((task_t) $arg1)->bsd_info))->p_pid
    if $kgm_iebt[0] != 0
        showptr $kgm_iebt[0]
        set $kgm_iebt_loop_ctr = 1
        while ($kgm_iebt_loop_ctr < 16 && $kgm_iebt[$kgm_iebt_loop_ctr])
            printf " "
            showptr $kgm_iebt[$kgm_iebt_loop_ctr]
            set $kgm_iebt_loop_ctr = $kgm_iebt_loop_ctr + 1
        end
        if $kgm_iepid != $kgm_procpid
            printf " (%d)", $kgm_iepid
        end
        printf "\n"
    end
end

define showportint
    showptr $arg0
    printf "  "
    set $kgm_portp = (struct ipc_port *)$arg0
    showptr &($kgm_portp->ip_messages)
    showptrhdrpad
    printf "  0x%08x  ", $kgm_portp->ip_messages.data.port.receiver_name
    if ($kgm_portp->ip_object.io_bits & 0x80000000)
	printf "A"
    else
	printf "D"
    end
    printf "Port"
    printf "%5d  ", $kgm_portp->ip_object.io_references
    set $kgm_destspacep = (struct ipc_space *)0
    showportdest $kgm_portp
    set $kgm_kmsgp = (ipc_kmsg_t)$kgm_portp->ip_messages.data.port.messages.ikmq_base
    if $arg1 && $kgm_kmsgp
	showkmsgheader
	showkmsgint $kgm_kmsgp 1
	set $kgm_kmsgheadp = $kgm_kmsgp
	set $kgm_kmsgp = $kgm_kmsgp->ikm_next
	while $kgm_kmsgp != $kgm_kmsgheadp
	    showkmsgint $kgm_kmsgp 1
	    set $kgm_kmsgp = $kgm_kmsgp->ikm_next
        end
    end
end

define showpsetint
    showptr $arg0
    printf "  "
    set $kgm_psetp = (struct ipc_pset *)$arg0
    showptr &($kgm_psetp->ips_messages)
    showptrhdrpad
    printf "  0x%08x  ", $kgm_psetp->ips_messages.data.pset.local_name
    if ($kgm_psetp->ips_object.io_bits & 0x80000000)
	printf "A"
    else
	printf "D"
    end
    printf "Set "
    printf "%5d  ", $kgm_psetp->ips_object.io_references
    showptr $kgm_psetp->ips_messages.data.pset.local_name
    printf "  "
    set $kgm_setlinksp = &($kgm_psetp->ips_messages.data.pset.set_queue.wqs_setlinks)
    set $kgm_wql = (WaitQueueLink *)$kgm_setlinksp->next
    set $kgm_found = 0
    while ( (queue_entry_t)$kgm_wql != (queue_entry_t)$kgm_setlinksp)
        set $kgm_portp = (struct ipc_port *)((uintptr_t)($kgm_wql->wql_element->wqe_queue) - (uintptr_t)$kgm_portoff)
	if !$kgm_found  
	    set $kgm_destspacep = (struct ipc_space *)0
	    showportdestproc $kgm_portp
	    showportmemberheader
	    set $kgm_found = 1
	end
	showportmember $kgm_portp 0
	set $kgm_wql = (WaitQueueLink *)$kgm_wql->wql_setlinks.next
    end
    if !$kgm_found
	printf "--n/e--\n"
    end
end

define showpset
    set $kgm_portoff = &(((struct ipc_port *)0)->ip_messages)
    showpsetheader
    showpsetint $arg0 1
end

define showport
    showportheader
    showportint $arg0 1
end

define showipcobject
    set $kgm_objectp = (ipc_object_t)$arg0
    if ($kgm_objectp->io_bits & 0x7fff0000)
        set $kgm_portoff = &(((struct ipc_port *)0)->ip_messages)
	showpset $kgm_objectp
    else
	showport $kgm_objectp
    end
end

define showmqueue
    set $kgm_mqueue = *(struct ipc_mqueue *)$arg0
    if ($kgm_mqueue.data.pset.set_queue.wqs_wait_queue.wq_type == 0xf1d1)
	set $kgm_psetoff = &(((struct ipc_pset *)0)->ips_messages)
	set $kgm_pset = (((long)$arg0) - ((long)$kgm_psetoff))
        showpsetheader
	showpsetint $kgm_pset 1
    end
    if ($kgm_mqueue.data.pset.set_queue.wqs_wait_queue.wq_type == 0xf1d0)
        set $kgm_portoff = &(((struct ipc_port *)0)->ip_messages)
	set $kgm_port = (((long)$arg0) - ((long)$kgm_portoff))
	showportheader
	showportint $kgm_port 1
    end
end

define zprint_one
    set $kgm_zone = (struct zone *)$arg0

    showptr $kgm_zone
    printf "  %8d ",$kgm_zone->count
    printf "%8x ",$kgm_zone->cur_size
    printf "%8x ",$kgm_zone->max_size
    printf "%8d ",$kgm_zone->elem_size
    printf "%8x ",$kgm_zone->alloc_size
	printf " %16ld ",$kgm_zone->num_allocs
	printf "%16ld ",$kgm_zone->num_frees
    printf "%s ",$kgm_zone->zone_name

    if ($kgm_zone->exhaustible)
        printf "H"
    end
    if ($kgm_zone->collectable)
        printf "C"
    end
    if ($kgm_zone->expandable)
        printf "X"
    end
    if ($kgm_zone->noencrypt)
        printf "$"
    end
    printf "\n"
end


define zprint
    printf "ZONE      "
    showptrhdrpad
    printf "     COUNT   TOT_SZ   MAX_SZ   ELT_SZ ALLOC_SZ         TOT_ALLOC         TOT_FREE NAME\n"
    set $kgm_zone_ptr = (struct zone *)first_zone
    while ($kgm_zone_ptr != 0)
        zprint_one $kgm_zone_ptr
        set $kgm_zone_ptr = $kgm_zone_ptr->next_zone
    end
    printf "\n"
end
document zprint
Syntax: (gdb) zprint
| Routine to print a summary listing of all the kernel zones
end

define showmtxgrp
    set $kgm_mtxgrp = (struct _lck_grp_ *)$arg0

    if ($kgm_mtxgrp->lck_grp_mtxcnt)
        showptr $kgm_mtxgrp
        printf " %8d ",$kgm_mtxgrp->lck_grp_mtxcnt
        printf "%12u ",$kgm_mtxgrp->lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_util_cnt
        printf "%8u ",$kgm_mtxgrp->lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_miss_cnt
        printf "%8u ",$kgm_mtxgrp->lck_grp_stat.lck_grp_mtx_stat.lck_grp_mtx_wait_cnt
        printf "%s ",&$kgm_mtxgrp->lck_grp_name
        printf "\n"
    end
end


define showallmtx
    printf "LCK GROUP "
    showptrhdrpad
    printf "      CNT         UTIL     MISS     WAIT NAME\n"
    set $kgm_mtxgrp_ptr = (struct _lck_grp_ *)&lck_grp_queue
    set $kgm_mtxgrp_ptr = (struct _lck_grp_ *)$kgm_mtxgrp_ptr->lck_grp_link.next
    while ($kgm_mtxgrp_ptr != (struct _lck_grp_ *)&lck_grp_queue)
        showmtxgrp $kgm_mtxgrp_ptr
        set $kgm_mtxgrp_ptr = (struct _lck_grp_ *)$kgm_mtxgrp_ptr->lck_grp_link.next
    end
    printf "\n"
end
document showallmtx
Syntax: (gdb) showallmtx
| Routine to print a summary listing of all mutexes
end

define showrwlckgrp
    set $kgm_rwlckgrp = (struct _lck_grp_ *)$arg0

    if ($kgm_rwlckgrp->lck_grp_rwcnt)
        showptr $kgm_rwlckgrp
        printf " %8d ",$kgm_rwlckgrp->lck_grp_rwcnt
        printf "%12u ",$kgm_rwlckgrp->lck_grp_stat.lck_grp_rw_stat.lck_grp_rw_util_cnt
        printf "%8u ",$kgm_rwlckgrp->lck_grp_stat.lck_grp_rw_stat.lck_grp_rw_miss_cnt
        printf "%8u ",$kgm_rwlckgrp->lck_grp_stat.lck_grp_rw_stat.lck_grp_rw_wait_cnt
        printf "%s ",&$kgm_rwlckgrp->lck_grp_name
        printf "\n"
    end
end


define showallrwlck
    printf "LCK GROUP "
    showptrhdrpad
    printf "      CNT         UTIL     MISS     WAIT NAME\n"
    set $kgm_rwlckgrp_ptr = (struct _lck_grp_ *)&lck_grp_queue
    set $kgm_rwlckgrp_ptr = (struct _lck_grp_ *)$kgm_rwlckgrp_ptr->lck_grp_link.next
    while ($kgm_rwlckgrp_ptr != (struct _lck_grp_ *)&lck_grp_queue)
        showrwlckgrp $kgm_rwlckgrp_ptr
        set $kgm_rwlckgrp_ptr = (struct _lck_grp_ *)$kgm_rwlckgrp_ptr->lck_grp_link.next
    end
    printf "\n"
end
document showallrwlck
Syntax: (gdb) showallrwlck
| Routine to print a summary listing of all read/writer locks
end

set $kdp_act_counter = 0
set $kdp_arm_act_counter = 0

set $r0_save	= 0
set $r1_save	= 0
set $r2_save	= 0
set $r3_save	= 0
set $r4_save	= 0
set $r5_save	= 0
set $r6_save	= 0
set $r7_save	= 0
set $r8_save	= 0
set $r9_save	= 0
set $r10_save	= 0
set $r11_save	= 0
set $r12_save	= 0
set $sp_save	= 0
set $lr_save	= 0
set $pc_save	= 0

define showcontext_int
	echo Context switched, current instruction pointer: 
	output/a $pc
	echo \n
end

define switchtoact
	set $newact = (struct thread *) $arg0
	select 0
	if ($newact->kernel_stack == 0)
		echo This activation does not have a stack.\n
		echo continuation:
		output/a (unsigned) $newact.continuation
		echo \n
	else
		if ($kgm_mtype == $kgm_mtype_ppc)
			if ($kdp_act_counter == 0)
				set $kdpstate = (struct savearea *) kdp.saved_state
			end
			set $kdp_act_counter = $kdp_act_counter + 1
			set (struct savearea *) kdp.saved_state=$newact->machine->pcb
			flushregs
			flushstack
			set $pc=$newact->machine->pcb.save_srr0
			update
		end
		if ($kgm_mtype == $kgm_mtype_i386)
			set $kdpstatep = (struct x86_saved_state32 *) kdp.saved_state
			if ($kdp_act_counter == 0)
			   set $kdpstate = *($kdpstatep)
			end	
			set $kdp_act_counter = $kdp_act_counter + 1
	
			set $kgm_statep = (struct x86_kernel_state *) \
						($newact->kernel_stack + kernel_stack_size \
						 - sizeof(struct x86_kernel_state))
			set $kdpstatep->ebx = $kgm_statep->k_ebx 
			set $kdpstatep->ebp = $kgm_statep->k_ebp 
			set $kdpstatep->edi = $kgm_statep->k_edi 
			set $kdpstatep->esi = $kgm_statep->k_esi 
			set $kdpstatep->eip = $kgm_statep->k_eip 
			flushregs
			flushstack
			set $pc = $kgm_statep->k_eip
			update
		end
		if ($kgm_mtype == $kgm_mtype_x86_64)
			set $kdpstatep = (struct x86_saved_state64 *) kdp.saved_state
			if ($kdp_act_counter == 0)
			   set $kdpstate = *($kdpstatep)
			end	
			set $kdp_act_counter = $kdp_act_counter + 1
	
			set $kgm_statep = (struct x86_kernel_state *) \
						($newact->kernel_stack + kernel_stack_size \
						 - sizeof(struct x86_kernel_state))
			set $kdpstatep->rbx = $kgm_statep->k_rbx 
			set $kdpstatep->rbp = $kgm_statep->k_rbp 
			set $kdpstatep->r12 = $kgm_statep->k_r12 
			set $kdpstatep->r13 = $kgm_statep->k_r13 
			set $kdpstatep->r14 = $kgm_statep->k_r14 
			set $kdpstatep->r15 = $kgm_statep->k_r15 
			set $kdpstatep->isf.rsp = $kgm_statep->k_rsp 
			flushregs
			flushstack
			set $pc = $kgm_statep->k_rip
			update
		end
		if ($kgm_mtype == $kgm_mtype_arm)
			set $kdp_arm_act_counter = $kdp_arm_act_counter + 1
			if ($kdp_arm_act_counter == 1)
				set $r0_save   = $r0
				set $r1_save   = $r1
				set $r2_save   = $r2
				set $r3_save   = $r3
				set $r4_save   = $r4
				set $r5_save   = $r5
				set $r6_save   = $r6
				set $r7_save   = $r7
				set $r8_save   = $r8
				set $r9_save   = $r9
				set $r10_save  = $r10
				set $r11_save  = $r11
				set $r12_save  = $r12
				set $sp_save   = $sp
				set $lr_save   = $lr
				set $pc_save   = $pc
			end
			set $pc_ctx = load_reg+8
			set $kgm_statep = (struct arm_saved_state *)((struct thread*)$arg0)->machine.kstackptr
			set $r0 =  $kgm_statep->r[0]
			set $r1 =  $kgm_statep->r[1]
			set $r2 =  $kgm_statep->r[2]
			set $r3 =  $kgm_statep->r[3]
			set $r4 =  $kgm_statep->r[4]
			set $r5 =  $kgm_statep->r[5]
			set $r6 =  $kgm_statep->r[6]
			set $r8 =  $kgm_statep->r[8]
			set $r9 =  $kgm_statep->r[9]
			set $r10 = $kgm_statep->r[10]
			set $r11 = $kgm_statep->r[11]
			set $r12 = $kgm_statep->r[12]
			set $sp = $kgm_statep->sp
			set $lr = $kgm_statep->lr
			set $pc = $pc_ctx
			set $r7 =  $kgm_statep->r[7]
			flushregs
			flushstack
		end
	end
	showcontext_int
end

document switchtoact  
Syntax: switchtoact <address of activation>
| This command allows gdb to examine the execution context and call
| stack for the specified activation. For example, to view the backtrace
| for an activation issue "switchtoact <address>", followed by "bt".
| Before resuming execution, issue a "resetctx" command, to
| return to the original execution context.
end     

define switchtoctx
	select 0
	if ($kgm_mtype == $kgm_mtype_ppc)
		if ($kdp_act_counter == 0)
		   set $kdpstate = (struct savearea *) kdp.saved_state
		end
		set $kdp_act_counter = $kdp_act_counter + 1
		set (struct savearea *) kdp.saved_state=(struct savearea *) $arg0
		flushregs
		flushstack
		set $pc=((struct savearea *) $arg0)->save_srr0
		update
	else
	if ($kgm_mtype == $kgm_mtype_arm)
		select 0
		set $kdp_arm_act_counter = $kdp_arm_act_counter + 1
		if ($kdp_arm_act_counter == 1)
			set $r0_save   = $r0
			set $r1_save   = $r1
			set $r2_save   = $r2
			set $r3_save   = $r3
			set $r4_save   = $r4
			set $r5_save   = $r5
			set $r6_save   = $r6
			set $r7_save   = $r7
			set $r8_save   = $r8
			set $r9_save   = $r9
			set $r10_save  = $r10
			set $r11_save  = $r11
			set $r12_save  = $r12
			set $sp_save   = $sp
			set $lr_save   = $lr
			set $pc_save   = $pc
		end
		set $kgm_statep = (struct arm_saved_state *)$arg0
		set $r0 =  $kgm_statep->r[0]
		set $r1 =  $kgm_statep->r[1]
		set $r2 =  $kgm_statep->r[2]
		set $r3 =  $kgm_statep->r[3]
		set $r4 =  $kgm_statep->r[4]
		set $r5 =  $kgm_statep->r[5]
		set $r6 =  $kgm_statep->r[6]
		set $r8 =  $kgm_statep->r[8]
		set $r9 =  $kgm_statep->r[9]
		set $r10 = $kgm_statep->r[10]
		set $r11 = $kgm_statep->r[11]
		set $r12 = $kgm_statep->r[12]
		set $sp = $kgm_statep->sp
		set $lr = $kgm_statep->lr
		set $r7 =  $kgm_statep->r[7]
		set $pc = $kgm_statep->pc
		flushregs
		flushstack
		update
	else
		echo switchtoctx not implemented for this architecture.\n
	end
end

document switchtoctx  
Syntax: switchtoctx <address of pcb>
| This command allows gdb to examine an execution context and dump the
| backtrace for this execution context.
| Before resuming execution, issue a "resetctx" command, to
| return to the original execution context.
end     

define resetctx
	select 0
	if ($kdp_act_counter != 0)
		if ($kgm_mtype == $kgm_mtype_ppc)
			set (struct savearea *)kdp.saved_state=$kdpstate
			flushregs
			flushstack
			set $pc=((struct savearea *) kdp.saved_state)->save_srr0
			update
			set $kdp_act_counter = 0
		end
		if ($kgm_mtype == $kgm_mtype_i386)
			set $kdpstatep = (struct x86_saved_state32 *) kdp.saved_state
			set *($kdpstatep)=$kdpstate
			flushregs
			flushstack
			set $pc=$kdpstatep->eip
			update
			set $kdp_act_counter = 0
		end
		if ($kgm_mtype == $kgm_mtype_x86_64)
			set $kdpstatep = (struct x86_saved_state64 *) kdp.saved_state
			set *($kdpstatep)=$kdpstate
			flushregs
			flushstack
			set $pc=$kdpstatep->isf.rip
			update
			set $kdp_act_counter = 0
		end
		showcontext_int
	end
	if ($kgm_mtype == $kgm_mtype_arm && $kdp_arm_act_counter != 0)
		echo Restoring context\n
		set $r0  = $r0_save
		flushregs
		set $r1  = $r1_save
		flushregs
		set $r2  = $r2_save
		flushregs
		set $r3  = $r3_save
		flushregs
		set $r4  = $r4_save
		flushregs
		set $r5  = $r5_save
		flushregs
		set $r6  = $r6_save
		flushregs
		set $r8  = $r8_save
		flushregs
		set $r9  = $r9_save
		flushregs
		set $r10 = $r10_save
		flushregs
		set $r11 = $r11_save
		flushregs
		set $r12 = $r12_save
		flushregs
		set $sp  = $sp_save
		flushregs
		set $lr  = $lr_save
		flushregs
		set $pc  = $pc_save
		flushregs
		set $r7  = $r7_save
		flushregs
		flushstack
		update
		set $kdp_arm_act_counter = 0
	end
end     
        
document resetctx
| Syntax: resetctx
| Returns to the original execution context. This command should be
| issued if you wish to resume execution after using the "switchtoact"
| or "switchtoctx" commands.
end     

# This is a pre-hook for the continue command, to prevent inadvertent attempts 
# to resume from the context switched to for examination.
define hook-continue
       resetctx
end

# This is a pre-hook for the detach command, to prevent inadvertent attempts 
# to resume from the context switched to for examination.
define hook-detach
       resetctx
end

define resume_on
        set $resume = KDP_DUMPINFO_SETINFO | KDP_DUMPINFO_RESUME
        dumpinfoint $resume
end

document resume_on
| Syntax: resume_on
| The target system will resume when detaching  or exiting from gdb. 
| This is the default behavior.
end

define resume_off
        set $noresume = KDP_DUMPINFO_SETINFO | KDP_DUMPINFO_NORESUME
        dumpinfoint $noresume
end

document resume_off
| Syntax: resume_off
| The target system  won't resume after detaching from gdb and
| can be attached with a new gdb session
end

define paniclog
	set $kgm_panic_bufptr = debug_buf
	set $kgm_panic_bufptr_max = debug_buf_ptr
	while $kgm_panic_bufptr < $kgm_panic_bufptr_max
		if *(char *)$kgm_panic_bufptr == 10
			printf "\n"
		else
			printf "%c", *(char *)$kgm_panic_bufptr
		end
		set $kgm_panic_bufptr= (char *)$kgm_panic_bufptr + 1
	end
end

document paniclog
| Syntax: paniclog
| Display the panic log information
|
end

define dumpcallqueue
	set $kgm_callhead = $arg0
	set $kgm_callentry = $kgm_callhead->next
	set $kgm_i = 0
	while $kgm_callentry != $kgm_callhead
		set $kgm_call = (struct call_entry *)$kgm_callentry
		printf "0x%08x ", $kgm_call
		printf "0x%08x 0x%08x ", $kgm_call->param0, $kgm_call->param1
		output $kgm_call->deadline
		printf "\t"
		output $kgm_call->func
		printf "\n"
		set $kgm_i = $kgm_i + 1
		set $kgm_callentry = $kgm_callentry->next
	end
	printf "%d entries\n", $kgm_i
end

document dumpcallqueue
| Syntax: dumpcallqueue <queue head>
| Displays the contents of the specified call_entry queue.
end

define showtaskacts
showtaskthreads $arg0
end
document showtaskacts
| See help showtaskthreads.
end

define showallacts
showallthreads
end
document showallacts
| See help showallthreads.
end


define resetstacks
       _kgm_flush_loop
       set kdp_pmap = 0
       _kgm_flush_loop
       resetctx
       _kgm_flush_loop
       _kgm_update_loop
       resetctx
       _kgm_update_loop
end

document resetstacks
| Syntax: resetstacks
| Internal kgmacro routine used by the "showuserstack" macro 
| to reset the target pmap to the kernel pmap.
end

#Barely effective hacks to work around bugs in the "flush" and "update" 
#gdb commands in Tiger (up to 219); these aren't necessary with Panther
#gdb, but do no harm.
define _kgm_flush_loop
       set $kgm_flush_loop_ctr = 0
       while ($kgm_flush_loop_ctr < 30)
       	     flushregs
	     flushstack
	     set $kgm_flush_loop_ctr = $kgm_flush_loop_ctr + 1
       end
end

define _kgm_update_loop
       set $kgm_update_loop_ctr = 0
       while ($kgm_update_loop_ctr < 30)
       	     update
       	     set $kgm_update_loop_ctr = $kgm_update_loop_ctr + 1
       end
end
# Internal routine used by "_loadfrom" to read from 64-bit addresses
# on 32-bit kernels
define _loadk32m64
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_readmem64_req_t)
       set $kgm_pkt = (kdp_readmem64_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request = KDP_READMEM64
       set $kgm_pkt->hdr.len = sizeof(kdp_readmem64_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq = 0
       set $kgm_pkt->hdr.key = 0
       set $kgm_pkt->address = (uint64_t)$arg0
       set $kgm_pkt->nbytes = sizeof(uint64_t)
       set manual_pkt.input = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_readmem64_reply_t *)&manual_pkt.data
       if ($kgm_pkt->error == 0)
       	  	set $kgm_k32read64 = *(uint64_t *)$kgm_pkt->data
       else
		set $kgm_k32read64 = 0
       end
end

# Internal routine used by "showx86backtrace" to abstract possible loads from
# user space
define _loadfrom
	if (kdp_pmap == 0)
		set $kgm_loadval = *(uintptr_t *)$arg0
	else
	if ($kgm_x86_abi == 0xe)
	      set $kgm_loadval = *(uint32_t *)$arg0
	else
	if ($kgm_x86_abi == 0xf)
	    if ($kgm_mtype == $kgm_mtype_i386)
	    	    _loadk32m64 $arg0
	    	    set $kgm_loadval = $kgm_k32read64
	    else
	    	    set $kgm_loadval = *(uint64_t *)$arg0
	    end
	end
	end
end
end


#This is necessary since gdb often doesn't do backtraces on x86 correctly
#in the absence of symbols.The code below in showuserstack and 
#showx86backtrace also contains several workarouds for the gdb bug where 
#gdb stops macro evaluation because of spurious "Cannot read memory"
#errors on x86. These errors appear on ppc as well, but they don't
#always stop macro evaluation.

set $kgm_cur_frame = 0
set $kgm_cur_pc = 0
set $kgm_x86_abi = 0
define showx86backtrace
	if ($kgm_mtype == $kgm_mtype_i386)
		set $kgm_frame_reg = $ebp
		set $kgm_pc = $eip
		set $kgm_ret_off = 4
	end
	if ($kgm_mtype == $kgm_mtype_x86_64)
		set $kgm_frame_reg = $rbp
		set $kgm_pc = $rip
		set $kgm_ret_off = 8
	end

	if ($kgm_x86_abi == 0xe)
		set $kgm_ret_off = 4
	end
	if ($kgm_x86_abi == 0xf)
		set $kgm_ret_off = 8
	end

	if ($kgm_cur_frame == 0)
		set $kgm_cur_frame = $kgm_frame_reg
	end
	if ($kgm_cur_pc == 0)
		set $kgm_cur_pc = $kgm_pc
	end
	printf "0: Frame: 0x%016llx PC: 0x%016llx\n", $kgm_cur_frame, $kgm_cur_pc
	if (!(($kgm_x86_abi == 0xf) && ($kgm_mtype == $kgm_mtype_i386)))
		x/i $kgm_cur_pc
	end
	set $kgm_tmp_frame = $kgm_cur_frame
	set $kgm_cur_frame = 0
	set $kgm_cur_pc = 0
	_loadfrom ($kgm_tmp_frame)
	set $kgm_prev_frame = $kgm_loadval
	_loadfrom ($kgm_tmp_frame+$kgm_ret_off)
	set $kgm_prev_pc = $kgm_loadval
	set $kgm_frameno = 1
	while ($kgm_prev_frame != 0) && ($kgm_prev_frame != 0x0000000800000008)
		printf "%d: Saved frame: 0x%016llx Saved PC: 0x%016llx\n", $kgm_frameno, $kgm_prev_frame, $kgm_prev_pc
		if (!(($kgm_x86_abi == 0xf) && ($kgm_mtype == $kgm_mtype_i386)))
		   x/i $kgm_prev_pc
		end
		_loadfrom ($kgm_prev_frame+$kgm_ret_off)
		set $kgm_prev_pc = $kgm_loadval
		_loadfrom ($kgm_prev_frame)
		set $kgm_prev_frame = $kgm_loadval
		set $kgm_frameno = $kgm_frameno + 1
	end
	set kdp_pmap = 0
	set $kgm_x86_abi = 0
end

define showx86backtrace2
	set $kgm_cur_frame = $arg0
	set $kgm_cur_pc = $arg1
	showx86backtrace
end

define showuserstack
		select 0
  		if ($kgm_mtype == $kgm_mtype_ppc)	
		   if ($kdp_act_counter == 0)
		      set $kdpstate = (struct savearea *) kdp.saved_state
		   end
		   set $kdp_act_counter = $kdp_act_counter + 1
		   set $newact = (struct thread *) $arg0
		   _kgm_flush_loop
		   set $checkpc = $newact->machine->upcb.save_srr0
		   if ($checkpc == 0)
		      echo This activation does not appear to have
		      echo \20 a valid user context.\n
		   else	      
		     set (struct savearea *) kdp.saved_state=$newact->machine->upcb
		     set $pc = $checkpc
#flush and update seem to be executed lazily by gdb on Tiger, hence the
#repeated invocations - see 3743135
	   	    _kgm_flush_loop
# This works because the new pmap is used only for reads
		     set kdp_pmap = $newact->task->map->pmap
		     _kgm_flush_loop
		     _kgm_update_loop
		     bt
		     resetstacks
		     _kgm_flush_loop
		     _kgm_update_loop
		     resetstacks
		     _kgm_flush_loop
		     _kgm_update_loop
		   end
		else
		if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
			set $newact = (struct thread *) $arg0
			set $newiss = (x86_saved_state_t *) ($newact->machine->iss)
			set $kgm_x86_abi = $newiss.flavor
			if ($newiss.flavor == 0xf) 
	   			set $checkpc = $newiss.uss.ss_64.isf.rip
				set $checkframe = $newiss.uss.ss_64.rbp

			else
				set $checkpc = $newiss.uss.ss_32.eip
				set $checkframe = $newiss.uss.ss_32.ebp
			end

			if ($checkpc == 0)
			    echo This activation does not appear to have
			    echo \20 a valid user context.\n
			else
			set $kgm_cur_frame = $checkframe
			set $kgm_cur_pc = $checkpc
# When have more than one argument is present, don't print usage
			if ( $argc == 1 )
				printf "You may now issue the showx86backtrace command to see the user space backtrace for this thread ("
				showptr $arg0
				printf "); you can also examine memory locations in this address space (pmap "
				showptr $newact->task->map->pmap
				printf ") before issuing the backtrace. This two-step process is necessary to work around various bugs in x86 gdb, which cause it to stop memory evaluation on spurious memory read errors. Additionally, you may need to issue a set kdp_pmap = 0 command after the showx86backtrace completes, to resume reading from the kernel address space.\n"
			end
			set kdp_pmap = $newact->task->map->pmap
			_kgm_flush_loop
			_kgm_update_loop
			end			
		else
		if ($kgm_mtype == $kgm_mtype_arm)
			if (kdp->is_conn > 0)
				set $kgm_threadp = (struct thread *)$arg0
				set $kgm_saved_pmap = kdp_pmap
				showactheader
				showactint $kgm_threadp 0
				set $kgm_thread_pmap = $kgm_threadp->task->map->pmap
				set $kgm_thread_sp = $kgm_threadp.machine->PcbData.r[7]
				set kdp_pmap = $kgm_thread_pmap
				while ($kgm_thread_sp != 0)
    				set $link_register = *($kgm_thread_sp + 4)
    				showptrhdrpad
					printf "                  "
					showptr $kgm_thread_sp
					printf "  "
    				showptr $link_register
    				printf "\n"
    				set $kgm_thread_sp = *$kgm_thread_sp
  				end
				set kdp_pmap = $kgm_saved_pmap
			else
				echo You must be connected via nanokdp to use this macro\n
			end
		else
			echo showuserstack not supported on this architecture\n
		end
		end
		end
end
document showuserstack
Syntax: showuserstack <address of thread activation>
|This command displays a numeric backtrace for the user space stack of
|the given thread activation. It may, of course, fail to display a
|complete backtrace if portions of the user stack are not mapped in.
|Symbolic backtraces can be obtained either by running gdb on the
|user space binary, or a tool such as "symbolicate".
|Note that while this command works on Panther's gdb, an issue
|with Tiger gdb (3743135) appears to hamper the evaluation of this
|macro in some cases.
end

define showtaskuserstacks
    set $kgm_taskp = (struct task *)$arg0
    set $kgm_head_actp = &($kgm_taskp->threads)
    set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
    while $kgm_actp != $kgm_head_actp
    	printf "For thread "
	showptr $kgm_actp
	printf "\n"
	showuserstack $kgm_actp quiet
	if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
		showx86backtrace
	end
	set kdp_pmap=0
    	set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
    	printf "\n"
    end
    showuserlibraries $kgm_taskp
end
document showtaskuserstacks
Syntax: (gdb) showtaskuserstacks <task>
| Print out the user stack for each thread in a task, followed by the user libraries.
end


define showuserregisters
	set $kgm_threadp = (struct thread *)$arg0
	set $kgm_taskp = $kgm_threadp->task
	if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
		set $newiss = (x86_saved_state_t *) ($kgm_threadp->machine.iss)
		set $kgm_x86_abi = $newiss.flavor
		if ($newiss.flavor == 0xf)
			printf "X86 Thread State (64-bit):\n"
	   		set $kgm_ss64 = $newiss.uss.ss_64

			printf "  rax: "
			showuserptr $kgm_ss64.rax
			printf "  rbx: "
			showuserptr $kgm_ss64.rbx
			printf "  rcx: "
			showuserptr $kgm_ss64.rcx
			printf "  rdx: "
			showuserptr $kgm_ss64.rdx
			printf "\n"

			printf "  rdi: "
			showuserptr $kgm_ss64.rdi
			printf "  rsi: "
			showuserptr $kgm_ss64.rsi
			printf "  rbp: "
			showuserptr $kgm_ss64.rbp
			printf "  rsp: "
			showuserptr $kgm_ss64.isf.rsp
			printf "\n"

			printf "   r8: "
			showuserptr $kgm_ss64.r8
			printf "   r9: "
			showuserptr $kgm_ss64.r9
			printf "  r10: "
			showuserptr $kgm_ss64.r10
			printf "  r11: "
			showuserptr $kgm_ss64.r11
			printf "\n"

			printf "  r12: "
			showuserptr $kgm_ss64.r12
			printf "  r13: "
			showuserptr $kgm_ss64.r13
			printf "  r14: "
			showuserptr $kgm_ss64.r14
			printf "  r15: "
			showuserptr $kgm_ss64.r15
			printf "\n"

			printf "  rip: "
			showuserptr $kgm_ss64.isf.rip
			printf "  rfl: "
			showuserptr $kgm_ss64.isf.rflags
			printf "  cr2: "
			showuserptr $kgm_ss64.cr2
			printf "\n"
		else
			printf "X86 Thread State (32-bit):\n"
	   		set $kgm_ss32 = $newiss.uss.ss_32

			printf "  eax: "
			showuserptr $kgm_ss32.eax
			printf "  ebx: "
			showuserptr $kgm_ss32.ebx
			printf "  ecx: "
			showuserptr $kgm_ss32.ecx
			printf "  edx: "
			showuserptr $kgm_ss32.edx
			printf "\n"

			printf "  edi: "
			showuserptr $kgm_ss32.edi
			printf "  esi: "
			showuserptr $kgm_ss32.esi
			printf "  ebp: "
			showuserptr $kgm_ss32.ebp
			printf "  esp: "
			showuserptr $kgm_ss32.uesp
			printf "\n"

			printf "   ss: "
			showuserptr $kgm_ss32.ss
			printf "  efl: "
			showuserptr $kgm_ss32.efl
			printf "  eip: "
			showuserptr $kgm_ss32.eip
			printf "   cs: "
			showuserptr $kgm_ss32.cs
			printf "\n"

			printf "   ds: "
			showuserptr $kgm_ss32.ds
			printf "   es: "
			showuserptr $kgm_ss32.es
			printf "   fs: "
			showuserptr $kgm_ss32.fs
			printf "   gs: "
			showuserptr $kgm_ss32.gs
			printf "\n"

			printf "  cr2: "
			showuserptr $kgm_ss32.cr2
			printf "\n"
		end
	else
	if ($kgm_mtype == $kgm_mtype_arm)
		printf "ARM Thread State:\n"
		set $kgm_pcb = (arm_saved_state_t *) ($kgm_threadp->machine.upcb)

		printf "    r0: "
		showuserptr $kgm_pcb.r[0]
		printf "    r1: "
		showuserptr $kgm_pcb.r[1]
		printf "    r2: "
		showuserptr $kgm_pcb.r[2]
		printf "    r3: "
		showuserptr $kgm_pcb.r[3]
		printf "\n"

		printf "    r4: "
		showuserptr $kgm_pcb.r[4]
		printf "    r5: "
		showuserptr $kgm_pcb.r[5]
		printf "    r6: "
		showuserptr $kgm_pcb.r[6]
		printf "    r7: "
		showuserptr $kgm_pcb.r[7]
		printf "\n"

		printf "    r8: "
		showuserptr $kgm_pcb.r[8]
		printf "    r9: "
		showuserptr $kgm_pcb.r[9]
		printf "   r10: "
		showuserptr $kgm_pcb.r[10]
		printf "   r11: "
		showuserptr $kgm_pcb.r[11]
		printf "\n"

		printf "    ip: "
		showuserptr $kgm_pcb.r[12]
		printf "    sp: "
		showuserptr $kgm_pcb.sp
		printf "    lr: "
		showuserptr $kgm_pcb.lr
		printf "    pc: "
		showuserptr $kgm_pcb.pc
		printf "\n"

		printf "  cpsr: "
		showuserptr $kgm_pcb.cpsr
		printf "\n"
	else
		echo showuserregisters not supported on this architecture\n
	end
	end
end
document showuserregisters
Syntax: showuserstack <address of thread>
|This command displays the last known user register state
|for the thread. This map not be correct for cases where
|the thread is currently executing in userspace. However
|for threads that have entered the kernel (either explicitly
|with a system call or implicitly with a fault), it should
|be accurate
end

define showtaskuserregisters
    set $kgm_taskp = (struct task *)$arg0
    set $kgm_head_actp = &($kgm_taskp->threads)
    set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
    while $kgm_actp != $kgm_head_actp
    	printf "For thread "
	showptr $kgm_actp
	printf "\n"
	showuserregisters $kgm_actp
    	set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
    	printf "\n"
    end
end
document showtaskuserregisters
Syntax: (gdb) showtaskuserregisters <task>
| Print out the user registers for each thread in a task
end

define kdp-reboot
# Alternatively, set *(*(unsigned **) 0x2498) = 1 
# (or 0x5498 on PPC, 0xffffff8000002928 on x86_64, 0xffff049c on arm)
       manualhdrint $kgm_kdp_pkt_hostreboot
       continue
end

document kdp-reboot
Syntax: kdp-reboot
|Reboot the remote target machine; not guaranteed to succeed. 
end

define kdpversionint
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_version_req_t)
       set $kgm_pkt = (kdp_version_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request  = KDP_VERSION
       set $kgm_pkt->hdr.len      = sizeof(kdp_version_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq      = 0
       set $kgm_pkt->hdr.key      = 0
       set manual_pkt.input       = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_version_reply_t *)&manual_pkt.data
       set $kgm_kdp_version = $kgm_pkt->version
       set $kgm_kdp_feature = $kgm_pkt->feature
end

define kdp-version
       kdpversionint
       printf "KDP VERSION = %d, FEATURE = 0x%x\n", $kgm_kdp_version, $kgm_kdp_feature
end

document kdp-version
Syntax: kdp-version
|Get the KDP protocol version being used by the kernel.
end

define dumpinfoint
       # set up the manual KDP packet
       set manual_pkt.input        = 0

       set manual_pkt.len          = sizeof(kdp_dumpinfo_req_t)
       set $kgm_pkt                = (kdp_dumpinfo_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request  = KDP_DUMPINFO
       set $kgm_pkt->hdr.len      = sizeof(kdp_dumpinfo_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq      = 0
       set $kgm_pkt->hdr.key      = 0
       set $kgm_pkt->type         = $arg0 
       set $kgm_pkt->name         = ""
       set $kgm_pkt->destip       = ""
       set $kgm_pkt->routerip     = ""
       set $kgm_pkt->port         = 0

       if $argc > 1
       	  set $kgm_pkt->name      = "$arg1"
       end
       if $argc > 2
          set $kgm_pkt->destip    = "$arg2"
       end
       if $argc > 3
       	  set $kgm_pkt->routerip  = "$arg3"
       end
       if $argc > 4
       	  set $kgm_pkt->port      = $arg4
       end

       set manual_pkt.input       = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
end

define sendcore
       if $argc > 1
       	  dumpinfoint KDP_DUMPINFO_CORE $arg1 $arg0
       else
       	  dumpinfoint KDP_DUMPINFO_CORE \0 $arg0
       end
end

document sendcore
Syntax: sendcore <IP address> [filename]
|Configure the kernel to transmit a kernel coredump to a server (kdumpd) 
|at the specified IP address. This is useful when the remote target has
|not been previously configured to transmit coredumps, and you wish to
|preserve kernel state for later examination. NOTE: You must issue a "continue"
|command after using this macro to trigger the kernel coredump. The kernel
|will resume waiting in the debugger after completion of the coredump. You
|may disable coredumps by executing the "disablecore" macro. You can 
|optionally specify the filename to be used for the generated core file.
end

define sendsyslog
       if $argc > 1
       	  dumpinfoint KDP_DUMPINFO_SYSTEMLOG $arg1 $arg0
       else
       	  dumpinfoint KDP_DUMPINFO_SYSTEMLOG \0 $arg0
       end
end

document sendsyslog
Syntax: sendsyslog <IP address> [filename]
|Configure the kernel to transmit a kernel system log to a server (kdumpd) 
|at the specified IP address. NOTE: You must issue a "continue"
|command after using this macro to trigger the kernel system log. The kernel
|will resume waiting in the debugger after completion. You can optionally
|specify the name to be used for the generated system log.
end

define sendpaniclog
       if panicstr 
	  if $argc > 1
	     dumpinfoint KDP_DUMPINFO_PANICLOG $arg1 $arg0
	  else
	     dumpinfoint KDP_DUMPINFO_PANICLOG \0 $arg0
	  end
       else
	  printf "No panic log available.\n"
       end
end

document sendpaniclog
Syntax: sendpaniclog <IP address> [filename]
|Configure the kernel to transmit a kernel paniclog to a server (kdumpd) 
|at the specified IP address. NOTE: You must issue a "continue"
|command after using this macro to trigger the kernel panic log. The kernel
|will resume waiting in the debugger after completion. You can optionally
|specify the name to be used for the generated panic log.
end

define getdumpinfo
       dumpinfoint KDP_DUMPINFO_GETINFO
       set $kgm_dumpinfo = (kdp_dumpinfo_reply_t *) manual_pkt.data
       if $kgm_dumpinfo->type & KDP_DUMPINFO_REBOOT
       	  printf "Sysem will reboot after kernel info gets dumped.\n"
       else
       	  printf "Sysem will not reboot after kernel info gets dumped.\n"
       end
       if $kgm_dumpinfo->type & KDP_DUMPINFO_NORESUME
       	  printf "System will allow a re-attach after a KDP disconnect.\n"
       else
       	  printf "System will resume after a KDP disconnect.\n"
       end
       set $kgm_dumpinfo_type = $kgm_dumpinfo->type & KDP_DUMPINFO_MASK
       if $kgm_dumpinfo_type == KDP_DUMPINFO_DISABLE
       	  printf "Kernel not setup for remote dumps.\n"
       else
          printf "Remote dump type: "
          if $kgm_dumpinfo_type == KDP_DUMPINFO_CORE
	     printf "Core file\n"
	  end
          if $kgm_dumpinfo_type == KDP_DUMPINFO_PANICLOG
	     printf "Panic log\n"
	  end
          if $kgm_dumpinfo_type == KDP_DUMPINFO_SYSTEMLOG
	     printf "System log\n"
	  end

	  printf "Name: "
	  if $kgm_dumpinfo->name[0] == '\0'
	     printf "(autogenerated)\n"
          else
	     printf "%s\n", $kgm_dumpinfo->name
          end		  

	  printf "Network Info: %s[%d] ", $kgm_dumpinfo->destip, $kgm_dumpinfo->port
	  if $kgm_dumpinfo->routerip[0] == '\0'
	     printf "\n"
	  else
	     printf "Router: %s\n", $kgm_dumpinfo->routerip
	  end
       end
end

document getdumpinfo
Syntax: getdumpinfo
|Retrieve the current remote dump settings. 
end

define setdumpinfo
       dumpinfoint KDP_DUMPINFO_SETINFO $arg0 $arg1 $arg2 $arg3
end

document setdumpinfo
Syntax: setdumpinfo <filename> <ip> <router> <port>
|Configure the current remote dump settings. Specify \0 if you
|want to use the defaults (filename) or previously configured
|settings (ip/router). Specify 0 for the port if you wish to 
|use the previously configured/default setting for that.
end

define disablecore
       dumpinfoint KDP_DUMPINFO_DISABLE
end

document disablecore
Syntax: disablecore
|Reconfigures the kernel so that it no longer transmits kernel coredumps. This
|complements the "sendcore" macro, but it may be used if the kernel has been
|configured to transmit coredumps through boot-args as well.
end

define switchtocorethread
	set $newact = (struct thread *) $arg0
	select 0
	if ($newact->kernel_stack == 0)
	   echo This thread does not have a stack.\n
	   echo continuation:
	   output/a (unsigned) $newact.continuation
	   echo \n
	else
	if ($kgm_mtype == $kgm_mtype_ppc)
	   loadcontext $newact->machine->pcb
	   flushstack
	   set $pc = $newact->machine->pcb.save_srr0
	else
	if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
		set $kgm_cstatep = (struct x86_kernel_state *) \
					($newact->kernel_stack + kernel_stack_size \
					 - sizeof(struct x86_kernel_state))
		loadcontext $kgm_cstatep
		flushstack
	else
		echo switchtocorethread not supported on this architecture\n
	end
 	end
	showcontext_int
	end
end

document switchtocorethread
Syntax: switchtocorethread <address of activation>
| The corefile equivalent of "switchtoact". When debugging a kernel coredump
| file, this command can be used to examine the execution context and stack
| trace for a given thread activation. For example, to view the backtrace
| for a thread issue "switchtocorethread <address>", followed by "bt".
| Before resuming execution, issue a "resetcorectx" command, to
| return to the original execution context. Note that this command
| requires gdb support, as documented in Radar 3401283.
end

define loadcontext
	select 0
	if ($kgm_mtype == $kgm_mtype_ppc)
	set $kgm_contextp = (struct savearea *) $arg0
	set $pc = $kgm_contextp.save_srr0
	set $r1 = $kgm_contextp.save_r1
	set $lr = $kgm_contextp.save_lr

	set $r2 = $kgm_contextp.save_r2
	set $r3 = $kgm_contextp.save_r3
	set $r4 = $kgm_contextp.save_r4
	set $r5 = $kgm_contextp.save_r5
	set $r6 = $kgm_contextp.save_r6
	set $r7 = $kgm_contextp.save_r7
	set $r8 = $kgm_contextp.save_r8
	set $r9 = $kgm_contextp.save_r9
	set $r10 = $kgm_contextp.save_r10
	set $r11 = $kgm_contextp.save_r11
	set $r12 = $kgm_contextp.save_r12
	set $r13 = $kgm_contextp.save_r13
	set $r14 = $kgm_contextp.save_r14
	set $r15 = $kgm_contextp.save_r15
	set $r16 = $kgm_contextp.save_r16
	set $r17 = $kgm_contextp.save_r17
	set $r18 = $kgm_contextp.save_r18
	set $r19 = $kgm_contextp.save_r19
	set $r20 = $kgm_contextp.save_r20
	set $r21 = $kgm_contextp.save_r21
	set $r22 = $kgm_contextp.save_r22
	set $r23 = $kgm_contextp.save_r23
	set $r24 = $kgm_contextp.save_r24
	set $r25 = $kgm_contextp.save_r25
	set $r26 = $kgm_contextp.save_r26
	set $r27 = $kgm_contextp.save_r27
	set $r28 = $kgm_contextp.save_r28
	set $r29 = $kgm_contextp.save_r29
	set $r30 = $kgm_contextp.save_r30
	set $r31 = $kgm_contextp.save_r31

	set $cr = $kgm_contextp.save_cr
	set $ctr = $kgm_contextp.save_ctr
       else
	if ($kgm_mtype == $kgm_mtype_i386)
		set $kgm_contextp = (struct x86_kernel_state *) $arg0
		set $ebx = $kgm_contextp->k_ebx 
		set $ebp = $kgm_contextp->k_ebp 
		set $edi = $kgm_contextp->k_edi 
		set $esi = $kgm_contextp->k_esi 
		set $eip = $kgm_contextp->k_eip 
		set $pc =  $kgm_contextp->k_eip
	else
	if ($kgm_mtype == $kgm_mtype_x86_64)
		set $kgm_contextp = (struct x86_kernel_state *) $arg0
		set $rbx = $kgm_contextp->k_rbx 
		set $rbp = $kgm_contextp->k_rbp 
		set $r12 = $kgm_contextp->k_r12 
		set $r13 = $kgm_contextp->k_r13 
		set $r14 = $kgm_contextp->k_r14 
		set $r15 = $kgm_contextp->k_r15 
		set $rip = $kgm_contextp->k_rip
		set $pc = $kgm_contextp->k_rip
	else
		echo loadcontext not supported on this architecture\n
	end
	end
	end
end

define resetcorectx
	select 0
	if ($kgm_mtype == $kgm_mtype_ppc)
		set $kgm_corecontext = (struct savearea *) kdp.saved_state
		loadcontext $kgm_corecontext
	else
	if ($kgm_mtype == $kgm_mtype_i386)
		set $kdpstatep = (struct x86_saved_state32 *) kdp.saved_state
		set $ebx = $kdpstatep->ebx
		set $ebp = $kdpstatep->ebp
		set $edi = $kdpstatep->edi
		set $esi = $kdpstatep->esi
		set $eip = $kdpstatep->eip
		set $eax = $kdpstatep->eax
		set $ecx = $kdpstatep->ecx
		set $edx = $kdpstatep->edx
		flushregs
		flushstack
		set $pc = $kdpstatep->eip
		update
	else
		echo resetcorectx not supported on this architecture\n
	end
	end
	showcontext_int
end

document resetcorectx
Syntax: resetcorectx
| The corefile equivalent of "resetctx". Returns to the original
| execution context (that of the active thread at the time of the NMI or
| panic). This command should be issued if you wish to resume
| execution after using the "switchtocorethread" command.
end

#Helper function for "showallgdbstacks"

define showgdbthread
	printf "            0x%08x  ", $arg0
	set $kgm_thread = *(struct thread *)$arg0
	printf "0x%08x  ", $arg0
	printf "%3d  ", $kgm_thread.sched_pri
	set $kgm_state = $kgm_thread.state
	if $kgm_state & 0x80
	    printf "I" 
	end
	if $kgm_state & 0x40
	    printf "P" 
	end
	if $kgm_state & 0x20
	    printf "A" 
	end
	if $kgm_state & 0x10
	    printf "H" 
	end
	if $kgm_state & 0x08
	    printf "U" 
	end
	if $kgm_state & 0x04
	    printf "R" 
	end
	if $kgm_state & 0x02
	    printf "S" 
	end
   	if $kgm_state & 0x01
	    printf "W\t" 
	    printf "0x%08x  ", $kgm_thread.wait_queue
            output /a (unsigned) $kgm_thread.wait_event
		if ($kgm_thread.uthread != 0)
			set $kgm_uthread = (struct uthread *)$kgm_thread.uthread
			if ($kgm_uthread->uu_wmesg != 0)
				printf " \"%s\"", $kgm_uthread->uu_wmesg
			end
	    end
	end
	if $arg1 != 0
	    if ($kgm_thread.kernel_stack != 0)
		if ($kgm_thread.reserved_stack != 0)
			printf "\n\t\treserved_stack=0x%08x", $kgm_thread.reserved_stack
		end
		printf "\n\t\tkernel_stack=0x%08x", $kgm_thread.kernel_stack
		if ($kgm_mtype == $kgm_mtype_ppc)
			set $mysp = $kgm_thread.machine.pcb->save_r1
		end
		if ($kgm_mtype == $kgm_mtype_i386)
			set $kgm_statep = (struct x86_kernel_state *) \
				($kgm_thread->kernel_stack + kernel_stack_size \
				 - sizeof(struct x86_kernel_state))
			set $mysp = $kgm_statep->k_ebp
		end
		if ($kgm_mtype == $kgm_mtype_arm)
			if (((unsigned long)$r7 < ((unsigned long) ($kgm_thread->kernel_stack+kernel_stack_size))) \
                      && ((unsigned long)$r7 > (unsigned long) ($kgm_thread->kernel_stack)))
				set $mysp = $r7
			else
                        	set $kgm_statep = (struct arm_saved_state *)$kgm_thread.machine.kstackptr
                        	set $mysp = $kgm_statep->r[7]
			end
		end
		set $prevsp = 0
		printf "\n\t\tstacktop=0x%08x", $mysp
		if ($arg2 == 0)
			switchtoact $arg0
		else
			switchtocorethread $arg0
		end
	    	bt
	    else
		printf "\n\t\t\tcontinuation="
		output /a (unsigned) $kgm_thread.continuation
	    end
	    printf "\n"
	else
	    printf "\n"
	end
end	    

#Use of this macro is currently (8/04) blocked by the fact that gdb
#stops evaluating macros when encountering an error, such as a failure
#to read memory from a certain location. Until this issue (described in
#3758949) is addressed, evaluation of this macro may stop upon
#encountering such an error.

define showallgdbstacks
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
        showtaskheader
	showtaskint $kgm_taskp
	set $kgm_head_actp = &($kgm_taskp->threads)
        set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
	while $kgm_actp != $kgm_head_actp
	    showactheader
	    showgdbthread $kgm_actp 1 0
  	    set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
        end
	printf "\n"
    	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
    resetctx
end

document showallgdbstacks
Syntax: showallgdbstacks
| An alternative to "showallstacks". Iterates through the task list and
| displays a gdb generated backtrace for each kernel thread. It is
| advantageous in that it is much faster than "showallstacks", and
| decodes function call arguments and displays source level traces, but
| it has the drawback that it doesn't determine if frames belong to
| functions from kernel extensions, as with "showallstacks".
| This command may terminate prematurely because of a gdb bug
| (Radar 3758949), which stops macro evaluation on memory read
| errors.
end

define showallgdbcorestacks
	select 0
	set $kgm_head_taskp = &tasks
	set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
        while $kgm_taskp != $kgm_head_taskp
		showtaskheader
		showtaskint $kgm_taskp
		set $kgm_head_actp = &($kgm_taskp->threads)
		set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
		while $kgm_actp != $kgm_head_actp
		showactheader
		showgdbthread $kgm_actp 1 1
		set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
		end
		printf "\n"
		set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
	end
	resetcorectx
end


document showallgdbcorestacks
Syntax: showallgdbcorestacks
|Corefile version of "showallgdbstacks"
end


define switchtouserthread
		select 0
  		if ($kgm_mtype == $kgm_mtype_ppc)	
		   if ($kdp_act_counter == 0)
		      set $kdpstate = (struct savearea *) kdp.saved_state
		   end
		   set $kdp_act_counter = $kdp_act_counter + 1
		   set $newact = (struct thread *) $arg0
		   _kgm_flush_loop
		   set $checkpc = $newact->machine->upcb.save_srr0
		   if ($checkpc == 0)
		      echo This activation does not appear to have
		      echo \20 a valid user context.\n
		   else	      
		     set (struct savearea *) kdp.saved_state=$newact->machine->upcb
		     set $pc = $checkpc
#flush and update seem to be executed lazily by gdb on Tiger, hence the
#repeated invocations - see 3743135
	   	    _kgm_flush_loop
# This works because the new pmap is used only for reads
		     set kdp_pmap = $newact->task->map->pmap
		     _kgm_flush_loop
		     _kgm_update_loop
		   end
		else
		   echo switchtouserthread not implemented for this architecture.\n
	end
end

document switchtouserthread
Syntax: switchtouserthread <address of thread>
| Analogous to switchtoact, but switches to the user context of a
| specified thread address. Similar to the "showuserstack"
| command, but this command does not return gdb to the kernel context
| immediately. This is to assist with the following (rather risky)
| manoeuvre - upon switching to the user context and virtual address
| space, the user may choose to call remove-symbol-file on the
| mach_kernel symbol file, and then add-symbol-file on the user space
| binary's symfile. gdb can then generate symbolic backtraces 
| for the user space thread. To return to the
| kernel context and virtual address space, the process must be
| reversed, i.e. call remove-symbol-file on the user space symbols, and
| then add-symbol-file on the appropriate mach_kernel, and issue the
| "resetstacks" command. Note that gdb may not react kindly to all these
| symbol file switches. The same restrictions that apply to "showuserstack"
| apply here - pages that have been paged out cannot be read while in the
| debugger context, so backtraces may terminate early.
| If the virtual addresses in the stack trace do not conflict with those
| of symbols in the kernel's address space, it may be sufficient to
| just do an add-symbol-file on the user space binary's symbol file.
| Note that while this command works on Panther's gdb, an issue
| with Tiger gdb (3743135) appears to hamper the evaluation of this
| macro in some cases.
end

define showmetaclass
    set $kgm_metaclassp = (OSMetaClass *)$arg0
    printf "%-5d", $kgm_metaclassp->instanceCount
    printf "x %5d bytes", $kgm_metaclassp->classSize
    printf " %s\n", $kgm_metaclassp->className->string
end

define showstring
    printf "\"%s\"", ((OSString *)$arg0)->string
end

define shownumber
    printf "%lld", ((OSNumber *)$arg0)->value
end

define showboolean
    if ($arg0 == gOSBooleanFalse)
	printf "No"
    else
	printf "Yes"
    end
end

define showdatabytes
    set $kgm_data = (OSData *)$arg0

    printf "<"
    set $kgm_datap = (const unsigned char *) $kgm_data->data
	set $kgm_idx = 0
	while ( $kgm_idx < $kgm_data->length )
		printf "%02X", *$kgm_datap
		set $kgm_datap = $kgm_datap + 1
		set $kgm_idx = $kgm_idx + 1
	end
	printf ">\n"
end

define showdata
    set $kgm_data = (OSData *)$arg0

    printf "<"
    set $kgm_datap = (const unsigned char *) $kgm_data->data

    set $kgm_printstr = 0
    if (0 == (3 & (unsigned int)$kgm_datap) && ($kgm_data->length >= 3))
	set $kgm_bytes = *(unsigned int *) $kgm_datap
	if (0xffff0000 & $kgm_bytes)
	    set $kgm_idx = 0
	    set $kgm_printstr = 1
	    while ($kgm_idx++ < 4)
		set $kgm_bytes = $kgm_bytes >> 8
		set $kgm_char = 0xff & $kgm_bytes
		if ($kgm_char && (($kgm_char < 0x20) || ($kgm_char > 0x7e)))
		    set $kgm_printstr = 0
		end
	    end
	end
    end
    
    set $kgm_idx = 0
    if ($kgm_printstr)
	set $kgm_quoted = 0
	while ($kgm_idx < $kgm_data->length)
	    set $kgm_char = $kgm_datap[$kgm_idx++]
	    if ($kgm_char)
		if (0 == $kgm_quoted)
		    set $kgm_quoted = 1
		    if ($kgm_idx > 1)
			printf ",\""
		    else
			printf "\""
		    end
		end
		printf "%c", $kgm_char
	    else
		if ($kgm_quoted)
		    set $kgm_quoted = 0
		    printf "\""
		end
	    end
	end
	if ($kgm_quoted)
	    printf "\""
	end
    else
	if (0 == (3 & (unsigned int)$kgm_datap))
	    while (($kgm_idx + 3) <= $kgm_data->length)
		printf "%08x", *(unsigned int *) &$kgm_datap[$kgm_idx]
		set $kgm_idx = $kgm_idx + 4
	    end
	end
	while ($kgm_idx < $kgm_data->length)
	    printf "%02x", $kgm_datap[$kgm_idx++]
	end
    end
    printf ">"
end

define showdictionaryint
    set $kgm$arg0_dict = (OSDictionary *)$arg1

    printf "{"
    set $kgm$arg0_idx = 0
    while ($kgm$arg0_idx < $kgm$arg0_dict->count)
	set $kgm_obj = $kgm$arg0_dict->dictionary[$kgm$arg0_idx].key
	showobjectint _$arg0 $kgm_obj
	printf "="
	set $kgm_obj = $kgm$arg0_dict->dictionary[$kgm$arg0_idx++].value
	showobjectint _$arg0 $kgm_obj
	if ($kgm$arg0_idx < $kgm$arg0_dict->count)
	    printf ","
	end
    end
    printf "}"
end

define indent
    set $kgm_idx = 0
    while ($kgm_idx < $arg0)
	if ($arg1 & (1 << $kgm_idx++))
	    printf "| "
	else
	    printf "  "
	end
    end
end

define showregdictionary
    indent $kgm_reg_depth+2 $arg1
    printf "{\n"

    set $kgm_reg_idx = 0
    while ($kgm_reg_idx < $arg0->count)
	indent $kgm_reg_depth+2 $arg1
	printf "  "
	set $kgm_obj = $arg0->dictionary[$kgm_reg_idx].key
	showobjectint _ $kgm_obj
	printf " = "

	set $kgm_obj = $arg0->dictionary[$kgm_reg_idx++].value
	showobjectint _ $kgm_obj
	printf "\n"
    end
    indent $kgm_reg_depth+2 $arg1
    printf "}\n"
end


define showorderedsetarrayint
    set $kgm$arg0_array = (_Element *)$arg1
    set $kgm$arg0_count = $arg2

    set $kgm$arg0_idx = 0
    while ($kgm$arg0_idx < $kgm$arg0_count)
        set $kgm_obj = $kgm$arg0_array[$kgm$arg0_idx++]
        showobjectint _$arg0 $kgm_obj
        if ($kgm$arg0_idx < $kgm$arg0_count)
	    printf ","
        end
    end
end

define showorderedsetint
    set $kgm_array = ((OSOrderedSet *)$arg1)->array
    set $count = ((OSOrderedSet *)$arg1)->count
    printf "["
    showorderedsetarrayint $arg0 $kgm_array $count
    printf "]"
end

define showarraysetint
    set $kgm$arg0_array = (OSArray *)$arg1

    set $kgm$arg0_idx = 0
    while ($kgm$arg0_idx < $kgm$arg0_array->count)
	set $kgm_obj = $kgm$arg0_array->array[$kgm$arg0_idx++]
	showobjectint _$arg0 $kgm_obj
	if ($kgm$arg0_idx < $kgm$arg0_array->count)
	    printf ","
	end
    end
end

define showarrayint
    printf "("
    showarraysetint $arg0 $arg1
    printf ")"
end

define showsetint
    set $kgm_array = ((OSSet *)$arg1)->members
    printf "["
    showarraysetint $arg0 $kgm_array
    printf "]"
end


define showobjectint
    set $kgm_obj = (OSObject *) $arg1
    set $kgm_vt = *((void **) $arg1)

    if ($kgm_lp64 || $kgm_mtype == $kgm_mtype_arm)
        set $kgm_vt = $kgm_vt - 2 * sizeof(void *)
    end

    if ($kgm_show_object_addrs)
        printf "`object "
        showptr $arg1
        printf ", vt "
        output /a (unsigned long) $kgm_vt
        if ($kgm_show_object_retain)
            printf ", retain count %d, container retain %d", (0xffff & $kgm_obj->retainCount), $kgm_obj->retainCount >> 16
        end
        printf "` "
    end

    # No multiple-inheritance
    set $kgm_shown = 0
    if ($kgm_vt == &_ZTV8OSString)
        showstring $arg1
        set $kgm_shown = 1
    end
	if ($kgm_vt == &_ZTV8OSSymbol)
	    showstring $arg1
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV8OSNumber)
		shownumber $arg1
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV6OSData)
        if $kgm_show_data_alwaysbytes == 1
            showdatabytes $arg1
        else
            showdata $arg1
        end
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV9OSBoolean)
        showboolean $arg1
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV12OSDictionary)
        showdictionaryint _$arg0 $arg1
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV7OSArray)
        showarrayint _$arg0 $arg1
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV5OSSet)
        showsetint _$arg0 $arg1
        set $kgm_shown = 1
    end
    if ($kgm_vt == &_ZTV12OSOrderedSet)
        showorderedsetint _$arg0 $arg1
        set $kgm_shown = 1
    end
    
    if ($kgm_shown != 1)
        if ($kgm_show_object_addrs == 0)
            printf "`object "
            showptr $arg1
            printf ", vt "
            output /a (unsigned long) $kgm_vt
            printf "`"
        end
    end
end

define showobject
    set $kgm_save = $kgm_show_object_addrs
    set $kgm_show_object_addrs = 1
    set $kgm_show_object_retain = 1
    showobjectint _ $arg0
    set $kgm_show_object_addrs = $kgm_save
    set $kgm_show_object_retain = 0
    printf "\n"
end
document showobject
Syntax: (gdb) showobject <object address>
| Show info about an OSObject - its vtable ptr and retain count.
| If the object is a simple container class, more info will be shown.
end

define dictget 
    set $kgm_dictp = (OSDictionary *)$arg0
    set $kgm_keyp = (const OSSymbol *)$arg1
    set $kgm_idx = 0
    set $kgm_result = 0
    while (($kgm_idx < $kgm_dictp->count) && ($kgm_result == 0))
	if ($kgm_keyp == $kgm_dictp->dictionary[$kgm_idx].key)
	    set $kgm_result = $kgm_dictp->dictionary[$kgm_idx].value
	end
	set $kgm_idx = $kgm_idx + 1
    end
end


define _registryentryrecurseinit
    set $kgm_re         = (IOService *)$arg1
    set $kgm$arg0_stack = (unsigned long long) $arg2

    if ($arg3)
	set $kgm$arg0_stack = $kgm$arg0_stack | (1ULL << $kgm_reg_depth)
    else
	set $kgm$arg0_stack = $kgm$arg0_stack & ~(1ULL << $kgm_reg_depth)
    end

    dictget $kgm_re->fRegistryTable $kgm_childkey
    set $kgm$arg0_child_array = (OSArray *) $kgm_result

    if ($kgm$arg0_child_array)
	set $kgm$arg0_child_count = $kgm$arg0_child_array->count
    else
	set $kgm$arg0_child_count = 0
    end

    if ($kgm$arg0_child_count)
	set $kgm$arg0_stack = $kgm$arg0_stack | (2ULL << $kgm_reg_depth)
    else
	set $kgm$arg0_stack = $kgm$arg0_stack & ~(2ULL << $kgm_reg_depth)
    end
end

define findregistryentryrecurse
    set $kgm_registry_entry = 0
    _registryentryrecurseinit $arg0 $arg1 $arg2 $arg3

    dictget $kgm_re->fRegistryTable $kgm_namekey
    if ($kgm_result == 0)
	dictget $kgm_re->fRegistryTable gIONameKey
    end
    if ($kgm_result == 0)
	dictget $kgm_re->fPropertyTable gIOClassKey
    end

    if ($kgm_result != 0)
       set $str = ((OSString *) $kgm_result)->string
       strcmp_nomalloc $str $kgm_reg_find_str0 $kgm_reg_find_str1 $kgm_reg_find_str2 $kgm_reg_find_str3 $kgm_reg_find_str4 $kgm_reg_find_str5 $kgm_reg_find_str6 $kgm_reg_find_str7 $kgm_reg_find_str8 
       if $kgm_findregistry_verbose
          echo .
       end

       if $kgm_strcmp_result == 0
	  if $kgm_findregistry_verbose
	     printf "\n%s:\n | ", ((OSString *) $kgm_result)->string
	     showobject $kgm_re
	     printf " | "
	     print $kgm_re
	  end

	  # don't populate $kgm_registry_entry if we want to show everything
	  if !$kgm_findregistry_continue
       	     set $kgm_registry_entry = $kgm_re
	  end
       end
    end

    # recurse
    if (!$kgm_registry_entry && ($kgm$arg0_child_count != 0))
        set $kgm_reg_depth = $kgm_reg_depth + 1
        set $kgm$arg0_child_idx = 0

        while ($kgm$arg0_child_idx < $kgm$arg0_child_count)
            set $kgm_re = $kgm$arg0_child_array->array[$kgm$arg0_child_idx++]
            set $kgm_more_sib = ($kgm$arg0_child_idx < $kgm$arg0_child_count)
	    if $kgm_reg_depth >= $kgm_reg_depth_max + 1
	       loop_break
	    end
            findregistryentryrecurse _$arg0 $kgm_re $kgm$arg0_stack $kgm_more_sib 
	    if $kgm_registry_entry
	       loop_break
	    end
        end
        set $kgm_reg_depth = $kgm_reg_depth - 1
    end
end

define findregdictvalue
    set $kgm_registry_value = 0
    set $kgm_reg_idx = 0
    while ($kgm_reg_idx < $arg0->count)
        set $kgm_obj = $arg0->dictionary + $kgm_reg_idx
	set $str = ((OSString *)$kgm_obj->key)->string
       	strcmp_nomalloc $str $kgm_reg_find_str0 $kgm_reg_find_str1 $kgm_reg_find_str2 $kgm_reg_find_str3 $kgm_reg_find_str4 $kgm_reg_find_str5 $kgm_reg_find_str6 $kgm_reg_find_str7 $kgm_reg_find_str8 

	if $kgm_strcmp_result == 0
	   set $kgm_registry_value = $kgm_obj->value
	   if $kgm_findregistry_verbose
	      showobject $kgm_registry_value
	      print $kgm_registry_value
	   end
	   loop_break
	end
	set $kgm_reg_idx = $kgm_reg_idx + 1
    end
end

define setfindregistrystr
    set $kgm_reg_find_str0 = 0
    set $kgm_reg_find_str1 = 0
    set $kgm_reg_find_str2 = 0
    set $kgm_reg_find_str3 = 0
    set $kgm_reg_find_str4 = 0
    set $kgm_reg_find_str5 = 0
    set $kgm_reg_find_str6 = 0
    set $kgm_reg_find_str7 = 0
    set $kgm_reg_find_str8 = 0

    if $argc > 0
       set $kgm_reg_find_str0 = $arg0
    end
    if $argc > 1
       set $kgm_reg_find_str1 = $arg1
    end
    if $argc > 2
       set $kgm_reg_find_str2 = $arg2
    end
    if $argc > 3
       set $kgm_reg_find_str3 = $arg3
    end
    if $argc > 4
       set $kgm_reg_find_str4 = $arg4
    end
    if $argc > 5
       set $kgm_reg_find_str5 = $arg5
    end
    if $argc > 6
       set $kgm_reg_find_str6 = $arg6
    end
    if $argc > 7
       set $kgm_reg_find_str7 = $arg7
    end
    if $argc > 8
       set $kgm_reg_find_str8 = $arg8
    end
end

document setfindregistrystr
Syntax: (gdb) setfindregistrystr [a] [b] [c] [d] [e] [f] [g] [h] [i]
| Store an encoded string into up to 9 arguments for use by 
| findregistryprop or findregistryentry. The arguments are created
| through calls to strcmp_arg_pack64
end

define _findregistryprop
    set $reg       = (IOService *) $arg0
    set $kgm_props = $reg->fPropertyTable
    set $kgm_findregistry_verbose = 0

    findregdictvalue $kgm_props 
end

define findregistryprop
    set $reg       = (IOService *) $arg0
    set $kgm_props = $reg->fPropertyTable

    set $kgm_findregistry_verbose = 1
    findregdictvalue $kgm_props 
end

document findregistryprop
Syntax: (gdb) findregistryprop <entry>
| Given a registry entry, print out the contents for the property that matches
| the encoded string specified via setfindregistrystr.
|
| For example, the following will print out the "intel-pic" property stored in
| the AppleACPIPlatformExpert registry entry $pe_entry:
|	     strcmp_arg_pack64 'i' 'n' 't' 'e' 'l' '-' 'p' 'i' 
|	     set $intel_pi = $kgm_strcmp_arg
|	     strcmp_arg_pack64 'c' 0 0 0 0 0 0 0
|	     set $c = $kgm_strcmp_arg
|	     setfindregistrystr $intel_pi $c
|	     findregistryprop $pe_entry
end

define findregistryentryint
    if !$kgm_reg_plane
       set $kgm_reg_plane = (IORegistryPlane *) gIOServicePlane
    end

    if !$kgm_reg_plane
       printf "Please load kgmacros after KDP attaching to the target.\n"
    else
       set $kgm_namekey   = (OSSymbol *) $kgm_reg_plane->nameKey
       set $kgm_childkey  = (OSSymbol *) $kgm_reg_plane->keys[1]
       if $kgm_findregistry_verbose
          printf "Searching"
       end
       findregistryentryrecurse _ $arg0 0 0
    end
end

define _findregistryentry
    set $kgm_findregistry_verbose  = 0
    set $kgm_findregistry_continue = 0
    set $kgm_reg_depth             = 0

    findregistryentryint gRegistryRoot
end

define findregistryentry
    set $kgm_findregistry_verbose  = 1
    set $kgm_findregistry_continue = 0
    set $kgm_reg_depth             = 0

    findregistryentryint gRegistryRoot
end

define findregistryentries
    set $kgm_findregistry_verbose  = 1
    set $kgm_findregistry_continue = 1
    set $kgm_reg_depth             = 0

    findregistryentryint gRegistryRoot
end

document findregistryentry
Syntax: (gdb) findregistryentry
| Search for a registry entry that matches the encoded string specified through
| setfindregistrystr. You can alter the search depth through use of 
| $kgm_reg_depth_max.
|
| For example, the following will pull out the AppleACPIPlatformExpert registry
| entry:
|    	  strcmp_arg_pack64 'A' 'p' 'p' 'l' 'e' 'A' 'C' 'P'
|      	  set $AppleACP = $kgm_strcmp_arg
|      	  strcmp_arg_pack64 'I' 'P' 'l' 'a' 't' 'f' 'o' 'r'
|      	  set $IPlatfor = $kgm_strcmp_arg
|      	  strcmp_arg_pack64 'm' 'E' 'x' 'p' 'e' 'r' 't' 0
|      	  set $mExpert = $kgm_strcmp_arg
|	  setfindregistrystr $AppleACP $IPlatfor $mExpert
| 	  findregistryentry
end

document findregistryentries
Syntax: (gdb) findregistryentries
| Search for all registry entries that match the encoded string specified through
| setfindregistrystr. You can alter the search depth through use of 
| $kgm_reg_depth_max. See findregistryentry for an example of how to encode a string.
end


define showregistryentryrecurse
    _registryentryrecurseinit $arg0 $arg1 $arg2 $arg3

    indent $kgm_reg_depth $kgm$arg0_stack
    printf "+-o "

    dictget $kgm_re->fRegistryTable $kgm_namekey
    if ($kgm_result == 0)
	dictget $kgm_re->fRegistryTable gIONameKey
    end
    if ($kgm_result == 0)
	dictget $kgm_re->fPropertyTable gIOClassKey
    end

    if ($kgm_result != 0)
	printf "%s", ((OSString *)$kgm_result)->string
    else
 	if (((IOService*)$kgm_re)->pwrMgt &&  ((IOService*)$kgm_re)->pwrMgt->Name)
 	    printf "%s", ((IOService*)$kgm_re)->pwrMgt->Name
	else
#	    printf ", guessclass "
#	    guessclass $kgm_re
	    printf "??"
	end
    end


    printf "  <object "
    showptr $kgm_re
    printf ", id 0x%llx, ", $kgm_re->IORegistryEntry::reserved->fRegistryEntryID
    printf "vtable "
    set $kgm_vt = (unsigned long) *(void**) $kgm_re
    if ($kgm_lp64 || $kgm_mtype == $kgm_mtype_arm)
        set $kgm_vt = $kgm_vt - 2 * sizeof(void *)
    end
    output /a $kgm_vt

    if ($kgm_vt != &_ZTV15IORegistryEntry)
        printf ", "
        set $kgm_state =  $kgm_re->__state[0]
        # kIOServiceRegisteredState
        if (0 == ($kgm_state & 2))
            printf "!"
        end
        printf "registered, "
        # kIOServiceMatchedState
        if (0 == ($kgm_state & 4))
            printf "!"
        end
        printf "matched, "
        # kIOServiceInactiveState
        if ($kgm_state & 1)
            printf "in"
        end
        printf "active, busy %d, retain count %d", (0xff & $kgm_re->__state[1]), (0xffff & $kgm_re->retainCount)
    end
    printf ">\n"

    if ($kgm_show_props)
        set $kgm_props = $kgm_re->fPropertyTable
        showregdictionary $kgm_props $kgm$arg0_stack
    end

    # recurse
    if ($kgm$arg0_child_count != 0)

        set $kgm_reg_depth = $kgm_reg_depth + 1
        set $kgm$arg0_child_idx = 0

        while ($kgm$arg0_child_idx < $kgm$arg0_child_count)
            set $kgm_re = $kgm$arg0_child_array->array[$kgm$arg0_child_idx++]
            set $kgm_more_sib = ($kgm$arg0_child_idx < $kgm$arg0_child_count)
	    if $kgm_reg_depth >= $kgm_reg_depth_max + 1
	       loop_break
	    end
   	    showregistryentryrecurse _$arg0 $kgm_re $kgm$arg0_stack $kgm_more_sib
        end

        set $kgm_reg_depth = $kgm_reg_depth - 1
    end
end

define showregistryentryint
    if !$kgm_reg_plane
       set $kgm_reg_plane = (IORegistryPlane *) gIOServicePlane
    end

    if !$kgm_reg_plane
       printf "Please load kgmacros after KDP attaching to the target.\n"
    else        
       set $kgm_namekey   = (OSSymbol *) $kgm_reg_plane->nameKey
       set $kgm_childkey  = (OSSymbol *) $kgm_reg_plane->keys[1]
       showregistryentryrecurse _ $arg0 0 0
    end
end

define showregistry
    set $kgm_reg_depth  = 0
    set $kgm_show_props = 0
    showregistryentryint gRegistryRoot
end
document showregistry
Syntax: (gdb) showregistry 
| Show info about all registry entries in the current plane. You can specify the maximum
| display depth with $kgm_reg_depth_max.
end

define showregistryprops
    set $kgm_reg_depth  = 0
    set $kgm_show_props = 1
    showregistryentryint gRegistryRoot
end
document showregistryprops
Syntax: (gdb) showregistryprops 
| Show info about all registry entries in the current plane, and their properties.
| set $kgm_show_object_addrs = 1 and/or set $kgm_show_object_retain = 1 will display
| more verbose information
end

define showregistryentry
    set $kgm_reg_depth  = 0
    set $kgm_show_props = 1
    showregistryentryint $arg0
end
document showregistryentry
Syntax: (gdb) showregistryentry <object address>
| Show info about a registry entry; its properties and descendants in the current plane.
end

define setregistryplane
    if ($arg0 != 0)
        set $kgm_reg_plane = (IORegistryPlane *) $arg0
    else
        showobjectint _ gIORegistryPlanes
        printf "\n"
    end
end
document setregistryplane
Syntax: (gdb) setregistryplane <plane object address>
| Set the plane to be used for the iokit registry macros. An argument of zero will 
| display known planes.
end

define guessclass
    set $kgm_classidx = 0
    set $kgm_lookvt = *((void **) $arg0)
    set $kgm_bestvt = (void *) 0
    set $kgm_bestidx = 0
    
    while $kgm_classidx < sAllClassesDict->count
	set $kgm_meta = (OSMetaClass *) sAllClassesDict->dictionary[$kgm_classidx].value

	set $kgm_vt = *((void **) $kgm_meta)

	if (($kgm_vt > $kgm_bestvt) && ($kgm_vt < $kgm_lookvt))
	    set $kgm_bestvt  = $kgm_vt
	    set $kgm_bestidx = $kgm_classidx
	end
	set $kgm_classidx = $kgm_classidx + 1
    end
    printf "%s", sAllClassesDict->dictionary[$kgm_bestidx].key->string
end

define showallclasses
    set $kgm_classidx = 0
    while $kgm_classidx < sAllClassesDict->count
	set $kgm_meta = (OSMetaClass *) sAllClassesDict->dictionary[$kgm_classidx++].value
	showmetaclass $kgm_meta
    end
end

document showallclasses
Syntax: (gdb) showallclasses
| Show the instance counts and ivar size of all OSObject subclasses. See ioclasscount man page for details.
end

define showioalloc
    printf " Instance allocation = 0x%08lx = %4ld K\n", (int) debug_ivars_size, ((int) debug_ivars_size) / 1024
    printf "Container allocation = 0x%08lx = %4ld K\n", (int) debug_container_malloc_size, ((int) debug_container_malloc_size) / 1024
    printf " IOMalloc allocation = 0x%08lx = %4ld K\n", (int) debug_iomalloc_size, ((int) debug_iomalloc_size) / 1024
    printf " Pageable allocation = 0x%08lx = %4ld K\n", (vm_size_t) debug_iomallocpageable_size, ((vm_size_t) debug_iomallocpageable_size) / 1024
end

document showioalloc
Syntax: (gdb) showioalloc
| Show some accounting of memory allocated by IOKit allocators. See ioalloccount man page for details.
end

define showosobjecttracking
    set $kgm_next = (OSObjectTracking *) gOSObjectTrackList.next
    while $kgm_next != &gOSObjectTrackList
	set $obj = (OSObject *) ($kgm_next+1)
	showobject $obj
	set $kgm_idx = 0
	while $kgm_idx < (sizeof($kgm_next->bt) / sizeof($kgm_next->bt[0]))
	    if ((unsigned long) $kgm_next->bt[$kgm_idx] > (unsigned long) &last_kernel_symbol)
		showkmodaddr $kgm_next->bt[$kgm_idx]
		printf "\n"
	    else
		if ((unsigned long) $kgm_next->bt[$kgm_idx] > 0)
		    output /a $kgm_next->bt[$kgm_idx]
		    printf "\n"
		end
	    end
	    set $kgm_idx = $kgm_idx + 1
	end
	printf "\n"
	set $kgm_next = (OSObjectTracking *) $kgm_next->link.next
    end
end

document showosobjecttracking
Syntax: (gdb) showosobjecttracking
| Show the list of tracked OSObject allocations with backtraces.
| Boot with the kOSTraceObjectAlloc (0x00400000) io debug flag set. 
| Set gOSObjectTrackThread to 1 or a thread_t to capture new OSObjects allocated by a thread or all threads.
end

# $kgm_readphys_force_kdp and $kgm_readphys_force_physmap
# can respectively cause physical memory access to use
# a KDP manual packet or the physical memory mapping
# even if the default behavior would be otherwise.
define readphysint
    set $kgm_readphysint_result = 0xBAD10AD

    if ($kgm_readphys_force_kdp != 0)
        set $kgm_readphys_use_kdp = 1
    else
        if ($kgm_readphys_force_physmap)
            set $kgm_readphys_use_kdp = 0
        else
            set $kgm_readphys_use_kdp = ( kdp->is_conn > 0 )
        end
    end

    if ($kgm_readphys_use_kdp)

        # set up the manual KDP packet
        set manual_pkt.input = 0
        set manual_pkt.len = sizeof(kdp_readphysmem64_req_t)
        set $kgm_pkt = (kdp_readphysmem64_req_t *)&manual_pkt.data
        set $kgm_pkt->hdr.request  = KDP_READPHYSMEM64
        set $kgm_pkt->hdr.len      = sizeof(kdp_readphysmem64_req_t)
        set $kgm_pkt->hdr.is_reply = 0
        set $kgm_pkt->hdr.seq      = 0
        set $kgm_pkt->hdr.key      = 0
        set $kgm_pkt->address      = (uint64_t)$arg0
        set $kgm_pkt->nbytes       = $arg1 >> 3
        set $kgm_pkt->lcpu         = $arg2
        set manual_pkt.input       = 1
        # dummy to make sure manual packet is executed
        set $kgm_dummy = &_mh_execute_header
        set $kgm_pkt = (kdp_readphysmem64_reply_t *)&manual_pkt.data
        if ($kgm_pkt->error == 0)
            if $arg1 == 8
                set $kgm_readphysint_result = *((uint8_t *)$kgm_pkt->data)
            end
            if $arg1 == 16
                set $kgm_readphysint_result = *((uint16_t *)$kgm_pkt->data)
            end
            if $arg1 == 32
                set $kgm_readphysint_result = *((uint32_t *)$kgm_pkt->data)
            end
            if $arg1 == 64
                set $kgm_readphysint_result = *((uint64_t *)$kgm_pkt->data)
            end
        end

    else
        # No KDP. Attempt to use physical memory mapping

        if ($kgm_mtype == $kgm_mtype_x86_64)
            set $kgm_readphys_paddr_in_kva = (unsigned long long)$arg0 + (((unsigned long long)-1 << 47) | ((unsigned long long)509 << 39))
        else
            if ($kgm_mtype == $kgm_mtype_arm)
                set $kgm_readphys_paddr_in_kva = (unsigned long long)$arg0 - gPhysBase + gVirtBase
            else
                printf "readphys not available for current architecture.\n"
                set $kgm_readphys_paddr_in_kva = 0
            end
        end
        if $kgm_readphys_paddr_in_kva
            if $arg1 == 8
                set $kgm_readphysint_result = *((uint8_t *)$kgm_readphys_paddr_in_kva)
            end
            if $arg1 == 16
                set $kgm_readphysint_result = *((uint16_t *)$kgm_readphys_paddr_in_kva)
            end
            if $arg1 == 32
                set $kgm_readphysint_result = *((uint32_t *)$kgm_readphys_paddr_in_kva)
            end
            if $arg1 == 64
                set $kgm_readphysint_result = *((uint64_t *)$kgm_readphys_paddr_in_kva)
            end
        end
    end
end

define readphys8
       readphysint $arg0 8 $kgm_lcpu_self
       output /a $arg0
       printf ":\t0x%02hhx\n", $kgm_readphysint_result
       set $kgm_readphys_result = (uint64_t)$kgm_readphysint_result
end

define readphys16
       readphysint $arg0 16 $kgm_lcpu_self
       output /a $arg0
       printf ":\t0x%04hx\n", $kgm_readphysint_result
       set $kgm_readphys_result = (uint64_t)$kgm_readphysint_result
end

define readphys32
       readphysint $arg0 32 $kgm_lcpu_self
       output /a $arg0
       printf ":\t0x%08x\n", $kgm_readphysint_result
       set $kgm_readphys_result = (uint64_t)$kgm_readphysint_result
end

define readphys64
       readphysint $arg0 64 $kgm_lcpu_self
       output /a $arg0
       printf ":\t0x%016llx\n", $kgm_readphysint_result
       set $kgm_readphys_result = (uint64_t)$kgm_readphysint_result
end

define readphys
       readphys32 $arg0
end

document readphys8
| See readphys64
end

document readphys16
| See readphys64
end

document readphys32
| See readphys64
end

document readphys64
| The argument is interpreted as a physical address, and the 64-bit word
| addressed is displayed. Saves 64-bit result in $kgm_readphys_result.
end

define writephysint
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_writephysmem64_req_t)
       set $kgm_pkt = (kdp_writephysmem64_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request  = KDP_WRITEPHYSMEM64
       set $kgm_pkt->hdr.len      = sizeof(kdp_writephysmem64_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq      = 0
       set $kgm_pkt->hdr.key      = 0
       set $kgm_pkt->address      = (uint64_t)$arg0
       set $kgm_pkt->nbytes       = $arg1 >> 3
       set $kgm_pkt->lcpu         = $arg3
       if $arg1 == 8
       	  set *(uint8_t *)$kgm_pkt->data = (uint8_t)$arg2
       end
       if $arg1 == 16
       	  set *(uint16_t *)$kgm_pkt->data = (uint16_t)$arg2
       end
       if $arg1 == 32
       	  set *(uint32_t *)$kgm_pkt->data = (uint32_t)$arg2
       end
       if $arg1 == 64
       	  set *(uint64_t *)$kgm_pkt->data = (uint64_t)$arg2
       end
       set manual_pkt.input = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_writephysmem64_reply_t *)&manual_pkt.data
       set $kgm_writephysint_result = $kgm_pkt->error
end

define writephys8
       writephysint $arg0 8 $arg1 $kgm_lcpu_self
end

define writephys16
       writephysint $arg0 16 $arg1 $kgm_lcpu_self
end

define writephys32
       writephysint $arg0 32 $arg1 $kgm_lcpu_self
end

define writephys64
       writephysint $arg0 64 $arg1 $kgm_lcpu_self
end

document writephys8
| See writephys64
end

document writephys16
| See writephys64
end

document writephys32
| See writephys64
end

document writephys64
| The argument is interpreted as a physical address, and the second argument is
| written to that address as a 64-bit word.
end

define addkextsyms
	shell echo cd `pwd` > /tmp/gdb-cd
	cd $arg0
	source kcbmacros
	source /tmp/gdb-cd
	set $kgm_show_kmod_syms = 1
end

document addkextsyms
| Takes a directory of symbols for kexts generated with kextcache -y and loads them
| into gdb.
| (gdb) addkextsyms /path/to/symboldir
end

define showprocfiles
    if ($argc == 1)
	_showprocheader
	_showprocfiles $arg0
    else
    	printf "| Usage:\n|\n"
	help showprocfiles
    end
end
document showprocfiles
Syntax: (gdb) showprocfiles <proc_t>
| Given a proc_t pointer, display the list of open file descriptors for the
| referenced process.
end

define _showprocheader
    printf "fd     fileglob  "
    showptrhdrpad
    printf "  fg flags    fg type   fg data   "
    showptrhdrpad
    printf "  info\n"
    printf "-----  ----------"
    if $kgm_lp64
        printf "--------"
    end
    printf "  ----------  --------  ----------"
    if $kgm_lp64
        printf "--------"
    end
    printf "  -------------------\n"
end

define _showprocfiles
    set $kgm_spf_filedesc = ((proc_t)$arg0)->p_fd
    set $kgm_spf_last = $kgm_spf_filedesc->fd_lastfile
    set $kgm_spf_ofiles = $kgm_spf_filedesc->fd_ofiles
    set $kgm_spf_count = 0
    while ($kgm_spf_count <= $kgm_spf_last)
	if ($kgm_spf_ofiles[$kgm_spf_count] == 0)
	    # DEBUG: For files that were open, but are now closed
	    # printf "%-5d  FILEPROC_NULL\n", $kgm_spf_count
	else
	    # display fd #, fileglob address, fileglob flags
	    set $kgm_spf_flags = $kgm_spf_ofiles[$kgm_spf_count].f_flags
	    set $kgm_spf_fg = $kgm_spf_ofiles[$kgm_spf_count].f_fglob
	    printf "%-5d  ", $kgm_spf_count
	    showptr $kgm_spf_fg
	    printf "  0x%08x  ", $kgm_spf_flags
	    # decode fileglob type
	    set $kgm_spf_fgt = $kgm_spf_fg->fg_type
	    if ($kgm_spf_fgt == 1)
	    	printf "VNODE   "
	    end
	    if ($kgm_spf_fgt == 2)
	    	printf "SOCKET  "
	    end
	    if ($kgm_spf_fgt == 3)
	    	printf "PSXSHM  "
	    end
	    if ($kgm_spf_fgt == 4)
	    	printf "PSXSEM  "
	    end
	    if ($kgm_spf_fgt == 5)
	    	printf "KQUEUE  "
	    end
	    if ($kgm_spf_fgt == 6)
	    	printf "PIPE    "
	    end
	    if ($kgm_spf_fgt == 7)
	    	printf "FSEVENTS"
	    end
	    if ($kgm_spf_fgt < 1 || $kgm_spf_fgt > 7)
	        printf "?: %-5d", $kgm_spf_fgt
	    end

	    # display fileglob data address and decode interesting fact(s)
	    # about data, if we know any
	    set $kgm_spf_fgd = $kgm_spf_fg->fg_data
	    printf "  "
	    showptr $kgm_spf_fgd
	    printf "  "
	    if ($kgm_spf_fgt == 1)
	    	set $kgm_spf_name = ((struct vnode *)$kgm_spf_fgd)->v_name
		if ($kgm_spf_name == 0)
		    printf "(null)"
		else
		    printf "%s", $kgm_spf_name
		end
	    end
	    printf "\n"
	end
    	set $kgm_spf_count = $kgm_spf_count + 1
    end
end

#
# Show all the advisory file locks held by a process for each of the vnode
# type files that it has open; do this by walking the per process open file
# table and looking at any vnode type fileglob that has a non-NULL lock list
# associated with it.
#
define showproclocks
    if ($argc == 1)
	_showproclocks $arg0
    else
    	printf "| Usage:\n|\n"
	help showproclocks
    end
end
document showproclocks
Syntax: (gdb) showproclocks <proc_t>
| Given a proc_t pointer, display the list of advisory file locks held by the
| referenced process.
end

define _showproclocks
    set $kgm_spl_filedesc = ((proc_t)$arg0)->p_fd
    set $kgm_spl_last = $kgm_spl_filedesc->fd_lastfile
    set $kgm_spl_ofiles = $kgm_spl_filedesc->fd_ofiles
    set $kgm_spl_count = 0
    set $kgm_spl_seen = 0
    while ($kgm_spl_count <= $kgm_spl_last)
	if ($kgm_spl_ofiles[$kgm_spl_count] == 0)
	    # DEBUG: For files that were open, but are now closed
	    # printf "%-5d  FILEPROC_NULL\n", $kgm_spl_count
	else
	    set $kgm_spl_fg = $kgm_spl_ofiles[$kgm_spl_count].f_fglob
	    # decode fileglob type
	    set $kgm_spl_fgt = $kgm_spl_fg->fg_type
	    if ($kgm_spl_fgt == 1)
		set $kgm_spl_fgd = $kgm_spl_fg->fg_data
	    	set $kgm_spl_name = ((struct vnode *)$kgm_spl_fgd)->v_name
		set $kgm_spl_vnode = ((vnode_t)$kgm_spl_fgd)
		set $kgm_spl_lockiter = $kgm_spl_vnode->v_lockf
		if ($kgm_spl_lockiter != 0)
		    if ($kgm_spl_seen == 0)
			_showvnodelockheader
		    end
		    set $kgm_spl_seen = $kgm_spl_seen + 1
		    printf "( fd %d, name ", $kgm_spl_count
		    if ($kgm_spl_name == 0)
			printf "(null) )"
		    else
			printf "%s )\n", $kgm_spl_name
		    end
		    _showvnodelocks $kgm_spl_fgd
		end
	    end
	end
    	set $kgm_spl_count = $kgm_spf_count + 1
    end
    printf "%d total locks for ", $kgm_spl_seen
    showptr $arg0
    printf "\n"
end

define showprocinfo
    set $kgm_spi_proc = (proc_t)$arg0
    printf "Process "
    showptr $kgm_spi_proc
    printf "\n"
    printf "   name %s\n", $kgm_spi_proc->p_comm
    printf "   pid:%.8d", $kgm_spi_proc->p_pid
    printf "   task:"
    showptr $kgm_spi_proc->task
    printf "   p_stat:%.1d", $kgm_spi_proc->p_stat
    printf "   parent pid:%.8d", $kgm_spi_proc->p_ppid
    printf "\n"
    # decode part of credential
    set $kgm_spi_cred = $kgm_spi_proc->p_ucred
    if ($kgm_spi_cred != 0)
	printf "Cred: euid %d ruid %d svuid %d\n", $kgm_spi_cred->cr_posix.cr_uid, $kgm_spi_cred->cr_posix.cr_ruid, $kgm_spi_cred->cr_posix.cr_svuid
    else
    	printf "Cred: (null)\n"
    end
    # decode flags
    set $kgm_spi_flag = $kgm_spi_proc->p_flag
    printf "Flags: 0x%08x\n", $kgm_spi_flag
    if ($kgm_spi_flag & 0x00000001)
	printf "    0x00000001 - may hold advisory locks\n"
    end
    if ($kgm_spi_flag & 0x00000002)
	printf "    0x00000002 - has a controlling tty\n"
    end
    if ($kgm_spi_flag & 0x00000004)
	printf "    0x00000004 - process is 64 bit\n"
    else
	printf "   !0x00000004 - process is 32 bit\n"
    end
    if ($kgm_spi_flag & 0x00000008)
	printf "    0x00000008 - no SIGCHLD on child stop\n"
    end
    if ($kgm_spi_flag & 0x00000010)
	printf "    0x00000010 - waiting for child exec/exit\n"
    end
    if ($kgm_spi_flag & 0x00000020)
	printf "    0x00000020 - has started profiling\n"
    end
    if ($kgm_spi_flag & 0x00000040)
	printf "    0x00000040 - in select; wakeup/waiting danger\n"
    end
    if ($kgm_spi_flag & 0x00000080)
	printf "    0x00000080 - was stopped and continued\n"
    end
    if ($kgm_spi_flag & 0x00000100)
	printf "    0x00000100 - has set privileges since exec\n"
    end
    if ($kgm_spi_flag & 0x00000200)
	printf "    0x00000200 - system process: no signals, stats, or swap\n"
    end
    if ($kgm_spi_flag & 0x00000400)
	printf "    0x00000400 - timing out during a sleep\n"
    end
    if ($kgm_spi_flag & 0x00000800)
	printf "    0x00000800 - debugged process being traced\n"
    end
    if ($kgm_spi_flag & 0x00001000)
	printf "    0x00001000 - debugging process has waited for child\n"
    end
    if ($kgm_spi_flag & 0x00002000)
	printf "    0x00002000 - exit in progress\n"
    end
    if ($kgm_spi_flag & 0x00004000)
	printf "    0x00004000 - process has called exec\n"
    end
    if ($kgm_spi_flag & 0x00008000)
	printf "    0x00008000 - owe process an addupc() XXX\n"
    end
    if ($kgm_spi_flag & 0x00010000)
	printf "    0x00010000 - affinity for Rosetta children\n"
    end
    if ($kgm_spi_flag & 0x00020000)
	printf "    0x00020000 - wants to run Rosetta\n"
    end
    if ($kgm_spi_flag & 0x00040000)
	printf "    0x00040000 - has wait() in progress\n"
    end
    if ($kgm_spi_flag & 0x00080000)
	printf "    0x00080000 - kdebug tracing on for this process\n"
    end
    if ($kgm_spi_flag & 0x00100000)
	printf "    0x00100000 - blocked due to SIGTTOU or SIGTTIN\n"
    end
    if ($kgm_spi_flag & 0x00200000)
	printf "    0x00200000 - has called reboot()\n"
    end
    if ($kgm_spi_flag & 0x00400000)
	printf "    0x00400000 - is TBE state\n"
    end
    if ($kgm_spi_flag & 0x00800000)
	printf "    0x00800000 - signal exceptions\n"
    end
    if ($kgm_spi_flag & 0x01000000)
	printf "    0x01000000 - has thread cwd\n"
    end
    if ($kgm_spi_flag & 0x02000000)
	printf "    0x02000000 - has vfork() children\n"
    end
    if ($kgm_spi_flag & 0x04000000)
	printf "    0x04000000 - not allowed to attach\n"
    end
    if ($kgm_spi_flag & 0x08000000)
	printf "    0x08000000 - vfork() in progress\n"
    end
    if ($kgm_spi_flag & 0x10000000)
	printf "    0x10000000 - no shared libraries\n"
    end
    if ($kgm_spi_flag & 0x20000000)
	printf "    0x20000000 - force quota for root\n"
    end
    if ($kgm_spi_flag & 0x40000000)
	printf "    0x40000000 - no zombies when children exit\n"
    end
    if ($kgm_spi_flag & 0x80000000)
	printf "    0x80000000 - don't hang on remote FS ops\n"
    end
    # decode state
    set $kgm_spi_state = $kgm_spi_proc->p_stat
    printf "State: "
    if ($kgm_spi_state == 1)
    	printf "Idle\n"
    end
    if ($kgm_spi_state == 2)
    	printf "Run\n"
    end
    if ($kgm_spi_state == 3)
    	printf "Sleep\n"
    end
    if ($kgm_spi_state == 4)
    	printf "Stop\n"
    end
    if ($kgm_spi_state == 5)
    	printf "Zombie\n"
    end
    if ($kgm_spi_state == 6)
    	printf "Reaping\n"
    end
    if ($kgm_spi_state < 1 || $kgm_spi_state > 6)
    	printf "(Unknown)\n"
    end
end

document showprocinfo
Syntax: (gdb) showprocinfo <proc_t>
| Displays name, pid, parent and task for a proc_t. Decodes cred, flag and p_stat fields.
end

#
# dump the zombprocs 
#
define zombproc
  set $basep = (struct proc  *)zombproc->lh_first
  set $pp = $basep
  while $pp
	showprocinfo $pp
      set $pp = $pp->p_list.le_next
  end
end

document zombproc
Syntax: (gdb) zombproc 
| Routine to print out all procs in the zombie list
end

#
# dump the zombstacks 
#
define zombstacks
  set $basep = (struct proc  *)zombproc->lh_first
  set $pp = $basep
  while $pp
	if $pp->p_stat != 5
		showtaskstacks $pp->task
	end
     set $pp = $pp->p_list.le_next
  end
end

document zombstacks
Syntax: (gdb) zombstacks 
| Routine to print out all stacks of tasks that are exiting
end


#
# dump the allprocs
#
define allproc
  set $basep = (struct proc  *)allproc->lh_first
  set $pp = $basep
  while $pp
	showprocinfo $pp
      set $pp = $pp->p_list.le_next
  end
end

document allproc
Syntax: (gdb) allproc 
| Routine to print out all process in the system 
| which are not in the zombie list
end
define showprocsiblingint
    set $kgm_sibling_ptr = (struct proc *)$arg0
    set $kgm_lx = $arg1 
    while $kgm_lx
        printf "|  " 
        set $kgm_lx = $kgm_lx-3 
    end   
    printf "|--%d    %s    [ 0x%llx ]\n", $kgm_sibling_ptr->p_pid, $kgm_sibling_ptr->p_comm, $kgm_sibling_ptr
end
define showproctreeint
#Initialize all the set variables used in this macro
    set $kgm_basep1 = 0
    set $kgm_sibling_ptr = 0
    set $kgm_lx = 0
    set $kgm_tmp_base = 0
    set $kgm_head_ptr = 0
    set $kgm_search_pid = 0 
    set $kgm_rev = 0
    set $kgm_x = 0

    set $kgm_basep1 = (struct proc *)allproc->lh_first
    if ($arg0 == 0)
        set $kgm_head_ptr = (struct proc *)initproc
    end       
    if ($arg0 > 0)
        set $kgm_tmp_base = (struct proc *)allproc->lh_first
        set $kgm_search_pid = $arg0 
        while $kgm_tmp_base
            if ( $kgm_tmp_base->p_pid == $kgm_search_pid)
               if ($kgm_tmp_base->p_childrencnt > 0)
                    set $kgm_head_ptr = $kgm_tmp_base->p_children.lh_first
               else
                    set $kgm_head_ptr = 0
                    printf "No children present for PID=%d", $kgm_search_pid
               end
               loop_break
            end
            set $kgm_tmp_base = $kgm_tmp_base->p_list.le_next
        end
    end
    set $kgm_rev = 0
    set $kgm_x = 0
    if ($kgm_head_ptr)
        printf "PID   PROCESS       POINTER]\n"
        printf "===   =======       =======\n"
        printf "%d    %s      [ 0x%llx ]\n", $kgm_head_ptr->p_ppid, $kgm_head_ptr->p_pptr->p_comm, $kgm_head_ptr
        printf "|--%d    %s      [ 0x%llx ]\n", $kgm_head_ptr->p_pid, $kgm_head_ptr->p_comm, $kgm_head_ptr
    end
    while ($kgm_head_ptr)
       #Is childrencnt = 0?       YES  {=> no children}
        if ($kgm_head_ptr->p_childrencnt == 0)
            # Does it have sibling? 
            if($kgm_head_ptr->p_sibling.le_next == 0)
                #No, it does not have sibling, so go back to its parent which will go to its sibling
                if($kgm_head_ptr == $kgm_head_ptr->p_pptr)
                    loop_break
                end
                set $kgm_head_ptr = $kgm_head_ptr->p_pptr
                if ($kgm_head_ptr == $kgm_tmp_base)
                    loop_break
                end
                if ($kgm_x > 3)
                    set $kgm_x = $kgm_x - 3
                end
                set $kgm_rev = 1
            end
            if($kgm_head_ptr->p_sibling.le_next != 0)
                # Yes, it has sibling. So print sibling
                set $kgm_rev = 0
                showprocsiblingint $kgm_head_ptr->p_sibling.le_next $kgm_x
                set $kgm_head_ptr = $kgm_head_ptr->p_sibling.le_next
            end
        # childrencnt != 0  {=> it has children}
        else
            if ($kgm_rev == 1)
                if($kgm_head_ptr->p_sibling.le_next == 0)
                    #No, it does not have sibling, so go back to its parent which will go to its sibling
                    if($kgm_head_ptr == $kgm_head_ptr->p_pptr)
                        loop_break
                    end
                    set $kgm_head_ptr = $kgm_head_ptr->p_pptr
                    if ($kgm_head_ptr == $kgm_tmp_base)
                        loop_break
                    end

                    if ($kgm_x > 3)
                        set $kgm_x = $kgm_x - 3
                    end
                    set $kgm_rev = 1
                end
                if($kgm_head_ptr->p_sibling.le_next != 0)
                    set $kgm_rev = 0
                    # Yes, it has sibling. So print sibling
                    showprocsiblingint $kgm_head_ptr->p_sibling.le_next $kgm_x
                    set $kgm_head_ptr = $kgm_head_ptr->p_sibling.le_next
                end
            else
                set $kgm_head_ptr = $kgm_head_ptr->p_children.lh_first
                set $kgm_x = $kgm_x + 3
                set $kgm_lx = $kgm_x
                while $kgm_lx
                    printf "|  "
                    set $kgm_lx = $kgm_lx-3
                end
                printf "|--%d    %s      [ 0x%llx ] \n", $kgm_head_ptr->p_pid, $kgm_head_ptr->p_comm, $kgm_head_ptr
            end
        end
    end
    printf "\n"
#Unset all the set variables used in this macro
    set $kgm_basep1 = 0
    set $kgm_sibling_ptr = 0
    set $kgm_lx = 0
    set $kgm_tmp_base = 0
    set $kgm_head_ptr = 0
    set $kgm_search_pid = 0
    set $kgm_rev = 0
    set $kgm_x = 0
end
define showproctree
    if ($argc > 0)
        showproctreeint $arg0
    else
        showproctreeint 0
    end
end
document showproctree
Syntax: (gdb) showproctree <pid>
| Routine to print the processes in the system in a hierarchical tree form. This routine does not print zombie processes.
| If no argument is given, showproctree will print all the processes in the system.  
| If pid is specified, showproctree prints all the descendants of the indicated process
end


define print_vnode
   set $vp = (struct vnode *)$arg0
   printf "   "
   printf " vp "
   showptr $vp
   printf " use %d", $vp->v_usecount
   printf " io %d", $vp->v_iocount
   printf " kuse %d", $vp->v_kusecount
   printf " type %d", $vp->v_type
   printf " flg 0x%.8x", $vp->v_flag
   printf " lflg 0x%.8x", $vp->v_lflag
   printf " par "
   showptr $vp->v_parent
   set $_name = (char *)$vp->v_name
   if ($_name != 0)
      printf " %s", $_name
   end
  if ($vp->v_type == VREG) && ($vp->v_un.vu_ubcinfo != 0)
       printf " mapped %d", ($vp->v_un.vu_ubcinfo.ui_flags & 0x08) ? 1 : 0
   end
   printf "\n"
end

document print_vnode
Syntax: (gdb) print_vnode <vnode>
| Prints out the fields of a vnode struct
end

define showprocvnodes
	set $pp = (struct proc *)$arg0
	set $fdp = (struct filedesc *)$pp->p_fd
	set $cvp = $fdp->fd_cdir
	set $rvp = $fdp->fd_rdir
	if $cvp
		printf "Current Working Directory \n"
		print_vnode $cvp
		printf "\n"
	end
	if $rvp
		printf "Current Root Directory \n"
		print_vnode $rvp
		printf "\n"
	end
	set $count = 0
	set $fpp =  (struct fileproc **)($fdp->fd_ofiles)
	set $fpo =  (char)($fdp->fd_ofileflags[0])
	while $count < $fdp->fd_nfiles
		#printf"fpp %x ", *$fpp
		if *$fpp
			set $fg =(struct fileglob *)((**$fpp)->f_fglob)
			if  $fg && (($fg)->fg_type == 1) 
				if $fdp->fd_ofileflags[$count] & 4
					printf "U: "
				else
					printf " "
				end
				printf "fd = %d ", $count
				print_vnode $fg->fg_data
			end
		end
		set $fpp = $fpp + 1
	 	set $count = $count + 1		
	end
end

document showprocvnodes
Syntax: (gdb) showprocvnodes <proc_address>
| Routine to print out all the open fds
| which are vnodes in a process
end

define showallprocvnodes
  set $basep = (struct proc  *)allproc->lh_first
  set $pp = $basep
  while $pp
	printf "============================================ \n"
	showprocinfo $pp
	showprocvnodes $pp
  	set $pp = $pp->p_list.le_next
  end
end

document showallprocvnodes
Syntax: (gdb) showallprocvnodes
| Routine to print out all the open fds
| which are vnodes 
end


#   
# dump the childrent of a proc 
#
define showinitchild
  set $basep = (struct proc  *)initproc->p_children.lh_first
  set $pp = $basep
  while $pp
	showprocinfo $pp
      set $pp = $pp->p_sibling.le_next
  end
end

document showinitchild
Syntax: (gdb) showinitchild 
| Routine to print out all processes in the system 
| which are children of init process
end


define showmountallvnodes
  set $mp = (struct mount *)$arg0
  set $basevp = (struct vnode *)$mp->mnt_vnodelist.tqh_first
  set $vp = $basevp
  printf "____________________ Vnode list Queue ---------------\n"
  while $vp
      print_vnode $vp
      set $vp = $vp->v_mntvnodes->tqe_next
  end
  set $basevp = (struct vnode *)$mp->mnt_workerqueue.tqh_first
  set $vp = $basevp
  printf "____________________ Worker Queue ---------------\n"
  while $vp
      print_vnode $vp
      set $vp = $vp->v_mntvnodes->tqe_next
  end
  set $basevp = (struct vnode *)$mp->mnt_newvnodes.tqh_first
  set $vp = $basevp
  printf "____________________ New vnodes  Queue ---------------\n"
  while $vp
      print_vnode $vp
      set $vp = $vp->v_mntvnodes->tqe_next
  end
end
document showmountallvnodes
Syntax: showmountallvnodes <struct mount *>
| Print the vnode inactive list
end


define showmountvnodes
  set $mp = (struct mount *)$arg0
  set $basevp = (struct vnode *)$mp->mnt_vnodelist.tqh_first
  set $vp = $basevp
  printf "____________________ Vnode list Queue ---------------\n"
  while $vp
      print_vnode $vp
      set $vp = $vp->v_mntvnodes->tqe_next
  end
end
document showmountvnodes
Syntax: showmountvnodes <struct mount *>
| Print the vnode list
end



define showworkqvnodes
  set $mp = (struct mount *)$arg0
  set $basevp = (struct vnode *)$mp->mnt_workerqueue.tqh_first
  set $vp = $basevp
  printf "____________________ Worker Queue ---------------\n"
  while $vp
      print_vnode $vp
      set $vp = $vp->v_mntvnodes->tqe_next
  end
end
document showworkqvnodes
Syntax: showworkqvnodes <struct mount *>
| Print the vnode worker list
end


define shownewvnodes
  set $mp = (struct mount *)$arg0
  set $basevp = (struct vnode *)$mp->mnt_newvnodes.tqh_first
  set $vp = $basevp
  printf "____________________ New vnodes  Queue ---------------\n"
  while $vp
      print_vnode $vp
      set $vp = $vp->v_mntvnodes->tqe_next
  end
end

document shownewvnodes
Syntax: shownewvnodes <struct mount *>
| Print the new vnode list
end


#
# print mount point info
define print_mount 
   set $mp = (struct mount *)$arg0
   printf "   "
   printf " mp "
   showptr $mp
   printf " flag %x", $mp->mnt_flag
   printf " kern_flag %x", $mp->mnt_kern_flag
   printf " lflag %x", $mp->mnt_lflag
   printf " type:  %s", $mp->mnt_vfsstat.f_fstypename
   printf " mnton:  %s", $mp->mnt_vfsstat.f_mntonname
   printf " mntfrom:  %s", $mp->mnt_vfsstat.f_mntfromname
   printf "\n"
end

define showallmounts
	set $mp=(struct mount *)mountlist.tqh_first
	while $mp
	 	print_mount $mp
		set $mp = $mp->mnt_list.tqe_next
	end
end
	
document showallmounts
Syntax: showallmounts
| Print all mount points
end

define pcprint
	if (((unsigned long) $arg0 < (unsigned long) &_mh_execute_header || \
	     (unsigned long) $arg0 >= (unsigned long) &last_kernel_symbol ))
		showkmodaddr $arg0
	else
		output /a $arg0
	end
end

define mbuf_walkpkt
	set $mp = (struct mbuf *)$arg0
	set $cnt = 1
	set $tot = 0
	while $mp
		printf "%4d: %p [len %4d, type %2d, ", $cnt, $mp, \
		    $mp->m_hdr.mh_len, $mp->m_hdr.mh_type
		if mclaudit != 0
			mbuf_buf2mca $mp
			printf ", "
		end
		set $tot = $tot + $mp->m_hdr.mh_len
		printf "total %d]\n", $tot
		set $mp = $mp->m_hdr.mh_nextpkt
		set $cnt = $cnt + 1
	end
end

document mbuf_walkpkt
Syntax: (gdb) mbuf_walkpkt <addr>
| Given an mbuf address, walk its m_nextpkt pointer
end

define mbuf_walk
	set $mp = (struct mbuf *)$arg0
	set $cnt = 1
	set $tot = 0
	while $mp
		printf "%4d: %p [len %4d, type %2d, ", $cnt, $mp, \
		    $mp->m_hdr.mh_len, $mp->m_hdr.mh_type
		if mclaudit != 0
			mbuf_buf2mca $mp
			printf ", "
		end
		set $tot = $tot + $mp->m_hdr.mh_len
		printf "total %d]\n", $tot
		set $mp = $mp->m_hdr.mh_next
		set $cnt = $cnt + 1
	end
end

document mbuf_walk
Syntax: (gdb) mbuf_walk <addr>
| Given an mbuf address, walk its m_next pointer
end

define mbuf_buf2slab
	set $addr = $arg0
	set $gix = ((char *)$addr - (char *)mbutl) >> 20
	set $ix = ((char *)$addr - (char *)slabstbl[$gix].slg_slab[0].sl_base) >> 12
	set $slab = &slabstbl[$gix].slg_slab[$ix]
	if $kgm_lp64
		printf "0x%-16llx", $slab
	else
		printf "0x%-8x", $slab
	end
end

document mbuf_buf2slab
| Given an mbuf object, find its corresponding slab address.
end

define mbuf_buf2mca
	set $addr = $arg0
	set $ix = ((char *)$addr - (char *)mbutl) >> 12
	set $clbase = ((union mbigcluster *)mbutl) + $ix
	set $mclidx = (((char *)$addr - (char *)$clbase) >> 8)
	set $mca = mclaudit[$ix].cl_audit[$mclidx]
	if $kgm_lp64
		printf "mca: 0x%-16llx", $mca
	else
		printf "mca: 0x%-8x", $mca
	end
end

document mbuf_buf2mca
Syntax: (gdb) mbuf_buf2mca <addr>
| Given an mbuf object, find its buffer audit structure address.
| This requires mbuf buffer auditing to be turned on, by setting
| the appropriate flags to the "mbuf_debug" boot-args parameter.
end

define mbuf_showmca
	set language c
	set $mca = (mcache_audit_t *)$arg0
	set $cp = (mcache_t *)$mca->mca_cache
	printf "object type:\t\t"
	mbuf_mca_ctype $mca 1
	printf "\ncontrolling mcache:\t%p (%s)\n", $mca->mca_cache, $cp->mc_name
	if $mca->mca_uflags & $MB_SCVALID
		set $ix = ((char *)$mca->mca_addr - (char *)mbutl) >> 12
		set $clbase = ((union mbigcluster *)mbutl) + $ix
		set $mclidx = (((char *)$mca->mca_addr - (char *)$clbase) >> 8)
		printf "mbuf obj:\t\t%p\n", $mca->mca_addr
		printf "mbuf index:\t\t%d (out of 16) in cluster base %p\n", \
		    $mclidx + 1, $clbase
		if $mca->mca_uptr != 0
			set $peer_mca = (mcache_audit_t *)$mca->mca_uptr
			printf "paired cluster obj:\t%p (mca %p)\n", \
			    $peer_mca->mca_addr, $peer_mca
		end
		printf "saved contents:\t\t%p (%d bytes)\n", \
		    $mca->mca_contents, $mca->mca_contents_size
	else
		printf "cluster obj:\t\t%p\n", $mca->mca_addr
		if $mca->mca_uptr != 0
			set $peer_mca = (mcache_audit_t *)$mca->mca_uptr
			printf "paired mbuf obj:\t%p (mca %p)\n", \
			    $peer_mca->mca_addr, $peer_mca
		end
	end
	printf "recent transaction for this buffer (thread %p):\n", \
	    $mca->mca_thread
	set $cnt = 0
	while $cnt < $mca->mca_depth
		set $kgm_pc = $mca->mca_stack[$cnt]
		printf "%4d: ", $cnt + 1
		pcprint $kgm_pc
		printf "\n"
		set $cnt = $cnt + 1
	end
	if $mca->mca_pdepth > 0
		printf "previous transaction for this buffer (thread %p):\n", \
		    $mca->mca_pthread
	end
	set $cnt = 0
	while $cnt < $mca->mca_pdepth
		set $kgm_pc = $mca->mca_pstack[$cnt]
		printf "%4d: ", $cnt + 1
		pcprint $kgm_pc
		printf "\n"
		set $cnt = $cnt + 1
	end
	set language auto
end

document mbuf_showmca
Syntax: (gdb) mbuf_showmca <addr>
| Given an mbuf/cluster buffer audit structure address, print the audit
| records including the stack trace of the last buffer transaction.
end

define mbuf_topleak
	set language c
	set $topcnt = 0
	if $arg0 < 5
		set $maxcnt = $arg0
	else
		set $maxcnt = 5
	end
	while $topcnt < $maxcnt
		mbuf_traceleak mleak_top_trace[$topcnt]
		set $topcnt = $topcnt + 1
	end
	set language auto
end

document mbuf_topleak
Syntax: (gdb) mbuf_topleak <num>
| Prints information about the top <num> suspected mbuf leakers
| where <num> is a value <= 5
end

define mbuf_traceleak
	set language c
	set $trace = (struct mtrace *) $arg0
	if $trace->allocs != 0
		printf "%p:%d outstanding allocs\n", $trace, $trace->allocs
		printf "backtrace saved %d deep:\n", $trace->depth
		if $trace->depth != 0
			set $cnt = 0
			while $cnt < $trace->depth
		                printf "%4d: ", $cnt + 1
				pcprint $trace->addr[$cnt]
				printf "\n"
				set $cnt = $cnt + 1
			end
		end
	end
	set language auto
end

document mbuf_traceleak
Syntax: (gdb) mbuf_traceleak <addr>
| Given an mbuf leak trace (mtrace) structure address, print out the
| stored information with that trace
end

set $MCF_NOCPUCACHE = 0x10

define mcache_stat
	set $head = (mcache_t *)mcache_head
	set $mc = $head

    if $kgm_lp64
		printf "cache                        cache              cache    buf   buf            backing             (# of retries)     bufs\n"
		printf "name                         state               addr   size align               zone     wait   nowait   failed  incache\n"
		printf "------------------------- -------- ------------------ ------ ----- ------------------ -------------------------- --------\n"
	else
		printf "cache                        cache      cache    buf   buf    backing             (# of retries)     bufs\n"
		printf "name                         state       addr   size align       zone     wait   nowait   failed  incache\n"
		printf "------------------------- -------- ---------- ------ ----- ---------- -------------------------- --------\n"
	end
	while $mc != 0
		set $bktsize = $mc->mc_cpu.cc_bktsize
		printf "%-25s ", $mc->mc_name
		if ($mc->mc_flags & $MCF_NOCPUCACHE)
			printf "disabled"
		else
			if $mc->mc_purge_cnt > 0
				printf " purging"
			else
				if $bktsize == 0
					printf " offline"
				else
					printf "  online"
				end
			end
		end
		printf " %p %6d %5d ",$mc, \
		    $mc->mc_bufsize, $mc->mc_align
		if $mc->mc_slab_zone != 0
			printf "%p", $mc->mc_slab_zone
		else
		    if $kgm_lp64
				printf "            custom"
			else
				printf "    custom"
			end
		end
		set $tot = 0
		set $tot += $mc->mc_full.bl_total * $bktsize
		set $ccp = (mcache_cpu_t *)$mc->mc_cpu
		set $n = 0
		while $n < ncpu
			if $ccp->cc_objs > 0
				set $tot += $ccp->cc_objs
			end
			if $ccp->cc_pobjs > 0
				set $tot += $ccp->cc_pobjs
			end
			set $n += 1
			set $ccp += 1
		end
		printf " %8d %8d %8d %8d", $mc->mc_wretry_cnt, \
		    $mc->mc_nwretry_cnt, $mc->mc_nwfail_cnt, $tot
		printf "\n"
		set $mc = (mcache_t *)$mc->mc_list.le_next
	end
end

document mcache_stat
Syntax: (gdb) mcache_stat
| Print all mcaches in the system.
end

define mcache_showzone
	set $mc = (mcache_t *)$arg0
	if $mc->mc_slab_zone != 0
		printf "%p", $mc->mc_slab_zone
	else
		printf "   custom"
end

document mcache_showzone
Syntax: (gdb) mcache_showzone <mcache_addr>
| Print the type of backend (custom or zone) of a mcache.
end

define mcache_walkobj
	set $p = (mcache_obj_t *)$arg0
	set $cnt = 1
	set $tot = 0
	while $p
		printf "%4d: %p\n", $cnt, $p,
		set $p = $p->obj_next
		set $cnt = $cnt + 1
	end
end

document mcache_walkobj
Syntax: (gdb) mcache_walkobj <addr>
| Given a mcache object address, walk its obj_next pointer
end

define mcache_showcache
	set $cp = (mcache_t *)$arg0
	set $ccp = (mcache_cpu_t *)$cp->mc_cpu
	set $bktsize = $cp->mc_cpu.cc_bktsize
	set $cnt = 0
	set $tot = 0
	printf "Showing cache '%s':\n\n", $cp->mc_name
	printf " CPU  cc_objs cc_pobjs    total\n"
	printf "---- -------- -------- --------\n"
	while $cnt < ncpu
		set $objs = $ccp->cc_objs
		if $objs <= 0
			set $objs = 0
		end
		set $pobjs = $ccp->cc_pobjs
		if $pobjs <= 0
			set $pobjs = 0
		end
		set $tot_cpu = $objs + $pobjs
		set $tot += $tot_cpu
		printf "%4d %8d %8d %8d\n", $cnt, $objs, $pobjs, $tot_cpu
		set $ccp += 1
		set $cnt += 1
	end
	printf "                       ========\n"
	printf "                       %8d\n", $tot
	printf "\n"
	set $tot += $cp->mc_full.bl_total * $bktsize
	printf "Total # of full buckets (%d objs/bkt):\t%-8d\n", \
	    $bktsize, $cp->mc_full.bl_total
	printf "Total # of objects cached:\t\t%-8d\n", $tot
end

document mcache_showcache
| Display the number of objects in the cache
end

set $NSLABSPMB = sizeof(mcl_slabg_t)/sizeof(mcl_slab_t)

define mbuf_slabstbl
	set $x = 0

	if $kgm_lp64
		printf "slot slabg              slabs range\n"
		printf "---- ------------------ -------------------------------------------\n"
	else
		printf "slot slabg      slabs range\n"
		printf "---- ---------- ---------------------------\n"
	end
	while $x < maxslabgrp
		set $slg = slabstbl[$x]
		printf "%3d: ", $x
		if $slg == 0
			printf "-\n"
		else
			if $kgm_lp64
				printf "0x%-16llx [ 0x%-16llx - 0x%-16llx ]\n", $slg, &$slg->slg_slab[0], \
				    &$slg->slg_slab[$NSLABSPMB-1]
			else
				printf "0x%-8x [ 0x%-8x - 0x%-8x ]\n", $slg, &$slg->slg_slab[0], \
				    &$slg->slg_slab[$NSLABSPMB-1]
			end
		end
		set $x += 1
	end
end

document mbuf_slabstbl
| Display the mbuf slabs table
end

set $SLF_MAPPED=0x0001
set $SLF_PARTIAL=0x0002
set $SLF_DETACHED=0x0004

define mbuf_slabs
	set $slg = (mcl_slabg_t *)$arg0
	set $x = 0

	if $kgm_lp64
		printf "slot slab               next               obj                mca                 C  R  N   size flags\n"
		printf "---- ------------------ ------------------ ------------------ ------------------ -- -- -- ------ -----\n"
	else
		printf "slot slab       next       obj        mca         C  R  N   size flags\n"
		printf "---- ---------- ---------- ---------- ---------- -- -- -- ------ -----\n"
	end
	while $x < $NSLABSPMB
		set $sl = &$slg->slg_slab[$x]
		set $mca = 0
		set $obj = $sl->sl_base

		if mclaudit != 0
			set $ix = ((char *)$obj - (char *)mbutl) >> 12
			set $clbase = ((union mbigcluster *)mbutl) + $ix
			set $mclidx = (((char *)$obj - (char *)$clbase) >> 8)
			set $mca = mclaudit[$ix].cl_audit[$mclidx]
		end

		if $kgm_lp64
			printf "%3d: 0x%-16llx 0x%-16llx 0x%-16llx 0x%-16llx %2d %2d %2d %6d 0x%04x ", \
			    $x + 1, $sl, $sl->sl_next, $obj, $mca, $sl->sl_class, \
			    $sl->sl_refcnt, $sl->sl_chunks, $sl->sl_len, \
			    $sl->sl_flags
		else
			printf "%3d: 0x%-8x 0x%-8x 0x%-8x 0x%-8x %2d %2d %2d %6d 0x%04x ", \
			    $x + 1, $sl, $sl->sl_next, $obj, $mca, $sl->sl_class, \
			    $sl->sl_refcnt, $sl->sl_chunks, $sl->sl_len, \
			    $sl->sl_flags
		end
		if $sl->sl_flags != 0
			printf "<"
			if $sl->sl_flags & $SLF_MAPPED
				printf "mapped"
			end
			if $sl->sl_flags & $SLF_PARTIAL
				printf ",partial"
			end
			if $sl->sl_flags & $SLF_DETACHED
				printf ",detached"
			end
			printf ">"
		end
		printf "\n"

		if $sl->sl_chunks > 1
			set $z = 1
			set $c = $sl->sl_len / $sl->sl_chunks

			while $z < $sl->sl_chunks
				set $obj = $sl->sl_base + ($c * $z)
				set $mca = 0

				if mclaudit != 0
					set $ix = ((char *)$obj - (char *)mbutl) >> 12
					set $clbase = ((union mbigcluster *)mbutl) + $ix
					set $mclidx = (((char *)$obj - (char *)$clbase) >> 8)
					set $mca = mclaudit[$ix].cl_audit[$mclidx]
				end

				if $kgm_lp64
					printf "                                           0x%-16llx 0x%-16llx\n", $obj, $mca
				else
					printf "                           0x%-8x 0x%-8x\n", $obj, $mca
				end
				set $z += 1
			end
		end

		set $x += 1
	end
end

document mbuf_slabs
| Display all mbuf slabs in the group
end

define mbuf_stat
	set $x = 0

	printf "class               total   cached     uncached           inuse           failed   waiter notified    purge\n"
	printf "name                 objs     objs     objs / slabs        objs      alloc count    count    count    count\n"
	printf "---------------- -------- -------- ------------------- -------- ---------------- -------- -------- --------\n"
	while $x < (sizeof(mbuf_table) / sizeof(mbuf_table[0]))
		set $mbt = mbuf_table[$x]
		set $mcs = (mb_class_stat_t *)mbuf_table[$x].mtbl_stats
		set $tot = 0
		set $mc = $mbt->mtbl_cache
		set $bktsize = $mc->mc_cpu.cc_bktsize
		set $tot += $mc->mc_full.bl_total * $bktsize
		set $ccp = (mcache_cpu_t *)$mc->mc_cpu
		set $n = 0
		while $n < ncpu
			if $ccp->cc_objs > 0
				set $tot += $ccp->cc_objs
			end
			if $ccp->cc_pobjs > 0
				set $tot += $ccp->cc_pobjs
			end
			set $n += 1
			set $ccp += 1
		end

		printf "%-16s %8d %8d %8d / %-8d %8d %16llu %8d %8llu %8llu", \
		    $mcs->mbcl_cname, $mcs->mbcl_total, $tot, \
		    $mcs->mbcl_infree, $mcs->mbcl_slab_cnt, \
		    ($mcs->mbcl_total - $tot - $mcs->mbcl_infree), \
		    $mcs->mbcl_fail_cnt, $mc->mc_waiter_cnt, \
		    $mcs->mbcl_notified, $mcs->mbcl_purge_cnt
		printf "\n"
		set $x += 1
	end
end

document mbuf_stat
| Print extended mbuf allocator statistics.
end

set $MB_INUSE = 0x1
set $MB_COMP_INUSE = 0x2
set $MB_SCVALID = 0x4

set $MCLBYTES = 2048
set $MSIZE = 256
set $NBPG = 4096
set $M16KCLBYTES = 16384

define mbuf_mca_ctype
	set $mca = (mcache_audit_t *)$arg0
	set $vopt = $arg1
	set $cp = $mca->mca_cache
	set $class = (unsigned int)$cp->mc_private
	set $csize = mbuf_table[$class].mtbl_stats->mbcl_size
	set $done = 0
	if $csize == $MSIZE
		if $vopt
			printf "M (mbuf) "
		else
			printf "M     "
		end
		set $done = 1
	end
	if !$done && $csize == $MCLBYTES
		if $vopt
			printf "CL (2K cluster) "
		else
			printf "CL    "
		end
		set $done = 1
	end
	if !$done && $csize == $NBPG
		if $vopt
			printf "BCL (4K cluster) "
		else
			printf "BCL   "
		end
		set $done = 1
	end
	if !$done && $csize == $M16KCLBYTES
		if $vopt
			printf "JCL (16K cluster) "
		else
			printf "JCL   "
		end
		set $done = 1
	end
	if !$done && $csize == ($MSIZE+$MCLBYTES)
		if $mca->mca_uflags & $MB_SCVALID
			if $mca->mca_uptr
				printf "M+CL  "
				if $vopt
					printf "(paired mbuf, 2K cluster)"
				end
			else
				printf "M-CL  "
				if $vopt
					printf "(unpaired mbuf, 2K cluster) "
				end
			end
		else
			if $mca->mca_uptr
				printf "CL+M  "
				if $vopt
					printf "(paired 2K cluster, mbuf) "
				end
			else
				printf "CL-M  "
				if $vopt
					printf "(paired 2K cluster, mbuf) "
				end
			end
		end
		set $done = 1
	end
	if !$done && $csize == ($MSIZE+$NBPG)
		if $mca->mca_uflags & $MB_SCVALID
			if $mca->mca_uptr
				printf "M+BCL "
				if $vopt
					printf "(paired mbuf, 4K cluster) "
				end
			else
				printf "M-BCL "
				if $vopt
					printf "(unpaired mbuf, 4K cluster) "
				end
			end
		else
			if $mca->mca_uptr
				printf "BCL+M "
				if $vopt
					printf "(paired 4K cluster, mbuf) "
				end
			else
				printf "BCL-M "
				if $vopt
					printf "(unpaired 4K cluster, mbuf) "
				end
			end
		end
		set $done = 1
	end
	if !$done && $csize == ($MSIZE+$M16KCLBYTES)
		if $mca->mca_uflags & $MB_SCVALID
			if $mca->mca_uptr
				printf "M+JCL "
				if $vopt
					printf "(paired mbuf, 16K cluster) "
				end
			else
				printf "M-JCL "
				if $vopt
					printf "(unpaired mbuf, 16K cluster) "
				end
			end
		else
			if $mca->mca_uptr
				printf "JCL+M "
				if $vopt
					printf "(paired 16K cluster, mbuf) "
				end
			else
				printf "JCL-M "
				if $vopt
					printf "(unpaired 16K cluster, mbuf) "
				end
			end
		end
		set $done = 1
	end
	if !$done
		printf "unknown: %s ", $cp->mc_name
	end
end

document mbuf_mca_ctype
| This is a helper macro for mbuf_show{active,inactive,all} that prints
| out the mbuf object type represented by a given mcache audit structure.
end

define mbuf_showactive
	if $argc == 0
		mbuf_walkallslabs 1 0
	else
		mbuf_walkallslabs 1 0 $arg0
	end
end

document mbuf_showactive
Syntax: (gdb) mbuf_showactive
| Walk the mbuf objects pool and print only the active ones; this
| requires mbuf debugging to be turned on, by setting the appropriate flags
| to the "mbuf_debug" boot-args parameter.  Active objects are those that
| are outstanding (have not returned to the mbuf slab layer) and in use
| by the client (have not been freed).
end

define mbuf_showinactive
	mbuf_walkallslabs 0 1
end

document mbuf_showinactive
Syntax: (gdb) mbuf_showinactive
| Walk the mbuf objects pool and print only the inactive ones; this
| requires mbuf debugging to be turned on, by setting the appropriate flags
| to the "mbuf_debug" boot-args parameter.  Inactive objects are those that
| are outstanding (have not returned to the mbuf slab layer) but have been
| freed by the client, i.e. they still reside in the mcache layer ready to
| be used for subsequent allocation requests.
end

define mbuf_showall
	mbuf_walkallslabs 1 1
end

document mbuf_showall
Syntax: (gdb) mbuf_showall
| Walk the mbuf objects pool and print them all; this requires
| mbuf debugging to be turned on, by setting the appropriate flags to the
| "mbuf_debug" boot-args parameter.
end

define mbuf_mcaobjs
end

define mbuf_walkallslabs
	set $show_a = $arg0
	set $show_f = $arg1
	if $argc == 3
		set $show_tr = $arg2
	else
		set $show_tr = 0
	end
	set $x = 0
	set $total = 0
	set $total_a = 0
	set $total_f = 0

	printf "("
	if $show_a && !$show_f
		printf "Searching only for active "
	end
	if !$show_a && $show_f
		printf "Searching only for inactive "
	end
	if $show_a && $show_f
		printf "Displaying all "
	end
	printf "objects; this may take a while ...)\n\n"

	if $kgm_lp64
		printf "                        slab                mca                obj        allocation\n"
		printf "slot idx             address            address            address type        state\n"
		printf "---- ---- ------------------ ------------------ ------------------ ----- -----------\n"
	else
		printf "                slab        mca        obj        allocation\n"
		printf "slot idx     address    address    address type        state\n"
		printf "---- ---- ---------- ---------- ---------- ----- -----------\n"
	end
	
	while $x < slabgrp
		set $slg = slabstbl[$x]
		set $y = 0
		set $stop = 0
		while $y < $NSLABSPMB && $stop == 0
			set $sl = &$slg->slg_slab[$y]
			set $base = (char *)$sl->sl_base
			set $ix = ($base - (char *)mbutl) >> 12
			set $clbase = ((union mbigcluster *)mbutl) + $ix
			set $mclidx = ($base - (char *)$clbase) >> 8
			set $mca = mclaudit[$ix].cl_audit[$mclidx]
			set $first = 1

			while $mca != 0 && $mca->mca_addr != 0
				set $printmca = 0
				if $mca->mca_uflags & ($MB_INUSE|$MB_COMP_INUSE)
					set $total_a = $total_a + 1
					set $printmca = $show_a
				else
					set $total_f = $total_f + 1
					set $printmca = $show_f
				end

				if $printmca != 0
					if $first == 1
						if $kgm_lp64
							printf "%4d %4d 0x%-16llx ", $x, $y, $sl
						else
							printf "%4d %4d 0x%-8x ", $x, $y, $sl
						end
					else
					    if $kgm_lp64
							printf "                             "
					    else
							printf "                     "
					    end
					end

					if $kgm_lp64
						printf "0x%-16llx 0x%-16llx ", $mca, $mca->mca_addr
					else
						printf "0x%-8x 0x%-8x ", $mca, $mca->mca_addr
					end

					mbuf_mca_ctype $mca 0
					if $mca->mca_uflags & ($MB_INUSE|$MB_COMP_INUSE)
						printf "active      "
					else
						printf "      freed "
					end
					if $first == 1
						set $first = 0
					end
					printf "\n"
					set $total = $total + 1
					
					if $show_tr != 0
						printf "recent transaction for this buffer (thread %p):\n", \
							$mca->mca_thread
						set $cnt = 0
						while $cnt < $mca->mca_depth
							set $kgm_pc = $mca->mca_stack[$cnt]
							printf "%4d: ", $cnt + 1
							pcprint $kgm_pc
							printf "\n"
							set $cnt = $cnt + 1
						end
					end
				end
				
				set $mca = $mca->mca_next
			end
			set $y += 1
			if $slg->slg_slab[$y].sl_base == 0
				set $stop = 1
			end
		end
		set $x += 1
	end
	if $total && $show_a && $show_f
		printf "\ntotal objects:\t%d\n", $total
		printf "active/unfreed:\t%d\n", $total_a
		printf "freed/in_cache:\t%d\n", $total_f
	end
end

document mbuf_walkallslabs
| Walk the mbuf objects pool; this requires mbuf debugging to be
| turned on, by setting the appropriate flags to the "mbuf_debug" boot-args
| parameter.  This is a backend routine for mbuf_show{active,inactive,all}.
end

define mbuf_countchain
	set $mp = (struct mbuf *)$arg0
	
	set $pkt = 0
	set $nxt = 0

	while $mp != 0
		set $pkt = $pkt + 1
	
		set $mn = (struct mbuf *)$mp->m_hdr.mh_next
		while $mn != 0
			set $nxt = $nxt + 1
			
			set $mn = (struct mbuf *)$mn->m_hdr.mh_next
		end
		
		set $mp = $mp->m_hdr.mh_nextpkt

		if (($pkt + $nxt) % 50) == 0
			printf "... %d\n", $pkt + $nxt
		end
	end

	printf "\ntotal: %d (via m_next: %d)\n", $pkt + $nxt, $nxt
end

document mbuf_countchain
Syntax: mbuf_countchain <addr>
| Count the total number of mbufs chained from the given the address of an mbuf. 
| The routine follows both the m_next pointers and m_nextpkt pointers.
end

set $RTF_UP          = 0x1
set $RTF_GATEWAY     = 0x2
set $RTF_HOST        = 0x4
set $RTF_REJECT      = 0x8
set $RTF_DYNAMIC     = 0x10
set $RTF_MODIFIED    = 0x20
set $RTF_DONE        = 0x40
set $RTF_DELCLONE    = 0x80
set $RTF_CLONING     = 0x100
set $RTF_XRESOLVE    = 0x200
set $RTF_LLINFO      = 0x400
set $RTF_STATIC      = 0x800
set $RTF_BLACKHOLE   = 0x1000
set $RTF_PROTO2      = 0x4000
set $RTF_PROTO1      = 0x8000
set $RTF_PRCLONING   = 0x10000
set $RTF_WASCLONED   = 0x20000
set $RTF_PROTO3      = 0x40000
set $RTF_PINNED      = 0x100000
set $RTF_LOCAL       = 0x200000
set $RTF_BROADCAST   = 0x400000
set $RTF_MULTICAST   = 0x800000
set $RTF_IFSCOPE     = 0x1000000
set $RTF_CONDEMNED   = 0x2000000

set $AF_INET = 2
set $AF_INET6 = 30
set $AF_LINK = 18

define rtentry_prdetails
	set $rt = (struct rtentry *)$arg0
	set $is_v6 = 0

	set $dst = (struct sockaddr *)$rt->rt_nodes->rn_u.rn_leaf.rn_Key
	if $dst->sa_family == $AF_INET
		showsockaddr_in $dst
		printf " "
	else
		if $dst->sa_family == $AF_INET6
			showsockaddr_in6 $dst
			printf " "
			set $is_v6 = 1
		else
			if $dst->sa_family == $AF_LINK
				showsockaddr_dl $dst
				printf " "
			else
				showsockaddr_unspec $dst
			end
		end
	end

	set $dst = (struct sockaddr *)$rt->rt_gateway
	if $dst->sa_family == $AF_INET
		showsockaddr_in $dst
		printf "   "
	else
		if $dst->sa_family == $AF_INET6
			set $is_v6 = 1
			showsockaddr_in6 $dst
			printf " "
		else
			if $dst->sa_family == $AF_LINK
				showsockaddr_dl $dst
				if $is_v6
					printf "                       "
				else
					printf " "
				end
			else
				showsockaddr_unspec $dst
			end
		end
	end

	if $rt->rt_flags & $RTF_WASCLONED
		if $kgm_lp64
			printf "%18p ", $rt->rt_parent
		else
			printf "%10p ", $rt->rt_parent
		end
	else
		if $kgm_lp64
			printf "                   "
		else
			printf "           "
		end
	end

	printf "%6u %8u ", $rt->rt_refcnt, $rt->rt_rmx.rmx_pksent

	if $rt->rt_flags & $RTF_UP
		printf "U"
	end
	if $rt->rt_flags & $RTF_GATEWAY
		printf "G"
	end
	if $rt->rt_flags & $RTF_HOST
		printf "H"
	end
	if $rt->rt_flags & $RTF_REJECT
		printf "R"
	end
	if $rt->rt_flags & $RTF_DYNAMIC
		printf "D"
	end
	if $rt->rt_flags & $RTF_MODIFIED
		printf "M"
	end
	if $rt->rt_flags & $RTF_CLONING
		printf "C"
	end
	if $rt->rt_flags & $RTF_PRCLONING
		printf "c"
	end
	if $rt->rt_flags & $RTF_LLINFO
		printf "L"
	end
	if $rt->rt_flags & $RTF_STATIC
		printf "S"
	end
	if $rt->rt_flags & $RTF_PROTO1
		printf "1"
	end
	if $rt->rt_flags & $RTF_PROTO2
		printf "2"
	end
	if $rt->rt_flags & $RTF_PROTO3
		printf "3"
	end
	if $rt->rt_flags & $RTF_WASCLONED
		printf "W"
	end
	if $rt->rt_flags & $RTF_BROADCAST
		printf "b"
	end
	if $rt->rt_flags & $RTF_MULTICAST
		printf "m"
	end
	if $rt->rt_flags & $RTF_XRESOLVE
		printf "X"
	end
	if $rt->rt_flags & $RTF_BLACKHOLE
		printf "B"
	end
	if $rt->rt_flags & $RTF_IFSCOPE
		printf "I"
	end

	printf "/%s%d", $rt->rt_ifp->if_name, $rt->rt_ifp->if_unit
end

set $RNF_ROOT   = 2

define _rttable_dump
	set $rnh = $arg0
	set $rn = (struct radix_node *)$rnh->rnh_treetop
	set $rnh_cnt = $rnh->rnh_cnt
		
	while $rn->rn_bit >= 0
		set $rn = $rn->rn_u.rn_node.rn_L
	end
	
	while 1
		set $base = (struct radix_node *)$rn
		while ($rn->rn_parent->rn_u.rn_node.rn_R == $rn) && ($rn->rn_flags & $RNF_ROOT) == 0
			set $rn = $rn->rn_parent
		end
		set $rn = $rn->rn_parent->rn_u.rn_node.rn_R
		while $rn->rn_bit >= 0
			set $rn = $rn->rn_u.rn_node.rn_L
		end
		set $next = $rn
		while $base != 0
			set $rn = $base
			set $base = $rn->rn_u.rn_leaf.rn_Dupedkey
			if ($rn->rn_flags & $RNF_ROOT) == 0
				
				set $rt = (struct rtentry *)$rn
				
				if $kgm_lp64
					printf "%18p ", $rt
				else
					printf "%10p ", $rt
				end
				rtentry_prdetails $rt
				printf "\n"
				
			end
		end
		set $rn = $next
		if ($rn->rn_flags & $RNF_ROOT) != 0
			loop_break
		end
	end
end


define show_rt_inet
	if $kgm_lp64
		printf " rtentry           dst             gw                parent             Refs   Use      flags/if\n"
		printf " ----------------- --------------- ----------------- ------------------ ------ -------- -----------\n"
	else
		printf " rtentry   dst             gw                parent     Refs   Use      flags/if\n"
		printf " --------- --------------- ----------------- ---------- ------ -------- -----------\n"
	end
	_rttable_dump rt_tables[2]
end

document show_rt_inet
Syntax: (gdb) show_rt_inet
| Show the entries of the IPv4 routing table.
end

define show_rt_inet6
	if $kgm_lp64
		printf " rtentry           dst                                     gw                                      parent             Refs   Use      flags/if\n"
		printf " ----------------- --------------------------------------- --------------------------------------- ------------------ ------ -------- -----------\n"
	else
		printf " rtentry   dst                                     gw                                      parent     Refs   Use      flags/if\n"
		printf " --------- --------------------------------------- --------------------------------------- ---------- ------ -------- -----------\n"
	end
	_rttable_dump rt_tables[30]
end

document show_rt_inet6
Syntax: (gdb) show_rt_inet6
| Show the entries of the IPv6 routing table.
end

define rtentry_trash
	set $rtd = (struct rtentry_dbg *)rttrash_head.tqh_first
	set $cnt = 0
	while $rtd != 0
		if $cnt == 0
		    if $kgm_lp64
				printf "                rtentry ref   hold   rele             dst    gw             parent flags/if\n"
				printf "      ----------------- --- ------ ------ --------------- ----- ------------------ -----------\n"
			else
				printf "        rtentry ref   hold   rele             dst    gw     parent flags/if\n"
				printf "      --------- --- ------ ------ --------------- ----- ---------- -----------\n"
			end
		end
		printf "%4d: %p %3d %6d %6d ", $cnt + 1, $rtd, \
		    $rtd->rtd_refhold_cnt - $rtd->rtd_refrele_cnt, \
		    $rtd->rtd_refhold_cnt, $rtd->rtd_refrele_cnt
		rtentry_prdetails $rtd
		printf "\n"
		set $rtd = $rtd->rtd_trash_link.tqe_next
		set $cnt = $cnt + 1
	end
end

document rtentry_trash
Syntax: (gdb) rtentry_trash
| Walk the list of trash route entries; this requires route entry
| debugging to be turned on, by setting the appropriate flags to the
| "rte_debug" boot-args parameter.
end

set $CTRACE_STACK_SIZE = ctrace_stack_size
set $CTRACE_HIST_SIZE = ctrace_hist_size

define rtentry_showdbg
	set $rtd = (struct rtentry_dbg *)$arg0
	set $cnt = 0

	printf "Total holds:\t%d\n", $rtd->rtd_refhold_cnt
	printf "Total releases:\t%d\n", $rtd->rtd_refrele_cnt

	set $ix = 0
	while $ix < $CTRACE_STACK_SIZE
		set $kgm_pc = $rtd->rtd_alloc.pc[$ix]
		if $kgm_pc != 0
			if $ix == 0
				printf "\nAlloc (thread %p):\n", \
				    $rtd->rtd_alloc.th
			end
			printf "%4d: ", $ix + 1
			pcprint $kgm_pc
			printf "\n"
		end
		set $ix = $ix + 1
	end
	set $ix = 0
	while $ix < $CTRACE_STACK_SIZE
		set $kgm_pc = $rtd->rtd_free.pc[$ix]
		if $kgm_pc != 0
			if $ix == 0
				printf "\nFree: (thread %p)\n", \
				    $rtd->rtd_free.th
			end
			printf "%4d: ", $ix + 1
			pcprint $kgm_pc
			printf "\n"
		end
		set $ix = $ix + 1
	end
	while $cnt < $CTRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $rtd->rtd_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, $rtd->rtd_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $CTRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $rtd->rtd_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, $rtd->rtd_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end

	printf "\nTotal locks:\t%d\n", $rtd->rtd_lock_cnt
	printf "Total unlocks:\t%d\n", $rtd->rtd_unlock_cnt

	set $cnt = 0
	while $cnt < $CTRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $rtd->rtd_lock[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nLock [%d] (thread %p):\n",\
					    $cnt, $rtd->rtd_lock[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $CTRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $rtd->rtd_unlock[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nUnlock [%d] (thread %p):\n",\
					    $cnt, $rtd->rtd_unlock[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document rtentry_showdbg
Syntax: (gdb) rtentry_showdbg <addr>
| Given a route entry structure address, print the debug information
| related to it.  This requires route entry debugging to be turned
| on, by setting the appropriate flags to the "rte_debug" boot-args
| parameter.
end

set $INIFA_TRACE_HIST_SIZE = inifa_trace_hist_size

define inifa_showdbg
	set $inifa = (struct in_ifaddr_dbg *)$arg0
	set $cnt = 0

	printf "Total holds:\t%d\n", $inifa->inifa_refhold_cnt
	printf "Total releases:\t%d\n", $inifa->inifa_refrele_cnt

	set $ix = 0
	while $ix < $CTRACE_STACK_SIZE
		set $kgm_pc = $inifa->inifa_alloc.pc[$ix]
		if $kgm_pc != 0
			if $ix == 0
				printf "\nAlloc (thread %p):\n", \
				    $inifa->inifa_alloc.th
			end
			printf "%4d: ", $ix + 1
			pcprint $kgm_pc
			printf "\n"
		end
		set $ix = $ix + 1
	end
	set $ix = 0
	while $ix < $CTRACE_STACK_SIZE
		set $kgm_pc = $inifa->inifa_free.pc[$ix]
		if $kgm_pc != 0
			if $ix == 0
				printf "\nFree: (thread %p)\n", \
				    $inifa->inifa_free.th
			end
			printf "%4d: ", $ix + 1
			pcprint $kgm_pc
			printf "\n"
		end
		set $ix = $ix + 1
	end
	while $cnt < $INIFA_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $inifa->inifa_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, $inifa->inifa_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $INIFA_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $inifa->inifa_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, $inifa->inifa_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document inifa_showdbg
Syntax: (gdb) inifa_showdbg <addr>
| Given an IPv4 interface structure address, print the debug information
| related to it.  This requires interface address debugging to be turned
| on, by setting the appropriate flags to the "ifa_debug" boot-args
| parameter.
end

set $IN6IFA_TRACE_HIST_SIZE = in6ifa_trace_hist_size

define in6ifa_showdbg
	set $in6ifa = (struct in6_ifaddr_dbg *)$arg0
	set $cnt = 0

	printf "Total holds:\t%d\n", $in6ifa->in6ifa_refhold_cnt
	printf "Total releases:\t%d\n", $in6ifa->in6ifa_refrele_cnt

	set $ix = 0
	while $ix < $CTRACE_STACK_SIZE
		set $kgm_pc = $in6ifa->in6ifa_alloc.pc[$ix]
		if $kgm_pc != 0
			if $ix == 0
				printf "\nAlloc (thread %p):\n", \
				    $in6ifa->in6ifa_alloc.th
			end
			printf "%4d: ", $ix + 1
			pcprint $kgm_pc
			printf "\n"
		end
		set $ix = $ix + 1
	end
	set $ix = 0
	while $ix < $CTRACE_STACK_SIZE
		set $kgm_pc = $in6ifa->in6ifa_free.pc[$ix]
		if $kgm_pc != 0
			if $ix == 0
				printf "\nFree: (thread %p)\n", \
				    $in6ifa->in6ifa_free.th
			end
			printf "%4d: ", $ix + 1
			pcprint $kgm_pc
			printf "\n"
		end
		set $ix = $ix + 1
	end
	while $cnt < $IN6IFA_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $in6ifa->in6ifa_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					  $cnt, $in6ifa->in6ifa_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $IN6IFA_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $in6ifa->in6ifa_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					  $cnt, $in6ifa->in6ifa_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document in6ifa_showdbg
Syntax: (gdb) in6ifa_showdbg <addr>
| Given an IPv6 interface structure address, print the debug information
| related to it.  This requires interface address debugging to be turned
| on, by setting the appropriate flags to the "ifa_debug" boot-args
| parameter.
end

set $IFMA_TRACE_HIST_SIZE = ifma_trace_hist_size

define ifma_showdbg
	set $ifma = (struct ifmultiaddr_dbg *)$arg0
	set $cnt = 0

	printf "Total holds:\t%d\n", $ifma->ifma_refhold_cnt
	printf "Total releases:\t%d\n", $ifma->ifma_refrele_cnt

	while $cnt < $IFMA_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $ifma->ifma_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					  $cnt, $ifma->ifma_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $IFMA_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $ifma->ifma_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					  $cnt, $ifma->ifma_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document ifma_showdbg
Syntax: (gdb) ifma_showdbg <addr>
| Given a link multicast structure address, print the debug information
| related to it.  This requires interface address debugging to be turned
| on, by setting the appropriate flags to the "ifa_debug" boot-args
| parameter.
end

set $INM_TRACE_HIST_SIZE = inm_trace_hist_size

define inm_showdbg
	set $inm = (struct in_multi_dbg *)$arg0
	set $cnt = 0

	printf "Total holds:\t%d\n", $inm->inm_refhold_cnt
	printf "Total releases:\t%d\n", $inm->inm_refrele_cnt

	while $cnt < $INM_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $inm->inm_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					  $cnt, $inm->inm_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $INM_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $inm->inm_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					  $cnt, $inm->inm_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document inm_showdbg
Syntax: (gdb) inm_showdbg <addr>
| Given an IPv4 multicast structure address, print the debug information
| related to it.  This requires interface address debugging to be turned
| on, by setting the appropriate flags to the "ifa_debug" boot-args
| parameter.
end

set $IF_REF_TRACE_HIST_SIZE = if_ref_trace_hist_size

define ifpref_showdbg
	set $dl_if = (struct dlil_ifnet_dbg *)$arg0
	set $cnt = 0

	printf "Total references:\t%d\n", $dl_if->dldbg_if_refhold_cnt
	printf "Total releases:\t\t%d\n", $dl_if->dldbg_if_refrele_cnt

	while $cnt < $IF_REF_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $dl_if->dldbg_if_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, \
					    $dl_if->dldbg_if_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $IF_REF_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $dl_if->dldbg_if_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, \
					    $dl_if->dldbg_if_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document ifpref_showdbg
Syntax: (gdb) ifpref_showdbg <addr>
| Given an ifnet structure address, print the debug information
| related to its refcnt.  This requires ifnet debugging to be turned
| on, by setting the appropriate flags to the "ifnet_debug" boot-args
| parameter.
end

define in6ifa_trash
	set $ifa = (struct in6_ifaddr_dbg *)in6ifa_trash_head.tqh_first
	set $cnt = 0
	while $ifa != 0
		if $cnt == 0
		    if $kgm_lp64
				printf "                in6_ifa ref   hold   rele\n"
				printf "      ----------------- --- ------ ------\n"
			else
				printf "        in6_ifa ref   hold   rele\n"
				printf "      --------- --- ------ ------\n"
			end
		end
		printf "%4d: %p %3d %6d %6d ", $cnt + 1, $ifa, \
		    $ifa->in6ifa_refhold_cnt - $ifa->in6ifa_refrele_cnt, \
		    $ifa->in6ifa_refhold_cnt, $ifa->in6ifa_refrele_cnt
		showsockaddr_in6 $ifa->in6ifa.ia_ifa.ifa_addr
		printf "\n"
		set $ifa = $ifa->in6ifa_trash_link.tqe_next
		set $cnt = $cnt + 1
	end
end

set $NDPR_TRACE_HIST_SIZE = ndpr_trace_hist_size

define ndpr_showdbg
	set $ndpr = (struct nd_prefix_dbg *)$arg0
	set $cnt = 0

	printf "Total references:\t%d\n", $ndpr->ndpr_refhold_cnt
	printf "Total releases:\t\t%d\n", $ndpr->ndpr_refrele_cnt

	while $cnt < $NDPR_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $ndpr->ndpr_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, \
					    $ndpr->ndpr_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $NDPR_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $ndpr->ndpr_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, \
					    $ndpr->ndpr_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document ndpr_showdbg
Syntax: (gdb) ndpr_showdbg <addr>
| Given a nd_prefix structure address, print the debug information
| related to its refcnt.  This requires the interface address debugging
| to be turned on, by setting the appropriate flags to the "ifa_debug"
| boot-args parameter.
end

set $NDDR_TRACE_HIST_SIZE = nddr_trace_hist_size

define nddr_showdbg
	set $nddr = (struct nd_defrouter_dbg *)$arg0
	set $cnt = 0

	printf "Total references:\t%d\n", $nddr->nddr_refhold_cnt
	printf "Total releases:\t\t%d\n", $nddr->nddr_refrele_cnt

	while $cnt < $NDDR_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $nddr->nddr_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, \
					    $nddr->nddr_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $NDDR_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $nddr->nddr_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, \
					    $nddr->nddr_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document nddr_showdbg
Syntax: (gdb) nddr_showdbg <addr>
| Given a nd_defrouter structure address, print the debug information
| related to its refcnt.  This requires the interface address debugging
| to be turned on, by setting the appropriate flags to the "ifa_debug"
| boot-args parameter.
end
set $IMO_TRACE_HIST_SIZE = imo_trace_hist_size

define imo_showdbg
	set $imo = (struct ip_moptions_dbg *)$arg0
	set $cnt = 0

	printf "Total references:\t%d\n", $imo->imo_refhold_cnt
	printf "Total releases:\t\t%d\n", $imo->imo_refrele_cnt

	while $cnt < $IMO_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $imo->imo_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, \
					    $imo->imo_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $IMO_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $imo->imo_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, \
					    $imo->imo_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document imo_showdbg
Syntax: (gdb) imo_showdbg <addr>
| Given a ip_moptions structure address, print the debug information
| related to its refcnt.  This requires the interface address debugging
| to be turned on, by setting the appropriate flags to the "ifa_debug"
| boot-args parameter.
end

set $IM6O_TRACE_HIST_SIZE = im6o_trace_hist_size

define im6o_showdbg
	set $im6o = (struct ip6_moptions_dbg *)$arg0
	set $cnt = 0

	printf "Total references:\t%d\n", $im6o->im6o_refhold_cnt
	printf "Total releases:\t\t%d\n", $im6o->im6o_refrele_cnt

	while $cnt < $IM6O_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $im6o->im6o_refhold[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nHold [%d] (thread %p):\n", \
					    $cnt, \
					    $im6o->im6o_refhold[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
	set $cnt = 0
	while $cnt < $IM6O_TRACE_HIST_SIZE
		set $ix = 0
		while $ix < $CTRACE_STACK_SIZE
			set $kgm_pc = $im6o->im6o_refrele[$cnt].pc[$ix]
			if $kgm_pc != 0
				if $ix == 0
					printf "\nRelease [%d] (thread %p):\n",\
					    $cnt, \
					    $im6o->im6o_refrele[$cnt].th
				end
				printf "%4d: ", $ix + 1
				pcprint $kgm_pc
				printf "\n"
			end
			set $ix = $ix + 1
		end
		set $cnt = $cnt + 1
	end
end

document im6o_showdbg
Syntax: (gdb) im6o_showdbg <addr>
| Given a ip6_moptions structure address, print the debug information
| related to its refcnt.  This requires the interface address debugging
| to be turned on, by setting the appropriate flags to the "ifa_debug"
| boot-args parameter.
end

document in6ifa_trash
Syntax: (gdb) in6ifa_trash
| Walk the list of trash in6_ifaddr entries; this requires interface
| address debugging to be turned on, by setting the appropriate flags
| to the "ifa_debug" boot-args parameter.
end

define inifa_trash
	set $ifa = (struct in_ifaddr_dbg *)inifa_trash_head.tqh_first
	set $cnt = 0
	while $ifa != 0
		if $cnt == 0
		    if $kgm_lp64
				printf "                 in_ifa ref   hold   rele\n"
				printf "      ----------------- --- ------ ------\n"
			else
				printf "         in_ifa ref   hold   rele\n"
				printf "      --------- --- ------ ------\n"
			end
		end
		printf "%4d: %p %3d %6d %6d ", $cnt + 1, $ifa, \
		    $ifa->inifa_refhold_cnt - $ifa->inifa_refrele_cnt, \
		    $ifa->inifa_refhold_cnt, $ifa->inifa_refrele_cnt
		showsockaddr_in $ifa->inifa.ia_ifa.ifa_addr
		printf "\n"
		set $ifa = $ifa->inifa_trash_link.tqe_next
		set $cnt = $cnt + 1
	end
end

document inifa_trash
Syntax: (gdb) inifa_trash
| Walk the list of trash in_ifaddr entries; this requires interface
| address debugging to be turned on, by setting the appropriate flags
| to the "ifa_debug" boot-args parameter.
end

define ifma_trash
	set $ifma = (struct ifmultiaddr_dbg *)ifma_trash_head.tqh_first
	set $cnt = 0
	while $ifma != 0
		if $cnt == 0
		    if $kgm_lp64
				printf "                   ifma ref   hold   rele\n"
				printf "      ----------------- --- ------ ------\n"
			else
				printf "          ifma  ref   hold   rele\n"
				printf "      --------- --- ------ ------\n"
			end
		end
		printf "%4d: %p %3d %6d %6d ", $cnt + 1, $ifma, \
		    $ifma->ifma_refhold_cnt - $ifma->ifma_refrele_cnt, \
		    $ifma->ifma_refhold_cnt, $ifma->ifma_refrele_cnt
		showsockaddr $ifma->ifma.ifma_addr
		printf " @ %s%d", $ifma->ifma.ifma_ifp->if_name, \
		    $ifma->ifma.ifma_ifp->if_unit
		printf "\n"
		set $ifma = $ifma->ifma_trash_link.tqe_next
		set $cnt = $cnt + 1
	end
end

document ifma_trash
Syntax: (gdb) ifma_trash
| Walk the list of trash ifmultiaddr entries; this requires interface
| address debugging to be turned on, by setting the appropriate flags
| to the "ifa_debug" boot-args parameter.
end

define inm_trash
	set $inm = (struct in_multi_dbg *)inm_trash_head.tqh_first
	set $cnt = 0
	while $inm != 0
		if $cnt == 0
		    if $kgm_lp64
				printf "                    inm ref   hold   rele\n"
				printf "      ----------------- --- ------ ------\n"
			else
				printf "            inm ref   hold   rele\n"
				printf "      --------- --- ------ ------\n"
			end
		end
		printf "%4d: %p %3d %6d %6d ", $cnt + 1, $inm, \
		    $inm->inm_refhold_cnt - $inm->inm_refrele_cnt, \
		    $inm->inm_refhold_cnt, $inm->inm_refrele_cnt
		show_in_addr &($inm->inm.inm_addr)
		printf "\n"
		set $inm = $inm->inm_trash_link.tqe_next
		set $cnt = $cnt + 1
	end
end

document inm_trash
Syntax: (gdb) inm_trash
| Walk the list of trash in_multi entries; this requires interface
| address debugging to be turned on, by setting the appropriate flags
| to the "ifa_debug" boot-args parameter.
end

define in6m_trash
	set $in6m = (struct in6_multi_dbg *)in6m_trash_head.tqh_first
	set $cnt = 0
	while $in6m != 0
		if $cnt == 0
		    if $kgm_lp64
				printf "                   in6m ref   hold   rele\n"
				printf "      ----------------- --- ------ ------\n"
			else
				printf "           in6m ref   hold   rele\n"
				printf "      --------- --- ------ ------\n"
			end
		end
		printf "%4d: %p %3d %6d %6d ", $cnt + 1, $in6m, \
		    $in6m->in6m_refhold_cnt - $in6m->in6m_refrele_cnt, \
		    $in6m->in6m_refhold_cnt, $in6m->in6m_refrele_cnt
		show_in_addr &($in6m->in6m.in6m_addr)
		printf "\n"
		set $in6m = $in6m->in6m_trash_link.tqe_next
		set $cnt = $cnt + 1
	end
end

document in6m_trash
Syntax: (gdb) in6m_trash
| Walk the list of trash in6_multi entries; this requires interface
| address debugging to be turned on, by setting the appropriate flags
| to the "ifa_debug" boot-args parameter.
end

#
# print all OSMalloc stats 

define ostag_print
set $kgm_tagp = (OSMallocTag)$arg0
printf "0x%08x: ", $kgm_tagp
printf "%8d ",$kgm_tagp->OSMT_refcnt
printf "%8x ",$kgm_tagp->OSMT_state
printf "%8x ",$kgm_tagp->OSMT_attr
printf "%s ",$kgm_tagp->OSMT_name
printf "\n"
end


define showosmalloc 
printf "TAG          COUNT     STATE     ATTR     NAME\n"
set $kgm_tagheadp = (struct _OSMallocTag_ *)&OSMalloc_tag_list
    set $kgm_tagptr = (struct _OSMallocTag_ * )($kgm_tagheadp->OSMT_link.next)
    while $kgm_tagptr != $kgm_tagheadp
	ostag_print $kgm_tagptr
	set $kgm_tagptr = (struct _OSMallocTag_ *)$kgm_tagptr->OSMT_link.next
    end
	printf "\n"
end
document showosmalloc
Syntax: (gdb) showosmalloc
| Print the outstanding allocation count by OSMallocTags.
end


define systemlog
    if msgbufp->msg_bufc[msgbufp->msg_bufx] == 0 \
       && msgbufp->msg_bufc[0] != 0
        # The buffer hasn't wrapped, so take the easy (and fast!) path
        printf "%s", msgbufp->msg_bufc
    else
        set $kgm_msgbuf = *msgbufp
        set $kgm_syslog_bufsize = $kgm_msgbuf.msg_size
        set $kgm_syslog_bufend = $kgm_msgbuf.msg_bufx
        if $kgm_syslog_bufend >= $kgm_syslog_bufsize
            set $kgm_syslog_bufend = 0
        end
    
        # print older messages from msg_bufx to end of buffer
        set $kgm_i = $kgm_syslog_bufend
        while $kgm_i < $kgm_syslog_bufsize
            set $kgm_syslog_char = $kgm_msgbuf.msg_bufc[$kgm_i]
            if $kgm_syslog_char == 0
                # break out of loop
                set $kgm_i = $kgm_syslog_bufsize
            else
                printf "%c", $kgm_syslog_char
            end
            set $kgm_i = $kgm_i + 1
        end
        
        # print newer messages from start of buffer to msg_bufx
        set $kgm_i = 0
        while $kgm_i < $kgm_syslog_bufend
            set $kgm_syslog_char = $kgm_msgbuf.msg_bufc[$kgm_i]
            if $kgm_syslog_char != 0
                printf "%c", $kgm_syslog_char
            end
            set $kgm_i = $kgm_i + 1
        end
    end
    printf "\n"
end
document systemlog
| Syntax: systemlog
| Display the kernel's printf ring buffer
end


define hexdump
	set $kgm_addr = (unsigned char *)$arg0
	set $kgm_len = $arg1
	while $kgm_len > 0
		showptr $kgm_addr
		printf ": "
		set $kgm_i = 0
		while $kgm_i < 16
			printf "%02x ", *($kgm_addr+$kgm_i)
			set $kgm_i += 1
		end
		printf " |"
		set $kgm_i = 0
		while $kgm_i < 16
			set $kgm_temp = *($kgm_addr+$kgm_i)
			if $kgm_temp < 32 || $kgm_temp >= 127
				printf "."
			else
				printf "%c", $kgm_temp
			end
			set $kgm_i += 1
		end
		printf "|\n"
		set $kgm_addr += 16
		set $kgm_len -= 16
	end
end
document hexdump
| Show the contents of memory as a hex/ASCII dump
| The following is the syntax:
| (gdb) hexdump <address> <length>
end


define printcolonhex
    if ($argc == 2) 
	set $addr = $arg0
	set $count = $arg1
	set $li = 0
	while ($li < $count)
	    if ($li == 0)
			printf "%02x", (u_char)$addr[$li]
	    end
	    if ($li != 0)
			printf ":%02x", (u_char)$addr[$li]
	    end
	    set $li = $li + 1
	end
    end
end

define showsockaddr_dl
    set $sdl = (struct sockaddr_dl *)$arg0
    if ($sdl == 0)
		printf "(null)           "
    else
	    if $sdl->sdl_nlen == 0 && $sdl->sdl_alen == 0 && $sdl->sdl_slen == 0
    		printf "link#%3d         ", $sdl->sdl_index
	    else
			set $addr = $sdl->sdl_data + $sdl->sdl_nlen
			set $count = $sdl->sdl_alen
			printcolonhex $addr $count
		end
    end
end

define showsockaddr_unspec
    set $sockaddr = (struct sockaddr *)$arg0
    set $addr = $sockaddr->sa_data
    set $count = $sockaddr->sa_len - 2
    printcolonhex $addr $count
end

define showsockaddr_at
    set $sockaddr = (struct sockaddr *)$arg0
    set $addr = $sockaddr->sa_data
    set $count = $sockaddr->sa_len - 2
    printcolonhex $addr $count
end

define show_in_addr
    set $ia = (unsigned char *)$arg0
    printf "%3u.%03u.%03u.%03u", $ia[0], $ia[1], $ia[2], $ia[3]
end

define showsockaddr_in
    set $sin = (struct sockaddr_in *)$arg0
    set $sa_bytes = (unsigned char *)&($sin->sin_addr)
    show_in_addr $sa_bytes
end

define show_in6_addr
    set $ia = (unsigned char *)$arg0
    printf "%2x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", \
    	$ia[0], $ia[1], $ia[2], $ia[3], $ia[4], $ia[5], $ia[6], $ia[7], $ia[8], $ia[9], $ia[10], $ia[11], $ia[12], $ia[13], $ia[14], $ia[15]
end

define showsockaddr_in6
    set $sin6 = (struct sockaddr_in6 *)$arg0
    set $sa_bytes = $sin6->sin6_addr.__u6_addr.__u6_addr8
    show_in6_addr $sa_bytes
end

define showsockaddr_un
	set $sun = (struct sockaddr_un *)$arg0
	if $sun == 0
		printf "(null)"
	else
		if $sun->sun_path[0] == 0
			printf "\"\""
		else
			printf "%s", $sun->sun_path
		end
	end
end

define showifmultiaddrs
   set $ifp = (struct ifnet *)$arg0
   set $if_multi = (struct ifmultiaddr *)$ifp->if_multiaddrs->lh_first
   set $mymulti = $if_multi
   set $myi = 0
   while ($mymulti != 0)
	printf "%2d. %p ", $myi, $mymulti
	set $sa_family = $mymulti->ifma_addr.sa_family
	if ($sa_family == 2)
	    if ($mymulti->ifma_ll != 0)
	    	showsockaddr_dl $mymulti->ifma_ll->ifma_addr
	        printf " "
	    end
	    showsockaddr_in $mymulti->ifma_addr
	end
	if ($sa_family == 30)
	    if ($mymulti->ifma_ll != 0)
	    	showsockaddr_dl $mymulti->ifma_ll->ifma_addr
	        printf " "
	    end
	    showsockaddr_in6 $mymulti->ifma_addr
	end
 	if ($sa_family == 18)
	    showsockaddr_dl $mymulti->ifma_addr
	end
	if ($sa_family == 0)
	    showsockaddr_unspec $mymulti->ifma_addr 6
	end
	printf " [%d]", $mymulti->ifma_refcount
	printf "\n"
	set $mymulti = $mymulti->ifma_link.le_next
	set $myi = $myi + 1
   end
end

document showifmultiaddrs
Syntax showifmultiaddrs <ifp>
| show the (struct ifnet).if_multiaddrs list of multicast addresses for the given ifp
end

define showinmultiaddrs
   set $in_multi = (struct in_multi *)(in_multihead->lh_first)
   set $mymulti = $in_multi
   set $myi = 0
   while ($mymulti != 0)
	set $ifp = (struct ifnet *)$mymulti->inm_ifp
	printf "%2d. %p ", $myi, $mymulti
	show_in_addr &($mymulti->inm_addr)
	printf " (ifp %p [%s%d] ifma %p) ", $ifp, $ifp->if_name, \
	    $ifp->if_unit, $mymulti->inm_ifma
	printf "\n"
	set $mymulti = $mymulti->inm_link.le_next
	set $myi = $myi + 1
   end
end

document showinmultiaddrs
Syntax showinmultiaddrs
| show the contents of IPv4 multicast address records
end

define showin6multiaddrs
   set $in6_multi = (struct in6_multi *)(in6_multihead->lh_first)
   set $mymulti = $in6_multi
   set $myi = 0
   while ($mymulti != 0)
	set $ifp = (struct ifnet *)$mymulti->in6m_ifp
	printf "%2d. %p ", $myi, $mymulti
	show_in6_addr &($mymulti->in6m_addr)
	printf " (ifp %p [%s%d] ifma %p) ", $ifp, $ifp->if_name, \
	    $ifp->if_unit, $mymulti->in6m_ifma
	printf "\n"
	set $mymulti = $mymulti->in6m_entry.le_next
	set $myi = $myi + 1
   end
end

document showin6multiaddrs
Syntax showin6multiaddrs
| show the contents of IPv6 multicast address records
end

define showsockaddr
    set $mysock = (struct sockaddr *)$arg0
    set $showsockaddr_handled = 0
    if ($mysock == 0)
	printf "(null)"
    else
	if ($mysock->sa_family == 0)
		printf "UNSPC"
	    showsockaddr_unspec $mysock
	    set $showsockaddr_handled = 1
	end
	if ($mysock->sa_family == 1)
		printf "UNIX "
	    showsockaddr_un $mysock
	    set $showsockaddr_handled = 1
	end
	if ($mysock->sa_family == 2)
		printf "INET "
	    showsockaddr_in $mysock
	    set $showsockaddr_handled = 1
	end
	if ($mysock->sa_family == 30)
		printf "INET6 "
	    showsockaddr_in6 $mysock
	    set $showsockaddr_handled = 1
	end
	if ($mysock->sa_family == 18)
		printf "LINK "
	    showsockaddr_dl $mysock
	    set $showsockaddr_handled = 1
	end
	if ($mysock->sa_family == 16)
		printf "ATLK "
	    showsockaddr_at $mysock
	    set $showsockaddr_handled = 1
	end
	if ($showsockaddr_handled == 0)
	    printf "FAM %d ", $mysock->sa_family
	    set $addr = $mysock->sa_data
	    set $count = $mysock->sa_len
	    printcolonhex $addr $count
	end
    end
end

define showifflags
	set $flags = (u_short)$arg0
	set $first = 1
	printf "<"
	if ($flags & 0x1)
	    printf "UP"
	    set $first = 0
	end
	if ($flags & 0x2)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "BROADCAST"
	end
	if ($flags & 0x4)
	    printf "DEBUG"
	end
	if ($flags & 0x8)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "LOOPBACK"
	end
	if ($flags & 0x10)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "POINTTOPOINT"
	end
##	if ($flags & 0x20)
##	    if ($first == 1)
#		set $first = 0
##	    else
#	    	printf ","
#	    end
#	    printf "NOTRAILERS"
#	end
	if ($flags & 0x40)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "RUNNING"
	end
	if ($flags & 0x80)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "NOARP"
	end
	if ($flags & 0x100)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "PROMISC"
	end
	if ($flags & 0x200)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "ALLMULTI"
	end
	if ($flags & 0x400)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "OACTIVE"
	end
	if ($flags & 0x800)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "SIMPLEX"
	end
	if ($flags & 0x1000)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "LINK0"
	end
	if ($flags & 0x2000)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "LINK1"
	end
	if ($flags & 0x4000)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "LINK2-ALTPHYS"
	end
	if ($flags & 0x8000)
	    if ($first == 1)
		set $first = 0
	    else
	    	printf ","
	    end
	    printf "MULTICAST"
	end
	printf ">"
end

define showifaddrs
   set $ifp = (struct ifnet *)$arg0
   set $myifaddr = (struct ifaddr *)$ifp->if_addrhead->tqh_first
   set $myi = 0
   while ($myifaddr != 0)
	printf "\t%d. %p ", $myi, $myifaddr
	showsockaddr $myifaddr->ifa_addr
	printf " [%d]\n", $myifaddr->ifa_refcnt
	set $myifaddr = $myifaddr->ifa_link->tqe_next
	set $myi = $myi + 1
   end
end

document showifaddrs
Syntax: showifaddrs <ifp>
| show the (struct ifnet).if_addrhead list of addresses for the given ifp
end

define ifconfig
   set $ifconfig_all = 0
   if ($argc == 1)
	set $ifconfig_all = 1
   end
   set $ifp = (struct ifnet *)(ifnet_head->tqh_first)
   while ($ifp != 0)
	printf "%s%d: flags=%hx", $ifp->if_name, $ifp->if_unit, (u_short)$ifp->if_flags
	showifflags $ifp->if_flags
	printf " index %d", $ifp->if_index
	printf " mtu %d\n", $ifp->if_data.ifi_mtu
	printf "\t(struct ifnet *)"
    showptr $ifp
    printf "\n"
	if ($ifconfig_all == 1) 
	   showifaddrs $ifp
    end
	set $ifp = $ifp->if_link->tqe_next
   end
end
document ifconfig
Syntax: (gdb) ifconfig
| display ifconfig-like output, and print the (struct ifnet *) pointers for further inspection
end

set $DLIF_INUSE	= 0x1
set $DLIF_REUSE	= 0x2

define showifnets
	set $all = 0
	if ($argc == 1)
		set $all = 1
	end
	set $dlifp = (struct dlil_ifnet *)(dlil_ifnet_head->tqh_first)
	while ($dlifp != 0)
		set $ifp = (struct ifnet *)$dlifp
		if ($dlifp->dl_if_flags & $DLIF_REUSE)
			printf "*"
		end
		if ($dlifp->dl_if_flags & $DLIF_INUSE)
			printf "%s%d: ", $ifp->if_name, $ifp->if_unit
		else
			printf "[%s%d]: ", $ifp->if_name, $ifp->if_unit
		end
		printf "flags=%hx", (u_short)$ifp->if_flags
		showifflags $ifp->if_flags
		printf " index %d", $ifp->if_index
		printf " mtu %d\n", $ifp->if_data.ifi_mtu
		printf "\t(struct ifnet *)"
		showptr $ifp
		printf "\n"
		if ($all == 1) 
			showifaddrs $ifp
		end
		set $dlifp = $dlifp->dl_if_link->tqe_next
	end
end

document showifnets
Syntax: (gdb) showifnets
| Display ifconfig-like output for all attached and detached interfaces
end

define _show_unix_domain_socket
	set $so = (struct socket *)$arg0
	set $pcb = (struct unpcb *)$so->so_pcb
	if $pcb == 0
		printf "unpcb: (null) "
	else
		printf "unpcb: %p ", $pcb
		printf "unp_vnode: %p ", $pcb->unp_vnode
		printf "unp_conn: %p ", $pcb->unp_conn
		printf "unp_addr: "
		showsockaddr_un $pcb->unp_addr
	end
end

define _show_in_port
	set $str = (unsigned char *)$arg0
	set $port = *(unsigned short *)$arg0
	
	if (((($port & 0xff00) >> 8) == $str[0])) && ((($port & 0x00ff) == $str[1]))
		#printf "big endian "
		printf ":%d ", $port
	else
		#printf "little endian "
		printf ":%d ", (($port & 0xff00) >> 8) | (($port & 0x00ff) << 8)
	end
end

define _show_in_addr_4in6
	set $ia = (unsigned char *)$arg0
	if $ia
	    printf "%3u.%03u.%03u.%03u", $ia[0], $ia[1], $ia[2], $ia[3]
	end
end

define _show_in6_addr
	set $ia = (unsigned char *)$arg0
	if $ia
		printf "%2x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x", \
			$ia[0], $ia[1], $ia[2], $ia[3], $ia[4], $ia[5], $ia[6], $ia[7], \
			$ia[8], $ia[9], $ia[10], $ia[11], $ia[12], $ia[13], $ia[14], $ia[15]
	end
end

define _showtcpstate
	set $tp = (struct tcpcb *)$arg0
	if $tp
		if $tp->t_state == 0
			printf "CLOSED      "
		end
		if $tp->t_state == 1
			printf "LISTEN      "
		end
		if $tp->t_state == 2
			printf "SYN_SENT    "
		end
		if $tp->t_state == 3
			printf "SYN_RCVD    "
		end
		if $tp->t_state == 4
			printf "ESTABLISHED "
		end
		if $tp->t_state == 5
			printf "CLOSE_WAIT   "
		end
		if $tp->t_state == 6
			printf "FIN_WAIT_1   "
		end
		if $tp->t_state == 7
			printf "CLOSING      "
		end
		if $tp->t_state == 8
			printf "LAST_ACK     "
		end
		if $tp->t_state == 9
			printf "FIN_WAIT_2   "
		end
		if $tp->t_state == 10
			printf "TIME_WAIT    "
		end
	end
end

define _showsockprotocol
	set $so = (struct socket *)$arg0
	set $inpcb = (struct inpcb *)$so->so_pcb

	if $so->so_proto->pr_protocol == 6
		printf "TCP "
		_showtcpstate $inpcb->inp_ppcb
	end
	if $so->so_proto->pr_protocol == 17
		printf "UDP "
	end
	if $so->so_proto->pr_protocol == 1
		printf "ICMP "
	end
	if $so->so_proto->pr_protocol == 254
		printf "DIVERT "
	end
	if $so->so_proto->pr_protocol == 255
		printf "RAW "
	end
end

define _show_ipv4_socket
	set $so = (struct socket *)$arg0
	set $inpcb = (struct inpcb *)$so->so_pcb
	if $inpcb == 0
		printf "inpcb: (null) "
	else
		printf "inpcb: %p ", $inpcb
		
		_showsockprotocol $so

		_show_in_addr_4in6  &$inpcb->inp_dependladdr.inp46_local
		_show_in_port  &$inpcb->inp_lport
		printf "-> "
		_show_in_addr_4in6  &$inpcb->inp_dependfaddr.inp46_foreign
		_show_in_port &$inpcb->inp_fport
	end
end

define _show_ipv6_socket
	set $so = (struct socket *)$arg0
	set $pcb = (struct inpcb *)$so->so_pcb
	if $pcb == 0
		printf "inpcb: (null) "
	else
		printf "inpcb: %p ", $pcb

		_showsockprotocol $so

		_show_in6_addr  &$pcb->inp_dependladdr.inp6_local
		_show_in_port  &$pcb->inp_lport
		printf "-> "
		_show_in6_addr  &$pcb->inp_dependfaddr.inp6_foreign
		_show_in_port &$pcb->inp_fport
	end
end


define showsocket
	set $so = (struct socket *)$arg0
	if $so == 0
		printf "so: (null) "
	else
		printf "so: %p ", $so
		if $so && $so->so_proto && $so->so_proto->pr_domain
			set $domain = (struct  domain *) $so->so_proto->pr_domain

			printf "%s ", $domain->dom_name
			if  $domain->dom_family == 1
				_show_unix_domain_socket $so
			end
			if  $domain->dom_family == 2
				_show_ipv4_socket $so
			end
			if  $domain->dom_family == 30
				_show_ipv6_socket $so
			end
		end
	end
	printf "\n"
end
document showsocket
Syntax: (gdb) showsocket <socket_address>
| Routine to print out a socket
end

define showprocsockets
	set $pp = (struct proc *)$arg0
	set $fdp = (struct filedesc *)$pp->p_fd

	set $count = 0
	set $fpp =  (struct fileproc **)($fdp->fd_ofiles)
	set $fpo =  (char)($fdp->fd_ofileflags[0])
	while $count < $fdp->fd_nfiles
		if *$fpp
			set $fg =(struct fileglob *)((**$fpp)->f_fglob)
			if  $fg && (($fg)->fg_type == 2) 
				if $fdp->fd_ofileflags[$count] & 4
					printf "U: "
				else
					printf " "
				end
				printf "fd = %d ", $count
				if  $fg->fg_data
					showsocket  $fg->fg_data
				else
					printf "\n"
				end
			end
		end
		set $fpp = $fpp + 1
	 	set $count = $count + 1		
	end
end
document showprocsockets
Syntax: (gdb) showprocsockets <proc_address>
| Routine to print out all the open fds
| which are sockets in a process
end

define showallprocsockets
  set $basep = (struct proc  *)allproc->lh_first
  set $pp = $basep
  while $pp
	printf "============================================ \n"
	showproc $pp
	showprocsockets $pp
  	set $pp = $pp->p_list.le_next
  end
end
document showallprocsockets
Syntax: (gdb) showallprocsockets
| Routine to print out all the open fds
| which are sockets 
end

define _print_ntohs
	set $port = (unsigned short)$arg0
	set $port = (unsigned short)((($arg0 & 0xff00) >> 8) & 0xff)
	set $port |= (unsigned short)(($arg0 & 0xff) << 8)
	printf "%5d", $port
end

set $INPCB_STATE_INUSE=0x1
set $INPCB_STATE_CACHED=0x2
set $INPCB_STATE_DEAD=0x3

set $INP_RECVOPTS=0x01
set $INP_RECVRETOPTS=0x02
set $INP_RECVDSTADDR=0x04
set $INP_HDRINCL=0x08
set $INP_HIGHPORT=0x10
set $INP_LOWPORT=0x20
set $INP_ANONPORT=0x40
set $INP_RECVIF=0x80
set $INP_MTUDISC=0x100
set $INP_STRIPHDR=0x200
set $INP_FAITH=0x400
set $INP_INADDR_ANY=0x800
set $INP_RECVTTL=0x1000
set $INP_UDP_NOCKSUM=0x2000
set $IN6P_IPV6_V6ONLY=0x008000
set $IN6P_PKTINFO=0x010000
set $IN6P_HOPLIMIT=0x020000
set $IN6P_HOPOPTS=0x040000
set $IN6P_DSTOPTS=0x080000
set $IN6P_RTHDR=0x100000
set $IN6P_RTHDRDSTOPTS=0x200000
set $IN6P_AUTOFLOWLABEL=0x800000
set $IN6P_BINDV6ONLY=0x10000000

set $INP_IPV4=0x1
set $INP_IPV6=0x2

set $IPPROTO_TCP=6
set $IPPROTO_UDP=17

define _dump_inpcb
	set $pcb = (struct inpcb *)$arg0
	if $kgm_lp64
		printf "%18p", $pcb
	else
		printf "%10p ", $pcb
	end
	if $arg1 == $IPPROTO_TCP
		printf "tcp"
	else
		if $arg1 == $IPPROTO_UDP
			printf "udp"
		else
			printf "%2d.", $arg1
		end
	end
	if ($pcb->inp_vflag & $INP_IPV4)
		printf "4 "
	end
	if ($pcb->inp_vflag & $INP_IPV6)
		printf "6 "
	end

	if ($pcb->inp_vflag & $INP_IPV4)
		printf "                        "
		_show_in_addr &$pcb->inp_dependladdr.inp46_local.ia46_addr4
	else
		_show_in6_addr &$pcb->inp_dependladdr.inp6_local
	end
	printf " "
	_print_ntohs $pcb->inp_lport
	printf " "
	if ($pcb->inp_vflag & $INP_IPV4)
		printf "                        "
		_show_in_addr &($pcb->inp_dependfaddr.inp46_foreign.ia46_addr4)
	else
		_show_in6_addr &($pcb->inp_dependfaddr.inp6_foreign)
	end
	printf " "
	_print_ntohs $pcb->inp_fport
	printf " "

	if $arg1 == $IPPROTO_TCP
		_showtcpstate $pcb->inp_ppcb
	end

#	printf "phd "
#	set $phd = $pcb->inp_phd
#	while $phd != 0
#		printf " "
#		_print_ntohs $phd->phd_port
#		set $phd = $phd->phd_hash.le_next
#	end
#	printf ", "
	if ($pcb->inp_flags & $INP_RECVOPTS)
		printf "recvopts "
	end
	if ($pcb->inp_flags & $INP_RECVRETOPTS)
		printf "recvretopts "
	end
	if ($pcb->inp_flags & $INP_RECVDSTADDR)
		printf "recvdstaddr "
	end
	if ($pcb->inp_flags & $INP_HDRINCL)
		printf "hdrincl "
	end
	if ($pcb->inp_flags & $INP_HIGHPORT)
		printf "highport "
	end
	if ($pcb->inp_flags & $INP_LOWPORT)
		printf "lowport "
	end
	if ($pcb->inp_flags & $INP_ANONPORT)
		printf "anonport "
	end
	if ($pcb->inp_flags & $INP_RECVIF)
		printf "recvif "
	end
	if ($pcb->inp_flags & $INP_MTUDISC)
		printf "mtudisc "
	end
	if ($pcb->inp_flags & $INP_STRIPHDR)
		printf "striphdr "
	end
	if ($pcb->inp_flags & $INP_FAITH)
		printf "faith "
	end
	if ($pcb->inp_flags & $INP_INADDR_ANY)
		printf "inaddr_any "
	end
	if ($pcb->inp_flags & $INP_RECVTTL)
		printf "recvttl "
	end
	if ($pcb->inp_flags & $INP_UDP_NOCKSUM)
		printf "nocksum "
	end
	if ($pcb->inp_flags & $IN6P_IPV6_V6ONLY)
		printf "v6only "
	end
	if ($pcb->inp_flags & $IN6P_PKTINFO)
		printf "pktinfo "
	end
	if ($pcb->inp_flags & $IN6P_HOPLIMIT)
		printf "hoplimit "
	end
	if ($pcb->inp_flags & $IN6P_HOPOPTS)
		printf "hopopts "
	end
	if ($pcb->inp_flags & $IN6P_DSTOPTS)
		printf "dstopts "
	end
	if ($pcb->inp_flags & $IN6P_RTHDR)
		printf "rthdr "
	end
	if ($pcb->inp_flags & $IN6P_RTHDRDSTOPTS)
		printf "rthdrdstopts "
	end
	if ($pcb->inp_flags & $IN6P_AUTOFLOWLABEL)
		printf "autoflowlabel "
	end
	if ($pcb->inp_flags & $IN6P_BINDV6ONLY)
		printf "bindv6only "
	end
	set $so = (struct socket *)$pcb->inp_socket
	if $so != 0
		printf "[so=%p s=%ld r=%ld usecnt=%ld] ", $so, $so->so_snd.sb_cc, \
		    $so->so_rcv.sb_cc, $so->so_usecount
	end
	if ($pcb->inp_state == 0 || $pcb->inp_state == $INPCB_STATE_INUSE)
		printf "inuse, "
	else
		if ($pcb->inp_state == $INPCB_STATE_CACHED)
			printf "cached, "
		else
			if ($pcb->inp_state == $INPCB_STATE_DEAD)
				printf "dead, "
			else
				printf "unknown (%d), ", $pcb->inp_state
			end
		end
	end
end

define _dump_inpcbport
	set $ppcb = (struct inpcbport *)$arg0
	printf "%p: lport ", $ppcb
	_print_ntohs $ppcb->phd_port
end

set $UDBHASHSIZE=16

define _dump_pcbinfo
	set $snd_cc = 0
	set $snd_buf = (unsigned int)0
	set $rcv_cc = 0
	set $rcv_buf = (unsigned int)0
	set $pcbseen = 0
	set $pcbi = (struct inpcbinfo *)$arg0
	printf "lastport %d lastlow %d lasthi %d\n", \
	    $pcbi->lastport, $pcbi->lastlow, $pcbi->lasthi
	printf "active pcb count is %d\n", $pcbi->ipi_count
	set $hashsize = $pcbi->hashmask + 1
	printf "hash size is %d\n", $hashsize
	printf "hash base %p has the following inpcb(s):\n", $pcbi->hashbase
	if $kgm_lp64
		printf "pcb                prot source                          address port  destination                     address port\n"
		printf "------------------ ---- --------------------------------------- ----- --------------------------------------- -----\n"
	else
		printf "pcb        prot source                          address port  destination                     address port\n"
		printf "---------- ---- --------------------------------------- ----- --------------------------------------- -----\n"
	end
	set $i = 0
	set $hashbase = $pcbi->hashbase
	set $head = *(uintptr_t *)$hashbase
	while $i < $hashsize
		if $head != 0
			set $pcb0 = (struct inpcb *)$head
			while $pcb0  != 0
				set $pcbseen += 1
				_dump_inpcb $pcb0 $arg1
				set $so = (struct socket *)$pcb->inp_socket
				if $so != 0
					set $snd_cc += $so->so_snd.sb_cc
					set $mp = $so->so_snd.sb_mb
					while $mp
						set $snd_buf += 256
						if ($mp->m_hdr.mh_flags & 0x01)
							set $snd_buf += $mp->M_dat.MH.MH_dat.MH_ext.ext_size
						end
						set $mp = $mp->m_hdr.mh_next
					end
					set $rcv_cc += $so->so_rcv.sb_cc
					set $mp = $so->so_rcv.sb_mb
					while $mp
						set $rcv_buf += 256
						if ($mp->m_hdr.mh_flags & 0x01)
							set $rcv_buf += $mp->M_dat.MH.MH_dat.MH_ext.ext_size
						end
						set $mp = $mp->m_hdr.mh_next
					end
				end
				set $pcb0 = $pcb0->inp_hash.le_next
				printf "\n"
			end
		end
		set $i += 1
		set $hashbase += 1
		set $head = *(uintptr_t *)$hashbase
	end
	printf "total seen %ld snd_cc %ld rcv_cc %ld\n", $pcbseen, $snd_cc, $rcv_cc
	printf "total snd_buf %u rcv_buf %u \n", (unsigned int)$snd_buf, (unsigned int)$rcv_buf
	printf "port hash base is %p\n", $pcbi->porthashbase
	set $i = 0
	set $hashbase = $pcbi->porthashbase
	set $head = *(uintptr_t *)$hashbase
	while $i < $hashsize
		if $head != 0
			set $pcb0 = (struct inpcbport *)$head
			while $pcb0  != 0
				printf "\t"
				_dump_inpcbport $pcb0
				printf "\n"
				set $pcb0 = $pcb0->phd_hash.le_next
			end
		end
		set $i += 1
		set $hashbase += 1
		set $head = *(uintptr_t *)$hashbase
	end
end

set $N_TIME_WAIT_SLOTS=128

define show_tcp_timewaitslots
	set $slot = -1
	set $all = 0
	if $argc == 1
		if (int)$arg0 == -1
			set $all = 1
		else
			set $slot = (int)$arg0
		end
	end
	printf "time wait slot size %d cur_tw_slot %ld\n", $N_TIME_WAIT_SLOTS, cur_tw_slot
	set $i = 0
	while $i < $N_TIME_WAIT_SLOTS
		set $perslot = 0
		set $head = (uintptr_t *)time_wait_slots[$i]
		if $i == $slot || $slot == -1
			if $head != 0
				set $pcb0 = (struct inpcb *)$head
				while $pcb0  != 0
					set $perslot += 1
					set $pcb0 = $pcb0->inp_list.le_next
				end
			end
			printf "  slot %ld count %ld\n", $i, $perslot
		end
		if $all ||  $i == $slot
			if $head != 0
				set $pcb0 = (struct inpcb *)$head
				while $pcb0  != 0
					printf "\t"
					_dump_inpcb $pcb0 $IPPROTO_TCP
					printf "\n"
					set $pcb0 = $pcb0->inp_list.le_next
				end
			end
		end
		set $i += 1
	end
end
document show_tcp_timewaitslots
Syntax: (gdb) show_tcp_timewaitslots
| Print the list of TCP protocol control block in the TIMEWAIT state
| Pass -1 to see the list of PCB for each slot
| Pass a slot number to see information for that slot with the list of PCB
end

define show_tcp_pcbinfo
	_dump_pcbinfo &tcbinfo $IPPROTO_TCP
end
document show_tcp_pcbinfo
Syntax: (gdb) show_tcp_pcbinfo
| Print the list of TCP protocol control block information
end


define show_udp_pcbinfo
	_dump_pcbinfo &udbinfo $IPPROTO_UDP
end
document show_udp_pcbinfo
Syntax: (gdb) show_udp_pcbinfo
| Print the list of UDP protocol control block information
end

define showbpfdtab
    set $myi = 0
    while ($myi < bpf_dtab_size)
	if (bpf_dtab[$myi] != 0)
		printf "Address 0x%x, bd_next 0x%x\n", bpf_dtab[$myi], bpf_dtab[$myi]->bd_next
		print *bpf_dtab[$myi]
	end
	set $myi = $myi + 1
    end
end

define printvnodepathint_recur
	if $arg0 != 0
		if ($arg0->v_flag & 0x000001) && ($arg0->v_mount != 0)
			if $arg0->v_mount->mnt_vnodecovered != 0
				printvnodepathint_recur  $arg0->v_mount->mnt_vnodecovered $arg0->v_mount->mnt_vnodecovered->v_name
			end
		else
			printvnodepathint_recur $arg0->v_parent $arg0->v_parent->v_name
			printf "/%s", $arg1
		end
	end
end

define showvnodepath
	set $vp = (struct vnode *)$arg0
	if $vp != 0
		if ($vp->v_flag & 0x000001) && ($vp->v_mount != 0) && ($vp->v_mount->mnt_flag & 0x00004000)
			printf "/"
		else
			printvnodepathint_recur $vp $vp->v_name
		end
	end
	printf "\n"
end

document showvnodepath
Syntax: (gdb) showvnodepath <vnode>
| Prints the path for a vnode
end

define showallvols
	printf "volume    "
    showptrhdrpad
    printf "  mnt_data  "
    showptrhdrpad
    printf "  mnt_devvp "
    showptrhdrpad
    printf "  typename    mountpoint\n"
	set $kgm_vol = (mount_t) mountlist.tqh_first
	while $kgm_vol
        showptr $kgm_vol
        printf "  "
        showptr $kgm_vol->mnt_data
        printf "  "
        showptr $kgm_vol->mnt_devvp
        printf "  "
		if  ($kgm_vol->mnt_vtable->vfc_name[0] == 'h') && \
		    ($kgm_vol->mnt_vtable->vfc_name[1] == 'f') && \
		    ($kgm_vol->mnt_vtable->vfc_name[2] == 's') && \
		    ($kgm_vol->mnt_vtable->vfc_name[3] == '\0')
			set $kgm_hfsmount = \
			    (struct hfsmount *) $kgm_vol->mnt_data
			if $kgm_hfsmount->hfs_freezing_proc != 0
				printf "FROZEN hfs  "
			else
				printf "hfs         "
			end
		else
			printf "%-10s  ", $kgm_vol->mnt_vtable->vfc_name
		end
		printf "%s\n", $kgm_vol->mnt_vfsstat.f_mntonname
		
		set $kgm_vol = (mount_t) $kgm_vol->mnt_list.tqe_next
	end
end

document showallvols
Syntax: (gdb) showallvols
| Display a summary of mounted volumes
end

define showvnodeheader
	printf "vnode     "
    showptrhdrpad
    printf "  usecount  iocount  v_data    "
    showptrhdrpad
    printf "  vtype  parent    "
    showptrhdrpad    
    printf "  name\n"
end

define showvnodeint
	set $kgm_vnode = (vnode_t) $arg0
	showptr $kgm_vnode
	printf "  %8d  ", $kgm_vnode->v_usecount
	printf "%7d  ", $kgm_vnode->v_iocount
# print information about clean/dirty blocks?
	showptr $kgm_vnode->v_data
    printf "  "
	# print the vtype, using the enum tag
	set $kgm_vtype = $kgm_vnode->v_type
	if $kgm_vtype == VNON
		printf "VNON   "
	end
	if $kgm_vtype == VREG
		printf "VREG   "
	end
	if $kgm_vtype == VDIR
		printf "VDIR   "
	end
	if $kgm_vtype == VBLK
		printf "VBLK   "
	end
	if $kgm_vtype == VCHR
		printf "VCHR   "
	end
	if $kgm_vtype == VLNK
		printf "VLNK   "
	end
	if $kgm_vtype == VSOCK
		printf "VSOCK  "
	end
	if $kgm_vtype == VFIFO
		printf "VFIFO  "
	end
	if $kgm_vtype == VBAD
		printf "VBAD   "
	end
	if ($kgm_vtype < VNON) || ($kgm_vtype > VBAD)
		printf "%5d  ", $kgm_vtype
	end

	showptr $kgm_vnode->v_parent
    printf "  "
	if ($kgm_vnode->v_name != 0)
		printf "%s\n", $kgm_vnode->v_name
	else 
		# If this is HFS vnode, get name from the cnode
		if ($kgm_vnode->v_tag == 16) 
			set $kgm_cnode = (struct cnode *)$kgm_vnode->v_data
			printf "hfs: %s\n", (char *)$kgm_cnode->c_desc->cd_nameptr
		else 
			printf "\n"
		end
	end
end

define showvnode
	showvnodeheader
	showvnodeint $arg0
end

document showvnode
Syntax: (gdb) showvnode <vnode>
| Display info about one vnode
end

define showvolvnodes
	showvnodeheader
	set $kgm_vol = (mount_t) $arg0
	set $kgm_vnode = (vnode_t) $kgm_vol.mnt_vnodelist.tqh_first
	while $kgm_vnode
		showvnodeint $kgm_vnode
		set $kgm_vnode = (vnode_t) $kgm_vnode->v_mntvnodes.tqe_next
	end
end

document showvolvnodes
Syntax: (gdb) showvolvnodes <mouont_t>
| Display info about all vnodes of a given mount_t
end

define showvolbusyvnodes
	showvnodeheader
	set $kgm_vol = (mount_t) $arg0
	set $kgm_vnode = (vnode_t) $kgm_vol.mnt_vnodelist.tqh_first
	while $kgm_vnode
		if $kgm_vnode->v_iocount != 0
			showvnodeint $kgm_vnode
		end
		set $kgm_vnode = (vnode_t) $kgm_vnode->v_mntvnodes.tqe_next
	end
end

document showvolbusyvnodes
Syntax: (gdb) showvolbusyvnodes <mount_t>
| Display info about busy (iocount!=0) vnodes of a given mount_t
end

define showallbusyvnodes
	showvnodeheader
	set $kgm_vol = (mount_t) mountlist.tqh_first
	while $kgm_vol
		set $kgm_vnode = (vnode_t) $kgm_vol.mnt_vnodelist.tqh_first
		while $kgm_vnode
			if $kgm_vnode->v_iocount != 0
				showvnodeint $kgm_vnode
			end
			set $kgm_vnode = (vnode_t) $kgm_vnode->v_mntvnodes.tqe_next
		end
		set $kgm_vol = (mount_t) $kgm_vol->mnt_list.tqe_next
	end
end

document showallbusyvnodes
Syntax: (gdb) showallbusyvnodes <vnode>
| Display info about all busy (iocount!=0) vnodes
end

define showallvnodes
	showvnodeheader
	set $kgm_vol = (mount_t) mountlist.tqh_first
	while $kgm_vol
		set $kgm_vnode = (vnode_t) $kgm_vol.mnt_vnodelist.tqh_first
		while $kgm_vnode
			showvnodeint $kgm_vnode
			set $kgm_vnode = (vnode_t) $kgm_vnode->v_mntvnodes.tqe_next
		end
		set $kgm_vol = (mount_t) $kgm_vol->mnt_list.tqe_next
	end
end

document showallvnodes
Syntax: (gdb) showallvnodes
| Display info about all vnodes
end

define _showvnodelockheader
    printf "*  type   W  held by        lock type  start               end\n"
    printf "-  -----  -  -------------  ---------  ------------------  ------------------\n"
end

define _showvnodelock
    set $kgm_svl_lock = ((struct lockf *)$arg0)

    # decode flags
    set $kgm_svl_flags = $kgm_svl_lock->lf_flags
    set $kgm_svl_type = $kgm_svl_lock->lf_type
    if ($kgm_svl_flags & 0x20)
	printf "flock"
    end
    if ($kgm_svl_flags & 0x40)
	printf "posix"
    end
    if ($kgm_svl_flags & 0x80)
	printf "prov "
    end
    if ($kgm_svl_flags & 0x10)
	printf "  W  "
    else
	printf "  .  "
    end

    # POSIX file vs. advisory range locks
    if ($kgm_svl_flags & 0x40)
	set $kgm_svl_proc = (proc_t)$kgm_svl_lock->lf_id
	printf "PID %8d   ", $kgm_svl_proc->p_pid
    else
	printf "ID 0x%08x  ", $kgm_svl_lock->lf_id
    end

    # lock type
    if ($kgm_svl_type == 1)
		printf "shared     "
    else
	if ($kgm_svl_type == 3)
		printf "exclusive  "
	else
	    if ($kgm_svl_type == 2)
		printf "unlock     "
	    else
		printf "unknown    "
	    end
	end
    end

    # start and stop
    printf "0x%016x..", $kgm_svl_lock->lf_start
    printf "0x%016x  ", $kgm_svl_lock->lf_end
    printf "\n"
end
# Body of showvnodelocks, not including header
define _showvnodelocks
    set $kgm_svl_vnode = ((vnode_t)$arg0)
    set $kgm_svl_lockiter = $kgm_svl_vnode->v_lockf
    while ($kgm_svl_lockiter != 0)
	# locks that are held
    	printf "H  "
    	_showvnodelock $kgm_svl_lockiter

	# and any locks blocked by them
	set $kgm_svl_blocker = $kgm_svl_lockiter->lf_blkhd.tqh_first
	while ($kgm_svl_blocker != 0)
	    printf ">  "
	    _showvnodelock $kgm_svl_blocker
	    set $kgm_svl_blocker = $kgm_svl_blocker->lf_block.tqe_next
	end

	# and on to the next one...
    	set $kgm_svl_lockiter = $kgm_svl_lockiter->lf_next
    end
end


define showvnodelocks
    if ($argc == 1)
    	_showvnodelockheader
	_showvnodelocks $arg0
    else
    	printf "| Usage:\n|\n"
	help showvnodelocks
    end
end

document showvnodelocks
Syntax: (gdb) showvnodelocks <vnode_t>
| Given a vnodet pointer, display the list of advisory record locks for the
| referenced pvnodes
end

define showbootargs
	printf "%s\n", (char*)((boot_args*)PE_state.bootArgs).CommandLine
end

document showbootargs
Syntax: showbootargs
| Display boot arguments passed to the target kernel
end

define showbootermemorymap
        if ($kgm_mtype == $kgm_mtype_i386)
            set $kgm_voffset = 0
        else
            if ($kgm_mtype == $kgm_mtype_x86_64)
                set $kgm_voffset = 0xFFFFFF8000000000ULL
            else
                echo showbootermemorymap not supported on this architecture
            end
        end

        set $kgm_boot_args = kernelBootArgs
        set $kgm_msize = kernelBootArgs->MemoryMapDescriptorSize
        set $kgm_mcount = kernelBootArgs->MemoryMapSize / $kgm_msize
        set $kgm_i = 0
       
        printf "Type       Physical Start   Number of Pages  Virtual Start    Attributes\n"
        while $kgm_i < $kgm_mcount
	     set $kgm_mptr = (EfiMemoryRange *)((unsigned long)kernelBootArgs->MemoryMap + $kgm_voffset + $kgm_i * $kgm_msize)
#	     p/x *$kgm_mptr
	     if $kgm_mptr->Type == 0
	       printf "Reserved  "
	     end
	     if $kgm_mptr->Type == 1
	       printf "LoaderCode"
	     end
	     if $kgm_mptr->Type == 2
	       printf "LoaderData"
	     end
	     if $kgm_mptr->Type == 3
	       printf "BS_code   "
	     end
	     if $kgm_mptr->Type == 4
	       printf "BS_data   "
	     end
	     if $kgm_mptr->Type == 5
	       printf "RT_code   "
	     end
	     if $kgm_mptr->Type == 6
	       printf "RT_data   "
	     end
	     if $kgm_mptr->Type == 7
	       printf "Convention"
	     end
	     if $kgm_mptr->Type == 8
    	       printf "Unusable  "
	     end
	     if $kgm_mptr->Type == 9
               printf "ACPI_recl "
	     end
	     if $kgm_mptr->Type == 10
               printf "ACPI_NVS  "
	     end
	     if $kgm_mptr->Type == 11
               printf "MemMapIO  "
	     end
	     if $kgm_mptr->Type == 12
               printf "MemPortIO "
	     end
	     if $kgm_mptr->Type == 13
               printf "PAL_code  "
	     end
	     if $kgm_mptr->Type > 13
               printf "UNKNOWN   "
	     end

	     printf " %016llx %016llx", $kgm_mptr->PhysicalStart, $kgm_mptr->NumberOfPages
	     if $kgm_mptr->VirtualStart != 0
	       printf " %016llx", $kgm_mptr->VirtualStart
	     else
	       printf "                 "
	     end
	     printf " %016llx\n", $kgm_mptr->Attribute
	     set $kgm_i = $kgm_i + 1
       end
end

document showbootermemorymap
Syntax: (gdb) showbootermemorymap
| Prints out the phys memory map from kernelBootArgs
end


define showstacksaftertask
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)$arg0
    set $kgm_taskp = (struct task *)$kgm_taskp->tasks.next
    while $kgm_taskp != $kgm_head_taskp
	showtaskheader
	showtaskint $kgm_taskp
	set $kgm_head_actp = &($kgm_taskp->threads)
	set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
	while $kgm_actp != $kgm_head_actp
	    showactheader
	    if ($decode_wait_events > 0)
	       showactint $kgm_actp 1
	    else
	       showactint $kgm_actp 2
	    end
	    set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
	end
	printf "\n"
	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
end
document showstacksaftertask
Syntax: (gdb) showstacksaftertask <task>
| Routine to print out all stacks (as in showallstacks) starting after a given task
| Useful if that gdb refuses to print a certain task's stack.
end

define showpmworkqueueint
    set $kgm_pm_workqueue = (IOPMWorkQueue *)$arg0
    set $kgm_pm_wq = &($kgm_pm_workqueue->fWorkQueue)
    set $kgm_pm_wqe = (IOServicePM *)$kgm_pm_wq->next
    while ((queue_entry_t) $kgm_pm_wqe != (queue_entry_t) $kgm_pm_wq)
        printf "service   "
        showptrhdrpad
        printf "  ps  ms  wr  name\n"
        showptr $kgm_pm_wqe->Owner
        printf "  "
        printf "%02d  ", $kgm_pm_wqe->CurrentPowerState
        printf "%02d  ", $kgm_pm_wqe->MachineState
        printf "%02d  ", $kgm_pm_wqe->WaitReason
        printf "%s\n", $kgm_pm_wqe->Name
        printf "request   "
        showptrhdrpad
        printf "  type  next      "
        showptrhdrpad
        printf "  root      "
        showptrhdrpad
        printf "  work_wait   free_wait\n"
        set $kgm_pm_rq = &($kgm_pm_wqe->RequestHead)
        set $kgm_pm_rqe = (IOPMRequest *)$kgm_pm_rq->next
        while ((queue_entry_t) $kgm_pm_rqe != (queue_entry_t) $kgm_pm_rq)
            showptr $kgm_pm_rqe
            printf "  0x%02x  ", $kgm_pm_rqe->fType
            showptr $kgm_pm_rqe->fRequestNext
            printf "  "
            showptr $kgm_pm_rqe->fRequestRoot
            printf "  0x%08x  0x%08x\n", $kgm_pm_rqe->fWorkWaitCount, $kgm_pm_rqe->fFreeWaitCount
            showptrhdrpad
            printf "            args  "
            showptr $kgm_pm_rqe->fArg0
            printf "  "
            showptr $kgm_pm_rqe->fArg1
            printf "  "
            showptr $kgm_pm_rqe->fArg2
            printf "\n"
            set $kgm_pm_rqe = (IOPMRequest *)$kgm_pm_rqe->fCommandChain.next
        end
        printf "\n"
        set $kgm_pm_wqe = (IOServicePM *)$kgm_pm_wqe->WorkChain.next
    end
end

define showpmworkqueue
    printf "IOPMWorkQueue "
    showptr gIOPMWorkQueue
    printf " length "
    printf "%u", gIOPMWorkQueue->fQueueLength
    printf "\n"
    if (gIOPMWorkQueue->fQueueLength > 0)
        showpmworkqueueint gIOPMWorkQueue
    end
end

document showpmworkqueue
Syntax: (gdb) showpmworkqueue
| Display the IOPMWorkQueue object
end

define showioservicepm
    set $kgm_iopmpriv = (IOServicePM *)$arg0
    printf "{ "
    printf "MachineState = %d (", $kgm_iopmpriv->MachineState
    if ( $kgm_iopmpriv->MachineState == 0 )
        printf "kIOPM_Finished"
    else
    if ( $kgm_iopmpriv->MachineState == 1 )
        printf "kIOPM_OurChangeTellClientsPowerDown"
    else
    if ( $kgm_iopmpriv->MachineState == 2 )
        printf "kIOPM_OurChangeTellPriorityClientsPowerDown"
    else
    if ( $kgm_iopmpriv->MachineState == 3 )
        printf "kIOPM_OurChangeNotifyInterestedDriversWillChange"
    else
    if ( $kgm_iopmpriv->MachineState == 4 )
        printf "kIOPM_OurChangeSetPowerState"
    else
    if ( $kgm_iopmpriv->MachineState == 5 )
        printf "kIOPM_OurChangeWaitForPowerSettle"
    else
    if ( $kgm_iopmpriv->MachineState == 6 )
        printf "kIOPM_OurChangeNotifyInterestedDriversDidChange"
    else
    if ( $kgm_iopmpriv->MachineState == 7 )
        printf "kIOPM_OurChangeTellCapabilityDidChange"
    else
    if ( $kgm_iopmpriv->MachineState == 8 )
        printf "kIOPM_OurChangeFinish"
    else
    if ( $kgm_iopmpriv->MachineState == 9 )
        printf "Unused_MachineState_9"
    else
    if ( $kgm_iopmpriv->MachineState == 10 )
        printf "kIOPM_ParentChangeTellPriorityClientsPowerDown"
    else
    if ( $kgm_iopmpriv->MachineState == 11 )
        printf "kIOPM_ParentChangeNotifyInterestedDriversWillChange"
    else
    if ( $kgm_iopmpriv->MachineState == 12 )
        printf "kIOPM_ParentChangeSetPowerState"
    else
    if ( $kgm_iopmpriv->MachineState == 13 )
        printf "kIOPM_ParentChangeWaitForPowerSettle"
    else
    if ( $kgm_iopmpriv->MachineState == 14)
        printf "kIOPM_ParentChangeNotifyInterestedDriversDidChange"
    else
    if ( $kgm_iopmpriv->MachineState == 15)
        printf "kIOPM_ParentChangeTellCapabilityDidChange"
    else
    if ( $kgm_iopmpriv->MachineState == 16)
        printf "kIOPM_ParentChangeAcknowledgePowerChange"
    else
    if ( $kgm_iopmpriv->MachineState == 17)
        printf "kIOPM_NotifyChildrenStart"
    else
    if ( $kgm_iopmpriv->MachineState == 18)
        printf "kIOPM_NotifyChildrenOrdered"
    else
    if ( $kgm_iopmpriv->MachineState == 19)
        printf "kIOPM_NotifyChildrenDelayed"
    else
    if ( $kgm_iopmpriv->MachineState == 20)
        printf "kIOPM_SyncTellClientsPowerDown"
    else
    if ( $kgm_iopmpriv->MachineState == 21)
        printf "kIOPM_SyncTellPriorityClientsPowerDown"
    else
    if ( $kgm_iopmpriv->MachineState == 22)
        printf "kIOPM_SyncNotifyWillChange"
    else
    if ( $kgm_iopmpriv->MachineState == 23)
        printf "kIOPM_SyncNotifyDidChange"
    else
    if ( $kgm_iopmpriv->MachineState == 24)
        printf "kIOPM_SyncTellCapabilityDidChange"
    else
    if ( $kgm_iopmpriv->MachineState == 25)
        printf "kIOPM_SyncFinish"
    else
    if ( $kgm_iopmpriv->MachineState == 26)
        printf "kIOPM_TellCapabilityChangeDone"
    else
    if ( $kgm_iopmpriv->MachineState == 27)
        printf "kIOPM_DriverThreadCallDone"
    else
        printf "Unknown_MachineState"
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
    end
	end
	printf "), "
	
	if ( $kgm_iopmpriv->MachineState != 20 )
        printf "DriverTimer = %d, ",(unsigned int)$kgm_iopmpriv->DriverTimer
        printf "SettleTime  = %d, ",(unsigned int)$kgm_iopmpriv->SettleTimeUS
        printf "HeadNoteFlags = %08x, ",(unsigned int)$kgm_iopmpriv->HeadNoteChangeFlags
        printf "HeadNotePendingAcks = %x, ",(unsigned int)$kgm_iopmpriv->HeadNotePendingAcks
	end

    if ( $kgm_iopmpriv->DeviceOverrideEnabled != 0 )
        printf"DeviceOverrides, "
    end
	
    printf "DeviceDesire = %d, ",(unsigned int)$kgm_iopmpriv->DeviceDesire
    printf "DesiredPowerState = %d, ",(unsigned int)$kgm_iopmpriv->DesiredPowerState
    printf "PreviousRequest = %d }\n",(unsigned int)$kgm_iopmpriv->PreviousRequestPowerFlags
end

document showioservicepm
Syntax: (gdb) showioservicepm <IOServicePM pointer>
| Routine to dump the IOServicePM object
end

define showregistryentryrecursepmstate
    set $kgm_re         = (IOService *)$arg1
    set $kgm$arg0_stack = (unsigned long long) $arg2

    if ($arg3)
	set $kgm$arg0_stack = $kgm$arg0_stack | (1ULL << $kgm_reg_depth)
    else
	set $kgm$arg0_stack = $kgm$arg0_stack & ~(1ULL << $kgm_reg_depth)
    end

    dictget $kgm_re->fRegistryTable $kgm_childkey
    set $kgm$arg0_child_array = (OSArray *) $kgm_result

    if ($kgm$arg0_child_array)
	set $kgm$arg0_child_count = $kgm$arg0_child_array->count
    else
	set $kgm$arg0_child_count = 0
    end

    if ($kgm$arg0_child_count)
	set $kgm$arg0_stack = $kgm$arg0_stack | (2ULL << $kgm_reg_depth)
    else
	set $kgm$arg0_stack = $kgm$arg0_stack & ~(2ULL << $kgm_reg_depth)
    end

    indent $kgm_reg_depth $kgm$arg0_stack
    printf "+-o "

    dictget $kgm_re->fRegistryTable $kgm_namekey
    if ($kgm_result == 0)
	dictget $kgm_re->fRegistryTable gIONameKey
    end
    if ($kgm_result == 0)
	dictget $kgm_re->fPropertyTable gIOClassKey
    end

    if ($kgm_result != 0)
	printf "%s <%p>", ((OSString *)$kgm_result)->string, $kgm_re
    else
 	if (((IOService*)$kgm_re)->pwrMgt &&  ((IOService*)$kgm_re)->pwrMgt->Name)
 	    printf "%s <", ((IOService*)$kgm_re)->pwrMgt->Name
 	    showptr $kgm_re
 	    printf ">"
	else
	    printf "?? <"
 	    showptr $kgm_re
 	    printf ">"
	end
    end

    if (((IOService*)$kgm_re)->pwrMgt )
    	printf " Current Power State: %ld ", ((IOService*)$kgm_re)->pwrMgt->CurrentPowerState
    	#printf " Mach State %ld", ((IOService*)$kgm_re)->pwrMgt->MachineState
        showioservicepm ((IOService*)$kgm_re)->pwrMgt
    end
    printf "\n"
   

    # recurse
    if ($kgm$arg0_child_count != 0)

	set $kgm_reg_depth = $kgm_reg_depth + 1
	set $kgm$arg0_child_idx = 0

	while ($kgm$arg0_child_idx < $kgm$arg0_child_count)
	    set $kgm_re = $kgm$arg0_child_array->array[$kgm$arg0_child_idx++]
	    set $kgm_more_sib = ($kgm$arg0_child_idx < $kgm$arg0_child_count)
	    if $kgm_reg_depth >= $kgm_reg_depth_max + 1
	       loop_break
	    end
	    showregistryentryrecursepmstate _$arg0 $kgm_re $kgm$arg0_stack $kgm_more_sib
	end

	set $kgm_reg_depth = $kgm_reg_depth - 1
    end
end

define showregistryentryintpmstate
    if !$kgm_reg_plane
       set $kgm_reg_plane = (IORegistryPlane *) gIOServicePlane
    end

    if !$kgm_reg_plane
       printf "Please load kgmacros after KDP attaching to the target.\n"
    else
       set $kgm_namekey   = (OSSymbol *) $kgm_reg_plane->nameKey
       set $kgm_childkey  = (OSSymbol *) $kgm_reg_plane->keys[1]
       showregistryentryrecursepmstate _ $arg0 0 0
    end
end

define showregistrypmstate
#    setregistryplane gIOPowerPlane
    set $kgm_reg_depth  = 0
    set $kgm_show_props = 1
    showregistryentryintpmstate gRegistryRoot
end

document showregistrypmstate
Syntax: (gdb) showregistrypmstate
| Routine to dump the PM state of each IOPower registry entry
end

define showstacksafterthread
    set $kgm_head_taskp = &tasks
    set $kgm_actp = (struct thread *)$arg0
    set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
    set $kgm_taskp = (struct task *)$kgm_actp->task
    while $kgm_taskp != $kgm_head_taskp
	showtaskheader
	showtaskint $kgm_taskp
	set $kgm_head_actp = &($kgm_taskp->threads)
	if $kgm_actp == 0
	    set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
	end
	while $kgm_actp != $kgm_head_actp
	    showactheader
	    if ($decode_wait_events > 0)
	       showactint $kgm_actp 1
	    else
	       showactint $kgm_actp 2
	    end
	    set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
	end
	printf "\n"
	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
	set $kgm_actp = 0
    end
end

document showstacksafterthread
Syntax: (gdb) showstacksafterthread <thread>
| Routine to print out all stacks (as in showallstacks) starting after a given thread
| Useful if that gdb refuses to print a certain task's stack.
end

define kdp-reenter
       set kdp_reentry_deadline = ((unsigned) $arg0)*1000
       continue
end

document kdp-reenter
Syntax: (gdb) kdp-reenter <seconds>
| Schedules reentry into the debugger after <seconds> seconds, and resumes
| the target system.
end

define _if_present
    if (!$arg0)
        printf " not"
    end
    printf " present"
end

define showMCAstate
    if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
        printf "Not available for current architecture.\n"
    else
        printf "MCA"
        _if_present mca_MCA_present
        printf ", control MSR"
        _if_present mca_control_MSR_present
        printf ", threshold status"
        _if_present mca_threshold_status_present
        printf "\n%d error banks, ", mca_error_bank_count
        printf "family code 0x%x, ", mca_family
        printf "machine-check dump state: %d\n", mca_dump_state
        set $kgm_cpu = 0
        while cpu_data_ptr[$kgm_cpu] != 0
            set $kgm_mcp = cpu_data_ptr[$kgm_cpu]->cpu_mca_state
            if $kgm_mcp
                printf "CPU %d:", $kgm_cpu
                printf " mca_mcg_ctl: 0x%016llx", $kgm_mcp->mca_mcg_ctl
                printf " mca_mcg_status: 0x%016llx\n", $kgm_mcp->mca_mcg_status.u64
                printf "bank   "
                printf "mca_mci_ctl        "
                printf "mca_mci_status     "
                printf "mca_mci_addr       "
                printf "mca_mci_misc\n"
                set $kgm_bank = 0
                while $kgm_bank < mca_error_bank_count
                    set $kgm_bp = &$kgm_mcp->mca_error_bank[$kgm_bank]
                    printf " %2d:", $kgm_bank
                    printf " 0x%016llx", $kgm_bp->mca_mci_ctl
                    printf " 0x%016llx", $kgm_bp->mca_mci_status.u64
                    printf " 0x%016llx", $kgm_bp->mca_mci_addr
                    printf " 0x%016llx\n", $kgm_bp->mca_mci_misc
                    set $kgm_bank = $kgm_bank + 1
                end
            end
            set $kgm_cpu = $kgm_cpu + 1
        end
    end
end

document showMCAstate
Syntax: showMCAstate
| Print machine-check register state after MC exception.
end

define _pt_step
    #
    # Step to lower-level page table and print attributes
    #   $kgm_pt_paddr: current page table entry physical address
    #   $kgm_pt_index: current page table entry index (0..511)
    # returns
    #   $kgm_pt_paddr: next level page table entry physical address
    #                  or null if invalid
    #   $kgm_pt_valid: 1 if $kgm_pt_paddr is valid, 0 if the walk
    #                  should be aborted
    #   $kgm_pt_large: 1 if kgm_pt_paddr is a page frame address
    #                  of a large page and not another page table entry
    # For $kgm_pt_verbose = 0: print nothing
    #                       1: print basic information
    #                       2: print basic information and hex table dump
    #
    set $kgm_entryp = $kgm_pt_paddr + 8*$kgm_pt_index
    readphysint $kgm_entryp 64 $kgm_lcpu_self
    set $entry = $kgm_readphysint_result
    if $kgm_pt_verbose >= 3
        set $kgm_pte_loop = 0
        while $kgm_pte_loop < 512
            set $kgm_pt_paddr_tmp = $kgm_pt_paddr + $kgm_pte_loop*8
            readphys64 $kgm_pt_paddr_tmp
            set $kgm_pte_loop = $kgm_pte_loop + 1
        end
    end
    set $kgm_paddr_mask = ~((0xfffULL<<52) | 0xfffULL)
    set $kgm_paddr_largemask = ~((0xfffULL<<52) | 0x1fffffULL)
    if $kgm_pt_verbose < 2
        if $entry & (0x1 << 0)
            set $kgm_pt_valid = 1
            if $entry & (0x1 << 7)
                set $kgm_pt_large = 1
                set $kgm_pt_paddr = $entry & $kgm_paddr_largemask
            else
                set $kgm_pt_large = 0
                set $kgm_pt_paddr = $entry & $kgm_paddr_mask
            end     
        else
            set $kgm_pt_valid = 0
            set $kgm_pt_large = 0
            set $kgm_pt_paddr = 0
        end
    else
        printf "0x%016llx:\n\t0x%016llx\n\t", $kgm_entryp, $entry
        if $entry & (0x1 << 0)
            printf "valid"      
            set $kgm_pt_paddr = $entry & $kgm_paddr_mask
            set $kgm_pt_valid = 1
        else
            printf "invalid"
            set $kgm_pt_paddr = 0
            set $kgm_pt_valid = 0
            # stop decoding other bits
            set $entry = 0
        end
        if $entry & (0x1 << 1)
            printf " writeable" 
        else
            printf " read-only" 
        end
        if $entry & (0x1 << 2)
            printf " user" 
        else
            printf " supervisor" 
        end
        if $entry & (0x1 << 3)
            printf " PWT" 
        end
        if $entry & (0x1 << 4)
            printf " PCD" 
        end
        if $entry & (0x1 << 5)
            printf " accessed" 
        end
        if $entry & (0x1 << 6)
            printf " dirty" 
        end
        if $entry & (0x1 << 7)
            printf " large" 
            set $kgm_pt_large = 1
        else
            set $kgm_pt_large = 0
        end
        if $entry & (0x1 << 8)
            printf " global" 
        end
        if $entry & (0x3 << 9)
            printf " avail:0x%x", ($entry >> 9) & 0x3
        end
        if $entry & (0x1ULL << 63)
            printf " noexec" 
        end
        printf "\n"
    end
end

define _pml4_walk
    set $kgm_pt_paddr = $arg0
    set $kgm_vaddr = $arg1
    set $kgm_pt_valid = $kgm_pt_paddr != 0
    set $kgm_pt_large = 0
    set $kgm_pframe_offset = 0
    if $kgm_pt_valid && cpu_64bit
        # Look up bits 47:39 of the linear address in PML4T
        set $kgm_pt_index = ($kgm_vaddr >> 39) & 0x1ffULL
        set $kgm_pframe_offset = $kgm_vaddr & 0x7fffffffffULL
        if $kgm_pt_verbose >= 2
            printf "pml4 (index %d):\n", $kgm_pt_index
        end
        _pt_step
    end
    if $kgm_pt_valid
        # Look up bits 38:30 of the linear address in PDPT
        set $kgm_pt_index = ($kgm_vaddr >> 30) & 0x1ffULL
        set $kgm_pframe_offset = $kgm_vaddr & 0x3fffffffULL
        if $kgm_pt_verbose >= 2
            printf "pdpt (index %d):\n", $kgm_pt_index
        end
        _pt_step
    end
    if $kgm_pt_valid && !$kgm_pt_large
        # Look up bits 29:21 of the linear address in PDT
        set $kgm_pt_index = ($kgm_vaddr >> 21) & 0x1ffULL
        set $kgm_pframe_offset = $kgm_vaddr & 0x1fffffULL
        if $kgm_pt_verbose >= 2
            printf "pdt (index %d):\n", $kgm_pt_index
        end
        _pt_step
    end
    if $kgm_pt_valid && !$kgm_pt_large
        # Look up bits 20:21 of the linear address in PT
        set $kgm_pt_index = ($kgm_vaddr >> 12) & 0x1ffULL
        set $kgm_pframe_offset = $kgm_vaddr & 0xfffULL
        if $kgm_pt_verbose >= 2
            printf "pt (index %d):\n", $kgm_pt_index
        end
        _pt_step
    end

    if $kgm_pt_valid
        set $kgm_paddr = $kgm_pt_paddr + $kgm_pframe_offset
        set $kgm_paddr_isvalid = 1
    else
        set $kgm_paddr = 0
        set $kgm_paddr_isvalid = 0
    end

    if $kgm_pt_verbose >= 1
        if $kgm_paddr_isvalid
            readphysint $kgm_paddr 32 $kgm_lcpu_self
            set $kgm_value = $kgm_readphysint_result
            printf "phys 0x%016llx: 0x%08x\n", $kgm_paddr, $kgm_value
        else
            printf "(no translation)\n"
        end
    end
end

define _pmap_walk_x86
    set $kgm_pmap = (pmap_t) $arg0
    _pml4_walk $kgm_pmap->pm_cr3 $arg1
end

define _pmap_walk_arm
    set $kgm_paddr = 0
    set $kgm_paddr_isvalid = 0
end

define pmap_walk
    if $argc != 2
        printf "pmap_walk <pmap> <vaddr>\n"
    else
        if !$kgm_pt_verbose
            set $kgm_pt_verbose = 2
        else
            if $kgm_pt_verbose > 3
                set $kgm_pt_verbose = 2
            end
        end
        if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
            _pmap_walk_x86 $arg0 $arg1
        else
            if ($kgm_mtype == $kgm_mtype_arm)
                _pmap_walk_arm $arg0 $arg1
            else
                printf "Not available for current architecture.\n"
            end
        end
    end
end

document pmap_walk
Syntax: (gdb) pmap_walk <pmap> <virtual_address>
| Perform a page-table walk in <pmap> for <virtual_address>.
| Set:
|     $kgm_pt_verbose=0 for no output, $kgm_paddr will be set
|                       if $kgm_paddr_isvalid is 1
|     $kgm_pt_verbose=1 for final physical address
|     $kgm_pt_verbose=2 for dump of page table entry.
|     $kgm_pt_verbose=3 for full hex dump of page tables.
end

define pmap_vtop
    if $argc != 2
        printf "pmap_vtop <pamp> <vaddr>\n"
    else
        set $kgm_pt_verbose = 1
        if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
            _pmap_walk_x86 $arg0 $arg1
        else
            if ($kgm_mtype == $kgm_mtype_arm)
                _pmap_walk_arm $arg0 $arg1
            else
                printf "Not available for current architecture.\n"
            end
        end
    end
end

document pmap_vtop
Syntax: (gdb) pmap_vtop <pmap> <virtual_address>
| For page-tables in <pmap> translate <virtual_address> to physical address.
end

define zstack
	set $index = $arg0

	if (log_records == 0)
		set $count = 0
		printf "Zone logging not enabled.  Add 'zlog=<zone name>' to boot-args.\n"
	else 
		if ($argc == 2)
			set $count = $arg1
		else
			set $count = 1
		end
	end

	while ($count)
		printf "\n--------------- "

		if (zrecords[$index].z_opcode == 1)
			printf "ALLOC  "
		else
			printf "FREE  "
		end

		showptr zrecords[$index].z_element
		printf " : index %d  :  ztime %d -------------\n", $index, zrecords[$index].z_time

		set $frame = 0

		while ($frame < 15)
			set $frame_pc = zrecords[$index].z_pc[$frame]

			if ($frame_pc == 0)
				loop_break
			end

			x/i $frame_pc
			set $frame = $frame + 1
		end

		set $index = $index + 1
		set $count = $count - 1
	end
end

document zstack
Syntax: (gdb) zstack <index> [<count>]
| Zone leak debugging: print the stack trace of log element at <index>.
| If a <count> is supplied, it prints <count> log elements starting at <index>.
|
| The suggested usage is to look at indexes below zcurrent and look for common stack traces.
| The stack trace that occurs the most is probably the cause of the leak.  Find the pc of the
| function calling into zalloc and use the countpcs kgmacro to find out how often that pc occurs in the log.
| The pc occuring in a high percentage of records is most likely the source of the leak.
|
| The findoldest kgmacro is also useful for leak debugging since it identifies the oldest record
| in the log, which may indicate the leaker.
end

define findoldest
	set $index = 0
	set $count = log_records
	set $cur_min = 2000000000
	set $cur_index = 0

	if (log_records == 0)
		printf "Zone logging not enabled.  Add 'zlog=<zone name>' to boot-args.\n"
	else

		while ($count)
			if (zrecords[$index].z_element && zrecords[$index].z_time < $cur_min)
				set $cur_index = $index
				set $cur_min = zrecords[$index].z_time
			end
	
			set $count = $count - 1
			set $index = $index + 1
		end
	
		printf "oldest record is at log index %d:\n", $cur_index
		zstack $cur_index
	end
end

document findoldest
Syntax: (gdb) findoldest
| Zone leak debugging: find and print the oldest record in the log.  Note that this command
| can take several minutes to run since it uses linear search.
|
| Once it prints a stack trace, find the pc of the caller above all the zalloc, kalloc and
| IOKit layers.  Then use the countpcs kgmacro to see how often this caller has allocated
| memory.  A caller with a high percentage of records in the log is probably the leaker.
end

define countpcs
	set $target_pc = $arg0
	set $index = 0
	set $count = log_records
	set $found = 0

	if (log_records == 0)
		printf "Zone logging not enabled.  Add 'zlog=<zone name>' to boot-args.\n"
	else

		while ($count)
			set $frame = 0
	
			if (zrecords[$index].z_element != 0)
				while ($frame < 15)
					if (zrecords[$index].z_pc[$frame] == $target_pc)
						set $found = $found + 1
						set $frame = 15
					end
		
					set $frame = $frame + 1
				end
			end
	
			set $index = $index + 1
			set $count = $count - 1
		end
	
		printf "occurred %d times in log (%d%c of records)\n", $found, ($found * 100) / zrecorded, '%'
	end
end

document countpcs
Syntax: (gdb) countpcs <pc>
| Zone leak debugging: search the log and print a count of all log entries that contain the given <pc>
| in the stack trace.  This is useful for verifying a suspected <pc> as being the source of
| the leak.  If a high percentage of the log entries contain the given <pc>, then it's most
| likely the source of the leak.  Note that this command can take several minutes to run.
end

define findelem
	set $fe_index = zcurrent
	set $fe_count = log_records
	set $fe_elem = $arg0
	set $fe_prev_op = -1

	if (log_records == 0)
		printf "Zone logging not enabled.  Add 'zlog=<zone name>' to boot-args.\n"
	end

	while ($fe_count)
		if (zrecords[$fe_index].z_element == $fe_elem)
			zstack $fe_index

			if (zrecords[$fe_index].z_opcode == $fe_prev_op)
				printf "***************   DOUBLE OP!   *********************\n
			end

			set $fe_prev_op = zrecords[$fe_index].z_opcode
		end

		set $fe_count = $fe_count - 1
		set $fe_index = $fe_index + 1

		if ($fe_index >= log_records)
			set $fe_index = 0
		end
	end
end

document findelem
Syntax: (gdb) findelem <elem addr>
| Zone corruption debugging: search the log and print out the stack traces for all log entries that
| refer to the given zone element.  When the kernel panics due to a corrupted zone element, get the
| element address and use this macro.  This will show you the stack traces of all logged zalloc and
| zfree operations which tells you who touched the element in the recent past.  This also makes
| double-frees readily apparent.
end


# This implements a shadowing scheme in kgmacros. If the
# current user data can be accessed by simply changing kdp_pmap,
# that is used. Otherwise, we copy data into a temporary buffer
# in the kernel's address space and use that instead. Don't rely on
# kdp_pmap between invocations of map/unmap. Since the shadow
# codepath uses a manual KDP packet, request no more than 128 bytes.
# Uses $kgm_lp64 for kernel address space size, and
# $kgm_readphys_use_kdp/$kgm_readphys_force_physmap to override
# how the user pages are accessed ($kgm_readphys_force_physmap
# implies walking the user task's pagetables to get a physical
# address and then shadowing data from there using the
# physical mapping of memory).
define _map_user_data_from_task
    set $kgm_map_user_taskp = (task_t)$arg0
    set $kgm_map_user_map = $kgm_map_user_taskp->map
    set $kgm_map_user_pmap = $kgm_map_user_map->pmap
    set $kgm_map_user_task_64 = ( $kgm_map_user_taskp->taskFeatures[0] & 0x80000000)
    set $kgm_map_user_window = 0
    set $kgm_map_switch_map = 0
    
    if ($kgm_readphys_force_kdp != 0)
        set $kgm_readphys_use_kdp = 1
    else
        if ($kgm_readphys_force_physmap)
            set $kgm_readphys_use_kdp = 0
        else
            set $kgm_readphys_use_kdp = ( kdp->is_conn > 0 )
        end
    end

    if ($kgm_readphys_use_kdp)

        if $kgm_lp64
            set $kgm_map_switch_map = 1
        else
            if !$kgm_map_user_task_64
                set $kgm_map_switch_map = 1
            end
        end
    
        if ($kgm_map_switch_map)
            # switch the map safely
            set $kgm_map_user_window = $arg1
            set kdp_pmap = $kgm_map_user_pmap
        else
            # requires shadowing/copying

            # set up the manual KDP packet
            set manual_pkt.input = 0
            set manual_pkt.len = sizeof(kdp_readmem64_req_t)
            set $kgm_pkt = (kdp_readmem64_req_t *)&manual_pkt.data
            set $kgm_pkt->hdr.request = KDP_READMEM64
            set $kgm_pkt->hdr.len = sizeof(kdp_readmem64_req_t)
            set $kgm_pkt->hdr.is_reply = 0
            set $kgm_pkt->hdr.seq = 0
            set $kgm_pkt->hdr.key = 0
            set $kgm_pkt->address = (uint64_t)$arg1
            set $kgm_pkt->nbytes = (uint32_t)$arg2

            set kdp_pmap = $kgm_map_user_pmap
            set manual_pkt.input = 1
            # dummy to make sure manual packet is executed
            set $kgm_dummy = &_mh_execute_header
            # Go back to kernel map so that we can access buffer directly
            set kdp_pmap = 0

            set $kgm_pkt = (kdp_readmem64_reply_t *)&manual_pkt.data
            if ($kgm_pkt->error == 0)
                set $kgm_map_user_window = $kgm_pkt->data
            else
                set $kgm_map_user_window = 0
            end
        end

    else
        # without the benefit of a KDP stub on the target, try to
        # find the user task's physical mapping and memcpy the data.
        # If it straddles a page boundary, copy in two passes
        set $kgm_vaddr_range1_start = (unsigned long long)$arg1
        set $kgm_vaddr_range1_count = (unsigned long long)$arg2
        if (($kgm_vaddr_range1_start + $kgm_vaddr_range1_count) & 0xFFF) < $kgm_vaddr_range1_count
            set $kgm_vaddr_range2_start = ($kgm_vaddr_range1_start + $kgm_vaddr_range1_count) & ~((unsigned long long)0xFFF)
            set $kgm_vaddr_range2_count = $kgm_vaddr_range1_start + $kgm_vaddr_range1_count - $kgm_vaddr_range2_start
            set $kgm_vaddr_range1_count = $kgm_vaddr_range2_start - $kgm_vaddr_range1_start
        else
            set $kgm_vaddr_range2_start = 0
            set $kgm_vaddr_range2_count = 0
        end
        set $kgm_paddr_range1_in_kva = 0
        set $kgm_paddr_range2_in_kva = 0

        if ($kgm_mtype == $kgm_mtype_x86_64)
            set $kgm_pt_verbose = 0
            _pmap_walk_x86 $kgm_map_user_pmap $kgm_vaddr_range1_start
            if $kgm_paddr_isvalid
                set $kgm_paddr_range1_in_kva = $kgm_paddr + (((unsigned long long)-1 << 47) | ((unsigned long long)509 << 39))
            end
            if $kgm_vaddr_range2_start
                _pmap_walk_x86 $kgm_map_user_pmap $kgm_vaddr_range2_start
                if $kgm_paddr_isvalid
                    set $kgm_paddr_range2_in_kva = $kgm_paddr + (((unsigned long long)-1 << 47) | ((unsigned long long)509 << 39))
                end
            end
        else
            if ($kgm_mtype == $kgm_mtype_arm)
                set $kgm_pt_verbose = 0
                _pmap_walk_arm $kgm_map_user_pmap $kgm_vaddr_range1_start
                if $kgm_paddr_isvalid
                   set $kgm_paddr_range1_in_kva = $kgm_paddr - gPhysBase + gVirtBase
                end
                if $kgm_vaddr_range2_start
                    _pmap_walk_arm $kgm_map_user_pmap $kgm_vaddr_range2_start
                    if $kgm_paddr_isvalid
                        set $kgm_paddr_range2_in_kva = $kgm_paddr - gPhysBase + gVirtBase
                    end
                end
            else
                printf "Not available for current architecture.\n"
                set $kgm_paddr_isvalid = 0
            end
        end
        if $kgm_paddr_range1_in_kva
            set $kgm_pkt = (kdp_readmem64_reply_t *)&manual_pkt.data
            memcpy $kgm_pkt->data $kgm_paddr_range1_in_kva $kgm_vaddr_range1_count
            if $kgm_paddr_range2_in_kva
                memcpy &$kgm_pkt->data[$kgm_vaddr_range1_count] $kgm_paddr_range2_in_kva $kgm_vaddr_range2_count
            end
            set $kgm_map_user_window  = $kgm_pkt->data
        else
            set $kgm_map_user_window  = 0
        end
    end
end

define _unmap_user_data_from_task
    set kdp_pmap = 0
end

# uses $kgm_taskp. Maps 32 bytes at a time and prints it
define _print_path_for_image
    set $kgm_print_path_address = (unsigned long long)$arg0
    set $kgm_path_str_notdone = 1

    if ($kgm_print_path_address == 0)
       set $kgm_path_str_notdone = 0
    end
    
    while $kgm_path_str_notdone
        _map_user_data_from_task $kgm_taskp $kgm_print_path_address 32

        set $kgm_print_path_ptr = (char *)$kgm_map_user_window
        set $kgm_path_i = 0
        while ($kgm_path_i < 32 && $kgm_print_path_ptr[$kgm_path_i] != '\0')
            set $kgm_path_i = $kgm_path_i + 1
        end
        printf "%.32s", $kgm_print_path_ptr
        
        _unmap_user_data_from_task $kgm_taskp
        
        # break out if we terminated on NUL
        if $kgm_path_i < 32
            set $kgm_path_str_notdone = 0
        else
            set $kgm_print_path_address = $kgm_print_path_address + 32
        end
    end
end

# uses $kgm_taskp and $kgm_task_64. May modify $kgm_dyld_load_path
define _print_image_info
    set $kgm_mh_image_address = (unsigned long long)$arg0
    set $kgm_mh_path_address = (unsigned long long)$arg1

    # 32 bytes enough for mach_header/mach_header_64
    _map_user_data_from_task $kgm_taskp $kgm_mh_image_address 32

    set $kgm_mh_ptr = (unsigned int*)$kgm_map_user_window
    set $kgm_mh_magic = $kgm_mh_ptr[0]
    set $kgm_mh_cputype = $kgm_mh_ptr[1]
    set $kgm_mh_cpusubtype = $kgm_mh_ptr[2]
    set $kgm_mh_filetype = $kgm_mh_ptr[3]
    set $kgm_mh_ncmds = $kgm_mh_ptr[4]
    set $kgm_mh_sizeofcmds = $kgm_mh_ptr[5]
    set $kgm_mh_flags = $kgm_mh_ptr[6]

    _unmap_user_data_from_task $kgm_taskp

    if $kgm_mh_magic == 0xfeedfacf
        set $kgm_mh_64 = 1
        set $kgm_lc_address = $kgm_mh_image_address + 32
    else
        set $kgm_mh_64 = 0
        set $kgm_lc_address = $kgm_mh_image_address + 28
    end
    
    set $kgm_lc_idx = 0
    set $kgm_uuid_data = 0
    while $kgm_lc_idx < $kgm_mh_ncmds
        
        # 24 bytes is size of uuid_command
        _map_user_data_from_task $kgm_taskp $kgm_lc_address 24
    
        set $kgm_lc_ptr = (unsigned int *)$kgm_map_user_window
        set $kgm_lc_cmd = $kgm_lc_ptr[0]
        set $kgm_lc_cmd_size = $kgm_lc_ptr[1]
        set $kgm_lc_data = (unsigned char *)$kgm_lc_ptr + 8

        if $kgm_lc_cmd == 0x1b
            set $kgm_uuid_data = $kgm_lc_data
            if $kgm_mh_64
                printf "0x%016llx  ", $kgm_mh_image_address
            else
                printf "0x%08x  ", $kgm_mh_image_address				
            end
            
            set $kgm_printed_type = 0
            if $kgm_mh_filetype == 0x2
                printf "MH_EXECUTE   "
                set $kgm_printed_type = 1
            end
            if $kgm_mh_filetype == 0x6
                printf "MH_DYLIB     "
                set $kgm_printed_type = 1
            end
            if $kgm_mh_filetype == 0x7
                printf "MH_DYLINKER  "
                set $kgm_printed_type = 1
            end
            if $kgm_mh_filetype == 0x8
                printf "MH_BUNDLE    "
                set $kgm_printed_type = 1
            end
            if !$kgm_printed_type
                printf "UNKNOWN      "            
            end
            printf "%02.2X%02.2X%02.2X%02.2X-", $kgm_uuid_data[0], $kgm_uuid_data[1], $kgm_uuid_data[2], $kgm_uuid_data[3]
            printf "%02.2X%02.2X-", $kgm_uuid_data[4], $kgm_uuid_data[5]
            printf "%02.2X%02.2X-", $kgm_uuid_data[6], $kgm_uuid_data[7]
            printf "%02.2X%02.2X-", $kgm_uuid_data[8], $kgm_uuid_data[9]
            printf "%02.2X%02.2X%02.2X%02.2X%02.2X%02.2X", $kgm_uuid_data[10], $kgm_uuid_data[11], $kgm_uuid_data[12], $kgm_uuid_data[13], $kgm_uuid_data[14], $kgm_uuid_data[15]

            _unmap_user_data_from_task $kgm_taskp

            printf "  "
            _print_path_for_image $kgm_mh_path_address
            printf "\n"

            loop_break
        else
            if $kgm_lc_cmd == 0xe
                set $kgm_load_dylinker_data = $kgm_lc_data
                set $kgm_dyld_load_path = $kgm_lc_address + *((unsigned int *)$kgm_load_dylinker_data)
            end
            _unmap_user_data_from_task $kgm_taskp
        end

        set $kgm_lc_address = $kgm_lc_address + $kgm_lc_cmd_size
        set $kgm_lc_idx = $kgm_lc_idx + 1
    end
    
    if (!$kgm_uuid_data)
            # didn't find LC_UUID, for a dylib, just print out basic info
            if $kgm_mh_64
                printf "0x%016llx  ", $kgm_mh_image_address
            else
                printf "0x%08x  ", $kgm_mh_image_address                          
            end
            set $kgm_printed_type = 0
            if $kgm_mh_filetype == 0x2
                printf "MH_EXECUTE   "
                set $kgm_printed_type = 1
            end
            if $kgm_mh_filetype == 0x6
                printf "MH_DYLIB     "
                set $kgm_printed_type = 1
            end
            if $kgm_mh_filetype == 0x7
                printf "MH_DYLINKER  "
                set $kgm_printed_type = 1
            end
            if $kgm_mh_filetype == 0x8
                printf "MH_BUNDLE    "
                set $kgm_printed_type = 1
            end
            if !$kgm_printed_type
                printf "UNKNOWN      "            
            end
        printf "                                    ",

        printf "  "
        _print_path_for_image $kgm_mh_path_address
        printf "\n"

    end
    
end

define _print_images_for_dyld_image_info
    set $kgm_taskp = $arg0
    set $kgm_task_64 = $arg1
    set $kgm_dyld_all_image_infos_address = (unsigned long long)$arg2

    _map_user_data_from_task $kgm_taskp $kgm_dyld_all_image_infos_address 112

    set $kgm_dyld_all_image_infos = (unsigned int *)$kgm_map_user_window    
    set $kgm_dyld_all_image_infos_version = $kgm_dyld_all_image_infos[0]
    if ($kgm_dyld_all_image_infos_version > 12)
        printf "Unknown dyld all_image_infos version number %d\n", $kgm_dyld_all_image_infos_version
    end
    set $kgm_image_info_count = $kgm_dyld_all_image_infos[1]

    set $kgm_dyld_load_path = 0    
    if $kgm_task_64
        set $kgm_image_info_size = 24
        set $kgm_image_info_array_address = ((unsigned long long *)$kgm_dyld_all_image_infos)[1]
        set $kgm_dyld_load_address = ((unsigned long long *)$kgm_dyld_all_image_infos)[4]
    else
        set $kgm_image_info_size = 12
        set $kgm_image_info_array_address = ((unsigned int *)$kgm_dyld_all_image_infos)[2]
        set $kgm_dyld_load_address = ((unsigned int *)$kgm_dyld_all_image_infos)[5]
    end

    _unmap_user_data_from_task $kgm_taskp

    set $kgm_image_info_i = 0
    while $kgm_image_info_i < $kgm_image_info_count

        set $kgm_image_info_address = $kgm_image_info_array_address + $kgm_image_info_size*$kgm_image_info_i

        _map_user_data_from_task $kgm_taskp $kgm_image_info_address $kgm_image_info_size
        if $kgm_task_64
            set $kgm_image_info_addr = ((unsigned long long *)$kgm_map_user_window)[0]
            set $kgm_image_info_path = ((unsigned long long *)$kgm_map_user_window)[1]
        else
            set $kgm_image_info_addr = ((unsigned int *)$kgm_map_user_window)[0]
            set $kgm_image_info_path = ((unsigned int *)$kgm_map_user_window)[1]
        end
        _unmap_user_data_from_task $kgm_taskp
 
        # printf "[%d] = image address %llx path address %llx\n", $kgm_image_info_i, $kgm_image_info_addr, $kgm_image_info_path
        _print_image_info $kgm_image_info_addr $kgm_image_info_path

        set $kgm_image_info_i = $kgm_image_info_i + 1
    end

    # $kgm_dyld_load_path may get set when the main executable is processed
    # printf "[dyld] = image address %llx path address %llx\n", $kgm_dyld_load_address, $kgm_dyld_load_path
    _print_image_info $kgm_dyld_load_address $kgm_dyld_load_path

end

define showuserlibraries
	set $kgm_taskp = (task_t)$arg0
	set $kgm_dyld_image_info = $kgm_taskp->all_image_info_addr

	set $kgm_map = $kgm_taskp->map
	set $kgm_task_64 = ( $kgm_taskp->taskFeatures[0] & 0x80000000)

	if ($kgm_dyld_image_info != 0)
		printf "address   "
		if $kgm_task_64
			printf "        "
		end
		printf "  type       "
		printf "  uuid                                  "
		printf "path\n"

		_print_images_for_dyld_image_info $kgm_taskp $kgm_task_64 $kgm_dyld_image_info
	else
		printf "No dyld shared library information available for task\n"
	end
end
document showuserlibraries
Syntax: (gdb) showuserlibraries <task_t>
| For a given user task, inspect the dyld shared library state and print 
| information about all Mach-O images.
end

define showuserdyldinfo
	set $kgm_taskp = (task_t)$arg0
	set $kgm_dyld_all_image_infos_address = (unsigned long long)$kgm_taskp->all_image_info_addr

	set $kgm_map = $kgm_taskp->map
	set $kgm_task_64 = ( $kgm_taskp->taskFeatures[0] & 0x80000000)

	if ($kgm_dyld_all_image_infos_address != 0)

	   _map_user_data_from_task $kgm_taskp $kgm_dyld_all_image_infos_address 112

	   set $kgm_dyld_all_image_infos = (unsigned char *)$kgm_map_user_window
	   set $kgm_dyld_all_image_infos_version = ((unsigned int *)$kgm_dyld_all_image_infos)[0]
	   if ($kgm_dyld_all_image_infos_version > 12)
		  printf "Unknown dyld all_image_infos version number %d\n", $kgm_dyld_all_image_infos_version
	   end

	   # Find fields by byte offset. We assume at least version 9 is supported
	   if $kgm_task_64
		  set $kgm_dyld_all_image_infos_infoArrayCount = *(unsigned int *)(&$kgm_dyld_all_image_infos[4])
		  set $kgm_dyld_all_image_infos_infoArray = *(unsigned long long *)(&$kgm_dyld_all_image_infos[8])
		  set $kgm_dyld_all_image_infos_notification = *(unsigned long long *)(&$kgm_dyld_all_image_infos[16])
		  set $kgm_dyld_all_image_infos_processDetachedFromSharedRegion = *(unsigned char *)(&$kgm_dyld_all_image_infos[24])
		  set $kgm_dyld_all_image_infos_libSystemInitialized = *(unsigned char *)(&$kgm_dyld_all_image_infos[25])
		  set $kgm_dyld_all_image_infos_dyldImageLoadAddress = *(unsigned long long *)(&$kgm_dyld_all_image_infos[32])
		  set $kgm_dyld_all_image_infos_jitInfo = *(unsigned long long *)(&$kgm_dyld_all_image_infos[40])
		  set $kgm_dyld_all_image_infos_dyldVersion = *(unsigned long long *)(&$kgm_dyld_all_image_infos[48])
		  set $kgm_dyld_all_image_infos_errorMessage = *(unsigned long long *)(&$kgm_dyld_all_image_infos[56])
		  set $kgm_dyld_all_image_infos_terminationFlags = *(unsigned long long *)(&$kgm_dyld_all_image_infos[64])
		  set $kgm_dyld_all_image_infos_coreSymbolicationShmPage = *(unsigned long long *)(&$kgm_dyld_all_image_infos[72])
		  set $kgm_dyld_all_image_infos_systemOrderFlag = *(unsigned long long *)(&$kgm_dyld_all_image_infos[80])
		  set $kgm_dyld_all_image_infos_uuidArrayCount = *(unsigned long long *)(&$kgm_dyld_all_image_infos[88])
		  set $kgm_dyld_all_image_infos_uuidArray = *(unsigned long long *)(&$kgm_dyld_all_image_infos[96])
		  set $kgm_dyld_all_image_infos_dyldAllImageInfosAddress = *(unsigned long long *)(&$kgm_dyld_all_image_infos[104])
	   else
		  set $kgm_dyld_all_image_infos_infoArrayCount = *(unsigned int *)(&$kgm_dyld_all_image_infos[4])
		  set $kgm_dyld_all_image_infos_infoArray = *(unsigned int *)(&$kgm_dyld_all_image_infos[8])
		  set $kgm_dyld_all_image_infos_notification = *(unsigned int *)(&$kgm_dyld_all_image_infos[12])
		  set $kgm_dyld_all_image_infos_processDetachedFromSharedRegion = *(unsigned char *)(&$kgm_dyld_all_image_infos[16])
		  set $kgm_dyld_all_image_infos_libSystemInitialized = *(unsigned char *)(&$kgm_dyld_all_image_infos[17])
		  set $kgm_dyld_all_image_infos_dyldImageLoadAddress = *(unsigned int *)(&$kgm_dyld_all_image_infos[20])
		  set $kgm_dyld_all_image_infos_jitInfo = *(unsigned int *)(&$kgm_dyld_all_image_infos[24])
		  set $kgm_dyld_all_image_infos_dyldVersion = *(unsigned int *)(&$kgm_dyld_all_image_infos[28])
		  set $kgm_dyld_all_image_infos_errorMessage = *(unsigned int *)(&$kgm_dyld_all_image_infos[32])
		  set $kgm_dyld_all_image_infos_terminationFlags = *(unsigned int *)(&$kgm_dyld_all_image_infos[36])
		  set $kgm_dyld_all_image_infos_coreSymbolicationShmPage = *(unsigned int *)(&$kgm_dyld_all_image_infos[40])
		  set $kgm_dyld_all_image_infos_systemOrderFlag = *(unsigned int *)(&$kgm_dyld_all_image_infos[44])
		  set $kgm_dyld_all_image_infos_uuidArrayCount = *(unsigned int *)(&$kgm_dyld_all_image_infos[48])
		  set $kgm_dyld_all_image_infos_uuidArray = *(unsigned int *)(&$kgm_dyld_all_image_infos[52])
		  set $kgm_dyld_all_image_infos_dyldAllImageInfosAddress = *(unsigned int *)(&$kgm_dyld_all_image_infos[56])
	   end

	   _unmap_user_data_from_task $kgm_taskp

	   printf "                        version %u\n", $kgm_dyld_all_image_infos_version
	   printf "                 infoArrayCount %u\n", $kgm_dyld_all_image_infos_infoArrayCount
	   printf "                      infoArray "
	   showuserptr $kgm_dyld_all_image_infos_infoArray
	   printf "\n"
	   printf "                   notification "
	   showuserptr $kgm_dyld_all_image_infos_notification
	   printf "\n"
	   printf "processDetachedFromSharedRegion %d\n", $kgm_dyld_all_image_infos_processDetachedFromSharedRegion
	   printf "           libSystemInitialized %d\n", $kgm_dyld_all_image_infos_libSystemInitialized
	   printf "           dyldImageLoadAddress "
	   showuserptr $kgm_dyld_all_image_infos_dyldImageLoadAddress
	   printf "\n"
	   printf "                        jitInfo "
	   showuserptr $kgm_dyld_all_image_infos_jitInfo
	   printf "\n"
	   printf "                    dyldVersion "
	   showuserptr $kgm_dyld_all_image_infos_dyldVersion
	   printf "\n"
	   printf "                                "
	   _print_path_for_image $kgm_dyld_all_image_infos_dyldVersion
	   printf "\n"

	   printf "                   errorMessage "
	   showuserptr $kgm_dyld_all_image_infos_errorMessage
	   printf "\n"
	   if $kgm_dyld_all_image_infos_errorMessage != 0
		  printf "                                "
		  _print_path_for_image $kgm_dyld_all_image_infos_errorMessage
		  printf "\n"
	   end

	   printf "               terminationFlags "
	   showuserptr $kgm_dyld_all_image_infos_terminationFlags
	   printf "\n"
	   printf "       coreSymbolicationShmPage "
	   showuserptr $kgm_dyld_all_image_infos_coreSymbolicationShmPage
	   printf "\n"
	   printf "                systemOrderFlag "
	   showuserptr $kgm_dyld_all_image_infos_systemOrderFlag
	   printf "\n"
	   printf "                 uuidArrayCount "
	   showuserptr $kgm_dyld_all_image_infos_uuidArrayCount
	   printf "\n"
	   printf "                      uuidArray "
	   showuserptr $kgm_dyld_all_image_infos_uuidArray
	   printf "\n"
	   printf "       dyldAllImageInfosAddress "
	   showuserptr $kgm_dyld_all_image_infos_dyldAllImageInfosAddress
	   printf "\n"
	   printf "                                (currently "
	   showuserptr $kgm_dyld_all_image_infos_address
	   printf ")\n"

	   if $kgm_task_64
		  set $kgm_dyld_all_image_infos_address = $kgm_dyld_all_image_infos_address + 112
		  _map_user_data_from_task $kgm_taskp $kgm_dyld_all_image_infos_address 64
		  set $kgm_dyld_all_image_infos_v10 = (unsigned char *)$kgm_map_user_window
		  set $kgm_dyld_all_image_infos_initialImageCount = *(unsigned long long *)(&$kgm_dyld_all_image_infos_v10[112-112])
		  set $kgm_dyld_all_image_infos_errorKind = *(unsigned long long *)(&$kgm_dyld_all_image_infos_v10[120-112])
		  set $kgm_dyld_all_image_infos_errorClientOfDylibPath = *(unsigned long long *)(&$kgm_dyld_all_image_infos_v10[128-112])
		  set $kgm_dyld_all_image_infos_errorTargetDylibPath = *(unsigned long long *)(&$kgm_dyld_all_image_infos_v10[136-112])
		  set $kgm_dyld_all_image_infos_errorSymbol = *(unsigned long long *)(&$kgm_dyld_all_image_infos_v10[144-112])
		  set $kgm_dyld_all_image_infos_sharedCacheSlide = *(unsigned long long *)(&$kgm_dyld_all_image_infos_v10[152-112])

		  _unmap_user_data_from_task $kgm_taskp
	   else
		  set $kgm_dyld_all_image_infos_address = $kgm_dyld_all_image_infos_address + 60
		  _map_user_data_from_task $kgm_taskp $kgm_dyld_all_image_infos_address 64
		  set $kgm_dyld_all_image_infos_v10 = (unsigned char *)$kgm_map_user_window
		  set $kgm_dyld_all_image_infos_initialImageCount = *(unsigned int *)(&$kgm_dyld_all_image_infos_v10[60-60])
		  set $kgm_dyld_all_image_infos_errorKind = *(unsigned int *)(&$kgm_dyld_all_image_infos_v10[64-60])
		  set $kgm_dyld_all_image_infos_errorClientOfDylibPath = *(unsigned int *)(&$kgm_dyld_all_image_infos_v10[68-60])
		  set $kgm_dyld_all_image_infos_errorTargetDylibPath = *(unsigned int *)(&$kgm_dyld_all_image_infos_v10[72-60])
		  set $kgm_dyld_all_image_infos_errorSymbol = *(unsigned int *)(&$kgm_dyld_all_image_infos_v10[76-60])
		  set $kgm_dyld_all_image_infos_sharedCacheSlide = *(unsigned int *)(&$kgm_dyld_all_image_infos_v10[80-60])
		  _unmap_user_data_from_task $kgm_taskp
	   end

	   if $kgm_dyld_all_image_infos_version >= 10
		  printf "              initialImageCount "
		  showuserptr $kgm_dyld_all_image_infos_initialImageCount
		  printf "\n"
	   end

	   if $kgm_dyld_all_image_infos_version >= 11
		  printf "                      errorKind "
		  showuserptr $kgm_dyld_all_image_infos_errorKind
		  printf "\n"
		  printf "         errorClientOfDylibPath "
		  showuserptr $kgm_dyld_all_image_infos_errorClientOfDylibPath
		  printf "\n"
		  if $kgm_dyld_all_image_infos_errorClientOfDylibPath != 0
			 printf "                                "
			 _print_path_for_image $kgm_dyld_all_image_infos_errorClientOfDylibPath
			 printf "\n"
		  end
		  printf "           errorTargetDylibPath "
		  showuserptr $kgm_dyld_all_image_infos_errorTargetDylibPath
		  printf "\n"
		  if $kgm_dyld_all_image_infos_errorTargetDylibPath != 0
			 printf "                                "
			 _print_path_for_image $kgm_dyld_all_image_infos_errorTargetDylibPath
			 printf "\n"
		  end
		  printf "                    errorSymbol "
		  showuserptr $kgm_dyld_all_image_infos_errorSymbol
		  printf "\n"
		  if $kgm_dyld_all_image_infos_errorSymbol != 0
			 printf "                                "
			 _print_path_for_image $kgm_dyld_all_image_infos_errorSymbol
			 printf "\n"
		  end
	   end

	   if $kgm_dyld_all_image_infos_version >= 12
		  printf "               sharedCacheSlide "
		  showuserptr $kgm_dyld_all_image_infos_sharedCacheSlide
		  printf "\n"
	   end

	else
		printf "No dyld information available for task\n"
	end
end
document showuserdyldinfo
Syntax: (gdb) showuserdyldinfo <task_t>
| For a given user task, inspect the dyld global info and print
| out all fields, including error messages.
end

define showkerneldebugheader
	printf "kd_buf     "
	showptrhdrpad
	printf "CPU  Thread     "
	showptrhdrpad
	printf "Timestamp         S/E Class Sub   Code   Code Specific Info\n"
end

define _printevflags
	if $arg0 & 1
		printf "EV_RE "
	end
	if $arg0 & 2
		printf "EV_WR "
	end
	if $arg0 & 4
		printf "EV_EX "
	end
	if $arg0 & 8
		printf "EV_RM "
	end

	if $arg0 & 0x00100
		printf "EV_RBYTES "
	end
	if $arg0 & 0x00200
		printf "EV_WBYTES "
	end
	if $arg0 & 0x00400
		printf "EV_RCLOSED "
	end
	if $arg0 & 0x00800
		printf "EV_RCONN "
	end
	if $arg0 & 0x01000
		printf "EV_WCLOSED "
	end
	if $arg0 & 0x02000
		printf "EV_WCONN "
	end
	if $arg0 & 0x04000
		printf "EV_OOB "
	end
	if $arg0 & 0x08000
		printf "EV_FIN "
	end
	if $arg0 & 0x10000
		printf "EV_RESET "
	end
	if $arg0 & 0x20000
		printf "EV_TIMEOUT "
	end
end

define showkerneldebugbufferentry
	set $kgm_kdebug_entry = (kd_buf *) $arg0
	
	set $kgm_debugid     = $kgm_kdebug_entry->debugid
	set $kgm_kdebug_arg1 = $kgm_kdebug_entry->arg1
	set $kgm_kdebug_arg2 = $kgm_kdebug_entry->arg2
	set $kgm_kdebug_arg3 = $kgm_kdebug_entry->arg3
	set $kgm_kdebug_arg4 = $kgm_kdebug_entry->arg4
	
	if $kgm_lp64
		set $kgm_kdebug_cpu	 = $kgm_kdebug_entry->cpuid
		set $kgm_ts_hi	     = ($kgm_kdebug_entry->timestamp >> 32) & 0xFFFFFFFF
		set $kgm_ts_lo	     = $kgm_kdebug_entry->timestamp & 0xFFFFFFFF
	else
		set $kgm_kdebug_cpu	 = ($kgm_kdebug_entry->timestamp >> 56)
		set $kgm_ts_hi	     = ($kgm_kdebug_entry->timestamp >> 32) & 0x00FFFFFF
		set $kgm_ts_lo	     = $kgm_kdebug_entry->timestamp & 0xFFFFFFFF
	end
	
	set $kgm_kdebug_class    = ($kgm_debugid >> 24) & 0x000FF
	set $kgm_kdebug_subclass = ($kgm_debugid >> 16) & 0x000FF
	set $kgm_kdebug_code     = ($kgm_debugid >>  2) & 0x03FFF
	set $kgm_kdebug_qual     = ($kgm_debugid      ) & 0x00003
	
	if $kgm_kdebug_qual == 0
		set $kgm_kdebug_qual = '-'
	else
		if $kgm_kdebug_qual == 1
			set $kgm_kdebug_qual = 'S'
		else
			if $kgm_kdebug_qual == 2
				set $kgm_kdebug_qual = 'E'
			else
				if $kgm_kdebug_qual == 3
					set $kgm_kdebug_qual = '?'
				end
			end
		end
	end

	# preamble and qual
	
	showptr $kgm_kdebug_entry
	printf "  %d   ", $kgm_kdebug_cpu
	showptr $kgm_kdebug_entry->arg5
	printf " 0x%08X%08X %c  ", $kgm_ts_hi, $kgm_ts_lo, $kgm_kdebug_qual
	
	# class
	
	if $kgm_kdebug_class == 1
		printf "MACH"
	else
	if $kgm_kdebug_class == 2
		printf "NET "
	else
	if $kgm_kdebug_class == 3
		printf "FS  "
	else
	if $kgm_kdebug_class == 4
		printf "BSD "
	else
	if $kgm_kdebug_class == 5
		printf "IOK "
	else
	if $kgm_kdebug_class == 6
		printf "DRVR"
	else
	if $kgm_kdebug_class == 7
		printf "TRAC"
	else
	if $kgm_kdebug_class == 8
		printf "DLIL"
	else
	if $kgm_kdebug_class == 8
		printf "SEC "
	else
	if $kgm_kdebug_class == 20
		printf "MISC"
	else
	if $kgm_kdebug_class == 31
		printf "DYLD"
	else
	if $kgm_kdebug_class == 32
		printf "QT  "
	else
	if $kgm_kdebug_class == 33
		printf "APPS"
	else
	if $kgm_kdebug_class == 255
		printf "MIG "
	else
		printf "0x%02X", $kgm_kdebug_class
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	
	# subclass and code
	
	printf "  0x%02X %5d   ", $kgm_kdebug_subclass, $kgm_kdebug_code

	# space for debugid-specific processing
	
	# EVPROC from bsd/kern/sys_generic.c
	
	# MISCDBG_CODE(DBG_EVENT,DBG_WAIT)
	if $kgm_debugid == 0x14100048
		printf "waitevent "
		if $kgm_kdebug_arg1 == 1
			printf "before sleep"
		else
		if $kgm_kdebug_arg1 == 2
			printf "after  sleep"
		else
			printf "????????????"
		end
		end
		printf " chan=0x%08X ", $kgm_kdebug_arg2
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_WAIT|DBG_FUNC_START)
	if $kgm_debugid == 0x14100049
		printf "waitevent "
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_WAIT|DBG_FUNC_END)
	if $kgm_debugid == 0x1410004a
		printf "waitevent error=%d ", $kgm_kdebug_arg1
		printf "eqp=0x%08X ", $kgm_kdebug_arg4
		_printevflags $kgm_kdebug_arg3
		printf "er_handle=%d ", $kgm_kdebug_arg2
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE|DBG_FUNC_START)
	if $kgm_debugid == 0x14100059
		printf "evprocdeque proc=0x%08X ", $kgm_kdebug_arg1
		if $kgm_kdebug_arg2 == 0
			printf "remove first "
		else
			printf "remove 0x%08X ", $kgm_kdebug_arg2
		end
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_DEQUEUE|DBG_FUNC_END)
	if $kgm_debugid == 0x1410005a
		printf "evprocdeque "
		if $kgm_kdebug_arg1 == 0
			printf "result=NULL "
		else
			printf "result=0x%08X ", $kgm_kdebug_arg1
		end
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_POST|DBG_FUNC_START)
	if $kgm_debugid == 0x14100041
		printf "postevent "
		_printevflags $kgm_kdebug_arg1
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_POST)
	if $kgm_debugid == 0x14100040
		printf "postevent "
		printf "evq=0x%08X ", $kgm_kdebug_arg1
		printf "er_eventbits="
		_printevflags $kgm_kdebug_arg2
		printf "mask="
		_printevflags $kgm_kdebug_arg3
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_POST|DBG_FUNC_END)
	if $kgm_debugid == 0x14100042
		printf "postevent "
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE|DBG_FUNC_START)
	if $kgm_debugid == 0x14100055
		printf "evprocenque eqp=0x%08d ", $kgm_kdebug_arg1
		if $kgm_kdebug_arg2 & 1
			printf "EV_QUEUED "
		end
		_printevflags $kgm_kdebug_arg3
	else
	
	# MISCDBG_CODE(DBG_EVENT,DBG_EWAKEUP)
	if $kgm_debugid == 0x14100050
		printf "evprocenque before wakeup eqp=0x%08d ", $kgm_kdebug_arg4
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_ENQUEUE|DBG_FUNC_END)
	if $kgm_debugid == 0x14100056
		printf "evprocenque "
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_MOD|DBG_FUNC_START)
	if $kgm_debugid == 0x1410004d
		printf "modwatch "
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_MOD)
	if $kgm_debugid == 0x1410004c
		printf "modwatch er_handle=%d ", $kgm_kdebug_arg1
		_printevflags $kgm_kdebug_arg2
		printf "evq=0x%08X ", $kgm_kdebug_arg3
	else
	# MISCDBG_CODE(DBG_EVENT,DBG_MOD|DBG_FUNC_END)
	if $kgm_debugid == 0x1410004e
		printf "modwatch er_handle=%d ", $kgm_kdebug_arg1
		printf "ee_eventmask="
		_printevflags $kgm_kdebug_arg2
		printf "sp=0x%08X ", $kgm_kdebug_arg3
		printf "flag="
		_printevflags $kgm_kdebug_arg4
	else
		printf "arg1=0x%08X ", $kgm_kdebug_arg1
		printf "arg2=0x%08X ", $kgm_kdebug_arg2
		printf "arg3=0x%08X ", $kgm_kdebug_arg3
		printf "arg4=0x%08X ", $kgm_kdebug_arg4
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	end
	
	# finish up
	
	printf "\n"
end

define showkerneldebugbuffercpu
	set $kgm_cpu_number = (int) $arg0
	set $kgm_entry_count = (int) $arg1
	set $kgm_debugentriesfound = 0
	
	if (kdebug_flags & 0x80000000)	# 0x80000000 == KDBG_BFINIT
		showkerneldebugheader
		
		if $kgm_entry_count == 0
			printf "<count> is 0, dumping 50 entries\n"
			set $kgm_entry_count = 50
		end
	
		if $kgm_cpu_number >= kd_cpus
			printf "cpu number too big\n"
		else
			set $kgm_kdbp = &kdbip[$kgm_cpu_number]
			set $kgm_kdsp = $kgm_kdbp->kd_list_head
			while (($kgm_kdsp != 0) && ($kgm_entry_count > 0))
				if $kgm_kdsp->kds_readlast != $kgm_kdsp->kds_bufptr
					set $kgm_kds_bufptr = $kgm_kdsp->kds_bufptr
					while (($kgm_kds_bufptr > $kgm_kdsp->kds_readlast) && ($kgm_entry_count > 0))
						set $kgm_kds_bufptr = $kgm_kds_bufptr - 1
						set $kgm_entry_count = $kgm_entry_count - 1
						showkerneldebugbufferentry $kgm_kds_bufptr
					end
				end
				set $kgm_kdsp = $kgm_kdsp->kds_next
			end
		end
	else
		printf "Trace buffer not enabled\n"
	end
end

document showkerneldebugbuffercpu
Syntax:  showkerneldebugbuffercpu <cpu> <count>
| Prints the last N entries in the kernel debug buffer for CPU x.
end

define showkerneldebugbuffer
	
	if (kdebug_flags & 0x80000000)	# 0x80000000 == KDBG_BFINIT
	
		set $kgm_entrycount = (int) $arg0
	
		if $kgm_entrycount == 0
			printf "<count> is 0, dumping 50 entries per cpu\n"
			set $kgm_entrycount = 50
		end
	
		set $kgm_cpu = (int) 0
	
		while $kgm_cpu < kd_cpus
			showkerneldebugbuffercpu $kgm_cpu $kgm_entrycount
			set $kgm_cpu = $kgm_cpu + 1
		end
	else
		printf "Trace buffer not enabled\n"
	end
end

document showkerneldebugbuffer
Syntax: showkerneldebugbuffer <count>
| Prints the last N entries in the kernel debug buffer per cpu. i.e. showkerneldebugbuffer 50 will
| display the last 50 entries in each CPU's debug buffer.
end

define showallvmstats
  printf "     pid           command    #ents           wired           vsize   rsize       max rsize\n"
  printf "                                            (pages)         (pages)   (pages)         (pages)\n"
  set $kgm_head_taskp = &tasks
  set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
  while $kgm_taskp != $kgm_head_taskp
    set $kgm_procp = (struct proc *)($kgm_taskp->bsd_info)
    set $kgm_mapp = (struct _vm_map *)($kgm_taskp->map)
    printf "%8d %17s %8d %15d %15d %15d %15d\n", $kgm_procp->p_pid, $kgm_procp->p_comm, $kgm_mapp->hdr.nentries, $kgm_mapp->pmap->stats.wired_count, $kgm_mapp->size >> 12, $kgm_mapp->pmap->stats.resident_count, $kgm_mapp->pmap->stats.resident_max
    set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
  end
end

document showallvmstats
Syntax: showallvmstats
| prints a summary of vm statistics in a table format
end

define memstats
	if ($kgm_mtype == $kgm_mtype_arm)
		printf "kern_memorystatus_level:  %8d\n", kern_memorystatus_level
	end	
	printf "vm_page_throttled_count:  %8d\n", vm_page_throttled_count 
	printf "vm_page_active_count:     %8d\n", vm_page_active_count
	printf "vm_page_inactive_count:   %8d\n", vm_page_inactive_count
	printf "vm_page_wire_count:       %8d\n", vm_page_wire_count
	printf "vm_page_free_count:       %8d\n", vm_page_free_count
	printf "vm_page_purgeable_count:  %8d\n", vm_page_purgeable_count
	printf "vm_page_inactive_target:  %8d\n", vm_page_inactive_target
	printf "vm_page_free_target:      %8d\n", vm_page_free_target
	printf "inuse_ptepages_count:     %8d\n", inuse_ptepages_count
	printf "vm_page_free_reserved:    %8d\n", vm_page_free_reserved
end

document memstats
Syntax: (gdb) memstats 
| Prints out a summary of various memory statistics. In particular vm_page_wire_count should
| be greater than 2K or you are under memory pressure.
end

define show_user_registers
       showuserregisters $arg0
end

document show_user_registers
Syntax: show_user_registers <thread_address>
| Display user registers associated with a kernel thread
| properly displays the 32 bit or 64 bit registers for intel architecture
end

define _cmp
       set $cmp0 = $arg0
       set $cmp1 = $arg1

       # check for end of string. cmp0 can be longer than cmp1. it
       # can't be shorter.
       if $cmp1 == '\0'
       	  set $kgm_strcmp_result = 0
	  set $kgm_strcmp_done   = 1
       end

       if !$kgm_strcmp_done && $cmp0 == '\0'
       	  set $kgm_strcmp_result = -1
       	  set $kgm_strcmp_done   =  1
       end

       # do they match?
       if !$kgm_strcmp_done 
       	  set $kgm_strcmp_result = (uint8_t) $cmp0 - (uint8_t) $cmp1
       	  if $kgm_strcmp_result != 0
       	     set $kgm_strcmp_done = 1
	  end
       end
end

define _cmp_arg64
       set $cmp = $arg1
       set $masked = $cmp & 0xFF
       _cmp $arg0[0] $masked

       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[1] $masked
       end
       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[2] $masked
       end
       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[3] $masked
       end
       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[4] $masked
       end
       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[5] $masked
       end
       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[6] $masked
       end
       if !$kgm_strcmp_done
       	  set $cmp = $cmp >> 8
	  set $masked  = $cmp & 0xFF
	  _cmp $arg0[7] $masked
       end
end

define strcmp_arg_pack64
       set $kgm_strcmp_arg = ((((((((((((((uint64_t) $arg7 << 8) | $arg6) << 8) | $arg5) << 8) | $arg4) << 8) | $arg3) << 8) | $arg2) << 8) | $arg1) << 8) | $arg0
end

document strcmp_arg_pack64
Syntax: strcmp_arg_pack64 <a> <b> <c> <d> <e <f> <g> <h>
| Packs a string given as 8 character arguments into a 64-bit int stored in 
| $kgm_strcmp_arg. Use 0 or '\0' for unused arguments. The encoded string
| is suitable for use by strcmp_nomalloc and setfindregistrystr.
| e.g., strcmp_arg_pack64 'H' 'e' 'l' 'l' 'o' 0 0 0 
|       packs "Hello" into $kgm_strcmp_arg.
| 
end

define strcmp_nomalloc
       set $str   = $arg0
       set $count = $argc - 1

       set $kgm_strcmp_result = 0
       set $kgm_strcmp_done   = 0

       if $count > 0 
       	  _cmp_arg64 $str $arg1
       end
       if !$kgm_strcmp_done && $count > 1
          set $str = $str + 8
       	  _cmp_arg64 $str $arg2
       end
       if !$kgm_strcmp_done && $count > 2
          set $str = $str + 8
       	  _cmp_arg64 $str $arg3
       end
       if !$kgm_strcmp_done && $count > 3
          set $str = $str + 8
       	  _cmp_arg64 $str $arg4
       end
       if !$kgm_strcmp_done && $count > 4
          set $str = $str + 8
       	  _cmp_arg64 $str $arg5
       end
       if !$kgm_strcmp_done && $count > 5
          set $str = $str + 8
       	  _cmp_arg64 $str $arg6
       end
       if !$kgm_strcmp_done && $count > 6
          set $str = $str + 8
       	  _cmp_arg64 $str $arg7
       end
       if !$kgm_strcmp_done && $count > 7
          set $str = $str + 8
       	  _cmp_arg64 $str $arg8
       end
       if !$kgm_strcmp_done && $count > 8
          set $str = $str + 8
       	  _cmp_arg64 $str $arg9
       end
end

document strcmp_nomalloc
Syntax: strcmp_nomalloc <string> <a> [b] [c] [d] [e] [f] [g] [h] [i]
| Given a pre-allocated <string>, perform a string compare with the 
| encoded string stored in arguments a - i. The result is stored in
| $kgm_strcmp_result.
|
| For example, the following will result in $kgm_strcmp_result == 0:
| strcmp_arg_pack64 'D' 'a' 'r' 'w' 'i' 'n' ' ' 'K' 
| strcmp_nomalloc version $kgm_strcmp_arg
end

define memcpy
    set $kgm_dst = (unsigned char *)$arg0
    set $kgm_src = (unsigned char *)$arg1
    set $kgm_count = $arg2

    # printf "src %p dst %p len %d\n", $kgm_src, $kgm_dst, $kgm_count

    while ($kgm_count >= 8)
        set *(unsigned long long *)$kgm_dst = *(unsigned long long *)$kgm_src

        set $kgm_dst = $kgm_dst + 8
        set $kgm_src = $kgm_src + 8
        set $kgm_count = $kgm_count - 8
    end
    while ($kgm_count > 0)
        set *$kgm_dst = *$kgm_src

        set $kgm_dst = $kgm_dst + 1
        set $kgm_src = $kgm_src + 1
        set $kgm_count = $kgm_count - 1
    end
end

document memcpy
Syntax: memcpy <dst> <src> <n>
| Given two addresses that are accessible by the debugger, perform
| a memory copy of <n> bytes from <src> to <dst>
end

# _pci_cfg_addr_value $addr $size
define _pci_cfg_addr_value
   readphysint $arg0 $arg1 $kgm_lcpu_self
   set $kgm_pci_cfg_value = $kgm_readphysint_result
end


set $kgm_pci_cfg_init = 0
define _pci_cfg_init
       # get this from the registry if it exists there
       if $kgm_pci_cfg_init == 0
       	  strcmp_arg_pack64 'A' 'p' 'p' 'l' 'e' 'A' 'C' 'P'
       	  set $AppleACP = $kgm_strcmp_arg
       	  strcmp_arg_pack64 'I' 'P' 'l' 'a' 't' 'f' 'o' 'r'
      	  set $IPlatfor = $kgm_strcmp_arg
       	  strcmp_arg_pack64 'm' 'E' 'x' 'p' 'e' 'r' 't' 0
       	  set $mExpert = $kgm_strcmp_arg
	  setfindregistrystr $AppleACP $IPlatfor $mExpert

	  set $olddepth = $kgm_reg_depth_max
	  set $kgm_reg_depth_max = 2
	  _findregistryentry 
	  set $kgm_reg_depth_max = $olddepth

	  if $kgm_registry_entry
	     strcmp_arg_pack64 'a' 'c' 'p' 'i' '-' 'm' 'm' 'c' 
	     set $acpi_mmc = $kgm_strcmp_arg
	     strcmp_arg_pack64 'f' 'g' '-' 's' 'e' 'g' '0' 0
	     set $fg_seg0 = $kgm_strcmp_arg
	     setfindregistrystr $acpi_mmc $fg_seg0

	     _findregistryprop $kgm_registry_entry
	     if $kgm_registry_value
	     	set $kgm_pci_cfg_base = ((OSNumber *) $kgm_registry_value)->value
		set $kgm_pci_cfg_init = 1
	     end
	  end
       end

       # search for 0:0:0 in likely places if the above fails
       if $kgm_pci_cfg_init == 0
       	  set $kgm_pci_cfg_base = 0xF0000000
	  while $kgm_pci_cfg_init == 0 && $kgm_pci_cfg_base > 0xA0000000
	      _pci_cfg_addr_value $kgm_pci_cfg_base 8
	      if $kgm_pci_cfg_value > 0x0 && $kgm_pci_cfg_value < 0xFF
	      	 set $kgm_pci_cfg_init = 1
	      else
	      	 set $kgm_pci_cfg_base = $kgm_pci_cfg_base - 0x10000000
	      end
	  end
       end
end

# _pci_cfg_addr $bus $dev $fcn $off
define _pci_cfg_addr
       set $bus = $arg0
       set $dev = $arg1
       set $fcn = $arg2
       set $off = $arg3

       _pci_cfg_init
       set $kgm_pci_cfg_addr = $kgm_pci_cfg_base | ($bus << 20) | ($dev << 15) | ($fcn << 12) | $off
end

define _pci_cfg_value
       _pci_cfg_addr $arg0 $arg1 $arg2 $arg3 
       _pci_cfg_addr_value $kgm_pci_cfg_addr $arg4
end

define pci_cfg_read8
       _pci_cfg_value $arg0 $arg1 $arg2 $arg3 8
       printf "%08X: %02X\n", $kgm_pci_cfg_addr, $kgm_pci_cfg_value
end

define pci_cfg_read16
       _pci_cfg_value $arg0 $arg1 $arg2 $arg3 16
       printf "%08X: %04X\n", $kgm_pci_cfg_addr, $kgm_pci_cfg_value
end

define pci_cfg_read32
       _pci_cfg_value $arg0 $arg1 $arg2 $arg3 32
       printf "%08X: %08X\n", $kgm_pci_cfg_addr, $kgm_pci_cfg_value
end

document pci_cfg_read8
Syntax: (gdb) pci_cfg_read8 <bus> <dev> <fcn> <off>
| read 8 bits for the given <off> of the pci device located at
| <bus>:<dev>:<fcn>. 
end

document pci_cfg_read16
Syntax: (gdb) pci_cfg_read <bus> <dev> <fcn> <off>
| read 16 bits for the given <off> of the pci device located at
| <bus>:<dev>:<fcn>.
end

document pci_cfg_read32
Syntax: (gdb) pci_cfg_read <bus> <dev> <fcn> <off>
| read 32 bits for the given <off> of the pci device located at
| <bus>:<dev>:<fcn>.
end

define pci_cfg_write8
       _pci_cfg_addr $arg0 $arg1 $arg2 $arg3 
       writephysint $kgm_pci_cfg_addr 8 $arg4 $kgm_lcpu_self
end

define pci_cfg_write16
       _pci_cfg_addr $arg0 $arg1 $arg2 $arg3 
       writephysint $kgm_pci_cfg_addr 16 $arg4 $kgm_lcpu_self
end

define pci_cfg_write32
       _pci_cfg_addr $arg0 $arg1 $arg2 $arg3 
       writephysint $kgm_pci_cfg_addr 32 $arg4 $kgm_lcpu_self
end

document pci_cfg_write8
Syntax: (gdb) pci_cfg_write8 <bus> <dev> <fcn> <off> <value>
| write an 8-bit <value> into the given <off> of the pci device located at
| <bus>:<dev>:<fcn>. 
end

document pci_cfg_write16
Syntax: (gdb) pci_cfg_write16 <bus> <dev> <fcn> <off> <value>
| write a 16-bit <value> into the given <off> of the pci device located at
| <bus>:<dev>:<fcn>. 
end

document pci_cfg_write32
Syntax: (gdb) pci_cfg_write32 <bus> <dev> <fcn> <off> <value>
| write a 32-bit <value> into the given <off> of the pci device located at
| <bus>:<dev>:<fcn>.
end


define pci_cfg_dump
       set $bus  = $arg0
       set $dev  = $arg1
       set $fcn  = $arg2
       set $off  = 0

       # check for a valid pci device
       _pci_cfg_value $bus $dev $fcn $off 8
       if $kgm_pci_cfg_value > 0x0 && $kgm_pci_cfg_value < 0xff
              printf " address: 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F\n"
              printf "---------------------------------------------------------"

              while $off < 256
       	          _pci_cfg_value $bus $dev $fcn $off 32
		  if ($off & 0xF) == 0
		     printf "\n%08X: ", $kgm_pci_cfg_addr
		  end
		  printf "%02X %02X %02X %02X ", $kgm_pci_cfg_value & 0xFF, ($kgm_pci_cfg_value >> 8) & 0xFF, ($kgm_pci_cfg_value >> 16) & 0xFF, ($kgm_pci_cfg_value >> 24) & 0xFF
	     	  set $off = $off + 4
	      end
	      printf "\n"

	      # check for pcie extended capability config space
	      _pci_cfg_value $bus $dev $fcn $off 8
	      if $kgm_pci_cfg_value < 0xff
	      	 while $off < 4096
       	      	       _pci_cfg_value $bus $dev $fcn $off 32
		       if ($off & 0xF) == 0
		       	  printf "\n%08X: ", $kgm_pci_cfg_addr
		       end
		       printf "%02X %02X %02X %02X ", $kgm_pci_cfg_value & 0xFF, ($kgm_pci_cfg_value >> 8) & 0xFF, ($kgm_pci_cfg_value >> 16) & 0xFF, ($kgm_pci_cfg_value >> 24) & 0xFF
	     	       set $off = $off + 4
	         end
	         printf "\n"
             end
       end      
end

document pci_cfg_dump
Syntax: (gdb) pci_cfg_dump <bus> <dev> <fcn> 
| dump config space for the pci device located at <bus>:<dev>:<fcn>
| if you specify an invalid/inaccessible pci device, nothing will be 
| printed out.
end

set $kgm_pci_cfg_bus_start    = 0
set $kgm_pci_cfg_bus_max      = 8
set $kgm_pci_cfg_device_max   = 32
set $kgm_pci_cfg_function_max = 8
define _pci_cfg_scan
       set $dump = $arg0

       set $bus = $kgm_pci_cfg_bus_start
       while $bus < $kgm_pci_cfg_bus_max
  	     # check for bus:0:0 to see if we should
	     # probe this bus further
	     _pci_cfg_value $bus 0x0 0x0 0x0 32
	     if $kgm_pci_cfg_value > 0 && $kgm_pci_cfg_value < 0xFFFFFFFF

	     	set $dev = 0
		while $dev < $kgm_pci_cfg_device_max

	     	   set $fcn = 0
	     	   while $fcn < $kgm_pci_cfg_function_max
		   	 _pci_cfg_value $bus $dev $fcn 0x0 32
			 if $kgm_pci_cfg_value > 0 && $kgm_pci_cfg_value < 0xFFFFFFFF
			    if $dump == 0
			       printf "%03X:%03X:%03X: %02X%02X   %02X%02X", $bus, $dev, $fcn, ($kgm_pci_cfg_value >> 8) & 0xFF, $kgm_pci_cfg_value & 0xFF, ($kgm_pci_cfg_value >> 24) & 0xFF, ($kgm_pci_cfg_value >> 16) & 0xFF 
			       _pci_cfg_value $bus $dev $fcn 0x8 32
			       printf "    %02X | %02X%02X%02X\n", $kgm_pci_cfg_value & 0xFF, ($kgm_pci_cfg_value >> 24) & 0xFF, ($kgm_pci_cfg_value >> 16) & 0xFF, ($kgm_pci_cfg_value >> 8) & 0xFF
			    else
			       printf "  device: %03X:%03X:%03X\n", $bus, $dev, $fcn			       
			       pci_cfg_dump $bus $dev $fcn	
			       printf "\n"
      			    end
			 end
			 set $fcn = $fcn + 1
		   end
		   set $dev = $dev + 1
	     	end
	     end
	     set $bus = $bus + 1
       end
end

define pci_cfg_dump_all
       _pci_cfg_scan 1
end

document pci_cfg_dump_all
Syntax: (gdb) pci_cfg_dump_all
| dump config spaces for scanned pci devices. the number of busses to scan 
| is stored in $kgm_pci_cfg_bus_max. the default for that is 8. you can also 
| specify the starting bus with $kgm_pci_cfg_bus_start. 
end

define pci_cfg_scan
       printf "bus:dev:fcn: vendor device rev | class\n"
       printf "---------------------------------------\n"
       _pci_cfg_scan 0
end

document pci_cfg_scan
Syntax: (gdb) pci_cfg_scan
| scan for pci devices. the number of busses to scan is stored in
| $kgm_pci_cfg_bus_max. the default for that is 8. you can also specify the
| starting bus with $kgm_pci_cfg_bus_start. 
end

define readioportint
       set $kgm_readioportint_result = 0xBAD10AD
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_readioport_req_t)
       set $kgm_pkt = (kdp_readioport_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request  = KDP_READIOPORT
       set $kgm_pkt->hdr.len      = sizeof(kdp_readioport_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq      = 0
       set $kgm_pkt->hdr.key      = 0
       set $kgm_pkt->address      = (uint16_t)$arg0
       set $kgm_pkt->nbytes       = $arg1 >> 3
       set $kgm_pkt->lcpu         = (uint16_t)$arg2
       set manual_pkt.input       = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_readioport_reply_t *)&manual_pkt.data
       if ($kgm_pkt->error == 0)
       	  if $arg1 == 8
	     set $kgm_readioportint_result = *((uint8_t *) $kgm_pkt->data)
	  end
       	  if $arg1 == 16
	     set $kgm_readioportint_result = *((uint16_t *) $kgm_pkt->data)
	  end
       	  if $arg1 == 32
	     set $kgm_readioportint_result = *((uint32_t *) $kgm_pkt->data)
	  end
       end
end

define readioport8
       set $lcpu = $kgm_lcpu_self
       if $argc > 1
       	  set $lcpu = $arg1
       end
       readioportint $arg0 8 $lcpu
       output /a $arg0
       printf ":\t0x%02hhx\n", $kgm_readioportint_result
end

define readioport16
       set $lcpu = $kgm_lcpu_self
       if $argc > 1
       	  set $lcpu = $arg1
       end
       readioportint $arg0 16 $lcpu
       output /a $arg0
       printf ":\t0x%04hx\n", $kgm_readioportint_result
end

define readioport32
       set $lcpu = $kgm_lcpu_self
       if $argc > 1
       	  set $lcpu = $arg1
       end
       readioportint $arg0 32 $lcpu
       output /a $arg0
       printf ":\t0x%08x\n", $kgm_readioportint_result
end

document readioport8
| See readioport32.
end

document readioport16
| See readioport32.
end

document readioport32
Syntax: (gdb) readioport32 <port> [lcpu (kernel's numbering convention)]
| Read value stored in the specified IO port. The CPU can be optionally
| specified as well.
end

define writeioportint
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_writeioport_req_t)
       set $kgm_pkt = (kdp_writeioport_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request  = KDP_WRITEIOPORT
       set $kgm_pkt->hdr.len      = sizeof(kdp_writeioport_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq      = 0
       set $kgm_pkt->hdr.key      = 0
       set $kgm_pkt->address      = (uint16_t)$arg0
       set $kgm_pkt->nbytes       = $arg1 >> 3
       set $kgm_pkt->lcpu         = (uint16_t)$arg3
       if $arg1 == 8
       	  set *(uint8_t *)$kgm_pkt->data = (uint8_t)$arg2
       end
       if $arg1 == 16
       	  set *(uint16_t *)$kgm_pkt->data = (uint16_t)$arg2
       end
       if $arg1 == 32
       	  set *(uint32_t *)$kgm_pkt->data = (uint32_t)$arg2
       end
       set manual_pkt.input = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_writeioport_reply_t *)&manual_pkt.data
       set $kgm_writeioportint_result = $kgm_pkt->error
end

define writeioport8
       set $lcpu = $kgm_lcpu_self
       if $argc > 2
       	  set $lcpu = $arg2
       end
       writeioportint $arg0 8 $arg1 $lcpu
end

define writeioport16
       set $lcpu = $kgm_lcpu_self
       if $argc > 2
       	  set $lcpu = $arg2
       end
       writeioportint $arg0 16 $arg1 $lcpu
end

define writeioport32
       set $lcpu = $kgm_lcpu_self
       if $argc > 2
       	  set $lcpu = $arg2
       end
       writeioportint $arg0 32 $arg1 $lcpu
end

document writeioport8
| See writeioport32.
end

document writeioport16
| See writeioport32.
end

document writeioport32
Syntax: (gdb) writeioport32 <port> <value> [lcpu (kernel's numbering convention)]
| Write the value to the specified IO port. The size of the value is
| determined by the name of the command. The CPU used can be optionally
| specified.
end

define readmsr64int
       set $kgm_readmsr64int_result = 0xBAD10AD
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_readmsr64_req_t)
       set $kgm_pkt = (kdp_readmsr64_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request  = KDP_READMSR64
       set $kgm_pkt->hdr.len      = sizeof(kdp_readmsr64_req_t)
       set $kgm_pkt->hdr.is_reply = 0
       set $kgm_pkt->hdr.seq      = 0
       set $kgm_pkt->hdr.key      = 0
       set $kgm_pkt->address      = (uint32_t)$arg0
       set $kgm_pkt->lcpu         = (uint16_t)$arg1
       set manual_pkt.input       = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_readmsr64_reply_t *)&manual_pkt.data
       if ($kgm_pkt->error == 0)
       	   set $kgm_readmsr64int_result = *((uint64_t *) $kgm_pkt->data)
       end
end

define readmsr64
       set $lcpu = $kgm_lcpu_self
       if $argc > 1
       	  set $lcpu = $arg1
       end
       readmsr64int $arg0 $lcpu
       output /a $arg0
       printf ":\t0x%016llx\n", $kgm_readmsr64int_result
end

define writemsr64int
       # set up the manual KDP packet
       set manual_pkt.input = 0
       set manual_pkt.len = sizeof(kdp_writemsr64_req_t)
       set $kgm_pkt = (kdp_writemsr64_req_t *)&manual_pkt.data
       set $kgm_pkt->hdr.request       = KDP_WRITEMSR64
       set $kgm_pkt->hdr.len           = sizeof(kdp_writemsr64_req_t)
       set $kgm_pkt->hdr.is_reply      = 0
       set $kgm_pkt->hdr.seq           = 0
       set $kgm_pkt->hdr.key           = 0
       set $kgm_pkt->address           = (uint32_t)$arg0
       set $kgm_pkt->lcpu              = (uint16_t)$arg2
       set *(uint64_t *)$kgm_pkt->data = (uint64_t)$arg1
       set manual_pkt.input            = 1
       # dummy to make sure manual packet is executed
       set $kgm_dummy = &_mh_execute_header
       set $kgm_pkt = (kdp_writemsr64_reply_t *)&manual_pkt.data
       set $kgm_writemsr64int_result = $kgm_pkt->error
end

define writemsr64
       set $lcpu = $kgm_lcpu_self
       if $argc > 2
       	  set $lcpu = $arg2
       end
       writemsr64int $arg0 $arg1 $lcpu
end

document writemsr64
Syntax: (gdb) writemsr64 <msr> <value> [lcpu (kernel's numbering convention)]
| Write <value> to the specified MSR. The CPU can be optionally specified.
end

document readmsr64
Syntax: (gdb) readmsr64 <msr> [lcpu (kernel's numbering convention)]
| Read the specified MSR. The CPU can be optionally specified.
end

# default if we can't find a registry entry
set $kgm_ioapic_addr           = 0xFEC00000
set $kgm_ioapic_init           = 0

set $_ioapic_index_off         = 0x00
set $_ioapic_data_off          = 0x10
set $_ioapic_eoi_off           = 0x40

set $_ioapic_index_id          = 0x00
set $_ioapic_index_ver         = 0x01
set $_ioapic_index_redir_base  = 0x10

set $_apic_vector_mask         = 0xFF
set $_apic_timer_tsc_deadline  = 0x40000
set $_apic_timer_periodic      = 0x20000
set $_apic_masked              = 0x10000
set $_apic_trigger_level       = 0x08000
set $_apic_polarity_high       = 0x02000
set $_apic_pending             = 0x01000

define _ioapic_init
       if $kgm_ioapic_init == 0
       	  strcmp_arg_pack64 'i' 'o' '-' 'a' 'p' 'i' 'c' 0
	  setfindregistrystr $kgm_strcmp_arg

	  set $olddepth = $kgm_reg_depth_max
	  set $kgm_reg_depth_max = 3
	  _findregistryentry 
	  set $kgm_reg_depth_max = $olddepth

	  if $kgm_registry_entry
	     strcmp_arg_pack64 'P' 'h' 'y' 's' 'i' 'c' 'a' 'l'
	     set $Physical = $kgm_strcmp_arg
	     strcmp_arg_pack64 ' ' 'A' 'd' 'd' 'r' 'e' 's' 's'
	     set $_Address = $kgm_strcmp_arg
	     setfindregistrystr $Physical $_Address

	     _findregistryprop $kgm_registry_entry 
	     if $kgm_registry_value
	     	set $kgm_ioapic_addr = ((OSNumber *) $kgm_registry_value)->value
	     end
	  end
	  set $kgm_ioapic_index_addr = $kgm_ioapic_addr + $_ioapic_index_off
	  set $kgm_ioapic_data_addr  = $kgm_ioapic_addr + $_ioapic_data_off
	  set $kgm_ioapic_init  = 1
       end
end

define _ioapic_addr_value
       _ioapic_init
       writephysint $kgm_ioapic_index_addr 8 $arg0 $kgm_lcpu_self
       if $argc > 1
       	  writephysint $kgm_ioapic_data_addr 32 $arg1 $kgm_lcpu_self
       else
	  readphysint $kgm_ioapic_data_addr 32 $kgm_lcpu_self
	  set $kgm_ioapic_value = $kgm_readphysint_result
       end
end

define _apic_print
       set $value = $arg0

       printf "[VEC=%3d", $value & $_apic_vector_mask
       if $value & $_apic_masked
	  printf " MASK=yes"
       else
	  printf " MASK=no "
       end       

       if $value & $_apic_trigger_level
       	  printf " TRIG=level"
       else
	  printf " TRIG=edge "
       end

       if $value & $_apic_polarity_high
       	  printf " POL=high"
       else
	  printf " POL=low "
       end

       if $value & $_apic_pending
       	  printf " PEND=yes"
       else
	  printf " PEND=no "
       end

       if $value & $_apic_timer_periodic
	  printf " PERIODIC"
       end
       if $value & $_apic_timer_tsc_deadline
	  printf " TSC_DEADLINE"
       end

       printf "]\n"
end

define ioapic_read32
   if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
       printf "ioapic_read32 not supported on this architecture.\n"
   else
       _ioapic_addr_value $arg0       
       printf "IOAPIC[0x%02X]: 0x%08X\n", $arg0, $kgm_ioapic_value
   end
end

document ioapic_read32
Syntax: (gdb) ioapic_read <offset>
| Read the IOAPIC register at the offset specified. 
end

define ioapic_write32
   if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
       printf "ioapic_write32 not supported on this architecture.\n"
   else
       _ioapic_addr_value $arg0 $arg1
   end
end

document ioapic_write32
Syntax: (gdb) ioapic_write32 <offset> <value>
| Write the IOAPIC register at the offset specified. 
end

define ioapic_dump
   if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
       printf "ioapic_dump not supported on this architecture.\n"
   else
       # id
       _ioapic_addr_value $_ioapic_index_id
       printf "IOAPIC[0x%02X] ID:        0x%08X\n", $_ioapic_index_id, $kgm_ioapic_value

       # version
       _ioapic_addr_value $_ioapic_index_ver
       set $maxredir = (($kgm_ioapic_value & 0xFF0000) >> 16) + 1

       printf "IOAPIC[0x%02X] VERSION:   0x%08X         [", $_ioapic_index_ver, $kgm_ioapic_value
       printf "MAXREDIR=%02d PRQ=%d VERSION=0x%02X]\n", $maxredir, ($kgm_ioapic_value >> 15) & 0x1, $kgm_ioapic_value & 0xFF
       
       # all the redir entries
       set $i = 0
       while $i < $maxredir
       	     set $addr0 = $_ioapic_index_redir_base + ($i << 1)
       	     set $addr1 = $addr0 + 1
             _ioapic_addr_value $addr1
	     printf "IOAPIC[0x%02X] IOREDIR%02d: 0x%08X", $addr0, $i, $kgm_ioapic_value

	     _ioapic_addr_value $addr0
	     printf "%08X ", $kgm_ioapic_value
	     _apic_print $kgm_ioapic_value
	     set $i = $i + 1
       end
   end
end

document ioapic_dump
Syntax: (gdb) ioapic_dump
| Dump all the IOAPIC entries.
end


set $_lapic_base_addr         = 0xFEE00000
set $_lapic_id                = 0x20
set $_lapic_version           = 0x30
set $_lapic_tpr               = 0x80
set $_lapic_apr               = 0x90
set $_lapic_ppr               = 0xA0
set $_lapic_eoi               = 0xB0
set $_lapic_ldr               = 0xD0
set $_lapic_dfr               = 0xE0
set $_lapic_sivr              = 0xF0

set $_lapic_isr_size          = 0x10
set $_lapic_isr_num           = 8
set $_lapic_isr0              = 0x100
set $_lapic_tmr0              = 0x180
set $_lapic_irr0              = 0x200

set $_lapic_esr               = 0x280
set $_lapic_esr_register      = 0x80
set $_lapic_esr_recv_vect     = 0x40
set $_lapic_esr_send_vect     = 0x20

set $_lapic_icr0              = 0x300
set $_lapic_icr1              = 0x310

set $_lapic_lvt_timer         = 0x320
set $_lapic_lvt_thermal       = 0x330
set $_lapic_lvt_pmcr          = 0x340
set $_lapic_lvt_lint0         = 0x350
set $_lapic_lvt_lint1         = 0x360
set $_lapic_lvt_error         = 0x370

set $_lapic_icr               = 0x380
set $_lapic_ccr               = 0x390
set $_lapic_dcr               = 0x3E0

set $_apic_cfg_msr          = 0x1B
set $_apic_cfg_msr_x2EN     = 0x00000C00
set $_x2apic_enabled         = -1

# _lapic_addr $offset returns the actual address to use
define _lapic_addr
       if $_x2apic_enabled < 0
       	  readmsr64int $_apic_cfg_msr $kgm_lcpu_self
	  if ($kgm_readmsr64int_result & $_apic_cfg_msr_x2EN) == $_apic_cfg_msr_x2EN
	     set $_x2apic_enabled = 1
	  else
	     set $_x2apic_enabled = 0
	  end
       end

       if $_x2apic_enabled 
       	  # x2APIC addresses are MSRs that use xAPIC offsets that
	  # are 4-bit shifted
       	  set $kgm_lapic_addr = $arg0 >> 4
       else
       	  set $kgm_lapic_addr = $_lapic_base_addr + $arg0
       end
end

# _lapic_addr_value $offset $lcpu
define _lapic_addr_value
       _lapic_addr $arg0
       if $_x2apic_enabled
       	  readmsr64int $kgm_lapic_addr $arg1
	  set $kgm_lapic_value = $kgm_readmsr64int_result       	  
       else
       	  readphysint $kgm_lapic_addr 32 $arg1
	  set $kgm_lapic_value = $kgm_readphysint_result
       end
end

# lapic_read32 $offset [$lcpu]
define lapic_read32
   if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
       printf "lapic_read32 not supported on this architecture.\n"
   else
       set $lcpu = $kgm_lcpu_self
       if $argc > 1
       	  set $lcpu = $arg1
       end
       _lapic_addr_value $arg0 $lcpu
       printf "LAPIC[0x%03X]: 0x%08X\n", $arg0, $kgm_lapic_value
   end
end

document lapic_read32
Syntax: (gdb) apic_read32_cpu <offset> [lcpu (kernel's numbering convention)]
| Read the LAPIC register at the offset specified. The CPU can be optionally
| specified.
end

# lapic_write32 $offset $value [$lcpu]
define lapic_write32
   if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
       printf "lapic_write32_cpu not supported on this architecture.\n"
   else
       set $lcpu = $kgm_lcpu_self
       if $argc > 2
       	  set $lcpu = $arg2
       end

       _lapic_addr $arg0
       if $_x2apic_enabled
       	  writemsr64int $kgm_lapic_addr $arg1 $lcpu
       else
       	  writephysint $kgm_lapic_addr 32 $arg1 $lcpu
       end
   end
end

document lapic_write32
Syntax: (gdb) lapic_write32 <offset> <value> [lcpu (kernel's numbering convention)]
| Write the LAPIC register at the offset specified. The CPU can be optionally
| specified.
end

# lapic_dump [lcpu]
define lapic_dump
   if (($kgm_mtype & $kgm_mtype_x86_mask) != $kgm_mtype_x86_any)
       printf "lapic_dump not supported on this architecture.\n"
   else
       set $lcpu = $kgm_lcpu_self
       if $argc > 0
       	  set $lcpu = $arg0
       end

       _lapic_addr_value $_lapic_id $lcpu

       # the above also figures out if we're using an xAPIC or an x2APIC
       printf "LAPIC operating mode: "
       if $_x2apic_enabled
       	  printf "           x2APIC\n"
       else
       	  printf "           xAPIC\n"
       end

       printf "LAPIC[0x%03X] ID:                 0x%08X\n", $_lapic_id, $kgm_lapic_value

       _lapic_addr_value $_lapic_version $lcpu
       set $lvt_num = ($kgm_lapic_value >> 16) + 1
       printf "LAPIC[0x%03X] VERSION:            0x%08X [VERSION=%d MaxLVT=%d]\n", $_lapic_version, $kgm_lapic_value, $kgm_lapic_value & 0xFF, $lvt_num
       
       _lapic_addr_value $_lapic_tpr $lcpu
       printf "LAPIC[0x%03X] TASK PRIORITY:      0x%08X\n", $_lapic_tpr, $kgm_lapic_value

       _lapic_addr_value $_lapic_ppr $lcpu
       printf "LAPIC[0x%03X] PROCESSOR PRIORITY: 0x%08X\n", $_lapic_ppr, $kgm_lapic_value

       _lapic_addr_value $_lapic_ldr $lcpu
       printf "LAPIC[0x%03X] LOGICAL DEST:       0x%08X\n", $_lapic_ldr, $kgm_lapic_value

       _lapic_addr_value $_lapic_dfr $lcpu
       printf "LAPIC[0x%03X] DEST FORMAT:        0x%08X\n", $_lapic_dfr, $kgm_lapic_value

       _lapic_addr_value $_lapic_sivr $lcpu
       printf "LAPIC[0x%03X] SPURIOUS VECTOR:    0x%08X [VEC=%3d ENABLED=%d]\n", $_lapic_sivr, $kgm_lapic_value, $kgm_lapic_value & $_apic_vector_mask, ($kgm_lapic_value & 0x100) >> 8, 

       set $i = 0
       while $i < $_lapic_isr_num
       	     set $addr = $_lapic_isr0 + $i * $_lapic_isr_size
             _lapic_addr_value $addr $lcpu
             printf "LAPIC[0x%03X] ISR[%03d:%03d]:       0x%08X\n", $addr, 32*($i + 1) - 1,  32*$i, $kgm_lapic_value
	     set $i = $i + 1
       end

       set $i = 0
       while $i < $_lapic_isr_num
       	     set $addr = $_lapic_tmr0 + $i * $_lapic_isr_size
       	     _lapic_addr_value $addr $lcpu
             printf "LAPIC[0x%03X] TMR[%03d:%03d]:       0x%08X\n", $addr, 32*($i + 1) - 1,  32*$i, $kgm_lapic_value
	     set $i = $i + 1
       end

       set $i = 0
       while $i < $_lapic_isr_num
       	     set $addr = $_lapic_irr0 + $i * $_lapic_isr_size
       	     _lapic_addr_value $addr $lcpu
             printf "LAPIC[0x%03X] IRR[%03d:%03d]:       0x%08X\n", $addr, 32*($i + 1) - 1,  32*$i, $kgm_lapic_value
	     set $i = $i + 1
       end

       _lapic_addr_value $_lapic_esr $lcpu
       printf "LAPIC[0x%03X] ERROR STATUS:       0x%08X ", $_lapic_esr, $kgm_lapic_value
       if $kgm_lapic_value
       	  printf "["
       end
       if $kgm_lapic_value & $_lapic_esr_register
       	  printf "Register  "
       end
       if $kgm_lapic_value & $_lapic_esr_recv_vect
       	  printf "Received Vector  "
       end
       if $kgm_lapic_value & $_lapic_esr_send_vect
       	  printf "Send Vector"
       end
       if $kgm_lapic_value
       	  printf "]"
       end
       printf "\n"

       _lapic_addr_value $_lapic_icr1 $lcpu
       printf "LAPIC[0x%03X] Interrupt Command:  0x%08X [DEST=%d]\n", $_lapic_icr0, $kgm_lapic_value, $kgm_lapic_value >> 24
       _lapic_addr_value $_lapic_icr0 $lcpu
       printf "                                 0x%08X ", $kgm_lapic_value
       _apic_print $kgm_lapic_value

       if $lvt_num > 0
       	  _lapic_addr_value $_lapic_lvt_timer $lcpu
	  printf "LAPIC[0x%03X] LVT Timer:          0x%08X ", $_lapic_lvt_timer, $kgm_lapic_value
	  _apic_print $kgm_lapic_value
       end

       if $lvt_num > 1
       	  _lapic_addr_value $_lapic_lvt_lint0 $lcpu
	  printf "LAPIC[0x%03X] LVT LINT0:          0x%08X ", $_lapic_lvt_lint0, $kgm_lapic_value
	  _apic_print $kgm_lapic_value
       end

       if $lvt_num > 2
       	  _lapic_addr_value $_lapic_lvt_lint1 $lcpu
	  printf "LAPIC[0x%03X] LVT LINT1:          0x%08X ", $_lapic_lvt_lint1, $kgm_lapic_value
	  _apic_print $kgm_lapic_value
       end

       if $lvt_num > 3
       	  _lapic_addr_value $_lapic_lvt_error $lcpu
	  printf "LAPIC[0x%03X] LVT Error:          0x%08X ", $_lapic_lvt_error, $kgm_lapic_value
	  _apic_print $kgm_lapic_value
       end

       if $lvt_num > 4
       	  _lapic_addr_value $_lapic_lvt_pmcr $lcpu
	  printf "LAPIC[0x%03X] LVT PerfMon:        0x%08X ", $_lapic_lvt_pmcr, $kgm_lapic_value
	  _apic_print $kgm_lapic_value
       end

       if $lvt_num > 5
       	  _lapic_addr_value $_lapic_lvt_thermal $lcpu
	  printf "LAPIC[0x%03X] LVT Thermal:        0x%08X ", $_lapic_lvt_thermal, $kgm_lapic_value
	  _apic_print $kgm_lapic_value
       end

       _lapic_addr_value $_lapic_dcr $lcpu
       printf "LAPIC[0x%03X] Timer Divide:       0x%08X [Divide by ", $_lapic_dcr, $kgm_lapic_value
       set $kgm_lapic_value = ($kgm_lapic_value & 0x8) >> 1 | $kgm_lapic_value & 0x3
       if $kgm_lapic_value == 0x7
       	  printf "1]\n"
       else
       	  printf "%d]\n", 2 << $kgm_lapic_value
       end

       _lapic_addr_value $_lapic_icr $lcpu
       printf "LAPIC[0x%03X] Timer Init Count:   0x%08X\n", $_lapic_icr, $kgm_lapic_value

       _lapic_addr_value $_lapic_ccr $lcpu
       printf "LAPIC[0x%03X] Timer Cur Count:    0x%08X\n", $_lapic_ccr, $kgm_lapic_value
   end
end

document lapic_dump
Syntax: (gdb) lapic_dump [lcpu (kernel's numbering convention)]
| Dump all the LAPIC entries. The CPU can be optionally specified. 
end
 
define showknoteheader
   printf "            knote       filter           ident  kn_ptr      status\n"
end

define showknoteint
    set $kgm_knotep = ((struct knote *) $arg0)
    printf "            "
    showptr $kgm_knotep
    printf "  "
    set $kgm_filt = -$kgm_knotep->kn_kevent.filter
    if ($kgm_filt == 1)
    	printf "EVFILT_READ    "
    end
    if ($kgm_filt == 2)
        printf "EVFILT_WRITE   "
    end
    if ($kgm_filt == 3)
        printf "EVFILT_AIO     "
    end
    if ($kgm_filt == 4)
        printf "EVFILT_VNODE   "
    end
    if ($kgm_filt == 5)
        printf "EVFILT_PROC    "
    end
    if ($kgm_filt == 6)
        printf "EVFILT_SIGNAL  "
    end
    if ($kgm_filt == 7)
        printf "EVFILT_TIMER   "
    end
    if ($kgm_filt == 8)
        printf "EVFILT_MACHPORT"
    end
    if ($kgm_filt == 9)
        printf "EVFILT_FS      "
    end
    if ($kgm_filt == 10)
        printf "EVFILT_USER    "
    end
    if ($kgm_filt == 11)
        printf "EVFILT_SESSION "
    end
    printf "%7d  ", $kgm_knotep->kn_kevent.ident
    showptr $kgm_knotep->kn_ptr.p_fp
    printf "  "
    if ($kgm_knotep->kn_status == 0)
        printf "-"
    else
        if ($kgm_knotep->kn_status & 0x01)
	    printf "A"
	end
        if ($kgm_knotep->kn_status & 0x02)
	    printf "Q"
	end
        if ($kgm_knotep->kn_status & 0x04)
	    printf "Dis"
	end
        if ($kgm_knotep->kn_status & 0x08)
	    printf "Dr"
	end
        if ($kgm_knotep->kn_status & 0x10)
	    printf "Uw"
	end
        if ($kgm_knotep->kn_status & 0x20)
	    printf "Att"
	end
        if ($kgm_knotep->kn_status & 0x40)
	    printf "Stq"
	end
    end
    printf "\n"
end

define showprocknotes
    showknoteheader
    set $kgm_fdp = ((proc_t)$arg0)->p_fd
    set $kgm_knlist = $kgm_fdp->fd_knlist
    set $i = 0
    while (($i < $kgm_fdp->fd_knlistsize) && ($kgm_knlist != 0))
        set $kgm_kn = ((struct knote *)$kgm_knlist[$i].slh_first)
	while ($kgm_kn != 0)
	    showknoteint $kgm_kn
	    set $kgm_kn = ((struct knote *)$kgm_kn->kn_link.sle_next)
	end
        set $i = $i + 1
    end
    set $kgm_knhash = $kgm_fdp->fd_knhash
    set $i = 0
    while (($i < $kgm_fdp->fd_knhashmask + 1) && ($kgm_knhash != 0))
        set $kgm_kn = ((struct knote *)$kgm_knhash[$i].slh_first)
	while ($kgm_kn != 0)
	    showknoteint $kgm_kn
	    set $kgm_kn = ((struct knote *)$kgm_kn->kn_link.sle_next)
	end
        set $i = $i + 1
    end
end

define showallknotes
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_taskp != $kgm_head_taskp
        showtaskheader
	showtaskint $kgm_taskp
	showprocknotes $kgm_taskp->bsd_info
    	set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
    end
end
document showprocknotes 
Syntax: showprocknotes <proc>
| Displays filter and status information for every kevent registered for
| the process.
end

#
# Device node related debug macros
#

define _showtty
	set $kgm_tty = (struct tty *) $arg0
	printf "tty struct at "
	showptr $kgm_tty
	printf "\n"
	printf "-last input to raw queue:\n"
	p $kgm_tty->t_rawq->c_cs
	printf "-last input to canonical queue:\n"
	p $kgm_tty->t_canq->c_cs
	printf "-last output data:\n"
	p $kgm_tty->t_outq->c_cs
	printf "state:\n"
	if ($kgm_tty->t_state & 0x00000001)
		printf "    TS_SO_OLOWAT (Wake up when output <= low water)\n"
	end
	if ($kgm_tty->t_state & 0x00000002)
		printf "    TS_ASYNC (async I/O mode)\n"
	else
		printf "    - (synchronous I/O mode)\n"
	end
	if ($kgm_tty->t_state & 0x00000004)
		printf "    TS_BUSY (Draining output)\n"
	end
	if ($kgm_tty->t_state & 0x00000008)
		printf "    TS_CARR_ON (Carrier is present)\n"
	else
		printf "    - (Carrier is NOT present)\n"
	end
	if ($kgm_tty->t_state & 0x00000010)
		printf "    TS_FLUSH (Outq has been flushed during DMA)\n"
	end
	if ($kgm_tty->t_state & 0x00000020)
		printf "    TS_ISOPEN (Open has completed)\n"
	else
		printf "    - (Open has NOT completed)\n"
	end
	if ($kgm_tty->t_state & 0x00000040)
		printf "    TS_TBLOCK (Further input blocked)\n"
	end
	if ($kgm_tty->t_state & 0x00000080)
		printf "    TS_TIMEOUT (Wait for output char processing)\n"
	end
	if ($kgm_tty->t_state & 0x00000100)
		printf "    TS_TTSTOP (Output paused)\n"
	end
	if ($kgm_tty->t_state & 0x00000200)
		printf "    TS_WOPEN (Open in progress)\n"
	end
	if ($kgm_tty->t_state & 0x00000400)
		printf "    TS_XCLUDE (Tty requires exclusivity)\n"
	end
	if ($kgm_tty->t_state & 0x00000800)
		printf "    TS_BKSL (State for lowercase \\ work)\n"
	end
	if ($kgm_tty->t_state & 0x00001000)
		printf "    TS_CNTTB (Counting tab width, ignore FLUSHO)\n"
	end
	if ($kgm_tty->t_state & 0x00002000)
		printf "    TS_ERASE (Within a \\.../ for PRTRUB)\n"
	end
	if ($kgm_tty->t_state & 0x00004000)
		printf "    TS_LNCH (Next character is literal)\n"
	end
	if ($kgm_tty->t_state & 0x00008000)
		printf "    TS_TYPEN (Retyping suspended input (PENDIN))\n"
	end
	if ($kgm_tty->t_state & 0x00010000)
		printf "    TS_CAN_BYPASS_L_RINT (Device in "raw" mode)\n"
	end
	if ($kgm_tty->t_state & 0x00020000)
		printf "    TS_CONNECTED (Connection open)\n"
	else
		printf "    - (Connection NOT open)\n"
	end
	if ($kgm_tty->t_state & 0x00040000)
		printf "    TS_SNOOP (Device is being snooped on)\n"
	end
	if ($kgm_tty->t_state & 0x80000)
		printf "    TS_SO_OCOMPLETE (Wake up when output completes)\n"
	end
	if ($kgm_tty->t_state & 0x00100000)
		printf "    TS_ZOMBIE (Connection lost)\n"
	end
	if ($kgm_tty->t_state & 0x00200000)
		printf "    TS_CAR_OFLOW (For MDMBUF - handle in driver)\n"
	end
	if ($kgm_tty->t_state & 0x00400000)
		printf "    TS_CTS_OFLOW (For CCTS_OFLOW - handle in driver)\n"
	end
	if ($kgm_tty->t_state & 0x00800000)
		printf "    TS_DSR_OFLOW (For CDSR_OFLOW - handle in driver)\n"
	end
	# xxx todo: do we care about decoding flags?
	printf "flags:                    0x%08x\n", $kgm_tty->t_flags
	printf "foreground process group: "
	showptr $kgm_tty->t_pgrp
	printf "\n"
	printf "enclosing session:        "
	showptr $kgm_tty->t_session
	printf "\n"
	printf "Termios:\n"
	# XXX todo: decode these flags, someday
	printf "    Input flags:   0x%08x\n", $kgm_tty->t_termios.c_iflag
	printf "    Output flags:  0x%08x\n", $kgm_tty->t_termios.c_oflag
	printf "    Control flags: 0x%08x\n", $kgm_tty->t_termios.c_cflag
	printf "    Local flags:   0x%08x\n", $kgm_tty->t_termios.c_lflag
	printf "    Input speed:   %d\n",  $kgm_tty->t_termios.c_ispeed
	printf "    Output speed:  %d\n",  $kgm_tty->t_termios.c_ospeed
	# XXX todo: useful to decode t_winsize? t_iokit? c_cc? anything else?
	printf "high watermark: %d bytes\n", $kgm_tty->t_hiwat
	printf "low watermark: %d bytes\n", $kgm_tty->t_lowat
end

define _showwhohas
	# _showwhohas <major> <minor>
	printf "fd     "
	printf "fileglob    "
showptrhdrpad
	printf "vnode       "
showptrhdrpad
	printf "process     "
showptrhdrpad
	printf "name\n"

	set $kgm_swh_devnode_dev = (((int) $arg0) << 24) | (int) $arg1
	# iterate all tasks to iterate all processes to iterate all
	# open files in each process to see who has a given major/minor
	# device open
	set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
	while $kgm_taskp != $kgm_head_taskp
		set $kgm_procp = (proc_t) $kgm_taskp->bsd_info
		set $kgm_spf_filedesc = $kgm_procp->p_fd
		set $kgm_spf_last = $kgm_spf_filedesc->fd_lastfile
		set $kgm_spf_ofiles = $kgm_spf_filedesc->fd_ofiles
		set $kgm_spf_count = 0
		while (($kgm_spf_ofiles != 0) && ($kgm_spf_count <= $kgm_spf_last))
		    # only files currently open
		    if ($kgm_spf_ofiles[$kgm_spf_count] != 0)
			set $kgm_spf_fg = $kgm_spf_ofiles[$kgm_spf_count].f_fglob
			if ($kgm_spf_fg->fg_type == 1)
			    # display fd #, fileglob & vnode address, proc name
			    set $kgm_swh_m_vnode = (vnode_t) $kgm_spf_fg->fg_data
			    set $kgm_swh_m_vtype = (enum vtype) $kgm_swh_m_vnode->v_type
			    if (($kgm_swh_m_vtype == VBLK) || ($kgm_swh_m_vtype == VCHR)) && ((((devnode_t *)$kgm_swh_m_vnode->v_data)->dn_typeinfo.dev) == $kgm_swh_devnode_dev)
			    	printf "%-5d  ", $kgm_spf_count
				showptr  $kgm_spf_fg
				printf "  "
				showptr $kgm_swh_m_vnode
				printf "  "
				showptr $kgm_procp
				printf "  %s\n", $kgm_procp->p_comm
			    end
			end
		    end
		    set $kgm_spf_count = $kgm_spf_count + 1
		end

		set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
	end
end

define _showvnodedev_cpty
	set $kgm_ptmx_major = (int) $arg0
	set $kgm_ptmx_minor = (int) $arg1
	set $kgm_ptmx_ioctl = _state.pis_ioctl_list[$kgm_ptmx_minor]
	set $kgm_ptmx_ioctl = _state.pis_ioctl_list[$kgm_ptmx_minor]
	printf "    ptmx_ioctl struct at "
	showptr $kgm_ptmx_ioctl
	printf "\n"
	printf "    flags:\n"
	if ($kgm_ptmx_ioctl->pt_flags & 0x0008)
		printf "        PF_PKT (packet mode)\n"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0010)
		printf "        PF_STOPPED (user told stopped)\n"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0020)
		printf "        PF_REMOTE (remote and flow controlled input)\n"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0040)
		printf "        PF_NOSTOP"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0080)
		printf "        PF_UCNTL (user control mode)\n"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0100)
		printf "        PF_UNLOCKED (slave unlock - master open resets)\n"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0200)
		printf "        PF_OPEN_M (master is open)\n"
		# XXX we should search for who has the master open, but
		# XXX each master gets the same minor, even though it
		# XXX gets a different vnode.  we chold probably change
		# XXX this, but to do it we would need some way of
		# XXX expressing the information in the vnode structure
		# XXX somewhere.  If we *did* change it, it would buy us
		# XXX the ability to determine who has the corresponding
		# XXX master end of the pty open
	else
		printf "        PF_OPEN_M (master is closed)\n"
	end
	if ($kgm_ptmx_ioctl->pt_flags & 0x0400)
		printf "        PF_OPEN_S (slave is open)\n"
		printf "---vvvvv--- fds open on this device ---vvvvv---\n"
		_showwhohas  ($kgm_ptmx_major) ($kgm_ptmx_minor)
		printf "---^^^^^--- fds open on this device ---^^^^^---\n"
	else
		printf "        - (slave is closed)\n"
	end
	printf "TTY Specific Information\n"
	_showtty $kgm_ptmx_ioctl->pt_tty
end

define showvnodedev
    if ($argc == 1)
	set $kgm_vnode = (vnode_t) $arg0
	set $kgm_vtype = (enum vtype) $kgm_vnode->v_type
	if (($kgm_vtype == VBLK) || ($kgm_vtype == VCHR))
		set $kgm_devnode = (devnode_t *) $kgm_vnode->v_data
		set $kgm_devnode_dev = $kgm_devnode->dn_typeinfo.dev
		set $kgm_devnode_major = ($kgm_devnode_dev >> 24) & 0xff
		set $kgm_devnode_minor = $kgm_devnode_dev & 0x00ffffff

		# boilerplate device information for a vnode 
		printf "Device Info:\n"
		printf "    vnode:        "
		showptr $kgm_vnode
		printf "\n"
		printf "    type:         "
		if ($kgm_vtype == VBLK)
			printf "VBLK "
		end
		if ($kgm_vtype == VCHR)
			printf "VCHR"
		end
		printf "\n"
		printf "    name:         %s\n", $kgm_vnode->v_name
		printf "    major, minor: %d, %d\n", $kgm_devnode_major, $kgm_devnode_minor
		printf "    mode          0%o\n", $kgm_devnode->dn_mode
		printf "    owner (u,g):  %d %d", $kgm_devnode->dn_uid, $kgm_devnode->dn_gid
		printf "\n"

		# decode device specific data
		printf "Device Specific Information:  "
		if ($kgm_vtype == VBLK)
			printf "    Sorry, I do not know how to decode block devices yet!\n"
			printf "    Maybe you can write me!"
		end
		if ($kgm_vtype == VCHR)
			# Device information; this is scanty
			# range check
			if ($kgm_devnode_major > 42) || ($kgm_devnode_major < 0)
				printf "Invalid major #\n"
			else
			# static assignments in conf
			if ($kgm_devnode_major == 0)
				printf "Console mux device\n"
			else
			if ($kgm_devnode_major == 2)
				printf "Current tty alias\n"
			else
			if ($kgm_devnode_major == 3)
				printf "NULL device\n"
			else
			if ($kgm_devnode_major == 4)
				printf "Old pty slave\n"
			else
			if ($kgm_devnode_major == 5)
				printf "Old pty master\n"
			else
			if ($kgm_devnode_major == 6)
				printf "Kernel log\n"
			else
			if ($kgm_devnode_major == 12)
				printf "Memory devices\n"
			else
			# Statically linked dynamic assignments
			if cdevsw[$kgm_devnode_major].d_open == ptmx_open
				printf "Cloning pty master\n"
				_showvnodedev_cpty ($kgm_devnode_major) ($kgm_devnode_minor)
			else
			if cdevsw[$kgm_devnode_major].d_open == ptsd_open
				printf "Cloning pty slave\n"
				_showvnodedev_cpty ($kgm_devnode_major) ($kgm_devnode_minor)
			else
				printf "RESERVED SLOT\n"
			end
			end
			end
			end
			end
			end
			end
			end
			end
			end
		end
	else
		showptr $kgm_vnode
		printf " is not a device\n"
	end
    else
    	printf "| Usage:\n|\n"
	help showvnodedev
    end
end
document showvnodedev
Syntax: (gdb) showvnodedev <vnode>
|     showvnodedev       Display information about a device vnode
end

define showtty
    if ($argc == 1)
	_showtty $arg0
    else
    	printf "| Usage:\n|\n"
	help showtty
    end
end
document showtty
Syntax: (gdb) showtty <tty struct>
|     showtty            Display information about a struct tty
end

define showeventsourceobject
    set $kgm_vt = *((void **) $arg1)
    if $kgm_lp64
        set $kgm_vt = $kgm_vt - 16
    end
    pcprint $kgm_vt
end
document showeventsourceobject
Syntax: (gdb) showeventsourceobject <prefix> <object>
| Routine to display information about an IOEventSource subclass.
end

define showworkloopallocator
	set $kgm_workloop = (struct IOWorkLoop*)$arg0
	set $kgm_bt = (void**)$kgm_workloop->reserved->allocationBacktrace
	set $kgm_bt_count = 0
	while $kgm_bt_count != (sizeof(IOWorkLoop::ExpansionData.allocationBacktrace) / sizeof(IOWorkLoop::ExpansionData.allocationBacktrace[0]))
		set $kgm_frame_address = (void*)$kgm_bt[$kgm_bt_count]
		if $kgm_frame_address != 0
			if (((unsigned long) $kgm_frame_address < (unsigned long) &_mh_execute_header || \
			     (unsigned long) $kgm_frame_address >= (unsigned long) &last_kernel_symbol ) \
			    && ($kgm_show_kmod_syms == 0))
				showkmodaddr $kgm_frame_address
			else
				output /a $kgm_frame_address
			end
			printf "\n"
		end
		set $kgm_bt_count = $kgm_bt_count + 1
	end
end
document showworkloopallocator
Syntax: (gdb) showworkloopallocator <workloop>
| Routine to display the backtrace of the thread which allocated the workloop in question. Only
| valid on DEBUG kernels.
end

define showworkloopeventsources
	set $kgm_eventsource = (struct IOEventSource*)$arg0
    while $kgm_eventsource != 0
    	printf "     "
    	printf "EventSource:\t"
    	showptr $kgm_eventsource
    	printf " Description: "
    	showeventsourceobject _ $kgm_eventsource
    	printf "\n"
    	if $kgm_eventsource->action != 0
	    	printf "          "
			printf "Action: \t"
    		pcprint $kgm_eventsource->action
    		printf "\n"
    	end
    	if $kgm_eventsource->owner != 0
	    	printf "          "
			printf "Owner: \t"
			showptr	$kgm_eventsource->owner
			printf " Description: "
			showeventsourceobject _ $kgm_eventsource->owner
    		printf "\n"
    	end
    	set $kgm_eventsource = $kgm_eventsource->eventChainNext
    	printf "\n"
    end
end
document showworkloopeventsources
Syntax: (gdb) showworkloopeventsources
| Routine to walk an IOEventSource chain associated with an IOWorkLoop and print information
| about each event source in the chain.
end

define showworkloopheader
    printf "thread    "
    showptrhdrpad
    printf "  workloop "
    showptrhdrpad
    printf "    pri state\tLockGroupName\n"
end
document showworkloopheader
Syntax: (gdb) showworkloopheader
| Routine to print out header info about an IOKit workloop.
end

define showworkloop
	set $kgm_workloopthread = (struct thread*)$arg0
	set $kgm_workloop = (struct IOWorkLoop*)$arg1
    showptr $kgm_workloopthread
	printf "  "
	showptr $kgm_workloop
	printf "   %3d ", $kgm_workloopthread.sched_pri
	set $kgm_state = $kgm_workloopthread.state
	if $kgm_state & 0x80
	    printf "I" 
	end
	if $kgm_state & 0x40
	    printf "P" 
	end
	if $kgm_state & 0x20
	    printf "A" 
	end
	if $kgm_state & 0x10
	    printf "H" 
	end
	if $kgm_state & 0x08
	    printf "U" 
	end
	if $kgm_state & 0x04
	    printf "R" 
	end
	if $kgm_state & 0x02
	    printf "S" 
	end
   	if $kgm_state & 0x01
	    printf "W"
	end
	printf "\t\t"
	set $kgm_gateLock = ( struct _IORecursiveLock *)$kgm_workloop->gateLock
	if $kgm_gateLock != 0
		set $kgm_lockGroup = (struct _lck_grp_*)($kgm_gateLock->group)
		printf "%s", $kgm_lockGroup->lck_grp_name
	else
		printf "No WorkLoop Lock found"
	end
	printf "\n\n"
	
	#Allocation backtrace is only valid on DEBUG kernels.
	#printf "Allocation path:\n\n"
	#showworkloopallocator $kgm_workloop
	#printf "\n\n"
	
	if $kgm_workloop->eventChain != 0
		printf "Active event sources:\n\n"
		showworkloopeventsources $kgm_workloop->eventChain
	end
	if $kgm_workloop->reserved->passiveEventChain != 0
		printf "Passive event sources:\n"
		showworkloopeventsources $kgm_workloop->reserved->passiveEventChain
	end
end
document showworkloop
Syntax: (gdb) showworkloop <thread> <workloop>
| Routine to print out info about an IOKit workloop.
end

define showallworkloopthreads
    set $kgm_head_taskp = &tasks
    set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
	set $kgm_head_actp = &($kgm_taskp->threads)
	set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
	while $kgm_actp != $kgm_head_actp
		if ($kgm_actp->continuation == _ZN10IOWorkLoop10threadMainEv)
			showworkloopheader
			showworkloop $kgm_actp $kgm_actp->parameter
		else
			if ($kgm_actp->kernel_stack != 0)
				if ($kgm_mtype == $kgm_mtype_x86_64)
					#Warning: Grokking stack looking for hopeful workloops until we squirrel some info in thread_t.
					set $kgm_workloop = *((struct IOWorkLoop **)($kgm_actp->kernel_stack + kernel_stack_size - 0xB8))
				else
					if ($kgm_mtype == $kgm_mtype_i386)
						set $kgm_workloop = *((struct IOWorkLoop **)($kgm_actp->kernel_stack + kernel_stack_size - 0x3C))
					end
				end
				if ($kgm_workloop != 0)
					set $kgm_vt = *((void **) $kgm_workloop)
					if $kgm_lp64
						set $kgm_vt = $kgm_vt - 16
					end
					if ($kgm_vt == &_ZTV10IOWorkLoop)
						showworkloopheader
						showworkloop $kgm_actp $kgm_workloop
					end
				end
			end
		end
		set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
	end
	printf "\n"
end
document showallworkloopthreads
Syntax: (gdb) showallworkloopthreads
| Routine to print out info about all IOKit workloop threads in the system. This macro will find
| all IOWorkLoop threads blocked in continuations and on i386 and x86_64 systems will make a
| best-effort guess to find any workloops that are actually not blocked in a continuation. For a
| complete list, it is best to compare the output of this macro against the output of 'showallstacks'.
end

define showthreadfortid
	set $kgm_id_found = 0
	
	set $kgm_head_taskp = &tasks
	set $kgm_taskp = (struct task *)($kgm_head_taskp->next)
	while $kgm_taskp != $kgm_head_taskp
		set $kgm_head_actp = &($kgm_taskp->threads)
		set $kgm_actp = (struct thread *)($kgm_taskp->threads.next)
		while $kgm_actp != $kgm_head_actp
			set $kgm_thread = *(struct thread *)$kgm_actp
			set $kgm_thread_id = $kgm_thread.thread_id
			if ($kgm_thread_id == $arg0)
				showptr $kgm_actp
				printf "\n"
				set $kgm_id_found = 1
				loop_break
			end
			set $kgm_actp = (struct thread *)($kgm_actp->task_threads.next)
		end
		if ($kgm_id_found == 1)
			loop_break
		end
		set $kgm_taskp = (struct task *)($kgm_taskp->tasks.next)
	end
	if ($kgm_id_found == 0)
		printf  "Not a valid thread_id\n"
	end
end

document showthreadfortid
Syntax:  showthreadfortid  <thread_id>
|The thread structure contains a unique thread_id value for each thread.
|This command is used to retrieve the address of the thread structure(thread_t) 
|corresponding to a given thread_id.
end

define showtaskbusyportsint
    set $kgm_isp = ((task_t)$arg0)->itk_space
	set $kgm_iindex = 0
	while ( $kgm_iindex < $kgm_isp->is_table_size )
		set $kgm_iep = &($kgm_isp->is_table[$kgm_iindex])
		if $kgm_iep->ie_bits & 0x00020000
			set $kgm_port = ((ipc_port_t)$kgm_iep->ie_object)
			if $kgm_port->ip_messages.data.port.msgcount > 0
				showport $kgm_port
			end
		end
		set $kgm_iindex = $kgm_iindex + 1
	end
end

define showtaskbusyports
    showtaskbusyportsint $arg0
end

document showtaskbusyports
Syntax:  showtaskbusyports <task>
|Routine to print information about receive rights belonging to this task that
|have enqueued messages. This is often a sign of a blocked or hung process.
end

define showallbusyports
    set $kgm_head_taskp = &tasks
    set $kgm_cur_taskp = (struct task *)($kgm_head_taskp->next)
    while $kgm_cur_taskp != $kgm_head_taskp
		showtaskbusyportsint $kgm_cur_taskp
    	set $kgm_cur_taskp = (struct task *)($kgm_cur_taskp->tasks.next)
    end
end

document showallbusyports
Syntax:  showallbusyports
|Routine to print information about all receive rights on the system that
|have enqueued messages.
end

define showallproviders
    set $kgm_providerp = dtrace_provider
    while $kgm_providerp
        p *(dtrace_provider_t *)$kgm_providerp
        printf "\n"
        set $kgm_providerp = (dtrace_provider_t *)($kgm_providerp->dtpv_next)
    end
end

document showallproviders
Syntax: showallproviders
| Display summary listing of all dtrace_providers
end

define showmodctlheader
    printf   "modctl    "
    showptrhdrpad
    printf "  stale     "
    showptrhdrpad
    printf "  symbols   "
    showptrhdrpad
    printf "  address   "
    showptrhdrpad
    printf "  size      "
    showptrhdrpad
    printf "  loadid    loaded  nenabled  flags      name\n"
end

define showmodctlint
    set $kgm_modctlp = (struct modctl *)$arg0
    showptr $kgm_modctlp
    printf "  "
    showptr $kgm_modctlp->mod_stale
    printf "  "
    showptr $kgm_modctlp->mod_user_symbols
    printf "  "
    showptr $kgm_modctlp->mod_address
    printf "  "
    showptr $kgm_modctlp->mod_size
    printf "  "
    printf "%6d  ", $kgm_modctlp->mod_loadcnt
    printf "%6d  ", $kgm_modctlp->mod_loaded
    printf "%6d  ", $kgm_modctlp->mod_nenabled
    printf "    0x%x  ", $kgm_modctlp->mod_flags
    printf "%s\n", $kgm_modctlp->mod_modname
end

define showmodctl
    showmodctlheader
    showmodctlint $arg0
end
document showmodctl
Syntax: (gdb) showmodctl <addr>
| Display info about a dtrace modctl
end

define showallmodctls
    showmodctlheader
    set $kgm_modctlp = (struct modctl *)dtrace_modctl_list
    while $kgm_modctlp
        showmodctlint $kgm_modctlp
        set $kgm_modctlp = $kgm_modctlp->mod_next
    end
end
document showallmodctls
Syntax: (gdb) showallmodctls
| Display summary listing of all dtrace modctls
end

define showfbtprobe
  printf "Be very patient, this traverses a large list \n"
  set $kgm_indx = 0
  set $kgm_found = 0
  set $kgm_depth = 0
  while $kgm_indx < fbt_probetab_size && !$kgm_found
    set $kgm_fbt_probep = (struct fbt_probe *)fbt_probetab[$kgm_indx]
    set $kgm_depth = 0
    if $kgm_fbt_probep
      set $kgm_probeid = (struct fbt_probe *)$kgm_fbt_probep->fbtp_id
      if $kgm_probeid == $arg0
        set $kgm_found = 1
        loop_break
      else
	set $kgm_fbt_probep = $kgm_fbt_probep->fbtp_hashnext
	while $kgm_fbt_probep
	  set $kgm_depth++
	  set $kgm_probeid = (struct fbt_probe *)$kgm_fbt_probep->fbtp_id
	  if $kgm_probeid == $arg0
	    set $kgm_found = 1
	    loop_break
	  else
	    set $kgm_fbt_probep = $kgm_fbt_probep->fbtp_hashnext
	  end
        end
      end
    end
    if !$kgm_found
      set $kgm_indx++
    else
      printf "fbt_probetab[index=%d], depth=%d, 0x%x\n", $kgm_indx, $kgm_depth, $kgm_fbt_probep
      printf "(gdb) p *(struct fbt_probe *)0x%x\n", $kgm_fbt_probep
      p *(struct fbt_probe *)$kgm_fbt_probep
      set $kgm_fbtp_ctl = (struct fbt_probe *)$kgm_fbt_probep->fbtp_ctl
      showmodctl $kgm_fbtp_ctl
      loop_break
    end
  end
end
document showfbtprobe
Syntax: (gdb) showfbtprobe <id>
| Display info about an fbt probe given an id.
| Traverses fbt_probetab and matches <id> with fbtp_id.
| The <id> is found using dtrace -l
end

define showzstacktrace
	set $kgm_trace = (void*)$arg0
	if ($argc == 1)
		set $kgm_trace_size = 15
	end
	if ($argc == 2)
		set $kgm_trace_size = $arg1
	end
	set $kgm_trace_current = 0
	while ($kgm_trace_current < $kgm_trace_size)
		set $kgm_trace_addr = (void**)$kgm_trace + $kgm_trace_current
		set $kgm_trace_value = *((void**)$kgm_trace_addr) 
		#printf "\t\t"
		output /a $kgm_trace_value
		set $kgm_trace_current = $kgm_trace_current + 1
		printf "\n"
	end
end

document showzstacktrace
Syntax:  showzstacktrace <saved stacktrace> [size]
| Routine to print a stacktrace stored by OSBacktrace.
| size is optional, defaults to 15.
end

define showzalloc
	set $kgm_zallocation = zallocations[$arg0]
	print $kgm_zallocation
	showztrace $kgm_zallocation->za_trace_index
end

document showzalloc
Syntax:  showzalloc <index>
| Prints a zallocation from the zallocations array based off its index, 
| and prints the associated symbolicated backtrace.
end

define showztrace
	set $kgm_ztrace = &ztraces[$arg0]
	showztraceaddr $kgm_ztrace
end

document showztrace
Syntax:  showztrace <trace index>
| Prints the backtrace from the ztraces array at index
end

define showztraceaddr
	print *$arg0
	showzstacktrace $arg0->zt_stack ($arg0)->zt_depth
end

document showztraceaddr
Syntax:  showztraceaddr <trace address>
| Prints the struct ztrace passed in
end

#TODO: Iterate through the hash table, or make top_ztrace accurate in the face of deallocations (better idea).
define showtopztrace
	set $kgm_top_ztrace = top_ztrace
	printf "Index: %d\n", (top_ztrace - ztraces)
	showztraceaddr $kgm_top_ztrace
end

document showtopztrace
Syntax:  showtopztrace 
| Shows the ztrace with the biggest size. (according to top_ztrace, not by iterating through the hash table)
end

define showzallocs
	set $kgm_zallocation_current_index = 0
	set $kgm_zallocations_count = 0
	set $kgm_max_zallocation = zleak_alloc_buckets
	printf "INDEX  ADDRESS     "
	if $kgm_lp64
        printf "         "
    end
	printf "TRACE   SIZE\n"
	while ($kgm_zallocation_current_index < $kgm_max_zallocation)
		set $kgm_zallocation_current = zallocations[$kgm_zallocation_current_index]
		if ($kgm_zallocation_current->element != 0)
			printf "%5d  %p   ", $kgm_zallocation_current_index, $kgm_zallocation_current->za_element
			printf "%5d %6lu\n", $kgm_zallocation_current->za_trace_index, $kgm_zallocation_current->za_size
			set $kgm_zallocations_count = $kgm_zallocations_count + 1
		end
		set $kgm_zallocation_current_index = $kgm_zallocation_current_index + 1
	end
	printf "Total allocations: %d\n", $kgm_zallocations_count
end

document showzallocs
Syntax:  showzallocs
| Prints all allocations in the zallocations table
end

define showzallocsfortrace
	set $kgm_zallocation_current_index = 0
	set $kgm_zallocations_count = 0
	set $kgm_max_zallocation = zleak_alloc_buckets
	printf "INDEX  ADDRESS     "
	if $kgm_lp64
        printf "         "
    end
	printf "SIZE\n"
	while ($kgm_zallocation_current_index < $kgm_max_zallocation)
		set $kgm_zallocation_current = zallocations[$kgm_zallocation_current_index]
		if ($kgm_zallocation_current->element != 0 && $kgm_zallocation_current->za_trace_index == $arg0)
			printf "%5d  %p ", $kgm_zallocation_current_index, $kgm_zallocation_current->za_element
			printf "%6lu\n", $kgm_zallocation_current->size
			set $kgm_zallocations_count = $kgm_zallocations_count + 1
		end
		set $kgm_zallocation_current_index = $kgm_zallocation_current_index + 1
	end
	printf "Total allocations: %d\n", $kgm_zallocations_count
end

document showzallocsfortrace
Syntax:  showzallocsfortrace <trace index>
| Prints all allocations pointing to the passed in trace's index into ztraces by looking through zallocations table
end

define showztraces
	showztracesabove 0
end

document showztraces
Syntax:  showztraces
| Prints all traces with size > 0
end

define showztracesabove
	set $kgm_ztrace_current_index = 0
	set $kgm_ztrace_count = 0
	set $kgm_max_ztrace = zleak_trace_buckets
	printf "INDEX    SIZE\n"
	while ($kgm_ztrace_current_index < $kgm_max_ztrace)
		set $kgm_ztrace_current = ztraces[$kgm_ztrace_current_index]
		if ($kgm_ztrace_current->zt_size > $arg0)
			printf "%5d  %6lu\n", $kgm_ztrace_current_index, $kgm_ztrace_current->zt_size
			set $kgm_ztrace_count = $kgm_ztrace_count + 1
		end
		set $kgm_ztrace_current_index = $kgm_ztrace_current_index + 1
	end
	printf "Total traces: %d\n", $kgm_ztrace_count
end

document showztracesabove
Syntax:  showztracesabove <size>
| Prints all traces with size greater than X
end

define showztracehistogram
	set $kgm_ztrace_current_index = 0
	set $kgm_ztrace_count = 0
	set $kgm_max_ztrace = zleak_trace_buckets
	printf "INDEX  HIT_COUNT  COLLISIONS\n"
	while ($kgm_ztrace_current_index < $kgm_max_ztrace)
		set $kgm_ztrace_current = ztraces[$kgm_ztrace_current_index]
		if ($kgm_ztrace_current->zt_hit_count != 0)
			printf "%5d      %5d    %5d\n", $kgm_ztrace_current_index, $kgm_ztrace_current->zt_hit_count, $kgm_ztrace_current->zt_collisions
			set $kgm_ztrace_count = $kgm_ztrace_count + 1
		end
		set $kgm_ztrace_current_index = $kgm_ztrace_current_index + 1
	end
	printf "Total traces: %d\n", $kgm_ztrace_count
end

document showztracehistogram
Syntax:  showztracehistogram
| Prints the histogram of the ztrace table
end

define showzallochistogram
	set $kgm_zallocation_current_index = 0
	set $kgm_zallocations_count = 0
	set $kgm_max_zallocation = zleak_alloc_buckets
	printf "INDEX  HIT_COUNT\n"
	while ($kgm_zallocation_current_index < $kgm_max_zallocation)
		set $kgm_zallocation_current = zallocations[$kgm_zallocation_current_index]
		if ($kgm_zallocation_current->za_hit_count != 0)
			printf "%5d      %5d\n", $kgm_zallocation_current_index, $kgm_zallocation_current->za_hit_count
			set $kgm_zallocations_count = $kgm_zallocations_count + 1
		end
		set $kgm_zallocation_current_index = $kgm_zallocation_current_index + 1
	end
	printf "Total allocations: %d\n", $kgm_zallocations_count
end

document showzallochistogram
Syntax:  showzallochistogram
| Prints the histogram for the zalloc table
end

define showzstats
	printf "z_alloc_collisions: %u, z_trace_collisions: %u\n", z_alloc_collisions, z_trace_collisions
	printf "z_alloc_overwrites: %u, z_trace_overwrites: %u\n", z_alloc_overwrites, z_trace_overwrites
	printf "z_alloc_recorded: %u, z_trace_recorded: %u\n", z_alloc_recorded, z_trace_recorded
end

document showzstats
Syntax:  showzstats
| Prints the zone leak detection stats
end


set $kgm_au_sentry_hash_table_size = 97

define showsession1
  set $p = (struct au_sentry *)$arg0
  showptr $p
  printf "  0x%08x  0x%08x  0x%016x", $p->se_auinfo.ai_asid, $p->se_auinfo.ai_auid, $p->se_auinfo.ai_flags
  printf "  %3ld  %3ld", $p->se_refcnt, $p->se_procnt
  printf "\n"
end

define showsessionhdr
  printf "au_sentry "
  showptrhdrpad
  printf "  ASID        AUID        FLAGS               C    P\n"
end

define showsession
  showsessionhdr
  showsession1 $arg0
end

document showsession
Syntax:  showsession
| Display info about a specified audit session
end

define showallsessions
  showsessionhdr
  set $kgm_au_sentry_hash_table = au_sentry_bucket
  set $i = $kgm_au_sentry_hash_table_size - 1
  while $i >= 0
    set $p = $kgm_au_sentry_hash_table[$i].lh_first
    while $p != 0
      showsession1 $p
      set $p = $p->se_link.le_next
    end
    set $i = $i - 1
  end
end

document showallsessions
Syntax:  showallsessions
| Prints the audit sessions in the global hash table
end

define showauhistorystack
  set $ii = $arg0
  set $pp = (void **)$arg1
  while $ii > 0
    printf "  "
    x/i $pp[$ii-1]
    set $ii = $ii - 1
  end
end

define showauhistory1
  set $p = (struct au_history *)$arg0
  set $stack_depth = $p->stack_depth
  set $stack = $p->stack
  showptr $p->ptr
  if $p->event == 1
    printf "    REF"
  end
  if $p->event == 2
    printf "  UNREF"
  end
  if $p->event == 3
    printf "  BIRTH"
  end
  if $p->event == 4
    printf "  DEATH"
  end
  if $p->event == 5
    printf "  FIND"
  end
  set $p = &$p->se
  printf "  0x%08x  0x%08x  0x%016x", $p->se_auinfo.ai_asid, $p->se_auinfo.ai_auid, $p->se_auinfo.ai_flags
  printf "  %3ld  %3ld", $p->se_refcnt, $p->se_procnt
  printf "\n"
  showauhistorystack $stack_depth $stack
end

define showauhistory
  set $i = (au_history_index-1) % au_history_size
  if au_history_index >= au_history_size
    set $n = au_history_size
  else
    set $n = au_history_index
  end
  while $n > 0
    if au_history[$i].ptr != 0 && (0 == $arg0 || au_history[$i].ptr == $arg0)
      printf "[% 4d]  ", $i
      showauhistory1 &au_history[$i]
    end
    set $n = $n - 1
    set $i = ($i - 1) % au_history_size
  end
end

define showallauhistory
  showauhistory 0
end

define showkwqheader
	printf "        kwq     "
    showptrhdrpad
	printf "    kwqaddr     "
    showptrhdrpad
    printf "  inqueue  fakecount  highseq  lowseq   flags   lastunlock    p_rwwc"
    printf "\n          "
end

define showkwqint
	printf "              "
    	set $kgm_kwq = (ksyn_wait_queue_t)$arg0
	showptr $kgm_kwq
	printf "  "
	showptr $kgm_kwq->kw_addr
	printf "   "
	printf "  %d      ", $kgm_kwq->kw_inqueue
	printf "    %d  ", $kgm_kwq->kw_fakecount
	printf "     0x%x  ", $kgm_kwq->kw_highseq
	printf "  0x%x  ", $kgm_kwq->kw_lowseq
	printf "  0x%x  ", $kgm_kwq->kw_flags
	printf "  0x%x  ", $kgm_kwq->kw_lastunlockseq
	printf "    0x%x  ", $kgm_kwq->kw_pre_rwwc
	printf "\n"
end

define show_kwq
	showkwqheader
	showkwqint $arg0
end

document show_kwq
Syntax: (gdb) show_kwq <kwq>
| Display info about one ksyn_wait_queue
end

# Internal routine used by "showpthread_mutex" to abstract possible loads from
# user space
define _loadfrommutex
        if (kdp_pmap == 0)
                set $kgm_loadval = *(uintptr_t *)$arg0
        else
        if ($kgm_x86_abi == 0xe)
              set $kgm_loadval = *(uint32_t *)$arg0
        else
        if ($kgm_x86_abi == 0xf)
            if ($kgm_mtype == $kgm_mtype_i386)
                    _loadk32m64 $arg0
                    set $kgm_loadval = $kgm_k32read64  
            else
                    set $kgm_loadval = *(uint32_t *)$arg0
            end
        end
        end
end
end

define show_pthreadmutex
	set $newact = (struct thread *) $arg0
	set $ourtask = (struct task *)($newact->task)
    	set $our_user_is64 = ($ourtask->taskFeatures[0] & 0x80000000)
	_kgm_flush_loop
	set $mutex = (void *)$arg1
	set kdp_pmap = $newact->task->map->pmap
	_kgm_flush_loop
	_kgm_update_loop
	set $newiss = (x86_saved_state_t *) ($newact->machine.pcb->iss)
	set $kgm_x86_abi = $newiss.flavor
	if ($our_user_is64 != 0)
		printf "\tUser 64Bit\n "
		printf "\tSignature: "
		set $nextval = $mutex
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tflags: "
		set $nextval = $mutex + 12
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tSeqs: "
		set $nextval = $mutex + 20
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $mutex + 24
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $mutex + 28
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\ttid[0]: "
		set $nextval = $mutex + 32
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\ttid[1]: "
		set $nextval = $mutex + 36
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
	else
		printf "\tUser 32Bit\n "
		printf "\tSignature: "
		set $nextval = $mutex
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tflags: "
		set $nextval = $mutex + 8
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tSeqs: "
		set $nextval = $mutex + 16
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $mutex + 20
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $mutex + 24
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\ttid[0]: "
		set $nextval = $mutex + 32
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\ttid[1]: "
		set $nextval = $mutex + 36
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
	end
	printf "\n"
	resetstacks
end


document show_pthreadmutex
Syntax: (gdb) show_pthreadmutex <thread> <user_mutexaddr>
| Display the mutex contents from userspace.
end


define show_pthreadcondition
	set $newact = (struct thread *) $arg0
	set $ourtask = (struct task *)($newact->task)
    	set $our_user_is64 = ($ourtask->taskFeatures[0] & 0x80000000)
	_kgm_flush_loop
	set $cond = (void *)$arg1
	set kdp_pmap = $newact->task->map->pmap
	_kgm_flush_loop
	_kgm_update_loop
	set $newiss = (x86_saved_state_t *) ($newact->machine.pcb->iss)
	set $kgm_x86_abi = $newiss.flavor
	if ($our_user_is64 != 0)
		printf "\tUser 64Bit\n "
		printf "\tSignature: "
		set $nextval = $cond
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tflags: "
		set $nextval = $cond + 12
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tSeqs: "
		set $nextval = $cond + 24
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $cond + 28
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $cond + 32
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tMutex lowaddr: "
		set $nextval = $cond + 16
		_loadfrommutex $nextval
		printf "0x%08x\n",$kgm_loadval
		printf "\tMutex highaddr: "
		set $nextval = $cond + 20
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
	else
		printf "\tUser 32Bit\n "
		printf "\tSignature: "
		set $nextval = $cond
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tflags: "
		set $nextval = $cond + 8
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tSeqs: "
		set $nextval = $cond + 16
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $cond + 20
		_loadfrommutex $nextval
		printf "0x%x   ",$kgm_loadval
		set $nextval = $cond + 24
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
		printf "\tMutex addr: "
		set $nextval = $cond + 12
		_loadfrommutex $nextval
		printf "0x%x\n",$kgm_loadval
	end
	printf "\n"
	resetstacks
end


document show_pthreadcondition
Syntax: (gdb) show_pthreadcondition <thread> <user_cvaddr>
| Display the condition variable contents from userspace.
end

define processortimers
    set $kgm_p = processor_list
    printf "Processor\t\t\t Last dispatch\t\t Next deadline\t\t difference\n"
    while $kgm_p
        printf "Processor %d: %p\t", $kgm_p->cpu_id, $kgm_p
        printf " 0x%016llx\t", $kgm_p->last_dispatch
        set $kgm_rt_timer = &(cpu_data_ptr[$kgm_p->cpu_id].rtclock_timer)
        printf " 0x%016llx    \t", $kgm_rt_timer->deadline
        set $kgm_rt_diff =  ((long long)$kgm_p->last_dispatch) - ((long long)$kgm_rt_timer->deadline)
        printf " 0x%016llx  ", $kgm_rt_diff
# normally the $kgm_rt_diff will be close to the last dispatch time, or negative 
# When it isn't, mark the result as bad. This is a suggestion, not an absolute
        if ( ($kgm_rt_diff > 0) && ((long long)$kgm_p->last_dispatch) - ($kgm_rt_diff + 1) > 0 )
            printf "probably BAD\n"
        else
            printf "(ok)\n"
        end
        # dump the call entries (Intel only)
        if (($kgm_mtype & $kgm_mtype_x86_mask) == $kgm_mtype_x86_any)
            printf "Next deadline set at: 0x%016llx. Timer call list:", $kgm_rt_timer->when_set
            set $kgm_entry = (queue_t *)$kgm_rt_timer->queue
            if ($kgm_entry == $kgm_rt_timer)
                printf " (empty)\n"
            else
                printf "\n entry:      "
                showptrhdrpad
                printf "deadline           soft_deadline      delta      (*func)(param0,param1)\n"
                while $kgm_entry != $kgm_rt_timer
                    set $kgm_timer_call = (timer_call_t) $kgm_entry
                    set $kgm_call_entry = (struct call_entry *) $kgm_entry
                    printf " "
                    showptr $kgm_entry
                    printf ": 0x%016llx 0x%016llx 0x%08x (%p)(%p,%p)\n", \
                        $kgm_call_entry->deadline, \
                        $kgm_timer_call->soft_deadline, \
                        ($kgm_call_entry->deadline - $kgm_timer_call->soft_deadline), \
                        $kgm_call_entry->func, \
                        $kgm_call_entry->param0, $kgm_call_entry->param1
                    set $kgm_entry = $kgm_entry->next
                end
            end
        end
        set $kgm_p = $kgm_p->processor_list
    end
    printf "\n"
end

document processortimers
Syntax: (gdb) processortimers 
| Print details of processor timers, noting any timer which might be suspicious
end