Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach a debugger at process creation?

I would like to debug a .NET application that fails immediately on startup (and exists without an error message or log), but I can't attach a debugger to it because the process exists almost immediately after I run it. I don't have the source code for the app, so I can't do "Start Debugging". I tried using a Visual Studio macro to start a process, attach to it, then break, but the macro is too slow and by the time it finds the process, the process has already exited:

Imports System
Imports EnvDTE80
Imports EnvDTE90
Imports System.Diagnostics

Public Module Module1
    Sub RunAndAttach()
        Try
            Dim dbg As Debugger3 = DTE.Debugger
            Dim trans As Transport = dbg.Transports.Item("Default")
            Dim sysProc As Process = System.Diagnostics.Process.Start(New ProcessStartInfo("C:\Temp\CrashingApp.exe") With {.WorkingDirectory = "C:\Temp"})
            Dim proc As EnvDTE90.Process3 = dbg.GetProcesses(trans, "ALLON-PC").Item("CrashingApp.exe")
            If (Not sysProc.HasExited) Then
                proc.Attach()
                proc.Break(False)
            Else
                MsgBox("Process " + proc.Name + " has already has exited.")
            End If
        Catch ex As System.Exception
            MsgBox(ex.Message)
        End Try
    End Sub

End Module

Is there a way to attach the debugger to a newly created process, like F5 does?

Thanks!

like image 381
Allon Guralnek Avatar asked Sep 26 '09 22:09

Allon Guralnek


People also ask

How do debuggers attach to processes?

You can attach the Visual Studio debugger to a running process on a local or remote computer. After the process is running, select Debug > Attach to Process or press Ctrl+Alt+p in Visual Studio, and use the Attach to Process dialog to attach the debugger to the process.

How do you attach code or debugger?

To bring up the Run and Debug view, select the Run and Debug icon in the Activity Bar on the side of VS Code. You can also use the keyboard shortcut Ctrl+Shift+D. The Run and Debug view displays all information related to running and debugging and has a top bar with debugging commands and configuration settings.

How do you attach the debugger to the w3wp exe process?

Open Visual Studio in Administrator Mode, then Debug -> attach to process -> tick the check box "Show processes from all user", select w3wp.exe. Administration mode is required for debugging the source code.

How do you attach a WinDbg?

When WinDbg is in dormant mode, you can attach to a running process by choosing Attach to a Process from the File menu or by pressing F6. In the Attach to Process dialog box, select the process you want to debug, and select OK.


1 Answers

  1. Create a new project (a console project is fine).
  2. Right-click the project and choose "Properties".
  3. Click the "Debug" tab.
  4. Choose "Start External Program:"
  5. Select the program.
  6. Hit F5.
like image 76
John Fisher Avatar answered Nov 16 '22 01:11

John Fisher