dbg-cli 0.3.3

A universal debugger CLI that lets AI agents observe runtime state instead of guessing from source code
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
//! Cachegrind/Xdebug profile parser and interactive REPL.
//!
//! Parses Xdebug profiler output (cachegrind format) into structured
//! function records, then provides a command-line interface for querying them.

use std::collections::HashMap;
use std::io::{self, BufRead, Write};

/// A call from one function to another.
#[derive(Debug, Clone)]
pub struct CallRecord {
    /// Callee function name.
    pub callee: String,
    /// Number of times called.
    pub call_count: u64,
    /// Inclusive time of the call.
    pub time: u64,
    /// Inclusive memory of the call.
    pub memory: i64,
}

/// A profiled PHP function.
#[derive(Debug, Clone)]
pub struct PhpFunction {
    /// Function name, e.g. "Matrix->multiply" or "php::array_fill"
    pub name: String,
    /// Source file path.
    pub file: String,
    /// Self (exclusive) time in 10ns units.
    pub self_time: u64,
    /// Self (exclusive) memory in bytes.
    pub self_memory: i64,
    /// Inclusive time (self + callees).
    pub inclusive_time: u64,
    /// Inclusive memory (self + callees).
    pub inclusive_memory: i64,
    /// Number of times this function was invoked (number of cost blocks).
    pub call_count: u64,
    /// Functions called by this one.
    pub calls: Vec<CallRecord>,
    /// Extra event counters beyond the first two (indexed by position in
    /// the `events:` header after skipping the first two columns). Used
    /// to surface native callgrind events like Bcm (branch mispredictions),
    /// Bi (indirect branches), Bim, etc.
    pub extra_self: Vec<u64>,
}

/// Parsed index of all functions in a cachegrind profile.
pub struct ProfileIndex {
    pub functions: Vec<PhpFunction>,
    /// Total program time.
    pub total_time: u64,
    /// Total program memory.
    pub total_memory: i64,
    /// Profiled script.
    pub command: String,
    /// Event column names from the `events:` header (e.g. ["Ir", "Bc", "Bcm", "Bi", "Bim"]).
    /// Empty for PHP/Xdebug profiles which don't emit `events:`.
    pub event_names: Vec<String>,
    /// Total counters for each event column, parallel to `event_names`.
    /// Populated from the `summary:` line.
    pub event_totals: Vec<u64>,
    /// Focus filter (function name substring).
    focus: Option<String>,
    /// Ignore filter (function name substring).
    ignore: Option<String>,
}

impl ProfileIndex {
    /// Parse cachegrind format text into a profile index.
    pub fn parse(text: &str) -> Self {
        // ID → name maps
        let mut file_names: HashMap<u32, String> = HashMap::new();
        let mut fn_names: HashMap<u32, String> = HashMap::new();

        // Aggregated data per function ID
        let mut fn_self_time: HashMap<u32, u64> = HashMap::new();
        let mut fn_self_memory: HashMap<u32, i64> = HashMap::new();
        let mut fn_call_count: HashMap<u32, u64> = HashMap::new();
        let mut fn_file: HashMap<u32, u32> = HashMap::new();
        let mut fn_calls: HashMap<u32, Vec<CallRecord>> = HashMap::new();
        // Extra per-function counters beyond time+memory (columns 3+)
        let mut fn_extra_self: HashMap<u32, Vec<u64>> = HashMap::new();

        let mut current_fl: u32 = 0;
        let mut current_fn: u32 = 0;
        let mut _current_cfl: u32 = 0;
        let mut current_cfn: u32 = 0;
        let mut pending_call_count: u64 = 0;
        let mut total_time: u64 = 0;
        let mut total_memory: i64 = 0;
        let mut command = String::new();
        // Event column names from `events:` header
        let mut event_names: Vec<String> = Vec::new();
        // Total counters from `summary:` line
        let mut event_totals: Vec<u64> = Vec::new();

        // Registry for bare-name callgrind format (e.g. stackprof output)
        let mut bare_name_ids: HashMap<String, u32> = HashMap::new();
        let mut next_bare_id: u32 = 500_000;

        for line in text.lines() {
            let line = line.trim();
            if line.is_empty() {
                continue;
            }

            // Header fields
            if let Some(rest) = line.strip_prefix("cmd: ") {
                command = rest.to_string();
                continue;
            }
            if line.starts_with("version:")
                || line.starts_with("creator:")
                || line.starts_with("part:")
                || line.starts_with("positions:")
            {
                continue;
            }
            // `events:` — capture column names for native callgrind profiles
            // (e.g. "Ir Bc Bcm Bi Bim"). PHP/Xdebug profiles use longer names
            // like "Time_(10ns) Memory_(bytes)" and may also emit this line.
            if let Some(rest) = line.strip_prefix("events:") {
                event_names = rest
                    .split_whitespace()
                    .map(str::to_string)
                    .collect();
                continue;
            }

            // Summary line — capture ALL event totals (not just first two)
            if let Some(rest) = line.strip_prefix("summary: ") {
                let parts: Vec<&str> = rest.split_whitespace().collect();
                if let Some(t) = parts.first() {
                    total_time = t.parse().unwrap_or(0);
                }
                if let Some(m) = parts.get(1) {
                    total_memory = m.parse().unwrap_or(0);
                }
                // Capture all columns for later display
                event_totals = parts
                    .iter()
                    .filter_map(|s| s.parse::<u64>().ok())
                    .collect();
                continue;
            }

            // fl=(id) [name] or fl=bare_name
            if let Some(rest) = line.strip_prefix("fl=") {
                if let Some((id_str, name)) = parse_id_assignment(rest, &mut bare_name_ids, &mut next_bare_id) {
                    current_fl = id_str;
                    if !name.is_empty() {
                        file_names.insert(id_str, name.to_string());
                    }
                }
                continue;
            }

            // fn=(id) [name] or fn=bare_name
            if let Some(rest) = line.strip_prefix("fn=") {
                if let Some((id_str, name)) = parse_id_assignment(rest, &mut bare_name_ids, &mut next_bare_id) {
                    current_fn = id_str;
                    if !name.is_empty() {
                        fn_names.insert(id_str, name.to_string());
                    }
                    // Track file association
                    fn_file.entry(current_fn).or_insert(current_fl);
                    // Increment call count (each fn= block is one invocation)
                    *fn_call_count.entry(current_fn).or_insert(0) += 1;
                }
                continue;
            }

            // cfl=(id) [name] or cfl=bare_name
            if let Some(rest) = line.strip_prefix("cfl=") {
                if let Some((id_str, name)) = parse_id_assignment(rest, &mut bare_name_ids, &mut next_bare_id) {
                    _current_cfl = id_str;
                    if !name.is_empty() {
                        file_names.insert(id_str, name.to_string());
                    }
                }
                continue;
            }

            // cfn=(id) [name] or cfn=bare_name
            if let Some(rest) = line.strip_prefix("cfn=") {
                if let Some((id_str, name)) = parse_id_assignment(rest, &mut bare_name_ids, &mut next_bare_id) {
                    current_cfn = id_str;
                    if !name.is_empty() {
                        fn_names.insert(id_str, name.to_string());
                    }
                }
                continue;
            }

            // calls=count target_position ...
            if let Some(rest) = line.strip_prefix("calls=") {
                let parts: Vec<&str> = rest.split_whitespace().collect();
                if let Some(c) = parts.first() {
                    pending_call_count = c.parse().unwrap_or(0);
                }
                continue;
            }

            // Cost line: line_number time memory [extra...]
            // Native callgrind also uses `+N` (relative offset) and `*` prefixes;
            // we handle the absolute-address form that starts with a digit here.
            // Relative-offset lines (`+N ...`) are handled below.
            let cost_parts_opt: Option<Vec<&str>> = if line
                .chars()
                .next()
                .map_or(false, |c| c.is_ascii_digit())
            {
                let p: Vec<&str> = line.split_whitespace().collect();
                if p.len() >= 2 { Some(p) } else { None }
            } else if line.starts_with('+') || line.starts_with('*') {
                // Relative-offset format: `+N time [extra...]` or `* line time [extra...]`
                let p: Vec<&str> = line.split_whitespace().collect();
                // For `+N` lines, col[0]="+N", col[1..] are counters.
                // For `* line` lines, col[0]="*", col[1]=line, col[2..] are counters.
                // In both cases we treat col[1] as time and col[2] as memory.
                if p.len() >= 2 { Some(p) } else { None }
            } else {
                None
            };
            if let Some(parts) = cost_parts_opt {
                let (time_idx, mem_idx, extra_start): (usize, usize, usize) =
                    if line.starts_with('*') {
                        (2, 3, 4) // * line time memory extra...
                    } else {
                        (1, 2, 3) // N time memory extra...  or  +N time memory extra...
                    };
                let time: u64 = parts.get(time_idx).and_then(|s| s.parse().ok()).unwrap_or(0);
                let memory: i64 = parts.get(mem_idx).and_then(|s| s.parse().ok()).unwrap_or(0);
                // Extra counters (Bcm etc.)
                let n_extra = event_names.len().saturating_sub(2);
                let extra: Vec<u64> = (0..n_extra)
                    .map(|i| {
                        parts
                            .get(extra_start + i)
                            .and_then(|s| s.parse::<u64>().ok())
                            .unwrap_or(0)
                    })
                    .collect();

                if pending_call_count > 0 {
                    // This is a callee cost line — use ID placeholder, resolved after parsing
                    let callee_name = format!("__id_{}__", current_cfn);
                    let calls = fn_calls.entry(current_fn).or_default();
                    // Merge with existing call to same target
                    if let Some(existing) = calls.iter_mut().find(|c| c.callee == callee_name) {
                        existing.call_count += pending_call_count;
                        existing.time += time;
                        existing.memory += memory;
                    } else {
                        calls.push(CallRecord {
                            callee: callee_name,
                            call_count: pending_call_count,
                            time,
                            memory,
                        });
                    }
                    pending_call_count = 0;
                } else {
                    // Self cost line
                    *fn_self_time.entry(current_fn).or_insert(0) += time;
                    *fn_self_memory.entry(current_fn).or_insert(0) += memory;
                    // Accumulate extra counters
                    if !extra.is_empty() {
                        let slot = fn_extra_self.entry(current_fn).or_insert_with(|| vec![0u64; n_extra]);
                        for (i, v) in extra.iter().enumerate() {
                            if i < slot.len() {
                                slot[i] += v;
                            }
                        }
                    }
                }
            }
        }

        // Resolve callee ID placeholders now that all fn= names are known
        for calls in fn_calls.values_mut() {
            for call in calls.iter_mut() {
                if let Some(rest) = call.callee.strip_prefix("__id_") {
                    if let Some(id_str) = rest.strip_suffix("__") {
                        if let Ok(id) = id_str.parse::<u32>() {
                            if let Some(name) = fn_names.get(&id) {
                                call.callee = name.clone();
                            }
                        }
                    }
                }
            }
        }

        // Merge calls that now share the same resolved callee name
        for calls in fn_calls.values_mut() {
            let mut merged: Vec<CallRecord> = Vec::new();
            for call in calls.drain(..) {
                if let Some(existing) = merged.iter_mut().find(|c| c.callee == call.callee) {
                    existing.call_count += call.call_count;
                    existing.time += call.time;
                    existing.memory += call.memory;
                } else {
                    merged.push(call);
                }
            }
            *calls = merged;
        }

        // Build function list
        let mut functions = Vec::new();
        let mut all_fn_ids: Vec<u32> = fn_names.keys().copied().collect();
        all_fn_ids.sort();
        let n_extra = event_names.len().saturating_sub(2);

        for id in all_fn_ids {
            let name = fn_names.get(&id).cloned().unwrap_or_else(|| format!("fn#{}", id));
            let file = fn_file
                .get(&id)
                .and_then(|fid| file_names.get(fid))
                .cloned()
                .unwrap_or_default();
            let self_time = fn_self_time.get(&id).copied().unwrap_or(0);
            let self_memory = fn_self_memory.get(&id).copied().unwrap_or(0);
            let calls = fn_calls.get(&id).cloned().unwrap_or_default();
            let callee_time: u64 = calls.iter().map(|c| c.time).sum();
            let callee_memory: i64 = calls.iter().map(|c| c.memory).sum();
            let extra_self = fn_extra_self
                .get(&id)
                .cloned()
                .unwrap_or_else(|| vec![0u64; n_extra]);

            functions.push(PhpFunction {
                name,
                file,
                self_time,
                self_memory,
                inclusive_time: self_time + callee_time,
                inclusive_memory: self_memory + callee_memory,
                call_count: fn_call_count.get(&id).copied().unwrap_or(1),
                calls,
                extra_self,
            });
        }

        ProfileIndex {
            functions,
            total_time,
            total_memory,
            command,
            event_names,
            event_totals,
            focus: None,
            ignore: None,
        }
    }

    /// Filter functions whose name matches a substring (case-insensitive),
    /// respecting focus/ignore filters.
    fn filter(&self, pattern: &str) -> Vec<&PhpFunction> {
        self.functions
            .iter()
            .filter(|f| {
                if !pattern.is_empty() && pattern != "." {
                    if !f.name.to_lowercase().contains(&pattern.to_lowercase()) {
                        return false;
                    }
                }
                if let Some(ref focus) = self.focus {
                    if !f.name.to_lowercase().contains(&focus.to_lowercase()) {
                        return false;
                    }
                }
                if let Some(ref ignore) = self.ignore {
                    if f.name.to_lowercase().contains(&ignore.to_lowercase()) {
                        return false;
                    }
                }
                true
            })
            .collect()
    }

    fn format_time(t: u64) -> String {
        if t >= 1_000_000 {
            format!("{:.1}ms", t as f64 / 100_000.0)
        } else if t >= 1_000 {
            format!("{:.1}µs", t as f64 / 100.0)
        } else {
            format!("{}0ns", t)
        }
    }

    fn format_memory(m: i64) -> String {
        let abs = m.unsigned_abs();
        let sign = if m < 0 { "-" } else { "" };
        if abs >= 1_048_576 {
            format!("{}{:.1}MB", sign, abs as f64 / 1_048_576.0)
        } else if abs >= 1_024 {
            format!("{}{:.1}KB", sign, abs as f64 / 1_024.0)
        } else {
            format!("{}{}B", sign, abs)
        }
    }

    fn format_pct(&self, time: u64) -> String {
        if self.total_time > 0 {
            format!("{:.1}%", time as f64 / self.total_time as f64 * 100.0)
        } else {
            "-%".to_string()
        }
    }

    /// `hotspots [N] [pattern]` — top N functions by inclusive time.
    pub fn cmd_hotspots(&self, n: usize, pattern: &str) -> String {
        let mut matched = self.filter(pattern);
        matched.sort_by(|a, b| b.inclusive_time.cmp(&a.inclusive_time));
        let mut out = String::new();
        for f in matched.iter().take(n) {
            out.push_str(&format!(
                "{:>7} {:>8}  {:<40} {}x  {}\n",
                self.format_pct(f.inclusive_time),
                Self::format_time(f.inclusive_time),
                f.name,
                f.call_count,
                Self::format_memory(f.inclusive_memory),
            ));
        }
        if out.is_empty() {
            out.push_str("no functions found\n");
        }
        out
    }

    /// `flat [N] [pattern]` — top N functions by self time.
    pub fn cmd_flat(&self, n: usize, pattern: &str) -> String {
        let mut matched = self.filter(pattern);
        matched.sort_by(|a, b| b.self_time.cmp(&a.self_time));
        let mut out = String::new();
        out.push_str(&format!(
            "{:>7} {:>8}  {:<40} {:>8}  {}\n",
            "self%", "self", "function", "calls", "self mem"
        ));
        out.push_str(&format!("{}\n", "-".repeat(80)));
        for f in matched.iter().take(n) {
            out.push_str(&format!(
                "{:>7} {:>8}  {:<40} {:>7}x  {}\n",
                self.format_pct(f.self_time),
                Self::format_time(f.self_time),
                f.name,
                f.call_count,
                Self::format_memory(f.self_memory),
            ));
        }
        if matched.is_empty() {
            out.push_str("no functions found\n");
        }
        out
    }

    /// `calls <pattern>` — what does this function call?
    pub fn cmd_calls(&self, pattern: &str) -> String {
        let matched = self.filter(pattern);
        let mut out = String::new();
        for f in &matched {
            if f.calls.is_empty() {
                out.push_str(&format!("{}: no calls\n", f.name));
            } else {
                out.push_str(&format!("{} ({} callees):\n", f.name, f.calls.len()));
                let mut sorted = f.calls.clone();
                sorted.sort_by(|a, b| b.time.cmp(&a.time));
                for c in &sorted {
                    out.push_str(&format!(
                        "  → {:<40} {}x  {}  {}\n",
                        c.callee,
                        c.call_count,
                        Self::format_time(c.time),
                        Self::format_memory(c.memory),
                    ));
                }
            }
        }
        if out.is_empty() {
            out.push_str("no functions found\n");
        }
        out
    }

    /// `callers <pattern>` — who calls this function?
    pub fn cmd_callers(&self, pattern: &str) -> String {
        let pat_lower = pattern.to_lowercase();
        let mut out = String::new();
        for f in &self.functions {
            let hits: Vec<&CallRecord> = f
                .calls
                .iter()
                .filter(|c| c.callee.to_lowercase().contains(&pat_lower))
                .collect();
            if !hits.is_empty() {
                for c in &hits {
                    out.push_str(&format!(
                        "{:<40} → {:<30} {}x  {}\n",
                        f.name,
                        c.callee,
                        c.call_count,
                        Self::format_time(c.time),
                    ));
                }
            }
        }
        if out.is_empty() {
            out.push_str(&format!("no callers found for '{}'\n", pattern));
        }
        out
    }

    /// `stats [pattern]` — summary statistics.
    pub fn cmd_stats(&self, pattern: &str) -> String {
        let matched = self.filter(pattern);
        if matched.is_empty() {
            return "no functions found\n".into();
        }

        let total_self_time: u64 = matched.iter().map(|f| f.self_time).sum();
        let total_self_mem: i64 = matched.iter().map(|f| f.self_memory).sum();
        let total_incl_time: u64 = matched.iter().map(|f| f.inclusive_time).sum();
        let total_calls: u64 = matched.iter().map(|f| f.call_count).sum();

        let label = if pattern.is_empty() || pattern == "." {
            format!("--- {} ---", self.command)
        } else {
            format!("--- filter: {} ---", pattern)
        };

        let mut out = format!("{}\n", label);
        out.push_str(&format!("Functions:      {}\n", matched.len()));
        out.push_str(&format!("Total calls:    {}\n", total_calls));
        out.push_str(&format!(
            "Self time:      {} ({})\n",
            Self::format_time(total_self_time),
            self.format_pct(total_self_time)
        ));
        out.push_str(&format!(
            "Inclusive time: {}\n",
            Self::format_time(total_incl_time)
        ));
        out.push_str(&format!("Self memory:    {}\n", Self::format_memory(total_self_mem)));
        if self.total_time > 0 {
            out.push_str(&format!(
                "Program total:  {}  {}\n",
                Self::format_time(self.total_time),
                Self::format_memory(self.total_memory)
            ));
        }
        // Surface native callgrind counters (Bcm = branch mispredictions, etc.)
        // when the `events:` header declared more than the standard two columns.
        if self.event_names.len() > 2 && !self.event_totals.is_empty() {
            out.push_str("\nHardware counters (from callgrind):\n");
            for (i, name) in self.event_names.iter().enumerate() {
                if let Some(&total) = self.event_totals.get(i) {
                    let label = match name.as_str() {
                        "Ir"  => "Instr refs",
                        "Bc"  => "Branch cond",
                        "Bcm" => "Branch mispred",
                        "Bi"  => "Indirect br",
                        "Bim" => "Indir br mispred",
                        other => other,
                    };
                    out.push_str(&format!("  {:<20} {:>14}\n", label, total));
                }
            }
            // Compute misprediction rate when both Bc and Bcm are present
            let bcm_idx = self.event_names.iter().position(|n| n == "Bcm");
            let bc_idx  = self.event_names.iter().position(|n| n == "Bc");
            if let (Some(bcm_i), Some(bc_i)) = (bcm_idx, bc_idx) {
                if let (Some(&bcm), Some(&bc)) = (self.event_totals.get(bcm_i), self.event_totals.get(bc_i)) {
                    if bc > 0 {
                        let pct = bcm as f64 / bc as f64 * 100.0;
                        out.push_str(&format!("  {:<20} {:>13.1}%\n", "Branch mispredict%", pct));
                    }
                }
            }
        }
        out
    }

    /// `inspect <pattern>` — detailed view of matching functions.
    pub fn cmd_inspect(&self, pattern: &str) -> String {
        let matched = self.filter(pattern);
        let mut out = String::new();
        for f in &matched {
            out.push_str(&format!("{}  ({})\n", f.name, f.file));
            out.push_str(&format!(
                "  Self:      {:>8}  ({})\n",
                Self::format_time(f.self_time),
                self.format_pct(f.self_time)
            ));
            out.push_str(&format!(
                "  Inclusive: {:>8}  ({})\n",
                Self::format_time(f.inclusive_time),
                self.format_pct(f.inclusive_time)
            ));
            out.push_str(&format!(
                "  Memory:    {:>8} self, {} inclusive\n",
                Self::format_memory(f.self_memory),
                Self::format_memory(f.inclusive_memory)
            ));
            out.push_str(&format!("  Calls:     {}x\n", f.call_count));
            // Surface per-function hardware counters when available
            if self.event_names.len() > 2 && !f.extra_self.is_empty() {
                let has_nonzero = f.extra_self.iter().any(|&v| v > 0);
                if has_nonzero {
                    out.push_str("  HW counters (self):\n");
                    for (i, name) in self.event_names.iter().skip(2).enumerate() {
                        if let Some(&v) = f.extra_self.get(i) {
                            if v > 0 {
                                let label = match name.as_str() {
                                    "Bcm" => "Branch mispred",
                                    "Bc"  => "Branch cond",
                                    "Bi"  => "Indirect br",
                                    "Bim" => "Indir br mispred",
                                    other => other,
                                };
                                out.push_str(&format!("    {:<20} {:>12}\n", label, v));
                            }
                        }
                    }
                }
            }
            if !f.calls.is_empty() {
                out.push_str("  Callees:\n");
                let mut sorted = f.calls.clone();
                sorted.sort_by(|a, b| b.time.cmp(&a.time));
                for c in &sorted {
                    out.push_str(&format!(
                        "    → {:<36} {}x  {}  {}\n",
                        c.callee,
                        c.call_count,
                        Self::format_time(c.time),
                        Self::format_memory(c.memory),
                    ));
                }
            }
            out.push('\n');
        }
        if out.is_empty() {
            out.push_str("no functions found\n");
        }
        out
    }

    /// `memory [N]` — top N functions by self memory allocation.
    pub fn cmd_memory(&self, n: usize, pattern: &str) -> String {
        let mut matched = self.filter(pattern);
        matched.sort_by(|a, b| b.self_memory.cmp(&a.self_memory));
        let mut out = String::new();
        for f in matched.iter().take(n) {
            if f.self_memory == 0 && f.inclusive_memory == 0 {
                continue;
            }
            out.push_str(&format!(
                "{:>8} self  {:>8} incl  {:<40} {}x\n",
                Self::format_memory(f.self_memory),
                Self::format_memory(f.inclusive_memory),
                f.name,
                f.call_count,
            ));
        }
        if out.is_empty() {
            out.push_str("no memory-allocating functions found\n");
        }
        out
    }

    /// `search <pattern>` — find functions matching a pattern.
    pub fn cmd_search(&self, pattern: &str) -> String {
        let pat_lower = pattern.to_lowercase();
        let mut matched: Vec<&PhpFunction> = self
            .functions
            .iter()
            .filter(|f| f.name.to_lowercase().contains(&pat_lower) && f.self_time > 0)
            .collect();
        if matched.is_empty() {
            return format!("no functions matching '{}'\n", pattern);
        }
        matched.sort_by(|a, b| b.inclusive_time.cmp(&a.inclusive_time));
        let mut out = format!("{} matches:\n", matched.len());
        for f in matched.iter().take(30) {
            out.push_str(&format!(
                "  {:>7}  {:<40} {}x\n",
                self.format_pct(f.inclusive_time),
                f.name,
                f.call_count,
            ));
        }
        out
    }

    /// `tree [N]` — call tree from roots, top N branches by time.
    pub fn cmd_tree(&self, n: usize) -> String {
        let filtered: Vec<&PhpFunction> = self.filter("");
        // Build callee map: function name → Vec<(callee PhpFunction ref, time)>
        // Start from functions that are not called by anyone (roots).
        let called_names: std::collections::HashSet<&str> = filtered
            .iter()
            .flat_map(|f| f.calls.iter().map(|c| c.callee.as_str()))
            .collect();

        let mut roots: Vec<&PhpFunction> = filtered
            .iter()
            .filter(|f| !called_names.contains(f.name.as_str()) && f.inclusive_time > 0)
            .copied()
            .collect();
        if roots.is_empty() {
            // No uncalled root found (e.g. focus on mutually-recursive functions).
            // Fall back to functions with the highest inclusive time.
            roots = filtered
                .iter()
                .filter(|f| f.inclusive_time > 0)
                .copied()
                .collect();
        }
        roots.sort_by(|a, b| b.inclusive_time.cmp(&a.inclusive_time));
        roots.truncate(n);

        let fn_map: HashMap<&str, &PhpFunction> = filtered
            .iter()
            .map(|f| (f.name.as_str(), *f))
            .collect();

        let mut out = String::new();
        let mut visited = std::collections::HashSet::new();
        for root in &roots {
            self.tree_recurse(root, 0, &fn_map, &mut out, 5, &mut visited);
            visited.clear();
        }
        if out.is_empty() {
            out.push_str("no call tree found\n");
        }
        out
    }

    fn tree_recurse<'a>(
        &self,
        func: &'a PhpFunction,
        depth: usize,
        fn_map: &HashMap<&str, &'a PhpFunction>,
        out: &mut String,
        max_depth: usize,
        visited: &mut std::collections::HashSet<&'a str>,
    ) {
        if depth > max_depth {
            return;
        }
        let pct = if self.total_time > 0 {
            func.inclusive_time as f64 / self.total_time as f64 * 100.0
        } else {
            0.0
        };
        if pct < 0.5 {
            return;
        }
        let indent = "  ".repeat(depth);
        if !visited.insert(func.name.as_str()) {
            out.push_str(&format!(
                "{}{:>6.1}%  {}  (recursive)\n",
                indent, pct, func.name,
            ));
            return;
        }
        out.push_str(&format!(
            "{}{:>6.1}%  {}  ({}x)\n",
            indent, pct, func.name, func.call_count,
        ));
        let mut sorted = func.calls.clone();
        sorted.sort_by(|a, b| b.time.cmp(&a.time));
        for call in &sorted {
            if let Some(callee) = fn_map.get(call.callee.as_str()) {
                self.tree_recurse(callee, depth + 1, fn_map, out, max_depth, visited);
            }
        }
        visited.remove(func.name.as_str());
    }

    /// `hotpath` — the single most expensive call chain.
    pub fn cmd_hotpath(&self) -> String {
        let filtered: Vec<&PhpFunction> = self.filter("");
        let fn_map: HashMap<&str, &PhpFunction> = filtered
            .iter()
            .map(|f| (f.name.as_str(), *f))
            .collect();

        // Find the root with the most inclusive time (among filtered functions)
        let called_names: std::collections::HashSet<&str> = filtered
            .iter()
            .flat_map(|f| f.calls.iter().map(|c| c.callee.as_str()))
            .collect();

        let root = filtered
            .iter()
            .filter(|f| !called_names.contains(f.name.as_str()) && f.inclusive_time > 0)
            .max_by_key(|f| f.inclusive_time)
            .or_else(|| {
                // No uncalled root found (e.g. focus on mutually-recursive functions).
                // Fall back to the function with the highest inclusive time.
                filtered.iter().filter(|f| f.inclusive_time > 0).max_by_key(|f| f.inclusive_time)
            });

        let Some(root) = root else {
            return "no call data\n".to_string();
        };

        let mut out = format!(
            "hottest path ({}):\n",
            Self::format_time(root.inclusive_time)
        );
        let mut current = *root;
        let mut depth = 0;
        let mut visited = std::collections::HashSet::new();
        loop {
            let indent = "  ".repeat(depth);
            out.push_str(&format!(
                "{}→ {}  ({}, {}x)\n",
                indent,
                current.name,
                Self::format_time(current.self_time),
                current.call_count,
            ));
            if !visited.insert(current.name.as_str()) {
                out.push_str(&format!("{}  (recursive)\n", indent));
                break;
            }
            // Follow the callee with the highest *callee* inclusive
            // time — not the per-call `c.time` the parser recorded.
            // Native callgrind attributes almost no time to pseudo-
            // frames (`(below main)`, stub PLT entries) even though
            // their descendants dominate runtime, so picking by
            // `c.time` stalls the walk one level deep. Picking by
            // `callee.inclusive_time` follows the time wherever it
            // actually lives.
            //
            // Cross-recursion subtlety: when a caller's hottest edge
            // points back at an already-visited frame (A → B → A, or
            // direct self-recursion), let the walk descend to that
            // visited frame so the next iteration triggers the
            // `(recursive)` banner. If a non-visited sibling carries
            // within 50 % of the visited winner's time, prefer it so
            // mutual-recursion (A ↔ B) still shows both legs before
            // terminating on the cycle — without this bias, a
            // dominant self-loop on A would swallow B entirely.
            let max_callee = current
                .calls
                .iter()
                .filter_map(|c| fn_map.get(c.callee.as_str()).copied())
                .max_by_key(|callee| callee.inclusive_time);
            let hottest_callee = match max_callee {
                Some(winner) if visited.contains(winner.name.as_str()) => {
                    let winner_t = winner.inclusive_time;
                    current
                        .calls
                        .iter()
                        .filter_map(|c| fn_map.get(c.callee.as_str()).copied())
                        .filter(|callee| !visited.contains(callee.name.as_str()))
                        .filter(|callee| callee.inclusive_time * 2 >= winner_t)
                        .max_by_key(|callee| callee.inclusive_time)
                        .or(Some(winner))
                }
                other => other,
            };
            if let Some(next) = hottest_callee {
                current = next;
                depth += 1;
            } else {
                break;
            }
        }
        out
    }
}

fn parse_id_assignment<'a>(
    s: &'a str,
    bare_name_ids: &mut HashMap<String, u32>,
    next_bare_id: &mut u32,
) -> Option<(u32, &'a str)> {
    let s = s.trim();
    if s.is_empty() {
        return None;
    }
    // Standard callgrind format: "(123) name" or "(123)"
    if s.starts_with('(') {
        let end_paren = s.find(')')?;
        let id: u32 = s[1..end_paren].parse().ok()?;
        let rest = s[end_paren + 1..].trim();
        return Some((id, rest));
    }
    // Bare name format (e.g. stackprof --callgrind): "Object#fibonacci"
    let name = s;
    let id = *bare_name_ids.entry(name.to_string()).or_insert_with(|| {
        let id = *next_bare_id;
        *next_bare_id += 1;
        id
    });
    Some((id, name))
}

/// Derive the `help` header label from the REPL prompt. The phpprofile
/// REPL is shared by callgrind / xdebug / stackprof backends — each
/// passes its own prompt (`callgrind> `, `xdebug> `, …), so stripping
/// the trailing `> ` gives the right backend name. The old code
/// hardcoded `"php-profile commands:"` for every caller, which made
/// `dbg help` on a callgrind session start with a PHP label.
pub fn help_label(prompt: &str) -> &str {
    let trimmed = prompt.trim_end_matches(char::is_whitespace);
    let trimmed = trimmed.trim_end_matches('>');
    let trimmed = trimmed.trim();
    if trimmed.is_empty() {
        "php-profile"
    } else {
        trimmed
    }
}

/// Parse the tail of a `hotspots`-family REPL command into
/// `(limit, pattern)`. Accepts `N [pat]`, `--top N [pat]`, `-n N [pat]`,
/// or just `[pat]`.
///
/// Before this was factored out, the match arm did
/// `arg1.parse().unwrap_or(default)` which silently swallowed flag
/// spellings: `hotspots --top 3` parsed `--top` as the limit (fell
/// back to default 10) and treated `3` as a pattern filter.
pub fn parse_top_n_args(rest: &str, default: usize) -> (usize, String) {
    let toks: Vec<&str> = rest.split_whitespace().collect();
    if toks.is_empty() {
        return (default, String::new());
    }
    let (n, pat_toks) = match toks[0] {
        "--top" | "-n" | "-N" => {
            if toks.len() >= 2 {
                match toks[1].parse::<usize>() {
                    Ok(v) => (v, &toks[2..]),
                    Err(_) => (default, &toks[1..]),
                }
            } else {
                (default, &toks[1..])
            }
        }
        _ => match toks[0].parse::<usize>() {
            Ok(v) => (v, &toks[1..]),
            Err(_) => (default, &toks[..]),
        },
    };
    (n, pat_toks.join(" "))
}

/// Run the interactive REPL. Reads commands from stdin, writes results to stdout.
pub fn run_repl(cachegrind_path: &str, prompt: &str) -> io::Result<()> {
    let text = std::fs::read_to_string(cachegrind_path)?;
    let mut index = ProfileIndex::parse(&text);

    eprintln!(
        "--- ready: {} functions profiled ---",
        index.functions.len()
    );
    eprintln!("Type: help");

    let stdin = io::stdin();
    let mut stdout = io::stdout();

    loop {
        print!("{prompt}");
        stdout.flush()?;

        let mut line = String::new();
        if stdin.lock().read_line(&mut line)? == 0 {
            break;
        }
        let line = line.trim();
        if line.is_empty() {
            continue;
        }

        let (cmd, rest) = match line.split_once(char::is_whitespace) {
            Some((c, r)) => (c, r.trim()),
            None => (line, ""),
        };
        let parts: Vec<&str> = rest.splitn(2, ' ').collect();
        let arg1 = parts.first().copied().unwrap_or("");
        let arg2 = parts.get(1).copied().unwrap_or("");
        let pat = if arg2.is_empty() { arg1.to_string() } else { format!("{arg1} {arg2}") };

        let result = match cmd {
            "hotspots" => {
                let (n, pattern) = parse_top_n_args(rest, 10);
                index.cmd_hotspots(n, &pattern)
            }
            "flat" => {
                let (n, pattern) = parse_top_n_args(rest, 20);
                index.cmd_flat(n, &pattern)
            }
            "calls" if arg1.is_empty() => "usage: calls <pattern>\n".into(),
            "calls" => index.cmd_calls(&pat),
            "callers" if arg1.is_empty() => "usage: callers <pattern>\n".into(),
            "callers" => index.cmd_callers(&pat),
            "inspect" if arg1.is_empty() => "usage: inspect <pattern>\n".into(),
            "inspect" => index.cmd_inspect(&pat),
            "stats" => index.cmd_stats(arg1),
            "memory" => {
                let (n, pattern) = parse_top_n_args(rest, 10);
                index.cmd_memory(n, &pattern)
            }
            "search" if arg1.is_empty() => "usage: search <pattern>\n".into(),
            "search" => index.cmd_search(&pat),
            "tree" => {
                let (n, _) = parse_top_n_args(rest, 10);
                index.cmd_tree(n)
            }
            "hotpath" => index.cmd_hotpath(),
            "focus" if arg1.is_empty() => "usage: focus <pattern>\n".into(),
            "focus" => {
                index.focus = Some(pat.clone());
                format!("focus set: {}\n", pat)
            }
            "ignore" if arg1.is_empty() => "usage: ignore <pattern>\n".into(),
            "ignore" => {
                index.ignore = Some(pat.clone());
                format!("ignore set: {}\n", pat)
            }
            "reset" => {
                index.focus = None;
                index.ignore = None;
                "filters cleared\n".into()
            }
            "help" => format!(
                "{label} commands:\n  \
                 hotspots [N] [pat]   top N functions by inclusive time (default 10)\n  \
                 flat [N] [pat]       top N functions by self time (default 20)\n  \
                 calls <pattern>      what does this function call?\n  \
                 callers <pattern>    who calls this function?\n  \
                 inspect <pattern>    detailed breakdown of matching functions\n  \
                 stats [pattern]      summary statistics\n  \
                 memory [N] [pat]     top N functions by memory allocation\n  \
                 search <pattern>     find functions matching a pattern\n  \
                 tree [N]             call tree from roots (top N branches)\n  \
                 hotpath              single most expensive call chain\n  \
                 focus <pattern>      filter all commands to matching functions\n  \
                 ignore <pattern>     exclude matching functions from all commands\n  \
                 reset                clear focus/ignore filters\n  \
                 help                 show this help\n  \
                 exit                 quit\n",
                label = help_label(prompt),
            ),
            "exit" | "quit" => break,
            _ => format!(
                "unknown command: {}. Type 'help' for available commands.\n",
                cmd
            ),
        };

        print!("{}", result);
        stdout.flush()?;
    }

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;

    const SAMPLE: &str = include_str!("../tests/fixtures/xdebug_cachegrind_sample.out");

    #[test]
    fn help_label_tracks_prompt_not_hardcoded() {
        // Regression: the help header was hardcoded to
        // `"php-profile commands:"` inside `run_repl`, so a callgrind
        // session typing `help` saw PHP-labelled output. The label
        // must follow whichever prompt the hosting backend supplied.
        assert_eq!(help_label("callgrind> "), "callgrind");
        assert_eq!(help_label("xdebug> "), "xdebug");
        assert_eq!(help_label("stackprof> "), "stackprof");
        assert_eq!(help_label("php-profile> "), "php-profile");
        // Prompts without a trailing `>` still work.
        assert_eq!(help_label("callgrind "), "callgrind");
        // Empty / weird prompts fall back to the historical label.
        assert_eq!(help_label(""), "php-profile");
        assert_eq!(help_label(">"), "php-profile");
    }

    #[test]
    fn parse_top_n_flag_and_positional_and_pattern() {
        // Regression: the hotspots/flat/memory REPL verbs used
        // `arg1.parse().unwrap_or(default)`, so `--top N` fell back
        // to the default while the actual number got treated as a
        // pattern filter. `hotspots --top 3` returned 10 rows.
        assert_eq!(parse_top_n_args("--top 3", 10), (3, String::new()));
        assert_eq!(parse_top_n_args("-n 7 Matrix", 10), (7, "Matrix".into()));
        // Plain positional form still works.
        assert_eq!(parse_top_n_args("5 Matrix", 10), (5, "Matrix".into()));
        assert_eq!(parse_top_n_args("5", 10), (5, String::new()));
        // Empty → default, no pattern.
        assert_eq!(parse_top_n_args("", 10), (10, String::new()));
        // Leading pattern, no number → default, pattern preserved.
        assert_eq!(parse_top_n_args("Matrix", 10), (10, "Matrix".into()));
        // Pattern spanning multiple words survives.
        assert_eq!(
            parse_top_n_args("--top 4 foo bar", 10),
            (4, "foo bar".into())
        );
        // Bogus value after --top → default, token stays in pattern.
        assert_eq!(
            parse_top_n_args("--top nope Matrix", 10),
            (10, "nope Matrix".into())
        );
    }

    #[test]
    fn parse_finds_all_functions() {
        let idx = ProfileIndex::parse(SAMPLE);
        assert_eq!(idx.functions.len(), 11);
    }

    #[test]
    fn parse_command() {
        let idx = ProfileIndex::parse(SAMPLE);
        assert_eq!(idx.command, "/tmp/demo.php");
    }

    #[test]
    fn parse_totals() {
        let idx = ProfileIndex::parse(SAMPLE);
        assert_eq!(idx.total_time, 429023);
        assert_eq!(idx.total_memory, 480384);
    }

    #[test]
    fn parse_self_time() {
        let idx = ProfileIndex::parse(SAMPLE);
        let multiply = idx.functions.iter().find(|f| f.name == "Matrix->multiply").unwrap();
        assert_eq!(multiply.self_time, 175000);
    }

    #[test]
    fn parse_inclusive_time() {
        let idx = ProfileIndex::parse(SAMPLE);
        let multiply = idx.functions.iter().find(|f| f.name == "Matrix->multiply").unwrap();
        // self 175000 + constructor 141 + set 9500
        assert_eq!(multiply.inclusive_time, 175000 + 141 + 9500);
    }

    #[test]
    fn parse_calls() {
        let idx = ProfileIndex::parse(SAMPLE);
        let multiply = idx.functions.iter().find(|f| f.name == "Matrix->multiply").unwrap();
        assert_eq!(multiply.calls.len(), 2);
        let set_call = multiply.calls.iter().find(|c| c.callee == "Matrix->set").unwrap();
        assert_eq!(set_call.call_count, 900);
    }

    #[test]
    fn parse_main_calls_buildrandom() {
        let idx = ProfileIndex::parse(SAMPLE);
        let main = idx.functions.iter().find(|f| f.name == "main").unwrap();
        let br_calls: Vec<&CallRecord> = main.calls.iter().filter(|c| c.callee == "buildRandom").collect();
        assert_eq!(br_calls.len(), 1);
        assert_eq!(br_calls[0].call_count, 2);
    }

    #[test]
    fn cmd_hotspots_returns_sorted() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_hotspots(3, "");
        let lines: Vec<&str> = out.lines().collect();
        // {main} or main should be first (highest inclusive time)
        assert!(lines[0].contains("main") || lines[0].contains("{main}"));
    }

    #[test]
    fn cmd_flat_returns_sorted_by_self() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_flat(3, "");
        // Header + separator + data
        let lines: Vec<&str> = out.lines().collect();
        // First data line (after header+separator) should be multiply (highest self time)
        assert!(lines[2].contains("multiply"));
    }

    #[test]
    fn cmd_calls_shows_callees() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_calls("multiply");
        assert!(out.contains("Matrix->set"));
        assert!(out.contains("900x"));
    }

    #[test]
    fn cmd_callers_shows_callers() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_callers("multiply");
        assert!(out.contains("main"));
    }

    #[test]
    fn cmd_callers_buildrandom() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_callers("buildRandom");
        assert!(out.contains("main"));
        assert!(out.contains("2x"));
    }

    #[test]
    fn cmd_stats_all() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_stats("");
        assert!(out.contains("Functions:      11"));
        assert!(out.contains("/tmp/demo.php"));
    }

    #[test]
    fn cmd_stats_filtered() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_stats("Matrix");
        assert!(out.contains("filter: Matrix"));
        // Matrix->__construct, set, get, multiply, trace = 5
        assert!(out.contains("Functions:      5"));
    }

    #[test]
    fn cmd_inspect_shows_detail() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_inspect("multiply");
        assert!(out.contains("Matrix->multiply"));
        assert!(out.contains("Self:"));
        assert!(out.contains("Inclusive:"));
        assert!(out.contains("Callees:"));
        assert!(out.contains("Matrix->set"));
    }

    #[test]
    fn cmd_memory_shows_allocations() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_memory(5, "");
        assert!(out.contains("Matrix->set"));
    }

    #[test]
    fn format_time_units() {
        assert_eq!(ProfileIndex::format_time(5), "50ns");
        assert_eq!(ProfileIndex::format_time(1500), "15.0µs");
        assert_eq!(ProfileIndex::format_time(1_500_000), "15.0ms");
    }

    #[test]
    fn format_memory_units() {
        assert_eq!(ProfileIndex::format_memory(500), "500B");
        assert_eq!(ProfileIndex::format_memory(2048), "2.0KB");
        assert_eq!(ProfileIndex::format_memory(1_048_576), "1.0MB");
        assert_eq!(ProfileIndex::format_memory(-500), "-500B");
    }

    #[test]
    fn parse_id_assignment_with_name() {
        let mut bare = HashMap::new();
        let mut next = 500_000;
        let (id, name) = parse_id_assignment("(2) /tmp/demo.php", &mut bare, &mut next).unwrap();
        assert_eq!(id, 2);
        assert_eq!(name, "/tmp/demo.php");
    }

    #[test]
    fn parse_id_assignment_without_name() {
        let mut bare = HashMap::new();
        let mut next = 500_000;
        let (id, name) = parse_id_assignment("(2)", &mut bare, &mut next).unwrap();
        assert_eq!(id, 2);
        assert_eq!(name, "");
    }

    #[test]
    fn parse_id_assignment_bare_name() {
        let mut bare = HashMap::new();
        let mut next = 500_000;
        let (id, name) = parse_id_assignment("Object#fibonacci", &mut bare, &mut next).unwrap();
        assert_eq!(id, 500_000);
        assert_eq!(name, "Object#fibonacci");
        // Same name should return same id
        let (id2, _) = parse_id_assignment("Object#fibonacci", &mut bare, &mut next).unwrap();
        assert_eq!(id2, 500_000);
        // Different name gets next id
        let (id3, _) = parse_id_assignment("Object#compute", &mut bare, &mut next).unwrap();
        assert_eq!(id3, 500_001);
    }

    #[test]
    fn cmd_search_finds_matches() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_search("Matrix");
        assert!(out.contains("5 matches"));
        assert!(out.contains("Matrix->multiply"));
        assert!(out.contains("Matrix->set"));
    }

    #[test]
    fn cmd_search_no_matches() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_search("nonexistent");
        assert!(out.contains("no functions matching"));
    }

    #[test]
    fn cmd_search_case_insensitive() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_search("matrix");
        assert!(out.contains("5 matches"));
    }

    #[test]
    fn cmd_tree_shows_hierarchy() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_tree(5);
        assert!(out.contains("{main}"));
        assert!(out.contains("main"));
    }

    #[test]
    fn cmd_hotpath_prefers_callee_inclusive_time_over_call_record_time() {
        // Regression: when a caller has two callees and the parser
        // recorded the *bigger* callee with a smaller per-call time
        // (pseudo-frames, stub PLT entries, and split cost blocks all
        // produce this shape), the traversal used to pick the
        // small-but-well-attributed callee and miss the hot leaf
        // entirely. Picking by the callee's own inclusive_time
        // follows the time regardless of how it was recorded on the
        // edge.
        let cg = "\
events: Time Memory
summary: 1000 0

fl=(1) app
fn=(1) main
1 10 0
cfn=(2) cheap_but_well_attributed
calls=1 1
1 50 0
cfn=(3) hot_but_poorly_attributed
calls=1 1
1 5 0

fl=(1)
fn=(2) cheap_but_well_attributed
1 50 0

fl=(1)
fn=(3) hot_but_poorly_attributed
1 935 0
";
        let idx = ProfileIndex::parse(cg);
        let out = idx.cmd_hotpath();
        assert!(
            out.contains("hot_but_poorly_attributed"),
            "hotpath must follow callee inclusive_time, not the call-edge \
             attribution; got:\n{out}"
        );
        assert!(
            !out.contains("cheap_but_well_attributed"),
            "hotpath walked the wrong branch:\n{out}"
        );
    }

    #[test]
    fn cmd_hotpath_descends_through_pseudo_frames() {
        // Regression: callgrind output produced by perf/native tools
        // often has pseudo-frames like `(below main)` whose explicit
        // call record has near-zero time attribution even though the
        // callee accounts for 99.9% of the program. When choosing the
        // "hottest" callee by the recorded call time, the algorithm
        // stalled one level deep on the low-time pseudo-call and never
        // reached the actual hot leaf. The traversal must prefer the
        // callee whose *own* inclusive_time dominates.
        let cg = "\
events: Time Memory
summary: 3062000000 0

fl=(1) /bin/prog
fn=(1) 0x23140
1 230 0
cfn=(2) (below main)
calls=1 1
1 60 0

fl=(1)
fn=(2) (below main)
1 60 0
cfn=(3) main
calls=1 1
1 3061999940 0

fl=(1)
fn=(3) main
1 100 0
cfn=(4) rand
calls=1000 1
1 3061999840 0

fl=(1)
fn=(4) rand
1 3061999840 0
";
        let idx = ProfileIndex::parse(cg);
        let out = idx.cmd_hotpath();
        assert!(
            out.contains("rand"),
            "hotpath must descend past `(below main)` to the real \
             hot leaf; got:\n{out}"
        );
        assert!(out.contains("main"), "main frame dropped:\n{out}");
    }

    #[test]
    fn cmd_hotpath_shows_chain() {
        let idx = ProfileIndex::parse(SAMPLE);
        let out = idx.cmd_hotpath();
        assert!(out.contains("hottest path"));
        assert!(out.contains("→"));
        // Should follow the chain from root to the hottest leaf
        assert!(out.contains("main") || out.contains("{main}"));
    }

    #[test]
    fn cmd_hotpath_terminates_on_recursion() {
        let cg = "\
events: Time Memory
summary: 1000 0

fl=(1) test.rb
fn=(1) main
1 100 0
cfn=(2)
calls=1 1
1 900 0

fn=(2) fib
1 500 0
cfn=(2)
calls=100 1
1 400 0
";
        let idx = ProfileIndex::parse(cg);
        let out = idx.cmd_hotpath();
        assert!(out.contains("recursive"), "output: {}", out);
        // Must terminate, not infinite loop
        assert!(out.lines().count() < 10);
    }

    #[test]
    fn cmd_tree_terminates_on_recursion() {
        let cg = "\
events: Time Memory
summary: 1000 0

fl=(1) test.rb
fn=(1) main
1 100 0
cfn=(2)
calls=1 1
1 900 0

fn=(2) fib
1 500 0
cfn=(2)
calls=100 1
1 400 0
";
        let idx = ProfileIndex::parse(cg);
        let out = idx.cmd_tree(5);
        assert!(out.contains("recursive"));
        assert!(out.lines().count() < 10);
    }

    #[test]
    fn focus_filters_hotspots() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.focus = Some("Matrix".to_string());
        let out = idx.cmd_hotspots(20, "");
        assert!(out.contains("Matrix->multiply"));
        assert!(!out.contains("buildRandom"));
        assert!(!out.contains("php::mt_rand"));
    }

    #[test]
    fn ignore_filters_hotspots() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.ignore = Some("Matrix".to_string());
        let out = idx.cmd_hotspots(20, "");
        assert!(!out.contains("Matrix->multiply"));
        assert!(!out.contains("Matrix->set"));
        assert!(out.contains("buildRandom"));
    }

    #[test]
    fn focus_and_ignore_combined() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.focus = Some("Matrix".to_string());
        idx.ignore = Some("set".to_string());
        let out = idx.cmd_hotspots(20, "");
        assert!(out.contains("Matrix->multiply"));
        assert!(!out.contains("Matrix->set"));
        assert!(!out.contains("buildRandom"));
    }

    #[test]
    fn reset_clears_filters() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.focus = Some("Matrix".to_string());
        idx.focus = None;
        idx.ignore = None;
        let out = idx.cmd_hotspots(20, "");
        assert!(out.contains("buildRandom"));
        assert!(out.contains("Matrix->multiply"));
    }

    #[test]
    fn focus_filters_hotpath() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.focus = Some("Matrix".to_string());
        let out = idx.cmd_hotpath();
        // Should only follow Matrix-prefixed functions
        for line in out.lines().skip(1) {
            // Each "→ name" line should be a Matrix function
            if let Some(name_part) = line.trim().strip_prefix("→ ") {
                let name = name_part.split("  ").next().unwrap_or("");
                assert!(name.contains("Matrix"), "unexpected function in focused hotpath: {}", name);
            }
        }
        assert!(!out.contains("buildRandom"));
        assert!(!out.contains("{main}"));
    }

    #[test]
    fn focus_filters_tree() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.focus = Some("Matrix".to_string());
        let out = idx.cmd_tree(5);
        assert!(!out.contains("buildRandom"), "tree output: {}", out);
        assert!(!out.contains("{main}"), "tree output: {}", out);
        assert!(!out.contains("php::"), "tree output: {}", out);
        // Should contain Matrix functions
        assert!(out.contains("Matrix"), "tree output: {}", out);
    }

    #[test]
    fn ignore_filters_hotpath() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.ignore = Some("multiply".to_string());
        let out = idx.cmd_hotpath();
        assert!(!out.contains("multiply"), "hotpath output: {}", out);
    }

    #[test]
    fn ignore_filters_tree() {
        let mut idx = ProfileIndex::parse(SAMPLE);
        idx.ignore = Some("multiply".to_string());
        let out = idx.cmd_tree(5);
        assert!(!out.contains("multiply"), "tree output: {}", out);
    }

    // =========================================================================
    // StackProf / Ruby profiler (bare-name callgrind format)
    // =========================================================================
    mod stackprof {
        use super::*;

        const SAMPLE: &str = include_str!("../tests/fixtures/stackprof_callgrind_sample.out");

        #[test]
        fn parse_finds_all_functions() {
            let idx = ProfileIndex::parse(SAMPLE);
            // Set#add, Object#fibonacci, block in <top (required)>,
            // <top (required)>, Benchmark.measure, Kernel#load, Set#include?
            assert_eq!(idx.functions.len(), 7);
        }

        #[test]
        fn parse_totals() {
            let idx = ProfileIndex::parse(SAMPLE);
            assert_eq!(idx.total_time, 313470);
        }

        #[test]
        fn parse_bare_name_self_time() {
            let idx = ProfileIndex::parse(SAMPLE);
            let fib = idx.functions.iter().find(|f| f.name == "Object#fibonacci").unwrap();
            assert_eq!(fib.self_time, 16200);
        }

        #[test]
        fn parse_bare_name_calls() {
            let idx = ProfileIndex::parse(SAMPLE);
            let fib = idx.functions.iter().find(|f| f.name == "Object#fibonacci").unwrap();
            assert_eq!(fib.calls.len(), 1);
            let self_call = &fib.calls[0];
            assert_eq!(self_call.callee, "Object#fibonacci");
            assert_eq!(self_call.call_count, 2624);
        }

        #[test]
        fn parse_inclusive_time() {
            let idx = ProfileIndex::parse(SAMPLE);
            let fib = idx.functions.iter().find(|f| f.name == "Object#fibonacci").unwrap();
            // self 16200 + recursive call 262400
            assert_eq!(fib.inclusive_time, 16200 + 262400);
        }

        #[test]
        fn cmd_hotspots() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_hotspots(5, "");
            assert!(out.contains("Object#fibonacci"));
        }

        #[test]
        fn cmd_flat() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_flat(5, "");
            // fibonacci has highest self time
            let lines: Vec<&str> = out.lines().collect();
            assert!(lines[2].contains("Object#fibonacci"));
        }

        #[test]
        fn cmd_calls() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_calls("fibonacci");
            assert!(out.contains("Object#fibonacci"));
            assert!(out.contains("2624x"));
        }

        #[test]
        fn cmd_callers() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_callers("fibonacci");
            assert!(out.contains("block in <top (required)>"));
            assert!(out.contains("Object#fibonacci"));
        }

        #[test]
        fn cmd_inspect() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_inspect("fibonacci");
            assert!(out.contains("Object#fibonacci"));
            assert!(out.contains("Self:"));
            assert!(out.contains("Inclusive:"));
        }

        #[test]
        fn cmd_search() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_search("Object");
            assert!(out.contains("Object#fibonacci"));
        }

        #[test]
        fn cmd_stats() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_stats("");
            assert!(out.contains("Functions:      7"));
        }

        #[test]
        fn cmd_hotpath_terminates_on_recursion() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_hotpath();
            assert!(out.contains("hottest path"), "output: {}", out);
            // fibonacci is recursive — must not infinite loop
            assert!(out.lines().count() < 20, "output: {}", out);
            // If the hotpath reaches fibonacci→fibonacci, it should mark recursion
            if out.contains("Object#fibonacci") {
                let fib_count = out.matches("Object#fibonacci").count();
                // Should appear at most twice (enter + recursive marker)
                assert!(fib_count <= 2, "fibonacci appears {} times: {}", fib_count, out);
            }
        }

        #[test]
        fn cmd_tree_terminates_on_recursion() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_tree(5);
            assert!(out.lines().count() < 30, "output: {}", out);
        }
    }

    // =========================================================================
    // Callgrind / native profiler (valgrind numeric-ID format)
    // =========================================================================
    mod callgrind_native {
        use super::*;

        const SAMPLE: &str = include_str!("../tests/fixtures/callgrind_native_sample.out");

        #[test]
        fn parse_finds_all_functions() {
            let idx = ProfileIndex::parse(SAMPLE);
            // main, matrix_set, matrix_multiply, build_random, matrix_trace,
            // qsort_compare, do_sort
            assert_eq!(idx.functions.len(), 7);
        }

        #[test]
        fn parse_totals() {
            let idx = ProfileIndex::parse(SAMPLE);
            assert_eq!(idx.total_time, 2473500);
        }

        #[test]
        fn parse_self_time() {
            let idx = ProfileIndex::parse(SAMPLE);
            let mul = idx.functions.iter().find(|f| f.name == "matrix_multiply").unwrap();
            assert_eq!(mul.self_time, 175000);
        }

        #[test]
        fn parse_forward_ref_callee() {
            let idx = ProfileIndex::parse(SAMPLE);
            // main calls build_random (cfn=(4) appears before fn=(4) is defined)
            let main = idx.functions.iter().find(|f| f.name == "main").unwrap();
            let br = main.calls.iter().find(|c| c.callee == "build_random").unwrap();
            assert_eq!(br.call_count, 2);
        }

        #[test]
        fn parse_back_ref_callee() {
            let idx = ProfileIndex::parse(SAMPLE);
            // matrix_multiply calls matrix_set (cfn=(2) after fn=(2) defined)
            let mul = idx.functions.iter().find(|f| f.name == "matrix_multiply").unwrap();
            let set = mul.calls.iter().find(|c| c.callee == "matrix_set").unwrap();
            assert_eq!(set.call_count, 900);
        }

        #[test]
        fn parse_recursive_calls() {
            let idx = ProfileIndex::parse(SAMPLE);
            let qsort = idx.functions.iter().find(|f| f.name == "qsort_compare").unwrap();
            assert_eq!(qsort.calls.len(), 1);
            assert_eq!(qsort.calls[0].callee, "qsort_compare");
            assert_eq!(qsort.calls[0].call_count, 3100);
        }

        #[test]
        fn cmd_hotspots() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_hotspots(5, "");
            assert!(out.contains("main"));
            assert!(out.contains("qsort_compare"));
        }

        #[test]
        fn cmd_flat() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_flat(5, "");
            assert!(out.contains("matrix_multiply"));
        }

        #[test]
        fn cmd_calls() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_calls("main");
            assert!(out.contains("build_random"));
            assert!(out.contains("matrix_multiply"));
            assert!(out.contains("matrix_trace"));
        }

        #[test]
        fn cmd_callers() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_callers("matrix_set");
            assert!(out.contains("matrix_multiply"));
            assert!(out.contains("build_random"));
        }

        #[test]
        fn cmd_inspect() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_inspect("qsort_compare");
            assert!(out.contains("Self:"));
            assert!(out.contains("Callees:"));
            assert!(out.contains("qsort_compare"));
        }

        #[test]
        fn cmd_search() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_search("matrix");
            assert!(out.contains("matrix_multiply"));
            assert!(out.contains("matrix_set"));
            assert!(out.contains("matrix_trace"));
        }

        #[test]
        fn cmd_stats() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_stats("");
            assert!(out.contains("Functions:      7"));
        }

        #[test]
        fn cmd_hotpath_terminates_on_recursion() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_hotpath();
            assert!(out.contains("hottest path"), "output: {}", out);
            // qsort_compare is recursive — must not infinite loop
            assert!(out.lines().count() < 20, "output: {}", out);
            if out.contains("qsort_compare") {
                let count = out.matches("qsort_compare").count();
                assert!(count <= 2, "qsort_compare appears {} times: {}", count, out);
            }
        }

        #[test]
        fn cmd_tree_terminates_on_recursion() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_tree(5);
            assert!(out.lines().count() < 30, "output: {}", out);
            // Recursive functions should be marked
            if out.contains("qsort_compare") {
                // Should appear but not blow up
                let count = out.matches("qsort_compare").count();
                assert!(count <= 3, "qsort_compare appears {} times: {}", count, out);
            }
        }

        #[test]
        fn cmd_hotpath_follows_through_forward_refs() {
            let idx = ProfileIndex::parse(SAMPLE);
            let out = idx.cmd_hotpath();
            // main→build_random or main→matrix_multiply should be reachable
            // (not broken by forward-ref callee resolution)
            assert!(out.contains("main"), "output: {}", out);
            let lines = out.lines().count();
            // Should follow at least 2 levels deep
            assert!(lines >= 3, "hotpath too shallow ({}): {}", lines, out);
        }
    }

    // =========================================================================
    // Callgrind with multiple hardware counters (Ir Bc Bcm Bi Bim)
    // =========================================================================
    mod callgrind_branch_counters {
        use super::*;

        /// Minimal callgrind profile with 5-event columns: Ir Bc Bcm Bi Bim.
        /// Exercises the branch-misprediction surfacing added for scenario 14.
        const MULTI_EVENT: &str = "\
version: 1
creator: callgrind-3.25.1
pid: 999
cmd: ./bench
part: 1
positions: line

events: Ir Bc Bcm Bi Bim

fl=(1) /tmp/bench.c
fn=(1) classify
10 1000000 500000 50000 100 5
+1 200000 100 10 0 0

fl=(1)
fn=(2) main
30 5000 100 2 50 1

summary: 1205000 600200 50012 150 6
";

        #[test]
        fn parse_event_names() {
            let idx = ProfileIndex::parse(MULTI_EVENT);
            assert_eq!(
                idx.event_names,
                vec!["Ir", "Bc", "Bcm", "Bi", "Bim"],
                "event_names must reflect the events: header"
            );
        }

        #[test]
        fn parse_event_totals_from_summary() {
            let idx = ProfileIndex::parse(MULTI_EVENT);
            assert_eq!(
                idx.event_totals,
                vec![1205000, 600200, 50012, 150, 6],
                "event_totals must reflect all 5 summary columns"
            );
        }

        #[test]
        fn cmd_stats_surfaces_branch_mispred() {
            let idx = ProfileIndex::parse(MULTI_EVENT);
            let out = idx.cmd_stats("");
            assert!(
                out.contains("Branch mispred"),
                "cmd_stats must show branch mispredictions when Bcm is present:\n{out}"
            );
            // Total Bcm from summary line is 50012
            assert!(
                out.contains("50012"),
                "cmd_stats must include the total Bcm count (50012):\n{out}"
            );
        }

        #[test]
        fn cmd_stats_shows_mispredict_rate() {
            let idx = ProfileIndex::parse(MULTI_EVENT);
            let out = idx.cmd_stats("");
            assert!(
                out.contains("Branch mispredict%"),
                "cmd_stats must show mispredict % when both Bc and Bcm are present:\n{out}"
            );
        }

        #[test]
        fn per_function_bcm_surfaced_in_inspect() {
            let idx = ProfileIndex::parse(MULTI_EVENT);
            let out = idx.cmd_inspect("classify");
            assert!(
                out.contains("Branch mispred"),
                "cmd_inspect must show per-function Bcm when non-zero:\n{out}"
            );
            // The classify function has Bcm=50000+10=50010 from its two cost lines
            assert!(
                out.contains("50010") || out.contains("50000"),
                "cmd_inspect must show a Bcm value near 50000 for classify:\n{out}"
            );
        }

        #[test]
        fn stats_no_hw_section_for_standard_php_profile() {
            // PHP/Xdebug profiles emit events: Time_(10ns) Memory_(bytes).
            // They have exactly 2 columns — the HW counters section must NOT appear.
            let php = "events: Time_(10ns) Memory_(bytes)\n\
                       fl=(1) /app/index.php\nfn=(1) main\n1 5000 1024\n\
                       summary: 5000 1024\n";
            let idx = ProfileIndex::parse(php);
            let out = idx.cmd_stats("");
            assert!(
                !out.contains("Hardware counters"),
                "PHP profiles must not show a hardware-counters section:\n{out}"
            );
        }
    }

    // =========================================================================
    // Xdebug / PHP profiler — recursion-specific tests
    // (basic parsing already covered by top-level tests using SAMPLE)
    // =========================================================================
    mod xdebug_recursion {
        use super::*;

        /// PHP-style recursive function (e.g. recursive directory scan)
        const RECURSIVE_PHP: &str = "\
events: Time_(10ns) Memory_(bytes)

fl=(1) /app/scan.php
fn=(1) {main}
1 500 0
cfl=(1)
cfn=(2)
calls=1 5
1 90000 40000

fl=(1)
fn=(2) scan_dir
5 3000 1000
cfl=(1)
cfn=(2)
calls=50 5
6 85000 38000
cfl=(1)
cfn=(3)
calls=200 20
10 2000 1000

fl=(1)
fn=(3) process_file
20 2000 1000

summary: 182500 80000
";

        #[test]
        fn parse_finds_all_functions() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            assert_eq!(idx.functions.len(), 3);
        }

        #[test]
        fn parse_recursive_self_call() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let scan = idx.functions.iter().find(|f| f.name == "scan_dir").unwrap();
            let self_call = scan.calls.iter().find(|c| c.callee == "scan_dir").unwrap();
            assert_eq!(self_call.call_count, 50);
        }

        #[test]
        fn cmd_hotpath_terminates() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_hotpath();
            assert!(out.contains("hottest path"), "output: {}", out);
            assert!(out.contains("recursive"), "output: {}", out);
            assert!(out.lines().count() < 10, "output: {}", out);
        }

        #[test]
        fn cmd_tree_terminates() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_tree(5);
            assert!(out.contains("recursive"), "output: {}", out);
            assert!(out.lines().count() < 15, "output: {}", out);
        }

        #[test]
        fn cmd_hotspots() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_hotspots(5, "");
            assert!(out.contains("scan_dir"));
        }

        #[test]
        fn cmd_calls() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_calls("scan_dir");
            assert!(out.contains("scan_dir"));
            assert!(out.contains("50x"));
            assert!(out.contains("process_file"));
        }

        #[test]
        fn cmd_callers() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_callers("scan_dir");
            assert!(out.contains("{main}"));
            assert!(out.contains("scan_dir")); // recursive caller
        }

        #[test]
        fn cmd_inspect() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_inspect("scan_dir");
            assert!(out.contains("Self:"));
            assert!(out.contains("Inclusive:"));
            assert!(out.contains("Callees:"));
        }

        #[test]
        fn cmd_stats() {
            let idx = ProfileIndex::parse(RECURSIVE_PHP);
            let out = idx.cmd_stats("");
            assert!(out.contains("Functions:      3"));
        }

        #[test]
        fn focus_on_recursive_fn_shows_hotpath() {
            let mut idx = ProfileIndex::parse(RECURSIVE_PHP);
            idx.focus = Some("scan_dir".to_string());
            let out = idx.cmd_hotpath();
            assert!(out.contains("scan_dir"), "output: {}", out);
            assert!(out.contains("recursive"), "output: {}", out);
        }

        #[test]
        fn focus_on_recursive_fn_shows_tree() {
            let mut idx = ProfileIndex::parse(RECURSIVE_PHP);
            idx.focus = Some("scan_dir".to_string());
            let out = idx.cmd_tree(5);
            assert!(out.contains("scan_dir"), "output: {}", out);
            assert!(!out.contains("{main}"), "output: {}", out);
            assert!(!out.contains("process_file"), "output: {}", out);
        }
    }

    // =========================================================================
    // Cross-recursion with focus/ignore filters
    // =========================================================================
    mod cross_recursion_filters {
        use super::*;

        /// mutual_a ↔ mutual_b cross-recursion, plus fibonacci (self-recursive)
        const CROSS_RECURSIVE: &str = "\
events: Time_(10ns) Memory_(bytes)

fl=(1) /app/test.php
fn=(1) {main}
1 500 0
cfn=(2)
calls=10 5
1 30000 0
cfn=(4)
calls=5 20
1 50000 0

fl=(1)
fn=(2) mutual_a
5 2000 0
cfn=(3)
calls=20000 10
6 15000 0
cfn=(2)
calls=20000 10
6 13000 0

fl=(1)
fn=(3) mutual_b
10 1500 0
cfn=(2)
calls=20000 5
11 14000 0

fl=(1)
fn=(4) fibonacci
20 25000 0
cfn=(4)
calls=5000000 20
20 25000 0

summary: 176000 0
";

        #[test]
        fn parse_cross_recursion() {
            let idx = ProfileIndex::parse(CROSS_RECURSIVE);
            let a = idx.functions.iter().find(|f| f.name == "mutual_a").unwrap();
            let b_call = a.calls.iter().find(|c| c.callee == "mutual_b").unwrap();
            assert_eq!(b_call.call_count, 20000);
            let b = idx.functions.iter().find(|f| f.name == "mutual_b").unwrap();
            let a_call = b.calls.iter().find(|c| c.callee == "mutual_a").unwrap();
            assert_eq!(a_call.call_count, 20000);
        }

        #[test]
        fn hotpath_terminates_with_cross_recursion() {
            let idx = ProfileIndex::parse(CROSS_RECURSIVE);
            let out = idx.cmd_hotpath();
            assert!(out.lines().count() < 15, "output: {}", out);
        }

        #[test]
        fn tree_terminates_with_cross_recursion() {
            let idx = ProfileIndex::parse(CROSS_RECURSIVE);
            let out = idx.cmd_tree(5);
            assert!(out.lines().count() < 20, "output: {}", out);
        }

        #[test]
        fn focus_mutual_shows_cross_recursive_hotpath() {
            let mut idx = ProfileIndex::parse(CROSS_RECURSIVE);
            idx.focus = Some("mutual".to_string());
            let out = idx.cmd_hotpath();
            // Should find mutual_a as root (highest inclusive time among mutual_*)
            assert!(out.contains("mutual_a"), "output: {}", out);
            assert!(out.contains("mutual_b"), "output: {}", out);
            assert!(out.contains("recursive"), "output: {}", out);
            assert!(!out.contains("fibonacci"), "output: {}", out);
            assert!(!out.contains("{main}"), "output: {}", out);
        }

        #[test]
        fn focus_mutual_shows_cross_recursive_tree() {
            let mut idx = ProfileIndex::parse(CROSS_RECURSIVE);
            idx.focus = Some("mutual".to_string());
            let out = idx.cmd_tree(5);
            assert!(out.contains("mutual_a"), "output: {}", out);
            assert!(out.contains("mutual_b"), "output: {}", out);
            assert!(!out.contains("fibonacci"), "output: {}", out);
            assert!(!out.contains("{main}"), "output: {}", out);
        }

        #[test]
        fn ignore_fibonacci_keeps_mutual() {
            let mut idx = ProfileIndex::parse(CROSS_RECURSIVE);
            idx.ignore = Some("fibonacci".to_string());
            let out = idx.cmd_tree(5);
            assert!(out.contains("mutual_a"), "output: {}", out);
            assert!(!out.contains("fibonacci"), "output: {}", out);
        }

        #[test]
        fn ignore_mutual_keeps_fibonacci() {
            let mut idx = ProfileIndex::parse(CROSS_RECURSIVE);
            idx.ignore = Some("mutual".to_string());
            let out = idx.cmd_hotpath();
            assert!(!out.contains("mutual"), "output: {}", out);
            assert!(out.contains("fibonacci"), "output: {}", out);
        }
    }
}