]> git.proxmox.com Git - ceph.git/blob - ceph/src/civetweb/src/third_party/duktape-1.3.0/src-separate/duk_api_debug.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / civetweb / src / third_party / duktape-1.3.0 / src-separate / duk_api_debug.c
1 /*
2 * Debugging related API calls
3 */
4
5 #include "duk_internal.h"
6
7 DUK_EXTERNAL void duk_push_context_dump(duk_context *ctx) {
8 duk_idx_t idx;
9 duk_idx_t top;
10
11 DUK_ASSERT_CTX_VALID(ctx);
12
13 /* We don't duk_require_stack() here now, but rely on the caller having
14 * enough space.
15 */
16
17 top = duk_get_top(ctx);
18 duk_push_array(ctx);
19 for (idx = 0; idx < top; idx++) {
20 duk_dup(ctx, idx);
21 duk_put_prop_index(ctx, -2, idx);
22 }
23
24 /* XXX: conversion errors should not propagate outwards.
25 * Perhaps values need to be coerced individually?
26 */
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*/);
34
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) ] */
37 duk_pop(ctx);
38 DUK_ASSERT(duk_is_string(ctx, -1));
39 }
40
41 #if defined(DUK_USE_DEBUGGER_SUPPORT)
42
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,
50 void *udata) {
51 duk_hthread *thr = (duk_hthread *) ctx;
52 duk_heap *heap;
53 const char *str;
54 duk_size_t len;
55
56 DUK_ASSERT_CTX_VALID(ctx);
57 DUK_ASSERT(read_cb != NULL);
58 DUK_ASSERT(write_cb != NULL);
59 /* Other callbacks are optional. */
60
61 heap = thr->heap;
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;
69
70 /* Start in paused state. */
71 heap->dbg_processing = 0;
72 heap->dbg_paused = 1;
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;
82
83 /* Send version identification and flush right afterwards. Note that
84 * we must write raw, unframed bytes here.
85 */
86 duk_push_sprintf(ctx, "%ld %ld %s %s\n",
87 (long) DUK_DEBUG_PROTOCOL_VERSION,
88 (long) DUK_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);
95 duk_pop(ctx);
96 }
97
98 DUK_EXTERNAL void duk_debugger_detach(duk_context *ctx) {
99 duk_hthread *thr;
100
101 DUK_ASSERT_CTX_VALID(ctx);
102 thr = (duk_hthread *) ctx;
103 DUK_ASSERT(thr != NULL);
104 DUK_ASSERT(thr->heap != NULL);
105
106 /* Can be called muliple times with no harm. */
107 duk_debug_do_detach(thr->heap);
108 }
109
110 DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) {
111 duk_hthread *thr;
112 duk_bool_t processed_messages;
113
114 DUK_ASSERT_CTX_VALID(ctx);
115 thr = (duk_hthread *) ctx;
116 DUK_ASSERT(thr != NULL);
117 DUK_ASSERT(thr->heap != NULL);
118
119 if (!DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
120 return;
121 }
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.
126 */
127 return;
128 }
129
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);
134 }
135
136 #else /* DUK_USE_DEBUGGER_SUPPORT */
137
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,
145 void *udata) {
146 DUK_ASSERT_CTX_VALID(ctx);
147 DUK_UNREF(read_cb);
148 DUK_UNREF(write_cb);
149 DUK_UNREF(peek_cb);
150 DUK_UNREF(read_flush_cb);
151 DUK_UNREF(write_flush_cb);
152 DUK_UNREF(detached_cb);
153 DUK_UNREF(udata);
154 duk_error(ctx, DUK_ERR_API_ERROR, "no debugger support");
155 }
156
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");
160 }
161
162 DUK_EXTERNAL void duk_debugger_cooperate(duk_context *ctx) {
163 /* nop */
164 DUK_ASSERT_CTX_VALID(ctx);
165 DUK_UNREF(ctx);
166 }
167
168 #endif /* DUK_USE_DEBUGGER_SUPPORT */