1 =========================================
2 Duktape debug client and JSON debug proxy
3 =========================================
8 Debugger web UI which connects to the Duktape command line tool or any other
9 target supporting the example TCP transport (``examples/debug-trans-socket``)
12 Also provides a JSON debug proxy with a JSON mapping for the Duktape debug
15 For detailed documentation of the debugger internals, see `debugger.rst`__.
17 __ https://github.com/svaarala/duktape/blob/master/doc/debugger.rst
19 Using the debugger web UI
20 =========================
24 * You'll need Node.js v0.10.x or newer. Older Node.js versions don't support
25 the required packages.
27 Compile Duktape command line tool with debugger support (for further options
28 see http://wiki.duktape.org/FeatureOptions.html):
30 * ``DUK_OPT_DEBUGGER_SUPPORT``
32 * ``DUK_OPT_INTERRUPT_COUNTER``
34 * ``DUK_CMDLINE_DEBUGGER_SUPPORT``
36 The source distributable contains a Makefile to build a "duk" command with
39 $ cd <duktape dist directory>
40 $ make -f Makefile.dukdebug
42 The Duktape Git repo "duk" target has debugger support enabled by default::
46 Start Duktape command line tool so that it waits for a debugger connection::
48 # For now we need to be in the directory containing the source files
49 # executed so that the 'fileName' properties of functions will match
50 # that on the debug client.
52 # Using source distributable
53 $ cd <duktape dist directory>
54 $ ./duk --debugger mandel.js
56 # Using Duktape Git repo
57 $ cd <duktape checkout>/tests/ecmascript/
58 $ ../../duk --debugger test-dev-mandel2-func.js
62 # Must be in 'debugger' directory.
65 $ make # runs 'node duk_debug.js'
67 Once the required packages are installed, the NodeJS debug client will be
68 up and running. Open the following in your browser and start debugging:
70 * http://localhost:9092/
72 The debug client automatically attaches to the debug target on startup.
73 If you start the debug target later, you'll need to click "Attach" in the
76 Using the JSON debug proxy
77 ==========================
79 There are two JSON debug proxy implementations: one implemented in DukLuv
80 and another in Node.js.
85 DukLuv (https://github.com/creationix/dukluv) is a small and portable event
86 loop based on LibUV and Duktape with MIT license (like Duktape). As such it's
87 easy to embed in a custom debug client: you just include the DukLuv executable
88 and the JSON proxy source file in your debug client.
92 * Ensure ``cmake`` is installed
94 * ``git clone https://github.com/creationix/dukluv.git``
96 * ``git submodule init; git submodule update``
100 * Binary should appear in:
102 - ``./build/dukluv`` on Linux
104 - ``.\build\Debug\dukluv.exe`` on Windows
108 # Using Makefile; autogenerates duk_debug_meta.json
109 # (You may need to edit DUKLUV in Makefile to point to your DukLuv)
110 $ make runproxydukluv
112 # Manually: see "dukluv duk_debug_proxy.js --help" for help
113 $ .../path/to/dukluv duk_debug_proxy.js
115 Start Duktape command line (or whatever your target is)::
117 $ cd <duktape checkout>/tests/ecmascript/
118 $ ../../duk --debugger test-dev-mandel2-func.js
120 Now connect to the proxy using e.g. telnet::
122 $ telnet localhost 9093
124 The proxy will then connect to the target and you can start issuing commands::
126 $ telnet localhost 9093
128 Connected to localhost.
129 Escape character is '^]'.
130 {"notify":"_TargetConnecting","args":["127.0.0.1",9091]}
131 {"notify":"_TargetConnected","args":["1 10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo"]}
132 {"notify":"Status","command":1,"args":[1,"test-dev-mandel2-func.js","global",58,0]}
133 {"request":"BasicInfo"}
134 {"reply":true,"args":[10499,"v1.4.0-140-gc9a6c7c","duk command built from Duktape repo",1]}
135 {"request":"Eval","args":["print('Hello world!'); 123;"]}
136 {"notify":"Print","command":2,"args":["Hello world!\n"]}
137 {"reply":true,"args":[0,{"type":"number","data":"405ec00000000000"}]}
140 The proxy log provides dumps both JSON and dvalue binary traffic which is
141 quite useful in development::
143 $ make runproxydukluv
144 Running Dukluv based debug proxy
145 "dukluv" duk_debug_proxy.js --log-level 2 --metadata duk_debug_meta.json
146 2016-02-17T13:59:42.308Z INF Proxy: Read proxy metadata from duk_debug_meta.json
147 2016-02-17T13:59:42.325Z INF Proxy: Listening for incoming JSON debug connection on 0.0.0.0:9093, target is 127.0.0.1:9091
148 2016-02-17T13:59:47.994Z INF Proxy: JSON proxy client connected
149 2016-02-17T13:59:47.994Z INF Proxy: Connecting to debug target at 127.0.0.1:9091
150 2016-02-17T13:59:47.994Z INF Proxy: PROXY --> CLIENT: {"notify":"_TargetConnecting","args":["127.0.0.1",9091]}
151 2016-02-17T13:59:47.994Z INF Proxy: Connected to debug target at 127.0.0.1:9091
152 2016-02-17T13:59:48.003Z INF Proxy: PROXY --> CLIENT: {"notify":"_TargetConnected","args":["1 10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo"]}
153 2016-02-17T13:59:48.003Z INF Proxy: Target handshake: {"line":"1 10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo","protocolVersion":1,"text":"10499 v1.4.0-140-gc9a6c7c duk command built from Duktape repo","dukVersion":"1","dukGitDescribe":"10499","targetString":"v1.4.0-140-gc9a6c7c"}
154 2016-02-17T13:59:48.151Z INF Proxy: PROXY <-- TARGET: |04|
155 2016-02-17T13:59:48.152Z INF Proxy: PROXY <-- TARGET: |81|
156 2016-02-17T13:59:48.152Z INF Proxy: PROXY <-- TARGET: |81|
157 2016-02-17T13:59:48.160Z INF Proxy: PROXY <-- TARGET: |78746573742d6465762d6d616e64656c322d66756e632e6a73|
158 2016-02-17T13:59:48.161Z INF Proxy: PROXY <-- TARGET: |66676c6f62616c|
159 2016-02-17T13:59:48.165Z INF Proxy: PROXY <-- TARGET: |ba|
160 2016-02-17T13:59:48.165Z INF Proxy: PROXY <-- TARGET: |80|
161 2016-02-17T13:59:48.165Z INF Proxy: PROXY <-- TARGET: |00|
162 2016-02-17T13:59:48.165Z INF Proxy: PROXY --> CLIENT: {"notify":"Status","command":1,"args":[1,"test-dev-mandel2-func.js","global",58,0]}
163 2016-02-17T13:59:51.289Z INF Proxy: PROXY <-- CLIENT: {"request":"BasicInfo"}
164 2016-02-17T13:59:51.289Z INF Proxy: PROXY --> TARGET: |01|
165 2016-02-17T13:59:51.289Z INF Proxy: PROXY --> TARGET: |90|
166 2016-02-17T13:59:51.289Z INF Proxy: PROXY --> TARGET: |00|
167 2016-02-17T13:59:51.291Z INF Proxy: PROXY <-- TARGET: |02|
168 2016-02-17T13:59:51.291Z INF Proxy: PROXY <-- TARGET: |e903|
169 2016-02-17T13:59:51.292Z INF Proxy: PROXY <-- TARGET: |7376312e342e302d3134302d6763396136633763|
170 2016-02-17T13:59:51.293Z INF Proxy: PROXY <-- TARGET: |12002364756b20636f6d6d616e64206275696c742066726f6d2044756b74617065207265706f|
171 2016-02-17T13:59:51.293Z INF Proxy: PROXY <-- TARGET: |81|
172 2016-02-17T13:59:51.293Z INF Proxy: PROXY <-- TARGET: |00|
173 2016-02-17T13:59:51.293Z INF Proxy: PROXY --> CLIENT: {"reply":true,"args":[10499,"v1.4.0-140-gc9a6c7c","duk command built from Duktape repo",1]}
174 2016-02-17T14:00:06.105Z INF Proxy: PROXY <-- CLIENT: {"request":"Eval","args":["print('Hello world!'); 123;"]}
175 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |01|
176 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |9e|
177 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |7b7072696e74282748656c6c6f20776f726c642127293b203132333b|
178 2016-02-17T14:00:06.105Z INF Proxy: PROXY --> TARGET: |00|
179 2016-02-17T14:00:06.167Z INF Proxy: PROXY <-- TARGET: |04|
180 2016-02-17T14:00:06.167Z INF Proxy: PROXY <-- TARGET: |82|
181 2016-02-17T14:00:06.167Z INF Proxy: PROXY <-- TARGET: |6d48656c6c6f20776f726c64210a|
182 2016-02-17T14:00:06.168Z INF Proxy: PROXY <-- TARGET: |00|
183 2016-02-17T14:00:06.168Z INF Proxy: PROXY --> CLIENT: {"notify":"Print","command":2,"args":["Hello world!\n"]}
184 2016-02-17T14:00:06.171Z INF Proxy: PROXY <-- TARGET: |02|
185 2016-02-17T14:00:06.171Z INF Proxy: PROXY <-- TARGET: |80|
186 2016-02-17T14:00:06.173Z INF Proxy: PROXY <-- TARGET: |1a405ec00000000000|
187 2016-02-17T14:00:06.173Z INF Proxy: PROXY <-- TARGET: |00|
188 2016-02-17T14:00:06.174Z INF Proxy: PROXY --> CLIENT: {"reply":true,"args":[0,{"type":"number","data":"405ec00000000000"}]}
194 A Node.js-based JSON debug proxy is also provided by ``duk_debug.js``::
196 # Same prerequisites as for running the debug client
197 $ make runproxynodejs
199 Start Duktape command line (or whatever your target is)::
201 $ cd <duktape checkout>/tests/ecmascript/
202 $ ../../duk --debugger test-dev-mandel2-func.js
204 You can then connect to localhost:9093 and interact with the proxy.
205 Here's an example session using telnet and manually typed in commands
206 The ``-->`` (send) and ``<--`` (receiver) markers have been added for
207 readability and are not part of the stream::
209 $ telnet localhost 9093
211 Connected to localhost.
212 Escape character is '^]'.
213 <-- {"notify":"_TargetConnected","args":["1 10199 v1.1.0-275-gbd4d610-dirty duk command built from Duktape repo"]}
214 <-- {"notify":"Status","command":1,"args":[1,"test-dev-mandel2-func.js","global",58,0]}
215 --> {"request":"BasicInfo"}
216 <-- {"reply":true,"args":[10199,"v1.1.0-275-gbd4d610-dirty","duk command built from Duktape repo",1]}
217 --> {"request":"Eval", "args":[ "print(Math.PI)" ]}
218 <-- {"notify":"Print","command":2,"args":["3.141592653589793\n"]}
219 <-- {"reply":true,"args":[0,{"type":"undefined"}]}
220 --> {"request":"Resume"}
221 <-- {"reply":true,"args":[]}
222 <-- {"notify":"Status","command":1,"args":[0,"test-dev-mandel2-func.js","global",58,0]}
223 <-- {"notify":"Status","command":1,"args":[0,"test-dev-mandel2-func.js","global",58,0]}
224 <-- {"notify":"Print","command":2,"args":["................................................................................\n"]}
225 <-- {"notify":"Print","command":2,"args":["................................................................................\n"]}
226 <-- {"notify":"Print","command":2,"args":["................................................................................\n"]}
228 <-- {"notify":"_Disconnecting"}
230 A telnet connection allows you to experiment with debug commands by simply
231 copy-pasting debug commands to the telnet session. This is useful even if
232 you decide to implement the binary protocol directly.
234 The debug target used by the proxy can be configured with ``duk_debug.js``
235 command line options.
240 The NodeJS debug client needs to be able to find source code files matching
241 code running on the target ("duk" command line). **The filenames used on the
242 target and on the debug client must match exactly**, because e.g. breakpoints
243 are targeted based on the 'fileName' property of Function objects.
245 The search path can be set using the ``--source-dirs`` option given to
246 ``duk_debug.js``, with the default search paths including only
247 ``../tests/ecmascript/``.
249 The default search path means that if a function on the target has fileName
250 ``foo/bar.js`` it would be loaded from (relative to the duk_debug.js working
251 directory, ``debugger/``)::
253 ../tests/ecmascript/foo/bar.js
255 Similarly, if the filesystem contained::
257 ../tests/ecmascript/baz/quux.js
259 the web UI dropdown would show ``baz/quux.js``. If you selected that file
260 and added a breakpoint, the breakpoint fileName sent to the debug target
261 would be ``baz/quux.js``.
263 .. note:: There's much to improve in the search path. For instance, it'd
264 be nice to add a certain path to search but exclude files based
265 on paths and patterns, etc.
272 +-------------------+
273 | Web browser | [debug UI]
274 +-------------------+
279 +-------------------+
280 | duk_debug.js | [debug client]
281 +-------------------+
284 +----------||---- [example tcp transport] (port 9091)
285 | || (application provides concrete transport)
287 | ||---- [debug protocol stream]
288 | || (between debug client and Duktape)
290 + - - | - - - - -|| - - +
292 : +-------------||-+ : [target]
293 : | application || | :
294 : +-------------||-+ :
297 : +----------||-------- debug transport callbacks
298 : | || : (read, write, peek, read/write flush)
299 : | || : implemented by application
301 : +----------------+ :
303 : +----------------+ :
304 + - - - - - - - - - - - +
306 The debug transport is application specific:
308 * Duktape command line ("duk") and this debug client use an **example** TCP
309 transport as a concrete example.
311 * It is entirely up to the application to come up with the most suitable
312 transport for its environment. Different mechanisms will be needed for
315 The debug protocol running inside the transport is transport independent:
317 * The debug protocol is documented in ``doc/debugger.rst``.
319 * This debug client provides further concrete examples and clarifications
320 on how the protocol can be used.
322 Using a custom transport
323 ========================
325 Quite possibly your target device cannot use the example TCP transport and
326 you need to implement your own transport. You'll need to implement your
327 custom transport both for the target device and for the debug client.
332 Implement the debug transport callbacks needed by ``duk_debugger_attach()``.
334 See ``doc/debugger.rst`` for details and ``examples/debug-trans-socket``
335 for example running code for a TCP transport.
337 Debug client alternative 1: duk_debug.js + custom TCP proxy
338 -----------------------------------------------------------
340 If you don't want to change ``duk_debug.js`` you can implement a TCP proxy
341 which accepts a TCP connection from ``duk_debug.js`` and then uses your
342 custom transport to talk to the target::
344 +--------------+ TCP +-------+ custom +--------+
345 | duk_debug.js | ------> | proxy | ---------> | target |
346 +--------------+ +-------+ +--------+
348 This is a straightforward option and a proxy can be used with other debug
349 clients too (perhaps custom scripts talking to the target etc).
351 You could also use netcat and implement your proxy so that it talks to
352 ``duk_debug.js`` using stdin/stdout.
354 Debug client alternative 2: duk_debug.js + custom NodeJS stream
355 ---------------------------------------------------------------
357 To make ``duk_debug.js`` use a custom transport you need to:
359 * Implement your own transport as NodeJS stream. You can add it directly to
360 ``duk_debug.js`` but it's probably easiest to use a separate module so that
361 the diff to ``duk_debug.js`` stays minimal.
363 * Change ``duk_debug.js`` to use the custom transport instead of a TCP
364 stream. Search for "CUSTOMTRANSPORT" in ``duk_debug.js``.
368 * http://nodejs.org/api/stream.html
370 * https://github.com/substack/stream-handbook
372 Debug client alternative 3: custom debug client
373 -----------------------------------------------
375 You can also implement your own debug client and debug UI with support for
376 your custom transport.
378 You'll also need to implement the client part of the Duktape debugger
379 protocol. See ``doc/debugger.rst`` for the specification and ``duk_debug.js``
380 for example running code which should illustrate the protocol in more detail.
382 The JSON debug proxy allows you to implement a debug client without needing
383 to implement the Duktape binary debug protocol. The JSON protocol provides
384 a roughly 1:1 mapping to the binary protocol but with an easier syntax.