Skip to content

Fix debug-compilation-dir for system libs #20776

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Dec 12, 2023

Conversation

kon72
Copy link
Contributor

@kon72 kon72 commented Nov 24, 2023

Fixes #20775

Before this change:

$ llvm-dwarfdump -debug-info -debug-line --recurse-depth=0  emscripten/cache/sysroot/lib/wasm32-emscripten/libcompiler_rt.a
...
              DW_AT_name    ("../../../system/lib/compiler-rt/__trap.c")
              DW_AT_comp_dir    ("/emsdk/emscripten")

After this change:

$ llvm-dwarfdump -debug-info -debug-line --recurse-depth=0  emscripten/cache/sysroot/lib/wasm32-emscripten/libcompiler_rt.a
...
              DW_AT_name    ("../../../system/lib/compiler-rt/__trap.c")
              DW_AT_comp_dir    ("/emsdk/emscripten/cache/build/libcompiler_rt-tmp")

@RReverser
Copy link
Collaborator

Good catch! It's worth adding a test for this in test_other.py too.

@@ -472,7 +472,7 @@ def generate_ninja(self, build_dir, libname):
if self.deterministic_paths:
source_dir = utils.path_from_root()
cflags += [f'-ffile-prefix-map={source_dir}=/emsdk/emscripten',
'-fdebug-compilation-dir=/emsdk/emscripten']
f'-fdebug-compilation-dir={build_dir}']
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like maybe the wrong fix.

I think the mapping on the line about is the one that needs updating so it ends up being something like -ffile-prefix-map=../..=/emsdk/emscripten'. I think the best way would be to replace source_dir` with somekind of relative source dir?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean that the DWARF entries should be like this?

DW_AT_name    ("/emsdk/emscripten/system/lib/compiler-rt/__trap.c")
DW_AT_comp_dir    ("/emsdk/emscripten")

I agree with your direction so that unnecessary build sub directories (like cache/build/libcompiler_rt-tmp) would be stripped out from dwarf info.

@kripken
Copy link
Member

kripken commented Nov 27, 2023

Regarding testing, maybe checking the contents of dwarfdump is simplest, like e.g. test_separate_dwarf does:

emscripten/test/test_other.py

Lines 9713 to 9720 in cfc68d8

# Check that dwarfdump can dump the debug info
dwdump = self.run_process(
[LLVM_DWARFDUMP, 'subdir/output.wasm.debug.wasm', '-name', 'main'],
stdout=PIPE).stdout
# Basic check that the debug info is more than a skeleton. If so it will
# have a subprogram descriptor for main
self.assertIn('DW_TAG_subprogram', dwdump)
self.assertIn('DW_AT_name\t("main")', dwdump)

@@ -9771,6 +9771,18 @@ def test_separate_dwarf_with_filename_and_path(self):
'-sSEPARATE_DWARF_URL=http://somewhere.com/hosted.wasm'])
self.assertIn(b'somewhere.com/hosted.wasm', read_binary('a.out.wasm'))

def test_dwarf_system_lib(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you mark this new test a @crossplatform so it runs on mac and windows too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

Copy link
Contributor Author

@kon72 kon72 Dec 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks the test is failing on Windows due to path separators (could be related to WebAssembly/binaryen#2781)

From CI output:

DW_AT_name\t("system\\\\lib/emmalloc.c")
DW_AT_comp_dir\t("/emsdk/emscripten")

@sbc100
Should we modify the test to make it pass on Windows? or drop @crossplatform?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be good to fix if possible. Perhaps if this is a pre-existing issue maybe we don't have to fix it now, but it would be good to fix if possible.

Any idea how system\\\\lib/emmalloc.c could end up like that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After some digging on my local Windows machine, It seems backslashes in system\\\\lib/emmalloc.c are coming from the source file path we're passing to Clang, and the slash before basename is due to -ffile-reproducible.

Currently, embuilder is passing the source file path in the platform-specific format (with backslashes as separators on Windows):

clang.exe <redacted> -c ..\..\..\system\lib\emmalloc.c

and I get the following DWARF entry

DW_AT_name ("system\\lib/emmalloc.c")

Then when I replace backslashes in the source file path with slashes, the DWARF also have slashes in its file names.

clang.exe <redacted> -c ../../../system/lib/emmalloc.c
DW_AT_name ("system/lib/emmalloc.c")

Therefore, I think the best approach is to apply utils.normalize_path to source file names in the build command.
What do you think?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks right to me.

@sbc100
Copy link
Collaborator

sbc100 commented Dec 2, 2023

Looks like you need to add these lines to the new test:

    if config.FROZEN_CACHE:                                                                          
      self.skipTest("test doesn't work with frozen cache")  

@kon72 kon72 force-pushed the fix-debug-comp-dir branch from 3ccba72 to ef45f57 Compare December 9, 2023 10:22
@@ -9771,6 +9771,18 @@ def test_separate_dwarf_with_filename_and_path(self):
'-sSEPARATE_DWARF_URL=http://somewhere.com/hosted.wasm'])
self.assertIn(b'somewhere.com/hosted.wasm', read_binary('a.out.wasm'))

def test_dwarf_system_lib(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That looks right to me.

@kripken kripken merged commit 1852e20 into emscripten-core:main Dec 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Wrong DWARF source file path for system libraries
4 participants