debuggercdi/com.nokia.cdt.debug.launch/src/com/nokia/cdt/internal/debug/launch/AttachLaunchDelegate.java
branchC3_BUILDER_WORK
changeset 1863 0142fe025ce6
parent 1862 e3bac873e5c8
parent 1860 63d0f74e878e
child 1871 4d38e1ac1cff
equal deleted inserted replaced
1862:e3bac873e5c8 1863:0142fe025ce6
     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.ensureConnectionFromConfig(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 					hookConnectionStatus();
       
    97 					// debug mode
       
    98 					ICDebugConfiguration debugConfig = getDebugConfig(config);
       
    99 					ICDISession dsession = null;
       
   100 	                String debugMode = config.getAttribute(ICDTLaunchConfigurationConstants.ATTR_DEBUGGER_START_MODE,
       
   101 									ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN);
       
   102 	                if (debugMode.equals(ICDTLaunchConfigurationConstants.DEBUGGER_MODE_RUN)) {
       
   103 	                    dsession = debugConfig.createDebugger().createDebuggerSession(launch, exeFile,
       
   104 										new SubProgressMonitor(monitor, 8));
       
   105 
       
   106 						assert(dsession instanceof Session);
       
   107 						Session cwDebugSession = (Session)dsession;
       
   108 
       
   109 						doAdditionalSessionSetup(cwDebugSession);
       
   110 						
       
   111 						IPath[] otherExecutables = getOtherExecutables(project, new Path(""), config, monitor);
       
   112 						{
       
   113 							try {
       
   114 								monitor.worked(1);
       
   115 
       
   116 								// if enabled in the prefs, show the console view(s)
       
   117 								if (config.getAttribute(PreferenceConstants.J_PN_ViewUnframedData, false)) {
       
   118 									SymbianPlugin.getDefault().openUnframedDataConsole(true);
       
   119 								}
       
   120 
       
   121 								if (config.getAttribute(PreferenceConstants.J_PN_ViewCommMessages, false)) {
       
   122 									SymbianPlugin.getDefault().openTRKCommLogConsole(true);
       
   123 								}
       
   124 
       
   125 								config = synchronizeWithProjectAccessPaths(project, config);
       
   126 
       
   127 								// Connect to device
       
   128 								cwDebugSession.connectRemote(monitor);
       
   129 
       
   130 								// get processes on the device
       
   131 								OSProcess[] processesOnTarget = cwDebugSession.getTargetProcesses();
       
   132 								
       
   133 								// Ask user to choose a process
       
   134 								String defaultProcessName = exeFile.getPath().removeFileExtension().lastSegment();
       
   135 								OSProcess attachTarget = null;
       
   136 								String choosenProcessTarget = config.getAttribute(SettingsData.AttachToProcessDialog_Selection, "");
       
   137 								if (choosenProcessTarget.length() > 0) {
       
   138 									attachTarget = chooseProcessTargetNoUI(processesOnTarget, choosenProcessTarget);
       
   139 								} else {
       
   140 									attachTarget = chooseProcessTarget(processesOnTarget, defaultProcessName);									
       
   141 								}
       
   142 								
       
   143 								if (attachTarget == null) {
       
   144 									this.cancel(LaunchMessages.getString("LocalAttachLaunchDelegate.No_Process_ID_selected"), 0); //$NON-NLS-1$
       
   145 								} else {
       
   146 									String processName = attachTarget.parseProcess().getProcessName();
       
   147 									
       
   148 									for (IPath oExePath : otherExecutables) {								
       
   149 										if (oExePath.lastSegment().startsWith(processName))
       
   150 											exePath = oExePath;									
       
   151 									}
       
   152 									
       
   153 									exeFile = verifyBinary(project, exePath);
       
   154 
       
   155 									cwDebugSession.attachToProcess(
       
   156 											attachTarget, launch, config,
       
   157 											exeFile, otherExecutables, monitor,
       
   158 											project,
       
   159 											getTargetLabel(exeFile.getName()));
       
   160 								}
       
   161 
       
   162 							} catch (CoreException e) {
       
   163 								Session session = (Session)dsession;
       
   164 								session.cleanupAfterLaunchFailure();
       
   165 								throw e;
       
   166 							} catch (Exception e) {
       
   167 								Session session = (Session)dsession;
       
   168 								session.debuggingStopped(null);
       
   169 								this.abort(e.getLocalizedMessage(), null, 0);
       
   170 							}
       
   171 						}
       
   172 					}
       
   173 	    			hookSessionEnded(launch);
       
   174 				} else if (mode.equals(ILaunchManager.RUN_MODE)) {
       
   175 					// run mode not supported for attach
       
   176 				}
       
   177 			} catch (CoreException e) {
       
   178 	      		connection.useConnection(false);
       
   179 	      		unhookConnectionStatus();
       
   180 	        	if (! monitor.isCanceled()) // don't throw on user cancellation
       
   181 					throw e;
       
   182 			} finally {
       
   183 				monitor.done();
       
   184 	            removeBeingLaunched(config);
       
   185 			}
       
   186 		} // end of synchronized.
       
   187 	}
       
   188 
       
   189 	private OSProcess chooseProcessTarget(final OSProcess[] processesOnTarget, final String defaultProcessName) {
       
   190 		attachTarget = null;
       
   191 
       
   192 		Display.getDefault().syncExec(new Runnable() {
       
   193 
       
   194 			public void run() {
       
   195 				ChooseProcessDialog dialog = new ChooseProcessDialog(
       
   196 						processesOnTarget, defaultProcessName, Display.getDefault().getActiveShell());
       
   197 				int dialogResult = dialog.open();
       
   198 
       
   199 				if (dialogResult == org.eclipse.jface.dialogs.Dialog.OK) {
       
   200 					attachTarget = dialog.getSelectedProcess();
       
   201 				}
       
   202 			}
       
   203 		});
       
   204 
       
   205 		return attachTarget;
       
   206 	}
       
   207 	
       
   208 	private OSProcess chooseProcessTargetNoUI(final OSProcess[] processesOnTarget, final String choosenProcessName) {
       
   209 		attachTarget = null;
       
   210 
       
   211 		for (OSProcess process : processesOnTarget) {
       
   212 			if (process.parseProcess().getProcessName().equals(choosenProcessName)) {
       
   213 				attachTarget = process;	
       
   214 				break;
       
   215 			}
       
   216 		}
       
   217 
       
   218 		return attachTarget;
       
   219 	}
       
   220 }