aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/cython/Cython/Debugger/libpython.py
blob: 92851290263ae9cd9b3f24cda6faacf5a04ab955 (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
#!/usr/bin/python 
 
# NOTE: this file is taken from the Python source distribution 
# It can be found under Tools/gdb/libpython.py. It is shipped with Cython 
# because it's not installed as a python module, and because changes are only 
# merged into new python versions (v3.2+). 
 
''' 
From gdb 7 onwards, gdb's build can be configured --with-python, allowing gdb 
to be extended with Python code e.g. for library-specific data visualizations, 
such as for the C++ STL types.  Documentation on this API can be seen at: 
http://sourceware.org/gdb/current/onlinedocs/gdb/Python-API.html 
 
 
This python module deals with the case when the process being debugged (the 
"inferior process" in gdb parlance) is itself python, or more specifically, 
linked against libpython.  In this situation, almost every item of data is a 
(PyObject*), and having the debugger merely print their addresses is not very 
enlightening. 
 
This module embeds knowledge about the implementation details of libpython so 
that we can emit useful visualizations e.g. a string, a list, a dict, a frame 
giving file/line information and the state of local variables 
 
In particular, given a gdb.Value corresponding to a PyObject* in the inferior 
process, we can generate a "proxy value" within the gdb process.  For example, 
given a PyObject* in the inferior process that is in fact a PyListObject* 
holding three PyObject* that turn out to be PyBytesObject* instances, we can
generate a proxy value within the gdb process that is a list of bytes
instances:
  [b"foo", b"bar", b"baz"]
 
Doing so can be expensive for complicated graphs of objects, and could take 
some time, so we also have a "write_repr" method that writes a representation 
of the data to a file-like object.  This allows us to stop the traversal by 
having the file-like object raise an exception if it gets too much data. 
 
With both "proxyval" and "write_repr" we keep track of the set of all addresses 
visited so far in the traversal, to avoid infinite recursion due to cycles in 
the graph of object references. 
 
We try to defer gdb.lookup_type() invocations for python types until as late as 
possible: for a dynamically linked python binary, when the process starts in 
the debugger, the libpython.so hasn't been dynamically loaded yet, so none of 
the type names are known to the debugger 
 
The module also extends gdb with some python-specific commands. 
''' 

# NOTE: some gdbs are linked with Python 3, so this file should be dual-syntax
# compatible (2.6+ and 3.0+).  See #19308.

from __future__ import print_function
import gdb
import os 
import locale
import sys 
 
if sys.version_info[0] >= 3:
    unichr = chr
    xrange = range 
    long = int
 
# Look up the gdb.Type for some standard types: 
# Those need to be refreshed as types (pointer sizes) may change when
# gdb loads different executables
 
def _type_char_ptr():
    return gdb.lookup_type('char').pointer()  # char*
 

def _type_unsigned_char_ptr():
    return gdb.lookup_type('unsigned char').pointer()  # unsigned char*


def _type_unsigned_short_ptr():
    return gdb.lookup_type('unsigned short').pointer()


def _type_unsigned_int_ptr():
    return gdb.lookup_type('unsigned int').pointer()


def _sizeof_void_p():
    return gdb.lookup_type('void').pointer().sizeof


# value computed later, see PyUnicodeObjectPtr.proxy()
_is_pep393 = None

Py_TPFLAGS_HEAPTYPE = (1 << 9) 
Py_TPFLAGS_LONG_SUBCLASS     = (1 << 24) 
Py_TPFLAGS_LIST_SUBCLASS     = (1 << 25) 
Py_TPFLAGS_TUPLE_SUBCLASS    = (1 << 26) 
Py_TPFLAGS_BYTES_SUBCLASS    = (1 << 27) 
Py_TPFLAGS_UNICODE_SUBCLASS  = (1 << 28) 
Py_TPFLAGS_DICT_SUBCLASS     = (1 << 29) 
Py_TPFLAGS_BASE_EXC_SUBCLASS = (1 << 30) 
Py_TPFLAGS_TYPE_SUBCLASS     = (1 << 31) 
 
 
MAX_OUTPUT_LEN=1024

hexdigits = "0123456789abcdef" 
 
ENCODING = locale.getpreferredencoding() 
 
EVALFRAME = '_PyEval_EvalFrameDefault'
 
class NullPyObjectPtr(RuntimeError): 
    pass 
 
 
def safety_limit(val): 
    # Given an integer value from the process being debugged, limit it to some
    # safety threshold so that arbitrary breakage within said process doesn't 
    # break the gdb process too much (e.g. sizes of iterations, sizes of lists) 
    return min(val, 1000) 
 
 
def safe_range(val): 
    # As per range, but don't trust the value too much: cap it to a safety 
    # threshold in case the data was corrupted 
    return xrange(safety_limit(int(val)))
 
if sys.version_info[0] >= 3:
    def write_unicode(file, text):
        file.write(text)
else:
    def write_unicode(file, text):
        # Write a byte or unicode string to file. Unicode strings are encoded to
        # ENCODING encoding with 'backslashreplace' error handler to avoid
        # UnicodeEncodeError.
        if isinstance(text, unicode):
            text = text.encode(ENCODING, 'backslashreplace')
        file.write(text)
 
try:
    os_fsencode = os.fsencode
except AttributeError:
    def os_fsencode(filename):
        if not isinstance(filename, unicode):
            return filename
        encoding = sys.getfilesystemencoding()
        if encoding == 'mbcs':
            # mbcs doesn't support surrogateescape
            return filename.encode(encoding)
        encoded = []
        for char in filename:
            # surrogateescape error handler
            if 0xDC80 <= ord(char) <= 0xDCFF:
                byte = chr(ord(char) - 0xDC00)
            else:
                byte = char.encode(encoding)
            encoded.append(byte)
        return ''.join(encoded)
 
class StringTruncated(RuntimeError): 
    pass 
 
class TruncatedStringIO(object): 
    '''Similar to io.StringIO, but can truncate the output by raising a
    StringTruncated exception''' 
    def __init__(self, maxlen=None): 
        self._val = '' 
        self.maxlen = maxlen 
 
    def write(self, data): 
        if self.maxlen: 
            if len(data) + len(self._val) > self.maxlen: 
                # Truncation: 
                self._val += data[0:self.maxlen - len(self._val)] 
                raise StringTruncated() 
 
        self._val += data 
 
    def getvalue(self): 
        return self._val 
 
class PyObjectPtr(object): 
    """ 
    Class wrapping a gdb.Value that's either a (PyObject*) within the
    inferior process, or some subclass pointer e.g. (PyBytesObject*)
 
    There will be a subclass for every refined PyObject type that we care 
    about. 
 
    Note that at every stage the underlying pointer could be NULL, point 
    to corrupt data, etc; this is the debugger, after all. 
    """ 
    _typename = 'PyObject' 
 
    def __init__(self, gdbval, cast_to=None): 
        if cast_to: 
            self._gdbval = gdbval.cast(cast_to) 
        else: 
            self._gdbval = gdbval 
 
    def field(self, name): 
        ''' 
        Get the gdb.Value for the given field within the PyObject, coping with 
        some python 2 versus python 3 differences. 
 
        Various libpython types are defined using the "PyObject_HEAD" and 
        "PyObject_VAR_HEAD" macros. 
 
        In Python 2, this these are defined so that "ob_type" and (for a var 
        object) "ob_size" are fields of the type in question. 
 
        In Python 3, this is defined as an embedded PyVarObject type thus: 
           PyVarObject ob_base; 
        so that the "ob_size" field is located insize the "ob_base" field, and 
        the "ob_type" is most easily accessed by casting back to a (PyObject*). 
        ''' 
        if self.is_null(): 
            raise NullPyObjectPtr(self) 
 
        if name == 'ob_type': 
            pyo_ptr = self._gdbval.cast(PyObjectPtr.get_gdb_type()) 
            return pyo_ptr.dereference()[name] 
 
        if name == 'ob_size': 
            pyo_ptr = self._gdbval.cast(PyVarObjectPtr.get_gdb_type()) 
            return pyo_ptr.dereference()[name] 
 
        # General case: look it up inside the object: 
        return self._gdbval.dereference()[name] 
 
    def pyop_field(self, name): 
        ''' 
        Get a PyObjectPtr for the given PyObject* field within this PyObject, 
        coping with some python 2 versus python 3 differences. 
        ''' 
        return PyObjectPtr.from_pyobject_ptr(self.field(name)) 
 
    def write_field_repr(self, name, out, visited): 
        ''' 
        Extract the PyObject* field named "name", and write its representation 
        to file-like object "out" 
        ''' 
        field_obj = self.pyop_field(name) 
        field_obj.write_repr(out, visited) 
 
    def get_truncated_repr(self, maxlen): 
        ''' 
        Get a repr-like string for the data, but truncate it at "maxlen" bytes 
        (ending the object graph traversal as soon as you do) 
        ''' 
        out = TruncatedStringIO(maxlen) 
        try: 
            self.write_repr(out, set()) 
        except StringTruncated: 
            # Truncation occurred: 
            return out.getvalue() + '...(truncated)' 
 
        # No truncation occurred: 
        return out.getvalue() 
 
    def type(self): 
        return PyTypeObjectPtr(self.field('ob_type')) 
 
    def is_null(self): 
        return 0 == long(self._gdbval)
 
    def is_optimized_out(self): 
        ''' 
        Is the value of the underlying PyObject* visible to the debugger? 
 
        This can vary with the precise version of the compiler used to build 
        Python, and the precise version of gdb. 
 
        See e.g. https://bugzilla.redhat.com/show_bug.cgi?id=556975 with 
        PyEval_EvalFrameEx's "f" 
        ''' 
        return self._gdbval.is_optimized_out 
 
    def safe_tp_name(self): 
        try: 
            return self.type().field('tp_name').string() 
        except NullPyObjectPtr: 
            # NULL tp_name? 
            return 'unknown' 
        except RuntimeError: 
            # Can't even read the object at all? 
            return 'unknown' 
 
    def proxyval(self, visited): 
        ''' 
        Scrape a value from the inferior process, and try to represent it 
        within the gdb process, whilst (hopefully) avoiding crashes when 
        the remote data is corrupt. 
 
        Derived classes will override this. 
 
        For example, a PyIntObject* with ob_ival 42 in the inferior process 
        should result in an int(42) in this process. 
 
        visited: a set of all gdb.Value pyobject pointers already visited 
        whilst generating this value (to guard against infinite recursion when 
        visiting object graphs with loops).  Analogous to Py_ReprEnter and 
        Py_ReprLeave 
        ''' 
 
        class FakeRepr(object): 
            """ 
            Class representing a non-descript PyObject* value in the inferior 
            process for when we don't have a custom scraper, intended to have 
            a sane repr(). 
            """ 
 
            def __init__(self, tp_name, address): 
                self.tp_name = tp_name 
                self.address = address 
 
            def __repr__(self): 
                # For the NULL pointer, we have no way of knowing a type, so 
                # special-case it as per 
                # http://bugs.python.org/issue8032#msg100882 
                if self.address == 0: 
                    return '0x0' 
                return '<%s at remote 0x%x>' % (self.tp_name, self.address) 
 
        return FakeRepr(self.safe_tp_name(), 
                        long(self._gdbval))
 
    def write_repr(self, out, visited): 
        ''' 
        Write a string representation of the value scraped from the inferior 
        process to "out", a file-like object. 
        ''' 
        # Default implementation: generate a proxy value and write its repr 
        # However, this could involve a lot of work for complicated objects, 
        # so for derived classes we specialize this 
        return out.write(repr(self.proxyval(visited))) 
 
    @classmethod 
    def subclass_from_type(cls, t): 
        ''' 
        Given a PyTypeObjectPtr instance wrapping a gdb.Value that's a 
        (PyTypeObject*), determine the corresponding subclass of PyObjectPtr 
        to use 
 
        Ideally, we would look up the symbols for the global types, but that 
        isn't working yet: 
          (gdb) python print gdb.lookup_symbol('PyList_Type')[0].value 
          Traceback (most recent call last): 
            File "<string>", line 1, in <module> 
          NotImplementedError: Symbol type not yet supported in Python scripts. 
          Error while executing Python code. 
 
        For now, we use tp_flags, after doing some string comparisons on the 
        tp_name for some special-cases that don't seem to be visible through 
        flags 
        ''' 
        try: 
            tp_name = t.field('tp_name').string() 
            tp_flags = int(t.field('tp_flags')) 
        except RuntimeError: 
            # Handle any kind of error e.g. NULL ptrs by simply using the base 
            # class 
            return cls 
 
        #print('tp_flags = 0x%08x' % tp_flags)
        #print('tp_name = %r' % tp_name)
 
        name_map = {'bool': PyBoolObjectPtr, 
                    'classobj': PyClassObjectPtr, 
                    'NoneType': PyNoneStructPtr, 
                    'frame': PyFrameObjectPtr, 
                    'set' : PySetObjectPtr, 
                    'frozenset' : PySetObjectPtr, 
                    'builtin_function_or_method' : PyCFunctionObjectPtr, 
                    'method-wrapper': wrapperobject,
                    } 
        if tp_name in name_map: 
            return name_map[tp_name] 
 
        if tp_flags & Py_TPFLAGS_HEAPTYPE:
            return HeapTypeObjectPtr
 
        if tp_flags & Py_TPFLAGS_LONG_SUBCLASS: 
            return PyLongObjectPtr 
        if tp_flags & Py_TPFLAGS_LIST_SUBCLASS: 
            return PyListObjectPtr 
        if tp_flags & Py_TPFLAGS_TUPLE_SUBCLASS: 
            return PyTupleObjectPtr 
        if tp_flags & Py_TPFLAGS_BYTES_SUBCLASS:
            return PyBytesObjectPtr
        if tp_flags & Py_TPFLAGS_UNICODE_SUBCLASS: 
            return PyUnicodeObjectPtr 
        if tp_flags & Py_TPFLAGS_DICT_SUBCLASS: 
            return PyDictObjectPtr 
        if tp_flags & Py_TPFLAGS_BASE_EXC_SUBCLASS: 
            return PyBaseExceptionObjectPtr 
        #if tp_flags & Py_TPFLAGS_TYPE_SUBCLASS:
        #    return PyTypeObjectPtr
 
        # Use the base class: 
        return cls 
 
    @classmethod 
    def from_pyobject_ptr(cls, gdbval): 
        ''' 
        Try to locate the appropriate derived class dynamically, and cast 
        the pointer accordingly. 
        ''' 
        try: 
            p = PyObjectPtr(gdbval) 
            cls = cls.subclass_from_type(p.type()) 
            return cls(gdbval, cast_to=cls.get_gdb_type()) 
        except RuntimeError:
            # Handle any kind of error e.g. NULL ptrs by simply using the base 
            # class 
            pass 
        return cls(gdbval) 
 
    @classmethod 
    def get_gdb_type(cls): 
        return gdb.lookup_type(cls._typename).pointer() 
 
    def as_address(self): 
        return long(self._gdbval)
 
class PyVarObjectPtr(PyObjectPtr): 
    _typename = 'PyVarObject' 
 
class ProxyAlreadyVisited(object): 
    ''' 
    Placeholder proxy to use when protecting against infinite recursion due to 
    loops in the object graph. 
 
    Analogous to the values emitted by the users of Py_ReprEnter and Py_ReprLeave 
    ''' 
    def __init__(self, rep): 
        self._rep = rep 
 
    def __repr__(self): 
        return self._rep 
 
 
def _write_instance_repr(out, visited, name, pyop_attrdict, address): 
    '''Shared code for use by all classes:
    write a representation to file-like object "out"''' 
    out.write('<') 
    out.write(name) 
 
    # Write dictionary of instance attributes: 
    if isinstance(pyop_attrdict, PyDictObjectPtr): 
        out.write('(') 
        first = True 
        for pyop_arg, pyop_val in pyop_attrdict.iteritems():
            if not first: 
                out.write(', ') 
            first = False 
            out.write(pyop_arg.proxyval(visited)) 
            out.write('=') 
            pyop_val.write_repr(out, visited) 
        out.write(')') 
    out.write(' at remote 0x%x>' % address) 
 
 
class InstanceProxy(object): 
 
    def __init__(self, cl_name, attrdict, address): 
        self.cl_name = cl_name 
        self.attrdict = attrdict 
        self.address = address 
 
    def __repr__(self): 
        if isinstance(self.attrdict, dict): 
            kwargs = ', '.join(["%s=%r" % (arg, val)
                                for arg, val in self.attrdict.iteritems()])
            return '<%s(%s) at remote 0x%x>' % (self.cl_name,
                                                kwargs, self.address)
        else: 
            return '<%s at remote 0x%x>' % (self.cl_name,
                                            self.address)
 
def _PyObject_VAR_SIZE(typeobj, nitems):
    if _PyObject_VAR_SIZE._type_size_t is None:
        _PyObject_VAR_SIZE._type_size_t = gdb.lookup_type('size_t')
 
    return ( ( typeobj.field('tp_basicsize') + 
               nitems * typeobj.field('tp_itemsize') + 
               (_sizeof_void_p() - 1)
             ) & ~(_sizeof_void_p() - 1)
           ).cast(_PyObject_VAR_SIZE._type_size_t)
_PyObject_VAR_SIZE._type_size_t = None
 
class HeapTypeObjectPtr(PyObjectPtr):
    _typename = 'PyObject'
 
    def get_attr_dict(self): 
        ''' 
        Get the PyDictObject ptr representing the attribute dictionary 
        (or None if there's a problem) 
        ''' 
        try: 
            typeobj = self.type() 
            dictoffset = int_from_int(typeobj.field('tp_dictoffset')) 
            if dictoffset != 0: 
                if dictoffset < 0: 
                    type_PyVarObject_ptr = gdb.lookup_type('PyVarObject').pointer() 
                    tsize = int_from_int(self._gdbval.cast(type_PyVarObject_ptr)['ob_size']) 
                    if tsize < 0: 
                        tsize = -tsize 
                    size = _PyObject_VAR_SIZE(typeobj, tsize) 
                    dictoffset += size 
                    assert dictoffset > 0 
                    assert dictoffset % _sizeof_void_p() == 0
 
                dictptr = self._gdbval.cast(_type_char_ptr()) + dictoffset
                PyObjectPtrPtr = PyObjectPtr.get_gdb_type().pointer() 
                dictptr = dictptr.cast(PyObjectPtrPtr) 
                return PyObjectPtr.from_pyobject_ptr(dictptr.dereference()) 
        except RuntimeError: 
            # Corrupt data somewhere; fail safe 
            pass 
 
        # Not found, or some kind of error: 
        return None 
 
    def proxyval(self, visited): 
        ''' 
        Support for classes.
 
        Currently we just locate the dictionary using a transliteration to 
        python of _PyObject_GetDictPtr, ignoring descriptors 
        ''' 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            return ProxyAlreadyVisited('<...>') 
        visited.add(self.as_address()) 
 
        pyop_attr_dict = self.get_attr_dict() 
        if pyop_attr_dict: 
            attr_dict = pyop_attr_dict.proxyval(visited) 
        else: 
            attr_dict = {} 
        tp_name = self.safe_tp_name() 
 
        # Class:
        return InstanceProxy(tp_name, attr_dict, long(self._gdbval))
 
    def write_repr(self, out, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            out.write('<...>') 
            return 
        visited.add(self.as_address()) 
 
        pyop_attrdict = self.get_attr_dict()
        _write_instance_repr(out, visited,
                             self.safe_tp_name(), pyop_attrdict, self.as_address())
 
class ProxyException(Exception): 
    def __init__(self, tp_name, args): 
        self.tp_name = tp_name 
        self.args = args 
 
    def __repr__(self): 
        return '%s%r' % (self.tp_name, self.args) 
 
class PyBaseExceptionObjectPtr(PyObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyBaseExceptionObject* i.e. an exception 
    within the process being debugged. 
    """ 
    _typename = 'PyBaseExceptionObject' 
 
    def proxyval(self, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            return ProxyAlreadyVisited('(...)') 
        visited.add(self.as_address()) 
        arg_proxy = self.pyop_field('args').proxyval(visited) 
        return ProxyException(self.safe_tp_name(), 
                              arg_proxy) 
 
    def write_repr(self, out, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            out.write('(...)') 
            return 
        visited.add(self.as_address()) 
 
        out.write(self.safe_tp_name()) 
        self.write_field_repr('args', out, visited) 
 
class PyClassObjectPtr(PyObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyClassObject* i.e. a <classobj> 
    instance within the process being debugged. 
    """ 
    _typename = 'PyClassObject' 
 
 
class BuiltInFunctionProxy(object): 
    def __init__(self, ml_name): 
        self.ml_name = ml_name 
 
    def __repr__(self): 
        return "<built-in function %s>" % self.ml_name 
 
class BuiltInMethodProxy(object): 
    def __init__(self, ml_name, pyop_m_self): 
        self.ml_name = ml_name 
        self.pyop_m_self = pyop_m_self 
 
    def __repr__(self): 
        return ('<built-in method %s of %s object at remote 0x%x>'
                % (self.ml_name,
                   self.pyop_m_self.safe_tp_name(),
                   self.pyop_m_self.as_address())
                )
 
class PyCFunctionObjectPtr(PyObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyCFunctionObject* 
    (see Include/methodobject.h and Objects/methodobject.c) 
    """ 
    _typename = 'PyCFunctionObject' 
 
    def proxyval(self, visited): 
        m_ml = self.field('m_ml') # m_ml is a (PyMethodDef*) 
        ml_name = m_ml['ml_name'].string() 
 
        pyop_m_self = self.pyop_field('m_self') 
        if pyop_m_self.is_null(): 
            return BuiltInFunctionProxy(ml_name) 
        else: 
            return BuiltInMethodProxy(ml_name, pyop_m_self) 
 
 
class PyCodeObjectPtr(PyObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyCodeObject* i.e. a <code> instance 
    within the process being debugged. 
    """ 
    _typename = 'PyCodeObject' 
 
    def addr2line(self, addrq): 
        ''' 
        Get the line number for a given bytecode offset 
 
        Analogous to PyCode_Addr2Line; translated from pseudocode in 
        Objects/lnotab_notes.txt 
        ''' 
        co_lnotab = self.pyop_field('co_lnotab').proxyval(set()) 
 
        # Initialize lineno to co_firstlineno as per PyCode_Addr2Line 
        # not 0, as lnotab_notes.txt has it: 
        lineno = int_from_int(self.field('co_firstlineno')) 
 
        addr = 0 
        for addr_incr, line_incr in zip(co_lnotab[::2], co_lnotab[1::2]): 
            addr += ord(addr_incr) 
            if addr > addrq: 
                return lineno 
            lineno += ord(line_incr) 
        return lineno 
 
 
class PyDictObjectPtr(PyObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyDictObject* i.e. a dict instance 
    within the process being debugged. 
    """ 
    _typename = 'PyDictObject' 
 
    def iteritems(self): 
        ''' 
        Yields a sequence of (PyObjectPtr key, PyObjectPtr value) pairs, 
        analogous to dict.iteritems()
        ''' 
        keys = self.field('ma_keys')
        values = self.field('ma_values')
        entries, nentries = self._get_entries(keys)
        for i in safe_range(nentries):
            ep = entries[i]
            if long(values):
                pyop_value = PyObjectPtr.from_pyobject_ptr(values[i])
            else:
                pyop_value = PyObjectPtr.from_pyobject_ptr(ep['me_value'])
            if not pyop_value.is_null(): 
                pyop_key = PyObjectPtr.from_pyobject_ptr(ep['me_key']) 
                yield (pyop_key, pyop_value) 
 
    def proxyval(self, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            return ProxyAlreadyVisited('{...}') 
        visited.add(self.as_address()) 
 
        result = {} 
        for pyop_key, pyop_value in self.iteritems():
            proxy_key = pyop_key.proxyval(visited) 
            proxy_value = pyop_value.proxyval(visited) 
            result[proxy_key] = proxy_value 
        return result 
 
    def write_repr(self, out, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            out.write('{...}') 
            return 
        visited.add(self.as_address()) 
 
        out.write('{') 
        first = True 
        for pyop_key, pyop_value in self.iteritems():
            if not first: 
                out.write(', ') 
            first = False 
            pyop_key.write_repr(out, visited) 
            out.write(': ') 
            pyop_value.write_repr(out, visited) 
        out.write('}') 
 
    def _get_entries(self, keys):
        dk_nentries = int(keys['dk_nentries'])
        dk_size = int(keys['dk_size'])
        try:
            # <= Python 3.5
            return keys['dk_entries'], dk_size
        except RuntimeError:
            # >= Python 3.6
            pass
 
        if dk_size <= 0xFF:
            offset = dk_size
        elif dk_size <= 0xFFFF:
            offset = 2 * dk_size
        elif dk_size <= 0xFFFFFFFF:
            offset = 4 * dk_size
        else:
            offset = 8 * dk_size
 
        ent_addr = keys['dk_indices']['as_1'].address
        ent_addr = ent_addr.cast(_type_unsigned_char_ptr()) + offset
        ent_ptr_t = gdb.lookup_type('PyDictKeyEntry').pointer()
        ent_addr = ent_addr.cast(ent_ptr_t)
 
        return ent_addr, dk_nentries
 
 
class PyListObjectPtr(PyObjectPtr): 
    _typename = 'PyListObject' 
 
    def __getitem__(self, i): 
        # Get the gdb.Value for the (PyObject*) with the given index: 
        field_ob_item = self.field('ob_item') 
        return field_ob_item[i] 
 
    def proxyval(self, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            return ProxyAlreadyVisited('[...]') 
        visited.add(self.as_address()) 
 
        result = [PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited) 
                  for i in safe_range(int_from_int(self.field('ob_size')))] 
        return result 
 
    def write_repr(self, out, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            out.write('[...]') 
            return 
        visited.add(self.as_address()) 
 
        out.write('[') 
        for i in safe_range(int_from_int(self.field('ob_size'))): 
            if i > 0: 
                out.write(', ') 
            element = PyObjectPtr.from_pyobject_ptr(self[i]) 
            element.write_repr(out, visited) 
        out.write(']') 
 
class PyLongObjectPtr(PyObjectPtr): 
    _typename = 'PyLongObject' 
 
    def proxyval(self, visited): 
        ''' 
        Python's Include/longobjrep.h has this declaration: 
           struct _longobject { 
               PyObject_VAR_HEAD 
               digit ob_digit[1]; 
           }; 
 
        with this description: 
            The absolute value of a number is equal to 
                 SUM(for i=0 through abs(ob_size)-1) ob_digit[i] * 2**(SHIFT*i) 
            Negative numbers are represented with ob_size < 0; 
            zero is represented by ob_size == 0. 
 
        where SHIFT can be either: 
            #define PyLong_SHIFT        30 
            #define PyLong_SHIFT        15 
        ''' 
        ob_size = long(self.field('ob_size'))
        if ob_size == 0: 
            return 0
 
        ob_digit = self.field('ob_digit') 
 
        if gdb.lookup_type('digit').sizeof == 2: 
            SHIFT = 15 
        else: 
            SHIFT = 30 
 
        digits = [long(ob_digit[i]) * 2**(SHIFT*i)
                  for i in safe_range(abs(ob_size))] 
        result = sum(digits) 
        if ob_size < 0: 
            result = -result 
        return result 
 
    def write_repr(self, out, visited): 
        # Write this out as a Python 3 int literal, i.e. without the "L" suffix 
        proxy = self.proxyval(visited) 
        out.write("%s" % proxy) 
 
 
class PyBoolObjectPtr(PyLongObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyBoolObject* i.e. one of the two 
    <bool> instances (Py_True/Py_False) within the process being debugged. 
    """ 
    def proxyval(self, visited): 
        if PyLongObjectPtr.proxyval(self, visited):
            return True
        else:
            return False
 
class PyNoneStructPtr(PyObjectPtr): 
    """ 
    Class wrapping a gdb.Value that's a PyObject* pointing to the 
    singleton (we hope) _Py_NoneStruct with ob_type PyNone_Type 
    """ 
    _typename = 'PyObject' 
 
    def proxyval(self, visited): 
        return None 
 
 
class PyFrameObjectPtr(PyObjectPtr): 
    _typename = 'PyFrameObject' 
 
    def __init__(self, gdbval, cast_to=None): 
        PyObjectPtr.__init__(self, gdbval, cast_to) 
 
        if not self.is_optimized_out(): 
            self.co = PyCodeObjectPtr.from_pyobject_ptr(self.field('f_code')) 
            self.co_name = self.co.pyop_field('co_name') 
            self.co_filename = self.co.pyop_field('co_filename') 
 
            self.f_lineno = int_from_int(self.field('f_lineno')) 
            self.f_lasti = int_from_int(self.field('f_lasti')) 
            self.co_nlocals = int_from_int(self.co.field('co_nlocals')) 
            self.co_varnames = PyTupleObjectPtr.from_pyobject_ptr(self.co.field('co_varnames')) 
 
    def iter_locals(self): 
        ''' 
        Yield a sequence of (name,value) pairs of PyObjectPtr instances, for 
        the local variables of this frame 
        ''' 
        if self.is_optimized_out(): 
            return 
 
        f_localsplus = self.field('f_localsplus') 
        for i in safe_range(self.co_nlocals): 
            pyop_value = PyObjectPtr.from_pyobject_ptr(f_localsplus[i]) 
            if not pyop_value.is_null(): 
                pyop_name = PyObjectPtr.from_pyobject_ptr(self.co_varnames[i]) 
                yield (pyop_name, pyop_value) 
 
    def iter_globals(self): 
        ''' 
        Yield a sequence of (name,value) pairs of PyObjectPtr instances, for 
        the global variables of this frame 
        ''' 
        if self.is_optimized_out(): 
            return ()
 
        pyop_globals = self.pyop_field('f_globals') 
        return pyop_globals.iteritems()
 
    def iter_builtins(self): 
        ''' 
        Yield a sequence of (name,value) pairs of PyObjectPtr instances, for 
        the builtin variables 
        ''' 
        if self.is_optimized_out(): 
            return ()
 
        pyop_builtins = self.pyop_field('f_builtins') 
        return pyop_builtins.iteritems()
 
    def get_var_by_name(self, name): 
        ''' 
        Look for the named local variable, returning a (PyObjectPtr, scope) pair 
        where scope is a string 'local', 'global', 'builtin' 
 
        If not found, return (None, None) 
        ''' 
        for pyop_name, pyop_value in self.iter_locals(): 
            if name == pyop_name.proxyval(set()): 
                return pyop_value, 'local' 
        for pyop_name, pyop_value in self.iter_globals(): 
            if name == pyop_name.proxyval(set()): 
                return pyop_value, 'global' 
        for pyop_name, pyop_value in self.iter_builtins(): 
            if name == pyop_name.proxyval(set()): 
                return pyop_value, 'builtin' 
        return None, None 
 
    def filename(self): 
        '''Get the path of the current Python source file, as a string''' 
        if self.is_optimized_out(): 
            return '(frame information optimized out)' 
        return self.co_filename.proxyval(set()) 
 
    def current_line_num(self): 
        '''Get current line number as an integer (1-based) 
 
        Translated from PyFrame_GetLineNumber and PyCode_Addr2Line 
 
        See Objects/lnotab_notes.txt 
        ''' 
        if self.is_optimized_out(): 
            return None 
        f_trace = self.field('f_trace') 
        if long(f_trace) != 0:
            # we have a non-NULL f_trace: 
            return self.f_lineno 
        else: 
            #try: 
            return self.co.addr2line(self.f_lasti) 
            #except ValueError: 
            #    return self.f_lineno 
 
    def current_line(self): 
        '''Get the text of the current source line as a string, with a trailing 
        newline character''' 
        if self.is_optimized_out(): 
            return '(frame information optimized out)' 
        filename = self.filename() 
        try:
            f = open(os_fsencode(filename), 'r')
        except IOError:
            return None
        with f:
            all_lines = f.readlines() 
            # Convert from 1-based current_line_num to 0-based list offset: 
            return all_lines[self.current_line_num()-1] 
 
    def write_repr(self, out, visited): 
        if self.is_optimized_out(): 
            out.write('(frame information optimized out)') 
            return 
        out.write('Frame 0x%x, for file %s, line %i, in %s (' 
                  % (self.as_address(), 
                     self.co_filename.proxyval(visited), 
                     self.current_line_num(), 
                     self.co_name.proxyval(visited))) 
        first = True 
        for pyop_name, pyop_value in self.iter_locals(): 
            if not first: 
                out.write(', ') 
            first = False 
 
            out.write(pyop_name.proxyval(visited)) 
            out.write('=') 
            pyop_value.write_repr(out, visited) 
 
        out.write(')') 
 
    def print_traceback(self):
        if self.is_optimized_out():
            sys.stdout.write('  (frame information optimized out)\n')
            return
        visited = set()
        sys.stdout.write('  File "%s", line %i, in %s\n'
                  % (self.co_filename.proxyval(visited),
                     self.current_line_num(),
                     self.co_name.proxyval(visited)))
 
class PySetObjectPtr(PyObjectPtr): 
    _typename = 'PySetObject' 
 
    @classmethod
    def _dummy_key(self):
        return gdb.lookup_global_symbol('_PySet_Dummy').value()

    def __iter__(self):
        dummy_ptr = self._dummy_key()
        table = self.field('table')
        for i in safe_range(self.field('mask') + 1):
            setentry = table[i]
            key = setentry['key']
            if key != 0 and key != dummy_ptr:
                yield PyObjectPtr.from_pyobject_ptr(key)

    def proxyval(self, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            return ProxyAlreadyVisited('%s(...)' % self.safe_tp_name()) 
        visited.add(self.as_address()) 
 
        members = (key.proxyval(visited) for key in self)
        if self.safe_tp_name() == 'frozenset': 
            return frozenset(members) 
        else: 
            return set(members) 
 
    def write_repr(self, out, visited): 
        # Emulate Python 3's set_repr 
        tp_name = self.safe_tp_name() 
 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            out.write('(...)') 
            return 
        visited.add(self.as_address()) 
 
        # Python 3's set_repr special-cases the empty set: 
        if not self.field('used'): 
            out.write(tp_name) 
            out.write('()') 
            return 
 
        # Python 3 uses {} for set literals: 
        if tp_name != 'set': 
            out.write(tp_name) 
            out.write('(') 
 
        out.write('{') 
        first = True 
        for key in self:
            if not first:
                out.write(', ')
            first = False
            key.write_repr(out, visited)
        out.write('}') 
 
        if tp_name != 'set': 
            out.write(')') 
 
 
class PyBytesObjectPtr(PyObjectPtr): 
    _typename = 'PyBytesObject' 
 
    def __str__(self): 
        field_ob_size = self.field('ob_size') 
        field_ob_sval = self.field('ob_sval') 
        char_ptr = field_ob_sval.address.cast(_type_unsigned_char_ptr())
        return ''.join([chr(char_ptr[i]) for i in safe_range(field_ob_size)])
 
    def proxyval(self, visited): 
        return str(self) 
 
    def write_repr(self, out, visited):
        # Write this out as a Python 3 bytes literal, i.e. with a "b" prefix 
 
        # Get a PyStringObject* within the Python 2 gdb process: 
        proxy = self.proxyval(visited) 
 
        # Transliteration of Python 3's Objects/bytesobject.c:PyBytes_Repr 
        # to Python 2 code: 
        quote = "'" 
        if "'" in proxy and not '"' in proxy: 
            quote = '"' 
        out.write('b')
        out.write(quote) 
        for byte in proxy: 
            if byte == quote or byte == '\\': 
                out.write('\\') 
                out.write(byte) 
            elif byte == '\t': 
                out.write('\\t') 
            elif byte == '\n': 
                out.write('\\n') 
            elif byte == '\r': 
                out.write('\\r') 
            elif byte < ' ' or ord(byte) >= 0x7f: 
                out.write('\\x') 
                out.write(hexdigits[(ord(byte) & 0xf0) >> 4]) 
                out.write(hexdigits[ord(byte) & 0xf]) 
            else: 
                out.write(byte) 
        out.write(quote) 
 
 
class PyStringObjectPtr(PyBytesObjectPtr): 
    _typename = 'PyStringObject' 
 
 
class PyTupleObjectPtr(PyObjectPtr): 
    _typename = 'PyTupleObject' 
 
    def __getitem__(self, i): 
        # Get the gdb.Value for the (PyObject*) with the given index: 
        field_ob_item = self.field('ob_item') 
        return field_ob_item[i] 
 
    def proxyval(self, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            return ProxyAlreadyVisited('(...)') 
        visited.add(self.as_address()) 
 
        result = tuple(PyObjectPtr.from_pyobject_ptr(self[i]).proxyval(visited)
                       for i in safe_range(int_from_int(self.field('ob_size'))))
        return result 
 
    def write_repr(self, out, visited): 
        # Guard against infinite loops: 
        if self.as_address() in visited: 
            out.write('(...)') 
            return 
        visited.add(self.as_address()) 
 
        out.write('(') 
        for i in safe_range(int_from_int(self.field('ob_size'))): 
            if i > 0: 
                out.write(', ') 
            element = PyObjectPtr.from_pyobject_ptr(self[i]) 
            element.write_repr(out, visited) 
        if self.field('ob_size') == 1: 
            out.write(',)') 
        else: 
            out.write(')') 
 
class PyTypeObjectPtr(PyObjectPtr):
    _typename = 'PyTypeObject'
 

def _unichr_is_printable(char): 
    # Logic adapted from Python 3's Tools/unicode/makeunicodedata.py 
    if char == u" ": 
        return True 
    import unicodedata 
    return unicodedata.category(char) not in ("C", "Z") 
 
if sys.maxunicode >= 0x10000: 
    _unichr = unichr
else: 
    # Needed for proper surrogate support if sizeof(Py_UNICODE) is 2 in gdb 
    def _unichr(x): 
        if x < 0x10000: 
            return unichr(x) 
        x -= 0x10000 
        ch1 = 0xD800 | (x >> 10) 
        ch2 = 0xDC00 | (x & 0x3FF) 
        return unichr(ch1) + unichr(ch2) 
 
 
class PyUnicodeObjectPtr(PyObjectPtr): 
    _typename = 'PyUnicodeObject' 
 
    def char_width(self): 
        _type_Py_UNICODE = gdb.lookup_type('Py_UNICODE') 
        return _type_Py_UNICODE.sizeof 
 
    def proxyval(self, visited): 
        global _is_pep393
        if _is_pep393 is None:
            fields = gdb.lookup_type('PyUnicodeObject').target().fields()
            _is_pep393 = 'data' in [f.name for f in fields]
        if _is_pep393:
            # Python 3.3 and newer
            may_have_surrogates = False
            compact = self.field('_base')
            ascii = compact['_base']
            state = ascii['state']
            is_compact_ascii = (int(state['ascii']) and int(state['compact']))
            if not int(state['ready']):
                # string is not ready
                field_length = long(compact['wstr_length'])
                may_have_surrogates = True
                field_str = ascii['wstr']
            else:
                field_length = long(ascii['length'])
                if is_compact_ascii:
                    field_str = ascii.address + 1
                elif int(state['compact']):
                    field_str = compact.address + 1
                else:
                    field_str = self.field('data')['any']
                repr_kind = int(state['kind'])
                if repr_kind == 1:
                    field_str = field_str.cast(_type_unsigned_char_ptr())
                elif repr_kind == 2:
                    field_str = field_str.cast(_type_unsigned_short_ptr())
                elif repr_kind == 4:
                    field_str = field_str.cast(_type_unsigned_int_ptr())
        else:
            # Python 3.2 and earlier
            field_length = long(self.field('length'))
            field_str = self.field('str')
            may_have_surrogates = self.char_width() == 2
 
        # Gather a list of ints from the Py_UNICODE array; these are either 
        # UCS-1, UCS-2 or UCS-4 code points:
        if not may_have_surrogates:
            Py_UNICODEs = [int(field_str[i]) for i in safe_range(field_length)] 
        else: 
            # A more elaborate routine if sizeof(Py_UNICODE) is 2 in the 
            # inferior process: we must join surrogate pairs. 
            Py_UNICODEs = [] 
            i = 0 
            limit = safety_limit(field_length) 
            while i < limit: 
                ucs = int(field_str[i]) 
                i += 1 
                if ucs < 0xD800 or ucs >= 0xDC00 or i == field_length: 
                    Py_UNICODEs.append(ucs) 
                    continue 
                # This could be a surrogate pair. 
                ucs2 = int(field_str[i]) 
                if ucs2 < 0xDC00 or ucs2 > 0xDFFF: 
                    continue 
                code = (ucs & 0x03FF) << 10 
                code |= ucs2 & 0x03FF 
                code += 0x00010000 
                Py_UNICODEs.append(code) 
                i += 1 
 
        # Convert the int code points to unicode characters, and generate a 
        # local unicode instance. 
        # This splits surrogate pairs if sizeof(Py_UNICODE) is 2 here (in gdb). 
        result = u''.join([
            (_unichr(ucs) if ucs <= 0x10ffff else '\ufffd')
            for ucs in Py_UNICODEs])
        return result 
 
    def write_repr(self, out, visited): 
        # Write this out as a Python 3 str literal, i.e. without a "u" prefix

        # Get a PyUnicodeObject* within the Python 2 gdb process: 
        proxy = self.proxyval(visited) 
 
        # Transliteration of Python 3's Object/unicodeobject.c:unicode_repr 
        # to Python 2: 
        if "'" in proxy and '"' not in proxy: 
            quote = '"' 
        else: 
            quote = "'" 
        out.write(quote) 
 
        i = 0 
        while i < len(proxy): 
            ch = proxy[i] 
            i += 1 
 
            # Escape quotes and backslashes 
            if ch == quote or ch == '\\': 
                out.write('\\') 
                out.write(ch) 
 
            #  Map special whitespace to '\t', \n', '\r' 
            elif ch == '\t': 
                out.write('\\t') 
            elif ch == '\n': 
                out.write('\\n') 
            elif ch == '\r': 
                out.write('\\r') 
 
            # Map non-printable US ASCII to '\xhh' */ 
            elif ch < ' ' or ch == 0x7F: 
                out.write('\\x') 
                out.write(hexdigits[(ord(ch) >> 4) & 0x000F]) 
                out.write(hexdigits[ord(ch) & 0x000F]) 
 
            # Copy ASCII characters as-is 
            elif ord(ch) < 0x7F: 
                out.write(ch) 
 
            # Non-ASCII characters 
            else: 
                ucs = ch 
                ch2 = None 
                if sys.maxunicode < 0x10000: 
                    # If sizeof(Py_UNICODE) is 2 here (in gdb), join 
                    # surrogate pairs before calling _unichr_is_printable. 
                    if (i < len(proxy) 
                    and 0xD800 <= ord(ch) < 0xDC00 \ 
                    and 0xDC00 <= ord(proxy[i]) <= 0xDFFF): 
                        ch2 = proxy[i] 
                        ucs = ch + ch2 
                        i += 1 
 
                # Unfortuately, Python 2's unicode type doesn't seem 
                # to expose the "isprintable" method 
                printable = _unichr_is_printable(ucs) 
                if printable: 
                    try: 
                        ucs.encode(ENCODING) 
                    except UnicodeEncodeError: 
                        printable = False 
 
                # Map Unicode whitespace and control characters 
                # (categories Z* and C* except ASCII space) 
                if not printable: 
                    if ch2 is not None: 
                        # Match Python 3's representation of non-printable 
                        # wide characters. 
                        code = (ord(ch) & 0x03FF) << 10 
                        code |= ord(ch2) & 0x03FF 
                        code += 0x00010000 
                    else: 
                        code = ord(ucs) 
 
                    # Map 8-bit characters to '\\xhh' 
                    if code <= 0xff: 
                        out.write('\\x') 
                        out.write(hexdigits[(code >> 4) & 0x000F]) 
                        out.write(hexdigits[code & 0x000F]) 
                    # Map 21-bit characters to '\U00xxxxxx' 
                    elif code >= 0x10000: 
                        out.write('\\U') 
                        out.write(hexdigits[(code >> 28) & 0x0000000F]) 
                        out.write(hexdigits[(code >> 24) & 0x0000000F]) 
                        out.write(hexdigits[(code >> 20) & 0x0000000F]) 
                        out.write(hexdigits[(code >> 16) & 0x0000000F]) 
                        out.write(hexdigits[(code >> 12) & 0x0000000F]) 
                        out.write(hexdigits[(code >> 8) & 0x0000000F]) 
                        out.write(hexdigits[(code >> 4) & 0x0000000F]) 
                        out.write(hexdigits[code & 0x0000000F]) 
                    # Map 16-bit characters to '\uxxxx' 
                    else: 
                        out.write('\\u') 
                        out.write(hexdigits[(code >> 12) & 0x000F]) 
                        out.write(hexdigits[(code >> 8) & 0x000F]) 
                        out.write(hexdigits[(code >> 4) & 0x000F]) 
                        out.write(hexdigits[code & 0x000F]) 
                else: 
                    # Copy characters as-is 
                    out.write(ch) 
                    if ch2 is not None: 
                        out.write(ch2) 
 
        out.write(quote) 
 
 
class wrapperobject(PyObjectPtr):
    _typename = 'wrapperobject'
 
    def safe_name(self):
        try:
            name = self.field('descr')['d_base']['name'].string()
            return repr(name)
        except (NullPyObjectPtr, RuntimeError):
            return '<unknown name>'
 
    def safe_tp_name(self):
        try:
            return self.field('self')['ob_type']['tp_name'].string()
        except (NullPyObjectPtr, RuntimeError):
            return '<unknown tp_name>'

    def safe_self_addresss(self):
        try:
            address = long(self.field('self'))
            return '%#x' % address
        except (NullPyObjectPtr, RuntimeError):
            return '<failed to get self address>'

    def proxyval(self, visited):
        name = self.safe_name()
        tp_name = self.safe_tp_name()
        self_address = self.safe_self_addresss()
        return ("<method-wrapper %s of %s object at %s>"
                % (name, tp_name, self_address))

    def write_repr(self, out, visited):
        proxy = self.proxyval(visited)
        out.write(proxy)


def int_from_int(gdbval): 
    return int(str(gdbval)) 
 
 
def stringify(val): 
    # TODO: repr() puts everything on one line; pformat can be nicer, but 
    # can lead to v.long results; this function isolates the choice 
    if True: 
        return repr(val) 
    else: 
        from pprint import pformat 
        return pformat(val) 
 
 
class PyObjectPtrPrinter: 
    "Prints a (PyObject*)" 
 
    def __init__ (self, gdbval): 
        self.gdbval = gdbval 
 
    def to_string (self): 
        pyop = PyObjectPtr.from_pyobject_ptr(self.gdbval) 
        if True: 
            return pyop.get_truncated_repr(MAX_OUTPUT_LEN) 
        else: 
            # Generate full proxy value then stringify it. 
            # Doing so could be expensive 
            proxyval = pyop.proxyval(set()) 
            return stringify(proxyval) 
 
def pretty_printer_lookup(gdbval): 
    type = gdbval.type.unqualified() 
    if type.code != gdb.TYPE_CODE_PTR:
        return None
 
    type = type.target().unqualified()
    t = str(type)
    if t in ("PyObject", "PyFrameObject", "PyUnicodeObject", "wrapperobject"):
        return PyObjectPtrPrinter(gdbval)

""" 
During development, I've been manually invoking the code in this way: 
(gdb) python 
 
import sys 
sys.path.append('/home/david/coding/python-gdb') 
import libpython 
end 
 
then reloading it after each edit like this: 
(gdb) python reload(libpython) 
 
The following code should ensure that the prettyprinter is registered 
if the code is autoloaded by gdb when visiting libpython.so, provided 
that this python file is installed to the same path as the library (or its 
.debug file) plus a "-gdb.py" suffix, e.g: 
  /usr/lib/libpython2.6.so.1.0-gdb.py 
  /usr/lib/debug/usr/lib/libpython2.6.so.1.0.debug-gdb.py 
""" 
def register (obj):
    if obj is None: 
        obj = gdb 
 
    # Wire up the pretty-printer 
    obj.pretty_printers.append(pretty_printer_lookup) 
 
register (gdb.current_objfile ())
 


# Unfortunately, the exact API exposed by the gdb module varies somewhat 
# from build to build 
# See http://bugs.python.org/issue8279?#msg102276 
 
class Frame(object): 
    ''' 
    Wrapper for gdb.Frame, adding various methods 
    ''' 
    def __init__(self, gdbframe): 
        self._gdbframe = gdbframe 
 
    def older(self): 
        older = self._gdbframe.older() 
        if older: 
            return Frame(older) 
        else: 
            return None 
 
    def newer(self): 
        newer = self._gdbframe.newer() 
        if newer: 
            return Frame(newer) 
        else: 
            return None 
 
    def select(self): 
        '''If supported, select this frame and return True; return False if unsupported 
 
        Not all builds have a gdb.Frame.select method; seems to be present on Fedora 12 
        onwards, but absent on Ubuntu buildbot''' 
        if not hasattr(self._gdbframe, 'select'): 
            print ('Unable to select frame: ' 
                   'this build of gdb does not expose a gdb.Frame.select method') 
            return False 
        self._gdbframe.select() 
        return True 
 
    def get_index(self): 
        '''Calculate index of frame, starting at 0 for the newest frame within 
        this thread''' 
        index = 0 
        # Go down until you reach the newest frame: 
        iter_frame = self 
        while iter_frame.newer(): 
            index += 1 
            iter_frame = iter_frame.newer() 
        return index 
 
    # We divide frames into:
    #   - "python frames":
    #       - "bytecode frames" i.e. PyEval_EvalFrameEx
    #       - "other python frames": things that are of interest from a python
    #         POV, but aren't bytecode (e.g. GC, GIL)
    #   - everything else

    def is_python_frame(self):
        '''Is this a _PyEval_EvalFrameDefault frame, or some other important
        frame? (see is_other_python_frame for what "important" means in this
        context)'''
        if self.is_evalframe():
            return True
        if self.is_other_python_frame():
            return True
        return False

    def is_evalframe(self):
        '''Is this a _PyEval_EvalFrameDefault frame?'''
        if self._gdbframe.name() == EVALFRAME:
            ''' 
            I believe we also need to filter on the inline 
            struct frame_id.inline_depth, only regarding frames with 
            an inline depth of 0 as actually being this function 
 
            So we reject those with type gdb.INLINE_FRAME 
            ''' 
            if self._gdbframe.type() == gdb.NORMAL_FRAME: 
                # We have a _PyEval_EvalFrameDefault frame:
                return True 
 
        return False 
 
    def is_other_python_frame(self):
        '''Is this frame worth displaying in python backtraces?
        Examples:
          - waiting on the GIL
          - garbage-collecting
          - within a CFunction
         If it is, return a descriptive string
         For other frames, return False
         '''
        if self.is_waiting_for_gil():
            return 'Waiting for the GIL'
 
        if self.is_gc_collect():
            return 'Garbage-collecting'
 
        # Detect invocations of PyCFunction instances:
        frame = self._gdbframe
        caller = frame.name()
        if not caller:
            return False

        if caller in ('_PyCFunction_FastCallDict',
                      '_PyCFunction_FastCallKeywords'):
            arg_name = 'func'
            # Within that frame:
            #   "func" is the local containing the PyObject* of the
            # PyCFunctionObject instance
            #   "f" is the same value, but cast to (PyCFunctionObject*)
            #   "self" is the (PyObject*) of the 'self'
            try: 
                # Use the prettyprinter for the func:
                func = frame.read_var(arg_name)
                return str(func)
            except RuntimeError:
                return 'PyCFunction invocation (unable to read %s)' % arg_name
 
        if caller == 'wrapper_call':
            try:
                func = frame.read_var('wp')
                return str(func)
            except RuntimeError:
                return '<wrapper_call invocation>'
 
        # This frame isn't worth reporting:
        return False
 
    def is_waiting_for_gil(self):
        '''Is this frame waiting on the GIL?'''
        # This assumes the _POSIX_THREADS version of Python/ceval_gil.h:
        name = self._gdbframe.name()
        if name:
            return 'pthread_cond_timedwait' in name

    def is_gc_collect(self):
        '''Is this frame "collect" within the garbage-collector?'''
        return self._gdbframe.name() == 'collect'

    def get_pyop(self): 
        try: 
            f = self._gdbframe.read_var('f')
            frame = PyFrameObjectPtr.from_pyobject_ptr(f)
            if not frame.is_optimized_out():
                return frame
            # gdb is unable to get the "f" argument of PyEval_EvalFrameEx()
            # because it was "optimized out". Try to get "f" from the frame
            # of the caller, PyEval_EvalCodeEx().
            orig_frame = frame
            caller = self._gdbframe.older()
            if caller:
                f = caller.read_var('f')
                frame = PyFrameObjectPtr.from_pyobject_ptr(f)
                if not frame.is_optimized_out():
                    return frame
            return orig_frame
        except ValueError:
            return None 
 
    @classmethod 
    def get_selected_frame(cls): 
        _gdbframe = gdb.selected_frame() 
        if _gdbframe: 
            return Frame(_gdbframe) 
        return None 
 
    @classmethod 
    def get_selected_python_frame(cls): 
        '''Try to obtain the Frame for the python-related code in the selected
        frame, or None'''
        try:
            frame = cls.get_selected_frame()
        except gdb.error:
            # No frame: Python didn't start yet
            return None

        while frame:
            if frame.is_python_frame():
                return frame
            frame = frame.older()

        # Not found:
        return None

    @classmethod
    def get_selected_bytecode_frame(cls):
        '''Try to obtain the Frame for the python bytecode interpreter in the
        selected GDB frame, or None'''
        frame = cls.get_selected_frame() 
 
        while frame: 
            if frame.is_evalframe():
                return frame 
            frame = frame.older() 
 
        # Not found: 
        return None 
 
    def print_summary(self): 
        if self.is_evalframe():
            pyop = self.get_pyop() 
            if pyop: 
                line = pyop.get_truncated_repr(MAX_OUTPUT_LEN) 
                write_unicode(sys.stdout, '#%i %s\n' % (self.get_index(), line)) 
                if not pyop.is_optimized_out():
                    line = pyop.current_line()
                    if line is not None:
                        sys.stdout.write('    %s\n' % line.strip())
            else: 
                sys.stdout.write('#%i (unable to read python frame information)\n' % self.get_index()) 
        else: 
            info = self.is_other_python_frame()
            if info:
                sys.stdout.write('#%i %s\n' % (self.get_index(), info))
            else:
                sys.stdout.write('#%i\n' % self.get_index())
 
    def print_traceback(self):
        if self.is_evalframe():
            pyop = self.get_pyop()
            if pyop:
                pyop.print_traceback()
                if not pyop.is_optimized_out():
                    line = pyop.current_line()
                    if line is not None:
                        sys.stdout.write('    %s\n' % line.strip())
            else:
                sys.stdout.write('  (unable to read python frame information)\n')
        else:
            info = self.is_other_python_frame()
            if info:
                sys.stdout.write('  %s\n' % info)
            else:
                sys.stdout.write('  (not a python frame)\n')
 
class PyList(gdb.Command): 
    '''List the current Python source code, if any 
 
    Use 
       py-list START 
    to list at a different line number within the python source. 
 
    Use 
       py-list START, END 
    to list a specific range of lines within the python source. 
    ''' 
 
    def __init__(self): 
        gdb.Command.__init__ (self, 
                              "py-list", 
                              gdb.COMMAND_FILES, 
                              gdb.COMPLETE_NONE) 
 

    def invoke(self, args, from_tty): 
        import re 
 
        start = None 
        end = None 
 
        m = re.match(r'\s*(\d+)\s*', args) 
        if m: 
            start = int(m.group(0)) 
            end = start + 10 
 
        m = re.match(r'\s*(\d+)\s*,\s*(\d+)\s*', args) 
        if m: 
            start, end = map(int, m.groups()) 
 
        # py-list requires an actual PyEval_EvalFrameEx frame:
        frame = Frame.get_selected_bytecode_frame()
        if not frame: 
            print('Unable to locate gdb frame for python bytecode interpreter')
            return 
 
        pyop = frame.get_pyop() 
        if not pyop or pyop.is_optimized_out():
            print('Unable to read information on python frame') 
            return 
 
        filename = pyop.filename() 
        lineno = pyop.current_line_num() 
 
        if start is None: 
            start = lineno - 5 
            end = lineno + 5 
 
        if start<1: 
            start = 1 
 
        try:
            f = open(os_fsencode(filename), 'r')
        except IOError as err:
            sys.stdout.write('Unable to open %s: %s\n'
                             % (filename, err))
            return
        with f:
            all_lines = f.readlines() 
            # start and end are 1-based, all_lines is 0-based; 
            # so [start-1:end] as a python slice gives us [start, end] as a 
            # closed interval 
            for i, line in enumerate(all_lines[start-1:end]): 
                linestr = str(i+start) 
                # Highlight current line: 
                if i + start == lineno: 
                    linestr = '>' + linestr 
                sys.stdout.write('%4s    %s' % (linestr, line)) 
 

# ...and register the command: 
PyList() 
 
def move_in_stack(move_up): 
    '''Move up or down the stack (for the py-up/py-down command)''' 
    frame = Frame.get_selected_python_frame() 
    if not frame:
        print('Unable to locate python frame')
        return

    while frame: 
        if move_up: 
            iter_frame = frame.older() 
        else: 
            iter_frame = frame.newer() 
 
        if not iter_frame: 
            break 
 
        if iter_frame.is_python_frame():
            # Result: 
            if iter_frame.select(): 
                iter_frame.print_summary() 
            return 
 
        frame = iter_frame 
 
    if move_up: 
        print('Unable to find an older python frame') 
    else: 
        print('Unable to find a newer python frame') 
 
class PyUp(gdb.Command): 
    'Select and print the python stack frame that called this one (if any)' 
    def __init__(self): 
        gdb.Command.__init__ (self, 
                              "py-up", 
                              gdb.COMMAND_STACK, 
                              gdb.COMPLETE_NONE) 
 

    def invoke(self, args, from_tty): 
        move_in_stack(move_up=True) 
 
class PyDown(gdb.Command): 
    'Select and print the python stack frame called by this one (if any)' 
    def __init__(self): 
        gdb.Command.__init__ (self, 
                              "py-down", 
                              gdb.COMMAND_STACK, 
                              gdb.COMPLETE_NONE) 
 

    def invoke(self, args, from_tty): 
        move_in_stack(move_up=False) 
 
# Not all builds of gdb have gdb.Frame.select 
if hasattr(gdb.Frame, 'select'): 
    PyUp() 
    PyDown() 
 
class PyBacktraceFull(gdb.Command):
    'Display the current python frame and all the frames within its call stack (if any)'
    def __init__(self):
        gdb.Command.__init__ (self,
                              "py-bt-full",
                              gdb.COMMAND_STACK,
                              gdb.COMPLETE_NONE)
 

    def invoke(self, args, from_tty):
        frame = Frame.get_selected_python_frame()
        if not frame:
            print('Unable to locate python frame')
            return

        while frame:
            if frame.is_python_frame():
                frame.print_summary()
            frame = frame.older()

PyBacktraceFull()

class PyBacktrace(gdb.Command): 
    'Display the current python frame and all the frames within its call stack (if any)' 
    def __init__(self): 
        gdb.Command.__init__ (self, 
                              "py-bt", 
                              gdb.COMMAND_STACK, 
                              gdb.COMPLETE_NONE) 
 
 
    def invoke(self, args, from_tty): 
        frame = Frame.get_selected_python_frame() 
        if not frame:
            print('Unable to locate python frame')
            return

        sys.stdout.write('Traceback (most recent call first):\n')
        while frame: 
            if frame.is_python_frame():
                frame.print_traceback()
            frame = frame.older() 
 
PyBacktrace() 
 
class PyPrint(gdb.Command): 
    'Look up the given python variable name, and print it' 
    def __init__(self): 
        gdb.Command.__init__ (self, 
                              "py-print", 
                              gdb.COMMAND_DATA, 
                              gdb.COMPLETE_NONE) 
 

    def invoke(self, args, from_tty): 
        name = str(args) 
 
        frame = Frame.get_selected_python_frame() 
        if not frame: 
            print('Unable to locate python frame') 
            return 
 
        pyop_frame = frame.get_pyop() 
        if not pyop_frame: 
            print('Unable to read information on python frame') 
            return 
 
        pyop_var, scope = pyop_frame.get_var_by_name(name) 
 
        if pyop_var: 
            print('%s %r = %s'
                   % (scope,
                      name,
                      pyop_var.get_truncated_repr(MAX_OUTPUT_LEN)))
        else: 
            print('%r not found' % name) 
 
PyPrint() 
 
class PyLocals(gdb.Command): 
    'Look up the given python variable name, and print it' 
    def __init__(self, command="py-locals"):
        gdb.Command.__init__ (self,
                              command,
                              gdb.COMMAND_DATA,
                              gdb.COMPLETE_NONE)
 

    def invoke(self, args, from_tty): 
        name = str(args) 
 
        frame = Frame.get_selected_python_frame() 
        if not frame: 
            print('Unable to locate python frame') 
            return 
 
        pyop_frame = frame.get_pyop() 
        if not pyop_frame: 
            print('Unable to read information on python frame') 
            return 
 
        namespace = self.get_namespace(pyop_frame) 
        namespace = [(name.proxyval(set()), val) for name, val in namespace] 
 
        if namespace: 
            name, val = max(namespace, key=lambda item: len(item[0])) 
            max_name_length = len(name) 
 
            for name, pyop_value in namespace: 
                value = pyop_value.get_truncated_repr(MAX_OUTPUT_LEN) 
                print('%-*s = %s' % (max_name_length, name, value)) 
 
    def get_namespace(self, pyop_frame): 
        return pyop_frame.iter_locals() 
 
PyLocals()
 

##################################################################
## added, not in CPython
##################################################################

import re
import warnings
import tempfile
import textwrap
import itertools

class PyGlobals(PyLocals): 
    'List all the globals in the currently select Python frame' 
 
    def get_namespace(self, pyop_frame): 
        return pyop_frame.iter_globals() 
 
 
PyGlobals("py-globals")
 
 
class PyNameEquals(gdb.Function): 
 
    def _get_pycurframe_attr(self, attr): 
        frame = Frame(gdb.selected_frame()) 
        if frame.is_evalframeex(): 
            pyframe = frame.get_pyop() 
            if pyframe is None: 
                warnings.warn("Use a Python debug build, Python breakpoints " 
                              "won't work otherwise.") 
                return None 
 
            return getattr(pyframe, attr).proxyval(set()) 
 
        return None 
 
    def invoke(self, funcname): 
        attr = self._get_pycurframe_attr('co_name') 
        return attr is not None and attr == funcname.string() 
 
PyNameEquals("pyname_equals") 
 
 
class PyModEquals(PyNameEquals): 
 
    def invoke(self, modname): 
        attr = self._get_pycurframe_attr('co_filename') 
        if attr is not None: 
            filename, ext = os.path.splitext(os.path.basename(attr)) 
            return filename == modname.string() 
        return False 
 
PyModEquals("pymod_equals") 
 
 
class PyBreak(gdb.Command): 
    """ 
    Set a Python breakpoint. Examples: 
 
    Break on any function or method named 'func' in module 'modname' 
 
        py-break modname.func 
 
    Break on any function or method named 'func' 
 
        py-break func 
    """ 
 
    def invoke(self, funcname, from_tty): 
        if '.' in funcname: 
            modname, dot, funcname = funcname.rpartition('.') 
            cond = '$pyname_equals("%s") && $pymod_equals("%s")' % (funcname, 
                                                                    modname) 
        else: 
            cond = '$pyname_equals("%s")' % funcname 
 
        gdb.execute('break PyEval_EvalFrameEx if ' + cond) 
 
PyBreak("py-break", gdb.COMMAND_RUNNING, gdb.COMPLETE_NONE) 
 
 
class _LoggingState(object): 
    """ 
    State that helps to provide a reentrant gdb.execute() function. 
    """ 
 
    def __init__(self): 
        f = tempfile.NamedTemporaryFile('r+')
        self.file = f
        self.filename = f.name
        self.fd = f.fileno()
        _execute("set logging file %s" % self.filename) 
        self.file_position_stack = [] 
 
    def __enter__(self): 
        if not self.file_position_stack: 
            _execute("set logging redirect on") 
            _execute("set logging on") 
            _execute("set pagination off") 
 
        self.file_position_stack.append(os.fstat(self.fd).st_size) 
        return self 
 
    def getoutput(self): 
        gdb.flush() 
        self.file.seek(self.file_position_stack[-1]) 
        result = self.file.read() 
        return result 
 
    def __exit__(self, exc_type, exc_val, tb): 
        startpos = self.file_position_stack.pop() 
        self.file.seek(startpos) 
        self.file.truncate() 
        if not self.file_position_stack: 
            _execute("set logging off") 
            _execute("set logging redirect off") 
            _execute("set pagination on") 
 
 
def execute(command, from_tty=False, to_string=False): 
    """ 
    Replace gdb.execute() with this function and have it accept a 'to_string' 
    argument (new in 7.2). Have it properly capture stderr also. Ensure 
    reentrancy. 
    """ 
    if to_string: 
        with _logging_state as state: 
            _execute(command, from_tty) 
            return state.getoutput() 
    else: 
        _execute(command, from_tty) 
 
 
_execute = gdb.execute 
gdb.execute = execute 
_logging_state = _LoggingState() 
 
 
def get_selected_inferior(): 
    """ 
    Return the selected inferior in gdb. 
    """ 
    # Woooh, another bug in gdb! Is there an end in sight? 
    # http://sourceware.org/bugzilla/show_bug.cgi?id=12212 
    return gdb.inferiors()[0] 
 
    selected_thread = gdb.selected_thread() 
 
    for inferior in gdb.inferiors(): 
        for thread in inferior.threads(): 
            if thread == selected_thread: 
                return inferior 
 
 
def source_gdb_script(script_contents, to_string=False): 
    """ 
    Source a gdb script with script_contents passed as a string. This is useful 
    to provide defines for py-step and py-next to make them repeatable (this is 
    not possible with gdb.execute()). See 
    http://sourceware.org/bugzilla/show_bug.cgi?id=12216 
    """ 
    fd, filename = tempfile.mkstemp() 
    f = os.fdopen(fd, 'w') 
    f.write(script_contents) 
    f.close() 
    gdb.execute("source %s" % filename, to_string=to_string) 
    os.remove(filename) 
 
 
def register_defines(): 
    source_gdb_script(textwrap.dedent("""\ 
        define py-step 
        -py-step 
        end 
 
        define py-next 
        -py-next 
        end 
 
        document py-step 
        %s 
        end 
 
        document py-next 
        %s 
        end 
    """) % (PyStep.__doc__, PyNext.__doc__)) 
 
 
def stackdepth(frame): 
    "Tells the stackdepth of a gdb frame." 
    depth = 0 
    while frame: 
        frame = frame.older() 
        depth += 1 
 
    return depth 
 
 
class ExecutionControlCommandBase(gdb.Command): 
    """ 
    Superclass for language specific execution control. Language specific 
    features should be implemented by lang_info using the LanguageInfo 
    interface. 'name' is the name of the command. 
    """ 
 
    def __init__(self, name, lang_info): 
        super(ExecutionControlCommandBase, self).__init__( 
                                name, gdb.COMMAND_RUNNING, gdb.COMPLETE_NONE) 
        self.lang_info = lang_info 
 
    def install_breakpoints(self): 
        all_locations = itertools.chain( 
            self.lang_info.static_break_functions(), 
            self.lang_info.runtime_break_functions()) 
 
        for location in all_locations: 
            result = gdb.execute('break %s' % location, to_string=True) 
            yield re.search(r'Breakpoint (\d+)', result).group(1) 
 
    def delete_breakpoints(self, breakpoint_list): 
        for bp in breakpoint_list: 
            gdb.execute("delete %s" % bp) 
 
    def filter_output(self, result): 
        reflags = re.MULTILINE 
 
        output_on_halt = [ 
            (r'^Program received signal .*', reflags|re.DOTALL), 
            (r'.*[Ww]arning.*', 0), 
            (r'^Program exited .*', reflags), 
        ] 
 
        output_always = [ 
            # output when halting on a watchpoint 
            (r'^(Old|New) value = .*', reflags), 
            # output from the 'display' command 
            (r'^\d+: \w+ = .*', reflags), 
        ] 
 
        def filter_output(regexes): 
            output = [] 
            for regex, flags in regexes: 
                for match in re.finditer(regex, result, flags): 
                    output.append(match.group(0)) 
 
            return '\n'.join(output) 
 
        # Filter the return value output of the 'finish' command 
        match_finish = re.search(r'^Value returned is \$\d+ = (.*)', result, 
                                 re.MULTILINE) 
        if match_finish: 
            finish_output = 'Value returned: %s\n' % match_finish.group(1) 
        else: 
            finish_output = '' 
 
        return (filter_output(output_on_halt), 
                finish_output + filter_output(output_always)) 
 
    def stopped(self): 
        return get_selected_inferior().pid == 0 
 
    def finish_executing(self, result): 
        """ 
        After doing some kind of code running in the inferior, print the line 
        of source code or the result of the last executed gdb command (passed 
        in as the `result` argument). 
        """ 
        output_on_halt, output_always = self.filter_output(result) 
 
        if self.stopped(): 
            print(output_always) 
            print(output_on_halt) 
        else: 
            frame = gdb.selected_frame() 
            source_line = self.lang_info.get_source_line(frame) 
            if self.lang_info.is_relevant_function(frame): 
                raised_exception = self.lang_info.exc_info(frame) 
                if raised_exception: 
                    print(raised_exception) 
 
            if source_line: 
                if output_always.rstrip(): 
                    print(output_always.rstrip()) 
                print(source_line) 
            else: 
                print(result) 
 
    def _finish(self): 
        """ 
        Execute until the function returns (or until something else makes it 
        stop) 
        """ 
        if gdb.selected_frame().older() is not None: 
            return gdb.execute('finish', to_string=True) 
        else: 
            # outermost frame, continue 
            return gdb.execute('cont', to_string=True) 
 
    def _finish_frame(self): 
        """ 
        Execute until the function returns to a relevant caller. 
        """ 
        while True: 
            result = self._finish() 
 
            try: 
                frame = gdb.selected_frame() 
            except RuntimeError: 
                break 
 
            hitbp = re.search(r'Breakpoint (\d+)', result) 
            is_relevant = self.lang_info.is_relevant_function(frame) 
            if hitbp or is_relevant or self.stopped(): 
                break 
 
        return result 
 
    def finish(self, *args): 
        "Implements the finish command." 
        result = self._finish_frame() 
        self.finish_executing(result) 
 
    def step(self, stepinto, stepover_command='next'): 
        """ 
        Do a single step or step-over. Returns the result of the last gdb 
        command that made execution stop. 
 
        This implementation, for stepping, sets (conditional) breakpoints for 
        all functions that are deemed relevant. It then does a step over until 
        either something halts execution, or until the next line is reached. 
 
        If, however, stepover_command is given, it should be a string gdb 
        command that continues execution in some way. The idea is that the 
        caller has set a (conditional) breakpoint or watchpoint that can work 
        more efficiently than the step-over loop. For Python this means setting 
        a watchpoint for f->f_lasti, which means we can then subsequently 
        "finish" frames. 
        We want f->f_lasti instead of f->f_lineno, because the latter only 
        works properly with local trace functions, see 
        PyFrameObjectPtr.current_line_num and PyFrameObjectPtr.addr2line. 
        """ 
        if stepinto: 
            breakpoint_list = list(self.install_breakpoints()) 
 
        beginframe = gdb.selected_frame() 
 
        if self.lang_info.is_relevant_function(beginframe): 
            # If we start in a relevant frame, initialize stuff properly. If 
            # we don't start in a relevant frame, the loop will halt 
            # immediately. So don't call self.lang_info.lineno() as it may 
            # raise for irrelevant frames. 
            beginline = self.lang_info.lineno(beginframe) 
 
            if not stepinto: 
                depth = stackdepth(beginframe) 
 
        newframe = beginframe 
 
        while True: 
            if self.lang_info.is_relevant_function(newframe): 
                result = gdb.execute(stepover_command, to_string=True) 
            else: 
                result = self._finish_frame() 
 
            if self.stopped(): 
                break 
 
            newframe = gdb.selected_frame() 
            is_relevant_function = self.lang_info.is_relevant_function(newframe) 
            try: 
                framename = newframe.name() 
            except RuntimeError: 
                framename = None 
 
            m = re.search(r'Breakpoint (\d+)', result) 
            if m: 
                if is_relevant_function and m.group(1) in breakpoint_list: 
                    # although we hit a breakpoint, we still need to check 
                    # that the function, in case hit by a runtime breakpoint, 
                    # is in the right context 
                    break 
 
            if newframe != beginframe: 
                # new function 
 
                if not stepinto: 
                    # see if we returned to the caller 
                    newdepth = stackdepth(newframe) 
                    is_relevant_function = (newdepth < depth and 
                                            is_relevant_function) 
 
                if is_relevant_function: 
                    break 
            else: 
                # newframe equals beginframe, check for a difference in the 
                # line number 
                lineno = self.lang_info.lineno(newframe) 
                if lineno and lineno != beginline: 
                    break 
 
        if stepinto: 
            self.delete_breakpoints(breakpoint_list) 
 
        self.finish_executing(result) 
 
    def run(self, args, from_tty): 
        self.finish_executing(gdb.execute('run ' + args, to_string=True)) 
 
    def cont(self, *args): 
        self.finish_executing(gdb.execute('cont', to_string=True)) 
 
 
class LanguageInfo(object): 
    """ 
    This class defines the interface that ExecutionControlCommandBase needs to 
    provide language-specific execution control. 
 
    Classes that implement this interface should implement: 
 
        lineno(frame) 
            Tells the current line number (only called for a relevant frame). 
            If lineno is a false value it is not checked for a difference. 
 
        is_relevant_function(frame) 
            tells whether we care about frame 'frame' 
 
        get_source_line(frame) 
            get the line of source code for the current line (only called for a 
            relevant frame). If the source code cannot be retrieved this 
            function should return None 
 
        exc_info(frame) -- optional 
            tells whether an exception was raised, if so, it should return a 
            string representation of the exception value, None otherwise. 
 
        static_break_functions() 
            returns an iterable of function names that are considered relevant 
            and should halt step-into execution. This is needed to provide a 
            performing step-into 
 
        runtime_break_functions() -- optional 
            list of functions that we should break into depending on the 
            context 
    """ 
 
    def exc_info(self, frame): 
        "See this class' docstring." 
 
    def runtime_break_functions(self): 
        """ 
        Implement this if the list of step-into functions depends on the 
        context. 
        """ 
        return () 
 
 
class PythonInfo(LanguageInfo): 
 
    def pyframe(self, frame): 
        pyframe = Frame(frame).get_pyop() 
        if pyframe: 
            return pyframe 
        else: 
            raise gdb.RuntimeError( 
                "Unable to find the Python frame, run your code with a debug " 
                "build (configure with --with-pydebug or compile with -g).") 
 
    def lineno(self, frame): 
        return self.pyframe(frame).current_line_num() 
 
    def is_relevant_function(self, frame): 
        return Frame(frame).is_evalframeex() 
 
    def get_source_line(self, frame): 
        try: 
            pyframe = self.pyframe(frame) 
            return '%4d    %s' % (pyframe.current_line_num(), 
                                  pyframe.current_line().rstrip()) 
        except IOError: 
            return None 
 
    def exc_info(self, frame): 
        try: 
            tstate = frame.read_var('tstate').dereference() 
            if gdb.parse_and_eval('tstate->frame == f'): 
                # tstate local variable initialized, check for an exception 
                inf_type = tstate['curexc_type'] 
                inf_value = tstate['curexc_value'] 
 
                if inf_type: 
                    return 'An exception was raised: %s' % (inf_value,) 
        except (ValueError, RuntimeError): 
            # Could not read the variable tstate or it's memory, it's ok 
            pass 
 
    def static_break_functions(self): 
        yield 'PyEval_EvalFrameEx' 
 
 
class PythonStepperMixin(object): 
    """ 
    Make this a mixin so CyStep can also inherit from this and use a 
    CythonCodeStepper at the same time. 
    """ 
 
    def python_step(self, stepinto): 
        """ 
        Set a watchpoint on the Python bytecode instruction pointer and try 
        to finish the frame 
        """ 
        output = gdb.execute('watch f->f_lasti', to_string=True) 
        watchpoint = int(re.search(r'[Ww]atchpoint (\d+):', output).group(1)) 
        self.step(stepinto=stepinto, stepover_command='finish') 
        gdb.execute('delete %s' % watchpoint) 
 
 
class PyStep(ExecutionControlCommandBase, PythonStepperMixin): 
    "Step through Python code." 
 
    stepinto = True 
 
    def invoke(self, args, from_tty): 
        self.python_step(stepinto=self.stepinto) 
 
 
class PyNext(PyStep): 
    "Step-over Python code." 
 
    stepinto = False 
 
 
class PyFinish(ExecutionControlCommandBase): 
    "Execute until function returns to a caller." 
 
    invoke = ExecutionControlCommandBase.finish 
 
 
class PyRun(ExecutionControlCommandBase): 
    "Run the program." 
 
    invoke = ExecutionControlCommandBase.run 
 
 
class PyCont(ExecutionControlCommandBase): 
 
    invoke = ExecutionControlCommandBase.cont 
 
 
def _pointervalue(gdbval): 
    """ 
    Return the value of the pointer as a Python int.
 
    gdbval.type must be a pointer type 
    """ 
    # don't convert with int() as it will raise a RuntimeError 
    if gdbval.address is not None: 
        return int(gdbval.address)
    else: 
        # the address attribute is None sometimes, in which case we can 
        # still convert the pointer to an int 
        return int(gdbval)
 
 
def pointervalue(gdbval): 
    pointer = _pointervalue(gdbval) 
    try: 
        if pointer < 0: 
            raise gdb.GdbError("Negative pointer value, presumably a bug " 
                               "in gdb, aborting.") 
    except RuntimeError: 
        # work around yet another bug in gdb where you get random behaviour 
        # and tracebacks 
        pass 
 
    return pointer 
 
 
def get_inferior_unicode_postfix(): 
    try: 
        gdb.parse_and_eval('PyUnicode_FromEncodedObject') 
    except RuntimeError: 
        try: 
            gdb.parse_and_eval('PyUnicodeUCS2_FromEncodedObject') 
        except RuntimeError: 
            return 'UCS4' 
        else: 
            return 'UCS2' 
    else: 
        return '' 
 
 
class PythonCodeExecutor(object): 
 
    Py_single_input = 256 
    Py_file_input = 257 
    Py_eval_input = 258 
 
    def malloc(self, size): 
        chunk = (gdb.parse_and_eval("(void *) malloc((size_t) %d)" % size)) 
 
        pointer = pointervalue(chunk) 
        if pointer == 0: 
            raise gdb.GdbError("No memory could be allocated in the inferior.") 
 
        return pointer 
 
    def alloc_string(self, string): 
        pointer = self.malloc(len(string)) 
        get_selected_inferior().write_memory(pointer, string) 
 
        return pointer 
 
    def alloc_pystring(self, string): 
        stringp = self.alloc_string(string) 
        PyString_FromStringAndSize = 'PyString_FromStringAndSize' 
 
        try: 
            gdb.parse_and_eval(PyString_FromStringAndSize) 
        except RuntimeError: 
            # Python 3 
            PyString_FromStringAndSize = ('PyUnicode%s_FromStringAndSize' % 
                                               (get_inferior_unicode_postfix(),)) 
 
        try: 
            result = gdb.parse_and_eval( 
                '(PyObject *) %s((char *) %d, (size_t) %d)' % ( 
                            PyString_FromStringAndSize, stringp, len(string))) 
        finally: 
            self.free(stringp) 
 
        pointer = pointervalue(result) 
        if pointer == 0: 
            raise gdb.GdbError("Unable to allocate Python string in " 
                               "the inferior.") 
 
        return pointer 
 
    def free(self, pointer): 
        gdb.parse_and_eval("free((void *) %d)" % pointer) 
 
    def incref(self, pointer): 
        "Increment the reference count of a Python object in the inferior." 
        gdb.parse_and_eval('Py_IncRef((PyObject *) %d)' % pointer) 
 
    def xdecref(self, pointer): 
        "Decrement the reference count of a Python object in the inferior." 
        # Py_DecRef is like Py_XDECREF, but a function. So we don't have 
        # to check for NULL. This should also decref all our allocated 
        # Python strings. 
        gdb.parse_and_eval('Py_DecRef((PyObject *) %d)' % pointer) 
 
    def evalcode(self, code, input_type, global_dict=None, local_dict=None): 
        """ 
        Evaluate python code `code` given as a string in the inferior and 
        return the result as a gdb.Value. Returns a new reference in the 
        inferior. 
 
        Of course, executing any code in the inferior may be dangerous and may 
        leave the debuggee in an unsafe state or terminate it altogether.
        """ 
        if '\0' in code: 
            raise gdb.GdbError("String contains NUL byte.") 
 
        code += '\0' 
 
        pointer = self.alloc_string(code) 
 
        globalsp = pointervalue(global_dict) 
        localsp = pointervalue(local_dict) 
 
        if globalsp == 0 or localsp == 0: 
            raise gdb.GdbError("Unable to obtain or create locals or globals.") 
 
        code = """ 
            PyRun_String( 
                (char *) %(code)d, 
                (int) %(start)d, 
                (PyObject *) %(globals)s, 
                (PyObject *) %(locals)d) 
        """ % dict(code=pointer, start=input_type, 
                   globals=globalsp, locals=localsp) 
 
        with FetchAndRestoreError(): 
            try: 
                pyobject_return_value = gdb.parse_and_eval(code) 
            finally: 
                self.free(pointer) 
 
        return pyobject_return_value 
 
 
class FetchAndRestoreError(PythonCodeExecutor): 
    """ 
    Context manager that fetches the error indicator in the inferior and 
    restores it on exit. 
    """ 
 
    def __init__(self): 
        self.sizeof_PyObjectPtr = gdb.lookup_type('PyObject').pointer().sizeof 
        self.pointer = self.malloc(self.sizeof_PyObjectPtr * 3) 
 
        type = self.pointer 
        value = self.pointer + self.sizeof_PyObjectPtr 
        traceback = self.pointer + self.sizeof_PyObjectPtr * 2 
 
        self.errstate = type, value, traceback 
 
    def __enter__(self): 
        gdb.parse_and_eval("PyErr_Fetch(%d, %d, %d)" % self.errstate) 
 
    def __exit__(self, *args): 
        if gdb.parse_and_eval("(int) PyErr_Occurred()"): 
            gdb.parse_and_eval("PyErr_Print()") 
 
        pyerr_restore = ("PyErr_Restore(" 
                            "(PyObject *) *%d," 
                            "(PyObject *) *%d," 
                            "(PyObject *) *%d)") 
 
        try: 
            gdb.parse_and_eval(pyerr_restore % self.errstate) 
        finally: 
            self.free(self.pointer) 
 
 
class FixGdbCommand(gdb.Command): 
 
    def __init__(self, command, actual_command): 
        super(FixGdbCommand, self).__init__(command, gdb.COMMAND_DATA, 
                                            gdb.COMPLETE_NONE) 
        self.actual_command = actual_command 
 
    def fix_gdb(self): 
        """ 
        It seems that invoking either 'cy exec' and 'py-exec' work perfectly
        fine, but after this gdb's python API is entirely broken.
        Maybe some uncleared exception value is still set? 
        sys.exc_clear() didn't help. A demonstration: 
 
        (gdb) cy exec 'hello' 
        'hello' 
        (gdb) python gdb.execute('cont') 
        RuntimeError: Cannot convert value to int. 
        Error while executing Python code. 
        (gdb) python gdb.execute('cont') 
        [15148 refs] 
 
        Program exited normally. 
        """ 
        warnings.filterwarnings('ignore', r'.*', RuntimeWarning, 
                                re.escape(__name__)) 
        try: 
            int(gdb.parse_and_eval("(void *) 0")) == 0
        except RuntimeError: 
            pass 
        # warnings.resetwarnings() 
 
    def invoke(self, args, from_tty): 
        self.fix_gdb() 
        try: 
            gdb.execute('%s %s' % (self.actual_command, args)) 
        except RuntimeError as e: 
            raise gdb.GdbError(str(e)) 
        self.fix_gdb() 
 
 
def _evalcode_python(executor, code, input_type): 
    """ 
    Execute Python code in the most recent stack frame. 
    """ 
    global_dict = gdb.parse_and_eval('PyEval_GetGlobals()') 
    local_dict = gdb.parse_and_eval('PyEval_GetLocals()') 
 
    if (pointervalue(global_dict) == 0 or pointervalue(local_dict) == 0): 
        raise gdb.GdbError("Unable to find the locals or globals of the " 
                           "most recent Python function (relative to the " 
                           "selected frame).") 
 
    return executor.evalcode(code, input_type, global_dict, local_dict) 
 
 
class PyExec(gdb.Command): 
 
    def readcode(self, expr): 
        if expr: 
            return expr, PythonCodeExecutor.Py_single_input 
        else: 
            lines = [] 
            while True: 
                try: 
                    line = input('>')
                except EOFError: 
                    break 
                else: 
                    if line.rstrip() == 'end': 
                        break 
 
                    lines.append(line) 
 
            return '\n'.join(lines), PythonCodeExecutor.Py_file_input 
 
    def invoke(self, expr, from_tty): 
        expr, input_type = self.readcode(expr) 
        executor = PythonCodeExecutor() 
        executor.xdecref(_evalcode_python(executor, input_type, global_dict, local_dict)) 
 
 
gdb.execute('set breakpoint pending on') 
 
if hasattr(gdb, 'GdbError'): 
     # Wrap py-step and py-next in gdb defines to make them repeatable. 
    py_step = PyStep('-py-step', PythonInfo()) 
    py_next = PyNext('-py-next', PythonInfo()) 
    register_defines() 
    py_finish = PyFinish('py-finish', PythonInfo()) 
    py_run = PyRun('py-run', PythonInfo()) 
    py_cont = PyCont('py-cont', PythonInfo()) 
 
    py_exec = FixGdbCommand('py-exec', '-py-exec') 
    _py_exec = PyExec("-py-exec", gdb.COMMAND_DATA, gdb.COMPLETE_NONE) 
else: 
    warnings.warn("Use gdb 7.2 or higher to use the py-exec command.")