1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 228265

Cleanup to have an internal utility class
This commit is contained in:
Marc Khouzam 2008-04-28 19:40:06 +00:00
parent 34314195bd
commit dc95d5a7f7

View file

@ -100,12 +100,13 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
monitor.subTask( "Debugging local C/C++ application" ); //$NON-NLS-1$
}
IPath exePath = verifyProgramPath( config );
ICProject project = verifyCProject( config );
if ( exePath != null ) {
verifyBinary( project, exePath );
}
// First verify we are dealing with a proper project.
ICProject project = LaunchUtils.verifyCProject(config);
// Now verify we know the program to debug.
IPath exePath = LaunchUtils.verifyProgramPath(config, project);
// Finally, make sure the program is a proper binary.
LaunchUtils.verifyBinary(config, exePath);
monitor.worked( 1 );
// If we are attaching, get the process id now, so as to avoid starting the launch
@ -183,7 +184,6 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
return SessionType.RUN;
}
// Copied from the CDT
protected int promptForProcessID(ILaunchConfiguration config) throws CoreException {
IStatus fPromptStatus = new Status(IStatus.INFO, "org.eclipse.debug.ui", 200, "", null); //$NON-NLS-1$//$NON-NLS-2$
IStatus processPrompt = new Status(IStatus.INFO, "org.eclipse.cdt.launch", 100, "", null); //$NON-NLS-1$//$NON-NLS-2$
@ -199,11 +199,11 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
}
@Override
public boolean preLaunchCheck( ILaunchConfiguration config, String mode, IProgressMonitor monitor ) throws CoreException {
public boolean preLaunchCheck(ILaunchConfiguration config, String mode, IProgressMonitor monitor) throws CoreException {
// no pre launch check for core file
if (mode.equals(ILaunchManager.DEBUG_MODE) && getSessionType(config) == SessionType.CORE) return true;
return super.preLaunchCheck( config, mode, monitor );
return super.preLaunchCheck(config, mode, monitor);
}
@Override
@ -238,135 +238,6 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
return null;
}
/**
* Throws a core exception with an error status object built from the given
* message, lower level exception, and error code.
*
* @param message
* the status message
* @param exception
* lower level exception associated with the error, or
* <code>null</code> if none
* @param code
* error code
*/
protected void abort(String message, Throwable exception, int code) throws CoreException {
MultiStatus status = new MultiStatus(GdbPlugin.PLUGIN_ID, code, message, exception);
status.add(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, code, exception == null ? "" : exception.getLocalizedMessage(), //$NON-NLS-1$
exception));
throw new CoreException(status);
}
public static ICProject getCProject(ILaunchConfiguration configuration) throws CoreException {
String projectName = getProjectName(configuration);
if (projectName != null) {
projectName = projectName.trim();
if (projectName.length() > 0) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
ICProject cProject = CCorePlugin.getDefault().getCoreModel().create(project);
if (cProject != null && cProject.exists()) {
return cProject;
}
}
}
return null;
}
public static String getProjectName(ILaunchConfiguration configuration) throws CoreException {
return configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null);
}
public static String getProgramName(ILaunchConfiguration configuration) throws CoreException {
return configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, (String)null);
}
public static IPath getProgramPath(ILaunchConfiguration configuration) throws CoreException {
String path = getProgramName(configuration);
if (path == null) {
return null;
}
return new Path(path);
}
protected ICProject verifyCProject(ILaunchConfiguration config) throws CoreException {
String name = getProjectName(config);
if (name == null) {
abort(LaunchMessages.getString("AbstractCLaunchDelegate.C_Project_not_specified"), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_UNSPECIFIED_PROJECT);
}
ICProject cproject = getCProject(config);
if (cproject == null) {
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!proj.exists()) {
abort(
LaunchMessages.getFormattedString("AbstractCLaunchDelegate.Project_NAME_does_not_exist", name), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
} else if (!proj.isOpen()) {
abort(LaunchMessages.getFormattedString("AbstractCLaunchDelegate.Project_NAME_is_closed", name), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
}
abort(LaunchMessages.getString("AbstractCLaunchDelegate.Not_a_C_CPP_project"), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
}
return cproject;
}
protected IPath verifyProgramPath(ILaunchConfiguration config) throws CoreException {
ICProject cproject = verifyCProject(config);
IPath programPath = getProgramPath(config);
if (programPath == null || programPath.isEmpty()) {
return null;
}
if (!programPath.isAbsolute()) {
IFile wsProgramPath = cproject.getProject().getFile(programPath);
programPath = wsProgramPath.getLocation();
}
if (!programPath.toFile().exists()) {
abort(
LaunchMessages.getString("AbstractCLaunchDelegate.Program_file_does_not_exist"), //$NON-NLS-1$
new FileNotFoundException(
LaunchMessages.getFormattedString(
"AbstractCLaunchDelegate.PROGRAM_PATH_not_found", programPath.toOSString())), //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
}
return programPath;
}
/**
* @param project
* @param exePath
* @return
* @throws CoreException
*/
protected IBinaryObject verifyBinary(ICProject proj, IPath exePath) throws CoreException {
ICExtensionReference[] parserRef = CCorePlugin.getDefault().getBinaryParserExtensions(proj.getProject());
for (int i = 0; i < parserRef.length; i++) {
try {
IBinaryParser parser = (IBinaryParser)parserRef[i].createExtension();
IBinaryObject exe = (IBinaryObject)parser.getBinary(exePath);
if (exe != null) {
return exe;
}
} catch (ClassCastException e) {
} catch (IOException e) {
}
}
IBinaryParser parser = CCorePlugin.getDefault().getDefaultBinaryParser();
try {
return (IBinaryObject)parser.getBinary(exePath);
} catch (ClassCastException e) {
} catch (IOException e) {
}
Throwable exception = new FileNotFoundException(LaunchMessages.getFormattedString(
"AbstractCLaunchDelegate.Program_is_not_a_recongnized_executable", exePath.toOSString())); //$NON-NLS-1$
int code = ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_BINARY;
MultiStatus status = new MultiStatus(GdbPlugin.PLUGIN_ID, code,
LaunchMessages.getString("AbstractCLaunchDelegate.Program_is_not_a_recongnized_executable"), exception); //$NON-NLS-1$
status.add(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, code, exception == null ? "" : exception.getLocalizedMessage(), //$NON-NLS-1$
exception));
throw new CoreException(status);
}
/**
* Recursively creates a set of projects referenced by the current project
*
@ -381,7 +252,7 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
private HashSet<IProject> getReferencedProjectSet(IProject proj, HashSet<IProject> referencedProjSet) throws CoreException {
// The top project is a reference too and it must be added at the top to avoid cycles
referencedProjSet.add(proj);
IProject[] projects = proj.getReferencedProjects();
for (IProject refProject : projects) {
if (refProject.exists() && !referencedProjSet.contains(refProject)) {
@ -400,7 +271,7 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
IProject[] orderedProjects = null;
ArrayList<IProject> orderedProjList = null;
ICProject cProject = getCProject(configuration);
ICProject cProject = LaunchUtils.verifyCProject(configuration);
if (cProject != null) {
HashSet<IProject> projectSet = getReferencedProjectSet(cProject.getProject(), new HashSet<IProject>());
@ -420,7 +291,7 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
}
}
}
// Add any remaining projects to the end of the list
orderedProjList.addAll(unorderedProjects);
@ -439,7 +310,7 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
protected IProject[] getProjectsForProblemSearch(ILaunchConfiguration configuration, String mode) throws CoreException {
return getBuildOrder(configuration, mode);
}
/**
* Searches for compile errors in the specified project
* Used in finalLaunchCheck()
@ -447,7 +318,6 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
* The project to search
* @return true if compile errors exist, otherwise false
*/
@Override
protected boolean existsProblems(IProject proj) throws CoreException {
IMarker[] markers = proj.findMarkers(ICModelMarker.C_MODEL_PROBLEM_MARKER, true, IResource.DEPTH_INFINITE);
@ -461,4 +331,152 @@ public class GdbLaunchDelegate extends LaunchConfigurationDelegate
}
return false;
}
private static class LaunchUtils {
/**
* Verify the following things about the project:
* - is a valid project name given
* - does the project exist
* - is the project open
* - is the project a C/C++ project
*/
public static ICProject verifyCProject(ILaunchConfiguration configuration) throws CoreException {
String name = getProjectName(configuration);
if (name == null) {
abort(LaunchMessages.getString("AbstractCLaunchDelegate.C_Project_not_specified"), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_UNSPECIFIED_PROJECT);
}
ICProject cproject = getCProject(configuration);
if (cproject == null) {
IProject proj = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
if (!proj.exists()) {
abort(
LaunchMessages.getFormattedString("AbstractCLaunchDelegate.Project_NAME_does_not_exist", name), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
} else if (!proj.isOpen()) {
abort(LaunchMessages.getFormattedString("AbstractCLaunchDelegate.Project_NAME_is_closed", name), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
}
abort(LaunchMessages.getString("AbstractCLaunchDelegate.Not_a_C_CPP_project"), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
}
return cproject;
}
/**
* Verify that program name of the configuration can be found as a file.
*
* @return Absolute path of the program location
*/
public static IPath verifyProgramPath(ILaunchConfiguration configuration, ICProject cproject) throws CoreException {
String programName = configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROGRAM_NAME, (String)null);
if (programName == null) {
abort(LaunchMessages.getString("AbstractCLaunchDelegate.Program_file_not_specified"), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
}
IPath programPath = new Path(programName);
if (programPath.isEmpty()) {
abort(LaunchMessages.getString("AbstractCLaunchDelegate.Program_file_does_not_exist"), null, //$NON-NLS-1$
ICDTLaunchConfigurationConstants.ERR_NOT_A_C_PROJECT);
}
if (!programPath.isAbsolute()) {
// Find the specified program within the specified project
IFile wsProgramPath = cproject.getProject().getFile(programPath);
programPath = wsProgramPath.getLocation();
}
if (!programPath.toFile().exists()) {
abort(LaunchMessages.getString("AbstractCLaunchDelegate.Program_file_does_not_exist"), //$NON-NLS-1$
new FileNotFoundException(
LaunchMessages.getFormattedString("AbstractCLaunchDelegate.PROGRAM_PATH_not_found", //$NON-NLS-1$
programPath.toOSString())),
ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_EXIST);
}
return programPath;
}
/**
* Verify that the executable path points to a valid binary file.
*
* @return An object representing the binary file.
*/
public static IBinaryObject verifyBinary(ILaunchConfiguration configuration, IPath exePath) throws CoreException {
ICExtensionReference[] parserRefs = CCorePlugin.getDefault().getBinaryParserExtensions(getCProject(configuration).getProject());
for (ICExtensionReference parserRef : parserRefs) {
try {
IBinaryParser parser = (IBinaryParser)parserRef.createExtension();
IBinaryObject exe = (IBinaryObject)parser.getBinary(exePath);
if (exe != null) {
return exe;
}
} catch (ClassCastException e) {
} catch (IOException e) {
}
}
IBinaryParser parser = CCorePlugin.getDefault().getDefaultBinaryParser();
try {
return (IBinaryObject)parser.getBinary(exePath);
} catch (ClassCastException e) {
} catch (IOException e) {
}
abort(LaunchMessages.getString("AbstractCLaunchDelegate.Program_is_not_a_recognized_executable"), //$NON-NLS-1$
new FileNotFoundException(
LaunchMessages.getFormattedString("AbstractCLaunchDelegate.Program_is_not_a_recognized_executable", //$NON-NLS-1$
exePath.toOSString())),
ICDTLaunchConfigurationConstants.ERR_PROGRAM_NOT_BINARY);
return null;
}
/**
* Throws a core exception with an error status object built from the given
* message, lower level exception, and error code.
*
* @param message
* the status message
* @param exception
* lower level exception associated with the error, or
* <code>null</code> if none
* @param code
* error code
*/
private static void abort(String message, Throwable exception, int code) throws CoreException {
MultiStatus status = new MultiStatus(GdbPlugin.PLUGIN_ID, code, message, exception);
status.add(new Status(IStatus.ERROR, GdbPlugin.PLUGIN_ID, code,
exception == null ? "" : exception.getLocalizedMessage(), //$NON-NLS-1$
exception));
throw new CoreException(status);
}
/**
* Returns an ICProject based on the project name provided in the configuration.
* First look for a project by name, and then confirm it is a C/C++ project.
*/
private static ICProject getCProject(ILaunchConfiguration configuration) throws CoreException {
String projectName = getProjectName(configuration);
if (projectName != null) {
projectName = projectName.trim();
if (projectName.length() > 0) {
IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
ICProject cProject = CCorePlugin.getDefault().getCoreModel().create(project);
if (cProject != null && cProject.exists()) {
return cProject;
}
}
}
return null;
}
private static String getProjectName(ILaunchConfiguration configuration) throws CoreException {
return configuration.getAttribute(ICDTLaunchConfigurationConstants.ATTR_PROJECT_NAME, (String)null);
}
}
}