1 /*
   2  * Copyright (c) 1998, 2011, 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 /*
  27  * This source code is provided to illustrate the usage of a given feature
  28  * or technique and has been deliberately simplified. Additional steps
  29  * required for a production-quality application, such as security checks,
  30  * input validation and proper error handling, might not be present in
  31  * this sample code.
  32  */
  33 
  34 
  35 package com.sun.tools.example.debug.bdi;
  36 
  37 import com.sun.jdi.VirtualMachine;
  38 import com.sun.jdi.VMDisconnectedException;
  39 
  40 /**
  41  * Our repository of what we know about the state of one
  42  * running VM.
  43  */
  44 class Session {
  45 
  46     final VirtualMachine vm;
  47     final ExecutionManager runtime;
  48     final OutputListener diagnostics;
  49 
  50     boolean running = true;  // Set false by JDIEventSource
  51     boolean interrupted = false;  // Set false by JDIEventSource
  52 
  53     private JDIEventSource eventSourceThread = null;
  54     private int traceFlags;
  55     private boolean dead = false;
  56 
  57     public Session(VirtualMachine vm, ExecutionManager runtime,
  58                    OutputListener diagnostics) {
  59         this.vm = vm;
  60         this.runtime = runtime;
  61         this.diagnostics = diagnostics;
  62         this.traceFlags = VirtualMachine.TRACE_NONE;
  63     }
  64 
  65     /**
  66      * Determine if VM is interrupted, i.e, present and not running.
  67      */
  68     public boolean isInterrupted() {
  69         return interrupted;
  70     }
  71 
  72     public void setTraceMode(int traceFlags) {
  73         this.traceFlags = traceFlags;
  74         if (!dead) {
  75             vm.setDebugTraceMode(traceFlags);
  76         }
  77     }
  78 
  79     public boolean attach() {
  80         vm.setDebugTraceMode(traceFlags);
  81         diagnostics.putString("Connected to VM");
  82         eventSourceThread = new JDIEventSource(this);
  83         eventSourceThread.start();
  84         return true;
  85     }
  86 
  87     public void detach() {
  88         if (!dead) {
  89             eventSourceThread.interrupt();
  90             eventSourceThread = null;
  91             //### The VM may already be disconnected
  92             //### if the debuggee did a System.exit().
  93             //### Exception handler here is a kludge,
  94             //### Rather, there are many other places
  95             //### where we need to handle this exception,
  96             //### and initiate a detach due to an error
  97             //### condition, e.g., connection failure.
  98             try {
  99                 vm.dispose();
 100             } catch (VMDisconnectedException ee) {}
 101             dead = true;
 102             diagnostics.putString("Disconnected from VM");
 103         }
 104     }
 105 }