sdb_debugger 0.2.2

Book: Building a Debugger. Rust port of C++ debugger sdb
Documentation
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
use std::cell::{OnceCell, Ref, RefCell};
use std::ffi::CStr;
use std::path::{Path, PathBuf};
use std::rc::Weak;
use std::{collections::HashMap, ops::AddAssign, rc::Rc};

use bytemuck::Pod;
use bytes::Bytes;
use gimli::{
    DW_AT_abstract_origin, DW_AT_bit_offset, DW_AT_bit_size, DW_AT_byte_size, DW_AT_call_file,
    DW_AT_call_line, DW_AT_comp_dir, DW_AT_data_bit_offset, DW_AT_decl_file, DW_AT_decl_line,
    DW_AT_frame_base, DW_AT_high_pc, DW_AT_location, DW_AT_low_pc, DW_AT_name, DW_AT_ranges,
    DW_AT_sibling, DW_AT_specification, DW_AT_stmt_list, DW_AT_type, DW_FORM_addr, DW_FORM_block,
    DW_FORM_block1, DW_FORM_block2, DW_FORM_block4, DW_FORM_data1, DW_FORM_data2, DW_FORM_data4,
    DW_FORM_data8, DW_FORM_exprloc, DW_FORM_flag, DW_FORM_flag_present, DW_FORM_indirect,
    DW_FORM_ref_addr, DW_FORM_ref_udata, DW_FORM_ref1, DW_FORM_ref2, DW_FORM_ref4, DW_FORM_ref8,
    DW_FORM_sdata, DW_FORM_sec_offset, DW_FORM_string, DW_FORM_strp, DW_FORM_udata,
    DW_LNE_define_file, DW_LNE_end_sequence, DW_LNE_set_address, DW_LNE_set_discriminator,
    DW_LNS_advance_line, DW_LNS_advance_pc, DW_LNS_const_add_pc, DW_LNS_copy,
    DW_LNS_fixed_advance_pc, DW_LNS_negate_stmt, DW_LNS_set_basic_block, DW_LNS_set_column,
    DW_LNS_set_epilogue_begin, DW_LNS_set_file, DW_LNS_set_isa, DW_LNS_set_prologue_end, DW_OP_abs,
    DW_OP_addr, DW_OP_and, DW_OP_bit_piece, DW_OP_bra, DW_OP_breg0, DW_OP_breg31, DW_OP_bregx,
    DW_OP_call_frame_cfa, DW_OP_call_ref, DW_OP_call2, DW_OP_call4, DW_OP_const1s, DW_OP_const1u,
    DW_OP_const2s, DW_OP_const2u, DW_OP_const4s, DW_OP_const4u, DW_OP_const8s, DW_OP_const8u,
    DW_OP_consts, DW_OP_constu, DW_OP_deref, DW_OP_deref_size, DW_OP_div, DW_OP_drop, DW_OP_dup,
    DW_OP_eq, DW_OP_fbreg, DW_OP_form_tls_address, DW_OP_ge, DW_OP_gt, DW_OP_implicit_value,
    DW_OP_le, DW_OP_lit0, DW_OP_lit31, DW_OP_lt, DW_OP_minus, DW_OP_mod, DW_OP_mul, DW_OP_ne,
    DW_OP_neg, DW_OP_nop, DW_OP_not, DW_OP_or, DW_OP_over, DW_OP_pick, DW_OP_piece, DW_OP_plus,
    DW_OP_plus_uconst, DW_OP_push_object_address, DW_OP_reg0, DW_OP_reg31, DW_OP_regx, DW_OP_rot,
    DW_OP_shl, DW_OP_shr, DW_OP_shra, DW_OP_skip, DW_OP_stack_value, DW_OP_swap, DW_OP_xderef,
    DW_OP_xderef_size, DW_OP_xor, DW_TAG_formal_parameter, DW_TAG_inlined_subroutine,
    DW_TAG_subprogram, DW_TAG_variable, DwForm, DwLne, DwLns, DwOp,
};
use multimap::MultiMap;
use typed_builder::TypedBuilder;

use super::bit::from_bytes;
use super::breakpoint::{CallFrameInformation, parse_eh_hdr};
use super::elf::Elf;
use super::process::Process;
use super::register_info::{RegisterId, register_info_by_dwarf};
use super::registers::{RegisterValue, Registers};
use super::sdb_error::SdbError;
use super::types::SdbType;
use super::types::{FileAddress, VirtualAddress};

type AbbrevTable = HashMap<u64, Rc<Abbrev>>;

#[derive(Debug, Clone)]
pub struct SourceLocation {
    pub file: Rc<LineTableFile>,
    pub line: u64,
}

#[derive(Debug, Clone, TypedBuilder, Default)]
pub struct LineTableEntry {
    #[builder(default = FileAddress::null())]
    pub address: FileAddress,
    #[builder(default = 1)]
    pub file_index: u64,
    #[builder(default = 1)]
    pub line: u64,
    #[builder(default = 0)]
    pub column: u64,
    #[builder(default = false)]
    pub is_stmt: bool,
    #[builder(default = false)]
    pub basic_block_start: bool,
    #[builder(default = false)]
    pub end_sequence: bool,
    #[builder(default = false)]
    pub prologue_end: bool,
    #[builder(default = false)]
    pub epilogue_begin: bool,
    #[builder(default = 0)]
    pub discriminator: u64,
    #[builder(default = None)]
    pub file_entry: Option<Rc<LineTableFile>>,
}

impl PartialEq for LineTableEntry {
    fn eq(&self, other: &Self) -> bool {
        self.address == other.address
            && self.file_index == other.file_index
            && self.line == other.line
            && self.column == other.column
            && self.discriminator == other.discriminator
    }
}

impl Eq for LineTableEntry {}

#[derive(Debug, Clone, Default)]
pub struct LineTableIter {
    table: Rc<LineTable>,
    current: Option<LineTableEntry>,
    registers: LineTableEntry,
    pos: Bytes,
}

impl PartialEq for LineTableIter {
    fn eq(&self, other: &Self) -> bool {
        self.current == other.current && self.pos == other.pos
    }
}

impl LineTableIter {
    pub fn new(table: &Rc<LineTable>) -> Result<Self, SdbError> {
        let registers = LineTableEntry::builder()
            .is_stmt(table.default_is_stmt)
            .build();
        let mut ret = Self {
            table: table.clone(),
            current: None,
            registers,
            pos: table.data.clone(),
        };
        ret.step()?;
        Ok(ret)
    }

    pub fn step(&mut self) -> Result<(), SdbError> {
        if self.pos.is_empty() {
            self.pos = Bytes::new();
            return Ok(());
        }
        let mut emitted;
        loop {
            emitted = self.execute_instruction()?;
            if emitted {
                break;
            }
        }
        self.current.as_mut().unwrap().file_entry = Some(Rc::new(
            self.table.file_names()[self.get_current().file_index as usize - 1].clone(),
        ));
        Ok(())
    }

    pub fn is_end(&self) -> bool {
        self.pos.is_empty()
    }

    pub fn get_current(&self) -> &LineTableEntry {
        self.current.as_ref().unwrap()
    }

    #[allow(non_upper_case_globals)]
    pub fn execute_instruction(&mut self) -> Result<bool, SdbError> {
        let elf = self.table.cu().dwarf_info().elf_file();
        let mut cursor = Cursor::new(&self.pos.slice(
            ..(self.table.data.as_ptr() as usize + self.table.data.len()
                - self.pos.as_ptr() as usize),
        ));
        let opcode = cursor.u8();
        let mut emitted = false;
        if opcode > 0 && opcode < self.table.opcode_base {
            match DwLns(opcode) {
                DW_LNS_copy => {
                    self.current = Some(self.registers.clone());
                    self.registers.basic_block_start = false;
                    self.registers.prologue_end = false;
                    self.registers.epilogue_begin = false;
                    self.registers.discriminator = 0;
                    emitted = true;
                }
                DW_LNS_advance_pc => {
                    self.registers.address += cursor.uleb128() as i64;
                }
                DW_LNS_advance_line => {
                    self.registers.line =
                        (self.registers.line as i64).wrapping_add(cursor.sleb128()) as u64;
                }
                DW_LNS_set_file => {
                    self.registers.file_index = cursor.uleb128();
                }
                DW_LNS_set_column => {
                    self.registers.column = cursor.uleb128();
                }
                DW_LNS_negate_stmt => {
                    self.registers.is_stmt = !self.registers.is_stmt;
                }
                DW_LNS_set_basic_block => {
                    self.registers.basic_block_start = true;
                }
                DW_LNS_const_add_pc => {
                    self.registers.address +=
                        ((255 - self.table.opcode_base) / self.table.line_range) as i64;
                }
                DW_LNS_fixed_advance_pc => {
                    self.registers.address += cursor.u16() as i64;
                }
                DW_LNS_set_prologue_end => {
                    self.registers.prologue_end = true;
                }
                DW_LNS_set_epilogue_begin => {
                    self.registers.epilogue_begin = true;
                }
                DW_LNS_set_isa => {
                    // Do nothing
                }
                _ => {
                    return SdbError::err("Unexpected standard opcode");
                }
            }
        } else if opcode == 0 {
            let _length = cursor.uleb128();
            let extended_opcode = cursor.u8();
            match DwLne(extended_opcode) {
                DW_LNE_end_sequence => {
                    self.registers.end_sequence = true;
                    self.current = Some(self.registers.clone());
                    self.registers = LineTableEntry::builder()
                        .is_stmt(self.table.default_is_stmt)
                        .build();
                    emitted = true;
                }
                DW_LNE_set_address => {
                    self.registers.address = FileAddress::new(&elf, cursor.u64());
                }
                DW_LNE_define_file => {
                    let compilation_dir = self
                        .table
                        .cu()
                        .root()
                        .index(DW_AT_comp_dir.0 as u64)?
                        .as_string()?;
                    let file = parse_line_table_file(
                        &mut cursor,
                        &compilation_dir,
                        &self.table.include_directories,
                    );
                    self.table.file_names.borrow_mut().push(file);
                }
                DW_LNE_set_discriminator => {
                    self.registers.discriminator = cursor.uleb128();
                }
                _ => {
                    return SdbError::err("Unexpected extended opcode");
                }
            }
        } else {
            let adjusted_opcode = opcode - self.table.opcode_base;
            self.registers.address += (adjusted_opcode / self.table.line_range) as i64;
            self.registers.line = self.registers.line.wrapping_add(
                ((self.table.line_base as i16)
                    .wrapping_add((adjusted_opcode % self.table.line_range) as i16))
                    as u64,
            );
            self.current = Some(self.registers.clone());
            self.registers.basic_block_start = false;
            self.registers.prologue_end = false;
            self.registers.epilogue_begin = false;
            self.registers.discriminator = 0;
            emitted = true;
        }
        self.pos = cursor.position();
        Ok(emitted)
    }
}

impl Iterator for LineTableIter {
    type Item = LineTableEntry;

    fn next(&mut self) -> Option<Self::Item> {
        if !self.is_end() {
            let ret = self.get_current().clone();
            self.step().unwrap();
            Some(ret)
        } else {
            None
        }
    }
}

#[derive(Debug, Clone)]
pub struct LineTableFile {
    pub path: PathBuf,
    pub modification_time: u64,
    pub file_length: u64,
}

#[derive(Debug, Default)]
pub struct LineTable {
    data: Bytes,
    cu: Weak<CompileUnit>,
    default_is_stmt: bool,
    line_base: i8,
    line_range: u8,
    opcode_base: u8,
    include_directories: Vec<PathBuf>,
    file_names: RefCell<Vec<LineTableFile>>,
}

impl LineTable {
    pub fn get_entry_by_address(
        self: &Rc<Self>,
        address: &FileAddress,
    ) -> Result<LineTableIter, SdbError> {
        let mut prev = LineTableIter::new(self)?;
        if prev.current.is_none() {
            return Ok(LineTableIter::default());
        }
        let mut it = prev.clone();
        it.step()?;
        while it.current.is_some() {
            if prev.get_current().address <= *address
                && it.get_current().address > *address
                && !prev.get_current().end_sequence
            {
                return Ok(prev);
            }
            prev = it.clone();
            it.step()?;
        }
        Ok(LineTableIter::default())
    }

    pub fn get_entries_by_line(
        self: &Rc<Self>,
        path: &Path,
        line: u64,
    ) -> Result<Vec<LineTableIter>, SdbError> {
        let mut entries = Vec::new();
        let mut it = LineTableIter::new(self)?;
        while !it.is_end() {
            let entry_path = &it.get_current().file_entry.as_ref().unwrap().path;
            #[allow(clippy::collapsible_if)]
            if it.get_current().line == line {
                if (path.is_absolute() && entry_path == path)
                    || (path.is_relative() && entry_path.ends_with(path))
                {
                    entries.push(it.clone());
                }
            }
            it.step()?;
        }
        Ok(entries)
    }

    pub fn iter(self: &Rc<Self>) -> Result<LineTableIter, SdbError> {
        LineTableIter::new(self)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn new(
        data: Bytes,
        cu: &Rc<CompileUnit>,
        default_is_stmt: bool,
        line_base: i8,
        line_range: u8,
        opcode_base: u8,
        include_directories: Vec<PathBuf>,
        file_names: Vec<LineTableFile>,
    ) -> Rc<Self> {
        Rc::new(Self {
            data,
            cu: Rc::downgrade(cu),
            default_is_stmt,
            line_base,
            line_range,
            opcode_base,
            include_directories,
            file_names: RefCell::new(file_names),
        })
    }

    pub fn cu(&self) -> Rc<CompileUnit> {
        self.cu.upgrade().unwrap()
    }

    pub fn file_names(&self) -> Ref<Vec<LineTableFile>> {
        self.file_names.borrow()
    }
}

#[derive(Debug, Clone)]
pub struct Die {
    pos: Bytes,
    cu: Weak<CompileUnit>,
    abbrev: Option<Rc<Abbrev>>,
    next: Bytes,
    attr_locs: Vec<Bytes>,
}

#[derive(Debug, Clone, Copy)]
pub struct BitfieldInformation {
    pub bit_size: u64,
    pub storage_byte_size: u64,
    pub bit_offset: u8,
}

impl Die {
    pub fn parameter_types(self: &Rc<Self>) -> Result<Vec<SdbType>, SdbError> {
        let mut ret = Vec::new();
        if self.abbrev_entry().tag as u16 != DW_TAG_subprogram.0 {
            return Ok(ret);
        }
        for c in self.children() {
            if c.abbrev_entry().tag as u16 == DW_TAG_formal_parameter.0 {
                ret.push(c.index(DW_AT_type.0 as u64)?.as_type());
            }
        }
        Ok(ret)
    }

    pub fn children(self: &Rc<Self>) -> DieChildenIter {
        DieChildenIter::new(self)
    }

    pub fn get_bitfield_information(
        &self,
        class_byte_size: u64,
    ) -> Result<Option<BitfieldInformation>, SdbError> {
        if !self.contains(DW_AT_bit_offset.0 as u64)
            && !self.contains(DW_AT_data_bit_offset.0 as u64)
        {
            return Ok(None);
        }
        let bit_size = self.index(DW_AT_bit_size.0 as u64)?.as_int()?;
        let storage_byte_size = if self.contains(DW_AT_byte_size.0 as u64) {
            self.index(DW_AT_byte_size.0 as u64)?.as_int()?
        } else {
            class_byte_size
        };
        let storage_bit_size = storage_byte_size * 8;
        let mut bit_offset = 0u8;
        if self.contains(DW_AT_bit_offset.0 as u64) {
            let offset_field = self.index(DW_AT_bit_offset.0 as u64)?.as_int()?;
            bit_offset = (storage_bit_size - offset_field - bit_size) as u8;
        }
        if self.contains(DW_AT_data_bit_offset.0 as u64) {
            bit_offset = self.index(DW_AT_data_bit_offset.0 as u64)?.as_int()? as u8 % 8;
        }
        Ok(Some(BitfieldInformation {
            bit_size,
            storage_byte_size,
            bit_offset,
        }))
    }
    pub fn new(
        pos: Bytes,
        cu: &Rc<CompileUnit>,
        abbrev: Rc<Abbrev>,
        attr_locs: Vec<Bytes>,
        next: Bytes,
    ) -> Rc<Self> {
        Rc::new(Self {
            pos,
            cu: Rc::downgrade(cu),
            abbrev: Some(abbrev),
            next,
            attr_locs,
        })
    }

    pub fn null(next: Bytes) -> Rc<Self> {
        Rc::new(Self {
            pos: Bytes::new(),
            cu: Weak::new(),
            abbrev: None,
            next,
            attr_locs: Vec::new(),
        })
    }

    pub fn cu(&self) -> Rc<CompileUnit> {
        self.cu.upgrade().unwrap()
    }

    pub fn abbrev_entry(&self) -> Rc<Abbrev> {
        self.abbrev.as_ref().unwrap().clone()
    }

    pub fn position(&self) -> Bytes {
        self.pos.clone()
    }

    pub fn next(&self) -> Bytes {
        self.next.clone()
    }

    pub fn contains(&self, attribute: u64) -> bool {
        if let Some(abbrev) = &self.abbrev {
            return abbrev.attr_specs.iter().any(|spec| spec.attr == attribute);
        }
        false
    }

    pub fn index(&self, attribute: u64) -> Result<DieAttr, SdbError> {
        if let Some(abbrev) = &self.abbrev
            && let Some((i, spec)) = abbrev
                .attr_specs
                .iter()
                .enumerate()
                .find(|(_, spec)| spec.attr == attribute)
        {
            return Ok(DieAttr::new(
                &self.cu(),
                spec.attr,
                spec.form,
                self.attr_locs[i].clone(),
            ));
        }
        SdbError::err("Attribute not found")
    }

    pub fn low_pc(&self) -> Result<FileAddress, SdbError> {
        if self.contains(DW_AT_ranges.0 as u64) {
            let first_entry = self
                .index(DW_AT_ranges.0 as u64)?
                .as_range_list()?
                .into_iter()
                .next()
                .unwrap();
            return Ok(first_entry.low);
        } else if self.contains(DW_AT_low_pc.0 as u64) {
            return self.index(DW_AT_low_pc.0 as u64)?.as_address();
        }
        SdbError::err("DIE does not have low PC")
    }

    pub fn high_pc(&self) -> Result<FileAddress, SdbError> {
        if self.contains(DW_AT_ranges.0 as u64) {
            let last_entry = self
                .index(DW_AT_ranges.0 as u64)?
                .as_range_list()?
                .into_iter()
                .last()
                .unwrap();
            return Ok(last_entry.high);
        } else if self.contains(DW_AT_high_pc.0 as u64) {
            let attr = self.index(DW_AT_high_pc.0 as u64)?;
            let addr: u64 = if attr.form() == DW_FORM_addr.0.into() {
                attr.as_address()?.addr()
            } else {
                self.low_pc()?.addr() + attr.as_int()?
            };
            return Ok(FileAddress::new(
                &self.cu.upgrade().unwrap().dwarf_info().elf_file(),
                addr,
            ));
        }
        SdbError::err("DIE does not have high PC")
    }

    pub fn contains_address(&self, addr: &FileAddress) -> Result<bool, SdbError> {
        if !addr.has_elf()
            || !Rc::ptr_eq(
                &self.cu.upgrade().unwrap().dwarf_info().elf_file(),
                &addr.rc_elf_file(),
            )
        {
            return Ok(false);
        }
        if self.contains(DW_AT_ranges.0 as u64) {
            return Ok(self
                .index(DW_AT_ranges.0 as u64)?
                .as_range_list()?
                .contains(addr));
        } else if self.contains(DW_AT_low_pc.0 as u64) {
            let low_pc = self.low_pc()?;
            let high_pc = self.high_pc()?;
            return Ok(&low_pc <= addr && &high_pc > addr);
        }
        Ok(false)
    }

    pub fn name(&self) -> Result<Option<String>, SdbError> {
        if self.contains(DW_AT_name.0 as u64) {
            return Ok(Some(self.index(DW_AT_name.0 as u64)?.as_string()?));
        }
        if self.contains(DW_AT_specification.0 as u64) {
            return self
                .index(DW_AT_specification.0 as u64)?
                .as_reference()
                .name();
        }
        if self.contains(DW_AT_abstract_origin.0 as u64) {
            return self
                .index(DW_AT_abstract_origin.0 as u64)?
                .as_reference()
                .name();
        }
        Ok(None)
    }

    pub fn location(&self) -> Result<SourceLocation, SdbError> {
        Ok(SourceLocation {
            file: self.file()?,
            line: self.line()?,
        })
    }

    pub fn file(&self) -> Result<Rc<LineTableFile>, SdbError> {
        let idx = if self.abbrev_entry().tag as u16 == DW_TAG_inlined_subroutine.0 {
            self.index(DW_AT_call_file.0 as u64)?.as_int()?
        } else {
            self.index(DW_AT_decl_file.0 as u64)?.as_int()?
        };
        Ok(Rc::new(
            self.cu().lines().file_names()[idx as usize - 1].clone(),
        ))
    }

    pub fn line(&self) -> Result<u64, SdbError> {
        if self.abbrev_entry().tag as u16 == DW_TAG_inlined_subroutine.0 {
            return self.index(DW_AT_call_line.0 as u64)?.as_int();
        }
        self.index(DW_AT_decl_line.0 as u64)?.as_int()
    }
}

pub struct DieAttr {
    cu: Weak<CompileUnit>,
    type_: u64,
    form: u64,
    location: Bytes,
}

impl DieAttr {
    pub fn new(cu: &Rc<CompileUnit>, type_: u64, form: u64, location: Bytes) -> Self {
        Self {
            cu: Rc::downgrade(cu),
            type_,
            form,
            location: location.clone(),
        }
    }

    pub fn name(&self) -> u64 {
        self.type_
    }

    pub fn form(&self) -> u64 {
        self.form
    }

    pub fn as_address(&self) -> Result<FileAddress, SdbError> {
        let mut cursor = Cursor::new(&self.location);
        if self.form as u16 != DW_FORM_addr.0 {
            return SdbError::err("Invalid address type");
        }
        let elf = self.cu.upgrade().unwrap().dwarf_info().elf_file();
        return Ok(FileAddress::new(&elf, cursor.u64()));
    }

    pub fn as_section_offset(&self) -> Result<u32, SdbError> {
        let mut cursor = Cursor::new(&self.location);
        if self.form as u16 != DW_FORM_sec_offset.0 {
            return SdbError::err("Invalid offset type");
        }
        return Ok(cursor.u32());
    }

    pub fn as_block(&self) -> Result<Bytes, SdbError> {
        let mut cursor = Cursor::new(&self.location);
        #[allow(non_upper_case_globals)]
        let size = match DwForm(self.form as u16) {
            DW_FORM_block1 => cursor.u8() as usize,
            DW_FORM_block2 => cursor.u16() as usize,
            DW_FORM_block4 => cursor.u32() as usize,
            DW_FORM_block => cursor.uleb128() as usize,
            _ => return SdbError::err("Invalid block type"),
        };
        Ok(cursor.position().slice(..size))
    }

    pub fn as_int(&self) -> Result<u64, SdbError> {
        let mut cursor = Cursor::new(&self.location);
        #[allow(non_upper_case_globals)]
        return match DwForm(self.form as u16) {
            DW_FORM_data1 => Ok(cursor.u8() as u64),
            DW_FORM_data2 => Ok(cursor.u16() as u64),
            DW_FORM_data4 => Ok(cursor.u32() as u64),
            DW_FORM_data8 => Ok(cursor.u64()),
            DW_FORM_udata => Ok(cursor.uleb128()),
            _ => SdbError::err("Invalid integer type"),
        };
    }

    pub fn as_string(&self) -> Result<String, SdbError> {
        let mut cursor = Cursor::new(&self.location);
        #[allow(non_upper_case_globals)]
        match DwForm(self.form as u16) {
            DW_FORM_string => Ok(cursor.string()),
            DW_FORM_strp => {
                let offset = cursor.u32() as usize;
                let stab = self
                    .cu
                    .upgrade()
                    .unwrap()
                    .dwarf_info()
                    .elf_file()
                    .get_section_contents(".debug_str");
                let mut stab_cur = Cursor::new(&stab.slice(offset..));
                Ok(stab_cur.string())
            }
            _ => SdbError::err("Invalid string type"),
        }
    }

    pub fn as_reference(&self) -> Rc<Die> {
        let mut cursor = Cursor::new(&self.location);
        #[allow(non_upper_case_globals)]
        let offset = match DwForm(self.form as u16) {
            DW_FORM_ref1 => cursor.u8() as usize,
            DW_FORM_ref2 => cursor.u16() as usize,
            DW_FORM_ref4 => cursor.u32() as usize,
            DW_FORM_ref8 => cursor.u64() as usize,
            DW_FORM_ref_udata => cursor.uleb128() as usize,
            DW_FORM_ref_addr => {
                let offset = cursor.u32() as usize;
                let cu = self.cu.upgrade().unwrap();
                let dwarf_info = cu.dwarf_info();
                let section = dwarf_info.elf_file().get_section_contents(".debug_info");
                let die_pos = section.slice(offset..);
                let die_ptr = die_pos.as_ptr() as usize;
                let cus = dwarf_info.compile_units();
                let cu_for_offset = cus
                    .iter()
                    .find(|cu| {
                        let cu_ptr = cu.data.as_ptr() as usize;
                        let cu_end = cu_ptr + cu.data.len();
                        cu_ptr <= die_ptr && cu_end > die_ptr
                    })
                    .unwrap();
                let offset_in_cu = die_ptr - (cu_for_offset.data().as_ptr() as usize);
                let ref_cursor = Cursor::new(&cu_for_offset.data.slice(offset_in_cu..));
                return parse_die(cu_for_offset, ref_cursor);
            }
            _ => panic!("Invalid reference type"),
        };
        let cu = self.cu.upgrade().unwrap();
        let ref_cursor = Cursor::new(&cu.data.slice(offset..));
        parse_die(&cu, ref_cursor)
    }

    pub fn as_range_list(&self) -> Result<CompileUnitRangeList, SdbError> {
        let cu = self.cu.upgrade().unwrap();
        let section = cu
            .dwarf_info()
            .elf_file()
            .get_section_contents(".debug_ranges");
        let offset = self.as_section_offset()? as usize;
        let data = section.slice(offset..);
        let root = cu.root();
        let base_address = if root.contains(DW_AT_low_pc.0 as u64) {
            root.index(DW_AT_low_pc.0 as u64)?.as_address()?
        } else {
            FileAddress::default()
        };
        Ok(CompileUnitRangeList::new(&cu, &data, base_address))
    }

    pub fn as_expression(&self, in_frame_info: bool) -> DwarfExpression {
        let cu_data_end = {
            let cu = self.cu.upgrade().unwrap();
            cu.data.as_ptr() as usize + cu.data.len()
        };
        let slice = cu_data_end - self.location.as_ptr() as usize;
        let mut cur = Cursor::new(&self.location.slice(..slice));
        let length = cur.uleb128();
        let data = cur.position().slice(..length as usize);

        DwarfExpression::builder()
            .parent(Rc::downgrade(&self.cu.upgrade().unwrap().dwarf_info()))
            .expr_data(data)
            .in_frame_info(in_frame_info)
            .build()
    }

    pub fn as_location_list(&self, in_frame_info: bool) -> LocationList {
        let section = self
            .cu
            .upgrade()
            .unwrap()
            .dwarf_info()
            .elf_file()
            .get_section_contents(".debug_loc");
        let cu_data_end = {
            let cu = self.cu.upgrade().unwrap();
            cu.data.as_ptr() as usize + cu.data.len()
        };
        let slice = cu_data_end - self.location.as_ptr() as usize;
        let mut cur = Cursor::new(&self.location.slice(..slice));
        let offset = cur.u32();

        let data = section.slice(offset as usize..);

        LocationList::new(
            Rc::downgrade(&self.cu.upgrade().unwrap().dwarf_info()),
            self.cu.clone(),
            data,
            in_frame_info,
        )
    }

    pub fn as_evaluated_location(
        &self,
        proc: &Process,
        regs: &Registers,
        in_frame_info: bool,
    ) -> Result<DwarfExpressionResult, SdbError> {
        if self.form == DW_FORM_exprloc.0 as u64 {
            let expr = self.as_expression(in_frame_info);
            expr.eval(proc, regs, false)
        } else if self.form == DW_FORM_sec_offset.0 as u64 {
            let loc_list = self.as_location_list(in_frame_info);
            Ok(loc_list.eval(proc, regs))
        } else {
            SdbError::err("Invalid location type")
        }
    }

    pub fn as_type(&self) -> SdbType {
        return SdbType::new(self.as_reference());
    }
}

pub struct LocationList {
    parent: Weak<Dwarf>,
    cu: Weak<CompileUnit>,
    expr_data: Bytes,
    in_frame_info: bool,
}

impl LocationList {
    pub fn new(
        parent: Weak<Dwarf>,
        cu: Weak<CompileUnit>,
        expr_data: Bytes,
        in_frame_info: bool,
    ) -> Self {
        Self {
            parent,
            cu,
            expr_data,
            in_frame_info,
        }
    }

    pub fn eval(&self, proc: &Process, regs: &Registers) -> DwarfExpressionResult {
        let virt_pc = VirtualAddress::new(regs.read_by_id_as::<u64>(RegisterId::rip).unwrap());
        let pc = virt_pc.to_file_addr_elf(&self.parent.upgrade().unwrap().elf_file());

        let mut cur = Cursor::new(&self.expr_data);
        let base_address_flag = !0u64;
        let mut base_address = self
            .cu
            .upgrade()
            .unwrap()
            .root()
            .index(DW_AT_low_pc.0 as u64)
            .unwrap()
            .as_address()
            .unwrap()
            .addr();

        let mut first = cur.u64();
        let mut second = cur.u64();
        while !(first == 0 && second == 0) {
            if first == base_address_flag {
                base_address = second;
            } else {
                let length = cur.u16();
                if pc.addr() >= base_address + first && pc.addr() < base_address + second {
                    let expr_data = cur.position().slice(..length as usize);
                    let expr = DwarfExpression::builder()
                        .parent(self.parent.clone())
                        .expr_data(expr_data)
                        .in_frame_info(self.in_frame_info)
                        .build();
                    return expr.eval(proc, regs, false).unwrap();
                } else {
                    cur += length as usize;
                }
            }
            first = cur.u64();
            second = cur.u64();
        }

        DwarfExpressionResult::SimpleLocation(DwarfExpressionSimpleLocation::Empty {})
    }
}

pub struct DieChildenIter {
    die: Option<Rc<Die>>,
}

impl DieChildenIter {
    pub fn new(die: &Rc<Die>) -> Self {
        if let Some(abbrev) = &die.abbrev
            && abbrev.has_children
        {
            let next_cursor = Cursor::new(&die.next);
            return Self {
                die: Some(parse_die(&die.cu.upgrade().unwrap(), next_cursor)),
            };
        }
        Self { die: None }
    }
}

impl Iterator for DieChildenIter {
    type Item = Rc<Die>;

    fn next(&mut self) -> Option<Self::Item> {
        if let Some(current_die) = self.die.take()
            && let Some(abbrev) = &current_die.abbrev
        {
            if !abbrev.has_children {
                let next_cursor = Cursor::new(&current_die.next);
                self.die = Some(parse_die(&current_die.cu.upgrade().unwrap(), next_cursor));
                return Some(current_die);
            } else if current_die.contains(DW_AT_sibling.0 as u64) {
                self.die = Some(
                    current_die
                        .index(DW_AT_sibling.0 as u64)
                        .unwrap()
                        .as_reference(),
                );
                return Some(current_die);
            } else {
                let sub_children = DieChildenIter::new(&current_die);
                for d in sub_children {
                    if d.abbrev.is_none() {
                        let next_cursor = Cursor::new(&d.next);
                        self.die = Some(parse_die(&current_die.cu.upgrade().unwrap(), next_cursor));
                        break;
                    }
                }
                return Some(current_die);
            }
        }
        None
    }
}

#[derive(Debug)]
pub struct CompileUnit {
    parent: Weak<Dwarf>,
    data: Bytes,
    abbrev_offset: usize,
    line_table: RefCell<Option<Rc<LineTable>>>,
}

fn parse_line_table_file<T: AsRef<Path>>(
    cur: &mut Cursor,
    compilation_dir: &T,
    include_directories: &[PathBuf],
) -> LineTableFile {
    let file = PathBuf::from(cur.string());
    let dir_index = cur.uleb128();
    let modification_time = cur.uleb128();
    let file_length = cur.uleb128();
    let mut path = file.clone();
    if !file.as_os_str().to_str().unwrap().starts_with('/') {
        if dir_index == 0 {
            path = compilation_dir.as_ref().join(file);
        } else {
            path = include_directories[dir_index as usize - 1].join(file);
        }
    }
    LineTableFile {
        path,
        modification_time,
        file_length,
    }
}

fn parse_line_table(cu: &Rc<CompileUnit>) -> Result<Option<Rc<LineTable>>, SdbError> {
    let section = cu
        .dwarf_info()
        .elf_file()
        .get_section_contents(".debug_line");
    if !cu.root().contains(DW_AT_stmt_list.0 as u64) {
        return Ok(None);
    }
    let offset = cu
        .root()
        .index(DW_AT_stmt_list.0 as u64)?
        .as_section_offset()? as usize;
    let mut cursor = Cursor::new(&section.slice(offset..));
    let size = cursor.u32();
    let end = cursor.position().as_ptr() as usize + size as usize;
    let version = cursor.u16();
    if version != 4 {
        return SdbError::err("Only DWARF 4 is supported");
    }
    let _header_length = cursor.u32();
    let minimum_instruction_length = cursor.u8();
    if minimum_instruction_length != 1 {
        return SdbError::err("Invalid minimum instruction length");
    }
    let maximum_operations_per_instruction = cursor.u8();
    if maximum_operations_per_instruction != 1 {
        return SdbError::err("Invalid maximum operations per instruction");
    }
    let default_is_stmt = cursor.u8();
    let line_base = cursor.s8();
    let line_range = cursor.u8();
    let opcode_base = cursor.u8();
    let expected_opcode_lengths = [0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1];
    for i in 0..opcode_base - 1 {
        if cursor.u8() != expected_opcode_lengths[i as usize] {
            return SdbError::err("Unexpected opcode length");
        }
    }
    let mut include_directories: Vec<PathBuf> = vec![];
    let compilation_dir = PathBuf::from(cu.root().index(DW_AT_comp_dir.0 as u64)?.as_string()?);
    let mut dir = cursor.string();
    while !dir.is_empty() {
        if dir.starts_with('/') {
            include_directories.push(PathBuf::from(dir));
        } else {
            include_directories.push(compilation_dir.clone().join(dir));
        }
        dir = cursor.string();
    }
    let mut file_names: Vec<LineTableFile> = vec![];
    while cursor.position()[0] != 0 {
        file_names.push(parse_line_table_file(
            &mut cursor,
            &compilation_dir,
            &include_directories,
        ));
    }
    cursor += 1;
    // Use ptr arithmetics
    let data = cursor
        .position()
        .slice(0..(end - cursor.position().as_ptr() as usize));
    Ok(Some(LineTable::new(
        data,
        cu,
        default_is_stmt != 0,
        line_base,
        line_range,
        opcode_base,
        include_directories,
        file_names,
    )))
}

impl CompileUnit {
    pub fn root(self: &Rc<Self>) -> Rc<Die> {
        let header_size = 11usize;
        let cursor = Cursor::new(&self.data.slice(header_size..));
        parse_die(self, cursor)
    }

    pub fn new(
        parent: &Rc<Dwarf>,
        data: Bytes,
        abbrev_offset: usize,
    ) -> Result<Rc<Self>, SdbError> {
        let ret = Rc::new(Self {
            parent: Rc::downgrade(parent),
            data,
            abbrev_offset,
            line_table: RefCell::new(None),
        });
        *ret.line_table.borrow_mut() = parse_line_table(&ret)?;
        Ok(ret)
    }

    pub fn dwarf_info(&self) -> Rc<Dwarf> {
        self.parent.upgrade().unwrap()
    }

    pub fn data(&self) -> &[u8] {
        &self.data
    }

    pub fn abbrev_table(&self) -> Rc<AbbrevTable> {
        self.parent
            .upgrade()
            .unwrap()
            .get_abbrev_table(self.abbrev_offset)
    }

    pub fn lines(&self) -> Rc<LineTable> {
        self.line_table.borrow().clone().unwrap()
    }
}

#[derive(Debug, Clone)]
pub struct CompileUnitRangeList {
    cu: Rc<CompileUnit>,
    data: Bytes,
    base_address: FileAddress,
}

impl CompileUnitRangeList {
    pub fn new(cu: &Rc<CompileUnit>, data: &Bytes, base_address: FileAddress) -> Self {
        Self {
            cu: cu.clone(),
            data: data.clone(),
            base_address,
        }
    }

    pub fn contains(&self, addr: &FileAddress) -> bool {
        let mut iter =
            CompileUnitRangeListIter::new(&self.cu, &self.data, self.base_address.clone());
        iter.any(|e| e.contains(addr))
    }
}

impl IntoIterator for CompileUnitRangeList {
    type Item = CompileUnitRangeEntry;
    type IntoIter = CompileUnitRangeListIter;

    fn into_iter(self) -> Self::IntoIter {
        CompileUnitRangeListIter::new(&self.cu, &self.data, self.base_address)
    }
}

pub struct CompileUnitRangeListIter {
    cu: Rc<CompileUnit>,
    pos: Bytes,
    base_address: FileAddress,
    current: CompileUnitRangeEntry,
}

impl CompileUnitRangeListIter {
    pub fn new(cu: &Rc<CompileUnit>, data: &Bytes, base_address: FileAddress) -> Self {
        let mut ret = Self {
            cu: cu.clone(),
            pos: data.clone(),
            base_address,
            current: CompileUnitRangeEntry::default(),
        };
        ret.next();
        ret
    }
}

impl Iterator for CompileUnitRangeListIter {
    type Item = CompileUnitRangeEntry;

    fn next(&mut self) -> Option<Self::Item> {
        if self.pos.is_empty() {
            return None;
        }
        let elf = self.cu.dwarf_info().elf_file();
        let base_address_flag = !0u64;
        let mut cursor = Cursor::new(&self.pos);
        let prev_current = self.current.clone();
        loop {
            self.current.low = FileAddress::new(&elf, cursor.u64());
            self.current.high = FileAddress::new(&elf, cursor.u64());
            if self.current.low.addr() == base_address_flag {
                self.base_address = self.current.high.clone();
            } else if self.current.low.addr() == 0 && self.current.high.addr() == 0 {
                self.pos = Bytes::new();
                break;
            } else {
                self.pos = cursor.position();
                self.current.low += self.base_address.addr() as i64;
                self.current.high += self.base_address.addr() as i64;
                break;
            }
        }
        return Some(prev_current);
    }
}
#[derive(Debug, Clone, Default)]
pub struct CompileUnitRangeEntry {
    pub low: FileAddress,
    pub high: FileAddress,
}

impl CompileUnitRangeEntry {
    pub fn contains(&self, addr: &FileAddress) -> bool {
        &self.low <= addr && addr < &self.high
    }
}

fn parse_die(cu: &Rc<CompileUnit>, mut cursor: Cursor) -> Rc<Die> {
    let pos = cursor.position();
    let abbrev_code = cursor.uleb128();
    if abbrev_code == 0 {
        let next = cursor.position();
        return Die::null(next);
    }
    let abbrev_table = cu.abbrev_table();
    let abbrev = &abbrev_table[&abbrev_code];
    let mut attr_locs = Vec::<Bytes>::with_capacity(abbrev.attr_specs.len());
    for attr in &abbrev.attr_specs {
        attr_locs.push(cursor.position());
        cursor.skip_form(attr.form).unwrap();
    }
    let next = cursor.position();
    Die::new(pos, cu, abbrev.clone(), attr_locs, next)
}

fn parse_call_frame_information(
    dwarf: &Rc<Dwarf>,
) -> Result<Rc<RefCell<CallFrameInformation>>, SdbError> {
    let eh_hdr = parse_eh_hdr(dwarf)?;
    Ok(CallFrameInformation::new(dwarf, eh_hdr))
}

#[derive(Debug)]
pub struct Dwarf {
    elf: Weak<Elf>,
    abbrev_tables: RefCell<HashMap<usize, Rc<AbbrevTable>>>,
    compile_units: OnceCell<Vec<Rc<CompileUnit>>>,
    function_index: RefCell<MultiMap<String, DwarfIndexEntry>>,
    cfi: OnceCell<Rc<RefCell<CallFrameInformation>>>,
    global_variable_index: RefCell<MultiMap<String, DwarfIndexEntry>>,
    member_function_index: RefCell<HashMap<Bytes, DwarfIndexEntry>>,
}

fn scopes_at_address_in_die(
    die: &Rc<Die>,
    address: &FileAddress,
    scopes: &mut Vec<Rc<Die>>,
) -> Result<(), SdbError> {
    for c in die.children() {
        if c.contains_address(address)? {
            scopes_at_address_in_die(&c, address, scopes)?;
            scopes.push(c);
        }
    }
    Ok(())
}
impl Dwarf {
    pub fn get_member_function_definition(
        &self,
        declaration: &Rc<Die>,
    ) -> Result<Option<Rc<Die>>, SdbError> {
        self.index()?;
        let it = self
            .member_function_index
            .borrow()
            .get(&declaration.position())
            .cloned();
        if it.is_some() {
            let it = it.unwrap();
            let cu = it.cu.upgrade().unwrap();
            let cu_data_end = cu.data().as_ptr() as usize + cu.data().len();

            let cursor = Cursor::new(&it.pos.slice(..(cu_data_end - it.pos.as_ptr() as usize)));
            let die = parse_die(&cu, cursor);
            if die.contains(DW_AT_low_pc.0 as u64) || die.contains(DW_AT_ranges.0 as u64) {
                return Ok(Some(die));
            }
            return self.get_member_function_definition(&die);
        }
        return Ok(None);
    }
    pub fn find_local_variable(
        &self,
        name: &str,
        pc: &FileAddress,
    ) -> Result<Option<Rc<Die>>, SdbError> {
        let scopes = self.scopes_at_address(pc)?;
        for scope in scopes {
            for child in scope.children() {
                let tag = child.abbrev_entry().tag;
                if (tag as u16 == DW_TAG_variable.0 || tag as u16 == DW_TAG_formal_parameter.0)
                    && let Some(child_name) = child.name()?
                    && child_name == name
                {
                    return Ok(Some(child));
                }
            }
        }
        Ok(None)
    }

    pub fn scopes_at_address(&self, address: &FileAddress) -> Result<Vec<Rc<Die>>, SdbError> {
        let func = self.function_containing_address(address)?;
        match func {
            Some(func) => {
                let mut scopes = Vec::new();
                scopes_at_address_in_die(&func, address, &mut scopes)?;
                scopes.push(func);
                Ok(scopes)
            }
            None => Ok(vec![]),
        }
    }

    pub fn find_global_variable(&self, name: &str) -> Result<Option<Rc<Die>>, SdbError> {
        self.index()?;
        let global_variable_index = self.global_variable_index.borrow();
        let indices = global_variable_index.get_vec(name);
        if let Some(entrys) = indices
            && let Some(entry) = entrys.iter().next()
        {
            let cu = entry.cu.upgrade().unwrap();
            let cu_data_end = cu.data().as_ptr() as usize + cu.data().len();
            let len = cu_data_end - entry.pos.as_ptr() as usize;
            let cursor = Cursor::new(&entry.pos.slice(0..len));
            return Ok(Some(parse_die(&cu, cursor)));
        }
        Ok(None)
    }

    pub fn cfi(&self) -> Rc<RefCell<CallFrameInformation>> {
        self.cfi.get().unwrap().clone()
    }

    pub fn new(parent: &Weak<Elf>) -> Result<Rc<Self>, SdbError> {
        let ret = Rc::new(Self {
            elf: parent.clone(),
            abbrev_tables: RefCell::new(HashMap::default()),
            compile_units: OnceCell::new(),
            function_index: RefCell::new(MultiMap::default()),
            global_variable_index: RefCell::new(MultiMap::default()),
            cfi: OnceCell::new(),
            member_function_index: RefCell::new(HashMap::default()),
        });
        let t = parse_compile_units(&ret, &ret.elf_file())?;
        ret.compile_units
            .set(t)
            .map_err(|_| SdbError::new_err("Failed to set compile units"))?;
        ret.cfi
            .set(parse_call_frame_information(&ret)?)
            .map_err(|_| SdbError::new_err("Failed to set call frame information"))?;
        Ok(ret)
    }

    pub fn elf_file(&self) -> Rc<Elf> {
        self.elf.upgrade().unwrap()
    }

    pub fn get_abbrev_table(&self, offset: usize) -> Rc<AbbrevTable> {
        if !self.abbrev_tables.borrow().contains_key(&offset) {
            self.abbrev_tables.borrow_mut().insert(
                offset,
                Rc::new(parse_abbrev_table(&self.elf_file(), offset)),
            );
        }
        self.abbrev_tables.borrow()[&offset].clone()
    }

    pub fn compile_units(&self) -> &Vec<Rc<CompileUnit>> {
        self.compile_units.get().unwrap()
    }

    pub fn compile_unit_containing_address(
        &self,
        address: &FileAddress,
    ) -> Result<Option<Rc<CompileUnit>>, SdbError> {
        for cu in self.compile_units().iter() {
            if cu.root().contains_address(address)? {
                return Ok(Some(cu.clone()));
            }
        }
        Ok(None)
    }

    pub fn function_containing_address(
        &self,
        address: &FileAddress,
    ) -> Result<Option<Rc<Die>>, SdbError> {
        self.index()?;
        for (_name, entry) in self.function_index.borrow().iter() {
            let cursor = Cursor::new(&entry.pos);
            let die = parse_die(&entry.cu.upgrade().unwrap(), cursor);
            if die.contains_address(address)?
                && die.abbrev_entry().tag == DW_TAG_subprogram.0 as u64
            {
                return Ok(Some(die));
            }
        }
        Ok(None)
    }

    pub fn find_functions(&self, name: &str) -> Result<Vec<Rc<Die>>, SdbError> {
        self.index()?;
        let mut found: Vec<Rc<Die>> = Vec::new();
        let function_index = self.function_index.borrow();
        let entrys = function_index.get_vec(name);
        if let Some(entrys) = entrys {
            for entry in entrys {
                let cursor = Cursor::new(&entry.pos);
                let die = parse_die(&entry.cu.upgrade().unwrap(), cursor);
                found.push(die);
            }
        }
        Ok(found)
    }

    fn index(&self) -> Result<(), SdbError> {
        if !self.function_index.borrow().is_empty() {
            return Ok(());
        }
        for cu in self.compile_units().iter() {
            self.index_die(&cu.root(), false)?;
        }
        Ok(())
    }

    fn index_die(
        &self,
        current: &Rc<Die>,
        mut in_function: bool, /* false */
    ) -> Result<(), SdbError> {
        let has_range =
            current.contains(DW_AT_low_pc.0 as u64) || current.contains(DW_AT_ranges.0 as u64);
        let is_function = current.abbrev_entry().tag == DW_TAG_subprogram.0 as u64
            || current.abbrev_entry().tag == DW_TAG_inlined_subroutine.0 as u64;
        if has_range
            && is_function
            && let Some(name) = current.name()?
        {
            let entry = DwarfIndexEntry {
                cu: current.cu.clone(),
                pos: current.pos.clone(),
            };
            self.function_index.borrow_mut().insert(name, entry);
        }
        if is_function {
            if current.contains(DW_AT_specification.0 as u64) {
                let index_entry = DwarfIndexEntry {
                    cu: current.cu.clone(),
                    pos: current.position(),
                };
                self.member_function_index.borrow_mut().insert(
                    current
                        .index(DW_AT_specification.0 as u64)?
                        .as_reference()
                        .position(),
                    index_entry,
                );
            } else if current.contains(DW_AT_abstract_origin.0 as u64) {
                let index_entry = DwarfIndexEntry {
                    cu: current.cu.clone(),
                    pos: current.position(),
                };
                self.member_function_index.borrow_mut().insert(
                    current
                        .index(DW_AT_abstract_origin.0 as u64)?
                        .as_reference()
                        .position(),
                    index_entry,
                );
            }
        }
        let has_location = current.contains(DW_AT_location.0 as u64);
        let is_variable = current.abbrev_entry().tag == DW_TAG_variable.0 as u64;
        if has_location
            && is_variable
            && !in_function
            && let Some(name) = current.name()?
        {
            let entry = DwarfIndexEntry {
                cu: current.cu.clone(),
                pos: current.position(),
            };
            self.global_variable_index.borrow_mut().insert(name, entry);
        }
        if is_function {
            in_function = true;
        }
        for child in current.children() {
            self.index_die(&child, in_function)?;
        }
        Ok(())
    }

    pub fn line_entry_at_address(&self, address: &FileAddress) -> Result<LineTableIter, SdbError> {
        let cu = self.compile_unit_containing_address(address)?;
        if let Some(cu) = cu {
            return cu.lines().get_entry_by_address(address);
        }
        Ok(LineTableIter::default())
    }

    pub fn inline_stack_at_address(&self, address: &FileAddress) -> Result<Vec<Rc<Die>>, SdbError> {
        let func = self.function_containing_address(address)?;
        let mut stack: Vec<Rc<Die>> = Vec::new();
        if let Some(func) = func {
            stack.push(func);
            loop {
                let mut children = stack.last().unwrap().children();
                let found = children.find(|child| {
                    child.abbrev_entry().tag == DW_TAG_inlined_subroutine.0 as u64
                        && child.contains_address(address).unwrap_or(false)
                });
                if let Some(found) = found {
                    stack.push(found);
                } else {
                    break;
                }
            }
        }
        Ok(stack)
    }
}

#[derive(Debug, Clone)]
pub struct DwarfIndexEntry {
    cu: Weak<CompileUnit>,
    pos: Bytes,
}

fn parse_compile_units(dwarf: &Rc<Dwarf>, obj: &Elf) -> Result<Vec<Rc<CompileUnit>>, SdbError> {
    let debug_info = obj.get_section_contents(".debug_info");
    let mut cursor = Cursor::new(&debug_info);
    let mut units: Vec<Rc<CompileUnit>> = Vec::new();
    while !cursor.finished() {
        if let Ok(unit) = parse_compile_unit(dwarf, obj, cursor.clone()) {
            cursor += unit.data.len();
            units.push(unit);
        } else {
            break;
        }
    }
    Ok(units)
}

fn parse_compile_unit(
    dwarf: &Rc<Dwarf>,
    _obj: &Elf,
    mut cursor: Cursor,
) -> Result<Rc<CompileUnit>, SdbError> {
    let start = cursor.position();
    let mut size = cursor.u32();
    let version = cursor.u16();
    let abbrev = cursor.u32();
    let address_size = cursor.u8();
    if size == 0xffffffff {
        return SdbError::err("Only DWARF32 is supported");
    }
    if version != 4 {
        return SdbError::err(&format!(
            "Only DWARF version 4 is supported, found version {version}"
        ));
    }
    if address_size != 8 {
        return SdbError::err("Invalid address size for DWARF");
    }
    size += size_of::<u32>() as u32;
    let data = start.slice(..size as usize);
    CompileUnit::new(dwarf, data, abbrev as usize)
}

fn parse_abbrev_table(obj: &Elf, offset: usize) -> AbbrevTable {
    let mut cursor = Cursor::new(&obj.get_section_contents(".debug_abbrev"));
    cursor += offset;
    let mut table: AbbrevTable = HashMap::new();
    let mut code: u64;
    loop {
        code = cursor.uleb128();
        // Bug fixed: should break early
        if code == 0 {
            break;
        }
        let tag = cursor.uleb128();
        let has_children = cursor.u8() != 0;
        let mut attr_specs = Vec::<AttrSpec>::new();
        let mut attr: u64;
        loop {
            attr = cursor.uleb128();
            let form = cursor.uleb128();
            if attr != 0 {
                attr_specs.push(AttrSpec { attr, form });
            }
            if attr == 0 {
                break;
            }
        }
        table.insert(
            code,
            Rc::new(Abbrev {
                code,
                tag,
                has_children,
                attr_specs,
            }),
        );
    }

    table
}

#[derive(Debug, Clone, Copy)]
pub struct AttrSpec {
    attr: u64,
    form: u64,
}

#[derive(Debug)]
pub struct Abbrev {
    pub code: u64,
    pub tag: u64,
    pub has_children: bool,
    pub attr_specs: Vec<AttrSpec>,
}

#[derive(Debug, Clone, Default)]
pub struct Cursor {
    data: Bytes,
}

macro_rules! gen_fixed_int {
    ($( $name:ident : $ty:ty ),* $(,)?) => {
       $(
            pub fn $name(&mut self) -> $ty {
                self.fixed_int::<$ty>()
            }
        )*
    };
}

impl Cursor {
    pub fn new(data: &Bytes) -> Self {
        Self { data: data.clone() }
    }

    pub fn finished(&self) -> bool {
        self.data.is_empty()
    }

    pub fn position(&self) -> Bytes {
        self.data.clone()
    }

    pub fn fixed_int<T: Pod>(&mut self) -> T {
        let t = from_bytes::<T>(&self.data);
        self.data = self.data.slice(size_of::<T>()..);
        t
    }

    pub fn string(&mut self) -> String {
        if let Some(pos) = self.data.iter().position(|&b| b == 0) {
            let next = pos + 1;
            let s = self.data.slice(..next);
            let s = CStr::from_bytes_with_nul(&s).unwrap().to_str().unwrap();
            if next < self.data.len() {
                self.data = self.data.slice(next..);
            } else {
                self.data = Bytes::new();
            }
            return s.to_owned();
        }

        panic!("Cannot find cstr")
    }

    pub fn uleb128(&mut self) -> u64 {
        let mut res = 0u64;
        let mut shift = 0i32;
        let mut byte: u8;
        loop {
            byte = self.u8();
            let masked = (byte & 0x7f) as u64;
            res |= masked << shift;
            shift += 7;
            if (byte & 0x80) == 0 {
                break;
            }
        }
        return res;
    }

    pub fn sleb128(&mut self) -> i64 {
        let mut res = 0u64;
        let mut shift = 0i32;
        let mut byte: u8;
        loop {
            byte = self.u8();
            let masked = (byte & 0x7f) as u64;
            res |= masked << shift;
            shift += 7;
            if byte & 0x80 == 0 {
                break;
            }
        }
        if ((shift as usize) < u64::BITS as usize) && ((byte & 0x40) != 0) {
            res |= !0u64 << shift;
        }
        res as i64
    }
    #[allow(non_upper_case_globals)]
    pub fn skip_form(&mut self, form: u64) -> Result<(), SdbError> {
        return match DwForm(form.try_into().unwrap()) {
            /* 0-byte forms -------------------------------------------------- */
            DW_FORM_flag_present => Ok(()),

            /* fixed-size scalar/reference forms ----------------------------- */
            DW_FORM_data1 | DW_FORM_ref1 | DW_FORM_flag => {
                self.data = self.data.slice(1..);
                Ok(())
            }
            DW_FORM_data2 | DW_FORM_ref2 => {
                self.data = self.data.slice(2..);
                Ok(())
            }
            DW_FORM_data4 | DW_FORM_ref4 | DW_FORM_ref_addr | DW_FORM_sec_offset | DW_FORM_strp => {
                self.data = self.data.slice(4..);
                Ok(())
            }
            DW_FORM_data8 | DW_FORM_addr => {
                self.data = self.data.slice(8..);
                Ok(())
            }

            /* variable-length scalars --------------------------------------- */
            DW_FORM_sdata => {
                self.sleb128();
                Ok(())
            }
            DW_FORM_udata | DW_FORM_ref_udata => {
                self.uleb128();
                Ok(())
            }

            /* blocks whose length precedes the data ------------------------- */
            DW_FORM_block1 => {
                let s = self.u8() as usize;
                self.data = self.data.slice(s..);
                Ok(())
            }
            DW_FORM_block2 => {
                let s = self.u16() as usize;
                self.data = self.data.slice(s..);
                Ok(())
            }
            DW_FORM_block4 => {
                let s = self.u32() as usize;
                self.data = self.data.slice(s..);
                Ok(())
            }
            DW_FORM_block | DW_FORM_exprloc => {
                let s = self.uleb128() as usize;
                self.data = self.data.slice(s..);
                Ok(())
            }

            /* in-line, NUL-terminated string ------------------------------- */
            DW_FORM_string => {
                while !self.finished() && self.data[0] != 0 {
                    self.data = self.data.slice(1..);
                }
                self.data = self.data.slice(1..); // consume trailing NUL
                Ok(())
            }
            /* indirection: the *next* ULEB128 is another form code ---------- */
            DW_FORM_indirect => {
                let s = self.uleb128();
                self.skip_form(s)?;
                Ok(())
            }
            _ => SdbError::err("Unrecognized DWARF form"),
        };
    }

    gen_fixed_int! {
        u8  : u8,
        u16 : u16,
        u32 : u32,
        u64 : u64,
        s8  : i8,
        s16 : i16,
        s32 : i32,
        s64 : i64,
    }
}

impl AddAssign<usize> for Cursor {
    fn add_assign(&mut self, rhs: usize) {
        self.data = self.data.slice(rhs..);
    }
}

#[derive(Debug, Clone, Copy)]
pub struct UndefinedRule {}
#[derive(Debug, Clone, Copy)]
pub struct SameRule {}
#[derive(Debug, Clone, Copy)]
pub struct OffsetRule {
    pub offset: i64,
}
#[derive(Debug, Clone, Copy)]
pub struct ValOffsetRule {
    pub offset: i64,
}
#[derive(Debug, Clone, Copy)]
pub struct RegisterRule {
    pub reg: u32,
}
#[derive(Debug, Clone, Copy, Default)]
pub struct CfaRegisterRule {
    pub reg: u32,
    pub offset: i64,
}
#[derive(Clone)]
pub struct ExprRule {
    pub expr: DwarfExpression,
}
#[derive(Clone)]
pub struct ValExprRule {
    pub expr: DwarfExpression,
}
#[derive(Clone, Default)]
pub struct CfaExprRule {
    pub expr: DwarfExpression,
}

#[derive(Clone)]
pub enum Rule {
    Undefined(UndefinedRule),
    Same(SameRule),
    Offset(OffsetRule),
    ValOffset(ValOffsetRule),
    Register(RegisterRule),
    CfaRegister(CfaRegisterRule),
    Expr(ExprRule),
    ValExpr(ValExprRule),
}

#[derive(Clone)]
pub enum CfaRuleType {
    Register(CfaRegisterRule),
    Expr(CfaExprRule),
}

impl Default for CfaRuleType {
    fn default() -> Self {
        Self::Register(CfaRegisterRule::default())
    }
}

pub type RuleSet = HashMap<u32, Rule>;

#[derive(Default)]
pub struct UnwindContext {
    pub cursor: Cursor,
    pub location: FileAddress,
    pub cfa_rule: CfaRuleType,
    pub cie_register_rules: RuleSet,
    pub register_rules: RuleSet,
    pub rule_stack: Vec<(RuleSet, CfaRuleType)>,
}

#[derive(Clone)]
pub enum DwarfExpressionSimpleLocation {
    Address { address: VirtualAddress },
    Register { reg_num: u64 },
    Data { data: Bytes },
    Literal { value: u64 },
    Empty {},
}

#[derive(Clone)]
pub struct DwarfExpressionPiece {
    pub location: DwarfExpressionSimpleLocation,
    pub bit_size: u64,
    pub offset: u64, /* 0 */
}

pub struct DwarfExpressionPiecesResult {
    pub pieces: Vec<DwarfExpressionPiece>,
}

pub enum DwarfExpressionResult {
    SimpleLocation(DwarfExpressionSimpleLocation),
    Pieces(DwarfExpressionPiecesResult),
}

#[derive(TypedBuilder, Clone, Default)]
pub struct DwarfExpression {
    parent: Weak<Dwarf>,
    expr_data: Bytes,
    in_frame_info: bool,
}

impl DwarfExpression {
    pub fn eval(
        &self,
        proc: &Process,
        regs: &Registers,
        push_cfa: bool, /* false */
    ) -> Result<DwarfExpressionResult, SdbError> {
        let mut cursor = Cursor::new(&self.expr_data);
        let mut stack = Vec::<u64>::new();

        if push_cfa {
            stack.push(regs.cfa().addr());
        }

        let mut most_recent_location: Option<DwarfExpressionSimpleLocation> = None;
        let mut pieces = Vec::<DwarfExpressionPiece>::new();
        let mut result_is_address = true;

        // Get current program counter and function context
        let virt_pc = VirtualAddress::from(regs.read_by_id_as::<u64>(RegisterId::rip)?);
        let pc = virt_pc.to_file_addr_elf(&self.parent.upgrade().unwrap().elf_file());
        let func = self
            .parent
            .upgrade()
            .unwrap()
            .function_containing_address(&pc)?;

        // Binary operation helper
        let binop = |stack: &mut Vec<u64>, op: fn(u64, u64) -> u64| {
            let rhs = stack.pop().unwrap();
            let lhs = stack.pop().unwrap();
            stack.push(op(lhs, rhs));
        };

        // Relational operation helper
        let relop = |stack: &mut Vec<u64>, op: fn(i64, i64) -> bool| {
            let rhs = stack.pop().unwrap() as i64;
            let lhs = stack.pop().unwrap() as i64;
            stack.push(if op(lhs, rhs) { 1 } else { 0 });
        };

        // Get current location helper
        let get_current_location =
            |stack: &mut Vec<u64>,
             most_recent_location: &mut Option<DwarfExpressionSimpleLocation>,
             result_is_address: &mut bool|
             -> DwarfExpressionSimpleLocation {
                if stack.is_empty() {
                    most_recent_location
                        .take()
                        .unwrap_or(DwarfExpressionSimpleLocation::Empty {})
                } else if *result_is_address {
                    DwarfExpressionSimpleLocation::Address {
                        address: VirtualAddress::from(stack.pop().unwrap()),
                    }
                } else {
                    let value = stack.pop().unwrap();
                    *result_is_address = true;
                    DwarfExpressionSimpleLocation::Literal { value }
                }
            };

        while !cursor.finished() {
            let opcode = cursor.u8();

            // Handle DW_OP_lit0 to DW_OP_lit31
            if (DW_OP_lit0.0..=DW_OP_lit31.0).contains(&opcode) {
                stack.push((opcode - DW_OP_lit0.0) as u64);
            }
            // Handle DW_OP_breg0 to DW_OP_breg31
            else if (DW_OP_breg0.0..=DW_OP_breg31.0).contains(&opcode) {
                let reg = (opcode - DW_OP_breg0.0) as i32;
                let reg_info = register_info_by_dwarf(reg)?;
                let reg_val = regs.read(&reg_info)?;
                let offset = cursor.sleb128();
                let val = match reg_val {
                    RegisterValue::U64(v) => v,
                    _ => return SdbError::err("Invalid register value type for breg operation"),
                };
                stack.push((val as i64 + offset) as u64);
            }
            // Handle DW_OP_reg0 to DW_OP_reg31
            else if (DW_OP_reg0.0..=DW_OP_reg31.0).contains(&opcode) {
                let reg = (opcode - DW_OP_reg0.0) as u64;
                if self.in_frame_info {
                    let reg_info = register_info_by_dwarf(reg as i32)?;
                    let reg_val = regs.read(&reg_info)?;
                    let val = match reg_val {
                        RegisterValue::U64(v) => v,
                        _ => return SdbError::err("Invalid register value type for reg operation"),
                    };
                    stack.push(val);
                } else {
                    most_recent_location =
                        Some(DwarfExpressionSimpleLocation::Register { reg_num: reg });
                }
            }

            #[allow(non_upper_case_globals)]
            match DwOp(opcode) {
                DW_OP_addr => {
                    let addr =
                        FileAddress::new(&self.parent.upgrade().unwrap().elf_file(), cursor.u64());
                    let virt_addr = addr.to_virt_addr();
                    stack.push(virt_addr.addr());
                }
                DW_OP_const1u => stack.push(cursor.u8() as u64),
                DW_OP_const1s => stack.push(cursor.s8() as u64),
                DW_OP_const2u => stack.push(cursor.u16() as u64),
                DW_OP_const2s => stack.push(cursor.s16() as u64),
                DW_OP_const4u => stack.push(cursor.u32() as u64),
                DW_OP_const4s => stack.push(cursor.s32() as u64),
                DW_OP_const8u => stack.push(cursor.u64()),
                DW_OP_const8s => stack.push(cursor.s64() as u64),
                DW_OP_constu => stack.push(cursor.uleb128()),
                DW_OP_consts => stack.push(cursor.sleb128() as u64),

                DW_OP_bregx => {
                    let reg = cursor.uleb128() as i32;
                    let reg_info = register_info_by_dwarf(reg)?;
                    let reg_val = regs.read(&reg_info)?;
                    let offset = cursor.sleb128();
                    let val = match reg_val {
                        RegisterValue::U64(v) => v,
                        _ => {
                            return SdbError::err(
                                "Invalid register value type for bregx operation",
                            );
                        }
                    };
                    stack.push((val as i64 + offset) as u64);
                }

                DW_OP_fbreg => {
                    let offset = cursor.sleb128();
                    if let Some(func) = &func {
                        let fb_loc = func
                            .index(DW_AT_frame_base.0 as u64)?
                            .as_evaluated_location(proc, regs, true)?;
                        let fb_addr = read_frame_base_result(&fb_loc, regs)?;
                        stack.push((fb_addr.addr() as i64 + offset) as u64);
                    } else {
                        return SdbError::err("No function context for DW_OP_fbreg");
                    }
                }

                // Stack manipulation operations
                DW_OP_dup => {
                    let val = *stack.last().unwrap();
                    stack.push(val);
                }
                DW_OP_drop => {
                    stack.pop();
                }
                DW_OP_pick => {
                    let idx = cursor.u8() as usize;
                    let val = stack[stack.len() - 1 - idx];
                    stack.push(val);
                }
                DW_OP_over => {
                    let val = stack[stack.len() - 2];
                    stack.push(val);
                }
                DW_OP_swap => {
                    let len = stack.len();
                    stack.swap(len - 1, len - 2);
                }
                DW_OP_rot => {
                    let c = stack.pop().unwrap();
                    let b = stack.pop().unwrap();
                    let a = stack.pop().unwrap();
                    stack.push(c);
                    stack.push(a);
                    stack.push(b);
                }

                // Memory operations
                DW_OP_deref => {
                    let addr = VirtualAddress::from(stack.pop().unwrap());
                    let val = proc.read_memory_as::<u64>(addr)?;
                    stack.push(val);
                }
                DW_OP_deref_size => {
                    let addr = VirtualAddress::from(stack.pop().unwrap());
                    let size = cursor.u8() as usize;
                    let mem = proc.read_memory(addr, size)?;
                    let mut val = 0u64;
                    for (i, &byte) in mem.iter().enumerate().take(8) {
                        val |= (byte as u64) << (i * 8);
                    }
                    stack.push(val);
                }

                // Unsupported operations
                DW_OP_xderef => return SdbError::err("DW_OP_xderef not supported"),
                DW_OP_xderef_size => return SdbError::err("DW_OP_xderef_size not supported"),
                DW_OP_push_object_address => {
                    return SdbError::err("Unsupported opcode DW_OP_push_object_address");
                }
                DW_OP_form_tls_address => {
                    return SdbError::err("Unsupported opcode DW_OP_form_tls_address");
                }

                DW_OP_call_frame_cfa => {
                    stack.push(regs.cfa().addr());
                }

                // Arithmetic operations
                DW_OP_minus => binop(&mut stack, |lhs, rhs| lhs.wrapping_sub(rhs)),
                DW_OP_mod => binop(&mut stack, |lhs, rhs| lhs % rhs),
                DW_OP_mul => binop(&mut stack, |lhs, rhs| lhs.wrapping_mul(rhs)),
                DW_OP_and => binop(&mut stack, |lhs, rhs| lhs & rhs),
                DW_OP_or => binop(&mut stack, |lhs, rhs| lhs | rhs),
                DW_OP_plus => binop(&mut stack, |lhs, rhs| lhs.wrapping_add(rhs)),
                DW_OP_shl => binop(&mut stack, |lhs, rhs| lhs << rhs),
                DW_OP_shr => binop(&mut stack, |lhs, rhs| lhs >> rhs),
                DW_OP_shra => binop(&mut stack, |lhs, rhs| ((lhs as i64) >> rhs) as u64),
                DW_OP_xor => binop(&mut stack, |lhs, rhs| lhs ^ rhs),

                DW_OP_div => {
                    let rhs = stack.pop().unwrap() as i64;
                    let lhs = stack.pop().unwrap() as i64;
                    stack.push((lhs / rhs) as u64);
                }

                // Unary operations
                DW_OP_abs => {
                    let val = stack.pop().unwrap() as i64;
                    stack.push(val.unsigned_abs());
                }
                DW_OP_neg => {
                    let val = stack.pop().unwrap() as i64;
                    stack.push((-val) as u64);
                }
                DW_OP_plus_uconst => {
                    let val = stack.pop().unwrap();
                    let const_val = cursor.uleb128();
                    stack.push(val + const_val);
                }
                DW_OP_not => {
                    let val = stack.pop().unwrap();
                    stack.push(!val);
                }

                // Comparison operations
                DW_OP_le => relop(&mut stack, |lhs, rhs| lhs <= rhs),
                DW_OP_ge => relop(&mut stack, |lhs, rhs| lhs >= rhs),
                DW_OP_eq => relop(&mut stack, |lhs, rhs| lhs == rhs),
                DW_OP_lt => relop(&mut stack, |lhs, rhs| lhs < rhs),
                DW_OP_gt => relop(&mut stack, |lhs, rhs| lhs > rhs),
                DW_OP_ne => relop(&mut stack, |lhs, rhs| lhs != rhs),

                // Control flow operations
                DW_OP_skip => {
                    let offset = cursor.s16();
                    cursor += offset as usize;
                }
                DW_OP_bra => {
                    let test_val = stack.pop().unwrap();
                    let offset = cursor.s16();
                    if test_val != 0 {
                        cursor += offset as usize;
                    }
                }

                // Unsupported call operations
                DW_OP_call2 => return SdbError::err("Unsupported opcode DW_OP_call2"),
                DW_OP_call4 => return SdbError::err("Unsupported opcode DW_OP_call4"),
                DW_OP_call_ref => return SdbError::err("Unsupported opcode DW_OP_call_ref"),

                DW_OP_regx => {
                    let reg = cursor.uleb128();
                    if self.in_frame_info {
                        let reg_info = register_info_by_dwarf(reg as i32)?;
                        let reg_val = regs.read(&reg_info)?;
                        let val = match reg_val {
                            RegisterValue::U64(v) => v,
                            _ => {
                                return SdbError::err(
                                    "Invalid register value type for regx operation",
                                );
                            }
                        };
                        stack.push(val);
                    } else {
                        most_recent_location =
                            Some(DwarfExpressionSimpleLocation::Register { reg_num: reg });
                    }
                }

                DW_OP_implicit_value => {
                    let length = cursor.uleb128() as usize;
                    let data = cursor.position().slice(..length);
                    most_recent_location = Some(DwarfExpressionSimpleLocation::Data { data });
                }

                DW_OP_stack_value => {
                    result_is_address = false;
                }

                DW_OP_nop => {
                    // Do nothing
                }

                // Piece operations
                DW_OP_piece => {
                    let byte_size = cursor.uleb128();
                    let loc = get_current_location(
                        &mut stack,
                        &mut most_recent_location,
                        &mut result_is_address,
                    );
                    pieces.push(DwarfExpressionPiece {
                        location: loc,
                        bit_size: byte_size * 8,
                        offset: 0,
                    });
                }

                DW_OP_bit_piece => {
                    let bit_size = cursor.uleb128();
                    let offset = cursor.uleb128();
                    let loc = get_current_location(
                        &mut stack,
                        &mut most_recent_location,
                        &mut result_is_address,
                    );
                    pieces.push(DwarfExpressionPiece {
                        location: loc,
                        bit_size,
                        offset,
                    });
                }

                _ => {}
            }
        }

        if !pieces.is_empty() {
            return Ok(DwarfExpressionResult::Pieces(DwarfExpressionPiecesResult {
                pieces,
            }));
        }

        let final_location = get_current_location(
            &mut stack,
            &mut most_recent_location,
            &mut result_is_address,
        );
        Ok(DwarfExpressionResult::SimpleLocation(final_location))
    }
}

fn read_frame_base_result(
    loc: &DwarfExpressionResult,
    _regs: &Registers,
) -> Result<VirtualAddress, SdbError> {
    let simple_loc = match loc {
        DwarfExpressionResult::SimpleLocation(simple_loc) => simple_loc,
        _ => return SdbError::err("Unsupported frame base location"),
    };
    let addr_res = match simple_loc {
        DwarfExpressionSimpleLocation::Address { address } => address,
        _ => return SdbError::err("Unsupported frame base location"),
    };
    Ok(*addr_res)
}