|
| 1 | +/* |
| 2 | + * Copyright (c) 2013, Oracle and/or its affiliates. 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. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle 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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package com.sun.btrace; |
| 27 | + |
| 28 | +import java.io.BufferedReader; |
| 29 | +import java.io.File; |
| 30 | +import java.io.InputStreamReader; |
| 31 | +import java.io.PrintWriter; |
| 32 | +import java.net.URL; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | + |
| 36 | +/** |
| 37 | + * |
| 38 | + * @author Jaroslav Bachorik |
| 39 | + */ |
| 40 | +public class TestBTraceUtils { |
| 41 | + private String cp = null; |
| 42 | + private String java = null; |
| 43 | + |
| 44 | + @Before |
| 45 | + public void setup() { |
| 46 | + cp = System.getProperty("java.class.path"); |
| 47 | + java = System.getProperty("java.home"); |
| 48 | + } |
| 49 | + @Test |
| 50 | + public void testOSMBean() throws Exception { |
| 51 | + String cp = System.getProperty("java.class.path"); |
| 52 | + String java = System.getProperty("java.home"); |
| 53 | + |
| 54 | + ProcessBuilder pb = new ProcessBuilder( |
| 55 | + java + "/bin/java", |
| 56 | + "-cp", |
| 57 | + cp, |
| 58 | + "resources.Main" |
| 59 | + ); |
| 60 | + |
| 61 | + Process p = pb.start(); |
| 62 | + PrintWriter pw = new PrintWriter(p.getOutputStream()); |
| 63 | + BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream())); |
| 64 | + |
| 65 | + String l = br.readLine(); |
| 66 | + if (l.startsWith("ready:")) { |
| 67 | + String pid = l.split("\\:")[1]; |
| 68 | + System.out.println("Target process ready: " + pid); |
| 69 | +// attach(pid, "traces/OSMBeanTest.btrace"); |
| 70 | + attach(pid, "traces/onmethod/Args.class"); |
| 71 | + Thread.sleep(500); |
| 72 | + |
| 73 | + pw.println("done"); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void attach(String pid, String trace) throws Exception { |
| 78 | + trace = trace.replace(".btrace", ".java"); |
| 79 | + URL u = ClassLoader.getSystemResource(trace); |
| 80 | + System.out.println(trace); |
| 81 | + System.out.println(u); |
| 82 | + trace = new File(u.toURI()).getAbsolutePath(); |
| 83 | + ProcessBuilder pb = new ProcessBuilder( |
| 84 | + java + "/bin/java", |
| 85 | + "-cp", |
| 86 | + cp, |
| 87 | + "com.sun.btrace.client.Main", |
| 88 | + pid, |
| 89 | + trace |
| 90 | + ); |
| 91 | + pb.start(); |
| 92 | + } |
| 93 | +} |
0 commit comments