Bug 465497, Wait for AUS to notice new snippets before testing them, r=bhearsum
[mozilla-1.9.git] / js / jsd / jsd_step.c
blob14c6caddba3acff5e48b2cf7455d7eddcb53c43a
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
13 * License.
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.
22 * Contributor(s):
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
42 #include "jsd.h"
45 * #define JSD_TRACE 1
48 #ifdef JSD_TRACE
50 static char*
51 _indentSpaces(int i)
53 #define MAX_INDENT 63
54 static char* p = NULL;
55 if(!p)
57 p = calloc(1, MAX_INDENT+1);
58 if(!p) return "";
59 memset(p, ' ', MAX_INDENT);
61 if(i > MAX_INDENT) return p;
62 return p + MAX_INDENT-i;
65 static void
66 _interpreterTrace(JSDContext* jsdc, JSContext *cx, JSStackFrame *fp,
67 JSBool before)
69 JSDScript* jsdscript = NULL;
70 JSScript * script;
71 static indent = 0;
72 char* buf;
73 const char* funName = NULL;
75 script = JS_GetFrameScript(cx, fp);
76 if(script)
78 JSD_LOCK_SCRIPTS(jsdc);
79 jsdscript = jsd_FindJSDScript(jsdc, script);
80 JSD_UNLOCK_SCRIPTS(jsdc);
81 if(jsdscript)
82 funName = JSD_GetScriptFunctionName(jsdc, jsdscript);
84 if(!funName)
85 funName = "TOP_LEVEL";
87 if(before)
89 buf = JS_smprintf("%sentering %s %s this: %0x\n",
90 _indentSpaces(indent++),
91 funName,
92 JS_IsConstructorFrame(cx, fp) ? "constructing":"",
93 (int)JS_GetFrameThis(cx, fp));
95 else
97 buf = JS_smprintf("%sleaving %s\n",
98 _indentSpaces(--indent),
99 funName);
101 JS_ASSERT(indent >= 0);
103 if(!buf)
104 return;
106 printf(buf);
107 free(buf);
109 #endif
111 JSBool
112 _callHook(JSDContext *jsdc, JSContext *cx, JSStackFrame *fp, JSBool before,
113 uintN type, JSD_CallHookProc hook, void *hookData)
115 JSDScript* jsdscript;
116 JSScript* jsscript;
117 JSBool hookresult = JS_TRUE;
119 if (!jsdc || !jsdc->inited)
120 return JS_FALSE;
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
127 * to do here.
129 return hookresult;
132 if (before && JS_IsConstructorFrame(cx, fp))
133 jsd_Constructing(jsdc, cx, JS_GetFrameThis(cx, fp), fp);
135 jsscript = JS_GetFrameScript(cx, fp);
136 if (jsscript)
138 JSD_LOCK_SCRIPTS(jsdc);
139 jsdscript = jsd_FindJSDScript(jsdc, jsscript);
140 JSD_UNLOCK_SCRIPTS(jsdc);
142 if (jsdscript)
144 if (JSD_IS_PROFILE_ENABLED(jsdc, jsdscript))
146 JSDProfileData *pdata;
147 pdata = jsd_GetScriptProfileData (jsdc, jsdscript);
148 if (pdata)
150 if (before)
152 if (JSLL_IS_ZERO(pdata->lastCallStart))
154 int64 now;
155 JSDProfileData *callerpdata;
157 /* Get the time just the once, for consistency. */
158 now = JS_Now();
159 /* This contains a pointer to the profile data for
160 * the caller of this function. */
161 callerpdata = jsdc->callingFunctionPData;
162 if (callerpdata)
164 int64 ll_delta;
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);
171 } else {
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;
184 } else {
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)) {
192 int64 now, ll_delta;
193 jsdouble delta;
194 now = JS_Now();
195 JSLL_SUB(ll_delta, now, pdata->lastCallStart);
196 JSLL_L2D(delta, ll_delta);
197 delta /= 1000.0;
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);
221 delta /= 1000.0;
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;
242 ++pdata->callCount;
243 } else if (pdata->recurseDepth) {
244 --pdata->recurseDepth;
245 ++pdata->callCount;
248 if (hook)
249 jsd_CallCallHook (jsdc, cx, type, hook, hookData);
250 } else {
251 if (hook)
252 hookresult =
253 jsd_CallCallHook (jsdc, cx, type, hook, hookData);
254 else
255 hookresult = JS_TRUE;
260 #ifdef JSD_TRACE
261 _interpreterTrace(jsdc, cx, fp, before);
262 return JS_TRUE;
263 #else
264 return hookresult;
265 #endif
269 void * JS_DLL_CALLBACK
270 jsd_FunctionCallHook(JSContext *cx, JSStackFrame *fp, JSBool before,
271 JSBool *ok, void *closure)
273 JSDContext* jsdc;
274 JSD_CallHookProc hook;
275 void* hookData;
277 jsdc = (JSDContext*) closure;
279 /* local in case jsdc->functionHook gets cleared on another thread */
280 JSD_LOCK();
281 hook = jsdc->functionHook;
282 hookData = jsdc->functionHookData;
283 JSD_UNLOCK();
285 if (_callHook (jsdc, cx, fp, before,
286 (before) ? JSD_HOOK_FUNCTION_CALL : JSD_HOOK_FUNCTION_RETURN,
287 hook, hookData))
289 return closure;
292 return NULL;
295 void * JS_DLL_CALLBACK
296 jsd_TopLevelCallHook(JSContext *cx, JSStackFrame *fp, JSBool before,
297 JSBool *ok, void *closure)
299 JSDContext* jsdc;
300 JSD_CallHookProc hook;
301 void* hookData;
303 jsdc = (JSDContext*) closure;
305 /* local in case jsdc->toplevelHook gets cleared on another thread */
306 JSD_LOCK();
307 hook = jsdc->toplevelHook;
308 hookData = jsdc->toplevelHookData;
309 JSD_UNLOCK();
311 if (_callHook (jsdc, cx, fp, before,
312 (before) ? JSD_HOOK_TOPLEVEL_START : JSD_HOOK_TOPLEVEL_END,
313 hook, hookData))
315 return closure;
318 return NULL;