ZZZ Digital Lab

Back to WaitLens

scripts/fake_sdp.py

"""Isolated fake SDP tools for --dry-run only. No QNX compiler/runtime evidence."""
from pathlib import Path

QCC = r'''# SYNTHETIC fake compiler; not a QNX artifact.
import pathlib, struct, sys
if '--version' in sys.argv:
    print('SYNTHETIC qcc test double; no installed SDP version')
else:
    data = bytearray(64)
    data[:6] = b'\x7fELF\x02\x01'
    struct.pack_into('<H', data, 18, 62)
    pathlib.Path(sys.argv[sys.argv.index('-o') + 1]).write_bytes(data)
'''

GDB = r'''# SYNTHETIC debugger double; no sockets, shell execution or QNX OS.
import pathlib, re, shlex, shutil, sys
root = pathlib.Path.cwd()
commands = (root / 'transfer.gdb').read_text(encoding='utf-8').splitlines()
target = root / 'fake-target'
target.mkdir()
capture = root / 'capture'
uploads = 0
for line in commands:
    if line.startswith('upload '):
        _, local, remote = shlex.split(line)
        shutil.copyfile(root / local, target / pathlib.PurePosixPath(remote).name)
        uploads += 1
    if line.startswith('set args -c "chmod '):
        assert uploads == 2 and (target / 'capture.sh').is_file()
        nonce = re.search(r'capture.sh ([0-9a-f]+)', line)[1]
        (target / 'platform.txt').write_text('QNX\n8.0.0\n', encoding='utf-8')
        (target / 'probe.log').write_text('SYNTHETIC TEST DATA\ncompiler_qnx=800\nserver=100 client=101 phase=delay_before_receive\nreply=OK\nWaitLens probe: completed\n', encoding='utf-8')
        for name, state in [('send.txt', 'SEND'), ('reply.txt', 'REPLY')]:
            (target / name).write_text('pid tid name prio STATE Blocked\n100 1 waitlens_probe 10r NANOSLEEP\n101 1 waitlens_probe 10r ' + state + ' 100\n', encoding='utf-8')
        (target / 'capture-complete.txt').write_text(nonce + '\n', encoding='utf-8')
    if line.startswith('download '):
        _, remote, local = shlex.split(line)
        if local == 'target-shell':
            shutil.copyfile(root / 'waitlens_probe', root / local)
        else:
            shutil.copyfile(target / pathlib.PurePosixPath(remote).name, root / local)
assert uploads == 2
print('SYNTHETIC transfer/capture double completed. No target contacted.')
'''


def create(root):
    root = Path(root)
    tools = root / 'host/usr/bin'
    tools.mkdir(parents=True)
    (root / 'target/qnx8/x86_64').mkdir(parents=True)
    (root / 'SYNTHETIC.txt').write_text('Fake layout, not an SDK.\n', encoding='utf-8')
    qcc, gdb = tools / 'qcc.py', tools / 'ntox86_64-gdb.py'
    qcc.write_text(QCC, encoding='utf-8')
    gdb.write_text(GDB, encoding='utf-8')
    return qcc, gdb