Source Code Cross Referenced for SolarisVirtualMachine.java in  » 6.0-JDK-Platform » solaris » sun » tools » attach » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » 6.0 JDK Platform » solaris » sun.tools.attach 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Copyright 2005-2006 Sun Microsystems, Inc.  All Rights Reserved.
003:         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004:         *
005:         * This code is free software; you can redistribute it and/or modify it
006:         * under the terms of the GNU General Public License version 2 only, as
007:         * published by the Free Software Foundation.  Sun designates this
008:         * particular file as subject to the "Classpath" exception as provided
009:         * by Sun in the LICENSE file that accompanied this code.
010:         *
011:         * This code is distributed in the hope that it will be useful, but WITHOUT
012:         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013:         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014:         * version 2 for more details (a copy is included in the LICENSE file that
015:         * accompanied this code).
016:         *
017:         * You should have received a copy of the GNU General Public License version
018:         * 2 along with this work; if not, write to the Free Software Foundation,
019:         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020:         *
021:         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022:         * CA 95054 USA or visit www.sun.com if you need additional information or
023:         * have any questions.
024:         */
025:        package sun.tools.attach;
026:
027:        import com.sun.tools.attach.VirtualMachine;
028:        import com.sun.tools.attach.AgentLoadException;
029:        import com.sun.tools.attach.AttachNotSupportedException;
030:        import com.sun.tools.attach.spi.AttachProvider;
031:        import java.io.InputStream;
032:        import java.io.IOException;
033:        import java.io.File;
034:        import java.io.FileNotFoundException;
035:        import java.util.Properties;
036:
037:        /*
038:         * Solaris implementation of HotSpotVirtualMachine.
039:         */
040:        public class SolarisVirtualMachine extends HotSpotVirtualMachine {
041:
042:            // door descriptor;
043:            private int fd = -1;
044:
045:            /**
046:             * Attaches to the target VM
047:             */
048:            SolarisVirtualMachine(AttachProvider provider, String vmid)
049:                    throws AttachNotSupportedException, IOException {
050:                super (provider, vmid);
051:                // This provider only understands process-ids (pids).
052:                int pid;
053:                try {
054:                    pid = Integer.parseInt(vmid);
055:                } catch (NumberFormatException x) {
056:                    throw new AttachNotSupportedException(
057:                            "invalid process identifier");
058:                }
059:
060:                // Opens the door file to the target VM. If the file is not
061:                // found it might mean that the attach mechanism isn't started in the
062:                // target VM so we attempt to start it and retry.
063:                try {
064:                    fd = openDoor(pid);
065:                } catch (FileNotFoundException fnf1) {
066:                    File f = createAttachFile(pid);
067:                    try {
068:                        // kill -QUIT will tickle target VM to check for the
069:                        // attach file.
070:                        sigquit(pid);
071:
072:                        // give the target VM time to start the attach mechanism
073:                        int i = 0;
074:                        long delay = 200;
075:                        int retries = (int) (attachTimeout() / delay);
076:                        do {
077:                            try {
078:                                Thread.sleep(delay);
079:                            } catch (InterruptedException x) {
080:                            }
081:                            try {
082:                                fd = openDoor(pid);
083:                            } catch (FileNotFoundException fnf2) {
084:                            }
085:                            i++;
086:                        } while (i <= retries && fd == -1);
087:                        if (fd == -1) {
088:                            throw new AttachNotSupportedException(
089:                                    "Unable to open door: target process not responding or "
090:                                            + "HotSpot VM not loaded");
091:                        }
092:                    } finally {
093:                        f.delete();
094:                    }
095:                }
096:                assert fd >= 0;
097:            }
098:
099:            /**
100:             * Detach from the target VM
101:             */
102:            public void detach() throws IOException {
103:                synchronized (this ) {
104:                    if (fd != -1) {
105:                        close(fd);
106:                        fd = -1;
107:                    }
108:                }
109:            }
110:
111:            /**
112:             * Execute the given command in the target VM.
113:             */
114:            InputStream execute(String cmd, Object... args)
115:                    throws AgentLoadException, IOException {
116:                assert args.length <= 3; // includes null
117:
118:                // first check that we are still attached
119:                int door;
120:                synchronized (this ) {
121:                    if (fd == -1) {
122:                        throw new IOException("Detached from target VM");
123:                    }
124:                    door = fd;
125:                }
126:
127:                // enqueue the command via a door call
128:                int s = enqueue(door, cmd, args);
129:                assert s >= 0; // valid file descriptor
130:
131:                // The door call returns a file descriptor (one end of a socket pair).
132:                // Create an input stream around it.
133:                SocketInputStream sis = new SocketInputStream(s);
134:
135:                // Read the command completion status
136:                int completionStatus;
137:                try {
138:                    completionStatus = readInt(sis);
139:                } catch (IOException ioe) {
140:                    sis.close();
141:                    throw ioe;
142:                }
143:
144:                // If non-0 it means an error but we need to special-case the
145:                // "load" command to ensure that the right exception is thrown.
146:                if (completionStatus != 0) {
147:                    sis.close();
148:                    if (cmd.equals("load")) {
149:                        throw new AgentLoadException(
150:                                "Failed to load agent library");
151:                    } else {
152:                        throw new IOException("Command failed in target VM");
153:                    }
154:                }
155:
156:                // Return the input stream so that the command output can be read
157:                return sis;
158:            }
159:
160:            // InputStream over a socket
161:            private class SocketInputStream extends InputStream {
162:                int s;
163:
164:                public SocketInputStream(int s) {
165:                    this .s = s;
166:                }
167:
168:                public synchronized int read() throws IOException {
169:                    byte b[] = new byte[1];
170:                    int n = this .read(b, 0, 1);
171:                    if (n == 1) {
172:                        return b[0] & 0xff;
173:                    } else {
174:                        return -1;
175:                    }
176:                }
177:
178:                public synchronized int read(byte[] bs, int off, int len)
179:                        throws IOException {
180:                    if ((off < 0) || (off > bs.length) || (len < 0)
181:                            || ((off + len) > bs.length) || ((off + len) < 0)) {
182:                        throw new IndexOutOfBoundsException();
183:                    } else if (len == 0)
184:                        return 0;
185:
186:                    return SolarisVirtualMachine.read(s, bs, off, len);
187:                }
188:
189:                public void close() throws IOException {
190:                    SolarisVirtualMachine.close(s);
191:                }
192:            }
193:
194:            // The door is attached to .java_pid<pid> in the target VM's working
195:            // directory or /tmp. 
196:            private int openDoor(int pid) throws IOException {
197:                // First check for a .java_pid<pid> file in the working directory
198:                // of the target process
199:                String fn = ".java_pid" + pid;
200:                String path = "/proc/" + pid + "/cwd/" + fn;
201:                try {
202:                    fd = open(path);
203:                } catch (FileNotFoundException fnf) {
204:                    path = "/tmp/" + fn;
205:                    fd = open(path);
206:                }
207:
208:                // Check that the file owner/permission to avoid attaching to
209:                // bogus process
210:                try {
211:                    checkPermissions(path);
212:                } catch (IOException ioe) {
213:                    close(fd);
214:                    throw ioe;
215:                }
216:                return fd;
217:            }
218:
219:            // On Solaris/Linux a simple handshake is used to start the attach mechanism
220:            // if not already started. The client creates a .attach_pid<pid> file in the
221:            // target VM's working directory (or /tmp), and the SIGQUIT handler checks
222:            // for the file.
223:            private File createAttachFile(int pid) throws IOException {
224:                String fn = ".attach_pid" + pid;
225:                String path = "/proc/" + pid + "/cwd/" + fn;
226:                File f = new File(path);
227:                try {
228:                    f.createNewFile();
229:                } catch (IOException x) {
230:                    path = "/tmp/" + fn;
231:                    f = new File(path);
232:                    f.createNewFile();
233:                }
234:                return f;
235:            }
236:
237:            //-- native methods
238:
239:            static native int open(String path) throws IOException;
240:
241:            static native void close(int fd) throws IOException;
242:
243:            static native int read(int fd, byte buf[], int off, int buflen)
244:                    throws IOException;
245:
246:            static native void checkPermissions(String path) throws IOException;
247:
248:            static native void sigquit(int pid) throws IOException;
249:
250:            // enqueue a command (and arguments) to the given door
251:            static native int enqueue(int fd, String cmd, Object... args)
252:                    throws IOException;
253:
254:            static {
255:                System.loadLibrary("attach");
256:            }
257:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.