Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / jsd / jsd_step.c
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4  *
5  * The contents of this file are subject to the Mozilla Public License Version
6  * 1.1 (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  * http://www.mozilla.org/MPL/
9  *
10  * Software distributed under the License is distributed on an "AS IS" basis,
11  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12  * for the specific language governing rights and limitations under the
13  * License.
14  *
15  * The Original Code is mozilla.org code.
16  *
17  * The Initial Developer of the Original Code is
18  * Netscape Communications Corporation.
19  * Portions created by the Initial Developer are Copyright (C) 1998
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Contributor(s):
23  *
24  * Alternatively, the contents of this file may be used under the terms of
25  * either the GNU General Public License Version 2 or later (the "GPL"), or
26  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27  * in which case the provisions of the GPL or the LGPL are applicable instead
28  * of those above. If you wish to allow use of your version of this file only
29  * under the terms of either the GPL or the LGPL, and not to allow others to
30  * use your version of this file under the terms of the MPL, indicate your
31  * decision by deleting the provisions above and replace them with the notice
32  * and other provisions required by the GPL or the LGPL. If you do not delete
33  * the provisions above, a recipient may use your version of this file under
34  * the terms of any one of the MPL, the GPL or the LGPL.
35  *
36  * ***** END LICENSE BLOCK ***** */
37
38 /*
39  * JavaScript Debugging support - Stepping support
40  */
41
42 #include "jsd.h"
43
44 /*
45 * #define JSD_TRACE 1
46 */
47
48 #ifdef JSD_TRACE
49
50 static char*
51 _indentSpaces(int i)
52 {
53 #define MAX_INDENT 63
54     static char* p = NULL;
55     if(!p)
56     {
57         p = calloc(1, MAX_INDENT+1);
58         if(!p) return "";
59         memset(p, ' ', MAX_INDENT);
60     }
61     if(i > MAX_INDENT) return p;
62     return p + MAX_INDENT-i;
63 }
64
65 static void
66 _interpreterTrace(JSDContext* jsdc, JSContext *cx, JSStackFrame *fp,
67                   JSBool before)
68 {
69     JSDScript* jsdscript = NULL;
70     JSScript * script;
71     static indent = 0;
72     JSString* funName = NULL;
73
74     script = JS_GetFrameScript(cx, fp);
75     if(script)
76     {
77         JSD_LOCK_SCRIPTS(jsdc);
78         jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, fp);
79         JSD_UNLOCK_SCRIPTS(jsdc);
80         if(jsdscript)
81             funName = JSD_GetScriptFunctionId(jsdc, jsdscript);
82     }
83
84     if(before)
85         printf("%sentering ", _indentSpaces(indent++));
86     else
87         printf("%sleaving ", _indentSpaces(--indent));
88
89     if (!funName)
90         printf("TOP_LEVEL");
91     else
92         JS_FileEscapedString(stdout, funName, 0);
93
94     if(before)
95     {
96         jsval thisVal;
97
98         printf("%s this: ", JS_IsConstructorFrame(cx, fp) ? "constructing":"");
99
100         if (JS_GetFrameThis(cx, fp, &thisVal))
101             printf("0x%0llx", (JSUword) thisVal);
102         else
103             puts("<unavailable>");
104     }
105     printf("\n");
106     JS_ASSERT(indent >= 0);
107 }
108 #endif
109
110 JSBool
111 _callHook(JSDContext *jsdc, JSContext *cx, JSStackFrame *fp, JSBool before,
112           uintN type, JSD_CallHookProc hook, void *hookData)
113 {
114     JSDScript*        jsdscript;
115     JSScript*         jsscript;
116     JSBool            hookresult = JS_TRUE;
117     
118     if (!jsdc || !jsdc->inited)
119         return JS_FALSE;
120
121     if (!hook && !(jsdc->flags & JSD_COLLECT_PROFILE_DATA))
122     {
123         /* no hook to call, no profile data needs to be collected,
124          * so there is nothing to do here.
125          */
126         return hookresult;
127     }
128     
129     if (before && JS_IsConstructorFrame(cx, fp)) {
130         jsval newObj;
131         if (!JS_GetFrameThis(cx, fp, &newObj))
132             return JS_FALSE;
133         jsd_Constructing(jsdc, cx, JSVAL_TO_OBJECT(newObj), fp);
134     }
135
136     jsscript = JS_GetFrameScript(cx, fp);
137     if (jsscript)
138     {
139         JSD_LOCK_SCRIPTS(jsdc);
140         jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, jsscript, fp);
141         JSD_UNLOCK_SCRIPTS(jsdc);
142     
143         if (jsdscript)
144         {
145             if (JSD_IS_PROFILE_ENABLED(jsdc, jsdscript))
146             {
147                 JSDProfileData *pdata;
148                 pdata = jsd_GetScriptProfileData (jsdc, jsdscript);
149                 if (pdata)
150                 {
151                     if (before)
152                     {
153                         if (JSLL_IS_ZERO(pdata->lastCallStart))
154                         {
155                             int64 now;
156                             JSDProfileData *callerpdata;
157                             
158                             /* Get the time just the once, for consistency. */
159                             now = JS_Now();
160                             /* This contains a pointer to the profile data for
161                              * the caller of this function. */
162                             callerpdata = jsdc->callingFunctionPData;
163                             if (callerpdata)
164                             {
165                                 int64 ll_delta;
166                                 pdata->caller = callerpdata;
167                                 /* We need to 'stop' the timer for the caller.
168                                  * Use time since last return if appropriate. */
169                                 if (JSLL_IS_ZERO(jsdc->lastReturnTime))
170                                 {
171                                     JSLL_SUB(ll_delta, now, callerpdata->lastCallStart);
172                                 } else {
173                                     JSLL_SUB(ll_delta, now, jsdc->lastReturnTime);
174                                 }
175                                 JSLL_ADD(callerpdata->runningTime, callerpdata->runningTime, ll_delta);
176                             }
177                             /* We're the new current function, and no return
178                              * has happened yet. */
179                             jsdc->callingFunctionPData = pdata;
180                             jsdc->lastReturnTime = 0;
181                             /* This function has no running time (just been
182                              * called!), and we'll need the call start time. */
183                             pdata->runningTime = 0;
184                             pdata->lastCallStart = now;
185                         } else {
186                             if (++pdata->recurseDepth > pdata->maxRecurseDepth)
187                                 pdata->maxRecurseDepth = pdata->recurseDepth;
188                         }
189                         /* make sure we're called for the return too. */
190                         hookresult = JS_TRUE;
191                     } else if (!pdata->recurseDepth &&
192                                !JSLL_IS_ZERO(pdata->lastCallStart)) {
193                         int64 now, ll_delta;
194                         jsdouble delta;
195                         now = JS_Now();
196                         JSLL_SUB(ll_delta, now, pdata->lastCallStart);
197                         JSLL_L2D(delta, ll_delta);
198                         delta /= 1000.0;
199                         pdata->totalExecutionTime += delta;
200                         /* minExecutionTime starts as 0, so we need to overwrite
201                          * it on the first call always. */
202                         if ((0 == pdata->callCount) ||
203                             delta < pdata->minExecutionTime)
204                         {
205                             pdata->minExecutionTime = delta;
206                         }
207                         if (delta > pdata->maxExecutionTime)
208                             pdata->maxExecutionTime = delta;
209                         
210                         /* If we last returned from a function (as opposed to
211                          * having last entered this function), we need to inc.
212                          * the running total by the time delta since the last
213                          * return, and use the running total instead of the
214                          * delta calculated above. */
215                         if (!JSLL_IS_ZERO(jsdc->lastReturnTime))
216                         {
217                             /* Add last chunk to running time, and use total
218                              * running time as 'delta'. */
219                             JSLL_SUB(ll_delta, now, jsdc->lastReturnTime);
220                             JSLL_ADD(pdata->runningTime, pdata->runningTime, ll_delta);
221                             JSLL_L2D(delta, pdata->runningTime);
222                             delta /= 1000.0;
223                         }
224                         
225                         pdata->totalOwnExecutionTime += delta;
226                         /* See minExecutionTime comment above. */
227                         if ((0 == pdata->callCount) ||
228                             delta < pdata->minOwnExecutionTime)
229                         {
230                             pdata->minOwnExecutionTime = delta;
231                         }
232                         if (delta > pdata->maxOwnExecutionTime)
233                             pdata->maxOwnExecutionTime = delta;
234                         
235                         /* Current function is now our caller. */
236                         jsdc->callingFunctionPData = pdata->caller;
237                         /* No hanging pointers, please. */
238                         pdata->caller = NULL;
239                         /* Mark the time we returned, and indicate this
240                          * function is no longer running. */
241                         jsdc->lastReturnTime = now;
242                         pdata->lastCallStart = 0;
243                         ++pdata->callCount;
244                     } else if (pdata->recurseDepth) {
245                         --pdata->recurseDepth;
246                         ++pdata->callCount;
247                     }
248                 }
249                 if (hook)
250                     jsd_CallCallHook (jsdc, cx, type, hook, hookData);
251             } else {
252                 if (hook)
253                     hookresult = 
254                         jsd_CallCallHook (jsdc, cx, type, hook, hookData);
255                 else
256                     hookresult = JS_TRUE;
257             }
258         }
259     }
260
261 #ifdef JSD_TRACE
262     _interpreterTrace(jsdc, cx, fp, before);
263     return JS_TRUE;
264 #else
265     return hookresult;
266 #endif
267
268 }
269
270 void *
271 jsd_FunctionCallHook(JSContext *cx, JSStackFrame *fp, JSBool before,
272                      JSBool *ok, void *closure)
273 {
274     JSDContext*       jsdc;
275     JSD_CallHookProc  hook;
276     void*             hookData;
277
278     jsdc = (JSDContext*) closure;
279     
280     /* local in case jsdc->functionHook gets cleared on another thread */
281     JSD_LOCK();
282     hook     = jsdc->functionHook;
283     hookData = jsdc->functionHookData;
284     JSD_UNLOCK();
285     
286     if (_callHook (jsdc, cx, fp, before,
287                    (before) ? JSD_HOOK_FUNCTION_CALL : JSD_HOOK_FUNCTION_RETURN,
288                    hook, hookData))
289     {
290         return closure;
291     }
292     
293     return NULL;
294 }
295
296 void *
297 jsd_TopLevelCallHook(JSContext *cx, JSStackFrame *fp, JSBool before,
298                      JSBool *ok, void *closure)
299 {
300     JSDContext*       jsdc;
301     JSD_CallHookProc  hook;
302     void*             hookData;
303
304     jsdc = (JSDContext*) closure;
305
306     /* local in case jsdc->toplevelHook gets cleared on another thread */
307     JSD_LOCK();
308     hook     = jsdc->toplevelHook;
309     hookData = jsdc->toplevelHookData;
310     JSD_UNLOCK();
311     
312     if (_callHook (jsdc, cx, fp, before,
313                    (before) ? JSD_HOOK_TOPLEVEL_START : JSD_HOOK_TOPLEVEL_END,
314                    hook, hookData))
315     {
316         return closure;
317     }
318     
319     return NULL;
320     
321 }