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;
72 JSString* funName = NULL;
74 script = JS_GetFrameScript(cx, fp);
77 JSD_LOCK_SCRIPTS(jsdc);
78 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, fp);
79 JSD_UNLOCK_SCRIPTS(jsdc);
81 funName = JSD_GetScriptFunctionId(jsdc, jsdscript);
85 printf("%sentering ", _indentSpaces(indent++));
87 printf("%sleaving ", _indentSpaces(--indent));
92 JS_FileEscapedString(stdout, funName, 0);
98 printf("%s this: ", JS_IsConstructorFrame(cx, fp) ? "constructing":"");
100 if (JS_GetFrameThis(cx, fp, &thisVal))
101 printf("0x%0llx", (JSUword) thisVal);
103 puts("<unavailable>");
106 JS_ASSERT(indent >= 0);
111 _callHook(JSDContext *jsdc, JSContext *cx, JSStackFrame *fp, JSBool before,
112 uintN type, JSD_CallHookProc hook, void *hookData)
114 JSDScript* jsdscript;
116 JSBool hookresult = JS_TRUE;
118 if (!jsdc || !jsdc->inited)
121 if (!hook && !(jsdc->flags & JSD_COLLECT_PROFILE_DATA))
123 /* no hook to call, no profile data needs to be collected,
124 * so there is nothing to do here.
129 if (before && JS_IsConstructorFrame(cx, fp)) {
131 if (!JS_GetFrameThis(cx, fp, &newObj))
133 jsd_Constructing(jsdc, cx, JSVAL_TO_OBJECT(newObj), fp);
136 jsscript = JS_GetFrameScript(cx, fp);
139 JSD_LOCK_SCRIPTS(jsdc);
140 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, jsscript, fp);
141 JSD_UNLOCK_SCRIPTS(jsdc);
145 if (JSD_IS_PROFILE_ENABLED(jsdc, jsdscript))
147 JSDProfileData *pdata;
148 pdata = jsd_GetScriptProfileData (jsdc, jsdscript);
153 if (JSLL_IS_ZERO(pdata->lastCallStart))
156 JSDProfileData *callerpdata;
158 /* Get the time just the once, for consistency. */
160 /* This contains a pointer to the profile data for
161 * the caller of this function. */
162 callerpdata = jsdc->callingFunctionPData;
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))
171 JSLL_SUB(ll_delta, now, callerpdata->lastCallStart);
173 JSLL_SUB(ll_delta, now, jsdc->lastReturnTime);
175 JSLL_ADD(callerpdata->runningTime, callerpdata->runningTime, ll_delta);
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;
186 if (++pdata->recurseDepth > pdata->maxRecurseDepth)
187 pdata->maxRecurseDepth = pdata->recurseDepth;
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)) {
196 JSLL_SUB(ll_delta, now, pdata->lastCallStart);
197 JSLL_L2D(delta, ll_delta);
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)
205 pdata->minExecutionTime = delta;
207 if (delta > pdata->maxExecutionTime)
208 pdata->maxExecutionTime = delta;
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))
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);
225 pdata->totalOwnExecutionTime += delta;
226 /* See minExecutionTime comment above. */
227 if ((0 == pdata->callCount) ||
228 delta < pdata->minOwnExecutionTime)
230 pdata->minOwnExecutionTime = delta;
232 if (delta > pdata->maxOwnExecutionTime)
233 pdata->maxOwnExecutionTime = delta;
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;
244 } else if (pdata->recurseDepth) {
245 --pdata->recurseDepth;
250 jsd_CallCallHook (jsdc, cx, type, hook, hookData);
254 jsd_CallCallHook (jsdc, cx, type, hook, hookData);
256 hookresult = JS_TRUE;
262 _interpreterTrace(jsdc, cx, fp, before);
271 jsd_FunctionCallHook(JSContext *cx, JSStackFrame *fp, JSBool before,
272 JSBool *ok, void *closure)
275 JSD_CallHookProc hook;
278 jsdc = (JSDContext*) closure;
280 /* local in case jsdc->functionHook gets cleared on another thread */
282 hook = jsdc->functionHook;
283 hookData = jsdc->functionHookData;
286 if (_callHook (jsdc, cx, fp, before,
287 (before) ? JSD_HOOK_FUNCTION_CALL : JSD_HOOK_FUNCTION_RETURN,
297 jsd_TopLevelCallHook(JSContext *cx, JSStackFrame *fp, JSBool before,
298 JSBool *ok, void *closure)
301 JSD_CallHookProc hook;
304 jsdc = (JSDContext*) closure;
306 /* local in case jsdc->toplevelHook gets cleared on another thread */
308 hook = jsdc->toplevelHook;
309 hookData = jsdc->toplevelHookData;
312 if (_callHook (jsdc, cx, fp, before,
313 (before) ? JSD_HOOK_TOPLEVEL_START : JSD_HOOK_TOPLEVEL_END,