001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: * The Original Software is NetBeans. The Initial Developer of the Original
026: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
027: * Microsystems, Inc. All Rights Reserved.
028: *
029: * If you wish your version of this file to be governed by only the CDDL
030: * or only the GPL Version 2, indicate your decision by adding
031: * "[Contributor] elects to include this software in this distribution
032: * under the [CDDL or GPL Version 2] license." If you do not indicate a
033: * single choice of license, a recipient has the option to distribute
034: * your version of this file under either the CDDL, the GPL Version 2 or
035: * to extend the choice of license to its licensees as provided above.
036: * However, if you add GPL Version 2 code and therefore, elected the GPL
037: * Version 2 license, then the option applies only if the new code is
038: * made subject to such option by the copyright holder.
039: */
040:
041: package org.netbeans.lib.profiler.instrumentation;
042:
043: import org.netbeans.lib.profiler.classfile.DynamicClassInfo;
044: import org.netbeans.lib.profiler.client.RuntimeProfilingPoint;
045: import org.netbeans.lib.profiler.global.CommonConstants;
046:
047: /**
048: * This class provides essentially a convenience static-method API that allows one to obtain a version of a particular
049: * method, instrumented in a particular predefined way.
050: *
051: * @author Tomas Hurka
052: * @author Misha Dmitriev
053: */
054: public class InstrumentationFactory implements CommonConstants {
055: //~ Methods ------------------------------------------------------------------------------------------------------------------
056:
057: public static byte[] instrumentAsProiflePointHitMethod(
058: DynamicClassInfo clazz, int methodIdx, int injType,
059: RuntimeProfilingPoint[] points) {
060: Injector mi = new ProfilePointHitCallInjector(
061: clazz,
062: clazz.getBaseCPoolCount(injType),
063: methodIdx,
064: points,
065: CPExtensionsRepository.normalContents_ProfilePointHitMethodIdx);
066:
067: return mi.instrumentMethod();
068: }
069:
070: public static byte[] instrumentAsReflectInvokeMethod(
071: DynamicClassInfo clazz, int methodIdx) {
072: Injector mi = new HandleReflectInvokeCallInjector(clazz, clazz
073: .getBaseCPoolCount(INJ_REFLECT_METHOD_INVOKE),
074: methodIdx);
075:
076: return mi.instrumentMethod();
077: }
078:
079: public static byte[] instrumentAsServletDoMethod(
080: DynamicClassInfo clazz, int methodIdx) {
081: Injector mi = new HandleServletDoMethodCallInjector(clazz,
082: clazz.getBaseCPoolCount(INJ_SERVLET_DO_METHOD),
083: methodIdx);
084:
085: return mi.instrumentMethod();
086: }
087:
088: public static byte[] instrumentCodeRegion(DynamicClassInfo clazz,
089: int methodIdx, int bci0, int bci1) {
090: Injector mi = new CodeRegionEntryExitCallsInjector(clazz, clazz
091: .getBaseCPoolCount(INJ_CODE_REGION), methodIdx, bci0,
092: bci1);
093:
094: return mi.instrumentMethod();
095: }
096:
097: /** injType is either INJ_OBJECT_ALLOCATIONS or INJ_OBJECT_LIVENESS */
098: public static byte[] instrumentForMemoryProfiling(
099: DynamicClassInfo clazz, int methodIdx,
100: boolean[] allUnprofiledClassStatusArray, int injType,
101: RuntimeProfilingPoint[] points) {
102: Injector mi = new ObjLivenessInstrCallsInjector(clazz, clazz
103: .getBaseCPoolCount(injType), methodIdx,
104: allUnprofiledClassStatusArray);
105: mi
106: .insertProfilingPoints(
107: points,
108: CPExtensionsRepository.memoryProfContents_ProfilePointHitMethodIdx);
109:
110: return mi.instrumentMethod();
111: }
112:
113: /**
114: * normalInjectionType is either INJ_RECURSIVE_NORMAL_METHOD or INJ_RECURSIVE_SAMPLED_NORMAL_METHOD
115: * rootInjectionType is either INJ_RECURSIVE_ROOT_METHOD or INJ_RECURSIVE_SAMPLED_ROOT_METHOD
116: */
117: public static byte[] instrumentMethod(DynamicClassInfo clazz,
118: int methodIdx, int normalInjectionType,
119: int rootInjectionType, int markerInjectionType,
120: int methodId, RuntimeProfilingPoint[] points) {
121: int baseCPCount0 = clazz.getBaseCPoolCount(normalInjectionType);
122: int baseCPCount1;
123: int injType;
124:
125: if (clazz.isMethodRoot(methodIdx)) {
126: baseCPCount1 = clazz.getBaseCPoolCount(rootInjectionType);
127: injType = rootInjectionType;
128: } else if (clazz.isMethodMarker(methodIdx)) {
129: baseCPCount1 = clazz.getBaseCPoolCount(markerInjectionType);
130: injType = markerInjectionType;
131: } else {
132: baseCPCount1 = 0;
133: injType = normalInjectionType;
134: }
135:
136: Injector mi = new MethodEntryExitCallsInjector(clazz,
137: baseCPCount0, baseCPCount1, methodIdx, injType,
138: methodId);
139: mi
140: .insertProfilingPoints(
141: points,
142: CPExtensionsRepository.normalContents_ProfilePointHitMethodIdx);
143:
144: byte[] res = mi.instrumentMethod();
145: clazz.setInstrMethodId(methodIdx, methodId);
146:
147: return res;
148: }
149: }
|