js/jsd/jsd_step.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 /*
michael@0 8 * JavaScript Debugging support - Stepping support
michael@0 9 */
michael@0 10
michael@0 11 #include "jsd.h"
michael@0 12
michael@0 13 /*
michael@0 14 * #define JSD_TRACE 1
michael@0 15 */
michael@0 16
michael@0 17 #ifdef JSD_TRACE
michael@0 18
michael@0 19 static char*
michael@0 20 _indentSpaces(int i)
michael@0 21 {
michael@0 22 #define MAX_INDENT 63
michael@0 23 static char* p = nullptr;
michael@0 24 if(!p)
michael@0 25 {
michael@0 26 p = calloc(1, MAX_INDENT+1);
michael@0 27 if(!p) return "";
michael@0 28 memset(p, ' ', MAX_INDENT);
michael@0 29 }
michael@0 30 if(i > MAX_INDENT) return p;
michael@0 31 return p + MAX_INDENT-i;
michael@0 32 }
michael@0 33
michael@0 34 static void
michael@0 35 _interpreterTrace(JSDContext* jsdc, JSContext *cx, JSAbstractFramePtr frame,
michael@0 36 bool isConstructing, bool before)
michael@0 37 {
michael@0 38 JSDScript* jsdscript = nullptr;
michael@0 39 JSScript * script;
michael@0 40 static indent = 0;
michael@0 41 JSString* funName = nullptr;
michael@0 42
michael@0 43 script = frame.script();
michael@0 44 if(script)
michael@0 45 {
michael@0 46 JSD_LOCK_SCRIPTS(jsdc);
michael@0 47 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, script, frame);
michael@0 48 JSD_UNLOCK_SCRIPTS(jsdc);
michael@0 49 if(jsdscript)
michael@0 50 funName = JSD_GetScriptFunctionId(jsdc, jsdscript);
michael@0 51 }
michael@0 52
michael@0 53 if(before)
michael@0 54 printf("%sentering ", _indentSpaces(indent++));
michael@0 55 else
michael@0 56 printf("%sleaving ", _indentSpaces(--indent));
michael@0 57
michael@0 58 if (!funName)
michael@0 59 printf("TOP_LEVEL");
michael@0 60 else
michael@0 61 JS_FileEscapedString(stdout, funName, 0);
michael@0 62
michael@0 63 if(before)
michael@0 64 {
michael@0 65 jsval thisVal;
michael@0 66
michael@0 67 printf("%s this: ", isConstructing ? "constructing":"");
michael@0 68
michael@0 69 if (JS_GetFrameThis(cx, frame, &thisVal))
michael@0 70 printf("0x%0llx", (uintptr_t) thisVal);
michael@0 71 else
michael@0 72 puts("<unavailable>");
michael@0 73 }
michael@0 74 printf("\n");
michael@0 75 MOZ_ASSERT(indent >= 0);
michael@0 76 }
michael@0 77 #endif
michael@0 78
michael@0 79 bool
michael@0 80 _callHook(JSDContext *jsdc, JSContext *cx, JSAbstractFramePtr frame, bool isConstructing,
michael@0 81 bool before, unsigned type, JSD_CallHookProc hook, void *hookData)
michael@0 82 {
michael@0 83 JSDScript* jsdscript;
michael@0 84 JSScript* jsscript;
michael@0 85 bool hookresult = true;
michael@0 86
michael@0 87 if (!jsdc || !jsdc->inited)
michael@0 88 return false;
michael@0 89
michael@0 90 if (!hook && !(jsdc->flags & JSD_COLLECT_PROFILE_DATA))
michael@0 91 {
michael@0 92 /* no hook to call, no profile data needs to be collected,
michael@0 93 * so there is nothing to do here.
michael@0 94 */
michael@0 95 return hookresult;
michael@0 96 }
michael@0 97
michael@0 98 if (before && isConstructing) {
michael@0 99 JS::RootedValue newObj(cx);
michael@0 100 if (!frame.getThisValue(cx, &newObj))
michael@0 101 return false;
michael@0 102 jsd_Constructing(jsdc, cx, JSVAL_TO_OBJECT(newObj), frame);
michael@0 103 }
michael@0 104
michael@0 105 jsscript = frame.script();
michael@0 106 if (jsscript)
michael@0 107 {
michael@0 108 JSD_LOCK_SCRIPTS(jsdc);
michael@0 109 jsdscript = jsd_FindOrCreateJSDScript(jsdc, cx, jsscript, frame);
michael@0 110 JSD_UNLOCK_SCRIPTS(jsdc);
michael@0 111
michael@0 112 if (jsdscript)
michael@0 113 {
michael@0 114 if (JSD_IS_PROFILE_ENABLED(jsdc, jsdscript))
michael@0 115 {
michael@0 116 JSDProfileData *pdata;
michael@0 117 pdata = jsd_GetScriptProfileData (jsdc, jsdscript);
michael@0 118 if (pdata)
michael@0 119 {
michael@0 120 if (before)
michael@0 121 {
michael@0 122 if (!pdata->lastCallStart)
michael@0 123 {
michael@0 124 int64_t now;
michael@0 125 JSDProfileData *callerpdata;
michael@0 126
michael@0 127 /* Get the time just the once, for consistency. */
michael@0 128 now = JS_Now();
michael@0 129 /* This contains a pointer to the profile data for
michael@0 130 * the caller of this function. */
michael@0 131 callerpdata = jsdc->callingFunctionPData;
michael@0 132 if (callerpdata)
michael@0 133 {
michael@0 134 int64_t ll_delta;
michael@0 135 pdata->caller = callerpdata;
michael@0 136 /* We need to 'stop' the timer for the caller.
michael@0 137 * Use time since last return if appropriate. */
michael@0 138 ll_delta = jsdc->lastReturnTime
michael@0 139 ? now - jsdc->lastReturnTime
michael@0 140 : now - callerpdata->lastCallStart;
michael@0 141 callerpdata->runningTime += ll_delta;
michael@0 142 }
michael@0 143 /* We're the new current function, and no return
michael@0 144 * has happened yet. */
michael@0 145 jsdc->callingFunctionPData = pdata;
michael@0 146 jsdc->lastReturnTime = 0;
michael@0 147 /* This function has no running time (just been
michael@0 148 * called!), and we'll need the call start time. */
michael@0 149 pdata->runningTime = 0;
michael@0 150 pdata->lastCallStart = now;
michael@0 151 } else {
michael@0 152 if (++pdata->recurseDepth > pdata->maxRecurseDepth)
michael@0 153 pdata->maxRecurseDepth = pdata->recurseDepth;
michael@0 154 }
michael@0 155 /* make sure we're called for the return too. */
michael@0 156 hookresult = true;
michael@0 157 } else if (!pdata->recurseDepth && pdata->lastCallStart) {
michael@0 158 int64_t now, ll_delta;
michael@0 159 double delta;
michael@0 160 now = JS_Now();
michael@0 161 ll_delta = now - pdata->lastCallStart;
michael@0 162 delta = ll_delta;
michael@0 163 delta /= 1000.0;
michael@0 164 pdata->totalExecutionTime += delta;
michael@0 165 /* minExecutionTime starts as 0, so we need to overwrite
michael@0 166 * it on the first call always. */
michael@0 167 if ((0 == pdata->callCount) ||
michael@0 168 delta < pdata->minExecutionTime)
michael@0 169 {
michael@0 170 pdata->minExecutionTime = delta;
michael@0 171 }
michael@0 172 if (delta > pdata->maxExecutionTime)
michael@0 173 pdata->maxExecutionTime = delta;
michael@0 174
michael@0 175 /* If we last returned from a function (as opposed to
michael@0 176 * having last entered this function), we need to inc.
michael@0 177 * the running total by the time delta since the last
michael@0 178 * return, and use the running total instead of the
michael@0 179 * delta calculated above. */
michael@0 180 if (jsdc->lastReturnTime)
michael@0 181 {
michael@0 182 /* Add last chunk to running time, and use total
michael@0 183 * running time as 'delta'. */
michael@0 184 ll_delta = now - jsdc->lastReturnTime;
michael@0 185 pdata->runningTime += ll_delta;
michael@0 186 delta = pdata->runningTime;
michael@0 187 delta /= 1000.0;
michael@0 188 }
michael@0 189
michael@0 190 pdata->totalOwnExecutionTime += delta;
michael@0 191 /* See minExecutionTime comment above. */
michael@0 192 if ((0 == pdata->callCount) ||
michael@0 193 delta < pdata->minOwnExecutionTime)
michael@0 194 {
michael@0 195 pdata->minOwnExecutionTime = delta;
michael@0 196 }
michael@0 197 if (delta > pdata->maxOwnExecutionTime)
michael@0 198 pdata->maxOwnExecutionTime = delta;
michael@0 199
michael@0 200 /* Current function is now our caller. */
michael@0 201 jsdc->callingFunctionPData = pdata->caller;
michael@0 202 /* No hanging pointers, please. */
michael@0 203 pdata->caller = nullptr;
michael@0 204 /* Mark the time we returned, and indicate this
michael@0 205 * function is no longer running. */
michael@0 206 jsdc->lastReturnTime = now;
michael@0 207 pdata->lastCallStart = 0;
michael@0 208 ++pdata->callCount;
michael@0 209 } else if (pdata->recurseDepth) {
michael@0 210 --pdata->recurseDepth;
michael@0 211 ++pdata->callCount;
michael@0 212 }
michael@0 213 }
michael@0 214 if (hook)
michael@0 215 jsd_CallCallHook (jsdc, cx, type, hook, hookData);
michael@0 216 } else {
michael@0 217 if (hook)
michael@0 218 hookresult =
michael@0 219 jsd_CallCallHook (jsdc, cx, type, hook, hookData);
michael@0 220 else
michael@0 221 hookresult = true;
michael@0 222 }
michael@0 223 }
michael@0 224 }
michael@0 225
michael@0 226 #ifdef JSD_TRACE
michael@0 227 _interpreterTrace(jsdc, cx, frame, isConstructing, before);
michael@0 228 return true;
michael@0 229 #else
michael@0 230 return hookresult;
michael@0 231 #endif
michael@0 232
michael@0 233 }
michael@0 234
michael@0 235 void *
michael@0 236 jsd_FunctionCallHook(JSContext *cx, JSAbstractFramePtr frame, bool isConstructing,
michael@0 237 bool before, bool *ok, void *closure)
michael@0 238 {
michael@0 239 JSDContext* jsdc;
michael@0 240 JSD_CallHookProc hook;
michael@0 241 void* hookData;
michael@0 242
michael@0 243 jsdc = (JSDContext*) closure;
michael@0 244
michael@0 245 /* local in case jsdc->functionHook gets cleared on another thread */
michael@0 246 JSD_LOCK();
michael@0 247 hook = jsdc->functionHook;
michael@0 248 hookData = jsdc->functionHookData;
michael@0 249 JSD_UNLOCK();
michael@0 250
michael@0 251 if (_callHook (jsdc, cx, frame, isConstructing, before,
michael@0 252 (before) ? JSD_HOOK_FUNCTION_CALL : JSD_HOOK_FUNCTION_RETURN,
michael@0 253 hook, hookData))
michael@0 254 {
michael@0 255 return closure;
michael@0 256 }
michael@0 257
michael@0 258 return nullptr;
michael@0 259 }
michael@0 260
michael@0 261 void *
michael@0 262 jsd_TopLevelCallHook(JSContext *cx, JSAbstractFramePtr frame, bool isConstructing,
michael@0 263 bool before, bool *ok, void *closure)
michael@0 264 {
michael@0 265 JSDContext* jsdc;
michael@0 266 JSD_CallHookProc hook;
michael@0 267 void* hookData;
michael@0 268
michael@0 269 jsdc = (JSDContext*) closure;
michael@0 270
michael@0 271 /* local in case jsdc->toplevelHook gets cleared on another thread */
michael@0 272 JSD_LOCK();
michael@0 273 hook = jsdc->toplevelHook;
michael@0 274 hookData = jsdc->toplevelHookData;
michael@0 275 JSD_UNLOCK();
michael@0 276
michael@0 277 if (_callHook (jsdc, cx, frame, isConstructing, before,
michael@0 278 (before) ? JSD_HOOK_TOPLEVEL_START : JSD_HOOK_TOPLEVEL_END,
michael@0 279 hook, hookData))
michael@0 280 {
michael@0 281 return closure;
michael@0 282 }
michael@0 283
michael@0 284 return nullptr;
michael@0 285
michael@0 286 }

mercurial