PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/js/lib/Socket.IO-node/support/expresso/deps/jscoverage/js/jsdtracef.c

http://github.com/onedayitwillmake/RealtimeMultiplayerNodeJs
C | 318 lines | 209 code | 35 blank | 74 comment | 19 complexity | cff6fe677ff0d32d62cb3f76f1a0c942 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: set ts=8 sw=4 et tw=80:
  3. *
  4. * ***** BEGIN LICENSE BLOCK *****
  5. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  6. *
  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/
  11. *
  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
  15. * License.
  16. *
  17. * Copyright (C) 2007 Sun Microsystems, Inc. All Rights Reserved.
  18. *
  19. * Contributor(s):
  20. * Brendan Eich <brendan@mozilla.org>
  21. *
  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.
  33. *
  34. * ***** END LICENSE BLOCK ***** */
  35. #include "jsapi.h"
  36. #include "jsutil.h"
  37. #include "jsatom.h"
  38. #include "jscntxt.h"
  39. #include "jsdbgapi.h"
  40. #include "jsfun.h"
  41. #include "jsinterp.h"
  42. #include "jsobj.h"
  43. #include "jsscript.h"
  44. #include "jsstr.h"
  45. #include "jsdtracef.h"
  46. #include <sys/types.h>
  47. #define TYPEOF(cx,v) (JSVAL_IS_NULL(v) ? JSTYPE_NULL : JS_TypeOfValue(cx,v))
  48. static char dempty[] = "<null>";
  49. char *
  50. jsdtrace_funcclass_name(JSFunction *fun)
  51. {
  52. return (!FUN_INTERPRETED(fun) &&
  53. !(fun->flags & JSFUN_TRACEABLE) &&
  54. FUN_CLASP(fun))
  55. ? (char *)FUN_CLASP(fun)->name
  56. : dempty;
  57. }
  58. char *
  59. jsdtrace_filename(JSStackFrame *fp)
  60. {
  61. while (fp && fp->script == NULL)
  62. fp = fp->down;
  63. return (fp && fp->script && fp->script->filename)
  64. ? (char *)fp->script->filename
  65. : dempty;
  66. }
  67. int
  68. jsdtrace_linenumber(JSContext *cx, JSStackFrame *fp)
  69. {
  70. while (fp && fp->script == NULL)
  71. fp = fp->down;
  72. return (fp && fp->regs)
  73. ? (int) js_PCToLineNumber(cx, fp->script, fp->regs->pc)
  74. : -1;
  75. }
  76. /*
  77. * This function is used to convert function arguments and return value (jsval)
  78. * into the following based on each value's type tag:
  79. *
  80. * jsval returned
  81. * -------------------
  82. * STRING -> char *
  83. * INT -> int
  84. * DOUBLE -> double *
  85. * BOOLEAN -> int
  86. * OBJECT -> void *
  87. *
  88. * All are presented as void * for DTrace consumers to use, after shifting or
  89. * masking out the JavaScript type bits. This allows D scripts to use ints and
  90. * booleans directly and copyinstr() for string arguments, when types are known
  91. * beforehand.
  92. *
  93. * This is used by the function-args and function-rval probes, which also
  94. * provide raw (unmasked) jsvals should type info be useful from D scripts.
  95. */
  96. void *
  97. jsdtrace_jsvaltovoid(JSContext *cx, jsval argval)
  98. {
  99. JSType type = TYPEOF(cx, argval);
  100. switch (type) {
  101. case JSTYPE_NULL:
  102. case JSTYPE_VOID:
  103. return (void *)JS_TYPE_STR(type);
  104. case JSTYPE_BOOLEAN:
  105. return (void *)JSVAL_TO_BOOLEAN(argval);
  106. case JSTYPE_STRING:
  107. return (void *)js_GetStringBytes(cx, JSVAL_TO_STRING(argval));
  108. case JSTYPE_NUMBER:
  109. if (JSVAL_IS_INT(argval))
  110. return (void *)JSVAL_TO_INT(argval);
  111. return JSVAL_TO_DOUBLE(argval);
  112. default:
  113. return JSVAL_TO_GCTHING(argval);
  114. }
  115. /* NOTREACHED */
  116. }
  117. char *
  118. jsdtrace_function_name(JSContext *cx, JSStackFrame *fp, JSFunction *fun)
  119. {
  120. JSAtom *atom;
  121. JSFrameRegs *regs;
  122. JSScript *script;
  123. jsbytecode *pc;
  124. char *name;
  125. atom = fun->atom;
  126. if (!atom) {
  127. if (fp->fun != fun || !fp->down)
  128. return dempty;
  129. regs = fp->down->regs;
  130. if (!regs)
  131. return dempty;
  132. /*
  133. * An anonymous function called from an active script or interpreted
  134. * function: try to fetch the variable or property name by which the
  135. * anonymous function was invoked.
  136. */
  137. pc = regs->pc;
  138. script = fp->down->script;
  139. switch ((JSOp) *pc) {
  140. case JSOP_CALL:
  141. case JSOP_EVAL:
  142. JS_ASSERT(fp->argv == regs->sp - (int)GET_ARGC(pc));
  143. /*
  144. * FIXME bug 422864: update this code to use the pc stack from the
  145. * decompiler.
  146. */
  147. break;
  148. default: ;
  149. }
  150. switch ((JSOp) *pc) {
  151. case JSOP_CALLNAME:
  152. case JSOP_CALLPROP:
  153. case JSOP_NAME:
  154. case JSOP_SETNAME:
  155. case JSOP_GETPROP:
  156. case JSOP_SETPROP:
  157. GET_ATOM_FROM_BYTECODE(script, pc, 0, atom);
  158. break;
  159. case JSOP_CALLELEM:
  160. case JSOP_GETELEM:
  161. case JSOP_SETELEM:
  162. case JSOP_CALLGVAR:
  163. case JSOP_GETGVAR:
  164. case JSOP_SETGVAR:
  165. case JSOP_CALLARG:
  166. case JSOP_CALLLOCAL:
  167. /* FIXME: try to recover a name from these ops. */
  168. /* FALL THROUGH */
  169. default:
  170. return dempty;
  171. }
  172. }
  173. name = (char *)js_GetStringBytes(cx, ATOM_TO_STRING(atom));
  174. return name ? name : dempty;
  175. }
  176. /*
  177. * These functions call the DTrace macros for the JavaScript USDT probes.
  178. * Originally this code was inlined in the JavaScript code; however since
  179. * a number of operations are called, these have been placed into functions
  180. * to reduce any negative compiler optimization effect that the addition of
  181. * a number of usually unused lines of code would cause.
  182. */
  183. void
  184. jsdtrace_function_entry(JSContext *cx, JSStackFrame *fp, JSFunction *fun)
  185. {
  186. JAVASCRIPT_FUNCTION_ENTRY(
  187. jsdtrace_filename(fp),
  188. jsdtrace_funcclass_name(fun),
  189. jsdtrace_function_name(cx, fp, fun)
  190. );
  191. }
  192. void
  193. jsdtrace_function_info(JSContext *cx, JSStackFrame *fp, JSStackFrame *dfp,
  194. JSFunction *fun)
  195. {
  196. JAVASCRIPT_FUNCTION_INFO(
  197. jsdtrace_filename(fp),
  198. jsdtrace_funcclass_name(fun),
  199. jsdtrace_function_name(cx, fp, fun),
  200. fp->script->lineno,
  201. jsdtrace_filename(dfp),
  202. jsdtrace_linenumber(cx, dfp)
  203. );
  204. }
  205. void
  206. jsdtrace_function_args(JSContext *cx, JSStackFrame *fp, JSFunction *fun)
  207. {
  208. JAVASCRIPT_FUNCTION_ARGS(
  209. jsdtrace_filename(fp),
  210. jsdtrace_funcclass_name(fun),
  211. jsdtrace_function_name(cx, fp, fun),
  212. fp->argc, (void *)fp->argv,
  213. (fp->argc > 0) ? jsdtrace_jsvaltovoid(cx, fp->argv[0]) : 0,
  214. (fp->argc > 1) ? jsdtrace_jsvaltovoid(cx, fp->argv[1]) : 0,
  215. (fp->argc > 2) ? jsdtrace_jsvaltovoid(cx, fp->argv[2]) : 0,
  216. (fp->argc > 3) ? jsdtrace_jsvaltovoid(cx, fp->argv[3]) : 0,
  217. (fp->argc > 4) ? jsdtrace_jsvaltovoid(cx, fp->argv[4]) : 0
  218. );
  219. }
  220. void
  221. jsdtrace_function_rval(JSContext *cx, JSStackFrame *fp, JSFunction *fun)
  222. {
  223. JAVASCRIPT_FUNCTION_RVAL(
  224. jsdtrace_filename(fp),
  225. jsdtrace_funcclass_name(fun),
  226. jsdtrace_function_name(cx, fp, fun),
  227. jsdtrace_linenumber(cx, fp), (void *)fp->rval,
  228. jsdtrace_jsvaltovoid(cx, fp->rval)
  229. );
  230. }
  231. void
  232. jsdtrace_function_return(JSContext *cx, JSStackFrame *fp, JSFunction *fun)
  233. {
  234. JAVASCRIPT_FUNCTION_RETURN(
  235. jsdtrace_filename(fp),
  236. jsdtrace_funcclass_name(fun),
  237. jsdtrace_function_name(cx, fp, fun)
  238. );
  239. }
  240. void
  241. jsdtrace_object_create_start(JSStackFrame *fp, JSClass *clasp)
  242. {
  243. JAVASCRIPT_OBJECT_CREATE_START(jsdtrace_filename(fp), (char *)clasp->name);
  244. }
  245. void
  246. jsdtrace_object_create_done(JSStackFrame *fp, JSClass *clasp)
  247. {
  248. JAVASCRIPT_OBJECT_CREATE_DONE(jsdtrace_filename(fp), (char *)clasp->name);
  249. }
  250. void
  251. jsdtrace_object_create(JSContext *cx, JSClass *clasp, JSObject *obj)
  252. {
  253. JAVASCRIPT_OBJECT_CREATE(
  254. jsdtrace_filename(cx->fp),
  255. (char *)clasp->name,
  256. (uintptr_t)obj,
  257. jsdtrace_linenumber(cx, cx->fp)
  258. );
  259. }
  260. void
  261. jsdtrace_object_finalize(JSObject *obj)
  262. {
  263. JSClass *clasp;
  264. clasp = LOCKED_OBJ_GET_CLASS(obj);
  265. /* the first arg is NULL - reserved for future use (filename?) */
  266. JAVASCRIPT_OBJECT_FINALIZE(NULL, (char *)clasp->name, (uintptr_t)obj);
  267. }
  268. void
  269. jsdtrace_execute_start(JSScript *script)
  270. {
  271. JAVASCRIPT_EXECUTE_START(
  272. script->filename ? (char *)script->filename : dempty,
  273. script->lineno
  274. );
  275. }
  276. void
  277. jsdtrace_execute_done(JSScript *script)
  278. {
  279. JAVASCRIPT_EXECUTE_DONE(
  280. script->filename ? (char *)script->filename : dempty,
  281. script->lineno
  282. );
  283. }