While debugging in Visual Studio is there any way to find all pointers pointing to a particular address if it is known that they are within the present scope ?
For VS2010, you can use a macro.
Open Macro window by clicking "Tools -> Macros -> Macro IDE".
Copy and paste the following macro
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module Module1
Sub DumpLocals()
Dim outputWindow As EnvDTE.OutputWindow
Dim address As String = "0x009efedc"
outputWindow = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput).Object
Dim currentStackFrame As EnvDTE.StackFrame
currentStackFrame = DTE.Debugger.CurrentStackFrame
outputWindow.ActivePane.OutputString("*Dumping Local Variables*" + vbCrLf)
For Each exp As EnvDTE.Expression In currentStackFrame.Locals
If exp.Value = address Then
outputWindow.ActivePane.OutputString("Match: " + exp.Name + " = " + exp.Value.ToString() + vbCrLf)
End If
Next
End Sub
End Module
Set a breakpoint in your C++ program where you want to list the pointers.
Run your C++ program.
When the breakpoint is hit
Go back to the Macro window.
Change the value of the address variable in the macro. Note that you may need to remove the "0x" if you are not using hex.
Press Ctrl+S to save the macro.
While still in the Macro window, press F5 to run the macro. The result will be in the Output window (Debug -> Windows -> Output).
Maybe you can even add parameter to the macro and call it from Immediate Window.
P/S: This macro is modified from http://weblogs.asp.net/scottgu/archive/2010/08/18/debugging-tips-with-visual-studio-2010.aspx
Err, I have another easier solution for you. When you hit a breakpoint:
Open the Locals window (Debug -> Windows -> Locals).
Press Ctrl+A to select everything in Locals window.
Press Ctrl+C to copy
Paste them into Excel
Sort the Value column in Excel.
Look for the matched pointers.
Related
This question was answered before for a previous version of Visual Studio (VS). The offered solutions involved macros, which are no longer available in VS 2015. Could I get a solution for VS 2015?
I would like to do a "find all" in VS and put a debug-breakpoint on every line with a find match.
Link to previous question asked by Noah:
How do I add Debug Breakpoints to lines displayed in a "Find Results" window in Visual Studio
I've converted the old macro to a VB command in Visual Commander (by adding explicit namespaces to classes):
Public Class C
Implements VisualCommanderExt.ICommand
Sub Run(DTE As EnvDTE80.DTE2, package As Microsoft.VisualStudio.Shell.Package) Implements VisualCommanderExt.ICommand.Run
Dim findResultsWindow As EnvDTE.Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindFindResults1)
Dim selection As EnvDTE.TextSelection
selection = findResultsWindow.Selection
selection.SelectAll()
Dim findResultsReader As New System.IO.StringReader(selection.Text)
Dim findResult As String = findResultsReader.ReadLine()
Dim findResultRegex As New System.Text.RegularExpressions.Regex("(?<Path>.*?)\((?<LineNumber>\d+)\):")
While Not findResult Is Nothing
Dim findResultMatch As System.Text.RegularExpressions.Match = findResultRegex.Match(findResult)
If findResultMatch.Success Then
Dim path As String = findResultMatch.Groups.Item("Path").Value
Dim lineNumber As Integer = Integer.Parse(findResultMatch.Groups.Item("LineNumber").Value)
Try
DTE.Debugger.Breakpoints.Add("", path, lineNumber)
Catch ex As System.Exception
' breakpoints can't be added everywhere
End Try
End If
findResult = findResultsReader.ReadLine()
End While
End Sub
End Class
If you have JetBrains Resharper and use one of Reharper's search commands, you can do this directly from Resharper's Find Results window (different from VS Find Results).
Example:
Resharper > Navigate > Go to Text... (Ctrl+T,T,T if using Resharper key map)
And then in Find Results (Resharper), right click any node or container in the tree view and choose "Set Breakpoint." This sets a breakpoint on all sub-nodes.
Reference:
https://blog.jetbrains.com/dotnet/2017/12/04/debugger-features-resharper/
I do WinForms and Web development in Visual Studio 2010. The web developers use a tab size of four spaces, and the WinForms developers use a tab size of two.
Since I switch backwards and forwards between the two, I was wondering if there's a quick way to do it, without having to navigate the options every single time?
There's not a dedicated shortcut, but let's make one!
Simply hit Alt+F11 to bring up the Macros editor and add the following code to a new module:
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module Tabspaces
Sub TwoTabSpaces()
Dim tabs As Properties
tabs = DTE.Properties("TextEditor", "AllLanguages")
tabs.Item("TabSize").Value = 2
tabs.Item("IndentSize").Value = 2
End Sub
Sub FourTabSpaces()
Dim tabs As Properties
tabs = DTE.Properties("TextEditor", "AllLanguages")
tabs.Item("TabSize").Value = 4
tabs.Item("IndentSize").Value = 4
End Sub
End Module
Then save your macros and bring up the Options window from the Tools menu and choose Environment->Keyboard:
Here, you can search for commands containing 'macros' and you should see your TwoTabSpaces() and FourTabSpaces() listed.
You can now assign a keyboard shortcut to these macros. In the picture above, I've chosen Alt+T, Alt+2 and Alt+T, Alt+4 respectively (t=Tabs, 2=two spaces etc.)
You can customize them as you wish, of course.
I thought I'd share my personal macro that does a toggle based on language. You can set up keyboard shortcuts like suggested in Widor's answer.
Public Sub ToggleSpaces()
If DTE.ActiveDocument.Language = "Basic" Then
Dim textEditorSettings = DTE.Properties("TextEditor", "Basic")
Dim currentSize = CShort(textEditorSettings.Item("TabSize").Value)
'If at 2, use 4... else switch to 2
Dim newSpaces As Short = If(currentSize = 2, 4, 2)
textEditorSettings.Item("TabSize").Value = newSpaces
textEditorSettings.Item("IndentSize").Value = newSpaces
End If
End Sub
My macro was adapted from James Alexander's macro to "Toggle Between Leading Tabs or Spaces Per Project" found in the following StackOverflow question.
Not how I can elaborate more than the title...
I like to write code without intellisense, exactly how I want it to look, then use ReSharper (or whatever) to generate the classes.
IntelliSense, much as I love it, gets in the way of this process!
Here is the solution I found. Requires some work, but still.
Actually we need to create two macros - one to disable Intellisense and one to enable it back. So what we need to do is launch Macros IDE (Tools -> Macros -> Macros IDE) and create two functions:
Sub DisableIntellisense()
Dim p As EnvDTE.Properties = DTE.Properties("TextEditor", "AllLanguages")
p.Item("AutoListMembers").Value = False
p.Item("AutoListParams").Value = False
End Sub
Sub EnableIntellisense()
Dim p As EnvDTE.Properties = DTE.Properties("TextEditor", "AllLanguages")
p.Item("AutoListMembers").Value = True
p.Item("AutoListParams").Value = True
End Sub
Then just save this macros and assign shortcuts for it. To do that, go to Tools -> Options -> Environment -> Keyboard. Type in the text box name of the macros (either DisableIntellisense or EnableIntellisense) - if everything was right our macros will show up in the list of commands.
I've only tried in the Visual Studio 2010 - not sure whether it was possible to interact with Options via macros in earlier versions of VS.
does anyone know of a tool or extension to Visual Studio 2010 to count non-whitespace (e.g. all characters but not spaces, new lines etc.) for current selection in a document?
Nice to have for code golfing :)
I have a command line tool, but an integrated tool would be very nice. Also I would prefer something to evaluate current selection.
I finally got to creating this crude macro below by first recording a temporary macro in Visual Studio and then modifying it to look like the below:
Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a
Imports EnvDTE100
Imports System.Diagnostics
Public Module CountNonWhiteSpaceCharacters
Sub Count()
Dim selection As EnvDTE.TextSelection = DTE.ActiveDocument.Selection()
Dim text As String = selection.Text
text = text.Replace(" ", "")
text = text.Replace(vbCrLf, "")
text = text.Replace(vbTab, "")
MsgBox("Count " + text.Length.ToString())
End Sub
End Module
This can be bound to a keybord shortcut if desired. Otherwise, double clicking it in Macro Explorer will run it.
I'm currently using DPack as this adds a "Collapse All Projects" option to the Solution node in Solution Explorer. It works pretty well but can take a while to execute and doesn't always collapse everything fully.
Are there any better alternatives? Preferably free and easy to install/setup. There are lots out there but which work best and don't have any bugs or performance issues.
I use the following Macro which works in Visual Studio 2005 and Visual Studio 2008:
View > Other Windows > Macro Explorer (Alt+F8)
Right-click on the MyMacros node in Macro Explorer
New Module...
Name it CollapseAll (or whatever you like)
Replace the default code with the code shown below
File > Save CollapseAll (Ctrl+S)
Close the Macro editor
To set up a keyboard shortcut:
Tools > Customize... > Commands
Keyboard...
Show commands containing: Macros.MyMacros.CollapseAll.CollapseAll
Assign a keyboard shortcut (I use Alt+C)
Code
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics
Public Module CollapseAll
Sub CollapseAll()
' Get the the Solution Explorer tree
Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (solutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If
' Get the top node (the name of the solution)
Dim rootNode As UIHierarchyItem = solutionExplorer.UIHierarchyItems.Item(1)
rootNode.DTE.SuppressUI = True
' Collapse each project node
Collapse(rootNode, solutionExplorer)
' Select the solution node, or else when you click
' on the solution window
' scrollbar, it will synchronize the open document
' with the tree and pop
' out the corresponding node which is probably not what you want.
rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI = False
End Sub
Sub CollapseSelected()
' Get the the Solution Explorer tree
Dim solutionExplorer As UIHierarchy
solutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (solutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If
' Get the top node (the name of the solution)
Dim selected As Array = solutionExplorer.SelectedItems
If (selected.Length = 0) Then Return
Dim rootNode As UIHierarchyItem = selected(0)
rootNode.DTE.SuppressUI = True
' Collapse each project node
Collapse(rootNode, solutionExplorer)
' Select the solution node, or else when you click
' on the solution window
' scrollbar, it will synchronize the open document
' with the tree and pop
' out the corresponding node which is probably not what you want.
rootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
rootNode.DTE.SuppressUI = False
End Sub
Private Sub Collapse(ByVal item As UIHierarchyItem, ByRef solutionExplorer As UIHierarchy)
For Each innerItem As UIHierarchyItem In item.UIHierarchyItems
If innerItem.UIHierarchyItems.Count > 0 Then
' Re-cursive call
Collapse(innerItem, solutionExplorer)
' Collapse
If innerItem.UIHierarchyItems.Expanded Then
innerItem.UIHierarchyItems.Expanded = False
If innerItem.UIHierarchyItems.Expanded = True Then
' Bug in VS 2005
innerItem.Select(vsUISelectionType.vsUISelectionTypeSelect)
solutionExplorer.DoDefaultAction()
End If
End If
End If
Next
End Sub
End Module
I didn't write this code and I'm not sure where this code came from, but there are variations of it online.
For VS2005, I've been using CoolCommands 4.0. The feature description is more complete for the older 3.0 version.
Version 3 had an .msi installer. Version 4 is a .zip file (which was easier for my environment anyway).
My favorite features (a subset of the complete list):
From the Solution explorer:
Collapse All Projects
Open containing folder (Project/file level only)
From the filename tabs above the editor
Locate in Solution Explorer
From the context menu in the editor
Demo Font
Power commands for visual studio will do the trick. Didn't notice any performance\stability issues with them.
Here is a better list of features for CoolCommands 4.0.
To install it for VS 2005, execute the include setup.bat.
To install it for VS 2008, modify the following line from
regpkg CoolCommands.dll /codebase
to:
regpkg CoolCommands.dll /root:Software\Microsoft\VisualStudio\9.0 /codebase
PowerCommands for Visual Studio will work for both VS2008 and VS2010. It is the Microsoft-enabled way to do this quickly.