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

// ==++==
//

//
// ==--==
// ****************************************************************************
// File: controller.cpp
// 

//
// controller.cpp: Debugger execution control routines
//
// ****************************************************************************
// Putting code & #includes, #defines, etc, before the stdafx.h will
// cause the code,etc, to be silently ignored
#include "stdafx.h"
#include "openum.h"
#include "../inc/common.h"
#include "eeconfig.h"

#include "../../vm/methoditer.h"

const char *GetTType( TraceType tt);

#define IsSingleStep(exception) (exception == EXCEPTION_SINGLE_STEP)





// -------------------------------------------------------------------------
//  DebuggerController routines
// -------------------------------------------------------------------------

SPTR_IMPL_INIT(DebuggerPatchTable, DebuggerController, g_patches, NULL);
SVAL_IMPL_INIT(BOOL, DebuggerController, g_patchTableValid, FALSE);

#if !defined(DACCESS_COMPILE)

DebuggerController             *DebuggerController::g_controllers = NULL;
DebuggerControllerPage         *DebuggerController::g_protections = NULL;
CrstStatic                      DebuggerController::g_criticalSection;
int                             DebuggerController::g_cTotalMethodEnter = 0;


// Is this patch at a position at which it's safe to take a stack?
bool DebuggerControllerPatch::IsSafeForStackTrace()
{
    LIMITED_METHOD_CONTRACT;

    TraceType tt = this->trace.GetTraceType();
    Module *module = this->key.module;
    BOOL managed = this->IsManagedPatch();

    // Patches placed by MgrPush can come at lots of illegal spots. Can't take a stack trace here.
    if ((module == NULL) && managed && (tt == TRACE_MGR_PUSH))
    {
        return false;
    }

    // Consider everything else legal.
    // This is a little shady for TRACE_FRAME_PUSH. But TraceFrame() needs a stackInfo
    // to get a RegDisplay (though almost nobody uses it, so perhaps it could be removed).
    return true;

}

#ifndef FEATURE_EMULATE_SINGLESTEP
// returns a pointer to the shared buffer.  each call will AddRef() the object
// before returning it so callers only need to Release() when they're finished with it.
SharedPatchBypassBuffer* DebuggerControllerPatch::GetOrCreateSharedPatchBypassBuffer()
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    if (m_pSharedPatchBypassBuffer == NULL)
    {
        m_pSharedPatchBypassBuffer = new (interopsafeEXEC) SharedPatchBypassBuffer();
        _ASSERTE(m_pSharedPatchBypassBuffer);
        TRACE_ALLOC(m_pSharedPatchBypassBuffer);
    }

    m_pSharedPatchBypassBuffer->AddRef();

    return m_pSharedPatchBypassBuffer;
}
#endif // FEATURE_EMULATE_SINGLESTEP

// @todo - remove all this splicing trash
// This Sort/Splice stuff just reorders the patches within a particular chain such
// that when we iterate through by calling GetPatch() and GetNextPatch(DebuggerControllerPatch),
// we'll get patches in increasing order of DebuggerControllerTypes.
// Practically, this means that calling GetPatch() will return EnC patches before stepping patches.
//
#if 1
void DebuggerPatchTable::SortPatchIntoPatchList(DebuggerControllerPatch **ppPatch)
{
    LOG((LF_CORDB, LL_EVERYTHING, "DPT::SPIPL called.\n"));
#ifdef _DEBUG
    DebuggerControllerPatch *patchFirst
        = (DebuggerControllerPatch *) Find(Hash((*ppPatch)), Key((*ppPatch)));
    _ASSERTE(patchFirst == (*ppPatch));
    _ASSERTE((*ppPatch)->controller->GetDCType() != DEBUGGER_CONTROLLER_STATIC);
#endif //_DEBUG
    DebuggerControllerPatch *patchNext = GetNextPatch((*ppPatch));
LOG((LF_CORDB, LL_EVERYTHING, "DPT::SPIPL GetNextPatch passed\n"));
    //List contains one, (sorted) element
    if (patchNext == NULL)
    {
        LOG((LF_CORDB, LL_INFO10000,
             "DPT::SPIPL: Patch 0x%x is a sorted singleton\n", (*ppPatch)));
        return;
    }

    // If we decide to reorder the list, we'll need to keep the element
    // indexed by the hash function as the (sorted)first item.  Everything else
    // chains off this element, can can thus stay put.
    // Thus, either the element we just added is already sorted, or else we'll
    // have to move it elsewhere in the list, meaning that we'll have to swap
    // the second item & the new item, so that the index points to the proper
    // first item in the list.

    //use Cur ptr for case where patch gets appended to list
    DebuggerControllerPatch *patchCur = patchNext;

    while (patchNext != NULL &&
            ((*ppPatch)->controller->GetDCType() >
             patchNext->controller->GetDCType()) )
    {
        patchCur = patchNext;
        patchNext = GetNextPatch(patchNext);
    }

    if (patchNext == GetNextPatch((*ppPatch)))
    {
        LOG((LF_CORDB, LL_INFO10000,
             "DPT::SPIPL: Patch 0x%x is already sorted\n", (*ppPatch)));
        return; //already sorted
    }

    LOG((LF_CORDB, LL_INFO10000,
         "DPT::SPIPL: Patch 0x%x will be moved \n", (*ppPatch)));

    //remove it from the list
    SpliceOutOfList((*ppPatch));

    // the kinda neat thing is: since we put it originally at the front of the list,
    // and it's not in order, then it must be behind another element of this list,
    // so we don't have to write any 'SpliceInFrontOf' code.

    _ASSERTE(patchCur != NULL);
    SpliceInBackOf((*ppPatch), patchCur);

    LOG((LF_CORDB, LL_INFO10000,
         "DPT::SPIPL: Patch 0x%x is now sorted\n", (*ppPatch)));
}

// This can leave the list empty, so don't do this unless you put
// the patch back somewhere else.
void DebuggerPatchTable::SpliceOutOfList(DebuggerControllerPatch *patch)
{
    // We need to get iHash, the index of the ptr within
    // m_piBuckets, ie it's entry in the hashtable.
    ULONG iHash = Hash(patch) % m_iBuckets;
    ULONG iElement = m_piBuckets[iHash];
    DebuggerControllerPatch *patchFirst
        = (DebuggerControllerPatch *) EntryPtr(iElement);

    // Fix up pointers to chain
    if (patchFirst == patch)
    {
        // The first patch shouldn't have anything behind it.
        _ASSERTE(patch->entry.iPrev == DPT_INVALID_SLOT);

        if (patch->entry.iNext != DPT_INVALID_SLOT)
        {
            m_piBuckets[iHash] = patch->entry.iNext;
        }
        else
        {
            m_piBuckets[iHash] = DPT_INVALID_SLOT;
        }
    }

    if (patch->entry.iNext != DPT_INVALID_SLOT)
    {
        EntryPtr(patch->entry.iNext)->iPrev = patch->entry.iPrev;
    }

    if (patch->entry.iPrev != DPT_INVALID_SLOT)
    {
        EntryPtr(patch->entry.iNext)->iNext = patch->entry.iNext;
    }

    patch->entry.iNext = DPT_INVALID_SLOT;
    patch->entry.iPrev = DPT_INVALID_SLOT;
}

void DebuggerPatchTable::SpliceInBackOf(DebuggerControllerPatch *patchAppend,
                                        DebuggerControllerPatch *patchEnd)
{
    ULONG iAppend = ItemIndex((HASHENTRY*)patchAppend);
    ULONG iEnd = ItemIndex((HASHENTRY*)patchEnd);

    patchAppend->entry.iPrev = iEnd;
    patchAppend->entry.iNext = patchEnd->entry.iNext;

    if (patchAppend->entry.iNext != DPT_INVALID_SLOT)
        EntryPtr(patchAppend->entry.iNext)->iPrev = iAppend;

    patchEnd->entry.iNext = iAppend;
}
#endif

//-----------------------------------------------------------------------------
// Stack safety rules.
// In general, we're safe to crawl whenever we're in preemptive mode.
// We're also must be safe at any spot the thread could get synchronized,
// because that means that the thread will be stopped to let the debugger shell
// inspect it and that can definitely take stack traces.
// Basically the only unsafe spot is in the middle of goofy stub with some
// partially constructed frame while in coop mode.
//-----------------------------------------------------------------------------

// Safe if we're at certain types of patches.
// See Patch::IsSafeForStackTrace for details.
StackTraceTicket::StackTraceTicket(DebuggerControllerPatch * patch)
{
    _ASSERTE(patch != NULL);
    _ASSERTE(patch->IsSafeForStackTrace());
}

// Safe if there was already another stack trace at this spot. (Grandfather clause)
// This is commonly used for StepOut, which takes runs stacktraces to crawl up
// the stack to find a place to patch.
StackTraceTicket::StackTraceTicket(ControllerStackInfo * info)
{
    _ASSERTE(info != NULL);

    // Ensure that the other stack info object actually executed (and thus was
    // actually valid).
    _ASSERTE(info->m_dbgExecuted);
}

// Safe b/c the context shows we're in native managed code.
// This must be safe because we could always set a managed breakpoint by native
// offset and thus synchronize the shell at this spot. So this is
// a specific example of the Synchronized case. The fact that we don't actually
// synchronize doesn't make us any less safe.
StackTraceTicket::StackTraceTicket(const BYTE * ip)
{
    _ASSERTE(g_pEEInterface->IsManagedNativeCode(ip));
}

// Safe it we're at a Synchronized point point.
StackTraceTicket::StackTraceTicket(Thread * pThread)
{
    _ASSERTE(pThread != NULL);

    // If we're synchronized, the debugger should be stopped.
    // That means all threads are synced and must be safe to take a stacktrace.
    // Thus we don't even need to do a thread-specific check.
    _ASSERTE(g_pDebugger->IsStopped());
}

// DebuggerUserBreakpoint has a special case of safety. See that ctor for details.
StackTraceTicket::StackTraceTicket(DebuggerUserBreakpoint * p)
{
    _ASSERTE(p != NULL);
}

//void ControllerStackInfo::GetStackInfo():   GetStackInfo
//      is invoked by the user to trigger the stack walk.  This will
//      cause the stack walk detailed in the class description to happen.
// Thread* thread:  The thread to do the stack walk on.
// void* targetFP:  Can be either NULL (meaning that the bottommost
//      frame is the target), or an frame pointer, meaning that the
//      caller wants information about a specific frame.
// CONTEXT* pContext:  A pointer to a CONTEXT structure.  Can be null,
// we use our temp context.
// bool suppressUMChainFromComPlusMethodFrameGeneric - A ridiculous flag that is trying to narrowly
//      target a fix for issue 650903.
// StackTraceTicket - ticket to ensure that we actually have permission for this stacktrace
void ControllerStackInfo::GetStackInfo(
    StackTraceTicket ticket,
    Thread *thread,
    FramePointer targetFP,
    CONTEXT *pContext,
    bool suppressUMChainFromComPlusMethodFrameGeneric
    )
{
    _ASSERTE(thread != NULL);

    BOOL contextValid = (pContext != NULL);
    if (!contextValid)
    {
        // We're assuming the thread is protected w/ a frame (which includes the redirection
        // case). The stackwalker will use that protection to prime the context.
        pContext = &this->m_tempContext;
    }
    else
    {
        // If we provided an explicit context for this thread, it better not be redirected.
        _ASSERTE(!ISREDIRECTEDTHREAD(thread));
    }

    // Mark this stackwalk as valid so that it can in turn be used to grandfather
    // in other stackwalks.
    INDEBUG(m_dbgExecuted = true);

    m_activeFound = false;
    m_returnFound = false;
    m_bottomFP  = LEAF_MOST_FRAME;
    m_targetFP  = targetFP;
    m_targetFrameFound = (m_targetFP == LEAF_MOST_FRAME);
    m_specialChainReason = CHAIN_NONE;
    m_suppressUMChainFromComPlusMethodFrameGeneric = suppressUMChainFromComPlusMethodFrameGeneric;

    int result = DebuggerWalkStack(thread,
                                   LEAF_MOST_FRAME,
                                   pContext,
                                   contextValid,
                                   WalkStack,
                                   (void *) this,
                                   FALSE);

    _ASSERTE(m_activeFound); // All threads have at least one unmanaged frame

    if (result == SWA_DONE)
    {
        _ASSERTE(!m_returnFound);
        m_returnFrame = m_activeFrame;
    }
}

//---------------------------------------------------------------------------------------
//
// This function "undoes" an unwind, i.e. it takes the active frame (the current frame)
// and sets it to be the return frame (the caller frame).  Currently it is only used by
// the stepper to step out of an LCG method.  See DebuggerStepper::DetectHandleLCGMethods()
// for more information.
//
// Assumptions:
//    The current frame is valid on entry.
//
// Notes:
//    After this function returns, the active frame on this instance of ControllerStackInfo will no longer be valid.
//
//    This function is specifically for DebuggerStepper::DetectHandleLCGMethods().  Using it in other scencarios may
//    require additional changes.
//

void ControllerStackInfo::SetReturnFrameWithActiveFrame()
{
    // Copy the active frame into the return frame.
    m_returnFound = true;
    m_returnFrame = m_activeFrame;

    // Invalidate the active frame.
    m_activeFound = false;
    memset(&(m_activeFrame), 0, sizeof(m_activeFrame));
    m_activeFrame.fp = LEAF_MOST_FRAME;
}

// Fill in a controller-stack info.
StackWalkAction ControllerStackInfo::WalkStack(FrameInfo *pInfo, void *data)
{
    LIMITED_METHOD_CONTRACT;

    _ASSERTE(!pInfo->HasStubFrame()); // we didn't ask for stub frames.

    ControllerStackInfo *i = (ControllerStackInfo *) data;

    //save this info away for later use
    if (i->m_bottomFP == LEAF_MOST_FRAME)
        i->m_bottomFP = pInfo->fp;

    // This is part of the targetted fix for issue 650903. (See the other
    // parts in in code:TrackUMChain and code:DebuggerStepper::TrapStepOut.)
    // pInfo->fIgnoreThisFrameIfSuppressingUMChainFromComPlusMethodFrameGeneric has been
    // set by TrackUMChain to help us remember that the current frame we're looking at is
    // ComPlusMethodFrameGeneric (we can't rely on looking at pInfo->frame to check
    // this), and i->m_suppressUMChainFromComPlusMethodFrameGeneric has been set by the
    // dude initiating this walk to remind us that our goal in life is to do a Step Out
    // during managed-only debugging. These two things together tell us we should ignore
    // this frame, rather than erroneously identifying it as the target frame.
#ifdef FEATURE_COMINTEROP
    if(i->m_suppressUMChainFromComPlusMethodFrameGeneric &&
        (pInfo->chainReason == CHAIN_ENTER_UNMANAGED) &&
        (pInfo->fIgnoreThisFrameIfSuppressingUMChainFromComPlusMethodFrameGeneric))
    {
        return SWA_CONTINUE;
    }
#endif // FEATURE_COMINTEROP

    //have we reached the correct frame yet?
    if (!i->m_targetFrameFound &&
        IsEqualOrCloserToLeaf(i->m_targetFP, pInfo->fp))
    {
        i->m_targetFrameFound = true;
    }

    if (i->m_targetFrameFound )
    {
        // Ignore Enter-managed chains.
        if (pInfo->chainReason == CHAIN_ENTER_MANAGED)
        {
            return SWA_CONTINUE;
        }

        if (i->m_activeFound )
        {
            // We care if the current frame is unmanaged (in case a managed stepper is initiated
            // on a thread currently in unmanaged code). But since we can't step-out to UM frames, 
            // we can just skip them in the stack walk.
            if (!pInfo->managed)
            {
                return SWA_CONTINUE;
            }
        
            if (pInfo->chainReason == CHAIN_CLASS_INIT)
                i->m_specialChainReason = pInfo->chainReason;

            if (pInfo->fp != i->m_activeFrame.fp) // avoid dups
            {
                i->m_returnFrame = *pInfo;

#if defined(WIN64EXCEPTIONS)
                CopyREGDISPLAY(&(i->m_returnFrame.registers), &(pInfo->registers));
#endif // WIN64EXCEPTIONS

                i->m_returnFound = true;

                return SWA_ABORT;
            }
        }
        else
        {
            i->m_activeFrame = *pInfo;

#if defined(WIN64EXCEPTIONS)
            CopyREGDISPLAY(&(i->m_activeFrame.registers), &(pInfo->registers));
#endif // WIN64EXCEPTIONS

            i->m_activeFound = true;

            return SWA_CONTINUE;
        }
    }

    return SWA_CONTINUE;
}


//
// Note that patches may be reallocated - do not keep a pointer to a patch.
//
DebuggerControllerPatch *DebuggerPatchTable::AddPatchForMethodDef(DebuggerController *controller,
                                  Module *module,
                                  mdMethodDef md,
                                  MethodDesc* pMethodDescFilter,
                                  size_t offset,
                                  BOOL offsetIsIL,
                                  DebuggerPatchKind kind,
                                  FramePointer fp,
                                  AppDomain *pAppDomain,
                                  SIZE_T masterEnCVersion,
                                  DebuggerJitInfo *dji)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;
    


    LOG( (LF_CORDB,LL_INFO10000,"DCP:AddPatchForMethodDef unbound "
        "relative in methodDef 0x%x with dji 0x%x "
        "controller:0x%x AD:0x%x\n", md,
        dji, controller, pAppDomain));

    DebuggerFunctionKey key;

    key.module = module;
    key.md = md;

    // Get a new uninitialized patch object
    DebuggerControllerPatch *patch =
      (DebuggerControllerPatch *) Add(HashKey(&key)); 
    if (patch == NULL)
    {
        ThrowOutOfMemory();
    }
#ifndef FEATURE_EMULATE_SINGLESTEP
    patch->Initialize();
#endif

    //initialize the patch data structure.
    InitializePRD(&(patch->opcode));
    patch->controller = controller;
    patch->key.module = module;
    patch->key.md = md;
    patch->pMethodDescFilter = pMethodDescFilter;
    patch->offset = offset;
    patch->offsetIsIL = offsetIsIL;
    patch->address = NULL;
    patch->fp = fp;
    patch->trace.Bad_SetTraceType(DPT_DEFAULT_TRACE_TYPE);      // TRACE_OTHER
    patch->refCount   = 1;            // AddRef()
    patch->fSaveOpcode = false;
    patch->pAppDomain = pAppDomain;
    patch->pid = m_pid++;

    if (kind == PATCH_KIND_IL_MASTER)
    {
        _ASSERTE(dji == NULL);
        patch->encVersion = masterEnCVersion;
    }
    else
    {
        patch->dji = dji;
    }
    patch->kind = kind;

    if (dji)
        LOG((LF_CORDB,LL_INFO10000,"AddPatchForMethodDef w/ version 0x%04x, "
        "pid:0x%x\n", dji->m_encVersion, patch->pid));
    else if (kind == PATCH_KIND_IL_MASTER)
        LOG((LF_CORDB,LL_INFO10000,"AddPatchForMethodDef w/ version 0x%04x, "
        "pid:0x%x\n", masterEnCVersion,patch->pid));
    else
        LOG((LF_CORDB,LL_INFO10000,"AddPatchForMethodDef w/ no dji or dmi, pid:0x%x\n",patch->pid));


    // This patch is not yet bound or activated
    _ASSERTE( !patch->IsBound() );
    _ASSERTE( !patch->IsActivated() );

    // The only kind of patch with IL offset is the IL master patch.
    _ASSERTE(patch->IsILMasterPatch() || patch->offsetIsIL == FALSE);

    // The only kind of patch that allows a MethodDescFilter is the IL master patch
    _ASSERTE(patch->IsILMasterPatch() || patch->pMethodDescFilter == NULL);

    // Zero is the only native offset that we allow to bind across different jitted
    // code bodies. There isn't any sensible meaning to binding at some other native offset.
    // Even if all the code bodies had an instruction that started at that offset there is
    // no guarantee those instructions represent a semantically equivalent point in the
    // method's execution.
    _ASSERTE(!(patch->IsILMasterPatch() && !patch->offsetIsIL && patch->offset != 0));

    return patch;
}

// Create and bind a patch to the specified address
// The caller should immediately activate the patch since we typically expect bound patches
// will always be activated.
DebuggerControllerPatch *DebuggerPatchTable::AddPatchForAddress(DebuggerController *controller,
                                  MethodDesc *fd,
                                  size_t offset,
                                  DebuggerPatchKind kind,
                                  CORDB_ADDRESS_TYPE *address,
                                  FramePointer fp,
                                  AppDomain *pAppDomain,
                                  DebuggerJitInfo *dji,
                                  SIZE_T pid,
                                  TraceType traceType)

{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;


    _ASSERTE(kind == PATCH_KIND_NATIVE_MANAGED || kind == PATCH_KIND_NATIVE_UNMANAGED);
    LOG((LF_CORDB,LL_INFO10000,"DCP:AddPatchForAddress bound "
        "absolute to 0x%x with dji 0x%x (mdDef:0x%x) "
        "controller:0x%x AD:0x%x\n",
        address, dji, (fd!=NULL?fd->GetMemberDef():0), controller,
        pAppDomain));

    // get new uninitialized patch object
    DebuggerControllerPatch *patch =
      (DebuggerControllerPatch *) Add(HashAddress(address)); 

    if (patch == NULL)
    {
        ThrowOutOfMemory();
    }
#ifndef FEATURE_EMULATE_SINGLESTEP
    patch->Initialize();
#endif

    // initialize the patch data structure
    InitializePRD(&(patch->opcode));
    patch->controller = controller;

    if (fd == NULL)
    {
        patch->key.module = NULL;
        patch->key.md = mdTokenNil;
    }
    else
    {
        patch->key.module = g_pEEInterface->MethodDescGetModule(fd);
        patch->key.md = fd->GetMemberDef();
    }
    patch->pMethodDescFilter = NULL;
    patch->offset = offset;
    patch->offsetIsIL = FALSE;
    patch->address = address;
    patch->fp = fp;
    patch->trace.Bad_SetTraceType(traceType);
    patch->refCount   = 1;            // AddRef()
    patch->fSaveOpcode = false;
    patch->pAppDomain = pAppDomain;
    if (pid == DCP_PID_INVALID)
        patch->pid = m_pid++;
    else
        patch->pid = pid;

    patch->dji = dji;
    patch->kind = kind;

    if (dji == NULL)
        LOG((LF_CORDB,LL_INFO10000,"AddPatchForAddress w/ version with no dji, pid:0x%x\n", patch->pid));
    else
    {
        LOG((LF_CORDB,LL_INFO10000,"AddPatchForAddress w/ version 0x%04x, "
            "pid:0x%x\n", dji->m_methodInfo->GetCurrentEnCVersion(), patch->pid));

        _ASSERTE( fd==NULL || fd == dji->m_nativeCodeVersion.GetMethodDesc() );
    }

    SortPatchIntoPatchList(&patch);

    // This patch is bound but not yet activated
    _ASSERTE( patch->IsBound() );
    _ASSERTE( !patch->IsActivated() );

    // The only kind of patch with IL offset is the IL master patch.
    _ASSERTE(patch->IsILMasterPatch() || patch->offsetIsIL == FALSE);
    return patch;
}

// Set the native address for this patch.
void DebuggerPatchTable::BindPatch(DebuggerControllerPatch *patch, CORDB_ADDRESS_TYPE *address)
{
    _ASSERTE(patch != NULL);
    _ASSERTE(address != NULL);
    _ASSERTE( !patch->IsILMasterPatch() );
    _ASSERTE(!patch->IsBound() );
    
    //Since the actual patch doesn't move, we don't have to worry about
    //zeroing out the opcode field (see lenghty comment above)
    // Since the patch is double-hashed based off Address, if we change the address,
    // we must remove and reinsert the patch.
    CHashTable::Delete(HashKey(&patch->key), ItemIndex((HASHENTRY*)patch));

    patch->address = address;

    CHashTable::Add(HashAddress(address), ItemIndex((HASHENTRY*)patch));

    SortPatchIntoPatchList(&patch);

    _ASSERTE(patch->IsBound() );
    _ASSERTE(!patch->IsActivated() );
}

// Disassociate a patch from a specific code address.
void DebuggerPatchTable::UnbindPatch(DebuggerControllerPatch *patch)
{
    _ASSERTE(patch != NULL);
    _ASSERTE(patch->kind != PATCH_KIND_IL_MASTER);
    _ASSERTE(patch->IsBound() );
    _ASSERTE(!patch->IsActivated() );

    //<REVISIT_TODO>@todo We're hosed if the patch hasn't been primed with
    // this info & we can't get it...</REVISIT_TODO>
    if (patch->key.module == NULL ||
        patch->key.md == mdTokenNil)
    {
        MethodDesc *fd = g_pEEInterface->GetNativeCodeMethodDesc(
            dac_cast<PCODE>(patch->address));
        _ASSERTE( fd != NULL );
        patch->key.module = g_pEEInterface->MethodDescGetModule(fd);
        patch->key.md = fd->GetMemberDef();
    }

    // Update it's index entry in the table to use it's unbound key
    // Since the patch is double-hashed based off Address, if we change the address,
    // we must remove and reinsert the patch.
    CHashTable::Delete( HashAddress(patch->address),
                        ItemIndex((HASHENTRY*)patch));

    patch->address = NULL;      // we're no longer bound to this address

    CHashTable::Add( HashKey(&patch->key),
                     ItemIndex((HASHENTRY*)patch));

     _ASSERTE(!patch->IsBound() );

}

void DebuggerPatchTable::RemovePatch(DebuggerControllerPatch *patch)
{
    // Since we're deleting this patch, it must not be activated (i.e. it must not have a stored opcode)
    _ASSERTE( !patch->IsActivated() );
#ifndef FEATURE_EMULATE_SINGLESTEP
    patch->DoCleanup();
#endif

    //
    // Because of the implementation of CHashTable, we can safely
    // delete elements while iterating through the table.  This
    // behavior is relied upon - do not change to a different
    // implementation without considering this fact.
    //
    Delete(Hash(patch),  (HASHENTRY *) patch);

}

DebuggerControllerPatch *DebuggerPatchTable::GetNextPatch(DebuggerControllerPatch *prev)
{
    ULONG iNext;
    HASHENTRY *psEntry;

    // Start at the next entry in the chain.
    // @todo - note that: EntryPtr(ItemIndex(x)) == x
    iNext = EntryPtr(ItemIndex((HASHENTRY*)prev))->iNext;

    // Search until we hit the end.
    while (iNext != UINT32_MAX)
    {
        // Compare the keys.
        psEntry = EntryPtr(iNext);

        // Careful here... we can hash the entries in this table
        // by two types of keys. In this type of search, the type
        // of the second key (psEntry) does not necessarily
        // indicate the type of the first key (prev), so we have
        // to check for sure.
        DebuggerControllerPatch *pc2 = (DebuggerControllerPatch*)psEntry;

        if (((pc2->address == NULL) && (prev->address == NULL)) ||
            ((pc2->address != NULL) && (prev->address != NULL)))
            if (!Cmp(Key(prev), psEntry))
                return pc2;

        // Advance to the next item in the chain.
        iNext = psEntry->iNext;
    }

    return NULL;
}

#ifdef _DEBUG_PATCH_TABLE
    // DEBUG An internal debugging routine, it iterates
    //      through the hashtable, stopping at every
    //      single entry, no matter what it's state.  For this to
    //      compile, you're going to have to add friend status
    //      of this class to CHashTableAndData in
    //      to $\Com99\Src\inc\UtilCode.h
void DebuggerPatchTable::CheckPatchTable()
{
    if (NULL != m_pcEntries)
    {
        DebuggerControllerPatch *dcp;
        int i = 0;
        while (i++ <m_iEntries)
        {
            dcp = (DebuggerControllerPatch*)&(((DebuggerControllerPatch *)m_pcEntries)[i]);
            if (dcp->opcode != 0 )
            {
                LOG((LF_CORDB,LL_INFO1000, "dcp->addr:0x%8x "
                    "mdMD:0x%8x, offset:0x%x, native:%d\n",
                    dcp->address, dcp->key.md, dcp->offset,
                    dcp->IsNativePatch()));
            }
        }
    }
}

#endif // _DEBUG_PATCH_TABLE

// Count how many patches are in the table.
// Use for asserts
int DebuggerPatchTable::GetNumberOfPatches()
{
    int total = 0;

    if (NULL != m_pcEntries)
    {
        DebuggerControllerPatch *dcp;
        ULONG i = 0;

        while (i++ <m_iEntries)
        {
            dcp = (DebuggerControllerPatch*)&(((DebuggerControllerPatch *)m_pcEntries)[i]);

            if (dcp->IsActivated() || !dcp->IsFree())
                total++;
        }
    }
    return total;
}

#if defined(_DEBUG)
//-----------------------------------------------------------------------------
// Debug check that we only have 1 thread-starter per thread.
// pNew - the new DTS. We'll make sure there's not already a DTS on this thread.
//-----------------------------------------------------------------------------
void DebuggerController::EnsureUniqueThreadStarter(DebuggerThreadStarter * pNew)
{
    // This lock should be safe to take since our base class ctor takes it.
    ControllerLockHolder lockController;
    DebuggerController * pExisting = g_controllers;
    while(pExisting != NULL)
    {
        if (pExisting->GetDCType() == DEBUGGER_CONTROLLER_THREAD_STARTER)
        {
            if (pExisting != pNew)
            {
                // If we have 2 thread starters, they'd better be on different threads.
                _ASSERTE((pExisting->GetThread() != pNew->GetThread()));
            }
        }
        pExisting = pExisting->m_next;
    }
}
#endif

//-----------------------------------------------------------------------------
// If we have a thread-starter on the given EE thread, make sure it's cancel.
// Thread-Starters normally delete themselves when they fire. But if the EE
// destroys the thread before it fires, then we'd still have an active DTS.
//-----------------------------------------------------------------------------
void DebuggerController::CancelOutstandingThreadStarter(Thread * pThread)
{
    _ASSERTE(pThread != NULL);
    LOG((LF_CORDB, LL_EVERYTHING, "DC:CancelOutstandingThreadStarter - checking on thread =0x%p\n", pThread));

    ControllerLockHolder lockController;
    DebuggerController * p = g_controllers;
    while(p != NULL)
    {
        if (p->GetDCType() == DEBUGGER_CONTROLLER_THREAD_STARTER)
        {
            if (p->GetThread() == pThread)
            {
                LOG((LF_CORDB, LL_EVERYTHING, "DC:CancelOutstandingThreadStarter, pThread=0x%p, Found=0x%p\n", p));

                // There's only 1 DTS per thread, so once we find it, we can quit.
                p->Delete();
                p = NULL;
                break;
            }
        }
        p = p->m_next;
    }
    // The common case is that our DTS hit its patch and did a SendEvent (and
    // deleted itself). So usually we'll get through the whole list w/o deleting anything.
    
}

//void DebuggerController::Initialize()   Sets up the static
// variables for the static DebuggerController class.
// How: Sets g_runningOnWin95, initializes the critical section
HRESULT DebuggerController::Initialize()
{
    CONTRACT(HRESULT)
    {
        THROWS;
        GC_NOTRIGGER;
        // This can be called in an "early attach" case, so DebuggerIsInvolved()
        // will be b/c we don't realize the debugger's attaching to us.
        //PRECONDITION(DebuggerIsInvolved());
        POSTCONDITION(CheckPointer(g_patches));
        POSTCONDITION(RETVAL == S_OK);
    }
    CONTRACT_END;

    if (g_patches == NULL)
    {        
        ZeroMemory(&g_criticalSection, sizeof(g_criticalSection)); // Init() expects zero-init memory.

        // NOTE: CRST_UNSAFE_ANYMODE prevents a GC mode switch when entering this crst.
        // If you remove this flag, we will switch to preemptive mode when entering
        // g_criticalSection, which means all functions that enter it will become
        // GC_TRIGGERS.  (This includes all uses of ControllerLockHolder.)  So be sure
        // to update the contracts if you remove this flag.
        g_criticalSection.Init(CrstDebuggerController, 
            (CrstFlags)(CRST_UNSAFE_ANYMODE | CRST_REENTRANCY | CRST_DEBUGGER_THREAD));

        g_patches = new (interopsafe) DebuggerPatchTable();
        _ASSERTE(g_patches != NULL); // throws on oom

        HRESULT hr = g_patches->Init();

        if (FAILED(hr))
        {
            DeleteInteropSafe(g_patches);
            ThrowHR(hr);
        }

        g_patchTableValid = TRUE;
        TRACE_ALLOC(g_patches);
    }

    _ASSERTE(g_patches != NULL);

    RETURN (S_OK);
}


//---------------------------------------------------------------------------------------
//
// Constructor for a controller
//
// Arguments:
//    pThread - thread that controller has affinity to. NULL if no thread - affinity.
//    pAppdomain - appdomain that controller has affinity to. NULL if no AD affinity.
//
//
// Notes:
//    "Affinity" is per-controller specific. Affinity is generally passed on to
//    any patches the controller creates. So if a controller has affinity to Thread X, 
//    then any patches it creates will only fire on Thread-X.
//
//---------------------------------------------------------------------------------------

DebuggerController::DebuggerController(Thread * pThread, AppDomain * pAppDomain)
  : m_pAppDomain(pAppDomain), 
    m_thread(pThread), 
    m_singleStep(false),
    m_exceptionHook(false), 
    m_traceCall(0), 
    m_traceCallFP(ROOT_MOST_FRAME),
    m_unwindFP(LEAF_MOST_FRAME), 
    m_eventQueuedCount(0), 
    m_deleted(false),
    m_fEnableMethodEnter(false)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        CONSTRUCTOR_CHECK;
    }
    CONTRACTL_END;

    LOG((LF_CORDB, LL_INFO10000, "DC: 0x%x m_eventQueuedCount to 0 - DC::DC\n", this));
    ControllerLockHolder lockController;
    {
        m_next = g_controllers;
        g_controllers = this;
    }    
}

//---------------------------------------------------------------------------------------
//
// Debugger::Controller::DeleteAllControlers - deletes all debugger contollers
//
// Arguments:
//    None
//
// Return Value:
//    None
//
// Notes:
//    This is used at detach time to remove all DebuggerControllers.  This will remove all
//    patches and do whatever other cleanup individual DebuggerControllers consider 
//    necessary to allow the debugger to detach and the process to run normally. 
//

void DebuggerController::DeleteAllControllers()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    ControllerLockHolder lockController;
    DebuggerController * pDebuggerController = g_controllers;
    DebuggerController * pNextDebuggerController = NULL;

    while (pDebuggerController != NULL)
    {
        pNextDebuggerController = pDebuggerController->m_next;
        pDebuggerController->DebuggerDetachClean();
        pDebuggerController->Delete();
        pDebuggerController = pNextDebuggerController;
    }    
}

DebuggerController::~DebuggerController()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        DESTRUCTOR_CHECK;
    }
    CONTRACTL_END;

    ControllerLockHolder lockController;

    _ASSERTE(m_eventQueuedCount == 0);

    DisableAll();

    //
    // Remove controller from list
    //

    DebuggerController **c;

    c = &g_controllers;
    while (*c != this)
        c = &(*c)->m_next;

    *c = m_next;
    
}

// void DebuggerController::Delete()
// What: Marks an instance as deletable.  If it's ref count
// (see Enqueue, Dequeue) is currently zero, it actually gets deleted
// How: Set m_deleted to true.  If m_eventQueuedCount==0, delete this
void DebuggerController::Delete()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    if (m_eventQueuedCount == 0)
    {
        LOG((LF_CORDB|LF_ENC, LL_INFO100000, "DC::Delete: actual delete of this:0x%x!\n", this));
        TRACE_FREE(this);
        DeleteInteropSafe(this);
    }
    else
    {
        LOG((LF_CORDB|LF_ENC, LL_INFO100000, "DC::Delete: marked for "
            "future delete of this:0x%x!\n", this));
        LOG((LF_CORDB|LF_ENC, LL_INFO10000, "DC:0x%x m_eventQueuedCount at 0x%x\n",
            this, m_eventQueuedCount));
        m_deleted = true;
    }
}

void DebuggerController::DebuggerDetachClean()
{
    //do nothing here
}

//static
void DebuggerController::AddRef(DebuggerControllerPatch *patch)
{
    patch->refCount++;
}

//static
void DebuggerController::Release(DebuggerControllerPatch *patch)
{
    patch->refCount--;
    if (patch->refCount == 0)
    {
        LOG((LF_CORDB, LL_INFO10000, "DCP::R: patch deleted, deactivating\n"));
        DeactivatePatch(patch);
        GetPatchTable()->RemovePatch(patch);
    }
}

// void DebuggerController::DisableAll()   DisableAll removes
// all control from the controller.  This includes all patches & page
// protection.  This will invoke Disable* for unwind,singlestep,
// exceptionHook, and tracecall.  It will also go through the patch table &
// attempt to remove any and all patches that belong to this controller.
// If the patch is currently triggering, then a Dispatch* method expects the
// patch to be there after we return, so we instead simply mark the patch
// itself as deleted.
void DebuggerController::DisableAll()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    LOG((LF_CORDB,LL_INFO1000, "DC::DisableAll\n"));
    _ASSERTE(g_patches != NULL);

    ControllerLockHolder ch;
    {
        //
        // Remove controller's patches from list.
        // Don't do this on shutdown because the shutdown thread may have killed another thread asynchronously
        // thus leaving the patchtable in an inconsistent state such that we may fail trying to walk it.
        // Since we're exiting anyways, leaving int3 in the code can't harm anybody.
        //
        if (!g_fProcessDetach)
        {
            HASHFIND f;
            for (DebuggerControllerPatch *patch = g_patches->GetFirstPatch(&f);
                 patch != NULL;
                 patch = g_patches->GetNextPatch(&f))
            {
                if (patch->controller == this)
                {
                    Release(patch);
                }
            }
        }            

        if (m_singleStep)
            DisableSingleStep();
        if (m_exceptionHook)
            DisableExceptionHook();
        if (m_unwindFP != LEAF_MOST_FRAME)
            DisableUnwind();
        if (m_traceCall)
            DisableTraceCall();
        if (m_fEnableMethodEnter)
            DisableMethodEnter();
    }
}

// void DebuggerController::Enqueue()    What: Does
// reference counting so we don't toast a
// DebuggerController while it's in a Dispatch queue.
// Why: In DispatchPatchOrSingleStep, we can't hold locks when going
// into PreEmptiveGC mode b/c we'll create a deadlock.
// So we have to UnLock() prior to
// EnablePreEmptiveGC().  But somebody else can show up and delete the
// DebuggerControllers since we no longer have the lock.  So we have to
// do this reference counting thing to make sure that the controllers
// don't get toasted as we're trying to invoke SendEvent on them.  We have to
// reaquire the lock before invoking Dequeue because Dequeue may
// result in the controller being deleted, which would change the global
// controller list.
// How: InterlockIncrement( m_eventQueuedCount )
void DebuggerController::Enqueue()
{
    LIMITED_METHOD_CONTRACT;

    m_eventQueuedCount++;
    LOG((LF_CORDB, LL_INFO10000, "DC::Enq DC:0x%x m_eventQueuedCount at 0x%x\n",
        this, m_eventQueuedCount));
}

// void DebuggerController::Dequeue()   What: Does
// reference counting so we don't toast a
// DebuggerController while it's in a Dispatch queue.
// How: InterlockDecrement( m_eventQueuedCount ), delete this if
// m_eventQueuedCount == 0 AND m_deleted has been set to true
void DebuggerController::Dequeue()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    LOG((LF_CORDB, LL_INFO10000, "DC::Deq DC:0x%x m_eventQueuedCount at 0x%x\n",
    this, m_eventQueuedCount));
    if (--m_eventQueuedCount == 0)
    {
        if (m_deleted)
        {
            TRACE_FREE(this);
            DeleteInteropSafe(this);
        }
    }
}


// bool DebuggerController::BindPatch()  If the method has
// been JITted and isn't hashed by address already, then hash
// it into the hashtable by address and not DebuggerFunctionKey.
// If the patch->address field is nonzero, we're done.
// Otherwise ask g_pEEInterface to FindLoadedMethodRefOrDef, then
// GetFunctionAddress of the method, if the method is in IL,
// MapILOffsetToNative.  If everything else went Ok, we can now invoke
// g_patches->BindPatch.
// Returns: false if we know that we can't bind the patch immediately.
//      true if we either can bind the patch right now, or can't right now,
//      but might be able to in the future (eg, the method hasn't been JITted)

// Have following outcomes:
// 1) Succeeded in binding the patch to a raw address. patch->address is set.
// (Note we still must apply the patch to put the int 3 in.)
// returns true, *pFail = false
//
// 2) Fails to bind, but a future attempt may succeed. Obvious ex, for an IL-only
// patch on an unjitted method.
// returns false, *pFail = false
//
// 3) Fails to bind because something's wrong. Ex: bad IL offset, no DJI to do a
// mapping with. Future calls will fail too.
// returns false, *pFail = true
bool DebuggerController::BindPatch(DebuggerControllerPatch *patch,
                                   MethodDesc *fd,
                                   CORDB_ADDRESS_TYPE *startAddr)
{
    CONTRACTL
    {
        THROWS; // from GetJitInfo
        GC_NOTRIGGER;
        MODE_ANY; // don't really care what mode we're in.

        PRECONDITION(ThisMaybeHelperThread());
    }
    CONTRACTL_END;

    _ASSERTE(patch != NULL);
    _ASSERTE(!patch->IsILMasterPatch());
    _ASSERTE(fd != NULL);

    //
    // Translate patch to address, if it hasn't been already.
    //

    if (patch->address != NULL)
    {
        return true;
    }

    if (startAddr == NULL)
    {
        if (patch->HasDJI() && patch->GetDJI()->m_jitComplete)
        {
            startAddr = (CORDB_ADDRESS_TYPE *) CORDB_ADDRESS_TO_PTR(patch->GetDJI()->m_addrOfCode);
            _ASSERTE(startAddr != NULL);
        }
        if (startAddr == NULL)
        {
            // Should not be trying to place patches on MethodDecs's for stubs.
            // These stubs will never get jitted.
            CONSISTENCY_CHECK_MSGF(!fd->IsWrapperStub(), ("Can't place patch at stub md %p, %s::%s",
                                   fd, fd->m_pszDebugClassName, fd->m_pszDebugMethodName));

            startAddr = (CORDB_ADDRESS_TYPE *)g_pEEInterface->GetFunctionAddress(fd);
            //
            // Code is not available yet to patch.  The prestub should
            // notify us when it is executed.
            //
            if (startAddr == NULL)
            {
                LOG((LF_CORDB, LL_INFO10000,
                    "DC::BP:Patch at 0x%x not bindable yet.\n", patch->offset));

                return false;
            }
        }
    }

    _ASSERTE(!g_pEEInterface->IsStub((const BYTE *)startAddr));

    // If we've jitted, map to a native offset.
    DebuggerJitInfo *info = g_pDebugger->GetJitInfo(fd, (const BYTE *)startAddr);

#ifdef LOGGING
    if (info == NULL)
    {
        LOG((LF_CORDB,LL_INFO10000, "DC::BindPa: For startAddr 0x%x, didn't find a DJI\n", startAddr));
    }
#endif //LOGGING
    if (info != NULL)
    {
        // There is a strange case with prejitted code and unjitted trace patches. We can enter this function
        // with no DebuggerJitInfo created, then have the call just above this actually create the
        // DebuggerJitInfo, which causes JitComplete to be called, which causes all patches to be bound! If this
        // happens, then we don't need to continue here (its already been done recursivley) and we don't need to
        // re-active the patch, so we return false from right here. We can check this by seeing if we suddently
        // have the address in the patch set.
        if (patch->address != NULL)
        {
            LOG((LF_CORDB,LL_INFO10000, "DC::BindPa: patch bound recursivley by GetJitInfo, bailing...\n"));
            return false;
        }

        LOG((LF_CORDB,LL_INFO10000, "DC::BindPa: For startAddr 0x%x, got DJI "
             "0x%x, from 0x%x size: 0x%x\n", startAddr, info, info->m_addrOfCode, info->m_sizeOfCode));
    }

    LOG((LF_CORDB, LL_INFO10000, "DC::BP:Trying to bind patch in %s::%s version %d\n",
         fd->m_pszDebugClassName, fd->m_pszDebugMethodName, info ? info->m_encVersion : (SIZE_T)-1));

    _ASSERTE(g_patches != NULL);

    CORDB_ADDRESS_TYPE *addr = (CORDB_ADDRESS_TYPE *)
                               CodeRegionInfo::GetCodeRegionInfo(NULL, NULL, startAddr).OffsetToAddress(patch->offset);
    g_patches->BindPatch(patch, addr);

    LOG((LF_CORDB, LL_INFO10000, "DC::BP:Binding patch at 0x%x(off:%x)\n", addr, patch->offset));

    return true;
}

// bool DebuggerController::ApplyPatch()    applies
// the patch described to the code, and
// remembers the replaced opcode.  Note that the same address
// cannot be patched twice at the same time.
// Grabs the opcode & stores in patch, then sets a break
// instruction for either native or IL.
// VirtualProtect & some macros.  Returns false if anything
// went bad.
// DebuggerControllerPatch *patch:  The patch, indicates where
//        to set the INT3 instruction
// Returns: true if the user break instruction was successfully
//        placed into the code-stream, false otherwise
bool DebuggerController::ApplyPatch(DebuggerControllerPatch *patch)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::ApplyPatch at addr 0x%p\n",
        patch->address));

    // If we try to apply an already applied patch, we'll overide our saved opcode
    // with the break opcode and end up getting a break in out patch bypass buffer.
    _ASSERTE(!patch->IsActivated() );
    _ASSERTE(patch->IsBound());

    // Note we may be patching at certain "blessed" points in mscorwks.
    // This is very dangerous b/c we can't be sure patch->Address is blessed or not.


    //
    // Apply the patch.
    //
    _ASSERTE(!(g_pConfig->GetGCStressLevel() & (EEConfig::GCSTRESS_INSTR_JIT|EEConfig::GCSTRESS_INSTR_NGEN))
                 && "Debugger does not work with GCSTRESS 4");

    if (patch->IsNativePatch())
    {
        if (patch->fSaveOpcode)
        {
            // We only used SaveOpcode for when we've moved code, so
            // the patch should already be there.
            patch->opcode = patch->opcodeSaved;
            _ASSERTE( AddressIsBreakpoint(patch->address) );
            return true;
        }

#if _DEBUG
        VerifyExecutableAddress((BYTE*)patch->address);
#endif

        LPVOID baseAddress = (LPVOID)(patch->address);

        DWORD oldProt;

        if (!VirtualProtect(baseAddress,
                            CORDbg_BREAK_INSTRUCTION_SIZE,
                            PAGE_EXECUTE_READWRITE, &oldProt))
        {
            _ASSERTE(!"VirtualProtect of code page failed");
            return false;
        }

        patch->opcode = CORDbgGetInstruction(patch->address);

        CORDbgInsertBreakpoint((CORDB_ADDRESS_TYPE *)patch->address);
        LOG((LF_CORDB, LL_EVERYTHING, "Breakpoint was inserted at %p for opcode %x\n", patch->address, patch->opcode));

        if (!VirtualProtect(baseAddress,
                            CORDbg_BREAK_INSTRUCTION_SIZE,
                            oldProt, &oldProt))
        {
            _ASSERTE(!"VirtualProtect of code page failed");
            return false;
        }
    }
// TODO: : determine if this is needed for AMD64
#if defined(_TARGET_X86_) //REVISIT_TODO what is this?!
    else
    {
        DWORD oldProt;

        //
        // !!! IL patch logic assumes reference insruction encoding
        //
        if (!VirtualProtect((void *) patch->address, 2,
                            PAGE_EXECUTE_READWRITE, &oldProt))
        {
            _ASSERTE(!"VirtualProtect of code page failed");
            return false;
        }
        patch->opcode =
          (unsigned int) *(unsigned short*)(patch->address+1);

        _ASSERTE(patch->opcode != CEE_BREAK);

        *(unsigned short *) (patch->address+1) = CEE_BREAK;

        if (!VirtualProtect((void *) patch->address, 2, oldProt, &oldProt))
        {
            _ASSERTE(!"VirtualProtect of code page failed");
            return false;
        }
    }
#endif //_TARGET_X86_

    return true;
}

// bool DebuggerController::UnapplyPatch()
// UnapplyPatch removes the patch described by the patch.
// (CopyOpcodeFromAddrToPatch, in reverse.)
// Looks a lot like CopyOpcodeFromAddrToPatch, except that we use a macro to
// copy the instruction back to the code-stream & immediately set the
// opcode field to 0 so ReadMemory,WriteMemory will work right.
// Note that it's very important to zero out the opcode field, as it
// is used by the right side to determine if a patch is
// valid or not.
// NO LOCKING
// DebuggerControllerPatch * patch:  Patch to remove
// Returns:  true if the patch was unapplied, false otherwise
bool DebuggerController::UnapplyPatch(DebuggerControllerPatch *patch)
{
    _ASSERTE(patch->address != NULL);
    _ASSERTE(patch->IsActivated() );

    LOG((LF_CORDB,LL_INFO1000, "DC::UP unapply patch at addr 0x%p\n",
        patch->address));

    if (patch->IsNativePatch())
    {
        if (patch->fSaveOpcode)
        {
            // We're doing this for MoveCode, and we don't want to
            // overwrite something if we don't get moved far enough.
            patch->opcodeSaved = patch->opcode;
            InitializePRD(&(patch->opcode));
            _ASSERTE( !patch->IsActivated() );
            return true;
        }

        LPVOID baseAddress = (LPVOID)(patch->address);

        DWORD oldProt;

        if (!VirtualProtect(baseAddress,
                            CORDbg_BREAK_INSTRUCTION_SIZE,
                            PAGE_EXECUTE_READWRITE, &oldProt))
        {
            //
            // We may be trying to remove a patch from memory
            // which has been unmapped. We can ignore the
            // error in this case.
            //
            InitializePRD(&(patch->opcode));
            return false;
        }

        CORDbgSetInstruction((CORDB_ADDRESS_TYPE *)patch->address, patch->opcode);

        //VERY IMPORTANT to zero out opcode, else we might mistake
        //this patch for an active on on ReadMem/WriteMem (see
        //header file comment)
        InitializePRD(&(patch->opcode));

        if (!VirtualProtect(baseAddress,
                            CORDbg_BREAK_INSTRUCTION_SIZE,
                            oldProt, &oldProt))
        {
            _ASSERTE(!"VirtualProtect of code page failed");
            return false;
        }
    }
    else
    {
        DWORD oldProt;

        if (!VirtualProtect((void *) patch->address, 2,
                            PAGE_EXECUTE_READWRITE, &oldProt))
        {
            //
            // We may be trying to remove a patch from memory
            // which has been unmapped. We can ignore the
            // error in this case.
            //
            InitializePRD(&(patch->opcode));
            return false;
        }

        //
        // !!! IL patch logic assumes reference encoding
        //
// TODO: : determine if this is needed for AMD64
#if defined(_TARGET_X86_)
        _ASSERTE(*(unsigned short*)(patch->address+1) == CEE_BREAK);

        *(unsigned short *) (patch->address+1)
          = (unsigned short) patch->opcode;
#endif //this makes no sense on anything but X86
        //VERY IMPORTANT to zero out opcode, else we might mistake
        //this patch for an active on on ReadMem/WriteMem (see
        //header file comment
        InitializePRD(&(patch->opcode));

        if (!VirtualProtect((void *) patch->address, 2, oldProt, &oldProt))
        {
            _ASSERTE(!"VirtualProtect of code page failed");
            return false;
        }
    }

    _ASSERTE( !patch->IsActivated() );
    _ASSERTE( patch->IsBound() );
    return true;
}

// void DebuggerController::UnapplyPatchAt()
// NO LOCKING
// UnapplyPatchAt removes the patch from a copy of the patched code.
// Like UnapplyPatch, except that we don't bother checking
// memory permissions, but instead replace the breakpoint instruction
// with the opcode at an arbitrary memory address.
void DebuggerController::UnapplyPatchAt(DebuggerControllerPatch *patch,
                                        CORDB_ADDRESS_TYPE *address)
{
    _ASSERTE(patch->IsBound() );

    if (patch->IsNativePatch())
    {
        CORDbgSetInstruction((CORDB_ADDRESS_TYPE *)address, patch->opcode);
        //note that we don't have to zero out opcode field
        //since we're unapplying at something other than
        //the original spot. We assert this is true:
        _ASSERTE( patch->address != address );
    }
    else
    {
        //
        // !!! IL patch logic assumes reference encoding
        //
// TODO: : determine if this is needed for AMD64
#ifdef _TARGET_X86_
        _ASSERTE(*(unsigned short*)(address+1) == CEE_BREAK);

        *(unsigned short *) (address+1)
          = (unsigned short) patch->opcode;
        _ASSERTE( patch->address != address );
#endif // this makes no sense on anything but X86
    }
}

// bool DebuggerController::IsPatched()  Is there a patch at addr?
// How: if fNative && the instruction at addr is the break
// instruction for this platform.
bool DebuggerController::IsPatched(CORDB_ADDRESS_TYPE *address, BOOL native)
{
    LIMITED_METHOD_CONTRACT;

    if (native)
    {
        return AddressIsBreakpoint(address);
    }
    else
        return false;
}

// DWORD DebuggerController::GetPatchedOpcode()  Gets the opcode
// at addr, 'looking underneath' any patches if needed.
// GetPatchedInstruction is a function for the EE to call to "see through"
// a patch to the opcodes which was patched.
// How: Lock() grab opcode directly unless there's a patch, in
// which case grab it out of the patch table.
// BYTE * address:  The address that we want to 'see through'
// Returns:  DWORD value, that is the opcode that should really be there,
//         if we hadn't placed a patch there.  If we haven't placed a patch
//        there, then we'll see the actual opcode at that address.
PRD_TYPE DebuggerController::GetPatchedOpcode(CORDB_ADDRESS_TYPE *address)
{
    _ASSERTE(g_patches != NULL);

    PRD_TYPE opcode;
    ZeroMemory(&opcode, sizeof(opcode));

    ControllerLockHolder lockController;

    //
    // Look for a patch at the address
    //

    DebuggerControllerPatch *patch = g_patches->GetPatch((CORDB_ADDRESS_TYPE *)address);

    if (patch != NULL)
    {
        // Since we got the patch at this address, is must by definition be bound to that address
        _ASSERTE( patch->IsBound() );
        _ASSERTE( patch->address == address );
        // If we're going to be returning it's opcode, then the patch must also be activated
        _ASSERTE( patch->IsActivated() );
        opcode = patch->opcode;
    }
    else
    {
        //
        // Patch was not found - it either is not our patch, or it has
        // just been removed. In either case, just return the current
        // opcode.
        //

        if (g_pEEInterface->IsManagedNativeCode((const BYTE *)address))
        {
            opcode = CORDbgGetInstruction((CORDB_ADDRESS_TYPE *)address);
        }
// <REVISIT_TODO>
// TODO: : determine if this is needed for AMD64
// </REVISIT_TODO>
#ifdef _TARGET_X86_ //what is this?!
        else
        {
            //
            // !!! IL patch logic assumes reference encoding
            //

            opcode = *(unsigned short*)(address+1);
        }
#endif //_TARGET_X86_

    }    

    return opcode;
}

// Holding the controller lock, this will check if an address is patched,
// and if so will then set the PRT_TYPE out parameter to the unpatched value.
BOOL DebuggerController::CheckGetPatchedOpcode(CORDB_ADDRESS_TYPE *address,
                                               /*OUT*/ PRD_TYPE *pOpcode)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    _ASSERTE(g_patches != NULL);

    BOOL res;

    ControllerLockHolder lockController;

    //
    // Look for a patch at the address
    //

    if (IsAddressPatched(address))
    {
        *pOpcode = GetPatchedOpcode(address);
        res = TRUE;
    }
    else
    {
        InitializePRD(pOpcode);
        res = FALSE;
    }
    

    return res;
}

// void DebuggerController::ActivatePatch()  Place a breakpoint
// so that threads will trip over this patch.
// If there any patches at the address already, then copy
// their opcode into this one & return.  Otherwise,
// call ApplyPatch(patch).  There is an implicit list of patches at this
// address by virtue of the fact that we can iterate through all the
// patches in the patch with the same address.
// DebuggerControllerPatch *patch:  The patch to activate
/* static */ void DebuggerController::ActivatePatch(DebuggerControllerPatch *patch)
{
    _ASSERTE(g_patches != NULL);
    _ASSERTE(patch != NULL);
    _ASSERTE(patch->IsBound() );
    _ASSERTE(!patch->IsActivated() );

    bool fApply = true;

    //
    // See if we already have an active patch at this address.
    //
    for (DebuggerControllerPatch *p = g_patches->GetPatch(patch->address);
         p != NULL;
         p = g_patches->GetNextPatch(p))
    {
        if (p != patch)
        {
            // If we're going to skip activating 'patch' because 'p' already exists at the same address
            // then 'p' must be activated.  We expect that all bound patches are activated.
            _ASSERTE( p->IsActivated() );
            patch->opcode = p->opcode;
            fApply = false;
            break;
        }
    }

    //
    // This is the only patch at this address - apply the patch
    // to the code.
    //
    if (fApply)
    {
        ApplyPatch(patch);
    }

    _ASSERTE(patch->IsActivated() );
}

// void DebuggerController::DeactivatePatch()  Make sure that a
// patch won't be hit.
// How: If this patch is the last one at this address, then
// UnapplyPatch.  The caller should then invoke RemovePatch to remove the
// patch from the patch table.
// DebuggerControllerPatch *patch:  Patch to deactivate
void DebuggerController::DeactivatePatch(DebuggerControllerPatch *patch)
{
    _ASSERTE(g_patches != NULL);

    if( !patch->IsBound() ) {
        // patch is not bound, nothing to do
        return;
    }

    // We expect that all bound patches are also activated.
    // One exception to this is if the shutdown thread killed another thread right after
    // if deactivated a patch but before it got to remove it.
    _ASSERTE(patch->IsActivated() );

    bool fUnapply = true;

    //
    // See if we already have an active patch at this address.
    //
    for (DebuggerControllerPatch *p = g_patches->GetPatch(patch->address);
         p != NULL;
         p = g_patches->GetNextPatch(p))
    {
        if (p != patch)
        {
            // There is another patch at this address, so don't remove it
            // However, clear the patch data so that we no longer consider this particular patch activated
            fUnapply = false;
            InitializePRD(&(patch->opcode));
            break;
        }
    }

    if (fUnapply)
    {
        UnapplyPatch(patch);
    }

     _ASSERTE(!patch->IsActivated() );

    //
    // Patch must now be removed from the table.
    //
}

// AddILMasterPatch: record a patch on IL code but do not bind it or activate it.  The master b.p.
// is associated with a module/token pair.  It is used later
// (e.g. in MapAndBindFunctionPatches) to create one or more "slave"
// breakpoints which are associated with particular MethodDescs/JitInfos.
//
// Rationale: For generic code a single IL patch (e.g a breakpoint)
// may give rise to several patches, one for each JITting of
// the IL (i.e. generic code may be JITted multiple times for
// different instantiations).
//
// So we keep one patch which describes
// the breakpoint but which is never actually bound or activated.
// This is then used to apply new "slave" patches to all copies of
// JITted code associated with the method.
//
// <REVISIT_TODO>In theory we could bind and apply the master patch when the
// code is known not to be generic (as used to happen to all breakpoint
// patches in V1).  However this seems like a premature
// optimization.</REVISIT_TODO>
DebuggerControllerPatch *DebuggerController::AddILMasterPatch(Module *module,
                                                              mdMethodDef md,
                                                              MethodDesc *pMethodDescFilter,
                                                              SIZE_T offset,
                                                              BOOL offsetIsIL,
                                                              SIZE_T encVersion)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    _ASSERTE(g_patches != NULL);

    ControllerLockHolder ch;

 
    DebuggerControllerPatch *patch = g_patches->AddPatchForMethodDef(this,
                                     module,
                                     md,
                                     pMethodDescFilter,
                                     offset,
                                     offsetIsIL,
                                     PATCH_KIND_IL_MASTER,
                                     LEAF_MOST_FRAME,
                                     NULL,
                                     encVersion,
                                     NULL);

    LOG((LF_CORDB, LL_INFO10000,
        "DC::AP: Added IL master patch 0x%p for mdTok 0x%x, desc 0x%p at %s offset %d encVersion %d\n", 
        patch, md, pMethodDescFilter, offsetIsIL ? "il" : "native", offset, encVersion));

    return patch;
}

// See notes above on AddILMasterPatch
BOOL DebuggerController::AddBindAndActivateILSlavePatch(DebuggerControllerPatch *master,
                                                        DebuggerJitInfo *dji)
{
    _ASSERTE(g_patches != NULL);
    _ASSERTE(master->IsILMasterPatch());
    _ASSERTE(dji != NULL);

    BOOL   result = FALSE;

    if (!master->offsetIsIL)
    {
        // Zero is the only native offset that we allow to bind across different jitted
        // code bodies. 
        _ASSERTE(master->offset == 0);
        INDEBUG(BOOL fOk = )
            AddBindAndActivatePatchForMethodDesc(dji->m_nativeCodeVersion.GetMethodDesc(), dji,
                0, PATCH_KIND_IL_SLAVE,
                LEAF_MOST_FRAME, m_pAppDomain);
        _ASSERTE(fOk);
        result = TRUE;
    }
    else // bind by IL offset
    {
        // Do not dereference the "master" pointer in the loop!  The loop may add more patches,
        // causing the patch table to grow and move.
        SIZE_T masterILOffset = master->offset;

        // Loop through all the native offsets mapped to the given IL offset.  On x86 the mapping
        // should be 1:1.  On WIN64, because there are funclets, we have have an 1:N mapping.
        DebuggerJitInfo::ILToNativeOffsetIterator it;
        for (dji->InitILToNativeOffsetIterator(it, masterILOffset); !it.IsAtEnd(); it.Next())
        {
            BOOL   fExact;
            SIZE_T offsetNative = it.Current(&fExact);

            // We special case offset 0, which is when a breakpoint is set
            // at the beginning of a method that hasn't been jitted yet.  In
            // that case it's possible that offset 0 has been optimized out,
            // but we still want to set the closest breakpoint to that.
            if (!fExact && (masterILOffset != 0))
            {
                LOG((LF_CORDB, LL_INFO10000, "DC::BP:Failed to bind patch at IL offset 0x%p in %s::%s\n",
                    masterILOffset, dji->m_nativeCodeVersion.GetMethodDesc()->m_pszDebugClassName, dji->m_nativeCodeVersion.GetMethodDesc()->m_pszDebugMethodName));

                continue;
            }
            else
            {
                result = TRUE;
            }

            INDEBUG(BOOL fOk = )
                AddBindAndActivatePatchForMethodDesc(dji->m_nativeCodeVersion.GetMethodDesc(), dji,
                    offsetNative, PATCH_KIND_IL_SLAVE,
                    LEAF_MOST_FRAME, m_pAppDomain);
            _ASSERTE(fOk);
        }
    }

    // As long as we have successfully bound at least one patch, we consider the operation successful.
    return result;
}



// This routine places a patch that is conceptually a patch on the IL code.
// The IL code may be jitted multiple times, e.g. due to generics.
// This routine ensures that both present and subsequent JITtings of code will
// also be patched.
//
// This routine will return FALSE only if we will _never_ be able to
// place the patch in any native code corresponding to the given offset.
// Otherwise it will:
// (a) record a "master" patch
// (b) apply as many slave patches as it can to existing copies of code
//     that have debugging information
BOOL DebuggerController::AddILPatch(AppDomain * pAppDomain, Module *module,
                                  mdMethodDef md,
                                  MethodDesc *pMethodDescFilter,
                                  SIZE_T encVersion,  // what encVersion does this apply to?
                                  SIZE_T offset,
                                  BOOL offsetIsIL)
{
    _ASSERTE(g_patches != NULL);
    _ASSERTE(md != NULL);
    _ASSERTE(module != NULL);

    BOOL fOk = FALSE;

    DebuggerMethodInfo *dmi = g_pDebugger->GetOrCreateMethodInfo(module, md); // throws
    if (dmi == NULL)
    {
        return false;
    }

    EX_TRY
    {
        // OK, we either have (a) no code at all or (b) we have both JIT information and code
        //.
        // Either way, lay down the MasterPatch.
        //
        // MapAndBindFunctionPatches will take care of any instantiations that haven't
        // finished JITting, by making a copy of the master breakpoint.
        DebuggerControllerPatch *master = AddILMasterPatch(module, md, pMethodDescFilter, offset, offsetIsIL, encVersion);

        // We have to keep the index here instead of the pointer.  The loop below adds more patches,
        // which may cause the patch table to grow and move.
        ULONG masterIndex = g_patches->GetItemIndex((HASHENTRY*)master);

        // Iterate through every existing NativeCodeBlob (with the same EnC version).
        // This includes generics + prejitted code.
        DebuggerMethodInfo::DJIIterator it;
        dmi->IterateAllDJIs(pAppDomain, NULL /* module filter */, pMethodDescFilter, &it);

        if (it.IsAtEnd())
        {
            // It is okay if we don't have any DJIs yet.  It just means that the method hasn't been jitted.
            fOk = TRUE;
        }
        else
        {
            // On the other hand, if the method has been jitted, then we expect to be able to bind at least
            // one breakpoint. The exception is when we have multiple EnC versions of the method, in which
            // case it is ok if we don't bind any breakpoint. One scenario is when a method has been updated
            // via EnC but it's not yet jitted. We need to allow a debugger to put a breakpoint on the new
            // version of the method, but the new version won't have a DJI yet.
            BOOL fVersionMatch = FALSE;
            while(!it.IsAtEnd())
            {
                DebuggerJitInfo *dji = it.Current();
                _ASSERTE(dji->m_jitComplete);
                if (dji->m_encVersion == encVersion &&
                   (pMethodDescFilter == NULL || pMethodDescFilter == dji->m_nativeCodeVersion.GetMethodDesc()))
                {
                    fVersionMatch = TRUE;

                    master = (DebuggerControllerPatch *)g_patches->GetEntryPtr(masterIndex);

                    // <REVISIT_TODO> If we're missing JIT info for any then
                    // we won't have applied the bp to every instantiation.  That should probably be reported
                    // as a new kind of condition to the debugger, i.e. report "bp only partially applied".  It would be
                    // a shame to completely fail just because on instantiation is missing debug info: e.g. just because
                    // one component hasn't been prejitted with debugging information.</REVISIT_TODO>
                    fOk = (AddBindAndActivateILSlavePatch(master, dji) || fOk);
                }
                it.Next();
            }

            // This is the exceptional case referred to in the comment above.  If we fail to put a breakpoint 
            // because we don't have a matching version of the method, we need to return TRUE.
            if (fVersionMatch == FALSE)
            {
                fOk = TRUE;
            }
        }
    }
    EX_CATCH
    {
        fOk = FALSE;
    }
    EX_END_CATCH(SwallowAllExceptions)
    return fOk;
}

// Add a patch at native-offset 0 in the latest version of the method.
// This is used by step-in.
// Calls to new methods always go to the latest version, so EnC is not an issue here.
// The method may be not yet jitted. Or it may be prejitted.
void DebuggerController::AddPatchToStartOfLatestMethod(MethodDesc * fd)
{
    CONTRACTL
    {
        THROWS; // from GetJitInfo
        GC_NOTRIGGER;
        MODE_ANY; // don't really care what mode we're in.

        PRECONDITION(ThisMaybeHelperThread());
        PRECONDITION(CheckPointer(fd));
    }
    CONTRACTL_END;

    _ASSERTE(g_patches != NULL);
    Module* pModule = fd->GetModule();
    mdToken defToken = fd->GetMemberDef();
    DebuggerMethodInfo* pDMI = g_pDebugger->GetOrCreateMethodInfo(pModule, defToken);
    DebuggerController::AddILPatch(GetAppDomain(), pModule, defToken, fd, pDMI->GetCurrentEnCVersion(), 0, FALSE);
    return;
}


// Place patch in method at native offset.
BOOL DebuggerController::AddBindAndActivateNativeManagedPatch(MethodDesc * fd,
                                  DebuggerJitInfo *dji,
                                  SIZE_T offsetNative,
                                  FramePointer fp,
                                  AppDomain *pAppDomain)
{
    CONTRACTL
    {
        THROWS; // from GetJitInfo
        GC_NOTRIGGER;
        MODE_ANY; // don't really care what mode we're in.

        PRECONDITION(ThisMaybeHelperThread());
        PRECONDITION(CheckPointer(fd));        
        PRECONDITION(fd->IsDynamicMethod() || (dji != NULL));
    }
    CONTRACTL_END;

    // For non-dynamic methods, we always expect to have a DJI, but just in case, we don't want the assert to AV.
    _ASSERTE((dji == NULL) || (fd == dji->m_nativeCodeVersion.GetMethodDesc()));
    _ASSERTE(g_patches != NULL);
    return DebuggerController::AddBindAndActivatePatchForMethodDesc(fd, dji, offsetNative, PATCH_KIND_NATIVE_MANAGED, fp, pAppDomain);
}

// Adds a breakpoint at a specific native offset in a particular jitted code version
BOOL DebuggerController::AddBindAndActivatePatchForMethodDesc(MethodDesc *fd,
                                  DebuggerJitInfo *dji,
                                  SIZE_T nativeOffset,
                                  DebuggerPatchKind kind,
                                  FramePointer fp,
                                  AppDomain *pAppDomain)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY; // don't really care what mode we're in.

        PRECONDITION(ThisMaybeHelperThread());
        PRECONDITION(kind != PATCH_KIND_IL_MASTER);
    }
    CONTRACTL_END;

    BOOL ok = FALSE;
    ControllerLockHolder ch;

    LOG((LF_CORDB|LF_ENC,LL_INFO10000,"DC::AP: Add to %s::%s, at offs 0x%x "
            "fp:0x%x AD:0x%x\n", fd->m_pszDebugClassName,
            fd->m_pszDebugMethodName,
            nativeOffset, fp.GetSPValue(), pAppDomain));
 
    DebuggerControllerPatch *patch = g_patches->AddPatchForMethodDef(
                            this,
                            g_pEEInterface->MethodDescGetModule(fd),
                            fd->GetMemberDef(),
                            NULL,
                            nativeOffset,
                            FALSE,
                            kind,
                            fp,
                            pAppDomain,
                            NULL,
                            dji);

    if (DebuggerController::BindPatch(patch, fd, NULL))
    {
        LOG((LF_CORDB|LF_ENC,LL_INFO1000,"BindPatch went fine, doing ActivatePatch\n"));
        DebuggerController::ActivatePatch(patch);
        ok = TRUE;
    }

    return ok;
}


// This version is particularly useful b/c it doesn't assume that the
// patch is inside a managed method.
DebuggerControllerPatch *DebuggerController::AddAndActivateNativePatchForAddress(CORDB_ADDRESS_TYPE *address,
                                  FramePointer fp,
                                  bool managed,
                                  TraceType traceType)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;

        PRECONDITION(g_patches != NULL);
    }
    CONTRACTL_END;


    ControllerLockHolder ch;

    DebuggerControllerPatch *patch
      = g_patches->AddPatchForAddress(this,
                            NULL,
                            0,
                            (managed? PATCH_KIND_NATIVE_MANAGED : PATCH_KIND_NATIVE_UNMANAGED),
                            address,
                            fp,
                            NULL,
                            NULL,
                            DebuggerPatchTable::DCP_PID_INVALID,
                            traceType);

    ActivatePatch(patch);

    return patch;
}

void DebuggerController::RemovePatchesFromModule(Module *pModule, AppDomain *pAppDomain )
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    LOG((LF_CORDB, LL_INFO100000, "DPT::CPFM mod:0x%p (%S)\n",
        pModule, pModule->GetDebugName()));

    // First find all patches of interest
    DebuggerController::ControllerLockHolder ch;
    HASHFIND f;
    for (DebuggerControllerPatch *patch = g_patches->GetFirstPatch(&f);
         patch != NULL;
         patch = g_patches->GetNextPatch(&f))
    {
        // Skip patches not in the specified domain
        if ((pAppDomain != NULL) && (patch->pAppDomain != pAppDomain))
            continue;

        BOOL fRemovePatch = FALSE;

        // Remove both native and IL patches the belong to this module
        if (patch->HasDJI())
        {
            DebuggerJitInfo * dji = patch->GetDJI();

            _ASSERTE(patch->key.module == dji->m_nativeCodeVersion.GetMethodDesc()->GetModule());

            // It is not necessary to check for m_fd->GetModule() here. It will
            // be covered by other module unload notifications issued for the appdomain.
            if ( dji->m_pLoaderModule == pModule )
                fRemovePatch = TRUE;
        }
        else
        if (patch->key.module == pModule)
        {
            fRemovePatch = TRUE;
        }

        if (fRemovePatch)
        {
            LOG((LF_CORDB, LL_EVERYTHING, "Removing patch 0x%p\n",
                patch));
            // we shouldn't be both hitting this patch AND
            // unloading the module it belongs to.
            _ASSERTE(!patch->IsTriggering());
            Release( patch );
        }
    }
}

#ifdef _DEBUG
bool DebuggerController::ModuleHasPatches( Module* pModule )
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    if( g_patches == NULL )
    {
        // Patch table hasn't been initialized
        return false;
    }
    
    // First find all patches of interest
    HASHFIND f;
    for (DebuggerControllerPatch *patch = g_patches->GetFirstPatch(&f);
         patch != NULL;
         patch = g_patches->GetNextPatch(&f))
    {
        //
        // This mirrors logic in code:DebuggerController::RemovePatchesFromModule
        //

        if (patch->HasDJI())
        {
            DebuggerJitInfo * dji = patch->GetDJI();

            _ASSERTE(patch->key.module == dji->m_nativeCodeVersion.GetMethodDesc()->GetModule());

            // It may be sufficient to just check m_pLoaderModule here. Since this is used for debug-only
            // check, we will check for m_fd->GetModule() as well to catch more potential problems.
            if ( (dji->m_pLoaderModule == pModule) || (dji->m_nativeCodeVersion.GetMethodDesc()->GetModule() == pModule) )
            {
                return true;
            }
        }

        if (patch->key.module == pModule)
        {
            return true;
        }
    }

    return false;
}
#endif // _DEBUG

//
// Returns true if the given address is in an internal helper
// function, false if its not.
//
// This is a temporary workaround function to avoid having us stop in
// unmanaged code belonging to the Runtime during a StepIn operation.
//
static bool _AddrIsJITHelper(PCODE addr)
{
#if !defined(_WIN64) && !defined(FEATURE_PAL)
    // Is the address in the runtime dll (clr.dll or coreclr.dll) at all? (All helpers are in
    // that dll)
    if (g_runtimeLoadedBaseAddress <= addr && 
            addr < g_runtimeLoadedBaseAddress + g_runtimeVirtualSize)
    {
        for (int i = 0; i < CORINFO_HELP_COUNT; i++)
        {
            if (hlpFuncTable[i].pfnHelper == (void*)addr)
            {
                LOG((LF_CORDB, LL_INFO10000,
                        "_ANIM: address of helper function found: 0x%08x\n",
                        addr));
                return true;
            }
        }

        for (unsigned d = 0; d < DYNAMIC_CORINFO_HELP_COUNT; d++)
        {
            if (hlpDynamicFuncTable[d].pfnHelper == (void*)addr)
            {
                LOG((LF_CORDB, LL_INFO10000,
                        "_ANIM: address of helper function found: 0x%08x\n",
                        addr));
                return true;
            }
        }

        LOG((LF_CORDB, LL_INFO10000,
             "_ANIM: address within runtime dll, but not a helper function "
             "0x%08x\n", addr));
    }
#else // !defined(_WIN64) && !defined(FEATURE_PAL)
    // TODO: Figure out what we want to do here
#endif // !defined(_WIN64) && !defined(FEATURE_PAL)

    return false;
}

// bool DebuggerController::PatchTrace()   What: Invoke
// AddPatch depending on the type of the given TraceDestination.
// How: Invokes AddPatch based on the trace type: TRACE_OTHER will
// return false, the others will obtain args for a call to an AddPatch
// method & return true.
//
// Return true if we set a patch, else false
bool DebuggerController::PatchTrace(TraceDestination *trace,
                                    FramePointer fp,
                                    bool fStopInUnmanaged)
{
    CONTRACTL
    {
        THROWS; // Because AddPatch may throw on oom. We may want to convert this to nothrow and return false.
        MODE_ANY;
        DISABLED(GC_TRIGGERS); // @todo - what should this be?
        
        PRECONDITION(ThisMaybeHelperThread());
    }
    CONTRACTL_END;
    DebuggerControllerPatch *dcp = NULL;
    SIZE_T nativeOffset = 0;

    switch (trace->GetTraceType())
    {
    case TRACE_ENTRY_STUB:  // fall through
    case TRACE_UNMANAGED:
        LOG((LF_CORDB, LL_INFO10000,
             "DC::PT: Setting unmanaged trace patch at 0x%p(%p)\n",
             trace->GetAddress(), fp.GetSPValue()));

        if (fStopInUnmanaged && !_AddrIsJITHelper(trace->GetAddress()))
        {
            AddAndActivateNativePatchForAddress((CORDB_ADDRESS_TYPE *)trace->GetAddress(),
                     fp,
                     FALSE,
                     trace->GetTraceType());
            return true;
        }
        else
        {
            LOG((LF_CORDB, LL_INFO10000, "DC::PT: decided to NOT "
                "place a patch in unmanaged code\n"));
            return false;
        }

    case TRACE_MANAGED:
        LOG((LF_CORDB, LL_INFO10000,
             "Setting managed trace patch at 0x%p(%p)\n", trace->GetAddress(), fp.GetSPValue()));

        MethodDesc *fd;
        fd = g_pEEInterface->GetNativeCodeMethodDesc(trace->GetAddress());
        _ASSERTE(fd);

        DebuggerJitInfo *dji;
        dji = g_pDebugger->GetJitInfoFromAddr(trace->GetAddress());
        //_ASSERTE(dji); //we'd like to assert this, but attach won't work

        nativeOffset = CodeRegionInfo::GetCodeRegionInfo(dji, fd).AddressToOffset((const BYTE *)trace->GetAddress());

        // Code versioning allows calls to be redirected to alternate code potentially after this trace is complete but before
        // execution reaches the call target. Rather than bind the breakpoint to a specific jitted code instance that is currently
        // configured to receive execution we need to prepare for that potential retargetting by binding all jitted code instances.
        //
        // Triggering this based of the native offset is a little subtle, but all of the stubmanagers follow a rule that if they 
        // trace across a call boundary into jitted code they either stop at offset zero of the new method, or they continue tracing
        // out of that jitted code.
        if (nativeOffset == 0)
        {
            AddPatchToStartOfLatestMethod(fd);
        }
        else
        {
            AddBindAndActivateNativeManagedPatch(fd, dji, nativeOffset, fp, NULL);
        }

        
        return true;

    case TRACE_UNJITTED_METHOD:
        // trace->address is actually a MethodDesc* of the method that we'll
        // soon JIT, so put a relative bp at offset zero in.
        LOG((LF_CORDB, LL_INFO10000,
            "Setting unjitted method patch in MethodDesc 0x%p %s\n", trace->GetMethodDesc(), trace->GetMethodDesc() ? trace->GetMethodDesc()->m_pszDebugMethodName : ""));

        // Note: we have to make sure to bind here. If this function is prejitted, this may be our only chance to get a
        // DebuggerJITInfo and thereby cause a JITComplete callback.
        AddPatchToStartOfLatestMethod(trace->GetMethodDesc());
        return true;

    case TRACE_FRAME_PUSH:
        LOG((LF_CORDB, LL_INFO10000,
             "Setting frame patch at 0x%p(%p)\n", trace->GetAddress(), fp.GetSPValue()));

        AddAndActivateNativePatchForAddress((CORDB_ADDRESS_TYPE *)trace->GetAddress(),
                 fp,
                 TRUE,
                 TRACE_FRAME_PUSH);
        return true;

    case TRACE_MGR_PUSH:
        LOG((LF_CORDB, LL_INFO10000,
             "Setting frame patch (TRACE_MGR_PUSH) at 0x%p(%p)\n",
             trace->GetAddress(), fp.GetSPValue()));

        dcp = AddAndActivateNativePatchForAddress((CORDB_ADDRESS_TYPE *)trace->GetAddress(),
                       LEAF_MOST_FRAME, // But Mgr_push can't have fp affinity!
                       TRUE,
                       DPT_DEFAULT_TRACE_TYPE); // TRACE_OTHER
        // Now copy over the trace field since TriggerPatch will expect this
        // to be set for this case.
        if (dcp != NULL)
        {
            dcp->trace = *trace;
        }

        return true;

    case TRACE_OTHER:
        LOG((LF_CORDB, LL_INFO10000,
             "Can't set a trace patch for TRACE_OTHER...\n"));
        return false;

    default:
        _ASSERTE(0);
        return false;
    }
}

//-----------------------------------------------------------------------------
// Checks if the patch matches the context + thread.
// Multiple patches can exist at a single address, so given a patch at the
// Context's current address, this does additional patch-affinity checks like
// thread, AppDomain, and frame-pointer.
// thread - thread executing the given context that hit the patch
// context - context of the thread that hit the patch
// patch - candidate patch that we're looking for a match.
// Returns:
//     True if the patch matches.
//     False
//-----------------------------------------------------------------------------
bool DebuggerController::MatchPatch(Thread *thread,
                                    CONTEXT *context,
                                    DebuggerControllerPatch *patch)
{
    LOG((LF_CORDB, LL_INFO100000, "DC::MP: EIP:0x%p\n", GetIP(context)));

    // Caller should have already matched our addresses.
    if (patch->address != dac_cast<PTR_CORDB_ADDRESS_TYPE>(GetIP(context)))
    {
        return false;
    }

    // <BUGNUM>RAID 67173 -</BUGNUM> we'll make sure that intermediate patches have NULL
    // pAppDomain so that we don't end up running to completion when
    // the appdomain switches halfway through a step.
    if (patch->pAppDomain != NULL)
    {
        AppDomain *pAppDomainCur = thread->GetDomain();

        if (pAppDomainCur != patch->pAppDomain)
        {
            LOG((LF_CORDB, LL_INFO10000, "DC::MP: patches didn't match b/c of "
                "appdomains!\n"));
            return false;
        }
    }

    if (patch->controller->m_thread != NULL && patch->controller->m_thread != thread)
    {
        LOG((LF_CORDB, LL_INFO10000, "DC::MP: patches didn't match b/c threads\n"));
        return false;
    }

    if (patch->fp != LEAF_MOST_FRAME)
    {
        // If we specified a Frame-pointer, than it should have been safe to take a stack trace.

        ControllerStackInfo info;
        StackTraceTicket ticket(patch);
        info.GetStackInfo(ticket, thread, LEAF_MOST_FRAME, context);

        // !!! This check should really be != , but there is some ambiguity about which frame is the parent frame
        // in the destination returned from Frame::TraceFrame, so this allows some slop there.

        if (info.HasReturnFrame() && IsCloserToLeaf(info.m_returnFrame.fp, patch->fp))
        {
            LOG((LF_CORDB, LL_INFO10000, "Patch hit but frame not matched at %p (current=%p, patch=%p)\n",
                patch->address, info.m_returnFrame.fp.GetSPValue(), patch->fp.GetSPValue()));

            return false;
        }
    }

    LOG((LF_CORDB, LL_INFO100000, "DC::MP: Returning true"));

    return true;
}

DebuggerPatchSkip *DebuggerController::ActivatePatchSkip(Thread *thread,
                                                         const BYTE *PC,
                                                         BOOL fForEnC)
{
#ifdef _DEBUG
    BOOL shouldBreak = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_ActivatePatchSkip);
    if (shouldBreak > 0) {
        _ASSERTE(!"ActivatePatchSkip");
    }
#endif

    LOG((LF_CORDB,LL_INFO10000, "DC::APS thread=0x%p pc=0x%p fForEnc=%d\n",
        thread, PC, fForEnC));
    _ASSERTE(g_patches != NULL);

    //      Previously, we assumed that if we got to this point & the patch
    // was still there that we'd have to skip the patch.  SetIP changes
    // this like so:
    //      A breakpoint is set, and hit (but not removed), and all the
    // EE threads come to a skreeching halt.  The Debugger RC thread
    // continues along, and is told to SetIP of the thread that hit
    // the BP to whatever.  Eventually the RC thread is told to continue,
    // and at that point the EE thread is released, finishes DispatchPatchOrSingleStep,
    // and shows up here.
    //      At that point, if the thread's current PC is
    // different from the patch PC, then SetIP must have moved it elsewhere
    // & we shouldn't do this patch skip (which will put us back to where
    // we were, which is clearly wrong).  If the PC _is_ the same, then
    // the thread hasn't been moved, the patch is still in the code stream,
    // and we want to do the patch skip thing in order to execute this
    // instruction w/o removing it from the code stream.

    DebuggerControllerPatch *patch = g_patches->GetPatch((CORDB_ADDRESS_TYPE *)PC);
    DebuggerPatchSkip *skip = NULL;

    if (patch != NULL && patch->IsNativePatch())
    {
        //
        // We adjust the thread's PC to someplace where we write
        // the next instruction, then
        // we single step over that, then we set the PC back here so
        // we don't let other threads race past here while we're stepping
        // this one.
        //
        // !!! check result
        LOG((LF_CORDB,LL_INFO10000, "DC::APS: About to skip from PC=0x%p\n", PC));
        skip = new (interopsafe) DebuggerPatchSkip(thread, patch, thread->GetDomain());
        TRACE_ALLOC(skip);
    }

    return skip;
}

DPOSS_ACTION DebuggerController::ScanForTriggers(CORDB_ADDRESS_TYPE *address,
                                         Thread *thread,
                                         CONTEXT *context,
                                         DebuggerControllerQueue *pDcq,
                                         SCAN_TRIGGER stWhat,
                                         TP_RESULT *pTpr)
{
    CONTRACTL
    {
        // @todo - should this throw or not?
        NOTHROW;

        // call Triggers which may invoke GC stuff... See comment in DispatchNativeException for why it's disabled.
        DISABLED(GC_TRIGGERS);
        PRECONDITION(!ThisIsHelperThreadWorker());

        PRECONDITION(CheckPointer(address));
        PRECONDITION(CheckPointer(thread));
        PRECONDITION(CheckPointer(context));
        PRECONDITION(CheckPointer(pDcq));
        PRECONDITION(CheckPointer(pTpr));
    }
    CONTRACTL_END;

    _ASSERTE(HasLock());

    CONTRACT_VIOLATION(ThrowsViolation);

    LOG((LF_CORDB, LL_INFO10000, "DC::SFT: starting scan for addr:0x%p"
            " thread:0x%x\n", address, thread));

    _ASSERTE( pTpr != NULL );
    DebuggerControllerPatch *patch = NULL;

    if (g_patches != NULL)
        patch = g_patches->GetPatch(address);

    ULONG iEvent = UINT32_MAX;
    ULONG iEventNext = UINT32_MAX;
    BOOL fDone = FALSE;

    // This is a debugger exception if there's a patch here, or
    // we're here for something like a single step.
    DPOSS_ACTION used = DPOSS_INVALID;
    if ((patch != NULL) || !IsPatched(address, TRUE))
    {
        // we are sure that we care for this exception but not sure
        // if we will send event to the RS
        used = DPOSS_USED_WITH_NO_EVENT;
    }
    else
    {
        // initialize it to don't care for now
        used = DPOSS_DONT_CARE;
    }

    TP_RESULT tpr = TPR_IGNORE;

    while (stWhat & ST_PATCH &&
           patch != NULL &&
           !fDone)
    {
        _ASSERTE(IsInUsedAction(used) == true);

        DebuggerControllerPatch *patchNext
          = g_patches->GetNextPatch(patch);

        LOG((LF_CORDB, LL_INFO10000, "DC::SFT: patch 0x%x, patchNext 0x%x\n", patch, patchNext));

        // Annoyingly, TriggerPatch may add patches, which may cause
        // the patch table to move, which may, in turn, invalidate
        // the patch (and patchNext) pointers.  Store indeces, instead.
        iEvent = g_patches->GetItemIndex( (HASHENTRY *)patch );

        if (patchNext != NULL)
        {
            iEventNext = g_patches->GetItemIndex((HASHENTRY *)patchNext);
        }

        if (MatchPatch(thread, context, patch))
        {
            LOG((LF_CORDB, LL_INFO10000, "DC::SFT: patch matched\n"));
            AddRef(patch);

            // We are hitting a patch at a virtual trace call target, so let's trigger trace call here.
            if (patch->trace.GetTraceType() == TRACE_ENTRY_STUB)
            {
                patch->controller->TriggerTraceCall(thread, dac_cast<PTR_CBYTE>(::GetIP(context)));
                tpr = TPR_IGNORE;
            }
            else
            {
                // Mark if we're at an unsafe place.
                AtSafePlaceHolder unsafePlaceHolder(thread);

                tpr = patch->controller->TriggerPatch(patch,
                                                    thread,
                                                    TY_NORMAL);
            }

            // Any patch may potentially send an event.
            // (Whereas some single-steps are "internal-only" and can
            // never send an event- such as a single step over an exception that
            // lands us in la-la land.)
            used = DPOSS_USED_WITH_EVENT;

            if (tpr == TPR_TRIGGER ||
                tpr == TPR_TRIGGER_ONLY_THIS ||
                tpr == TPR_TRIGGER_ONLY_THIS_AND_LOOP)
            {
                // Make sure we've still got a valid pointer.
                patch = (DebuggerControllerPatch *)
                    DebuggerController::g_patches->GetEntryPtr( iEvent );

                pDcq->dcqEnqueue(patch->controller, TRUE); // <REVISIT_TODO>@todo Return value</REVISIT_TODO>
            }

            // Make sure we've got a valid pointer in case TriggerPatch
            // returned false but still caused the table to move.
            patch = (DebuggerControllerPatch *)
                g_patches->GetEntryPtr( iEvent );

            // A patch can be deleted as a result of it's being triggered.
            // The actual deletion of the patch is delayed until after the
            // the end of the trigger.
            // Moreover, "patchNext" could have been deleted as a result of DisableAll()
            // being called in TriggerPatch().  Thus, we should update our patchNext
            // pointer now.  We were just lucky before, because the now-deprecated
            // "deleted" flag didn't get set when we iterate the patches in DisableAll().
            patchNext = g_patches->GetNextPatch(patch);
            if (patchNext != NULL)
                iEventNext = g_patches->GetItemIndex((HASHENTRY *)patchNext);

            // Note that Release() actually removes the patch if its ref count
            // reaches 0 after the release.
            Release(patch);
        }

        if (tpr == TPR_IGNORE_AND_STOP ||
            tpr == TPR_TRIGGER_ONLY_THIS ||
            tpr == TPR_TRIGGER_ONLY_THIS_AND_LOOP)
        {
#ifdef _DEBUG
            if (tpr == TPR_TRIGGER_ONLY_THIS ||
                tpr == TPR_TRIGGER_ONLY_THIS_AND_LOOP)
                _ASSERTE(pDcq->dcqGetCount() == 1);
#endif //_DEBUG

            fDone = TRUE;
        }
        else if (patchNext != NULL)
        {
            patch = (DebuggerControllerPatch *)
                g_patches->GetEntryPtr(iEventNext);
        }
        else
        {
            patch = NULL;
        }
    }

#ifdef FEATURE_DATABREAKPOINT
    if (stWhat & ST_SINGLE_STEP &&
        tpr != TPR_TRIGGER_ONLY_THIS && 
        DebuggerDataBreakpoint::TriggerDataBreakpoint(thread, context))
    {
        DebuggerDataBreakpoint *pDataBreakpoint = new (interopsafe) DebuggerDataBreakpoint(thread);
        pDcq->dcqEnqueue(pDataBreakpoint, FALSE);
    }
#endif

    if (stWhat & ST_SINGLE_STEP &&
        tpr != TPR_TRIGGER_ONLY_THIS)
    {
        LOG((LF_CORDB, LL_INFO10000, "DC::SFT: Trigger controllers with single step\n"));

        //
        // Now, go ahead & trigger all controllers with
        // single step events
        //

        DebuggerController *p;

        p = g_controllers;
        while (p != NULL)
        {
            DebuggerController *pNext = p->m_next;

            if (p->m_thread == thread && p->m_singleStep)
            {
                if (used == DPOSS_DONT_CARE)
                {
                    // Debugger does care for this exception.
                    used = DPOSS_USED_WITH_NO_EVENT;
                }

                if (p->TriggerSingleStep(thread, (const BYTE *)address))
                {
                    // by now, we should already know that we care for this exception.
                    _ASSERTE(IsInUsedAction(used) == true);

                    // now we are sure that we will send event to the RS
                    used = DPOSS_USED_WITH_EVENT;
                    pDcq->dcqEnqueue(p, FALSE); // <REVISIT_TODO>@todo Return value</REVISIT_TODO>

                }
            }

            p = pNext;
        }

        UnapplyTraceFlag(thread);

        //
        // See if we have any steppers still active for this thread, if so
        // re-apply the trace flag.
        //

        p = g_controllers;
        while (p != NULL)
        {
            if (p->m_thread == thread && p->m_singleStep)
            {
                ApplyTraceFlag(thread);
                break;
            }

            p = p->m_next;
        }
    }

    // Significant speed increase from single dereference, I bet :)
    (*pTpr) = tpr;

    LOG((LF_CORDB, LL_INFO10000, "DC::SFT returning 0x%x as used\n",used));
    return used;
}

#ifdef EnC_SUPPORTED
DebuggerControllerPatch *DebuggerController::IsXXXPatched(const BYTE *PC,
                                                          DEBUGGER_CONTROLLER_TYPE dct)
{
    _ASSERTE(g_patches != NULL);

    DebuggerControllerPatch *patch = g_patches->GetPatch((CORDB_ADDRESS_TYPE *)PC);

    while(patch != NULL &&
          (int)patch->controller->GetDCType() <= (int)dct)
    {
        if (patch->IsNativePatch() &&
            patch->controller->GetDCType()==dct)
        {
            return patch;
        }
        patch = g_patches->GetNextPatch(patch);
    }

    return NULL;
}

// This function will check for an EnC patch at the given address and return
// it if one is there, otherwise it will return NULL.
DebuggerControllerPatch *DebuggerController::GetEnCPatch(const BYTE *address)
{
    _ASSERTE(address);

    if( g_pEEInterface->IsManagedNativeCode(address) )
    {
        DebuggerJitInfo *dji = g_pDebugger->GetJitInfoFromAddr((TADDR) address);
        if (dji == NULL)
            return NULL;

        // we can have two types of patches - one in code where the IL has been updated to trigger
        // the switch and the other in the code we've switched to in order to trigger FunctionRemapComplete
        // callback. If version == default then can't be the latter, but otherwise if haven't handled the
        // remap for this function yet is certainly the latter.
        if (! dji->m_encBreakpointsApplied &&
            (dji->m_encVersion == CorDB_DEFAULT_ENC_FUNCTION_VERSION))
        {
            return NULL;
        }
    }
    return IsXXXPatched(address, DEBUGGER_CONTROLLER_ENC);
}
#endif //EnC_SUPPORTED

// DebuggerController::DispatchPatchOrSingleStep - Ask any patches that are active at a given 
// address if they want to do anything about the exception that's occurred there.  How: For the given 
// address, go through the list of patches & see if any of them are interested (by invoking their 
// DebuggerController's TriggerPatch).  Put any DCs that are interested into a queue and then calls
// SendEvent on each.
// Note that control will not return from this function in the case of EnC remap
DPOSS_ACTION DebuggerController::DispatchPatchOrSingleStep(Thread *thread, CONTEXT *context, CORDB_ADDRESS_TYPE *address, SCAN_TRIGGER which)
{
    CONTRACT(DPOSS_ACTION)
    {
        // @todo - should this throw or not?
        NOTHROW;
        DISABLED(GC_TRIGGERS); // Only GC triggers if we send an event. See Comment in DispatchNativeException
        PRECONDITION(!ThisIsHelperThreadWorker());

        PRECONDITION(CheckPointer(thread));
        PRECONDITION(CheckPointer(context));
        PRECONDITION(CheckPointer(address));
        PRECONDITION(!HasLock());

        POSTCONDITION(!HasLock()); // make sure we're not leaking the controller lock
    }
    CONTRACT_END;

    CONTRACT_VIOLATION(ThrowsViolation);

    LOG((LF_CORDB|LF_ENC,LL_INFO1000,"DC:DPOSS at 0x%p trigger:0x%x\n", address, which));

    // We should only have an exception if some managed thread was running.
    // Thus we should never be here when we're stopped.
    // @todo - this assert fires! Is that an issue, or is it invalid?
    //_ASSERTE(!g_pDebugger->IsStopped());
    DPOSS_ACTION used = DPOSS_DONT_CARE;

    DebuggerControllerQueue dcq;
    if (!g_patchTableValid)
    {

        LOG((LF_CORDB|LF_ENC, LL_INFO1000, "DC::DPOSS returning, no patch table.\n"));
        RETURN (used);
    }
    _ASSERTE(g_patches != NULL);
    
    CrstHolderWithState lockController(&g_criticalSection);

    TADDR originalAddress = 0;

#ifdef EnC_SUPPORTED
    DebuggerControllerPatch *dcpEnCOriginal = NULL;

    // If this sequence point has an EnC patch, we want to process it ahead of any others. If the
    // debugger wants to remap the function at this point, then we'll call ResumeInUpdatedFunction and
    // not return, otherwise we will just continue with regular patch-handling logic
    dcpEnCOriginal = GetEnCPatch(dac_cast<PTR_CBYTE>(GetIP(context)));

    if (dcpEnCOriginal)
    {
        LOG((LF_CORDB|LF_ENC,LL_INFO10000, "DC::DPOSS EnC short-circuit\n"));
        TP_RESULT tpres =
            dcpEnCOriginal->controller->TriggerPatch(dcpEnCOriginal,
                                                     thread,
                                                     TY_SHORT_CIRCUIT);

        // We will only come back here on a RemapOpportunity that wasn't taken, or on a RemapComplete.
        // If we processed a RemapComplete (which returns TPR_IGNORE_AND_STOP), then don't want to handle
        // additional breakpoints on the current line because we've already effectively executed to that point
        // and would have hit them already. If they are new, we also don't want to hit them because eg. if are
        // sitting on line 10 and add a breakpoint at line 10 and step,
        // don't expect to stop at line 10, expect to go to line 11.
        //
        // Special case is if an EnC remap breakpoint exists in the function. This could only happen if the function was
        // updated between the RemapOpportunity and the RemapComplete.  In that case we want to not skip the patches
        // and fall through to handle the remap breakpoint.

        if (tpres == TPR_IGNORE_AND_STOP)
        {
            // It was a RemapComplete, so fall through.  Set dcpEnCOriginal to NULL to indicate that any
            // EnC patch still there should be treated as a new patch.  Any RemapComplete patch will have been
            // already removed by patch processing.
            dcpEnCOriginal = NULL;
            LOG((LF_CORDB|LF_ENC,LL_INFO10000, "DC::DPOSS done EnC short-circuit, exiting\n"));
            used = DPOSS_USED_WITH_EVENT;    // indicate that we handled a patch
            goto Exit;
        }

        _ASSERTE(tpres==TPR_IGNORE);
        LOG((LF_CORDB|LF_ENC,LL_INFO10000, "DC::DPOSS done EnC short-circuit, ignoring\n"));
        // if we got here, then the EnC remap opportunity was not taken, so just continue on.
    }
#endif // EnC_SUPPORTED

    TP_RESULT tpr;

    used = ScanForTriggers((CORDB_ADDRESS_TYPE *)address, thread, context, &dcq, which, &tpr);

    LOG((LF_CORDB|LF_ENC, LL_EVERYTHING, "DC::DPOSS ScanForTriggers called and returned.\n"));


    // If we setip, then that will change the address in the context.
    // Remeber the old address so that we can compare it to the context's ip and see if it changed.
    // If it did change, then don't dispatch our current event.
    originalAddress = (TADDR) address;

#ifdef _DEBUG
    // If we do a SetIP after this point, the value of address will be garbage.  Set it to a distictive pattern now, so
    // we don't accidentally use what will (98% of the time) appear to be a valid value.
    address = (CORDB_ADDRESS_TYPE *)(UINT_PTR)0xAABBCCFF;
#endif //_DEBUG

    if (dcq.dcqGetCount()> 0)
    {        
        lockController.Release();

        // Mark if we're at an unsafe place.
        bool atSafePlace = g_pDebugger->IsThreadAtSafePlace(thread);
        if (!atSafePlace)
            g_pDebugger->IncThreadsAtUnsafePlaces();

        DWORD dwEvent = 0xFFFFFFFF;
        DWORD dwNumberEvents = 0;
        BOOL reabort = FALSE;

        SENDIPCEVENT_BEGIN(g_pDebugger, thread);

        // Now that we've resumed from blocking, check if somebody did a SetIp on us.
        bool fIpChanged = (originalAddress != GetIP(context));

        // Send the events outside of the controller lock
        bool anyEventsSent = false;

        dwNumberEvents = dcq.dcqGetCount();
        dwEvent = 0;

        while (dwEvent < dwNumberEvents)
        {
            DebuggerController *event = dcq.dcqGetElement(dwEvent);

            if (!event->m_deleted)
            {
#ifdef DEBUGGING_SUPPORTED
                if (thread->GetDomain()->IsDebuggerAttached())
                {
                    if (event->SendEvent(thread, fIpChanged))
                    {
                        anyEventsSent = true;
                    }
                }
#endif //DEBUGGING_SUPPORTED
            }

            dwEvent++;
        }

        // Trap all threads if necessary, but only if we actually sent a event up (i.e., all the queued events weren't
        // deleted before we got a chance to get the EventSending lock.)
        if (anyEventsSent)
        {
            LOG((LF_CORDB|LF_ENC, LL_EVERYTHING, "DC::DPOSS We sent an event\n"));
            g_pDebugger->SyncAllThreads(SENDIPCEVENT_PtrDbgLockHolder);
            LOG((LF_CORDB,LL_INFO1000, "SAT called!\n"));
        }


        // If we need to to a re-abort (see below), then save the current IP in the thread's context before we block and
        // possibly let another func eval get setup.
        reabort = thread->m_StateNC & Thread::TSNC_DebuggerReAbort;
        SENDIPCEVENT_END;

        if (!atSafePlace)
            g_pDebugger->DecThreadsAtUnsafePlaces();

        lockController.Acquire();

        // Dequeue the events while we have the controller lock.
        dwEvent = 0;
        while (dwEvent < dwNumberEvents)
        {
            dcq.dcqDequeue();
            dwEvent++;
        }
        // If a func eval completed with a ThreadAbortException, go ahead and setup the thread to re-abort itself now
        // that we're continuing the thread. Note: we make sure that the thread's IP hasn't changed between now and when
        // we blocked above. While blocked above, the debugger has a chance to setup another func eval on this
        // thread. If that happens, we don't want to setup the reabort just yet.
        if (reabort)
        {
            if ((UINT_PTR)GetEEFuncEntryPoint(::FuncEvalHijack) != (UINT_PTR)GetIP(context))
            {
                HRESULT hr;
                hr = g_pDebugger->FuncEvalSetupReAbort(thread, Thread::TAR_Thread);
                _ASSERTE(SUCCEEDED(hr));
            }
        }
    }

#if defined EnC_SUPPORTED
Exit:
#endif

    // Note: if the thread filter context is NULL, then SetIP would have failed & thus we should do the 
    // patch skip thing.
    // @todo  - do we need to get the context again here?
    CONTEXT *pCtx = GetManagedLiveCtx(thread);

#ifdef EnC_SUPPORTED
    DebuggerControllerPatch *dcpEnCCurrent = GetEnCPatch(dac_cast<PTR_CBYTE>((GetIP(context))));

    // we have a new patch if the original was null and the current is non-null. Otherwise we have an old 
    // patch. We want to skip old patches, but handle new patches.
    if (dcpEnCOriginal == NULL && dcpEnCCurrent != NULL)
    {
        LOG((LF_CORDB|LF_ENC,LL_INFO10000, "DC::DPOSS EnC post-processing\n"));
            dcpEnCCurrent->controller->TriggerPatch( dcpEnCCurrent,
                                                     thread,
                                                     TY_SHORT_CIRCUIT);
        used = DPOSS_USED_WITH_EVENT;    // indicate that we handled a patch
    }
#endif

    ActivatePatchSkip(thread, dac_cast<PTR_CBYTE>(GetIP(pCtx)), FALSE);

    lockController.Release();


    // We pulse the GC mode here too cooperate w/ a thread trying to suspend the runtime. If we didn't pulse
    // the GC, the odds of catching this thread in interuptable code may be very small (since this filter
    // could be very large compared to the managed code this thread is running).
    // Only do this if the exception was actually for the debugger. (We don't want to toggle the GC mode on every
    // random exception). We can't do this while holding any debugger locks.
    if (used == DPOSS_USED_WITH_EVENT)
    {
        bool atSafePlace = g_pDebugger->IsThreadAtSafePlace(thread);
        if (!atSafePlace)
        {
            g_pDebugger->IncThreadsAtUnsafePlaces();
        }

        // Always pulse the GC mode. This will allow an async break to complete even if we have a patch
        // at an unsafe place.
        // If we are at an unsafe place, then we can't do a GC.
        thread->PulseGCMode();

        if (!atSafePlace)
        {
            g_pDebugger->DecThreadsAtUnsafePlaces();
        }

    }

    RETURN used;
}

bool DebuggerController::IsSingleStepEnabled()
{
    LIMITED_METHOD_CONTRACT;
    return m_singleStep;
}

void DebuggerController::EnableSingleStep()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

#ifdef _DEBUG
    // Some controllers don't need to set the SS to do their job, and if they are setting it, it's likely an issue.
    // So we assert here to catch them red-handed. This assert can always be updated to accomodate changes
    // in a controller's behavior.

    switch(GetDCType())
    {
        case DEBUGGER_CONTROLLER_THREAD_STARTER:
        case DEBUGGER_CONTROLLER_BREAKPOINT:
        case DEBUGGER_CONTROLLER_USER_BREAKPOINT:
        case DEBUGGER_CONTROLLER_FUNC_EVAL_COMPLETE:
            CONSISTENCY_CHECK_MSGF(false, ("Controller pThis=%p shouldn't be setting ss flag.", this));
            break;
        default: // MingW compilers require all enum cases to be handled in switch statement.
            break;
    }
#endif

    EnableSingleStep(m_thread);
    m_singleStep = true;
}

#ifdef EnC_SUPPORTED
// Note that this doesn't tell us if Single Stepping is currently enabled
// at the hardware level (ie, for x86, if (context->EFlags & 0x100), but
// rather, if we WANT single stepping enabled (pThread->m_State &Thread::TS_DebuggerIsStepping)
// This gets called from exactly one place - ActivatePatchSkipForEnC
BOOL DebuggerController::IsSingleStepEnabled(Thread *pThread)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    // This should be an atomic operation, do we
    // don't need to lock it.
    if(pThread->m_StateNC & Thread::TSNC_DebuggerIsStepping)
    {
        _ASSERTE(pThread->m_StateNC & Thread::TSNC_DebuggerIsStepping);

        return TRUE;
    }
    else
        return FALSE;
}
#endif //EnC_SUPPORTED

void DebuggerController::EnableSingleStep(Thread *pThread)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    LOG((LF_CORDB,LL_INFO1000, "DC::EnableSingleStep\n"));

    _ASSERTE(pThread != NULL);

    ControllerLockHolder lockController;
    
    ApplyTraceFlag(pThread);
}

// Disable Single stepping for this controller.
// If none of the controllers on this thread want single-stepping, then also
// ensure that it's disabled on the hardware level.
void DebuggerController::DisableSingleStep()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    _ASSERTE(m_thread != NULL);

    LOG((LF_CORDB,LL_INFO1000, "DC::DisableSingleStep\n"));

    ControllerLockHolder lockController;
    {
        DebuggerController *p = g_controllers;

        m_singleStep = false;

        while (p != NULL)
        {
            if (p->m_thread == m_thread
                && p->m_singleStep)
                break;

            p = p->m_next;
        }

        if (p == NULL)
        {
            UnapplyTraceFlag(m_thread);
        }
    }
}


//
// ApplyTraceFlag sets the trace flag (i.e., turns on single-stepping)
// for a thread.
//
void DebuggerController::ApplyTraceFlag(Thread *thread)
{
    LOG((LF_CORDB,LL_INFO1000, "DC::ApplyTraceFlag thread:0x%x [0x%0x]\n", thread, Debugger::GetThreadIdHelper(thread)));

    CONTEXT *context;
    if(thread->GetInteropDebuggingHijacked())
    {
        context = GetManagedLiveCtx(thread);
    }
    else
    {
        context = GetManagedStoppedCtx(thread);
    }
    CONSISTENCY_CHECK_MSGF(context != NULL, ("Can't apply ss flag to thread 0x%p b/c it's not in a safe place.\n", thread));
    PREFIX_ASSUME(context != NULL);


    g_pEEInterface->MarkThreadForDebugStepping(thread, true);
    LOG((LF_CORDB,LL_INFO1000, "DC::ApplyTraceFlag marked thread for debug stepping\n"));
    
    SetSSFlag(reinterpret_cast<DT_CONTEXT *>(context) ARM_ARG(thread) ARM64_ARG(thread));
    LOG((LF_CORDB,LL_INFO1000, "DC::ApplyTraceFlag Leaving, baby!\n"));
}

//
// UnapplyTraceFlag sets the trace flag for a thread.
// Removes the hardware trace flag on this thread.
//

void DebuggerController::UnapplyTraceFlag(Thread *thread)
{
    LOG((LF_CORDB,LL_INFO1000, "DC::UnapplyTraceFlag thread:0x%x\n", thread));


    // Either this is the helper thread, or we're manipulating our own context.
    _ASSERTE(
        ThisIsHelperThreadWorker() ||
        (thread == ::GetThread())
    );

    CONTEXT *context = GetManagedStoppedCtx(thread);

    // If there's no context available, then the thread shouldn't have the single-step flag
    // enabled and there's nothing for us to do.
    if (context == NULL)
    {
        // In theory, I wouldn't expect us to ever get here.
        // Even if we are here, our single-step flag should already be deactivated,
        // so there should be nothing to do. However, we still assert b/c we want to know how
        // we'd actually hit this.
        // @todo - is there a path if TriggerUnwind() calls DisableAll(). But why would
        CONSISTENCY_CHECK_MSGF(false, ("How did we get here?. thread=%p\n", thread));
        LOG((LF_CORDB,LL_INFO1000, "DC::UnapplyTraceFlag couldn't get context.\n"));
        return;
    }

    // Always need to unmark for stepping
    g_pEEInterface->MarkThreadForDebugStepping(thread, false);
    UnsetSSFlag(reinterpret_cast<DT_CONTEXT *>(context) ARM_ARG(thread) ARM64_ARG(thread));
}

void DebuggerController::EnableExceptionHook()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    _ASSERTE(m_thread != NULL);

    ControllerLockHolder lockController;
    
    m_exceptionHook = true;    
}

void DebuggerController::DisableExceptionHook()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    _ASSERTE(m_thread != NULL);

    ControllerLockHolder lockController;
    m_exceptionHook = false;
}


// void DebuggerController::DispatchExceptionHook()  Called before
// the switch statement in DispatchNativeException (therefore
// when any exception occurs), this allows patches to do something before the
// regular DispatchX methods.
// How: Iterate through list of controllers.  If m_exceptionHook
// is set & m_thread is either thread or NULL, then invoke TriggerExceptionHook()
BOOL DebuggerController::DispatchExceptionHook(Thread *thread,
                                               CONTEXT *context,
                                               EXCEPTION_RECORD *pException)
{
    // ExceptionHook has restrictive contract b/c it could come from anywhere.
    // This can only modify controller's internal state. Can't send managed debug events.
    CONTRACTL
    {
        GC_NOTRIGGER;
        NOTHROW;
        MODE_ANY;

        // Filter context not set yet b/c we can only set it in COOP, and this may be in preemptive.
        PRECONDITION(thread == ::GetThread());
        PRECONDITION((g_pEEInterface->GetThreadFilterContext(thread) == NULL));
        PRECONDITION(CheckPointer(pException));
    }
    CONTRACTL_END;

    LOG((LF_CORDB, LL_INFO1000, "DC:: DispatchExceptionHook\n"));

    if (!g_patchTableValid)
    {
        LOG((LF_CORDB, LL_INFO1000, "DC::DEH returning, no patch table.\n"));
        return (TRUE);
    }

 
    _ASSERTE(g_patches != NULL);

    ControllerLockHolder lockController;

    TP_RESULT tpr = TPR_IGNORE;
    DebuggerController *p;

    p = g_controllers;
    while (p != NULL)
    {
        DebuggerController *pNext = p->m_next;

        if (p->m_exceptionHook
            && (p->m_thread == NULL || p->m_thread == thread) &&
            tpr != TPR_IGNORE_AND_STOP)
        {
                        LOG((LF_CORDB, LL_INFO1000, "DC::DEH calling TEH...\n"));
            tpr = p->TriggerExceptionHook(thread, context , pException);
                        LOG((LF_CORDB, LL_INFO1000, "DC::DEH ... returned.\n"));

            if (tpr == TPR_IGNORE_AND_STOP)
            {
                LOG((LF_CORDB, LL_INFO1000, "DC:: DEH: leaving early!\n"));
                break;
            }
        }

        p = pNext;
    }

    LOG((LF_CORDB, LL_INFO1000, "DC:: DEH: returning 0x%x!\n", tpr));

    return (tpr != TPR_IGNORE_AND_STOP);
}

//
// EnableUnwind enables an unwind event to be called when the stack is unwound
// (via an exception) to or past the given pointer.
//

void DebuggerController::EnableUnwind(FramePointer fp)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    ASSERT(m_thread != NULL);
    LOG((LF_CORDB,LL_EVERYTHING,"DC:EU EnableUnwind at 0x%x\n", fp.GetSPValue()));

    ControllerLockHolder lockController;
    m_unwindFP = fp;
}

FramePointer DebuggerController::GetUnwind()
{
    LIMITED_METHOD_CONTRACT;

    return m_unwindFP;
}

//
// DisableUnwind disables the unwind event for the controller.
//

void DebuggerController::DisableUnwind()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_ANY;
        CAN_TAKE_LOCK;
    }
    CONTRACTL_END;

    ASSERT(m_thread != NULL);

    LOG((LF_CORDB,LL_INFO1000, "DC::DU\n"));

    ControllerLockHolder lockController;
    
    m_unwindFP = LEAF_MOST_FRAME;    
}

//
// DispatchUnwind is called when an unwind happens.
// the event to the appropriate controllers.
// - handlerFP is the frame pointer that the handler will be invoked at.
// - DJI is EnC-aware method that the handler is in.
// - newOffset is the 
//
bool DebuggerController::DispatchUnwind(Thread *thread,
                                        MethodDesc *fd, DebuggerJitInfo * pDJI, 
                                        SIZE_T newOffset,
                                        FramePointer handlerFP,
                                        CorDebugStepReason unwindReason)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER; // don't send IPC events
        MODE_COOPERATIVE; // TriggerUnwind always is coop

        PRECONDITION(!IsDbgHelperSpecialThread());
    }
    CONTRACTL_END;


    CONTRACT_VIOLATION(ThrowsViolation); // trigger unwind throws

    _ASSERTE(unwindReason == STEP_EXCEPTION_FILTER || unwindReason == STEP_EXCEPTION_HANDLER);

    bool used = false;

    LOG((LF_CORDB, LL_INFO10000, "DC: Dispatch Unwind\n"));

    ControllerLockHolder lockController;
    {
        DebuggerController *p;

        p = g_controllers;

        while (p != NULL)
        {
            DebuggerController *pNext = p->m_next;

            if (p->m_thread == thread && p->m_unwindFP != LEAF_MOST_FRAME)
            {
                LOG((LF_CORDB, LL_INFO10000, "Dispatch Unwind: Found candidate\n"));


                //  Assumptions here:
                //      Function with handlers are -ALWAYS- EBP-frame based (JIT assumption)
                //
                //      newFrame is the EBP for the handler
                //      p->m_unwindFP points to the stack slot with the return address of the function.
                //
                //  For the interesting case: stepover, we want to know if the handler is in the same function
                //  as the stepper, if its above it (caller) o under it (callee) in order to know if we want
                //  to patch the handler or not.
                //
                //  3 cases:
                //
                //      a) Handler is in a function under the function where the step happened. It therefore is
                //         a stepover. We don't want to patch this handler. The handler will have an EBP frame.
                //         So it will be at least be 2 DWORDs away from the m_unwindFP of the controller (
                //         1 DWORD from the pushed return address and 1 DWORD for the push EBP).
                //
                //      b) Handler is in the same function as the stepper. We want to patch the handler. In this
                //         case handlerFP will be the same as p->m_unwindFP-sizeof(void*). Why? p->m_unwindFP
                //         stores a pointer to the return address of the function. As a function with a handler
                //         is always EBP frame based it will have the following code in the prolog:
                //
                //                  push ebp        <- ( sub esp, 4 ; mov [esp], ebp )
                //                  mov  esp, ebp
                //
                //         Therefore EBP will be equal to &CallerReturnAddress-4.
                //
                //      c) Handler is above the function where the stepper is. We want to patch the handler. handlerFP
                //         will be always greater than the pointer to the return address of the function where the
                //         stepper is.
                //
                //
                //

                if (IsEqualOrCloserToRoot(handlerFP, p->m_unwindFP))
                {
                    used = true;

                    //
                    // Assume that this isn't going to block us at all --
                    // other threads may be waiting to patch or unpatch something,
                    // or to dispatch.
                    //
                    LOG((LF_CORDB, LL_INFO10000,
                        "Unwind trigger at offset 0x%p; handlerFP: 0x%p unwindReason: 0x%x.\n",
                         newOffset, handlerFP.GetSPValue(), unwindReason));

                    p->TriggerUnwind(thread,
                                     fd, pDJI,
                                     newOffset,
                                     handlerFP,
                                     unwindReason);
                }
                else
                {
                    LOG((LF_CORDB, LL_INFO10000,
                        "Unwind trigger at offset 0x%p; handlerFP: 0x%p unwindReason: 0x%x.\n",
                         newOffset, handlerFP.GetSPValue(), unwindReason));
                }
            }

            p = pNext;
        }
    }

    return used;
}

//
// EnableTraceCall enables a call event on the controller
// maxFrame is the leaf-most frame that we want notifications for.
// For step-in stuff, this will always be LEAF_MOST_FRAME.
// for step-out, this will be the current frame because we don't
// care if the current frame calls back into managed code when we're
// only interested in our parent frames.
//

void DebuggerController::EnableTraceCall(FramePointer maxFrame)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    ASSERT(m_thread != NULL);

    LOG((LF_CORDB,LL_INFO1000, "DC::ETC maxFrame=0x%x, thread=0x%x\n",
         maxFrame.GetSPValue(), Debugger::GetThreadIdHelper(m_thread)));

    // JMC stepper should never enabled this. (They should enable ME instead).
    _ASSERTE((DEBUGGER_CONTROLLER_JMC_STEPPER != this->GetDCType()) || !"JMC stepper shouldn't enable trace-call");


    ControllerLockHolder lockController;
    {
        if (!m_traceCall)
        {
            m_traceCall = true;
            g_pEEInterface->EnableTraceCall(m_thread);
        }

        if (IsCloserToLeaf(maxFrame, m_traceCallFP))
            m_traceCallFP = maxFrame;
    }    
}

struct PatchTargetVisitorData
{
    DebuggerController* controller;
    FramePointer maxFrame;
};

VOID DebuggerController::PatchTargetVisitor(TADDR pVirtualTraceCallTarget, VOID* pUserData)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    DebuggerController* controller = ((PatchTargetVisitorData*) pUserData)->controller;
    FramePointer maxFrame = ((PatchTargetVisitorData*) pUserData)->maxFrame;

    EX_TRY
    {
        CONTRACT_VIOLATION(GCViolation);    // PatchTrace throws, which implies GC-triggers
        TraceDestination trace;
        trace.InitForUnmanagedStub(pVirtualTraceCallTarget);
        controller->PatchTrace(&trace, maxFrame, true);
    }
    EX_CATCH
    {
        // not much we can do here
    }
    EX_END_CATCH(SwallowAllExceptions)
}

//
// DisableTraceCall disables call events on the controller
//

void DebuggerController::DisableTraceCall()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    ASSERT(m_thread != NULL);

    ControllerLockHolder lockController;
    {
        if (m_traceCall)
        {
            LOG((LF_CORDB,LL_INFO1000, "DC::DTC thread=0x%x\n",
             Debugger::GetThreadIdHelper(m_thread)));

            g_pEEInterface->DisableTraceCall(m_thread);            

            m_traceCall = false;
            m_traceCallFP = ROOT_MOST_FRAME;
        }
    }    
}

// Get a FramePointer for the leafmost frame on this thread's stacktrace.
// It's tempting to create this off the head of the Frame chain, but that may
// include internal EE Frames (like GCRoot frames) which a FrameInfo-stackwalk may skip over.
// Thus using the Frame chain would err on the side of returning a FramePointer that
// closer to the leaf.
FramePointer GetCurrentFramePointerFromStackTraceForTraceCall(Thread * thread)
{
    _ASSERTE(thread != NULL);

    // Ensure this is really the same as CSI.
    ControllerStackInfo info;

    // It's possible this stackwalk may be done at an unsafe time.
    // this method may trigger a GC, for example, in
    // FramedMethodFrame::AskStubForUnmanagedCallSite
    // which will trash the incoming argument array
    // which is not gc-protected.

    // We could probably imagine a more specialized stackwalk that
    // avoids these calls and is thus GC_NOTRIGGER.
    CONTRACT_VIOLATION(GCViolation);

    // This is being run live, so there's no filter available.
    CONTEXT *context;
    context = g_pEEInterface->GetThreadFilterContext(thread);
    _ASSERTE(context == NULL);
    _ASSERTE(!ISREDIRECTEDTHREAD(thread));

    // This is actually safe because we're coming from a TraceCall, which
    // means we're not in the middle of a stub. We don't have some partially
    // constructed frame, so we can safely traverse the stack.
    // However, we may still have a problem w/ the GC-violation.
    StackTraceTicket ticket(StackTraceTicket::SPECIAL_CASE_TICKET);
    info.GetStackInfo(ticket, thread, LEAF_MOST_FRAME, NULL);

    FramePointer fp = info.m_activeFrame.fp;

    return fp;
}
//
// DispatchTraceCall is called when a call is traced in the EE
// It dispatches the event to the appropriate controllers.
//

bool DebuggerController::DispatchTraceCall(Thread *thread,
                                           const BYTE *ip)
{
    CONTRACTL
    {
        GC_NOTRIGGER;
        THROWS;
    }
    CONTRACTL_END;

    bool used = false;

    LOG((LF_CORDB, LL_INFO10000,
         "DC::DTC: TraceCall at 0x%x\n", ip));

    ControllerLockHolder lockController;
    {
        DebuggerController *p;

        p = g_controllers;
        while (p != NULL)
        {
            DebuggerController *pNext = p->m_next;

            if (p->m_thread == thread && p->m_traceCall)
            {
                bool trigger;

                if (p->m_traceCallFP == LEAF_MOST_FRAME)
                    trigger = true;
                else
                {
                    // We know we don't have a filter context, so get a frame pointer from our frame chain.
                    FramePointer fpToCheck = GetCurrentFramePointerFromStackTraceForTraceCall(thread);


                    // <REVISIT_TODO>
                    //
                    // Currently, we never ever put a patch in an IL stub, and as such, if the IL stub
                    // throws an exception after returning from unmanaged code, we would not trigger
                    // a trace call when we call the constructor of the exception.  The following is
                    // kind of a workaround to make that working.  If we ever make the change to stop in
                    // IL stubs (for example, if we start to share security IL stub), then this can be
                    // removed.                                                    
                    //
                    // </REVISIT_TODO>



                    // It's possible this stackwalk may be done at an unsafe time.
                    // this method may trigger a GC, for example, in
                    // FramedMethodFrame::AskStubForUnmanagedCallSite
                    // which will trash the incoming argument array
                    // which is not gc-protected.
                    ControllerStackInfo info;
                    {
                        CONTRACT_VIOLATION(GCViolation);
#ifdef _DEBUG
                        CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);
#endif // _DEBUG
                        _ASSERTE(context == NULL);
                        _ASSERTE(!ISREDIRECTEDTHREAD(thread));

                        // See explanation in GetCurrentFramePointerFromStackTraceForTraceCall.
                        StackTraceTicket ticket(StackTraceTicket::SPECIAL_CASE_TICKET);
                        info.GetStackInfo(ticket, thread, LEAF_MOST_FRAME, NULL);
                    }

                    if (info.m_activeFrame.chainReason == CHAIN_ENTER_UNMANAGED)
                    {
                        _ASSERTE(info.HasReturnFrame());

                        // This check makes sure that we don't do this logic for inlined frames.
                        if (info.m_returnFrame.md->IsILStub())
                        {
                            // Make sure that the frame pointer of the active frame is actually
                            // the address of an exit frame.
                            _ASSERTE( (static_cast<Frame*>(info.m_activeFrame.fp.GetSPValue()))->GetFrameType()
                                      == Frame::TYPE_EXIT );
                            _ASSERTE(!info.m_returnFrame.HasChainMarker());
                            fpToCheck = info.m_returnFrame.fp;
                        }
                    }

                    // @todo - This comparison seems somewhat nonsensical. We don't have a filter context
                    // in place, so what frame pointer is fpToCheck actually for?
                    trigger = IsEqualOrCloserToRoot(fpToCheck, p->m_traceCallFP);
                }

                if (trigger)
                {
                    used = true;

                    // This can only update controller's state, can't actually send IPC events.
                    p->TriggerTraceCall(thread, ip);
                }
            }

            p = pNext;
        }
    }    

    return used;
}

bool DebuggerController::IsMethodEnterEnabled()
{
    LIMITED_METHOD_CONTRACT;
    return m_fEnableMethodEnter;
}


// Notify dispatching logic that this controller wants to get TriggerMethodEnter
// We keep a count of total controllers waiting for MethodEnter (in g_cTotalMethodEnter).
// That way we know if any controllers want MethodEnter callbacks. If none do,
// then we can set the JMC probe flag to false for all modules.
void DebuggerController::EnableMethodEnter()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    ControllerLockHolder chController;
    Debugger::DebuggerDataLockHolder chInfo(g_pDebugger);

    // Both JMC + Traditional steppers may use MethodEnter.
    // For JMC, it's a core part of functionality. For Traditional steppers, we use it as a backstop
    // in case the stub-managers fail.
    _ASSERTE(g_cTotalMethodEnter >= 0);
    if (!m_fEnableMethodEnter)
    {
        LOG((LF_CORDB, LL_INFO1000000, "DC::EnableME, this=%p, previously disabled\n", this));
        m_fEnableMethodEnter = true;

        g_cTotalMethodEnter++;
    }
    else
    {
        LOG((LF_CORDB, LL_INFO1000000, "DC::EnableME, this=%p, already set\n", this));
    }    
    g_pDebugger->UpdateAllModuleJMCFlag(g_cTotalMethodEnter != 0); // Needs JitInfo lock
}

// Notify dispatching logic that this controller doesn't want to get
// TriggerMethodEnter
void DebuggerController::DisableMethodEnter()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    ControllerLockHolder chController;
    Debugger::DebuggerDataLockHolder chInfo(g_pDebugger);
        
    if (m_fEnableMethodEnter)
    {
        LOG((LF_CORDB, LL_INFO1000000, "DC::DisableME, this=%p, previously set\n", this));
        m_fEnableMethodEnter = false;

        g_cTotalMethodEnter--;
        _ASSERTE(g_cTotalMethodEnter >= 0);
    }
    else
    {
        LOG((LF_CORDB, LL_INFO1000000, "DC::DisableME, this=%p, already disabled\n", this));
    }

    g_pDebugger->UpdateAllModuleJMCFlag(g_cTotalMethodEnter != 0); // Needs JitInfo lock
}

// Loop through controllers and dispatch TriggerMethodEnter
void DebuggerController::DispatchMethodEnter(void * pIP, FramePointer fp)
{
    _ASSERTE(pIP != NULL);

    Thread * pThread = g_pEEInterface->GetThread();
    _ASSERTE(pThread  != NULL);

    // Lookup the DJI for this method & ip.
    // Since we create DJIs when we jit the code, and this code has been jitted
    // (that's where the probe's coming from!), we will have a DJI.
    DebuggerJitInfo * dji = g_pDebugger->GetJitInfoFromAddr((TADDR) pIP);

    // This includes the case where we have a LightWeight codegen method.
    if (dji == NULL)
    {
        return;
    }

    LOG((LF_CORDB, LL_INFO100000, "DC::DispatchMethodEnter for '%s::%s'\n",
        dji->m_nativeCodeVersion.GetMethodDesc()->m_pszDebugClassName,
        dji->m_nativeCodeVersion.GetMethodDesc()->m_pszDebugMethodName));

    ControllerLockHolder lockController;

    // For debug check, keep a count to make sure that g_cTotalMethodEnter
    // is actually the number of controllers w/ MethodEnter enabled.
    int count = 0;

    DebuggerController *p = g_controllers;
    while (p != NULL)
    {
        if (p->m_fEnableMethodEnter)
        {
            if ((p->GetThread() == NULL) || (p->GetThread() == pThread))
            {
                ++count;
                p->TriggerMethodEnter(pThread, dji, (const BYTE *) pIP, fp);   
            }
        }
        p = p->m_next;
    }

    _ASSERTE(g_cTotalMethodEnter == count);
    
}

//
// AddProtection adds page protection to (at least) the given range of
// addresses
//

void DebuggerController::AddProtection(const BYTE *start, const BYTE *end,
                                       bool readable)
{
    // !!!
    _ASSERTE(!"Not implemented yet");
}

//
// RemoveProtection removes page protection from the given
// addresses. The parameters should match an earlier call to
// AddProtection
//

void DebuggerController::RemoveProtection(const BYTE *start, const BYTE *end,
                                          bool readable)
{
    // !!!
    _ASSERTE(!"Not implemented yet");
}


// Default implementations for FuncEvalEnter & Exit notifications.
void DebuggerController::TriggerFuncEvalEnter(Thread * thread)
{
    LOG((LF_CORDB, LL_INFO100000, "DC::TFEEnter, thead=%p, this=%p\n", thread, this));
}

void DebuggerController::TriggerFuncEvalExit(Thread * thread)
{
    LOG((LF_CORDB, LL_INFO100000, "DC::TFEExit, thead=%p, this=%p\n", thread, this));
}

// bool DebuggerController::TriggerPatch()   What: Tells the
// static DC whether this patch should be activated now.
// Returns true if it should be, false otherwise.
// How: Base class implementation returns false.  Others may
// return true.
TP_RESULT DebuggerController::TriggerPatch(DebuggerControllerPatch *patch,
                              Thread *thread,
                              TRIGGER_WHY tyWhy)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::TP: in default TriggerPatch\n"));
    return TPR_IGNORE;
}

bool DebuggerController::TriggerSingleStep(Thread *thread,
                                           const BYTE *ip)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::TP: in default TriggerSingleStep\n"));
    return false;
}

void DebuggerController::TriggerUnwind(Thread *thread,
                                       MethodDesc *fd, DebuggerJitInfo * pDJI, SIZE_T offset,
                                       FramePointer fp,
                                       CorDebugStepReason unwindReason)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::TP: in default TriggerUnwind\n"));
}

void DebuggerController::TriggerTraceCall(Thread *thread,
                                          const BYTE *ip)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::TP: in default TriggerTraceCall\n"));
}

TP_RESULT DebuggerController::TriggerExceptionHook(Thread *thread, CONTEXT * pContext,
                                              EXCEPTION_RECORD *exception)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::TP: in default TriggerExceptionHook\n"));
    return TPR_IGNORE;
}

void DebuggerController::TriggerMethodEnter(Thread * thread,
                                            DebuggerJitInfo * dji,
                                            const BYTE * ip,
                                            FramePointer fp)
{
    LOG((LF_CORDB, LL_INFO10000, "DC::TME in default impl. dji=%p, addr=%p, fp=%p\n",
        dji, ip, fp.GetSPValue()));
}

bool DebuggerController::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        NOTHROW;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;

    LOG((LF_CORDB, LL_INFO10000, "DC::TP: in default SendEvent\n"));

    // If any derived class trigger SendEvent, it should also implement SendEvent.
    _ASSERTE(false || !"Base DebuggerController sending an event?");
    return false;
}


// Dispacth Func-Eval Enter & Exit notifications.
void DebuggerController::DispatchFuncEvalEnter(Thread * thread)
{
    LOG((LF_CORDB, LL_INFO100000, "DC::DispatchFuncEvalEnter for thread 0x%p\n", thread));

    ControllerLockHolder lockController;

    DebuggerController *p = g_controllers;
    while (p != NULL)
    {
        if ((p->GetThread() == NULL) || (p->GetThread() == thread))
        {
            p->TriggerFuncEvalEnter(thread);
        }

        p = p->m_next;
    }

    
}

void DebuggerController::DispatchFuncEvalExit(Thread * thread)
{
    LOG((LF_CORDB, LL_INFO100000, "DC::DispatchFuncEvalExit for thread 0x%p\n", thread));

    ControllerLockHolder lockController;

    DebuggerController *p = g_controllers;
    while (p != NULL)
    {
        if ((p->GetThread() == NULL) || (p->GetThread() == thread))
        {
            p->TriggerFuncEvalExit(thread);
        }

        p = p->m_next;
    }

    
}


#ifdef _DEBUG
// See comment in DispatchNativeException
void ThisFunctionMayHaveTriggerAGC()
{
    CONTRACTL
    {
        GC_TRIGGERS;
        NOTHROW;
    }
    CONTRACTL_END;
}
#endif

// bool DebuggerController::DispatchNativeException()  Figures out
// if any debugger controllers will handle the exception.
// DispatchNativeException should be called by the EE when a native exception
// occurs.  If it returns true, the exception was generated by a Controller and
// should be ignored.
// How: Calls DispatchExceptionHook to see if anything is
// interested in ExceptionHook, then does a switch on dwCode:
//         EXCEPTION_BREAKPOINT means invoke DispatchPatchOrSingleStep(ST_PATCH).
//         EXCEPTION_SINGLE_STEP means DispatchPatchOrSingleStep(ST_SINGLE_STEP).
//         EXCEPTION_ACCESS_VIOLATION means invoke DispatchAccessViolation.
// Returns true if the exception was actually meant for the debugger,
//        returns false otherwise.
bool DebuggerController::DispatchNativeException(EXCEPTION_RECORD *pException,
                                                 CONTEXT *pContext,
                                                 DWORD dwCode,
                                                 Thread *pCurThread)
{
    CONTRACTL
    {
        NOTHROW;

        // If this exception is for the debugger, then we may trigger a GC.
        // But we'll be called on _any_ exception, including ones in a GC-no-triggers region.
        // Our current contract system doesn't let us specify such conditions on GC_TRIGGERS.
        // So we disable it now, and if we find out the exception is meant for the debugger,
        // we'll call ThisFunctionMayHaveTriggerAGC() to ping that we're really a GC_TRIGGERS.
        DISABLED(GC_TRIGGERS); // Only GC triggers if we send an event,
        PRECONDITION(!IsDbgHelperSpecialThread());

        // If we're called from preemptive mode, than our caller has protected the stack.
        // If we're in cooperative mode, then we need to protect the stack before toggling GC modes
        // (by setting the filter-context)
        MODE_ANY;

        PRECONDITION(CheckPointer(pException));
        PRECONDITION(CheckPointer(pContext));
        PRECONDITION(CheckPointer(pCurThread));
    }
    CONTRACTL_END;

    LOG((LF_CORDB, LL_EVERYTHING, "DispatchNativeException was called\n"));
    LOG((LF_CORDB, LL_INFO10000, "Native exception at 0x%p, code=0x%8x, context=0x%p, er=0x%p\n",
         pException->ExceptionAddress, dwCode, pContext, pException));


    bool fDebuggers;
    BOOL fDispatch;
    DPOSS_ACTION result = DPOSS_DONT_CARE;


    // We have a potentially ugly locking problem here. This notification is called on any exception,
    // but we have no idea what our locking context is at the time. Thus we may hold locks smaller
    // than the controller lock. 
    // The debugger logic really only cares about exceptions directly in managed code (eg, hardware exceptions)
    // or in patch-skippers (since that's a copy of managed code running in a look-aside buffer).
    // That should exclude all C++ exceptions, which are the common case if Runtime code throws an internal ex.
    // So we ignore those to avoid the lock violation.
    if (pException->ExceptionCode == EXCEPTION_MSVC)
    {
        LOG((LF_CORDB, LL_INFO1000, "Debugger skipping for C++ exception.\n"));         
        return FALSE;
    }

    // The debugger really only cares about exceptions in managed code.  Any exception that occurs
    // while the thread is redirected (such as EXCEPTION_HIJACK) is not of interest to the debugger.
    // Allowing this would be problematic because when an exception occurs while the thread is
    // redirected, we don't know which context (saved redirection context or filter context)
    // we should be operating on (see code:GetManagedStoppedCtx).
    if( ISREDIRECTEDTHREAD(pCurThread) )
    {
        LOG((LF_CORDB, LL_INFO1000, "Debugger ignoring exception 0x%x on redirected thread.\n", dwCode));

        // We shouldn't be seeing debugging exceptions on a redirected thread.  While a thread is
        // redirected we only call a few internal things (see code:Thread.RedirectedHandledJITCase),
        // and may call into the host.  We can't call normal managed code or anything we'd want to debug.
        _ASSERTE(dwCode != EXCEPTION_BREAKPOINT);
        _ASSERTE(dwCode != EXCEPTION_SINGLE_STEP);
 
        return FALSE;
    }

    // It's possible we're here without a debugger (since we have to call the
    // patch skippers). The Debugger may detach anytime,
    // so remember the attach state now.
#ifdef _DEBUG
    bool fWasAttached = false;
#ifdef DEBUGGING_SUPPORTED
    fWasAttached = (CORDebuggerAttached() != 0);
#endif //DEBUGGING_SUPPORTED
#endif //_DEBUG

    {
        // If we're in cooperative mode, it's unsafe to do a GC until we've put a filter context in place.
        GCX_NOTRIGGER();

        // If we know the debugger doesn't care about this exception, bail now.
        // Usually this is just if there's a debugger attached.
        // However, if a debugger detached but left outstanding controllers (like patch-skippers),
        // we still may care.
        // The only way a controller would get created outside of the helper thread is from
        // a patch skipper, so we always handle breakpoints.
        if (!CORDebuggerAttached() && (g_controllers == NULL) && (dwCode != EXCEPTION_BREAKPOINT))
        {
            return false;
        }

        FireEtwDebugExceptionProcessingStart(); 

        // We should never be here if the debugger was never involved.
        CONTEXT * pOldContext;
        pOldContext = pCurThread->GetFilterContext();

        // In most cases it is an error to nest, however in the patch-skipping logic we must
        // copy an unknown amount of code into another buffer and it occasionally triggers
        // an AV. This heuristic should filter that case out. See DDB 198093.
        // Ensure we perform this exception nesting filtering even before the call to 
        // DebuggerController::DispatchExceptionHook, otherwise the nesting will continue when
        // a contract check is triggered in DispatchExceptionHook and another BP exception is
        // raised. See Dev11 66058.
        if ((pOldContext != NULL) && pCurThread->AVInRuntimeImplOkay() && 
            pException->ExceptionCode == STATUS_ACCESS_VIOLATION)
        {
            STRESS_LOG1(LF_CORDB, LL_INFO100, "DC::DNE Nested Access Violation at 0x%p is being ignored\n",
                pException->ExceptionAddress);
            return false;
        }
        // Otherwise it is an error to nest at all
        _ASSERTE(pOldContext == NULL);

        fDispatch = DebuggerController::DispatchExceptionHook(pCurThread,
                                                                   pContext,
                                                                   pException);

        {
            // Must be in cooperative mode to set the filter context. We know there are times we'll be in preemptive mode,
            // (such as M2U handoff, or potentially patches in the middle of a stub, or various random exceptions)

            // @todo - We need to worry about GC-protecting our stack. If we're in preemptive mode, the caller did it for us.
            // If we're in cooperative, then we need to set the FilterContext *before* we toggle GC mode (since
            // the FC protects the stack).
            // If we're in preemptive, then we need to set the FilterContext *after* we toggle ourselves to Cooperative.
            // Also note it may not be possible to toggle GC mode at these times (such as in the middle of the stub).
            //
            // Part of the problem is that the Filter Context is serving 2 purposes here:
            // - GC protect the stack. (essential if we're in coop mode).
            // - provide info to controllers (such as current IP, and a place to set the Single-Step flag).
            //
            // This contract violation is mitigated in that we must have had the debugger involved to get to this point.
            CONTRACT_VIOLATION(ModeViolation);
            g_pEEInterface->SetThreadFilterContext(pCurThread, pContext);
        }
        // Now that we've set the filter context, we can let the GCX_NOTRIGGER expire.
        // It's still possible that we may be called from a No-trigger region.
    }


    if (fDispatch)
    {
        // Disable SingleStep for all controllers on this thread. This requires the filter context set.
        // This is what would disable the ss-flag when single-stepping over an AV.
        if (g_patchTableValid && (dwCode != EXCEPTION_SINGLE_STEP))
        {
            LOG((LF_CORDB, LL_INFO1000, "DC::DNE non-single-step exception; check if any controller has ss turned on\n"));

            ControllerLockHolder lockController;
            for (DebuggerController* p = g_controllers; p != NULL; p = p->m_next)
            {
                if (p->m_singleStep && (p->m_thread == pCurThread))
                {
                    LOG((LF_CORDB, LL_INFO1000, "DC::DNE turn off ss for controller 0x%p\n", p));
                    p->DisableSingleStep();
                }
            }
            // implicit controller lock release
        }

        CORDB_ADDRESS_TYPE * ip = dac_cast<PTR_CORDB_ADDRESS_TYPE>(GetIP(pContext));

        switch (dwCode)
        {
        case EXCEPTION_BREAKPOINT:
            // EIP should be properly set up at this point.
            result = DebuggerController::DispatchPatchOrSingleStep(pCurThread,
                                                       pContext,
                                                       ip,
                                                       ST_PATCH);
            LOG((LF_CORDB, LL_EVERYTHING, "DC::DNE DispatchPatch call returned\n"));

            // If we detached, we should remove all our breakpoints. So if we try
            // to handle this breakpoint, make sure that we're attached.
            if (IsInUsedAction(result) == true)
            {
                _ASSERTE(fWasAttached);
            }
            break;

        case EXCEPTION_SINGLE_STEP:
            LOG((LF_CORDB, LL_EVERYTHING, "DC::DNE SINGLE_STEP Exception\n"));

            result = DebuggerController::DispatchPatchOrSingleStep(pCurThread,
                                                            pContext,
                                                            ip,
                                        (SCAN_TRIGGER)(ST_PATCH|ST_SINGLE_STEP));
                // We pass patch | single step since single steps actually
                // do both (eg, you SS onto a breakpoint).
            break;

        default:
            break;
        } // end switch

    }
#ifdef _DEBUG
    else
    {
        LOG((LF_CORDB, LL_INFO1000, "DC:: DNE step-around fDispatch:0x%x!\n", fDispatch));
    }
#endif //_DEBUG

    fDebuggers = (fDispatch?(IsInUsedAction(result)?true:false):true);

    LOG((LF_CORDB, LL_INFO10000, "DC::DNE, returning 0x%x.\n", fDebuggers));

#ifdef _DEBUG
    if (fDebuggers && (result == DPOSS_USED_WITH_EVENT))
    {
        // If the exception belongs to the debugger, then we may have sent an event,
        // and thus we may have triggered a GC.
        ThisFunctionMayHaveTriggerAGC();
    }
#endif



    // Must restore the filter context. After the filter context is gone, we're
    // unprotected again and unsafe for a GC.
    {
        CONTRACT_VIOLATION(ModeViolation);
        g_pEEInterface->SetThreadFilterContext(pCurThread, NULL);
    }

#ifdef FEATURE_EMULATE_SINGLESTEP
    if (pCurThread->IsSingleStepEnabled())
        pCurThread->ApplySingleStep(pContext);
#endif

    FireEtwDebugExceptionProcessingEnd(); 

    return fDebuggers;
}

// * -------------------------------------------------------------------------
// * DebuggerPatchSkip routines
// * -------------------------------------------------------------------------

DebuggerPatchSkip::DebuggerPatchSkip(Thread *thread,
                                     DebuggerControllerPatch *patch,
                                     AppDomain *pAppDomain)
  : DebuggerController(thread, pAppDomain),
    m_address(patch->address)
{
    LOG((LF_CORDB, LL_INFO10000,
         "DPS::DPS: Patch skip 0x%p\n", patch->address));

    // On ARM the single-step emulation already utilizes a per-thread execution buffer similar to the scheme
    // below. As a result we can skip most of the instruction parsing logic that's instead internalized into
    // the single-step emulation itself.
#ifndef FEATURE_EMULATE_SINGLESTEP

    // NOTE: in order to correctly single-step RIP-relative writes on multiple threads we need to set up
    // a shared buffer with the instruction and a buffer for the RIP-relative value so that all threads
    // are working on the same copy.  as the single-steps complete the modified data in the buffer is
    // copied back to the real address to ensure proper execution of the program.

    //
    // Create the shared instruction block. this will also create the shared RIP-relative buffer
    //

    m_pSharedPatchBypassBuffer = patch->GetOrCreateSharedPatchBypassBuffer();
    BYTE* patchBypass = m_pSharedPatchBypassBuffer->PatchBypass;

    // Copy the instruction block over to the patch skip
    // WARNING: there used to be an issue here because CopyInstructionBlock copied the breakpoint from the
    // jitted code stream into the patch buffer. Further below CORDbgSetInstruction would correct the
    // first instruction. This buffer is shared by all threads so if another thread executed the buffer
    // between this thread's execution of CopyInstructionBlock and CORDbgSetInstruction the wrong
    // code would be executed. The bug has been fixed by changing CopyInstructionBlock to only copy
    // the code bytes after the breakpoint.
    //   You might be tempted to stop copying the code at all, however that wouldn't work well with rejit.
    // If we skip a breakpoint that is sitting at the beginning of a method, then the profiler rejits that
    // method causing a jump-stamp to be placed, then we skip the breakpoint again, we need to make sure
    // the 2nd skip executes the new jump-stamp code and not the original method prologue code. Copying
    // the code every time ensures that we have the most up-to-date version of the code in the buffer.
    _ASSERTE( patch->IsBound() );
    CopyInstructionBlock(patchBypass, (const BYTE *)patch->address);

    // Technically, we could create a patch skipper for an inactive patch, but we rely on the opcode being
    // set here.
    _ASSERTE( patch->IsActivated() );
    CORDbgSetInstruction((CORDB_ADDRESS_TYPE *)patchBypass, patch->opcode);

    LOG((LF_CORDB, LL_EVERYTHING, "SetInstruction was called\n"));
    //
    // Look at instruction to get some attributes
    //

    NativeWalker::DecodeInstructionForPatchSkip(patchBypass, &(m_instrAttrib));

#if defined(_TARGET_AMD64_)
    

    // The code below handles RIP-relative addressing on AMD64.  the original implementation made the assumption that
    // we are only using RIP-relative addressing to access read-only data (see VSW 246145 for more information).  this
    // has since been expanded to handle RIP-relative writes as well.
    if (m_instrAttrib.m_dwOffsetToDisp != 0)
    {
        _ASSERTE(m_instrAttrib.m_cbInstr != 0);

        //
        // Populate the RIP-relative buffer with the current value if needed
        //

        BYTE* bufferBypass = m_pSharedPatchBypassBuffer->BypassBuffer;

        // Overwrite the *signed* displacement.
        int dwOldDisp = *(int*)(&patchBypass[m_instrAttrib.m_dwOffsetToDisp]);
        int dwNewDisp = offsetof(SharedPatchBypassBuffer, BypassBuffer) -
                          (offsetof(SharedPatchBypassBuffer, PatchBypass) + m_instrAttrib.m_cbInstr);
        *(int*)(&patchBypass[m_instrAttrib.m_dwOffsetToDisp]) = dwNewDisp;

        // This could be an LEA, which we'll just have to change into a MOV
        // and copy the original address
        if (((patchBypass[0] == 0x4C) || (patchBypass[0] == 0x48)) && (patchBypass[1] == 0x8d))
        {
            patchBypass[1] = 0x8b; // MOV reg, mem
            _ASSERTE((int)sizeof(void*) <= SharedPatchBypassBuffer::cbBufferBypass);
            *(void**)bufferBypass = (void*)(patch->address + m_instrAttrib.m_cbInstr + dwOldDisp);
        }
        else
        {
            // Copy the data into our buffer.
            memcpy(bufferBypass, patch->address + m_instrAttrib.m_cbInstr + dwOldDisp, SharedPatchBypassBuffer::cbBufferBypass);
        
            if (m_instrAttrib.m_fIsWrite)
            {
                // save the actual destination address and size so when we TriggerSingleStep() we can update the value
                m_pSharedPatchBypassBuffer->RipTargetFixup = (UINT_PTR)(patch->address + m_instrAttrib.m_cbInstr + dwOldDisp);
                m_pSharedPatchBypassBuffer->RipTargetFixupSize = m_instrAttrib.m_cOperandSize;
            }
        }
    }
#endif // _TARGET_AMD64_

#endif // !FEATURE_EMULATE_SINGLESTEP

    // Signals our thread that the debugger will be manipulating the context 
    // during the patch skip operation. This effectively prevents other threads
    // from suspending us until we have completed skiping the patch and restored
    // a good context (See DDB 188816)
    thread->BeginDebuggerPatchSkip(this);

    //
    // Set IP of context to point to patch bypass buffer
    //

    T_CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);
    _ASSERTE(!ISREDIRECTEDTHREAD(thread));
    CONTEXT c;
    if (context == NULL)
    {
        // We can't play with our own context!
#if _DEBUG
        if (g_pEEInterface->GetThread())
        {
            // current thread is mamaged thread
            _ASSERTE(Debugger::GetThreadIdHelper(thread) != Debugger::GetThreadIdHelper(g_pEEInterface->GetThread()));
        }
#endif // _DEBUG

        c.ContextFlags = CONTEXT_CONTROL;

        thread->GetThreadContext(&c);
        context =(T_CONTEXT *) &c;

        ARM_ONLY(_ASSERTE(!"We should always have a filter context in DebuggerPatchSkip."));
    }

#ifdef FEATURE_EMULATE_SINGLESTEP
    // Since we emulate all single-stepping on ARM/ARM64 using an instruction buffer and a breakpoint all we have to
    // do here is initiate a normal single-step except that we pass the instruction to be stepped explicitly
    // (calling EnableSingleStep() would infer this by looking at the PC in the context, which would pick up
    // the patch we're trying to skip).
    //
    // Ideally we'd refactor the EnableSingleStep to support this alternative calling sequence but since this
    // involves three levels of methods and is only applicable to ARM we've chosen to replicate the relevant
    // implementation here instead.
    {
        ControllerLockHolder lockController;
        g_pEEInterface->MarkThreadForDebugStepping(thread, true);

#ifdef _TARGET_ARM_
        WORD opcode2 = 0;

        if (Is32BitInstruction(patch->opcode))
        {
            opcode2 = CORDbgGetInstruction((CORDB_ADDRESS_TYPE *)(((DWORD)patch->address) + 2));
        }
#endif // _TARGET_ARM_

        thread->BypassWithSingleStep(patch->address, patch->opcode ARM_ARG(opcode2));
        m_singleStep = true;
    }

#else // FEATURE_EMULATE_SINGLESTEP
   
#ifdef _TARGET_ARM64_
    patchBypass = NativeWalker::SetupOrSimulateInstructionForPatchSkip(context, m_pSharedPatchBypassBuffer, (const BYTE *)patch->address, patch->opcode);
#endif //_TARGET_ARM64_

    //set eip to point to buffer...
    SetIP(context, (PCODE)patchBypass);

    if (context ==(T_CONTEXT*) &c)
        thread->SetThreadContext(&c);
        

    LOG((LF_CORDB, LL_INFO10000, "DPS::DPS Bypass at 0x%p for opcode %p \n", patchBypass, patch->opcode));

    //
    // Turn on single step (if the platform supports it) so we can
    // fix up state after the instruction is executed.
    // Also turn on exception hook so we can adjust IP in exceptions
    //

    EnableSingleStep();

#endif // FEATURE_EMULATE_SINGLESTEP

    EnableExceptionHook();
}

DebuggerPatchSkip::~DebuggerPatchSkip()
{
#ifndef FEATURE_EMULATE_SINGLESTEP
    _ASSERTE(m_pSharedPatchBypassBuffer);
    m_pSharedPatchBypassBuffer->Release();
#endif
}

void DebuggerPatchSkip::DebuggerDetachClean()
{
// Since for ARM/ARM64 SharedPatchBypassBuffer isn't existed, we don't have to anything here.
#ifndef FEATURE_EMULATE_SINGLESTEP
   // Fix for Bug 1176448
   // When a debugger is detaching from the debuggee, we need to move the IP if it is pointing 
   // somewhere in PatchBypassBuffer.All managed threads are suspended during detach, so changing 
   // the context without notifications is safe.
   // Notice:
   // THIS FIX IS INCOMPLETE!It attempts to update the IP in the cases we can easily detect.However, 
   // if a thread is in pre - emptive mode, and its filter context has been propagated to a VEH 
   // context, then the filter context we get will be NULL and this fix will not work.Our belief is 
   // that this scenario is rare enough that it doesnt justify the cost and risk associated with a 
   // complete fix, in which we would have to either :
   // 1. Change the reference counting for DebuggerController and then change the exception handling 
   // logic in the debuggee so that we can handle the debugger event after detach.
   // 2. Create a "stack walking" implementation for native code and use it to get the current IP and 
   // set the IP to the right place.

    Thread *thread = GetThread();
    if (thread != NULL)
    {
        BYTE *patchBypass = m_pSharedPatchBypassBuffer->PatchBypass;
        CONTEXT *context = thread->GetFilterContext();
        if (patchBypass != NULL &&
            context != NULL &&
            (size_t)GetIP(context) >= (size_t)patchBypass &&
            (size_t)GetIP(context) <= (size_t)(patchBypass + MAX_INSTRUCTION_LENGTH + 1))
        {
            SetIP(context, (PCODE)((BYTE *)GetIP(context) - (patchBypass - (BYTE *)m_address)));
        }
    }
#endif
}


//
// We have to have a whole seperate function for this because you
// can't use __try in a function that requires object unwinding...
//

LONG FilterAccessViolation2(LPEXCEPTION_POINTERS ep, PVOID pv)
{
    LIMITED_METHOD_CONTRACT;

    return (ep->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION)
        ? EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH;
}

// This helper is required because the AVInRuntimeImplOkayHolder can not
// be directly placed inside the scope of a PAL_TRY
void _CopyInstructionBlockHelper(BYTE* to, const BYTE* from)
{
    AVInRuntimeImplOkayHolder AVOkay;

    // This function only copies the portion of the instruction that follows the
    // breakpoint opcode, not the breakpoint itself
    to += CORDbg_BREAK_INSTRUCTION_SIZE;
    from += CORDbg_BREAK_INSTRUCTION_SIZE;

    // If an AV occurs because we walked off a valid page then we need
    // to be certain that all bytes on the previous page were copied.
    // We are certain that we copied enough bytes to contain the instruction
    // because it must have fit within the valid page.
    for (int i = 0; i < MAX_INSTRUCTION_LENGTH - CORDbg_BREAK_INSTRUCTION_SIZE; i++)
    {
        *to++ = *from++;
    }

}

// WARNING: this function skips copying the first CORDbg_BREAK_INSTRUCTION_SIZE bytes by design
// See the comment at the callsite in DebuggerPatchSkip::DebuggerPatchSkip for more details on
// this
void DebuggerPatchSkip::CopyInstructionBlock(BYTE *to, const BYTE* from)
{
    // We wrap the memcpy in an exception handler to handle the
    // extremely rare case where we're copying an instruction off the
    // end of a method that is also at the end of a page, and the next
    // page is unmapped.
    struct Param
    {
        BYTE *to;
        const BYTE* from;
    } param;
    param.to = to;
    param.from = from;
    PAL_TRY(Param *, pParam, &param)
    {
        _CopyInstructionBlockHelper(pParam->to, pParam->from);
    }
    PAL_EXCEPT_FILTER(FilterAccessViolation2)
    {
        // The whole point is that if we copy up the the AV, then
        // that's enough to execute, otherwise we would not have been
        // able to execute the code anyway. So we just ignore the
        // exception.
        LOG((LF_CORDB, LL_INFO10000,
             "DPS::DPS: AV copying instruction block ignored.\n"));
    }
    PAL_ENDTRY

    // We just created a new buffer of code, but the CPU caches code and may
    // not be aware of our changes. This should force the CPU to dump any cached
    // instructions it has in this region and load the new ones from memory
    FlushInstructionCache(GetCurrentProcess(), to + CORDbg_BREAK_INSTRUCTION_SIZE,
                          MAX_INSTRUCTION_LENGTH - CORDbg_BREAK_INSTRUCTION_SIZE);
}

TP_RESULT DebuggerPatchSkip::TriggerPatch(DebuggerControllerPatch *patch,
                              Thread *thread,
                              TRIGGER_WHY tyWhy)
{
    ARM_ONLY(_ASSERTE(!"Should not have called DebuggerPatchSkip::TriggerPatch."));
    LOG((LF_CORDB, LL_EVERYTHING, "DPS::TP called\n"));

#if defined(_DEBUG) && !defined(FEATURE_EMULATE_SINGLESTEP)
    CONTEXT *context = GetManagedLiveCtx(thread);

    LOG((LF_CORDB, LL_INFO1000, "DPS::TP: We've patched 0x%x (byPass:0x%x) "
        "for a skip after an EnC update!\n", GetIP(context),
        GetBypassAddress()));
    _ASSERTE(g_patches != NULL);

    // We shouldn't have mucked with EIP, yet.
    _ASSERTE(dac_cast<PTR_CORDB_ADDRESS_TYPE>(GetIP(context)) == GetBypassAddress());

    //We should be the _only_ patch here
    MethodDesc *md2 = dac_cast<PTR_MethodDesc>(GetIP(context));
    DebuggerControllerPatch *patchCheck = g_patches->GetPatch(g_pEEInterface->MethodDescGetModule(md2),md2->GetMemberDef());
    _ASSERTE(patchCheck == patch);
    _ASSERTE(patchCheck->controller == patch->controller);

    patchCheck = g_patches->GetNextPatch(patchCheck);
    _ASSERTE(patchCheck == NULL);
#endif // defined(_DEBUG) && !defined(FEATURE_EMULATE_SINGLESTEP)

    DisableAll();
    EnableExceptionHook();
    EnableSingleStep(); //gets us back to where we want.
    return TPR_IGNORE; // don't actually want to stop here....
}

TP_RESULT DebuggerPatchSkip::TriggerExceptionHook(Thread *thread, CONTEXT * context,
                                                  EXCEPTION_RECORD *exception)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        // Patch skippers only operate on patches set in managed code. But the infrastructure may have
        // toggled the GC mode underneath us.
        MODE_ANY;

        PRECONDITION(GetThread() == thread);
        PRECONDITION(thread != NULL);
        PRECONDITION(CheckPointer(context));
    }
    CONTRACTL_END;

    if (m_pAppDomain != NULL)
    {
        AppDomain *pAppDomainCur = thread->GetDomain();

        if (pAppDomainCur != m_pAppDomain)
        {
            LOG((LF_CORDB,LL_INFO10000, "DPS::TEH: Appdomain mismatch - not skiiping!\n"));
            return TPR_IGNORE;
        }
    }

    LOG((LF_CORDB,LL_INFO10000, "DPS::TEH: doing the patch-skip thing\n"));    

#if defined(_TARGET_ARM64_) && !defined(FEATURE_EMULATE_SINGLESTEP)

    if (!IsSingleStep(exception->ExceptionCode))
    {
        LOG((LF_CORDB, LL_INFO10000, "Exception in patched Bypass instruction .\n"));
        return (TPR_IGNORE_AND_STOP);
    }

    _ASSERTE(m_pSharedPatchBypassBuffer);
    BYTE* patchBypass = m_pSharedPatchBypassBuffer->PatchBypass;
    PCODE targetIp;
    if (m_pSharedPatchBypassBuffer->RipTargetFixup)
    {
        targetIp = m_pSharedPatchBypassBuffer->RipTargetFixup;
    }
    else
    {
        targetIp = (PCODE)((BYTE *)GetIP(context) - (patchBypass - (BYTE *)m_address));
    }

    SetIP(context, targetIp);
    LOG((LF_CORDB, LL_ALWAYS, "Redirecting after Patch to 0x%p\n", GetIP(context)));

#elif defined(FEATURE_EMULATE_SINGLESTEP)

//Do nothing 
#else
    _ASSERTE(m_pSharedPatchBypassBuffer);
    BYTE* patchBypass = m_pSharedPatchBypassBuffer->PatchBypass;

    if (m_instrAttrib.m_fIsCall && IsSingleStep(exception->ExceptionCode))
    {
        // Fixup return address on stack
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
        SIZE_T *sp = (SIZE_T *) GetSP(context);

        LOG((LF_CORDB, LL_INFO10000,
             "Bypass call return address redirected from 0x%p\n", *sp));

        *sp -= patchBypass - (BYTE*)m_address;

        LOG((LF_CORDB, LL_INFO10000, "to 0x%p\n", *sp));
#else
        PORTABILITY_ASSERT("DebuggerPatchSkip::TriggerExceptionHook -- return address fixup NYI");
#endif
    }

    if (!m_instrAttrib.m_fIsAbsBranch || !IsSingleStep(exception->ExceptionCode))
    {
        // Fixup IP

        LOG((LF_CORDB, LL_INFO10000, "Bypass instruction redirected from 0x%p\n", GetIP(context)));

        if (IsSingleStep(exception->ExceptionCode))
        {
#ifndef FEATURE_PAL
            // Check if the current IP is anywhere near the exception dispatcher logic.
            // If it is, ignore the exception, as the real exception is coming next.
            static FARPROC pExcepDispProc = NULL;

            if (!pExcepDispProc)
            {
                HMODULE hNtDll = WszGetModuleHandle(W("ntdll.dll"));

                if (hNtDll != NULL)
                {
                    pExcepDispProc = GetProcAddress(hNtDll, "KiUserExceptionDispatcher");

                    if (!pExcepDispProc)
                        pExcepDispProc = (FARPROC)(size_t)(-1);
                }
                else
                    pExcepDispProc = (FARPROC)(size_t)(-1);
            }

            _ASSERTE(pExcepDispProc != NULL);

            if ((size_t)pExcepDispProc != (size_t)(-1))
            {
                LPVOID pExcepDispEntryPoint = pExcepDispProc;

                if ((size_t)GetIP(context) > (size_t)pExcepDispEntryPoint &&
                    (size_t)GetIP(context) <= ((size_t)pExcepDispEntryPoint + MAX_INSTRUCTION_LENGTH * 2 + 1))
                {
                    LOG((LF_CORDB, LL_INFO10000,
                         "Bypass instruction not redirected. Landed in exception dispatcher.\n"));

                    return (TPR_IGNORE_AND_STOP);
                }
            }
#endif // FEATURE_PAL

            // If the IP is close to the skip patch start, or if we were skipping over a call, then assume the IP needs
            // adjusting.
            if (m_instrAttrib.m_fIsCall ||
                ((size_t)GetIP(context) >  (size_t)patchBypass &&
                 (size_t)GetIP(context) <= (size_t)(patchBypass + MAX_INSTRUCTION_LENGTH + 1)))
            {
                LOG((LF_CORDB, LL_INFO10000, "Bypass instruction redirected because still in skip area.\n"));
                LOG((LF_CORDB, LL_INFO10000, "m_fIsCall = %d, patchBypass = 0x%x, m_address = 0x%x\n",
                    m_instrAttrib.m_fIsCall, patchBypass, m_address));
                SetIP(context, (PCODE)((BYTE *)GetIP(context) - (patchBypass - (BYTE *)m_address)));
            }
            else
            {
                // Otherwise, need to see if the IP is something we recognize (either managed code
                // or stub code) - if not, we ignore the exception
                PCODE newIP = GetIP(context);
                newIP -= PCODE(patchBypass - (BYTE *)m_address);
                TraceDestination trace;

                if (g_pEEInterface->IsManagedNativeCode(dac_cast<PTR_CBYTE>(newIP)) ||
                    (g_pEEInterface->TraceStub(LPBYTE(newIP), &trace)))
                {
                    LOG((LF_CORDB, LL_INFO10000, "Bypass instruction redirected because we landed in managed or stub code\n"));
                    SetIP(context, newIP);
                }

                // If we have no idea where things have gone, then we assume that the IP needs no adjusting (which
                // could happen if the instruction we were trying to patch skip caused an AV).  In this case we want
                // to claim it as ours but ignore it and continue execution.
                else
                {
                    LOG((LF_CORDB, LL_INFO10000, "Bypass instruction not redirected because we're not in managed or stub code.\n"));
                    return (TPR_IGNORE_AND_STOP);
                }
            }
        }
        else
        {
            LOG((LF_CORDB, LL_INFO10000, "Bypass instruction redirected because it wasn't a single step exception.\n"));
            SetIP(context, (PCODE)((BYTE *)GetIP(context) - (patchBypass - (BYTE *)m_address)));
        }

        LOG((LF_CORDB, LL_ALWAYS, "to 0x%x\n", GetIP(context)));

    }

#endif 


    // Signals our thread that the debugger is done manipulating the context
    // during the patch skip operation. This effectively prevented other threads
    // from suspending us until we completed skiping the patch and restored
    // a good context (See DDB 188816)
    m_thread->EndDebuggerPatchSkip();

    // Don't delete the controller yet if this is a single step exception, as the code will still want to dispatch to
    // our single step method, and if it doesn't find something to dispatch to we won't continue from the exception.
    //
    // (This is kind of broken behavior but is easily worked around here
    // by this test)
    if (!IsSingleStep(exception->ExceptionCode))
    {
        Delete();
    }

    DisableExceptionHook();

    return TPR_TRIGGER;
}

bool DebuggerPatchSkip::TriggerSingleStep(Thread *thread, const BYTE *ip)
{
    LOG((LF_CORDB,LL_INFO10000, "DPS::TSS: basically a no-op\n"));

    if (m_pAppDomain != NULL)
    {
        AppDomain *pAppDomainCur = thread->GetDomain();

        if (pAppDomainCur != m_pAppDomain)
        {
            LOG((LF_CORDB,LL_INFO10000, "DPS::TSS: Appdomain mismatch - "
                "not SingSteping!!\n"));
            return false;
        }
    }
#if defined(_TARGET_AMD64_)
    // Dev11 91932: for RIP-relative writes we need to copy the value that was written in our buffer to the actual address
    _ASSERTE(m_pSharedPatchBypassBuffer);
    if (m_pSharedPatchBypassBuffer->RipTargetFixup)
    {
        _ASSERTE(m_pSharedPatchBypassBuffer->RipTargetFixupSize);

        BYTE* bufferBypass = m_pSharedPatchBypassBuffer->BypassBuffer;
        BYTE fixupSize = m_pSharedPatchBypassBuffer->RipTargetFixupSize;
        UINT_PTR targetFixup = m_pSharedPatchBypassBuffer->RipTargetFixup;

        switch (fixupSize)
        {
        case 1:
            *(reinterpret_cast<BYTE*>(targetFixup)) = *(reinterpret_cast<BYTE*>(bufferBypass));
            break;

        case 2:
            *(reinterpret_cast<WORD*>(targetFixup)) = *(reinterpret_cast<WORD*>(bufferBypass));
            break;

        case 4:
            *(reinterpret_cast<DWORD*>(targetFixup)) = *(reinterpret_cast<DWORD*>(bufferBypass));
            break;

        case 8:
            *(reinterpret_cast<ULONGLONG*>(targetFixup)) = *(reinterpret_cast<ULONGLONG*>(bufferBypass));
            break;

        case 16:
            memcpy(reinterpret_cast<void*>(targetFixup), bufferBypass, 16);
            break;

        default:
            _ASSERTE(!"bad operand size");
        }
    }
#endif
    LOG((LF_CORDB,LL_INFO10000, "DPS::TSS: triggered, about to delete\n"));

    TRACE_FREE(this);
    Delete();
    return false;
}

// * -------------------------------------------------------------------------
// * DebuggerBreakpoint routines
// * -------------------------------------------------------------------------
// DebuggerBreakpoint::DebuggerBreakpoint()   The constructor
// invokes AddBindAndActivatePatch to set the breakpoint
DebuggerBreakpoint::DebuggerBreakpoint(Module *module,
                                       mdMethodDef md,
                                       AppDomain *pAppDomain,
                                       SIZE_T offset,
                                       bool native,
                                       SIZE_T ilEnCVersion,  // must give the EnC version for non-native bps
                                       MethodDesc *nativeMethodDesc,  // use only when m_native
                                       DebuggerJitInfo *nativeJITInfo,  // optional when m_native, null otherwise
                                       bool nativeCodeBindAllVersions,
                                       BOOL *pSucceed
                                       )
                                       : DebuggerController(NULL, pAppDomain)
{
    _ASSERTE(pSucceed != NULL);
    _ASSERTE((native == (nativeMethodDesc != NULL)) || nativeCodeBindAllVersions);
    _ASSERTE(native || nativeJITInfo == NULL);
    _ASSERTE(!nativeJITInfo || nativeJITInfo->m_jitComplete); // this is sent by the left-side, and it couldn't have got the code if the JIT wasn't complete

    if (native && !nativeCodeBindAllVersions)
    {
        (*pSucceed) = AddBindAndActivateNativeManagedPatch(nativeMethodDesc, nativeJITInfo, offset, LEAF_MOST_FRAME, pAppDomain);
        return;
    }
    else
    {
        _ASSERTE(!native || offset == 0);
        (*pSucceed) = AddILPatch(pAppDomain, module, md, NULL, ilEnCVersion, offset, !native);
    }
}

// TP_RESULT DebuggerBreakpoint::TriggerPatch()
// What: This patch will always be activated.
// How: return true.
TP_RESULT DebuggerBreakpoint::TriggerPatch(DebuggerControllerPatch *patch,
                                      Thread *thread,
                                      TRIGGER_WHY tyWhy)
{
    LOG((LF_CORDB, LL_INFO10000, "DB::TP\n"));

    return TPR_TRIGGER;
}

// void DebuggerBreakpoint::SendEvent()  What: Inform
// the right side that the breakpoint was reached.
// How: g_pDebugger->SendBreakpoint()
bool DebuggerBreakpoint::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        NOTHROW;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;


    LOG((LF_CORDB, LL_INFO10000, "DB::SE: in DebuggerBreakpoint's SendEvent\n"));

    CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);

    // If we got interupted by SetIp, we just don't send the IPC event. Our triggers are still
    // active so no harm done.
    if (!fIpChanged)
    {
        g_pDebugger->SendBreakpoint(thread, context, this);
        return true;
    }

    // Controller is still alive, will fire if we hit the breakpoint again.
    return false;
}

//* -------------------------------------------------------------------------
// * DebuggerStepper routines
// * -------------------------------------------------------------------------

DebuggerStepper::DebuggerStepper(Thread *thread,
                                 CorDebugUnmappedStop rgfMappingStop,
                                 CorDebugIntercept interceptStop,
                                 AppDomain *appDomain)
  : DebuggerController(thread, appDomain),
    m_stepIn(false),
    m_reason(STEP_NORMAL),
    m_fpStepInto(LEAF_MOST_FRAME),
    m_rgfInterceptStop(interceptStop),
    m_rgfMappingStop(rgfMappingStop),
    m_range(NULL),
    m_rangeCount(0),
    m_realRangeCount(0),
    m_fp(LEAF_MOST_FRAME),
#if defined(WIN64EXCEPTIONS)
    m_fpParentMethod(LEAF_MOST_FRAME),
#endif // WIN64EXCEPTIONS
    m_fpException(LEAF_MOST_FRAME),
    m_fdException(0),
    m_cFuncEvalNesting(0)
{
#ifdef _DEBUG
    m_fReadyToSend = false;
#endif
}

DebuggerStepper::~DebuggerStepper()
{
    if (m_range != NULL)
    {
        TRACE_FREE(m_range);
        DeleteInteropSafe(m_range);
    }
}

// bool DebuggerStepper::ShouldContinueStep()   Return true if
// the stepper should not stop at this address.  The stepper should not
// stop here if: here is in the {prolog,epilog,etc};
// and the stepper is not interested in stopping here.
// We assume that this is being called in the frame which the stepper steps
// through.  Unless, of course, we're returning from a call, in which
// case we want to stop in the epilog even if the user didn't say so,
// to prevent stepping out of multiple frames at once.
// <REVISIT_TODO>Possible optimization: GetJitInfo, then AddPatch @ end of prolog?</REVISIT_TODO>
bool DebuggerStepper::ShouldContinueStep( ControllerStackInfo *info,
                                          SIZE_T nativeOffset)
{
    LOG((LF_CORDB,LL_INFO10000, "DeSt::ShContSt: nativeOffset:0x%p \n", nativeOffset));
    if (m_rgfMappingStop != STOP_ALL && (m_reason != STEP_EXIT) )
    {

        DebuggerJitInfo *ji = info->m_activeFrame.GetJitInfoFromFrame();

        if ( ji != NULL )
        {
            LOG((LF_CORDB,LL_INFO10000,"DeSt::ShContSt: For code 0x%p, got "
            "DJI 0x%p, from 0x%p to 0x%p\n",
            (const BYTE*)GetControlPC(&(info->m_activeFrame.registers)),
            ji, ji->m_addrOfCode, ji->m_addrOfCode+ji->m_sizeOfCode));
        }
        else
        {
            LOG((LF_CORDB,LL_INFO10000,"DeSt::ShCoSt: For code 0x%p, didn't "
                "get DJI\n",(const BYTE*)GetControlPC(&(info->m_activeFrame.registers))));

            return false; // Haven't a clue if we should continue, so
                          // don't
        }
        CorDebugMappingResult map = MAPPING_UNMAPPED_ADDRESS;
        DWORD whichIDontCare;
        ji->MapNativeOffsetToIL( nativeOffset, &map, &whichIDontCare);
        unsigned int interestingMappings =
            (map & ~(MAPPING_APPROXIMATE | MAPPING_EXACT));

        LOG((LF_CORDB,LL_INFO10000,
             "DeSt::ShContSt: interestingMappings:0x%x m_rgfMappingStop:%x\n",
             interestingMappings,m_rgfMappingStop));

        // If we're in a prolog,epilog, then we may want to skip
        // over it or stop
        if ( interestingMappings )
        {
            if ( interestingMappings & m_rgfMappingStop )
                return false;
            else
                return true;
        }
    }
    return false;
}

bool DebuggerStepper::IsRangeAppropriate(ControllerStackInfo *info)
{
    LOG((LF_CORDB,LL_INFO10000, "DS::IRA: info:0x%x \n", info));
    if (m_range == NULL)
    {
        LOG((LF_CORDB,LL_INFO10000, "DS::IRA: m_range == NULL, returning FALSE\n"));
        return false;
    }

    FrameInfo *realFrame;

#if defined(WIN64EXCEPTIONS)
    bool fActiveFrameIsFunclet = info->m_activeFrame.IsNonFilterFuncletFrame();

    if (fActiveFrameIsFunclet)
    {
        realFrame = &(info->m_returnFrame);
    }
    else
#endif // WIN64EXCEPTIONS
    {
        realFrame = &(info->m_activeFrame);
    }

    LOG((LF_CORDB,LL_INFO10000, "DS::IRA: info->m_activeFrame.fp:0x%x m_fp:0x%x\n", info->m_activeFrame.fp, m_fp));
    LOG((LF_CORDB,LL_INFO10000, "DS::IRA: m_fdException:0x%x realFrame->md:0x%x realFrame->fp:0x%x m_fpException:0x%x\n",
        m_fdException, realFrame->md, realFrame->fp, m_fpException));
    if ( (info->m_activeFrame.fp == m_fp) ||
         ( (m_fdException != NULL) && (realFrame->md == m_fdException) && 
           IsEqualOrCloserToRoot(realFrame->fp, m_fpException) ) )
    {
        LOG((LF_CORDB,LL_INFO10000, "DS::IRA: returning TRUE\n"));
        return true;
    }

#if defined(WIN64EXCEPTIONS)
    // There are two scenarios which make this function more complicated on WIN64.
    // 1)  We initiate a step in the parent method or a funclet but end up stepping into another funclet closer to the leaf.
    //      a)  start in the parent method
    //      b)  start in a funclet
    // 2)  We initiate a step in a funclet but end up stepping out to the parent method or a funclet closer to the root.
    //      a) end up in the parent method
    //      b) end up in a funclet
    // In both cases the range of the stepper should still be appropriate.

    bool fValidParentMethodFP = (m_fpParentMethod != LEAF_MOST_FRAME);

    if (fActiveFrameIsFunclet)
    {
        // Scenario 1a
        if (m_fp == info->m_returnFrame.fp)
        {
            LOG((LF_CORDB,LL_INFO10000, "DS::IRA: returning TRUE\n"));
            return true;
        }
        // Scenario 1b & 2b have the same condition
        else if (fValidParentMethodFP && (m_fpParentMethod == info->m_returnFrame.fp))
        {
            LOG((LF_CORDB,LL_INFO10000, "DS::IRA: returning TRUE\n"));
            return true;
        }
    }
    else
    {
        // Scenario 2a
        if (fValidParentMethodFP && (m_fpParentMethod == info->m_activeFrame.fp))
        {
            LOG((LF_CORDB,LL_INFO10000, "DS::IRA: returning TRUE\n"));
            return true;
        }
    }
#endif // WIN64EXCEPTIONS

    LOG((LF_CORDB,LL_INFO10000, "DS::IRA: returning FALSE\n"));
    return false;
}

// bool DebuggerStepper::IsInRange()   Given the native offset ip,
// returns true if ip falls within any of the native offset ranges specified
// by the array of COR_DEBUG_STEP_RANGEs.
// Returns true if ip falls within any of the ranges.  Returns false
// if ip doesn't, or if there are no ranges (rangeCount==0). Note that a
// COR_DEBUG_STEP_RANGE with an endOffset of zero is interpreted as extending
// from startOffset to the end of the method.
// SIZE_T ip:  Native offset, relative to the beginning of the method.
// COR_DEBUG_STEP_RANGE *range:  An array of ranges, which are themselves
//        native offsets, to compare against ip.
// SIZE_T rangeCount:  Number of elements in range
bool DebuggerStepper::IsInRange(SIZE_T ip, COR_DEBUG_STEP_RANGE *range, SIZE_T rangeCount,
                                ControllerStackInfo *pInfo)
{
    LOG((LF_CORDB,LL_INFO10000,"DS::IIR: off=0x%x\n", ip));

    if (range == NULL)
    {
        LOG((LF_CORDB,LL_INFO10000,"DS::IIR: range == NULL -> not in range\n"));
        return false;
    }

    if (pInfo && !IsRangeAppropriate(pInfo))
    {
        LOG((LF_CORDB,LL_INFO10000,"DS::IIR: no pInfo or range not appropriate -> not in range\n"));
        return false;
    }

    COR_DEBUG_STEP_RANGE *r = range;
    COR_DEBUG_STEP_RANGE *rEnd = r + rangeCount;

    while (r < rEnd)
    {
        SIZE_T endOffset = r->endOffset ? r->endOffset : ~0;
        LOG((LF_CORDB,LL_INFO100000,"DS::IIR: so=0x%x, eo=0x%x\n",
             r->startOffset, endOffset));

        if (ip >= r->startOffset && ip < endOffset)
        {
            LOG((LF_CORDB,LL_INFO1000,"DS::IIR:this:0x%x Found native offset "
                "0x%x to be in the range"
                "[0x%x, 0x%x), index 0x%x\n\n", this, ip, r->startOffset,
                endOffset, ((r-range)/sizeof(COR_DEBUG_STEP_RANGE *)) ));
            return true;
        }

        r++;
    }

    LOG((LF_CORDB,LL_INFO10000,"DS::IIR: not in range\n"));
    return false;
}

// bool DebuggerStepper::DetectHandleInterceptors()  Return true if
// the current execution takes place within an interceptor (that is, either
// the current frame, or the parent frame is a framed frame whose
// GetInterception method returns something other than INTERCEPTION_NONE),
// and this stepper doesn't want to stop in an interceptor, and we successfully
// set a breakpoint after the top-most interceptor in the stack.
bool DebuggerStepper::DetectHandleInterceptors(ControllerStackInfo *info)
{
    LOG((LF_CORDB,LL_INFO10000,"DS::DHI: Start DetectHandleInterceptors\n"));
    LOG((LF_CORDB,LL_INFO10000,"DS::DHI: active frame=0x%08x, has return frame=%d, return frame=0x%08x m_reason:%d\n",
         info->m_activeFrame.frame, info->HasReturnFrame(), info->m_returnFrame.frame, m_reason));

    // If this is a normal step, then we want to continue stepping, even if we
    // are in an interceptor.
    if (m_reason == STEP_NORMAL || m_reason == STEP_RETURN || m_reason == STEP_EXCEPTION_HANDLER)
    {
        LOG((LF_CORDB,LL_INFO1000,"DS::DHI: Returning false while stepping within function, finally!\n"));
        return false;
    }

    bool fAttemptStepOut = false;

    if (m_rgfInterceptStop != INTERCEPT_ALL) // we may have to skip out of one
    {
        if (info->m_activeFrame.frame != NULL &&
            info->m_activeFrame.frame != FRAME_TOP &&
            info->m_activeFrame.frame->GetInterception() != Frame::INTERCEPTION_NONE)
        {
            if (!((CorDebugIntercept)info->m_activeFrame.frame->GetInterception() & Frame::Interception(m_rgfInterceptStop)))
            {
                LOG((LF_CORDB,LL_INFO10000,"DS::DHI: Stepping out b/c of excluded frame type:0x%x\n",
                     info->m_returnFrame. frame->GetInterception()));

                fAttemptStepOut = true;
            }
            else
            {
                LOG((LF_CORDB,LL_INFO10000,"DS::DHI: 0x%x set to STEP_INTERCEPT\n", this));

                m_reason = STEP_INTERCEPT; //remember why we've stopped
            }
        }

        if ((m_reason == STEP_EXCEPTION_FILTER) ||
            (info->HasReturnFrame() &&
            info->m_returnFrame.frame != NULL &&
            info->m_returnFrame.frame != FRAME_TOP &&
            info->m_returnFrame.frame->GetInterception() != Frame::INTERCEPTION_NONE))
        {
            if (m_reason == STEP_EXCEPTION_FILTER)
            {
                // Exceptions raised inside of the EE by COMPlusThrow, FCThrow, etc will not
                // insert an ExceptionFrame, and hence info->m_returnFrame.frame->GetInterception()
                // will not be accurate. Hence we use m_reason instead

                if (!(Frame::INTERCEPTION_EXCEPTION & Frame::Interception(m_rgfInterceptStop)))
                {
                    LOG((LF_CORDB,LL_INFO10000,"DS::DHI: Stepping out b/c of excluded INTERCEPTION_EXCEPTION\n"));
                    fAttemptStepOut = true;
                }
            }
            else if (!(info->m_returnFrame.frame->GetInterception() & Frame::Interception(m_rgfInterceptStop)))
            {
                LOG((LF_CORDB,LL_INFO10000,"DS::DHI: Stepping out b/c of excluded return frame type:0x%x\n",
                     info->m_returnFrame.frame->GetInterception()));

                fAttemptStepOut = true;
            }

            if (!fAttemptStepOut)
            {
                LOG((LF_CORDB,LL_INFO10000,"DS::DHI 0x%x set to STEP_INTERCEPT\n", this));

                m_reason = STEP_INTERCEPT; //remember why we've stopped
            }
        }
        else if (info->m_specialChainReason != CHAIN_NONE)
        {
            if(!(info->m_specialChainReason & CorDebugChainReason(m_rgfInterceptStop)) )
            {
                LOG((LF_CORDB,LL_INFO10000, "DS::DHI: (special) Stepping out b/c of excluded return frame type:0x%x\n",
                     info->m_specialChainReason));

                fAttemptStepOut = true;
            }
            else
            {
                LOG((LF_CORDB,LL_INFO10000,"DS::DHI 0x%x set to STEP_INTERCEPT\n", this));

                m_reason = STEP_INTERCEPT; //remember why we've stopped
            }
        }
        else if (info->m_activeFrame.frame == NULL)
        {
            // Make sure we are not dealing with a chain here.
            if (info->m_activeFrame.HasMethodFrame())
            {
                // Check whether we are executing in a class constructor.
                _ASSERTE(info->m_activeFrame.md != NULL);
                if (info->m_activeFrame.md->IsClassConstructor())
                {
                    // We are in a class constructor.  Check whether we want to stop in it.
                    if (!(CHAIN_CLASS_INIT & CorDebugChainReason(m_rgfInterceptStop)))
                    {
                        LOG((LF_CORDB, LL_INFO10000, "DS::DHI: Stepping out b/c of excluded cctor:0x%x\n",
                             CHAIN_CLASS_INIT));

                        fAttemptStepOut = true;
                    }
                    else
                    {
                        LOG((LF_CORDB, LL_INFO10000,"DS::DHI 0x%x set to STEP_INTERCEPT\n", this));

                        m_reason = STEP_INTERCEPT; //remember why we've stopped
                    }
                }
            }
        }
    }

    if (fAttemptStepOut)
    {
        LOG((LF_CORDB,LL_INFO1000,"DS::DHI: Doing TSO!\n"));

        // TrapStepOut could alter the step reason if we're stepping out of an inteceptor and it looks like we're
        // running off the top of the program. So hold onto it here, and if our step reason becomes STEP_EXIT, then
        // reset it to what it was.
        CorDebugStepReason holdReason = m_reason;

        // @todo - should this be TrapStepNext??? But that may stop in a child...
        TrapStepOut(info);
        EnableUnwind(m_fp);

        if (m_reason == STEP_EXIT)
        {
            m_reason = holdReason;
        }

        return true;
    }

    // We're not in a special area of code, so we don't want to continue unless some other part of the code decides that
    // we should.
    LOG((LF_CORDB,LL_INFO1000,"DS::DHI: Returning false, finally!\n"));

    return false;
}


//---------------------------------------------------------------------------------------
//
// This function checks whether the given IP is in an LCG method.  If so, it enables 
// JMC and does a step out.  This effectively makes sure that we never stop in an LCG method.
//
// There are two common scnearios here:
// 1)  We single-step into an LCG method from a managed method.
// 2)  We single-step off the end of a method called by an LCG method and end up in the calling LCG method.
//
// In both cases, we don't want to stop in the LCG method.  If the LCG method directly or indirectly calls
// another user method, we want to stop there.  Otherwise, we just want to step out back to the caller of 
// LCG method.  In other words, what we want is exactly the JMC behaviour.
//
// Arguments:
//    ip    - the current IP where the thread is stopped at
//    pMD   - This is the MethodDesc for the specified ip.  This can be NULL, but if it's not, 
//            then it has to match the specified IP.
//    pInfo - the ControllerStackInfo taken at the specified IP (see Notes below)
//
// Return Value:
//    Returns TRUE if the specified IP is indeed in an LCG method, in which case this function has already 
//    enabled all the traps to catch the thread, including turning on JMC, enabling unwind callback, and
//    putting a patch in the caller.
//
// Notes:
//    LCG methods don't show up in stackwalks done by the ControllerStackInfo.  So even if the specified IP 
//    is in an LCG method, the LCG method won't show up in the call strack.  That's why we need to call
//    ControllerStackInfo::SetReturnFrameWithActiveFrame() in this function before calling TrapStepOut().
//    Otherwise TrapStepOut() will put a patch in the caller's caller (if there is one).
//

BOOL DebuggerStepper::DetectHandleLCGMethods(const PCODE ip, MethodDesc * pMD, ControllerStackInfo * pInfo)
{
    // Look up the MethodDesc for the given IP.
    if (pMD == NULL)
    {
        if (g_pEEInterface->IsManagedNativeCode((const BYTE *)ip))
        {
            pMD = g_pEEInterface->GetNativeCodeMethodDesc(ip);
            _ASSERTE(pMD != NULL);
        }
    }
#if defined(_DEBUG)
    else
    {
        // If a MethodDesc is specified, it has to match the given IP.
        _ASSERTE(pMD == g_pEEInterface->GetNativeCodeMethodDesc(ip));
    }
#endif // _DEBUG

    // If the given IP is in unmanaged code, then we won't have a MethodDesc by this point.
    if (pMD != NULL)
    {
        if (pMD->IsLCGMethod())
        {
            // Enable all the traps to catch the thread.
            EnableUnwind(m_fp);
            EnableJMCBackStop(pMD);

            pInfo->SetReturnFrameWithActiveFrame();
            TrapStepOut(pInfo);
            return TRUE;
        }
    }

    return FALSE;
}


// Steppers override these so that they can skip func-evals. Note that steppers can
// be created & used inside of func-evals (nested-break states).
// On enter, we check for freezing the stepper.
void DebuggerStepper::TriggerFuncEvalEnter(Thread * thread)
{
    LOG((LF_CORDB, LL_INFO10000, "DS::TFEEnter, this=0x%p, old nest=%d\n", this, m_cFuncEvalNesting));

    // Since this is always called on the hijacking thread, we should be thread-safe
    _ASSERTE(thread == this->GetThread());

    if (IsDead())
        return;

    m_cFuncEvalNesting++;

    if (m_cFuncEvalNesting == 1)
    {
        // We're entering our 1st funceval, so freeze us.
        LOG((LF_CORDB, LL_INFO100000, "DS::TFEEnter - freezing stepper\n"));

        // Freeze the stepper by disabling all triggers
        m_bvFrozenTriggers = 0;

        //
        // We dont explicitly disable single-stepping because the OS
        // gives us a new thread context during an exception.  Since
        // all func-evals are done inside exceptions, we should never
        // have this problem.
        //
        // Note: however, that if func-evals were no longer done in
        // exceptions, this would have to change.
        //


        if (IsMethodEnterEnabled())
        {
            m_bvFrozenTriggers |= kMethodEnter;
            DisableMethodEnter();
        }

    }
    else
    {
        LOG((LF_CORDB, LL_INFO100000, "DS::TFEEnter - new nest=%d\n", m_cFuncEvalNesting));
    }
}

// On Func-EvalExit, we check if the stepper is trying to step-out of a func-eval
// (in which case we kill it)
// or if we previously entered this func-eval and should thaw it now.
void DebuggerStepper::TriggerFuncEvalExit(Thread * thread)
{
    LOG((LF_CORDB, LL_INFO10000, "DS::TFEExit, this=0x%p, old nest=%d\n", this, m_cFuncEvalNesting));

    // Since this is always called on the hijacking thread, we should be thread-safe
    _ASSERTE(thread == this->GetThread());

    if (IsDead())
        return;

    m_cFuncEvalNesting--;

    if (m_cFuncEvalNesting == -1)
    {
        LOG((LF_CORDB, LL_INFO100000, "DS::TFEExit - disabling stepper\n"));

        // we're exiting the func-eval session we were created in. So we just completely
        // disable ourselves so that we don't fire anything anymore.
        // The RS still has to free the stepper though.

        // This prevents us from stepping-out of a func-eval. For traditional steppers,
        // this is overkill since it won't have any outstanding triggers. (trap-step-out
        // won't patch if it crosses a func-eval frame).
        // But JMC-steppers have Method-Enter; and so this is the only place we have to
        // disable that.
        DisableAll();
    }
    else if (m_cFuncEvalNesting == 0)
    {
        // We're back to our starting Func-eval session, we should have been frozen,
        // so now we thaw.
        LOG((LF_CORDB, LL_INFO100000, "DS::TFEExit - thawing stepper\n"));

        // Thaw the stepper (reenable triggers)
        if ((m_bvFrozenTriggers & kMethodEnter) != 0)
        {
            EnableMethodEnter();
        }
        m_bvFrozenTriggers = 0;

    }
    else
    {
        LOG((LF_CORDB, LL_INFO100000, "DS::TFEExit - new nest=%d\n", m_cFuncEvalNesting));
    }
}


// Return true iff we set a patch (which implies to caller that we should
// let controller run free and hit that patch)
bool DebuggerStepper::TrapStepInto(ControllerStackInfo *info,
                                   const BYTE *ip,
                                   TraceDestination *pTD)
{
    _ASSERTE( pTD != NULL );
    _ASSERTE(this->GetDCType() == DEBUGGER_CONTROLLER_STEPPER);

    EnableTraceCall(LEAF_MOST_FRAME);
    if (IsCloserToRoot(info->m_activeFrame.fp, m_fpStepInto))
        m_fpStepInto = info->m_activeFrame.fp;

    LOG((LF_CORDB, LL_INFO1000, "Ds::TSI this:0x%x m_fpStepInto:0x%x\n",
        this, m_fpStepInto.GetSPValue()));

    TraceDestination trace;

    // Trace through the stubs.
    // If we're calling from managed code, this should either succeed
    // or become an ecall into mscorwks.
    // @Todo - what about stubs in mscorwks.
    // @todo - if this fails, we want to provde as much info as possible.
    if (!g_pEEInterface->TraceStub(ip, &trace)
        || !g_pEEInterface->FollowTrace(&trace))
    {
        return false;
    }


    (*pTD) = trace; //bitwise copy

    // Step-in always operates at the leaf-most frame. Thus the frame pointer for any
    // patch for step-in should be LEAF_MOST_FRAME, regardless of whatever our current fp
    // is before the step-in.
    // Note that step-in may skip 'internal' frames (FrameInfo w/ internal=true) since
    // such frames may really just be a marker for an internal EE Frame on the stack.
    // However, step-out uses these frames b/c it may call frame->TraceFrame() on them.
    return PatchTrace(&trace,
                      LEAF_MOST_FRAME, // step-in is always leaf-most frame.
                      (m_rgfMappingStop&STOP_UNMANAGED)?(true):(false));
}

// Enable the JMC backstop for stepping on Step-In.
// This activate the JMC probes, which will provide a safety net
// to stop a stepper if the StubManagers don't predict the call properly.
// Ideally, this should never be necessary (because the SMs would do their job).
void DebuggerStepper::EnableJMCBackStop(MethodDesc * pStartMethod)
{
    // JMC steppers should not need the JMC backstop unless a thread inadvertently stops in an LCG method.
    //_ASSERTE(DEBUGGER_CONTROLLER_JMC_STEPPER != this->GetDCType());    

    // Since we should never hit the JMC backstop (since it's really a SM issue), we'll assert if we actually do.
    // However, there's 1 corner case here. If we trace calls at the start of the method before the JMC-probe, 
    // then we'll still hit the JMC backstop in our own method.
    // Record that starting method. That way, if we end up hitting our JMC backstop in our own method,
    // we don't over aggressively fire the assert. (This won't work for recursive cases, but since this is just
    // changing an assert, we don't care).

#ifdef _DEBUG    
    // May be NULL if we didn't start in a method.
    m_StepInStartMethod = pStartMethod;
#endif    

    // We don't want traditional steppers to rely on MethodEnter (b/c it's not guaranteed to be correct),
    // but it may be a useful last resort.
    this->EnableMethodEnter();    
}

// Return true if the stepper can run free.
bool DebuggerStepper::TrapStepInHelper(
    ControllerStackInfo * pInfo,
    const BYTE * ipCallTarget,
    const BYTE * ipNext,
    bool fCallingIntoFunclet)
{
    TraceDestination td;

#ifdef _DEBUG
    // Begin logging the step-in activity in debug builds.
    StubManager::DbgBeginLog((TADDR) ipNext, (TADDR) ipCallTarget);
#endif


    if (TrapStepInto(pInfo, ipCallTarget, &td))
    {
        // If we placed a patch, see if we need to update our step-reason
        if (td.GetTraceType() == TRACE_MANAGED )
        {
            // Possible optimization: Roll all of g_pEEInterface calls into
            // one function so we don't repeatedly get the CodeMan,etc
            MethodDesc *md = NULL;
            _ASSERTE( g_pEEInterface->IsManagedNativeCode((const BYTE *)td.GetAddress()) );
            md = g_pEEInterface->GetNativeCodeMethodDesc(td.GetAddress());

            DebuggerJitInfo* pDJI = g_pDebugger->GetJitInfoFromAddr(td.GetAddress());
            CodeRegionInfo code = CodeRegionInfo::GetCodeRegionInfo(pDJI, md);
            if (code.AddressToOffset((const BYTE *)td.GetAddress()) == 0)
            {

                LOG((LF_CORDB,LL_INFO1000,"\tDS::TS 0x%x m_reason = STEP_CALL"
                     "@ip0x%x\n", this, (BYTE*)GetControlPC(&(pInfo->m_activeFrame.registers))));
                  m_reason = STEP_CALL;
            }
            else
            {
                LOG((LF_CORDB, LL_INFO1000, "Didn't step: md:0x%x"
                     "td.type:%s td.address:0x%p,  hot code address:0x%p\n",
                     md, GetTType(td.GetTraceType()), td.GetAddress(),
                    code.getAddrOfHotCode()));
            }
        }
        else
        {
            LOG((LF_CORDB,LL_INFO10000,"DS::TS else 0x%x m_reason = STEP_CALL\n",
                 this));
            m_reason = STEP_CALL;
        }


        return true;
    } // end TrapStepIn
    else
    {
        // If we can't figure out where the stepper should call into (likely because we can't find a stub-manager),
        // then enable the JMC backstop.
        EnableJMCBackStop(pInfo->m_activeFrame.md);

    }

    // We ignore ipNext here. Instead we'll return false and let the caller (TrapStep)
    // set the patch for us.
    return false;
}

FORCEINLINE bool IsTailCall(const BYTE * pTargetIP)
{
    return TailCallStubManager::IsTailCallStubHelper(reinterpret_cast<PCODE>(pTargetIP));
}

// bool DebuggerStepper::TrapStep()   TrapStep attepts to set a
// patch at the next IL instruction to be executed.  If we're stepping in &
// the next IL instruction is a call, then this'll set a breakpoint inside
// the code that will be called.
// How: There are a number of cases, depending on where the IP
// currently is:
// Unmanaged code: EnableTraceCall() & return false - try and get
// it when it returns.
// In a frame: if the <p in> param is true, then do an
// EnableTraceCall().  If the frame isn't the top frame, also do
// g_pEEInterface->TraceFrame(), g_pEEInterface->FollowTrace, and
// PatchTrace.
// Normal managed frame: create a Walker and walk the instructions until either
// leave the provided range (AddPatch there, return true), or we don't know what the
// next instruction is (say, after a call, or return, or branch - return false).
// Returns a boolean indicating if we were able to set a patch successfully
// in either this method, or (if in == true & the next instruction is a call)
// inside a callee method.
// true:   Patch successfully placed either in this method or a callee,
// so the stepping is taken care of.
// false:  Unable to place patch in either this method or any
// applicable callee methods, so the only option the caller has to put
// patch to control flow is to call TrapStepOut & try and place a patch
// on the method that called the current frame's method.
bool DebuggerStepper::TrapStep(ControllerStackInfo *info, bool in)
{
    LOG((LF_CORDB,LL_INFO10000,"DS::TS: this:0x%x\n", this));
    if (!info->m_activeFrame.managed)
    {
        //
        // We're not in managed code.  Patch up all paths back in.
        //

        LOG((LF_CORDB,LL_INFO10000, "DS::TS: not in managed code\n"));

        if (in)
        {
            EnablePolyTraceCall();
        }

        return false;
    }

    if (info->m_activeFrame.frame != NULL)
    {

        //
        // We're in some kind of weird frame.  Patch further entry to the frame.
        // or if we can't, patch return from the frame
        //

        LOG((LF_CORDB,LL_INFO10000, "DS::TS: in a weird frame\n"));

        if (in)
        {
            EnablePolyTraceCall();

            // Only traditional steppers should patch a frame. JMC steppers will
            // just rely on TriggerMethodEnter.
            if (DEBUGGER_CONTROLLER_STEPPER == this->GetDCType())
            {
                if (info->m_activeFrame.frame != FRAME_TOP)
                {
                    TraceDestination trace;

                    CONTRACT_VIOLATION(GCViolation); // TraceFrame GC-triggers

                    // This could be anywhere, especially b/c step could be on non-leaf frame.
                    if (g_pEEInterface->TraceFrame(this->GetThread(),
                                                   info->m_activeFrame.frame,
                                                   FALSE, &trace,
                                                   &(info->m_activeFrame.registers))
                        && g_pEEInterface->FollowTrace(&trace)
                        && PatchTrace(&trace, info->m_activeFrame.fp,
                                      (m_rgfMappingStop&STOP_UNMANAGED)?
                                        (true):(false)))

                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }

#ifdef _TARGET_X86_
    LOG((LF_CORDB,LL_INFO1000, "GetJitInfo for pc = 0x%x (addr of "
        "that value:0x%x)\n", (const BYTE*)(GetControlPC(&info->m_activeFrame.registers)),
        info->m_activeFrame.registers.PCTAddr));
#endif

    // Note: we used to pass in the IP from the active frame to GetJitInfo, but there seems to be no value in that, and
    // it was causing problems creating a stepper while sitting in ndirect stubs after we'd returned from the unmanaged
    // function that had been called.
    DebuggerJitInfo *ji = info->m_activeFrame.GetJitInfoFromFrame();
    if( ji != NULL )
    {
        LOG((LF_CORDB,LL_INFO10000,"DS::TS: For code 0x%p, got DJI 0x%p, "
            "from 0x%p to 0x%p\n",
            (const BYTE*)(GetControlPC(&info->m_activeFrame.registers)),
            ji, ji->m_addrOfCode, ji->m_addrOfCode+ji->m_sizeOfCode));
    }
    else
    {
        LOG((LF_CORDB,LL_INFO10000,"DS::TS: For code 0x%p, "
            "didn't get a DJI \n",
            (const BYTE*)(GetControlPC(&info->m_activeFrame.registers))));
    }

    //
    // We're in a normal managed frame - walk the code
    //

    NativeWalker walker;

    LOG((LF_CORDB,LL_INFO1000, "DS::TS: &info->m_activeFrame.registers 0x%p\n", &info->m_activeFrame.registers));

    // !!! Eventually when using the fjit, we'll want
    // to walk the IL to get the next location, & then map
    // it back to native.
    walker.Init((BYTE*)GetControlPC(&(info->m_activeFrame.registers)), &info->m_activeFrame.registers);


    // Is the active frame really the active frame?
    // What if the thread is stopped at a managed debug event outside of a filter ctx? Eg, stopped
    // somewhere directly in mscorwks (like sending a LogMsg or ClsLoad event) or even at WaitForSingleObject.
    // ActiveFrame is either the stepper's initial frame or the frame of a filterctx.
    bool fIsActiveFrameLive = (info->m_activeFrame.fp == info->m_bottomFP);

    // If this thread isn't stopped in managed code, it can't be at the active frame.
    if (GetManagedStoppedCtx(this->GetThread()) == NULL)
    {
        fIsActiveFrameLive = false;
    }

    bool fIsJump             = false;
    bool fCallingIntoFunclet = false;

    // If m_activeFrame is not the actual active frame,
    // we should skip this first switch - never single step, and
    // assume our context is bogus.
    if (fIsActiveFrameLive)
    {
        LOG((LF_CORDB,LL_INFO10000, "DC::TS: immediate?\n"));

        // Note that by definition our walker must always be able to step
        // through a single instruction, so any return
        // of NULL IP's from those cases on the first step
        // means that an exception is going to be generated.
        //
        // (On future steps, it can also mean that the destination
        // simply can't be computed.)
        WALK_TYPE wt = walker.GetOpcodeWalkType();
        {            
            switch (wt)
            {
            case WALK_RETURN:
                {
                    LOG((LF_CORDB,LL_INFO10000, "DC::TS:Imm:WALK_RETURN\n"));

                    // Normally a 'ret' opcode means we're at the end of a function and doing a step-out.
                    // But the jit is free to use a 'ret' opcode to implement various goofy constructs like
                    // managed filters, in which case we may ret to the same function or we may ret to some
                    // internal CLR stub code.
                    // So we'll just ignore this and tell the Stepper to enable every notification it has
                    // and let the thread run free. This will include TrapStepOut() and EnableUnwind()
                    // to catch any potential filters.


                    // Go ahead and enable the single-step flag too. We know it's safe.
                    // If this lands in random code, then TriggerSingleStep will just ignore it.
                    EnableSingleStep();

                    // Don't set step-reason yet. If another trigger gets hit, it will set the reason.
                    return false;
                }

            case WALK_BRANCH:
                LOG((LF_CORDB,LL_INFO10000, "DC::TS:Imm:WALK_BRANCH\n"));
                // A branch can be handled just like a call. If the branch is within the current method, then we just
                // down to WALK_UNKNOWN, otherwise we handle it just like a call.  Note: we need to force in=true
                // because for a jmp, in or over is the same thing, we're still going there, and the in==true case is
                // the case we want to use...
                fIsJump = true;

                // fall through...

            case WALK_CALL:
                LOG((LF_CORDB,LL_INFO10000, "DC::TS:Imm:WALK_CALL ip=%p nextip=%p\n", walker.GetIP(), walker.GetNextIP()));

                // If we're doing some sort of intra-method jump (usually, to get EIP in a clever way, via the CALL
                // instruction), then put the bp where we're going, NOT at the instruction following the call
                if (IsAddrWithinFrame(ji, info->m_activeFrame.md, walker.GetIP(), walker.GetNextIP()))
                {
                    LOG((LF_CORDB, LL_INFO1000, "Walk call within method!" ));
                    goto LWALK_UNKNOWN;
                }

                if (walker.GetNextIP() != NULL)
                {
#ifdef WIN64EXCEPTIONS
                    // There are 4 places we could be jumping:
                    // 1) to the beginning of the same method (recursive call)
                    // 2) somewhere in the same funclet, that isn't the method start
                    // 3) somewhere in the same method but different funclet
                    // 4) somewhere in a different method
                    //
                    // IsAddrWithinFrame ruled out option 2, IsAddrWithinMethodIncludingFunclet rules out option 4,
                    // and checking the IP against the start address rules out option 1. That leaves option only what we
                    // wanted, option #3
                    fCallingIntoFunclet = IsAddrWithinMethodIncludingFunclet(ji, info->m_activeFrame.md, walker.GetNextIP()) &&
                        ((CORDB_ADDRESS)(SIZE_T)walker.GetNextIP() != ji->m_addrOfCode);
#endif
                    // At this point, we know that the call/branch target is not in the current method.
                    // So if the current instruction is a jump, this must be a tail call or possibly a jump to the finally.
                    // So, check if the  call/branch target is the JIT helper for handling tail calls if we are not calling
                    // into the funclet.
                    if ((fIsJump && !fCallingIntoFunclet) || IsTailCall(walker.GetNextIP()))
                    {
                        // A step-over becomes a step-out for a tail call.
                        if (!in)
                        {
                            TrapStepOut(info);
                            return true;
                        }
                    }

                    // To preserve the old behaviour, if this is not a tail call, then we assume we want to
                    // follow the call/jump.
                    if (fIsJump) 
                    {
                        in = true;
                    }


                    // There are two cases where we need to perform a step-in.  One, if the step operation is
                    // a step-in.  Two, if the target address of the call is in a funclet of the current method.
                    // In this case, we want to step into the funclet even if the step operation is a step-over.
                    if (in || fCallingIntoFunclet)
                    {
                        if (TrapStepInHelper(info, walker.GetNextIP(), walker.GetSkipIP(), fCallingIntoFunclet))
                        {
                            return true;
                        }
                    }

                }
                if (walker.GetSkipIP() == NULL)
                {
                    LOG((LF_CORDB,LL_INFO10000,"DS::TS 0x%x m_reason = STEP_CALL (skip)\n",
                         this));
                    m_reason = STEP_CALL;

                    return true;
                }


                LOG((LF_CORDB,LL_INFO100000, "DC::TS:Imm:WALK_CALL Skip instruction\n"));
                walker.Skip();
                break;

            case WALK_UNKNOWN:
    LWALK_UNKNOWN:
                LOG((LF_CORDB,LL_INFO10000,"DS::TS:WALK_UNKNOWN - curIP:0x%x "
                    "nextIP:0x%x skipIP:0x%x 1st byte of opcode:0x%x\n", (BYTE*)GetControlPC(&(info->m_activeFrame.
                    registers)), walker.GetNextIP(),walker.GetSkipIP(),
                    *(BYTE*)GetControlPC(&(info->m_activeFrame.registers))));

                EnableSingleStep();

                return true;

            default:
                if (walker.GetNextIP() == NULL)
                {
                    return true;
                }

                walker.Next();
            }
        }
    } // if (fIsActiveFrameLive)

    //
    // Use our range, if we're in the original
    // frame.
    //

    COR_DEBUG_STEP_RANGE    *range;
    SIZE_T                    rangeCount;

    if (info->m_activeFrame.fp == m_fp)
    {
        range = m_range;
        rangeCount = m_rangeCount;
    }
    else
    {
        range = NULL;
        rangeCount = 0;
    }

    //
    // Keep walking until either we're out of range, or
    // else we can't predict ahead any more.
    //

    while (TRUE)
    {
        const BYTE *ip = walker.GetIP();

        SIZE_T offset = CodeRegionInfo::GetCodeRegionInfo(ji, info->m_activeFrame.md).AddressToOffset(ip);

        LOG((LF_CORDB, LL_INFO1000, "Walking to ip 0x%p (natOff:0x%x)\n",ip,offset));

        if (!IsInRange(offset, range, rangeCount)
            && !ShouldContinueStep( info, offset ))
        {
            AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                     ji,
                     offset,
                     info->m_returnFrame.fp,
                     NULL);
            return true;
        }

        switch (walker.GetOpcodeWalkType())
        {
        case WALK_RETURN:

            LOG((LF_CORDB, LL_INFO10000, "DS::TS: WALK_RETURN Adding Patch.\n"));

            // In the loop above, if we're at the return address, we'll check & see
            // if we're returning to elsewhere within the same method, and if so,
            // we'll single step rather than TrapStepOut. If we see a return in the
            // code stream, then we'll set a breakpoint there, so that we can
            // examine the return address, and decide whether to SS or TSO then
            AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                     ji,
                     offset,
                     info->m_returnFrame.fp,
                     NULL);
            return true;

        case WALK_CALL:

            LOG((LF_CORDB, LL_INFO10000, "DS::TS: WALK_CALL.\n"));

            // If we're doing some sort of intra-method jump (usually, to get EIP in a clever way, via the CALL
            // instruction), then put the bp where we're going, NOT at the instruction following the call
            if (IsAddrWithinFrame(ji, info->m_activeFrame.md, walker.GetIP(), walker.GetNextIP()))
            {
                LOG((LF_CORDB, LL_INFO10000, "DS::TS: WALK_CALL IsAddrWithinFrame, Adding Patch.\n"));

                // How else to detect this?
                AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                         ji,
                         CodeRegionInfo::GetCodeRegionInfo(ji, info->m_activeFrame.md).AddressToOffset(walker.GetNextIP()),
                         info->m_returnFrame.fp,
                         NULL);
                return true;
            }

            if (IsTailCall(walker.GetNextIP()))
            {
                if (!in)
                {
                    AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md, 
                                                         ji, 
                                                         offset, 
                                                         info->m_returnFrame.fp, 
                                                         NULL);
                    return true;
                }
            }

#ifdef WIN64EXCEPTIONS
            fCallingIntoFunclet = IsAddrWithinMethodIncludingFunclet(ji, info->m_activeFrame.md, walker.GetNextIP());
#endif
            if (in || fCallingIntoFunclet)
            {
                LOG((LF_CORDB, LL_INFO10000, "DS::TS: WALK_CALL step in is true\n"));
                if (walker.GetNextIP() == NULL)
                {
                    LOG((LF_CORDB, LL_INFO10000, "DS::TS: WALK_CALL NextIP == NULL\n"));
                    AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                             ji,
                             offset,
                             info->m_returnFrame.fp,
                             NULL);

                    LOG((LF_CORDB,LL_INFO10000,"DS0x%x m_reason=STEP_CALL 2\n",
                         this));
                    m_reason = STEP_CALL;

                    return true;
                }

                if (TrapStepInHelper(info, walker.GetNextIP(), walker.GetSkipIP(), fCallingIntoFunclet))
                {
                    return true;
                }

            }

            LOG((LF_CORDB, LL_INFO10000, "DS::TS: WALK_CALL Calling GetSkipIP\n"));
            if (walker.GetSkipIP() == NULL)
            {
                AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                         ji,
                         offset,
                         info->m_returnFrame.fp,
                         NULL);

                LOG((LF_CORDB,LL_INFO10000,"DS 0x%x m_reason=STEP_CALL4\n",this));
                m_reason = STEP_CALL;

                return true;
            }

            walker.Skip();
            LOG((LF_CORDB, LL_INFO10000, "DS::TS: skipping over call.\n"));
            break;

        default:
            if (walker.GetNextIP() == NULL)
            {
                AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                    ji,
                    offset,
                    info->m_returnFrame.fp,
                    NULL);
                return true;
            }
            walker.Next();
            break;
        }
    }
    LOG((LF_CORDB,LL_INFO1000,"Ending TrapStep\n"));
}

bool DebuggerStepper::IsAddrWithinFrame(DebuggerJitInfo *dji,
                                        MethodDesc* pMD,
                                        const BYTE* currentAddr,
                                        const BYTE* targetAddr)
{
    _ASSERTE(dji != NULL);

    bool result = IsAddrWithinMethodIncludingFunclet(dji, pMD, targetAddr);

    // We need to check if this is a recursive call.  In RTM we should see if this method is really necessary,
    // since it looks like the X86 JIT doesn't emit intra-method jumps anymore.
    if (result)
    {
        if ((CORDB_ADDRESS)(SIZE_T)targetAddr == dji->m_addrOfCode)
        {
            result = false;
        }
    }

#if defined(WIN64EXCEPTIONS)
    // On WIN64, we also check whether the targetAddr and the currentAddr is in the same funclet.
    _ASSERTE(currentAddr != NULL);
    if (result)
    {
        int currentFuncletIndex = dji->GetFuncletIndex((CORDB_ADDRESS)currentAddr, DebuggerJitInfo::GFIM_BYADDRESS);
        int targetFuncletIndex  = dji->GetFuncletIndex((CORDB_ADDRESS)targetAddr,  DebuggerJitInfo::GFIM_BYADDRESS);
        result = (currentFuncletIndex == targetFuncletIndex);
    }
#endif // WIN64EXCEPTIONS

    return result;
}

// x86 shouldn't need to call this method directly.  We should call IsAddrWithinFrame() on x86 instead.
// That's why I use a name with the word "funclet" in it to scare people off.
bool DebuggerStepper::IsAddrWithinMethodIncludingFunclet(DebuggerJitInfo *dji,
                                                         MethodDesc* pMD,
                                                         const BYTE* targetAddr)
{
    _ASSERTE(dji != NULL);
    return CodeRegionInfo::GetCodeRegionInfo(dji, pMD).IsMethodAddress(targetAddr);
}

void DebuggerStepper::TrapStepNext(ControllerStackInfo *info)
{
    LOG((LF_CORDB, LL_INFO10000, "DS::TrapStepNext, this=%p\n", this));
    // StepNext for a Normal stepper is just a step-out
    TrapStepOut(info);

    // @todo -should we also EnableTraceCall??
}

// Is this frame interesting?
// For a traditional stepper, all frames are interesting.
bool DebuggerStepper::IsInterestingFrame(FrameInfo * pFrame)
{
    LIMITED_METHOD_CONTRACT;

    return true;
}

// Place a single patch somewhere up the stack to do a step-out
void DebuggerStepper::TrapStepOut(ControllerStackInfo *info, bool fForceTraditional)
{
    ControllerStackInfo returnInfo;
    DebuggerJitInfo *dji;

    LOG((LF_CORDB, LL_INFO10000, "DS::TSO this:0x%p\n", this));

    bool fReturningFromFinallyFunclet = false;

#if defined(WIN64EXCEPTIONS)
    // When we step out of a funclet, we should do one of two things, depending
    // on the original stepping intention:
    // 1) If we originally want to step out, then we should skip the parent method.
    // 2) If we originally want to step in/over but we step off the end of the funclet,
    //    then we should resume in the parent, if possible.
    if (info->m_activeFrame.IsNonFilterFuncletFrame())
    {
        // There should always be a frame for the parent method.
        _ASSERTE(info->HasReturnFrame());
        
#ifdef _TARGET_ARM_
        while (info->HasReturnFrame() && info->m_activeFrame.md != info->m_returnFrame.md)
        {
            StackTraceTicket ticket(info);
            returnInfo.GetStackInfo(ticket, GetThread(), info->m_returnFrame.fp, NULL);
            info = &returnInfo;
        }
        
        _ASSERTE(info->HasReturnFrame());
#endif

        _ASSERTE(info->m_activeFrame.md == info->m_returnFrame.md);

        if (m_eMode == cStepOut)
        {
            StackTraceTicket ticket(info);
            returnInfo.GetStackInfo(ticket, GetThread(), info->m_returnFrame.fp, NULL);
            info = &returnInfo;
        }
        else
        {
            _ASSERTE(info->m_returnFrame.managed);
            _ASSERTE(info->m_returnFrame.frame == NULL);

            MethodDesc *md = info->m_returnFrame.md;
            dji = info->m_returnFrame.GetJitInfoFromFrame();

            // The return value of a catch funclet is the control PC to resume to.
            // The return value of a finally funclet has no meaning, so we need to check
            // if the return value is in the main method.
            LPVOID resumePC = GetRegdisplayReturnValue(&(info->m_activeFrame.registers));

            // For finally funclet, there are two possible situations.  Either the finally is
            // called normally (i.e. no exception), in which case we simply fall through and
            // let the normal loop do its work below, or the finally is called by the EH
            // routines, in which case we need the unwind notification.
            if (IsAddrWithinMethodIncludingFunclet(dji, md, (const BYTE *)resumePC))
            {
                SIZE_T reloffset = dji->m_codeRegionInfo.AddressToOffset((BYTE*)resumePC);

                AddBindAndActivateNativeManagedPatch(info->m_returnFrame.md,
                    dji,
                    reloffset,
                    info->m_returnFrame.fp,
                    NULL);

                LOG((LF_CORDB, LL_INFO10000,
                     "DS::TSO:normally managed code AddPatch"
                     " in %s::%s, offset 0x%x, m_reason=%d\n",
                     info->m_returnFrame.md->m_pszDebugClassName,
                     info->m_returnFrame.md->m_pszDebugMethodName,
                     reloffset, m_reason));

                // Do not set m_reason to STEP_RETURN here.  Logically, the funclet and the parent method are the
                // same method, so we should not "return" to the parent method.
                LOG((LF_CORDB, LL_INFO10000,"DS::TSO: done\n"));

                return;
            }
            else
            {
                // This is the case where we step off the end of a finally funclet.
                fReturningFromFinallyFunclet = true;
            }
        }
    }
#endif // WIN64EXCEPTIONS

#ifdef _DEBUG
    FramePointer dbgLastFP; // for debug, make sure we're making progress through the stack.
#endif

    while (info->HasReturnFrame())
    {

#ifdef _DEBUG
        dbgLastFP = info->m_activeFrame.fp;
#endif

        // Continue walking up the stack & set a patch upon the next
        // frame up.  We will eventually either hit managed code
        // (which we can set a definite patch in), or the top of the
        // stack.
        StackTraceTicket ticket(info);

        // The last parameter here is part of a really targetted (*cough* dirty) fix to
        // disable getting an unwanted UMChain to fix issue 650903 (See
        // code:ControllerStackInfo::WalkStack and code:TrackUMChain for the other
        // parts.) In the case of managed step out we know that we aren't interested in
        // unmanaged frames, and generating that unmanaged frame causes the stackwalker
        // not to report the managed frame that was at the same SP. However the unmanaged
        // frame might be used in the mixed-mode step out case so I don't suppress it
        // there.
        returnInfo.GetStackInfo(ticket, GetThread(), info->m_returnFrame.fp, NULL, !(m_rgfMappingStop & STOP_UNMANAGED));
        info = &returnInfo;

#ifdef _DEBUG
        // If this assert fires, then it means that we're not making progress while
        // tracing up the towards the root of the stack. Likely an issue in the Left-Side's
        // stackwalker.
        _ASSERTE(IsCloserToLeaf(dbgLastFP, info->m_activeFrame.fp));
#endif

#ifdef FEATURE_MULTICASTSTUB_AS_IL
        if (info->m_activeFrame.md != nullptr && info->m_activeFrame.md->IsILStub() && info->m_activeFrame.md->AsDynamicMethodDesc()->IsMulticastStub())
        {
            LOG((LF_CORDB, LL_INFO10000,
                 "DS::TSO: multicast frame.\n"));
            
            // User break should always be called from managed code, so it should never actually hit this codepath.
            _ASSERTE(GetDCType() != DEBUGGER_CONTROLLER_USER_BREAKPOINT);
            
            // JMC steppers shouldn't be patching stubs.
            if (DEBUGGER_CONTROLLER_JMC_STEPPER == this->GetDCType())
            {
                LOG((LF_CORDB, LL_INFO10000, "DS::TSO: JMC stepper skipping frame.\n"));
                continue;
            }
            
            TraceDestination trace;
            
            EnableTraceCall(info->m_activeFrame.fp);
            
            PCODE ip = GetControlPC(&(info->m_activeFrame.registers));
            if (g_pEEInterface->TraceStub((BYTE*)ip, &trace)
                && g_pEEInterface->FollowTrace(&trace)
                && PatchTrace(&trace, info->m_activeFrame.fp,
                              true))
                break;          
        }
        else 
#endif // FEATURE_MULTICASTSTUB_AS_IL
        if (info->m_activeFrame.managed)
        {
            LOG((LF_CORDB, LL_INFO10000,
                 "DS::TSO: return frame is managed.\n"));

            if (info->m_activeFrame.frame == NULL)
            {
                // Returning normally to managed code.
                _ASSERTE(info->m_activeFrame.md != NULL);

                // Polymorphic check to skip over non-interesting frames.
                if (!fForceTraditional && !this->IsInterestingFrame(&info->m_activeFrame))
                    continue;

                dji = info->m_activeFrame.GetJitInfoFromFrame();
                _ASSERTE(dji != NULL);

                // Note: we used to pass in the IP from the active frame to GetJitInfo, but there seems to be no value
                // in that, and it was causing problems creating a stepper while sitting in ndirect stubs after we'd
                // returned from the unmanaged function that had been called.
                ULONG reloffset = info->m_activeFrame.relOffset;

                AddBindAndActivateNativeManagedPatch(info->m_activeFrame.md,
                    dji,
                    reloffset,
                    info->m_returnFrame.fp,
                    NULL);

                LOG((LF_CORDB, LL_INFO10000,
                     "DS::TSO:normally managed code AddPatch"
                     " in %s::%s, offset 0x%x, m_reason=%d\n",
                     info->m_activeFrame.md->m_pszDebugClassName,
                     info->m_activeFrame.md->m_pszDebugMethodName,
                     reloffset, m_reason));


                // Do not set m_reason to STEP_RETURN here.  Logically, the funclet and the parent method are the
                // same method, so we should not "return" to the parent method.
                if (!fReturningFromFinallyFunclet)
                {
                    m_reason = STEP_RETURN;
                }
                break;
            }
            else if (info->m_activeFrame.frame == FRAME_TOP)
            {

                // Trad-stepper's step-out is actually like a step-next when we go off the top.
                // JMC-steppers do a true-step out. So for JMC-steppers, don't enable trace-call.
                if (DEBUGGER_CONTROLLER_JMC_STEPPER == this->GetDCType())
                {
                    LOG((LF_CORDB, LL_EVERYTHING, "DS::TSO: JMC stepper skipping exit-frame case.\n"));
                    break;
                }

                // User break should always be called from managed code, so it should never actually hit this codepath.
                _ASSERTE(GetDCType() != DEBUGGER_CONTROLLER_USER_BREAKPOINT);


                // We're walking off the top of the stack. Note that if we call managed code again,
                // this trace-call will cause us our stepper-to fire. So we'll actually do a
                // step-next; not a true-step out.
                EnableTraceCall(info->m_activeFrame.fp);

                LOG((LF_CORDB, LL_INFO1000, "DS::TSO: Off top of frame!\n"));

                m_reason = STEP_EXIT; //we're on the way out..

                // <REVISIT_TODO>@todo not that it matters since we don't send a
                // stepComplete message to the right side.</REVISIT_TODO>
                break;
            }
            else if (info->m_activeFrame.frame->GetFrameType() == Frame::TYPE_FUNC_EVAL)
            {
                // Note: we treat walking off the top of the stack and
                // walking off the top of a func eval the same way,
                // except that we don't enable trace call since we
                // know exactly where were going.

                LOG((LF_CORDB, LL_INFO1000,
                     "DS::TSO: Off top of func eval!\n"));

                m_reason = STEP_EXIT;
                break;
            }
            else if (info->m_activeFrame.frame->GetFrameType() == Frame::TYPE_SECURITY &&
                     info->m_activeFrame.frame->GetInterception() == Frame::INTERCEPTION_NONE)
            {
                // If we're stepping out of something that was protected by (declarative) security,
                // the security subsystem may leave a frame on the stack to cache it's computation.
                // HOWEVER, this isn't a real frame, and so we don't want to stop here.  On the other
                // hand, if we're in the security goop (sec. executes managed code to do stuff), then
                // we'll want to use the "returning to stub case", below.  GetInterception()==NONE
                // indicates that the frame is just a cache frame:
                // Skip it and keep on going

                LOG((LF_CORDB, LL_INFO10000,
                     "DS::TSO: returning to a non-intercepting frame. Keep unwinding\n"));
                continue;
            }
            else
            {
                LOG((LF_CORDB, LL_INFO10000,
                     "DS::TSO: returning to a stub frame.\n"));

                // User break should always be called from managed code, so it should never actually hit this codepath.
                _ASSERTE(GetDCType() != DEBUGGER_CONTROLLER_USER_BREAKPOINT);

                // JMC steppers shouldn't be patching stubs.
                if (DEBUGGER_CONTROLLER_JMC_STEPPER == this->GetDCType())
                {
                    LOG((LF_CORDB, LL_INFO10000, "DS::TSO: JMC stepper skipping frame.\n"));
                    continue;
                }

                // We're returning to some funky frame.
                // (E.g. a security frame has called a native method.)

                // Patch the frame from entering other methods. This effectively gives the Step-out
                // a step-next behavior. For eg, this can be useful for step-out going between multicast delegates.
                // This step-next could actually land us leaf-more on the callstack than we currently are!
                // If we were a true-step out, we'd skip this and keep crawling.
                // up the callstack.
                //
                // !!! For now, we assume that the TraceFrame entry
                // point is smart enough to tell where it is in the
                // calling sequence.  We'll see how this holds up.
                TraceDestination trace;

                // We don't want notifications of trace-calls leaf-more than our current frame.
                // For eg, if our current frame calls out to unmanaged code and then back in,
                // we'll get a TraceCall notification. But since it's leaf-more than our current frame,
                // we don't care because we just want to step out of our current frame (and everything
                // our current frame may call).
                EnableTraceCall(info->m_activeFrame.fp);

                CONTRACT_VIOLATION(GCViolation); // TraceFrame GC-triggers

                if (g_pEEInterface->TraceFrame(GetThread(),
                                               info->m_activeFrame.frame, FALSE,
                                               &trace, &(info->m_activeFrame.registers))
                    && g_pEEInterface->FollowTrace(&trace)
                    && PatchTrace(&trace, info->m_activeFrame.fp,
                                  true))
                    break;

                // !!! Problem: we don't know which return frame to use -
                // the TraceFrame patch may be in a frame below the return
                // frame, or in a frame parallel with it
                // (e.g. prestub popping itself & then calling.)
                //
                // For now, I've tweaked the FP comparison in the
                // patch dispatching code to allow either case.
            }
        }
        else
        {
            LOG((LF_CORDB, LL_INFO10000,
                 "DS::TSO: return frame is not managed.\n"));

            // Only step out to unmanaged code if we're actually
            // marked to stop in unamanged code. Otherwise, just loop
            // to get us past the unmanaged frames.
            if (m_rgfMappingStop & STOP_UNMANAGED)
            {
                LOG((LF_CORDB, LL_INFO10000,
                     "DS::TSO: return to unmanaged code "
                     "m_reason=STEP_RETURN\n"));

                // Do not set m_reason to STEP_RETURN here.  Logically, the funclet and the parent method are the
                // same method, so we should not "return" to the parent method.
                if (!fReturningFromFinallyFunclet)
                {
                    m_reason = STEP_RETURN;
                }

                // We're stepping out into unmanaged code
                LOG((LF_CORDB, LL_INFO10000,
                 "DS::TSO: Setting unmanaged trace patch at 0x%x(%x)\n",
                     GetControlPC(&(info->m_activeFrame.registers)),
                     info->m_returnFrame.fp.GetSPValue()));


                AddAndActivateNativePatchForAddress((CORDB_ADDRESS_TYPE *)GetControlPC(&(info->m_activeFrame.registers)),
                         info->m_returnFrame.fp,
                         FALSE,
                         TRACE_UNMANAGED);

                break;

            }
        }
    }

    // <REVISIT_TODO>If we get here, we may be stepping out of the last frame.  Our thread
    // exit logic should catch this case. (@todo)</REVISIT_TODO>
    LOG((LF_CORDB, LL_INFO10000,"DS::TSO: done\n"));
}


// void DebuggerStepper::StepOut()
// Called by Debugger::HandleIPCEvent  to setup
// everything so that the process will step over the range of IL
// correctly.
// How: Converts the provided array of ranges from IL ranges to
// native ranges (if they're not native already), and then calls
// TrapStep or TrapStepOut, like so:
//   Get the appropriate MethodDesc & JitInfo
//   Iterate through array of IL ranges, use
//   JitInfo::MapILRangeToMapEntryRange to translate IL to native
//   ranges.
// Set member variables to remember that the DebuggerStepper now uses
// the ranges: m_range, m_rangeCount, m_stepIn, m_fp
// If (!TrapStep()) then {m_stepIn = true; TrapStepOut()}
// EnableUnwind( m_fp );
void DebuggerStepper::StepOut(FramePointer fp, StackTraceTicket ticket)
{
    LOG((LF_CORDB, LL_INFO10000, "Attempting to step out, fp:0x%x this:0x%x"
        "\n", fp.GetSPValue(), this ));

    Thread *thread = GetThread();


    CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);
    ControllerStackInfo info;

    // We pass in the ticket b/c this is called both when we're live (via
    // DebuggerUserBreakpoint) and when we're stopped (via normal StepOut)
    info.GetStackInfo(ticket, thread, fp, context);


    ResetRange();


    m_stepIn = FALSE;
    m_fp = info.m_activeFrame.fp;
#if defined(WIN64EXCEPTIONS)
    // We need to remember the parent method frame pointer here so that we will recognize
    // the range of the stepper as being valid when we return to the parent method.
    if (info.m_activeFrame.IsNonFilterFuncletFrame())
    {
        m_fpParentMethod = info.m_returnFrame.fp;
    }
#endif // WIN64EXCEPTIONS

    m_eMode = cStepOut;

    _ASSERTE((fp == LEAF_MOST_FRAME) || (info.m_activeFrame.md != NULL) || (info.m_returnFrame.md != NULL));

    TrapStepOut(&info);
    EnableUnwind(m_fp);
}

#define GROW_RANGES_IF_NECESSARY()                            \
    if (rTo == rToEnd)                                        \
    {                                                         \
        ULONG NewSize, OldSize;                               \
        if (!ClrSafeInt<ULONG>::multiply(sizeof(COR_DEBUG_STEP_RANGE), (ULONG)(realRangeCount*2), NewSize) || \
            !ClrSafeInt<ULONG>::multiply(sizeof(COR_DEBUG_STEP_RANGE), (ULONG)realRangeCount, OldSize) || \
            NewSize < OldSize)                                \
        {                                                     \
            DeleteInteropSafe(m_range);                       \
            m_range = NULL;                                   \
            return false;                                     \
        }                                                     \
        COR_DEBUG_STEP_RANGE *_pTmp = (COR_DEBUG_STEP_RANGE*) \
            g_pDebugger->GetInteropSafeHeap()->Realloc(m_range, \
                                         NewSize,             \
                                         OldSize);            \
                                                              \
        if (_pTmp == NULL)                                    \
        {                                                     \
            DeleteInteropSafe(m_range);                       \
            m_range = NULL;                                   \
            return false;                                     \
        }                                                     \
                                                              \
        m_range = _pTmp;                                      \
        rTo     = m_range + realRangeCount;                   \
        rToEnd  = m_range + (realRangeCount*2);               \
        realRangeCount *= 2;                                  \
    }

//-----------------------------------------------------------------------------
//  Given a set of IL ranges, convert them to native and cache them.
// Return true on success, false on error.
//-----------------------------------------------------------------------------
bool DebuggerStepper::SetRangesFromIL(DebuggerJitInfo *dji, COR_DEBUG_STEP_RANGE *ranges, SIZE_T rangeCount)
{
    CONTRACTL
    {
        WRAPPER(THROWS);
        GC_NOTRIGGER;
        PRECONDITION(ThisIsHelperThreadWorker()); // Only help initializes a stepper.
        PRECONDITION(m_range == NULL); // shouldn't be set already.
        PRECONDITION(CheckPointer(ranges));
        PRECONDITION(CheckPointer(dji));
    }
    CONTRACTL_END;

    // Note: we used to pass in the IP from the active frame to GetJitInfo, but there seems to be no value in that, and
    // it was causing problems creating a stepper while sitting in ndirect stubs after we'd returned from the unmanaged
    // function that had been called.
    MethodDesc *fd = dji->m_nativeCodeVersion.GetMethodDesc();

    // The "+1" is for internal use, when we need to
    // set an intermediate patch in pitched code.  Isn't
    // used unless the method is pitched & a patch is set
    // inside it.  Thus we still pass cRanges as the
    // range count.
    m_range = new (interopsafe) COR_DEBUG_STEP_RANGE[rangeCount+1];

    if (m_range == NULL)
        return false;

    TRACE_ALLOC(m_range);

    SIZE_T realRangeCount = rangeCount;

    if (dji != NULL)
    {
        LOG((LF_CORDB,LL_INFO10000,"DeSt::St: For code md=0x%x, got DJI 0x%x, from 0x%x to 0x%x\n",
            fd,
            dji, dji->m_addrOfCode, (ULONG)dji->m_addrOfCode
            + (ULONG)dji->m_sizeOfCode));

        //
        // Map ranges to native offsets for jitted code
        //
        COR_DEBUG_STEP_RANGE *r, *rEnd, *rTo, *rToEnd;

        r = ranges;
        rEnd = r + rangeCount;

        rTo = m_range;
        rToEnd  = rTo + realRangeCount;

        // <NOTE>
        // rTo may also be incremented in the middle of the loop on WIN64 platforms.
        // </NOTE>
        for (/**/; r < rEnd; r++, rTo++)
        {
            // If we are already at the end of our allocated array, but there are still
            // more ranges to copy over, then grow the array.
            GROW_RANGES_IF_NECESSARY();

            if (r->startOffset == 0 && r->endOffset == (ULONG) ~0)
            {
                // {0...-1} means use the entire method as the range
                // Code dup'd from below case.
                LOG((LF_CORDB, LL_INFO10000, "DS:Step: Have DJI, special (0,-1) entry\n"));
                rTo->startOffset = 0;
                rTo->endOffset   = (ULONG32)g_pEEInterface->GetFunctionSize(fd);
            }
            else
            {
                //
                // One IL range may consist of multiple
                // native ranges.
                //

                DebuggerILToNativeMap *mStart, *mEnd;

                dji->MapILRangeToMapEntryRange(r->startOffset,
                                                r->endOffset,
                                                &mStart,
                                                &mEnd);

                // Either mStart and mEnd are both NULL (we don't have any sequence point),
                // or they are both non-NULL.
                _ASSERTE( ((mStart == NULL) && (mEnd == NULL)) ||
                          ((mStart != NULL) && (mEnd != NULL)) );

                if (mStart == NULL)
                {
                    // <REVISIT_TODO>@todo Won't this result in us stepping across
                    // the entire method?</REVISIT_TODO>
                    rTo->startOffset = 0;
                    rTo->endOffset   = 0;
                }
                else if (mStart == mEnd)
                {
                    rTo->startOffset = mStart->nativeStartOffset;
                    rTo->endOffset   = mStart->nativeEndOffset;
                }
                else
                {
                    // Account for more than one continuous range here.

                    // Move the pointer back to work with the loop increment below.
                    // Don't dereference this pointer now!
                    rTo--;

                    for (DebuggerILToNativeMap* pMap = mStart;
                         pMap <= mEnd;
                         pMap = pMap + 1)
                    {
                        if ((pMap == mStart) ||
                            (pMap->nativeStartOffset != (pMap-1)->nativeEndOffset))
                        {
                            rTo++;
                            GROW_RANGES_IF_NECESSARY();

                            rTo->startOffset = pMap->nativeStartOffset;
                            rTo->endOffset   = pMap->nativeEndOffset;
                        }
                        else
                        {
                            // If we have continuous ranges, then lump them together.
                            _ASSERTE(rTo->endOffset == pMap->nativeStartOffset);
                            rTo->endOffset = pMap->nativeEndOffset;
                        }
                    }

                    LOG((LF_CORDB, LL_INFO10000, "DS:Step: nat off:0x%x to 0x%x\n", rTo->startOffset, rTo->endOffset));
                }
            }
        }

        rangeCount = (int)((BYTE*)rTo - (BYTE*)m_range) / sizeof(COR_DEBUG_STEP_RANGE);
    }
    else
    {
        // Even if we don't have debug info, we'll be able to
        // step through the method
        SIZE_T functionSize = g_pEEInterface->GetFunctionSize(fd);

        COR_DEBUG_STEP_RANGE *r    = ranges;
        COR_DEBUG_STEP_RANGE *rEnd = r + rangeCount;

        COR_DEBUG_STEP_RANGE *rTo  = m_range;

        for(/**/; r < rEnd; r++, rTo++)
        {
            if (r->startOffset == 0 && r->endOffset == (ULONG) ~0)
            {
                LOG((LF_CORDB, LL_INFO10000, "DS:Step:No DJI, (0,-1) special entry\n"));
                // Code dup'd from above case.
                // {0...-1} means use the entire method as the range
                rTo->startOffset = 0;
                rTo->endOffset   = (ULONG32)functionSize;
            }
            else
            {
                LOG((LF_CORDB, LL_INFO10000, "DS:Step:No DJI, regular entry\n"));
                // We can't just leave ths IL entry - we have to
                // get rid of it.
                // This will just be ignored
                rTo->startOffset = rTo->endOffset = (ULONG32)functionSize;
            }
        }
    }


    m_rangeCount = rangeCount;
    m_realRangeCount = rangeCount;

    return true;
}


// void DebuggerStepper::Step()  Tells the stepper to step over
// the provided ranges.
// void *fp:  frame pointer.
// bool in:  true if we want to step into a function within the range,
//        false if we want to step over functions within the range.
// COR_DEBUG_STEP_RANGE *ranges:  Assumed to be nonNULL, it will
//        always hold at least one element.
// SIZE_T rangeCount:  One less than the true number of elements in
//        the ranges argument.
// bool rangeIL:  true if the ranges are provided in IL (they'll be
//        converted to native before the DebuggerStepper uses them,
//      false if they already are native.
bool DebuggerStepper::Step(FramePointer fp, bool in,
                           COR_DEBUG_STEP_RANGE *ranges, SIZE_T rangeCount,
                           bool rangeIL)
{
    LOG((LF_CORDB, LL_INFO1000, "DeSt:Step this:0x%x  ", this));
    if (rangeCount>0)
        LOG((LF_CORDB,LL_INFO10000," start,end[0]:(0x%x,0x%x)\n",
             ranges[0].startOffset, ranges[0].endOffset));
    else
        LOG((LF_CORDB,LL_INFO10000," single step\n"));

    Thread *thread = GetThread();
    CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);
    
    // ControllerStackInfo doesn't report IL stubs, so if we are in an IL stub, we need
    // to handle the single-step specially.  There are probably other problems when we stop
    // in an IL stub.  We need to revisit this later.
    bool fIsILStub = false;
    if ((context != NULL) &&
        g_pEEInterface->IsManagedNativeCode(reinterpret_cast<const BYTE *>(GetIP(context))))
    {
        MethodDesc * pMD = g_pEEInterface->GetNativeCodeMethodDesc(GetIP(context));
        if (pMD != NULL)
        {
            fIsILStub = pMD->IsILStub();
        }
    }
    LOG((LF_CORDB, LL_INFO10000, "DS::S - fIsILStub = %d\n", fIsILStub));

    ControllerStackInfo info;


    StackTraceTicket ticket(thread);
    info.GetStackInfo(ticket, thread, fp, context);

    _ASSERTE((fp == LEAF_MOST_FRAME) || (info.m_activeFrame.md != NULL) ||
             (info.m_returnFrame.md != NULL));

    m_stepIn = in;

    DebuggerJitInfo *dji = info.m_activeFrame.GetJitInfoFromFrame();

    if (dji == NULL)
    {
        // !!! ERROR range step in frame with no code
        ranges = NULL;
        rangeCount = 0;
    }


    if (m_range != NULL)
    {
        TRACE_FREE(m_range);
        DeleteInteropSafe(m_range);
        m_range = NULL;
        m_rangeCount = 0;
        m_realRangeCount = 0;
    }

    if (rangeCount > 0)
    {
        if (rangeIL)
        {
            // IL ranges supplied, we need to convert them to native ranges.
            bool fOk = SetRangesFromIL(dji, ranges, rangeCount);
            if (!fOk)
            {
                return false;
            }
        }
        else
        {
            // Native ranges, already supplied. Just copy them over.
            m_range = new (interopsafe) COR_DEBUG_STEP_RANGE[rangeCount];

            if (m_range == NULL)
            {
                return false;
            }

            memcpy(m_range, ranges, sizeof(COR_DEBUG_STEP_RANGE) * rangeCount);
            m_realRangeCount  = m_rangeCount = rangeCount;
        }
        _ASSERTE(m_range != NULL);
        _ASSERTE(m_rangeCount > 0);
        _ASSERTE(m_realRangeCount > 0);
    }
    else
    {
        // !!! ERROR cannot map IL ranges
        ranges = NULL;
        rangeCount = 0;
    }

    if (fIsILStub)
    {
        // Don't use the ControllerStackInfo if we are in an IL stub.
        m_fp = fp;
    }
    else
    {
        m_fp = info.m_activeFrame.fp;
#if defined(WIN64EXCEPTIONS)
        // We need to remember the parent method frame pointer here so that we will recognize
        // the range of the stepper as being valid when we return to the parent method.
        if (info.m_activeFrame.IsNonFilterFuncletFrame())
        {
            m_fpParentMethod = info.m_returnFrame.fp;
        }
#endif // WIN64EXCEPTIONS
    }
    m_eMode = m_stepIn ? cStepIn : cStepOver;

    LOG((LF_CORDB,LL_INFO10000,"DS 0x%x STep: STEP_NORMAL\n",this));
    m_reason = STEP_NORMAL; //assume it'll be a normal step & set it to
    //something else if we walk over it
    if (fIsILStub)
    {
        LOG((LF_CORDB, LL_INFO10000, "DS:Step: stepping in an IL stub\n"));

        // Enable the right triggers if the user wants to step in.
        if (in)
        {
            if (this->GetDCType() == DEBUGGER_CONTROLLER_STEPPER)
            {
                EnableTraceCall(info.m_activeFrame.fp);
            }
            else if (this->GetDCType() == DEBUGGER_CONTROLLER_JMC_STEPPER)
            {
                EnableMethodEnter();
            }
        }

        // Also perform a step-out in case this IL stub is returning to managed code.
        // However, we must fix up the ControllerStackInfo first, since it doesn't 
        // report IL stubs.  The active frame reported by the ControllerStackInfo is
        // actually the return frame in this case.
        info.SetReturnFrameWithActiveFrame();
        TrapStepOut(&info);
    }
    else if (!TrapStep(&info, in))
    {
        LOG((LF_CORDB,LL_INFO10000,"DS:Step: Did TS\n"));
        m_stepIn = true;
        TrapStepNext(&info);
    }

    LOG((LF_CORDB,LL_INFO10000,"DS:Step: Did TS,TSO\n"));

    EnableUnwind(m_fp);

    return true;
}

// TP_RESULT DebuggerStepper::TriggerPatch()
// What: Triggers patch if we're not in a stub, and we're
// outside of the stepping range.  Otherwise sets another patch so as to
// step out of the stub, or in the next instruction within the range.
// How: If module==NULL & managed==> we're in a stub:
// TrapStepOut() and return false.  Module==NULL&!managed==> return
// true.  If m_range != NULL & execution is currently in the range,
// attempt a TrapStep (TrapStepOut otherwise) & return false.  Otherwise,
// return true.
TP_RESULT DebuggerStepper::TriggerPatch(DebuggerControllerPatch *patch,
                                   Thread *thread,
                                   TRIGGER_WHY tyWhy)
{
    LOG((LF_CORDB, LL_INFO10000, "DeSt::TP\n"));

    // If we're frozen, we may hit a patch but we just ignore it
    if (IsFrozen())
    {
        LOG((LF_CORDB, LL_INFO1000000, "DS::TP, ignoring patch at %p during frozen state\n", patch->address));
        return TPR_IGNORE;
    }

    Module *module = patch->key.module;
    BOOL managed = patch->IsManagedPatch();
    mdMethodDef md = patch->key.md;
    SIZE_T offset = patch->offset;

    _ASSERTE((this->GetThread() == thread) || !"Stepper should only get patches on its thread");

    // Note we can only run a stack trace if:
    // - the context is in managed code (eg, not a stub)
    // - OR we have a frame in place to prime the stackwalk.
    ControllerStackInfo info;
    CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);

    _ASSERTE(!ISREDIRECTEDTHREAD(thread));

    // Context should always be from patch.
    _ASSERTE(context != NULL);

    bool fSafeToDoStackTrace = true;

    // If we're in a stub (module == NULL and still in managed code), then our context is off in lala-land
    // Then, it's only safe to do a stackwalk if the top frame is protecting us. That's only true for a
    // frame_push. If we're here on a manager_push, then we don't have any such protection, so don't do the
    // stackwalk.

    fSafeToDoStackTrace = patch->IsSafeForStackTrace();


    if (fSafeToDoStackTrace)
    {
        StackTraceTicket ticket(patch);
        info.GetStackInfo(ticket, thread, LEAF_MOST_FRAME, context);

        LOG((LF_CORDB, LL_INFO10000, "DS::TP: this:0x%p in %s::%s (fp:0x%p, "
            "off:0x%p md:0x%p), \n\texception source:%s::%s (fp:0x%p)\n",
            this,
            info.m_activeFrame.md!=NULL?info.m_activeFrame.md->m_pszDebugClassName:"Unknown",
            info.m_activeFrame.md!=NULL?info.m_activeFrame.md->m_pszDebugMethodName:"Unknown",
            info.m_activeFrame.fp.GetSPValue(), patch->offset, patch->key.md,
            m_fdException!=NULL?m_fdException->m_pszDebugClassName:"None",
            m_fdException!=NULL?m_fdException->m_pszDebugMethodName:"None",
             m_fpException.GetSPValue()));
    }

    DisableAll();

    if (DetectHandleLCGMethods(dac_cast<PCODE>(patch->address), NULL, &info))
    {
        return TPR_IGNORE;
    }

    if (module == NULL)
    {
        // JMC steppers should not be patching here...
        _ASSERTE(DEBUGGER_CONTROLLER_JMC_STEPPER != this->GetDCType());

        if (managed)
        {

            LOG((LF_CORDB, LL_INFO10000,
                 "Frame (stub) patch hit at offset 0x%x\n", offset));

            // This is a stub patch. If it was a TRACE_FRAME_PUSH that
            // got us here, then the stub's frame is pushed now, so we
            // tell the frame to apply the real patch. If we got here
            // via a TRACE_MGR_PUSH, however, then there is no frame
            // and we tell the stub manager that generated the
            // TRACE_MGR_PUSH to apply the real patch.
            TraceDestination trace;
            bool traceOk;
            FramePointer     frameFP;
            PTR_BYTE traceManagerRetAddr = NULL;

            if (patch->trace.GetTraceType() == TRACE_MGR_PUSH)
            {
                _ASSERTE(context != NULL);
                CONTRACT_VIOLATION(GCViolation);
                traceOk = g_pEEInterface->TraceManager(
                                                 thread,
                                                 patch->trace.GetStubManager(),
                                                 &trace,
                                                 context,
                                                 &traceManagerRetAddr);

                // We don't hae an active frame here, so patch with a
                // FP of NULL so anything will match.
                //
                // <REVISIT_TODO>@todo: should we take Esp out of the context?</REVISIT_TODO>
                frameFP = LEAF_MOST_FRAME;
            }
            else
            {
                _ASSERTE(fSafeToDoStackTrace);
                CONTRACT_VIOLATION(GCViolation); // TraceFrame GC-triggers
                traceOk = g_pEEInterface->TraceFrame(thread,
                                                     thread->GetFrame(),
                                                     TRUE,
                                                     &trace,
                                                     &(info.m_activeFrame.registers));

                frameFP = info.m_activeFrame.fp;
            }

            // Enable the JMC backstop for traditional steppers to catch us in case 
            // we didn't predict the call target properly.
            EnableJMCBackStop(NULL);


            if (!traceOk
                || !g_pEEInterface->FollowTrace(&trace)
                || !PatchTrace(&trace, frameFP,
                               (m_rgfMappingStop&STOP_UNMANAGED)?
                                    (true):(false)))
            {
                //
                // We can't set a patch in the frame -- we need
                // to trap returning from this frame instead.
                //
                // Note: if we're in the TRACE_MGR_PUSH case from
                // above, then we must place a patch where the
                // TraceManager function told us to, since we can't
                // actually unwind from here.
                //
                if (patch->trace.GetTraceType() != TRACE_MGR_PUSH)
                {
                    _ASSERTE(fSafeToDoStackTrace);
                    LOG((LF_CORDB,LL_INFO10000,"TSO for non TRACE_MGR_PUSH case\n"));
                    TrapStepOut(&info);
                }
                else
                {
                    LOG((LF_CORDB, LL_INFO10000,
                         "TSO for TRACE_MGR_PUSH case."));

                    // We'd better have a valid return address.
                    _ASSERTE(traceManagerRetAddr != NULL);
                    
                    if (g_pEEInterface->IsManagedNativeCode(traceManagerRetAddr))
                    {
                        // Grab the jit info for the method.
                        DebuggerJitInfo *dji;
                        dji = g_pDebugger->GetJitInfoFromAddr((TADDR) traceManagerRetAddr);
                        
                        MethodDesc * mdNative = (dji == NULL) ? 
                            g_pEEInterface->GetNativeCodeMethodDesc(dac_cast<PCODE>(traceManagerRetAddr)) : dji->m_nativeCodeVersion.GetMethodDesc();
                        _ASSERTE(mdNative != NULL);

                        // Find the method that the return is to.
                        _ASSERTE(g_pEEInterface->GetFunctionAddress(mdNative) != NULL);
                        SIZE_T offsetRet = dac_cast<TADDR>(traceManagerRetAddr -
                            g_pEEInterface->GetFunctionAddress(mdNative));

                        // Place the patch.
                        AddBindAndActivateNativeManagedPatch(mdNative,
                                 dji,
                                 offsetRet,
                                 LEAF_MOST_FRAME,
                                 NULL);

                        LOG((LF_CORDB, LL_INFO10000,
                             "DS::TP: normally managed code AddPatch"
                             " in %s::%s, offset 0x%x\n",
                             mdNative->m_pszDebugClassName,
                             mdNative->m_pszDebugMethodName,
                             offsetRet));
                    }
                    else
                    {
                        // We're hitting this code path with MC++ assemblies
                        // that have an unmanaged entry point so the stub returns to CallDescrWorker.
                        _ASSERTE(g_pEEInterface->GetNativeCodeMethodDesc(dac_cast<PCODE>(patch->address))->IsILStub());
                    }

                }

                m_reason = STEP_NORMAL; //we tried to do a STEP_CALL, but since it didn't
                //work, we're doing what amounts to a normal step.
                LOG((LF_CORDB,LL_INFO10000,"DS 0x%x m_reason = STEP_NORMAL"
                     "(attempted call thru stub manager, SM didn't know where"
                     " we're going, so did a step out to original call\n",this));
            }
            else
            {
                m_reason = STEP_CALL;
            }

            EnableTraceCall(LEAF_MOST_FRAME);
            EnableUnwind(m_fp);

            return TPR_IGNORE;
        }
        else
        {
            // @todo - when would we hit this codepath?
            // If we're not in managed, then we should have pushed a frame onto the Thread's frame chain,
            // and thus we should still safely be able to do a stackwalk here.
            _ASSERTE(fSafeToDoStackTrace);
            if (DetectHandleInterceptors(&info) )
            {
                return TPR_IGNORE; //don't actually want to stop
            }

            LOG((LF_CORDB, LL_INFO10000,
                 "Unmanaged step patch hit at 0x%x\n", offset));

            StackTraceTicket ticket(patch);
            PrepareForSendEvent(ticket);
            return TPR_TRIGGER;
        }
    } // end (module == NULL)

    // If we're inside an interceptor but don't want to be,then we'll set a
    // patch outside the current function.
    _ASSERTE(fSafeToDoStackTrace);
    if (DetectHandleInterceptors(&info) )
    {
        return TPR_IGNORE; //don't actually want to stop
    }

    LOG((LF_CORDB,LL_INFO10000, "DS: m_fp:0x%p, activeFP:0x%p fpExc:0x%p\n",
        m_fp.GetSPValue(), info.m_activeFrame.fp.GetSPValue(), m_fpException.GetSPValue()));

    if (IsInRange(offset, m_range, m_rangeCount, &info) ||
        ShouldContinueStep( &info, offset))
    {
        LOG((LF_CORDB, LL_INFO10000,
             "Intermediate step patch hit at 0x%x\n", offset));

        if (!TrapStep(&info, m_stepIn))
            TrapStepNext(&info);

        EnableUnwind(m_fp);
        return TPR_IGNORE;
    }
    else
    {
        LOG((LF_CORDB, LL_INFO10000, "Step patch hit at 0x%x\n", offset));

        // For a JMC stepper, we have an additional constraint:
        // skip non-user code. So if we're still in non-user code, then
        // we've got to keep going
        DebuggerMethodInfo * dmi = g_pDebugger->GetOrCreateMethodInfo(module, md);

        if ((dmi != NULL) && DetectHandleNonUserCode(&info, dmi))
        {
            return TPR_IGNORE;
        }

        StackTraceTicket ticket(patch);
        PrepareForSendEvent(ticket);
        return TPR_TRIGGER;
    }
}

// Return true if this should be skipped.
// For a non-jmc stepper, we don't care about non-user code, so we
// don't skip it and so we always return false.
bool DebuggerStepper::DetectHandleNonUserCode(ControllerStackInfo *info, DebuggerMethodInfo * pInfo)
{
    LIMITED_METHOD_CONTRACT;

    return false;
}

// For regular steppers, trace-call is just a trace-call.
void DebuggerStepper::EnablePolyTraceCall()
{
    this->EnableTraceCall(LEAF_MOST_FRAME);
}

// Traditional steppers enable MethodEnter as a back-stop for step-in.
// We hope that the stub-managers will predict the step-in for us,
// but in case they don't the Method-Enter should catch us.
// MethodEnter is not fully correct for traditional steppers for a few reasons:
// - doesn't handle step-in to native
// - stops us *after* the prolog (a traditional stepper can stop us before the prolog).
// - only works for methods that have the JMC probe. That can exclude all optimized code.
void DebuggerStepper::TriggerMethodEnter(Thread * thread,
                                            DebuggerJitInfo *dji,
                                            const BYTE * ip,
                                            FramePointer fp)
{
    _ASSERTE(dji != NULL);
    _ASSERTE(thread != NULL);
    _ASSERTE(ip != NULL);



    _ASSERTE(this->GetDCType() == DEBUGGER_CONTROLLER_STEPPER);

    _ASSERTE(!IsFrozen());

    MethodDesc * pDesc = dji->m_nativeCodeVersion.GetMethodDesc();
    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper::TME, desc=%p, addr=%p\n",
        pDesc, ip));

    // JMC steppers won't stop in Lightweight delegates. Just return & keep executing.
    if (pDesc->IsNoMetadata())
    {
        LOG((LF_CORDB, LL_INFO100000, "DJMCStepper::TME, skipping b/c it's lw-codegen\n"));
        return;
    }

    // This is really just a heuristic.  We don't want to trigger a JMC probe when we are
    // executing in an IL stub, or in one of the marshaling methods called by the IL stub.
    // The problem is that the IL stub can call into arbitrary code, including custom marshalers.
    // In that case the user has to put a breakpoint to stop in the code.
    if (g_pEEInterface->DetectHandleILStubs(thread))
    {
        return;
    }

#ifdef _DEBUG
    // To help trace down if a problem is related to a stubmanager,
    // we add a knob that lets us skip the MethodEnter checks. This lets tests directly
    // go against the Stub-managers w/o the MethodEnter check backstops.
    int fSkip = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_DbgSkipMEOnStep);
    if (fSkip)
    {
        return;
    }

    // See EnableJMCBackStop() for details here. This check just makes sure that we don't fire
    // the assert if we end up in the method we started in (which could happen if we trace call
    // instructions before the JMC probe).
    // m_StepInStartMethod may be null (if this step-in didn't start from managed code).
    if ((m_StepInStartMethod != pDesc) && 
        (!m_StepInStartMethod->IsLCGMethod()))
    {
        // Since normal step-in should stop us at the prolog, and TME is after the prolog,
        // if a stub-manager did successfully find the address, we should get a TriggerPatch first
        // at native offset 0 (before the prolog) and before we get the TME. That means if
        // we do get the TME, then there was no stub-manager to find us.

        SString sLog;
        StubManager::DbgGetLog(&sLog);

        // Assert b/c the Stub-manager should have caught us first.
        // We don't want people relying on TriggerMethodEnter as the real implementation for Traditional Step-in
        // (see above for reasons why). However, using TME will provide a bandage for the final retail product
        // in cases where we are missing a stub-manager.
        CONSISTENCY_CHECK_MSGF(false, (
            "\nThe Stubmanagers failed to identify and trace a stub on step-in. The stub-managers for this code-path path need to be fixed.\n"
            "See http://team/sites/clrdev/Devdocs/StubManagers.rtf for more information on StubManagers.\n"
            "Stepper this=0x%p, startMethod='%s::%s'\n"
            "---------------------------------\n"
            "Stub manager log:\n%S"
            "\n"
            "The thread is now in managed method '%s::%s'.\n"
            "---------------------------------\n",
            this, 
            ((m_StepInStartMethod == NULL) ? "unknown" : m_StepInStartMethod->m_pszDebugClassName), 
            ((m_StepInStartMethod == NULL) ? "unknown" : m_StepInStartMethod->m_pszDebugMethodName),
            sLog.GetUnicode(),
            pDesc->m_pszDebugClassName, pDesc->m_pszDebugMethodName
            ));
    }
#endif



    // Place a patch to stopus.
    // Don't bind to a particular AppDomain so that we can do a Cross-Appdomain step.
    AddBindAndActivateNativeManagedPatch(pDesc,
                  dji,
                  CodeRegionInfo::GetCodeRegionInfo(dji, pDesc).AddressToOffset(ip),
                  fp,
                  NULL // AppDomain
    );

    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper::TME, after setting patch to stop\n"));

    // Once we resume, we'll go hit that patch (duh, we patched our return address)
    // Furthermore, we know the step will complete with reason = call, so set that now.
    m_reason = STEP_CALL;
}


// We may have single-stepped over a return statement to land us up a frame.
// Or we may have single-stepped through a method.
// We never single-step into calls (we place a patch at the call destination).
bool DebuggerStepper::TriggerSingleStep(Thread *thread, const BYTE *ip)
{
    LOG((LF_CORDB,LL_INFO10000,"DS:TSS this:0x%p, @ ip:0x%p\n", this, ip));

    _ASSERTE(!IsFrozen());

    // User break should only do a step-out and never actually need a singlestep flag.
    _ASSERTE(GetDCType() != DEBUGGER_CONTROLLER_USER_BREAKPOINT);

    //
    // there's one weird case here - if the last instruction generated
    // a hardware exception, we may be in lala land.  If so, rely on the unwind
    // handler to figure out what happened.
    //
    // <REVISIT_TODO>@todo this could be wrong when we have the incremental collector going</REVISIT_TODO>
    //

    if (!g_pEEInterface->IsManagedNativeCode(ip))
    {
        LOG((LF_CORDB,LL_INFO10000, "DS::TSS: not in managed code, Returning false (case 0)!\n"));
        DisableSingleStep();
        return false;
    }

    // If we EnC the method, we'll blast the function address,
    // and so have to get it from teh DJI that we'll have.  If
    // we haven't gotten debugger info about a regular function, then
    // we'll have to get the info from the EE, which will be valid
    // since we're standing in the function at this point, and
    // EnC couldn't have happened yet.
    MethodDesc *fd = g_pEEInterface->GetNativeCodeMethodDesc((PCODE)ip);

    SIZE_T offset;
    DebuggerJitInfo *dji = g_pDebugger->GetJitInfoFromAddr((TADDR) ip);
    offset = CodeRegionInfo::GetCodeRegionInfo(dji, fd).AddressToOffset(ip);

    ControllerStackInfo info;

    // Safe to stackwalk b/c we've already checked that our IP is in crawlable code.
    StackTraceTicket ticket(ip);
    info.GetStackInfo(ticket, GetThread(), LEAF_MOST_FRAME, NULL);

    // This is a special case where we return from a managed method back to an IL stub.  This can
    // only happen if there's no more managed method frames closer to the root and we want to perform
    // a step out, or if we step-next off the end of a method called by an IL stub.  In either case,
    // we'll get a single step in an IL stub, which we want to ignore.  We also want to enable trace
    // call here, just in case this IL stub is about to call the managed target (in the reverse interop case).
    if (fd->IsILStub())
    {
        LOG((LF_CORDB,LL_INFO10000, "DS::TSS: not in managed code, Returning false (case 0)!\n"));
        if (this->GetDCType() == DEBUGGER_CONTROLLER_STEPPER)
        {
            EnableTraceCall(info.m_activeFrame.fp);
        }
        else if (this->GetDCType() == DEBUGGER_CONTROLLER_JMC_STEPPER)
        {
            EnableMethodEnter();
        }
        DisableSingleStep();
        return false;
    }

    DisableAll();

    LOG((LF_CORDB,LL_INFO10000, "DS::TSS m_fp:0x%p, activeFP:0x%p fpExc:0x%p\n",
        m_fp.GetSPValue(), info.m_activeFrame.fp.GetSPValue(), m_fpException.GetSPValue()));

    if (DetectHandleLCGMethods((PCODE)ip, fd, &info))
    {
        return false;
    }

    if (IsInRange(offset, m_range, m_rangeCount, &info) ||
        ShouldContinueStep( &info, offset))
    {
        if (!TrapStep(&info, m_stepIn))
            TrapStepNext(&info);

        EnableUnwind(m_fp);

        LOG((LF_CORDB,LL_INFO10000, "DS::TSS: Returning false Case 1!\n"));
        return false;
    }
    else
    {
        LOG((LF_CORDB,LL_INFO10000, "DS::TSS: Returning true Case 2 for reason STEP_%02x!\n", m_reason));

        // @todo - when would a single-step (not a patch) land us in user-code?
        // For a JMC stepper, we have an additional constraint:
        // skip non-user code. So if we're still in non-user code, then
        // we've got to keep going
        DebuggerMethodInfo * dmi = g_pDebugger->GetOrCreateMethodInfo(fd->GetModule(), fd->GetMemberDef());

        if ((dmi != NULL) && DetectHandleNonUserCode(&info, dmi))
            return false;

        PrepareForSendEvent(ticket);
        return true;
    }
}

void DebuggerStepper::TriggerTraceCall(Thread *thread, const BYTE *ip)
{
    LOG((LF_CORDB,LL_INFO10000,"DS:TTC this:0x%x, @ ip:0x%x\n",this,ip));
    TraceDestination trace;

    if (IsFrozen())
    {
        LOG((LF_CORDB,LL_INFO10000,"DS:TTC exit b/c of Frozen\n"));
        return;
    }

    // This is really just a heuristic.  We don't want to trigger a JMC probe when we are
    // executing in an IL stub, or in one of the marshaling methods called by the IL stub.
    // The problem is that the IL stub can call into arbitrary code, including custom marshalers.
    // In that case the user has to put a breakpoint to stop in the code.
    if (g_pEEInterface->DetectHandleILStubs(thread))
    {
        return;
    }

    if (g_pEEInterface->TraceStub(ip, &trace)
        && g_pEEInterface->FollowTrace(&trace)
        && PatchTrace(&trace, LEAF_MOST_FRAME,
            (m_rgfMappingStop&STOP_UNMANAGED)?(true):(false)))
    {
        // !!! We really want to know ahead of time if PatchTrace will succeed.
        DisableAll();
        PatchTrace(&trace, LEAF_MOST_FRAME, (m_rgfMappingStop&STOP_UNMANAGED)?
            (true):(false));

        // If we're triggering a trace call, and we're following a trace into either managed code or unjitted managed
        // code, then we need to update our stepper's reason to STEP_CALL to reflect the fact that we're going to land
        // into a new function because of a call.
        if ((trace.GetTraceType() == TRACE_UNJITTED_METHOD) || (trace.GetTraceType() == TRACE_MANAGED))
        {
            m_reason = STEP_CALL;
        }

        EnableUnwind(m_fp);

        LOG((LF_CORDB, LL_INFO10000, "DS::TTC potentially a step call!\n"));
    }
}

void DebuggerStepper::TriggerUnwind(Thread *thread, 
                                    MethodDesc *fd, DebuggerJitInfo * pDJI, SIZE_T offset,
                                    FramePointer fp,
                                    CorDebugStepReason unwindReason)
{
    CONTRACTL
    {
        THROWS; // from GetJitInfo
        GC_NOTRIGGER; // don't send IPC events
        MODE_COOPERATIVE; // TriggerUnwind always is coop

        PRECONDITION(!IsDbgHelperSpecialThread());
        PRECONDITION(fd->IsDynamicMethod() ||  (pDJI != NULL));
    }
    CONTRACTL_END;

    LOG((LF_CORDB,LL_INFO10000,"DS::TU this:0x%p, in %s::%s, offset 0x%p "
        "frame:0x%p unwindReason:0x%x\n", this, fd->m_pszDebugClassName,
        fd->m_pszDebugMethodName, offset, fp.GetSPValue(), unwindReason));

    _ASSERTE(unwindReason == STEP_EXCEPTION_FILTER || unwindReason == STEP_EXCEPTION_HANDLER);

    if (IsFrozen())
    {
        LOG((LF_CORDB,LL_INFO10000,"DS:TTC exit b/c of Frozen\n"));
        return;
    }

    if (IsCloserToRoot(fp, GetUnwind()))
    {
        // Handler is in a parent frame . For all steps (in,out,over)
        // we want to stop in the handler.
        // This will be like a Step Out, so we don't need any range.
        ResetRange();
    }
    else
    {
        // Handler/Filter is in the same frame as the stepper
        // For a step-in/over, we want to patch the handler/filter.
        // But for a step-out, we want to just continue executing (and don't change
        // the step-reason either).
        if (m_eMode == cStepOut)
        {
            LOG((LF_CORDB, LL_INFO10000, "DS::TU Step-out, returning for same-frame case.\n"));
            return;
        }

    }

    // Remember the origin of the exception, so that if the step looks like
    // it's going to complete in a different frame, but the code comes from the
    // same frame as the one we're in, we won't stop twice in the "same" range
    m_fpException = fp;
    m_fdException = fd;

    //
    // An exception is exiting the step region.  Set a patch on
    // the filter/handler.
    //

    DisableAll();

    BOOL fOk;
    fOk = AddBindAndActivateNativeManagedPatch(fd, pDJI, offset, LEAF_MOST_FRAME, NULL);

    // Since we're unwinding to an already executed method, the method should already
    // be jitted and placing the patch should work.
    CONSISTENCY_CHECK_MSGF(fOk, ("Failed to place patch at TriggerUnwind.\npThis=0x%p md=0x%p, native offset=0x%x\n", this, fd, offset));

    LOG((LF_CORDB,LL_INFO100000,"Step reason:%s\n", unwindReason==STEP_EXCEPTION_FILTER
        ? "STEP_EXCEPTION_FILTER":"STEP_EXCEPTION_HANDLER"));
    m_reason = unwindReason;
}


// Prepare for sending an event.
// This is called 1:1 w/ SendEvent, but this guy can be called in a GC_TRIGGERABLE context
// whereas SendEvent is pretty strict.
// Caller ensures that it's safe to run a stack trace.
void DebuggerStepper::PrepareForSendEvent(StackTraceTicket ticket)
{
#ifdef _DEBUG
    _ASSERTE(!m_fReadyToSend);
    m_fReadyToSend = true;
#endif

    LOG((LF_CORDB, LL_INFO10000, "DS::SE m_fpStepInto:0x%x\n", m_fpStepInto.GetSPValue()));

    if (m_fpStepInto != LEAF_MOST_FRAME)
    {
        ControllerStackInfo csi;
        csi.GetStackInfo(ticket, GetThread(), LEAF_MOST_FRAME, NULL);

        if (csi.m_targetFrameFound &&
#if !defined(WIN64EXCEPTIONS)
            IsCloserToRoot(m_fpStepInto, csi.m_activeFrame.fp)
#else
            IsCloserToRoot(m_fpStepInto, (csi.m_activeFrame.IsNonFilterFuncletFrame() ? csi.m_returnFrame.fp : csi.m_activeFrame.fp))
#endif // WIN64EXCEPTIONS
           )

        {
            m_reason = STEP_CALL;
            LOG((LF_CORDB, LL_INFO10000, "DS::SE this:0x%x STEP_CALL!\n", this));
        }
#ifdef _DEBUG
        else
        {
            LOG((LF_CORDB, LL_INFO10000, "DS::SE this:0x%x not a step call!\n", this));
        }
#endif
    }

#ifdef _DEBUG
        // Steppers should only stop in interesting code.
        if (this->GetDCType() == DEBUGGER_CONTROLLER_JMC_STEPPER)
        {
            // If we're at either a patch or SS, we'll have a context.
            CONTEXT *context = g_pEEInterface->GetThreadFilterContext(GetThread());
            if (context == NULL)
            {
                void * pIP = CORDbgGetIP(reinterpret_cast<DT_CONTEXT *>(context));

                DebuggerJitInfo * dji = g_pDebugger->GetJitInfoFromAddr((TADDR) pIP);
                DebuggerMethodInfo * dmi = NULL;
                if (dji != NULL)
                {
                    dmi = dji->m_methodInfo;

                    CONSISTENCY_CHECK_MSGF(dmi->IsJMCFunction(), ("JMC stepper %p stopping in non-jmc method, MD=%p, '%s::%s'",
                        this, dji->m_nativeCodeVersion.GetMethodDesc(), dji->m_nativeCodeVersion.GetMethodDesc()->m_pszDebugClassName, dji->m_nativeCodeVersion.GetMethodDesc()->m_pszDebugMethodName));

                }


            }
        }

#endif
}

bool DebuggerStepper::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        NOTHROW;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;

    // We practically should never have a step interupted by SetIp.
    // We'll still go ahead and send the Step-complete event because we've already
    // deactivated our triggers by now and we haven't placed any new patches to catch us.
    // We assert here because we don't believe we'll ever be able to hit this scenario.
    // This is technically an issue, but we consider it benign enough to leave in.
    _ASSERTE(!fIpChanged || !"Stepper interupted by SetIp");

    LOG((LF_CORDB, LL_INFO10000, "DS::SE m_fpStepInto:0x%x\n", m_fpStepInto.GetSPValue()));

    _ASSERTE(m_fReadyToSend);
    _ASSERTE(GetThread() == thread);

    CONTEXT *context = g_pEEInterface->GetThreadFilterContext(thread);
    _ASSERTE(!ISREDIRECTEDTHREAD(thread));

    // We need to send the stepper and delete the controller because our stepper
    // no longer has any patches or other triggers that will let it send the step-complete event.
    g_pDebugger->SendStep(thread, context, this, m_reason);

    this->Delete();

#ifdef _DEBUG
    // Now that we've sent the event, we can stop recording information.
    StubManager::DbgFinishLog();
#endif

    return true;
}

void DebuggerStepper::ResetRange()
{
    if (m_range)
    {
        TRACE_FREE(m_range);
        DeleteInteropSafe(m_range);

        m_range = NULL;
    }
}

//-----------------------------------------------------------------------------
// Return true if this stepper is alive, but frozen. (we freeze when the stepper
// enters a nested func-eval).
//-----------------------------------------------------------------------------
bool DebuggerStepper::IsFrozen()
{
    return (m_cFuncEvalNesting > 0);
}

//-----------------------------------------------------------------------------
// Returns true if this stepper is 'dead' - which happens if a non-frozen stepper
// gets a func-eval exit.
//-----------------------------------------------------------------------------
bool DebuggerStepper::IsDead()
{
    return (m_cFuncEvalNesting < 0);
}

// * ------------------------------------------------------------------------
// * DebuggerJMCStepper routines
// * ------------------------------------------------------------------------
DebuggerJMCStepper::DebuggerJMCStepper(Thread *thread,
                    CorDebugUnmappedStop rgfMappingStop,
                    CorDebugIntercept interceptStop,
                    AppDomain *appDomain) :
    DebuggerStepper(thread, rgfMappingStop, interceptStop, appDomain)
{
    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper ctor, this=%p\n", this));
}

DebuggerJMCStepper::~DebuggerJMCStepper()
{
    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper dtor, this=%p\n", this));
}

// If we're a JMC stepper, then don't stop in non-user code.
bool DebuggerJMCStepper::IsInterestingFrame(FrameInfo * pFrame)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    DebuggerMethodInfo *pInfo = pFrame->GetMethodInfoFromFrameOrThrow();
    _ASSERTE(pInfo != NULL); // throws on failure

    bool fIsUserCode = pInfo->IsJMCFunction();


    LOG((LF_CORDB, LL_INFO1000000, "DS::TSO, frame '%s::%s' is '%s' code\n",
            pFrame->DbgGetClassName(), pFrame->DbgGetMethodName(),
            fIsUserCode ? "user" : "non-user"));

    return fIsUserCode;
}

// A JMC stepper's step-next stops at the next thing of code run.
// This may be a Step-Out, or any User code called before that.
// A1 -> B1 -> { A2, B2 -> B3 -> A3}
// So TrapStepNex at end of A2 should land us in A3.
void DebuggerJMCStepper::TrapStepNext(ControllerStackInfo *info)
{
    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper::TrapStepNext, this=%p\n", this));
    EnableMethodEnter();

    // This will place a patch up the stack and set m_reason = STEP_RETURN.
    // If we end up hitting JMC before that patch, we'll hit TriggerMethodEnter
    // and that will set our reason to STEP_CALL.
    TrapStepOut(info);
}

// ip - target address for call instruction
bool DebuggerJMCStepper::TrapStepInHelper(
    ControllerStackInfo * pInfo,
    const BYTE * ipCallTarget,
    const BYTE * ipNext,
    bool fCallingIntoFunclet)
{
#ifndef WIN64EXCEPTIONS
    // There are no funclets on x86.
    _ASSERTE(!fCallingIntoFunclet);
#endif

    // If we are calling into a funclet, then we can't rely on the JMC probe to stop us because there are no 
    // JMC probes in funclets.  Instead, we have to perform a traditional step-in here.
    if (fCallingIntoFunclet)
    {
        TraceDestination td;
        td.InitForManaged(reinterpret_cast<PCODE>(ipCallTarget));
        PatchTrace(&td, LEAF_MOST_FRAME, false);

        // If this succeeds, then we still need to put a patch at the return address.  This is done below.
        // If this fails, then we definitely need to put a patch at the return address to trap the thread.
        // So in either case, we have to execute the rest of this function.
    }

    MethodDesc * pDesc = pInfo->m_activeFrame.md;
    DebuggerJitInfo *dji = NULL;

    // We may not have a DJI if we're in an attach case. We should still be able to do a JMC-step in though.
    // So NULL is ok here.
    dji = g_pDebugger->GetJitInfo(pDesc, (const BYTE*) ipNext);


    // Place patch after call, which is at ipNext. Note we don't need an IL->Native map here
    // since we disassembled native code to find the ip after the call.
    SIZE_T offset = CodeRegionInfo::GetCodeRegionInfo(dji, pDesc).AddressToOffset(ipNext);


    LOG((LF_CORDB, LL_INFO100000, "DJMCStepper::TSIH, at '%s::%s', calling=0x%p, next=0x%p, offset=%d\n",
        pDesc->m_pszDebugClassName,
        pDesc->m_pszDebugMethodName,
        ipCallTarget, ipNext,
        offset));

    // Place a patch at the native address (inside the managed method).
    AddBindAndActivateNativeManagedPatch(pInfo->m_activeFrame.md,
             dji,
             offset,
             pInfo->m_returnFrame.fp,
             NULL);

    EnableMethodEnter();

    // Return true means that we want to let the stepper run free. It will either
    // hit the patch after the call instruction or it will hit a TriggerMethodEnter.
    return true;
}

// For JMC-steppers, we don't enable trace-call; we enable Method-Enter.
void DebuggerJMCStepper::EnablePolyTraceCall()
{
    _ASSERTE(!IsFrozen());

    this->EnableMethodEnter();
}

// Return true if this is non-user code. This means we've setup the proper patches &
// triggers, etc and so we expect the controller to just run free.
// This is called when all other stepping criteria are met and we're about to
// send a step-complete. For JMC, this is when we see if we're in non-user code
// and if so, continue stepping instead of send the step complete.
// Return false if this is user-code.
bool DebuggerJMCStepper::DetectHandleNonUserCode(ControllerStackInfo *pInfo, DebuggerMethodInfo * dmi)
{
    _ASSERTE(dmi != NULL);
    bool fIsUserCode = dmi->IsJMCFunction();

    if (!fIsUserCode)
    {
        LOG((LF_CORDB, LL_INFO10000, "JMC stepper stopped in non-user code, continuing.\n"));
        // Not-user code, we want to skip through this.

        // We may be here while trying to step-out.
        // Step-out just means stop at the first interesting frame above us.
        // So JMC TrapStepOut won't patch a non-user frame.
        // But if we're skipping over other stuff (prolog, epilog, interceptors,
        // trace calls), then we may still be in the middle of non-user
        //_ASSERTE(m_eMode != cStepOut);

        if (m_eMode == cStepOut)
        {
            TrapStepOut(pInfo);
        }
        else if (m_stepIn)
        {
            EnableMethodEnter();
            TrapStepOut(pInfo);
            // Run until we hit the next thing of managed code.
        } else {
            // Do a traditional step-out since we just want to go up 1 frame.
            TrapStepOut(pInfo, true); // force trad step out.


            // If we're not in the original frame anymore, then
            // If we did a Step-over at the end of a method, and that did a single-step over the return
            // then we may already be in our parent frame. In that case, we also want to behave
            // like a step-in and TriggerMethodEnter.
            if (this->m_fp != pInfo->m_activeFrame.fp)
            {
                // If we're a step-over, then we should only be stopped in a parent frame.
                _ASSERTE(m_stepIn || IsCloserToLeaf(this->m_fp, pInfo->m_activeFrame.fp));
                EnableMethodEnter();
            }

            // Step-over shouldn't stop in a frame below us in the same callstack.
            // So we do a tradional step-out of our current frame, which guarantees
            // that. After that, we act just like a step-in.
            m_stepIn = true;
        }
        EnableUnwind(m_fp);

        // Must keep going...
        return true;
    }

    return false;
}

// Dispatched right after the prolog of a JMC function.
// We may be blocking the GC here, so let's be fast!
void DebuggerJMCStepper::TriggerMethodEnter(Thread * thread,
                                            DebuggerJitInfo *dji,
                                            const BYTE * ip,
                                            FramePointer fp)
{
    _ASSERTE(dji != NULL);
    _ASSERTE(thread != NULL);
    _ASSERTE(ip != NULL);

    _ASSERTE(!IsFrozen());

    MethodDesc * pDesc = dji->m_nativeCodeVersion.GetMethodDesc();
    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper::TME, desc=%p, addr=%p\n",
        pDesc, ip));

    // JMC steppers won't stop in Lightweight delegates. Just return & keep executing.
    if (pDesc->IsNoMetadata())
    {
        LOG((LF_CORDB, LL_INFO100000, "DJMCStepper::TME, skipping b/c it's lw-codegen\n"));
        return;
    }

    // Is this user code?
    DebuggerMethodInfo * dmi = dji->m_methodInfo;
    bool fIsUserCode = dmi->IsJMCFunction();


    LOG((LF_CORDB, LL_INFO100000, "DJMCStepper::TME, '%s::%s' is '%s' code\n",
        pDesc->m_pszDebugClassName,
        pDesc->m_pszDebugMethodName,
        fIsUserCode ? "user" : "non-user"
    ));

    // If this isn't user code, then just return and continue executing.
    if (!fIsUserCode)
        return;

    // MethodEnter is only enabled when we want to stop in a JMC function.
    // And that's where we are now. So patch the ip and resume.
    // The stepper will hit the patch, and stop.

    // It's a good thing we have the fp passed in, because we have no other
    // way of getting it. We can't do a stack trace here (the stack trace
    // would start at the last pushed Frame, which miss a lot of managed
    // frames).

    // Don't bind to a particular AppDomain so that we can do a Cross-Appdomain step.
    AddBindAndActivateNativeManagedPatch(pDesc,
                  dji,
                  CodeRegionInfo::GetCodeRegionInfo(dji, pDesc).AddressToOffset(ip),
                  fp,
                  NULL // AppDomain
    );

    LOG((LF_CORDB, LL_INFO10000, "DJMCStepper::TME, after setting patch to stop\n"));

    // Once we resume, we'll go hit that patch (duh, we patched our return address)
    // Furthermore, we know the step will complete with reason = call, so set that now.
    m_reason = STEP_CALL;
}



//-----------------------------------------------------------------------------
// Helper to convert form an EE Frame's interception enum to a CorDebugIntercept
// bitfield.
// The intercept value in EE Frame's is a 0-based enumeration (not a bitfield).
// The intercept value for ICorDebug is a bitfied.
//-----------------------------------------------------------------------------
CorDebugIntercept ConvertFrameBitsToDbg(Frame::Interception i)
{
    _ASSERTE(i >= 0 && i < Frame::INTERCEPTION_COUNT);

    // Since the ee frame is a 0-based enum, we can just use a map.
    const CorDebugIntercept map[Frame::INTERCEPTION_COUNT] =
    {
        // ICorDebug                     EE Frame
        INTERCEPT_NONE,               // INTERCEPTION_NONE,
        INTERCEPT_CLASS_INIT,         // INTERCEPTION_CLASS_INIT
        INTERCEPT_EXCEPTION_FILTER,   // INTERCEPTION_EXCEPTION
        INTERCEPT_CONTEXT_POLICY,     // INTERCEPTION_CONTEXT
        INTERCEPT_SECURITY,           // INTERCEPTION_SECURITY
        INTERCEPT_INTERCEPTION,       // INTERCEPTION_OTHER
    };

    return map[i];
}

//-----------------------------------------------------------------------------
// This is a helper class to do a stack walk over a certain range and find all the interceptors.
// This allows a JMC stepper to see if there are any interceptors it wants to skip over (though
// there's nothing JMC-specific about this).
// Note that we only want to walk the stack range that the stepper is operating in.
// That's because we don't care about interceptors that happened _before_ the
// stepper was created.
//-----------------------------------------------------------------------------
class InterceptorStackInfo
{
public:
#ifdef _DEBUG
    InterceptorStackInfo()
    {
        // since this ctor just nulls out fpTop (which is already done in Init), we
        // only need it in debug.
        m_fpTop = LEAF_MOST_FRAME;
    }
#endif

    // Get a CorDebugIntercept bitfield that contains a bit for each type of interceptor
    // if that interceptor is present within our stack-range.
    // Stack range is from leaf-most up to and including fp
    CorDebugIntercept GetInterceptorsInRange()
    {
        _ASSERTE(m_fpTop != LEAF_MOST_FRAME || !"Must call Init first");
        return (CorDebugIntercept) m_bits;
    }

    // Prime the stackwalk.
    void Init(FramePointer fpTop, Thread *thread, CONTEXT *pContext, BOOL contextValid)
    {
        _ASSERTE(fpTop != LEAF_MOST_FRAME);
        _ASSERTE(thread != NULL);

        m_bits = 0;
        m_fpTop = fpTop;

        LOG((LF_CORDB,LL_EVERYTHING, "ISI::Init - fpTop=%p, thread=%p, pContext=%p, contextValid=%d\n",
            fpTop.GetSPValue(), thread, pContext, contextValid));

        int result;
        result = DebuggerWalkStack(
           thread,
           LEAF_MOST_FRAME,
           pContext,
           contextValid,
           WalkStack,
           (void *) this,
           FALSE
        );
    }


protected:
    // This is a bitfield of all the interceptors we encounter in our stack-range
    int m_bits;

    // This is the top of our stack range.
    FramePointer m_fpTop;

    static StackWalkAction WalkStack(FrameInfo *pInfo, void *data)
    {
        _ASSERTE(pInfo != NULL);
        _ASSERTE(data != NULL);
        InterceptorStackInfo * pThis = (InterceptorStackInfo*) data;

        // If there's an interceptor frame here, then set those
        // bits in our bitfield.
        Frame::Interception i = Frame::INTERCEPTION_NONE;
        Frame * pFrame = pInfo->frame;
        if ((pFrame != NULL) && (pFrame != FRAME_TOP))
        {
            i = pFrame->GetInterception();
            if (i != Frame::INTERCEPTION_NONE)
            {
                pThis->m_bits |= (int) ConvertFrameBitsToDbg(i);
            }
        }
        else if (pInfo->HasMethodFrame())
        {
            // Check whether we are executing in a class constructor.
            _ASSERTE(pInfo->md != NULL);

            // Need to be careful about an off-by-one error here! Imagine your stack looks like:
            // Foo.DoSomething()
            // Foo..cctor <--- step starts/ends in here
            // Bar.Bar();
            //
            // and your code looks like this:
            // Foo..cctor()
            // {
            //     Foo.DoSomething();  <-- JMC step started here
            //     int x = 1;          <-- step ends here
            //  }
            // This stackwalk covers the inclusive range [Foo..cctor, Foo.DoSomething()] so we will see
            // the static cctor in this walk. However executing inside a static class constructor does not
            // count as an interceptor. You must start the step outside the static constructor and then call
            // into it to have an interceptor. Therefore only static constructors that aren't the outermost
            // frame should be treated as interceptors.
            if (pInfo->md->IsClassConstructor() && (pInfo->fp != pThis->m_fpTop))
            {
                // We called a class constructor, add the appropriate flag
                pThis->m_bits |= (int) INTERCEPT_CLASS_INIT;
            }
        }
        LOG((LF_CORDB,LL_EVERYTHING,"ISI::WS- Frame=%p, fp=%p, Frame bits=%x, Cor bits=0x%x\n", pInfo->frame, pInfo->fp.GetSPValue(), i, pThis->m_bits));


        // We can stop once we hit the top frame.
        if (pInfo->fp == pThis->m_fpTop)
        {
            return SWA_ABORT;
        }
        else
        {
            return SWA_CONTINUE;
        }
    }
};




// Skip interceptors for JMC steppers.
// Return true if we patch something (and thus should keep stepping)
// Return false if we're done.
bool DebuggerJMCStepper::DetectHandleInterceptors(ControllerStackInfo * info)
{
    LOG((LF_CORDB,LL_INFO10000,"DJMCStepper::DHI: Start DetectHandleInterceptors\n"));

    // For JMC, we could stop very far way from an interceptor.
    // So we have to do a stack walk to search for interceptors...
    // If we find any in our stack range (from m_fp ... current fp), then we just do a trap-step-next.

    // Note that this logic should also work for regular steppers, but we've left that in
    // as to keep that code-path unchanged.

    // ControllerStackInfo only gives us the bottom 2 frames on the stack, so we ignore it and
    // have to do our own stack walk.

    // @todo - for us to properly skip filters, we need to make sure that filters show up in our chains.


    InterceptorStackInfo info2;
    CONTEXT *context = g_pEEInterface->GetThreadFilterContext(this->GetThread());
    CONTEXT tempContext;

    _ASSERTE(!ISREDIRECTEDTHREAD(this->GetThread()));

    if (context == NULL)
    {
        info2.Init(this->m_fp, this->GetThread(), &tempContext, FALSE);
    }
    else
    {
        info2.Init(this->m_fp, this->GetThread(), context, TRUE);
    }

    // The following casts are safe on WIN64 platforms.
    int iOnStack = (int) info2.GetInterceptorsInRange();
    int iSkip = ~((int) m_rgfInterceptStop);

    LOG((LF_CORDB,LL_INFO10000,"DJMCStepper::DHI: iOnStack=%x, iSkip=%x\n", iOnStack, iSkip));

    // If the bits on the stack contain any interceptors we want to skip, then we need to keep going.
    if ((iOnStack & iSkip) != 0)
    {
        LOG((LF_CORDB,LL_INFO10000,"DJMCStepper::DHI: keep going!\n"));
        TrapStepNext(info);
        EnableUnwind(m_fp);
        return true;
    }

    LOG((LF_CORDB,LL_INFO10000,"DJMCStepper::DHI: Done!!\n"));
    return false;
}


// * ------------------------------------------------------------------------
// * DebuggerThreadStarter routines
// * ------------------------------------------------------------------------

DebuggerThreadStarter::DebuggerThreadStarter(Thread *thread)
  : DebuggerController(thread, NULL)
{
    LOG((LF_CORDB, LL_INFO1000, "DTS::DTS: this:0x%x Thread:0x%x\n",
        this, thread));

    // Check to make sure we only have 1 ThreadStarter on a given thread. (Inspired by NDPWhidbey issue 16888)
#if defined(_DEBUG)
    EnsureUniqueThreadStarter(this);
#endif
}

// TP_RESULT DebuggerThreadStarter::TriggerPatch()   If we're in a
// stub (module==NULL&&managed) then do a PatchTrace up the stack &
// return false.  Otherwise DisableAll & return
// true
TP_RESULT DebuggerThreadStarter::TriggerPatch(DebuggerControllerPatch *patch,
                                         Thread *thread,
                                         TRIGGER_WHY tyWhy)
{
    Module *module = patch->key.module;
    BOOL managed = patch->IsManagedPatch();

    LOG((LF_CORDB,LL_INFO1000, "DebuggerThreadStarter::TriggerPatch for thread 0x%x\n", Debugger::GetThreadIdHelper(thread)));

    if (module == NULL && managed)
    {
        // This is a stub patch. If it was a TRACE_FRAME_PUSH that got us here, then the stub's frame is pushed now, so
        // we tell the frame to apply the real patch. If we got here via a TRACE_MGR_PUSH, however, then there is no
        // frame and we go back to the stub manager that generated the stub for where to patch next.
        TraceDestination trace;
        bool traceOk;
        if (patch->trace.GetTraceType() == TRACE_MGR_PUSH)
        {
            BYTE *dummy = NULL;
            CONTEXT *context = GetManagedLiveCtx(thread);
            CONTRACT_VIOLATION(GCViolation);
            traceOk = g_pEEInterface->TraceManager(thread, patch->trace.GetStubManager(), &trace, context, &dummy);
        }
        else if ((patch->trace.GetTraceType() == TRACE_FRAME_PUSH) && (thread->GetFrame()->IsTransitionToNativeFrame()))
        {
            // If we've got a frame that is transitioning to native, there's no reason to try to keep tracing. So we
            // bail early and save ourselves some effort. This also works around a problem where we deadlock trying to
            // do too much work to determine the destination of a ComPlusMethodFrame. (See issue 87103.)
            //
            // Note: trace call is still enabled, so we can just ignore this patch and wait for trace call to fire
            // again...
            return TPR_IGNORE;
        }
        else
        {
            // It's questionable whether Trace_Frame_Push is actually safe or not.
            ControllerStackInfo csi;
            StackTraceTicket ticket(patch);
            csi.GetStackInfo(ticket, thread, LEAF_MOST_FRAME, NULL);

            CONTRACT_VIOLATION(GCViolation); // TraceFrame GC-triggers
            traceOk = g_pEEInterface->TraceFrame(thread, thread->GetFrame(), TRUE, &trace, &(csi.m_activeFrame.registers));
        }

        if (traceOk && g_pEEInterface->FollowTrace(&trace))
        {
            PatchTrace(&trace, LEAF_MOST_FRAME, TRUE);
        }

        return TPR_IGNORE;
    }
    else
    {
        // We've hit user code; trigger our event.
        DisableAll();


        {

            // Give the helper thread a chance to get ready. The temporary helper can't handle
            // execution control well, and the RS won't do any execution control until it gets a
            // create Thread event, which it won't get until here.
            // So now's our best time to wait for the real helper thread.
            g_pDebugger->PollWaitingForHelper();
        }

        return TPR_TRIGGER;
    }
}

void DebuggerThreadStarter::TriggerTraceCall(Thread *thread, const BYTE *ip)
{
    LOG((LF_CORDB, LL_EVERYTHING, "DTS::TTC called\n"));
#ifdef DEBUGGING_SUPPORTED
    if (thread->GetDomain()->IsDebuggerAttached())
    {
        TraceDestination trace;

        if (g_pEEInterface->TraceStub(ip, &trace) && g_pEEInterface->FollowTrace(&trace))
        {
            PatchTrace(&trace, LEAF_MOST_FRAME, true);
        }
    }
#endif //DEBUGGING_SUPPORTED

}

bool DebuggerThreadStarter::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        NOTHROW;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;

    // This SendEvent can't be interupted by a SetIp because until the client
    // gets a ThreadStarter event, it doesn't even know the thread exists, so
    // it certainly can't change its ip.
    _ASSERTE(!fIpChanged);

    LOG((LF_CORDB, LL_INFO10000, "DTS::SE: in DebuggerThreadStarter's SendEvent\n"));

    // Send the thread started event.
    g_pDebugger->ThreadStarted(thread);

    // We delete this now because its no longer needed. We can call
    // delete here because the queued count is above 0. This object
    // will really be deleted when its dequeued shortly after this
    // call returns.
    Delete();

    return true;
}

// * ------------------------------------------------------------------------
// * DebuggerUserBreakpoint routines
// * ------------------------------------------------------------------------

bool DebuggerUserBreakpoint::IsFrameInDebuggerNamespace(FrameInfo * pFrame)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    // Steppers ignore internal frames, so should only be called on real frames.
    _ASSERTE(pFrame->HasMethodFrame());

    // Now get the namespace of the active frame
    MethodDesc *pMD = pFrame->md;

    if (pMD  != NULL)
    {
        MethodTable * pMT = pMD->GetMethodTable();

        LPCUTF8 szNamespace = NULL;
        LPCUTF8 szClassName = pMT->GetFullyQualifiedNameInfo(&szNamespace);

        if (szClassName != NULL && szNamespace != NULL)
        {
            MAKE_WIDEPTR_FROMUTF8(wszNamespace, szNamespace); // throw
            MAKE_WIDEPTR_FROMUTF8(wszClassName, szClassName);
            if (wcscmp(wszClassName, W("Debugger")) == 0 &&
                wcscmp(wszNamespace, W("System.Diagnostics")) == 0)
            {
                // This will continue stepping
                return true;
            }
        }
    }
    return false;
}

// Helper check if we're directly in a dynamic method (ignoring any chain goo
// or stuff in the Debugger namespace.
class IsLeafFrameDynamic
{
protected:
    static StackWalkAction WalkStackWrapper(FrameInfo *pInfo, void *data)
    {
        IsLeafFrameDynamic * pThis = reinterpret_cast<IsLeafFrameDynamic*> (data);
        return pThis->WalkStack(pInfo);
    }

    StackWalkAction WalkStack(FrameInfo *pInfo)
    {
        _ASSERTE(pInfo != NULL);

        // A FrameInfo may have both Method + Chain rolled into one.
        if (!pInfo->HasMethodFrame() && !pInfo->HasStubFrame())
        {
            // We're  a chain. Ignore it and keep looking.
            return SWA_CONTINUE;
        }

        // So now this is the first non-chain, non-Debugger namespace frame.
        // LW frames don't have a name, so we check if it's LW first.
        if (pInfo->eStubFrameType == STUBFRAME_LIGHTWEIGHT_FUNCTION)
        {
            m_fInLightWeightMethod = true;
            return SWA_ABORT;
        }

        // Ignore Debugger.Break() frames.
        // All Debugger.Break calls will have this on the stack.
        if (DebuggerUserBreakpoint::IsFrameInDebuggerNamespace(pInfo))
        {
            return SWA_CONTINUE;
        }

        // We've now determined leafmost thing, so stop stackwalking.
        _ASSERTE(m_fInLightWeightMethod == false);
        return SWA_ABORT;
    }


    bool m_fInLightWeightMethod;

    // Need this context to do stack trace.
    CONTEXT m_tempContext;

public:
    // On success, copies the leafmost non-chain frameinfo (including stubs) for the current thread into pInfo
    // and returns true.
    // On failure, returns false.
    // Return true on success.
    bool DoCheck(IN Thread * pThread)
    {
        CONTRACTL
        {
            GC_TRIGGERS;
            THROWS;
            MODE_ANY;

            PRECONDITION(CheckPointer(pThread));
        }
        CONTRACTL_END;

        m_fInLightWeightMethod = false;


        DebuggerWalkStack(
            pThread,
            LEAF_MOST_FRAME,
            &m_tempContext, false,
            WalkStackWrapper,
            (void *) this,
            TRUE // includes everything
            );

        // We don't care whether the stackwalk succeeds or not because the
        // callback sets our status via this field either way, so just return it.
        return m_fInLightWeightMethod;
    };
};

// Handle a Debug.Break() notification.
// This may create a controller to step-out out the Debug.Break() call (so that
// we appear stopped at the callsite).
// If we can't step-out (eg, we're directly in a dynamic method), then send
// the debug event immediately.
void DebuggerUserBreakpoint::HandleDebugBreak(Thread * pThread)
{
    bool fDoStepOut = true;

    // If the leaf frame is not a LW method, then step-out.
    IsLeafFrameDynamic info;
    fDoStepOut = !info.DoCheck(pThread);

    if (fDoStepOut)
    {
        // Create a controller that will step out for us.
        new (interopsafe) DebuggerUserBreakpoint(pThread);
    }
    else
    {
        // Send debug event immediately.
        g_pDebugger->SendUserBreakpointAndSynchronize(pThread);
    }
}


DebuggerUserBreakpoint::DebuggerUserBreakpoint(Thread *thread)
  : DebuggerStepper(thread, (CorDebugUnmappedStop) (STOP_ALL & ~STOP_UNMANAGED), INTERCEPT_ALL,  NULL)
{
    // Setup a step out from the current frame (which we know is
    // unmanaged, actually...)


    // This happens to be safe, but it's a very special case (so we have a special case ticket)
    // This is called while we're live (so no filter context) and from the fcall,
    // and we pushed a HelperMethodFrame to protect us. We also happen to know that we have
    // done anything illegal or dangerous since then.

    StackTraceTicket ticket(this);
    StepOut(LEAF_MOST_FRAME, ticket);
}


// Is this frame interesting?
// Use this to skip all code in the namespace "Debugger.Diagnostics"
bool DebuggerUserBreakpoint::IsInterestingFrame(FrameInfo * pFrame)
{
    CONTRACTL
    {
        THROWS;
        MODE_ANY;
        GC_NOTRIGGER;
    }
    CONTRACTL_END;

    return !IsFrameInDebuggerNamespace(pFrame);
}

bool DebuggerUserBreakpoint::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        NOTHROW;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;

    // See DebuggerStepper::SendEvent for why we assert here.
    // This is technically an issue, but it's too benign to fix.
    _ASSERTE(!fIpChanged);

    LOG((LF_CORDB, LL_INFO10000,
         "DUB::SE: in DebuggerUserBreakpoint's SendEvent\n"));

    // Send the user breakpoint event.
    g_pDebugger->SendRawUserBreakpoint(thread);

    // We delete this now because its no longer needed. We can call
    // delete here because the queued count is above 0. This object
    // will really be deleted when its dequeued shortly after this
    // call returns.
    Delete();

    return true;
}

// * ------------------------------------------------------------------------
// * DebuggerFuncEvalComplete routines
// * ------------------------------------------------------------------------

DebuggerFuncEvalComplete::DebuggerFuncEvalComplete(Thread *thread,
                                                   void *dest)
  : DebuggerController(thread, NULL)
{
#ifdef _TARGET_ARM_
    m_pDE = reinterpret_cast<DebuggerEvalBreakpointInfoSegment*>(((DWORD)dest) & ~THUMB_CODE)->m_associatedDebuggerEval;
#else
    m_pDE = reinterpret_cast<DebuggerEvalBreakpointInfoSegment*>(dest)->m_associatedDebuggerEval;
#endif

    // Add an unmanaged patch at the destination.
    AddAndActivateNativePatchForAddress((CORDB_ADDRESS_TYPE*)dest, LEAF_MOST_FRAME, FALSE, TRACE_UNMANAGED);
}

TP_RESULT DebuggerFuncEvalComplete::TriggerPatch(DebuggerControllerPatch *patch,
                                            Thread *thread,
                                            TRIGGER_WHY tyWhy)
{

    // It had better be an unmanaged patch...
    _ASSERTE((patch->key.module == NULL) && !patch->IsManagedPatch());

    // set ThreadFilterContext back here because we need make stack crawlable! In case,
    // GC got triggered.

    // Restore the thread's context to what it was before we hijacked it for this func eval.
    CONTEXT *pCtx = GetManagedLiveCtx(thread);
#ifdef FEATURE_DATABREAKPOINT
#ifdef FEATURE_PAL    
        #error Not supported
#endif // FEATURE_PAL
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
    // If a data breakpoint is set while we hit a breakpoint inside a FuncEval, this will make sure the data breakpoint stays
    m_pDE->m_context.Dr0 = pCtx->Dr0;
    m_pDE->m_context.Dr1 = pCtx->Dr1;
    m_pDE->m_context.Dr2 = pCtx->Dr2;
    m_pDE->m_context.Dr3 = pCtx->Dr3;
    m_pDE->m_context.Dr6 = pCtx->Dr6;
    m_pDE->m_context.Dr7 = pCtx->Dr7;
#else
    #error Not supported
#endif
#endif
    CORDbgCopyThreadContext(reinterpret_cast<DT_CONTEXT *>(pCtx), 
                            reinterpret_cast<DT_CONTEXT *>(&(m_pDE->m_context)));

    // We've hit our patch, so simply disable all (which removes the
    // patch) and trigger the event.
    DisableAll();
    return TPR_TRIGGER;
}

bool DebuggerFuncEvalComplete::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        THROWS;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;


    // This should not ever be interupted by a SetIp.
    // The BP will be off in random native code for which SetIp would be illegal.
    // However, func-eval conroller will restore the context from when we're at the patch,
    // so that will look like the IP changed on us.
    _ASSERTE(fIpChanged);

    LOG((LF_CORDB, LL_INFO10000, "DFEC::SE: in DebuggerFuncEval's SendEvent\n"));

    _ASSERTE(!ISREDIRECTEDTHREAD(thread));

    // The DebuggerEval is at our faulting address.
    DebuggerEval *pDE = m_pDE;

    // Send the func eval complete (or exception) event.
    g_pDebugger->FuncEvalComplete(thread, pDE);

    // We delete this now because its no longer needed. We can call
    // delete here because the queued count is above 0. This object
    // will really be deleted when its dequeued shortly after this
    // call returns.
    Delete();

    return true;
}

#ifdef EnC_SUPPORTED

// * ------------------------------------------------------------------------ *
// * DebuggerEnCBreakpoint routines
// * ------------------------------------------------------------------------ *

//---------------------------------------------------------------------------------------
//
// DebuggerEnCBreakpoint constructor - creates and activates a new EnC breakpoint
//
// Arguments:
//    offset        - native offset in the function to place the patch
//    jitInfo       - identifies the function in which the breakpoint is being placed
//    fTriggerType  - breakpoint type: either REMAP_PENDING or REMAP_COMPLETE
//    pAppDomain    - the breakpoint applies to the specified AppDomain only
//

DebuggerEnCBreakpoint::DebuggerEnCBreakpoint(SIZE_T offset,
                                             DebuggerJitInfo *jitInfo,
                                             DebuggerEnCBreakpoint::TriggerType fTriggerType,
                                             AppDomain *pAppDomain)
  : DebuggerController(NULL, pAppDomain),
    m_fTriggerType(fTriggerType),
    m_jitInfo(jitInfo)
{
    _ASSERTE( jitInfo != NULL );
    // Add and activate the specified patch
    AddBindAndActivateNativeManagedPatch(jitInfo->m_nativeCodeVersion.GetMethodDesc(), jitInfo, offset, LEAF_MOST_FRAME, pAppDomain);
    LOG((LF_ENC,LL_INFO1000, "DEnCBPDEnCBP::adding %S patch!\n",
        fTriggerType == REMAP_PENDING ? W("remap pending") : W("remap complete")));
}


//---------------------------------------------------------------------------------------
//
// DebuggerEnCBreakpoint::TriggerPatch 
//   called by the debugging infrastructure when the patch is hit.
//
// Arguments:
//    patch         - specifies the patch that was hit
//    thread        - identifies the thread on which the patch was hit
//    tyWhy         - TY_SHORT_CIRCUIT for normal REMAP_PENDING EnC patches
//
// Return value:
//   TPR_IGNORE if the debugger chooses not to take a remap opportunity
//   TPR_IGNORE_AND_STOP when a remap-complete event is sent
//   Doesn't return at all if the debugger remaps execution to the new version of the method
// 
TP_RESULT DebuggerEnCBreakpoint::TriggerPatch(DebuggerControllerPatch *patch,
                                         Thread *thread,
                                         TRIGGER_WHY tyWhy)
{
    _ASSERTE(HasLock());

    Module *module = patch->key.module;
    mdMethodDef md = patch->key.md;
    SIZE_T offset = patch->offset;

    // Map the current native offset back to the IL offset in the old
    // function.  This will be mapped to the new native offset within
    // ResumeInUpdatedFunction
    CorDebugMappingResult map;
    DWORD which;
    SIZE_T currentIP = (SIZE_T)m_jitInfo->MapNativeOffsetToIL(offset,
            &map, &which);

    // We only lay DebuggerEnCBreakpoints at sequence points
    _ASSERTE(map == MAPPING_EXACT);

    LOG((LF_ENC, LL_ALWAYS,
         "DEnCBP::TP: triggered E&C %S breakpoint: tid=0x%x, module=0x%08x, "
         "method def=0x%08x, version=%d, native offset=0x%x, IL offset=0x%x\n this=0x%x\n",
         m_fTriggerType == REMAP_PENDING ? W("ResumePending") : W("ResumeComplete"),
         thread, module, md, m_jitInfo->m_encVersion, offset, currentIP, this));

    // If this is a REMAP_COMPLETE patch, then dispatch the RemapComplete callback
    if (m_fTriggerType == REMAP_COMPLETE)
    {
        return HandleRemapComplete(patch, thread, tyWhy);
    }

    // This must be a REMAP_PENDING patch
    // unless we got here on an explicit short-circuit, don't do any work
    if (tyWhy != TY_SHORT_CIRCUIT)
    {
        LOG((LF_ENC, LL_ALWAYS, "DEnCBP::TP: not short-circuit ... bailing\n"));
        return TPR_IGNORE;
    }

    _ASSERTE(patch->IsManagedPatch());

    // Grab the MethodDesc for this function.
    _ASSERTE(module != NULL);

    // GENERICS: @todo generics. This should be replaced by a similar loop
    // over the DJIs for the DMI as in BindPatch up above.
    MethodDesc *pFD = g_pEEInterface->FindLoadedMethodRefOrDef(module, md);

    _ASSERTE(pFD != NULL);

    LOG((LF_ENC, LL_ALWAYS,
         "DEnCBP::TP: in %s::%s\n", pFD->m_pszDebugClassName,pFD->m_pszDebugMethodName));

    // Grab the jit info for the original copy of the method, which is
    // what we are executing right now.
    DebuggerJitInfo *pJitInfo = m_jitInfo;
    _ASSERTE(pJitInfo);
    _ASSERTE(pJitInfo->m_nativeCodeVersion.GetMethodDesc() == pFD);

    // Grab the context for this thread. This is the context that was
    // passed to COMPlusFrameHandler.
    CONTEXT *pContext = GetManagedLiveCtx(thread);

    // We use the module the current function is in.
    _ASSERTE(module->IsEditAndContinueEnabled());
    EditAndContinueModule *pModule = (EditAndContinueModule*)module;

    // Release the controller lock for the rest of this method
    CrstBase::UnsafeCrstInverseHolder inverseLock(&g_criticalSection);

    // resumeIP is the native offset in the new version of the method the debugger wants
    // to resume to.  We'll pass the address of this variable over to the right-side
    // and if it modifies the contents while we're stopped dispatching the RemapOpportunity, 
    // then we know it wants a remap.
    // This form of side-channel communication seems like an error-prone workaround.  Ideally the
    // remap IP (if any) would just be returned in a response event.
    SIZE_T resumeIP = (SIZE_T) -1;

    // Debugging code to enable a break after N RemapOpportunities
#ifdef _DEBUG
        static int breakOnRemapOpportunity = -1;
        if (breakOnRemapOpportunity == -1)
            breakOnRemapOpportunity = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EnCBreakOnRemapOpportunity);

        static int remapOpportunityCount = 0;

        ++remapOpportunityCount;
        if (breakOnRemapOpportunity == 1 || breakOnRemapOpportunity == remapOpportunityCount)
        {
            _ASSERTE(!"BreakOnRemapOpportunity");
        }
#endif

    // Send an event to the RS to call the RemapOpportunity callback, passing the address of resumeIP.
    // If the debugger responds with a call to RemapFunction, the supplied IP will be copied into resumeIP
    // and we will know to update the context and resume the function at the new IP. Otherwise we just do
    // nothing and try again on next RemapFunction breakpoint
    g_pDebugger->LockAndSendEnCRemapEvent(pJitInfo, currentIP, &resumeIP);

    LOG((LF_ENC, LL_ALWAYS,
         "DEnCBP::TP: resume IL offset is 0x%x\n", resumeIP));

    // Has the debugger requested a remap?
    if (resumeIP != (SIZE_T) -1)
    {
        // This will jit the function, update the context, and resume execution at the new location.
        g_pEEInterface->ResumeInUpdatedFunction(pModule,
                                                pFD,
                                                (void*)pJitInfo,
                                                resumeIP,
                                                pContext);
        _ASSERTE(!"Returned from ResumeInUpdatedFunction!");
    }

    LOG((LF_CORDB, LL_ALWAYS, "DEnCB::TP: We've returned from ResumeInUpd"
        "atedFunction, we're going to skip the EnC patch ####\n"));

    // We're returning then we'll have to re-get this lock. Be careful that we haven't kept any controller/patches
    // in the caller. They can move when we unlock, so when we release the lock and reget it here, things might have
    // changed underneath us.
    // inverseLock holder will reaquire lock.

    return TPR_IGNORE;
}

//
// HandleResumeComplete is called for an EnC patch in the newly updated function
// so that we can notify the debugger that the remap has completed and they can
// now remap their steppers or anything else that depends on the new code actually
// being on the stack. We return TPR_IGNORE_AND_STOP because it's possible that the
// function was edited after we handled remap complete and want to make sure we
// start a fresh call to TriggerPatch
//
TP_RESULT DebuggerEnCBreakpoint::HandleRemapComplete(DebuggerControllerPatch *patch,
                                                     Thread *thread,
                                                     TRIGGER_WHY tyWhy)
{
    LOG((LF_ENC, LL_ALWAYS, "DEnCBP::HRC: HandleRemapComplete\n"));

    // Debugging code to enable a break after N RemapCompletes
#ifdef _DEBUG
    static int breakOnRemapComplete = -1;
    if (breakOnRemapComplete == -1)
        breakOnRemapComplete = CLRConfig::GetConfigValue(CLRConfig::INTERNAL_EnCBreakOnRemapComplete);

    static int remapCompleteCount = 0;
    ++remapCompleteCount;
    if (breakOnRemapComplete == 1 || breakOnRemapComplete == remapCompleteCount)
    {
        _ASSERTE(!"BreakOnRemapComplete");
    }
#endif
    _ASSERTE(HasLock());


    bool fApplied = m_jitInfo->m_encBreakpointsApplied;
    // Need to delete this before unlock below so if any other thread come in after the unlock
    // they won't handle this patch.
    Delete();

    // We just deleted ourselves. Can't access anything any instances after this point.

    // if have somehow updated this function before we resume into it then just bail
    if (fApplied)
    {
        LOG((LF_ENC, LL_ALWAYS, "DEnCBP::HRC:  function already updated, ignoring\n"));
        return TPR_IGNORE_AND_STOP;
    }

    // GENERICS: @todo generics. This should be replaced by a similar loop
    // over the DJIs for the DMI as in BindPatch up above.
    MethodDesc *pFD = g_pEEInterface->FindLoadedMethodRefOrDef(patch->key.module, patch->key.md);

    LOG((LF_ENC, LL_ALWAYS, "DEnCBP::HRC: unlocking controller\n"));

    // Unlock the controller lock and dispatch the remap complete event
    CrstBase::UnsafeCrstInverseHolder inverseLock(&g_criticalSection);

    LOG((LF_ENC, LL_ALWAYS, "DEnCBP::HRC: sending RemapCompleteEvent\n"));

    g_pDebugger->LockAndSendEnCRemapCompleteEvent(pFD);

    // We're returning then we'll have to re-get this lock. Be careful that we haven't kept any controller/patches
    // in the caller. They can move when we unlock, so when we release the lock and reget it here, things might have
    // changed underneath us.
    // inverseLock  holder will reacquire.
    
    return TPR_IGNORE_AND_STOP;
}
#endif //EnC_SUPPORTED

// continuable-exceptions
// * ------------------------------------------------------------------------ *
// * DebuggerContinuableExceptionBreakpoint routines
// * ------------------------------------------------------------------------ *


//---------------------------------------------------------------------------------------
//
// constructor
//
// Arguments:
//    pThread       - the thread on which we are intercepting an exception
//    nativeOffset  - This is the target native offset.  It is where we are going to resume execution.
//    jitInfo       - the DebuggerJitInfo of the method at which we are intercepting
//    pAppDomain    - the AppDomain in which the thread is executing 
//

DebuggerContinuableExceptionBreakpoint::DebuggerContinuableExceptionBreakpoint(Thread *pThread,
                                                                               SIZE_T nativeOffset,
                                                                               DebuggerJitInfo *jitInfo,
                                                                               AppDomain *pAppDomain)
  : DebuggerController(pThread, pAppDomain)
{
    _ASSERTE( jitInfo != NULL );
    // Add a native patch at the specified native offset, which is where we are going to resume execution.
    AddBindAndActivateNativeManagedPatch(jitInfo->m_nativeCodeVersion.GetMethodDesc(), jitInfo, nativeOffset, LEAF_MOST_FRAME, pAppDomain);
}

//---------------------------------------------------------------------------------------
//
// This function is called when the patch added in the constructor is hit.  At this point,
// we have already resumed execution, and the exception is no longer in flight.
//
// Arguments:
//    patch     - the patch added in the constructor; unused
//    thread    - the thread in question; unused
//    tyWhy     - a flag which is only useful for EnC; unused
//
// Return Value:
//    This function always returns TPR_TRIGGER, meaning that it wants to send an event to notify the RS.
//

TP_RESULT DebuggerContinuableExceptionBreakpoint::TriggerPatch(DebuggerControllerPatch *patch,
                                                               Thread *thread,
                                                               TRIGGER_WHY tyWhy)
{
    LOG((LF_CORDB, LL_INFO10000, "DCEBP::TP\n"));

    //
    // Disable the patch
    //
    DisableAll();

    // We will send a notification to the RS when the patch is triggered.
    return TPR_TRIGGER;
}

//---------------------------------------------------------------------------------------
//
// This function is called when we want to notify the RS that an interception is complete.
// At this point, we have already resumed execution, and the exception is no longer in flight.
//
// Arguments:
//    thread        - the thread in question
//    fIpChanged    - whether the IP has changed by SetIP after the patch is hit but 
//                    before this function is called
//

bool DebuggerContinuableExceptionBreakpoint::SendEvent(Thread *thread, bool fIpChanged)
{
    CONTRACTL
    {
        NOTHROW;
        SENDEVENT_CONTRACT_ITEMS;
    }
    CONTRACTL_END;



    LOG((LF_CORDB, LL_INFO10000,
         "DCEBP::SE: in DebuggerContinuableExceptionBreakpoint's SendEvent\n"));

    if (!fIpChanged)
    {
        g_pDebugger->SendInterceptExceptionComplete(thread);
    }

    // On WIN64, by the time we get here the DebuggerExState is gone already.
    // ExceptionTrackers are cleaned up before we resume execution for a handled exception.
#if !defined(WIN64EXCEPTIONS)
    thread->GetExceptionState()->GetDebuggerState()->SetDebuggerInterceptContext(NULL);
#endif // !WIN64EXCEPTIONS


    //
    // We delete this now because its no longer needed. We can call
    // delete here because the queued count is above 0. This object
    // will really be deleted when its dequeued shortly after this
    // call returns.
    //
    Delete();

    return true;
}

#ifdef FEATURE_DATABREAKPOINT

/* static */ bool DebuggerDataBreakpoint::TriggerDataBreakpoint(Thread *thread, CONTEXT * pContext)
{
    LOG((LF_CORDB, LL_INFO10000, "D::DDBP: Doing TriggerDataBreakpoint...\n"));

    bool hitDataBp = false;
    bool result = false;
#ifdef FEATURE_PAL    
    #error Not supported
#endif // FEATURE_PAL    
#if defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
    PDR6 pdr6 = (PDR6)&(pContext->Dr6);

    if (pdr6->B0 || pdr6->B1 || pdr6->B2 || pdr6->B3)
    {
        hitDataBp = true;
    }
#else // defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
    #error Not supported
#endif // defined(_TARGET_X86_) || defined(_TARGET_AMD64_)
    if (hitDataBp)
    {
        if (g_pDebugger->IsThreadAtSafePlace(thread))
        {
            LOG((LF_CORDB, LL_INFO10000, "D::DDBP: HIT DATA BREAKPOINT...\n"));
            result = true;
        }
        else
        {
            CONTEXT contextToAdjust;
            BOOL adjustedContext = FALSE;
            memcpy(&contextToAdjust, pContext, sizeof(CONTEXT));
            adjustedContext = g_pEEInterface->AdjustContextForWriteBarrierForDebugger(&contextToAdjust);        
            if (adjustedContext)
            {
                LOG((LF_CORDB, LL_INFO10000, "D::DDBP: HIT DATA BREAKPOINT INSIDE WRITE BARRIER...\n"));
                DebuggerDataBreakpoint *pDataBreakpoint = new (interopsafe) DebuggerDataBreakpoint(thread);
                pDataBreakpoint->AddAndActivateNativePatchForAddress((CORDB_ADDRESS_TYPE*)GetIP(&contextToAdjust), FramePointer::MakeFramePointer(GetFP(&contextToAdjust)), true, DPT_DEFAULT_TRACE_TYPE);
            }
            else
            {
                LOG((LF_CORDB, LL_INFO10000, "D::DDBP: HIT DATA BREAKPOINT BUT STILL NEED TO ROLL ...\n"));
                DebuggerDataBreakpoint *pDataBreakpoint = new (interopsafe) DebuggerDataBreakpoint(thread);
                pDataBreakpoint->EnableSingleStep();
            }
            result = false;
        }
    }
    else
    {
        LOG((LF_CORDB, LL_INFO10000, "D::DDBP: DIDN'T TRIGGER DATA BREAKPOINT...\n"));
        result = false;
    }
    return result;
}

TP_RESULT DebuggerDataBreakpoint::TriggerPatch(DebuggerControllerPatch *patch, Thread *thread,  TRIGGER_WHY tyWhy)
{
    if (g_pDebugger->IsThreadAtSafePlace(thread))
    {
        return TPR_TRIGGER;
    }
    else
    {
        LOG((LF_CORDB, LL_INFO10000, "D::DDBP: REACH RETURN OF JIT HELPER BUT STILL NEED TO ROLL ...\n"));
        this->EnableSingleStep();
        return TPR_IGNORE;
    }
}

bool DebuggerDataBreakpoint::TriggerSingleStep(Thread *thread, const BYTE *ip)
{
    if (g_pDebugger->IsThreadAtSafePlace(thread))
    {
        LOG((LF_CORDB, LL_INFO10000, "D:DDBP: Finally safe for stopping, stop stepping\n"));
        this->DisableSingleStep();
        return true;
    }
    else
    {
        LOG((LF_CORDB, LL_INFO10000, "D:DDBP: Still not safe for stopping, continue stepping\n"));
        return false;
    }
}

#endif // FEATURE_DATABREAKPOINT

#endif // !DACCESS_COMPILE