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
|
/*
Copyright (C) 2021 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _WIN32
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <unistd.h>
#endif
#include <stdio.h>
#include <algorithm>
#include "sinsp.h"
#include "sinsp_int.h"
#include "protodecoder.h"
#include "tracers.h"
#ifdef HAS_ANALYZER
#include "tracer_emitter.h"
#endif
extern sinsp_evttables g_infotables;
static void copy_ipv6_address(uint32_t* dest, uint32_t* src)
{
dest[0] = src[0];
dest[1] = src[1];
dest[2] = src[2];
dest[3] = src[3];
}
///////////////////////////////////////////////////////////////////////////////
// sinsp_threadinfo implementation
///////////////////////////////////////////////////////////////////////////////
sinsp_threadinfo::sinsp_threadinfo(sinsp* inspector) :
m_tracer_parser(NULL),
m_inspector(inspector),
m_fdtable(inspector)
{
init();
}
void sinsp_threadinfo::init()
{
m_pid = (uint64_t) - 1LL;
m_sid = (uint64_t) - 1LL;
m_vpgid = (uint64_t) - 1LL;
set_lastevent_data_validity(false);
m_lastevent_type = -1;
m_lastevent_ts = 0;
m_prevevent_ts = 0;
m_lastaccess_ts = 0;
m_clone_ts = 0;
m_lastevent_category.m_category = EC_UNKNOWN;
m_flags = PPM_CL_NAME_CHANGED;
m_nchilds = 0;
m_fdlimit = -1;
m_vmsize_kb = 0;
m_vmrss_kb = 0;
m_vmswap_kb = 0;
m_pfmajor = 0;
m_pfminor = 0;
m_vtid = -1;
m_vpid = -1;
m_main_thread.reset();
m_lastevent_fd = 0;
#ifdef HAS_FILTERING
m_last_latency_entertime = 0;
m_latency = 0;
#endif
m_program_hash = 0;
m_program_hash_scripts = 0;
m_lastevent_data = NULL;
m_parent_loop_detected = false;
m_tty = 0;
m_category = CAT_NONE;
m_blprogram = NULL;
m_loginuid = 0;
}
sinsp_threadinfo::~sinsp_threadinfo()
{
uint32_t j;
for(j = 0; j < m_private_state.size(); j++)
{
free(m_private_state[j]);
}
m_private_state.clear();
if(m_lastevent_data)
{
free(m_lastevent_data);
}
if(m_tracer_parser)
{
delete m_tracer_parser;
}
}
void sinsp_threadinfo::fix_sockets_coming_from_proc()
{
unordered_map<int64_t, sinsp_fdinfo_t>::iterator it;
for(it = m_fdtable.m_table.begin(); it != m_fdtable.m_table.end(); it++)
{
if(it->second.m_type == SCAP_FD_IPV4_SOCK)
{
if(m_inspector->m_thread_manager->m_server_ports.find(it->second.m_sockinfo.m_ipv4info.m_fields.m_sport) !=
m_inspector->m_thread_manager->m_server_ports.end())
{
uint32_t tip;
uint16_t tport;
tip = it->second.m_sockinfo.m_ipv4info.m_fields.m_sip;
tport = it->second.m_sockinfo.m_ipv4info.m_fields.m_sport;
it->second.m_sockinfo.m_ipv4info.m_fields.m_sip = it->second.m_sockinfo.m_ipv4info.m_fields.m_dip;
it->second.m_sockinfo.m_ipv4info.m_fields.m_dip = tip;
it->second.m_sockinfo.m_ipv4info.m_fields.m_sport = it->second.m_sockinfo.m_ipv4info.m_fields.m_dport;
it->second.m_sockinfo.m_ipv4info.m_fields.m_dport = tport;
it->second.m_name = ipv4tuple_to_string(&it->second.m_sockinfo.m_ipv4info, m_inspector->m_hostname_and_port_resolution_enabled);
it->second.set_role_server();
}
else
{
it->second.set_role_client();
}
}
}
}
#define STR_AS_NUM_JAVA 0x6176616a
#define STR_AS_NUM_RUBY 0x79627572
#define STR_AS_NUM_PERL 0x6c726570
#define STR_AS_NUM_NODE 0x65646f6e
#define MAX_PROG_HASH_LEN 1024
void sinsp_threadinfo::compute_program_hash()
{
auto curr_hash = std::hash<std::string>()(m_exe);
hash_combine(curr_hash, m_container_id);
auto rem_len = MAX_PROG_HASH_LEN - (m_exe.size() + m_container_id.size());
//
// By default, the scripts hash is just exe+container
//
m_program_hash_scripts = curr_hash;
//
// The program hash includes the arguments as well
//
for (auto arg = m_args.begin(); arg != m_args.end() && rem_len > 0; ++arg)
{
if (arg->size() >= rem_len)
{
auto partial_str = arg->substr(0, rem_len);
hash_combine(curr_hash, partial_str);
break;
}
hash_combine(curr_hash, *arg);
rem_len -= arg->size();
}
m_program_hash = curr_hash;
//
// For some specific processes (essentially the scripting languages)
// we include the arguments in the scripts hash as well
//
if(m_comm.size() == 4)
{
uint32_t ncomm = *(uint32_t*)m_comm.c_str();
if(ncomm == STR_AS_NUM_JAVA || ncomm == STR_AS_NUM_RUBY ||
ncomm == STR_AS_NUM_PERL || ncomm == STR_AS_NUM_NODE)
{
m_program_hash_scripts = m_program_hash;
}
}
else if(m_comm.size() >= 6)
{
if(m_comm.substr(0, 6) == "python")
{
m_program_hash_scripts = m_program_hash;
}
}
}
void sinsp_threadinfo::add_fd_from_scap(scap_fdinfo *fdi, OUT sinsp_fdinfo_t *res)
{
sinsp_fdinfo_t* newfdi = res;
newfdi->reset();
bool do_add = true;
newfdi->m_type = fdi->type;
newfdi->m_openflags = 0;
newfdi->m_type = fdi->type;
newfdi->m_flags = sinsp_fdinfo_t::FLAGS_FROM_PROC;
newfdi->m_ino = fdi->ino;
switch(newfdi->m_type)
{
case SCAP_FD_IPV4_SOCK:
newfdi->m_sockinfo.m_ipv4info.m_fields.m_sip = fdi->info.ipv4info.sip;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_dip = fdi->info.ipv4info.dip;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_sport = fdi->info.ipv4info.sport;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_dport = fdi->info.ipv4info.dport;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_l4proto = fdi->info.ipv4info.l4proto;
if(fdi->info.ipv4info.l4proto == SCAP_L4_TCP)
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_SOCKET_CONNECTED;
}
if(m_inspector->m_network_interfaces)
{
m_inspector->m_network_interfaces->update_fd(newfdi);
}
newfdi->m_name = ipv4tuple_to_string(&newfdi->m_sockinfo.m_ipv4info, m_inspector->m_hostname_and_port_resolution_enabled);
break;
case SCAP_FD_IPV4_SERVSOCK:
newfdi->m_sockinfo.m_ipv4serverinfo.m_ip = fdi->info.ipv4serverinfo.ip;
newfdi->m_sockinfo.m_ipv4serverinfo.m_port = fdi->info.ipv4serverinfo.port;
newfdi->m_sockinfo.m_ipv4serverinfo.m_l4proto = fdi->info.ipv4serverinfo.l4proto;
newfdi->m_name = ipv4serveraddr_to_string(&newfdi->m_sockinfo.m_ipv4serverinfo, m_inspector->m_hostname_and_port_resolution_enabled);
//
// We keep note of all the host bound server ports.
// We'll need them later when patching connections direction.
//
m_inspector->m_thread_manager->m_server_ports.insert(newfdi->m_sockinfo.m_ipv4serverinfo.m_port);
break;
case SCAP_FD_IPV6_SOCK:
if(sinsp_utils::is_ipv4_mapped_ipv6((uint8_t*)&fdi->info.ipv6info.sip) &&
sinsp_utils::is_ipv4_mapped_ipv6((uint8_t*)&fdi->info.ipv6info.dip))
{
//
// This is an IPv4-mapped IPv6 addresses (http://en.wikipedia.org/wiki/IPv6#IPv4-mapped_IPv6_addresses).
// Convert it into the IPv4 representation.
//
newfdi->m_type = SCAP_FD_IPV4_SOCK;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_sip = fdi->info.ipv6info.sip[3];
newfdi->m_sockinfo.m_ipv4info.m_fields.m_dip = fdi->info.ipv6info.dip[3];
newfdi->m_sockinfo.m_ipv4info.m_fields.m_sport = fdi->info.ipv6info.sport;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_dport = fdi->info.ipv6info.dport;
newfdi->m_sockinfo.m_ipv4info.m_fields.m_l4proto = fdi->info.ipv6info.l4proto;
if(fdi->info.ipv6info.l4proto == SCAP_L4_TCP)
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_SOCKET_CONNECTED;
}
if(m_inspector->m_network_interfaces)
{
m_inspector->m_network_interfaces->update_fd(newfdi);
}
newfdi->m_name = ipv4tuple_to_string(&newfdi->m_sockinfo.m_ipv4info, m_inspector->m_hostname_and_port_resolution_enabled);
}
else
{
copy_ipv6_address(newfdi->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b, fdi->info.ipv6info.sip);
copy_ipv6_address(newfdi->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b, fdi->info.ipv6info.dip);
newfdi->m_sockinfo.m_ipv6info.m_fields.m_sport = fdi->info.ipv6info.sport;
newfdi->m_sockinfo.m_ipv6info.m_fields.m_dport = fdi->info.ipv6info.dport;
newfdi->m_sockinfo.m_ipv6info.m_fields.m_l4proto = fdi->info.ipv6info.l4proto;
if(fdi->info.ipv6info.l4proto == SCAP_L4_TCP)
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_SOCKET_CONNECTED;
}
newfdi->m_name = ipv6tuple_to_string(&newfdi->m_sockinfo.m_ipv6info, m_inspector->m_hostname_and_port_resolution_enabled);
}
break;
case SCAP_FD_IPV6_SERVSOCK:
copy_ipv6_address(newfdi->m_sockinfo.m_ipv6serverinfo.m_ip.m_b, fdi->info.ipv6serverinfo.ip);
newfdi->m_sockinfo.m_ipv6serverinfo.m_port = fdi->info.ipv6serverinfo.port;
newfdi->m_sockinfo.m_ipv6serverinfo.m_l4proto = fdi->info.ipv6serverinfo.l4proto;
newfdi->m_name = ipv6serveraddr_to_string(&newfdi->m_sockinfo.m_ipv6serverinfo, m_inspector->m_hostname_and_port_resolution_enabled);
//
// We keep note of all the host bound server ports.
// We'll need them later when patching connections direction.
//
m_inspector->m_thread_manager->m_server_ports.insert(newfdi->m_sockinfo.m_ipv6serverinfo.m_port);
break;
case SCAP_FD_UNIX_SOCK:
newfdi->m_sockinfo.m_unixinfo.m_fields.m_source = fdi->info.unix_socket_info.source;
newfdi->m_sockinfo.m_unixinfo.m_fields.m_dest = fdi->info.unix_socket_info.destination;
newfdi->m_name = fdi->info.unix_socket_info.fname;
if(newfdi->m_name.empty())
{
newfdi->set_role_client();
}
else
{
newfdi->set_role_server();
}
break;
case SCAP_FD_FILE_V2:
newfdi->m_openflags = fdi->info.regularinfo.open_flags;
newfdi->m_name = fdi->info.regularinfo.fname;
newfdi->m_dev = fdi->info.regularinfo.dev;
newfdi->m_mount_id = fdi->info.regularinfo.mount_id;
if(newfdi->m_name == USER_EVT_DEVICE_NAME)
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE;
}
else
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD;
}
break;
case SCAP_FD_FIFO:
case SCAP_FD_FILE:
case SCAP_FD_DIRECTORY:
case SCAP_FD_UNSUPPORTED:
case SCAP_FD_SIGNALFD:
case SCAP_FD_EVENTPOLL:
case SCAP_FD_EVENT:
case SCAP_FD_INOTIFY:
case SCAP_FD_TIMERFD:
case SCAP_FD_NETLINK:
newfdi->m_name = fdi->info.fname;
if(newfdi->m_name == USER_EVT_DEVICE_NAME)
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_TRACER_FILE;
}
else
{
newfdi->m_flags |= sinsp_fdinfo_t::FLAGS_IS_NOT_TRACER_FD;
}
break;
default:
ASSERT(false);
do_add = false;
break;
}
//
// Call the protocol decoder callbacks associated to notify them about this FD
//
ASSERT(m_inspector != NULL);
vector<sinsp_protodecoder*>::iterator it;
for(it = m_inspector->m_parser->m_open_callbacks.begin();
it != m_inspector->m_parser->m_open_callbacks.end(); ++it)
{
(*it)->on_fd_from_proc(newfdi);
}
//
// Add the FD to the table
//
if(do_add)
{
m_fdtable.add(fdi->fd, newfdi);
}
}
void sinsp_threadinfo::init(scap_threadinfo* pi)
{
scap_fdinfo *fdi;
scap_fdinfo *tfdi;
init();
m_tid = pi->tid;
m_pid = pi->pid;
m_ptid = pi->ptid;
m_sid = pi->sid;
m_vpgid = pi->vpgid;
m_comm = pi->comm;
m_exe = pi->exe;
m_exepath = pi->exepath;
m_exe_writable = pi->exe_writable;
set_args(pi->args, pi->args_len);
if(is_main_thread())
{
set_env(pi->env, pi->env_len);
set_cwd(pi->cwd, (uint32_t)strlen(pi->cwd));
}
m_flags |= pi->flags;
m_flags |= PPM_CL_ACTIVE; // Assume that all the threads coming from /proc are real, active threads
m_fdtable.clear();
m_fdtable.m_tid = m_tid;
m_fdlimit = pi->fdlimit;
m_uid = pi->uid;
m_gid = pi->gid;
m_vmsize_kb = pi->vmsize_kb;
m_vmrss_kb = pi->vmrss_kb;
m_vmswap_kb = pi->vmswap_kb;
m_pfmajor = pi->pfmajor;
m_pfminor = pi->pfminor;
m_nchilds = 0;
m_vtid = pi->vtid;
m_vpid = pi->vpid;
m_clone_ts = pi->clone_ts;
m_tty = pi->tty;
m_loginuid = pi->loginuid;
m_category = CAT_NONE;
set_cgroups(pi->cgroups, pi->cgroups_len);
m_root = pi->root;
ASSERT(m_inspector);
m_inspector->m_container_manager.resolve_container(this, !m_inspector->is_capture());
//
// Prepare for filtering
//
sinsp_fdinfo_t tfdinfo;
sinsp_evt tevt;
scap_evt tscapevt;
//
// Initialize the fake events for filtering
//
tscapevt.ts = 0;
tscapevt.type = PPME_SYSCALL_READ_X;
tscapevt.len = 0;
tscapevt.nparams = 0;
tevt.m_inspector = m_inspector;
tevt.m_info = &(g_infotables.m_event_info[PPME_SYSCALL_READ_X]);
tevt.m_pevt = NULL;
tevt.m_cpuid = 0;
tevt.m_evtnum = 0;
tevt.m_pevt = &tscapevt;
bool match = false;
HASH_ITER(hh, pi->fdlist, fdi, tfdi)
{
add_fd_from_scap(fdi, &tfdinfo);
if(m_inspector->m_filter != NULL && m_inspector->m_filter_proc_table_when_saving)
{
tevt.m_tinfo = this;
tevt.m_fdinfo = &tfdinfo;
tscapevt.tid = m_tid;
int64_t tlefd = tevt.m_tinfo->m_lastevent_fd;
tevt.m_tinfo->m_lastevent_fd = fdi->fd;
if(m_inspector->m_filter->run(&tevt))
{
match = true;
}
else
{
//
// This tells scap not to include this FD in the write file
//
fdi->type = SCAP_FD_UNINITIALIZED;
}
tevt.m_tinfo->m_lastevent_fd = tlefd;
}
}
m_lastevent_data = NULL;
if(m_inspector->m_filter != NULL && m_inspector->m_filter_proc_table_when_saving)
{
if(!match)
{
pi->filtered_out = 1;
}
}
}
std::string sinsp_threadinfo::get_comm() const
{
return m_comm;
}
std::string sinsp_threadinfo::get_exe() const
{
return m_exe;
}
std::string sinsp_threadinfo::get_exepath() const
{
return m_exepath;
}
void sinsp_threadinfo::set_args(const char* args, size_t len)
{
m_args.clear();
size_t offset = 0;
while(offset < len)
{
m_args.push_back(args + offset);
offset += m_args.back().length() + 1;
}
}
void sinsp_threadinfo::set_env(const char* env, size_t len)
{
if (len == SCAP_MAX_ENV_SIZE && m_inspector->large_envs_enabled())
{
// the environment is possibly truncated, try to read from /proc
// this may fail for short-lived processes
if (set_env_from_proc())
{
g_logger.format(sinsp_logger::SEV_DEBUG, "Large environment for process %lu [%s], loaded from /proc", m_pid, m_comm.c_str());
return;
} else {
g_logger.format(sinsp_logger::SEV_INFO, "Failed to load environment for process %lu [%s] from /proc, using first %d bytes", m_pid, m_comm.c_str(), SCAP_MAX_ENV_SIZE);
}
}
m_env.clear();
size_t offset = 0;
while(offset < len)
{
const char* left = env + offset;
// environment string may actually be shorter than indicated by len
// if the rest is empty, we bail out early
if(!strlen(left))
{
size_t sz = len - offset;
void* zero = calloc(sz, sizeof(char));
if(!memcmp(left, zero, sz))
{
free(zero);
return;
}
free(zero);
}
m_env.push_back(left);
offset += m_env.back().length() + 1;
}
}
bool sinsp_threadinfo::set_env_from_proc() {
string environ_path = string(scap_get_host_root()) + "/proc/" + to_string(m_pid) + "/environ";
ifstream environment(environ_path);
if (!environment)
{
// failed to read the environment from /proc, work with what we have
return false;
}
m_env.clear();
while (environment) {
string env;
getline(environment, env, '\0');
if (!env.empty())
{
m_env.emplace_back(env);
}
}
return true;
}
const vector<string>& sinsp_threadinfo::get_env()
{
if(is_main_thread())
{
return m_env;
}
else
{
auto mtinfo = get_main_thread();
if(mtinfo != nullptr)
{
return mtinfo->get_env();
}
else
{
// it should never happen but provide a safe fallback just in case
// except during sinsp::scap_open() (see sinsp::get_thread()).
ASSERT(false);
return m_env;
}
}
}
// Return value string for the exact environment variable name given
string sinsp_threadinfo::get_env(const string& name)
{
size_t nlen = name.length();
for(const auto& env_var : get_env())
{
if((env_var.length() > (nlen + 1)) && (env_var[nlen] == '=') &&
!env_var.compare(0, nlen, name))
{
// Stripping spaces, not sure if we really should or need to
size_t first = env_var.find_first_not_of(' ', nlen + 1);
if (first == string::npos)
return "";
size_t last = env_var.find_last_not_of(' ');
return env_var.substr(first, last - first + 1);
}
}
return "";
}
void sinsp_threadinfo::set_cgroups(const char* cgroups, size_t len)
{
m_cgroups.clear();
size_t offset = 0;
while(offset < len)
{
const char* str = cgroups + offset;
const char* sep = strrchr(str, '=');
if(sep == NULL)
{
ASSERT(false);
return;
}
string subsys(str, sep - str);
string cgroup(sep + 1);
size_t subsys_length = subsys.length();
size_t pos = subsys.find("_cgroup");
if(pos != string::npos)
{
subsys.erase(pos, sizeof("_cgroup") - 1);
}
if(subsys == "perf")
{
subsys = "perf_event";
}
else if(subsys == "mem")
{
subsys = "memory";
}
else if(subsys == "io")
{
// blkio has been renamed just `io`
// in kernel space:
// https://github.com/torvalds/linux/commit/c165b3e3c7bb68c2ed55a5ac2623f030d01d9567
subsys = "blkio";
}
m_cgroups.push_back(std::make_pair(subsys, cgroup));
offset += subsys_length + 1 + cgroup.length() + 1;
}
}
sinsp_threadinfo* sinsp_threadinfo::get_parent_thread()
{
return m_inspector->get_thread_ref(m_ptid, false, true).get();
}
sinsp_fdinfo_t* sinsp_threadinfo::add_fd(int64_t fd, sinsp_fdinfo_t *fdinfo)
{
sinsp_fdinfo_t* res = get_fd_table()->add(fd, fdinfo);
//
// Update the last event fd. It's needed by the filtering engine
//
m_lastevent_fd = fd;
return res;
}
void sinsp_threadinfo::remove_fd(int64_t fd)
{
get_fd_table()->erase(fd);
}
bool sinsp_threadinfo::is_bound_to_port(uint16_t number)
{
unordered_map<int64_t, sinsp_fdinfo_t>::iterator it;
sinsp_fdtable* fdt = get_fd_table();
for(it = fdt->m_table.begin(); it != fdt->m_table.end(); ++it)
{
if(it->second.m_type == SCAP_FD_IPV4_SOCK)
{
if(it->second.m_sockinfo.m_ipv4info.m_fields.m_dport == number)
{
return true;
}
}
else if(it->second.m_type == SCAP_FD_IPV4_SERVSOCK)
{
if(it->second.m_sockinfo.m_ipv4serverinfo.m_port == number)
{
return true;
}
}
}
return false;
}
bool sinsp_threadinfo::uses_client_port(uint16_t number)
{
unordered_map<int64_t, sinsp_fdinfo_t>::iterator it;
sinsp_fdtable* fdt = get_fd_table();
for(it = fdt->m_table.begin();
it != fdt->m_table.end(); ++it)
{
if(it->second.m_type == SCAP_FD_IPV4_SOCK)
{
if(it->second.m_sockinfo.m_ipv4info.m_fields.m_sport == number)
{
return true;
}
}
}
return false;
}
bool sinsp_threadinfo::is_lastevent_data_valid()
{
return (m_lastevent_cpuid != (uint16_t) - 1);
}
sinsp_threadinfo* sinsp_threadinfo::get_cwd_root()
{
if(!(m_flags & PPM_CL_CLONE_FS))
{
return this;
}
else
{
return get_main_thread();
}
}
string sinsp_threadinfo::get_cwd()
{
// Ideally we should use get_cwd_root()
// but scap does not read CLONE_FS from /proc
// Also glibc and muslc use always
// CLONE_THREAD|CLONE_FS so let's use
// get_main_thread() for now
sinsp_threadinfo* tinfo = get_main_thread();
if(tinfo)
{
return tinfo->m_cwd;
}
else
{
ASSERT(false);
return "./";
}
}
void sinsp_threadinfo::set_cwd(const char* cwd, uint32_t cwdlen)
{
char tpath[SCAP_MAX_PATH_SIZE];
sinsp_threadinfo* tinfo = get_main_thread();
if(tinfo)
{
sinsp_utils::concatenate_paths(tpath,
SCAP_MAX_PATH_SIZE,
(char*)tinfo->m_cwd.c_str(),
(uint32_t)tinfo->m_cwd.size(),
cwd,
cwdlen,
m_inspector->m_is_windows);
tinfo->m_cwd = tpath;
uint32_t size = tinfo->m_cwd.size();
if(size == 0 || (tinfo->m_cwd[size - 1] != '/'))
{
tinfo->m_cwd += '/';
}
}
else
{
ASSERT(false);
}
}
void sinsp_threadinfo::allocate_private_state()
{
uint32_t j = 0;
if(m_inspector != NULL)
{
m_private_state.clear();
vector<uint32_t>* sizes = &m_inspector->m_thread_privatestate_manager.m_memory_sizes;
for(j = 0; j < sizes->size(); j++)
{
void* newbuf = malloc(sizes->at(j));
memset(newbuf, 0, sizes->at(j));
m_private_state.push_back(newbuf);
}
}
}
void* sinsp_threadinfo::get_private_state(uint32_t id)
{
if(id >= m_private_state.size())
{
ASSERT(false);
throw sinsp_exception("invalid thread state ID" + to_string((long long) id));
}
return m_private_state[id];
}
uint64_t sinsp_threadinfo::get_fd_usage_pct()
{
int64_t fdlimit = get_fd_limit();
if(fdlimit > 0)
{
uint64_t fd_opencount = get_fd_opencount();
ASSERT(fd_opencount <= (uint64_t) fdlimit);
if(fd_opencount <= (uint64_t) fdlimit)
{
return (fd_opencount * 100) / fdlimit;
}
else
{
return 100;
}
}
else
{
return 0;
}
}
double sinsp_threadinfo::get_fd_usage_pct_d()
{
int64_t fdlimit = get_fd_limit();
if(fdlimit > 0)
{
uint64_t fd_opencount = get_fd_opencount();
ASSERT(fd_opencount <= (uint64_t) fdlimit);
if(fd_opencount <= (uint64_t) fdlimit)
{
return ((double)fd_opencount * 100) / fdlimit;
}
else
{
return 100;
}
}
else
{
return 0;
}
}
uint64_t sinsp_threadinfo::get_fd_opencount() const
{
return get_main_thread()->m_fdtable.size();
}
uint64_t sinsp_threadinfo::get_fd_limit()
{
return get_main_thread()->m_fdlimit;
}
const std::string& sinsp_threadinfo::get_cgroup(const std::string& subsys) const
{
static const std::string notfound = "/";
for(const auto& it : m_cgroups)
{
if(it.first == subsys)
{
return it.second;
}
}
return notfound;
}
void sinsp_threadinfo::traverse_parent_state(visitor_func_t &visitor)
{
// Use two pointers starting at this, traversing the parent
// state, at different rates. If they ever equal each other
// before slow is NULL there's a loop.
sinsp_threadinfo *slow=this->get_parent_thread(), *fast=slow;
// Move fast to its parent
fast = (fast ? fast->get_parent_thread() : fast);
// The slow pointer must be valid and not have a tid of -1.
while(slow && slow->m_tid != -1)
{
if(!visitor(slow))
{
break;
}
// Advance slow one step and advance fast two steps
slow = slow->get_parent_thread();
// advance fast 2 steps, checking to see if we meet
// slow after each step.
for (uint32_t i = 0; i < 2; i++) {
fast = (fast ? fast->get_parent_thread() : fast);
// If not at the end but fast == slow or if
// slow points to itself, there's a loop in
// the thread state.
if(slow && (slow == fast ||
slow->m_tid == slow->m_ptid))
{
// Note we only log a loop once for a given main thread, to avoid flooding logs.
if(!m_parent_loop_detected)
{
g_logger.log(string("Loop in parent thread state detected for pid ") +
std::to_string(m_pid) +
". stopped at tid= " + std::to_string(slow->m_tid) +
" ptid=" + std::to_string(slow->m_ptid),
sinsp_logger::SEV_WARNING);
m_parent_loop_detected = true;
}
return;
}
}
}
}
void sinsp_threadinfo::populate_cmdline(string &cmdline, const sinsp_threadinfo *tinfo)
{
cmdline = tinfo->get_comm();
for (const auto& arg : tinfo->m_args)
{
cmdline += " ";
cmdline += arg;
}
}
bool sinsp_threadinfo::is_health_probe()
{
return (m_category == sinsp_threadinfo::CAT_HEALTHCHECK ||
m_category == sinsp_threadinfo::CAT_LIVENESS_PROBE ||
m_category == sinsp_threadinfo::CAT_READINESS_PROBE);
}
string sinsp_threadinfo::get_path_for_dir_fd(int64_t dir_fd)
{
sinsp_fdinfo_t* dir_fdinfo = get_fd(dir_fd);
if (!dir_fdinfo || dir_fdinfo->m_name.empty())
{
#ifndef WIN32 // we will have to implement this for Windows
#ifdef HAS_CAPTURE
// Sad day; we don't have the directory in the tinfo's fd cache.
// Must manually look it up so we can resolve filenames correctly.
char proc_path[PATH_MAX];
char dirfd_path[PATH_MAX];
int ret;
snprintf(proc_path,
sizeof(proc_path),
"%s/proc/%lld/fd/%lld",
scap_get_host_root(),
(long long)m_pid,
(long long)dir_fd);
ret = readlink(proc_path, dirfd_path, sizeof(dirfd_path) - 1);
if (ret < 0)
{
g_logger.log("Unable to determine path for file descriptor.",
sinsp_logger::SEV_INFO);
return "";
}
dirfd_path[ret] = '\0';
std::string rel_path_base = dirfd_path;
sanitize_string(rel_path_base);
rel_path_base.append("/");
g_logger.log(std::string("Translating to ") + rel_path_base);
return rel_path_base;
#else
g_logger.log("Can't translate working directory outside of live capture.",
sinsp_logger::SEV_INFO);
return "";
#endif
#endif // WIN32
}
return dir_fdinfo->m_name;
}
shared_ptr<sinsp_threadinfo> sinsp_threadinfo::lookup_thread() const
{
return m_inspector->get_thread_ref(m_pid, true, true, true);
}
size_t sinsp_threadinfo::args_len() const
{
return strvec_len(m_args);
}
size_t sinsp_threadinfo::env_len() const
{
return strvec_len(m_env);
}
size_t sinsp_threadinfo::cgroups_len() const
{
size_t totlen = 0;
for(auto &cgroup : m_cgroups)
{
totlen += cgroup.first.size() + 1 + cgroup.second.size();
totlen++; // Trailing NULL
}
return totlen;
}
void sinsp_threadinfo::args_to_iovec(struct iovec **iov, int *iovcnt,
std::string &rem) const
{
return strvec_to_iovec(m_args,
iov, iovcnt,
rem);
}
void sinsp_threadinfo::env_to_iovec(struct iovec **iov, int *iovcnt,
std::string &rem) const
{
return strvec_to_iovec(m_env,
iov, iovcnt,
rem);
}
// Set the provided iovec to the string in str, if it will fit. If it
// won't, copy the portion that will fit to rem and set the iovec to
// rem. Updates alen with the new total length and possibly sets rem
// to any truncated string.
void sinsp_threadinfo::add_to_iovec(const string &str,
const bool include_trailing_null,
struct iovec &iov,
uint32_t &alen,
std::string &rem) const
{
uint32_t len = str.size() + (include_trailing_null ? 1 : 0);
const char *buf = str.c_str();
if(len > alen)
{
// The entire string won't fit. Use rem to hold a
// truncated copy
rem = str.substr(0, alen-1);
buf = rem.c_str();
len = alen;
}
iov.iov_base = (void *) buf;
iov.iov_len = len;
alen -= len;
}
// iov will be allocated and must be freed. rem is used to hold a
// possibly truncated final argument.
void sinsp_threadinfo::cgroups_to_iovec(struct iovec **iov, int *iovcnt,
std::string &rem) const
{
uint32_t alen = SCAP_MAX_ARGS_SIZE;
static const string eq = "=";
// We allocate an iovec big enough to hold all the cgroups and
// intermediate '=' signs. Based on alen, we might not use all
// of the iovec.
*iov = (struct iovec *) malloc((3 * m_cgroups.size()) * sizeof(struct iovec));
*iovcnt = 0;
for(auto it = m_cgroups.begin(); it != m_cgroups.end() && alen > 0; ++it)
{
add_to_iovec(it->first, false, (*iov)[(*iovcnt)++], alen, rem);
if(alen > 0)
{
add_to_iovec(eq, false, (*iov)[(*iovcnt)++], alen, rem);
}
if(alen > 0)
{
add_to_iovec(it->second, true, (*iov)[(*iovcnt)++], alen, rem);
}
}
}
size_t sinsp_threadinfo::strvec_len(const vector<string> &strs) const
{
size_t totlen = 0;
for(auto &str : strs)
{
totlen += str.size();
totlen++; // Trailing NULL
}
return totlen;
}
// iov will be allocated and must be freed. rem is used to hold a
// possibly truncated final argument.
void sinsp_threadinfo::strvec_to_iovec(const vector<string> &strs,
struct iovec **iov, int *iovcnt,
std::string &rem) const
{
uint32_t alen = SCAP_MAX_ARGS_SIZE;
// We allocate an iovec big enough to hold all the entries in
// strs. Based on alen, we might not use all of the iovec.
*iov = (struct iovec *) malloc(strs.size() * sizeof(struct iovec));
*iovcnt = 0;
for(auto it = strs.begin(); it != strs.end() && alen > 0; ++it)
{
add_to_iovec(*it, true, (*iov)[(*iovcnt)++], alen, rem);
}
}
void sinsp_threadinfo::fd_to_scap(scap_fdinfo *dst, sinsp_fdinfo_t* src)
{
dst->type = src->m_type;
dst->ino = src->m_ino;
switch(dst->type)
{
case SCAP_FD_IPV4_SOCK:
dst->info.ipv4info.sip = src->m_sockinfo.m_ipv4info.m_fields.m_sip;
dst->info.ipv4info.dip = src->m_sockinfo.m_ipv4info.m_fields.m_dip;
dst->info.ipv4info.sport = src->m_sockinfo.m_ipv4info.m_fields.m_sport;
dst->info.ipv4info.dport = src->m_sockinfo.m_ipv4info.m_fields.m_dport;
dst->info.ipv4info.l4proto = src->m_sockinfo.m_ipv4info.m_fields.m_l4proto;
break;
case SCAP_FD_IPV4_SERVSOCK:
dst->info.ipv4serverinfo.ip = src->m_sockinfo.m_ipv4serverinfo.m_ip;
dst->info.ipv4serverinfo.port = src->m_sockinfo.m_ipv4serverinfo.m_port;
dst->info.ipv4serverinfo.l4proto = src->m_sockinfo.m_ipv4serverinfo.m_l4proto;
break;
case SCAP_FD_IPV6_SOCK:
copy_ipv6_address(dst->info.ipv6info.sip, src->m_sockinfo.m_ipv6info.m_fields.m_sip.m_b);
copy_ipv6_address(dst->info.ipv6info.dip, src->m_sockinfo.m_ipv6info.m_fields.m_dip.m_b);
dst->info.ipv6info.sport = src->m_sockinfo.m_ipv6info.m_fields.m_sport;
dst->info.ipv6info.dport = src->m_sockinfo.m_ipv6info.m_fields.m_dport;
dst->info.ipv6info.l4proto = src->m_sockinfo.m_ipv6info.m_fields.m_l4proto;
break;
case SCAP_FD_IPV6_SERVSOCK:
copy_ipv6_address(dst->info.ipv6serverinfo.ip, src->m_sockinfo.m_ipv6serverinfo.m_ip.m_b);
dst->info.ipv6serverinfo.port = src->m_sockinfo.m_ipv6serverinfo.m_port;
dst->info.ipv6serverinfo.l4proto = src->m_sockinfo.m_ipv6serverinfo.m_l4proto;
break;
case SCAP_FD_UNIX_SOCK:
dst->info.unix_socket_info.source = src->m_sockinfo.m_unixinfo.m_fields.m_source;
dst->info.unix_socket_info.destination = src->m_sockinfo.m_unixinfo.m_fields.m_dest;
strlcpy(dst->info.unix_socket_info.fname, src->m_name.c_str(), sizeof(dst->info.unix_socket_info.fname));
break;
case SCAP_FD_FILE_V2:
dst->info.regularinfo.open_flags = src->m_openflags;
strlcpy(dst->info.regularinfo.fname, src->m_name.c_str(), sizeof(dst->info.regularinfo.fname));
dst->info.regularinfo.dev = src->m_dev;
dst->info.regularinfo.mount_id = src->m_mount_id;
break;
case SCAP_FD_FIFO:
case SCAP_FD_FILE:
case SCAP_FD_DIRECTORY:
case SCAP_FD_UNSUPPORTED:
case SCAP_FD_SIGNALFD:
case SCAP_FD_EVENTPOLL:
case SCAP_FD_EVENT:
case SCAP_FD_INOTIFY:
case SCAP_FD_TIMERFD:
case SCAP_FD_NETLINK:
strlcpy(dst->info.fname, src->m_name.c_str(), sizeof(dst->info.fname));
break;
default:
ASSERT(false);
break;
}
}
///////////////////////////////////////////////////////////////////////////////
// sinsp_thread_manager implementation
///////////////////////////////////////////////////////////////////////////////
sinsp_thread_manager::sinsp_thread_manager(sinsp* inspector)
: m_max_thread_table_size(m_thread_table_absolute_max_size)
{
m_inspector = inspector;
clear();
}
void sinsp_thread_manager::clear()
{
m_threadtable.clear();
m_last_tid = 0;
m_last_tinfo.reset();
m_last_flush_time_ns = 0;
m_n_drops = 0;
#ifdef GATHER_INTERNAL_STATS
m_failed_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_failed_lookups","Failed thread lookups"));
m_cached_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_cached_lookups","Cached thread lookups"));
m_non_cached_lookups = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_non_cached_lookups","Non cached thread lookups"));
m_added_threads = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_added","Number of added threads"));
m_removed_threads = &m_inspector->m_stats.get_metrics_registry().register_counter(internal_metrics::metric_name("thread_removed","Removed threads"));
#endif
}
void sinsp_thread_manager::increment_mainthread_childcount(sinsp_threadinfo* threadinfo)
{
if(threadinfo->m_flags & PPM_CL_CLONE_THREAD)
{
//
// Increment the refcount of the main thread so it won't
// be deleted (if it calls pthread_exit()) until we are done
//
ASSERT(threadinfo->m_pid != threadinfo->m_tid);
sinsp_threadinfo* main_thread = m_inspector->get_thread_ref(threadinfo->m_pid, true, true).get();
if(main_thread)
{
++main_thread->m_nchilds;
}
else
{
ASSERT(false);
}
}
}
bool sinsp_thread_manager::add_thread(sinsp_threadinfo *threadinfo, bool from_scap_proctable)
{
#ifdef GATHER_INTERNAL_STATS
m_added_threads->increment();
#endif
m_last_tinfo.reset();
if(m_threadtable.size() >= m_max_thread_table_size
#if defined(HAS_CAPTURE)
&& threadinfo->m_pid != m_inspector->m_self_pid
#endif
)
{
// rate limit messages to avoid spamming the logs
if (m_n_drops % m_max_thread_table_size == 0)
{
g_logger.format(sinsp_logger::SEV_INFO, "Thread table full, dropping tid %lu (pid %lu, comm \"%s\")",
threadinfo->m_tid, threadinfo->m_pid, threadinfo->m_comm.c_str());
}
m_n_drops++;
return false;
}
if(!from_scap_proctable)
{
increment_mainthread_childcount(threadinfo);
}
threadinfo->compute_program_hash();
threadinfo->allocate_private_state();
m_threadtable.put(threadinfo);
return true;
}
void sinsp_thread_manager::remove_thread(int64_t tid, bool force)
{
uint64_t nchilds;
sinsp_threadinfo* tinfo = m_threadtable.get(tid);
if(tinfo == nullptr)
{
//
// Looks like there's no thread to remove.
// Either the thread creation event was dropped or our logic doesn't support the
// call that created this thread. The assertion will detect it, while in release mode we just
// keep going.
//
#ifdef GATHER_INTERNAL_STATS
m_failed_lookups->increment();
#endif
return;
}
else if((nchilds = tinfo->m_nchilds) == 0 || force)
{
//
// Decrement the refcount of the main thread/program because
// this reference is gone
//
if(tinfo->m_flags & PPM_CL_CLONE_THREAD)
{
ASSERT(tinfo->m_pid != tinfo->m_tid);
sinsp_threadinfo* main_thread = m_inspector->get_thread_ref(tinfo->m_pid, false, true).get();
if(main_thread)
{
if(main_thread->m_nchilds > 0)
{
--main_thread->m_nchilds;
}
else
{
ASSERT(false);
}
}
else
{
ASSERT(false);
}
}
//
// If this is the main thread of a process, erase all the FDs that the process owns
//
if((tinfo->m_pid == tinfo->m_tid) || tinfo->m_flags & PPM_CL_IS_MAIN_THREAD)
{
unordered_map<int64_t, sinsp_fdinfo_t>* fdtable = &(tinfo->get_fd_table()->m_table);
unordered_map<int64_t, sinsp_fdinfo_t>::iterator fdit;
erase_fd_params eparams;
eparams.m_remove_from_table = false;
eparams.m_tinfo = tinfo;
eparams.m_ts = m_inspector->m_lastevent_ts;
for(fdit = fdtable->begin(); fdit != fdtable->end(); ++fdit)
{
eparams.m_fd = fdit->first;
//
// The canceled fd should always be deleted immediately, so if it appears
// here it means we have a problem.
//
ASSERT(eparams.m_fd != CANCELED_FD_NUMBER);
eparams.m_fdinfo = &(fdit->second);
m_inspector->m_parser->erase_fd(&eparams);
}
}
//
// Reset the cache
//
m_last_tid = 0;
m_last_tinfo.reset();
#ifdef GATHER_INTERNAL_STATS
m_removed_threads->increment();
#endif
m_threadtable.erase(tid);
//
// If the thread has a nonzero refcount, it means that we are forcing the removal
// of a main process or program that some child refer to.
// We need to recalculate the child relationships, or the table will become
// corrupted.
//
if(nchilds != 0)
{
recreate_child_dependencies();
}
}
}
void sinsp_thread_manager::fix_sockets_coming_from_proc()
{
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
tinfo.fix_sockets_coming_from_proc();
return true;
});
}
void sinsp_thread_manager::clear_thread_pointers(sinsp_threadinfo& tinfo)
{
tinfo.m_main_thread.reset();
sinsp_fdtable* fdt = tinfo.get_fd_table();
if(fdt != NULL)
{
fdt->reset_cache();
}
}
/*
void sinsp_thread_manager::clear_thread_pointers(threadinfo_map_iterator_t it)
{
it->second.m_main_program_thread = NULL;
it->second.m_main_thread = NULL;
it->second.m_progid = -1LL;
it->second.m_fdtable.reset_cache();
}
*/
void sinsp_thread_manager::reset_child_dependencies()
{
m_last_tinfo.reset();
m_last_tid = 0;
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
tinfo.m_nchilds = 0;
clear_thread_pointers(tinfo);
return true;
});
}
void sinsp_thread_manager::create_child_dependencies()
{
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
increment_mainthread_childcount(&tinfo);
return true;
});
}
void sinsp_thread_manager::recreate_child_dependencies()
{
reset_child_dependencies();
create_child_dependencies();
}
void sinsp_thread_manager::update_statistics()
{
#ifdef GATHER_INTERNAL_STATS
m_inspector->m_stats.m_n_threads = get_thread_count();
m_inspector->m_stats.m_n_fds = 0;
for(threadinfo_map_iterator_t it = m_threadtable.begin(); it != m_threadtable.end(); it++)
{
m_inspector->m_stats.m_n_fds += it->second.get_fd_table()->size();
}
#endif
}
void sinsp_thread_manager::free_dump_fdinfos(vector<scap_fdinfo*>* fdinfos_to_free)
{
for(uint32_t j = 0; j < fdinfos_to_free->size(); j++)
{
free(fdinfos_to_free->at(j));
}
fdinfos_to_free->clear();
}
// NOTE: This does *not* populate any array-based fields (comm, exe,
// exepath, args, env, cwd, cgroups, root)
void sinsp_thread_manager::thread_to_scap(sinsp_threadinfo& tinfo, scap_threadinfo* sctinfo)
{
//
// Fill in the thread data
//
// NOTE: This is doing a shallow copy of the strings from
// tinfo, and is valid only as long as tinfo is valid.
sctinfo->tid = tinfo.m_tid;
sctinfo->pid = tinfo.m_pid;
sctinfo->ptid = tinfo.m_ptid;
sctinfo->sid = tinfo.m_sid;
sctinfo->vpgid = tinfo.m_vpgid;
sctinfo->flags = tinfo.m_flags ;
sctinfo->fdlimit = tinfo.m_fdlimit;
sctinfo->uid = tinfo.m_uid;
sctinfo->gid = tinfo.m_gid;
sctinfo->vmsize_kb = tinfo.m_vmsize_kb;
sctinfo->vmrss_kb = tinfo.m_vmrss_kb;
sctinfo->vmswap_kb = tinfo.m_vmswap_kb;
sctinfo->pfmajor = tinfo.m_pfmajor;
sctinfo->pfminor = tinfo.m_pfminor;
sctinfo->vtid = tinfo.m_vtid;
sctinfo->vpid = tinfo.m_vpid;
sctinfo->fdlist = NULL;
sctinfo->loginuid = tinfo.m_loginuid;
sctinfo->filtered_out = false;
}
void sinsp_thread_manager::dump_threads_to_file(scap_dumper_t* dumper)
{
//
// First pass of the table to calculate the lengths
//
uint32_t totlen = 0;
vector<uint32_t> lengths;
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
uint32_t il = (uint32_t)
(sizeof(uint32_t) + // len
sizeof(uint64_t) + // tid
sizeof(uint64_t) + // pid
sizeof(uint64_t) + // ptid
sizeof(uint64_t) + // sid
sizeof(uint64_t) + // pgid
2 + MIN(tinfo.m_comm.size(), SCAP_MAX_PATH_SIZE) +
2 + MIN(tinfo.m_exe.size(), SCAP_MAX_PATH_SIZE) +
2 + MIN(tinfo.m_exepath.size(), SCAP_MAX_PATH_SIZE) +
2 + MIN(tinfo.args_len(), SCAP_MAX_ARGS_SIZE) +
// 1 is sizeof("/")
2 + MIN((tinfo.m_cwd == "")? 1 : tinfo.m_cwd.size(), SCAP_MAX_PATH_SIZE) +
sizeof(uint64_t) + // fdlimit
sizeof(uint32_t) + // flags
sizeof(uint32_t) + // uid
sizeof(uint32_t) + // gid
sizeof(uint32_t) + // vmsize_kb
sizeof(uint32_t) + // vmrss_kb
sizeof(uint32_t) + // vmswap_kb
sizeof(uint64_t) + // pfmajor
sizeof(uint64_t) + // pfminor
2 + MIN(tinfo.env_len(), SCAP_MAX_ENV_SIZE) +
sizeof(int64_t) + // vtid
sizeof(int64_t) + // vpid
2 + MIN(tinfo.cgroups_len(), SCAP_MAX_CGROUPS_SIZE) +
2 + MIN(tinfo.m_root.size(), SCAP_MAX_PATH_SIZE)) +
sizeof(int32_t) + // loginuid;
sizeof(uint8_t); // exe_writable
lengths.push_back(il);
totlen += il;
return true;
});
//
// Second pass of the table to dump the Threads
//
if(scap_write_proclist_header(m_inspector->m_h, dumper, totlen) != SCAP_SUCCESS)
{
throw sinsp_exception(scap_getlasterr(m_inspector->m_h));
}
uint32_t idx = 0;
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
scap_threadinfo *sctinfo;
struct iovec *args_iov, *envs_iov, *cgroups_iov;
int argscnt, envscnt, cgroupscnt;
string argsrem, envsrem, cgroupsrem;
if((sctinfo = scap_proc_alloc(m_inspector->m_h)) == NULL)
{
throw sinsp_exception(scap_getlasterr(m_inspector->m_h));
}
thread_to_scap(tinfo, sctinfo);
tinfo.args_to_iovec(&args_iov, &argscnt, argsrem);
tinfo.env_to_iovec(&envs_iov, &envscnt, envsrem);
tinfo.cgroups_to_iovec(&cgroups_iov, &cgroupscnt, cgroupsrem);
if(scap_write_proclist_entry_bufs(m_inspector->m_h, dumper, sctinfo, lengths[idx++],
tinfo.m_comm.c_str(),
tinfo.m_exe.c_str(),
tinfo.m_exepath.c_str(),
args_iov, argscnt,
envs_iov, envscnt,
(tinfo.m_cwd == "" ? "/" : tinfo.m_cwd.c_str()),
cgroups_iov, cgroupscnt,
tinfo.m_root.c_str()) != SCAP_SUCCESS)
{
throw sinsp_exception(scap_getlasterr(m_inspector->m_h));
}
free(args_iov);
free(envs_iov);
free(cgroups_iov);
scap_proc_free(m_inspector->m_h, sctinfo);
return true;
});
if(scap_write_proclist_trailer(m_inspector->m_h, dumper, totlen) != SCAP_SUCCESS)
{
throw sinsp_exception(scap_getlasterr(m_inspector->m_h));
}
//
// Third pass of the table to dump the FDs
//
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
scap_threadinfo *sctinfo;
if((sctinfo = scap_proc_alloc(m_inspector->m_h)) == NULL)
{
throw sinsp_exception(scap_getlasterr(m_inspector->m_h));
}
// Note: as scap_fd_add/scap_write_proc_fds do not use
// any of the array-based fields like comm, etc. a
// shallow copy is safe
thread_to_scap(tinfo, sctinfo);
if(tinfo.is_main_thread())
{
//
// Add the FDs
//
unordered_map<int64_t, sinsp_fdinfo_t>& fdtable = tinfo.get_fd_table()->m_table;
for(auto it = fdtable.begin(); it != fdtable.end(); ++it)
{
//
// Allocate the scap fd info
//
scap_fdinfo* scfdinfo = (scap_fdinfo*)malloc(sizeof(scap_fdinfo));
if(scfdinfo == NULL)
{
scap_proc_free(m_inspector->m_h, sctinfo);
throw sinsp_exception("thread memory allocation error in sinsp_thread_manager::to_scap");
}
//
// Populate the fd info
//
scfdinfo->fd = it->first;
tinfo.fd_to_scap(scfdinfo, &it->second);
//
// Add the new fd to the scap table.
//
if(scap_fd_add(m_inspector->m_h, sctinfo, it->first, scfdinfo) != SCAP_SUCCESS)
{
scap_proc_free(m_inspector->m_h, sctinfo);
throw sinsp_exception("error calling scap_fd_add in sinsp_thread_manager::to_scap (" + string(scap_getlasterr(m_inspector->m_h)) + ")");
}
}
}
//
// Dump the thread to disk
//
if(scap_write_proc_fds(m_inspector->m_h, sctinfo, dumper) != SCAP_SUCCESS)
{
scap_proc_free(m_inspector->m_h, sctinfo);
throw sinsp_exception("error calling scap_proc_add in sinsp_thread_manager::to_scap (" + string(scap_getlasterr(m_inspector->m_h)) + ")");
}
scap_proc_free(m_inspector->m_h, sctinfo);
return true;
});
}
threadinfo_map_t::ptr_t sinsp_thread_manager::get_thread_ref(int64_t tid, bool query_os_if_not_found, bool lookup_only, bool main_thread)
{
auto sinsp_proc = find_thread(tid, lookup_only);
if(!sinsp_proc && query_os_if_not_found &&
(m_threadtable.size() < m_max_thread_table_size
#if defined(HAS_CAPTURE)
|| tid == m_inspector->m_self_pid
#endif
))
{
// Certain code paths can lead to this point from scap_open() (incomplete example:
// scap_proc_scan_proc_dir() -> resolve_container() -> get_env()). Adding a
// defensive check here to protect both, callers of get_env and get_thread.
if (!m_inspector->m_h)
{
g_logger.format(sinsp_logger::SEV_INFO, "%s: Unable to complete for tid=%"
PRIu64 ": sinsp::scap_t* is uninitialized", __func__, tid);
return NULL;
}
scap_threadinfo* scap_proc = NULL;
// unfortunately, sinsp owns the threade factory
sinsp_threadinfo* newti = m_inspector->build_threadinfo();
m_n_proc_lookups++;
if(main_thread)
{
m_n_main_thread_lookups++;
}
if(m_n_proc_lookups == m_max_n_proc_lookups)
{
g_logger.format(sinsp_logger::SEV_INFO, "Reached max process lookup number, duration=%" PRIu64 "ms",
m_n_proc_lookups_duration_ns / 1000000);
}
if(m_max_n_proc_lookups < 0 ||
m_n_proc_lookups <= m_max_n_proc_lookups)
{
#ifdef HAS_ANALYZER
tracer_emitter("sinsp_proc_lookup");
#endif
bool scan_sockets = false;
if(m_max_n_proc_socket_lookups < 0 ||
m_n_proc_lookups <= m_max_n_proc_socket_lookups)
{
scan_sockets = true;
if(m_n_proc_lookups == m_max_n_proc_socket_lookups)
{
g_logger.format(sinsp_logger::SEV_INFO, "Reached max socket lookup number, tid=%" PRIu64 ", duration=%" PRIu64 "ms",
tid, m_n_proc_lookups_duration_ns / 1000000);
}
}
#ifdef HAS_ANALYZER
uint64_t ts = sinsp_utils::get_current_time_ns();
#endif
scap_proc = scap_proc_get(m_inspector->m_h, tid, scan_sockets);
#ifdef HAS_ANALYZER
m_n_proc_lookups_duration_ns += sinsp_utils::get_current_time_ns() - ts;
#endif
}
if(scap_proc)
{
newti->init(scap_proc);
scap_proc_free(m_inspector->m_h, scap_proc);
}
else
{
//
// Add a fake entry to avoid a continuous lookup
//
newti->m_tid = tid;
newti->m_pid = tid;
newti->m_ptid = -1;
newti->m_comm = "<NA>";
newti->m_exe = "<NA>";
newti->m_uid = 0xffffffff;
newti->m_gid = 0xffffffff;
newti->m_nchilds = 0;
newti->m_loginuid = 0xffffffff;
}
//
// Since this thread is created out of thin air, we need to
// properly set its reference count, by scanning the table
//
m_threadtable.loop([&] (sinsp_threadinfo& tinfo) {
if(tinfo.m_pid == tid)
{
newti->m_nchilds++;
}
return true;
});
//
// Done. Add the new thread to the list.
//
add_thread(newti, false);
sinsp_proc = find_thread(tid, lookup_only);
}
return sinsp_proc;
}
threadinfo_map_t::ptr_t sinsp_thread_manager::find_thread(int64_t tid, bool lookup_only)
{
threadinfo_map_t::ptr_t thr;
//
// Try looking up in our simple cache
//
if(tid == m_last_tid)
{
thr = m_last_tinfo.lock();
if (thr) {
#ifdef GATHER_INTERNAL_STATS
m_cached_lookups->increment();
#endif
// This allows us to avoid performing an actual timestamp lookup
// for something that may not need to be precise
thr->m_lastaccess_ts = m_inspector->get_lastevent_ts();
return thr;
} }
//
// Caching failed, do a real lookup
//
thr = m_threadtable.get_ref(tid);
if(thr)
{
#ifdef GATHER_INTERNAL_STATS
m_non_cached_lookups->increment();
#endif
if(!lookup_only)
{
m_last_tid = tid;
m_last_tinfo = thr;
thr->m_lastaccess_ts = m_inspector->get_lastevent_ts();
}
return thr;
}
else
{
#ifdef GATHER_INTERNAL_STATS
m_failed_lookups->increment();
#endif
return NULL;
}
}
void sinsp_thread_manager::set_max_thread_table_size(uint32_t value)
{
m_max_thread_table_size = std::min(value, m_thread_table_absolute_max_size);
}
|