Introduce generic backtrace capturing (#4555)

Remove jerry_get_backtrace_from function

JerryScript-DCO-1.0-Signed-off-by: Zoltan Herczeg zherczeg.u-szeged@partner.samsung.com
This commit is contained in:
Zoltan Herczeg
2021-02-04 16:33:59 +01:00
committed by GitHub
parent feb2855f0c
commit 3623ac789d
8 changed files with 673 additions and 297 deletions
+87 -29
View File
@@ -4681,51 +4681,109 @@ jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb, /**< per
jerry_value_t
jerry_get_backtrace (uint32_t max_depth) /**< depth limit of the backtrace */
{
return vm_get_backtrace (max_depth, NULL);
return vm_get_backtrace (max_depth);
} /* jerry_get_backtrace */
/**
* Get backtrace. The backtrace is an array of strings where
* each string contains the position of the corresponding frame.
* The array length is zero if the backtrace is not available.
*
* @return array value
* Low-level function to capture each backtrace frame.
* The captured frame data is passed to a callback function.
*/
jerry_value_t
jerry_get_backtrace_from (uint32_t max_depth, /**< depth limit of the backtrace */
jerry_value_t ignored_function) /**< collect backtrace after this function */
void
jerry_backtrace_capture (jerry_backtrace_callback_t callback, /**< callback function */
void *user_p) /**< user pointer passed to the callback function */
{
ecma_object_t *ignored_function_p = NULL;
jerry_backtrace_frame_t frame;
vm_frame_ctx_t *context_p = JERRY_CONTEXT (vm_top_context_p);
if (ecma_is_value_object (ignored_function))
while (context_p != NULL)
{
ignored_function_p = ecma_get_object_from_value (ignored_function);
frame.context_p = context_p;
frame.frame_type = JERRY_BACKTRACE_FRAME_JS;
while (true)
if (!callback (&frame, user_p))
{
ecma_object_type_t type = ecma_get_object_type (ignored_function_p);
return;
}
if (type == ECMA_OBJECT_TYPE_FUNCTION || type == ECMA_OBJECT_TYPE_NATIVE_FUNCTION)
{
break;
}
context_p = context_p->prev_context_p;
}
} /* jerry_backtrace_capture */
if (type == ECMA_OBJECT_TYPE_BOUND_FUNCTION)
{
ecma_bound_function_t *bound_func_p = (ecma_bound_function_t *) ignored_function_p;
jmem_cpointer_tag_t target_function = bound_func_p->header.u.bound_function.target_function;
/**
* Returns with the type of the backtrace frame.
*
* @return frame type listed in jerry_backtrace_frame_types_t
*/
jerry_backtrace_frame_types_t
jerry_backtrace_get_frame_type (jerry_backtrace_frame_t *frame_p) /**< frame pointer */
{
return (jerry_backtrace_frame_types_t) frame_p->frame_type;
} /* jerry_backtrace_get_frame_type */
ignored_function_p = ECMA_GET_NON_NULL_POINTER_FROM_POINTER_TAG (ecma_object_t, target_function);
continue;
}
/**
* Initialize and return with the location private field of a backtrace frame.
*
* @return pointer to the location private field - if the location is available,
* NULL - otherwise
*/
const jerry_backtrace_location_t *
jerry_backtrace_get_location (jerry_backtrace_frame_t *frame_p) /**< frame pointer */
{
JERRY_UNUSED (frame_p);
ignored_function_p = NULL;
break;
#if ENABLED (JERRY_LINE_INFO)
if (frame_p->frame_type == JERRY_BACKTRACE_FRAME_JS)
{
vm_frame_ctx_t *context_p = frame_p->context_p;
frame_p->location.resource_name = ecma_get_resource_name (context_p->shared_p->bytecode_header_p);
frame_p->location.line = context_p->current_line;
frame_p->location.column = 1;
return &frame_p->location;
}
#endif /* ENABLED (JERRY_LINE_INFO) */
return NULL;
} /* jerry_backtrace_get_location */
/**
* Initialize and return with the called function private field of a backtrace frame.
* The backtrace frame is created for running the code bound to this function.
*
* @return pointer to the called function - if the function is available,
* NULL - otherwise
*/
const jerry_value_t *
jerry_backtrace_get_function (jerry_backtrace_frame_t *frame_p) /**< frame pointer */
{
if (frame_p->frame_type == JERRY_BACKTRACE_FRAME_JS)
{
vm_frame_ctx_t *context_p = frame_p->context_p;
if (context_p->shared_p->status_flags & VM_FRAME_CTX_SHARED_HAS_ARG_LIST)
{
vm_frame_ctx_shared_args_t *shared_args_p = (vm_frame_ctx_shared_args_t *) context_p->shared_p;
frame_p->function = ecma_make_object_value (shared_args_p->function_object_p);
return &frame_p->function;
}
}
return vm_get_backtrace (max_depth, ignored_function_p);
} /* jerry_get_backtrace_from */
return NULL;
} /* jerry_backtrace_get_function */
/**
* Returns true, if the code bound to the backtrace frame is strict mode code.
*
* @return true - if strict mode code is bound to the frame,
* false - otherwise
*/
bool
jerry_backtrace_is_strict (jerry_backtrace_frame_t *frame_p) /**< frame pointer */
{
return (frame_p->frame_type == JERRY_BACKTRACE_FRAME_JS
&& (frame_p->context_p->status_flags & VM_FRAME_CTX_IS_STRICT) != 0);
} /* jerry_backtrace_is_strict */
/**
* Get the resource name (usually a file name) of the currently executed script or the given function object
+1 -1
View File
@@ -177,7 +177,7 @@ ecma_new_standard_error (ecma_standard_error_t error_type, /**< native error typ
NULL);
ecma_deref_ecma_string (stack_str_p);
ecma_value_t backtrace_value = vm_get_backtrace (0, NULL);
ecma_value_t backtrace_value = vm_get_backtrace (0);
prop_value_p->value = backtrace_value;
ecma_deref_object (ecma_get_object_from_value (backtrace_value));
+43 -2
View File
@@ -205,6 +205,34 @@ typedef struct
jerry_value_t setter;
} jerry_property_descriptor_t;
/**
* List of backtrace frame types returned by jerry_backtrace_get_frame_type.
*/
typedef enum
{
JERRY_BACKTRACE_FRAME_JS, /**< indicates that the frame is created for a JavaScript function/method */
} jerry_backtrace_frame_types_t;
/**
* Location info retreived by jerry_backtrace_get_location.
*/
typedef struct
{
jerry_value_t resource_name; /**< resource name */
jerry_size_t line; /**< line index */
jerry_size_t column; /**< column index */
} jerry_backtrace_location_t;
/**
* Internal data structure for jerry_backtrace_frame_t definition.
*/
struct jerry_backtrace_frame_internal_t;
/**
* Backtrace frame data passed to the jerry_backtrace_callback_t handler.
*/
typedef struct jerry_backtrace_frame_internal_t jerry_backtrace_frame_t;
/**
* Description of JerryScript heap memory stats.
* It is for memory profiling.
@@ -237,6 +265,11 @@ typedef void (*jerry_object_native_free_callback_t) (void *native_p);
*/
typedef void (*jerry_error_object_created_callback_t) (const jerry_value_t error_object, void *user_p);
/**
* Callback function which is called by jerry_backtrace_capture for each stack frame.
*/
typedef bool (*jerry_backtrace_callback_t) (jerry_backtrace_frame_t *frame_p, void *user_p);
/**
* Callback which tells whether the ECMAScript execution should be stopped.
*
@@ -765,12 +798,20 @@ void jerry_heap_free (void *mem_p, size_t size);
*/
jerry_context_t *jerry_create_context (uint32_t heap_size, jerry_context_alloc_t alloc, void *cb_data_p);
/**
* Backtrace functions.
*/
jerry_value_t jerry_get_backtrace (uint32_t max_depth);
void jerry_backtrace_capture (jerry_backtrace_callback_t callback, void *user_p);
jerry_backtrace_frame_types_t jerry_backtrace_get_frame_type (jerry_backtrace_frame_t *frame_p);
const jerry_backtrace_location_t *jerry_backtrace_get_location (jerry_backtrace_frame_t *frame_p);
const jerry_value_t *jerry_backtrace_get_function (jerry_backtrace_frame_t *frame_p);
bool jerry_backtrace_is_strict (jerry_backtrace_frame_t *frame_p);
/**
* Miscellaneous functions.
*/
void jerry_set_vm_exec_stop_callback (jerry_vm_exec_stop_callback_t stop_cb, void *user_p, uint32_t frequency);
jerry_value_t jerry_get_backtrace (uint32_t max_depth);
jerry_value_t jerry_get_backtrace_from (uint32_t max_depth, jerry_value_t ignored_function);
jerry_value_t jerry_get_resource_name (const jerry_value_t value);
jerry_value_t jerry_get_new_target (void);
+11
View File
@@ -151,6 +151,17 @@ typedef struct
vm_frame_ctx_t frame_ctx; /**< frame context part */
} vm_executable_object_t;
/**
* Real backtrace frame data passed to the jerry_backtrace_callback_t handler.
*/
struct jerry_backtrace_frame_internal_t
{
vm_frame_ctx_t *context_p; /**< context pointer */
uint8_t frame_type; /**< frame type */
jerry_backtrace_location_t location; /**< location information */
ecma_value_t function; /**< function reference */
};
/**
* @}
* @}
+1 -31
View File
@@ -59,40 +59,11 @@ vm_is_direct_eval_form_call (void)
* @return array ecma value
*/
ecma_value_t
vm_get_backtrace (uint32_t max_depth, /**< maximum backtrace depth, 0 = unlimited */
ecma_object_t *ignored_function_p) /**< ignore functions up to this function */
vm_get_backtrace (uint32_t max_depth) /**< maximum backtrace depth, 0 = unlimited */
{
#if ENABLED (JERRY_LINE_INFO)
vm_frame_ctx_t *context_p = JERRY_CONTEXT (vm_top_context_p);
if (ignored_function_p != NULL)
{
JERRY_ASSERT (ecma_get_object_type (ignored_function_p) == ECMA_OBJECT_TYPE_FUNCTION
|| ecma_get_object_type (ignored_function_p) == ECMA_OBJECT_TYPE_NATIVE_FUNCTION);
while (true)
{
if (context_p == NULL)
{
context_p = JERRY_CONTEXT (vm_top_context_p);
break;
}
if (context_p->shared_p->status_flags & VM_FRAME_CTX_SHARED_HAS_ARG_LIST)
{
vm_frame_ctx_shared_args_t *shared_args_p = (vm_frame_ctx_shared_args_t *) context_p->shared_p;
if (shared_args_p->function_object_p == ignored_function_p)
{
context_p = context_p->prev_context_p;
break;
}
}
context_p = context_p->prev_context_p;
}
}
if (max_depth == 0)
{
max_depth = UINT32_MAX;
@@ -138,7 +109,6 @@ vm_get_backtrace (uint32_t max_depth, /**< maximum backtrace depth, 0 = unlimite
return ecma_make_object_value (array_p);
#else /* !ENABLED (JERRY_LINE_INFO) */
JERRY_UNUSED (max_depth);
JERRY_UNUSED (ignored_function_p);
return ecma_make_object_value (ecma_op_new_array_object (0));
#endif /* ENABLED (JERRY_LINE_INFO) */
+1 -1
View File
@@ -489,7 +489,7 @@ ecma_value_t vm_execute (vm_frame_ctx_t *frame_ctx_p);
bool vm_is_strict_mode (void);
bool vm_is_direct_eval_form_call (void);
ecma_value_t vm_get_backtrace (uint32_t max_depth, ecma_object_t *ignored_function_p);
ecma_value_t vm_get_backtrace (uint32_t max_depth);
/**
* @}