Debugging an executable in visual studio
Asked Answered
P

4

33

Want to debug an executable under debugger. How to do it in visual studio.

Windbg has an option of open executable. But I find this is missing in VS 2010.

The question is not exactly same as Debug exe in visual studio 2010 as I am not really interested in image file execution to debug a start up.

Rather want to just debug the exe under debugger, once it is broken, want to set some break points and understand the flow of execution.

Albeit image file execution is a workaround for this.

I am not sure if this question is naive; But this is a very straight line use case scenario, I find missing in MS VS 2010.

Prem answered 26/2, 2013 at 19:28 Comment(2)
I have been looking for the answer to the same question. The problem is that there is a way to open an executable in WinDbg such that you can attach to all the sub processes the executable spawns automatically, whereas in Visual Studio one has to manually attach to every sub process that is spawned.Adolfoadolph
See How to debug and profile any EXE with Visual Studio - Visual Studio Blog and Debug an app that isn't part of a Visual Studio solution | Microsoft Docs.Specialistic
B
45

You did not specify it in the question, but I assume you do not have the source code. Just use File/Open Project/Solution, select EXE file and Open it. Then select Debug/Start debugging.

The other option is to run the EXE first and then Select Debug/Attach to process.

Bemba answered 26/2, 2013 at 22:18 Comment(5)
If you happen to have a command line application to debug that starts and finishes immediately without any user interaction, you might want to use Debug / Step Into instead of Debug / Start. BTW, if you open the Solution Explorer, you should see your EXE under the solution. By right-clicking on it, you have a Debug menu with two options, "Start new instance" and "Step Into new instance". You can start debugging using these options as well.Roseberry
it gives me System.ExecutionEngineExceptionBalcom
When I select an exe on a Remote Machine. Will be this executed remote or local?Hanhana
If you select an exe located on a file share (example: \\server\share\myfile.exe). it will be executed locally.Bemba
Even if you have no source code and the EXE has no debugging symbols this is very useful. VS will report any exceptions that occur - info that might be otherwise lost. You can see all loaded DLLs (Modules window) and also the disassembly as cryptic as it may be.Newcomen
S
3

If what your asking is how do you attach VS to the exe you want to run then you can follow these steps:

  1. Run the executable
  2. In VS navigate to Debug -> Attached to Process
  3. Find your process created by running your executable and click "attach".

However, if the executable you are trying to run fails almost immediately or runs quickly and exits then you could try the following steps:

  1. Set a debug point at the start of the code
  2. Switch your build to Debug and run the application.

If your application is running in Debug, but failing when you execute the exe then you could try these steps to see if the your app will give more information in a console window or other.

  1. Make sure your build is set to Release.
  2. Navigate to Debug -> Start Without Debugging
Sclerophyll answered 17/7, 2018 at 17:56 Comment(0)
T
2

If you have the source code, you can use Debugger.Launch();

You put it anywhere in your code, build the .exe and then once it gets launched (by Process.Start for example) and reaches Debugger.Launch();, a window will be asking you how you want to attach to the process.
enter image description here

Typically, you'll attach to some Visual Studio instance and it'll automatically pause the debugger where Debugger.Launch(); has been placed.

You can then open the project's files (File -> Open -> File...) and place breakpoints wherever you want.

Telophase answered 22/9, 2020 at 12:41 Comment(0)
P
0

This is an old question, but I ran into it since debugging whatever command you wanted in versions of Visual Studio before 2022, even with .netcore projects, could be done via the VS gui: you just went to the project properties went to the debug section, and could enter any exe or batch file command even, as the Executable. But in Visual Studio 2022 this became undocumented, and I had to use Visual Studio 2019 to figure out how to do the same in VS 2022. Here's how (and yes VS 2019 will do this for you, but 2022 will not):

In the launchsettings.json file you will find this line :

"commandName": "Project",

Replace it with these:

"commandName": "Executable",
"executablePath: "c:\myexe.exe",

That commandName property can not be set via the VS gui any more, but it still works, and the gui will reflect it after you set it in the launchSettings.json file; after you do this, you can use the gui to modify the executablePath and commandLineArgs, but not the commandName, other than to reset it to the default.

And to specify arguments (which the 2022 will help you with) add this:

"commandLineArgs": "arg1 arg2"

And that should get you going.

Pasia answered 10/5, 2024 at 15:51 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.