1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * vim: set ts=8 sw=4 et tw=80:
4 * ***** BEGIN LICENSE BLOCK *****
5 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * Copyright (C) 2007 Sun Microsystems, Inc. All Rights Reserved.
20 * Brendan Eich <brendan@mozilla.org>
22 * Alternatively, the contents of this file may be used under the terms of
23 * either of the GNU General Public License Version 2 or later (the "GPL"),
24 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25 * in which case the provisions of the GPL or the LGPL are applicable instead
26 * of those above. If you wish to allow use of your version of this file only
27 * under the terms of either the GPL or the LGPL, and not to allow others to
28 * use your version of this file under the terms of the MPL, indicate your
29 * decision by deleting the provisions above and replace them with the notice
30 * and other provisions required by the GPL or the LGPL. If you do not delete
31 * the provisions above, a recipient may use your version of this file under
32 * the terms of any one of the MPL, the GPL or the LGPL.
34 * ***** END LICENSE BLOCK ***** */
47 #include "jsdtracef.h"
48 #include <sys/types.h>
50 #define TYPEOF(cx,v) (JSVAL_IS_NULL(v) ? JSTYPE_NULL : JS_TypeOfValue(cx,v))
52 static char dempty
[] = "<null>";
55 jsdtrace_fun_classname(const JSFunction
*fun
)
58 !FUN_INTERPRETED(fun
) &&
59 !(fun
->flags
& JSFUN_TRCINFO
) &&
61 ? (char *)FUN_CLASP(fun
)->name
66 jsdtrace_filename(JSStackFrame
*fp
)
68 return (fp
&& fp
->script
&& fp
->script
->filename
)
69 ? (char *)fp
->script
->filename
74 jsdtrace_fun_linenumber(JSContext
*cx
, const JSFunction
*fun
)
76 if (fun
&& FUN_INTERPRETED(fun
))
77 return (int) JS_GetScriptBaseLineNumber(cx
, FUN_SCRIPT(fun
));
83 jsdtrace_frame_linenumber(JSContext
*cx
, JSStackFrame
*fp
)
86 return (int) js_FramePCToLineNumber(cx
, fp
);
92 * This function is used to convert function arguments and return value (jsval)
93 * into the following based on each value's type tag:
103 * All are presented as void * for DTrace consumers to use, after shifting or
104 * masking out the JavaScript type bits. This allows D scripts to use ints and
105 * booleans directly and copyinstr() for string arguments, when types are known
108 * This is used by the function-args and function-rval probes, which also
109 * provide raw (unmasked) jsvals should type info be useful from D scripts.
112 jsdtrace_jsvaltovoid(JSContext
*cx
, const jsval argval
)
114 JSType type
= TYPEOF(cx
, argval
);
119 return (void *)JS_TYPE_STR(type
);
122 return (void *)JSVAL_TO_BOOLEAN(argval
);
125 return (void *)js_GetStringBytes(cx
, JSVAL_TO_STRING(argval
));
128 if (JSVAL_IS_INT(argval
))
129 return (void *)JSVAL_TO_INT(argval
);
130 return JSVAL_TO_DOUBLE(argval
);
133 return JSVAL_TO_GCTHING(argval
);
139 jsdtrace_fun_name(JSContext
*cx
, const JSFunction
*fun
)
150 * TODO: maybe do more work here to figure out the name of the property
151 * or variable that held the anonymous function that we're calling, if anyone
152 * cares; an easy workaround is to just give your anonymous functions names.
157 name
= (char *)js_GetStringBytes(cx
, ATOM_TO_STRING(atom
));
158 return name
? name
: dempty
;
162 * These functions call the DTrace macros for the JavaScript USDT probes.
163 * Originally this code was inlined in the JavaScript code; however since
164 * a number of operations are called, these have been placed into functions
165 * to reduce any negative compiler optimization effect that the addition of
166 * a number of usually unused lines of code would cause.
169 jsdtrace_function_entry(JSContext
*cx
, JSStackFrame
*fp
, const JSFunction
*fun
)
171 JAVASCRIPT_FUNCTION_ENTRY(
172 jsdtrace_filename(fp
),
173 jsdtrace_fun_classname(fun
),
174 jsdtrace_fun_name(cx
, fun
)
179 jsdtrace_function_info(JSContext
*cx
, JSStackFrame
*fp
, JSStackFrame
*dfp
,
182 JAVASCRIPT_FUNCTION_INFO(
183 jsdtrace_filename(fp
),
184 jsdtrace_fun_classname(fun
),
185 jsdtrace_fun_name(cx
, fun
),
186 jsdtrace_fun_linenumber(cx
, fun
),
187 jsdtrace_filename(dfp
),
188 jsdtrace_frame_linenumber(cx
, dfp
)
193 jsdtrace_function_args(JSContext
*cx
, JSStackFrame
*fp
, const JSFunction
*fun
,
194 jsuint argc
, jsval
*argv
)
196 JAVASCRIPT_FUNCTION_ARGS(
197 jsdtrace_filename(fp
),
198 jsdtrace_fun_classname(fun
),
199 jsdtrace_fun_name(cx
, fun
),
201 (argc
> 0) ? jsdtrace_jsvaltovoid(cx
, argv
[0]) : 0,
202 (argc
> 1) ? jsdtrace_jsvaltovoid(cx
, argv
[1]) : 0,
203 (argc
> 2) ? jsdtrace_jsvaltovoid(cx
, argv
[2]) : 0,
204 (argc
> 3) ? jsdtrace_jsvaltovoid(cx
, argv
[3]) : 0,
205 (argc
> 4) ? jsdtrace_jsvaltovoid(cx
, argv
[4]) : 0
210 jsdtrace_function_rval(JSContext
*cx
, JSStackFrame
*fp
, JSFunction
*fun
, jsval
*rval
)
212 JAVASCRIPT_FUNCTION_RVAL(
213 jsdtrace_filename(fp
),
214 jsdtrace_fun_classname(fun
),
215 jsdtrace_fun_name(cx
, fun
),
216 jsdtrace_fun_linenumber(cx
, fun
),
218 jsdtrace_jsvaltovoid(cx
, *rval
)
223 jsdtrace_function_return(JSContext
*cx
, JSStackFrame
*fp
, JSFunction
*fun
)
225 JAVASCRIPT_FUNCTION_RETURN(
226 jsdtrace_filename(fp
),
227 jsdtrace_fun_classname(fun
),
228 jsdtrace_fun_name(cx
, fun
)
233 jsdtrace_object_create_start(JSStackFrame
*fp
, JSClass
*clasp
)
235 JAVASCRIPT_OBJECT_CREATE_START(jsdtrace_filename(fp
), (char *)clasp
->name
);
239 jsdtrace_object_create_done(JSStackFrame
*fp
, JSClass
*clasp
)
241 JAVASCRIPT_OBJECT_CREATE_DONE(jsdtrace_filename(fp
), (char *)clasp
->name
);
245 jsdtrace_object_create(JSContext
*cx
, JSClass
*clasp
, JSObject
*obj
)
247 JAVASCRIPT_OBJECT_CREATE(
248 jsdtrace_filename(cx
->fp
),
251 jsdtrace_frame_linenumber(cx
, cx
->fp
)
256 jsdtrace_object_finalize(JSObject
*obj
)
260 clasp
= obj
->getClass();
262 /* the first arg is NULL - reserved for future use (filename?) */
263 JAVASCRIPT_OBJECT_FINALIZE(NULL
, (char *)clasp
->name
, (uintptr_t)obj
);
267 jsdtrace_execute_start(JSScript
*script
)
269 JAVASCRIPT_EXECUTE_START(
270 script
->filename
? (char *)script
->filename
: dempty
,
276 jsdtrace_execute_done(JSScript
*script
)
278 JAVASCRIPT_EXECUTE_DONE(
279 script
->filename
? (char *)script
->filename
: dempty
,