jdk/src/share/classes/com/sun/tools/example/debug/bdi/JDIEventSource.java
author xdono
Wed, 02 Jul 2008 12:55:45 -0700
changeset 715 f16baef3a20e
parent 51 6fe31bc95bbc
child 5506 202f599c92aa
permissions -rw-r--r--
6719955: Update copyright year Summary: Update copyright year for files that have been modified in 2008 Reviewed-by: ohair, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
715
f16baef3a20e 6719955: Update copyright year
xdono
parents: 51
diff changeset
     2
 * Copyright 1999-2008 Sun Microsystems, Inc.  All Rights Reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.example.debug.bdi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import com.sun.jdi.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import com.sun.jdi.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import com.sun.tools.example.debug.event.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import javax.swing.SwingUtilities;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
class JDIEventSource extends Thread {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    private /*final*/ EventQueue queue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private /*final*/ Session session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private /*final*/ ExecutionManager runtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    private final JDIListener firstListener = new FirstListener();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private boolean wantInterrupt;  //### Hack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
     * Create event source.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    JDIEventSource(Session session) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        super("JDI Event Set Dispatcher");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        this.session = session;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        this.runtime = session.runtime;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        this.queue = session.vm.eventQueue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            runLoop();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        } catch (Exception exc) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            //### Do something different for InterruptedException???
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            // just exit
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        session.running = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private void runLoop() throws InterruptedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        AbstractEventSet es;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            EventSet jdiEventSet = queue.remove();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            es = AbstractEventSet.toSpecificEventSet(jdiEventSet);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            session.interrupted = es.suspendedAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            dispatchEventSet(es);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        } while(!(es instanceof VMDisconnectEventSet));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    //### Gross foul hackery!
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    private void dispatchEventSet(final AbstractEventSet es) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        SwingUtilities.invokeLater(new Runnable() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            public void run() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                boolean interrupted = es.suspendedAll();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                es.notify(firstListener);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                boolean wantInterrupt = JDIEventSource.this.wantInterrupt;
51
6fe31bc95bbc 6600143: Remove another 450 unnecessary casts
martin
parents: 2
diff changeset
    85
                for (JDIListener jl : session.runtime.jdiListeners) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                    es.notify(jl);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                if (interrupted && !wantInterrupt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                    session.interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                    //### Catch here is a hack
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                        session.vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                    } catch (VMDisconnectedException ee) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                if (es instanceof ThreadDeathEventSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                    ThreadReference t = ((ThreadDeathEventSet)es).getThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                    session.runtime.removeThreadInfo(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private void finalizeEventSet(AbstractEventSet es) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if (session.interrupted && !wantInterrupt) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            session.interrupted = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            //### Catch here is a hack
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                session.vm.resume();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            } catch (VMDisconnectedException ee) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        if (es instanceof ThreadDeathEventSet) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
            ThreadReference t = ((ThreadDeathEventSet)es).getThread();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            session.runtime.removeThreadInfo(t);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    //### This is a Hack, deal with it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private class FirstListener implements JDIListener {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        public void accessWatchpoint(AccessWatchpointEventSet e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            session.runtime.validateThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            wantInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        public void classPrepare(ClassPrepareEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            runtime.resolve(e.getReferenceType());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        public void classUnload(ClassUnloadEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        public void exception(ExceptionEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            wantInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        public void locationTrigger(LocationTriggerEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            session.runtime.validateThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            wantInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        public void modificationWatchpoint(ModificationWatchpointEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            session.runtime.validateThreadInfo();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            wantInterrupt = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        public void threadDeath(ThreadDeathEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        public void threadStart(ThreadStartEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        public void vmDeath(VMDeathEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            //### Should have some way to notify user
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
            //### that VM died before the session ended.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        public void vmDisconnect(VMDisconnectEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            //### Notify user?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            session.runtime.endSession();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        public void vmStart(VMStartEventSet e)  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            //### Do we need to do anything with it?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
            wantInterrupt = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
}