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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Copyright 2018 Joyent, Inc.
*/
/*
* MDB Target Layer
*
* The *target* is the program being inspected by the debugger. The MDB target
* layer provides a set of functions that insulate common debugger code,
* including the MDB Module API, from the implementation details of how the
* debugger accesses information from a given target. Each target exports a
* standard set of properties, including one or more address spaces, one or
* more symbol tables, a set of load objects, and a set of threads that can be
* examined using the interfaces in <mdb/mdb_target.h>. This technique has
* been employed successfully in other debuggers, including [1], primarily
* to improve portability, although the term "target" often refers to the
* encapsulation of architectural or operating system-specific details. The
* target abstraction is useful for MDB because it allows us to easily extend
* the debugger to examine a variety of different program forms. Primarily,
* the target functions validate input arguments and then call an appropriate
* function in the target ops vector, defined in <mdb/mdb_target_impl.h>.
* However, this interface layer provides a very high level of flexibility for
* separating the debugger interface from instrumentation details. Experience
* has shown this kind of design can facilitate separating out debugger
* instrumentation into an external agent [2] and enable the development of
* advanced instrumentation frameworks [3]. We want MDB to be an ideal
* extensible framework for the development of such applications.
*
* Aside from a set of wrapper functions, the target layer also provides event
* management for targets that represent live executing programs. Our model of
* events is also extensible, and is based upon work in [3] and [4]. We define
* a *software event* as a state transition in the target program (for example,
* the transition of the program counter to a location of interest) that is
* observed by the debugger or its agent. A *software event specifier* is a
* description of a class of software events that is used by the debugger to
* instrument the target so that the corresponding software events can be
* observed. In MDB, software event specifiers are represented by the
* mdb_sespec_t structure, defined in <mdb/mdb_target_impl.h>. As the user,
* the internal debugger code, and MDB modules may all wish to observe software
* events and receive appropriate notification and callbacks, we do not expose
* software event specifiers directly as part of the user interface. Instead,
* clients of the target layer request that events be observed by creating
* new *virtual event specifiers*. Each virtual specifier is named by a unique
* non-zero integer (the VID), and is represented by a mdb_vespec_t structure.
* One or more virtual specifiers are then associated with each underlying
* software event specifier. This design enforces the constraint that the
* target must only insert one set of instrumentation, regardless of how many
* times the target layer was asked to trace a given event. For example, if
* multiple clients request a breakpoint at a particular address, the virtual
* specifiers will map to the same sespec, ensuring that only one breakpoint
* trap instruction is actually planted at the given target address. When no
* virtual specifiers refer to an sespec, it is no longer needed and can be
* removed, along with the corresponding instrumentation.
*
* The following state transition diagram illustrates the life cycle of a
* software event specifier and example transitions:
*
* cont/
* +--------+ delete +--------+ stop +-------+
* (|( DEAD )|) <------- ( ACTIVE ) <------> ( ARMED )
* +--------+ +--------+ +-------+
* ^ load/unload ^ ^ failure/ |
* delete | object / \ reset | failure
* | v v |
* | +--------+ +-------+ |
* +---- ( IDLE ) ( ERR ) <----+
* | +--------+ +-------+
* | |
* +------------------------------+
*
* The MDB execution control model is based upon the synchronous debugging
* model exported by Solaris proc(4). A target program is set running or the
* debugger is attached to a running target. On ISTOP (stop on event of
* interest), one target thread is selected as the representative. The
* algorithm for selecting the representative is target-specific, but we assume
* that if an observed software event has occurred, the target will select the
* thread that triggered the state transition of interest. The other threads
* are stopped in sympathy with the representative as soon as possible. Prior
* to continuing the target, we plant our instrumentation, transitioning event
* specifiers from the ACTIVE to the ARMED state, and then back again when the
* target stops. We then query each active event specifier to learn which ones
* are matched, and then invoke the callbacks associated with their vespecs.
* If an OS error occurs while attempting to arm or disarm a specifier, the
* specifier is transitioned to the ERROR state; we will attempt to arm it
* again at the next continue. If no target process is under our control or
* if an event is not currently applicable (e.g. a deferred breakpoint on an
* object that is not yet loaded), it remains in the IDLE state. The target
* implementation should intercept object load events and then transition the
* specifier to the ACTIVE state when the corresponding object is loaded.
*
* To simplify the debugger implementation and allow targets to easily provide
* new types of observable events, most of the event specifier management is
* done by the target layer. Each software event specifier provides an ops
* vector of subroutines that the target layer can call to perform the
* various state transitions described above. The target maintains two lists
* of mdb_sespec_t's: the t_idle list (IDLE state) and the t_active list
* (ACTIVE, ARMED, and ERROR states). Each mdb_sespec_t maintains a list of
* associated mdb_vespec_t's. If an sespec is IDLE or ERROR, its se_errno
* field will have an errno value specifying the reason for its inactivity.
* The vespec stores the client's callback function and private data, and the
* arguments used to construct the sespec. All objects are reference counted
* so we can destroy an object when it is no longer needed. The mdb_sespec_t
* invariants for the respective states are as follows:
*
* IDLE: on t_idle list, se_data == NULL, se_errno != 0, se_ctor not called
* ACTIVE: on t_active list, se_data valid, se_errno == 0, se_ctor called
* ARMED: on t_active list, se_data valid, se_errno == 0, se_ctor called
* ERROR: on t_active list, se_data valid, se_errno != 0, se_ctor called
*
* Additional commentary on specific state transitions and issues involving
* event management can be found below near the target layer functions.
*
* References
*
* [1] John Gilmore, "Working in GDB", Technical Report, Cygnus Support,
* 1.84 edition, 1994.
*
* [2] David R. Hanson and Mukund Raghavachari, "A Machine-Independent
* Debugger", Software--Practice and Experience, 26(11), 1277-1299(1996).
*
* [3] Michael W. Shapiro, "RDB: A System for Incremental Replay Debugging",
* Technical Report CS-97-12, Department of Computer Science,
* Brown University.
*
* [4] Daniel B. Price, "New Techniques for Replay Debugging", Technical
* Report CS-98-05, Department of Computer Science, Brown University.
*/
#include <mdb/mdb_target_impl.h>
#include <mdb/mdb_debug.h>
#include <mdb/mdb_modapi.h>
#include <mdb/mdb_err.h>
#include <mdb/mdb_callb.h>
#include <mdb/mdb_gelf.h>
#include <mdb/mdb_io_impl.h>
#include <mdb/mdb_string.h>
#include <mdb/mdb_signal.h>
#include <mdb/mdb_frame.h>
#include <mdb/mdb.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/signal.h>
#include <strings.h>
#include <stdlib.h>
#include <errno.h>
/*
* Define convenience macros for referencing the set of vespec flag bits that
* are preserved by the target implementation, and the set of bits that
* determine automatic ve_hits == ve_limit behavior.
*/
#define T_IMPL_BITS \
(MDB_TGT_SPEC_INTERNAL | MDB_TGT_SPEC_SILENT | MDB_TGT_SPEC_MATCHED | \
MDB_TGT_SPEC_DELETED)
#define T_AUTO_BITS \
(MDB_TGT_SPEC_AUTOSTOP | MDB_TGT_SPEC_AUTODEL | MDB_TGT_SPEC_AUTODIS)
/*
* Define convenience macro for referencing target flag pending continue bits.
*/
#define T_CONT_BITS \
(MDB_TGT_F_STEP | MDB_TGT_F_STEP_OUT | MDB_TGT_F_NEXT | MDB_TGT_F_CONT)
mdb_tgt_t *
mdb_tgt_create(mdb_tgt_ctor_f *ctor, int flags, int argc, const char *argv[])
{
mdb_module_t *mp;
mdb_tgt_t *t;
if (flags & ~MDB_TGT_F_ALL) {
(void) set_errno(EINVAL);
return (NULL);
}
t = mdb_zalloc(sizeof (mdb_tgt_t), UM_SLEEP);
mdb_list_append(&mdb.m_tgtlist, t);
t->t_module = &mdb.m_rmod;
t->t_matched = T_SE_END;
t->t_flags = flags;
t->t_vepos = 1;
t->t_veneg = 1;
for (mp = mdb.m_mhead; mp != NULL; mp = mp->mod_next) {
if (ctor == mp->mod_tgt_ctor) {
t->t_module = mp;
break;
}
}
if (ctor(t, argc, argv) != 0) {
mdb_list_delete(&mdb.m_tgtlist, t);
mdb_free(t, sizeof (mdb_tgt_t));
return (NULL);
}
mdb_dprintf(MDB_DBG_TGT, "t_create %s (%p)\n",
t->t_module->mod_name, (void *)t);
(void) t->t_ops->t_status(t, &t->t_status);
return (t);
}
int
mdb_tgt_getflags(mdb_tgt_t *t)
{
return (t->t_flags);
}
int
mdb_tgt_setflags(mdb_tgt_t *t, int flags)
{
if (flags & ~MDB_TGT_F_ALL)
return (set_errno(EINVAL));
return (t->t_ops->t_setflags(t, flags));
}
int
mdb_tgt_setcontext(mdb_tgt_t *t, void *context)
{
return (t->t_ops->t_setcontext(t, context));
}
/*ARGSUSED*/
static int
tgt_delete_vespec(mdb_tgt_t *t, void *private, int vid, void *data)
{
(void) mdb_tgt_vespec_delete(t, vid);
return (0);
}
void
mdb_tgt_destroy(mdb_tgt_t *t)
{
mdb_xdata_t *xdp, *nxdp;
if (mdb.m_target == t) {
mdb_dprintf(MDB_DBG_TGT, "t_deactivate %s (%p)\n",
t->t_module->mod_name, (void *)t);
t->t_ops->t_deactivate(t);
mdb.m_target = NULL;
}
mdb_dprintf(MDB_DBG_TGT, "t_destroy %s (%p)\n",
t->t_module->mod_name, (void *)t);
for (xdp = mdb_list_next(&t->t_xdlist); xdp != NULL; xdp = nxdp) {
nxdp = mdb_list_next(xdp);
mdb_list_delete(&t->t_xdlist, xdp);
mdb_free(xdp, sizeof (mdb_xdata_t));
}
mdb_tgt_sespec_idle_all(t, EBUSY, TRUE);
(void) mdb_tgt_vespec_iter(t, tgt_delete_vespec, NULL);
t->t_ops->t_destroy(t);
mdb_list_delete(&mdb.m_tgtlist, t);
mdb_free(t, sizeof (mdb_tgt_t));
if (mdb.m_target == NULL)
mdb_tgt_activate(mdb_list_prev(&mdb.m_tgtlist));
}
void
mdb_tgt_activate(mdb_tgt_t *t)
{
mdb_tgt_t *otgt = mdb.m_target;
if (mdb.m_target != NULL) {
mdb_dprintf(MDB_DBG_TGT, "t_deactivate %s (%p)\n",
mdb.m_target->t_module->mod_name, (void *)mdb.m_target);
mdb.m_target->t_ops->t_deactivate(mdb.m_target);
}
if ((mdb.m_target = t) != NULL) {
const char *v = strstr(mdb.m_root, "%V");
mdb_dprintf(MDB_DBG_TGT, "t_activate %s (%p)\n",
t->t_module->mod_name, (void *)t);
/*
* If the root was explicitly set with -R and contains %V,
* expand it like a path. If the resulting directory is
* not present, then replace %V with "latest" and re-evaluate.
*/
if (v != NULL) {
char old_root[MAXPATHLEN];
const char **p;
#ifndef _KMDB
struct stat s;
#endif
size_t len;
p = mdb_path_alloc(mdb.m_root, &len);
(void) strcpy(old_root, mdb.m_root);
(void) strncpy(mdb.m_root, p[0], MAXPATHLEN);
mdb.m_root[MAXPATHLEN - 1] = '\0';
mdb_path_free(p, len);
#ifndef _KMDB
if (stat(mdb.m_root, &s) == -1 && errno == ENOENT) {
mdb.m_flags |= MDB_FL_LATEST;
p = mdb_path_alloc(old_root, &len);
(void) strncpy(mdb.m_root, p[0], MAXPATHLEN);
mdb.m_root[MAXPATHLEN - 1] = '\0';
mdb_path_free(p, len);
}
#endif
}
/*
* Re-evaluate the macro and dmod paths now that we have the
* new target set and m_root figured out.
*/
if (otgt == NULL) {
mdb_set_ipath(mdb.m_ipathstr);
mdb_set_lpath(mdb.m_lpathstr);
}
t->t_ops->t_activate(t);
}
}
void
mdb_tgt_periodic(mdb_tgt_t *t)
{
t->t_ops->t_periodic(t);
}
const char *
mdb_tgt_name(mdb_tgt_t *t)
{
return (t->t_ops->t_name(t));
}
const char *
mdb_tgt_isa(mdb_tgt_t *t)
{
return (t->t_ops->t_isa(t));
}
const char *
mdb_tgt_platform(mdb_tgt_t *t)
{
return (t->t_ops->t_platform(t));
}
int
mdb_tgt_uname(mdb_tgt_t *t, struct utsname *utsp)
{
return (t->t_ops->t_uname(t, utsp));
}
int
mdb_tgt_dmodel(mdb_tgt_t *t)
{
return (t->t_ops->t_dmodel(t));
}
int
mdb_tgt_auxv(mdb_tgt_t *t, const auxv_t **auxvp)
{
return (t->t_ops->t_auxv(t, auxvp));
}
ssize_t
mdb_tgt_aread(mdb_tgt_t *t, mdb_tgt_as_t as,
void *buf, size_t n, mdb_tgt_addr_t addr)
{
if (t->t_flags & MDB_TGT_F_ASIO)
return (t->t_ops->t_aread(t, as, buf, n, addr));
switch ((uintptr_t)as) {
case (uintptr_t)MDB_TGT_AS_VIRT:
case (uintptr_t)MDB_TGT_AS_VIRT_I:
case (uintptr_t)MDB_TGT_AS_VIRT_S:
return (t->t_ops->t_vread(t, buf, n, addr));
case (uintptr_t)MDB_TGT_AS_PHYS:
return (t->t_ops->t_pread(t, buf, n, addr));
case (uintptr_t)MDB_TGT_AS_FILE:
return (t->t_ops->t_fread(t, buf, n, addr));
case (uintptr_t)MDB_TGT_AS_IO:
return (t->t_ops->t_ioread(t, buf, n, addr));
}
return (t->t_ops->t_aread(t, as, buf, n, addr));
}
ssize_t
mdb_tgt_awrite(mdb_tgt_t *t, mdb_tgt_as_t as,
const void *buf, size_t n, mdb_tgt_addr_t addr)
{
if (!(t->t_flags & MDB_TGT_F_RDWR))
return (set_errno(EMDB_TGTRDONLY));
if (t->t_flags & MDB_TGT_F_ASIO)
return (t->t_ops->t_awrite(t, as, buf, n, addr));
switch ((uintptr_t)as) {
case (uintptr_t)MDB_TGT_AS_VIRT:
case (uintptr_t)MDB_TGT_AS_VIRT_I:
case (uintptr_t)MDB_TGT_AS_VIRT_S:
return (t->t_ops->t_vwrite(t, buf, n, addr));
case (uintptr_t)MDB_TGT_AS_PHYS:
return (t->t_ops->t_pwrite(t, buf, n, addr));
case (uintptr_t)MDB_TGT_AS_FILE:
return (t->t_ops->t_fwrite(t, buf, n, addr));
case (uintptr_t)MDB_TGT_AS_IO:
return (t->t_ops->t_iowrite(t, buf, n, addr));
}
return (t->t_ops->t_awrite(t, as, buf, n, addr));
}
ssize_t
mdb_tgt_vread(mdb_tgt_t *t, void *buf, size_t n, uintptr_t addr)
{
return (t->t_ops->t_vread(t, buf, n, addr));
}
ssize_t
mdb_tgt_vwrite(mdb_tgt_t *t, const void *buf, size_t n, uintptr_t addr)
{
if (t->t_flags & MDB_TGT_F_RDWR)
return (t->t_ops->t_vwrite(t, buf, n, addr));
return (set_errno(EMDB_TGTRDONLY));
}
ssize_t
mdb_tgt_pread(mdb_tgt_t *t, void *buf, size_t n, physaddr_t addr)
{
return (t->t_ops->t_pread(t, buf, n, addr));
}
ssize_t
mdb_tgt_pwrite(mdb_tgt_t *t, const void *buf, size_t n, physaddr_t addr)
{
if (t->t_flags & MDB_TGT_F_RDWR)
return (t->t_ops->t_pwrite(t, buf, n, addr));
return (set_errno(EMDB_TGTRDONLY));
}
ssize_t
mdb_tgt_fread(mdb_tgt_t *t, void *buf, size_t n, uintptr_t addr)
{
return (t->t_ops->t_fread(t, buf, n, addr));
}
ssize_t
mdb_tgt_fwrite(mdb_tgt_t *t, const void *buf, size_t n, uintptr_t addr)
{
if (t->t_flags & MDB_TGT_F_RDWR)
return (t->t_ops->t_fwrite(t, buf, n, addr));
return (set_errno(EMDB_TGTRDONLY));
}
ssize_t
mdb_tgt_ioread(mdb_tgt_t *t, void *buf, size_t n, uintptr_t addr)
{
return (t->t_ops->t_ioread(t, buf, n, addr));
}
ssize_t
mdb_tgt_iowrite(mdb_tgt_t *t, const void *buf, size_t n, uintptr_t addr)
{
if (t->t_flags & MDB_TGT_F_RDWR)
return (t->t_ops->t_iowrite(t, buf, n, addr));
return (set_errno(EMDB_TGTRDONLY));
}
int
mdb_tgt_vtop(mdb_tgt_t *t, mdb_tgt_as_t as, uintptr_t va, physaddr_t *pap)
{
return (t->t_ops->t_vtop(t, as, va, pap));
}
ssize_t
mdb_tgt_readstr(mdb_tgt_t *t, mdb_tgt_as_t as, char *buf,
size_t nbytes, mdb_tgt_addr_t addr)
{
ssize_t n, nread = mdb_tgt_aread(t, as, buf, nbytes, addr);
char *p;
if (nread >= 0) {
if ((p = memchr(buf, '\0', nread)) != NULL)
nread = (size_t)(p - buf);
goto done;
}
nread = 0;
p = &buf[0];
while (nread < nbytes && (n = mdb_tgt_aread(t, as, p, 1, addr)) == 1) {
if (*p == '\0')
return (nread);
nread++;
addr++;
p++;
}
if (nread == 0 && n == -1)
return (-1); /* If we can't even read a byte, return -1 */
done:
if (nbytes != 0)
buf[MIN(nread, nbytes - 1)] = '\0';
return (nread);
}
ssize_t
mdb_tgt_writestr(mdb_tgt_t *t, mdb_tgt_as_t as,
const char *buf, mdb_tgt_addr_t addr)
{
ssize_t nwritten = mdb_tgt_awrite(t, as, buf, strlen(buf) + 1, addr);
return (nwritten > 0 ? nwritten - 1 : nwritten);
}
int
mdb_tgt_lookup_by_name(mdb_tgt_t *t, const char *obj,
const char *name, GElf_Sym *symp, mdb_syminfo_t *sip)
{
mdb_syminfo_t info;
GElf_Sym sym;
uint_t id;
if (name == NULL || t == NULL)
return (set_errno(EINVAL));
if (obj == MDB_TGT_OBJ_EVERY &&
mdb_gelf_symtab_lookup_by_name(mdb.m_prsym, name, &sym, &id) == 0) {
info.sym_table = MDB_TGT_PRVSYM;
info.sym_id = id;
goto found;
}
if (t->t_ops->t_lookup_by_name(t, obj, name, &sym, &info) == 0)
goto found;
return (-1);
found:
if (symp != NULL)
*symp = sym;
if (sip != NULL)
*sip = info;
return (0);
}
int
mdb_tgt_lookup_by_addr(mdb_tgt_t *t, uintptr_t addr, uint_t flags,
char *buf, size_t len, GElf_Sym *symp, mdb_syminfo_t *sip)
{
mdb_syminfo_t info;
GElf_Sym sym;
if (t == NULL)
return (set_errno(EINVAL));
if (t->t_ops->t_lookup_by_addr(t, addr, flags,
buf, len, &sym, &info) == 0) {
if (symp != NULL)
*symp = sym;
if (sip != NULL)
*sip = info;
return (0);
}
return (-1);
}
/*
* The mdb_tgt_lookup_by_scope function is a convenience routine for code that
* wants to look up a scoped symbol name such as "object`symbol". It is
* implemented as a simple wrapper around mdb_tgt_lookup_by_name. Note that
* we split on the *last* occurrence of "`", so the object name itself may
* contain additional scopes whose evaluation is left to the target. This
* allows targets to implement additional scopes, such as source files,
* function names, link map identifiers, etc.
*/
int
mdb_tgt_lookup_by_scope(mdb_tgt_t *t, const char *s, GElf_Sym *symp,
mdb_syminfo_t *sip)
{
const char *object = MDB_TGT_OBJ_EVERY;
const char *name = s;
char buf[MDB_TGT_SYM_NAMLEN];
if (t == NULL)
return (set_errno(EINVAL));
if (strchr(name, '`') != NULL) {
(void) strncpy(buf, s, sizeof (buf));
buf[sizeof (buf) - 1] = '\0';
name = buf;
if ((s = strrsplit(buf, '`')) != NULL) {
object = buf;
name = s;
if (*object == '\0')
return (set_errno(EMDB_NOOBJ));
if (*name == '\0')
return (set_errno(EMDB_NOSYM));
}
}
return (mdb_tgt_lookup_by_name(t, object, name, symp, sip));
}
int
mdb_tgt_symbol_iter(mdb_tgt_t *t, const char *obj, uint_t which,
uint_t type, mdb_tgt_sym_f *cb, void *p)
{
if ((which != MDB_TGT_SYMTAB && which != MDB_TGT_DYNSYM) ||
(type & ~(MDB_TGT_BIND_ANY | MDB_TGT_TYPE_ANY)) != 0)
return (set_errno(EINVAL));
return (t->t_ops->t_symbol_iter(t, obj, which, type, cb, p));
}
ssize_t
mdb_tgt_readsym(mdb_tgt_t *t, mdb_tgt_as_t as, void *buf, size_t nbytes,
const char *obj, const char *name)
{
GElf_Sym sym;
if (mdb_tgt_lookup_by_name(t, obj, name, &sym, NULL) == 0)
return (mdb_tgt_aread(t, as, buf, nbytes, sym.st_value));
return (-1);
}
ssize_t
mdb_tgt_writesym(mdb_tgt_t *t, mdb_tgt_as_t as, const void *buf,
size_t nbytes, const char *obj, const char *name)
{
GElf_Sym sym;
if (mdb_tgt_lookup_by_name(t, obj, name, &sym, NULL) == 0)
return (mdb_tgt_awrite(t, as, buf, nbytes, sym.st_value));
return (-1);
}
int
mdb_tgt_mapping_iter(mdb_tgt_t *t, mdb_tgt_map_f *cb, void *p)
{
return (t->t_ops->t_mapping_iter(t, cb, p));
}
int
mdb_tgt_object_iter(mdb_tgt_t *t, mdb_tgt_map_f *cb, void *p)
{
return (t->t_ops->t_object_iter(t, cb, p));
}
const mdb_map_t *
mdb_tgt_addr_to_map(mdb_tgt_t *t, uintptr_t addr)
{
return (t->t_ops->t_addr_to_map(t, addr));
}
const mdb_map_t *
mdb_tgt_name_to_map(mdb_tgt_t *t, const char *name)
{
return (t->t_ops->t_name_to_map(t, name));
}
struct ctf_file *
mdb_tgt_addr_to_ctf(mdb_tgt_t *t, uintptr_t addr)
{
return (t->t_ops->t_addr_to_ctf(t, addr));
}
struct ctf_file *
mdb_tgt_name_to_ctf(mdb_tgt_t *t, const char *name)
{
return (t->t_ops->t_name_to_ctf(t, name));
}
/*
* Return the latest target status. We just copy out our cached copy. The
* status only needs to change when the target is run, stepped, or continued.
*/
int
mdb_tgt_status(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
{
uint_t dstop = (t->t_status.st_flags & MDB_TGT_DSTOP);
uint_t istop = (t->t_status.st_flags & MDB_TGT_ISTOP);
uint_t state = t->t_status.st_state;
if (tsp == NULL)
return (set_errno(EINVAL));
/*
* If we're called with the address of the target's internal status,
* then call down to update it; otherwise copy out the saved status.
*/
if (tsp == &t->t_status && t->t_ops->t_status(t, &t->t_status) != 0)
return (-1); /* errno is set for us */
/*
* Assert that our state is valid before returning it. The state must
* be valid, and DSTOP and ISTOP cannot be set simultaneously. ISTOP
* is only valid when stopped. DSTOP is only valid when running or
* stopped. If any test fails, abort the debugger.
*/
if (state > MDB_TGT_LOST)
fail("invalid target state (%u)\n", state);
if (state != MDB_TGT_STOPPED && istop)
fail("target state is (%u) and ISTOP is set\n", state);
if (state != MDB_TGT_STOPPED && state != MDB_TGT_RUNNING && dstop)
fail("target state is (%u) and DSTOP is set\n", state);
if (istop && dstop)
fail("target has ISTOP and DSTOP set simultaneously\n");
if (tsp != &t->t_status)
bcopy(&t->t_status, tsp, sizeof (mdb_tgt_status_t));
return (0);
}
/*
* For the given sespec, scan its list of vespecs for ones that are marked
* temporary and delete them. We use the same method as vespec_delete below.
*/
/*ARGSUSED*/
void
mdb_tgt_sespec_prune_one(mdb_tgt_t *t, mdb_sespec_t *sep)
{
mdb_vespec_t *vep, *nvep;
for (vep = mdb_list_next(&sep->se_velist); vep; vep = nvep) {
nvep = mdb_list_next(vep);
if ((vep->ve_flags & (MDB_TGT_SPEC_DELETED |
MDB_TGT_SPEC_TEMPORARY)) == MDB_TGT_SPEC_TEMPORARY) {
vep->ve_flags |= MDB_TGT_SPEC_DELETED;
mdb_tgt_vespec_rele(t, vep);
}
}
}
/*
* Prune each sespec on the active list of temporary vespecs. This function
* is called, for example, after the target finishes a continue operation.
*/
void
mdb_tgt_sespec_prune_all(mdb_tgt_t *t)
{
mdb_sespec_t *sep, *nsep;
for (sep = mdb_list_next(&t->t_active); sep != NULL; sep = nsep) {
nsep = mdb_list_next(sep);
mdb_tgt_sespec_prune_one(t, sep);
}
}
/*
* Transition the given sespec to the IDLE state. We invoke the destructor,
* and then move the sespec from the active list to the idle list.
*/
void
mdb_tgt_sespec_idle_one(mdb_tgt_t *t, mdb_sespec_t *sep, int reason)
{
ASSERT(sep->se_state != MDB_TGT_SPEC_IDLE);
if (sep->se_state == MDB_TGT_SPEC_ARMED)
(void) sep->se_ops->se_disarm(t, sep);
sep->se_ops->se_dtor(t, sep);
sep->se_data = NULL;
sep->se_state = MDB_TGT_SPEC_IDLE;
sep->se_errno = reason;
mdb_list_delete(&t->t_active, sep);
mdb_list_append(&t->t_idle, sep);
mdb_tgt_sespec_prune_one(t, sep);
}
/*
* Transition each sespec on the active list to the IDLE state. This function
* is called, for example, after the target terminates execution.
*/
void
mdb_tgt_sespec_idle_all(mdb_tgt_t *t, int reason, int clear_matched)
{
mdb_sespec_t *sep, *nsep;
mdb_vespec_t *vep;
while ((sep = t->t_matched) != T_SE_END && clear_matched) {
for (vep = mdb_list_next(&sep->se_velist); vep != NULL; ) {
vep->ve_flags &= ~MDB_TGT_SPEC_MATCHED;
vep = mdb_list_next(vep);
}
t->t_matched = sep->se_matched;
sep->se_matched = NULL;
mdb_tgt_sespec_rele(t, sep);
}
for (sep = mdb_list_next(&t->t_active); sep != NULL; sep = nsep) {
nsep = mdb_list_next(sep);
mdb_tgt_sespec_idle_one(t, sep, reason);
}
}
/*
* Attempt to transition the given sespec from the IDLE to ACTIVE state. We
* do this by invoking se_ctor -- if this fails, we save the reason in se_errno
* and return -1 with errno set. One strange case we need to deal with here is
* the possibility that a given vespec is sitting on the idle list with its
* corresponding sespec, but it is actually a duplicate of another sespec on the
* active list. This can happen if the sespec is associated with a
* MDB_TGT_SPEC_DISABLED vespec that was just enabled, and is now ready to be
* activated. A more interesting reason this situation might arise is the case
* where a virtual address breakpoint is set at an address just mmap'ed by
* dlmopen. Since no symbol table information is available for this mapping
* yet, a pre-existing deferred symbolic breakpoint may already exist for this
* address, but it is on the idle list. When the symbol table is ready and the
* DLACTIVITY event occurs, we now discover that the virtual address obtained by
* evaluating the symbolic breakpoint matches the explicit virtual address of
* the active virtual breakpoint. To resolve this conflict in either case, we
* destroy the idle sespec, and attach its list of vespecs to the existing
* active sespec.
*/
int
mdb_tgt_sespec_activate_one(mdb_tgt_t *t, mdb_sespec_t *sep)
{
mdb_vespec_t *vep = mdb_list_next(&sep->se_velist);
mdb_vespec_t *nvep;
mdb_sespec_t *dup;
ASSERT(sep->se_state == MDB_TGT_SPEC_IDLE);
ASSERT(vep != NULL);
if (vep->ve_flags & MDB_TGT_SPEC_DISABLED)
return (0); /* cannot be activated while disabled bit set */
/*
* First search the active list for an existing, duplicate sespec to
* handle the special case described above.
*/
for (dup = mdb_list_next(&t->t_active); dup; dup = mdb_list_next(dup)) {
if (dup->se_ops == sep->se_ops &&
dup->se_ops->se_secmp(t, dup, vep->ve_args)) {
ASSERT(dup != sep);
break;
}
}
/*
* If a duplicate is found, destroy the existing, idle sespec, and
* attach all of its vespecs to the duplicate sespec.
*/
if (dup != NULL) {
for (vep = mdb_list_next(&sep->se_velist); vep; vep = nvep) {
mdb_dprintf(MDB_DBG_TGT, "merge [ %d ] to sespec %p\n",
vep->ve_id, (void *)dup);
if (dup->se_matched != NULL)
vep->ve_flags |= MDB_TGT_SPEC_MATCHED;
nvep = mdb_list_next(vep);
vep->ve_hits = 0;
mdb_list_delete(&sep->se_velist, vep);
mdb_tgt_sespec_rele(t, sep);
mdb_list_append(&dup->se_velist, vep);
mdb_tgt_sespec_hold(t, dup);
vep->ve_se = dup;
}
mdb_dprintf(MDB_DBG_TGT, "merged idle sespec %p with %p\n",
(void *)sep, (void *)dup);
return (0);
}
/*
* If no duplicate is found, call the sespec's constructor. If this
* is successful, move the sespec to the active list.
*/
if (sep->se_ops->se_ctor(t, sep, vep->ve_args) < 0) {
sep->se_errno = errno;
sep->se_data = NULL;
return (-1);
}
for (vep = mdb_list_next(&sep->se_velist); vep; vep = nvep) {
nvep = mdb_list_next(vep);
vep->ve_hits = 0;
}
mdb_list_delete(&t->t_idle, sep);
mdb_list_append(&t->t_active, sep);
sep->se_state = MDB_TGT_SPEC_ACTIVE;
sep->se_errno = 0;
return (0);
}
/*
* Transition each sespec on the idle list to the ACTIVE state. This function
* is called, for example, after the target's t_run() function returns. If
* the se_ctor() function fails, the specifier is not yet applicable; it will
* remain on the idle list and can be activated later.
*
* Returns 1 if there weren't any unexpected activation failures; 0 if there
* were.
*/
int
mdb_tgt_sespec_activate_all(mdb_tgt_t *t)
{
mdb_sespec_t *sep, *nsep;
int rc = 1;
for (sep = mdb_list_next(&t->t_idle); sep != NULL; sep = nsep) {
nsep = mdb_list_next(sep);
if (mdb_tgt_sespec_activate_one(t, sep) < 0 &&
sep->se_errno != EMDB_NOOBJ)
rc = 0;
}
return (rc);
}
/*
* Transition the given sespec to the ARMED state. Note that we attempt to
* re-arm sespecs previously in the ERROR state. If se_arm() fails the sespec
* transitions to the ERROR state but stays on the active list.
*/
void
mdb_tgt_sespec_arm_one(mdb_tgt_t *t, mdb_sespec_t *sep)
{
ASSERT(sep->se_state != MDB_TGT_SPEC_IDLE);
if (sep->se_state == MDB_TGT_SPEC_ARMED)
return; /* do not arm sespecs more than once */
if (sep->se_ops->se_arm(t, sep) == -1) {
sep->se_state = MDB_TGT_SPEC_ERROR;
sep->se_errno = errno;
} else {
sep->se_state = MDB_TGT_SPEC_ARMED;
sep->se_errno = 0;
}
}
/*
* Transition each sespec on the active list (except matched specs) to the
* ARMED state. This function is called prior to continuing the target.
*/
void
mdb_tgt_sespec_arm_all(mdb_tgt_t *t)
{
mdb_sespec_t *sep, *nsep;
for (sep = mdb_list_next(&t->t_active); sep != NULL; sep = nsep) {
nsep = mdb_list_next(sep);
if (sep->se_matched == NULL)
mdb_tgt_sespec_arm_one(t, sep);
}
}
/*
* Transition each sespec on the active list that is in the ARMED state to
* the ACTIVE state. If se_disarm() fails, the sespec is transitioned to
* the ERROR state instead, but left on the active list.
*/
static void
tgt_disarm_sespecs(mdb_tgt_t *t)
{
mdb_sespec_t *sep;
for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
if (sep->se_state != MDB_TGT_SPEC_ARMED)
continue; /* do not disarm if in ERROR state */
if (sep->se_ops->se_disarm(t, sep) == -1) {
sep->se_state = MDB_TGT_SPEC_ERROR;
sep->se_errno = errno;
} else {
sep->se_state = MDB_TGT_SPEC_ACTIVE;
sep->se_errno = 0;
}
}
}
/*
* Determine if the software event that triggered the most recent stop matches
* any of the active event specifiers. If 'all' is TRUE, we consider all
* sespecs in our search. If 'all' is FALSE, we only consider ARMED sespecs.
* If we successfully match an event, we add it to the t_matched list and
* place an additional hold on it.
*/
static mdb_sespec_t *
tgt_match_sespecs(mdb_tgt_t *t, int all)
{
mdb_sespec_t *sep;
for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
if (all == FALSE && sep->se_state != MDB_TGT_SPEC_ARMED)
continue; /* restrict search to ARMED sespecs */
if (sep->se_state != MDB_TGT_SPEC_ERROR &&
sep->se_ops->se_match(t, sep, &t->t_status)) {
mdb_dprintf(MDB_DBG_TGT, "match se %p\n", (void *)sep);
mdb_tgt_sespec_hold(t, sep);
sep->se_matched = t->t_matched;
t->t_matched = sep;
}
}
return (t->t_matched);
}
/*
* This function provides the low-level target continue algorithm. We proceed
* in three phases: (1) we arm the active sespecs, except the specs matched at
* the time we last stopped, (2) we call se_cont() on any matched sespecs to
* step over these event transitions, and then arm the corresponding sespecs,
* and (3) we call the appropriate low-level continue routine. Once the
* target stops again, we determine which sespecs were matched, and invoke the
* appropriate vespec callbacks and perform other vespec maintenance.
*/
static int
tgt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp,
int (*t_cont)(mdb_tgt_t *, mdb_tgt_status_t *))
{
mdb_var_t *hitv = mdb_nv_lookup(&mdb.m_nv, "hits");
uintptr_t pc = t->t_status.st_pc;
int error = 0;
mdb_sespec_t *sep, *nsep, *matched;
mdb_vespec_t *vep, *nvep;
uintptr_t addr;
uint_t cbits = 0; /* union of pending continue bits */
uint_t ncont = 0; /* # of callbacks that requested cont */
uint_t n = 0; /* # of callbacks */
/*
* If the target is undead, dead, or lost, we no longer allow continue.
* This effectively forces the user to use ::kill or ::run after death.
*/
if (t->t_status.st_state == MDB_TGT_UNDEAD)
return (set_errno(EMDB_TGTZOMB));
if (t->t_status.st_state == MDB_TGT_DEAD)
return (set_errno(EMDB_TGTCORE));
if (t->t_status.st_state == MDB_TGT_LOST)
return (set_errno(EMDB_TGTLOST));
/*
* If any of single-step, step-over, or step-out is pending, it takes
* precedence over an explicit or pending continue, because these are
* all different specialized forms of continue.
*/
if (t->t_flags & MDB_TGT_F_STEP)
t_cont = t->t_ops->t_step;
else if (t->t_flags & MDB_TGT_F_NEXT)
t_cont = t->t_ops->t_step;
else if (t->t_flags & MDB_TGT_F_STEP_OUT)
t_cont = t->t_ops->t_cont;
/*
* To handle step-over, we ask the target to find the address past the
* next control transfer instruction. If an address is found, we plant
* a temporary breakpoint there and continue; otherwise just step.
*/
if ((t->t_flags & MDB_TGT_F_NEXT) && !(t->t_flags & MDB_TGT_F_STEP)) {
if (t->t_ops->t_next(t, &addr) == -1 || mdb_tgt_add_vbrkpt(t,
addr, MDB_TGT_SPEC_HIDDEN | MDB_TGT_SPEC_TEMPORARY,
no_se_f, NULL) == 0) {
mdb_dprintf(MDB_DBG_TGT, "next falling back to step: "
"%s\n", mdb_strerror(errno));
} else
t_cont = t->t_ops->t_cont;
}
/*
* To handle step-out, we ask the target to find the return address of
* the current frame, plant a temporary breakpoint there, and continue.
*/
if (t->t_flags & MDB_TGT_F_STEP_OUT) {
if (t->t_ops->t_step_out(t, &addr) == -1)
return (-1); /* errno is set for us */
if (mdb_tgt_add_vbrkpt(t, addr, MDB_TGT_SPEC_HIDDEN |
MDB_TGT_SPEC_TEMPORARY, no_se_f, NULL) == 0)
return (-1); /* errno is set for us */
}
(void) mdb_signal_block(SIGHUP);
(void) mdb_signal_block(SIGTERM);
mdb_intr_disable();
t->t_flags &= ~T_CONT_BITS;
t->t_flags |= MDB_TGT_F_BUSY;
mdb_tgt_sespec_arm_all(t);
ASSERT(t->t_matched != NULL);
matched = t->t_matched;
t->t_matched = T_SE_END;
if (mdb.m_term != NULL)
IOP_SUSPEND(mdb.m_term);
/*
* Iterate over the matched sespec list, performing autostop processing
* and clearing the matched bit for each associated vespec. We then
* invoke each sespec's se_cont callback in order to continue past
* the corresponding event. If the matched list has more than one
* sespec, we assume that the se_cont callbacks are non-interfering.
*/
for (sep = matched; sep != T_SE_END; sep = sep->se_matched) {
for (vep = mdb_list_next(&sep->se_velist); vep != NULL; ) {
if ((vep->ve_flags & MDB_TGT_SPEC_AUTOSTOP) &&
(vep->ve_limit && vep->ve_hits == vep->ve_limit))
vep->ve_hits = 0;
vep->ve_flags &= ~MDB_TGT_SPEC_MATCHED;
vep = mdb_list_next(vep);
}
if (sep->se_ops->se_cont(t, sep, &t->t_status) == -1) {
error = errno ? errno : -1;
tgt_disarm_sespecs(t);
break;
}
if (!(t->t_status.st_flags & MDB_TGT_ISTOP)) {
tgt_disarm_sespecs(t);
if (t->t_status.st_state == MDB_TGT_UNDEAD)
mdb_tgt_sespec_idle_all(t, EMDB_TGTZOMB, TRUE);
else if (t->t_status.st_state == MDB_TGT_LOST)
mdb_tgt_sespec_idle_all(t, EMDB_TGTLOST, TRUE);
break;
}
}
/*
* Clear the se_matched field for each matched sespec, and drop the
* reference count since the sespec is no longer on the matched list.
*/
for (sep = matched; sep != T_SE_END; sep = nsep) {
nsep = sep->se_matched;
sep->se_matched = NULL;
mdb_tgt_sespec_rele(t, sep);
}
/*
* If the matched list was non-empty, see if we hit another event while
* performing se_cont() processing. If so, don't bother continuing any
* further. If not, arm the sespecs on the old matched list by calling
* mdb_tgt_sespec_arm_all() again and then continue by calling t_cont.
*/
if (matched != T_SE_END) {
if (error != 0 || !(t->t_status.st_flags & MDB_TGT_ISTOP))
goto out; /* abort now if se_cont() failed */
if ((t->t_matched = tgt_match_sespecs(t, FALSE)) != T_SE_END) {
tgt_disarm_sespecs(t);
goto out;
}
mdb_tgt_sespec_arm_all(t);
}
if (t_cont != t->t_ops->t_step || pc == t->t_status.st_pc) {
if (t_cont(t, &t->t_status) != 0)
error = errno ? errno : -1;
}
tgt_disarm_sespecs(t);
if (t->t_flags & MDB_TGT_F_UNLOAD)
longjmp(mdb.m_frame->f_pcb, MDB_ERR_QUIT);
if (t->t_status.st_state == MDB_TGT_UNDEAD)
mdb_tgt_sespec_idle_all(t, EMDB_TGTZOMB, TRUE);
else if (t->t_status.st_state == MDB_TGT_LOST)
mdb_tgt_sespec_idle_all(t, EMDB_TGTLOST, TRUE);
else if (t->t_status.st_flags & MDB_TGT_ISTOP)
t->t_matched = tgt_match_sespecs(t, TRUE);
out:
if (mdb.m_term != NULL)
IOP_RESUME(mdb.m_term);
(void) mdb_signal_unblock(SIGTERM);
(void) mdb_signal_unblock(SIGHUP);
mdb_intr_enable();
for (sep = t->t_matched; sep != T_SE_END; sep = sep->se_matched) {
/*
* When we invoke a ve_callback, it may in turn request that the
* target continue immediately after callback processing is
* complete. We only allow this to occur if *all* callbacks
* agree to continue. To implement this behavior, we keep a
* count (ncont) of such requests, and only apply the cumulative
* continue bits (cbits) to the target if ncont is equal to the
* total number of callbacks that are invoked (n).
*/
for (vep = mdb_list_next(&sep->se_velist);
vep != NULL; vep = nvep, n++) {
/*
* Place an extra hold on the current vespec and pick
* up the next pointer before invoking the callback: we
* must be prepared for the vespec to be deleted or
* moved to a different list by the callback.
*/
mdb_tgt_vespec_hold(t, vep);
nvep = mdb_list_next(vep);
vep->ve_flags |= MDB_TGT_SPEC_MATCHED;
vep->ve_hits++;
mdb_nv_set_value(mdb.m_dot, t->t_status.st_pc);
mdb_nv_set_value(hitv, vep->ve_hits);
ASSERT((t->t_flags & T_CONT_BITS) == 0);
vep->ve_callback(t, vep->ve_id, vep->ve_data);
ncont += (t->t_flags & T_CONT_BITS) != 0;
cbits |= (t->t_flags & T_CONT_BITS);
t->t_flags &= ~T_CONT_BITS;
if (vep->ve_limit && vep->ve_hits == vep->ve_limit) {
if (vep->ve_flags & MDB_TGT_SPEC_AUTODEL)
(void) mdb_tgt_vespec_delete(t,
vep->ve_id);
else if (vep->ve_flags & MDB_TGT_SPEC_AUTODIS)
(void) mdb_tgt_vespec_disable(t,
vep->ve_id);
}
if (vep->ve_limit && vep->ve_hits < vep->ve_limit) {
if (vep->ve_flags & MDB_TGT_SPEC_AUTOSTOP)
(void) mdb_tgt_continue(t, NULL);
}
mdb_tgt_vespec_rele(t, vep);
}
}
if (t->t_matched != T_SE_END && ncont == n)
t->t_flags |= cbits; /* apply continues (see above) */
mdb_tgt_sespec_prune_all(t);
t->t_status.st_flags &= ~MDB_TGT_BUSY;
t->t_flags &= ~MDB_TGT_F_BUSY;
if (tsp != NULL)
bcopy(&t->t_status, tsp, sizeof (mdb_tgt_status_t));
if (error != 0)
return (set_errno(error));
return (0);
}
/*
* This function is the common glue that connects the high-level target layer
* continue functions (e.g. step and cont below) with the low-level
* tgt_continue() function above. Since vespec callbacks may perform any
* actions, including attempting to continue the target itself, we must be
* prepared to be called while the target is still marked F_BUSY. In this
* case, we just set a pending bit and return. When we return from the call
* to tgt_continue() that made us busy into the tgt_request_continue() call
* that is still on the stack, we will loop around and call tgt_continue()
* again. This allows vespecs to continue the target without recursion.
*/
static int
tgt_request_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp, uint_t tflag,
int (*t_cont)(mdb_tgt_t *, mdb_tgt_status_t *))
{
mdb_tgt_spec_desc_t desc;
mdb_sespec_t *sep;
char buf[BUFSIZ];
int status;
if (t->t_flags & MDB_TGT_F_BUSY) {
t->t_flags |= tflag;
return (0);
}
do {
status = tgt_continue(t, tsp, t_cont);
} while (status == 0 && (t->t_flags & T_CONT_BITS));
if (status == 0) {
for (sep = t->t_matched; sep != T_SE_END;
sep = sep->se_matched) {
mdb_vespec_t *vep;
for (vep = mdb_list_next(&sep->se_velist); vep;
vep = mdb_list_next(vep)) {
if (vep->ve_flags & MDB_TGT_SPEC_SILENT)
continue;
warn("%s\n", sep->se_ops->se_info(t, sep,
vep, &desc, buf, sizeof (buf)));
}
}
mdb_callb_fire(MDB_CALLB_STCHG);
}
t->t_flags &= ~T_CONT_BITS;
return (status);
}
/*
* Restart target execution: we rely upon the underlying target implementation
* to do most of the work for us. In particular, we assume it will properly
* preserve the state of our event lists if the run fails for some reason,
* and that it will reset all events to the IDLE state if the run succeeds.
* If it is successful, we attempt to activate all of the idle sespecs. The
* t_run() operation is defined to leave the target stopped at the earliest
* possible point in execution, and then return control to the debugger,
* awaiting a step or continue operation to set it running again.
*/
int
mdb_tgt_run(mdb_tgt_t *t, int argc, const mdb_arg_t *argv)
{
int i;
for (i = 0; i < argc; i++) {
if (argv->a_type != MDB_TYPE_STRING)
return (set_errno(EINVAL));
}
if (t->t_ops->t_run(t, argc, argv) == -1)
return (-1); /* errno is set for us */
t->t_flags &= ~T_CONT_BITS;
(void) mdb_tgt_sespec_activate_all(t);
if (mdb.m_term != NULL)
IOP_CTL(mdb.m_term, MDB_IOC_CTTY, NULL);
return (0);
}
int
mdb_tgt_step(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
{
return (tgt_request_continue(t, tsp, MDB_TGT_F_STEP, t->t_ops->t_step));
}
int
mdb_tgt_step_out(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
{
t->t_flags |= MDB_TGT_F_STEP_OUT; /* set flag even if tgt not busy */
return (tgt_request_continue(t, tsp, 0, t->t_ops->t_cont));
}
int
mdb_tgt_next(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
{
t->t_flags |= MDB_TGT_F_NEXT; /* set flag even if tgt not busy */
return (tgt_request_continue(t, tsp, 0, t->t_ops->t_step));
}
int
mdb_tgt_continue(mdb_tgt_t *t, mdb_tgt_status_t *tsp)
{
return (tgt_request_continue(t, tsp, MDB_TGT_F_CONT, t->t_ops->t_cont));
}
int
mdb_tgt_signal(mdb_tgt_t *t, int sig)
{
return (t->t_ops->t_signal(t, sig));
}
void *
mdb_tgt_vespec_data(mdb_tgt_t *t, int vid)
{
mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, vid);
if (vep == NULL) {
(void) set_errno(EMDB_NOSESPEC);
return (NULL);
}
return (vep->ve_data);
}
/*
* Return a structured description and comment string for the given vespec.
* We fill in the common information from the vespec, and then call down to
* the underlying sespec to provide the comment string and modify any
* event type-specific information.
*/
char *
mdb_tgt_vespec_info(mdb_tgt_t *t, int vid, mdb_tgt_spec_desc_t *sp,
char *buf, size_t nbytes)
{
mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, vid);
mdb_tgt_spec_desc_t desc;
mdb_sespec_t *sep;
if (vep == NULL) {
if (sp != NULL)
bzero(sp, sizeof (mdb_tgt_spec_desc_t));
(void) set_errno(EMDB_NOSESPEC);
return (NULL);
}
if (sp == NULL)
sp = &desc;
sep = vep->ve_se;
sp->spec_id = vep->ve_id;
sp->spec_flags = vep->ve_flags;
sp->spec_hits = vep->ve_hits;
sp->spec_limit = vep->ve_limit;
sp->spec_state = sep->se_state;
sp->spec_errno = sep->se_errno;
sp->spec_base = 0;
sp->spec_size = 0;
sp->spec_data = vep->ve_data;
return (sep->se_ops->se_info(t, sep, vep, sp, buf, nbytes));
}
/*
* Qsort callback for sorting vespecs by VID, used below.
*/
static int
tgt_vespec_compare(const mdb_vespec_t **lp, const mdb_vespec_t **rp)
{
return ((*lp)->ve_id - (*rp)->ve_id);
}
/*
* Iterate over all vespecs and call the specified callback function with the
* corresponding VID and caller data pointer. We want the callback function
* to see a consistent, sorted snapshot of the vespecs, and allow the callback
* to take actions such as deleting the vespec itself, so we cannot simply
* iterate over the lists. Instead, we pre-allocate an array of vespec
* pointers, fill it in and place an additional hold on each vespec, and then
* sort it. After the callback has been executed on each vespec in the
* sorted array, we remove our hold and free the temporary array.
*/
int
mdb_tgt_vespec_iter(mdb_tgt_t *t, mdb_tgt_vespec_f *func, void *p)
{
mdb_vespec_t **veps, **vepp, **vend;
mdb_vespec_t *vep, *nvep;
mdb_sespec_t *sep;
uint_t vecnt = t->t_vecnt;
veps = mdb_alloc(sizeof (mdb_vespec_t *) * vecnt, UM_SLEEP);
vend = veps + vecnt;
vepp = veps;
for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
for (vep = mdb_list_next(&sep->se_velist); vep; vep = nvep) {
mdb_tgt_vespec_hold(t, vep);
nvep = mdb_list_next(vep);
*vepp++ = vep;
}
}
for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
for (vep = mdb_list_next(&sep->se_velist); vep; vep = nvep) {
mdb_tgt_vespec_hold(t, vep);
nvep = mdb_list_next(vep);
*vepp++ = vep;
}
}
if (vepp != vend) {
fail("target has %u vespecs on list but vecnt shows %u\n",
(uint_t)(vepp - veps), vecnt);
}
qsort(veps, vecnt, sizeof (mdb_vespec_t *),
(int (*)(const void *, const void *))tgt_vespec_compare);
for (vepp = veps; vepp < vend; vepp++) {
if (func(t, p, (*vepp)->ve_id, (*vepp)->ve_data) != 0)
break;
}
for (vepp = veps; vepp < vend; vepp++)
mdb_tgt_vespec_rele(t, *vepp);
mdb_free(veps, sizeof (mdb_vespec_t *) * vecnt);
return (0);
}
/*
* Reset the vespec flags, match limit, and callback data to the specified
* values. We silently correct invalid parameters, except for the VID.
* The caller is required to query the existing properties and pass back
* the existing values for any properties that should not be modified.
* If the callback data is modified, the caller is responsible for cleaning
* up any state associated with the previous value.
*/
int
mdb_tgt_vespec_modify(mdb_tgt_t *t, int id, uint_t flags,
uint_t limit, void *data)
{
mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, id);
if (vep == NULL)
return (set_errno(EMDB_NOSESPEC));
/*
* If the value of the MDB_TGT_SPEC_DISABLED bit is changing, call the
* appropriate vespec function to do the enable/disable work.
*/
if ((flags & MDB_TGT_SPEC_DISABLED) !=
(vep->ve_flags & MDB_TGT_SPEC_DISABLED)) {
if (flags & MDB_TGT_SPEC_DISABLED)
(void) mdb_tgt_vespec_disable(t, id);
else
(void) mdb_tgt_vespec_enable(t, id);
}
/*
* Make that only one MDB_TGT_SPEC_AUTO* bit is set in the new flags
* value: extra bits are cleared according to order of precedence.
*/
if (flags & MDB_TGT_SPEC_AUTOSTOP)
flags &= ~(MDB_TGT_SPEC_AUTODEL | MDB_TGT_SPEC_AUTODIS);
else if (flags & MDB_TGT_SPEC_AUTODEL)
flags &= ~MDB_TGT_SPEC_AUTODIS;
/*
* The TEMPORARY property always takes precedence over STICKY.
*/
if (flags & MDB_TGT_SPEC_TEMPORARY)
flags &= ~MDB_TGT_SPEC_STICKY;
/*
* If any MDB_TGT_SPEC_AUTO* bits are changing, reset the hit count
* back to zero and clear all of the old auto bits.
*/
if ((flags & T_AUTO_BITS) != (vep->ve_flags & T_AUTO_BITS)) {
vep->ve_flags &= ~T_AUTO_BITS;
vep->ve_hits = 0;
}
vep->ve_flags = (vep->ve_flags & T_IMPL_BITS) | (flags & ~T_IMPL_BITS);
vep->ve_data = data;
/*
* If any MDB_TGT_SPEC_AUTO* flags are set, make sure the limit is at
* least one. If none are set, reset it back to zero.
*/
if (vep->ve_flags & T_AUTO_BITS)
vep->ve_limit = MAX(limit, 1);
else
vep->ve_limit = 0;
/*
* As a convenience, we allow the caller to specify SPEC_DELETED in
* the flags field as indication that the event should be deleted.
*/
if (flags & MDB_TGT_SPEC_DELETED)
(void) mdb_tgt_vespec_delete(t, id);
return (0);
}
/*
* Remove the user disabled bit from the specified vespec, and attempt to
* activate the underlying sespec and move it to the active list if possible.
*/
int
mdb_tgt_vespec_enable(mdb_tgt_t *t, int id)
{
mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, id);
if (vep == NULL)
return (set_errno(EMDB_NOSESPEC));
if (vep->ve_flags & MDB_TGT_SPEC_DISABLED) {
ASSERT(mdb_list_next(vep) == NULL);
vep->ve_flags &= ~MDB_TGT_SPEC_DISABLED;
if (mdb_tgt_sespec_activate_one(t, vep->ve_se) < 0)
return (-1); /* errno is set for us */
}
return (0);
}
/*
* Set the user disabled bit on the specified vespec, and move it to the idle
* list. If the vespec is not alone with its sespec or if it is a currently
* matched event, we must always create a new idle sespec and move the vespec
* there. If the vespec was alone and active, we can simply idle the sespec.
*/
int
mdb_tgt_vespec_disable(mdb_tgt_t *t, int id)
{
mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, id);
mdb_sespec_t *sep;
if (vep == NULL)
return (set_errno(EMDB_NOSESPEC));
if (vep->ve_flags & MDB_TGT_SPEC_DISABLED)
return (0); /* already disabled */
if (mdb_list_prev(vep) != NULL || mdb_list_next(vep) != NULL ||
vep->ve_se->se_matched != NULL) {
sep = mdb_tgt_sespec_insert(t, vep->ve_se->se_ops, &t->t_idle);
mdb_list_delete(&vep->ve_se->se_velist, vep);
mdb_tgt_sespec_rele(t, vep->ve_se);
mdb_list_append(&sep->se_velist, vep);
mdb_tgt_sespec_hold(t, sep);
vep->ve_flags &= ~MDB_TGT_SPEC_MATCHED;
vep->ve_se = sep;
} else if (vep->ve_se->se_state != MDB_TGT_SPEC_IDLE)
mdb_tgt_sespec_idle_one(t, vep->ve_se, EMDB_SPECDIS);
vep->ve_flags |= MDB_TGT_SPEC_DISABLED;
return (0);
}
/*
* Delete the given vespec. We use the MDB_TGT_SPEC_DELETED flag to ensure that
* multiple calls to mdb_tgt_vespec_delete to not attempt to decrement the
* reference count on the vespec more than once. This is because the vespec
* may remain referenced if it is currently held by another routine (e.g.
* vespec_iter), and so the user could attempt to delete it more than once
* since it reference count will be >= 2 prior to the first delete call.
*/
int
mdb_tgt_vespec_delete(mdb_tgt_t *t, int id)
{
mdb_vespec_t *vep = mdb_tgt_vespec_lookup(t, id);
if (vep == NULL)
return (set_errno(EMDB_NOSESPEC));
if (vep->ve_flags & MDB_TGT_SPEC_DELETED)
return (set_errno(EBUSY));
vep->ve_flags |= MDB_TGT_SPEC_DELETED;
mdb_tgt_vespec_rele(t, vep);
return (0);
}
int
mdb_tgt_add_vbrkpt(mdb_tgt_t *t, uintptr_t addr,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
return (t->t_ops->t_add_vbrkpt(t, addr, spec_flags, func, p));
}
int
mdb_tgt_add_sbrkpt(mdb_tgt_t *t, const char *symbol,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
return (t->t_ops->t_add_sbrkpt(t, symbol, spec_flags, func, p));
}
int
mdb_tgt_add_pwapt(mdb_tgt_t *t, physaddr_t pa, size_t n, uint_t flags,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
if ((flags & ~MDB_TGT_WA_RWX) || flags == 0) {
(void) set_errno(EINVAL);
return (0);
}
if (pa + n < pa) {
(void) set_errno(EMDB_WPRANGE);
return (0);
}
return (t->t_ops->t_add_pwapt(t, pa, n, flags, spec_flags, func, p));
}
int
mdb_tgt_add_vwapt(mdb_tgt_t *t, uintptr_t va, size_t n, uint_t flags,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
if ((flags & ~MDB_TGT_WA_RWX) || flags == 0) {
(void) set_errno(EINVAL);
return (0);
}
if (va + n < va) {
(void) set_errno(EMDB_WPRANGE);
return (0);
}
return (t->t_ops->t_add_vwapt(t, va, n, flags, spec_flags, func, p));
}
int
mdb_tgt_add_iowapt(mdb_tgt_t *t, uintptr_t addr, size_t n, uint_t flags,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
if ((flags & ~MDB_TGT_WA_RWX) || flags == 0) {
(void) set_errno(EINVAL);
return (0);
}
if (addr + n < addr) {
(void) set_errno(EMDB_WPRANGE);
return (0);
}
return (t->t_ops->t_add_iowapt(t, addr, n, flags, spec_flags, func, p));
}
int
mdb_tgt_add_sysenter(mdb_tgt_t *t, int sysnum,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
return (t->t_ops->t_add_sysenter(t, sysnum, spec_flags, func, p));
}
int
mdb_tgt_add_sysexit(mdb_tgt_t *t, int sysnum,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
return (t->t_ops->t_add_sysexit(t, sysnum, spec_flags, func, p));
}
int
mdb_tgt_add_signal(mdb_tgt_t *t, int sig,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
return (t->t_ops->t_add_signal(t, sig, spec_flags, func, p));
}
int
mdb_tgt_add_fault(mdb_tgt_t *t, int flt,
int spec_flags, mdb_tgt_se_f *func, void *p)
{
return (t->t_ops->t_add_fault(t, flt, spec_flags, func, p));
}
int
mdb_tgt_getareg(mdb_tgt_t *t, mdb_tgt_tid_t tid,
const char *rname, mdb_tgt_reg_t *rp)
{
return (t->t_ops->t_getareg(t, tid, rname, rp));
}
int
mdb_tgt_putareg(mdb_tgt_t *t, mdb_tgt_tid_t tid,
const char *rname, mdb_tgt_reg_t r)
{
return (t->t_ops->t_putareg(t, tid, rname, r));
}
int
mdb_tgt_stack_iter(mdb_tgt_t *t, const mdb_tgt_gregset_t *gregs,
mdb_tgt_stack_f *cb, void *p)
{
return (t->t_ops->t_stack_iter(t, gregs, cb, p));
}
int
mdb_tgt_xdata_iter(mdb_tgt_t *t, mdb_tgt_xdata_f *func, void *private)
{
mdb_xdata_t *xdp;
for (xdp = mdb_list_next(&t->t_xdlist); xdp; xdp = mdb_list_next(xdp)) {
if (func(private, xdp->xd_name, xdp->xd_desc,
xdp->xd_copy(t, NULL, 0)) != 0)
break;
}
return (0);
}
ssize_t
mdb_tgt_getxdata(mdb_tgt_t *t, const char *name, void *buf, size_t nbytes)
{
mdb_xdata_t *xdp;
for (xdp = mdb_list_next(&t->t_xdlist); xdp; xdp = mdb_list_next(xdp)) {
if (strcmp(xdp->xd_name, name) == 0)
return (xdp->xd_copy(t, buf, nbytes));
}
return (set_errno(ENODATA));
}
long
mdb_tgt_notsup()
{
return (set_errno(EMDB_TGTNOTSUP));
}
void *
mdb_tgt_null()
{
(void) set_errno(EMDB_TGTNOTSUP);
return (NULL);
}
long
mdb_tgt_nop()
{
return (0L);
}
int
mdb_tgt_xdata_insert(mdb_tgt_t *t, const char *name, const char *desc,
ssize_t (*copy)(mdb_tgt_t *, void *, size_t))
{
mdb_xdata_t *xdp;
for (xdp = mdb_list_next(&t->t_xdlist); xdp; xdp = mdb_list_next(xdp)) {
if (strcmp(xdp->xd_name, name) == 0)
return (set_errno(EMDB_XDEXISTS));
}
xdp = mdb_alloc(sizeof (mdb_xdata_t), UM_SLEEP);
mdb_list_append(&t->t_xdlist, xdp);
xdp->xd_name = name;
xdp->xd_desc = desc;
xdp->xd_copy = copy;
return (0);
}
int
mdb_tgt_xdata_delete(mdb_tgt_t *t, const char *name)
{
mdb_xdata_t *xdp;
for (xdp = mdb_list_next(&t->t_xdlist); xdp; xdp = mdb_list_next(xdp)) {
if (strcmp(xdp->xd_name, name) == 0) {
mdb_list_delete(&t->t_xdlist, xdp);
mdb_free(xdp, sizeof (mdb_xdata_t));
return (0);
}
}
return (set_errno(EMDB_NOXD));
}
int
mdb_tgt_sym_match(const GElf_Sym *sym, uint_t mask)
{
#if STT_NUM != (STT_TLS + 1)
#error "STT_NUM has grown. update mdb_tgt_sym_match()"
#endif
uchar_t s_bind = GELF_ST_BIND(sym->st_info);
uchar_t s_type = GELF_ST_TYPE(sym->st_info);
/*
* In case you haven't already guessed, this relies on the bitmask
* used by <mdb/mdb_target.h> and <libproc.h> for encoding symbol
* type and binding matching the order of STB and STT constants
* in <sys/elf.h>. Changes to ELF must maintain binary
* compatibility, so I think this is reasonably fair game.
*/
if (s_bind < STB_NUM && s_type < STT_NUM) {
uint_t type = (1 << (s_type + 8)) | (1 << s_bind);
return ((type & ~mask) == 0);
}
return (0); /* Unknown binding or type; fail to match */
}
void
mdb_tgt_elf_export(mdb_gelf_file_t *gf)
{
GElf_Xword d = 0, t = 0;
GElf_Addr b = 0, e = 0;
uint32_t m = 0;
mdb_var_t *v;
/*
* Reset legacy adb variables based on the specified ELF object file
* provided by the target. We define these variables:
*
* b - the address of the data segment (first writeable Phdr)
* d - the size of the data segment
* e - the address of the entry point
* m - the magic number identifying the file
* t - the address of the text segment (first executable Phdr)
*/
if (gf != NULL) {
const GElf_Phdr *text = NULL, *data = NULL;
size_t i;
e = gf->gf_ehdr.e_entry;
bcopy(&gf->gf_ehdr.e_ident[EI_MAG0], &m, sizeof (m));
for (i = 0; i < gf->gf_npload; i++) {
if (text == NULL && (gf->gf_phdrs[i].p_flags & PF_X))
text = &gf->gf_phdrs[i];
if (data == NULL && (gf->gf_phdrs[i].p_flags & PF_W))
data = &gf->gf_phdrs[i];
}
if (text != NULL)
t = text->p_memsz;
if (data != NULL) {
b = data->p_vaddr;
d = data->p_memsz;
}
}
if ((v = mdb_nv_lookup(&mdb.m_nv, "b")) != NULL)
mdb_nv_set_value(v, b);
if ((v = mdb_nv_lookup(&mdb.m_nv, "d")) != NULL)
mdb_nv_set_value(v, d);
if ((v = mdb_nv_lookup(&mdb.m_nv, "e")) != NULL)
mdb_nv_set_value(v, e);
if ((v = mdb_nv_lookup(&mdb.m_nv, "m")) != NULL)
mdb_nv_set_value(v, m);
if ((v = mdb_nv_lookup(&mdb.m_nv, "t")) != NULL)
mdb_nv_set_value(v, t);
}
/*ARGSUSED*/
void
mdb_tgt_sespec_hold(mdb_tgt_t *t, mdb_sespec_t *sep)
{
sep->se_refs++;
ASSERT(sep->se_refs != 0);
}
void
mdb_tgt_sespec_rele(mdb_tgt_t *t, mdb_sespec_t *sep)
{
ASSERT(sep->se_refs != 0);
if (--sep->se_refs == 0) {
mdb_dprintf(MDB_DBG_TGT, "destroying sespec %p\n", (void *)sep);
ASSERT(mdb_list_next(&sep->se_velist) == NULL);
if (sep->se_state != MDB_TGT_SPEC_IDLE) {
sep->se_ops->se_dtor(t, sep);
mdb_list_delete(&t->t_active, sep);
} else
mdb_list_delete(&t->t_idle, sep);
mdb_free(sep, sizeof (mdb_sespec_t));
}
}
mdb_sespec_t *
mdb_tgt_sespec_insert(mdb_tgt_t *t, const mdb_se_ops_t *ops, mdb_list_t *list)
{
mdb_sespec_t *sep = mdb_zalloc(sizeof (mdb_sespec_t), UM_SLEEP);
if (list == &t->t_active)
sep->se_state = MDB_TGT_SPEC_ACTIVE;
else
sep->se_state = MDB_TGT_SPEC_IDLE;
mdb_list_append(list, sep);
sep->se_ops = ops;
return (sep);
}
mdb_sespec_t *
mdb_tgt_sespec_lookup_active(mdb_tgt_t *t, const mdb_se_ops_t *ops, void *args)
{
mdb_sespec_t *sep;
for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
if (sep->se_ops == ops && sep->se_ops->se_secmp(t, sep, args))
break;
}
return (sep);
}
mdb_sespec_t *
mdb_tgt_sespec_lookup_idle(mdb_tgt_t *t, const mdb_se_ops_t *ops, void *args)
{
mdb_sespec_t *sep;
for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
if (sep->se_ops == ops && sep->se_ops->se_vecmp(t,
mdb_list_next(&sep->se_velist), args))
break;
}
return (sep);
}
/*ARGSUSED*/
void
mdb_tgt_vespec_hold(mdb_tgt_t *t, mdb_vespec_t *vep)
{
vep->ve_refs++;
ASSERT(vep->ve_refs != 0);
}
void
mdb_tgt_vespec_rele(mdb_tgt_t *t, mdb_vespec_t *vep)
{
ASSERT(vep->ve_refs != 0);
if (--vep->ve_refs == 0) {
/*
* Remove this vespec from the sespec's velist and decrement
* the reference count on the sespec.
*/
mdb_list_delete(&vep->ve_se->se_velist, vep);
mdb_tgt_sespec_rele(t, vep->ve_se);
/*
* If we are deleting the most recently assigned VID, reset
* t_vepos or t_veneg as appropriate to re-use that number.
* This could be enhanced to re-use any free number by
* maintaining a bitmap or hash of the allocated IDs.
*/
if (vep->ve_id > 0 && t->t_vepos == vep->ve_id + 1)
t->t_vepos = vep->ve_id;
else if (vep->ve_id < 0 && t->t_veneg == -vep->ve_id + 1)
t->t_veneg = -vep->ve_id;
/*
* Call the destructor to clean up ve_args, and then free
* the actual vespec structure.
*/
vep->ve_dtor(vep);
mdb_free(vep, sizeof (mdb_vespec_t));
ASSERT(t->t_vecnt != 0);
t->t_vecnt--;
}
}
int
mdb_tgt_vespec_insert(mdb_tgt_t *t, const mdb_se_ops_t *ops, int flags,
mdb_tgt_se_f *func, void *data, void *args, void (*dtor)(mdb_vespec_t *))
{
mdb_vespec_t *vep = mdb_zalloc(sizeof (mdb_vespec_t), UM_SLEEP);
int id, mult, *seqp;
mdb_sespec_t *sep;
/*
* Make that only one MDB_TGT_SPEC_AUTO* bit is set in the new flags
* value: extra bits are cleared according to order of precedence.
*/
if (flags & MDB_TGT_SPEC_AUTOSTOP)
flags &= ~(MDB_TGT_SPEC_AUTODEL | MDB_TGT_SPEC_AUTODIS);
else if (flags & MDB_TGT_SPEC_AUTODEL)
flags &= ~MDB_TGT_SPEC_AUTODIS;
/*
* The TEMPORARY property always takes precedence over STICKY.
*/
if (flags & MDB_TGT_SPEC_TEMPORARY)
flags &= ~MDB_TGT_SPEC_STICKY;
/*
* Find a matching sespec or create a new one on the appropriate list.
* We always create a new sespec if the vespec is created disabled.
*/
if (flags & MDB_TGT_SPEC_DISABLED)
sep = mdb_tgt_sespec_insert(t, ops, &t->t_idle);
else if ((sep = mdb_tgt_sespec_lookup_active(t, ops, args)) == NULL &&
(sep = mdb_tgt_sespec_lookup_idle(t, ops, args)) == NULL)
sep = mdb_tgt_sespec_insert(t, ops, &t->t_active);
/*
* Generate a new ID for the vespec. Increasing positive integers are
* assigned to visible vespecs; decreasing negative integers are
* assigned to hidden vespecs. The target saves our most recent choice.
*/
if (flags & MDB_TGT_SPEC_INTERNAL) {
seqp = &t->t_veneg;
mult = -1;
} else {
seqp = &t->t_vepos;
mult = 1;
}
id = *seqp;
while (mdb_tgt_vespec_lookup(t, id * mult) != NULL)
id = MAX(id + 1, 1);
*seqp = MAX(id + 1, 1);
vep->ve_id = id * mult;
vep->ve_flags = flags & ~(MDB_TGT_SPEC_MATCHED | MDB_TGT_SPEC_DELETED);
vep->ve_se = sep;
vep->ve_callback = func;
vep->ve_data = data;
vep->ve_args = args;
vep->ve_dtor = dtor;
mdb_list_append(&sep->se_velist, vep);
mdb_tgt_sespec_hold(t, sep);
mdb_tgt_vespec_hold(t, vep);
t->t_vecnt++;
/*
* If this vespec is the first reference to the sespec and it's active,
* then it is newly created and we should attempt to initialize it.
* If se_ctor fails, then move the sespec back to the idle list.
*/
if (sep->se_refs == 1 && sep->se_state == MDB_TGT_SPEC_ACTIVE &&
sep->se_ops->se_ctor(t, sep, vep->ve_args) == -1) {
mdb_list_delete(&t->t_active, sep);
mdb_list_append(&t->t_idle, sep);
sep->se_state = MDB_TGT_SPEC_IDLE;
sep->se_errno = errno;
sep->se_data = NULL;
}
/*
* If the sespec is active and the target is currently running (because
* we grabbed it using PGRAB_NOSTOP), then go ahead and attempt to arm
* the sespec so it will take effect immediately.
*/
if (sep->se_state == MDB_TGT_SPEC_ACTIVE &&
t->t_status.st_state == MDB_TGT_RUNNING)
mdb_tgt_sespec_arm_one(t, sep);
mdb_dprintf(MDB_DBG_TGT, "inserted [ %d ] sep=%p refs=%u state=%d\n",
vep->ve_id, (void *)sep, sep->se_refs, sep->se_state);
return (vep->ve_id);
}
/*
* Search the target's active, idle, and disabled lists for the vespec matching
* the specified VID, and return a pointer to it, or NULL if no match is found.
*/
mdb_vespec_t *
mdb_tgt_vespec_lookup(mdb_tgt_t *t, int vid)
{
mdb_sespec_t *sep;
mdb_vespec_t *vep;
if (vid == 0)
return (NULL); /* 0 is never a valid VID */
for (sep = mdb_list_next(&t->t_active); sep; sep = mdb_list_next(sep)) {
for (vep = mdb_list_next(&sep->se_velist); vep;
vep = mdb_list_next(vep)) {
if (vep->ve_id == vid)
return (vep);
}
}
for (sep = mdb_list_next(&t->t_idle); sep; sep = mdb_list_next(sep)) {
for (vep = mdb_list_next(&sep->se_velist); vep;
vep = mdb_list_next(vep)) {
if (vep->ve_id == vid)
return (vep);
}
}
return (NULL);
}
/*ARGSUSED*/
void
no_ve_dtor(mdb_vespec_t *vep)
{
/* default destructor does nothing */
}
/*ARGSUSED*/
void
no_se_f(mdb_tgt_t *t, int vid, void *data)
{
/* default callback does nothing */
}
/*ARGSUSED*/
void
no_se_dtor(mdb_tgt_t *t, mdb_sespec_t *sep)
{
/* default destructor does nothing */
}
/*ARGSUSED*/
int
no_se_secmp(mdb_tgt_t *t, mdb_sespec_t *sep, void *args)
{
return (sep->se_data == args);
}
/*ARGSUSED*/
int
no_se_vecmp(mdb_tgt_t *t, mdb_vespec_t *vep, void *args)
{
return (vep->ve_args == args);
}
/*ARGSUSED*/
int
no_se_arm(mdb_tgt_t *t, mdb_sespec_t *sep)
{
return (0); /* return success */
}
/*ARGSUSED*/
int
no_se_disarm(mdb_tgt_t *t, mdb_sespec_t *sep)
{
return (0); /* return success */
}
/*ARGSUSED*/
int
no_se_cont(mdb_tgt_t *t, mdb_sespec_t *sep, mdb_tgt_status_t *tsp)
{
if (tsp != &t->t_status)
bcopy(&t->t_status, tsp, sizeof (mdb_tgt_status_t));
return (0); /* return success */
}
int
mdb_tgt_register_dcmds(mdb_tgt_t *t, const mdb_dcmd_t *dcp, int flags)
{
int fail = 0;
for (; dcp->dc_name != NULL; dcp++) {
if (mdb_module_add_dcmd(t->t_module, dcp, flags) == -1) {
warn("failed to add dcmd %s", dcp->dc_name);
fail++;
}
}
return (fail > 0 ? -1 : 0);
}
int
mdb_tgt_register_walkers(mdb_tgt_t *t, const mdb_walker_t *wp, int flags)
{
int fail = 0;
for (; wp->walk_name != NULL; wp++) {
if (mdb_module_add_walker(t->t_module, wp, flags) == -1) {
warn("failed to add walk %s", wp->walk_name);
fail++;
}
}
return (fail > 0 ? -1 : 0);
}
void
mdb_tgt_register_regvars(mdb_tgt_t *t, const mdb_tgt_regdesc_t *rdp,
const mdb_nv_disc_t *disc, int flags)
{
for (; rdp->rd_name != NULL; rdp++) {
if (!(rdp->rd_flags & MDB_TGT_R_EXPORT))
continue; /* Don't export register as a variable */
if (rdp->rd_flags & MDB_TGT_R_RDONLY)
flags |= MDB_NV_RDONLY;
(void) mdb_nv_insert(&mdb.m_nv, rdp->rd_name, disc,
(uintptr_t)t, MDB_NV_PERSIST | flags);
}
}
|