1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269
|
//===-- DataExtractor.cpp ---------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// C Includes
// C++ Includes
#include <bitset>
#include <cassert>
#include <cstddef>
#include <cmath>
#include <sstream>
#include <string>
// Other libraries and framework includes
#include "llvm/ADT/APFloat.h"
#include "llvm/ADT/APInt.h"
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/Support/MD5.h"
#include "clang/AST/ASTContext.h"
// Project includes
#include "lldb/Core/DataBufferHeap.h"
#include "lldb/Core/DataExtractor.h"
#include "lldb/Core/DataBuffer.h"
#include "lldb/Core/Disassembler.h"
#include "lldb/Core/Log.h"
#include "lldb/Core/Stream.h"
#include "lldb/Core/StreamString.h"
#include "lldb/Core/UUID.h"
#include "lldb/Core/dwarf.h"
#include "lldb/Host/Endian.h"
#include "lldb/Symbol/ClangASTContext.h"
#include "lldb/Target/ExecutionContext.h"
#include "lldb/Target/ExecutionContextScope.h"
#include "lldb/Target/SectionLoadList.h"
#include "lldb/Target/Target.h"
using namespace lldb;
using namespace lldb_private;
static inline uint16_t
ReadInt16(const unsigned char* ptr, offset_t offset)
{
uint16_t value;
memcpy (&value, ptr + offset, 2);
return value;
}
static inline uint32_t
ReadInt32 (const unsigned char* ptr, offset_t offset = 0)
{
uint32_t value;
memcpy (&value, ptr + offset, 4);
return value;
}
static inline uint64_t
ReadInt64(const unsigned char* ptr, offset_t offset = 0)
{
uint64_t value;
memcpy (&value, ptr + offset, 8);
return value;
}
static inline uint16_t
ReadInt16(const void* ptr)
{
uint16_t value;
memcpy (&value, ptr, 2);
return value;
}
static inline uint16_t
ReadSwapInt16(const unsigned char* ptr, offset_t offset)
{
uint16_t value;
memcpy (&value, ptr + offset, 2);
return llvm::ByteSwap_16(value);
}
static inline uint32_t
ReadSwapInt32 (const unsigned char* ptr, offset_t offset)
{
uint32_t value;
memcpy (&value, ptr + offset, 4);
return llvm::ByteSwap_32(value);
}
static inline uint64_t
ReadSwapInt64(const unsigned char* ptr, offset_t offset)
{
uint64_t value;
memcpy (&value, ptr + offset, 8);
return llvm::ByteSwap_64(value);
}
static inline uint16_t
ReadSwapInt16(const void* ptr)
{
uint16_t value;
memcpy (&value, ptr, 2);
return llvm::ByteSwap_16(value);
}
static inline uint32_t
ReadSwapInt32 (const void* ptr)
{
uint32_t value;
memcpy (&value, ptr, 4);
return llvm::ByteSwap_32(value);
}
static inline uint64_t
ReadSwapInt64(const void* ptr)
{
uint64_t value;
memcpy (&value, ptr, 8);
return llvm::ByteSwap_64(value);
}
#define NON_PRINTABLE_CHAR '.'
DataExtractor::DataExtractor () :
m_start(nullptr),
m_end(nullptr),
m_byte_order(endian::InlHostByteOrder()),
m_addr_size(sizeof(void *)),
m_data_sp(),
m_target_byte_size(1)
{
}
//----------------------------------------------------------------------
// This constructor allows us to use data that is owned by someone else.
// The data must stay around as long as this object is valid.
//----------------------------------------------------------------------
DataExtractor::DataExtractor (const void* data, offset_t length, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size/*=1*/) :
m_start (const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data))),
m_end (const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(data)) + length),
m_byte_order(endian),
m_addr_size (addr_size),
m_data_sp (),
m_target_byte_size(target_byte_size)
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (addr_size == 4 || addr_size == 8);
#endif
}
//----------------------------------------------------------------------
// Make a shared pointer reference to the shared data in "data_sp" and
// set the endian swapping setting to "swap", and the address size to
// "addr_size". The shared data reference will ensure the data lives
// as long as any DataExtractor objects exist that have a reference to
// this data.
//----------------------------------------------------------------------
DataExtractor::DataExtractor (const DataBufferSP& data_sp, ByteOrder endian, uint32_t addr_size, uint32_t target_byte_size/*=1*/) :
m_start(nullptr),
m_end(nullptr),
m_byte_order(endian),
m_addr_size(addr_size),
m_data_sp(),
m_target_byte_size(target_byte_size)
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (addr_size == 4 || addr_size == 8);
#endif
SetData (data_sp);
}
//----------------------------------------------------------------------
// Initialize this object with a subset of the data bytes in "data".
// If "data" contains shared data, then a reference to this shared
// data will added and the shared data will stay around as long
// as any object contains a reference to that data. The endian
// swap and address size settings are copied from "data".
//----------------------------------------------------------------------
DataExtractor::DataExtractor (const DataExtractor& data, offset_t offset, offset_t length, uint32_t target_byte_size/*=1*/) :
m_start(nullptr),
m_end(nullptr),
m_byte_order(data.m_byte_order),
m_addr_size(data.m_addr_size),
m_data_sp(),
m_target_byte_size(target_byte_size)
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (m_addr_size == 4 || m_addr_size == 8);
#endif
if (data.ValidOffset(offset))
{
offset_t bytes_available = data.GetByteSize() - offset;
if (length > bytes_available)
length = bytes_available;
SetData(data, offset, length);
}
}
DataExtractor::DataExtractor (const DataExtractor& rhs) :
m_start (rhs.m_start),
m_end (rhs.m_end),
m_byte_order (rhs.m_byte_order),
m_addr_size (rhs.m_addr_size),
m_data_sp (rhs.m_data_sp),
m_target_byte_size(rhs.m_target_byte_size)
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (m_addr_size == 4 || m_addr_size == 8);
#endif
}
//----------------------------------------------------------------------
// Assignment operator
//----------------------------------------------------------------------
const DataExtractor&
DataExtractor::operator= (const DataExtractor& rhs)
{
if (this != &rhs)
{
m_start = rhs.m_start;
m_end = rhs.m_end;
m_byte_order = rhs.m_byte_order;
m_addr_size = rhs.m_addr_size;
m_data_sp = rhs.m_data_sp;
}
return *this;
}
DataExtractor::~DataExtractor() = default;
//------------------------------------------------------------------
// Clears the object contents back to a default invalid state, and
// release any references to shared data that this object may
// contain.
//------------------------------------------------------------------
void
DataExtractor::Clear ()
{
m_start = nullptr;
m_end = nullptr;
m_byte_order = endian::InlHostByteOrder();
m_addr_size = sizeof(void *);
m_data_sp.reset();
}
//------------------------------------------------------------------
// If this object contains shared data, this function returns the
// offset into that shared data. Else zero is returned.
//------------------------------------------------------------------
size_t
DataExtractor::GetSharedDataOffset () const
{
if (m_start != nullptr)
{
const DataBuffer * data = m_data_sp.get();
if (data != nullptr)
{
const uint8_t * data_bytes = data->GetBytes();
if (data_bytes != nullptr)
{
assert(m_start >= data_bytes);
return m_start - data_bytes;
}
}
}
return 0;
}
//----------------------------------------------------------------------
// Set the data with which this object will extract from to data
// starting at BYTES and set the length of the data to LENGTH bytes
// long. The data is externally owned must be around at least as
// long as this object points to the data. No copy of the data is
// made, this object just refers to this data and can extract from
// it. If this object refers to any shared data upon entry, the
// reference to that data will be released. Is SWAP is set to true,
// any data extracted will be endian swapped.
//----------------------------------------------------------------------
lldb::offset_t
DataExtractor::SetData (const void *bytes, offset_t length, ByteOrder endian)
{
m_byte_order = endian;
m_data_sp.reset();
if (bytes == nullptr || length == 0)
{
m_start = nullptr;
m_end = nullptr;
}
else
{
m_start = const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(bytes));
m_end = m_start + length;
}
return GetByteSize();
}
//----------------------------------------------------------------------
// Assign the data for this object to be a subrange in "data"
// starting "data_offset" bytes into "data" and ending "data_length"
// bytes later. If "data_offset" is not a valid offset into "data",
// then this object will contain no bytes. If "data_offset" is
// within "data" yet "data_length" is too large, the length will be
// capped at the number of bytes remaining in "data". If "data"
// contains a shared pointer to other data, then a ref counted
// pointer to that data will be made in this object. If "data"
// doesn't contain a shared pointer to data, then the bytes referred
// to in "data" will need to exist at least as long as this object
// refers to those bytes. The address size and endian swap settings
// are copied from the current values in "data".
//----------------------------------------------------------------------
lldb::offset_t
DataExtractor::SetData (const DataExtractor& data, offset_t data_offset, offset_t data_length)
{
m_addr_size = data.m_addr_size;
#ifdef LLDB_CONFIGURATION_DEBUG
assert (m_addr_size == 4 || m_addr_size == 8);
#endif
// If "data" contains shared pointer to data, then we can use that
if (data.m_data_sp)
{
m_byte_order = data.m_byte_order;
return SetData(data.m_data_sp, data.GetSharedDataOffset() + data_offset, data_length);
}
// We have a DataExtractor object that just has a pointer to bytes
if (data.ValidOffset(data_offset))
{
if (data_length > data.GetByteSize() - data_offset)
data_length = data.GetByteSize() - data_offset;
return SetData (data.GetDataStart() + data_offset, data_length, data.GetByteOrder());
}
return 0;
}
//----------------------------------------------------------------------
// Assign the data for this object to be a subrange of the shared
// data in "data_sp" starting "data_offset" bytes into "data_sp"
// and ending "data_length" bytes later. If "data_offset" is not
// a valid offset into "data_sp", then this object will contain no
// bytes. If "data_offset" is within "data_sp" yet "data_length" is
// too large, the length will be capped at the number of bytes
// remaining in "data_sp". A ref counted pointer to the data in
// "data_sp" will be made in this object IF the number of bytes this
// object refers to in greater than zero (if at least one byte was
// available starting at "data_offset") to ensure the data stays
// around as long as it is needed. The address size and endian swap
// settings will remain unchanged from their current settings.
//----------------------------------------------------------------------
lldb::offset_t
DataExtractor::SetData (const DataBufferSP& data_sp, offset_t data_offset, offset_t data_length)
{
m_start = m_end = nullptr;
if (data_length > 0)
{
m_data_sp = data_sp;
if (data_sp)
{
const size_t data_size = data_sp->GetByteSize();
if (data_offset < data_size)
{
m_start = data_sp->GetBytes() + data_offset;
const size_t bytes_left = data_size - data_offset;
// Cap the length of we asked for too many
if (data_length <= bytes_left)
m_end = m_start + data_length; // We got all the bytes we wanted
else
m_end = m_start + bytes_left; // Not all the bytes requested were available in the shared data
}
}
}
size_t new_size = GetByteSize();
// Don't hold a shared pointer to the data buffer if we don't share
// any valid bytes in the shared buffer.
if (new_size == 0)
m_data_sp.reset();
return new_size;
}
//----------------------------------------------------------------------
// Extract a single unsigned char from the binary data and update
// the offset pointed to by "offset_ptr".
//
// RETURNS the byte that was extracted, or zero on failure.
//----------------------------------------------------------------------
uint8_t
DataExtractor::GetU8 (offset_t *offset_ptr) const
{
const uint8_t *data = (const uint8_t *)GetData (offset_ptr, 1);
if (data)
return *data;
return 0;
}
//----------------------------------------------------------------------
// Extract "count" unsigned chars from the binary data and update the
// offset pointed to by "offset_ptr". The extracted data is copied into
// "dst".
//
// RETURNS the non-nullptr buffer pointer upon successful extraction of
// all the requested bytes, or nullptr when the data is not available in
// the buffer due to being out of bounds, or insufficient data.
//----------------------------------------------------------------------
void *
DataExtractor::GetU8 (offset_t *offset_ptr, void *dst, uint32_t count) const
{
const uint8_t *data = (const uint8_t *)GetData (offset_ptr, count);
if (data)
{
// Copy the data into the buffer
memcpy (dst, data, count);
// Return a non-nullptr pointer to the converted data as an indicator of success
return dst;
}
return nullptr;
}
//----------------------------------------------------------------------
// Extract a single uint16_t from the data and update the offset
// pointed to by "offset_ptr".
//
// RETURNS the uint16_t that was extracted, or zero on failure.
//----------------------------------------------------------------------
uint16_t
DataExtractor::GetU16 (offset_t *offset_ptr) const
{
uint16_t val = 0;
const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
if (data)
{
if (m_byte_order != endian::InlHostByteOrder())
val = ReadSwapInt16(data);
else
val = ReadInt16 (data);
}
return val;
}
uint16_t
DataExtractor::GetU16_unchecked (offset_t *offset_ptr) const
{
uint16_t val;
if (m_byte_order == endian::InlHostByteOrder())
val = ReadInt16 (m_start, *offset_ptr);
else
val = ReadSwapInt16(m_start, *offset_ptr);
*offset_ptr += sizeof(val);
return val;
}
uint32_t
DataExtractor::GetU32_unchecked (offset_t *offset_ptr) const
{
uint32_t val;
if (m_byte_order == endian::InlHostByteOrder())
val = ReadInt32 (m_start, *offset_ptr);
else
val = ReadSwapInt32 (m_start, *offset_ptr);
*offset_ptr += sizeof(val);
return val;
}
uint64_t
DataExtractor::GetU64_unchecked (offset_t *offset_ptr) const
{
uint64_t val;
if (m_byte_order == endian::InlHostByteOrder())
val = ReadInt64 (m_start, *offset_ptr);
else
val = ReadSwapInt64 (m_start, *offset_ptr);
*offset_ptr += sizeof(val);
return val;
}
//----------------------------------------------------------------------
// Extract "count" uint16_t values from the binary data and update
// the offset pointed to by "offset_ptr". The extracted data is
// copied into "dst".
//
// RETURNS the non-nullptr buffer pointer upon successful extraction of
// all the requested bytes, or nullptr when the data is not available
// in the buffer due to being out of bounds, or insufficient data.
//----------------------------------------------------------------------
void *
DataExtractor::GetU16 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
{
const size_t src_size = sizeof(uint16_t) * count;
const uint16_t *src = (const uint16_t *)GetData (offset_ptr, src_size);
if (src)
{
if (m_byte_order != endian::InlHostByteOrder())
{
uint16_t *dst_pos = (uint16_t *)void_dst;
uint16_t *dst_end = dst_pos + count;
const uint16_t *src_pos = src;
while (dst_pos < dst_end)
{
*dst_pos = ReadSwapInt16 (src_pos);
++dst_pos;
++src_pos;
}
}
else
{
memcpy (void_dst, src, src_size);
}
// Return a non-nullptr pointer to the converted data as an indicator of success
return void_dst;
}
return nullptr;
}
//----------------------------------------------------------------------
// Extract a single uint32_t from the data and update the offset
// pointed to by "offset_ptr".
//
// RETURNS the uint32_t that was extracted, or zero on failure.
//----------------------------------------------------------------------
uint32_t
DataExtractor::GetU32 (offset_t *offset_ptr) const
{
uint32_t val = 0;
const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
if (data)
{
if (m_byte_order != endian::InlHostByteOrder())
{
val = ReadSwapInt32 (data);
}
else
{
memcpy (&val, data, 4);
}
}
return val;
}
//----------------------------------------------------------------------
// Extract "count" uint32_t values from the binary data and update
// the offset pointed to by "offset_ptr". The extracted data is
// copied into "dst".
//
// RETURNS the non-nullptr buffer pointer upon successful extraction of
// all the requested bytes, or nullptr when the data is not available
// in the buffer due to being out of bounds, or insufficient data.
//----------------------------------------------------------------------
void *
DataExtractor::GetU32 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
{
const size_t src_size = sizeof(uint32_t) * count;
const uint32_t *src = (const uint32_t *)GetData (offset_ptr, src_size);
if (src)
{
if (m_byte_order != endian::InlHostByteOrder())
{
uint32_t *dst_pos = (uint32_t *)void_dst;
uint32_t *dst_end = dst_pos + count;
const uint32_t *src_pos = src;
while (dst_pos < dst_end)
{
*dst_pos = ReadSwapInt32 (src_pos);
++dst_pos;
++src_pos;
}
}
else
{
memcpy (void_dst, src, src_size);
}
// Return a non-nullptr pointer to the converted data as an indicator of success
return void_dst;
}
return nullptr;
}
//----------------------------------------------------------------------
// Extract a single uint64_t from the data and update the offset
// pointed to by "offset_ptr".
//
// RETURNS the uint64_t that was extracted, or zero on failure.
//----------------------------------------------------------------------
uint64_t
DataExtractor::GetU64 (offset_t *offset_ptr) const
{
uint64_t val = 0;
const uint8_t *data = (const uint8_t *)GetData (offset_ptr, sizeof(val));
if (data)
{
if (m_byte_order != endian::InlHostByteOrder())
{
val = ReadSwapInt64 (data);
}
else
{
memcpy (&val, data, 8);
}
}
return val;
}
//----------------------------------------------------------------------
// GetU64
//
// Get multiple consecutive 64 bit values. Return true if the entire
// read succeeds and increment the offset pointed to by offset_ptr, else
// return false and leave the offset pointed to by offset_ptr unchanged.
//----------------------------------------------------------------------
void *
DataExtractor::GetU64 (offset_t *offset_ptr, void *void_dst, uint32_t count) const
{
const size_t src_size = sizeof(uint64_t) * count;
const uint64_t *src = (const uint64_t *)GetData (offset_ptr, src_size);
if (src)
{
if (m_byte_order != endian::InlHostByteOrder())
{
uint64_t *dst_pos = (uint64_t *)void_dst;
uint64_t *dst_end = dst_pos + count;
const uint64_t *src_pos = src;
while (dst_pos < dst_end)
{
*dst_pos = ReadSwapInt64 (src_pos);
++dst_pos;
++src_pos;
}
}
else
{
memcpy (void_dst, src, src_size);
}
// Return a non-nullptr pointer to the converted data as an indicator of success
return void_dst;
}
return nullptr;
}
//----------------------------------------------------------------------
// Extract a single integer value from the data and update the offset
// pointed to by "offset_ptr". The size of the extracted integer
// is specified by the "byte_size" argument. "byte_size" should have
// a value between 1 and 4 since the return value is only 32 bits
// wide. Any "byte_size" values less than 1 or greater than 4 will
// result in nothing being extracted, and zero being returned.
//
// RETURNS the integer value that was extracted, or zero on failure.
//----------------------------------------------------------------------
uint32_t
DataExtractor::GetMaxU32 (offset_t *offset_ptr, size_t byte_size) const
{
switch (byte_size)
{
case 1: return GetU8 (offset_ptr); break;
case 2: return GetU16(offset_ptr); break;
case 4: return GetU32(offset_ptr); break;
default:
assert(false && "GetMaxU32 unhandled case!");
break;
}
return 0;
}
//----------------------------------------------------------------------
// Extract a single integer value from the data and update the offset
// pointed to by "offset_ptr". The size of the extracted integer
// is specified by the "byte_size" argument. "byte_size" should have
// a value >= 1 and <= 8 since the return value is only 64 bits
// wide. Any "byte_size" values less than 1 or greater than 8 will
// result in nothing being extracted, and zero being returned.
//
// RETURNS the integer value that was extracted, or zero on failure.
//----------------------------------------------------------------------
uint64_t
DataExtractor::GetMaxU64 (offset_t *offset_ptr, size_t size) const
{
switch (size)
{
case 1: return GetU8 (offset_ptr); break;
case 2: return GetU16(offset_ptr); break;
case 4: return GetU32(offset_ptr); break;
case 8: return GetU64(offset_ptr); break;
default:
assert(false && "GetMax64 unhandled case!");
break;
}
return 0;
}
uint64_t
DataExtractor::GetMaxU64_unchecked (offset_t *offset_ptr, size_t size) const
{
switch (size)
{
case 1: return GetU8_unchecked (offset_ptr); break;
case 2: return GetU16_unchecked (offset_ptr); break;
case 4: return GetU32_unchecked (offset_ptr); break;
case 8: return GetU64_unchecked (offset_ptr); break;
default:
assert(false && "GetMax64 unhandled case!");
break;
}
return 0;
}
int64_t
DataExtractor::GetMaxS64 (offset_t *offset_ptr, size_t size) const
{
switch (size)
{
case 1: return (int8_t)GetU8 (offset_ptr); break;
case 2: return (int16_t)GetU16(offset_ptr); break;
case 4: return (int32_t)GetU32(offset_ptr); break;
case 8: return (int64_t)GetU64(offset_ptr); break;
default:
assert(false && "GetMax64 unhandled case!");
break;
}
return 0;
}
uint64_t
DataExtractor::GetMaxU64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
{
uint64_t uval64 = GetMaxU64 (offset_ptr, size);
if (bitfield_bit_size > 0)
{
int32_t lsbcount = bitfield_bit_offset;
if (m_byte_order == eByteOrderBig)
lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
if (lsbcount > 0)
uval64 >>= lsbcount;
uint64_t bitfield_mask = ((1ul << bitfield_bit_size) - 1);
if (!bitfield_mask && bitfield_bit_offset == 0 && bitfield_bit_size == 64)
return uval64;
uval64 &= bitfield_mask;
}
return uval64;
}
int64_t
DataExtractor::GetMaxS64Bitfield (offset_t *offset_ptr, size_t size, uint32_t bitfield_bit_size, uint32_t bitfield_bit_offset) const
{
int64_t sval64 = GetMaxS64 (offset_ptr, size);
if (bitfield_bit_size > 0)
{
int32_t lsbcount = bitfield_bit_offset;
if (m_byte_order == eByteOrderBig)
lsbcount = size * 8 - bitfield_bit_offset - bitfield_bit_size;
if (lsbcount > 0)
sval64 >>= lsbcount;
uint64_t bitfield_mask = (((uint64_t)1) << bitfield_bit_size) - 1;
sval64 &= bitfield_mask;
// sign extend if needed
if (sval64 & (((uint64_t)1) << (bitfield_bit_size - 1)))
sval64 |= ~bitfield_mask;
}
return sval64;
}
float
DataExtractor::GetFloat (offset_t *offset_ptr) const
{
typedef float float_type;
float_type val = 0.0;
const size_t src_size = sizeof(float_type);
const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
if (src)
{
if (m_byte_order != endian::InlHostByteOrder())
{
const uint8_t *src_data = (const uint8_t *)src;
uint8_t *dst_data = (uint8_t *)&val;
for (size_t i = 0; i < sizeof(float_type); ++i)
dst_data[sizeof(float_type) - 1 - i] = src_data[i];
}
else
{
val = *src;
}
}
return val;
}
double
DataExtractor::GetDouble (offset_t *offset_ptr) const
{
typedef double float_type;
float_type val = 0.0;
const size_t src_size = sizeof(float_type);
const float_type *src = (const float_type *)GetData (offset_ptr, src_size);
if (src)
{
if (m_byte_order != endian::InlHostByteOrder())
{
const uint8_t *src_data = (const uint8_t *)src;
uint8_t *dst_data = (uint8_t *)&val;
for (size_t i = 0; i < sizeof(float_type); ++i)
dst_data[sizeof(float_type) - 1 - i] = src_data[i];
}
else
{
val = *src;
}
}
return val;
}
long double
DataExtractor::GetLongDouble (offset_t *offset_ptr) const
{
long double val = 0.0;
#if defined (__i386__) || defined (__amd64__) || defined (__x86_64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_X64)
*offset_ptr += CopyByteOrderedData (*offset_ptr, 10, &val, sizeof(val), endian::InlHostByteOrder());
#else
*offset_ptr += CopyByteOrderedData (*offset_ptr, sizeof(val), &val, sizeof(val), endian::InlHostByteOrder());
#endif
return val;
}
//------------------------------------------------------------------
// Extract a single address from the data and update the offset
// pointed to by "offset_ptr". The size of the extracted address
// comes from the "this->m_addr_size" member variable and should be
// set correctly prior to extracting any address values.
//
// RETURNS the address that was extracted, or zero on failure.
//------------------------------------------------------------------
uint64_t
DataExtractor::GetAddress (offset_t *offset_ptr) const
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (m_addr_size == 4 || m_addr_size == 8);
#endif
return GetMaxU64 (offset_ptr, m_addr_size);
}
uint64_t
DataExtractor::GetAddress_unchecked (offset_t *offset_ptr) const
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (m_addr_size == 4 || m_addr_size == 8);
#endif
return GetMaxU64_unchecked (offset_ptr, m_addr_size);
}
//------------------------------------------------------------------
// Extract a single pointer from the data and update the offset
// pointed to by "offset_ptr". The size of the extracted pointer
// comes from the "this->m_addr_size" member variable and should be
// set correctly prior to extracting any pointer values.
//
// RETURNS the pointer that was extracted, or zero on failure.
//------------------------------------------------------------------
uint64_t
DataExtractor::GetPointer (offset_t *offset_ptr) const
{
#ifdef LLDB_CONFIGURATION_DEBUG
assert (m_addr_size == 4 || m_addr_size == 8);
#endif
return GetMaxU64 (offset_ptr, m_addr_size);
}
//----------------------------------------------------------------------
// GetDwarfEHPtr
//
// Used for calls when the value type is specified by a DWARF EH Frame
// pointer encoding.
//----------------------------------------------------------------------
uint64_t
DataExtractor::GetGNUEHPointer (offset_t *offset_ptr, uint32_t eh_ptr_enc, lldb::addr_t pc_rel_addr, lldb::addr_t text_addr, lldb::addr_t data_addr)//, BSDRelocs *data_relocs) const
{
if (eh_ptr_enc == DW_EH_PE_omit)
return ULLONG_MAX; // Value isn't in the buffer...
uint64_t baseAddress = 0;
uint64_t addressValue = 0;
const uint32_t addr_size = GetAddressByteSize();
#ifdef LLDB_CONFIGURATION_DEBUG
assert (addr_size == 4 || addr_size == 8);
#endif
bool signExtendValue = false;
// Decode the base part or adjust our offset
switch (eh_ptr_enc & 0x70)
{
case DW_EH_PE_pcrel:
signExtendValue = true;
baseAddress = *offset_ptr;
if (pc_rel_addr != LLDB_INVALID_ADDRESS)
baseAddress += pc_rel_addr;
// else
// Log::GlobalWarning ("PC relative pointer encoding found with invalid pc relative address.");
break;
case DW_EH_PE_textrel:
signExtendValue = true;
if (text_addr != LLDB_INVALID_ADDRESS)
baseAddress = text_addr;
// else
// Log::GlobalWarning ("text relative pointer encoding being decoded with invalid text section address, setting base address to zero.");
break;
case DW_EH_PE_datarel:
signExtendValue = true;
if (data_addr != LLDB_INVALID_ADDRESS)
baseAddress = data_addr;
// else
// Log::GlobalWarning ("data relative pointer encoding being decoded with invalid data section address, setting base address to zero.");
break;
case DW_EH_PE_funcrel:
signExtendValue = true;
break;
case DW_EH_PE_aligned:
{
// SetPointerSize should be called prior to extracting these so the
// pointer size is cached
assert(addr_size != 0);
if (addr_size)
{
// Align to a address size boundary first
uint32_t alignOffset = *offset_ptr % addr_size;
if (alignOffset)
offset_ptr += addr_size - alignOffset;
}
}
break;
default:
break;
}
// Decode the value part
switch (eh_ptr_enc & DW_EH_PE_MASK_ENCODING)
{
case DW_EH_PE_absptr :
{
addressValue = GetAddress (offset_ptr);
// if (data_relocs)
// addressValue = data_relocs->Relocate(*offset_ptr - addr_size, *this, addressValue);
}
break;
case DW_EH_PE_uleb128 : addressValue = GetULEB128(offset_ptr); break;
case DW_EH_PE_udata2 : addressValue = GetU16(offset_ptr); break;
case DW_EH_PE_udata4 : addressValue = GetU32(offset_ptr); break;
case DW_EH_PE_udata8 : addressValue = GetU64(offset_ptr); break;
case DW_EH_PE_sleb128 : addressValue = GetSLEB128(offset_ptr); break;
case DW_EH_PE_sdata2 : addressValue = (int16_t)GetU16(offset_ptr); break;
case DW_EH_PE_sdata4 : addressValue = (int32_t)GetU32(offset_ptr); break;
case DW_EH_PE_sdata8 : addressValue = (int64_t)GetU64(offset_ptr); break;
default:
// Unhandled encoding type
assert(eh_ptr_enc);
break;
}
// Since we promote everything to 64 bit, we may need to sign extend
if (signExtendValue && addr_size < sizeof(baseAddress))
{
uint64_t sign_bit = 1ull << ((addr_size * 8ull) - 1ull);
if (sign_bit & addressValue)
{
uint64_t mask = ~sign_bit + 1;
addressValue |= mask;
}
}
return baseAddress + addressValue;
}
size_t
DataExtractor::ExtractBytes (offset_t offset, offset_t length, ByteOrder dst_byte_order, void *dst) const
{
const uint8_t *src = PeekData (offset, length);
if (src)
{
if (dst_byte_order != GetByteOrder())
{
// Validate that only a word- or register-sized dst is byte swapped
assert (length == 1 || length == 2 || length == 4 || length == 8 ||
length == 10 || length == 16 || length == 32);
for (uint32_t i = 0; i < length; ++i)
((uint8_t*)dst)[i] = src[length - i - 1];
}
else
::memcpy (dst, src, length);
return length;
}
return 0;
}
// Extract data as it exists in target memory
lldb::offset_t
DataExtractor::CopyData (offset_t offset,
offset_t length,
void *dst) const
{
const uint8_t *src = PeekData (offset, length);
if (src)
{
::memcpy (dst, src, length);
return length;
}
return 0;
}
// Extract data and swap if needed when doing the copy
lldb::offset_t
DataExtractor::CopyByteOrderedData (offset_t src_offset,
offset_t src_len,
void *dst_void_ptr,
offset_t dst_len,
ByteOrder dst_byte_order) const
{
// Validate the source info
if (!ValidOffsetForDataOfSize(src_offset, src_len))
assert (ValidOffsetForDataOfSize(src_offset, src_len));
assert (src_len > 0);
assert (m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle);
// Validate the destination info
assert(dst_void_ptr != nullptr);
assert (dst_len > 0);
assert (dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle);
// Validate that only a word- or register-sized dst is byte swapped
assert (dst_byte_order == m_byte_order || dst_len == 1 || dst_len == 2 ||
dst_len == 4 || dst_len == 8 || dst_len == 10 || dst_len == 16 ||
dst_len == 32);
// Must have valid byte orders set in this object and for destination
if (!(dst_byte_order == eByteOrderBig || dst_byte_order == eByteOrderLittle) ||
!(m_byte_order == eByteOrderBig || m_byte_order == eByteOrderLittle))
return 0;
uint8_t* dst = (uint8_t*)dst_void_ptr;
const uint8_t* src = (const uint8_t *)PeekData (src_offset, src_len);
if (src)
{
if (dst_len >= src_len)
{
// We are copying the entire value from src into dst.
// Calculate how many, if any, zeroes we need for the most
// significant bytes if "dst_len" is greater than "src_len"...
const size_t num_zeroes = dst_len - src_len;
if (dst_byte_order == eByteOrderBig)
{
// Big endian, so we lead with zeroes...
if (num_zeroes > 0)
::memset (dst, 0, num_zeroes);
// Then either copy or swap the rest
if (m_byte_order == eByteOrderBig)
{
::memcpy (dst + num_zeroes, src, src_len);
}
else
{
for (uint32_t i = 0; i < src_len; ++i)
dst[i+num_zeroes] = src[src_len - 1 - i];
}
}
else
{
// Little endian destination, so we lead the value bytes
if (m_byte_order == eByteOrderBig)
{
for (uint32_t i = 0; i < src_len; ++i)
dst[i] = src[src_len - 1 - i];
}
else
{
::memcpy (dst, src, src_len);
}
// And zero the rest...
if (num_zeroes > 0)
::memset (dst + src_len, 0, num_zeroes);
}
return src_len;
}
else
{
// We are only copying some of the value from src into dst..
if (dst_byte_order == eByteOrderBig)
{
// Big endian dst
if (m_byte_order == eByteOrderBig)
{
// Big endian dst, with big endian src
::memcpy (dst, src + (src_len - dst_len), dst_len);
}
else
{
// Big endian dst, with little endian src
for (uint32_t i = 0; i < dst_len; ++i)
dst[i] = src[dst_len - 1 - i];
}
}
else
{
// Little endian dst
if (m_byte_order == eByteOrderBig)
{
// Little endian dst, with big endian src
for (uint32_t i = 0; i < dst_len; ++i)
dst[i] = src[src_len - 1 - i];
}
else
{
// Little endian dst, with big endian src
::memcpy (dst, src, dst_len);
}
}
return dst_len;
}
}
return 0;
}
//----------------------------------------------------------------------
// Extracts a variable length NULL terminated C string from
// the data at the offset pointed to by "offset_ptr". The
// "offset_ptr" will be updated with the offset of the byte that
// follows the NULL terminator byte.
//
// If the offset pointed to by "offset_ptr" is out of bounds, or if
// "length" is non-zero and there aren't enough available
// bytes, nullptr will be returned and "offset_ptr" will not be
// updated.
//----------------------------------------------------------------------
const char*
DataExtractor::GetCStr (offset_t *offset_ptr) const
{
const char *cstr = (const char *)PeekData (*offset_ptr, 1);
if (cstr)
{
const char *cstr_end = cstr;
const char *end = (const char *)m_end;
while (cstr_end < end && *cstr_end)
++cstr_end;
// Now we are either at the end of the data or we point to the
// NULL C string terminator with cstr_end...
if (*cstr_end == '\0')
{
// Advance the offset with one extra byte for the NULL terminator
*offset_ptr += (cstr_end - cstr + 1);
return cstr;
}
// We reached the end of the data without finding a NULL C string
// terminator. Fall through and return nullptr otherwise anyone that
// would have used the result as a C string can wander into
// unknown memory...
}
return nullptr;
}
//----------------------------------------------------------------------
// Extracts a NULL terminated C string from the fixed length field of
// length "len" at the offset pointed to by "offset_ptr".
// The "offset_ptr" will be updated with the offset of the byte that
// follows the fixed length field.
//
// If the offset pointed to by "offset_ptr" is out of bounds, or if
// the offset plus the length of the field is out of bounds, or if the
// field does not contain a NULL terminator byte, nullptr will be returned
// and "offset_ptr" will not be updated.
//----------------------------------------------------------------------
const char*
DataExtractor::GetCStr (offset_t *offset_ptr, offset_t len) const
{
const char *cstr = (const char *)PeekData (*offset_ptr, len);
if (cstr != nullptr)
{
if (memchr(cstr, '\0', len) == nullptr)
{
return nullptr;
}
*offset_ptr += len;
return cstr;
}
return nullptr;
}
//------------------------------------------------------------------
// Peeks at a string in the contained data. No verification is done
// to make sure the entire string lies within the bounds of this
// object's data, only "offset" is verified to be a valid offset.
//
// Returns a valid C string pointer if "offset" is a valid offset in
// this object's data, else nullptr is returned.
//------------------------------------------------------------------
const char *
DataExtractor::PeekCStr (offset_t offset) const
{
return (const char *)PeekData (offset, 1);
}
//----------------------------------------------------------------------
// Extracts an unsigned LEB128 number from this object's data
// starting at the offset pointed to by "offset_ptr". The offset
// pointed to by "offset_ptr" will be updated with the offset of the
// byte following the last extracted byte.
//
// Returned the extracted integer value.
//----------------------------------------------------------------------
uint64_t
DataExtractor::GetULEB128 (offset_t *offset_ptr) const
{
const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
if (src == nullptr)
return 0;
const uint8_t *end = m_end;
if (src < end)
{
uint64_t result = *src++;
if (result >= 0x80)
{
result &= 0x7f;
int shift = 7;
while (src < end)
{
uint8_t byte = *src++;
result |= (uint64_t)(byte & 0x7f) << shift;
if ((byte & 0x80) == 0)
break;
shift += 7;
}
}
*offset_ptr = src - m_start;
return result;
}
return 0;
}
//----------------------------------------------------------------------
// Extracts an signed LEB128 number from this object's data
// starting at the offset pointed to by "offset_ptr". The offset
// pointed to by "offset_ptr" will be updated with the offset of the
// byte following the last extracted byte.
//
// Returned the extracted integer value.
//----------------------------------------------------------------------
int64_t
DataExtractor::GetSLEB128 (offset_t *offset_ptr) const
{
const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
if (src == nullptr)
return 0;
const uint8_t *end = m_end;
if (src < end)
{
int64_t result = 0;
int shift = 0;
int size = sizeof (int64_t) * 8;
uint8_t byte = 0;
int bytecount = 0;
while (src < end)
{
bytecount++;
byte = *src++;
result |= (int64_t)(byte & 0x7f) << shift;
shift += 7;
if ((byte & 0x80) == 0)
break;
}
// Sign bit of byte is 2nd high order bit (0x40)
if (shift < size && (byte & 0x40))
result |= - (1 << shift);
*offset_ptr += bytecount;
return result;
}
return 0;
}
//----------------------------------------------------------------------
// Skips a ULEB128 number (signed or unsigned) from this object's
// data starting at the offset pointed to by "offset_ptr". The
// offset pointed to by "offset_ptr" will be updated with the offset
// of the byte following the last extracted byte.
//
// Returns the number of bytes consumed during the extraction.
//----------------------------------------------------------------------
uint32_t
DataExtractor::Skip_LEB128 (offset_t *offset_ptr) const
{
uint32_t bytes_consumed = 0;
const uint8_t *src = (const uint8_t *)PeekData (*offset_ptr, 1);
if (src == nullptr)
return 0;
const uint8_t *end = m_end;
if (src < end)
{
const uint8_t *src_pos = src;
while ((src_pos < end) && (*src_pos++ & 0x80))
++bytes_consumed;
*offset_ptr += src_pos - src;
}
return bytes_consumed;
}
static bool
GetAPInt (const DataExtractor &data, lldb::offset_t *offset_ptr, lldb::offset_t byte_size, llvm::APInt &result)
{
llvm::SmallVector<uint64_t, 2> uint64_array;
lldb::offset_t bytes_left = byte_size;
uint64_t u64;
const lldb::ByteOrder byte_order = data.GetByteOrder();
if (byte_order == lldb::eByteOrderLittle)
{
while (bytes_left > 0)
{
if (bytes_left >= 8)
{
u64 = data.GetU64(offset_ptr);
bytes_left -= 8;
}
else
{
u64 = data.GetMaxU64(offset_ptr, (uint32_t)bytes_left);
bytes_left = 0;
}
uint64_array.push_back(u64);
}
result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
return true;
}
else if (byte_order == lldb::eByteOrderBig)
{
lldb::offset_t be_offset = *offset_ptr + byte_size;
lldb::offset_t temp_offset;
while (bytes_left > 0)
{
if (bytes_left >= 8)
{
be_offset -= 8;
temp_offset = be_offset;
u64 = data.GetU64(&temp_offset);
bytes_left -= 8;
}
else
{
be_offset -= bytes_left;
temp_offset = be_offset;
u64 = data.GetMaxU64(&temp_offset, (uint32_t)bytes_left);
bytes_left = 0;
}
uint64_array.push_back(u64);
}
*offset_ptr += byte_size;
result = llvm::APInt(byte_size * 8, llvm::ArrayRef<uint64_t>(uint64_array));
return true;
}
return false;
}
static lldb::offset_t
DumpAPInt (Stream *s, const DataExtractor &data, lldb::offset_t offset, lldb::offset_t byte_size, bool is_signed, unsigned radix)
{
llvm::APInt apint;
if (GetAPInt (data, &offset, byte_size, apint))
{
std::string apint_str(apint.toString(radix, is_signed));
switch (radix)
{
case 2:
s->Write ("0b", 2);
break;
case 8:
s->Write ("0", 1);
break;
case 10:
break;
}
s->Write(apint_str.c_str(), apint_str.size());
}
return offset;
}
static float
half2float (uint16_t half)
{
union { float f; uint32_t u; } u;
int32_t v = (int16_t) half;
if (0 == (v & 0x7c00))
{
u.u = v & 0x80007FFFU;
return u.f * ldexpf(1, 125);
}
v <<= 13;
u.u = v | 0x70000000U;
return u.f * ldexpf(1, -112);
}
lldb::offset_t
DataExtractor::Dump (Stream *s,
offset_t start_offset,
lldb::Format item_format,
size_t item_byte_size,
size_t item_count,
size_t num_per_line,
uint64_t base_addr,
uint32_t item_bit_size, // If zero, this is not a bitfield value, if non-zero, the value is a bitfield
uint32_t item_bit_offset, // If "item_bit_size" is non-zero, this is the shift amount to apply to a bitfield
ExecutionContextScope *exe_scope) const
{
if (s == nullptr)
return start_offset;
if (item_format == eFormatPointer)
{
if (item_byte_size != 4 && item_byte_size != 8)
item_byte_size = s->GetAddressByteSize();
}
offset_t offset = start_offset;
if (item_format == eFormatInstruction)
{
TargetSP target_sp;
if (exe_scope)
target_sp = exe_scope->CalculateTarget();
if (target_sp)
{
DisassemblerSP disassembler_sp(Disassembler::FindPlugin(target_sp->GetArchitecture(), nullptr, nullptr));
if (disassembler_sp)
{
lldb::addr_t addr = base_addr + start_offset;
lldb_private::Address so_addr;
bool data_from_file = true;
if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
{
data_from_file = false;
}
else
{
if (target_sp->GetSectionLoadList().IsEmpty() || !target_sp->GetImages().ResolveFileAddress(addr, so_addr))
so_addr.SetRawAddress(addr);
}
size_t bytes_consumed = disassembler_sp->DecodeInstructions (so_addr, *this, start_offset, item_count, false, data_from_file);
if (bytes_consumed)
{
offset += bytes_consumed;
const bool show_address = base_addr != LLDB_INVALID_ADDRESS;
const bool show_bytes = true;
ExecutionContext exe_ctx;
exe_scope->CalculateExecutionContext(exe_ctx);
disassembler_sp->GetInstructionList().Dump (s, show_address, show_bytes, &exe_ctx);
}
}
}
else
s->Printf ("invalid target");
return offset;
}
if ((item_format == eFormatOSType || item_format == eFormatAddressInfo) && item_byte_size > 8)
item_format = eFormatHex;
lldb::offset_t line_start_offset = start_offset;
for (uint32_t count = 0; ValidOffset(offset) && count < item_count; ++count)
{
if ((count % num_per_line) == 0)
{
if (count > 0)
{
if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
{
s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
}
s->EOL();
}
if (base_addr != LLDB_INVALID_ADDRESS)
s->Printf ("0x%8.8" PRIx64 ": ",
(uint64_t)(base_addr + (offset - start_offset)/m_target_byte_size ));
line_start_offset = offset;
}
else if (item_format != eFormatChar &&
item_format != eFormatCharPrintable &&
item_format != eFormatCharArray &&
count > 0)
{
s->PutChar(' ');
}
switch (item_format)
{
case eFormatBoolean:
if (item_byte_size <= 8)
s->Printf ("%s", GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset) ? "true" : "false");
else
{
s->Printf("error: unsupported byte size (%" PRIu64 ") for boolean format", (uint64_t)item_byte_size);
return offset;
}
break;
case eFormatBinary:
if (item_byte_size <= 8)
{
uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
// Avoid std::bitset<64>::to_string() since it is missing in
// earlier C++ libraries
std::string binary_value(64, '0');
std::bitset<64> bits(uval64);
for (uint32_t i = 0; i < 64; ++i)
if (bits[i])
binary_value[64 - 1 - i] = '1';
if (item_bit_size > 0)
s->Printf("0b%s", binary_value.c_str() + 64 - item_bit_size);
else if (item_byte_size > 0 && item_byte_size <= 8)
s->Printf("0b%s", binary_value.c_str() + 64 - item_byte_size * 8);
}
else
{
const bool is_signed = false;
const unsigned radix = 2;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatBytes:
case eFormatBytesWithASCII:
for (uint32_t i = 0; i < item_byte_size; ++i)
{
s->Printf ("%2.2x", GetU8(&offset));
}
// Put an extra space between the groups of bytes if more than one
// is being dumped in a group (item_byte_size is more than 1).
if (item_byte_size > 1)
s->PutChar(' ');
break;
case eFormatChar:
case eFormatCharPrintable:
case eFormatCharArray:
{
// If we are only printing one character surround it with single
// quotes
if (item_count == 1 && item_format == eFormatChar)
s->PutChar('\'');
const uint64_t ch = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
if (isprint(ch))
s->Printf ("%c", (char)ch);
else if (item_format != eFormatCharPrintable)
{
switch (ch)
{
case '\033': s->Printf ("\\e"); break;
case '\a': s->Printf ("\\a"); break;
case '\b': s->Printf ("\\b"); break;
case '\f': s->Printf ("\\f"); break;
case '\n': s->Printf ("\\n"); break;
case '\r': s->Printf ("\\r"); break;
case '\t': s->Printf ("\\t"); break;
case '\v': s->Printf ("\\v"); break;
case '\0': s->Printf ("\\0"); break;
default:
if (item_byte_size == 1)
s->Printf ("\\x%2.2x", (uint8_t)ch);
else
s->Printf ("%" PRIu64, ch);
break;
}
}
else
{
s->PutChar(NON_PRINTABLE_CHAR);
}
// If we are only printing one character surround it with single quotes
if (item_count == 1 && item_format == eFormatChar)
s->PutChar('\'');
}
break;
case eFormatEnum: // Print enum value as a signed integer when we don't get the enum type
case eFormatDecimal:
if (item_byte_size <= 8)
s->Printf ("%" PRId64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
else
{
const bool is_signed = true;
const unsigned radix = 10;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatUnsigned:
if (item_byte_size <= 8)
s->Printf ("%" PRIu64, GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
else
{
const bool is_signed = false;
const unsigned radix = 10;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatOctal:
if (item_byte_size <= 8)
s->Printf ("0%" PRIo64, GetMaxS64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
else
{
const bool is_signed = false;
const unsigned radix = 8;
offset = DumpAPInt (s, *this, offset, item_byte_size, is_signed, radix);
}
break;
case eFormatOSType:
{
uint64_t uval64 = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
s->PutChar('\'');
for (uint32_t i = 0; i < item_byte_size; ++i)
{
uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
if (isprint(ch))
s->Printf ("%c", ch);
else
{
switch (ch)
{
case '\033': s->Printf ("\\e"); break;
case '\a': s->Printf ("\\a"); break;
case '\b': s->Printf ("\\b"); break;
case '\f': s->Printf ("\\f"); break;
case '\n': s->Printf ("\\n"); break;
case '\r': s->Printf ("\\r"); break;
case '\t': s->Printf ("\\t"); break;
case '\v': s->Printf ("\\v"); break;
case '\0': s->Printf ("\\0"); break;
default: s->Printf ("\\x%2.2x", ch); break;
}
}
}
s->PutChar('\'');
}
break;
case eFormatCString:
{
const char *cstr = GetCStr(&offset);
if (!cstr)
{
s->Printf("NULL");
offset = LLDB_INVALID_OFFSET;
}
else
{
s->PutChar('\"');
while (const char c = *cstr)
{
if (isprint(c))
{
s->PutChar(c);
}
else
{
switch (c)
{
case '\033': s->Printf ("\\e"); break;
case '\a': s->Printf ("\\a"); break;
case '\b': s->Printf ("\\b"); break;
case '\f': s->Printf ("\\f"); break;
case '\n': s->Printf ("\\n"); break;
case '\r': s->Printf ("\\r"); break;
case '\t': s->Printf ("\\t"); break;
case '\v': s->Printf ("\\v"); break;
default: s->Printf ("\\x%2.2x", c); break;
}
}
++cstr;
}
s->PutChar('\"');
}
}
break;
case eFormatPointer:
s->Address(GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset), sizeof (addr_t));
break;
case eFormatComplexInteger:
{
size_t complex_int_byte_size = item_byte_size / 2;
if (complex_int_byte_size > 0 && complex_int_byte_size <= 8)
{
s->Printf("%" PRIu64, GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
s->Printf(" + %" PRIu64 "i", GetMaxU64Bitfield(&offset, complex_int_byte_size, 0, 0));
}
else
{
s->Printf("error: unsupported byte size (%" PRIu64 ") for complex integer format", (uint64_t)item_byte_size);
return offset;
}
}
break;
case eFormatComplex:
if (sizeof(float) * 2 == item_byte_size)
{
float f32_1 = GetFloat (&offset);
float f32_2 = GetFloat (&offset);
s->Printf ("%g + %gi", f32_1, f32_2);
break;
}
else if (sizeof(double) * 2 == item_byte_size)
{
double d64_1 = GetDouble (&offset);
double d64_2 = GetDouble (&offset);
s->Printf ("%lg + %lgi", d64_1, d64_2);
break;
}
else if (sizeof(long double) * 2 == item_byte_size)
{
long double ld64_1 = GetLongDouble (&offset);
long double ld64_2 = GetLongDouble (&offset);
s->Printf ("%Lg + %Lgi", ld64_1, ld64_2);
break;
}
else
{
s->Printf("error: unsupported byte size (%" PRIu64 ") for complex float format", (uint64_t)item_byte_size);
return offset;
}
break;
default:
case eFormatDefault:
case eFormatHex:
case eFormatHexUppercase:
{
bool wantsuppercase = (item_format == eFormatHexUppercase);
switch (item_byte_size)
{
case 1:
case 2:
case 4:
case 8:
s->Printf(wantsuppercase ? "0x%*.*" PRIX64 : "0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset));
break;
default:
{
assert (item_bit_size == 0 && item_bit_offset == 0);
const uint8_t *bytes = (const uint8_t* )GetData(&offset, item_byte_size);
if (bytes)
{
s->PutCString("0x");
uint32_t idx;
if (m_byte_order == eByteOrderBig)
{
for (idx = 0; idx < item_byte_size; ++idx)
s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[idx]);
}
else
{
for (idx = 0; idx < item_byte_size; ++idx)
s->Printf(wantsuppercase ? "%2.2X" : "%2.2x", bytes[item_byte_size - 1 - idx]);
}
}
}
break;
}
}
break;
case eFormatFloat:
{
TargetSP target_sp;
bool used_apfloat = false;
if (exe_scope)
target_sp = exe_scope->CalculateTarget();
if (target_sp)
{
ClangASTContext *clang_ast = target_sp->GetScratchClangASTContext();
if (clang_ast)
{
clang::ASTContext *ast = clang_ast->getASTContext();
if (ast)
{
llvm::SmallVector<char, 256> sv;
// Show full precision when printing float values
const unsigned format_precision = 0;
const unsigned format_max_padding = 100;
size_t item_bit_size = item_byte_size * 8;
if (item_bit_size == ast->getTypeSize(ast->FloatTy))
{
llvm::APInt apint(item_bit_size, this->GetMaxU64(&offset, item_byte_size));
llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->FloatTy), apint);
apfloat.toString(sv, format_precision, format_max_padding);
}
else if (item_bit_size == ast->getTypeSize(ast->DoubleTy))
{
llvm::APInt apint;
if (GetAPInt (*this, &offset, item_byte_size, apint))
{
llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->DoubleTy), apint);
apfloat.toString(sv, format_precision, format_max_padding);
}
}
else if (item_bit_size == ast->getTypeSize(ast->LongDoubleTy))
{
const auto &semantics = ast->getFloatTypeSemantics(ast->LongDoubleTy);
const auto byte_size = (llvm::APFloat::getSizeInBits(semantics) + 7) / 8;
llvm::APInt apint;
if (GetAPInt(*this, &offset, byte_size, apint))
{
llvm::APFloat apfloat(semantics, apint);
apfloat.toString(sv, format_precision, format_max_padding);
}
}
else if (item_bit_size == ast->getTypeSize(ast->HalfTy))
{
llvm::APInt apint(item_bit_size, this->GetU16(&offset));
llvm::APFloat apfloat (ast->getFloatTypeSemantics(ast->HalfTy), apint);
apfloat.toString(sv, format_precision, format_max_padding);
}
if (!sv.empty())
{
s->Printf("%*.*s", (int)sv.size(), (int)sv.size(), sv.data());
used_apfloat = true;
}
}
}
}
if (!used_apfloat)
{
std::ostringstream ss;
if (item_byte_size == sizeof(float) || item_byte_size == 2)
{
float f;
if (item_byte_size == 2)
{
uint16_t half = this->GetU16(&offset);
f = half2float(half);
}
else
{
f = GetFloat (&offset);
}
ss.precision(std::numeric_limits<float>::digits10);
ss << f;
}
else if (item_byte_size == sizeof(double))
{
ss.precision(std::numeric_limits<double>::digits10);
ss << GetDouble(&offset);
}
else if (item_byte_size == sizeof(long double) || item_byte_size == 10)
{
ss.precision(std::numeric_limits<long double>::digits10);
ss << GetLongDouble(&offset);
}
else
{
s->Printf("error: unsupported byte size (%" PRIu64 ") for float format", (uint64_t)item_byte_size);
return offset;
}
ss.flush();
s->Printf("%s", ss.str().c_str());
}
}
break;
case eFormatUnicode16:
s->Printf("U+%4.4x", GetU16 (&offset));
break;
case eFormatUnicode32:
s->Printf("U+0x%8.8x", GetU32 (&offset));
break;
case eFormatAddressInfo:
{
addr_t addr = GetMaxU64Bitfield(&offset, item_byte_size, item_bit_size, item_bit_offset);
s->Printf("0x%*.*" PRIx64, (int)(2 * item_byte_size), (int)(2 * item_byte_size), addr);
if (exe_scope)
{
TargetSP target_sp (exe_scope->CalculateTarget());
lldb_private::Address so_addr;
if (target_sp)
{
if (target_sp->GetSectionLoadList().ResolveLoadAddress(addr, so_addr))
{
s->PutChar(' ');
so_addr.Dump (s,
exe_scope,
Address::DumpStyleResolvedDescription,
Address::DumpStyleModuleWithFileAddress);
}
else
{
so_addr.SetOffset(addr);
so_addr.Dump (s, exe_scope, Address::DumpStyleResolvedPointerDescription);
}
}
}
}
break;
case eFormatHexFloat:
if (sizeof(float) == item_byte_size)
{
char float_cstr[256];
llvm::APFloat ap_float (GetFloat (&offset));
ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
s->Printf ("%s", float_cstr);
break;
}
else if (sizeof(double) == item_byte_size)
{
char float_cstr[256];
llvm::APFloat ap_float (GetDouble (&offset));
ap_float.convertToHexString (float_cstr, 0, false, llvm::APFloat::rmNearestTiesToEven);
s->Printf ("%s", float_cstr);
break;
}
else
{
s->Printf("error: unsupported byte size (%" PRIu64 ") for hex float format", (uint64_t)item_byte_size);
return offset;
}
break;
// please keep the single-item formats below in sync with FormatManager::GetSingleItemFormat
// if you fail to do so, users will start getting different outputs depending on internal
// implementation details they should not care about ||
case eFormatVectorOfChar: // ||
s->PutChar('{'); // \/
offset = Dump (s, offset, eFormatCharArray, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt8:
s->PutChar('{');
offset = Dump (s, offset, eFormatDecimal, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt8:
s->PutChar('{');
offset = Dump (s, offset, eFormatHex, 1, item_byte_size, item_byte_size, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt16:
s->PutChar('{');
offset = Dump (s, offset, eFormatDecimal, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt16:
s->PutChar('{');
offset = Dump (s, offset, eFormatHex, sizeof(uint16_t), item_byte_size / sizeof(uint16_t), item_byte_size / sizeof(uint16_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt32:
s->PutChar('{');
offset = Dump (s, offset, eFormatDecimal, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt32:
s->PutChar('{');
offset = Dump (s, offset, eFormatHex, sizeof(uint32_t), item_byte_size / sizeof(uint32_t), item_byte_size / sizeof(uint32_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfSInt64:
s->PutChar('{');
offset = Dump (s, offset, eFormatDecimal, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt64:
s->PutChar('{');
offset = Dump (s, offset, eFormatHex, sizeof(uint64_t), item_byte_size / sizeof(uint64_t), item_byte_size / sizeof(uint64_t), LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfFloat16:
s->PutChar('{');
offset = Dump (s, offset, eFormatFloat, 2, item_byte_size / 2, item_byte_size / 2, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfFloat32:
s->PutChar('{');
offset = Dump (s, offset, eFormatFloat, 4, item_byte_size / 4, item_byte_size / 4, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfFloat64:
s->PutChar('{');
offset = Dump (s, offset, eFormatFloat, 8, item_byte_size / 8, item_byte_size / 8, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
case eFormatVectorOfUInt128:
s->PutChar('{');
offset = Dump (s, offset, eFormatHex, 16, item_byte_size / 16, item_byte_size / 16, LLDB_INVALID_ADDRESS, 0, 0);
s->PutChar('}');
break;
}
}
if (item_format == eFormatBytesWithASCII && offset > line_start_offset)
{
s->Printf("%*s", static_cast<int>((num_per_line - (offset - line_start_offset)) * 3 + 2), "");
Dump(s, line_start_offset, eFormatCharPrintable, 1, offset - line_start_offset, SIZE_MAX, LLDB_INVALID_ADDRESS, 0, 0);
}
return offset; // Return the offset at which we ended up
}
//----------------------------------------------------------------------
// Dumps bytes from this object's data to the stream "s" starting
// "start_offset" bytes into this data, and ending with the byte
// before "end_offset". "base_addr" will be added to the offset
// into the dumped data when showing the offset into the data in the
// output information. "num_per_line" objects of type "type" will
// be dumped with the option to override the format for each object
// with "type_format". "type_format" is a printf style formatting
// string. If "type_format" is nullptr, then an appropriate format
// string will be used for the supplied "type". If the stream "s"
// is nullptr, then the output will be send to Log().
//----------------------------------------------------------------------
lldb::offset_t
DataExtractor::PutToLog(Log *log,
offset_t start_offset,
offset_t length,
uint64_t base_addr,
uint32_t num_per_line,
DataExtractor::Type type,
const char *format) const
{
if (log == nullptr)
return start_offset;
offset_t offset;
offset_t end_offset;
uint32_t count;
StreamString sstr;
for (offset = start_offset, end_offset = offset + length, count = 0; ValidOffset(offset) && offset < end_offset; ++count)
{
if ((count % num_per_line) == 0)
{
// Print out any previous string
if (sstr.GetSize() > 0)
{
log->Printf("%s", sstr.GetData());
sstr.Clear();
}
// Reset string offset and fill the current line string with address:
if (base_addr != LLDB_INVALID_ADDRESS)
sstr.Printf("0x%8.8" PRIx64 ":", (uint64_t)(base_addr + (offset - start_offset)));
}
switch (type)
{
case TypeUInt8: sstr.Printf (format ? format : " %2.2x", GetU8(&offset)); break;
case TypeChar:
{
char ch = GetU8(&offset);
sstr.Printf (format ? format : " %c", isprint(ch) ? ch : ' ');
}
break;
case TypeUInt16: sstr.Printf (format ? format : " %4.4x", GetU16(&offset)); break;
case TypeUInt32: sstr.Printf (format ? format : " %8.8x", GetU32(&offset)); break;
case TypeUInt64: sstr.Printf (format ? format : " %16.16" PRIx64, GetU64(&offset)); break;
case TypePointer: sstr.Printf (format ? format : " 0x%" PRIx64, GetAddress(&offset)); break;
case TypeULEB128: sstr.Printf (format ? format : " 0x%" PRIx64, GetULEB128(&offset)); break;
case TypeSLEB128: sstr.Printf (format ? format : " %" PRId64, GetSLEB128(&offset)); break;
}
}
if (sstr.GetSize() > 0)
log->Printf("%s", sstr.GetData());
return offset; // Return the offset at which we ended up
}
//----------------------------------------------------------------------
// DumpUUID
//
// Dump out a UUID starting at 'offset' bytes into the buffer
//----------------------------------------------------------------------
void
DataExtractor::DumpUUID (Stream *s, offset_t offset) const
{
if (s)
{
const uint8_t *uuid_data = PeekData(offset, 16);
if ( uuid_data )
{
lldb_private::UUID uuid(uuid_data, 16);
uuid.Dump(s);
}
else
{
s->Printf("<not enough data for UUID at offset 0x%8.8" PRIx64 ">", offset);
}
}
}
void
DataExtractor::DumpHexBytes (Stream *s,
const void *src,
size_t src_len,
uint32_t bytes_per_line,
addr_t base_addr)
{
DataExtractor data (src, src_len, eByteOrderLittle, 4);
data.Dump (s,
0, // Offset into "src"
eFormatBytes, // Dump as hex bytes
1, // Size of each item is 1 for single bytes
src_len, // Number of bytes
bytes_per_line, // Num bytes per line
base_addr, // Base address
0, 0); // Bitfield info
}
size_t
DataExtractor::Copy (DataExtractor &dest_data) const
{
if (m_data_sp)
{
// we can pass along the SP to the data
dest_data.SetData(m_data_sp);
}
else
{
const uint8_t *base_ptr = m_start;
size_t data_size = GetByteSize();
dest_data.SetData(DataBufferSP(new DataBufferHeap(base_ptr, data_size)));
}
return GetByteSize();
}
bool
DataExtractor::Append(DataExtractor& rhs)
{
if (rhs.GetByteOrder() != GetByteOrder())
return false;
if (rhs.GetByteSize() == 0)
return true;
if (GetByteSize() == 0)
return (rhs.Copy(*this) > 0);
size_t bytes = GetByteSize() + rhs.GetByteSize();
DataBufferHeap *buffer_heap_ptr = nullptr;
DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
if (!buffer_sp || buffer_heap_ptr == nullptr)
return false;
uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
memcpy(bytes_ptr, GetDataStart(), GetByteSize());
memcpy(bytes_ptr + GetByteSize(), rhs.GetDataStart(), rhs.GetByteSize());
SetData(buffer_sp);
return true;
}
bool
DataExtractor::Append(void* buf, offset_t length)
{
if (buf == nullptr)
return false;
if (length == 0)
return true;
size_t bytes = GetByteSize() + length;
DataBufferHeap *buffer_heap_ptr = nullptr;
DataBufferSP buffer_sp(buffer_heap_ptr = new DataBufferHeap(bytes, 0));
if (!buffer_sp || buffer_heap_ptr == nullptr)
return false;
uint8_t* bytes_ptr = buffer_heap_ptr->GetBytes();
if (GetByteSize() > 0)
memcpy(bytes_ptr, GetDataStart(), GetByteSize());
memcpy(bytes_ptr + GetByteSize(), buf, length);
SetData(buffer_sp);
return true;
}
void
DataExtractor::Checksum (llvm::SmallVectorImpl<uint8_t> &dest,
uint64_t max_data)
{
if (max_data == 0)
max_data = GetByteSize();
else
max_data = std::min(max_data, GetByteSize());
llvm::MD5 md5;
const llvm::ArrayRef<uint8_t> data(GetDataStart(),max_data);
md5.update(data);
llvm::MD5::MD5Result result;
md5.final(result);
dest.resize(16);
std::copy(result,
result+16,
dest.begin());
}
|