@@ -1184,8 +1184,8 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
11841184 final PythonLanguage language = PythonLanguage .get (this );
11851185 final Assumption noTraceOrProfile = language .noTracingOrProfilingAssumption ;
11861186 final InstrumentationSupport instrumentation = instrumentationRoot .getInstrumentation ();
1187- if (instrumentation != null ) {
1188- Object result = enterRoot (virtualFrame );
1187+ if (instrumentation != null && ! fromOSR ) {
1188+ Object result = notifyEnter (virtualFrame , instrumentation , initialBci );
11891189 if (result != null ) {
11901190 return result ;
11911191 }
@@ -1237,10 +1237,6 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
12371237 CompilerAsserts .partialEvaluationConstant (stackTop );
12381238
12391239 try {
1240- if (instrumentation != null ) {
1241- notifyStatement (virtualFrame , instrumentation , mutableData , bci );
1242- }
1243-
12441240 switch (bc ) {
12451241 case OpCodesConstants .LOAD_NONE :
12461242 virtualFrame .setObject (++stackTop , PNone .NONE );
@@ -1689,8 +1685,8 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
16891685 Object value = virtualFrame .getObject (stackTop );
16901686 traceOrProfileReturn (virtualFrame , mutableData , value , tracingEnabled , profilingEnabled );
16911687
1692- if (instrumentation != null && instrumentationRoot instanceof WrapperNode ) {
1693- notifyRootReturn (virtualFrame , mutableData , value );
1688+ if (instrumentation != null ) {
1689+ notifyReturn (virtualFrame , mutableData , instrumentation , beginBci , value );
16941690 }
16951691 if (isGeneratorOrCoroutine ) {
16961692 throw new GeneratorReturnException (value );
@@ -1790,6 +1786,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
17901786 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
17911787 bci += oparg ;
17921788 oparg = 0 ;
1789+ if (instrumentation != null ) {
1790+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1791+ }
17931792 continue ;
17941793 case OpCodesConstants .POP_AND_JUMP_IF_FALSE : {
17951794 bytecodePopAndJumpIfFalse (virtualFrame , bci , stackTop );
@@ -1805,6 +1804,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18051804 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18061805 bci += oparg ;
18071806 oparg = 0 ;
1807+ if (instrumentation != null ) {
1808+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1809+ }
18081810 continue ;
18091811 } else {
18101812 bci += 3 ;
@@ -1817,6 +1819,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18171819 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18181820 bci += oparg ;
18191821 oparg = 0 ;
1822+ if (instrumentation != null ) {
1823+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1824+ }
18201825 continue ;
18211826 } else {
18221827 bci += 3 ;
@@ -1832,6 +1837,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18321837 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18331838 bci += oparg ;
18341839 oparg = 0 ;
1840+ if (instrumentation != null ) {
1841+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1842+ }
18351843 continue ;
18361844 } else {
18371845 bci += 3 ;
@@ -1847,6 +1855,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18471855 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18481856 bci += oparg ;
18491857 oparg = 0 ;
1858+ if (instrumentation != null ) {
1859+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1860+ }
18501861 continue ;
18511862 } else {
18521863 bci += 3 ;
@@ -1861,6 +1872,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18611872 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18621873 bci += oparg ;
18631874 oparg = 0 ;
1875+ if (instrumentation != null ) {
1876+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1877+ }
18641878 continue ;
18651879 } else {
18661880 virtualFrame .setObject (stackTop --, null );
@@ -1876,6 +1890,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18761890 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18771891 bci += oparg ;
18781892 oparg = 0 ;
1893+ if (instrumentation != null ) {
1894+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1895+ }
18791896 continue ;
18801897 } else {
18811898 virtualFrame .setObject (stackTop --, null );
@@ -1886,6 +1903,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
18861903 case OpCodesConstants .JUMP_BACKWARD : {
18871904 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
18881905 bci -= oparg ;
1906+ if (instrumentation != null ) {
1907+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1908+ }
18891909 if (CompilerDirectives .hasNextTier ()) {
18901910 mutableData .loopCount ++;
18911911 }
@@ -1951,6 +1971,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
19511971 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
19521972 bci += oparg ;
19531973 oparg = 0 ;
1974+ if (instrumentation != null ) {
1975+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
1976+ }
19541977 continue ;
19551978 }
19561979 break ;
@@ -1972,6 +1995,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
19721995 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
19731996 bci += oparg ;
19741997 oparg = 0 ;
1998+ if (instrumentation != null ) {
1999+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
2000+ }
19752001 continue ;
19762002 }
19772003 break ;
@@ -2042,6 +2068,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
20422068 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
20432069 bci += oparg ;
20442070 oparg = 0 ;
2071+ if (instrumentation != null ) {
2072+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
2073+ }
20452074 continue ;
20462075 } else {
20472076 bci += 3 ;
@@ -2108,7 +2137,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21082137 }
21092138 traceOrProfileYield (virtualFrame , mutableData , value , tracingEnabled , profilingEnabled );
21102139 if (instrumentation != null && instrumentationRoot instanceof WrapperNode ) {
2111- notifyRootReturn (virtualFrame , mutableData , value );
2140+ notifyReturn (virtualFrame , mutableData , instrumentation , beginBci , value );
21122141 }
21132142 return new GeneratorYieldResult (bci + 1 , stackTop , value );
21142143 }
@@ -2135,6 +2164,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21352164 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
21362165 bci += oparg ;
21372166 oparg = 0 ;
2167+ if (instrumentation != null ) {
2168+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
2169+ }
21382170 continue ;
21392171 }
21402172 }
@@ -2155,6 +2187,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21552187 oparg |= Byte .toUnsignedInt (localBC [bci + 1 ]);
21562188 bci += oparg ;
21572189 oparg = 0 ;
2190+ if (instrumentation != null ) {
2191+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
2192+ }
21582193 continue ;
21592194 }
21602195 }
@@ -2174,6 +2209,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21742209 // prepare next loop
21752210 oparg = 0 ;
21762211 bci ++;
2212+ if (instrumentation != null ) {
2213+ notifyStatement (virtualFrame , instrumentation , beginBci , bci );
2214+ }
21772215 } catch (PythonExitException | PythonThreadKillException e ) {
21782216 throw e ;
21792217 } catch (OSRException e ) {
@@ -2182,13 +2220,14 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21822220 } catch (Throwable e ) {
21832221 if (instrumentation != null ) {
21842222 // Need to handle instrumentation frame unwind
2185- Object result = notifyException (virtualFrame , instrumentation , mutableData , bci , e );
2223+ Object result = notifyException (virtualFrame , instrumentation , mutableData , beginBci , e );
21862224 if (result == ProbeNode .UNWIND_ACTION_REENTER ) {
21872225 CompilerDirectives .transferToInterpreter ();
21882226 copyArgs (virtualFrame .getArguments (), virtualFrame );
21892227 bci = 0 ;
21902228 stackTop = getInitialStackTop ();
21912229 oparg = 0 ;
2230+ notifyStatementAfterException (virtualFrame , instrumentation , beginBci );
21922231 continue ;
21932232 } else if (result != null ) {
21942233 return result ;
@@ -2197,7 +2236,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21972236 if (e instanceof ThreadDeath ) {
21982237 throw e ;
21992238 }
2200- int targetIndex = handleException (virtualFrame , localFrame , isGeneratorOrCoroutine , noTraceOrProfile , mutableData , bciSlot , initialStackTop , beginBci , stackTop , e );
2239+ int targetIndex = handleException (virtualFrame , localFrame , isGeneratorOrCoroutine , noTraceOrProfile , instrumentation , mutableData , bciSlot , initialStackTop , beginBci , stackTop , e );
22012240 if (targetIndex == -1 ) {
22022241 throw e ;
22032242 }
@@ -2209,7 +2248,8 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
22092248 }
22102249
22112250 @ InliningCutoff
2212- private int handleException (VirtualFrame virtualFrame , Frame localFrame , boolean isGeneratorOrCoroutine , Assumption noTraceOrProfile , MutableLoopData mutableData , int bciSlot , int initialStackTop ,
2251+ private int handleException (VirtualFrame virtualFrame , Frame localFrame , boolean isGeneratorOrCoroutine , Assumption noTraceOrProfile , InstrumentationSupport instrumentation ,
2252+ MutableLoopData mutableData , int bciSlot , int initialStackTop ,
22132253 int beginBci , int stackTop , Throwable e ) {
22142254 PException pe = null ;
22152255 boolean isInteropException = false ;
@@ -2278,9 +2318,19 @@ private int handleException(VirtualFrame virtualFrame, Frame localFrame, boolean
22782318 * replaced with the exception
22792319 */
22802320 virtualFrame .setObject (targetStackTop , isInteropException ? e : pe );
2321+
2322+ if (instrumentation != null ) {
2323+ int bci = exceptionHandlerRanges [targetIndex ];
2324+ notifyStatementAfterException (virtualFrame , instrumentation , bci );
2325+ }
22812326 return targetIndex ;
22822327 }
22832328
2329+ @ InliningCutoff
2330+ private void notifyStatementAfterException (VirtualFrame virtualFrame , InstrumentationSupport instrumentation , int bci ) {
2331+ instrumentation .notifyStatementEnter (virtualFrame , bciToLine (bci ));
2332+ }
2333+
22842334 @ InliningCutoff
22852335 private Object notifyException (VirtualFrame virtualFrame , InstrumentationSupport instrumentation , MutableLoopData mutableData , int bci , Throwable e ) {
22862336 try {
@@ -2307,23 +2357,22 @@ private void checkOnReturnExceptionalOrUnwindResult(Object result) {
23072357 }
23082358
23092359 @ InliningCutoff
2310- private void notifyRootReturn (VirtualFrame virtualFrame , MutableLoopData mutableData , Object value ) {
2360+ private void notifyReturn (VirtualFrame virtualFrame , MutableLoopData mutableData , InstrumentationSupport instrumentation , int bci , Object value ) {
2361+ instrumentation .notifyStatementExit (virtualFrame , bciToLine (bci ));
23112362 mutableData .setReturnCalled (true );
2312- WrapperNode wrapper = (WrapperNode ) instrumentationRoot ;
2313- wrapper .getProbeNode ().onReturnValue (virtualFrame , value );
2363+ if (instrumentationRoot instanceof WrapperNode ) {
2364+ WrapperNode wrapper = (WrapperNode ) instrumentationRoot ;
2365+ wrapper .getProbeNode ().onReturnValue (virtualFrame , value );
2366+ }
23142367 }
23152368
23162369 @ InliningCutoff
2317- private void notifyStatement (VirtualFrame virtualFrame , InstrumentationSupport instrumentation , MutableLoopData mutableData , int bci ) {
2318- int line = bciToLine (bci );
2319- int pastLine = mutableData .getPastLine ();
2320- instrumentation .notifyStatement (virtualFrame , pastLine , line );
2321- mutableData .setPastLine (line );
2322- mutableData .setPastBci (bci );
2370+ private void notifyStatement (VirtualFrame virtualFrame , InstrumentationSupport instrumentation , int prevBci , int nextBci ) {
2371+ instrumentation .notifyStatement (virtualFrame , bciToLine (prevBci ), bciToLine (nextBci ));
23232372 }
23242373
23252374 @ InliningCutoff
2326- private Object enterRoot (VirtualFrame virtualFrame ) {
2375+ private Object notifyEnter (VirtualFrame virtualFrame , InstrumentationSupport instrumentation , int initialBci ) {
23272376 if (instrumentationRoot instanceof WrapperNode ) {
23282377 WrapperNode wrapper = (WrapperNode ) instrumentationRoot ;
23292378 try {
@@ -2340,6 +2389,13 @@ private Object enterRoot(VirtualFrame virtualFrame) {
23402389 }
23412390 }
23422391 }
2392+ int line = bciToLine (initialBci );
2393+ try {
2394+ instrumentation .notifyStatementEnter (virtualFrame , line );
2395+ } catch (Throwable t ) {
2396+ instrumentation .notifyException (virtualFrame , line , t );
2397+ throw t ;
2398+ }
23432399 return null ;
23442400 }
23452401
@@ -4921,12 +4977,10 @@ public Object readSelf(VirtualFrame virtualFrame) {
49214977 }
49224978 }
49234979
4924- @ TruffleBoundary
49254980 public int bciToLine (int bci ) {
49264981 return co .bciToLine (bci );
49274982 }
49284983
4929- @ TruffleBoundary
49304984 public int getFirstLineno () {
49314985 return co .startLine ;
49324986 }
0 commit comments