File: kernel.c

package info (click to toggle)
crash 3.10-11-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 19,768 kB
  • ctags: 8,466
  • sloc: ansic: 85,196; makefile: 23,212; sh: 20
file content (4703 lines) | stat: -rwxr-xr-x 138,371 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
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
/* kernel.c - core analysis suite
 *
 * Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
 * Copyright (C) 2002, 2003, 2004 David Anderson
 * Copyright (C) 2002, 2003, 2004 Red Hat, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 11/09/99, 1.0    Initial Release
 * 11/12/99, 1.0-1  Bug fixes
 * 12/10/99, 1.1    Fixes, new commands, support for v1 SGI dumps
 * 01/18/00, 2.0    Initial gdb merger, support for Alpha
 * 02/01/00, 2.1    Bug fixes, new commands, options, support for v2 SGI dumps
 * 02/29/00, 2.2    Bug fixes, new commands, options
 * 04/11/00, 2.3    Bug fixes, new command, options, initial PowerPC framework
 * 04/12/00  ---    Transition to BitKeeper version control
 * 
 * BitKeeper ID: @(#)kernel.c 1.21
 *
 * 09/28/00  ---    Transition to CVS version control
 *
 * CVS: $Revision: 1.115 $ $Date: 2005/01/28 20:26:13 $
 */

#include "defs.h"
#include <elf.h>

static void do_module_cmd(ulong, char *, ulong, char *, char *);
static char *find_module_objfile(char *, char *, char *);
static char *get_uptime(char *);
static char *get_loadavg(char *);
static void get_lkcd_regs(struct bt_info *, ulong *, ulong *);
static void dump_sys_call_table(char *, int);
static int get_NR_syscalls(void);
static void display_bh_1(void);
static void display_bh_2(void);
static void display_bh_3(void);
static void dump_timer_data(void);
static void dump_timer_data_tvec_bases_v1(void);
static void dump_timer_data_tvec_bases_v2(void);
struct tv_range;
static void init_tv_ranges(struct tv_range *, int, int, int);
static int do_timer_list(ulong,int, ulong *, void *,ulong *,struct tv_range *);
static int compare_timer_data(const void *, const void *);
static void panic_this_kernel(void);
static void dump_waitq(ulong, char *);
static void reinit_modules(void);
static int verify_modules(void);
static void verify_namelist(void);
static char *debug_kernel_version(char *);
static int restore_stack(struct bt_info *);


/*
 *  Gather a few kernel basics.
 */
void
kernel_init(int when)
{
	int i;
	char *p1, *p2, buf[BUFSIZE];;
	struct syment *sp1, *sp2;

	if (pc->flags & KERNEL_DEBUG_QUERY)
		return;

	switch (when)
	{
	case PRE_GDB:
		kt->stext = symbol_value("_stext");
		kt->etext = symbol_value("_etext");
		get_text_init_space(); 
		if (symbol_exists("__init_begin")) {
			kt->init_begin = symbol_value("__init_begin");
			kt->init_end = symbol_value("__init_end");
		}
		kt->end = symbol_value("_end");
	
		if (symbol_exists("smp_num_cpus")) {
			kt->flags |= SMP;
			get_symbol_data("smp_num_cpus", sizeof(int), &kt->cpus);
			if (kt->cpus < 1 || kt->cpus > NR_CPUS)
				error(WARNING, 
				    "invalid value: smp_num_cpus: %d\n",
					kt->cpus);
		} else if (symbol_exists("__per_cpu_offset")) {
			kt->flags |= SMP;
			kt->cpus = 1;
		} else 
			kt->cpus = 1;

		if ((sp1 = symbol_search("__per_cpu_start")) &&
	 	    (sp2 =  symbol_search("__per_cpu_end")) &&
		    (sp1->type == 'A') && (sp2->type == 'A') &&
		    (sp2->value > sp1->value))
			kt->flags |= SMP|PER_CPU_OFF;
	
		get_symbol_data("xtime", sizeof(struct timespec), &kt->date);
	
		if (pc->flags & GET_TIMESTAMP) {
	        	fprintf(fp, "%s\n\n", 
				strip_linefeeds(ctime(&kt->date.tv_sec)));
			clean_exit(0);
		}
	
	        readmem(symbol_value("system_utsname"), KVADDR, &kt->utsname,
	                sizeof(struct new_utsname), "system_utsname", 
			FAULT_ON_ERROR);
		strncpy(buf, kt->utsname.release, MIN(strlen(kt->utsname.release), 65));
		if (ascii_string(kt->utsname.release)) {
			p1 = p2 = buf;
			while (*p2 != '.')
				p2++;
			*p2 = NULLCHAR;
			kt->kernel_version[0] = atoi(p1);
			p1 = ++p2;
			while (*p2 != '.')
				p2++;
			*p2 = NULLCHAR;
			kt->kernel_version[1] = atoi(p1);
			p1 = ++p2;
			while ((*p2 >= '0') && (*p2 <= '9'))
				p2++;
			*p2 = NULLCHAR;
			kt->kernel_version[2] = atoi(p1);
		}
		break;

	case POST_GDB:
		if (symbol_exists("__per_cpu_offset")) {
			i = get_array_length("__per_cpu_offset", NULL, 0);
			get_symbol_data("__per_cpu_offset", 
				sizeof(long)*(i <= NR_CPUS ? i : NR_CPUS),
				&kt->__per_cpu_offset[0]); 
			kt->flags |= PER_CPU_OFF;
		}
		MEMBER_OFFSET_INIT(runqueue_cpu, "runqueue", "cpu");
		if (VALID_MEMBER(runqueue_cpu)) {
			MEMBER_OFFSET_INIT(cpu_s_curr, "cpu_s", "curr");
			MEMBER_OFFSET_INIT(cpu_s_idle, "cpu_s", "idle");
		 	STRUCT_SIZE_INIT(cpu_s, "cpu_s"); 
			kt->runq_siblings = get_array_length("runqueue.cpu", 
				NULL, 0);
			if (symbol_exists("__cpu_idx") &&
			    symbol_exists("__rq_idx")) {
				if (!readmem(symbol_value("__cpu_idx"), KVADDR, 
			            &kt->__cpu_idx[0], sizeof(long) * NR_CPUS,
	                            "__cpu_idx[NR_CPUS]", RETURN_ON_ERROR))
					error(INFO, 
				      "cannot read __cpu_idx[NR_CPUS] array\n");
				if (!readmem(symbol_value("__rq_idx"), KVADDR, 
			            &kt->__rq_idx[0], sizeof(long) * NR_CPUS,
	                            "__rq_idx[NR_CPUS]", RETURN_ON_ERROR))
					error(INFO, 
				       "cannot read __rq_idx[NR_CPUS] array\n");
			} else if (kt->runq_siblings > 1) 
				error(INFO, 
	     "runq_siblings: %d: __cpu_idx and __rq_idx arrays don't exist?\n",
					kt->runq_siblings);
		} else {
			MEMBER_OFFSET_INIT(runqueue_idle, "runqueue", "idle");
			MEMBER_OFFSET_INIT(runqueue_curr, "runqueue", "curr");
		}
		MEMBER_OFFSET_INIT(runqueue_active, "runqueue", "active");
		MEMBER_OFFSET_INIT(runqueue_expired, "runqueue", "expired");
		MEMBER_OFFSET_INIT(runqueue_arrays, "runqueue", "arrays");
		MEMBER_OFFSET_INIT(prio_array_queue, "prio_array", "queue");
                MEMBER_OFFSET_INIT(prio_array_nr_active, "prio_array",
                        "nr_active");
		STRUCT_SIZE_INIT(runqueue, "runqueue"); 
		STRUCT_SIZE_INIT(prio_array, "prio_array"); 

               /*
                *  In 2.4, smp_send_stop() sets smp_num_cpus back to 1
                *  in some, but not all, architectures.  So if a count
                *  of 1 is found, be suspicious, and check the
                *  init_tasks[NR_CPUS] array (also intro'd in 2.4),
                *  for idle thread addresses.  For 2.2, prepare for the
	        *  eventuality by verifying the cpu count with the machine
		*  dependent count.
                */
                if ((kt->flags & SMP) && DUMPFILE() && (kt->cpus == 1)) {
                        if (symbol_exists("init_tasks")) {
                                ulong init_tasks[NR_CPUS];
				int nr_cpus;

				BZERO(&init_tasks[0], sizeof(ulong) * NR_CPUS);

				nr_cpus = get_array_length("init_tasks", 
					NULL, 0);
				if ((nr_cpus < 1) || (nr_cpus > NR_CPUS))
                                        nr_cpus = NR_CPUS;

				get_idle_threads(&init_tasks[0], nr_cpus);

                                for (i = kt->cpus = 0; i < nr_cpus; i++)
                                        if (init_tasks[i])
                                                kt->cpus++;
                	} else 
				kt->cpus = machdep->get_smp_cpus();
		}

		if ((kt->flags & SMP) && ACTIVE() && (kt->cpus == 1) &&
		    (kt->flags & PER_CPU_OFF))
			kt->cpus = machdep->get_smp_cpus();

		if (kt->cpus > NR_CPUS) {
			error(WARNING, 
       "calculated number of cpus (%d) greater than compiled-in NR_CPUS (%d)\n",
				kt->cpus, NR_CPUS);
			error(FATAL, "recompile crash with larger NR_CPUS\n");
		}

		STRUCT_SIZE_INIT(spinlock_t, "spinlock_t");
		verify_spinlock();

		STRUCT_SIZE_INIT(list_head, "list_head"); 
		MEMBER_OFFSET_INIT(list_head_next, "list_head", "next"); 
		MEMBER_OFFSET_INIT(list_head_prev, "list_head", "prev"); 
		if (OFFSET(list_head_next) != 0)
		    error(WARNING, 
			"list_head.next offset: %ld: list command may fail\n",
				OFFSET(list_head_next));

        	MEMBER_OFFSET_INIT(hlist_node_next, "hlist_node", "next");
        	MEMBER_OFFSET_INIT(hlist_node_pprev, "hlist_node", "pprev");
		STRUCT_SIZE_INIT(hlist_head, "hlist_head"); 
		STRUCT_SIZE_INIT(hlist_node, "hlist_node"); 

		MEMBER_OFFSET_INIT(irq_desc_t_status,  "irq_desc_t", "status");
		MEMBER_OFFSET_INIT(irq_desc_t_handler, "irq_desc_t", "handler");
		MEMBER_OFFSET_INIT(irq_desc_t_action, "irq_desc_t", "action");
		MEMBER_OFFSET_INIT(irq_desc_t_depth, "irq_desc_t", "depth");
		MEMBER_OFFSET_INIT(hw_interrupt_type_typename, 
			"hw_interrupt_type", "typename");
		MEMBER_OFFSET_INIT(hw_interrupt_type_startup,
			"hw_interrupt_type", "startup");
		MEMBER_OFFSET_INIT(hw_interrupt_type_shutdown,
			"hw_interrupt_type", "shutdown");
		MEMBER_OFFSET_INIT(hw_interrupt_type_handle, 
                        "hw_interrupt_type", "handle");
		MEMBER_OFFSET_INIT(hw_interrupt_type_enable,
			"hw_interrupt_type", "enable");
		MEMBER_OFFSET_INIT(hw_interrupt_type_disable,
			"hw_interrupt_type", "disable");
		MEMBER_OFFSET_INIT(hw_interrupt_type_ack, 
			"hw_interrupt_type", "ack");
		MEMBER_OFFSET_INIT(hw_interrupt_type_end, 
			"hw_interrupt_type", "end");
		MEMBER_OFFSET_INIT(hw_interrupt_type_set_affinity,
			"hw_interrupt_type", "set_affinity");
		MEMBER_OFFSET_INIT(irqaction_handler, "irqaction", "handler");
		MEMBER_OFFSET_INIT(irqaction_flags, "irqaction", "flags");
		MEMBER_OFFSET_INIT(irqaction_mask, "irqaction", "mask");
		MEMBER_OFFSET_INIT(irqaction_name, "irqaction", "name");
		MEMBER_OFFSET_INIT(irqaction_dev_id, "irqaction", "dev_id");
		MEMBER_OFFSET_INIT(irqaction_next, "irqaction", "next");

		STRUCT_SIZE_INIT(irq_desc_t, "irq_desc_t");

                STRUCT_SIZE_INIT(irq_cpustat_t, "irq_cpustat_t");
                MEMBER_OFFSET_INIT(irq_cpustat_t___softirq_active, 
                        "irq_cpustat_t", "__softirq_active");
                MEMBER_OFFSET_INIT(irq_cpustat_t___softirq_mask, 
                        "irq_cpustat_t", "__softirq_mask");

                STRUCT_SIZE_INIT(timer_list, "timer_list");
                MEMBER_OFFSET_INIT(timer_list_list, "timer_list", "list");
                MEMBER_OFFSET_INIT(timer_list_next, "timer_list", "next");
                MEMBER_OFFSET_INIT(timer_list_entry, "timer_list", "entry");
                MEMBER_OFFSET_INIT(timer_list_expires, "timer_list", "expires");
                MEMBER_OFFSET_INIT(timer_list_function, 
                        "timer_list", "function");
                STRUCT_SIZE_INIT(timer_vec_root, "timer_vec_root");
		if (VALID_STRUCT(timer_vec_root))
                	MEMBER_OFFSET_INIT(timer_vec_root_vec, 
				"timer_vec_root", "vec");
                STRUCT_SIZE_INIT(timer_vec, "timer_vec");
		if (VALID_STRUCT(timer_vec))
                	MEMBER_OFFSET_INIT(timer_vec_vec, "timer_vec", "vec");

	        STRUCT_SIZE_INIT(tvec_root_s, "tvec_root_s");
                if (VALID_STRUCT(tvec_root_s)) {
                	STRUCT_SIZE_INIT(tvec_t_base_s, "tvec_t_base_s");
                        MEMBER_OFFSET_INIT(tvec_t_base_s_tv1,
                                "tvec_t_base_s", "tv1");
	               	MEMBER_OFFSET_INIT(tvec_root_s_vec, 
				"tvec_root_s", "vec");
	                STRUCT_SIZE_INIT(tvec_s, "tvec_s");
	               	MEMBER_OFFSET_INIT(tvec_s_vec, "tvec_s", "vec");
		}

                STRUCT_SIZE_INIT(__wait_queue, "__wait_queue");
                if (VALID_STRUCT(__wait_queue)) {
                        MEMBER_OFFSET_INIT(__wait_queue_task,
                                "__wait_queue", "task");
                        MEMBER_OFFSET_INIT(__wait_queue_head_task_list,
                                "__wait_queue_head", "task_list");
                        MEMBER_OFFSET_INIT(__wait_queue_task_list,
                                "__wait_queue", "task_list");
                } else {
                	STRUCT_SIZE_INIT(wait_queue, "wait_queue");
			if (VALID_STRUCT(wait_queue)) {
                		MEMBER_OFFSET_INIT(wait_queue_task, 
					"wait_queue", "task");
                		MEMBER_OFFSET_INIT(wait_queue_next, 
					"wait_queue", "next");
			}
		}

		STRUCT_SIZE_INIT(pt_regs, "pt_regs");
		STRUCT_SIZE_INIT(softirq_state, "softirq_state");
		STRUCT_SIZE_INIT(desc_struct, "desc_struct");

		STRUCT_SIZE_INIT(char_device_struct, "char_device_struct");
		if (VALID_STRUCT(char_device_struct)) {
			MEMBER_OFFSET_INIT(char_device_struct_next,
				"char_device_struct", "next");
			MEMBER_OFFSET_INIT(char_device_struct_name,
				"char_device_struct", "name");
			MEMBER_OFFSET_INIT(char_device_struct_fops,
				"char_device_struct", "fops");
			MEMBER_OFFSET_INIT(char_device_struct_major,
				"char_device_struct", "major");
		}

	        MEMBER_OFFSET_INIT(module_kallsyms_start, "module", 
			"kallsyms_start");

		if (VALID_MEMBER(module_kallsyms_start)) {
        		MEMBER_OFFSET_INIT(kallsyms_header_sections,
				"kallsyms_header", "sections");
        		MEMBER_OFFSET_INIT(kallsyms_header_section_off,
				"kallsyms_header", "section_off");
        		MEMBER_OFFSET_INIT(kallsyms_header_symbols,
				"kallsyms_header", "symbols");
        		MEMBER_OFFSET_INIT(kallsyms_header_symbol_off,
				"kallsyms_header", "symbol_off");
        		MEMBER_OFFSET_INIT(kallsyms_header_string_off,
				"kallsyms_header", "string_off");
        		MEMBER_OFFSET_INIT(kallsyms_symbol_section_off,
				"kallsyms_symbol", "section_off");
        		MEMBER_OFFSET_INIT(kallsyms_symbol_symbol_addr,
				"kallsyms_symbol", "symbol_addr");
        		MEMBER_OFFSET_INIT(kallsyms_symbol_name_off,
				"kallsyms_symbol", "name_off");
        		MEMBER_OFFSET_INIT(kallsyms_section_start,
				"kallsyms_section", "start");
        		MEMBER_OFFSET_INIT(kallsyms_section_size,
				"kallsyms_section", "size");
        		MEMBER_OFFSET_INIT(kallsyms_section_name_off,
				"kallsyms_section", "name_off");
			STRUCT_SIZE_INIT(kallsyms_header, "kallsyms_header");
			STRUCT_SIZE_INIT(kallsyms_symbol, "kallsyms_symbol");
			STRUCT_SIZE_INIT(kallsyms_section, "kallsyms_section");
			
			if (!(kt->flags & NO_KALLSYMS))
				kt->flags |= KALLSYMS_V1;
		}

		MEMBER_OFFSET_INIT(module_num_symtab, "module", "num_symtab");

		if (VALID_MEMBER(module_num_symtab)) {
			MEMBER_OFFSET_INIT(module_symtab, "module", "symtab");
			MEMBER_OFFSET_INIT(module_strtab, "module", "strtab");
			
			if (!(kt->flags & NO_KALLSYMS))
				kt->flags |= KALLSYMS_V2;
		}
		break;
	}
}

/*
 *  For lack of a better manner of verifying that the namelist and dumpfile
 *  (or live kernel) match up, verify that the Linux banner is where
 *  the namelist says it is.  Since this is common place to bail, extra
 *  debug statements are available.
 */
void
verify_version(void)
{
	char buf[BUFSIZE];
	ulong linux_banner;
        int argc;
        char *arglist[MAXARGS];
	char *p1, *p2;

	if (pc->flags & KERNEL_DEBUG_QUERY)
		return;

	BZERO(buf, BUFSIZE);

	get_symbol_data("linux_banner", sizeof(ulong), &linux_banner);

	if (!IS_KVADDR(linux_banner))
		error(WARNING, "invalid linux_banner pointer: %lx\n", 
			linux_banner);

	if (!accessible(linux_banner)) 
		goto bad_match;

	if (!read_string(linux_banner, buf, BUFSIZE-1))
		error(WARNING, "cannot read linux_banner string\n");

	if (ACTIVE()) {
		if (strlen(kt->proc_version) && !STREQ(buf, kt->proc_version)) {
               		if (CRASHDEBUG(1)) {
                        	fprintf(fp, "/proc/version:\n%s", 
					kt->proc_version);
                        	fprintf(fp, "linux_banner:\n%s\n", buf);
                	}
			goto bad_match;
		} else if (CRASHDEBUG(1)) 
                       	fprintf(fp, "linux_banner:\n%s\n", buf);
	}

	if (DUMPFILE()) {
		if (!STRNEQ(buf, "Linux version")) {
                	if (CRASHDEBUG(1)) 
                        	fprintf(fp, "linux_banner:\n%s\n", buf);
			goto bad_match;
		}
		strcpy(kt->proc_version, buf);
	}

	verify_namelist();

	if (strstr(kt->proc_version, "gcc version 3.3.3"))
		kt->flags |= GCC_3_3_3;
	if (strstr(kt->proc_version, "gcc version 3.3.2"))
		kt->flags |= GCC_3_3_2;
	else if (strstr(kt->proc_version, "gcc version 3.2.3"))
		kt->flags |= GCC_3_2_3;
	else if (strstr(kt->proc_version, "gcc version 3.2"))
		kt->flags |= GCC_3_2;
	else if (strstr(kt->proc_version, "gcc version 2.96"))
		kt->flags |= GCC_2_96;

	/*
	 *  Keeping the gcc version with #define's is getting out of hand.
	 */
	if ((p1 = strstr(kt->proc_version, "gcc version "))) {
		BZERO(buf, BUFSIZE);
		p1 += strlen("gcc version ");
		p2 = buf;
		while (((*p1 >= '0') && (*p1 <= '9')) || (*p1 == '.')) {
			if (*p1 == '.')
				*p2++ = ' ';
			else
				*p2++ = *p1;
			p1++;
		}
		argc = parse_line(buf, arglist);

		switch (argc)
		{
		case 0:
		case 1:
			break;
		case 2:
			kt->gcc_version[0] = atoi(arglist[0]);
			kt->gcc_version[1] = atoi(arglist[1]);
			break;
		default:
			kt->gcc_version[0] = atoi(arglist[0]);
			kt->gcc_version[1] = atoi(arglist[1]);		
			kt->gcc_version[2] = atoi(arglist[2]);
			break;
		}
	}

	return;

bad_match:
	if (REMOTE())
		sprintf(buf, "%s:%s", pc->server, pc->server_memsrc);
	else
		sprintf(buf, ACTIVE() ? pc->live_memsrc : pc->dumpfile);

	error(INFO, "%s and %s do not match!\n",
		pc->system_map ? pc->system_map : 
		pc->namelist_debug ? pc->namelist_debug : pc->namelist, buf); 

	program_usage(SHORT_FORM);
}

/*
 *  Quick test to verify that we're not using a UP debug kernel on
 *  an SMP system.
 */
void
verify_spinlock(void)
{
	char buf[BUFSIZE];

	if ((kt->flags & SMP) && (SIZE(spinlock_t) == 0)) {
        	error(INFO,
           "debug data shows spinlock_t as an incomplete type (undefined),\n");
                fprintf(fp, "%sbut \"%s\" is an SMP kernel.\n",
                	space(strlen(pc->program_name)+2),
                        pc->namelist);
                if (CRASHDEBUG(1)) {
                        fprintf(fp, "\ngdb> ptype spinlock_t\n");
                        sprintf(buf, "ptype spinlock_t");
                        gdb_pass_through(buf, NULL, 0);
                }
                non_matching_kernel();
	}
}

/*
 *  Something doesn't jive.
 */
void
non_matching_kernel(void)
{
	int kernels = 0;

	if (pc->namelist)
		kernels++;
	if (pc->namelist_debug)
		kernels++;
	if (pc->debuginfo_file)
		kernels++;

	fprintf(fp, 
"\nErrors like the one above typically occur when the kernel%s and memory source\ndo not match.  These are the files being used:\n\n", kernels > 1 ? "s" : "");

        if (REMOTE()) {
                switch (pc->flags &
                        (NAMELIST_LOCAL|NAMELIST_UNLINKED|NAMELIST_SAVED))
                {
                case NAMELIST_UNLINKED:
                        fprintf(fp, "      KERNEL: %s  (temporary)\n",
                                pc->namelist);
                        break;

                case (NAMELIST_UNLINKED|NAMELIST_SAVED):
                        fprintf(fp, "      KERNEL: %s\n", pc->namelist);
                        break;

                case NAMELIST_LOCAL:
                        fprintf(fp, "      KERNEL: %s\n", pc->namelist);
                        break;
                }
        } else {
        	if (pc->system_map) {
                	fprintf(fp, "  SYSTEM MAP: %s\n", pc->system_map);
                	fprintf(fp, "DEBUG KERNEL: %s %s\n", pc->namelist,
                		debug_kernel_version(pc->namelist));
        	} else
                	fprintf(fp, "      KERNEL: %s\n", pc->namelist);
	}

        if (pc->debuginfo_file)
                fprintf(fp, "   DEBUGINFO: %s\n", pc->debuginfo_file);
        else if (pc->namelist_debug)
                fprintf(fp, "DEBUG KERNEL: %s %s\n", pc->namelist_debug,
                        debug_kernel_version(pc->namelist_debug));

        fprintf(fp, "    DUMPFILE: ");
        if (ACTIVE()) {
                if (REMOTE_ACTIVE())
                        fprintf(fp, "%s@%s  (remote live system)\n",
                                pc->server_memsrc, pc->server);
                else
                        fprintf(fp, "%s\n", pc->live_memsrc);
        } else {
                if (REMOTE_DUMPFILE())
                        fprintf(fp, "%s@%s  (remote dumpfile)\n",
                                pc->server_memsrc, pc->server);
                else
                        fprintf(fp, "%s\n", pc->dumpfile);
        }

	fprintf(fp, "\n");

	if ((pc->flags & FINDKERNEL) && !(pc->system_map)) {
		fprintf(fp, 
		   "The kernel \"%s\" is most likely incorrect.\n",
			pc->namelist);
		fprintf(fp, 
         "Try a different kernel name, or use a System.map file argument.\n\n");
	}

	exit(1);
}

/*
 *  Only two checks are made here: 
 *
 *    1. if the namelist is SMP and the memory source isn't, bail out.
 *    2. if the basic gcc versions differ, issue a warning only.
 */
static void
verify_namelist()
{
	int i;
	char command[BUFSIZE];
	char buffer[BUFSIZE];
	char buffer2[BUFSIZE];
	char buffer3[BUFSIZE];
	char buffer4[BUFSIZE];
	char buffer5[BUFSIZE];
	char *p1;
	FILE *pipe;
	int found;
	char *namelist;
	int namelist_smp;
	int target_smp;

	if (pc->flags & KERNEL_DEBUG_QUERY)
		return;

	if (!strlen(kt->utsname.version))
		return;

	namelist = pc->namelist ? pc->namelist : pc->namelist_debug;
	target_smp = strstr(kt->utsname.version, " SMP ") ? TRUE : FALSE;
	namelist_smp = FALSE;

        sprintf(command, "/usr/bin/strings %s", namelist);
        if ((pipe = popen(command, "r")) == NULL) {
                error(INFO, "%s: %s\n", namelist, strerror(errno));
                return;
        }

	found = FALSE;
	sprintf(buffer3, "(unknown)");
        while (fgets(buffer, BUFSIZE-1, pipe)) {
		if (!strstr(buffer, "Linux version 2."))
			continue;

                if (STREQ(buffer, kt->proc_version)) {
                	found = TRUE;
			break;
		}

		if (strstr(buffer, " SMP ")) {
			namelist_smp = TRUE;
			strcpy(buffer2, buffer);
		}

		if ((p1 = strstr(buffer, "(gcc version "))) {
			p1 += strlen("(gcc version ");
			i = 0;
			while (*p1 != ' ') 
				buffer3[i++] = *p1++;
			buffer3[i] = NULLCHAR;
		}

		break;
        }
        pclose(pipe);

	if (!found && (p1 = strstr(kt->proc_version, "(gcc version "))) {
		p1 += strlen("(gcc version ");
		i = 0;
		while (*p1 != ' ') 
			buffer4[i++] = *p1++;
		buffer4[i] = NULLCHAR;
		if (!STREQ(buffer3, buffer4)) {
        		if (REMOTE())
                		sprintf(buffer, "%s:%s kernel", 
					pc->server, pc->server_memsrc);
        		else
                		sprintf(buffer, "%s kernel", ACTIVE() ? 
					"live system" : pc->dumpfile);
        		sprintf(buffer5, "  %s: %s\n  %s: %s\n\n",
                		namelist, buffer3,
                		buffer, buffer4);
        		error(WARNING, 
		           "kernels compiled by different gcc versions:\n%s",
				buffer5);
		}
	}

	if (found) {
                if (CRASHDEBUG(1)) {
                	fprintf(fp, "verify_namelist:\n");
			fprintf(fp, "/proc/version:\n%s", kt->proc_version);
			fprintf(fp, "utsname version: %s\n",
				kt->utsname.version);
			fprintf(fp, "%s:\n%s\n", namelist, buffer);
		}
		return;
	}

        if (CRASHDEBUG(1)) {
               	fprintf(fp, "verify_namelist:\n");
                fprintf(fp, "/proc/version:\n%s", kt->proc_version);
                fprintf(fp, "utsname version: %s\n", kt->utsname.version);
                fprintf(fp, "%s:\n%s\n", namelist, buffer2);
        }

	if (target_smp == namelist_smp)
		return;

        if (REMOTE())
                sprintf(buffer, "%s:%s", pc->server, pc->server_memsrc);
        else
                sprintf(buffer, ACTIVE() ? "live system" : pc->dumpfile);

	sprintf(buffer2, " %s is %s -- %s is %s\n",
                namelist, namelist_smp ? "SMP" : "not SMP",
                buffer, target_smp ? "SMP" : "not SMP");

	error(INFO, "incompatible arguments: %s%s",
		strlen(buffer2) > 48 ? "\n  " : "", buffer2);

        program_usage(SHORT_FORM);
}




/*
 *  This routine disassembles text in one of four manners.  A starting
 *  address, an expression, or symbol must be entered.  Then:
 *
 *   1. if a count is appended, disassemble that many instructions starting
 *      at the target address.
 *   2. if a count is NOT entered, and the target address is the starting
 *      address of a function, disassemble the whole function.
 *   3. if the target address is other than the starting address of a 
 *      function, and no count argument is appended, then disassemble one 
 *      instruction. 
 *   4. If the -r option is used, disassemble all instructions in a routine
 *      up to and including the target address.
 *   5. If -u option, just pass the user address and count, ignoring any of
 *      the above.
 */

static char *dis_err = "gdb unable to disassemble kernel virtual address %lx\n";

void
cmd_dis(void)
{
	int c;
	int do_load_module_filter, do_machdep_filter, reverse; 
	int unfiltered, user_mode, count_entered;
	ulong curaddr;
	ulong revtarget;
	ulong count;
	ulong offset;
	struct syment *sp;
	struct gnu_request *req;
	char *savename;
	char buf1[BUFSIZE];
	char buf2[BUFSIZE];
	char buf3[BUFSIZE];
	char buf4[BUFSIZE];
	char buf5[BUFSIZE];
	
	reverse = count_entered = FALSE;
	sp = NULL;
	unfiltered = user_mode = do_machdep_filter = do_load_module_filter = 0;

	req = (struct gnu_request *)getbuf(sizeof(struct gnu_request));
	req->buf = GETBUF(BUFSIZE);
	req->flags |= GNU_FROM_TTY_OFF|GNU_RETURN_ON_ERROR;
	req->count = 1;

        while ((c = getopt(argcnt, args, "ulrx")) != EOF) {
                switch(c)
		{
		case 'x':
			unfiltered = TRUE;
			break;

		case 'u':
			user_mode = TRUE;
			break;

		case 'r':
			reverse = TRUE;
			break;

		case 'l':
			if (GDB_PATCHED())
				error(INFO, "line numbers are not available\n");
			else
				req->flags |= GNU_PRINT_LINE_NUMBERS;
			BZERO(buf4, BUFSIZE);
			break;

		default:
			argerrs++;
			break;
		}
	}

	if (argerrs)
		cmd_usage(pc->curcmd, SYNOPSIS);

        if (args[optind]) {
                if (can_eval(args[optind])) 
                        req->addr = eval(args[optind], FAULT_ON_ERROR, NULL);
                else if (hexadecimal(args[optind], 0)) {
                        req->addr = htol(args[optind], FAULT_ON_ERROR, NULL);
			if (!user_mode && 
			    !(sp = value_search(req->addr, &offset))) {
				error(WARNING, 
				    "%x: no associated kernel symbol found\n",
					req->addr);
				unfiltered = TRUE;
			}
                } else if ((sp = symbol_search(args[optind]))) {
                        req->addr = sp->value;
			req->flags |= GNU_FUNCTION_ONLY;
		} else {
                        fprintf(fp, "symbol not found: %s\n", args[optind]);
                        fprintf(fp, "possible aternatives:\n");
                        if (!symbol_query(args[optind], "  ", NULL))
                                fprintf(fp, "  (none found)\n");
			FREEBUF(req->buf);
			FREEBUF(req);
                        return;
                }

                if (args[++optind]) {
			if (reverse) {
				error(INFO, 
			            "count argument ignored with -r option\n");
			} else {
                        	req->count = stol(args[optind], 
					FAULT_ON_ERROR, NULL);
				req->flags &= ~GNU_FUNCTION_ONLY;
				count_entered++;
			}
		}

		if (unfiltered) {
                	sprintf(buf1, "x/%ldi 0x%lx",  
				req->count ? req->count : 1, req->addr);
        		gdb_pass_through(buf1, NULL, 0);
			return;
		}

		if (!user_mode && !IS_KVADDR(req->addr)) 
			error(FATAL, "%lx is not a kernel virtual address\n",
				req->addr);

		if (user_mode) {
                	sprintf(buf1, "x/%ldi 0x%lx",  
				req->count ? req->count : 1, req->addr);
			pc->cmdgenspec = pc->cmdgencur;
        		gdb_pass_through(buf1, NULL, 0);
			return;
		}

		do_load_module_filter = module_symbol(req->addr, NULL, NULL, 
			NULL, output_radix);

		if (!reverse) {
			req->command = GNU_RESOLVE_TEXT_ADDR;
			gdb_interface(req);
                        if ((req->flags & GNU_COMMAND_FAILED) ||
			    do_load_module_filter ||
			    (req->flags & GNU_FUNCTION_ONLY)) {
				req->flags &= ~GNU_COMMAND_FAILED;
				if (sp) {
					savename = sp->name;
                                        if ((sp = next_symbol(NULL, sp)))
                                                req->addr2 = sp->value;
					else
                                		error(FATAL, 
				        "unable to determine symbol after %s\n",
                                        		savename);
				} else {
					if ((sp = value_search(req->addr, NULL))
                                             && (sp = next_symbol(NULL, sp)))
						req->addr2 = sp->value;	
					else 
						error(FATAL, dis_err, req->addr);
				}
                        }

			do_machdep_filter = machdep->dis_filter(req->addr,NULL);
			count = 0;
			open_tmpfile();
#ifdef OLDWAY
			req->command = GNU_DISASSEMBLE;
			req->fp = pc->tmpfile;
			gdb_interface(req);
#else
			sprintf(buf1, "x/%ldi 0x%lx",
                                count_entered && req->count ? req->count : 
				req->flags & GNU_FUNCTION_ONLY ? 
				req->addr2 - req->addr : 1, 
				req->addr);
        		gdb_pass_through(buf1, NULL, GNU_RETURN_ON_ERROR);
#endif
			if (req->flags & GNU_COMMAND_FAILED) {
				close_tmpfile();
				error(FATAL, dis_err, req->addr);
			}

        		rewind(pc->tmpfile);
        		while (fgets(buf2, BUFSIZE, pc->tmpfile)) {
				if (STRNEQ(buf2, "Dump of") ||
				    STRNEQ(buf2, "End of"))
					continue;

				if (do_load_module_filter)
					load_module_filter(buf2, LM_DIS_FILTER);

				if (STRNEQ(buf2, "0x")) 
					extract_hex(buf2, &curaddr, ':', TRUE);

				if ((req->flags & GNU_FUNCTION_ONLY) &&
				    (curaddr >= req->addr2))
					break;

				if (do_machdep_filter)
					machdep->dis_filter(curaddr, buf2);

				if (req->flags & GNU_FUNCTION_ONLY) {
                                        if (req->flags & 
                                            GNU_PRINT_LINE_NUMBERS) {
                                                get_line_number(curaddr, buf3,
                                                        FALSE);
                                                if (!STREQ(buf3, buf4)) {
                                                        print_verbatim(
                                                            pc->saved_fp, buf3);
                                                        print_verbatim(
                                                            pc->saved_fp, "\n");
                                                        strcpy(buf4, buf3);
                                                }
                                        }

                			print_verbatim(pc->saved_fp, buf2); 
					continue;
				} else {
					if (curaddr < req->addr) 
						continue;

                			if (req->flags & 
					    GNU_PRINT_LINE_NUMBERS) {
                        			get_line_number(curaddr, buf3, 
							FALSE);
                        			if (!STREQ(buf3, buf4)) {
                                			print_verbatim(
							    pc->saved_fp, buf3);
                                			print_verbatim(
						            pc->saved_fp, "\n");
                                			strcpy(buf4, buf3);
                        			}
                			} 

                			print_verbatim(pc->saved_fp, buf2);

					if (LASTCHAR(clean_line(buf2)) 
						!= ':') {
						if (++count == req->count)
							break;
					}
				}
        		}
			close_tmpfile();
		}
        }
        else cmd_usage(pc->curcmd, SYNOPSIS);

	if (!reverse) {
		FREEBUF(req->buf);
		FREEBUF(req);
		return;
	}

        revtarget = req->addr;
        if ((sp = value_search(revtarget, NULL)) == NULL)
                error(FATAL, "cannot resolve address: %lx\n", revtarget);

        sprintf(buf1, "0x%lx", revtarget);

        open_tmpfile();

        req->addr = sp->value;
        req->flags |= GNU_FUNCTION_ONLY;
        req->command = GNU_RESOLVE_TEXT_ADDR;
        gdb_interface(req);
        req->flags &= ~GNU_COMMAND_FAILED;
	savename = sp->name;
        if ((sp = next_symbol(NULL, sp)))
                req->addr2 = sp->value;
        else {
		close_tmpfile();
                error(FATAL, "unable to determine symbol after %s\n", savename);
	}

	do_machdep_filter = machdep->dis_filter(req->addr, NULL);
#ifdef OLDWAY
	req->command = GNU_DISASSEMBLE;
	req->fp = pc->tmpfile;
	gdb_interface(req);
#else
        sprintf(buf5, "x/%ldi 0x%lx",
        	(revtarget - req->addr) ? revtarget - req->addr : 1, 
		req->addr);
        gdb_pass_through(buf5, NULL, GNU_RETURN_ON_ERROR);
#endif
        if (req->flags & GNU_COMMAND_FAILED) {
		close_tmpfile();
        	error(FATAL, dis_err, req->addr);
	}

        rewind(pc->tmpfile);
        while (fgets(buf2, BUFSIZE, pc->tmpfile)) {
                if (STRNEQ(buf2, "Dump of") || STRNEQ(buf2, "End of"))
                	continue;

                if (do_load_module_filter)
                        load_module_filter(buf2, LM_DIS_FILTER);

                if (STRNEQ(buf2, "0x"))
                	extract_hex(buf2, &curaddr, ':', TRUE);

		if (do_machdep_filter)
			machdep->dis_filter(curaddr, buf2);

		if (req->flags & GNU_PRINT_LINE_NUMBERS) {
			get_line_number(curaddr, buf3, FALSE);
			if (!STREQ(buf3, buf4)) {
				print_verbatim(pc->saved_fp, buf3);
				print_verbatim(pc->saved_fp, "\n");
				strcpy(buf4, buf3);
			}
		}

                print_verbatim(pc->saved_fp, buf2);
                if (STRNEQ(buf2, buf1)) {
                	if (LASTCHAR(clean_line(buf2)) != ':') 
                        	break;

        		fgets(buf2, BUFSIZE, pc->tmpfile);

                	if (do_load_module_filter)
                        	load_module_filter(buf2, LM_DIS_FILTER);

			if (do_machdep_filter) 
				machdep->dis_filter(curaddr, buf2);

                	print_verbatim(pc->saved_fp, buf2);
			break;
		}
        }

        close_tmpfile();
	FREEBUF(req->buf);
	FREEBUF(req);
}

#ifdef NOT_USED
/*
 *  To avoid premature stoppage/extension of a dis <function> that includes
 *  one of the following x86/gcc 3.2 constant declarations, don't allow them
 *  to be considered the next text symbol.
 */
static struct syment *
next_text_symbol(struct syment *sp_in)
{
	return next_symbol(NULL, sp_in);
	struct syment *sp;

	sp = sp_in; 
	while ((sp = next_symbol(NULL, sp))) {
		if (STREQ(sp->name, "__constant_c_and_count_memset") ||
		    STREQ(sp->name, "__constant_copy_from_user") ||
	            STREQ(sp->name, "__constant_copy_from_user_nocheck") ||
	            STREQ(sp->name, "__constant_copy_to_user") ||
                    STREQ(sp->name, "__constant_copy_to_user_nocheck") ||
		    STREQ(sp->name, "__constant_memcpy") ||
		    STREQ(sp->name, "__constant_c_and_count_memset") ||
		    STREQ(sp->name, "__constant_c_x_memset") ||
		    STREQ(sp->name, "__constant_memcpy")) {
			continue;
		}
		break;
	}
	return sp;
}
#endif  /* NOT_USED */

/*
 *  Nothing to do.
 */
int
generic_dis_filter(ulong value, char *buf)
{
	return TRUE;
}

#define FRAMESIZE_DEBUG_MESSAGE \
"usage: bt -F [size|clear|dump|seek|noseek|validate|novalidate] [-I eip]\n  If eip:  set its associated framesize to size.\n           \"validate/novalidate\" will turn on/off V bit for this eip entry.\n  If !eip: \"clear\" will clear the framesize cache and RA seek/noseek flags.\n           \"dump\" will dump the current framesize cache entries.\n           \"seek/noseek\" turns on/off RA seeking.\n           \"validate/novalidate\" turns on/off V bit for all current entries.\n"

/*
 *  Display a kernel stack backtrace.  Arguments may be any number pid or task
 *  values, or, if no arguments are given, the stack trace of the current 
 *  context will be displayed.  Alternatively:
 *
 *     -a  displays the stack traces of the active tasks on each CPU.
 *         (only applicable to crash dumps)
 *     -r  display raw stack data, consisting of a memory dump of the two
 *         pages of memory containing the task_union structure.
 *     -s  displays arguments symbolically.
 */

#define BT_SETUP(TC)                                          \
	BCOPY(&bt_setup, bt, sizeof(struct bt_info));         \
        if (refptr) {                                         \
		BZERO(&reference, sizeof(struct reference));  \
		bt->ref = &reference;                         \
        	bt->ref->str = refptr;                        \
	}                                                     \
        bt->tc = (TC);                                        \
        bt->task = ((TC)->task);			      \
	bt->stackbase = GET_STACKBASE((TC)->task);            \
	bt->stacktop = GET_STACKTOP((TC)->task);              \
	bt->stackbuf = NULL;
 
void
cmd_bt(void)
{
	int c;
	ulong value;
        struct task_context *tc;
	int count, subsequent, active;
	struct stack_hook hook;
	struct bt_info bt_info, bt_setup, *bt;
	struct reference reference;
	char *refptr;

	tc = NULL;
	subsequent = active = count = 0;
	hook.eip = hook.esp = 0;
	refptr = 0;
	bt = &bt_info;
	BZERO(bt, sizeof(struct bt_info));

        while ((c = getopt(argcnt, args, "fF:I:S:aloreEgstd:R:")) != EOF) {
                switch(c)
		{
		case 'f':
			bt->flags |= BT_FULL;
			break;

		case 'o':
			bt->flags |= BT_OLD_BACK_TRACE;
			break;

		case 'R':
			if (refptr) 
				error(INFO, "only one -R option allowed\n");
			else 
				refptr = optarg;
			break;
			
		case 'l':
			if (GDB_PATCHED())
				error(INFO, "line numbers are not available\n");
			else
				bt->flags |= BT_LINE_NUMBERS;
			break;

		case 'E':
			bt->flags |= BT_EFRAME_SEARCH|BT_EFRAME_SEARCH2;
			bt->hp = &hook;
			break;

		case 'e':
			bt->flags |= BT_EFRAME_SEARCH;
			break;

		case 'g':
#if defined(GDB_6_0) || defined(GDB_6_1)
			error(FATAL, 
		       "-g option is not supported with this version of gdb\n");
#else
			bt->flags |= BT_USE_GDB;
#endif
			break;

		case 'd':
			bt->debug = dtol(optarg, FAULT_ON_ERROR, NULL);
			break;

		case 'I':
			bt->hp = &hook;
			hook.eip = convert(optarg, FAULT_ON_ERROR, 
				NULL, NUM_HEX|NUM_EXPR);
			break;

		case 'F':
			if (STREQ(optarg, "seek")) {
				kt->flags |= RA_SEEK;
				kt->flags &= ~NO_RA_SEEK;
				return;
			} else if (STREQ(optarg, "noseek")) {
				kt->flags |= NO_RA_SEEK;
				kt->flags &= ~RA_SEEK;
				return;
			}
			bt->hp = &hook;
			bt->flags |= BT_FRAMESIZE_DEBUG;
			if (STREQ(optarg, "dump"))
				hook.esp = 1;
			else if (STREQ(optarg, "validate"))
				hook.esp = (ulong)-1;
			else if (STREQ(optarg, "novalidate"))
				hook.esp = (ulong)-2;
			else if (STREQ(optarg, "clear")) {
				kt->flags &= ~(RA_SEEK|NO_RA_SEEK);
				hook.esp = 0;
			} else if (*optarg == '-') {
				hook.esp = dtol(optarg+1, FAULT_ON_ERROR, NULL);
				hook.esp = (ulong)(0 - (long)hook.esp);
			} else
				hook.esp = dtol(optarg, FAULT_ON_ERROR, NULL);
			break;

		case 'S':
			bt->hp = &hook;
			hook.esp = htol(optarg, FAULT_ON_ERROR, NULL);
			break;

		case 'a':
			active++;
			break;

		case 'r':
			bt->flags |= BT_RAW;
			break;

		case 's':
			bt->flags |= BT_SYMBOLIC_ARGS;
			break;

		case 't':
			bt->flags |= BT_TEXT_SYMBOLS;
			break;

		default:
			argerrs++;
			if (optopt == 'F') {
				fprintf(fp, FRAMESIZE_DEBUG_MESSAGE);
				return;
			}
			break;
		}
	}

	if (argerrs)
		cmd_usage(pc->curcmd, SYNOPSIS);

	if (bt->flags & BT_FRAMESIZE_DEBUG) {
		if (machdep->flags & FRAMESIZE_DEBUG) {
			while (args[optind]) {
				if (!hook.eip)
                       			hook.eip = convert(args[optind], 
						FAULT_ON_ERROR, NULL, 
						NUM_HEX|NUM_EXPR);
				else {
					fprintf(fp, FRAMESIZE_DEBUG_MESSAGE);
					return;
				}
				optind++;
			}
			machdep->back_trace(bt);
			return;
		}
		error(FATAL, "framesize debug not available\n");
	}

	BCOPY(bt, &bt_setup, sizeof(struct bt_info));

	if (bt->flags & BT_EFRAME_SEARCH2) {
               	tc = CURRENT_CONTEXT();  /* borrow stack */
                BT_SETUP(tc);
                back_trace(bt);
                return;
	}

	if (active) {
		if (ACTIVE())
			error(FATAL, 
				"-a option not supported on a live system\n");

		for (c = 0; c < NR_CPUS; c++) {
			if ((tc = task_to_context(tt->panic_threads[c]))) {
				BT_SETUP(tc);
				if (!BT_REFERENCE_CHECK(bt))
					print_task_header(fp, tc, subsequent++);
				back_trace(bt);
			}
		}

		return;
	}

	if (!args[optind]) {
		tc = CURRENT_CONTEXT();
		BT_SETUP(tc);
		if (!BT_REFERENCE_CHECK(bt))
			print_task_header(fp, tc, 0);
		back_trace(bt);
		return;
	}

	while (args[optind]) {
                switch (str_to_context(args[optind], &value, &tc))
                {
                case STR_PID:
                        for (tc = pid_to_context(value); tc; tc = tc->tc_next) {
                                BT_SETUP(tc);
                                if (!BT_REFERENCE_CHECK(bt))
                                        print_task_header(fp, tc, subsequent++);
                                back_trace(bt);
                        }
                        break;

                case STR_TASK:
                        BT_SETUP(tc);
                        if (!BT_REFERENCE_CHECK(bt))
                                print_task_header(fp, tc, subsequent++);
                        back_trace(bt);
                        break;

                case STR_INVALID:
                        error(INFO, "%sinvalid task or pid value: %s\n",
                                subsequent++ ? "\n" : "", args[optind]);
                        break;
                }

		optind++;
	}
}

void
print_stack_text_syms(struct bt_info *bt, ulong esp, ulong eip)
{
	ulong next_sp, next_pc;
	int i;
	ulong *up;
	char buf[BUFSIZE];

	if (bt->flags & BT_TEXT_SYMBOLS) {
		fprintf(fp, "%sSTART: %s at %lx\n",
			space(VADDR_PRLEN > 8 ? 14 : 6),
		        closest_symbol(eip), eip);
	}

	if (bt->hp) 
		bt->hp->eip = bt->hp->esp = 0;
	next_pc = next_sp = 0;

	for (i = (esp - bt->stackbase)/sizeof(ulong);
	     i < LONGS_PER_STACK; i++) {
		up = (ulong *)(&bt->stackbuf[i*sizeof(ulong)]);
		if (is_kernel_text_offset(*up)) {
			if (!next_pc) 
				next_pc = *up;
			else if (!next_sp) 
				next_sp = bt->stackbase + (i * sizeof(long));
		}
		if (is_kernel_text(*up) && (bt->flags & 
		    (BT_TEXT_SYMBOLS|BT_TEXT_SYMBOLS_PRINT))) { 
			if (bt->flags & (BT_ERROR_MASK|BT_TEXT_SYMBOLS))
                               	fprintf(fp, "  %s[%s] %s at %lx\n",
					bt->flags & BT_ERROR_MASK ?
					"  " : "",
					mkstring(buf, VADDR_PRLEN, 
					RJUST|LONG_HEX,
                               		MKSTR(bt->stackbase + 
					(i * sizeof(long)))),
					closest_symbol(*up), *up);
			else
                               	fprintf(fp, "%lx: %s\n",
                                       	bt->stackbase + 
					(i * sizeof(long)),
                                       	value_to_symstr(*up, buf, 0));
		}
	}

	if (bt->hp) {
		bt->hp->eip = next_pc;
		bt->hp->esp = next_sp;
	}
}

/*
 *  Gather the EIP, ESP and stack address for the target task, and passing 
 *  them on to the machine-specific back trace command.
 */
void
back_trace(struct bt_info *bt)
{
	int i;
	ulong *up;
	char buf[BUFSIZE];
	ulong eip, esp;
	struct bt_info btsave;

	if (bt->flags & BT_RAW) {
		if (bt->hp && bt->hp->esp)
			esp = bt->hp->esp;
		else 
			esp = GET_STACKBASE(bt->task);
		raw_stack_dump(esp, STACKSIZE());
		return;
	}

	if (ACTIVE() && !(bt->flags & BT_EFRAME_SEARCH) && 
            ((bt->task == tt->this_task) || is_task_active(bt->task))) {

		if (BT_REFERENCE_CHECK(bt) ||
		    bt->flags & (BT_TEXT_SYMBOLS_PRINT|BT_TEXT_SYMBOLS_NOPRINT))
			return;

		if (!(bt->flags & BT_KSTACKP))
			fprintf(fp, "(active)\n");

		return;
 	}

	fill_stackbuf(bt);

	if (CRASHDEBUG(4)) {
		for (i = 0, up = (ulong *)bt->stackbuf; 
		     i < LONGS_PER_STACK; i++, up++) {
			if (is_kernel_text(*up))
				fprintf(fp, "%lx: %s\n", 
					bt->task + (i * sizeof(long)),
					value_to_symstr(*up, buf, 0));
		}
	}

	if (BT_REFERENCE_CHECK(bt)) {
		if (can_eval(bt->ref->str)) {
			bt->ref->hexval = eval(bt->ref->str, 
				FAULT_ON_ERROR, NULL);
			bt->ref->cmdflags |= BT_REF_HEXVAL;
		} else if (hexadecimal(bt->ref->str, 0)) {
			bt->ref->hexval = htol(bt->ref->str, 
				FAULT_ON_ERROR, NULL);
			bt->ref->cmdflags |= BT_REF_HEXVAL;
		} else
			bt->ref->cmdflags |= BT_REF_SYMBOL;
	}

	if (bt->flags & BT_EFRAME_SEARCH) {
		machdep->eframe_search(bt); 
		return;
	}
	
	if (bt->hp) {
		if (bt->hp->esp && !INSTACK(bt->hp->esp, bt))
			error(INFO, 
			    "invalid stack address for this task: %lx\n",
				bt->hp->esp);
		eip = bt->hp->eip;
		esp = bt->hp->esp;

		machdep->get_stack_frame(bt, eip ? NULL : &eip, 
			esp ? NULL : &esp);
	 
        } else if (NETDUMP_DUMPFILE())
                get_netdump_regs(bt, &eip, &esp);
	else if (DISKDUMP_DUMPFILE())
                get_diskdump_regs(bt, &eip, &esp);
        else if (LKCD_DUMPFILE())
                get_lkcd_regs(bt, &eip, &esp);
        else
                machdep->get_stack_frame(bt, &eip, &esp);

	if (bt->flags & BT_KSTACKP) {
		bt->stkptr = esp;
		return;
	}

	if (bt->flags & 
	    (BT_TEXT_SYMBOLS|BT_TEXT_SYMBOLS_PRINT|BT_TEXT_SYMBOLS_NOPRINT)) {

		if (machdep->flags & MACHDEP_BT_TEXT) {
			bt->instptr = eip;
			bt->stkptr = esp;
			machdep->back_trace(bt);
		} else
			print_stack_text_syms(bt, esp, eip);

		if (bt->flags & (BT_HARDIRQ|BT_SOFTIRQ)) {
			struct bt_info btloc;
			struct stack_hook stack_hook;

			BZERO(&btloc, sizeof(struct bt_info));
			BZERO(&stack_hook, sizeof(struct stack_hook));
			btloc.flags = bt->flags & ~(BT_HARDIRQ|BT_SOFTIRQ);
			btloc.hp = &stack_hook;
			btloc.tc = bt->tc;
			btloc.task = bt->task;
			btloc.stackbase = GET_STACKBASE(bt->task);
			btloc.stacktop = GET_STACKTOP(bt->task);

        		switch (bt->flags & (BT_HARDIRQ|BT_SOFTIRQ))
        		{
        		case BT_HARDIRQ:
				btloc.hp->eip = symbol_value("do_IRQ");
                		btloc.hp->esp = ULONG(bt->stackbuf +
                        	    SIZE(irq_ctx) - (sizeof(unsigned int)*2));
				fprintf(fp, "--- <hard IRQ> ---\n");
                		break;

        		case BT_SOFTIRQ:
				btloc.hp->eip = symbol_value("do_softirq");
                		btloc.hp->esp = ULONG(bt->stackbuf +
                        		OFFSET(thread_info_previous_esp));
				fprintf(fp, "--- <soft IRQ> ---\n");
                		break;
        		}

			back_trace(&btloc);
		}
		
		return;
	}

	bt->instptr = eip;
	bt->stkptr = esp;

complete_trace:

	if (BT_REFERENCE_CHECK(bt))
		BCOPY(bt, &btsave, sizeof(struct bt_info));

	if (CRASHDEBUG(4))
		dump_bt_info(bt);

	machdep->back_trace(bt);

	if ((bt->flags & (BT_HARDIRQ|BT_SOFTIRQ)) && restore_stack(bt))  
		goto complete_trace;

	if (BT_REFERENCE_FOUND(bt)) {
		print_task_header(fp, task_to_context(bt->task), 0);
		BCOPY(&btsave, bt, sizeof(struct bt_info));
		bt->ref = NULL;
		machdep->back_trace(bt);
		fprintf(fp, "\n");
	}
}

/*
 *  Restore a bt_info to make the jump from an IRQ stack to the task's
 *  normal stack.
 */
static int 
restore_stack(struct bt_info *bt)
{
	ulonglong type;

	bt->instptr = bt->stkptr = 0;
	type = 0;

	switch (bt->flags & (BT_HARDIRQ|BT_SOFTIRQ)) 
	{ 
	case BT_HARDIRQ:
		bt->instptr = symbol_value("do_IRQ");
		bt->stkptr = ULONG(bt->stackbuf + 
			SIZE(irq_ctx) - (sizeof(unsigned int)*2));
		type = BT_HARDIRQ;
		break;

	case BT_SOFTIRQ:
		bt->instptr = symbol_value("do_softirq");
               	bt->stkptr = ULONG(bt->stackbuf +
                       	OFFSET(thread_info_previous_esp));
		type = BT_SOFTIRQ;
		break;
	}

	bt->flags &= ~(BT_HARDIRQ|BT_SOFTIRQ); 
	bt->stackbase = GET_STACKBASE(bt->tc->task);
        bt->stacktop = GET_STACKTOP(bt->tc->task);

        if (!readmem(bt->stackbase, KVADDR, bt->stackbuf,
            bt->stacktop - bt->stackbase, 
	    "restore_stack contents", RETURN_ON_ERROR)) {
        	error(INFO, "restore_stack of stack at %lx failed\n", 
			bt->stackbase);
		type = 0;
	}

	if (!(bt->instptr && INSTACK(bt->stkptr, bt)))
		type = 0;

	if (type) {
		if (!BT_REFERENCE_CHECK(bt))
		fprintf(fp, "--- %s ---\n", type == BT_HARDIRQ ? 
			"<hard IRQ>" : "<soft IRQ>");
		return TRUE;
	}

	return FALSE;
}


#define MAXHOOKS (100)

struct stack_hook *
gather_text_list(struct bt_info *bt) 
{
	int cnt;
	struct bt_info btloc;
	char buf[BUFSIZE], *p1;
	struct stack_hook *hooks;
	ulong esp, eip;
	FILE *savedfp;

	BCOPY(bt, &btloc, sizeof(struct bt_info));
	hooks = (struct stack_hook *)GETBUF(sizeof(struct stack_hook)*MAXHOOKS);
	cnt = 0;

	savedfp = fp;
        open_tmpfile2();
	fp = pc->tmpfile2;
        btloc.flags = BT_TEXT_SYMBOLS_PRINT;
        back_trace(&btloc);
        rewind(pc->tmpfile2);
        while (fgets(buf, BUFSIZE, pc->tmpfile2)) {
		if ((p1 = strstr(buf, ":"))) {
			esp = eip = 0;
                	*p1 = NULLCHAR;
			if ((esp = htol(buf, RETURN_ON_ERROR, NULL)) != BADADDR)
                                eip = GET_STACK_ULONG(esp);
			if (esp && eip) {
				hooks[cnt].esp = esp;
				hooks[cnt].eip = eip;
				if (++cnt == MAXHOOKS)
					break;
			}
		}
	}
	close_tmpfile2();
	fp = savedfp;

	if (cnt)
		return (bt->textlist = hooks);
	else {
		FREEBUF(hooks);
		return (bt->textlist = NULL);
	}
}

/*
 *  Debug routine most likely useful from above in back_trace()
 */
void
dump_bt_info(struct bt_info *bt)
{
	fprintf(fp, "        task: %lx\n", bt->task);
	fprintf(fp, "       flags: %llx\n", bt->flags);
	fprintf(fp, "     instptr: %lx\n", bt->instptr);
	fprintf(fp, "      stkptr: %lx\n", bt->stkptr);
	fprintf(fp, "   stackbase: %lx\n", bt->stackbase);
	fprintf(fp, "    stacktop: %lx\n", bt->stacktop);
	fprintf(fp, "          tc: %lx ", (ulong)bt->tc);
	if (bt->tc)
		fprintf(fp, "(%ld, %lx)\n", bt->tc->pid, bt->tc->task);
	else
		fprintf(fp, "(unknown context)\n");
	fprintf(fp, "          hp: %lx\n", (ulong)bt->hp);
	fprintf(fp, "         ref: %lx\n", (ulong)bt->ref);
	fprintf(fp, "    stackbuf: %lx\n", (ulong)bt->stackbuf);
	fprintf(fp, "    textlist: %lx\n", (ulong)bt->textlist);
	fprintf(fp, "    frameptr: %lx\n", (ulong)bt->frameptr);
	fprintf(fp, " call_target: %s\n", bt->call_target ? 
		bt->call_target : "none");
	fprintf(fp, "       debug: %lx\n", bt->debug);
}

/*
 *  LKCD doesn't save state of the active tasks in the TSS, so poke around 
 *  the raw stack for some reasonable hooks.
 */
static void
get_lkcd_regs(struct bt_info *bt, ulong *eip, ulong *esp)
{
	int i;
	char *sym;
	ulong *up;
	ulong sysrq_eip, sysrq_esp;

	if (!is_task_active(bt->task)) {
		machdep->get_stack_frame(bt, eip, esp);
		return;
	}

	sysrq_eip = sysrq_esp = 0;

	for (i = 0, up = (ulong *)bt->stackbuf; i < LONGS_PER_STACK; i++, up++){
		sym = closest_symbol(*up);
		if (STREQ(sym, "dump_execute") && INSTACK(*(up-1), bt)) {
			*eip = *up;
			*esp = *(up-1);
			return;
		}
                /* Begin 3PAR change -- required for our panic path */
		if (STREQ(sym, "dump_ipi") && INSTACK(*(up-1), bt)) {
			*eip = *up;
			*esp = *(up-1);
			return;
		}
		/* End 3PAR change */
                if (STREQ(sym, "panic") && INSTACK(*(up-1), bt)) {
                        *eip = *up;
                        *esp = *(up-1);
                        return;
                }
                if (STREQ(sym, "smp_stop_cpu_interrupt")) {
                        *eip = *up;
                        *esp = bt->task + 
				((char *)(up-1) - bt->stackbuf);
                        return;
                }
                if (STREQ(sym, "stop_this_cpu")) {
                        *eip = *up;
                        *esp = bt->task + 
				((char *)(up-1) - bt->stackbuf);
                        return;
                }
                if (SYSRQ_TASK(bt->task) &&
		    STREQ(sym, "smp_call_function_interrupt")) {
                        sysrq_eip = *up;
                        sysrq_esp = bt->task +
                                ((char *)(up-1) - bt->stackbuf);
                }
	}

	if (sysrq_eip) {
        	*eip = sysrq_eip;
        	*esp = sysrq_esp;
		return;
	}

	machdep->get_stack_frame(bt, eip, esp);
}


/*
 *  Store the head of the kernel module list for future use.
 *  Count the number of symbols defined by all modules in the system,
 *  and pass it on to store_module_symbols() to deal with.
 */
void
module_init(void)
{
	int i, c;
        ulong mod, mod_next;
	uint nsyms;
	ulong total, numksyms;
        char *modbuf, *kallsymsbuf;
	ulong kallsyms_header;
	struct syment *sp, *sp_array[10];
	struct kernel_list_head list;
	int modules_found;

	if (kernel_symbol_exists("module_list")) 
		kt->flags |= KMOD_V1;
	else if (kernel_symbol_exists("modules"))
		kt->flags |= KMOD_V2;
	else 
		error(WARNING, "cannot determine how modules are linked\n");

        if (kt->flags & NO_MODULE_ACCESS || !(kt->flags & (KMOD_V1|KMOD_V2))) {
                error(WARNING, "no kernel module access\n\n");
                kt->module_list = 0;
                kt->mods_installed = 0;
                return;
        }

	STRUCT_SIZE_INIT(module, "module");
        MEMBER_OFFSET_INIT(module_name, "module", "name");
        MEMBER_OFFSET_INIT(module_syms, "module", "syms");

	switch (kt->flags & (KMOD_V1|KMOD_V2))
	{
	case KMOD_V1:
        	MEMBER_OFFSET_INIT(module_size_of_struct, "module", 
			"size_of_struct");
        	MEMBER_OFFSET_INIT(module_next, "module", "next");
        	MEMBER_OFFSET_INIT(module_nsyms, "module", "nsyms");
        	MEMBER_OFFSET_INIT(module_size, "module", "size");
        	MEMBER_OFFSET_INIT(module_flags, "module", "flags");

        	get_symbol_data("module_list", sizeof(ulong), &kt->module_list);
        	kt->kernel_module = symbol_value("kernel_module");
		break;	

	case KMOD_V2: 
		MEMBER_OFFSET_INIT(module_num_syms, "module", "num_syms");
		MEMBER_OFFSET_INIT(module_list, "module", "list");
        	MEMBER_OFFSET_INIT(module_gpl_syms, "module", "gpl_syms");
        	MEMBER_OFFSET_INIT(module_num_gpl_syms, "module", 
			"num_gpl_syms");
        	MEMBER_OFFSET_INIT(module_module_core, "module", 
			"module_core");
        	MEMBER_OFFSET_INIT(module_core_size, "module", 
			"core_size");
        	MEMBER_OFFSET_INIT(module_core_text_size, "module", 
			"core_text_size");

		/*
		 *  Make sure to pick the kernel "modules" list_head symbol,
		 *  not to be confused with the ia64/sn "modules[]" array.
		 *  The kernel modules list_head will either point to itself
		 *  (empty) or contain vmalloc'd module addresses; the ia64/sn
		 *  modules array contains a list of kmalloc'd addresses.
		 */
        	if ((c = get_syment_array("modules", sp_array, 10)) > 1) {
			modules_found = FALSE;
			for (i = 0; i < c; i++) {
				sp = sp_array[i];

				if (!readmem(sp->value, KVADDR, 
				    &list, sizeof(struct kernel_list_head), 
				    "modules list_head test", 
				    RETURN_ON_ERROR|QUIET)) 
					continue;

				if ((ulong)list.next == symbol_value("modules")) {
                			kt->mods_installed = 0;
					return;
				}

				if (IS_VMALLOC_ADDR(list.next) &&
				    IS_VMALLOC_ADDR(list.prev)) {
					kt->kernel_module = sp->value;
					kt->module_list = (ulong)list.next;
					modules_found = TRUE;
					break;
				}
			} 

			if (!modules_found) {
                        	error(WARNING,
          "cannot determine which of %d \"modules\" symbols is appropriate\n\n",
					c);
                       		kt->mods_installed = 0;
                        	kt->flags |= NO_MODULE_ACCESS;
				return;
			}
		} else {
        		get_symbol_data("modules", sizeof(ulong), 
				&kt->module_list);
			if (kt->module_list == symbol_value("modules")) {
                		kt->mods_installed = 0;
				return;
			}
        		kt->kernel_module = symbol_value("modules");
		}
		kt->module_list -= OFFSET(module_list);
		break;
	}

	total = kt->mods_installed = 0;

        modbuf = GETBUF(SIZE(module));
	kallsymsbuf = kt->flags & KALLSYMS_V1 ?
		GETBUF(SIZE(kallsyms_header)) : NULL;

        for (mod = kt->module_list; mod != kt->kernel_module; mod = mod_next) {
		if (CRASHDEBUG(7))
			fprintf(fp, "module: %lx\n", mod);

                if (!readmem(mod, KVADDR, modbuf, SIZE(module), 
		    "module struct", RETURN_ON_ERROR|QUIET)) {
                        error(WARNING,
                            "cannot access vmalloc'd module memory\n\n");
                        kt->mods_installed = 0;
                        kt->flags |= NO_MODULE_ACCESS;
                        FREEBUF(modbuf); 
			return;
		}

		switch (kt->flags & (KMOD_V1|KMOD_V2))
		{
		case KMOD_V1:
                	nsyms = UINT(modbuf + OFFSET(module_nsyms));
			break;
		case KMOD_V2: 
                	nsyms = UINT(modbuf + OFFSET(module_num_syms)) +
				UINT(modbuf + OFFSET(module_num_gpl_syms));
			break;
		}

		total += nsyms;
		total += 2;  /* store the module's start/ending addresses */

		/*
		 *  If the module has kallsyms, set up to grab them as well.
		 */
		switch (kt->flags & (KALLSYMS_V1|KALLSYMS_V2))
		{
		case KALLSYMS_V1: 
			kallsyms_header = ULONG(modbuf +
				OFFSET(module_kallsyms_start));	
			if (kallsyms_header) {
	                	if (!readmem(kallsyms_header, KVADDR, 
				    kallsymsbuf, SIZE(kallsyms_header), 
				    "kallsyms_header", RETURN_ON_ERROR|QUIET)) {
	                        	error(WARNING,
                                      "cannot access module kallsyms_header\n");
				} else {
					nsyms = UINT(kallsymsbuf +
				 	    OFFSET(kallsyms_header_symbols));
					total += nsyms; 
				}
			}
			break;

		case KALLSYMS_V2:
			numksyms = ULONG(modbuf + OFFSET(module_num_symtab));
			total += numksyms; 
			break;
		}

		kt->mods_installed++;

		NEXT_MODULE(mod_next, modbuf);
	}

        FREEBUF(modbuf);
	if (kallsymsbuf)
		FREEBUF(kallsymsbuf);

	switch (kt->flags & (KMOD_V1|KMOD_V2))
	{
	case KMOD_V1:
		store_module_symbols_v1(total, kt->mods_installed);
		break;
	case KMOD_V2:
		store_module_symbols_v2(total, kt->mods_installed);
		break;
	}
}


/*
 *  Verify that the current set of modules jives with what's stored.
 */
static int
verify_modules(void)
{
	int i;
	int found, irregularities;
        ulong mod, mod_next, mod_base;
	long mod_size;
        char *modbuf, *module_name;
	ulong module_list, mod_name; 
	physaddr_t paddr;
	int mods_installed;
	struct load_module *lm;
	char buf[BUFSIZE];

	if (DUMPFILE() || !kt->module_list || (kt->flags & NO_MODULE_ACCESS))
		return TRUE;

	switch (kt->flags & (KMOD_V1|KMOD_V2))
	{
	case KMOD_V1:
        	get_symbol_data("module_list", sizeof(ulong), &module_list);
		break;
	case KMOD_V2:
                if (kt->module_list == symbol_value("modules")) {
			if (!kt->mods_installed)
				return TRUE;
                }
                get_symbol_data("modules", sizeof(ulong), &module_list);
                module_list -= OFFSET(module_list);
		break;
	}

	mods_installed = irregularities = 0;
        modbuf = GETBUF(SIZE(module));

        for (mod = module_list; mod != kt->kernel_module; mod = mod_next) {

                if (!readmem(mod, KVADDR, modbuf, SIZE(module), 
		    "module struct", RETURN_ON_ERROR|QUIET)) {
                        error(WARNING,
                            "cannot access vmalloc'd module memory\n");
                        FREEBUF(modbuf);
                        return FALSE;
		}


                for (i = 0, found = FALSE; i < kt->mods_installed; i++) {
                        lm = &st->load_modules[i];
			if (!kvtop(NULL, lm->mod_base, &paddr, 0)) {
				irregularities++;
                                break;
			}

			switch (kt->flags & (KMOD_V1|KMOD_V2))
			{
			case KMOD_V1:
				mod_base = mod;
				break;
			case KMOD_V2:
				mod_base = ULONG(modbuf + 
					OFFSET(module_module_core));
				break;
			}

			if (lm->mod_base == mod_base) {
				switch (kt->flags & (KMOD_V1|KMOD_V2))
				{
				case KMOD_V1:
        				mod_name = ULONG(modbuf + 
						OFFSET(module_name));
					mod_size = LONG(modbuf + 
						OFFSET(module_size));
                			if (!read_string(mod_name, buf, 
					    BUFSIZE-1) || !STREQ(lm->mod_name, 
					    buf) || (mod_size != lm->mod_size)){
						irregularities++;
						goto irregularity;
					}
					break;
				case KMOD_V2:
        				module_name = modbuf + 
						OFFSET(module_name);
					mod_size = LONG(modbuf + 
						OFFSET(module_core_size));
                			if (strlen(module_name) < MAX_MOD_NAME)
                        			strcpy(buf, module_name);
                			else 
                        			strncpy(buf, module_name, 
							MAX_MOD_NAME-1);
					if (!STREQ(lm->mod_name, buf) ||
					    (mod_size != lm->mod_size)) {
						irregularities++;
						goto irregularity;
					}
					break;
				}
				found = TRUE;
irregularity:
				break;
			}
		}

		if (!found || irregularities) 
			return FALSE;

		mods_installed++;

		NEXT_MODULE(mod_next, modbuf);
	}

        FREEBUF(modbuf);

	if (mods_installed != kt->mods_installed) 
		return FALSE;

	return TRUE;
}


/*
 *  With no arguments, just dump basic data concerning each of the 
 *  currently-loaded modules.  The -s and -S arguments dynamically
 *  loads module symbols from its object file.
 */
#define LIST_MODULE_HDR               (0)
#define LIST_MODULE                   (1)
#define LOAD_ALL_MODULE_SYMBOLS       (2)
#define LOAD_SPECIFIED_MODULE_SYMBOLS (3)
#define DELETE_MODULE_SYMBOLS         (4)
#define DELETE_ALL_MODULE_SYMBOLS     (5)
#define REMOTE_MODULE_SAVE_MSG        (6)
#define REINIT_MODULES                (7)

void
cmd_mod(void)
{
	int c;
	char *objfile, *modref, *tree;
	ulong flag, address;
	char buf[BUFSIZE];

	if (kt->flags & NO_MODULE_ACCESS)
		error(FATAL, "cannot access vmalloc'd module memory\n");

	if (!verify_modules()) {
 	 	error(NOTE, 
	             "modules have changed on this system -- reinitializing\n");
		reinit_modules();
	}

	if (!kt->mods_installed) {
		fprintf(fp, "no modules installed\n");
		return;
	}

	modref = objfile = tree = NULL;
	address = 0;
	flag = LIST_MODULE_HDR;

        while ((c = getopt(argcnt, args, "rd:Ds:St:")) != EOF) {
                switch(c)
		{
                case 'r':
                        if (flag)
                                cmd_usage(pc->curcmd, SYNOPSIS);
                        flag = REINIT_MODULES;
                        break;

		case 'D':
			if (flag) 
				cmd_usage(pc->curcmd, SYNOPSIS);
			flag = DELETE_ALL_MODULE_SYMBOLS;
			break;

		case 'd':
                        if (flag)
                                cmd_usage(pc->curcmd, SYNOPSIS);
                        else
                                flag = DELETE_MODULE_SYMBOLS;

                        if (hexadecimal(optarg, 0) &&
                            (strlen(optarg) == VADDR_PRLEN)) {
                                address = htol(optarg, FAULT_ON_ERROR, NULL);
                                if (!is_module_address(address, buf))
                                        cmd_usage(pc->curcmd, SYNOPSIS);
				modref = buf;
                        } else if (is_module_name(optarg, &address, NULL))
                                modref = optarg;
                        else
                                cmd_usage(pc->curcmd, SYNOPSIS);
                        break;

		case 't':
			if (is_directory(optarg))
				tree = optarg;
			else {
                		error(INFO, "%s is not a directory\n", args[2]);
                                cmd_usage(pc->curcmd, SYNOPSIS);
			}
			break;

		case 'S':
			if (flag) 
				cmd_usage(pc->curcmd, SYNOPSIS);
			else
				flag = LOAD_ALL_MODULE_SYMBOLS; 
			break;

		case 's':
                        if (flag)
				cmd_usage(pc->curcmd, SYNOPSIS);
			else
				flag = LOAD_SPECIFIED_MODULE_SYMBOLS;

			if (hexadecimal(optarg, 0) && 
			    (strlen(optarg) == VADDR_PRLEN)) {
				address = htol(optarg, FAULT_ON_ERROR, NULL);
				if (!is_module_address(address, buf))
					cmd_usage(pc->curcmd, SYNOPSIS);
				modref = buf;
			} else if (is_module_name(optarg, &address, NULL))
				modref = optarg;
			else
				cmd_usage(pc->curcmd, SYNOPSIS);
			break;

		default:
			argerrs++;
			break;
		}
	}

	if (tree && (flag != LOAD_ALL_MODULE_SYMBOLS))
		argerrs++;

	if (argerrs)
		cmd_usage(pc->curcmd, SYNOPSIS);

	if (NO_MODULES()) {
                error(INFO, "no modules loaded in this kernel\n");
		if (flag != LIST_MODULE_HDR)
                	cmd_usage(pc->curcmd, SYNOPSIS);
		return;
	}

	switch (flag)
	{
	case LOAD_ALL_MODULE_SYMBOLS:
		switch (argcnt) 
		{
		case 3:
			if (is_directory(args[2]))
				tree = args[2];
			else {
                		error(INFO, "%s is not a directory\n", args[2]);
				cmd_usage(pc->curcmd, SYNOPSIS);
			}
			break;

		case 2:
			break;

		default:
			cmd_usage(pc->curcmd, SYNOPSIS);
		}
		break;

	case LOAD_SPECIFIED_MODULE_SYMBOLS:
		switch (argcnt)
		{
		case 4:
			objfile = args[3];
			if (!file_exists(objfile, NULL)) { 
				if (!(objfile = 
				    find_module_objfile(modref, objfile, tree)))
					error(FATAL, 
				    "%s: cannot find or load object file: %s\n",
						modref, args[3]);
			} 
			break;

		case 3:
                        if (!(objfile = find_module_objfile(modref,NULL,tree)))
                        	error(FATAL, 
                              "cannot find or load object file for %s module\n",
					modref);
			break;

		default:
			cmd_usage(pc->curcmd, SYNOPSIS);
		}

                if (!is_elf_file(objfile)) {
                        error(INFO, "%s: not an ELF format object file\n", 
				objfile);
                        cmd_usage(pc->curcmd, SYNOPSIS);
                }

		break;

	default:
		break;
	}

	do_module_cmd(flag, modref, address, objfile, tree);

}


/*
 *  Do the simple list work for cmd_mod().
 */

static void
do_module_cmd(ulong flag, char *modref, ulong address, 
	char *objfile, char *tree)
{
	int i, j;
	struct load_module *lm, *lmp;
	int maxnamelen;
	int maxsizelen;
	char buf1[BUFSIZE];
	char buf2[BUFSIZE];
	char buf3[BUFSIZE];

	if (NO_MODULES())
		return;

	switch (flag)
	{
	case LIST_MODULE:
	case LIST_MODULE_HDR:
	 	maxnamelen = maxsizelen = 0;
		
		for (i = 0; i < kt->mods_installed; i++) {
			lm = &st->load_modules[i];
			maxnamelen = strlen(lm->mod_name) > maxnamelen ? 
				strlen(lm->mod_name) : maxnamelen;
	
	                sprintf(buf1, "%ld", lm->mod_size);
			maxsizelen = strlen(buf1) > maxsizelen ? 
				strlen(buf1) : maxsizelen;
	        }
	
		if (flag == LIST_MODULE_HDR) {
			fprintf(fp, "%s  %s  %s  OBJECT FILE\n",
				mkstring(buf1, VADDR_PRLEN, CENTER|LJUST, 
				"MODULE"),
				mkstring(buf2, maxnamelen, LJUST, "NAME"),
				mkstring(buf3, maxsizelen, RJUST, "SIZE"));
		}
	
		for (i = 0; i < kt->mods_installed; i++) {
			lm = &st->load_modules[i];
			if (!address || (lm->module_struct == address) ||
			    (lm->mod_base == address)) {
				fprintf(fp, "%lx  ", lm->module_struct);
		
				fprintf(fp, "%s  ", mkstring(buf1, maxnamelen, 
					LJUST, lm->mod_name));
		
				fprintf(fp, "%6ld  ", lm->mod_size);
		
				if (strlen(lm->mod_namelist))
					fprintf(fp, "%s %s", 
						lm->mod_namelist,
						lm->mod_flags & MOD_REMOTE ?
						" (temporary)" : "");  
				else {
					fprintf(fp, "(not loaded)");
					if (lm->mod_flags & MOD_KALLSYMS)
						fprintf(fp, 
					   "  [CONFIG_KALLSYMS]");
				}
		
				fprintf(fp, "\n");
			}
		}
		break;

	case REMOTE_MODULE_SAVE_MSG:
		if (!REMOTE())
			return;

                for (i = j = 0, lmp = NULL; i < kt->mods_installed; i++) {
                        lm = &st->load_modules[i];
			if (lm->mod_flags & MOD_REMOTE) {
				j++;
				lmp = lm;
			}
		}

		switch (j)
		{
		case 0:
			return;

		case 1:
			error(NOTE, 
 "\nTo save the %s module object locally,\n      enter: \"save %s\"\n",
				lmp->mod_name, lmp->mod_name);
			break;

		default:
			error(NOTE, 
"\nTo save all temporary remote module objects locally,\n      enter: \"save modules\"\n");
			fprintf(fp, 
 "      To save a single remote module object locally,\n      enter: \"save NAME\",\n"
 "      where \"NAME\" is one of the module names shown in the list above.\n");
			break;
		}
		break;
	
	case LOAD_SPECIFIED_MODULE_SYMBOLS:
		if (!load_module_symbols(modref, objfile, address)) 
			error(FATAL, "cannot load symbols from: %s\n", objfile);
		do_module_cmd(LIST_MODULE_HDR, 0, address, 0, NULL);
		do_module_cmd(REMOTE_MODULE_SAVE_MSG, 0, 0, 0, NULL);
		break;

	case LOAD_ALL_MODULE_SYMBOLS:
		for (i = j = 0; i < kt->mods_installed; i++) {
			lm = &st->load_modules[i];

			if (STREQ(lm->mod_name, "(unknown module)")) {
				error(INFO,
                          "cannot find object file for unknown module at %lx\n",
					lm->mod_base);
				continue;
			}

			modref = lm->mod_name;
			address = lm->mod_base;

			if ((objfile = find_module_objfile(modref,NULL,tree))) {
				if (!is_elf_file(objfile)) {
                        		error(INFO, 
			                  "%s: not an ELF format object file\n",
						objfile);
				} else if (!load_module_symbols(modref, 
					objfile, address))
					error(INFO, 
				           "cannot load symbols from: %s\n",
						objfile);
				do_module_cmd(j++ ? 
					LIST_MODULE : LIST_MODULE_HDR,
					0, address, 0, tree);
				FREEBUF(objfile);
			} else if ((lm->mod_flags & MOD_LOAD_SYMS) ||
			    strlen(lm->mod_namelist)) { 
				if (CRASHDEBUG(1))
                        		fprintf(fp, 
				      "%s: module symbols are already loaded\n",
                                		modref);
				do_module_cmd(j++ ? 
					LIST_MODULE : LIST_MODULE_HDR,
					0, address, 0, tree);
			} else
				error(INFO,
                              "cannot find or load object file for %s module\n",
					modref);
		}
		do_module_cmd(REMOTE_MODULE_SAVE_MSG, 0, 0, 0, tree);
		break;

	case DELETE_ALL_MODULE_SYMBOLS:
		delete_load_module(ALL_MODULES);
		break;

	case DELETE_MODULE_SYMBOLS:
		delete_load_module(address);
		break;

	case REINIT_MODULES:
		reinit_modules();
        	do_module_cmd(LIST_MODULE_HDR, NULL, 0, NULL, NULL);
		break;
	}
}

/*
 *  Reinitialize the current set of modules:
 *
 *   1. first clear out all references to the current set.
 *   2. call module_init() again.
 *   3. display the new set.
 */
static void
reinit_modules(void)
{
        delete_load_module(ALL_MODULES);
        st->mods_installed = 0;
        st->flags &= ~MODULE_SYMS;
        free(st->ext_module_symtable);
        free(st->load_modules);
        st->ext_module_symtable = NULL;
        st->load_modules = NULL;
        kt->mods_installed = 0;
	clear_text_value_cache();

        module_init();
}


static char *
find_module_objfile(char *modref, char *filename, char *tree)
{
	char buf[BUFSIZE];
	char file[BUFSIZE];
	char dir[BUFSIZE];
	struct load_module *lm;
	char *retbuf;
	int initrd;
	struct syment *sp;
	char *p1, *p2;

	retbuf = NULL;
	initrd = FALSE;

	if (filename)
		strcpy(file, filename);
#ifdef MODULES_IN_CWD
       else {
                sprintf(file, "%s.o", modref);
                if (access(file, R_OK) == 0) {
                        retbuf = GETBUF(strlen(file)+1);
                        strcpy(retbuf, file);
                        if (CRASHDEBUG(1))
                            	fprintf(fp, 
				    "find_module_objfile: [%s] file in cwd\n",
                                	retbuf);
                        return retbuf;
                }
	}
#else
	else 
		sprintf(file, "%s.o", modref);
#endif

	/*
	 *  Later versions of insmod create a symbol at the module's base
	 *  address.  Examples:
         *
         * __insmod_sunrpc_O/lib/modules/2.2.17/misc/sunrpc.o_M3A7EE300_V131601 
         * __insmod_lockd_O/lib/modules/2.2.17/fs/lockd.o_M3A7EE300_V131601  
         * __insmod_nfsd_O/lib/modules/2.2.17/fs/nfsd.o_M3A7EE300_V131601  
         * __insmod_nfs_O/lib/modules/2.2.17/fs/nfs.o_M3A7EE300_V131601
	 */
	if ((st->flags & INSMOD_BUILTIN) && !filename) {
		sprintf(buf, "__insmod_%s_O/", modref);
		if (symbol_query(buf, NULL, &sp) == 1) {
			BZERO(buf, BUFSIZE);
			p1 = strstr(sp->name, "/");
			if ((p2 = strstr(sp->name, file)))
				p2 += strlen(file);
			if (p2) {
				strncpy(buf, p1, p2-p1); 	
                                if (!strstr(buf, "/lib/modules/")) {
					sprintf(dir, "/lib/%s.o", modref);
					if (STREQ(dir, buf))
						initrd = TRUE;
				} else if (REMOTE()) 
					strcpy(file, buf);
				else {
					retbuf = GETBUF(strlen(buf)+1);
					strcpy(retbuf, buf);
					if (CRASHDEBUG(1))
				    		fprintf(fp, 
					          "find_module_objfile: [%s]\n",
							retbuf);
					return retbuf;
				}
			}
		}
		if (is_module_name(modref, NULL, &lm) && 
		    (lm->mod_flags & MOD_INITRD)) {
			sprintf(dir, "/lib/%s.o", modref);
			initrd = TRUE;
		}
	}
        
	if (initrd) 
		error(NOTE, "%s: installed from initrd image\n", dir);

	if (REMOTE()) {
		retbuf = GETBUF(MAX_MOD_NAMELIST*2);

		if (!is_module_name(modref, NULL, &lm)) {
			error(INFO, "%s is not a module reference\n", modref);
			return NULL;
		}

        	if ((lm->mod_flags & MOD_LOAD_SYMS) &&
		    strlen(lm->mod_namelist)) {
			if (CRASHDEBUG(1))
				fprintf(fp, "redundant mod call: %s\n", 
					lm->mod_namelist);
			strcpy(retbuf, lm->mod_namelist);
			return retbuf;
		}

		if (find_remote_module_objfile(lm, file, retbuf))
			return retbuf;

		return NULL;
	}

	if (tree) {
		if (!(retbuf = search_directory_tree(tree, file))) {
			switch (kt->flags & (KMOD_V1|KMOD_V2))
			{
			case KMOD_V2:
				sprintf(file, "%s.ko", modref);
				retbuf = search_directory_tree(tree, file);
			}
		}
		return retbuf;
	}

	sprintf(dir, "%s/%s", DEFAULT_REDHAT_DEBUG_LOCATION, 
		kt->utsname.release);
	retbuf = search_directory_tree(dir, file);

	if (!retbuf) {
		sprintf(dir, "/lib/modules/%s", kt->utsname.release);
		if (!(retbuf = search_directory_tree(dir, file))) {
			switch (kt->flags & (KMOD_V1|KMOD_V2))
			{
			case KMOD_V2:
				sprintf(file, "%s.ko", modref);
				retbuf = search_directory_tree(dir, file);
			}
		}
	}

	return retbuf;
}


/*
 *  Unlink any temporary remote module object files.
 */
void
unlink_module(struct load_module *load_module)
{
	int i;
	struct load_module *lm;

	if (load_module) {
		if (load_module->mod_flags & MOD_REMOTE)
			unlink(load_module->mod_namelist);
		return;
	}

        for (i = 0; i < kt->mods_installed; i++) {
                lm = &st->load_modules[i];
		if (lm->mod_flags & MOD_REMOTE) 
			unlink(lm->mod_namelist);
        }
}


/*
 *  Dump the kernel log_buf in chronological order.
 */

void
cmd_log(void)
{
	int c;
	int msg_level;

	msg_level = FALSE;

        while ((c = getopt(argcnt, args, "m")) != EOF) {
                switch(c)
                {
                case 'm':
                        msg_level = TRUE;
                        break;
                default:
                        argerrs++;
                        break;
                }
        }

        if (argerrs)
                cmd_usage(pc->curcmd, SYNOPSIS);

	dump_log(msg_level);
}


void 
dump_log(int msg_level)
{
	int i;
	ulong log_buf, log_start, logged_chars;
	char *buf;
	char last;
	ulong index;
	struct syment *nsp;
	int log_wrap, loglevel, log_buf_len;

	if (symbol_exists("log_buf_len")) {
		get_symbol_data("log_buf_len", sizeof(int), &log_buf_len);
		get_symbol_data("log_buf", sizeof(ulong), &log_buf);
	} else {
		if ((ARRAY_LENGTH(log_buf) == 0) &&
	            (get_array_length("log_buf", NULL, 0) == 0)) {
	                if ((nsp = next_symbol("log_buf", NULL)) == NULL)
	                        error(FATAL, 
				    "cannot determine length of log_buf\n");
	                builtin_array_length("log_buf", 
				(int)(nsp->value - symbol_value("log_buf")), 
				NULL);
		}
	
		log_buf_len = ARRAY_LENGTH(log_buf);
		log_buf = symbol_value("log_buf");
	}

	buf = GETBUF(log_buf_len);
	log_wrap = FALSE;
	get_symbol_data("log_start", sizeof(ulong), &log_start);
	get_symbol_data("logged_chars", sizeof(ulong), &logged_chars);
        readmem(log_buf, KVADDR, buf,
        	log_buf_len, "log_buf contents", FAULT_ON_ERROR);

	log_start &= log_buf_len-1;
	index = (logged_chars < log_buf_len) ? 0 : log_start;

	if ((logged_chars < log_buf_len) && (index == 0) && (buf[index] == '<'))
		loglevel = TRUE;
	else
		loglevel = FALSE;

	if (index != 0)
		log_wrap = TRUE;

wrap_around:

	for (i = index; i < log_buf_len; i++) {
                if (loglevel && !msg_level) {
                        switch (buf[i])
                        {
                        case '>':
                                loglevel = FALSE;
                                /* FALLTHROUGH */
                        case '<':
                        case '0':
                        case '1':
                        case '2':
                        case '3':
                        case '4':
                        case '5':
                        case '6':
                        case '7':
                                continue;

                        default:
                                loglevel = FALSE;
                                break;
                        }
                }

		if (buf[i]) {
                	fputc(ascii(buf[i]) ? buf[i] : '.', fp);
                	loglevel = buf[i] == '\n' ? TRUE : FALSE;
                	last = buf[i];
		}
	}

	if (log_wrap) {
		log_buf_len = index;
		index = 0;
		log_wrap = FALSE;
		goto wrap_around;
	}

	if (last != '\n')
		fprintf(fp, "\n");

	FREEBUF(buf);
}


/*
 *  Display general system info.
 */
void
cmd_sys(void)
{
        int c, cnt;
	ulong sflag;

	sflag = FALSE;

        while ((c = getopt(argcnt, args, "cp:")) != EOF) {
                switch(c)
                {
		case 'p':
			if (STREQ(optarg, "anic"))
				panic_this_kernel();
			else
				argerrs++;
			break;

		case 'c':
			sflag = TRUE;
			break;

                default:
                        argerrs++;
                        break;
                }
        }

        if (argerrs)
                cmd_usage(pc->curcmd, SYNOPSIS);

        if (!args[optind]) {
		if (sflag)
			dump_sys_call_table(NULL, 0);
		else
			display_sys_stats();
		return;
	}

	cnt = 0;
        do {
                if (sflag)
                        dump_sys_call_table(args[optind], cnt++);
                else
                        cmd_usage(args[optind], COMPLETE_HELP);
                optind++;
        } while (args[optind]);
}

/*
 *  Display system stats at init-time or for the sys command.
 */
void
display_sys_stats(void)
{
        struct new_utsname *uts;
        char buf[BUFSIZE];
	ulong mhz;

	uts = &kt->utsname;

//	if (!(pc->flags & RUNTIME) && !DUMPFILE() && !GDB_PATCHED())
//		fprintf(fp, "\n");

        /*
         *  It's now safe to unlink the remote namelist.
         */
        if (pc->flags & UNLINK_NAMELIST) {
                unlink(pc->namelist);
                pc->flags &= ~UNLINK_NAMELIST;
                pc->flags |= NAMELIST_UNLINKED;
        }

	if (REMOTE()) {
		switch (pc->flags & 
			(NAMELIST_LOCAL|NAMELIST_UNLINKED|NAMELIST_SAVED))
		{
		case NAMELIST_UNLINKED:
			fprintf(fp, "      KERNEL: %s  (temporary)\n", 
				pc->namelist);
			break;

		case (NAMELIST_UNLINKED|NAMELIST_SAVED):
			fprintf(fp, "      KERNEL: %s\n", pc->namelist);
			break;

		case NAMELIST_LOCAL:
			fprintf(fp, "      KERNEL: %s\n", pc->namelist);
			break;
		}
	} else {
        	if (pc->system_map) {
                	fprintf(fp, "  SYSTEM MAP: %s\n", pc->system_map);
			fprintf(fp, "DEBUG KERNEL: %s %s\n", 
					pc->namelist,
					debug_kernel_version(pc->namelist));
		} else
			fprintf(fp, "      KERNEL: %s\n", pc->namelist);
	}

	if (pc->debuginfo_file)
		fprintf(fp, "   DEBUGINFO: %s\n", pc->debuginfo_file);
	else if (pc->namelist_debug)
		fprintf(fp, "DEBUG KERNEL: %s %s\n", pc->namelist_debug,
			debug_kernel_version(pc->namelist_debug));

	fprintf(fp, "    DUMPFILE: ");
        if (ACTIVE()) {
		if (REMOTE_ACTIVE()) 
			fprintf(fp, "%s@%s  (remote live system)\n",
			    	pc->server_memsrc, pc->server);
		else
                	fprintf(fp, "%s\n", pc->live_memsrc);
	} else {
		if (REMOTE_DUMPFILE())
                	fprintf(fp, "%s@%s  (remote dumpfile)\n", 
				pc->server_memsrc, pc->server);
		else
                	fprintf(fp, "%s\n", pc->dumpfile);
	}

        fprintf(fp, "        CPUS: %d\n", kt->cpus);
	if (ACTIVE())
        	get_symbol_data("xtime", sizeof(struct timespec), &kt->date);
        fprintf(fp, "        DATE: %s\n", 
		strip_linefeeds(ctime(&kt->date.tv_sec))); 
        fprintf(fp, "      UPTIME: %s\n", get_uptime(buf)); 
        fprintf(fp, "LOAD AVERAGE: %s\n", get_loadavg(buf)); 
	fprintf(fp, "       TASKS: %ld\n", RUNNING_TASKS());
	fprintf(fp, "    NODENAME: %s\n", uts->nodename); 
        fprintf(fp, "     RELEASE: %s\n", uts->release); 
	fprintf(fp, "     VERSION: %s\n", uts->version); 
	fprintf(fp, "     MACHINE: %s  ", uts->machine);
	if ((mhz = machdep->processor_speed()))
		fprintf(fp, "(%ld Mhz)\n", mhz);
	else
		fprintf(fp, "(unknown Mhz)\n");
	fprintf(fp, "      MEMORY: %s\n", get_memory_size(buf));
#ifdef WHO_CARES
	fprintf(fp, "  DOMAINNAME: %s\n", uts->domainname);
#endif
	if (DUMPFILE()) {
		fprintf(fp, "       PANIC: ");
		if (machdep->flags & HWRESET)
			fprintf(fp, "HARDWARE RESET\n");
		else {
        		strip_linefeeds(get_panicmsg(buf));
			fprintf(fp, "\"%s\"%s\n", buf, 
				strstr(buf, "Oops: ") ? 
				" (check log for details)" : "");
		}
	}
}

/*
 *  Get the kernel version from the debug kernel and store it here.
 */
static char *debug_kernel_version_string = NULL;

static char *
debug_kernel_version(char *namelist)
{
	FILE *pipe;
	int found, argc;
	char buf[BUFSIZE], *ptr;
	char command[BUFSIZE];
	char *arglist[MAXARGS];

	if (debug_kernel_version_string)
		return debug_kernel_version_string;

        sprintf(command, "/usr/bin/strings %s", namelist);

        if ((pipe = popen(command, "r")) == NULL) { 
		debug_kernel_version_string = " ";
                return debug_kernel_version_string;
	}

	argc = 0;
	ptr = NULL;
        found = FALSE;
        while (fgets(buf, BUFSIZE-1, pipe)) {
                if (!strstr(buf, "Linux version 2."))
                        continue;

		argc = parse_line(buf, arglist); 
		break;
        }
        pclose(pipe);

	if ((argc >= 3) && (debug_kernel_version_string = (char *) 
	    malloc(strlen(arglist[2])+3)))
		sprintf(debug_kernel_version_string, "(%s)", arglist[2]);
	else
		debug_kernel_version_string = " ";

	return debug_kernel_version_string;
}

/*
 *  Calculate and return the uptime.
 */

static char *
get_uptime(char *buf)
{
	ulong jiffies; 

	get_symbol_data("jiffies", sizeof(long), &jiffies);
	convert_time((ulonglong)jiffies, buf);

	return buf;
}


#define FSHIFT          11              /* nr of bits of precision */
#define FIXED_1 (1<<FSHIFT)
#define LOAD_INT(x) ((x) >> FSHIFT)
#define LOAD_FRAC(x) LOAD_INT(((x) & (FIXED_1-1)) * 100)

static char *
get_loadavg(char *buf)
{
        int a, b, c;
	long avenrun[3];

        readmem(symbol_value("avenrun"), KVADDR, &avenrun[0],
                sizeof(long)*3, "avenrun array", FAULT_ON_ERROR);

        a = avenrun[0] + (FIXED_1/200);
        b = avenrun[1] + (FIXED_1/200);
        c = avenrun[2] + (FIXED_1/200);
        sprintf(buf, "%d.%02d, %d.%02d, %d.%02d",
                LOAD_INT(a), LOAD_FRAC(a),
                LOAD_INT(b), LOAD_FRAC(b),
                LOAD_INT(c), LOAD_FRAC(c));

	return buf;
}



/*
 *  Determine whether a string or value equates to a system call name or value.
 */
int
is_system_call(char *name, ulong value)
{
	int i;
        ulong *sys_call_table, *sct;
	char *sp;
        long size;
	int NR_syscalls;

	NR_syscalls = get_NR_syscalls();
        size = sizeof(void *) * NR_syscalls;
        sys_call_table = (ulong *)GETBUF(size);

        readmem(symbol_value("sys_call_table"), KVADDR, sys_call_table,
                size, "sys_call_table", FAULT_ON_ERROR);

	for (i = 0, sct = sys_call_table; i < NR_syscalls; i++, sct++) {
		if (name && (sp = value_symbol(*sct))) {
			if (STREQ(name, sp))
				return TRUE;
		} else if (value) {
			if (value == *sct)
				return TRUE;
		}
	}

        return FALSE;
}

char *sys_call_hdr = "NUM  SYSTEM CALL                FILE AND LINE NUMBER\n";

static void
dump_sys_call_table(char *spec, int cnt)
{
        int i;
        ulong *sys_call_table, *sct, addr;
        char buf1[BUFSIZE], *scp;
        char buf2[BUFSIZE], *p;
	char buf3[BUFSIZE];
	char *arglist[MAXARGS];
	int argc, NR_syscalls;
	int number, printit, hdr_printed;
	struct syment *sp, *spn;
        long size;

	if (GDB_PATCHED())
		error(INFO, "line numbers are not available\n"); 

	NR_syscalls = get_NR_syscalls();
	if (CRASHDEBUG(1))
		fprintf(fp, "NR_syscalls: %d\n", NR_syscalls);
        size = sizeof(void *) * NR_syscalls;
        sys_call_table = (ulong *)GETBUF(size);

        readmem(symbol_value("sys_call_table"), KVADDR, sys_call_table,
                size, "sys_call_table", FAULT_ON_ERROR);

	if (spec)
		open_tmpfile();

	fprintf(fp, sys_call_hdr);

        for (i = 0, sct = sys_call_table; i < NR_syscalls; i++, sct++) {
                if (!(scp = value_symbol(*sct))) {
			if (CRASHDEBUG(1)) {
				fprintf(fp, (output_radix == 16) ? 
					"%3x  " : "%3d  ", i);
				fprintf(fp, 
			    	    "invalid sys_call_table entry: %lx (%s)\n", 
					*sct, value_to_symstr(*sct, buf1, 0));
			}
			continue;
		}
		
		fprintf(fp, (output_radix == 16) ? "%3x  " : "%3d  ", i);
		fprintf(fp, "%-26s ", scp);

		/*
		 *  For system call symbols whose first instruction is
		 *  an inline from a header file, the file/line-number is 
		 *  confusing.  For this command only, look for the first
	 	 *  instruction address in the system call that shows the
		 *  the actual source file containing the system call.
	  	 */
                sp = value_search(*sct, NULL);
                spn = next_symbol(NULL, sp);
		get_build_directory(buf2);

		for (addr = *sct; sp && spn && (addr < spn->value); addr++) {
			BZERO(buf1, BUFSIZE);
			get_line_number(addr, buf1, FALSE);

			if (strstr(buf1, ".h: ") && strstr(buf1, "include/")) 
				continue;

			if (strstr(buf1, buf2)) {
                                p = buf1 + strlen(buf2);
                                fprintf(fp, "%s%s",
                                        strlen(buf1) ? ".." : "", p);
                                break;
                        }
		}

		fprintf(fp, "\n");
       	}

        if (spec) {
                rewind(pc->tmpfile);

		hdr_printed = cnt;
		if ((number = IS_A_NUMBER(spec))) 
                	sprintf(buf3, (output_radix == 16) ?  "%lx" : "%ld",
                        	stol(spec, FAULT_ON_ERROR, NULL));

                while (fgets(buf1, BUFSIZE, pc->tmpfile)) {
			printit = FALSE;
			strcpy(buf2, buf1);
			argc = parse_line(buf2, arglist);
			if (argc < 2)
				continue;

			if (number && STREQ(arglist[0], buf3))
				printit = TRUE;
			else if (!number && strstr(arglist[1], spec))
				printit = TRUE;

			if (printit) {
				fprintf(pc->saved_fp, "%s%s", hdr_printed++ ? 
					"" : sys_call_hdr, buf1);
				if (number)
					break;
			}
                }

                close_tmpfile();
        }
}

/*
 *  Get the number of system calls in the sys_call_table based upon the
 *  next symbol after it. 
 */
static int
get_NR_syscalls(void)
{
       	ulong sys_call_table;
	struct syment *sp;
	int cnt;

	sys_call_table = symbol_value("sys_call_table");
	if (!(sp = next_symbol("sys_call_table", NULL)))
		return 256;

        while (sp->value == sys_call_table) {
                if (!(sp = next_symbol(sp->name, NULL)))
                        return 256;
        }

	cnt = (sp->value - sys_call_table)/sizeof(void *);

	return cnt;
}

/*
 *  "help -k" output
 */
void
dump_kernel_table(void)
{
	int i;
        struct new_utsname *uts;
        int others;

        others = 0;
        uts = &kt->utsname;

        fprintf(fp, "         flags: %lx  (", kt->flags);
	if (kt->flags & NO_MODULE_ACCESS)
		fprintf(fp, "%sNO_MODULE_ACCESS", others++ ? "|" : "");
	if (kt->flags & TVEC_BASES_V1)
		fprintf(fp, "%sTVEC_BASES_V1", others++ ? "|" : "");
	if (kt->flags & TVEC_BASES_V2)
		fprintf(fp, "%sTVEC_BASES_V2", others++ ? "|" : "");
	if (kt->flags & GCC_2_96)
		fprintf(fp, "%sGCC_2_96", others++ ? "|" : "");
	if (kt->flags & GCC_3_2)
		fprintf(fp, "%sGCC_3_2", others++ ? "|" : "");
	if (kt->flags & GCC_3_2_3)
		fprintf(fp, "%sGCC_3_2_3", others++ ? "|" : "");
	if (kt->flags & GCC_3_3_2)
		fprintf(fp, "%sGCC_3_3_2", others++ ? "|" : "");
	if (kt->flags & GCC_3_3_3)
		fprintf(fp, "%sGCC_3_3_3", others++ ? "|" : "");
	if (kt->flags & RA_SEEK)
		fprintf(fp, "%sRA_SEEK", others++ ? "|" : "");
	if (kt->flags & NO_RA_SEEK)
		fprintf(fp, "%sNO_RA_SEEK", others++ ? "|" : "");
	if (kt->flags & KALLSYMS_V1)
		fprintf(fp, "%sKALLSYMS_V1", others++ ? "|" : "");
	if (kt->flags & NO_KALLSYMS)
		fprintf(fp, "%sNO_KALLSYMS", others++ ? "|" : "");
	if (kt->flags & PER_CPU_OFF)
		fprintf(fp, "%sPER_CPU_OFF", others++ ? "|" : "");
	if (kt->flags & SMP)
		fprintf(fp, "%sSMP", others++ ? "|" : "");
	if (kt->flags & KMOD_V1)
		fprintf(fp, "%sKMOD_V1", others++ ? "|" : "");
	if (kt->flags & KMOD_V2)
		fprintf(fp, "%sKMOD_V2", others++ ? "|" : "");
	if (kt->flags & KALLSYMS_V2)
		fprintf(fp, "%sKALLSYMS_V2", others++ ? "|" : "");
	fprintf(fp, ")\n");
        fprintf(fp, "         stext: %lx\n", kt->stext);
        fprintf(fp, "         etext: %lx\n", kt->etext);
        fprintf(fp, "    stext_init: %lx\n", kt->stext_init);
        fprintf(fp, "    etext_init: %lx\n", kt->etext_init);
        fprintf(fp, "    init_begin: %lx\n", kt->init_begin);
        fprintf(fp, "      init_end: %lx\n", kt->init_end);
        fprintf(fp, "           end: %lx\n", kt->end);
        fprintf(fp, "          cpus: %d\n", kt->cpus);
        fprintf(fp, "       NR_CPUS: %d (compiled-in to this version of %s)\n",
		NR_CPUS, pc->program_name); 
	if (kt->display_bh == display_bh_1)
        	fprintf(fp, "    display_bh: display_bh_1()\n");
	else if (kt->display_bh == display_bh_2)
        	fprintf(fp, "    display_bh: display_bh_2()\n");
	else if (kt->display_bh == display_bh_3)
        	fprintf(fp, "    display_bh: display_bh_3()\n");
	else
        	fprintf(fp, "    display_bh: %lx\n", (ulong)kt->display_bh);
        fprintf(fp, "   module_list: %lx\n", kt->module_list);
        fprintf(fp, " kernel_module: %lx\n", kt->kernel_module);
	fprintf(fp, "mods_installed: %d\n", kt->mods_installed);
	if (!(pc->flags & KERNEL_DEBUG_QUERY) && ACTIVE()) 
                get_symbol_data("xtime", sizeof(struct timespec), &kt->date);
        fprintf(fp, "          date: %s\n",
                strip_linefeeds(ctime(&kt->date.tv_sec)));
        fprintf(fp, "  proc_version: %s\n", strip_linefeeds(kt->proc_version));
        fprintf(fp, "   new_utsname: \n");
        fprintf(fp, "      .sysname: %s\n", uts->sysname);
        fprintf(fp, "     .nodename: %s\n", uts->nodename);
        fprintf(fp, "      .release: %s\n", uts->release);
        fprintf(fp, "      .version: %s\n", uts->version);
        fprintf(fp, "      .machine: %s\n", uts->machine);
        fprintf(fp, "   .domainname: %s\n", uts->domainname);
	fprintf(fp, "kernel_version: %d.%d.%d\n", kt->kernel_version[0], 
		kt->kernel_version[1], kt->kernel_version[2]);
	fprintf(fp, "   gcc_version: %d.%d.%d\n", kt->gcc_version[0], 
		kt->gcc_version[1], kt->gcc_version[2]);
	fprintf(fp, " runq_siblings: %d\n", kt->runq_siblings);
	fprintf(fp, "  __rq_idx[NR_CPUS]: ");
	for (i = 0; i < NR_CPUS; i++) 
		fprintf(fp, "%ld ", kt->__rq_idx[i]);
	fprintf(fp, "\n __cpu_idx[NR_CPUS]: ");
	for (i = 0; i < NR_CPUS; i++) 
		fprintf(fp, "%ld ", kt->__cpu_idx[i]);
	fprintf(fp, "\n __per_cpu_offset[NR_CPUS]:");
	for (i = 0; i < NR_CPUS; i++) 
		fprintf(fp, "%s%.*lx ", (i % 4) == 0 ? "\n    " : "",
			LONG_PRLEN, kt->__per_cpu_offset[i]);
	fprintf(fp, "\n cpu_flags[NR_CPUS]:");
	for (i = 0; i < NR_CPUS; i++) 
		fprintf(fp, "%lx ", kt->cpu_flags[i]);
	fprintf(fp, "\n");
}

/*
 *  Set the context to the active task on a given cpu -- dumpfiles only.
 */
void
set_cpu(int cpu)
{
	ulong task;

	if (cpu >= kt->cpus)
		error(FATAL, "invalid cpu number: system has only %d cpu%s\n", 
			kt->cpus, kt->cpus > 1 ? "s" : "");
	if ((task = get_active_task(cpu))) 
		set_context(task, NO_PID);
	else
		error(FATAL, "cannot determine active task on cpu %ld\n", cpu);

	show_context(CURRENT_CONTEXT());
}


/*
 *  Collect the irq_desc[] entry along with its associated handler and
 *  action structures.
 */

void
cmd_irq(void)
{
        int i, c;
	int nr_irqs;

        while ((c = getopt(argcnt, args, "db")) != EOF) {
                switch(c)
                {
		case 'd':
			display_idt_table();
			return;

		case 'b':
			if (!kt->display_bh) {
			        if (symbol_exists("bh_base") &&
			            symbol_exists("bh_mask") &&
			            symbol_exists("bh_active"))
			                kt->display_bh = display_bh_1;
			        else if (symbol_exists("bh_base") &&
			            symbol_exists("softirq_state") &&
			            symbol_exists("softirq_vec"))
			                kt->display_bh = display_bh_2;
			        else if (symbol_exists("bh_base") &&
			            symbol_exists("irq_stat") &&
			            symbol_exists("softirq_vec") &&
				    VALID_MEMBER(irq_cpustat_t___softirq_active)
                        	    && VALID_MEMBER(irq_cpustat_t___softirq_mask))
			                kt->display_bh = display_bh_3;
				else
					error(FATAL, 
					    "bottom-half option not supported\n");
			}
			kt->display_bh();
			return;

                default:
                        argerrs++;
                        break;
                }
        }

        if (argerrs)
                cmd_usage(pc->curcmd, SYNOPSIS);

	if ((nr_irqs = machdep->nr_irqs) == 0)
		error(FATAL, "cannot determine number of IRQs\n");

	if (!args[optind]) {
		for (i = 0; i < nr_irqs; i++)
			machdep->dump_irq(i);
		return;
	}

	while (args[optind]) {
		i = dtoi(args[optind], FAULT_ON_ERROR, NULL);
		if (i >= nr_irqs)
			error(FATAL, "invalid IRQ value: %d  (%d max)\n", 
				i, nr_irqs-1);
		machdep->dump_irq(i);
		optind++;
	}
}


/*
 *  Do the work for cmd_irq().
 */
void
generic_dump_irq(int irq)
{
	struct datatype_member datatype_member, *dm;
	ulong irq_desc_addr;
	long len;
	char buf[BUFSIZE];
	int status, depth, others;
	ulong handler, action, value;
	ulong tmp1, tmp2;

	dm = &datatype_member;
	
	if (!VALID_STRUCT(irq_desc_t))
		error(FATAL, "cannot determine size of irq_desc_t\n");
	len = SIZE(irq_desc_t);

        if (symbol_exists("irq_desc"))
		irq_desc_addr = symbol_value("irq_desc") + (len * irq);
        else if (symbol_exists("_irq_desc"))
		irq_desc_addr = symbol_value("_irq_desc") + (len * irq);
	else
		error(FATAL, "neither irq_desc nor _irq_desc symbols exist\n");

        readmem(irq_desc_addr + OFFSET(irq_desc_t_status), KVADDR, &status,
                sizeof(int), "irq_desc entry", FAULT_ON_ERROR);
        readmem(irq_desc_addr + OFFSET(irq_desc_t_handler), KVADDR, &handler,
                sizeof(long), "irq_desc entry", FAULT_ON_ERROR);
        readmem(irq_desc_addr + OFFSET(irq_desc_t_action), KVADDR, &action,
                sizeof(long), "irq_desc entry", FAULT_ON_ERROR);
        readmem(irq_desc_addr + OFFSET(irq_desc_t_depth), KVADDR, &depth,
                sizeof(int), "irq_desc entry", FAULT_ON_ERROR);

	fprintf(fp, "    IRQ: %d\n", irq);
	fprintf(fp, " STATUS: %x %s", status, status ? "(" : "");
	others = 0;
	if (status & IRQ_INPROGRESS) {
		fprintf(fp, "IRQ_INPROGRESS");
		others++;
	}
	if (status & IRQ_DISABLED)
		fprintf(fp, "%sIRQ_DISABLED", others++ ? "|" : "");
        if (status & IRQ_PENDING)
                fprintf(fp, "%sIRQ_PENDING", others++ ? "|" : "");
        if (status & IRQ_REPLAY)
                fprintf(fp, "%sIRQ_REPLAY", others++ ? "|" : "");
        if (status & IRQ_AUTODETECT)
                fprintf(fp, "%sIRQ_AUTODETECT", others++ ? "|" : "");
        if (status & IRQ_WAITING)
                fprintf(fp, "%sIRQ_WAITING", others++ ? "|" : "");
        if (status & IRQ_LEVEL)
                fprintf(fp, "%sIRQ_LEVEL", others++ ? "|" : "");
        if (status & IRQ_MASKED)
                fprintf(fp, "%sIRQ_MASKED", others++ ? "|" : "");
	fprintf(fp, "%s\n", status ? ")" : "");

	fprintf(fp, "HANDLER: ");
	if (value_symbol(handler)) {
		fprintf(fp, "%lx  ", handler);
		pad_line(fp, VADDR_PRLEN == 8 ? 
			VADDR_PRLEN+2 : VADDR_PRLEN-6, ' ');
		fprintf(fp, "<%s>\n", value_symbol(handler));
	} else
		fprintf(fp, "%lx\n", handler);

	if (handler) { 
        	readmem(handler+OFFSET(hw_interrupt_type_typename), KVADDR, 
			&tmp1, sizeof(void *),
                	"hw_interrupt_type typename", FAULT_ON_ERROR);
	 	fprintf(fp, "         typename: %lx  ", tmp1);
		BZERO(buf, BUFSIZE);
        	if (read_string(tmp1, buf, BUFSIZE-1))
			fprintf(fp, "\"%s\"", buf);
		fprintf(fp, "\n");

		readmem(handler+OFFSET(hw_interrupt_type_startup), KVADDR,
			&tmp1, sizeof(void *),
			"hw_interrupt_type startup", FAULT_ON_ERROR);
		fprintf(fp, "          startup: %lx  ", tmp1); 
		if (is_kernel_text(tmp1)) 
			fprintf(fp, "<%s>", value_to_symstr(tmp1, buf, 0));
		else if (readmem(tmp1, KVADDR, &tmp2,
                	sizeof(ulong), "startup indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                            	fprintf(fp, "<%s>",
                                	value_to_symstr(tmp2, buf, 0));
		fprintf(fp, "\n");

                readmem(handler+OFFSET(hw_interrupt_type_shutdown), KVADDR,
                        &tmp1, sizeof(void *),
                        "hw_interrupt_type shutdown", FAULT_ON_ERROR);
                fprintf(fp, "         shutdown: %lx  ", tmp1);
                if (is_kernel_text(tmp1))
                        fprintf(fp, "<%s>", value_to_symstr(tmp1, buf, 0));
                else if (readmem(tmp1, KVADDR, &tmp2,
                        sizeof(ulong), "shutdown indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                fprintf(fp, "<%s>",
                                        value_to_symstr(tmp2, buf, 0));
                fprintf(fp, "\n");

		if (VALID_MEMBER(hw_interrupt_type_handle)) {
	                readmem(handler+OFFSET(hw_interrupt_type_handle), 
				KVADDR,
	                        &tmp1, sizeof(void *),
	                        "hw_interrupt_type handle", FAULT_ON_ERROR);
	                fprintf(fp, "           handle: %lx  ", tmp1);
	                if (is_kernel_text(tmp1))
	                        fprintf(fp, "<%s>", 
					value_to_symstr(tmp1, buf, 0));
	                else if (readmem(tmp1, KVADDR, &tmp2,
	                        sizeof(ulong), "handle indirection",
	                        RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
	                                fprintf(fp, "<%s>",
	                                        value_to_symstr(tmp2, buf, 0));
	                fprintf(fp, "\n");
		}

                readmem(handler+OFFSET(hw_interrupt_type_enable), KVADDR,
                        &tmp1, sizeof(void *),
                        "hw_interrupt_type enable", FAULT_ON_ERROR);
                fprintf(fp, "           enable: %lx  ", tmp1);
                if (is_kernel_text(tmp1))
                        fprintf(fp, "<%s>", value_to_symstr(tmp1, buf, 0));
                else if (readmem(tmp1, KVADDR, &tmp2,
                        sizeof(ulong), "enable indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                fprintf(fp, "<%s>",
                                        value_to_symstr(tmp2, buf, 0));
                fprintf(fp, "\n");

                readmem(handler+OFFSET(hw_interrupt_type_disable), KVADDR,
                        &tmp1, sizeof(void *),
                        "hw_interrupt_type disable", FAULT_ON_ERROR);
                fprintf(fp, "          disable: %lx  ", tmp1);
                if (is_kernel_text(tmp1))
                        fprintf(fp, "<%s>", value_to_symstr(tmp1, buf, 0));
                else if (readmem(tmp1, KVADDR, &tmp2,
                        sizeof(ulong), "disable indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                fprintf(fp, "<%s>",
                                        value_to_symstr(tmp2, buf, 0));
                fprintf(fp, "\n");

		if (VALID_MEMBER(hw_interrupt_type_ack)) {
                	readmem(handler+OFFSET(hw_interrupt_type_ack), KVADDR,
                        	&tmp1, sizeof(void *),
                        	"hw_interrupt_type ack", FAULT_ON_ERROR);
                	fprintf(fp, "              ack: %lx  ", tmp1);
                	if (is_kernel_text(tmp1))
                        	fprintf(fp, "<%s>", 
					value_to_symstr(tmp1, buf, 0));
                	else if (readmem(tmp1, KVADDR, &tmp2,
                        	sizeof(ulong), "ack indirection",
                        	RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                	fprintf(fp, "<%s>",
                                        	value_to_symstr(tmp2, buf, 0));
                	fprintf(fp, "\n");
		}

		if (VALID_MEMBER(hw_interrupt_type_end)) {
                	readmem(handler+OFFSET(hw_interrupt_type_end), KVADDR,
                        	&tmp1, sizeof(void *),
                        	"hw_interrupt_type end", FAULT_ON_ERROR);
                        fprintf(fp, "              end: %lx  ", tmp1);
                        if (is_kernel_text(tmp1))
                                fprintf(fp, "<%s>", 
                                        value_to_symstr(tmp1, buf, 0));
                        else if (readmem(tmp1, KVADDR, &tmp2,
                                sizeof(ulong), "end indirection",
                                RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                        fprintf(fp, "<%s>",
                                                value_to_symstr(tmp2, buf, 0));
                        fprintf(fp, "\n");
		}

		if (VALID_MEMBER(hw_interrupt_type_set_affinity)) {
                	readmem(handler+OFFSET(hw_interrupt_type_set_affinity),
				KVADDR, &tmp1, sizeof(void *),
                        	"hw_interrupt_type set_affinity", 
				FAULT_ON_ERROR);
                        fprintf(fp, "     set_affinity: %lx  ", tmp1);
                        if (is_kernel_text(tmp1))
                                fprintf(fp, "<%s>", 
                                        value_to_symstr(tmp1, buf, 0));
                        else if (readmem(tmp1, KVADDR, &tmp2,
                                sizeof(ulong), "set_affinity indirection",
                                RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                        fprintf(fp, "<%s>",
                                                value_to_symstr(tmp2, buf, 0));
                        fprintf(fp, "\n");
		}
	}

do_linked_action:

	fprintf(fp, " ACTION: ");
        if (value_symbol(action)) {
                fprintf(fp, "%lx  ", action);
                pad_line(fp, VADDR_PRLEN == 8 ? 
			VADDR_PRLEN+2 : VADDR_PRLEN-6, ' ');
                fprintf(fp, "<%s>\n", value_symbol(action));
        } else if (action)
                fprintf(fp, "%lx\n", action);
	else
		fprintf(fp, "(none)\n");


	if (action) {
                readmem(action+OFFSET(irqaction_handler), KVADDR,
                        &tmp1, sizeof(void *),
                        "irqaction handler", FAULT_ON_ERROR);
		fprintf(fp, "          handler: %lx  ", tmp1);
                if (is_kernel_text(tmp1))
                        fprintf(fp, "<%s>", value_to_symstr(tmp1, buf, 0));
                else if (readmem(tmp1, KVADDR, &tmp2,
                        sizeof(ulong), "handler indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(tmp2))
                                fprintf(fp, "<%s>",
                                        value_to_symstr(tmp2, buf, 0));
                fprintf(fp, "\n");

                readmem(action+OFFSET(irqaction_flags), KVADDR,
                        &value, sizeof(void *),
                        "irqaction flags", FAULT_ON_ERROR);
                fprintf(fp, "            flags: %lx  ", value);
                if (value) {
                        others = 0;
                        fprintf(fp, "(");
				
                        if (value & SA_INTERRUPT)
                                fprintf(fp, "%sSA_INTERRUPT",
                                                        others++ ? "|" : "");
                        if (value & SA_PROBE)
                                fprintf(fp, "%sSA_PROBE",
                                                        others++ ? "|" : "");
                        if (value & SA_SAMPLE_RANDOM)
                                fprintf(fp, "%sSA_SAMPLE_RANDOM",
                                                        others++ ? "|" : "");
                        if (value & SA_SHIRQ)
                                fprintf(fp, "%sSA_SHIRQ", others++ ? "|" : "");

                        fprintf(fp, ")");
                        if (value & ~ACTION_FLAGS) {
                                fprintf(fp, "  (bits %lx not translated)",
                                        	value & ~ACTION_FLAGS);
                        }
                }
		fprintf(fp, "\n");

                readmem(action+OFFSET(irqaction_mask), KVADDR,
                        &tmp1, sizeof(void *),
                        "irqaction mask", FAULT_ON_ERROR);
                fprintf(fp, "             mask: %lx\n", tmp1);

                readmem(action+OFFSET(irqaction_name), KVADDR,
                        &tmp1, sizeof(void *),
                        "irqaction name", FAULT_ON_ERROR);
                fprintf(fp, "             name: %lx  ", tmp1);
                BZERO(buf, BUFSIZE);
                if (read_string(tmp1, buf, BUFSIZE-1))
                        fprintf(fp, "\"%s\"", buf);
                fprintf(fp, "\n");

                readmem(action+OFFSET(irqaction_dev_id), KVADDR,
                        &tmp1, sizeof(void *),
                        "irqaction dev_id", FAULT_ON_ERROR);
                fprintf(fp, "           dev_id: %lx\n", tmp1);

                readmem(action+OFFSET(irqaction_next), KVADDR,
                        &action, sizeof(void *),
                        "irqaction dev_id", FAULT_ON_ERROR);
                fprintf(fp, "             next: %lx\n", action);
	}

	if (action) 
		goto do_linked_action;

	fprintf(fp, "  DEPTH: %d\n\n", depth);
}

/*
 *  Dump the earlier 2.2 Linux version's bottom-half essentials.
 */
static void
display_bh_1(void)
{
        int i;
        ulong bh_mask, bh_active;
        ulong bh_base[32];
        char buf[BUFSIZE];

        get_symbol_data("bh_mask", sizeof(ulong), &bh_mask);
        get_symbol_data("bh_active", sizeof(ulong), &bh_active);
        readmem(symbol_value("bh_base"), KVADDR, bh_base, sizeof(void *) * 32,
                "bh_base[32]", FAULT_ON_ERROR);

        fprintf(fp, "BH_MASK   BH_ACTIVE\n");
        fprintf(fp, "%08lx  %08lx\n", bh_mask, bh_active);
        fprintf(fp, "\nBH_BASE   %s\n",
                mkstring(buf, VADDR_PRLEN, CENTER|LJUST, "FUNCTION"));
        for (i = 0; i < 32; i++) {
                if (!bh_base[i])
                        continue;
                fprintf(fp, "  %2d      %lx  <%s>\n", i, bh_base[i],
                        value_to_symstr(bh_base[i], buf, 0));
        }
}

/*
 *  Dump the 2.3-ish Linux version's bottom half essentials.  
 */
static void 
display_bh_2(void)
{
	int i;
        ulong bh_base[32];
	struct softirq_state {
        	uint32_t active;
        	uint32_t mask;
	} softirq_state; 
	struct softirq_action {
	        void    *action;
        	void    *data;
	} softirq_vec[32];
	char buf[BUFSIZE];

        readmem(symbol_value("bh_base"), KVADDR, bh_base, sizeof(void *) * 32,
                "bh_base[32]", FAULT_ON_ERROR);

        readmem(symbol_value("softirq_vec"), KVADDR, softirq_vec, 
		sizeof(struct softirq_action) * 32,
                "softirq_vec[32]", FAULT_ON_ERROR);

	fprintf(fp, "CPU    MASK     ACTIVE\n");
	
	for (i = 0; i < kt->cpus; i++) {
		readmem(symbol_value("softirq_state") + 
			(i * SIZE(softirq_state)), KVADDR,
			&softirq_state, sizeof(struct softirq_state),
			"softirq_state", FAULT_ON_ERROR);

		fprintf(fp, " %-2d  %08x  %08x\n", 
			i, softirq_state.mask,
			softirq_state.active);
	}

	fprintf(fp, "\nVEC  %s\n", 
		mkstring(buf, VADDR_PRLEN, CENTER|LJUST, "ACTION"));

	for (i = 0; i < 32; i++) {
		if (!softirq_vec[i].action)
			continue;

		fprintf(fp, " %-2d  %lx  <%s>\n", i, 
			(ulong)softirq_vec[i].action,
			value_to_symstr((ulong)softirq_vec[i].action, buf, 0));
	}

        fprintf(fp, "\nBH_BASE   %s\n", 
		mkstring(buf, VADDR_PRLEN, CENTER|LJUST, "FUNCTION"));
        for (i = 0; i < 32; i++) {
                if (!bh_base[i])
                        continue;
                fprintf(fp, "  %2d      %lx  <%s>\n", i, bh_base[i],
                        value_to_symstr(bh_base[i], buf, 0));
        }

}

/*
 *  Dump the 2.4 Linux version's bottom half essentials.  
 */
static void 
display_bh_3(void)
{
	int i;
        ulong bh_base[32];
	struct softirq_action {
	        void    *action;
        	void    *data;
	} softirq_vec[32];
	char buf[BUFSIZE];
	uint active, mask;
	ulong function;

        readmem(symbol_value("bh_base"), KVADDR, bh_base, sizeof(void *) * 32,
                "bh_base[32]", FAULT_ON_ERROR);

        readmem(symbol_value("softirq_vec"), KVADDR, softirq_vec, 
		sizeof(struct softirq_action) * 32,
                "softirq_vec[32]", FAULT_ON_ERROR);

	fprintf(fp, "CPU    MASK     ACTIVE\n");
	
	for (i = 0; i < kt->cpus; i++) {
		readmem(symbol_value("irq_stat") + 
			(i * SIZE(irq_cpustat_t)) +
			OFFSET(irq_cpustat_t___softirq_active), KVADDR,
			&active, sizeof(uint),
			"__softirq_active", FAULT_ON_ERROR);

                readmem(symbol_value("irq_stat") +
                        (i * SIZE(irq_cpustat_t)) +
                        OFFSET(irq_cpustat_t___softirq_mask), KVADDR,
                        &mask, sizeof(uint),
                        "__softirq_mask", FAULT_ON_ERROR);

		fprintf(fp, " %-2d  %08x  %08x\n", i, mask, active);
	}

	fprintf(fp, "\nVEC  %s\n", 
		mkstring(buf, VADDR_PRLEN, CENTER|LJUST, "ACTION"));

	for (i = 0; i < 32; i++) {
		if (!softirq_vec[i].action)
			continue;

		fprintf(fp, " %-2d  %lx  ", i, (ulong)softirq_vec[i].action);
		if (is_kernel_text((ulong)softirq_vec[i].action))
			fprintf(fp, "<%s>",
			    	value_to_symstr((ulong)softirq_vec[i].action, 
			    	buf, 0));
                else if (readmem((ulong)softirq_vec[i].action, KVADDR, 
			&function, sizeof(ulong), "action indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(function))
                                fprintf(fp, "<%s>",
                                        value_to_symstr(function, buf, 0));
		fprintf(fp, "\n");
	}

        fprintf(fp, "\nBH_BASE   %s\n", 
		mkstring(buf, VADDR_PRLEN, CENTER|LJUST, "FUNCTION"));
        for (i = 0; i < 32; i++) {
                if (!bh_base[i])
                        continue;
                fprintf(fp, "  %2d      %lx  ", i, bh_base[i]);
		if (is_kernel_text(bh_base[i]))
			fprintf(fp, "<%s>", 
				value_to_symstr(bh_base[i], buf, 0));
                else if (readmem(bh_base[i], KVADDR, &function,
                        sizeof(ulong), "bh_base indirection",
                        RETURN_ON_ERROR|QUIET) && is_kernel_text(function))
                                fprintf(fp, "<%s>",
                                        value_to_symstr(function, buf, 0));
		fprintf(fp, "\n");
        }

}

/*
 *  Dump the entries in the old- and new-style timer queues in
 *  chronological order.
 */
void
cmd_timer(void)
{
        int c;

        while ((c = getopt(argcnt, args, "")) != EOF) {
                switch(c)
                {
                default:
                        argerrs++;
                        break;
                }
        }

        if (argerrs)
                cmd_usage(pc->curcmd, SYNOPSIS);

	dump_timer_data();
}

/*
 *  Display the pending timer queue entries, both the old and new-style.
 */
struct timer_data {
	ulong address; 
	ulong expires;
	ulong function;
};

struct tv_range {
        ulong base;
        ulong end;
};

#define TVN (6)

static void
dump_timer_data(void)
{
	int i;
	ulong timer_active;
	struct timer_struct {
        	unsigned long expires;
        	void *fn;
	} timer_table[32];
	char buf[BUFSIZE];
	char buf1[BUFSIZE];
        struct timer_struct *tp;
        ulong mask, highest, function;
	ulong jiffies, timer_jiffies;
	ulong *vec;
	long count;
        int vec_root_size, vec_size;
	struct timer_data *td;
	int flen, tdx, old_timers_exist;
        struct tv_range tv[TVN];

	if (symbol_exists("tvec_bases")) {
		dump_timer_data_tvec_bases_v1();
		return;
	} else if (symbol_exists("per_cpu__tvec_bases")) {
		dump_timer_data_tvec_bases_v2();
		return;
	}

	BZERO(tv, sizeof(struct tv_range) * TVN);

	vec_root_size = (i = ARRAY_LENGTH(timer_vec_root_vec)) ?
		i : get_array_length("timer_vec_root.vec", 
			NULL, SIZE(list_head));
	vec_size = (i = ARRAY_LENGTH(timer_vec_vec)) ? 
		i : get_array_length("timer_vec.vec", NULL, SIZE(list_head));

	vec = (ulong *)GETBUF(SIZE(list_head) * MAX(vec_root_size, vec_size));

	if (symbol_exists("timer_active") && symbol_exists("timer_table")) {
		get_symbol_data("timer_active", sizeof(ulong), &timer_active);
        	readmem(symbol_value("timer_table"), KVADDR, &timer_table,
                	sizeof(struct timer_struct) * 32, "timer_table[32]", 
			FAULT_ON_ERROR);
		old_timers_exist = TRUE;
	} else
		old_timers_exist = FALSE;

	/*
 	 * Get rough count first, and then gather a bunch of timer_data
	 * structs to stuff in a sortable array.
	 */

	count = 0;
        for (mask = 1, tp = timer_table+0; old_timers_exist && mask; 
	     tp++, mask += mask) {
                if (mask > timer_active)
                        break;
                if (!(mask & timer_active))
                        continue;
		count++;
        }

	init_tv_ranges(tv, vec_root_size, vec_size, 0);

        count += do_timer_list(symbol_value("tv1") + OFFSET(timer_vec_root_vec),
		vec_root_size, vec, NULL, NULL, tv);
        count += do_timer_list(symbol_value("tv2") + OFFSET(timer_vec_vec),
		vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(symbol_value("tv3") + OFFSET(timer_vec_vec),
		vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(symbol_value("tv4") + OFFSET(timer_vec_vec),
		vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(symbol_value("tv4") + OFFSET(timer_vec_vec),
		vec_size, vec, NULL, NULL, tv);

	td = (struct timer_data *)
		GETBUF((count*2) * sizeof(struct timer_data));
	tdx = 0;

	get_symbol_data("jiffies", sizeof(ulong), &jiffies);
	get_symbol_data("timer_jiffies", sizeof(ulong), &timer_jiffies);
	if (old_timers_exist)
		get_symbol_data("timer_active", sizeof(ulong), &timer_active);

	highest = 0;
        for (i = 0, mask = 1, tp = timer_table+0; old_timers_exist && mask; 
	     i++, tp++, mask += mask) {
                if (mask > timer_active) 
                        break;

                if (!(mask & timer_active)) 
                        continue;

		td[tdx].address = i;
		td[tdx].expires = tp->expires;
		td[tdx].function = (ulong)tp->fn;
		if (td[tdx].expires > highest)
			highest = td[tdx].expires;
		tdx++;
        }

	do_timer_list(symbol_value("tv1") + OFFSET(timer_vec_root_vec),
		vec_root_size, vec, (void *)td, &highest, tv);
	do_timer_list(symbol_value("tv2") + OFFSET(timer_vec_vec),
		vec_size, vec, (void *)td, &highest, tv);
	do_timer_list(symbol_value("tv3") + OFFSET(timer_vec_vec),
		vec_size, vec, (void *)td, &highest, tv);
	do_timer_list(symbol_value("tv4") + OFFSET(timer_vec_vec),
		vec_size, vec, (void *)td, &highest, tv);
	tdx = do_timer_list(symbol_value("tv5") + OFFSET(timer_vec_vec),
		vec_size, vec, (void *)td, &highest, tv);

        qsort(td, tdx, sizeof(struct timer_data), compare_timer_data);

	/*
	 *  Because the jiffies values can fluctuate wildly from dump to
	 *  dump, try to use the appropriate amount of space...
	 */
	sprintf(buf, "%ld", highest); 
	flen = MAX(strlen(buf), strlen("JIFFIES"));
	fprintf(fp, "%s\n", mkstring(buf, flen, CENTER|LJUST, "JIFFIES"));
	fprintf(fp, "%s\n", mkstring(buf, flen, RJUST|LONG_DEC,MKSTR(jiffies)));

	fprintf(fp, "%s  TIMER_LIST/TABLE  FUNCTION\n",
		mkstring(buf, flen, CENTER|LJUST, "EXPIRES"));

        for (i = 0; i < tdx; i++) {
        	fprintf(fp, "%s", 
		    mkstring(buf, flen, RJUST|LONG_DEC, MKSTR(td[i].expires)));

		if (td[i].address < 32) {
                        sprintf(buf, "timer_table[%ld]", td[i].address);
                        fprintf(fp, "  %s  ",
                                mkstring(buf, 16, CENTER|LJUST, NULL));
		} else {
			mkstring(buf1, VADDR_PRLEN, RJUST|LONG_HEX, 
				MKSTR(td[i].address));
			fprintf(fp, "  %s  ", mkstring(buf, 16, CENTER, buf1));
		}
		
		if (is_kernel_text(td[i].function)) 
			fprintf(fp, "%s  <%s>\n",
				mkstring(buf1, VADDR_PRLEN, RJUST|LONG_HEX, 
				MKSTR(td[i].function)),
                        	value_to_symstr(td[i].function, buf, 0));
		else {
			fprintf(fp, "%s  ", 
				mkstring(buf1, VADDR_PRLEN, RJUST|LONG_HEX, 
				MKSTR(td[i].function)));
                	if (readmem(td[i].function, KVADDR, &function,
                            sizeof(ulong), "timer function",
                            RETURN_ON_ERROR|QUIET)) {
				if (is_kernel_text(function))
					fprintf(fp, "<%s>",
					    value_to_symstr(function, buf, 0));
			} 
			fprintf(fp, "\n");
		}
        }
}

/*
 *  Newer per-cpu timers, using "tvec_bases".
 */

static void
dump_timer_data_tvec_bases_v1(void)
{
	int i, cpu, tdx, flen;
        struct timer_data *td;
        int vec_root_size, vec_size;
        struct tv_range tv[TVN];
	ulong *vec, jiffies, highest, function;
	long count;
	char buf1[BUFSIZE];
	char buf2[BUFSIZE];
	char buf3[BUFSIZE];

	kt->flags |= TVEC_BASES_V1;

	/*
         */
        vec_root_size = (i = ARRAY_LENGTH(tvec_root_s_vec)) ?
                i : get_array_length("tvec_root_s.vec", NULL, SIZE(list_head));
        vec_size = (i = ARRAY_LENGTH(tvec_s_vec)) ?
                i : get_array_length("tvec_s.vec", NULL, SIZE(list_head));
        vec = (ulong *)GETBUF(SIZE(list_head) * MAX(vec_root_size, vec_size));

	cpu = 0;

next_cpu:

        count = 0;
        td = (struct timer_data *)NULL;

	BZERO(tv, sizeof(struct tv_range) * TVN);

        init_tv_ranges(tv, vec_root_size, vec_size, cpu);

        count += do_timer_list(tv[1].base + OFFSET(tvec_root_s_vec),
                vec_root_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[2].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[3].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[4].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[5].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);

	if (count)
        	td = (struct timer_data *)
                	GETBUF((count*2) * sizeof(struct timer_data));
        tdx = 0;
	highest = 0;
        get_symbol_data("jiffies", sizeof(ulong), &jiffies);

        do_timer_list(tv[1].base + OFFSET(tvec_root_s_vec),
                vec_root_size, vec, (void *)td, &highest, tv);
        do_timer_list(tv[2].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);
        do_timer_list(tv[3].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);
        do_timer_list(tv[4].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);
        tdx = do_timer_list(tv[5].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);

        qsort(td, tdx, sizeof(struct timer_data), compare_timer_data);

	fprintf(fp, "TVEC_BASES[%d]: %lx\n", cpu,
        	symbol_value("tvec_bases") + (SIZE(tvec_t_base_s) * cpu));
		
        sprintf(buf1, "%ld", highest);
        flen = MAX(strlen(buf1), strlen("JIFFIES"));
        fprintf(fp, "%s\n", mkstring(buf1,flen, CENTER|RJUST, "JIFFIES"));
        fprintf(fp, "%s\n", mkstring(buf1,flen, 
		RJUST|LONG_DEC,MKSTR(jiffies)));

	fprintf(fp, "%s  %s  %s\n",
		mkstring(buf1, flen, CENTER|RJUST, "EXPIRES"),
		mkstring(buf2, VADDR_PRLEN, CENTER|LJUST, "TIMER_LIST"),
		mkstring(buf3, VADDR_PRLEN, CENTER|LJUST, "FUNCTION"));

        for (i = 0; i < tdx; i++) {
                fprintf(fp, "%s",
                    mkstring(buf1, flen, RJUST|LONG_DEC, MKSTR(td[i].expires)));

                fprintf(fp, "  %s  ", mkstring(buf1, 
			MAX(VADDR_PRLEN, strlen("TIMER_LIST")), 
			RJUST|CENTER|LONG_HEX, MKSTR(td[i].address)));

                if (is_kernel_text(td[i].function)) {
                        fprintf(fp, "%s  <%s>\n",
				mkstring(buf2, VADDR_PRLEN, RJUST|LONG_HEX,
				MKSTR(td[i].function)),
                                value_to_symstr(td[i].function, buf1, 0));
                } else {
                        fprintf(fp, "%s  ", mkstring(buf1, VADDR_PRLEN, 
				RJUST|LONG_HEX, MKSTR(td[i].function)));
                        if (readmem(td[i].function, KVADDR, &function,
                            sizeof(ulong), "timer function",
                            RETURN_ON_ERROR|QUIET)) {
                                if (is_kernel_text(function))
                                        fprintf(fp, "<%s>",
                                            value_to_symstr(function, buf1, 0));
                        }
                        fprintf(fp, "\n");
                }
        }

	if (td)
		FREEBUF(td);

	if (++cpu < kt->cpus)
		goto next_cpu;
}

/*
 *  2.6 per-cpu timers, using "per_cpu__tvec_bases".  XXX
 */

static void
dump_timer_data_tvec_bases_v2(void)
{
	int i, cpu, tdx, flen;
        struct timer_data *td;
        int vec_root_size, vec_size;
        struct tv_range tv[TVN];
	ulong *vec, jiffies, highest, function;
	ulong tvec_bases;
	long count;
	char buf1[BUFSIZE];
	char buf2[BUFSIZE];
	char buf3[BUFSIZE];

	kt->flags |= TVEC_BASES_V2;

	/*
         */
        vec_root_size = (i = ARRAY_LENGTH(tvec_root_s_vec)) ?
                i : get_array_length("tvec_root_s.vec", NULL, SIZE(list_head));
        vec_size = (i = ARRAY_LENGTH(tvec_s_vec)) ?
                i : get_array_length("tvec_s.vec", NULL, SIZE(list_head));
        vec = (ulong *)GETBUF(SIZE(list_head) * MAX(vec_root_size, vec_size));
	cpu = 0;

next_cpu:

	count = 0;
	td = (struct timer_data *)NULL;

	BZERO(tv, sizeof(struct tv_range) * TVN);

        init_tv_ranges(tv, vec_root_size, vec_size, cpu);

        count += do_timer_list(tv[1].base + OFFSET(tvec_root_s_vec),
                vec_root_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[2].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[3].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[4].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);
        count += do_timer_list(tv[5].base + OFFSET(tvec_s_vec),
                vec_size, vec, NULL, NULL, tv);

	if (count)
        	td = (struct timer_data *)
                	GETBUF((count*2) * sizeof(struct timer_data));
        tdx = 0;
	highest = 0;
        get_symbol_data("jiffies", sizeof(ulong), &jiffies);

        do_timer_list(tv[1].base + OFFSET(tvec_root_s_vec),
                vec_root_size, vec, (void *)td, &highest, tv);
        do_timer_list(tv[2].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);
        do_timer_list(tv[3].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);
        do_timer_list(tv[4].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);
        tdx = do_timer_list(tv[5].base + OFFSET(tvec_s_vec),
                vec_size, vec, (void *)td, &highest, tv);

        qsort(td, tdx, sizeof(struct timer_data), compare_timer_data);

        if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF))
                tvec_bases = symbol_value("per_cpu__tvec_bases") +
                        kt->__per_cpu_offset[cpu];
        else
                tvec_bases =  symbol_value("per_cpu__tvec_bases");

        fprintf(fp, "TVEC_BASES[%d]: %lx\n", cpu,
                tvec_bases + SIZE(tvec_t_base_s));
		
        sprintf(buf1, "%ld", highest);
        flen = MAX(strlen(buf1), strlen("JIFFIES"));
        fprintf(fp, "%s\n", mkstring(buf1,flen, CENTER|RJUST, "JIFFIES"));
        fprintf(fp, "%s\n", mkstring(buf1,flen, 
		RJUST|LONG_DEC,MKSTR(jiffies)));

	fprintf(fp, "%s  %s  %s\n",
		mkstring(buf1, flen, CENTER|RJUST, "EXPIRES"),
		mkstring(buf2, VADDR_PRLEN, CENTER|LJUST, "TIMER_LIST"),
		mkstring(buf3, VADDR_PRLEN, CENTER|LJUST, "FUNCTION"));

        for (i = 0; i < tdx; i++) {
                fprintf(fp, "%s",
                    mkstring(buf1, flen, RJUST|LONG_DEC, MKSTR(td[i].expires)));

                fprintf(fp, "  %s  ", mkstring(buf1, 
			MAX(VADDR_PRLEN, strlen("TIMER_LIST")), 
			RJUST|CENTER|LONG_HEX, MKSTR(td[i].address)));

                if (is_kernel_text(td[i].function)) {
                        fprintf(fp, "%s  <%s>\n",
				mkstring(buf2, VADDR_PRLEN, RJUST|LONG_HEX,
				MKSTR(td[i].function)),
                                value_to_symstr(td[i].function, buf1, 0));
                } else {
                        fprintf(fp, "%s  ", mkstring(buf1, VADDR_PRLEN, 
				RJUST|LONG_HEX, MKSTR(td[i].function)));
                        if (readmem(td[i].function, KVADDR, &function,
                            sizeof(ulong), "timer function",
                            RETURN_ON_ERROR|QUIET)) {
                                if (is_kernel_text(function))
                                        fprintf(fp, "<%s>",
                                            value_to_symstr(function, buf1, 0));
                        }
                        fprintf(fp, "\n");
                }
        }

	if (td)
		FREEBUF(td);

	if (++cpu < kt->cpus)
		goto next_cpu;
}

/*
 *  The comparison function must return an integer less  than,
 *  equal  to,  or  greater than zero if the first argument is
 *  considered to be respectively  less  than,  equal  to,  or
 *  greater than the second.  If two members compare as equal,
 *  their order in the sorted array is undefined.
 */

static int
compare_timer_data(const void *v1, const void *v2)
{
        struct timer_data *t1, *t2;

        t1 = (struct timer_data *)v1;
        t2 = (struct timer_data *)v2;

	return (t1->expires < t2->expires ? -1 :
		t1->expires == t2->expires ? 0 : 1);
}

/*
 *  Create the address range for each of the timer vectors.
 */
static void
init_tv_ranges(struct tv_range *tv, int vec_root_size, int vec_size, int cpu)
{
	ulong tvec_bases;

	if (kt->flags & TVEC_BASES_V1) {
                tv[1].base = symbol_value("tvec_bases") +
			(SIZE(tvec_t_base_s) * cpu) +
			OFFSET(tvec_t_base_s_tv1);
                tv[1].end = tv[1].base + SIZE(tvec_root_s);

                tv[2].base = tv[1].end;
                tv[2].end = tv[2].base + SIZE(tvec_s);

                tv[3].base = tv[2].end;
                tv[3].end = tv[3].base + SIZE(tvec_s);

                tv[4].base = tv[3].end;
                tv[4].end = tv[4].base + SIZE(tvec_s);

                tv[5].base = tv[4].end;
                tv[5].end = tv[5].base + SIZE(tvec_s);
	} else if (kt->flags & TVEC_BASES_V2) {
		if ((kt->flags & SMP) && (kt->flags & PER_CPU_OFF)) 
			tvec_bases = symbol_value("per_cpu__tvec_bases") +
				kt->__per_cpu_offset[cpu];
		else		
			tvec_bases =  symbol_value("per_cpu__tvec_bases");

                tv[1].base = tvec_bases +
                        OFFSET(tvec_t_base_s_tv1);
                tv[1].end = tv[1].base + SIZE(tvec_root_s);

                tv[2].base = tv[1].end;
                tv[2].end = tv[2].base + SIZE(tvec_s);

                tv[3].base = tv[2].end;
                tv[3].end = tv[3].base + SIZE(tvec_s);

                tv[4].base = tv[3].end;
                tv[4].end = tv[4].base + SIZE(tvec_s);

                tv[5].base = tv[4].end;
                tv[5].end = tv[5].base + SIZE(tvec_s);
	} else {
		tv[1].base = symbol_value("tv1");
	        tv[1].end = tv[1].base + SIZE(timer_vec_root);
	
	        tv[2].base = symbol_value("tv2");
	        tv[2].end = tv[2].base + SIZE(timer_vec);
	
	        tv[3].base = symbol_value("tv3");
	        tv[3].end = tv[3].base + SIZE(timer_vec);
	
	        tv[4].base = symbol_value("tv4");
	        tv[4].end = tv[4].base + SIZE(timer_vec);
	
	        tv[5].base = symbol_value("tv5");
	        tv[5].end = tv[5].base + SIZE(timer_vec);
	}
}

#define IN_TV_RANGE(vaddr) \
	((((vaddr) >= tv[1].base) && ((vaddr) < tv[1].end)) || \
	 (((vaddr) >= tv[2].base) && ((vaddr) < tv[2].end)) || \
	 (((vaddr) >= tv[3].base) && ((vaddr) < tv[3].end)) || \
	 (((vaddr) >= tv[4].base) && ((vaddr) < tv[4].end)) || \
	 (((vaddr) >= tv[5].base) && ((vaddr) < tv[5].end)))

/*
 *  Count, or stash, the entries of a linked timer_list -- depending
 *  upon the option value.
 */
static int
do_timer_list(ulong vec_kvaddr,
	      int size, 
	      ulong *vec, 
	      void *option, 
	      ulong *highest,
	      struct tv_range *tv)
{
	int i, t; 
	int count, tdx;
	ulong expires, function;
	struct timer_data *td;
	char *timer_list_buf;
	ulong *timer_list;
	int timer_cnt;
        struct list_data list_data, *ld;
	long sz;
	ulong offset;

	td = option ? (struct timer_data *)option : NULL;
	if (td) {
		tdx = 0;
		while (td[tdx].function)
			tdx++;
	}

        if (VALID_MEMBER(timer_list_list))
		sz = SIZE(list_head) * size;
	else if (VALID_MEMBER(timer_list_entry))
		sz = SIZE(list_head) * size;
	else 
		sz = sizeof(ulong) * size;

        readmem(vec_kvaddr, KVADDR, vec, sz, "timer_list vec array",
                FAULT_ON_ERROR);

	if (VALID_MEMBER(timer_list_list)) {
		offset = OFFSET(timer_list_list);
		goto new_timer_list_format;
	}

	if (VALID_MEMBER(timer_list_entry)) {
		offset = OFFSET(timer_list_entry);
		goto new_timer_list_format;
	}

	if (VALID_MEMBER(timer_list_next) >= 0)
		offset = OFFSET(timer_list_next);
	else
		error(FATAL, "no timer_list next, list, or entry members?\n");

	ld = &list_data;
	timer_list_buf = GETBUF(SIZE(timer_list));

        for (i = count = 0; i < size; i++) {
                if (vec[i]) {
			BZERO(ld, sizeof(struct list_data));
			ld->start = vec[i];
			ld->member_offset = offset;

			hq_open();
                	timer_cnt = do_list(ld);
			if (!timer_cnt)
				continue;
                	timer_list = (ulong *)GETBUF(timer_cnt * sizeof(ulong));
                	timer_cnt = retrieve_list(timer_list, timer_cnt);
                	hq_close();

			for (t = 0; t < timer_cnt; t++) {
                                readmem(timer_list[t], KVADDR, timer_list_buf,
                                        SIZE(timer_list), "timer_list buffer",
                                        FAULT_ON_ERROR);

                                expires = ULONG(timer_list_buf +
                                        OFFSET(timer_list_expires));
                                function = ULONG(timer_list_buf +
                                        OFFSET(timer_list_function));

                                if (td) {
                                        td[tdx].address = timer_list[t];
                                        td[tdx].expires = expires;
                                        td[tdx].function = function;
                                        if (highest && (expires > *highest))
                                                *highest = expires;
                                        tdx++;
                                }
			}
			FREEBUF(timer_list);
			count += timer_cnt;
        	}
	}

	FREEBUF(timer_list_buf);

	return(td ? tdx : count);

new_timer_list_format:

	ld = &list_data;
	timer_list_buf = GETBUF(SIZE(timer_list));

        for (i = count = 0; i < (size*2); i += 2, 
	     vec_kvaddr += SIZE(list_head)) {

		if (vec[i] == vec_kvaddr)
			continue;

                BZERO(ld, sizeof(struct list_data));
                ld->start = vec[i];
                ld->list_head_offset = offset;
		ld->end = vec_kvaddr;

                hq_open();
                timer_cnt = do_list(ld);
                if (!timer_cnt)
                	continue;
                timer_list = (ulong *)GETBUF(timer_cnt * sizeof(ulong));
                timer_cnt = retrieve_list(timer_list, timer_cnt);
                hq_close();

                for (t = 0; t < timer_cnt; t++) {
			if (IN_TV_RANGE(timer_list[t]))
				break;

			count++;

                        readmem(timer_list[t], KVADDR, timer_list_buf,
                                SIZE(timer_list), "timer_list buffer",
                                FAULT_ON_ERROR);

                        expires = ULONG(timer_list_buf + 
				OFFSET(timer_list_expires));
                        function = ULONG(timer_list_buf +
                        	OFFSET(timer_list_function));

                        if (td) {
                                td[tdx].address = timer_list[t];
                                td[tdx].expires = expires;
                                td[tdx].function = function;
                                if (highest && (expires > *highest))
                                        *highest = expires;
                                tdx++;
                        }
		}
	}

	FREEBUF(timer_list_buf);

	return(td ? tdx : count);
}

/*
 *  Panic a live system by exploiting this code in do_exit():
 *
 *      if (!tsk->pid)
 *              panic("Attempted to kill the idle task!");
 *
 *  by writing a zero to this task's pid number.  If the write
 *  succeeds, the subsequent exit() call will invoke the panic.
 */
static void
panic_this_kernel(void)
{
	pid_t zero_pid = 0;

	if (DUMPFILE())
		error(FATAL, "cannot panic a dumpfile!\n");

	if (!(pc->flags & MFD_RDWR) || (pc->flags & MEMMOD))
		error(FATAL, "cannot write to %s\n", pc->live_memsrc);

	writemem(pid_to_task(pc->program_pid) + OFFSET(task_struct_pid), KVADDR,
		&zero_pid, sizeof(pid_t), "zero pid", FAULT_ON_ERROR);

	clean_exit(0);
}

/*
 *  Dump the list of entries on a wait queue, taking into account the two
 *  different definitions: wait_queue vs. __wait_queue (wait_queue_t).
 */
void
cmd_waitq(void)
{
	ulong q = 0;
	char *wq_name = NULL;		/* name of symbol which is a waitq */
	char *wq_struct = NULL;		/* struct containing the waitq */
	char *wq_member = NULL;		/* member of struct which is a waitq */
	int recd_address = 0;

	if (argcnt < 2 || argcnt > 3) {
		cmd_usage(pc->curcmd, SYNOPSIS);
	}

	if (IS_A_NUMBER(args[1])) {
		q = htol(args[1], FAULT_ON_ERROR, NULL);
		recd_address = 1;
	} else {
		/*
		 * We weren't given a number... see if it is the name of
		 * a symbol or and struct.member format.
		 */
		char *dot;

		dot = strstr(args[1], ".");
		if (dot == NULL) {
			wq_name = args[1];
			q = symbol_value(wq_name);
		} else {

			wq_struct = args[1];
			wq_member = dot+1;
			*dot = '\0';
			if (argcnt != 3) {
				fprintf(fp, "must supply an address for %s\n",
					wq_struct);
				return;
			}
			q = htol(args[2], FAULT_ON_ERROR, NULL);
			if (MEMBER_OFFSET(wq_struct, wq_member) == -1) {
				fprintf(fp, "%s is not a member of %s\n",
					wq_member, wq_struct);
				return;
			}
			q += MEMBER_OFFSET(wq_struct, wq_member);
		}
	}

	if (q != 0 && IS_KVADDR(q)) {
		/*
		 * If we weren't passed in an address and we're dealing
		 * with old style wait_queue, we must dereference the pointer
		 * and pass in the addr of the first elem on the queue.
		 * If we were supplied an address, assume the user knows
		 * what should be provided.
		 */
		if (!recd_address && VALID_STRUCT(wait_queue)) {
			ulong first_elem;
			readmem(q, KVADDR, &first_elem, sizeof(q),
				"wait queue pointer", FAULT_ON_ERROR);
			if (first_elem == 0) {
				fprintf(fp, "wait queue %lx is empty\n", q);
				return;
			} else {
				q = first_elem;
			}
		}
		dump_waitq(q, wq_name);
	}
}

static void
dump_waitq(ulong wq, char *wq_name)
{
	struct list_data list_data, *ld;
	ulong *wq_list;			/* addr of wait queue element */
	ulong next_offset;		/* next pointer of wq element */
	ulong task_offset;		/* offset of task in wq element */
	int cnt;			/* # elems on Queue */
	int start_index;		/* where to start in wq array */
	int i;

	ld = &list_data;
	BZERO(ld, sizeof(*ld));

	/*
	 * setup list depending on how the wait queues are organized.
	 */
	if (VALID_STRUCT(wait_queue)) {
		task_offset = OFFSET(wait_queue_task);
		next_offset = OFFSET(wait_queue_next);
		ld->end = wq;
		ld->start = wq;
		ld->member_offset = next_offset;
		ld->list_head_offset = task_offset;

		start_index = 0;
	} else if (VALID_STRUCT(__wait_queue)) {
		ulong task_list_offset;

                next_offset = OFFSET(list_head_next);
                task_offset = OFFSET(__wait_queue_task);
                task_list_offset = OFFSET(__wait_queue_head_task_list);
                ld->end = ld->start = wq + task_list_offset + next_offset;
                ld->list_head_offset = OFFSET(__wait_queue_task_list);
                ld->member_offset = next_offset;

		start_index = 1;
	} else {
		return;
	}

	hq_open();

	cnt = do_list(ld);
	if (cnt <= 1) {
		/*
		 * Due to the queueing of wait queues, list count returns
		 * an extra number of list entries:
		 * - in the case of a wait_queue_head_t, there is the
		 *   the list_entry in that structure;
		 * - in the case of a simple wait_queue, we have the
		 *   pointer back to the wait_queue head (see the
		 *   WAIT_QUEUE_HEAD macro in 2.2 systems).
		 */
		if (wq_name)
			fprintf(fp, "wait queue \"%s\" (%lx) is empty\n", 
				wq_name, wq);
		else
			fprintf(fp, "wait queue %lx is empty\n", wq);
		hq_close();
		return;
	}

	wq_list = (ulong *) GETBUF(cnt * sizeof(ulong));
	cnt = retrieve_list(wq_list, cnt);

	for (i = start_index; i < cnt; i++) {
		struct task_context *tc;
		ulong task;

		readmem(wq_list[i] + task_offset, KVADDR, &task,
			sizeof(void *), "wait_queue_t.task", FAULT_ON_ERROR);

		if ((tc = task_to_context(task))) {
			print_task_header(fp, tc, 0);
		} else {
			break;
		}
	}

	hq_close();
}

/*
 *  If active, clear the references to the last page tables read.
 */
void
clear_machdep_cache(void)
{
	if (ACTIVE()) {
		machdep->last_pgd_read = 0;
		machdep->last_pmd_read = 0;
		machdep->last_ptbl_read = 0;
	}
}

/*
 *  For kernels containing cpu_online_map, count the bits.
 */
int
get_cpus_online()
{
	ulong cpu_online_map;

	if (!symbol_exists("cpu_online_map")) 
		return 0;

	get_symbol_data("cpu_online_map", sizeof(ulong), &cpu_online_map);

	return count_bits_long(cpu_online_map);
}