coreminer 0.5.2

A debugger which can be used to debug programs that do not want to be debugged
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
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
//! # Debugger Module
//!
//! This module provides the core debugging functionality, coordinating between the user interface and the debugged process.
//!
//! The debugger is the central orchestrator in coreminer that:
//!
//! 1. **Controls process execution** - Manages starting, stopping, and stepping through the target program
//! 2. **Handles breakpoints** - Sets, removes, and manages hitting breakpoints during execution
//! 3. **Provides memory access** - Reads and writes to the target's memory space
//! 4. **Exposes registers** - Allows inspection and modification of CPU registers
//! 5. **Interprets debug info** - Uses DWARF debug information to resolve symbols, variables, and source locations
//! 6. **Manages signals** - Intercepts and processes signals from the target process
//! 7. **Handles user interaction** - Communicates with the UI to present information and receive commands
//!
//! ## Architecture
//!
//! The debugger uses a combination of:
//!
//! - **ptrace** - For low-level process control and memory access
//! - **DWARF debug information** - To understand program structure and variables
//! - **waitpid** - To synchronize with the debugged process's state changes
//! - **signal handling** - To interpret and respond to exceptions in the target
//! - **[Debuggee]** - Various methods of the [Debuggee] struct.
//!

use std::collections::HashMap;
use std::ffi::CString;
use std::fmt::Display;
use std::path::{Path, PathBuf};
#[cfg(feature = "plugins")]
use std::sync::{Arc, Mutex};

use iced_x86::FormatterTextKind;
use nix::sys::ptrace;
use nix::sys::signal::Signal;
use nix::sys::wait::{waitpid, WaitPidFlag, WaitStatus};
use nix::unistd::execv;
use tracing::{debug, error, info, trace, warn};
use which::which;

use crate::breakpoint::Breakpoint;
use crate::consts::{SI_KERNEL, TRAP_BRKPT, TRAP_TRACE};
use crate::dbginfo::{CMDebugInfo, OwnedSymbol};
use crate::debuggee::Debuggee;
use crate::disassemble::Disassembly;
use crate::dwarf_parse::FrameInfo;
use crate::errors::{DebuggerError, Result};
use crate::feedback::{Feedback, InternalFeedback, Status};
use crate::ui::DebuggerUI;
use crate::variable::{VariableExpression, VariableValue};
use crate::{mem_read_word, mem_write_word, unwind, Addr, Register, Word};

// plugin stuff
use crate::for_hooks; // does nothing without the feature
#[cfg(feature = "plugins")]
use crate::plugins::extension_points::{EPreSignalHandler, EPreSigtrap};
#[cfg(feature = "plugins")]
use steckrs::{PluginIDOwned, PluginManager};

/// Manages the debugging session and coordinates between the UI and debuggee
///
/// The [`Debugger`] struct is the central component that ties together the user interface and
/// the debugged process. It holds state for the current debugging session, processes commands
/// from the UI, and controls the execution of the debuggee.
///
/// # Type Parameters
///
/// * `'executable` - Lifetime of the executable data
/// * `UI` - The user interface type, which must implement [`DebuggerUI`]
///
/// ## Examples
///
/// Basic usage of the debugger:
///
/// ```no_run
/// #[cfg(feature = "cli")]
/// # mod featguard { fn _do_thing() {
/// use coreminer::debugger::Debugger;
/// use coreminer::ui::cli::CliUi;
/// use coreminer::errors::Result;
/// use std::path::Path;
/// use std::ffi::CString;
///
/// fn main() -> Result<()> {
///     // Create a UI implementation, this may be antthing implementing the DebuggerUI trait
///     let ui = CliUi::build(None)?;
///
///     // Build a debugger with the UI
///     let mut debugger = Debugger::build(ui)?;
///
///     // Run the main interactive debugging loop
///     debugger.run_debugger()?;
///
///     // Clean up resources
///     debugger.cleanup()?;
///
///     Ok(())
/// }
///
/// # }}
/// ```
///
/// Theoretically, automated usage of the debugger functions is also possible:
///
///
/// ```no_run
/// #[cfg(feature = "cli")]
/// # mod featguard { fn _do_thing() {
/// use coreminer::debugger::Debugger;
/// use coreminer::ui::cli::CliUi;
/// use coreminer::errors::Result;
/// use coreminer::feedback::Feedback;
/// use std::path::Path;
/// use std::ffi::CString;
///
/// fn main() -> Result<()> {
///     // Create a UI implementation, this may be antthing implementing the DebuggerUI trait
///     // for automated, this is not really needed.
///     let ui = CliUi::build(None)?;
///
///     // Build a debugger with the UI
///     let mut debugger = Debugger::build(ui)?;
///
///     // Launch a program for debugging
///     let program_path = Path::new("./target/debug/my_program");
///     let args = vec![CString::new("my_program").unwrap(), CString::new("my_program").unwrap()];
///     // returns control shortly after forking off the debuggee as child process
///     debugger.run(program_path, &args)?;
///
///     if let Feedback::Registers(regs) = debugger.dump_regs()? {
///         println!("rip is here: {}", regs.rip)
///     } else {
///         eprintln!("something did not work!")
///     }
///
///     // Step over a single instruction
///     debugger.single_step()?;
///
///     // more automated debugging...
///
///     // Clean up resources
///     debugger.cleanup()?;
///
///     Ok(())
/// }
///
/// # }}
/// ```
pub struct Debugger<'executable, UI: DebuggerUI> {
    pub(crate) debuggee: Option<Debuggee>,
    pub(crate) ui: UI,
    stored_obj_data: Option<object::File<'executable>>,
    stored_obj_data_raw: Vec<u8>,
    last_signal: Option<Signal>,
    #[cfg(feature = "plugins")]
    plugins: Arc<Mutex<PluginManager>>,
}

impl<'executable, UI: DebuggerUI> Debugger<'executable, UI> {
    /// Creates a new debugger with the provided user interface
    ///
    /// # Parameters
    ///
    /// * `ui` - The user interface implementation
    ///
    /// # Returns
    ///
    /// * `Ok(Debugger)` - A new debugger instance
    /// * `Err(DebuggerError)` - If the debugger could not be created
    ///
    /// # Errors
    ///
    /// Cannot fail.
    ///
    /// # Examples
    ///
    ///
    /// ```
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// use coreminer::debugger::Debugger;
    /// use coreminer::ui::cli::CliUi;
    ///
    /// let ui = CliUi::build(None).unwrap();
    /// let debugger = Debugger::build(ui).unwrap();
    ///
    /// # }}
    /// ```
    pub fn build(ui: UI) -> Result<Self> {
        Ok(Debugger {
            debuggee: None,
            ui,
            stored_obj_data: None,
            stored_obj_data_raw: Vec::new(),
            last_signal: None,
            #[cfg(feature = "plugins")]
            plugins: Arc::new(crate::plugins::default_plugin_manager().into()),
        })
    }

    /// Launches a new debuggee process
    ///
    /// This function loads an executable, parses its debug information, forks a new process,
    /// and sets up ptrace for debugging.
    ///
    /// # Parameters
    ///
    /// * `path` - Path to the executable
    /// * `arguments` - Command-line arguments for the executable
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the debuggee was successfully launched
    /// * `Err(DebuggerError)` - If the debuggee could not be launched
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The executable does not exist
    /// - The executable is not a valid file
    /// - Debug information cannot be parsed
    /// - The process cannot be forked
    /// - ptrace cannot be initialized
    ///
    /// # Panics
    ///
    /// This function will panic if the the argument vector cannot be built from the path and the
    /// arguments. This can happen if the path has unicode.
    fn launch_debuggee(&mut self, path: impl AsRef<Path>, arguments: &[CString]) -> Result<()> {
        let path = path.as_ref();
        let path_as_cstring = CString::new(path.to_string_lossy().as_bytes())
            .expect("could not make argv from given path and args");
        let mut argv: Vec<&CString> = vec![&path_as_cstring];
        argv.extend(arguments);
        if !path.exists() {
            let err = DebuggerError::ExecutableDoesNotExist;
            error!("{err}");
            return Err(err);
        }
        if !path.is_file() {
            let err = DebuggerError::ExecutableIsNotAFile;
            error!("{err}");
            return Err(err);
        }

        let executable_obj_data: object::File<'_> = self.stored_obj_data.take().unwrap();

        let dbginfo: CMDebugInfo = CMDebugInfo::build(executable_obj_data)?;

        let fork_res = unsafe { nix::unistd::fork() };
        match fork_res {
            Err(e) => {
                error!("could not start executable: {e}");
                Err(e.into())
            }
            Ok(fr) => match fr {
                nix::unistd::ForkResult::Parent { child: pid } => {
                    let dbge = Debuggee::build(pid, &dbginfo, HashMap::new())?;
                    self.debuggee = Some(dbge);
                    Ok(())
                }
                nix::unistd::ForkResult::Child => {
                    let cpath = CString::new(path.to_string_lossy().to_string().as_str())?;
                    trace!("CHILD: requested run with executable={cpath:?} and argv={argv:?}");
                    ptrace::traceme()
                        .inspect_err(|e| eprintln!("error while doing traceme: {e}"))?;
                    execv(&cpath, &argv)?; // NOTE: unsure if args[0] is set to the executable
                    unreachable!()
                }
            },
        }
    }

    /// Waits for a signal from the debuggee and processes it
    ///
    /// This function waits for signals from the debuggee, such as breakpoints, signals, or exits,
    /// and processes them appropriately.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback)` - The result of the wait operation
    /// * `Err(DebuggerError)` - If there was an error during waiting
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - waitpid fails
    /// - Signal information cannot be retrieved
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// match debugger.wait_signal() {
    ///     Ok(Feedback::Exit(code)) => println!("Process exited with code {}", code),
    ///     Ok(Feedback::Ok) => println!("Process stopped"),
    ///     Ok(other) => println!("something else happened: {other}"), // impossible
    ///     Err(e) => eprintln!("Error: {}", e),
    /// }
    ///
    /// # }}
    /// ```
    // BUG: this seems to eat signals, the debuggee does not get them. This is especially bad for
    // SIGTERM #43
    pub fn wait_signal(&mut self) -> Result<Feedback> {
        trace!("new wait signal iteration");
        match self.wait(&[])? {
            WaitStatus::Exited(_, exit_code) => Ok(Feedback::Exit(exit_code)),
            WaitStatus::Signaled(_, signal, _) => {
                info!("Debuggee terminated by signal: {}", signal);
                Ok(Feedback::Exit(-1))
            }
            wait_status => {
                // Get and handle other signals as before
                let siginfo = ptrace::getsiginfo(
                    self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?.pid,
                )?;
                let sig = Signal::try_from(siginfo.si_signo)?;
                debug!("wait status: {wait_status:?}");

                for_hooks!(
                    for hook[EPreSignalHandler] in self {
                        self.hook_feedback_loop(hook.name(), |f| {
                            hook.inner_mut().pre_handle_signal(f, &siginfo, &sig, &wait_status)
                        })?;
                    }
                );

                match sig {
                    Signal::SIGTRAP => {
                        self.handle_sigtrap(sig, siginfo)?;
                        Ok(Feedback::Ok)
                    }
                    Signal::SIGSEGV
                    | Signal::SIGINT
                    | Signal::SIGPIPE
                    | Signal::SIGSTOP
                    | Signal::SIGWINCH
                    | Signal::SIGTERM
                    | Signal::SIGILL => {
                        self.handle_important_signal(sig, siginfo)?;
                        Ok(Feedback::Ok)
                    }
                    _ => {
                        self.handle_other_signal(sig, siginfo)?;
                        Ok(Feedback::Ok)
                    }
                }
            }
        }
    }

    /// Low-level wait for a change in the debuggee's state
    ///
    /// # Parameters
    ///
    /// * `options` - Options to pass to waitpid, usually `&[]`
    ///
    /// # Returns
    ///
    /// * `Ok(WaitStatus)` - The status of the wait operation
    /// * `Err(DebuggerError)` - If there was an error during waiting
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - waitpid fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use nix::sys::wait::{WaitPidFlag, WaitStatus};
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Wait for any status change without options
    /// let status = debugger.wait(&[]).unwrap();
    ///
    /// // Wait with the WNOHANG option (non-blocking)
    /// let status = debugger.wait(&[WaitPidFlag::WNOHANG]).unwrap();
    ///
    /// match status {
    ///     WaitStatus::Exited(_, code) => println!("Process exited with code {}", code),
    ///     WaitStatus::Stopped(_, signal) => println!("Process stopped by signal {:?}", signal),
    ///     _ => println!("Other status change"),
    /// }
    ///
    /// # }}
    /// ```
    pub fn wait(&self, options: &[WaitPidFlag]) -> Result<WaitStatus> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        let mut flags = WaitPidFlag::empty();
        trace!("wait flags: {flags:?}");
        for f in options {
            flags |= *f;
        }
        Ok(waitpid(
            dbge.pid,
            if flags.is_empty() { None } else { Some(flags) },
        )?)
    }

    /// Runs the main debugger loop
    ///
    /// This function forms the main execution loop of the debugger, processing
    /// UI commands and executing corresponding debugger actions until the user
    /// requests to quit.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the debugger loop completed successfully
    /// * `Err(DebuggerError)` - If there was an error during execution
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee already exists but can't be waited for.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// let ui = CliUi::build(None).unwrap();
    /// let mut debugger = Debugger::build(ui).unwrap();
    ///
    /// // Start the main debugger loop
    /// // This will run until the ui exits
    /// debugger.run_debugger().unwrap();
    ///
    /// debugger.cleanup().unwrap();
    ///
    /// # }}
    /// ```
    pub fn run_debugger(&mut self) -> Result<()> {
        if self.debuggee.as_ref().is_some() {
            self.wait_signal()?; // wait until the debuggee is stopped
        } else {
            info!("debuggee not yet launched");
        }

        let mut feedback: Feedback = Feedback::Ok;
        loop {
            let ui_res = self.ui.process(feedback);
            feedback = {
                match ui_res {
                    Err(e) => {
                        error!("{e}");
                        return Err(e);
                    }
                    Ok(s) => match self.process_status(&s) {
                        Ok(Feedback::Internal(InternalFeedback::Quit)) => break,
                        other => other,
                    },
                }
            }
            .into();

            // Clean up if process exited
            if let Feedback::Exit(_) = feedback {
                self.debuggee = None;
            }
        }

        Ok(())
    }

    /// Process a [`Status`] by executing the specified action.
    ///
    /// This function takes a [`Status`] and has the debugger perform actions to generate
    /// a [`Feedback`].
    ///
    /// For example, a [`Status`] might request that the [`Debugger`] should read some [`Word`]
    /// from the memory of the [`Debuggee`] with [`Status::ReadMem`].
    ///
    /// This function will then match
    /// the [`Status`] to the fitting implementation function, [`Self::read_mem`] for this example,
    /// execute it, and return the [`Feedback`] ([`Feedback::Word`]).
    ///
    /// If the action taken fails, a [`Err`] variant will be returned instead. Since [`Feedback`]
    /// can be constructed from a [`DebuggerError`], you can wrap the error in a [`Feedback`] and
    /// send this to the [`DebuggerUI`] or a [`Plugin`](steckrs::Plugin) with [`Self::hook_feedback_loop`].
    ///
    /// # Parameters
    ///
    /// * `status` - A [`Status`] to be processed
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback)` - The result of the action detailed by the `status`
    /// * `Err(DebuggerError)` - If there was an error performing the requested action
    ///
    /// # Errors
    ///
    /// This function will return an error if the taken action for that [`Status`] fails.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// # use coreminer::feedback::{Feedback, Status};
    /// #
    /// let ui = CliUi::build(None).unwrap();
    /// let mut debugger = Debugger::build(ui).unwrap();
    ///
    /// let status = Status::ReadMem(Addr::from(98421479usize));
    /// let feedback: Feedback = debugger.process_status(&status).unwrap();
    ///
    /// if let Feedback::Word(w) = feedback {
    ///     println!("read the word: {w}");
    /// } else {
    ///     eprintln!("could not read the word");
    /// }
    ///
    /// # }}
    /// ```
    pub fn process_status(&mut self, status: &Status) -> Result<Feedback> {
        match status {
            Status::Infos => self.infos(),
            Status::DebuggerQuit => Ok(Feedback::Internal(InternalFeedback::Quit)),
            Status::Continue => self.cont(),
            Status::SetBreakpoint(addr) => self.set_bp(*addr),
            Status::DelBreakpoint(addr) => self.del_bp(*addr),
            Status::DumpRegisters => self.dump_regs(),
            Status::SetRegister(r, v) => self.set_reg(*r, *v),
            Status::WriteMem(a, v) => self.write_mem(*a, *v),
            Status::ReadMem(a) => self.read_mem(*a),
            Status::DisassembleAt(a, l, literal) => self.disassemble_at(*a, *l, *literal),
            Status::GetSymbolsByName(s) => self.get_symbol_by_name(s),
            Status::StepSingle => self.single_step(),
            Status::StepOut => self.step_out(),
            Status::StepInto => self.step_into(),
            Status::StepOver => self.step_over(),
            Status::Backtrace => self.backtrace(),
            Status::ReadVariable(va) => self.read_variable(va),
            Status::WriteVariable(va, val) => self.write_variable(va, *val),
            Status::GetStack => self.get_stack(),
            Status::ProcMap => self.get_process_map(),
            Status::Run(exe, args) => self.run(exe, args),
            Status::GetBreakpoint(addr) => self.get_bp(*addr),
            Status::SetLastSignal(signum) => self.set_last_signal(*signum),
            #[cfg(feature = "plugins")]
            Status::PluginContinue => Err(DebuggerError::UiUsedPluginContinue),
            #[cfg(feature = "plugins")]
            Status::PluginSetEnable(id, status) => self.plugin_set_enable(id, *status),
            #[cfg(feature = "plugins")]
            Status::PluginGetStatus(id) => self.plugin_get_status(id),
            #[cfg(feature = "plugins")]
            Status::PluginGetList => self.list_plugins(),
        }
    }

    /// Continues execution of the debuggee, optionally delivering a signal
    ///
    /// This function tells the debuggee to continue execution from its current state.
    /// If a signal is provided, it will be delivered to the debuggee.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback)` - The result of the continuation
    /// * `Err(DebuggerError)` - If there was an error during continuation
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - ptrace's cont operation fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use nix::sys::signal::Signal;
    /// #
    /// let ui = CliUi::build(None).unwrap();
    /// let mut debugger = Debugger::build(ui).unwrap();
    /// // Assume debuggee is already running
    ///
    /// // Continue execution
    /// debugger.cont().unwrap();
    ///
    /// # }}
    /// ```
    pub fn cont(&mut self) -> Result<Feedback> {
        if self.go_back_step_over_bp()? {
            info!("breakpoint before, caught up and continueing with single step");
        }
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        ptrace::cont(dbge.pid, self.take_last_status())?;

        self.wait_signal() // wait until the debuggee is stopped again!!!
    }

    /// Gets the current registers of the debuggee
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Registers)` - The registers
    /// * `Err(DebuggerError)` - If there was an error retrieving registers
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - ptrace's getregs operation fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// if let Ok(Feedback::Registers(regs)) = debugger.dump_regs() {
    ///     println!("RIP: {:#x}", regs.rip);
    ///     println!("RSP: {:#x}", regs.rsp);
    ///     println!("RAX: {:#x}", regs.rax);
    /// }
    ///
    /// # }}
    /// ```
    pub fn dump_regs(&self) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        let regs = ptrace::getregs(dbge.pid)?;
        Ok(Feedback::Registers(regs.into()))
    }

    /// Cleans up resources used by the debugger
    ///
    /// This function terminates the debuggee if it's still running
    /// and releases any resources held by the debugger.
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If cleanup was successful
    /// * `Err(DebuggerError)` - If there was an error during cleanup
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The exists but debuggee cannot be killed with [`Debuggee::kill`]
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// #
    /// // Clean up resources at the end of debugging
    /// debugger.cleanup().unwrap();
    ///
    /// # }}
    /// ```
    pub fn cleanup(&mut self) -> Result<()> {
        if let Some(dbge) = &self.debuggee {
            dbge.kill()?;
            self.debuggee = None;
        }
        Ok(())
    }

    /// Sets a breakpoint at the specified address
    ///
    /// # Parameters
    ///
    /// * `addr` - The address to set the breakpoint at
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the breakpoint was set successfully
    /// * `Err(DebuggerError)` - If there was an error setting the breakpoint
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The breakpoint could not be enabled
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Set a breakpoint at absolute address 0x1000
    /// debugger.set_bp(Addr::from(0x1000usize)).unwrap();
    ///
    /// // Set a breakpoint at the program's entry point
    /// let base = debugger.get_current_addr().unwrap();
    /// debugger.set_bp(base).unwrap();
    ///
    /// // Set a breakpoint at the relative address 0x1000
    /// let base = debugger.get_current_addr().unwrap();
    /// debugger.set_bp(base + 0x1000).unwrap();
    ///
    /// # }}
    /// ```
    pub fn set_bp(&mut self, addr: Addr) -> Result<Feedback> {
        let dbge = self.debuggee.as_mut().ok_or(DebuggerError::NoDebugee)?;
        let mut bp = Breakpoint::new(dbge.pid, addr);
        bp.enable()?;
        dbge.breakpoints.insert(addr, bp);

        Ok(Feedback::Ok)
    }

    /// Removes a breakpoint at the specified address
    ///
    /// # Parameters
    ///
    /// * `addr` - The address to remove the breakpoint from
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the breakpoint was removed successfully
    /// * `Err(DebuggerError)` - If there was an error removing the breakpoint
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The breakpoint could not be disabled
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Remove a breakpoint at address 0x1000
    /// debugger.del_bp(Addr::from(0x1000usize)).unwrap();
    ///
    /// // Remove a breakpoint at the relative address 0x1000
    /// let base = debugger.get_current_addr().unwrap();
    /// debugger.del_bp(base + 0x1000).unwrap();
    ///
    /// # }}
    /// ```
    pub fn del_bp(&mut self, addr: Addr) -> Result<Feedback> {
        let dbge = self.debuggee.as_mut().ok_or(DebuggerError::NoDebugee)?;

        if let Some(_bp) = dbge.breakpoints.get_mut(&addr) {
            dbge.breakpoints.remove(&addr); // gets disabled on dropping
        } else {
            warn!("removed a breakpoint at {addr:x?} that did not exist");
        }

        Ok(Feedback::Ok)
    }

    /// Performs a single, atomic step of exactly one instruction through the debuggee
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the step was successful
    /// * `Err(DebuggerError)` - If there was an error during stepping
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - ptrace's step operation fails
    fn atomic_single_step(&mut self) -> Result<()> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        // FIXME: this is probably noticeable
        if let Err(e) = ptrace::step(dbge.pid, self.take_last_status()) {
            error!("could not do atomic step: {e}");
            return Err(e.into());
        }

        Ok(())
    }

    /// Steps a single instruction in the debuggee
    ///
    /// This function advances execution by a single instruction, taking
    /// care to handle breakpoints appropriately.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the step was successful
    /// * `Err(DebuggerError)` - If there was an error during stepping
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - ptrace operations fail
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Step through one instruction
    /// debugger.single_step().unwrap();
    ///
    /// // Step through multiple instructions
    /// for _ in 0..5 {
    ///     debugger.single_step().unwrap();
    /// }
    ///
    /// # }}
    /// ```
    pub fn single_step(&mut self) -> Result<Feedback> {
        if self.go_back_step_over_bp()? {
            info!("breakpoint before, caught up and continueing with single step");
        }
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let maybe_bp_addr: Addr = self.get_current_addr()?;
        if dbge.breakpoints.contains_key(&maybe_bp_addr) {
            trace!("step over instruction with breakpoint");
            self.dse(maybe_bp_addr)?;
        } else {
            trace!("step regular instruction");
            self.atomic_single_step()?;
            self.wait_signal()?;
        }
        trace!("now at {:018x}", self.get_reg(Register::rip)?);

        Ok(Feedback::Ok)
    }

    /// Steps out of the current function
    ///
    /// This function sets a temporary breakpoint at the return address
    /// and continues execution until that breakpoint is hit.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the step-out was successful
    /// * `Err(DebuggerError)` - If there was an error during step-out
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - No valid return address of the current function can be found
    /// - The current function is main (cannot step out)
    /// - Could not read or write a [Register].
    /// - Could not read from memory.
    /// - Could not set or delete a [Breakpoint].
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running and in a function
    /// #
    /// // Step out of the current function
    /// debugger.step_out().unwrap();
    ///
    /// # }}
    /// ```
    pub fn step_out(&mut self) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        {
            let a = dbge.get_function_by_addr(self.get_reg(Register::rip)?.into())?;
            if let Some(s) = a {
                debug!("step out in following function: {s:#?}");
                if s.name() == Some("main") {
                    error!("you're about to do something stupid: no stepping out of the earliest stack frame allowed");
                    return Err(DebuggerError::StepOutMain);
                }
            } else {
                warn!("did not find debug symbol for current address");
            }
        }

        let stack_frame_pointer: Addr = self.get_reg(Register::rbp)?.into();
        let return_addr: Addr = mem_read_word(dbge.pid, stack_frame_pointer + 8)?.into();
        trace!("rsb: {stack_frame_pointer}");
        trace!("ret_addr: {return_addr}");

        let should_remove_breakpoint = if dbge.breakpoints.contains_key(&return_addr) {
            false
        } else {
            self.set_bp(return_addr)?;
            true
        };

        self.cont()?;

        if should_remove_breakpoint {
            self.del_bp(return_addr)?;
            self.set_reg(Register::rip, self.get_reg(Register::rip)? - 1)?; // we need to go back
                                                                            // else we skip an instruction
        }
        Ok(Feedback::Ok)
    }

    /// Temporarily disables a breakpoint, steps over it, and then re-enables it
    ///
    /// # Parameters
    ///
    /// * `here` - The address of the breakpoint
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the operation was successful
    /// * `Err(DebuggerError)` - If there was an error
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Breakpoint operations fail
    /// - Step operations fail
    fn dse(&mut self, here: Addr) -> Result<()> {
        trace!("disabling the breakpoint");
        self.debuggee
            .as_mut()
            .unwrap()
            .breakpoints
            .get_mut(&here)
            .unwrap()
            .disable()?;

        trace!("atomic step");
        self.atomic_single_step()?;
        trace!("waiting");
        self.wait_signal()
            .inspect_err(|e| warn!("weird wait_signal error: {e}"))?;
        trace!("enable stepped over bp again");
        self.debuggee
            .as_mut()
            .unwrap()
            .breakpoints
            .get_mut(&here)
            .unwrap()
            .enable()?;
        trace!("dse done");

        Ok(())
    }

    /// Checks if we need to restore an instruction pointer after hitting a breakpoint
    ///
    /// When a breakpoint is hit, the instruction pointer is just after the INT3 instruction.
    /// This function checks if we need to move the instruction pointer back to the breakpoint
    /// address and execute the original instruction. If so, it does that.
    ///
    /// # Returns
    ///
    /// * `Ok(bool)` - True if the IP was adjusted and a breakpoint was stepped over
    /// * `Err(DebuggerError)` - If there was an error
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Register operations fail
    /// - Breakpoint operations fail
    #[allow(clippy::missing_panics_doc)] // this function cant panic
    pub fn go_back_step_over_bp(&mut self) -> Result<bool> {
        if self.debuggee.is_none() {
            return Err(DebuggerError::NoDebugee);
        }

        let maybe_bp_addr: Addr = self.get_current_addr()? - 1;
        trace!("Checkinf if {maybe_bp_addr} had a breakpoint");

        if self
            .debuggee
            .as_mut()
            // safe because we check earlier, needed because we use a mutable reference
            // and can only drop in place this way
            .unwrap()
            .breakpoints
            .get_mut(&maybe_bp_addr)
            .is_some_and(|a| a.is_enabled())
        {
            let here = maybe_bp_addr;
            trace!("set register to {here}");
            self.set_reg(Register::rip, here.into())?;

            self.dse(here)?;
            Ok(true)
        } else {
            trace!("breakpoint is disabled or does not exist, doing nothing");
            Ok(false)
        }
    }

    /// Disassembles memory at the specified address
    ///
    /// # Parameters
    ///
    /// * `addr` - The starting address
    /// * `len` - The number of bytes to disassemble
    /// * `literal` - Whether to show literal bytes instead of original code (including breakpoint instructions)
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Disassembly)` - The disassembly result
    /// * `Err(DebuggerError)` - If there was an error during disassembly
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Memory cannot be read
    /// - Disassembly fails
    ///
    /// # Panics
    ///
    /// If a [Breakpoint] is enabled but has no saved data, this will panic.
    /// If a [Breakpoint] was found before making the [Disassembly], but the same breakpoint does
    /// not exist after the [Disassembly] was created, this will also panic.
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Disassemble 16 bytes at address 0x1000
    /// if let Ok(Feedback::Disassembly(disasm)) = debugger.disassemble_at(Addr::from(0x1000usize), 16, false) {
    ///     for (addr, raw, content, has_bp) in disasm.inner() {
    ///         println!("{}: {} {}", addr, if *has_bp { "*" } else { " " }, content[0].0);
    ///     }
    /// }
    ///
    /// # }}
    /// ```
    pub fn disassemble_at(&self, addr: Addr, len: usize, literal: bool) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let t = dbge.disassemble(addr, len, literal)?;

        Ok(Feedback::Disassembly(t))
    }

    /// Searches for symbols by name
    ///
    /// # Parameters
    ///
    /// * `name` - The name to search for
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Symbols)` - The matching symbols
    /// * `Err(DebuggerError)` - If there was an error during search
    ///
    /// Note: If the executable that is being debugged has no DWARF information (was stripped), this will always
    /// return no symbols.
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debug information is not loaded
    /// - Symbol information is not available
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Find the "main" function
    /// if let Ok(Feedback::Symbols(symbols)) = debugger.get_symbol_by_name("main") {
    ///     for symbol in symbols {
    ///         println!("Found main at: {:?}", symbol.low_addr());
    ///     }
    /// }
    ///
    /// # }}
    /// ```
    pub fn get_symbol_by_name(&self, name: impl Display) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let symbols: Vec<OwnedSymbol> = dbge.get_symbol_by_name(name)?;
        Ok(Feedback::Symbols(symbols))
    }

    /// Handles a SIGTRAP signal from the debuggee
    ///
    /// # Parameters
    ///
    /// * `sig` - The signal
    /// * `siginfo` - The signal information
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the signal was handled successfully
    /// * `Err(DebuggerError)` - If there was an error handling the signal
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    pub fn handle_sigtrap(
        &mut self,
        sig: nix::sys::signal::Signal,
        siginfo: nix::libc::siginfo_t,
    ) -> Result<()> {
        #[allow(unused_mut)] // used for plugins
        let mut return_early = false;

        for_hooks!(
            for hook[EPreSigtrap] in self {
                trace!("process hook {}", hook.name());
                self.hook_feedback_loop(hook.name(), |f| {
                    match hook.inner_mut().pre_handle_sigtrap(f, &siginfo, &sig) {
                        Ok((status, ret_e)) => {
                            return_early |= ret_e;
                            Ok(status)
                        },
                        Err(e) => Err(e)
                    }
                })?;
            }
        );

        if return_early {
            return Ok(());
        }

        info!("debugee received {}: {}", sig.as_str(), siginfo.si_code);

        match siginfo.si_code {
            SI_KERNEL => trace!("SI_KERNEL"), // we don't know what do do?
            TRAP_BRKPT => {
                trace!("TRAP_BRKPT");
            }
            TRAP_TRACE => trace!("TRAP_TRACE"), // single stepping
            _ => warn!("Strange SIGTRAP code: {}", siginfo.si_code),
        }

        Ok(())
    }

    /// Handles important signals from the debuggee
    ///
    /// # Parameters
    ///
    /// * `sig` - The signal
    /// * `siginfo` - The signal information
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the signal was handled successfully
    /// * `Err(DebuggerError)` - If there was an error handling the signal
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    pub fn handle_important_signal(
        &mut self,
        sig: Signal,
        siginfo: nix::libc::siginfo_t,
    ) -> Result<()> {
        info!("debugee received {}: {}", sig.as_str(), siginfo.si_code);
        self.last_signal = Some(sig);
        Ok(())
    }

    /// Handles other signals from the debuggee
    ///
    /// # Parameters
    ///
    /// * `sig` - The signal
    /// * `siginfo` - The signal information
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the signal was handled successfully
    /// * `Err(DebuggerError)` - If there was an error handling the signal
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    pub fn handle_other_signal(
        &mut self,
        sig: Signal,
        siginfo: nix::libc::siginfo_t,
    ) -> Result<()> {
        trace!("handle other");
        info!("debugee received {}: {}", sig.as_str(), siginfo.si_code);
        self.last_signal = Some(sig);
        Ok(())
    }

    /// Logs information about the debugger state
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the information was logged successfully
    /// * `Err(DebuggerError)` - If there was an error
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    fn infos(&self) -> std::result::Result<Feedback, DebuggerError> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        info!("Breakpoints:\n{:#?}", dbge.breakpoints);
        Ok(Feedback::Ok)
    }

    /// Steps into a function call
    ///
    /// This function steps through instructions until a call instruction is found,
    /// then steps into that function.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the step-into was successful
    /// * `Err(DebuggerError)` - If there was an error during step-into
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - No call instruction is found
    /// - Step operations fail
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Step into the next function call
    /// debugger.step_into().unwrap();
    ///
    /// # }}
    /// ```
    #[allow(clippy::missing_panics_doc)] // this function cannot panic
    pub fn step_into(&mut self) -> Result<Feedback> {
        if self.debuggee.is_none() {
            return Err(DebuggerError::NoDebugee);
        }
        self.go_back_step_over_bp()?;

        loop {
            let rip: Addr = (self.get_reg(Register::rip)?).into();
            let disassembly: Disassembly =
                // unwrap is safe because we check earlier, needed because we need the mutable
                // reference
                self.debuggee.as_ref().unwrap().disassemble(rip, 8, true)?;
            let next_instruction = &disassembly.inner()[0];
            let operator = next_instruction.2[0].clone();

            if operator.1 != FormatterTextKind::Mnemonic {
                error!("could not read operator from disassembly");
            }
            // PERF: this is very inefficient :/ maybe remove the autostepper or work with continue
            // somehow
            if operator.0.trim() == "call" {
                self.single_step()?;
                break;
            }
            self.single_step()?;
        }

        Ok(Feedback::Ok)
    }

    /// Steps over a function call
    ///
    /// This function combines [`Self::step_into`] and [`Self::step_out`] to step over a function call.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the step-over was successful
    /// * `Err(DebuggerError)` - If there was an error during step-over
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Step operations fail
    pub fn step_over(&mut self) -> Result<Feedback> {
        self.go_back_step_over_bp()?;

        self.step_into()?;
        self.step_out()
    }

    /// Gets a backtrace of the current call stack
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Backtrace)` - The backtrace
    /// * `Err(DebuggerError)` - If there was an error generating the backtrace
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Stack unwinding fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Get a backtrace of the call stack
    /// if let Ok(Feedback::Backtrace(bt)) = debugger.backtrace() {
    ///     for (i, frame) in bt.frames.iter().enumerate() {
    ///         println!("#{} {} at {:?}", i, frame.name.as_deref().unwrap_or("??"), frame.addr);
    ///     }
    /// }
    ///
    /// # }}
    /// ```
    pub fn backtrace(&self) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let backtrace = unwind::unwind(dbge.pid)?;

        Ok(Feedback::Backtrace(backtrace))
    }

    /// Gets the current instruction pointer address
    ///
    /// # Returns
    ///
    /// * `Ok(Addr)` - The current address
    /// * `Err(DebuggerError)` - If there was an error getting the address
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - [Register] read fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Get the current instruction pointer
    /// let curr_addr = debugger.get_current_addr().unwrap();
    /// println!("Current IP: {}", curr_addr);
    ///
    /// # }}
    /// ```
    pub fn get_current_addr(&self) -> Result<Addr> {
        Ok((self.get_reg(Register::rip)?).into())
    }

    /// Prepares for variable access by gathering necessary context
    ///
    /// # Parameters
    ///
    /// * `expression` - The variable expression to access
    ///
    /// # Returns
    ///
    /// * `Ok((OwnedSymbol, OwnedSymbol, FrameInfo))` - Function, variable symbols and frame info
    /// * `Err(DebuggerError)` - If preparation failed
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The current location is not in a function
    /// - The variable is not found
    /// - Frame information cannot be constructed
    #[allow(clippy::missing_panics_doc)] // this function cant panic
    pub fn prepare_variable_access(
        &self,
        expression: &VariableExpression,
    ) -> Result<(OwnedSymbol, OwnedSymbol, FrameInfo)> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        let rip: Addr = self.get_current_addr()?;

        // Get current function
        let current_function = match dbge.get_function_by_addr(rip)? {
            Some(f) if f.frame_base().is_some() => f,
            Some(_) => {
                return Err(DebuggerError::AttributeDoesNotExist(
                    gimli::DW_AT_frame_base,
                ))
            }
            None => return Err(DebuggerError::NotInFunction),
        };

        // Find variable
        let locals = dbge.get_local_variables(rip)?;
        let vars = dbge.filter_expressions(&locals, expression)?;
        let var = match vars.len() {
            0 => {
                return Err(DebuggerError::VarExprReturnedNothing(
                    expression.to_string(),
                ))
            }
            1 => vars[0].clone(),
            _ => return Err(DebuggerError::AmbiguousVarExpr(expression.to_string())),
        };

        // Build frame info
        let mut frame_info = FrameInfo::new(
            None,
            Some(Into::<Addr>::into(self.get_reg(Register::rbp)?) + 16usize),
        );

        let frame_base = dbge.parse_location(
            current_function.frame_base().unwrap(), // safe: we check above if this is some
            &frame_info,
            current_function.encoding(),
        )?;

        let frame_base: Addr = match frame_base {
            gimli::Location::Address { address } => address.into(),
            other => unimplemented!(
                "frame base DWARF location was not an address as expected: is {other:?}"
            ),
        };

        frame_info.frame_base = Some(frame_base);

        Ok((current_function, var, frame_info))
    }

    /// Reads the value of a variable
    ///
    /// # Parameters
    ///
    /// * `expression` - The variable name to read
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Variable)` - The variable value
    /// * `Err(DebuggerError)` - If the variable could not be read
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The variable is not found
    /// - The variable cannot be accessed
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Read the value of a variable named "count"
    /// if let Ok(Feedback::Variable(value)) = debugger.read_variable(&"count".to_string()) {
    ///     println!("count = {:?}", value);
    /// }
    ///
    /// # }}
    /// ```
    pub fn read_variable(&self, expression: &VariableExpression) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let (_, symbol, frame_info) = self.prepare_variable_access(expression)?;

        let val = dbge.var_read(&symbol, &frame_info)?;

        Ok(Feedback::Variable(val))
    }

    /// Writes a value to a variable
    ///
    /// # Parameters
    ///
    /// * `expression` - The variable name to write to
    /// * `value` - The value to write
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the write was successful
    /// * `Err(DebuggerError)` - If the variable could not be written
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The variable is not found
    /// - The variable cannot be accessed
    /// - The value is incompatible with the variable type
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Set the value of a variable named "count" to 42
    /// debugger.write_variable(&"count".to_string(), 42).unwrap();
    ///
    /// # }}
    /// ```
    pub fn write_variable(
        &self,
        expression: &VariableExpression,
        value: impl Into<VariableValue>,
    ) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let (_, var, frame_info) = self.prepare_variable_access(expression)?;

        dbge.var_write(&var, &frame_info, &value.into())?;

        Ok(Feedback::Ok)
    }

    /// Reads a single [Word] from memory at the specified address
    ///
    /// # Parameters
    ///
    /// * `addr` - The address to read from
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Word)` - The value read from memory
    /// * `Err(DebuggerError)` - If the memory could not be read
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The memory address is invalid
    /// - The memory cannot be accessed
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Read a word from memory at address 0x1000
    /// if let Ok(Feedback::Word(value)) = debugger.read_mem(Addr::from(0x1000usize)) {
    ///     println!("Memory at 0x1000: {:#x}", value);
    /// }
    ///
    /// # }}
    /// ```
    pub fn read_mem(&self, addr: Addr) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let w = mem_read_word(dbge.pid, addr)?;

        Ok(Feedback::Word(w))
    }

    /// Writes a [Word] to memory at the specified address
    ///
    /// # Parameters
    ///
    /// * `addr` - The address to write to
    /// * `value` - The value to write
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the write was successful
    /// * `Err(DebuggerError)` - If the memory could not be written
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The memory address is invalid
    /// - The memory cannot be accessed
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Write the value 0x42 to memory at address 0x1000
    /// debugger.write_mem(Addr::from(0x1000usize), 0x42).unwrap();
    ///
    /// # }}
    /// ```
    pub fn write_mem(&self, addr: Addr, value: Word) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        mem_write_word(dbge.pid, addr, value)?;

        Ok(Feedback::Ok)
    }

    /// Gets the value of a register
    ///
    /// # Parameters
    ///
    /// * `r` - The register to get
    ///
    /// # Returns
    ///
    /// * `Ok(u64)` - The register value
    /// * `Err(DebuggerError)` - If the register could not be read
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Register access fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::Register;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Get the value of the rax register
    /// let rax = debugger.get_reg(Register::rax).unwrap();
    /// println!("RAX: {:#x}", rax);
    ///
    /// # }}
    /// ```
    pub fn get_reg(&self, r: Register) -> Result<u64> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        crate::get_reg(dbge.pid, r)
    }

    /// Sets the value of a register
    ///
    /// # Parameters
    ///
    /// * `r` - The register to set
    /// * `v` - The value to set
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the register was set successfully
    /// * `Err(DebuggerError)` - If the register could not be set
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Register access fails
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::Register;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Set the value of the rax register to 0x42
    /// debugger.set_reg(Register::rax, 0x42).unwrap();
    ///
    /// # }}
    /// ```
    pub fn set_reg(&self, r: Register, v: u64) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;
        crate::set_reg(dbge.pid, r, v)?;
        Ok(Feedback::Ok)
    }

    /// Gets the current stack of the debugged process
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Stack)` - The stack
    /// * `Err(DebuggerError)` - If the stack could not be retrieved
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - Stack memory cannot be accessed
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Get the current stack
    /// if let Ok(Feedback::Stack(stack)) = debugger.get_stack() {
    ///     println!("Stack: {}", stack);
    /// }
    ///
    /// # }}
    /// ```
    pub fn get_stack(&self) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let stack = dbge.get_stack()?;
        Ok(Feedback::Stack(stack))
    }

    /// Gets the process memory map
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::ProcessMap)` - The process memory map
    /// * `Err(DebuggerError)` - If the memory map could not be retrieved
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running
    /// - The memory map cannot be accessed
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let debugger = Debugger::build(ui).unwrap();
    /// # // Assume debuggee is already running
    /// #
    /// // Get the process memory map
    /// if let Ok(Feedback::ProcessMap(map)) = debugger.get_process_map() {
    ///     println!("{:?}", map);
    /// }
    ///
    /// # }}
    /// ```
    pub fn get_process_map(&self) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let pm = dbge.get_process_map()?;

        Ok(Feedback::ProcessMap(pm))
    }

    /// Gets a [`Breakpoint`] at the specified address
    ///
    /// This method retrieves a [`Breakpoint`] object at the given address, if one exists.
    /// It's primarily used by plugins to determine whether a [`Breakpoint`] exists at a
    /// specific location.
    ///
    /// # Parameters
    ///
    /// * `addr` - The memory [`Addr`] to check for a [`Breakpoint`]
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Breakpoint)` - Feedback containing either `Some(Breakpoint)` if a
    ///   [`Breakpoint`] exists at the address, or [`None`] if there is no [`Breakpoint`]
    /// * `Err(DebuggerError)` - If there was an error accessing the debuggee
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The debuggee is not running (`DebuggerError::NoDebugee`)
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use coreminer::addr::Addr;
    /// # use coreminer::feedback::Feedback;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// # // Set a breakpoint first
    /// # debugger.set_bp(Addr::from(0x1000usize)).unwrap();
    ///
    /// // Check if a breakpoint exists at address 0x1000
    /// if let Ok(Feedback::Breakpoint(maybe_bp)) = debugger.get_bp(Addr::from(0x1000usize)) {
    ///     match maybe_bp {
    ///         Some(bp) => println!("Found breakpoint at address 0x1000"),
    ///         None => println!("No breakpoint at address 0x1000"),
    ///     }
    /// }
    /// # }}
    /// ```
    pub fn get_bp(&self, addr: Addr) -> Result<Feedback> {
        let dbge = self.debuggee.as_ref().ok_or(DebuggerError::NoDebugee)?;

        let bp = dbge.breakpoints.get(&addr);

        Ok(Feedback::Breakpoint(bp.cloned()))
    }

    /// Sets the last signal received from the [`Debuggee`]
    ///
    /// This method allows the [`Debugger`] or a plugin to set or modify the `last_signal`
    /// that will be delivered to the [`Debuggee`].
    ///
    /// The set signal will be used the next time the debugger continues execution with
    /// [`Self::cont()`] or one of the stepping methods.
    ///
    /// # Parameters
    ///
    /// * `sig` - The signal number to set as the last signal
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the signal was successfully set
    /// * `Err(DebuggerError)` - If the signal number could not be converted to a valid signal
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The signal number cannot be converted to a valid [`Signal`] type
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use nix::sys::signal::Signal;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    ///
    /// // Set SIGTRAP as the last signal
    /// debugger.set_last_signal(Signal::SIGTRAP as i32).unwrap();
    ///
    /// // When continuing execution, this signal will be passed to the debuggee
    /// debugger.cont().unwrap();
    /// # }}
    /// ```
    pub fn set_last_signal(&mut self, sig: i32) -> Result<Feedback> {
        let sig = Signal::try_from(sig)?;

        info!("set last signal to {sig}");
        self.last_signal = Some(sig);

        Ok(Feedback::Ok)
    }

    /// Runs a program for debugging
    ///
    /// This function loads an executable, parses its debug information, and
    /// launches it under debugger control.
    ///
    /// # Parameters
    ///
    /// * `executable_path` - Path to the executable
    /// * `arguments` - Command-line arguments for the executable
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the program was launched successfully
    /// * `Err(DebuggerError)` - If the program could not be launched
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - A debuggee is already running
    /// - The executable does not exist
    /// - The executable is not a valid file
    /// - Debug information cannot be parsed
    /// - The process cannot be forked
    ///
    /// # Examples
    ///
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::ui::cli::CliUi;
    /// # use std::path::Path;
    /// # use std::ffi::CString;
    /// #
    /// # let ui = CliUi::build(None).unwrap();
    /// # let mut debugger = Debugger::build(ui).unwrap();
    /// #
    /// // Run a program with arguments
    /// let program = Path::new("./target/debug/my_program");
    /// let args = vec![
    ///     CString::new("my_program").unwrap(),
    ///     CString::new("--arg1").unwrap(),
    ///     CString::new("value1").unwrap()
    /// ];
    ///
    /// debugger.run(program, &args).unwrap();
    ///
    /// # }}
    /// ```
    pub fn run(
        &mut self,
        executable_path: impl AsRef<Path>,
        arguments: &[CString],
    ) -> Result<Feedback> {
        if self.debuggee.is_some() {
            return Err(DebuggerError::AlreadyRunning);
        }

        debug!(
            "exe to run are: {}",
            executable_path.as_ref().to_string_lossy()
        );
        debug!("arguments to run are: {arguments:?}");

        // NOTE: the lifetimes of the raw object data have given us many problems. It would be
        // possible to read the object data out in the main function and passing it to the
        // constructor of Debugger, but that would mean that we cannot debug a different program in
        // the same session.
        let exe: &Path = executable_path.as_ref();
        let exe: PathBuf = which(exe).unwrap_or(exe.into());
        info!("using executable path '{}'", exe.to_string_lossy());

        // First, read the file data
        self.stored_obj_data_raw = std::fs::read(&exe)?;

        // Create a new scope to handle the borrow checker
        {
            // Create a reference to the raw data that matches the 'executable lifetime
            let raw_data: &'executable [u8] = unsafe {
                std::mem::transmute::<&[u8], &'executable [u8]>(&self.stored_obj_data_raw)
            };

            // Parse the object file
            let obj_data = object::File::parse(raw_data)?;
            self.stored_obj_data = Some(obj_data);
        }

        // Now launch the debuggee
        self.launch_debuggee(&exe, arguments)?;

        Ok(Feedback::Ok)
    }

    /// Runs a feedback loop for plugin hooks
    ///
    /// This function enables plugin hooks to interact with the debugger through a feedback loop.
    /// The hook can process feedback from the debugger and provide new status commands to be executed,
    /// creating a continuous interaction until the hook signals completion with [`Status::PluginContinue`].
    ///
    /// This method is primarily used together with [`for_hooks`].
    ///
    /// # Parameters
    ///
    /// * `hook` - The plugin hook that's being executed
    /// * `f` - A closure that processes feedback and returns new status commands
    ///
    /// # Type Parameters
    ///
    /// * `F` - The type of the closure that processes feedback
    /// * `E` - The extension point type for the hook
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the feedback loop completed successfully
    /// * `Err(DebuggerError)` - If an error occurred during the feedback loop
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The hook returns an invalid status
    /// - `process_status` fails when executing a command
    ///
    /// # Examples
    ///
    /// ```no_run
    /// # use steckrs::hook::ExtensionPoint;
    /// # use coreminer::feedback::{Feedback, Status};
    /// # use coreminer::plugins::extension_points::EPreSignalHandler;
    /// # use coreminer::for_hooks;
    /// # use coreminer::ui::DebuggerUI;
    /// # use coreminer::debugger::Debugger;
    /// # use coreminer::addr::Addr;
    /// # fn helper<UI: DebuggerUI>(debugger: &mut Debugger<UI>) {
    ///
    /// // Inside a method that has access to hooks
    /// for_hooks!(
    ///     for hook[EPreSignalHandler] in debugger {
    ///         debugger.hook_feedback_loop(hook.name(), |feedback| {
    ///             // Process the feedback and return a new status
    ///             println!("Received feedback: {}", feedback);
    ///
    ///             if let Feedback::Word(w) = feedback {
    ///                 println!("got word {w}");
    ///                 Ok(Status::PluginContinue)
    ///             } else {
    ///                 Ok(Status::ReadMem(Addr::from(0xdeadbeef_usize)))
    ///             }
    ///         }).unwrap();
    ///     }
    /// );
    /// # }
    /// ```
    ///
    /// In this example, the hook processes feedback and decides whether to continue or
    /// request more information from the debugger before completing.
    #[cfg(feature = "plugins")]
    pub fn hook_feedback_loop<F>(&mut self, hook_name: &str, mut f: F) -> Result<()>
    where
        F: FnMut(&Feedback) -> Result<Status>,
    {
        let mut feedback = Feedback::Ok;
        let mut status;
        let mut guard = 0;
        loop {
            if guard > 10 {
                return Err(DebuggerError::TooManyPluginIterations);
            }
            guard += 1;
            status = match f(&feedback) {
                Ok(s) => s,
                Err(e) => {
                    error!("Error in Hook '{}': {e}", hook_name);
                    break;
                }
            };
            if status == Status::PluginContinue {
                break;
            }
            feedback = self.process_status(&status)?;
        }

        Ok(())
    }

    // NOTE: this is used in the for_hooks macro
    //
    /// Get a reference to the [`PluginManager`] of this [`Debugger`]
    #[cfg(feature = "plugins")]
    pub fn plugins(&self) -> Arc<Mutex<PluginManager>> {
        self.plugins.clone()
    }

    /// Enables or disables a plugin by its ID
    ///
    /// This method modifies the enabled status of a plugin in the plugin manager.
    /// It acquires a lock on the plugin manager, then calls the appropriate method
    /// based on the requested status.
    ///
    /// # Parameters
    ///
    /// * `id` - The unique identifier of the plugin to modify
    /// * `status` - `true` to enable the plugin, `false` to disable it
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::Ok)` - If the operation was successful
    /// * `Err(DebuggerError)` - If there was an error enabling or disabling the plugin
    ///
    /// # Errors
    ///
    /// This function can fail if:
    /// - The plugin with the specified ID is not found
    /// - There was an error in the plugin manager operation
    ///
    /// # Panics
    ///
    /// This method will panic if it cannot acquire a lock on the plugin manager.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use coreminer::debugger::Debugger;
    /// use coreminer::ui::cli::CliUi;
    /// use steckrs::PluginIDOwned;
    ///
    /// # fn example() -> coreminer::errors::Result<()> {
    /// let ui = CliUi::build(None)?;
    /// let mut debugger = Debugger::build(ui)?;
    ///
    /// // Enable a plugin
    /// let plugin_id = PluginIDOwned::from("hello_world");
    /// debugger.plugin_set_enable(&plugin_id, true)?;
    ///
    /// // Later, disable the plugin
    /// debugger.plugin_set_enable(&plugin_id, false)?;
    /// # Ok(())
    /// # }
    /// ```
    #[cfg(feature = "plugins")]
    pub fn plugin_set_enable(&mut self, id: &PluginIDOwned, status: bool) -> Result<Feedback> {
        let mut plugins = self.plugins.lock().expect("could not lock plugin_manager");
        if status {
            plugins.enable_plugin(id.clone().into())?;
            info!("enabled plugin {id}");
        } else {
            plugins.disable_plugin(id.clone().into())?;
            info!("disabled plugin {id}");
        }
        Ok(Feedback::Ok)
    }

    /// Gets the enabled status of a plugin by its ID
    ///
    /// This method retrieves the current enabled status of a plugin from the plugin manager.
    /// It acquires a lock on the plugin manager, then checks if the plugin is enabled.
    ///
    /// # Parameters
    ///
    /// * `id` - The unique identifier of the plugin to check
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::PluginStatus(Some(bool)))` - The plugin's enabled status (`true` if enabled, `false` if disabled)
    /// * `Ok(Feedback::PluginStatus(None))` - If the plugin was not found
    /// * `Err(DebuggerError)` - If there was an error retrieving the plugin status
    ///
    /// # Errors
    ///
    /// This method cannot fail.
    ///
    /// # Panics
    ///
    /// This method will panic if it cannot acquire a lock on the plugin manager.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// use coreminer::debugger::Debugger;
    /// use coreminer::ui::cli::CliUi;
    /// use coreminer::feedback::Feedback;
    /// use steckrs::PluginIDOwned;
    ///
    /// # fn example() -> coreminer::errors::Result<()> {
    /// let ui = CliUi::build(None)?;
    /// let debugger = Debugger::build(ui)?;
    ///
    /// // Check a plugin's status
    /// let plugin_id = PluginIDOwned::from("hello_world");
    /// if let Ok(Feedback::PluginStatus(Some(is_enabled))) = debugger.plugin_get_status(&plugin_id) {
    ///     println!("Plugin is {}", if is_enabled { "enabled" } else { "disabled" });
    /// } else {
    ///     println!("Plugin not found");
    /// }
    /// # Ok(())
    /// # }
    /// # }}
    /// ```
    #[cfg(feature = "plugins")]
    pub fn plugin_get_status(&self, id: &PluginIDOwned) -> Result<Feedback> {
        let status: Option<bool> = self
            .plugins
            .lock()
            .expect("could not lock plugin_manager")
            .plugin_is_enabled(id.clone().into());

        Ok(Feedback::PluginStatus(status))
    }

    /// Lists all loaded plugins with their enabled status
    ///
    /// This method provides a list of all plugins currently loaded in the plugin manager,
    /// along with their enabled status. This is useful for UI components that need to
    /// display and manage plugins.
    ///
    /// # Returns
    ///
    /// * `Ok(Feedback::PluginList)` - A list of tuples containing plugin IDs and their enabled status
    /// * `Err(DebuggerError)` - If there was an error accessing the plugin manager
    ///
    /// # Errors
    ///
    /// This method cannot fail by design but returns a `Result` for consistency with other methods.
    ///
    /// # Panics
    ///
    /// This method will panic if it cannot acquire a lock on the plugin manager.
    ///
    /// # Examples
    ///
    /// ```
    /// #[cfg(feature = "cli")]
    /// # mod featguard { fn _do_thing() {
    /// use coreminer::debugger::Debugger;
    /// use coreminer::ui::cli::CliUi;
    /// use coreminer::feedback::Feedback;
    ///
    /// # fn example() -> coreminer::errors::Result<()> {
    /// let ui = CliUi::build(None)?;
    /// let debugger = Debugger::build(ui)?;
    ///
    /// // Get a list of all loaded plugins
    /// if let Ok(Feedback::PluginList(plugins)) = debugger.list_plugins() {
    ///     println!("Loaded plugins:");
    ///     for (plugin_id, is_enabled) in plugins {
    ///         println!("  {} ({})", plugin_id, if is_enabled { "enabled" } else { "disabled" });
    ///     }
    /// }
    /// # Ok(())
    /// # }
    /// # }}
    /// ```
    #[cfg(feature = "plugins")]
    pub fn list_plugins(&self) -> Result<Feedback> {
        Ok(Feedback::PluginList(
            self.plugins
                .lock()
                .expect("could not lock plugin_manager")
                .plugins()
                .iter()
                .map(|plugin| (plugin.id().into(), plugin.is_enabled()))
                .collect(),
        ))
    }

    /// Take the `last_signal` field of the debugger, leaving `None` in it's place
    fn take_last_status(&mut self) -> Option<Signal> {
        self.last_signal.take()
    }
}