Andrew Connell [MVP MOSS]
1543 Posts |  42 Articles |  4753 Comments
.NET  |  MCMS  |  SharePoint  |  Office System
SharePoint Quick Links
Article Categories
Archives
Post Categories


Add to Technorati Favorites

Those of us SharePoint developers know all too well how to debug our solutions. If you're building a new Web Part, in order to debug it, you have to manually attach the Visual Studio debugger to the W3SVC.EXE process by selecting Debug -> Attached to Process... and then selecting one or more instances of the W3SVC.EXE process, and click Attach. This is tedious... and repetitive.

Hmm... repetitive... can't that be automated? Anyone who's seen one of my "Don't be a Tool..." talks in the Florida community knows that I'm a huge proponent of automating anything and everything possible. Recently I spotted a post on the PowerShell blog titled the Philosophy of Automation (a killer post, and a recommended read) where the author, Jeffery Snover, nails a great quote on why I love tools & automating stuff:

"Rather by increasing the number of things that we don't have to think about, we can spend our thinking on NEW things and solving NEW problems and then transforming those things into things that we no longer have to think about."

- Philosophy of Automation; Jeffery Snover (MSFT PowerShell Blog)

Anyway... getting back to the point of this post...

This whole debugging thing in SharePoint drives me batty. If I want to debug my Web Part or event receiver, I have to manually attach to the W3SVC.EXE processes (or the specific one for my Application Pool if I want to take the time to find it). If I'm debugging a custom timer job, I have to attach to the OWSTIMER.EXE process. Well, these repetitive processes can be automated in Visual Studio!

Follow the same process I outlined in my post When "F5" debugging doesn't cut it and manually attaching to processes takes too long... last May on creating a new macro. Here are three I use in my SharePoint development. One attaches to all W3SVC.EXE processes, the other attaches to the OWSTIMER.EXE process, and finally, as a bonus, there's another that attaches to the MbUnit GUI which I use for my unit testing (here's the text version to download and copy into your macro editor):

   1:  Imports System
   2:  Imports EnvDTE
   3:  Imports EnvDTE80
   4:  Imports System.Diagnostics
   5:   
   6:  Public Module Debuggers
   7:      Function AttachToProcess(ByVal processName As String) As Boolean
   8:          Dim proc As EnvDTE.Process
   9:          Dim attached As Boolean
  10:          For Each proc In DTE.Debugger.LocalProcesses
  11:              If (Right(proc.Name, Len(processName)) = processName) Then
  12:                  proc.Attach()
  13:                  attached = True
  14:              End If
  15:          Next
  16:   
  17:          Return attached
  18:      End Function
  19:   
  20:      Sub AttachDebuggerToMbUnit()
  21:          Dim processToAttachTo As String = "MbUnit.GUI.exe"
  22:   
  23:          If Not AttachToProcess(processToAttachTo) Then
  24:              MsgBox(processToAttachTo & " is not running")
  25:          End If
  26:   
  27:      End Sub
  28:   
  29:      Sub AttachDebuggerToWssTimerService()
  30:          Dim processToAttachTo As String = "OWSTIMER.EXE"
  31:   
  32:          If Not AttachToProcess(processToAttachTo) Then
  33:              MsgBox(processToAttachTo & " is not running")
  34:          End If
  35:      End Sub
  36:   
  37:      Sub AttachDebuggerToIIS()
  38:          Dim processToAttachTo As String = "w3wp.exe"
  39:   
  40:          If Not AttachToProcess(processToAttachTo) Then
  41:              MsgBox(processToAttachTo & " is not running")
  42:          End If
  43:      End Sub
  44:  End Module
 
 
Technorati tags: , , , ,
posted on Thursday, January 25, 2007 10:57 AM

Feedback

# Sharepoint debug 2/4/2007 2:06 PM Alberto Casu
Gravatar In un post di Andrew Connell una soluzione per semplificare il processo di debug di WSS o MOSS (che tipicamente

# re: Make your SharePoint debugging experience a little less painful 2/7/2007 2:03 AM Mauner
Gravatar Thank you, its very powerfull.
Please and what about REMOTE debuging? (w3wp.exe and debuger are running on the server, WS2005 is running on local PC)
How attach it?

# Any way to debug only managed code? 2/7/2007 8:17 AM Martin Laporte
Gravatar I tried using a similar macro recently.... The big problem I had with it is that Visual Studio would debug the process in both managed and unmanaged modes, and since my machine is configured to use a symbol server, attaching in native mode takes ages. Do you know any way of getting around this?

# re: Make your SharePoint debugging experience a little less painful 2/7/2007 9:11 AM AC [MVP MOSS]
Gravatar Martin - Sorry... I haven't come across that. But since it's just VBScript... you should be able to edit it for your situation.

# re: Make your SharePoint debugging experience a little less painful 2/7/2007 9:16 AM AC [MVP MOSS]
Gravatar Mauner - Sorry... I don't do any remote debugging... all my development is done in a virtual machine with SharePoint installed locally.

# re: Macro for remote debugging 7/12/2007 6:54 AM Tudor Olariu
Gravatar Hello all,

Here is the macro I wrote to remotely attach to a process on another machine. Please note that the second argument of the method dbg2.GetProcesses must be in the form "DOMAIN\USERNAME@MACHINE";

for example, the values

domain=SALES
username=John
machine=comp123

would map to "SALES\John@comp123".


Hope it helps!






Option Strict Off
Option Explicit Off
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module RemoteAttach


Sub attach()
Try

Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(1) As EnvDTE80.Engine
dbgeng(0) = trans.Engines.Item("Workflow")
Dim processRemote As EnvDTE80.Process2


Dim processesRemote = dbg2.GetProcesses(trans, "DOMAIN\USERNAME@MACHINE")

Dim pidRemote As Integer
pidremote = Convert.ToInt16(InputBox("Process number:"))

For Each processRemote In processesRemote
If processRemote.ProcessID = pidRemote Then
processRemote.Attach2()
End If
Next

Catch ex As System.Exception

MsgBox(ex.Message)

End Try


End Sub
End Module

# Debug Only Managed Code 10/16/2007 6:20 PM Chris Drudge
Gravatar Here's the code to debug only managed code:

Imports System
Imports EnvDTE
Imports EnvDTE80
Imports System.Diagnostics

Public Module AttachToWebServer
Public Sub AttachToWebServer()
Dim AspNetWp As String = "aspnet_wp.exe"
Dim W3WP As String = "w3wp.exe"
Dim K2Server As String = "K2Server.exe"
If Not (AttachToProcess(AspNetWp)) Then
If Not AttachToProcess(W3WP) Then
System.Windows.Forms.MessageBox.Show(String.Format("Process {0} or {1} Cannot Be Found", AspNetWp, W3WP), "Attach To Web Server Macro")
End If
End If
End Sub

Public Function AttachToProcess(ByVal ProcessName As String) As Boolean
Try
Dim ProcessFound As Boolean = False
Dim dbg2 As EnvDTE80.Debugger2 = DTE.Debugger
Dim trans As EnvDTE80.Transport = dbg2.Transports.Item("Default")
Dim dbgeng(10) As EnvDTE80.Engine
Dim indexer As Integer = 0

For Each myEngine As Engine In trans.Engines
' Possible values here could be "T-SQL", "Native", "Managed", "Workflow"
' "Managed/Native", or "Script"
If myEngine.Name.Equals("Managed") Then
dbgeng(indexer) = myEngine
indexer += 1
End If
Next

Dim processes As EnvDTE.Processes = dbg2.GetProcesses(trans, "localhost")
For Each process As EnvDTE80.Process2 In processes
If process.Name.Contains(ProcessName) Then
process.Attach2(dbgeng)
ProcessFound = True
End If
Next
Return processFound
Catch ex As System.Exception
MsgBox(ex.Message)
End Try
End Function
End Module

# re: Make your SharePoint debugging experience a little less painful 12/7/2007 2:43 AM jafin
Gravatar Thanks Chris, works a treat!

# R6034 5/28/2008 4:01 PM Peter
Gravatar I want to be able to REMOTE DEBUG a SharePoint workflow, with the workflow running on the windows server 2003 box and the debugger on my windows XP box. I get a C Run-Time Error R6034 that pops up on the windows server 2003 box when I try to remote debug it.

# re: Make your SharePoint debugging experience a little less painful 5/28/2008 11:37 PM AC [MVP MOSS]
Gravatar Peter-
Sorry... I can't help you there. I don't do any remote debugging because there are so many hassels. I'd look in the MSDN Online Forums for some help here.

# re: Make your SharePoint debugging experience a little less painful 10/6/2008 1:14 PM Tobias Zimmergren
Gravatar Sweet. Why didn't I see this before?
Thanks to Vince Rothwell for pointing it out in his blog!

Cheers
Tobias

# re: Make your SharePoint debugging experience a little less painful 10/9/2009 9:44 AM Jennifer
Gravatar If you're using VS 2008 you need to add the line
"Imports EnvDTE90"


Post Feedback

Title:
Name:
Email:
(email will not be displayed)
Url:
Comments: 
Please add 2 and 1 and type the answer here:    
All Comments Are Filtered & Moderated
Unfortunately comment spammers are just too effecient and are constantly dirtying up blogs with irrelevant and unwanted comments trying to improve their standing on search engines. All comments on this blog are moderated. I do not censor comments, but I don't approve comments with vulger language or those soliciting products. Most of the time comments are approved within a few hours of being submitted with the only exception when I'm traveling.

Why are you asking for my email address?
The only reason I'm asking for your email address, which isn't required to submit a comment, is to provide a gravatar if you've created an account for yourself and associated your email address with a small image. If you have a gravatar created for the email address you submit, it will appear next to your comment. Otherwise nothing will appear.

What is a gravatar?
A gravatar is a "globally recognized avatar." You can get more information about gravatars, as well as create your own for free, at www.gravatar.com. You can also view my gravatar here.


Copyright © 2003 - 2010 Andrew Connell
Creative Commons License 
This work is licensed under a Creative Commons License
Site design by Heather Solomon.

 
 
MOSS WCM Training
Looking for MOSS 2007 WCM developer training? Look no further! I teach my 5-day hands-on and online WCM classes for developers I offer through my company: Critical Path Training.

Get more information on the WCM courses!