diff options
author | Jukka Jylänki <jujjyl@gmail.com> | 2013-09-27 22:53:25 +0300 |
---|---|---|
committer | Jukka Jylänki <jujjyl@gmail.com> | 2013-12-20 11:49:32 +0200 |
commit | 184a425ce07a95e882f3f60b87c1c5037cbffec5 (patch) | |
tree | 25514d7efba73ca73fc36a6eca462ff78154ea0e | |
parent | 26d6ad370e05ff22128df6f0ff17538c8bc1030c (diff) |
Also give the ability to print out function parameters in the callstack with emscripten_get_callstack.
-rw-r--r-- | src/library.js | 50 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 3 | ||||
-rw-r--r-- | tests/emscripten_log/emscripten_log.cpp | 2 |
3 files changed, 52 insertions, 3 deletions
diff --git a/src/library.js b/src/library.js index 6d235e2a..1a4d685a 100644 --- a/src/library.js +++ b/src/library.js @@ -8860,8 +8860,37 @@ LibraryManager.library = { STACKTOP = sp; return str; }, - - emscripten_get_callstack_js__deps: ['emscripten_demangle'], + + // Returns [parentFuncArguments, functionName, paramListName] + _emscripten_traverse_stack: function(args) { + if (!args || !args.callee || !args.callee.name) { + return [null, '', '']; + } + + var funstr = args.callee.toString(); + var funcname = args.callee.name; + var str = '('; + var first = true; + for(i in args) { + var a = args[i]; + if (!first) { + str += ", "; + } + first = false; + if (typeof a === 'number' || typeof a === 'string') { + str += a; + } else { + str += '(' + typeof a + ')'; + } + } + str += ')'; + args = args.callee.caller.arguments; + if (first) + str = ''; + return [args, funcname, str]; + }, + + emscripten_get_callstack_js__deps: ['emscripten_demangle', '_emscripten_traverse_stack'], emscripten_get_callstack_js: function(flags) { var err = new Error(); if (!err.stack) { @@ -8883,6 +8912,14 @@ LibraryManager.library = { flags |= 16/*EM_LOG_JS_STACK*/; } + var stack_args = null; + if (flags & 128 /*EM_LOG_FUNC_PARAMS*/) { + // To get the actual parameters to the functions, traverse the stack via the unfortunately deprecated 'arguments.callee' method, if it works: + var stack_args = __emscripten_traverse_stack(arguments); + while (stack_args[1].indexOf('_emscripten_') >= 0) + stack_args = __emscripten_traverse_stack(stack_args[0]); + } + // Process all lines: lines = callstack.split('\n'); callstack = ''; @@ -8941,6 +8978,15 @@ LibraryManager.library = { } callstack += (haveSourceMap ? (' = '+jsSymbolName) : (' at '+cSymbolName)) + ' (' + file + ':' + lineno + ':' + column + ')\n'; } + + // If we are still keeping track with the callstack by traversing via 'arguments.callee', print the function parameters as well. + if (flags & 128 /*EM_LOG_FUNC_PARAMS*/ && stack_args[0]) { + if (stack_args[1] == jsSymbolName && stack_args[2].length > 0) { + callstack = callstack.replace(/\s+$/, ''); + callstack += ' with values: ' + stack_args[1] + stack_args[2] + '\n'; + } + stack_args = __emscripten_traverse_stack(stack_args[0]); + } } // Trim extra whitespace at the end of the output. callstack = callstack.replace(/\s+$/, ''); diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 22fa9946..b6e6307b 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -471,6 +471,9 @@ double emscripten_asm_const_double(const char *code, ...); /* If specified, the pathnames of the file information in the call * stack will be omitted. */ #define EM_LOG_NO_PATHS 64 +/* If specified, prints out the actual values of the parameters the + * functions were invoked with. */ +#define EM_LOG_FUNC_PARAMS 128 /* * Prints out a message to the console, optionally with the diff --git a/tests/emscripten_log/emscripten_log.cpp b/tests/emscripten_log/emscripten_log.cpp index 39d8c9f5..0cd77467 100644 --- a/tests/emscripten_log/emscripten_log.cpp +++ b/tests/emscripten_log/emscripten_log.cpp @@ -56,7 +56,7 @@ void bar(int = 0, char * = 0, double = 0) // Arbitrary function signature to add else MYASSERT(1 == 1); - int flags = EM_LOG_NO_PATHS | EM_LOG_JS_STACK | EM_LOG_DEMANGLE; + int flags = EM_LOG_NO_PATHS | EM_LOG_JS_STACK | EM_LOG_DEMANGLE | EM_LOG_FUNC_PARAMS; #ifndef RUN_FROM_JS_SHELL flags |= EM_LOG_C_STACK; #endif |