Skip to content

Commit

Permalink
Bug 572759: Allow debugging binaries with project relative path
Browse files Browse the repository at this point in the history
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 <torbjorn.svensson@st.com>
  • Loading branch information
Torbjorn-Svensson committed Apr 11, 2021
1 parent 450e0ca commit 5654112
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion dsf-gdb/org.eclipse.cdt.dsf.gdb/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -52,7 +53,6 @@
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;
Expand Down Expand Up @@ -120,13 +120,12 @@ public static IPath verifyProgramPath(ILaunchConfiguration configuration, ICProj
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$
Expand Down
2 changes: 1 addition & 1 deletion launch/org.eclipse.cdt.launch/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
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;
Expand Down Expand Up @@ -692,13 +691,12 @@ protected IPath verifyProgramPath(ILaunchConfiguration configuration, ICProject
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -152,12 +153,11 @@ public static String resolveProgramPath(ILaunchConfiguration configuration, Stri
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())),
Expand All @@ -167,6 +167,33 @@ public static String resolveProgramPath(ILaunchConfiguration configuration, Stri
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 <code>null</code> if project is not accessible or not specified.
* @param configuration Launch configuration to obtain project from
Expand Down Expand Up @@ -322,7 +349,8 @@ public static ICConfigurationDescription getBuildConfigByProgramPath(IProject pr
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
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;
Expand Down Expand Up @@ -475,10 +476,16 @@ public boolean isValid(ILaunchConfiguration config) {
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.
Expand Down

0 comments on commit 5654112

Please sign in to comment.