From 5e6b5e313cb2fa9c699934df72a7c975d1d02ad2 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Sat, 12 Oct 2013 20:46:11 -0700 Subject: [PATCH] demangle stacks in abort(), using new stackTrace utility --- src/library.js | 2 +- src/postamble.js | 2 +- src/preamble.js | 12 ++++++++++-- tests/test_core.py | 26 ++++++++++++++++++++++++++ 4 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src/library.js b/src/library.js index 5e71b087c..aeebe8bb7 100644 --- a/src/library.js +++ b/src/library.js @@ -4296,7 +4296,7 @@ LibraryManager.library = { }, llvm_trap: function() { - throw 'trap! ' + new Error().stack; + abort('trap!'); }, __assert_fail: function(condition, filename, line, func) { diff --git a/src/postamble.js b/src/postamble.js index 62edeeb6e..d64fb2201 100644 --- a/src/postamble.js +++ b/src/postamble.js @@ -180,7 +180,7 @@ function abort(text) { ABORT = true; EXITSTATUS = 1; - throw 'abort() at ' + (new Error().stack); + throw 'abort() at ' + stackTrace(); } Module['abort'] = Module.abort = abort; diff --git a/src/preamble.js b/src/preamble.js index 63fab34e6..a85cf2f58 100644 --- a/src/preamble.js +++ b/src/preamble.js @@ -644,7 +644,7 @@ function demangle(func) { try { if (typeof func === 'number') func = Pointer_stringify(func); if (func[0] !== '_') return func; - if (func[1] !== '_') return func.substr(1); // C function + if (func[1] !== '_') return func; // C function if (func[2] !== 'Z') return func; var i = 3; // params, etc. @@ -747,10 +747,18 @@ function demangle(func) { } return parse(); } catch(e) { - return func + '' + e; + return func; } } +function demangleAll(text) { + return text.replace(/__Z[\w\d_]+/, function(x) { var y = demangle(x); return x === y ? x : (x + ' (' + y + ')') }); +} + +function stackTrace() { + return demangleAll(new Error().stack); +} + // Memory management var PAGE_SIZE = 4096; diff --git a/tests/test_core.py b/tests/test_core.py index 66f9b8e44..879250824 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -9476,6 +9476,32 @@ def process(filename): Settings.ALIASING_FUNCTION_POINTERS = 1 - Settings.ALIASING_FUNCTION_POINTERS # flip the test self.do_run(src, '''Hello 7 from JS!''') + def test_demangle_stacks(self): + if Settings.ASM_JS: return self.skip('spidermonkey has stack trace issues') + + src = r''' + #include + #include + + namespace NameSpace { + class Class { + public: + int Aborter(double x, char y, int *z) { + int addr = x+y+(int)z; + void *p = (void*)addr; + for (int i = 0; i < 100; i++) free(p); // will abort, should show proper stack trace + } + }; + } + + int main(int argc, char **argv) { + NameSpace::Class c; + c.Aborter(1.234, 'a', NULL); + return 0; + } + ''' + self.do_run(src, 'NameSpace::Class::Aborter(double, char, int*)'); + def test_embind(self): if self.emcc_args is None: return self.skip('requires emcc') Building.COMPILER_TEST_OPTS += ['--bind']