2 * Debugging related API calls
5 #include "duk_internal.h"
7 DUK_EXTERNAL
void duk_push_context_dump(duk_context
*ctx
) {
11 DUK_ASSERT_CTX_VALID(ctx
);
13 /* We don't duk_require_stack() here now, but rely on the caller having
17 top
= duk_get_top(ctx
);
19 for (idx
= 0; idx
< top
; idx
++) {
21 duk_put_prop_index(ctx
, -2, idx
);
24 /* XXX: conversion errors should not propagate outwards.
25 * Perhaps values need to be coerced individually?
27 duk_bi_json_stringify_helper(ctx
,
28 duk_get_top_index(ctx
), /*idx_value*/
29 DUK_INVALID_INDEX
, /*idx_replacer*/
30 DUK_INVALID_INDEX
, /*idx_space*/
31 DUK_JSON_FLAG_EXT_CUSTOM
|
32 DUK_JSON_FLAG_ASCII_ONLY
|
33 DUK_JSON_FLAG_AVOID_KEY_QUOTES
/*flags*/);
35 duk_push_sprintf(ctx
, "ctx: top=%ld, stack=%s", (long) top
, (const char *) duk_safe_to_string(ctx
, -1));
36 duk_replace(ctx
, -3); /* [ ... arr jsonx(arr) res ] -> [ ... res jsonx(arr) ] */
38 DUK_ASSERT(duk_is_string(ctx
, -1));
41 #if defined(DUK_USE_DEBUGGER_SUPPORT)
43 DUK_EXTERNAL
void duk_debugger_attach(duk_context
*ctx
,
44 duk_debug_read_function read_cb
,
45 duk_debug_write_function write_cb
,
46 duk_debug_peek_function peek_cb
,
47 duk_debug_read_flush_function read_flush_cb
,
48 duk_debug_write_flush_function write_flush_cb
,
49 duk_debug_detached_function detached_cb
,
51 duk_hthread
*thr
= (duk_hthread
*) ctx
;
56 DUK_ASSERT_CTX_VALID(ctx
);
57 DUK_ASSERT(read_cb
!= NULL
);
58 DUK_ASSERT(write_cb
!= NULL
);
59 /* Other callbacks are optional. */
62 heap
->dbg_read_cb
= read_cb
;
63 heap
->dbg_write_cb
= write_cb
;
64 heap
->dbg_peek_cb
= peek_cb
;
65 heap
->dbg_read_flush_cb
= read_flush_cb
;
66 heap
->dbg_write_flush_cb
= write_flush_cb
;
67 heap
->dbg_detached_cb
= detached_cb
;
68 heap
->dbg_udata
= udata
;
70 /* Start in paused state. */
71 heap
->dbg_processing
= 0;
73 heap
->dbg_state_dirty
= 1;
74 heap
->dbg_force_restart
= 0;
75 heap
->dbg_step_type
= 0;
76 heap
->dbg_step_thread
= NULL
;
77 heap
->dbg_step_csindex
= 0;
78 heap
->dbg_step_startline
= 0;
79 heap
->dbg_exec_counter
= 0;
80 heap
->dbg_last_counter
= 0;
81 heap
->dbg_last_time
= 0.0;
83 /* Send version identification and flush right afterwards. Note that
84 * we must write raw, unframed bytes here.
86 duk_push_sprintf(ctx
, "%ld %ld %s %s\n",
87 (long) DUK_DEBUG_PROTOCOL_VERSION
,
89 (const char *) DUK_GIT_DESCRIBE
,
90 (const char *) DUK_USE_TARGET_INFO
);
91 str
= duk_get_lstring(ctx
, -1, &len
);
92 DUK_ASSERT(str
!= NULL
);
93 duk_debug_write_bytes(thr
, (const duk_uint8_t
*) str
, len
);
94 duk_debug_write_flush(thr
);
98 DUK_EXTERNAL
void duk_debugger_detach(duk_context
*ctx
) {
101 DUK_ASSERT_CTX_VALID(ctx
);
102 thr
= (duk_hthread
*) ctx
;
103 DUK_ASSERT(thr
!= NULL
);
104 DUK_ASSERT(thr
->heap
!= NULL
);
106 /* Can be called muliple times with no harm. */
107 duk_debug_do_detach(thr
->heap
);
110 DUK_EXTERNAL
void duk_debugger_cooperate(duk_context
*ctx
) {
112 duk_bool_t processed_messages
;
114 DUK_ASSERT_CTX_VALID(ctx
);
115 thr
= (duk_hthread
*) ctx
;
116 DUK_ASSERT(thr
!= NULL
);
117 DUK_ASSERT(thr
->heap
!= NULL
);
119 if (!DUK_HEAP_IS_DEBUGGER_ATTACHED(thr
->heap
)) {
122 if (thr
->callstack_top
> 0 || thr
->heap
->dbg_processing
) {
123 /* Calling duk_debugger_cooperate() while Duktape is being
124 * called into is not supported. This is not a 100% check
125 * but prevents any damage in most cases.
130 thr
->heap
->dbg_processing
= 1;
131 processed_messages
= duk_debug_process_messages(thr
, 1 /*no_block*/);
132 thr
->heap
->dbg_processing
= 0;
133 DUK_UNREF(processed_messages
);
136 #else /* DUK_USE_DEBUGGER_SUPPORT */
138 DUK_EXTERNAL
void duk_debugger_attach(duk_context
*ctx
,
139 duk_debug_read_function read_cb
,
140 duk_debug_write_function write_cb
,
141 duk_debug_peek_function peek_cb
,
142 duk_debug_read_flush_function read_flush_cb
,
143 duk_debug_write_flush_function write_flush_cb
,
144 duk_debug_detached_function detached_cb
,
146 DUK_ASSERT_CTX_VALID(ctx
);
150 DUK_UNREF(read_flush_cb
);
151 DUK_UNREF(write_flush_cb
);
152 DUK_UNREF(detached_cb
);
154 duk_error(ctx
, DUK_ERR_API_ERROR
, "no debugger support");
157 DUK_EXTERNAL
void duk_debugger_detach(duk_context
*ctx
) {
158 DUK_ASSERT_CTX_VALID(ctx
);
159 duk_error(ctx
, DUK_ERR_API_ERROR
, "no debugger support");
162 DUK_EXTERNAL
void duk_debugger_cooperate(duk_context
*ctx
) {
164 DUK_ASSERT_CTX_VALID(ctx
);
168 #endif /* DUK_USE_DEBUGGER_SUPPORT */