Skip to content

Commit dd1fd6a

Browse files
authored
Support arbitrary -gX debug options, and limit DWARF to line-tables-only (emscripten-core#9872)
Fixes emscripten-core#9611
1 parent a6a8b96 commit dd1fd6a

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

emcc.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1821,6 +1821,7 @@ def check_human_readable_list(items):
18211821
shared.Settings.BUNDLED_CD_DEBUG_FILE = target + ".cd"
18221822
shared.Settings.SYSTEM_JS_LIBRARIES.append(shared.path_from_root('src', 'library_cyberdwarf.js'))
18231823
shared.Settings.SYSTEM_JS_LIBRARIES.append(shared.path_from_root('src', 'library_debugger_toolkit.js'))
1824+
newargs.append('-g')
18241825

18251826
if options.tracing:
18261827
if shared.Settings.ALLOW_MEMORY_GROWTH:
@@ -1891,11 +1892,8 @@ def check_human_readable_list(items):
18911892
logger.debug("running (for precompiled headers): " + clang_compiler + ' ' + ' '.join(args))
18921893
return run_process([clang_compiler] + args, check=False).returncode
18931894

1894-
if need_llvm_debug_info(options):
1895-
newargs.append('-g')
1896-
18971895
# For asm.js, the generated JavaScript could preserve LLVM value names, which can be useful for debugging.
1898-
if options.debug_level >= 3 and not shared.Settings.WASM:
1896+
if options.debug_level >= 3 and not shared.Settings.WASM and not shared.Settings.WASM_BACKEND:
18991897
newargs.append('-fno-discard-value-names')
19001898

19011899
def is_link_flag(flag):
@@ -2640,10 +2638,26 @@ def check_bad_eq(arg):
26402638
newargs[i] = ''
26412639
newargs[i + 1] = ''
26422640
elif newargs[i].startswith('-g'):
2643-
requested_level = newargs[i][2:] or '3'
2644-
options.debug_level = validate_arg_level(requested_level, 4, 'Invalid debug level: ' + newargs[i])
26452641
options.requested_debug = newargs[i]
2646-
newargs[i] = ''
2642+
requested_level = newargs[i][2:] or '3'
2643+
if is_int(requested_level):
2644+
# the -gX value is the debug level (-g1, -g2, etc.)
2645+
options.debug_level = validate_arg_level(requested_level, 4, 'Invalid debug level: ' + newargs[i])
2646+
# if we don't need to preserve LLVM debug info, do not keep this flag
2647+
# for clang
2648+
if options.debug_level < 3:
2649+
newargs[i] = ''
2650+
else:
2651+
# until we support full DWARF info, limit the clang frontend to just
2652+
# emit line tables, which can be represented in source maps
2653+
newargs[i] = '-gline-tables-only'
2654+
else:
2655+
# a non-integer level can be something like -gline-tables-only. keep
2656+
# the flag for the clang frontend to emit the appropriate DWARF info.
2657+
# set the emscripten debug level to 3 so that we do not remove that
2658+
# debug info during link (during compile, this does not make a
2659+
# difference).
2660+
options.debug_level = 3
26472661
elif newargs[i] == '-profiling' or newargs[i] == '--profiling':
26482662
options.debug_level = max(options.debug_level, 2)
26492663
options.profiling = True
@@ -3676,6 +3690,14 @@ def validate_arg_level(level_string, max_level, err_msg, clamp=False):
36763690
return level
36773691

36783692

3693+
def is_int(s):
3694+
try:
3695+
int(s)
3696+
return True
3697+
except ValueError:
3698+
return False
3699+
3700+
36793701
if __name__ == '__main__':
36803702
try:
36813703
sys.exit(run(sys.argv))

site/source/docs/tools_reference/emcc.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ Options that are modified or new in *emcc* are listed below:
112112
Preserve debug information.
113113

114114
- When compiling to object files, this is the same as in *Clang* and *gcc*, it adds debug information to the object files.
115-
- When linking, this is equivalent to :ref:`-g3 <emcc-g3>` (preserve JS whitespace and compiled function names).
115+
- When linking, this is equivalent to :ref:`-g3 <emcc-g3>`.
116116

117117
.. _emcc-gN:
118118

@@ -137,7 +137,7 @@ Options that are modified or new in *emcc* are listed below:
137137
-
138138
.. _emcc-g3:
139139

140-
``-g3``: When compiling to object files, keep debug info (this is the same as :ref:`-g <emcc-g>`).
140+
``-g3``: When compiling to object files, keep debug info, including JS whitespace, function names, and LLVM debug info if any (this is the same as :ref:`-g <emcc-g>`).
141141

142142
.. _emcc-g4:
143143

tests/test_other.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2329,6 +2329,33 @@ def test_debuginfo(self):
23292329
else:
23302330
self.assertIn('strip-debug', err)
23312331

2332+
@no_fastcomp()
2333+
def test_debuginfo_line_tables_only(self):
2334+
def test(do_compile):
2335+
do_compile([])
2336+
no_size = os.path.getsize('a.out.wasm')
2337+
do_compile(['-gline-tables-only'])
2338+
line_size = os.path.getsize('a.out.wasm')
2339+
do_compile(['-g'])
2340+
full_size = os.path.getsize('a.out.wasm')
2341+
self.assertLess(no_size, line_size)
2342+
# currently we don't support full debug info anyhow, so line tables
2343+
# is all we have
2344+
self.assertEqual(line_size, full_size)
2345+
2346+
def compile_directly(compile_args):
2347+
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.cpp')] + compile_args)
2348+
2349+
test(compile_directly)
2350+
2351+
def compile_to_object_first(compile_args):
2352+
# compile with the specified args
2353+
run_process([PYTHON, EMCC, path_from_root('tests', 'hello_world.cpp')] + compile_args + ['-c', '-o', 'a.o'])
2354+
# link with debug info
2355+
run_process([PYTHON, EMCC, 'a.o', '-g'])
2356+
2357+
test(compile_to_object_first)
2358+
23322359
@unittest.skipIf(not scons_path, 'scons not found in PATH')
23332360
@with_env_modify({'EMSCRIPTEN_ROOT': path_from_root()})
23342361
def test_scons(self):

0 commit comments

Comments
 (0)