From 0307260c3144737229b0959bd7a73e8b87ac509d Mon Sep 17 00:00:00 2001 From: Mikhail Khodjaiants Date: Mon, 19 Aug 2002 22:21:50 +0000 Subject: [PATCH] Implementation of the source location. --- .../eclipse/cdt/debug/core/CDebugModel.java | 3 + .../cdt/debug/core/ICSourceLocator.java | 49 ++++ .../cdt/debug/internal/core/CDebugTarget.java | 63 +++-- .../debug/internal/core/CSourceLocator.java | 217 ++++++++++++++++++ .../cdt/debug/internal/core/CStackFrame.java | 6 +- .../ui/CDTDebugModelPresentation.java | 35 +++ 6 files changed, 349 insertions(+), 24 deletions(-) create mode 100644 debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICSourceLocator.java create mode 100644 debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CSourceLocator.java diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/CDebugModel.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/CDebugModel.java index 072d9cb693c..79b4e7bab15 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/CDebugModel.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/CDebugModel.java @@ -14,6 +14,7 @@ import org.eclipse.cdt.debug.core.cdi.ICDILocation; import org.eclipse.cdt.debug.core.cdi.model.ICDITarget; import org.eclipse.cdt.debug.internal.core.CDebugElement; import org.eclipse.cdt.debug.internal.core.CDebugTarget; +import org.eclipse.core.resources.IProject; import org.eclipse.core.resources.IWorkspaceRunnable; import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; @@ -77,6 +78,7 @@ public class CDebugModel final ICDITarget cdiTarget, final String name, final IProcess process, + final IProject project, final boolean allowTerminate, final boolean allowDisconnect, final boolean stopInMain ) throws DebugException @@ -90,6 +92,7 @@ public class CDebugModel cdiTarget, name, process, + project, allowTerminate, allowDisconnect ); } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICSourceLocator.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICSourceLocator.java new file mode 100644 index 00000000000..61016174117 --- /dev/null +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/core/ICSourceLocator.java @@ -0,0 +1,49 @@ +/* + *(c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + * + */ +package org.eclipse.cdt.debug.core; + +import org.eclipse.core.resources.IProject; +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.model.ISourceLocator; + +/** + * + * A C/C++ extension of ISourceLocator. + * Provides constants and methods to manage different source modes. + * + * @since Aug 19, 2002 + */ +public interface ICSourceLocator extends ISourceLocator +{ + static final public int MODE_SOURCE = 0; + static final public int MODE_DISASSEMBLY = 1; + static final public int MODE_MIXED = 2; + + /** + * Returns the current source presentation mode. + * + * @return the current source presentation mode + */ + int getMode(); + + /** + * Sets the source presentation mode. + * + * @param the source presentation mode to set + */ + void setMode( int mode ); + + /** + * Returns the line number of the instruction pointer in the specified + * stack frame that corresponds to a line in an associated source element, + * or -1 if line number information is unavailable. + * + * @param frameInfo the frame data + * @return line number of instruction pointer in this stack frame, + * or -1 if line number information is unavailable + */ + int getLineNumber( IStackFrameInfo frameInfo ); +} diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CDebugTarget.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CDebugTarget.java index 6f4b876cdba..031ba7883ea 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CDebugTarget.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CDebugTarget.java @@ -12,6 +12,7 @@ import java.util.List; import org.eclipse.cdt.debug.core.CDebugCorePlugin; import org.eclipse.cdt.debug.core.CDebugModel; +import org.eclipse.cdt.debug.core.ICSourceLocator; import org.eclipse.cdt.debug.core.IFormattedMemoryBlock; import org.eclipse.cdt.debug.core.IFormattedMemoryRetrieval; import org.eclipse.cdt.debug.core.IRestart; @@ -24,6 +25,7 @@ import org.eclipse.cdt.debug.core.cdi.ICDISessionObject; import org.eclipse.cdt.debug.core.cdi.ICDISignal; import org.eclipse.cdt.debug.core.cdi.event.ICDIChangedEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDICreatedEvent; +import org.eclipse.cdt.debug.core.cdi.event.ICDIDestroyedEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDIDisconnectedEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDIEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDIEventListener; @@ -31,13 +33,12 @@ import org.eclipse.cdt.debug.core.cdi.event.ICDIExitedEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDILoadedEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDIRestartedEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDIResumedEvent; -//import org.eclipse.cdt.debug.core.cdi.event.ICDISteppingEvent; import org.eclipse.cdt.debug.core.cdi.event.ICDISuspendedEvent; -import org.eclipse.cdt.debug.core.cdi.event.ICDIDestroyedEvent; import org.eclipse.cdt.debug.core.cdi.model.ICDIObject; import org.eclipse.cdt.debug.core.cdi.model.ICDITarget; import org.eclipse.cdt.debug.core.cdi.model.ICDIThread; import org.eclipse.core.resources.IMarkerDelta; +import org.eclipse.core.resources.IProject; import org.eclipse.debug.core.DebugEvent; import org.eclipse.debug.core.DebugException; import org.eclipse.debug.core.DebugPlugin; @@ -48,6 +49,7 @@ import org.eclipse.debug.core.model.IBreakpoint; import org.eclipse.debug.core.model.IDebugTarget; import org.eclipse.debug.core.model.IMemoryBlock; import org.eclipse.debug.core.model.IProcess; +import org.eclipse.debug.core.model.ISourceLocator; import org.eclipse.debug.core.model.IThread; /** @@ -146,6 +148,11 @@ public class CDebugTarget extends CDebugElement */ private int fSuspendCount = 0; + /** + * The source locator for this target. + */ + private ICSourceLocator fSourceLocator = null; + /** * Constructor for CDebugTarget. * @param target @@ -154,6 +161,7 @@ public class CDebugTarget extends CDebugElement ICDITarget cdiTarget, String name, IProcess process, + IProject project, boolean allowsTerminate, boolean allowsDisconnect ) { @@ -166,6 +174,8 @@ public class CDebugTarget extends CDebugElement fConfig = cdiTarget.getSession().getConfiguration(); fSupportsTerminate = allowsTerminate & fConfig.supportsTerminate(); fSupportsDisconnect = allowsDisconnect & fConfig.supportsDisconnect(); + fSourceLocator = createSourceLocator( project ); + launch.setSourceLocator( fSourceLocator ); setThreadList( new ArrayList( 5 ) ); initialize(); DebugPlugin.getDefault().getLaunchManager().addLaunchListener( this ); @@ -341,7 +351,7 @@ public class CDebugTarget extends CDebugElement try { setTerminating( true ); - getCDITarget().terminate(); + getCDISession().terminate(); } catch( CDIException e ) { @@ -453,9 +463,10 @@ public class CDebugTarget extends CDebugElement * Refreshes the thread list. * */ - protected synchronized void refreshThreads() + protected synchronized List refreshThreads() { ArrayList list = new ArrayList( 5 ); + ArrayList newThreads = new ArrayList( 5 ); try { ICDIThread[] cdiThreads = getCDITarget().getThreads(); @@ -464,7 +475,8 @@ public class CDebugTarget extends CDebugElement CThread thread = findThread( cdiThreads[i] ); if ( thread == null ) { - thread = createThread( cdiThreads[i] ); + thread = new CThread( this, cdiThreads[i] ); + newThreads.add( thread ); } else { @@ -479,11 +491,17 @@ public class CDebugTarget extends CDebugElement } getThreadList().clear(); setThreadList( list ); + it = newThreads.iterator(); + while( it.hasNext() ) + { + ((CThread)it.next()).fireCreationEvent(); + } } catch( CDIException e ) { CDebugCorePlugin.log( e ); } + return newThreads; } /** @@ -939,12 +957,7 @@ public class CDebugTarget extends CDebugElement */ protected CThread createThread( ICDIThread cdiThread ) { - CThread thread = null; - thread = new CThread( this, cdiThread ); - if ( isDisconnected() ) - { - return null; - } + CThread thread = new CThread( this, cdiThread ); getThreadList().add( thread ); thread.fireCreationEvent(); return thread; @@ -958,12 +971,7 @@ public class CDebugTarget extends CDebugElement */ protected CThread createRunningThread( ICDIThread cdiThread ) { - CThread thread = null; - thread = new CThread( this, cdiThread ); - if ( isDisconnected() ) - { - return null; - } + CThread thread = new CThread( this, cdiThread ); thread.setRunning( true ); getThreadList().add( thread ); thread.fireCreationEvent(); @@ -996,12 +1004,14 @@ public class CDebugTarget extends CDebugElement setCurrentStateId( IState.SUSPENDED ); ICDISessionObject reason = event.getReason(); setCurrentStateInfo( reason ); - refreshThreads(); + List newThreads = refreshThreads(); if ( event.getSource() instanceof ICDIThread ) { CThread thread = findThread( (ICDIThread)event.getSource() ); - if ( thread != null ) + if ( thread != null && newThreads.contains( thread ) ) + { thread.handleDebugEvent( event ); + } } if ( reason instanceof ICDIEndSteppingRange ) { @@ -1216,4 +1226,19 @@ public class CDebugTarget extends CDebugElement incrementSuspendCount(); super.fireSuspendEvent( detail ); } + + /** + * Creates the source locator for this target. + * + * @return the source locator for this target + */ + private ICSourceLocator createSourceLocator( IProject project ) + { + return new CSourceLocator( this, project ); + } + + protected ICSourceLocator getSourceLocator() + { + return fSourceLocator; + } } diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CSourceLocator.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CSourceLocator.java new file mode 100644 index 00000000000..329e38829c2 --- /dev/null +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CSourceLocator.java @@ -0,0 +1,217 @@ +/* + *(c) Copyright QNX Software Systems Ltd. 2002. + * All Rights Reserved. + * + */ + +package org.eclipse.cdt.debug.internal.core; + +import org.eclipse.cdt.debug.core.CDebugCorePlugin; +import org.eclipse.cdt.debug.core.ICSourceLocator; +import org.eclipse.cdt.debug.core.IStackFrameInfo; +import org.eclipse.core.internal.resources.File; +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResource; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.Path; +import org.eclipse.debug.core.DebugException; +import org.eclipse.debug.core.model.IStackFrame; + +/** + * + * Locates source for a C/C++ debug session. + * + * @since Aug 19, 2002 + */ +public class CSourceLocator implements ICSourceLocator +{ + /** + * The project in which to look for source. + */ + private IProject fProject; + + /** + * The debug target this source locator is associated with. + */ + private CDebugTarget fTarget; + + /** + * The source presentation mode. + */ + private int fMode = ICSourceLocator.MODE_SOURCE; + + private int fInternalMode = MODE_SOURCE; + + /** + * Constructor for CSourceLocator. + */ + public CSourceLocator( CDebugTarget target, IProject project ) + { + fProject = project; + fTarget = target; + } + + /* (non-Javadoc) + * @see org.eclipse.debug.core.model.ISourceLocator#getSourceElement(IStackFrame) + */ + public Object getSourceElement( IStackFrame stackFrame ) + { + Object result = null; + if ( stackFrame != null && stackFrame.getAdapter( IStackFrameInfo.class ) != null ) + { + try + { + result = getInput( (IStackFrameInfo)stackFrame.getAdapter( IStackFrameInfo.class ) ); + } + catch( DebugException e ) + { + CDebugCorePlugin.log( e ); + } + } + return result; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.debug.core.ICSourceLocator#getLineNumber(IStackFrameInfo) + */ + public int getLineNumber( IStackFrameInfo frameInfo ) + { + int result = 0; + if ( getMode() == MODE_SOURCE ) + result = frameInfo.getFrameLineNumber(); + return result; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.debug.core.ICSourceLocator#getMode() + */ + public int getMode() + { + return fMode; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.debug.core.ICSourceLocator#setMode(int) + */ + public void setMode( int mode ) + { + } + + protected void setInternalMode( int mode ) + { + fInternalMode = mode; + } + + protected Object getInput( IStackFrameInfo frameInfo ) throws DebugException + { + Object result = null; + switch( getMode() ) + { + case ICSourceLocator.MODE_SOURCE: + result = getSourceInput( frameInfo ); + break; +/* + case ICSourceLocator.MODE_DISASSEMBLY: + result = getDisassemblyInput( frameInfo ); + break; + case ICSourceLocator.MODE_MIXED: + result = getMixedInput( frameInfo ); + break; +*/ + } + return result; + } + + private Object getSourceInput( IStackFrameInfo info ) + { + Object result = null; + if ( info != null ) + { + setInternalMode( ICSourceLocator.MODE_SOURCE ); + String fileName = info.getFile(); + if ( fileName != null && fileName.length() > 0 ) + result = findFile( fProject, fileName ); + } + // switch to assembly mode if source file not found +/* + if ( result == null ) + result = getDisassemblyInput( info ); +*/ + return result; + } + + private Object findFile( IProject project, String fileName ) + { + IPath path = new Path( fileName ); + fileName = path.toOSString(); + + /* + * We have a few possible cases: either we get a source file name with no path, + * or we get a fully-qualified filename from the debugger. + */ + String pPath = new String( project.getLocation().toOSString() ); + int i; + + if ( (i = fileName.indexOf( pPath )) >= 0 ) + { + i += pPath.length() + 1; + if ( fileName.length() > i ) + return project.getFile( fileName.substring( i ) ); + } + // Then just do a search on our project, and if that fails, any dependent projects + IFile f = null; + try + { + f = findFile( (IContainer)project, path ); + if ( f != null ) + return f; + if ( f == null ) + { + IProject[] p = project.getReferencedProjects(); + for ( int j= 0; j < p.length; j++ ) + { + if ( (f = findFile( (IContainer)p[j], new Path( fileName ) )) != null ) + { + return f; + } + } + } + } + catch( CoreException e ) + { + } + if ( f != null ) + return f; + // Search for an external file +// return findExternalFile( fileName ); + return null; + } + + private IFile findFile( IContainer parent, IPath name ) throws CoreException + { + if ( name.isAbsolute() ) + { + if ( name.toOSString().startsWith( parent.getLocation().toOSString() ) ) + { + name = new Path( name.toOSString().substring( parent.getLocation().toOSString().length() + 1 ) ); + } + } + IResource found = parent.findMember( name ); + if ( found != null && found.getType() == IResource.FILE ) + { + return (IFile)found; + } + IResource[] children= parent.members(); + for ( int i= 0; i < children.length; i++ ) + { + if ( children[i] instanceof IContainer ) + { + return findFile( (IContainer)children[i], name ); + } + } + return null; + } +} diff --git a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CStackFrame.java b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CStackFrame.java index 826d7cba843..0ea1d4cfe8a 100644 --- a/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CStackFrame.java +++ b/debug/org.eclipse.cdt.debug.core/src/org/eclipse/cdt/debug/internal/core/CStackFrame.java @@ -170,11 +170,7 @@ public class CStackFrame extends CDebugElement { if ( isSuspended() ) { - ICDILocation location = getCDIStackFrame().getLocation(); - if ( location != null ) - { - return location.getLineNumber(); - } + return ((CDebugTarget)getDebugTarget()).getSourceLocator().getLineNumber( this ); } return -1; } diff --git a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDTDebugModelPresentation.java b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDTDebugModelPresentation.java index 1918cdc1d71..272f532b05e 100644 --- a/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDTDebugModelPresentation.java +++ b/debug/org.eclipse.cdt.debug.ui/src/org/eclipse/cdt/debug/internal/ui/CDTDebugModelPresentation.java @@ -11,6 +11,9 @@ import java.util.HashMap; import org.eclipse.cdt.debug.core.IStackFrameInfo; import org.eclipse.cdt.debug.core.IState; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IResource; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.debug.core.DebugException; @@ -24,7 +27,11 @@ import org.eclipse.debug.ui.IDebugModelPresentation; import org.eclipse.debug.ui.IValueDetailListener; import org.eclipse.jface.viewers.LabelProvider; import org.eclipse.swt.graphics.Image; +import org.eclipse.ui.IEditorDescriptor; import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IEditorRegistry; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.FileEditorInput; /** * @@ -87,6 +94,27 @@ public class CDTDebugModelPresentation extends LabelProvider */ public IEditorInput getEditorInput( Object element ) { + IFile file = null; + if ( element instanceof IMarker ) + { + IResource resource = ((IMarker)element).getResource(); + if ( resource instanceof IFile ) + file = (IFile)resource; + } + if ( element instanceof IFile ) + file = (IFile)element; + if ( file != null ) + return new FileEditorInput( file ); +/* + if ( element instanceof IFileStorage ) + { + return new FileStorageEditorInput( (IFileStorage)element ); + } + if ( element instanceof IDisassemblyStorage ) + { + return new DisassemblyEditorInput( (IStorage)element ); + } +*/ return null; } @@ -95,6 +123,13 @@ public class CDTDebugModelPresentation extends LabelProvider */ public String getEditorId( IEditorInput input, Object element ) { + if ( input != null ) + { + IEditorRegistry registry = PlatformUI.getWorkbench().getEditorRegistry(); + IEditorDescriptor descriptor = registry.getDefaultEditor( input.getName() ); + if ( descriptor != null ) + return descriptor.getId(); + } return null; }