1 /*
2  * Copyright (c) 1998, 2017, 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.tools.jdi;
27 
28 import com.sun.jdi.ClassNotPreparedException;
29 import com.sun.jdi.InconsistentDebugInfoException;
30 import com.sun.jdi.ObjectCollectedException;
31 import com.sun.jdi.VMDisconnectedException;
32 import com.sun.jdi.VMOutOfMemoryException;
33 import com.sun.jdi.VirtualMachine;
34 import com.sun.jdi.event.ClassPrepareEvent;
35 import com.sun.jdi.event.ClassUnloadEvent;
36 import com.sun.jdi.event.Event;
37 import com.sun.jdi.event.EventIterator;
38 import com.sun.jdi.event.EventSet;
39 
40 public class InternalEventHandler implements Runnable
41 {
42     EventQueueImpl queue;
43     VirtualMachineImpl vm;
44 
InternalEventHandler(VirtualMachineImpl vm, EventQueueImpl queue)45     InternalEventHandler(VirtualMachineImpl vm, EventQueueImpl queue) {
46         this.vm = vm;
47         this.queue = queue;
48         Thread thread = new Thread(vm.threadGroupForJDI(), this,
49                                    "JDI Internal Event Handler");
50         thread.setDaemon(true);
51         thread.start();
52     }
53 
run()54     public void run() {
55         if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) {
56             vm.printTrace("Internal event handler running");
57         }
58         try {
59             while (true) {
60                 try {
61                     EventSet eventSet = queue.removeInternal();
62                     EventIterator it = eventSet.eventIterator();
63                     while (it.hasNext()) {
64                         Event event = it.nextEvent();
65                         if (event instanceof ClassUnloadEvent) {
66                             ClassUnloadEvent cuEvent = (ClassUnloadEvent)event;
67                             vm.removeReferenceType(cuEvent.classSignature());
68 
69                             if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) {
70                                 vm.printTrace("Handled Unload Event for " +
71                                               cuEvent.classSignature());
72                             }
73                         } else if (event instanceof ClassPrepareEvent) {
74                             ClassPrepareEvent cpEvent = (ClassPrepareEvent)event;
75                             ((ReferenceTypeImpl)cpEvent.referenceType())
76                                                             .markPrepared();
77 
78                             if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) {
79                                 vm.printTrace("Handled Prepare Event for " +
80                                               cpEvent.referenceType().name());
81                             }
82                         }
83                     }
84 
85                 /*
86                  * Handle exceptions that can occur in normal operation
87                  * but which can't be accounted for by event builder
88                  * methods. The thread should not be terminated if they
89                  * occur.
90                  *
91                  * TO DO: We need a better way to log these conditions.
92                  */
93                 } catch (VMOutOfMemoryException vmme) {
94                     vmme.printStackTrace();
95                 } catch (InconsistentDebugInfoException idie) {
96                     idie.printStackTrace();
97 
98                 /*
99                  * If any of these exceptions below occurs, there is some
100                  * sort of programming error that should be addressed in
101                  * the JDI implemementation. However, it would cripple
102                  * the implementation if we let this thread die due to
103                  * one of them. So, a notification of the exception is
104                  * given and we attempt to continue.
105                  */
106                 } catch (ObjectCollectedException oce) {
107                     oce.printStackTrace();
108                 } catch (ClassNotPreparedException cnpe) {
109                     cnpe.printStackTrace();
110                 }
111             }
112         } catch (InterruptedException e) {  // should we really die here
113         } catch (VMDisconnectedException e) {  // time to die
114         }
115         if ((vm.traceFlags & VirtualMachine.TRACE_EVENTS) != 0) {
116             vm.printTrace("Internal event handler exiting");
117         }
118     }
119 }
120