Skip to content

Commit

Permalink
Integrate gdbdebughelpers tests into gate tests
Browse files Browse the repository at this point in the history
Update tests to work with graal-enterprise
Add gdbdebughelperstest with variations to enterprise debuginfotests
Abort test if GDB version is below 14, or the Python API could not be used
Pass heap base register as CGlobalData to gdb-debughelpers
  • Loading branch information
dominikmascherbauer committed Nov 19, 2024
1 parent bb028ca commit c8ad6bb
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 43 deletions.
33 changes: 4 additions & 29 deletions substratevm/debug/gdbpy/gdb-debughelpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ class SVMUtil:
compression_shift = try_or_else(lambda: int(gdb.parse_and_eval('(int)__svm_compression_shift')), 0, gdb.error)
reserved_bits_mask = try_or_else(lambda: int(gdb.parse_and_eval('(int)__svm_reserved_bits_mask')), 0, gdb.error)
object_alignment = try_or_else(lambda: int(gdb.parse_and_eval('(int)__svm_object_alignment')), 0, gdb.error)
heap_base_regnum = try_or_else(lambda: int(gdb.parse_and_eval('(int)__svm_heap_base_regnum')), 0, gdb.error)

string_type = gdb.lookup_type("java.lang.String")
enum_type = gdb.lookup_type("java.lang.Enum")
Expand All @@ -124,39 +125,13 @@ class SVMUtil:
deopt_stub_adr = 0

@classmethod
def get_architecture(cls) -> str:
def get_heap_base(cls) -> gdb.Value:
try:
arch_name = gdb.selected_frame().architecture().name()
return gdb.selected_frame().read_register(cls.heap_base_regnum)
except gdb.error:
# no frame available
arch_name = ""

if "x86-64" in arch_name:
return "amd64"
elif "aarch64" in arch_name:
return "arm64"
else:
return arch_name

@classmethod
def get_isolate_thread(cls) -> gdb.Value:
arch = cls.get_architecture()
if arch == "amd64":
return gdb.selected_frame().read_register('r15')
elif arch == "arm64":
return gdb.selected_frame().read_register('r28')
else:
return cls.null

@classmethod
def get_heap_base(cls) -> gdb.Value:
arch = cls.get_architecture()
if arch == "amd64":
return gdb.selected_frame().read_register('r14')
elif arch == "arm64":
return gdb.selected_frame().read_register('r29')
return cls.null

@classmethod
def is_null(cls, obj: gdb.Value) -> bool:
return adr(obj) == 0 or (cls.use_heap_base and adr(obj) == int(cls.get_heap_base()))
Expand Down Expand Up @@ -211,7 +186,7 @@ def get_compressed_oop(cls, obj: gdb.Value) -> int:
compressed_oop -= int(SVMUtil.get_heap_base())
assert compression_shift >= 0
compressed_oop = compressed_oop >> compression_shift
if is_hub:
if is_hub and num_reserved_bits != 0:
assert compression_shift >= 0
compressed_oop = compressed_oop << compression_shift
assert num_alignment_bits >= 0
Expand Down
18 changes: 10 additions & 8 deletions substratevm/mx.substratevm/mx_substratevm.py
Original file line number Diff line number Diff line change
Expand Up @@ -1028,18 +1028,17 @@ def build_debug_test(variant_name, image_name, extra_args):
def _gdbdebughelperstest(native_image, path, with_isolates_only, args):

# ====== check gdb version ======
# gdb-debughelperstest and this tests are designed for gdb 14 and higher with the python API enabled
# if we encounter a version lower than 14, we skip this tests
# TODO: remove when gdb 14 is available
# gdb-debughelperstests are designed for GDB 14 and higher with the GDB Python API enabled
# abort if we encounter a version lower than 14 or the GDB Python API is not available
gdb_version = mx.run([
os.environ.get('GDB_BIN', 'gdb'), '--nx', '-q',
'-ex', 'py gdb.execute("quit " + gdb.VERSION.split(".")[0])', # try to get gdb version via python
'-ex', 'quit 0' # fallback gdb exit
'-ex', 'py gdb.execute("quit " + gdb.VERSION.split(".")[0])', # try to get GDB version via Python API
'-ex', 'quit 0' # fallback GDB exit
]
, nonZeroIsFatal=False)
if gdb_version < 14:
mx.warn(f"Skipping gdb-debughelpers tests (Requires gdb version 14, found gdb version {gdb_version}).")
return
mx.abort('gdb-debughelpers test requires at least GDB version 14 with the GDB Python API enabled, ' +
('GDB Python API is not available.' if gdb_version == 0 else f'found GDB version {gdb_version}.'))
# ===============================

test_proj = mx.dependency('com.oracle.svm.test')
Expand Down Expand Up @@ -1108,6 +1107,9 @@ def run_debug_test(image_name: str, testfile: str, source_path: str, with_isolat
'-H:DebugInfoSourceSearchPath=' + source_path,
]) + extra_args

if '--shared' in extra_args:
build_args = [arg for arg in build_args if arg not in ['--libc=musl', '--static']]

if not with_isolates:
build_args += svm_experimental_options(['-H:-SpawnIsolates'])

Expand Down Expand Up @@ -1695,7 +1697,7 @@ def gdbdebughelperstest(args, config=None):
builds and tests gdb-debughelpers.py with multiple native images with debuginfo
"""
parser = ArgumentParser(prog='mx gdbdebughelperstest')
all_args = ['--output-path', '--with-isolates-only', '--build-only', '--test-only']
all_args = ['--output-path', '--with-isolates-only']
masked_args = [_mask(arg, all_args) for arg in args]
parser.add_argument(all_args[0], metavar='<output-path>', nargs=1, help='Path of the generated image', default=[join(svmbuild_dir(), "gdbdebughelperstest")])
parser.add_argument(all_args[1], action='store_true', help='Only build and test the native image with isolates')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.function.Function;
import java.util.function.Supplier;

import com.oracle.svm.core.ReservedRegisters;
import org.graalvm.nativeimage.ImageSingletons;
import org.graalvm.nativeimage.Platform;
import org.graalvm.word.PointerBase;
Expand Down Expand Up @@ -121,10 +122,12 @@ public void beforeAnalysis(BeforeAnalysisAccess access) {
CGlobalData<PointerBase> useHeapBase = CGlobalDataFactory.createWord(WordFactory.unsigned(compressEncoding.hasBase() ? 1 : 0), "__svm_use_heap_base");
CGlobalData<PointerBase> reservedBitsMask = CGlobalDataFactory.createWord(WordFactory.unsigned(Heap.getHeap().getObjectHeader().getReservedBitsMask()), "__svm_reserved_bits_mask");
CGlobalData<PointerBase> objectAlignment = CGlobalDataFactory.createWord(WordFactory.unsigned(ConfigurationValues.getObjectLayout().getAlignment()), "__svm_object_alignment");
CGlobalData<PointerBase> heapBaseRegnum = CGlobalDataFactory.createWord(WordFactory.unsigned(ReservedRegisters.singleton().getHeapBaseRegister().number), "__svm_heap_base_regnum");
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(compressionShift);
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(useHeapBase);
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(reservedBitsMask);
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(objectAlignment);
CGlobalDataFeature.singleton().registerWithGlobalHiddenSymbol(heapBaseRegnum);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,21 @@ def tearDown(self):
def test_instanceMethod_named_classloader(self):
gdb_set_breakpoint("com.oracle.svm.test.missing.classes.TestClass::instanceMethod")
gdb_continue() # named classloader is called first in test code
self.assertRegex(gdb_output("this"), rf'testClassLoader_{hex_rexp.pattern}::com\.oracle\.svm\.test\.missing\.classes\.TestClass = {{instanceField = null}}')
exec_string = gdb_output("this")
self.assertTrue(exec_string.startswith("testClassLoader_"), f"GDB output: '{exec_string}'") # check for correct class loader
self.assertIn("::com.oracle.svm.test.missing.classes.TestClass = {", exec_string) # check if TestClass has a namespace
self.assertIn("instanceField = null", exec_string)
gdb_output("$other=(('java.lang.Object' *)this)")
self.assertIn("null", gdb_advanced_print("$other.instanceField")) # force a typecast

def test_instanceMethod_unnamed_classloader(self):
gdb_set_breakpoint("com.oracle.svm.test.missing.classes.TestClass::instanceMethod")
gdb_continue() # skip named classloader
gdb_continue() # unnamed classloader is called second in test code
self.assertRegex(gdb_output("this"), rf'URLClassLoader_{hex_rexp.pattern}::com\.oracle\.svm\.test\.missing\.classes\.TestClass = {{instanceField = null}}')
exec_string = gdb_output("this")
self.assertTrue(exec_string.startswith("URLClassLoader_"), f"GDB output: '{exec_string}'") # check for correct class loader
self.assertIn("::com.oracle.svm.test.missing.classes.TestClass = {", exec_string) # check if TestClass has a namespace
self.assertIn("instanceField = null", exec_string)
gdb_output("$other=(('java.lang.Object' *)this)")
self.assertIn("null", gdb_advanced_print("$other.instanceField")) # force a typecast

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,17 @@ def test_svm_print_field_limit(self):
gdb_set_breakpoint("com.oracle.svm.test.debug.helper.PrettyPrinterTest::testObject")
gdb_run()
gdb_set_param('svm-print-field-limit', '1')
self.assertIn('f7 = "test string"', gdb_output('object'))
self.assertIn('f8 = ...', gdb_output('object'))
exec_string_1 = gdb_output('object')
self.assertTrue(exec_string_1.startswith('com.oracle.svm.test.debug.helper.PrettyPrinterTest$ExampleClass = {'))
self.assertEqual(exec_string_1.count('='), 3) # 3 '=' signs, one each for object, first field, cut off field
self.assertTrue(exec_string_1.endswith('= ...}'))
gdb_set_param('svm-print-field-limit', 'unlimited')
self.assertIn('f7 = "test string"', gdb_output('object'))
self.assertIn('f8 = Monday(0)', gdb_output('object'))
exec_string = gdb_output('object')
# check if at least 2 member fields are printed
self.assertIn('f7 = "test string"', exec_string)
self.assertIn('f8 = Monday(0)', exec_string)
self.assertNotIn('= ...', exec_string)
self.assertNotEqual(exec_string, exec_string_1)

def test_svm_print_depth_limit(self):
gdb_set_breakpoint("com.oracle.svm.test.debug.helper.PrettyPrinterTest::testObject")
Expand Down

0 comments on commit c8ad6bb

Please sign in to comment.