From 2e9c586ba7345ae7e29c5171f7f98ce31fd8ffb5 Mon Sep 17 00:00:00 2001 From: Jeremy <51220084+jeremy-rifkin@users.noreply.github.com> Date: Sat, 10 Feb 2024 19:31:35 -0600 Subject: [PATCH] Printbug --- .github/workflows/test.yml | 3 +++ ci/test-all-configs.py | 11 ++++++++++- src/binary/mach-o.hpp | 6 +++++- src/binary/object.hpp | 4 +++- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5090241..dd51aa7 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -80,6 +80,9 @@ jobs: make -j sudo make install cd ../../cpptrace + file /usr/lib/dyld + otool -l /usr/lib/dyld + uname -a - name: dependencies run: | pip3 install colorama diff --git a/ci/test-all-configs.py b/ci/test-all-configs.py index 1930bfb..fd96fc3 100644 --- a/ci/test-all-configs.py +++ b/ci/test-all-configs.py @@ -110,7 +110,7 @@ def output_matches(raw_output: str, params: Tuple[str]): return not errored -def run_command(*args: List[str]): +def run_command(*args: List[str], always_output=False): global failed print(f"{Fore.CYAN}{Style.BRIGHT}Running Command \"{' '.join(args)}\"{Style.RESET_ALL}") p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) @@ -126,6 +126,11 @@ def run_command(*args: List[str]): return False else: print(f"{Fore.GREEN}{Style.BRIGHT}Command succeeded{Style.RESET_ALL}") + if always_output: + print("stdout:") + print(stdout.decode("utf-8"), end="") + print("stderr:") + print(stderr.decode("utf-8"), end="") return True def run_test(test_binary, params: Tuple[str]): @@ -255,6 +260,8 @@ def build_full_or_auto(matrix): def test(matrix): if platform.system() != "Windows": + if platform.system() == "Darwin": + run_command("otool", "-l", "./test", always_output=True) return run_test( "./test", (matrix["compiler"], matrix["unwind"], matrix["symbols"], matrix["demangle"]) @@ -273,6 +280,8 @@ def test(matrix): def test_full_or_auto(matrix): if platform.system() != "Windows": + if platform.system() == "Darwin": + run_command("otool", "-l", "./test", always_output=True) return run_test( "./test", (matrix["compiler"],) diff --git a/src/binary/mach-o.hpp b/src/binary/mach-o.hpp index 2a02f5c..66046fe 100644 --- a/src/binary/mach-o.hpp +++ b/src/binary/mach-o.hpp @@ -141,6 +141,7 @@ namespace detail { public: mach_o(const std::string& object_path) : object_path(object_path) { + fprintf(stderr, "new mach -------------------------------------- %s\n", object_path.c_str()); file = std::fopen(object_path.c_str(), "rb"); if(file == nullptr) { throw file_error("Unable to read object file " + object_path); @@ -166,11 +167,13 @@ namespace detail { } std::uintptr_t get_text_vmaddr() { + fprintf(stderr, "get_text_vmaddr------------- %s\n", object_path.c_str()); for(const auto& command : load_commands) { if(command.cmd == LC_SEGMENT_64 || command.cmd == LC_SEGMENT) { auto segment = command.cmd == LC_SEGMENT_64 ? load_segment_command<64>(command.file_offset) : load_segment_command<32>(command.file_offset); + fprintf(stderr, "foo \"%s\", %d\n", segment.segname, std::strcmp(segment.segname, "__TEXT") == 0); if(std::strcmp(segment.segname, "__TEXT") == 0) { return segment.vmaddr; } @@ -178,7 +181,6 @@ namespace detail { } // somehow no __TEXT section was found... throw std::runtime_error("Couldn't find __TEXT section while parsing Mach-O object"); - return 0; } std::size_t get_fat_index() const { @@ -422,6 +424,7 @@ namespace detail { flags = header.flags; // handle load commands std::uint32_t ncmds = header.ncmds; + fprintf(stderr, "ncmds: %u\n", ncmds); std::uint32_t load_commands_offset = load_base + header_size; // iterate load commands std::uint32_t actual_offset = load_commands_offset; @@ -491,6 +494,7 @@ namespace detail { std::uint32_t magic = load_bytes(file, mach_header_offset); load_base = mach_header_offset; fat_index = best - fat_arches.data(); + fprintf(stderr, "INDEX: %llu\n", to_ull(fat_index)); if(is_magic_64(magic)) { load_mach<64>(true); } else { diff --git a/src/binary/object.hpp b/src/binary/object.hpp index efb3ae0..7d8f569 100644 --- a/src/binary/object.hpp +++ b/src/binary/object.hpp @@ -56,7 +56,9 @@ namespace detail { if(it == cache.end()) { // arguably it'd be better to release the lock while computing this, but also arguably it's good to not // have two threads try to do the same computation - auto base = mach_o(object_path).get_text_vmaddr(); + mach_o obj(object_path); + obj.print_segments(); + auto base = obj.get_text_vmaddr(); cache.insert(it, {object_path, base}); return base; } else {