yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 1 | // Copyright 2012 the V8 project authors. All rights reserved. |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 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_LIVEEDIT_H_ |
| 29 | #define V8_LIVEEDIT_H_ |
| 30 | |
| 31 | |
| 32 | |
| 33 | // Live Edit feature implementation. |
| 34 | // User should be able to change script on already running VM. This feature |
| 35 | // matches hot swap features in other frameworks. |
| 36 | // |
| 37 | // The basic use-case is when user spots some mistake in function body |
| 38 | // from debugger and wishes to change the algorithm without restart. |
| 39 | // |
| 40 | // A single change always has a form of a simple replacement (in pseudo-code): |
| 41 | // script.source[positions, positions+length] = new_string; |
| 42 | // Implementation first determines, which function's body includes this |
| 43 | // change area. Then both old and new versions of script are fully compiled |
| 44 | // in order to analyze, whether the function changed its outer scope |
| 45 | // expectations (or number of parameters). If it didn't, function's code is |
| 46 | // patched with a newly compiled code. If it did change, enclosing function |
| 47 | // gets patched. All inner functions are left untouched, whatever happened |
| 48 | // to them in a new script version. However, new version of code will |
| 49 | // instantiate newly compiled functions. |
| 50 | |
| 51 | |
lrn@chromium.org | 1c09276 | 2011-05-09 09:42:16 +0000 | [diff] [blame] | 52 | #include "allocation.h" |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 53 | #include "compiler.h" |
| 54 | |
| 55 | namespace v8 { |
| 56 | namespace internal { |
| 57 | |
| 58 | // This class collects some specific information on structure of functions |
| 59 | // in a particular script. It gets called from compiler all the time, but |
| 60 | // actually records any data only when liveedit operation is in process; |
| 61 | // in any other time this class is very cheap. |
| 62 | // |
| 63 | // The primary interest of the Tracker is to record function scope structures |
| 64 | // in order to analyze whether function code maybe safely patched (with new |
| 65 | // code successfully reading existing data from function scopes). The Tracker |
| 66 | // also collects compiled function codes. |
| 67 | class LiveEditFunctionTracker { |
| 68 | public: |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 69 | explicit LiveEditFunctionTracker(Isolate* isolate, FunctionLiteral* fun); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 70 | ~LiveEditFunctionTracker(); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 71 | void RecordFunctionInfo(Handle<SharedFunctionInfo> info, |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 72 | FunctionLiteral* lit, Zone* zone); |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 73 | void RecordRootFunctionInfo(Handle<Code> code); |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 74 | |
sgjesse@chromium.org | ea88ce9 | 2011-03-23 11:19:56 +0000 | [diff] [blame] | 75 | static bool IsActive(Isolate* isolate); |
| 76 | |
| 77 | private: |
| 78 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 79 | Isolate* isolate_; |
| 80 | #endif |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 81 | }; |
| 82 | |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 83 | #ifdef ENABLE_DEBUGGER_SUPPORT |
| 84 | |
| 85 | class LiveEdit : AllStatic { |
| 86 | public: |
| 87 | static JSArray* GatherCompileInfo(Handle<Script> script, |
| 88 | Handle<String> source); |
| 89 | |
| 90 | static void WrapSharedFunctionInfos(Handle<JSArray> array); |
| 91 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 92 | MUST_USE_RESULT static MaybeObject* ReplaceFunctionCode( |
| 93 | Handle<JSArray> new_compile_info_array, |
| 94 | Handle<JSArray> shared_info_array); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 95 | |
kasperl@chromium.org | a555126 | 2010-12-07 12:49:48 +0000 | [diff] [blame] | 96 | static MaybeObject* FunctionSourceUpdated(Handle<JSArray> shared_info_array); |
| 97 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 98 | // Updates script field in FunctionSharedInfo. |
| 99 | static void SetFunctionScript(Handle<JSValue> function_wrapper, |
| 100 | Handle<Object> script_handle); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 101 | |
lrn@chromium.org | 303ada7 | 2010-10-27 09:33:13 +0000 | [diff] [blame] | 102 | MUST_USE_RESULT static MaybeObject* PatchFunctionPositions( |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 103 | Handle<JSArray> shared_info_array, Handle<JSArray> position_change_array); |
| 104 | |
kmillikin@chromium.org | 4111b80 | 2010-05-03 10:34:42 +0000 | [diff] [blame] | 105 | // For a script updates its source field. If old_script_name is provided |
| 106 | // (i.e. is a String), also creates a copy of the script with its original |
| 107 | // source and sends notification to debugger. |
| 108 | static Object* ChangeScriptSource(Handle<Script> original_script, |
| 109 | Handle<String> new_source, |
| 110 | Handle<Object> old_script_name); |
| 111 | |
| 112 | // In a code of a parent function replaces original function as embedded |
| 113 | // object with a substitution one. |
| 114 | static void ReplaceRefToNestedFunction(Handle<JSValue> parent_function_shared, |
| 115 | Handle<JSValue> orig_function_shared, |
| 116 | Handle<JSValue> subst_function_shared); |
| 117 | |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 118 | // Checks listed functions on stack and return array with corresponding |
| 119 | // FunctionPatchabilityStatus statuses; extra array element may |
| 120 | // contain general error message. Modifies the current stack and |
| 121 | // has restart the lowest found frames and drops all other frames above |
| 122 | // if possible and if do_drop is true. |
| 123 | static Handle<JSArray> CheckAndDropActivations( |
mmassi@chromium.org | 7028c05 | 2012-06-13 11:51:58 +0000 | [diff] [blame] | 124 | Handle<JSArray> shared_info_array, bool do_drop, Zone* zone); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 125 | |
yangguo@chromium.org | 5a11aaf | 2012-06-20 11:29:00 +0000 | [diff] [blame] | 126 | // Restarts the call frame and completely drops all frames above it. |
| 127 | // Return error message or NULL. |
| 128 | static const char* RestartFrame(JavaScriptFrame* frame, Zone* zone); |
| 129 | |
kmillikin@chromium.org | 5d8f0e6 | 2010-03-24 08:21:20 +0000 | [diff] [blame] | 130 | // A copy of this is in liveedit-debugger.js. |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 131 | enum FunctionPatchabilityStatus { |
ager@chromium.org | 357bf65 | 2010-04-12 11:30:10 +0000 | [diff] [blame] | 132 | FUNCTION_AVAILABLE_FOR_PATCH = 1, |
| 133 | FUNCTION_BLOCKED_ON_ACTIVE_STACK = 2, |
| 134 | FUNCTION_BLOCKED_ON_OTHER_STACK = 3, |
| 135 | FUNCTION_BLOCKED_UNDER_NATIVE_CODE = 4, |
| 136 | FUNCTION_REPLACED_ON_ACTIVE_STACK = 5 |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 137 | }; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 138 | |
sgjesse@chromium.org | c6c5718 | 2011-01-17 12:24:25 +0000 | [diff] [blame] | 139 | // Compares 2 strings line-by-line, then token-wise and returns diff in form |
| 140 | // of array of triplets (pos1, pos1_end, pos2_end) describing list |
| 141 | // of diff chunks. |
| 142 | static Handle<JSArray> CompareStrings(Handle<String> s1, |
| 143 | Handle<String> s2); |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
| 146 | |
| 147 | // A general-purpose comparator between 2 arrays. |
lrn@chromium.org | c34f580 | 2010-04-28 12:53:43 +0000 | [diff] [blame] | 148 | class Comparator { |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 149 | public: |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 150 | // Holds 2 arrays of some elements allowing to compare any pair of |
| 151 | // element from the first array and element from the second array. |
| 152 | class Input { |
| 153 | public: |
ricow@chromium.org | d2be901 | 2011-06-01 06:00:58 +0000 | [diff] [blame] | 154 | virtual int GetLength1() = 0; |
| 155 | virtual int GetLength2() = 0; |
| 156 | virtual bool Equals(int index1, int index2) = 0; |
ricow@chromium.org | c9c8082 | 2010-04-21 08:22:37 +0000 | [diff] [blame] | 157 | |
| 158 | protected: |
| 159 | virtual ~Input() {} |
| 160 | }; |
| 161 | |
| 162 | // Receives compare result as a series of chunks. |
| 163 | class Output { |
| 164 | public: |
| 165 | // Puts another chunk in result list. Note that technically speaking |
| 166 | // only 3 arguments actually needed with 4th being derivable. |
| 167 | virtual void AddChunk(int pos1, int pos2, int len1, int len2) = 0; |
| 168 | |
| 169 | protected: |
| 170 | virtual ~Output() {} |
| 171 | }; |
| 172 | |
| 173 | // Finds the difference between 2 arrays of elements. |
| 174 | static void CalculateDifference(Input* input, |
| 175 | Output* result_writer); |
ager@chromium.org | ce5e87b | 2010-03-10 10:24:18 +0000 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | #endif // ENABLE_DEBUGGER_SUPPORT |
| 179 | |
| 180 | |
ager@chromium.org | 5c83825 | 2010-02-19 08:53:10 +0000 | [diff] [blame] | 181 | } } // namespace v8::internal |
| 182 | |
| 183 | #endif /* V*_LIVEEDIT_H_ */ |