Skip to content

Commit 9505a23

Browse files
committed
#BTRACE-87: Fixing the CALL location instrumentor to correctly work with AnyType
1 parent deded99 commit 9505a23

5 files changed

Lines changed: 150 additions & 9 deletions

File tree

src/share/classes/com/sun/btrace/runtime/Instrumentor.java

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
package com.sun.btrace.runtime;
2727

28+
import com.sun.btrace.AnyType;
2829
import java.util.List;
2930
import java.util.ArrayList;
3031
import java.util.Set;
@@ -257,7 +258,7 @@ private MethodVisitor instrumentorFor(
257258

258259
private MethodVisitor instrumentorFor(
259260
final OnMethod om, MethodVisitor mv, final LocalVariablesSorter lvs,
260-
final int[] tsIndex, int access, String name, String desc) {
261+
final int[] tsIndex, int access, String name, final String desc) {
261262
final Location loc = om.getLocation();
262263
final Where where = loc.getWhere();
263264
final Type[] actionArgTypes = Type.getArgumentTypes(om.getTargetDescriptor());
@@ -415,13 +416,24 @@ protected void onAfterArrayStore(int opcode) {
415416
private int returnVarIndex = -1;
416417
int[] backupArgsIndexes;
417418

418-
private void injectBtrace(ValidationResult vr, final String method, final Type returnType) {
419+
private void injectBtrace(ValidationResult vr, final String method, final Type[] callArgTypes, final Type returnType) {
419420
ArgumentProvider[] actionArgs = new ArgumentProvider[actionArgTypes.length + 6];
420421
for(int i=0;i<vr.getArgCnt();i++) {
421422
int index = vr.getArgIdx(i);
422423
Type t = actionArgTypes[index];
423424
if (TypeUtils.isAnyTypeArray(t)) {
424-
actionArgs[i] = new AnyTypeArgProvider(i, backupArgsIndexes[i+1]);
425+
if (i < backupArgsIndexes.length - 1) {
426+
actionArgs[i] = new AnyTypeArgProvider(index, backupArgsIndexes[i+1], callArgTypes);
427+
} else {
428+
actionArgs[i] = new ArgumentProvider(index) {
429+
430+
@Override
431+
protected void doProvide() {
432+
push(0);
433+
visitTypeInsn(ANEWARRAY, TypeUtils.objectType.getInternalName());
434+
}
435+
};
436+
}
425437
} else {
426438
actionArgs[i] = new LocalVarArgProvider(index, actionArgTypes[index], backupArgsIndexes[i+1]);;
427439
}
@@ -472,7 +484,7 @@ && typeMatches(loc.getType(), desc)) {
472484
// will store the call args into local variables
473485
backupArgsIndexes = backupStack(lvs, Type.getArgumentTypes(desc), isStaticCall);
474486
if (where == Where.BEFORE) {
475-
injectBtrace(vr, method, Type.getReturnType(desc));
487+
injectBtrace(vr, method, Type.getArgumentTypes(desc), Type.getReturnType(desc));
476488
}
477489
// put the call args back on stack so the method call can find them
478490
restoreStack(backupArgsIndexes, Type.getArgumentTypes(desc), isStaticCall);
@@ -509,7 +521,7 @@ && typeMatches(loc.getType(), desc)) {
509521
returnVarIndex = index;
510522
}
511523
// will also retrieve the call args and the return value from the backup variables
512-
injectBtrace(vr, method, returnType);
524+
injectBtrace(vr, method, calledMethodArgs, returnType);
513525
if (withReturn) {
514526
loadLocal(returnType, returnVarIndex); // restore the return value
515527
}

src/share/classes/com/sun/btrace/runtime/MethodInstrumentor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,25 @@ public String toString() {
221221

222222
protected class AnyTypeArgProvider extends ArgumentProvider {
223223
private int argPtr;
224-
224+
private Type[] myArgTypes;
225225
public AnyTypeArgProvider(int index, int basePtr) {
226+
this(index, basePtr, argumentTypes);
227+
}
228+
229+
public AnyTypeArgProvider(int index, int basePtr, Type[] argTypes) {
226230
super(index);
227231
this.argPtr = basePtr;
232+
this.myArgTypes = argTypes;
228233
}
234+
229235

230236
public void doProvide() {
231-
push(argumentTypes.length);
237+
push(myArgTypes.length);
232238
visitTypeInsn(ANEWARRAY, TypeUtils.objectType.getInternalName());
233-
for (int j = 0; j < argumentTypes.length; j++) {
239+
for (int j = 0; j < myArgTypes.length; j++) {
234240
dup();
235241
push(j);
236-
Type argType = argumentTypes[j];
242+
Type argType = myArgTypes[j];
237243
loadLocal(argType, argPtr);
238244
box(argType);
239245
arrayStore(TypeUtils.objectType);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2008-2010 Sun Microsystems, Inc. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Sun designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Sun in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22+
* CA 95054 USA or visit www.sun.com if you need additional information or
23+
* have any questions.
24+
*/
25+
26+
package com.sun.btrace.runtime;
27+
28+
import support.InstrumentorTestBase;
29+
import org.junit.Test;
30+
31+
/**
32+
*
33+
* @author Jaroslav Bachorik
34+
*/
35+
public class BTRACE87Test extends InstrumentorTestBase {
36+
@Test
37+
public void bytecodeValidation() throws Exception {
38+
originalBC = loadTargetClass("issues/BTRACE87");
39+
transform("issues/BTRACE87");
40+
checkTransformation("ASTORE 2\nALOAD 0\nLDC \"containerMethod\"\n" +
41+
"ICONST_0\nANEWARRAY java/lang/Object\n" +
42+
"INVOKESTATIC resources/issues/BTRACE87.$btrace$traces$issues$BTRACE87$o (Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V\n" +
43+
"ALOAD 2\nASTORE 3\nASTORE 4\nALOAD 0\nLDC \"containerMethod\"\n" +
44+
"ICONST_1\nANEWARRAY java/lang/Object\n" +
45+
"DUP\nICONST_0\nALOAD 3\nAASTORE\n" +
46+
"INVOKESTATIC resources/issues/BTRACE87.$btrace$traces$issues$BTRACE87$o (Ljava/lang/Object;Ljava/lang/String;[Ljava/lang/Object;)V");
47+
}
48+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* To change this template, choose Tools | Templates
3+
* and open the template in the editor.
4+
*/
5+
package resources.issues;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
/**
11+
*
12+
* @author Jaroslav Bachorik <jaroslav.bachorik@oracle.com>
13+
*/
14+
public class BTRACE87 {
15+
private void containerMethod() {
16+
List a = new ArrayList();
17+
a.clear();
18+
a.add("sample");
19+
}
20+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2008-2010 Sun Microsystems, Inc. All Rights Reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Sun designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Sun in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22+
* CA 95054 USA or visit www.sun.com if you need additional information or
23+
* have any questions.
24+
*/
25+
26+
package traces.issues;
27+
28+
import com.sun.btrace.AnyType;
29+
import com.sun.btrace.annotations.*;
30+
import static com.sun.btrace.BTraceUtils.*;
31+
32+
/**
33+
* This script traces method/block entry into every method of
34+
* every class in javax.swing package! Think before using
35+
* this script -- this will slow down your app significantly!!
36+
* Note tha Where.BEFORE is default. For synchronized blocks, BEFORE
37+
* means before "monitorenter" bytecode. For synchronized methods, we
38+
* can not have probe point Where.BEFORE. Lock is acquired before entering
39+
* synchronized method. By making the probe point Where.AFTER for SYNC_ENTER,
40+
* we probe after monitorenter bytecode or synchronized method entry.
41+
*/
42+
@BTrace public class BTRACE87 {
43+
@OnMethod(
44+
clazz="/.*\\.BTRACE87/",
45+
method="/.*/",
46+
location=@Location(value=Kind.CALL, clazz="/.*/", method="/.*/")
47+
)
48+
public static void o(@Self Object self, @ProbeMethodName String pmn, AnyType[] args) { // all calls to methods
49+
// self - this for the method call
50+
// pmn - textual representation of the method
51+
// contents of args array:
52+
// [0]..[n] - original method call arguments
53+
printArray(args);
54+
}
55+
}

0 commit comments

Comments
 (0)