1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
12035
12036
12037
12038
12039
12040
12041
12042
12043
12044
12045
12046
12047
12048
12049
12050
12051
12052
12053
12054
12055
12056
12057
12058
12059
12060
12061
12062
12063
12064
12065
12066
12067
12068
12069
12070
12071
12072
12073
12074
12075
12076
12077
12078
12079
12080
12081
12082
12083
12084
12085
12086
12087
12088
12089
12090
12091
12092
12093
12094
12095
12096
12097
12098
12099
12100
12101
12102
12103
12104
12105
12106
12107
12108
12109
12110
12111
12112
12113
12114
12115
12116
12117
12118
12119
12120
12121
12122
12123
12124
12125
12126
12127
12128
12129
12130
12131
12132
12133
12134
12135
12136
12137
12138
12139
12140
12141
12142
12143
12144
12145
12146
12147
12148
12149
12150
12151
12152
12153
12154
12155
12156
12157
12158
12159
12160
12161
12162
12163
12164
12165
12166
12167
12168
12169
12170
12171
12172
12173
12174
12175
12176
12177
12178
12179
12180
12181
12182
12183
12184
12185
12186
12187
12188
12189
12190
12191
12192
12193
12194
12195
12196
12197
12198
12199
12200
12201
12202
12203
12204
12205
12206
12207
12208
12209
12210
12211
12212
12213
12214
12215
12216
12217
12218
12219
12220
12221
12222
12223
12224
12225
12226
12227
12228
12229
12230
12231
12232
12233
12234
12235
12236
12237
12238
12239
12240
12241
12242
12243
12244
12245
12246
12247
12248
12249
12250
12251
12252
12253
12254
12255
12256
12257
12258
12259
12260
12261
12262
12263
12264
12265
12266
12267
12268
12269
12270
12271
12272
12273
12274
12275
12276
12277
12278
12279
12280
12281
12282
12283
12284
12285
12286
12287
12288
12289
12290
12291
12292
12293
12294
12295
12296
12297
12298
12299
12300
12301
12302
12303
12304
12305
12306
12307
12308
12309
12310
12311
12312
12313
12314
12315
12316
12317
12318
12319
12320
12321
12322
12323
12324
12325
12326
12327
12328
12329
12330
12331
12332
12333
12334
12335
12336
12337
12338
12339
12340
12341
12342
12343
12344
12345
12346
12347
12348
12349
12350
12351
12352
12353
12354
12355
12356
12357
12358
12359
12360
12361
12362
12363
12364
12365
12366
12367
12368
12369
12370
12371
12372
12373
12374
12375
12376
12377
12378
12379
12380
12381
12382
12383
12384
12385
12386
12387
12388
12389
12390
12391
12392
12393
12394
12395
12396
12397
12398
12399
12400
12401
12402
12403
12404
12405
12406
12407
12408
12409
12410
12411
12412
12413
12414
12415
12416
12417
12418
12419
12420
12421
12422
12423
12424
12425
12426
12427
12428
12429
12430
12431
12432
12433
12434
12435
12436
12437
12438
12439
12440
12441
12442
12443
12444
12445
12446
12447
12448
12449
12450
12451
12452
12453
12454
12455
12456
12457
12458
12459
12460
12461
12462
12463
12464
12465
12466
12467
12468
12469
12470
12471
12472
12473
12474
12475
12476
12477
12478
12479
12480
12481
12482
12483
12484
12485
12486
12487
12488
12489
12490
12491
12492
12493
12494
12495
12496
12497
12498
12499
12500
12501
12502
12503
12504
12505
12506
12507
12508
12509
12510
12511
12512
12513
12514
12515
12516
12517
12518
12519
12520
12521
12522
12523
12524
12525
12526
12527
12528
12529
12530
12531
12532
12533
12534
12535
12536
12537
12538
12539
12540
12541
12542
12543
12544
12545
12546
12547
12548
12549
12550
12551
12552
12553
12554
12555
12556
12557
12558
12559
12560
12561
12562
12563
12564
12565
12566
12567
12568
12569
12570
12571
12572
12573
12574
12575
12576
12577
12578
12579
12580
12581
12582
12583
12584
12585
12586
12587
12588
12589
12590
12591
12592
12593
12594
12595
12596
12597
12598
12599
12600
12601
12602
12603
12604
12605
12606
12607
12608
12609
12610
12611
12612
12613
12614
12615
12616
12617
12618
12619
12620
12621
12622
12623
12624
12625
12626
12627
12628
12629
12630
12631
12632
12633
12634
12635
12636
12637
12638
12639
12640
12641
12642
12643
12644
12645
12646
12647
12648
12649
12650
12651
12652
12653
12654
12655
12656
12657
12658
12659
12660
12661
12662
12663
12664
12665
12666
12667
12668
12669
12670
12671
12672
12673
12674
12675
12676
12677
12678
12679
12680
12681
12682
12683
12684
12685
12686
12687
12688
12689
12690
12691
12692
12693
12694
12695
12696
12697
12698
12699
12700
12701
12702
12703
12704
12705
12706
12707
12708
12709
12710
12711
12712
12713
12714
12715
12716
12717
12718
12719
12720
12721
12722
12723
12724
12725
12726
12727
12728
12729
12730
12731
12732
12733
12734
12735
12736
12737
12738
12739
12740
12741
12742
12743
12744
12745
12746
12747
12748
12749
12750
12751
12752
12753
12754
12755
12756
12757
12758
12759
12760
12761
12762
12763
12764
12765
12766
12767
12768
12769
12770
12771
12772
12773
12774
12775
12776
12777
12778
12779
12780
12781
12782
12783
12784
12785
12786
12787
12788
12789
12790
12791
12792
12793
12794
12795
12796
12797
12798
12799
12800
12801
12802
12803
12804
12805
12806
12807
12808
12809
12810
12811
12812
12813
12814
12815
12816
12817
12818
12819
12820
12821
12822
12823
12824
12825
12826
12827
12828
12829
12830
12831
12832
12833
12834
12835
12836
12837
12838
12839
12840
12841
12842
12843
12844
12845
12846
12847
12848
12849
12850
12851
12852
12853
12854
12855
12856
12857
12858
12859
12860
12861
12862
12863
12864
12865
12866
12867
12868
12869
12870
12871
12872
12873
12874
12875
12876
12877
12878
12879
12880
12881
12882
12883
12884
12885
12886
12887
12888
12889
12890
12891
12892
12893
12894
12895
12896
12897
12898
12899
12900
12901
12902
12903
12904
12905
12906
12907
12908
12909
12910
12911
12912
12913
12914
12915
12916
12917
12918
12919
12920
12921
12922
12923
12924
12925
12926
12927
12928
12929
12930
12931
12932
12933
12934
12935
12936
12937
12938
12939
12940
12941
12942
12943
12944
12945
12946
12947
12948
12949
12950
12951
12952
12953
12954
12955
12956
12957
12958
12959
12960
12961
12962
12963
12964
12965
12966
12967
12968
12969
12970
12971
12972
12973
12974
12975
12976
12977
12978
12979
12980
12981
12982
12983
12984
12985
12986
12987
12988
12989
12990
12991
12992
12993
12994
12995
12996
12997
12998
12999
13000
13001
13002
13003
13004
13005
13006
13007
13008
13009
13010
13011
13012
13013
13014
13015
13016
13017
13018
13019
13020
13021
13022
13023
13024
13025
13026
13027
13028
13029
13030
13031
13032
13033
13034
13035
13036
13037
13038
13039
13040
13041
13042
13043
13044
13045
13046
13047
13048
13049
13050
13051
13052
13053
13054
13055
13056
13057
13058
13059
13060
13061
13062
13063
13064
13065
13066
13067
13068
13069
13070
13071
13072
13073
13074
13075
13076
13077
13078
13079
13080
13081
13082
13083
13084
13085
13086
13087
13088
13089
13090
13091
13092
13093
13094
13095
13096
13097
13098
13099
13100
13101
13102
13103
13104
13105
13106
13107
13108
13109
13110
13111
13112
13113
13114
13115
13116
13117
13118
13119
13120
13121
13122
13123
13124
13125
13126
13127
13128
13129
13130
13131
13132
13133
13134
13135
13136
13137
13138
13139
13140
13141
13142
13143
13144
13145
13146
13147
13148
13149
13150
13151
13152
13153
13154
13155
13156
13157
13158
13159
13160
13161
13162
13163
13164
13165
13166
13167
13168
13169
13170
13171
13172
13173
13174
13175
13176
13177
13178
13179
13180
13181
13182
13183
13184
13185
13186
13187
13188
13189
13190
13191
13192
13193
13194
13195
13196
13197
13198
13199
13200
13201
13202
13203
13204
13205
13206
13207
13208
13209
13210
13211
13212
13213
13214
13215
13216
13217
13218
13219
13220
13221
13222
13223
13224
13225
13226
13227
13228
13229
13230
13231
13232
13233
13234
13235
13236
13237
13238
13239
13240
13241
13242
13243
13244
13245
13246
13247
13248
13249
13250
13251
13252
13253
13254
13255
13256
13257
13258
13259
13260
13261
13262
13263
13264
13265
13266
13267
13268
13269
13270
13271
13272
13273
13274
13275
13276
13277
13278
13279
13280
13281
13282
13283
13284
13285
13286
13287
13288
13289
13290
13291
13292
13293
13294
13295
13296
13297
13298
13299
13300
13301
13302
13303
13304
13305
13306
13307
13308
13309
13310
13311
13312
13313
13314
13315
13316
13317
13318
13319
13320
13321
13322
13323
13324
13325
13326
13327
13328
13329
13330
13331
13332
13333
13334
13335
13336
13337
13338
13339
13340
13341
13342
13343
13344
13345
13346
13347
13348
13349
13350
13351
13352
13353
13354
13355
13356
13357
13358
13359
13360
13361
13362
13363
13364
13365
13366
13367
13368
13369
13370
13371
13372
13373
13374
13375
13376
13377
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
13394
13395
13396
13397
13398
13399
13400
13401
13402
13403
13404
13405
13406
13407
13408
13409
13410
13411
13412
13413
13414
13415
13416
13417
13418
13419
13420
13421
13422
13423
13424
13425
13426
13427
13428
13429
13430
13431
13432
13433
13434
13435
13436
13437
13438
13439
13440
13441
13442
13443
13444
13445
13446
13447
13448
13449
13450
13451
13452
13453
13454
13455
13456
13457
13458
13459
13460
13461
13462
13463
13464
13465
13466
13467
13468
13469
13470
13471
13472
13473
13474
13475
13476
13477
13478
13479
13480
13481
13482
13483
13484
13485
13486
13487
13488
13489
13490
13491
13492
13493
13494
13495
13496
13497
13498
13499
13500
13501
13502
13503
13504
13505
13506
13507
13508
13509
13510
13511
13512
13513
13514
13515
13516
13517
13518
13519
13520
13521
13522
13523
13524
13525
13526
13527
13528
13529
13530
13531
13532
13533
13534
13535
13536
13537
13538
13539
13540
13541
13542
13543
13544
13545
13546
13547
13548
13549
13550
13551
13552
13553
13554
13555
13556
13557
13558
13559
13560
13561
13562
13563
13564
13565
13566
13567
13568
13569
13570
13571
13572
13573
13574
13575
13576
13577
13578
13579
13580
13581
13582
13583
13584
13585
13586
13587
13588
13589
13590
13591
13592
13593
13594
13595
13596
13597
13598
13599
13600
13601
13602
13603
13604
13605
13606
13607
13608
13609
13610
13611
13612
13613
13614
13615
13616
13617
13618
13619
13620
13621
13622
13623
13624
13625
13626
13627
13628
13629
13630
13631
13632
13633
13634
13635
13636
13637
13638
13639
13640
13641
13642
13643
13644
13645
13646
13647
13648
13649
13650
13651
13652
13653
13654
13655
13656
13657
13658
13659
13660
13661
13662
13663
13664
13665
13666
13667
13668
13669
13670
13671
13672
13673
13674
13675
13676
13677
13678
13679
13680
13681
13682
13683
13684
13685
13686
13687
13688
13689
13690
13691
13692
13693
13694
13695
13696
13697
13698
13699
13700
13701
13702
13703
13704
13705
13706
13707
13708
13709
13710
13711
13712
13713
13714
13715
13716
13717
13718
13719
13720
13721
13722
13723
13724
13725
13726
13727
13728
13729
13730
13731
13732
13733
13734
13735
13736
13737
13738
13739
13740
13741
13742
13743
13744
13745
13746
13747
13748
13749
13750
13751
13752
13753
13754
13755
13756
13757
13758
13759
13760
13761
13762
13763
13764
13765
13766
13767
13768
13769
13770
13771
13772
13773
13774
13775
13776
13777
13778
13779
13780
13781
13782
13783
13784
13785
13786
13787
13788
13789
13790
13791
13792
13793
13794
13795
13796
13797
13798
13799
13800
13801
13802
13803
13804
13805
13806
13807
13808
13809
13810
13811
13812
13813
13814
13815
13816
13817
13818
13819
13820
13821
13822
13823
13824
13825
13826
13827
13828
13829
13830
13831
13832
13833
13834
13835
13836
13837
13838
13839
13840
13841
13842
13843
13844
13845
13846
13847
13848
13849
13850
13851
13852
13853
13854
13855
13856
13857
13858
13859
13860
13861
13862
13863
13864
13865
13866
13867
13868
13869
13870
13871
13872
13873
13874
13875
13876
13877
13878
13879
13880
13881
13882
13883
13884
13885
13886
13887
13888
13889
13890
13891
13892
13893
13894
13895
13896
13897
13898
13899
13900
13901
13902
13903
13904
13905
13906
13907
13908
13909
13910
13911
13912
13913
13914
13915
13916
13917
13918
13919
13920
13921
13922
13923
13924
13925
13926
13927
13928
13929
13930
13931
13932
13933
13934
13935
13936
13937
13938
13939
13940
13941
13942
13943
13944
13945
13946
13947
13948
13949
13950
13951
13952
13953
13954
13955
13956
13957
13958
13959
13960
13961
13962
13963
13964
13965
13966
13967
13968
13969
13970
13971
13972
13973
13974
13975
13976
13977
13978
13979
13980
13981
13982
13983
13984
13985
13986
13987
13988
13989
13990
13991
13992
13993
13994
13995
13996
13997
13998
13999
14000
14001
14002
14003
14004
14005
14006
14007
14008
14009
14010
14011
14012
14013
14014
14015
14016
14017
14018
14019
14020
14021
14022
14023
14024
14025
14026
14027
14028
14029
14030
14031
14032
14033
14034
14035
14036
14037
14038
14039
14040
14041
14042
14043
14044
14045
14046
14047
14048
14049
14050
14051
14052
14053
14054
14055
14056
14057
14058
14059
14060
14061
14062
14063
14064
14065
14066
14067
14068
14069
14070
14071
14072
14073
14074
14075
14076
14077
14078
14079
14080
14081
14082
14083
14084
14085
14086
14087
14088
14089
14090
14091
14092
14093
14094
14095
14096
14097
14098
14099
14100
14101
14102
14103
14104
14105
14106
14107
14108
14109
14110
14111
14112
14113
14114
14115
14116
14117
14118
14119
14120
14121
14122
14123
14124
14125
14126
14127
14128
14129
14130
14131
14132
14133
14134
14135
14136
14137
14138
14139
14140
14141
14142
14143
14144
14145
14146
14147
14148
14149
14150
14151
14152
14153
14154
14155
14156
14157
14158
14159
14160
14161
14162
14163
14164
14165
14166
14167
14168
14169
14170
14171
14172
14173
14174
14175
14176
14177
14178
14179
14180
14181
14182
14183
14184
14185
14186
14187
14188
14189
14190
14191
14192
14193
14194
14195
14196
14197
14198
14199
14200
14201
14202
14203
14204
14205
14206
14207
14208
14209
14210
14211
14212
14213
14214
14215
14216
14217
14218
14219
14220
14221
14222
14223
14224
14225
14226
14227
14228
14229
14230
14231
14232
14233
14234
14235
14236
14237
14238
14239
14240
14241
14242
14243
14244
14245
14246
14247
14248
14249
14250
14251
14252
14253
14254
14255
14256
14257
14258
14259
14260
14261
14262
14263
14264
14265
14266
14267
14268
14269
14270
14271
14272
14273
14274
14275
14276
14277
14278
14279
14280
14281
14282
14283
14284
14285
14286
14287
14288
14289
14290
14291
14292
14293
14294
14295
14296
14297
14298
14299
14300
14301
14302
14303
14304
14305
14306
14307
14308
14309
14310
14311
14312
14313
14314
14315
14316
14317
14318
14319
14320
14321
14322
14323
14324
14325
14326
14327
14328
14329
14330
14331
14332
14333
14334
14335
14336
14337
14338
14339
14340
14341
14342
14343
14344
14345
14346
14347
14348
14349
14350
14351
14352
14353
14354
14355
14356
14357
14358
14359
14360
14361
14362
14363
14364
14365
14366
14367
14368
14369
14370
14371
14372
14373
14374
14375
14376
14377
14378
14379
14380
14381
14382
14383
14384
14385
14386
14387
14388
14389
14390
14391
14392
14393
14394
14395
14396
14397
14398
14399
14400
14401
14402
14403
14404
14405
14406
14407
14408
14409
14410
14411
14412
14413
14414
14415
14416
14417
14418
14419
14420
14421
14422
14423
14424
14425
14426
14427
14428
14429
14430
14431
14432
14433
14434
14435
14436
14437
14438
14439
14440
14441
14442
14443
14444
14445
14446
14447
14448
14449
14450
14451
14452
14453
14454
14455
14456
14457
14458
14459
14460
14461
14462
14463
14464
14465
14466
14467
14468
14469
14470
14471
14472
14473
14474
14475
14476
14477
14478
14479
14480
14481
14482
14483
14484
14485
14486
14487
14488
14489
14490
14491
14492
14493
14494
14495
14496
14497
14498
14499
14500
14501
14502
14503
14504
14505
14506
14507
14508
14509
14510
14511
14512
14513
14514
14515
14516
14517
14518
14519
14520
14521
14522
14523
14524
14525
14526
14527
14528
14529
14530
14531
14532
14533
14534
14535
14536
14537
14538
14539
14540
14541
14542
14543
14544
14545
14546
14547
14548
14549
14550
14551
14552
14553
14554
14555
14556
14557
14558
14559
14560
14561
14562
14563
14564
14565
14566
14567
14568
14569
14570
14571
14572
14573
14574
14575
14576
14577
14578
14579
14580
14581
14582
14583
14584
14585
14586
14587
14588
14589
14590
14591
14592
14593
14594
14595
14596
14597
14598
14599
14600
14601
14602
14603
14604
14605
14606
14607
14608
14609
14610
14611
14612
14613
14614
14615
14616
14617
14618
14619
14620
14621
14622
14623
14624
14625
14626
14627
14628
14629
14630
14631
14632
14633
14634
14635
14636
14637
14638
14639
14640
14641
14642
14643
14644
14645
14646
14647
14648
14649
14650
14651
14652
14653
14654
14655
14656
14657
14658
14659
14660
14661
14662
14663
14664
14665
14666
14667
14668
14669
14670
14671
14672
14673
14674
14675
14676
14677
14678
14679
14680
14681
14682
14683
14684
14685
14686
14687
14688
14689
14690
14691
14692
14693
14694
14695
14696
14697
14698
14699
14700
14701
14702
14703
14704
14705
14706
14707
14708
14709
14710
14711
14712
14713
14714
14715
14716
14717
14718
14719
14720
14721
14722
14723
14724
14725
14726
14727
14728
14729
14730
14731
14732
14733
14734
14735
14736
14737
14738
14739
14740
14741
14742
14743
14744
14745
14746
14747
14748
14749
14750
14751
14752
14753
14754
14755
14756
14757
14758
14759
14760
14761
14762
14763
14764
14765
14766
14767
14768
14769
14770
14771
14772
14773
14774
14775
14776
14777
14778
14779
14780
14781
14782
14783
14784
14785
14786
14787
14788
14789
14790
14791
14792
14793
14794
14795
14796
14797
14798
14799
14800
14801
14802
14803
14804
14805
14806
14807
14808
14809
14810
14811
14812
14813
14814
14815
14816
14817
14818
14819
14820
14821
14822
14823
14824
14825
14826
14827
14828
14829
14830
14831
14832
14833
14834
14835
14836
14837
14838
14839
14840
14841
14842
14843
14844
14845
14846
14847
14848
14849
14850
14851
14852
14853
14854
14855
14856
14857
14858
14859
14860
14861
14862
14863
14864
14865
14866
14867
14868
14869
14870
14871
14872
14873
14874
14875
14876
14877
14878
14879
14880
14881
14882
14883
14884
14885
14886
14887
14888
14889
14890
14891
14892
14893
14894
14895
14896
14897
14898
14899
14900
14901
14902
14903
14904
14905
14906
14907
14908
14909
14910
14911
14912
14913
14914
14915
14916
14917
14918
14919
14920
14921
14922
14923
14924
14925
14926
14927
14928
14929
14930
14931
14932
14933
14934
14935
14936
14937
14938
14939
14940
14941
14942
14943
14944
14945
14946
14947
14948
14949
14950
14951
14952
14953
14954
14955
14956
14957
14958
14959
14960
14961
14962
14963
14964
14965
14966
14967
14968
14969
14970
14971
14972
14973
14974
14975
14976
14977
14978
14979
14980
14981
14982
14983
14984
14985
14986
14987
14988
14989
14990
14991
14992
14993
14994
14995
14996
14997
14998
14999
15000
15001
15002
15003
15004
15005
15006
15007
15008
15009
15010
15011
15012
15013
15014
15015
15016
15017
15018
15019
15020
15021
15022
15023
15024
15025
15026
15027
15028
15029
15030
15031
15032
15033
15034
15035
15036
15037
15038
15039
15040
15041
15042
15043
15044
15045
15046
15047
15048
15049
15050
15051
15052
15053
15054
15055
15056
15057
15058
15059
15060
15061
15062
15063
15064
15065
15066
15067
15068
15069
15070
15071
15072
15073
15074
15075
15076
15077
15078
15079
15080
15081
15082
15083
15084
15085
15086
15087
15088
15089
15090
15091
15092
15093
15094
15095
15096
15097
15098
15099
15100
15101
15102
15103
15104
15105
15106
15107
15108
15109
15110
15111
15112
15113
15114
15115
15116
15117
15118
15119
15120
15121
15122
15123
15124
15125
15126
15127
15128
15129
15130
15131
15132
15133
15134
15135
15136
15137
15138
15139
15140
15141
15142
15143
15144
15145
15146
15147
15148
15149
15150
15151
15152
15153
15154
15155
15156
15157
15158
15159
15160
15161
15162
15163
15164
15165
15166
15167
15168
15169
15170
15171
15172
15173
15174
15175
15176
15177
15178
15179
15180
15181
15182
15183
15184
15185
15186
15187
15188
15189
15190
15191
15192
15193
15194
15195
15196
15197
15198
15199
15200
15201
15202
15203
15204
15205
15206
15207
15208
15209
15210
15211
15212
15213
15214
15215
15216
15217
15218
15219
15220
15221
15222
15223
15224
15225
15226
15227
15228
15229
15230
15231
15232
15233
15234
15235
15236
15237
15238
15239
15240
15241
15242
15243
15244
15245
15246
15247
15248
15249
15250
15251
15252
15253
15254
15255
15256
15257
15258
15259
15260
15261
15262
15263
15264
15265
15266
15267
15268
15269
15270
15271
15272
15273
15274
15275
15276
15277
15278
15279
15280
15281
15282
15283
15284
15285
15286
15287
15288
15289
15290
15291
15292
15293
15294
15295
15296
15297
15298
15299
15300
15301
15302
15303
15304
15305
15306
15307
15308
15309
15310
15311
15312
15313
15314
15315
15316
15317
15318
15319
15320
15321
15322
15323
15324
15325
15326
15327
15328
15329
15330
15331
15332
15333
15334
15335
15336
15337
15338
15339
15340
15341
15342
15343
15344
15345
15346
15347
15348
15349
15350
15351
15352
15353
15354
15355
15356
15357
15358
15359
15360
15361
15362
15363
15364
15365
15366
15367
15368
15369
15370
15371
15372
15373
15374
15375
15376
15377
15378
15379
15380
15381
15382
15383
15384
15385
15386
15387
15388
15389
15390
15391
15392
15393
15394
15395
15396
15397
15398
15399
15400
15401
15402
15403
15404
15405
15406
15407
15408
15409
15410
15411
15412
15413
15414
15415
15416
15417
15418
15419
15420
15421
15422
15423
15424
15425
15426
15427
15428
15429
15430
15431
15432
15433
15434
15435
15436
15437
15438
15439
15440
15441
15442
15443
15444
15445
15446
15447
15448
15449
15450
15451
15452
15453
15454
15455
15456
15457
15458
15459
15460
15461
15462
15463
15464
15465
15466
15467
15468
15469
15470
15471
15472
15473
15474
15475
15476
15477
15478
15479
15480
15481
15482
15483
15484
15485
15486
15487
15488
15489
15490
15491
15492
15493
15494
15495
15496
15497
15498
15499
15500
15501
15502
15503
15504
15505
15506
15507
15508
15509
15510
15511
15512
15513
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
15530
15531
15532
15533
15534
15535
15536
15537
15538
15539
15540
15541
15542
15543
15544
15545
15546
15547
15548
15549
15550
15551
15552
15553
15554
15555
15556
15557
15558
15559
15560
15561
15562
15563
15564
15565
15566
15567
15568
15569
15570
15571
15572
15573
15574
15575
15576
15577
15578
15579
15580
15581
15582
15583
15584
15585
15586
15587
15588
15589
15590
15591
15592
15593
15594
15595
15596
15597
15598
15599
15600
15601
15602
15603
15604
15605
15606
15607
15608
15609
15610
15611
15612
15613
15614
15615
15616
15617
15618
15619
15620
15621
15622
15623
15624
15625
15626
15627
15628
15629
15630
15631
15632
15633
15634
15635
15636
15637
15638
15639
15640
15641
15642
15643
15644
15645
15646
15647
15648
15649
15650
15651
15652
15653
15654
15655
15656
15657
15658
15659
15660
15661
15662
15663
15664
15665
15666
15667
15668
15669
15670
15671
15672
15673
15674
15675
15676
15677
15678
15679
15680
15681
15682
15683
15684
15685
15686
15687
15688
15689
15690
15691
15692
15693
15694
15695
15696
15697
15698
15699
15700
15701
15702
15703
15704
15705
15706
15707
15708
15709
15710
15711
15712
15713
15714
15715
15716
15717
15718
15719
15720
15721
15722
15723
15724
15725
15726
15727
15728
15729
15730
15731
15732
15733
15734
15735
15736
15737
15738
15739
15740
15741
15742
15743
15744
15745
15746
15747
15748
15749
15750
15751
15752
15753
15754
15755
15756
15757
15758
15759
15760
15761
15762
15763
15764
15765
15766
15767
15768
15769
15770
15771
15772
15773
15774
15775
15776
15777
15778
15779
15780
15781
15782
15783
15784
15785
15786
15787
15788
15789
15790
15791
15792
15793
15794
15795
15796
15797
15798
15799
15800
15801
15802
15803
15804
15805
15806
15807
15808
15809
15810
15811
15812
15813
15814
15815
15816
15817
15818
15819
15820
15821
15822
15823
15824
15825
15826
15827
15828
15829
15830
15831
15832
15833
15834
15835
15836
15837
15838
15839
15840
15841
15842
15843
15844
15845
15846
15847
15848
15849
15850
15851
15852
15853
15854
15855
15856
15857
15858
15859
15860
15861
15862
15863
15864
15865
15866
15867
15868
15869
15870
15871
15872
15873
15874
15875
15876
15877
15878
15879
15880
15881
15882
15883
15884
15885
15886
15887
15888
15889
15890
15891
15892
15893
15894
15895
15896
15897
15898
15899
15900
15901
15902
15903
15904
15905
15906
15907
15908
15909
15910
15911
15912
15913
15914
15915
15916
15917
15918
15919
15920
15921
15922
15923
15924
15925
15926
15927
15928
15929
15930
15931
15932
15933
15934
15935
15936
15937
15938
15939
15940
15941
15942
15943
15944
15945
15946
15947
15948
15949
15950
15951
15952
15953
15954
15955
15956
15957
15958
15959
15960
15961
15962
15963
15964
15965
15966
15967
15968
15969
15970
15971
15972
15973
15974
15975
15976
15977
15978
15979
15980
15981
15982
15983
15984
15985
15986
15987
15988
15989
15990
15991
15992
15993
15994
15995
15996
15997
15998
15999
16000
16001
16002
16003
16004
16005
16006
16007
16008
16009
16010
16011
16012
16013
16014
16015
16016
16017
16018
16019
16020
16021
16022
16023
16024
16025
16026
16027
16028
16029
16030
16031
16032
16033
16034
16035
16036
16037
16038
16039
16040
16041
16042
16043
16044
16045
16046
16047
16048
16049
16050
16051
16052
16053
16054
16055
16056
16057
16058
16059
16060
16061
16062
16063
16064
16065
16066
16067
16068
16069
16070
16071
16072
16073
16074
16075
16076
16077
16078
16079
16080
16081
16082
16083
16084
16085
16086
16087
16088
16089
16090
16091
16092
16093
16094
16095
16096
16097
16098
16099
16100
16101
16102
16103
16104
16105
16106
16107
16108
16109
16110
16111
16112
16113
16114
16115
16116
16117
16118
16119
16120
16121
16122
16123
16124
16125
16126
16127
16128
16129
16130
16131
16132
16133
16134
16135
16136
16137
16138
16139
16140
16141
16142
16143
16144
16145
16146
16147
16148
16149
16150
16151
16152
16153
16154
16155
16156
16157
16158
16159
16160
16161
16162
16163
16164
16165
16166
16167
16168
16169
16170
16171
16172
16173
16174
16175
16176
16177
16178
16179
16180
16181
16182
16183
16184
16185
16186
16187
16188
16189
16190
16191
16192
16193
16194
16195
16196
16197
16198
16199
16200
16201
16202
16203
16204
16205
16206
16207
16208
16209
16210
16211
16212
16213
16214
16215
16216
16217
16218
16219
16220
16221
16222
16223
16224
16225
16226
16227
16228
16229
16230
16231
16232
16233
16234
16235
16236
16237
16238
16239
16240
16241
16242
16243
16244
16245
16246
16247
16248
16249
16250
16251
16252
16253
16254
16255
16256
16257
16258
16259
16260
16261
16262
16263
16264
16265
16266
16267
16268
16269
16270
16271
16272
16273
16274
16275
16276
16277
16278
16279
16280
16281
16282
16283
16284
16285
16286
16287
16288
16289
16290
16291
16292
16293
16294
16295
16296
16297
16298
16299
16300
16301
16302
16303
16304
16305
16306
16307
16308
16309
16310
16311
16312
16313
16314
16315
16316
16317
16318
16319
16320
16321
16322
16323
16324
16325
16326
16327
16328
16329
16330
16331
16332
16333
16334
16335
16336
16337
16338
16339
16340
16341
16342
16343
16344
16345
16346
16347
16348
16349
16350
16351
16352
16353
16354
16355
16356
16357
16358
16359
16360
16361
16362
16363
16364
16365
16366
16367
16368
16369
16370
16371
16372
16373
16374
16375
16376
16377
16378
16379
16380
16381
16382
16383
16384
16385
16386
16387
16388
16389
16390
16391
16392
16393
16394
16395
16396
16397
16398
16399
16400
16401
16402
16403
16404
16405
16406
16407
16408
16409
16410
16411
16412
16413
16414
16415
16416
16417
16418
16419
16420
16421
16422
16423
16424
16425
16426
16427
16428
16429
16430
16431
16432
16433
16434
16435
16436
16437
16438
16439
16440
16441
16442
16443
16444
16445
16446
16447
16448
16449
16450
16451
16452
16453
16454
16455
16456
16457
16458
16459
16460
16461
16462
16463
16464
16465
16466
16467
16468
16469
16470
16471
16472
16473
16474
16475
16476
16477
16478
16479
16480
16481
16482
16483
16484
16485
16486
16487
16488
16489
16490
16491
16492
16493
16494
16495
16496
16497
16498
16499
16500
16501
16502
16503
16504
16505
16506
16507
16508
16509
16510
16511
16512
16513
16514
16515
16516
16517
16518
16519
16520
16521
16522
16523
16524
16525
16526
16527
16528
16529
16530
16531
16532
16533
16534
16535
16536
16537
16538
16539
16540
16541
16542
16543
16544
16545
16546
16547
16548
16549
16550
16551
16552
16553
16554
16555
16556
16557
16558
16559
16560
16561
16562
16563
16564
16565
16566
16567
16568
16569
16570
16571
16572
16573
16574
16575
16576
16577
16578
16579
16580
16581
16582
16583
16584
16585
16586
16587
16588
16589
16590
16591
16592
16593
16594
16595
16596
16597
16598
16599
16600
16601
16602
16603
16604
16605
16606
16607
16608
16609
16610
16611
16612
16613
16614
16615
16616
16617
16618
16619
16620
16621
16622
16623
16624
16625
16626
16627
16628
16629
16630
16631
16632
16633
16634
16635
16636
16637
16638
16639
16640
16641
16642
16643
16644
16645
16646
16647
16648
16649
16650
16651
16652
16653
16654
16655
16656
16657
16658
16659
16660
16661
16662
16663
16664
16665
16666
16667
16668
16669
16670
16671
16672
16673
16674
16675
16676
16677
16678
16679
16680
16681
16682
16683
16684
16685
16686
16687
16688
16689
16690
16691
16692
16693
16694
16695
16696
16697
16698
16699
16700
16701
16702
16703
16704
16705
16706
16707
16708
16709
16710
16711
16712
16713
16714
16715
16716
16717
16718
16719
16720
16721
16722
16723
16724
16725
16726
16727
16728
16729
16730
16731
16732
16733
16734
16735
16736
16737
16738
16739
16740
16741
16742
16743
16744
16745
16746
16747
16748
16749
16750
16751
16752
16753
16754
16755
16756
16757
16758
16759
16760
16761
16762
16763
16764
16765
16766
16767
16768
16769
16770
16771
16772
16773
16774
16775
16776
16777
16778
16779
16780
16781
16782
16783
16784
16785
16786
16787
16788
16789
16790
16791
16792
16793
16794
16795
16796
16797
16798
16799
16800
16801
16802
16803
16804
16805
16806
16807
16808
16809
16810
16811
16812
16813
16814
16815
16816
16817
16818
16819
16820
16821
16822
16823
16824
16825
16826
16827
16828
16829
16830
16831
16832
16833
16834
16835
16836
16837
16838
16839
16840
16841
16842
16843
16844
16845
16846
16847
16848
16849
16850
16851
16852
16853
16854
16855
16856
16857
16858
16859
16860
16861
16862
16863
16864
16865
16866
16867
16868
16869
16870
16871
16872
16873
16874
16875
16876
16877
16878
16879
16880
16881
16882
16883
16884
16885
16886
16887
16888
16889
16890
16891
16892
16893
16894
16895
16896
16897
16898
16899
16900
16901
16902
16903
16904
16905
16906
16907
16908
16909
16910
16911
16912
16913
16914
16915
16916
16917
16918
16919
16920
16921
16922
16923
16924
16925
16926
16927
16928
16929
16930
16931
16932
16933
16934
16935
16936
16937
16938
16939
16940
16941
16942
16943
16944
16945
16946
16947
16948
16949
16950
16951
16952
16953
16954
16955
16956
16957
16958
16959
16960
16961
16962
16963
16964
16965
16966
16967
16968
16969
16970
16971
16972
16973
16974
16975
16976
16977
16978
16979
16980
16981
16982
16983
16984
16985
16986
16987
16988
16989
16990
16991
16992
16993
16994
16995
16996
16997
16998
16999
17000
|
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//
//*****************************************************************************
// File: debugger.cpp
//
//
// Debugger runtime controller routines.
//
//*****************************************************************************
#include "stdafx.h"
#include "debugdebugger.h"
#include "ipcmanagerinterface.h"
#include "../inc/common.h"
#include "perflog.h"
#include "eeconfig.h" // This is here even for retail & free builds...
#include "../../dlls/mscorrc/resource.h"
#ifdef FEATURE_REMOTING
#include "remoting.h"
#endif
#include "context.h"
#include "vars.hpp"
#include <limits.h>
#include "ilformatter.h"
#include "typeparse.h"
#include "debuginfostore.h"
#include "generics.h"
#include "../../vm/security.h"
#include "../../vm/methoditer.h"
#include "../../vm/encee.h"
#include "../../vm/dwreport.h"
#include "../../vm/eepolicy.h"
#include "../../vm/excep.h"
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
#include "dbgtransportsession.h"
#include "dbgtransportproxy.h"
#include "dbgsecureconnection.h"
#endif // FEATURE_DBGIPC_TRANSPORT_VM
#ifdef TEST_DATA_CONSISTENCY
#include "datatest.h"
#endif // TEST_DATA_CONSISTENCY
#if defined(FEATURE_CORECLR)
#include "dbgenginemetrics.h"
#endif // FEATURE_CORECLR
#include "../../vm/rejit.h"
#include "threadsuspend.h"
class CCLRSecurityAttributeManager;
extern CCLRSecurityAttributeManager s_CLRSecurityAttributeManager;
#ifdef DEBUGGING_SUPPORTED
#ifdef _DEBUG
// Reg key. We can set this and then any debugger-lazy-init code will assert.
// This helps track down places where we're caching in debugger stuff in a
// non-debugger scenario.
bool g_DbgShouldntUseDebugger = false;
#endif
/* ------------------------------------------------------------------------ *
* Global variables
* ------------------------------------------------------------------------ */
GPTR_IMPL(Debugger, g_pDebugger);
GPTR_IMPL(EEDebugInterface, g_pEEInterface);
SVAL_IMPL_INIT(BOOL, Debugger, s_fCanChangeNgenFlags, TRUE);
bool g_EnableSIS = false;
#ifndef DACCESS_COMPILE
DebuggerRCThread *g_pRCThread = NULL;
#ifndef _PREFAST_
// Do some compile time checking on the events in DbgIpcEventTypes.h
// No one ever calls this. But the compiler should still compile it,
// and that should be sufficient.
void DoCompileTimeCheckOnDbgIpcEventTypes()
{
_ASSERTE(!"Don't call this function. It just does compile time checking\n");
// We use the C_ASSERT macro here to get a compile-time assert.
// Make sure we don't have any duplicate numbers.
// The switch statements in the main loops won't always catch this
// since we may not switch on all events.
// store Type-0 in const local vars, so we can use them for bounds checking
// Create local vars with the val from Type1 & Type2. If there are any
// collisions, then the variables' names will collide at compile time.
#define IPC_EVENT_TYPE0(type, val) const int e_##type = val;
#define IPC_EVENT_TYPE1(type, val) int T_##val; T_##val = 0;
#define IPC_EVENT_TYPE2(type, val) int T_##val; T_##val = 0;
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
// Ensure that all identifiers are unique and are matched with
// integer values.
#define IPC_EVENT_TYPE0(type, val) int T2_##type; T2_##type = val;
#define IPC_EVENT_TYPE1(type, val) int T2_##type; T2_##type = val;
#define IPC_EVENT_TYPE2(type, val) int T2_##type; T2_##type = val;
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
// Make sure all values are subset of the bits specified by DB_IPCE_TYPE_MASK
#define IPC_EVENT_TYPE0(type, val)
#define IPC_EVENT_TYPE1(type, val) C_ASSERT((val & e_DB_IPCE_TYPE_MASK) == val);
#define IPC_EVENT_TYPE2(type, val) C_ASSERT((val & e_DB_IPCE_TYPE_MASK) == val);
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
// Make sure that no value is DB_IPCE_INVALID_EVENT
#define IPC_EVENT_TYPE0(type, val)
#define IPC_EVENT_TYPE1(type, val) C_ASSERT(val != e_DB_IPCE_INVALID_EVENT);
#define IPC_EVENT_TYPE2(type, val) C_ASSERT(val != e_DB_IPCE_INVALID_EVENT);
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
// Make sure first-last values are well structured.
static_assert_no_msg(e_DB_IPCE_RUNTIME_FIRST < e_DB_IPCE_RUNTIME_LAST);
static_assert_no_msg(e_DB_IPCE_DEBUGGER_FIRST < e_DB_IPCE_DEBUGGER_LAST);
// Make sure that event ranges don't overlap.
// This check is simplified because L->R events come before R<-L
static_assert_no_msg(e_DB_IPCE_RUNTIME_LAST < e_DB_IPCE_DEBUGGER_FIRST);
// Make sure values are in the proper ranges
// Type1 should be in the Runtime range, Type2 in the Debugger range.
#define IPC_EVENT_TYPE0(type, val)
#define IPC_EVENT_TYPE1(type, val) C_ASSERT((e_DB_IPCE_RUNTIME_FIRST <= val) && (val < e_DB_IPCE_RUNTIME_LAST));
#define IPC_EVENT_TYPE2(type, val) C_ASSERT((e_DB_IPCE_DEBUGGER_FIRST <= val) && (val < e_DB_IPCE_DEBUGGER_LAST));
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
// Make sure that events are in increasing order
// It's ok if the events skip numbers.
// This is a more specific check than the range check above.
/* Expands to look like this:
const bool f = (
first <=
10) && (10 <
11) && (11 <
12) && (12 <
last)
static_assert_no_msg(f);
*/
const bool f1 = (
(e_DB_IPCE_RUNTIME_FIRST <=
#define IPC_EVENT_TYPE0(type, val)
#define IPC_EVENT_TYPE1(type, val) val) && (val <
#define IPC_EVENT_TYPE2(type, val)
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
e_DB_IPCE_RUNTIME_LAST)
);
static_assert_no_msg(f1);
const bool f2 = (
(e_DB_IPCE_DEBUGGER_FIRST <=
#define IPC_EVENT_TYPE0(type, val)
#define IPC_EVENT_TYPE1(type, val)
#define IPC_EVENT_TYPE2(type, val) val) && (val <
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
e_DB_IPCE_DEBUGGER_LAST)
);
static_assert_no_msg(f2);
} // end checks
#endif // _PREFAST_
//-----------------------------------------------------------------------------
// Ctor for AtSafePlaceHolder
AtSafePlaceHolder::AtSafePlaceHolder(Thread * pThread)
{
_ASSERTE(pThread != NULL);
if (!g_pDebugger->IsThreadAtSafePlace(pThread))
{
m_pThreadAtUnsafePlace = pThread;
g_pDebugger->IncThreadsAtUnsafePlaces();
}
else
{
m_pThreadAtUnsafePlace = NULL;
}
}
//-----------------------------------------------------------------------------
// Dtor for AtSafePlaceHolder
AtSafePlaceHolder::~AtSafePlaceHolder()
{
Clear();
}
//-----------------------------------------------------------------------------
// Returns true if this adjusted the unsafe counter
bool AtSafePlaceHolder::IsAtUnsafePlace()
{
return m_pThreadAtUnsafePlace != NULL;
}
//-----------------------------------------------------------------------------
// Clear the holder.
// Notes:
// This can be called multiple times.
// Calling this makes the dtor a nop.
void AtSafePlaceHolder::Clear()
{
if (m_pThreadAtUnsafePlace != NULL)
{
// The thread is still at an unsafe place.
// We're clearing the flag to avoid the Dtor() calling DecThreads again.
m_pThreadAtUnsafePlace = NULL;
g_pDebugger->DecThreadsAtUnsafePlaces();
}
}
//-----------------------------------------------------------------------------
// Is the guard page missing on this thread?
// Should only be called for managed threads handling a managed exception.
// If we're handling a stack overflow (ie, missing guard page), then another
// stack overflow will instantly terminate the process. In that case, do stack
// intensive stuff on the helper thread (which has lots of stack space). Only
// problem is that if the faulting thread has a lock, the helper thread may
// get stuck.
// Serves as a hint whether we want to do a favor on the
// faulting thread (preferred) or the helper thread (if low stack).
// See whidbey issue 127436.
//-----------------------------------------------------------------------------
bool IsGuardPageGone()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
Thread * pThread = g_pEEInterface->GetThread();
// We're not going to be called for a unmanaged exception.
// Should always have a managed thread, but just in case something really
// crazy happens, it's not worth an AV. (since this is just being used as a hint)
if (pThread == NULL)
{
return false;
}
// Don't use pThread->IsGuardPageGone(), it's not accurate here.
bool fGuardPageGone = (pThread->DetermineIfGuardPagePresent() == FALSE);
LOG((LF_CORDB, LL_INFO1000000, "D::IsGuardPageGone=%d\n", fGuardPageGone));
return fGuardPageGone;
}
// This is called from AppDomainEnumerationIPCBlock::Lock and Unlock
void BeginThreadAffinityHelper()
{
WRAPPER_NO_CONTRACT;
Thread::BeginThreadAffinity();
}
void EndThreadAffinityHelper()
{
WRAPPER_NO_CONTRACT;
Thread::EndThreadAffinity();
}
//-----------------------------------------------------------------------------
// LSPTR_XYZ is a type-safe wrapper around an opaque reference type XYZ in the left-side.
// But TypeHandles are value-types that can't be directly converted into a pointer.
// Thus converting between LSPTR_XYZ and TypeHandles requires some extra glue.
// The following conversions are valid:
// LSPTR_XYZ <--> XYZ* (via Set/UnWrap methods)
// TypeHandle <--> void* (via AsPtr() and FromPtr()).
// so we can't directly convert between LSPTR_TYPEHANDLE and TypeHandle.
// We must do: TypeHandle <--> void* <--> XYZ <--> LSPTR_XYZ
// So LSPTR_TYPEHANDLE is actually for TypeHandleDummyPtr, and then we unsafe cast
// that to a void* to use w/ AsPtr() and FromPtr() to convert to TypeHandles.
// @todo- it would be nice to have these happen automatically w/ Set & UnWrap.
//-----------------------------------------------------------------------------
// helper class to do conversion above.
class TypeHandleDummyPtr
{
private:
TypeHandleDummyPtr() { }; // should never actually create this.
void * data;
};
// Convert: VMPTR_TYPEHANDLE --> TypeHandle
TypeHandle GetTypeHandle(VMPTR_TypeHandle ptr)
{
return TypeHandle::FromPtr(ptr.GetRawPtr());
}
// Convert: TypeHandle --> LSPTR_TYPEHANDLE
VMPTR_TypeHandle WrapTypeHandle(TypeHandle th)
{
return VMPTR_TypeHandle::MakePtr(reinterpret_cast<TypeHandle *> (th.AsPtr()));
}
extern void WaitForEndOfShutdown();
// Get the Canary structure which can sniff if the helper thread is safe to run.
HelperCanary * Debugger::GetCanary()
{
return g_pRCThread->GetCanary();
}
// IMPORTANT!!!!!
// Do not call Lock and Unlock directly. Because you might not unlock
// if exception takes place. Use DebuggerLockHolder instead!!!
// Only AcquireDebuggerLock can call directly.
//
void Debugger::DoNotCallDirectlyPrivateLock(void)
{
WRAPPER_NO_CONTRACT;
LOG((LF_CORDB,LL_INFO10000, "D::Lock aquire attempt by 0x%x\n",
GetCurrentThreadId()));
// Debugger lock is larger than both Controller & debugger-data locks.
// So we should never try to take the D lock if we hold either of the others.
// Lock becomes no-op in late shutdown.
if (g_fProcessDetach)
{
return;
}
//
// If the debugger has been disabled by the runtime, this means that it should block
// all threads that are trying to travel thru the debugger. We do this by blocking
// threads as they try and take the debugger lock.
//
if (m_fDisabled)
{
__SwitchToThread(INFINITE, CALLER_LIMITS_SPINNING);
_ASSERTE (!"Can not reach here");
}
m_mutex.Enter();
//
// If we were blocked on the lock and the debugging facilities got disabled
// while we were waiting, release the lock and park this thread.
//
if (m_fDisabled)
{
m_mutex.Leave();
__SwitchToThread(INFINITE, CALLER_LIMITS_SPINNING);
_ASSERTE (!"Can not reach here");
}
//
// Now check if we are in a shutdown case...
//
Thread * pThread;
bool fIsCooperative;
BEGIN_GETTHREAD_ALLOWED;
pThread = g_pEEInterface->GetThread();
fIsCooperative = (pThread != NULL) && (pThread->PreemptiveGCDisabled());
END_GETTHREAD_ALLOWED;
if (m_fShutdownMode && !fIsCooperative)
{
// The big fear is that some other random thread will take the debugger-lock and then block on something else,
// and thus prevent the helper/finalizer threads from taking the debugger-lock in shutdown scenarios.
//
// If we're in shutdown mode, then some locks (like the Thread-Store-Lock) get special semantics.
// Only helper / finalizer / shutdown threads can actually take these locks.
// Other threads that try to take them will just get parked and block forever.
// This is ok b/c the only threads that need to run at this point are the Finalizer and Helper threads.
//
// We need to be in preemptive to block for shutdown, so we don't do this block in Coop mode.
// Fortunately, it's safe to take this lock in coop mode because we know the thread can't block
// on anything interesting because we're in a GC-forbid region (see crst flags).
m_mutex.ReleaseAndBlockForShutdownIfNotSpecialThread();
}
#ifdef _DEBUG
_ASSERTE(m_mutexCount >= 0);
if (m_mutexCount>0)
{
if (pThread)
{
// mamaged thread
_ASSERTE(m_mutexOwner == GetThreadIdHelper(pThread));
}
else
{
// unmanaged thread
_ASSERTE(m_mutexOwner == GetCurrentThreadId());
}
}
m_mutexCount++;
if (pThread)
{
m_mutexOwner = GetThreadIdHelper(pThread);
}
else
{
// unmanaged thread
m_mutexOwner = GetCurrentThreadId();
}
if (m_mutexCount == 1)
{
LOG((LF_CORDB,LL_INFO10000, "D::Lock aquired by 0x%x\n", m_mutexOwner));
}
#endif
}
// See comment above.
// Only ReleaseDebuggerLock can call directly.
void Debugger::DoNotCallDirectlyPrivateUnlock(void)
{
WRAPPER_NO_CONTRACT;
// Controller lock is "smaller" than debugger lock.
if (!g_fProcessDetach)
{
#ifdef _DEBUG
if (m_mutexCount == 1)
LOG((LF_CORDB,LL_INFO10000, "D::Unlock released by 0x%x\n",
m_mutexOwner));
if(0 == --m_mutexCount)
m_mutexOwner = 0;
_ASSERTE( m_mutexCount >= 0);
#endif
m_mutex.Leave();
//
// If the debugger has been disabled by the runtime, this means that it should block
// all threads that are trying to travel thru the debugger. We do this by blocking
// threads also as they leave the debugger lock.
//
if (m_fDisabled)
{
__SwitchToThread(INFINITE, CALLER_LIMITS_SPINNING);
_ASSERTE (!"Can not reach here");
}
}
}
#ifdef TEST_DATA_CONSISTENCY
// ---------------------------------------------------------------------------------
// Implementations for DataTest member functions
// ---------------------------------------------------------------------------------
// Send an event to the RS to signal that it should test to determine if a crst is held.
// This is for testing purposes only.
// Arguments:
// input: pCrst - the lock to test
// fOkToTake - true iff the LS does NOT currently hold the lock
// output: none
// Notes: The RS will throw if the lock is held. The code that tests the lock will catch the
// exception and assert if throwing was not the correct thing to do (determined via the
// boolean). See the case for DB_IPCE_TEST_CRST in code:CordbProcess::RawDispatchEvent.
//
void DataTest::SendDbgCrstEvent(Crst * pCrst, bool fOkToTake)
{
DebuggerIPCEvent * pLockEvent = g_pDebugger->m_pRCThread->GetIPCEventSendBuffer();
g_pDebugger->InitIPCEvent(pLockEvent, DB_IPCE_TEST_CRST);
pLockEvent->TestCrstData.vmCrst.SetRawPtr(pCrst);
pLockEvent->TestCrstData.fOkToTake = fOkToTake;
g_pDebugger->SendRawEvent(pLockEvent);
} // DataTest::SendDbgCrstEvent
// Send an event to the RS to signal that it should test to determine if a SimpleRWLock is held.
// This is for testing purposes only.
// Arguments:
// input: pRWLock - the lock to test
// fOkToTake - true iff the LS does NOT currently hold the lock
// output: none
// Note: The RS will throw if the lock is held. The code that tests the lock will catch the
// exception and assert if throwing was not the correct thing to do (determined via the
// boolean). See the case for DB_IPCE_TEST_RWLOCK in code:CordbProcess::RawDispatchEvent.
//
void DataTest::SendDbgRWLockEvent(SimpleRWLock * pRWLock, bool okToTake)
{
DebuggerIPCEvent * pLockEvent = g_pDebugger->m_pRCThread->GetIPCEventSendBuffer();
g_pDebugger->InitIPCEvent(pLockEvent, DB_IPCE_TEST_RWLOCK);
pLockEvent->TestRWLockData.vmRWLock.SetRawPtr(pRWLock);
pLockEvent->TestRWLockData.fOkToTake = okToTake;
g_pDebugger->SendRawEvent(pLockEvent);
} // DataTest::SendDbgRWLockEvent
// Takes a series of locks in various ways and signals the RS to test the locks at interesting
// points to ensure we reliably detect when the LS holds a lock. If in the course of inspection, the
// DAC needs to execute a code path where the LS holds a lock, we assume that the locked data is in
// an inconsistent state. In this situation, we don't want to report information about this data, so
// we throw an exception.
// This is for testing purposes only.
//
// Arguments: none
// Return Value: none
// Notes: See code:CordbProcess::RawDispatchEvent for the RS part of this test and code:Debugger::Startup
// for the LS invocation of the test.
// The environment variable TestDataConsistency must be set to 1 to make this test run.
void DataTest::TestDataSafety()
{
const bool okToTake = true;
SendDbgCrstEvent(&m_crst1, okToTake);
{
CrstHolder ch1(&m_crst1);
SendDbgCrstEvent(&m_crst1, !okToTake);
{
CrstHolder ch2(&m_crst2);
SendDbgCrstEvent(&m_crst2, !okToTake);
SendDbgCrstEvent(&m_crst1, !okToTake);
}
SendDbgCrstEvent(&m_crst2, okToTake);
SendDbgCrstEvent(&m_crst1, !okToTake);
}
SendDbgCrstEvent(&m_crst1, okToTake);
{
SendDbgRWLockEvent(&m_rwLock, okToTake);
SimpleReadLockHolder readLock(&m_rwLock);
SendDbgRWLockEvent(&m_rwLock, okToTake);
}
SendDbgRWLockEvent(&m_rwLock, okToTake);
{
SimpleWriteLockHolder readLock(&m_rwLock);
SendDbgRWLockEvent(&m_rwLock, !okToTake);
}
} // DataTest::TestDataSafety
#endif // TEST_DATA_CONSISTENCY
#if _DEBUG
static DebugEventCounter g_debugEventCounter;
static int g_iDbgRuntimeCounter[DBG_RUNTIME_MAX];
static int g_iDbgDebuggerCounter[DBG_DEBUGGER_MAX];
void DoAssertOnType(DebuggerIPCEventType event, int count)
{
WRAPPER_NO_CONTRACT;
// check to see if we need fire the assertion or not.
if ((event & 0x0300) == 0x0100)
{
// use the Runtime array
if (g_iDbgRuntimeCounter[event & 0x00ff] == count)
{
char tmpStr[256];
sprintf(tmpStr, "%s == %d, break now!",
IPCENames::GetName(event), count);
// fire the assertion
DbgAssertDialog(__FILE__, __LINE__, tmpStr);
}
}
// check to see if we need fire the assertion or not.
else if ((event & 0x0300) == 0x0200)
{
// use the Runtime array
if (g_iDbgDebuggerCounter[event & 0x00ff] == count)
{
char tmpStr[256];
sprintf(tmpStr, "%s == %d, break now!",
IPCENames::GetName(event), count);
// fire the assertion
DbgAssertDialog(__FILE__, __LINE__, tmpStr);
}
}
}
void DbgLogHelper(DebuggerIPCEventType event)
{
WRAPPER_NO_CONTRACT;
switch (event)
{
// we don't need to handle event type 0
#define IPC_EVENT_TYPE0(type, val)
#define IPC_EVENT_TYPE1(type, val) case type: {\
g_debugEventCounter.m_iDebugCount_##type++; \
DoAssertOnType(type, g_debugEventCounter.m_iDebugCount_##type); \
break; \
}
#define IPC_EVENT_TYPE2(type, val) case type: { \
g_debugEventCounter.m_iDebugCount_##type++; \
DoAssertOnType(type, g_debugEventCounter.m_iDebugCount_##type); \
break; \
}
#include "dbgipceventtypes.h"
#undef IPC_EVENT_TYPE2
#undef IPC_EVENT_TYPE1
#undef IPC_EVENT_TYPE0
default:
break;
}
}
#endif // _DEBUG
/* ------------------------------------------------------------------------ *
* DLL export routine
* ------------------------------------------------------------------------ */
Debugger *CreateDebugger(void)
{
Debugger *pDebugger = NULL;
EX_TRY
{
pDebugger = new (nothrow) Debugger();
}
EX_CATCH
{
if (pDebugger != NULL)
{
delete pDebugger;
pDebugger = NULL;
}
}
EX_END_CATCH(RethrowTerminalExceptions);
return pDebugger;
}
//
// CorDBGetInterface is exported to the Runtime so that it can call
// the Runtime Controller.
//
extern "C"{
HRESULT __cdecl CorDBGetInterface(DebugInterface** rcInterface)
{
CONTRACT(HRESULT)
{
NOTHROW; // use HRESULTS instead
GC_NOTRIGGER;
POSTCONDITION(FAILED(RETVAL) || (rcInterface == NULL) || (*rcInterface != NULL));
}
CONTRACT_END;
HRESULT hr = S_OK;
if (rcInterface != NULL)
{
if (g_pDebugger == NULL)
{
LOG((LF_CORDB, LL_INFO10,
"CorDBGetInterface: initializing debugger.\n"));
g_pDebugger = CreateDebugger();
TRACE_ALLOC(g_pDebugger);
if (g_pDebugger == NULL)
hr = E_OUTOFMEMORY;
}
*rcInterface = g_pDebugger;
}
RETURN hr;
}
}
//-----------------------------------------------------------------------------
// Send a pre-init IPC event and block.
// We assume the IPC event has already been initialized. There's nothing special
// here; it just used the standard formula for sending an IPC event to the RS.
// This should match up w/ the description in SENDIPCEVENT_BEGIN.
//-----------------------------------------------------------------------------
void Debugger::SendSimpleIPCEventAndBlock()
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// BEGIN will acquire the lock (END will release it). While blocking, the
// debugger may have detached though, so we need to check for that.
_ASSERTE(ThreadHoldsLock());
if (CORDebuggerAttached())
{
m_pRCThread->SendIPCEvent();
// Stop all Runtime threads
this->TrapAllRuntimeThreads();
}
}
//-----------------------------------------------------------------------------
// Get context from a thread in managed code.
// See header for exact semantics.
//-----------------------------------------------------------------------------
CONTEXT * GetManagedStoppedCtx(Thread * pThread)
{
WRAPPER_NO_CONTRACT;
_ASSERTE(pThread != NULL);
// We may be stopped or live.
// If we're stopped at an interop-hijack, we'll have a filter context,
// but we'd better not be redirected for a managed-suspension hijack.
if (pThread->GetInteropDebuggingHijacked())
{
_ASSERTE(!ISREDIRECTEDTHREAD(pThread));
return NULL;
}
// Check if we have a filter ctx. This should only be for managed-code.
// We're stopped at some exception (likely an int3 or single-step).
// Can't have both filter ctx + redirected ctx.
CONTEXT *pCtx = g_pEEInterface->GetThreadFilterContext(pThread);
if (pCtx != NULL)
{
_ASSERTE(!ISREDIRECTEDTHREAD(pThread));
return pCtx;
}
if (ISREDIRECTEDTHREAD(pThread))
{
pCtx = GETREDIRECTEDCONTEXT(pThread);
_ASSERTE(pCtx != NULL);
return pCtx;
}
// Not stopped somewhere in managed code.
return NULL;
}
//-----------------------------------------------------------------------------
// See header for exact semantics.
// Never NULL. (Caller guarantees this is active.)
//-----------------------------------------------------------------------------
CONTEXT * GetManagedLiveCtx(Thread * pThread)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(pThread != NULL);
// We should never be on the helper thread, we should only be inspecting our own thread.
// We're in some Controller's Filter after hitting an exception.
// We're not stopped.
//_ASSERTE(!g_pDebugger->IsStopped()); <-- @todo - this fires, need to find out why.
_ASSERTE(GetThread() == pThread);
CONTEXT *pCtx = g_pEEInterface->GetThreadFilterContext(pThread);
// Note that we may be in a M2U hijack. So we can't assert !pThread->GetInteropDebuggingHijacked()
_ASSERTE(!ISREDIRECTEDTHREAD(pThread));
_ASSERTE(pCtx);
return pCtx;
}
// Attempt to validate a GC handle.
HRESULT ValidateGCHandle(OBJECTHANDLE oh)
{
// The only real way to do this is to Enumerate all GC handles in the handle table.
// That's too expensive. So we'll use a similar workaround that we use in ValidateObject.
// This will err on the side off returning True for invalid handles.
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
HRESULT hr = S_OK;
EX_TRY
{
// Use AVInRuntimeImplOkHolder.
AVInRuntimeImplOkayHolder AVOkay;
// This may throw if the Object Handle is invalid.
Object * objPtr = *((Object**) oh);
// NULL is certinally valid...
if (objPtr != NULL)
{
if (!objPtr->ValidateObjectWithPossibleAV())
{
LOG((LF_CORDB, LL_INFO10000, "GAV: object methodtable-class invariant doesn't hold.\n"));
hr = E_INVALIDARG;
goto LExit;
}
}
LExit: ;
}
EX_CATCH
{
LOG((LF_CORDB, LL_INFO10000, "GAV: exception indicated ref is bad.\n"));
hr = E_INVALIDARG;
}
EX_END_CATCH(SwallowAllExceptions);
return hr;
}
// Validate an object. Returns E_INVALIDARG or S_OK.
HRESULT ValidateObject(Object *objPtr)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
HRESULT hr = S_OK;
EX_TRY
{
// Use AVInRuntimeImplOkHolder.
AVInRuntimeImplOkayHolder AVOkay;
// NULL is certinally valid...
if (objPtr != NULL)
{
if (!objPtr->ValidateObjectWithPossibleAV())
{
LOG((LF_CORDB, LL_INFO10000, "GAV: object methodtable-class invariant doesn't hold.\n"));
hr = E_INVALIDARG;
goto LExit;
}
}
LExit: ;
}
EX_CATCH
{
LOG((LF_CORDB, LL_INFO10000, "GAV: exception indicated ref is bad.\n"));
hr = E_INVALIDARG;
}
EX_END_CATCH(SwallowAllExceptions);
return hr;
} // ValidateObject
/* ------------------------------------------------------------------------ *
* Debugger routines
* ------------------------------------------------------------------------ */
//
// a Debugger object represents the global state of the debugger program.
//
//
// Constructor & Destructor
//
/******************************************************************************
*
******************************************************************************/
Debugger::Debugger()
:
m_fLeftSideInitialized(FALSE),
#ifdef _DEBUG
m_mutexCount(0),
#endif //_DEBUG
m_pRCThread(NULL),
m_trappingRuntimeThreads(FALSE),
m_stopped(FALSE),
m_unrecoverableError(FALSE),
m_ignoreThreadDetach(FALSE),
m_pMethodInfos(NULL),
m_mutex(CrstDebuggerMutex, (CrstFlags)(CRST_UNSAFE_ANYMODE | CRST_REENTRANCY | CRST_DEBUGGER_THREAD)),
#ifdef _DEBUG
m_mutexOwner(0),
m_tidLockedForEventSending(0),
#endif //_DEBUG
m_threadsAtUnsafePlaces(0),
m_jitAttachInProgress(FALSE),
m_attachingForManagedEvent(FALSE),
m_launchingDebugger(FALSE),
m_userRequestedDebuggerLaunch(FALSE),
m_LoggingEnabled(TRUE),
m_pAppDomainCB(NULL),
m_dClassLoadCallbackCount(0),
m_pModules(NULL),
m_RSRequestedSync(FALSE),
m_sendExceptionsOutsideOfJMC(TRUE),
m_pIDbgThreadControl(NULL),
m_forceNonInterceptable(FALSE),
m_pLazyData(NULL),
m_defines(_defines)
{
CONTRACTL
{
SO_INTOLERANT;
WRAPPER(THROWS);
WRAPPER(GC_TRIGGERS);
CONSTRUCTOR_CHECK;
}
CONTRACTL_END;
m_fShutdownMode = false;
m_fDisabled = false;
#ifdef _DEBUG
InitDebugEventCounting();
#endif
m_processId = GetCurrentProcessId();
// Initialize these in ctor because we free them in dtor.
// And we can't set them to some safe uninited value (like NULL).
//------------------------------------------------------------------------------
// Metadata data structure version numbers
//
// 1 - initial state of the layouts ( .Net 4.5.2 )
//
// as data structure layouts change, add a new version number
// and comment the changes
m_mdDataStructureVersion = 1;
}
/******************************************************************************
*
******************************************************************************/
Debugger::~Debugger()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
DESTRUCTOR_CHECK;
SO_INTOLERANT;
}
CONTRACTL_END;
// We explicitly leak the debugger object on shutdown. See Debugger::StopDebugger for details.
_ASSERTE(!"Debugger dtor should not be called.");
}
#ifdef FEATURE_HIJACK
typedef void (*PFN_HIJACK_FUNCTION) (void);
// Given the start address and the end address of a function, return a MemoryRange for the function.
inline MemoryRange GetMemoryRangeForFunction(PFN_HIJACK_FUNCTION pfnStart, PFN_HIJACK_FUNCTION pfnEnd)
{
PCODE pfnStartAddress = (PCODE)GetEEFuncEntryPoint(pfnStart);
PCODE pfnEndAddress = (PCODE)GetEEFuncEntryPoint(pfnEnd);
return MemoryRange(dac_cast<PTR_VOID>(pfnStartAddress), (pfnEndAddress - pfnStartAddress));
}
// static
MemoryRange Debugger::s_hijackFunction[kMaxHijackFunctions] =
{GetMemoryRangeForFunction(ExceptionHijack, ExceptionHijackEnd),
GetMemoryRangeForFunction(RedirectedHandledJITCaseForGCThreadControl_Stub,
RedirectedHandledJITCaseForGCThreadControl_StubEnd),
GetMemoryRangeForFunction(RedirectedHandledJITCaseForDbgThreadControl_Stub,
RedirectedHandledJITCaseForDbgThreadControl_StubEnd),
GetMemoryRangeForFunction(RedirectedHandledJITCaseForUserSuspend_Stub,
RedirectedHandledJITCaseForUserSuspend_StubEnd),
GetMemoryRangeForFunction(RedirectedHandledJITCaseForYieldTask_Stub,
RedirectedHandledJITCaseForYieldTask_StubEnd)};
#endif // FEATURE_HIJACK
// Save the necessary information for the debugger to recognize an IP in one of the thread redirection
// functions.
void Debugger::InitializeHijackFunctionAddress()
{
#ifdef FEATURE_HIJACK
// Advertise hijack address for the DD Hijack primitive
m_rgHijackFunction = Debugger::s_hijackFunction;
#endif // FEATURE_HIJACK
}
// For debug-only builds, we'll have a debugging feature to count
// the number of ipc events and break on a specific number.
// Initialize the stuff to do that.
void Debugger::InitDebugEventCounting()
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
#ifdef _DEBUG
// initialize the debug event counter structure to zero
memset(&g_debugEventCounter, 0, sizeof(DebugEventCounter));
memset(&g_iDbgRuntimeCounter, 0, DBG_RUNTIME_MAX*sizeof(int));
memset(&g_iDbgDebuggerCounter, 0, DBG_DEBUGGER_MAX*sizeof(int));
// retrieve the possible counter for break point
LPWSTR wstrValue = NULL;
// The string value is of the following format
// <Event Name>=Count;<Event Name>=Count;....;
// The string must end with ;
if ((wstrValue = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DebuggerBreakPoint)) != NULL)
{
LPSTR strValue;
int cbReq;
cbReq = WszWideCharToMultiByte(CP_UTF8, 0, wstrValue,-1, 0,0, 0,0);
strValue = new (nothrow) char[cbReq+1];
// This is a debug only thingy, if it fails, not worth taking
// down the process.
if (strValue == NULL)
return;
// now translate the unicode to ansi string
WszWideCharToMultiByte(CP_UTF8, 0, wstrValue, -1, strValue, cbReq+1, 0,0);
char *szEnd = (char *)strchr(strValue, ';');
char *szStart = strValue;
while (szEnd != NULL)
{
// Found a key value
char *szNameEnd = strchr(szStart, '=');
int iCount;
DebuggerIPCEventType eventType;
if (szNameEnd != NULL)
{
// This is a well form key
*szNameEnd = '\0';
*szEnd = '\0';
// now szStart is the key name null terminated. Translate the counter into integer.
iCount = atoi(szNameEnd+1);
if (iCount != 0)
{
eventType = IPCENames::GetEventType(szStart);
if (eventType < DB_IPCE_DEBUGGER_FIRST)
{
// use the runtime one
g_iDbgRuntimeCounter[eventType & 0x00ff] = iCount;
}
else if (eventType < DB_IPCE_DEBUGGER_LAST)
{
// use the debugger one
g_iDbgDebuggerCounter[eventType & 0x00ff] = iCount;
}
else
_ASSERTE(!"Unknown Event Type");
}
}
szStart = szEnd + 1;
// try to find next key value
szEnd = (char *)strchr(szStart, ';');
}
// free the ansi buffer
delete [] strValue;
REGUTIL::FreeConfigString(wstrValue);
}
#endif // _DEBUG
}
// This is a notification from the EE it's about to go to fiber mode.
// This is given *before* it actually goes to fiber mode.
HRESULT Debugger::SetFiberMode(bool isFiberMode)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
// Notifications from EE never come on helper worker.
PRECONDITION(!ThisIsHelperThreadWorker());
}
CONTRACTL_END;
Thread * pThread = ::GetThread();
m_pRCThread->m_pDCB->m_bHostingInFiber = isFiberMode;
// If there is a debugger already attached, then we have a big problem. As of V2.0, the debugger
// does not support debugging processes with fibers in them. We set the unrecoverable state to
// indicate that we're in a bad state now. The debugger will notice this, and take appropiate action.
if (isFiberMode && CORDebuggerAttached())
{
LOG((LF_CORDB, LL_INFO10, "Thread has entered fiber mode while debugger attached.\n"));
EX_TRY
{
// We send up a MDA for two reasons: 1) we want to give the user some chance to see what went wrong,
// and 2) we want to get the Right Side to notice that we're in an unrecoverable error state now.
SString szName(W("DebuggerFiberModeNotSupported"));
SString szDescription;
szDescription.LoadResource(CCompRC::Debugging, MDARC_DEBUGGER_FIBER_MODE_NOT_SUPPORTED);
SString szXML(W(""));
// Sending any debug event will be a GC violation.
// However, if we're enabling fiber-mode while a debugger is attached, we're already doomed.
// Deadlocks and AVs are just around the corner. A Gc-violation is the least of our worries.
// We want to at least notify the debugger at all costs.
CONTRACT_VIOLATION(GCViolation);
// As soon as we set unrecoverable error in the LS, the RS will pick it up and basically shut down.
// It won't dispatch any events. So we fire the MDA first, and then set unrecoverable error.
SendMDANotification(pThread, &szName, &szDescription, &szXML, (CorDebugMDAFlags) 0, FALSE);
CORDBDebuggerSetUnrecoverableError(this, CORDBG_E_CANNOT_DEBUG_FIBER_PROCESS, false);
// Fire the MDA again just to force the RS to sniff the LS and pick up that we're in an unrecoverable error.
// No harm done from dispatching an MDA twice. And
SendMDANotification(pThread, &szName, &szDescription, &szXML, (CorDebugMDAFlags) 0, FALSE);
}
EX_CATCH
{
LOG((LF_CORDB, LL_INFO10, "Error sending MDA regarding fiber mode.\n"));
}
EX_END_CATCH(SwallowAllExceptions);
}
return S_OK;
}
// Checks if the MethodInfos table has been allocated, and if not does so.
// Throw on failure, so we always return
HRESULT Debugger::CheckInitMethodInfoTable()
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (m_pMethodInfos == NULL)
{
DebuggerMethodInfoTable *pMethodInfos = NULL;
EX_TRY
{
pMethodInfos = new (interopsafe) DebuggerMethodInfoTable();
}
EX_CATCH
{
pMethodInfos = NULL;
}
EX_END_CATCH(RethrowTerminalExceptions);
if (pMethodInfos == NULL)
{
return E_OUTOFMEMORY;
}
if (InterlockedCompareExchangeT(&m_pMethodInfos, pMethodInfos, NULL) != NULL)
{
DeleteInteropSafe(pMethodInfos);
}
}
return S_OK;
}
// Checks if the m_pModules table has been allocated, and if not does so.
HRESULT Debugger::CheckInitModuleTable()
{
CONTRACT(HRESULT)
{
NOTHROW;
GC_NOTRIGGER;
POSTCONDITION(m_pModules != NULL);
}
CONTRACT_END;
if (m_pModules == NULL)
{
DebuggerModuleTable *pModules = new (interopsafe, nothrow) DebuggerModuleTable();
if (pModules == NULL)
{
RETURN (E_OUTOFMEMORY);
}
if (InterlockedCompareExchangeT(&m_pModules, pModules, NULL) != NULL)
{
DeleteInteropSafe(pModules);
}
}
RETURN (S_OK);
}
// Checks if the m_pModules table has been allocated, and if not does so.
HRESULT Debugger::CheckInitPendingFuncEvalTable()
{
CONTRACT(HRESULT)
{
NOTHROW;
GC_NOTRIGGER;
POSTCONDITION(GetPendingEvals() != NULL);
}
CONTRACT_END;
#ifndef DACCESS_COMPILE
if (GetPendingEvals() == NULL)
{
DebuggerPendingFuncEvalTable *pPendingEvals = new (interopsafe, nothrow) DebuggerPendingFuncEvalTable();
if (pPendingEvals == NULL)
{
RETURN(E_OUTOFMEMORY);
}
// Since we're setting, we need an LValue and not just an accessor.
if (InterlockedCompareExchangeT(&(GetLazyData()->m_pPendingEvals), pPendingEvals, NULL) != NULL)
{
DeleteInteropSafe(pPendingEvals);
}
}
#endif
RETURN (S_OK);
}
#ifdef _DEBUG_DMI_TABLE
// Returns the number of (official) entries in the table
ULONG DebuggerMethodInfoTable::CheckDmiTable(void)
{
LIMITED_METHOD_CONTRACT;
ULONG cApparant = 0;
ULONG cOfficial = 0;
if (NULL != m_pcEntries)
{
DebuggerMethodInfoEntry *dcp;
int i = 0;
while (i++ <m_iEntries)
{
dcp = (DebuggerMethodInfoEntry*)&(((DebuggerMethodInfoEntry *)m_pcEntries)[i]);
if(dcp->pFD != 0 &&
dcp->pFD != (MethodDesc*)0xcdcdcdcd &&
dcp->mi != NULL)
{
cApparant++;
_ASSERTE( dcp->pFD == dcp->mi->m_fd );
LOG((LF_CORDB, LL_INFO1000, "DMIT::CDT:Entry:0x%p mi:0x%p\nPrevs:\n",
dcp, dcp->mi));
DebuggerMethodInfo *dmi = dcp->mi->m_prevMethodInfo;
while(dmi != NULL)
{
LOG((LF_CORDB, LL_INFO1000, "\t0x%p\n", dmi));
dmi = dmi->m_prevMethodInfo;
}
dmi = dcp->mi->m_nextMethodInfo;
LOG((LF_CORDB, LL_INFO1000, "Nexts:\n", dmi));
while(dmi != NULL)
{
LOG((LF_CORDB, LL_INFO1000, "\t0x%p\n", dmi));
dmi = dmi->m_nextMethodInfo;
}
LOG((LF_CORDB, LL_INFO1000, "DMIT::CDT:DONE\n",
dcp, dcp->mi));
}
}
if (m_piBuckets == 0)
{
LOG((LF_CORDB, LL_INFO1000, "DMIT::CDT: The table is officially empty!\n"));
return cOfficial;
}
LOG((LF_CORDB, LL_INFO1000, "DMIT::CDT:Looking for official entries:\n"));
ULONG iNext = m_piBuckets[0];
ULONG iBucket = 1;
HASHENTRY *psEntry = NULL;
while (TRUE)
{
while (iNext != UINT32_MAX)
{
cOfficial++;
psEntry = EntryPtr(iNext);
dcp = ((DebuggerMethodInfoEntry *)psEntry);
LOG((LF_CORDB, LL_INFO1000, "\tEntry:0x%p mi:0x%p @idx:0x%x @bucket:0x%x\n",
dcp, dcp->mi, iNext, iBucket));
iNext = psEntry->iNext;
}
// Advance to the next bucket.
if (iBucket < m_iBuckets)
iNext = m_piBuckets[iBucket++];
else
break;
}
LOG((LF_CORDB, LL_INFO1000, "DMIT::CDT:Finished official entries: ****************"));
}
return cOfficial;
}
#endif // _DEBUG_DMI_TABLE
//---------------------------------------------------------------------------------------
//
// Class constructor for DebuggerEval. This is the supporting data structure for
// func-eval tracking.
//
// Arguments:
// pContext - The context to return to when done with this eval.
// pEvalInfo - Contains all the important information, such as parameters, type args, method.
// fInException - TRUE if the thread for the eval is currently in an exception notification.
//
DebuggerEval::DebuggerEval(CONTEXT * pContext, DebuggerIPCE_FuncEvalInfo * pEvalInfo, bool fInException)
{
WRAPPER_NO_CONTRACT;
// This must be non-zero so that the saved opcode is non-zero, and on IA64 we want it to be 0x16
// so that we can have a breakpoint instruction in any slot in the bundle.
m_breakpointInstruction[0] = 0x16;
#if defined(_TARGET_ARM_)
USHORT *bp = (USHORT*)&m_breakpointInstruction;
*bp = CORDbg_BREAK_INSTRUCTION;
#endif // _TARGET_ARM_
m_thread = pEvalInfo->vmThreadToken.GetRawPtr();
m_evalType = pEvalInfo->funcEvalType;
m_methodToken = pEvalInfo->funcMetadataToken;
m_classToken = pEvalInfo->funcClassMetadataToken;
// Note: we can't rely on just the DebuggerModule* or AppDomain* because the AppDomain
// could get unloaded between now and when the funceval actually starts. So we stash an
// AppDomain ID which is safe to use after the AD is unloaded. It's only safe to
// use the DebuggerModule* after we've verified the ADID is still valid (i.e. by entering that domain).
m_debuggerModule = g_pDebugger->LookupOrCreateModule(pEvalInfo->vmDomainFile);
if (m_debuggerModule == NULL)
{
// We have no associated code.
_ASSERTE((m_evalType == DB_IPCE_FET_NEW_STRING) || (m_evalType == DB_IPCE_FET_NEW_ARRAY));
// We'll just do the creation in whatever domain the thread is already in.
// It's conceivable that we might want to allow the caller to specify a specific domain, but
// ICorDebug provides the debugger with no was to specify the domain.
m_appDomainId = m_thread->GetDomain()->GetId();
}
else
{
m_appDomainId = m_debuggerModule->GetAppDomain()->GetId();
}
m_funcEvalKey = pEvalInfo->funcEvalKey;
m_argCount = pEvalInfo->argCount;
m_targetCodeAddr = NULL;
m_stringSize = pEvalInfo->stringSize;
m_arrayRank = pEvalInfo->arrayRank;
m_genericArgsCount = pEvalInfo->genericArgsCount;
m_genericArgsNodeCount = pEvalInfo->genericArgsNodeCount;
m_successful = false;
m_argData = NULL;
m_result = 0;
m_md = NULL;
m_resultType = TypeHandle();
m_aborting = FE_ABORT_NONE;
m_aborted = false;
m_completed = false;
m_evalDuringException = fInException;
m_rethrowAbortException = false;
m_retValueBoxing = Debugger::NoValueTypeBoxing;
m_requester = (Thread::ThreadAbortRequester)0;
m_vmObjectHandle = VMPTR_OBJECTHANDLE::NullPtr();
// Copy the thread's context.
if (pContext == NULL)
{
memset(&m_context, 0, sizeof(m_context));
}
else
{
memcpy(&m_context, pContext, sizeof(m_context));
}
}
//---------------------------------------------------------------------------------------
//
// This constructor is only used when setting up an eval to re-abort a thread.
//
// Arguments:
// pContext - The context to return to when done with this eval.
// pThread - The thread to re-abort.
// requester - The type of abort to throw.
//
DebuggerEval::DebuggerEval(CONTEXT * pContext, Thread * pThread, Thread::ThreadAbortRequester requester)
{
WRAPPER_NO_CONTRACT;
// This must be non-zero so that the saved opcode is non-zero, and on IA64 we want it to be 0x16
// so that we can have a breakpoint instruction in any slot in the bundle.
m_breakpointInstruction[0] = 0x16;
m_thread = pThread;
m_evalType = DB_IPCE_FET_RE_ABORT;
m_methodToken = mdMethodDefNil;
m_classToken = mdTypeDefNil;
m_debuggerModule = NULL;
m_funcEvalKey = RSPTR_CORDBEVAL::NullPtr();
m_argCount = 0;
m_stringSize = 0;
m_arrayRank = 0;
m_genericArgsCount = 0;
m_genericArgsNodeCount = 0;
m_successful = false;
m_argData = NULL;
m_targetCodeAddr = NULL;
m_result = 0;
m_md = NULL;
m_resultType = TypeHandle();
m_aborting = FE_ABORT_NONE;
m_aborted = false;
m_completed = false;
m_evalDuringException = false;
m_rethrowAbortException = false;
m_retValueBoxing = Debugger::NoValueTypeBoxing;
m_requester = requester;
if (pContext == NULL)
{
memset(&m_context, 0, sizeof(m_context));
}
else
{
memcpy(&m_context, pContext, sizeof(m_context));
}
}
#ifdef _DEBUG
// Thread proc for interop stress coverage. Have an unmanaged thread
// that just loops throwing native exceptions. This can test corner cases
// such as getting an native exception while the runtime is synced.
DWORD WINAPI DbgInteropStressProc(void * lpParameter)
{
LIMITED_METHOD_CONTRACT;
int i = 0;
int zero = 0;
// This will ensure that the compiler doesn't flag our 1/0 exception below at compile-time.
if (lpParameter != NULL)
{
zero = 1;
}
// Note that this thread is a non-runtime thread. So it can't take any CLR locks
// or do anything else that may block the helper thread.
// (Log statements take CLR locks).
while(true)
{
i++;
if ((i % 10) != 0)
{
// Generate an in-band event.
PAL_CPP_TRY
{
// Throw a handled exception. Don't use an AV since that's pretty special.
*(int*)lpParameter = 1 / zero;
}
PAL_CPP_CATCH_ALL
{
}
PAL_CPP_ENDTRY
}
else
{
// Generate the occasional oob-event.
WszOutputDebugString(W("Ping from DbgInteropStressProc"));
}
// This helps parallelize if we have a lot of threads, and keeps us from
// chewing too much CPU time.
ClrSleepEx(2000,FALSE);
ClrSleepEx(GetRandomInt(1000), FALSE);
}
return 0;
}
// ThreadProc that does everything in a can't stop region.
DWORD WINAPI DbgInteropCantStopStressProc(void * lpParameter)
{
WRAPPER_NO_CONTRACT;
// This will mark us as a can't stop region.
ClrFlsSetThreadType (ThreadType_DbgHelper);
return DbgInteropStressProc(lpParameter);
}
// Generate lots of OOB events.
DWORD WINAPI DbgInteropDummyStressProc(void * lpParameter)
{
LIMITED_METHOD_CONTRACT;
ClrSleepEx(1,FALSE);
return 0;
}
DWORD WINAPI DbgInteropOOBStressProc(void * lpParameter)
{
WRAPPER_NO_CONTRACT;
int i = 0;
while(true)
{
i++;
if (i % 10 == 1)
{
// Create a dummy thread. That generates 2 oob events
// (1 for create, 1 for destroy)
DWORD id;
::CreateThread(NULL, 0, DbgInteropDummyStressProc, NULL, 0, &id);
}
else
{
// Generate the occasional oob-event.
WszOutputDebugString(W("OOB ping from "));
}
ClrSleepEx(3000, FALSE);
}
return 0;
}
// List of the different possible stress procs.
LPTHREAD_START_ROUTINE g_pStressProcs[] =
{
DbgInteropOOBStressProc,
DbgInteropCantStopStressProc,
DbgInteropStressProc
};
#endif
DebuggerHeap * Debugger::GetInteropSafeHeap()
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Lazily initialize our heap.
if (!m_heap.IsInit())
{
_ASSERTE(!"InteropSafe Heap should have already been initialized in LazyInit");
// Just in case we miss it in retail, convert to OOM here:
ThrowOutOfMemory();
}
return &m_heap;
}
DebuggerHeap * Debugger::GetInteropSafeHeap_NoThrow()
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Lazily initialize our heap.
if (!m_heap.IsInit())
{
_ASSERTE(!"InteropSafe Heap should have already been initialized in LazyInit");
// Just in case we miss it in retail, convert to OOM here:
return NULL;
}
return &m_heap;
}
DebuggerHeap * Debugger::GetInteropSafeExecutableHeap()
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Lazily initialize our heap.
if (!m_executableHeap.IsInit())
{
_ASSERTE(!"InteropSafe Executable Heap should have already been initialized in LazyInit");
// Just in case we miss it in retail, convert to OOM here:
ThrowOutOfMemory();
}
return &m_executableHeap;
}
DebuggerHeap * Debugger::GetInteropSafeExecutableHeap_NoThrow()
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Lazily initialize our heap.
if (!m_executableHeap.IsInit())
{
_ASSERTE(!"InteropSafe Executable Heap should have already been initialized in LazyInit");
// Just in case we miss it in retail, convert to OOM here:
return NULL;
}
return &m_executableHeap;
}
//---------------------------------------------------------------------------------------
//
// Notify potential debugger that the runtime has started up
//
//
// Assumptions:
// Called during startup path
//
// Notes:
// If no debugger is attached, this does nothing.
//
//---------------------------------------------------------------------------------------
void Debugger::RaiseStartupNotification()
{
// Right-side will read this field from OOP via DAC-primitive to determine attach or launch case.
// We do an interlocked increment to gaurantee this is an atomic memory write, and to ensure
// that it's flushed from any CPU cache into memory.
InterlockedIncrement(&m_fLeftSideInitialized);
// If we are remote debugging, don't send the event now if a debugger is not attached. No one will be
// listening, and we will fail. However, we still want to initialize the variable above.
BOOL fRaiseStartupNotification = TRUE;
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
DWORD useTransport = CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgUseTransport);
if(useTransport)
{
fRaiseStartupNotification = (CORDebuggerAttached() ? TRUE : FALSE);
}
#endif
if (fRaiseStartupNotification)
{
DebuggerIPCEvent startupEvent;
InitIPCEvent(&startupEvent, DB_IPCE_LEFTSIDE_STARTUP, NULL, VMPTR_AppDomain::NullPtr());
SendRawEvent(&startupEvent);
// RS will set flags from OOP while we're stopped at the event if it wants to attach.
}
}
//---------------------------------------------------------------------------------------
//
// Sends a raw managed debug event to the debugger.
//
// Arguments:
// pManagedEvent - managed debug event
//
//
// Notes:
// This can be called even if a debugger is not attached.
// The entire process will get frozen by the debugger once we send. The debugger
// needs to resume the process. It may detach as well.
// See code:IsEventDebuggerNotification for decoding this event. These methods must stay in sync.
// The debugger process reads the events via code:CordbProcess.CopyManagedEventFromTarget.
//
//---------------------------------------------------------------------------------------
void Debugger::SendRawEvent(const DebuggerIPCEvent * pManagedEvent)
{
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
DWORD useTransport = CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgUseTransport);
if(useTransport)
{
HRESULT hr = g_pDbgTransport->SendDebugEvent(const_cast<DebuggerIPCEvent *>(pManagedEvent));
if (FAILED(hr))
{
_ASSERTE(!"Failed to send debugger event");
STRESS_LOG1(LF_CORDB, LL_INFO1000, "D::SendIPCEvent Error on Send with 0x%x\n", hr);
UnrecoverableError(hr,
0,
FILE_DEBUG,
LINE_DEBUG,
false);
// @dbgtodo Mac - what can we do here?
}
}
else
{
#endif
// We get to send an array of ULONG_PTRs as data with the notification.
// The debugger can then use ReadProcessMemory to read through this array.
ULONG_PTR rgData [] = {
CLRDBG_EXCEPTION_DATA_CHECKSUM,
(ULONG_PTR) g_pMSCorEE,
(ULONG_PTR) pManagedEvent
};
// If no debugger attached, then don't bother raising a 1st-chance exception because nobody will sniff it.
// @dbgtodo iDNA: in iDNA case, the recorder may sniff it.
if (!IsDebuggerPresent())
{
return;
}
//
// Physically send the event via an OS Exception. We're using exceptions as a notification
// mechanism on top of the OS native debugging pipeline.
// @dbgtodo cross-plat - this needs to be cross-plat.
//
EX_TRY
{
const DWORD dwFlags = 0; // continuable (eg, Debugger can continue GH)
RaiseException(CLRDBG_NOTIFICATION_EXCEPTION_CODE, dwFlags, NumItems(rgData), rgData);
// If debugger continues "GH" (DBG_CONTINUE), then we land here.
// This is the expected path for a well-behaved ICorDebug debugger.
}
EX_CATCH
{
// If no debugger is attached, or if the debugger continues "GN" (DBG_EXCEPTION_NOT_HANDLED), then we land here.
// A naive (not-ICorDebug aware) native-debugger won't handle the exception and so land us here.
// We may also get here if a debugger detaches at the Exception notification
// (and thus implicitly continues GN).
}
EX_END_CATCH(SwallowAllExceptions);
#ifdef FEATURE_DBGIPC_TRANSPORT_VM
}
#endif // FEATURE_DBGIPC_TRANSPORT_VM
}
//---------------------------------------------------------------------------------------
// Send a createProcess event to give the RS a chance to do SetDesiredNGENFlags
//
// Arguments:
// pDbgLockHolder - lock holder.
//
// Assumptions:
// Lock is initially held. This will toggle the lock to send an IPC event.
// This will start a synchronization.
//
// Notes:
// In V2, this also gives the RS a chance to intialize the IPC protocol.
// Spefically, this needs to be sent before the LS can send a sync-complete.
//---------------------------------------------------------------------------------------
void Debugger::SendCreateProcess(DebuggerLockHolder * pDbgLockHolder)
{
pDbgLockHolder->Release();
// Encourage helper thread to spin up so that we're in a consistent state.
PollWaitingForHelper();
// we don't need to use SENDIPCEVENT_BEGIN/END macros that perform the debug-suspend aware checks,
// as this code executes on the startup path...
SENDIPCEVENT_RAW_BEGIN(pDbgLockHolder);
// Send a CreateProcess event.
// @dbgtodo pipeline - eliminate these reasons for needing a CreateProcess event (part of pipeline feature crew)
// This will let the RS know that the IPC block is up + ready, and then the RS can read it.
// The RS will then update the DCB with enough information so that we can send the sync-complete.
// (such as letting us know whether we're interop-debugging or not).
DebuggerIPCEvent event;
InitIPCEvent(&event, DB_IPCE_CREATE_PROCESS, NULL, VMPTR_AppDomain::NullPtr());
SendRawEvent(&event);
// @dbgtodo inspection- it doesn't really make sense to sync on a CreateProcess. We only have 1 thread
// in the CLR and we know exactly what state we're in and we can ensure that we're synchronized.
// For V3,RS should be able to treat a CreateProcess like a synchronized.
// Remove this in V3 as we make SetDesiredNgenFlags operate OOP.
TrapAllRuntimeThreads();
// Must have a thread object so that we ensure that we will actually block here.
// This ensures the debuggee is actually stopped at startup, and
// this gives the debugger a chance to call SetDesiredNGENFlags before we
// set s_fCanChangeNgenFlags to FALSE.
_ASSERTE(GetThread() != NULL);
SENDIPCEVENT_RAW_END;
pDbgLockHolder->Acquire();
}
#ifdef FEATURE_CORECLR
HANDLE g_hContinueStartupEvent = NULL;
CLR_ENGINE_METRICS g_CLREngineMetrics = {
sizeof(CLR_ENGINE_METRICS),
CorDebugVersion_4_0,
&g_hContinueStartupEvent};
bool IsTelestoDebugPackInstalled()
{
#ifdef FEATURE_PAL
return false;
#else
RegKeyHolder hKey;
if (ERROR_SUCCESS != WszRegOpenKeyEx(HKEY_LOCAL_MACHINE, FRAMEWORK_REGISTRY_KEY_W, 0, KEY_READ, &hKey))
return false;
bool debugPackInstalled = false;
DWORD cbValue = 0;
if (ERROR_SUCCESS == WszRegQueryValueEx(hKey, CLRConfig::EXTERNAL_DbgPackShimPath, NULL, NULL, NULL, &cbValue))
{
if (cbValue != 0)
{
debugPackInstalled = true;
}
}
// RegCloseKey called by holder
return debugPackInstalled;
#endif // FEATURE_PAL
}
#define StartupNotifyEventNamePrefix W("TelestoStartupEvent_")
const int cchEventNameBufferSize = sizeof(StartupNotifyEventNamePrefix)/sizeof(WCHAR) + 8; // + hex DWORD (8). NULL terminator is included in sizeof(StartupNotifyEventNamePrefix)
HANDLE OpenStartupNotificationEvent()
{
DWORD debuggeePID = GetCurrentProcessId();
WCHAR szEventName[cchEventNameBufferSize];
swprintf_s(szEventName, cchEventNameBufferSize, StartupNotifyEventNamePrefix W("%08x"), debuggeePID);
return WszOpenEvent(EVENT_ALL_ACCESS, FALSE, szEventName);
}
void NotifyDebuggerOfTelestoStartup()
{
// Create the continue event first so that we guarantee that any
// enumeration of this process will get back a valid continue event
// the instant we signal the startup notification event.
CONSISTENCY_CHECK(NULL == g_hContinueStartupEvent);
g_hContinueStartupEvent = WszCreateEvent(NULL, TRUE, FALSE, NULL);
CONSISTENCY_CHECK(INVALID_HANDLE_VALUE != g_hContinueStartupEvent); // we reserve this value for error conditions in EnumerateCLRs
HANDLE startupEvent = OpenStartupNotificationEvent();
if (startupEvent != NULL)
{
// signal notification event
SetEvent(startupEvent);
CloseHandle(startupEvent);
startupEvent = NULL;
// wait on continue startup event
// The debugger may attach to us while we're blocked here.
WaitForSingleObject(g_hContinueStartupEvent, INFINITE);
}
CloseHandle(g_hContinueStartupEvent);
g_hContinueStartupEvent = NULL;
}
#endif // FEATURE_CORECLR
//---------------------------------------------------------------------------------------
//
// Initialize Left-Side debugger object
//
// Return Value:
// S_OK on successs. May also throw.
//
// Assumptions:
// This is called in the startup path.
//
// Notes:
// Startup initializes any necessary debugger objects, including creating
// and starting the Runtime Controller thread. Once the RC thread is started
// and we return successfully, the Debugger object can expect to have its
// event handlers called.
//
//---------------------------------------------------------------------------------------
HRESULT Debugger::Startup(void)
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
HRESULT hr = S_OK;
_ASSERTE(g_pEEInterface != NULL);
#if defined(FEATURE_CORECLR) && !defined(FEATURE_PAL)
if (IsWatsonEnabled() || IsTelestoDebugPackInstalled())
{
// Iff the debug pack is installed, then go through the telesto debugging pipeline.
LOG((LF_CORDB, LL_INFO10, "Debugging service is enabled because debug pack is installed or Watson support is enabled)\n"));
#ifdef FEATURE_DBGIPC_TRANSPORT_VM
DWORD useTransport = CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgUseTransport);
if(!useTransport)
{
#endif
// This may block while an attach occurs.
NotifyDebuggerOfTelestoStartup();
#ifdef FEATURE_DBGIPC_TRANSPORT_VM
}
#endif
}
else
{
// On Windows, it's actually safe to finish the initialization here even without the debug pack.
// However, doing so causes a perf regression because we used to bail out early if the debug
// pack is not installed.
//
// Unlike Windows, we can't continue executing this function if the debug pack is not installed.
// The transport requires the debug pack to be present. Otherwise it'll raise a fatal error.
return S_FALSE;
}
#endif // FEATURE_CORECLR && !FEATURE_PAL
DebuggerLockHolder dbgLockHolder(this);
// Stubs in Stacktraces are always enabled.
g_EnableSIS = true;
// We can get extra Interop-debugging test coverage by having some auxillary unmanaged
// threads running and throwing debug events. Keep these stress procs separate so that
// we can focus on certain problem areas.
#ifdef _DEBUG
g_DbgShouldntUseDebugger = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgNoDebugger) != 0;
// Creates random thread procs.
DWORD dwRegVal = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgExtraThreads);
DWORD dwId;
DWORD i;
if (dwRegVal > 0)
{
for(i = 0; i < dwRegVal; i++)
{
int iProc = GetRandomInt(NumItems(g_pStressProcs));
LPTHREAD_START_ROUTINE pStartRoutine = g_pStressProcs[iProc];
::CreateThread(NULL, 0, pStartRoutine, NULL, 0, &dwId);
LOG((LF_CORDB, LL_INFO1000, "Created random thread (%d) with tid=0x%x\n", i, dwId));
}
}
dwRegVal = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgExtraThreadsIB);
if (dwRegVal > 0)
{
for(i = 0; i < dwRegVal; i++)
{
::CreateThread(NULL, 0, DbgInteropStressProc, NULL, 0, &dwId);
LOG((LF_CORDB, LL_INFO1000, "Created extra thread (%d) with tid=0x%x\n", i, dwId));
}
}
dwRegVal = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgExtraThreadsCantStop);
if (dwRegVal > 0)
{
for(i = 0; i < dwRegVal; i++)
{
::CreateThread(NULL, 0, DbgInteropCantStopStressProc, NULL, 0, &dwId);
LOG((LF_CORDB, LL_INFO1000, "Created extra thread 'can't-stop' (%d) with tid=0x%x\n", i, dwId));
}
}
dwRegVal = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgExtraThreadsOOB);
if (dwRegVal > 0)
{
for(i = 0; i < dwRegVal; i++)
{
::CreateThread(NULL, 0, DbgInteropOOBStressProc, NULL, 0, &dwId);
LOG((LF_CORDB, LL_INFO1000, "Created extra thread OOB (%d) with tid=0x%x\n", i, dwId));
}
}
#endif
// Lazily initialize the interop-safe heap
// Must be done before the RC thread is initialized.
// @dbgtodo - In V2, LS was lazily initialized; but was eagerly pre-initialized if launched by debugger.
// (This was for perf reasons). But we don't want Launch vs. Attach checks in the LS, so we now always
// init. As we move more to OOP, this init will become cheaper.
{
LazyInit();
DebuggerController::Initialize();
}
InitializeHijackFunctionAddress();
// Create the runtime controller thread, a.k.a, the debug helper thread.
// Don't use the interop-safe heap b/c we don't want to lazily create it.
m_pRCThread = new DebuggerRCThread(this);
_ASSERTE(m_pRCThread != NULL); // throws on oom
TRACE_ALLOC(m_pRCThread);
hr = m_pRCThread->Init();
_ASSERTE(SUCCEEDED(hr)); // throws on error
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
DWORD useTransport = CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgUseTransport);
if(useTransport)
{
// The in-process DAC for Mac is lazily initialized when we get the first DDMessage.
// We check whether the DAC and the runtime has matching versions when we do the initialization, and
// we'll fail if the versions don't match. That's why we don't want to do the initialization here because
// even if we have the wrong version of DAC, managed apps can still run. We just can't debug it.
// Create transport control block and initialize it.
g_pDbgTransport = new DbgTransportSession();
hr = g_pDbgTransport->Init(m_pRCThread->GetDCB(), m_pAppDomainCB, &m_inProcDac);
if (FAILED(hr))
ThrowHR(hr);
// Create interface to talk to debugger proxy and initialize it.
DbgTransportProxy *pProxy = new DbgTransportProxy();
hr = pProxy->Init(g_pDbgTransport->GetPort());
if (FAILED(hr))
ThrowHR(hr);
// Contact the debugger proxy process for this machine. This has several purposes:
// 1) Register this runtime instance as available for debugging.
// 2) Check whether a debugger is already waiting to attach to us.
// 3) Publish the port number we expect debugging requests to target.
// The following call blocks until we receive a reply from the proxy or time out.
DbgProxyResult result = pProxy->RegisterWithProxy();
switch (result)
{
case RequestTimedOut:
// The proxy doesn't appear to be there, we're not debuggable as a result.
// To be careful (and avoid malicious types trying to connect to us even when the proxy is not up)
// neuter the transport so that it won't accept any connections. Ideally we'd just shutdown the
// debugger subsystem entirely, but this appears to be somewhat complex at this late stage.
g_pDbgTransport->Neuter();
break;
case RequestSuccessful:
// We registered with the proxy successfully. No debugger was interested in
// us just yet.
break;
case PendingDebuggerAttach:
// We registered with the proxy and found that a debugger was registered for
// an early attach.
// Mark this process as launched by the debugger and the debugger as attached.
g_CORDebuggerControlFlags |= DBCF_GENERATE_DEBUG_CODE;
MarkDebuggerAttachedInternal();
LazyInit();
DebuggerController::Initialize();
break;
default:
_ASSERTE(!"Unknown result code from DbgTransportSession::RegisterWithProxy()");
}
// The debugger no longer needs to talk with the proxy.
pProxy->Shutdown();
delete pProxy;
}
#endif // FEATURE_DBGIPC_TRANSPORT_VM
RaiseStartupNotification();
// Also initialize the AppDomainEnumerationIPCBlock
#if defined(FEATURE_IPCMAN)
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
DWORD useTransport = CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgUseTransport);
if(useTransport)
{
m_pAppDomainCB = new (nothrow) AppDomainEnumerationIPCBlock();
}
else
{
#endif
m_pAppDomainCB = g_pIPCManagerInterface->GetAppDomainBlock();
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
}
#endif
#else // FEATURE_IPCMAN
m_pAppDomainCB = new (nothrow) AppDomainEnumerationIPCBlock();
#endif // FEATURE_IPCMAN
if (m_pAppDomainCB == NULL)
{
LOG((LF_CORDB, LL_INFO100, "D::S: Failed to get AppDomain IPC block from IPCManager.\n"));
ThrowHR(E_FAIL);
}
hr = InitAppDomainIPC();
_ASSERTE(SUCCEEDED(hr)); // throws on error.
// See if we need to spin up the helper thread now, rather than later.
DebuggerIPCControlBlock* pIPCControlBlock = m_pRCThread->GetDCB();
(void)pIPCControlBlock; //prevent "unused variable" error from GCC
_ASSERTE(pIPCControlBlock != NULL);
_ASSERTE(!pIPCControlBlock->m_rightSideShouldCreateHelperThread);
{
// Create the win32 thread for the helper and let it run free.
hr = m_pRCThread->Start();
// convert failure to exception as with old contract
if (FAILED(hr))
{
ThrowHR(hr);
}
LOG((LF_CORDB, LL_EVERYTHING, "Start was successful\n"));
}
#ifdef TEST_DATA_CONSISTENCY
// if we have set the environment variable TestDataConsistency, run the data consistency test.
// See code:DataTest::TestDataSafety for more information
if((g_pConfig != NULL) && (g_pConfig->TestDataConsistency() == true))
{
DataTest dt;
dt.TestDataSafety();
}
#endif
// We don't bother changing this process's permission.
// A managed debugger will have the SE_DEBUG permission which will allow it to open our process handle,
// even if we're a guest account.
return hr;
}
//---------------------------------------------------------------------------------------
// Finishes startup once we have a Thread object.
//
// Arguments:
// pThread - the current thread. Must be non-null
//
// Notes:
// Most debugger initialization is done in code:Debugger.Startup,
// However, debugger can't block on synchronization without a Thread object,
// so sending IPC events must wait until after we have a thread object.
//---------------------------------------------------------------------------------------
HRESULT Debugger::StartupPhase2(Thread * pThread)
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
HRESULT hr = S_OK;
// Must have a thread so that we can block
_ASSERTE(pThread != NULL);
DebuggerLockHolder dbgLockHolder(this);
// @dbgtodo - This may need to change when we remove SetupSyncEvent...
// If we're launching, then sync now so that the RS gets an early chance to dispatch the CreateProcess event.
// This is especially important b/c certain portions of the ICorDebugAPI (like setting ngen flags) are only
// valid during the CreateProcess callback in the launch case.
// We need to send the callback early enough so those APIs can set the flags before they're actually used.
// We also ensure the debugger is actually attached.
if (SUCCEEDED(hr) && CORDebuggerAttached())
{
StartCanaryThread();
SendCreateProcess(&dbgLockHolder); // toggles lock
}
// After returning from debugger startup we assume that the runtime might start using the NGEN flags to make
// binding decisions. From now on the debugger can not influence NGEN binding policy
s_fCanChangeNgenFlags = FALSE;
// Must release the lock (which would be done at the end of this method anyways) so that
// the helper thread can do the jit-attach.
dbgLockHolder.Release();
#ifdef _DEBUG
// Give chance for stress harnesses to launch a managed debugger when a managed app starts up.
// This lets us run a set of managed apps under a debugger.
if (!CORDebuggerAttached())
{
#define DBG_ATTACH_ON_STARTUP_ENV_VAR W("COMPlus_DbgAttachOnStartup")
// We explicitly just check the env because we don't want a switch this invasive to be global.
DWORD fAttach = WszGetEnvironmentVariable(DBG_ATTACH_ON_STARTUP_ENV_VAR, NULL, 0) > 0;
if (fAttach)
{
// Remove the env var from our process so that the debugger we spin up won't inherit it.
// Else, if the debugger is managed, we'll have an infinite recursion.
BOOL fOk = WszSetEnvironmentVariable(DBG_ATTACH_ON_STARTUP_ENV_VAR, NULL);
if (fOk)
{
// We've already created the helper thread (which can service the attach request)
// So just do a normal jit-attach now.
SString szName(W("DebuggerStressStartup"));
SString szDescription(W("MDA used for debugger-stress scenario. This is fired to trigger a jit-attach")
W("to allow us to attach a debugger to any managed app that starts up.")
W("This MDA is only fired when the 'DbgAttachOnStartup' COM+ knob/reg-key is set on checked builds."));
SString szXML(W("<xml>See the description</xml>"));
SendMDANotification(
NULL, // NULL b/c we don't have a thread yet
&szName,
&szDescription,
&szXML,
((CorDebugMDAFlags) 0 ),
TRUE // this will force the jit-attach
);
}
}
}
#endif
return hr;
}
//---------------------------------------------------------------------------------------
//
// Public entrypoint into the debugger to force the lazy data to be initialized at a
// controlled point in time. This is useful for those callers into the debugger (e.g.,
// ETW rundown) that know they will need the lazy data initialized but cannot afford to
// have it initialized unpredictably or inside a lock.
//
// This may be called more than once, and will know to initialize the lazy data only
// once.
//
void Debugger::InitializeLazyDataIfNecessary()
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
if (!HasLazyData())
{
DebuggerLockHolder lockHolder(this);
LazyInit(); // throws
}
}
/******************************************************************************
Lazy initialize stuff once we know we are debugging.
This reduces the startup cost in the non-debugging case.
We can do this at a bunch of random strategic places.
******************************************************************************/
HRESULT Debugger::LazyInitWrapper()
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(ThisMaybeHelperThread());
}
CONTRACTL_END;
HRESULT hr = S_OK;
// Do lazy initialization now.
EX_TRY
{
LazyInit(); // throws on errors.
}
EX_CATCH
{
Exception *_ex = GET_EXCEPTION();
hr = _ex->GetHR();
STRESS_LOG1(LF_CORDB, LL_ALWAYS, "LazyInit failed w/ hr:0x%08x\n", hr);
}
EX_END_CATCH(SwallowAllExceptions);
return hr;
}
void Debugger::LazyInit()
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
PRECONDITION(ThreadHoldsLock()); // ensure we're serialized, requires GC_NOTRIGGER
PRECONDITION(ThisMaybeHelperThread());
}
CONTRACTL_END;
// Have knob that catches places where we lazy init.
_ASSERTE(!g_DbgShouldntUseDebugger);
// If we're already init, then bail.
if (m_pLazyData != NULL)
{
return;
}
// Lazily create our heap.
HRESULT hr = m_heap.Init(FALSE);
IfFailThrow(hr);
hr = m_executableHeap.Init(TRUE);
IfFailThrow(hr);
m_pLazyData = new (interopsafe) DebuggerLazyInit();
_ASSERTE(m_pLazyData != NULL); // throws on oom.
m_pLazyData->Init();
}
HelperThreadFavor::HelperThreadFavor() :
m_fpFavor(NULL),
m_pFavorData(NULL),
m_FavorReadEvent(NULL),
m_FavorLock(CrstDebuggerFavorLock, CRST_DEFAULT),
m_FavorAvailableEvent(NULL)
{
}
void HelperThreadFavor::Init()
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
PRECONDITION(ThisMaybeHelperThread());
}
CONTRACTL_END;
// Create events for managing favors.
m_FavorReadEvent = CreateWin32EventOrThrow(NULL, kAutoResetEvent, FALSE);
m_FavorAvailableEvent = CreateWin32EventOrThrow(NULL, kAutoResetEvent, FALSE);
}
DebuggerLazyInit::DebuggerLazyInit() :
m_pPendingEvals(NULL),
// @TODO: a-meicht
// Major clean up needed for giving the right flag
// There are cases where DebuggerDataLock is taken by managed thread and unmanaged trhead is also trying to take it.
// It could cause deadlock if we toggle GC upon taking lock.
// Unfortunately UNSAFE_COOPGC is not enough. There is a code path in Jit comipling that we are in GC Preemptive
// enabled. workaround by orring the unsafe_anymode flag. But we really need to do proper clean up.
//
// NOTE: If this ever gets fixed, you should replace CALLED_IN_DEBUGGERDATALOCK_HOLDER_SCOPE_MAY_GC_TRIGGERS_CONTRACT
// with appropriate contracts at each site.
//
m_DebuggerDataLock(CrstDebuggerJitInfo, (CrstFlags)(CRST_UNSAFE_ANYMODE | CRST_REENTRANCY | CRST_DEBUGGER_THREAD)),
m_CtrlCMutex(NULL),
m_exAttachEvent(NULL),
m_exUnmanagedAttachEvent(NULL),
m_DebuggerHandlingCtrlC(NULL)
{
}
void DebuggerLazyInit::Init()
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
PRECONDITION(ThisMaybeHelperThread());
}
CONTRACTL_END;
// Caller ensures this isn't double-called.
// This event is only used in the unmanaged attach case. We must mark this event handle as inheritable.
// Otherwise, the unmanaged debugger won't be able to notify us.
//
// Note that PAL currently doesn't support specifying the security attributes when creating an event, so
// unmanaged attach for unhandled exceptions is broken on PAL.
SECURITY_ATTRIBUTES* pSA = NULL;
SECURITY_ATTRIBUTES secAttrib;
secAttrib.nLength = sizeof(secAttrib);
secAttrib.lpSecurityDescriptor = NULL;
secAttrib.bInheritHandle = TRUE;
pSA = &secAttrib;
// Create some synchronization events...
// these events stay signaled all the time except when an attach is in progress
m_exAttachEvent = CreateWin32EventOrThrow(NULL, kManualResetEvent, TRUE);
m_exUnmanagedAttachEvent = CreateWin32EventOrThrow(pSA, kManualResetEvent, TRUE);
m_CtrlCMutex = CreateWin32EventOrThrow(NULL, kAutoResetEvent, FALSE);
m_DebuggerHandlingCtrlC = FALSE;
// Let the helper thread lazy init stuff too.
m_RCThread.Init();
}
DebuggerLazyInit::~DebuggerLazyInit()
{
{
USHORT cBlobs = m_pMemBlobs.Count();
void **rgpBlobs = m_pMemBlobs.Table();
for (int i = 0; i < cBlobs; i++)
{
g_pDebugger->ReleaseRemoteBuffer(rgpBlobs[i], false);
}
}
if (m_pPendingEvals)
{
DeleteInteropSafe(m_pPendingEvals);
m_pPendingEvals = NULL;
}
if (m_CtrlCMutex != NULL)
{
CloseHandle(m_CtrlCMutex);
}
if (m_exAttachEvent != NULL)
{
CloseHandle(m_exAttachEvent);
}
if (m_exUnmanagedAttachEvent != NULL)
{
CloseHandle(m_exUnmanagedAttachEvent);
}
}
//
// RequestFavor gets the debugger helper thread to call a function. It's
// typically called when the current thread can't call the function directly,
// e.g, there isn't enough stack space.
//
// RequestFavor can be called in stack-overflow scenarios and thus explicitly
// avoids any lazy initialization.
// It blocks until the favor callback completes.
//
// Parameters:
// fp - a non-null Favour callback function
// pData - the parameter passed to the favor callback function. This can be any value.
//
// Return values:
// S_OK if the function succeeds, else a failure HRESULT
//
HRESULT Debugger::RequestFavor(FAVORCALLBACK fp, void * pData)
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_TRIGGERS;
PRECONDITION(fp != NULL);
}
CONTRACTL_END;
if (m_pRCThread == NULL ||
m_pRCThread->GetRCThreadId() == GetCurrentThreadId())
{
// Since favors are only used internally, we know that the helper should alway be up and ready
// to handle them. Also, since favors can be used in low-stack scenarios, there's not any
// extra initialization needed for them.
_ASSERTE(!"Helper not initialized for favors.");
return E_UNEXPECTED;
}
m_pRCThread->DoFavor(fp, pData);
return S_OK;
}
/******************************************************************************
// Called to set the interface that the Runtime exposes to us.
******************************************************************************/
void Debugger::SetEEInterface(EEDebugInterface* i)
{
LIMITED_METHOD_CONTRACT;
// @@@
// Implements DebugInterface API
g_pEEInterface = i;
}
/******************************************************************************
// Called to shut down the debugger. This stops the RC thread and cleans
// the object up.
******************************************************************************/
void Debugger::StopDebugger(void)
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Leak almost everything on process exit. The OS will clean it up anyways and trying to
// clean it up ourselves is just one more place we may AV / deadlock.
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
if (g_pDbgTransport != NULL)
{
g_pDbgTransport->Shutdown();
}
#endif // FEATURE_DBGIPC_TRANSPORT_VM
// Ping the helper thread to exit. This will also prevent the helper from servicing new requests.
if (m_pRCThread != NULL)
{
m_pRCThread->AsyncStop();
}
// Also clean up the AppDomain stuff since this is cross-process.
TerminateAppDomainIPC ();
//
// Tell the VM to clear out all references to the debugger before we start cleaning up,
// so that nothing will reference (accidentally) through the partially cleaned up debugger.
//
// NOTE: we cannot clear out g_pDebugger before the delete call because the
// stuff in delete (particularly deleteinteropsafe) needs to look at it.
//
g_pEEInterface->ClearAllDebugInterfaceReferences();
g_pDebugger = NULL;
}
/* ------------------------------------------------------------------------ *
* JIT Interface routines
* ------------------------------------------------------------------------ */
/******************************************************************************
*
******************************************************************************/
DebuggerMethodInfo *Debugger::CreateMethodInfo(Module *module, mdMethodDef md)
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
PRECONDITION(HasDebuggerDataLock());
}
CONTRACTL_END;
// <TODO>@todo perf: creating these on the heap is slow. We should use a
// pool and create them out of there since we never free them
// until the AD is unloaded.</TODO>
//
DebuggerMethodInfo *mi = new (interopsafe) DebuggerMethodInfo(module, md);
_ASSERTE(mi != NULL); // throws on oom error
TRACE_ALLOC(mi);
LOG((LF_CORDB, LL_INFO100000, "D::CreateMethodInfo module=%p, token=0x%08x, info=%p\n",
module, md, mi));
//
// Lock a mutex when changing the table.
//
//@TODO : _ASSERTE(EnC);
HRESULT hr;
hr =InsertToMethodInfoList(mi);
if (FAILED(hr))
{
LOG((LF_CORDB, LL_EVERYTHING, "IAHOL Failed!!\n"));
DeleteInteropSafe(mi);
return NULL;
}
return mi;
}
/******************************************************************************
// void Debugger::JITComplete(): JITComplete is called by
// the jit interface when the JIT completes, successfully or not.
//
// MethodDesc* fd: MethodDesc of the code that's been JITted
// BYTE* newAddress: The address of that the method begins at.
// If newAddress is NULL then the JIT failed. Remember that this
// gets called before the start address of the MethodDesc gets set,
// and so methods like GetFunctionAddress & GetFunctionSize won't work.
//
// <TODO>@Todo If we're passed 0 for the newAddress param, the jit has been
// cancelled & should be undone.</TODO>
******************************************************************************/
void Debugger::JITComplete(MethodDesc* fd, TADDR newAddress)
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
PRECONDITION(!HasDebuggerDataLock());
PRECONDITION(newAddress != NULL);
CALLED_IN_DEBUGGERDATALOCK_HOLDER_SCOPE_MAY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
#ifdef _TARGET_ARM_
newAddress = newAddress|THUMB_CODE;
#endif
// @@@
// Can be called on managed thread only
// This API Implements DebugInterface
if (CORDebuggerAttached())
{
// Populate the debugger's cache of DJIs. Normally we can do this lazily,
// the only reason we do it here is b/c the MethodDesc is not yet officially marked as "jitted",
// and so we can't lazily create it yet. Furthermore, the binding operations may need the DJIs.
//
// This also gives the debugger a chance to know if new JMC methods are coming.
DebuggerMethodInfo * dmi = GetOrCreateMethodInfo(fd->GetModule(), fd->GetMemberDef());
if (dmi == NULL)
{
goto Exit;
}
DebuggerJitInfo * ji = dmi->CreateInitAndAddJitInfo(fd, newAddress);
// Bind any IL patches to the newly jitted native code.
HRESULT hr;
hr = MapAndBindFunctionPatches(ji, fd, (CORDB_ADDRESS_TYPE *)newAddress);
_ASSERTE(SUCCEEDED(hr));
}
LOG((LF_CORDB, LL_EVERYTHING, "JitComplete completed successfully\n"));
Exit:
;
}
/******************************************************************************
// Get the number of fixed arguments to a function, i.e., the explicit args and the "this" pointer.
// This does not include other implicit arguments or varargs. This is used to compute a variable ID
// (see comment in CordbJITILFrame::ILVariableToNative for more detail)
// fVarArg is not used when this is called by Debugger::GetAndSendJITInfo, thus it has a default value.
// The return value is not used when this is called by Debugger::getVars.
******************************************************************************/
SIZE_T Debugger::GetArgCount(MethodDesc *fd,BOOL *fVarArg /* = NULL */)
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Create a MetaSig for the given method's sig. (Easier than
// picking the sig apart ourselves.)
PCCOR_SIGNATURE pCallSig;
DWORD cbCallSigSize;
fd->GetSig(&pCallSig, &cbCallSigSize);
if (pCallSig == NULL)
{
// Sig should only be null if the image is corrupted. (Even for lightweight-codegen)
// We expect the jit+verifier to catch this, so that we never land here.
// But just in case ...
CONSISTENCY_CHECK_MSGF(false, ("Corrupted image, null sig.(%s::%s)", fd->m_pszDebugClassName, fd->m_pszDebugMethodName));
return 0;
}
MetaSig msig(pCallSig, cbCallSigSize, g_pEEInterface->MethodDescGetModule(fd), NULL, MetaSig::sigMember);
// Get the arg count.
UINT32 NumArguments = msig.NumFixedArgs();
// Account for the 'this' argument.
if (!(g_pEEInterface->MethodDescIsStatic(fd)))
NumArguments++;
// Is this a VarArg's function?
if (msig.IsVarArg() && fVarArg != NULL)
{
*fVarArg = true;
}
return NumArguments;
}
#endif // #ifndef DACCESS_COMPILE
/******************************************************************************
DebuggerJitInfo * Debugger::GetJitInfo(): GetJitInfo
will return a pointer to a DebuggerJitInfo. If the DJI
doesn't exist, or it does exist, but the method has actually
been pitched (and the caller wants pitched methods filtered out),
then we'll return NULL.
Note: This will also create a DMI for if one does not exist for this DJI.
MethodDesc* fd: MethodDesc for the method we're interested in.
CORDB_ADDRESS_TYPE * pbAddr: Address within the code, to indicate which
version we want. If this is NULL, then we want the
head of the DebuggerJitInfo list, whether it's been
JITted or not.
******************************************************************************/
// Get a DJI from an address.
DebuggerJitInfo *Debugger::GetJitInfoFromAddr(TADDR addr)
{
WRAPPER_NO_CONTRACT;
MethodDesc *fd;
fd = g_pEEInterface->GetNativeCodeMethodDesc(addr);
_ASSERTE(fd);
return GetJitInfo(fd, (const BYTE*) addr, NULL);
}
// Get a DJI for a Native MD (MD for a native function).
// In the EnC scenario, the MethodDesc refers to the most recent method.
// This is very dangerous since there may be multiple versions alive at the same time.
// This will give back the wrong DJI if we're lookikng for a stale method desc.
// @todo - can a caller possibly use this correctly?
DebuggerJitInfo *Debugger::GetLatestJitInfoFromMethodDesc(MethodDesc * pMethodDesc)
{
WRAPPER_NO_CONTRACT;
_ASSERTE(pMethodDesc != NULL);
// We'd love to assert that we're jitted; but since this may be in the JitComplete
// callback path, we can't be sure.
return GetJitInfoWorker(pMethodDesc, NULL, NULL);
}
DebuggerJitInfo *Debugger::GetJitInfo(MethodDesc *fd, const BYTE *pbAddr, DebuggerMethodInfo **pMethInfo )
{
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_NOTRIGGER;
PRECONDITION(!g_pDebugger->HasDebuggerDataLock());
}
CONTRACTL_END;
// Address should be non-null and in range of MethodDesc. This lets us tell which EnC version.
_ASSERTE(pbAddr != NULL);
return GetJitInfoWorker(fd, pbAddr, pMethInfo);
}
// Internal worker to GetJitInfo. Doesn't validate parameters.
DebuggerJitInfo *Debugger::GetJitInfoWorker(MethodDesc *fd, const BYTE *pbAddr, DebuggerMethodInfo **pMethInfo)
{
DebuggerMethodInfo *dmi = NULL;
DebuggerJitInfo *dji = NULL;
// If we have a null MethodDesc - we're not going to get a jit-info. Do this check once at the top
// rather than littered throughout the rest of this function.
if (fd == NULL)
{
LOG((LF_CORDB, LL_EVERYTHING, "Debugger::GetJitInfo, addr=0x%p - null fd - returning null\n", pbAddr));
return NULL;
}
else
{
CONSISTENCY_CHECK_MSGF(!fd->IsWrapperStub(), ("Can't get Jit-info for wrapper MDesc,'%s'", fd->m_pszDebugMethodName));
}
// The debugger doesn't track Lightweight-codegen methods b/c they have no metadata.
if (fd->IsDynamicMethod())
{
return NULL;
}
// initialize our out param
if (pMethInfo)
{
*pMethInfo = NULL;
}
LOG((LF_CORDB, LL_EVERYTHING, "Debugger::GetJitInfo called\n"));
// CHECK_DJI_TABLE_DEBUGGER;
// Find the DJI via the DMI
//
// One way to improve the perf, both in terms of memory usage, number of allocations
// and lookup speeds would be to have the first JitInfo inline in the MethodInfo
// struct. After all, we never want to have a MethodInfo in the table without an
// associated JitInfo, and this should bring us back very close to the old situation
// in terms of perf. But correctness comes first, and perf later...
// CHECK_DMI_TABLE;
dmi = GetOrCreateMethodInfo(fd->GetModule(), fd->GetMemberDef());
if (dmi == NULL)
{
// If we can't create the DMI, we won't be able to create the DJI.
return NULL;
}
// This may take the lock and lazily create an entry, so we do it up front.
dji = dmi->GetLatestJitInfo(fd);
DebuggerDataLockHolder debuggerDataLockHolder(this);
// Note the call to GetLatestJitInfo() will lazily create the first DJI if we don't already have one.
for (; dji != NULL; dji = dji->m_prevJitInfo)
{
if (PTR_TO_TADDR(dji->m_fd) == PTR_HOST_TO_TADDR(fd))
{
break;
}
}
LOG((LF_CORDB, LL_INFO1000, "D::GJI: for md:0x%x (%s::%s), got dmi:0x%x.\n",
fd, fd->m_pszDebugClassName, fd->m_pszDebugMethodName,
dmi));
// Log stuff - fd may be null; so we don't want to AV in the log.
LOG((LF_CORDB, LL_INFO1000, "D::GJI: for md:0x%x (%s::%s), got dmi:0x%x, dji:0x%x, latest dji:0x%x, latest fd:0x%x, prev dji:0x%x\n",
fd, fd->m_pszDebugClassName, fd->m_pszDebugMethodName,
dmi, dji, (dmi ? dmi->GetLatestJitInfo_NoCreate() : 0),
((dmi && dmi->GetLatestJitInfo_NoCreate()) ? dmi->GetLatestJitInfo_NoCreate()->m_fd:0),
(dji?dji->m_prevJitInfo:0)));
if ((dji != NULL) && (pbAddr != NULL))
{
dji = dji->GetJitInfoByAddress(pbAddr);
// XXX Microsoft - dac doesn't support stub tracing
// so this just results in not-impl exceptions.
#ifndef DACCESS_COMPILE
if (dji == NULL) //may have been given address of a thunk
{
LOG((LF_CORDB,LL_INFO1000,"Couldn't find a DJI by address 0x%p, "
"so it might be a stub or thunk\n", pbAddr));
TraceDestination trace;
g_pEEInterface->TraceStub((const BYTE *)pbAddr, &trace);
if ((trace.GetTraceType() == TRACE_MANAGED) && (pbAddr != (const BYTE *)trace.GetAddress()))
{
LOG((LF_CORDB,LL_INFO1000,"Address thru thunk"
": 0x%p\n", trace.GetAddress()));
dji = GetJitInfo(fd, dac_cast<PTR_CBYTE>(trace.GetAddress()));
}
#ifdef LOGGING
else
{
_ASSERTE(trace.GetTraceType() != TRACE_UNJITTED_METHOD ||
(fd == trace.GetMethodDesc()));
LOG((LF_CORDB,LL_INFO1000,"Address not thunked - "
"must be to unJITted method, or normal managed "
"method lacking a DJI!\n"));
}
#endif //LOGGING
}
#endif // #ifndef DACCESS_COMPILE
}
if (pMethInfo)
{
*pMethInfo = dmi;
}
// DebuggerDataLockHolder out of scope - release implied
return dji;
}
DebuggerMethodInfo *Debugger::GetOrCreateMethodInfo(Module *pModule, mdMethodDef token)
{
CONTRACTL
{
SO_INTOLERANT;
SUPPORTS_DAC;
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
DebuggerMethodInfo *info = NULL;
// When dump debugging, we don't expect to have a lock,
// nor would it be useful for anything.
ALLOW_DATATARGET_MISSING_MEMORY(
// In case we don't have already, take it now.
DebuggerDataLockHolder debuggerDataLockHolder(this);
);
if (m_pMethodInfos != NULL)
{
info = m_pMethodInfos->GetMethodInfo(pModule, token);
}
// dac checks ngen'ed image content first, so
// if we didn't find information it doesn't exist.
#ifndef DACCESS_COMPILE
if (info == NULL)
{
info = CreateMethodInfo(pModule, token);
LOG((LF_CORDB, LL_INFO1000, "D::GOCMI: created DMI for mdToken:0x%x, dmi:0x%x\n",
token, info));
}
#endif // #ifndef DACCESS_COMPILE
if (info == NULL)
{
// This should only happen in an oom scenario. It would be nice to throw here.
STRESS_LOG2(LF_CORDB, LL_EVERYTHING, "OOM - Failed to allocate DJI (0x%p, 0x%x)\n", pModule, token);
}
// DebuggerDataLockHolder out of scope - release implied
return info;
}
#ifndef DACCESS_COMPILE
/******************************************************************************
* GetILToNativeMapping returns a map from IL offsets to native
* offsets for this code. An array of COR_PROF_IL_TO_NATIVE_MAP
* structs will be returned, and some of the ilOffsets in this array
* may be the values specified in CorDebugIlToNativeMappingTypes.
******************************************************************************/
HRESULT Debugger::GetILToNativeMapping(MethodDesc *pMD, ULONG32 cMap,
ULONG32 *pcMap, COR_DEBUG_IL_TO_NATIVE_MAP map[])
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
}
CONTRACTL_END;
#ifdef PROFILING_SUPPORTED
// At this point, we're pulling in the debugger.
if (!HasLazyData())
{
DebuggerLockHolder lockHolder(this);
LazyInit(); // throws
}
// Get the JIT info by functionId.
// This function is unsafe to use during EnC because the MethodDesc doesn't tell
// us which version is being requested.
// However, this function is only used by the profiler, and you can't profile with EnC,
// which means that getting the latest jit-info is still correct.
#if defined(PROFILING_SUPPORTED)
_ASSERTE(CORProfilerPresent());
#endif // PROFILING_SUPPORTED
DebuggerJitInfo *pDJI = GetLatestJitInfoFromMethodDesc(pMD);
// Dunno what went wrong
if (pDJI == NULL)
return (E_FAIL);
// If they gave us space to copy into...
if (map != NULL)
{
// Only copy as much as either they gave us or we have to copy.
ULONG32 cpyCount = min(cMap, pDJI->GetSequenceMapCount());
// Read the map right out of the Left Side.
if (cpyCount > 0)
ExportILToNativeMap(cpyCount,
map,
pDJI->GetSequenceMap(),
pDJI->m_sizeOfCode);
}
// Return the true count of entries
if (pcMap)
{
*pcMap = pDJI->GetSequenceMapCount();
}
return (S_OK);
#else
return E_NOTIMPL;
#endif
}
//---------------------------------------------------------------------------------------
//
// This is morally the same as GetILToNativeMapping, except the output is in a different
// format, to better facilitate sending the ETW ILToNativeMap events.
//
// Arguments:
// pMD - MethodDesc whose IL-to-native map will be returned
// cMapMax - Max number of map entries to return. Although
// this function handles the allocation of the returned
// array, the caller still wants to limit how big this
// can get, since ETW itself has limits on how big
// events can get
// pcMap - [out] Number of entries returned in each output parallel array (next
// two parameters).
// prguiILOffset - [out] Array of IL offsets. This function allocates, caller must free.
// prguiNativeOffset - [out] Array of the starting native offsets that correspond
// to each (*prguiILOffset)[i]. This function allocates,
// caller must free.
//
// Return Value:
// HRESULT indicating success or failure.
//
// Notes:
// * This function assumes lazy data has already been initialized (in order to
// ensure that this doesn't trigger or take the large debugger mutex). So
// callers must guarantee they call InitializeLazyDataIfNecessary() first.
// * Either this function fails, and (*prguiILOffset) & (*prguiNativeOffset) will be
// untouched OR this function succeeds and (*prguiILOffset) & (*prguiNativeOffset)
// will both be non-NULL, set to the parallel arrays this function allocated.
// * If this function returns success, then the caller must free (*prguiILOffset) and
// (*prguiNativeOffset)
// * (*prguiILOffset) and (*prguiNativeOffset) are parallel arrays, such that
// (*prguiILOffset)[i] corresponds to (*prguiNativeOffset)[i] for each 0 <= i < *pcMap
// * If EnC is enabled, this function will return the IL-to-native mapping for the latest
// EnC version of the function. This may not be what the profiler wants, but EnC
// + ETW-map events is not a typical combination, and this is consistent with
// other ETW events like JittingStarted or MethodLoad, which also fire multiple
// events for the same MethodDesc (each time it's EnC'd), with each event
// corresponding to the most recent EnC version at the time.
//
HRESULT Debugger::GetILToNativeMappingIntoArrays(
MethodDesc * pMD,
USHORT cMapMax,
USHORT * pcMap,
UINT ** prguiILOffset,
UINT ** prguiNativeOffset)
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
_ASSERTE(pcMap != NULL);
_ASSERTE(prguiILOffset != NULL);
_ASSERTE(prguiNativeOffset != NULL);
// Any caller of GetILToNativeMappingIntoArrays had better call
// InitializeLazyDataIfNecessary first!
_ASSERTE(HasLazyData());
// Get the JIT info by functionId.
DebuggerJitInfo * pDJI = GetLatestJitInfoFromMethodDesc(pMD);
// Dunno what went wrong
if (pDJI == NULL)
return E_FAIL;
ULONG32 cMap = min(cMapMax, pDJI->GetSequenceMapCount());
DebuggerILToNativeMap * rgMapInt = pDJI->GetSequenceMap();
NewArrayHolder<UINT> rguiILOffsetTemp = new (nothrow) UINT[cMap];
if (rguiILOffsetTemp == NULL)
return E_OUTOFMEMORY;
NewArrayHolder<UINT> rguiNativeOffsetTemp = new (nothrow) UINT[cMap];
if (rguiNativeOffsetTemp == NULL)
return E_OUTOFMEMORY;
for (ULONG32 iMap=0; iMap < cMap; iMap++)
{
rguiILOffsetTemp[iMap] = rgMapInt[iMap].ilOffset;
rguiNativeOffsetTemp[iMap] = rgMapInt[iMap].nativeStartOffset;
}
// Since cMap is the min of cMapMax (and something else) and cMapMax is a USHORT,
// then cMap must fit in a USHORT as well
_ASSERTE(FitsIn<USHORT>(cMap));
*pcMap = (USHORT) cMap;
*prguiILOffset = rguiILOffsetTemp.Extract();
*prguiNativeOffset = rguiNativeOffsetTemp.Extract();
return S_OK;
}
#endif // #ifndef DACCESS_COMPILE
/******************************************************************************
*
******************************************************************************/
CodeRegionInfo CodeRegionInfo::GetCodeRegionInfo(DebuggerJitInfo *dji, MethodDesc *md, PTR_CORDB_ADDRESS_TYPE addr)
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
SUPPORTS_DAC;
MODE_ANY;
}
CONTRACTL_END;
if (dji && dji->m_addrOfCode)
{
LOG((LF_CORDB, LL_EVERYTHING, "CRI::GCRI: simple case\n"));
return dji->m_codeRegionInfo;
}
else
{
LOG((LF_CORDB, LL_EVERYTHING, "CRI::GCRI: more complex case\n"));
CodeRegionInfo codeRegionInfo;
// Use method desc from dji if present
if (dji && dji->m_fd)
{
_ASSERTE(!md || md == dji->m_fd);
md = dji->m_fd;
}
if (!addr)
{
_ASSERTE(md);
addr = dac_cast<PTR_CORDB_ADDRESS_TYPE>(g_pEEInterface->GetFunctionAddress(md));
}
else
{
_ASSERTE(!md ||
(addr == dac_cast<PTR_CORDB_ADDRESS_TYPE>(g_pEEInterface->GetFunctionAddress(md))));
}
if (addr)
{
PCODE pCode = (PCODE)dac_cast<TADDR>(addr);
#ifdef _TARGET_ARM_
pCode |= THUMB_CODE;
#endif
codeRegionInfo.InitializeFromStartAddress(pCode);
}
return codeRegionInfo;
}
}
#ifndef DACCESS_COMPILE
/******************************************************************************
// Helper function for getBoundaries to get around AMD64 compiler and
// contract holders with PAL_TRY in the same function.
******************************************************************************/
void Debugger::getBoundariesHelper(MethodDesc * md,
unsigned int *cILOffsets,
DWORD **pILOffsets)
{
//
// CANNOT ADD A CONTRACT HERE. Contract is in getBoundaries
//
//
// Grab the JIT info struct for this method. Create if needed, as this
// may be called before JITComplete.
//
DebuggerMethodInfo *dmi = NULL;
dmi = GetOrCreateMethodInfo(md->GetModule(), md->GetMemberDef());
if (dmi != NULL)
{
LOG((LF_CORDB,LL_INFO10000,"De::NGB: Got dmi 0x%x\n",dmi));
#if defined(FEATURE_ISYM_READER)
// Note: we need to make sure to enable preemptive GC here just in case we block in the symbol reader.
GCX_PREEMP_EEINTERFACE();
Module *pModule = md->GetModule();
(void)pModule; //prevent "unused variable" error from GCC
_ASSERTE(pModule != NULL);
SafeComHolder<ISymUnmanagedReader> pReader(pModule->GetISymUnmanagedReader());
// If we got a reader, use it.
if (pReader != NULL)
{
// Grab the sym reader's method.
ISymUnmanagedMethod *pISymMethod;
HRESULT hr = pReader->GetMethod(md->GetMemberDef(),
&pISymMethod);
ULONG32 n = 0;
if (SUCCEEDED(hr))
{
// Get the count of sequence points.
hr = pISymMethod->GetSequencePointCount(&n);
_ASSERTE(SUCCEEDED(hr));
LOG((LF_CORDB, LL_INFO100000,
"D::NGB: Reader seq pt count is %d\n", n));
ULONG32 *p;
if (n > 0)
{
ULONG32 dummy;
p = new ULONG32[n];
_ASSERTE(p != NULL); // throws on oom errror
hr = pISymMethod->GetSequencePoints(n, &dummy,
p, NULL, NULL, NULL,
NULL, NULL);
_ASSERTE(SUCCEEDED(hr));
_ASSERTE(dummy == n);
*pILOffsets = (DWORD*)p;
// Translate the IL offets based on an
// instrumented IL map if one exists.
if (dmi->HasInstrumentedILMap())
{
InstrumentedILOffsetMapping mapping =
dmi->GetRuntimeModule()->GetInstrumentedILOffsetMapping(dmi->m_token);
for (SIZE_T i = 0; i < n; i++)
{
int origOffset = *p;
*p = dmi->TranslateToInstIL(
&mapping,
origOffset,
bOriginalToInstrumented);
LOG((LF_CORDB, LL_INFO100000,
"D::NGB: 0x%04x (Real IL:0x%x)\n",
origOffset, *p));
p++;
}
}
#ifdef LOGGING
else
{
for (SIZE_T i = 0; i < n; i++)
{
LOG((LF_CORDB, LL_INFO100000,
"D::NGB: 0x%04x \n", *p));
p++;
}
}
#endif
}
else
*pILOffsets = NULL;
pISymMethod->Release();
}
else
{
*pILOffsets = NULL;
LOG((LF_CORDB, LL_INFO10000,
"De::NGB: failed to find method 0x%x in sym reader.\n",
md->GetMemberDef()));
}
*cILOffsets = n;
}
else
{
LOG((LF_CORDB, LL_INFO100000, "D::NGB: no reader.\n"));
}
#else // FEATURE_ISYM_READER
// We don't have ISymUnmanagedReader. Pretend there are no sequence points.
*cILOffsets = 0;
#endif // FEATURE_ISYM_READER
}
LOG((LF_CORDB, LL_INFO100000, "D::NGB: cILOffsets=%d\n", *cILOffsets));
return;
}
#endif
/******************************************************************************
// Use an ISymUnmanagedReader to get method sequence points.
******************************************************************************/
void Debugger::getBoundaries(MethodDesc * md,
unsigned int *cILOffsets,
DWORD **pILOffsets,
ICorDebugInfo::BoundaryTypes *implicitBoundaries)
{
#ifndef DACCESS_COMPILE
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
// May be here even when a debugger is not attached.
// @@@
// Implements DebugInterface API
*cILOffsets = 0;
*pILOffsets = NULL;
*implicitBoundaries = ICorDebugInfo::DEFAULT_BOUNDARIES;
// If there has been an unrecoverable Left Side error, then we
// just pretend that there are no boundaries.
if (CORDBUnrecoverableError(this))
{
return;
}
// LCG methods have their own resolution scope that is seperate from a module
// so they shouldn't have their symbols looked up in the module PDB. Right now
// LCG methods have no symbols so we can just early out, but if they ever
// had some symbols attached we would need a different way of getting to them.
// See Dev10 issue 728519
if(md->IsLCGMethod())
{
return;
}
// If JIT optimizations are allowed for the module this function
// lives in, then don't grab specific boundaries from the symbol
// store since any boundaries we give the JIT will be pretty much
// ignored anyway.
if (!CORDisableJITOptimizations(md->GetModule()->GetDebuggerInfoBits()))
{
*implicitBoundaries = ICorDebugInfo::BoundaryTypes(ICorDebugInfo::STACK_EMPTY_BOUNDARIES |
ICorDebugInfo::CALL_SITE_BOUNDARIES);
return;
}
Module* pModule = md->GetModule();
DWORD dwBits = pModule->GetDebuggerInfoBits();
if ((dwBits & DACF_IGNORE_PDBS) != 0)
{
//
// If told to explicitly ignore PDBs for this function, then bail now.
//
return;
}
if( !pModule->IsSymbolReadingEnabled() )
{
// Symbol reading is disabled for this module, so bail out early (for efficiency only)
return;
}
if (pModule == SystemDomain::SystemModule())
{
// We don't look up PDBs for mscorlib. This is not quite right, but avoids
// a bootstrapping problem. When an EXE loads, it has the option of setting
// the COM appartment model to STA if we need to. It is important that no
// other Coinitialize happens before this. Since loading the PDB reader uses
// com we can not come first. However managed code IS run before the COM
// appartment model is set, and thus we have a problem since this code is
// called for when JITTing managed code. We avoid the problem by just
// bailing for mscorlib.
return;
}
// At this point, we're pulling in the debugger.
if (!HasLazyData())
{
DebuggerLockHolder lockHolder(this);
LazyInit(); // throws
}
getBoundariesHelper(md, cILOffsets, pILOffsets);
#else
DacNotImpl();
#endif // #ifndef DACCESS_COMPILE
}
/******************************************************************************
*
******************************************************************************/
void Debugger::getVars(MethodDesc * md, ULONG32 *cVars, ICorDebugInfo::ILVarInfo **vars,
bool *extendOthers)
{
#ifndef DACCESS_COMPILE
CONTRACTL
{
SO_INTOLERANT;
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
PRECONDITION(!ThisIsHelperThreadWorker());
}
CONTRACTL_END;
// At worst return no information
*cVars = 0;
*vars = NULL;
// Just tell the JIT to extend everything.
// Note that if optimizations are enabled, the native compilers are
// free to ingore *extendOthers
*extendOthers = true;
DWORD bits = md->GetModule()->GetDebuggerInfoBits();
if (CORDBUnrecoverableError(this))
goto Exit;
if (CORDisableJITOptimizations(bits))
// if (!CORDebuggerAllowJITOpts(bits))
{
//
// @TODO: Do we really need this code since *extendOthers==true?
//
// Is this a vararg function?
BOOL fVarArg = false;
GetArgCount(md, &fVarArg);
if (fVarArg)
{
COR_ILMETHOD *ilMethod = g_pEEInterface->MethodDescGetILHeader(md);
if (ilMethod)
{
// It is, so we need to tell the JIT to give us the
// varags handle.
ICorDebugInfo::ILVarInfo *p = new ICorDebugInfo::ILVarInfo[1];
_ASSERTE(p != NULL); // throws on oom error
COR_ILMETHOD_DECODER header(ilMethod);
unsigned int ilCodeSize = header.GetCodeSize();
p->startOffset = 0;
p->endOffset = ilCodeSize;
p->varNumber = (DWORD) ICorDebugInfo::VARARGS_HND_ILNUM;
*cVars = 1;
*vars = p;
}
}
}
LOG((LF_CORDB, LL_INFO100000, "D::gV: cVars=%d, extendOthers=%d\n",
*cVars, *extendOthers));
Exit:
;
#else
DacNotImpl();
#endif // #ifndef DACCESS_COMPILE
}
#ifndef DACCESS_COMPILE
// If we have a varargs function, we can't set the IP (we don't know how to pack/unpack the arguments), so if we
// call SetIP with fCanSetIPOnly = true, we need to check for that.
// Arguments:
// input: nEntries - number of entries in varNativeInfo
// varNativeInfo - array of entries describing the args and locals for the function
// output: true iff the function has varargs
BOOL Debugger::IsVarArgsFunction(unsigned int nEntries, PTR_NativeVarInfo varNativeInfo)
{
for (unsigned int i = 0; i < nEntries; ++i)
{
if (varNativeInfo[i].loc.vlType == ICorDebugInfo::VLT_FIXED_VA)
{
return TRUE;
}
}
return FALSE;
}
// We want to keep the 'worst' HRESULT - if one has failed (..._E_...) & the
// other hasn't, take the failing one. If they've both/neither failed, then
// it doesn't matter which we take.
// Note that this macro favors retaining the first argument
#define WORST_HR(hr1,hr2) (FAILED(hr1)?hr1:hr2)
/******************************************************************************
*
******************************************************************************/
HRESULT Debugger::SetIP( bool fCanSetIPOnly, Thread *thread,Module *module,
mdMethodDef mdMeth, DebuggerJitInfo* dji,
SIZE_T offsetILTo, BOOL fIsIL)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(thread));
PRECONDITION(CheckPointer(module));
PRECONDITION(mdMeth != mdMethodDefNil);
}
CONTRACTL_END;
#ifdef _DEBUG
static ConfigDWORD breakOnSetIP;
if (breakOnSetIP.val(CLRConfig::INTERNAL_DbgBreakOnSetIP)) _ASSERTE(!"DbgBreakOnSetIP");
#endif
HRESULT hr = S_OK;
HRESULT hrAdvise = S_OK;
DWORD offsetILFrom;
CorDebugMappingResult map;
DWORD whichIgnore;
ControllerStackInfo csi;
BOOL exact;
SIZE_T offsetNatTo;
PCODE pbDest = NULL;
BYTE *pbBase = NULL;
CONTEXT *pCtx = NULL;
DWORD dwSize = 0;
SIZE_T *rgVal1 = NULL;
SIZE_T *rgVal2 = NULL;
BYTE **pVCs = NULL;
LOG((LF_CORDB, LL_INFO1000, "D::SIP: In SetIP ==> fCanSetIPOnly:0x%x <==!\n", fCanSetIPOnly));
if (ReJitManager::IsReJITEnabled())
{
return CORDBG_E_SET_IP_IMPOSSIBLE;
}
pCtx = GetManagedStoppedCtx(thread);
// If we can't get a context, then we can't possibly be a in a good place
// to do a setip.
if (pCtx == NULL)
{
return CORDBG_S_BAD_START_SEQUENCE_POINT;
}
// Implicit Caveat: We need to be the active frame.
// We can safely take a stack trace because the thread is synchronized.
StackTraceTicket ticket(thread);
csi.GetStackInfo(ticket, thread, LEAF_MOST_FRAME, NULL);
ULONG offsetNatFrom = csi.m_activeFrame.relOffset;
#if defined(WIN64EXCEPTIONS)
if (csi.m_activeFrame.IsFuncletFrame())
{
offsetNatFrom = (ULONG)((SIZE_T)GetControlPC(&(csi.m_activeFrame.registers)) -
(SIZE_T)(dji->m_addrOfCode));
}
#endif // WIN64EXCEPTIONS
_ASSERTE(dji != NULL);
// On WIN64 platforms, it's important to use the total size of the
// parent method and the funclets below (i.e. m_sizeOfCode). Don't use
// the size of the individual funclets or the parent method.
pbBase = (BYTE*)CORDB_ADDRESS_TO_PTR(dji->m_addrOfCode);
dwSize = (DWORD)dji->m_sizeOfCode;
#if defined(WIN64EXCEPTIONS)
// Currently, method offsets are not bigger than 4 bytes even on WIN64.
// Assert that it is so here.
_ASSERTE((SIZE_T)dwSize == dji->m_sizeOfCode);
#endif // WIN64EXCEPTIONS
// Create our structure for analyzing this.
// <TODO>@PERF: optimize - hold on to this so we don't rebuild it for both
// CanSetIP & SetIP.</TODO>
int cFunclet = 0;
const DWORD * rgFunclet = NULL;
#if defined(WIN64EXCEPTIONS)
cFunclet = dji->GetFuncletCount();
rgFunclet = dji->m_rgFunclet;
#endif // WIN64EXCEPTIONS
EHRangeTree* pEHRT = new (nothrow) EHRangeTree(csi.m_activeFrame.pIJM,
csi.m_activeFrame.MethodToken,
dwSize,
cFunclet,
rgFunclet);
// To maintain the current semantics, we will check the following right before SetIPFromSrcToDst() is called
// (instead of checking them now):
// 1) pEHRT == NULL
// 2) FAILED(pEHRT->m_hrInit)
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Got version info fine\n"));
// Caveat: we need to start from a sequence point
offsetILFrom = dji->MapNativeOffsetToIL(offsetNatFrom,
&map, &whichIgnore);
if ( !(map & MAPPING_EXACT) )
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Starting native offset is bad!\n"));
hrAdvise = WORST_HR(hrAdvise, CORDBG_S_BAD_START_SEQUENCE_POINT);
}
else
{ // exact IL mapping
if (!(dji->GetSrcTypeFromILOffset(offsetILFrom) & ICorDebugInfo::STACK_EMPTY))
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Starting offset isn't stack empty!\n"));
hrAdvise = WORST_HR(hrAdvise, CORDBG_S_BAD_START_SEQUENCE_POINT);
}
}
// Caveat: we need to go to a sequence point
if (fIsIL )
{
#if defined(WIN64EXCEPTIONS)
int funcletIndexFrom = dji->GetFuncletIndex((CORDB_ADDRESS)offsetNatFrom, DebuggerJitInfo::GFIM_BYOFFSET);
offsetNatTo = dji->MapILOffsetToNativeForSetIP(offsetILTo, funcletIndexFrom, pEHRT, &exact);
#else // WIN64EXCEPTIONS
DebuggerJitInfo::ILToNativeOffsetIterator it;
dji->InitILToNativeOffsetIterator(it, offsetILTo);
offsetNatTo = it.CurrentAssertOnlyOne(&exact);
#endif // WIN64EXCEPTIONS
if (!exact)
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Dest (via IL offset) is bad!\n"));
hrAdvise = WORST_HR(hrAdvise, CORDBG_S_BAD_END_SEQUENCE_POINT);
}
}
else
{
offsetNatTo = offsetILTo;
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Dest of 0x%p (via native "
"offset) is fine!\n", offsetNatTo));
}
CorDebugMappingResult mapping;
DWORD which;
offsetILTo = dji->MapNativeOffsetToIL(offsetNatTo, &mapping, &which);
// We only want to perhaps return CORDBG_S_BAD_END_SEQUENCE_POINT if
// we're not already returning CORDBG_S_BAD_START_SEQUENCE_POINT.
if (hr != CORDBG_S_BAD_START_SEQUENCE_POINT)
{
if ( !(mapping & MAPPING_EXACT) )
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Ending native offset is bad!\n"));
hrAdvise = WORST_HR(hrAdvise, CORDBG_S_BAD_END_SEQUENCE_POINT);
}
else
{
// <NOTE WIN64>
// All duplicate sequence points (ones with the same IL offset) should have the same SourceTypes.
// </NOTE WIN64>
if (!(dji->GetSrcTypeFromILOffset(offsetILTo) & ICorDebugInfo::STACK_EMPTY))
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Ending offset isn't a sequence"
" point, or not stack empty!\n"));
hrAdvise = WORST_HR(hrAdvise, CORDBG_S_BAD_END_SEQUENCE_POINT);
}
}
}
// Once we finally have a native offset, it had better be in range.
if (offsetNatTo >= dwSize)
{
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Code out of range! offsetNatTo = 0x%x, dwSize=0x%x\n", offsetNatTo, dwSize));
hrAdvise = E_INVALIDARG;
goto LExit;
}
pbDest = CodeRegionInfo::GetCodeRegionInfo(dji).OffsetToAddress(offsetNatTo);
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Dest is 0x%p\n", pbDest));
// Don't allow SetIP if the source or target is cold (SetIPFromSrcToDst does not
// correctly handle this case).
if (!CodeRegionInfo::GetCodeRegionInfo(dji).IsOffsetHot(offsetNatTo) ||
!CodeRegionInfo::GetCodeRegionInfo(dji).IsOffsetHot(offsetNatFrom))
{
hrAdvise = WORST_HR(hrAdvise, CORDBG_E_SET_IP_IMPOSSIBLE);
goto LExit;
}
}
if (!fCanSetIPOnly)
{
hr = ShuffleVariablesGet(dji,
offsetNatFrom,
pCtx,
&rgVal1,
&rgVal2,
&pVCs);
LOG((LF_CORDB|LF_ENC,
LL_INFO10000,
"D::SIP: rgVal1 0x%X, rgVal2 0x%X\n",
rgVal1,
rgVal2));
if (FAILED(hr))
{
// This will only fail fatally, so exit.
hrAdvise = WORST_HR(hrAdvise, hr);
goto LExit;
}
}
else // fCanSetIPOnly
{
if (IsVarArgsFunction(dji->GetVarNativeInfoCount(), dji->GetVarNativeInfo()))
{
hrAdvise = E_INVALIDARG;
goto LExit;
}
}
if (pEHRT == NULL)
{
hr = E_OUTOFMEMORY;
}
else if (FAILED(pEHRT->m_hrInit))
{
hr = pEHRT->m_hrInit;
}
else
{
//
// This is a known, ok, violation. END_EXCEPTION_GLUE has a call to GetThrowable in it, but
// we will never hit it because we are passing in NULL below. This is to satisfy the static
// contract analyzer.
//
CONTRACT_VIOLATION(GCViolation);
EX_TRY
{
hr =g_pEEInterface->SetIPFromSrcToDst(thread,
pbBase,
offsetNatFrom,
(DWORD)offsetNatTo,
fCanSetIPOnly,
&(csi.m_activeFrame.registers),
pCtx,
(void *)dji,
pEHRT);
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions);
}
// Get the return code, if any
if (hr != S_OK)
{
hrAdvise = WORST_HR(hrAdvise, hr);
goto LExit;
}
// If we really want to do this, we'll have to put the
// variables into their new locations.
if (!fCanSetIPOnly && !FAILED(hrAdvise))
{
// TODO: We should zero out any registers which have now become live GC roots,
// but which aren't tracked variables (i.e. they are JIT temporaries). Such registers may
// have garbage left over in them, and we don't want the GC to try and dereference them
// as object references. However, we can't easily tell here which of the callee-saved regs
// are used in this method and therefore safe to clear.
//
hr = ShuffleVariablesSet(dji,
offsetNatTo,
pCtx,
&rgVal1,
&rgVal2,
pVCs);
if (hr != S_OK)
{
hrAdvise = WORST_HR(hrAdvise, hr);
goto LExit;
}
_ASSERTE(pbDest != NULL);
::SetIP(pCtx, pbDest);
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Set IP to be 0x%p\n", GetIP(pCtx)));
}
LExit:
if (rgVal1 != NULL)
{
DeleteInteropSafe(rgVal1);
}
if (rgVal2 != NULL)
{
DeleteInteropSafe(rgVal2);
}
#if defined(_TARGET_X86_) || defined(_WIN64) || defined(_TARGET_ARM_)
if (pEHRT != NULL)
{
delete pEHRT;
}
#endif // _TARGET_X86_ || _WIN64
LOG((LF_CORDB, LL_INFO1000, "D::SIP:Returning 0x%x\n", hr));
return hrAdvise;
}
#include "nativevaraccessors.h"
/******************************************************************************
*
******************************************************************************/
HRESULT Debugger::ShuffleVariablesGet(DebuggerJitInfo *dji,
SIZE_T offsetFrom,
CONTEXT *pCtx,
SIZE_T **prgVal1,
SIZE_T **prgVal2,
BYTE ***prgpVCs)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(dji));
PRECONDITION(CheckPointer(pCtx));
PRECONDITION(CheckPointer(prgVal1));
PRECONDITION(CheckPointer(prgVal2));
PRECONDITION(dji->m_sizeOfCode >= offsetFrom);
}
CONTRACTL_END;
LONG cVariables = 0;
DWORD i;
//
// Find the largest variable number
//
for (i = 0; i < dji->GetVarNativeInfoCount(); i++)
{
if ((LONG)(dji->GetVarNativeInfo()[i].varNumber) > cVariables)
{
cVariables = (LONG)(dji->GetVarNativeInfo()[i].varNumber);
}
}
HRESULT hr = S_OK;
//
// cVariables is a zero-based count of the number of variables. Increment it.
//
cVariables++;
SIZE_T *rgVal1 = new (interopsafe, nothrow) SIZE_T[cVariables + unsigned(-ICorDebugInfo::UNKNOWN_ILNUM)];
SIZE_T *rgVal2 = NULL;
if (rgVal1 == NULL)
{
hr = E_OUTOFMEMORY;
goto LExit;
}
rgVal2 = new (interopsafe, nothrow) SIZE_T[cVariables + unsigned(-ICorDebugInfo::UNKNOWN_ILNUM)];
if (rgVal2 == NULL)
{
hr = E_OUTOFMEMORY;
goto LExit;
}
memset(rgVal1, 0, sizeof(SIZE_T) * (cVariables + unsigned(-ICorDebugInfo::UNKNOWN_ILNUM)));
memset(rgVal2, 0, sizeof(SIZE_T) * (cVariables + unsigned(-ICorDebugInfo::UNKNOWN_ILNUM)));
LOG((LF_CORDB|LF_ENC,
LL_INFO10000,
"D::SVG cVariables %d, hiddens %d, rgVal1 0x%X, rgVal2 0x%X\n",
cVariables,
unsigned(-ICorDebugInfo::UNKNOWN_ILNUM),
rgVal1,
rgVal2));
GetVariablesFromOffset(dji->m_fd,
dji->GetVarNativeInfoCount(),
dji->GetVarNativeInfo(),
offsetFrom,
pCtx,
rgVal1,
rgVal2,
cVariables + unsigned(-ICorDebugInfo::UNKNOWN_ILNUM),
prgpVCs);
LExit:
if (!FAILED(hr))
{
(*prgVal1) = rgVal1;
(*prgVal2) = rgVal2;
}
else
{
LOG((LF_CORDB, LL_INFO100, "D::SVG: something went wrong hr=0x%x!", hr));
(*prgVal1) = NULL;
(*prgVal2) = NULL;
if (rgVal1 != NULL)
delete[] rgVal1;
if (rgVal2 != NULL)
delete[] rgVal2;
}
return hr;
}
/******************************************************************************
*
******************************************************************************/
HRESULT Debugger::ShuffleVariablesSet(DebuggerJitInfo *dji,
SIZE_T offsetTo,
CONTEXT *pCtx,
SIZE_T **prgVal1,
SIZE_T **prgVal2,
BYTE **rgpVCs)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(dji));
PRECONDITION(CheckPointer(pCtx));
PRECONDITION(CheckPointer(prgVal1));
PRECONDITION(CheckPointer(prgVal2));
PRECONDITION(dji->m_sizeOfCode >= offsetTo);
}
CONTRACTL_END;
LOG((LF_CORDB|LF_ENC,
LL_INFO10000,
"D::SVS: rgVal1 0x%X, rgVal2 0x%X\n",
(*prgVal1),
(*prgVal2)));
HRESULT hr = SetVariablesAtOffset(dji->m_fd,
dji->GetVarNativeInfoCount(),
dji->GetVarNativeInfo(),
offsetTo,
pCtx,
*prgVal1,
*prgVal2,
rgpVCs);
LOG((LF_CORDB|LF_ENC,
LL_INFO100000,
"D::SVS deleting rgVal1 0x%X, rgVal2 0x%X\n",
(*prgVal1),
(*prgVal2)));
DeleteInteropSafe(*prgVal1);
(*prgVal1) = NULL;
DeleteInteropSafe(*prgVal2);
(*prgVal2) = NULL;
return hr;
}
//
// This class is used by Get and SetVariablesFromOffsets to manage a frameHelper
// list for the arguments and locals corresponding to each varNativeInfo. The first
// four are hidden args, but the remainder will all have a corresponding entry
// in the argument or local signature list.
//
// The structure of the array varNativeInfo contains home information for each variable
// at various points in the function. Thus, you have to search for the proper native offset
// (IP) in the varNativeInfo, and then find the correct varNumber in that native offset to
// find the correct home information.
//
// Important to note is that the JIT has hidden args that have varNumbers that are negative.
// Thus we cannot use varNumber as a strict index into our holder arrays, and instead shift
// indexes before indexing into our holder arrays.
//
// The hidden args are a fixed-sized array given by the value of 0-UNKNOWN_ILNUM. These are used
// to pass cookies about the arguments (var args, generics, retarg buffer etc.) to the function.
// The real arguments and locals are as one would expect.
//
class GetSetFrameHelper
{
public:
GetSetFrameHelper();
~GetSetFrameHelper();
HRESULT Init(MethodDesc* pMD);
bool GetValueClassSizeOfVar(int varNum, ICorDebugInfo::VarLocType varType, SIZE_T* pSize);
int ShiftIndexForHiddens(int varNum);
private:
MethodDesc* m_pMD;
SIZE_T* m_rgSize;
CorElementType* m_rgElemType;
ULONG m_numArgs;
ULONG m_numTotalVars;
SIZE_T GetValueClassSize(MetaSig* pSig);
static SIZE_T GetSizeOfElement(CorElementType cet);
};
//
// GetSetFrameHelper::GetSetFrameHelper()
//
// This is the constructor. It just initailizes all member variables.
//
// parameters: none
//
// return value: none
//
GetSetFrameHelper::GetSetFrameHelper() : m_pMD(NULL), m_rgSize(NULL), m_rgElemType(NULL),
m_numArgs(0), m_numTotalVars(0)
{
LIMITED_METHOD_CONTRACT;
}
//
// GetSetFrameHelper::Init()
//
// This method extracts the element type and the size of the arguments and locals of the method we are doing
// the SetIP on and stores this information in instance variables.
//
// parameters: pMD - MethodDesc of the method we are doing the SetIP on
//
// return value: S_OK or E_OUTOFMEMORY
//
HRESULT
GetSetFrameHelper::Init(MethodDesc *pMD)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(CheckPointer(pMD));
}
CONTRACTL_END;
HRESULT hr = S_OK;
COR_ILMETHOD* pILHeader = NULL;
m_pMD = pMD;
MetaSig *pLocSig = NULL;
MetaSig *pArgSig = NULL;
m_rgSize = NULL;
m_rgElemType = NULL;
// Initialize decoderOldIL before checking the method argument signature.
EX_TRY
{
pILHeader = pMD->GetILHeader();
}
EX_CATCH_HRESULT(hr);
if (FAILED(hr))
return hr;
COR_ILMETHOD_DECODER decoderOldIL(pILHeader);
mdSignature mdLocalSig = (decoderOldIL.GetLocalVarSigTok()) ? (decoderOldIL.GetLocalVarSigTok()):
(mdSignatureNil);
PCCOR_SIGNATURE pCallSig;
DWORD cbCallSigSize;
pMD->GetSig(&pCallSig, &cbCallSigSize);
if (pCallSig != NULL)
{
// Yes, we do need to pass in the text because this might be generic function!
SigTypeContext tmpContext(pMD);
pArgSig = new (interopsafe, nothrow) MetaSig(pCallSig,
cbCallSigSize,
pMD->GetModule(),
&tmpContext,
MetaSig::sigMember);
if (pArgSig == NULL)
{
IfFailGo(E_OUTOFMEMORY);
}
m_numArgs = pArgSig->NumFixedArgs();
if (pArgSig->HasThis())
{
m_numArgs++;
}
// <TODO>
// What should we do in this case?
// </TODO>
/*
if (argSig.IsVarArg())
m_numArgs++;
*/
}
// allocation of pArgSig succeeded
ULONG cbSig;
PCCOR_SIGNATURE pLocalSig;
pLocalSig = NULL;
if (mdLocalSig != mdSignatureNil)
{
IfFailGo(pMD->GetModule()->GetMDImport()->GetSigFromToken(mdLocalSig, &cbSig, &pLocalSig));
}
if (pLocalSig != NULL)
{
SigTypeContext tmpContext(pMD);
pLocSig = new (interopsafe, nothrow) MetaSig(pLocalSig,
cbSig,
pMD->GetModule(),
&tmpContext,
MetaSig::sigLocalVars);
if (pLocSig == NULL)
{
IfFailGo(E_OUTOFMEMORY);
}
}
// allocation of pLocalSig succeeded
m_numTotalVars = m_numArgs + (pLocSig != NULL ? pLocSig->NumFixedArgs() : 0);
if (m_numTotalVars > 0)
{
m_rgSize = new (interopsafe, nothrow) SIZE_T[m_numTotalVars];
m_rgElemType = new (interopsafe, nothrow) CorElementType[m_numTotalVars];
if ((m_rgSize == NULL) || (m_rgElemType == NULL))
{
IfFailGo(E_OUTOFMEMORY);
}
else
{
// allocation of m_rgSize and m_rgElemType succeeded
for (ULONG i = 0; i < m_numTotalVars; i++)
{
// Choose the correct signature to walk.
MetaSig *pCur = NULL;
if (i < m_numArgs)
{
pCur = pArgSig;
}
else
{
pCur = pLocSig;
}
// The "this" argument isn't stored in the signature, so we have to
// check for it manually.
if (i == 0 && pCur->HasThis())
{
_ASSERTE(pCur == pArgSig);
m_rgElemType[i] = ELEMENT_TYPE_CLASS;
m_rgSize[i] = sizeof(SIZE_T);
}
else
{
m_rgElemType[i] = pCur->NextArg();
if (m_rgElemType[i] == ELEMENT_TYPE_VALUETYPE)
{
m_rgSize[i] = GetValueClassSize(pCur);
}
else
{
m_rgSize[i] = GetSetFrameHelper::GetSizeOfElement(m_rgElemType[i]);
}
LOG((LF_CORDB, LL_INFO10000, "GSFH::I: var 0x%x is of type %x, size:0x%x\n",
i, m_rgElemType[i], m_rgSize[i]));
}
}
} // allocation of m_rgSize and m_rgElemType succeeded
} // if there are variables to take care of
ErrExit:
// clean up
if (pArgSig != NULL)
{
DeleteInteropSafe(pArgSig);
}
if (pLocSig != NULL)
{
DeleteInteropSafe(pLocSig);
}
if (FAILED(hr))
{
if (m_rgSize != NULL)
{
DeleteInteropSafe(m_rgSize);
}
if (m_rgElemType != NULL)
{
DeleteInteropSafe((int*)m_rgElemType);
}
}
return hr;
} // GetSetFrameHelper::Init
//
// GetSetFrameHelper::~GetSetFrameHelper()
//
// This is the destructor. It checks the two arrays we have allocated and frees the memory accordingly.
//
// parameters: none
//
// return value: none
//
GetSetFrameHelper::~GetSetFrameHelper()
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
}
CONTRACTL_END;
if (m_rgSize)
{
DeleteInteropSafe(m_rgSize);
}
if (m_rgElemType)
{
DeleteInteropSafe((int*)m_rgElemType);
}
}
//
// GetSetFrameHelper::GetSizeOfElement()
//
// Given a CorElementType, this function returns the size of this type.
// Note that this function doesn't handle ELEMENT_TYPE_VALUETYPE. Use GetValueClassSize() instead.
//
// parameters: cet - the CorElementType of the argument/local we are dealing with
//
// return value: the size of the argument/local
//
// static
SIZE_T GetSetFrameHelper::GetSizeOfElement(CorElementType cet)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(cet != ELEMENT_TYPE_VALUETYPE);
}
CONTRACTL_END;
if (!CorIsPrimitiveType(cet))
{
return sizeof(SIZE_T);
}
else
{
switch (cet)
{
case ELEMENT_TYPE_I8:
case ELEMENT_TYPE_U8:
#if defined(_WIN64)
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
#endif // _WIN64
case ELEMENT_TYPE_R8:
return 8;
case ELEMENT_TYPE_I4:
case ELEMENT_TYPE_U4:
#if !defined(_WIN64)
case ELEMENT_TYPE_I:
case ELEMENT_TYPE_U:
#endif // !_WIN64
case ELEMENT_TYPE_R4:
return 4;
case ELEMENT_TYPE_I2:
case ELEMENT_TYPE_U2:
case ELEMENT_TYPE_CHAR:
return 2;
case ELEMENT_TYPE_I1:
case ELEMENT_TYPE_U1:
case ELEMENT_TYPE_BOOLEAN:
return 1;
case ELEMENT_TYPE_VOID:
case ELEMENT_TYPE_END:
_ASSERTE(!"debugger.cpp - Check this code path\n");
return 0;
case ELEMENT_TYPE_STRING:
return sizeof(SIZE_T);
default:
_ASSERTE(!"debugger.cpp - Check this code path\n");
return sizeof(SIZE_T);
}
}
}
//
// GetSetFrameHelper::GetValueClassSize()
//
// Given a MetaSig pointer to the signature of a value type, this function returns its size.
//
// parameters: pSig - MetaSig pointer to the signature of a value type
//
// return value: the size of this value type
//
SIZE_T GetSetFrameHelper::GetValueClassSize(MetaSig* pSig)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pSig));
}
CONTRACTL_END;
// We need to determine the number of bytes for this value-type.
SigPointer sp = pSig->GetArgProps();
TypeHandle vcType = TypeHandle();
{
// Lookup operations run the class loader in non-load mode.
ENABLE_FORBID_GC_LOADER_USE_IN_THIS_SCOPE();
// This will return Null if type is not restored
// @todo : is this what we want?
SigTypeContext typeContext(m_pMD);
vcType = sp.GetTypeHandleThrowing(m_pMD->GetModule(),
&typeContext,
// == FailIfNotLoaded
ClassLoader::DontLoadTypes);
}
// We need to know the size of the class in bytes. This means:
// - we need a specific instantiation (since that affects size)
// - but we don't care if it's shared (since it will be the same size either way)
_ASSERTE(!vcType.IsNull() && vcType.IsValueType());
return (vcType.GetMethodTable()->GetAlignedNumInstanceFieldBytes());
}
//
// GetSetFrameHelper::GetValueClassSizeOfVar()
//
// This method retrieves the size of the variable saved in the array m_rgSize. Also, it returns true
// if the variable is a value type.
//
// parameters: varNum - the variable number (arguments come before locals)
// varType - the type of variable home
// pSize - [out] the size
//
// return value: whether this variable is a value type
//
bool GetSetFrameHelper::GetValueClassSizeOfVar(int varNum, ICorDebugInfo::VarLocType varType, SIZE_T* pSize)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(varType != ICorDebugInfo::VLT_FIXED_VA);
PRECONDITION(pSize != NULL);
}
CONTRACTL_END;
// preliminary checking
if (varNum < 0)
{
// Make sure this is one of the secret parameters (e.g. VASigCookie, generics context, etc.).
_ASSERTE(varNum > (int)ICorDebugInfo::MAX_ILNUM);
*pSize = sizeof(LPVOID);
return false;
}
// This check is only safe after we make sure that varNum is not negative.
if ((UINT)varNum >= m_numTotalVars)
{
_ASSERTE(!"invalid variable index encountered during setip");
*pSize = 0;
return false;
}
CorElementType cet = m_rgElemType[varNum];
*pSize = m_rgSize[varNum];
if ((cet != ELEMENT_TYPE_VALUETYPE) ||
(varType == ICorDebugInfo::VLT_REG) ||
(varType == ICorDebugInfo::VLT_REG_REG) ||
(varType == ICorDebugInfo::VLT_REG_STK) ||
(varType == ICorDebugInfo::VLT_STK_REG))
{
return false;
}
else
{
return true;
}
}
int GetSetFrameHelper::ShiftIndexForHiddens(int varNum)
{
LIMITED_METHOD_CONTRACT;
//
// Need to shift them up so are appropriate index for rgVal arrays
//
return varNum - ICorDebugInfo::UNKNOWN_ILNUM;
}
// Helper method pair to grab all, then set all, variables at a given
// point in a routine.
// NOTE: GetVariablesFromOffset and SetVariablesAtOffset are
// very similar - modifying one will probably need to be reflected in the other...
// rgVal1 and rgVal2 are preallocated by callers with estimated size.
// We pass in the size of the allocation in rRgValeSize. The safe index will be rgVal1[0..uRgValSize - 1]
//
HRESULT Debugger::GetVariablesFromOffset(MethodDesc *pMD,
UINT varNativeInfoCount,
ICorDebugInfo::NativeVarInfo *varNativeInfo,
SIZE_T offsetFrom,
CONTEXT *pCtx,
SIZE_T *rgVal1,
SIZE_T *rgVal2,
UINT uRgValSize, // number of elements of the preallocated rgVal1 and rgVal2
BYTE ***rgpVCs)
{
// @todo - convert this to throwing w/ holders. It will be cleaner.
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(rgpVCs));
PRECONDITION(CheckPointer(pCtx));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(varNativeInfo));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(rgVal1));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(rgVal2));
// This may or may not be called on the helper thread.
}
CONTRACTL_END;
*rgpVCs = NULL;
// if there are no locals, well, we are done!
if (varNativeInfoCount == 0)
{
return S_OK;
}
memset( rgVal1, 0, sizeof(SIZE_T)*uRgValSize);
memset( rgVal2, 0, sizeof(SIZE_T)*uRgValSize);
LOG((LF_CORDB|LF_ENC, LL_INFO10000, "D::GVFO: %s::%s, infoCount:0x%x, from:0x%p\n",
pMD->m_pszDebugClassName,
pMD->m_pszDebugMethodName,
varNativeInfoCount,
offsetFrom));
GetSetFrameHelper frameHelper;
HRESULT hr = frameHelper.Init(pMD);
if (FAILED(hr))
{
return hr;
}
// preallocate enough to hold all possible valueclass args & locals
// sure this is more than we need, but not a big deal and better
// than having to crawl through the frameHelper and count
ULONG cValueClasses = 0;
BYTE **rgpValueClasses = new (interopsafe, nothrow) BYTE *[varNativeInfoCount];
if (rgpValueClasses == NULL)
{
return E_OUTOFMEMORY;
}
memset(rgpValueClasses, 0, sizeof(BYTE *)*varNativeInfoCount);
hr = S_OK;
LOG((LF_CORDB|LF_ENC,
LL_INFO10000,
"D::GVFO rgVal1 0x%X, rgVal2 0x%X\n",
rgVal1,
rgVal2));
// Now go through the full array and save off each arg and local
for (UINT i = 0; i< varNativeInfoCount;i++)
{
// Ignore variables not live at offsetFrom
//
// #VarLife
//
// The condition below is a little strange. If a var is alive when this is true:
//
// startOffset <= offsetFrom < endOffset
//
// Then you'd expect the negated expression below to be:
//
// startOffset > offsetFrom || endOffset <= offsetFrom
//
// instead of what we're doing ("<" instead of "<="):
//
// startOffset > offsetFrom || endOffset < offsetFrom
//
// I'm not sure if the condition below is a mistake, or if it's intentionally
// mirroring a workaround from FindNativeInfoInILVariableArray() (Debug\DI\module.cpp)
// to deal with optimized code. So I'm leaving it alone for now. See
// code:FindNativeInfoInILVariableArray for more info on this workaround.
if ((varNativeInfo[i].startOffset > offsetFrom) ||
(varNativeInfo[i].endOffset < offsetFrom) ||
(varNativeInfo[i].loc.vlType == ICorDebugInfo::VLT_INVALID))
{
LOG((LF_CORDB|LF_ENC,LL_INFO10000, "D::GVFO [%2d] invalid\n", i));
continue;
}
SIZE_T cbClass;
bool isVC = frameHelper.GetValueClassSizeOfVar(varNativeInfo[i].varNumber,
varNativeInfo[i].loc.vlType,
&cbClass);
if (!isVC)
{
int rgValIndex = frameHelper.ShiftIndexForHiddens(varNativeInfo[i].varNumber);
_ASSERTE(rgValIndex >= 0 && rgValIndex < (int)uRgValSize);
BOOL res = GetNativeVarVal(varNativeInfo[i].loc,
pCtx,
rgVal1 + rgValIndex,
rgVal2 + rgValIndex
WIN64_ARG(cbClass));
LOG((LF_CORDB|LF_ENC,LL_INFO10000,
"D::GVFO [%2d] varnum %d, nonVC type %x, addr %8.8x: %8.8x;%8.8x\n",
i,
varNativeInfo[i].varNumber,
varNativeInfo[i].loc.vlType,
NativeVarStackAddr(varNativeInfo[i].loc, pCtx),
rgVal1[rgValIndex],
rgVal2[rgValIndex]));
if (res == TRUE)
{
continue;
}
_ASSERTE(res == TRUE);
hr = E_FAIL;
break;
}
// it's definately a value class
// Make space for it - note that it uses the VC index, NOT the variable index
_ASSERTE(cbClass != 0);
rgpValueClasses[cValueClasses] = new (interopsafe, nothrow) BYTE[cbClass];
if (rgpValueClasses[cValueClasses] == NULL)
{
hr = E_OUTOFMEMORY;
break;
}
memcpy(rgpValueClasses[cValueClasses],
NativeVarStackAddr(varNativeInfo[i].loc, pCtx),
cbClass);
// Move index up.
cValueClasses++;
#ifdef _DEBUG
LOG((LF_CORDB|LF_ENC,LL_INFO10000,
"D::GVFO [%2d] varnum %d, VC len %d, addr %8.8x, sample: %8.8x%8.8x\n",
i,
varNativeInfo[i].varNumber,
cbClass,
NativeVarStackAddr(varNativeInfo[i].loc, pCtx),
(rgpValueClasses[cValueClasses-1])[0], (rgpValueClasses[cValueClasses-1])[1]));
#endif
}
LOG((LF_CORDB|LF_ENC, LL_INFO10000, "D::GVFO: returning %8.8x\n", hr));
if (SUCCEEDED(hr))
{
(*rgpVCs) = rgpValueClasses;
return hr;
}
// We failed for some reason
if (rgpValueClasses != NULL)
{ // free any memory we allocated for VCs here
while(cValueClasses > 0)
{
--cValueClasses;
DeleteInteropSafe(rgpValueClasses[cValueClasses]); // OK to delete NULL
}
DeleteInteropSafe(rgpValueClasses);
rgpValueClasses = NULL;
}
return hr;
}
// NOTE: GetVariablesFromOffset and SetVariablesAtOffset are
// very similar - modifying one will probably need to be reflected in the other...
HRESULT Debugger::SetVariablesAtOffset(MethodDesc *pMD,
UINT varNativeInfoCount,
ICorDebugInfo::NativeVarInfo *varNativeInfo,
SIZE_T offsetTo,
CONTEXT *pCtx,
SIZE_T *rgVal1,
SIZE_T *rgVal2,
BYTE **rgpVCs)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pCtx));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(rgpVCs));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(varNativeInfo));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(rgVal1));
PRECONDITION(varNativeInfoCount == 0 || CheckPointer(rgVal2));
// This may or may not be called on the helper thread.
}
CONTRACTL_END;
LOG((LF_CORDB|LF_ENC, LL_INFO10000, "D::SVAO: %s::%s, infoCount:0x%x, to:0x%p\n",
pMD->m_pszDebugClassName,
pMD->m_pszDebugMethodName,
varNativeInfoCount,
offsetTo));
if (varNativeInfoCount == 0)
{
return S_OK;
}
GetSetFrameHelper frameHelper;
HRESULT hr = frameHelper.Init(pMD);
if (FAILED(hr))
{
return hr;
}
ULONG iVC = 0;
hr = S_OK;
// Note that since we obtain all the variables in the first loop, we
// can now splatter those variables into their new locations
// willy-nilly, without the fear that variable locations that have
// been swapped might accidentally overwrite a variable value.
for (UINT i = 0;i< varNativeInfoCount;i++)
{
// Ignore variables not live at offsetTo
//
// If this IF condition looks wrong to you, see
// code:Debugger::GetVariablesFromOffset#VarLife for more info
if ((varNativeInfo[i].startOffset > offsetTo) ||
(varNativeInfo[i].endOffset < offsetTo) ||
(varNativeInfo[i].loc.vlType == ICorDebugInfo::VLT_INVALID))
{
LOG((LF_CORDB|LF_ENC,LL_INFO10000, "D::SVAO [%2d] invalid\n", i));
continue;
}
SIZE_T cbClass;
bool isVC = frameHelper.GetValueClassSizeOfVar(varNativeInfo[i].varNumber,
varNativeInfo[i].loc.vlType,
&cbClass);
if (!isVC)
{
int rgValIndex = frameHelper.ShiftIndexForHiddens(varNativeInfo[i].varNumber);
_ASSERTE(rgValIndex >= 0);
BOOL res = SetNativeVarVal(varNativeInfo[i].loc,
pCtx,
rgVal1[rgValIndex],
rgVal2[rgValIndex]
WIN64_ARG(cbClass));
LOG((LF_CORDB|LF_ENC,LL_INFO10000,
"D::SVAO [%2d] varnum %d, nonVC type %x, addr %8.8x: %8.8x;%8.8x\n",
i,
varNativeInfo[i].varNumber,
varNativeInfo[i].loc.vlType,
NativeVarStackAddr(varNativeInfo[i].loc, pCtx),
rgVal1[rgValIndex],
rgVal2[rgValIndex]));
if (res == TRUE)
{
continue;
}
_ASSERTE(res == TRUE);
hr = E_FAIL;
break;
}
// It's definately a value class.
_ASSERTE(cbClass != 0);
if (rgpVCs[iVC] == NULL)
{
// it's new in scope, so just clear it
memset(NativeVarStackAddr(varNativeInfo[i].loc, pCtx), 0, cbClass);
LOG((LF_CORDB|LF_ENC,LL_INFO10000, "D::SVAO [%2d] varnum %d, new VC len %d, addr %8.8x\n",
i,
varNativeInfo[i].varNumber,
cbClass,
NativeVarStackAddr(varNativeInfo[i].loc, pCtx)));
continue;
}
// it's a pre-existing VC, so copy it
memmove(NativeVarStackAddr(varNativeInfo[i].loc, pCtx), rgpVCs[iVC], cbClass);
#ifdef _DEBUG
LOG((LF_CORDB|LF_ENC,LL_INFO10000,
"D::SVAO [%2d] varnum %d, VC len %d, addr: %8.8x sample: %8.8x%8.8x\n",
i,
varNativeInfo[i].varNumber,
cbClass,
NativeVarStackAddr(varNativeInfo[i].loc, pCtx),
rgpVCs[iVC][0],
rgpVCs[iVC][1]));
#endif
// Now get rid of the memory
DeleteInteropSafe(rgpVCs[iVC]);
rgpVCs[iVC] = NULL;
iVC++;
}
LOG((LF_CORDB|LF_ENC, LL_INFO10000, "D::SVAO: returning %8.8x\n", hr));
if (rgpVCs != NULL)
{
DeleteInteropSafe(rgpVCs);
}
return hr;
}
BOOL IsDuplicatePatch(SIZE_T *rgEntries,
ULONG cEntries,
SIZE_T Entry )
{
LIMITED_METHOD_CONTRACT;
for( ULONG i = 0; i < cEntries;i++)
{
if (rgEntries[i] == Entry)
return TRUE;
}
return FALSE;
}
/******************************************************************************
// HRESULT Debugger::MapAndBindFunctionBreakpoints(): For each breakpoint
// that we've set in any version of the existing function,
// set a correponding breakpoint in the new function if we haven't moved
// the patch to the new version already.
//
// This must be done _AFTER_ the MethodDesc has been udpated
// with the new address (ie, when GetFunctionAddress pFD returns
// the address of the new EnC code)
//
// Parameters:
// djiNew - this is the DJI created in D::JitComplete.
// If djiNew == NULL iff we aren't tracking debug-info.
// fd - the method desc that we're binding too.
// addrOfCode - address of the native blob of code we just jitted
//
// <TODO>@todo Replace array with hashtable for improved efficiency</TODO>
// <TODO>@todo Need to factor code,so that we can selectively map forward DFK(ilOFfset) BPs</TODO>
******************************************************************************/
HRESULT Debugger::MapAndBindFunctionPatches(DebuggerJitInfo *djiNew,
MethodDesc * fd,
CORDB_ADDRESS_TYPE *addrOfCode)
{
// @@@
// Internal helper API. Can be called from Debugger or Controller.
//
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
CALLED_IN_DEBUGGERDATALOCK_HOLDER_SCOPE_MAY_GC_TRIGGERS_CONTRACT;
PRECONDITION(!djiNew || djiNew->m_fd == fd);
}
CONTRACTL_END;
HRESULT hr = S_OK;
HASHFIND hf;
SIZE_T *pidTableEntry = NULL;
SIZE_T pidInCaseTableMoves;
Module *pModule = g_pEEInterface->MethodDescGetModule(fd);
mdMethodDef md = fd->GetMemberDef();
LOG((LF_CORDB,LL_INFO10000,"D::MABFP: All BPs will be mapped to "
"Ver:0x%04x (DJI:0x%08x)\n", djiNew?djiNew->m_methodInfo->GetCurrentEnCVersion():0, djiNew));
// We need to traverse the patch list while under the controller lock (small lock).
// But we can only send BreakpointSetErros while under the debugger lock (big lock).
// So to avoid a lock violation, we queue any errors we find under the small lock,
// and then send the whole list when under the big lock.
PATCH_UNORDERED_ARRAY listUnbindablePatches;
// First lock the patch table so it doesn't move while we're
// examining it.
LOG((LF_CORDB,LL_INFO10000, "D::MABFP: About to lock patch table\n"));
{
DebuggerController::ControllerLockHolder ch;
// Manipulate tables AFTER lock's been acquired.
DebuggerPatchTable *pPatchTable = DebuggerController::GetPatchTable();
GetBPMappingDuplicates()->Clear(); //dups are tracked per-version
for (DebuggerControllerPatch *dcp = pPatchTable->GetFirstPatch(&hf);
dcp != NULL;
dcp = pPatchTable->GetNextPatch( &hf ))
{
LOG((LF_CORDB, LL_INFO10000, "D::MABFP: got patch 0x%p\n", dcp));
// Only copy over breakpoints that are in this method
// Ideally we'd have a per-method index since there can be a lot of patches
// when the EnCBreakpoint patches are included.
if (dcp->key.module != pModule || dcp->key.md != md)
{
LOG((LF_CORDB, LL_INFO10000, "Patch not in this method\n"));
continue;
}
// Do not copy over slave breakpoint patches. Instead place a new slave
// based off the master.
if (dcp->IsILSlavePatch())
{
LOG((LF_CORDB, LL_INFO10000, "Not copying over slave breakpoint patch\n"));
continue;
}
// If the patch is already bound, then we don't want to try to rebind it.
// Eg. It may be bound to a different generic method instantiation.
if (dcp->IsBound())
{
LOG((LF_CORDB, LL_INFO10000, "Skipping already bound patch\n"));
continue;
}
// Only apply breakpoint patches that are for this version.
// If the patch doesn't have a particular EnCVersion available from its data then
// we're (probably) not tracking JIT info.
if (dcp->IsBreakpointPatch() && dcp->HasEnCVersion() && djiNew && dcp->GetEnCVersion() != djiNew->m_encVersion)
{
LOG((LF_CORDB, LL_INFO10000, "Not applying breakpoint patch to new version\n"));
continue;
}
// Only apply breakpoint and stepper patches
//
// The DJI gets deleted as part of the Unbind/Rebind process in MovedCode.
// This is to signal that we should not skip here.
// <NICE> under exactly what scenarios (EnC, code pitching etc.) will this apply?... </NICE>
// <NICE> can't we be a little clearer about why we don't want to bind the patch in this arcance situation?</NICE>
if (dcp->HasDJI() && !dcp->IsBreakpointPatch() && !dcp->IsStepperPatch())
{
LOG((LF_CORDB, LL_INFO10000, "Neither stepper nor BP but we have valid a DJI (i.e. the DJI hasn't been deleted as part of the Unbind/MovedCode/Rebind mess)! - getting next patch!\n"));
continue;
}
// Now check if we're tracking JIT info or not
if (djiNew == NULL)
{
// This means we put a patch in a method w/ no debug info.
_ASSERTE(dcp->IsBreakpointPatch() ||
dcp->IsStepperPatch() ||
dcp->controller->GetDCType() == DEBUGGER_CONTROLLER_THREAD_STARTER);
// W/o Debug-info, We can only patch native offsets, and only at the start of the method (native offset 0).
// <TODO> Why can't we patch other native offsets??
// Maybe b/c we don't know if we're patching
// in the middle of an instruction. Though that's not a
// strict requirement.</TODO>
// We can't even do a IL-offset 0 because that's after the prolog and w/o the debug-info,
// we don't know where the prolog ends.
// Failing this assert is arguably an API misusage - the debugger should have enabled
// jit-tracking if they wanted to put bps at offsets other than native:0.
if (dcp->IsNativePatch() && (dcp->offset == 0))
{
DebuggerController::g_patches->BindPatch(dcp, addrOfCode);
DebuggerController::ActivatePatch(dcp);
}
else
{
// IF a debugger calls EnableJitDebugging(true, ...) in the module-load callback,
// we should never get here.
*(listUnbindablePatches.AppendThrowing()) = dcp;
}
}
else
{
pidInCaseTableMoves = dcp->pid;
// If we've already mapped this one to the current version,
// don't map it again.
LOG((LF_CORDB,LL_INFO10000,"D::MABFP: Checking if 0x%x is a dup...",
pidInCaseTableMoves));
if ( IsDuplicatePatch(GetBPMappingDuplicates()->Table(),
GetBPMappingDuplicates()->Count(),
pidInCaseTableMoves) )
{
LOG((LF_CORDB,LL_INFO10000,"it is!\n"));
continue;
}
LOG((LF_CORDB,LL_INFO10000,"nope!\n"));
// Attempt mapping from patch to new version of code, and
// we don't care if it turns out that there isn't a mapping.
// <TODO>@todo-postponed: EnC: Make sure that this doesn't cause
// the patch-table to shift.</TODO>
hr = MapPatchToDJI( dcp, djiNew );
if (CORDBG_E_CODE_NOT_AVAILABLE == hr )
{
*(listUnbindablePatches.AppendThrowing()) = dcp;
hr = S_OK;
}
if (FAILED(hr))
break;
//Remember the patch id to prevent duplication later
pidTableEntry = GetBPMappingDuplicates()->Append();
if (NULL == pidTableEntry)
{
hr = E_OUTOFMEMORY;
break;
}
*pidTableEntry = pidInCaseTableMoves;
LOG((LF_CORDB,LL_INFO10000,"D::MABFP Adding 0x%x to list of "
"already mapped patches\n", pidInCaseTableMoves));
}
}
// unlock controller lock before sending events.
}
LOG((LF_CORDB,LL_INFO10000, "D::MABFP: Unlocked patch table\n"));
// Now send any Breakpoint bind error events.
if (listUnbindablePatches.Count() > 0)
{
LockAndSendBreakpointSetError(&listUnbindablePatches);
}
return hr;
}
/******************************************************************************
// HRESULT Debugger::MapPatchToDJI(): Maps the given
// patch to the corresponding location at the new address.
// We assume that the new code has been JITTed.
// Returns: CORDBG_E_CODE_NOT_AVAILABLE - Indicates that a mapping wasn't
// available, and thus no patch was placed. The caller may or may
// not care.
******************************************************************************/
HRESULT Debugger::MapPatchToDJI( DebuggerControllerPatch *dcp,DebuggerJitInfo *djiTo)
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
CALLED_IN_DEBUGGERDATALOCK_HOLDER_SCOPE_MAY_GC_TRIGGERS_CONTRACT;
PRECONDITION(djiTo != NULL);
PRECONDITION(djiTo->m_jitComplete == true);
}
CONTRACTL_END;
_ASSERTE(DebuggerController::HasLock());
#ifdef _DEBUG
static BOOL shouldBreak = -1;
if (shouldBreak == -1)
shouldBreak = UnsafeGetConfigDWORD(CLRConfig::INTERNAL_DbgBreakOnMapPatchToDJI);
if (shouldBreak > 0) {
_ASSERTE(!"DbgBreakOnMatchPatchToDJI");
}
#endif
LOG((LF_CORDB, LL_EVERYTHING, "Calling MapPatchToDJI\n"));
// We shouldn't have been asked to map an already bound patch
_ASSERTE( !dcp->IsBound() );
if ( dcp->IsBound() )
{
return S_OK;
}
// If the patch has no DJI then we're doing a UnbindFunctionPatches/RebindFunctionPatches. Either
// way, we simply want the most recent version. In the absence of EnC we should have djiCur == djiTo.
DebuggerJitInfo *djiCur = dcp->HasDJI() ? dcp->GetDJI() : djiTo;
PREFIX_ASSUME(djiCur != NULL);
// If the source and destination are the same version, then this method
// decays into BindFunctionPatch's BindPatch function
if (djiCur->m_encVersion == djiTo->m_encVersion)
{
// If the patch is a "master" then make a new "slave" patch instead of
// binding the old one. This is to stop us mucking with the master breakpoint patch
// which we may need to bind several times for generic code.
if (dcp->IsILMasterPatch())
{
LOG((LF_CORDB, LL_EVERYTHING, "Add, Bind, Activate new patch from master patch\n"));
if (dcp->controller->AddBindAndActivateILSlavePatch(dcp, djiTo))
{
LOG((LF_CORDB, LL_INFO1000, "Add, Bind Activate went fine!\n" ));
return S_OK;
}
else
{
LOG((LF_CORDB, LL_INFO1000, "Didn't work for some reason!\n"));
// Caller can track this HR and send error.
return CORDBG_E_CODE_NOT_AVAILABLE;
}
}
else
{
// <TODO>
// We could actually have a native managed patch here. This patch is probably added
// as a result of tracing a patch. See if we can eliminate the need for this code path
// </TODO>
_ASSERTE( dcp->GetKind() == PATCH_KIND_NATIVE_MANAGED );
// We have an unbound native patch (eg. for PatchTrace), lets try to bind and activate it
dcp->SetDJI(djiTo);
LOG((LF_CORDB, LL_EVERYTHING, "trying to bind patch... could be problem\n"));
if (DebuggerController::BindPatch(dcp, djiTo->m_fd, NULL))
{
DebuggerController::ActivatePatch(dcp);
LOG((LF_CORDB, LL_INFO1000, "Application went fine!\n" ));
return S_OK;
}
else
{
LOG((LF_CORDB, LL_INFO1000, "Didn't apply for some reason!\n"));
// Caller can track this HR and send error.
return CORDBG_E_CODE_NOT_AVAILABLE;
}
}
}
// Breakpoint patches never get mapped over
_ASSERTE(!dcp->IsBreakpointPatch());
return S_OK;
}
//
// Wrapper function for debugger to WaitForSingleObject. If CLR is hosted,
// notify host before we leave runtime.
//
DWORD Debugger::WaitForSingleObjectHelper(HANDLE handle, DWORD dwMilliseconds)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
DWORD dw = 0;
EX_TRY
{
// make sure that we let host know that we are leaving runtime.
LeaveRuntimeHolder holder((size_t)(::WaitForSingleObject));
dw = ::WaitForSingleObject(handle,dwMilliseconds);
}
EX_CATCH
{
// Only possibility to enter here is when Thread::LeaveRuntime
// throws exception.
dw = WAIT_ABANDONED;
}
EX_END_CATCH(SwallowAllExceptions);
return dw;
}
/* ------------------------------------------------------------------------ *
* EE Interface routines
* ------------------------------------------------------------------------ */
//
// SendSyncCompleteIPCEvent sends a Sync Complete event to the Right Side.
//
void Debugger::SendSyncCompleteIPCEvent()
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(ThreadHoldsLock());
// Anyone sending the synccomplete must hold the TSL.
PRECONDITION(ThreadStore::HoldingThreadStore() || g_fProcessDetach);
// The sync complete is now only sent on a helper thread.
PRECONDITION(ThisIsHelperThreadWorker());
MODE_COOPERATIVE;
// We had better be trapping Runtime threads and not stopped yet.
PRECONDITION(m_stopped && m_trappingRuntimeThreads);
}
CONTRACTL_END;
// @@@
// Internal helper API.
// This is to send Sync Complete event to RightSide.
// We should have hold the debugger lock
//
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::SSCIPCE: sync complete.\n");
// Synchronizing while in in rude shutdown should be extremely rare b/c we don't
// TART in rude shutdown. Shutdown must have started after we started to sync.
// We know we're not on the shutdown thread here.
// And we also know we can't block the shutdown thread (b/c it has the TSL and will
// get a free pass through the GC toggles that normally block threads for debugging).
if (g_fProcessDetach)
{
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::SSCIPCE: Skipping for shutdown.\n");
return;
}
// If we're not marked as attached yet, then do that now.
// This can be safely called multiple times.
// This can happen in the normal attach case. The Right-side sends an async-break,
// but we don't want to be considered attach until we've actually gotten our first synchronization.
// Else threads may slip forward during attach and send debug events while we're tyring to attach.
MarkDebuggerAttachedInternal();
DebuggerIPCControlBlock * pDCB;
pDCB = m_pRCThread->GetDCB();
(void)pDCB; //prevent "unused variable" error from GCC
PREFIX_ASSUME(pDCB != NULL); // must have DCB by the time we're sending IPC events.
#ifdef FEATURE_INTEROP_DEBUGGING
// The synccomplete can't be the first IPC event over. That's b/c the LS needs to know
// if we're interop-debugging and the RS needs to know special addresses for interop-debugging
// (like flares). All of this info is in the DCB.
if (pDCB->m_rightSideIsWin32Debugger)
{
// If the Right Side is the win32 debugger of this process, then we need to throw a special breakpoint exception
// here instead of sending the sync complete event. The Right Side treats this the same as a sync complete
// event, but its also able to suspend unmanaged threads quickly.
// This also prevents races between sending the sync-complete and getting a native debug event
// (since the sync-complete becomes a native debug event, and all native debug events are serialized).
//
// Note: we reset the syncThreadIsLockFree event before sending the sync complete flare. This thread will set
// this event once its released the debugger lock. This will prevent the Right Side from suspending this thread
// until it has released the debugger lock.
Debugger::NotifyRightSideOfSyncComplete();
}
else
#endif // FEATURE_INTEROP_DEBUGGING
{
STRESS_LOG0(LF_CORDB, LL_EVERYTHING, "GetIPCEventSendBuffer called in SendSyncCompleteIPCEvent\n");
// Send the Sync Complete event to the Right Side
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_SYNC_COMPLETE);
m_pRCThread->SendIPCEvent();
}
}
//
// Lookup or create a DebuggerModule for the given pDomainFile.
//
// Arguments:
// pDomainFile - non-null domain file.
//
// Returns:
// DebuggerModule instance for the given domain file. May be lazily created.
//
// Notes:
// @dbgtodo JMC - this should go away when we get rid of DebuggerModule.
//
DebuggerModule * Debugger::LookupOrCreateModule(DomainFile * pDomainFile)
{
_ASSERTE(pDomainFile != NULL);
LOG((LF_CORDB, LL_INFO1000, "D::LOCM df=0x%x\n", pDomainFile));
DebuggerModule * pDModule = LookupOrCreateModule(pDomainFile->GetModule(), pDomainFile->GetAppDomain());
LOG((LF_CORDB, LL_INFO1000, "D::LOCM m=0x%x ad=0x%x -> dm=0x%x\n", pDomainFile->GetModule(), pDomainFile->GetAppDomain(), pDModule));
_ASSERTE(pDModule != NULL);
_ASSERTE(pDModule->GetDomainFile() == pDomainFile);
return pDModule;
}
// Overloaded Wrapper around for VMPTR_DomainFile-->DomainFile*
//
// Arguments:
// vmDomainFile - VMPTR cookie for a domain file. This can be NullPtr().
//
// Returns:
// Debugger Module instance for the given domain file. May be lazily created.
//
// Notes:
// VMPTR comes from IPC events
DebuggerModule * Debugger::LookupOrCreateModule(VMPTR_DomainFile vmDomainFile)
{
DomainFile * pDomainFile = vmDomainFile.GetRawPtr();
if (pDomainFile == NULL)
{
return NULL;
}
return LookupOrCreateModule(pDomainFile);
}
// Lookup or create a DebuggerModule for the given (Module, AppDomain) pair.
//
// Arguments:
// pModule - required runtime module. May be domain netural.
// pAppDomain - required appdomain that the module is in.
//
// Returns:
// Debugger Module isntance for the given domain file. May be lazily created.
//
DebuggerModule* Debugger::LookupOrCreateModule(Module* pModule, AppDomain *pAppDomain)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000, "D::LOCM m=0x%x ad=0x%x\n", pModule, pAppDomain));
// DebuggerModules are relative to a specific AppDomain so we should always be looking up a module /
// AppDomain pair.
_ASSERTE( pModule != NULL );
_ASSERTE( pAppDomain != NULL );
// This is called from all over. We just need to lock in order to lookup. We don't need
// the lock when actually using the DebuggerModule (since it won't be unloaded as long as there is a thread
// in that appdomain). Many of our callers already have this lock, many don't.
// We can take the lock anyways because it's reentrant.
DebuggerDataLockHolder ch(g_pDebugger); // need to traverse module list
// if this is a module belonging to the system assembly, then scan
// the complete list of DebuggerModules looking for the one
// with a matching appdomain id
// it.
_ASSERTE( SystemDomain::SystemAssembly()->IsDomainNeutral() );
DebuggerModule* dmod = NULL;
if (m_pModules != NULL)
{
if (pModule->GetAssembly()->IsDomainNeutral())
{
// We have to make sure to lookup the module with the app domain parameter if the module lives in a shared assembly
dmod = m_pModules->GetModule(pModule, pAppDomain);
}
else
{
dmod = m_pModules->GetModule(pModule);
}
}
// If it doesn't exist, create it.
if (dmod == NULL)
{
HRESULT hr = S_OK;
EX_TRY
{
DomainFile * pDomainFile = pModule->FindDomainFile(pAppDomain);
SIMPLIFYING_ASSUMPTION(pDomainFile != NULL);
dmod = AddDebuggerModule(pDomainFile); // throws
}
EX_CATCH_HRESULT(hr);
SIMPLIFYING_ASSUMPTION(dmod != NULL); // may not be true in OOM cases; but LS doesn't handle OOM.
}
// The module must be in the AppDomain that was requested
_ASSERTE( (dmod == NULL) || (dmod->GetAppDomain() == pAppDomain) );
LOG((LF_CORDB, LL_INFO1000, "D::LOCM m=0x%x ad=0x%x -> dm=0x%x\n", pModule, pAppDomain, dmod));
return dmod;
}
// Create a new DebuggerModule object
//
// Arguments:
// pDomainFile- runtime domain file to create debugger module object around
//
// Returns:
// New instnace of a DebuggerModule. Throws on failure.
//
DebuggerModule* Debugger::AddDebuggerModule(DomainFile * pDomainFile)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000, "D::ADM df=0x%x\n", pDomainFile));
DebuggerDataLockHolder chInfo(this);
Module * pRuntimeModule = pDomainFile->GetCurrentModule();
AppDomain * pAppDomain = pDomainFile->GetAppDomain();
HRESULT hr = CheckInitModuleTable();
IfFailThrow(hr);
DebuggerModule* pModule = new (interopsafe) DebuggerModule(pRuntimeModule, pDomainFile, pAppDomain);
_ASSERTE(pModule != NULL); // throws on oom
TRACE_ALLOC(pModule);
m_pModules->AddModule(pModule); // throws
// @dbgtodo inspection/exceptions - this may leak module in OOM case. LS is not OOM resilient; and we
// expect to get rid of DebuggerModule anyways.
LOG((LF_CORDB, LL_INFO1000, "D::ADM df=0x%x -> dm=0x%x\n", pDomainFile, pModule));
return pModule;
}
//
// TrapAllRuntimeThreads causes every Runtime thread that is executing
// in the EE to trap and send the at safe point event to the RC thread as
// soon as possible. It also sets the EE up so that Runtime threads that
// are outside of the EE will trap when they try to re-enter.
//
// @TODO::
// Neither pDbgLockHolder nor pAppDomain are used.
void Debugger::TrapAllRuntimeThreads()
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
// We acquired the lock b/c we're in a scope between LFES & UFES.
PRECONDITION(ThreadHoldsLock());
// This should never be called on a Temporary Helper thread.
PRECONDITION(IsDbgHelperSpecialThread() ||
(g_pEEInterface->GetThread() == NULL) ||
!g_pEEInterface->IsPreemptiveGCDisabled());
}
CONTRACTL_END;
#if defined(FEATURE_DBGIPC_TRANSPORT_VM)
DWORD useTransport = CLRConfig::GetConfigValue(CLRConfig::UNSUPPORTED_DbgUseTransport);
if(!useTransport)
{
#endif
// Only sync if RS requested it.
if (!m_RSRequestedSync)
{
return;
}
m_RSRequestedSync = FALSE;
#ifdef FEATURE_DBGIPC_TRANSPORT_VM
}
#endif
// If we're doing shutdown, then don't bother trying to communicate w/ the RS.
// If we're not the thread doing shutdown, then we may be asynchronously killed by the OS.
// If we are the thread in shutdown, don't TART b/c that may block and do complicated stuff.
if (g_fProcessDetach)
{
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::TART: Skipping for shutdown.\n");
return;
}
// Only try to start trapping if we're not already trapping.
if (m_trappingRuntimeThreads == FALSE)
{
bool fSuspended;
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::TART: Trapping all Runtime threads.\n");
// There's no way that we should be stopped and still trying to call this function.
_ASSERTE(!m_stopped);
// Mark that we're trapping now.
m_trappingRuntimeThreads = TRUE;
// Take the thread store lock.
STRESS_LOG0(LF_CORDB,LL_INFO1000, "About to lock thread Store\n");
ThreadSuspend::LockThreadStore(ThreadSuspend::SUSPEND_FOR_DEBUGGER);
STRESS_LOG0(LF_CORDB,LL_INFO1000, "Locked thread store\n");
// We start the suspension here, and let the helper thread finish it.
// If there's no helper thread, then we need to do helper duty.
{
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
fSuspended = g_pEEInterface->StartSuspendForDebug(NULL, TRUE);
}
// We tell the RC Thread to check for other threads now and then and help them get synchronized. (This
// is similar to what is done when suspending threads for GC with the HandledJITCase() function.)
// This does not block.
// Pinging this will waken the helper thread (or temp H. thread) and tell it to sweep & send
// the sync complete.
m_pRCThread->WatchForStragglers();
// It's possible we may not have a real helper thread.
// - on startup in dllmain, helper is blocked on DllMain loader lock.
// - on shutdown, helper has been removed on us.
// In those cases, we need somebody to send the sync-complete, and handle
// managed events, and wait for the continue. So we pretend to be the helper thread.
STRESS_LOG0(LF_CORDB, LL_EVERYTHING, "D::SSCIPCE: Calling IsRCThreadReady()\n");
// We must check the helper thread status while under the lock.
_ASSERTE(ThreadHoldsLock());
// If we failed to suspend, then that means we must have multiple managed threads.
// That means that our helper is not blocked on starting up, thus we can wait infinite on it.
// Thus we don't need to do helper duty if the suspend fails.
bool fShouldDoHelperDuty = !m_pRCThread->IsRCThreadReady() && fSuspended;
if (fShouldDoHelperDuty && !g_fProcessDetach)
{
// In V1.0, we had the assumption that if the helper thread isn't ready yet, then we're in
// a state that SuspendForDebug will succeed on the first try, and thus we'll
// never call Sweep when doing helper thread duty.
_ASSERTE(fSuspended);
// This call will do a ton of work, it will toggle the lock,
// and it will block until we receive a continue!
DoHelperThreadDuty();
// We will have released the TSL after the call to continue.
}
else
{
// We have a live and active helper thread which will handle events
// from the RS now that we're stopped.
// We need to release the TSL which we acquired above. (The helper will
// likely take this lock while doing stuff).
STRESS_LOG0(LF_CORDB,LL_INFO1000, "About to unlock thread store!\n");
ThreadSuspend::UnlockThreadStore(FALSE, ThreadSuspend::SUSPEND_FOR_DEBUGGER);
STRESS_LOG0(LF_CORDB,LL_INFO1000, "TART: Unlocked thread store!\n");
}
_ASSERTE(ThreadHoldsLock()); // still hold the lock. (though it may have been toggled)
}
}
//
// ReleaseAllRuntimeThreads releases all Runtime threads that may be
// stopped after trapping and sending the at safe point event.
//
void Debugger::ReleaseAllRuntimeThreads(AppDomain *pAppDomain)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
// We acquired the lock b/c we're in a scope between LFES & UFES.
PRECONDITION(ThreadHoldsLock());
// Currently, this is only done on a helper thread.
PRECONDITION(ThisIsHelperThreadWorker());
// Make sure that we were stopped...
PRECONDITION(m_trappingRuntimeThreads && m_stopped);
}
CONTRACTL_END;
//<TODO>@todo APPD if we want true isolation, remove this & finish the work</TODO>
pAppDomain = NULL;
STRESS_LOG1(LF_CORDB, LL_INFO10000, "D::RART: Releasing all Runtime threads"
"for AppD 0x%x.\n", pAppDomain);
// Mark that we're on our way now...
m_trappingRuntimeThreads = FALSE;
m_stopped = FALSE;
// Go ahead and resume the Runtime threads.
g_pEEInterface->ResumeFromDebug(pAppDomain);
}
// Given a method, get's its EnC version number. 1 if the method is not EnCed.
// Note that MethodDescs are reused between versions so this will give us
// the most recent EnC number.
int Debugger::GetMethodEncNumber(MethodDesc * pMethod)
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
DebuggerJitInfo * dji = GetLatestJitInfoFromMethodDesc(pMethod);
if (dji == NULL)
{
// If there's no DJI, couldn't have been EnCed.
return 1;
}
return (int) dji->m_encVersion;
}
bool Debugger::IsJMCMethod(Module* pModule, mdMethodDef tkMethod)
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
GC_NOTRIGGER;
MODE_ANY;
PRECONDITION(CORDebuggerAttached());
}
CONTRACTL_END;
#ifdef _DEBUG
Crst crstDbg(CrstIsJMCMethod, CRST_UNSAFE_ANYMODE);
PRECONDITION(crstDbg.IsSafeToTake());
#endif
DebuggerMethodInfo *pInfo = GetOrCreateMethodInfo(pModule, tkMethod);
if (pInfo == NULL)
return false;
return pInfo->IsJMCFunction();
}
/******************************************************************************
* Called by Runtime when on a 1st chance Native Exception.
* This is likely when we hit a breakpoint / single-step.
* This is called for all native exceptions (except COM+) on managed threads,
* regardless of whether the debugger is attached.
******************************************************************************/
bool Debugger::FirstChanceNativeException(EXCEPTION_RECORD *exception,
CONTEXT *context,
DWORD code,
Thread *thread)
{
// @@@
// Implement DebugInterface
// Can be called from EE exception code. Or from our M2UHandoffHijackFilter
// must be on managed thread.
CONTRACTL
{
SO_TOLERANT;
NOTHROW;
// No clear GC_triggers semantics here. See DispatchNativeException.
WRAPPER(GC_TRIGGERS);
MODE_ANY;
PRECONDITION(CheckPointer(exception));
PRECONDITION(CheckPointer(context));
PRECONDITION(CheckPointer(thread));
}
CONTRACTL_END;
// Ignore any notification exceptions sent from code:Debugger.SendRawEvent.
// This is not a common case, but could happen in some cases described
// in SendRawEvent. Either way, Left-Side and VM should just ignore these.
if (IsEventDebuggerNotification(exception, PTR_TO_CORDB_ADDRESS(g_pMSCorEE)))
{
return true;
}
bool retVal;
// Don't stop for native debugging anywhere inside our inproc-Filters.
CantStopHolder hHolder;
if (!CORDBUnrecoverableError(this))
{
retVal = DebuggerController::DispatchNativeException(exception, context,
code, thread);
}
else
{
retVal = false;
}
return retVal;
}
/******************************************************************************
*
******************************************************************************/
PRD_TYPE Debugger::GetPatchedOpcode(CORDB_ADDRESS_TYPE *ip)
{
WRAPPER_NO_CONTRACT;
if (!CORDBUnrecoverableError(this))
{
return DebuggerController::GetPatchedOpcode(ip);
}
else
{
PRD_TYPE mt;
InitializePRD(&mt);
return mt;
}
}
/******************************************************************************
*
******************************************************************************/
BOOL Debugger::CheckGetPatchedOpcode(CORDB_ADDRESS_TYPE *address, /*OUT*/ PRD_TYPE *pOpcode)
{
WRAPPER_NO_CONTRACT;
CONSISTENCY_CHECK(CheckPointer(address));
CONSISTENCY_CHECK(CheckPointer(pOpcode));
if (CORDebuggerAttached() && !CORDBUnrecoverableError(this))
{
return DebuggerController::CheckGetPatchedOpcode(address, pOpcode);
}
else
{
InitializePRD(pOpcode);
return FALSE;
}
}
/******************************************************************************
*
******************************************************************************/
void Debugger::TraceCall(const BYTE *code)
{
CONTRACTL
{
// We're being called right before we call managed code. Can't trigger
// because there may be unprotected args on the stack.
MODE_COOPERATIVE;
GC_NOTRIGGER;
NOTHROW;
}
CONTRACTL_END;
Thread * pCurThread = g_pEEInterface->GetThread();
// Ensure we never even think about running managed code on the helper thread.
_ASSERTE(!ThisIsHelperThreadWorker() || !"You're running managed code on the helper thread");
// One threat is that our helper thread may be forced to execute a managed DLL main.
// In that case, it's before the helper thread proc is even executed, so our conventional
// IsHelperThread() checks are inadequate.
_ASSERTE((GetCurrentThreadId() != g_pRCThread->m_DbgHelperThreadOSTid) || !"You're running managed code on the helper thread");
_ASSERTE((g_pEEInterface->GetThreadFilterContext(pCurThread) == NULL) || !"Shouldn't run managed code w/ Filter-Context set");
if (!CORDBUnrecoverableError(this))
{
// There are situations where our callers can't tolerate us throwing.
EX_TRY
{
// Since we have a try catch and the debugger code can deal properly with
// faults occuring inside DebuggerController::DispatchTraceCall, we can safely
// establish a FAULT_NOT_FATAL region. This is required since some callers can't
// tolerate faults.
FAULT_NOT_FATAL();
DebuggerController::DispatchTraceCall(pCurThread, code);
}
EX_CATCH
{
// We're being called for our benefit, not our callers. So if we fail,
// they don't care.
// Failure for us means that some steppers may miss their notification
// for entering managed code.
LOG((LF_CORDB, LL_INFO10000, "Debugger::TraceCall - inside catch, %p\n", code));
}
EX_END_CATCH(SwallowAllExceptions);
}
}
/******************************************************************************
* For Just-My-Code (aka Just-User-Code).
* Invoked from a probe in managed code when we enter a user method and
* the flag (set by GetJMCFlagAddr) for that method is != 0.
* pIP - the ip within the method, right after the prolog.
* sp - stack pointer (frame pointer on x86) for the managed method we're entering.
* bsp - backing store pointer for the managed method we're entering
******************************************************************************/
void Debugger::OnMethodEnter(void * pIP)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
SO_NOT_MAINLINE;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000000, "D::OnMethodEnter(ip=%p)\n", pIP));
if (!CORDebuggerAttached())
{
LOG((LF_CORDB, LL_INFO1000000, "D::OnMethodEnter returning since debugger attached.\n"));
return;
}
FramePointer fp = LEAF_MOST_FRAME;
DebuggerController::DispatchMethodEnter(pIP, fp);
}
/******************************************************************************
* GetJMCFlagAddr
* Provide an address of the flag that the JMC probes use to decide whether
* or not to call TriggerMethodEnter.
* Called for each method that we jit.
* md - method desc for the JMC probe
* returns an address of a flag that the probe can use.
******************************************************************************/
DWORD* Debugger::GetJMCFlagAddr(Module * pModule)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_TOLERANT;
PRECONDITION(CheckPointer(pModule));
}
CONTRACTL_END;
// This callback will be invoked whenever we jit debuggable code.
// A debugger may not be attached yet, but we still need someplace
// to store this dword.
// Use the EE's module, because it's always around, even if a debugger
// is attached or not.
return &(pModule->m_dwDebuggerJMCProbeCount);
}
/******************************************************************************
* Updates the JMC flag on all the EE modules.
* We can do this as often as we'd like - though it's a perf hit.
******************************************************************************/
void Debugger::UpdateAllModuleJMCFlag(bool fStatus)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000000, "D::UpdateModuleJMCFlag to %d\n", fStatus));
_ASSERTE(HasDebuggerDataLock());
// Loop through each module.
// The module table is lazily allocated. As soon as we set JMC status on any module, that will cause an
// allocation of the module table. So if the table isn't allocated no module has JMC set,
// and so there is nothing to update.
if (m_pModules != NULL)
{
HASHFIND f;
for (DebuggerModule * m = m_pModules->GetFirstModule(&f);
m != NULL;
m = m_pModules->GetNextModule(&f))
{
// the primary module may get called multiple times, but that's ok.
UpdateModuleJMCFlag(m->GetRuntimeModule(), fStatus);
} // end for all modules.
}
}
/******************************************************************************
* Updates the JMC flag on the given Primary module
* We can do this as often as we'd like - though it's a perf hit.
* If we've only changed methods in a single module, then we can just call this.
* If we do a more global thing (Such as enable MethodEnter), then that could
* affect all modules, so we use the UpdateAllModuleJMCFlag helper.
******************************************************************************/
void Debugger::UpdateModuleJMCFlag(Module * pRuntimeModule, bool fStatus)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
_ASSERTE(HasDebuggerDataLock());
DWORD * pFlag = &(pRuntimeModule->m_dwDebuggerJMCProbeCount);
_ASSERTE(pFlag != NULL);
if (pRuntimeModule->HasAnyJMCFunctions())
{
// If this is a user-code module, then update the JMC flag
// the probes look at so that we get MethodEnter callbacks.
*pFlag = fStatus;
LOG((LF_CORDB, LL_EVERYTHING, "D::UpdateModuleJMCFlag, module %p is user code\n", pRuntimeModule));
} else {
LOG((LF_CORDB, LL_EVERYTHING, "D::UpdateModuleJMCFlag, module %p is not-user code\n", pRuntimeModule));
// if non-user code, flag should be 0 so that we don't waste
// cycles in the callbacks.
_ASSERTE(*pFlag == 0);
}
}
// This sets the JMC status for the entire module.
// fStatus - default status for whole module
void Debugger::SetModuleDefaultJMCStatus(Module * pRuntimeModule, bool fStatus)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(ThisIsHelperThreadWorker());
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO100000, "DM::SetJMCStatus, status=%d, this=%p\n", fStatus, this));
// Ensure that all active DMIs have our status.
// All new DMIs can lookup their status from us.
// This should also update the module count of active JMC DMI's.
DebuggerMethodInfoTable * pTable = g_pDebugger->GetMethodInfoTable();
if (pTable != NULL)
{
Debugger::DebuggerDataLockHolder debuggerDataLockHolder(g_pDebugger);
HASHFIND info;
for (DebuggerMethodInfo *dmi = pTable->GetFirstMethodInfo(&info);
dmi != NULL;
dmi = pTable->GetNextMethodInfo(&info))
{
if (dmi->GetRuntimeModule() == pRuntimeModule)
{
// This DMI is in this module, so update its status
dmi->SetJMCStatus(fStatus);
}
}
}
pRuntimeModule->SetJMCStatus(fStatus);
#ifdef _DEBUG
// If we're disabling JMC in this module, then we shouldn't
// have any active JMC functions.
if (!fStatus)
{
_ASSERTE(!pRuntimeModule->HasAnyJMCFunctions());
}
#endif
}
/******************************************************************************
* Called by GC to determine if it's safe to do a GC.
******************************************************************************/
bool Debugger::ThreadsAtUnsafePlaces(void)
{
LIMITED_METHOD_CONTRACT;
// If we're in shutdown mode, then all other threads are parked.
// Even if they claim to be at unsafe regions, they're still safe to do a GC. They won't touch
// their stacks.
if (m_fShutdownMode)
{
if (m_threadsAtUnsafePlaces > 0)
{
STRESS_LOG1(LF_CORDB, LL_INFO10000, "D::TAUP: Claiming safety in shutdown mode.%d\n", m_threadsAtUnsafePlaces);
}
return false;
}
return (m_threadsAtUnsafePlaces != 0);
}
//
// SendBreakpoint is called by Runtime threads to send that they've
// hit a breakpoint to the Right Side.
//
void Debugger::SendBreakpoint(Thread *thread, CONTEXT *context,
DebuggerBreakpoint *breakpoint)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
#ifdef _DEBUG
static BOOL shouldBreak = -1;
if (shouldBreak == -1)
shouldBreak = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgBreakOnSendBreakpoint);
if (shouldBreak > 0) {
_ASSERTE(!"DbgBreakOnSendBreakpoint");
}
#endif
LOG((LF_CORDB, LL_INFO10000, "D::SB: breakpoint BP:0x%x\n", breakpoint));
_ASSERTE((g_pEEInterface->GetThread() &&
!g_pEEInterface->GetThread()->m_fPreemptiveGCDisabled) ||
g_fInControlC);
_ASSERTE(ThreadHoldsLock());
// Send a breakpoint event to the Right Side
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_BREAKPOINT,
thread,
thread->GetDomain());
ipce->BreakpointData.breakpointToken.Set(breakpoint);
_ASSERTE( breakpoint->m_pAppDomain == ipce->vmAppDomain.GetRawPtr());
m_pRCThread->SendIPCEvent();
}
//---------------------------------------------------------------------------------------
// Send a user breakpoint event for this thread and sycnhronize the process.
//
// Arguments:
// pThread - non-null thread to send user breakpoint event for.
//
// Notes:
// Can't assume that a debugger is attached (since it may detach before we get the lock).
void Debugger::SendUserBreakpointAndSynchronize(Thread * pThread)
{
AtSafePlaceHolder unsafePlaceHolder(pThread);
SENDIPCEVENT_BEGIN(this, pThread);
// Actually send the event
if (CORDebuggerAttached())
{
SendRawUserBreakpoint(pThread);
TrapAllRuntimeThreads();
}
SENDIPCEVENT_END;
}
//---------------------------------------------------------------------------------------
//
// SendRawUserBreakpoint is called by Runtime threads to send that
// they've hit a user breakpoint to the Right Side. This is the event
// send only part, since it can be called from a few different places.
//
// Arguments:
// pThread - [in] managed thread where user break point takes place.
// mus be curernt thread.
//
void Debugger::SendRawUserBreakpoint(Thread * pThread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
PRECONDITION(pThread == GetThread());
PRECONDITION(ThreadHoldsLock());
// Debugger must have been attached to get us to this point.
// We hold the Debugger-lock, so debugger could not have detached from
// underneath us either.
PRECONDITION(CORDebuggerAttached());
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO10000, "D::SRUB: user breakpoint\n"));
// Send a breakpoint event to the Right Side
DebuggerIPCEvent* pEvent = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(pEvent,
DB_IPCE_USER_BREAKPOINT,
pThread,
pThread->GetDomain());
m_pRCThread->SendIPCEvent();
}
//
// SendInterceptExceptionComplete is called by Runtime threads to send that
// they've completed intercepting an exception to the Right Side. This is the event
// send only part, since it can be called from a few different places.
//
void Debugger::SendInterceptExceptionComplete(Thread *thread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO10000, "D::SIEC: breakpoint\n"));
_ASSERTE(!g_pEEInterface->IsPreemptiveGCDisabled());
_ASSERTE(ThreadHoldsLock());
// Send a breakpoint event to the Right Side
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_INTERCEPT_EXCEPTION_COMPLETE,
thread,
thread->GetDomain());
m_pRCThread->SendIPCEvent();
}
//
// SendStep is called by Runtime threads to send that they've
// completed a step to the Right Side.
//
void Debugger::SendStep(Thread *thread, CONTEXT *context,
DebuggerStepper *stepper,
CorDebugStepReason reason)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO10000, "D::SS: step:token:0x%p reason:0x%x\n",
stepper, reason));
_ASSERTE((g_pEEInterface->GetThread() &&
!g_pEEInterface->GetThread()->m_fPreemptiveGCDisabled) ||
g_fInControlC);
_ASSERTE(ThreadHoldsLock());
// Send a step event to the Right Side
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_STEP_COMPLETE,
thread,
thread->GetDomain());
ipce->StepData.stepperToken.Set(stepper);
ipce->StepData.reason = reason;
m_pRCThread->SendIPCEvent();
}
//-------------------------------------------------------------------------------------------------
// Send an EnC remap opportunity and block until it is continued.
//
// dji - current method information
// currentIP - IL offset within that method
// resumeIP - address of a SIZE_T that the RS will write to cross-process if they take the
// remap opportunity. *resumeIP is untouched if the RS does not remap.
//-------------------------------------------------------------------------------------------------
void Debugger::LockAndSendEnCRemapEvent(DebuggerJitInfo * dji, SIZE_T currentIP, SIZE_T *resumeIP)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS; // From SendIPCEvent
PRECONDITION(dji != NULL);
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRE:\n"));
if (CORDBUnrecoverableError(this))
return;
MethodDesc * pFD = dji->m_fd;
// Note that the debugger lock is reentrant, so we may or may not hold it already.
Thread *thread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, thread);
// Send an EnC remap event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_ENC_REMAP,
thread,
thread->GetDomain());
ipce->EnCRemap.currentVersionNumber = dji->m_encVersion;
ipce->EnCRemap.resumeVersionNumber = dji->m_methodInfo->GetCurrentEnCVersion();;
ipce->EnCRemap.currentILOffset = currentIP;
ipce->EnCRemap.resumeILOffset = resumeIP;
ipce->EnCRemap.funcMetadataToken = pFD->GetMemberDef();
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRE: token 0x%x, from version %d to %d\n",
ipce->EnCRemap.funcMetadataToken, ipce->EnCRemap.currentVersionNumber, ipce->EnCRemap.resumeVersionNumber));
Module *pRuntimeModule = pFD->GetModule();
DebuggerModule * pDModule = LookupOrCreateModule(pRuntimeModule, thread->GetDomain());
ipce->EnCRemap.vmDomainFile.SetRawPtr((pDModule ? pDModule->GetDomainFile() : NULL));
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRE: %s::%s "
"dmod:0x%x, methodDef:0x%x \n",
pFD->m_pszDebugClassName, pFD->m_pszDebugMethodName,
pDModule,
ipce->EnCRemap.funcMetadataToken));
// IPC event is now initialized, so we can send it over.
SendSimpleIPCEventAndBlock();
// This will block on the continue
SENDIPCEVENT_END;
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRE: done\n"));
}
// Send the RemapComplete event and block until the debugger Continues
// pFD - specifies the method in which we've remapped into
void Debugger::LockAndSendEnCRemapCompleteEvent(MethodDesc *pFD)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRE:\n"));
if (CORDBUnrecoverableError(this))
return;
Thread *thread = g_pEEInterface->GetThread();
// Note that the debugger lock is reentrant, so we may or may not hold it already.
SENDIPCEVENT_BEGIN(this, thread);
EX_TRY
{
// Ensure the DJI for the latest version of this method has been pre-created.
// It's not clear whether this is necessary or not, but it shouldn't hurt since
// we're going to need to create it anyway since we'll be debugging inside it.
DebuggerJitInfo *dji = g_pDebugger->GetLatestJitInfoFromMethodDesc(pFD);
(void)dji; //prevent "unused variable" error from GCC
_ASSERTE( dji != NULL );
}
EX_CATCH
{
// GetLatestJitInfo could throw on OOM, but the debugger isn't resiliant to OOM.
// I'm not aware of any other legitimate reason why it may throw, so we'll ASSERT
// if it fails.
_ASSERTE(!"Unexpected exception from Debugger::GetLatestJitInfoFromMethodDesc on EnC remap complete");
}
EX_END_CATCH(RethrowTerminalExceptions);
// Send an EnC remap complete event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_ENC_REMAP_COMPLETE,
thread,
thread->GetDomain());
ipce->EnCRemapComplete.funcMetadataToken = pFD->GetMemberDef();
Module *pRuntimeModule = pFD->GetModule();
DebuggerModule * pDModule = LookupOrCreateModule(pRuntimeModule, thread->GetDomain());
ipce->EnCRemapComplete.vmDomainFile.SetRawPtr((pDModule ? pDModule->GetDomainFile() : NULL));
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRC: %s::%s "
"dmod:0x%x, methodDef:0x%x \n",
pFD->m_pszDebugClassName, pFD->m_pszDebugMethodName,
pDModule,
ipce->EnCRemap.funcMetadataToken));
// IPC event is now initialized, so we can send it over.
SendSimpleIPCEventAndBlock();
// This will block on the continue
SENDIPCEVENT_END;
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCRC: done\n"));
}
//
// This function sends a notification to the RS about a specific update that has occurred as part of
// applying an Edit and Continue. We send notification only for function add/update and field add.
// At this point, the EE is already stopped for handling an EnC ApplyChanges operation, so no need
// to take locks etc.
//
void Debugger::SendEnCUpdateEvent(DebuggerIPCEventType eventType,
Module * pModule,
mdToken memberToken,
mdTypeDef classToken,
SIZE_T enCVersion)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCUFE:\n"));
_ASSERTE(eventType == DB_IPCE_ENC_UPDATE_FUNCTION ||
eventType == DB_IPCE_ENC_ADD_FUNCTION ||
eventType== DB_IPCE_ENC_ADD_FIELD);
if (CORDBUnrecoverableError(this))
return;
// Send an EnC UpdateFunction event to the Right Side.
DebuggerIPCEvent* event = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(event,
eventType,
NULL,
NULL);
event->EnCUpdate.newVersionNumber = enCVersion;
event->EnCUpdate.memberMetadataToken = memberToken;
// we have to pass the class token across to the RS because we cannot look it up over
// there based on the added field/method because the metadata on the RS will not yet
// have the changes applied, so the token will not exist in its metadata and we have
// no way to find it.
event->EnCUpdate.classMetadataToken = classToken;
_ASSERTE(pModule);
// we don't support shared assemblies, so must have an appdomain
_ASSERTE(pModule->GetDomain()->IsAppDomain());
DebuggerModule * pDModule = LookupOrCreateModule(pModule, pModule->GetDomain()->AsAppDomain());
event->EnCUpdate.vmDomainFile.SetRawPtr((pDModule ? pDModule->GetDomainFile() : NULL));
m_pRCThread->SendIPCEvent();
LOG((LF_CORDB, LL_INFO10000, "D::LASEnCUE: done\n"));
}
//
// Send a BreakpointSetError event to the Right Side if the given patch is for a breakpoint. Note: we don't care if this
// fails, there is nothing we can do about it anyway, and the breakpoint just wont hit.
//
void Debugger::LockAndSendBreakpointSetError(PATCH_UNORDERED_ARRAY * listUnbindablePatches)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
GC_TRIGGERS;
}
CONTRACTL_END;
_ASSERTE(listUnbindablePatches != NULL);
if (CORDBUnrecoverableError(this))
return;
ULONG count = listUnbindablePatches->Count();
_ASSERTE(count > 0); // must send at least 1 event.
Thread *thread = g_pEEInterface->GetThread();
// Note that the debugger lock is reentrant, so we may or may not hold it already.
SENDIPCEVENT_BEGIN(this, thread);
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
for(ULONG i = 0; i < count; i++)
{
DebuggerControllerPatch *patch = listUnbindablePatches->Table()[i];
_ASSERTE(patch != NULL);
// Only do this for breakpoint controllers
DebuggerController *controller = patch->controller;
if (controller->GetDCType() != DEBUGGER_CONTROLLER_BREAKPOINT)
{
continue;
}
LOG((LF_CORDB, LL_INFO10000, "D::LASBSE:\n"));
// Send a breakpoint set error event to the Right Side.
InitIPCEvent(ipce, DB_IPCE_BREAKPOINT_SET_ERROR, thread, thread->GetDomain());
ipce->BreakpointSetErrorData.breakpointToken.Set(static_cast<DebuggerBreakpoint*> (controller));
// IPC event is now initialized, so we can send it over.
m_pRCThread->SendIPCEvent();
}
// Stop all Runtime threads
TrapAllRuntimeThreads();
// This will block on the continue
SENDIPCEVENT_END;
}
//
// Called from the controller to lock the debugger for event
// sending. This is called before controller events are sent, like
// breakpoint, step complete, and thread started.
//
// Note that it's possible that the debugger detached (and destroyed our IPC
// events) while we're waiting for our turn.
// So Callers should check for that case.
void Debugger::LockForEventSending(DebuggerLockHolder *dbgLockHolder)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
// @todo - Force our parents to bump up the stop-count. That way they can
// guarantee it's balanced.
IncCantStopCount();
_ASSERTE(IsInCantStopRegion());
// What we need is for caller to get the debugger lock
if (dbgLockHolder != NULL)
{
dbgLockHolder->Acquire();
}
#ifdef _DEBUG
// Track our TID. We're not re-entrant.
//_ASSERTE(m_tidLockedForEventSending == 0);
m_tidLockedForEventSending = GetCurrentThreadId();
#endif
}
//
// Called from the controller to unlock the debugger from event
// sending. This is called after controller events are sent, like
// breakpoint, step complete, and thread started.
//
void Debugger::UnlockFromEventSending(DebuggerLockHolder *dbgLockHolder)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
#ifdef _DEBUG
//_ASSERTE(m_tidLockedForEventSending == GetCurrentThreadId());
m_tidLockedForEventSending = 0;
#endif
if (dbgLockHolder != NULL)
{
dbgLockHolder->Release();
}
// @todo - Force our parents to bump up the stop-count. That way they can
// guarantee it's balanced.
_ASSERTE(IsInCantStopRegion());
DecCantStopCount();
}
//
// Called from the controller after all events have been sent for a
// thread to sync the process.
//
void Debugger::SyncAllThreads(DebuggerLockHolder *dbgLockHolder)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::SAT: sync all threads.\n");
Thread *pThread = g_pEEInterface->GetThread();
(void)pThread; //prevent "unused variable" error from GCC
_ASSERTE((pThread &&
!pThread->m_fPreemptiveGCDisabled) ||
g_fInControlC);
_ASSERTE(ThreadHoldsLock());
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
//---------------------------------------------------------------------------------------
// Launch a debugger and then trigger a breakpoint (either managed or native)
//
// Arguments:
// useManagedBPForManagedAttach - TRUE if we should stop with a managed breakpoint
// when managed attached, FALSE if we should always
// stop with a native breakpoint
// pThread - the managed thread that attempts to launch the registered debugger
// pExceptionInfo - the unhandled exception info
// explicitUserRequest - TRUE if this attach is caused by a call to the Debugger.Launch() API.
//
// Returns:
// S_OK on success. Else failure.
//
// Notes:
// This function doesn't try to stop the launched native debugger by calling DebugBreak().
// It sends a breakpoint event only for managed debuggers.
//
HRESULT Debugger::LaunchDebuggerForUser(Thread * pThread, EXCEPTION_POINTERS * pExceptionInfo,
BOOL useManagedBPForManagedAttach, BOOL explicitUserRequest)
{
WRAPPER_NO_CONTRACT;
LOG((LF_CORDB, LL_INFO10000, "D::LDFU: Attaching Debugger.\n"));
//
// Initiate a jit attach
//
JitAttach(pThread, pExceptionInfo, useManagedBPForManagedAttach, explicitUserRequest);
if (useManagedBPForManagedAttach)
{
if(CORDebuggerAttached() && (g_pEEInterface->GetThread() != NULL))
{
//
// Send a managed-breakpoint.
//
SendUserBreakpointAndSynchronize(g_pEEInterface->GetThread());
}
else if (!CORDebuggerAttached() && IsDebuggerPresent())
{
//
// If the registered debugger is not a managed debugger, send a native breakpoint
//
DebugBreak();
}
}
else if(!useManagedBPForManagedAttach)
{
//
// Send a native breakpoint
//
DebugBreak();
}
if (!IsDebuggerPresent())
{
LOG((LF_CORDB, LL_ERROR, "D::LDFU: Failed to launch the debugger.\n"));
}
return S_OK;
}
// The following JDI structures will be passed to a debugger on Vista. Because we do not know when the debugger
// will be done looking at them, and there is at most one debugger attaching to the process, we always set them
// once and leave them set without the risk of clobbering something we care about.
JIT_DEBUG_INFO Debugger::s_DebuggerLaunchJitInfo = {0};
EXCEPTION_RECORD Debugger::s_DebuggerLaunchJitInfoExceptionRecord = {0};
CONTEXT Debugger::s_DebuggerLaunchJitInfoContext = {0};
//----------------------------------------------------------------------------
//
// InitDebuggerLaunchJitInfo - initialize JDI structure on Vista
//
// Arguments:
// pThread - the managed thread with the unhandled excpetion
// pExceptionInfo - unhandled exception info
//
// Return Value:
// None
//
//----------------------------------------------------------------------------
void Debugger::InitDebuggerLaunchJitInfo(Thread * pThread, EXCEPTION_POINTERS * pExceptionInfo)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE((pExceptionInfo != NULL) &&
(pExceptionInfo->ContextRecord != NULL) &&
(pExceptionInfo->ExceptionRecord != NULL));
if ((pExceptionInfo == NULL) || (pExceptionInfo->ContextRecord == NULL) || (pExceptionInfo->ExceptionRecord == NULL))
{
return;
}
s_DebuggerLaunchJitInfoExceptionRecord = *pExceptionInfo->ExceptionRecord;
s_DebuggerLaunchJitInfoContext = *pExceptionInfo->ContextRecord;
s_DebuggerLaunchJitInfo.dwSize = sizeof(s_DebuggerLaunchJitInfo);
s_DebuggerLaunchJitInfo.dwThreadID = pThread == NULL ? GetCurrentThreadId() : pThread->GetOSThreadId();
s_DebuggerLaunchJitInfo.lpExceptionRecord = reinterpret_cast<ULONG64>(&s_DebuggerLaunchJitInfoExceptionRecord);
s_DebuggerLaunchJitInfo.lpContextRecord = reinterpret_cast<ULONG64>(&s_DebuggerLaunchJitInfoContext);
s_DebuggerLaunchJitInfo.lpExceptionAddress = s_DebuggerLaunchJitInfoExceptionRecord.ExceptionAddress != NULL ?
reinterpret_cast<ULONG64>(s_DebuggerLaunchJitInfoExceptionRecord.ExceptionAddress) :
reinterpret_cast<ULONG64>(reinterpret_cast<PVOID>(GetIP(pExceptionInfo->ContextRecord)));
#if defined(_TARGET_X86_)
s_DebuggerLaunchJitInfo.dwProcessorArchitecture = PROCESSOR_ARCHITECTURE_INTEL;
#elif defined(_TARGET_AMD64_)
s_DebuggerLaunchJitInfo.dwProcessorArchitecture = PROCESSOR_ARCHITECTURE_AMD64;
#elif defined(_TARGET_ARM_)
s_DebuggerLaunchJitInfo.dwProcessorArchitecture = PROCESSOR_ARCHITECTURE_ARM;
#elif defined(_TARGET_ARM64_)
s_DebuggerLaunchJitInfo.dwProcessorArchitecture = PROCESSOR_ARCHITECTURE_ARM64;
#else
#error Unknown processor.
#endif
}
//----------------------------------------------------------------------------
//
// GetDebuggerLaunchJitInfo - retrieve the initialized JDI structure on Vista
//
// Arguments:
// None
//
// Return Value:
// JIT_DEBUG_INFO * - pointer to JDI structure
//
//----------------------------------------------------------------------------
JIT_DEBUG_INFO * Debugger::GetDebuggerLaunchJitInfo(void)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE((s_DebuggerLaunchJitInfo.lpExceptionAddress != NULL) &&
(s_DebuggerLaunchJitInfo.lpExceptionRecord != NULL) &&
(s_DebuggerLaunchJitInfo.lpContextRecord != NULL) &&
(((EXCEPTION_RECORD *)(s_DebuggerLaunchJitInfo.lpExceptionRecord))->ExceptionAddress != NULL));
return &s_DebuggerLaunchJitInfo;
}
#endif // !DACCESS_COMPILE
// This function checks the registry for the debug launch setting upon encountering an exception or breakpoint.
DebuggerLaunchSetting Debugger::GetDbgJITDebugLaunchSetting()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
#if FEATURE_PAL
DebuggerLaunchSetting setting = DLS_ATTACH_DEBUGGER;
#else
BOOL bAuto = FALSE;
DebuggerLaunchSetting setting = DLS_ASK_USER;
DWORD cchDbgFormat = MAX_PATH;
INDEBUG(DWORD cchOldDbgFormat = cchDbgFormat);
#if defined(DACCESS_COMPILE)
WCHAR * wszDbgFormat = new (nothrow) WCHAR[cchDbgFormat];
#else
WCHAR * wszDbgFormat = new (interopsafe, nothrow) WCHAR[cchDbgFormat];
#endif // DACCESS_COMPILE
if (wszDbgFormat == NULL)
{
return setting;
}
HRESULT hr = GetDebuggerSettingInfoWorker(wszDbgFormat, &cchDbgFormat, &bAuto);
while (hr == HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER))
{
_ASSERTE(cchDbgFormat > cchOldDbgFormat);
INDEBUG(cchOldDbgFormat = cchDbgFormat);
#if defined(DACCESS_COMPILE)
delete [] wszDbgFormat;
wszDbgFormat = new (nothrow) WCHAR[cchDbgFormat];
#else
DeleteInteropSafe(wszDbgFormat);
wszDbgFormat = new (interopsafe, nothrow) WCHAR[cchDbgFormat];
#endif // DACCESS_COMPILE
if (wszDbgFormat == NULL)
{
return setting;
}
hr = GetDebuggerSettingInfoWorker(wszDbgFormat, &cchDbgFormat, &bAuto);
}
#if defined(DACCESS_COMPILE)
delete [] wszDbgFormat;
#else
DeleteInteropSafe(wszDbgFormat);
#endif // DACCESS_COMPILE
if (SUCCEEDED(hr) && bAuto)
{
setting = DLS_ATTACH_DEBUGGER;
}
#endif // FEATURE_PAL
return setting;
}
// Returns a bitfield reflecting the managed debugging state at the time of
// the jit attach.
CLR_DEBUGGING_PROCESS_FLAGS Debugger::GetAttachStateFlags()
{
LIMITED_METHOD_DAC_CONTRACT;
return (CLR_DEBUGGING_PROCESS_FLAGS)
((m_attachingForManagedEvent ? CLR_DEBUGGING_MANAGED_EVENT_PENDING : 0)
| (m_userRequestedDebuggerLaunch ? CLR_DEBUGGING_MANAGED_EVENT_DEBUGGER_LAUNCH : 0));
}
#ifndef DACCESS_COMPILE
//-----------------------------------------------------------------------------
// Get the full launch string for a jit debugger.
//
// If a jit-debugger is registed, then writes string into pStrArgsBuf and
// return true.
//
// If no jit-debugger is registered, then return false.
//
// Throws on error (like OOM).
//-----------------------------------------------------------------------------
bool Debugger::GetCompleteDebuggerLaunchString(SString * pStrArgsBuf)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
#ifndef FEATURE_PAL
DWORD pid = GetCurrentProcessId();
SString ssDebuggerString;
GetDebuggerSettingInfo(ssDebuggerString, NULL);
if (ssDebuggerString.IsEmpty())
{
// No jit-debugger available. Don't make one up.
return false;
}
// There is no security concern to expect that the debug string we retrieve from HKLM follows a certain
// format because changing HKLM keys requires admin priviledge. Padding with zeros is not a security mitigation,
// but rather a forward looking compability measure. If future verions of Windows introduces more parameters for
// JIT debugger launch, it is preferrable to pass zeros than other random values for those unsupported parameters.
pStrArgsBuf->Printf(ssDebuggerString, pid, GetUnmanagedAttachEvent(), GetDebuggerLaunchJitInfo(), 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
return true;
#else // !FEATURE_PAL
return false;
#endif // !FEATURE_PAL
}
// Proxy code for EDA
struct EnsureDebuggerAttachedParams
{
Debugger * m_pThis;
HRESULT m_retval;
PROCESS_INFORMATION * m_pProcessInfo;
EnsureDebuggerAttachedParams() :
m_pThis(NULL), m_retval(E_FAIL), m_pProcessInfo(NULL) {LIMITED_METHOD_CONTRACT; }
};
// This is called by the helper thread
void EDAHelperStub(EnsureDebuggerAttachedParams * p)
{
WRAPPER_NO_CONTRACT;
p->m_retval = p->m_pThis->EDAHelper(p->m_pProcessInfo);
}
// This gets called just like the normal version, but it sends the call over to the helper thread
HRESULT Debugger::EDAHelperProxy(PROCESS_INFORMATION * pProcessInfo)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
}
CONTRACTL_END;
_ASSERTE(!ThisIsHelperThreadWorker());
_ASSERTE(ThreadHoldsLock());
HRESULT hr = LazyInitWrapper();
if (FAILED(hr))
{
// We already stress logged this case.
return hr;
}
if (!IsGuardPageGone())
{
return EDAHelper(pProcessInfo);
}
EnsureDebuggerAttachedParams p;
p.m_pThis = this;
p.m_pProcessInfo = pProcessInfo;
LOG((LF_CORDB, LL_INFO1000000, "D::EDAHelperProxy\n"));
m_pRCThread->DoFavor((FAVORCALLBACK) EDAHelperStub, &p);
LOG((LF_CORDB, LL_INFO1000000, "D::EDAHelperProxy return\n"));
return p.m_retval;
}
// E_ABORT - if the attach was declined
// S_OK - Jit-attach successfully started
HRESULT Debugger::EDAHelper(PROCESS_INFORMATION *pProcessInfo)
{
CONTRACTL
{
NOTHROW;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
PRECONDITION(ThisMaybeHelperThread()); // on helper if stackoverflow.
}
CONTRACTL_END;
#ifndef FEATURE_PAL
LOG((LF_CORDB, LL_INFO10000, "D::EDA: thread 0x%x is launching the debugger.\n", GetCurrentThreadId()));
_ASSERTE(HasLazyData());
// Another potential hang. This may get run on the helper if we have a stack overflow.
// Hopefully the odds of 1 thread hitting a stack overflow while another is stuck holding the heap
// lock is very small.
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
BOOL fCreateSucceeded = FALSE;
StackSString strDbgCommand;
const WCHAR * wszDbgCommand = NULL;
SString strCurrentDir;
const WCHAR * wszCurrentDir = NULL;
EX_TRY
{
// Get the debugger to launch. The returned string is via the strDbgCommand out param. Throws on error.
bool fHasDebugger = GetCompleteDebuggerLaunchString(&strDbgCommand);
if (fHasDebugger)
{
wszDbgCommand = strDbgCommand.GetUnicode();
_ASSERTE(wszDbgCommand != NULL); // would have thrown on oom.
LOG((LF_CORDB, LL_INFO10000, "D::EDA: launching with command [%S]\n", wszDbgCommand));
ClrGetCurrentDirectory(strCurrentDir);
wszCurrentDir = strCurrentDir.GetUnicode();
}
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions);
STARTUPINFOW startupInfo = {0};
startupInfo.cb = sizeof(STARTUPINFOW);
DWORD errCreate = 0;
if (wszDbgCommand != NULL)
{
// Create the debugger process
// When we are launching an debugger, we need to let the child process inherit our handles.
// This is necessary for the debugger to signal us that the attach is complete.
fCreateSucceeded = WszCreateProcess(NULL, const_cast<WCHAR*> (wszDbgCommand),
NULL, NULL,
TRUE,
CREATE_NEW_CONSOLE,
NULL, wszCurrentDir,
&startupInfo,
pProcessInfo);
errCreate = GetLastError();
}
if (!fCreateSucceeded)
{
LOG((LF_CORDB, LL_INFO10000, "D::EDA: debugger did not launch successfully.\n"));
return E_ABORT;
}
LOG((LF_CORDB, LL_INFO10000, "D::EDA: debugger launched successfully.\n"));
return S_OK;
#else // !FEATURE_PAL
return E_ABORT;
#endif // !FEATURE_PAL
}
// ---------------------------------------------------------------------------------------------------------------------
// This function decides who wins the race for any jit attach and marks the appropriate state that a jit
// attach is in progress.
//
// Arguments
// willSendManagedEvent - indicates whether or not we plan to send a managed debug event after the jit attach
// explicitUserRequest - TRUE if this attach is caused by a call to the Debugger.Launch() API.
//
// Returns
// TRUE - if some other thread already has jit attach in progress -> this thread should block until that is complete
// FALSE - this is the first thread to jit attach -> this thread should launch the debugger
//
//
BOOL Debugger::PreJitAttach(BOOL willSendManagedEvent, BOOL willLaunchDebugger, BOOL explicitUserRequest)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
PRECONDITION(!ThisIsHelperThreadWorker());
}
CONTRACTL_END;
LOG( (LF_CORDB, LL_INFO10000, "D::PreJA: Entering\n") );
// Multiple threads may be calling this, so need to take the lock.
if(!m_jitAttachInProgress)
{
// TODO: This is a known deadlock! Debugger::PreJitAttach is called during WatsonLastChance.
// If the event (exception/crash) happens while this thread is holding the ThreadStore
// lock, we may deadlock if another thread holds the DebuggerMutex and is waiting on
// the ThreadStore lock. The DebuggerMutex has to be broken into two smaller locks
// so that you can take that lock here when holding the ThreadStore lock.
DebuggerLockHolder dbgLockHolder(this);
if (!m_jitAttachInProgress)
{
m_jitAttachInProgress = TRUE;
m_attachingForManagedEvent = willSendManagedEvent;
m_launchingDebugger = willLaunchDebugger;
m_userRequestedDebuggerLaunch = explicitUserRequest;
ResetEvent(GetUnmanagedAttachEvent());
ResetEvent(GetAttachEvent());
LOG( (LF_CORDB, LL_INFO10000, "D::PreJA: Leaving - first thread\n") );
return TRUE;
}
}
LOG( (LF_CORDB, LL_INFO10000, "D::PreJA: Leaving - following thread\n") );
return FALSE;
}
//---------------------------------------------------------------------------------------------------------------------
// This function gets the jit debugger launched and waits for the native attach to complete
// Make sure you called PreJitAttach and it returned TRUE before you call this
//
// Arguments:
// pThread - the managed thread with the unhandled excpetion
// pExceptionInfo - the unhandled exception info
//
// Returns:
// S_OK if the debugger was launched successfully and a failing HRESULT otherwise
//
HRESULT Debugger::LaunchJitDebuggerAndNativeAttach(Thread * pThread, EXCEPTION_POINTERS * pExceptionInfo)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
PRECONDITION(!ThisIsHelperThreadWorker());
}
CONTRACTL_END;
// You need to have called PreJitAttach first to determine which thread gets to launch the debugger
_ASSERTE(m_jitAttachInProgress);
LOG( (LF_CORDB, LL_INFO10000, "D::LJDANA: Entering\n") );
PROCESS_INFORMATION processInfo = {0};
DebuggerLockHolder dbgLockHolder(this);
// <TODO>
// If the JIT debugger failed to launch or if there is no JIT debugger, EDAHelperProxy will
// switch to preemptive GC mode to display a dialog to the user indicating the JIT debugger
// was unavailable. There are some rare cases where this could cause a deadlock with the
// debugger lock; however these are rare enough that fixing this doesn't meet the bar for
// Whidbey at this point. We might want to revisit this later however.
// </TODO>
CONTRACT_VIOLATION(GCViolation);
{
LOG((LF_CORDB, LL_INFO1000, "D::EDA: Initialize JDI.\n"));
EXCEPTION_POINTERS exceptionPointer;
EXCEPTION_RECORD exceptionRecord;
CONTEXT context;
if (pExceptionInfo == NULL)
{
ZeroMemory(&exceptionPointer, sizeof(exceptionPointer));
ZeroMemory(&exceptionRecord, sizeof(exceptionRecord));
ZeroMemory(&context, sizeof(context));
context.ContextFlags = CONTEXT_CONTROL;
ClrCaptureContext(&context);
exceptionRecord.ExceptionAddress = reinterpret_cast<PVOID>(GetIP(&context));
exceptionPointer.ContextRecord = &context;
exceptionPointer.ExceptionRecord = &exceptionRecord;
pExceptionInfo = &exceptionPointer;
}
InitDebuggerLaunchJitInfo(pThread, pExceptionInfo);
}
// This will make the CreateProcess call to create the debugger process.
// We then expect that the debugger process will turn around and attach to us.
HRESULT hr = EDAHelperProxy(&processInfo);
if(FAILED(hr))
{
return hr;
}
LOG((LF_CORDB, LL_INFO10000, "D::LJDANA: waiting on m_exUnmanagedAttachEvent and debugger's process handle\n"));
DWORD dwHandles = 2;
HANDLE arrHandles[2];
arrHandles[0] = GetUnmanagedAttachEvent();
arrHandles[1] = processInfo.hProcess;
// Let the helper thread do the attach logic for us and wait for the
// attach event. Must release the lock before blocking on a wait.
dbgLockHolder.Release();
// Wait for one or the other to be set. Multiple threads could be waiting here.
// The events are manual events, so when they go high, all threads will be released.
DWORD res = WaitForMultipleObjectsEx(dwHandles, arrHandles, FALSE, INFINITE, FALSE);
// We no long need to keep handles to the debugger process.
CloseHandle(processInfo.hProcess);
CloseHandle(processInfo.hThread);
// Indicate to the caller that the attach was aborted
if (res == WAIT_OBJECT_0 + 1)
{
LOG((LF_CORDB, LL_INFO10000, "D::LJDANA: Debugger process is unexpectedly terminated!\n"));
return E_FAIL;
}
// Otherwise, attach was successful (Note, only native attach is done so far)
_ASSERTE((res == WAIT_OBJECT_0) && "WaitForMultipleObjectsEx failed!");
LOG( (LF_CORDB, LL_INFO10000, "D::LJDANA: Leaving\n") );
return S_OK;
}
// Blocks until the debugger completes jit attach
void Debugger::WaitForDebuggerAttach()
{
LIMITED_METHOD_CONTRACT;
LOG( (LF_CORDB, LL_INFO10000, "D::WFDA:Entering\n") );
// if this thread previously called LaunchDebuggerAndNativeAttach then this wait is spurious,
// the event is still set and it continues immediately. If this is an auxilliary thread however
// then the wait is necessary
// If we are not launching the debugger (e.g. unhandled exception on Win7), then we should not
// wait on the unmanaged attach event. If the debugger is launched by the OS, then the unmanaged
// attach event passed to the debugger is created by the OS, not by us, so our event will never
// be signaled.
if (m_launchingDebugger)
{
WaitForSingleObject(GetUnmanagedAttachEvent(), INFINITE);
}
// Wait until the pending managed debugger attach is completed
if (CORDebuggerPendingAttach() && !CORDebuggerAttached())
{
LOG( (LF_CORDB, LL_INFO10000, "D::WFDA: Waiting for managed attach too\n") );
WaitForSingleObject(GetAttachEvent(), INFINITE);
}
// We can't reset the event here because some threads may
// be just about to wait on it. If we reset it before the
// other threads hit the wait, they'll block.
// We have an innate race here that can't easily fix. The best
// we can do is have a super small window (by moving the reset as
// far out this making it very unlikely that a thread will
// hit the window.
LOG( (LF_CORDB, LL_INFO10000, "D::WFDA: Leaving\n") );
}
// Cleans up after jit attach is complete
void Debugger::PostJitAttach()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
PRECONDITION(!ThisIsHelperThreadWorker());
}
CONTRACTL_END;
LOG( (LF_CORDB, LL_INFO10000, "D::PostJA: Entering\n") );
// Multiple threads may be calling this, so need to take the lock.
DebuggerLockHolder dbgLockHolder(this);
// clear the attaching flags which allows other threads to initiate jit attach if needed
m_jitAttachInProgress = FALSE;
m_attachingForManagedEvent = FALSE;
m_launchingDebugger = FALSE;
m_userRequestedDebuggerLaunch = FALSE;
// set the attaching events to unblock other threads waiting on this attach
// regardless of whether or not it completed
SetEvent(GetUnmanagedAttachEvent());
SetEvent(GetAttachEvent());
LOG( (LF_CORDB, LL_INFO10000, "D::PostJA: Leaving\n") );
}
//---------------------------------------------------------------------------------------
// Launches a debugger and blocks waiting for it to either attach or abort the attach.
//
// Arguments:
// pThread - the managed thread with the unhandled excpetion
// pExceptionInfo - the unhandled exception info
// willSendManagedEvent - TRUE if after getting attached we will send a managed debug event
// explicitUserRequest - TRUE if this attach is caused by a call to the Debugger.Launch() API.
//
// Returns:
// None. Callers can requery if a debugger is attached.
//
// Assumptions:
// This may be called by multiple threads, each firing their own debug events. This function will handle locking.
// Thus this could block for an arbitrary length of time:
// - may need to prompt the user to decide if an attach occurs.
// - may block waiting for a debugger to attach.
//
// Notes:
// The launch string is retrieved from code:GetDebuggerSettingInfo.
// This will not do a sync-complete. Instead, the caller can send a debug event (the jit-attach
// event, such as a User-breakpoint or unhandled exception) and that can send a sync-complete,
// just as if the debugger was always attached. This ensures that the jit-attach event is in the
// same callback queue as any faked-up events that the Right-side Shim creates.
//
void Debugger::JitAttach(Thread * pThread, EXCEPTION_POINTERS * pExceptionInfo, BOOL willSendManagedEvent, BOOL explicitUserRequest)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(!ThisIsHelperThreadWorker()); // Must be a managed thread
}
CONTRACTL_END;
if (IsDebuggerPresent())
return;
GCX_PREEMP_EEINTERFACE_TOGGLE_IFTHREAD();
EnsureDebuggerAttached(pThread, pExceptionInfo, willSendManagedEvent, explicitUserRequest);
}
//-----------------------------------------------------------------------------
// Ensure that a debugger is attached. Will jit-attach if needed.
//
// Arguments
// pThread - the managed thread with the unhandled excpetion
// pExceptionInfo - the unhandled exception info
// willSendManagedEvent - true if after getting (or staying) attached we will send
// a managed debug event
// explicitUserRequest - true if this attach is caused by a call to the
// Debugger.Launch() API.
//
// Returns:
// None. Either a debugger is attached or it is not.
//
// Notes:
// There are several intermediate possible outcomes:
// - Debugger already attached before this was called.
// - JIT-atttach debugger spawned, and attached successfully.
// - JIT-attach debugger spawned, but declined to attach.
// - Failed to spawn jit-attach debugger.
//
// Ultimately, the only thing that matters at the end is whether a debugger
// is now attached, which is retreived via CORDebuggerAttached().
//-----------------------------------------------------------------------------
void Debugger::EnsureDebuggerAttached(Thread * pThread, EXCEPTION_POINTERS * pExceptionInfo, BOOL willSendManagedEvent, BOOL explicitUserRequest)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
MODE_PREEMPTIVE;
PRECONDITION(!ThisIsHelperThreadWorker());
}
CONTRACTL_END;
LOG( (LF_CORDB,LL_INFO10000,"D::EDA\n") );
HRESULT hr = S_OK;
// We could be in three states:
// 1) no debugger attached
// 2) native attached but not managed (yet?)
// 3) native attached and managed
// There is a race condition here that can be hit if multiple threads
// were to trigger jit attach at the right time
// Thread 1 starts jit attach
// Thread 2 also starts jit attach and gets to waiting for the attach complete
// Thread 1 rapidly completes the jit attach then starts it again
// Thread 2 may still be waiting from the first jit attach at this point
//
// Note that this isn't all that bad because if the debugger hasn't actually detached
// in the middle then the second jit attach will complete almost instantly and thread 2
// is unblocked. If the debugger did detach in the middle then it seems reasonable for
// thread 2 to continue to wait until until the debugger is attached once again for the
// second attach. Basically if one jit attach completes and restarts fast enough it might
// just go unnoticed by some threads and it will be as if it never happened. Doesn't seem
// that bad as long as we know another jit attach is again in progress.
BOOL startedJitAttach = FALSE;
// First check to see if we need to launch the debugger ourselves
if(PreJitAttach(willSendManagedEvent, TRUE, explicitUserRequest))
{
// if the debugger is already attached then we can't launch one
// and whatever attach state we are in is just what we get
if(IsDebuggerPresent())
{
// unblock other threads waiting on our attach and clean up
PostJitAttach();
return;
}
else
{
hr = LaunchJitDebuggerAndNativeAttach(pThread, pExceptionInfo);
if(FAILED(hr))
{
// unblock other threads waiting on our attach and clean up
PostJitAttach();
return;
}
}
startedJitAttach = TRUE;
}
// at this point someone should have launched the native debugger and
// it is somewhere between not attached and attach complete
// (it might have even been completely attached before this function even started)
// step 2 - wait for the attach to complete
WaitForDebuggerAttach();
// step 3 - if we initiated then we also cleanup
if(startedJitAttach)
PostJitAttach();
LOG( (LF_CORDB, LL_INFO10000, "D::EDA:Leaving\n") );
}
// Proxy code for AttachDebuggerForBreakpoint
// Structure used in the proxy function callback
struct SendExceptionOnHelperThreadParams
{
Debugger *m_pThis;
HRESULT m_retval;
Thread *m_pThread;
OBJECTHANDLE m_exceptionHandle;
bool m_continuable;
FramePointer m_framePointer;
SIZE_T m_nOffset;
CorDebugExceptionCallbackType m_eventType;
DWORD m_dwFlags;
SendExceptionOnHelperThreadParams() :
m_pThis(NULL),
m_retval(S_OK),
m_pThread(NULL)
{LIMITED_METHOD_CONTRACT; }
};
//**************************************************************************
// This function sends Exception and ExceptionCallback2 event.
//
// Arguments:
// pThread : managed thread which exception takes place
// exceptionHandle : handle to the managed exception object (usually
// something derived from System.Exception)
// fContinuable : true iff continuable
// framePointer : frame pointer associated with callback.
// nOffset : il offset associated with callback.
// eventType : type of callback
// dwFlags : additional flags (see CorDebugExceptionFlags).
//
// Returns:
// S_OK on sucess. Else some error. May also throw.
//
// Notes:
// This is a helper for code:Debugger.SendExceptionEventsWorker.
// See code:Debugger.SendException for more details about parameters.
// This is always called on a managed thread (never the helper thread)
// This will synchronize and block.
//**************************************************************************
HRESULT Debugger::SendExceptionHelperAndBlock(
Thread *pThread,
OBJECTHANDLE exceptionHandle,
bool fContinuable,
FramePointer framePointer,
SIZE_T nOffset,
CorDebugExceptionCallbackType eventType,
DWORD dwFlags)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(CheckPointer(pThread));
}
CONTRACTL_END;
HRESULT hr = S_OK;
// This is a normal event to send from LS to RS
SENDIPCEVENT_BEGIN(this, pThread);
// This function can be called on helper thread or managed thread.
// However, we should be holding locks upon entry
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
//
// Send pre-Whidbey EXCEPTION IPC event.
//
InitIPCEvent(ipce, DB_IPCE_EXCEPTION, pThread, pThread->GetDomain());
ipce->Exception.vmExceptionHandle.SetRawPtr(exceptionHandle);
ipce->Exception.firstChance = (eventType == DEBUG_EXCEPTION_FIRST_CHANCE);
ipce->Exception.continuable = fContinuable;
hr = m_pRCThread->SendIPCEvent();
_ASSERTE(SUCCEEDED(hr) && "D::SE: Send ExceptionCallback event failed.");
//
// Send Whidbey EXCEPTION IPC event.
//
InitIPCEvent(ipce, DB_IPCE_EXCEPTION_CALLBACK2, pThread, pThread->GetDomain());
ipce->ExceptionCallback2.framePointer = framePointer;
ipce->ExceptionCallback2.eventType = eventType;
ipce->ExceptionCallback2.nOffset = nOffset;
ipce->ExceptionCallback2.dwFlags = dwFlags;
ipce->ExceptionCallback2.vmExceptionHandle.SetRawPtr(exceptionHandle);
LOG((LF_CORDB, LL_INFO10000, "D::SE: sending ExceptionCallback2 event"));
hr = m_pRCThread->SendIPCEvent();
if (eventType == DEBUG_EXCEPTION_FIRST_CHANCE)
{
pThread->GetExceptionState()->GetFlags()->SetSentDebugFirstChance();
}
else
{
_ASSERTE(eventType == DEBUG_EXCEPTION_UNHANDLED);
}
_ASSERTE(SUCCEEDED(hr) && "D::SE: Send ExceptionCallback2 event failed.");
if (SUCCEEDED(hr))
{
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
// Let other Runtime threads handle their events.
SENDIPCEVENT_END;
return hr;
}
// Send various first-chance / unhandled exception events.
//
// Assumptions:
// Caller has already determined that we want to send exception events.
//
// Notes:
// This is a helper function for code:Debugger.SendException
void Debugger::SendExceptionEventsWorker(
Thread * pThread,
bool fFirstChance,
bool fIsInterceptable,
bool fContinuable,
SIZE_T currentIP,
FramePointer framePointer,
bool atSafePlace)
{
HRESULT hr = S_OK;
ThreadExceptionState* pExState = pThread->GetExceptionState();
//
// Figure out parameters to the IPC events.
//
const BYTE *ip;
SIZE_T nOffset = (SIZE_T)ICorDebugInfo::NO_MAPPING;
DebuggerMethodInfo *pDebugMethodInfo = NULL;
// If we're passed a zero IP or SP, then go to the ThreadExceptionState on the thread to get the data. Note:
// we can only do this if there is a context in the pExState. There are cases (most notably the
// EEPolicy::HandleFatalError case) where we don't have that. So we just leave the IP/SP 0.
if ((currentIP == 0) && (pExState->GetContextRecord() != NULL))
{
ip = (BYTE *)GetIP(pExState->GetContextRecord());
}
else
{
ip = (BYTE *)currentIP;
}
if (g_pEEInterface->IsManagedNativeCode(ip))
{
MethodDesc *pMethodDesc = g_pEEInterface->GetNativeCodeMethodDesc(PCODE(ip));
_ASSERTE(pMethodDesc != NULL);
if (pMethodDesc != NULL)
{
DebuggerJitInfo *pDebugJitInfo = GetJitInfo(pMethodDesc, ip, &pDebugMethodInfo);
if (pDebugJitInfo != NULL)
{
SIZE_T nativeOffset = CodeRegionInfo::GetCodeRegionInfo(pDebugJitInfo, pMethodDesc).AddressToOffset(ip);
CorDebugMappingResult mapResult;
DWORD which;
nOffset = pDebugJitInfo->MapNativeOffsetToIL(nativeOffset, &mapResult, &which);
}
}
}
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
if (fFirstChance)
{
// We can call into this method when there is no exception in progress to alert
// the debugger to a stack overflow, however that case should never specify first
// chance. An exception must be in progress to check the flags on the exception state
_ASSERTE(pThread->IsExceptionInProgress());
//
// Send the first chance exception if we have not already and if it is not suppressed
//
if (m_sendExceptionsOutsideOfJMC && !pExState->GetFlags()->SentDebugFirstChance())
{
// Blocking here is especially important so that the debugger can mark any code as JMC.
hr = SendExceptionHelperAndBlock(
pThread,
g_pEEInterface->GetThreadException(pThread),
fContinuable,
framePointer,
nOffset,
DEBUG_EXCEPTION_FIRST_CHANCE,
fIsInterceptable ? DEBUG_EXCEPTION_CAN_BE_INTERCEPTED : 0);
{
// Toggle GC into COOP to block this thread.
GCX_COOP_EEINTERFACE();
//
// If we weren't at a safe place when we enabled PGC, then go ahead and unmark that fact now that we've successfully
// disabled.
//
if (!atSafePlace)
{
g_pDebugger->DecThreadsAtUnsafePlaces();
}
ProcessAnyPendingEvals(pThread);
//
// If we weren't at a safe place, increment the unsafe count before we enable preemptive mode.
//
if (!atSafePlace)
{
g_pDebugger->IncThreadsAtUnsafePlaces();
}
} // end of GCX_CCOP_EEINTERFACE();
} //end if (m_sendExceptionsOutsideOfJMC && !SentDebugFirstChance())
//
// If this is a JMC function, then we send a USER's first chance as well.
//
if ((pDebugMethodInfo != NULL) &&
pDebugMethodInfo->IsJMCFunction() &&
!pExState->GetFlags()->SentDebugUserFirstChance())
{
SENDIPCEVENT_BEGIN(this, pThread);
InitIPCEvent(ipce, DB_IPCE_EXCEPTION_CALLBACK2, pThread, pThread->GetDomain());
ipce->ExceptionCallback2.framePointer = framePointer;
ipce->ExceptionCallback2.eventType = DEBUG_EXCEPTION_USER_FIRST_CHANCE;
ipce->ExceptionCallback2.nOffset = nOffset;
ipce->ExceptionCallback2.dwFlags = fIsInterceptable ? DEBUG_EXCEPTION_CAN_BE_INTERCEPTED : 0;
ipce->ExceptionCallback2.vmExceptionHandle.SetRawPtr(g_pEEInterface->GetThreadException(pThread));
LOG((LF_CORDB, LL_INFO10000, "D::SE: sending ExceptionCallback2 (USER FIRST CHANCE)"));
hr = m_pRCThread->SendIPCEvent();
_ASSERTE(SUCCEEDED(hr) && "D::SE: Send ExceptionCallback2 (User) event failed.");
if (SUCCEEDED(hr))
{
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
pExState->GetFlags()->SetSentDebugUserFirstChance();
// Let other Runtime threads handle their events.
SENDIPCEVENT_END;
} // end if (!SentDebugUserFirstChance)
} // end if (firstChance)
else
{
// unhandled exception case
// if there is no exception in progress then we are sending a fake exception object
// as an indication of a fatal error (stack overflow). In this case it is illegal
// to read GetFlags() from the exception state.
// else if there is an exception in progress we only want to send the notification if
// we did not already send a CHF, previous unhandled, or unwind begin notification
BOOL sendNotification = TRUE;
if(pThread->IsExceptionInProgress())
{
sendNotification = !pExState->GetFlags()->DebugCatchHandlerFound() &&
!pExState->GetFlags()->SentDebugUnhandled() &&
!pExState->GetFlags()->SentDebugUnwindBegin();
}
if(sendNotification)
{
hr = SendExceptionHelperAndBlock(
pThread,
g_pEEInterface->GetThreadException(pThread),
fContinuable,
LEAF_MOST_FRAME,
(SIZE_T)ICorDebugInfo::NO_MAPPING,
DEBUG_EXCEPTION_UNHANDLED,
fIsInterceptable ? DEBUG_EXCEPTION_CAN_BE_INTERCEPTED : 0);
if(pThread->IsExceptionInProgress())
{
pExState->GetFlags()->SetSentDebugUnhandled();
}
}
} // end if (!firstChance)
}
//
// SendException is called by Runtime threads to send that they've hit an Managed exception to the Right Side.
// This may block this thread and suspend the debuggee, and let the debugger inspect us.
//
// The thread's throwable should be set so that the debugger can inspect the current exception.
// It does not report native exceptions in native code (which is consistent because those don't have a
// managed exception object).
//
// This may kick off a jit-attach (in which case fAttaching==true), and so may be called even when no debugger
// is yet involved.
//
// Parameters:
// pThread - the thread throwing the exception.
// fFirstChance - true if this is a first chance exception. False if this is an unhandled exception.
// currentIP - absolute native address of the exception if it is from managed code. If this is 0, we try to find it
// based off the thread's current exception state.
// currentSP - stack pointer of the exception. This will get converted into a FramePointer and then used by the debugger
// to identify which stack frame threw the exception.
// currentBSP - additional information for IA64 only to identify the stack frame.
// fContinuable - not used.
// fAttaching - true iff this exception may initiate a jit-attach. In the common case, if this is true, then
// CorDebuggerAttached() is false. However, since a debugger can attach at any time, it's possible
// for another debugger to race against the jit-attach and win. Thus this may err on the side of being true.
// fForceNonInterceptable - This is used to determine if the exception is continuable (ie "Interceptible",
// we can handle a DB_IPCE_INTERCEPT_EXCEPTION event for it). If true, then the exception can not be continued.
// If false, we get continuation status from the exception properties of the current thread.
//
// Returns:
// S_OK on success (common case by far).
// propogates other errors.
//
HRESULT Debugger::SendException(Thread *pThread,
bool fFirstChance,
SIZE_T currentIP,
SIZE_T currentSP,
bool fContinuable, // not used by RS.
bool fAttaching,
bool fForceNonInterceptable,
EXCEPTION_POINTERS * pExceptionInfo)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(HasLazyData());
PRECONDITION(CheckPointer(pThread));
PRECONDITION((pThread->GetFilterContext() == NULL) || !fFirstChance);
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::SendException\n"));
if (CORDBUnrecoverableError(this))
{
return (E_FAIL);
}
// Mark if we're at an unsafe place.
AtSafePlaceHolder unsafePlaceHolder(pThread);
// Grab the exception name from the current exception object to pass to the JIT attach.
bool fIsInterceptable;
if (fForceNonInterceptable)
{
fIsInterceptable = false;
m_forceNonInterceptable = true;
}
else
{
fIsInterceptable = IsInterceptableException(pThread);
m_forceNonInterceptable = false;
}
ThreadExceptionState* pExState = pThread->GetExceptionState();
BOOL managedEventNeeded = ((!fFirstChance) ||
(fFirstChance && (!pExState->GetFlags()->SentDebugFirstChance() || !pExState->GetFlags()->SentDebugUserFirstChance())));
// There must be a managed exception object to send a managed exception event
if (g_pEEInterface->IsThreadExceptionNull(pThread) && (pThread->LastThrownObjectHandle() == NULL))
{
managedEventNeeded = FALSE;
}
if (fAttaching)
{
JitAttach(pThread, pExceptionInfo, managedEventNeeded, FALSE);
// If the jit-attach occured, CORDebuggerAttached() may now be true and we can
// just act as if a debugger was always attached.
}
if(managedEventNeeded)
{
{
// We have to send enabled, so enable now.
GCX_PREEMP_EEINTERFACE();
// Send the exception events. Even in jit-attach case, we should now be fully attached.
if (CORDebuggerAttached())
{
// Initialize frame-pointer associated with exception notification.
LPVOID stackPointer;
if ((currentSP == 0) && (pExState->GetContextRecord() != NULL))
{
stackPointer = dac_cast<PTR_VOID>(GetSP(pExState->GetContextRecord()));
}
else
{
stackPointer = (LPVOID)currentSP;
}
FramePointer framePointer = FramePointer::MakeFramePointer(stackPointer);
// Do the real work of sending the events
SendExceptionEventsWorker(
pThread,
fFirstChance,
fIsInterceptable,
fContinuable,
currentIP,
framePointer,
!unsafePlaceHolder.IsAtUnsafePlace());
}
else
{
LOG((LF_CORDB,LL_INFO100, "D:SE: Skipping SendIPCEvent because not supposed to send anything, or RS detached.\n"));
}
}
// If we weren't at a safe place when we switched to PREEMPTIVE, then go ahead and unmark that fact now
// that we're successfully back in COOPERATIVE mode.
unsafePlaceHolder.Clear();
{
GCX_COOP_EEINTERFACE();
ProcessAnyPendingEvals(pThread);
}
}
if (CORDebuggerAttached())
{
return S_FALSE;
}
else
{
return S_OK;
}
}
/*
* ProcessAnyPendingEvals
*
* This function checks for, and then processes, any pending func-evals.
*
* Parameters:
* pThread - The thread to process.
*
* Returns:
* None.
*
*/
void Debugger::ProcessAnyPendingEvals(Thread *pThread)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_COOPERATIVE;
}
CONTRACTL_END;
#ifndef DACCESS_COMPILE
// If no debugger is attached, then no evals to process.
// We may get here in oom situations during jit-attach, so we'll check now and be safe.
if (!CORDebuggerAttached())
{
return;
}
//
// Note: if there is a filter context installed, we may need remove it, do the eval, then put it back. I'm not 100%
// sure which yet... it kinda depends on whether or not we really need the filter context updated due to a
// collection during the func eval...
//
// If we need to do a func eval on this thread, then there will be a pending eval registered for this thread. We'll
// loop so long as there are pending evals registered. We block in FuncEvalHijackWorker after sending up the
// FuncEvalComplete event, so if the user asks for another func eval then there will be a new pending eval when we
// loop and check again.
//
DebuggerPendingFuncEval *pfe;
while (GetPendingEvals() != NULL && (pfe = GetPendingEvals()->GetPendingEval(pThread)) != NULL)
{
DebuggerEval *pDE = pfe->pDE;
_ASSERTE(pDE->m_evalDuringException);
_ASSERTE(pDE->m_thread == GetThread());
// Remove the pending eval from the hash. This ensures that if we take a first chance exception during the eval
// that we can do another nested eval properly.
GetPendingEvals()->RemovePendingEval(pThread);
// Go ahead and do the pending func eval. pDE is invalid after this.
void *ret;
ret = ::FuncEvalHijackWorker(pDE);
// The return value should be NULL when FuncEvalHijackWorker is called as part of an exception.
_ASSERTE(ret == NULL);
}
// If we need to re-throw a ThreadAbortException, go ahead and do it now.
if (GetThread()->m_StateNC & Thread::TSNC_DebuggerReAbort)
{
// Now clear the bit else we'll see it again when we process the Exception notification
// from this upcoming UserAbort exception.
pThread->ResetThreadStateNC(Thread::TSNC_DebuggerReAbort);
pThread->UserAbort(Thread::TAR_Thread, EEPolicy::TA_Safe, INFINITE, Thread::UAC_Normal);
}
#endif
}
/*
* FirstChanceManagedException is called by Runtime threads when crawling the managed stack frame
* for a handler for the exception. It is called for each managed call on the stack.
*
* Parameters:
* pThread - The thread the exception is occurring on.
* currentIP - the IP in the current stack frame.
* currentSP - the SP in the current stack frame.
*
* Returns:
* Always FALSE.
*
*/
bool Debugger::FirstChanceManagedException(Thread *pThread, SIZE_T currentIP, SIZE_T currentSP)
{
// @@@
// Implement DebugInterface
// Can only be called from EE/exception
// must be on managed thread.
CONTRACTL
{
THROWS;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
PRECONDITION(CORDebuggerAttached());
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::FCE: First chance exception, TID:0x%x, \n", GetThreadIdHelper(pThread)));
_ASSERTE(GetThread() != NULL);
#ifdef _DEBUG
static ConfigDWORD d_fce;
if (d_fce.val(CLRConfig::INTERNAL_D__FCE))
_ASSERTE(!"Stop in Debugger::FirstChanceManagedException?");
#endif
SendException(pThread, TRUE, currentIP, currentSP, FALSE, FALSE, FALSE, NULL);
return false;
}
/*
* FirstChanceManagedExceptionCatcherFound is called by Runtime threads when crawling the
* managed stack frame and a handler for the exception is found.
*
* Parameters:
* pThread - The thread the exception is occurring on.
* pTct - Contains the function information that has the catch clause.
* pEHClause - Contains the native offset information of the catch clause.
*
* Returns:
* None.
*
*/
void Debugger::FirstChanceManagedExceptionCatcherFound(Thread *pThread,
MethodDesc *pMD, TADDR pMethodAddr,
BYTE *currentSP,
EE_ILEXCEPTION_CLAUSE *pEHClause)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
MODE_ANY;
}
CONTRACTL_END;
// @@@
// Implements DebugInterface
// Call by EE/exception. Must be on managed thread
_ASSERTE(GetThread() != NULL);
// Quick check.
if (!CORDebuggerAttached())
{
return;
}
// Compute the offset
DWORD nOffset = (DWORD)(SIZE_T)ICorDebugInfo::NO_MAPPING;
DebuggerMethodInfo *pDebugMethodInfo = NULL;
DebuggerJitInfo *pDebugJitInfo = NULL;
bool isInJMCFunction = false;
if (pMD != NULL)
{
_ASSERTE(!pMD->IsILStub());
pDebugJitInfo = GetJitInfo(pMD, (const BYTE *) pMethodAddr, &pDebugMethodInfo);
if (pDebugMethodInfo != NULL)
{
isInJMCFunction = pDebugMethodInfo->IsJMCFunction();
}
}
// Here we check if debugger opted-out of receiving exception related events from outside of JMC methods
// or this exception ever crossed JMC frame (in this case we have already sent user first chance event)
if (m_sendExceptionsOutsideOfJMC ||
isInJMCFunction ||
pThread->GetExceptionState()->GetFlags()->SentDebugUserFirstChance())
{
if (pDebugJitInfo != NULL)
{
CorDebugMappingResult mapResult;
DWORD which;
// Map the native instruction to the IL instruction.
// Be sure to skip past the prolog on amd64/arm to get the right IL
// instruction (on x86 there will not be a prolog as x86 does not use
// funclets).
nOffset = pDebugJitInfo->MapNativeOffsetToIL(
pEHClause->HandlerStartPC,
&mapResult,
&which,
TRUE
);
}
bool fIsInterceptable = IsInterceptableException(pThread);
m_forceNonInterceptable = false;
DWORD dwFlags = fIsInterceptable ? DEBUG_EXCEPTION_CAN_BE_INTERCEPTED : 0;
FramePointer fp = FramePointer::MakeFramePointer(currentSP);
SendCatchHandlerFound(pThread, fp, nOffset, dwFlags);
}
// flag that we catch handler found so that we won't send other mutually exclusive events
// such as unwind begin or unhandled
pThread->GetExceptionState()->GetFlags()->SetDebugCatchHandlerFound();
}
// Filter to trigger CHF callback
// Notify of a catch-handler found callback.
LONG Debugger::NotifyOfCHFFilter(EXCEPTION_POINTERS* pExceptionPointers, PVOID pData)
{
CONTRACTL
{
if ((GetThread() == NULL) || g_pEEInterface->IsThreadExceptionNull(GetThread()))
{
NOTHROW;
GC_NOTRIGGER;
}
else
{
THROWS;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
MODE_ANY;
}
CONTRACTL_END;
SCAN_IGNORE_TRIGGER; // Scan can't handle conditional contracts.
// @@@
// Implements DebugInterface
// Can only be called from EE
// If no debugger is attached, then don't bother sending the events.
// This can't kick off a jit-attach.
if (!CORDebuggerAttached())
{
return EXCEPTION_CONTINUE_SEARCH;
}
//
// If this exception has never bubbled thru to managed code, then there is no
// useful information for the debugger and, in fact, it may be a completely
// internally handled runtime exception, so we should do nothing.
//
if ((GetThread() == NULL) || g_pEEInterface->IsThreadExceptionNull(GetThread()))
{
return EXCEPTION_CONTINUE_SEARCH;
}
// Caller must pass in the stack address. This should match up w/ a Frame.
BYTE * pCatcherStackAddr = (BYTE*) pData;
// If we don't have any catcher frame, then use ebp from the context.
if (pData == NULL)
{
pCatcherStackAddr = (BYTE*) GetFP(pExceptionPointers->ContextRecord);
}
else
{
#ifdef _DEBUG
_ASSERTE(pData != NULL);
{
// We want the CHF stack addr to match w/ the Internal Frame Cordbg sees
// in the stacktrace.
// The Internal Frame comes from an EE Frame. This means that the CHF stack
// addr must match that EE Frame exactly. Let's check that now.
Frame * pFrame = reinterpret_cast<Frame*>(pData);
// Calling a virtual method will enforce that we have a valid Frame. ;)
// If we got passed in a random catch address, then when we cast to a Frame
// the vtable pointer will be bogus and this call will AV.
Frame::ETransitionType e;
e = pFrame->GetTransitionType();
}
#endif
}
// @todo - when Stubs-In-Stacktraces is always enabled, remove this.
if (!g_EnableSIS)
{
return EXCEPTION_CONTINUE_SEARCH;
}
// Stubs don't have an IL offset.
const SIZE_T offset = (SIZE_T)ICorDebugInfo::NO_MAPPING;
Thread *pThread = GetThread();
DWORD dwFlags = IsInterceptableException(pThread) ? DEBUG_EXCEPTION_CAN_BE_INTERCEPTED : 0;
m_forceNonInterceptable = false;
FramePointer fp = FramePointer::MakeFramePointer(pCatcherStackAddr);
//
// If we have not sent a first-chance notification, do so now.
//
ThreadExceptionState* pExState = pThread->GetExceptionState();
if (!pExState->GetFlags()->SentDebugFirstChance())
{
SendException(pThread,
TRUE, // first-chance
(SIZE_T)(GetIP(pExceptionPointers->ContextRecord)), // IP
(SIZE_T)pCatcherStackAddr, // SP
FALSE, // fContinuable
FALSE, // attaching
TRUE, // ForceNonInterceptable since we are transition stub, the first and last place
// that will see this exception.
pExceptionPointers);
}
// Here we check if debugger opted-out of receiving exception related events from outside of JMC methods
// or this exception ever crossed JMC frame (in this case we have already sent user first chance event)
if (m_sendExceptionsOutsideOfJMC || pExState->GetFlags()->SentDebugUserFirstChance())
{
SendCatchHandlerFound(pThread, fp, offset, dwFlags);
}
// flag that we catch handler found so that we won't send other mutually exclusive events
// such as unwind begin or unhandled
pExState->GetFlags()->SetDebugCatchHandlerFound();
#ifdef DEBUGGING_SUPPORTED
if ( (pThread != NULL) &&
(pThread->IsExceptionInProgress()) &&
(pThread->GetExceptionState()->GetFlags()->DebuggerInterceptInfo()) )
{
//
// The debugger wants to intercept this exception. It may return in a failure case,
// in which case we want to continue thru this path.
//
ClrDebuggerDoUnwindAndIntercept(X86_FIRST_ARG(EXCEPTION_CHAIN_END) pExceptionPointers->ExceptionRecord);
}
#endif // DEBUGGING_SUPPORTED
return EXCEPTION_CONTINUE_SEARCH;
}
// Actually send the catch handler found event.
// This can be used to send CHF for both regular managed catchers as well
// as stubs that catch (Func-eval, COM-Interop, AppDomains)
void Debugger::SendCatchHandlerFound(
Thread * pThread,
FramePointer fp,
SIZE_T nOffset,
DWORD dwFlags
)
{
CONTRACTL
{
THROWS;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
MODE_ANY;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::FirstChanceManagedExceptionCatcherFound\n"));
if ((pThread == NULL))
{
_ASSERTE(!"Bad parameter");
LOG((LF_CORDB, LL_INFO10000, "D::FirstChanceManagedExceptionCatcherFound - Bad parameter.\n"));
return;
}
if (CORDBUnrecoverableError(this))
{
return;
}
//
// Mark if we're at an unsafe place.
//
AtSafePlaceHolder unsafePlaceHolder(pThread);
{
GCX_COOP_EEINTERFACE();
{
SENDIPCEVENT_BEGIN(this, pThread);
if (CORDebuggerAttached() &&
!pThread->GetExceptionState()->GetFlags()->DebugCatchHandlerFound() &&
!pThread->GetExceptionState()->GetFlags()->SentDebugUnhandled() &&
!pThread->GetExceptionState()->GetFlags()->SentDebugUnwindBegin())
{
HRESULT hr;
//
// Figure out parameters to the IPC events.
//
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
//
// Send Whidbey EXCEPTION IPC event.
//
InitIPCEvent(ipce, DB_IPCE_EXCEPTION_CALLBACK2, pThread, pThread->GetDomain());
ipce->ExceptionCallback2.framePointer = fp;
ipce->ExceptionCallback2.eventType = DEBUG_EXCEPTION_CATCH_HANDLER_FOUND;
ipce->ExceptionCallback2.nOffset = nOffset;
ipce->ExceptionCallback2.dwFlags = dwFlags;
ipce->ExceptionCallback2.vmExceptionHandle.SetRawPtr(g_pEEInterface->GetThreadException(pThread));
LOG((LF_CORDB, LL_INFO10000, "D::FCMECF: sending ExceptionCallback2"));
hr = m_pRCThread->SendIPCEvent();
_ASSERTE(SUCCEEDED(hr) && "D::FCMECF: Send ExceptionCallback2 event failed.");
//
// Stop all Runtime threads
//
TrapAllRuntimeThreads();
} // end if (!Attached)
else
{
LOG((LF_CORDB,LL_INFO1000, "D:FCMECF: Skipping SendIPCEvent because RS detached.\n"));
}
//
// Let other Runtime threads handle their events.
//
SENDIPCEVENT_END;
}
//
// If we weren't at a safe place when we enabled PGC, then go ahead and unmark that fact now that we've successfully
// disabled.
//
unsafePlaceHolder.Clear();
ProcessAnyPendingEvals(pThread);
} // end of GCX_COOP_EEINTERFACE();
return;
}
/*
* ManagedExceptionUnwindBegin is called by Runtime threads when crawling the
* managed stack frame and unwinding them.
*
* Parameters:
* pThread - The thread the unwind is occurring on.
*
* Returns:
* None.
*
*/
void Debugger::ManagedExceptionUnwindBegin(Thread *pThread)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// @@@
// Implements DebugInterface
// Can only be called on managed threads
//
LOG((LF_CORDB, LL_INFO10000, "D::ManagedExceptionUnwindBegin\n"));
if (pThread == NULL)
{
_ASSERTE(!"Bad parameter");
LOG((LF_CORDB, LL_INFO10000, "D::ManagedExceptionUnwindBegin - Bad parameter.\n"));
return;
}
if (CORDBUnrecoverableError(this))
{
return;
}
//
// Mark if we're at an unsafe place.
//
AtSafePlaceHolder unsafePlaceHolder(pThread);
{
GCX_COOP_EEINTERFACE();
{
SENDIPCEVENT_BEGIN(this, pThread);
if (CORDebuggerAttached() &&
!pThread->GetExceptionState()->GetFlags()->SentDebugUnwindBegin() &&
!pThread->GetExceptionState()->GetFlags()->DebugCatchHandlerFound() &&
!pThread->GetExceptionState()->GetFlags()->SentDebugUnhandled())
{
HRESULT hr;
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
//
// Send Whidbey EXCEPTION IPC event.
//
InitIPCEvent(ipce, DB_IPCE_EXCEPTION_UNWIND, pThread, pThread->GetDomain());
ipce->ExceptionUnwind.eventType = DEBUG_EXCEPTION_UNWIND_BEGIN;
ipce->ExceptionUnwind.dwFlags = 0;
LOG((LF_CORDB, LL_INFO10000, "D::MEUB: sending ExceptionUnwind event"));
hr = m_pRCThread->SendIPCEvent();
_ASSERTE(SUCCEEDED(hr) && "D::MEUB: Send ExceptionUnwind event failed.");
pThread->GetExceptionState()->GetFlags()->SetSentDebugUnwindBegin();
//
// Stop all Runtime threads
//
TrapAllRuntimeThreads();
} // end if (!Attached)
//
// Let other Runtime threads handle their events.
//
SENDIPCEVENT_END;
}
//
// If we weren't at a safe place when we enabled PGC, then go ahead and unmark that fact now that we've successfully
// disabled.
//
unsafePlaceHolder.Clear();
}
return;
}
/*
* DeleteInterceptContext
*
* This function is called by the VM to release any debugger specific information for an
* exception object. It is called when the VM releases its internal exception stuff, i.e.
* ExInfo on X86 and ExceptionTracker on WIN64.
*
*
* Parameters:
* pContext - Debugger specific context.
*
* Returns:
* None.
*
* Notes:
* pContext is just a pointer to a DebuggerContinuableExceptionBreakpoint.
*
*/
void Debugger::DeleteInterceptContext(void *pContext)
{
LIMITED_METHOD_CONTRACT;
DebuggerContinuableExceptionBreakpoint *pBp = (DebuggerContinuableExceptionBreakpoint *)pContext;
if (pBp != NULL)
{
DeleteInteropSafe(pBp);
}
}
// Get the frame point for an exception handler
FramePointer GetHandlerFramePointer(BYTE *pStack)
{
FramePointer handlerFP;
#if !defined(_TARGET_ARM_)
// Refer to the comment in DispatchUnwind() to see why we have to add
// sizeof(LPVOID) to the handler ebp.
handlerFP = FramePointer::MakeFramePointer(LPVOID(pStack + sizeof(void*)));
#else
// ARM is similar to IA64 in that it uses the establisher frame as the
// handler. in this case we don't need to add sizeof(void*) to the FP.
handlerFP = FramePointer::MakeFramePointer((LPVOID)pStack);
#endif // _TARGET_ARM_
return handlerFP;
}
//
// ExceptionFilter is called by the Runtime threads when an exception
// is being processed.
// - fd - MethodDesc of filter function
// - pMethodAddr - any address inside of the method. This lets us resolve exactly which version
// of the method is being executed (for EnC)
// - offset - native offset to handler.
// - pStack, pBStore - stack pointers.
//
void Debugger::ExceptionFilter(MethodDesc *fd, TADDR pMethodAddr, SIZE_T offset, BYTE *pStack)
{
CONTRACTL
{
MODE_COOPERATIVE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(!IsDbgHelperSpecialThread());
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO10000, "D::EF: pStack:0x%x MD: %s::%s, offset:0x%x\n",
pStack, fd->m_pszDebugClassName, fd->m_pszDebugMethodName, offset));
//
// !!! Need to think through logic for when to step through filter code -
// perhaps only during a "step in".
//
//
// !!! Eventually there may be some weird mechanics introduced for
// returning from the filter that we have to understand. For now we should
// be able to proceed normally.
//
FramePointer handlerFP;
handlerFP = GetHandlerFramePointer(pStack);
DebuggerJitInfo * pDJI = NULL;
EX_TRY
{
pDJI = GetJitInfo(fd, (const BYTE *) pMethodAddr);
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions);
if (!fd->IsDynamicMethod() && (pDJI == NULL))
{
// The only way we shouldn't have a DJI is from a dynamic method or from oom (which the LS doesn't handle).
_ASSERTE(!"Debugger doesn't support OOM scenarios.");
return;
}
DebuggerController::DispatchUnwind(g_pEEInterface->GetThread(),
fd, pDJI, offset, handlerFP, STEP_EXCEPTION_FILTER);
}
//
// ExceptionHandle is called by Runtime threads when an exception is
// being handled.
// - fd - MethodDesc of filter function
// - pMethodAddr - any address inside of the method. This lets us resolve exactly which version
// of the method is being executed (for EnC)
// - offset - native offset to handler.
// - pStack, pBStore - stack pointers.
//
void Debugger::ExceptionHandle(MethodDesc *fd, TADDR pMethodAddr, SIZE_T offset, BYTE *pStack)
{
CONTRACTL
{
MODE_COOPERATIVE;
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(!IsDbgHelperSpecialThread());
}
CONTRACTL_END;
FramePointer handlerFP;
handlerFP = GetHandlerFramePointer(pStack);
DebuggerJitInfo * pDJI = NULL;
EX_TRY
{
pDJI = GetJitInfo(fd, (const BYTE *) pMethodAddr);
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions);
if (!fd->IsDynamicMethod() && (pDJI == NULL))
{
// The only way we shouldn't have a DJI is from a dynamic method or from oom (which the LS doesn't handle).
_ASSERTE(!"Debugger doesn't support OOM scenarios.");
return;
}
DebuggerController::DispatchUnwind(g_pEEInterface->GetThread(),
fd, pDJI, offset, handlerFP, STEP_EXCEPTION_HANDLER);
}
BOOL Debugger::ShouldAutoAttach()
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
_ASSERTE(!CORDebuggerAttached());
// We're relying on the caller to determine the
LOG((LF_CORDB, LL_INFO1000000, "D::SAD\n"));
// Check if the user has specified a seting in the registry about what he
// wants done when an unhandled exception occurs.
DebuggerLaunchSetting dls = GetDbgJITDebugLaunchSetting();
return (dls == DLS_ATTACH_DEBUGGER);
// @TODO cache the debugger launch setting.
}
BOOL Debugger::FallbackJITAttachPrompt()
{
_ASSERTE(!CORDebuggerAttached());
return (ATTACH_YES == this->ShouldAttachDebuggerProxy(false));
}
void Debugger::MarkDebuggerAttachedInternal()
{
LIMITED_METHOD_CONTRACT;
// Attach is complete now.
LOG((LF_CORDB, LL_INFO10000, "D::FEDA: Attach Complete!\n"));
g_pEEInterface->MarkDebuggerAttached();
_ASSERTE(HasLazyData());
}
void Debugger::MarkDebuggerUnattachedInternal()
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(HasLazyData());
g_pEEInterface->MarkDebuggerUnattached();
}
//-----------------------------------------------------------------------------
// Favor to do lazy initialization on helper thread.
// This is needed to allow lazy intialization in Stack Overflow scenarios.
// We may or may not already be initialized.
//-----------------------------------------------------------------------------
void LazyInitFavor(void *)
{
CONTRACTL
{
NOTHROW;
MODE_ANY;
}
CONTRACTL_END;
Debugger::DebuggerLockHolder dbgLockHolder(g_pDebugger);
HRESULT hr;
hr = g_pDebugger->LazyInitWrapper();
(void)hr; //prevent "unused variable" error from GCC
// On checked builds, warn that we're hitting a scenario that debugging doesn't support.
_ASSERTE(SUCCEEDED(hr) || !"Couldn't initialize lazy data for LastChanceManagedException");
}
/******************************************************************************
*
******************************************************************************/
LONG Debugger::LastChanceManagedException(EXCEPTION_POINTERS * pExceptionInfo,
Thread *pThread,
BOOL jitAttachRequested)
{
CONTRACTL
{
NOTHROW;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
MODE_ANY;
}
CONTRACTL_END;
// @@@
// Implements DebugInterface.
// Can be run only on managed thread.
LOG((LF_CORDB, LL_INFO10000, "D::LastChanceManagedException\n"));
// Don't stop for native debugging anywhere inside our inproc-Filters.
CantStopHolder hHolder;
EXCEPTION_RECORD * pExceptionRecord = pExceptionInfo->ExceptionRecord;
CONTEXT * pContext = pExceptionInfo->ContextRecord;
// You're allowed to call this function with a NULL exception record and context. If you do, then its assumed
// that we want to head right down to asking the user if they want to attach a debugger. No need to try to
// dispatch the exception to the debugger controllers. You have to pass NULL for both the exception record and
// the context, though. They're a pair. Both have to be NULL, or both have to be valid.
_ASSERTE(((pExceptionRecord != NULL) && (pContext != NULL)) ||
((pExceptionRecord == NULL) && (pContext == NULL)));
if (CORDBUnrecoverableError(this))
{
return ExceptionContinueSearch;
}
// We don't do anything on the second pass
if ((pExceptionRecord != NULL) && ((pExceptionRecord->ExceptionFlags & EXCEPTION_UNWINDING) != 0))
{
return ExceptionContinueSearch;
}
// Let the controllers have a chance at it - this may be the only handler which can catch the exception if this
// is a native patch.
if ((pThread != NULL) &&
(pContext != NULL) &&
CORDebuggerAttached() &&
DebuggerController::DispatchNativeException(pExceptionRecord,
pContext,
pExceptionRecord->ExceptionCode,
pThread))
{
return ExceptionContinueExecution;
}
// Otherwise, run our last chance exception logic
ATTACH_ACTION action;
action = ATTACH_NO;
if (CORDebuggerAttached() || jitAttachRequested)
{
LOG((LF_CORDB, LL_INFO10000, "D::BEH ... debugger attached.\n"));
Thread *thread = g_pEEInterface->GetThread();
_ASSERTE((thread != NULL) && (thread == pThread));
// ExceptionFlags is 0 for continuable, EXCEPTION_NONCONTINUABLE otherwise. Note that if we don't have an
// exception record, then we assume this is a non-continuable exception.
bool continuable = (pExceptionRecord != NULL) && (pExceptionRecord->ExceptionFlags == 0);
LOG((LF_CORDB, LL_INFO10000, "D::BEH ... sending exception.\n"));
HRESULT hr = E_FAIL;
// In the jit-attach case, lazy-init. We may be in a stack-overflow, so do it via a favor to avoid
// using this thread's stack space.
if (jitAttachRequested)
{
m_pRCThread->DoFavor((FAVORCALLBACK) LazyInitFavor, NULL);
}
// The only way we don't have lazy data at this point is in an OOM scenario, which
// the debugger doesn't support.
if (!HasLazyData())
{
return ExceptionContinueSearch;
}
// In Whidbey, we used to set the filter CONTEXT when we hit an unhandled exception while doing
// mixed-mode debugging. This helps the debugger walk the stack since it can skip the leaf
// portion of the stack (including stack frames in the runtime) and start the stackwalk at the
// faulting stack frame. The code to set the filter CONTEXT is in a hijack function which is only
// used during mixed-mode debugging.
if (m_pRCThread->GetDCB()->m_rightSideIsWin32Debugger)
{
GCX_COOP();
_ASSERTE(thread->GetFilterContext() == NULL);
thread->SetFilterContext(pExceptionInfo->ContextRecord);
}
EX_TRY
{
// We pass the attaching status to SendException so that it knows
// whether to attach a debugger or not. We should really do the
// attach stuff out here and not bother with the flag.
hr = SendException(thread,
FALSE,
((pContext != NULL) ? (SIZE_T)GetIP(pContext) : NULL),
((pContext != NULL) ? (SIZE_T)GetSP(pContext) : NULL),
continuable,
!!jitAttachRequested, // If we are JIT attaching on an unhandled exceptioin, we force
!!jitAttachRequested, // the exception to be uninterceptable.
pExceptionInfo);
}
EX_CATCH
{
}
EX_END_CATCH(SwallowAllExceptions);
if (m_pRCThread->GetDCB()->m_rightSideIsWin32Debugger)
{
GCX_COOP();
thread->SetFilterContext(NULL);
}
}
else
{
// Note: we don't do anything on NO or TERMINATE. We just return to the exception logic, which will abort the
// app or not depending on what the CLR impl decides is appropiate.
_ASSERTE(action == ATTACH_TERMINATE || action == ATTACH_NO);
}
return ExceptionContinueSearch;
}
//
// NotifyUserOfFault notifies the user of a fault (unhandled exception
// or user breakpoint) in the process, giving them the option to
// attach a debugger or terminate the application.
//
int Debugger::NotifyUserOfFault(bool userBreakpoint, DebuggerLaunchSetting dls)
{
LOG((LF_CORDB, LL_INFO1000000, "D::NotifyUserOfFault\n"));
CONTRACTL
{
NOTHROW;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;;
MODE_PREEMPTIVE;
}
CONTRACTL_END;
int result = IDCANCEL;
if (!CORDebuggerAttached())
{
DWORD pid;
DWORD tid;
pid = GetCurrentProcessId();
tid = GetCurrentThreadId();
DWORD flags = 0;
UINT resIDMessage = 0;
if (userBreakpoint)
{
resIDMessage = IDS_DEBUG_USER_BREAKPOINT_MSG;
flags |= MB_ABORTRETRYIGNORE | MB_ICONEXCLAMATION;
}
else
{
resIDMessage = IDS_DEBUG_UNHANDLED_EXCEPTION_MSG;
flags |= MB_OKCANCEL | MB_ICONEXCLAMATION;
}
{
// Another potential hang. This may get run on the helper if we have a stack overflow.
// Hopefully the odds of 1 thread hitting a stack overflow while another is stuck holding the heap
// lock is very small.
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
result = MessageBox(resIDMessage, IDS_DEBUG_SERVICE_CAPTION,
flags, TRUE, TRUE, pid, pid, tid, tid);
}
}
LOG((LF_CORDB, LL_INFO1000000, "D::NotifyUserOfFault left\n"));
return result;
}
// Proxy for ShouldAttachDebugger
struct ShouldAttachDebuggerParams {
Debugger* m_pThis;
bool m_fIsUserBreakpoint;
Debugger::ATTACH_ACTION m_retval;
};
// This is called by the helper thread
void ShouldAttachDebuggerStub(ShouldAttachDebuggerParams * p)
{
WRAPPER_NO_CONTRACT;
p->m_retval = p->m_pThis->ShouldAttachDebugger(p->m_fIsUserBreakpoint);
}
// This gets called just like the normal version, but it sends the call over to the helper thread
Debugger::ATTACH_ACTION Debugger::ShouldAttachDebuggerProxy(bool fIsUserBreakpoint)
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
}
CONTRACTL_END;
if (!HasLazyData())
{
DebuggerLockHolder lockHolder(this);
HRESULT hr = LazyInitWrapper();
if (FAILED(hr))
{
// We already stress logged this case.
return ATTACH_NO;
}
}
if (!IsGuardPageGone())
return ShouldAttachDebugger(fIsUserBreakpoint);
ShouldAttachDebuggerParams p;
p.m_pThis = this;
p.m_fIsUserBreakpoint = fIsUserBreakpoint;
LOG((LF_CORDB, LL_INFO1000000, "D::SADProxy\n"));
m_pRCThread->DoFavor((FAVORCALLBACK) ShouldAttachDebuggerStub, &p);
LOG((LF_CORDB, LL_INFO1000000, "D::SADProxy return %d\n", p.m_retval));
return p.m_retval;
}
//---------------------------------------------------------------------------------------
// Do policy to determine if we should attach a debugger.
//
// Arguments:
// fIsUserBreakpoint - true iff this is in response to a user-breakpoint, else false.
//
// Returns:
// Action to perform based off policy.
// ATTACH_NO if a debugger is already attached.
Debugger::ATTACH_ACTION Debugger::ShouldAttachDebugger(bool fIsUserBreakpoint)
{
CONTRACTL
{
NOTHROW;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
MODE_ANY;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000000, "D::SAD\n"));
// If the debugger is already attached, not necessary to re-attach
if (CORDebuggerAttached())
{
return ATTACH_NO;
}
// Check if the user has specified a seting in the registry about what he wants done when an unhandled exception
// occurs.
DebuggerLaunchSetting dls = GetDbgJITDebugLaunchSetting();
if (dls == DLS_ATTACH_DEBUGGER)
{
return ATTACH_YES;
}
else
{
// Only ask the user once if they wish to attach a debugger. This is because LastChanceManagedException can be called
// twice, which causes ShouldAttachDebugger to be called twice, which causes the user to have to answer twice.
static BOOL s_fHasAlreadyAsked = FALSE;
static ATTACH_ACTION s_action;
// This lock is also part of the above workaround.
// Must go to preemptive to take this lock since we'll trigger down the road.
GCX_PREEMP();
DebuggerLockHolder lockHolder(this);
// We always want to ask about user breakpoints!
if (!s_fHasAlreadyAsked || fIsUserBreakpoint)
{
if (!fIsUserBreakpoint)
s_fHasAlreadyAsked = TRUE;
// While we could theoretically run into a deadlock if another thread
// which acquires the debugger lock in cooperative GC mode is blocked
// on this thread while it is running arbitrary user code out of the
// MessageBox message pump, given that this codepath will only be used
// on Win9x and that the chances of this happenning are quite slim,
// for Whidbey a GCViolation is acceptable.
CONTRACT_VIOLATION(GCViolation);
// Ask the user if they want to attach
int iRes = NotifyUserOfFault(fIsUserBreakpoint, dls);
// If it's a user-defined breakpoint, they must hit Retry to launch
// the debugger. If it's an unhandled exception, user must press
// Cancel to attach the debugger.
if ((iRes == IDCANCEL) || (iRes == IDRETRY))
s_action = ATTACH_YES;
else if ((iRes == IDABORT) || (iRes == IDOK))
s_action = ATTACH_TERMINATE;
else
s_action = ATTACH_NO;
}
// dbgLockHolder goes out of scope - implicit Release
return s_action;
}
}
//---------------------------------------------------------------------------------------
// SendUserBreakpoint is called by Runtime threads to send that they've hit
// a user breakpoint to the Right Side.
//
// Parameters:
// thread - managed thread that the breakpoint is on
//
// Notes:
// A user breakpoint is generally triggered by a call to System.Diagnostics.Debugger.Break.
// This can be very common. VB's 'stop' statement compiles to a Debugger.Break call.
// Some other CLR facilities (MDAs) may call this directly too.
//
// This may trigger a Jit attach.
// If the debugger is already attached, this will issue a step-out so that the UserBreakpoint
// appears to come from the callsite.
void Debugger::SendUserBreakpoint(Thread * thread)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
PRECONDITION(thread != NULL);
PRECONDITION(thread == ::GetThread());
}
CONTRACTL_END;
#ifdef _DEBUG
// For testing Watson, we want a consistent way to be able to generate a
// Fatal Execution Error
// So we have a debug-only knob in this particular managed call that can be used
// to artificially inject the error.
// This is only for testing.
static int fDbgInjectFEE = -1;
if (fDbgInjectFEE == -1)
fDbgInjectFEE = UnsafeGetConfigDWORD(CLRConfig::INTERNAL_DbgInjectFEE);
if (fDbgInjectFEE)
{
STRESS_LOG0(LF_CORDB, LL_INFO10000, "Debugger posting bogus FEE b/c knob DbgInjectFEE is set.\n");
EEPOLICY_HANDLE_FATAL_ERROR(COR_E_EXECUTIONENGINE);
// These never return.
}
#endif
if (CORDBUnrecoverableError(this))
{
return;
}
// UserBreakpoint behaves differently if we're under a debugger vs. a jit-attach.
// If we're under the debugger, it does an additional step-out to get us back to the call site.
// If already attached, then do a step-out and send the userbreak event.
if (CORDebuggerAttached())
{
// A debugger is already attached, so setup a DebuggerUserBreakpoint controller to get us out of the helper
// that got us here. The DebuggerUserBreakpoint will call AttachDebuggerForBreakpoint for us when we're out
// of the helper. The controller will delete itself when its done its work.
DebuggerUserBreakpoint::HandleDebugBreak(thread);
return;
}
ATTACH_ACTION dbgAction = ShouldAttachDebugger(true);
// No debugger is attached. Consider a JIT attach.
// This will do ShouldAttachDebugger() and wait for the results.
// - It may terminate if the user requested that.
// - It may do a full jit-attach.
if (dbgAction == ATTACH_YES)
{
JitAttach(thread, NULL, TRUE, FALSE);
}
else if (dbgAction == ATTACH_TERMINATE)
{
// ATTACH_TERMINATE indicates the the user wants to terminate the app.
LOG((LF_CORDB, LL_INFO10000, "D::SUB: terminating this process due to user request\n"));
// Should this go through the host?
TerminateProcess(GetCurrentProcess(), 0);
_ASSERTE(!"Should never reach this point.");
}
else
{
_ASSERTE(dbgAction == ATTACH_NO);
}
if (CORDebuggerAttached())
{
// On jit-attach, we just send the UserBreak event. Don't do an extra step-out.
SendUserBreakpointAndSynchronize(thread);
}
else if (IsDebuggerPresent())
{
DebugBreak();
}
}
// void Debugger::ThreadCreated(): ThreadCreated is called when
// a new Runtime thread has been created, but before its ever seen
// managed code. This is a callback invoked by the EE into the Debugger.
// This will create a DebuggerThreadStarter patch, which will set
// a patch at the first instruction in the managed code. When we hit
// that patch, the DebuggerThreadStarter will invoke ThreadStarted, below.
//
// Thread* pRuntimeThread: The EE Thread object representing the
// runtime thread that has just been created.
void Debugger::ThreadCreated(Thread* pRuntimeThread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// @@@
// This function implements the DebugInterface. But it is also called from Attach
// logic internally.
//
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO100, "D::TC: thread created for 0x%x. ******\n",
GetThreadIdHelper(pRuntimeThread)));
// Sanity check the thread.
_ASSERTE(pRuntimeThread != NULL);
_ASSERTE(pRuntimeThread->GetThreadId() != 0);
// Create a thread starter and enable its WillEnterManaged code
// callback. This will cause the starter to trigger once the
// thread has hit managed code, which will cause
// Debugger::ThreadStarted() to be called. NOTE: the starter will
// be deleted automatically when its done its work.
DebuggerThreadStarter *starter = new (interopsafe, nothrow) DebuggerThreadStarter(pRuntimeThread);
if (starter == NULL)
{
CORDBDebuggerSetUnrecoverableWin32Error(this, 0, false);
return;
}
starter->EnableTraceCall(LEAF_MOST_FRAME);
}
// void Debugger::ThreadStarted(): ThreadStarted is called when
// a new Runtime thread has reached its first managed code. This is
// called by the DebuggerThreadStarter patch's SendEvent method.
//
// Thread* pRuntimeThread: The EE Thread object representing the
// runtime thread that has just hit managed code.
void Debugger::ThreadStarted(Thread* pRuntimeThread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// @@@
// This method implemented DebugInterface but it is also called from Controller
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO100, "D::TS: thread attach : ID=%#x AD:%#x\n",
GetThreadIdHelper(pRuntimeThread), pRuntimeThread->GetDomain()));
// We just need to send a VMPTR_Thread. The RS will get everything else it needs from DAC.
//
_ASSERTE((g_pEEInterface->GetThread() &&
!g_pEEInterface->GetThread()->m_fPreemptiveGCDisabled) ||
g_fInControlC);
_ASSERTE(ThreadHoldsLock());
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_THREAD_ATTACH,
pRuntimeThread,
pRuntimeThread->GetDomain());
m_pRCThread->SendIPCEvent();
//
// Well, if this thread got created _after_ we started sync'ing
// then its Runtime thread flags don't have the fact that there
// is a debug suspend pending. We need to call over to the
// Runtime and set the flag in the thread now...
//
if (m_trappingRuntimeThreads)
{
g_pEEInterface->MarkThreadForDebugSuspend(pRuntimeThread);
}
}
//---------------------------------------------------------------------------------------
//
// DetachThread is called by Runtime threads when they are completing
// their execution and about to be destroyed.
//
// Arguments:
// pRuntimeThread - Pointer to the runtime's thread object to detach.
//
// Return Value:
// None
//
//---------------------------------------------------------------------------------------
void Debugger::DetachThread(Thread *pRuntimeThread)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
{
return;
}
if (m_ignoreThreadDetach)
{
return;
}
_ASSERTE (pRuntimeThread != NULL);
LOG((LF_CORDB, LL_INFO100, "D::DT: thread detach : ID=%#x AD:%#x.\n",
GetThreadIdHelper(pRuntimeThread), pRuntimeThread->GetDomain()));
// We may be killing a thread before the Thread-starter fired.
// So check (and cancel) any outstanding thread-starters.
// If we don't, this old thread starter may conflict w/ a new thread-starter
// if AppDomains or EE Thread's get recycled.
DebuggerController::CancelOutstandingThreadStarter(pRuntimeThread);
// Controller lock is bigger than debugger lock.
// Don't take debugger lock before the CancelOutStandingThreadStarter function.
SENDIPCEVENT_BEGIN(this, pRuntimeThread);
if (CORDebuggerAttached())
{
// Send a detach thread event to the Right Side.
DebuggerIPCEvent * pEvent = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(pEvent,
DB_IPCE_THREAD_DETACH,
pRuntimeThread,
pRuntimeThread->GetDomain());
m_pRCThread->SendIPCEvent();
// Stop all Runtime threads
TrapAllRuntimeThreads();
// This prevents a race condition where we blocked on the Lock()
// above while another thread was sending an event and while we
// were blocked the debugger suspended us and so we wouldn't be
// resumed after the suspension about to happen below.
pRuntimeThread->ResetThreadStateNC(Thread::TSNC_DebuggerUserSuspend);
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::DT: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
}
//
// SuspendComplete is called when the last Runtime thread reaches a safe point in response to having its trap flags set.
// This may be called on either the real helper thread or someone doing helper thread duty.
//
BOOL Debugger::SuspendComplete()
{
CONTRACTL
{
NOTHROW;
GC_TRIGGERS;
// This will is conceptually mode-cooperative.
// But we haven't marked the runtime as stopped yet (m_stopped), so the contract
// subsystem doesn't realize it yet.
DISABLED(MODE_COOPERATIVE);
}
CONTRACTL_END;
// @@@
// Call from RCThread::MainLoop and TemporaryHelperThreadMainLoop.
// when all threads suspended. Can happen on managed thread or helper thread.
// If happen on managed thread, it must be doing the helper thread duty.
//
_ASSERTE(ThreadStore::HoldingThreadStore() || g_fProcessDetach);
// We should be holding debugger lock m_mutex.
_ASSERTE(ThreadHoldsLock());
// We can't throw here (we're in the middle of the runtime suspension logic).
// But things below us throw. So we catch the exception, but then what state are we in?
_ASSERTE((!g_pEEInterface->GetThread() || !g_pEEInterface->GetThread()->m_fPreemptiveGCDisabled) || g_fInControlC);
_ASSERTE(ThisIsHelperThreadWorker());
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::SC: suspension complete\n");
// We have suspended runtime.
// We're stopped now. Marking m_stopped allows us to use MODE_COOPERATIVE contracts.
_ASSERTE(!m_stopped && m_trappingRuntimeThreads);
m_stopped = true;
// Send the sync complete event to the Right Side.
{
// If we fail to send the SyncComplete, what do we do?
CONTRACT_VIOLATION(ThrowsViolation);
SendSyncCompleteIPCEvent(); // sets m_stopped = true...
}
// Everything in the next scope is meant to mimic what we do UnlockForEventSending minus EnableEventHandling.
// We do the EEH part when we get the Continue event.
{
#ifdef _DEBUG
//_ASSERTE(m_tidLockedForEventSending == GetCurrentThreadId());
m_tidLockedForEventSending = 0;
#endif
//
// Event handling is re-enabled by the RCThread in response to a
// continue message from the Right Side.
}
// @todo - what should we do if this function failed?
return TRUE;
}
//---------------------------------------------------------------------------------------
//
// Debugger::SendCreateAppDomainEvent - notify the RS of an AppDomain
//
// Arguments:
// pRuntimeAppdomain - pointer to the AppDomain
//
// Return Value:
// None
//
// Notes:
// This is used to notify the debugger of either a newly created
// AppDomain (when fAttaching is FALSE) or of existing AppDomains
// at attach time (fAttaching is TRUE). In both cases, this should
// be called before any LoadModule/LoadAssembly events are sent for
// this domain. Otherwise the RS will get an event for an AppDomain
// it doesn't recognize and ASSERT.
//
// For the non-attach case this means there is no need to enumerate
// the assemblies/modules in an AppDomain after sending this event
// because we know there won't be any.
//
void Debugger::SendCreateAppDomainEvent(AppDomain * pRuntimeAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
MODE_COOPERATIVE;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
{
return;
}
STRESS_LOG2(LF_CORDB, LL_INFO10000, "D::SCADE: AppDomain creation:%#08x, %#08x\n",
pRuntimeAppDomain, pRuntimeAppDomain->GetId().m_dwId);
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
// We may have detached while waiting in LockForEventSending,
// in which case we can't send the event.
if (CORDebuggerAttached())
{
// Send a create appdomain event to the Right Side.
DebuggerIPCEvent * pEvent = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(pEvent,
DB_IPCE_CREATE_APP_DOMAIN,
pThread,
pRuntimeAppDomain);
// Only send a pointer to the AppDomain, the RS will get everything else via DAC.
pEvent->AppDomainData.vmAppDomain.SetRawPtr(pRuntimeAppDomain);
m_pRCThread->SendIPCEvent();
TrapAllRuntimeThreads();
}
// Let other Runtime threads handle their events.
SENDIPCEVENT_END;
}
//
// SendExitAppDomainEvent is called when an app domain is destroyed.
//
void Debugger::SendExitAppDomainEvent(AppDomain* pRuntimeAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO100, "D::EAD: Exit AppDomain 0x%08x.\n",
pRuntimeAppDomain));
STRESS_LOG3(LF_CORDB, LL_INFO10000, "D::EAD: AppDomain exit:%#08x, %#08x, %#08x\n",
pRuntimeAppDomain, pRuntimeAppDomain->GetId().m_dwId, CORDebuggerAttached());
Thread *thread = g_pEEInterface->GetThread();
// Prevent other Runtime threads from handling events.
SENDIPCEVENT_BEGIN(this, thread);
if (CORDebuggerAttached())
{
if (pRuntimeAppDomain->IsDefaultDomain() )
{
// The Debugger expects to never get an unload event for the default Domain.
// Currently we should never get here because g_fProcessDetach will be true by
// the time this method is called. However, we'd like to know if this ever changes
_ASSERTE(!"Trying to deliver notification of unload for default domain" );
return;
}
// Send the exit appdomain event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_EXIT_APP_DOMAIN,
thread,
pRuntimeAppDomain);
m_pRCThread->SendIPCEvent();
// Delete any left over modules for this appdomain.
// Note that we're doing this under the lock.
if (m_pModules != NULL)
{
DebuggerDataLockHolder ch(this);
m_pModules->RemoveModules(pRuntimeAppDomain);
}
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::EAD: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
}
//
// LoadAssembly is called when a new Assembly gets loaded.
//
void Debugger::LoadAssembly(DomainAssembly * pDomainAssembly)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO100, "D::LA: Load Assembly Asy:0x%p AD:0x%p which:%ls\n",
pDomainAssembly, pDomainAssembly->GetAppDomain(), pDomainAssembly->GetAssembly()->GetDebugName() ));
if (!CORDebuggerAttached())
{
return;
}
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread)
if (CORDebuggerAttached())
{
// Send a load assembly event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_LOAD_ASSEMBLY,
pThread,
pDomainAssembly->GetAppDomain());
ipce->AssemblyData.vmDomainAssembly.SetRawPtr(pDomainAssembly);
m_pRCThread->SendIPCEvent();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::LA: Skipping SendIPCEvent because RS detached."));
}
// Stop all Runtime threads
if (CORDebuggerAttached())
{
TrapAllRuntimeThreads();
}
SENDIPCEVENT_END;
}
//
// UnloadAssembly is called when a Runtime thread unloads an assembly.
//
void Debugger::UnloadAssembly(DomainAssembly * pDomainAssembly)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO100, "D::UA: Unload Assembly Asy:0x%p AD:0x%p which:%ls\n",
pDomainAssembly, pDomainAssembly->GetAppDomain(), pDomainAssembly->GetAssembly()->GetDebugName() ));
Thread *thread = g_pEEInterface->GetThread();
// Note that the debugger lock is reentrant, so we may or may not hold it already.
SENDIPCEVENT_BEGIN(this, thread);
// Send the unload assembly event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_UNLOAD_ASSEMBLY,
thread,
pDomainAssembly->GetAppDomain());
ipce->AssemblyData.vmDomainAssembly.SetRawPtr(pDomainAssembly);
SendSimpleIPCEventAndBlock();
// This will block on the continue
SENDIPCEVENT_END;
}
//
// LoadModule is called when a Runtime thread loads a new module and a debugger
// is attached. This also includes when a domain-neutral module is "loaded" into
// a new domain.
//
// TODO: remove pszModuleName and perhaps other args.
void Debugger::LoadModule(Module* pRuntimeModule,
LPCWSTR pszModuleName, // module file name.
DWORD dwModuleName, // length of pszModuleName in chars, not including null.
Assembly *pAssembly,
AppDomain *pAppDomain,
DomainFile * pDomainFile,
BOOL fAttaching)
{
CONTRACTL
{
NOTHROW; // not protected for Throws.
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// @@@@
// Implement DebugInterface but can be called internally as well.
// This can be called by EE loading module or when we are attaching called by IteratingAppDomainForAttaching
//
_ASSERTE(!fAttaching);
if (CORDBUnrecoverableError(this))
return;
// If this is a dynamic module, then it's part of a multi-module assembly. The manifest
// module within the assembly contains metadata for all the module names in the assembly.
// When a new dynamic module is created, the manifest module's metadata is updated to
// include the new module (see code:Assembly.CreateDynamicModule).
// So we need to update the RS's copy of the metadata. One place the manifest module's
// metadata gets used is in code:DacDbiInterfaceImpl.GetModuleSimpleName
//
// See code:ReflectionModule.CaptureModuleMetaDataToMemory for why we send the metadata-refresh here.
if (pRuntimeModule->IsReflection() && !pRuntimeModule->IsManifest() && !fAttaching)
{
HRESULT hr = S_OK;
EX_TRY
{
// The loader lookups may throw or togggle GC mode, so do them inside a TRY/Catch and
// outside any debugger locks.
Module * pManifestModule = pRuntimeModule->GetAssembly()->GetManifestModule();
_ASSERTE(pManifestModule != pRuntimeModule);
_ASSERTE(pManifestModule->IsManifest());
_ASSERTE(pManifestModule->GetAssembly() == pRuntimeModule->GetAssembly());
DomainFile * pManifestDomainFile = pManifestModule->GetDomainFile(pAppDomain);
DebuggerLockHolder dbgLockHolder(this);
// Raise the debug event.
// This still tells the debugger that the manifest module metadata is invalid and needs to
// be refreshed.
DebuggerIPCEvent eventMetadataUpdate;
InitIPCEvent(&eventMetadataUpdate, DB_IPCE_METADATA_UPDATE, NULL, pAppDomain);
eventMetadataUpdate.MetadataUpdateData.vmDomainFile.SetRawPtr(pManifestDomainFile);
SendRawEvent(&eventMetadataUpdate);
}
EX_CATCH_HRESULT(hr);
SIMPLIFYING_ASSUMPTION_SUCCEEDED(hr);
}
DebuggerModule * module = NULL;
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
#ifdef FEATURE_FUSION
// Fix for issue Whidbey - 106398
// Populate the pdb to fusion cache.
//
if (pRuntimeModule->IsIStream() == FALSE)
{
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
HRESULT hrCopy = S_OK;
EX_TRY
{
pRuntimeModule->FusionCopyPDBs(pRuntimeModule->GetPath());
}
EX_CATCH_HRESULT(hrCopy); // ignore failures
}
#endif // FEATURE_FUSION
DebuggerIPCEvent* ipce = NULL;
// Don't create new record if already loaded. We do still want to send the ModuleLoad event, however.
// The RS has logic to ignore duplicate ModuleLoad events. We have to send what could possibly be a dup, though,
// due to some really nasty issues with getting proper assembly and module load events from the loader when dealing
// with shared assemblies.
module = LookupOrCreateModule(pDomainFile);
_ASSERTE(module != NULL);
// During a real LoadModule event, debugger can change jit flags.
// Can't do this during a fake event sent on attach.
// This is cleared after we send the LoadModule event.
module->SetCanChangeJitFlags(true);
// @dbgtodo inspection - Check whether the DomainFile we get is consistent with the Module and AppDomain we get.
// We should simply things when we actually get rid of DebuggerModule, possibly by just passing the
// DomainFile around.
_ASSERTE(module->GetDomainFile() == pDomainFile);
_ASSERTE(module->GetAppDomain() == pDomainFile->GetAppDomain());
_ASSERTE(module->GetRuntimeModule() == pDomainFile->GetModule());
// Send a load module event to the Right Side.
ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,DB_IPCE_LOAD_MODULE, pThread, pAppDomain);
ipce->LoadModuleData.vmDomainFile.SetRawPtr(pDomainFile);
m_pRCThread->SendIPCEvent();
{
// Stop all Runtime threads
HRESULT hr = S_OK;
EX_TRY
{
TrapAllRuntimeThreads();
}
EX_CATCH_HRESULT(hr); // @dbgtodo synchronization - catch exception and go on to restore state.
// Synchronization feature crew needs to figure out what happens to TrapAllRuntimeThreads().
}
SENDIPCEVENT_END;
// need to update pdb stream for SQL passed in pdb stream
// regardless attach or not.
//
if (pRuntimeModule->IsIStream())
{
// Just ignore failures. Caller was just sending a debug event and we don't
// want that to interop non-debugging functionality.
HRESULT hr = S_OK;
EX_TRY
{
SendUpdateModuleSymsEventAndBlock(pRuntimeModule, pAppDomain);
}
EX_CATCH_HRESULT(hr);
}
// Now that we're done with the load module event, can no longer change Jit flags.
module->SetCanChangeJitFlags(false);
}
//---------------------------------------------------------------------------------------
//
// Special LS-only notification that a module has reached the FILE_LOADED level. For now
// this is only useful to bind breakpoints in generic instantiations from NGENd modules
// that we couldn't bind earlier (at LoadModule notification time) because the method
// iterator refuses to consider modules earlier than the FILE_LOADED level. Normally
// generic instantiations would have their breakpoints bound when they get JITted, but in
// the case of NGEN that may never happen, so we need to bind them here.
//
// Arguments:
// * pRuntimeModule - Module that just loaded
// * pAppDomain - AD into which the Module was loaded
//
// Assumptions:
// This is called during the loading process, and blocks that process from
// completing. The module has reached the FILE_LOADED stage, but typically not yet
// the IsReadyForTypeLoad stage.
//
void Debugger::LoadModuleFinished(Module * pRuntimeModule, AppDomain * pAppDomain)
{
CONTRACTL
{
SUPPORTS_DAC;
STANDARD_VM_CHECK;
}
CONTRACTL_END;
_ASSERTE(pRuntimeModule != NULL);
_ASSERTE(pAppDomain != NULL);
if (CORDBUnrecoverableError(this))
return;
// Just as an optimization, skip binding breakpoints if there's no debugger attached.
// If a debugger attaches at some point after here, it will be able to bind patches
// by making the request at that time. If a debugger detaches at some point after
// here, there's no harm in having extra patches bound.
if (!CORDebuggerAttached())
return;
// For now, this notification only does interesting work if the module that loaded is
// an NGENd module, because all we care about in this notification is ensuring NGENd
// methods get breakpoints bound on them
if (!pRuntimeModule->HasNativeImage())
return;
// This notification is called just before MODULE_READY_FOR_TYPELOAD gets set. But
// for shared modules (loaded into multiple domains), MODULE_READY_FOR_TYPELOAD has
// already been set if this module was already loaded into an earlier domain. For
// such cases, there's no need to bind breakpoints now because the module has already
// been fully loaded into at least one domain, and breakpoint binding has already
// been done for us
if (pRuntimeModule->IsReadyForTypeLoad())
return;
#ifdef _DEBUG
{
// This notification is called once the module is loaded
DomainFile * pDomainFile = pRuntimeModule->FindDomainFile(pAppDomain);
_ASSERTE((pDomainFile != NULL) && (pDomainFile->GetLoadLevel() >= FILE_LOADED));
}
#endif // _DEBUG
// Find all IL Master patches for this module, and bind & activate their
// corresponding slave patches.
{
DebuggerController::ControllerLockHolder ch;
HASHFIND info;
DebuggerPatchTable * pTable = DebuggerController::GetPatchTable();
for (DebuggerControllerPatch * pMasterPatchCur = pTable->GetFirstPatch(&info);
pMasterPatchCur != NULL;
pMasterPatchCur = pTable->GetNextPatch(&info))
{
if (!pMasterPatchCur->IsILMasterPatch())
continue;
DebuggerMethodInfo *dmi = GetOrCreateMethodInfo(pMasterPatchCur->key.module, pMasterPatchCur->key.md);
// Found a relevant IL master patch. Now bind all corresponding slave patches
// that belong to this Module
DebuggerMethodInfo::DJIIterator it;
dmi->IterateAllDJIs(pAppDomain, pRuntimeModule, &it);
for (; !it.IsAtEnd(); it.Next())
{
DebuggerJitInfo *dji = it.Current();
_ASSERTE(dji->m_jitComplete);
if (dji->m_encVersion != pMasterPatchCur->GetEnCVersion())
continue;
// Do we already have a slave for this DJI & Controller? If so, no need
// to add another one
BOOL fSlaveExists = FALSE;
HASHFIND f;
for (DebuggerControllerPatch * pSlavePatchCur = pTable->GetFirstPatch(&f);
pSlavePatchCur != NULL;
pSlavePatchCur = pTable->GetNextPatch(&f))
{
if (pSlavePatchCur->IsILSlavePatch() &&
(pSlavePatchCur->GetDJI() == dji) &&
(pSlavePatchCur->controller == pMasterPatchCur->controller))
{
fSlaveExists = TRUE;
break;
}
}
if (fSlaveExists)
continue;
pMasterPatchCur->controller->AddBindAndActivateILSlavePatch(pMasterPatchCur, dji);
}
}
}
}
// Send the raw event for Updating symbols. Debugger must query for contents from out-of-process
//
// Arguments:
// pRuntimeModule - required, module to send symbols for. May be domain neutral.
// pAppDomain - required, appdomain that module is in.
//
// Notes:
// This is just a ping event. Debugger must query for actual symbol contents.
// This keeps the launch + attach cases identical.
// This just sends the raw event and does not synchronize the runtime.
// Use code:Debugger.SendUpdateModuleSymsEventAndBlock for that.
void Debugger::SendRawUpdateModuleSymsEvent(Module *pRuntimeModule, AppDomain *pAppDomain)
{
// @telest - do we need an #ifdef FEATURE_FUSION here?
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_PREEMPTIVE;
PRECONDITION(ThreadHoldsLock());
// Debugger must have been attached to get us to this point.
// We hold the Debugger-lock, so debugger could not have detached from
// underneath us either.
PRECONDITION(CORDebuggerAttached());
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this))
return;
// This event is used to trigger the ICorDebugManagedCallback::UpdateModuleSymbols
// callback. That callback is defined to pass a PDB stream, and so we still use this
// only for legacy compatability reasons when we've actually got PDB symbols.
// New clients know they must request a new symbol reader after ClassLoad events.
if (pRuntimeModule->GetInMemorySymbolStreamFormat() != eSymbolFormatPDB)
return; // Non-PDB symbols
DebuggerModule* module = LookupOrCreateModule(pRuntimeModule, pAppDomain);
PREFIX_ASSUME(module != NULL);
DebuggerIPCEvent* ipce = NULL;
ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_UPDATE_MODULE_SYMS,
g_pEEInterface->GetThread(),
pAppDomain);
ipce->UpdateModuleSymsData.vmDomainFile.SetRawPtr((module ? module->GetDomainFile() : NULL));
m_pRCThread->SendIPCEvent();
}
//
// UpdateModuleSyms is called when the symbols for a module need to be
// sent to the Right Side because they've changed.
//
// Arguments:
// pRuntimeModule - required, module to send symbols for. May be domain neutral.
// pAppDomain - required, appdomain that module is in.
//
//
// Notes:
// This will send the event (via code:Debugger.SendRawUpdateModuleSymsEvent) and then synchronize
// the runtime waiting for a continue.
//
// This should only be called in cases where we reasonably expect to send symbols.
// However, this may not send symbols if the symbols aren't available.
void Debugger::SendUpdateModuleSymsEventAndBlock(Module* pRuntimeModule, AppDomain *pAppDomain)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
if (CORDBUnrecoverableError(this) || !CORDebuggerAttached())
{
return;
}
CGrowableStream * pStream = pRuntimeModule->GetInMemorySymbolStream();
LOG((LF_CORDB, LL_INFO10000, "D::UMS: update module syms RuntimeModule:0x%08x CGrowableStream:0x%08x\n", pRuntimeModule, pStream));
if (pStream == NULL)
{
// No in-memory Pdb available.
STRESS_LOG1(LF_CORDB, LL_INFO10000, "No syms available %p", pRuntimeModule);
return;
}
SENDIPCEVENT_BEGIN(this, g_pEEInterface->GetThread()); // toggles to preemptive
// Actually send the event
if (CORDebuggerAttached())
{
SendRawUpdateModuleSymsEvent(pRuntimeModule, pAppDomain);
TrapAllRuntimeThreads();
}
SENDIPCEVENT_END;
}
//
// UnloadModule is called by the Runtime for each module (including shared ones)
// in an AppDomain that is being unloaded, when a debugger is attached.
// In the EE, a module may be domain-neutral and therefore shared across all AppDomains.
// We abstract this detail away in the Debugger and consider each such EE module to correspond
// to multiple "Debugger Module" instances (one per AppDomain).
// Therefore, this doesn't necessarily mean the runtime is unloading the module, just
// that the Debugger should consider it's (per-AppDomain) DebuggerModule to be unloaded.
//
void Debugger::UnloadModule(Module* pRuntimeModule,
AppDomain *pAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// @@@@
// implements DebugInterface.
// can only called by EE on Module::NotifyDebuggerUnload
//
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO100, "D::UM: unload module Mod:%#08x AD:%#08x runtimeMod:%#08x modName:%ls\n",
LookupOrCreateModule(pRuntimeModule, pAppDomain), pAppDomain, pRuntimeModule, pRuntimeModule->GetDebugName()));
Thread *thread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, thread);
if (CORDebuggerAttached())
{
DebuggerModule* module = LookupOrCreateModule(pRuntimeModule, pAppDomain);
if (module == NULL)
{
LOG((LF_CORDB, LL_INFO100, "D::UM: module already unloaded AD:%#08x runtimeMod:%#08x modName:%ls\n",
pAppDomain, pRuntimeModule, pRuntimeModule->GetDebugName()));
goto LExit;
}
_ASSERTE(module != NULL);
STRESS_LOG3(LF_CORDB, LL_INFO10000, "D::UM: Unloading Mod:%#08x, %#08x, %#08x\n",
pRuntimeModule, pAppDomain, pRuntimeModule->IsIStream());
// Note: the appdomain the module was loaded in must match the appdomain we're unloading it from. If it doesn't,
// then we've either found the wrong DebuggerModule in LookupModule or we were passed bad data.
_ASSERTE(module->GetAppDomain() == pAppDomain);
// Send the unload module event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_UNLOAD_MODULE, thread, pAppDomain);
ipce->UnloadModuleData.vmDomainFile.SetRawPtr((module ? module->GetDomainFile() : NULL));
ipce->UnloadModuleData.debuggerAssemblyToken.Set(pRuntimeModule->GetClassLoader()->GetAssembly());
m_pRCThread->SendIPCEvent();
//
// Cleanup the module (only for resources consumed when a debugger is attached)
//
// Remove all patches that apply to this module/AppDomain combination
AppDomain* domainToRemovePatchesIn = NULL; // all domains by default
if( pRuntimeModule->GetAssembly()->IsDomainNeutral() )
{
// Deactivate all the patches specific to the AppDomain being unloaded
domainToRemovePatchesIn = pAppDomain;
}
// Note that we'll explicitly NOT delete DebuggerControllers, so that
// the Right Side can delete them later.
DebuggerController::RemovePatchesFromModule(pRuntimeModule, domainToRemovePatchesIn);
// Deactive all JMC functions in this module. We don't do this for shared assemblies
// because JMC status is not maintained on a per-AppDomain basis and we don't
// want to change the JMC behavior of the module in other domains.
if( !pRuntimeModule->GetAssembly()->IsDomainNeutral() )
{
LOG((LF_CORDB, LL_EVERYTHING, "Setting all JMC methods to false:\n"));
DebuggerDataLockHolder debuggerDataLockHolder(this);
DebuggerMethodInfoTable * pTable = GetMethodInfoTable();
if (pTable != NULL)
{
HASHFIND info;
for (DebuggerMethodInfo *dmi = pTable->GetFirstMethodInfo(&info);
dmi != NULL;
dmi = pTable->GetNextMethodInfo(&info))
{
if (dmi->m_module == pRuntimeModule)
{
dmi->SetJMCStatus(false);
}
}
}
LOG((LF_CORDB, LL_EVERYTHING, "Done clearing JMC methods!\n"));
}
// Delete the Left Side representation of the module.
if (m_pModules != NULL)
{
DebuggerDataLockHolder chInfo(this);
m_pModules->RemoveModule(pRuntimeModule, pAppDomain);
}
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::UM: Skipping SendIPCEvent because RS detached."));
}
LExit:
SENDIPCEVENT_END;
}
// Called when this module is completely gone from ALL AppDomains, regardless of
// whether a debugger is attached.
// Note that this doesn't get called until after the ADUnload is complete, which happens
// asyncronously in Whidbey (and won't happen at all if the process shuts down first).
// This is normally not called only domain-neutral assemblies because they can't be unloaded.
// However, it may be called if the loader fails to completely load a domain-neutral assembly.
void Debugger::DestructModule(Module *pModule)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO100, "D::DM: destruct module runtimeMod:%#08x modName:%ls\n",
pModule, pModule->GetDebugName()));
// @@@
// Implements DebugInterface.
// It is called for Module::Destruct. We do not need to send any IPC event.
DebuggerLockHolder dbgLockHolder(this);
// We should have removed all patches at AD unload time (or detach time if the
// debugger detached).
_ASSERTE( !DebuggerController::ModuleHasPatches(pModule) );
// Do module clean-up that applies even when no debugger is attached.
// Ideally, we might like to do this cleanup more eagerly and detministically,
// but we don't currently get any early AD unload callback from the loader
// when no debugger is attached. Perhaps we should make the loader
// call this callback earlier.
RemoveModuleReferences(pModule);
}
// Internal helper to remove all the DJIs / DMIs and other references for a given Module.
// If we don't remove the DJIs / DMIs, then we're subject to recycling bugs because the underlying
// MethodDescs will get removed. Thus we'll look up a new MD and it will pull up an old DMI that matched
// the old MD. Now the DMI and MD are out of sync and it's downhill from there.
// Note that DMIs may be used (and need cleanup) even when no debugger is attached.
void Debugger::RemoveModuleReferences( Module* pModule )
{
_ASSERTE( ThreadHoldsLock() );
// We want to remove all references to the module from the various
// tables. It's not just possible, but probable, that the module
// will be re-loaded at the exact same address, and in that case,
// we'll have piles of entries in our DJI table that mistakenly
// match this new module.
// Note that this doesn't apply to domain neutral assemblies, that only
// get unloaded when the process dies. We won't be reclaiming their
// DJIs/patches b/c the process is going to die, so we'll reclaim
// the memory when the various hashtables are unloaded.
if (m_pMethodInfos != NULL)
{
HRESULT hr = S_OK;
if (!HasLazyData())
{
hr = LazyInitWrapper();
}
if (SUCCEEDED(hr))
{
DebuggerDataLockHolder debuggerDataLockHolder(this);
m_pMethodInfos->ClearMethodsOfModule(pModule);
// DebuggerDataLockHolder out of scope - release implied
}
}
}
//---------------------------------------------------------------------------------------
//
// SendClassLoadUnloadEvent - notify the RS of a class either loading or unloading.
//
// Arguments:
//
// fAttaching - true if a debugger is in the process of attaching
//
// Return Value:
// None
//
//---------------------------------------------------------------------------------------
void Debugger::SendClassLoadUnloadEvent (mdTypeDef classMetadataToken,
DebuggerModule * pClassDebuggerModule,
Assembly *pAssembly,
AppDomain *pAppDomain,
BOOL fIsLoadEvent)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO10000, "D::SCLUE: Tok:0x%x isLoad:0x%x Mod:%#08x AD:%#08x\n",
classMetadataToken, fIsLoadEvent, pClassDebuggerModule, pAppDomain));
DebuggerIPCEvent * pEvent = m_pRCThread->GetIPCEventSendBuffer();
BOOL fIsReflection = pClassDebuggerModule->GetRuntimeModule()->IsReflection();
if (fIsLoadEvent == TRUE)
{
// We need to update Metadata before Symbols (since symbols depend on metadata)
// It's debatable which needs to come first: Class Load or Sym update.
// V1.1 sent Sym Update first so that binding at the class load has the latest symbols.
// However, The Class Load may need to be in sync with updating new metadata,
// and that has to come before the Sym update.
InitIPCEvent(pEvent, DB_IPCE_LOAD_CLASS, g_pEEInterface->GetThread(), pAppDomain);
pEvent->LoadClass.classMetadataToken = classMetadataToken;
pEvent->LoadClass.vmDomainFile.SetRawPtr((pClassDebuggerModule ? pClassDebuggerModule->GetDomainFile() : NULL));
pEvent->LoadClass.classDebuggerAssemblyToken.Set(pAssembly);
// For class loads in dynamic modules, RS knows that the metadata has now grown and is invalid.
// RS will re-fetch new metadata from out-of-process.
}
else
{
InitIPCEvent(pEvent, DB_IPCE_UNLOAD_CLASS, g_pEEInterface->GetThread(), pAppDomain);
pEvent->UnloadClass.classMetadataToken = classMetadataToken;
pEvent->UnloadClass.vmDomainFile.SetRawPtr((pClassDebuggerModule ? pClassDebuggerModule->GetDomainFile() : NULL));
pEvent->UnloadClass.classDebuggerAssemblyToken.Set(pAssembly);
}
m_pRCThread->SendIPCEvent();
if (fIsLoadEvent && fIsReflection)
{
// Send the raw event, but don't actually sync and block the runtime.
SendRawUpdateModuleSymsEvent(pClassDebuggerModule->GetRuntimeModule(), pAppDomain);
}
}
/******************************************************************************
*
******************************************************************************/
BOOL Debugger::SendSystemClassLoadUnloadEvent(mdTypeDef classMetadataToken,
Module *classModule,
BOOL fIsLoadEvent)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
if (!m_dClassLoadCallbackCount)
{
return FALSE;
}
BOOL fRetVal = FALSE;
Assembly *pAssembly = classModule->GetAssembly();
if (!m_pAppDomainCB->Lock())
return (FALSE);
AppDomainInfo *pADInfo = m_pAppDomainCB->FindFirst();
while (pADInfo != NULL)
{
AppDomain *pAppDomain = pADInfo->m_pAppDomain;
_ASSERTE(pAppDomain != NULL);
// Only notify for app domains where the module has been fully loaded already
// We used to make a different check here domain->ContainsAssembly() but that
// triggers too early in the loading process. FindDomainFile will not become
// non-NULL until the module is fully loaded into the domain which is what we
// want.
if ((classModule->FindDomainFile(pAppDomain) != NULL ) &&
!(fIsLoadEvent && pAppDomain->IsUnloading()) )
{
// Find the Left Side module that this class belongs in.
DebuggerModule* pModule = LookupOrCreateModule(classModule, pAppDomain);
_ASSERTE(pModule != NULL);
// Only send a class load event if they're enabled for this module.
if (pModule && pModule->ClassLoadCallbacksEnabled())
{
SendClassLoadUnloadEvent(classMetadataToken,
pModule,
pAssembly,
pAppDomain,
fIsLoadEvent);
fRetVal = TRUE;
}
}
pADInfo = m_pAppDomainCB->FindNext(pADInfo);
}
m_pAppDomainCB->Unlock();
return fRetVal;
}
//
// LoadClass is called when a Runtime thread loads a new Class.
// Returns TRUE if an event is sent, FALSE otherwise
BOOL Debugger::LoadClass(TypeHandle th,
mdTypeDef classMetadataToken,
Module *classModule,
AppDomain *pAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// @@@
// Implements DebugInterface
// This can be called by EE/Loader when class is loaded.
//
BOOL fRetVal = FALSE;
if (CORDBUnrecoverableError(this))
return FALSE;
// Note that pAppDomain may be null. The AppDomain isn't used here, and doesn't make a lot of sense since
// we may be delivering the notification for a class in an assembly which is loaded into multiple AppDomains. We
// handle this in SendSystemClassLoadUnloadEvent below by looping through all AppDomains and dispatching
// events for each that contain this assembly.
LOG((LF_CORDB, LL_INFO10000, "D::LC: load class Tok:%#08x Mod:%#08x AD:%#08x classMod:%#08x modName:%ls\n",
classMetadataToken, (pAppDomain == NULL) ? NULL : LookupOrCreateModule(classModule, pAppDomain),
pAppDomain, classModule, classModule->GetDebugName()));
//
// If we're attaching, then we only need to send the event. We
// don't need to disable event handling or lock the debugger
// object.
//
SENDIPCEVENT_BEGIN(this, g_pEEInterface->GetThread());
if (CORDebuggerAttached())
{
fRetVal = SendSystemClassLoadUnloadEvent(classMetadataToken, classModule, TRUE);
if (fRetVal == TRUE)
{
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::LC: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
return fRetVal;
}
//
// UnloadClass is called when a Runtime thread unloads a Class.
//
void Debugger::UnloadClass(mdTypeDef classMetadataToken,
Module *classModule,
AppDomain *pAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// @@@
// Implements DebugInterface
// Can only be called from EE
if (CORDBUnrecoverableError(this))
{
return;
}
LOG((LF_CORDB, LL_INFO10000, "D::UC: unload class Tok:0x%08x Mod:%#08x AD:%#08x runtimeMod:%#08x modName:%ls\n",
classMetadataToken, LookupOrCreateModule(classModule, pAppDomain), pAppDomain, classModule, classModule->GetDebugName()));
Assembly *pAssembly = classModule->GetClassLoader()->GetAssembly();
DebuggerModule *pModule = LookupOrCreateModule(classModule, pAppDomain);
if ((pModule == NULL) || !pModule->ClassLoadCallbacksEnabled())
{
return;
}
SENDIPCEVENT_BEGIN(this, g_pEEInterface->GetThread());
if (CORDebuggerAttached())
{
_ASSERTE((pAppDomain != NULL) && (pAssembly != NULL) && (pModule != NULL));
SendClassLoadUnloadEvent(classMetadataToken, pModule, pAssembly, pAppDomain, FALSE);
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::UC: Skipping SendIPCEvent because RS detached."));
}
// Let other Runtime threads handle their events.
SENDIPCEVENT_END;
}
/******************************************************************************
*
******************************************************************************/
void Debugger::FuncEvalComplete(Thread* pThread, DebuggerEval *pDE)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
#ifndef DACCESS_COMPILE
if (CORDBUnrecoverableError(this))
return;
LOG((LF_CORDB, LL_INFO10000, "D::FEC: func eval complete pDE:%08x evalType:%d %s %s\n",
pDE, pDE->m_evalType, pDE->m_successful ? "Success" : "Fail", pDE->m_aborted ? "Abort" : "Completed"));
_ASSERTE(pDE->m_completed);
_ASSERTE((g_pEEInterface->GetThread() && !g_pEEInterface->GetThread()->m_fPreemptiveGCDisabled) || g_fInControlC);
_ASSERTE(ThreadHoldsLock());
// If we need to rethrow a ThreadAbortException then set the thread's state so we remember that.
if (pDE->m_rethrowAbortException)
{
pThread->SetThreadStateNC(Thread::TSNC_DebuggerReAbort);
}
//
// Get the domain that the result is valid in. The RS will cache this in the ICorDebugValue
// Note: it's possible that the AppDomain has (or is about to be) unloaded, which could lead to a
// crash when we use the DebuggerModule. Ideally we'd only be using AppDomain IDs here.
// We can't easily convert our ADID to an AppDomain* (SystemDomain::GetAppDomainFromId)
// because we can't proove that that the AppDomain* would be valid (not unloaded).
//
AppDomain *pDomain = pThread->GetDomain();
AppDomain *pResultDomain = ((pDE->m_debuggerModule == NULL) ? pDomain : pDE->m_debuggerModule->GetAppDomain());
_ASSERTE( pResultDomain->GetId() == pDE->m_appDomainId );
// Send a func eval complete event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_FUNC_EVAL_COMPLETE, pThread, pDomain);
ipce->FuncEvalComplete.funcEvalKey = pDE->m_funcEvalKey;
ipce->FuncEvalComplete.successful = pDE->m_successful;
ipce->FuncEvalComplete.aborted = pDE->m_aborted;
ipce->FuncEvalComplete.resultAddr = &(pDE->m_result);
ipce->FuncEvalComplete.vmAppDomain.SetRawPtr(pResultDomain);
ipce->FuncEvalComplete.vmObjectHandle = pDE->m_vmObjectHandle;
LOG((LF_CORDB, LL_INFO10000, "D::FEC: TypeHandle is :%08x\n", pDE->m_resultType.AsPtr()));
Debugger::TypeHandleToExpandedTypeInfo(pDE->m_retValueBoxing, // whether return values get boxed or not depends on the particular FuncEval we're doing...
pResultDomain,
pDE->m_resultType,
&ipce->FuncEvalComplete.resultType);
_ASSERTE(ipce->FuncEvalComplete.resultType.elementType != ELEMENT_TYPE_VALUETYPE);
LOG((LF_CORDB, LL_INFO10000, "D::FEC: returned from call\n"));
// We must adjust the result address to point to the right place
ipce->FuncEvalComplete.resultAddr = ArgSlotEndianessFixup((ARG_SLOT*)ipce->FuncEvalComplete.resultAddr,
GetSizeForCorElementType(ipce->FuncEvalComplete.resultType.elementType));
m_pRCThread->SendIPCEvent();
#endif
}
/******************************************************************************
*
******************************************************************************/
bool Debugger::ResumeThreads(AppDomain* pAppDomain)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(ThisIsHelperThreadWorker());
}
CONTRACTL_END;
// Okay, mark that we're not stopped anymore and let the
// Runtime threads go...
ReleaseAllRuntimeThreads(pAppDomain);
// Return that we've continued the process.
return true;
}
class CodeBuffer
{
public:
BYTE *getCodeBuffer(DebuggerJitInfo *dji)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
CodeRegionInfo codeRegionInfo = CodeRegionInfo::GetCodeRegionInfo(dji);
if (codeRegionInfo.getAddrOfColdCode())
{
_ASSERTE(codeRegionInfo.getSizeOfHotCode() != 0);
_ASSERTE(codeRegionInfo.getSizeOfColdCode() != 0);
S_SIZE_T totalSize = S_SIZE_T( codeRegionInfo.getSizeOfHotCode() ) +
S_SIZE_T( codeRegionInfo.getSizeOfColdCode() );
if ( totalSize.IsOverflow() )
{
_ASSERTE(0 && "Buffer overflow error in getCodeBuffer");
return NULL;
}
BYTE *code = (BYTE *) buffer.AllocNoThrow( totalSize.Value() );
if (code)
{
memcpy(code,
(void *) codeRegionInfo.getAddrOfHotCode(),
codeRegionInfo.getSizeOfHotCode());
memcpy(code + codeRegionInfo.getSizeOfHotCode(),
(void *) codeRegionInfo.getAddrOfColdCode(),
codeRegionInfo.getSizeOfColdCode());
// Now patch the control transfer instructions
}
return code;
}
else
{
return dac_cast<PTR_BYTE>(codeRegionInfo.getAddrOfHotCode());
}
}
private:
CQuickBytes buffer;
};
//---------------------------------------------------------------------------------------
//
// Called on the helper thread to serialize metadata so it can be read out-of-process.
//
// Arguments:
// pModule - module that needs metadata serialization
// countBytes - out value, holds the number of bytes which were allocated in the
// serialized buffer
//
// Return Value:
// A pointer to a serialized buffer of metadata. The caller should free this bufer using
// DeleteInteropSafe
//
// Assumptions:
// This is called on the helper-thread, or a thread pretending to be the helper-thread.
// For any synchronous message, the debuggee should be synchronized. The only async
// messages are Attach and Async-Break.
//
//
//---------------------------------------------------------------------------------------
BYTE* Debugger::SerializeModuleMetaData(Module * pModule, DWORD * countBytes)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "Debugger::SMMD called\n"));
// Do not release the emitter. This is a weak reference.
IMetaDataEmit *pEmitter = pModule->GetEmitter();
_ASSERTE(pEmitter != NULL);
HRESULT hr;
BYTE* metadataBuffer = NULL;
ReleaseHolder<IMDInternalEmit> pInternalEmitter;
ULONG originalUpdateMode;
hr = pEmitter->QueryInterface(IID_IMDInternalEmit, (void **)&pInternalEmitter);
if(FAILED(hr))
{
LOG((LF_CORDB, LL_INFO10, "Debugger::SMMD pEmitter doesn't support IID_IMDInternalEmit hr=0x%x\n", hr));
ThrowHR(hr);
}
_ASSERTE(pInternalEmitter != NULL);
hr = pInternalEmitter->SetMDUpdateMode(MDUpdateExtension, &originalUpdateMode);
if(FAILED(hr))
{
LOG((LF_CORDB, LL_INFO10, "Debugger::SMMD SetMDUpdateMode failed hr=0x%x\n", hr));
ThrowHR(hr);
}
_ASSERTE(originalUpdateMode == MDUpdateFull);
hr = pEmitter->GetSaveSize(cssQuick, countBytes);
if(FAILED(hr))
{
LOG((LF_CORDB, LL_INFO10, "Debugger::SMMD GetSaveSize failed hr=0x%x\n", hr));
pInternalEmitter->SetMDUpdateMode(originalUpdateMode, NULL);
ThrowHR(hr);
}
EX_TRY
{
metadataBuffer = new (interopsafe) BYTE[*countBytes];
}
EX_CATCH
{
LOG((LF_CORDB, LL_INFO10, "Debugger::SMMD Allocation failed\n"));
pInternalEmitter->SetMDUpdateMode(originalUpdateMode, NULL);
EX_RETHROW;
}
EX_END_CATCH(SwallowAllExceptions);
_ASSERTE(metadataBuffer != NULL); // allocation would throw first
// Caller ensures serialization that guarantees that the metadata doesn't grow underneath us.
hr = pEmitter->SaveToMemory(metadataBuffer, *countBytes);
if(FAILED(hr))
{
LOG((LF_CORDB, LL_INFO10, "Debugger::SMMD SaveToMemory failed hr=0x%x\n", hr));
DeleteInteropSafe(metadataBuffer);
pInternalEmitter->SetMDUpdateMode(originalUpdateMode, NULL);
ThrowHR(hr);
}
pInternalEmitter->SetMDUpdateMode(originalUpdateMode, NULL);
LOG((LF_CORDB, LL_INFO10000, "Debugger::SMMD exiting\n"));
return metadataBuffer;
}
#ifdef FEATURE_LEGACYNETCF_DBG_HOST_CONTROL
//---------------------------------------------------------------------------------------
//
// Called on the helper thread to send a pause notification to the host
//
//
// This is called on the helper-thread, or a thread pretending to be the helper-thread.
// The debuggee should be synchronized. This callback to the host is only supported
// on Windows Phone as a replacement for some legacy NetCF behavior. In general I don't
// like being the transport between the VS debugger and the host, so don't use
// this as precedent that we should start making more callbacks for them. In the future
// the debugger and host should make alternative arrangements such as window messages,
// out of proc event signaling, or any other IPC mechanism.
//
// This should be deprecated as soon as mixed-mode debugging is available. The
// end goal on phone is to pause the UI thread while VS is in the break state. That
// will be accomplished by a mixed-mode debugger suspending all native threads when
// it breaks rather than having us send a special message.
//
//---------------------------------------------------------------------------------------
VOID Debugger::InvokeLegacyNetCFHostPauseCallback()
{
IHostNetCFDebugControlManager* pHostCallback = CorHost2::GetHostNetCFDebugControlManager();
if(pHostCallback != NULL)
{
pHostCallback->NotifyPause(0);
}
}
//---------------------------------------------------------------------------------------
//
// Called on the helper thread to send a resume notification to the host
//
//
// This is called on the helper-thread, or a thread pretending to be the helper-thread.
// The debuggee should be synchronized. This callback to the host is only supported
// on Windows Phone as a replacement for some legacy NetCF behavior. In general I don't
// like being the transport between the VS debugger and the host, so don't use
// this as precedent that we should start making more callbacks for them. In the future
// the debugger and host should make alternative arrangements such as window messages,
// out of proc event signaling, or any other IPC mechanism.
//
// This should be deprecated as soon as mixed-mode debugging is available. The
// end goal on phone is to pause the UI thread while VS is in the break state. That
// will be accomplished by a mixed-mode debugger suspending all native threads when
// it breaks rather than having us send a special message.
//
//---------------------------------------------------------------------------------------
VOID Debugger::InvokeLegacyNetCFHostResumeCallback()
{
IHostNetCFDebugControlManager* pHostCallback = CorHost2::GetHostNetCFDebugControlManager();
if(pHostCallback != NULL)
{
pHostCallback->NotifyResume(0);
}
}
#endif //FEATURE_LEGACYNETCF_DBG_HOST_CONTROL
//---------------------------------------------------------------------------------------
//
// Handle an IPC event from the Debugger.
//
// Arguments:
// event - IPC event to handle.
//
// Return Value:
// True if the event was a continue. Else false.
//
// Assumptions:
// This is called on the helper-thread, or a thread pretending to be the helper-thread.
// For any synchronous message, the debuggee should be synchronized. The only async
// messages are Attach and Async-Break.
//
// Notes:
// HandleIPCEvent is called by the RC thread in response to an event
// from the Debugger Interface. No other IPC events, nor any Runtime
// events will come in until this method returns. Returns true if this
// was a Continue event.
//
// If this function is called on native debugger helper thread, we will
// handle everything. However if this is called on managed thread doing
// helper thread duty, we will fail on operation since we are mainly
// waiting for CONTINUE message from the RS.
//
//
//---------------------------------------------------------------------------------------
#ifdef _PREFAST_
#pragma warning(push)
#pragma warning(disable:21000) // Suppress PREFast warning about overly large function
#endif
bool Debugger::HandleIPCEvent(DebuggerIPCEvent * pEvent)
{
CONTRACTL
{
THROWS;
if (g_pEEInterface->GetThread() != NULL) { GC_TRIGGERS; } else { GC_NOTRIGGER; }
PRECONDITION(ThisIsHelperThreadWorker());
if (m_stopped)
{
MODE_COOPERATIVE;
}
else
{
MODE_ANY;
}
}
CONTRACTL_END;
// If we're the temporary helper thread, then we may reject certain operations.
bool temporaryHelp = ThisIsTempHelperThread();
#ifdef _DEBUG
// This reg key allows us to test our unhandled event filter installed in HandleIPCEventWrapper
// to make sure it works properly.
static int s_fDbgFaultInHandleIPCEvent = -1;
if (s_fDbgFaultInHandleIPCEvent == -1)
{
s_fDbgFaultInHandleIPCEvent = UnsafeGetConfigDWORD(CLRConfig::INTERNAL_DbgFaultInHandleIPCEvent);
}
// If we need to fault, let's generate an access violation.
if (s_fDbgFaultInHandleIPCEvent)
{
*((volatile BYTE *)0) = 0;
}
#endif
BOOL fSuccess;
bool fContinue = false;
HRESULT hr = S_OK;
LOG((LF_CORDB, LL_INFO10000, "D::HIPCE: got %s\n", IPCENames::GetName(pEvent->type)));
DbgLog((DebuggerIPCEventType)(pEvent->type & DB_IPCE_TYPE_MASK));
// As for runtime is considered stopped, it means that managed threads will not
// execute anymore managed code. However, these threads may be still running for
// unmanaged code. So it is not true that we do not need to hold the lock while processing
// synchrnoized event.
//
// The worst of all, it is the special case where user break point and exception can
// be sent as part of attach if debugger was launched by managed app.
//
DebuggerLockHolder dbgLockHolder(this, FALSE);
if ((pEvent->type & DB_IPCE_TYPE_MASK) == DB_IPCE_ASYNC_BREAK ||
(pEvent->type & DB_IPCE_TYPE_MASK) == DB_IPCE_ATTACHING)
{
dbgLockHolder.Acquire();
}
else
{
_ASSERTE(m_stopped);
_ASSERTE(ThreadHoldsLock());
}
switch (pEvent->type & DB_IPCE_TYPE_MASK)
{
case DB_IPCE_ATTACHING:
// In V3, Attach is atomic, meaning that there isn't a complex handshake back and forth between LS + RS.
// the RS sends a single-attaching event and attaches at the first response from the Left-side.
StartCanaryThread();
// In V3 after attaching event was handled we iterate throughout all ADs and made shadow copies of PDBs in the BIN directories.
// After all AppDomain, DomainAssembly and modules iteration was available in out-of-proccess model in V4 the code that enables
// PDBs to be copied was not called at attach time.
// Eliminating PDBs copying side effect is an issue: Dev10 #927143
EX_TRY
{
IterateAppDomainsForPdbs();
}
EX_CATCH_HRESULT(hr); // ignore failures
if (m_jitAttachInProgress)
{
// For jit-attach, mark that we're attached now.
// This lets callers to code:Debugger.JitAttach check the flag and
// send the jit-attach event just like a normal event.
MarkDebuggerAttachedInternal();
// set the managed attach event so that waiting threads can continue
VERIFY(SetEvent(GetAttachEvent()));
break;
}
VERIFY(SetEvent(GetAttachEvent()));
//
// For regular (non-jit) attach, fall through to do an async break.
//
case DB_IPCE_ASYNC_BREAK:
{
if (temporaryHelp)
{
// Don't support async break on temporary helper thread.
// Well, this function does not return HR. So this means that
// ASYNC_BREAK event will be catching silently while we are
// doing helper thread duty!
//
hr = CORDBG_E_NOTREADY;
}
else
{
// not synchornized. We get debugger lock upon the function entry
_ASSERTE(ThreadHoldsLock());
// Simply trap all Runtime threads if we're not already trying to.
if (!m_trappingRuntimeThreads)
{
// If the RS sent an Async-break, then that's an explicit request.
m_RSRequestedSync = TRUE;
TrapAllRuntimeThreads(); // Non-blocking...
}
}
break;
}
case DB_IPCE_CONTINUE:
{
GetCanary()->ClearCache();
fContinue = ResumeThreads(pEvent->vmAppDomain.GetRawPtr());
//
// Go ahead and release the TSL now that we're continuing. This ensures that we've held
// the thread store lock the entire time the Runtime was just stopped.
//
ThreadSuspend::UnlockThreadStore(FALSE, ThreadSuspend::SUSPEND_FOR_DEBUGGER);
break;
}
case DB_IPCE_BREAKPOINT_ADD:
{
//
// Currently, we can't create a breakpoint before a
// function desc is available.
// Also, we can't know if a breakpoint is ok
// prior to the method being JITted.
//
_ASSERTE(hr == S_OK);
DebuggerBreakpoint * pDebuggerBP = NULL;
DebuggerModule * pDebuggerModule = LookupOrCreateModule(pEvent->BreakpointData.vmDomainFile);
Module * pModule = pDebuggerModule->GetRuntimeModule();
DebuggerMethodInfo * pDMI = GetOrCreateMethodInfo(pModule, pEvent->BreakpointData.funcMetadataToken);
MethodDesc * pMethodDesc = pEvent->BreakpointData.nativeCodeMethodDescToken.UnWrap();
DebuggerJitInfo * pDJI = NULL;
if ((pMethodDesc != NULL) && (pDMI != NULL))
{
pDJI = pDMI->FindOrCreateInitAndAddJitInfo(pMethodDesc);
}
{
// If we haven't been either JITted or EnC'd yet, then
// we'll put a patch in by offset, implicitly relative
// to the first version of the code.
pDebuggerBP = new (interopsafe, nothrow) DebuggerBreakpoint(pModule,
pEvent->BreakpointData.funcMetadataToken,
pEvent->vmAppDomain.GetRawPtr(),
pEvent->BreakpointData.offset,
!pEvent->BreakpointData.isIL,
pEvent->BreakpointData.encVersion,
pMethodDesc,
pDJI,
&fSuccess);
TRACE_ALLOC(pDebuggerBP);
if ((pDebuggerBP != NULL) && !fSuccess)
{
DeleteInteropSafe(pDebuggerBP);
pDebuggerBP = NULL;
hr = CORDBG_E_UNABLE_TO_SET_BREAKPOINT;
}
}
if ((pDebuggerBP == NULL) && !FAILED(hr))
{
hr = E_OUTOFMEMORY;
}
LOG((LF_CORDB,LL_INFO10000,"\tBP Add: BPTOK:"
"0x%x, tok=0x%08x, offset=0x%x, isIL=%d dm=0x%x m=0x%x\n",
pDebuggerBP,
pEvent->BreakpointData.funcMetadataToken,
pEvent->BreakpointData.offset,
pEvent->BreakpointData.isIL,
pDebuggerModule,
pModule));
//
// We're using a two-way event here, so we place the
// result event into the _receive_ buffer, not the send
// buffer.
//
DebuggerIPCEvent * pIPCResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pIPCResult,
DB_IPCE_BREAKPOINT_ADD_RESULT,
g_pEEInterface->GetThread(),
pEvent->vmAppDomain);
pIPCResult->BreakpointData.breakpointToken.Set(pDebuggerBP);
pIPCResult->hr = hr;
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_STEP:
{
LOG((LF_CORDB,LL_INFO10000, "D::HIPCE: stepIn:0x%x frmTok:0x%x"
"StepIn:0x%x RangeIL:0x%x RangeCount:0x%x MapStop:0x%x "
"InterceptStop:0x%x AppD:0x%x\n",
pEvent->StepData.stepIn,
pEvent->StepData.frameToken.GetSPValue(),
pEvent->StepData.stepIn,
pEvent->StepData.rangeIL,
pEvent->StepData.rangeCount,
pEvent->StepData.rgfMappingStop,
pEvent->StepData.rgfInterceptStop,
pEvent->vmAppDomain.GetRawPtr()));
// <TODO>@todo memory allocation - bad if we're synced</TODO>
Thread * pThread = pEvent->StepData.vmThreadToken.GetRawPtr();
AppDomain * pAppDomain = pEvent->vmAppDomain.GetRawPtr();
DebuggerIPCEvent * pIPCResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pIPCResult,
DB_IPCE_STEP_RESULT,
pThread,
pEvent->vmAppDomain);
if (temporaryHelp)
{
// Can't step on the temporary helper thread.
pIPCResult->hr = CORDBG_E_NOTREADY;
}
else
{
DebuggerStepper * pStepper;
if (pEvent->StepData.IsJMCStop)
{
pStepper = new (interopsafe, nothrow) DebuggerJMCStepper(pThread,
pEvent->StepData.rgfMappingStop,
pEvent->StepData.rgfInterceptStop,
pAppDomain);
}
else
{
pStepper = new (interopsafe, nothrow) DebuggerStepper(pThread,
pEvent->StepData.rgfMappingStop,
pEvent->StepData.rgfInterceptStop,
pAppDomain);
}
if (pStepper == NULL)
{
pIPCResult->hr = E_OUTOFMEMORY;
m_pRCThread->SendIPCReply();
break;
}
TRACE_ALLOC(pStepper);
unsigned int cRanges = pEvent->StepData.totalRangeCount;
_ASSERTE(cRanges == 0 || ((cRanges > 0) && (cRanges == pEvent->StepData.rangeCount)));
if (!pStepper->Step(pEvent->StepData.frameToken,
pEvent->StepData.stepIn,
&(pEvent->StepData.range),
cRanges,
((cRanges > 0) ? pEvent->StepData.rangeIL : false)))
{
pIPCResult->hr = E_OUTOFMEMORY;
m_pRCThread->SendIPCReply();
DeleteInteropSafe(pStepper);
break;
}
pIPCResult->StepData.stepperToken.Set(pStepper);
} // end normal step case.
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_STEP_OUT:
{
// <TODO>@todo memory allocation - bad if we're synced</TODO>
Thread * pThread = pEvent->StepData.vmThreadToken.GetRawPtr();
AppDomain * pAppDomain = pEvent->vmAppDomain.GetRawPtr();
DebuggerIPCEvent * pIPCResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pIPCResult,
DB_IPCE_STEP_RESULT,
pThread,
pAppDomain);
if (temporaryHelp)
{
// Can't step on the temporary helper thread.
pIPCResult->hr = CORDBG_E_NOTREADY;
}
else
{
DebuggerStepper * pStepper;
if (pEvent->StepData.IsJMCStop)
{
pStepper = new (interopsafe, nothrow) DebuggerJMCStepper(pThread,
pEvent->StepData.rgfMappingStop,
pEvent->StepData.rgfInterceptStop,
pAppDomain);
}
else
{
pStepper = new (interopsafe, nothrow) DebuggerStepper(pThread,
pEvent->StepData.rgfMappingStop,
pEvent->StepData.rgfInterceptStop,
pAppDomain);
}
if (pStepper == NULL)
{
pIPCResult->hr = E_OUTOFMEMORY;
m_pRCThread->SendIPCReply();
break;
}
TRACE_ALLOC(pStepper);
// Safe to stack trace b/c we're stopped.
StackTraceTicket ticket(pThread);
pStepper->StepOut(pEvent->StepData.frameToken, ticket);
pIPCResult->StepData.stepperToken.Set(pStepper);
}
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_BREAKPOINT_REMOVE:
{
// <TODO>@todo memory allocation - bad if we're synced</TODO>
DebuggerBreakpoint * pDebuggerBP = pEvent->BreakpointData.breakpointToken.UnWrap();
pDebuggerBP->Delete();
}
break;
case DB_IPCE_STEP_CANCEL:
{
// <TODO>@todo memory allocation - bad if we're synced</TODO>
LOG((LF_CORDB,LL_INFO10000, "D:HIPCE:Got STEP_CANCEL for stepper 0x%p\n",
pEvent->StepData.stepperToken.UnWrap()));
DebuggerStepper * pStepper = pEvent->StepData.stepperToken.UnWrap();
pStepper->Delete();
}
break;
case DB_IPCE_SET_ALL_DEBUG_STATE:
{
Thread * pThread = pEvent->SetAllDebugState.vmThreadToken.GetRawPtr();
CorDebugThreadState debugState = pEvent->SetAllDebugState.debugState;
LOG((LF_CORDB,LL_INFO10000,"HandleIPCE: SetAllDebugState: except thread 0x%08x (ID:0x%x) to state 0x%x\n",
pThread,
(pThread != NULL) ? GetThreadIdHelper(pThread) : 0,
debugState));
if (!g_fProcessDetach)
{
g_pEEInterface->SetAllDebugState(pThread, debugState);
}
STRESS_LOG1(LF_CORDB,LL_INFO10000,"HandleIPC: Got 0x%x back from SetAllDebugState\n", hr);
// Just send back an HR.
DebuggerIPCEvent * pIPCResult = m_pRCThread->GetIPCEventReceiveBuffer();
PREFIX_ASSUME(pIPCResult != NULL);
InitIPCEvent(pIPCResult, DB_IPCE_SET_DEBUG_STATE_RESULT, NULL, NULL);
pIPCResult->hr = S_OK;
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_GET_GCHANDLE_INFO:
// Given an unvalidated GC-handle, find out all the info about it to view the object
// at the other end
{
OBJECTHANDLE objectHandle = pEvent->GetGCHandleInfo.GCHandle.GetRawPtr();
DebuggerIPCEvent * pIPCResult = m_pRCThread->GetIPCEventReceiveBuffer();
PREFIX_ASSUME(pIPCResult != NULL);
InitIPCEvent(pIPCResult, DB_IPCE_GET_GCHANDLE_INFO_RESULT, NULL, NULL);
bool fValid = SUCCEEDED(ValidateGCHandle(objectHandle));
AppDomain * pAppDomain = NULL;
if(fValid)
{
// Get the appdomain
ADIndex appDomainIndex = HndGetHandleADIndex(objectHandle);
pAppDomain = SystemDomain::GetAppDomainAtIndex(appDomainIndex);
_ASSERTE(pAppDomain != NULL);
}
pIPCResult->hr = S_OK;
pIPCResult->GetGCHandleInfoResult.vmAppDomain.SetRawPtr(pAppDomain);
pIPCResult->GetGCHandleInfoResult.fValid = fValid;
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_GET_BUFFER:
{
GetAndSendBuffer(m_pRCThread, pEvent->GetBuffer.bufSize);
}
break;
case DB_IPCE_RELEASE_BUFFER:
{
SendReleaseBuffer(m_pRCThread, pEvent->ReleaseBuffer.pBuffer);
}
break;
#ifdef EnC_SUPPORTED
case DB_IPCE_APPLY_CHANGES:
{
LOG((LF_ENC, LL_INFO100, "D::HIPCE: DB_IPCE_APPLY_CHANGES 1\n"));
DebuggerModule * pDebuggerModule = LookupOrCreateModule(pEvent->ApplyChanges.vmDomainFile);
//
// @todo handle error.
//
hr = ApplyChangesAndSendResult(pDebuggerModule,
pEvent->ApplyChanges.cbDeltaMetadata,
(BYTE*) CORDB_ADDRESS_TO_PTR(pEvent->ApplyChanges.pDeltaMetadata),
pEvent->ApplyChanges.cbDeltaIL,
(BYTE*) CORDB_ADDRESS_TO_PTR(pEvent->ApplyChanges.pDeltaIL));
LOG((LF_ENC, LL_INFO100, "D::HIPCE: DB_IPCE_APPLY_CHANGES 2\n"));
}
break;
#endif // EnC_SUPPORTED
case DB_IPCE_SET_CLASS_LOAD_FLAG:
{
DebuggerModule *pDebuggerModule = LookupOrCreateModule(pEvent->SetClassLoad.vmDomainFile);
_ASSERTE(pDebuggerModule != NULL);
LOG((LF_CORDB, LL_INFO10000,
"D::HIPCE: class load flag is %d for module 0x%p\n",
pEvent->SetClassLoad.flag,
pDebuggerModule));
pDebuggerModule->EnableClassLoadCallbacks((BOOL)pEvent->SetClassLoad.flag);
}
break;
case DB_IPCE_IS_TRANSITION_STUB:
GetAndSendTransitionStubInfo((CORDB_ADDRESS_TYPE*)pEvent->IsTransitionStub.address);
break;
case DB_IPCE_MODIFY_LOGSWITCH:
g_pEEInterface->DebuggerModifyingLogSwitch (pEvent->LogSwitchSettingMessage.iLevel,
pEvent->LogSwitchSettingMessage.szSwitchName.GetString());
break;
case DB_IPCE_ENABLE_LOG_MESSAGES:
{
bool fOnOff = pEvent->LogSwitchSettingMessage.iLevel ? true : false;
EnableLogMessages (fOnOff);
}
break;
case DB_IPCE_SET_IP:
{
// This is a synchronous event (reply required)
DebuggerIPCEvent * pIPCResult = m_pRCThread->GetIPCEventReceiveBuffer();
// Don't have an explicit reply msg
InitIPCReply(pIPCResult, DB_IPCE_SET_IP);
if (temporaryHelp)
{
pIPCResult->hr = CORDBG_E_NOTREADY;
}
else if (!g_fProcessDetach)
{
//
// Since this pointer is coming from the RS, it may be NULL or something
// unexpected in an OOM situation. Quickly just sanity check them.
//
Thread * pThread = pEvent->SetIP.vmThreadToken.GetRawPtr();
Module * pModule = pEvent->SetIP.vmDomainFile.GetRawPtr()->GetModule();
// Get the DJI for this function
DebuggerMethodInfo * pDMI = GetOrCreateMethodInfo(pModule, pEvent->SetIP.mdMethod);
DebuggerJitInfo * pDJI = NULL;
if (pDMI != NULL)
{
// In the EnC case, if we look for an older version, we need to find the DJI by starting
// address, rather than just by MethodDesc. In the case of generics, we may need to create a DJI, so we
pDJI = pDMI->FindJitInfo(pEvent->SetIP.vmMethodDesc.GetRawPtr(),
(TADDR)pEvent->SetIP.startAddress);
if (pDJI == NULL)
{
// In the case of other functions, we may need to lazily create a DJI, so we need
// FindOrCreate semantics for those.
pDJI = pDMI->FindOrCreateInitAndAddJitInfo(pEvent->SetIP.vmMethodDesc.GetRawPtr());
}
}
if ((pDJI != NULL) && (pThread != NULL) && (pModule != NULL))
{
CHECK_IF_CAN_TAKE_HELPER_LOCKS_IN_THIS_SCOPE(&(pIPCResult->hr), GetCanary());
if (SUCCEEDED(pIPCResult->hr))
{
pIPCResult->hr = SetIP(pEvent->SetIP.fCanSetIPOnly,
pThread,
pModule,
pEvent->SetIP.mdMethod,
pDJI,
pEvent->SetIP.offset,
pEvent->SetIP.fIsIL
);
}
}
else
{
pIPCResult->hr = E_INVALIDARG;
}
}
else
{
pIPCResult->hr = S_OK;
}
// Send the result
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_DETACH_FROM_PROCESS:
LOG((LF_CORDB, LL_INFO10000, "Detaching from process!\n"));
// Delete all controllers (remove patches etc.)
DebuggerController::DeleteAllControllers();
// Note that we'd like to be able to do this assert here
// _ASSERTE(DebuggerController::GetNumberOfPatches() == 0);
// However controllers may get queued for deletion if there is outstanding
// work and so we can't gaurentee the deletion will complete now.
// @dbgtodo inspection: This shouldn't be an issue in the complete V3 architecture
MarkDebuggerUnattachedInternal();
m_pRCThread->RightSideDetach();
// Clear JMC status
{
LOG((LF_CORDB, LL_EVERYTHING, "Setting all JMC methods to false:\n"));
// On detach, set all DMI's JMC status to false.
// We have to do this b/c we clear the DebuggerModules and allocated
// new ones on re-attach; and the DMI & DM need to be in sync
// (in this case, agreeing that JMC-status = false).
// This also syncs the EE modules and disables all JMC probes.
DebuggerMethodInfoTable * pMethodInfoTable = g_pDebugger->GetMethodInfoTable();
if (pMethodInfoTable != NULL)
{
HASHFIND hashFind;
DebuggerDataLockHolder debuggerDataLockHolder(this);
for (DebuggerMethodInfo * pMethodInfo = pMethodInfoTable->GetFirstMethodInfo(&hashFind);
pMethodInfo != NULL;
pMethodInfo = pMethodInfoTable->GetNextMethodInfo(&hashFind))
{
pMethodInfo->SetJMCStatus(false);
}
}
LOG((LF_CORDB, LL_EVERYTHING, "Done clearing JMC methods!\n"));
}
// Clean up the hash of DebuggerModules
// This method is overridden to also free all DebuggerModule objects
if (m_pModules != NULL)
{
// Removes all DebuggerModules
DebuggerDataLockHolder ch(this);
m_pModules->Clear();
}
// Reply to the detach message before we release any Runtime threads. This ensures that the debugger will get
// the detach reply before the process exits if the main thread is near exiting.
m_pRCThread->SendIPCReply();
// Let the process run free now... there is no debugger to bother it anymore.
fContinue = ResumeThreads(NULL);
//
// Go ahead and release the TSL now that we're continuing. This ensures that we've held
// the thread store lock the entire time the Runtime was just stopped.
//
ThreadSuspend::UnlockThreadStore(FALSE, ThreadSuspend::SUSPEND_FOR_DEBUGGER);
break;
#ifndef DACCESS_COMPILE
case DB_IPCE_FUNC_EVAL:
{
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
Thread * pThread = pEvent->FuncEval.vmThreadToken.GetRawPtr();
InitIPCEvent(pEvent, DB_IPCE_FUNC_EVAL_SETUP_RESULT, pThread, pThread->GetDomain());
BYTE * pbArgDataArea = NULL;
DebuggerEval * pDebuggerEvalKey = NULL;
pEvent->hr = FuncEvalSetup(&(pEvent->FuncEval), &pbArgDataArea, &pDebuggerEvalKey);
// Send the result of how the func eval setup went.
pEvent->FuncEvalSetupComplete.argDataArea = PTR_TO_CORDB_ADDRESS(pbArgDataArea);
pEvent->FuncEvalSetupComplete.debuggerEvalKey.Set(pDebuggerEvalKey);
m_pRCThread->SendIPCReply();
}
break;
#endif
case DB_IPCE_SET_REFERENCE:
{
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent, DB_IPCE_SET_REFERENCE_RESULT);
pEvent->hr = SetReference(pEvent->SetReference.objectRefAddress,
pEvent->SetReference.vmObjectHandle,
pEvent->SetReference.newReference);
// Send the result of how the set reference went.
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_SET_VALUE_CLASS:
{
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent, DB_IPCE_SET_VALUE_CLASS_RESULT);
pEvent->hr = SetValueClass(pEvent->SetValueClass.oldData,
pEvent->SetValueClass.newData,
&pEvent->SetValueClass.type);
// Send the result of how the set reference went.
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_GET_THREAD_FOR_TASKID:
{
TASKID taskid = pEvent->GetThreadForTaskId.taskid;
Thread *pThread = ThreadStore::GetThreadList(NULL);
Thread *pThreadRet = NULL;
while (pThread != NULL)
{
if (pThread->GetTaskId() == taskid)
{
pThreadRet = pThread;
break;
}
pThread = ThreadStore::GetThreadList(pThread);
}
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent, DB_IPCE_GET_THREAD_FOR_TASKID_RESULT);
pEvent->GetThreadForTaskIdResult.vmThreadToken.SetRawPtr(pThreadRet);
pEvent->hr = S_OK;
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_CREATE_HANDLE:
{
Object * pObject = (Object*)pEvent->CreateHandle.objectToken;
OBJECTREF objref = ObjectToOBJECTREF(pObject);
AppDomain * pAppDomain = pEvent->vmAppDomain.GetRawPtr();
BOOL fStrong = pEvent->CreateHandle.fStrong;
OBJECTHANDLE objectHandle;
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent, DB_IPCE_CREATE_HANDLE_RESULT);
{
// Handle creation may need to allocate memory.
// The API specifically limits the number of handls Cordbg can create,
// so we could preallocate and fail allocating anything beyond that.
CHECK_IF_CAN_TAKE_HELPER_LOCKS_IN_THIS_SCOPE(&(pEvent->hr), GetCanary());
if (SUCCEEDED(pEvent->hr))
{
if (fStrong == TRUE)
{
// create strong handle
objectHandle = pAppDomain->CreateStrongHandle(objref);
}
else
{
// create the weak long handle
objectHandle = pAppDomain->CreateLongWeakHandle(objref);
}
pEvent->CreateHandleResult.vmObjectHandle.SetRawPtr(objectHandle);
}
}
m_pRCThread->SendIPCReply();
break;
}
case DB_IPCE_DISPOSE_HANDLE:
{
// DISPOSE an object handle
OBJECTHANDLE objectHandle = pEvent->DisposeHandle.vmObjectHandle.GetRawPtr();
if (pEvent->DisposeHandle.fStrong == TRUE)
{
DestroyStrongHandle(objectHandle);
}
else
{
DestroyLongWeakHandle(objectHandle);
}
break;
}
#ifndef DACCESS_COMPILE
case DB_IPCE_FUNC_EVAL_ABORT:
{
LOG((LF_CORDB, LL_INFO1000, "D::HIPCE: Got FuncEvalAbort for pDE:%08x\n",
pEvent->FuncEvalAbort.debuggerEvalKey.UnWrap()));
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent,DB_IPCE_FUNC_EVAL_ABORT_RESULT);
pEvent->hr = FuncEvalAbort(pEvent->FuncEvalAbort.debuggerEvalKey.UnWrap());
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_FUNC_EVAL_RUDE_ABORT:
{
LOG((LF_CORDB, LL_INFO1000, "D::HIPCE: Got FuncEvalRudeAbort for pDE:%08x\n",
pEvent->FuncEvalRudeAbort.debuggerEvalKey.UnWrap()));
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent, DB_IPCE_FUNC_EVAL_RUDE_ABORT_RESULT);
pEvent->hr = FuncEvalRudeAbort(pEvent->FuncEvalRudeAbort.debuggerEvalKey.UnWrap());
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_FUNC_EVAL_CLEANUP:
// This is a synchronous event (reply required)
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent,DB_IPCE_FUNC_EVAL_CLEANUP_RESULT);
pEvent->hr = FuncEvalCleanup(pEvent->FuncEvalCleanup.debuggerEvalKey.UnWrap());
m_pRCThread->SendIPCReply();
break;
#endif
case DB_IPCE_CONTROL_C_EVENT_RESULT:
{
// store the result of whether the event has been handled by the debugger and
// wake up the thread waiting for the result
SetDebuggerHandlingCtrlC(pEvent->hr == S_OK);
VERIFY(SetEvent(GetCtrlCMutex()));
}
break;
// Set the JMC status on invididual methods
case DB_IPCE_SET_METHOD_JMC_STATUS:
{
// Get the info out of the event
DebuggerModule * pDebuggerModule = LookupOrCreateModule(pEvent->SetJMCFunctionStatus.vmDomainFile);
Module * pModule = pDebuggerModule->GetRuntimeModule();
bool fStatus = (pEvent->SetJMCFunctionStatus.dwStatus != 0);
mdMethodDef token = pEvent->SetJMCFunctionStatus.funcMetadataToken;
// Prepare reply
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pEvent, DB_IPCE_SET_METHOD_JMC_STATUS_RESULT, NULL, NULL);
pEvent->hr = S_OK;
if (pDebuggerModule->HasAnyOptimizedCode() && fStatus)
{
// If there's optimized code, then we can't be set JMC status to true.
// That's because JMC probes are not injected in optimized code, and we
// need a JMC probe to have a JMC function.
pEvent->hr = CORDBG_E_CANT_SET_TO_JMC;
}
else
{
DebuggerDataLockHolder debuggerDataLockHolder(this);
// This may be called on an unjitted method, so we may
// have to create the MethodInfo.
DebuggerMethodInfo * pMethodInfo = GetOrCreateMethodInfo(pModule, token);
if (pMethodInfo == NULL)
{
pEvent->hr = E_OUTOFMEMORY;
}
else
{
// Update the storage on the LS
pMethodInfo->SetJMCStatus(fStatus);
}
}
// Send reply
m_pRCThread->SendIPCReply();
}
break;
// Get the JMC status on a given function
case DB_IPCE_GET_METHOD_JMC_STATUS:
{
// Get the method
DebuggerModule * pDebuggerModule = LookupOrCreateModule(pEvent->SetJMCFunctionStatus.vmDomainFile);
Module * pModule = pDebuggerModule->GetRuntimeModule();
mdMethodDef token = pEvent->SetJMCFunctionStatus.funcMetadataToken;
// Init reply
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pEvent, DB_IPCE_GET_METHOD_JMC_STATUS_RESULT, NULL, NULL);
//
// This may be called on an unjitted method, so we may
// have to create the MethodInfo.
//
DebuggerMethodInfo * pMethodInfo = GetOrCreateMethodInfo(pModule, token);
if (pMethodInfo == NULL)
{
pEvent->hr = E_OUTOFMEMORY;
}
else
{
bool fStatus = pMethodInfo->IsJMCFunction();
pEvent->SetJMCFunctionStatus.dwStatus = fStatus;
pEvent->hr = S_OK;
}
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_SET_MODULE_JMC_STATUS:
{
// Get data out of event
DebuggerModule * pDebuggerModule = LookupOrCreateModule(pEvent->SetJMCFunctionStatus.vmDomainFile);
bool fStatus = (pEvent->SetJMCFunctionStatus.dwStatus != 0);
// Prepare reply
pEvent = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(pEvent, DB_IPCE_SET_MODULE_JMC_STATUS_RESULT);
pEvent->hr = S_OK;
if (pDebuggerModule->HasAnyOptimizedCode() && fStatus)
{
// If there's optimized code, then we can't be set JMC status to true.
// That's because JMC probes are not injected in optimized code, and we
// need a JMC probe to have a JMC function.
pEvent->hr = CORDBG_E_CANT_SET_TO_JMC;
}
else
{
g_pDebugger->SetModuleDefaultJMCStatus(pDebuggerModule->GetRuntimeModule(), fStatus);
}
// Send reply
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_INTERCEPT_EXCEPTION:
GetAndSendInterceptCommand(pEvent);
break;
case DB_IPCE_RESOLVE_UPDATE_METADATA_1:
{
LOG((LF_CORDB, LL_INFO10000, "D::HIPCE Handling DB_IPCE_RESOLVE_UPDATE_METADATA_1\n"));
// This isn't ideal - Making SerializeModuleMetaData not call new is hard,
// but the odds of trying to load a module after a thread is stopped w/
// the heap lock should be pretty low.
// All of the metadata calls can violate this and call new.
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
Module * pModule = pEvent->MetadataUpdateRequest.vmModule.GetRawPtr();
LOG((LF_CORDB, LL_INFO100000, "D::HIPCE Got module 0x%x\n", pModule));
DWORD countBytes = 0;
// This will allocate memory. Debugger will then copy from here and send a
// DB_IPCE_RESOLVE_UPDATE_METADATA_2 to free this memory.
BYTE* pData = NULL;
EX_TRY
{
LOG((LF_CORDB, LL_INFO100000, "D::HIPCE Calling SerializeModuleMetaData\n"));
pData = SerializeModuleMetaData(pModule, &countBytes);
}
EX_CATCH_HRESULT(hr);
LOG((LF_CORDB, LL_INFO100000, "D::HIPCE hr is 0x%x\n", hr));
DebuggerIPCEvent * pResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pResult, DB_IPCE_RESOLVE_UPDATE_METADATA_1_RESULT, NULL, NULL);
pResult->MetadataUpdateRequest.pMetadataStart = pData;
pResult->MetadataUpdateRequest.nMetadataSize = countBytes;
pResult->hr = hr;
LOG((LF_CORDB, LL_INFO1000000, "D::HIPCE metadataStart=0x%x, nMetadataSize=0x%x\n", pData, countBytes));
m_pRCThread->SendIPCReply();
LOG((LF_CORDB, LL_INFO1000000, "D::HIPCE reply sent\n"));
}
break;
case DB_IPCE_RESOLVE_UPDATE_METADATA_2:
{
// Delete memory allocated with DB_IPCE_RESOLVE_UPDATE_METADATA_1.
BYTE * pData = (BYTE *) pEvent->MetadataUpdateRequest.pMetadataStart;
DeleteInteropSafe(pData);
DebuggerIPCEvent * pResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pResult, DB_IPCE_RESOLVE_UPDATE_METADATA_2_RESULT, NULL, NULL);
pResult->hr = S_OK;
m_pRCThread->SendIPCReply();
}
break;
#ifdef FEATURE_LEGACYNETCF_DBG_HOST_CONTROL
case DB_IPCE_NETCF_HOST_CONTROL_PAUSE:
{
LOG((LF_CORDB, LL_INFO10000, "D::HIPCE Handling DB_IPCE_NETCF_HOST_CONTROL_PAUSE\n"));
InvokeLegacyNetCFHostPauseCallback();
DebuggerIPCEvent * pResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pResult, DB_IPCE_NETCF_HOST_CONTROL_PAUSE_RESULT, NULL, NULL);
pResult->hr = S_OK;
m_pRCThread->SendIPCReply();
}
break;
case DB_IPCE_NETCF_HOST_CONTROL_RESUME:
{
LOG((LF_CORDB, LL_INFO10000, "D::HIPCE Handling DB_IPCE_NETCF_HOST_CONTROL_RESUME\n"));
InvokeLegacyNetCFHostResumeCallback();
DebuggerIPCEvent * pResult = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(pResult, DB_IPCE_NETCF_HOST_CONTROL_RESUME_RESULT, NULL, NULL);
pResult->hr = S_OK;
m_pRCThread->SendIPCReply();
}
break;
#endif // FEATURE_LEGACYNETCF_DBG_HOST_CONTROL
default:
// We should never get an event that we don't know about.
CONSISTENCY_CHECK_MSGF(false, ("Unknown Debug-Event on LS:id=0x%08x.", pEvent->type));
LOG((LF_CORDB, LL_INFO10000, "Unknown event type: 0x%08x\n",
pEvent->type));
}
STRESS_LOG0(LF_CORDB, LL_INFO10000, "D::HIPCE: finished handling event\n");
// dbgLockHolder goes out of scope - implicit Release
return fContinue;
}
#ifdef _PREFAST_
#pragma warning(pop)
#endif
/*
* GetAndSendInterceptCommand
*
* This function processes an INTERCEPT_EXCEPTION IPC event, sending the appropriate response.
*
* Parameters:
* event - the event to process.
*
* Returns:
* hr - HRESULT.
*
*/
HRESULT Debugger::GetAndSendInterceptCommand(DebuggerIPCEvent *event)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
}
CONTRACTL_END;
HRESULT hr = S_OK;
_ASSERTE((event->type & DB_IPCE_TYPE_MASK) == DB_IPCE_INTERCEPT_EXCEPTION);
//
// Simple state validation first.
//
Thread *pThread = event->InterceptException.vmThreadToken.GetRawPtr();
if ((pThread != NULL) &&
!m_forceNonInterceptable &&
IsInterceptableException(pThread))
{
ThreadExceptionState* pExState = pThread->GetExceptionState();
// We can only have one interception going on at any given time.
if (!pExState->GetFlags()->DebuggerInterceptInfo())
{
//
// Now start processing the parameters from the event.
//
FramePointer targetFramePointer = event->InterceptException.frameToken;
ControllerStackInfo csi;
// Safe because we're stopped.
StackTraceTicket ticket(pThread);
csi.GetStackInfo(ticket, pThread, targetFramePointer, NULL);
if (csi.m_targetFrameFound)
{
//
// If the target frame is below the point where the current exception was
// thrown from, then we should reject this interception command. This
// can happen in a func-eval during an exception callback, or during a
// breakpoint in a filter function. Or it can just be a user error.
//
CONTEXT* pContext = pExState->GetContextRecord();
// This is an approximation on IA64, where we should use the caller SP instead of
// the current SP. However, if the targetFramePointer is valid, the comparison should
// still work. targetFramePointer should be valid because it ultimately comes from a
// full stackwalk.
FramePointer excepFramePointer = FramePointer::MakeFramePointer(GetSP(pContext));
if (IsCloserToRoot(excepFramePointer, targetFramePointer))
{
hr = CORDBG_E_CURRENT_EXCEPTION_IS_OUTSIDE_CURRENT_EXECUTION_SCOPE;
goto LSendResponse;
}
//
// If the instruction that faulted is not in this managed code, at the leaf
// frame, then the IP is actually the return address from the managed or
// unmanaged function that really did fault. Thus, we actually want the
// IP of the call instruction. I fake this by simply subtracting 1 from
// the IP, which is close enough approximation for the search below.
//
if (pExState->GetContextRecord() != NULL)
{
// If the faulting instruction is not in managed code, then the interception frame
// must be non-leaf.
if (!g_pEEInterface->IsManagedNativeCode((BYTE *)(GetIP(pExState->GetContextRecord()))))
{
csi.m_activeFrame.relOffset--;
}
else
{
MethodDesc *pMethodDesc = g_pEEInterface->GetNativeCodeMethodDesc(dac_cast<PCODE>(GetIP(pExState->GetContextRecord())));
// check if the interception frame is the leaf frame
if ((pMethodDesc == NULL) ||
(pMethodDesc != csi.m_activeFrame.md) ||
(GetSP(pExState->GetContextRecord()) != GetRegdisplaySP(&(csi.m_activeFrame.registers))))
{
csi.m_activeFrame.relOffset--;
}
}
}
//
// Now adjust the IP to be the previous zero-stack depth sequence point.
//
SIZE_T foundOffset = 0;
DebuggerJitInfo *pJitInfo = csi.m_activeFrame.GetJitInfoFromFrame();
if (pJitInfo != NULL)
{
ICorDebugInfo::SourceTypes src;
ULONG relOffset = csi.m_activeFrame.relOffset;
#if defined(WIN64EXCEPTIONS)
int funcletIndex = PARENT_METHOD_INDEX;
// For funclets, we need to make sure that the stack empty sequence point we use is
// in the same funclet as the current offset.
if (csi.m_activeFrame.IsFuncletFrame())
{
funcletIndex = pJitInfo->GetFuncletIndex(relOffset, DebuggerJitInfo::GFIM_BYOFFSET);
}
// Refer to the loop using pMap below.
DebuggerILToNativeMap* pMap = NULL;
#endif // WIN64EXCEPTIONS
for (unsigned int i = 0; i < pJitInfo->GetSequenceMapCount(); i++)
{
SIZE_T startOffset = pJitInfo->GetSequenceMap()[i].nativeStartOffset;
if (DbgIsSpecialILOffset(pJitInfo->GetSequenceMap()[i].ilOffset))
{
LOG((LF_CORDB, LL_INFO10000,
"D::HIPCE: not placing breakpoint at special offset 0x%x\n", startOffset));
continue;
}
if ((i >= 1) && (startOffset == pJitInfo->GetSequenceMap()[i-1].nativeStartOffset))
{
LOG((LF_CORDB, LL_INFO10000,
"D::HIPCE: not placing redundant breakpoint at duplicate offset 0x%x\n", startOffset));
continue;
}
if (startOffset > relOffset)
{
LOG((LF_CORDB, LL_INFO10000,
"D::HIPCE: Stopping scan for breakpoint at offset 0x%x\n", startOffset));
continue;
}
src = pJitInfo->GetSequenceMap()[i].source;
if (!(src & ICorDebugInfo::STACK_EMPTY))
{
LOG((LF_CORDB, LL_INFO10000, "D::HIPCE: not placing E&C breakpoint at offset "
"0x%x b/c not STACK_EMPTY:it's 0x%x\n", startOffset, src));
continue;
}
if ((foundOffset < startOffset) && (startOffset <= relOffset)
#if defined(WIN64EXCEPTIONS)
// Check if we are still in the same funclet.
&& (funcletIndex == pJitInfo->GetFuncletIndex(startOffset, DebuggerJitInfo::GFIM_BYOFFSET))
#endif // WIN64EXCEPTIONS
)
{
LOG((LF_CORDB, LL_INFO10000, "D::HIPCE: updating breakpoint at native offset 0x%x\n",
startOffset));
foundOffset = startOffset;
#if defined(WIN64EXCEPTIONS)
// Save the map entry for modification later.
pMap = &(pJitInfo->GetSequenceMap()[i]);
#endif // WIN64EXCEPTIONS
}
}
#if defined(WIN64EXCEPTIONS)
// This is nasty. Starting recently we could have multiple sequence points with the same IL offset
// in the SAME funclet/parent method (previously different sequence points with the same IL offset
// imply that they are in different funclet/parent method). Fortunately, we only run into this
// if we have a loop which throws a range check failed exception. The code for throwing the
// exception executes out of line (this is JIT-specific, of course). The following loop makes sure
// that when we interecept the exception, we intercept it at the smallest native offset instead
// of intercepting it right before we throw the exception.
for (/* no initialization */; pMap > pJitInfo->GetSequenceMap() ; pMap--)
{
if (pMap->ilOffset == (pMap-1)->ilOffset)
{
foundOffset = (pMap-1)->nativeStartOffset;
}
else
{
break;
}
}
_ASSERTE(foundOffset < relOffset);
#endif // WIN64EXCEPTIONS
//
// Set up a breakpoint on the intercept IP
//
DebuggerContinuableExceptionBreakpoint *pBreakpoint;
pBreakpoint = new (interopsafe, nothrow) DebuggerContinuableExceptionBreakpoint(pThread,
foundOffset,
pJitInfo,
csi.m_activeFrame.currentAppDomain
);
if (pBreakpoint != NULL)
{
//
// Set up the VM side of intercepting.
//
if (pExState->GetDebuggerState()->SetDebuggerInterceptInfo(csi.m_activeFrame.pIJM,
pThread,
csi.m_activeFrame.MethodToken,
csi.m_activeFrame.md,
foundOffset,
#ifdef _TARGET_ARM_
// ARM requires the caller stack pointer, not the current stack pointer
CallerStackFrame::FromRegDisplay(&(csi.m_activeFrame.registers)),
#else
StackFrame::FromRegDisplay(&(csi.m_activeFrame.registers)),
#endif
pExState->GetFlags()
))
{
//
// Make sure no more exception callbacks come thru.
//
pExState->GetFlags()->SetSentDebugFirstChance();
pExState->GetFlags()->SetSentDebugUserFirstChance();
pExState->GetFlags()->SetSentDebugUnwindBegin();
//
// Save off this breakpoint, so that if the exception gets unwound before we hit
// the breakpoint - the exeception info can call back to remove it.
//
pExState->GetDebuggerState()->SetDebuggerInterceptContext((void *)pBreakpoint);
hr = S_OK;
}
else // VM could not set up for intercept
{
DeleteInteropSafe(pBreakpoint);
hr = E_INVALIDARG;
}
}
else // could not allocate for breakpoint
{
hr = E_OUTOFMEMORY;
}
}
else // could not get JitInfo
{
hr = E_FAIL;
}
}
else // target frame not found.
{
hr = E_INVALIDARG;
}
}
else // already set up for an intercept.
{
hr = CORDBG_E_INTERCEPT_FRAME_ALREADY_SET;
}
}
else if (pThread == NULL)
{
hr = E_INVALIDARG; // pThread is NULL.
}
else
{
hr = CORDBG_E_NONINTERCEPTABLE_EXCEPTION;
}
LSendResponse:
//
// Prepare reply
//
event = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCReply(event, DB_IPCE_INTERCEPT_EXCEPTION_RESULT);
event->hr = hr;
//
// Send reply
//
m_pRCThread->SendIPCReply();
return hr;
}
// Poll & wait for the real helper thread to come up.
// It's possible that the helper thread is blocked by DllMain, and so we can't
// Wait infinite. If this poll does timeout, then it just means we're likely
// go do helper duty instead of have the real helper do it.
void Debugger::PollWaitingForHelper()
{
LOG((LF_CORDB, LL_INFO10000, "PollWaitingForHelper() start\n"));
DebuggerIPCControlBlock * pDCB = g_pRCThread->GetDCB();
PREFIX_ASSUME(pDCB != NULL);
int nTotalMSToWait = 8 * 1000;
// Spin waiting for either the real helper thread or a temp. to be ready.
// This should never timeout unless the helper is blocked on the loader lock.
while (!pDCB->m_helperThreadId && !pDCB->m_temporaryHelperThreadId)
{
STRESS_LOG1(LF_CORDB,LL_INFO1000, "PollWaitForHelper. %d\n", nTotalMSToWait);
// If we hold the lock, we'll block the helper thread and this poll is not useful
_ASSERTE(!ThreadHoldsLock());
const DWORD dwTime = 50;
ClrSleepEx(dwTime, FALSE);
nTotalMSToWait -= dwTime;
if (nTotalMSToWait <= 0)
{
LOG((LF_CORDB, LL_INFO10000, "PollWaitingForHelper() timeout\n"));
return;
}
}
LOG((LF_CORDB, LL_INFO10000, "PollWaitingForHelper() succeed\n"));
return;
}
void Debugger::TypeHandleToBasicTypeInfo(AppDomain *pAppDomain, TypeHandle th, DebuggerIPCE_BasicTypeData *res)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::THTBTI: converting left-side type handle to basic right-side type info, ELEMENT_TYPE: %d.\n", th.GetSignatureCorElementType()));
// GetSignatureCorElementType returns E_T_CLASS for E_T_STRING... :-(
if (th.IsNull())
{
res->elementType = ELEMENT_TYPE_VOID;
}
else if (th.GetMethodTable() == g_pObjectClass)
{
res->elementType = ELEMENT_TYPE_OBJECT;
}
else if (th.GetMethodTable() == g_pStringClass)
{
res->elementType = ELEMENT_TYPE_STRING;
}
else
{
res->elementType = th.GetSignatureCorElementType();
}
switch (res->elementType)
{
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_FNPTR:
case ELEMENT_TYPE_BYREF:
res->vmTypeHandle = WrapTypeHandle(th);
res->metadataToken = mdTokenNil;
res->vmDomainFile.SetRawPtr(NULL);
break;
case ELEMENT_TYPE_CLASS:
case ELEMENT_TYPE_VALUETYPE:
{
res->vmTypeHandle = th.HasInstantiation() ? WrapTypeHandle(th) : VMPTR_TypeHandle::NullPtr();
// only set if instantiated
res->metadataToken = th.GetCl();
DebuggerModule * pDModule = LookupOrCreateModule(th.GetModule(), pAppDomain);
res->vmDomainFile.SetRawPtr((pDModule ? pDModule->GetDomainFile() : NULL));
break;
}
default:
res->vmTypeHandle = VMPTR_TypeHandle::NullPtr();
res->metadataToken = mdTokenNil;
res->vmDomainFile.SetRawPtr(NULL);
break;
}
return;
}
void Debugger::TypeHandleToExpandedTypeInfo(AreValueTypesBoxed boxed,
AppDomain *pAppDomain,
TypeHandle th,
DebuggerIPCE_ExpandedTypeData *res)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (th.IsNull())
{
res->elementType = ELEMENT_TYPE_VOID;
}
else if (th.GetMethodTable() == g_pObjectClass)
{
res->elementType = ELEMENT_TYPE_OBJECT;
}
else if (th.GetMethodTable() == g_pStringClass)
{
res->elementType = ELEMENT_TYPE_STRING;
}
else
{
LOG((LF_CORDB, LL_INFO10000, "D::THTETI: converting left-side type handle to expanded right-side type info, ELEMENT_TYPE: %d.\n", th.GetSignatureCorElementType()));
// GetSignatureCorElementType returns E_T_CLASS for E_T_STRING... :-(
res->elementType = th.GetSignatureCorElementType();
}
switch (res->elementType)
{
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
_ASSERTE(th.IsArray());
res->ArrayTypeData.arrayRank = th.AsArray()->GetRank();
TypeHandleToBasicTypeInfo(pAppDomain,
th.AsArray()->GetArrayElementTypeHandle(),
&(res->ArrayTypeData.arrayTypeArg));
break;
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
if (boxed == AllBoxed)
{
res->elementType = ELEMENT_TYPE_CLASS;
goto treatAllValuesAsBoxed;
}
_ASSERTE(th.IsTypeDesc());
TypeHandleToBasicTypeInfo(pAppDomain,
th.AsTypeDesc()->GetTypeParam(),
&(res->UnaryTypeData.unaryTypeArg));
break;
case ELEMENT_TYPE_VALUETYPE:
if (boxed == OnlyPrimitivesUnboxed || boxed == AllBoxed)
res->elementType = ELEMENT_TYPE_CLASS;
// drop through
case ELEMENT_TYPE_CLASS:
{
treatAllValuesAsBoxed:
res->ClassTypeData.typeHandle = th.HasInstantiation() ? WrapTypeHandle(th) : VMPTR_TypeHandle::NullPtr(); // only set if instantiated
res->ClassTypeData.metadataToken = th.GetCl();
DebuggerModule * pModule = LookupOrCreateModule(th.GetModule(), pAppDomain);
res->ClassTypeData.vmDomainFile.SetRawPtr((pModule ? pModule->GetDomainFile() : NULL));
_ASSERTE(!res->ClassTypeData.vmDomainFile.IsNull());
break;
}
case ELEMENT_TYPE_FNPTR:
{
if (boxed == AllBoxed)
{
res->elementType = ELEMENT_TYPE_CLASS;
goto treatAllValuesAsBoxed;
}
res->NaryTypeData.typeHandle = WrapTypeHandle(th);
break;
}
default:
// The element type is sufficient, unless the type is effectively a "boxed"
// primitive value type...
if (boxed == AllBoxed)
{
res->elementType = ELEMENT_TYPE_CLASS;
goto treatAllValuesAsBoxed;
}
break;
}
LOG((LF_CORDB, LL_INFO10000, "D::THTETI: converted left-side type handle to expanded right-side type info, res->ClassTypeData.typeHandle = 0x%08x.\n", res->ClassTypeData.typeHandle.GetRawPtr()));
return;
}
HRESULT Debugger::BasicTypeInfoToTypeHandle(DebuggerIPCE_BasicTypeData *data, TypeHandle *pRes)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::BTITTH: expanding basic right-side type to left-side type, ELEMENT_TYPE: %d.\n", data->elementType));
*pRes = TypeHandle();
TypeHandle th;
switch (data->elementType)
{
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
_ASSERTE(!data->vmTypeHandle.IsNull());
th = GetTypeHandle(data->vmTypeHandle);
break;
case ELEMENT_TYPE_CLASS:
case ELEMENT_TYPE_VALUETYPE:
{
if (!data->vmTypeHandle.IsNull())
{
th = GetTypeHandle(data->vmTypeHandle);
}
else
{
DebuggerModule *pDebuggerModule = g_pDebugger->LookupOrCreateModule(data->vmDomainFile);
th = g_pEEInterface->FindLoadedClass(pDebuggerModule->GetRuntimeModule(), data->metadataToken);
if (th.IsNull())
{
LOG((LF_CORDB, LL_INFO10000, "D::ETITTH: class isn't loaded.\n"));
return CORDBG_E_CLASS_NOT_LOADED;
}
_ASSERTE(th.GetNumGenericArgs() == 0);
}
break;
}
case ELEMENT_TYPE_FNPTR:
{
_ASSERTE(!data->vmTypeHandle.IsNull());
th = GetTypeHandle(data->vmTypeHandle);
break;
}
default:
th = g_pEEInterface->FindLoadedElementType(data->elementType);
break;
}
if (th.IsNull())
return CORDBG_E_CLASS_NOT_LOADED;
*pRes = th;
return S_OK;
}
// Iterate through the type argument data, creating type handles as we go.
void Debugger::TypeDataWalk::ReadTypeHandles(unsigned int nTypeArgs, TypeHandle *ppResults)
{
WRAPPER_NO_CONTRACT;
for (unsigned int i = 0; i < nTypeArgs; i++)
ppResults[i] = ReadTypeHandle();
}
TypeHandle Debugger::TypeDataWalk::ReadInstantiation(Module *pModule, mdTypeDef tok, unsigned int nTypeArgs)
{
WRAPPER_NO_CONTRACT;
DWORD dwAllocSize;
if (!ClrSafeInt<DWORD>::multiply(nTypeArgs, sizeof(TypeHandle), dwAllocSize))
{
ThrowHR(COR_E_OVERFLOW);
}
TypeHandle * inst = (TypeHandle *) _alloca(dwAllocSize);
ReadTypeHandles(nTypeArgs, inst) ;
TypeHandle th = g_pEEInterface->LoadInstantiation(pModule, tok, nTypeArgs, inst);
if (th.IsNull())
COMPlusThrow(kArgumentException, W("Argument_InvalidGenericArg"));
return th;
}
TypeHandle Debugger::TypeDataWalk::ReadTypeHandle()
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
}
CONTRACTL_END;
DebuggerIPCE_TypeArgData * data = ReadOne();
if (!data)
COMPlusThrow(kArgumentException, W("Argument_InvalidGenericArg"));
LOG((LF_CORDB, LL_INFO10000, "D::ETITTH: expanding right-side type to left-side type, ELEMENT_TYPE: %d.\n", data->data.elementType));
TypeHandle th;
CorElementType et = data->data.elementType;
switch (et)
{
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
if(data->numTypeArgs == 1)
{
TypeHandle typar = ReadTypeHandle();
switch (et)
{
case ELEMENT_TYPE_ARRAY:
case ELEMENT_TYPE_SZARRAY:
th = g_pEEInterface->LoadArrayType(data->data.elementType, typar, data->data.ArrayTypeData.arrayRank);
break;
case ELEMENT_TYPE_PTR:
case ELEMENT_TYPE_BYREF:
th = g_pEEInterface->LoadPointerOrByrefType(data->data.elementType, typar);
break;
default:
_ASSERTE(0);
}
}
break;
case ELEMENT_TYPE_CLASS:
case ELEMENT_TYPE_VALUETYPE:
{
DebuggerModule *pDebuggerModule = g_pDebugger->LookupOrCreateModule(data->data.ClassTypeData.vmDomainFile);
th = ReadInstantiation(pDebuggerModule->GetRuntimeModule(), data->data.ClassTypeData.metadataToken, data->numTypeArgs);
break;
}
case ELEMENT_TYPE_FNPTR:
{
SIZE_T cbAllocSize;
if ((!ClrSafeInt<SIZE_T>::multiply(data->numTypeArgs, sizeof(TypeHandle), cbAllocSize)) ||
(cbAllocSize != (size_t)(cbAllocSize)))
{
_ASSERTE(COR_E_OVERFLOW);
cbAllocSize = UINT_MAX;
}
TypeHandle * inst = (TypeHandle *) _alloca(cbAllocSize);
ReadTypeHandles(data->numTypeArgs, inst) ;
th = g_pEEInterface->LoadFnptrType(inst, data->numTypeArgs);
break;
}
default:
th = g_pEEInterface->LoadElementType(data->data.elementType);
break;
}
if (th.IsNull())
COMPlusThrow(kArgumentNullException, W("ArgumentNull_Type"));
return th;
}
//
// GetAndSendTransitionStubInfo figures out if an address is a stub
// address and sends the result back to the right side.
//
void Debugger::GetAndSendTransitionStubInfo(CORDB_ADDRESS_TYPE *stubAddress)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::GASTSI: IsTransitionStub. Addr=0x%08x\n", stubAddress));
bool result = false;
result = g_pEEInterface->IsStub((const BYTE *)stubAddress);
// If its not a stub, then maybe its an address in mscoree?
if (result == false)
{
result = (IsIPInModule(g_pMSCorEE, (PCODE)stubAddress) == TRUE);
}
// This is a synchronous event (reply required)
DebuggerIPCEvent *event = m_pRCThread->GetIPCEventReceiveBuffer();
InitIPCEvent(event, DB_IPCE_IS_TRANSITION_STUB_RESULT, NULL, NULL);
event->IsTransitionStubResult.isStub = result;
// Send the result
m_pRCThread->SendIPCReply();
}
/*
* A generic request for a buffer in the left-side for use by the right-side
*
* This is a synchronous event (reply required).
*/
HRESULT Debugger::GetAndSendBuffer(DebuggerRCThread* rcThread, ULONG bufSize)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// This is a synchronous event (reply required)
DebuggerIPCEvent* event = rcThread->GetIPCEventReceiveBuffer();
PREFIX_ASSUME(event != NULL);
InitIPCEvent(event, DB_IPCE_GET_BUFFER_RESULT, NULL, NULL);
// Allocate the buffer
event->GetBufferResult.hr = AllocateRemoteBuffer( bufSize, &event->GetBufferResult.pBuffer );
// Send the result
return rcThread->SendIPCReply();
}
/*
* Allocate a buffer in the left-side for use by the right-side
*/
HRESULT Debugger::AllocateRemoteBuffer( ULONG bufSize, void **ppBuffer )
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// The call to Append below will call CUnorderedArray, which will call unsafe New.
HRESULT hr;
CHECK_IF_CAN_TAKE_HELPER_LOCKS_IN_THIS_SCOPE(&hr, GetCanary());
if( FAILED(hr) )
{
return hr;
}
// Actually allocate the buffer
BYTE* pBuffer = new (interopsafe, nothrow) BYTE[bufSize];
LOG((LF_CORDB, LL_EVERYTHING, "D::ARB: new'd 0x%x\n", *ppBuffer));
// Check for out of memory error
if (pBuffer == NULL)
{
return E_OUTOFMEMORY;
}
// Track the allocation so we can free it later
void **ppNextBlob = GetMemBlobs()->Append();
if( ppNextBlob == NULL )
{
DeleteInteropSafe( pBuffer );
return E_OUTOFMEMORY;
}
*ppNextBlob = pBuffer;
// Return the allocated memory
*ppBuffer = pBuffer;
return S_OK;
}
/*
* Used to release a previously-requested buffer
*
* This is a synchronous event (reply required).
*/
HRESULT Debugger::SendReleaseBuffer(DebuggerRCThread* rcThread, void *pBuffer)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO10000, "D::SRB for buffer 0x%x\n", pBuffer));
// This is a synchronous event (reply required)
DebuggerIPCEvent* event = rcThread->GetIPCEventReceiveBuffer();
PREFIX_ASSUME(event != NULL);
InitIPCEvent(event, DB_IPCE_RELEASE_BUFFER_RESULT, NULL, NULL);
_ASSERTE(pBuffer != NULL);
// Free the memory
ReleaseRemoteBuffer(pBuffer, true);
// Indicate success in reply
event->ReleaseBufferResult.hr = S_OK;
// Send the result
return rcThread->SendIPCReply();
}
//
// Used to delete the buffer previously-requested by the right side.
// We've factored the code since both the ~Debugger and SendReleaseBuffer
// methods do this.
//
HRESULT Debugger::ReleaseRemoteBuffer(void *pBuffer, bool removeFromBlobList)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_EVERYTHING, "D::RRB: Releasing RS-alloc'd buffer 0x%x\n", pBuffer));
// Remove the buffer from the blob list if necessary.
if (removeFromBlobList)
{
USHORT cBlobs = GetMemBlobs()->Count();
void **rgpBlobs = GetMemBlobs()->Table();
USHORT i;
for (i = 0; i < cBlobs; i++)
{
if (rgpBlobs[i] == pBuffer)
{
GetMemBlobs()->DeleteByIndex(i);
break;
}
}
// We should have found a match. All buffers passed to ReleaseRemoteBuffer
// should have been allocated with AllocateRemoteBuffer and not yet freed.
_ASSERTE( i < cBlobs );
}
// Delete the buffer. (Need cast for GCC template support)
DeleteInteropSafe( (BYTE*)pBuffer );
return S_OK;
}
//
// UnrecoverableError causes the Left Side to enter a state where no more
// debugging can occur and we leave around enough information for the
// Right Side to tell what happened.
//
void Debugger::UnrecoverableError(HRESULT errorHR,
unsigned int errorCode,
const char *errorFile,
unsigned int errorLine,
bool exitThread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10,
"Unrecoverable error: hr=0x%08x, code=%d, file=%s, line=%d\n",
errorHR, errorCode, errorFile, errorLine));
//
// Setting this will ensure that not much else happens...
//
m_unrecoverableError = TRUE;
//
// Fill out the control block with the error.
// in-proc will find out when the function fails
//
DebuggerIPCControlBlock *pDCB = m_pRCThread->GetDCB();
PREFIX_ASSUME(pDCB != NULL);
pDCB->m_errorHR = errorHR;
pDCB->m_errorCode = errorCode;
//
// If we're told to, exit the thread.
//
if (exitThread)
{
LOG((LF_CORDB, LL_INFO10,
"Thread exiting due to unrecoverable error.\n"));
ExitThread(errorHR);
}
}
//
// Callback for IsThreadAtSafePlace's stack walk.
//
StackWalkAction Debugger::AtSafePlaceStackWalkCallback(CrawlFrame *pCF,
VOID* data)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pCF));
PRECONDITION(CheckPointer(data));
}
CONTRACTL_END;
bool *atSafePlace = (bool*)data;
LOG((LF_CORDB, LL_INFO100000, "D:AtSafePlaceStackWalkCallback\n"));
if (pCF->IsFrameless() && pCF->IsActiveFunc())
{
LOG((LF_CORDB, LL_INFO1000000, "D:AtSafePlaceStackWalkCallback, IsFrameLess() and IsActiveFunc()\n"));
if (g_pEEInterface->CrawlFrameIsGcSafe(pCF))
{
LOG((LF_CORDB, LL_INFO1000000, "D:AtSafePlaceStackWalkCallback - TRUE: CrawlFrameIsGcSafe()\n"));
*atSafePlace = true;
}
}
return SWA_ABORT;
}
//
// Determine, via a quick one frame stack walk, if a given thread is
// in a gc safe place.
//
bool Debugger::IsThreadAtSafePlaceWorker(Thread *thread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(thread));
}
CONTRACTL_END;
bool atSafePlace = false;
// Setup our register display.
REGDISPLAY rd;
CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);
_ASSERTE(!(g_pEEInterface->GetThreadFilterContext(thread) && ISREDIRECTEDTHREAD(thread)));
if (context != NULL)
{
g_pEEInterface->InitRegDisplay(thread, &rd, context, TRUE);
}
else
{
CONTEXT ctx;
ZeroMemory(&rd, sizeof(rd));
ZeroMemory(&ctx, sizeof(ctx));
#if defined(_TARGET_X86_)
rd.ControlPC = ctx.Eip;
rd.PCTAddr = (TADDR)&(ctx.Eip);
#else
FillRegDisplay(&rd, &ctx);
#endif
if (ISREDIRECTEDTHREAD(thread))
{
thread->GetFrame()->UpdateRegDisplay(&rd);
}
}
// Do the walk. If it fails, we don't care, because we default
// atSafePlace to false.
g_pEEInterface->StackWalkFramesEx(
thread,
&rd,
Debugger::AtSafePlaceStackWalkCallback,
(VOID*)(&atSafePlace),
QUICKUNWIND | HANDLESKIPPEDFRAMES |
DISABLE_MISSING_FRAME_DETECTION);
#ifdef LOGGING
if (!atSafePlace)
LOG((LF_CORDB | LF_GC, LL_INFO1000,
"Thread 0x%x is not at a safe place.\n",
GetThreadIdHelper(thread)));
#endif
return atSafePlace;
}
bool Debugger::IsThreadAtSafePlace(Thread *thread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(thread));
}
CONTRACTL_END;
if (m_fShutdownMode)
{
return true;
}
// <TODO>
//
// Make sure this fix is evaluated when doing real work for debugging SO handling.
//
// On the Stack Overflow code path calling IsThreadAtSafePlaceWorker as it is
// currently implemented is way too stack intensive. For now we cheat and just
// say that if a thread is in the middle of handling a SO it is NOT at a safe
// place. This is a reasonably safe assumption to make and hopefully shouldn't
// result in deadlocking the debugger.
if ( (thread->IsExceptionInProgress()) &&
(g_pEEInterface->GetThreadException(thread) == CLRException::GetPreallocatedStackOverflowExceptionHandle()) )
{
return false;
}
// </TODO>
else
{
return IsThreadAtSafePlaceWorker(thread);
}
}
//-----------------------------------------------------------------------------
// Get the complete user state flags.
// This will collect flags both from the EE and from the LS.
// This is the real implementation of the RS's ICorDebugThread::GetUserState().
//
// Parameters:
// pThread - non-null thread to get state for.
//
// Returns: a CorDebugUserState flags enum describing state.
//-----------------------------------------------------------------------------
CorDebugUserState Debugger::GetFullUserState(Thread *pThread)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
PRECONDITION(CheckPointer(pThread));
}
CONTRACTL_END;
CorDebugUserState state = g_pEEInterface->GetPartialUserState(pThread);
bool fSafe = IsThreadAtSafePlace(pThread);
if (!fSafe)
{
state = (CorDebugUserState) (state | USER_UNSAFE_POINT);
}
return state;
}
/******************************************************************************
*
* Helper for debugger to get an unique thread id
* If we are not in Fiber mode, we can safely use OSThreadId
* Otherwise, we will use our own unique ID.
*
* We will return our unique ID when our host is hosting Thread.
*
*
******************************************************************************/
DWORD Debugger::GetThreadIdHelper(Thread *pThread)
{
WRAPPER_NO_CONTRACT;
if (!CLRTaskHosted())
{
// use the plain old OS Thread ID
return pThread->GetOSThreadId();
}
else
{
// use our unique thread ID
return pThread->GetThreadId();
}
}
//-----------------------------------------------------------------------------
// Called by EnC during remapping to get information about the local vars.
// EnC will then use this to set values in the new version to their corresponding
// values from the old version.
//
// Returns a pointer to the debugger's copies of the maps. Caller
// does not own the memory provided via vars outparameter.
//-----------------------------------------------------------------------------
void Debugger::GetVarInfo(MethodDesc * fd, // [IN] method of interest
void *DebuggerVersionToken, // [IN] which edit version
SIZE_T * cVars, // [OUT] size of 'vars'
const ICorDebugInfo::NativeVarInfo **vars // [OUT] map telling where local vars are stored
)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
}
CONTRACTL_END;
DebuggerJitInfo * ji = (DebuggerJitInfo *)DebuggerVersionToken;
// If we didn't supply a DJI, then we're asking for the most recent version.
if (ji == NULL)
{
ji = GetLatestJitInfoFromMethodDesc(fd);
}
_ASSERTE(fd == ji->m_fd);
PREFIX_ASSUME(ji != NULL);
*vars = ji->GetVarNativeInfo();
*cVars = ji->GetVarNativeInfoCount();
}
#include "openum.h"
#ifdef EnC_SUPPORTED
//---------------------------------------------------------------------------------------
//
// Apply an EnC edit to the CLR datastructures and send the result event to the
// debugger right-side.
//
// Arguments:
// pDebuggerModule - the module in which the edit should occur
// cbMetadata - the number of bytes in pMetadata
// pMetadata - pointer to the delta metadata
// cbIL - the number of bytes in pIL
// pIL - pointer to the delta IL
//
// Return Value:
//
// Assumptions:
//
// Notes:
//
// This is just the first half of processing an EnC request (hot swapping). This updates
// the metadata and other CLR data structures to reflect the edit, but does not directly
// affect code which is currently running. In order to achieve on-stack replacement
// (remap of running code), we mine all old methods with "EnC remap breakpoints"
// (instances of DebuggerEnCBreakpoint) at many sequence points. When one of those
// breakpoints is hit, we give the debugger a RemapOpportunity event and give it a
// chance to remap the execution to the new version of the method.
//
HRESULT Debugger::ApplyChangesAndSendResult(DebuggerModule * pDebuggerModule,
DWORD cbMetadata,
BYTE *pMetadata,
DWORD cbIL,
BYTE *pIL)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
// @todo - if EnC never works w/ interop, caller New on the helper thread may be ok.
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
HRESULT hr = S_OK;
LOG((LF_ENC, LL_INFO100, "Debugger::ApplyChangesAndSendResult\n"));
Module *pModule = pDebuggerModule->GetRuntimeModule();
if (! pModule->IsEditAndContinueEnabled())
{
hr = CORDBG_E_ENC_MODULE_NOT_ENC_ENABLED;
}
else
{
// Violation with the following call stack:
// CONTRACT in MethodTableBuilder::InitMethodDesc
// CONTRACT in EEClass::AddMethod
// CONTRACT in EditAndContinueModule::AddMethod
// CONTRACT in EditAndContinueModule::ApplyEditAndContinue
// CONTRACT in EEDbgInterfaceImpl::EnCApplyChanges
// VIOLATED--> CONTRACT in Debugger::ApplyChangesAndSendResult
CONTRACT_VIOLATION(GCViolation);
// Tell the VM to apply the edit
hr = g_pEEInterface->EnCApplyChanges(
(EditAndContinueModule*)pModule, cbMetadata, pMetadata, cbIL, pIL);
}
LOG((LF_ENC, LL_INFO100, "Debugger::ApplyChangesAndSendResult 2\n"));
DebuggerIPCEvent* event = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(event,
DB_IPCE_APPLY_CHANGES_RESULT,
NULL,
NULL);
event->ApplyChangesResult.hr = hr;
// Send the result
return m_pRCThread->SendIPCEvent();
}
//
// This structure is used to hold a list of the sequence points in a function and
// determine which should have remap breakpoints applied to them for EnC
//
class EnCSequencePointHelper
{
public:
// Calculates remap info given the supplied JitInfo
EnCSequencePointHelper(DebuggerJitInfo *pJitInfo);
~EnCSequencePointHelper();
// Returns true if the specified sequence point (given by it's index in the
// sequence point table in the JitInfo) should get an EnC remap breakpoint.
BOOL ShouldSetRemapBreakpoint(unsigned int offsetIndex);
private:
DebuggerJitInfo *m_pJitInfo;
DebugOffsetToHandlerInfo *m_pOffsetToHandlerInfo;
};
//
// Goes through the list of sequence points for a function and determines whether or not each
// is a valid Remap Breakpoint location (not in a special offset, must be empty stack, and not in a handler.
//
EnCSequencePointHelper::EnCSequencePointHelper(DebuggerJitInfo *pJitInfo)
: m_pOffsetToHandlerInfo(NULL),
m_pJitInfo(pJitInfo)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (m_pJitInfo->GetSequenceMapCount() == 0)
{
return;
}
// Construct a list of native offsets we may want to place EnC breakpoints at
m_pOffsetToHandlerInfo = new DebugOffsetToHandlerInfo[m_pJitInfo->GetSequenceMapCount()];
for (unsigned int i = 0; i < m_pJitInfo->GetSequenceMapCount(); i++)
{
// By default this slot is unused. We want the indexes in m_pOffsetToHandlerInfo
// to correspond to the indexes of m_pJitInfo->GetSequenceMapCount, so we rely
// on a -1 offset to indicate that a DebuggerOffsetToHandlerInfo is unused.
// However, it would be cleaner and permit a simpler API to the EE if we just
// had an array mapping the offsets instead.
m_pOffsetToHandlerInfo[i].offset = (SIZE_T) -1;
m_pOffsetToHandlerInfo[i].isInFilterOrHandler = FALSE;
SIZE_T offset = m_pJitInfo->GetSequenceMap()[i].nativeStartOffset;
// Check if this is a "special" IL offset, such as representing the prolog or eppilog,
// or other region not directly mapped to native code.
if (DbgIsSpecialILOffset(pJitInfo->GetSequenceMap()[i].ilOffset))
{
LOG((LF_ENC, LL_INFO10000,
"D::UF: not placing E&C breakpoint at special offset 0x%x (IL: 0x%x)\n",
offset, m_pJitInfo->GetSequenceMap()[i].ilOffset));
continue;
}
// Skip duplicate sequence points
if (i >=1 && offset == pJitInfo->GetSequenceMap()[i-1].nativeStartOffset)
{
LOG((LF_ENC, LL_INFO10000,
"D::UF: not placing redundant E&C "
"breakpoint at duplicate offset 0x%x (IL: 0x%x)\n",
offset, m_pJitInfo->GetSequenceMap()[i].ilOffset));
continue;
}
// Skip sequence points that aren't due to the evaluation stack being empty
// We can only remap at stack-empty points (since we don't have a mapping for
// contents of the evaluation stack).
if (!(pJitInfo->GetSequenceMap()[i].source & ICorDebugInfo::STACK_EMPTY))
{
LOG((LF_ENC, LL_INFO10000,
"D::UF: not placing E&C breakpoint at offset "
"0x%x (IL: 0x%x) b/c not STACK_EMPTY:it's 0x%x\n", offset,
m_pJitInfo->GetSequenceMap()[i].ilOffset, pJitInfo->GetSequenceMap()[i].source));
continue;
}
// So far this sequence point looks good, so store it's native offset so we can get
// EH information about it from the EE.
LOG((LF_ENC, LL_INFO10000,
"D::UF: possibly placing E&C breakpoint at offset "
"0x%x (IL: 0x%x)\n", offset, m_pJitInfo->GetSequenceMap()[i].ilOffset));
m_pOffsetToHandlerInfo[i].offset = m_pJitInfo->GetSequenceMap()[i].nativeStartOffset;
}
// Ask the EE to fill in the isInFilterOrHandler bit for the native offsets we're interested in
g_pEEInterface->DetermineIfOffsetsInFilterOrHandler(
(BYTE *)pJitInfo->m_addrOfCode, m_pOffsetToHandlerInfo, m_pJitInfo->GetSequenceMapCount());
}
EnCSequencePointHelper::~EnCSequencePointHelper()
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (m_pOffsetToHandlerInfo)
{
delete m_pOffsetToHandlerInfo;
}
}
//
// Returns if we should set a remap breakpoint at a given offset. We only set them at 0-depth stack
// and not when inside a handler, either finally, filter, or catch
//
BOOL EnCSequencePointHelper::ShouldSetRemapBreakpoint(unsigned int offsetIndex)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
MODE_ANY;
CANNOT_TAKE_LOCK;
}
CONTRACTL_END;
{
// GetSequenceMapCount calls LazyInitBounds() which can eventually
// call ExecutionManager::IncrementReader
CONTRACT_VIOLATION(TakesLockViolation);
_ASSERTE(offsetIndex <= m_pJitInfo->GetSequenceMapCount());
}
// If this slot is unused (offset -1), we excluded it early
if (m_pOffsetToHandlerInfo[offsetIndex].offset == (SIZE_T) -1)
{
return FALSE;
}
// Otherwise, check the isInFilterOrHandler bit
if (m_pOffsetToHandlerInfo[offsetIndex].isInFilterOrHandler)
{
LOG((LF_ENC, LL_INFO10000,
"D::UF: not placing E&C breakpoint in filter/handler at offset 0x%x\n",
m_pOffsetToHandlerInfo[offsetIndex].offset));
return FALSE;
}
return TRUE;
}
//-----------------------------------------------------------------------------
// For each function that's EnC-ed, the EE will call either UpdateFunction
// (if the function already is loaded + jitted) or Addfunction
//
// This is called before the EE updates the MethodDesc, so pMD does not yet
// point to the version we'll be remapping to.
//-----------------------------------------------------------------------------
HRESULT Debugger::UpdateFunction(MethodDesc* pMD, SIZE_T encVersion)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
PRECONDITION(ThisIsHelperThread()); // guarantees we're serialized.
PRECONDITION(IsStopped());
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::UF: updating "
"%s::%s to version %d\n", pMD->m_pszDebugClassName, pMD->m_pszDebugMethodName, encVersion));
// tell the RS that this function has been updated so that it can create new CorDBFunction
Module *pModule = g_pEEInterface->MethodDescGetModule(pMD);
_ASSERTE(pModule != NULL);
mdToken methodDef = pMD->GetMemberDef();
SendEnCUpdateEvent(DB_IPCE_ENC_UPDATE_FUNCTION,
pModule,
methodDef,
pMD->GetMethodTable()->GetCl(),
encVersion);
DebuggerMethodInfo *dmi = GetOrCreateMethodInfo(pModule, methodDef);
if (dmi == NULL)
{
return E_OUTOFMEMORY;
}
// The DMI always holds the most current EnC version number. We always JIT the most
// current version of the function, so when we do see a JitBegin we will create a new
// dji for it and stash the current version there. We don't want to change the current
// jit info because it has to maintain the version for the code it corresponds to.
dmi->SetCurrentEnCVersion(encVersion);
// This is called before the MethodDesc is updated to point to the new function.
// So this call will get the most recent old function.
DebuggerJitInfo *pJitInfo = GetLatestJitInfoFromMethodDesc(pMD);
if (pJitInfo == NULL )
{
LOG((LF_CORDB,LL_INFO10000,"Unable to get DJI by recently "
"D::UF: JITted version number (it hasn't been jitted yet),"
"which is fine\n"));
return S_OK;
}
//
// Mine the old version of the method with patches so that we can provide
// remap opportunities whenever the old version of the method is executed.
//
if (pJitInfo->m_encBreakpointsApplied)
{
LOG((LF_CORDB,LL_INFO10000,"D::UF: Breakpoints already applied\n"));
return S_OK;
}
LOG((LF_CORDB,LL_INFO10000,"D::UF: Applying breakpoints\n"));
// We only place the patches if we have jit info for this
// function, i.e., its already been jitted. Otherwise, the EE will
// pickup the new method on the next JIT anyway.
ICorDebugInfo::SourceTypes src;
EnCSequencePointHelper sequencePointHelper(pJitInfo);
// For each offset in the IL->Native map, set a new EnC breakpoint on the
// ones that we know could be remap points.
for (unsigned int i = 0; i < pJitInfo->GetSequenceMapCount(); i++)
{
// Skip if this isn't a valid remap point (eg. is in an exception handler)
if (! sequencePointHelper.ShouldSetRemapBreakpoint(i))
{
continue;
}
SIZE_T offset = pJitInfo->GetSequenceMap()[i].nativeStartOffset;
LOG((LF_CORDB, LL_INFO10000,
"D::UF: placing E&C breakpoint at native offset 0x%x\n",
offset));
DebuggerEnCBreakpoint *bp;
// Create and activate a new EnC remap breakpoint here in the old version of the method
bp = new (interopsafe) DebuggerEnCBreakpoint( offset,
pJitInfo,
DebuggerEnCBreakpoint::REMAP_PENDING,
(AppDomain *)pModule->GetDomain());
_ASSERTE(bp != NULL);
}
pJitInfo->m_encBreakpointsApplied = true;
return S_OK;
}
// Called to update a function that hasn't yet been loaded (and so we don't have a MethodDesc).
// This may be updating an existing function on a type that hasn't been loaded
// or adding a new function to a type that hasn't been loaded.
// We need to notify the debugger so that it can properly track version info.
HRESULT Debugger::UpdateNotYetLoadedFunction(mdMethodDef token, Module * pModule, SIZE_T encVersion)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
PRECONDITION(ThisIsHelperThread());
PRECONDITION(ThreadHoldsLock()); // must have lock since we're on helper and stopped.
}
CONTRACTL_END;
DebuggerMethodInfo *dmi = GetOrCreateMethodInfo(pModule, token);
if (! dmi)
{
return E_OUTOFMEMORY;
}
dmi->SetCurrentEnCVersion(encVersion);
// Must tell the RS that this function has been added so that it can create new CorDBFunction.
mdTypeDef classToken = 0;
HRESULT hr = pModule->GetMDImport()->GetParentToken(token, &classToken);
if (FAILED(hr))
{
// We never expect this to actually fail, but just in case it does for some other crazy reason,
// we'll return before we AV.
CONSISTENCY_CHECK_MSGF(false, ("Class lookup failed:mdToken:0x%08x, pModule=%p. hr=0x%08x\n", token, pModule, hr));
return hr;
}
SendEnCUpdateEvent(DB_IPCE_ENC_ADD_FUNCTION, pModule, token, classToken, encVersion);
return S_OK;
}
// Called to add a new function when the type has been loaded already.
// This is effectively the same as above, except that we're given a
// MethodDesc instead of a module and token.
// This should probably be merged into a single method since the caller
// should always have a module and token available in both cases.
HRESULT Debugger::AddFunction(MethodDesc* pMD, SIZE_T encVersion)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
PRECONDITION(ThisIsHelperThread());
PRECONDITION(ThreadHoldsLock()); // must have lock since we're on helper and stopped.
}
CONTRACTL_END;
DebuggerDataLockHolder debuggerDataLockHolder(this);
LOG((LF_CORDB, LL_INFO10000, "D::AF: adding "
"%s::%s to version %d\n", pMD->m_pszDebugClassName, pMD->m_pszDebugMethodName, encVersion));
_ASSERTE(pMD != NULL);
Module *pModule = g_pEEInterface->MethodDescGetModule(pMD);
_ASSERTE(pModule != NULL);
mdToken methodDef = pMD->GetMemberDef();
// tell the RS that this function has been added so that it can create new CorDBFunction
SendEnCUpdateEvent( DB_IPCE_ENC_ADD_FUNCTION,
pModule,
methodDef,
pMD->GetMethodTable()->GetCl(),
encVersion);
DebuggerMethodInfo *dmi = CreateMethodInfo(pModule, methodDef);
if (! dmi)
{
return E_OUTOFMEMORY;
}
dmi->SetCurrentEnCVersion(encVersion);
return S_OK;
}
// Invoke when a field is added to a class using EnC
HRESULT Debugger::AddField(FieldDesc* pFD, SIZE_T encVersion)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::AFld: adding "
"%8.8d::%8.8d to version %d\n", pFD->GetApproxEnclosingMethodTable()->GetCl(), pFD->GetMemberDef(), encVersion));
// tell the RS that this field has been added so that it can update it's structures
SendEnCUpdateEvent( DB_IPCE_ENC_ADD_FIELD,
pFD->GetModule(),
pFD->GetMemberDef(),
pFD->GetApproxEnclosingMethodTable()->GetCl(),
encVersion);
return S_OK;
}
//
// RemapComplete is called when we are just about to resume into
// the function so that we can setup our breakpoint to trigger
// a call to the RemapComplete callback once the function is actually
// on the stack. We need to wait until the function is jitted before
// we can add the trigger, which doesn't happen until we call
// ResumeInUpdatedFunction in the VM
//
// addr is address within the given function, which we use to determine
// exact EnC version.
//
HRESULT Debugger::RemapComplete(MethodDesc* pMD, TADDR addr, SIZE_T nativeOffset)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
}
CONTRACTL_END;
_ASSERTE(pMD != NULL);
_ASSERTE(addr != NULL);
LOG((LF_CORDB, LL_INFO10000, "D::RC: installed remap complete patch for "
"%s::%s to version %d\n", pMD->m_pszDebugClassName, pMD->m_pszDebugMethodName));
DebuggerMethodInfo *dmi = GetOrCreateMethodInfo(pMD->GetModule(), pMD->GetMemberDef());
if (dmi == NULL)
{
return E_OUTOFMEMORY;
}
DebuggerJitInfo *pJitInfo = GetJitInfo(pMD, (const BYTE *) addr);
if (pJitInfo == NULL)
{
_ASSERTE(!"Debugger doesn't handle OOM");
return E_OUTOFMEMORY;
}
_ASSERTE(pJitInfo->m_addrOfCode + nativeOffset == addr);
DebuggerEnCBreakpoint *bp;
// Create and activate a new REMAP_COMPLETE EnC breakpoint to let us know when
// the EE has completed the remap process.
// This will be deleted when the patch is hit.
bp = new (interopsafe, nothrow) DebuggerEnCBreakpoint( nativeOffset,
pJitInfo,
DebuggerEnCBreakpoint::REMAP_COMPLETE,
(AppDomain *)pMD->GetModule()->GetDomain());
if (bp == NULL)
{
return E_OUTOFMEMORY;
}
return S_OK;
}
//-----------------------------------------------------------------------------
// Called by EnC stuff to map an IL offset to a native offset for the given
// method described by (pMD, nativeFnxStart).
//
// pMD - methoddesc for method being remapped
// ilOffset - incoming offset in old method to remap.
// nativeFnxStart - address of new function. This can be used to find the DJI
// for the new method.
// nativeOffset - outparameter for native linear offset relative to start address.
//-----------------------------------------------------------------------------
HRESULT Debugger::MapILInfoToCurrentNative(MethodDesc *pMD,
SIZE_T ilOffset,
TADDR nativeFnxStart,
SIZE_T *nativeOffset)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
PRECONDITION(nativeOffset != NULL);
PRECONDITION(CheckPointer(pMD));
PRECONDITION(nativeFnxStart != NULL);
}
CONTRACTL_END;
_ASSERTE(HasLazyData()); // only used for EnC, should have already inited.
LOG((LF_CORDB, LL_INFO1000000, "D::MILITCN: %s::%s ilOff:0x%x, "
", natFnx:0x%x dji:0x%x\n", pMD->m_pszDebugClassName,
pMD->m_pszDebugMethodName, ilOffset, nativeFnxStart));
*nativeOffset = 0;
DebuggerJitInfo *djiTo = GetJitInfo( pMD, (const BYTE *)nativeFnxStart);
if (djiTo == NULL)
{
_ASSERTE(!"No DJI in EnC case: should only happen on oom. Debugger doesn't support OOM.");
return E_FAIL;
}
DebuggerJitInfo::ILToNativeOffsetIterator it;
djiTo->InitILToNativeOffsetIterator(it, ilOffset);
*nativeOffset = it.CurrentAssertOnlyOne(NULL);
return S_OK;
}
#endif // EnC_SUPPORTED
//---------------------------------------------------------------------------------------
// Hijack worker stub called from asm stub. This can then delegate to other hijacks.
//
// Arguments:
// pContext - context from which we were hijacked. Always non-null.
// pRecord - exception record if hijacked from an exception event.
// Else null (if hijacked from a managed IP).
// reason - hijack reason. Use this to delegate to the proper hijack stub.
// pData - arbitrary data for the hijack to use. (eg, such as a DebuggerEval object)
//
// Returns:
// This does not return. Instead it restores this threads context to pContext.
//
// Assumptions:
// If hijacked at an exception event, the debugger must have cleared the exception.
//
// Notes:
// The debugger hijacked the thread to get us here via the DacDbi Hijack primitive.
// This is called from a hand coded asm stub.
//
void STDCALL ExceptionHijackWorker(
CONTEXT * pContext,
EXCEPTION_RECORD * pRecord,
EHijackReason::EHijackReason reason,
void * pData)
{
STRESS_LOG0(LF_CORDB,LL_INFO100, "D::EHW: Enter ExceptionHijackWorker\n");
// We could have many different reasons for hijacking. Switch and invoke the proper hijacker.
switch(reason)
{
case EHijackReason::kUnhandledException:
STRESS_LOG0(LF_CORDB,LL_INFO10, "D::EHW: Calling g_pDebugger->UnhandledHijackWorker()\n");
_ASSERTE(pData == NULL);
g_pDebugger->UnhandledHijackWorker(pContext, pRecord);
break;
#ifdef FEATURE_INTEROP_DEBUGGING
case EHijackReason::kM2UHandoff:
_ASSERTE(pData == NULL);
g_pDebugger->M2UHandoffHijackWorker(pContext, pRecord);
break;
case EHijackReason::kFirstChanceSuspend:
_ASSERTE(pData == NULL);
g_pDebugger->FirstChanceSuspendHijackWorker(pContext, pRecord);
break;
case EHijackReason::kGenericHijack:
_ASSERTE(pData == NULL);
g_pDebugger->GenericHijackFunc();
break;
#endif
default:
CONSISTENCY_CHECK_MSGF(false, ("Unrecognized Hijack code: %d", reason));
}
// Currently, no Hijack actually returns yet.
UNREACHABLE();
// If we return to this point, then we'll restore ourselves.
// We've got the context that we were hijacked from, so we should be able to just
// call SetThreadContext on ourself to fix us.
}
#if defined(WIN64EXCEPTIONS)
#if defined(_TARGET_AMD64_)
// ----------------------------------------------------------------------------
// EmptyPersonalityRoutine
//
// Description:
// This personality routine is used to work around a limitation of the OS unwinder when we return
// ExceptionCollidedUnwind.
// See code:ExceptionHijackPersonalityRoutine for more information.
//
// Arguments:
// * pExceptionRecord - not used
// * MemoryStackFp - not used
// * BackingStoreFp - not used
// * pContextRecord - not used
// * pDispatcherContext - not used
// * GlobalPointer - not used
//
// Return Value:
// Always return ExceptionContinueSearch.
//
EXCEPTION_DISPOSITION EmptyPersonalityRoutine(IN PEXCEPTION_RECORD pExceptionRecord,
IN ULONG64 MemoryStackFp,
IN OUT PCONTEXT pContextRecord,
IN OUT PDISPATCHER_CONTEXT pDispatcherContext)
{
LIMITED_METHOD_CONTRACT;
return ExceptionContinueSearch;
}
#endif // _TARGET_AMD64_
//---------------------------------------------------------------------------------------
// Personality routine for unwinder the assembly hijack stub on 64-bit.
//
// Arguments:
// standard Personality routine signature.
//
// Assumptions:
// This is caleld by the OS exception logic during exception handling.
//
// Notes:
// We just need 1 personality routine for the tiny assembly hijack stub.
// All the C++ code invoked by the stub is ok.
//
// This needs to fetch the original context that this thread was hijacked from
// (which the hijack pushed onto the stack) and pass that back to the OS. This lets
// ths OS unwind out of the hijack.
//
// This function should only be executed if an unhandled exception is intercepted by a managed debugger.
// Otherwise there should never be a 2nd pass exception dispatch crossing the hijack stub.
//
// The basic idea here is straightforward. The OS does an exception dispatch and hit our hijack stub.
// Since the hijack stub is not unwindable, we need a personality routine to restore the CONTEXT and
// tell the OS to continue the dispatch with that CONTEXT by returning ExceptionCollidedUnwind.
//
// However, empricially, the OS expects that when we return ExceptionCollidedUnwind, the function
// represented by the CONTEXT has a personality routine. The OS will actually AV if we return a NULL
// personality routine.
//
// On AMD64, we work around this by using an empty personality routine.
EXTERN_C EXCEPTION_DISPOSITION
ExceptionHijackPersonalityRoutine(IN PEXCEPTION_RECORD pExceptionRecord
WIN64_ARG(IN ULONG64 MemoryStackFp)
NOT_WIN64_ARG(IN ULONG32 MemoryStackFp),
IN OUT PCONTEXT pContextRecord,
IN OUT PDISPATCHER_CONTEXT pDispatcherContext
)
{
#if defined(_TARGET_AMD64_)
CONTEXT * pHijackContext = NULL;
// Get the 1st parameter (the Context) from hijack worker.
pHijackContext = *reinterpret_cast<CONTEXT **>(pDispatcherContext->EstablisherFrame);
// This copies pHijackContext into pDispatcherContext, which the OS can then
// use to walk the stack.
FixupDispatcherContext(pDispatcherContext, pHijackContext, pContextRecord, (PEXCEPTION_ROUTINE)EmptyPersonalityRoutine);
#else
_ASSERTE(!"NYI - ExceptionHijackPersonalityRoutine()");
#endif
// Returning ExceptionCollidedUnwind will cause the OS to take our new context record and
// dispatcher context and restart the exception dispatching on this call frame, which is
// exactly the behavior we want.
return ExceptionCollidedUnwind;
}
#endif // WIN64EXCEPTIONS
// UEF Prototype from excep.cpp
LONG InternalUnhandledExceptionFilter_Worker(EXCEPTION_POINTERS *pExceptionInfo);
//---------------------------------------------------------------------------------------
// Hijack for a 2nd-chance exception. Will invoke the CLR's UEF.
//
// Arguments:
// pContext - context that this thread was hijacked from.
// pRecord - exception record of the exception that this was hijacked at.
// pData - random data.
// Notes:
// When under a native-debugger, the OS does not invoking the Unhandled Exception Filter (UEF).
// It dispatches a 2nd-chance Exception event instead.
// However, the CLR's UEF does lots of useful work (like dispatching the 2nd-chance managed exception,
// allowing func-eval on 2nd-chance, and allowing intercepting unhandled exceptions).
// So we'll emulate the OS behavior here by invoking the CLR's UEF directly.
//
void Debugger::UnhandledHijackWorker(CONTEXT * pContext, EXCEPTION_RECORD * pRecord)
{
CONTRACTL
{
// The ultimate protection shield is that this hijack can be executed under the same circumstances
// as a top-level UEF that pinvokes into managed code
// - That means we're GC-triggers safe
// - that means that we can crawl the stack. (1st-pass EH logic ensures this).
// We need to be GC-triggers because this may invoke a func-eval.
GC_TRIGGERS;
// Don't throw out of a hijack! There's nobody left to catch this.
NOTHROW;
// We expect to always be in preemptive here by the time we get this unhandled notification.
// We know this is true because a native UEF is preemptive.
// More detail:
// 1) If we got here from a software exception (eg, Throw from C#), then the jit helper
// toggled us to preemptive before calling RaiseException().
// 2) If we got here from a hardware exception in managed code, then the 1st-pass already did
// some magic to get us into preemptive. On x86, this is magic. On 64-bit, it did some magic
// to push a Faulting-Exception-Frame and rethrow the exception as a software exception.
MODE_PREEMPTIVE;
PRECONDITION(CheckPointer(pContext));
PRECONDITION(CheckPointer(pRecord));
}
CONTRACTL_END;
EXCEPTION_POINTERS exceptionInfo;
exceptionInfo.ContextRecord = pContext;
exceptionInfo.ExceptionRecord = pRecord;
// Snag the Runtime thread. Since we're hijacking a managed exception, we should always have one.
Thread * pThread = g_pEEInterface->GetThread();
(void)pThread; //prevent "unused variable" error from GCC
_ASSERTE(pThread != NULL);
BOOL fSOException = FALSE;
if ((pRecord != NULL) &&
(pRecord->ExceptionCode == STATUS_STACK_OVERFLOW))
{
fSOException = TRUE;
}
// because we hijack here during jit attach invoked by the OS we need to make sure that the debugger is completely
// attached before continuing. If we ever hijacked here when an attach was not in progress this function returns
// immediately so no problems there.
WaitForDebuggerAttach();
PostJitAttach();
// On Win7 WatsonLastChance returns CONTINUE_SEARCH for unhandled exceptions execpt stack overflow, and
// lets OS launch debuggers for us. Before the unhandled exception reaches the OS, CLR UEF has already
// processed this unhandled exception. Thus, we should not call into CLR UEF again if it is the case.
if (RunningOnWin7() &&
pThread &&
(pThread->HasThreadStateNC(Thread::TSNC_ProcessedUnhandledException) ||
pThread->HasThreadStateNC(Thread::TSNC_AppDomainContainUnhandled) ||
fSOException))
{
FrameWithCookie<FaultingExceptionFrame> fef;
#if defined(WIN64EXCEPTIONS)
*((&fef)->GetGSCookiePtr()) = GetProcessGSCookie();
#endif // WIN64EXCEPTIONS
if ((pContext != NULL) && fSOException)
{
GCX_COOP(); // Must be cooperative to modify frame chain.
// EEPolicy::HandleFatalStackOverflow pushes a FaultingExceptionFrame on the stack after SO
// exception. Our hijack code runs in the exception context, and overwrites the stack space
// after SO excpetion, so this frame was popped out before invoking RaiseFailFast. We need to
// put it back here for running func-eval code.
// This cumbersome code should be removed once SO synchronization is moved to be completely
// out-of-process.
fef.InitAndLink(pContext);
}
STRESS_LOG0(LF_CORDB, LL_INFO10, "D::EHW: Calling NotifyDebuggerLastChance\n");
NotifyDebuggerLastChance(pThread, &exceptionInfo, TRUE);
// Continuing from a second chance managed exception causes the process to exit.
TerminateProcess(GetCurrentProcess(), 0);
}
// Since this is a unhandled managed exception:
// - we always have a Thread* object.
// - we always have a throwable
// - we executed through the 1st-pass of the EH logic. This means the 1st-pass could do work
// to enforce certain invariants (like the ones listed here, or ensuring the thread can be crawled)
// Need to call the CLR's UEF. This will do all the key work including:
// - send the managed 2nd-chance exception event.
// - deal with synchronization.
// - allow func-evals.
// - deal with interception.
// If intercepted, then this never returns. It will manually invoke the unwinders and fix the context.
// InternalUnhandledExceptionFilter_Worker has a throws contract, but should not be throwing in any
// conditions we care about. This hijack should never throw, so catch everything.
HRESULT hrIgnore;
EX_TRY
{
InternalUnhandledExceptionFilter_Worker(&exceptionInfo);
}
EX_CATCH_HRESULT(hrIgnore);
// Continuing from a second chance managed exception causes the process to exit.
TerminateProcess(GetCurrentProcess(), 0);
}
#ifdef FEATURE_INTEROP_DEBUGGING
//
// This is the handler function that is put in place of a thread's top-most SEH handler function when it is hijacked by
// the Right Side during an unmanaged first chance exception.
//
typedef EXCEPTION_DISPOSITION (__cdecl *SEHHandler)(EXCEPTION_RECORD *pExceptionRecord,
EXCEPTION_REGISTRATION_RECORD *pEstablisherFrame,
CONTEXT *pContext,
void *DispatcherContext);
#define DOSPEW 0
#if DOSPEW
#define SPEW(s) s
#else
#define SPEW(s)
#endif
//-----------------------------------------------------------------------------
// Hijack when we have a M2U handoff.
// This happens when we do a step-out from Managed-->Unmanaged, and so we hit a managed patch in Native code.
// This also happens when a managed stepper does a step-in to unmanaged code.
// Since we're in native code, there's no CPFH, and so we have to hijack.
// @todo- could this be removed? Step-out to native is illegal in v2.0, and do existing
// CLR filters catch the step-in patch?
// @dbgtodo controller/stepping - this will be completely unneeded in V3 when all stepping is oop
//-----------------------------------------------------------------------------
VOID Debugger::M2UHandoffHijackWorker(CONTEXT *pContext,
EXCEPTION_RECORD *pExceptionRecord)
{
// We must use a static contract here because the function does not return normally
STATIC_CONTRACT_NOTHROW;
STATIC_CONTRACT_GC_TRIGGERS; // from sending managed event
STATIC_CONTRACT_MODE_PREEMPTIVE; // we're in umanaged code.
SO_NOT_MAINLINE_FUNCTION;
LOG((LF_CORDB, LL_INFO1000, "D::M2UHHW: Context=0x%p exception record=0x%p\n",
pContext, pExceptionRecord));
// We should only be here for a BP
_ASSERTE(pExceptionRecord->ExceptionCode == STATUS_BREAKPOINT);
// Get the current runtime thread. This is only an optimized TLS access.
// Since we're coming off a managed-step, we should always have a thread.
Thread *pEEThread = g_pEEInterface->GetThread();
_ASSERTE(pEEThread != NULL);
_ASSERTE(!pEEThread->GetInteropDebuggingHijacked());
pEEThread->SetInteropDebuggingHijacked(TRUE);
//win32 has a weird property where EIP points after the BP in the debug event
//so we are adjusting it to point at the BP
CORDbgAdjustPCForBreakInstruction((DT_CONTEXT*)pContext);
LOG((LF_CORDB, LL_INFO1000, "D::M2UHHW: Context ip set to 0x%p\n", GetIP(pContext)));
_ASSERTE(!ISREDIRECTEDTHREAD(pEEThread));
// Don't bother setting FilterContext here because we already pass it to FirstChanceNativeException.
// Shortcut right to our dispatch native exception logic, there may be no COMPlusFrameHandler in place!
EX_TRY
{
LOG((LF_CORDB, LL_INFO1000, "D::M2UHHW: Calling FirstChanceNativeException\n"));
bool okay;
okay = g_pDebugger->FirstChanceNativeException(pExceptionRecord,
pContext,
pExceptionRecord->ExceptionCode,
pEEThread);
_ASSERTE(okay == true);
LOG((LF_CORDB, LL_INFO1000, "D::M2UHHW: FirstChanceNativeException returned\n"));
}
EX_CATCH
{
// It would be really bad if somebody threw here. We're actually outside of managed code,
// so there's not a lot we can do besides just swallow the exception and hope for the best.
LOG((LF_CORDB, LL_INFO1000, "D::M2UHHW: ERROR! FirstChanceNativeException threw an exception\n"));
}
EX_END_CATCH(SwallowAllExceptions);
_ASSERTE(!ISREDIRECTEDTHREAD(pEEThread));
_ASSERTE(pEEThread->GetInteropDebuggingHijacked());
pEEThread->SetInteropDebuggingHijacked(FALSE);
// This signal will be received by the RS and it will use SetThreadContext
// to clear away the entire hijack frame. This function does not return.
LOG((LF_CORDB, LL_INFO1000, "D::M2UHHW: Flaring hijack complete\n"));
SignalHijackComplete();
_ASSERTE(!"UNREACHABLE");
}
//-----------------------------------------------------------------------------
// This hijack is run after receiving an IB event that we don't know how the
// debugger will want to continue. Under the covers we clear the event and divert
// execution here where we block until the debugger decides whether or not to clear
// the event. At that point we exit this hijack and the LS diverts execution back
// to the offending instruction.
// We don't know:
// - whether we have an EE-thread?
// - how we're going to continue this (handled / not-handled).
//
// But we do know that:
// - this exception does not belong to the CLR.
// - this thread is not in cooperative mode.
//-----------------------------------------------------------------------------
LONG Debugger::FirstChanceSuspendHijackWorker(CONTEXT *pContext,
EXCEPTION_RECORD *pExceptionRecord)
{
// if we aren't set up to do interop debugging this function should just bail out
if(m_pRCThread == NULL)
return EXCEPTION_CONTINUE_SEARCH;
DebuggerIPCControlBlock *pDCB = m_pRCThread->GetDCB();
if(pDCB == NULL)
return EXCEPTION_CONTINUE_SEARCH;
if (!pDCB->m_rightSideIsWin32Debugger)
return EXCEPTION_CONTINUE_SEARCH;
// at this point we know that there is an interop debugger attached. This makes it safe to send
// flares
#if DOSPEW
DWORD tid = GetCurrentThreadId();
#endif
SPEW(fprintf(stderr, "0x%x D::FCHF: in first chance hijack filter.\n", tid));
SPEW(fprintf(stderr, "0x%x D::FCHF: pExceptionRecord=0x%p (%d), pContext=0x%p (%d)\n", tid, pExceptionRecord, sizeof(EXCEPTION_RECORD),
pContext, sizeof(CONTEXT)));
#if defined(_TARGET_AMD64_)
SPEW(fprintf(stderr, "0x%x D::FCHF: code=0x%08x, addr=0x%p, Rip=0x%p, Rsp=0x%p, EFlags=0x%08x\n",
tid, pExceptionRecord->ExceptionCode, pExceptionRecord->ExceptionAddress, pContext->Rip, pContext->Rsp,
pContext->EFlags));
#elif defined(_TARGET_X86_)
SPEW(fprintf(stderr, "0x%x D::FCHF: code=0x%08x, addr=0x%08x, Eip=0x%08x, Esp=0x%08x, EFlags=0x%08x\n",
tid, pExceptionRecord->ExceptionCode, pExceptionRecord->ExceptionAddress, pContext->Eip, pContext->Esp,
pContext->EFlags));
#endif
// This memory is used as IPC during the hijack. We will place a pointer to this in
// either the EEThreadPtr or the EEDebuggerWord and then the RS can write info into
// the memory
DebuggerIPCFirstChanceData fcd;
// accessing through the volatile pointer to fend off some potential compiler optimizations.
// If the debugger changes that data from OOP we need to see those updates
volatile DebuggerIPCFirstChanceData* pFcd = &fcd;
{
// Hijack filters are always in the can't stop range.
// The RS knows this b/c it knows which threads it hijacked.
// Bump up the CS counter so that any further calls in the LS can see this too.
// (This makes places where we assert that we're in a CS region happy).
CantStopHolder hCantStop;
// Get the current runtime thread. This is only an optimized TLS access.
Thread *pEEThread = g_pEEInterface->GetThread();
// Is that really a ptr to a Thread? If the low bit is set or it its NULL then we don't have an EE Thread. If we
// have a EE Thread, then we know the original handler now. If not, we have to wait for the Right Side to fixup our
// handler chain once we've notified it that the exception does not belong to the runtime. Note: if we don't have an
// EE thread, then the exception never belongs to the Runtime.
bool hasEEThread = false;
if ((pEEThread != NULL) && !(((UINT_PTR)pEEThread) & 0x01))
{
SPEW(fprintf(stderr, "0x%x D::FCHF: Has EE thread.\n", tid));
hasEEThread = true;
}
// Hook up the memory so RS can get to it
fcd.pLeftSideContext.Set((DT_CONTEXT*)pContext);
fcd.action = HIJACK_ACTION_EXIT_UNHANDLED;
fcd.debugCounter = 0;
if(hasEEThread)
{
SPEW(fprintf(stderr, "0x%x D::FCHF: Set Debugger word to 0x%p.\n", tid, pFcd));
g_pEEInterface->SetThreadDebuggerWord(pEEThread, (VOID*) pFcd);
}
else
{
// this shouldn't be re-entrant
_ASSERTE(pEEThread == NULL);
SPEW(fprintf(stderr, "0x%x D::FCHF: EEThreadPtr word to 0x%p.\n", tid, (BYTE*)pFcd + 1));
g_pEEInterface->SetEEThreadPtr((void*) ((BYTE*)pFcd + 1));
}
// Signal the RS to tell us what to do
SPEW(fprintf(stderr, "0x%x D::FCHF: Signaling hijack started.\n", tid));
SignalHijackStarted();
SPEW(fprintf(stderr, "0x%x D::FCHF: Signaling hijack started complete. DebugCounter=0x%x\n", tid, pFcd->debugCounter));
if(pFcd->action == HIJACK_ACTION_WAIT)
{
// This exception does NOT belong to the CLR.
// If we belong to the CLR, then we either:
// - were a M2U transition, in which case we should be in a different Hijack
// - were a CLR exception in CLR code, in which case we should have continued and let the inproc handlers get it.
SPEW(fprintf(stderr, "0x%x D::FCHF: exception does not belong to the Runtime, hasEEThread=%d, pContext=0x%p\n",
tid, hasEEThread, pContext));
if(hasEEThread)
{
_ASSERTE(!pEEThread->GetInteropDebuggingHijacked()); // hijack is not re-entrant.
pEEThread->SetInteropDebuggingHijacked(TRUE);
// Setting the FilterContext must be done in cooperative mode (since it's like pushing a Frame onto the Frame chain).
// Thus we have a violation. We don't really need the filter context specifically here, we're just using
// it for legacy purposes as a way to stash the context of the original exception (that this thread was hijacked from).
// @todo - use another way to store the context indepedent of the Filter context.
CONTRACT_VIOLATION(ModeViolation);
_ASSERTE(g_pEEInterface->GetThreadFilterContext(pEEThread) == NULL);
g_pEEInterface->SetThreadFilterContext(pEEThread, pContext);
}
// Wait for the continue. We may / may not have an EE Thread for this, (and we're definitely
// not doing fiber-mode debugging), so just use a raw win32 API, and not some fancy fiber-safe call.
SPEW(fprintf(stderr, "0x%x D::FCHF: waiting for continue.\n", tid));
DWORD ret = WaitForSingleObject(g_pDebugger->m_pRCThread->GetDCB()->m_leftSideUnmanagedWaitEvent,
INFINITE);
SPEW(fprintf(stderr, "0x%x D::FCHF: waiting for continue complete.\n", tid));
if (ret != WAIT_OBJECT_0)
{
SPEW(fprintf(stderr, "0x%x D::FCHF: wait failed!\n", tid));
}
if(hasEEThread)
{
_ASSERTE(pEEThread->GetInteropDebuggingHijacked());
pEEThread->SetInteropDebuggingHijacked(FALSE);
_ASSERTE(!ISREDIRECTEDTHREAD(pEEThread));
// See violation above.
CONTRACT_VIOLATION(ModeViolation);
g_pEEInterface->SetThreadFilterContext(pEEThread, NULL);
_ASSERTE(g_pEEInterface->GetThreadFilterContext(pEEThread) == NULL);
}
}
SPEW(fprintf(stderr, "0x%x D::FCHF: signaling HijackComplete.\n", tid));
SignalHijackComplete();
SPEW(fprintf(stderr, "0x%x D::FCHF: done signaling HijackComplete. DebugCounter=0x%x\n", tid, pFcd->debugCounter));
// we should know what we are about to do now
_ASSERTE(pFcd->action != HIJACK_ACTION_WAIT);
// cleanup from above
if (hasEEThread)
{
SPEW(fprintf(stderr, "0x%x D::FCHF: set debugger word = NULL.\n", tid));
g_pEEInterface->SetThreadDebuggerWord(pEEThread, (VOID*) NULL);
}
else
{
SPEW(fprintf(stderr, "0x%x D::FCHF: set EEThreadPtr = NULL.\n", tid));
g_pEEInterface->SetEEThreadPtr(NULL);
}
} // end can't stop region
if(pFcd->action == HIJACK_ACTION_EXIT_HANDLED)
{
SPEW(fprintf(stderr, "0x%x D::FCHF: exiting with CONTINUE_EXECUTION\n", tid));
return EXCEPTION_CONTINUE_EXECUTION;
}
else
{
SPEW(fprintf(stderr, "0x%x D::FCHF: exiting with CONTINUE_SEARCH\n", tid));
_ASSERTE(pFcd->action == HIJACK_ACTION_EXIT_UNHANDLED);
return EXCEPTION_CONTINUE_SEARCH;
}
}
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
void GenericHijackFuncHelper()
{
#if DOSPEW
DWORD tid = GetCurrentThreadId();
#endif
// Hijack filters are always in the can't stop range.
// The RS knows this b/c it knows which threads it hijacked.
// Bump up the CS counter so that any further calls in the LS can see this too.
// (This makes places where we assert that we're in a CS region happy).
CantStopHolder hCantStop;
SPEW(fprintf(stderr, "0x%x D::GHF: in generic hijack.\n", tid));
// There is no need to setup any context pointer or interact with the Right Side in anyway. We simply wait for
// the continue event to be set.
SPEW(fprintf(stderr, "0x%x D::GHF: waiting for continue.\n", tid));
// If this thread has an EE thread and that EE thread has preemptive gc disabled, then mark that there is a
// thread at an unsafe place and enable pgc. This will allow us to sync even with this thread hijacked.
bool disabled = false;
Thread *pEEThread = g_pEEInterface->GetThread();
if ((pEEThread != NULL) && !(((UINT_PTR)pEEThread) & 0x01))
{
disabled = g_pEEInterface->IsPreemptiveGCDisabled();
_ASSERTE(!disabled);
_ASSERTE(!pEEThread->GetInteropDebuggingHijacked());
pEEThread->SetInteropDebuggingHijacked(TRUE);
}
DWORD ret = WaitForSingleObject(g_pRCThread->GetDCB()->m_leftSideUnmanagedWaitEvent,
INFINITE);
if (ret != WAIT_OBJECT_0)
{
SPEW(fprintf(stderr, "0x%x D::GHF: wait failed!\n", tid));
}
// Get the continue type. Non-zero means that the exception was not cleared by the Right Side and therefore has
// not been handled. Zero means that the exception has been cleared. (Presumably, the debugger altered the
// thread's context before clearing the exception, so continuing will give a different result.)
DWORD continueType = 0;
pEEThread = g_pEEInterface->GetThread();
if (((UINT_PTR)pEEThread) & 0x01)
{
// There is no EE Thread for this thread, so we null out the TLS word so we don't confuse the Runtime.
continueType = 1;
g_pEEInterface->SetEEThreadPtr(NULL);
pEEThread = NULL;
}
else if (pEEThread)
{
// We've got a Thread ptr, so get the continue type out of the thread's debugger word.
continueType = (DWORD) g_pEEInterface->GetThreadDebuggerWord(pEEThread);
_ASSERTE(pEEThread->GetInteropDebuggingHijacked());
pEEThread->SetInteropDebuggingHijacked(FALSE);
}
SPEW(fprintf(stderr, "0x%x D::GHF: continued with %d.\n", tid, continueType));
if (continueType)
{
SPEW(fprintf(stderr, "0x%x D::GHF: calling ExitProcess\n", tid));
// Continuing from a second chance exception without clearing the exception causes the process to
// exit. Note: the continue type will only be non-zero if this hijack was setup for a second chance
// exception. If the hijack was setup for another type of debug event, then we'll never get here.
//
// We explicitly terminate the process directly instead of going through any escalation policy because:
// 1) that's what a native-only debugger would do. Interop and Native-only should be the same.
// 2) there's no CLR escalation policy anyways for *native* unhandled exceptions.
// 3) The escalation policy may do lots of extra confusing work (like fire MDAs) that can only cause
// us grief.
TerminateProcess(GetCurrentProcess(), 0);
}
SPEW(fprintf(stderr, "0x%x D::GHF: signaling continue...\n", tid));
}
#endif
//
// This is the function that a thread is hijacked to by the Right Side during a variety of debug events. This function
// must be naked.
//
#if defined(_TARGET_X86_)
__declspec(naked)
#endif // defined (_x86_)
void Debugger::GenericHijackFunc(void)
{
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
#if defined(_TARGET_X86_)
_asm
{
push ebp
mov ebp,esp
sub esp,__LOCAL_SIZE
}
#endif
// We can't have C++ classes w/ dtors in a declspec naked, so just have call into a helper.
GenericHijackFuncHelper();
#if defined(_TARGET_X86_)
_asm
{
mov esp,ebp
pop ebp
}
#endif
// This signals the Right Side that this thread is ready to have its context restored.
ExceptionNotForRuntime();
#else
_ASSERTE(!"@todo - port GenericHijackFunc");
#endif // defined (_x86_)
_ASSERTE(!"Should never get here (Debugger::GenericHijackFunc)");
}
//#ifdef _TARGET_X86_
//
// This is the function that is called when we determine that a first chance exception hijack has
// begun and memory is prepared for the RS to tell the LS what to do
//
void Debugger::SignalHijackStarted(void)
{
WRAPPER_NO_CONTRACT;
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
SignalHijackStartedFlare();
#else
_ASSERTE(!"@todo - port the flares to the platform your running on.");
#endif
}
//
// This is the function that is called when we determine that a first chance exception really belongs to the Runtime,
// and that that exception is due to a managed->unmanaged transition. This notifies the Right Side of this and the Right
// Side fixes up the thread's execution state from there, making sure to remember that it needs to continue to hide the
// hijack state of the thread.
//
void Debugger::ExceptionForRuntimeHandoffStart(void)
{
WRAPPER_NO_CONTRACT;
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
ExceptionForRuntimeHandoffStartFlare();
#else
_ASSERTE(!"@todo - port the flares to the platform your running on.");
#endif
}
//
// This is the function that is called when the original handler returns after we've determined that an exception was
// due to a managed->unmanaged transition. This notifies the Right Side of this and the Right Side fixes up the thread's
// execution state from there, making sure to turn off its flag indicating that the thread's hijack state should still
// be hidden.
//
void Debugger::ExceptionForRuntimeHandoffComplete(void)
{
WRAPPER_NO_CONTRACT;
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
ExceptionForRuntimeHandoffCompleteFlare();
#else
_ASSERTE(!"@todo - port the flares to the platform your running on.");
#endif
}
//
// This signals the RS that a hijack function is ready to return. This will cause the RS to restore
// the thread context
//
void Debugger::SignalHijackComplete(void)
{
WRAPPER_NO_CONTRACT;
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
SignalHijackCompleteFlare();
#else
_ASSERTE(!"@todo - port the flares to the platform your running on.");
#endif
}
//
// This is the function that is called when we determine that a first chance exception does not belong to the
// Runtime. This notifies the Right Side of this and the Right Side fixes up the thread's execution state from there.
//
void Debugger::ExceptionNotForRuntime(void)
{
WRAPPER_NO_CONTRACT;
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
ExceptionNotForRuntimeFlare();
#else
_ASSERTE(!"@todo - port the flares to the platform your running on.");
#endif
}
//
// This is the function that is called when we want to send a sync complete event to the Right Side when it is the Win32
// debugger of this process. This notifies the Right Side of this and the Right Side fixes up the thread's execution
// state from there.
//
void Debugger::NotifyRightSideOfSyncComplete(void)
{
WRAPPER_NO_CONTRACT;
STRESS_LOG0(LF_CORDB, LL_INFO100000, "D::NRSOSC: Sending flare...\n");
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
NotifyRightSideOfSyncCompleteFlare();
#else
_ASSERTE(!"@todo - port the flares to the platform your running on.");
#endif
STRESS_LOG0(LF_CORDB, LL_INFO100000, "D::NRSOSC: Flare sent\n");
}
#endif // FEATURE_INTEROP_DEBUGGING
/******************************************************************************
*
******************************************************************************/
bool Debugger::GetILOffsetFromNative (MethodDesc *pFunc, const BYTE *pbAddr,
DWORD nativeOffset, DWORD *ilOffset)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
}
CONTRACTL_END;
if (!HasLazyData())
{
DebuggerLockHolder dbgLockHolder(this);
// This is an entry path into the debugger, so make sure we're inited.
LazyInit();
}
// Sometimes we'll get called w/ an instantiating stub MD.
if (pFunc->IsWrapperStub())
{
pFunc = pFunc->GetWrappedMethodDesc();
}
DebuggerJitInfo *jitInfo =
GetJitInfo(pFunc, (const BYTE *)pbAddr);
if (jitInfo != NULL)
{
CorDebugMappingResult map;
DWORD whichIDontCare;
*ilOffset = jitInfo->MapNativeOffsetToIL(
nativeOffset,
&map,
&whichIDontCare);
return true;
}
return false;
}
/******************************************************************************
*
******************************************************************************/
DWORD Debugger::GetHelperThreadID(void )
{
LIMITED_METHOD_CONTRACT;
return m_pRCThread->GetDCB()
->m_temporaryHelperThreadId;
}
// HRESULT Debugger::InsertToMethodInfoList(): Make sure
// that there's only one head of the the list of DebuggerMethodInfos
// for the (implicitly) given MethodDef/Module pair.
HRESULT
Debugger::InsertToMethodInfoList( DebuggerMethodInfo *dmi )
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO10000,"D:IAHOL DMI: dmi:0x%08x\n", dmi));
HRESULT hr = S_OK;
_ASSERTE(dmi != NULL);
_ASSERTE(HasDebuggerDataLock());
// CHECK_DJI_TABLE_DEBUGGER;
hr = CheckInitMethodInfoTable();
if (FAILED(hr)) {
return (hr);
}
DebuggerMethodInfo *dmiPrev = m_pMethodInfos->GetMethodInfo(dmi->m_module, dmi->m_token);
_ASSERTE((dmiPrev == NULL) || ((dmi->m_token == dmiPrev->m_token) && (dmi->m_module == dmiPrev->m_module)));
LOG((LF_CORDB,LL_INFO10000,"D:IAHOL: current head of dmi list:0x%08x\n",dmiPrev));
if (dmiPrev != NULL)
{
dmi->m_prevMethodInfo = dmiPrev;
dmiPrev->m_nextMethodInfo = dmi;
_ASSERTE(dmi->m_module != NULL);
hr = m_pMethodInfos->OverwriteMethodInfo(dmi->m_module,
dmi->m_token,
dmi,
FALSE);
LOG((LF_CORDB,LL_INFO10000,"D:IAHOL: DMI version 0x%04x for token 0x%08x\n",
dmi->GetCurrentEnCVersion(),dmi->m_token));
}
else
{
LOG((LF_CORDB, LL_EVERYTHING, "AddMethodInfo being called in D:IAHOL\n"));
hr = m_pMethodInfos->AddMethodInfo(dmi->m_module,
dmi->m_token,
dmi);
}
#ifdef _DEBUG
dmiPrev = m_pMethodInfos->GetMethodInfo(dmi->m_module, dmi->m_token);
LOG((LF_CORDB,LL_INFO10000,"D:IAHOL: new head of dmi list:0x%08x\n",
dmiPrev));
#endif //_DEBUG
// DebuggerDataLockHolder out of scope - release implied
return hr;
}
//-----------------------------------------------------------------------------
// Helper to get an SString through the IPC buffer.
// We do this by putting the SString data into a LS_RS_buffer object,
// and then the RS reads it out as soon as it's queued.
// It's very very important that the SString's buffer is around while we send the event.
// So we pass the SString by reference in case there's an implicit conversion (because
// we don't want to do the conversion on a temporary object and then lose that object).
//-----------------------------------------------------------------------------
void SetLSBufferFromSString(Ls_Rs_StringBuffer * pBuffer, SString & str)
{
// Copy string contents (+1 for null terminator) into a LS_RS_Buffer.
// Then the RS can pull it out as a null-terminated string.
pBuffer->SetLsData(
(BYTE*) str.GetUnicode(),
(str.GetCount() +1)* sizeof(WCHAR)
);
}
//*************************************************************
// structure that we to marshal MDA Notification event data.
//*************************************************************
struct SendMDANotificationParams
{
Thread * m_pThread; // may be NULL. Lets us send on behalf of other threads.
// Pass SStrings by ptr in case to guarantee that they're shared (in case we internally modify their storage).
SString * m_szName;
SString * m_szDescription;
SString * m_szXML;
CorDebugMDAFlags m_flags;
SendMDANotificationParams(
Thread * pThread, // may be NULL. Lets us send on behalf of other threads.
SString * szName,
SString * szDescription,
SString * szXML,
CorDebugMDAFlags flags
) :
m_pThread(pThread),
m_szName(szName),
m_szDescription(szDescription),
m_szXML(szXML),
m_flags(flags)
{
LIMITED_METHOD_CONTRACT;
}
};
//-----------------------------------------------------------------------------
// Actually send the MDA event. (Could be on any thread)
// Parameters:
// params - data to initialize the IPC event.
//-----------------------------------------------------------------------------
void Debugger::SendRawMDANotification(
SendMDANotificationParams * params
)
{
// Send the unload assembly event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
Thread * pThread = params->m_pThread;
AppDomain *pAppDomain = (pThread != NULL) ? pThread->GetDomain() : NULL;
InitIPCEvent(ipce,
DB_IPCE_MDA_NOTIFICATION,
pThread,
pAppDomain);
SetLSBufferFromSString(&ipce->MDANotification.szName, *(params->m_szName));
SetLSBufferFromSString(&ipce->MDANotification.szDescription, *(params->m_szDescription));
SetLSBufferFromSString(&ipce->MDANotification.szXml, *(params->m_szXML));
ipce->MDANotification.dwOSThreadId = GetCurrentThreadId();
ipce->MDANotification.flags = params->m_flags;
m_pRCThread->SendIPCEvent();
}
//-----------------------------------------------------------------------------
// Send an MDA notification. This ultimately translates to an ICorDebugMDA object on the Right-Side.
// Called by EE to send a MDA debug event. This will block on the debug event
// until the RS continues us.
// Debugger may or may not be attached. If bAttached, then this
// will trigger a jitattach as well.
// See MDA documentation for what szName, szDescription + szXML should look like.
// The debugger just passes them through.
//
// Parameters:
// pThread - thread for debug event. May be null.
// szName - short name of MDA.
// szDescription - full description of MDA.
// szXML - xml string for MDA.
// bAttach - do a JIT-attach
//-----------------------------------------------------------------------------
void Debugger::SendMDANotification(
Thread * pThread, // may be NULL. Lets us send on behalf of other threads.
SString * szName,
SString * szDescription,
SString * szXML,
CorDebugMDAFlags flags,
BOOL bAttach
)
{
CONTRACTL
{
THROWS;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
PREFIX_ASSUME(szName != NULL);
PREFIX_ASSUME(szDescription != NULL);
PREFIX_ASSUME(szXML != NULL);
// Note: we normally don't send events like this when there is an unrecoverable error. However,
// if a host attempts to setup fiber mode on a thread, then we'll set an unrecoverable error
// and use an MDA to 1) tell the user and 2) get the Right Side to notice the unrecoverable error.
// Therefore, we'll go ahead and send a MDA event if the unrecoverable error is
// CORDBG_E_CANNOT_DEBUG_FIBER_PROCESS.
DebuggerIPCControlBlock *pDCB = m_pRCThread->GetDCB();
// If the MDA is ocuring very early in startup before the DCB is setup, then bail.
if (pDCB == NULL)
{
return;
}
if (CORDBUnrecoverableError(this) && (pDCB->m_errorHR != CORDBG_E_CANNOT_DEBUG_FIBER_PROCESS))
{
return;
}
// Validate flags. Make sure that folks don't start passing flags that we don't handle.
// If pThread != current thread, caller should either pass in MDA_FLAG_SLIP or guarantee
// that pThread is not slipping.
_ASSERTE((flags & ~(MDA_FLAG_SLIP)) == 0);
// Helper thread should not be triggering MDAs. The helper thread is executing code in a very constrained
// and controlled region and shouldn't be able to do anything dangerous.
// If we revise this in the future, we should probably just post the event to the RS w/ use the MDA_FLAG_SLIP flag,
// and then not bother suspending the runtime. The RS will get it on its next event.
// The jit-attach logic below assumes we're not on the helper. (If we are on the helper, then a debugger should already
// be attached)
if (ThisIsHelperThreadWorker())
{
CONSISTENCY_CHECK_MSGF(false, ("MDA '%s' fired on *helper* thread.\r\nDesc:%s",
szName->GetUnicode(), szDescription->GetUnicode()
));
// If for some reason we're wrong about the assert above, we'll just ignore the MDA (rather than potentially deadlock)
return;
}
// Public entry point into the debugger. May cause a jit-attach, so we may need to be lazily-init.
if (!HasLazyData())
{
DebuggerLockHolder dbgLockHolder(this);
// This is an entry path into the debugger, so make sure we're inited.
LazyInit();
}
// Cases:
// 1) Debugger already attached, send event normally (ignore severity)
// 2) No debugger attached, Non-severe probe - ignore.
// 3) No debugger attached, Severe-probe - do a jit-attach.
bool fTryJitAttach = bAttach == TRUE;
// Check case #2 - no debugger, and no jit-attach. Early opt out.
if (!CORDebuggerAttached() && !fTryJitAttach)
{
return;
}
if (pThread == NULL)
{
// If there's no thread object, then we're not blocking after the event,
// and thus this probe may slip.
flags = (CorDebugMDAFlags) (flags | MDA_FLAG_SLIP);
}
{
GCX_PREEMP_EEINTERFACE_TOGGLE_IFTHREAD();
// For "Severe" probes, we'll do a jit attach dialog
if (fTryJitAttach)
{
// May return:
// - S_OK if we do a jit-attach,
// - S_FALSE if a debugger is already attached.
// - Error in other cases..
JitAttach(pThread, NULL, TRUE, FALSE);
}
// Debugger may be attached now...
if (CORDebuggerAttached())
{
SendMDANotificationParams params(pThread, szName, szDescription, szXML, flags);
// Non-attach case. Send like normal event.
// This includes if someone launch the debugger during the meantime.
// just send the event
SENDIPCEVENT_BEGIN(this, pThread);
// Send Log message event to the Right Side
SendRawMDANotification(¶ms);
// Stop all Runtime threads
// Even if we don't have a managed thead object, this will catch us at the next good spot.
TrapAllRuntimeThreads();
// Let other Runtime threads handle their events.
SENDIPCEVENT_END;
}
} // end of GCX_PREEMP_EEINTERFACE_TOGGLE()
}
//*************************************************************
// This method sends a log message over to the right side for the debugger to log it.
//
// The CLR doesn't assign any semantics to the level or cateogory values.
// The BCL has a level convention (LoggingLevels enum), but this isn't exposed publicly,
// so we shouldn't base our behavior on it in any way.
//*************************************************************
void Debugger::SendLogMessage(int iLevel,
SString * pSwitchName,
SString * pMessage)
{
CONTRACTL
{
GC_TRIGGERS;
THROWS;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::SLM: Sending log message.\n"));
// Send the message only if the debugger is attached to this appdomain.
// Note the the debugger may detach at any time, so we'll have to check
// this again after we get the lock.
AppDomain *pAppDomain = g_pEEInterface->GetThread()->GetDomain();
if (!CORDebuggerAttached())
{
return;
}
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
// Send Log message event to the Right Side
SendRawLogMessage(
pThread,
pAppDomain,
iLevel,
pSwitchName,
pMessage);
// Stop all Runtime threads
TrapAllRuntimeThreads();
// Let other Runtime threads handle their events.
SENDIPCEVENT_END;
}
//*************************************************************
//
// Helper function to just send LogMessage event. Can be called on either
// helper thread or managed thread.
//
//*************************************************************
void Debugger::SendRawLogMessage(
Thread *pThread,
AppDomain *pAppDomain,
int iLevel,
SString * pCategory,
SString * pMessage
)
{
DebuggerIPCEvent* ipce;
// We should have hold debugger lock
// This can happen on either native helper thread or managed thread
_ASSERTE(ThreadHoldsLock());
// It's possible that the debugger dettached while we were waiting
// for our lock. Check again and abort the event if it did.
if (!CORDebuggerAttached())
{
return;
}
ipce = m_pRCThread->GetIPCEventSendBuffer();
// Send a LogMessage event to the Right Side
InitIPCEvent(ipce,
DB_IPCE_FIRST_LOG_MESSAGE,
pThread,
pAppDomain);
ipce->FirstLogMessage.iLevel = iLevel;
ipce->FirstLogMessage.szCategory.SetString(pCategory->GetUnicode());
SetLSBufferFromSString(&ipce->FirstLogMessage.szContent, *pMessage);
m_pRCThread->SendIPCEvent();
}
// This function sends a message to the right side informing it about
// the creation/modification of a LogSwitch
void Debugger::SendLogSwitchSetting(int iLevel,
int iReason,
__in_z LPCWSTR pLogSwitchName,
__in_z LPCWSTR pParentSwitchName)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000, "D::SLSS: Sending log switch message switch=%S parent=%S.\n",
pLogSwitchName, pParentSwitchName));
// Send the message only if the debugger is attached to this appdomain.
if (!CORDebuggerAttached())
{
return;
}
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
if (CORDebuggerAttached())
{
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_LOGSWITCH_SET_MESSAGE,
pThread,
pThread->GetDomain());
ipce->LogSwitchSettingMessage.iLevel = iLevel;
ipce->LogSwitchSettingMessage.iReason = iReason;
ipce->LogSwitchSettingMessage.szSwitchName.SetString(pLogSwitchName);
if (pParentSwitchName == NULL)
{
pParentSwitchName = W("");
}
ipce->LogSwitchSettingMessage.szParentSwitchName.SetString(pParentSwitchName);
m_pRCThread->SendIPCEvent();
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::SLSS: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
}
// send a custom debugger notification to the RS
// Arguments:
// input: pThread - thread on which the notification occurred
// pDomain - domain file for the domain in which the notification occurred
// classToken - metadata token for the type of the notification object
void Debugger::SendCustomDebuggerNotification(Thread * pThread,
DomainFile * pDomain,
mdTypeDef classToken)
{
CONTRACTL
{
GC_TRIGGERS;
THROWS;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO10000, "D::SLM: Sending log message.\n"));
// Send the message only if the debugger is attached to this appdomain.
// Note the the debugger may detach at any time, so we'll have to check
// this again after we get the lock.
if (!CORDebuggerAttached())
{
return;
}
Thread *curThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, curThread);
if (CORDebuggerAttached())
{
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_CUSTOM_NOTIFICATION,
curThread,
curThread->GetDomain());
VMPTR_DomainFile vmDomainFile = VMPTR_DomainFile::MakePtr(pDomain);
ipce->CustomNotification.classToken = classToken;
ipce->CustomNotification.vmDomainFile = vmDomainFile;
m_pRCThread->SendIPCEvent();
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::SCDN: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
}
//-----------------------------------------------------------------------------
//
// Add the AppDomain to the list stored in the IPC block. It adds the id and
// the name.
//
// Arguments:
// pAppDomain - The runtime app domain object to add.
//
// Return Value:
// S_OK on success, else detailed error code.
//
HRESULT Debugger::AddAppDomainToIPC(AppDomain *pAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
GC_TRIGGERS;
MODE_ANY;
}
CONTRACTL_END;
HRESULT hr = S_OK;
LPCWSTR szName = NULL;
LOG((LF_CORDB, LL_INFO100, "D::AADTIPC: Executing AADTIPC for AppDomain 0x%08x (0x%x).\n",
pAppDomain,
pAppDomain->GetId().m_dwId));
STRESS_LOG2(LF_CORDB, LL_INFO10000, "D::AADTIPC: AddAppDomainToIPC:%#08x, %#08x\n",
pAppDomain, pAppDomain->GetId().m_dwId);
_ASSERTE(m_pAppDomainCB->m_iTotalSlots > 0);
_ASSERTE(m_pAppDomainCB->m_rgListOfAppDomains != NULL);
{
//
// We need to synchronize this routine with the attach logic. The "normal"
// attach case uses the HelperThread and TrapAllRuntimeThreads to synchronize
// the runtime before sending any of the events (including AppDomainCreates)
// to the right-side. Thus, we can synchronize with this case by forcing us
// to go co-operative. If we were already co-op, then the helper thread will
// wait to start the attach until all co-op threads are paused. If we were
// pre-emptive, then going co-op will suspend us until the HelperThread finishes.
//
// The second case is under the IPC event for ATTACHING, which is where there are
// zero app domains, so it is considered an 'early attach' case. To synchronize
// with this we have to grab and hold the AppDomainDB lock.
//
GCX_COOP();
// Lock the list
if (!m_pAppDomainCB->Lock())
{
return E_FAIL;
}
// Get a free entry from the list
AppDomainInfo *pAppDomainInfo = m_pAppDomainCB->GetFreeEntry();
// Function returns NULL if the list is full and a realloc failed.
if (!pAppDomainInfo)
{
hr = E_OUTOFMEMORY;
goto LErrExit;
}
// copy the ID
pAppDomainInfo->m_id = pAppDomain->GetId().m_dwId;
// Now set the AppDomainName.
/*
* TODO :
*
* Make sure that returning NULL here does not result in a catastrophic
* failure.
*
* GetFriendlyNameNoThrow may call SetFriendlyName, which may call
* UpdateAppDomainEntryInIPC. There is no recursive death, however, because
* the AppDomainInfo object does not contain a pointer to the app domain
* yet.
*/
szName = pAppDomain->GetFriendlyNameForDebugger();
pAppDomainInfo->SetName(szName);
// Save on to the appdomain pointer
pAppDomainInfo->m_pAppDomain = pAppDomain;
// bump the used slot count
m_pAppDomainCB->m_iNumOfUsedSlots++;
LErrExit:
// UnLock the list
m_pAppDomainCB->Unlock();
// Send event to debugger if one is attached.
if (CORDebuggerAttached())
{
SendCreateAppDomainEvent(pAppDomain);
}
}
return hr;
}
/******************************************************************************
* Remove the AppDomain from the list stored in the IPC block and send an ExitAppDomain
* event to the debugger if attached.
******************************************************************************/
HRESULT Debugger::RemoveAppDomainFromIPC (AppDomain *pAppDomain)
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
SO_INTOLERANT;
}
CONTRACTL_END;
HRESULT hr = E_FAIL;
LOG((LF_CORDB, LL_INFO100, "D::RADFIPC: Executing RADFIPC for AppDomain 0x%08x (0x%x).\n",
pAppDomain,
pAppDomain->GetId().m_dwId));
// if none of the slots are occupied, then simply return.
if (m_pAppDomainCB->m_iNumOfUsedSlots == 0)
return hr;
// Lock the list
if (!m_pAppDomainCB->Lock())
return (E_FAIL);
// Look for the entry
AppDomainInfo *pADInfo = m_pAppDomainCB->FindEntry(pAppDomain);
// Shouldn't be trying to remove an appdomain that was never added
if (!pADInfo)
{
// We'd like to assert this, but there is a small window where we may have
// called AppDomain::Init (and so it's fair game to call Stop, and hence come here),
// but not yet published the app domain.
// _ASSERTE(!"D::RADFIPC: trying to remove an AppDomain that was never added");
hr = (E_FAIL);
goto ErrExit;
}
// Release the entry
m_pAppDomainCB->FreeEntry(pADInfo);
ErrExit:
// UnLock the list
m_pAppDomainCB->Unlock();
// send event to debugger if one is attached
if (CORDebuggerAttached())
{
SendExitAppDomainEvent(pAppDomain);
}
return hr;
}
/******************************************************************************
* Update the AppDomain in the list stored in the IPC block.
******************************************************************************/
HRESULT Debugger::UpdateAppDomainEntryInIPC(AppDomain *pAppDomain)
{
CONTRACTL
{
NOTHROW;
if (GetThread()) { GC_TRIGGERS;} else {DISABLED(GC_NOTRIGGER);}
SO_INTOLERANT;
}
CONTRACTL_END;
HRESULT hr = S_OK;
LPCWSTR szName = NULL;
LOG((LF_CORDB, LL_INFO100,
"D::UADEIIPC: Executing UpdateAppDomainEntryInIPC ad:0x%x.\n",
pAppDomain));
// if none of the slots are occupied, then simply return.
if (m_pAppDomainCB->m_iNumOfUsedSlots == 0)
return (E_FAIL);
// Lock the list
if (!m_pAppDomainCB->Lock())
return (E_FAIL);
// Look up the info entry
AppDomainInfo *pADInfo = m_pAppDomainCB->FindEntry(pAppDomain);
if (!pADInfo)
{
hr = E_FAIL;
goto ErrExit;
}
// Update the name only if new name is non-null
szName = pADInfo->m_pAppDomain->GetFriendlyNameForDebugger();
pADInfo->SetName(szName);
LOG((LF_CORDB, LL_INFO100,
"D::UADEIIPC: New name:%ls (AD:0x%x)\n", pADInfo->m_szAppDomainName,
pAppDomain));
ErrExit:
// UnLock the list
m_pAppDomainCB->Unlock();
return hr;
}
HRESULT Debugger::CopyModulePdb(Module* pRuntimeModule)
{
CONTRACTL
{
THROWS;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
SO_NOT_MAINLINE;
PRECONDITION(ThisIsHelperThread());
MODE_ANY;
}
CONTRACTL_END;
if (!pRuntimeModule->IsVisibleToDebugger())
{
return S_OK;
}
HRESULT hr = S_OK;
#ifdef FEATURE_FUSION
//
// Populate the pdb to fusion cache.
//
if (pRuntimeModule->IsIStream() == FALSE)
{
SUPPRESS_ALLOCATION_ASSERTS_IN_THIS_SCOPE;
EX_TRY
{
pRuntimeModule->FusionCopyPDBs(pRuntimeModule->GetPath());
}
EX_CATCH_HRESULT(hr); // ignore failures
}
#endif // FEATURE_FUSION
return hr;
}
/******************************************************************************
* When attaching to a process, this is called to enumerate all of the
* AppDomains currently in the process and allow modules pdbs to be copied over to the shadow dir maintaining out V2 in-proc behaviour.
******************************************************************************/
HRESULT Debugger::IterateAppDomainsForPdbs()
{
CONTRACTL
{
THROWS;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
SO_NOT_MAINLINE;
PRECONDITION(ThisIsHelperThread());
MODE_ANY;
}
CONTRACTL_END;
STRESS_LOG0(LF_CORDB, LL_INFO100, "Entered function IterateAppDomainsForPdbs()\n");
HRESULT hr = S_OK;
// Lock the list
if (!m_pAppDomainCB->Lock())
return (E_FAIL);
// Iterate through the app domains
AppDomainInfo *pADInfo = m_pAppDomainCB->FindFirst();
while (pADInfo)
{
STRESS_LOG3(LF_CORDB, LL_INFO100, "Iterating over domain %#08x AD:%#08x %ls\n", pADInfo->m_pAppDomain->GetId().m_dwId, pADInfo->m_pAppDomain, pADInfo->m_szAppDomainName);
AppDomain::AssemblyIterator i;
i = pADInfo->m_pAppDomain->IterateAssembliesEx((AssemblyIterationFlags)(kIncludeLoaded | kIncludeLoading | kIncludeExecution));
CollectibleAssemblyHolder<DomainAssembly *> pDomainAssembly;
while (i.Next(pDomainAssembly.This()))
{
if (!pDomainAssembly->IsVisibleToDebugger())
continue;
DomainAssembly::ModuleIterator j = pDomainAssembly->IterateModules(kModIterIncludeLoading);
while (j.Next())
{
DomainFile * pDomainFile = j.GetDomainFile();
if (!pDomainFile->ShouldNotifyDebugger())
continue;
Module* pRuntimeModule = pDomainFile->GetModule();
CopyModulePdb(pRuntimeModule);
}
if (pDomainAssembly->ShouldNotifyDebugger())
{
CopyModulePdb(pDomainAssembly->GetModule());
}
}
// Get the next appdomain in the list
pADInfo = m_pAppDomainCB->FindNext(pADInfo);
}
// Unlock the list
m_pAppDomainCB->Unlock();
STRESS_LOG0(LF_CORDB, LL_INFO100, "Exiting function IterateAppDomainsForPdbs\n");
return hr;
}
/******************************************************************************
*
******************************************************************************/
HRESULT Debugger::InitAppDomainIPC(void)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
SO_INTOLERANT;
PRECONDITION(CheckPointer(m_pAppDomainCB));
}
CONTRACTL_END;
// Ensure that if we throw here, the Terminate will get called and cleanup all resources.
// This will make Init an atomic operation - it either fully inits or fully fails.
class EnsureCleanup
{
Debugger * m_pThis;
public:
EnsureCleanup(Debugger * pThis)
{
m_pThis = pThis;
}
void SupressCleanup()
{
m_pThis = NULL;
}
~EnsureCleanup()
{
if (m_pThis != NULL)
{
m_pThis->TerminateAppDomainIPC();
}
}
} hEnsureCleanup(this);
DWORD dwStrLen = 0;
WCHAR szExeName[MAX_PATH];
int i;
// all fields in the object can be zero initialized.
// If we throw, before fully initializing this, then cleanup won't try to free
// uninited values.
ZeroMemory(m_pAppDomainCB, sizeof(*m_pAppDomainCB));
// Fix for issue: whidbey 143061
// We are creating the mutex as hold, when we unlock, the EndThreadAffinity in
// hosting case will be unbalanced.
// Ideally, I would like to fix this by creating mutex not-held and call Lock method.
// This way, when we clean up the OOM, (as you can tell, we never release the mutex in
// some error cases), we can change it to holder class.
//
Thread::BeginThreadAffinity();
// Create a mutex to allow the Left and Right Sides to properly
// synchronize. The Right Side will spin until m_hMutex is valid,
// then it will acquire it before accessing the data.
HandleHolder hMutex(WszCreateMutex(NULL, TRUE/*hold*/, NULL));
if (hMutex == NULL)
{
ThrowLastError();
}
if (!m_pAppDomainCB->m_hMutex.SetLocal(hMutex))
{
ThrowLastError();
}
hMutex.SuppressRelease();
m_pAppDomainCB->m_iSizeInBytes = INITIAL_APP_DOMAIN_INFO_LIST_SIZE *
sizeof (AppDomainInfo);
// Number of slots in AppDomainListElement array
m_pAppDomainCB->m_rgListOfAppDomains = new AppDomainInfo[INITIAL_APP_DOMAIN_INFO_LIST_SIZE];
_ASSERTE(m_pAppDomainCB->m_rgListOfAppDomains != NULL); // throws on oom
m_pAppDomainCB->m_iTotalSlots = INITIAL_APP_DOMAIN_INFO_LIST_SIZE;
// Initialize each AppDomainListElement
for (i = 0; i < INITIAL_APP_DOMAIN_INFO_LIST_SIZE; i++)
{
m_pAppDomainCB->m_rgListOfAppDomains[i].FreeEntry();
}
// also initialize the process name
dwStrLen = WszGetModuleFileName(NULL,
szExeName,
MAX_PATH);
// If we couldn't get the name, then use a nice default.
if (dwStrLen == 0)
{
wcscpy_s(szExeName, COUNTOF(szExeName), W("<NoProcessName>"));
dwStrLen = (DWORD)wcslen(szExeName);
}
// If we got the name, copy it into a buffer. dwStrLen is the
// count of characters in the name, not including the null
// terminator.
m_pAppDomainCB->m_szProcessName = new WCHAR[dwStrLen + 1];
_ASSERTE(m_pAppDomainCB->m_szProcessName != NULL); // throws on oom
wcscpy_s(m_pAppDomainCB->m_szProcessName, dwStrLen + 1, szExeName);
// Add 1 to the string length so the Right Side will copy out the
// null terminator, too.
m_pAppDomainCB->m_iProcessNameLengthInBytes = (dwStrLen + 1) * sizeof(WCHAR);
if (m_pAppDomainCB->m_hMutex != NULL)
{
m_pAppDomainCB->Unlock();
}
hEnsureCleanup.SupressCleanup();
return S_OK;
}
/******************************************************************************
* Unitialize the AppDomain IPC block
* Returns:
* S_OK -if fully unitialized
* E_FAIL - if we can't get ownership of the block, and thus no unitialization
* work is done.
******************************************************************************/
HRESULT Debugger::TerminateAppDomainIPC(void)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_INTOLERANT;
}
CONTRACTL_END;
// If we have no AppDomain block, then we can consider it's already terminated.
if (m_pAppDomainCB == NULL)
return S_OK;
HRESULT hr = S_OK;
// Lock the list
// If there's no mutex, then we're in a partially created state.
// This means InitAppDomainIPC failed halfway through. But we're still thread safe
// since other threads can't access us if we don't have the mutex.
if ((m_pAppDomainCB->m_hMutex != NULL) && !m_pAppDomainCB->Lock())
{
// The callers don't check our return value, we may want to know when we can't gracefully clean up
LOG((LF_CORDB, LL_INFO10, "Debugger::TerminateAppDomainIPC: Failed to get AppDomain IPC lock, not cleaning up.\n"));
// If the lock is valid, but we can't get it, then we can't really
// uninitialize since someone else is using the block.
return (E_FAIL);
}
// The shared IPC segment could still be around after the debugger
// object has been destroyed during process shutdown. So, reset
// the UsedSlots count to 0 so that any out of process clients
// enumeratingthe app domains in this process see 0 AppDomains.
m_pAppDomainCB->m_iNumOfUsedSlots = 0;
m_pAppDomainCB->m_iTotalSlots = 0;
// Now delete the memory alloacted for AppDomainInfo array
delete [] m_pAppDomainCB->m_rgListOfAppDomains;
m_pAppDomainCB->m_rgListOfAppDomains = NULL;
delete [] m_pAppDomainCB->m_szProcessName;
m_pAppDomainCB->m_szProcessName = NULL;
m_pAppDomainCB->m_iProcessNameLengthInBytes = 0;
// Set the mutex handle to NULL.
// If the Right Side acquires the mutex, it will verify
// that the handle is still not NULL. If it is, then it knows it
// really lost.
RemoteHANDLE m = m_pAppDomainCB->m_hMutex;
m_pAppDomainCB->m_hMutex.m_hLocal = NULL;
// And bring us back to a fully unintialized state.
ZeroMemory(m_pAppDomainCB, sizeof(*m_pAppDomainCB));
// We're done. release and close the mutex. Note that this must be done
// after we clear it out above to ensure there is no race condition.
if( m != NULL )
{
VERIFY(ReleaseMutex(m));
m.Close();
}
return hr;
}
#ifndef DACCESS_COMPILE
//
// FuncEvalSetup sets up a function evaluation for the given method on the given thread.
//
HRESULT Debugger::FuncEvalSetup(DebuggerIPCE_FuncEvalInfo *pEvalInfo,
BYTE **argDataArea,
DebuggerEval **debuggerEvalKey)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_NOT_MAINLINE;
}
CONTRACTL_END;
Thread *pThread = pEvalInfo->vmThreadToken.GetRawPtr();
//
// If TS_AbortRequested (which may have been set by a pending FuncEvalAbort),
// we will not be able to do a new func-eval
//
// <TODO>@TODO: Remember the current value of m_State, reset m_State as appropriate,
// do the new func-eval, and then set m_State to the original value</TODO>
if (pThread->m_State & Thread::TS_AbortRequested)
return CORDBG_E_FUNC_EVAL_BAD_START_POINT;
if (g_fProcessDetach)
return CORDBG_E_FUNC_EVAL_BAD_START_POINT;
// If there is no guard page on this thread, then we've taken a stack overflow exception and can't run managed
// code on this thread. Therefore, we can't do a func eval on this thread.
if (!pThread->DetermineIfGuardPagePresent())
{
return CORDBG_E_ILLEGAL_IN_STACK_OVERFLOW;
}
bool fInException = pEvalInfo->evalDuringException;
// The thread has to be at a GC safe place for now, just in case the func eval causes a collection. Processing an
// exception also counts as a "safe place." Eventually, we'd like to have to avoid this check and eval anyway, but
// that's a way's off...
if (!fInException && !g_pDebugger->IsThreadAtSafePlace(pThread))
return CORDBG_E_ILLEGAL_AT_GC_UNSAFE_POINT;
// For now, we assume that the target thread must be stopped in managed code due to a single step or a
// breakpoint. Being stopped while sending a first or second chance exception is also valid, and there may or may
// not be a filter context when we do a func eval from such places. This will loosen over time, eventually allowing
// threads that are stopped anywhere in managed code to perform func evals.
CONTEXT *filterContext = GetManagedStoppedCtx(pThread);
if (filterContext == NULL && !fInException)
{
return CORDBG_E_ILLEGAL_AT_GC_UNSAFE_POINT;
}
// Create a DebuggerEval to hold info about this eval while its in progress. Constructor copies the thread's
// CONTEXT.
DebuggerEval *pDE = new (interopsafeEXEC, nothrow) DebuggerEval(filterContext, pEvalInfo, fInException);
if (pDE == NULL)
{
return E_OUTOFMEMORY;
}
else if (!pDE->Init())
{
// We fail to change the m_breakpointInstruction field to PAGE_EXECUTE_READWRITE permission.
return E_FAIL;
}
SIZE_T argDataAreaSize = 0;
argDataAreaSize += pEvalInfo->genericArgsNodeCount * sizeof(DebuggerIPCE_TypeArgData);
if ((pEvalInfo->funcEvalType == DB_IPCE_FET_NORMAL) ||
(pEvalInfo->funcEvalType == DB_IPCE_FET_NEW_OBJECT) ||
(pEvalInfo->funcEvalType == DB_IPCE_FET_NEW_OBJECT_NC))
argDataAreaSize += pEvalInfo->argCount * sizeof(DebuggerIPCE_FuncEvalArgData);
else if (pEvalInfo->funcEvalType == DB_IPCE_FET_NEW_STRING)
argDataAreaSize += pEvalInfo->stringSize;
else if (pEvalInfo->funcEvalType == DB_IPCE_FET_NEW_ARRAY)
argDataAreaSize += pEvalInfo->arrayRank * sizeof(SIZE_T);
if (argDataAreaSize > 0)
{
pDE->m_argData = new (interopsafe, nothrow) BYTE[argDataAreaSize];
if (pDE->m_argData == NULL)
{
DeleteInteropSafeExecutable(pDE);
return E_OUTOFMEMORY;
}
// Pass back the address of the argument data area so the right side can write to it for us.
*argDataArea = pDE->m_argData;
}
// Set the thread's IP (in the filter context) to our hijack function if we're stopped due to a breakpoint or single
// step.
if (!fInException)
{
_ASSERTE(filterContext != NULL);
::SetIP(filterContext, (UINT_PTR)GetEEFuncEntryPoint(::FuncEvalHijack));
// Don't be fooled into thinking you can push things onto the thread's stack now. If the thread is stopped at a
// breakpoint or from a single step, then its really suspended in the SEH filter. ESP in the thread's CONTEXT,
// therefore, points into the middle of the thread's current stack. So we pass things we need in the hijack in
// the thread's registers.
// Set the first argument to point to the DebuggerEval.
#if defined(_TARGET_X86_)
filterContext->Eax = (DWORD)pDE;
#elif defined(_TARGET_AMD64_)
filterContext->Rcx = (SIZE_T)pDE;
#elif defined(_TARGET_ARM_)
filterContext->R0 = (DWORD)pDE;
#else
PORTABILITY_ASSERT("Debugger::FuncEvalSetup is not implemented on this platform.");
#endif
//
// To prevent GCs until the func-eval gets a chance to run, we increment the counter here.
// We only need to do this if we have changed the filter CONTEXT, since the stack will be unwalkable
// in this case.
//
g_pDebugger->IncThreadsAtUnsafePlaces();
}
else
{
HRESULT hr = CheckInitPendingFuncEvalTable();
if (FAILED(hr))
{
DeleteInteropSafeExecutable(pDE); // Note this runs the destructor for DebuggerEval, which releases its internal buffers
return (hr);
}
// If we're in an exception, then add a pending eval for this thread. This will cause us to perform the func
// eval when the user continues the process after the current exception event.
GetPendingEvals()->AddPendingEval(pDE->m_thread, pDE);
}
// Return that all went well. Tracing the stack at this point should not show that the func eval is setup, but it
// will show a wrong IP, so it shouldn't be done.
*debuggerEvalKey = pDE;
LOG((LF_CORDB, LL_INFO100000, "D:FES for pDE:%08x evalType:%d on thread %#x, id=0x%x\n",
pDE, pDE->m_evalType, pThread, GetThreadIdHelper(pThread)));
return S_OK;
}
//
// FuncEvalSetupReAbort sets up a function evaluation specifically to rethrow a ThreadAbortException on the given
// thread.
//
HRESULT Debugger::FuncEvalSetupReAbort(Thread *pThread, Thread::ThreadAbortRequester requester)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_NOT_MAINLINE;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000,
"D::FESRA: performing reabort on thread %#x, id=0x%x\n",
pThread, GetThreadIdHelper(pThread)));
// The thread has to be at a GC safe place. It should be, since this is only done in response to a previous eval
// completing with a ThreadAbortException.
if (!g_pDebugger->IsThreadAtSafePlace(pThread))
return CORDBG_E_ILLEGAL_AT_GC_UNSAFE_POINT;
// Grab the filter context.
CONTEXT *filterContext = GetManagedStoppedCtx(pThread);
if (filterContext == NULL)
{
return CORDBG_E_ILLEGAL_AT_GC_UNSAFE_POINT;
}
// Create a DebuggerEval to hold info about this eval while its in progress. Constructor copies the thread's
// CONTEXT.
DebuggerEval *pDE = new (interopsafeEXEC, nothrow) DebuggerEval(filterContext, pThread, requester);
if (pDE == NULL)
{
return E_OUTOFMEMORY;
}
else if (!pDE->Init())
{
// We fail to change the m_breakpointInstruction field to PAGE_EXECUTE_READWRITE permission.
return E_FAIL;
}
// Set the thread's IP (in the filter context) to our hijack function.
_ASSERTE(filterContext != NULL);
::SetIP(filterContext, (UINT_PTR)GetEEFuncEntryPoint(::FuncEvalHijack));
#ifdef _TARGET_X86_ // reliance on filterContext->Eip & Eax
// Set EAX to point to the DebuggerEval.
filterContext->Eax = (DWORD)pDE;
#elif defined(_TARGET_AMD64_)
// Set RCX to point to the DebuggerEval.
filterContext->Rcx = (SIZE_T)pDE;
#elif defined(_TARGET_ARM_)
filterContext->R0 = (DWORD)pDE;
#else
PORTABILITY_ASSERT("FuncEvalSetupReAbort (Debugger.cpp) is not implemented on this platform.");
#endif
// Now clear the bit requesting a re-abort
pThread->ResetThreadStateNC(Thread::TSNC_DebuggerReAbort);
g_pDebugger->IncThreadsAtUnsafePlaces();
// Return that all went well. Tracing the stack at this point should not show that the func eval is setup, but it
// will show a wrong IP, so it shouldn't be done.
return S_OK;
}
//
// FuncEvalAbort: Does a gentle abort of a func-eval already in progress.
// Because this type of abort waits for the thread to get to a good state,
// it may never return, or may time out.
//
//
// Wait at most 0.5 seconds.
//
#define FUNC_EVAL_DEFAULT_TIMEOUT_VALUE 500
HRESULT
Debugger::FuncEvalAbort(
DebuggerEval *debuggerEvalKey
)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
}
CONTRACTL_END;
DebuggerEval *pDE = (DebuggerEval*) debuggerEvalKey;
HRESULT hr = S_OK;
CHECK_IF_CAN_TAKE_HELPER_LOCKS_IN_THIS_SCOPE(&hr, GetCanary());
if (FAILED(hr))
{
return hr;
}
if (pDE->m_aborting == DebuggerEval::FE_ABORT_NONE)
{
// Remember that we're aborting this func eval.
pDE->m_aborting = DebuggerEval::FE_ABORT_NORMAL;
LOG((LF_CORDB, LL_INFO1000,
"D::FEA: performing UserAbort on thread %#x, id=0x%x\n",
pDE->m_thread, GetThreadIdHelper(pDE->m_thread)));
if (!g_fProcessDetach && !pDE->m_completed)
{
//
// Perform a stop on the thread that the eval is running on.
// This will cause a ThreadAbortException to be thrown on the thread.
//
EX_TRY
{
hr = pDE->m_thread->UserAbort(Thread::TAR_FuncEval, EEPolicy::TA_Safe, (DWORD)FUNC_EVAL_DEFAULT_TIMEOUT_VALUE, Thread::UAC_Normal);
if (hr == HRESULT_FROM_WIN32(ERROR_TIMEOUT))
{
hr = S_OK;
}
}
EX_CATCH
{
_ASSERTE(!"Unknown exception from UserAbort(), not expected");
}
EX_END_CATCH(EX_RETHROW);
}
LOG((LF_CORDB, LL_INFO1000, "D::FEA: UserAbort complete.\n"));
}
return hr;
}
//
// FuncEvalRudeAbort: Does a rude abort of a func-eval in progress. This
// leaves the thread in an undetermined state.
//
HRESULT
Debugger::FuncEvalRudeAbort(
DebuggerEval *debuggerEvalKey
)
{
CONTRACTL
{
THROWS;
GC_NOTRIGGER;
SO_NOT_MAINLINE;
}
CONTRACTL_END;
HRESULT hr = S_OK;
CHECK_IF_CAN_TAKE_HELPER_LOCKS_IN_THIS_SCOPE(&hr, GetCanary());
if (FAILED(hr))
{
return hr;
}
DebuggerEval *pDE = debuggerEvalKey;
if (!(pDE->m_aborting & DebuggerEval::FE_ABORT_RUDE))
{
//
// Remember that we're aborting this func eval.
//
pDE->m_aborting = (DebuggerEval::FUNC_EVAL_ABORT_TYPE)(pDE->m_aborting | DebuggerEval::FE_ABORT_RUDE);
LOG((LF_CORDB, LL_INFO1000,
"D::FEA: performing RudeAbort on thread %#x, id=0x%x\n",
pDE->m_thread, Debugger::GetThreadIdHelper(pDE->m_thread)));
if (!g_fProcessDetach && !pDE->m_completed)
{
//
// Perform a stop on the thread that the eval is running on.
// This will cause a ThreadAbortException to be thrown on the thread.
//
EX_TRY
{
hr = pDE->m_thread->UserAbort(Thread::TAR_FuncEval, EEPolicy::TA_Rude, (DWORD)FUNC_EVAL_DEFAULT_TIMEOUT_VALUE, Thread::UAC_Normal);
if (hr == HRESULT_FROM_WIN32(ERROR_TIMEOUT))
{
hr = S_OK;
}
}
EX_CATCH
{
_ASSERTE(!"Unknown exception from UserAbort(), not expected");
EX_RETHROW;
}
EX_END_CATCH(RethrowTerminalExceptions);
}
LOG((LF_CORDB, LL_INFO1000, "D::FEA: RudeAbort complete.\n"));
}
return hr;
}
//
// FuncEvalCleanup cleans up after a function evaluation is released.
//
HRESULT Debugger::FuncEvalCleanup(DebuggerEval *debuggerEvalKey)
{
LIMITED_METHOD_CONTRACT;
DebuggerEval *pDE = debuggerEvalKey;
_ASSERTE(pDE->m_completed);
LOG((LF_CORDB, LL_INFO1000, "D::FEC: pDE:%08x 0x%08x, id=0x%x\n",
pDE, pDE->m_thread, GetThreadIdHelper(pDE->m_thread)));
DeleteInteropSafeExecutable(pDE);
return S_OK;
}
#endif // ifndef DACCESS_COMPILE
//
// SetReference sets an object reference for the Right Side,
// respecting the write barrier for references that are in the heap.
//
HRESULT Debugger::SetReference(void *objectRefAddress,
VMPTR_OBJECTHANDLE vmObjectHandle,
void *newReference)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
HRESULT hr = S_OK;
hr = ValidateObject((Object *)newReference);
if (FAILED(hr))
{
return hr;
}
// If the object ref isn't in a handle, then go ahead and use
// SetObjectReference.
if (vmObjectHandle.IsNull())
{
OBJECTREF *dst = (OBJECTREF*)objectRefAddress;
OBJECTREF src = *((OBJECTREF*)&newReference);
SetObjectReferenceUnchecked(dst, src);
}
else
{
// If the object reference to set is inside of a handle, then
// fixup the handle.
OBJECTHANDLE h = vmObjectHandle.GetRawPtr();
OBJECTREF src = *((OBJECTREF*)&newReference);
HndAssignHandle(h, src);
}
return S_OK;
}
//
// SetValueClass sets a value class for the Right Side, respecting the write barrier for references that are embedded
// within in the value class.
//
HRESULT Debugger::SetValueClass(void *oldData, void *newData, DebuggerIPCE_BasicTypeData * type)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
HRESULT hr = S_OK;
TypeHandle th;
hr = BasicTypeInfoToTypeHandle(type, &th);
if (FAILED(hr))
return CORDBG_E_CLASS_NOT_LOADED;
// Update the value class.
CopyValueClassUnchecked(oldData, newData, th.GetMethodTable());
// Free the buffer that is holding the new data. This is a buffer that was created in response to a GET_BUFFER
// message, so we release it with ReleaseRemoteBuffer.
ReleaseRemoteBuffer((BYTE*)newData, true);
return hr;
}
/******************************************************************************
*
******************************************************************************/
HRESULT Debugger::SetILInstrumentedCodeMap(MethodDesc *fd,
BOOL fStartJit,
ULONG32 cILMapEntries,
COR_IL_MAP rgILMapEntries[])
{
CONTRACTL
{
THROWS;
GC_TRIGGERS_FROM_GETJITINFO;
}
CONTRACTL_END;
if (!HasLazyData())
{
DebuggerLockHolder dbgLockHolder(this);
// This is an entry path into the debugger, so make sure we're inited.
LazyInit();
}
DebuggerMethodInfo * dmi = GetOrCreateMethodInfo(fd->GetModule(), fd->GetMemberDef());
if (dmi == NULL)
{
return E_OUTOFMEMORY;
}
dmi->SetInstrumentedILMap(rgILMapEntries, cILMapEntries);
return S_OK;
}
//
// EarlyHelperThreadDeath handles the case where the helper
// thread has been ripped out from underneath of us by
// ExitProcess or TerminateProcess. These calls are bad, whacking
// all threads except the caller in the process. This can happen, for
// instance, when an app calls ExitProcess. All threads are wacked,
// the main thread calls all DLL main's, and the EE starts shutting
// down in its DLL main with the helper thread terminated.
//
void Debugger::EarlyHelperThreadDeath(void)
{
WRAPPER_NO_CONTRACT;
if (m_pRCThread)
m_pRCThread->EarlyHelperThreadDeath();
}
//
// This tells the debugger that shutdown of the in-proc debugging services has begun. We need to know this during
// managed/unmanaged debugging so we can stop doing certian things to the process (like hijacking threads.)
//
void Debugger::ShutdownBegun(void)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_INTOLERANT;
}
CONTRACTL_END;
// Shouldn't be Debugger-stopped if we're shutting down.
// However, shutdown can occur in preemptive mode. Thus if the RS does an AsyncBreak late
// enough, then the LS will appear to be stopped but may still shutdown.
// Since the debuggee can exit asynchronously at any time (eg, suppose somebody forcefully
// kills it with taskman), this doesn't introduce a new case.
// That aside, it would be great to be able to assert this:
//_ASSERTE(!IsStopped());
if (m_pRCThread != NULL)
{
DebuggerIPCControlBlock *dcb = m_pRCThread->GetDCB();
if ((dcb != NULL) && (dcb->m_rightSideIsWin32Debugger))
dcb->m_shutdownBegun = true;
}
}
/*
* LockDebuggerForShutdown
*
* This routine is used during shutdown to tell the in-process portion of the
* debugger to synchronize with any threads that are currently using the
* debugging facilities such that no more threads will run debugging services.
*
* This is accomplished by transitioning the debugger lock in to a state where
* it will block all threads, except for the finalizer, shutdown, and helper thread.
*/
void Debugger::LockDebuggerForShutdown(void)
{
#ifndef DACCESS_COMPILE
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_INTOLERANT;
MODE_ANY;
}
CONTRACTL_END;
DebuggerLockHolder dbgLockHolder(this);
// Shouldn't be Debugger-stopped if we're shutting down.
// However, shutdown can occur in preemptive mode. Thus if the RS does an AsyncBreak late
// enough, then the LS will appear to be stopped but may still shutdown.
// Since the debuggee can exit asynchronously at any time (eg, suppose somebody forcefully
// kills it with taskman), this doesn't introduce a new case.
// That aside, it would be great to be able to assert this:
//_ASSERTE(!IsStopped());
// After setting this flag, nonspecial threads will not be able to
// take the debugger lock.
m_fShutdownMode = true;
m_ignoreThreadDetach = TRUE;
#else
DacNotImpl();
#endif
}
/*
* DisableDebugger
*
* This routine is used by the EE to inform the debugger that it should block all
* threads from executing as soon as it can. Any thread entering the debugger can
* block infinitely, as well.
*
* This is accomplished by transitioning the debugger lock into a mode where it will
* block all threads infinitely rather than taking the lock.
*
*/
void Debugger::DisableDebugger(void)
{
#ifndef DACCESS_COMPILE
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_INTOLERANT;
PRECONDITION(ThisMaybeHelperThread());
}
CONTRACTL_END;
m_fDisabled = true;
CORDBDebuggerSetUnrecoverableError(this, CORDBG_E_DEBUGGING_DISABLED, false);
#else
DacNotImpl();
#endif
}
/****************************************************************************
* This will perform the duties of the helper thread if none already exists.
* This is called in the case that the loader lock is held and so no new
* threads can be spun up to be the helper thread, so the existing thread
* must be the helper thread until a new one can spin up.
* This is also called in the shutdown case (g_fProcessDetach==true) and our
* helper may have already been blown away.
***************************************************************************/
void Debugger::DoHelperThreadDuty()
{
CONTRACTL
{
SO_NOT_MAINLINE;
THROWS;
WRAPPER(GC_TRIGGERS);
}
CONTRACTL_END;
// This should not be a real helper thread.
_ASSERTE(!IsDbgHelperSpecialThread());
_ASSERTE(ThreadHoldsLock());
// We may be here in the shutdown case (only if the shutdown started after we got here).
// We'll get killed randomly anyways, so not much we can do.
// These assumptions are based off us being called from TART.
_ASSERTE(ThreadStore::HoldingThreadStore() || g_fProcessDetach); // got this from TART
_ASSERTE(m_trappingRuntimeThreads); // We're only called from TART.
_ASSERTE(!m_stopped); // we haven't sent the sync-complete yet.
// Can't have 2 threads doing helper duty.
_ASSERTE(m_pRCThread->GetDCB()->m_temporaryHelperThreadId == 0);
LOG((LF_CORDB, LL_INFO1000,
"D::SSCIPCE: helper thread is not ready, doing helper "
"thread duty...\n"));
// We're the temporary helper thread now.
DWORD dwMyTID = GetCurrentThreadId();
m_pRCThread->GetDCB()->m_temporaryHelperThreadId = dwMyTID;
// Make sure the helper thread has something to wait on while
// we're trying to be the helper thread.
VERIFY(ResetEvent(m_pRCThread->GetHelperThreadCanGoEvent()));
// We have not sent the sync-complete flare yet.
// Now that we've synchronized, we'll eventually send the sync-complete. But we're currently within the
// scope of sombody already sending an event. So unlock from that event so that we can send the sync-complete.
// Don't release the debugger lock
//
UnlockFromEventSending(NULL);
// We are the temporary helper thread. We will not deal with everything! But just pump for
// continue.
//
m_pRCThread->TemporaryHelperThreadMainLoop();
// We do not need to relock it since we never release it.
LockForEventSending(NULL);
_ASSERTE(ThreadHoldsLock());
STRESS_LOG1(LF_CORDB, LL_INFO1000,
"D::SSCIPCE: done doing helper thread duty. "
"Current helper thread id=0x%x\n",
m_pRCThread->GetDCB()->m_helperThreadId);
// We're not the temporary helper thread anymore.
_ASSERTE(m_pRCThread->GetDCB()->m_temporaryHelperThreadId == dwMyTID);
m_pRCThread->GetDCB()->m_temporaryHelperThreadId = 0;
// Let the helper thread go if its waiting on us.
VERIFY(SetEvent(m_pRCThread->GetHelperThreadCanGoEvent()));
}
// This function is called from the EE to notify the right side
// whenever the name of a thread or AppDomain changes
//
// Notes:
// This just sends a ping event to notify that the name has been changed.
// It does not send the actual updated name. Instead, the debugger can query for the name.
//
// For an AppDomain name change:
// - pAppDoamin != NULL
// - name retrieved via ICorDebugAppDomain::GetName
//
// For a Thread name change:
// - pAppDomain == NULL, pThread != NULL
// - name retrieved via a func-eval of Thread::get_Name
HRESULT Debugger::NameChangeEvent(AppDomain *pAppDomain, Thread *pThread)
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
// Don't try to send one of these if the thread really isn't setup
// yet. This can happen when initially setting up an app domain,
// before the appdomain create event has been sent. Since the app
// domain create event hasn't been sent yet in this case, its okay
// to do this...
if (g_pEEInterface->GetThread() == NULL)
return S_OK;
// Skip if thread doesn't yet have native ID.
// This can easily happen if an app sets Thread.Name before it calls Thread.Start.
// Since this is just a ping-event, it's ignorable. The debugger can query the thread name at Thread.Start in this case.
// This emulates whidbey semantics.
if (pThread != NULL)
{
if (pThread->GetOSThreadId() == 0)
{
return S_OK;
}
}
LOG((LF_CORDB, LL_INFO1000, "D::NCE: Sending NameChangeEvent 0x%x 0x%x\n",
pAppDomain, pThread));
Thread *curThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, curThread);
if (CORDebuggerAttached())
{
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_NAME_CHANGE,
curThread,
curThread->GetDomain());
if (pAppDomain)
{
ipce->NameChange.eventType = APP_DOMAIN_NAME_CHANGE;
ipce->NameChange.vmAppDomain.SetRawPtr(pAppDomain);
}
else
{
// Thread Name
ipce->NameChange.eventType = THREAD_NAME_CHANGE;
_ASSERTE (pThread);
ipce->NameChange.vmThread.SetRawPtr(pThread);
}
m_pRCThread->SendIPCEvent();
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::NCE: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
return S_OK;
}
//---------------------------------------------------------------------------------------
//
// Send an event to the RS indicating that there's a Ctrl-C or Ctrl-Break.
//
// Arguments:
// dwCtrlType - represents the type of the event (Ctrl-C or Ctrl-Break)
//
// Return Value:
// Return TRUE if the event has been handled by the debugger.
//
BOOL Debugger::SendCtrlCToDebugger(DWORD dwCtrlType)
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
LOG((LF_CORDB, LL_INFO1000, "D::SCCTD: Sending CtrlC Event 0x%x\n", dwCtrlType));
// Prevent other Runtime threads from handling events.
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
if (CORDebuggerAttached())
{
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce,
DB_IPCE_CONTROL_C_EVENT,
pThread,
NULL);
// The RS doesn't do anything with dwCtrlType
m_pRCThread->SendIPCEvent();
// Stop all Runtime threads
TrapAllRuntimeThreads();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::SCCTD: Skipping SendIPCEvent because RS detached."));
}
SENDIPCEVENT_END;
// now wait for notification from the right side about whether or not
// the out-of-proc debugger is handling ControlC events.
WaitForSingleObjectHelper(GetCtrlCMutex(), INFINITE);
return GetDebuggerHandlingCtrlC();
}
// Allows the debugger to keep an up to date list of special threads
HRESULT Debugger::UpdateSpecialThreadList(DWORD cThreadArrayLength,
DWORD *rgdwThreadIDArray)
{
LIMITED_METHOD_CONTRACT;
_ASSERTE(g_pRCThread != NULL);
DebuggerIPCControlBlock *pIPC = g_pRCThread->GetDCB();
_ASSERTE(pIPC);
if (!pIPC)
return (E_FAIL);
// Save the thread list information, and mark the dirty bit so
// the right side knows.
pIPC->m_specialThreadList = rgdwThreadIDArray;
pIPC->m_specialThreadListLength = cThreadArrayLength;
pIPC->m_specialThreadListDirty = true;
return (S_OK);
}
// Updates the pointer for the debugger services
void Debugger::SetIDbgThreadControl(IDebuggerThreadControl *pIDbgThreadControl)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
if (m_pIDbgThreadControl)
m_pIDbgThreadControl->Release();
m_pIDbgThreadControl = pIDbgThreadControl;
if (m_pIDbgThreadControl)
m_pIDbgThreadControl->AddRef();
}
//
// If a thread is Win32 suspended right after hitting a breakpoint instruction, but before the OS has transitioned the
// thread over to the user-level exception dispatching logic, then we may see the IP pointing after the breakpoint
// instruction. There are times when the Runtime will use the IP to try to determine what code as run in the prolog or
// epilog, most notably when unwinding a frame. If the thread is suspended in such a case, then the unwind will believe
// that the instruction that the breakpoint replaced has really been executed, which is not true. This confuses the
// unwinding logic. This function is called from Thread::HandledJITCase() to help us recgonize when this may have
// happened and allow us to skip the unwind and abort the HandledJITCase.
//
// The criteria is this:
//
// 1) If a debugger is attached.
//
// 2) If the instruction before the IP is a breakpoint instruction.
//
// 3) If the IP is in the prolog or epilog of a managed function.
//
BOOL Debugger::IsThreadContextInvalid(Thread *pThread)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
BOOL invalid = FALSE;
// Get the thread context.
CONTEXT ctx;
ctx.ContextFlags = CONTEXT_CONTROL;
BOOL success = pThread->GetThreadContext(&ctx);
if (success)
{
// Check single-step flag
if (IsSSFlagEnabled(reinterpret_cast<DT_CONTEXT *>(&ctx) ARM_ARG(pThread)))
{
// Can't hijack a thread whose SS-flag is set. This could lead to races
// with the thread taking the SS-exception.
// The debugger's controller filters will poll for GC to avoid starvation.
STRESS_LOG0(LF_CORDB, LL_EVERYTHING, "HJC - Hardware trace flag applied\n");
return TRUE;
}
}
if (success)
{
#ifdef _TARGET_X86_
// Grab Eip - 1
LPVOID address = (((BYTE*)GetIP(&ctx)) - 1);
EX_TRY
{
// Use AVInRuntimeImplOkHolder.
AVInRuntimeImplOkayHolder AVOkay;
// Is it a breakpoint?
if (AddressIsBreakpoint((CORDB_ADDRESS_TYPE*)address))
{
size_t prologSize; // Unused...
if (g_pEEInterface->IsInPrologOrEpilog((BYTE*)GetIP(&ctx), &prologSize))
{
LOG((LF_CORDB, LL_INFO1000, "D::ITCI: thread is after a BP and in prolog or epilog.\n"));
invalid = TRUE;
}
}
}
EX_CATCH
{
// If we fault trying to read the byte before EIP, then we know that its not a breakpoint.
// Do nothing. The default return value is FALSE.
}
EX_END_CATCH(SwallowAllExceptions);
#else // _TARGET_X86_
// Non-x86 can detect whether the thread is suspended after an exception is hit but before
// the kernel has dispatched the exception to user mode by trap frame reporting.
// See Thread::IsContextSafeToRedirect().
#endif // _TARGET_X86_
}
else
{
// If we can't get the context, then its definetly invalid... ;)
LOG((LF_CORDB, LL_INFO1000, "D::ITCI: couldn't get thread's context!\n"));
invalid = TRUE;
}
return invalid;
}
// notification when a SQL connection begins
void Debugger::CreateConnection(CONNID dwConnectionId, __in_z WCHAR *wzName)
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO1000, "D::CreateConnection %d\n.", dwConnectionId));
if (CORDBUnrecoverableError(this))
return;
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
if (CORDebuggerAttached())
{
DebuggerIPCEvent* ipce;
// Send a update module syns event to the Right Side.
ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_CREATE_CONNECTION,
pThread,
NULL);
ipce->CreateConnection.connectionId = dwConnectionId;
_ASSERTE(wzName != NULL);
ipce->CreateConnection.wzConnectionName.SetString(wzName);
m_pRCThread->SendIPCEvent();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::CreateConnection: Skipping SendIPCEvent because RS detached."));
}
// Stop all Runtime threads if we actually sent an event
if (CORDebuggerAttached())
{
TrapAllRuntimeThreads();
}
SENDIPCEVENT_END;
}
// notification when a SQL connection ends
void Debugger::DestroyConnection(CONNID dwConnectionId)
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO1000, "D::DestroyConnection %d\n.", dwConnectionId));
if (CORDBUnrecoverableError(this))
return;
Thread *thread = g_pEEInterface->GetThread();
// Note that the debugger lock is reentrant, so we may or may not hold it already.
SENDIPCEVENT_BEGIN(this, thread);
// Send a update module syns event to the Right Side.
DebuggerIPCEvent* ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_DESTROY_CONNECTION,
thread,
NULL);
ipce->ConnectionChange.connectionId = dwConnectionId;
// IPC event is now initialized, so we can send it over.
SendSimpleIPCEventAndBlock();
// This will block on the continue
SENDIPCEVENT_END;
}
// notification for SQL connection changes
void Debugger::ChangeConnection(CONNID dwConnectionId)
{
CONTRACTL
{
SO_NOT_MAINLINE;
MAY_DO_HELPER_THREAD_DUTY_THROWS_CONTRACT;
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
}
CONTRACTL_END;
LOG((LF_CORDB,LL_INFO1000, "D::ChangeConnection %d\n.", dwConnectionId));
if (CORDBUnrecoverableError(this))
return;
Thread *pThread = g_pEEInterface->GetThread();
SENDIPCEVENT_BEGIN(this, pThread);
if (CORDebuggerAttached())
{
DebuggerIPCEvent* ipce;
// Send a update module syns event to the Right Side.
ipce = m_pRCThread->GetIPCEventSendBuffer();
InitIPCEvent(ipce, DB_IPCE_CHANGE_CONNECTION,
pThread,
NULL);
ipce->ConnectionChange.connectionId = dwConnectionId;
m_pRCThread->SendIPCEvent();
}
else
{
LOG((LF_CORDB,LL_INFO1000, "D::ChangeConnection: Skipping SendIPCEvent because RS detached."));
}
// Stop all Runtime threads if we actually sent an event
if (CORDebuggerAttached())
{
TrapAllRuntimeThreads();
}
SENDIPCEVENT_END;
}
//
// Are we the helper thread?
// Some important things about running on the helper thread:
// - there's only 1, so guaranteed to be thread-safe.
// - we'll never run managed code.
// - therefore, Never GC.
// - It listens for events from the RS.
// - It's the only thread to send a sync complete.
//
bool ThisIsHelperThreadWorker(void)
{
CONTRACTL
{
NOTHROW;
GC_NOTRIGGER;
SO_TOLERANT;
}
CONTRACTL_END;
// This can
Thread * pThread;
pThread = GetThreadNULLOk();
// First check for a real helper thread. This will do a FLS access.
bool fIsHelperThread = !!IsDbgHelperSpecialThread();
if (fIsHelperThread)
{
// If we're on the real helper thread, we never run managed code
// and so we'd better not have an EE thread object.
_ASSERTE((pThread == NULL) || !"The helper thread should not being running managed code.\n"
"Are you running managed code inside the dllmain? If so, your scenario is invalid and this"
"assert is only the tip of the iceberg.\n");
return true;
}
// Even if we're not on the real helper thread, we may still be on a thread
// pretending to be the helper. (Helper Duty, etc).
DWORD id = GetCurrentThreadId();
// Check for temporary helper thread.
if (ThisIsTempHelperThread(id))
{
return true;
}
return false;
}
//
// Make call to the static method.
// This is exposed to the contracts susbsystem so that the helper thread can call
// things on MODE_COOPERATIVE.
//
bool Debugger::ThisIsHelperThread(void)
{
WRAPPER_NO_CONTRACT;
return ThisIsHelperThreadWorker();
}
// Check if we're the temporary helper thread. Have 2 forms of this, 1 that assumes the current
// thread (but has the overhead of an extra call to GetCurrentThreadId() if we laready know the tid.
bool ThisIsTempHelperThread()
{
WRAPPER_NO_CONTRACT;
DWORD id = GetCurrentThreadId();
return ThisIsTempHelperThread(id);
}
bool ThisIsTempHelperThread(DWORD tid)
{
WRAPPER_NO_CONTRACT;
// If helper thread class isn't created, then there's no helper thread.
// No one is doing helper thread duty either.
// It's also possible we're in a shutdown case and have already deleted the
// data for the helper thread.
if (g_pRCThread != NULL)
{
// May be the temporary helper thread...
DebuggerIPCControlBlock * pBlock = g_pRCThread->GetDCB();
if (pBlock != NULL)
{
DWORD idTemp = pBlock->m_temporaryHelperThreadId;
if (tid == idTemp)
{
return true;
}
}
}
return false;
}
// This function is called when host call ICLRSecurityAttributeManager::setDacl.
// It will redacl our SSE, RSEA, RSER events.
HRESULT Debugger::ReDaclEvents(PSECURITY_DESCRIPTOR securityDescriptor)
{
WRAPPER_NO_CONTRACT;
return m_pRCThread->ReDaclEvents(securityDescriptor);
}
/* static */
void Debugger::AcquireDebuggerDataLock(Debugger *pDebugger)
{
WRAPPER_NO_CONTRACT;
if (!g_fProcessDetach)
{
pDebugger->GetDebuggerDataLock()->Enter();
}
}
/* static */
void Debugger::ReleaseDebuggerDataLock(Debugger *pDebugger)
{
WRAPPER_NO_CONTRACT;
if (!g_fProcessDetach)
{
pDebugger->GetDebuggerDataLock()->Leave();
}
}
#else // DACCESS_COMPILE
// determine whether the LS holds the data lock. If it does, we will assume the locked data is in an
// inconsistent state and will throw an exception. The DAC will execute this if we are executing code
// that takes the lock.
// Arguments: input: pDebugger - the LS debugger data structure
/* static */
void Debugger::AcquireDebuggerDataLock(Debugger *pDebugger)
{
SUPPORTS_DAC;
if (pDebugger->GetDebuggerDataLock()->GetEnterCount() != 0)
{
ThrowHR(CORDBG_E_PROCESS_NOT_SYNCHRONIZED);
}
}
#endif // DACCESS_COMPILE
/* ------------------------------------------------------------------------ *
* DebuggerHeap impl
* ------------------------------------------------------------------------ */
DebuggerHeap::DebuggerHeap()
{
#ifdef USE_INTEROPSAFE_HEAP
m_hHeap = NULL;
#endif
}
DebuggerHeap::~DebuggerHeap()
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
Destroy();
}
void DebuggerHeap::Destroy()
{
#ifdef USE_INTEROPSAFE_HEAP
if (IsInit())
{
::HeapDestroy(m_hHeap);
m_hHeap = NULL;
}
#endif
}
bool DebuggerHeap::IsInit()
{
LIMITED_METHOD_CONTRACT;
#ifdef USE_INTEROPSAFE_HEAP
return m_hHeap != NULL;
#else
return true;
#endif
}
HRESULT DebuggerHeap::Init(BOOL fExecutable)
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
// Have knob catch if we don't want to lazy init the debugger.
_ASSERTE(!g_DbgShouldntUseDebugger);
#ifdef USE_INTEROPSAFE_HEAP
// If already inited, then we're done.
// We normally don't double-init. However, we may oom between when we allocate the heap and when we do other initialization.
// We don't worry about backout code to free the heap. Rather, we'll just leave it alive and nop if we try to allocate it again.
if (IsInit())
{
return S_OK;
}
#ifndef HEAP_CREATE_ENABLE_EXECUTE
#define HEAP_CREATE_ENABLE_EXECUTE 0x00040000 // winnt create heap with executable pages
#endif
// Create a standard, grow-able, thread-safe heap.
DWORD dwFlags = ((fExecutable == TRUE)? HEAP_CREATE_ENABLE_EXECUTE : 0);
m_hHeap = ::HeapCreate(dwFlags, 0, 0);
if (m_hHeap == NULL)
{
return HRESULT_FROM_GetLastError();
}
#endif
return S_OK;
}
// Only use canaries on x86 b/c they throw of alignment on Ia64.
#if defined(_DEBUG) && defined(_TARGET_X86_)
#define USE_INTEROPSAFE_CANARY
#endif
#ifdef USE_INTEROPSAFE_CANARY
// Small header to to prefix interop-heap blocks.
// This lets us enforce that we don't delete interopheap data from a non-interop heap.
struct InteropHeapCanary
{
ULONGLONG m_canary;
// Raw address - this is what the heap alloc + free routines use.
// User address - this is what the user sees after we adjust the raw address for the canary
// Given a raw address to an allocated block, get the canary + mark it.
static InteropHeapCanary * GetFromRawAddr(void * pStart)
{
_ASSERTE(pStart != NULL);
InteropHeapCanary * p = (InteropHeapCanary*) pStart;
p->Mark();
return p;
}
// Get the raw address from this canary.
void * GetRawAddr()
{
return (void*) this;
}
// Get a canary from a start address.
static InteropHeapCanary * GetFromUserAddr(void * pStart)
{
_ASSERTE(pStart != NULL);
InteropHeapCanary * p = ((InteropHeapCanary*) pStart)-1;
p->Check();
return p;
}
void * GetUserAddr()
{
this->Check();
return (void*) (this + 1);
}
protected:
void Check()
{
CONSISTENCY_CHECK_MSGF((m_canary == kInteropHeapCookie),
("Using InteropSafe delete on non-interopsafe allocated memory.\n"));
}
void Mark()
{
m_canary = kInteropHeapCookie;
}
static const ULONGLONG kInteropHeapCookie = 0x12345678;
};
#endif // USE_INTEROPSAFE_CANARY
void *DebuggerHeap::Alloc(DWORD size)
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
#ifdef USE_INTEROPSAFE_CANARY
// Make sure we allocate enough space for the canary at the start.
size += sizeof(InteropHeapCanary);
#endif
void *ret;
#ifdef USE_INTEROPSAFE_HEAP
_ASSERTE(m_hHeap != NULL);
ret = ::HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY, size);
#else // USE_INTEROPSAFE_HEAP
#ifndef FEATURE_PAL
HANDLE hExecutableHeap = ClrGetProcessExecutableHeap();
#else // !FEATURE_PAL
HANDLE hExecutableHeap = ClrGetProcessHeap();
#endif // !FEATURE_PAL
if (hExecutableHeap == NULL)
{
return NULL;
}
ret = ClrHeapAlloc(hExecutableHeap, NULL, S_SIZE_T(size));
#endif // USE_INTEROPSAFE_HEAP
#ifdef USE_INTEROPSAFE_CANARY
if (ret == NULL)
{
return NULL;
}
InteropHeapCanary * pCanary = InteropHeapCanary::GetFromRawAddr(ret);
ret = pCanary->GetUserAddr();
#endif
return ret;
}
// Realloc memory.
// If this fails, the original memory is still valid.
void *DebuggerHeap::Realloc(void *pMem, DWORD newSize, DWORD oldSize)
{
CONTRACTL
{
SO_NOT_MAINLINE;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
_ASSERTE(pMem != NULL);
_ASSERTE(newSize != 0);
_ASSERTE(oldSize != 0);
#if defined(USE_INTEROPSAFE_HEAP) && !defined(USE_INTEROPSAFE_CANARY)
// No canaries in this case.
// Call into realloc.
void *ret;
_ASSERTE(m_hHeap != NULL);
ret = ::HeapReAlloc(m_hHeap, HEAP_ZERO_MEMORY, pMem, newSize);
#else
// impl Realloc on top of alloc & free.
void *ret;
ret = this->Alloc(newSize);
if (ret == NULL)
{
// Not supposed to free original memory in failure condition.
return NULL;
}
memcpy(ret, pMem, oldSize);
this->Free(pMem);
#endif
return ret;
}
void DebuggerHeap::Free(void *pMem)
{
CONTRACTL
{
SO_INTOLERANT;
NOTHROW;
GC_NOTRIGGER;
}
CONTRACTL_END;
#ifdef USE_INTEROPSAFE_CANARY
// Check for canary
if (pMem != NULL)
{
InteropHeapCanary * pCanary = InteropHeapCanary::GetFromUserAddr(pMem);
pMem = pCanary->GetRawAddr();
}
#endif
#ifdef USE_INTEROPSAFE_HEAP
if (pMem != NULL)
{
_ASSERTE(m_hHeap != NULL);
::HeapFree(m_hHeap, 0, pMem);
}
#else
if (pMem != NULL)
{
HANDLE hExecutableHeap = ClrGetProcessExecutableHeap();
_ASSERTE(hExecutableHeap != NULL);
ClrHeapFree(hExecutableHeap, NULL, pMem);
}
#endif
}
#ifndef DACCESS_COMPILE
// Undef this so we can call them from the EE versions.
#undef UtilMessageBoxVA
// Message box API for the left side of the debugger. This API handles calls from the
// debugger helper thread as well as from normal EE threads. It is the only one that
// should be used from inside the debugger left side.
int Debugger::MessageBox(
UINT uText, // Resource Identifier for Text message
UINT uCaption, // Resource Identifier for Caption
UINT uType, // Style of MessageBox
BOOL displayForNonInteractive, // Display even if the process is running non interactive
BOOL showFileNameInTitle, // Flag to show FileName in Caption
...) // Additional Arguments
{
CONTRACTL
{
MAY_DO_HELPER_THREAD_DUTY_GC_TRIGGERS_CONTRACT;
MODE_PREEMPTIVE;
NOTHROW;
PRECONDITION(ThisMaybeHelperThread());
}
CONTRACTL_END;
va_list marker;
va_start(marker, showFileNameInTitle);
// Add the MB_TASKMODAL style to indicate that the dialog should be displayed on top of the windows
// owned by the current thread and should prevent interaction with them until dismissed.
uType |= MB_TASKMODAL;
int result = UtilMessageBoxVA(NULL, uText, uCaption, uType, displayForNonInteractive, showFileNameInTitle, marker);
va_end( marker );
return result;
}
// Redefine this to an error just in case code is added after this point in the file.
#define UtilMessageBoxVA __error("Use g_pDebugger->MessageBox from inside the left side of the debugger")
#else // DACCESS_COMPILE
void
Debugger::EnumMemoryRegions(CLRDataEnumMemoryFlags flags)
{
DAC_ENUM_VTHIS();
SUPPORTS_DAC;
if ( flags != CLRDATA_ENUM_MEM_TRIAGE)
{
if (m_pMethodInfos.IsValid())
{
m_pMethodInfos->EnumMemoryRegions(flags);
}
DacEnumMemoryRegion(dac_cast<TADDR>(m_pLazyData),
sizeof(DebuggerLazyInit));
}
// Needed for stack walking from an initial native context. If the debugger can find the
// on-disk image of clr.dll, then this is not necessary.
DacEnumMemoryRegion(dac_cast<TADDR>(m_rgHijackFunction), sizeof(MemoryRange)*kMaxHijackFunctions);
}
// This code doesn't hang out in Frame/TransitionFrame/FuncEvalFrame::EnumMemoryRegions() like it would
// for other normal VM objects because we don't want to have code in VM directly referencing LS types.
// Frames.h's FuncEvalFrame simply does a forward decl of DebuggerEval and gets away with it because it
// never does anything but a cast of a TADDR.
void
Debugger::EnumMemoryRegionsIfFuncEvalFrame(CLRDataEnumMemoryFlags flags, Frame * pFrame)
{
SUPPORTS_DAC;
if ((pFrame != NULL) && (pFrame->GetFrameType() == Frame::TYPE_FUNC_EVAL))
{
FuncEvalFrame * pFEF = dac_cast<PTR_FuncEvalFrame>(pFrame);
DebuggerEval * pDE = pFEF->GetDebuggerEval();
if (pDE != NULL)
{
DacEnumMemoryRegion(dac_cast<TADDR>(pDE), sizeof(DebuggerEval), true);
if (pDE->m_debuggerModule != NULL)
DacEnumMemoryRegion(dac_cast<TADDR>(pDE->m_debuggerModule), sizeof(DebuggerModule), true);
}
}
}
#endif // #ifdef DACCESS_COMPILE
#ifndef DACCESS_COMPILE
void Debugger::StartCanaryThread()
{
// we need to already have the rcthread running and the pointer stored
_ASSERTE(m_pRCThread != NULL && g_pRCThread == m_pRCThread);
_ASSERTE(m_pRCThread->GetDCB() != NULL);
_ASSERTE(GetCanary() != NULL);
GetCanary()->Init();
}
#endif // DACCESS_COMPILE
#endif //DEBUGGING_SUPPORTED
|