1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 import dalvik.system.PathClassLoader; 18 import dalvik.system.VMDebug; 19 import java.io.BufferedInputStream; 20 import java.io.BufferedOutputStream; 21 import java.io.FileInputStream; 22 import java.io.FileOutputStream; 23 import java.io.File; 24 import java.io.IOException; 25 26 public class Main { main(String[] args)27 public static void main(String[] args) throws Exception { 28 System.loadLibrary(args[0]); 29 System.out.println("Hello, world!"); 30 String agent = null; 31 // By default allow debugging 32 boolean debugging_allowed = true; 33 for(String a : args) { 34 if(a.startsWith("agent:")) { 35 agent = a.substring(6); 36 } else if (a.equals("disallow-debugging")) { 37 debugging_allowed = false; 38 } 39 } 40 if (agent == null) { 41 throw new Error("Could not find agent: argument!"); 42 } 43 setDebuggingAllowed(debugging_allowed); 44 // Setup is finished. Try to attach agent in 2 ways. 45 try { 46 VMDebug.attachAgent(agent, null); 47 } catch(SecurityException e) { 48 System.out.println(e.getMessage()); 49 } 50 attachWithClassLoader(args); 51 System.out.println("Goodbye!"); 52 } 53 setDebuggingAllowed(boolean val)54 private static native void setDebuggingAllowed(boolean val); 55 attachWithClassLoader(String[] args)56 private static void attachWithClassLoader(String[] args) throws Exception { 57 for(String a : args) { 58 if(a.startsWith("agent:")) { 59 try { 60 VMDebug.attachAgent(a.substring(6), Main.class.getClassLoader()); 61 } catch(SecurityException e) { 62 System.out.println(e.getMessage()); 63 } catch (Exception e) { 64 e.printStackTrace(System.out); 65 } 66 } 67 } 68 } 69 } 70