1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-22 22:22:11 +02:00

Bug 439631 - [dynamic printf] Backslashes in the file name should be escaped

The default dprintf string on windows contains backslashes which cause
unexpected results when printed.
This commit is contained in:
Anton Leherbauer 2014-12-11 14:11:40 +01:00
parent 20c1166f1a
commit e11e3d082e
2 changed files with 26 additions and 7 deletions

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.debug.internal.ui.actions.breakpoints;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import org.eclipse.cdt.debug.core.CDIDebugModel; import org.eclipse.cdt.debug.core.CDIDebugModel;
import org.eclipse.cdt.debug.core.model.ICBreakpointType; import org.eclipse.cdt.debug.core.model.ICBreakpointType;
@ -51,12 +52,12 @@ public class ToggleDynamicPrintfAdapter extends AbstractToggleBreakpointAdapter
// Although the user will be given the opportunity to provide the printf string // Although the user will be given the opportunity to provide the printf string
// in the properties dialog, we pre-fill it with the default string to be nice // in the properties dialog, we pre-fill it with the default string to be nice
attributes.put(ICDynamicPrintf.PRINTF_STRING, attributes.put(ICDynamicPrintf.PRINTF_STRING,
NLS.bind(Messages.Default_LineDynamicPrintf_String, sourceHandle, lineNumber)); NLS.bind(Messages.Default_LineDynamicPrintf_String, escapeBackslashes(sourceHandle), lineNumber));
openBreakpointPropertiesDialog(dprintf, part, resource, attributes); openBreakpointPropertiesDialog(dprintf, part, resource, attributes);
} else { } else {
// We provide a default printf string to make the dynamic printf useful automatically // We provide a default printf string to make the dynamic printf useful automatically
String printfStr = NLS.bind(Messages.Default_LineDynamicPrintf_String, sourceHandle, lineNumber); String printfStr = NLS.bind(Messages.Default_LineDynamicPrintf_String, escapeBackslashes(sourceHandle), lineNumber);
CDIDebugModel.createLineDynamicPrintf( CDIDebugModel.createLineDynamicPrintf(
sourceHandle, resource, getBreakpointType(), lineNumber, true, 0, "", printfStr, true );//$NON-NLS-1$ sourceHandle, resource, getBreakpointType(), lineNumber, true, 0, "", printfStr, true );//$NON-NLS-1$
@ -102,7 +103,8 @@ public class ToggleDynamicPrintfAdapter extends AbstractToggleBreakpointAdapter
return false; return false;
} }
protected void createWatchpoint( boolean interactive, IWorkbenchPart part, String sourceHandle, IResource resource, @Override
protected void createWatchpoint( boolean interactive, IWorkbenchPart part, String sourceHandle, IResource resource,
int charStart, int charEnd, int lineNumber, String expression, String memorySpace, String range) throws CoreException int charStart, int charEnd, int lineNumber, String expression, String memorySpace, String range) throws CoreException
{ {
} }
@ -121,4 +123,12 @@ public class ToggleDynamicPrintfAdapter extends AbstractToggleBreakpointAdapter
protected int getBreakpointType() { protected int getBreakpointType() {
return ICBreakpointType.REGULAR; return ICBreakpointType.REGULAR;
} }
/**
* Escape embedded backslashes for inclusion in C string.
*/
private static String escapeBackslashes(String str) {
return str.replaceAll(Pattern.quote("\\"), "\\\\\\\\"); //$NON-NLS-1$//$NON-NLS-2$
}
} }

View file

@ -12,6 +12,7 @@ package org.eclipse.cdt.dsf.gdb.internal.ui.breakpoints;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.regex.Pattern;
import org.eclipse.cdt.core.IAddress; import org.eclipse.cdt.core.IAddress;
import org.eclipse.cdt.debug.core.CDIDebugModel; import org.eclipse.cdt.debug.core.CDIDebugModel;
@ -34,7 +35,7 @@ public class DisassemblyToggleDynamicPrintfTarget extends AbstractDisassemblyBre
@Override @Override
protected void createLineBreakpoint(String sourceHandle, IResource resource, int lineNumber) throws CoreException { protected void createLineBreakpoint(String sourceHandle, IResource resource, int lineNumber) throws CoreException {
// We provide a default printf string to make the dynamic printf useful automatically // We provide a default printf string to make the dynamic printf useful automatically
String printfStr = NLS.bind(Messages.Default_LineDynamicPrintf_String, sourceHandle, lineNumber); String printfStr = NLS.bind(Messages.Default_LineDynamicPrintf_String, escapeBackslashes(sourceHandle), lineNumber);
CDIDebugModel.createLineDynamicPrintf(sourceHandle, resource, getBreakpointType(), lineNumber, true, 0, "", printfStr, true); //$NON-NLS-1$ CDIDebugModel.createLineDynamicPrintf(sourceHandle, resource, getBreakpointType(), lineNumber, true, 0, "", printfStr, true); //$NON-NLS-1$
} }
@ -51,7 +52,7 @@ public class DisassemblyToggleDynamicPrintfTarget extends AbstractDisassemblyBre
// Although the user will be given the opportunity to provide the printf string // Although the user will be given the opportunity to provide the printf string
// in the properties dialog, we pre-fill it with the default string to be nice. // in the properties dialog, we pre-fill it with the default string to be nice.
attributes.put(ICDynamicPrintf.PRINTF_STRING, attributes.put(ICDynamicPrintf.PRINTF_STRING,
NLS.bind(Messages.Default_LineDynamicPrintf_String, sourceHandle, lineNumber)); NLS.bind(Messages.Default_LineDynamicPrintf_String, escapeBackslashes(sourceHandle), lineNumber));
openBreakpointPropertiesDialog(dprintf, part, resource, attributes); openBreakpointPropertiesDialog(dprintf, part, resource, attributes);
} }
@ -87,4 +88,12 @@ public class DisassemblyToggleDynamicPrintfTarget extends AbstractDisassemblyBre
protected int getBreakpointType() { protected int getBreakpointType() {
return ICBreakpointType.REGULAR; return ICBreakpointType.REGULAR;
} }
/**
* Escape embedded backslashes for inclusion in C string.
*/
private static String escapeBackslashes(String str) {
return str.replaceAll(Pattern.quote("\\"), "\\\\\\\\"); //$NON-NLS-1$//$NON-NLS-2$
}
} }