2 * Copyright (C) 2008, 2014 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
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ProfileGenerator.h"
29 #include "CallFrame.h"
30 #include "CodeBlock.h"
31 #include "JSGlobalObject.h"
32 #include "JSStringRef.h"
33 #include "JSFunction.h"
34 #include "LegacyProfiler.h"
35 #include "JSCInlines.h"
37 #include "StackVisitor.h"
42 PassRefPtr
<ProfileGenerator
> ProfileGenerator::create(ExecState
* exec
, const String
& title
, unsigned uid
)
44 return adoptRef(new ProfileGenerator(exec
, title
, uid
));
47 ProfileGenerator::ProfileGenerator(ExecState
* exec
, const String
& title
, unsigned uid
)
48 : m_origin(exec
? exec
->lexicalGlobalObject() : 0)
49 , m_profileGroup(exec
? exec
->lexicalGlobalObject()->profileGroup() : 0)
50 , m_foundConsoleStartParent(false)
52 m_profile
= Profile::create(title
, uid
);
53 m_currentNode
= m_head
= m_profile
->head();
55 addParentForConsoleStart(exec
);
58 class AddParentForConsoleStartFunctor
{
60 AddParentForConsoleStartFunctor(ExecState
* exec
, RefPtr
<ProfileNode
>& head
, RefPtr
<ProfileNode
>& currentNode
)
62 , m_hasSkippedFirstFrame(false)
63 , m_foundParent(false)
65 , m_currentNode(currentNode
)
69 bool foundParent() const { return m_foundParent
; }
71 StackVisitor::Status
operator()(StackVisitor
& visitor
)
73 if (!m_hasSkippedFirstFrame
) {
74 m_hasSkippedFirstFrame
= true;
75 return StackVisitor::Continue
;
80 visitor
->computeLineAndColumn(line
, column
);
81 m_currentNode
= ProfileNode::create(m_exec
, LegacyProfiler::createCallIdentifier(m_exec
, visitor
->callee(), visitor
->sourceURL(), line
, column
), m_head
.get(), m_head
.get());
82 m_head
->insertNode(m_currentNode
.get());
85 return StackVisitor::Done
;
90 bool m_hasSkippedFirstFrame
;
92 RefPtr
<ProfileNode
>& m_head
;
93 RefPtr
<ProfileNode
>& m_currentNode
;
96 void ProfileGenerator::addParentForConsoleStart(ExecState
* exec
)
98 AddParentForConsoleStartFunctor
functor(exec
, m_head
, m_currentNode
);
99 exec
->iterate(functor
);
101 m_foundConsoleStartParent
= functor
.foundParent();
104 const String
& ProfileGenerator::title() const
106 return m_profile
->title();
109 void ProfileGenerator::willExecute(ExecState
* callerCallFrame
, const CallIdentifier
& callIdentifier
)
111 if (JAVASCRIPTCORE_PROFILE_WILL_EXECUTE_ENABLED()) {
112 CString name
= callIdentifier
.functionName().utf8();
113 CString url
= callIdentifier
.url().utf8();
114 JAVASCRIPTCORE_PROFILE_WILL_EXECUTE(m_profileGroup
, const_cast<char*>(name
.data()), const_cast<char*>(url
.data()), callIdentifier
.lineNumber());
120 ASSERT(m_currentNode
);
121 m_currentNode
= m_currentNode
->willExecute(callerCallFrame
, callIdentifier
);
124 void ProfileGenerator::didExecute(ExecState
* callerCallFrame
, const CallIdentifier
& callIdentifier
)
126 if (JAVASCRIPTCORE_PROFILE_DID_EXECUTE_ENABLED()) {
127 CString name
= callIdentifier
.functionName().utf8();
128 CString url
= callIdentifier
.url().utf8();
129 JAVASCRIPTCORE_PROFILE_DID_EXECUTE(m_profileGroup
, const_cast<char*>(name
.data()), const_cast<char*>(url
.data()), callIdentifier
.lineNumber());
135 ASSERT(m_currentNode
);
136 if (m_currentNode
->callIdentifier() != callIdentifier
) {
137 RefPtr
<ProfileNode
> returningNode
= ProfileNode::create(callerCallFrame
, callIdentifier
, m_head
.get(), m_currentNode
.get());
138 returningNode
->lastCall().setStartTime(m_currentNode
->lastCall().startTime());
139 returningNode
->didExecute();
140 m_currentNode
->insertNode(returningNode
.release());
144 m_currentNode
= m_currentNode
->didExecute();
147 void ProfileGenerator::exceptionUnwind(ExecState
* handlerCallFrame
, const CallIdentifier
&)
149 // If the current node was called by the handler (==) or any
150 // more nested function (>) the we have exited early from it.
151 ASSERT(m_currentNode
);
152 while (m_currentNode
->callerCallFrame() >= handlerCallFrame
) {
153 didExecute(m_currentNode
->callerCallFrame(), m_currentNode
->callIdentifier());
154 ASSERT(m_currentNode
);
158 void ProfileGenerator::stopProfiling()
160 m_profile
->forEach(&ProfileNode::stopProfiling
);
162 if (m_foundConsoleStartParent
) {
163 removeProfileStart();
167 ASSERT(m_currentNode
);
169 // Set the current node to the parent, because we are in a call that
170 // will not get didExecute call.
171 m_currentNode
= m_currentNode
->parent();
173 if (double headSelfTime
= m_head
->selfTime()) {
174 m_head
->setSelfTime(0.0);
175 m_profile
->setIdleTime(headSelfTime
);
179 // The console.profile that started this ProfileGenerator will be the first child.
180 void ProfileGenerator::removeProfileStart()
182 ProfileNode
* currentNode
= 0;
183 for (ProfileNode
* next
= m_head
.get(); next
; next
= next
->firstChild())
186 if (currentNode
->callIdentifier().functionName() != "profile")
189 // Attribute the time of the node aobut to be removed to the self time of its parent
190 currentNode
->parent()->setSelfTime(currentNode
->parent()->selfTime() + currentNode
->totalTime());
191 currentNode
->parent()->removeChild(currentNode
);
194 // The console.profileEnd that stopped this ProfileGenerator will be the last child.
195 void ProfileGenerator::removeProfileEnd()
197 ProfileNode
* currentNode
= 0;
198 for (ProfileNode
* next
= m_head
.get(); next
; next
= next
->lastChild())
201 if (currentNode
->callIdentifier().functionName() != "profileEnd")
204 // Attribute the time of the node aobut to be removed to the self time of its parent
205 currentNode
->parent()->setSelfTime(currentNode
->parent()->selfTime() + currentNode
->totalTime());
207 ASSERT(currentNode
->callIdentifier() == (currentNode
->parent()->children()[currentNode
->parent()->children().size() - 1])->callIdentifier());
208 currentNode
->parent()->removeChild(currentNode
);