File: debug_interface.rst

package info (click to toggle)
squirrel3 3.1-8.3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,380 kB
  • sloc: cpp: 12,722; ansic: 917; makefile: 316; python: 40
file content (58 lines) | stat: -rw-r--r-- 2,371 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
.. _embedding_debug_interface:

===============
Debug Interface
===============

The squirrel VM exposes a very simple debug interface that allows to easily built a full
featured debugger.
Through the functions sq_setdebughook and sq_setnativedebughook is possible in fact to set a callback function that
will be called every time the VM executes an new line of a script or if a function get
called/returns. The callback will pass as argument the current line the current source and the
current function name (if any).::

    SQUIRREL_API void sq_setdebughook(HSQUIRRELVM v);

or ::

    SQUIRREL_API void sq_setnativedebughook(HSQUIRRELVM v,SQDEBUGHOOK hook);

The following code shows how a debug hook could look like(obviously is possible to
implement this function in C as well). ::

    function debughook(event_type,sourcefile,line,funcname)
    {
        local fname=funcname?funcname:"unknown";
        local srcfile=sourcefile?sourcefile:"unknown"
        switch (event_type) {
        case 'l': //called every line(that contains some code)
            ::print("LINE line [" + line + "] func [" + fname + "]");
            ::print("file [" + srcfile + "]\n");
            break;
        case 'c': //called when a function has been called
            ::print("LINE line [" + line + "] func [" + fname + "]");
            ::print("file [" + srcfile + "]\n");
            break;
        case 'r': //called when a function returns
            ::print("LINE line [" + line + "] func [" + fname + "]");
            ::print("file [" + srcfile + "]\n");
            break;
        }
    }

The parameter *event_type* can be 'l' ,'c' or 'r' ; a hook with a 'l' event is called for each line that
gets executed, 'c' every time a function gets called and 'r' every time a function returns.

A full-featured debugger always allows displaying local variables and calls stack.
The call stack information are retrieved through sq_getstackinfos()::

    SQInteger sq_stackinfos(HSQUIRRELVM v,SQInteger level,SQStackInfos *si);

While the local variables info through sq_getlocal()::

    SQInteger sq_getlocal(HSQUIRRELVM v,SQUnsignedInteger level,SQUnsignedInteger nseq);

In order to receive line callbacks the scripts have to be compiled with debug infos enabled
this is done through sq_enabledebuginfo(); ::

    void sq_enabledebuginfo(HSQUIRRELVM v, SQInteger debuginfo);