1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2  * vim: set ts=8 sts=2 et sw=2 tw=80:
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 
7 #include "vm/Probes-inl.h"
8 
9 #include "js/CharacterEncoding.h"
10 #include "vm/JSContext.h"
11 
12 #ifdef INCLUDE_MOZILLA_DTRACE
13 #  include "vm/JSScript-inl.h"
14 #endif
15 
16 using namespace js;
17 
18 const char probes::nullName[] = "(null)";
19 const char probes::anonymousName[] = "(anonymous)";
20 
21 bool probes::ProfilingActive = true;
22 
23 #ifdef INCLUDE_MOZILLA_DTRACE
ScriptFilename(const JSScript * script)24 static const char* ScriptFilename(const JSScript* script) {
25   if (!script) {
26     return probes::nullName;
27   }
28   if (!script->filename()) {
29     return probes::anonymousName;
30   }
31   return script->filename();
32 }
33 
FunctionName(JSContext * cx,JSFunction * fun,UniqueChars * bytes)34 static const char* FunctionName(JSContext* cx, JSFunction* fun,
35                                 UniqueChars* bytes) {
36   if (!fun) {
37     return probes::nullName;
38   }
39   if (!fun->displayAtom()) {
40     return probes::anonymousName;
41   }
42   *bytes = JS_EncodeStringToLatin1(cx, fun->displayAtom());
43   return *bytes ? bytes->get() : probes::nullName;
44 }
45 
46 /*
47  * These functions call the DTrace macros for the JavaScript USDT probes.
48  * Originally this code was inlined in the JavaScript code; however since
49  * a number of operations are called, these have been placed into functions
50  * to reduce any negative compiler optimization effect that the addition of
51  * a number of usually unused lines of code would cause.
52  */
DTraceEnterJSFun(JSContext * cx,JSFunction * fun,JSScript * script)53 void probes::DTraceEnterJSFun(JSContext* cx, JSFunction* fun,
54                               JSScript* script) {
55   UniqueChars funNameBytes;
56   JAVASCRIPT_FUNCTION_ENTRY(ScriptFilename(script), probes::nullName,
57                             FunctionName(cx, fun, &funNameBytes));
58 }
59 
DTraceExitJSFun(JSContext * cx,JSFunction * fun,JSScript * script)60 void probes::DTraceExitJSFun(JSContext* cx, JSFunction* fun, JSScript* script) {
61   UniqueChars funNameBytes;
62   JAVASCRIPT_FUNCTION_RETURN(ScriptFilename(script), probes::nullName,
63                              FunctionName(cx, fun, &funNameBytes));
64 }
65 #endif
66