Skip to content

Commit 574b075

Browse files
committed
Add backtrace print when the 'assert' js method fails
In the jerry-ext there is a native 'assert' handler implemented. This change adds extra backtrace information if an 'assert' call fails. To print out the backtrace the library should be built with `JERRY_LINE_INFO` enabled. JerryScript-DCO-1.0-Signed-off-by: Peter Gal pgal.u-szeged@partner.samsung.com
1 parent 1088273 commit 574b075

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

docs/10.EXT-REFERENCE-HANDLER.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,9 @@ For example usage see [jerryx_set_properties](#jerryx_set_properties).
421421
422422
Hard assert for scripts. The routine calls `jerry_port_fatal` on assertion failure.
423423
424+
If the `JERRY_FEATURE_LINE_INFO` runtime feature is enabled (build option: `JERRY_LINE_INFO`)
425+
a backtrace is also printed out.
426+
424427
**Prototype**
425428
426429
```c

jerry-ext/handler/handler-assert.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,15 @@
1616
#include "jerryscript-ext/handler.h"
1717
#include "jerryscript-port.h"
1818

19+
#include <inttypes.h>
20+
1921
/**
2022
* Hard assert for scripts. The routine calls jerry_port_fatal on assertion failure.
2123
*
24+
* Notes:
25+
* * If the `JERRY_FEATURE_LINE_INFO` runtime feature is enabled (build option: `JERRY_LINE_INFO`)
26+
* a backtrace is also printed out.
27+
*
2228
* @return true - if only one argument was passed and that argument was a boolean true.
2329
* Note that the function does not return otherwise.
2430
*/
@@ -38,7 +44,52 @@ jerryx_handler_assert_fatal (const jerry_value_t func_obj_val, /**< function obj
3844
return jerry_create_boolean (true);
3945
}
4046

47+
/* Assert failed, print a bit of JS backtrace */
4148
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script Error: assertion failed\n");
49+
50+
if (jerry_is_feature_enabled (JERRY_FEATURE_LINE_INFO))
51+
{
52+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "Script backtrace (top 5):\n");
53+
54+
/* If the line info feature is disabled an empty array will be returned. */
55+
jerry_value_t backtrace_array = jerry_get_backtrace (5);
56+
uint32_t array_length = jerry_get_array_length (backtrace_array);
57+
58+
for (uint32_t idx = 0; idx < array_length; idx++)
59+
{
60+
jerry_value_t property = jerry_get_property_by_index (backtrace_array, idx);
61+
62+
jerry_length_t total_size = jerry_get_utf8_string_size (property);
63+
jerry_length_t current_size = 0;
64+
jerry_char_t string_buffer[64];
65+
const jerry_length_t copy_size = (jerry_length_t) (sizeof (string_buffer) - 1);
66+
67+
/* On some systems the uint32_t values can't be printed with "%u" and
68+
* on some systems it can be printed. To avoid differences in the uint32_t typdef
69+
* The "PRIu32" macro is used to correctly add the formatter.
70+
*/
71+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, " %"PRIu32": ", idx);
72+
do
73+
{
74+
jerry_size_t copied_bytes = jerry_substring_to_utf8_char_buffer (property,
75+
current_size,
76+
current_size + copy_size,
77+
string_buffer,
78+
copy_size);
79+
string_buffer[copied_bytes] = '\0';
80+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "%s", string_buffer);
81+
82+
current_size += copied_bytes;
83+
}
84+
while (total_size != current_size);
85+
jerry_port_log (JERRY_LOG_LEVEL_ERROR, "\n");
86+
87+
jerry_release_value (property);
88+
}
89+
90+
jerry_release_value (backtrace_array);
91+
}
92+
4293
jerry_port_fatal (ERR_FAILED_INTERNAL_ASSERTION);
4394
} /* jerryx_handler_assert_fatal */
4495

0 commit comments

Comments
 (0)