2 * Copyright (C) 2008 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "ExecState.h"
34 #include "FunctionCallProfile.h"
35 #include "JSGlobalObject.h"
42 static Profiler* sharedProfiler = 0;
43 static const char* Script = "[SCRIPT] ";
45 static void getStackNames(Vector<UString>&, ExecState*);
46 static void getStackNames(Vector<UString>&, ExecState*, JSObject*);
47 static void getStackNames(Vector<UString>&, ExecState*, const UString& sourceURL, int startingLineNumber);
48 static UString getFunctionName(FunctionImp*);
50 Profiler* Profiler::profiler()
53 sharedProfiler = new Profiler;
54 return sharedProfiler;
57 void Profiler::startProfiling(unsigned pageGroupIdentifier, const UString& title)
62 m_pageGroupIdentifier = pageGroupIdentifier;
64 m_currentProfile = Profile::create(title);
68 void Profiler::stopProfiling()
72 if (!m_currentProfile)
75 m_currentProfile->stopProfiling();
76 m_allProfiles.append(m_currentProfile.release());
79 void Profiler::willExecute(ExecState* exec, JSObject* calledFunction)
81 if (!m_profiling || exec->lexicalGlobalObject()->pageGroupIdentifier() != m_pageGroupIdentifier)
84 Vector<UString> callStackNames;
85 getStackNames(callStackNames, exec, calledFunction);
86 m_currentProfile->willExecute(callStackNames);
89 void Profiler::willExecute(ExecState* exec, const UString& sourceURL, int startingLineNumber)
91 if (!m_profiling || exec->lexicalGlobalObject()->pageGroupIdentifier() != m_pageGroupIdentifier)
94 Vector<UString> callStackNames;
95 getStackNames(callStackNames, exec, sourceURL, startingLineNumber);
96 m_currentProfile->willExecute(callStackNames);
99 void Profiler::didExecute(ExecState* exec, JSObject* calledFunction)
101 if (!m_profiling || exec->lexicalGlobalObject()->pageGroupIdentifier() != m_pageGroupIdentifier)
104 Vector<UString> callStackNames;
105 getStackNames(callStackNames, exec, calledFunction);
106 m_currentProfile->didExecute(callStackNames);
109 void Profiler::didExecute(ExecState* exec, const UString& sourceURL, int startingLineNumber)
111 if (!m_profiling || exec->lexicalGlobalObject()->pageGroupIdentifier() != m_pageGroupIdentifier)
114 Vector<UString> callStackNames;
115 getStackNames(callStackNames, exec, sourceURL, startingLineNumber);
116 m_currentProfile->didExecute(callStackNames);
119 void getStackNames(Vector<UString>& names, ExecState* exec)
121 for (ExecState* currentState = exec; currentState; currentState = currentState->callingExecState()) {
123 if (FunctionImp* functionImp = currentState->function())
124 names.prepend(getFunctionName(functionImp));
125 else if (ScopeNode* scopeNode = currentState->scopeNode())
126 names.prepend(Script + scopeNode->sourceURL() + ": " + UString::from(scopeNode->lineNo() + 1)); // FIXME: Why is the line number always off by one?
130 void getStackNames(Vector<UString>& names, ExecState* exec, JSObject* calledFunction)
132 getStackNames(names, exec);
133 if (calledFunction->inherits(&FunctionImp::info))
134 names.append(getFunctionName(static_cast<FunctionImp*>(calledFunction)));
135 else if (calledFunction->inherits(&InternalFunctionImp::info))
136 names.append(static_cast<InternalFunctionImp*>(calledFunction)->functionName().ustring());
140 void getStackNames(Vector<UString>& names, ExecState* exec, const UString& sourceURL, int startingLineNumber)
142 getStackNames(names, exec);
143 names.append(Script + sourceURL + ": " + UString::from(startingLineNumber + 1));
146 UString getFunctionName(FunctionImp* functionImp)
148 UString name = functionImp->functionName().ustring();
149 int lineNumber = functionImp->body->lineNo();
150 UString URL = functionImp->body->sourceURL();
152 return (name.isEmpty() ? "[anonymous function]" : name) + " " + URL + ": " + UString::from(lineNumber);
155 void Profiler::printDataInspectorStyle(unsigned whichProfile) const
157 m_allProfiles[whichProfile]->printDataInspectorStyle();
160 void Profiler::printDataSampleStyle(unsigned whichProfile) const
162 m_allProfiles[whichProfile]->printDataSampleStyle();
165 void Profiler::debugLog(UString message)
167 printf("Profiler Log: %s\n", message.UTF8String().c_str());