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 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="keywords" content="uftrace,ftrace,tracing,profiling" />
<meta name="description" content="A function (graph) tracer for C/C++/Rust/Python programs." />
<title>uftrace</title>
<style>
@import url(https://fonts.googleapis.com/css?family=Droid+Serif);
@import url(https://fonts.googleapis.com/css?family=Yanone+Kaffeesatz);
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
body {
font-family: 'Droid Serif';
//font-family: 'Roboto', sans-serif;
background-color: black;
}
textarea {
display: none;
}
h1, h2, h3, h4 {
font-family: 'Yanone Kaffeesatz';
//font-family: 'Roboto', sans-serif;
//font-family: 'Arials', serif;
font-weight: 400;
margin-bottom: 0;
}
.remark-slide-content h1 { font-size: 85px; min-height: 87px; }
.remark-slide-content h2 { font-size: 60px; min-height: 62px; }
.remark-slide-content h3 { font-size: 35px; min-height: 37px; }
.remark-slide-content h4 { font-size: 30px; min-height: 32px; }
.remark-slide-content {
background-color: #000;
background-size: cover;
background-position: center;
color: #f8f8f8;
font-size: 24px;
text-shadow: 0px 0px 16px #000, 0px 0px 32px #000;
padding: 10px 20px 10px 20px;
}
.remark-slide-number { font-size: 20px }
.footnote {
font-size: 22px;
position: absolute;
bottom: 1.8em;
right: 1em;
}
.remark-container {
background-color: #000;
}
.remark-slide-scaler {
-moz-box-shadow: 0 0 30px #000;
-webkit-box-shadow: 0 0 30px #000;
box-shadow: 0 0 30px #000;
}
li p { line-height: 1.25em; }
.large { font-size: 2em; }
a, a > code {
color: rgb(100, 100, 255);
text-decoration: none;
}
/*
code {
background: #e7e8e2;
border-radius: 5px;
}
*/
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono', monospace; }
.remark-code-span-highlighted { background-color: #000; color: red; padding: 0px; }
.remark-code-line-highlighted { background-color: #600; }
.remark-code { font-size: 22px; }
.remark-code-line { min-height: 24px; }
.picture {
align: center;
width: 680px;
background-color: #FFF;
}
.center-image {
background-color: #FFF;
margin: 0 auto;
display: block;
}
/* Two-column layout */
.left-column {
float: left;
width: 49%;
}
.right-column {
float: right;
width: 49%;
}
/* Two-column layout */
.left30-column {
float: left;
width: 30%;
}
.right70-column {
float: right;
width: 70%;
}
/* Code Font Size */
.font-20px { font-size: 20px; }
.font-22px { font-size: 22px; }
.code-3px .remark-code-line { font-size: 3px; min-height: 5px; }
.code-5px .remark-code-line { font-size: 5px; min-height: 7px; }
.code-7px .remark-code-line { font-size: 7px; min-height: 9px; }
.code-8px .remark-code-line { font-size: 8px; min-height: 10px; }
.code-9px .remark-code-line { font-size: 9px; min-height: 11px; }
.code-10px .remark-code-line { font-size: 10px; min-height: 12px; }
.code-11px .remark-code-line { font-size: 11px; min-height: 13px; }
.code-12px .remark-code-line { font-size: 12px; min-height: 14px; }
.code-13px .remark-code-line { font-size: 13px; min-height: 15px; }
.code-14px .remark-code-line { font-size: 14px; min-height: 16px; }
.code-15px .remark-code-line { font-size: 15px; min-height: 17px; }
.code-16px .remark-code-line { font-size: 16px; min-height: 18px; }
.code-17px .remark-code-line { font-size: 17px; min-height: 19px; }
.code-18px .remark-code-line { font-size: 18px; min-height: 20px; }
.code-19px .remark-code-line { font-size: 19px; min-height: 21px; }
.code-20px .remark-code-line { font-size: 20px; min-height: 22px; }
.code-21px .remark-code-line { font-size: 21px; min-height: 23px; }
.code-22px .remark-code-line { font-size: 22px; min-height: 24px; }
.code-23px .remark-code-line { font-size: 23px; min-height: 25px; }
.code-24px .remark-code-line { font-size: 24px; min-height: 26px; }
.code-25px .remark-code-line { font-size: 25px; min-height: 27px; }
.code-26px .remark-code-line { font-size: 26px; min-height: 28px; }
.code-27px .remark-code-line { font-size: 27px; min-height: 29px; }
.code-28px .remark-code-line { font-size: 28px; min-height: 30px; }
/* Colors */
.red { color: red; }
.green { color: green; }
.blue { color: blue; }
.yellow { color: yellow; }
.orange { color: orange; }
.grey { color: grey; }
.cyan { color: cyan; }
.magenta { color: magenta; }
x-red { color: red; }
x-green { color: green; }
x-blue { color: blue; }
x-yellow { color: yellow; }
x-orange { color: orange; }
x-grey { color: grey; }
x-cyan { color: cyan; }
x-magenta { color: magenta; }
/* for TUI pages */
.tui .remark-code-line {
font-family: 'Courier New';
font-size: 14px;
min-height: 16px;
}
x-tui-blue-line { background-color: blue; }
x-tui-white-line { background-color: white; color: black; }
.tui2 .remark-code-line {
font-family: 'Courier New';
font-size: 12px;
min-height: 14px;
}
x-tui-blue-line2 { background-color: blue; }
x-tui-white-line2 { background-color: white; color: black; }
</style>
</head>
<body>
<textarea id="source">
name: title-layout
layout: true
class: center, middle, title
---
name: picture-layout
layout: true
class: center, middle
---
name: basic-layout
layout: true
class: left, top
---
name: title
template: title-layout
# uftrace
### A function (graph) tracer for C/C++/Rust/Python programs
### [https://github.com/namhyung/uftrace](https://github.com/namhyung/uftrace)
.footnote[[https://uftrace.github.io](https://uftrace.github.io)]
---
name: toc
class: font-22px
### Table of Contents
- [Installation](#installation)
- [Basic Tracing](#basic-tracing)
- [(user) Function Tracing](#user-tracing)
- [Library Function Tracing](#lib-tracing)
- [Linux Kernel Function Tracing](#kernel-tracing)
- [Event Tracing](#event-tracing)
- Filters and Arguments
- [Filters](#filters)
- [Display Function Arguments](#args)
- [Arguments Detection with Debug Info](#autoargs)
- Recorded Data Analysis
- [report command](#report)
- [dump command](#dump)
- [graph command](#graph)
- [tui command](#tui)
- Advanced Topics
- [(Python) Scripting](#python)
- [Dynamic Tracing](#dynamic-tracing)
- [Full Dynamic Tracing](#full-dynamic-tracing)
- [Triggers for Trace Control](#trace-control)
- [Python Function Tracing](#python-tracing)
---
### uftrace
- The [uftrace](https://github.com/namhyung/uftrace) tool is to trace and analyze execution of a program written in C/C++.
- It was heavily inspired by the [ftrace](https://www.kernel.org/doc/Documentation/trace/ftrace.txt) framework of the Linux kernel (especially function graph tracer) and supports userspace programs.
- It supports various kind of commands and filters to help analysis of the program execution and performance.
---
template: picture-layout
<img src="https://raw.githubusercontent.com/namhyung/uftrace/master/doc/uftrace-live-demo.gif" class="center-image" width="680">
---
name: installation
### Quick Installation
.footnote[[INSTALL.md](https://github.com/namhyung/uftrace/blob/master/INSTALL.md)]
<pre>
<code class="bash">$ git clone https://github.com/namhyung/uftrace && cd uftrace
$ ./configure
uftrace detected system features:
... prefix: /usr/local
... libelf: [ .green[on] ] - more flexible ELF data handling
... libdw: [ .red[OFF] ] - DWARF debug info support
... libpython: [ .red[OFF] ] - python tracing & scripting support
... libluajit: [ .red[OFF] ] - luajit scripting support
... libncursesw: [ .red[OFF] ] - TUI support
... cxa_demangle: [ .green[on] ] - full demangler support with libstdc++
... perf_event: [ .green[on] ] - perf (PMU) event support
... schedule: [ .green[on] ] - scheduler event support
... capstone: [ .red[OFF] ] - full dynamic tracing support
$ make
$ sudo make install
</code>
</pre>
---
### Quick Installation
.footnote[[INSTALL.md](https://github.com/namhyung/uftrace/blob/master/INSTALL.md)]
<pre>
<code class="bash">$ git clone https://github.com/namhyung/uftrace && cd uftrace
# To install required packages
$ sudo <a href="https://raw.githubusercontent.com/namhyung/uftrace/master/misc/install-deps.sh">./misc/install-deps.sh</a>
$ ./configure
uftrace detected system features:
... prefix: /usr/local
... libelf: [ .green[on] ] - more flexible ELF data handling
... libdw: [ .green[on] ] - DWARF debug info support
... libpython: [ .green[on] ] - python tracing & scripting support
... libluajit: [ .green[on] ] - luajit scripting support
... libncursesw: [ .green[on] ] - TUI support
... cxa_demangle: [ .green[on] ] - full demangler support with libstdc++
... perf_event: [ .green[on] ] - perf (PMU) event support
... schedule: [ .green[on] ] - scheduler event support
... capstone: [ .green[on] ] - full dynamic tracing support
$ make
$ sudo make install
</code>
</pre>
---
name: basic-tracing
template: title-layout
# Basic Tracing
### record and replay
---
### uftrace
- uftrace is able to trace
- C/C++ (user space) functions
- compiled with -pg or -finstrument-functions
- Library functions
- Linux kernel functions
- Some of system events
---
name: user-tracing
### C/C++ (user) Function Tracing
- Compiler assists tracing with -pg or -finstrument-functions
.left-column[
```
$ cat foobar.c
void bar() {
}
void foo() {
bar();
}
int main() {
foo();
bar();
}
```
]
.right-column[
```
```
]
---
### C/C++ (user) Function Tracing
- Compiler assists tracing with -pg or -finstrument-functions
.left-column[
```
$ cat foobar.c
void bar() {
}
void foo() {
bar();
}
int main() {
foo();
bar();
}
```
]
.right-column[
```
$ gcc foobar.c
<bar>:
ret
<foo>:
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
---
### C/C++ (user) Function Tracing
- Compiler assists tracing with -pg or -finstrument-functions
.left-column[
```
$ cat foobar.c
void bar() {
}
void foo() {
bar();
}
int main() {
foo();
bar();
}
```
]
.right-column[
```
$ gcc `-pg` foobar.c
<bar>:
* call <mcount@plt>
ret
<foo>:
* call <mcount@plt>
call <bar>
ret
<main>:
* call <mcount@plt>
call <foo>
call <bar>
ret
```
]
---
### C/C++ (user) Function Tracing
- Compiler assists tracing with -pg or -finstrument-functions
.left-column[
```
$ cat foobar.c
void bar() {
}
void foo() {
bar();
}
int main() {
foo();
bar();
}
```
]
.right-column[
```
$ gcc `-finstrument-functions` foobar.c
<bar>:
* call <__cyg_profile_func_enter@plt>
* call <__cyg_profile_func_exit@plt>
ret
<foo>:
* call <__cyg_profile_func_enter@plt>
call <bar>
* call <__cyg_profile_func_exit@plt>
ret
<main>:
* call <__cyg_profile_func_enter@plt>
call <foo>
call <bar>
* call <__cyg_profile_func_exit@plt>
ret
```
]
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace record
- Run a command and record its trace data
```
$ gcc foobar.c
```
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace record
- Run a command and record its trace data
```
$ gcc `-pg` foobar.c
```
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace record
- Run a command and record its trace data
```
$ gcc -pg foobar.c
$ ./a.out
```
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace record
- Run a command and record its trace data
```
$ gcc -pg foobar.c
$ uftrace `record` a.out
```
---
class: code-26px
### C/C++ (user) Function Tracing
.footnote[[uftrace.data](https://github.com/namhyung/uftrace/wiki/Data-Format) contains recorded data]
- uftrace record
- Run a command and record its trace data
<pre><code> $ gcc -pg foobar.c
$ uftrace record a.out
$ ls
<x-green>a.out</x-green> foobar.c <a href="https://github.com/namhyung/uftrace/wiki/Data-Format">uftrace.data</a>
</code></pre>
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace record
- Run a command and record its trace data
```
$ gcc -pg foobar.c
$ uftrace record a.out
```
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace replay
- Print recorded function trace
```
$ gcc -pg foobar.c
$ uftrace record a.out
$ uftrace `replay`
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
```
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace live
- Trace functions both doing record and replay
```
$ gcc -pg foobar.c
$ uftrace `live` a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
```
---
class: code-26px
### C/C++ (user) Function Tracing
- uftrace .grey[(live)]
- .red[live] can be omitted
```
$ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
```
---
name: lib-tracing
### Library Function Tracing
- Library Function Tracing works via PLT hooking
- PLT contains trampoline code for calling shared functions
```
$ gcc -pg foobar.c
```
.left-column[
```
void bar() {
}
void foo() {
bar();
}
int main() {
foo();
bar();
}
```
]
.right-column[
```
<bar>:
call <mcount@plt>
ret
<foo>:
call <mcount@plt>
call <bar>
ret
<main>:
call <mcount@plt>
call <foo>
call <bar>
ret
```
]
---
### Library Function Tracing
- Library Function Tracing works via .red[PLT hooking]
- PLT contains trampoline code for calling shared functions
```
$ gcc -pg foobar.c
```
.left-column[
```
void bar() {
`getpid()`;
}
void foo() {
bar();
}
int main() {
foo();
bar();
}
```
]
.right-column[
```
<bar>:
call <mcount@plt>
* call <getpid@plt> # call to PLT
ret
<foo>:
call <mcount@plt>
call <bar>
ret
<main>:
call <mcount@plt>
call <foo>
call <bar>
ret
```
]
---
class: code-26px
### Library Function Tracing
- Library Function Tracing works via PLT hooking
- PLT contains trampoline code for calling shared functions
```
$ gcc -pg foobar.c
```
---
class: code-26px
### Library Function Tracing
- Library Function Tracing works via PLT hooking
- PLT contains trampoline code for calling shared functions
```
$ gcc -pg foobar.c
$ uftrace a.out
```
---
class: code-26px
### Library Function Tracing
- Library Function Tracing works via PLT hooking
- PLT contains trampoline code for calling shared functions
```
$ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112962] | main() {
[112962] | foo() {
[112962] | bar() {
0.863 us [112962] | `getpid`();
1.774 us [112962] | } /* bar */
2.334 us [112962] | } /* foo */
[112962] | bar() {
0.176 us [112962] | `getpid`();
0.666 us [112962] | } /* bar */
3.663 us [112962] | } /* main */
```
---
class: code-26px
### Library Function Tracing
- --no-libcall
- Do not record library function invocations.
```
$ gcc -pg foobar.c
$ uftrace `--no-libcall` a.out
# DURATION TID FUNCTION
[112962] | main() {
[112962] | foo() {
[112962] | bar() {
// 0.863 us [112962] | getpid();
1.774 us [112962] | } /* bar */
2.334 us [112962] | } /* foo */
[112962] | bar() {
// 0.176 us [112962] | getpid();
0.666 us [112962] | } /* bar */
3.663 us [112962] | } /* main */
```
---
class: code-26px
### Library Function Tracing
- --no-libcall
- Do not record library function invocations.
```
$ gcc -pg foobar.c
$ uftrace `--no-libcall` a.out
# DURATION TID FUNCTION
[112962] | main() {
[112962] | foo() {
1.774 us [112962] | bar();
2.334 us [112962] | } /* foo */
0.666 us [112962] | bar();
3.663 us [112962] | } /* main */
```
---
class: code-26px
### Library Function Tracing
- C++ example
- new and delete library calls
```
$ cat new-delete.cpp
int main()
{
int* p = new int;
delete p;
}
```
---
class: code-26px
### Library Function Tracing
- C++ example
- new and delete library calls
```
$ cat new-delete.cpp
int main()
{
int* p = new int;
delete p;
}
$ g++ -pg new-delete.cpp
```
---
class: code-26px
### Library Function Tracing
- C++ example
- It shows .red[operator new] and .red[operator delete]
```
$ uftrace a.out
# DURATION TID FUNCTION
[180964] | main() {
2.420 us [180964] | `operator new`();
3.170 us [180964] | `operator delete`();
11.220 us [180964] | } /* main */
```
---
class: code-26px
### Library Function Tracing
- --nest-libcall
- Trace function calls between libraries.
```
$ uftrace `--nest-libcall` a.out
# DURATION TID FUNCTION
[180978] | main() {
[180978] | operator new() {
4.250 us [180978] | } /* operator new */
[180978] | operator delete() {
3.547 us [180978] | } /* operator delete */
11.857 us [180978] | } /* main */
```
---
class: code-26px
### Library Function Tracing
- --nest-libcall
- Trace function calls between libraries.
```
$ uftrace `--nest-libcall` a.out
# DURATION TID FUNCTION
[180978] | main() {
[180978] | operator new() {
0.957 us [180978] | `malloc`();
4.250 us [180978] | } /* operator new */
[180978] | operator delete() {
1.540 us [180978] | `free`();
3.547 us [180978] | } /* operator delete */
11.857 us [180978] | } /* main */
```
---
name: kernel-tracing
class: code-26px
### Linux Kernel Function Tracing
- -k, --kernel
- Trace kernel functions as well as user functions.
```
$ gcc -pg hello.c
```
---
class: code-26px
### Linux Kernel Function Tracing
- -k, --kernel
- Trace kernel functions as well as user functions.
```
$ gcc -pg hello.c
$ uftrace a.out
Hello World!
# DURATION TID FUNCTION
[161960] | main() {
7.267 us [161960] | printf();
6.920 us [161960] | fflush();
15.694 us [161960] | } /* main */
```
---
class: code-26px
### Linux Kernel Function Tracing
- -k, --kernel
- Trace kernel functions as well as user functions.
```
$ gcc -pg hello.c
$ `sudo` uftrace `-k` a.out
Hello World!
# DURATION TID FUNCTION
[161968] | main() {
[161968] | printf() {
32.457 us [161968] | } /* printf */
[161968] | fflush() {
25.244 us [161968] | } /* fflush */
59.221 us [161968] | } /* main */
```
---
class: code-26px
### Linux Kernel Function Tracing
- -k, --kernel
- Trace kernel functions as well as user functions.
```
$ gcc -pg hello.c
$ `sudo` uftrace `-k` a.out
Hello World!
# DURATION TID FUNCTION
[161968] | main() {
[161968] | printf() {
7.770 us [161968] | `sys_newfstat`();
11.737 us [161968] | `__do_page_fault`();
32.457 us [161968] | } /* printf */
[161968] | fflush() {
18.637 us [161968] | `sys_write`();
25.244 us [161968] | } /* fflush */
59.221 us [161968] | } /* main */
```
---
class: code-18px
### Linux Kernel Function Tracing
- -K DEPTH, --kernel-depth=DEPTH
- Set kernel max function depth separately.
```
$ sudo uftrace `-K 2` a.out
Hello World!
```
---
class: code-18px
### Linux Kernel Function Tracing
- -K DEPTH, --kernel-depth=DEPTH
- Set kernel max function depth separately.
```
$ sudo uftrace `-K 2` a.out
Hello World!
# DURATION TID FUNCTION
[162954] | main() {
[162954] | printf() {
[162954] | `sys_newfstat`() {
3.074 us [162954] | `vfs_fstat`();
1.200 us [162954] | `cp_new_stat`();
6.914 us [162954] | } /* sys_newfstat */
[162954] | `__do_page_fault`() {
0.643 us [162954] | `down_read_trylock`();
0.630 us [162954] | `_cond_resched`();
2.440 us [162954] | `find_vma`();
9.697 us [162954] | `handle_mm_fault`();
0.690 us [162954] | `up_read`();
21.813 us [162954] | } /* __do_page_fault */
42.064 us [162954] | } /* printf */
[162954] | fflush() {
[162954] | `sys_write`() {
0.933 us [162954] | `__fdget_pos`();
18.310 us [162954] | `vfs_write`();
27.117 us [162954] | } /* sys_write */
30.673 us [162954] | } /* fflush */
74.223 us [162954] | } /* main */
```
---
name: event-tracing
class: code-20px
### Event Tracing (scheduling event)
<pre>
<code class="plain"> $ uftrace tests/t-fork
# DURATION TID FUNCTION
[ 14528] | main() {
127.033 us [ 14528] | fork();
[ 14528] | wait() {
[ 14528] | /* <x-green>linux:sched-out</x-green> */
[ 14540] | } /* fork */
[ 14540] | a() {
[ 14540] | b() {
[ 14540] | c() {
1.507 us [ 14540] | getpid();
2.987 us [ 14540] | } /* c */
3.464 us [ 14540] | } /* b */
3.854 us [ 14540] | } /* a */
13.394 us [ 14540] | } /* main */
[ 14528] | /* <x-green>linux:sched-in</x-green> */
799.270 us [ 14528] | } /* wait */
[ 14528] | a() {
[ 14528] | b() {
[ 14528] | c() {
2.410 us [ 14528] | getpid();
3.470 us [ 14528] | } /* c */
3.833 us [ 14528] | } /* b */
4.144 us [ 14528] | } /* a */
952.797 us [ 14528] | } /* main */
</code>
</pre>
---
class: code-20px
### Event Tracing (scheduling event)
<pre>
<code class="plain"> $ uftrace `--no-event` tests/t-fork
# DURATION TID FUNCTION
[ 14528] | main() {
127.033 us [ 14528] | fork();
[ 14528] | wait() {
[ 14540] | } /* fork */
[ 14540] | a() {
[ 14540] | b() {
[ 14540] | c() {
1.507 us [ 14540] | getpid();
2.987 us [ 14540] | } /* c */
3.464 us [ 14540] | } /* b */
3.854 us [ 14540] | } /* a */
13.394 us [ 14540] | } /* main */
799.270 us [ 14528] | } /* wait */
[ 14528] | a() {
[ 14528] | b() {
[ 14528] | c() {
2.410 us [ 14528] | getpid();
3.470 us [ 14528] | } /* c */
3.833 us [ 14528] | } /* b */
4.144 us [ 14528] | } /* a */
952.797 us [ 14528] | } /* main */
</code>
</pre>
---
name: filters
template: title-layout
# Filters
.footnote[[wiki/Filters](https://github.com/namhyung/uftrace/wiki/Filters)]
---
class: code-26px
### Filters
- -D DEPTH, --depth=DEPTH
- Set global trace limit in nesting level.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -D DEPTH, --depth=DEPTH
- Set global trace limit in nesting level.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-D 2` a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
// 0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -D DEPTH, --depth=DEPTH
- Set global trace limit in nesting level.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-D 2` a.out
# DURATION TID FUNCTION
[112643] | main() {
1.083 us [112643] | foo();
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -F FUNC, --filter=FUNC
- Set filter to trace selected functions and their children functions.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -F FUNC, --filter=FUNC
- Set filter to trace selected functions and their children functions.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-F foo` a.out
# DURATION TID FUNCTION
// [112643] | main() {
[112643] | `foo`() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
// 0.127 us [112643] | bar();
// 2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -F FUNC, --filter=FUNC
- Set filter to trace selected functions and their children functions.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-F foo` a.out
# DURATION TID FUNCTION
[112643] | `foo`() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
</code></pre>
---
class: code-26px
### Filters
- -C FUNC, --caller-filter=FUNC
- Set filter to trace callers of selected functions only.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -C FUNC, --caller-filter=FUNC
- Set filter to trace callers of selected functions only.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-C foo` a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | `foo`() {
// 0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
// 0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -C FUNC, --caller-filter=FUNC
- Set filter to trace callers of selected functions only.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-C foo` a.out
# DURATION TID FUNCTION
[112643] | main() {
1.083 us [112643] | `foo`();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -N FUNC, --notrace=FUNC
- Set filter not to trace selected functions and their children functions.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -N FUNC, --notrace=FUNC
- Set filter not to trace selected functions and their children functions.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-N foo` a.out
# DURATION TID FUNCTION
[112643] | main() {
// [112643] | `foo`() {
// 0.190 us [112643] | bar();
// 1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -N FUNC, --notrace=FUNC
- Set filter not to trace selected functions and their children functions.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-N foo` a.out
# DURATION TID FUNCTION
[112643] | main() {
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -H FUNC, --hide=FUNC
- Set filter not to trace selected functions only.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -H FUNC, --hide=FUNC
- Set filter not to trace selected functions only.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-H foo` a.out
# DURATION TID FUNCTION
[112643] | main() {
// [112643] | `foo`() {
0.190 us [112643] | bar();
// 1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -H FUNC, --hide=FUNC
- Set filter not to trace selected functions only.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-H foo` a.out
# DURATION TID FUNCTION
[112643] | main() {
0.190 us [112643] | bar();
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -t TIME, --time-filter=TIME
- Do not show small functions under the time threshold.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
0.190 us [112643] | bar();
1.083 us [112643] | } /* foo */
0.127 us [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -t TIME, --time-filter=TIME
- Do not show small functions under the time threshold.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-t 200ns` a.out
# DURATION TID FUNCTION
[112643] | main() {
[112643] | foo() {
// `0.190 us` [112643] | bar();
1.083 us [112643] | } /* foo */
// `0.127 us` [112643] | bar();
2.050 us [112643] | } /* main */
</code></pre>
---
class: code-26px
### Filters
- -t TIME, --time-filter=TIME
- Do not show small functions under the time threshold.
<pre><code class="plain"> $ gcc -pg foobar.c
$ uftrace `-t 200ns` a.out
# DURATION TID FUNCTION
[112643] | main() {
1.083 us [112643] | foo();
2.050 us [112643] | } /* main */
</code></pre>
---
name: args
template: title-layout
## Display Function Arguments
.footnote[[wiki/Arguments](https://github.com/namhyung/uftrace/wiki/Arguments)]
---
### Display Function Arguments
```
$ gcc -pg fibonacci.c
```
---
### Display Function Arguments
```
$ gcc -pg fibonacci.c
$ uftrace a.out 5
fib(5) = 5
```
---
### Display Function Arguments
```
$ gcc -pg fibonacci.c
$ uftrace a.out 5
fib(5) = 5
# DURATION TID FUNCTION
[31321] | main() {
1.478 us [31321] | atoi();
[31321] | fib() {
[31321] | fib() {
[31321] | fib() {
0.155 us [31321] | fib();
0.123 us [31321] | fib();
0.883 us [31321] | } /* fib */
0.125 us [31321] | fib();
1.483 us [31321] | } /* fib */
[31321] | fib() {
0.125 us [31321] | fib();
0.125 us [31321] | fib();
0.774 us [31321] | } /* fib */
2.716 us [31321] | } /* fib */
4.382 us [31321] | printf();
9.456 us [31321] | } /* main */
```
---
### Record function arguments with -A / --argument
```
$ gcc -pg fibonacci.c
$ uftrace `-A fib@arg1` a.out 5
fib(5) = 5
# DURATION TID FUNCTION
[31321] | main() {
1.478 us [31321] | atoi();
[31321] | fib(`5`) {
[31321] | fib(`4`) {
[31321] | fib(`3`) {
0.155 us [31321] | fib(`2`);
0.123 us [31321] | fib(`1`);
0.883 us [31321] | } /* fib */
0.125 us [31321] | fib(`2`);
1.483 us [31321] | } /* fib */
[31321] | fib(`3`) {
0.125 us [31321] | fib(`2`);
0.125 us [31321] | fib(`1`);
0.774 us [31321] | } /* fib */
2.716 us [31321] | } /* fib */
4.382 us [31321] | printf();
9.456 us [31321] | } /* main */
```
---
### Record function return values with -R / --retval
```
$ gcc -pg fibonacci.c
$ uftrace -A fib@arg1 `-R fib@retval` a.out 5
fib(5) = 5
# DURATION TID FUNCTION
[31321] | main() {
1.478 us [31321] | atoi();
[31321] | fib(5) {
[31321] | fib(4) {
[31321] | fib(3) {
0.155 us [31321] | fib(2) `= 1`;
0.123 us [31321] | fib(1) `= 1`;
0.883 us [31321] | } `= 2`; /* fib */
0.125 us [31321] | fib(2) `= 1`;
1.483 us [31321] | } `= 3`; /* fib */
[31321] | fib(3) {
0.125 us [31321] | fib(2) `= 1`;
0.125 us [31321] | fib(1) `= 1`;
0.774 us [31321] | } `= 2`; /* fib */
2.716 us [31321] | } `= 5`; /* fib */
4.382 us [31321] | printf();
9.456 us [31321] | } /* main */
```
---
### Record function arguments with type setting
```
$ clang -pg hello.c
$ uftrace -A printf@`arg1` a.out
```
---
### Record function arguments with type setting
```
$ clang -pg hello.c
$ uftrace -A printf@`arg1` a.out
Hello World!
# DURATION TID FUNCTION
[117536] | main() {
6.100 us [117536] | printf(`0x40074c`);
7.390 us [117536] | } /* main */
```
---
### Record function arguments with type setting
```
$ clang -pg hello.c
$ uftrace -A printf@arg1`/s` a.out
```
---
### Record function arguments with type setting
```
$ clang -pg hello.c
$ uftrace -A printf@arg1`/s` a.out
Hello World!
# DURATION TID FUNCTION
[117594] | main() {
319.904 us [117594] | printf(`"Hello World!\n"`);
321.521 us [117594] | } /* main */
```
---
class: code-18px
### Display Function Arguments
- uftrace can record function arguments and return values
- using the .red[-A]/.red[--argument] and .red[-R]/.red[--retval] options.
```
<argument> := <symbol> "@" <specs>
<specs> := <spec> | <spec> "," <spec>
<spec> := ( <int_spec> | <float_spec> | <ret_spec> )
<int_spec> := "arg" N [ "/" <format> [ <size> ] ] [ "%" ( <reg> | <stack> ) ]
<float_spec> := "fparg" N [ "/" ( <size> | "80" ) ] [ "%" ( <reg> | <stack> ) ]
<ret_spec> := "retval" [ "/" <format> [ <size> ] ]
<format> := "d" | "i" | "u" | "x" | "o" | "s" | "c" | "f" | "S" | "p"
<size> := "8" | "16" | "32" | "64"
<reg> := <arch-specific register name> # "rdi", "xmm0", "r0", ...
<stack> := "stack" [ "+" ] <offset>
```
---
name: autoargs
template: title-layout
## Arguments Detection with Debug Info
#### -a / --auto-args option
.footnote[[wiki/Arguments](https://github.com/namhyung/uftrace/wiki/Arguments)]
<pre><code class="bash">$ ./configure
libdw: [ .green[on] ]</code></pre>
---
### Detect function types using debug info with -a / --auto-args
```
$ gcc -pg fibonacci.c
```
---
### Detect function types using debug info with -a / --auto-args
.footnote[gcc .red[-g] produces debug info ([DWARF](http://dwarfstd.org))]
```
$ gcc -pg `-g` fibonacci.c
```
---
### Detect function types using debug info with -a / --auto-args
.footnote[.red[-a]/.red[--auto-args] uses debug info to detect type info of user functions]
```
$ gcc -pg `-g` fibonacci.c
$ uftrace `-a` a.out 5
```
---
### Detect function types using debug info with -a / --auto-args
<pre><code> $ gcc -pg `-g` fibonacci.c
$ uftrace `-a` a.out 5
<x-orange>fib(5) = 5</x-orange>
# DURATION TID FUNCTION
[31321] | main(`2`, `0x7ffd62a92a18`) {
1.478 us [31321] | atoi();
[31321] | fib(`5`) {
[31321] | fib(`4`) {
[31321] | fib(`3`) {
0.155 us [31321] | fib(`2`) `= 1`;
0.123 us [31321] | fib(`1`) `= 1`;
0.883 us [31321] | } = `2`; /* fib */
0.125 us [31321] | fib(`2`) `= 1`;
1.483 us [31321] | } `= 3`; /* fib */
[31321] | fib(`3`) {
0.125 us [31321] | fib(`2`) `= 1`;
0.125 us [31321] | fib(`1`) `= 1`;
0.774 us [31321] | } `= 2`; /* fib */
2.716 us [31321] | } `= 5`; /* fib */
4.382 us [31321] | printf(<x-orange>"fib(%d) = %d\n"</x-orange>) `= 11`;
9.456 us [31321] | } `= 0`; /* main */
</code></pre>
---
class: code-24px
### Detect function types using debug info with -a / --auto-args
.footnote[-a/--auto-args is able to detect .red[enum] and .red[function pointer] types]
<pre><code>
$ gcc -pg <x-red>-g</x-red> [tests/s-signal.c](https://raw.githubusercontent.com/namhyung/uftrace/master/tests/s-signal.c)
</code><pre>
```
$ uftrace `-a` a.out
# DURATION TID FUNCTION
[117990] | main(1, 0x7ffd6b5381f8) {
0.437 us [117990] | foo();
2.250 us [117990] | signal(`SIGUSR1`, `&sighandler`) = 0;
[117990] | raise(`SIGUSR1`) {
[117990] | sighandler(10) {
0.240 us [117990] | bar(10);
1.343 us [117990] | } /* sighandler */
14.254 us [117990] | } /* raise */
0.197 us [117990] | foo();
22.654 us [117990] | } = 0; /* main */
```
---
name: dwarf-args-clang
class: code-11px
<pre><code>$ uftrace replay --tid 179275 -t 5ms
\# DURATION TID FUNCTION
[179275] | main(52, 0x7ffe180f20d8) {
[179275] | cc1_main(0x7ffe180f17c0, 0x7ffe180f403f, <x-cyan>&GetExecutablePath::cxx11</x-cyan>) {
[179275] | clang::CompilerInvocation::CreateFromArgs(0x15ec5be0, 0x7ffe180f17c0, 0x7ffe180f1950, 0x7ffe180f04c0) {
[179275] | clang::driver::createDriverOptTable() {
5.356 <x-green>ms</x-green> [179275] | llvm::opt::OptTable::OptTable(0x15ec7b70, <x-cyan>&InfoTable</x-cyan>, 0);
6.919 <x-green>ms</x-green> [179275] | llvm::opt::OptTable::addValues(0x15ec7b70, <x-magenta>"-std="</x-magenta>, <x-magenta>"c89,c90,iso9899:1990,iso9899:199409,gnu89,gnu90..."</x-magenta>) = 1;
6.860 <x-green>ms</x-green> [179275] | llvm::opt::OptTable::addValues(0x15ec7b70, <x-magenta>"--std="</x-magenta>, <x-magenta>"c89,c90,iso9899:1990,iso9899:199409,gnu89,gnu90..."</x-magenta>) = 1;
19.213 <x-green>ms</x-green> [179275] | } = 0x7ffe180eff70; /\* clang::driver::createDriverOptTable \*/
23.958 <x-green>ms</x-green> [179275] | } = 1; /\* clang::CompilerInvocation::CreateFromArgs \*/
[179275] | clang::ExecuteCompilerInvocation(0x15ec3850) {
[179275] | clang::CompilerInstance::ExecuteAction(0x15ec3850, 0x15eed930) {
[179275] | clang::FrontendAction::BeginSourceFile(0x15eed930, 0x15ec3850, 0x15eec700) {
5.804 <x-green>ms</x-green> [179275] | clang::CompilerInstance::createPreprocessor(0x15ec3850, <x-blue>TU_Complete</x-blue>);
13.783 <x-green>ms</x-green> [179275] | clang::Builtin::Context::initializeBuiltins(0x15ec9288, 0x15ec91f8, 0x15ec39d0);
20.387 <x-green>ms</x-green> [179275] | } = 1; /\* clang::FrontendAction::BeginSourceFile \*/
[179275] | clang::FrontendAction::Execute(0x15eed930) {
[179275] | clang::CodeGenAction::ExecuteAction(0x15eed930) {
[179275] | clang::ASTFrontendAction::ExecuteAction(0x15eed930) {
[179275] | clang::ParseAST(0x15f45240, 0, 0) {
[179275] | clang::Parser::Initialize(0x15f4c1d0) {
45.142 <x-green>ms</x-green> [179275] | clang::Preprocessor::Lex(0x15ec9020, 0x15f4c1e0);
45.593 <x-green>ms</x-green> [179275] | } /\* clang::Parser::Initialize \*/
[179275] | clang::Parser::ParseTopLevelDecl(0x15f4c1d0, 0x7ffe180f0098) {
[179275] | clang::Parser::ParseExternalDeclaration(0x15f4c1d0, 0x7ffe180f0000, 0) {
[179275] | clang::Parser::ParseDeclaration(0x15f4c1d0, <x-blue>FileContext</x-blue>, 0x7ffe180eff90, 0x7ffe180f0000) {
[179275] | clang::Parser::ParseSimpleDeclaration(0x15f4c1d0, <x-blue>FileContext</x-blue>, 0x7ffe180eff90, 0x7ffe180f0000, 1, 0) {
[179275] | clang::Parser::ParseDeclGroup(0x15f4c1d0, 0x7ffe180efc40, <x-blue>FileContext</x-blue>, 0x7ffe180eff90, 0) {
[179275] | clang::Parser::ExpectAndConsumeSemi(0x15f4c1d0, 1263) {
6.473 <x-green>ms</x-green> [179275] | clang::Preprocessor::Lex(0x15ec9020, 0x15f4c1e0);
6.475 <x-green>ms</x-green> [179275] | } /\* clang::Parser::ExpectAndConsumeSemi \*/
6.592 <x-green>ms</x-green> [179275] | } = 0x15f99e88; /\* clang::Parser::ParseDeclGroup \*/
6.660 <x-green>ms</x-green> [179275] | } = 0x15f99e88; /\* clang::Parser::ParseSimpleDeclaration \*/
6.662 <x-green>ms</x-green> [179275] | } = 0x15f99e88; /\* clang::Parser::ParseDeclaration \*/
6.663 <x-green>ms</x-green> [179275] | } = 0x15f99e88; /\* clang::Parser::ParseExternalDeclaration \*/
6.664 <x-green>ms</x-green> [179275] | } = 0; /\* clang::Parser::ParseTopLevelDecl \*/
[179275] | clang::Parser::ParseTopLevelDecl(0x15f4c1d0, 0x7ffe180f0098) {
[179275] | clang::Parser::ParseExternalDeclaration(0x15f4c1d0, 0x7ffe180f0000, 0) {
[179275] | clang::Parser::ParseDeclaration(0x15f4c1d0, <x-blue>FileContext</x-blue>, 0x7ffe180eff90, 0x7ffe180f0000) {
[179275] | clang::Parser::ParseSimpleDeclaration(0x15f4c1d0, <x-blue>FileContext</x-blue>, 0x7ffe180eff90, 0x7ffe180f0000, 1, 0) {
[179275] | clang::Parser::ParseDeclGroup(0x15f4c1d0, 0x7ffe180efc40, <x-blue>FileContext</x-blue>, 0x7ffe180eff90, 0) {
[179275] | clang::Parser::ExpectAndConsumeSemi(0x15f4c1d0, 1263) {
[179275] | clang::Preprocessor::Lex(0x15ec9020, 0x15f4c1e0) {
[179275] | clang::Preprocessor::CachingLex(0x15ec9020, 0x15f4c1e0) {
[179275] | clang::Preprocessor::Lex(0x15ec9020, 0x15f4c1e0) {
[179275] | clang::Lexer::Lex(0x15f8b290, 0x15f4c1e0) {
[179275] | clang::Lexer::LexTokenInternal(0x15f8b290, 0x15f4c1e0, 1) {
</code><pre>
---
name: report
template: title-layout
# uftrace report
### Print statistics and summary for trace data
.footnote[[doc/uftrace-report.md](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-report.md)]
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
```
$ gcc -pg tests/s-signal.c
```
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
```
$ gcc -pg tests/s-signal.c
$ uftrace record a.out
```
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
```
$ gcc -pg tests/s-signal.c
$ uftrace record a.out
$ uftrace report
```
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
```
$ gcc -pg tests/s-signal.c
$ uftrace record a.out
$ uftrace report
Total time Self time Calls Function
========== ========== ========== ====================
25.310 us 5.848 us 1 main
16.474 us 15.104 us 1 raise
3.007 us 3.007 us 1 __monstartup
2.247 us 2.247 us 1 signal
1.370 us 1.086 us 1 sighandler
1.276 us 1.276 us 1 __cxa_atexit
0.741 us 0.741 us 2 foo
0.284 us 0.284 us 1 bar
```
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
- Sort functions by .red[total time] (default)
```
$ gcc -pg tests/s-signal.c
$ uftrace record a.out
$ uftrace report `-s total`
`Total time` Self time Calls Function
========== ========== ========== ====================
25.310 us 5.848 us 1 main
16.474 us 15.104 us 1 raise
3.007 us 3.007 us 1 __monstartup
2.247 us 2.247 us 1 signal
1.370 us 1.086 us 1 sighandler
1.276 us 1.276 us 1 __cxa_atexit
0.741 us 0.741 us 2 foo
0.284 us 0.284 us 1 bar
```
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
- Sort functions by .red[self time]
```
$ gcc -pg tests/s-signal.c
$ uftrace record a.out
$ uftrace report `-s self`
Total time `Self time` Calls Function
========== ========== ========== ====================
16.474 us 15.104 us 1 raise
25.310 us 5.848 us 1 main
3.007 us 3.007 us 1 __monstartup
2.247 us 2.247 us 1 signal
1.276 us 1.276 us 1 __cxa_atexit
1.370 us 1.086 us 1 sighandler
0.741 us 0.741 us 2 foo
0.284 us 0.284 us 1 bar
```
---
class: code-26px
### uftrace report
- Print statistics and summary for trace data
- Sort functions by .red[number of calls]
```
$ gcc -pg tests/s-signal.c
$ uftrace record a.out
$ uftrace report `-s call`
Total time Self time `Calls` Function
========== ========== ========== ====================
0.741 us 0.741 us 2 foo
1.276 us 1.276 us 1 __cxa_atexit
3.007 us 3.007 us 1 __monstartup
0.284 us 0.284 us 1 bar
25.310 us 5.848 us 1 main
16.474 us 15.104 us 1 raise
1.370 us 1.086 us 1 sighandler
2.247 us 2.247 us 1 signal
```
---
name: dump
template: title-layout
# uftrace dump
### with visualization
.footnote[[doc/uftrace-dump.md](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-dump.md)]
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
- Below is a trace of clang (LLVM) compiling a small C++ template metaprogram.
- uftrace dump --chrome
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
.footnote[<a href="https://uftrace.github.io/dump/clang.tmp.fib.html" target="_blank">open in chrome trace viewer</a>]
- Below is a trace of clang (LLVM) compiling a small C++ template metaprogram.
- uftrace dump --chrome
<a href="https://uftrace.github.io/dump/clang.tmp.fib.html" target="_blank">
<img src="https://raw.githubusercontent.com/namhyung/uftrace/master/doc/uftrace-chrome.png" class="center-image" width="90%">
</a>
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
.footnote[[Catapult Project](https://chromium.googlesource.com/catapult)]
- uftrace dump --chrome generates trace in [JSON format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview).
- Catapult's [trace2html](https://chromium.googlesource.com/catapult/+/refs/heads/main/tracing/bin/trace2html) script can convert it to HTML.
```
$ uftrace record a.out
```
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
.footnote[[Catapult Project](https://chromium.googlesource.com/catapult)]
- uftrace dump --chrome generates trace in [JSON format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview).
- Catapult's [trace2html](https://chromium.googlesource.com/catapult/+/refs/heads/main/tracing/bin/trace2html) script can convert it to HTML.
```
$ uftrace record a.out
$ uftrace dump `--chrome` > uftrace-dump-chrome.json
```
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
.footnote[[Catapult Project](https://chromium.googlesource.com/catapult)]
- uftrace dump --chrome generates trace in [JSON format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview).
- Catapult's [trace2html](https://chromium.googlesource.com/catapult/+/refs/heads/main/tracing/bin/trace2html) script can convert it to HTML.
```
$ uftrace record a.out
$ uftrace dump --chrome > uftrace-dump-chrome.json
$ `trace2html` uftrace-dump-chrome.json
```
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
.footnote[[Catapult Project](https://chromium.googlesource.com/catapult)]
- uftrace dump --chrome generates trace in [JSON format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview).
- Catapult's [trace2html](https://chromium.googlesource.com/catapult/+/refs/heads/main/tracing/bin/trace2html) script can convert it to HTML.
```
$ uftrace record a.out
$ uftrace dump --chrome > uftrace-dump-chrome.json
$ trace2html uftrace-dump-chrome.json
`uftrace-dump-chrome.html`
```
---
### [Chrome Trace Viewer](https://www.chromium.org/developers/how-tos/trace-event-profiling-tool)
.footnote[[Catapult Project](https://chromium.googlesource.com/catapult)]
- uftrace dump --chrome generates trace in [JSON format](https://docs.google.com/document/d/1CvAClvFfyA5R-PhYUmn5OOQtYMH4h6I0nSsKchNAySU/preview).
- Catapult's [trace2html](https://chromium.googlesource.com/catapult/+/refs/heads/main/tracing/bin/trace2html) script can convert it to HTML.
```
$ uftrace record a.out
$ uftrace dump --chrome > uftrace-dump-chrome.json
$ trace2html uftrace-dump-chrome.json
uftrace-dump-chrome.html
```
- Then run a web server to open in chrome browser (if needed)
<pre><code class="plain"> $ python -m <a href="https://docs.python.org/2/library/simplehttpserver.html">SimpleHTTPServer</a> 12345
or
$ python3 -m <a href="https://docs.python.org/3/library/http.server.html">http.server</a> 12345
</code></pre>
---
### [Flame Graph](http://www.brendangregg.com/flamegraphs.html)
- Flame graph of gcc compiling a simple c program.
- uftrace dump --flame-graph | [flamegraph.pl](https://raw.githubusercontent.com/brendangregg/FlameGraph/master/flamegraph.pl) > out.svg
---
### [Flame Graph](http://www.brendangregg.com/flamegraphs.html)
.footnote[<a href="https://honggyukim.github.io/uftrace/v8/octane-splay/trace_splay.js.svg" target="_blank">open in browser</a>]
- Flame graph of gcc compiling a simple c program.
- uftrace dump --flame-graph | [flamegraph.pl](https://raw.githubusercontent.com/brendangregg/FlameGraph/master/flamegraph.pl) > out.svg
<a href="https://uftrace.github.io/dump/gcc.svg" target="_blank">
<img src="https://uftrace.github.io/dump/gcc.svg" class="center-image" width="90%">
</a>
---
name: graph
template: title-layout
# uftrace graph
### Show function call graph
.footnote[[doc/uftrace-graph.md](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-graph.md)]
---
### Call graph of std::shared_ptr
```cpp
$ cat shared_ptr.cc
#include <memory>
int main()
{
std::shared_ptr<int> sp1(new int);
}
```
---
### Call graph of std::shared_ptr
```cpp
$ cat shared_ptr.cc
#include <memory>
int main()
{
std::shared_ptr<int> sp1(new int);
}
```
```
$ g++ -std=c++11 -pg -g shared_ptr.cc
```
---
### Call graph of std::shared_ptr
```cpp
$ cat shared_ptr.cc
#include <memory>
int main()
{
std::shared_ptr<int> sp1(new int);
}
```
```
$ g++ -std=c++11 -pg -g shared_ptr.cc
$ uftrace record a.out
```
---
### Call graph of std::shared_ptr
```cpp
$ cat shared_ptr.cc
#include <memory>
int main()
{
std::shared_ptr<int> sp1(new int);
}
```
```
$ g++ -std=c++11 -pg -g shared_ptr.cc
$ uftrace record a.out
$ uftrace `graph`
```
---
class: code-15px
.footnote[aggregated call graph]
```
$ uftrace `graph`
# Function Call Graph for 'a.out' (session: c6e074b930018807)
========== FUNCTION CALL GRAPH ==========
# TOTAL TIME FUNCTION
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : +-(1) operator new
: |
4.170 us : +-(1) std::shared_ptr::shared_ptr
3.797 us : | (1) std::__shared_ptr::__shared_ptr
2.967 us : | +-(1) std::__shared_count::__shared_count
0.630 us : | | +-(1) operator new
: | | |
1.357 us : | | +-(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : | | (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us : | | (1) std::_Mutex_base::_Mutex_base
: | |
0.153 us : | +-(1) std::__enable_shared_from_this_helper
: |
10.027 us : +-(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : +-(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : | +-(2) __gthread_active_p
: | |
0.260 us : | +-(2) __gnu_cxx::__exchange_and_add_single
: |
3.803 us : +-(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : | (1) operator delete
: |
2.083 us : +-(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : +-(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : | (1) std::_Sp_counted_base::~_Sp_counted_base
: |
0.284 us : +-(1) operator delete
```
---
class: code-15px
.footnote[.red[number of calls]]
```
$ uftrace graph
# Function Call Graph for 'a.out' (session: c6e074b930018807)
========== FUNCTION CALL GRAPH ==========
# TOTAL TIME FUNCTION
20.914 us : (`1`) a.out
20.914 us : (`1`) main
1.997 us : +-(`1`) operator new
: |
4.170 us : +-(`1`) std::shared_ptr::shared_ptr
3.797 us : | (`1`) std::__shared_ptr::__shared_ptr
2.967 us : | +-(`1`) std::__shared_count::__shared_count
0.630 us : | | +-(`1`) operator new
: | | |
1.357 us : | | +-(`1`) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : | | (`1`) std::_Sp_counted_base::_Sp_counted_base
0.230 us : | | (`1`) std::_Mutex_base::_Mutex_base
: | |
0.153 us : | +-(`1`) std::__enable_shared_from_this_helper
: |
10.027 us : +-(`1`) std::shared_ptr::~shared_ptr
9.700 us : (`1`) std::__shared_ptr::~__shared_ptr
9.374 us : (`1`) std::__shared_count::~__shared_count
9.054 us : (`1`) std::_Sp_counted_base::_M_release
2.093 us : +-(`2`) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : | +-(`2`) __gthread_active_p
: | |
0.260 us : | +-(`2`) __gnu_cxx::__exchange_and_add_single
: |
3.803 us : +-(`1`) std::_Sp_counted_ptr::_M_dispose
2.890 us : | (`1`) operator delete
: |
2.083 us : +-(`1`) std::_Sp_counted_ptr::_M_destroy
1.697 us : (`1`) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : +-(`1`) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : | (`1`) std::_Sp_counted_base::~_Sp_counted_base
: |
0.284 us : +-(`1`) operator delete
```
---
class: code-15px
.footnote[.red[total time of each call graph]]
```
$ uftrace graph
# Function Call Graph for 'a.out' (session: c6e074b930018807)
========== FUNCTION CALL GRAPH ==========
# `TOTAL TIME` FUNCTION
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : +-(1) operator new
: |
`4.170 us` : +-(1) `std::shared_ptr::shared_ptr`
3.797 us : | (1) std::__shared_ptr::__shared_ptr
2.967 us : | +-(1) std::__shared_count::__shared_count
0.630 us : | | +-(1) operator new
: | | |
1.357 us : | | +-(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : | | (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us : | | (1) std::_Mutex_base::_Mutex_base
: | |
0.153 us : | +-(1) std::__enable_shared_from_this_helper
: |
`10.027 us` : +-(1) `std::shared_ptr::~shared_ptr`
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : +-(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : | +-(2) __gthread_active_p
: | |
0.260 us : | +-(2) __gnu_cxx::__exchange_and_add_single
: |
3.803 us : +-(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : | (1) operator delete
: |
2.083 us : +-(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : +-(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : | (1) std::_Sp_counted_base::~_Sp_counted_base
: |
0.284 us : +-(1) operator delete
```
---
class: code-15px
```
$ uftrace graph `-f +self`
# Function Call Graph for 'a.out' (session: c6e074b930018807)
========== FUNCTION CALL GRAPH ==========
# TOTAL TIME `SELF TIME` FUNCTION
20.914 us : (1) a.out
20.914 us 4.720 us : (1) main
1.997 us 1.997 us : +-(1) operator new
: |
4.170 us 0.373 us : +-(1) std::shared_ptr::shared_ptr
3.797 us 0.677 us : | (1) std::__shared_ptr::__shared_ptr
2.967 us 0.980 us : | +-(1) std::__shared_count::__shared_count
0.630 us 0.630 us : | | +-(1) operator new
: | | |
1.357 us 0.497 us : | | +-(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us 0.630 us : | | (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us 0.230 us : | | (1) std::_Mutex_base::_Mutex_base
: | |
0.153 us 0.153 us : | +-(1) std::__enable_shared_from_this_helper
: |
10.027 us 0.327 us : +-(1) std::shared_ptr::~shared_ptr
9.700 us 0.326 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us 0.320 us : (1) std::__shared_count::~__shared_count
9.054 us 1.075 us : (1) std::_Sp_counted_base::_M_release
2.093 us 1.546 us : +-(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us 0.287 us : | +-(2) __gthread_active_p
: | |
0.260 us 0.260 us : | +-(2) __gnu_cxx::__exchange_and_add_single
: |
3.803 us 0.913 us : +-(1) std::_Sp_counted_ptr::_M_dispose
2.890 us 2.890 us : | (1) operator delete
: |
2.083 us 0.386 us : +-(1) std::_Sp_counted_ptr::_M_destroy
1.697 us 0.727 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us 0.543 us : +-(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us 0.143 us : | (1) std::_Sp_counted_base::~_Sp_counted_base
: |
0.284 us 0.284 us : +-(1) operator delete
```
---
class: code-15px
```
$ uftrace graph `-f +self,addr`
# Function Call Graph for 'a.out' (session: c6e074b930018807)
========== FUNCTION CALL GRAPH ==========
# TOTAL TIME `SELF TIME` `ADDRESS` FUNCTION
20.914 us 400c3c : (1) a.out
20.914 us 4.720 us 400c3c : (1) main
1.997 us 1.997 us 400a10 : +-(1) operator new
: |
4.170 us 0.373 us 400cdf : +-(1) std::shared_ptr::shared_ptr
3.797 us 0.677 us 400d3b : | (1) std::__shared_ptr::__shared_ptr
2.967 us 0.980 us 400e16 : | +-(1) std::__shared_count::__shared_count
0.630 us 0.630 us 400a10 : | | +-(1) operator new
: | | |
1.357 us 0.497 us 400f21 : | | +-(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us 0.630 us 400fd1 : | | (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us 0.230 us 400fbd : | | (1) std::_Mutex_base::_Mutex_base
: | |
0.153 us 0.153 us 400e91 : | +-(1) std::__enable_shared_from_this_helper
: |
10.027 us 0.327 us 400cbf : +-(1) std::shared_ptr::~shared_ptr
9.700 us 0.326 us 400c9b : (1) std::__shared_ptr::~__shared_ptr
9.374 us 0.320 us 400d0b : (1) std::__shared_count::~__shared_count
9.054 us 1.075 us 400d95 : (1) std::_Sp_counted_base::_M_release
2.093 us 1.546 us 400bf4 : +-(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us 0.287 us 400b7f : | +-(2) __gthread_active_p
: | |
0.260 us 0.260 us 400bc1 : | +-(2) __gnu_cxx::__exchange_and_add_single
: |
3.803 us 0.913 us 40107f : +-(1) std::_Sp_counted_ptr::_M_dispose
2.890 us 2.890 us 400960 : | (1) operator delete
: |
2.083 us 0.386 us 4010a3 : +-(1) std::_Sp_counted_ptr::_M_destroy
1.697 us 0.727 us 401053 : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us 0.543 us 401013 : +-(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us 0.143 us 400f5d : | (1) std::_Sp_counted_base::~_Sp_counted_base
: |
0.284 us 0.284 us 400960 : +-(1) operator delete
```
---
### Task graph of GCC compiler
- `graph` command is for function-level call graph
- `graph --task` is for task-level (hierarchy) graph
```
$ uftrace record --force /usr/bin/gcc hello.c
$ uftrace graph `--task`
```
---
### Task graph of GCC compiler
- `graph` command is for function-level call graph
- `graph --task` is for task-level (hierarchy) graph
```
$ uftrace record --force /usr/bin/gcc hello.c
$ uftrace graph `--task`
========== TASK GRAPH ==========
# TOTAL TIME SELF TIME TID TASK NAME
159.854 ms 4.440 ms [ 82723] : gcc
: |
90.951 ms 90.951 ms [ 82734] : +----cc1
: |
17.150 ms 17.150 ms [ 82735] : +----as
: |
45.183 ms 6.076 ms [ 82736] : +----collect2
: |
38.880 ms 38.880 ms [ 82737] : +----ld
```
---
name: tui
template: title-layout
# uftrace tui
### (Interactive) Text-based User Interface
.footnote[[doc/uftrace-tui.md](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-tui.md)]
<pre><code class="bash">$ ./configure
libncursesw: [ .green[on] ]</code></pre>
---
class: tui
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
<x-tui-white-line> 20.914 us : (1) a.out</x-tui-white-line>
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├─(1) std::shared_ptr::shared_ptr
3.797 us : │ (1) std::__shared_ptr::__shared_ptr
2.967 us : │ ├─(1) std::__shared_count::__shared_count
0.630 us : │ │ ├─(1) operator new
: │ │ │
1.357 us : │ │ └─(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : │ │ (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us : │ │ (1) std::_Mutex_base::_Mutex_base
: │ │
0.153 us : │ └─(1) std::__enable_shared_from_this_helper
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├─(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : │ ├─(2) __gthread_active_p
: │ │
0.260 us : │ └─(2) __gnu_cxx::__exchange_and_add_single
: │
3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : │ (1) operator delete
: │
2.083 us : └─(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: session c6e074b930018807 (/home/honggyu/uftrace/a.out)</x-tui-blue-line>
</code>
</pre>
---
class: tui
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
<x-tui-white-line> 20.914 us : (1) main</x-tui-white-line>
1.997 us : ├─(1) operator new
: │
4.170 us : ├─(1) std::shared_ptr::shared_ptr
3.797 us : │ (1) std::__shared_ptr::__shared_ptr
2.967 us : │ ├─(1) std::__shared_count::__shared_count
0.630 us : │ │ ├─(1) operator new
: │ │ │
1.357 us : │ │ └─(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : │ │ (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us : │ │ (1) std::_Mutex_base::_Mutex_base
: │ │
0.153 us : │ └─(1) std::__enable_shared_from_this_helper
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├─(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : │ ├─(2) __gthread_active_p
: │ │
0.260 us : │ └─(2) __gnu_cxx::__exchange_and_add_single
: │
3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : │ (1) operator delete
: │
2.083 us : └─(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: /home/honggyu/shared_ptr.cc [line:3]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[h]/.red[?] - Show help]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
<x-tui-white-line> 20.914 us : (1) main</x-tui-white-line>
1.997 us : ├─(1) operator new
: │
4.170 us : ├─┌──────────────────────────────────────────────────────────────┐
3.797 us : │ │Help: (press any key to exit) │
2.967 us : │ │ │
0.630 us : │ │ ARROW Navigation │
: │ │ PgUp/Dn │
1.357 us : │ │ Home/End │
0.860 us : │ │ Enter Fold/unfold graph or Select session │
0.230 us : │ │ G Show (full) call graph │
: │ │ g Show call graph for this function │
0.153 us : │ │ R Show uftrace report │
: │ │ I Show uftrace info │
10.027 us : └─│ S Change session │
9.700 us : │ O Open editor │
9.374 us : │ c/e Collapse/Expand graph │
9.054 us : │ n/p Next/Prev sibling │
2.093 us : │ u Move up to parent │
0.287 us : │ l Move to the longest executed child │
: │ j/k Move down/up │
0.260 us : │ / Search │
: │ <​/>/N/P Search next/prev │
3.803 us : │ v Show debug message │
2.890 us : │ h/? Show this help │
: │ q Quit │
2.083 us : │ │
1.697 us : └──────────────────────────────────────────────────────────────┘
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: /home/honggyu/shared_ptr.cc [line:3]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[Enter] - .red[Fold]/Unfold]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
<x-tui-white-line> 20.914 us :<x-red>▶</x-red>(1) main</x-tui-white-line>
<x-tui-blue-line>uftrace graph: /home/honggyu/shared_ptr.cc [line:3]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[Enter] - Fold/.red[Unfold]]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
<x-tui-white-line> 20.914 us : (1) main</x-tui-white-line>
1.997 us : ├─(1) operator new
: │
4.170 us : ├─(1) std::shared_ptr::shared_ptr
3.797 us : │ (1) std::__shared_ptr::__shared_ptr
2.967 us : │ ├─(1) std::__shared_count::__shared_count
0.630 us : │ │ ├─(1) operator new
: │ │ │
1.357 us : │ │ └─(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : │ │ (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us : │ │ (1) std::_Mutex_base::_Mutex_base
: │ │
0.153 us : │ └─(1) std::__enable_shared_from_this_helper
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├─(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : │ ├─(2) __gthread_active_p
: │ │
0.260 us : │ └─(2) __gnu_cxx::__exchange_and_add_single
: │
3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : │ (1) operator delete
: │
2.083 us : └─(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: /home/honggyu/shared_ptr.cc [line:3]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[c]/e - .red[Collapse]/Expand graph]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
<x-tui-white-line> 20.914 us : (1) main</x-tui-white-line>
1.997 us : ├─(1) operator new
: │
4.170 us : ├<x-red>▶</x-red>(1) std::shared_ptr::shared_ptr
: │
10.027 us : └<x-red>▶</x-red>(1) std::shared_ptr::~shared_ptr
<x-tui-blue-line>uftrace graph: /home/honggyu/shared_ptr.cc [line:3]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[c/.red[e] - Collapse/.red[Expand] graph]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
<x-tui-white-line> 20.914 us : (1) main</x-tui-white-line>
1.997 us : ├─(1) operator new
: │
4.170 us : ├─(1) std::shared_ptr::shared_ptr
3.797 us : │ (1) std::__shared_ptr::__shared_ptr
2.967 us : │ ├─(1) std::__shared_count::__shared_count
0.630 us : │ │ ├─(1) operator new
: │ │ │
1.357 us : │ │ └─(1) std::_Sp_counted_ptr::_Sp_counted_ptr
0.860 us : │ │ (1) std::_Sp_counted_base::_Sp_counted_base
0.230 us : │ │ (1) std::_Mutex_base::_Mutex_base
: │ │
0.153 us : │ └─(1) std::__enable_shared_from_this_helper
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├─(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : │ ├─(2) __gthread_active_p
: │ │
0.260 us : │ └─(2) __gnu_cxx::__exchange_and_add_single
: │
3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : │ (1) operator delete
: │
2.083 us : └─(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: /home/honggyu/shared_ptr.cc [line:3]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[l] - Move to the longest executed child]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
<x-red>1.997 us</x-red> : ├─(1) operator new
: │
<x-red>4.170 us</x-red> : ├<x-red>▶</x-red>(1) std::shared_ptr::shared_ptr
: │
<x-tui-white-line> <x-red>10.027 us</x-red> : └─(1) std::shared_ptr::~shared_ptr</x-tui-white-line>
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├─(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : │ ├─(2) __gthread_active_p
: │ │
0.260 us : │ └─(2) __gnu_cxx::__exchange_and_add_single
: │
3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : │ (1) operator delete
: │
2.083 us : └─(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: /usr/include/c++/5/bits/shared_ptr.h [line:93]</x-tui-blue-line>
</code>
</pre>
---
class: tui
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├▶(1) std::shared_ptr::shared_ptr
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
<x-tui-white-line> 9.054 us : (1) std::_Sp_counted_base::_M_release</x-tui-white-line>
2.093 us : ├─(2) __gnu_cxx::__exchange_and_add_dispatch
0.287 us : │ ├─(2) __gthread_active_p
: │ │
0.260 us : │ └─(2) __gnu_cxx::__exchange_and_add_single
: │
3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose
2.890 us : │ (1) operator delete
: │
2.083 us : └─(1) std::_Sp_counted_ptr::_M_destroy
1.697 us : (1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.686 us : ├─(1) std::_Sp_counted_ptr::~_Sp_counted_ptr
0.143 us : │ (1) std::_Sp_counted_base::~_Sp_counted_base
: │
0.284 us : └─(1) operator delete
<x-tui-blue-line>uftrace graph: /usr/include/c++/5/bits/shared_ptr.h [line:93]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[l] - Move to the longest executed child]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├▶(1) std::shared_ptr::shared_ptr
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
<x-red>2.093 us</x-red> : ├<x-red>▶</x-red>(2) __gnu_cxx::__exchange_and_add_dispatch
: │
<x-tui-white-line> <x-red>3.803 us</x-red> : ├─(1) std::_Sp_counted_ptr::_M_dispose</x-tui-white-line>
2.890 us : │ (1) operator delete
: │
<x-red>2.083 us</x-red> : └<x-red>▶</x-red>(1) std::_Sp_counted_ptr::_M_destroy
<x-tui-blue-line>uftrace graph: /usr/include/c++/5/bits/shared_ptr_base.h [line:373]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[O] - Open editor]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├▶(1) std::shared_ptr::shared_ptr
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├▶(2) __gnu_cxx::__exchange_and_add_dispatch
: │
<x-tui-white-line> 3.803 us : ├─(1) <x-red>std::_Sp_counted_ptr::_M_dispose</x-red></x-tui-white-line>
2.890 us : │ (1) operator delete
: │
2.083 us : └▶(1) std::_Sp_counted_ptr::_M_destroy
<x-tui-blue-line>uftrace graph: <x-red>/usr/include/c++/5/bits/shared_ptr_base.h [line:373]</x-red></x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[O] - Open editor]
```cpp
357 class __weak_count;
358
359 template<_Lock_policy _Lp = __default_lock_policy>
360 class __shared_count;
361
362
363 // Counted ptr with no deleter or allocator support
364 template<typename _Ptr, _Lock_policy _Lp>
365 class _Sp_counted_ptr final : public _Sp_counted_base<_Lp>
366 {
367 public:
368 explicit
369 _Sp_counted_ptr(_Ptr __p) noexcept
370 : _M_ptr(__p) { }
371
372 virtual void
* 373 _M_dispose() noexcept
374 { delete _M_ptr; }
375
376 virtual void
377 _M_destroy() noexcept
378 { delete this; }
379
380 virtual void*
381 _M_get_deleter(const std::type_info&) noexcept
382 { return nullptr; }
383
384 _Sp_counted_ptr(const _Sp_counted_ptr&) = delete;
385 _Sp_counted_ptr& operator=(const _Sp_counted_ptr&) = delete;
386
387 private:
388 _Ptr _M_ptr;
389 };
390
/usr/include/c++/5/bits/shared_ptr_base.h [RO] 373,7 22%
```
---
class: tui
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├▶(1) std::shared_ptr::shared_ptr
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
9.054 us : (1) std::_Sp_counted_base::_M_release
2.093 us : ├▶(2) __gnu_cxx::__exchange_and_add_dispatch
: │
<x-tui-white-line> 3.803 us : ├─(1) std::_Sp_counted_ptr::_M_dispose</x-tui-white-line>
2.890 us : │ (1) operator delete
: │
2.083 us : └▶(1) std::_Sp_counted_ptr::_M_destroy
<x-tui-blue-line>uftrace graph: /usr/include/c++/5/bits/shared_ptr_base.h [line:373]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[u] - Move up to parent]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├▶(1) std::shared_ptr::shared_ptr
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
9.374 us : (1) std::__shared_count::~__shared_count
<x-tui-white-line> 9.054 us : (1) std::_Sp_counted_base::_M_release</x-tui-white-line>
2.093 us : ├<x-red>▶</x-red>(2) __gnu_cxx::__exchange_and_add_dispatch
: │
3.803 us : ├<x-red>▶</x-red>(1) std::_Sp_counted_ptr::_M_dispose
: │
2.083 us : └<x-red>▶</x-red>(1) std::_Sp_counted_ptr::_M_destroy
<x-tui-blue-line>uftrace graph: /usr/include/c++/5/bits/shared_ptr_base.h [line:143]</x-tui-blue-line>
</code>
</pre>
---
class: tui
.footnote[.red[u] - Move up to parent]
<pre>
<code class="plain"> <x-tui-blue-line> TOTAL TIME : FUNCTION</x-tui-blue-line>
20.914 us : (1) a.out
20.914 us : (1) main
1.997 us : ├─(1) operator new
: │
4.170 us : ├▶(1) std::shared_ptr::shared_ptr
: │
10.027 us : └─(1) std::shared_ptr::~shared_ptr
9.700 us : (1) std::__shared_ptr::~__shared_ptr
<x-tui-white-line> 9.374 us : (1) std::__shared_count::~__shared_count</x-tui-white-line>
9.054 us : <x-red>▶</x-red>(1) std::_Sp_counted_base::_M_release
<x-tui-blue-line>uftrace graph: /usr/include/c++/5/bits/shared_ptr_base.h [line:656]</x-tui-blue-line>
</code>
</pre>
---
name: python
template: title-layout
# (Python) Scripting
### uftrace record -S / uftrace script -S
.footnote[[doc/uftrace-script.md](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-script.md)]
<pre><code class="bash">$ ./configure
libpython2.7: [ .green[on] ]</code></pre>
---
### uftrace script
- uftrace is able to run (python) script
- at the entry and exit for each user and library function
---
### uftrace script
- uftrace is able to run (python) script
- at the entry and exit for each user and library function
- Script can be executed
- at runtime (record time)
- execute a given script at record time.
- able to perform additional actions while target binary execution.
- but slow
- at analysis time (post processing)
- run a given script for a recorded data (uftrace.data)
- using [uftrace script](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-script.md) command
- faster and more reliable
---
### uftrace script
- uftrace script (python) APIs
- `uftrace_entry`(.red[ctx])
- runs at the entry of each function
- `uftrace_exit`(.red[ctx])
- runs at the exit of each function
- `uftrace_begin`(.red[ctx])
- runs only once at the beginning of program
- optional, so can be omitted
- `uftrace_end`()
- runs only once at the end of program
- optional, so can be omitted
- The .red[ctx] variable is a dictionary type.
- including function name, tid, timestamp, etc.
---
class: code-20px
### A Simple Python Script (simple.py)
```
$ uftrace tests/t-abc
```
---
class: code-20px
### A Simple Python Script (simple.py)
```
$ uftrace tests/t-abc
# DURATION TID FUNCTION
[ 89491] | main() {
[ 89491] | a() {
[ 89491] | b() {
[ 89491] | c() {
4.993 us [ 89491] | getpid();
17.374 us [ 89491] | } /* c */
25.934 us [ 89491] | } /* b */
35.023 us [ 89491] | } /* a */
52.927 us [ 89491] | } /* main */
```
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace `-S scripts/simple.py` tests/t-abc
```
]
.right-column[
```
$ cat `scripts/simple.py`
def uftrace_begin(ctx):
print("program begins...")
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
`program begins...`
```
]
.right-column[
```
$ cat scripts/simple.py
`def uftrace_begin(ctx):`
` print("program begins...")`
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
program begins...
`entry : main()`
`entry : a()`
`entry : b()`
`entry : c()`
`entry : getpid()`
```
]
.right-column[
```
$ cat scripts/simple.py
def uftrace_begin(ctx):
print("program begins...")
`def uftrace_entry(ctx):`
` func = ctx["name"]`
` print("entry : " + func + "()")`
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
program begins...
entry : main()
entry : a()
entry : b()
entry : c()
entry : getpid()
`exit : getpid()`
`exit : c()`
`exit : b()`
`exit : a()`
`exit : main()`
```
]
.right-column[
```
$ cat scripts/simple.py
def uftrace_begin(ctx):
print("program begins...")
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
`def uftrace_exit(ctx):`
` func = ctx["name"]`
` print("exit : " + func + "()")`
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
program begins...
entry : main()
entry : a()
entry : b()
entry : c()
entry : getpid()
exit : getpid()
exit : c()
exit : b()
exit : a()
exit : main()
`program is finished`
```
]
.right-column[
```
$ cat scripts/simple.py
def uftrace_begin(ctx):
print("program begins...")
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
`def uftrace_end():`
` print("program is finished")`
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
program begins...
entry : main()
entry : a()
entry : b()
entry : c()
entry : getpid()
exit : getpid()
exit : c()
exit : b()
exit : a()
exit : main()
program is finished
# DURATION TID FUNCTION
[ 89491] | main() {
[ 89491] | a() {
[ 89491] | b() {
[ 89491] | c() {
4.993 us [ 89491] | getpid();
17.374 us [ 89491] | } /* c */
25.934 us [ 89491] | } /* b */
35.023 us [ 89491] | } /* a */
52.927 us [ 89491] | } /* main */
```
]
.right-column[
```
$ cat scripts/simple.py
def uftrace_begin(ctx):
print("program begins...")
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
program begins...
// entry : main()
// entry : a()
entry : `b`()
// entry : c()
entry : `getpid`()
exit : `getpid`()
// exit : c()
exit : `b`()
// exit : a()
// exit : main()
program is finished
# DURATION TID FUNCTION
[ 89491] | main() {
[ 89491] | a() {
[ 89491] | `b`() {
[ 89491] | c() {
4.993 us [ 89491] | `getpid`();
17.374 us [ 89491] | } /* c */
25.934 us [ 89491] | } /* b */
35.023 us [ 89491] | } /* a */
52.927 us [ 89491] | } /* main */
```
]
.right-column[
```
`UFTRACE_FUNCS = ["b", "getpid"]`
def uftrace_begin(ctx):
print("program begins...")
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### A Simple Python Script (simple.py)
.left-column[
```
$ uftrace -S scripts/simple.py tests/t-abc
`program begins...`
`entry : b()`
`entry : getpid()`
`exit : getpid()`
`exit : b()`
`program is finished`
# DURATION TID FUNCTION
[ 89491] | main() {
[ 89491] | a() {
[ 89491] | `b`() {
[ 89491] | c() {
4.993 us [ 89491] | `getpid`();
17.374 us [ 89491] | } /* c */
25.934 us [ 89491] | } /* b */
35.023 us [ 89491] | } /* a */
52.927 us [ 89491] | } /* main */
```
]
.right-column[
```
`UFTRACE_FUNCS = ["b", "getpid"]`
def uftrace_begin(ctx):
print("program begins...")
def uftrace_entry(ctx):
func = ctx["name"]
print("entry : " + func + "()")
def uftrace_exit(ctx):
func = ctx["name"]
print("exit : " + func + "()")
def uftrace_end():
print("program is finished")
```
]
---
class: code-20px
### Arguments Passing in Python Script
- uftrace script (python) APIs
- `uftrace_entry`(.red[ctx])
- `uftrace_exit`(.red[ctx])
- `uftrace_begin`(.red[ctx])
- `uftrace_end`()
```sh
/* context info passed to uftrace_entry(`ctx`) and uftrace_exit(`ctx`) */
script_context = {
int tid;
int depth;
long timestamp;
long duration; # exit only
long address;
string name;
list args; # entry only (if available)
value retval; # exit only (if available)
};
/* context info passed to uftrace_begin(`ctx`) */
script_context = {
bool record; # True if it runs at record time, otherwise False
string version; # uftrace version info
list cmds; # execution commands
};
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
```
$ uftrace record tests/t-abc
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
```
$ uftrace record `-S scripts/dump.py` tests/t-abc
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed by .red[uftrace_begin(ctx)]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
`uftrace_begin(ctx)`
` record : True`
` version : v0.9-57-gf112 ( dwarf python tui perf sched )`
` cmds : tests/t-abc`
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["record"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
` record : True`
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["version"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
` version : v0.9-57-gf112 ( dwarf python tui perf sched )`
cmds : tests/t-abc
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["cmds"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
` cmds : tests/t-abc`
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed by .red[uftrace_entry(ctx)]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
`18267435.348824687 87238: [entry] main(40072a) depth: 0`
` args[0] <type 'long'>: 1`
` args[1] <type 'long'>: 140731679137864`
`18267435.348891549 87238: [entry] a(4006cf) depth: 1`
`18267435.348912748 87238: [entry] b(4006e2) depth: 2`
`18267435.348926465 87238: [entry] c(4006f5) depth: 3`
`18267435.348939564 87238: [entry] getpid(400520) depth: 4`
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["timestamp"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
`18267435.348824687` 87238: [entry] main(40072a) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
`18267435.348891549` 87238: [entry] a(4006cf) depth: 1
`18267435.348912748` 87238: [entry] b(4006e2) depth: 2
`18267435.348926465` 87238: [entry] c(4006f5) depth: 3
`18267435.348939564` 87238: [entry] getpid(400520) depth: 4
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["tid"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 `87238`: [entry] main(40072a) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 `87238`: [entry] a(4006cf) depth: 1
18267435.348912748 `87238`: [entry] b(4006e2) depth: 2
18267435.348926465 `87238`: [entry] c(4006f5) depth: 3
18267435.348939564 `87238`: [entry] getpid(400520) depth: 4
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["name"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] `main`(40072a) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 87238: [entry] `a`(4006cf) depth: 1
18267435.348912748 87238: [entry] `b`(4006e2) depth: 2
18267435.348926465 87238: [entry] `c`(4006f5) depth: 3
18267435.348939564 87238: [entry] `getpid`(400520) depth: 4
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["address"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] main(`40072a`) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 87238: [entry] a(`4006cf`) depth: 1
18267435.348912748 87238: [entry] b(`4006e2`) depth: 2
18267435.348926465 87238: [entry] c(`4006f5`) depth: 3
18267435.348939564 87238: [entry] getpid(`400520`) depth: 4
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["depth"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] main(40072a) `depth`: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 87238: [entry] a(4006cf) `depth`: 1
18267435.348912748 87238: [entry] b(4006e2) `depth`: 2
18267435.348926465 87238: [entry] c(4006f5) `depth`: 3
18267435.348939564 87238: [entry] getpid(400520) `depth`: 4
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["args"][0]] and .red[ctx["args"][1]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] main(40072a) depth: 0
` args[0] <type 'long'>: 1`
` args[1] <type 'long'>: 140731679137864`
18267435.348891549 87238: [entry] a(4006cf) depth: 1
18267435.348912748 87238: [entry] b(4006e2) depth: 2
18267435.348926465 87238: [entry] c(4006f5) depth: 3
18267435.348939564 87238: [entry] getpid(400520) depth: 4
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed by .red[uftrace_exit(ctx)]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] main(40072a) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 87238: [entry] a(4006cf) depth: 1
18267435.348912748 87238: [entry] b(4006e2) depth: 2
18267435.348926465 87238: [entry] c(4006f5) depth: 3
18267435.348939564 87238: [entry] getpid(400520) depth: 4
`18267435.348939564 87238: [exit ] getpid(400520) depth: 4`
` retval <type 'long'>: 87238`
`18267435.348926465 87238: [exit ] c(4006f5) depth: 3`
` retval <type 'long'>: 87238`
`18267435.348912748 87238: [exit ] b(4006e2) depth: 2`
` retval <type 'long'>: 87239`
`18267435.348891549 87238: [exit ] a(4006cf) depth: 1`
` retval <type 'long'>: 87238`
`18267435.348824687 87238: [exit ] main(40072a) depth: 0`
` retval <type 'long'>: 0`
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed using .red[ctx["retval"]]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] main(40072a) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 87238: [entry] a(4006cf) depth: 1
18267435.348912748 87238: [entry] b(4006e2) depth: 2
18267435.348926465 87238: [entry] c(4006f5) depth: 3
18267435.348939564 87238: [entry] getpid(400520) depth: 4
18267435.348939564 87238: [exit ] getpid(400520) depth: 4
` retval <type 'long'>: 87238`
18267435.348926465 87238: [exit ] c(4006f5) depth: 3
` retval <type 'long'>: 87238`
18267435.348912748 87238: [exit ] b(4006e2) depth: 2
` retval <type 'long'>: 87239`
18267435.348891549 87238: [exit ] a(4006cf) depth: 1
` retval <type 'long'>: 87238`
18267435.348824687 87238: [exit ] main(40072a) depth: 0
` retval <type 'long'>: 0`
```
---
class: code-20px
### Arguments Passing in Python Script ([dump.py](https://github.com/namhyung/uftrace/blob/master/scripts/dump.py))
.footnote[printed by .red[uftrace_end()]]
```
$ uftrace record -S scripts/dump.py tests/t-abc
uftrace_begin(ctx)
record : True
version : v0.9-57-gf112 ( dwarf python tui perf sched )
cmds : tests/t-abc
18267435.348824687 87238: [entry] main(40072a) depth: 0
args[0] <type 'long'>: 1
args[1] <type 'long'>: 140731679137864
18267435.348891549 87238: [entry] a(4006cf) depth: 1
18267435.348912748 87238: [entry] b(4006e2) depth: 2
18267435.348926465 87238: [entry] c(4006f5) depth: 3
18267435.348939564 87238: [entry] getpid(400520) depth: 4
18267435.348939564 87238: [exit ] getpid(400520) depth: 4
retval <type 'long'>: 87238
18267435.348926465 87238: [exit ] c(4006f5) depth: 3
retval <type 'long'>: 87238
18267435.348912748 87238: [exit ] b(4006e2) depth: 2
retval <type 'long'>: 87239
18267435.348891549 87238: [exit ] a(4006cf) depth: 1
retval <type 'long'>: 87238
18267435.348824687 87238: [exit ] main(40072a) depth: 0
retval <type 'long'>: 0
`uftrace_end()`
```
---
name: dynamic-tracing
template: title-layout
# Dynamic Tracing
### dynamic function tracing enabled at runtime
.footnote[[doc/uftrace-record.md#dynamic-tracing](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-record.md#dynamic-tracing)]
---
### Dynamic tracing
- need (recent) compiler support
- add a [nop-sled](https://en.wikipedia.org/wiki/NOP_slide) when compile
- no performance overhead having NOPs
- convert it to call at runtime (load-time precisely)
---
### Dynamic tracing
- need (recent) compiler support
- add a [nop-sled](https://en.wikipedia.org/wiki/NOP_slide) when compile
- no performance overhead having NOPs
- convert it to call at runtime (load-time precisely)
- NOPs can be added by .red[-fpatchable-function-entry=N] option.
- The .red[N] should be 5 for x86_64 and 2 for aarch64.
- supported since gcc-8.1 and clang-10.
```
$ gcc `-fpatchable-function-entry=5` foobar.c
```
---
### Dynamic tracing
```
$ gcc `-fpatchable-function-entry=5` foobar.c
```
---
### Dynamic tracing
```
$ gcc -fpatchable-function-entry=5 foobar.c
$ uftrace a.out
`uftrace: /home/honggyu/work/uftrace/cmds/record.c:1503:check_binary`
ERROR: Can't find 'mcount' symbol in the 'a.out'.
It seems not to be compiled with -pg or -finstrument-functions flag
which generates traceable code. Please check your binary file.
```
---
### Dynamic tracing
```
$ gcc -fpatchable-function-entry=5 foobar.c
$ uftrace `-P foo` a.out
```
---
### Dynamic tracing
```
$ gcc -fpatchable-function-entry=5 foobar.c
$ uftrace `-P foo` a.out
# DURATION TID FUNCTION
0.459 us [ 4086] | `foo`();
```
---
### Dynamic tracing
```
$ gcc -fpatchable-function-entry=5 foobar.c
$ uftrace `-P main -P foo` a.out
# DURATION TID FUNCTION
[ 4153] | `main`() {
0.125 us [ 4153] | `foo`();
1.333 us [ 4153] | } /* main */
```
---
### Dynamic tracing
```
$ gcc -fpatchable-function-entry=5 foobar.c
$ uftrace `-P .` a.out
# DURATION TID FUNCTION
[ 4253] | `main`() {
[ 4253] | `foo`() {
0.083 us [ 4253] | `bar`();
1.208 us [ 4253] | } /* foo */
0.125 us [ 4253] | `bar`();
2.042 us [ 4253] | } /* main */
```
---
### Dynamic tracing
- The NOPs can be added by a compiler attribute [patchable_function_entry](https://clang.llvm.org/docs/AttributeReference.html#patchable-function-entry).
- add .red[N] NOPs only to the functions with the attribute.
- <span class="red">\_\_attribute\_\_((patchable_function_entry(N)))</span>
- doesn't require source to be compiled with a compiler flag.
---
### Dynamic tracing
- The NOPs can be added by a compiler attribute [patchable_function_entry](https://clang.llvm.org/docs/AttributeReference.html#patchable-function-entry).
- add .red[N] NOPs only to the functions with the attribute.
- <span class="red">\_\_attribute\_\_((patchable_function_entry(N)))</span>
- doesn't require source to be compiled with a compiler flag.
```
$ cat foobar.c
`__attribute__((patchable_function_entry(5)))`
void `bar`() {
}
`__attribute__((patchable_function_entry(5)))`
void `foo`() {
bar();
}
int main() {
foo();
bar();
}
```
---
### Dynamic tracing
- gcc compiles the source with no extra flags.
- but NOPs are added to the attributed functions.
.left30-column[
```
$ gcc foobar.c
<bar>:
* nop (5 times)
ret
<foo>:
* nop (5 times)
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
---
### Dynamic tracing
- gcc compiles the source with no extra flags.
- but NOPs are added to the attributed functions.
.left30-column[
```
$ gcc foobar.c
<bar>:
* nop (5 times)
ret
<foo>:
* nop (5 times)
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
.right70-column[
```
$ uftrace `--force` a.out
WARN: cannot open record data: /tmp/uftrace-live- ...
```
]
---
### Dynamic tracing
.footnote[.red[-P FUNC]/.red[--patch=FUNC] Patch FUNC dynamically]
- uftrace patches only NOPs to mcount call at runtime.
- `main` doesn't have NOPs so won't show up in the result.
.left30-column[
```
$ gcc foobar.c
<bar>:
* call <mcount@plt>
ret
<foo>:
* call <mcount@plt>
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
.right70-column[
```
$ uftrace `-P .` a.out
# DURATION TID FUNCTION
[ 14813] | `foo`() {
0.507 us [ 14813] | `bar`();
4.130 us [ 14813] | } /* foo */
0.240 us [ 14813] | `bar`();
```
]
---
name: full-dynamic-tracing
template: title-layout
# Full Dynamic Tracing
### full-dynamic function tracing without compiler support
.footnote[[doc/uftrace-record.md#full-dynamic-tracing](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-record.md#full-dynamic-tracing)]
<pre><code class="bash">$ ./configure
capstone: [ .green[on] ]</code></pre>
---
### Full Dynamic Tracing
- uftrace patches mcount call at runtime without compiler support.
.left30-column[
```
$ gcc foobar.c
<bar>:
ret
<foo>:
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
.right70-column[
```
```
]
---
### Full Dynamic Tracing
- uftrace patches mcount call at runtime without compiler support.
.left30-column[
```
$ gcc foobar.c
<bar>:
ret
<foo>:
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
.right70-column[
```
$ uftrace `--force` a.out
WARN: cannot open record data: /tmp/uftrace-live- ...
```
]
---
### Full Dynamic Tracing
.footnote[.red[-P FUNC]/.red[--patch=FUNC] Patch FUNC dynamically]
- uftrace patches mcount call at runtime without compiler support.
.left30-column[
```
$ gcc foobar.c
<bar>:
ret
<foo>:
call <bar>
ret
<main>:
* call <mcount@plt>
call <foo>
call <bar>
ret
```
]
.right70-column[
```
$ uftrace `-P main` a.out
# DURATION TID FUNCTION
1.420 us [ 14766] | `main`();
```
]
---
### Full Dynamic Tracing
.footnote[.red[-P FUNC]/.red[--patch=FUNC] Patch FUNC dynamically]
- uftrace patches mcount call at runtime without compiler support.
.left30-column[
```
$ gcc foobar.c
<bar>:
* call <mcount@plt>
ret
<foo>:
* call <mcount@plt>
call <bar>
ret
<main>:
call <foo>
call <bar>
ret
```
]
.right70-column[
```
$ uftrace `-P foo -P bar` a.out
# DURATION TID FUNCTION
[ 14813] | `foo`() {
0.507 us [ 14813] | `bar`();
4.130 us [ 14813] | } /* foo */
0.240 us [ 14813] | `bar`();
```
]
---
### Full Dynamic Tracing
.footnote[.red[-P FUNC]/.red[--patch=FUNC] Patch FUNC dynamically]
- uftrace patches mcount call at runtime without compiler support.
.left30-column[
```
$ gcc foobar.c
<bar>:
* call <mcount@plt>
ret
<foo>:
* call <mcount@plt>
call <bar>
ret
<main>:
* call <mcount@plt>
call <foo>
call <bar>
ret
```
]
.right70-column[
```
$ uftrace `-P .` a.out
# DURATION TID FUNCTION
[ 14830] | `main`() {
[ 14830] | `foo`() {
0.303 us [ 14830] | `bar`();
2.433 us [ 14830] | } /* foo */
0.176 us [ 14830] | `bar`();
4.917 us [ 14830] | } /* main */
```
]
---
name: trace-control
template: title-layout
# Triggers
### Trace Control with "trace_on" / "trace_off" / "finish"
.footnote[[doc/uftrace-record.md#triggers](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-record.md#triggers)]
---
class: code-24px
### Trace Control Triggers
<pre><code>
$ gcc -pg -g [tests/s-signal.c](https://raw.githubusercontent.com/namhyung/uftrace/master/tests/s-signal.c)
</code><pre>
```
$ uftrace -a a.out
# DURATION TID FUNCTION
[ 17249] | main(1, 0x7fffdc926f98) {
0.290 us [ 17249] | foo() = 0;
2.093 us [ 17249] | signal(SIGUSR1, &sighandler) = 0;
[ 17249] | raise(SIGUSR1) {
[ 17249] | sighandler(10) {
0.257 us [ 17249] | bar(10);
1.447 us [ 17249] | } /* sighandler */
12.186 us [ 17249] | } = 0; /* raise */
0.183 us [ 17249] | foo() = 10;
19.990 us [ 17249] | } = 0; /* main */
```
---
class: code-24px
### Trace Control Triggers (--trace=off)
.footnote[.red[--trace=off] starts uftrace with tracing disabled]
```
$ gcc -pg -g tests/s-signal.c
$ uftrace -a `--trace=off` a.out
WARN: cannot open record data: ... : No data available
```
---
class: code-24px
### Trace Control Triggers (trace_on)
.footnote[.red[trace_on] trigger enables uftrace to start tracing]
```
$ gcc -pg -g tests/s-signal.c
$ uftrace -a --trace=off `-T foo@trace_on` a.out
# DURATION TID FUNCTION
0.316 us [ 17381] | `foo`() = 0;
2.760 us [ 17381] | signal(SIGUSR1, &sighandler) = 0;
[ 17381] | raise(SIGUSR1) {
[ 17381] | sighandler(10) {
0.230 us [ 17381] | bar(10);
1.524 us [ 17381] | } /* sighandler */
13.487 us [ 17381] | } = 0; /* raise */
0.183 us [ 17381] | foo() = 10;
21.310 us [ 17381] | } = 0; /* main */
```
---
class: code-24px
### Trace Control Triggers (finish)
.footnote[.red[finish] trigger is to end recording]
```
$ gcc -pg -g tests/s-signal.c
$ uftrace -a `-T bar@finish` a.out
# DURATION TID FUNCTION
[ 17407] | main(1, 0x7ffd8aa8a3e8) {
0.267 us [ 17407] | foo() = 0;
2.210 us [ 17407] | signal(SIGUSR1, &sighandler) = 0;
[ 17407] | raise(SIGUSR1) {
[ 17407] | sighandler(10) {
[ 17407] | `bar`() {
[ 17407] | /* linux:task-exit */
uftrace stopped tracing with remaining functions
================================================
task: 172407
[3] bar
[2] sighandler
[1] raise
[0] main
```
---
class: code-24px
### Signal Trigger (--signal)
.footnote[.red[--signal] is to register a trigger action]
```
$ gcc -pg -g tests/s-signal.c
$ uftrace -a `--signal SIGUSR1@finish` a.out
uftrace: install signal handlers to task 172467
# DURATION TID FUNCTION
[ 17467] | main(1, 0x7ffc5d5e8158) {
0.330 us [ 17467] | foo() = 0;
2.113 us [ 17467] | signal(SIGUSR1, &sighandler) = 0;
[ 17467] | raise(`SIGUSR1`) {
[ 17467] | sighandler(10) {
0.233 us [ 17467] | bar(10);
1.456 us [ 17467] | } /* sighandler */
[ 17467] | /* `linux:task-exit` */
uftrace stopped tracing with remaining functions
================================================
task: 172467
[1] raise
[0] main
```
---
class: code-24px
### Signal Trigger (--signal)
.footnote[.red[--signal] is to register a trigger action]
```
$ gcc -pg -g tests/s-signal.c
$ uftrace -a --trace=off `--signal SIGUSR1@trace_on` a.out
uftrace: install signal handlers to task 175678
# DURATION TID FUNCTION
[ 17678] | } /* sighandler */
3.230 us [ 17678] | } = 0; /* raise */
0.217 us [ 17678] | foo() = 10;
5.700 us [ 17678] | } = 0; /* main */
```
---
name: python-tracing
template: title-layout
# Python Function Tracing
### Tracing Support for Python Language
---
### Python Function Tracing
- uftrace used to be only for compiled native binaries.
- i.e. Linux ELF binaries
- uftrace now supports python program tracing as well.
- with .red[uftrace.py] and .red[uftrace_python.so]
- Python .red[sys.setprofile()] provides a way to get hooks
- for each Python function entry and exit (.red[`call`] and .red[`return`] precisely)
- record trace data as if -finstrument-functions compiled binaries.
- at `__cyg_profile_func_enter` and `__cyg_profile_func_exit`
- create a fake symbol table for python functions.
---
class: code-20px
### Python Function Tracing
- uftrace is able to trace python scripts.
---
class: code-20px
### Python Function Tracing
- uftrace is able to trace python scripts.
.left30-column[
```py
$ cat tests/s-abc.py
#!/usr/bin/env python3
import os
def a():
b()
def b():
c()
def c():
return os.getpid()
a()
```
]
---
class: code-20px
### Python Function Tracing
- uftrace is able to trace python scripts.
.left30-column[
```py
$ cat tests/s-abc.py
#!/usr/bin/env python3
import os
def a():
b()
def b():
c()
def c():
return os.getpid()
a()
```
]
.right70-column[
```
$ uftrace s-abc.py
# DURATION TID FUNCTION
[378162] | __main__.<module>() {
[378162] | a() {
[378162] | b() {
[378162] | c() {
0.466 us [378162] | posix.getpid();
3.034 us [378162] | } /* c */
4.970 us [378162] | } /* b */
7.557 us [378162] | } /* a */
10.704 us [378162] | } /* __main__.<module> */
```
]
---
class: code-20px
### Python Function Tracing
- .red[--srcline] shows source locations of python functions.
.left30-column[
```py
$ cat tests/s-abc.py
#!/usr/bin/env python3
import os
def a():
b()
def b():
c()
def c():
return os.getpid()
a()
```
]
.right70-column[
```
$ uftrace `--srcline` s-abc.py
# DURATION TID FUNCTION
[378246] | __main__.<module>() { /* `s-abc.py:1` */
[378246] | a() { /* `s-abc.py:4` */
[378246] | b() { /* `s-abc.py:7` */
[378246] | c() { /* `s-abc.py:10` */
0.539 us [378246] | posix.getpid();
2.972 us [378246] | } /* c */
4.745 us [378246] | } /* b */
6.321 us [378246] | } /* a */
10.378 us [378246] | } /* __main__.<module> */
```
]
---
class: code-19px
### Python Function Tracing
- uftrace shows imported functions, but discards their internals.
---
class: code-19px
### Python Function Tracing
- uftrace shows imported functions, but discards their internals.
```py
$ cat s-file-var.py
#!/usr/bin/env python3
import os
def foo():
print(os.path.basename(__file__))
foo()
```
---
class: code-19px
### Python Function Tracing
- uftrace shows imported functions, but discards their internals.
```py
$ cat s-file-var.py
#!/usr/bin/env python3
import os
def foo():
print(os.path.basename(__file__))
foo()
```
```
$ uftrace s-file-var.py
s-file-var.py
# DURATION TID FUNCTION
[378976] | __main__.<module>() {
[378976] | foo() {
10.166 us [378976] | posixpath.basename();
7.844 us [378976] | builtins.print();
29.320 us [378976] | } /* foo */
32.321 us [378976] | } /* __main__.<module> */
```
---
class: code-19px
### Python Function Tracing
- .red[--no-libcall] discards imported functions.
```py
$ cat s-file-var.py
#!/usr/bin/env python3
import os
def foo():
print(os.path.basename(__file__))
foo()
```
```
$ uftrace `--no-libcall` s-file-var.py
s-file-var.py
# DURATION TID FUNCTION
[378985] | __main__.<module>() {
22.894 us [378985] | foo();
25.991 us [378985] | } /* __main__.<module> */
```
---
class: code-19px
### Python Function Tracing
- uftrace shows imported functions, but discards their internals.
```py
$ cat s-file-var.py
#!/usr/bin/env python3
import os
def foo():
print(os.path.basename(__file__))
foo()
```
```
$ uftrace s-file-var.py
s-file-var.py
# DURATION TID FUNCTION
[378976] | __main__.<module>() {
[378976] | foo() {
10.166 us [378976] | posixpath.basename();
7.844 us [378976] | builtins.print();
29.320 us [378976] | } /* foo */
32.321 us [378976] | } /* __main__.<module> */
```
---
class: code-19px
### Python Function Tracing
- .red[-l]/.red[--nest-libcall] shows all the internally imported functions.
```py
$ cat s-file-var.py
#!/usr/bin/env python3
import os
def foo():
print(os.path.basename(__file__))
foo()
```
```
$ uftrace `--nest-libcall` s-file-var.py
s-file-var.py
# DURATION TID FUNCTION
[379000] | __main__.<module>() {
[379000] | foo() {
[379000] | posixpath.basename() {
0.427 us [379000] | `posix.fspath`();
[379000] | `posixpath._get_sep`() {
0.291 us [379000] | `builtins.isinstance`();
1.999 us [379000] | } /* posixpath._get_sep */
0.642 us [379000] | `str.rfind`();
10.331 us [379000] | } /* posixpath.basename */
7.977 us [379000] | builtins.print();
23.021 us [379000] | } /* foo */
26.175 us [379000] | } /* __main__.<module> */
```
---
### Python Function Tracing
- Other commands can be used in the same way.
---
### Python Function Tracing
- Other commands can be used in the same way.
```
$ uftrace `record` s-file-var.py
```
---
### Python Function Tracing
- Other commands can be used in the same way.
```
$ uftrace `record` s-file-var.py
$ uftrace `report`
Total time Self time Calls Function
========== ========== ========== ====================
24.617 us 2.835 us 1 __main__.<module>
21.782 us 4.250 us 1 foo
11.136 us 11.136 us 1 posixpath.basename
6.396 us 6.396 us 1 builtins.print
```
---
### Python Function Tracing
- Other commands can be used in the same way.
```
$ uftrace `record` s-file-var.py
$ uftrace `report`
Total time Self time Calls Function
========== ========== ========== ====================
24.617 us 2.835 us 1 __main__.<module>
21.782 us 4.250 us 1 foo
11.136 us 11.136 us 1 posixpath.basename
6.396 us 6.396 us 1 builtins.print
$ uftrace `graph`
# Function Call Graph for 'python3.10' (session: e12caa4f83352a9a)
========== FUNCTION CALL GRAPH ==========
# TOTAL TIME FUNCTION
24.617 us : (1) python3.10
24.617 us : (1) __main__.<module>
21.782 us : (1) foo
11.136 us : +-(1) posixpath.basename
: |
6.396 us : +-(1) builtins.print
```
---
class: code-14px
### Python Function Tracing for tensorflow and keras
- More compilicated python program can be traced as follows.
- [mnist_convnet.py](https://github.com/keras-team/keras-io/blob/ebe14fb985afd908e10dba8cbca179e5491e6d58/examples/vision/mnist_convnet.py)
---
class: code-14px
### Python Function Tracing for tensorflow and keras
- More compilicated python program can be traced as follows.
- [mnist_convnet.py](https://github.com/keras-team/keras-io/blob/ebe14fb985afd908e10dba8cbca179e5491e6d58/examples/vision/mnist_convnet.py)
```
$ uftrace `record` `--nest-libcall` mnist_convnet.py
...
Total params: 34826 (136.04 KB)
Trainable params: 34826 (136.04 KB)
Non-trainable params: 0 (0.00 Byte)
...
Epoch 15/15
422/422 [============] - 8s 19ms/step - loss: 0.0322 - accuracy: 0.9900 - val_loss: 0.0295 - val_accuracy: 0.9918
Test loss: 0.026609841734170914
Test accuracy: 0.9916999936103821
```
---
class: code-14px
### Python Function Tracing for tensorflow and keras
- More compilicated python program can be traced as follows.
- [mnist_convnet.py](https://github.com/keras-team/keras-io/blob/ebe14fb985afd908e10dba8cbca179e5491e6d58/examples/vision/mnist_convnet.py)
```
$ uftrace `record` `--nest-libcall` mnist_convnet.py
...
Total params: 34826 (136.04 KB)
Trainable params: 34826 (136.04 KB)
Non-trainable params: 0 (0.00 Byte)
...
Epoch 15/15
422/422 [============] - 8s 19ms/step - loss: 0.0322 - accuracy: 0.9900 - val_loss: 0.0295 - val_accuracy: 0.9918
Test loss: 0.026609841734170914
Test accuracy: 0.9916999936103821
$ uftrace `replay` -N importlib._bootstrap._find_and_load `-t 1s`
# DURATION TID FUNCTION
[ 6169] | __main__.<module>() {
[ 6169] | keras.src.utils.traceback_utils.error_handler() {
[ 6169] | keras.src.engine.training.fit() {
[ 6169] | tensorflow.python.util.traceback_utils.error_handler() {
[ 6169] | tensorflow.python.eager.polymorphic_function.polymorphic_function.__call__() {
1.503 s [ 6169] | tensorflow.python.eager.polymorphic_function.polymorphic_function._call();
1.503 s [ 6169] | } /* tensorflow.python.eager.polymorphic_function.polymorphic_function.__call__ */
1.503 s [ 6169] | } /* tensorflow.python.util.traceback_utils.error_handler */
2.001 `m` [ 6169] | } /* keras.src.engine.training.fit */
2.001 `m` [ 6169] | } /* keras.src.utils.traceback_utils.error_handler */
[ 6169] | keras.src.utils.traceback_utils.error_handler() {
1.098 s [ 6169] | keras.src.engine.training.evaluate();
1.098 s [ 6169] | } /* keras.src.utils.traceback_utils.error_handler */
5.010 `m` [ 6169] | } /* __main__.<module> */
```
---
class: code-14px
### Python Function Tracing for tensorflow and keras
- More compilicated python program can be traced as follows.
- [mnist_convnet.py](https://github.com/keras-team/keras-io/blob/ebe14fb985afd908e10dba8cbca179e5491e6d58/examples/vision/mnist_convnet.py)
```
$ uftrace `record` `--nest-libcall` mnist_convnet.py
...
Total params: 34826 (136.04 KB)
Trainable params: 34826 (136.04 KB)
Non-trainable params: 0 (0.00 Byte)
...
Epoch 15/15
422/422 [============] - 8s 19ms/step - loss: 0.0322 - accuracy: 0.9900 - val_loss: 0.0295 - val_accuracy: 0.9918
Test loss: 0.026609841734170914
Test accuracy: 0.9916999936103821
$ uftrace `replay` -N importlib._bootstrap._find_and_load `-t 1s`
# DURATION TID FUNCTION
[ 6169] | __main__.<module>() {
[ 6169] | keras.src.utils.traceback_utils.error_handler() {
[ 6169] | keras.src.engine.training.fit() {
[ 6169] | tensorflow.python.util.traceback_utils.error_handler() {
[ 6169] | tensorflow.python.eager.polymorphic_function.polymorphic_function.__call__() {
1.503 s [ 6169] | tensorflow.python.eager.polymorphic_function.polymorphic_function._call();
1.503 s [ 6169] | } /* tensorflow.python.eager.polymorphic_function.polymorphic_function.__call__ */
1.503 s [ 6169] | } /* tensorflow.python.util.traceback_utils.error_handler */
2.001 `m` [ 6169] | } /* keras.src.engine.training.fit */
2.001 `m` [ 6169] | } /* keras.src.utils.traceback_utils.error_handler */
[ 6169] | keras.src.utils.traceback_utils.error_handler() {
1.098 s [ 6169] | keras.src.engine.training.evaluate();
1.098 s [ 6169] | } /* keras.src.utils.traceback_utils.error_handler */
5.010 `m` [ 6169] | } /* __main__.<module> */
$ uftrace `tui` `-t 10ms`
```
---
class: tui2
<pre>
<code class="plain"> <x-tui-blue-line2> TOTAL TIME : FUNCTION</x-tui-blue-line2>
2.010 <x-red>m</x-red> : (1) python3.10</x-tui-white-line2>
2.010 <x-red>m</x-red> : (1) __main__.<module>
7.556 <x-yellow>s</x-yellow> : ├▶(2) importlib._bootstrap._find_and_load
: │
215.860 <x-green>ms</x-green> : ├▶(1) keras.src.datasets.mnist.load_data
: │
20.716 <x-green>ms</x-green> : ├─(1) ndarray.astype
: │
2.002 <x-red>m</x-red> : ├─(4) keras.src.utils.traceback_utils.error_handler
13.893 <x-green>ms</x-green> : │ ├─(1) keras.src.engine.input_layer.Input
: │ │
19.355 <x-green>ms</x-green> : │ ├─(1) keras.src.engine.training.compile
: │ │
2.001 <x-red>m</x-red> : │ ├─(1) keras.src.engine.training.fit
278.522 <x-green>ms</x-green> : │ │ ├▶(2) keras.src.engine.data_adapter.get_data_handler
: │ │ │
13.048 <x-green>ms</x-green> : │ │ ├▶(1) keras.src.engine.data_adapter.enumerate_epochs
: │ │ │
1.050 <x-red>m</x-red> : │ │ ├─(6330) tensorflow.python.util.traceback_utils.error_handler
1.050 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python.eager.polymorphic_function.polymorphic_function.__call__
1.049 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python.eager.polymorphic_function.polymorphic_function._call
778.787 <x-green>ms</x-green> : │ │ │ ├▶(1) tensorflow.python.eager.polymorphic_function.polymorphic_function._initialize
: │ │ │ │
1.047 <x-red>m</x-red> : │ │ │ └─(6330) tensorflow.python.eager.polymorphic_function.tracing_compilation.call_function
1.613 <x-yellow>s</x-yellow> : │ │ │ ├▶(5) tensorflow.python.eager.polymorphic_function.tracing_compilation.trace_function
: │ │ │ │
1.037 <x-red>m</x-red> : │ │ │ └─(6330) tensorflow.python.eager.polymorphic_function.concrete_function._call_flat
1.037 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python.eager.polymorphic_function.atomic_function.call_preflattened
1.037 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python.eager.polymorphic_function.atomic_function.call_flat
1.036 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python.eager.context.call_function
<x-tui-white-line2> 1.036 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python.eager.execute.quick_execute</x-tui-white-line2>
1.036 <x-red>m</x-red> : │ │ │ (6330) tensorflow.python._pywrap_tfe.PyCapsule.TFE_Py_Execute
: │ │ │
4.691 <x-yellow>s</x-yellow> : │ │ └▶(15) keras.src.utils.traceback_utils.error_handler
: │ │
1.098 <x-yellow>s</x-yellow> : │ └▶(1) keras.src.engine.training.evaluate
: │
154.935 <x-green>ms</x-green> : └▶(1) tensorflow.python.trackable.base._method_wrapper
<x-tui-blue-line2>uftrace graph: /home/honggyu/.local/lib/python3.10/site-packages/tensorflow/python/eager/execute.py [line:28]</x-tui-blue-line2>
</code>
</pre>
---
template: title-layout
# For more information
---
### man pages
- uftrace provides manual pages for each command
- [man uftrace](https://github.com/namhyung/uftrace/blob/master/doc/uftrace.md)
- [man uftrace record](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-record.md)
- [man uftrace replay](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-replay.md)
- [man uftrace live](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-live.md)
- [man uftrace report](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-report.md)
- [man uftrace dump](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-dump.md)
- [man uftrace graph](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-graph.md)
- [man uftrace script](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-script.md)
- [man uftrace tui](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-tui.md)
- [man uftrace info](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-info.md)
- [man uftrace recv](https://github.com/namhyung/uftrace/blob/master/doc/uftrace-recv.md)
---
name:end
template: title-layout
# Thanks!
### https://github.com/namhyung/uftrace
</textarea>
<script src="https://remarkjs.com/downloads/remark-latest.min.js"></script>
<script>
var hljs = remark.highlighter.engine;
</script>
<script src="https://remarkjs.com/remark.language.js"></script>
<script>
var slideshow = remark.create({
// Set the slideshow display ratio
// Default: '4:3'
// Alternatives: '16:9', ...
ratio: '4:3',
// Syntax highlighting theme
highlightStyle: 'ir-black',
highlightLanguage: 'plain',
highlightLines: true,
highlightSpans: true,
// Customize slide number label, either using a format string..
slideNumberFormat: '%current% / %total%',
//sourceUrl: 'extern.md',
}) ;
</script>
<script>
var comment = /(\/\*.*\*\/)/g;
var line_comment = /^\/\/(.*)/g;
var lines = document.getElementsByClassName('remark-code-line');
console.log(lines);
for (var i = 0; i < lines.length; i++) {
lines[i].innerHTML = lines[i].innerHTML.replace(comment, '<x-grey>$1</x-grey>');
lines[i].innerHTML = lines[i].innerHTML.replace(line_comment, '<x-grey>$1</x-grey>');
}
function rightPad(str, padch, padlen) {
var pad = '';
var tag = /<[a-z\-/]+>/g;
padlen -= str.replace(tag, '').length;
for (var j = 0; j < padlen; j++)
pad += padch;
return str + pad;
}
var tui_blue_lines = document.getElementsByTagName('x-tui-blue-line');
for (var i = 0; i < tui_blue_lines.length; i++) {
var elem = tui_blue_lines[i];
elem.innerHTML = rightPad(elem.innerHTML, ' ', 101);
}
var tui_white_lines = document.getElementsByTagName('x-tui-white-line');
for (var i = 0; i < tui_white_lines.length; i++) {
var elem = tui_white_lines[i];
elem.innerHTML = rightPad(elem.innerHTML, ' ', 101);
}
var tui_blue_lines2 = document.getElementsByTagName('x-tui-blue-line2');
for (var i = 0; i < tui_blue_lines2.length; i++) {
var elem = tui_blue_lines2[i];
elem.innerHTML = rightPad(elem.innerHTML, ' ', 118);
}
var tui_white_lines2 = document.getElementsByTagName('x-tui-white-line2');
for (var i = 0; i < tui_white_lines2.length; i++) {
var elem = tui_white_lines2[i];
elem.innerHTML = rightPad(elem.innerHTML, ' ', 118);
}
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-125043112-1"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-125043112-1');
</script>
</body>
</html>
|