Skip to content

Commit 854c6e2

Browse files
authored
Support -gseparate-dwarf=FILENAME. fixes #10759 (#10766)
1 parent bb8f39d commit 854c6e2

File tree

4 files changed

+30
-8
lines changed

4 files changed

+30
-8
lines changed

emcc.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,9 +2825,17 @@ def check_bad_eq(arg):
28252825
if requested_level.startswith('force_dwarf'):
28262826
exit_with_error('gforce_dwarf was a temporary option and is no longer necessary (use -g)')
28272827
elif requested_level.startswith('separate-dwarf'):
2828-
# Emit full DWARF but also emit it in a file on the side
2828+
# emit full DWARF but also emit it in a file on the side
28292829
newargs[i] = '-g'
2830-
shared.Settings.SEPARATE_DWARF = 1
2830+
# if a file is provided, use that; otherwise use the default location
2831+
# (note that we do not know the default location until all args have
2832+
# been parsed, so just note True for now).
2833+
if requested_level != 'separate-dwarf':
2834+
if not requested_level.startswith('separate-dwarf=') or requested_level.count('=') != 1:
2835+
exit_with_error('invalid -gseparate-dwarf=FILENAME notation')
2836+
shared.Settings.SEPARATE_DWARF = requested_level.split('=')[1]
2837+
else:
2838+
shared.Settings.SEPARATE_DWARF = True
28312839
# a non-integer level can be something like -gline-tables-only. keep
28322840
# the flag for the clang frontend to emit the appropriate DWARF info.
28332841
# set the emscripten debug level to 3 so that we do not remove that
@@ -3317,7 +3325,11 @@ def run_closure_compiler(final):
33173325
save_intermediate_with_wasm('symbolmap', wasm_binary_target)
33183326

33193327
if shared.Settings.DEBUG_LEVEL >= 3 and shared.Settings.SEPARATE_DWARF and os.path.exists(wasm_binary_target):
3320-
shared.Building.emit_debug_on_side(wasm_binary_target)
3328+
# if the dwarf filename wasn't provided, use the default target + a suffix
3329+
dwarf_target = shared.Settings.SEPARATE_DWARF
3330+
if dwarf_target is True:
3331+
dwarf_target = wasm_binary_target + '.debug.wasm'
3332+
shared.Building.emit_debug_on_side(wasm_binary_target, dwarf_target)
33213333

33223334
# replace placeholder strings with correct subresource locations
33233335
if shared.Settings.SINGLE_FILE:

site/source/docs/tools_reference/emcc.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,10 +114,11 @@ Options that are modified or new in *emcc* are listed below:
114114
- When compiling to object files, this is the same as in *Clang* and *gcc*, it adds debug information to the object files.
115115
- When linking, this is equivalent to :ref:`-g3 <emcc-g3>`.
116116

117-
``-gseparate-dwarf``
117+
``-gseparate-dwarf[=FILENAME]``
118118
Preserve debug information, but in a separate file on the side. This is the
119-
same as ``-g``, but the main file will contain no debug info, while a file
120-
with suffix ``.debug.wasm`` will contain the debug info.
119+
same as ``-g``, but the main file will contain no debug info, while debug
120+
info will be present in a file on the side (``FILENAME`` if provided,
121+
otherwise the same as the wasm file but with suffix ``.debug.wasm``).
121122

122123
.. _emcc-gN:
123124

tests/test_other.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9147,6 +9147,16 @@ def test_separate_dwarf(self):
91479147
self.assertIn(b'external_debug_info', wasm)
91489148
self.assertIn(b'a.out.wasm.debug.wasm', wasm)
91499149

9150+
@no_fastcomp('dwarf')
9151+
def test_separate_dwarf_with_filename(self):
9152+
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), '-gseparate-dwarf=with_dwarf.wasm'])
9153+
self.assertNotExists('a.out.wasm.debug.wasm')
9154+
self.assertExists('with_dwarf.wasm')
9155+
# the correct notation is to have exactly one '=' and in the right place
9156+
for invalid in ('-gseparate-dwarf=x=', '-gseparate-dwarfy=', '-gseparate-dwarf-hmm'):
9157+
stderr = self.expect_fail([PYTHON, EMCC, path_from_root('tests', 'hello_world.c'), invalid])
9158+
self.assertContained('invalid -gseparate-dwarf=FILENAME notation', stderr)
9159+
91509160
def test_wasm_producers_section(self):
91519161
# no producers section by default
91529162
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.c')])

tools/shared.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2704,12 +2704,11 @@ def wasm2js(js_file, wasm_file, opt_level, minify_whitespace, use_closure_compil
27042704
return js_file
27052705

27062706
@staticmethod
2707-
def emit_debug_on_side(wasm_file):
2707+
def emit_debug_on_side(wasm_file, wasm_file_with_dwarf):
27082708
# extract the DWARF info from the main file, and leave the wasm with
27092709
# debug into as a file on the side
27102710
# TODO: emit only debug sections in the side file, and not the entire
27112711
# wasm as well
2712-
wasm_file_with_dwarf = wasm_file + '.debug.wasm'
27132712
shutil.move(wasm_file, wasm_file_with_dwarf)
27142713
run_process([LLVM_OBJCOPY, '--remove-section=.debug*', wasm_file_with_dwarf, wasm_file])
27152714

0 commit comments

Comments
 (0)