Skip to content

Commit c900e07

Browse files
committed
jlink & openocd: support project-less debug
- uses John Moule patches
1 parent 452c67a commit c900e07

File tree

6 files changed

+182
-14
lines changed

6 files changed

+182
-14
lines changed

ilg.gnuarmeclipse.debug.gdbjtag.jlink/src/ilg/gnuarmeclipse/debug/gdbjtag/jlink/Backend.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,7 @@ protected IStatus run(IProgressMonitor monitor) {
281281
return Status.OK_STATUS;
282282
}
283283
try {
284-
String projectName = fLaunchConfiguration.getAttribute(
285-
"org.eclipse.cdt.launch.PROJECT_ATTR", "");
286-
File dir = Utils.getProjectOsPath(projectName);
284+
File dir = Utils.getProjectOsPath(fLaunchConfiguration);
287285

288286
fServerProcess = launchGDBProcess(commandLineArray, dir);
289287
// Need to do this on the executor for thread-safety

ilg.gnuarmeclipse.debug.gdbjtag.jlink/src/ilg/gnuarmeclipse/debug/gdbjtag/jlink/Backend0.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -609,9 +609,7 @@ protected IStatus run(IProgressMonitor monitor) {
609609
.getGdbClientCommandLineArray(fLaunchConfiguration);
610610

611611
try {
612-
String projectName = fLaunchConfiguration.getAttribute(
613-
"org.eclipse.cdt.launch.PROJECT_ATTR", "");
614-
File dir = Utils.getProjectOsPath(projectName);
612+
File dir = Utils.getProjectOsPath(fLaunchConfiguration);
615613

616614
fProcess = launchGDBProcess(commandLineArray, dir);
617615
// Need to do this on the executor for thread-safety

ilg.gnuarmeclipse.debug.gdbjtag.jlink/src/ilg/gnuarmeclipse/debug/gdbjtag/jlink/Utils.java

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
2828
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
2929
import org.eclipse.cdt.dsf.gdb.launching.LaunchUtils;
30+
import org.eclipse.cdt.internal.core.envvar.EnvVarCollector;
31+
import org.eclipse.cdt.internal.core.envvar.EnvVarDescriptor;
32+
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
33+
import org.eclipse.cdt.internal.core.envvar.IEnvironmentContextInfo;
3034
import org.eclipse.cdt.utils.spawner.ProcessFactory;
3135
import org.eclipse.core.resources.IProject;
3236
import org.eclipse.core.resources.IWorkspace;
@@ -247,7 +251,7 @@ public static String[] getLaunchEnvironment(ILaunchConfiguration config)
247251

248252
ICConfigurationDescription cfg = getBuildConfig(config);
249253
if (cfg == null)
250-
return null;
254+
return getLaunchEnvironmentWithoutProject();
251255

252256
// Environment variables and inherited vars
253257
HashMap<String, String> envMap = new HashMap<String, String>();
@@ -364,4 +368,88 @@ public static File getProjectOsPath(String projectName) {
364368
return dir;
365369
}
366370

371+
/**
372+
* Gets working directory from either the location of the project, or if
373+
* project-less launching, from the location of the executable.
374+
*
375+
* @param configuration
376+
* @return Working directory
377+
* @throws CoreException
378+
*/
379+
public static File getProjectOsPath(ILaunchConfiguration configuration)
380+
throws CoreException {
381+
382+
IPath path = null;
383+
File dir = null;
384+
if (null != configuration) {
385+
String projectName = configuration.getAttribute(
386+
"org.eclipse.cdt.launch.PROJECT_ATTR", "");
387+
if (projectName.length() > 0) {
388+
IWorkspace workspace = ResourcesPlugin.getWorkspace();
389+
IProject project = workspace.getRoot().getProject(projectName);
390+
path = project.getLocation();
391+
} else {
392+
/*
393+
* If project-less launch then PROGRAM_NAME will be an absolute
394+
* path
395+
*/
396+
String executableName = configuration.getAttribute(
397+
"org.eclipse.cdt.launch.PROGRAM_NAME", "");
398+
path = new Path(executableName).removeLastSegments(1);
399+
}
400+
}
401+
if (null != path) {
402+
dir = new File(path.toOSString());
403+
}
404+
return dir;
405+
}
406+
407+
/**
408+
* Get environment from workspace. Useful when project-less launching when
409+
* there is no environment available from the configuration.
410+
*
411+
* @return String [] of environment variables in variable=value format.
412+
* @throws CoreException
413+
*/
414+
public static String[] getLaunchEnvironmentWithoutProject()
415+
throws CoreException {
416+
String[] retVal = null;
417+
IEnvironmentContextInfo contextInfo = EnvironmentVariableManager
418+
.getDefault().getContextInfo(null);
419+
EnvVarCollector envVarMergedColletion = EnvironmentVariableManager
420+
.getVariables(contextInfo, true);
421+
if (null != envVarMergedColletion) {
422+
EnvVarDescriptor envVars[] = envVarMergedColletion.toArray(false);
423+
if (envVars != null) {
424+
List<String> strings = new ArrayList<String>();
425+
for (int i = 0; i < envVars.length; i++) {
426+
IEnvironmentVariable resolved = EnvironmentVariableManager
427+
.getDefault().calculateResolvedVariable(envVars[i],
428+
contextInfo);
429+
if (null != resolved) {
430+
// The project_classpath variable contributed by JDT is
431+
// useless
432+
// for running C/C++
433+
// binaries, but it can be lethal if it has a very large
434+
// value
435+
// that exceeds shell
436+
// limit. See
437+
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=408522
438+
if (!"project_classpath".equals(resolved.getName())) {//$NON-NLS-1$
439+
StringBuffer buffer = new StringBuffer(
440+
resolved.getName());
441+
buffer.append('=').append(resolved.getValue());
442+
strings.add(buffer.toString());
443+
}
444+
}
445+
}
446+
retVal = strings.toArray(new String[strings.size()]);
447+
}
448+
} else {
449+
throw new CoreException(new Status(IStatus.ERROR,
450+
Activator.PLUGIN_ID,
451+
"Error retrieving workspace environment."));
452+
}
453+
return retVal;
454+
}
367455
}

ilg.gnuarmeclipse.debug.gdbjtag.openocd/src/ilg/gnuarmeclipse/debug/gdbjtag/openocd/Backend.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -278,9 +278,7 @@ protected IStatus run(IProgressMonitor monitor) {
278278
.getGdbServerCommandLineArray(fLaunchConfiguration);
279279

280280
try {
281-
String projectName = fLaunchConfiguration.getAttribute(
282-
"org.eclipse.cdt.launch.PROJECT_ATTR", "");
283-
File dir = Utils.getProjectOsPath(projectName);
281+
File dir = Utils.getProjectOsPath(fLaunchConfiguration);
284282

285283
fServerProcess = launchGDBProcess(commandLineArray, dir);
286284
// Need to do this on the executor for thread-safety

ilg.gnuarmeclipse.debug.gdbjtag.openocd/src/ilg/gnuarmeclipse/debug/gdbjtag/openocd/Backend0.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,7 @@ protected IStatus run(IProgressMonitor monitor) {
627627
.getGdbClientCommandLineArray(fLaunchConfiguration);
628628

629629
try {
630-
String projectName = fLaunchConfiguration.getAttribute(
631-
"org.eclipse.cdt.launch.PROJECT_ATTR", "");
632-
File dir = Utils.getProjectOsPath(projectName);
630+
File dir = Utils.getProjectOsPath(fLaunchConfiguration);
633631

634632
fProcess = launchGDBProcess(commandLineArray, dir);
635633
// Need to do this on the executor for thread-safety

ilg.gnuarmeclipse.debug.gdbjtag.openocd/src/ilg/gnuarmeclipse/debug/gdbjtag/openocd/Utils.java

Lines changed: 89 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
import org.eclipse.cdt.dsf.gdb.IGdbDebugPreferenceConstants;
2828
import org.eclipse.cdt.dsf.gdb.internal.GdbPlugin;
2929
import org.eclipse.cdt.dsf.gdb.launching.LaunchUtils;
30+
import org.eclipse.cdt.internal.core.envvar.EnvVarCollector;
31+
import org.eclipse.cdt.internal.core.envvar.EnvVarDescriptor;
32+
import org.eclipse.cdt.internal.core.envvar.EnvironmentVariableManager;
33+
import org.eclipse.cdt.internal.core.envvar.IEnvironmentContextInfo;
3034
import org.eclipse.cdt.utils.spawner.ProcessFactory;
3135
import org.eclipse.core.resources.IProject;
3236
import org.eclipse.core.resources.IWorkspace;
@@ -247,7 +251,7 @@ public static String[] getLaunchEnvironment(ILaunchConfiguration config)
247251

248252
ICConfigurationDescription cfg = getBuildConfig(config);
249253
if (cfg == null)
250-
return null;
254+
return getLaunchEnvironmentWithoutProject();
251255

252256
// Environment variables and inherited vars
253257
HashMap<String, String> envMap = new HashMap<String, String>();
@@ -363,4 +367,88 @@ public static File getProjectOsPath(String projectName) {
363367
File dir = new File(path.toOSString());
364368
return dir;
365369
}
370+
371+
/**
372+
* Gets working directory from either the location of the project, or if
373+
* project-less launching, from the location of the executable.
374+
*
375+
* @param configuration
376+
* @return Working directory
377+
* @throws CoreException
378+
*/
379+
public static File getProjectOsPath(ILaunchConfiguration configuration)
380+
throws CoreException {
381+
IPath path = null;
382+
File dir = null;
383+
if (null != configuration) {
384+
String projectName = configuration.getAttribute(
385+
"org.eclipse.cdt.launch.PROJECT_ATTR", "");
386+
if (projectName.length() > 0) {
387+
IWorkspace workspace = ResourcesPlugin.getWorkspace();
388+
IProject project = workspace.getRoot().getProject(projectName);
389+
path = project.getLocation();
390+
} else {
391+
/*
392+
* If projectless launch then PROGRAM_NAME will be an absolute
393+
* path
394+
*/
395+
String executableName = configuration.getAttribute(
396+
"org.eclipse.cdt.launch.PROGRAM_NAME", "");
397+
path = new Path(executableName).removeLastSegments(1);
398+
}
399+
}
400+
if (null != path) {
401+
dir = new File(path.toOSString());
402+
}
403+
return dir;
404+
}
405+
406+
/**
407+
* Get environment from workspace. Useful when project-less launching when
408+
* there is no environment available from the configuration.
409+
*
410+
* @return String [] of environment variables in variable=value format.
411+
* @throws CoreException
412+
*/
413+
public static String[] getLaunchEnvironmentWithoutProject()
414+
throws CoreException {
415+
String[] retVal = null;
416+
IEnvironmentContextInfo contextInfo = EnvironmentVariableManager
417+
.getDefault().getContextInfo(null);
418+
EnvVarCollector envVarMergedColletion = EnvironmentVariableManager
419+
.getVariables(contextInfo, true);
420+
if (null != envVarMergedColletion) {
421+
EnvVarDescriptor envVars[] = envVarMergedColletion.toArray(false);
422+
if (envVars != null) {
423+
List<String> strings = new ArrayList<String>();
424+
for (int i = 0; i < envVars.length; i++) {
425+
IEnvironmentVariable resolved = EnvironmentVariableManager
426+
.getDefault().calculateResolvedVariable(envVars[i],
427+
contextInfo);
428+
if (null != resolved) {
429+
// The project_classpath variable contributed by JDT is
430+
// useless
431+
// for running C/C++
432+
// binaries, but it can be lethal if it has a very large
433+
// value
434+
// that exceeds shell
435+
// limit. See
436+
// http://bugs.eclipse.org/bugs/show_bug.cgi?id=408522
437+
if (!"project_classpath".equals(resolved.getName())) {//$NON-NLS-1$
438+
StringBuffer buffer = new StringBuffer(
439+
resolved.getName());
440+
buffer.append('=').append(resolved.getValue());
441+
strings.add(buffer.toString());
442+
}
443+
}
444+
}
445+
retVal = strings.toArray(new String[strings.size()]);
446+
}
447+
} else {
448+
throw new CoreException(new Status(IStatus.ERROR,
449+
Activator.PLUGIN_ID,
450+
"Error retrieving workspace environment."));
451+
}
452+
return retVal;
453+
}
366454
}

0 commit comments

Comments
 (0)