From 5654112209d440469e40660c946ffdee3daa938f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torbj=C3=B6rn=20Svensson?= Date: Mon, 1 Mar 2021 18:36:28 +0100 Subject: [PATCH] Bug 572759: Allow debugging binaries with project relative path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In some situations, it makes sense to have a build structure parallel with the source tree, and it this case, the build results may not be part of the resources visible in the Eclipse workspace. Current implementation allows absolute paths to the binary to debug. While it works, it's a cumbersome way to handle the above situation. By resolving the relative path outside of Eclipse scope allows to point to files that are not part of the Eclipse workspace, allthough the path is relative to a project in the workspace. Contributed by STMicroelectronics Change-Id: I284a5dad61e692dae4029e5f142d23d8cda98ed0 Signed-off-by: Torbjörn Svensson --- .../META-INF/MANIFEST.MF | 2 +- .../cdt/dsf/gdb/launching/LaunchUtils.java | 11 +++--- .../META-INF/MANIFEST.MF | 2 +- .../cdt/launch/AbstractCLaunchDelegate2.java | 10 ++--- .../org/eclipse/cdt/launch/LaunchUtils.java | 38 ++++++++++++++++--- .../org/eclipse/cdt/launch/ui/CMainTab2.java | 9 ++++- 6 files changed, 52 insertions(+), 20 deletions(-) diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/META-INF/MANIFEST.MF b/dsf-gdb/org.eclipse.cdt.dsf.gdb/META-INF/MANIFEST.MF index bc10f86d201..4e421bc64c9 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/META-INF/MANIFEST.MF +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/META-INF/MANIFEST.MF @@ -12,7 +12,7 @@ Require-Bundle: org.eclipse.core.runtime, org.eclipse.cdt.core, org.eclipse.cdt.debug.core, org.eclipse.core.variables, - org.eclipse.cdt.launch;bundle-version="9.3.0", + org.eclipse.cdt.launch;bundle-version="10.3.0", org.eclipse.cdt.gdb;bundle-version="7.0.0", org.eclipse.core.resources, org.eclipse.launchbar.core;bundle-version="2.0.0";visibility:=reexport, diff --git a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java index ecee91644c3..4ba7534fecf 100644 --- a/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java +++ b/dsf-gdb/org.eclipse.cdt.dsf.gdb/src/org/eclipse/cdt/dsf/gdb/launching/LaunchUtils.java @@ -19,6 +19,7 @@ package org.eclipse.cdt.dsf.gdb.launching; import java.io.BufferedReader; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; @@ -52,7 +53,6 @@ import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin; import org.eclipse.cdt.dsf.gdb.service.SessionType; import org.eclipse.cdt.utils.CommandLineUtil; import org.eclipse.cdt.utils.spawner.ProcessFactory; -import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -120,13 +120,12 @@ public class LaunchUtils { ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST); } - if (!programPath.isAbsolute() && cproject != null) { - // Find the specified program within the specified project - IFile wsProgramPath = cproject.getProject().getFile(programPath); - programPath = wsProgramPath.getLocation(); + if (cproject != null) { + programPath = org.eclipse.cdt.launch.LaunchUtils.toAbsoluteProgramPath(cproject.getProject(), programPath); } - if (!programPath.toFile().exists()) { + File executable = programPath.toFile(); + if (!executable.exists() || !executable.isFile()) { abort(LaunchMessages.getString("AbstractCLaunchDelegate.Program_file_does_not_exist"), //$NON-NLS-1$ new FileNotFoundException( LaunchMessages.getFormattedString("AbstractCLaunchDelegate.PROGRAM_PATH_not_found", //$NON-NLS-1$ diff --git a/launch/org.eclipse.cdt.launch/META-INF/MANIFEST.MF b/launch/org.eclipse.cdt.launch/META-INF/MANIFEST.MF index c31055a9407..c6667667825 100644 --- a/launch/org.eclipse.cdt.launch/META-INF/MANIFEST.MF +++ b/launch/org.eclipse.cdt.launch/META-INF/MANIFEST.MF @@ -2,7 +2,7 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: %pluginName Bundle-SymbolicName: org.eclipse.cdt.launch; singleton:=true -Bundle-Version: 10.2.200.qualifier +Bundle-Version: 10.3.0.qualifier Bundle-Activator: org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin Bundle-Vendor: %providerName Bundle-Localization: plugin diff --git a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/AbstractCLaunchDelegate2.java b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/AbstractCLaunchDelegate2.java index 721927ab270..8cc28c210dc 100644 --- a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/AbstractCLaunchDelegate2.java +++ b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/AbstractCLaunchDelegate2.java @@ -39,7 +39,6 @@ import org.eclipse.cdt.launch.internal.ui.LaunchMessages; import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin; import org.eclipse.core.resources.ICommand; import org.eclipse.core.resources.IContainer; -import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IResource; @@ -692,13 +691,12 @@ public abstract class AbstractCLaunchDelegate2 extends LaunchConfigurationDelega ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST); } - if (!programPath.isAbsolute() && cproject != null) { - // Find the specified program within the specified project - IFile wsProgramPath = cproject.getProject().getFile(programPath); - programPath = wsProgramPath.getLocation(); + if (cproject != null) { + programPath = LaunchUtils.toAbsoluteProgramPath(cproject.getProject(), programPath); } - if (!programPath.toFile().exists()) { + File executable = programPath.toFile(); + if (!executable.exists() || !executable.isFile()) { abort(LaunchMessages.AbstractCLaunchDelegate_Program_file_does_not_exist, new FileNotFoundException( NLS.bind(LaunchMessages.AbstractCLaunchDelegate_PROGRAM_PATH_not_found, programPath.toOSString())), ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST); diff --git a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/LaunchUtils.java b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/LaunchUtils.java index e90b1be21cb..53afc404862 100644 --- a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/LaunchUtils.java +++ b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/LaunchUtils.java @@ -15,6 +15,7 @@ *******************************************************************************/ package org.eclipse.cdt.launch; +import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashSet; @@ -152,12 +153,11 @@ public class LaunchUtils { IProject project = getProject(configuration); ICProject cproject = CCorePlugin.getDefault().getCoreModel().create(project); if (cproject != null) { - // Find the specified program within the specified project - IFile wsProgramPath = cproject.getProject().getFile(programPath); - programPath = wsProgramPath.getLocation(); + programPath = toAbsoluteProgramPath(cproject.getProject(), programPath); } } - if (!programPath.toFile().exists()) { + File executable = programPath.toFile(); + if (!executable.exists() || !executable.isFile()) { throwException(Messages.LaunchUtils_program_file_does_not_exist, new FileNotFoundException( MessageFormat.format(Messages.LaunchUtils__0_not_found, programPath.toOSString())), @@ -167,6 +167,33 @@ public class LaunchUtils { return programPath.toOSString(); } + /** + * Return the program path, resolved as an absolute IPath. + * + * @param project Project to resolve relative paths to + * @param programPath The program path to resolve + * @return the program path + * @since 10.3 + */ + public static IPath toAbsoluteProgramPath(IProject project, IPath programPath) { + if (project == null || programPath == null || programPath.isAbsolute()) { + return programPath; + } + + // Find the specified program within the specified project + try { + IPath resolvedPath = project.getFile(programPath).getLocation(); + if (resolvedPath != null && resolvedPath.isAbsolute() && resolvedPath.toFile().exists()) { + return resolvedPath; + } + } catch (IllegalArgumentException e) { + } + + // Find the specified program relative to the specified project + IPath projectPath = project.getLocation(); + return projectPath.append(programPath).makeAbsolute(); + } + /** * Return project or null if project is not accessible or not specified. * @param configuration Launch configuration to obtain project from @@ -322,7 +349,8 @@ public class LaunchUtils { if (dirLocation == null) continue; for (IFile file : files) { - if (dirLocation.isPrefixOf(file.getLocation())) { + IPath location = file.getLocation(); + if (location != null && dirLocation.isPrefixOf(location)) { if (buildConfig != null && buildConfig != cfgDes) { // Matched more than one, so use the active configuration buildConfig = null; diff --git a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CMainTab2.java b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CMainTab2.java index bc9e0bf01ed..44c90a15bdf 100644 --- a/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CMainTab2.java +++ b/launch/org.eclipse.cdt.launch/src/org/eclipse/cdt/launch/ui/CMainTab2.java @@ -28,6 +28,7 @@ import org.eclipse.cdt.core.model.ICElement; import org.eclipse.cdt.core.model.ICProject; import org.eclipse.cdt.core.settings.model.ICProjectDescription; import org.eclipse.cdt.debug.core.ICDTLaunchConfigurationConstants; +import org.eclipse.cdt.launch.LaunchUtils; import org.eclipse.cdt.launch.internal.ui.LaunchImages; import org.eclipse.cdt.launch.internal.ui.LaunchMessages; import org.eclipse.cdt.launch.internal.ui.LaunchUIPlugin; @@ -475,10 +476,16 @@ public class CMainTab2 extends CAbstractMainTab { setErrorMessage(LaunchMessages.CMainTab_Project_must_be_opened); return false; } - if (!project.getFile(programName).exists()) { + exePath = LaunchUtils.toAbsoluteProgramPath(project, exePath); + File executable = exePath.toFile(); + if (!executable.exists()) { setErrorMessage(LaunchMessages.CMainTab_Program_does_not_exist); return false; } + if (!executable.isFile()) { + setErrorMessage(LaunchMessages.CMainTab_Selection_must_be_file); + return false; + } } // Notice that we don't check if exePath points to a valid executable since such // check is too expensive to be done on the UI thread.