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
|
"""
This LLDB module contains miscellaneous utilities.
Some of the test suite takes advantage of the utility functions defined here.
They can also be useful for general purpose lldb scripting.
"""
from __future__ import print_function
from __future__ import absolute_import
# System modules
import errno
import io
import os
import re
import sys
import subprocess
from typing import Dict
# LLDB modules
import lldb
from . import lldbtest_config
from . import configuration
# How often failed simulator process launches are retried.
SIMULATOR_RETRY = 3
# ===================================================
# Utilities for locating/checking executable programs
# ===================================================
def is_exe(fpath):
"""Returns True if fpath is an executable."""
return os.path.isfile(fpath) and os.access(fpath, os.X_OK)
def which(program):
"""Returns the full path to a program; None otherwise."""
fpath, fname = os.path.split(program)
if fpath:
if is_exe(program):
return program
else:
for path in os.environ["PATH"].split(os.pathsep):
exe_file = os.path.join(path, program)
if is_exe(exe_file):
return exe_file
return None
def mkdir_p(path):
try:
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
if not os.path.isdir(path):
raise OSError(errno.ENOTDIR, "%s is not a directory" % path)
# ============================
# Dealing with SDK and triples
# ============================
def get_xcode_sdk(os, env):
# Respect --apple-sdk <path> if it's specified. If the SDK is simply
# mounted from some disk image, and not actually installed, this is the
# only way to use it.
if configuration.apple_sdk:
return configuration.apple_sdk
if os == "ios":
if env == "simulator":
return "iphonesimulator"
if env == "macabi":
return "macosx"
return "iphoneos"
elif os == "tvos":
if env == "simulator":
return "appletvsimulator"
return "appletvos"
elif os == "watchos":
if env == "simulator":
return "watchsimulator"
return "watchos"
return os
def get_xcode_sdk_version(sdk):
return (
subprocess.check_output(["xcrun", "--sdk", sdk, "--show-sdk-version"])
.rstrip()
.decode("utf-8")
)
def get_xcode_sdk_root(sdk):
return (
subprocess.check_output(["xcrun", "--sdk", sdk, "--show-sdk-path"])
.rstrip()
.decode("utf-8")
)
def get_xcode_clang(sdk):
return (
subprocess.check_output(["xcrun", "-sdk", sdk, "-f", "clang"])
.rstrip()
.decode("utf-8")
)
# ===================================================
# Disassembly for an SBFunction or an SBSymbol object
# ===================================================
def disassemble(target, function_or_symbol):
"""Disassemble the function or symbol given a target.
It returns the disassembly content in a string object.
"""
buf = io.StringIO()
insts = function_or_symbol.GetInstructions(target)
for i in insts:
print(i, file=buf)
return buf.getvalue()
# ==========================================================
# Integer (byte size 1, 2, 4, and 8) to bytearray conversion
# ==========================================================
def int_to_bytearray(val, bytesize):
"""Utility function to convert an integer into a bytearray.
It returns the bytearray in the little endian format. It is easy to get the
big endian format, just do ba.reverse() on the returned object.
"""
import struct
if bytesize == 1:
return bytearray([val])
# Little endian followed by a format character.
template = "<%c"
if bytesize == 2:
fmt = template % "h"
elif bytesize == 4:
fmt = template % "i"
elif bytesize == 4:
fmt = template % "q"
else:
return None
packed = struct.pack(fmt, val)
return bytearray(packed)
def bytearray_to_int(bytes, bytesize):
"""Utility function to convert a bytearray into an integer.
It interprets the bytearray in the little endian format. For a big endian
bytearray, just do ba.reverse() on the object before passing it in.
"""
import struct
if bytesize == 1:
return bytes[0]
# Little endian followed by a format character.
template = "<%c"
if bytesize == 2:
fmt = template % "h"
elif bytesize == 4:
fmt = template % "i"
elif bytesize == 4:
fmt = template % "q"
else:
return None
unpacked = struct.unpack_from(fmt, bytes)
return unpacked[0]
# ==============================================================
# Get the description of an lldb object or None if not available
# ==============================================================
def get_description(obj, option=None):
"""Calls lldb_obj.GetDescription() and returns a string, or None.
For SBTarget, SBBreakpointLocation, and SBWatchpoint lldb objects, an extra
option can be passed in to describe the detailed level of description
desired:
o lldb.eDescriptionLevelBrief
o lldb.eDescriptionLevelFull
o lldb.eDescriptionLevelVerbose
"""
method = getattr(obj, "GetDescription")
if not method:
return None
tuple = (lldb.SBTarget, lldb.SBBreakpointLocation, lldb.SBWatchpoint)
if isinstance(obj, tuple):
if option is None:
option = lldb.eDescriptionLevelBrief
stream = lldb.SBStream()
if option is None:
success = method(stream)
else:
success = method(stream, option)
if not success:
return None
return stream.GetData()
# =================================================
# Convert some enum value to its string counterpart
# =================================================
def _enum_names(prefix: str) -> Dict[int, str]:
"""Generate a mapping of enum value to name, for the enum prefix."""
suffix_start = len(prefix)
return {
getattr(lldb, attr): attr[suffix_start:].lower()
for attr in dir(lldb)
if attr.startswith(prefix)
}
_STATE_NAMES = _enum_names(prefix="eState")
def state_type_to_str(enum: int) -> str:
"""Returns the stateType string given an enum."""
name = _STATE_NAMES.get(enum)
if name:
return name
raise Exception(f"Unknown StateType enum: {enum}")
_STOP_REASON_NAMES = _enum_names(prefix="eStopReason")
def stop_reason_to_str(enum: int) -> str:
"""Returns the stopReason string given an enum."""
name = _STOP_REASON_NAMES.get(enum)
if name:
return name
raise Exception(f"Unknown StopReason enum: {enum}")
_SYMBOL_TYPE_NAMES = _enum_names(prefix="eSymbolType")
def symbol_type_to_str(enum: int) -> str:
"""Returns the symbolType string given an enum."""
name = _SYMBOL_TYPE_NAMES.get(enum)
if name:
return name
raise Exception(f"Unknown SymbolType enum: {enum}")
_VALUE_TYPE_NAMES = _enum_names(prefix="eValueType")
def value_type_to_str(enum: int) -> str:
"""Returns the valueType string given an enum."""
name = _VALUE_TYPE_NAMES.get(enum)
if name:
return name
raise Exception(f"Unknown ValueType enum: {enum}")
# ==================================================
# Get stopped threads due to each stop reason.
# ==================================================
def sort_stopped_threads(
process,
breakpoint_threads=None,
crashed_threads=None,
watchpoint_threads=None,
signal_threads=None,
exiting_threads=None,
other_threads=None,
):
"""Fills array *_threads with threads stopped for the corresponding stop
reason.
"""
for lst in [
breakpoint_threads,
watchpoint_threads,
signal_threads,
exiting_threads,
other_threads,
]:
if lst is not None:
lst[:] = []
for thread in process:
dispatched = False
for reason, list in [
(lldb.eStopReasonBreakpoint, breakpoint_threads),
(lldb.eStopReasonException, crashed_threads),
(lldb.eStopReasonWatchpoint, watchpoint_threads),
(lldb.eStopReasonSignal, signal_threads),
(lldb.eStopReasonThreadExiting, exiting_threads),
(None, other_threads),
]:
if not dispatched and list is not None:
if thread.GetStopReason() == reason or reason is None:
list.append(thread)
dispatched = True
# ==================================================
# Utility functions for setting breakpoints
# ==================================================
def run_break_set_by_script(
test, class_name, extra_options=None, num_expected_locations=1
):
"""Set a scripted breakpoint. Check that it got the right number of locations."""
test.assertTrue(class_name is not None, "Must pass in a class name.")
command = "breakpoint set -P " + class_name
if extra_options is not None:
command += " " + extra_options
break_results = run_break_set_command(test, command)
check_breakpoint_result(test, break_results, num_locations=num_expected_locations)
return get_bpno_from_match(break_results)
def run_break_set_by_file_and_line(
test,
file_name,
line_number,
extra_options=None,
num_expected_locations=1,
loc_exact=False,
module_name=None,
):
"""Set a breakpoint by file and line, returning the breakpoint number.
If extra_options is not None, then we append it to the breakpoint set command.
If num_expected_locations is -1, we check that we got AT LEAST one location. If num_expected_locations is -2, we don't
check the actual number at all. Otherwise, we check that num_expected_locations equals the number of locations.
If loc_exact is true, we check that there is one location, and that location must be at the input file and line number.
"""
if file_name is None:
command = "breakpoint set -l %d" % (line_number)
else:
command = 'breakpoint set -f "%s" -l %d' % (file_name, line_number)
if module_name:
command += " --shlib '%s'" % (module_name)
if extra_options:
command += " " + extra_options
break_results = run_break_set_command(test, command)
if num_expected_locations == 1 and loc_exact:
check_breakpoint_result(
test,
break_results,
num_locations=num_expected_locations,
file_name=file_name,
line_number=line_number,
module_name=module_name,
)
else:
check_breakpoint_result(
test, break_results, num_locations=num_expected_locations
)
return get_bpno_from_match(break_results)
def run_break_set_by_symbol(
test,
symbol,
extra_options=None,
num_expected_locations=-1,
sym_exact=False,
module_name=None,
):
"""Set a breakpoint by symbol name. Common options are the same as run_break_set_by_file_and_line.
If sym_exact is true, then the output symbol must match the input exactly, otherwise we do a substring match.
"""
command = 'breakpoint set -n "%s"' % (symbol)
if module_name:
command += " --shlib '%s'" % (module_name)
if extra_options:
command += " " + extra_options
break_results = run_break_set_command(test, command)
if num_expected_locations == 1 and sym_exact:
check_breakpoint_result(
test,
break_results,
num_locations=num_expected_locations,
symbol_name=symbol,
module_name=module_name,
)
else:
check_breakpoint_result(
test, break_results, num_locations=num_expected_locations
)
return get_bpno_from_match(break_results)
def run_break_set_by_selector(
test, selector, extra_options=None, num_expected_locations=-1, module_name=None
):
"""Set a breakpoint by selector. Common options are the same as run_break_set_by_file_and_line."""
command = 'breakpoint set -S "%s"' % (selector)
if module_name:
command += ' --shlib "%s"' % (module_name)
if extra_options:
command += " " + extra_options
break_results = run_break_set_command(test, command)
if num_expected_locations == 1:
check_breakpoint_result(
test,
break_results,
num_locations=num_expected_locations,
symbol_name=selector,
symbol_match_exact=False,
module_name=module_name,
)
else:
check_breakpoint_result(
test, break_results, num_locations=num_expected_locations
)
return get_bpno_from_match(break_results)
def run_break_set_by_regexp(
test, regexp, extra_options=None, num_expected_locations=-1
):
"""Set a breakpoint by regular expression match on symbol name. Common options are the same as run_break_set_by_file_and_line."""
command = 'breakpoint set -r "%s"' % (regexp)
if extra_options:
command += " " + extra_options
break_results = run_break_set_command(test, command)
check_breakpoint_result(test, break_results, num_locations=num_expected_locations)
return get_bpno_from_match(break_results)
def run_break_set_by_source_regexp(
test, regexp, extra_options=None, num_expected_locations=-1
):
"""Set a breakpoint by source regular expression. Common options are the same as run_break_set_by_file_and_line."""
command = 'breakpoint set -p "%s"' % (regexp)
if extra_options:
command += " " + extra_options
break_results = run_break_set_command(test, command)
check_breakpoint_result(test, break_results, num_locations=num_expected_locations)
return get_bpno_from_match(break_results)
def run_break_set_by_file_colon_line(
test,
specifier,
path,
line_number,
column_number=0,
extra_options=None,
num_expected_locations=-1,
):
command = 'breakpoint set -y "%s"' % (specifier)
if extra_options:
command += " " + extra_options
print("About to run: '%s'", command)
break_results = run_break_set_command(test, command)
check_breakpoint_result(
test,
break_results,
num_locations=num_expected_locations,
file_name=path,
line_number=line_number,
column_number=column_number,
)
return get_bpno_from_match(break_results)
def run_break_set_by_exception(test, language, exception_typename=None):
command = "breakpoint set -E " + language
if exception_typename:
command += " --exception-typename " + exception_typename
break_results = run_break_set_command(test, command)
# No call to check_breakpoint_result as there's nothing to check. In
# particular, the exception breakpoint isn't yet guaranteed to be resolved.
return get_bpno_from_match(break_results)
def run_break_set_command(test, command):
"""Run the command passed in - it must be some break set variant - and analyze the result.
Returns a dictionary of information gleaned from the command-line results.
Will assert if the breakpoint setting fails altogether.
Dictionary will contain:
bpno - breakpoint of the newly created breakpoint, -1 on error.
num_locations - number of locations set for the breakpoint.
If there is only one location, the dictionary MAY contain:
file - source file name
line_no - source line number
column - source column number
symbol - symbol name
inline_symbol - inlined symbol name
offset - offset from the original symbol
module - module
address - address at which the breakpoint was set."""
patterns = [
r"^Breakpoint (?P<bpno>[0-9]+): (?P<num_locations>[0-9]+) locations\.$",
r"^Breakpoint (?P<bpno>[0-9]+): (?P<num_locations>no) locations \(pending\)\.",
r"^Breakpoint (?P<bpno>[0-9]+): where = (?P<module>.*)`(?P<symbol>[+\-]{0,1}[^+]+)( \+ (?P<offset>[0-9]+)){0,1}( \[inlined\] (?P<inline_symbol>.*)){0,1} at (?P<file>[^:]+):(?P<line_no>[0-9]+)(?P<column>(:[0-9]+)?), address = (?P<address>0x[0-9a-fA-F]+)$",
r"^Breakpoint (?P<bpno>[0-9]+): where = (?P<module>.*)`(?P<symbol>.*)( \+ (?P<offset>[0-9]+)){0,1}, address = (?P<address>0x[0-9a-fA-F]+)$",
]
match_object = test.match(command, patterns)
break_results = match_object.groupdict()
# We always insert the breakpoint number, setting it to -1 if we couldn't find it
# Also, make sure it gets stored as an integer.
if not "bpno" in break_results:
break_results["bpno"] = -1
else:
break_results["bpno"] = int(break_results["bpno"])
# We always insert the number of locations
# If ONE location is set for the breakpoint, then the output doesn't mention locations, but it has to be 1...
# We also make sure it is an integer.
if not "num_locations" in break_results:
num_locations = 1
else:
num_locations = break_results["num_locations"]
if num_locations == "no":
num_locations = 0
else:
num_locations = int(break_results["num_locations"])
break_results["num_locations"] = num_locations
if "line_no" in break_results:
break_results["line_no"] = int(break_results["line_no"])
return break_results
def get_bpno_from_match(break_results):
return int(break_results["bpno"])
def check_breakpoint_result(
test,
break_results,
file_name=None,
line_number=-1,
column_number=0,
symbol_name=None,
symbol_match_exact=True,
module_name=None,
offset=-1,
num_locations=-1,
):
out_num_locations = break_results["num_locations"]
if num_locations == -1:
test.assertTrue(
out_num_locations > 0, "Expecting one or more locations, got none."
)
elif num_locations != -2:
test.assertTrue(
num_locations == out_num_locations,
"Expecting %d locations, got %d." % (num_locations, out_num_locations),
)
if file_name:
out_file_name = ""
if "file" in break_results:
out_file_name = break_results["file"]
test.assertTrue(
file_name.endswith(out_file_name),
"Breakpoint file name '%s' doesn't match resultant name '%s'."
% (file_name, out_file_name),
)
if line_number != -1:
out_line_number = -1
if "line_no" in break_results:
out_line_number = break_results["line_no"]
test.assertTrue(
line_number == out_line_number,
"Breakpoint line number %s doesn't match resultant line %s."
% (line_number, out_line_number),
)
if column_number != 0:
out_column_number = 0
if "column" in break_results:
out_column_number = break_results["column"]
test.assertTrue(
column_number == out_column_number,
"Breakpoint column number %s doesn't match resultant column %s."
% (column_number, out_column_number),
)
if symbol_name:
out_symbol_name = ""
# Look first for the inlined symbol name, otherwise use the symbol
# name:
if "inline_symbol" in break_results and break_results["inline_symbol"]:
out_symbol_name = break_results["inline_symbol"]
elif "symbol" in break_results:
out_symbol_name = break_results["symbol"]
if symbol_match_exact:
test.assertTrue(
symbol_name == out_symbol_name,
"Symbol name '%s' doesn't match resultant symbol '%s'."
% (symbol_name, out_symbol_name),
)
else:
test.assertTrue(
out_symbol_name.find(symbol_name) != -1,
"Symbol name '%s' isn't in resultant symbol '%s'."
% (symbol_name, out_symbol_name),
)
if module_name:
out_module_name = None
if "module" in break_results:
out_module_name = break_results["module"]
test.assertTrue(
module_name.find(out_module_name) != -1,
"Symbol module name '%s' isn't in expected module name '%s'."
% (out_module_name, module_name),
)
def check_breakpoint(
test,
bpno,
expected_locations=None,
expected_resolved_count=None,
expected_hit_count=None,
location_id=None,
expected_location_resolved=True,
expected_location_hit_count=None,
):
"""
Test breakpoint or breakpoint location.
Breakpoint resolved count is always checked. If not specified the assumption is that all locations
should be resolved.
To test a breakpoint location, breakpoint number (bpno) and location_id must be set. In this case
the resolved count for a breakpoint is not tested by default. The location is expected to be resolved,
unless expected_location_resolved is set to False.
test - test context
bpno - breakpoint number to test
expected_locations - expected number of locations for this breakpoint. If 'None' this parameter is not tested.
expected_resolved_count - expected resolved locations number for the breakpoint. If 'None' - all locations should be resolved.
expected_hit_count - expected hit count for this breakpoint. If 'None' this parameter is not tested.
location_id - If not 'None' sets the location ID for the breakpoint to test.
expected_location_resolved - Extected resolved status for the location_id (True/False). Default - True.
expected_location_hit_count - Expected hit count for the breakpoint at location_id. Must be set if the location_id parameter is set.
"""
if isinstance(test.target, lldb.SBTarget):
target = test.target
else:
target = test.target()
bkpt = target.FindBreakpointByID(bpno)
test.assertTrue(bkpt.IsValid(), "Breakpoint is not valid.")
if expected_locations is not None:
test.assertEqual(expected_locations, bkpt.GetNumLocations())
if expected_resolved_count is not None:
test.assertEqual(expected_resolved_count, bkpt.GetNumResolvedLocations())
else:
expected_resolved_count = bkpt.GetNumLocations()
if location_id is None:
test.assertEqual(expected_resolved_count, bkpt.GetNumResolvedLocations())
if expected_hit_count is not None:
test.assertEqual(expected_hit_count, bkpt.GetHitCount())
if location_id is not None:
loc_bkpt = bkpt.FindLocationByID(location_id)
test.assertTrue(loc_bkpt.IsValid(), "Breakpoint location is not valid.")
test.assertEqual(loc_bkpt.IsResolved(), expected_location_resolved)
if expected_location_hit_count is not None:
test.assertEqual(expected_location_hit_count, loc_bkpt.GetHitCount())
# ==================================================
# Utility functions related to Threads and Processes
# ==================================================
def get_stopped_threads(process, reason):
"""Returns the thread(s) with the specified stop reason in a list.
The list can be empty if no such thread exists.
"""
threads = []
for t in process:
if t.GetStopReason() == reason:
threads.append(t)
return threads
def get_stopped_thread(process, reason):
"""A convenience function which returns the first thread with the given stop
reason or None.
Example usages:
1. Get the stopped thread due to a breakpoint condition
...
from lldbutil import get_stopped_thread
thread = get_stopped_thread(process, lldb.eStopReasonPlanComplete)
self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint condition")
...
2. Get the thread stopped due to a breakpoint
...
from lldbutil import get_stopped_thread
thread = get_stopped_thread(process, lldb.eStopReasonBreakpoint)
self.assertTrue(thread.IsValid(), "There should be a thread stopped due to breakpoint")
...
"""
threads = get_stopped_threads(process, reason)
if len(threads) == 0:
return None
return threads[0]
def get_threads_stopped_at_breakpoint_id(process, bpid):
"""For a stopped process returns the thread stopped at the breakpoint passed in bkpt"""
stopped_threads = []
threads = []
stopped_threads = get_stopped_threads(process, lldb.eStopReasonBreakpoint)
if len(stopped_threads) == 0:
return threads
for thread in stopped_threads:
# Make sure we've hit our breakpoint...
break_id = thread.GetStopReasonDataAtIndex(0)
if break_id == bpid:
threads.append(thread)
return threads
def get_threads_stopped_at_breakpoint(process, bkpt):
return get_threads_stopped_at_breakpoint_id(process, bkpt.GetID())
def get_one_thread_stopped_at_breakpoint_id(process, bpid, require_exactly_one=True):
threads = get_threads_stopped_at_breakpoint_id(process, bpid)
if len(threads) == 0:
return None
if require_exactly_one and len(threads) != 1:
return None
return threads[0]
def get_one_thread_stopped_at_breakpoint(process, bkpt, require_exactly_one=True):
return get_one_thread_stopped_at_breakpoint_id(
process, bkpt.GetID(), require_exactly_one
)
def is_thread_crashed(test, thread):
"""In the test suite we dereference a null pointer to simulate a crash. The way this is
reported depends on the platform."""
if test.platformIsDarwin():
return (
thread.GetStopReason() == lldb.eStopReasonException
and "EXC_BAD_ACCESS" in thread.GetStopDescription(100)
)
elif test.getPlatform() == "linux":
return (
thread.GetStopReason() == lldb.eStopReasonSignal
and thread.GetStopReasonDataAtIndex(0)
== thread.GetProcess().GetUnixSignals().GetSignalNumberFromName("SIGSEGV")
)
elif test.getPlatform() == "windows":
return "Exception 0xc0000005" in thread.GetStopDescription(200)
else:
return "invalid address" in thread.GetStopDescription(100)
def get_crashed_threads(test, process):
threads = []
if process.GetState() != lldb.eStateStopped:
return threads
for thread in process:
if is_thread_crashed(test, thread):
threads.append(thread)
return threads
# Helper functions for run_to_{source,name}_breakpoint:
def run_to_breakpoint_make_target(test, exe_name="a.out", in_cwd=True):
exe = test.getBuildArtifact(exe_name) if in_cwd else exe_name
# Create the target
target = test.dbg.CreateTarget(exe)
test.assertTrue(target, "Target: %s is not valid." % (exe_name))
# Set environment variables for the inferior.
if lldbtest_config.inferior_env:
test.runCmd(
"settings set target.env-vars {}".format(lldbtest_config.inferior_env)
)
return target
def run_to_breakpoint_do_run(
test, target, bkpt, launch_info=None, only_one_thread=True, extra_images=None
):
# Launch the process, and do not stop at the entry point.
if not launch_info:
launch_info = target.GetLaunchInfo()
launch_info.SetWorkingDirectory(test.get_process_working_directory())
if extra_images:
environ = test.registerSharedLibrariesWithTarget(target, extra_images)
launch_info.SetEnvironmentEntries(environ, True)
error = lldb.SBError()
process = target.Launch(launch_info, error)
# Unfortunate workaround for the iPhone simulator.
retry = SIMULATOR_RETRY
while (
retry
and error.Fail()
and error.GetCString()
and "Unable to boot the Simulator" in error.GetCString()
):
retry -= 1
print("** Simulator is unresponsive. Retrying %d more time(s)" % retry)
import time
time.sleep(60)
error = lldb.SBError()
process = target.Launch(launch_info, error)
test.assertTrue(
process,
"Could not create a valid process for %s: %s"
% (target.GetExecutable().GetFilename(), error.GetCString()),
)
test.assertFalse(error.Fail(), "Process launch failed: %s" % (error.GetCString()))
def processStateInfo(process):
info = "state: {}".format(state_type_to_str(process.state))
if process.state == lldb.eStateExited:
info += ", exit code: {}".format(process.GetExitStatus())
if process.exit_description:
info += ", exit description: '{}'".format(process.exit_description)
stdout = process.GetSTDOUT(999)
if stdout:
info += ", stdout: '{}'".format(stdout)
stderr = process.GetSTDERR(999)
if stderr:
info += ", stderr: '{}'".format(stderr)
return info
if process.state != lldb.eStateStopped:
test.fail(
"Test process is not stopped at breakpoint: {}".format(
processStateInfo(process)
)
)
# Frame #0 should be at our breakpoint.
threads = get_threads_stopped_at_breakpoint(process, bkpt)
num_threads = len(threads)
if only_one_thread:
test.assertEqual(
num_threads,
1,
"Expected 1 thread to stop at breakpoint, %d did." % (num_threads),
)
else:
test.assertGreater(num_threads, 0, "No threads stopped at breakpoint")
thread = threads[0]
return (target, process, thread, bkpt)
def run_to_name_breakpoint(
test,
bkpt_name,
launch_info=None,
exe_name="a.out",
bkpt_module=None,
in_cwd=True,
only_one_thread=True,
extra_images=None,
):
"""Start up a target, using exe_name as the executable, and run it to
a breakpoint set by name on bkpt_name restricted to bkpt_module.
If you want to pass in launch arguments or environment
variables, you can optionally pass in an SBLaunchInfo. If you
do that, remember to set the working directory as well.
If your executable isn't called a.out, you can pass that in.
And if your executable isn't in the CWD, pass in the absolute
path to the executable in exe_name, and set in_cwd to False.
If you need to restrict the breakpoint to a particular module,
pass the module name (a string not a FileSpec) in bkpt_module. If
nothing is passed in setting will be unrestricted.
If the target isn't valid, the breakpoint isn't found, or hit, the
function will cause a testsuite failure.
If successful it returns a tuple with the target process and
thread that hit the breakpoint, and the breakpoint that we set
for you.
If only_one_thread is true, we require that there be only one
thread stopped at the breakpoint. Otherwise we only require one
or more threads stop there. If there are more than one, we return
the first thread that stopped.
"""
target = run_to_breakpoint_make_target(test, exe_name, in_cwd)
breakpoint = target.BreakpointCreateByName(bkpt_name, bkpt_module)
test.assertTrue(
breakpoint.GetNumLocations() > 0,
"No locations found for name breakpoint: '%s'." % (bkpt_name),
)
return run_to_breakpoint_do_run(
test, target, breakpoint, launch_info, only_one_thread, extra_images
)
def run_to_source_breakpoint(
test,
bkpt_pattern,
source_spec,
launch_info=None,
exe_name="a.out",
bkpt_module=None,
in_cwd=True,
only_one_thread=True,
extra_images=None,
has_locations_before_run=True,
):
"""Start up a target, using exe_name as the executable, and run it to
a breakpoint set by source regex bkpt_pattern.
The rest of the behavior is the same as run_to_name_breakpoint.
"""
target = run_to_breakpoint_make_target(test, exe_name, in_cwd)
# Set the breakpoints
breakpoint = target.BreakpointCreateBySourceRegex(
bkpt_pattern, source_spec, bkpt_module
)
if has_locations_before_run:
test.assertTrue(
breakpoint.GetNumLocations() > 0,
'No locations found for source breakpoint: "%s", file: "%s", dir: "%s"'
% (bkpt_pattern, source_spec.GetFilename(), source_spec.GetDirectory()),
)
return run_to_breakpoint_do_run(
test, target, breakpoint, launch_info, only_one_thread, extra_images
)
def run_to_line_breakpoint(
test,
source_spec,
line_number,
column=0,
launch_info=None,
exe_name="a.out",
bkpt_module=None,
in_cwd=True,
only_one_thread=True,
extra_images=None,
):
"""Start up a target, using exe_name as the executable, and run it to
a breakpoint set by (source_spec, line_number(, column)).
The rest of the behavior is the same as run_to_name_breakpoint.
"""
target = run_to_breakpoint_make_target(test, exe_name, in_cwd)
# Set the breakpoints
breakpoint = target.BreakpointCreateByLocation(
source_spec, line_number, column, 0, lldb.SBFileSpecList()
)
test.assertTrue(
breakpoint.GetNumLocations() > 0,
'No locations found for line breakpoint: "%s:%d(:%d)", dir: "%s"'
% (source_spec.GetFilename(), line_number, column, source_spec.GetDirectory()),
)
return run_to_breakpoint_do_run(
test, target, breakpoint, launch_info, only_one_thread, extra_images
)
def continue_to_breakpoint(process, bkpt):
"""Continues the process, if it stops, returns the threads stopped at bkpt; otherwise, returns None"""
process.Continue()
if process.GetState() != lldb.eStateStopped:
return None
else:
return get_threads_stopped_at_breakpoint(process, bkpt)
def continue_to_source_breakpoint(test, process, bkpt_pattern, source_spec):
"""
Sets a breakpoint set by source regex bkpt_pattern, continues the process, and deletes the breakpoint again.
Otherwise the same as `continue_to_breakpoint`
"""
breakpoint = process.target.BreakpointCreateBySourceRegex(
bkpt_pattern, source_spec, None
)
test.assertTrue(
breakpoint.GetNumLocations() > 0,
'No locations found for source breakpoint: "%s", file: "%s", dir: "%s"'
% (bkpt_pattern, source_spec.GetFilename(), source_spec.GetDirectory()),
)
stopped_threads = continue_to_breakpoint(process, breakpoint)
process.target.BreakpointDelete(breakpoint.GetID())
return stopped_threads
def get_caller_symbol(thread):
"""
Returns the symbol name for the call site of the leaf function.
"""
depth = thread.GetNumFrames()
if depth <= 1:
return None
caller = thread.GetFrameAtIndex(1).GetSymbol()
if caller:
return caller.GetName()
else:
return None
def get_function_names(thread):
"""
Returns a sequence of function names from the stack frames of this thread.
"""
def GetFuncName(i):
return thread.GetFrameAtIndex(i).GetFunctionName()
return list(map(GetFuncName, list(range(thread.GetNumFrames()))))
def get_symbol_names(thread):
"""
Returns a sequence of symbols for this thread.
"""
def GetSymbol(i):
return thread.GetFrameAtIndex(i).GetSymbol().GetName()
return list(map(GetSymbol, list(range(thread.GetNumFrames()))))
def get_pc_addresses(thread):
"""
Returns a sequence of pc addresses for this thread.
"""
def GetPCAddress(i):
return thread.GetFrameAtIndex(i).GetPCAddress()
return list(map(GetPCAddress, list(range(thread.GetNumFrames()))))
def get_filenames(thread):
"""
Returns a sequence of file names from the stack frames of this thread.
"""
def GetFilename(i):
return thread.GetFrameAtIndex(i).GetLineEntry().GetFileSpec().GetFilename()
return list(map(GetFilename, list(range(thread.GetNumFrames()))))
def get_line_numbers(thread):
"""
Returns a sequence of line numbers from the stack frames of this thread.
"""
def GetLineNumber(i):
return thread.GetFrameAtIndex(i).GetLineEntry().GetLine()
return list(map(GetLineNumber, list(range(thread.GetNumFrames()))))
def get_module_names(thread):
"""
Returns a sequence of module names from the stack frames of this thread.
"""
def GetModuleName(i):
return thread.GetFrameAtIndex(i).GetModule().GetFileSpec().GetFilename()
return list(map(GetModuleName, list(range(thread.GetNumFrames()))))
def get_stack_frames(thread):
"""
Returns a sequence of stack frames for this thread.
"""
def GetStackFrame(i):
return thread.GetFrameAtIndex(i)
return list(map(GetStackFrame, list(range(thread.GetNumFrames()))))
def print_stacktrace(thread, string_buffer=False):
"""Prints a simple stack trace of this thread."""
output = io.StringIO() if string_buffer else sys.stdout
target = thread.GetProcess().GetTarget()
depth = thread.GetNumFrames()
mods = get_module_names(thread)
funcs = get_function_names(thread)
symbols = get_symbol_names(thread)
files = get_filenames(thread)
lines = get_line_numbers(thread)
addrs = get_pc_addresses(thread)
if thread.GetStopReason() != lldb.eStopReasonInvalid:
desc = "stop reason=" + stop_reason_to_str(thread.GetStopReason())
else:
desc = ""
print(
"Stack trace for thread id={0:#x} name={1} queue={2} ".format(
thread.GetThreadID(), thread.GetName(), thread.GetQueueName()
)
+ desc,
file=output,
)
for i in range(depth):
frame = thread.GetFrameAtIndex(i)
function = frame.GetFunction()
load_addr = addrs[i].GetLoadAddress(target)
if not function:
file_addr = addrs[i].GetFileAddress()
start_addr = frame.GetSymbol().GetStartAddress().GetFileAddress()
symbol_offset = file_addr - start_addr
print(
" frame #{num}: {addr:#016x} {mod}`{symbol} + {offset}".format(
num=i,
addr=load_addr,
mod=mods[i],
symbol=symbols[i],
offset=symbol_offset,
),
file=output,
)
else:
print(
" frame #{num}: {addr:#016x} {mod}`{func} at {file}:{line} {args}".format(
num=i,
addr=load_addr,
mod=mods[i],
func="%s [inlined]" % funcs[i] if frame.IsInlined() else funcs[i],
file=files[i],
line=lines[i],
args=get_args_as_string(frame, showFuncName=False)
if not frame.IsInlined()
else "()",
),
file=output,
)
if string_buffer:
return output.getvalue()
def print_stacktraces(process, string_buffer=False):
"""Prints the stack traces of all the threads."""
output = io.StringIO() if string_buffer else sys.stdout
print("Stack traces for " + str(process), file=output)
for thread in process:
print(print_stacktrace(thread, string_buffer=True), file=output)
if string_buffer:
return output.getvalue()
def expect_state_changes(test, listener, process, states, timeout=30):
"""Listens for state changed events on the listener and makes sure they match what we
expect. Stop-and-restart events (where GetRestartedFromEvent() returns true) are ignored.
"""
for expected_state in states:
def get_next_event():
event = lldb.SBEvent()
if not listener.WaitForEventForBroadcasterWithType(
timeout,
process.GetBroadcaster(),
lldb.SBProcess.eBroadcastBitStateChanged,
event,
):
test.fail(
"Timed out while waiting for a transition to state %s"
% lldb.SBDebugger.StateAsCString(expected_state)
)
return event
event = get_next_event()
while lldb.SBProcess.GetStateFromEvent(
event
) == lldb.eStateStopped and lldb.SBProcess.GetRestartedFromEvent(event):
# Ignore restarted event and the subsequent running event.
event = get_next_event()
test.assertEqual(
lldb.SBProcess.GetStateFromEvent(event),
lldb.eStateRunning,
"Restarted event followed by a running event",
)
event = get_next_event()
test.assertEqual(lldb.SBProcess.GetStateFromEvent(event), expected_state)
def start_listening_from(broadcaster, event_mask):
"""Creates a listener for a specific event mask and add it to the source broadcaster."""
listener = lldb.SBListener("lldb.test.listener")
broadcaster.AddListener(listener, event_mask)
return listener
def fetch_next_event(test, listener, broadcaster, match_class=False, timeout=10):
"""Fetch one event from the listener and return it if it matches the provided broadcaster.
If `match_class` is set to True, this will match an event with an entire broadcaster class.
Fails otherwise."""
event = lldb.SBEvent()
if listener.WaitForEvent(timeout, event):
if match_class:
if event.GetBroadcasterClass() == broadcaster:
return event
else:
if event.BroadcasterMatchesRef(broadcaster):
return event
stream = lldb.SBStream()
event.GetDescription(stream)
test.fail(
"received event '%s' from unexpected broadcaster '%s'."
% (stream.GetData(), event.GetBroadcaster().GetName())
)
test.fail("couldn't fetch an event before reaching the timeout.")
# ===================================
# Utility functions related to Frames
# ===================================
def get_parent_frame(frame):
"""
Returns the parent frame of the input frame object; None if not available.
"""
thread = frame.GetThread()
parent_found = False
for f in thread:
if parent_found:
return f
if f.GetFrameID() == frame.GetFrameID():
parent_found = True
# If we reach here, no parent has been found, return None.
return None
def get_args_as_string(frame, showFuncName=True):
"""
Returns the args of the input frame object as a string.
"""
# arguments => True
# locals => False
# statics => False
# in_scope_only => True
vars = frame.GetVariables(True, False, False, True) # type of SBValueList
args = [] # list of strings
for var in vars:
args.append("(%s)%s=%s" % (var.GetTypeName(), var.GetName(), var.GetValue()))
if frame.GetFunction():
name = frame.GetFunction().GetName()
elif frame.GetSymbol():
name = frame.GetSymbol().GetName()
else:
name = ""
if showFuncName:
return "%s(%s)" % (name, ", ".join(args))
else:
return "(%s)" % (", ".join(args))
def print_registers(frame, string_buffer=False):
"""Prints all the register sets of the frame."""
output = io.StringIO() if string_buffer else sys.stdout
print("Register sets for " + str(frame), file=output)
registerSet = frame.GetRegisters() # Return type of SBValueList.
print(
"Frame registers (size of register set = %d):" % registerSet.GetSize(),
file=output,
)
for value in registerSet:
# print(value, file=output)
print(
"%s (number of children = %d):" % (value.GetName(), value.GetNumChildren()),
file=output,
)
for child in value:
print(
"Name: %s, Value: %s" % (child.GetName(), child.GetValue()), file=output
)
if string_buffer:
return output.getvalue()
def get_registers(frame, kind):
"""Returns the registers given the frame and the kind of registers desired.
Returns None if there's no such kind.
"""
registerSet = frame.GetRegisters() # Return type of SBValueList.
for value in registerSet:
if kind.lower() in value.GetName().lower():
return value
return None
def get_GPRs(frame):
"""Returns the general purpose registers of the frame as an SBValue.
The returned SBValue object is iterable. An example:
...
from lldbutil import get_GPRs
regs = get_GPRs(frame)
for reg in regs:
print("%s => %s" % (reg.GetName(), reg.GetValue()))
...
"""
return get_registers(frame, "general purpose")
def get_FPRs(frame):
"""Returns the floating point registers of the frame as an SBValue.
The returned SBValue object is iterable. An example:
...
from lldbutil import get_FPRs
regs = get_FPRs(frame)
for reg in regs:
print("%s => %s" % (reg.GetName(), reg.GetValue()))
...
"""
return get_registers(frame, "floating point")
def get_ESRs(frame):
"""Returns the exception state registers of the frame as an SBValue.
The returned SBValue object is iterable. An example:
...
from lldbutil import get_ESRs
regs = get_ESRs(frame)
for reg in regs:
print("%s => %s" % (reg.GetName(), reg.GetValue()))
...
"""
return get_registers(frame, "exception state")
# ======================================
# Utility classes/functions for SBValues
# ======================================
class BasicFormatter(object):
"""The basic formatter inspects the value object and prints the value."""
def format(self, value, buffer=None, indent=0):
if not buffer:
output = io.StringIO()
else:
output = buffer
# If there is a summary, it suffices.
val = value.GetSummary()
# Otherwise, get the value.
if val is None:
val = value.GetValue()
if val is None and value.GetNumChildren() > 0:
val = "%s (location)" % value.GetLocation()
print(
"{indentation}({type}) {name} = {value}".format(
indentation=" " * indent,
type=value.GetTypeName(),
name=value.GetName(),
value=val,
),
file=output,
)
return output.getvalue()
class ChildVisitingFormatter(BasicFormatter):
"""The child visiting formatter prints the value and its immediate children.
The constructor takes a keyword arg: indent_child, which defaults to 2.
"""
def __init__(self, indent_child=2):
"""Default indentation of 2 SPC's for the children."""
self.cindent = indent_child
def format(self, value, buffer=None):
if not buffer:
output = io.StringIO()
else:
output = buffer
BasicFormatter.format(self, value, buffer=output)
for child in value:
BasicFormatter.format(self, child, buffer=output, indent=self.cindent)
return output.getvalue()
class RecursiveDecentFormatter(BasicFormatter):
"""The recursive decent formatter prints the value and the decendents.
The constructor takes two keyword args: indent_level, which defaults to 0,
and indent_child, which defaults to 2. The current indentation level is
determined by indent_level, while the immediate children has an additional
indentation by inden_child.
"""
def __init__(self, indent_level=0, indent_child=2):
self.lindent = indent_level
self.cindent = indent_child
def format(self, value, buffer=None):
if not buffer:
output = io.StringIO()
else:
output = buffer
BasicFormatter.format(self, value, buffer=output, indent=self.lindent)
new_indent = self.lindent + self.cindent
for child in value:
if child.GetSummary() is not None:
BasicFormatter.format(self, child, buffer=output, indent=new_indent)
else:
if child.GetNumChildren() > 0:
rdf = RecursiveDecentFormatter(indent_level=new_indent)
rdf.format(child, buffer=output)
else:
BasicFormatter.format(self, child, buffer=output, indent=new_indent)
return output.getvalue()
def check_expression(test, frame, expression, expected_result, use_summary=True):
"""Asserts that the result of evaluating the given expression gives the passed expected result"""
value = frame.EvaluateExpression(expression)
test.assertTrue(value.IsValid(), expression + "returned a valid value")
answer = value.GetSummary() if use_summary else value.GetValue()
report_str = "%s expected: %s got: %s" % (expression, expected_result, answer)
test.assertTrue(answer == expected_result, report_str)
def check_variable(
test,
valobj,
use_dynamic=False,
summary=None,
value=None,
typename=None,
num_children=None,
use_synthetic=True,
):
name = valobj.GetName()
test.assertTrue(
valobj.IsValid(), "variable %s is not valid" % (name if valobj else "<unknown>")
)
if use_dynamic:
valobj = valobj.GetDynamicValue(lldb.eDynamicCanRunTarget)
test.assertTrue(
valobj.IsValid(),
"dynamic value of %s is not valid" % (name if valobj else "<unknown>"),
)
test.assertTrue(
valobj.IsDynamic(),
"dynamic value of %s is not dynamic" % (name if valobj else "<unknown>"),
)
if use_synthetic:
valobj.SetPreferSyntheticValue(True)
if summary:
valobj_summary = valobj.GetSummary()
test.assertTrue(
valobj_summary == summary,
"expected summary: '%s' - actual summary: '%s'"
% (summary, valobj_summary if valobj else "<unknown>"),
)
if value:
valobj_value = valobj.GetValue()
test.assertTrue(
valobj_value == value,
"expected value: '%s' - actual value: '%s'"
% (value, valobj_value if valobj else "<unknown>"),
)
if typename:
valobj_typename = valobj.GetTypeName()
test.assertTrue(
valobj_typename == typename,
"expected typename: '%s' - actual typename: '%s'"
% (typename, valobj_typename if valobj else "<unknown>"),
)
if num_children:
valobj_num_children = valobj.GetNumChildren()
test.assertTrue(
valobj_num_children == num_children,
"expected num children: '%s' - actual num children: '%s'"
% (num_children, valobj_num_children if valobj else "<unknown>"),
)
def check_children(test, valobj, thecallable):
test.assertTrue(
valobj.IsValid(),
"variable %s is not valid" % (valobj.GetName() if valobj else "<unknown>"),
)
i = 0
while i < valobj.GetNumChildren():
child = valobj.GetChildAtIndex(i)
test.assertTrue(thecallable(child, i), "child %d failed the test" % (i))
i = i + 1
# ===========================================================
# Utility functions for path manipulation on remote platforms
# ===========================================================
def join_remote_paths(*paths):
# TODO: update with actual platform name for remote windows once it exists
if lldb.remote_platform.GetName() == "remote-windows":
return os.path.join(*paths).replace(os.path.sep, "\\")
return os.path.join(*paths).replace(os.path.sep, "/")
def append_to_process_working_directory(test, *paths):
remote = lldb.remote_platform
if remote:
return join_remote_paths(remote.GetWorkingDirectory(), *paths)
return os.path.join(test.getBuildDir(), *paths)
# ==================================================
# Utility functions to get the correct signal number
# ==================================================
import signal
def get_signal_number(signal_name):
platform = lldb.remote_platform
if platform and platform.IsValid():
signals = platform.GetUnixSignals()
if signals.IsValid():
signal_number = signals.GetSignalNumberFromName(signal_name)
if signal_number > 0:
return signal_number
# No remote platform; fall back to using local python signals.
return getattr(signal, signal_name)
def get_actions_for_signal(
testcase, signal_name, from_target=False, expected_absent=False
):
"""Returns a triple of (pass, stop, notify)"""
return_obj = lldb.SBCommandReturnObject()
command = "process handle {0}".format(signal_name)
if from_target:
command += " -t"
testcase.dbg.GetCommandInterpreter().HandleCommand(command, return_obj)
match = re.match(
"NAME *PASS *STOP *NOTIFY.*(false|true|not set) *(false|true|not set) *(false|true|not set)",
return_obj.GetOutput(),
re.IGNORECASE | re.DOTALL,
)
if match and expected_absent:
testcase.fail('Signal "{0}" was supposed to be absent'.format(signal_name))
if not match:
if expected_absent:
return (None, None, None)
testcase.fail("Unable to retrieve default signal disposition.")
return (match.group(1), match.group(2), match.group(3))
def set_actions_for_signal(
testcase, signal_name, pass_action, stop_action, notify_action, expect_success=True
):
return_obj = lldb.SBCommandReturnObject()
command = "process handle {0}".format(signal_name)
if pass_action != None:
command += " -p {0}".format(pass_action)
if stop_action != None:
command += " -s {0}".format(stop_action)
if notify_action != None:
command += " -n {0}".format(notify_action)
testcase.dbg.GetCommandInterpreter().HandleCommand(command, return_obj)
testcase.assertEqual(
expect_success,
return_obj.Succeeded(),
"Setting signal handling for {0} worked as expected".format(signal_name),
)
class PrintableRegex(object):
def __init__(self, text):
self.regex = re.compile(text)
self.text = text
def match(self, str):
return self.regex.match(str)
def __str__(self):
return "%s" % (self.text)
def __repr__(self):
return "re.compile(%s) -> %s" % (self.text, self.regex)
def skip_if_callable(test, mycallable, reason):
if callable(mycallable):
if mycallable(test):
test.skipTest(reason)
return True
return False
def skip_if_library_missing(test, target, library):
def find_library(target, library):
for module in target.modules:
filename = module.file.GetFilename()
if isinstance(library, str):
if library == filename:
return False
elif hasattr(library, "match"):
if library.match(filename):
return False
return True
def find_library_callable(test):
return find_library(target, library)
return skip_if_callable(
test,
find_library_callable,
"could not find library matching '%s' in target %s" % (library, target),
)
def read_file_on_target(test, remote):
if lldb.remote_platform:
local = test.getBuildArtifact("file_from_target")
error = lldb.remote_platform.Get(
lldb.SBFileSpec(remote, False), lldb.SBFileSpec(local, True)
)
test.assertTrue(
error.Success(), "Reading file {0} failed: {1}".format(remote, error)
)
else:
local = remote
with open(local, "r") as f:
return f.read()
def read_file_from_process_wd(test, name):
path = append_to_process_working_directory(test, name)
return read_file_on_target(test, path)
def wait_for_file_on_target(testcase, file_path, max_attempts=6):
for i in range(max_attempts):
err, retcode, msg = testcase.run_platform_command("ls %s" % file_path)
if err.Success() and retcode == 0:
break
if i < max_attempts:
# Exponential backoff!
import time
time.sleep(pow(2, i) * 0.25)
else:
testcase.fail(
"File %s not found even after %d attempts." % (file_path, max_attempts)
)
return read_file_on_target(testcase, file_path)
def packetlog_get_process_info(log):
"""parse a gdb-remote packet log file and extract the response to qProcessInfo"""
process_info = dict()
with open(log, "r") as logfile:
process_info_ostype = None
expect_process_info_response = False
for line in logfile:
if expect_process_info_response:
for pair in line.split(";"):
keyval = pair.split(":")
if len(keyval) == 2:
process_info[keyval[0]] = keyval[1]
break
if "send packet: $qProcessInfo#" in line:
expect_process_info_response = True
return process_info
def packetlog_get_dylib_info(log):
"""parse a gdb-remote packet log file and extract the *last* complete
(=> fetch_all_solibs=true) response to jGetLoadedDynamicLibrariesInfos"""
import json
dylib_info = None
with open(log, "r") as logfile:
dylib_info = None
expect_dylib_info_response = False
for line in logfile:
if expect_dylib_info_response:
while line[0] != "$":
line = line[1:]
line = line[1:]
# Unescape '}'.
dylib_info = json.loads(line.replace("}]", "}")[:-4])
expect_dylib_info_response = False
if (
'send packet: $jGetLoadedDynamicLibrariesInfos:{"fetch_all_solibs":true}'
in line
):
expect_dylib_info_response = True
return dylib_info
|