aboutsummaryrefslogtreecommitdiff
path: root/gdb/infttrace.c
blob: 30e893c323b4e30ad433ebf49887068704f27f1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
/* Low level Unix child interface to ttrace, for GDB when running under HP-UX.

   Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
   1998, 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc.

   This file is part of GDB.

   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.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.  */

#include "defs.h"
#include "frame.h"
#include "inferior.h"
#include "target.h"
#include "gdb_string.h"
#include "gdb_wait.h"
#include "command.h"
#include "gdbthread.h"
#include "infttrace.h"

/* We need pstat functionality so that we can get the exec file
   for a process we attach to.

   According to HP, we should use the 64bit interfaces, so we
   define _PSTAT64 to achieve this.  */
#define _PSTAT64
#include <sys/pstat.h>

/* Some hackery to work around a use of the #define name NO_FLAGS
 * in both gdb and HPUX (bfd.h and /usr/include/machine/vmparam.h).
 */
#ifdef  NO_FLAGS
#define INFTTRACE_TEMP_HACK NO_FLAGS
#undef  NO_FLAGS
#endif

#include <sys/param.h>
#include <sys/dir.h>
#include <signal.h>
#include <sys/ioctl.h>

#include <sys/ttrace.h>
#include <sys/mman.h>

#ifndef NO_PTRACE_H
#ifdef PTRACE_IN_WRONG_PLACE
#include <ptrace.h>
#else
#include <sys/ptrace.h>
#endif
#endif /* NO_PTRACE_H */

/* Second half of the hackery above.  Non-ANSI C, so
 * we can't use "#error", alas.
 */
#ifdef NO_FLAGS
#if (NO_FLAGS != INFTTRACE_TEMP_HACK )
  /* #error "Hackery to remove warning didn't work right" */
#else
  /* Ok, new def'n of NO_FLAGS is same as old one; no action needed. */
#endif
#else
  /* #error "Didn't get expected re-definition of NO_FLAGS" */
#define NO_FLAGS INFTTRACE_TEMP_HACK
#endif

#if !defined (PT_SETTRC)
#define PT_SETTRC	0	/* Make process traceable by parent */
#endif
#if !defined (PT_READ_I)
#define PT_READ_I	1	/* Read word from text space */
#endif
#if !defined (PT_READ_D)
#define	PT_READ_D	2	/* Read word from data space */
#endif
#if !defined (PT_READ_U)
#define PT_READ_U	3	/* Read word from kernel user struct */
#endif
#if !defined (PT_WRITE_I)
#define PT_WRITE_I	4	/* Write word to text space */
#endif
#if !defined (PT_WRITE_D)
#define PT_WRITE_D	5	/* Write word to data space */
#endif
#if !defined (PT_WRITE_U)
#define PT_WRITE_U	6	/* Write word to kernel user struct */
#endif
#if !defined (PT_CONTINUE)
#define PT_CONTINUE	7	/* Continue after signal */
#endif
#if !defined (PT_STEP)
#define PT_STEP		9	/* Set flag for single stepping */
#endif
#if !defined (PT_KILL)
#define PT_KILL		8	/* Send child a SIGKILL signal */
#endif

#ifndef PT_ATTACH
#define PT_ATTACH PTRACE_ATTACH
#endif
#ifndef PT_DETACH
#define PT_DETACH PTRACE_DETACH
#endif

#include "gdbcore.h"
#ifdef	HAVE_SYS_FILE_H
#include <sys/file.h>
#endif

/* This semaphore is used to coordinate the child and parent processes
   after a fork(), and before an exec() by the child.  See parent_attach_all
   for details.
 */
typedef struct
  {
    int parent_channel[2];	/* Parent "talks" to [1], child "listens" to [0] */
    int child_channel[2];	/* Child "talks" to [1], parent "listens" to [0] */
  }
startup_semaphore_t;

#define SEM_TALK (1)
#define SEM_LISTEN (0)

static startup_semaphore_t startup_semaphore;

/* See can_touch_threads_of_process for details. */
static int vforking_child_pid = 0;
static int vfork_in_flight = 0;

/* 1 if ok as results of a ttrace or ttrace_wait call, 0 otherwise.
 */
#define TT_OK( _status, _errno ) \
    (((_status) == 1) && ((_errno) == 0))

#define TTRACE_ARG_TYPE uint64_t

/* When supplied as the "addr" operand, ttrace interprets this
   to mean, "from the current address".
 */
#define TT_USE_CURRENT_PC ((TTRACE_ARG_TYPE) TT_NOPC)

/* When supplied as the "addr", "data" or "addr2" operand for most
   requests, ttrace interprets this to mean, "pay no heed to this
   argument".
 */
#define TT_NIL ((TTRACE_ARG_TYPE) TT_NULLARG)

/* This is capable of holding the value of a 32-bit register.  The
   value is always left-aligned in the buffer; i.e., [0] contains
   the most-significant byte of the register's value, and [sizeof(reg)]
   contains the least-significant value.

   ??rehrauer: Yes, this assumes that an int is 32-bits on HP-UX, and
   that registers are 32-bits on HP-UX.  The latter assumption changes
   with PA2.0.
 */
typedef int register_value_t;

/********************************************************************

                 How this works:

   1.  Thread numbers

   The rest of GDB sees threads as being things with different
   "pid" (process id) values.  See "thread.c" for details.  The
   separate threads will be seen and reacted to if infttrace passes
   back different pid values (for _events_).  See wait_for_inferior
   in inftarg.c.

   So infttrace is going to use thread ids externally, pretending
   they are process ids, and keep track internally so that it can
   use the real process id (and thread id) when calling ttrace.

   The data structure that supports this is a linked list of the
   current threads.  Since at some date infttrace will have to
   deal with multiple processes, each list element records its
   corresponding pid, rather than having a single global.

   Note that the list is only approximately current; that's ok, as
   it's up to date when we need it (we hope!).  Also, it can contain
   dead threads, as there's no harm if it does.

   The approach taken here is to bury the translation from external
   to internal inside "call_ttrace" and a few other places.

   There are some wrinkles:

   o  When GDB forks itself to create the debug target process,
      there's only a pid of 0 around in the child, so the
      TT_PROC_SETTRC operation uses a more direct call to ttrace;
      Similiarly, the initial setting of the event mask happens
      early as  well, and so is also special-cased, and an attach
      uses a real pid;

   o  We define an unthreaded application as having a "pseudo"
      thread;

   o  To keep from confusing the rest of GDB, we don't switch
      the PID for the pseudo thread to a TID.  A table will help:

      Rest of GDB sees these PIDs:     pid   tid1  tid2  tid3 ...
                                        
      Our thread list stores:          pid   pid   pid   pid  ...
                                       tid0  tid1  tid2  tid3
      
      Ttrace sees these TIDS:          tid0  tid1  tid2  tid3 ...

      Both pid and tid0 will map to tid0, as there are infttrace.c-internal
      calls to ttrace using tid0.

   2. Step and Continue

   Since we're implementing the "stop the world" model, sub-model
   "other threads run during step", we have some stuff to do:

   o  User steps require continuing all threads other than the
      one the user is stepping;

   o  Internal debugger steps (such as over a breakpoint or watchpoint,
      but not out of a library load thunk) require stepping only
      the selected thread; this means that we have to report the
      step finish on that thread, which can lead to complications;

   o  When a thread is created, it is created running, rather
      than stopped--so we have to stop it.

   The OS doesn't guarantee the stopped thread list will be stable,
   no does it guarantee where on the stopped thread list a thread
   that is single-stepped will wind up: it's possible that it will
   be off the list for a while, it's possible the step will complete
   and it will be re-posted to the end...

   This means we have to scan the stopped thread list, build up
   a work-list, and then run down the work list; we can't do the
   step/continue during the scan.

   3. Buffering events

   Then there's the issue of waiting for an event.  We do this by
   noticing how many events are reported at the end of each wait.
   From then on, we "fake" all resumes and steps, returning instantly,
   and don't do another wait.  Once all pending events are reported,
   we can really resume again.

   To keep this hidden, all the routines which know about tids and
   pids or real events and simulated ones are static (file-local).

   This code can make lots of calls to ttrace, in particular it
   can spin down the list of thread states more than once.  If this
   becomes a performance hit, the spin could be done once and the
   various "tsp" blocks saved, keeping all later spins in this
   process.

   The O/S doesn't promise to keep the list straight, and so we must
   re-scan a lot.  By observation, it looks like a single-step/wait
   puts the stepped thread at the end of the list but doesn't change
   it otherwise.

****************************************************************
*/

/* Uncomment these to turn on various debugging output */
/* #define THREAD_DEBUG */
/* #define WAIT_BUFFER_DEBUG */
/* #define PARANOIA */


#define INFTTRACE_ALL_THREADS (-1)
#define INFTTRACE_STEP        (1)
#define INFTTRACE_CONTINUE    (0)

/* FIX: this is used in inftarg.c/child_wait, in a hack.
 */
extern int not_same_real_pid;

/* This is used to count buffered events.
 */
static unsigned int more_events_left = 0;

/* Process state.
 */
typedef enum process_state_enum
  {
    STOPPED,
    FAKE_STEPPING,
    FAKE_CONTINUE,		/* For later use */
    RUNNING,
    FORKING,
    VFORKING
  }
process_state_t;

static process_state_t process_state = STOPPED;

/* User-specified stepping modality.
 */
typedef enum stepping_mode_enum
  {
    DO_DEFAULT,			/* ...which is a continue! */
    DO_STEP,
    DO_CONTINUE
  }
stepping_mode_t;

/* Action to take on an attach, depends on
 * what kind (user command, fork, vfork).
 *
 * At the moment, this is either:
 *
 * o  continue with a SIGTRAP signal, or
 *
 * o  leave stopped.
 */
typedef enum attach_continue_enum
  {
    DO_ATTACH_CONTINUE,
    DONT_ATTACH_CONTINUE
  }
attach_continue_t;

/* This flag is true if we are doing a step-over-bpt
 * with buffered events.  We will have to be sure to
 * report the right thread, as otherwise the spaghetti
 * code in "infrun.c/wait_for_inferior" will get
 * confused.
 */
static int doing_fake_step = 0;
static lwpid_t fake_step_tid = 0;


/****************************************************
 * Thread information structure routines and types. *
 ****************************************************
 */
typedef
struct thread_info_struct
  {
    int am_pseudo;		/* This is a pseudo-thread for the process. */
    int pid;			/* Process ID */
    lwpid_t tid;		/* Thread  ID */
    int handled;		/* 1 if a buffered event was handled. */
    int seen;			/* 1 if this thread was seen on a traverse. */
    int terminated;		/* 1 if thread has terminated. */
    int have_signal;		/* 1 if signal to be sent */
    enum target_signal signal_value;	/* Signal to send */
    int have_start;		/* 1 if alternate starting address */
    stepping_mode_t stepping_mode;	/* Whether to step or continue */
    CORE_ADDR start;		/* Where to start */
    int have_state;		/* 1 if the event state has been set */
    ttstate_t last_stop_state;	/* The most recently-waited event for this thread. */
    struct thread_info_struct
     *next;			/* All threads are linked via this field. */
    struct thread_info_struct
     *next_pseudo;		/* All pseudo-threads are linked via this field. */
  }
thread_info;

typedef
struct thread_info_header_struct
  {
    int count;
    thread_info *head;
    thread_info *head_pseudo;

  }
thread_info_header;

static thread_info_header thread_head =
{0, NULL, NULL};
static thread_info_header deleted_threads =
{0, NULL, NULL};

static ptid_t saved_real_ptid;


/*************************************************
 *          Debugging support functions          *
 *************************************************
 */
CORE_ADDR
get_raw_pc (lwpid_t ttid)
{
  unsigned long pc_val;
  int offset;
  int res;

  offset = register_addr (PC_REGNUM, U_REGS_OFFSET);
  res = read_from_register_save_state (
					ttid,
					(TTRACE_ARG_TYPE) offset,
					(char *) &pc_val,
					sizeof (pc_val));
  if (res <= 0)
    {
      return (CORE_ADDR) pc_val;
    }
  else
    {
      return (CORE_ADDR) 0;
    }
}

static char *
get_printable_name_of_stepping_mode (stepping_mode_t mode)
{
  switch (mode)
    {
    case DO_DEFAULT:
      return "DO_DEFAULT";
    case DO_STEP:
      return "DO_STEP";
    case DO_CONTINUE:
      return "DO_CONTINUE";
    default:
      return "?unknown mode?";
    }
}

/* This function returns a pointer to a string describing the
 * ttrace event being reported.
 */
char *
get_printable_name_of_ttrace_event (ttevents_t event)
{
  /* This enumeration is "gappy", so don't use a table. */
  switch (event)
    {

    case TTEVT_NONE:
      return "TTEVT_NONE";
    case TTEVT_SIGNAL:
      return "TTEVT_SIGNAL";
    case TTEVT_FORK:
      return "TTEVT_FORK";
    case TTEVT_EXEC:
      return "TTEVT_EXEC";
    case TTEVT_EXIT:
      return "TTEVT_EXIT";
    case TTEVT_VFORK:
      return "TTEVT_VFORK";
    case TTEVT_SYSCALL_RETURN:
      return "TTEVT_SYSCALL_RETURN";
    case TTEVT_LWP_CREATE:
      return "TTEVT_LWP_CREATE";
    case TTEVT_LWP_TERMINATE:
      return "TTEVT_LWP_TERMINATE";
    case TTEVT_LWP_EXIT:
      return "TTEVT_LWP_EXIT";
    case TTEVT_LWP_ABORT_SYSCALL:
      return "TTEVT_LWP_ABORT_SYSCALL";
    case TTEVT_SYSCALL_ENTRY:
      return "TTEVT_SYSCALL_ENTRY";
    case TTEVT_SYSCALL_RESTART:
      return "TTEVT_SYSCALL_RESTART";
    default:
      return "?new event?";
    }
}


/* This function translates the ttrace request enumeration into
 * a character string that is its printable (aka "human readable")
 * name.
 */
char *
get_printable_name_of_ttrace_request (ttreq_t request)
{
  if (!IS_TTRACE_REQ (request))
    return "?bad req?";

  /* This enumeration is "gappy", so don't use a table. */
  switch (request)
    {
    case TT_PROC_SETTRC:
      return "TT_PROC_SETTRC";
    case TT_PROC_ATTACH:
      return "TT_PROC_ATTACH";
    case TT_PROC_DETACH:
      return "TT_PROC_DETACH";
    case TT_PROC_RDTEXT:
      return "TT_PROC_RDTEXT";
    case TT_PROC_WRTEXT:
      return "TT_PROC_WRTEXT";
    case TT_PROC_RDDATA:
      return "TT_PROC_RDDATA";
    case TT_PROC_WRDATA:
      return "TT_PROC_WRDATA";
    case TT_PROC_STOP:
      return "TT_PROC_STOP";
    case TT_PROC_CONTINUE:
      return "TT_PROC_CONTINUE";
    case TT_PROC_GET_PATHNAME:
      return "TT_PROC_GET_PATHNAME";
    case TT_PROC_GET_EVENT_MASK:
      return "TT_PROC_GET_EVENT_MASK";
    case TT_PROC_SET_EVENT_MASK:
      return "TT_PROC_SET_EVENT_MASK";
    case TT_PROC_GET_FIRST_LWP_STATE:
      return "TT_PROC_GET_FIRST_LWP_STATE";
    case TT_PROC_GET_NEXT_LWP_STATE:
      return "TT_PROC_GET_NEXT_LWP_STATE";
    case TT_PROC_EXIT:
      return "TT_PROC_EXIT";
    case TT_PROC_GET_MPROTECT:
      return "TT_PROC_GET_MPROTECT";
    case TT_PROC_SET_MPROTECT:
      return "TT_PROC_SET_MPROTECT";
    case TT_PROC_SET_SCBM:
      return "TT_PROC_SET_SCBM";
    case TT_LWP_STOP:
      return "TT_LWP_STOP";
    case TT_LWP_CONTINUE:
      return "TT_LWP_CONTINUE";
    case TT_LWP_SINGLE:
      return "TT_LWP_SINGLE";
    case TT_LWP_RUREGS:
      return "TT_LWP_RUREGS";
    case TT_LWP_WUREGS:
      return "TT_LWP_WUREGS";
    case TT_LWP_GET_EVENT_MASK:
      return "TT_LWP_GET_EVENT_MASK";
    case TT_LWP_SET_EVENT_MASK:
      return "TT_LWP_SET_EVENT_MASK";
    case TT_LWP_GET_STATE:
      return "TT_LWP_GET_STATE";
    default:
      return "?new req?";
    }
}


/* This function translates the process state enumeration into
 * a character string that is its printable (aka "human readable")
 * name.
 */
static char *
get_printable_name_of_process_state (process_state_t process_state)
{
  switch (process_state)
    {
    case STOPPED:
      return "STOPPED";
    case FAKE_STEPPING:
      return "FAKE_STEPPING";
    case RUNNING:
      return "RUNNING";
    case FORKING:
      return "FORKING";
    case VFORKING:
      return "VFORKING";
    default:
      return "?some unknown state?";
    }
}

/* Set a ttrace thread state to a safe, initial state.
 */
static void
clear_ttstate_t (ttstate_t *tts)
{
  tts->tts_pid = 0;
  tts->tts_lwpid = 0;
  tts->tts_user_tid = 0;
  tts->tts_event = TTEVT_NONE;
}

/* Copy ttrace thread state TTS_FROM into TTS_TO.
 */
static void
copy_ttstate_t (ttstate_t *tts_to, ttstate_t *tts_from)
{
  memcpy ((char *) tts_to, (char *) tts_from, sizeof (*tts_to));
}

/* Are there any live threads we know about?
 */
static int
any_thread_records (void)
{
  return (thread_head.count > 0);
}

/* Create, fill in and link in a thread descriptor.
 */
static thread_info *
create_thread_info (int pid, lwpid_t tid)
{
  thread_info *new_p;
  thread_info *p;
  int thread_count_of_pid;

  new_p = xmalloc (sizeof (thread_info));
  new_p->pid = pid;
  new_p->tid = tid;
  new_p->have_signal = 0;
  new_p->have_start = 0;
  new_p->have_state = 0;
  clear_ttstate_t (&new_p->last_stop_state);
  new_p->am_pseudo = 0;
  new_p->handled = 0;
  new_p->seen = 0;
  new_p->terminated = 0;
  new_p->next = NULL;
  new_p->next_pseudo = NULL;
  new_p->stepping_mode = DO_DEFAULT;

  if (0 == thread_head.count)
    {
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("First thread, pid %d tid %d!\n", pid, tid);
#endif
      saved_real_ptid = inferior_ptid;
    }
  else
    {
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("Subsequent thread, pid %d tid %d\n", pid, tid);
#endif
    }

  /* Another day, another thread...
   */
  thread_head.count++;

  /* The new thread always goes at the head of the list.
   */
  new_p->next = thread_head.head;
  thread_head.head = new_p;

  /* Is this the "pseudo" thread of a process?  It is if there's
   * no other thread for this process on the list.  (Note that this
   * accomodates multiple processes, such as we see even for simple
   * cases like forking "non-threaded" programs.)
   */
  p = thread_head.head;
  thread_count_of_pid = 0;
  while (p)
    {
      if (p->pid == new_p->pid)
	thread_count_of_pid++;
      p = p->next;
    }

  /* Did we see any other threads for this pid?  (Recall that we just
   * added this thread to the list...)
   */
  if (thread_count_of_pid == 1)
    {
      new_p->am_pseudo = 1;
      new_p->next_pseudo = thread_head.head_pseudo;
      thread_head.head_pseudo = new_p;
    }

  return new_p;
}

/* Get rid of our thread info.
 */
static void
clear_thread_info (void)
{
  thread_info *p;
  thread_info *q;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Clearing all thread info\n");
#endif

  p = thread_head.head;
  while (p)
    {
      q = p;
      p = p->next;
      xfree (q);
    }

  thread_head.head = NULL;
  thread_head.head_pseudo = NULL;
  thread_head.count = 0;

  p = deleted_threads.head;
  while (p)
    {
      q = p;
      p = p->next;
      xfree (q);
    }

  deleted_threads.head = NULL;
  deleted_threads.head_pseudo = NULL;
  deleted_threads.count = 0;

  /* No threads, so can't have pending events.
   */
  more_events_left = 0;
}

/* Given a tid, find the thread block for it.
 */
static thread_info *
find_thread_info (lwpid_t tid)
{
  thread_info *p;

  for (p = thread_head.head; p; p = p->next)
    {
      if (p->tid == tid)
	{
	  return p;
	}
    }

  for (p = deleted_threads.head; p; p = p->next)
    {
      if (p->tid == tid)
	{
	  return p;
	}
    }

  return NULL;
}

/* For any but the pseudo thread, this maps to the
 * thread ID.  For the pseudo thread, if you pass either
 * the thread id or the PID, you get the pseudo thread ID.
 *
 * We have to be prepared for core gdb to ask about
 * deleted threads.  We do the map, but we don't like it.
 */
static lwpid_t
map_from_gdb_tid (lwpid_t gdb_tid)
{
  thread_info *p;

  /* First assume gdb_tid really is a tid, and try to find a
   * matching entry on the threads list.
   */
  for (p = thread_head.head; p; p = p->next)
    {
      if (p->tid == gdb_tid)
	return gdb_tid;
    }

  /* It doesn't appear to be a tid; perhaps it's really a pid?
   * Try to find a "pseudo" thread entry on the threads list.
   */
  for (p = thread_head.head_pseudo; p != NULL; p = p->next_pseudo)
    {
      if (p->pid == gdb_tid)
	return p->tid;
    }

  /* Perhaps it's the tid of a deleted thread we may still
   * have some knowledge of?
   */
  for (p = deleted_threads.head; p; p = p->next)
    {
      if (p->tid == gdb_tid)
	return gdb_tid;
    }

  /* Or perhaps it's the pid of a deleted process we may still
   * have knowledge of?
   */
  for (p = deleted_threads.head_pseudo; p != NULL; p = p->next_pseudo)
    {
      if (p->pid == gdb_tid)
	return p->tid;
    }

  return 0;			/* Error? */
}

/* Map the other way: from a real tid to the
 * "pid" known by core gdb.  This tid may be
 * for a thread that just got deleted, so we
 * also need to consider deleted threads.
 */
static lwpid_t
map_to_gdb_tid (lwpid_t real_tid)
{
  thread_info *p;

  for (p = thread_head.head; p; p = p->next)
    {
      if (p->tid == real_tid)
	{
	  if (p->am_pseudo)
	    return p->pid;
	  else
	    return real_tid;
	}
    }

  for (p = deleted_threads.head; p; p = p->next)
    {
      if (p->tid == real_tid)
	if (p->am_pseudo)
	  return p->pid;	/* Error? */
	else
	  return real_tid;
    }

  return 0;			/* Error?  Never heard of this thread! */
}

/* Do any threads have saved signals?
 */
static int
saved_signals_exist (void)
{
  thread_info *p;

  for (p = thread_head.head; p; p = p->next)
    {
      if (p->have_signal)
	{
	  return 1;
	}
    }

  return 0;
}

/* Is this the tid for the zero-th thread?
 */
static int
is_pseudo_thread (lwpid_t tid)
{
  thread_info *p = find_thread_info (tid);
  if (NULL == p || p->terminated)
    return 0;
  else
    return p->am_pseudo;
}

/* Is this thread terminated?
 */
static int
is_terminated (lwpid_t tid)
{
  thread_info *p = find_thread_info (tid);

  if (NULL != p)
    return p->terminated;

  return 0;
}

/* Is this pid a real PID or a TID?
 */
static int
is_process_id (int pid)
{
  lwpid_t tid;
  thread_info *tinfo;
  pid_t this_pid;
  int this_pid_count;

  /* What does PID really represent?
   */
  tid = map_from_gdb_tid (pid);
  if (tid <= 0)
    return 0;			/* Actually, is probably an error... */

  tinfo = find_thread_info (tid);

  /* Does it appear to be a true thread?
   */
  if (!tinfo->am_pseudo)
    return 0;

  /* Else, it looks like it may be a process.  See if there's any other
   * threads with the same process ID, though.  If there are, then TID
   * just happens to be the first thread of several for this process.
   */
  this_pid = tinfo->pid;
  this_pid_count = 0;
  for (tinfo = thread_head.head; tinfo; tinfo = tinfo->next)
    {
      if (tinfo->pid == this_pid)
	this_pid_count++;
    }

  return (this_pid_count == 1);
}


/* Add a thread to our info.  Prevent duplicate entries.
 */
static thread_info *
add_tthread (int pid, lwpid_t tid)
{
  thread_info *p;

  p = find_thread_info (tid);
  if (NULL == p)
    p = create_thread_info (pid, tid);

  return p;
}

/* Notice that a thread was deleted.
 */
static void
del_tthread (lwpid_t tid)
{
  thread_info *p;
  thread_info *chase;

  if (thread_head.count <= 0)
    {
      error ("Internal error in thread database.");
      return;
    }

  chase = NULL;
  for (p = thread_head.head; p; p = p->next)
    {
      if (p->tid == tid)
	{

#ifdef THREAD_DEBUG
	  if (debug_on)
	    printf ("Delete here: %d \n", tid);
#endif

	  if (p->am_pseudo)
	    {
	      /*
	       * Deleting a main thread is ok if we're doing
	       * a parent-follow on a child; this is odd but
	       * not wrong.  It apparently _doesn't_ happen
	       * on the child-follow, as we don't just delete
	       * the pseudo while keeping the rest of the
	       * threads around--instead, we clear out the whole
	       * thread list at once.
	       */
	      thread_info *q;
	      thread_info *q_chase;

	      q_chase = NULL;
	      for (q = thread_head.head_pseudo; q; q = q->next)
		{
		  if (q == p)
		    {
		      /* Remove from pseudo list.
		       */
		      if (q_chase == NULL)
			thread_head.head_pseudo = p->next_pseudo;
		      else
			q_chase->next = p->next_pseudo;
		    }
		  else
		    q_chase = q;
		}
	    }

	  /* Remove from live list.
	   */
	  thread_head.count--;

	  if (NULL == chase)
	    thread_head.head = p->next;
	  else
	    chase->next = p->next;

	  /* Add to deleted thread list.
	   */
	  p->next = deleted_threads.head;
	  deleted_threads.head = p;
	  deleted_threads.count++;
	  if (p->am_pseudo)
	    {
	      p->next_pseudo = deleted_threads.head_pseudo;
	      deleted_threads.head_pseudo = p;
	    }
	  p->terminated = 1;

	  return;
	}

      else
	chase = p;
    }
}

/* Get the pid for this tid. (Has to be a real TID!).
 */
static int
get_pid_for (lwpid_t tid)
{
  thread_info *p;

  for (p = thread_head.head; p; p = p->next)
    {
      if (p->tid == tid)
	{
	  return p->pid;
	}
    }

  for (p = deleted_threads.head; p; p = p->next)
    {
      if (p->tid == tid)
	{
	  return p->pid;
	}
    }

  return 0;
}

/* Note that this thread's current event has been handled.
 */
static void
set_handled (int pid, lwpid_t tid)
{
  thread_info *p;

  p = find_thread_info (tid);
  if (NULL == p)
    p = add_tthread (pid, tid);

  p->handled = 1;
}

/* Was this thread's current event handled?
 */
static int
was_handled (lwpid_t tid)
{
  thread_info *p;

  p = find_thread_info (tid);
  if (NULL != p)
    return p->handled;

  return 0;			/* New threads have not been handled */
}

/* Set this thread to unhandled.
 */
static void
clear_handled (lwpid_t tid)
{
  thread_info *p;

#ifdef WAIT_BUFFER_DEBUG
  if (debug_on)
    printf ("clear_handled %d\n", (int) tid);
#endif

  p = find_thread_info (tid);
  if (p == NULL)
    error ("Internal error: No thread state to clear?");

  p->handled = 0;
}

/* Set all threads to unhandled.
 */
static void
clear_all_handled (void)
{
  thread_info *p;

#ifdef WAIT_BUFFER_DEBUG
  if (debug_on)
    printf ("clear_all_handled\n");
#endif

  for (p = thread_head.head; p; p = p->next)
    {
      p->handled = 0;
    }

  for (p = deleted_threads.head; p; p = p->next)
    {
      p->handled = 0;
    }
}

/* Set this thread to default stepping mode.
 */
static void
clear_stepping_mode (lwpid_t tid)
{
  thread_info *p;

#ifdef WAIT_BUFFER_DEBUG
  if (debug_on)
    printf ("clear_stepping_mode %d\n", (int) tid);
#endif

  p = find_thread_info (tid);
  if (p == NULL)
    error ("Internal error: No thread state to clear?");

  p->stepping_mode = DO_DEFAULT;
}

/* Set all threads to do default continue on resume.
 */
static void
clear_all_stepping_mode (void)
{
  thread_info *p;

#ifdef WAIT_BUFFER_DEBUG
  if (debug_on)
    printf ("clear_all_stepping_mode\n");
#endif

  for (p = thread_head.head; p; p = p->next)
    {
      p->stepping_mode = DO_DEFAULT;
    }

  for (p = deleted_threads.head; p; p = p->next)
    {
      p->stepping_mode = DO_DEFAULT;
    }
}

/* Set all threads to unseen on this pass.
 */
static void
set_all_unseen (void)
{
  thread_info *p;

  for (p = thread_head.head; p; p = p->next)
    {
      p->seen = 0;
    }
}

#if (defined( THREAD_DEBUG ) || defined( PARANOIA ))
/* debugging routine.
 */
static void
print_tthread (thread_info *p)
{
  printf (" Thread pid %d, tid %d", p->pid, p->tid);
  if (p->have_state)
    printf (", event is %s",
	 get_printable_name_of_ttrace_event (p->last_stop_state.tts_event));

  if (p->am_pseudo)
    printf (", pseudo thread");

  if (p->have_signal)
    printf (", have signal 0x%x", p->signal_value);

  if (p->have_start)
    printf (", have start at 0x%x", p->start);

  printf (", step is %s", get_printable_name_of_stepping_mode (p->stepping_mode));

  if (p->handled)
    printf (", handled");
  else
    printf (", not handled");

  if (p->seen)
    printf (", seen");
  else
    printf (", not seen");

  printf ("\n");
}

static void
print_tthreads (void)
{
  thread_info *p;

  if (thread_head.count == 0)
    printf ("Thread list is empty\n");
  else
    {
      printf ("Thread list has ");
      if (thread_head.count == 1)
	printf ("1 entry:\n");
      else
	printf ("%d entries:\n", thread_head.count);
      for (p = thread_head.head; p; p = p->next)
	{
	  print_tthread (p);
	}
    }

  if (deleted_threads.count == 0)
    printf ("Deleted thread list is empty\n");
  else
    {
      printf ("Deleted thread list has ");
      if (deleted_threads.count == 1)
	printf ("1 entry:\n");
      else
	printf ("%d entries:\n", deleted_threads.count);

      for (p = deleted_threads.head; p; p = p->next)
	{
	  print_tthread (p);
	}
    }
}
#endif

/* Update the thread list based on the "seen" bits.
 */
static void
update_thread_list (void)
{
  thread_info *p;
  thread_info *chase;

  chase = NULL;
  for (p = thread_head.head; p; p = p->next)
    {
      /* Is this an "unseen" thread which really happens to be a process?
         If so, is it inferior_ptid and is a vfork in flight?  If yes to
         all, then DON'T REMOVE IT!  We're in the midst of moving a vfork
         operation, which is a multiple step thing, to the point where we
         can touch the parent again.  We've most likely stopped to examine
         the child at a late stage in the vfork, and if we're not following
         the child, we'd best not treat the parent as a dead "thread"...
       */
      if ((!p->seen) && p->am_pseudo && vfork_in_flight
	  && (p->pid != vforking_child_pid))
	p->seen = 1;

      if (!p->seen)
	{
	  /* Remove this one
	   */

#ifdef THREAD_DEBUG
	  if (debug_on)
	    printf ("Delete unseen thread: %d \n", p->tid);
#endif
	  del_tthread (p->tid);
	}
    }
}



/************************************************
 *            O/S call wrappers                 *
 ************************************************
 */

/* This function simply calls ttrace with the given arguments.  
 * It exists so that all calls to ttrace are isolated.  All
 * parameters should be as specified by "man 2 ttrace".
 *
 * No other "raw" calls to ttrace should exist in this module.
 */
static int
call_real_ttrace (ttreq_t request, pid_t pid, lwpid_t tid, TTRACE_ARG_TYPE addr,
		  TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
{
  int tt_status;

  errno = 0;
  tt_status = ttrace (request, pid, tid, addr, data, addr2);

#ifdef THREAD_DEBUG
  if (errno)
    {
      /* Don't bother for a known benign error: if you ask for the
       * first thread state, but there is only one thread and it's
       * not stopped, ttrace complains.
       *
       * We have this inside the #ifdef because our caller will do
       * this check for real.
       */
      if (request != TT_PROC_GET_FIRST_LWP_STATE
	  || errno != EPROTO)
	{
	  if (debug_on)
	    printf ("TT fail for %s, with pid %d, tid %d, status %d \n",
		    get_printable_name_of_ttrace_request (request),
		    pid, tid, tt_status);
	}
    }
#endif

#if 0
  /* ??rehrauer: It would probably be most robust to catch and report
   * failed requests here.  However, some clients of this interface
   * seem to expect to catch & deal with them, so we'd best not.
   */
  if (errno)
    {
      strcpy (reason_for_failure, "ttrace (");
      strcat (reason_for_failure, get_printable_name_of_ttrace_request (request));
      strcat (reason_for_failure, ")");
      printf ("ttrace error, errno = %d\n", errno);
      perror_with_name (reason_for_failure);
    }
#endif

  return tt_status;
}


/* This function simply calls ttrace_wait with the given arguments.  
 * It exists so that all calls to ttrace_wait are isolated.
 *
 * No "raw" calls to ttrace_wait should exist elsewhere.
 */
static int
call_real_ttrace_wait (int pid, lwpid_t tid, ttwopt_t option, ttstate_t *tsp,
		       size_t tsp_size)
{
  int ttw_status;
  thread_info *tinfo = NULL;

  errno = 0;
  ttw_status = ttrace_wait (pid, tid, option, tsp, tsp_size);

  if (errno)
    {
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("TW fail with pid %d, tid %d \n", pid, tid);
#endif

      perror_with_name ("ttrace wait");
    }

  return ttw_status;
}


/* A process may have one or more kernel threads, of which all or
   none may be stopped.  This function returns the ID of the first
   kernel thread in a stopped state, or 0 if none are stopped.

   This function can be used with get_process_next_stopped_thread_id
   to iterate over the IDs of all stopped threads of this process.
 */
static lwpid_t
get_process_first_stopped_thread_id (int pid, ttstate_t *thread_state)
{
  int tt_status;

  tt_status = call_real_ttrace (TT_PROC_GET_FIRST_LWP_STATE,
				(pid_t) pid,
				(lwpid_t) TT_NIL,
				(TTRACE_ARG_TYPE) thread_state,
				(TTRACE_ARG_TYPE) sizeof (*thread_state),
				TT_NIL);

  if (errno)
    {
      if (errno == EPROTO)
	{
	  /* This is an error we can handle: there isn't any stopped
	   * thread.  This happens when we're re-starting the application
	   * and it has only one thread.  GET_NEXT handles the case of
	   * no more stopped threads well; GET_FIRST doesn't.  (A ttrace
	   * "feature".)
	   */
	  tt_status = 1;
	  errno = 0;
	  return 0;
	}
      else
	perror_with_name ("ttrace");
    }

  if (tt_status < 0)
    /* Failed somehow.
     */
    return 0;

  return thread_state->tts_lwpid;
}


/* This function returns the ID of the "next" kernel thread in a
   stopped state, or 0 if there are none.  "Next" refers to the
   thread following that of the last successful call to this
   function or to get_process_first_stopped_thread_id, using
   the value of thread_state returned by that call.

   This function can be used with get_process_first_stopped_thread_id
   to iterate over the IDs of all stopped threads of this process.
 */
static lwpid_t
get_process_next_stopped_thread_id (int pid, ttstate_t *thread_state)
{
  int tt_status;

  tt_status = call_real_ttrace (
				 TT_PROC_GET_NEXT_LWP_STATE,
				 (pid_t) pid,
				 (lwpid_t) TT_NIL,
				 (TTRACE_ARG_TYPE) thread_state,
				 (TTRACE_ARG_TYPE) sizeof (*thread_state),
				 TT_NIL);
  if (errno)
    perror_with_name ("ttrace");

  if (tt_status < 0)
    /* Failed
     */
    return 0;

  else if (tt_status == 0)
    {
      /* End of list, no next state.  Don't return the
       * tts_lwpid, as it's a meaningless "240".
       *
       * This is an HPUX "feature".
       */
      return 0;
    }

  return thread_state->tts_lwpid;
}

/* ??rehrauer: Eventually this function perhaps should be calling
   pid_to_thread_id.  However, that function currently does nothing
   for HP-UX.  Even then, I'm not clear whether that function
   will return a "kernel" thread ID, or a "user" thread ID.  If
   the former, we can just call it here.  If the latter, we must
   map from the "user" tid to a "kernel" tid.

   NOTE: currently not called.
 */
static lwpid_t
get_active_tid_of_pid (int pid)
{
  ttstate_t thread_state;

  return get_process_first_stopped_thread_id (pid, &thread_state);
}

/* This function returns 1 if tt_request is a ttrace request that
 * operates upon all threads of a (i.e., the entire) process.
 */
int
is_process_ttrace_request (ttreq_t tt_request)
{
  return IS_TTRACE_PROCREQ (tt_request);
}


/* This function translates a thread ttrace request into
 * the equivalent process request for a one-thread process.
 */
static ttreq_t
make_process_version (ttreq_t request)
{
  if (!IS_TTRACE_REQ (request))
    {
      error ("Internal error, bad ttrace request made\n");
      return -1;
    }

  switch (request)
    {
    case TT_LWP_STOP:
      return TT_PROC_STOP;

    case TT_LWP_CONTINUE:
      return TT_PROC_CONTINUE;

    case TT_LWP_GET_EVENT_MASK:
      return TT_PROC_GET_EVENT_MASK;

    case TT_LWP_SET_EVENT_MASK:
      return TT_PROC_SET_EVENT_MASK;

    case TT_LWP_SINGLE:
    case TT_LWP_RUREGS:
    case TT_LWP_WUREGS:
    case TT_LWP_GET_STATE:
      return -1;		/* No equivalent */

    default:
      return request;
    }
}


/* This function translates the "pid" used by the rest of
 * gdb to a real pid and a tid.  It then calls "call_real_ttrace"
 * with the given arguments.
 *
 * In general, other parts of this module should call this
 * function when they are dealing with external users, who only
 * have tids to pass (but they call it "pid" for historical
 * reasons).
 */
static int
call_ttrace (ttreq_t request, int gdb_tid, TTRACE_ARG_TYPE addr,
	     TTRACE_ARG_TYPE data, TTRACE_ARG_TYPE addr2)
{
  lwpid_t real_tid;
  int real_pid;
  ttreq_t new_request;
  int tt_status;
  char reason_for_failure[100];	/* Arbitrary size, should be big enough. */

#ifdef THREAD_DEBUG
  int is_interesting = 0;

  if (TT_LWP_RUREGS == request)
    {
      is_interesting = 1;	/* Adjust code here as desired */
    }

  if (is_interesting && 0 && debug_on)
    {
      if (!is_process_ttrace_request (request))
	{
	  printf ("TT: Thread request, tid is %d", gdb_tid);
	  printf ("== SINGLE at %x", addr);
	}
      else
	{
	  printf ("TT: Process request, tid is %d\n", gdb_tid);
	  printf ("==! SINGLE at %x", addr);
	}
    }
#endif

  /* The initial SETTRC and SET_EVENT_MASK calls (and all others
   * which happen before any threads get set up) should go
   * directly to "call_real_ttrace", so they don't happen here.
   *
   * But hardware watchpoints do a SET_EVENT_MASK, so we can't
   * rule them out....
   */
#ifdef THREAD_DEBUG
  if (request == TT_PROC_SETTRC && debug_on)
    printf ("Unexpected call for TT_PROC_SETTRC\n");
#endif

  /* Sometimes we get called with a bogus tid (e.g., if a
   * thread has terminated, we return 0; inftarg later asks
   * whether the thread has exited/forked/vforked).
   */
  if (gdb_tid == 0)
    {
      errno = ESRCH;		/* ttrace's response would probably be "No such process". */
      return -1;
    }

  /* All other cases should be able to expect that there are
   * thread records.
   */
  if (!any_thread_records ())
    {
#ifdef THREAD_DEBUG
      if (debug_on)
	warning ("No thread records for ttrace call");
#endif
      errno = ESRCH;		/* ttrace's response would be "No such process". */
      return -1;
    }

  /* OK, now the task is to translate the incoming tid into
   * a pid/tid pair.
   */
  real_tid = map_from_gdb_tid (gdb_tid);
  real_pid = get_pid_for (real_tid);

  /* Now check the result.  "Real_pid" is NULL if our list
   * didn't find it.  We have some tricks we can play to fix
   * this, however.
   */
  if (0 == real_pid)
    {
      ttstate_t thread_state;

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("No saved pid for tid %d\n", gdb_tid);
#endif

      if (is_process_ttrace_request (request))
	{

	  /* Ok, we couldn't get a tid.  Try to translate to
	   * the equivalent process operation.  We expect this
	   * NOT to happen, so this is a desparation-type
	   * move.  It can happen if there is an internal
	   * error and so no "wait()" call is ever done.
	   */
	  new_request = make_process_version (request);
	  if (new_request == -1)
	    {

#ifdef THREAD_DEBUG
	      if (debug_on)
		printf ("...and couldn't make process version of thread operation\n");
#endif

	      /* Use hacky saved pid, which won't always be correct
	       * in the multi-process future.  Use tid as thread,
	       * probably dooming this to failure.  FIX!
	       */
	      if (! ptid_equal (saved_real_ptid, null_ptid))
		{
#ifdef THREAD_DEBUG
		  if (debug_on)
		    printf ("...using saved pid %d\n",
		            PIDGET (saved_real_ptid));
#endif

		  real_pid = PIDGET (saved_real_ptid);
		  real_tid = gdb_tid;
		}

	      else
		error ("Unable to perform thread operation");
	    }

	  else
	    {
	      /* Sucessfully translated this to a process request,
	       * which needs no thread value.
	       */
	      real_pid = gdb_tid;
	      real_tid = 0;
	      request = new_request;

#ifdef THREAD_DEBUG
	      if (debug_on)
		{
		  printf ("Translated thread request to process request\n");
		  if (ptid_equal (saved_real_ptid, null_ptid))
		    printf ("...but there's no saved pid\n");

		  else
		    {
		      if (gdb_tid != PIDGET (saved_real_ptid))
			printf ("...but have the wrong pid (%d rather than %d)\n",
				gdb_tid, PIDGET (saved_real_ptid));
		    }
		}
#endif
	    }			/* Translated to a process request */
	}			/* Is a process request */

      else
	{
	  /* We have to have a thread.  Ooops.
	   */
	  error ("Thread request with no threads (%s)",
		 get_printable_name_of_ttrace_request (request));
	}
    }

  /* Ttrace doesn't like to see tid values on process requests,
   * even if we have the right one.
   */
  if (is_process_ttrace_request (request))
    {
      real_tid = 0;
    }

#ifdef THREAD_DEBUG
  if (is_interesting && 0 && debug_on)
    {
      printf ("    now tid %d, pid %d\n", real_tid, real_pid);
      printf ("    request is %s\n", get_printable_name_of_ttrace_request (request));
    }
#endif

  /* Finally, the (almost) real call.
   */
  tt_status = call_real_ttrace (request, real_pid, real_tid, addr, data, addr2);

#ifdef THREAD_DEBUG
  if (is_interesting && debug_on)
    {
      if (!TT_OK (tt_status, errno)
	  && !(tt_status == 0 & errno == 0))
	printf (" got error (errno==%d, status==%d)\n", errno, tt_status);
    }
#endif

  return tt_status;
}


/* Stop all the threads of a process.

 * NOTE: use of TT_PROC_STOP can cause a thread with a real event
 *       to get a TTEVT_NONE event, discarding the old event.  Be
 *       very careful, and only call TT_PROC_STOP when you mean it!
 */
static void
stop_all_threads_of_process (pid_t real_pid)
{
  int ttw_status;

  ttw_status = call_real_ttrace (TT_PROC_STOP,
				 (pid_t) real_pid,
				 (lwpid_t) TT_NIL,
				 (TTRACE_ARG_TYPE) TT_NIL,
				 (TTRACE_ARG_TYPE) TT_NIL,
				 TT_NIL);
  if (errno)
    perror_with_name ("ttrace stop of other threads");
}


/* Under some circumstances, it's unsafe to attempt to stop, or even
   query the state of, a process' threads.

   In ttrace-based HP-UX, an example is a vforking child process.  The
   vforking parent and child are somewhat fragile, w/r/t what we can do
   what we can do to them with ttrace, until after the child exits or
   execs, or until the parent's vfork event is delivered.  Until that
   time, we must not try to stop the process' threads, or inquire how
   many there are, or even alter its data segments, or it typically dies
   with a SIGILL.  Sigh.

   This function returns 1 if this stopped process, and the event that
   we're told was responsible for its current stopped state, cannot safely
   have its threads examined.
 */
#define CHILD_VFORKED(evt,pid) \
  (((evt) == TTEVT_VFORK) && ((pid) != PIDGET (inferior_ptid)))
#define CHILD_URPED(evt,pid) \
  ((((evt) == TTEVT_EXEC) || ((evt) == TTEVT_EXIT)) && ((pid) != vforking_child_pid))
#define PARENT_VFORKED(evt,pid) \
  (((evt) == TTEVT_VFORK) && ((pid) == PIDGET (inferior_ptid)))

static int
can_touch_threads_of_process (int pid, ttevents_t stopping_event)
{
  if (CHILD_VFORKED (stopping_event, pid))
    {
      vforking_child_pid = pid;
      vfork_in_flight = 1;
    }

  else if (vfork_in_flight &&
	   (PARENT_VFORKED (stopping_event, pid) ||
	    CHILD_URPED (stopping_event, pid)))
    {
      vfork_in_flight = 0;
      vforking_child_pid = 0;
    }

  return !vfork_in_flight;
}


/* If we can find an as-yet-unhandled thread state of a
 * stopped thread of this process return 1 and set "tsp".
 * Return 0 if we can't.
 *
 * If this function is used when the threads of PIS haven't
 * been stopped, undefined behaviour is guaranteed!
 */
static int
select_stopped_thread_of_process (int pid, ttstate_t *tsp)
{
  lwpid_t candidate_tid, tid;
  ttstate_t candidate_tstate, tstate;

  /* If we're not allowed to touch the process now, then just
   * return the current value of *TSP.
   *
   * This supports "vfork".  It's ok, really, to double the
   * current event (the child EXEC, we hope!).
   */
  if (!can_touch_threads_of_process (pid, tsp->tts_event))
    return 1;

  /* Decide which of (possibly more than one) events to
   * return as the first one.  We scan them all so that
   * we always return the result of a fake-step first.
   */
  candidate_tid = 0;
  for (tid = get_process_first_stopped_thread_id (pid, &tstate);
       tid != 0;
       tid = get_process_next_stopped_thread_id (pid, &tstate))
    {
      /* TTEVT_NONE events are uninteresting to our clients.  They're
       * an artifact of our "stop the world" model--the thread is
       * stopped because we stopped it.
       */
      if (tstate.tts_event == TTEVT_NONE)
	{
	  set_handled (pid, tstate.tts_lwpid);
	}

      /* Did we just single-step a single thread, without letting any
       * of the others run?  Is this an event for that thread?
       *
       * If so, we believe our client would prefer to see this event
       * over any others.  (Typically the client wants to just push
       * one thread a little farther forward, and then go around
       * checking for what all threads are doing.)
       */
      else if (doing_fake_step && (tstate.tts_lwpid == fake_step_tid))
	{
#ifdef WAIT_BUFFER_DEBUG
	  /* It's possible here to see either a SIGTRAP (due to
	   * successful completion of a step) or a SYSCALL_ENTRY
	   * (due to a step completion with active hardware
	   * watchpoints).
	   */
	  if (debug_on)
	    printf ("Ending fake step with tid %d, state %s\n",
		    tstate.tts_lwpid,
		    get_printable_name_of_ttrace_event (tstate.tts_event));
#endif

	  /* Remember this one, and throw away any previous
	   * candidate.
	   */
	  candidate_tid = tstate.tts_lwpid;
	  candidate_tstate = tstate;
	}

#ifdef FORGET_DELETED_BPTS

      /* We can't just do this, as if we do, and then wind
       * up the loop with no unhandled events, we need to
       * handle that case--the appropriate reaction is to
       * just continue, but there's no easy way to do that.
       *
       * Better to put this in the ttrace_wait call--if, when
       * we fake a wait, we update our events based on the
       * breakpoint_here_pc call and find there are no more events,
       * then we better continue and so on.
       *
       * Or we could put it in the next/continue fake.
       * But it has to go in the buffering code, not in the
       * real go/wait code.
       */
      else if ((TTEVT_SIGNAL == tstate.tts_event)
	       && (5 == tstate.tts_u.tts_signal.tts_signo)
	       && (0 != get_raw_pc (tstate.tts_lwpid))
	       && !breakpoint_here_p (get_raw_pc (tstate.tts_lwpid)))
	{
	  /*
	   * If the user deleted a breakpoint while this
	   * breakpoint-hit event was buffered, we can forget
	   * it now.
	   */
#ifdef WAIT_BUFFER_DEBUG
	  if (debug_on)
	    printf ("Forgetting deleted bp hit for thread %d\n",
		    tstate.tts_lwpid);
#endif

	  set_handled (pid, tstate.tts_lwpid);
	}
#endif

      /* Else, is this the first "unhandled" event?  If so,
       * we believe our client wants to see it (if we don't
       * see a fake-step later on in the scan).
       */
      else if (!was_handled (tstate.tts_lwpid) && candidate_tid == 0)
	{
	  candidate_tid = tstate.tts_lwpid;
	  candidate_tstate = tstate;
	}

      /* This is either an event that has already been "handled",
       * and thus we believe is uninteresting to our client, or we
       * already have a candidate event.  Ignore it...
       */
    }

  /* What do we report?
   */
  if (doing_fake_step)
    {
      if (candidate_tid == fake_step_tid)
	{
	  /* Fake step.
	   */
	  tstate = candidate_tstate;
	}
      else
	{
	  warning ("Internal error: fake-step failed to complete.");
	  return 0;
	}
    }
  else if (candidate_tid != 0)
    {
      /* Found a candidate unhandled event.
       */
      tstate = candidate_tstate;
    }
  else if (tid != 0)
    {
      warning ("Internal error in call of ttrace_wait.");
      return 0;
    }
  else
    {
      warning ("Internal error: no unhandled thread event to select");
      return 0;
    }

  copy_ttstate_t (tsp, &tstate);
  return 1;
}				/* End of select_stopped_thread_of_process */

#ifdef PARANOIA
/* Check our internal thread data against the real thing.
 */
static void
check_thread_consistency (pid_t real_pid)
{
  int tid;			/* really lwpid_t */
  ttstate_t tstate;
  thread_info *p;

  /* Spin down the O/S list of threads, checking that they
   * match what we've got.
   */
  for (tid = get_process_first_stopped_thread_id (real_pid, &tstate);
       tid != 0;
       tid = get_process_next_stopped_thread_id (real_pid, &tstate))
    {

      p = find_thread_info (tid);

      if (NULL == p)
	{
	  warning ("No internal thread data for thread %d.", tid);
	  continue;
	}

      if (!p->seen)
	{
	  warning ("Inconsistent internal thread data for thread %d.", tid);
	}

      if (p->terminated)
	{
	  warning ("Thread %d is not terminated, internal error.", tid);
	  continue;
	}


#define TT_COMPARE( fld ) \
            tstate.fld != p->last_stop_state.fld

      if (p->have_state)
	{
	  if (TT_COMPARE (tts_pid)
	      || TT_COMPARE (tts_lwpid)
	      || TT_COMPARE (tts_user_tid)
	      || TT_COMPARE (tts_event)
	      || TT_COMPARE (tts_flags)
	      || TT_COMPARE (tts_scno)
	      || TT_COMPARE (tts_scnargs))
	    {
	      warning ("Internal thread data for thread %d is wrong.", tid);
	      continue;
	    }
	}
    }
}
#endif /* PARANOIA */


/* This function wraps calls to "call_real_ttrace_wait" so
 * that a actual wait is only done when all pending events
 * have been reported.
 *
 * Note that typically it is called with a pid of "0", i.e. 
 * the "don't care" value.
 *
 * Return value is the status of the pseudo wait.
 */
static int
call_ttrace_wait (int pid, ttwopt_t option, ttstate_t *tsp, size_t tsp_size)
{
  /* This holds the actual, for-real, true process ID.
   */
  static int real_pid;

  /* As an argument to ttrace_wait, zero pid
   * means "Any process", and zero tid means
   * "Any thread of the specified process".
   */
  int wait_pid = 0;
  lwpid_t wait_tid = 0;
  lwpid_t real_tid;

  int ttw_status = 0;		/* To be returned */

  thread_info *tinfo = NULL;

  if (pid != 0)
    {
      /* Unexpected case.
       */
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("TW: Pid to wait on is %d\n", pid);
#endif

      if (!any_thread_records ())
	error ("No thread records for ttrace call w. specific pid");

      /* OK, now the task is to translate the incoming tid into
       * a pid/tid pair.
       */
      real_tid = map_from_gdb_tid (pid);
      real_pid = get_pid_for (real_tid);
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("==TW: real pid %d, real tid %d\n", real_pid, real_tid);
#endif
    }


  /* Sanity checks and set-up.
   *                             Process State
   *
   *                        Stopped   Running    Fake-step  (v)Fork
   *                      \________________________________________
   *                      |
   *  No buffered events  |  error     wait       wait      wait
   *                      |
   *  Buffered events     |  debuffer  error      wait      debuffer (?)
   *
   */
  if (more_events_left == 0)
    {

      if (process_state == RUNNING)
	{
	  /* OK--normal call of ttrace_wait with no buffered events.
	   */
	  ;
	}
      else if (process_state == FAKE_STEPPING)
	{
	  /* Ok--call of ttrace_wait to support
	   * fake stepping with no buffered events.
	   *
	   * But we better be fake-stepping!
	   */
	  if (!doing_fake_step)
	    {
	      warning ("Inconsistent thread state.");
	    }
	}
      else if ((process_state == FORKING)
	       || (process_state == VFORKING))
	{
	  /* Ok--there are two processes, so waiting
	   * for the second while the first is stopped
	   * is ok.  Handled bits stay as they were.
	   */
	  ;
	}
      else if (process_state == STOPPED)
	{
	  warning ("Process not running at wait call.");
	}
      else
	/* No known state.
	 */
	warning ("Inconsistent process state.");
    }

  else
    {
      /* More events left
       */
      if (process_state == STOPPED)
	{
	  /* OK--buffered events being unbuffered.
	   */
	  ;
	}
      else if (process_state == RUNNING)
	{
	  /* An error--shouldn't have buffered events
	   * when running.
	   */
	  warning ("Trying to continue with buffered events:");
	}
      else if (process_state == FAKE_STEPPING)
	{
	  /*
	   * Better be fake-stepping!
	   */
	  if (!doing_fake_step)
	    {
	      warning ("Losing buffered thread events!\n");
	    }
	}
      else if ((process_state == FORKING)
	       || (process_state == VFORKING))
	{
	  /* Ok--there are two processes, so waiting
	   * for the second while the first is stopped
	   * is ok.  Handled bits stay as they were.
	   */
	  ;
	}
      else
	warning ("Process in unknown state with buffered events.");
    }

  /* Sometimes we have to wait for a particular thread
   * (if we're stepping over a bpt).  In that case, we
   * _know_ it's going to complete the single-step we
   * asked for (because we're only doing the step under
   * certain very well-understood circumstances), so it
   * can't block.
   */
  if (doing_fake_step)
    {
      wait_tid = fake_step_tid;
      wait_pid = get_pid_for (fake_step_tid);

#ifdef WAIT_BUFFER_DEBUG
      if (debug_on)
	printf ("Doing a wait after a fake-step for %d, pid %d\n",
		wait_tid, wait_pid);
#endif
    }

  if (more_events_left == 0	/* No buffered events, need real ones. */
      || process_state != STOPPED)
    {
      /* If there are no buffered events, and so we need
       * real ones, or if we are FORKING, VFORKING, 
       * FAKE_STEPPING or RUNNING, and thus have to do
       * a real wait, then do a real wait.
       */

#ifdef WAIT_BUFFER_DEBUG
      /* Normal case... */
      if (debug_on)
	printf ("TW: do it for real; pid %d, tid %d\n", wait_pid, wait_tid);
#endif

      /* The actual wait call.
       */
      ttw_status = call_real_ttrace_wait (wait_pid, wait_tid, option, tsp, tsp_size);

      /* Note that the routines we'll call will be using "call_real_ttrace",
       * not "call_ttrace", and thus need the real pid rather than the pseudo-tid
       * the rest of the world uses (which is actually the tid).
       */
      real_pid = tsp->tts_pid;

      /* For most events: Stop the world!

       * It's sometimes not safe to stop all threads of a process.
       * Sometimes it's not even safe to ask for the thread state
       * of a process!
       */
      if (can_touch_threads_of_process (real_pid, tsp->tts_event))
	{
	  /* If we're really only stepping a single thread, then don't
	   * try to stop all the others -- we only do this single-stepping
	   * business when all others were already stopped...and the stop
	   * would mess up other threads' events.
	   *
	   * Similiarly, if there are other threads with events,
	   * don't do the stop.
	   */
	  if (!doing_fake_step)
	    {
	      if (more_events_left > 0)
		warning ("Internal error in stopping process");

	      stop_all_threads_of_process (real_pid);

	      /* At this point, we could scan and update_thread_list(),
	       * and only use the local list for the rest of the
	       * module! We'd get rid of the scans in the various
	       * continue routines (adding one in attach).  It'd
	       * be great--UPGRADE ME!
	       */
	    }
	}

#ifdef PARANOIA
      else if (debug_on)
	{
	  if (more_events_left > 0)
	    printf ("== Can't stop process; more events!\n");
	  else
	    printf ("== Can't stop process!\n");
	}
#endif

      process_state = STOPPED;

#ifdef WAIT_BUFFER_DEBUG
      if (debug_on)
	printf ("Process set to STOPPED\n");
#endif
    }

  else
    {
      /* Fake a call to ttrace_wait.  The process must be
       * STOPPED, as we aren't going to do any wait.
       */
#ifdef WAIT_BUFFER_DEBUG
      if (debug_on)
	printf ("TW: fake it\n");
#endif

      if (process_state != STOPPED)
	{
	  warning ("Process not stopped at wait call, in state '%s'.\n",
		   get_printable_name_of_process_state (process_state));
	}

      if (doing_fake_step)
	error ("Internal error in stepping over breakpoint");

      ttw_status = 0;		/* Faking it is always successful! */
    }				/* End of fake or not? if */

  /* Pick an event to pass to our caller.  Be paranoid.
   */
  if (!select_stopped_thread_of_process (real_pid, tsp))
    warning ("Can't find event, using previous event.");

  else if (tsp->tts_event == TTEVT_NONE)
    warning ("Internal error: no thread has a real event.");

  else if (doing_fake_step)
    {
      if (fake_step_tid != tsp->tts_lwpid)
	warning ("Internal error in stepping over breakpoint.");

      /* This wait clears the (current) fake-step if there was one.
       */
      doing_fake_step = 0;
      fake_step_tid = 0;
    }

  /* We now have a correct tsp and ttw_status for the thread
   * which we want to report.  So it's "handled"!  This call
   * will add it to our list if it's not there already.
   */
  set_handled (real_pid, tsp->tts_lwpid);

  /* Save a copy of the ttrace state of this thread, in our local
     thread descriptor.

     This caches the state.  The implementation of queries like
     hpux_has_execd can then use this cached state, rather than
     be forced to make an explicit ttrace call to get it.

     (Guard against the condition that this is the first time we've
     waited on, i.e., seen this thread, and so haven't yet entered
     it into our list of threads.)
   */
  tinfo = find_thread_info (tsp->tts_lwpid);
  if (tinfo != NULL)
    {
      copy_ttstate_t (&tinfo->last_stop_state, tsp);
      tinfo->have_state = 1;
    }

  return ttw_status;
}				/* call_ttrace_wait */

#if defined(CHILD_REPORTED_EXEC_EVENTS_PER_EXEC_CALL)
int
child_reported_exec_events_per_exec_call (void)
{
  return 1;			/* ttrace reports the event once per call. */
}
#endif



/* Our implementation of hardware watchpoints involves making memory
   pages write-protected.  We must remember a page's original permissions,
   and we must also know when it is appropriate to restore a page's
   permissions to its original state.

   We use a "dictionary" of hardware-watched pages to do this.  Each
   hardware-watched page is recorded in the dictionary.  Each page's
   dictionary entry contains the original permissions and a reference
   count.  Pages are hashed into the dictionary by their start address.

   When hardware watchpoint is set on page X for the first time, page X
   is added to the dictionary with a reference count of 1.  If other
   hardware watchpoints are subsequently set on page X, its reference
   count is incremented.  When hardware watchpoints are removed from
   page X, its reference count is decremented.  If a page's reference
   count drops to 0, it's permissions are restored and the page's entry
   is thrown out of the dictionary.
 */
typedef struct memory_page
{
  CORE_ADDR page_start;
  int reference_count;
  int original_permissions;
  struct memory_page *next;
  struct memory_page *previous;
}
memory_page_t;

#define MEMORY_PAGE_DICTIONARY_BUCKET_COUNT  128

static struct
  {
    LONGEST page_count;
    int page_size;
    int page_protections_allowed;
    /* These are just the heads of chains of actual page descriptors. */
    memory_page_t buckets[MEMORY_PAGE_DICTIONARY_BUCKET_COUNT];
  }
memory_page_dictionary;


static void
require_memory_page_dictionary (void)
{
  int i;

  /* Is the memory page dictionary ready for use?  If so, we're done. */
  if (memory_page_dictionary.page_count >= (LONGEST) 0)
    return;

  /* Else, initialize it. */
  memory_page_dictionary.page_count = (LONGEST) 0;

  for (i = 0; i < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; i++)
    {
      memory_page_dictionary.buckets[i].page_start = (CORE_ADDR) 0;
      memory_page_dictionary.buckets[i].reference_count = 0;
      memory_page_dictionary.buckets[i].next = NULL;
      memory_page_dictionary.buckets[i].previous = NULL;
    }
}


static void
retire_memory_page_dictionary (void)
{
  memory_page_dictionary.page_count = (LONGEST) - 1;
}


/* Write-protect the memory page that starts at this address.

   Returns the original permissions of the page.
 */
static int
write_protect_page (int pid, CORE_ADDR page_start)
{
  int tt_status;
  int original_permissions;
  int new_permissions;

  tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
			   pid,
			   (TTRACE_ARG_TYPE) page_start,
			   TT_NIL,
			   (TTRACE_ARG_TYPE) & original_permissions);
  if (errno || (tt_status < 0))
    {
      return 0;			/* What else can we do? */
    }

  /* We'll also write-protect the page now, if that's allowed. */
  if (memory_page_dictionary.page_protections_allowed)
    {
      new_permissions = original_permissions & ~PROT_WRITE;
      tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
			       pid,
			       (TTRACE_ARG_TYPE) page_start,
			 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
			       (TTRACE_ARG_TYPE) new_permissions);
      if (errno || (tt_status < 0))
	{
	  return 0;		/* What else can we do? */
	}
    }

  return original_permissions;
}


/* Unwrite-protect the memory page that starts at this address, restoring
   (what we must assume are) its original permissions.
 */
static void
unwrite_protect_page (int pid, CORE_ADDR page_start, int original_permissions)
{
  int tt_status;

  tt_status = call_ttrace (TT_PROC_SET_MPROTECT,
			   pid,
			   (TTRACE_ARG_TYPE) page_start,
			 (TTRACE_ARG_TYPE) memory_page_dictionary.page_size,
			   (TTRACE_ARG_TYPE) original_permissions);
  if (errno || (tt_status < 0))
    {
      return;			/* What else can we do? */
    }
}


/* Memory page-protections are used to implement "hardware" watchpoints
   on HP-UX.

   For every memory page that is currently being watched (i.e., that
   presently should be write-protected), write-protect it.
 */
void
hppa_enable_page_protection_events (int pid)
{
  int bucket;

  memory_page_dictionary.page_protections_allowed = 1;

  for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
    {
      memory_page_t *page;

      page = memory_page_dictionary.buckets[bucket].next;
      while (page != NULL)
	{
	  page->original_permissions = write_protect_page (pid, page->page_start);
	  page = page->next;
	}
    }
}


/* Memory page-protections are used to implement "hardware" watchpoints
   on HP-UX.

   For every memory page that is currently being watched (i.e., that
   presently is or should be write-protected), un-write-protect it.
 */
void
hppa_disable_page_protection_events (int pid)
{
  int bucket;

  for (bucket = 0; bucket < MEMORY_PAGE_DICTIONARY_BUCKET_COUNT; bucket++)
    {
      memory_page_t *page;

      page = memory_page_dictionary.buckets[bucket].next;
      while (page != NULL)
	{
	  unwrite_protect_page (pid, page->page_start, page->original_permissions);
	  page = page->next;
	}
    }

  memory_page_dictionary.page_protections_allowed = 0;
}

/* Count the number of outstanding events.  At this
 * point, we have selected one thread and its event
 * as the one to be "reported" upwards to core gdb.
 * That thread is already marked as "handled".
 *
 * Note: we could just scan our own thread list.  FIXME!
 */
static int
count_unhandled_events (int real_pid, lwpid_t real_tid)
{
  ttstate_t tstate;
  lwpid_t ttid;
  int events_left;

  /* Ok, find out how many threads have real events to report.
   */
  events_left = 0;
  ttid = get_process_first_stopped_thread_id (real_pid, &tstate);

#ifdef THREAD_DEBUG
  if (debug_on)
    {
      if (ttid == 0)
	printf ("Process %d has no threads\n", real_pid);
      else
	printf ("Process %d has these threads:\n", real_pid);
    }
#endif

  while (ttid > 0)
    {
      if (tstate.tts_event != TTEVT_NONE
	  && !was_handled (ttid))
	{
	  /* TTEVT_NONE implies we just stopped it ourselves
	   * because we're the stop-the-world guys, so it's
	   * not an event from our point of view.
	   *
	   * If "was_handled" is true, this is an event we
	   * already handled, so don't count it.
	   *
	   * Note that we don't count the thread with the
	   * currently-reported event, as it's already marked
	   * as handled.
	   */
	  events_left++;
	}

#if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
      if (debug_on)
	{
	  if (ttid == real_tid)
	    printf ("*");	/* Thread we're reporting */
	  else
	    printf (" ");

	  if (tstate.tts_event != TTEVT_NONE)
	    printf ("+");	/* Thread with a real event */
	  else
	    printf (" ");

	  if (was_handled (ttid))
	    printf ("h");	/* Thread has been handled */
	  else
	    printf (" ");

	  printf (" %d, with event %s", ttid,
		  get_printable_name_of_ttrace_event (tstate.tts_event));

	  if (tstate.tts_event == TTEVT_SIGNAL
	      && 5 == tstate.tts_u.tts_signal.tts_signo)
	    {
	      CORE_ADDR pc_val;

	      pc_val = get_raw_pc (ttid);

	      if (pc_val > 0)
		printf (" breakpoint at 0x%x\n", pc_val);
	      else
		printf (" bpt, can't fetch pc.\n");
	    }
	  else
	    printf ("\n");
	}
#endif

      ttid = get_process_next_stopped_thread_id (real_pid, &tstate);
    }

#if defined( THREAD_DEBUG ) || defined( WAIT_BUFFER_DEBUG )
  if (debug_on)
    if (events_left > 0)
      printf ("There are thus %d pending events\n", events_left);
#endif

  return events_left;
}

/* This function is provided as a sop to clients that are calling
 * ptrace_wait to wait for a process to stop.  (see the
 * implementation of child_wait.)  Return value is the pid for
 * the event that ended the wait.
 *
 * Note: used by core gdb and so uses the pseudo-pid (really tid).
 */
int
ptrace_wait (ptid_t ptid, int *status)
{
  ttstate_t tsp;
  int ttwait_return;
  int real_pid;
  ttstate_t state;
  lwpid_t real_tid;
  int return_pid;

  /* The ptrace implementation of this also ignores pid.
   */
  *status = 0;

  ttwait_return = call_ttrace_wait (0, TTRACE_WAITOK, &tsp, sizeof (tsp));
  if (ttwait_return < 0)
    {
      /* ??rehrauer: It appears that if our inferior exits and we
         haven't asked for exit events, that we're not getting any
         indication save a negative return from ttrace_wait and an
         errno set to ESRCH?
       */
      if (errno == ESRCH)
	{
	  *status = 0;		/* WIFEXITED */
	  return PIDGET (inferior_ptid);
	}

      warning ("Call of ttrace_wait returned with errno %d.",
	       errno);
      *status = ttwait_return;
      return PIDGET (inferior_ptid);
    }

  real_pid = tsp.tts_pid;
  real_tid = tsp.tts_lwpid;

  /* One complication is that the "tts_event" structure has
   * a set of flags, and more than one can be set.  So we
   * either have to force an order (as we do here), or handle
   * more than one flag at a time.
   */
  if (tsp.tts_event & TTEVT_LWP_CREATE)
    {

      /* Unlike what you might expect, this event is reported in
       * the _creating_ thread, and the _created_ thread (whose tid
       * we have) is still running.  So we have to stop it.  This
       * has already been done in "call_ttrace_wait", but should we
       * ever abandon the "stop-the-world" model, here's the command
       * to use:
       *
       *    call_ttrace( TT_LWP_STOP, real_tid, TT_NIL, TT_NIL, TT_NIL );
       *
       * Note that this would depend on being called _after_ "add_tthread"
       * below for the tid-to-pid translation to be done in "call_ttrace".
       */

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("New thread: pid %d, tid %d, creator tid %d\n",
		real_pid, tsp.tts_u.tts_thread.tts_target_lwpid,
		real_tid);
#endif

      /* Now we have to return the tid of the created thread, not
       * the creating thread, or "wait_for_inferior" won't know we
       * have a new "process" (thread).  Plus we should record it
       * right, too.
       */
      real_tid = tsp.tts_u.tts_thread.tts_target_lwpid;

      add_tthread (real_pid, real_tid);
    }

  else if ((tsp.tts_event & TTEVT_LWP_TERMINATE)
	   || (tsp.tts_event & TTEVT_LWP_EXIT))
    {

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("Thread dies: %d\n", real_tid);
#endif

      del_tthread (real_tid);
    }

  else if (tsp.tts_event & TTEVT_EXEC)
    {

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("Pid %d has zero'th thread %d; inferior pid is %d\n",
		real_pid, real_tid, PIDGET (inferior_ptid));
#endif

      add_tthread (real_pid, real_tid);
    }

#ifdef THREAD_DEBUG
  else if (debug_on)
    {
      printf ("Process-level event %s, using tid %d\n",
	      get_printable_name_of_ttrace_event (tsp.tts_event),
	      real_tid);

      /* OK to do this, as "add_tthread" won't add
       * duplicate entries.  Also OK not to do it,
       * as this event isn't one which can change the
       * thread state.
       */
      add_tthread (real_pid, real_tid);
    }
#endif


  /* How many events are left to report later?
   * In a non-stop-the-world model, this isn't needed.
   *
   * Note that it's not always safe to query the thread state of a process,
   * which is what count_unhandled_events does.  (If unsafe, we're left with
   * no other resort than to assume that no more events remain...)
   */
  if (can_touch_threads_of_process (real_pid, tsp.tts_event))
    more_events_left = count_unhandled_events (real_pid, real_tid);

  else
    {
      if (more_events_left > 0)
	warning ("Vfork or fork causing loss of %d buffered events.",
		 more_events_left);

      more_events_left = 0;
    }

  /* Attempt to translate the ttrace_wait-returned status into the
     ptrace equivalent.

     ??rehrauer: This is somewhat fragile.  We really ought to rewrite
     clients that expect to pick apart a ptrace wait status, to use
     something a little more abstract.
   */
  if ((tsp.tts_event & TTEVT_EXEC)
      || (tsp.tts_event & TTEVT_FORK)
      || (tsp.tts_event & TTEVT_VFORK))
    {
      /* Forks come in pairs (parent and child), so core gdb
       * will do two waits.  Be ready to notice this.
       */
      if (tsp.tts_event & TTEVT_FORK)
	{
	  process_state = FORKING;

#ifdef WAIT_BUFFER_DEBUG
	  if (debug_on)
	    printf ("Process set to FORKING\n");
#endif
	}
      else if (tsp.tts_event & TTEVT_VFORK)
	{
	  process_state = VFORKING;

#ifdef WAIT_BUFFER_DEBUG
	  if (debug_on)
	    printf ("Process set to VFORKING\n");
#endif
	}

      /* Make an exec or fork look like a breakpoint.  Definitely a hack,
         but I don't think non HP-UX-specific clients really carefully
         inspect the first events they get after inferior startup, so
         it probably almost doesn't matter what we claim this is.
       */

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("..a process 'event'\n");
#endif

      /* Also make fork and exec events look like bpts, so they can be caught.
       */
      *status = 0177 | (_SIGTRAP << 8);
    }

  /* Special-cases: We ask for syscall entry and exit events to implement
     "fast" (aka "hardware") watchpoints.

     When we get a syscall entry, we want to disable page-protections,
     and resume the inferior; this isn't an event we wish for
     wait_for_inferior to see.  Note that we must resume ONLY the
     thread that reported the syscall entry; we don't want to allow
     other threads to run with the page protections off, as they might
     then be able to write to watch memory without it being caught.

     When we get a syscall exit, we want to reenable page-protections,
     but we don't want to resume the inferior; this is an event we wish
     wait_for_inferior to see.  Make it look like the signal we normally
     get for a single-step completion.  This should cause wait_for_inferior
     to evaluate whether any watchpoint triggered.

     Or rather, that's what we'd LIKE to do for syscall exit; we can't,
     due to some HP-UX "features".  Some syscalls have problems with
     write-protections on some pages, and some syscalls seem to have
     pending writes to those pages at the time we're getting the return
     event.  So, we'll single-step the inferior to get out of the syscall,
     and then reenable protections.

     Note that we're intentionally allowing the syscall exit case to
     fall through into the succeeding cases, as sometimes we single-
     step out of one syscall only to immediately enter another...
   */
  else if ((tsp.tts_event & TTEVT_SYSCALL_ENTRY)
	   || (tsp.tts_event & TTEVT_SYSCALL_RETURN))
    {
      /* Make a syscall event look like a breakpoint.  Same comments
         as for exec & fork events.
       */
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("..a syscall 'event'\n");
#endif

      /* Also make syscall events look like bpts, so they can be caught.
       */
      *status = 0177 | (_SIGTRAP << 8);
    }

  else if ((tsp.tts_event & TTEVT_LWP_CREATE)
	   || (tsp.tts_event & TTEVT_LWP_TERMINATE)
	   || (tsp.tts_event & TTEVT_LWP_EXIT))
    {
      /* Make a thread event look like a breakpoint.  Same comments
       * as for exec & fork events.
       */
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("..a thread 'event'\n");
#endif

      /* Also make thread events look like bpts, so they can be caught.
       */
      *status = 0177 | (_SIGTRAP << 8);
    }

  else if ((tsp.tts_event & TTEVT_EXIT))
    {				/* WIFEXITED */

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("..an exit\n");
#endif

      /* Prevent rest of gdb from thinking this is
       * a new thread if for some reason it's never
       * seen the main thread before.
       */
      inferior_ptid = pid_to_ptid (map_to_gdb_tid (real_tid));	/* HACK, FIX */

      *status = 0 | (tsp.tts_u.tts_exit.tts_exitcode);
    }

  else if (tsp.tts_event & TTEVT_SIGNAL)
    {				/* WIFSTOPPED */
#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("..a signal, %d\n", tsp.tts_u.tts_signal.tts_signo);
#endif

      *status = 0177 | (tsp.tts_u.tts_signal.tts_signo << 8);
    }

  else
    {				/* !WIFSTOPPED */

      /* This means the process or thread terminated.  But we should've
         caught an explicit exit/termination above.  So warn (this is
         really an internal error) and claim the process or thread
         terminated with a SIGTRAP.
       */

      warning ("process_wait: unknown process state");

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("Process-level event %s, using tid %d\n",
		get_printable_name_of_ttrace_event (tsp.tts_event),
		real_tid);
#endif

      *status = _SIGTRAP;
    }

  target_post_wait (pid_to_ptid (tsp.tts_pid), *status);


#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Done waiting, pid is %d, tid %d\n", real_pid, real_tid);
#endif

  /* All code external to this module uses the tid, but calls
   * it "pid".  There's some tweaking so that the outside sees
   * the first thread as having the same number as the starting
   * pid.
   */
  return_pid = map_to_gdb_tid (real_tid);

  if (real_tid == 0 || return_pid == 0)
    {
      warning ("Internal error: process-wait failed.");
    }

  return return_pid;
}


/* This function causes the caller's process to be traced by its
   parent.  This is intended to be called after GDB forks itself,
   and before the child execs the target.  Despite the name, it
   is called by the child.

   Note that HP-UX ttrace is rather funky in how this is done.
   If the parent wants to get the initial exec event of a child,
   it must set the ttrace event mask of the child to include execs.
   (The child cannot do this itself.)  This must be done after the
   child is forked, but before it execs.

   To coordinate the parent and child, we implement a semaphore using
   pipes.  After SETTRC'ing itself, the child tells the parent that
   it is now traceable by the parent, and waits for the parent's
   acknowledgement.  The parent can then set the child's event mask,
   and notify the child that it can now exec.

   (The acknowledgement by parent happens as a result of a call to
   child_acknowledge_created_inferior.)
 */
int
parent_attach_all (int p1, PTRACE_ARG3_TYPE p2, int p3)
{
  int tt_status;

  /* We need a memory home for a constant, to pass it to ttrace.
     The value of the constant is arbitrary, so long as both
     parent and child use the same value.  Might as well use the
     "magic" constant provided by ttrace...
   */
  uint64_t tc_magic_child = TT_VERSION;
  uint64_t tc_magic_parent = 0;

  tt_status = call_real_ttrace (
				 TT_PROC_SETTRC,
				 (int) TT_NIL,
				 (lwpid_t) TT_NIL,
				 TT_NIL,
				 (TTRACE_ARG_TYPE) TT_VERSION,
				 TT_NIL);

  if (tt_status < 0)
    return tt_status;

  /* Notify the parent that we're potentially ready to exec(). */
  write (startup_semaphore.child_channel[SEM_TALK],
	 &tc_magic_child,
	 sizeof (tc_magic_child));

  /* Wait for acknowledgement from the parent. */
  read (startup_semaphore.parent_channel[SEM_LISTEN],
	&tc_magic_parent,
	sizeof (tc_magic_parent));

  if (tc_magic_child != tc_magic_parent)
    warning ("mismatched semaphore magic");

  /* Discard our copy of the semaphore. */
  (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
  (void) close (startup_semaphore.parent_channel[SEM_TALK]);
  (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
  (void) close (startup_semaphore.child_channel[SEM_TALK]);

  return tt_status;
}

/* Despite being file-local, this routine is dealing with
 * actual process IDs, not thread ids.  That's because it's
 * called before the first "wait" call, and there's no map
 * yet from tids to pids.
 *
 * When it is called, a forked child is running, but waiting on
 * the semaphore.  If you stop the child and re-start it,
 * things get confused, so don't do that!  An attached child is
 * stopped.
 *
 * Since this is called after either attach or run, we
 * have to be the common part of both.
 */
static void
require_notification_of_events (int real_pid)
{
  int tt_status;
  ttevent_t notifiable_events;

  lwpid_t tid;
  ttstate_t thread_state;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Require notif, pid is %d\n", real_pid);
#endif

  /* Temporary HACK: tell inftarg.c/child_wait to not
   * loop until pids are the same.
   */
  not_same_real_pid = 0;

  sigemptyset (&notifiable_events.tte_signals);
  notifiable_events.tte_opts = TTEO_NONE;

  /* This ensures that forked children inherit their parent's
   * event mask, which we're setting here.
   *
   * NOTE: if you debug gdb with itself, then the ultimate
   *       debuggee gets flags set by the outermost gdb, as
   *       a child of a child will still inherit.
   */
  notifiable_events.tte_opts |= TTEO_PROC_INHERIT;

  notifiable_events.tte_events = TTEVT_DEFAULT;
  notifiable_events.tte_events |= TTEVT_SIGNAL;
  notifiable_events.tte_events |= TTEVT_EXEC;
  notifiable_events.tte_events |= TTEVT_EXIT;
  notifiable_events.tte_events |= TTEVT_FORK;
  notifiable_events.tte_events |= TTEVT_VFORK;
  notifiable_events.tte_events |= TTEVT_LWP_CREATE;
  notifiable_events.tte_events |= TTEVT_LWP_EXIT;
  notifiable_events.tte_events |= TTEVT_LWP_TERMINATE;

  tt_status = call_real_ttrace (
				 TT_PROC_SET_EVENT_MASK,
				 real_pid,
				 (lwpid_t) TT_NIL,
				 (TTRACE_ARG_TYPE) & notifiable_events,
			       (TTRACE_ARG_TYPE) sizeof (notifiable_events),
				 TT_NIL);
}

static void
require_notification_of_exec_events (int real_pid)
{
  int tt_status;
  ttevent_t notifiable_events;

  lwpid_t tid;
  ttstate_t thread_state;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Require notif, pid is %d\n", real_pid);
#endif

  /* Temporary HACK: tell inftarg.c/child_wait to not
   * loop until pids are the same.
   */
  not_same_real_pid = 0;

  sigemptyset (&notifiable_events.tte_signals);
  notifiable_events.tte_opts = TTEO_NOSTRCCHLD;

  /* This ensures that forked children don't inherit their parent's
   * event mask, which we're setting here.
   */
  notifiable_events.tte_opts &= ~TTEO_PROC_INHERIT;

  notifiable_events.tte_events = TTEVT_DEFAULT;
  notifiable_events.tte_events |= TTEVT_EXEC;
  notifiable_events.tte_events |= TTEVT_EXIT;

  tt_status = call_real_ttrace (
				 TT_PROC_SET_EVENT_MASK,
				 real_pid,
				 (lwpid_t) TT_NIL,
				 (TTRACE_ARG_TYPE) & notifiable_events,
			       (TTRACE_ARG_TYPE) sizeof (notifiable_events),
				 TT_NIL);
}


/* This function is called by the parent process, with pid being the
 * ID of the child process, after the debugger has forked.
 */
void
child_acknowledge_created_inferior (int pid)
{
  /* We need a memory home for a constant, to pass it to ttrace.
     The value of the constant is arbitrary, so long as both
     parent and child use the same value.  Might as well use the
     "magic" constant provided by ttrace...
   */
  uint64_t tc_magic_parent = TT_VERSION;
  uint64_t tc_magic_child = 0;

  /* Wait for the child to tell us that it has forked. */
  read (startup_semaphore.child_channel[SEM_LISTEN],
	&tc_magic_child,
	sizeof (tc_magic_child));

  /* Clear thread info now.  We'd like to do this in
   * "require...", but that messes up attach.
   */
  clear_thread_info ();

  /* Tell the "rest of gdb" that the initial thread exists.
   * This isn't really a hack.  Other thread-based versions
   * of gdb (e.g. gnu-nat.c) seem to do the same thing.
   *
   * Q: Why don't we also add this thread to the local
   *    list via "add_tthread"?
   *
   * A: Because we don't know the tid, and can't stop the
   *    the process safely to ask what it is.  Anyway, we'll
   *    add it when it gets the EXEC event.
   */
  add_thread (pid_to_ptid (pid));		/* in thread.c */

  /* We can now set the child's ttrace event mask.
   */
  require_notification_of_exec_events (pid);

  /* Tell ourselves that the process is running.
   */
  process_state = RUNNING;

  /* Notify the child that it can exec. */
  write (startup_semaphore.parent_channel[SEM_TALK],
	 &tc_magic_parent,
	 sizeof (tc_magic_parent));

  /* Discard our copy of the semaphore. */
  (void) close (startup_semaphore.parent_channel[SEM_LISTEN]);
  (void) close (startup_semaphore.parent_channel[SEM_TALK]);
  (void) close (startup_semaphore.child_channel[SEM_LISTEN]);
  (void) close (startup_semaphore.child_channel[SEM_TALK]);
}


/*
 * arrange for notification of all events by
 * calling require_notification_of_events.
 */
void
child_post_startup_inferior (ptid_t ptid)
{
  require_notification_of_events (PIDGET (ptid));
}

/* From here on, we should expect tids rather than pids.
 */
static void
hppa_enable_catch_fork (int tid)
{
  int tt_status;
  ttevent_t ttrace_events;

  /* Get the set of events that are currently enabled.
   */
  tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);
  if (errno)
    perror_with_name ("ttrace");

  /* Add forks to that set. */
  ttrace_events.tte_events |= TTEVT_FORK;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("enable fork, tid is %d\n", tid);
#endif

  tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);
  if (errno)
    perror_with_name ("ttrace");
}


static void
hppa_disable_catch_fork (int tid)
{
  int tt_status;
  ttevent_t ttrace_events;

  /* Get the set of events that are currently enabled.
   */
  tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);

  if (errno)
    perror_with_name ("ttrace");

  /* Remove forks from that set. */
  ttrace_events.tte_events &= ~TTEVT_FORK;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("disable fork, tid is %d\n", tid);
#endif

  tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);

  if (errno)
    perror_with_name ("ttrace");
}


#if defined(CHILD_INSERT_FORK_CATCHPOINT)
int
child_insert_fork_catchpoint (int tid)
{
  /* Enable reporting of fork events from the kernel. */
  /* ??rehrauer: For the moment, we're always enabling these events,
     and just ignoring them if there's no catchpoint to catch them.
   */
  return 0;
}
#endif


#if defined(CHILD_REMOVE_FORK_CATCHPOINT)
int
child_remove_fork_catchpoint (int tid)
{
  /* Disable reporting of fork events from the kernel. */
  /* ??rehrauer: For the moment, we're always enabling these events,
     and just ignoring them if there's no catchpoint to catch them.
   */
  return 0;
}
#endif


static void
hppa_enable_catch_vfork (int tid)
{
  int tt_status;
  ttevent_t ttrace_events;

  /* Get the set of events that are currently enabled.
   */
  tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);

  if (errno)
    perror_with_name ("ttrace");

  /* Add vforks to that set. */
  ttrace_events.tte_events |= TTEVT_VFORK;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("enable vfork, tid is %d\n", tid);
#endif

  tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);

  if (errno)
    perror_with_name ("ttrace");
}


static void
hppa_disable_catch_vfork (int tid)
{
  int tt_status;
  ttevent_t ttrace_events;

  /* Get the set of events that are currently enabled. */
  tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);

  if (errno)
    perror_with_name ("ttrace");

  /* Remove vforks from that set. */
  ttrace_events.tte_events &= ~TTEVT_VFORK;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("disable vfork, tid is %d\n", tid);
#endif
  tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
			   tid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);

  if (errno)
    perror_with_name ("ttrace");
}


#if defined(CHILD_INSERT_VFORK_CATCHPOINT)
int
child_insert_vfork_catchpoint (int tid)
{
  /* Enable reporting of vfork events from the kernel. */
  /* ??rehrauer: For the moment, we're always enabling these events,
     and just ignoring them if there's no catchpoint to catch them.
   */
  return 0;
}
#endif


#if defined(CHILD_REMOVE_VFORK_CATCHPOINT)
int
child_remove_vfork_catchpoint (int tid)
{
  /* Disable reporting of vfork events from the kernel. */
  /* ??rehrauer: For the moment, we're always enabling these events,
     and just ignoring them if there's no catchpoint to catch them.
   */
  return 0;
}
#endif

/* Q: Do we need to map the returned process ID to a thread ID?

 * A: I don't think so--here we want a _real_ pid.  Any later
 *    operations will call "require_notification_of_events" and
 *    start the mapping.
 */
int
hpux_has_forked (int tid, int *childpid)
{
  int tt_status;
  ttstate_t ttrace_state;
  thread_info *tinfo;

  /* Do we have cached thread state that we can consult?  If so, use it. */
  tinfo = find_thread_info (map_from_gdb_tid (tid));
  if (tinfo != NULL)
    {
      copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);
    }

  /* Nope, must read the thread's current state */
  else
    {
      tt_status = call_ttrace (TT_LWP_GET_STATE,
			       tid,
			       (TTRACE_ARG_TYPE) & ttrace_state,
			       (TTRACE_ARG_TYPE) sizeof (ttrace_state),
			       TT_NIL);

      if (errno)
	perror_with_name ("ttrace");

      if (tt_status < 0)
	return 0;
    }

  if (ttrace_state.tts_event & TTEVT_FORK)
    {
      *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
      return 1;
    }

  return 0;
}

/* See hpux_has_forked for pid discussion.
 */
int
hpux_has_vforked (int tid, int *childpid)
{
  int tt_status;
  ttstate_t ttrace_state;
  thread_info *tinfo;

  /* Do we have cached thread state that we can consult?  If so, use it. */
  tinfo = find_thread_info (map_from_gdb_tid (tid));
  if (tinfo != NULL)
    copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);

  /* Nope, must read the thread's current state */
  else
    {
      tt_status = call_ttrace (TT_LWP_GET_STATE,
			       tid,
			       (TTRACE_ARG_TYPE) & ttrace_state,
			       (TTRACE_ARG_TYPE) sizeof (ttrace_state),
			       TT_NIL);

      if (errno)
	perror_with_name ("ttrace");

      if (tt_status < 0)
	return 0;
    }

  if (ttrace_state.tts_event & TTEVT_VFORK)
    {
      *childpid = ttrace_state.tts_u.tts_fork.tts_fpid;
      return 1;
    }

  return 0;
}


#if defined(CHILD_INSERT_EXEC_CATCHPOINT)
int
child_insert_exec_catchpoint (int tid)
{
  /* Enable reporting of exec events from the kernel. */
  /* ??rehrauer: For the moment, we're always enabling these events,
     and just ignoring them if there's no catchpoint to catch them.
   */
  return 0;
}
#endif


#if defined(CHILD_REMOVE_EXEC_CATCHPOINT)
int
child_remove_exec_catchpoint (int tid)
{
  /* Disable reporting of execevents from the kernel. */
  /* ??rehrauer: For the moment, we're always enabling these events,
     and just ignoring them if there's no catchpoint to catch them.
   */
  return 0;
}
#endif


int
hpux_has_execd (int tid, char **execd_pathname)
{
  int tt_status;
  ttstate_t ttrace_state;
  thread_info *tinfo;

  /* Do we have cached thread state that we can consult?  If so, use it. */
  tinfo = find_thread_info (map_from_gdb_tid (tid));
  if (tinfo != NULL)
    copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);

  /* Nope, must read the thread's current state */
  else
    {
      tt_status = call_ttrace (TT_LWP_GET_STATE,
			       tid,
			       (TTRACE_ARG_TYPE) & ttrace_state,
			       (TTRACE_ARG_TYPE) sizeof (ttrace_state),
			       TT_NIL);

      if (errno)
	perror_with_name ("ttrace");

      if (tt_status < 0)
	return 0;
    }

  if (ttrace_state.tts_event & TTEVT_EXEC)
    {
      /* See child_pid_to_exec_file in this file: this is a macro.
       */
      char *exec_file = target_pid_to_exec_file (tid);

      *execd_pathname = savestring (exec_file, strlen (exec_file));
      return 1;
    }

  return 0;
}


int
hpux_has_syscall_event (int pid, enum target_waitkind *kind, int *syscall_id)
{
  int tt_status;
  ttstate_t ttrace_state;
  thread_info *tinfo;

  /* Do we have cached thread state that we can consult?  If so, use it. */
  tinfo = find_thread_info (map_from_gdb_tid (pid));
  if (tinfo != NULL)
    copy_ttstate_t (&ttrace_state, &tinfo->last_stop_state);

  /* Nope, must read the thread's current state */
  else
    {
      tt_status = call_ttrace (TT_LWP_GET_STATE,
			       pid,
			       (TTRACE_ARG_TYPE) & ttrace_state,
			       (TTRACE_ARG_TYPE) sizeof (ttrace_state),
			       TT_NIL);

      if (errno)
	perror_with_name ("ttrace");

      if (tt_status < 0)
	return 0;
    }

  *kind = TARGET_WAITKIND_SPURIOUS;	/* Until proven otherwise... */
  *syscall_id = -1;

  if (ttrace_state.tts_event & TTEVT_SYSCALL_ENTRY)
    *kind = TARGET_WAITKIND_SYSCALL_ENTRY;
  else if (ttrace_state.tts_event & TTEVT_SYSCALL_RETURN)
    *kind = TARGET_WAITKIND_SYSCALL_RETURN;
  else
    return 0;

  *syscall_id = ttrace_state.tts_scno;
  return 1;
}



#if defined(CHILD_THREAD_ALIVE)

/* Check to see if the given thread is alive.

 * We'll trust the thread list, as the more correct
 * approach of stopping the process and spinning down
 * the OS's thread list is _very_ expensive.
 *
 * May need a FIXME for that reason.
 */
int
child_thread_alive (ptid_t ptid)
{
  lwpid_t gdb_tid = PIDGET (ptid);
  lwpid_t tid;

  /* This spins down the lists twice.
   * Possible peformance improvement here!
   */
  tid = map_from_gdb_tid (gdb_tid);
  return !is_terminated (tid);
}

#endif



/* This function attempts to read the specified number of bytes from the
   save_state_t that is our view into the hardware registers, starting at
   ss_offset, and ending at ss_offset + sizeof_buf - 1

   If this function succeeds, it deposits the fetched bytes into buf,
   and returns 0.

   If it fails, it returns a negative result.  The contents of buf are
   undefined it this function fails.
 */
int
read_from_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
			       int sizeof_buf)
{
  int tt_status;
  register_value_t register_value = 0;

  tt_status = call_ttrace (TT_LWP_RUREGS,
			   tid,
			   ss_offset,
			   (TTRACE_ARG_TYPE) sizeof_buf,
			   (TTRACE_ARG_TYPE) buf);

  if (tt_status == 1)
    /* Map ttrace's version of success to our version.
     * Sometime ttrace returns 0, but that's ok here.
     */
    return 0;

  return tt_status;
}


/* This function attempts to write the specified number of bytes to the
   save_state_t that is our view into the hardware registers, starting at
   ss_offset, and ending at ss_offset + sizeof_buf - 1

   If this function succeeds, it deposits the bytes in buf, and returns 0.

   If it fails, it returns a negative result.  The contents of the save_state_t
   are undefined it this function fails.
 */
int
write_to_register_save_state (int tid, TTRACE_ARG_TYPE ss_offset, char *buf,
			      int sizeof_buf)
{
  int tt_status;
  register_value_t register_value = 0;

  tt_status = call_ttrace (TT_LWP_WUREGS,
			   tid,
			   ss_offset,
			   (TTRACE_ARG_TYPE) sizeof_buf,
			   (TTRACE_ARG_TYPE) buf);
  return tt_status;
}


/* This function is a sop to the largeish number of direct calls
   to call_ptrace that exist in other files.  Rather than create
   functions whose name abstracts away from ptrace, and change all
   the present callers of call_ptrace, we'll do the expedient (and
   perhaps only practical) thing.

   Note HP-UX explicitly disallows a mix of ptrace & ttrace on a traced
   process.  Thus, we must translate all ptrace requests into their
   process-specific, ttrace equivalents.
 */
int
call_ptrace (int pt_request, int gdb_tid, PTRACE_ARG3_TYPE addr, int data)
{
  ttreq_t tt_request;
  TTRACE_ARG_TYPE tt_addr = (TTRACE_ARG_TYPE) addr;
  TTRACE_ARG_TYPE tt_data = (TTRACE_ARG_TYPE) data;
  TTRACE_ARG_TYPE tt_addr2 = TT_NIL;
  int tt_status;
  register_value_t register_value;
  int read_buf;

  /* Perform the necessary argument translation.  Note that some
     cases are funky enough in the ttrace realm that we handle them
     very specially.
   */
  switch (pt_request)
    {
      /* The following cases cannot conveniently be handled conveniently
         by merely adjusting the ptrace arguments and feeding into the
         generic call to ttrace at the bottom of this function.

         Note that because all branches of this switch end in "return",
         there's no need for any "break" statements.
       */
    case PT_SETTRC:
      return parent_attach_all (0, 0, 0);

    case PT_RUREGS:
      tt_status = read_from_register_save_state (gdb_tid,
						 tt_addr,
						 &register_value,
						 sizeof (register_value));
      if (tt_status < 0)
	return tt_status;
      return register_value;

    case PT_WUREGS:
      register_value = (int) tt_data;
      tt_status = write_to_register_save_state (gdb_tid,
						tt_addr,
						&register_value,
						sizeof (register_value));
      return tt_status;
      break;

    case PT_READ_I:
      tt_status = call_ttrace (TT_PROC_RDTEXT,	/* Implicit 4-byte xfer becomes block-xfer. */
			       gdb_tid,
			       tt_addr,
			       (TTRACE_ARG_TYPE) 4,
			       (TTRACE_ARG_TYPE) & read_buf);
      if (tt_status < 0)
	return tt_status;
      return read_buf;

    case PT_READ_D:
      tt_status = call_ttrace (TT_PROC_RDDATA,	/* Implicit 4-byte xfer becomes block-xfer. */
			       gdb_tid,
			       tt_addr,
			       (TTRACE_ARG_TYPE) 4,
			       (TTRACE_ARG_TYPE) & read_buf);
      if (tt_status < 0)
	return tt_status;
      return read_buf;

    case PT_ATTACH:
      tt_status = call_real_ttrace (TT_PROC_ATTACH,
				    map_from_gdb_tid (gdb_tid),
				    (lwpid_t) TT_NIL,
				    tt_addr,
				    (TTRACE_ARG_TYPE) TT_VERSION,
				    tt_addr2);
      if (tt_status < 0)
	return tt_status;
      return tt_status;

      /* The following cases are handled by merely adjusting the ptrace
         arguments and feeding into the generic call to ttrace.
       */
    case PT_DETACH:
      tt_request = TT_PROC_DETACH;
      break;

    case PT_WRITE_I:
      tt_request = TT_PROC_WRTEXT;	/* Translates 4-byte xfer to block-xfer. */
      tt_data = 4;		/* This many bytes. */
      tt_addr2 = (TTRACE_ARG_TYPE) & data;	/* Address of xfer source. */
      break;

    case PT_WRITE_D:
      tt_request = TT_PROC_WRDATA;	/* Translates 4-byte xfer to block-xfer. */
      tt_data = 4;		/* This many bytes. */
      tt_addr2 = (TTRACE_ARG_TYPE) & data;	/* Address of xfer source. */
      break;

    case PT_RDTEXT:
      tt_request = TT_PROC_RDTEXT;
      break;

    case PT_RDDATA:
      tt_request = TT_PROC_RDDATA;
      break;

    case PT_WRTEXT:
      tt_request = TT_PROC_WRTEXT;
      break;

    case PT_WRDATA:
      tt_request = TT_PROC_WRDATA;
      break;

    case PT_CONTINUE:
      tt_request = TT_PROC_CONTINUE;
      break;

    case PT_STEP:
      tt_request = TT_LWP_SINGLE;	/* Should not be making this request? */
      break;

    case PT_KILL:
      tt_request = TT_PROC_EXIT;
      break;

    case PT_GET_PROCESS_PATHNAME:
      tt_request = TT_PROC_GET_PATHNAME;
      break;

    default:
      tt_request = pt_request;	/* Let ttrace be the one to complain. */
      break;
    }

  return call_ttrace (tt_request,
		      gdb_tid,
		      tt_addr,
		      tt_data,
		      tt_addr2);
}

/* Kill that pesky process!
 */
void
kill_inferior (void)
{
  int tid;
  int wait_status;
  thread_info *t;
  thread_info **paranoia;
  int para_count, i;

  if (PIDGET (inferior_ptid) == 0)
    return;

  /* Walk the list of "threads", some of which are "pseudo threads",
     aka "processes".  For each that is NOT inferior_ptid, stop it,
     and detach it.

     You see, we may not have just a single process to kill.  If we're
     restarting or quitting or detaching just after the inferior has
     forked, then we've actually two processes to clean up.

     But we can't just call target_mourn_inferior() for each, since that
     zaps the target vector.
   */

  paranoia = (thread_info **) xmalloc (thread_head.count *
				       sizeof (thread_info *));
  para_count = 0;

  t = thread_head.head;
  while (t)
    {

      paranoia[para_count] = t;
      for (i = 0; i < para_count; i++)
	{
	  if (t->next == paranoia[i])
	    {
	      warning ("Bad data in gdb's thread data; repairing.");
	      t->next = 0;
	    }
	}
      para_count++;

      if (t->am_pseudo && (t->pid != PIDGET (inferior_ptid)))
	{
	  call_ttrace (TT_PROC_EXIT,
		       t->pid,
		       TT_NIL,
		       TT_NIL,
		       TT_NIL);
	}
      t = t->next;
    }

  xfree (paranoia);

  call_ttrace (TT_PROC_EXIT,
	       PIDGET (inferior_ptid),
	       TT_NIL,
	       TT_NIL,
	       TT_NIL);
  target_mourn_inferior ();
  clear_thread_info ();
}


#ifndef DEPRECATED_CHILD_RESUME

/* Sanity check a thread about to be continued.
 */
static void
thread_dropping_event_check (thread_info *p)
{
  if (!p->handled)
    {
      /*
       * This seems to happen when we "next" over a
       * "fork()" while following the parent.  If it's
       * the FORK event, that's ok.  If it's a SIGNAL
       * in the unfollowed child, that's ok to--but
       * how can we know that's what's going on?
       *
       * FIXME!
       */
      if (p->have_state)
	{
	  if (p->last_stop_state.tts_event == TTEVT_FORK)
	    {
	      /* Ok */
	      ;
	    }
	  else if (p->last_stop_state.tts_event == TTEVT_SIGNAL)
	    {
	      /* Ok, close eyes and let it happen.
	       */
	      ;
	    }
	  else
	    {
	      /* This shouldn't happen--we're dropping a
	       * real event.
	       */
	      warning ("About to continue process %d, thread %d with unhandled event %s.",
		       p->pid, p->tid,
		       get_printable_name_of_ttrace_event (
					     p->last_stop_state.tts_event));

#ifdef PARANOIA
	      if (debug_on)
		print_tthread (p);
#endif
	    }
	}
      else
	{
	  /* No saved state, have to assume it failed.
	   */
	  warning ("About to continue process %d, thread %d with unhandled event.",
		   p->pid, p->tid);
#ifdef PARANOIA
	  if (debug_on)
	    print_tthread (p);
#endif
	}
    }

}				/* thread_dropping_event_check */

/* Use a loop over the threads to continue all the threads but
 * the one specified, which is to be stepped.
 */
static void
threads_continue_all_but_one (lwpid_t gdb_tid, int signal)
{
  thread_info *p;
  int thread_signal;
  lwpid_t real_tid;
  lwpid_t scan_tid;
  ttstate_t state;
  int real_pid;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Using loop over threads to step/resume with signals\n");
#endif

  /* First update the thread list.
   */
  set_all_unseen ();
  real_tid = map_from_gdb_tid (gdb_tid);
  real_pid = get_pid_for (real_tid);

  scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
  while (0 != scan_tid)
    {

#ifdef THREAD_DEBUG
      /* FIX: later should check state is stopped;
       * state.tts_flags & TTS_STATEMASK == TTS_WASSUSPENDED
       */
      if (debug_on)
 	if ((state.tts_flags & TTS_STATEMASK) != TTS_WASSUSPENDED)
	  printf ("About to continue non-stopped thread %d\n", scan_tid);
#endif

      p = find_thread_info (scan_tid);
      if (NULL == p)
	{
	  add_tthread (real_pid, scan_tid);
	  p = find_thread_info (scan_tid);

	  /* This is either a newly-created thread or the
	   * result of a fork; in either case there's no
	   * actual event to worry about.
	   */
	  p->handled = 1;

	  if (state.tts_event != TTEVT_NONE)
	    {
	      /* Oops, do need to worry!
	       */
	      warning ("Unexpected thread with \"%s\" event.",
		       get_printable_name_of_ttrace_event (state.tts_event));
	    }
	}
      else if (scan_tid != p->tid)
	error ("Bad data in thread database.");

#ifdef THREAD_DEBUG
      if (debug_on)
	if (p->terminated)
	  printf ("Why are we continuing a dead thread?\n");
#endif

      p->seen = 1;

      scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
    }

  /* Remove unseen threads.
   */
  update_thread_list ();

  /* Now run down the thread list and continue or step.
   */
  for (p = thread_head.head; p; p = p->next)
    {

      /* Sanity check.
       */
      thread_dropping_event_check (p);

      /* Pass the correct signals along.
       */
      if (p->have_signal)
	{
	  thread_signal = p->signal_value;
	  p->have_signal = 0;
	}
      else
	thread_signal = 0;

      if (p->tid != real_tid)
	{
	  /*
	   * Not the thread of interest, so continue it
	   * as the user expects.
	   */
	  if (p->stepping_mode == DO_STEP)
	    {
	      /* Just step this thread.
	       */
	      call_ttrace (
			    TT_LWP_SINGLE,
			    p->tid,
			    TT_USE_CURRENT_PC,
			    (TTRACE_ARG_TYPE) target_signal_to_host (signal),
			    TT_NIL);
	    }
	  else
	    {
	      /* Regular continue (default case).
	       */
	      call_ttrace (
			    TT_LWP_CONTINUE,
			    p->tid,
			    TT_USE_CURRENT_PC,
		    (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
			    TT_NIL);
	    }
	}
      else
	{
	  /* Step the thread of interest.
	   */
	  call_ttrace (
			TT_LWP_SINGLE,
			real_tid,
			TT_USE_CURRENT_PC,
			(TTRACE_ARG_TYPE) target_signal_to_host (signal),
			TT_NIL);
	}
    }				/* Loop over threads */
}				/* End threads_continue_all_but_one */

/* Use a loop over the threads to continue all the threads.
 * This is done when a signal must be sent to any of the threads.
 */
static void
threads_continue_all_with_signals (lwpid_t gdb_tid, int signal)
{
  thread_info *p;
  int thread_signal;
  lwpid_t real_tid;
  lwpid_t scan_tid;
  ttstate_t state;
  int real_pid;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Using loop over threads to resume with signals\n");
#endif

  /* Scan and update thread list.
   */
  set_all_unseen ();
  real_tid = map_from_gdb_tid (gdb_tid);
  real_pid = get_pid_for (real_tid);

  scan_tid = get_process_first_stopped_thread_id (real_pid, &state);
  while (0 != scan_tid)
    {

#ifdef THREAD_DEBUG
      if (debug_on)
	if ((state.tts_flags & TTS_STATEMASK) != TTS_WASSUSPENDED)
	  warning ("About to continue non-stopped thread %d\n", scan_tid);
#endif

      p = find_thread_info (scan_tid);
      if (NULL == p)
	{
	  add_tthread (real_pid, scan_tid);
	  p = find_thread_info (scan_tid);

	  /* This is either a newly-created thread or the
	   * result of a fork; in either case there's no
	   * actual event to worry about.
	   */
	  p->handled = 1;

	  if (state.tts_event != TTEVT_NONE)
	    {
	      /* Oops, do need to worry!
	       */
	      warning ("Unexpected thread with \"%s\" event.",
		       get_printable_name_of_ttrace_event (state.tts_event));
	    }
	}

#ifdef THREAD_DEBUG
      if (debug_on)
	if (p->terminated)
	  printf ("Why are we continuing a dead thread? (1)\n");
#endif

      p->seen = 1;

      scan_tid = get_process_next_stopped_thread_id (real_pid, &state);
    }

  /* Remove unseen threads from our list.
   */
  update_thread_list ();

  /* Continue the threads.
   */
  for (p = thread_head.head; p; p = p->next)
    {

      /* Sanity check.
       */
      thread_dropping_event_check (p);

      /* Pass the correct signals along.
       */
      if (p->tid == real_tid)
	{
	  thread_signal = signal;
	  p->have_signal = 0;
	}
      else if (p->have_signal)
	{
	  thread_signal = p->signal_value;
	  p->have_signal = 0;
	}
      else
	thread_signal = 0;

      if (p->stepping_mode == DO_STEP)
	{
	  call_ttrace (
			TT_LWP_SINGLE,
			p->tid,
			TT_USE_CURRENT_PC,
			(TTRACE_ARG_TYPE) target_signal_to_host (signal),
			TT_NIL);
	}
      else
	{
	  /* Continue this thread (default case).
	   */
	  call_ttrace (
			TT_LWP_CONTINUE,
			p->tid,
			TT_USE_CURRENT_PC,
		    (TTRACE_ARG_TYPE) target_signal_to_host (thread_signal),
			TT_NIL);
	}
    }
}				/* End threads_continue_all_with_signals */

/* Step one thread only.  
 */
static void
thread_fake_step (lwpid_t tid, enum target_signal signal)
{
  thread_info *p;

#ifdef THREAD_DEBUG
  if (debug_on)
    {
      printf ("Doing a fake-step over a bpt, etc. for %d\n", tid);

      if (is_terminated (tid))
	printf ("Why are we continuing a dead thread? (4)\n");
    }
#endif

  if (doing_fake_step)
    warning ("Step while step already in progress.");

  /* See if there's a saved signal value for this
   * thread to be passed on, but no current signal.
   */
  p = find_thread_info (tid);
  if (p != NULL)
    {
      if (p->have_signal && signal == TARGET_SIGNAL_0)
	{
	  /* Pass on a saved signal.
	   */
	  signal = p->signal_value;
	}

      p->have_signal = 0;
    }

  if (!p->handled)
    warning ("Internal error: continuing unhandled thread.");

  call_ttrace (TT_LWP_SINGLE,
	       tid,
	       TT_USE_CURRENT_PC,
	       (TTRACE_ARG_TYPE) target_signal_to_host (signal),
	       TT_NIL);

  /* Do bookkeeping so "call_ttrace_wait" knows it has to wait
   * for this thread only, and clear any saved signal info.
   */
  doing_fake_step = 1;
  fake_step_tid = tid;

}				/* End thread_fake_step */

/* Continue one thread when a signal must be sent to it.
 */
static void
threads_continue_one_with_signal (lwpid_t gdb_tid, int signal)
{
  thread_info *p;
  lwpid_t real_tid;
  int real_pid;

#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("Continuing one thread with a signal\n");
#endif

  real_tid = map_from_gdb_tid (gdb_tid);
  real_pid = get_pid_for (real_tid);

  p = find_thread_info (real_tid);
  if (NULL == p)
    {
      add_tthread (real_pid, real_tid);
    }

#ifdef THREAD_DEBUG
  if (debug_on)
    if (p->terminated)
      printf ("Why are we continuing a dead thread? (2)\n");
#endif

  if (!p->handled)
    warning ("Internal error: continuing unhandled thread.");

  p->have_signal = 0;

  call_ttrace (TT_LWP_CONTINUE,
	       gdb_tid,
	       TT_USE_CURRENT_PC,
	       (TTRACE_ARG_TYPE) target_signal_to_host (signal),
	       TT_NIL);
}
#endif

#ifndef DEPRECATED_CHILD_RESUME

/* Resume execution of the inferior process.

 * This routine is in charge of setting the "handled" bits. 
 *
 *   If STEP is zero,      continue it.
 *   If STEP is nonzero,   single-step it.
 *   
 *   If SIGNAL is nonzero, give it that signal.
 *
 *   If TID is -1,         apply to all threads.
 *   If TID is not -1,     apply to specified thread.
 *   
 *           STEP
 *      \      !0                        0
 *  TID  \________________________________________________
 *       |
 *   -1  |   Step current            Continue all threads
 *       |   thread and              (but which gets any
 *       |   continue others         signal?--We look at
 *       |                           "inferior_ptid")
 *       |
 *    N  |   Step _this_ thread      Continue _this_ thread
 *       |   and leave others        and leave others 
 *       |   stopped; internally     stopped; used only for
 *       |   used by gdb, never      hardware watchpoints
 *       |   a user command.         and attach, never a
 *       |                           user command.
 */
void
child_resume (ptid_t ptid, int step, enum target_signal signal)
{
  int resume_all_threads;
  lwpid_t tid;
  process_state_t new_process_state;
  lwpid_t gdb_tid = PIDGET (ptid);

  resume_all_threads =
    (gdb_tid == INFTTRACE_ALL_THREADS) ||
    (vfork_in_flight);

  if (resume_all_threads)
    {
      /* Resume all threads, but first pick a tid value
       * so we can get the pid when in call_ttrace doing
       * the map.
       */
      if (vfork_in_flight)
	tid = vforking_child_pid;
      else
	tid = map_from_gdb_tid (PIDGET (inferior_ptid));
    }
  else
    tid = map_from_gdb_tid (gdb_tid);

#ifdef THREAD_DEBUG
  if (debug_on)
    {
      if (more_events_left)
	printf ("More events; ");

      if (signal != 0)
	printf ("Sending signal %d; ", signal);

      if (resume_all_threads)
	{
	  if (step == 0)
	    printf ("Continue process %d\n", tid);
	  else
	    printf ("Step/continue thread %d\n", tid);
	}
      else
	{
	  if (step == 0)
	    printf ("Continue thread %d\n", tid);
	  else
	    printf ("Step just thread %d\n", tid);
	}

      if (vfork_in_flight)
	printf ("Vfork in flight\n");
    }
#endif

  if (process_state == RUNNING)
    warning ("Internal error in resume logic; doing resume or step anyway.");

  if (!step			/* Asked to continue...       */
      && resume_all_threads	/* whole process..            */
      && signal != 0		/* with a signal...           */
      && more_events_left > 0)
    {				/* but we can't yet--save it! */

      /* Continue with signal means we have to set the pending
       * signal value for this thread.
       */
      thread_info *k;

#ifdef THREAD_DEBUG
      if (debug_on)
	printf ("Saving signal %d for thread %d\n", signal, tid);
#endif

      k = find_thread_info (tid);
      if (k != NULL)
	{
	  k->have_signal = 1;
	  k->signal_value = signal;

#ifdef THREAD_DEBUG
	  if (debug_on)
	    if (k->terminated)
	      printf ("Why are we continuing a dead thread? (3)\n");
#endif

	}

#ifdef THREAD_DEBUG
      else if (debug_on)
	{
	  printf ("No thread info for tid %d\n", tid);
	}
#endif
    }

  /* Are we faking this "continue" or "step"?

   * We used to do steps by continuing all the threads for 
   * which the events had been handled already.  While
   * conceptually nicer (hides it all in a lower level), this
   * can lead to starvation and a hang (e.g. all but one thread
   * are unhandled at a breakpoint just before a "join" operation,
   * and one thread is in the join, and the user wants to step that
   * thread).
   */
  if (resume_all_threads	/* Whole process, therefore user command */
      && more_events_left > 0)
    {				/* But we can't do this yet--fake it! */
      thread_info *p;

      if (!step)
	{
	  /* No need to do any notes on a per-thread
	   * basis--we're done!
	   */
#ifdef WAIT_BUFFER_DEBUG
	  if (debug_on)
	    printf ("Faking a process resume.\n");
#endif

	  return;
	}
      else
	{

#ifdef WAIT_BUFFER_DEBUG
	  if (debug_on)
	    printf ("Faking a process step.\n");
#endif

	}

      p = find_thread_info (tid);
      if (p == NULL)
	{
	  warning ("No thread information for tid %d, 'next' command ignored.\n", tid);
	  return;
	}
      else
	{

#ifdef THREAD_DEBUG
	  if (debug_on)
	    if (p->terminated)
	      printf ("Why are we continuing a dead thread? (3.5)\n");
#endif

	  if (p->stepping_mode != DO_DEFAULT)
	    {
	      warning ("Step or continue command applied to thread which is already stepping or continuing; command ignored.");

	      return;
	    }

	  if (step)
	    p->stepping_mode = DO_STEP;
	  else
	    p->stepping_mode = DO_CONTINUE;

	  return;
	}			/* Have thread info */
    }				/* Must fake step or go */

  /* Execept for fake-steps, from here on we know we are
   * going to wind up with a running process which will
   * need a real wait.
   */
  new_process_state = RUNNING;

  /* An address of TT_USE_CURRENT_PC tells ttrace to continue from where
   * it was.  (If GDB wanted it to start some other way, we have already
   * written a new PC value to the child.)
   *
   * If this system does not support PT_STEP, a higher level function will
   * have called single_step() to transmute the step request into a
   * continue request (by setting breakpoints on all possible successor
   * instructions), so we don't have to worry about that here.
   */
  if (step)
    {
      if (resume_all_threads)
	{
	  /*
	   * Regular user step: other threads get a "continue".
	   */
	  threads_continue_all_but_one (tid, signal);
	  clear_all_handled ();
	  clear_all_stepping_mode ();
	}

      else
	{
	  /* "Fake step": gdb is stepping one thread over a
	   * breakpoint, watchpoint, or out of a library load
	   * event, etc.  The rest just stay where they are.
	   *
	   * Also used when there are pending events: we really
	   * step the current thread, but leave the rest stopped.
	   * Users can't request this, but "wait_for_inferior"
	   * does--a lot!
	   */
	  thread_fake_step (tid, signal);

	  /* Clear the "handled" state of this thread, because
	   * we'll soon get a new event for it.  Other events
	   * stay as they were.
	   */
	  clear_handled (tid);
	  clear_stepping_mode (tid);
	  new_process_state = FAKE_STEPPING;
	}
    }

  else
    {
      /* TT_LWP_CONTINUE can pass signals to threads, TT_PROC_CONTINUE can't.
	 Therefore, we really can't use TT_PROC_CONTINUE here.

	 Consider a process which stopped due to signal which gdb decides
	 to handle and not pass on to the inferior.  In that case we must
	 clear the pending signal by restarting the inferior using
	 TT_LWP_CONTINUE and pass zero as the signal number.  Else the
	 pending signal will be passed to the inferior.  interrupt.exp
	 in the testsuite does this precise thing and fails due to the
	 unwanted signal delivery to the inferior.  */
      /* drow/2002-12-05: However, note that we must use TT_PROC_CONTINUE
	 if we are tracing a vfork.  */
      if (vfork_in_flight)
	{
	  call_ttrace (TT_PROC_CONTINUE, tid, TT_NIL, TT_NIL, TT_NIL);
	  clear_all_handled ();
	  clear_all_stepping_mode ();
	}
      else if (resume_all_threads)
	{
#ifdef THREAD_DEBUG
	  if (debug_on)
	    printf ("Doing a continue by loop of all threads\n");
#endif

	  threads_continue_all_with_signals (tid, signal);

	  clear_all_handled ();
	  clear_all_stepping_mode ();
	}
      else
	{
#ifdef THREAD_DEBUG
	  printf ("Doing a continue w/signal of just thread %d\n", tid);
#endif

	  threads_continue_one_with_signal (tid, signal);

	  /* Clear the "handled" state of this thread, because we
	     will soon get a new event for it.  Other events can
	     stay as they were.  */
	  clear_handled (tid);
	  clear_stepping_mode (tid);
	}
    }

  process_state = new_process_state;

#ifdef WAIT_BUFFER_DEBUG
  if (debug_on)
    printf ("Process set to %s\n",
	    get_printable_name_of_process_state (process_state));
#endif

}
#endif /* DEPRECATED_CHILD_RESUME */

/*
 * Like it says.
 *
 * One worry is that we may not be attaching to "inferior_ptid"
 * and thus may not want to clear out our data.  FIXME?
 * 
 */
static void
update_thread_state_after_attach (int pid, attach_continue_t kind_of_go)
{
  int tt_status;
  ttstate_t thread_state;
  lwpid_t a_thread;
  lwpid_t tid;

  /* The process better be stopped.
   */
  if (process_state != STOPPED
      && process_state != VFORKING)
    warning ("Internal error attaching.");

  /* Clear out old tthread info and start over.  This has the
   * side effect of ensuring that the TRAP is reported as being
   * in the right thread (re-mapped from tid to pid).
   *
   * It's because we need to add the tthread _now_ that we
   * need to call "clear_thread_info" _now_, and that's why
   * "require_notification_of_events" doesn't clear the thread
   * info (it's called later than this routine).
   */
  clear_thread_info ();
  a_thread = 0;

  for (tid = get_process_first_stopped_thread_id (pid, &thread_state);
       tid != 0;
       tid = get_process_next_stopped_thread_id (pid, &thread_state))
    {
      thread_info *p;

      if (a_thread == 0)
	{
	  a_thread = tid;
#ifdef THREAD_DEBUG
	  if (debug_on)
	    printf ("Attaching to process %d, thread %d\n",
		    pid, a_thread);
#endif
	}

      /* Tell ourselves and the "rest of gdb" that this thread
       * exists.
       *
       * This isn't really a hack.  Other thread-based versions
       * of gdb (e.g. gnu-nat.c) seem to do the same thing.
       *
       * We don't need to do mapping here, as we know this
       * is the first thread and thus gets the real pid
       * (and is "inferior_ptid").
       *
       * NOTE: it probably isn't the originating thread,
       *       but that doesn't matter (we hope!).
       */
      add_tthread (pid, tid);
      p = find_thread_info (tid);
      if (NULL == p)		/* ?We just added it! */
	error ("Internal error adding a thread on attach.");

      copy_ttstate_t (&p->last_stop_state, &thread_state);
      p->have_state = 1;

      if (DO_ATTACH_CONTINUE == kind_of_go)
	{
	  /*
	   * If we are going to CONTINUE afterwards,
	   * raising a SIGTRAP, don't bother trying to
	   * handle this event.  But check first!
	   */
	  switch (p->last_stop_state.tts_event)
	    {

	    case TTEVT_NONE:
	      /* Ok to set this handled.
	       */
	      break;

	    default:
	      warning ("Internal error; skipping event %s on process %d, thread %d.",
		       get_printable_name_of_ttrace_event (
					      p->last_stop_state.tts_event),
		       p->pid, p->tid);
	    }

	  set_handled (pid, tid);

	}
      else
	{
	  /* There will be no "continue" opertion, so the
	   * process remains stopped.  Don't set any events
	   * handled except the "gimmies".
	   */
	  switch (p->last_stop_state.tts_event)
	    {

	    case TTEVT_NONE:
	      /* Ok to ignore this.
	       */
	      set_handled (pid, tid);
	      break;

	    case TTEVT_EXEC:
	    case TTEVT_FORK:
	      /* Expected "other" FORK or EXEC event from a
	       * fork or vfork.
	       */
	      break;

	    default:
	      printf ("Internal error: failed to handle event %s on process %d, thread %d.",
		      get_printable_name_of_ttrace_event (
					      p->last_stop_state.tts_event),
		      p->pid, p->tid);
	    }
	}

      add_thread (pid_to_ptid (pid));		/* in thread.c */
    }

#ifdef PARANOIA
  if (debug_on)
    print_tthreads ();
#endif

  /* One mustn't call ttrace_wait() after attaching via ttrace,
     'cause the process is stopped already.

     However, the upper layers of gdb's execution control will
     want to wait after attaching (but not after forks, in
     which case they will be doing a "target_resume", anticipating
     a later TTEVT_EXEC or TTEVT_FORK event).

     To make this attach() implementation more compatible with
     others, we'll make the attached-to process raise a SIGTRAP.

     Issue: this continues only one thread.  That could be
     dangerous if the thread is blocked--the process won't run
     and no trap will be raised.  FIX! (check state.tts_flags?
     need one that's either TTS_WASRUNNING--but we've stopped
     it and made it TTS_WASSUSPENDED.  Hum...FIXME!)
   */
  if (DO_ATTACH_CONTINUE == kind_of_go)
    {
      tt_status = call_real_ttrace (
				     TT_LWP_CONTINUE,
				     pid,
				     a_thread,
				     TT_USE_CURRENT_PC,
	       (TTRACE_ARG_TYPE) target_signal_to_host (TARGET_SIGNAL_TRAP),
				     TT_NIL);
      if (errno)
	perror_with_name ("ttrace");

      clear_handled (a_thread);	/* So TRAP will be reported. */

      /* Now running.
       */
      process_state = RUNNING;
    }

  attach_flag = 1;
}


/* Start debugging the process whose number is PID.
 * (A _real_ pid).
 */
int
attach (int pid)
{
  int tt_status;

  tt_status = call_real_ttrace (
				 TT_PROC_ATTACH,
				 pid,
				 (lwpid_t) TT_NIL,
				 TT_NIL,
				 (TTRACE_ARG_TYPE) TT_VERSION,
				 TT_NIL);
  if (errno)
    perror_with_name ("ttrace attach");

  /* If successful, the process is now stopped.
   */
  process_state = STOPPED;

  /* Our caller ("attach_command" in "infcmd.c")
   * expects to do a "wait_for_inferior" after
   * the attach, so make sure the inferior is
   * running when we're done.
   */
  update_thread_state_after_attach (pid, DO_ATTACH_CONTINUE);

  return pid;
}


#if defined(CHILD_POST_ATTACH)
void
child_post_attach (int pid)
{
#ifdef THREAD_DEBUG
  if (debug_on)
    printf ("child-post-attach call\n");
#endif

  require_notification_of_events (pid);
}
#endif


/* Stop debugging the process whose number is PID
   and continue it with signal number SIGNAL.
   SIGNAL = 0 means just continue it.
 */
void
detach (int signal)
{
  errno = 0;
  call_ttrace (TT_PROC_DETACH,
	       PIDGET (inferior_ptid),
	       TT_NIL,
	       (TTRACE_ARG_TYPE) signal,
	       TT_NIL);
  attach_flag = 0;

  clear_thread_info ();

  /* Process-state? */
}


/* Default the type of the ttrace transfer to int.  */
#ifndef TTRACE_XFER_TYPE
#define TTRACE_XFER_TYPE int
#endif

void
_initialize_kernel_u_addr (void)
{
}

#if !defined (CHILD_XFER_MEMORY)
/* NOTE! I tried using TTRACE_READDATA, etc., to read and write memory
   in the NEW_SUN_TTRACE case.
   It ought to be straightforward.  But it appears that writing did
   not write the data that I specified.  I cannot understand where
   it got the data that it actually did write.  */

/* Copy LEN bytes to or from inferior's memory starting at MEMADDR
   to debugger memory starting at MYADDR.   Copy to inferior if
   WRITE is nonzero.  TARGET is ignored.

   Returns the length copied, which is either the LEN argument or zero.
   This xfer function does not do partial moves, since child_ops
   doesn't allow memory operations to cross below us in the target stack
   anyway.  */

int
child_xfer_memory (CORE_ADDR memaddr, char *myaddr, int len, int write,
		   struct mem_attrib *attrib,
		   struct target_ops *target)
{
  int i;
  /* Round starting address down to longword boundary.  */
  CORE_ADDR addr = memaddr & -(CORE_ADDR) sizeof (TTRACE_XFER_TYPE);
  /* Round ending address up; get number of longwords that makes.  */
  int count
  = (((memaddr + len) - addr) + sizeof (TTRACE_XFER_TYPE) - 1)
  / sizeof (TTRACE_XFER_TYPE);
  /* Allocate buffer of that many longwords.  */
  /* FIXME (alloca): This code, cloned from infptrace.c, is unsafe
     because it uses alloca to allocate a buffer of arbitrary size.
     For very large xfers, this could crash GDB's stack.  */
  TTRACE_XFER_TYPE *buffer
    = (TTRACE_XFER_TYPE *) alloca (count * sizeof (TTRACE_XFER_TYPE));

  if (write)
    {
      /* Fill start and end extra bytes of buffer with existing memory data.  */

      if (addr != memaddr || len < (int) sizeof (TTRACE_XFER_TYPE))
	{
	  /* Need part of initial word -- fetch it.  */
	  buffer[0] = call_ttrace (TT_LWP_RDTEXT,
				   PIDGET (inferior_ptid),
				   (TTRACE_ARG_TYPE) addr,
				   TT_NIL,
				   TT_NIL);
	}

      if (count > 1)		/* FIXME, avoid if even boundary */
	{
	  buffer[count - 1] = call_ttrace (TT_LWP_RDTEXT,
					   PIDGET (inferior_ptid),
					   ((TTRACE_ARG_TYPE)
			  (addr + (count - 1) * sizeof (TTRACE_XFER_TYPE))),
					   TT_NIL,
					   TT_NIL);
	}

      /* Copy data to be written over corresponding part of buffer */

      memcpy ((char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
	      myaddr,
	      len);

      /* Write the entire buffer.  */

      for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
	{
	  errno = 0;
	  call_ttrace (TT_LWP_WRDATA,
		       PIDGET (inferior_ptid),
		       (TTRACE_ARG_TYPE) addr,
		       (TTRACE_ARG_TYPE) buffer[i],
		       TT_NIL);
	  if (errno)
	    {
	      /* Using the appropriate one (I or D) is necessary for
	         Gould NP1, at least.  */
	      errno = 0;
	      call_ttrace (TT_LWP_WRTEXT,
			   PIDGET (inferior_ptid),
			   (TTRACE_ARG_TYPE) addr,
			   (TTRACE_ARG_TYPE) buffer[i],
			   TT_NIL);
	    }
	  if (errno)
	    return 0;
	}
    }
  else
    {
      /* Read all the longwords */
      for (i = 0; i < count; i++, addr += sizeof (TTRACE_XFER_TYPE))
	{
	  errno = 0;
	  buffer[i] = call_ttrace (TT_LWP_RDTEXT,
				   PIDGET (inferior_ptid),
				   (TTRACE_ARG_TYPE) addr,
				   TT_NIL,
				   TT_NIL);
	  if (errno)
	    return 0;
	  QUIT;
	}

      /* Copy appropriate bytes out of the buffer.  */
      memcpy (myaddr,
	      (char *) buffer + (memaddr & (sizeof (TTRACE_XFER_TYPE) - 1)),
	      len);
    }
  return len;
}


static void
udot_info (void)
{
  int udot_off;			/* Offset into user struct */
  int udot_val;			/* Value from user struct at udot_off */
  char mess[128];		/* For messages */

  if (!target_has_execution)
    {
      error ("The program is not being run.");
    }

#if !defined (KERNEL_U_SIZE)

  /* Adding support for this command is easy.  Typically you just add a
     routine, called "kernel_u_size" that returns the size of the user
     struct, to the appropriate *-nat.c file and then add to the native
     config file "#define KERNEL_U_SIZE kernel_u_size()" */
  error ("Don't know how large ``struct user'' is in this version of gdb.");

#else

  for (udot_off = 0; udot_off < KERNEL_U_SIZE; udot_off += sizeof (udot_val))
    {
      if ((udot_off % 24) == 0)
	{
	  if (udot_off > 0)
	    {
	      printf_filtered ("\n");
	    }
	  printf_filtered ("%04x:", udot_off);
	}
      udot_val = call_ttrace (TT_LWP_RUREGS,
			      PIDGET (inferior_ptid),
			      (TTRACE_ARG_TYPE) udot_off,
			      TT_NIL,
			      TT_NIL);
      if (errno != 0)
	{
	  sprintf (mess, "\nreading user struct at offset 0x%x", udot_off);
	  perror_with_name (mess);
	}
      /* Avoid using nonportable (?) "*" in print specs */
      printf_filtered (sizeof (int) == 4 ? " 0x%08x" : " 0x%16x", udot_val);
    }
  printf_filtered ("\n");

#endif
}
#endif /* !defined (CHILD_XFER_MEMORY).  */


/* TTrace version of "target_pid_to_exec_file"
 */
char *
child_pid_to_exec_file (int tid)
{
  int tt_status;
  static char exec_file_buffer[1024];
  pid_t pid;
  static struct pst_status buf;

  /* On various versions of hpux11, this may fail due to a supposed
     kernel bug.  We have alternate methods to get this information
     (ie pstat).  */
  tt_status = call_ttrace (TT_PROC_GET_PATHNAME,
			   tid,
			   (uint64_t) exec_file_buffer,
			   sizeof (exec_file_buffer) - 1,
			   0);
  if (tt_status >= 0)
    return exec_file_buffer;

  /* Try to get process information via pstat and extract the filename
     from the pst_cmd field within the pst_status structure.  */
  if (pstat_getproc (&buf, sizeof (struct pst_status), 0, tid) != -1)
    {
      char *p = buf.pst_cmd;

      while (*p && *p != ' ')
	p++;
      *p = 0;

      return (buf.pst_cmd);
    }

  return (NULL);
}

void
pre_fork_inferior (void)
{
  int status;

  status = pipe (startup_semaphore.parent_channel);
  if (status < 0)
    {
      warning ("error getting parent pipe for startup semaphore");
      return;
    }

  status = pipe (startup_semaphore.child_channel);
  if (status < 0)
    {
      warning ("error getting child pipe for startup semaphore");
      return;
    }
}

/* Called from child_follow_fork in hppah-nat.c.
 *
 * This seems to be intended to attach after a fork or
 * vfork, while "attach" is used to attach to a pid
 * given by the user.  The check for an existing attach
 * seems odd--it always fails in our test system.
 */
int
hppa_require_attach (int pid)
{
  int tt_status;
  CORE_ADDR pc;
  CORE_ADDR pc_addr;
  unsigned int regs_offset;
  process_state_t old_process_state = process_state;

  /* Are we already attached?  There appears to be no explicit
   * way to answer this via ttrace, so we try something which
   * should be innocuous if we are attached.  If that fails,
   * then we assume we're not attached, and so attempt to make
   * it so.
   */
  errno = 0;
  tt_status = call_real_ttrace (TT_PROC_STOP,
				pid,
				(lwpid_t) TT_NIL,
				(TTRACE_ARG_TYPE) TT_NIL,
				(TTRACE_ARG_TYPE) TT_NIL,
				TT_NIL);

  if (errno)
    {
      /* No change to process-state!
       */
      errno = 0;
      pid = attach (pid);
    }
  else
    {
      /* If successful, the process is now stopped.  But if
       * we're VFORKING, the parent is still running, so don't
       * change the process state.
       */
      if (process_state != VFORKING)
	process_state = STOPPED;

      /* If we were already attached, you'd think that we
       * would need to start going again--but you'd be wrong,
       * as the fork-following code is actually in the middle
       * of the "resume" routine in in "infrun.c" and so
       * will (almost) immediately do a resume.
       *
       * On the other hand, if we are VFORKING, which means
       * that the child and the parent share a process for a
       * while, we know that "resume" won't be resuming
       * until the child EXEC event is seen.  But we still
       * don't want to continue, as the event is already
       * there waiting.
       */
      update_thread_state_after_attach (pid, DONT_ATTACH_CONTINUE);
    }				/* STOP succeeded */

  return pid;
}

int
hppa_require_detach (int pid, int signal)
{
  int tt_status;

  /* If signal is non-zero, we must pass the signal on to the active
     thread prior to detaching.  We do this by continuing the threads
     with the signal.
   */
  if (signal != 0)
    {
      errno = 0;
      threads_continue_all_with_signals (pid, signal);
    }

  errno = 0;
  tt_status = call_ttrace (TT_PROC_DETACH,
			   pid,
			   TT_NIL,
			   TT_NIL,
			   TT_NIL);

  errno = 0;			/* Ignore any errors. */

  /* process_state? */

  return pid;
}

/* Given the starting address of a memory page, hash it to a bucket in
   the memory page dictionary.
 */
static int
get_dictionary_bucket_of_page (CORE_ADDR page_start)
{
  int hash;

  hash = (page_start / memory_page_dictionary.page_size);
  hash = hash % MEMORY_PAGE_DICTIONARY_BUCKET_COUNT;

  return hash;
}


/* Given a memory page's starting address, get (i.e., find an existing
   or create a new) dictionary entry for the page.  The page will be
   write-protected when this function returns, but may have a reference
   count of 0 (if the page was newly-added to the dictionary).
 */
static memory_page_t *
get_dictionary_entry_of_page (int pid, CORE_ADDR page_start)
{
  int bucket;
  memory_page_t *page = NULL;
  memory_page_t *previous_page = NULL;

  /* We're going to be using the dictionary now, than-kew. */
  require_memory_page_dictionary ();

  /* Try to find an existing dictionary entry for this page.  Hash
     on the page's starting address.
   */
  bucket = get_dictionary_bucket_of_page (page_start);
  page = &memory_page_dictionary.buckets[bucket];
  while (page != NULL)
    {
      if (page->page_start == page_start)
	break;
      previous_page = page;
      page = page->next;
    }

  /* Did we find a dictionary entry for this page?  If not, then
     add it to the dictionary now.
   */
  if (page == NULL)
    {
      /* Create a new entry. */
      page = (memory_page_t *) xmalloc (sizeof (memory_page_t));
      page->page_start = page_start;
      page->reference_count = 0;
      page->next = NULL;
      page->previous = NULL;

      /* We'll write-protect the page now, if that's allowed. */
      page->original_permissions = write_protect_page (pid, page_start);

      /* Add the new entry to the dictionary. */
      page->previous = previous_page;
      previous_page->next = page;

      memory_page_dictionary.page_count++;
    }

  return page;
}


static void
remove_dictionary_entry_of_page (int pid, memory_page_t *page)
{
  /* Restore the page's original permissions. */
  unwrite_protect_page (pid, page->page_start, page->original_permissions);

  /* Kick the page out of the dictionary. */
  if (page->previous != NULL)
    page->previous->next = page->next;
  if (page->next != NULL)
    page->next->previous = page->previous;

  /* Just in case someone retains a handle to this after it's freed. */
  page->page_start = (CORE_ADDR) 0;

  memory_page_dictionary.page_count--;

  xfree (page);
}


static void
hppa_enable_syscall_events (int pid)
{
  int tt_status;
  ttevent_t ttrace_events;

  /* Get the set of events that are currently enabled. */
  tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
			   pid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);
  if (errno)
    perror_with_name ("ttrace");

  /* Add syscall events to that set. */
  ttrace_events.tte_events |= TTEVT_SYSCALL_ENTRY;
  ttrace_events.tte_events |= TTEVT_SYSCALL_RETURN;

  tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
			   pid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);
  if (errno)
    perror_with_name ("ttrace");
}


static void
hppa_disable_syscall_events (int pid)
{
  int tt_status;
  ttevent_t ttrace_events;

  /* Get the set of events that are currently enabled. */
  tt_status = call_ttrace (TT_PROC_GET_EVENT_MASK,
			   pid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);
  if (errno)
    perror_with_name ("ttrace");

  /* Remove syscall events from that set. */
  ttrace_events.tte_events &= ~TTEVT_SYSCALL_ENTRY;
  ttrace_events.tte_events &= ~TTEVT_SYSCALL_RETURN;

  tt_status = call_ttrace (TT_PROC_SET_EVENT_MASK,
			   pid,
			   (TTRACE_ARG_TYPE) & ttrace_events,
			   (TTRACE_ARG_TYPE) sizeof (ttrace_events),
			   TT_NIL);
  if (errno)
    perror_with_name ("ttrace");
}


/* The address range beginning with START and ending with START+LEN-1
   (inclusive) is to be watched via page-protection by a new watchpoint.
   Set protection for all pages that overlap that range.

   Note that our caller sets TYPE to:
   0 for a bp_hardware_watchpoint,
   1 for a bp_read_watchpoint,
   2 for a bp_access_watchpoint

   (Yes, this is intentionally (though lord only knows why) different
   from the TYPE that is passed to hppa_remove_hw_watchpoint.)
 */
int
hppa_insert_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
{
  CORE_ADDR page_start;
  int dictionary_was_empty;
  int page_size;
  int page_id;
  LONGEST range_size_in_pages;

  if (type != 0)
    error ("read or access hardware watchpoints not supported on HP-UX");

  /* Examine all pages in the address range. */
  require_memory_page_dictionary ();

  dictionary_was_empty = (memory_page_dictionary.page_count == (LONGEST) 0);

  page_size = memory_page_dictionary.page_size;
  page_start = (start / page_size) * page_size;
  range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;

  for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
    {
      memory_page_t *page;

      /* This gets the page entered into the dictionary if it was
         not already entered.
       */
      page = get_dictionary_entry_of_page (pid, page_start);
      page->reference_count++;
    }

  /* Our implementation depends on seeing calls to kernel code, for the
     following reason.  Here we ask to be notified of syscalls.

     When a protected page is accessed by user code, HP-UX raises a SIGBUS.
     Fine.

     But when kernel code accesses the page, it doesn't give a SIGBUS.
     Rather, the system call that touched the page fails, with errno=EFAULT.
     Not good for us.

     We could accomodate this "feature" by asking to be notified of syscall
     entries & exits; upon getting an entry event, disabling page-protections;
     upon getting an exit event, reenabling page-protections and then checking
     if any watchpoints triggered.

     However, this turns out to be a real performance loser.  syscalls are
     usually a frequent occurrence.  Having to unprotect-reprotect all watched
     pages, and also to then read all watched memory locations and compare for
     triggers, can be quite expensive.

     Instead, we'll only ask to be notified of syscall exits.  When we get
     one, we'll check whether errno is set.  If not, or if it's not EFAULT,
     we can just continue the inferior.

     If errno is set upon syscall exit to EFAULT, we must perform some fairly
     hackish stuff to determine whether the failure really was due to a
     page-protect trap on a watched location.
   */
  if (dictionary_was_empty)
    hppa_enable_syscall_events (pid);

  return 1;
}


/* The address range beginning with START and ending with START+LEN-1
   (inclusive) was being watched via page-protection by a watchpoint
   which has been removed.  Remove protection for all pages that
   overlap that range, which are not also being watched by other
   watchpoints.
 */
int
hppa_remove_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len, int type)
{
  CORE_ADDR page_start;
  int dictionary_is_empty;
  int page_size;
  int page_id;
  LONGEST range_size_in_pages;

  if (type != 0)
    error ("read or access hardware watchpoints not supported on HP-UX");

  /* Examine all pages in the address range. */
  require_memory_page_dictionary ();

  page_size = memory_page_dictionary.page_size;
  page_start = (start / page_size) * page_size;
  range_size_in_pages = ((LONGEST) len + (LONGEST) page_size - 1) / (LONGEST) page_size;

  for (page_id = 0; page_id < range_size_in_pages; page_id++, page_start += page_size)
    {
      memory_page_t *page;

      page = get_dictionary_entry_of_page (pid, page_start);
      page->reference_count--;

      /* Was this the last reference of this page?  If so, then we
         must scrub the entry from the dictionary, and also restore
         the page's original permissions.
       */
      if (page->reference_count == 0)
	remove_dictionary_entry_of_page (pid, page);
    }

  dictionary_is_empty = (memory_page_dictionary.page_count == (LONGEST) 0);

  /* If write protections are currently disallowed, then that implies that
     wait_for_inferior believes that the inferior is within a system call.
     Since we want to see both syscall entry and return, it's clearly not
     good to disable syscall events in this state!

     ??rehrauer: Yeah, it'd be better if we had a specific flag that said,
     "inferior is between syscall events now".  Oh well.
   */
  if (dictionary_is_empty && memory_page_dictionary.page_protections_allowed)
    hppa_disable_syscall_events (pid);

  return 1;
}


/* Could we implement a watchpoint of this type via our available
   hardware support?

   This query does not consider whether a particular address range
   could be so watched, but just whether support is generally available
   for such things.  See hppa_range_profitable_for_hw_watchpoint for a
   query that answers whether a particular range should be watched via
   hardware support.
 */
int
hppa_can_use_hw_watchpoint (int type, int cnt, int ot)
{
  return (type == bp_hardware_watchpoint);
}


/* Assuming we could set a hardware watchpoint on this address, do
   we think it would be profitable ("a good idea") to do so?  If not,
   we can always set a regular (aka single-step & test) watchpoint
   on the address...
 */
int
hppa_range_profitable_for_hw_watchpoint (int pid, CORE_ADDR start, LONGEST len)
{
  int range_is_stack_based;
  int range_is_accessible;
  CORE_ADDR page_start;
  int page_size;
  int page;
  LONGEST range_size_in_pages;

  /* ??rehrauer: For now, say that all addresses are potentially
     profitable.  Possibly later we'll want to test the address
     for "stackness"?
   */
  range_is_stack_based = 0;

  /* If any page in the range is inaccessible, then we cannot
     really use hardware watchpointing, even though our client
     thinks we can.  In that case, it's actually an error to
     attempt to use hw watchpoints, so we'll tell our client
     that the range is "unprofitable", and hope that they listen...
   */
  range_is_accessible = 1;	/* Until proven otherwise. */

  /* Examine all pages in the address range. */
  errno = 0;
  page_size = sysconf (_SC_PAGE_SIZE);

  /* If we can't determine page size, we're hosed.  Tell our
     client it's unprofitable to use hw watchpoints for this
     range.
   */
  if (errno || (page_size <= 0))
    {
      errno = 0;
      return 0;
    }

  page_start = (start / page_size) * page_size;
  range_size_in_pages = len / (LONGEST) page_size;

  for (page = 0; page < range_size_in_pages; page++, page_start += page_size)
    {
      int tt_status;
      int page_permissions;

      /* Is this page accessible? */
      errno = 0;
      tt_status = call_ttrace (TT_PROC_GET_MPROTECT,
			       pid,
			       (TTRACE_ARG_TYPE) page_start,
			       TT_NIL,
			       (TTRACE_ARG_TYPE) & page_permissions);
      if (errno || (tt_status < 0))
	{
	  errno = 0;
	  range_is_accessible = 0;
	  break;
	}

      /* Yes, go for another... */
    }

  return (!range_is_stack_based && range_is_accessible);
}


char *
hppa_pid_or_tid_to_str (ptid_t ptid)
{
  static char buf[100];		/* Static because address returned. */
  pid_t id = PIDGET (ptid);

  /* Does this appear to be a process?  If so, print it that way. */
  if (is_process_id (id))
    return child_pid_to_str (ptid);

  /* Else, print both the GDB thread number and the system thread id. */
  sprintf (buf, "thread %d (", pid_to_thread_id (ptid));
  strcat (buf, hppa_tid_to_str (ptid));
  strcat (buf, ")\0");

  return buf;
}


void
hppa_ensure_vforking_parent_remains_stopped (int pid)
{
  /* Nothing to do when using ttrace.  Only the ptrace-based implementation
     must do real work.
   */
}


int
hppa_resume_execd_vforking_child_to_get_parent_vfork (void)
{
  return 0;			/* No, the parent vfork is available now. */
}


/* Write a register as a 64bit value.  This may be necessary if the
   native OS is too braindamaged to allow some (or all) registers to
   be written in 32bit hunks such as hpux11 and the PC queue registers.

   This is horribly gross and disgusting.  */
 
int
ttrace_write_reg_64 (int gdb_tid, CORE_ADDR dest_addr, CORE_ADDR src_addr)
{
  pid_t 	pid;
  lwpid_t 	tid;
  int  		tt_status;

  tid = map_from_gdb_tid (gdb_tid);
  pid = get_pid_for (tid);

  errno = 0;
  tt_status = ttrace (TT_LWP_WUREGS, 
		      pid, 
		      tid, 
		      (TTRACE_ARG_TYPE) dest_addr, 
		      8, 
		      (TTRACE_ARG_TYPE) src_addr );

#ifdef THREAD_DEBUG
  if (errno)
    {
      /* Don't bother for a known benign error: if you ask for the
         first thread state, but there is only one thread and it's
         not stopped, ttrace complains.
        
         We have this inside the #ifdef because our caller will do
         this check for real.  */
      if( request != TT_PROC_GET_FIRST_LWP_STATE
          ||  errno   != EPROTO )
        {
          if( debug_on )
            printf( "TT fail for %s, with pid %d, tid %d, status %d \n",
                    get_printable_name_of_ttrace_request (TT_LWP_WUREGS),
                    pid, tid, tt_status );
        }
    }
#endif

  return tt_status;
}

void
_initialize_infttrace (void)
{
  /* Initialize the ttrace-based hardware watchpoint implementation. */
  memory_page_dictionary.page_count = (LONGEST) - 1;
  memory_page_dictionary.page_protections_allowed = 1;

  errno = 0;
  memory_page_dictionary.page_size = sysconf (_SC_PAGE_SIZE);

  /* We do a lot of casts from pointers to TTRACE_ARG_TYPE; make sure
     this is okay.  */
  if (sizeof (TTRACE_ARG_TYPE) < sizeof (void *))
    internal_error (__FILE__, __LINE__, "failed internal consistency check");

  if (errno || (memory_page_dictionary.page_size <= 0))
    perror_with_name ("sysconf");
}