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
|
// Copyright (c) Meta Platforms, Inc. and affiliates.
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <assert.h>
#include <byteswap.h>
#include <dirent.h>
#include <elf.h>
#include <elfutils/libdw.h>
#include <errno.h>
#include <fcntl.h>
#include <gelf.h>
#include <inttypes.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/statfs.h>
#include <sys/types.h>
#include <unistd.h>
#include "cleanup.h"
#include "debug_info.h"
#include "elf_notes.h"
#include "error.h"
#include "helpers.h"
#include "io.h"
#include "language.h"
#include "log.h"
#include "linux_kernel.h"
#include "log.h"
#include "memory_reader.h"
#include "minmax.h"
#include "object.h"
#include "plugins.h"
#include "program.h"
#include "serialize.h"
#include "symbol.h"
#include "util.h"
#include "vector.h"
static inline uint32_t drgn_thread_to_key(const struct drgn_thread *entry)
{
return entry->tid;
}
DEFINE_HASH_TABLE_FUNCTIONS(drgn_thread_set, drgn_thread_to_key,
int_key_hash_pair, scalar_key_eq);
struct drgn_thread_iterator {
struct drgn_program *prog;
union {
/* For userspace core dumps. */
struct drgn_thread_set_iterator iterator;
struct {
union {
/* For live processes. */
DIR *tasks_dir;
/* For the Linux kernel. */
struct linux_helper_task_iterator task_iter;
};
/* For both live processes and the Linux kernel. */
struct drgn_thread entry;
};
};
};
LIBDRGN_PUBLIC enum drgn_program_flags
drgn_program_flags(struct drgn_program *prog)
{
return prog->flags;
}
LIBDRGN_PUBLIC const struct drgn_platform *
drgn_program_platform(struct drgn_program *prog)
{
return prog->has_platform ? &prog->platform : NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_address_size(struct drgn_program *prog, uint64_t *ret)
{
if (!prog->has_platform) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"program address size is not known");
}
*ret = drgn_platform_address_size(&prog->platform);
return NULL;
}
LIBDRGN_PUBLIC
const char *drgn_program_core_dump_path(struct drgn_program *prog)
{
return prog->core_path;
}
LIBDRGN_PUBLIC const struct drgn_language *
drgn_program_language(struct drgn_program *prog)
{
if (prog->lang)
return prog->lang;
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
prog->lang = &drgn_language_c;
return prog->lang;
}
if (!prog->tried_main_language) {
prog->tried_main_language = true;
prog->lang = drgn_debug_info_main_language(&prog->dbinfo);
if (prog->lang) {
drgn_log_debug(prog,
"set default language to %s from main()",
prog->lang->name);
return prog->lang;
} else {
drgn_log_debug(prog,
"couldn't find language of main(); defaulting to %s",
drgn_default_language.name);
}
}
return &drgn_default_language;
}
LIBDRGN_PUBLIC void drgn_program_set_language(struct drgn_program *prog,
const struct drgn_language *lang)
{
prog->lang = lang;
}
void drgn_program_set_platform(struct drgn_program *prog,
const struct drgn_platform *platform)
{
if (!prog->has_platform) {
prog->platform = *platform;
prog->has_platform = true;
}
}
void drgn_program_init(struct drgn_program *prog,
const struct drgn_platform *platform)
{
memset(prog, 0, sizeof(*prog));
drgn_memory_reader_init(&prog->reader);
drgn_program_init_types(prog);
drgn_debug_info_init(&prog->dbinfo, prog);
prog->core_fd = -1;
if (platform)
drgn_program_set_platform(prog, platform);
drgn_thread_set_init(&prog->thread_set);
drgn_program_set_log_level(prog, DRGN_LOG_NONE);
drgn_program_set_log_file(prog, stderr);
prog->default_progress_file = true;
drgn_object_init(&prog->vmemmap, prog);
}
void drgn_program_deinit(struct drgn_program *prog)
{
drgn_thread_set_deinit(&prog->thread_set);
if (drgn_program_is_userspace_core(prog)) {
free(prog->core_dump_fname_cached);
} else {
// For userspace core dumps, main_thread and crashed_thread are
// in prog->thread_set and thus freed by the above call to
// drgn_thread_set_deinit().
drgn_thread_destroy(prog->crashed_thread);
drgn_thread_destroy(prog->main_thread);
}
if (prog->pgtable_it)
prog->platform.arch->linux_kernel_pgtable_iterator_destroy(prog->pgtable_it);
drgn_object_deinit(&prog->vmemmap);
drgn_handler_list_deinit(struct drgn_symbol_finder, finder,
&prog->symbol_finders,
if (finder->ops.destroy)
finder->ops.destroy(finder->arg);
);
drgn_handler_list_deinit(struct drgn_object_finder, finder,
&prog->object_finders,
if (finder->ops.destroy)
finder->ops.destroy(finder->arg);
);
drgn_program_deinit_types(prog);
drgn_memory_reader_deinit(&prog->reader);
free(prog->file_segments);
free(prog->vmcoreinfo.raw);
free(prog->irq_regs_cached);
#ifdef WITH_LIBKDUMPFILE
if (prog->kdump_ctx)
kdump_free(prog->kdump_ctx);
#endif
free(prog->core_path);
elf_end(prog->core);
if (prog->core_fd != -1)
close(prog->core_fd);
drgn_debug_info_deinit(&prog->dbinfo);
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_add_memory_segment(struct drgn_program *prog, uint64_t address,
uint64_t size, drgn_memory_read_fn read_fn,
void *arg, bool physical)
{
uint64_t address_mask;
struct drgn_error *err = drgn_program_address_mask(prog, &address_mask);
if (err)
return err;
if (size == 0 || address > address_mask)
return NULL;
uint64_t max_address = address + min(size - 1, address_mask - address);
return drgn_memory_reader_add_segment(&prog->reader, address,
max_address, read_fn, arg,
physical);
}
#define DRGN_PROGRAM_FINDER(which) \
struct drgn_error * \
drgn_program_register_##which##_finder_impl(struct drgn_program *prog, \
struct drgn_##which##_finder *finder,\
const char *name, \
const struct drgn_##which##_finder_ops *ops,\
void *arg, size_t enable_index) \
{ \
struct drgn_error *err; \
if (finder) { \
finder->handler.name = name; \
finder->handler.free = false; \
} else { \
finder = malloc(sizeof(*finder)); \
if (!finder) \
return &drgn_enomem; \
finder->handler.name = strdup(name); \
if (!finder->handler.name) { \
free(finder); \
return &drgn_enomem; \
} \
finder->handler.free = true; \
} \
memcpy(&finder->ops, ops, sizeof(finder->ops)); \
finder->arg = arg; \
err = drgn_handler_list_register(&prog->which##_finders, \
&finder->handler, enable_index, \
#which " finder"); \
if (err && finder->handler.free) { \
free((char *)finder->handler.name); \
free(finder); \
} \
return err; \
} \
\
LIBDRGN_PUBLIC struct drgn_error * \
drgn_program_register_##which##_finder(struct drgn_program *prog, const char *name,\
const struct drgn_##which##_finder_ops *ops,\
void *arg, size_t enable_index) \
{ \
return drgn_program_register_##which##_finder_impl(prog, NULL, name, \
ops, arg, \
enable_index); \
} \
\
LIBDRGN_PUBLIC struct drgn_error * \
drgn_program_registered_##which##_finders(struct drgn_program *prog, \
const char ***names_ret, \
size_t *count_ret) \
{ \
return drgn_handler_list_registered(&prog->which##_finders, names_ret, \
count_ret); \
} \
\
LIBDRGN_PUBLIC struct drgn_error * \
drgn_program_set_enabled_##which##_finders(struct drgn_program *prog, \
const char * const *names, \
size_t count) \
{ \
return drgn_handler_list_set_enabled(&prog->which##_finders, names, \
count, #which "finder"); \
} \
\
LIBDRGN_PUBLIC struct drgn_error * \
drgn_program_enabled_##which##_finders(struct drgn_program *prog, \
const char ***names_ret, \
size_t *count_ret) \
{ \
return drgn_handler_list_enabled(&prog->which##_finders, names_ret, \
count_ret); \
}
DRGN_PROGRAM_FINDER(type)
DRGN_PROGRAM_FINDER(object)
DRGN_PROGRAM_FINDER(symbol)
#undef DRGN_PROGRAM_FINDER
static struct drgn_error *
drgn_program_check_initialized(struct drgn_program *prog)
{
if (prog->core_fd != -1 || !drgn_memory_reader_empty(&prog->reader)) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"program memory was already initialized");
}
return NULL;
}
static struct drgn_error *
has_kdump_signature(struct drgn_program *prog, const char *path, bool *ret)
{
char signature[max_iconst(KDUMP_SIG_LEN, FLATTENED_SIG_LEN)];
ssize_t r = pread_all(prog->core_fd, signature, sizeof(signature), 0);
if (r < 0)
return drgn_error_create_os("pread", errno, path);
*ret = false;
if (r >= FLATTENED_SIG_LEN
&& memcmp(signature, FLATTENED_SIGNATURE, FLATTENED_SIG_LEN) == 0) {
drgn_log_warning(prog,
"the given file is in the makedumpfile flattened "
"format; if open fails or is too slow, reassemble "
"it with 'makedumpfile -R newfile <oldfile'");
*ret = true;
} else if (r >= KDUMP_SIG_LEN
&& memcmp(signature, KDUMP_SIGNATURE, KDUMP_SIG_LEN) == 0)
*ret = true;
return NULL;
}
static struct drgn_error *
drgn_program_set_core_dump_fd_internal(struct drgn_program *prog, int fd,
const char *path)
{
struct drgn_error *err;
GElf_Ehdr ehdr_mem, *ehdr;
bool had_platform;
bool is_64_bit, little_endian, is_kdump;
size_t phnum, i;
size_t num_file_segments, j;
bool have_phys_addrs = false;
bool have_qemu_note = false;
const char *vmcoreinfo_note = NULL;
size_t vmcoreinfo_size = 0;
bool have_nt_taskstruct = false, is_proc_kcore;
bool have_vmcoreinfo = prog->vmcoreinfo.raw;
bool had_vmcoreinfo = have_vmcoreinfo;
prog->core_fd = fd;
prog->core_path = fd_canonical_path(fd, path);
if (!prog->core_path) {
err = &drgn_enomem;
goto out_fd;
}
err = has_kdump_signature(prog, prog->core_path, &is_kdump);
if (err)
goto out_path;
if (is_kdump) {
err = drgn_program_set_kdump(prog);
if (err)
goto out_path;
return NULL;
}
elf_version(EV_CURRENT);
prog->core = elf_begin(prog->core_fd, ELF_C_READ, NULL);
if (!prog->core) {
err = drgn_error_libelf();
goto out_path;
}
ehdr = gelf_getehdr(prog->core, &ehdr_mem);
if (!ehdr || ehdr->e_type != ET_CORE) {
err = drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
"not an ELF core file");
goto out_elf;
}
had_platform = prog->has_platform;
if (!had_platform) {
struct drgn_platform platform;
drgn_platform_from_elf(ehdr, &platform);
drgn_program_set_platform(prog, &platform);
}
is_64_bit = ehdr->e_ident[EI_CLASS] == ELFCLASS64;
little_endian = ehdr->e_ident[EI_DATA] == ELFDATA2LSB;
if (elf_getphdrnum(prog->core, &phnum) != 0) {
err = drgn_error_libelf();
goto out_platform;
}
/*
* First pass: count the number of loadable segments, check if p_paddr
* is valid, and check for notes.
*/
num_file_segments = 0;
for (i = 0; i < phnum; i++) {
GElf_Phdr phdr_mem, *phdr;
phdr = gelf_getphdr(prog->core, i, &phdr_mem);
if (!phdr) {
err = drgn_error_libelf();
goto out_notes;
}
if (phdr->p_type == PT_LOAD) {
if (phdr->p_paddr)
have_phys_addrs = true;
num_file_segments++;
} else if (phdr->p_type == PT_NOTE) {
Elf_Data *data;
size_t offset;
GElf_Nhdr nhdr;
size_t name_offset, desc_offset;
data = elf_getdata_rawchunk(prog->core, phdr->p_offset,
phdr->p_filesz,
note_header_type(phdr->p_align));
if (!data) {
err = drgn_error_libelf();
goto out_notes;
}
offset = 0;
while (offset < data->d_size &&
(offset = gelf_getnote(data, offset, &nhdr,
&name_offset,
&desc_offset))) {
const char *name, *desc;
name = (char *)data->d_buf + name_offset;
desc = (char *)data->d_buf + desc_offset;
if (nhdr.n_namesz == sizeof("CORE") &&
memcmp(name, "CORE", sizeof("CORE")) == 0) {
if (nhdr.n_type == NT_TASKSTRUCT)
have_nt_taskstruct = true;
} else if (nhdr.n_namesz == sizeof("LINUX") &&
memcmp(name, "LINUX",
sizeof("LINUX")) == 0) {
if (nhdr.n_type == NT_ARM_PAC_MASK &&
nhdr.n_descsz >=
2 * sizeof(uint64_t)) {
memcpy(&prog->aarch64_insn_pac_mask,
(uint64_t *)desc + 1,
sizeof(uint64_t));
if (little_endian !=
HOST_LITTLE_ENDIAN)
bswap_64(prog->aarch64_insn_pac_mask);
}
} else if (nhdr.n_namesz == sizeof("VMCOREINFO") &&
memcmp(name, "VMCOREINFO",
sizeof("VMCOREINFO")) == 0) {
vmcoreinfo_note = desc;
vmcoreinfo_size = nhdr.n_descsz;
/*
* This is either a vmcore or
* /proc/kcore, so even a p_paddr of 0
* may be valid.
*/
have_phys_addrs = true;
have_vmcoreinfo = true;
} else if (nhdr.n_namesz == sizeof("QEMU") &&
memcmp(name, "QEMU",
sizeof("QEMU")) == 0) {
have_qemu_note = true;
}
}
}
}
if (have_nt_taskstruct) {
/*
* If the core file has an NT_TASKSTRUCT note and is in /proc,
* then it's probably /proc/kcore.
*/
struct statfs fs;
if (fstatfs(prog->core_fd, &fs) == -1) {
err = drgn_error_create_os("fstatfs", errno,
prog->core_path);
if (err)
goto out_notes;
}
is_proc_kcore = fs.f_type == 0x9fa0; /* PROC_SUPER_MAGIC */
} else {
is_proc_kcore = false;
}
if (have_vmcoreinfo && !is_proc_kcore) {
char *env;
/* Use libkdumpfile for ELF vmcores if it was requested. */
env = getenv("DRGN_USE_LIBKDUMPFILE_FOR_ELF");
if (env && atoi(env)) {
err = drgn_program_set_kdump(prog);
if (err)
goto out_notes;
return NULL;
}
}
prog->file_segments = malloc_array(num_file_segments,
sizeof(*prog->file_segments));
if (!prog->file_segments) {
err = &drgn_enomem;
goto out_notes;
}
bool pgtable_reader =
(is_proc_kcore || have_vmcoreinfo) &&
prog->platform.arch->linux_kernel_pgtable_iterator_next;
if (pgtable_reader) {
/*
* Try to read any memory that isn't in the core dump via the
* page table.
*/
err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX,
read_memory_via_pgtable,
prog, false);
if (err)
goto out_segments;
}
/* Second pass: add the segments. */
for (i = 0, j = 0; i < phnum && j < num_file_segments; i++) {
GElf_Phdr phdr_mem, *phdr;
phdr = gelf_getphdr(prog->core, i, &phdr_mem);
if (!phdr) {
err = drgn_error_libelf();
goto out_segments;
}
if (phdr->p_type != PT_LOAD)
continue;
prog->file_segments[j].file_offset = phdr->p_offset;
prog->file_segments[j].file_size = phdr->p_filesz;
prog->file_segments[j].fd = prog->core_fd;
prog->file_segments[j].eio_is_fault = false;
/*
* p_filesz < p_memsz is ambiguous for core dumps. The ELF
* specification says that "if the segment's memory size p_memsz
* is larger than the file size p_filesz, the 'extra' bytes are
* defined to hold the value 0 and to follow the segment's
* initialized area."
*
* However, the Linux kernel generates userspace core dumps with
* segments with p_filesz < p_memsz to indicate that the range
* between p_filesz and p_memsz was filtered out (see
* coredump_filter in core(5)). These bytes were not necessarily
* zeroes in the process's memory, which contradicts the ELF
* specification in a way.
*
* As of Linux 5.19, /proc/kcore and /proc/vmcore never have
* segments with p_filesz < p_memsz. However, makedumpfile
* creates segments with p_filesz < p_memsz to indicate ranges
* that were excluded. This is similar to Linux userspace core
* dumps, except that makedumpfile can also exclude ranges that
* were all zeroes.
*
* So, for userspace core dumps, we want to fault for ranges
* between p_filesz and p_memsz to indicate that the memory was
* not saved rather than lying and returning zeroes. For
* /proc/kcore, we don't expect to see p_filesz < p_memsz but we
* fault to be safe. For Linux kernel core dumps, we can't
* distinguish between memory that was excluded because it was
* all zeroes and memory that was excluded by makedumpfile for
* another reason, so we're forced to always return zeroes.
*/
prog->file_segments[j].zerofill = have_vmcoreinfo && !is_proc_kcore;
err = drgn_program_add_memory_segment(prog, phdr->p_vaddr,
phdr->p_memsz,
drgn_read_memory_file,
&prog->file_segments[j],
false);
if (err)
goto out_segments;
if (have_phys_addrs &&
phdr->p_paddr != (is_64_bit ? UINT64_MAX : UINT32_MAX)) {
err = drgn_program_add_memory_segment(prog,
phdr->p_paddr,
phdr->p_memsz,
drgn_read_memory_file,
&prog->file_segments[j],
true);
if (err)
goto out_segments;
}
j++;
}
/*
* Before Linux kernel commit 464920104bf7 ("/proc/kcore: update
* physical address for kcore ram and text") (in v4.11), p_paddr in
* /proc/kcore is always zero. If we know the address of the direct
* mapping, we can still add physical segments. This needs to be a third
* pass, as we may need to read virtual memory to determine the mapping.
*/
if (is_proc_kcore && !have_phys_addrs &&
prog->platform.arch->linux_kernel_live_direct_mapping_fallback) {
uint64_t direct_mapping, direct_mapping_size;
err = prog->platform.arch->linux_kernel_live_direct_mapping_fallback(prog,
&direct_mapping,
&direct_mapping_size);
if (err)
goto out_segments;
for (i = 0, j = 0; i < phnum && j < num_file_segments; i++) {
GElf_Phdr phdr_mem, *phdr;
phdr = gelf_getphdr(prog->core, i, &phdr_mem);
if (!phdr) {
err = drgn_error_libelf();
goto out_segments;
}
if (phdr->p_type != PT_LOAD)
continue;
if (phdr->p_vaddr >= direct_mapping &&
phdr->p_vaddr - direct_mapping + phdr->p_memsz <=
direct_mapping_size) {
uint64_t phys_addr;
phys_addr = phdr->p_vaddr - direct_mapping;
err = drgn_program_add_memory_segment(prog,
phys_addr,
pgtable_reader ?
phdr->p_filesz :
phdr->p_memsz,
drgn_read_memory_file,
&prog->file_segments[j],
true);
if (err)
goto out_segments;
}
j++;
}
}
if (vmcoreinfo_note && !prog->vmcoreinfo.raw) {
err = drgn_program_parse_vmcoreinfo(prog, vmcoreinfo_note,
vmcoreinfo_size);
if (err)
goto out_segments;
}
if (is_proc_kcore) {
if (!have_vmcoreinfo) {
err = read_vmcoreinfo_fallback(prog);
if (err)
goto out_segments;
}
prog->flags |= (DRGN_PROGRAM_IS_LINUX_KERNEL |
DRGN_PROGRAM_IS_LIVE |
DRGN_PROGRAM_IS_LOCAL);
elf_end(prog->core);
prog->core = NULL;
} else if (have_vmcoreinfo) {
prog->flags |= DRGN_PROGRAM_IS_LINUX_KERNEL;
} else if (have_qemu_note) {
err = drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"unrecognized QEMU memory dump; "
"for Linux guests, run QEMU with '-device vmcoreinfo', "
"compile the kernel with CONFIG_FW_CFG_SYSFS and CONFIG_KEXEC, "
"and load the qemu_fw_cfg kernel module "
"before dumping the guest memory "
"(requires Linux >= 4.17 and QEMU >= 2.11)");
goto out_segments;
}
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
err = drgn_program_finish_set_kernel(prog);
if (err)
goto out_segments;
}
drgn_call_plugins_prog("drgn_prog_set", prog);
return NULL;
out_segments:
drgn_memory_reader_deinit(&prog->reader);
drgn_memory_reader_init(&prog->reader);
free(prog->file_segments);
prog->file_segments = NULL;
out_notes:
// Reset anything we parsed from ELF notes.
prog->aarch64_insn_pac_mask = 0;
// Free vmcoreinfo buffer if it was not provided by the caller
if (!had_vmcoreinfo) {
free(prog->vmcoreinfo.raw);
memset(&prog->vmcoreinfo, 0, sizeof(prog->vmcoreinfo));
}
out_platform:
prog->has_platform = had_platform;
out_elf:
elf_end(prog->core);
prog->core = NULL;
out_path:
free(prog->core_path);
prog->core_path = NULL;
out_fd:
close(prog->core_fd);
prog->core_fd = -1;
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_set_core_dump_fd(struct drgn_program *prog, int fd)
{
struct drgn_error *err;
err = drgn_program_check_initialized(prog);
if (err)
return err;
return drgn_program_set_core_dump_fd_internal(prog, fd, NULL);
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_set_core_dump(struct drgn_program *prog, const char *path)
{
struct drgn_error *err;
err = drgn_program_check_initialized(prog);
if (err)
return err;
int fd = open(path, O_RDONLY);
if (fd == -1)
return drgn_error_create_os("open", errno, path);
return drgn_program_set_core_dump_fd_internal(prog, fd, path);
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_set_kernel(struct drgn_program *prog)
{
return drgn_program_set_core_dump(prog, "/proc/kcore");
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_set_pid(struct drgn_program *prog, pid_t pid)
{
struct drgn_error *err;
err = drgn_program_check_initialized(prog);
if (err)
return err;
#define FORMAT "/proc/%ld/mem"
char buf[sizeof(FORMAT) - sizeof("%ld") + max_decimal_length(long) + 1];
snprintf(buf, sizeof(buf), FORMAT, (long)pid);
#undef FORMAT
prog->core_fd = open(buf, O_RDONLY);
if (prog->core_fd == -1)
return drgn_error_create_os("open", errno, buf);
bool had_platform = prog->has_platform;
drgn_program_set_platform(prog, &drgn_host_platform);
prog->file_segments = malloc(sizeof(*prog->file_segments));
if (!prog->file_segments) {
err = &drgn_enomem;
goto out_fd;
}
prog->file_segments[0].file_offset = 0;
prog->file_segments[0].file_size = UINT64_MAX;
prog->file_segments[0].fd = prog->core_fd;
prog->file_segments[0].eio_is_fault = true;
prog->file_segments[0].zerofill = false;
err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX,
drgn_read_memory_file,
prog->file_segments, false);
if (err)
goto out_segments;
prog->pid = pid;
prog->flags |= DRGN_PROGRAM_IS_LIVE | DRGN_PROGRAM_IS_LOCAL;
drgn_call_plugins_prog("drgn_prog_set", prog);
return NULL;
out_segments:
drgn_memory_reader_deinit(&prog->reader);
drgn_memory_reader_init(&prog->reader);
free(prog->file_segments);
prog->file_segments = NULL;
out_fd:
prog->has_platform = had_platform;
close(prog->core_fd);
prog->core_fd = -1;
return err;
}
struct drgn_error *drgn_program_cache_auxv(struct drgn_program *prog)
{
if (prog->auxv_cached)
return NULL;
_cleanup_close_ int fd = -1;
const void *note;
size_t note_size;
#define FORMAT "/proc/%ld/auxv"
char path[sizeof(FORMAT)
- sizeof("%ld")
+ max_decimal_length(long)
+ 1];
if (drgn_program_is_userspace_process(prog)) {
snprintf(path, sizeof(path), FORMAT, (long)prog->pid);
#undef FORMAT
fd = open(path, O_RDONLY);
if (fd < 0)
return drgn_error_create_os("open", errno, path);
drgn_log_debug(prog, "parsing %s", path);
} else {
assert(drgn_program_is_userspace_core(prog));
if (find_elf_note(prog->core, "CORE", NT_AUXV, ¬e,
¬e_size))
return drgn_error_libelf();
if (!note) {
return drgn_error_create(DRGN_ERROR_OTHER,
"core file is missing NT_AUXV");
}
drgn_log_debug(prog, "parsing NT_AUXV");
}
memset(&prog->auxv, 0, sizeof(prog->auxv));
bool is_64_bit = drgn_platform_is_64_bit(&prog->platform);
bool bswap = drgn_platform_bswap(&prog->platform);
size_t aux_size = is_64_bit ? 16 : 8;
#define visit_aux_members(visit_scalar_member, visit_raw_member) do { \
visit_scalar_member(a_type); \
visit_scalar_member(a_un.a_val); \
} while (0)
for (;;) {
Elf64_auxv_t auxv;
if (fd >= 0) {
ssize_t r = read_all(fd, &auxv, aux_size);
if (r < 0)
return drgn_error_create_os("read", errno, path);
if (r < aux_size)
break;
deserialize_struct64_inplace(&auxv, Elf32_auxv_t,
visit_aux_members,
is_64_bit, bswap);
} else {
if (note_size < aux_size)
break;
deserialize_struct64(&auxv, Elf32_auxv_t,
visit_aux_members, note, is_64_bit,
bswap);
note = (char *)note + aux_size;
note_size -= aux_size;
}
if (auxv.a_type == 0 && auxv.a_un.a_val == 0)
break;
switch (auxv.a_type) {
case AT_PHDR:
drgn_log_debug(prog, "found AT_PHDR 0x%" PRIx64,
auxv.a_un.a_val);
prog->auxv.at_phdr = auxv.a_un.a_val;
break;
case AT_PHNUM:
drgn_log_debug(prog, "found AT_PHNUM %" PRIu64,
auxv.a_un.a_val);
prog->auxv.at_phnum = auxv.a_un.a_val;
break;
case AT_SYSINFO_EHDR:
drgn_log_debug(prog, "found AT_SYSINFO_EHDR 0x%" PRIx64,
auxv.a_un.a_val);
prog->auxv.at_sysinfo_ehdr = auxv.a_un.a_val;
break;
}
}
#undef visit_aux_members
prog->auxv_cached = true;
return NULL;
}
static struct drgn_error *get_prstatus_pid(struct drgn_program *prog, const char *data,
size_t size, uint32_t *ret)
{
bool is_64_bit, bswap;
struct drgn_error *err = drgn_program_is_64_bit(prog, &is_64_bit);
if (err)
return err;
err = drgn_program_bswap(prog, &bswap);
if (err)
return err;
size_t offset = is_64_bit ? 32 : 24;
uint32_t pr_pid;
if (size < offset + sizeof(pr_pid)) {
return drgn_error_create(DRGN_ERROR_OTHER,
"NT_PRSTATUS is truncated");
}
memcpy(&pr_pid, data + offset, sizeof(pr_pid));
if (bswap)
pr_pid = bswap_32(pr_pid);
*ret = pr_pid;
return NULL;
}
static struct drgn_error *get_prpsinfo_pid(struct drgn_program *prog,
const char *data, size_t size,
uint32_t *ret)
{
bool is_64_bit, bswap;
struct drgn_error *err = drgn_program_is_64_bit(prog, &is_64_bit);
if (err)
return err;
err = drgn_program_bswap(prog, &bswap);
if (err)
return err;
size_t offset = is_64_bit ? 24 : 12;
uint32_t pr_pid;
if (size < offset + sizeof(pr_pid)) {
return drgn_error_create(DRGN_ERROR_OTHER,
"NT_PRPSINFO is truncated");
}
memcpy(&pr_pid, data + offset, sizeof(pr_pid));
if (bswap)
pr_pid = bswap_32(pr_pid);
*ret = pr_pid;
return NULL;
}
static struct drgn_error *get_prpsinfo_fname(struct drgn_program *prog,
const char *data, size_t size,
char **ret)
{
bool is_64_bit;
struct drgn_error *err = drgn_program_is_64_bit(prog, &is_64_bit);
if (err)
return err;
size_t offset = is_64_bit ? 40 : 28;
// pr_fname is defined as 16 byte buffer in elf_prpsinfo
// https://github.com/torvalds/linux/blob/075dbe9f6e3c21596c5245826a4ee1f1c1676eb8/include/linux/elfcore.h#L73
#define PR_FNAME_LEN 16
if (size < offset + PR_FNAME_LEN) {
return drgn_error_create(DRGN_ERROR_OTHER,
"NT_PRPSINFO is truncated");
}
char *tmp = strndup(data + offset, PR_FNAME_LEN);
#undef PR_FNAME_LEN
if (!tmp)
return &drgn_enomem;
*ret = tmp;
return NULL;
}
struct drgn_error *drgn_thread_dup_internal(const struct drgn_thread *thread,
struct drgn_thread *ret)
{
struct drgn_error *err = NULL;
ret->prog = thread->prog;
ret->tid = thread->tid;
/* Don't need a deep copy here since the PRSTATUS notes are cached. */
ret->prstatus = thread->prstatus;
if (thread->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
drgn_object_init(&ret->object, thread->prog);
err = drgn_object_copy(&ret->object, &thread->object);
if (err)
drgn_object_deinit(&ret->object);
}
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_thread_dup(const struct drgn_thread *thread, struct drgn_thread **ret)
{
if (drgn_program_is_userspace_core(thread->prog)) {
/*
* For userspace core dumps, all threads are cached and
* immutable, so we can return the same handle.
*/
*ret = (struct drgn_thread *)thread;
return NULL;
}
*ret = malloc(sizeof(**ret));
if (!*ret)
return &drgn_enomem;
struct drgn_error *err = drgn_thread_dup_internal(thread, *ret);
if (err)
free(*ret);
return err;
}
void drgn_thread_deinit(struct drgn_thread *thread) {
if (thread->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)
drgn_object_deinit(&thread->object);
}
LIBDRGN_PUBLIC void drgn_thread_destroy(struct drgn_thread *thread)
{
if (thread) {
drgn_thread_deinit(thread);
if (!drgn_program_is_userspace_core(thread->prog))
free(thread);
}
}
struct drgn_error *drgn_program_cache_prstatus_entry(struct drgn_program *prog,
const char *data,
size_t size, uint32_t *ret)
{
struct drgn_thread thread = {
.prog = prog,
.prstatus = { data, size },
};
struct drgn_error *err = get_prstatus_pid(prog, data, size,
&thread.tid);
if (err)
return err;
*ret = thread.tid;
if (drgn_thread_set_insert(&prog->thread_set, &thread, NULL) == -1)
return &drgn_enomem;
return NULL;
}
static struct drgn_error *
drgn_program_cache_core_dump_threads(struct drgn_program *prog)
{
struct drgn_error *err;
size_t phnum, i;
bool found_prstatus = false;
uint32_t first_prstatus_tid;
bool found_prpsinfo = false;
uint32_t prpsinfo_pid;
_cleanup_free_ char *prpsinfo_fname = NULL;
if (prog->core_dump_threads_cached)
return NULL;
assert(!(prog->flags & DRGN_PROGRAM_IS_LIVE));
#ifdef WITH_LIBKDUMPFILE
if (prog->kdump_ctx) {
err = drgn_program_cache_kdump_threads(prog);
if (err)
goto err;
goto out;
}
#endif
if (!prog->core) {
err = NULL;
goto out;
}
if (elf_getphdrnum(prog->core, &phnum) != 0) {
err = drgn_error_libelf();
goto err;
}
for (i = 0; i < phnum; i++) {
GElf_Phdr phdr_mem, *phdr;
Elf_Data *data;
size_t offset;
GElf_Nhdr nhdr;
size_t name_offset, desc_offset;
phdr = gelf_getphdr(prog->core, i, &phdr_mem);
if (!phdr) {
err = drgn_error_libelf();
goto err;
}
if (phdr->p_type != PT_NOTE)
continue;
data = elf_getdata_rawchunk(prog->core, phdr->p_offset,
phdr->p_filesz,
note_header_type(phdr->p_align));
if (!data) {
err = drgn_error_libelf();
goto err;
}
offset = 0;
while (offset < data->d_size &&
(offset = gelf_getnote(data, offset, &nhdr, &name_offset,
&desc_offset))) {
const char *name;
name = (char *)data->d_buf + name_offset;
if (strncmp(name, "CORE", nhdr.n_namesz) != 0)
continue;
if (nhdr.n_type == NT_PRPSINFO) {
err = get_prpsinfo_pid(prog,
(char *)data->d_buf + desc_offset,
nhdr.n_descsz,
&prpsinfo_pid);
if (err)
goto err;
err = get_prpsinfo_fname(prog,
(char *)data->d_buf + desc_offset,
nhdr.n_descsz,
&prpsinfo_fname);
if (err)
goto err;
found_prpsinfo = true;
} else if (nhdr.n_type == NT_PRSTATUS) {
uint32_t tid;
err = drgn_program_cache_prstatus_entry(prog,
(char *)data->d_buf + desc_offset,
nhdr.n_descsz,
&tid);
if (err)
goto err;
/*
* The first PRSTATUS note is the crashed thread. See
* fs/binfmt_elf.c:fill_note_info in the Linux kernel
* and bfd/elf.c:elfcore_grok_prstatus in BFD.
*/
if (!found_prstatus) {
found_prstatus = true;
first_prstatus_tid = tid;
}
}
}
}
out:
prog->core_dump_threads_cached = true;
if (!(prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) {
if (found_prpsinfo) {
struct drgn_thread_set_iterator it =
drgn_thread_set_search(&prog->thread_set,
&prpsinfo_pid);
/* If the PID isn't found, then this is NULL. */
prog->main_thread = it.entry;
prog->core_dump_fname_cached = no_cleanup_ptr(prpsinfo_fname);
}
if (found_prstatus) {
/*
* Now that thread_set won't be modified, look up the crashed
* thread entry.
*/
struct drgn_thread_set_iterator it =
drgn_thread_set_search(&prog->thread_set,
&first_prstatus_tid);
assert(it.entry);
prog->crashed_thread = it.entry;
}
}
return NULL;
err:
drgn_thread_set_deinit(&prog->thread_set);
drgn_thread_set_init(&prog->thread_set);
return err;
}
static struct drgn_error *
drgn_thread_iterator_init_linux_kernel(struct drgn_thread_iterator *it)
{
struct drgn_error *err = linux_helper_task_iterator_init(&it->task_iter,
it->prog);
if (err)
return err;
drgn_object_init(&it->entry.object, it->prog);
it->entry.prstatus = (struct nstring){};
return NULL;
}
static struct drgn_error *
drgn_thread_iterator_init_userspace_process(struct drgn_thread_iterator *it)
{
#define FORMAT "/proc/%ld/task"
char path[sizeof(FORMAT)
- sizeof("%ld")
+ max_decimal_length(long)
+ 1];
snprintf(path, sizeof(path), FORMAT, (long)it->prog->pid);
#undef FORMAT
it->tasks_dir = opendir(path);
if (!it->tasks_dir)
return drgn_error_create_os("opendir", errno, path);
it->entry.prog = it->prog;
it->entry.prstatus = (struct nstring){};
return NULL;
}
static struct drgn_error *
drgn_thread_iterator_init_userspace_core(struct drgn_thread_iterator *it)
{
struct drgn_error *err = drgn_program_cache_core_dump_threads(it->prog);
if (err)
return err;
it->iterator = drgn_thread_set_first(&it->prog->thread_set);
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_thread_iterator_create(struct drgn_program *prog,
struct drgn_thread_iterator **ret)
{
struct drgn_error *err;
*ret = malloc(sizeof(**ret));
if (!*ret)
return &drgn_enomem;
(*ret)->prog = prog;
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)
err = drgn_thread_iterator_init_linux_kernel(*ret);
else if (drgn_program_is_userspace_process(prog))
err = drgn_thread_iterator_init_userspace_process(*ret);
else if (drgn_program_is_userspace_core(prog))
err = drgn_thread_iterator_init_userspace_core(*ret);
else
err = NULL;
if (err)
free(*ret);
return err;
}
LIBDRGN_PUBLIC void
drgn_thread_iterator_destroy(struct drgn_thread_iterator *it)
{
if (it) {
if (it->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
drgn_object_deinit(&it->entry.object);
linux_helper_task_iterator_deinit(&it->task_iter);
} else if (drgn_program_is_userspace_process(it->prog)) {
closedir(it->tasks_dir);
}
free(it);
}
}
static struct drgn_error *
drgn_thread_iterator_next_linux_kernel(struct drgn_thread_iterator *it,
struct drgn_thread **ret)
{
struct drgn_error *err;
err = linux_helper_task_iterator_next(&it->task_iter,
&it->entry.object);
if (err == &drgn_stop) {
*ret = NULL;
return NULL;
} else if (err) {
return err;
}
it->entry.prog = drgn_object_program(&it->entry.object);
union drgn_value tid_value;
DRGN_OBJECT(tid, drgn_object_program(&it->entry.object));
err = drgn_object_member_dereference(&tid, &it->entry.object, "pid");
if (!err)
err = drgn_object_read_integer(&tid, &tid_value);
if (err)
return err;
it->entry.tid = tid_value.uvalue;
*ret = &it->entry;
return NULL;
}
static struct drgn_error *
drgn_thread_iterator_next_userspace_process(struct drgn_thread_iterator *it,
struct drgn_thread **ret)
{
struct dirent *task;
unsigned long tid;
char *end;
do {
errno = 0;
task = readdir(it->tasks_dir);
if (!task) {
if (errno) {
return drgn_error_create_os("readdir", errno,
NULL);
}
*ret = NULL;
return NULL;
}
errno = 0;
tid = strtoul(task->d_name, &end, 10);
/*
* Skip anything that isn't a number (like "." and "..") or
* overflows (which is impossible normally).
*/
} while (*end != '\0' || (tid == ULONG_MAX && errno == ERANGE));
it->entry.tid = tid;
*ret = &it->entry;
return NULL;
}
static void
drgn_thread_iterator_next_userspace_core(struct drgn_thread_iterator *it,
struct drgn_thread **ret)
{
*ret = it->iterator.entry;
if (it->iterator.entry)
it->iterator = drgn_thread_set_next(it->iterator);
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_thread_iterator_next(struct drgn_thread_iterator *it,
struct drgn_thread **ret)
{
if (it->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
return drgn_thread_iterator_next_linux_kernel(it, ret);
} else if (drgn_program_is_userspace_process(it->prog)) {
return drgn_thread_iterator_next_userspace_process(it, ret);
} else if (drgn_program_is_userspace_core(it->prog)) {
drgn_thread_iterator_next_userspace_core(it, ret);
return NULL;
} else {
*ret = NULL;
return NULL;
}
}
static struct drgn_error *
drgn_program_find_thread_linux_kernel(struct drgn_program *prog, uint32_t tid,
struct drgn_thread **ret)
{
struct drgn_error *err;
*ret = malloc(sizeof(**ret));
if (!*ret)
return &drgn_enomem;
(*ret)->prog = prog;
(*ret)->tid = tid;
(*ret)->prstatus = (struct nstring){};
struct drgn_object *object = &(*ret)->object;
drgn_object_init(object, prog);
err = drgn_program_find_object(prog, "init_pid_ns", NULL,
DRGN_FIND_OBJECT_VARIABLE, object);
if (err)
goto err;
err = drgn_object_address_of(object, object);
if (err)
goto err;
err = linux_helper_find_task(object, object, tid);
if (err)
goto err;
bool truthy;
err = drgn_object_bool(object, &truthy);
if (err)
goto err;
if (!truthy) {
drgn_thread_destroy(*ret);
*ret = NULL;
}
return NULL;
err:
drgn_thread_destroy(*ret);
return err;
}
static struct drgn_error *
drgn_program_find_thread_userspace_process(struct drgn_program *prog,
uint32_t tid,
struct drgn_thread **ret)
{
#define FORMAT "/proc/%ld/task/%" PRIu32
char path[sizeof(FORMAT)
- sizeof("%ld%" PRIu32)
+ max_decimal_length(long)
+ max_decimal_length(uint32_t)
+ 1];
snprintf(path, sizeof(path), FORMAT, (long)prog->pid, tid);
#undef FORMAT
int r = access(path, F_OK);
if (r == 0) {
*ret = malloc(sizeof(**ret));
if (!*ret)
return &drgn_enomem;
(*ret)->prog = prog;
(*ret)->tid = tid;
(*ret)->prstatus = (struct nstring){};
return NULL;
} else if (errno == ENOENT) {
*ret = NULL;
return NULL;
} else {
return drgn_error_create_os("access", errno, path);
}
}
static struct drgn_error *
drgn_program_find_thread_userspace_core(struct drgn_program *prog, uint32_t tid,
struct drgn_thread **ret)
{
struct drgn_error *err = drgn_program_cache_core_dump_threads(prog);
if (err)
return err;
*ret = drgn_thread_set_search(&prog->thread_set, &tid).entry;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_find_thread(struct drgn_program *prog, uint32_t tid,
struct drgn_thread **ret)
{
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
return drgn_program_find_thread_linux_kernel(prog, tid, ret);
} else if (drgn_program_is_userspace_process(prog)) {
return drgn_program_find_thread_userspace_process(prog, tid,
ret);
} else if (drgn_program_is_userspace_core(prog)) {
return drgn_program_find_thread_userspace_core(prog, tid, ret);
} else {
*ret = NULL;
return NULL;
}
}
// Get the CPU that crashed in a Linux kernel core dump.
static struct drgn_error *
drgn_program_kernel_get_crashed_cpu(struct drgn_program *prog, uint64_t *ret)
{
struct drgn_error *err;
DRGN_OBJECT(cpu, prog);
union drgn_value cpu_value;
// Since Linux kernel commit 1717f2096b54 ("panic, x86: Fix re-entrance
// problem due to panic on NMI") (in v4.5), the crashed CPU is stored in
// an atomic_t panic_cpu on all architectures.
err = drgn_program_find_object(prog, "panic_cpu", NULL,
DRGN_FIND_OBJECT_VARIABLE, &cpu);
if (!err) {
err = drgn_object_member(&cpu, &cpu, "counter");
if (err)
return err;
err = drgn_object_read_integer(&cpu, &cpu_value);
if (!err)
*ret = cpu_value.uvalue;
} else if (err->code == DRGN_ERROR_LOOKUP) {
// On x86 and x86-64 only, the crashed CPU is also in an int
// crashing_cpu. Use this as a fallback for kernels before
// commit 1717f2096b54 ("panic, x86: Fix re-entrance problem due
// to panic on NMI") (in v4.5).
drgn_error_destroy(err);
err = drgn_program_find_object(prog, "crashing_cpu", NULL,
DRGN_FIND_OBJECT_VARIABLE, &cpu);
if (!err) {
err = drgn_object_read_integer(&cpu, &cpu_value);
if (err)
return err;
// Since Linux kernel commit 5bc329503e81 ("x86/mce:
// Handle broadcasted MCE gracefully with kexec") (in
// v4.12), crashing_cpu is defined in !SMP kernels, but
// it's always -1.
if (cpu_value.svalue == -1)
*ret = 0;
else
*ret = cpu_value.uvalue;
} else if (err->code == DRGN_ERROR_LOOKUP) {
// Before Linux kernel commit 5bc329503e81 ("x86/mce:
// Handle broadcasted MCE gracefully with kexec") (in
// v4.12), crashing_cpu is only defined in SMP kernels.
drgn_error_destroy(err);
err = NULL;
*ret = 0;
}
}
return err;
}
static struct drgn_error *
drgn_program_find_thread_kernel_cpu_curr(struct drgn_program *prog,
uint64_t cpu,
struct drgn_thread **ret)
{
struct drgn_error *err;
struct drgn_thread *thread = malloc(sizeof(*thread));
if (!thread)
return &drgn_enomem;
thread->prog = prog;
DRGN_OBJECT(tmp, prog);
drgn_object_init(&thread->object, prog);
err = linux_helper_cpu_curr(&thread->object, cpu);
if (err)
goto out;
err = drgn_object_member_dereference(&tmp, &thread->object, "pid");
if (err)
goto out;
union drgn_value tid;
err = drgn_object_read_integer(&tmp, &tid);
if (err)
goto out;
thread->tid = tid.uvalue;
thread->prstatus = (struct nstring){};
*ret = thread;
out:
if (err) {
drgn_object_deinit(&thread->object);
free(thread);
}
return err;
}
static struct drgn_error *
drgn_program_kernel_core_dump_cache_crashed_thread(struct drgn_program *prog)
{
struct drgn_error *err;
assert((prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) &&
!(prog->flags & DRGN_PROGRAM_IS_LIVE));
if (prog->crashed_thread)
return NULL;
uint64_t crashed_cpu;
err = drgn_program_kernel_get_crashed_cpu(prog, &crashed_cpu);
if (err)
return err;
err = drgn_program_find_thread_kernel_cpu_curr(prog, crashed_cpu,
&prog->crashed_thread);
if (err) {
prog->crashed_thread = NULL;
return err;
}
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_main_thread(struct drgn_program *prog, struct drgn_thread **ret)
{
struct drgn_error *err;
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"main thread is not defined for the Linux kernel");
}
if (drgn_program_is_userspace_process(prog)) {
if (!prog->main_thread) {
err = drgn_program_find_thread(prog, prog->pid,
&prog->main_thread);
if (err) {
prog->main_thread = NULL;
return err;
}
}
} else if (drgn_program_is_userspace_core(prog)) {
err = drgn_program_cache_core_dump_threads(prog);
if (err)
return err;
}
if (!prog->main_thread) {
return drgn_error_create(DRGN_ERROR_OTHER,
"main thread not found");
}
*ret = prog->main_thread;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_crashed_thread(struct drgn_program *prog, struct drgn_thread **ret)
{
struct drgn_error *err;
if (prog->flags & DRGN_PROGRAM_IS_LIVE) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"crashed thread is only defined for core dumps");
}
if (prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)
err = drgn_program_kernel_core_dump_cache_crashed_thread(prog);
else if (drgn_program_is_userspace_core(prog))
err = drgn_program_cache_core_dump_threads(prog);
else
err = NULL;
if (err)
return err;
if (!prog->crashed_thread) {
return drgn_error_create(DRGN_ERROR_OTHER,
"crashed thread not found");
}
*ret = prog->crashed_thread;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_thread_object(struct drgn_thread *thread, const struct drgn_object **ret)
{
if (!(thread->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL)) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"thread object is currently only defined for the Linux kernel");
}
*ret = &thread->object;
return NULL;
}
static struct drgn_error *
drgn_thread_name_linux_kernel(struct drgn_thread *thread, char **ret)
{
struct drgn_error *err;
DRGN_OBJECT(comm, drgn_object_program(&thread->object));
err = drgn_object_member_dereference(&comm, &thread->object, "comm");
if (!err)
err = drgn_object_read_c_string(&comm, ret);
return err;
}
static struct drgn_error *
drgn_thread_name_userspace_process(struct drgn_thread *thread, char **ret)
{
#define FORMAT "/proc/%" PRIu32 "/comm"
char path[sizeof(FORMAT)
- sizeof("%" PRIu32)
+ max_decimal_length(uint32_t)
+ 1];
snprintf(path, sizeof(path), FORMAT, thread->tid);
#undef FORMAT
_cleanup_close_ int fd = open(path, O_RDONLY);
if (fd < 0)
return drgn_error_create_os("open", errno, path);
// While userspace threads use 16 byte buffer, kernel threads use a 64 byte buffer
// https://github.com/torvalds/linux/blob/075dbe9f6e3c21596c5245826a4ee1f1c1676eb8/fs/proc/array.c#L101
char buf[64];
ssize_t bytes_read = read_all(fd, buf, sizeof(buf));
if (bytes_read < 0)
return drgn_error_create_os("read", errno, path);
if (bytes_read > 0 && buf[bytes_read - 1] == '\n')
bytes_read--;
char *tmp = strndup(buf, bytes_read);
if (!tmp)
return &drgn_enomem;
*ret = tmp;
return NULL;
}
static struct drgn_error *
drgn_thread_name_userspace_core(struct drgn_thread *thread, char **ret)
{
struct drgn_error *err = drgn_program_cache_core_dump_threads(thread->prog);
if (err)
return err;
// Core dumps only contain the main thread name so check if this is the main thread.
// Otherwise, set ret to NULL which will return None in Python.
bool is_main_thread = thread->prog->main_thread && thread->prog->main_thread->tid == thread->tid;
if (is_main_thread && thread->prog->core_dump_fname_cached) {
char *tmp = strdup(thread->prog->core_dump_fname_cached);
if (!tmp)
return &drgn_enomem;
*ret = tmp;
} else {
*ret = NULL;
}
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_thread_name(struct drgn_thread *thread, char **ret)
{
if (thread->prog->flags & DRGN_PROGRAM_IS_LINUX_KERNEL) {
return drgn_thread_name_linux_kernel(thread, ret);
} else if (drgn_program_is_userspace_process(thread->prog)) {
return drgn_thread_name_userspace_process(thread, ret);
} else if (drgn_program_is_userspace_core(thread->prog)) {
return drgn_thread_name_userspace_core(thread, ret);
} else {
*ret = NULL;
return NULL;
}
}
struct drgn_error *drgn_program_find_prstatus(struct drgn_program *prog,
uint32_t tid, struct nstring *ret)
{
struct drgn_error *err = drgn_program_cache_core_dump_threads(prog);
if (err)
return err;
struct drgn_thread *thread =
drgn_thread_set_search(&prog->thread_set, &tid).entry;
if (!thread) {
ret->str = NULL;
ret->len = 0;
return NULL;
}
*ret = thread->prstatus;
return NULL;
}
struct drgn_error *drgn_program_init_core_dump(struct drgn_program *prog,
const char *path)
{
struct drgn_error *err;
err = drgn_program_set_core_dump(prog, path);
if (err)
return err;
err = drgn_program_load_debug_info(prog, NULL, 0, true, true);
if (err && err->code == DRGN_ERROR_MISSING_DEBUG_INFO) {
drgn_error_destroy(err);
err = NULL;
}
return err;
}
struct drgn_error *drgn_program_init_core_dump_fd(struct drgn_program *prog, int fd)
{
struct drgn_error *err;
err = drgn_program_set_core_dump_fd(prog, fd);
if (err)
return err;
err = drgn_program_load_debug_info(prog, NULL, 0, true, true);
if (err && err->code == DRGN_ERROR_MISSING_DEBUG_INFO) {
drgn_error_destroy(err);
err = NULL;
}
return err;
}
struct drgn_error *drgn_program_init_kernel(struct drgn_program *prog)
{
struct drgn_error *err;
err = drgn_program_set_kernel(prog);
if (err)
return err;
err = drgn_program_load_debug_info(prog, NULL, 0, true, true);
if (err && err->code == DRGN_ERROR_MISSING_DEBUG_INFO) {
drgn_error_destroy(err);
err = NULL;
}
return err;
}
struct drgn_error *drgn_program_init_pid(struct drgn_program *prog, pid_t pid)
{
struct drgn_error *err;
err = drgn_program_set_pid(prog, pid);
if (err)
return err;
err = drgn_program_load_debug_info(prog, NULL, 0, true, true);
if (err && err->code == DRGN_ERROR_MISSING_DEBUG_INFO) {
drgn_error_destroy(err);
err = NULL;
}
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_from_core_dump(const char *path, struct drgn_program **ret)
{
struct drgn_program *prog;
struct drgn_error *err = drgn_program_create(NULL, &prog);
if (err)
return err;
err = drgn_program_init_core_dump(prog, path);
if (err) {
drgn_program_destroy(prog);
return err;
}
*ret = prog;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_from_core_dump_fd(int fd, struct drgn_program **ret)
{
struct drgn_program *prog;
struct drgn_error *err = drgn_program_create(NULL, &prog);
if (err)
return err;
err = drgn_program_init_core_dump_fd(prog, fd);
if (err) {
drgn_program_destroy(prog);
return err;
}
*ret = prog;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_from_kernel(struct drgn_program **ret)
{
struct drgn_program *prog;
struct drgn_error *err = drgn_program_create(NULL, &prog);
if (err)
return err;
err = drgn_program_init_kernel(prog);
if (err) {
drgn_program_destroy(prog);
return err;
}
*ret = prog;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_from_pid(pid_t pid, struct drgn_program **ret)
{
struct drgn_program *prog;
struct drgn_error *err = drgn_program_create(NULL, &prog);
if (err)
return err;
err = drgn_program_init_pid(prog, pid);
if (err) {
drgn_program_destroy(prog);
return err;
}
*ret = prog;
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_read_memory(struct drgn_program *prog, void *buf, uint64_t address,
size_t count, bool physical)
{
uint64_t address_mask;
struct drgn_error *err = drgn_program_address_mask(prog, &address_mask);
if (err)
return err;
err = drgn_program_untagged_addr(prog, &address);
if (err)
return err;
char *p = buf;
while (count > 0) {
size_t n = min((uint64_t)(count - 1), address_mask - address) + 1;
err = drgn_memory_reader_read(&prog->reader, p, address, n,
physical);
if (err)
return err;
p += n;
address = 0;
count -= n;
}
return NULL;
}
DEFINE_VECTOR(char_vector, char);
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_read_c_string(struct drgn_program *prog, uint64_t address,
bool physical, size_t max_size, char **ret)
{
VECTOR(char_vector, str);
for (;;) {
struct drgn_error *err = drgn_program_untagged_addr(prog, &address);
if (err)
return err;
char *c = char_vector_append_entry(&str);
if (!c)
return &drgn_enomem;
if (char_vector_size(&str) <= max_size) {
err = drgn_memory_reader_read(&prog->reader, c, address,
1, physical);
if (err)
return err;
if (!*c)
break;
} else {
*c = '\0';
break;
}
address++;
}
char_vector_shrink_to_fit(&str);
char_vector_steal(&str, ret, NULL);
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_read_u8(struct drgn_program *prog, uint64_t address, bool physical,
uint8_t *ret)
{
return drgn_program_read_memory(prog, ret, address, sizeof(*ret),
physical);
}
#define DEFINE_PROGRAM_READ_U(n) \
LIBDRGN_PUBLIC struct drgn_error * \
drgn_program_read_u##n(struct drgn_program *prog, uint64_t address, \
bool physical, uint##n##_t *ret) \
{ \
bool bswap; \
struct drgn_error *err = drgn_program_bswap(prog, &bswap); \
if (err) \
return err; \
uint##n##_t tmp; \
err = drgn_program_read_memory(prog, &tmp, address, sizeof(tmp), \
physical); \
if (err) \
return err; \
if (bswap) \
tmp = bswap_##n(tmp); \
*ret = tmp; \
return NULL; \
}
DEFINE_PROGRAM_READ_U(16)
DEFINE_PROGRAM_READ_U(32)
DEFINE_PROGRAM_READ_U(64)
#undef DEFINE_PROGRAM_READ_U
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_read_word(struct drgn_program *prog, uint64_t address,
bool physical, uint64_t *ret)
{
bool is_64_bit, bswap;
struct drgn_error *err = drgn_program_is_64_bit(prog, &is_64_bit);
if (err)
return err;
err = drgn_program_bswap(prog, &bswap);
if (err)
return err;
if (is_64_bit) {
uint64_t tmp;
err = drgn_program_read_memory(prog, &tmp, address, sizeof(tmp),
physical);
if (err)
return err;
if (bswap)
tmp = bswap_64(tmp);
*ret = tmp;
} else {
uint32_t tmp;
err = drgn_program_read_memory(prog, &tmp, address, sizeof(tmp),
physical);
if (err)
return err;
if (bswap)
tmp = bswap_32(tmp);
*ret = tmp;
}
return NULL;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_find_object(struct drgn_program *prog, const char *name,
const char *filename,
enum drgn_find_object_flags flags,
struct drgn_object *ret)
{
struct drgn_error *err;
if ((flags & ~DRGN_FIND_OBJECT_ANY) || !flags) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"invalid find object flags");
}
if (ret && drgn_object_program(ret) != prog) {
return drgn_error_create(DRGN_ERROR_INVALID_ARGUMENT,
"object is from wrong program");
}
size_t name_len = strlen(name);
drgn_handler_list_for_each_enabled(struct drgn_object_finder, finder,
&prog->object_finders) {
err = finder->ops.find(name, name_len, filename, flags,
finder->arg, ret);
if (err != &drgn_not_found)
return err;
}
const char *kind_str;
switch (flags) {
case DRGN_FIND_OBJECT_CONSTANT:
kind_str = "constant ";
break;
case DRGN_FIND_OBJECT_FUNCTION:
kind_str = "function ";
break;
case DRGN_FIND_OBJECT_VARIABLE:
kind_str = "variable ";
break;
default:
kind_str = "";
break;
}
if (filename) {
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find %s'%s' in '%s'",
kind_str, name, filename);
} else {
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find %s'%s'", kind_str,
name);
}
}
struct drgn_error *drgn_error_symbol_not_found(uint64_t address)
{
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find symbol containing 0x%" PRIx64,
address);
}
static struct drgn_error *
drgn_program_symbols_search(struct drgn_program *prog, const char *name,
uint64_t addr, enum drgn_find_symbol_flags flags,
struct drgn_symbol_result_builder *builder)
{
struct drgn_error *err = NULL;
drgn_handler_list_for_each_enabled(struct drgn_symbol_finder, finder,
&prog->symbol_finders) {
err = finder->ops.find(name, addr, flags, finder->arg, builder);
if (err ||
((flags & DRGN_FIND_SYMBOL_ONE)
&& drgn_symbol_result_builder_count(builder) > 0))
break;
}
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_find_symbols_by_name(struct drgn_program *prog, const char *name,
struct drgn_symbol ***syms_ret,
size_t *count_ret)
{
struct drgn_symbol_result_builder builder;
enum drgn_find_symbol_flags flags = name ? DRGN_FIND_SYMBOL_NAME : 0;
drgn_symbol_result_builder_init(&builder, false);
struct drgn_error *err = drgn_program_symbols_search(prog, name, 0,
flags, &builder);
if (err)
drgn_symbol_result_builder_abort(&builder);
else
drgn_symbol_result_builder_array(&builder, syms_ret, count_ret);
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_find_symbols_by_address(struct drgn_program *prog,
uint64_t address,
struct drgn_symbol ***syms_ret,
size_t *count_ret)
{
struct drgn_symbol_result_builder builder;
enum drgn_find_symbol_flags flags = DRGN_FIND_SYMBOL_ADDR;
drgn_symbol_result_builder_init(&builder, false);
struct drgn_error *err = drgn_program_symbols_search(prog, NULL, address,
flags, &builder);
if (err)
drgn_symbol_result_builder_abort(&builder);
else
drgn_symbol_result_builder_array(&builder, syms_ret, count_ret);
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_find_symbol_by_name(struct drgn_program *prog,
const char *name, struct drgn_symbol **ret)
{
struct drgn_symbol_result_builder builder;
enum drgn_find_symbol_flags flags = DRGN_FIND_SYMBOL_NAME | DRGN_FIND_SYMBOL_ONE;
drgn_symbol_result_builder_init(&builder, true);
struct drgn_error *err = drgn_program_symbols_search(prog, name, 0,
flags, &builder);
if (err) {
drgn_symbol_result_builder_abort(&builder);
return err;
}
if (!drgn_symbol_result_builder_count(&builder))
return drgn_error_format(DRGN_ERROR_LOOKUP,
"could not find symbol with name '%s'", name);
*ret = drgn_symbol_result_builder_single(&builder);
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_find_symbol_by_address(struct drgn_program *prog, uint64_t address,
struct drgn_symbol **ret)
{
struct drgn_symbol_result_builder builder;
enum drgn_find_symbol_flags flags = DRGN_FIND_SYMBOL_ADDR | DRGN_FIND_SYMBOL_ONE;
drgn_symbol_result_builder_init(&builder, true);
struct drgn_error *err = drgn_program_symbols_search(prog, NULL, address,
flags, &builder);
if (err) {
drgn_symbol_result_builder_abort(&builder);
return err;
}
if (!drgn_symbol_result_builder_count(&builder))
return drgn_error_symbol_not_found(address);
*ret = drgn_symbol_result_builder_single(&builder);
return err;
}
struct drgn_error *
drgn_program_find_symbol_by_address_internal(struct drgn_program *prog,
uint64_t address,
struct drgn_symbol **ret)
{
struct drgn_symbol_result_builder builder;
enum drgn_find_symbol_flags flags = DRGN_FIND_SYMBOL_ADDR | DRGN_FIND_SYMBOL_ONE;
drgn_symbol_result_builder_init(&builder, true);
struct drgn_error *err = drgn_program_symbols_search(prog, NULL, address,
flags, &builder);
if (err)
drgn_symbol_result_builder_abort(&builder);
else
*ret = drgn_symbol_result_builder_single(&builder);
return err;
}
LIBDRGN_PUBLIC struct drgn_error *
drgn_program_element_info(struct drgn_program *prog, struct drgn_type *type,
struct drgn_element_info *ret)
{
struct drgn_type *underlying_type;
bool is_pointer, is_array;
underlying_type = drgn_underlying_type(type);
is_pointer = drgn_type_kind(underlying_type) == DRGN_TYPE_POINTER;
is_array = drgn_type_kind(underlying_type) == DRGN_TYPE_ARRAY;
if (!is_pointer && !is_array)
return drgn_type_error("'%s' is not an array or pointer", type);
ret->qualified_type = drgn_type_type(underlying_type);
return drgn_type_bit_size(ret->qualified_type.type, &ret->bit_size);
}
|