Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 0e0e2655d2fb58e6d9f3bdd43b66b5fb7fb33dff (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008, 2010 Monta Vista and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Monta Vista - initial API and implementation
 *     Ericsson    - Modified for handling of multiple execution contexts
 *     Ericsson    - Major updates for GDB/MI implementation
 *     Ericsson    - Major re-factoring to deal with children
 *     Axel Mueller - Bug 306555 - Add support for cast to type / view as array (IExpressions2)
 *     Jens Elmenthaler (Verigy) - Added Full GDB pretty-printing support (bug 302121)
 *******************************************************************************/
package org.eclipse.cdt.dsf.mi.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.eclipse.cdt.dsf.concurrent.CountingRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
import org.eclipse.cdt.dsf.concurrent.IDsfStatusConstants;
import org.eclipse.cdt.dsf.concurrent.RequestMonitor;
import org.eclipse.cdt.dsf.datamodel.DMContexts;
import org.eclipse.cdt.dsf.debug.service.IExpressions;
import org.eclipse.cdt.dsf.debug.service.IExpressions.IExpressionDMContext;
import org.eclipse.cdt.dsf.debug.service.IExpressions2.ICastedExpressionDMContext;
import org.eclipse.cdt.dsf.debug.service.IFormattedValues;
import org.eclipse.cdt.dsf.debug.service.IFormattedValues.FormattedValueDMContext;
import org.eclipse.cdt.dsf.debug.service.IFormattedValues.FormattedValueDMData;
import org.eclipse.cdt.dsf.debug.service.IMemory.IMemoryChangedEvent;
import org.eclipse.cdt.dsf.debug.service.IRunControl;
import org.eclipse.cdt.dsf.debug.service.IRunControl.IExecutionDMContext;
import org.eclipse.cdt.dsf.debug.service.IStack;
import org.eclipse.cdt.dsf.debug.service.IStack.IFrameDMContext;
import org.eclipse.cdt.dsf.debug.service.command.ICommand;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControl;
import org.eclipse.cdt.dsf.debug.service.command.ICommandControlService.ICommandControlDMContext;
import org.eclipse.cdt.dsf.debug.service.command.ICommandListener;
import org.eclipse.cdt.dsf.debug.service.command.ICommandResult;
import org.eclipse.cdt.dsf.debug.service.command.ICommandToken;
import org.eclipse.cdt.dsf.debug.service.command.IEventListener;
import org.eclipse.cdt.dsf.gdb.GDBTypeParser;
import org.eclipse.cdt.dsf.gdb.GDBTypeParser.GDBType;
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
import org.eclipse.cdt.dsf.gdb.service.IGDBTraceControl.ITraceRecordSelectedChangedDMEvent;
import org.eclipse.cdt.dsf.mi.service.MIExpressions.ExpressionInfo;
import org.eclipse.cdt.dsf.mi.service.MIExpressions.MIExpressionDMC;
import org.eclipse.cdt.dsf.mi.service.command.CommandFactory;
import org.eclipse.cdt.dsf.mi.service.command.commands.ExprMetaGetAttributes;
import org.eclipse.cdt.dsf.mi.service.command.commands.ExprMetaGetChildCount;
import org.eclipse.cdt.dsf.mi.service.command.commands.ExprMetaGetChildren;
import org.eclipse.cdt.dsf.mi.service.command.commands.ExprMetaGetValue;
import org.eclipse.cdt.dsf.mi.service.command.commands.ExprMetaGetVar;
import org.eclipse.cdt.dsf.mi.service.command.commands.MIDataEvaluateExpression;
import org.eclipse.cdt.dsf.mi.service.command.output.ExprMetaGetAttributesInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.ExprMetaGetChildCountInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.ExprMetaGetChildrenInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.ExprMetaGetValueInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.ExprMetaGetVarInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIDisplayHint;
import org.eclipse.cdt.dsf.mi.service.command.output.MIDisplayHint.GdbDisplayHint;
import org.eclipse.cdt.dsf.mi.service.command.output.MIInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVar;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarAssignInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarChange;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarCreateInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarDeleteInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarEvaluateExpressionInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarInfoPathExpressionInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarListChildrenInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarSetFormatInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarShowAttributesInfo;
import org.eclipse.cdt.dsf.mi.service.command.output.MIVarUpdateInfo;
import org.eclipse.cdt.dsf.service.DsfServiceEventHandler;
import org.eclipse.cdt.dsf.service.DsfServicesTracker;
import org.eclipse.cdt.dsf.service.DsfSession;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;

/**
 * Manages a list of variable objects as created through GDB/MI commands.
 * 
 * This class is passed expression-meta-commands which have their own cache.
 * Therefore, we don't use the standard MICommandCache in this class.
 * In fact, we can't even use it, because many variableObject MI commands,
 * should not be cached as they alter the state of the back-end.
 * 
 * Design details:
 * ==============
 * 
 * GDB variable object information
 * -------------------------------
 * o Variable objects are recursively hierarchical, where children can be created through 
 *   the parent.
 * o A varObject created with -var-create is a ROOT
 * o A varObject created with -var-list-children, is not a root
 * o Only varObject with no children or varObjects that are pointers can change values 
 *   and therefore 
 *   those objects can be used with -var-assign
 * o After a program stops, a varObject must be 'updated' before used 
 * o Only root varObject can be updated with -var-update, which will trigger all
 *   of the root's descendants to be updated.
 * o Once updated, a varObject need not be updated until the program resumes again;
 *   this is true even after -var-assign is used (because it does an implicit -var-update)
 * o -var-update will return the list of all modifiable descendants of the udpated root which
 *   have changed
 * o -var-update will indicate if a root is out-of-scope (which implies that all
 *   its descendants are out-of-scope)
 * o if a varObject is out-of-scope, another varObject may be valid for the same
 *   expression as the out-of-scope varObject
 * o deleting a varObject will delete all its descendants, therefore, it is only
 *   necessary to delete roots
 * 
 * 
 * Class details
 * -------------
 * - We have an MIVariableObject class which represents a variable object in GDB
 * 
 * - MIVariableObject includes a buffered value for each allowed format.
 * 
 * - We have an MIRootVariableObject class inheriting from MIVariableObject to describe
 *   root varObjects created with -var-create.  Objects created with -var-list-children
 *   are MIVariableObjects only.  The root class will keep track of if the root object 
 *   needs to be updated, if the root object is out-of-scope, and of a list of all 
 *   modifiable descendants of this root.  The list of modifiable descendants is 
 *   accessed using the gdb-given name to allow quick updates from the -var-update 
 *   result (see below.)
 * 
 * - we do not use -var-list-children for arrays, but create them manually
 *  
 * - when the program stops, we should mark all roots as needing to be updated. 
 * To achieve this efficiently, we have a dedicated list of roots that are updated.
 * When the program stops, we go through this list, remove each element and mark it
 * as needing to be updated.
 * 
 * - when a varObject is accessed, if its root must be updated, the var-update
 * command shall be used.  The result of that command will indicate all
 * modifiable descendants that have changed.  We also use --all-values with -var-update
 * to get the new value (in the current format) for each modified descendant.  Using the list of modifiable
 * descendants of the root, we can quickly update the changed ones to invalidate their buffered
 * values and store the new current format value.
 * 
 * - all values of non-modifiable varObjects (except arrays) will be set to {...}
 * without going to the back-end
 * 
 * - requesting the value of an array varObject will trigger the creation of a new
 * varObject for the array's address.  Note that we must still use a variable
 * object and not the command -data-evaluate-expression, because we still need to get
 * the array address in multiple formats.
 * 
 * - we keep an LRU (Least Recently Used) structure of all variable objects.  This LRU
 * will be bounded to a maximum allowed number of variable objects.  Whenever we get an
 * object from the LRU cleanup will be done if the maximum size has been reached.
 * The LRU will not delete a parent varObject until all its children are deleted; this is
 * achieved by touching each of the parents of an object whenever that object is put or get
 *
 * - It may happen that when accessing a varObject we find its root to be
 * out-of-scope.  The expression for which we are trying to access a varObject
 * could still be valid, and therefore we should try to create a new varObject for
 * that expression.  This can happen for example if two methods use the same name
 * for a variable. In the case when we find that a varObject is out-of-scope (when
 * its root is out-of-scope) the following should be done:
 *  - replace the varObject in the LRU with a newly created one in GDB
 *  - if the old object was a root, delete it in GDB.
 *  
 * - In GDB, -var-update will only report a change if -var-evaluate-expression has
 *   changed -- in the current format--.  This means that situations like
 *    double z = 1.2;
 *    z = 1.4;
 *   Will not report a change if the format is anything else than natural.
 *   This is because 1.2 and 1.4 are both printed as 1, 0x1, etc
 *   Since we cache the values of every format, we must know if the value has
 *   change in -any- format, not just the current one.
 *   To solve this, we always keep the display format of variable objects (and their
 *   children) to the natural format; we believe that if the value changes in any
 *   format, it guarantees that it will change in the natural format.
 *   The simplest way to do this is that whenever we change the format
 *   of a variable object, we immediately set it back to natural with a second
 *   var-set-format command.
 *   Note that versions of GDB after 6.7 will allows to issue -var-evaluate-expression
 *   with a specified format, therefore allowing us to never use -var-set-format, and
 *   consequently, to easily keep the display format of all variable objects to natural.
 *   
 * Notes on Dynamic Variable Objects (varobj)
 * ------------------------------------------
 *   - with version 7.0, gdb support so-called pretty printers.
 *   
 *   - pretty printers are registered for certain types
 *   
 *   - if there is a pretty printer registered for the type of a variable,
 *     the pretty printer provides the value and the children of that variable
 *     
 *   - a varobj whose value and/or children are provided by a pretty printer,
 *     are referred to as dynamic variable objects
 *     
 *   - dynamic varobjs change the game: it's not wise to ask it about all its
 *     children, not even the number of children it has. The reason is that
 *     in order to find out about the number of children the pretty printer
 *     must fetch all the children from the inferiors memory. If the variable
 *     is not yet initialized, the set of children are random, and thus might
 *     be huge. Even worse, there are data structures where fetching all
 *     children may result in an endless loop. The Eclipse debugger then hangs.
 *     
 *   - In order to avoid this, we will always fetch up to a certain maximum
 *     number of children. Furthermore, it is possible to find out whether there
 *     are more children available. In the UI, all the currently fetched
 *     children are available. In addition, if there are more children
 *     available, a special node will be appended to indicate that there is
 *     more the user could fetch.
 *     
 *   - Dynamic varobjs can change their value, as leaf varobjs can do.
 *     Especially, children can be added or removed during an update.
 *     
 *   - There is no expression for children of dynamic varobjs (at least not
 *     yet, http://sourceware.org/bugzilla/show_bug.cgi?id=10252 would fix that).
 *     The reason -var-info-path-expression returns garbage for children of
 *     dynamic varobjs.
 *
 *   - Because of this, the variable of an expression that is a child of
 *     a dynamic varobj cannot be created again using -var-create, once
 *     the LRU cache has deleted it. Instead, we track the parent and index
 *     within this parent for each non-root variable, and later use
 *       -var-list-children parent indexInParent (indexInParent + 1)
 *     in order to create the MI variable anew.
 *     
 *   - The fetching of children for dynamic varobjs becomes a bit more complicated.
 *     For the traditional varobjs, once children where requested, all children
 *     were fetched. For dynamic varobjs, we can no longer fetch all children.
 *     Instead, the client will provide a maximum number of children that
 *     is to be fetched. Every time the child count or children are requested,
 *     we must check whether there are additional children to be fetched,
 *     because the limit might have extended.
 *     Fetching additional children can only be done by one request monitor at a time.
 *     The serialization of the request monitors is done in getChildren by
 *     ensuring that fetchChildren is called for one request monitor at a time.
 *     fetchChildren in turn checks whether enough children are fetched, and
 *     if not, fetches the additional children.
 */
public class MIVariableManager implements ICommandControl {

	/**
	 * Stores the information about children of a variable object.
	 * 
	 * @since 4.0
	 */
	protected static class ChildrenInfo {
		private final ExpressionInfo[] children;
		private final boolean hasMore;
		
		public ChildrenInfo(ExpressionInfo[] children, boolean hasMore) {
			this.children = children;
			this.hasMore = hasMore;
		}

		/**
		 * @return The currently fetched children. Ask {@link #hasMore()} in
		 *         order to find out whether there are more to fetch.
		 */
		public ExpressionInfo[] getChildren() {
			return children;
		}
		
		/**
		 * @return true, if there are more than just those returned by
		 *         {@link #getChildren()}.
		 */
		public boolean hasMore() {
			return hasMore;
		}
	}
	
	/**
	 * Stores the information about the children count of a variable object.
	 * 
	 * @since 4.0
	 */
	protected static class ChildrenCountInfo {
		private final int childrenCount;
		private final boolean hasMore;
		
		public ChildrenCountInfo(int childrenCount, boolean hasMore) {
			this.childrenCount = childrenCount;
			this.hasMore = hasMore;
		}

		/**
		 * The number of children that we currently know of. Ask
		 * {@link #hasMore()} in order to find out whether there is at least one
		 * more.
		 */
		public int getChildrenCount() {
			return childrenCount;
		}

		/**
		 * @return <code>true</code> if there are more children than actually
		 *         returned by {@link #getChildrenCount()}.
		 */
		public boolean hasMore() {
			return hasMore;
		}
	}
	
	/**
	 * Utility class to track the progress and information of MI variable objects
	 */
	public class MIVariableObject {

		// Don't use an enumeration to allow subclasses to extend this
		protected static final int STATE_READY = 0;
		protected static final int STATE_UPDATING = 1;
		/** @since 4.0 */
		protected static final int STATE_NOT_CREATED = 10;
		/** @since 4.0 */
		protected static final int STATE_CREATING = 11;
		/** @since 4.0 */
		protected static final int STATE_CREATION_FAILED = 12;
	    
		protected int currentState;	

		// This is the lock used when we must run multiple
		// operations at once.  This lock should be independent of the
		// UPDATING state, which is why we don't make it part of the state
		private boolean locked = false;
		
	    // This id is the one used to search for this object in our hash-map
	    private final VariableObjectId internalId;
	    // This is the name of the variable object, as given by GDB (e.g., var1 or var1.public.x)
		private String gdbName = null;
		// The current format of this variable object, within GDB
		private String format = IFormattedValues.NATURAL_FORMAT;

		// The full expression that can be used to characterize this object
		// plus some other information that shall live longer than the
		// MIVariableObject.
		private ExpressionInfo exprInfo;
		private String type = null;
		private GDBType gdbType;
		// A hint at the number of children.  This value is obtained
		// from -var-create or -var-list-children.  It may not be right in the case
		// of C++ structures, where GDB has a level of children for private/public/protected.
		private int numChildrenHint = 0;
		private Boolean editable = null;

		// The current values of the expression for each format. (null if not known yet)
		private Map<String, String> valueMap = null;
		
		// A queue of request monitors waiting for this object to be ready
		private LinkedList<RequestMonitor> operationsPending;
		
		// A queue of request monitors that requested an update
		protected LinkedList<DataRequestMonitor<Boolean>> updatesPending;
		
		/** @since 4.0 */
		protected LinkedList<DataRequestMonitor<ChildrenInfo>> fetchChildrenPending;

		// The children of this variable, if any.  
		// Null means we didn't fetch them yet, while an empty array means no children
        private ExpressionInfo[] children = null; 
		private boolean hasMore = false;
		private MIDisplayHint displayHint = MIDisplayHint.NONE;
		
        // The parent of this variable object within GDB.  Null if this object has no parent
        private MIVariableObject parent = null;
        
        // The root parent that must be used to issue -var-update.
        // If this object is a root, then the rootToUpdate is itself
        private MIRootVariableObject rootToUpdate = null;
                
		protected boolean outOfScope = false;
		
		private boolean fetchingChildren = false;
		
		public MIVariableObject(VariableObjectId id, MIVariableObject parentObj) {
			this(id, parentObj, false);
		}
		
		/** @since 4.0 */
		public MIVariableObject(VariableObjectId id, MIVariableObject parentObj,
				boolean needsCreation) {
			currentState = needsCreation ? STATE_NOT_CREATED : STATE_READY; 
			
			operationsPending = new LinkedList<RequestMonitor>();
			updatesPending = new LinkedList<DataRequestMonitor<Boolean>>();
			fetchChildrenPending = new LinkedList<DataRequestMonitor<ChildrenInfo>>();
			
			internalId = id;
			setParent(parentObj);
			
			// No values are available yet
			valueMap = new HashMap<String, String>();
			resetValues();
		}
		
		public VariableObjectId getInternalId() { return internalId; }
		public String getGdbName() { return gdbName; }
		public String getCurrentFormat() {	return format; }
		public MIVariableObject getParent() { return parent; }
		public MIRootVariableObject getRootToUpdate() { return rootToUpdate; }
		
		public String getExpression() { return exprInfo.getFullExpr(); }
		public String getType() { return type; }
		
		/**
		 * @since 4.0
		 */
		public ExpressionInfo getExpressionInfo() { return exprInfo; }

		/**
		 * @return <code>true</code> if value and children of this varobj are
		 *         currently provided by a pretty printer.
		 *         
		 * @since 4.0
		 */
		public boolean isDynamic() { return exprInfo.isDynamic(); }
		
		/**
		 * @return For dynamic varobjs ({@link #isDynamic() returns true}) this
		 *         method returns whether there are children in addition to the
		 *         currently fetched, i.e. whether there are more children than
		 *         {@link #getNumChildrenHint()} returns.
		 *         
		 * @since 4.0
		 */
		public boolean hasMore() { return hasMore; }

		/**
		 * @since 4.0
		 */
		public MIDisplayHint getDisplayHint() { return displayHint; };
		
		/**
		 * @since 4.0
		 */
		protected void setDisplayHint(MIDisplayHint displayHint) {
			this.displayHint = displayHint;
		};
		
		/**
		 * @since 4.0
		 */
		public boolean hasChildren() { return (getNumChildrenHint() != 0 || hasMore()); }

		/** @since 3.0 */
		public GDBType getGDBType() { return gdbType; }
		/** 
		 * Returns a hint to the number of children.  This hint is often correct,
		 * except when we are dealing with C++ complex structures where
		 * GDB has 'private/public/protected' as children.
		 * 
		 * Use <code>isNumChildrenHintTrustworthy()</code> to know if the
		 * hint can be trusted.
		 * 
		 * Note that a hint of 0 children can always be trusted, except for
		 * <code>{@link #hasMore} == true</code>.
		 * 
		 * @since 3.0 */
		public int getNumChildrenHint() { return numChildrenHint; }
		/** 
		 * Returns whether the number of children hint can be 
		 * trusted for this variable object.
		 * 
		 * @since 3.0 
		 */
		public boolean isNumChildrenHintTrustworthy() {
			// We cannot trust the hint about the number of children when we are
			// dealing with a complex structure that could have the
			// 'protected/public/private' children types.
			// Note that a pointer could have such children, if it points to
			// a complex structure.
			//
			// This is only valid for C++, so we should even check for it using
			// -var-info-expression.  Do we have to use -var-info-expression for each
			// variable object, or can we do it one time only for the whole program?
			// Right now, we always assume we could be using C++
			return ((getNumChildrenHint() == 0 && ! hasMore()) || isArray());
		}
 
		public String getValue(String format) { return valueMap.get(format); }
		
        public ExpressionInfo[] getChildren() { return children; }

        
		public boolean isArray() { return (getGDBType() == null) ? false : getGDBType().getType() == GDBType.ARRAY; }
		public boolean isPointer() { return (getGDBType() == null) ? false : getGDBType().getType() == GDBType.POINTER; }
		public boolean isMethod() { return (getGDBType() == null) ? false : getGDBType().getType() == GDBType.FUNCTION; }
		// A complex variable is one with children.  However, it must not be a pointer since a pointer 
		// does have children, but is still a 'simple' variable, as it can be modifed.  
		// Note that the numChildrenHint can be trusted when asking if the number of children is 0 or not
		public boolean isComplex() {
			return (getGDBType() == null) ? false
					: getGDBType().getType() != GDBType.POINTER
							&& (getNumChildrenHint() > 0
									|| hasMore() || getDisplayHint().isCollectionHint());
		}
		
		/**
		 * @return Whether this varobj can safely be asked for all its children.
		 * 
		 * @since 4.0
		 */
		public boolean isSafeToAskForAllChildren() {
			GdbDisplayHint displayHint = getDisplayHint().getGdbDisplayHint();
			
			// Here we balance usability against a slight risk of instability:
			//
			// Usability: if you have a class/struct-like pretty printer
			// all children are fetched, regardless of any limit. This
			// should be safe from gdb side.
			//
			// Risk: If somebody provides a pretty printer for a collection,
			// but forgets to implement the display_hint method, viewing
			// the collection while it is uninitiliazed may cause gdb
			// to never return.
			//
			// => The risk seams reasonable, so we require a limit only
			//    for collections.
			boolean isDynamicButSafe = (displayHint == GdbDisplayHint.GDB_DISPLAY_HINT_STRING)
					|| (displayHint == GdbDisplayHint.GDB_DISPLAY_HINT_NONE);
			
			return !isDynamic() || isDynamicButSafe;
		}
		
		public void setGdbName(String n) { gdbName = n; }
		public void setCurrentFormat(String f) { format = f; }

		/**
		 * @param fullExpression
		 * @param t
		 * @param num
		 * 
		 * @deprecated Use
		 *             {@link #setExpressionData(ExpressionInfo, String, int, boolean)}
		 *             instead.
		 */
		@Deprecated
		public void setExpressionData(String fullExpression, String t, int num) {
			new ExpressionInfo(fullExpression, fullExpression);
		}

		/**
		 * @param info
		 * @param t
		 * @param num
		 *            If the correspinding MI variable is dynamic, the number of
		 *            children currently fetched by gdb.
		 * @param hasMore
		 *            Whether their are more children to fetch.
		 *            
		 * @since 4.0
		 */
		public void setExpressionData(ExpressionInfo info, String t, int num, boolean hasMore) {
			exprInfo = info;
			type = t;
			gdbType = fGDBTypeParser.parse(t);
			numChildrenHint = num;
			this.hasMore = hasMore;
		}

		public void setValue(String format, String val) { valueMap.put(format, val); }
		
		public void resetValues(String valueInCurrentFormat) {
			resetValues();
			setValue(getCurrentFormat(), valueInCurrentFormat); 
		}
		
		public void resetValues() {
			valueMap.put(IFormattedValues.NATURAL_FORMAT, null);
			valueMap.put(IFormattedValues.BINARY_FORMAT, null);
			valueMap.put(IFormattedValues.HEX_FORMAT, null);
			valueMap.put(IFormattedValues.OCTAL_FORMAT, null);
			valueMap.put(IFormattedValues.DECIMAL_FORMAT, null);
		}
		
		/**
		 * @param c
		 *            The new children, or null in order to force fetching
		 *            children anew.
		 */
        public void setChildren(ExpressionInfo[] c) { 
        	children = c;
        	if (children != null) {
        		numChildrenHint = children.length;
        	}

        	if (children != null) {
        		for (ExpressionInfo child : children) {
        			assert (child != null);
        		}
        	}
		}
        
        /**
         * @param newChildren
         * 
         * @since 4.0
         */
        public void addChildren(ExpressionInfo[] newChildren) {
        	if (children == null) {
        		children = new ExpressionInfo[newChildren.length]; 
        		System.arraycopy(newChildren, 0, children, 0, newChildren.length);
        	} else {
        		ExpressionInfo[] oldChildren = children;
        		
        		children = new ExpressionInfo[children.length + newChildren.length];

        		System.arraycopy(oldChildren, 0, children, 0, oldChildren.length);
        		System.arraycopy(newChildren, 0, children, oldChildren.length, newChildren.length);
        	}
        	
        	numChildrenHint = children.length;

			for (ExpressionInfo child : children) {
				assert (child != null);
			}
        }

        /**
         * @param newChildren
         * 
         * @since 4.0
         */
        protected void addChildren(ExpressionInfo[][] newChildren) {
        	int requiredSize = 0;
        	
        	for (ExpressionInfo[] subArray : newChildren) {
        		requiredSize += subArray.length;
			}

        	ExpressionInfo[] plainChildren = new ExpressionInfo[requiredSize];

        	int i = 0;
        	for (ExpressionInfo[] subArray : newChildren) {
        		System.arraycopy(subArray, 0, plainChildren, i, subArray.length);
        		i += subArray.length;
			}
        	
        	addChildren(plainChildren);
        }
        
        /**
         * @param newNumChildren
         * 
         * @since 4.0
         */
        public void shrinkChildrenTo(int newNumChildren) {
        	if (children != null) {
        		ExpressionInfo[] oldChildren = children;
        		for (int i = oldChildren.length - 1; i >= newNumChildren; --i) {
        			String childFullExpression = children[i].getFullExpr();

        			VariableObjectId childId = new VariableObjectId();
        			childId.generateId(childFullExpression, getInternalId());
        			lruVariableList.remove(childId);
        		}


        		children = new ExpressionInfo[newNumChildren];
        		System.arraycopy(oldChildren, 0, children, 0, newNumChildren);
        	}
        	
        	numChildrenHint = newNumChildren;
        }
        
        public void setParent(MIVariableObject p) { 
        	parent = p; 
        	if (p == null) {
				rootToUpdate = (this instanceof MIRootVariableObject) ? (MIRootVariableObject) this
						: null;
        	} else {
        		rootToUpdate = p.getRootToUpdate();
        	}
        }
        
        public void executeWhenNotUpdating(RequestMonitor rm) {
			getRootToUpdate().executeWhenNotUpdating(rm);
		}

		private void lock() {
			locked = true;
		}
		
		private void unlock() {
			locked = false;

			while (operationsPending.size() > 0) {
				operationsPending.poll().done();
			}
		}

		public boolean isOutOfScope() { return outOfScope; }

		/**
		 * @param success
		 * 
		 * @since 4.0
		 */
		protected void creationCompleted(boolean success) {
        	// A creation completed we must be up-to-date, so we
			// can tell any pending monitors that updates are done
			if (success) {
				currentState = STATE_READY;
				while (updatesPending.size() > 0) {
					DataRequestMonitor<Boolean> rm = updatesPending.poll();
					// Nothing to be re-created
					rm.setData(false);
					rm.done();
				}
			} else {
				currentState = STATE_CREATION_FAILED;

				// Creation failed, inform anyone waiting.
				while (updatesPending.size() > 0) {
					RequestMonitor rm = updatesPending.poll();
		            rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_HANDLE, 
		            		"Unable to create variable object", null)); //$NON-NLS-1$
					rm.done();
				}
			}
		}

		/**
		 * This method updates the variable object.
		 * Updating a variable object is done by updating its root.
		 */
		public void update(final DataRequestMonitor<Boolean> rm) {
			
			// We check to see if we are already out-of-scope
			// We must do this to avoid the risk of re-creating this object
			// twice, due to race conditions
			if (isOutOfScope()) {
				rm.setData(false);
				rm.done();
			} else if (currentState != STATE_READY) {
				// If we were already updating, we just queue the request monitor
				// until the on-going update finishes.
				updatesPending.add(rm);
			} else {			
				currentState = STATE_UPDATING;
				getRootToUpdate().update(new DataRequestMonitor<Boolean>(fSession.getExecutor(), rm) {
					@Override
					protected void handleCompleted() {
						currentState = STATE_READY;
						
						if (isSuccess()) {
							outOfScope = getRootToUpdate().isOutOfScope();
							// This request monitor is the one that should re-create
							// the variable object if the old one was out-of-scope							
							rm.setData(outOfScope);
							rm.done();
							
							// All the other request monitors must be notified but must
							// not re-create the object, even if it is out-of-scope
							while (updatesPending.size() > 0) {
								DataRequestMonitor<Boolean> pendingRm = updatesPending.poll();
								pendingRm.setData(false);
								pendingRm.done();
							}
						} else {
							rm.setStatus(getStatus());
							rm.done();

							while (updatesPending.size() > 0) {
								DataRequestMonitor<Boolean> pendingRm = updatesPending.poll();
								pendingRm.setStatus(getStatus());
								pendingRm.done();
							}							
						}
					}
				});
			}
		}
		
		/**
         * Process an update on this variable object.
         *
         * @param update What has changed.
         * 
         * @since 4.0
         */
		protected void processChange(final MIVarChange update, final RequestMonitor rm) {

			MIVar[] newChildren = update.getNewChildren();

			// children == null means fetchChildren will happen later, so
			// don't try to create a sparsely filled children array here.
			final boolean addNewChildren = (children != null);

			final ExpressionInfo[] addedChildren = (addNewChildren && (newChildren != null)) ? new ExpressionInfo[newChildren.length]
					: null;

			final CountingRequestMonitor countingRm = new CountingRequestMonitor(fSession.getExecutor(), rm) {

				@Override
				protected void handleCompleted() {
					
					if (! isSuccess()) {
						rm.setStatus(getStatus());
					} else {
						if (update.numChildrenChanged()) {
							if (children != null) {
								// Remove those children that don't exist any longer.
								if (children.length > update.getNewNumChildren()) {
									shrinkChildrenTo(update.getNewNumChildren());
								}
							} else {
								// Just update the child count.
								numChildrenHint = update.getNewNumChildren();
							}

							// Add the new children.
							if ((addedChildren != null) && (addedChildren.length != 0)) {
								addChildren(addedChildren);
							}
						}
						
						assert((children == null) || (children.length == numChildrenHint));

						hasMore = update.hasMore();

						// If there was no -var-list-children yet for a varobj,
						// the new children will not be reported by -var-update.
						// Set the children to null such that the next time children
						// are requested, they will be fetched.
						if (hasMore() && (numChildrenHint == 0)) {
							setChildren(null);
						}	

						resetValues(update.getValue());
					}
					rm.done();
				}
			};
			
			// Process all the child MIVariableObjects.
			int pendingVariableCreationCount = 0;
			if (newChildren != null && newChildren.length != 0) {
				int i = update.getNewNumChildren() - newChildren.length;
				int arrayPosition = 0;				

				for (final MIVar newChild : newChildren) {

					// As long as http://sourceware.org/bugzilla/show_bug.cgi?id=10252
					// is not fixed, it doesn't make sense to use
					// -var-info-path-expression. New children can only
					// be added during the update, if we are a child of a
					// dynamic varobj, and in this case -var-info-path-expression
					// won't work.
					final String childFullExpression = buildChildExpression(
							getExpression(), newChild.getExp());
					
					// Now try to see if we already have this variable
					// object in our Map
					// Since our map names use the expression, and not the
					// GDB given
					// name, we must determine the correct map name from the
					// varName
					final VariableObjectId childId = new VariableObjectId();
					childId.generateId(childFullExpression, getInternalId());
					MIVariableObject childVar = lruVariableList.get(childId);

					if (childVar != null) {
						if ((childVar.currentState == STATE_CREATING)
								|| (childVar.currentState == STATE_NOT_CREATED)) {

							// We must wait until the child MIVariableObject is fully created.
							// This might succeed, or fail. If it succeeds, we can reuse it as
							// a child, otherwise we create a new MIVariableObject for the
							// varobj just provided by gdb.
							
							++pendingVariableCreationCount;
							
							final int insertPosition = arrayPosition;
							final MIVariableObject monitoredVar = childVar;
							final int indexInParent = i;
							
							// varobj is not fully created so add RequestMonitor to pending queue
							childVar.updatesPending.add(new DataRequestMonitor<Boolean>(fSession.getExecutor(), countingRm) {

								@Override
								protected void handleCompleted() {
									if (isSuccess()) {
										if (addNewChildren) {
											addedChildren[insertPosition] = monitoredVar.exprInfo;
										}
									} else {
										// Create a fresh MIVariableObject for this child, using
										// the new varobj provided by gdb.
										MIVariableObject newVar = createChild(childId, childFullExpression,
												indexInParent, newChild);
										if (addNewChildren) {
											addedChildren[insertPosition] = newVar.exprInfo;
										}
									}
									
									countingRm.done();
								}
								
							});

						} else if (childVar.currentState == STATE_CREATION_FAILED) {
							// There has been an attempt the create a MIRootVariableObject for a full
							// expression representing a child of a dynamic varobj. Such an attempt
							// always fails. But here we can now create it (see below).
							childVar = null;
						}
						// Note that we must check the root to know if it is out-of-scope.
						// We cannot check the child as it has not be updated and its
						// outOfScope variable is not updated either.
						else if (childVar.getRootToUpdate().isOutOfScope()) {
							childVar.deleteInGdb();
							childVar = null;
						}
					}

					// Note: we don't need to check for fake children (public, protected, private) here.
					// We enter this code only if the children are provided by a pretty printer and
					// they don't return such children.
					
					if (childVar == null) {
						// Create a fresh MIVariableObject for this child, using
						// the new varobj provided by -var-update.
						childVar = createChild(childId, childFullExpression, i, newChild);
						if (addNewChildren) {
							addedChildren[arrayPosition] = childVar.exprInfo;
						}
					}
					
					++i;
					++arrayPosition;
				}
			}

			countingRm.setDoneCount(pendingVariableCreationCount);
		}

		/**
		 * Variable objects need not be deleted unless they are root.
		 * This method is specialized in the MIRootVariableObject class.
		 */
		public void deleteInGdb() {}
		
		/** 
		 * This method returns the value of the variable object attributes by
		 * using -var-show-attributes.
		 * Currently, the only attribute available is 'editable'.
		 *  
		 * @param rm
		 *            The data request monitor that will hold the value returned
		 */
        private void getAttributes(final DataRequestMonitor<Boolean> rm) {
            if (editable != null) { 
                rm.setData(editable);
                rm.done();
            } else if (isComplex()) {
                editable = false;
                rm.setData(editable);
                rm.done();
            } else {
                fCommandControl.queueCommand(
                		fCommandFactory.createMIVarShowAttributes(getRootToUpdate().getControlDMContext(), getGdbName()), 
                        new DataRequestMonitor<MIVarShowAttributesInfo>(fSession.getExecutor(), rm) {
                            @Override
                            protected void handleSuccess() {
                            	editable = getData().isEditable();

                            	rm.setData(editable);
                            	rm.done();
                            }
                        });
            }
        }

		/** 
		 * This method returns the value of the variable object.
		 * This operation translates to multiple MI commands which affect the state of the
		 * variable object in the back-end; therefore, we must make sure the object is not
		 * locked doing another operation, and we must lock the object once it is our turn
		 * to use it.
		 * 
		 * @param dmc  
		 *            The context containing the format to be used for the evaluation
		 * @param rm
		 *            The data request monitor that will hold the value returned
		 */
		private void getValue(final FormattedValueDMContext dmc,
				              final DataRequestMonitor<FormattedValueDMData> rm) {

			// We might already know the value
			String value = getValue(dmc.getFormatID());
			if (value != null) {
				rm.setData(new FormattedValueDMData(value));
				rm.done();
				return;
			}

			// If the variable is a complex structure, there is no need to ask the back-end for a value,
			// we can give it the {...} ourselves
			// Unless we are dealing with an array, in which case, we want to get the address of it
			if (isComplex() && ! isDynamic()) {
				if (isArray()) {
					// Figure out the address
					IExpressionDMContext exprCxt = DMContexts.getAncestorOfType(dmc, IExpressionDMContext.class);
					IExpressionDMContext addrCxt = fExpressionService.createExpression(exprCxt, "&(" + exprCxt.getExpression() + ")");  //$NON-NLS-1$//$NON-NLS-2$

					final FormattedValueDMContext formatCxt = new FormattedValueDMContext(
							fSession.getId(),
							addrCxt,
							dmc.getFormatID()
					);

					getVariable(
							addrCxt,
							new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), rm) {
								@Override
								protected void handleSuccess() {
									getData().getValue(formatCxt, rm);

								}
							});
				} else {
					// Other complex structure
					String complexValue = "{...}";     //$NON-NLS-1$
					setValue(dmc.getFormatID(), complexValue);
					rm.setData(new FormattedValueDMData(complexValue));
					rm.done();
				}
				
				return;
			}

			if (locked) {
				operationsPending.add(new RequestMonitor(fSession.getExecutor(), rm) {
					@Override
					protected void handleSuccess() {
						getValue(dmc, rm);
					}
				});
			} else {
				lock();

				// If the format is already the one set for this variable object,
				// we don't need to set it again
				if (dmc.getFormatID().equals(getCurrentFormat())) {
					evaluate(rm);								
				} else {
					// We must first set the new format and then evaluate the variable
					fCommandControl.queueCommand(
							fCommandFactory.createMIVarSetFormat(getRootToUpdate().getControlDMContext(), getGdbName(), dmc.getFormatID()), 
							new DataRequestMonitor<MIVarSetFormatInfo>(fSession.getExecutor(), rm) {
								@Override
								protected void handleCompleted() {
									if (isSuccess()) {
										setCurrentFormat(dmc.getFormatID());
										
										// If set-format returned the value, no need to evaluate
										// This is only valid after GDB 6.7
										if (getData().getValue() != null) {
    										setValue(dmc.getFormatID(), getData().getValue());
											rm.setData(new FormattedValueDMData(getData().getValue()));
											rm.done();
											
											// Unlock is done within this method
											resetFormatToNatural();
										} else {
										    evaluate(rm);
										}
									} else {
										rm.setStatus(getStatus());
										rm.done();

										unlock();
									}
								}
							});
				}
			}
		}

		/** 
		 * This method evaluates a variable object 
		 */
		private void evaluate(final DataRequestMonitor<FormattedValueDMData> rm) {
			fCommandControl.queueCommand(
					fCommandFactory.createMIVarEvaluateExpression(getRootToUpdate().getControlDMContext(), getGdbName()), 
					new DataRequestMonitor<MIVarEvaluateExpressionInfo>(fSession.getExecutor(), rm) {
						@Override
						protected void handleCompleted() {
							if (isSuccess()) {
								setValue(getCurrentFormat(), getData().getValue());
								rm.setData(new FormattedValueDMData(getData().getValue()));
								rm.done();
							} else {
								rm.setStatus(getStatus());
								rm.done();
							}
							
							// Unlock is done within this method
							resetFormatToNatural();
						}
					});
		}

		// In GDB, var-update will only report a change if -var-evaluate-expression has
		// changed -- in the current format--.  This means that situations like
		// double z = 1.2;
		// z = 1.4;
		// Will not report a change if the format is anything else than natural.
		// This is because 1.2 and 1.4 are both printed as 1, 0x1, etc
		// Since we cache the values of every format, we must know if -any- format has
		// changed, not just the current one.
		// To solve this, we always do an update in the natural format; I am not aware
		// of any case where the natural format would stay the same, but another format
		// would change.  However, since a var-update update all children as well,
		// we must make sure these children are also in the natural format
		// The simplest way to do this is that whenever we change the format
		// of a variable object, we immediately set it back to natural with a second
		// var-set-format command.
		private void resetFormatToNatural() {
			if (!getCurrentFormat().equals(IFormattedValues.NATURAL_FORMAT)) {
				fCommandControl.queueCommand(
						fCommandFactory.createMIVarSetFormat(getRootToUpdate().getControlDMContext(), getGdbName(), IFormattedValues.NATURAL_FORMAT), 
						new DataRequestMonitor<MIVarSetFormatInfo>(fSession.getExecutor(), null) {
							@Override
							protected void handleCompleted() {
								if (isSuccess()) {
									setCurrentFormat(IFormattedValues.NATURAL_FORMAT);
								}
								unlock();
							}
						});
			} else {
				unlock();
			}			
		}

		/**
		 * This method returns the list of children of the variable object
		 * passed as a parameter.
		 * 
		 * @param exprDmc
		 * 
		 * @param clientNumChildrenLimit
		 *            If the current limit for the given expression is smaller,
		 *            this limit will be applied.
		 * @param rm
		 *            The data request monitor that will hold the children
		 *            returned
		 */
		private void getChildren(final MIExpressionDMC exprDmc,
				final int clientNumChildrenLimit, final DataRequestMonitor<ChildrenInfo> rm) {
			
			if (fetchingChildren) {
				// Only one request monitor can fetch children at a time.
				fetchChildrenPending.add(new DataRequestMonitor<ChildrenInfo>(fSession.getExecutor(), rm) {

					@Override
					protected void handleSuccess() {
						ChildrenInfo info = getData();
						int numChildren = info.getChildren().length;
						if (! info.hasMore() || numChildren >= clientNumChildrenLimit) {
							// No need to fetch further children.
							rm.setData(getData());
							rm.done();
						} else {
							// Need to retry.
							getChildren(exprDmc, clientNumChildrenLimit, rm);
						}
					}
					
				});
			} else {
			
				fetchingChildren = true;
				
				fetchChildren(exprDmc, clientNumChildrenLimit, new DataRequestMonitor<ChildrenInfo>(fSession.getExecutor(), rm) {

					@Override
					protected void handleCompleted() {
						fetchingChildren = false;

						if (isSuccess()) {
							rm.setData(getData());
							rm.done();
							
							while (fetchChildrenPending.size() > 0) {
								DataRequestMonitor<ChildrenInfo> pendingRm = fetchChildrenPending.poll();
								pendingRm.setData(getData());
								pendingRm.done();
							}
						} else {
							rm.setStatus(getStatus());
							rm.done();

							while (fetchChildrenPending.size() > 0) {
								DataRequestMonitor<ChildrenInfo> pendingRm = fetchChildrenPending.poll();
								pendingRm.setStatus(getStatus());
								pendingRm.done();
							}							
						}
					}
				});
			}
		}

		/**
		 * Fetch the out-standing children.
		 * 
		 * @param exprDmc
		 * 
		 * @param clientNumChildrenLimit
		 *            If the current limit for the given expression is smaller,
		 *            this limit will be applied.
		 * @param rm
		 *            The data request monitor that will hold the children
		 *            returned
		 */
		private void fetchChildren(final MIExpressionDMC exprDmc,
				int clientNumChildrenLimit, final DataRequestMonitor<ChildrenInfo> rm) {
			
			final int newNumChildrenLimit = clientNumChildrenLimit != IMIExpressions.CHILD_COUNT_LIMIT_UNSPECIFIED ?
					clientNumChildrenLimit : 1;

			boolean addChildren = requiresAdditionalChildren(newNumChildrenLimit);
			
	        // If we already know the children, no need to go to the back-end
			ExpressionInfo[] childrenArray = getChildren();
	        if (childrenArray != null && ! addChildren) {
	            rm.setData(new ChildrenInfo(childrenArray, hasMore));
	            rm.done();
	            return;
	        }
	        
			// If the variable does not have children, we can return an empty list right away
			if (! hasChildren()) {
	        	// First store the empty list, for the next time
				setChildren(new ExpressionInfo[0]);
				rm.setData(new ChildrenInfo(getChildren(), hasMore));
				rm.done();
				return;
			}
	        
	        // For arrays (which could be very large), we create the children ourselves.  This is
	        // to avoid creating an enormous amount of children variable objects that the view may
	        // never need.  Using -var-list-children will create a variable object for every child
	        // immediately, that is why we don't want to use it for arrays.
	        if (isArray()) {
	        	ExpressionInfo[] childrenOfArray = new ExpressionInfo[getNumChildrenHint()];
	        	String exprName = exprDmc.getExpression();
	        	
        		int castingIndex = 0;
	        	// in case of casts, need to resolve that before dereferencing, to be safe
	        	if (exprDmc instanceof ICastedExpressionDMContext) {
	        		// When casting, if we are dealing with a resulting array, we should surround
	        		// it with parenthesis before we subscript it.
	        		exprName = '(' + exprName + ')';
	        		castingIndex = ((ICastedExpressionDMContext)exprDmc).getCastInfo().getArrayStartIndex();
	        	}
	        	for (int i= 0; i < childrenOfArray.length; i++) {
	        		String fullExpr = exprName + "[" + i + "]";//$NON-NLS-1$//$NON-NLS-2$
	        		String relExpr = exprDmc.getRelativeExpression() + "[" + (castingIndex + i) + "]";//$NON-NLS-1$//$NON-NLS-2$

	        		childrenOfArray[i] = new ExpressionInfo(fullExpr, relExpr, false, exprInfo, i);
	        	}

	        	// First store these children, for the next time
				setChildren(childrenOfArray);
				hasMore = false;
				rm.setData(new ChildrenInfo(getChildren(), hasMore));
	        	rm.done();
	        	return;
	        }
	        
	        // No need to wait for the object to be ready since listing children can be performed
	        // at any time, as long as the object is created, which we know it is, since we can only
	        // be called here with a fully created object.
	        // Also no need to lock the object, since getting the children won't affect other operations

	        final int from = (addChildren && (children != null)) ? getNumChildrenHint() : 0;
	        final int to = Math.max(newNumChildrenLimit, exprInfo.getChildCountLimit());
	        
	        ICommand<MIVarListChildrenInfo> varListChildren = isSafeToAskForAllChildren() ?
	        		fCommandFactory.createMIVarListChildren(getRootToUpdate().getControlDMContext(), getGdbName())
	        		:fCommandFactory.createMIVarListChildren(getRootToUpdate().getControlDMContext(), getGdbName(), from, to);
					
	        fCommandControl.queueCommand(
	        		varListChildren,
	        		new DataRequestMonitor<MIVarListChildrenInfo>(fSession.getExecutor(), rm) {
	        			@Override
	        			protected void handleSuccess() {
	        				MIVar[] children = getData().getMIVars();
	        				final boolean localHasMore = getData().hasMore();
	        				
	        				// The elements of this array normally are an ExpressionInfo, unless it corresponds to
	        				// a fake child (public, protected, private). In this case it is an ExpressionInfo[]
	        				// representing the children of the fake child.
	        				// This in done in order to preserve the order (the index-in-parent information),
	        				// when replacing a fake child by its real children.
	        				final ExpressionInfo[][] realChildren = new ExpressionInfo[children.length][];

	        				final CountingRequestMonitor countingRm = new CountingRequestMonitor(fSession.getExecutor(), rm) {
	        					@Override
	        					protected void handleSuccess() {
	        						// Store the children in our variable object cache
	        						addChildren(realChildren);
	        						hasMore = localHasMore; 
	        						rm.setData(new ChildrenInfo(getChildren(), hasMore));
	        						
	        						int updateLimit = updateLimit(to);
	        						
	        						if (! isSafeToAskForAllChildren()) {
	        							// Make sure the gdb will not hang, if later
	        							// the varobj is updated, but the underlying
	        							// data is still uninitialized.
	        							fCommandControl.queueCommand(
	        									fCommandFactory.createMIVarSetUpdateRange(getRootToUpdate().getControlDMContext(),
	        									getGdbName(), 0, updateLimit),	new DataRequestMonitor<MIInfo>(fSession.getExecutor(), rm));
	        						} else {	        							
	        							rm.done();
	        						}
	        					}
	        				};

	        				int numSubRequests = 0;
	        				for (int i = 0; i < children.length; ++i) {
	        					final MIVar child = children[i];
	        					final int indexInParent = from + i;
	        					final int arrayPosition = i;
	        					
	        					// These children get created automatically as variable objects in GDB, so we should
	        					// add them to the LRU.
	        					// Note that if this variable object already exists, we can be in three scenarios:
	        					// 1- the existing object is the same variable object in GDB.  In this case,
	        					//    the existing and new one are identical so we can keep either one.
	        					// 2- the existing object is out-of-scope and should be replaced by the new one.
	        					//    This can happen if a root was found to be out-of-scope, but this child
	        					//    had not been accessed and therefore had not been removed.
	        					// 3- the existing object is an in-scope root object representing the same expression.
	        					//    In this case, either object can be kept and the other one can be deleted.
	        					//    The existing root could currently be in-use by another operation and may
	        					//    not be deleted; but since we can only have one entry in the LRU, we are
	        					//    forced to keep the existing root.  Note that we need not worry about
	        					//    the newly created child since it will automatically be deleted when
	        					//    its root is deleted.

	        					numSubRequests++;
	        					
	        					final DataRequestMonitor<String> childPathRm =
	        						new DataRequestMonitor<String>(fSession.getExecutor(), countingRm) {
	        						@Override
	        						protected void handleSuccess() {
	        							// For children that do not map to a real expression (such as f.public)
	        							// GDB returns an empty string.  In this case, we can use another unique
	        							// name, such as the variable name
	        							final boolean fakeChild = (getData().length() == 0);
	        							final String childFullExpression = fakeChild ? child.getVarName() : getData();
	        							
	        							// Now try to see if we already have this variable object in our Map
	        							// Since our map names use the expression, and not the GDB given
	        							// name, we must determine the correct map name from the varName
	        							final VariableObjectId childId = new VariableObjectId();
	        							childId.generateId(childFullExpression, getInternalId());
	        							MIVariableObject childVar = lruVariableList.get(childId);

										if (childVar != null) {
											
											if ((childVar.currentState == STATE_CREATING)
													|| (childVar.currentState == STATE_NOT_CREATED)) {

												// We must wait until the child MIVariableObject is fully created.
												// This might succeed, or fail. If it succeeds, we can reuse it as
												// a child, otherwise we create a new MIVariableObject for the
												// varobj just provided by gdb.
												
												final MIVariableObject monitoredVar = childVar;
												
												// childVar is not fully created so add a RequestMonitor to the queue
												childVar.updatesPending.add(new DataRequestMonitor<Boolean>(fSession.getExecutor(), countingRm) {

													@Override
													protected void handleCompleted() {
														MIVariableObject var = monitoredVar;
														
														if (! isSuccess()) {
															
															// Create a fresh MIVariableObject for this child, using
															// the new varobj provided by gdb.
															var = createChild(childId, childFullExpression, indexInParent, child);
														}
														
														if (fakeChild) {

															addRealChildrenOfFake(var,	exprDmc, realChildren,
																	arrayPosition, countingRm);
														} else {
															// This is a real child
															realChildren[arrayPosition] = new ExpressionInfo[] { var.exprInfo };
															countingRm.done();
														}														
													}
												});
												
											} else if (childVar.currentState == STATE_CREATION_FAILED) {
												// There has been an attempt the create a MIRootVariableObject for a full
												// expression representing a child of a dynamic varobj. Such an attempt always
												// fails. But here we can now create it (see below).
												childVar = null;
											}
											// Note that we must check the root to know if it is out-of-scope.
											// We cannot check the child as it has not be updated and its
											// outOfScope variable is not updated either.
											else if (childVar.getRootToUpdate().isOutOfScope()) {
												childVar.deleteInGdb();
												childVar = null;
											}
										}

	        							if (childVar == null) {
	        								childVar = createChild(childId, childFullExpression, indexInParent, child);
											
											if (fakeChild) {
												
												addRealChildrenOfFake(childVar,	exprDmc, realChildren,
														arrayPosition, countingRm);
											} else {
												// This is a real child
												realChildren[arrayPosition] = new ExpressionInfo[] { childVar.exprInfo };
												countingRm.done();
											}
	        							}
	        						}
	        					};


	        					if (isAccessQualifier(child.getExp())) {	        						
	        						// This is just a qualifier level of C++, so we don't need
	        						// to call -var-info-path-expression for real, but just pretend we did.
	        						childPathRm.setData("");  //$NON-NLS-1$
	        						childPathRm.done();
	        					} else if (isDynamic() || exprInfo.hasDynamicAncestor()) {
	        						// Equivalent to (which can't be implemented): child.hasDynamicAncestor
	        						// The new child has a dynamic ancestor. Such children don't support
	        						// var-info-path-expression. Build the expression ourselves.
    	    						childPathRm.setData(buildChildExpression(exprDmc.getExpression(), child.getExp()));
	        						childPathRm.done();
	        					} else {
	        						// To build the child id, we need the fully qualified expression which we
	        						// can get from -var-info-path-expression starting from GDB 6.7 
	        						fCommandControl.queueCommand(
	        								fCommandFactory.createMIVarInfoPathExpression(getRootToUpdate().getControlDMContext(), child.getVarName()),
	    	        						new DataRequestMonitor<MIVarInfoPathExpressionInfo>(fSession.getExecutor(), childPathRm) {
	        	        						@Override
	        	        						protected void handleCompleted() {
	        	        							if (isSuccess()) {
	        	        								childPathRm.setData(getData().getFullExpression());
	        	        							} else {
	        	        								// If we don't have var-info-path-expression
	        	        								// build the expression ourselves
	        	        								// Note that this does not work well yet
	        	        	    						childPathRm.setData(buildChildExpression(exprDmc.getExpression(), child.getExp()));
	        	        							}
        	        								childPathRm.done();
	        	        						}
	        								});
	        					}
	        				}

	        				countingRm.setDoneCount(numSubRequests);
	        			}
	        		});
		}
		
		/**
		 * Create a child variable of this MIVariableObject and initialize
		 * it from the given MIVar data.
		 * 
		 * @param childId
		 * @param childFullExpression
		 * @param indexInParent
		 * @param childData
		 * 
		 * @return The new child.
		 * 
		 * @since 4.0
		 */
		protected MIVariableObject createChild(final VariableObjectId childId,
				final String childFullExpression, final int indexInParent,
				final MIVar childData) {
			
			MIVariableObject var = createVariableObject(childId, this);
			ExpressionInfo childInfo = new ExpressionInfo(childFullExpression,
					childData.getExp(), childData.isDynamic(), exprInfo,
					indexInParent);

			var.initFrom(childData, childInfo);
			return var;
		}
		
		/**
		 * @param clientLimit
		 * @return True, if the client specified limit requires to check for
		 *         further children.
		 *         
		 * @since 4.0
		 */
		protected boolean requiresAdditionalChildren(int clientLimit) {
			return !isSafeToAskForAllChildren()
					&& (exprInfo.getChildCountLimit() < calculateNewLimit(clientLimit));
		}

		/**
		 * @param newLimit
		 *            The new limit on the number of children being asked for or
		 *            being updated.
		 * 
		 * @return The new limit.
		 * @since 4.0
		 */
		protected int updateLimit(int newLimit) {
			exprInfo.setChildCountLimit(calculateNewLimit(newLimit));
			return exprInfo.getChildCountLimit();
		}
		
		private int calculateNewLimit(int clientLimit) {
			int limit = exprInfo.getChildCountLimit();
			
			if (!isSafeToAskForAllChildren() && clientLimit != IMIExpressions.CHILD_COUNT_LIMIT_UNSPECIFIED) {
				if (limit == IMIExpressions.CHILD_COUNT_LIMIT_UNSPECIFIED) {
					return clientLimit;
				} else if (limit < clientLimit) {
					return clientLimit;
				}
			}
			
			return limit;
		}

		/**
		 * Obtain the children of the given fake child (public, protected, or
		 * private) and insert them into a list of children at a given position.
		 * 
		 * @param fakeChild
		 * @param frameCtxProvider
		 * @param realChildren
		 * @param position
		 * @param rm The request monitor on which to call done upon completion.
		 */
		private void addRealChildrenOfFake(MIVariableObject fakeChild,
				IExpressionDMContext frameCtxProvider,
				final ExpressionInfo[][] realChildren, final int position,
				final RequestMonitor rm) {

			MIExpressionDMC fakeExprCtx = createExpressionCtx(frameCtxProvider,
					fakeChild.exprInfo);

			// This is just a qualifier level of C++, and we must get the
			// children of this child to get the real children
			fakeChild.getChildren(fakeExprCtx,
					IMIExpressions.CHILD_COUNT_LIMIT_UNSPECIFIED,
					new DataRequestMonitor<ChildrenInfo>(
							fSession.getExecutor(), rm) {

						@Override
						protected void handleCompleted() {
							if (isSuccess()) {
								realChildren[position] = getData()
										.getChildren();

								// Fake nodes can never be dynamic varobjs, and because
								// of this, hasMore is always false.
								assert (!getData().hasMore());
							}
							rm.done();
						}
					});
		}

		/**
		 * This method builds a child expression based on its parent's expression.
		 * It is a fallback solution for when GDB doesn't support the var-info-path-expression.
		 * 
		 * Currently, this does not support inherited class such as
		 * class foo : bar {
		 * ...
		 * }
		 * because we'll create foo.bar instead of (bar)foo.
		 */
		private String buildChildExpression(String parentExp, String childExp) {
			// For pointers, the child expression is already contained in the parent,
			// so we must simply prefix with *
		    //  See Bug219179 for more information.
			if (!isDynamic() && !exprInfo.hasDynamicAncestor() && isPointer()) {
				return "*("+parentExp+")"; //$NON-NLS-1$//$NON-NLS-2$
			}

		    return parentExp + "." + childExp; //$NON-NLS-1$
		    // No need for a special case for arrays since we deal with arrays differently
		    // and don't call this method for them
		}

		/**
		 * This method returns the count of children of the variable object
		 * passed as a parameter.
		 * 
		 * @param exprDmc
		 * 
		 * @param numChildrenLimit
		 *            No need to check for more than this number of children.
		 *            However, it is legal to return a higher count if
		 *            we already new from earlier call that there are more
		 *            children.
		 *            
		 * @param rm
		 *            The data request monitor that will hold the count of
		 *            children returned
		 */
		private void getChildrenCount(MIExpressionDMC exprDmc, final int numChildrenLimit,
				final DataRequestMonitor<ChildrenCountInfo> rm) {
			if (isNumChildrenHintTrustworthy()){
				rm.setData(new ChildrenCountInfo(getNumChildrenHint(), hasMore()));
				rm.done();
				return;
			}
			
			getChildren(exprDmc, numChildrenLimit,
					new DataRequestMonitor<ChildrenInfo>(fSession.getExecutor(), rm) {

				@Override
				protected void handleSuccess() {
					rm.setData(new ChildrenCountInfo(getData()
							.getChildren().length, getData().hasMore()));
					rm.done();
				}
			});
		}
		

		
		/** 
		 * This method request the back-end to change the value of the variable object.
		 * 
		 * @param value
		 *            The new value.
		 *  @param formatId
		 *            The format the new value is specified in.
		 * @param rm
		 *            The request monitor to indicate the operation is finished
		 */
		private void writeValue(String value, String formatId, final RequestMonitor rm) {

			// If the variable is a complex structure (including an array), then we cannot write to it
			if (isComplex()) {
				rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.REQUEST_FAILED, 
						"Cannot change the value of a complex expression", null)); //$NON-NLS-1$
				rm.done();
				return;
	        }
			
			// First deal with the format.  For GDB, the way to specify a format is to prefix the value with
			// 0x for hex, 0 for octal etc  So we need to make sure that 'value' has this prefix.
			// Note that there is no way to specify a binary format for GDB up to and including
			// GDB 6.7.1, so we convert 'value' into a decimal format.  
			// If the formatId is NATURAL, we do nothing for now because it is more complicated.
			// For example for a bool, a value of "true" is correct and should be left as is,
			// but for a pointer a value of 16 should be sent to GDB as 0x16.  To figure this out,
			// we need to know the type of the variable, which we don't have yet.
			
			if (formatId.equals(IFormattedValues.HEX_FORMAT)) {
				if (!value.startsWith("0x")) value = "0x" + value;  //$NON-NLS-1$  //$NON-NLS-2$
			}
			else if (formatId.equals(IFormattedValues.OCTAL_FORMAT)) {
				if (!value.startsWith("0")) value = "0" + value;  //$NON-NLS-1$  //$NON-NLS-2$
			}
			else if (formatId.equals(IFormattedValues.BINARY_FORMAT)) {
				// convert from binary to decimal
				if (value.startsWith("0b")) value = value.substring(2, value.length());  //$NON-NLS-1$
				try {
	    			value = Integer.toString(Integer.parseInt(value, 2));
				} catch (NumberFormatException e) {
					rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_HANDLE, 
							"Invalid binary number: " + value, e)); //$NON-NLS-1$
					rm.done();
					return;
				}
				
				formatId = IFormattedValues.DECIMAL_FORMAT;
			}
			else if (formatId.equals(IFormattedValues.DECIMAL_FORMAT)) {
				// nothing to do
			}
			else if (formatId.equals(IFormattedValues.NATURAL_FORMAT)) {
				// we do nothing for now and let the user have put in the proper value
			}
			else {
				rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.INVALID_HANDLE, 
						"Unknown format: " + formatId, null)); //$NON-NLS-1$
				rm.done();
				return;
			}
			
			// If the value has not changed, no need to set it.
			// Return a warning status so that handleSuccess is not called and we don't send
			// an ExpressionChanged event
			if (value.equals(getValue(formatId))) {
				rm.setStatus(new Status(IStatus.WARNING, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.NOT_SUPPORTED, 
						"Setting to the same value of: " + value, null)); //$NON-NLS-1$
				rm.done();
				return;				
			}

			// No need to be in ready state or to lock the object
			fCommandControl.queueCommand(
					fCommandFactory.createMIVarAssign(getRootToUpdate().getControlDMContext(), getGdbName(), value),
					new DataRequestMonitor<MIVarAssignInfo>(fSession.getExecutor(), rm) {
						@Override
						protected void handleSuccess() {
							// We must also mark all variable objects
							// as out-of-date. This is because some variable objects may be affected
							// by this one having changed.
							// e.g., 
							//    int i;  
							//    int* pi = &i;
							// Here, if 'i' is changed by the user, then 'pi' will also change
							// Since there is no way to know this unless we keep track of all addresses,
							// we must mark everything as out-of-date.  See bug 213061
							markAllOutOfDate();
							
							// Useless since we just marked everything as out-of-date
							// resetValues(getData().getValue());
							
							rm.done();
						}
					});
		}

		private boolean isAccessQualifier(String str) {
			return str.equals("private") || str.equals("public") || str.equals("protected");  //$NON-NLS-1$  //$NON-NLS-2$  //$NON-NLS-3$ 
	    }

		/**
		 * @return If true, this variable object can be reported as changed in
		 *         a -var-update MI command.
		 */
		public boolean isModifiable() {
			if (!isComplex() || isDynamic()) return true;
			return false;
		}

		/**
		 * @param exprCtx
		 * @param rm
		 * 
		 * @since 4.0
		 */
		public void create(final IExpressionDMContext exprCtx,
				final RequestMonitor rm) {

			if (currentState == STATE_NOT_CREATED) {

				currentState = STATE_CREATING;
				
				final MIExpressionDMC miExprCtx = (MIExpressionDMC) exprCtx;
				final int indexInParent = miExprCtx.getExpressionInfo().getIndexInParentExpression();

				fCommandControl.queueCommand(fCommandFactory.createMIVarListChildren(getParent().getRootToUpdate().getControlDMContext(),
								getParent().getGdbName(), indexInParent, indexInParent + 1),
						new DataRequestMonitor<MIVarListChildrenInfo>(fSession.getExecutor(), rm) {

							@Override
							protected void handleSuccess() {
								if (getData().getMIVars().length == 1) {
									MIVar miVar = getData().getMIVars()[0];
									
									ExpressionInfo localExprInfo = miExprCtx.getExpressionInfo();
									
									localExprInfo.setDynamic(miVar.isDynamic());
									
									initFrom(miVar, localExprInfo);

									if (exprInfo.isDynamic()
											&& (exprInfo.getChildCountLimit() != IMIExpressions.CHILD_COUNT_LIMIT_UNSPECIFIED)) {
										// Restore the original update range.
										fCommandControl.queueCommand(fCommandFactory.createMIVarSetUpdateRange(
												getRootToUpdate().getControlDMContext(),
												getGdbName(), 0, exprInfo.getChildCountLimit()),
												new DataRequestMonitor<MIInfo>(fSession.getExecutor(), rm));
									} else {
										rm.done();
									}
								} else {
									rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.INTERNAL_ERROR, 
											"Unexpected return on -var-list-children", null)); //$NON-NLS-1$
									rm.done();
								}
							}
						});
			} else {
				assert false;
			}
		}

		private void initFrom(MIVar miVar, ExpressionInfo newExprInfo) {
			// Possible Optimization in GDB: In -var-list-children, the has_more
			// field is missing for the children. As a workaround, we assume that
			// if numChild is 0 and has_more is omitted, we have more children.
			boolean newHasMore = miVar.hasMore()
					|| (miVar.isDynamic() && (miVar.getNumChild() == 0));
			
			setGdbName(miVar.getVarName());
			setDisplayHint(miVar.getDisplayHint());
			setExpressionData(
					newExprInfo,
					miVar.getType(),
			        miVar.getNumChild(),
					newHasMore);

			// This will replace any existing entry
			lruVariableList.put(getInternalId(), this);

			// Is this new child a modifiable descendant of the root?
			if (isModifiable()) {
				getRootToUpdate().addModifiableDescendant(miVar.getVarName(), this);
			}
		}
	}
	
	/**
	 * Method to allow to override the MIVariableObject creation
	 * 
     * @since 3.0
     */
	protected MIVariableObject createVariableObject(VariableObjectId id, MIVariableObject parentObj) {
	    return new MIVariableObject(id, parentObj);
	}

	/**
	 * Method to allow to override the MIVariableObject creation
	 * 
     * @since 4.0
	 */
	protected MIVariableObject createVariableObject(VariableObjectId id,
			MIVariableObject parentObj, boolean needsCreation) {
		return new MIVariableObject(id, parentObj, needsCreation);
	}

	/**
     * @since 3.0
     */
	public class MIRootVariableObject extends MIVariableObject {

		// The control context within which this variable object was created
		// It only needs to be stored in the Root VarObj since any children
		// will have the same control context
	    private ICommandControlDMContext fControlContext = null;
	    
		private boolean fOutOfDate = false;
		
		/**
	     * A modifiable descendant is any variable object that is a descendant and
	     * for which the value (leaf variable objects and dynamic variable objects)
	     * or number of children (dynamic variable objects) can change.
		 */
		private Map<String, MIVariableObject> modifiableDescendants;

		public MIRootVariableObject(VariableObjectId id) {
			super(id, null);
			currentState = STATE_NOT_CREATED;
			modifiableDescendants = new HashMap<String, MIVariableObject>();
		}

		public ICommandControlDMContext getControlDMContext() { return fControlContext; }

		public boolean isUpdating() { return currentState == STATE_UPDATING; }
        
		public void setOutOfDate(boolean outOfDate) { fOutOfDate = outOfDate; }
		
		public boolean getOutOfDate() { return fOutOfDate; }
		
		// Remember that we must add ourself as a modifiable descendant if our value can change
		public void addModifiableDescendant(String gdbName, MIVariableObject descendant) {
			modifiableDescendants.put(gdbName, descendant);
		}
		
		/**
		 * @since 4.0
		 */
		public void processChanges(MIVarChange[] updates, RequestMonitor rm) {
			CountingRequestMonitor countingRm = new CountingRequestMonitor(fSession.getExecutor(), rm);
			countingRm.setDoneCount(updates.length);
			
			for (MIVarChange update : updates) {
				MIVariableObject descendant = modifiableDescendants.get(update.getVarName());
				
				// Descendant should never be null, but just to be safe
				if (descendant != null) {
					descendant.processChange(update, countingRm);
				} else {
					// This can for instance happen if a child MIVariableObject is deleted
					// from the cache. The corresponding varobj in gdb does still exist,
					// and if it is changed, gdb reports it as changed in -var-update.
					countingRm.done();
				}
			}
		}
		
		@Override
		public void create(final IExpressionDMContext exprCtx,
                           final RequestMonitor rm) {

			if (currentState == STATE_NOT_CREATED) {
				
				currentState = STATE_CREATING;
				fControlContext = DMContexts.getAncestorOfType(exprCtx, ICommandControlDMContext.class);

				fCommandControl.queueCommand(
						fCommandFactory.createMIVarCreate(exprCtx, exprCtx.getExpression()), 
						new DataRequestMonitor<MIVarCreateInfo>(fSession.getExecutor(), rm) {
							@Override
							protected void handleCompleted() {
								if (isSuccess()) {
									setGdbName(getData().getName());
									setDisplayHint(getData().getDisplayHint());
									
									MIExpressionDMC miExprCtx = (MIExpressionDMC) exprCtx;
									ExpressionInfo localExprInfo = miExprCtx
											.getExpressionInfo();
									
									localExprInfo.setDynamic(getData()
											.isDynamic());
									localExprInfo.setParent(null);
									localExprInfo.setIndexInParent(-1);
									
									setExpressionData(
											localExprInfo,
											getData().getType(),
											getData().getNumChildren(),
											getData().hasMore());
									
									// Store the value returned at create (available in GDB 6.7)
									// Don't store if it is an array, since we want to show
									// the address of an array as its value
									if (getData().getValue() != null && !isArray()) { 
										setValue(getCurrentFormat(), getData().getValue());
									}
									
									// If we are modifiable, we should be in our modifiable list
									if (isModifiable()) {
										addModifiableDescendant(getData().getName(), MIRootVariableObject.this);
									}

									if (localExprInfo.isDynamic()
											&& (localExprInfo.getChildCountLimit() != IMIExpressions.CHILD_COUNT_LIMIT_UNSPECIFIED)) {
										
										// Restore the original update range.
										fCommandControl.queueCommand(fCommandFactory.createMIVarSetUpdateRange(
												getRootToUpdate().getControlDMContext(),getGdbName(),
												0,localExprInfo.getChildCountLimit()),
												new DataRequestMonitor<MIInfo>(fSession.getExecutor(), rm));
									} else {
										rm.done();
									}											 	        						
								} else {
									rm.setStatus(getStatus());
									rm.done();
								}								
							}
						});
			} else {
				assert false;
			}
		}
		
		@Override
		public void update(final DataRequestMonitor<Boolean> rm) {

			if (isOutOfScope()) {
		    	rm.setData(false);
				rm.done();
			} else if (currentState != STATE_READY) {
				// Object is not fully created or is being updated
				// so add RequestMonitor to pending queue
				updatesPending.add(rm);
			} else if (getOutOfDate() == false) {
				rm.setData(false);
				rm.done();
			} else {
				// Object needs to be updated in the back-end
				currentState = STATE_UPDATING;

				// In GDB, var-update will only report a change if -var-evaluate-expression has
				// changed -- in the current format--.  This means that situations like
				// double z = 1.2;
				// z = 1.4;
				// Will not report a change if the format is anything else than natural.
				// This is because 1.2 and 1.4 are both printed as 1, 0x1, etc
				// Since we cache the values of every format, we must know if -any- format has
				// changed, not just the current one.
				// To solve this, we always do an update in the natural format; I am not aware
				// of any case where the natural format would stay the same, but another format
				// would change.  However, since a var-update update all children as well,
			    // we must make sure these children are also in the natural format
				// The simplest way to do this is that whenever we change the format
				// of a variable object, we immediately set it back to natural with a second
				// var-set-format command.  This is done in the getValue() method
				fCommandControl.queueCommand(
						fCommandFactory.createMIVarUpdate(getRootToUpdate().getControlDMContext(), getGdbName()),
						new DataRequestMonitor<MIVarUpdateInfo>(fSession.getExecutor(), rm) {
							@Override
							protected void handleCompleted() {
								if (isSuccess()) {
									setOutOfDate(false);
									
									MIVarChange[] changes = getData().getMIVarChanges();
									if (changes.length > 0 && changes[0].isInScope() == false) {
										// Object is out-of-scope
										currentState = STATE_READY;

										outOfScope = true;
										
										// We can delete this root in GDB right away.  This is safe, even
									 	// if the root has children, because they are also out-of-scope.
										// We -must- also remove this entry from our LRU.  If we don't
										// we can end-up with a race condition that create this object
										// twice, or have an infinite loop while never re-creating the object.
										// The can happen if we update a child first then we request 
										// the root later,
										lruVariableList.remove(getInternalId());

										rm.setData(true);
										rm.done();
										
										while (updatesPending.size() > 0) {
											DataRequestMonitor<Boolean> pendingRm = updatesPending.poll();
											pendingRm.setData(false);
											pendingRm.done();
										}
									} else {
										// The root object is now up-to-date, we must parse the changes, if any.
										processChanges(changes, new RequestMonitor(fSession.getExecutor(), rm) {
											@Override
											protected void handleCompleted() {
												currentState = STATE_READY;

												// We only mark this root as updated in our list if it is in-scope.
												// For out-of-scope object, we don't ever need to re-update them so
												// we don't need to add them to this list.
												rootVariableUpdated(MIRootVariableObject.this);

												if (isSuccess()) {
													rm.setData(false);
												} else {
													rm.setStatus(getStatus());
												}
												rm.done();

												while (updatesPending.size() > 0) {
													DataRequestMonitor<Boolean> pendingRm = updatesPending.poll();
													if (isSuccess()) {
														pendingRm.setData(false);
													} else {
														pendingRm.setStatus(getStatus());
													}
													pendingRm.done();
												}
											};
										});
									}									
								} else {
									// We were not able to update for some reason
									currentState = STATE_READY;

									rm.setData(false);
									rm.done();

									while (updatesPending.size() > 0) {
										DataRequestMonitor<Boolean> pendingRm = updatesPending.poll();
										pendingRm.setStatus(getStatus());
										pendingRm.done();
									}
								}
							}
						});
		    }
		}

		/**
		 * This method request the back-end to delete a variable object.
		 * We check if the GDB name has been filled to confirm that this object
		 * was actually successfully created on the back-end.
		 * Only root variable objects are deleted, while children are left in GDB 
		 * to be deleted automatically when their root is deleted.
		 */
		@Override
		public void deleteInGdb() {			
		    if (getGdbName() != null) {
	    		fCommandControl.queueCommand(
	    				fCommandFactory.createMIVarDelete(getRootToUpdate().getControlDMContext(), getGdbName()),
	    				new DataRequestMonitor<MIVarDeleteInfo>(fSession.getExecutor(), null));
		        // Nothing to do in the requestMonitor, since the object was already
		        // removed from our list before calling this method.

		    	// Set the GDB name to null to make sure we don't attempt to delete
		    	// this variable a second time.  This can happen if the LRU triggers
		    	// an automatic delete.
		    	setGdbName(null);
		    } else {
		        // Variable was never created or was already deleted, no need to do anything.
		    }
		    
			super.deleteInGdb();
		}
	}
	
    /**
 	 * Method to allow to override the MIRootVariableObject creation.
	 *
     * @since 3.0
     */
	protected MIRootVariableObject createRootVariableObject(VariableObjectId id) {
	    return new MIRootVariableObject(id);
	}
	
	/**
	 * This class represents an unique identifier for a variable object.
	 * 
	 * The following must be considered to obtain a unique name:
	 *     - the expression itself
	 *     - the execution context 
	 *     - relative depth of frame based on the frame context and the total depth of the stack
	 *     
	 * Note that if no frameContext is specified (only Execution, or even only Container), which can
	 * characterize a global variable for example, we will only use the available information.
	 * 
	 * @since 3.0
	 */
	public class VariableObjectId {
		// We don't use the expression context because it is not safe to compare them
		// See bug 187718.  So we store the expression itself, and it's parent execution context.
		String fExpression = null;
		IExecutionDMContext fExecContext = null;
		// We need the depth of the frame.  The frame level is not sufficient because 
        // the same frame will have a different level based on the current depth of the stack 
		Integer fFrameId = null;
		
		public VariableObjectId() {
		}
		
		@Override
		public boolean equals(Object other) {
			if (other instanceof VariableObjectId) {
				VariableObjectId otherId = (VariableObjectId) other;
				return (fExpression == null ? otherId.fExpression == null : fExpression.equals(otherId.fExpression)) &&
    				(fExecContext == null ? otherId.fExecContext == null : fExecContext.equals(otherId.fExecContext)) &&
                    (fFrameId == null ? otherId.fFrameId == null : fFrameId.equals(otherId.fFrameId));
			}
			return false;
		}

		@Override
		public int hashCode() {
			return (fExpression == null ? 0 : fExpression.hashCode()) + 
		           (fExecContext == null ? 0 : fExecContext.hashCode()) +
			       (fFrameId  == null ? 0 : fFrameId.hashCode());
		}

		public void generateId(IExpressionDMContext exprCtx, final RequestMonitor rm) {
			fExpression = exprCtx.getExpression();

			fExecContext = DMContexts.getAncestorOfType(exprCtx, IExecutionDMContext.class);
			if (fExecContext == null) {
				rm.done();
				return;
			}
			
			final IFrameDMContext frameCtx = DMContexts.getAncestorOfType(exprCtx, IFrameDMContext.class);
			if (frameCtx == null) {
				rm.done();
				return;
			}

			// We need the current stack depth to be able to make a unique and reproducible name
			// for this expression.  This is pretty efficient since the stackDepth will be retrieved
			// from the StackService command cache after the first time.
			fStackService.getStackDepth(
					fExecContext, 0,
					new DataRequestMonitor<Integer>(fSession.getExecutor(), rm) {
						@Override
						protected void handleSuccess() {
							fFrameId = new Integer(getData() - frameCtx.getLevel());
							rm.done();
						}
					});
		}
		
		public void generateId(String childFullExp, VariableObjectId parentId) {
			// The execution context and the frame depth are the same as the parent
			fExecContext = parentId.fExecContext;
			fFrameId = parentId.fFrameId;
			// The expression here must be the one that is part of IExpressionContext for this child
			// This will allow us to find a variable object directly
			fExpression = childFullExp;
		}
	}
	
    /**
 	 * Method to allow to override the VariableObjectId creation.
	 *
     * @since 3.0
     */
	protected VariableObjectId createVariableObjectId() {
	    return new VariableObjectId();
	}

	
	/**
	 * This is the real work horse of managing our objects. Not only must every
	 * value be unique to get inserted, this also creates an LRU (least recently
	 * used). When we hit our size limitation, the LRUsed will be removed to
	 * make space. Removing means that a GDB request to delete the object is
	 * generated.  We must also take into consideration the fact that GDB will
	 * automatically delete children of a variable object, when deleting the parent
	 * variable object.  Our solution to that is to tweak the LRU to make sure that 
	 * children are always older than their parents, to guarantee the children will 
	 * always be delete before their parents.
	 * 
	 */
	private class LRUVariableCache extends LinkedHashMap<VariableObjectId, MIVariableObject> {
		public static final long serialVersionUID = 0;

		// Maximum allowed concurrent variables
		private static final int MAX_VARIABLE_LIST = 1000;
		
		public LRUVariableCache() {
			super(0,     // Initial load capacity
				  0.75f, // Load factor as defined in JAVA 1.5
				  true); // Order is dictated by access, not insertion
		}

		// We never remove doing put operations.  Instead, we rely on our get() operations
		// to trigger the remove.  See bug 200897
		@Override
		public boolean removeEldestEntry(Map.Entry<VariableObjectId, MIVariableObject> eldest) {
		    return false;
		}

		@Override
		public MIVariableObject get(Object key) {
			MIVariableObject varObj = super.get(key);
		    touchAncestors(varObj);
		    
		    // If we're over our max size, attempt to remove eldest entry.
		    if (size() > MAX_VARIABLE_LIST) {
		    	Map.Entry<VariableObjectId, MIVariableObject> eldest = entrySet().iterator().next();
		    	// First make sure we are not deleting ourselves!
		    	if (!eldest.getValue().equals(varObj)) {
		    		if (eldest.getValue().currentState == MIVariableObject.STATE_READY) {
		    			remove(eldest.getKey());
		    		}
		    	}
		    }
		    return varObj;
		}
		
		private void touchAncestors(MIVariableObject varObj) {
			while (varObj != null) {
				varObj = varObj.getParent();
				// If there is a parent, touch it
				if (varObj != null) super.get(varObj.getInternalId());
			}
		}

        @Override
		public MIVariableObject put(VariableObjectId key, MIVariableObject varObj) {
        	MIVariableObject retVal = super.put(key, varObj);

            // Touch all parents of this element so as
            // to guarantee they are not deleted before their children.
            touchAncestors(varObj);

            return retVal;
        }

		@Override
		public MIVariableObject remove(Object key) {
			MIVariableObject varObj = super.remove(key);
			if (varObj != null) {
				varObj.deleteInGdb();
			}
			return varObj; 
		}
	}

    /**
     * @since 3.0
     */
    private static final GDBTypeParser fGDBTypeParser = new GDBTypeParser();
    
	private final DsfSession fSession;
	
	/** Provides access to the GDB/MI back-end */
	private final ICommandControl fCommandControl;
	private CommandFactory fCommandFactory;

	// The stack service needs to be used to get information such
	// as the stack depth to differentiate between expressions that have the
	// same name but refer to a different context
	private final IStack fStackService;
	private IExpressions fExpressionService;

	// Typically, there will only be one listener, since only the ExpressionService will use this class
    private final List<ICommandListener> fCommandProcessors = new ArrayList<ICommandListener>();
    
	/** Our least recently used cache */
	private final LRUVariableCache lruVariableList;
	
	/** The list of root variable objects that have been updated */
	private final LinkedList<MIRootVariableObject> updatedRootList = new LinkedList<MIRootVariableObject>();

	/**
	 * MIVariableManager constructor
	 * 
	 * @param session
	 *            The session we are working with
	 * @param tracker
	 *            The service tracker that can be used to find other services
	 */
	public MIVariableManager(DsfSession session, DsfServicesTracker tracker) {
	    fSession = session;
		lruVariableList = new LRUVariableCache();
		fCommandControl = tracker.getService(ICommandControl.class);
		fStackService  = tracker.getService(IStack.class);
		fExpressionService = tracker.getService(IExpressions.class);
		fCommandFactory = tracker.getService(IMICommandControl.class).getCommandFactory();

		// Register to receive service events for this session.
        fSession.addServiceEventListener(this, null);
	}

	public void dispose() {
    	fSession.removeServiceEventListener(this);
	}

    /**
     * @since 3.0
     */
	protected DsfSession getSession() {
	    return fSession;
	}
	
	/**
     * @since 3.0
     */
	protected ICommandControl getCommandControl() {
	    return fCommandControl;
	}
	
    /**
     * @since 3.0
     */
	protected void rootVariableUpdated(MIRootVariableObject rootObj) {
	    updatedRootList.add(rootObj);
	}
	
    /**
     * @since 3.0
     */
	protected Map<VariableObjectId, MIVariableObject> getLRUCache() {
		return lruVariableList;
	}
	
	/** 
	 * This method returns a variable object based on the specified
	 * ExpressionDMC, creating it in GDB if it was not created already.
	 * The method guarantees that the variable is finished creating and that
	 * is it not out-of-scope.
	 * 
	 * @param exprCtx
	 *            The expression context to which the variable object is applied to.
	 * 
	 * @param rm
	 *            The data request monitor that will contain the requested variable object
	 */
	private void getVariable(final IExpressionDMContext exprCtx,
                             final DataRequestMonitor<MIVariableObject> rm) {
		// Generate an id for this expression so that we can determine if we already
		// have a variable object tracking it.  If we don't we'll need to create one.
		final VariableObjectId id = createVariableObjectId();
		id.generateId(
				exprCtx,
				new RequestMonitor(fSession.getExecutor(), rm) {
					@Override
					protected void handleSuccess() {
					    getVariable(id, exprCtx, rm);
					}
				});
	}

	private void getVariable(final VariableObjectId id,
			                 final IExpressionDMContext exprCtx,
			                 final DataRequestMonitor<MIVariableObject> rm) {

		final MIVariableObject varObj = lruVariableList.get(id);

		if (varObj == null) {
			// We do not have this varObject, so we create it
			createVariable(id, exprCtx, rm);
		} else {
			// We have found a varObject, but it may not be updated yet.
			// Updating the object will also tell us if it is out-of-scope
			// and if we should re-create it.
			varObj.update(new DataRequestMonitor<Boolean>(fSession.getExecutor(), rm) {
				@Override
				protected void handleSuccess() {
					
					boolean shouldCreateNew = getData().booleanValue();
					
					if (varObj.isOutOfScope()) {
					    // The variable object is out-of-scope and we
						// should not use it.
						if (shouldCreateNew) {
							/*
							 * It may happen that when accessing a varObject we find it to be
							 * out-of-scope.  The expression for which we are trying to access a varObject
							 * could still be valid, and therefore we should try to create a new varObject for
							 * that expression.  This can happen for example if two methods use the same name
							 * for a variable. In the case when we find that a varObject is out-of-scope (when
							 * its root is out-of-scope) the following should be done:
						     *
						     * - create a new varObject for the expression (as a root varObject) and insert it
						     * in the LRU.  Make sure that when creating children of this new varObject, they 
						     * will replace any old children with the same name in the LRU (this is ok since the 
						     * children being replaced are also out-of-scope).
							 */
							
						    createVariable(id, exprCtx, rm);
						} else {
							// Just request the variable object again
							// We must use this call to handle the fact that
							// the new object might be in the middle of being
							// created.
							getVariable(id, exprCtx, rm);
						}
					} else {
						// The variable object is up-to-date and valid

						MIExpressionDMC miExprCtx = (MIExpressionDMC) exprCtx;
						ExpressionInfo ctxExprInfo = miExprCtx.getExpressionInfo();
						ExpressionInfo varExprInfo = varObj.getExpressionInfo();
						if (ctxExprInfo != varExprInfo) {
							// exprCtrx could just be created via IExpressions.createExpression,
							// and thus the parent-child relationship is not yet set.
							miExprCtx.setExpressionInfo(varExprInfo);
						}
						
						rm.setData(varObj);
						rm.done();
					}
				}
			});
		}
	}

	
	
	/**
	 * This method creates a variable object in GDB.
	 */
	private void createVariable(final VariableObjectId id, 
			                    final IExpressionDMContext exprCtx,
                                final DataRequestMonitor<MIVariableObject> rm) {

		// If we have a dynamic variable object as ancestor, we cannot use
		// -var-create, so we must create ourselves creating the root, and
		// then use -var-list-children for each further ancestor.
		final MIExpressionDMC miExprCtx =(MIExpressionDMC) exprCtx;
		final ExpressionInfo parentInfo = miExprCtx.getExpressionInfo().getParent();
				
		if ((parentInfo != null) && miExprCtx.getExpressionInfo().hasDynamicAncestor()) {
			
			// Need to set parent when it is known.
			final MIVariableObject newVarObj = createVariableObject(id, null, true);

			// We must put this object in our map right away, in case it is 
			// requested again, before it completes its creation.
			// Note that this will replace any old entry with the same id.
			lruVariableList.put(id, newVarObj);

			MIExpressionDMC parentExprCtx = createExpressionCtx(exprCtx,
					parentInfo);
			
			getVariable(parentExprCtx,
					new DataRequestMonitor<MIVariableObject>(
							fSession.getExecutor(), rm) {
				
				@Override
				protected void handleCompleted() {
					
					if (isSuccess()) {
						final MIVariableObject parentObj = getData();
						newVarObj.setParent(parentObj);
						
						newVarObj.create(miExprCtx,	new RequestMonitor(fSession.getExecutor(), rm) {

							@Override
							protected void handleCompleted() {
								if (isSuccess()) {									
									rm.setData(newVarObj);
									newVarObj.creationCompleted(true);
								} else {
									// Object was not created, remove it from our list
									lruVariableList.remove(id);
									// We avoid this race condition by sending the notifications _after_ removing 
									// the object from the LRU, to avoid any new requests being queue.
									// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=231655
									newVarObj.creationCompleted(false);
									rm.setStatus(getStatus());									
								}
								rm.done();
							}
						});
					} else {
						// Object was not created, remove it from our list
						lruVariableList.remove(id);
						// We avoid this race condition by sending the notifications _after_ removing 
						// the object from the LRU, to avoid any new requests being queue.
						// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=231655
						newVarObj.creationCompleted(false);
						
						rm.setStatus(getStatus());
						rm.done();
					}
				}
			});
			
			return;
		}
		
		// Variable objects that are created directly like this, are considered ROOT variable objects
		// in comparison to variable objects that are children of other variable objects.
		final MIRootVariableObject newVarObj = createRootVariableObject(id);
		
		// We must put this object in our map right away, in case it is 
		// requested again, before it completes its creation.
		// Note that this will replace any old entry with the same id.
		lruVariableList.put(id, newVarObj);
		
		newVarObj.create(exprCtx, new RequestMonitor(fSession.getExecutor(), rm) {
			@Override
			protected void handleCompleted() {
				if (isSuccess()) {
					// Also store the object as a varObj that is up-to-date
    				rootVariableUpdated(newVarObj);	
					// VarObj can now be used by others
					newVarObj.creationCompleted(true);

					rm.setData(newVarObj);
					rm.done();
				} else {
					// Object was not created, remove it from our list
					lruVariableList.remove(id);
					// Tell any waiting monitors that creation failed.
					// It is important to do this call after we have removed the id
					// from our LRU; this is to avoid the following:
					//   The same varObj is requested before it was removed from the LRU
					//   but after we called creationCompleted().  
					//   In this case, the request for this varObj would be queued, but  
					//   since creationCompleted() already sent the notifications
					//   the newly queue request will never get serviced.
					// We avoid this race condition by sending the notifications _after_ removing 
					// the object from the LRU, to avoid any new requests being queue.
					// See https://bugs.eclipse.org/bugs/show_bug.cgi?id=231655
					newVarObj.creationCompleted(false);

					rm.setStatus(getStatus());
					rm.done();
				}
			}
		});
	}

	private MIExpressionDMC createExpressionCtx(
			final IExpressionDMContext frameCtxProvider,
			final ExpressionInfo exprInfo) {
		
		IFrameDMContext frameCtx = DMContexts.getAncestorOfType(frameCtxProvider, IFrameDMContext.class);

		MIExpressionDMC exprCtx = new MIExpressionDMC(
				frameCtxProvider.getSessionId(), exprInfo, frameCtx);

		return exprCtx;
	}

	/** 
	 * This method requests the back-end to change the value of an expression.
	 * 
	 * @param ctx
	 *            The context of the expression we want to change
	 * @param expressionValue
	 *            The new value of the expression
	 * @param formatId
	 *            The format in which the expressionValue is specified in
	 * @param rm
	 *            The request monitor to indicate the operation is finished
	 */
	// This method can be called directly from the ExpressionService, since it cannot be cached
	public void writeValue(final IExpressionDMContext ctx, final String expressionValue, 
			final String formatId, final RequestMonitor rm) {
		
		getVariable(
				ctx, 
				new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), rm) {
					@Override
					protected void handleSuccess() {
						getData().writeValue(expressionValue, formatId, rm);
					}
				});
	}

    public <V extends ICommandResult> ICommandToken queueCommand(final ICommand<V> command, DataRequestMonitor<V> rm) {
    	
        final ICommandToken token = new ICommandToken() {
            public ICommand<? extends ICommandResult> getCommand() {
                return command;
            }
        };
        
    	// The MIVariableManager does not buffer commands itself, but sends them directly to the real
    	// MICommandControl service.  Therefore, we must immediately tell our calling cache that the command
    	// has been sent, since we can never cancel it.  Note that this removes any option of coalescing, 
    	// but coalescing was not applicable to variableObjects anyway.
    	processCommandSent(token);
    	
    	if (command instanceof ExprMetaGetVar) {
            @SuppressWarnings("unchecked")            
    		final DataRequestMonitor<ExprMetaGetVarInfo> drm = (DataRequestMonitor<ExprMetaGetVarInfo>)rm;
            final MIExpressionDMC exprCtx = (MIExpressionDMC)(command.getContext());
            
            getVariable(
            		exprCtx, 
            		new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), drm) {
            			@Override
            			protected void handleSuccess() {
            				final MIVariableObject varObj = getData();
            				
							if (varObj.isDynamic() && (varObj.getNumChildrenHint() == 0) && varObj.hasMore()) {
								// Bug/feature in gdb? MI sometimes reports 0 number of children, and hasMore=1.
								// This however, is not a safe indicator that there are children,
								// unless there has been a -var-list-children before.
								// The following call will
								// 1) try to fetch at least one child, because isNumChildrenHintTrustworthy()
								//    returns false for this combination
								// 2) result in the desired -var-list-children, unless it has happened already
								// such that the number of children can at least be used to reliably
								// tell whether there are children or not.
								varObj.getChildrenCount(
										exprCtx, 1,
										new DataRequestMonitor<ChildrenCountInfo>(fSession.getExecutor(), drm) {
											@Override
											protected void handleSuccess() {
												drm.setData(
														new ExprMetaGetVarInfo(
																exprCtx.getRelativeExpression(),
																varObj.isSafeToAskForAllChildren(),
																getData().getChildrenCount(),
																varObj.getType(),
																varObj.getGDBType(),
																!varObj.isComplex(),
																varObj.getDisplayHint().isCollectionHint()));
												drm.done();
												processCommandDone(token, drm.getData());
											}
										});
							} else {
								drm.setData(
										new ExprMetaGetVarInfo(
												exprCtx.getRelativeExpression(),
												varObj.isSafeToAskForAllChildren(),
												// We only provide the hint here.  It will be used for hasChildren()
												// To obtain the correct number of children, the user should use
												// IExpressions#getSubExpressionCount()
												varObj.getNumChildrenHint(),
												varObj.getType(),
												varObj.getGDBType(),
												!varObj.isComplex(),
												varObj.getDisplayHint().isCollectionHint()));
								drm.done();
								processCommandDone(token, drm.getData());
							}
            			}
            		});
        } else if (command instanceof ExprMetaGetAttributes) {
            @SuppressWarnings("unchecked")        
            final DataRequestMonitor<ExprMetaGetAttributesInfo> drm = (DataRequestMonitor<ExprMetaGetAttributesInfo>)rm;
            final IExpressionDMContext exprCtx = (IExpressionDMContext)(command.getContext());
 
            getVariable(
                    exprCtx, 
                    new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), drm) {
                        @Override
                        protected void handleSuccess() {
                            getData().getAttributes(
                                    new DataRequestMonitor<Boolean>(fSession.getExecutor(), drm) {
                                        @Override
                                        protected void handleSuccess() {
                                            drm.setData(new ExprMetaGetAttributesInfo(getData()));
                                            drm.done();
                                            processCommandDone(token, drm.getData());
                                        }
                                    }); 
                        }
                    });
            

    	} else if (command instanceof ExprMetaGetValue) {
            @SuppressWarnings("unchecked")            
    		final DataRequestMonitor<ExprMetaGetValueInfo> drm = (DataRequestMonitor<ExprMetaGetValueInfo>)rm;
            final FormattedValueDMContext valueCtx = (FormattedValueDMContext)(command.getContext());
            final IExpressionDMContext exprCtx = DMContexts.getAncestorOfType(valueCtx, IExpressionDMContext.class);
            
    		getVariable(
    				exprCtx, 
    			    new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), drm) {
    	                @Override
    	                protected void handleSuccess() {
    	                	getData().getValue(
    	                			valueCtx, 
    	                			new DataRequestMonitor<FormattedValueDMData>(fSession.getExecutor(), drm) {
    	                    			@Override
    	                    			protected void handleSuccess() {
    	                    				drm.setData(
    	                    						new ExprMetaGetValueInfo(getData().getFormattedValue()));
    	                    				drm.done();
    	                                    processCommandDone(token, drm.getData());
    	                    			}
    	                    		});
    	                }
    			    });
    	
    	} else if (command instanceof ExprMetaGetChildren) {
            @SuppressWarnings("unchecked")            
    		final DataRequestMonitor<ExprMetaGetChildrenInfo> drm = (DataRequestMonitor<ExprMetaGetChildrenInfo>)rm;
            final MIExpressionDMC exprCtx = (MIExpressionDMC)(command.getContext());
            
    		getVariable(
    				exprCtx, 
    				new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), drm) {
    					@Override
    					protected void handleSuccess() {
    						getData().getChildren(exprCtx, ((ExprMetaGetChildren)command).getNumChildLimit(),
    								new DataRequestMonitor<ChildrenInfo>(fSession.getExecutor(), drm) {
    									@Override
    									protected void handleSuccess() {
    										drm.setData(new ExprMetaGetChildrenInfo(
    												getData().getChildren()));
    										drm.done();
    										processCommandDone(token, drm.getData());
    									}
    								});
    					}
    				});
    	
    	} else if (command instanceof ExprMetaGetChildCount) {
            @SuppressWarnings("unchecked")            
    		final DataRequestMonitor<ExprMetaGetChildCountInfo> drm = (DataRequestMonitor<ExprMetaGetChildCountInfo>)rm;
            final MIExpressionDMC exprCtx = (MIExpressionDMC)(command.getContext());

    		getVariable(
    				exprCtx, 
    				new DataRequestMonitor<MIVariableObject>(fSession.getExecutor(), drm) {
    					@Override
    					protected void handleSuccess() {
    						
    						getData().getChildrenCount(
    								exprCtx, ((ExprMetaGetChildCount) command).getNumChildLimit(),
    								new DataRequestMonitor<ChildrenCountInfo>(fSession.getExecutor(), drm) {
    									@Override
    									protected void handleSuccess() {
									drm.setData(new ExprMetaGetChildCountInfo(
											getData().getChildrenCount()));
    										drm.done();
    			                            processCommandDone(token, drm.getData());
    									}
    								}); 
    					}
    				});
    	
    	} else if (command instanceof MIDataEvaluateExpression<?>) {
    		// This does not use the variable objects but sends the command directly to the back-end
			fCommandControl.queueCommand(command, rm);
    	} else {
			rm.setStatus(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, IDsfStatusConstants.INTERNAL_ERROR, 
					"Unexpected Expression Meta command", null)); //$NON-NLS-1$
			rm.done();
    	}
    	return token;
    }
    
    /*
     *   This is the command which allows the user to retract a previously issued command. The
     *   state of the command  is that it is in the waiting queue  and has not yet been handed 
     *   to the back-end yet.
     *   
     * (non-Javadoc)
     * @see org.eclipse.cdt.dsf.mi.service.command.IDebuggerControl#removeCommand(org.eclipse.cdt.dsf.mi.service.command.commands.ICommand)
     */
    public void removeCommand(ICommandToken token) {
    	// It is impossible to remove a command from the MIVariableManager.
    	// This should never be called, if we did things right.
    	assert false;
    }
   
    /*
     *  This command allows the user to try and cancel commands  which have been handed off to the 
     *  back-end. Some back-ends support this with extended GDB/MI commands. If the support is there
     *  then we will attempt it.  Because of the bidirectional nature of the GDB/MI command stream
     *  there is no guarantee that this will work. The response to the command could be on its way
     *  back when the cancel command is being issued.
     *  
     * (non-Javadoc)
     * @see org.eclipse.cdt.dsf.mi.service.command.IDebuggerControl#cancelCommand(org.eclipse.cdt.dsf.mi.service.command.commands.ICommand)
     */
    public void addCommandListener(ICommandListener processor) { fCommandProcessors.add(processor); }
    public void removeCommandListener(ICommandListener processor) { fCommandProcessors.remove(processor); }    
    public void addEventListener(IEventListener processor) {}
    public void removeEventListener(IEventListener processor) {}

    
    private void processCommandSent(ICommandToken token) {
        for (ICommandListener processor : fCommandProcessors) {
            processor.commandSent(token);
        }
    }

    private void processCommandDone(ICommandToken token, ICommandResult result) {
        for (ICommandListener processor : fCommandProcessors) {
            processor.commandDone(token, result);
        }
    }
    
    /**
     * @since 1.1
     */
    public void markAllOutOfDate() {
    	MIRootVariableObject root;
    	while ((root = updatedRootList.poll()) != null) {
    		root.setOutOfDate(true);
    	}       
    }

    @DsfServiceEventHandler 
    public void eventDispatched(IRunControl.IResumedDMEvent e) {
    	// Program has resumed, all variable objects need to be updated.
    	// Since only roots can actually be updated in GDB, we only need
    	// to deal with those.  Also, to optimize this operation, we have
    	// a list of all roots that have been updated, so we only have to
    	// set those to needing to be updated.
    	markAllOutOfDate();
    }
    
    @DsfServiceEventHandler 
    public void eventDispatched(IRunControl.ISuspendedDMEvent e) {
    }
    
    @DsfServiceEventHandler 
    public void eventDispatched(IMemoryChangedEvent e) {
    	// Some memory has changed.  We currently do not know the address
    	// of each of our variable objects, so there is no way to know
    	// which one is affected.  Mark them all as out of date.
    	// The views will fully refresh on a MemoryChangedEvent
    	markAllOutOfDate();
    }
    
    /**
	 * @since 3.0
	 */
    @DsfServiceEventHandler 
    public void eventDispatched(ITraceRecordSelectedChangedDMEvent e) {
    	// We have a big limitation with tracepoints!
    	// GDB usually only reports a depth of 1, for every trace record, no
    	// matter where it occurred.  This means that our naming scheme for VariableObjectId 
    	// fails miserably because all objects will have the same depth and we will confuse
    	// them.  Until we find a good solution, we have to clear our entire list of
    	// of variable objects (and delete them in GDB to avoid having too many).
    	Iterator<Map.Entry<VariableObjectId, MIVariableObject>> iterator = lruVariableList.entrySet().iterator();
    	while (iterator.hasNext()){
    		iterator.next();
    		iterator.remove();
    	}
    }
}

Back to the top