| 1 |
# Copyright (c) 2025, Oracle and/or its affiliates. |
| 2 |
# SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 |
import contextlib |
| 4 |
import io |
| 5 |
import struct |
| 6 |
|
| 7 |
from drgn import ( |
| 8 |
Architecture, |
| 9 |
FaultError, |
| 10 |
Platform, |
| 11 |
Program, |
| 12 |
Symbol, |
| 13 |
SymbolBinding, |
| 14 |
SymbolKind, |
| 15 |
) |
| 16 |
from drgn.commands._crash._rd import _print_memory |
| 17 |
from tests import TestCase |
| 18 |
|
| 19 |
|
| 20 |
def add_fake_memory_segment(prog, start, bytes, is_phys=False): |
| 21 |
|
| 22 |
def reader(address, count, offset, physical): |
| 23 |
if physical == is_phys: |
| 24 |
return bytes[offset : offset + count] |
| 25 |
raise FaultError("unable to read memory", address) |
| 26 |
|
| 27 |
prog.add_memory_segment(start, len(bytes), reader, physical=is_phys) |
| 28 |
|
| 29 |
|
| 30 |
class TestPrintMemory(TestCase): |
| 31 |
|
| 32 |
@classmethod |
| 33 |
def setUpClass(cls): |
| 34 |
# The only reason to set a specific architecture is to have a known word |
| 35 |
# size, which does impact alignment. The rest is not |
| 36 |
# architecture-specific. |
| 37 |
cls.prog = Program(platform=Platform(Architecture.X86_64)) |
| 38 |
mem_data = bytes(range(8)) + struct.pack("<Q", 0x12345678) + bytes(range(8, 64)) |
| 39 |
add_fake_memory_segment(cls.prog, 0xFFFF0000, mem_data) |
| 40 |
add_fake_memory_segment(cls.prog, 0x10000, b"ABCD", is_phys=True) |
| 41 |
cls.prog.register_symbol_finder( |
| 42 |
"test", |
| 43 |
lambda prog, name, address, one: ( |
| 44 |
[ |
| 45 |
Symbol( |
| 46 |
"test_sym", |
| 47 |
0x12345678, |
| 48 |
0, |
| 49 |
SymbolBinding.GLOBAL, |
| 50 |
SymbolKind.OBJECT, |
| 51 |
) |
| 52 |
] |
| 53 |
if (name == "test_sym" or (name is None and address == 0x12345678)) |
| 54 |
else [] |
| 55 |
), |
| 56 |
) |
| 57 |
cls.prog.set_enabled_symbol_finders(["test"]) |
| 58 |
|
| 59 |
def run_print_memory(self, address: int, count: int, *, unit: int, **kwargs): |
| 60 |
output = io.StringIO() |
| 61 |
physical = kwargs.pop("physical", False) |
| 62 |
memory = self.prog.read(address, count * unit, physical=physical) |
| 63 |
with contextlib.redirect_stdout(output): |
| 64 |
_print_memory(self.prog, address, memory, unit=unit, **kwargs) |
| 65 |
return output.getvalue() |
| 66 |
|
| 67 |
def test_print_memory_basic(self): |
| 68 |
result = self.run_print_memory(0xFFFF0000, 4, unit=1) |
| 69 |
self.assertEqual( |
| 70 |
result, |
| 71 |
"""\ |
| 72 |
ffff0000: 00 01 02 03 .... |
| 73 |
""", |
| 74 |
) |
| 75 |
|
| 76 |
def test_unit_2(self): |
| 77 |
result = self.run_print_memory(0xFFFF0000, 2, unit=2) |
| 78 |
self.assertEqual( |
| 79 |
result, |
| 80 |
"""\ |
| 81 |
ffff0000: 0100 0302 .... |
| 82 |
""", |
| 83 |
) |
| 84 |
|
| 85 |
def test_physical(self): |
| 86 |
result = self.run_print_memory(0x10000, 1, physical=True, unit=4) |
| 87 |
self.assertEqual( |
| 88 |
result, |
| 89 |
"""\ |
| 90 |
10000: 44434241 ABCD |
| 91 |
""", |
| 92 |
) |
| 93 |
|
| 94 |
def test_no_ascii(self): |
| 95 |
result = self.run_print_memory(0xFFFF0000, 4, unit=1, show_ascii=False) |
| 96 |
self.assertEqual( |
| 97 |
result, |
| 98 |
"""\ |
| 99 |
ffff0000: 00 01 02 03 |
| 100 |
""", |
| 101 |
) |
| 102 |
|
| 103 |
def test_annotate_symbols(self): |
| 104 |
result = self.run_print_memory( |
| 105 |
0xFFFF0008, 2, unit=8, annotate="symbols", show_ascii=False |
| 106 |
) |
| 107 |
self.assertEqual( |
| 108 |
result, |
| 109 |
"""\ |
| 110 |
ffff0008: test_sym+0 0f0e0d0c0b0a0908 |
| 111 |
""", |
| 112 |
) |
| 113 |
|
| 114 |
def test_format_decimal(self): |
| 115 |
result = self.run_print_memory(0xFFFF0000, 1, unit=4, format="d") |
| 116 |
self.assertEqual( |
| 117 |
result, |
| 118 |
"""\ |
| 119 |
ffff0000: 50462976 .... |
| 120 |
""", |
| 121 |
) |
| 122 |
|
| 123 |
def test_endian_big(self): |
| 124 |
result = self.run_print_memory(0x10000, 1, physical=True, unit=4, endian="big") |
| 125 |
self.assertEqual( |
| 126 |
result, |
| 127 |
"""\ |
| 128 |
10000: 41424344 ABCD |
| 129 |
""", |
| 130 |
) |
| 131 |
|
| 132 |
def test_annotate_wrong_unit(self): |
| 133 |
with self.assertRaises(ValueError): |
| 134 |
self.run_print_memory(0xFFFF0000, 1, unit=4, annotate="symbols") |
| 135 |
|
| 136 |
def test_annotate_wrong_format(self): |
| 137 |
with self.assertRaises(ValueError): |
| 138 |
self.run_print_memory(0xFFFF0000, 1, unit=4, format="d", annotate="symbols") |