| 1 |
import os |
| 2 |
import sys |
| 3 |
import pytest |
| 4 |
import subprocess |
| 5 |
import ctypes |
| 6 |
|
| 7 |
try: |
| 8 |
import __pypy__ |
| 9 |
except ImportError: |
| 10 |
pytestmark = pytest.mark.skip('can only run these tests on pypy') |
| 11 |
|
| 12 |
if sys.platform.startswith('linux'): |
| 13 |
import _pypy_remote_debug |
| 14 |
import _vmprof |
| 15 |
else: |
| 16 |
pytestmark = pytest.mark.skip('only works on linux so far') |
| 17 |
_vmprof = None |
| 18 |
|
| 19 |
def test_parse_maps(): |
| 20 |
maps = _pypy_remote_debug._read_and_parse_maps('self', sys.executable) |
| 21 |
assert os.path.realpath(sys.executable) == maps[0]['file'] |
| 22 |
|
| 23 |
def test_elf_find_symbol(): |
| 24 |
pid = os.getpid() |
| 25 |
file, base_addr = _pypy_remote_debug._find_file_and_base_addr(pid) |
| 26 |
with open(file, 'rb') as f: |
| 27 |
value = _pypy_remote_debug.elf_find_symbol(f, b'pypysig_counter') |
| 28 |
|
| 29 |
out = subprocess.check_output(['nm', file]) |
| 30 |
if not out: |
| 31 |
pytest.skip("test can't work on stripped binary") |
| 32 |
for line in out.decode('ascii').splitlines(): |
| 33 |
if 'pypysig_counter' in line: |
| 34 |
addr, _, _ = line.split() |
| 35 |
assert int(addr, 16) == value |
| 36 |
break |
| 37 |
else: |
| 38 |
assert False |
| 39 |
assert value |
| 40 |
|
| 41 |
def test_elf_read_first_load_section(): |
| 42 |
pid = os.getpid() |
| 43 |
file, base_addr = _pypy_remote_debug._find_file_and_base_addr(pid) |
| 44 |
with open(file, 'rb') as f: |
| 45 |
phdr = _pypy_remote_debug.elf_read_first_load_section(f) |
| 46 |
|
| 47 |
|
| 48 |
out = subprocess.check_output(['objdump', '-p', file]) |
| 49 |
for line in out.decode('ascii').splitlines(): |
| 50 |
if 'LOAD' in line: |
| 51 |
outline = line |
| 52 |
break |
| 53 |
content = outline.split()[1:] |
| 54 |
for name, value in zip(content[::2], content[1::2]): |
| 55 |
if name == 'vaddr': |
| 56 |
assert int(value, 16) == phdr.vaddr |
| 57 |
|
| 58 |
def skip_on_oserror(func): |
| 59 |
code = func.__code__ |
| 60 |
def check_error(e): |
| 61 |
if "Operation not permitted" in str(e): |
| 62 |
pytest.skip('yama ptrace_scope likely forbids read_memory call') |
| 63 |
if code.co_varnames[:code.co_argcount] == ('tmpdir', ): |
| 64 |
def wrapper(tmpdir): |
| 65 |
try: |
| 66 |
return func(tmpdir) |
| 67 |
except OSError as e: |
| 68 |
check_error(e) |
| 69 |
if "Operation not permitted" in str(e): |
| 70 |
pytest.skip('yama ptrace_scope likely forbids read_memory call') |
| 71 |
else: |
| 72 |
assert code.co_argcount == 0 |
| 73 |
def wrapper(): |
| 74 |
try: |
| 75 |
return func() |
| 76 |
except OSError as e: |
| 77 |
check_error(e) |
| 78 |
return wrapper |
| 79 |
|
| 80 |
@skip_on_oserror |
| 81 |
def test_read_memory(): |
| 82 |
|
| 83 |
ffi = _pypy_remote_debug.ffi |
| 84 |
pid = os.getpid() |
| 85 |
data = b'hello, world!' |
| 86 |
sourcebuffer = ffi.new('char[]', len(data)) |
| 87 |
for i in range(len(data)): |
| 88 |
sourcebuffer[i] = data[i:i+1] |
| 89 |
result = _pypy_remote_debug.read_memory(pid, int(ffi.cast('intptr_t', sourcebuffer)), len(data)) |
| 90 |
assert result == data |
| 91 |
|
| 92 |
@skip_on_oserror |
| 93 |
def test_write_memory(): |
| 94 |
|
| 95 |
ffi = _pypy_remote_debug.ffi |
| 96 |
pid = os.getpid() |
| 97 |
data = b'hello, world!' |
| 98 |
targetbuffer = ffi.new('char[]', len(data)) |
| 99 |
result = _pypy_remote_debug.write_memory(pid, int(ffi.cast('intptr_t', targetbuffer)), data) |
| 100 |
assert ffi.buffer(targetbuffer)[:] == data |
| 101 |
|
| 102 |
@skip_on_oserror |
| 103 |
def test_cookie(): |
| 104 |
pid = os.getpid() |
| 105 |
addr = _pypy_remote_debug.compute_remote_addr(pid) |
| 106 |
cookie = _pypy_remote_debug.read_memory(pid, addr + _pypy_remote_debug.COOKIE_OFFSET, 8) |
| 107 |
assert cookie == b'pypysigs' |
| 108 |
|
| 109 |
def test_remote_find_file_and_base_addr(): |
| 110 |
code = """ |
| 111 |
import sys |
| 112 |
sys.stdin.readline() |
| 113 |
""" |
| 114 |
out = subprocess.Popen([sys.executable, '-c', |
| 115 |
code], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
| 116 |
pid = out.pid |
| 117 |
file, base_addr = _pypy_remote_debug._find_file_and_base_addr(pid) |
| 118 |
assert file == sys.executable or 'libpypy' in file |
| 119 |
out.stdin.write(b'1\n') |
| 120 |
out.stdin.flush() |
| 121 |
out.wait() |
| 122 |
|
| 123 |
@skip_on_oserror |
| 124 |
def test_integration(tmpdir): |
| 125 |
import __pypy__ |
| 126 |
code = """ |
| 127 |
import time |
| 128 |
for i in range(10): |
| 129 |
time.sleep(0.1) |
| 130 |
print("done") |
| 131 |
""" |
| 132 |
debug_code = rb""" |
| 133 |
import sys, os |
| 134 |
sys.stdout.write('hello from %s\n' % os.getpid()) |
| 135 |
sys.stdout.flush() |
| 136 |
""" |
| 137 |
debug_script = tmpdir.join('debug.py') |
| 138 |
debug_script.write(debug_code) |
| 139 |
for func in (_pypy_remote_debug.start_debugger, __pypy__.remote_exec): |
| 140 |
out = subprocess.Popen([sys.executable, '-c', |
| 141 |
code], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
| 142 |
pid = out.pid |
| 143 |
func(pid, str(debug_script).encode('utf-8')) |
| 144 |
l = out.stdout.readline() |
| 145 |
assert l == b'Executing remote debugger script %s\n' % str(debug_script).encode('utf-8') |
| 146 |
l = out.stdout.readline() |
| 147 |
assert l == ('hello from %s\n' % pid).encode('ascii') |
| 148 |
exitcode = out.wait() |
| 149 |
assert exitcode == 0 |
| 150 |
|
| 151 |
@skip_on_oserror |
| 152 |
def test_disable_remote_debug(tmpdir): |
| 153 |
import __pypy__ |
| 154 |
code = """ |
| 155 |
import time |
| 156 |
for i in range(10): |
| 157 |
time.sleep(0.1) |
| 158 |
print("done") |
| 159 |
""" |
| 160 |
debug_code = rb""" |
| 161 |
import sys, os |
| 162 |
sys.stdout.write('hello from %s\n' % os.getpid()) |
| 163 |
sys.stdout.flush() |
| 164 |
""" |
| 165 |
debug_script = tmpdir.join('debug.py') |
| 166 |
debug_script.write(debug_code) |
| 167 |
for func in (_pypy_remote_debug.start_debugger, __pypy__.remote_exec): |
| 168 |
|
| 169 |
out = subprocess.Popen([sys.executable, '-X', 'disable-remote-debug', '-c', |
| 170 |
code], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) |
| 171 |
pid = out.pid |
| 172 |
func(pid, str(debug_script).encode('utf-8')) |
| 173 |
l = out.stdout.readline() |
| 174 |
assert l == b'done\n' |
| 175 |
|
| 176 |
|
| 177 |
env = os.environ.copy() |
| 178 |
env['PYTHON_DISABLE_REMOTE_DEBUG'] = 'true' |
| 179 |
out = subprocess.Popen([sys.executable, '-c', |
| 180 |
code], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE, |
| 181 |
env=env) |
| 182 |
pid = out.pid |
| 183 |
func(pid, str(debug_script).encode('utf-8')) |
| 184 |
l = out.stdout.readline() |
| 185 |
assert l == b'done\n' |
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
def load_so_or_skip(name): |
| 192 |
try: |
| 193 |
return ctypes.CDLL(name) |
| 194 |
except OSError: |
| 195 |
pytest.skip("couldn't find %s" % name) |
| 196 |
|
| 197 |
|
| 198 |
def test_proc_map_find_base_map(): |
| 199 |
pid = os.getpid() |
| 200 |
so = load_so_or_skip('libexpat.so') |
| 201 |
address_of_function = (ctypes.cast(so.XML_Parse, ctypes.c_void_p)).value |
| 202 |
map = _pypy_remote_debug._proc_map_find_base_map(address_of_function) |
| 203 |
assert 'libexpat.so' in map['file'] |
| 204 |
|
| 205 |
def test_proc_map_find_base_map_bug(): |
| 206 |
|
| 207 |
|
| 208 |
s = """\ |
| 209 |
eb284000-eb285000 rw-p 00000000 00:00 0 |
| 210 |
eb285000-eb286000 r-xp 01a80000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 211 |
eb286000-eb287000 rw-p 00000000 00:00 0 |
| 212 |
eb287000-eb288000 r--p 00000000 103:05 8993868 /home/user/pypy/lib_pypy/_pypy_util_cffi_inner.pypy-73.so |
| 213 |
eb288000-eb289000 r-xp 00001000 103:05 8993868 /home/user/pypy/lib_pypy/_pypy_util_cffi_inner.pypy-73.so |
| 214 |
eb289000-eb28a000 r--p 00002000 103:05 8993868 /home/user/pypy/lib_pypy/_pypy_util_cffi_inner.pypy-73.so |
| 215 |
eb28a000-eb28b000 r--p 00002000 103:05 8993868 /home/user/pypy/lib_pypy/_pypy_util_cffi_inner.pypy-73.so |
| 216 |
eb28b000-eb28c000 rw-p 00003000 103:05 8993868 /home/user/pypy/lib_pypy/_pypy_util_cffi_inner.pypy-73.so |
| 217 |
eb28c000-eb28e000 r--p 00570000 103:05 65313846 /usr/lib/locale/locale-archive |
| 218 |
eb28e000-eb8f6000 r--p 00000000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 219 |
eb8f6000-ecd10000 r-xp 00668000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 220 |
ecd10000-ed32e000 r--p 01a82000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 221 |
ed32e000-ed32f000 ---p 020a0000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 222 |
ed32f000-ed33d000 r--p 020a0000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 223 |
ed33d000-ede21000 rw-p 020ae000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 224 |
ede21000-ede3f000 rw-p 00000000 00:00 0 |
| 225 |
ede3f000-ede47000 rw-p 02b92000 103:05 8993453 /home/user/pypy/bin/libpypy-c.so |
| 226 |
""" |
| 227 |
maps = _pypy_remote_debug._parse_maps(lineiter=iter(s.splitlines())) |
| 228 |
map = _pypy_remote_debug._proc_map_find_base_map(0xeb28f000, maps=maps) |
| 229 |
assert map['file'] == '/home/user/pypy/bin/libpypy-c.so' |
| 230 |
assert map['from_'] == 0xeb28e000 |
| 231 |
assert map['file_offset'] == 0 |
| 232 |
|
| 233 |
def test_symbolify(): |
| 234 |
pid = os.getpid() |
| 235 |
so = load_so_or_skip('libexpat.so') |
| 236 |
address_of_function = (ctypes.cast(so.XML_Parse, ctypes.c_void_p)).value |
| 237 |
name, filename = _pypy_remote_debug._symbolify(address_of_function) |
| 238 |
assert name == b'XML_Parse' |
| 239 |
assert 'libexpat.so' in filename |
| 240 |
|
| 241 |
def test_symbolify_all(): |
| 242 |
pid = os.getpid() |
| 243 |
so = load_so_or_skip('libexpat.so') |
| 244 |
names = ['XML_Parse', 'XML_GetBase'] |
| 245 |
all = [] |
| 246 |
for name in names: |
| 247 |
address_of_function = (ctypes.cast(getattr(so, name), ctypes.c_void_p)).value |
| 248 |
all.append(address_of_function) |
| 249 |
all.append(1) |
| 250 |
res = _pypy_remote_debug._symbolify_all(all) |
| 251 |
for index, name in enumerate(names): |
| 252 |
addr = all[index] |
| 253 |
assert res[addr][0] == name.encode('ascii') |
| 254 |
assert 'libexpat.so' in res[addr][1] |
| 255 |
|
| 256 |
def test_symbolify_pypy_function(): |
| 257 |
addr = _pypy_remote_debug.compute_remote_addr() |
| 258 |
name, filename = _pypy_remote_debug._symbolify(addr) |
| 259 |
assert name == b'pypysig_counter' |
| 260 |
addr = _pypy_remote_debug.compute_remote_addr(symbolname=b'pypy_g_DiskFile_read') |
| 261 |
name, filename = _pypy_remote_debug._symbolify(addr) |
| 262 |
assert name == b'pypy_g_DiskFile_read' |
| 263 |
|
| 264 |
def test_symbolify_all_pypy_function(): |
| 265 |
names = [b'pypy_g_DiskFile_read', b'pypy_g_DiskFile_seek'] |
| 266 |
all = [] |
| 267 |
for name in names: |
| 268 |
address_of_function = _pypy_remote_debug.compute_remote_addr('self', name) |
| 269 |
all.append(address_of_function) |
| 270 |
all.append(1) |
| 271 |
res = _pypy_remote_debug._symbolify_all(all) |
| 272 |
for index, name in enumerate(names): |
| 273 |
addr = all[index] |
| 274 |
assert res[addr][0] == name |
| 275 |
|
| 276 |
@pytest.mark.skipif(not hasattr(_vmprof, 'resolve_addr'), reason="not implemented") |
| 277 |
def test_symbolify_vmprof(): |
| 278 |
import _vmprof, ctypes |
| 279 |
so = load_so_or_skip('libexpat.so') |
| 280 |
address_of_function = (ctypes.cast(so.XML_Parse, ctypes.c_void_p)).value |
| 281 |
name, lineno, filename = _vmprof.resolve_addr(address_of_function) |
| 282 |
assert isinstance(name, str) |
| 283 |
assert isinstance(filename, str) |
| 284 |
assert name == 'XML_Parse' |
| 285 |
assert 'libexpat.so' in filename |
| 286 |
|
| 287 |
result = _vmprof.resolve_addr(1) |
| 288 |
assert result is None |
| 289 |
|
| 290 |
@pytest.mark.skipif(not hasattr(_vmprof, 'resolve_many_addr'), reason="not implemented") |
| 291 |
def test_symbolify_vmprof_many(): |
| 292 |
import _vmprof, ctypes |
| 293 |
names = [b'pypy_g_DiskFile_read', b'pypy_g_DiskFile_seek'] |
| 294 |
all = [] |
| 295 |
for name in names: |
| 296 |
address_of_function = _pypy_remote_debug.compute_remote_addr('self', name) |
| 297 |
all.append(address_of_function) |
| 298 |
|
| 299 |
names2 = ['XML_Parse', 'XML_GetBase'] |
| 300 |
so = load_so_or_skip('libexpat.so') |
| 301 |
for name in names2: |
| 302 |
address_of_function = (ctypes.cast(getattr(so, name), ctypes.c_void_p)).value |
| 303 |
all.append(address_of_function) |
| 304 |
all.append(1) |
| 305 |
|
| 306 |
res = _vmprof.resolve_many_addr(all) |
| 307 |
for index, name in enumerate(names + names2): |
| 308 |
addr = all[index] |
| 309 |
if isinstance(name, bytes): |
| 310 |
name = name.decode('utf-8') |
| 311 |
assert res[addr][0] == name |