debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/AttachLaunchDelegate.java
changeset 0 fb279309251b
child 397 5354388b2a6b
child 404 d40ec9228b9c
equal deleted inserted replaced
-1:000000000000 0:fb279309251b
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of the License "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 package com.nokia.cdt.internal.debug.launch;
       
    18 
       
    19 import org.eclipse.cdt.core.IBinaryParser.IBinaryObject;
       
    20 import org.eclipse.cdt.core.model.ICProject;
       
    21 import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants;
       
    22 import org.eclipse.cdt.debug.core.ICDebugConfiguration;
       
    23 import org.eclipse.cdt.debug.core.cdi.ICDISession;
       
    24 import org.eclipse.cdt.launch.internal.ui.LaunchMessages;
       
    25 import org.eclipse.core.runtime.CoreException;
       
    26 import org.eclipse.core.runtime.IPath;
       
    27 import org.eclipse.core.runtime.IProgressMonitor;
       
    28 import org.eclipse.core.runtime.IStatus;
       
    29 import org.eclipse.core.runtime.NullProgressMonitor;
       
    30 import org.eclipse.core.runtime.Path;
       
    31 import org.eclipse.core.runtime.Status;
       
    32 import org.eclipse.core.runtime.SubProgressMonitor;
       
    33 import org.eclipse.debug.core.DebugException;
       
    34 import org.eclipse.debug.core.ILaunch;
       
    35 import org.eclipse.debug.core.ILaunchConfiguration;
       
    36 import org.eclipse.debug.core.ILaunchManager;
       
    37 import org.eclipse.swt.widgets.Display;
       
    38 
       
    39 import com.freescale.cdt.debug.cw.core.RemoteConnectionsTRKHelper;
       
    40 import com.freescale.cdt.debug.cw.core.cdi.Session;
       
    41 import com.freescale.cdt.debug.cw.core.os.OSProcess;
       
    42 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    43 import com.nokia.cdt.debug.cw.symbian.SettingsData;
       
    44 import com.nokia.cdt.debug.cw.symbian.SymbianPlugin;
       
    45 import com.nokia.cdt.internal.debug.launch.ui.ChooseProcessDialog;
       
    46 
       
    47 import cwdbg.PreferenceConstants;
       
    48 
       
    49 public class AttachLaunchDelegate extends TRKLaunchDelegate {
       
    50 
       
    51 	private OSProcess attachTarget;
       
    52 
       
    53 	@Override
       
    54 	public void launch(ILaunchConfiguration config, String mode,
       
    55 			ILaunch launch, IProgressMonitor monitor) throws CoreException {
       
    56 		// See comment at definition of the "mutex" for why this "synchronized".
       
    57 		synchronized(Session.sessionStartStopMutex()) {
       
    58 
       
    59 			if (monitor == null) {
       
    60 				monitor = new NullProgressMonitor();
       
    61 			}
       
    62 
       
    63 	        monitor.beginTask(LaunchMessages.getString("LocalRunLaunchDelegate.Launching_Local_C_Application"), 10); //$NON-NLS-1$
       
    64 			// check for cancellation
       
    65 			if (monitor.isCanceled()) {
       
    66 				return;
       
    67 			}
       
    68 
       
    69 	        if (!RemoteConnectionsTRKHelper.configUsesConnectionAttribute(config)) {
       
    70 	        	config = RemoteConnectionsTRKHelper.attemptUpdateLaunchConfiguration(config.getWorkingCopy());
       
    71 	        }
       
    72 	        
       
    73 	        final IConnection connection = RemoteConnectionsTRKHelper.getConnectionFromConfig(config);
       
    74 			if (connection == null) {
       
    75 				IStatus status = new Status(Status.ERROR, LaunchPlugin.PLUGIN_ID, 
       
    76 					LaunchMessages.getString("TRKLaunchDelegate.NoConnectionErrorMsg")); //$NON-NLS-1$
       
    77 				throw new DebugException(status);
       
    78 			}
       
    79 			connection.useConnection(true);
       
    80 
       
    81 			try {
       
    82 	        	addBeingLaunched(config); // indicating the LC is being launched
       
    83 	        	
       
    84 				monitor.worked(1);
       
    85 				IPath exePath = verifyProgramPath(config);
       
    86 				ICProject project = verifyCProject(config);
       
    87 				IBinaryObject exeFile = verifyBinary(project, exePath);
       
    88 
       
    89 				// See comment for this method for more.
       
    90 	            SettingsData.setInternalPreferences(config, SettingsData.LaunchConfig_AppTRK);
       
    91 	            			
       
    92 				// set the default source locator if required
       
    93 				setDefaultSourceLocator(launch, config);
       
    94 
       
    95 				if (mode.equals(ILaunchManager.DEBUG_MODE)) {
       
    96 					// debug mode
       
    97 					ICDebugConfiguration debugConfig = getDebugConfig(config);
       
    98 					ICDISession dsession = null;
       
    99 	                String debugMode = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE,
       
   100 									ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN);
       
   101 	                if (debugMode.equals(ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN)) {
       
   102 	                    dsession = debugConfig.createDebugger().createDebuggerSession(launch, exeFile,
       
   103 										new SubProgressMonitor(monitor, 8));
       
   104 
       
   105 						assert(dsession instanceof Session);
       
   106 						Session cwDebugSession = (Session)dsession;
       
   107 
       
   108 						doAdditionalSessionSetup(cwDebugSession);
       
   109 						
       
   110 						IPath[] otherExecutables = getOtherExecutables(project, new Path(""), config, monitor);
       
   111 						{
       
   112 							try {
       
   113 								monitor.worked(1);
       
   114 
       
   115 								// if enabled in the prefs, show the console view(s)
       
   116 								if (config.getAttribute(PreferenceConstants.J_PN_ViewUnframedData, false)) {
       
   117 									SymbianPlugin.getDefault().openUnframedDataConsole(true);
       
   118 								}
       
   119 
       
   120 								if (config.getAttribute(PreferenceConstants.J_PN_ViewCommMessages, false)) {
       
   121 									SymbianPlugin.getDefault().openTRKCommLogConsole(true);
       
   122 								}
       
   123 
       
   124 								config = synchronizeWithProjectAccessPaths(project, config);
       
   125 
       
   126 								// Connect to device
       
   127 								cwDebugSession.connectRemote(monitor);
       
   128 
       
   129 								// get processes on the device
       
   130 								OSProcess[] processesOnTarget = cwDebugSession.getTargetProcesses();
       
   131 								
       
   132 								// Ask user to choose a process
       
   133 								String defaultProcessName = exeFile.getPath().removeFileExtension().lastSegment();
       
   134 								OSProcess attachTarget = chooseProcessTarget(processesOnTarget, defaultProcessName);
       
   135 								
       
   136 								if (attachTarget == null) {
       
   137 									this.cancel(LaunchMessages.getString("LocalAttachLaunchDelegate.No_Process_ID_selected"), 0); //$NON-NLS-1$
       
   138 								} else {
       
   139 									String processName = attachTarget.parseProcess().getProcessName();
       
   140 									
       
   141 									for (IPath oExePath : otherExecutables) {								
       
   142 										if (oExePath.lastSegment().startsWith(processName))
       
   143 											exePath = oExePath;									
       
   144 									}
       
   145 									
       
   146 									exeFile = verifyBinary(project, exePath);
       
   147 
       
   148 									cwDebugSession.attachToProcess(
       
   149 											attachTarget, launch, config,
       
   150 											exeFile, otherExecutables, monitor,
       
   151 											project,
       
   152 											getTargetLabel(exeFile.getName()));
       
   153 								}
       
   154 
       
   155 							} catch (CoreException e) {
       
   156 								Session session = (Session)dsession;
       
   157 								session.cleanupAfterLaunchFailure();
       
   158 								throw e;
       
   159 							} catch (Exception e) {
       
   160 								Session session = (Session)dsession;
       
   161 								session.debuggingStopped(null);
       
   162 								this.abort(e.getLocalizedMessage(), null, 0);
       
   163 							}
       
   164 						}
       
   165 					}
       
   166 				} else if (mode.equals(ILaunchManager.RUN_MODE)) {
       
   167 					// run mode not supported for attach
       
   168 				}
       
   169 			} catch (CoreException e) {
       
   170 	      		connection.useConnection(false);
       
   171 	        	if (! monitor.isCanceled()) // don't throw on user cancellation
       
   172 					throw e;
       
   173 			} finally {
       
   174 				monitor.done();
       
   175 	            removeBeingLaunched(config);
       
   176 			}
       
   177 		} // end of synchronized.
       
   178 	}
       
   179 
       
   180 	private OSProcess chooseProcessTarget(final OSProcess[] processesOnTarget, final String defaultProcessName) {
       
   181 		attachTarget = null;
       
   182 
       
   183 		Display.getDefault().syncExec(new Runnable() {
       
   184 
       
   185 			public void run() {
       
   186 				ChooseProcessDialog dialog = new ChooseProcessDialog(
       
   187 						processesOnTarget, defaultProcessName, Display.getDefault().getActiveShell());
       
   188 				int dialogResult = dialog.open();
       
   189 
       
   190 				if (dialogResult == org.eclipse.jface.dialogs.Dialog.OK) {
       
   191 					attachTarget = dialog.getSelectedProcess();
       
   192 				}
       
   193 			}
       
   194 		});
       
   195 
       
   196 		return attachTarget;
       
   197 	}
       
   198 }