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