Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_FRAMES_H_ |
| 29 | #define V8_FRAMES_H_ |
| 30 | |
| 31 | namespace v8 { |
| 32 | namespace internal { |
| 33 | |
| 34 | typedef uint32_t RegList; |
| 35 | |
| 36 | // Get the number of registers in a given register list. |
| 37 | int NumRegs(RegList list); |
| 38 | |
| 39 | // Return the code of the n-th saved register available to JavaScript. |
| 40 | int JSCallerSavedCode(int n); |
| 41 | |
| 42 | |
| 43 | // Forward declarations. |
| 44 | class StackFrameIterator; |
| 45 | class Top; |
| 46 | class ThreadLocalTop; |
| 47 | |
| 48 | |
| 49 | class StackHandler BASE_EMBEDDED { |
| 50 | public: |
| 51 | enum State { |
| 52 | ENTRY, |
| 53 | TRY_CATCH, |
| 54 | TRY_FINALLY |
| 55 | }; |
| 56 | |
| 57 | // Get the address of this stack handler. |
| 58 | inline Address address() const; |
| 59 | |
| 60 | // Get the next stack handler in the chain. |
| 61 | inline StackHandler* next() const; |
| 62 | |
| 63 | // Tells whether the given address is inside this handler. |
| 64 | inline bool includes(Address address) const; |
| 65 | |
| 66 | // Garbage collection support. |
| 67 | inline void Iterate(ObjectVisitor* v) const; |
| 68 | |
| 69 | // Conversion support. |
| 70 | static inline StackHandler* FromAddress(Address address); |
| 71 | |
| 72 | // Testers |
| 73 | bool is_entry() { return state() == ENTRY; } |
| 74 | bool is_try_catch() { return state() == TRY_CATCH; } |
| 75 | bool is_try_finally() { return state() == TRY_FINALLY; } |
| 76 | |
| 77 | // Garbage collection support. |
| 78 | void Cook(Code* code); |
| 79 | void Uncook(Code* code); |
| 80 | |
| 81 | private: |
| 82 | // Accessors. |
| 83 | inline State state() const; |
| 84 | |
| 85 | inline Address pc() const; |
| 86 | inline void set_pc(Address value); |
| 87 | |
| 88 | DISALLOW_IMPLICIT_CONSTRUCTORS(StackHandler); |
| 89 | }; |
| 90 | |
| 91 | |
| 92 | #define STACK_FRAME_TYPE_LIST(V) \ |
| 93 | V(ENTRY, EntryFrame) \ |
| 94 | V(ENTRY_CONSTRUCT, EntryConstructFrame) \ |
| 95 | V(EXIT, ExitFrame) \ |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 96 | V(JAVA_SCRIPT, JavaScriptFrame) \ |
| 97 | V(INTERNAL, InternalFrame) \ |
| 98 | V(CONSTRUCT, ConstructFrame) \ |
| 99 | V(ARGUMENTS_ADAPTOR, ArgumentsAdaptorFrame) |
| 100 | |
| 101 | |
| 102 | // Abstract base class for all stack frames. |
| 103 | class StackFrame BASE_EMBEDDED { |
| 104 | public: |
| 105 | #define DECLARE_TYPE(type, ignore) type, |
| 106 | enum Type { |
| 107 | NONE = 0, |
| 108 | STACK_FRAME_TYPE_LIST(DECLARE_TYPE) |
| 109 | NUMBER_OF_TYPES |
| 110 | }; |
| 111 | #undef DECLARE_TYPE |
| 112 | |
| 113 | // Opaque data type for identifying stack frames. Used extensively |
| 114 | // by the debugger. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 115 | // ID_MIN_VALUE and ID_MAX_VALUE are specified to ensure that enumeration type |
| 116 | // has correct value range (see Issue 830 for more details). |
| 117 | enum Id { |
| 118 | ID_MIN_VALUE = kMinInt, |
| 119 | ID_MAX_VALUE = kMaxInt, |
| 120 | NO_ID = 0 |
| 121 | }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 122 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 123 | // Copy constructor; it breaks the connection to host iterator. |
| 124 | StackFrame(const StackFrame& original) { |
| 125 | this->state_ = original.state_; |
| 126 | this->iterator_ = NULL; |
| 127 | } |
| 128 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 129 | // Type testers. |
| 130 | bool is_entry() const { return type() == ENTRY; } |
| 131 | bool is_entry_construct() const { return type() == ENTRY_CONSTRUCT; } |
| 132 | bool is_exit() const { return type() == EXIT; } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 133 | bool is_java_script() const { return type() == JAVA_SCRIPT; } |
| 134 | bool is_arguments_adaptor() const { return type() == ARGUMENTS_ADAPTOR; } |
| 135 | bool is_internal() const { return type() == INTERNAL; } |
| 136 | bool is_construct() const { return type() == CONSTRUCT; } |
| 137 | virtual bool is_standard() const { return false; } |
| 138 | |
| 139 | // Accessors. |
| 140 | Address sp() const { return state_.sp; } |
| 141 | Address fp() const { return state_.fp; } |
| 142 | Address caller_sp() const { return GetCallerStackPointer(); } |
| 143 | |
| 144 | Address pc() const { return *pc_address(); } |
| 145 | void set_pc(Address pc) { *pc_address() = pc; } |
| 146 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 147 | virtual void SetCallerFp(Address caller_fp) = 0; |
| 148 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 149 | Address* pc_address() const { return state_.pc_address; } |
| 150 | |
| 151 | // Get the id of this stack frame. |
| 152 | Id id() const { return static_cast<Id>(OffsetFrom(caller_sp())); } |
| 153 | |
| 154 | // Checks if this frame includes any stack handlers. |
| 155 | bool HasHandler() const; |
| 156 | |
| 157 | // Get the type of this frame. |
| 158 | virtual Type type() const = 0; |
| 159 | |
| 160 | // Get the code associated with this frame. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 161 | // This method could be called during marking phase of GC. |
| 162 | virtual Code* unchecked_code() const = 0; |
| 163 | |
| 164 | // Get the code associated with this frame. |
| 165 | inline Code* code() const { |
| 166 | return Code::cast(unchecked_code()); |
| 167 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 168 | |
| 169 | // Garbage collection support. |
| 170 | static void CookFramesForThread(ThreadLocalTop* thread); |
| 171 | static void UncookFramesForThread(ThreadLocalTop* thread); |
| 172 | |
| 173 | virtual void Iterate(ObjectVisitor* v) const { } |
| 174 | |
| 175 | // Printing support. |
| 176 | enum PrintMode { OVERVIEW, DETAILS }; |
| 177 | virtual void Print(StringStream* accumulator, |
| 178 | PrintMode mode, |
| 179 | int index) const { } |
| 180 | |
| 181 | protected: |
| 182 | struct State { |
| 183 | Address sp; |
| 184 | Address fp; |
| 185 | Address* pc_address; |
| 186 | }; |
| 187 | |
| 188 | explicit StackFrame(StackFrameIterator* iterator) : iterator_(iterator) { } |
| 189 | virtual ~StackFrame() { } |
| 190 | |
| 191 | // Compute the stack pointer for the calling frame. |
| 192 | virtual Address GetCallerStackPointer() const = 0; |
| 193 | |
| 194 | // Printing support. |
| 195 | static void PrintIndex(StringStream* accumulator, |
| 196 | PrintMode mode, |
| 197 | int index); |
| 198 | |
| 199 | // Get the top handler from the current stack iterator. |
| 200 | inline StackHandler* top_handler() const; |
| 201 | |
| 202 | // Compute the stack frame type for the given state. |
| 203 | static Type ComputeType(State* state); |
| 204 | |
| 205 | private: |
| 206 | const StackFrameIterator* iterator_; |
| 207 | State state_; |
| 208 | |
| 209 | // Fill in the state of the calling frame. |
| 210 | virtual void ComputeCallerState(State* state) const = 0; |
| 211 | |
| 212 | // Get the type and the state of the calling frame. |
| 213 | virtual Type GetCallerState(State* state) const; |
| 214 | |
| 215 | // Cooking/uncooking support. |
| 216 | void Cook(); |
| 217 | void Uncook(); |
| 218 | |
| 219 | friend class StackFrameIterator; |
| 220 | friend class StackHandlerIterator; |
| 221 | friend class SafeStackFrameIterator; |
| 222 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 223 | private: |
| 224 | void operator=(const StackFrame& original); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | |
| 228 | // Entry frames are used to enter JavaScript execution from C. |
| 229 | class EntryFrame: public StackFrame { |
| 230 | public: |
| 231 | virtual Type type() const { return ENTRY; } |
| 232 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 233 | virtual Code* unchecked_code() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 234 | |
| 235 | // Garbage collection support. |
| 236 | virtual void Iterate(ObjectVisitor* v) const; |
| 237 | |
| 238 | static EntryFrame* cast(StackFrame* frame) { |
| 239 | ASSERT(frame->is_entry()); |
| 240 | return static_cast<EntryFrame*>(frame); |
| 241 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 242 | virtual void SetCallerFp(Address caller_fp); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 243 | |
| 244 | protected: |
| 245 | explicit EntryFrame(StackFrameIterator* iterator) : StackFrame(iterator) { } |
| 246 | |
| 247 | // The caller stack pointer for entry frames is always zero. The |
| 248 | // real information about the caller frame is available through the |
| 249 | // link to the top exit frame. |
| 250 | virtual Address GetCallerStackPointer() const { return 0; } |
| 251 | |
| 252 | private: |
| 253 | virtual void ComputeCallerState(State* state) const; |
| 254 | virtual Type GetCallerState(State* state) const; |
| 255 | |
| 256 | friend class StackFrameIterator; |
| 257 | }; |
| 258 | |
| 259 | |
| 260 | class EntryConstructFrame: public EntryFrame { |
| 261 | public: |
| 262 | virtual Type type() const { return ENTRY_CONSTRUCT; } |
| 263 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 264 | virtual Code* unchecked_code() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 265 | |
| 266 | static EntryConstructFrame* cast(StackFrame* frame) { |
| 267 | ASSERT(frame->is_entry_construct()); |
| 268 | return static_cast<EntryConstructFrame*>(frame); |
| 269 | } |
| 270 | |
| 271 | protected: |
| 272 | explicit EntryConstructFrame(StackFrameIterator* iterator) |
| 273 | : EntryFrame(iterator) { } |
| 274 | |
| 275 | private: |
| 276 | friend class StackFrameIterator; |
| 277 | }; |
| 278 | |
| 279 | |
| 280 | // Exit frames are used to exit JavaScript execution and go to C. |
| 281 | class ExitFrame: public StackFrame { |
| 282 | public: |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 283 | enum Mode { MODE_NORMAL, MODE_DEBUG }; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 284 | virtual Type type() const { return EXIT; } |
| 285 | |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 286 | virtual Code* unchecked_code() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 287 | |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 288 | Object*& code_slot() const; |
| 289 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 290 | // Garbage collection support. |
| 291 | virtual void Iterate(ObjectVisitor* v) const; |
| 292 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 293 | virtual void SetCallerFp(Address caller_fp); |
| 294 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 295 | static ExitFrame* cast(StackFrame* frame) { |
| 296 | ASSERT(frame->is_exit()); |
| 297 | return static_cast<ExitFrame*>(frame); |
| 298 | } |
| 299 | |
| 300 | // Compute the state and type of an exit frame given a frame |
| 301 | // pointer. Used when constructing the first stack frame seen by an |
| 302 | // iterator and the frames following entry frames. |
| 303 | static Type GetStateForFramePointer(Address fp, State* state); |
| 304 | |
| 305 | protected: |
| 306 | explicit ExitFrame(StackFrameIterator* iterator) : StackFrame(iterator) { } |
| 307 | |
| 308 | virtual Address GetCallerStackPointer() const; |
| 309 | |
| 310 | private: |
| 311 | virtual void ComputeCallerState(State* state) const; |
| 312 | |
| 313 | friend class StackFrameIterator; |
| 314 | }; |
| 315 | |
| 316 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 317 | class StandardFrame: public StackFrame { |
| 318 | public: |
| 319 | // Testers. |
| 320 | virtual bool is_standard() const { return true; } |
| 321 | |
| 322 | // Accessors. |
| 323 | inline Object* context() const; |
| 324 | |
| 325 | // Access the expressions in the stack frame including locals. |
| 326 | inline Object* GetExpression(int index) const; |
| 327 | inline void SetExpression(int index, Object* value); |
| 328 | int ComputeExpressionsCount() const; |
| 329 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 330 | virtual void SetCallerFp(Address caller_fp); |
| 331 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 332 | static StandardFrame* cast(StackFrame* frame) { |
| 333 | ASSERT(frame->is_standard()); |
| 334 | return static_cast<StandardFrame*>(frame); |
| 335 | } |
| 336 | |
| 337 | protected: |
| 338 | explicit StandardFrame(StackFrameIterator* iterator) |
| 339 | : StackFrame(iterator) { } |
| 340 | |
| 341 | virtual void ComputeCallerState(State* state) const; |
| 342 | |
| 343 | // Accessors. |
| 344 | inline Address caller_fp() const; |
| 345 | inline Address caller_pc() const; |
| 346 | |
| 347 | // Computes the address of the PC field in the standard frame given |
| 348 | // by the provided frame pointer. |
| 349 | static inline Address ComputePCAddress(Address fp); |
| 350 | |
| 351 | // Iterate over expression stack including stack handlers, locals, |
| 352 | // and parts of the fixed part including context and code fields. |
| 353 | void IterateExpressions(ObjectVisitor* v) const; |
| 354 | |
| 355 | // Returns the address of the n'th expression stack element. |
| 356 | Address GetExpressionAddress(int n) const; |
| 357 | |
| 358 | // Determines if the n'th expression stack element is in a stack |
| 359 | // handler or not. Requires traversing all handlers in this frame. |
| 360 | bool IsExpressionInsideHandler(int n) const; |
| 361 | |
| 362 | // Determines if the standard frame for the given frame pointer is |
| 363 | // an arguments adaptor frame. |
| 364 | static inline bool IsArgumentsAdaptorFrame(Address fp); |
| 365 | |
| 366 | // Determines if the standard frame for the given frame pointer is a |
| 367 | // construct frame. |
| 368 | static inline bool IsConstructFrame(Address fp); |
| 369 | |
| 370 | private: |
| 371 | friend class StackFrame; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 372 | friend class StackFrameIterator; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 373 | }; |
| 374 | |
| 375 | |
| 376 | class JavaScriptFrame: public StandardFrame { |
| 377 | public: |
| 378 | virtual Type type() const { return JAVA_SCRIPT; } |
| 379 | |
| 380 | // Accessors. |
| 381 | inline Object* function() const; |
| 382 | inline Object* receiver() const; |
| 383 | inline void set_receiver(Object* value); |
| 384 | |
| 385 | // Access the parameters. |
| 386 | Object* GetParameter(int index) const; |
| 387 | int ComputeParametersCount() const; |
| 388 | |
| 389 | // Temporary way of getting access to the number of parameters |
| 390 | // passed on the stack by the caller. Once argument adaptor frames |
| 391 | // has been introduced on ARM, this number will always match the |
| 392 | // computed parameters count. |
| 393 | int GetProvidedParametersCount() const; |
| 394 | |
| 395 | // Check if this frame is a constructor frame invoked through 'new'. |
| 396 | bool IsConstructor() const; |
| 397 | |
| 398 | // Check if this frame has "adapted" arguments in the sense that the |
| 399 | // actual passed arguments are available in an arguments adaptor |
| 400 | // frame below it on the stack. |
| 401 | inline bool has_adapted_arguments() const; |
| 402 | |
| 403 | // Garbage collection support. |
| 404 | virtual void Iterate(ObjectVisitor* v) const; |
| 405 | |
| 406 | // Printing support. |
| 407 | virtual void Print(StringStream* accumulator, |
| 408 | PrintMode mode, |
| 409 | int index) const; |
| 410 | |
| 411 | // Determine the code for the frame. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 412 | virtual Code* unchecked_code() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 413 | |
| 414 | static JavaScriptFrame* cast(StackFrame* frame) { |
| 415 | ASSERT(frame->is_java_script()); |
| 416 | return static_cast<JavaScriptFrame*>(frame); |
| 417 | } |
| 418 | |
| 419 | protected: |
| 420 | explicit JavaScriptFrame(StackFrameIterator* iterator) |
| 421 | : StandardFrame(iterator), disable_heap_access_(false) { } |
| 422 | |
| 423 | virtual Address GetCallerStackPointer() const; |
| 424 | |
| 425 | // When this mode is enabled it is not allowed to access heap objects. |
| 426 | // This is a special mode used when gathering stack samples in profiler. |
| 427 | // A shortcoming is that caller's SP value will be calculated incorrectly |
| 428 | // (see GetCallerStackPointer implementation), but it is not used for stack |
| 429 | // sampling. |
| 430 | void DisableHeapAccess() { disable_heap_access_ = true; } |
| 431 | |
| 432 | private: |
| 433 | bool disable_heap_access_; |
| 434 | inline Object* function_slot_object() const; |
| 435 | |
| 436 | friend class StackFrameIterator; |
| 437 | }; |
| 438 | |
| 439 | |
| 440 | // Arguments adaptor frames are automatically inserted below |
| 441 | // JavaScript frames when the actual number of parameters does not |
| 442 | // match the formal number of parameters. |
| 443 | class ArgumentsAdaptorFrame: public JavaScriptFrame { |
| 444 | public: |
| 445 | virtual Type type() const { return ARGUMENTS_ADAPTOR; } |
| 446 | |
| 447 | // Determine the code for the frame. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 448 | virtual Code* unchecked_code() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 449 | |
| 450 | static ArgumentsAdaptorFrame* cast(StackFrame* frame) { |
| 451 | ASSERT(frame->is_arguments_adaptor()); |
| 452 | return static_cast<ArgumentsAdaptorFrame*>(frame); |
| 453 | } |
| 454 | |
| 455 | // Printing support. |
| 456 | virtual void Print(StringStream* accumulator, |
| 457 | PrintMode mode, |
| 458 | int index) const; |
| 459 | protected: |
| 460 | explicit ArgumentsAdaptorFrame(StackFrameIterator* iterator) |
| 461 | : JavaScriptFrame(iterator) { } |
| 462 | |
| 463 | virtual Address GetCallerStackPointer() const; |
| 464 | |
| 465 | private: |
| 466 | friend class StackFrameIterator; |
| 467 | }; |
| 468 | |
| 469 | |
| 470 | class InternalFrame: public StandardFrame { |
| 471 | public: |
| 472 | virtual Type type() const { return INTERNAL; } |
| 473 | |
| 474 | // Garbage collection support. |
| 475 | virtual void Iterate(ObjectVisitor* v) const; |
| 476 | |
| 477 | // Determine the code for the frame. |
Iain Merrick | 7568138 | 2010-08-19 15:07:18 +0100 | [diff] [blame^] | 478 | virtual Code* unchecked_code() const; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 479 | |
| 480 | static InternalFrame* cast(StackFrame* frame) { |
| 481 | ASSERT(frame->is_internal()); |
| 482 | return static_cast<InternalFrame*>(frame); |
| 483 | } |
| 484 | |
| 485 | protected: |
| 486 | explicit InternalFrame(StackFrameIterator* iterator) |
| 487 | : StandardFrame(iterator) { } |
| 488 | |
| 489 | virtual Address GetCallerStackPointer() const; |
| 490 | |
| 491 | private: |
| 492 | friend class StackFrameIterator; |
| 493 | }; |
| 494 | |
| 495 | |
| 496 | // Construct frames are special trampoline frames introduced to handle |
| 497 | // function invocations through 'new'. |
| 498 | class ConstructFrame: public InternalFrame { |
| 499 | public: |
| 500 | virtual Type type() const { return CONSTRUCT; } |
| 501 | |
| 502 | static ConstructFrame* cast(StackFrame* frame) { |
| 503 | ASSERT(frame->is_construct()); |
| 504 | return static_cast<ConstructFrame*>(frame); |
| 505 | } |
| 506 | |
| 507 | protected: |
| 508 | explicit ConstructFrame(StackFrameIterator* iterator) |
| 509 | : InternalFrame(iterator) { } |
| 510 | |
| 511 | private: |
| 512 | friend class StackFrameIterator; |
| 513 | }; |
| 514 | |
| 515 | |
| 516 | class StackFrameIterator BASE_EMBEDDED { |
| 517 | public: |
| 518 | // An iterator that iterates over the current thread's stack. |
| 519 | StackFrameIterator(); |
| 520 | |
| 521 | // An iterator that iterates over a given thread's stack. |
| 522 | explicit StackFrameIterator(ThreadLocalTop* thread); |
| 523 | |
| 524 | // An iterator that can start from a given FP address. |
| 525 | // If use_top, then work as usual, if fp isn't NULL, use it, |
| 526 | // otherwise, do nothing. |
| 527 | StackFrameIterator(bool use_top, Address fp, Address sp); |
| 528 | |
| 529 | StackFrame* frame() const { |
| 530 | ASSERT(!done()); |
| 531 | return frame_; |
| 532 | } |
| 533 | |
| 534 | bool done() const { return frame_ == NULL; } |
| 535 | void Advance() { (this->*advance_)(); } |
| 536 | |
| 537 | // Go back to the first frame. |
| 538 | void Reset(); |
| 539 | |
| 540 | private: |
| 541 | #define DECLARE_SINGLETON(ignore, type) type type##_; |
| 542 | STACK_FRAME_TYPE_LIST(DECLARE_SINGLETON) |
| 543 | #undef DECLARE_SINGLETON |
| 544 | StackFrame* frame_; |
| 545 | StackHandler* handler_; |
| 546 | ThreadLocalTop* thread_; |
| 547 | Address fp_; |
| 548 | Address sp_; |
| 549 | void (StackFrameIterator::*advance_)(); |
| 550 | |
| 551 | StackHandler* handler() const { |
| 552 | ASSERT(!done()); |
| 553 | return handler_; |
| 554 | } |
| 555 | |
| 556 | // Get the type-specific frame singleton in a given state. |
| 557 | StackFrame* SingletonFor(StackFrame::Type type, StackFrame::State* state); |
| 558 | // A helper function, can return a NULL pointer. |
| 559 | StackFrame* SingletonFor(StackFrame::Type type); |
| 560 | |
| 561 | void AdvanceWithHandler(); |
| 562 | void AdvanceWithoutHandler(); |
| 563 | |
| 564 | friend class StackFrame; |
| 565 | friend class SafeStackFrameIterator; |
| 566 | DISALLOW_COPY_AND_ASSIGN(StackFrameIterator); |
| 567 | }; |
| 568 | |
| 569 | |
| 570 | // Iterator that supports iterating through all JavaScript frames. |
| 571 | template<typename Iterator> |
| 572 | class JavaScriptFrameIteratorTemp BASE_EMBEDDED { |
| 573 | public: |
| 574 | JavaScriptFrameIteratorTemp() { if (!done()) Advance(); } |
| 575 | |
| 576 | explicit JavaScriptFrameIteratorTemp(ThreadLocalTop* thread) : |
| 577 | iterator_(thread) { |
| 578 | if (!done()) Advance(); |
| 579 | } |
| 580 | |
| 581 | // Skip frames until the frame with the given id is reached. |
| 582 | explicit JavaScriptFrameIteratorTemp(StackFrame::Id id); |
| 583 | |
| 584 | JavaScriptFrameIteratorTemp(Address fp, Address sp, |
| 585 | Address low_bound, Address high_bound) : |
| 586 | iterator_(fp, sp, low_bound, high_bound) { |
| 587 | if (!done()) Advance(); |
| 588 | } |
| 589 | |
| 590 | inline JavaScriptFrame* frame() const; |
| 591 | |
| 592 | bool done() const { return iterator_.done(); } |
| 593 | void Advance(); |
| 594 | |
| 595 | // Advance to the frame holding the arguments for the current |
| 596 | // frame. This only affects the current frame if it has adapted |
| 597 | // arguments. |
| 598 | void AdvanceToArgumentsFrame(); |
| 599 | |
| 600 | // Go back to the first frame. |
| 601 | void Reset(); |
| 602 | |
| 603 | private: |
| 604 | Iterator iterator_; |
| 605 | }; |
| 606 | |
| 607 | |
| 608 | typedef JavaScriptFrameIteratorTemp<StackFrameIterator> JavaScriptFrameIterator; |
| 609 | |
| 610 | |
| 611 | // NOTE: The stack trace frame iterator is an iterator that only |
| 612 | // traverse proper JavaScript frames; that is JavaScript frames that |
| 613 | // have proper JavaScript functions. This excludes the problematic |
| 614 | // functions in runtime.js. |
| 615 | class StackTraceFrameIterator: public JavaScriptFrameIterator { |
| 616 | public: |
| 617 | StackTraceFrameIterator(); |
| 618 | void Advance(); |
Leon Clarke | 4515c47 | 2010-02-03 11:58:03 +0000 | [diff] [blame] | 619 | |
| 620 | private: |
| 621 | bool IsValidFrame(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 622 | }; |
| 623 | |
| 624 | |
| 625 | class SafeStackFrameIterator BASE_EMBEDDED { |
| 626 | public: |
| 627 | SafeStackFrameIterator(Address fp, Address sp, |
| 628 | Address low_bound, Address high_bound); |
| 629 | |
| 630 | StackFrame* frame() const { |
| 631 | ASSERT(is_working_iterator_); |
| 632 | return iterator_.frame(); |
| 633 | } |
| 634 | |
| 635 | bool done() const { return iteration_done_ ? true : iterator_.done(); } |
| 636 | |
| 637 | void Advance(); |
| 638 | void Reset(); |
| 639 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 640 | static bool IsWithinBounds( |
| 641 | Address low_bound, Address high_bound, Address addr) { |
| 642 | return low_bound <= addr && addr <= high_bound; |
| 643 | } |
Leon Clarke | d91b9f7 | 2010-01-27 17:25:45 +0000 | [diff] [blame] | 644 | |
| 645 | private: |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 646 | bool IsValidStackAddress(Address addr) const { |
| 647 | return IsWithinBounds(low_bound_, high_bound_, addr); |
| 648 | } |
| 649 | bool CanIterateHandles(StackFrame* frame, StackHandler* handler); |
| 650 | bool IsValidFrame(StackFrame* frame) const; |
| 651 | bool IsValidCaller(StackFrame* frame); |
| 652 | |
| 653 | Address low_bound_; |
| 654 | Address high_bound_; |
| 655 | const bool is_valid_top_; |
| 656 | const bool is_valid_fp_; |
| 657 | const bool is_working_iterator_; |
| 658 | bool iteration_done_; |
| 659 | StackFrameIterator iterator_; |
| 660 | }; |
| 661 | |
| 662 | |
| 663 | #ifdef ENABLE_LOGGING_AND_PROFILING |
| 664 | typedef JavaScriptFrameIteratorTemp<SafeStackFrameIterator> |
| 665 | SafeJavaScriptFrameIterator; |
| 666 | |
| 667 | |
| 668 | class SafeStackTraceFrameIterator: public SafeJavaScriptFrameIterator { |
| 669 | public: |
| 670 | explicit SafeStackTraceFrameIterator(Address fp, Address sp, |
| 671 | Address low_bound, Address high_bound); |
| 672 | void Advance(); |
| 673 | }; |
| 674 | #endif |
| 675 | |
| 676 | |
| 677 | class StackFrameLocator BASE_EMBEDDED { |
| 678 | public: |
| 679 | // Find the nth JavaScript frame on the stack. The caller must |
| 680 | // guarantee that such a frame exists. |
| 681 | JavaScriptFrame* FindJavaScriptFrame(int n); |
| 682 | |
| 683 | private: |
| 684 | StackFrameIterator iterator_; |
| 685 | }; |
| 686 | |
| 687 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 688 | // Reads all frames on the current stack and copies them into the current |
| 689 | // zone memory. |
| 690 | Vector<StackFrame*> CreateStackMap(); |
| 691 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 692 | } } // namespace v8::internal |
| 693 | |
| 694 | #endif // V8_FRAMES_H_ |