Introduced a JavaScript recording script that is loaded dynamically.

This commit is contained in:
Andreas Gal 2008-06-03 21:01:23 -07:00
parent be39054e45
commit 9684e2070f
7 changed files with 104 additions and 16 deletions

View File

@ -2864,8 +2864,8 @@ void
js_TraceTraceMonitor(JSTracer *trc, JSTraceMonitor *tm)
{
TRACE_JSVALS(trc, tm->loopTableSize, tm->loopTable, "loop table");
if (tm->recorderScriptObject)
JS_CALL_OBJECT_TRACER(trc, tm->recorderScriptObject, "recorder script object");
if (tm->recorder)
JS_CALL_OBJECT_TRACER(trc, tm->recorder, "recorder object");
}
void

View File

@ -2760,7 +2760,7 @@ JS_INTERPRET(JSContext *cx)
JS_END_MACRO
# define BEGIN_CASE(OP) L_##OP: \
trace_stop(#OP);
trace_stop(cx, #OP);
# define TRACE_CASE(OP) L_##OP:
# define END_CASE(OP) DO_NEXT_OP(OP##_LENGTH);
# define END_VARLEN_CASE DO_NEXT_OP(len);
@ -2780,7 +2780,7 @@ JS_INTERPRET(JSContext *cx)
JS_END_MACRO
# define BEGIN_CASE(OP) case OP: \
trace_stop(#OP);
trace_stop(cx, #OP);
# define TRACE_CASE(OP) case OP:
# define END_CASE(OP) END_CASE_LEN(OP##_LENGTH)
# define END_CASE_LEN(n) END_CASE_LENX(n)
@ -3046,6 +3046,7 @@ JS_INTERPRET(JSContext *cx)
* compiled a tree for us, and in that case reuse that
* tree instead of recording a new one.
*/
trace_start(cx, regs.pc);
obj = js_NewObject(cx, &js_ObjectClass, NULL, NULL, 0);
if (!obj)
goto error;
@ -6968,7 +6969,7 @@ JS_INTERPRET(JSContext *cx)
#define DEFINE_HANDLER(handler) \
handler: \
trace_stop(#handler);
trace_stop(cx, #handler);
DEFINE_HANDLER(error)
JS_ASSERT((size_t)(regs.pc - script->code) < script->length);

View File

@ -21,7 +21,7 @@
* Andreas Gal <gal@uci.edu>
*
* Contributor(s):
* Brendan Eich <brendan@mozilla.org
* Brendan Eich <brendan@mozilla.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
@ -454,12 +454,19 @@ PRIMITIVE(guard_both_jsvals_are_string)(jsval& a, jsval& b)
return JSVAL_IS_STRING(a) && JSVAL_IS_STRING(b);
}
static inline void
PRIMITIVE(trace_start)(JSContext* cx, jsbytecode* pc)
{
jsval args[] = { native_pointer_to_jsval(pc) };
js_CallRecorder(cx, "start", 1, args);
}
/*
* Unsupported opcodes trigger a trace stop condition and cause the trace
* recorder to abandon the current trace.
*/
static inline void
PRIMITIVE(trace_stop)(const char* op)
PRIMITIVE(trace_stop)(JSContext* cx, const char* op)
{
/* If we are not tracing, this is a no-op. */
}

View File

@ -88,3 +88,27 @@ js_GrowLoopTable(JSContext* cx, uint32 index)
}
return JS_TRUE;
}
JSObject*
js_GetRecorder(JSContext* cx)
{
JSTraceMonitor* tm = &JS_TRACE_MONITOR(cx);
if (tm->recorder)
return tm->recorder;
JSScript* script = JS_CompileFile(cx, JS_GetGlobalObject(cx), "recorder.js");
JS_ASSERT(script != NULL);
jsval result;
JSBool ok = JS_ExecuteScript(cx, JS_GetGlobalObject(cx), script, &result);
JS_ASSERT(ok && JSVAL_IS_OBJECT(result));
return tm->recorder = JSVAL_TO_OBJECT(result);
}
jsval
js_CallRecorder(JSContext* cx, const char* fn, uintN argc, jsval* argv)
{
jsval rval;
JSBool ok;
ok = JS_CallFunctionName(cx, js_GetRecorder(cx), fn, argc, argv, &rval);
JS_ASSERT(ok);
return rval;
}

View File

@ -57,8 +57,7 @@
struct JSTraceMonitor {
jsval *loopTable;
uint32 loopTableSize;
JSScript *recorderScript;
JSObject *recorderScriptObject;
JSObject *recorder;
};
#define TRACE_THRESHOLD 10
@ -66,5 +65,17 @@ struct JSTraceMonitor {
uint32 js_AllocateLoopTableSlot(JSRuntime *rt);
void js_FreeLoopTableSlot(JSRuntime *rt, uint32 slot);
JSBool js_GrowLoopTable(JSContext *cx, uint32 index);
jsval js_CallRecorder(JSContext* cx, const char* fn, uintN argc, jsval* argv);
/*
* The recorder needs to keep track of native machine addresses. We speculate
* that these addresses can be wrapped into a 31-bit integer, which is true
* for most 32-bit machines.
*/
static inline jsval
native_pointer_to_jsval(void* p) {
JS_ASSERT(INT_FITS_IN_JSVAL((int)p));
return INT_TO_JSVAL((int)p);
}
#endif /* jstracer_h___ */

View File

@ -414,14 +414,14 @@ guard_both_jsvals_are_string(jsval& a, jsval& b)
return interp_guard_both_jsvals_are_string(a, b);
}
/*
* Unsupported opcodes trigger a trace stop condition and cause the trace
* recorder to abandon the current trace.
*/
static inline void
trace_stop(const char* op)
static inline void
trace_start(JSContext* cx, jsbytecode* pc)
{
}
static inline void
trace_stop(JSContext* cx, const char* op)
{
/* If we are not tracing, this is a no-op. */
}
#endif /* jstracerinlines_h___ */

45
js/src/recorder.js Normal file
View File

@ -0,0 +1,45 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
* vim: set ts=8 sw=4 et tw=78:
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is Mozilla SpiderMonkey JavaScript 1.9 code, released
* May 28, 2008.
*
* The Initial Developer of the Original Code is
* Andreas Gal <gal@uci.edu>
*
* Contributor(s):
* Brendan Eich <brendan@mozilla.org>
*
* Alternatively, the contents of this file may be used under the terms of
* either of the GNU General Public License Version 2 or later (the "GPL"),
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
eval({
start: function(pc) {
print("Recording at @" + pc);
}
})