debug-x64.cc 5.05 KB
Newer Older
1
// Copyright 2012 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
#if V8_TARGET_ARCH_X64
6

7
#include "src/debug/debug.h"
8

9 10 11
#include "src/assembler.h"
#include "src/codegen.h"
#include "src/debug/liveedit.h"
12

13 14
namespace v8 {
namespace internal {
15

16
#define __ ACCESS_MASM(masm)
17 18


19
void EmitDebugBreakSlot(MacroAssembler* masm) {
20
  Label check_codesize;
21 22 23 24 25
  __ bind(&check_codesize);
  __ Nop(Assembler::kDebugBreakSlotLength);
  DCHECK_EQ(Assembler::kDebugBreakSlotLength,
            masm->SizeOfCodeGeneratedSince(&check_codesize));
}
26 27


28
void DebugCodegen::GenerateSlot(MacroAssembler* masm, RelocInfo::Mode mode) {
29
  // Generate enough nop's to make space for a call instruction.
30
  masm->RecordDebugBreakSlot(mode);
31
  EmitDebugBreakSlot(masm);
32 33 34
}


35 36
void DebugCodegen::ClearDebugBreakSlot(Isolate* isolate, Address pc) {
  CodePatcher patcher(isolate, pc, Assembler::kDebugBreakSlotLength);
37
  EmitDebugBreakSlot(patcher.masm());
38 39 40
}


41 42
void DebugCodegen::PatchDebugBreakSlot(Isolate* isolate, Address pc,
                                       Handle<Code> code) {
43
  DCHECK(code->is_debug_stub());
44
  static const int kSize = Assembler::kDebugBreakSlotLength;
45
  CodePatcher patcher(isolate, pc, kSize);
46 47 48 49 50 51 52
  Label check_codesize;
  patcher.masm()->bind(&check_codesize);
  patcher.masm()->movp(kScratchRegister, reinterpret_cast<void*>(code->entry()),
                       Assembler::RelocInfoNone());
  patcher.masm()->call(kScratchRegister);
  // Check that the size of the code generated is as expected.
  DCHECK_EQ(kSize, patcher.masm()->SizeOfCodeGeneratedSince(&check_codesize));
53 54
}

55 56 57
bool DebugCodegen::DebugBreakSlotIsPatched(Address pc) {
  return !Assembler::IsNop(pc);
}
58

59 60 61
void DebugCodegen::GenerateDebugBreakStub(MacroAssembler* masm,
                                          DebugBreakCallHelperMode mode) {
  __ RecordComment("Debug break");
62

63
  // Enter an internal frame.
64 65 66
  {
    FrameScope scope(masm, StackFrame::INTERNAL);

67
    // Load padding words on stack.
68 69
    for (int i = 0; i < LiveEdit::kFramePaddingInitialSize; i++) {
      __ Push(Smi::FromInt(LiveEdit::kFramePaddingValue));
70
    }
71
    __ Push(Smi::FromInt(LiveEdit::kFramePaddingInitialSize));
72

73 74 75 76 77 78 79 80 81
    // Push arguments for DebugBreak call.
    if (mode == SAVE_RESULT_REGISTER) {
      // Break on return.
      __ Push(rax);
    } else {
      // Non-return breaks.
      __ Push(masm->isolate()->factory()->the_hole_value());
    }
    __ Set(rax, 1);
82 83
    __ Move(rbx, ExternalReference(Runtime::FunctionForId(Runtime::kDebugBreak),
                                   masm->isolate()));
84

85
    CEntryStub ceb(masm->isolate(), 1);
86 87
    __ CallStub(&ceb);

88 89 90
    if (FLAG_debug_code) {
      for (int i = 0; i < kNumJSCallerSaved; ++i) {
        Register reg = {JSCallerSavedCode(i)};
91 92 93 94 95
        // Do not clobber rax if mode is SAVE_RESULT_REGISTER. It will
        // contain return value of the function.
        if (!(reg.is(rax) && (mode == SAVE_RESULT_REGISTER))) {
          __ Set(reg, kDebugZapValue);
        }
96
      }
97
    }
98

99
    // Read current padding counter and skip corresponding number of words.
100
    __ Pop(kScratchRegister);
101
    __ SmiToInteger32(kScratchRegister, kScratchRegister);
102
    __ leap(rsp, Operand(rsp, kScratchRegister, times_pointer_size, 0));
103

104 105
    // Get rid of the internal frame.
  }
106

107 108 109
  // This call did not replace a call , so there will be an unwanted
  // return address left on the stack. Here we get rid of that.
  __ addp(rsp, Immediate(kPCOnStackSize));
110 111 112 113 114

  // Now that the break point has been handled, resume normal execution by
  // jumping to the target address intended by the caller and that was
  // overwritten by the address of DebugBreakXXX.
  ExternalReference after_break_target =
115
      ExternalReference::debug_after_break_target_address(masm->isolate());
116
  __ Move(kScratchRegister, after_break_target);
117
  __ Jump(Operand(kScratchRegister, 0));
118 119 120
}


121
void DebugCodegen::GenerateFrameDropperLiveEdit(MacroAssembler* masm) {
122
  // We do not know our frame height, but set rsp based on rbp.
123
  __ leap(rsp, Operand(rbp, FrameDropperFrameConstants::kFunctionOffset));
124
  __ Pop(rdi);  // Function.
125 126 127 128
  __ addp(rsp,
          Immediate(-FrameDropperFrameConstants::kCodeOffset));  // INTERNAL
                                                                 // frame marker
                                                                 // and code
129
  __ popq(rbp);
130

131
  ParameterCount dummy(0);
132
  __ CheckDebugHook(rdi, no_reg, dummy, dummy);
133

134
  // Load context from the function.
135
  __ movp(rsi, FieldOperand(rdi, JSFunction::kContextOffset));
136

137 138 139
  // Clear new.target as a safety measure.
  __ LoadRoot(rdx, Heap::kUndefinedValueRootIndex);

140
  // Get function code.
141 142 143
  __ movp(rbx, FieldOperand(rdi, JSFunction::kSharedFunctionInfoOffset));
  __ movp(rbx, FieldOperand(rbx, SharedFunctionInfo::kCodeOffset));
  __ leap(rbx, FieldOperand(rbx, Code::kHeaderSize));
144 145

  // Re-run JSFunction, rdi is function, rsi is context.
146
  __ jmp(rbx);
147 148
}

149
const bool LiveEdit::kFrameDropperSupported = true;
150

151 152
#undef __

153 154
}  // namespace internal
}  // namespace v8
155 156

#endif  // V8_TARGET_ARCH_X64