|
| 1 | +# |
| 2 | +# Copyright (c) 2025 Nordic Semiconductor ASA |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause |
| 5 | +# |
| 6 | + |
| 7 | +import logging |
| 8 | +import re |
| 9 | +import subprocess |
| 10 | +import time |
| 11 | + |
| 12 | +import psutil |
| 13 | +from twister_harness import DeviceAdapter |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | + |
| 18 | +# Kill parent process and all child processes (if started) |
| 19 | +def _kill(proc): |
| 20 | + try: |
| 21 | + for child in psutil.Process(proc.pid).children(recursive=True): |
| 22 | + child.kill() |
| 23 | + proc.kill() |
| 24 | + logger.debug("Process was killed.") |
| 25 | + except Exception as e: |
| 26 | + logger.exception(f"Could not kill process - {e}") |
| 27 | + |
| 28 | + |
| 29 | +def test_west_debug(dut: DeviceAdapter): |
| 30 | + """ |
| 31 | + Compile and flash test application on MCU. |
| 32 | + Start debug session by calling `west debug` command. |
| 33 | + Set brakepoint and check that breakpoint was hit. |
| 34 | + """ |
| 35 | + BUILD_DIR = str(dut.device_config.build_dir) |
| 36 | + SEGGER_ID = dut.device_config.id |
| 37 | + COLLECT_TIMEOUT = 15.0 |
| 38 | + EXPECTED = r"15\s+counter\+\+;" |
| 39 | + |
| 40 | + logger.debug(f"{dut.device_config=}") |
| 41 | + |
| 42 | + # Wait a bit for the core to boot |
| 43 | + time.sleep(4) |
| 44 | + |
| 45 | + gdb_port = int(SEGGER_ID) % 1000 + 2331 |
| 46 | + cmd = f"west debug -d {BUILD_DIR} -- --dev-id {SEGGER_ID} --gdb-port {gdb_port}" |
| 47 | + |
| 48 | + logger.info(f"Executing:\n{cmd}") |
| 49 | + proc = subprocess.Popen( |
| 50 | + cmd.split(), |
| 51 | + stdin=subprocess.PIPE, |
| 52 | + stdout=subprocess.PIPE, |
| 53 | + stderr=subprocess.STDOUT, |
| 54 | + encoding='UTF-8', |
| 55 | + text=True, |
| 56 | + ) |
| 57 | + |
| 58 | + time.sleep(2) |
| 59 | + logger.info("Try to communicate with the process.") |
| 60 | + |
| 61 | + try: |
| 62 | + # set breakpoint in main.c line 18 which is `counter++;` |
| 63 | + # continue |
| 64 | + # [breakpoint shall hit] |
| 65 | + # quit |
| 66 | + outs, errs = proc.communicate( |
| 67 | + input="b main.c:15\nc\ndisconnect\nq\n", |
| 68 | + timeout=COLLECT_TIMEOUT, |
| 69 | + ) |
| 70 | + except subprocess.TimeoutExpired: |
| 71 | + _kill(proc) |
| 72 | + finally: |
| 73 | + outs, errs = proc.communicate() |
| 74 | + logger.info(f"{outs=}\n{errs=}") |
| 75 | + |
| 76 | + expected_str = re.search(EXPECTED, outs) |
| 77 | + assert expected_str is not None, f"Failed to match {EXPECTED} in {outs}" |
| 78 | + |
| 79 | + |
| 80 | +def test_west_attach(dut: DeviceAdapter): |
| 81 | + """ |
| 82 | + Compile and flash test application on MCU. |
| 83 | + Cal `west atach` command. |
| 84 | + Check response to where. |
| 85 | + """ |
| 86 | + BUILD_DIR = str(dut.device_config.build_dir) |
| 87 | + SEGGER_ID = dut.device_config.id |
| 88 | + COLLECT_TIMEOUT = 15.0 |
| 89 | + EXPECTED = r"in k_busy_wait \(usec_to_wait=500000\)" |
| 90 | + |
| 91 | + # Wait a bit for the core to boot |
| 92 | + time.sleep(4) |
| 93 | + |
| 94 | + gdb_port = int(SEGGER_ID) % 1000 + 2331 |
| 95 | + cmd = f"west attach -d {BUILD_DIR} -- --dev-id {SEGGER_ID} --gdb-port {gdb_port}" |
| 96 | + |
| 97 | + logger.info(f"Executing:\n{cmd}") |
| 98 | + proc = subprocess.Popen( |
| 99 | + cmd.split(), |
| 100 | + stdin=subprocess.PIPE, |
| 101 | + stdout=subprocess.PIPE, |
| 102 | + stderr=subprocess.STDOUT, |
| 103 | + encoding='UTF-8', |
| 104 | + text=True, |
| 105 | + ) |
| 106 | + |
| 107 | + time.sleep(2) |
| 108 | + logger.info("Try to communicate with the process.") |
| 109 | + |
| 110 | + try: |
| 111 | + # where |
| 112 | + # quit |
| 113 | + outs, errs = proc.communicate(input="where\ndisconnect\nq\n", timeout=COLLECT_TIMEOUT) |
| 114 | + except subprocess.TimeoutExpired: |
| 115 | + _kill(proc) |
| 116 | + finally: |
| 117 | + outs, errs = proc.communicate() |
| 118 | + logger.info(f"{outs=}\n{errs=}") |
| 119 | + |
| 120 | + expected_str = re.search(EXPECTED, outs) |
| 121 | + assert expected_str is not None, f"Failed to match {EXPECTED} in {outs}" |
0 commit comments