From b60e4ff808039b7f71bdc3ad0ad1f3b243c026fd Mon Sep 17 00:00:00 2001 From: Samuel Obuch Date: Mon, 28 Apr 2025 15:58:35 +0200 Subject: [PATCH 1/7] ci: enable sysview examples for all chips --- examples/system/.build-test-rules.yml | 7 +- examples/system/gcov/pytest_gcov.py | 1 + .../sysview_tracing/main/sysview_tracing.c | 4 +- .../sysview_tracing/pytest_sysview_tracing.py | 198 +++++++++++++++--- .../system/sysview_tracing/sdkconfig.defaults | 2 - .../main/sysview_heap_log.c | 6 +- .../pytest_sysview_tracing_heap_log.py | 186 +++++++++++++--- 7 files changed, 332 insertions(+), 72 deletions(-) diff --git a/examples/system/.build-test-rules.yml b/examples/system/.build-test-rules.yml index 4124981e9a..d86e63057d 100644 --- a/examples/system/.build-test-rules.yml +++ b/examples/system/.build-test-rules.yml @@ -83,9 +83,6 @@ examples/system/gcov: - if: IDF_TARGET == "esp32p4" temporary: true reason: lack of runners - - if: IDF_TARGET == "esp32s3" - temporary: true - reason: unstable, known data corruption issue #TODO: OCD-1048 examples/system/gdbstub: disable: @@ -243,7 +240,7 @@ examples/system/sysview_tracing: disable: - if: SOC_GPTIMER_SUPPORTED != 1 disable_test: - - if: IDF_TARGET != "esp32" + - if: IDF_TARGET == "esp32p4" temporary: true reason: lack of runners @@ -251,7 +248,7 @@ examples/system/sysview_tracing_heap_log: disable: - if: SOC_GPTIMER_SUPPORTED != 1 disable_test: - - if: IDF_TARGET != "esp32" + - if: IDF_TARGET == "esp32p4" temporary: true reason: lack of runners diff --git a/examples/system/gcov/pytest_gcov.py b/examples/system/gcov/pytest_gcov.py index 5d8fe62c81..1408b52ed1 100644 --- a/examples/system/gcov/pytest_gcov.py +++ b/examples/system/gcov/pytest_gcov.py @@ -188,6 +188,7 @@ def test_gcov(dut: IdfDut) -> None: _test_gcov(dut) +@pytest.mark.esp32s3 @pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 diff --git a/examples/system/sysview_tracing/main/sysview_tracing.c b/examples/system/sysview_tracing/main/sysview_tracing.c index 5a68888bfe..115224fb97 100644 --- a/examples/system/sysview_tracing/main/sysview_tracing.c +++ b/examples/system/sysview_tracing/main/sysview_tracing.c @@ -134,11 +134,13 @@ static void example_task(void *p) void app_main(void) { + ESP_LOGI(TAG, "Ready for OpenOCD connection"); + static example_event_data_t event_data[CONFIG_FREERTOS_NUMBER_OF_CORES]; #if CONFIG_APPTRACE_SV_ENABLE && CONFIG_USE_CUSTOM_EVENT_ID // Currently OpenOCD does not support requesting module info from target. So do the following... - // Wait untill SystemView module receives START command from host, + // Wait until SystemView module receives START command from host, // after that data can be sent to the host using onboard API, // so user module description does not need to be requested by OpenOCD itself. while (!SEGGER_SYSVIEW_Started()) { diff --git a/examples/system/sysview_tracing/pytest_sysview_tracing.py b/examples/system/sysview_tracing/pytest_sysview_tracing.py index 6af3e02c71..eb3edeaf28 100644 --- a/examples/system/sysview_tracing/pytest_sysview_tracing.py +++ b/examples/system/sysview_tracing/pytest_sysview_tracing.py @@ -1,47 +1,179 @@ # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 +import json +import logging +import os.path import re +import signal import time +from telnetlib import Telnet +from typing import Any +from typing import Optional -import pexpect.fdpexpect +import pexpect import pytest +from pytest_embedded.utils import to_bytes +from pytest_embedded.utils import to_str from pytest_embedded_idf import IdfDut +MAX_RETRIES = 3 +RETRY_DELAY = 1 +TELNET_PORT = 4444 + + +class OpenOCD: + def __init__(self, dut: 'IdfDut'): + self.dut = dut + self.telnet: Optional[Telnet] = None + self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') + self.proc: Optional[pexpect.spawn] = None + + def run(self) -> Optional['OpenOCD']: + desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') + + try: + with open(desc_path, 'r') as f: + project_desc = json.load(f) + except FileNotFoundError: + logging.error('Project description file not found at %s', desc_path) + return None + + openocd_scripts = os.getenv('OPENOCD_SCRIPTS') + if not openocd_scripts: + logging.error('OPENOCD_SCRIPTS environment variable is not set.') + return None + + debug_args = project_desc.get('debug_arguments_openocd') + if not debug_args: + logging.error("'debug_arguments_openocd' key is missing in project_description.json") + return None + + # For debug purposes, make the value '4' + ocd_env = os.environ.copy() + ocd_env['LIBUSB_DEBUG'] = '1' + + for _ in range(1, MAX_RETRIES + 1): + try: + self.proc = pexpect.spawn( + command='openocd', + args=['-s', openocd_scripts] + debug_args.split(), + timeout=5, + encoding='utf-8', + codec_errors='ignore', + env=ocd_env, + ) + if self.proc and self.proc.isalive(): + self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) + return self + except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT) as e: + logging.error('Error running OpenOCD: %s', str(e)) + if self.proc and self.proc.isalive(): + self.proc.terminate() + time.sleep(RETRY_DELAY) + + logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) + return None + + def connect_telnet(self) -> None: + for attempt in range(1, MAX_RETRIES + 1): + try: + self.telnet = Telnet('127.0.0.1', TELNET_PORT, 5) + break + except ConnectionRefusedError as e: + logging.error('Error telnet connection: %s in attempt:%d', e, attempt) + time.sleep(1) + else: + raise ConnectionRefusedError + + def write(self, s: str) -> Any: + if self.telnet is None: + logging.error('Telnet connection is not established.') + return '' + resp = self.telnet.read_very_eager() + self.telnet.write(to_bytes(s, '\n')) + resp += self.telnet.read_until(b'>') + return to_str(resp) + + def apptrace_wait_stop(self, timeout: int = 30) -> None: + stopped = False + end_before = time.time() + timeout + while not stopped: + cmd_out = self.write('esp apptrace status') + for line in cmd_out.splitlines(): + if line.startswith('Tracing is STOPPED.'): + stopped = True + break + if not stopped and time.time() > end_before: + raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') + time.sleep(1) + + def kill(self) -> None: + # Check if the process is still running + if self.proc and self.proc.isalive(): + self.proc.terminate() + self.proc.kill(signal.SIGKILL) + + +def _test_examples_sysview_tracing(dut: IdfDut) -> None: + # Construct trace log paths + trace_log = [ + os.path.join(dut.logdir, 'sys_log0.svdat') # pylint: disable=protected-access + ] + if not dut.app.sdkconfig.get('ESP_SYSTEM_SINGLE_CORE_MODE') or dut.target == 'esp32s3': + trace_log.append(os.path.join(dut.logdir, 'sys_log1.svdat')) # pylint: disable=protected-access + trace_files = ' '.join([f'file://{log}' for log in trace_log]) + + # Prepare gdbinit file + gdb_logfile = os.path.join(dut.logdir, 'gdb.txt') + gdbinit_orig = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'gdbinit') + gdbinit = os.path.join(dut.logdir, 'gdbinit') + with open(gdbinit_orig, 'r') as f_r, open(gdbinit, 'w') as f_w: + for line in f_r: + if line.startswith('mon esp sysview start'): + f_w.write(f'mon esp sysview start {trace_files}\n') + else: + f_w.write(line) + + def dut_expect_task_event() -> None: + dut.expect(re.compile(rb'example: Task\[0x[0-9A-Fa-f]+\]: received event \d+'), timeout=30) + + dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) + openocd = OpenOCD(dut).run() + assert openocd + try: + openocd.connect_telnet() + openocd.write('log_output {}'.format(openocd.log_file)) + + with open(gdb_logfile, 'w') as gdb_log, pexpect.spawn( + f'idf.py -B {dut.app.binary_path} gdb --batch -x {gdbinit}', + timeout=60, + logfile=gdb_log, + encoding='utf-8', + codec_errors='ignore', + ) as p: + p.expect_exact('hit Breakpoint 1, app_main ()') + dut.expect('example: Created task') # dut has been restarted by gdb since the last dut.expect() + dut_expect_task_event() + + # Do a sleep while sysview samples are captured. + time.sleep(3) + openocd.write('esp sysview stop') + finally: + openocd.kill() + @pytest.mark.esp32 +@pytest.mark.esp32s2 +@pytest.mark.esp32c2 @pytest.mark.jtag -@pytest.mark.parametrize( - 'embedded_services', - [ - 'esp,idf,jtag', - ], - indirect=True, -) def test_examples_sysview_tracing(dut: IdfDut) -> None: - def dut_expect_task_event() -> None: - dut.expect(re.compile(rb'example: Task\[0x3[0-9A-Fa-f]+\]: received event \d+'), timeout=30) + _test_examples_sysview_tracing(dut) - dut.gdb.write('mon reset halt') - dut.gdb.write('maintenance flush register-cache') - dut.gdb.write('b app_main') - dut.gdb.write('commands', non_blocking=True) - dut.gdb.write( - 'mon esp sysview start file:///tmp/sysview_example0.svdat file:///tmp/sysview_example1.svdat', non_blocking=True - ) - dut.gdb.write('c', non_blocking=True) - dut.gdb.write('end') - - dut.gdb.write('c', non_blocking=True) - time.sleep(1) # to avoid EOF file error - with open(dut.gdb._logfile, encoding='utf-8') as fr: # pylint: disable=protected-access - gdb_pexpect_proc = pexpect.fdpexpect.fdspawn(fr.fileno()) - gdb_pexpect_proc.expect('Thread 2 "main" hit Breakpoint 1, app_main ()') - - dut.expect('example: Created task') # dut has been restarted by gdb since the last dut.expect() - dut_expect_task_event() - - # Do a sleep while sysview samples are captured. - time.sleep(3) - # GDB isn't responding now to any commands, therefore, the following command is issued to openocd - dut.openocd.write('esp sysview stop') +@pytest.mark.esp32s3 +@pytest.mark.esp32c3 +@pytest.mark.esp32c6 +@pytest.mark.esp32h2 +@pytest.mark.usb_serial_jtag +def test_examples_sysview_tracing_usj(dut: IdfDut) -> None: + _test_examples_sysview_tracing(dut) diff --git a/examples/system/sysview_tracing/sdkconfig.defaults b/examples/system/sysview_tracing/sdkconfig.defaults index d93b1caeb7..a08cc7e128 100644 --- a/examples/system/sysview_tracing/sdkconfig.defaults +++ b/examples/system/sysview_tracing/sdkconfig.defaults @@ -1,5 +1,3 @@ -# Enable single core mode by default -CONFIG_FREERTOS_UNICORE=y # 1ms tick period CONFIG_FREERTOS_HZ=1000 # Enable application tracing by default diff --git a/examples/system/sysview_tracing_heap_log/main/sysview_heap_log.c b/examples/system/sysview_tracing_heap_log/main/sysview_heap_log.c index f07b6c821b..aecf041da5 100644 --- a/examples/system/sysview_tracing_heap_log/main/sysview_heap_log.c +++ b/examples/system/sysview_tracing_heap_log/main/sysview_heap_log.c @@ -53,7 +53,7 @@ static void alloc_task(void *p) snprintf(task_name, sizeof(task_name), "free%d", task_args->idx); xTaskCreatePinnedToCore(free_task, task_name, 2500, queue, 5, NULL, CONFIG_FREERTOS_NUMBER_OF_CORES-1); - // here GDB will stop at brekpoint and execute OpenOCD command to start tracing + // here GDB will stop at breakpoint and execute OpenOCD command to start tracing for(int i = 1; i < 10; i++) { uint32_t sz = 2*i*(task_args->idx + 1); void *p = malloc(sz/2); @@ -73,6 +73,8 @@ static void alloc_task(void *p) void app_main(void) { + ESP_LOGI(TAG, "Ready for OpenOCD connection"); + const int num_allocers = 3; char task_name[20]; // redirect log messages to the host using SystemView tracing module @@ -100,6 +102,6 @@ void app_main(void) uint32_t val = ulTaskNotifyTake(pdFALSE, portMAX_DELAY); ESP_LOGI(TAG, "Got notify val %"PRIu32, val); } - // here GDB will stop at brekpoint and execute OpenOCD command to stop tracing + // here GDB will stop at breakpoint and execute OpenOCD command to stop tracing heap_trace_stop(); } diff --git a/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py b/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py index c6147f52d5..fa8d8e6d13 100644 --- a/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py +++ b/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py @@ -1,47 +1,156 @@ # SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 +import json +import logging import os.path +import signal import time +from telnetlib import Telnet +from typing import Any +from typing import Optional import pexpect.fdpexpect import pytest +from pytest_embedded.utils import to_bytes +from pytest_embedded.utils import to_str from pytest_embedded_idf import IdfDut +MAX_RETRIES = 3 +RETRY_DELAY = 1 +TELNET_PORT = 4444 -@pytest.mark.esp32 -@pytest.mark.jtag -@pytest.mark.parametrize('config', ['app_trace_jtag'], indirect=True) -@pytest.mark.parametrize('embedded_services', ['esp,idf,jtag'], indirect=True) -def test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: + +class OpenOCD: + def __init__(self, dut: 'IdfDut'): + self.dut = dut + self.telnet: Optional[Telnet] = None + self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') + self.proc: Optional[pexpect.spawn] = None + + def run(self) -> Optional['OpenOCD']: + desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') + + try: + with open(desc_path, 'r') as f: + project_desc = json.load(f) + except FileNotFoundError: + logging.error('Project description file not found at %s', desc_path) + return None + + openocd_scripts = os.getenv('OPENOCD_SCRIPTS') + if not openocd_scripts: + logging.error('OPENOCD_SCRIPTS environment variable is not set.') + return None + + debug_args = project_desc.get('debug_arguments_openocd') + if not debug_args: + logging.error("'debug_arguments_openocd' key is missing in project_description.json") + return None + + # For debug purposes, make the value '4' + ocd_env = os.environ.copy() + ocd_env['LIBUSB_DEBUG'] = '1' + + for _ in range(1, MAX_RETRIES + 1): + try: + self.proc = pexpect.spawn( + command='openocd', + args=['-s', openocd_scripts] + debug_args.split(), + timeout=5, + encoding='utf-8', + codec_errors='ignore', + env=ocd_env, + ) + if self.proc and self.proc.isalive(): + self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) + return self + except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT) as e: + logging.error('Error running OpenOCD: %s', str(e)) + if self.proc and self.proc.isalive(): + self.proc.terminate() + time.sleep(RETRY_DELAY) + + logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) + return None + + def connect_telnet(self) -> None: + for attempt in range(1, MAX_RETRIES + 1): + try: + self.telnet = Telnet('127.0.0.1', TELNET_PORT, 5) + break + except ConnectionRefusedError as e: + logging.error('Error telnet connection: %s in attempt:%d', e, attempt) + time.sleep(1) + else: + raise ConnectionRefusedError + + def write(self, s: str) -> Any: + if self.telnet is None: + logging.error('Telnet connection is not established.') + return '' + resp = self.telnet.read_very_eager() + self.telnet.write(to_bytes(s, '\n')) + resp += self.telnet.read_until(b'>') + return to_str(resp) + + def apptrace_wait_stop(self, timeout: int = 30) -> None: + stopped = False + end_before = time.time() + timeout + while not stopped: + cmd_out = self.write('esp apptrace status') + for line in cmd_out.splitlines(): + if line.startswith('Tracing is STOPPED.'): + stopped = True + break + if not stopped and time.time() > end_before: + raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') + time.sleep(1) + + def kill(self) -> None: + # Check if the process is still running + if self.proc and self.proc.isalive(): + self.proc.terminate() + self.proc.kill(signal.SIGKILL) + + +def _test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: # Construct trace log paths trace_log = [ - os.path.join(os.path.dirname(dut.gdb._logfile), 'heap_log0.svdat') # pylint: disable=protected-access + os.path.join(dut.logdir, 'heap_log0.svdat') # pylint: disable=protected-access ] - if dut.target in ['esp32', 'esp32s3', 'esp32p4']: - trace_log.append(os.path.join(os.path.dirname(dut.gdb._logfile), 'heap_log1.svdat')) # pylint: disable=protected-access - - # Set up GDB - dut.gdb.write('set width unlimited') # Don't split output lines for easy parsing - dut.gdb.write('mon reset halt') - dut.gdb.write('maintenance flush register-cache') - - # Start sysview tracing - dut.gdb.write('tb heap_trace_start') - dut.gdb.write('commands', non_blocking=True) + if not dut.app.sdkconfig.get('ESP_SYSTEM_SINGLE_CORE_MODE') or dut.target == 'esp32s3': + trace_log.append(os.path.join(dut.logdir, 'heap_log1.svdat')) # pylint: disable=protected-access trace_files = ' '.join([f'file://{log}' for log in trace_log]) - dut.gdb.write(f'mon esp sysview start {trace_files}', non_blocking=True) - dut.gdb.write('c', non_blocking=True) - dut.gdb.write('end') - # Stop sysview tracing - dut.gdb.write('tb heap_trace_stop') - dut.gdb.write('commands', non_blocking=True) - dut.gdb.write('mon esp sysview stop', non_blocking=True) - dut.gdb.write('end') - dut.gdb.write('c', non_blocking=True) + # Prepare gdbinit file + gdb_logfile = os.path.join(dut.logdir, 'gdb.txt') + gdbinit_orig = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'gdbinit') + gdbinit = os.path.join(dut.logdir, 'gdbinit') + with open(gdbinit_orig, 'r') as f_r, open(gdbinit, 'w') as f_w: + for line in f_r: + if line.startswith('mon esp sysview start'): + f_w.write(f'mon esp sysview start {trace_files}\n') + else: + f_w.write(line) - # Wait for sysview files to be generated - time.sleep(1) + dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) + openocd = OpenOCD(dut).run() + assert openocd + try: + openocd.connect_telnet() + openocd.write('log_output {}'.format(openocd.log_file)) + + with open(gdb_logfile, 'w') as gdb_log, pexpect.spawn( + f'idf.py -B {dut.app.binary_path} gdb --batch -x {gdbinit}', + timeout=60, + logfile=gdb_log, + encoding='utf-8', + codec_errors='ignore', + ) as p: + # Wait for sysview files to be generated + p.expect_exact('Tracing is STOPPED') + finally: + openocd.kill() # Process sysview trace logs command = [os.path.join(idf_path, 'tools', 'esp_app_trace', 'sysviewtrace_proc.py'), '-p'] + trace_log @@ -49,8 +158,27 @@ def test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: sysviewtrace.expect(r'Found \d+ leaked bytes in \d+ blocks.', timeout=120) # Validate GDB logs - with open(dut.gdb._logfile, encoding='utf-8') as fr: # pylint: disable=protected-access + with open(gdb_logfile, encoding='utf-8') as fr: # pylint: disable=protected-access gdb_pexpect_proc = pexpect.fdpexpect.fdspawn(fr.fileno()) gdb_pexpect_proc.expect_exact( 'Thread 2 "main" hit Temporary breakpoint 1, heap_trace_start (mode_param', timeout=10) # should be (mode_param=HEAP_TRACE_ALL) # TODO GCC-329 gdb_pexpect_proc.expect_exact('Thread 2 "main" hit Temporary breakpoint 2, heap_trace_stop ()', timeout=10) + + +@pytest.mark.parametrize('config', ['app_trace_jtag'], indirect=True) +@pytest.mark.jtag +@pytest.mark.esp32 +@pytest.mark.esp32s2 +@pytest.mark.esp32c2 +def test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: + _test_examples_sysview_tracing_heap_log(idf_path, dut) + + +@pytest.mark.parametrize('config', ['app_trace_jtag'], indirect=True) +@pytest.mark.usb_serial_jtag +@pytest.mark.esp32s3 +@pytest.mark.esp32c3 +@pytest.mark.esp32c6 +@pytest.mark.esp32h2 +def test_examples_sysview_tracing_heap_log_usj(idf_path: str, dut: IdfDut) -> None: + _test_examples_sysview_tracing_heap_log(idf_path, dut) From 30d9149ef7686b220b3d6302c32a2767a21ae14b Mon Sep 17 00:00:00 2001 From: Samuel Obuch Date: Tue, 20 May 2025 10:44:33 +0200 Subject: [PATCH 2/7] ci: OpenOCD class as fixture --- conftest.py | 114 +++++++++++++ .../app_trace_basic/pytest_app_trace_basic.py | 127 ++------------- examples/system/gcov/pytest_gcov.py | 129 ++------------- .../sysview_tracing/pytest_sysview_tracing.py | 151 +++--------------- .../pytest_sysview_tracing_heap_log.py | 144 ++--------------- 5 files changed, 171 insertions(+), 494 deletions(-) diff --git a/conftest.py b/conftest.py index 786eff95b5..3ecddffd81 100644 --- a/conftest.py +++ b/conftest.py @@ -19,16 +19,21 @@ if os.path.join(os.path.dirname(__file__), 'tools', 'ci', 'python_packages') not import glob import io +import json import logging import os import re +import signal +import time import typing as t import zipfile from copy import deepcopy +from telnetlib import Telnet from urllib.parse import quote import common_test_methods # noqa: F401 import gitlab_api +import pexpect import pytest import requests import yaml @@ -44,6 +49,8 @@ from idf_pytest.constants import DEFAULT_SDKCONFIG, ENV_MARKERS, SPECIAL_MARKERS from idf_pytest.plugin import IDF_PYTEST_EMBEDDED_KEY, ITEM_PYTEST_CASE_KEY, IdfPytestEmbedded from idf_pytest.utils import format_case_id from pytest_embedded.plugin import multi_dut_argument, multi_dut_fixture +from pytest_embedded.utils import to_bytes +from pytest_embedded.utils import to_str from pytest_embedded_idf.dut import IdfDut from pytest_embedded_idf.unity_tester import CaseTester @@ -139,6 +146,113 @@ class BuildReportDownloader(AppDownloader): super().download_app(app_build_path, artifact_type) +class OpenOCD: + def __init__(self, dut: 'IdfDut'): + self.MAX_RETRIES = 3 + self.RETRY_DELAY = 1 + self.TELNET_PORT = 4444 + self.dut = dut + self.telnet: t.Optional[Telnet] = None + self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') + self.proc: t.Optional[pexpect.spawn] = None + + def __enter__(self) -> 'OpenOCD': + return self + + def __exit__(self, exception_type: t.Any, exception_value: t.Any, exception_traceback: t.Any) -> None: + self.kill() + + def run(self) -> t.Optional['OpenOCD']: + desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') + + try: + with open(desc_path, 'r') as f: + project_desc = json.load(f) + except FileNotFoundError: + logging.error('Project description file not found at %s', desc_path) + raise + + openocd_scripts = os.getenv('OPENOCD_SCRIPTS') + if not openocd_scripts: + raise RuntimeError('OPENOCD_SCRIPTS environment variable is not set.') + + debug_args = project_desc.get('debug_arguments_openocd') + if not debug_args: + raise KeyError("'debug_arguments_openocd' key is missing in project_description.json") + + # For debug purposes, make the value '4' + ocd_env = os.environ.copy() + ocd_env['LIBUSB_DEBUG'] = '1' + + for _ in range(1, self.MAX_RETRIES + 1): + try: + self.proc = pexpect.spawn( + command='openocd', + args=['-s', openocd_scripts] + debug_args.split(), + timeout=5, + encoding='utf-8', + codec_errors='ignore', + env=ocd_env, + ) + if self.proc and self.proc.isalive(): + self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) + self.connect_telnet() + self.write('log_output {}'.format(self.log_file)) + return self + except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT, ConnectionRefusedError) as e: + logging.error('Error running OpenOCD: %s', str(e)) + self.kill() + time.sleep(self.RETRY_DELAY) + + raise RuntimeError('Failed to run OpenOCD after %d attempts.', self.MAX_RETRIES) + + def connect_telnet(self) -> None: + for attempt in range(1, self.MAX_RETRIES + 1): + try: + self.telnet = Telnet('127.0.0.1', self.TELNET_PORT, 5) + break + except ConnectionRefusedError as e: + logging.error('Error telnet connection: %s in attempt:%d', e, attempt) + time.sleep(1) + else: + raise ConnectionRefusedError + + def write(self, s: str) -> t.Any: + if self.telnet is None: + logging.error('Telnet connection is not established.') + return '' + resp = self.telnet.read_very_eager() + self.telnet.write(to_bytes(s, '\n')) + resp += self.telnet.read_until(b'>') + return to_str(resp) + + def apptrace_wait_stop(self, timeout: int = 30) -> None: + stopped = False + end_before = time.time() + timeout + while not stopped: + cmd_out = self.write('esp apptrace status') + for line in cmd_out.splitlines(): + if line.startswith('Tracing is STOPPED.'): + stopped = True + break + if not stopped and time.time() > end_before: + raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') + time.sleep(1) + + def kill(self) -> None: + # Check if the process is still running + if self.proc and self.proc.isalive(): + self.proc.terminate() + self.proc.kill(signal.SIGKILL) + + +@pytest.fixture +def openocd_dut(dut: IdfDut) -> OpenOCD: + if isinstance(dut, tuple): + raise ValueError('Multi-DUT support is not implemented yet') + return OpenOCD(dut) + + @pytest.fixture(scope='session') def app_downloader(pipeline_id: t.Optional[str]) -> t.Optional[AppDownloader]: if not pipeline_id: diff --git a/examples/system/app_trace_basic/pytest_app_trace_basic.py b/examples/system/app_trace_basic/pytest_app_trace_basic.py index bdb9243332..5f2128ac73 100644 --- a/examples/system/app_trace_basic/pytest_app_trace_basic.py +++ b/examples/system/app_trace_basic/pytest_app_trace_basic.py @@ -1,125 +1,18 @@ # SPDX-FileCopyrightText: 2022-2025 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 -import json -import logging -import os -import signal import time -from telnetlib import Telnet -from typing import Any -from typing import Optional +import typing -import pexpect import pytest -from pytest_embedded.utils import to_bytes -from pytest_embedded.utils import to_str from pytest_embedded_idf import IdfDut -MAX_RETRIES = 3 -RETRY_DELAY = 1 -TELNET_PORT = 4444 +if typing.TYPE_CHECKING: + from conftest import OpenOCD -class OpenOCD: - def __init__(self, dut: 'IdfDut'): - self.dut = dut - self.telnet: Optional[Telnet] = None - self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') - self.proc: Optional[pexpect.spawn] = None - - def run(self) -> Optional['OpenOCD']: - desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') - - try: - with open(desc_path, 'r') as f: - project_desc = json.load(f) - except FileNotFoundError: - logging.error('Project description file not found at %s', desc_path) - return None - - openocd_scripts = os.getenv('OPENOCD_SCRIPTS') - if not openocd_scripts: - logging.error('OPENOCD_SCRIPTS environment variable is not set.') - return None - - debug_args = project_desc.get('debug_arguments_openocd') - if not debug_args: - logging.error("'debug_arguments_openocd' key is missing in project_description.json") - return None - - # For debug purposes, make the value '4' - ocd_env = os.environ.copy() - ocd_env['LIBUSB_DEBUG'] = '1' - - for _ in range(1, MAX_RETRIES + 1): - try: - self.proc = pexpect.spawn( - command='openocd', - args=['-s', openocd_scripts] + debug_args.split(), - timeout=5, - encoding='utf-8', - codec_errors='ignore', - env=ocd_env, - ) - if self.proc and self.proc.isalive(): - self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) - return self - except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT) as e: - logging.error('Error running OpenOCD: %s', str(e)) - if self.proc and self.proc.isalive(): - self.proc.terminate() - time.sleep(RETRY_DELAY) - - logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) - return None - - def connect_telnet(self) -> None: - for attempt in range(1, MAX_RETRIES + 1): - try: - self.telnet = Telnet('127.0.0.1', TELNET_PORT, 5) - break - except ConnectionRefusedError as e: - logging.error('Error telnet connection: %s in attempt:%d', e, attempt) - time.sleep(1) - else: - raise ConnectionRefusedError - - def write(self, s: str) -> Any: - if self.telnet is None: - logging.error('Telnet connection is not established.') - return '' - resp = self.telnet.read_very_eager() - self.telnet.write(to_bytes(s, '\n')) - resp += self.telnet.read_until(b'>') - return to_str(resp) - - def apptrace_wait_stop(self, timeout: int = 30) -> None: - stopped = False - end_before = time.time() + timeout - while not stopped: - cmd_out = self.write('esp apptrace status') - for line in cmd_out.splitlines(): - if line.startswith('Tracing is STOPPED.'): - stopped = True - break - if not stopped and time.time() > end_before: - raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') - time.sleep(1) - - def kill(self) -> None: - # Check if the process is still running - if self.proc and self.proc.isalive(): - self.proc.terminate() - self.proc.kill(signal.SIGKILL) - - -def _test_examples_app_trace_basic(dut: IdfDut) -> None: +def _test_examples_app_trace_basic(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: dut.expect_exact('example: Waiting for OpenOCD connection', timeout=5) - openocd = OpenOCD(dut).run() - assert openocd - try: - openocd.connect_telnet() - openocd.write('log_output {}'.format(openocd.log_file)) + with openocd_dut.run() as openocd: openocd.write('reset run') dut.expect_exact('example: Waiting for OpenOCD connection', timeout=5) time.sleep(1) # wait for APPTRACE_INIT semihosting call @@ -150,16 +43,14 @@ def _test_examples_app_trace_basic(dut: IdfDut) -> None: break if found is not True: raise RuntimeError('"{}" could not be found in {}'.format(log_str, 'apptrace.log')) - finally: - openocd.kill() @pytest.mark.esp32 @pytest.mark.esp32c2 @pytest.mark.esp32s2 @pytest.mark.jtag -def test_examples_app_trace_basic(dut: IdfDut) -> None: - _test_examples_app_trace_basic(dut) +def test_examples_app_trace_basic(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_examples_app_trace_basic(openocd_dut, dut) @pytest.mark.esp32s3 @@ -167,5 +58,5 @@ def test_examples_app_trace_basic(dut: IdfDut) -> None: @pytest.mark.esp32c6 @pytest.mark.esp32h2 @pytest.mark.usb_serial_jtag -def test_examples_app_trace_basic_usj(dut: IdfDut) -> None: - _test_examples_app_trace_basic(dut) +def test_examples_app_trace_basic_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_examples_app_trace_basic(openocd_dut, dut) diff --git a/examples/system/gcov/pytest_gcov.py b/examples/system/gcov/pytest_gcov.py index 1408b52ed1..b69e842b03 100644 --- a/examples/system/gcov/pytest_gcov.py +++ b/examples/system/gcov/pytest_gcov.py @@ -1,128 +1,22 @@ # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 -import json -import logging import os.path -import signal import time -from telnetlib import Telnet -from typing import Any -from typing import Optional +import typing -import pexpect import pytest -from pytest_embedded.utils import to_bytes -from pytest_embedded.utils import to_str from pytest_embedded_idf import IdfDut -MAX_RETRIES = 3 -RETRY_DELAY = 1 -TELNET_PORT = 4444 +if typing.TYPE_CHECKING: + from conftest import OpenOCD -class OpenOCD: - def __init__(self, dut: 'IdfDut'): - self.dut = dut - self.telnet: Optional[Telnet] = None - self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') - self.proc: Optional[pexpect.spawn] = None - - def run(self) -> Optional['OpenOCD']: - desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') - - try: - with open(desc_path, 'r') as f: - project_desc = json.load(f) - except FileNotFoundError: - logging.error('Project description file not found at %s', desc_path) - return None - - openocd_scripts = os.getenv('OPENOCD_SCRIPTS') - if not openocd_scripts: - logging.error('OPENOCD_SCRIPTS environment variable is not set.') - return None - - debug_args = project_desc.get('debug_arguments_openocd') - if not debug_args: - logging.error("'debug_arguments_openocd' key is missing in project_description.json") - return None - - # For debug purposes, make the value '4' - ocd_env = os.environ.copy() - ocd_env['LIBUSB_DEBUG'] = '1' - - for _ in range(1, MAX_RETRIES + 1): - try: - self.proc = pexpect.spawn( - command='openocd', - args=['-s', openocd_scripts] + debug_args.split(), - timeout=5, - encoding='utf-8', - codec_errors='ignore', - env=ocd_env, - ) - if self.proc and self.proc.isalive(): - self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) - return self - except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT) as e: - logging.error('Error running OpenOCD: %s', str(e)) - if self.proc and self.proc.isalive(): - self.proc.terminate() - time.sleep(RETRY_DELAY) - - logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) - return None - - def connect_telnet(self) -> None: - for attempt in range(1, MAX_RETRIES + 1): - try: - self.telnet = Telnet('127.0.0.1', TELNET_PORT, 5) - break - except ConnectionRefusedError as e: - logging.error('Error telnet connection: %s in attempt:%d', e, attempt) - time.sleep(1) - else: - raise ConnectionRefusedError - - def write(self, s: str) -> Any: - if self.telnet is None: - logging.error('Telnet connection is not established.') - return '' - resp = self.telnet.read_very_eager() - self.telnet.write(to_bytes(s, '\n')) - resp += self.telnet.read_until(b'>') - return to_str(resp) - - def apptrace_wait_stop(self, timeout: int = 30) -> None: - stopped = False - end_before = time.time() + timeout - while not stopped: - cmd_out = self.write('esp apptrace status') - for line in cmd_out.splitlines(): - if line.startswith('Tracing is STOPPED.'): - stopped = True - break - if not stopped and time.time() > end_before: - raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') - time.sleep(1) - - def kill(self) -> None: - # Check if the process is still running - if self.proc and self.proc.isalive(): - self.proc.terminate() - self.proc.kill(signal.SIGKILL) - - -def _test_gcov(dut: IdfDut) -> None: +def _test_gcov(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: # create the generated .gcda folder, otherwise would have error: failed to open file. # normally this folder would be created via `idf.py build`. but in CI the non-related files would not be preserved os.makedirs(os.path.join(dut.app.binary_path, 'esp-idf', 'main', 'CMakeFiles', '__idf_main.dir'), exist_ok=True) os.makedirs(os.path.join(dut.app.binary_path, 'esp-idf', 'sample', 'CMakeFiles', '__idf_sample.dir'), exist_ok=True) - dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) - openocd = OpenOCD(dut).run() - assert openocd - def expect_counter_output(loop: int, timeout: int = 10) -> None: dut.expect_exact( [f'blink_dummy_func: Counter = {loop}', f'some_dummy_func: Counter = {loop * 2}'], @@ -152,9 +46,8 @@ def _test_gcov(dut: IdfDut) -> None: assert len(expect_lines) == 0 - try: - openocd.connect_telnet() - openocd.write('log_output {}'.format(openocd.log_file)) + dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) + with openocd_dut.run() as openocd: openocd.write('reset run') dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) @@ -176,16 +69,14 @@ def _test_gcov(dut: IdfDut) -> None: time.sleep(1) # Test instant run-time dump dump_coverage('esp gcov') - finally: - openocd.kill() @pytest.mark.jtag @pytest.mark.esp32 @pytest.mark.esp32c2 @pytest.mark.esp32s2 -def test_gcov(dut: IdfDut) -> None: - _test_gcov(dut) +def test_gcov(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_gcov(openocd_dut, dut) @pytest.mark.esp32s3 @@ -193,5 +84,5 @@ def test_gcov(dut: IdfDut) -> None: @pytest.mark.esp32c6 @pytest.mark.esp32h2 @pytest.mark.usb_serial_jtag -def test_gcov_usj(dut: IdfDut) -> None: - _test_gcov(dut) +def test_gcov_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_gcov(openocd_dut, dut) diff --git a/examples/system/sysview_tracing/pytest_sysview_tracing.py b/examples/system/sysview_tracing/pytest_sysview_tracing.py index eb3edeaf28..cbe90bc581 100644 --- a/examples/system/sysview_tracing/pytest_sysview_tracing.py +++ b/examples/system/sysview_tracing/pytest_sysview_tracing.py @@ -1,120 +1,19 @@ # SPDX-FileCopyrightText: 2022 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 -import json -import logging import os.path import re -import signal import time -from telnetlib import Telnet -from typing import Any -from typing import Optional +import typing import pexpect import pytest -from pytest_embedded.utils import to_bytes -from pytest_embedded.utils import to_str from pytest_embedded_idf import IdfDut -MAX_RETRIES = 3 -RETRY_DELAY = 1 -TELNET_PORT = 4444 +if typing.TYPE_CHECKING: + from conftest import OpenOCD -class OpenOCD: - def __init__(self, dut: 'IdfDut'): - self.dut = dut - self.telnet: Optional[Telnet] = None - self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') - self.proc: Optional[pexpect.spawn] = None - - def run(self) -> Optional['OpenOCD']: - desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') - - try: - with open(desc_path, 'r') as f: - project_desc = json.load(f) - except FileNotFoundError: - logging.error('Project description file not found at %s', desc_path) - return None - - openocd_scripts = os.getenv('OPENOCD_SCRIPTS') - if not openocd_scripts: - logging.error('OPENOCD_SCRIPTS environment variable is not set.') - return None - - debug_args = project_desc.get('debug_arguments_openocd') - if not debug_args: - logging.error("'debug_arguments_openocd' key is missing in project_description.json") - return None - - # For debug purposes, make the value '4' - ocd_env = os.environ.copy() - ocd_env['LIBUSB_DEBUG'] = '1' - - for _ in range(1, MAX_RETRIES + 1): - try: - self.proc = pexpect.spawn( - command='openocd', - args=['-s', openocd_scripts] + debug_args.split(), - timeout=5, - encoding='utf-8', - codec_errors='ignore', - env=ocd_env, - ) - if self.proc and self.proc.isalive(): - self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) - return self - except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT) as e: - logging.error('Error running OpenOCD: %s', str(e)) - if self.proc and self.proc.isalive(): - self.proc.terminate() - time.sleep(RETRY_DELAY) - - logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) - return None - - def connect_telnet(self) -> None: - for attempt in range(1, MAX_RETRIES + 1): - try: - self.telnet = Telnet('127.0.0.1', TELNET_PORT, 5) - break - except ConnectionRefusedError as e: - logging.error('Error telnet connection: %s in attempt:%d', e, attempt) - time.sleep(1) - else: - raise ConnectionRefusedError - - def write(self, s: str) -> Any: - if self.telnet is None: - logging.error('Telnet connection is not established.') - return '' - resp = self.telnet.read_very_eager() - self.telnet.write(to_bytes(s, '\n')) - resp += self.telnet.read_until(b'>') - return to_str(resp) - - def apptrace_wait_stop(self, timeout: int = 30) -> None: - stopped = False - end_before = time.time() + timeout - while not stopped: - cmd_out = self.write('esp apptrace status') - for line in cmd_out.splitlines(): - if line.startswith('Tracing is STOPPED.'): - stopped = True - break - if not stopped and time.time() > end_before: - raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') - time.sleep(1) - - def kill(self) -> None: - # Check if the process is still running - if self.proc and self.proc.isalive(): - self.proc.terminate() - self.proc.kill(signal.SIGKILL) - - -def _test_examples_sysview_tracing(dut: IdfDut) -> None: +def _test_examples_sysview_tracing(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: # Construct trace log paths trace_log = [ os.path.join(dut.logdir, 'sys_log0.svdat') # pylint: disable=protected-access @@ -138,36 +37,28 @@ def _test_examples_sysview_tracing(dut: IdfDut) -> None: dut.expect(re.compile(rb'example: Task\[0x[0-9A-Fa-f]+\]: received event \d+'), timeout=30) dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) - openocd = OpenOCD(dut).run() - assert openocd - try: - openocd.connect_telnet() - openocd.write('log_output {}'.format(openocd.log_file)) + with openocd_dut.run() as openocd, open(gdb_logfile, 'w') as gdb_log, pexpect.spawn( + f'idf.py -B {dut.app.binary_path} gdb --batch -x {gdbinit}', + timeout=60, + logfile=gdb_log, + encoding='utf-8', + codec_errors='ignore', + ) as p: + p.expect_exact('hit Breakpoint 1, app_main ()') + dut.expect('example: Created task') # dut has been restarted by gdb since the last dut.expect() + dut_expect_task_event() - with open(gdb_logfile, 'w') as gdb_log, pexpect.spawn( - f'idf.py -B {dut.app.binary_path} gdb --batch -x {gdbinit}', - timeout=60, - logfile=gdb_log, - encoding='utf-8', - codec_errors='ignore', - ) as p: - p.expect_exact('hit Breakpoint 1, app_main ()') - dut.expect('example: Created task') # dut has been restarted by gdb since the last dut.expect() - dut_expect_task_event() - - # Do a sleep while sysview samples are captured. - time.sleep(3) - openocd.write('esp sysview stop') - finally: - openocd.kill() + # Do a sleep while sysview samples are captured. + time.sleep(3) + openocd.write('esp sysview stop') @pytest.mark.esp32 @pytest.mark.esp32s2 @pytest.mark.esp32c2 @pytest.mark.jtag -def test_examples_sysview_tracing(dut: IdfDut) -> None: - _test_examples_sysview_tracing(dut) +def test_examples_sysview_tracing(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_examples_sysview_tracing(openocd_dut, dut) @pytest.mark.esp32s3 @@ -175,5 +66,5 @@ def test_examples_sysview_tracing(dut: IdfDut) -> None: @pytest.mark.esp32c6 @pytest.mark.esp32h2 @pytest.mark.usb_serial_jtag -def test_examples_sysview_tracing_usj(dut: IdfDut) -> None: - _test_examples_sysview_tracing(dut) +def test_examples_sysview_tracing_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_examples_sysview_tracing(openocd_dut, dut) diff --git a/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py b/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py index fa8d8e6d13..0fb8544fa3 100644 --- a/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py +++ b/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py @@ -1,119 +1,17 @@ # SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 -import json -import logging import os.path -import signal -import time -from telnetlib import Telnet -from typing import Any -from typing import Optional +import typing import pexpect.fdpexpect import pytest -from pytest_embedded.utils import to_bytes -from pytest_embedded.utils import to_str from pytest_embedded_idf import IdfDut -MAX_RETRIES = 3 -RETRY_DELAY = 1 -TELNET_PORT = 4444 +if typing.TYPE_CHECKING: + from conftest import OpenOCD -class OpenOCD: - def __init__(self, dut: 'IdfDut'): - self.dut = dut - self.telnet: Optional[Telnet] = None - self.log_file = os.path.join(self.dut.logdir, 'ocd.txt') - self.proc: Optional[pexpect.spawn] = None - - def run(self) -> Optional['OpenOCD']: - desc_path = os.path.join(self.dut.app.binary_path, 'project_description.json') - - try: - with open(desc_path, 'r') as f: - project_desc = json.load(f) - except FileNotFoundError: - logging.error('Project description file not found at %s', desc_path) - return None - - openocd_scripts = os.getenv('OPENOCD_SCRIPTS') - if not openocd_scripts: - logging.error('OPENOCD_SCRIPTS environment variable is not set.') - return None - - debug_args = project_desc.get('debug_arguments_openocd') - if not debug_args: - logging.error("'debug_arguments_openocd' key is missing in project_description.json") - return None - - # For debug purposes, make the value '4' - ocd_env = os.environ.copy() - ocd_env['LIBUSB_DEBUG'] = '1' - - for _ in range(1, MAX_RETRIES + 1): - try: - self.proc = pexpect.spawn( - command='openocd', - args=['-s', openocd_scripts] + debug_args.split(), - timeout=5, - encoding='utf-8', - codec_errors='ignore', - env=ocd_env, - ) - if self.proc and self.proc.isalive(): - self.proc.expect_exact('Info : Listening on port 3333 for gdb connections', timeout=5) - return self - except (pexpect.exceptions.EOF, pexpect.exceptions.TIMEOUT) as e: - logging.error('Error running OpenOCD: %s', str(e)) - if self.proc and self.proc.isalive(): - self.proc.terminate() - time.sleep(RETRY_DELAY) - - logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) - return None - - def connect_telnet(self) -> None: - for attempt in range(1, MAX_RETRIES + 1): - try: - self.telnet = Telnet('127.0.0.1', TELNET_PORT, 5) - break - except ConnectionRefusedError as e: - logging.error('Error telnet connection: %s in attempt:%d', e, attempt) - time.sleep(1) - else: - raise ConnectionRefusedError - - def write(self, s: str) -> Any: - if self.telnet is None: - logging.error('Telnet connection is not established.') - return '' - resp = self.telnet.read_very_eager() - self.telnet.write(to_bytes(s, '\n')) - resp += self.telnet.read_until(b'>') - return to_str(resp) - - def apptrace_wait_stop(self, timeout: int = 30) -> None: - stopped = False - end_before = time.time() + timeout - while not stopped: - cmd_out = self.write('esp apptrace status') - for line in cmd_out.splitlines(): - if line.startswith('Tracing is STOPPED.'): - stopped = True - break - if not stopped and time.time() > end_before: - raise pexpect.TIMEOUT('Failed to wait for apptrace stop!') - time.sleep(1) - - def kill(self) -> None: - # Check if the process is still running - if self.proc and self.proc.isalive(): - self.proc.terminate() - self.proc.kill(signal.SIGKILL) - - -def _test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: +def _test_examples_sysview_tracing_heap_log(openocd_dut: 'OpenOCD', idf_path: str, dut: IdfDut) -> None: # Construct trace log paths trace_log = [ os.path.join(dut.logdir, 'heap_log0.svdat') # pylint: disable=protected-access @@ -134,23 +32,15 @@ def _test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: f_w.write(line) dut.expect_exact('example: Ready for OpenOCD connection', timeout=5) - openocd = OpenOCD(dut).run() - assert openocd - try: - openocd.connect_telnet() - openocd.write('log_output {}'.format(openocd.log_file)) - - with open(gdb_logfile, 'w') as gdb_log, pexpect.spawn( - f'idf.py -B {dut.app.binary_path} gdb --batch -x {gdbinit}', - timeout=60, - logfile=gdb_log, - encoding='utf-8', - codec_errors='ignore', - ) as p: - # Wait for sysview files to be generated - p.expect_exact('Tracing is STOPPED') - finally: - openocd.kill() + with openocd_dut.run(), open(gdb_logfile, 'w') as gdb_log, pexpect.spawn( + f'idf.py -B {dut.app.binary_path} gdb --batch -x {gdbinit}', + timeout=60, + logfile=gdb_log, + encoding='utf-8', + codec_errors='ignore', + ) as p: + # Wait for sysview files to be generated + p.expect_exact('Tracing is STOPPED') # Process sysview trace logs command = [os.path.join(idf_path, 'tools', 'esp_app_trace', 'sysviewtrace_proc.py'), '-p'] + trace_log @@ -170,8 +60,8 @@ def _test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: @pytest.mark.esp32 @pytest.mark.esp32s2 @pytest.mark.esp32c2 -def test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: - _test_examples_sysview_tracing_heap_log(idf_path, dut) +def test_examples_sysview_tracing_heap_log(openocd_dut: 'OpenOCD', idf_path: str, dut: IdfDut) -> None: + _test_examples_sysview_tracing_heap_log(openocd_dut, idf_path, dut) @pytest.mark.parametrize('config', ['app_trace_jtag'], indirect=True) @@ -180,5 +70,5 @@ def test_examples_sysview_tracing_heap_log(idf_path: str, dut: IdfDut) -> None: @pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 -def test_examples_sysview_tracing_heap_log_usj(idf_path: str, dut: IdfDut) -> None: - _test_examples_sysview_tracing_heap_log(idf_path, dut) +def test_examples_sysview_tracing_heap_log_usj(openocd_dut: 'OpenOCD', idf_path: str, dut: IdfDut) -> None: + _test_examples_sysview_tracing_heap_log(openocd_dut, idf_path, dut) From cbb2896bc7c344df25081c358fe2ac094409b8e1 Mon Sep 17 00:00:00 2001 From: Samuel Obuch Date: Tue, 20 May 2025 17:29:33 +0200 Subject: [PATCH 3/7] ci: use shared OpenOCD class for GDB test app --- tools/test_apps/system/.build-test-rules.yml | 2 +- tools/test_apps/system/gdb/pytest_gdb.py | 104 ++++--------------- 2 files changed, 24 insertions(+), 82 deletions(-) diff --git a/tools/test_apps/system/.build-test-rules.yml b/tools/test_apps/system/.build-test-rules.yml index 3341706593..3e0d42b4bf 100644 --- a/tools/test_apps/system/.build-test-rules.yml +++ b/tools/test_apps/system/.build-test-rules.yml @@ -37,7 +37,7 @@ tools/test_apps/system/g1_components: tools/test_apps/system/gdb: disable_test: - - if: IDF_TARGET in ["esp32s3", "esp32c2", "esp32c3", "esp32c5", "esp32c61", "esp32p4"] + - if: IDF_TARGET == "esp32p4" temporary: true reason: lack of runners diff --git a/tools/test_apps/system/gdb/pytest_gdb.py b/tools/test_apps/system/gdb/pytest_gdb.py index f43bb74556..29e56dd159 100644 --- a/tools/test_apps/system/gdb/pytest_gdb.py +++ b/tools/test_apps/system/gdb/pytest_gdb.py @@ -1,73 +1,18 @@ # SPDX-FileCopyrightText: 2022-2024 Espressif Systems (Shanghai) CO LTD # SPDX-License-Identifier: Unlicense OR CC0-1.0 -import json -import logging import os import re -import subprocess -import time -from subprocess import Popen -from typing import Optional +import typing import pexpect import pytest from pytest_embedded_idf import IdfDut - -MAX_RETRIES = 3 -RETRY_DELAY = 3 # seconds +if typing.TYPE_CHECKING: + from conftest import OpenOCD -def run_openocd(dut: IdfDut) -> Optional[Popen]: - - desc_path = os.path.join(dut.app.binary_path, 'project_description.json') - try: - with open(desc_path, 'r') as f: - project_desc = json.load(f) - except FileNotFoundError: - logging.error('Project description file not found at %s', desc_path) - return None - - openocd_scripts = os.getenv('OPENOCD_SCRIPTS') - if not openocd_scripts: - logging.error('OPENOCD_SCRIPTS environment variable is not set.') - return None - - debug_args = project_desc.get('debug_arguments_openocd') - if not debug_args: - logging.error("'debug_arguments_openocd' key is missing in project_description.json") - return None - - cmd = ['openocd'] + ['-s', openocd_scripts] + debug_args.split() - - # For debug purpose, make the value '4' - ocd_env = os.environ.copy() - ocd_env['LIBUSB_DEBUG'] = '1' - - for attempt in range(1, MAX_RETRIES + 1): - logging.info('Attempt %d: Running %s', attempt, cmd) - with open(os.path.join(dut.logdir, 'ocd.txt'), 'w') as ocd_log: - try: - ocd = subprocess.Popen(cmd, stdout=ocd_log, stderr=ocd_log, env=ocd_env) - time.sleep(1) - - # Check if the process is running successfully - if ocd.poll() is None: - return ocd - else: - logging.error('OpenOCD exited with error code %d', ocd.returncode) - except subprocess.SubprocessError as e: - logging.error('Error running OpenOCD: %s', e) - - logging.warning("OpenOCD couldn't be run. Retrying in %d seconds...", RETRY_DELAY) - time.sleep(RETRY_DELAY) - - logging.error('Failed to run OpenOCD after %d attempts.', MAX_RETRIES) - - return None - - -def _test_idf_gdb(dut: IdfDut) -> None: +def _test_idf_gdb(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: # Need to wait a moment to connect via OpenOCD after the hard reset happened. # Along with this check that app runs ok dut.expect('Hello world!') @@ -75,35 +20,32 @@ def _test_idf_gdb(dut: IdfDut) -> None: # Don't need to have output from UART anymore dut.serial.stop_redirect_thread() - ocd = run_openocd(dut) - assert ocd - - try: - with open(os.path.join(dut.logdir, 'gdb.txt'), 'w') as gdb_log, \ - pexpect.spawn(f'idf.py -B {dut.app.binary_path} gdb --batch', - timeout=60, - logfile=gdb_log, - encoding='utf-8', - codec_errors='ignore') as p: - p.expect(re.compile(r'add symbol table from file.*bootloader.elf')) - p.expect(re.compile(r'add symbol table from file.*rom.elf')) # if fail here: add target support here https://github.com/espressif/esp-rom-elfs - p.expect_exact('hit Temporary breakpoint 1, app_main ()') - finally: - # Check if the process is still running - if ocd.poll() is None: - ocd.terminate() - ocd.kill() + with openocd_dut.run(), open(os.path.join(dut.logdir, 'gdb.txt'), 'w') as gdb_log, pexpect.spawn( + f'idf.py -B {dut.app.binary_path} gdb --batch', + timeout=60, + logfile=gdb_log, + encoding='utf-8', + codec_errors='ignore', + ) as p: + p.expect(re.compile(r'add symbol table from file.*bootloader.elf')) + p.expect( + re.compile(r'add symbol table from file.*rom.elf') + ) # if fail here: add target support here https://github.com/espressif/esp-rom-elfs + p.expect_exact('hit Temporary breakpoint 1, app_main ()') @pytest.mark.esp32 @pytest.mark.esp32s2 +@pytest.mark.esp32c2 @pytest.mark.jtag -def test_idf_gdb(dut: IdfDut) -> None: - _test_idf_gdb(dut) +def test_idf_gdb(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_idf_gdb(openocd_dut, dut) +@pytest.mark.esp32s3 +@pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 @pytest.mark.usb_serial_jtag -def test_idf_gdb_usj(dut: IdfDut) -> None: - _test_idf_gdb(dut) +def test_idf_gdb_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: + _test_idf_gdb(openocd_dut, dut) From a64b16ceb6d9c88568eb320821ad0e56c9379199 Mon Sep 17 00:00:00 2001 From: Erhan Kurubas Date: Fri, 23 May 2025 09:20:36 +0200 Subject: [PATCH 4/7] feat(tools): add esp32c3 rev1.1 rom version string --- tools/idf_py_actions/roms.json | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tools/idf_py_actions/roms.json b/tools/idf_py_actions/roms.json index 40a4ff930a..f12012ee59 100644 --- a/tools/idf_py_actions/roms.json +++ b/tools/idf_py_actions/roms.json @@ -42,6 +42,11 @@ "rev": 3, "build_date_str_addr": "0x3ff1a374", "build_date_str": "Feb 7 2021" + }, + { + "rev": 101, + "build_date_str_addr": "0x3ff1a3dc", + "build_date_str": "Mar 1 2023" } ], "esp32c6": [ From f4914946621657c206c7869aa7af6d71f3e67321 Mon Sep 17 00:00:00 2001 From: Samuel Obuch Date: Tue, 3 Jun 2025 12:55:03 +0200 Subject: [PATCH 5/7] ci: enable example tests for esp32p4 --- examples/system/.build-test-rules.yml | 20 ------------------- examples/system/app_trace_basic/README.md | 4 ++-- .../app_trace_basic/pytest_app_trace_basic.py | 1 + examples/system/gcov/pytest_gcov.py | 1 + .../sysview_tracing/pytest_sysview_tracing.py | 1 + .../pytest_sysview_tracing_heap_log.py | 1 + tools/test_apps/system/.build-test-rules.yml | 2 +- 7 files changed, 7 insertions(+), 23 deletions(-) diff --git a/examples/system/.build-test-rules.yml b/examples/system/.build-test-rules.yml index d86e63057d..e5bc622e23 100644 --- a/examples/system/.build-test-rules.yml +++ b/examples/system/.build-test-rules.yml @@ -1,11 +1,5 @@ # Documentation: .gitlab/ci/README.md#manifest-file-to-control-the-buildtest-apps -examples/system/app_trace_basic: - disable: - - if: IDF_TARGET in ["esp32p4"] - temporary: true - reason: lack of runner. - examples/system/base_mac_address: depends_components: - esp_hw_support @@ -78,12 +72,6 @@ examples/system/freertos: depends_components: - freertos -examples/system/gcov: - disable_test: - - if: IDF_TARGET == "esp32p4" - temporary: true - reason: lack of runners - examples/system/gdbstub: disable: - if: IDF_TARGET == "esp32p4" @@ -239,18 +227,10 @@ examples/system/select: examples/system/sysview_tracing: disable: - if: SOC_GPTIMER_SUPPORTED != 1 - disable_test: - - if: IDF_TARGET == "esp32p4" - temporary: true - reason: lack of runners examples/system/sysview_tracing_heap_log: disable: - if: SOC_GPTIMER_SUPPORTED != 1 - disable_test: - - if: IDF_TARGET == "esp32p4" - temporary: true - reason: lack of runners examples/system/task_watchdog: disable: diff --git a/examples/system/app_trace_basic/README.md b/examples/system/app_trace_basic/README.md index 71560d48cd..ab049fa32a 100644 --- a/examples/system/app_trace_basic/README.md +++ b/examples/system/app_trace_basic/README.md @@ -1,5 +1,5 @@ -| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-S2 | ESP32-S3 | -| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | +| Supported Targets | ESP32 | ESP32-C2 | ESP32-C3 | ESP32-C6 | ESP32-H2 | ESP32-P4 | ESP32-S2 | ESP32-S3 | +| ----------------- | ----- | -------- | -------- | -------- | -------- | -------- | -------- | -------- | # Application Level Tracing Example (Basic) diff --git a/examples/system/app_trace_basic/pytest_app_trace_basic.py b/examples/system/app_trace_basic/pytest_app_trace_basic.py index 5f2128ac73..6e02dcd26d 100644 --- a/examples/system/app_trace_basic/pytest_app_trace_basic.py +++ b/examples/system/app_trace_basic/pytest_app_trace_basic.py @@ -57,6 +57,7 @@ def test_examples_app_trace_basic(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: @pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 +@pytest.mark.esp32p4 @pytest.mark.usb_serial_jtag def test_examples_app_trace_basic_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: _test_examples_app_trace_basic(openocd_dut, dut) diff --git a/examples/system/gcov/pytest_gcov.py b/examples/system/gcov/pytest_gcov.py index b69e842b03..de6182808e 100644 --- a/examples/system/gcov/pytest_gcov.py +++ b/examples/system/gcov/pytest_gcov.py @@ -83,6 +83,7 @@ def test_gcov(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: @pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 +@pytest.mark.esp32p4 @pytest.mark.usb_serial_jtag def test_gcov_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: _test_gcov(openocd_dut, dut) diff --git a/examples/system/sysview_tracing/pytest_sysview_tracing.py b/examples/system/sysview_tracing/pytest_sysview_tracing.py index cbe90bc581..4bcb57921d 100644 --- a/examples/system/sysview_tracing/pytest_sysview_tracing.py +++ b/examples/system/sysview_tracing/pytest_sysview_tracing.py @@ -65,6 +65,7 @@ def test_examples_sysview_tracing(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: @pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 +@pytest.mark.esp32p4 @pytest.mark.usb_serial_jtag def test_examples_sysview_tracing_usj(openocd_dut: 'OpenOCD', dut: IdfDut) -> None: _test_examples_sysview_tracing(openocd_dut, dut) diff --git a/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py b/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py index 0fb8544fa3..6b03bd589c 100644 --- a/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py +++ b/examples/system/sysview_tracing_heap_log/pytest_sysview_tracing_heap_log.py @@ -70,5 +70,6 @@ def test_examples_sysview_tracing_heap_log(openocd_dut: 'OpenOCD', idf_path: str @pytest.mark.esp32c3 @pytest.mark.esp32c6 @pytest.mark.esp32h2 +@pytest.mark.esp32p4 def test_examples_sysview_tracing_heap_log_usj(openocd_dut: 'OpenOCD', idf_path: str, dut: IdfDut) -> None: _test_examples_sysview_tracing_heap_log(openocd_dut, idf_path, dut) diff --git a/tools/test_apps/system/.build-test-rules.yml b/tools/test_apps/system/.build-test-rules.yml index 3e0d42b4bf..8c4620262e 100644 --- a/tools/test_apps/system/.build-test-rules.yml +++ b/tools/test_apps/system/.build-test-rules.yml @@ -39,7 +39,7 @@ tools/test_apps/system/gdb: disable_test: - if: IDF_TARGET == "esp32p4" temporary: true - reason: lack of runners + reason: not supported yet # TODO: IDF-13142 tools/test_apps/system/gdb_loadable_elf: disable_test: From 018ab9d4e5b9a216cda9d19731eddc226e0ba111 Mon Sep 17 00:00:00 2001 From: Samuel Obuch Date: Thu, 5 Jun 2025 21:17:52 +0200 Subject: [PATCH 6/7] fix(sysview): fix timestamp freq when not using APB clock --- .../esp/SEGGER_SYSVIEW_Config_FreeRTOS.c | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/components/app_trace/sys_view/Sample/FreeRTOSV10.4/Config/esp/SEGGER_SYSVIEW_Config_FreeRTOS.c b/components/app_trace/sys_view/Sample/FreeRTOSV10.4/Config/esp/SEGGER_SYSVIEW_Config_FreeRTOS.c index fcf33d365a..86aef3fd00 100644 --- a/components/app_trace/sys_view/Sample/FreeRTOSV10.4/Config/esp/SEGGER_SYSVIEW_Config_FreeRTOS.c +++ b/components/app_trace/sys_view/Sample/FreeRTOSV10.4/Config/esp/SEGGER_SYSVIEW_Config_FreeRTOS.c @@ -64,6 +64,7 @@ Revision: $Rev: 7745 $ #include "esp_app_trace.h" #include "esp_app_trace_util.h" #include "esp_intr_alloc.h" +#include "esp_clk_tree.h" #include "esp_cpu.h" #include "soc/soc.h" #include "soc/interrupts.h" @@ -104,9 +105,6 @@ extern const SEGGER_SYSVIEW_OS_API SYSVIEW_X_OS_TraceAPI; // Timer group timer divisor #define SYSVIEW_TIMER_DIV 2 -// Frequency of the timestamp, using APB as GPTimer source clock -#define SYSVIEW_TIMESTAMP_FREQ (esp_clk_apb_freq() / SYSVIEW_TIMER_DIV) - // GPTimer handle gptimer_handle_t s_sv_gptimer; @@ -175,30 +173,38 @@ static void _cbSendSystemDesc(void) { * ********************************************************************** */ -static void SEGGER_SYSVIEW_TS_Init(void) +static int SEGGER_SYSVIEW_TS_Init(void) { /* We only need to initialize something if we use Timer Group. * esp_timer and ccount can be used as is. */ #if TS_USE_TIMERGROUP + // get clock source frequency + uint32_t counter_src_hz = 0; + ESP_ERROR_CHECK(esp_clk_tree_src_get_freq_hz( + (soc_module_clk_t)GPTIMER_CLK_SRC_DEFAULT, + ESP_CLK_TREE_SRC_FREQ_PRECISION_CACHED, &counter_src_hz)); gptimer_config_t config = { .clk_src = GPTIMER_CLK_SRC_DEFAULT, .direction = GPTIMER_COUNT_UP, - .resolution_hz = SYSVIEW_TIMESTAMP_FREQ, + .resolution_hz = counter_src_hz / SYSVIEW_TIMER_DIV, }; // pick any free GPTimer instance ESP_ERROR_CHECK(gptimer_new_timer(&config, &s_sv_gptimer)); /* Start counting */ gptimer_enable(s_sv_gptimer); gptimer_start(s_sv_gptimer); + return config.resolution_hz; +#else + return SYSVIEW_TIMESTAMP_FREQ; #endif // TS_USE_TIMERGROUP } void SEGGER_SYSVIEW_Conf(void) { U32 disable_evts = 0; - SEGGER_SYSVIEW_TS_Init(); - SEGGER_SYSVIEW_Init(SYSVIEW_TIMESTAMP_FREQ, SYSVIEW_CPU_FREQ, + int timestamp_freq = SEGGER_SYSVIEW_TS_Init(); + SEGGER_SYSVIEW_Init(timestamp_freq, SYSVIEW_CPU_FREQ, &SYSVIEW_X_OS_TraceAPI, _cbSendSystemDesc); SEGGER_SYSVIEW_SetRAMBase(SYSVIEW_RAM_BASE); From 7b09c3682d5f88d0e9df0b15acaffc3f0ffc20c8 Mon Sep 17 00:00:00 2001 From: Fu Hanxi Date: Wed, 16 Jul 2025 08:41:17 +0200 Subject: [PATCH 7/7] ci: use fixed telnetlib since python 3.13 removed this from stdlib --- conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 3ecddffd81..b809ab0f1e 100644 --- a/conftest.py +++ b/conftest.py @@ -28,7 +28,6 @@ import time import typing as t import zipfile from copy import deepcopy -from telnetlib import Telnet from urllib.parse import quote import common_test_methods # noqa: F401 @@ -53,6 +52,7 @@ from pytest_embedded.utils import to_bytes from pytest_embedded.utils import to_str from pytest_embedded_idf.dut import IdfDut from pytest_embedded_idf.unity_tester import CaseTester +from pytest_embedded_jtag._telnetlib.telnetlib import Telnet # python 3.13 removed telnetlib, use this instead ############