From 98d1f7de4bb439ee217290d2659b3484d7b35b8d Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 10 Jan 2023 17:45:59 +0100 Subject: [PATCH 1/3] tools: fix idf_monitor.py not working on macOS for Linux target Since the introduction of PCAddressMatcher, the executable produced by the build system is passed to elftools.elf.elffile.ELFFile. However on macOS, native executables are not ELF files, so the ELFFile class raises a rather unhelpful AssertionError exception. Given that the rest of the idf_monitor.py doesn't have assumptions that the "elf_file" argument is an ELF file (rather than just an executable), check if the file is a real ELF file inside PCAddressMatcher. --- tools/idf_monitor_base/pc_address_matcher.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/tools/idf_monitor_base/pc_address_matcher.py b/tools/idf_monitor_base/pc_address_matcher.py index 9a97e29483..c0c69dc911 100644 --- a/tools/idf_monitor_base/pc_address_matcher.py +++ b/tools/idf_monitor_base/pc_address_matcher.py @@ -7,8 +7,8 @@ from elftools.elf.elffile import ELFFile class PcAddressMatcher(object): """ - Class for detecting potentional addresses which will consequently run through the external addr2line command to - indentify and print information about it. + Class for detecting potential addresses which will consequently run through the external addr2line command to + identify and print information about it. The input to this class is the path to the ELF file. Addresses of sections with executable flag are stored and used later for lookup. @@ -18,6 +18,14 @@ class PcAddressMatcher(object): self.intervals = [] try: with open(elf_path, 'rb') as f: + # Is this an ELF file? + elf_magic = f.read(4) + if elf_magic != b'\x7fELF': + # Probably not an ELF file + # (could be Mach-O format on macOS, for example) + raise NotImplementedError() + f.seek(0) + elf = ELFFile(f) for section in elf.iter_sections(): @@ -30,13 +38,15 @@ class PcAddressMatcher(object): except FileNotFoundError: # ELF file is just an optional argument pass + except NotImplementedError: + pass # sort them in order to have faster lookup self.intervals = sorted(self.intervals) def is_executable_address(self, addr): # type: (int) -> bool """ - Returns True/False depending on of "addr" is in one of the ELF sections with executable flag set. + Returns True/False if "addr" is in one of the ELF sections with executable flag set. """ for start, end in self.intervals: From ec8f38c9da3905fb529d773f523c12790b5e728b Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 10 Jan 2023 17:47:20 +0100 Subject: [PATCH 2/3] build system: generate the partition table by default for linux target This fixes the issue that "idf.py partition-table" had to be run manually in order for the partition table to be generated, when building for linux target. --- .../partition_api_test/CMakeLists.txt | 2 -- .../host_test/partition_api_test/README.md | 3 +-- .../host_test/nvs_host_test/CMakeLists.txt | 2 -- components/partition_table/CMakeLists.txt | 18 ++++++++++++++++++ components/spiffs/host_test/CMakeLists.txt | 3 ++- 5 files changed, 21 insertions(+), 7 deletions(-) diff --git a/components/esp_partition/host_test/partition_api_test/CMakeLists.txt b/components/esp_partition/host_test/partition_api_test/CMakeLists.txt index 3986bdb314..469a614a14 100644 --- a/components/esp_partition/host_test/partition_api_test/CMakeLists.txt +++ b/components/esp_partition/host_test/partition_api_test/CMakeLists.txt @@ -7,5 +7,3 @@ set(COMPONENTS main) list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") project(partition_api_test) - -add_dependencies(partition_api_test.elf partition-table) diff --git a/components/esp_partition/host_test/partition_api_test/README.md b/components/esp_partition/host_test/partition_api_test/README.md index 8e2e38e920..73c2e9762f 100644 --- a/components/esp_partition/host_test/partition_api_test/README.md +++ b/components/esp_partition/host_test/partition_api_test/README.md @@ -8,9 +8,8 @@ Source the IDF environment as usual. Once this is done, build the application: ```bash -idf.py build partition-table +idf.py build ``` -Note that for the time being, `partition-table` target needs to be built manually. # Run ```bash diff --git a/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt b/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt index 655d83dcf0..cd3f3ebece 100644 --- a/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt +++ b/components/nvs_flash/host_test/nvs_host_test/CMakeLists.txt @@ -8,5 +8,3 @@ set(COMPONENTS main) list(APPEND EXTRA_COMPONENT_DIRS "$ENV{IDF_PATH}/tools/mocks/freertos/") project(nvs_host_test) - -add_dependencies(nvs_host_test.elf partition-table) diff --git a/components/partition_table/CMakeLists.txt b/components/partition_table/CMakeLists.txt index 31260c1f12..f386028612 100644 --- a/components/partition_table/CMakeLists.txt +++ b/components/partition_table/CMakeLists.txt @@ -96,6 +96,24 @@ else() "Either change partition table in menuconfig or create this input file.") endif() +if(${target} STREQUAL "linux" AND EXISTS ${partition_csv}) + # partition-table target is normally invoked as a dependency of 'flash' target. + # However, when building for "linux" target, 'flash' target doesn't exist, + # so we need to attach the partition table build to the executable target. + # + # The problem is that the executable target is not yet defined + # when the component CMakeLists.txt file is evaluated, so we + # can only get it as a generator expression. But generator expressions + # can't be used in 'add_dependencies': + # https://gitlab.kitware.com/cmake/cmake/-/issues/19467 + # + # Therefore attach partition-table to the internal __idf_build_target + # target. This is a hack, since that target name is an implementation detail + # of the build system. + + add_dependencies(__idf_build_target partition-table) +endif() + # Add signing steps if(CONFIG_SECURE_SIGNED_APPS_ECDSA_SCHEME) if(CONFIG_SECURE_BOOT_BUILD_SIGNED_BINARIES) diff --git a/components/spiffs/host_test/CMakeLists.txt b/components/spiffs/host_test/CMakeLists.txt index 03f7125aed..81a2485a61 100644 --- a/components/spiffs/host_test/CMakeLists.txt +++ b/components/spiffs/host_test/CMakeLists.txt @@ -22,4 +22,5 @@ set_property( DIRECTORY APPEND PROPERTY ADDITIONAL_CLEAN_FILES "../image.bin") -add_dependencies(host_test_spiffs.elf partition-table image.bin) + +add_dependencies(host_test_spiffs.elf image.bin) From e7540dbe0e8e50e185f5564b8ad3ae6cd89a21f0 Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 10 Jan 2023 18:08:52 +0100 Subject: [PATCH 3/3] docs: remove the outdated "IDF monitor doesn't work on linux" notes --- .../esp_partition/host_test/partition_api_test/README.md | 2 +- components/esp_rom/host_test/rom_test/README.md | 6 ++---- components/log/host_test/log_test/README.md | 6 ++---- components/nvs_flash/host_test/nvs_page_test/README.md | 6 ++---- components/spiffs/host_test/README.md | 2 +- 5 files changed, 8 insertions(+), 14 deletions(-) diff --git a/components/esp_partition/host_test/partition_api_test/README.md b/components/esp_partition/host_test/partition_api_test/README.md index 73c2e9762f..10b893d003 100644 --- a/components/esp_partition/host_test/partition_api_test/README.md +++ b/components/esp_partition/host_test/partition_api_test/README.md @@ -13,5 +13,5 @@ idf.py build # Run ```bash -`build/partition_api_test.elf` +idf.py monitor ``` diff --git a/components/esp_rom/host_test/rom_test/README.md b/components/esp_rom/host_test/rom_test/README.md index 4e99101c90..22e67c9eae 100644 --- a/components/esp_rom/host_test/rom_test/README.md +++ b/components/esp_rom/host_test/rom_test/README.md @@ -19,10 +19,8 @@ First, make sure that the target is set to Linux. Run `idf.py --preview set-targ ## Run -IDF monitor doesn't work yet for Linux. You have to run the app manually: - ```bash -./build/test_rom_host.elf +idf.py monitor ``` ## Example Output @@ -30,7 +28,7 @@ IDF monitor doesn't work yet for Linux. You have to run the app manually: Ideally, all tests pass, which is indicated by "All tests passed" in the last line: ```bash -$ ./build/test_rom_host.elf +$ idf.py monitor test =============================================================================== All tests passed (8 assertions in 6 test cases) diff --git a/components/log/host_test/log_test/README.md b/components/log/host_test/log_test/README.md index 4c37b52c7d..47d25b4716 100644 --- a/components/log/host_test/log_test/README.md +++ b/components/log/host_test/log_test/README.md @@ -19,10 +19,8 @@ First, make sure that the target is set to Linux. Run `idf.py --preview set-targ ## Run -IDF monitor doesn't work yet for Linux. You have to run the app manually: - ```bash -./build/test_log_host.elf +idf.py monitor ``` ## Example Output @@ -30,7 +28,7 @@ IDF monitor doesn't work yet for Linux. You have to run the app manually: Ideally, all tests pass, which is indicated by "All tests passed" in the last line: ```bash -$ ./build/test_log_host.elf +$ idf.py monitor =============================================================================== All tests passed (8 assertions in 6 test cases) ``` diff --git a/components/nvs_flash/host_test/nvs_page_test/README.md b/components/nvs_flash/host_test/nvs_page_test/README.md index cf48e50a71..65699e802f 100644 --- a/components/nvs_flash/host_test/nvs_page_test/README.md +++ b/components/nvs_flash/host_test/nvs_page_test/README.md @@ -33,10 +33,8 @@ First, make sure that the target is set to Linux. Run `idf.py --preview set-targ ## Run -IDF monitor doesn't work yet for Linux. You have to run the app manually: - ```bash -./build/host_nvs_page_test.elf +idf.py monitor ``` ## Coverage @@ -48,7 +46,7 @@ To generate the coverage, run: `idf.py coverage`. Afterwards, you can view the c Ideally, all tests pass, which is indicated by the last two log lines after the dashed line: ```bash -build/host_nvs_page_test.elf +$ idf.py monitor ../main/nvs_page_test.cpp:880:test_Page_load_reading_header_fails:PASS ../main/nvs_page_test.cpp:881:test_Page_load_reading_data_fails:PASS ../main/nvs_page_test.cpp:882:test_Page_load__uninitialized_page_has_0xfe:PASS diff --git a/components/spiffs/host_test/README.md b/components/spiffs/host_test/README.md index dc6f6df5fd..a545955fa3 100644 --- a/components/spiffs/host_test/README.md +++ b/components/spiffs/host_test/README.md @@ -13,5 +13,5 @@ idf.py build # Run ```bash -build/host_test_spiffs.elf +idf.py monitor ```