migrate substitution keywords to SVN
[reactos.git] / reactos / lib / kjs / src / debug.c
1 /*
2 * Debugging utilities.
3 * Copyright (c) 1998 New Generation Software (NGS) Oy
4 *
5 * Author: Markku Rossi <mtr@ngs.fi>
6 */
7
8 /*
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the Free
21 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
22 * MA 02111-1307, USA
23 */
24
25 /*
26 * $Source: /cygdrive/c/RCVS/CVS/ReactOS/reactos/lib/kjs/src/debug.c,v $
27 * $Id$
28 */
29
30 #include "jsint.h"
31
32 /*
33 * Global functions.
34 */
35
36 #define STRING_MAX_PRINT_LEN 10
37
38 void
39 js_vm_stacktrace (JSVirtualMachine *vm, unsigned int num_frames)
40 {
41 unsigned int frame = 0;
42 JSNode *sp = vm->sp;
43 void *pc = vm->pc;
44 JSNode *fp;
45 char buf[512];
46 int i;
47
48 sprintf (buf, "VM: stacktrace: stacksize=%d, used=%d%s",
49 vm->stack_size,
50 (vm->stack + vm->stack_size - sp),
51 JS_HOST_LINE_BREAK);
52 js_iostream_write (vm->s_stderr, buf, strlen (buf));
53
54 /* STACKFRAME */
55
56 /* Find frame pointer. */
57 for (fp = sp + 1; fp->type != JS_IPTR; fp++)
58 ;
59
60 /* The first iptr is the return address. */
61 fp++;
62
63 /* The second iptr is the with pointer. */
64 fp++;
65
66 /* The third item is a JS_ARGS_FIX node. */
67 assert (fp->type == JS_ARGS_FIX);
68 fp++;
69
70 while (fp && frame < num_frames)
71 {
72 JSNode *n;
73 const char *func_name = js_vm_func_name (vm, pc);
74
75 sprintf (buf, "#%-3u %s%s:", frame++, func_name,
76 func_name[0] == '.' ? "" : "()");
77 js_iostream_write (vm->s_stderr, buf, strlen (buf));
78
79 if (vm->verbose_stacktrace)
80 {
81 sprintf (buf,
82 " ra=0x%lx, wp=0x%lx, af=%d:%d, ofp=0x%lx",
83 (unsigned long) (fp - 3)->u.iptr,
84 (unsigned long) JS_WITHPTR->u.iptr,
85 JS_ARGS_FIXP->u.args_fix.argc,
86 JS_ARGS_FIXP->u.args_fix.delta,
87 (unsigned long) fp->u.iptr);
88 js_iostream_write (vm->s_stderr, buf, strlen (buf));
89 }
90
91 for (n = sp + 1; n != fp - 3; n++)
92 {
93 switch (n->type)
94 {
95 case JS_UNDEFINED:
96 sprintf (buf, " undefined");
97 break;
98
99 case JS_NULL:
100 sprintf (buf, " null");
101 break;
102
103 case JS_BOOLEAN:
104 sprintf (buf, " %s", n->u.vboolean ? "true" : "false");
105 break;
106
107 case JS_INTEGER:
108 sprintf (buf, " %ld", n->u.vinteger);
109 break;
110
111 case JS_STRING:
112 if (n->u.vstring->len > STRING_MAX_PRINT_LEN)
113 sprintf (buf, " \"%.*s...\"",
114 STRING_MAX_PRINT_LEN,
115 n->u.vstring->data);
116 else
117 sprintf (buf, " \"%.*s\"",
118 (int) n->u.vstring->len,
119 n->u.vstring->data);
120 break;
121
122 case JS_FLOAT:
123 sprintf (buf, " %g", n->u.vfloat);
124 break;
125
126 case JS_ARRAY:
127 sprintf (buf, " array");
128 break;
129
130 case JS_OBJECT:
131 sprintf (buf, " object");
132 break;
133
134 case JS_SYMBOL:
135 sprintf (buf, " %s", js_vm_symname (vm, n->u.vsymbol));
136 break;
137
138 case JS_BUILTIN:
139 sprintf (buf, " builtin");
140 break;
141
142 case JS_FUNC:
143 sprintf (buf, " function");
144 break;
145
146 case JS_IPTR:
147 sprintf (buf, " 0x%lx", (unsigned long) n->u.iptr);
148 break;
149
150 case JS_ARGS_FIX:
151 sprintf (buf, " <num=%d, delta=%d>", n->u.args_fix.argc,
152 n->u.args_fix.delta);
153 break;
154
155 default:
156 sprintf (buf, " type=%d???", n->type);
157 break;
158 }
159
160 js_iostream_write (vm->s_stderr, buf, strlen (buf));
161 }
162
163 js_iostream_write (vm->s_stderr, JS_HOST_LINE_BREAK,
164 JS_HOST_LINE_BREAK_LEN);
165
166 /* Move to the caller. */
167 sp = fp;
168 pc = fp[-3].u.iptr;
169 fp = fp->u.iptr;
170 }
171 }