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
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/
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
15 * The Original Code is mozilla.org code.
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.
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.
36 * ***** END LICENSE BLOCK ***** */
39 * JavaScript Debugging support - Stepping support
54 static char* p
= NULL
;
57 p
= calloc(1, MAX_INDENT
+1);
59 memset(p
, ' ', MAX_INDENT
);
61 if(i
> MAX_INDENT
) return p
;
62 return p
+ MAX_INDENT
-i
;
66 _interpreterTrace(JSDContext
* jsdc
, JSContext
*cx
, JSStackFrame
*fp
,
69 JSDScript
* jsdscript
= NULL
;
73 const char* funName
= NULL
;
75 script
= JS_GetFrameScript(cx
, fp
);
78 JSD_LOCK_SCRIPTS(jsdc
);
79 jsdscript
= jsd_FindJSDScript(jsdc
, script
);
80 JSD_UNLOCK_SCRIPTS(jsdc
);
82 funName
= JSD_GetScriptFunctionName(jsdc
, jsdscript
);
85 funName
= "TOP_LEVEL";
89 buf
= JS_smprintf("%sentering %s %s this: %0x\n",
90 _indentSpaces(indent
++),
92 JS_IsConstructorFrame(cx
, fp
) ? "constructing":"",
93 (int)JS_GetFrameThis(cx
, fp
));
97 buf
= JS_smprintf("%sleaving %s\n",
98 _indentSpaces(--indent
),
101 JS_ASSERT(indent
>= 0);
112 _callHook(JSDContext
*jsdc
, JSContext
*cx
, JSStackFrame
*fp
, JSBool before
,
113 uintN type
, JSD_CallHookProc hook
, void *hookData
)
115 JSDScript
* jsdscript
;
117 JSBool hookresult
= JS_TRUE
;
119 if (!jsdc
|| !jsdc
->inited
)
122 if (!hook
&& !(jsdc
->flags
& JSD_COLLECT_PROFILE_DATA
) &&
123 jsdc
->flags
& JSD_DISABLE_OBJECT_TRACE
)
125 /* no hook to call, no profile data needs to be collected, and
126 * the client has object tracing disabled, so there is nothing
132 if (before
&& JS_IsConstructorFrame(cx
, fp
))
133 jsd_Constructing(jsdc
, cx
, JS_GetFrameThis(cx
, fp
), fp
);
135 jsscript
= JS_GetFrameScript(cx
, fp
);
138 JSD_LOCK_SCRIPTS(jsdc
);
139 jsdscript
= jsd_FindJSDScript(jsdc
, jsscript
);
140 JSD_UNLOCK_SCRIPTS(jsdc
);
144 if (JSD_IS_PROFILE_ENABLED(jsdc
, jsdscript
))
146 JSDProfileData
*pdata
;
147 pdata
= jsd_GetScriptProfileData (jsdc
, jsdscript
);
152 if (JSLL_IS_ZERO(pdata
->lastCallStart
))
155 JSDProfileData
*callerpdata
;
157 /* Get the time just the once, for consistency. */
159 /* This contains a pointer to the profile data for
160 * the caller of this function. */
161 callerpdata
= jsdc
->callingFunctionPData
;
165 pdata
->caller
= callerpdata
;
166 /* We need to 'stop' the timer for the caller.
167 * Use time since last return if appropriate. */
168 if (JSLL_IS_ZERO(jsdc
->lastReturnTime
))
170 JSLL_SUB(ll_delta
, now
, callerpdata
->lastCallStart
);
172 JSLL_SUB(ll_delta
, now
, jsdc
->lastReturnTime
);
174 JSLL_ADD(callerpdata
->runningTime
, callerpdata
->runningTime
, ll_delta
);
176 /* We're the new current function, and no return
177 * has happened yet. */
178 jsdc
->callingFunctionPData
= pdata
;
179 jsdc
->lastReturnTime
= 0;
180 /* This function has no running time (just been
181 * called!), and we'll need the call start time. */
182 pdata
->runningTime
= 0;
183 pdata
->lastCallStart
= now
;
185 if (++pdata
->recurseDepth
> pdata
->maxRecurseDepth
)
186 pdata
->maxRecurseDepth
= pdata
->recurseDepth
;
188 /* make sure we're called for the return too. */
189 hookresult
= JS_TRUE
;
190 } else if (!pdata
->recurseDepth
&&
191 !JSLL_IS_ZERO(pdata
->lastCallStart
)) {
195 JSLL_SUB(ll_delta
, now
, pdata
->lastCallStart
);
196 JSLL_L2D(delta
, ll_delta
);
198 pdata
->totalExecutionTime
+= delta
;
199 /* minExecutionTime starts as 0, so we need to overwrite
200 * it on the first call always. */
201 if ((0 == pdata
->callCount
) ||
202 delta
< pdata
->minExecutionTime
)
204 pdata
->minExecutionTime
= delta
;
206 if (delta
> pdata
->maxExecutionTime
)
207 pdata
->maxExecutionTime
= delta
;
209 /* If we last returned from a function (as opposed to
210 * having last entered this function), we need to inc.
211 * the running total by the time delta since the last
212 * return, and use the running total instead of the
213 * delta calculated above. */
214 if (!JSLL_IS_ZERO(jsdc
->lastReturnTime
))
216 /* Add last chunk to running time, and use total
217 * running time as 'delta'. */
218 JSLL_SUB(ll_delta
, now
, jsdc
->lastReturnTime
);
219 JSLL_ADD(pdata
->runningTime
, pdata
->runningTime
, ll_delta
);
220 JSLL_L2D(delta
, pdata
->runningTime
);
224 pdata
->totalOwnExecutionTime
+= delta
;
225 /* See minExecutionTime comment above. */
226 if ((0 == pdata
->callCount
) ||
227 delta
< pdata
->minOwnExecutionTime
)
229 pdata
->minOwnExecutionTime
= delta
;
231 if (delta
> pdata
->maxOwnExecutionTime
)
232 pdata
->maxOwnExecutionTime
= delta
;
234 /* Current function is now our caller. */
235 jsdc
->callingFunctionPData
= pdata
->caller
;
236 /* No hanging pointers, please. */
237 pdata
->caller
= NULL
;
238 /* Mark the time we returned, and indicate this
239 * function is no longer running. */
240 jsdc
->lastReturnTime
= now
;
241 pdata
->lastCallStart
= 0;
243 } else if (pdata
->recurseDepth
) {
244 --pdata
->recurseDepth
;
249 jsd_CallCallHook (jsdc
, cx
, type
, hook
, hookData
);
253 jsd_CallCallHook (jsdc
, cx
, type
, hook
, hookData
);
255 hookresult
= JS_TRUE
;
261 _interpreterTrace(jsdc
, cx
, fp
, before
);
269 void * JS_DLL_CALLBACK
270 jsd_FunctionCallHook(JSContext
*cx
, JSStackFrame
*fp
, JSBool before
,
271 JSBool
*ok
, void *closure
)
274 JSD_CallHookProc hook
;
277 jsdc
= (JSDContext
*) closure
;
279 /* local in case jsdc->functionHook gets cleared on another thread */
281 hook
= jsdc
->functionHook
;
282 hookData
= jsdc
->functionHookData
;
285 if (_callHook (jsdc
, cx
, fp
, before
,
286 (before
) ? JSD_HOOK_FUNCTION_CALL
: JSD_HOOK_FUNCTION_RETURN
,
295 void * JS_DLL_CALLBACK
296 jsd_TopLevelCallHook(JSContext
*cx
, JSStackFrame
*fp
, JSBool before
,
297 JSBool
*ok
, void *closure
)
300 JSD_CallHookProc hook
;
303 jsdc
= (JSDContext
*) closure
;
305 /* local in case jsdc->toplevelHook gets cleared on another thread */
307 hook
= jsdc
->toplevelHook
;
308 hookData
= jsdc
->toplevelHookData
;
311 if (_callHook (jsdc
, cx
, fp
, before
,
312 (before
) ? JSD_HOOK_TOPLEVEL_START
: JSD_HOOK_TOPLEVEL_END
,