cdt/cdt_6_0_x/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/internal/GdbPlugin.java
author timkelly
Tue, 15 Sep 2009 10:25:52 -0500
changeset 96 defec9d2b40a
parent 37 c2bce6dd59e7
permissions -rw-r--r--
refactor to use AbstractCLaunchDelegate (bug 9762)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     1
/*******************************************************************************
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     2
 * Copyright (c) 2006, 2008 Wind River Systems and others.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     3
 * All rights reserved. This program and the accompanying materials
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     4
 * are made available under the terms of the Eclipse Public License v1.0
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     5
 * which accompanies this distribution, and is available at
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     6
 * http://www.eclipse.org/legal/epl-v10.html
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     7
 * 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     8
 * Contributors:
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
     9
 *     Wind River Systems - initial API and implementation
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    10
 *******************************************************************************/
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    11
package org.eclipse.cdt.dsf.gdb.internal;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    12
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    13
import java.util.concurrent.ExecutionException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    14
import java.util.concurrent.RejectedExecutionException;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    15
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    16
import org.eclipse.cdt.dsf.concurrent.DataRequestMonitor;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    17
import org.eclipse.cdt.dsf.concurrent.Query;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    18
import org.eclipse.cdt.dsf.gdb.launching.GdbLaunch;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    19
import org.eclipse.core.runtime.IStatus;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    20
import org.eclipse.core.runtime.Platform;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    21
import org.eclipse.core.runtime.Plugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    22
import org.eclipse.core.runtime.Status;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    23
import org.eclipse.debug.core.DebugPlugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    24
import org.eclipse.debug.core.ILaunch;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    25
import org.osgi.framework.BundleContext;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    26
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    27
/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    28
 * The activator class controls the plug-in life cycle
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    29
 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    30
public class GdbPlugin extends Plugin {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    31
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    32
    // Debugging flag
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    33
    public static boolean DEBUG = false;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    34
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    35
	// The plug-in ID
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    36
	public static final String PLUGIN_ID = "org.eclipse.cdt.dsf.gdb"; //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    38
	// The shared instance
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    39
	private static GdbPlugin plugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    40
	
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    41
    private static BundleContext fgBundleContext; 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    42
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    43
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    44
	 * The constructor
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    45
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    46
	public GdbPlugin() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    47
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    48
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    49
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    50
	 * (non-Javadoc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    51
	 * @see org.eclipse.core.runtime.Plugins#start(org.osgi.framework.BundleContext)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    52
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    53
	@Override
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    54
    public void start(BundleContext context) throws Exception {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    55
        fgBundleContext = context;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    56
		super.start(context);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    57
		plugin = this;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    58
		
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    59
        DEBUG = "true".equals(Platform.getDebugOption("org.eclipse.cdt.dsf.gdb/debug"));  //$NON-NLS-1$//$NON-NLS-2$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    60
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    61
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    62
	/*
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    63
	 * (non-Javadoc)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    64
	 * @see org.eclipse.core.runtime.Plugin#stop(org.osgi.framework.BundleContext)
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    65
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    66
	@Override
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    67
    public void stop(BundleContext context) throws Exception {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    68
	    shutdownActiveLaunches();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    69
		plugin = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    70
		super.stop(context);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    71
        fgBundleContext = null;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    72
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    73
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    74
	/**
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    75
	 * Returns the shared instance
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    76
	 *
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    77
	 * @return the shared instance
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    78
	 */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    79
	public static GdbPlugin getDefault() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    80
		return plugin;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    81
	}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    82
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    83
    public static BundleContext getBundleContext() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    84
        return fgBundleContext;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    85
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    86
    
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    87
    /** 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    88
     * Shuts down any active launches.  We must shutdown any active sessions 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    89
     * and services associated with this plugin before this plugin is stopped.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    90
     * Any attempts to use the plugins {@link BundleContext} after the plugin
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    91
     * is shut down will result in exceptions. 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    92
     */
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    93
    private void shutdownActiveLaunches() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    94
        for (ILaunch launch : DebugPlugin.getDefault().getLaunchManager().getLaunches()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    95
            if (launch instanceof GdbLaunch && ((GdbLaunch)launch).getSession().isActive()) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    96
                final GdbLaunch gdbLaunch = (GdbLaunch)launch;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    97
                
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    98
                Query<Object> launchShutdownQuery = new Query<Object>() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
    99
                    @Override
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   100
                    protected void execute(DataRequestMonitor<Object> rm) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   101
                        gdbLaunch.shutdownSession(rm);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   102
                    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   103
                };
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   104
                
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   105
                try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   106
                    gdbLaunch.getSession().getExecutor().execute(launchShutdownQuery);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   107
                } catch (RejectedExecutionException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   108
                    // We can get this exception if the session is shutdown concurrently
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   109
                    // to this method running.
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   110
                    break;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   111
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   112
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   113
                // The Query.get() method is a synchronous call which blocks until the 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   114
                // query completes.  
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   115
                try {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   116
                    launchShutdownQuery.get();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   117
                } catch (InterruptedException e) { 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   118
                    getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, "InterruptedException while shutting down PDA debugger launch " + gdbLaunch, e.getCause())); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   119
                } catch (ExecutionException e) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   120
                    getLog().log(new Status(IStatus.ERROR, PLUGIN_ID, "Exception while shutting down PDA debugger launch " + gdbLaunch, e.getCause())); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   121
                }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   122
            }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   123
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   124
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   125
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   126
    public static void debug(String message) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   127
        if (DEBUG) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   128
			while (message.length() > 100) {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   129
				String partial = message.substring(0, 100); 
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   130
				message = message.substring(100);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   131
				System.out.println(partial + "\\"); //$NON-NLS-1$
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   132
			}
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   133
			System.out.print(message);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   134
        }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   135
    }
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   136
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   137
    public static String getDebugTime() {
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   138
        StringBuilder traceBuilder = new StringBuilder();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   139
        
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   140
        // Record the time
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   141
        long time = System.currentTimeMillis();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   142
        long seconds = (time / 1000) % 1000;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   143
        if (seconds < 100) traceBuilder.append('0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   144
        if (seconds < 10) traceBuilder.append('0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   145
        traceBuilder.append(seconds);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   146
        traceBuilder.append(',');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   147
        long millis = time % 1000;
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   148
        if (millis < 100) traceBuilder.append('0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   149
        if (millis < 10) traceBuilder.append('0');
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   150
        traceBuilder.append(millis);
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   151
        return traceBuilder.toString();
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   152
    }
96
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   153
    
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   154
	/**
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   155
	 * Convenience method which returns the unique identifier of this plugin.
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   156
	 */
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   157
	public static String getUniqueIdentifier() {
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   158
		if (getDefault() == null) {
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   159
			// If the default instance is not yet initialized,
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   160
			// return a static identifier. This identifier must
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   161
			// match the plugin id defined in plugin.xml
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   162
			return PLUGIN_ID;
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   163
		}
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   164
		return getDefault().getBundle().getSymbolicName();
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   165
	}
defec9d2b40a refactor to use AbstractCLaunchDelegate (bug 9762)
timkelly
parents: 37
diff changeset
   166
37
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   167
c2bce6dd59e7 add cdt_6_0_x
cawthron
parents:
diff changeset
   168
}