Debug a process that terminates after start - windows

I need to debug a process (starting from an external exe) that terminates immediately after start, so I don't have time to attach. How can I debug it?
UPD I don't have source code of that external exe; I can decompile it, but it's impossible to compile it back

You need to launch your process for debug in a suspended state. Visual Studio is capable of that, just invoke the debugger like this:
devenv /debugexe yourprog.exe <arguments>
The process will start suspended so you'll be able to iterate through first instructions before the crash.
See the detailed answer here.

You just need to open Visual Studio, go to File -> Open -> Project / Solution
and select the exe.
Press F5 and you will see the exception in the Output window. If you go to the Debug -> Exceptions window and select everything you will see the first chance exception before it shutdowns the application.
Note that you don't need the source code at all to do this.
Hope it helps.

You need to start it with the debugger. This will start the program with the debugger already attached.
If you cannot do that with Visual Studio, you can use the Windows Debugging Tools, part of the Windows Driver Kit. Note that the linked kit is for Windows 8.1, you may need to find older versions if you're not on Windows 8.

You can enable debug mode runtime by placing some piece of code.
write a method as below:
[Conditional("DEBUG_SERVICE")]
private static void DebugMode()
{
Debugger.Break();
}
and call this method where you want to start debugging, for example in the OnStart event.
you have to build your code with debug mode. dont forget to remove this piece of code after debugging and want to release.

Related

Visual studio 2015 Cant stop debugging, stop debugging button is disabled, Shif+F5 not working.How can i stop my project?

Cant stop my project in visual studio 2015. stop debugging button is disabled also i cant close my application.stop debugging button disables
When i closing my application that message is showing
I had the same issue when I used some external methods from a system dll.
The code started to execute the external method but it never terminated and the whole thing stucked. I wasn't able to stop it, even in task manager. Every time the code called that problematic ext method, I had to restart my computer to stop it.
Try to find something like this, and disable the call. If you can stop the debugging after that, then it is the problem.
(If you want to know: It was the system32.dll and the problem was caused by a wrong HDD driver.)

Linking Windows Debugger to Project

i coded a big project that runs when I open it in Debug or Release Mode, but when i open it without Debugging (ctrl + f5) it crashs. I searched a long time to find the heap error, but didnt find anything. The problem is i need the running .exe of the programm, so i wanted to ask if there is a possibility to link the windows debugger to the .exe so it always starts with it.
If it doesn't crash right away, maybe this helps:
You can run the executable.
Open your solution in visual studio. Make sure it's the same build.
Open the DEBUG menu and click attach to process.
A window will open, listing all processes that are running. Select the executable that's crashing
Click the DEBUG menu again and select Exceptions (ctrl-alt-E)
Make sure the checkbox "Thrown" is checked for Common Language Runtime Exceptions
Now crash your application.. It will halt at the line that causes it.
Also look for environment directives. like #IF DEBUG #END IF. or #IF RELEASE That kind of stuff. Tricked me a couple of times too..
Good luck. Hope this helps!
You can do various things. First make sure you have a "big out try block" in main. i.e. put the main logic in a try can catch exceptions and report these clearly. This probably isn't what's happening in your case.
You can attach a debugger - including Visual Studio, to a running process - see the "Attach to process" option under the debug menu. If it's built with debug symbols, which you can do, even for release code this may help. If it's optimised you may find it difficult though.
Finally, you could generate a crash dump and inspect that after it's failed. See docs on MiniDumpWriteDump. There are several examples on its usage. Or you can install an abort handler: See here. This mentions _set_abort_behavior which if invoked with _CALL_REPORTFAULT will generate a crash dump too.

Visual Studio debugging program locked in memory

When i am debugging my program and an error occur, the debug session ends, but the program remains in memory. Using the activity manager of Windows to close it does not work. I need to close Visual Studio in order to kill the process.
Why is this happening?
When during debugging a program error occurs, the program usually does not "end". Instead, the debugger (VS2010) pauses execution, allowing you to inspect the code resulting in the error. Depending on the language used (e.g. C#) and the way you compiled your program, you may even be able to edit the program on the fly, move the execution cursor back a bit and continue the program from there.
If the Debug toolbar is visible (in my case it shows up automatically whenever I'm debugging), you should see a couple of "playback" buttons, allowing you to start/continue, pause, stop your program etc. If you stop your program, it will be gone from the task manager too.
As I mentioned in a comment on your question, you can also use the Debug menu to accomplish these tasks.

How to attach debug IIS native module with VS 2010

I am getting difficulty debugging my custom IIS module written in C++, using VS 2010. The problem is that IIS worker process (w3wp.exe) starts too quickly and there is no way to catch it at the beginning in VS "attach to process" dialog. Is there a good skill to do this? Thanks.
You can add a Debugger Breakpoint in your code . I use this all the time to catch things that are too fast to catch manually.
FYi in .net you use System.Diagnostics.Debugger.Break() to achieve the same effect.
It causes the windows system debugger to be activated.
A trick that I have sometimes used is to introduce an artificial infinite loop at the entry point of my code, I then set a breakpoint in this loop and simply move the next instruction to be out of the loop when the breakpoint is hit - job done!

How to start a process and then pause it immediately?

I would like to know if there is any command or tool that can be used to start a process and then pause it immediately.
I need this function so that I can have time to attach a debugger to it. I have tried visual studio's /debugexe feature, but the behavior of the program seemed changed. So I need to find other way to attach it and debug.
Thank you.
You can use CreateProcess() with CREATE_SUSPENDED flag. This will start the process with the main thread suspended. Then you call ResumeThread() after having attached.
In your main routine insert the line:
System.Diagnostics.Debugger.Launch();
Compile in debug mode.
Run your program and it will break and prompt you to attach a debugger. If you have studio open it will ask if you want to use it to debug.
How about adding a Sleep() call as the very first statement.
You can also add a call to
System.Diagnostics.Debugger.Break();
in the code. It will suspend the program and then ask you to choose the debugger you want to use. You can then attach to a running Visual Studio instance. If you're already running from a debugger, it will simply break as if it would have hit a breakpoint.

Resources