1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
|
/*
* 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 2010 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*
* Portions Copyright 2007 Chad Mynhier
* Copyright 2012 DEY Storage Systems, Inc. All rights reserved.
* Copyright (c) 2013 by Delphix. All rights reserved.
* Copyright 2015, Joyent, Inc.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <fcntl.h>
#include <string.h>
#include <strings.h>
#include <memory.h>
#include <errno.h>
#include <dirent.h>
#include <limits.h>
#include <signal.h>
#include <atomic.h>
#include <zone.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <sys/param.h>
#include <sys/stack.h>
#include <sys/fault.h>
#include <sys/syscall.h>
#include <sys/sysmacros.h>
#include <sys/systeminfo.h>
#include <sys/secflags.h>
#include "libproc.h"
#include "Pcontrol.h"
#include "Putil.h"
#include "P32ton.h"
int _libproc_debug; /* set non-zero to enable debugging printfs */
int _libproc_no_qsort; /* set non-zero to inhibit sorting */
/* of symbol tables */
int _libproc_incore_elf; /* only use in-core elf data */
sigset_t blockable_sigs; /* signals to block when we need to be safe */
static int minfd; /* minimum file descriptor returned by dupfd(fd, 0) */
char procfs_path[PATH_MAX] = "/proc";
/*
* Function prototypes for static routines in this module.
*/
static void deadcheck(struct ps_prochandle *);
static void restore_tracing_flags(struct ps_prochandle *);
static void Lfree_internal(struct ps_prochandle *, struct ps_lwphandle *);
static prheader_t *read_lfile(struct ps_prochandle *, const char *);
/*
* Ops vector functions for live processes.
*/
/*ARGSUSED*/
static ssize_t
Pread_live(struct ps_prochandle *P, void *buf, size_t n, uintptr_t addr,
void *data)
{
return (pread(P->asfd, buf, n, (off_t)addr));
}
/*ARGSUSED*/
static ssize_t
Pwrite_live(struct ps_prochandle *P, const void *buf, size_t n, uintptr_t addr,
void *data)
{
return (pwrite(P->asfd, buf, n, (off_t)addr));
}
/*ARGSUSED*/
static int
Pread_maps_live(struct ps_prochandle *P, prmap_t **Pmapp, ssize_t *nmapp,
void *data)
{
char mapfile[PATH_MAX];
int mapfd;
struct stat statb;
ssize_t nmap;
prmap_t *Pmap = NULL;
(void) snprintf(mapfile, sizeof (mapfile), "%s/%d/map",
procfs_path, (int)P->pid);
if ((mapfd = open(mapfile, O_RDONLY)) < 0 ||
fstat(mapfd, &statb) != 0 ||
statb.st_size < sizeof (prmap_t) ||
(Pmap = malloc(statb.st_size)) == NULL ||
(nmap = pread(mapfd, Pmap, statb.st_size, 0L)) <= 0 ||
(nmap /= sizeof (prmap_t)) == 0) {
if (Pmap != NULL)
free(Pmap);
if (mapfd >= 0)
(void) close(mapfd);
Preset_maps(P); /* utter failure; destroy tables */
return (-1);
}
(void) close(mapfd);
*Pmapp = Pmap;
*nmapp = nmap;
return (0);
}
/*ARGSUSED*/
static void
Pread_aux_live(struct ps_prochandle *P, auxv_t **auxvp, int *nauxp, void *data)
{
char auxfile[64];
int fd;
struct stat statb;
auxv_t *auxv;
ssize_t naux;
(void) snprintf(auxfile, sizeof (auxfile), "%s/%d/auxv",
procfs_path, (int)P->pid);
if ((fd = open(auxfile, O_RDONLY)) < 0) {
dprintf("%s: failed to open %s: %s\n",
__func__, auxfile, strerror(errno));
return;
}
if (fstat(fd, &statb) == 0 &&
statb.st_size >= sizeof (auxv_t) &&
(auxv = malloc(statb.st_size + sizeof (auxv_t))) != NULL) {
if ((naux = read(fd, auxv, statb.st_size)) < 0 ||
(naux /= sizeof (auxv_t)) < 1) {
dprintf("%s: read failed: %s\n",
__func__, strerror(errno));
free(auxv);
} else {
auxv[naux].a_type = AT_NULL;
auxv[naux].a_un.a_val = 0L;
*auxvp = auxv;
*nauxp = (int)naux;
}
}
(void) close(fd);
}
/*ARGSUSED*/
static int
Pcred_live(struct ps_prochandle *P, prcred_t *pcrp, int ngroups, void *data)
{
return (proc_get_cred(P->pid, pcrp, ngroups));
}
/* ARGSUSED */
static int
Psecflags_live(struct ps_prochandle *P, prsecflags_t **psf, void *data)
{
return (proc_get_secflags(P->pid, psf));
}
/*ARGSUSED*/
static int
Ppriv_live(struct ps_prochandle *P, prpriv_t **pprv, void *data)
{
prpriv_t *pp;
pp = proc_get_priv(P->pid);
if (pp == NULL) {
return (-1);
}
*pprv = pp;
return (0);
}
/*ARGSUSED*/
static const psinfo_t *
Ppsinfo_live(struct ps_prochandle *P, psinfo_t *psinfo, void *data)
{
if (proc_get_psinfo(P->pid, psinfo) == -1)
return (NULL);
return (psinfo);
}
/*ARGSUSED*/
static prheader_t *
Plstatus_live(struct ps_prochandle *P, void *data)
{
return (read_lfile(P, "lstatus"));
}
/*ARGSUSED*/
static prheader_t *
Plpsinfo_live(struct ps_prochandle *P, void *data)
{
return (read_lfile(P, "lpsinfo"));
}
/*ARGSUSED*/
static char *
Pplatform_live(struct ps_prochandle *P, char *s, size_t n, void *data)
{
if (sysinfo(SI_PLATFORM, s, n) == -1)
return (NULL);
return (s);
}
/*ARGSUSED*/
static int
Puname_live(struct ps_prochandle *P, struct utsname *u, void *data)
{
return (uname(u));
}
/*ARGSUSED*/
static char *
Pzonename_live(struct ps_prochandle *P, char *s, size_t n, void *data)
{
if (getzonenamebyid(P->status.pr_zoneid, s, n) < 0)
return (NULL);
s[n - 1] = '\0';
return (s);
}
/*
* Callback function for Pfindexec(). We return a match if we can stat the
* suggested pathname and confirm its device and inode number match our
* previous information about the /proc/<pid>/object/a.out file.
*/
static int
stat_exec(const char *path, void *arg)
{
struct stat64 *stp = arg;
struct stat64 st;
return (stat64(path, &st) == 0 && S_ISREG(st.st_mode) &&
stp->st_dev == st.st_dev && stp->st_ino == st.st_ino);
}
/*ARGSUSED*/
static char *
Pexecname_live(struct ps_prochandle *P, char *buf, size_t buflen, void *data)
{
char exec_name[PATH_MAX];
char cwd[PATH_MAX];
char proc_cwd[64];
struct stat64 st;
int ret;
/*
* Try to get the path information first.
*/
(void) snprintf(exec_name, sizeof (exec_name),
"%s/%d/path/a.out", procfs_path, (int)P->pid);
if ((ret = readlink(exec_name, buf, buflen - 1)) > 0) {
buf[ret] = '\0';
(void) Pfindobj(P, buf, buf, buflen);
return (buf);
}
/*
* Stat the executable file so we can compare Pfindexec's
* suggestions to the actual device and inode number.
*/
(void) snprintf(exec_name, sizeof (exec_name),
"%s/%d/object/a.out", procfs_path, (int)P->pid);
if (stat64(exec_name, &st) != 0 || !S_ISREG(st.st_mode))
return (NULL);
/*
* Attempt to figure out the current working directory of the
* target process. This only works if the target process has
* not changed its current directory since it was exec'd.
*/
(void) snprintf(proc_cwd, sizeof (proc_cwd),
"%s/%d/path/cwd", procfs_path, (int)P->pid);
if ((ret = readlink(proc_cwd, cwd, PATH_MAX - 1)) > 0)
cwd[ret] = '\0';
(void) Pfindexec(P, ret > 0 ? cwd : NULL, stat_exec, &st);
return (NULL);
}
#if defined(__i386) || defined(__amd64)
/*ARGSUSED*/
static int
Pldt_live(struct ps_prochandle *P, struct ssd *pldt, int nldt, void *data)
{
return (proc_get_ldt(P->pid, pldt, nldt));
}
#endif
static const ps_ops_t P_live_ops = {
.pop_pread = Pread_live,
.pop_pwrite = Pwrite_live,
.pop_read_maps = Pread_maps_live,
.pop_read_aux = Pread_aux_live,
.pop_cred = Pcred_live,
.pop_priv = Ppriv_live,
.pop_psinfo = Ppsinfo_live,
.pop_lstatus = Plstatus_live,
.pop_lpsinfo = Plpsinfo_live,
.pop_platform = Pplatform_live,
.pop_uname = Puname_live,
.pop_zonename = Pzonename_live,
.pop_execname = Pexecname_live,
.pop_secflags = Psecflags_live,
#if defined(__i386) || defined(__amd64)
.pop_ldt = Pldt_live
#endif
};
/*
* This is the library's .init handler.
*/
#pragma init(_libproc_init)
void
_libproc_init(void)
{
const char *root;
_libproc_debug = getenv("LIBPROC_DEBUG") != NULL;
_libproc_no_qsort = getenv("LIBPROC_NO_QSORT") != NULL;
_libproc_incore_elf = getenv("LIBPROC_INCORE_ELF") != NULL;
if ((root = zone_get_nroot()) != NULL)
(void) snprintf(procfs_path, sizeof (procfs_path), "%s/proc",
root);
(void) sigfillset(&blockable_sigs);
(void) sigdelset(&blockable_sigs, SIGKILL);
(void) sigdelset(&blockable_sigs, SIGSTOP);
}
void
Pset_procfs_path(const char *path)
{
(void) snprintf(procfs_path, sizeof (procfs_path), "%s", path);
}
/*
* Call set_minfd() once before calling dupfd() several times.
* We assume that the application will not reduce its current file
* descriptor limit lower than 512 once it has set at least that value.
*/
int
set_minfd(void)
{
static mutex_t minfd_lock = DEFAULTMUTEX;
struct rlimit rlim;
int fd;
if ((fd = minfd) < 256) {
(void) mutex_lock(&minfd_lock);
if ((fd = minfd) < 256) {
if (getrlimit(RLIMIT_NOFILE, &rlim) != 0)
rlim.rlim_cur = rlim.rlim_max = 0;
if (rlim.rlim_cur >= 512)
fd = 256;
else if ((fd = rlim.rlim_cur / 2) < 3)
fd = 3;
membar_producer();
minfd = fd;
}
(void) mutex_unlock(&minfd_lock);
}
return (fd);
}
int
dupfd(int fd, int dfd)
{
int mfd;
/*
* Make fd be greater than 255 (the 32-bit stdio limit),
* or at least make it greater than 2 so that the
* program will work when spawned by init(1m).
* Also, if dfd is non-zero, dup the fd to be dfd.
*/
if ((mfd = minfd) == 0)
mfd = set_minfd();
if (dfd > 0 || (0 <= fd && fd < mfd)) {
if (dfd <= 0)
dfd = mfd;
dfd = fcntl(fd, F_DUPFD, dfd);
(void) close(fd);
fd = dfd;
}
/*
* Mark it close-on-exec so any created process doesn't inherit it.
*/
if (fd >= 0)
(void) fcntl(fd, F_SETFD, FD_CLOEXEC);
return (fd);
}
/*
* Create a new controlled process.
* Leave it stopped on successful exit from exec() or execve().
* Return an opaque pointer to its process control structure.
* Return NULL if process cannot be created (fork()/exec() not successful).
*/
struct ps_prochandle *
Pxcreate(const char *file, /* executable file name */
char *const *argv, /* argument vector */
char *const *envp, /* environment */
int *perr, /* pointer to error return code */
char *path, /* if non-null, holds exec path name on return */
size_t len) /* size of the path buffer */
{
char execpath[PATH_MAX];
char procname[PATH_MAX];
struct ps_prochandle *P;
pid_t pid;
int fd;
char *fname;
int rc;
int lasterrno = 0;
if (len == 0) /* zero length, no path */
path = NULL;
if (path != NULL)
*path = '\0';
if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
*perr = C_STRANGE;
return (NULL);
}
if ((pid = fork1()) == -1) {
free(P);
*perr = C_FORK;
return (NULL);
}
if (pid == 0) { /* child process */
id_t id;
extern char **environ;
/*
* If running setuid or setgid, reset credentials to normal.
*/
if ((id = getgid()) != getegid())
(void) setgid(id);
if ((id = getuid()) != geteuid())
(void) setuid(id);
Pcreate_callback(P); /* execute callback (see below) */
(void) pause(); /* wait for PRSABORT from parent */
/*
* This is ugly. There is no execvep() function that takes a
* path and an environment. We cheat here by replacing the
* global 'environ' variable right before we call this.
*/
if (envp)
environ = (char **)envp;
(void) execvp(file, argv); /* execute the program */
_exit(127);
}
/*
* Initialize the process structure.
*/
(void) memset(P, 0, sizeof (*P));
(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
P->flags |= CREATED;
P->state = PS_RUN;
P->pid = pid;
P->asfd = -1;
P->ctlfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
Pinit_ops(&P->ops, &P_live_ops);
Pinitsym(P);
/*
* Open the /proc/pid files.
*/
(void) snprintf(procname, sizeof (procname), "%s/%d/",
procfs_path, (int)pid);
fname = procname + strlen(procname);
(void) set_minfd();
/*
* Exclusive write open advises others not to interfere.
* There is no reason for any of these open()s to fail.
*/
(void) strcpy(fname, "as");
if ((fd = open(procname, (O_RDWR|O_EXCL))) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
dprintf("Pcreate: failed to open %s: %s\n",
procname, strerror(errno));
rc = C_STRANGE;
goto bad;
}
P->asfd = fd;
(void) strcpy(fname, "status");
if ((fd = open(procname, O_RDONLY)) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
dprintf("Pcreate: failed to open %s: %s\n",
procname, strerror(errno));
rc = C_STRANGE;
goto bad;
}
P->statfd = fd;
(void) strcpy(fname, "ctl");
if ((fd = open(procname, O_WRONLY)) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
dprintf("Pcreate: failed to open %s: %s\n",
procname, strerror(errno));
rc = C_STRANGE;
goto bad;
}
P->ctlfd = fd;
(void) Pstop(P, 0); /* stop the controlled process */
/*
* Wait for process to sleep in pause().
* If the process has already called pause(), then it should be
* stopped (PR_REQUESTED) while asleep in pause and we are done.
* Else we set up to catch entry/exit to pause() and set the process
* running again, expecting it to stop when it reaches pause().
* There is no reason for this to fail other than an interrupt.
*/
(void) Psysentry(P, SYS_pause, 1);
(void) Psysexit(P, SYS_pause, 1);
for (;;) {
if (P->state == PS_STOP &&
P->status.pr_lwp.pr_syscall == SYS_pause &&
(P->status.pr_lwp.pr_why == PR_REQUESTED ||
P->status.pr_lwp.pr_why == PR_SYSENTRY ||
P->status.pr_lwp.pr_why == PR_SYSEXIT))
break;
if (P->state != PS_STOP || /* interrupt or process died */
Psetrun(P, 0, 0) != 0) { /* can't restart */
if (errno == EINTR || errno == ERESTART)
rc = C_INTR;
else {
dprintf("Pcreate: Psetrun failed: %s\n",
strerror(errno));
rc = C_STRANGE;
}
goto bad;
}
(void) Pwait(P, 0);
}
(void) Psysentry(P, SYS_pause, 0);
(void) Psysexit(P, SYS_pause, 0);
/*
* Kick the process off the pause() and catch
* it again on entry to exec() or exit().
*/
(void) Psysentry(P, SYS_exit, 1);
(void) Psysentry(P, SYS_execve, 1);
if (Psetrun(P, 0, PRSABORT) == -1) {
dprintf("Pcreate: Psetrun failed: %s\n", strerror(errno));
rc = C_STRANGE;
goto bad;
}
(void) Pwait(P, 0);
if (P->state != PS_STOP) {
dprintf("Pcreate: Pwait failed: %s\n", strerror(errno));
rc = C_STRANGE;
goto bad;
}
/*
* Move the process through instances of failed exec()s
* to reach the point of stopped on successful exec().
*/
(void) Psysexit(P, SYS_execve, TRUE);
while (P->state == PS_STOP &&
P->status.pr_lwp.pr_why == PR_SYSENTRY &&
P->status.pr_lwp.pr_what == SYS_execve) {
/*
* Fetch the exec path name now, before we complete
* the exec(). We may lose the process and be unable
* to get the information later.
*/
(void) Pread_string(P, execpath, sizeof (execpath),
(off_t)P->status.pr_lwp.pr_sysarg[0]);
if (path != NULL)
(void) strncpy(path, execpath, len);
/*
* Set the process running and wait for
* it to stop on exit from the exec().
*/
(void) Psetrun(P, 0, 0);
(void) Pwait(P, 0);
if (P->state == PS_LOST && /* we lost control */
Preopen(P) != 0) { /* and we can't get it back */
rc = C_PERM;
goto bad;
}
/*
* If the exec() failed, continue the loop, expecting
* there to be more attempts to exec(), based on PATH.
*/
if (P->state == PS_STOP &&
P->status.pr_lwp.pr_why == PR_SYSEXIT &&
P->status.pr_lwp.pr_what == SYS_execve &&
(lasterrno = P->status.pr_lwp.pr_errno) != 0) {
/*
* The exec() failed. Set the process running and
* wait for it to stop on entry to the next exec().
*/
(void) Psetrun(P, 0, 0);
(void) Pwait(P, 0);
continue;
}
break;
}
if (P->state == PS_STOP &&
P->status.pr_lwp.pr_why == PR_SYSEXIT &&
P->status.pr_lwp.pr_what == SYS_execve &&
P->status.pr_lwp.pr_errno == 0) {
/*
* The process is stopped on successful exec() or execve().
* Turn off all tracing flags and return success.
*/
restore_tracing_flags(P);
#ifndef _LP64
/* We must be a 64-bit process to deal with a 64-bit process */
if (P->status.pr_dmodel == PR_MODEL_LP64) {
rc = C_LP64;
goto bad;
}
#endif
/*
* Set run-on-last-close so the controlled process
* runs even if we die on a signal.
*/
(void) Psetflags(P, PR_RLC);
*perr = 0;
return (P);
}
rc = lasterrno == ENOENT ? C_NOENT : C_NOEXEC;
bad:
(void) kill(pid, SIGKILL);
if (path != NULL && rc != C_PERM && rc != C_LP64)
*path = '\0';
Pfree(P);
*perr = rc;
return (NULL);
}
struct ps_prochandle *
Pcreate(
const char *file, /* executable file name */
char *const *argv, /* argument vector */
int *perr, /* pointer to error return code */
char *path, /* if non-null, holds exec path name on return */
size_t len) /* size of the path buffer */
{
return (Pxcreate(file, argv, NULL, perr, path, len));
}
/*
* Return a printable string corresponding to a Pcreate() error return.
*/
const char *
Pcreate_error(int error)
{
const char *str;
switch (error) {
case C_FORK:
str = "cannot fork";
break;
case C_PERM:
str = "file is set-id or unreadable";
break;
case C_NOEXEC:
str = "cannot execute file";
break;
case C_INTR:
str = "operation interrupted";
break;
case C_LP64:
str = "program is _LP64, self is not";
break;
case C_STRANGE:
str = "unanticipated system error";
break;
case C_NOENT:
str = "cannot find executable file";
break;
default:
str = "unknown error";
break;
}
return (str);
}
/*
* Callback to execute in each child process created with Pcreate() after fork
* but before it execs the new process image. By default, we do nothing, but
* by calling this function we allow the client program to define its own
* version of the function which will interpose on our empty default. This
* may be useful for clients that need to modify signal dispositions, terminal
* attributes, or process group and session properties for each new victim.
*/
/*ARGSUSED*/
void
Pcreate_callback(struct ps_prochandle *P)
{
/* nothing to do here */
}
/*
* Grab an existing process.
* Return an opaque pointer to its process control structure.
*
* pid: UNIX process ID.
* flags:
* PGRAB_RETAIN Retain tracing flags (default clears all tracing flags).
* PGRAB_FORCE Grab regardless of whether process is already traced.
* PGRAB_RDONLY Open the address space file O_RDONLY instead of O_RDWR,
* and do not open the process control file.
* PGRAB_NOSTOP Open the process but do not force it to stop.
* perr: pointer to error return code.
*/
struct ps_prochandle *
Pgrab(pid_t pid, int flags, int *perr)
{
struct ps_prochandle *P;
int fd, omode;
char procname[PATH_MAX];
char *fname;
int rc = 0;
/*
* PGRAB_RDONLY means that we do not open the /proc/<pid>/control file,
* and so it implies RETAIN and NOSTOP since both require control.
*/
if (flags & PGRAB_RDONLY)
flags |= PGRAB_RETAIN | PGRAB_NOSTOP;
if ((P = malloc(sizeof (struct ps_prochandle))) == NULL) {
*perr = G_STRANGE;
return (NULL);
}
P->asfd = -1;
P->ctlfd = -1;
P->statfd = -1;
again: /* Come back here if we lose it in the Window of Vulnerability */
if (P->ctlfd >= 0)
(void) close(P->ctlfd);
if (P->asfd >= 0)
(void) close(P->asfd);
if (P->statfd >= 0)
(void) close(P->statfd);
(void) memset(P, 0, sizeof (*P));
(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
P->ctlfd = -1;
P->asfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
Pinit_ops(&P->ops, &P_live_ops);
Pinitsym(P);
/*
* Open the /proc/pid files
*/
(void) snprintf(procname, sizeof (procname), "%s/%d/",
procfs_path, (int)pid);
fname = procname + strlen(procname);
(void) set_minfd();
/*
* Request exclusive open to avoid grabbing someone else's
* process and to prevent others from interfering afterwards.
* If this fails and the 'PGRAB_FORCE' flag is set, attempt to
* open non-exclusively.
*/
(void) strcpy(fname, "as");
omode = (flags & PGRAB_RDONLY) ? O_RDONLY : O_RDWR;
if (((fd = open(procname, omode | O_EXCL)) < 0 &&
(fd = ((flags & PGRAB_FORCE)? open(procname, omode) : -1)) < 0) ||
(fd = dupfd(fd, 0)) < 0) {
switch (errno) {
case ENOENT:
rc = G_NOPROC;
break;
case EACCES:
case EPERM:
rc = G_PERM;
break;
case EMFILE:
rc = G_NOFD;
break;
case EBUSY:
if (!(flags & PGRAB_FORCE) || geteuid() != 0) {
rc = G_BUSY;
break;
}
/* FALLTHROUGH */
default:
dprintf("Pgrab: failed to open %s: %s\n",
procname, strerror(errno));
rc = G_STRANGE;
break;
}
goto err;
}
P->asfd = fd;
(void) strcpy(fname, "status");
if ((fd = open(procname, O_RDONLY)) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
switch (errno) {
case ENOENT:
rc = G_NOPROC;
break;
case EMFILE:
rc = G_NOFD;
break;
default:
dprintf("Pgrab: failed to open %s: %s\n",
procname, strerror(errno));
rc = G_STRANGE;
break;
}
goto err;
}
P->statfd = fd;
if (!(flags & PGRAB_RDONLY)) {
(void) strcpy(fname, "ctl");
if ((fd = open(procname, O_WRONLY)) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
switch (errno) {
case ENOENT:
rc = G_NOPROC;
break;
case EMFILE:
rc = G_NOFD;
break;
default:
dprintf("Pgrab: failed to open %s: %s\n",
procname, strerror(errno));
rc = G_STRANGE;
break;
}
goto err;
}
P->ctlfd = fd;
}
P->state = PS_RUN;
P->pid = pid;
/*
* We are now in the Window of Vulnerability (WoV). The process may
* exec() a setuid/setgid or unreadable object file between the open()
* and the PCSTOP. We will get EAGAIN in this case and must start over.
* As Pstopstatus will trigger the first read() from a /proc file,
* we also need to handle EOVERFLOW here when 32-bit as an indicator
* that this process is 64-bit. Finally, if the process has become
* a zombie (PS_UNDEAD) while we were trying to grab it, just remain
* silent about this and pretend there was no process.
*/
if (Pstopstatus(P, PCNULL, 0) != 0) {
#ifndef _LP64
if (errno == EOVERFLOW) {
rc = G_LP64;
goto err;
}
#endif
if (P->state == PS_LOST) { /* WoV */
(void) mutex_destroy(&P->proc_lock);
goto again;
}
if (P->state == PS_UNDEAD)
rc = G_NOPROC;
else
rc = G_STRANGE;
goto err;
}
/*
* If the process is a system process, we can't control it even as root
*/
if (P->status.pr_flags & PR_ISSYS) {
rc = G_SYS;
goto err;
}
#ifndef _LP64
/*
* We must be a 64-bit process to deal with a 64-bit process
*/
if (P->status.pr_dmodel == PR_MODEL_LP64) {
rc = G_LP64;
goto err;
}
#endif
/*
* Remember the status for use by Prelease().
*/
P->orig_status = P->status; /* structure copy */
/*
* Before stopping the process, make sure we are not grabbing ourselves.
* If we are, make sure we are doing it PGRAB_RDONLY.
*/
if (pid == getpid()) {
/*
* Verify that the process is really ourself:
* Set a magic number, read it through the
* /proc file and see if the results match.
*/
uint32_t magic1 = 0;
uint32_t magic2 = 2;
errno = 0;
if (Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
== sizeof (magic2) &&
magic2 == 0 &&
(magic1 = 0xfeedbeef) &&
Pread(P, &magic2, sizeof (magic2), (uintptr_t)&magic1)
== sizeof (magic2) &&
magic2 == 0xfeedbeef &&
!(flags & PGRAB_RDONLY)) {
rc = G_SELF;
goto err;
}
}
/*
* If the process is already stopped or has been directed
* to stop via /proc, do not set run-on-last-close.
*/
if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
!(flags & PGRAB_RDONLY)) {
/*
* Mark the process run-on-last-close so
* it runs even if we die from SIGKILL.
*/
if (Psetflags(P, PR_RLC) != 0) {
if (errno == EAGAIN) { /* WoV */
(void) mutex_destroy(&P->proc_lock);
goto again;
}
if (errno == ENOENT) /* No complaint about zombies */
rc = G_ZOMB;
else {
dprintf("Pgrab: failed to set RLC\n");
rc = G_STRANGE;
}
goto err;
}
}
/*
* If a stop directive is pending and the process has not yet stopped,
* then synchronously wait for the stop directive to take effect.
* Limit the time spent waiting for the process to stop by iterating
* at most 10 times. The time-out of 20 ms corresponds to the time
* between sending the stop directive and the process actually stopped
* as measured by DTrace on a slow, busy system. If the process doesn't
* stop voluntarily, clear the PR_DSTOP flag so that the code below
* forces the process to stop.
*/
if (!(flags & PGRAB_RDONLY)) {
int niter = 0;
while ((P->status.pr_lwp.pr_flags & (PR_STOPPED|PR_DSTOP)) ==
PR_DSTOP && niter < 10 &&
Pstopstatus(P, PCTWSTOP, 20) != 0) {
niter++;
if (flags & PGRAB_NOSTOP)
break;
}
if (niter == 10 && !(flags & PGRAB_NOSTOP)) {
/* Try it harder down below */
P->status.pr_lwp.pr_flags &= ~PR_DSTOP;
}
}
/*
* If the process is not already stopped or directed to stop
* and PGRAB_NOSTOP was not specified, stop the process now.
*/
if (!(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) &&
!(flags & PGRAB_NOSTOP)) {
/*
* Stop the process, get its status and signal/syscall masks.
*/
if (((P->status.pr_lwp.pr_flags & PR_STOPPED) &&
Pstopstatus(P, PCDSTOP, 0) != 0) ||
Pstopstatus(P, PCSTOP, 2000) != 0) {
#ifndef _LP64
if (errno == EOVERFLOW) {
rc = G_LP64;
goto err;
}
#endif
if (P->state == PS_LOST) { /* WoV */
(void) mutex_destroy(&P->proc_lock);
goto again;
}
if ((errno != EINTR && errno != ERESTART) ||
(P->state != PS_STOP &&
!(P->status.pr_flags & PR_DSTOP))) {
if (P->state != PS_RUN && errno != ENOENT) {
dprintf("Pgrab: failed to PCSTOP\n");
rc = G_STRANGE;
} else {
rc = G_ZOMB;
}
goto err;
}
}
/*
* Process should now either be stopped via /proc or there
* should be an outstanding stop directive.
*/
if (!(P->status.pr_flags & (PR_ISTOP|PR_DSTOP))) {
dprintf("Pgrab: process is not stopped\n");
rc = G_STRANGE;
goto err;
}
#ifndef _LP64
/*
* Test this again now because the 32-bit victim process may
* have exec'd a 64-bit process in the meantime.
*/
if (P->status.pr_dmodel == PR_MODEL_LP64) {
rc = G_LP64;
goto err;
}
#endif
}
/*
* Cancel all tracing flags unless the PGRAB_RETAIN flag is set.
*/
if (!(flags & PGRAB_RETAIN)) {
(void) Psysentry(P, 0, FALSE);
(void) Psysexit(P, 0, FALSE);
(void) Psignal(P, 0, FALSE);
(void) Pfault(P, 0, FALSE);
Psync(P);
}
*perr = 0;
return (P);
err:
Pfree(P);
*perr = rc;
return (NULL);
}
/*
* Return a printable string corresponding to a Pgrab() error return.
*/
const char *
Pgrab_error(int error)
{
const char *str;
switch (error) {
case G_NOPROC:
str = "no such process";
break;
case G_NOCORE:
str = "no such core file";
break;
case G_NOPROCORCORE:
str = "no such process or core file";
break;
case G_NOEXEC:
str = "cannot find executable file";
break;
case G_ZOMB:
str = "zombie process";
break;
case G_PERM:
str = "permission denied";
break;
case G_BUSY:
str = "process is traced";
break;
case G_SYS:
str = "system process";
break;
case G_SELF:
str = "attempt to grab self";
break;
case G_INTR:
str = "operation interrupted";
break;
case G_LP64:
str = "program is _LP64, self is not";
break;
case G_FORMAT:
str = "file is not an ELF core file";
break;
case G_ELF:
str = "libelf error";
break;
case G_NOTE:
str = "core file is corrupt or missing required data";
break;
case G_STRANGE:
str = "unanticipated system error";
break;
case G_ISAINVAL:
str = "wrong ELF machine type";
break;
case G_BADLWPS:
str = "bad lwp specification";
break;
case G_NOFD:
str = "too many open files";
break;
default:
str = "unknown error";
break;
}
return (str);
}
/*
* Free a process control structure.
* Close the file descriptors but don't do the Prelease logic.
*/
void
Pfree(struct ps_prochandle *P)
{
uint_t i;
if (P->ucaddrs != NULL) {
free(P->ucaddrs);
P->ucaddrs = NULL;
P->ucnelems = 0;
}
(void) mutex_lock(&P->proc_lock);
if (P->hashtab != NULL) {
struct ps_lwphandle *L;
for (i = 0; i < HASHSIZE; i++) {
while ((L = P->hashtab[i]) != NULL)
Lfree_internal(P, L);
}
free(P->hashtab);
}
while (P->num_fd > 0) {
fd_info_t *fip = list_next(&P->fd_head);
list_unlink(fip);
free(fip);
P->num_fd--;
}
(void) mutex_unlock(&P->proc_lock);
(void) mutex_destroy(&P->proc_lock);
if (P->agentctlfd >= 0)
(void) close(P->agentctlfd);
if (P->agentstatfd >= 0)
(void) close(P->agentstatfd);
if (P->ctlfd >= 0)
(void) close(P->ctlfd);
if (P->asfd >= 0)
(void) close(P->asfd);
if (P->statfd >= 0)
(void) close(P->statfd);
Preset_maps(P);
P->ops.pop_fini(P, P->data);
/* clear out the structure as a precaution against reuse */
(void) memset(P, 0, sizeof (*P));
P->ctlfd = -1;
P->asfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
free(P);
}
/*
* Return the state of the process, one of the PS_* values.
*/
int
Pstate(struct ps_prochandle *P)
{
return (P->state);
}
/*
* Return the open address space file descriptor for the process.
* Clients must not close this file descriptor, not use it
* after the process is freed.
*/
int
Pasfd(struct ps_prochandle *P)
{
return (P->asfd);
}
/*
* Return the open control file descriptor for the process.
* Clients must not close this file descriptor, not use it
* after the process is freed.
*/
int
Pctlfd(struct ps_prochandle *P)
{
return (P->ctlfd);
}
/*
* Return a pointer to the process psinfo structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Prelease().
*/
const psinfo_t *
Ppsinfo(struct ps_prochandle *P)
{
return (P->ops.pop_psinfo(P, &P->psinfo, P->data));
}
/*
* Return a pointer to the process status structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Prelease().
*/
const pstatus_t *
Pstatus(struct ps_prochandle *P)
{
return (&P->status);
}
static void
Pread_status(struct ps_prochandle *P)
{
P->ops.pop_status(P, &P->status, P->data);
}
/*
* Fill in a pointer to a process credentials structure. The ngroups parameter
* is the number of supplementary group entries allocated in the caller's cred
* structure. It should equal zero or one unless extra space has been
* allocated for the group list by the caller.
*/
int
Pcred(struct ps_prochandle *P, prcred_t *pcrp, int ngroups)
{
return (P->ops.pop_cred(P, pcrp, ngroups, P->data));
}
/* Return an allocated prsecflags_t */
int
Psecflags(struct ps_prochandle *P, prsecflags_t **psf)
{
int ret;
if ((ret = P->ops.pop_secflags(P, psf, P->data)) == 0) {
if ((*psf)->pr_version != PRSECFLAGS_VERSION_1) {
errno = EINVAL;
return (-1);
}
}
return (ret);
}
void
Psecflags_free(prsecflags_t *psf)
{
free(psf);
}
static prheader_t *
Plstatus(struct ps_prochandle *P)
{
return (P->ops.pop_lstatus(P, P->data));
}
static prheader_t *
Plpsinfo(struct ps_prochandle *P)
{
return (P->ops.pop_lpsinfo(P, P->data));
}
#if defined(__i386) || defined(__amd64)
/*
* Fill in a pointer to a process LDT structure.
* The caller provides a buffer of size 'nldt * sizeof (struct ssd)';
* If pldt == NULL or nldt == 0, we return the number of existing LDT entries.
* Otherwise we return the actual number of LDT entries fetched (<= nldt).
*/
int
Pldt(struct ps_prochandle *P, struct ssd *pldt, int nldt)
{
return (P->ops.pop_ldt(P, pldt, nldt, P->data));
}
#endif /* __i386 */
/* ARGSUSED */
void
Ppriv_free(struct ps_prochandle *P, prpriv_t *prv)
{
free(prv);
}
/*
* Return a malloced process privilege structure in *pprv.
*/
int
Ppriv(struct ps_prochandle *P, prpriv_t **pprv)
{
return (P->ops.pop_priv(P, pprv, P->data));
}
int
Psetpriv(struct ps_prochandle *P, prpriv_t *pprv)
{
int rc;
long *ctl;
size_t sz;
if (P->state == PS_DEAD) {
errno = EBADF;
return (-1);
}
sz = PRIV_PRPRIV_SIZE(pprv) + sizeof (long);
sz = ((sz - 1) / sizeof (long) + 1) * sizeof (long);
ctl = malloc(sz);
if (ctl == NULL)
return (-1);
ctl[0] = PCSPRIV;
(void) memcpy(&ctl[1], pprv, PRIV_PRPRIV_SIZE(pprv));
if (write(P->ctlfd, ctl, sz) != sz)
rc = -1;
else
rc = 0;
free(ctl);
return (rc);
}
void *
Pprivinfo(struct ps_prochandle *P)
{
core_info_t *core = P->data;
/* Use default from libc */
if (P->state != PS_DEAD)
return (NULL);
return (core->core_privinfo);
}
/*
* Ensure that all cached state is written to the process.
* The cached state is the LWP's signal mask and registers
* and the process's tracing flags.
*/
void
Psync(struct ps_prochandle *P)
{
int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
long cmd[6];
iovec_t iov[12];
int n = 0;
if (P->flags & SETHOLD) {
cmd[0] = PCSHOLD;
iov[n].iov_base = (caddr_t)&cmd[0];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_lwphold;
iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_lwphold);
}
if (P->flags & SETREGS) {
cmd[1] = PCSREG;
#ifdef __i386
/* XX64 we should probably restore REG_GS after this */
if (ctlfd == P->agentctlfd)
P->status.pr_lwp.pr_reg[GS] = 0;
#elif defined(__amd64)
/* XX64 */
#endif
iov[n].iov_base = (caddr_t)&cmd[1];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&P->status.pr_lwp.pr_reg[0];
iov[n++].iov_len = sizeof (P->status.pr_lwp.pr_reg);
}
if (P->flags & SETSIG) {
cmd[2] = PCSTRACE;
iov[n].iov_base = (caddr_t)&cmd[2];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&P->status.pr_sigtrace;
iov[n++].iov_len = sizeof (P->status.pr_sigtrace);
}
if (P->flags & SETFAULT) {
cmd[3] = PCSFAULT;
iov[n].iov_base = (caddr_t)&cmd[3];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&P->status.pr_flttrace;
iov[n++].iov_len = sizeof (P->status.pr_flttrace);
}
if (P->flags & SETENTRY) {
cmd[4] = PCSENTRY;
iov[n].iov_base = (caddr_t)&cmd[4];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&P->status.pr_sysentry;
iov[n++].iov_len = sizeof (P->status.pr_sysentry);
}
if (P->flags & SETEXIT) {
cmd[5] = PCSEXIT;
iov[n].iov_base = (caddr_t)&cmd[5];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&P->status.pr_sysexit;
iov[n++].iov_len = sizeof (P->status.pr_sysexit);
}
if (n == 0 || writev(ctlfd, iov, n) < 0)
return; /* nothing to do or write failed */
P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT|SETHOLD|SETREGS);
}
/*
* Reopen the /proc file (after PS_LOST).
*/
int
Preopen(struct ps_prochandle *P)
{
int fd;
char procname[PATH_MAX];
char *fname;
if (P->state == PS_DEAD || P->state == PS_IDLE)
return (0);
if (P->agentcnt > 0) {
P->agentcnt = 1;
Pdestroy_agent(P);
}
(void) snprintf(procname, sizeof (procname), "%s/%d/",
procfs_path, (int)P->pid);
fname = procname + strlen(procname);
(void) strcpy(fname, "as");
if ((fd = open(procname, O_RDWR)) < 0 ||
close(P->asfd) < 0 ||
(fd = dupfd(fd, P->asfd)) != P->asfd) {
dprintf("Preopen: failed to open %s: %s\n",
procname, strerror(errno));
if (fd >= 0)
(void) close(fd);
return (-1);
}
P->asfd = fd;
(void) strcpy(fname, "status");
if ((fd = open(procname, O_RDONLY)) < 0 ||
close(P->statfd) < 0 ||
(fd = dupfd(fd, P->statfd)) != P->statfd) {
dprintf("Preopen: failed to open %s: %s\n",
procname, strerror(errno));
if (fd >= 0)
(void) close(fd);
return (-1);
}
P->statfd = fd;
(void) strcpy(fname, "ctl");
if ((fd = open(procname, O_WRONLY)) < 0 ||
close(P->ctlfd) < 0 ||
(fd = dupfd(fd, P->ctlfd)) != P->ctlfd) {
dprintf("Preopen: failed to open %s: %s\n",
procname, strerror(errno));
if (fd >= 0)
(void) close(fd);
return (-1);
}
P->ctlfd = fd;
/*
* Set the state to PS_RUN and wait for the process to stop so that
* we re-read the status from the new P->statfd. If this fails, Pwait
* will reset the state to PS_LOST and we fail the reopen. Before
* returning, we also forge a bit of P->status to allow the debugger to
* see that we are PS_LOST following a successful exec.
*/
P->state = PS_RUN;
if (Pwait(P, 0) == -1) {
#ifdef _ILP32
if (errno == EOVERFLOW)
P->status.pr_dmodel = PR_MODEL_LP64;
#endif
P->status.pr_lwp.pr_why = PR_SYSEXIT;
P->status.pr_lwp.pr_what = SYS_execve;
P->status.pr_lwp.pr_errno = 0;
return (-1);
}
/*
* The process should be stopped on exec (REQUESTED)
* or else should be stopped on exit from exec() (SYSEXIT)
*/
if (P->state == PS_STOP &&
(P->status.pr_lwp.pr_why == PR_REQUESTED ||
(P->status.pr_lwp.pr_why == PR_SYSEXIT &&
P->status.pr_lwp.pr_what == SYS_execve))) {
/* fake up stop-on-exit-from-execve */
if (P->status.pr_lwp.pr_why == PR_REQUESTED) {
P->status.pr_lwp.pr_why = PR_SYSEXIT;
P->status.pr_lwp.pr_what = SYS_execve;
P->status.pr_lwp.pr_errno = 0;
}
} else {
dprintf("Preopen: expected REQUESTED or "
"SYSEXIT(SYS_execve) stop\n");
}
return (0);
}
/*
* Define all settable flags other than the microstate accounting flags.
*/
#define ALL_SETTABLE_FLAGS (PR_FORK|PR_RLC|PR_KLC|PR_ASYNC|PR_BPTADJ|PR_PTRACE)
/*
* Restore /proc tracing flags to their original values
* in preparation for releasing the process.
* Also called by Pcreate() to clear all tracing flags.
*/
static void
restore_tracing_flags(struct ps_prochandle *P)
{
long flags;
long cmd[4];
iovec_t iov[8];
if (P->flags & CREATED) {
/* we created this process; clear all tracing flags */
premptyset(&P->status.pr_sigtrace);
premptyset(&P->status.pr_flttrace);
premptyset(&P->status.pr_sysentry);
premptyset(&P->status.pr_sysexit);
if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) != 0)
(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
} else {
/* we grabbed the process; restore its tracing flags */
P->status.pr_sigtrace = P->orig_status.pr_sigtrace;
P->status.pr_flttrace = P->orig_status.pr_flttrace;
P->status.pr_sysentry = P->orig_status.pr_sysentry;
P->status.pr_sysexit = P->orig_status.pr_sysexit;
if ((P->status.pr_flags & ALL_SETTABLE_FLAGS) !=
(flags = (P->orig_status.pr_flags & ALL_SETTABLE_FLAGS))) {
(void) Punsetflags(P, ALL_SETTABLE_FLAGS);
if (flags)
(void) Psetflags(P, flags);
}
}
cmd[0] = PCSTRACE;
iov[0].iov_base = (caddr_t)&cmd[0];
iov[0].iov_len = sizeof (long);
iov[1].iov_base = (caddr_t)&P->status.pr_sigtrace;
iov[1].iov_len = sizeof (P->status.pr_sigtrace);
cmd[1] = PCSFAULT;
iov[2].iov_base = (caddr_t)&cmd[1];
iov[2].iov_len = sizeof (long);
iov[3].iov_base = (caddr_t)&P->status.pr_flttrace;
iov[3].iov_len = sizeof (P->status.pr_flttrace);
cmd[2] = PCSENTRY;
iov[4].iov_base = (caddr_t)&cmd[2];
iov[4].iov_len = sizeof (long);
iov[5].iov_base = (caddr_t)&P->status.pr_sysentry;
iov[5].iov_len = sizeof (P->status.pr_sysentry);
cmd[3] = PCSEXIT;
iov[6].iov_base = (caddr_t)&cmd[3];
iov[6].iov_len = sizeof (long);
iov[7].iov_base = (caddr_t)&P->status.pr_sysexit;
iov[7].iov_len = sizeof (P->status.pr_sysexit);
(void) writev(P->ctlfd, iov, 8);
P->flags &= ~(SETSIG|SETFAULT|SETENTRY|SETEXIT);
}
/*
* Release the process. Frees the process control structure.
* flags:
* PRELEASE_CLEAR Clear all tracing flags.
* PRELEASE_RETAIN Retain current tracing flags.
* PRELEASE_HANG Leave the process stopped and abandoned.
* PRELEASE_KILL Terminate the process with SIGKILL.
*/
void
Prelease(struct ps_prochandle *P, int flags)
{
if (P->state == PS_DEAD) {
dprintf("Prelease: releasing handle %p PS_DEAD of pid %d\n",
(void *)P, (int)P->pid);
Pfree(P);
return;
}
if (P->state == PS_IDLE) {
file_info_t *fptr = list_next(&P->file_head);
dprintf("Prelease: releasing handle %p PS_IDLE of file %s\n",
(void *)P, fptr->file_pname);
Pfree(P);
return;
}
dprintf("Prelease: releasing handle %p pid %d\n",
(void *)P, (int)P->pid);
if (P->ctlfd == -1) {
Pfree(P);
return;
}
if (P->agentcnt > 0) {
P->agentcnt = 1;
Pdestroy_agent(P);
}
/*
* Attempt to stop the process.
*/
P->state = PS_RUN;
(void) Pstop(P, 1000);
if (flags & PRELEASE_KILL) {
if (P->state == PS_STOP)
(void) Psetrun(P, SIGKILL, 0);
(void) kill(P->pid, SIGKILL);
Pfree(P);
return;
}
/*
* If we lost control, all we can do now is close the files.
* In this case, the last close sets the process running.
*/
if (P->state != PS_STOP &&
(P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
Pfree(P);
return;
}
/*
* We didn't lose control; we do more.
*/
Psync(P);
if (flags & PRELEASE_CLEAR)
P->flags |= CREATED;
if (!(flags & PRELEASE_RETAIN))
restore_tracing_flags(P);
if (flags & PRELEASE_HANG) {
/* Leave the process stopped and abandoned */
(void) Punsetflags(P, PR_RLC|PR_KLC);
Pfree(P);
return;
}
/*
* Set the process running if we created it or if it was
* not originally stopped or directed to stop via /proc
* or if we were given the PRELEASE_CLEAR flag.
*/
if ((P->flags & CREATED) ||
(P->orig_status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP)) == 0) {
(void) Psetflags(P, PR_RLC);
/*
* We do this repeatedly because the process may have
* more than one LWP stopped on an event of interest.
* This makes sure all of them are set running.
*/
do {
if (Psetrun(P, 0, 0) == -1 && errno == EBUSY)
break; /* Agent LWP may be stuck */
} while (Pstopstatus(P, PCNULL, 0) == 0 &&
P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP));
if (P->status.pr_lwp.pr_flags & (PR_ISTOP|PR_DSTOP))
dprintf("Prelease: failed to set process running\n");
}
Pfree(P);
}
/* debugging */
void
prldump(const char *caller, lwpstatus_t *lsp)
{
char name[32];
uint32_t bits;
switch (lsp->pr_why) {
case PR_REQUESTED:
dprintf("%s: REQUESTED\n", caller);
break;
case PR_SIGNALLED:
dprintf("%s: SIGNALLED %s\n", caller,
proc_signame(lsp->pr_what, name, sizeof (name)));
break;
case PR_FAULTED:
dprintf("%s: FAULTED %s\n", caller,
proc_fltname(lsp->pr_what, name, sizeof (name)));
break;
case PR_SYSENTRY:
dprintf("%s: SYSENTRY %s\n", caller,
proc_sysname(lsp->pr_what, name, sizeof (name)));
break;
case PR_SYSEXIT:
dprintf("%s: SYSEXIT %s\n", caller,
proc_sysname(lsp->pr_what, name, sizeof (name)));
break;
case PR_JOBCONTROL:
dprintf("%s: JOBCONTROL %s\n", caller,
proc_signame(lsp->pr_what, name, sizeof (name)));
break;
case PR_SUSPENDED:
dprintf("%s: SUSPENDED\n", caller);
break;
case PR_BRAND:
dprintf("%s: BRANDPRIVATE (%d)\n", caller, lsp->pr_what);
break;
default:
dprintf("%s: Unknown\n", caller);
break;
}
if (lsp->pr_cursig)
dprintf("%s: p_cursig = %d\n", caller, lsp->pr_cursig);
bits = *((uint32_t *)&lsp->pr_lwppend);
if (bits)
dprintf("%s: pr_lwppend = 0x%.8X\n", caller, bits);
}
/* debugging */
static void
prdump(struct ps_prochandle *P)
{
uint32_t bits;
prldump("Pstopstatus", &P->status.pr_lwp);
bits = *((uint32_t *)&P->status.pr_sigpend);
if (bits)
dprintf("Pstopstatus: pr_sigpend = 0x%.8X\n", bits);
}
/*
* Wait for the specified process to stop or terminate.
* Or, just get the current status (PCNULL).
* Or, direct it to stop and get the current status (PCDSTOP).
* If the agent LWP exists, do these things to the agent,
* else do these things to the process as a whole.
*/
int
Pstopstatus(struct ps_prochandle *P,
long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
uint_t msec) /* if non-zero, timeout in milliseconds */
{
int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
long ctl[3];
ssize_t rc;
int err;
int old_state = P->state;
switch (P->state) {
case PS_RUN:
break;
case PS_STOP:
if (request != PCNULL && request != PCDSTOP)
return (0);
break;
case PS_LOST:
if (request != PCNULL) {
errno = EAGAIN;
return (-1);
}
break;
case PS_UNDEAD:
case PS_DEAD:
case PS_IDLE:
if (request != PCNULL) {
errno = ENOENT;
return (-1);
}
break;
default: /* corrupted state */
dprintf("Pstopstatus: corrupted state: %d\n", P->state);
errno = EINVAL;
return (-1);
}
ctl[0] = PCDSTOP;
ctl[1] = PCTWSTOP;
ctl[2] = (long)msec;
rc = 0;
switch (request) {
case PCSTOP:
rc = write(ctlfd, &ctl[0], 3*sizeof (long));
break;
case PCWSTOP:
rc = write(ctlfd, &ctl[1], 2*sizeof (long));
break;
case PCDSTOP:
rc = write(ctlfd, &ctl[0], 1*sizeof (long));
break;
case PCNULL:
if (P->state == PS_DEAD || P->state == PS_IDLE)
return (0);
break;
default: /* programming error */
errno = EINVAL;
return (-1);
}
err = (rc < 0)? errno : 0;
Psync(P);
if (P->agentstatfd < 0) {
if (pread(P->statfd, &P->status,
sizeof (P->status), (off_t)0) < 0)
err = errno;
} else {
if (pread(P->agentstatfd, &P->status.pr_lwp,
sizeof (P->status.pr_lwp), (off_t)0) < 0)
err = errno;
P->status.pr_flags = P->status.pr_lwp.pr_flags;
}
if (err) {
switch (err) {
case EINTR: /* user typed ctl-C */
case ERESTART:
dprintf("Pstopstatus: EINTR\n");
break;
case EAGAIN: /* we lost control of the the process */
case EOVERFLOW:
dprintf("Pstopstatus: PS_LOST, errno=%d\n", err);
P->state = PS_LOST;
break;
default: /* check for dead process */
if (_libproc_debug) {
const char *errstr;
switch (request) {
case PCNULL:
errstr = "Pstopstatus PCNULL"; break;
case PCSTOP:
errstr = "Pstopstatus PCSTOP"; break;
case PCDSTOP:
errstr = "Pstopstatus PCDSTOP"; break;
case PCWSTOP:
errstr = "Pstopstatus PCWSTOP"; break;
default:
errstr = "Pstopstatus PC???"; break;
}
dprintf("%s: %s\n", errstr, strerror(err));
}
deadcheck(P);
break;
}
if (err != EINTR && err != ERESTART) {
errno = err;
return (-1);
}
}
if (!(P->status.pr_flags & PR_STOPPED)) {
P->state = PS_RUN;
if (request == PCNULL || request == PCDSTOP || msec != 0)
return (0);
dprintf("Pstopstatus: process is not stopped\n");
errno = EPROTO;
return (-1);
}
P->state = PS_STOP;
if (_libproc_debug) /* debugging */
prdump(P);
/*
* If the process was already stopped coming into Pstopstatus(),
* then don't use its PC to set P->sysaddr since it may have been
* changed since the time the process originally stopped.
*/
if (old_state == PS_STOP)
return (0);
switch (P->status.pr_lwp.pr_why) {
case PR_SYSENTRY:
case PR_SYSEXIT:
if (Pissyscall_prev(P, P->status.pr_lwp.pr_reg[R_PC],
&P->sysaddr) == 0)
P->sysaddr = P->status.pr_lwp.pr_reg[R_PC];
break;
case PR_REQUESTED:
case PR_SIGNALLED:
case PR_FAULTED:
case PR_JOBCONTROL:
case PR_SUSPENDED:
case PR_BRAND:
break;
default:
errno = EPROTO;
return (-1);
}
return (0);
}
/*
* Wait for the process to stop for any reason.
*/
int
Pwait(struct ps_prochandle *P, uint_t msec)
{
return (Pstopstatus(P, PCWSTOP, msec));
}
/*
* Direct the process to stop; wait for it to stop.
*/
int
Pstop(struct ps_prochandle *P, uint_t msec)
{
return (Pstopstatus(P, PCSTOP, msec));
}
/*
* Direct the process to stop; don't wait.
*/
int
Pdstop(struct ps_prochandle *P)
{
return (Pstopstatus(P, PCDSTOP, 0));
}
static void
deadcheck(struct ps_prochandle *P)
{
int fd;
void *buf;
size_t size;
if (P->statfd < 0)
P->state = PS_UNDEAD;
else {
if (P->agentstatfd < 0) {
fd = P->statfd;
buf = &P->status;
size = sizeof (P->status);
} else {
fd = P->agentstatfd;
buf = &P->status.pr_lwp;
size = sizeof (P->status.pr_lwp);
}
while (pread(fd, buf, size, (off_t)0) != size) {
switch (errno) {
default:
P->state = PS_UNDEAD;
break;
case EINTR:
case ERESTART:
continue;
case EAGAIN:
P->state = PS_LOST;
break;
}
break;
}
P->status.pr_flags = P->status.pr_lwp.pr_flags;
}
}
/*
* Get the value of one register from stopped process.
*/
int
Pgetareg(struct ps_prochandle *P, int regno, prgreg_t *preg)
{
if (regno < 0 || regno >= NPRGREG) {
errno = EINVAL;
return (-1);
}
if (P->state == PS_IDLE) {
errno = ENODATA;
return (-1);
}
if (P->state != PS_STOP && P->state != PS_DEAD) {
errno = EBUSY;
return (-1);
}
*preg = P->status.pr_lwp.pr_reg[regno];
return (0);
}
/*
* Put value of one register into stopped process.
*/
int
Pputareg(struct ps_prochandle *P, int regno, prgreg_t reg)
{
if (regno < 0 || regno >= NPRGREG) {
errno = EINVAL;
return (-1);
}
if (P->state != PS_STOP) {
errno = EBUSY;
return (-1);
}
P->status.pr_lwp.pr_reg[regno] = reg;
P->flags |= SETREGS; /* set registers before continuing */
return (0);
}
int
Psetrun(struct ps_prochandle *P,
int sig, /* signal to pass to process */
int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
{
int ctlfd = (P->agentctlfd >= 0) ? P->agentctlfd : P->ctlfd;
int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
long ctl[1 + /* PCCFAULT */
1 + sizeof (siginfo_t)/sizeof (long) + /* PCSSIG/PCCSIG */
2 ]; /* PCRUN */
long *ctlp = ctl;
size_t size;
if (P->state != PS_STOP && (P->status.pr_lwp.pr_flags & sbits) == 0) {
errno = EBUSY;
return (-1);
}
Psync(P); /* flush tracing flags and registers */
if (flags & PRCFAULT) { /* clear current fault */
*ctlp++ = PCCFAULT;
flags &= ~PRCFAULT;
}
if (flags & PRCSIG) { /* clear current signal */
*ctlp++ = PCCSIG;
flags &= ~PRCSIG;
} else if (sig && sig != P->status.pr_lwp.pr_cursig) {
/* make current signal */
siginfo_t *infop;
*ctlp++ = PCSSIG;
infop = (siginfo_t *)ctlp;
(void) memset(infop, 0, sizeof (*infop));
infop->si_signo = sig;
ctlp += sizeof (siginfo_t) / sizeof (long);
}
*ctlp++ = PCRUN;
*ctlp++ = flags;
size = (char *)ctlp - (char *)ctl;
P->info_valid = 0; /* will need to update map and file info */
/*
* If we've cached ucontext-list information while we were stopped,
* free it now.
*/
if (P->ucaddrs != NULL) {
free(P->ucaddrs);
P->ucaddrs = NULL;
P->ucnelems = 0;
}
if (write(ctlfd, ctl, size) != size) {
/* If it is dead or lost, return the real status, not PS_RUN */
if (errno == ENOENT || errno == EAGAIN) {
(void) Pstopstatus(P, PCNULL, 0);
return (0);
}
/* If it is not in a jobcontrol stop, issue an error message */
if (errno != EBUSY ||
P->status.pr_lwp.pr_why != PR_JOBCONTROL) {
dprintf("Psetrun: %s\n", strerror(errno));
return (-1);
}
/* Otherwise pretend that the job-stopped process is running */
}
P->state = PS_RUN;
return (0);
}
ssize_t
Pread(struct ps_prochandle *P,
void *buf, /* caller's buffer */
size_t nbyte, /* number of bytes to read */
uintptr_t address) /* address in process */
{
return (P->ops.pop_pread(P, buf, nbyte, address, P->data));
}
ssize_t
Pread_string(struct ps_prochandle *P,
char *buf, /* caller's buffer */
size_t size, /* upper limit on bytes to read */
uintptr_t addr) /* address in process */
{
enum { STRSZ = 40 };
char string[STRSZ + 1];
ssize_t leng = 0;
int nbyte;
if (size < 2) {
errno = EINVAL;
return (-1);
}
size--; /* ensure trailing null fits in buffer */
*buf = '\0';
string[STRSZ] = '\0';
for (nbyte = STRSZ; nbyte == STRSZ && leng < size; addr += STRSZ) {
if ((nbyte = P->ops.pop_pread(P, string, STRSZ, addr,
P->data)) <= 0) {
buf[leng] = '\0';
return (leng ? leng : -1);
}
if ((nbyte = strlen(string)) > 0) {
if (leng + nbyte > size)
nbyte = size - leng;
(void) strncpy(buf + leng, string, nbyte);
leng += nbyte;
}
}
buf[leng] = '\0';
return (leng);
}
ssize_t
Pwrite(struct ps_prochandle *P,
const void *buf, /* caller's buffer */
size_t nbyte, /* number of bytes to write */
uintptr_t address) /* address in process */
{
return (P->ops.pop_pwrite(P, buf, nbyte, address, P->data));
}
int
Pclearsig(struct ps_prochandle *P)
{
int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
long ctl = PCCSIG;
if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
return (-1);
P->status.pr_lwp.pr_cursig = 0;
return (0);
}
int
Pclearfault(struct ps_prochandle *P)
{
int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
long ctl = PCCFAULT;
if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
return (-1);
return (0);
}
/*
* Set a breakpoint trap, return original instruction.
*/
int
Psetbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t *saved)
{
long ctl[1 + sizeof (priovec_t) / sizeof (long) + /* PCREAD */
1 + sizeof (priovec_t) / sizeof (long)]; /* PCWRITE */
long *ctlp = ctl;
size_t size;
priovec_t *iovp;
instr_t bpt = BPT;
instr_t old;
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE) {
errno = ENOENT;
return (-1);
}
/* fetch the old instruction */
*ctlp++ = PCREAD;
iovp = (priovec_t *)ctlp;
iovp->pio_base = &old;
iovp->pio_len = sizeof (old);
iovp->pio_offset = address;
ctlp += sizeof (priovec_t) / sizeof (long);
/* write the BPT instruction */
*ctlp++ = PCWRITE;
iovp = (priovec_t *)ctlp;
iovp->pio_base = &bpt;
iovp->pio_len = sizeof (bpt);
iovp->pio_offset = address;
ctlp += sizeof (priovec_t) / sizeof (long);
size = (char *)ctlp - (char *)ctl;
if (write(P->ctlfd, ctl, size) != size)
return (-1);
/*
* Fail if there was already a breakpoint there from another debugger
* or DTrace's user-level tracing on x86.
*/
if (old == BPT) {
errno = EBUSY;
return (-1);
}
*saved = (ulong_t)old;
return (0);
}
/*
* Restore original instruction where a breakpoint was set.
*/
int
Pdelbkpt(struct ps_prochandle *P, uintptr_t address, ulong_t saved)
{
instr_t old = (instr_t)saved;
instr_t cur;
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE) {
errno = ENOENT;
return (-1);
}
/*
* If the breakpoint instruction we had placed has been overwritten
* with a new instruction, then don't try to replace it with the
* old instruction. Doing do can cause problems with self-modifying
* code -- PLTs for example. If the Pread() fails, we assume that we
* should proceed though most likely the Pwrite() will also fail.
*/
if (Pread(P, &cur, sizeof (cur), address) == sizeof (cur) &&
cur != BPT)
return (0);
if (Pwrite(P, &old, sizeof (old), address) != sizeof (old))
return (-1);
return (0);
}
/*
* Common code for Pxecbkpt() and Lxecbkpt().
* Develop the array of requests that will do the job, then
* write them to the specified control file descriptor.
* Return the non-zero errno if the write fails.
*/
static int
execute_bkpt(
int ctlfd, /* process or LWP control file descriptor */
const fltset_t *faultset, /* current set of traced faults */
const sigset_t *sigmask, /* current signal mask */
uintptr_t address, /* address of breakpint */
ulong_t saved) /* the saved instruction */
{
long ctl[
1 + sizeof (sigset_t) / sizeof (long) + /* PCSHOLD */
1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
1 + sizeof (priovec_t) / sizeof (long) + /* PCWRITE */
2 + /* PCRUN */
1 + /* PCWSTOP */
1 + /* PCCFAULT */
1 + sizeof (priovec_t) / sizeof (long) + /* PCWRITE */
1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
1 + sizeof (sigset_t) / sizeof (long)]; /* PCSHOLD */
long *ctlp = ctl;
sigset_t unblock;
size_t size;
ssize_t ssize;
priovec_t *iovp;
sigset_t *holdp;
fltset_t *faultp;
instr_t old = (instr_t)saved;
instr_t bpt = BPT;
int error = 0;
/* block our signals for the duration */
(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
/* hold posted signals */
*ctlp++ = PCSHOLD;
holdp = (sigset_t *)ctlp;
prfillset(holdp);
prdelset(holdp, SIGKILL);
prdelset(holdp, SIGSTOP);
ctlp += sizeof (sigset_t) / sizeof (long);
/* force tracing of FLTTRACE */
if (!(prismember(faultset, FLTTRACE))) {
*ctlp++ = PCSFAULT;
faultp = (fltset_t *)ctlp;
*faultp = *faultset;
praddset(faultp, FLTTRACE);
ctlp += sizeof (fltset_t) / sizeof (long);
}
/* restore the old instruction */
*ctlp++ = PCWRITE;
iovp = (priovec_t *)ctlp;
iovp->pio_base = &old;
iovp->pio_len = sizeof (old);
iovp->pio_offset = address;
ctlp += sizeof (priovec_t) / sizeof (long);
/* clear current signal and fault; set running w/ single-step */
*ctlp++ = PCRUN;
*ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
/* wait for stop, cancel the fault */
*ctlp++ = PCWSTOP;
*ctlp++ = PCCFAULT;
/* restore the breakpoint trap */
*ctlp++ = PCWRITE;
iovp = (priovec_t *)ctlp;
iovp->pio_base = &bpt;
iovp->pio_len = sizeof (bpt);
iovp->pio_offset = address;
ctlp += sizeof (priovec_t) / sizeof (long);
/* restore fault tracing set */
if (!(prismember(faultset, FLTTRACE))) {
*ctlp++ = PCSFAULT;
*(fltset_t *)ctlp = *faultset;
ctlp += sizeof (fltset_t) / sizeof (long);
}
/* restore the hold mask */
*ctlp++ = PCSHOLD;
*(sigset_t *)ctlp = *sigmask;
ctlp += sizeof (sigset_t) / sizeof (long);
size = (char *)ctlp - (char *)ctl;
if ((ssize = write(ctlfd, ctl, size)) != size)
error = (ssize == -1)? errno : EINTR;
(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
return (error);
}
/*
* Step over a breakpoint, i.e., execute the instruction that
* really belongs at the breakpoint location (the current %pc)
* and leave the process stopped at the next instruction.
*/
int
Pxecbkpt(struct ps_prochandle *P, ulong_t saved)
{
int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
int rv, error;
if (P->state != PS_STOP) {
errno = EBUSY;
return (-1);
}
Psync(P);
error = execute_bkpt(ctlfd,
&P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold,
P->status.pr_lwp.pr_reg[R_PC], saved);
rv = Pstopstatus(P, PCNULL, 0);
if (error != 0) {
if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
error == EBUSY) { /* jobcontrol stop -- back off */
P->state = PS_RUN;
return (0);
}
if (error == ENOENT)
return (0);
errno = error;
return (-1);
}
return (rv);
}
/*
* Install the watchpoint described by wp.
*/
int
Psetwapt(struct ps_prochandle *P, const prwatch_t *wp)
{
long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
prwatch_t *cwp = (prwatch_t *)&ctl[1];
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE) {
errno = ENOENT;
return (-1);
}
ctl[0] = PCWATCH;
cwp->pr_vaddr = wp->pr_vaddr;
cwp->pr_size = wp->pr_size;
cwp->pr_wflags = wp->pr_wflags;
if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
return (-1);
return (0);
}
/*
* Remove the watchpoint described by wp.
*/
int
Pdelwapt(struct ps_prochandle *P, const prwatch_t *wp)
{
long ctl[1 + sizeof (prwatch_t) / sizeof (long)];
prwatch_t *cwp = (prwatch_t *)&ctl[1];
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE) {
errno = ENOENT;
return (-1);
}
ctl[0] = PCWATCH;
cwp->pr_vaddr = wp->pr_vaddr;
cwp->pr_size = wp->pr_size;
cwp->pr_wflags = 0;
if (write(P->ctlfd, ctl, sizeof (ctl)) != sizeof (ctl))
return (-1);
return (0);
}
/*
* Common code for Pxecwapt() and Lxecwapt(). Develop the array of requests
* that will do the job, then write them to the specified control file
* descriptor. Return the non-zero errno if the write fails.
*/
static int
execute_wapt(
int ctlfd, /* process or LWP control file descriptor */
const fltset_t *faultset, /* current set of traced faults */
const sigset_t *sigmask, /* current signal mask */
const prwatch_t *wp) /* watchpoint descriptor */
{
long ctl[
1 + sizeof (sigset_t) / sizeof (long) + /* PCSHOLD */
1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
1 + sizeof (prwatch_t) / sizeof (long) + /* PCWATCH */
2 + /* PCRUN */
1 + /* PCWSTOP */
1 + /* PCCFAULT */
1 + sizeof (prwatch_t) / sizeof (long) + /* PCWATCH */
1 + sizeof (fltset_t) / sizeof (long) + /* PCSFAULT */
1 + sizeof (sigset_t) / sizeof (long)]; /* PCSHOLD */
long *ctlp = ctl;
int error = 0;
sigset_t unblock;
sigset_t *holdp;
fltset_t *faultp;
prwatch_t *prw;
ssize_t ssize;
size_t size;
(void) sigprocmask(SIG_BLOCK, &blockable_sigs, &unblock);
/*
* Hold all posted signals in the victim process prior to stepping.
*/
*ctlp++ = PCSHOLD;
holdp = (sigset_t *)ctlp;
prfillset(holdp);
prdelset(holdp, SIGKILL);
prdelset(holdp, SIGSTOP);
ctlp += sizeof (sigset_t) / sizeof (long);
/*
* Force tracing of FLTTRACE since we need to single step.
*/
if (!(prismember(faultset, FLTTRACE))) {
*ctlp++ = PCSFAULT;
faultp = (fltset_t *)ctlp;
*faultp = *faultset;
praddset(faultp, FLTTRACE);
ctlp += sizeof (fltset_t) / sizeof (long);
}
/*
* Clear only the current watchpoint by setting pr_wflags to zero.
*/
*ctlp++ = PCWATCH;
prw = (prwatch_t *)ctlp;
prw->pr_vaddr = wp->pr_vaddr;
prw->pr_size = wp->pr_size;
prw->pr_wflags = 0;
ctlp += sizeof (prwatch_t) / sizeof (long);
/*
* Clear the current signal and fault; set running with single-step.
* Then wait for the victim to stop and cancel the FLTTRACE.
*/
*ctlp++ = PCRUN;
*ctlp++ = PRCSIG | PRCFAULT | PRSTEP;
*ctlp++ = PCWSTOP;
*ctlp++ = PCCFAULT;
/*
* Restore the current watchpoint.
*/
*ctlp++ = PCWATCH;
(void) memcpy(ctlp, wp, sizeof (prwatch_t));
ctlp += sizeof (prwatch_t) / sizeof (long);
/*
* Restore fault tracing set if we modified it.
*/
if (!(prismember(faultset, FLTTRACE))) {
*ctlp++ = PCSFAULT;
*(fltset_t *)ctlp = *faultset;
ctlp += sizeof (fltset_t) / sizeof (long);
}
/*
* Restore the hold mask to the current hold mask (i.e. the one
* before we executed any of the previous operations).
*/
*ctlp++ = PCSHOLD;
*(sigset_t *)ctlp = *sigmask;
ctlp += sizeof (sigset_t) / sizeof (long);
size = (char *)ctlp - (char *)ctl;
if ((ssize = write(ctlfd, ctl, size)) != size)
error = (ssize == -1)? errno : EINTR;
(void) sigprocmask(SIG_SETMASK, &unblock, NULL);
return (error);
}
/*
* Step over a watchpoint, i.e., execute the instruction that was stopped by
* the watchpoint, and then leave the LWP stopped at the next instruction.
*/
int
Pxecwapt(struct ps_prochandle *P, const prwatch_t *wp)
{
int ctlfd = (P->agentctlfd >= 0)? P->agentctlfd : P->ctlfd;
int rv, error;
if (P->state != PS_STOP) {
errno = EBUSY;
return (-1);
}
Psync(P);
error = execute_wapt(ctlfd,
&P->status.pr_flttrace, &P->status.pr_lwp.pr_lwphold, wp);
rv = Pstopstatus(P, PCNULL, 0);
if (error != 0) {
if (P->status.pr_lwp.pr_why == PR_JOBCONTROL &&
error == EBUSY) { /* jobcontrol stop -- back off */
P->state = PS_RUN;
return (0);
}
if (error == ENOENT)
return (0);
errno = error;
return (-1);
}
return (rv);
}
int
Psetflags(struct ps_prochandle *P, long flags)
{
int rc;
long ctl[2];
ctl[0] = PCSET;
ctl[1] = flags;
if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
rc = -1;
} else {
P->status.pr_flags |= flags;
P->status.pr_lwp.pr_flags |= flags;
rc = 0;
}
return (rc);
}
int
Punsetflags(struct ps_prochandle *P, long flags)
{
int rc;
long ctl[2];
ctl[0] = PCUNSET;
ctl[1] = flags;
if (write(P->ctlfd, ctl, 2*sizeof (long)) != 2*sizeof (long)) {
rc = -1;
} else {
P->status.pr_flags &= ~flags;
P->status.pr_lwp.pr_flags &= ~flags;
rc = 0;
}
return (rc);
}
/*
* Common function to allow clients to manipulate the action to be taken
* on receipt of a signal, receipt of machine fault, entry to a system call,
* or exit from a system call. We make use of our private prset_* functions
* in order to make this code be common. The 'which' parameter identifies
* the code for the event of interest (0 means change the entire set), and
* the 'stop' parameter is a boolean indicating whether the process should
* stop when the event of interest occurs. The previous value is returned
* to the caller; -1 is returned if an error occurred.
*/
static int
Psetaction(struct ps_prochandle *P, void *sp, size_t size,
uint_t flag, int max, int which, int stop)
{
int oldval;
if (which < 0 || which > max) {
errno = EINVAL;
return (-1);
}
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE) {
errno = ENOENT;
return (-1);
}
oldval = prset_ismember(sp, size, which) ? TRUE : FALSE;
if (stop) {
if (which == 0) {
prset_fill(sp, size);
P->flags |= flag;
} else if (!oldval) {
prset_add(sp, size, which);
P->flags |= flag;
}
} else {
if (which == 0) {
prset_empty(sp, size);
P->flags |= flag;
} else if (oldval) {
prset_del(sp, size, which);
P->flags |= flag;
}
}
if (P->state == PS_RUN)
Psync(P);
return (oldval);
}
/*
* Set action on specified signal.
*/
int
Psignal(struct ps_prochandle *P, int which, int stop)
{
int oldval;
if (which == SIGKILL && stop != 0) {
errno = EINVAL;
return (-1);
}
oldval = Psetaction(P, &P->status.pr_sigtrace, sizeof (sigset_t),
SETSIG, PRMAXSIG, which, stop);
if (oldval != -1 && which == 0 && stop != 0)
prdelset(&P->status.pr_sigtrace, SIGKILL);
return (oldval);
}
/*
* Set all signal tracing flags.
*/
void
Psetsignal(struct ps_prochandle *P, const sigset_t *set)
{
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE)
return;
P->status.pr_sigtrace = *set;
P->flags |= SETSIG;
if (P->state == PS_RUN)
Psync(P);
}
/*
* Set action on specified fault.
*/
int
Pfault(struct ps_prochandle *P, int which, int stop)
{
return (Psetaction(P, &P->status.pr_flttrace, sizeof (fltset_t),
SETFAULT, PRMAXFAULT, which, stop));
}
/*
* Set all machine fault tracing flags.
*/
void
Psetfault(struct ps_prochandle *P, const fltset_t *set)
{
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE)
return;
P->status.pr_flttrace = *set;
P->flags |= SETFAULT;
if (P->state == PS_RUN)
Psync(P);
}
/*
* Set action on specified system call entry.
*/
int
Psysentry(struct ps_prochandle *P, int which, int stop)
{
return (Psetaction(P, &P->status.pr_sysentry, sizeof (sysset_t),
SETENTRY, PRMAXSYS, which, stop));
}
/*
* Set all system call entry tracing flags.
*/
void
Psetsysentry(struct ps_prochandle *P, const sysset_t *set)
{
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE)
return;
P->status.pr_sysentry = *set;
P->flags |= SETENTRY;
if (P->state == PS_RUN)
Psync(P);
}
/*
* Set action on specified system call exit.
*/
int
Psysexit(struct ps_prochandle *P, int which, int stop)
{
return (Psetaction(P, &P->status.pr_sysexit, sizeof (sysset_t),
SETEXIT, PRMAXSYS, which, stop));
}
/*
* Set all system call exit tracing flags.
*/
void
Psetsysexit(struct ps_prochandle *P, const sysset_t *set)
{
if (P->state == PS_DEAD || P->state == PS_UNDEAD ||
P->state == PS_IDLE)
return;
P->status.pr_sysexit = *set;
P->flags |= SETEXIT;
if (P->state == PS_RUN)
Psync(P);
}
/*
* Utility function to read the contents of a file that contains a
* prheader_t at the start (/proc/pid/lstatus or /proc/pid/lpsinfo).
* Returns a malloc()d buffer or NULL on failure.
*/
static prheader_t *
read_lfile(struct ps_prochandle *P, const char *lname)
{
prheader_t *Lhp;
char lpath[PATH_MAX];
struct stat64 statb;
int fd;
size_t size;
ssize_t rval;
(void) snprintf(lpath, sizeof (lpath), "%s/%d/%s", procfs_path,
(int)P->status.pr_pid, lname);
if ((fd = open(lpath, O_RDONLY)) < 0 || fstat64(fd, &statb) != 0) {
if (fd >= 0)
(void) close(fd);
return (NULL);
}
/*
* 'size' is just the initial guess at the buffer size.
* It will have to grow if the number of lwps increases
* while we are looking at the process.
* 'size' must be larger than the actual file size.
*/
size = statb.st_size + 32;
for (;;) {
if ((Lhp = malloc(size)) == NULL)
break;
if ((rval = pread(fd, Lhp, size, 0)) < 0 ||
rval <= sizeof (prheader_t)) {
free(Lhp);
Lhp = NULL;
break;
}
if (rval < size)
break;
/* need a bigger buffer */
free(Lhp);
size *= 2;
}
(void) close(fd);
return (Lhp);
}
/*
* LWP iteration interface.
*/
int
Plwp_iter(struct ps_prochandle *P, proc_lwp_f *func, void *cd)
{
prheader_t *Lhp;
lwpstatus_t *Lsp;
long nlwp;
int rv;
switch (P->state) {
case PS_RUN:
(void) Pstopstatus(P, PCNULL, 0);
break;
case PS_STOP:
Psync(P);
break;
case PS_IDLE:
errno = ENODATA;
return (-1);
}
/*
* For either live processes or cores, the single LWP case is easy:
* the pstatus_t contains the lwpstatus_t for the only LWP.
*/
if (P->status.pr_nlwp <= 1)
return (func(cd, &P->status.pr_lwp));
/*
* For the core file multi-LWP case, we just iterate through the
* list of LWP structs we read in from the core file.
*/
if (P->state == PS_DEAD) {
core_info_t *core = P->data;
lwp_info_t *lwp = list_prev(&core->core_lwp_head);
uint_t i;
for (i = 0; i < core->core_nlwp; i++, lwp = list_prev(lwp)) {
if (lwp->lwp_psinfo.pr_sname != 'Z' &&
(rv = func(cd, &lwp->lwp_status)) != 0)
break;
}
return (rv);
}
/*
* For the live process multi-LWP case, we have to work a little
* harder: the /proc/pid/lstatus file has the array of LWP structs.
*/
if ((Lhp = Plstatus(P)) == NULL)
return (-1);
for (nlwp = Lhp->pr_nent, Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
nlwp > 0;
nlwp--, Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize)) {
if ((rv = func(cd, Lsp)) != 0)
break;
}
free(Lhp);
return (rv);
}
/*
* Extended LWP iteration interface.
* Iterate over all LWPs, active and zombie.
*/
int
Plwp_iter_all(struct ps_prochandle *P, proc_lwp_all_f *func, void *cd)
{
prheader_t *Lhp = NULL;
lwpstatus_t *Lsp;
lwpstatus_t *sp;
prheader_t *Lphp = NULL;
lwpsinfo_t *Lpsp;
long nstat;
long ninfo;
int rv;
retry:
if (Lhp != NULL)
free(Lhp);
if (Lphp != NULL)
free(Lphp);
if (P->state == PS_RUN)
(void) Pstopstatus(P, PCNULL, 0);
(void) Ppsinfo(P);
if (P->state == PS_STOP)
Psync(P);
/*
* For either live processes or cores, the single LWP case is easy:
* the pstatus_t contains the lwpstatus_t for the only LWP and
* the psinfo_t contains the lwpsinfo_t for the only LWP.
*/
if (P->status.pr_nlwp + P->status.pr_nzomb <= 1)
return (func(cd, &P->status.pr_lwp, &P->psinfo.pr_lwp));
/*
* For the core file multi-LWP case, we just iterate through the
* list of LWP structs we read in from the core file.
*/
if (P->state == PS_DEAD) {
core_info_t *core = P->data;
lwp_info_t *lwp = list_prev(&core->core_lwp_head);
uint_t i;
for (i = 0; i < core->core_nlwp; i++, lwp = list_prev(lwp)) {
sp = (lwp->lwp_psinfo.pr_sname == 'Z')? NULL :
&lwp->lwp_status;
if ((rv = func(cd, sp, &lwp->lwp_psinfo)) != 0)
break;
}
return (rv);
}
/*
* For all other cases retrieve the array of lwpstatus_t's and
* lwpsinfo_t's.
*/
if ((Lhp = Plstatus(P)) == NULL)
return (-1);
if ((Lphp = Plpsinfo(P)) == NULL) {
free(Lhp);
return (-1);
}
/*
* If we are looking at a running process, or one we do not control,
* the active and zombie lwps in the process may have changed since
* we read the process status structure. If so, just start over.
*/
if (Lhp->pr_nent != P->status.pr_nlwp ||
Lphp->pr_nent != P->status.pr_nlwp + P->status.pr_nzomb)
goto retry;
/*
* To be perfectly safe, prescan the two arrays, checking consistency.
* We rely on /proc giving us lwpstatus_t's and lwpsinfo_t's in the
* same order (the lwp directory order) in their respective files.
* We also rely on there being (possibly) more lwpsinfo_t's than
* lwpstatus_t's (the extra lwpsinfo_t's are for zombie lwps).
*/
Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
nstat = Lhp->pr_nent;
for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
if (Lpsp->pr_sname != 'Z') {
/*
* Not a zombie lwp; check for matching lwpids.
*/
if (nstat == 0 || Lsp->pr_lwpid != Lpsp->pr_lwpid)
goto retry;
Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
nstat--;
}
Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
}
if (nstat != 0)
goto retry;
/*
* Rescan, this time for real.
*/
Lsp = (lwpstatus_t *)(uintptr_t)(Lhp + 1);
Lpsp = (lwpsinfo_t *)(uintptr_t)(Lphp + 1);
for (ninfo = Lphp->pr_nent; ninfo != 0; ninfo--) {
if (Lpsp->pr_sname != 'Z') {
sp = Lsp;
Lsp = (lwpstatus_t *)((uintptr_t)Lsp + Lhp->pr_entsize);
} else {
sp = NULL;
}
if ((rv = func(cd, sp, Lpsp)) != 0)
break;
Lpsp = (lwpsinfo_t *)((uintptr_t)Lpsp + Lphp->pr_entsize);
}
free(Lhp);
free(Lphp);
return (rv);
}
core_content_t
Pcontent(struct ps_prochandle *P)
{
core_info_t *core = P->data;
if (P->state == PS_DEAD)
return (core->core_content);
if (P->state == PS_IDLE)
return (CC_CONTENT_TEXT | CC_CONTENT_DATA | CC_CONTENT_CTF);
return (CC_CONTENT_ALL);
}
/*
* =================================================================
* The remainder of the functions in this file are for the
* control of individual LWPs in the controlled process.
* =================================================================
*/
/*
* Find an entry in the process hash table for the specified lwpid.
* The entry will either point to an existing struct ps_lwphandle
* or it will point to an empty slot for a new struct ps_lwphandle.
*/
static struct ps_lwphandle **
Lfind(struct ps_prochandle *P, lwpid_t lwpid)
{
struct ps_lwphandle **Lp;
struct ps_lwphandle *L;
for (Lp = &P->hashtab[lwpid % (HASHSIZE - 1)];
(L = *Lp) != NULL; Lp = &L->lwp_hash)
if (L->lwp_id == lwpid)
break;
return (Lp);
}
/*
* Grab an LWP contained within the controlled process.
* Return an opaque pointer to its LWP control structure.
* perr: pointer to error return code.
*/
struct ps_lwphandle *
Lgrab(struct ps_prochandle *P, lwpid_t lwpid, int *perr)
{
struct ps_lwphandle **Lp;
struct ps_lwphandle *L;
int fd;
char procname[PATH_MAX];
char *fname;
int rc = 0;
(void) mutex_lock(&P->proc_lock);
if (P->state == PS_UNDEAD || P->state == PS_IDLE)
rc = G_NOPROC;
else if (P->hashtab == NULL &&
(P->hashtab = calloc(HASHSIZE, sizeof (struct ps_lwphandle *)))
== NULL)
rc = G_STRANGE;
else if (*(Lp = Lfind(P, lwpid)) != NULL)
rc = G_BUSY;
else if ((L = malloc(sizeof (struct ps_lwphandle))) == NULL)
rc = G_STRANGE;
if (rc) {
*perr = rc;
(void) mutex_unlock(&P->proc_lock);
return (NULL);
}
(void) memset(L, 0, sizeof (*L));
L->lwp_ctlfd = -1;
L->lwp_statfd = -1;
L->lwp_proc = P;
L->lwp_id = lwpid;
*Lp = L; /* insert into the hash table */
if (P->state == PS_DEAD) { /* core file */
if (getlwpstatus(P, lwpid, &L->lwp_status) == -1) {
rc = G_NOPROC;
goto err;
}
L->lwp_state = PS_DEAD;
*perr = 0;
(void) mutex_unlock(&P->proc_lock);
return (L);
}
/*
* Open the /proc/<pid>/lwp/<lwpid> files
*/
(void) snprintf(procname, sizeof (procname), "%s/%d/lwp/%d/",
procfs_path, (int)P->pid, (int)lwpid);
fname = procname + strlen(procname);
(void) set_minfd();
(void) strcpy(fname, "lwpstatus");
if ((fd = open(procname, O_RDONLY)) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
switch (errno) {
case ENOENT:
rc = G_NOPROC;
break;
default:
dprintf("Lgrab: failed to open %s: %s\n",
procname, strerror(errno));
rc = G_STRANGE;
break;
}
goto err;
}
L->lwp_statfd = fd;
if (pread(fd, &L->lwp_status, sizeof (L->lwp_status), (off_t)0) < 0) {
switch (errno) {
case ENOENT:
rc = G_NOPROC;
break;
default:
dprintf("Lgrab: failed to read %s: %s\n",
procname, strerror(errno));
rc = G_STRANGE;
break;
}
goto err;
}
(void) strcpy(fname, "lwpctl");
if ((fd = open(procname, O_WRONLY)) < 0 ||
(fd = dupfd(fd, 0)) < 0) {
switch (errno) {
case ENOENT:
rc = G_NOPROC;
break;
default:
dprintf("Lgrab: failed to open %s: %s\n",
procname, strerror(errno));
rc = G_STRANGE;
break;
}
goto err;
}
L->lwp_ctlfd = fd;
L->lwp_state =
((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
== (PR_STOPPED|PR_ISTOP))?
PS_STOP : PS_RUN;
*perr = 0;
(void) mutex_unlock(&P->proc_lock);
return (L);
err:
Lfree_internal(P, L);
*perr = rc;
(void) mutex_unlock(&P->proc_lock);
return (NULL);
}
/*
* Return a printable string corresponding to an Lgrab() error return.
*/
const char *
Lgrab_error(int error)
{
const char *str;
switch (error) {
case G_NOPROC:
str = "no such LWP";
break;
case G_BUSY:
str = "LWP already grabbed";
break;
case G_STRANGE:
str = "unanticipated system error";
break;
default:
str = "unknown error";
break;
}
return (str);
}
/*
* Free an LWP control structure.
*/
void
Lfree(struct ps_lwphandle *L)
{
struct ps_prochandle *P = L->lwp_proc;
(void) mutex_lock(&P->proc_lock);
Lfree_internal(P, L);
(void) mutex_unlock(&P->proc_lock);
}
static void
Lfree_internal(struct ps_prochandle *P, struct ps_lwphandle *L)
{
*Lfind(P, L->lwp_id) = L->lwp_hash; /* delete from hash table */
if (L->lwp_ctlfd >= 0)
(void) close(L->lwp_ctlfd);
if (L->lwp_statfd >= 0)
(void) close(L->lwp_statfd);
/* clear out the structure as a precaution against reuse */
(void) memset(L, 0, sizeof (*L));
L->lwp_ctlfd = -1;
L->lwp_statfd = -1;
free(L);
}
/*
* Return the state of the process, one of the PS_* values.
*/
int
Lstate(struct ps_lwphandle *L)
{
return (L->lwp_state);
}
/*
* Return the open control file descriptor for the LWP.
* Clients must not close this file descriptor, nor use it
* after the LWP is freed.
*/
int
Lctlfd(struct ps_lwphandle *L)
{
return (L->lwp_ctlfd);
}
/*
* Return a pointer to the LWP lwpsinfo structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Lfree().
*/
const lwpsinfo_t *
Lpsinfo(struct ps_lwphandle *L)
{
if (Plwp_getpsinfo(L->lwp_proc, L->lwp_id, &L->lwp_psinfo) == -1)
return (NULL);
return (&L->lwp_psinfo);
}
/*
* Return a pointer to the LWP status structure.
* Clients should not hold on to this pointer indefinitely.
* It will become invalid on Lfree().
*/
const lwpstatus_t *
Lstatus(struct ps_lwphandle *L)
{
return (&L->lwp_status);
}
/*
* Given an LWP handle, return the process handle.
*/
struct ps_prochandle *
Lprochandle(struct ps_lwphandle *L)
{
return (L->lwp_proc);
}
/*
* Ensure that all cached state is written to the LWP.
* The cached state is the LWP's signal mask and registers.
*/
void
Lsync(struct ps_lwphandle *L)
{
int ctlfd = L->lwp_ctlfd;
long cmd[2];
iovec_t iov[4];
int n = 0;
if (L->lwp_flags & SETHOLD) {
cmd[0] = PCSHOLD;
iov[n].iov_base = (caddr_t)&cmd[0];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&L->lwp_status.pr_lwphold;
iov[n++].iov_len = sizeof (L->lwp_status.pr_lwphold);
}
if (L->lwp_flags & SETREGS) {
cmd[1] = PCSREG;
iov[n].iov_base = (caddr_t)&cmd[1];
iov[n++].iov_len = sizeof (long);
iov[n].iov_base = (caddr_t)&L->lwp_status.pr_reg[0];
iov[n++].iov_len = sizeof (L->lwp_status.pr_reg);
}
if (n == 0 || writev(ctlfd, iov, n) < 0)
return; /* nothing to do or write failed */
L->lwp_flags &= ~(SETHOLD|SETREGS);
}
/*
* Wait for the specified LWP to stop or terminate.
* Or, just get the current status (PCNULL).
* Or, direct it to stop and get the current status (PCDSTOP).
*/
static int
Lstopstatus(struct ps_lwphandle *L,
long request, /* PCNULL, PCDSTOP, PCSTOP, PCWSTOP */
uint_t msec) /* if non-zero, timeout in milliseconds */
{
int ctlfd = L->lwp_ctlfd;
long ctl[3];
ssize_t rc;
int err;
switch (L->lwp_state) {
case PS_RUN:
break;
case PS_STOP:
if (request != PCNULL && request != PCDSTOP)
return (0);
break;
case PS_LOST:
if (request != PCNULL) {
errno = EAGAIN;
return (-1);
}
break;
case PS_UNDEAD:
case PS_DEAD:
if (request != PCNULL) {
errno = ENOENT;
return (-1);
}
break;
default: /* corrupted state */
dprintf("Lstopstatus: corrupted state: %d\n", L->lwp_state);
errno = EINVAL;
return (-1);
}
ctl[0] = PCDSTOP;
ctl[1] = PCTWSTOP;
ctl[2] = (long)msec;
rc = 0;
switch (request) {
case PCSTOP:
rc = write(ctlfd, &ctl[0], 3*sizeof (long));
break;
case PCWSTOP:
rc = write(ctlfd, &ctl[1], 2*sizeof (long));
break;
case PCDSTOP:
rc = write(ctlfd, &ctl[0], 1*sizeof (long));
break;
case PCNULL:
if (L->lwp_state == PS_DEAD)
return (0); /* Nothing else to do for cores */
break;
default: /* programming error */
errno = EINVAL;
return (-1);
}
err = (rc < 0)? errno : 0;
Lsync(L);
if (pread(L->lwp_statfd, &L->lwp_status,
sizeof (L->lwp_status), (off_t)0) < 0)
err = errno;
if (err) {
switch (err) {
case EINTR: /* user typed ctl-C */
case ERESTART:
dprintf("Lstopstatus: EINTR\n");
break;
case EAGAIN: /* we lost control of the the process */
dprintf("Lstopstatus: EAGAIN\n");
L->lwp_state = PS_LOST;
errno = err;
return (-1);
default:
if (_libproc_debug) {
const char *errstr;
switch (request) {
case PCNULL:
errstr = "Lstopstatus PCNULL"; break;
case PCSTOP:
errstr = "Lstopstatus PCSTOP"; break;
case PCDSTOP:
errstr = "Lstopstatus PCDSTOP"; break;
case PCWSTOP:
errstr = "Lstopstatus PCWSTOP"; break;
default:
errstr = "Lstopstatus PC???"; break;
}
dprintf("%s: %s\n", errstr, strerror(err));
}
L->lwp_state = PS_UNDEAD;
errno = err;
return (-1);
}
}
if ((L->lwp_status.pr_flags & (PR_STOPPED|PR_ISTOP))
!= (PR_STOPPED|PR_ISTOP)) {
L->lwp_state = PS_RUN;
if (request == PCNULL || request == PCDSTOP || msec != 0)
return (0);
dprintf("Lstopstatus: LWP is not stopped\n");
errno = EPROTO;
return (-1);
}
L->lwp_state = PS_STOP;
if (_libproc_debug) /* debugging */
prldump("Lstopstatus", &L->lwp_status);
switch (L->lwp_status.pr_why) {
case PR_SYSENTRY:
case PR_SYSEXIT:
case PR_REQUESTED:
case PR_SIGNALLED:
case PR_FAULTED:
case PR_JOBCONTROL:
case PR_SUSPENDED:
case PR_BRAND:
break;
default:
errno = EPROTO;
return (-1);
}
return (0);
}
/*
* Wait for the LWP to stop for any reason.
*/
int
Lwait(struct ps_lwphandle *L, uint_t msec)
{
return (Lstopstatus(L, PCWSTOP, msec));
}
/*
* Direct the LWP to stop; wait for it to stop.
*/
int
Lstop(struct ps_lwphandle *L, uint_t msec)
{
return (Lstopstatus(L, PCSTOP, msec));
}
/*
* Direct the LWP to stop; don't wait.
*/
int
Ldstop(struct ps_lwphandle *L)
{
return (Lstopstatus(L, PCDSTOP, 0));
}
/*
* Get the value of one register from stopped LWP.
*/
int
Lgetareg(struct ps_lwphandle *L, int regno, prgreg_t *preg)
{
if (regno < 0 || regno >= NPRGREG) {
errno = EINVAL;
return (-1);
}
if (L->lwp_state != PS_STOP) {
errno = EBUSY;
return (-1);
}
*preg = L->lwp_status.pr_reg[regno];
return (0);
}
/*
* Put value of one register into stopped LWP.
*/
int
Lputareg(struct ps_lwphandle *L, int regno, prgreg_t reg)
{
if (regno < 0 || regno >= NPRGREG) {
errno = EINVAL;
return (-1);
}
if (L->lwp_state != PS_STOP) {
errno = EBUSY;
return (-1);
}
L->lwp_status.pr_reg[regno] = reg;
L->lwp_flags |= SETREGS; /* set registers before continuing */
return (0);
}
int
Lsetrun(struct ps_lwphandle *L,
int sig, /* signal to pass to LWP */
int flags) /* PRSTEP|PRSABORT|PRSTOP|PRCSIG|PRCFAULT */
{
int ctlfd = L->lwp_ctlfd;
int sbits = (PR_DSTOP | PR_ISTOP | PR_ASLEEP);
long ctl[1 + /* PCCFAULT */
1 + sizeof (siginfo_t)/sizeof (long) + /* PCSSIG/PCCSIG */
2 ]; /* PCRUN */
long *ctlp = ctl;
size_t size;
if (L->lwp_state != PS_STOP &&
(L->lwp_status.pr_flags & sbits) == 0) {
errno = EBUSY;
return (-1);
}
Lsync(L); /* flush registers */
if (flags & PRCFAULT) { /* clear current fault */
*ctlp++ = PCCFAULT;
flags &= ~PRCFAULT;
}
if (flags & PRCSIG) { /* clear current signal */
*ctlp++ = PCCSIG;
flags &= ~PRCSIG;
} else if (sig && sig != L->lwp_status.pr_cursig) {
/* make current signal */
siginfo_t *infop;
*ctlp++ = PCSSIG;
infop = (siginfo_t *)ctlp;
(void) memset(infop, 0, sizeof (*infop));
infop->si_signo = sig;
ctlp += sizeof (siginfo_t) / sizeof (long);
}
*ctlp++ = PCRUN;
*ctlp++ = flags;
size = (char *)ctlp - (char *)ctl;
L->lwp_proc->info_valid = 0; /* will need to update map and file info */
L->lwp_proc->state = PS_RUN;
L->lwp_state = PS_RUN;
if (write(ctlfd, ctl, size) != size) {
/* Pretend that a job-stopped LWP is running */
if (errno != EBUSY || L->lwp_status.pr_why != PR_JOBCONTROL)
return (Lstopstatus(L, PCNULL, 0));
}
return (0);
}
int
Lclearsig(struct ps_lwphandle *L)
{
int ctlfd = L->lwp_ctlfd;
long ctl = PCCSIG;
if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
return (-1);
L->lwp_status.pr_cursig = 0;
return (0);
}
int
Lclearfault(struct ps_lwphandle *L)
{
int ctlfd = L->lwp_ctlfd;
long ctl = PCCFAULT;
if (write(ctlfd, &ctl, sizeof (ctl)) != sizeof (ctl))
return (-1);
return (0);
}
/*
* Step over a breakpoint, i.e., execute the instruction that
* really belongs at the breakpoint location (the current %pc)
* and leave the LWP stopped at the next instruction.
*/
int
Lxecbkpt(struct ps_lwphandle *L, ulong_t saved)
{
struct ps_prochandle *P = L->lwp_proc;
int rv, error;
if (L->lwp_state != PS_STOP) {
errno = EBUSY;
return (-1);
}
Lsync(L);
error = execute_bkpt(L->lwp_ctlfd,
&P->status.pr_flttrace, &L->lwp_status.pr_lwphold,
L->lwp_status.pr_reg[R_PC], saved);
rv = Lstopstatus(L, PCNULL, 0);
if (error != 0) {
if (L->lwp_status.pr_why == PR_JOBCONTROL &&
error == EBUSY) { /* jobcontrol stop -- back off */
L->lwp_state = PS_RUN;
return (0);
}
if (error == ENOENT)
return (0);
errno = error;
return (-1);
}
return (rv);
}
/*
* Step over a watchpoint, i.e., execute the instruction that was stopped by
* the watchpoint, and then leave the LWP stopped at the next instruction.
*/
int
Lxecwapt(struct ps_lwphandle *L, const prwatch_t *wp)
{
struct ps_prochandle *P = L->lwp_proc;
int rv, error;
if (L->lwp_state != PS_STOP) {
errno = EBUSY;
return (-1);
}
Lsync(L);
error = execute_wapt(L->lwp_ctlfd,
&P->status.pr_flttrace, &L->lwp_status.pr_lwphold, wp);
rv = Lstopstatus(L, PCNULL, 0);
if (error != 0) {
if (L->lwp_status.pr_why == PR_JOBCONTROL &&
error == EBUSY) { /* jobcontrol stop -- back off */
L->lwp_state = PS_RUN;
return (0);
}
if (error == ENOENT)
return (0);
errno = error;
return (-1);
}
return (rv);
}
int
Lstack(struct ps_lwphandle *L, stack_t *stkp)
{
struct ps_prochandle *P = L->lwp_proc;
uintptr_t addr = L->lwp_status.pr_ustack;
if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
if (Pread(P, stkp, sizeof (*stkp), addr) != sizeof (*stkp))
return (-1);
#ifdef _LP64
} else {
stack32_t stk32;
if (Pread(P, &stk32, sizeof (stk32), addr) != sizeof (stk32))
return (-1);
stack_32_to_n(&stk32, stkp);
#endif
}
return (0);
}
int
Lmain_stack(struct ps_lwphandle *L, stack_t *stkp)
{
struct ps_prochandle *P = L->lwp_proc;
if (Lstack(L, stkp) != 0)
return (-1);
/*
* If the SS_ONSTACK flag is set then this LWP is operating on the
* alternate signal stack. We can recover the original stack from
* pr_oldcontext.
*/
if (!(stkp->ss_flags & SS_ONSTACK))
return (0);
if (P->status.pr_dmodel == PR_MODEL_NATIVE) {
ucontext_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
if (Pread(P, stkp, sizeof (*stkp),
(uintptr_t)&ctxp->uc_stack) != sizeof (*stkp))
return (-1);
#ifdef _LP64
} else {
ucontext32_t *ctxp = (void *)L->lwp_status.pr_oldcontext;
stack32_t stk32;
if (Pread(P, &stk32, sizeof (stk32),
(uintptr_t)&ctxp->uc_stack) != sizeof (stk32))
return (-1);
stack_32_to_n(&stk32, stkp);
#endif
}
return (0);
}
int
Lalt_stack(struct ps_lwphandle *L, stack_t *stkp)
{
if (L->lwp_status.pr_altstack.ss_flags & SS_DISABLE) {
errno = ENODATA;
return (-1);
}
*stkp = L->lwp_status.pr_altstack;
return (0);
}
/*
* Add a mapping to the given proc handle. Resizes the array as appropriate and
* manages reference counts on the given file_info_t.
*
* The 'map_relocate' member is used to tell Psort_mappings() that the
* associated file_map pointer needs to be relocated after the mappings have
* been sorted. It is only set for the first mapping, and has no meaning
* outside these two functions.
*/
int
Padd_mapping(struct ps_prochandle *P, off64_t off, file_info_t *fp,
prmap_t *pmap)
{
map_info_t *mp;
if (P->map_count == P->map_alloc) {
size_t next = P->map_alloc ? P->map_alloc * 2 : 16;
if ((P->mappings = realloc(P->mappings,
next * sizeof (map_info_t))) == NULL)
return (-1);
P->map_alloc = next;
}
mp = &P->mappings[P->map_count++];
mp->map_offset = off;
mp->map_pmap = *pmap;
mp->map_relocate = 0;
if ((mp->map_file = fp) != NULL) {
if (fp->file_map == NULL) {
fp->file_map = mp;
mp->map_relocate = 1;
}
fp->file_ref++;
}
return (0);
}
static int
map_sort(const void *a, const void *b)
{
const map_info_t *ap = a, *bp = b;
if (ap->map_pmap.pr_vaddr < bp->map_pmap.pr_vaddr)
return (-1);
else if (ap->map_pmap.pr_vaddr > bp->map_pmap.pr_vaddr)
return (1);
else
return (0);
}
/*
* Sort the current set of mappings. Should be called during target
* initialization after all calls to Padd_mapping() have been made.
*/
void
Psort_mappings(struct ps_prochandle *P)
{
int i;
map_info_t *mp;
qsort(P->mappings, P->map_count, sizeof (map_info_t), map_sort);
/*
* Update all the file_map pointers to refer to the new locations.
*/
for (i = 0; i < P->map_count; i++) {
mp = &P->mappings[i];
if (mp->map_relocate)
mp->map_file->file_map = mp;
mp->map_relocate = 0;
}
}
struct ps_prochandle *
Pgrab_ops(pid_t pid, void *data, const ps_ops_t *ops, int flags)
{
struct ps_prochandle *P;
if ((P = calloc(1, sizeof (*P))) == NULL) {
return (NULL);
}
Pinit_ops(&P->ops, ops);
(void) mutex_init(&P->proc_lock, USYNC_THREAD, NULL);
P->pid = pid;
P->state = PS_STOP;
P->asfd = -1;
P->ctlfd = -1;
P->statfd = -1;
P->agentctlfd = -1;
P->agentstatfd = -1;
Pinitsym(P);
P->data = data;
Pread_status(P);
if (flags & PGRAB_INCORE) {
P->flags |= INCORE;
}
return (P);
}
|