Free resource during debugging (C ++, MSVC)

Is it possible to free a resource (file lock, timer) when the program is stopped in the debugger?

In general, could I execute the code before MSVC stopped the program for debugging and after resuming execution?

I would like to do the following: - Automatically release file locks for editing during debugging. - Automatically reloads the file after resuming the process - Prevent the start of timers that overflow while debugging is stopped - Subtract the duration of debugging from the timers

Workaround: if I can’t execute the code before the program is stopped by the debugger, then program programming in the program that the debugger stopped the application will already help a lot.


Edit: I looked at the Windows API functions for integrating the debugger, but it seems like functions like ContinueDebugEventthat are intended only for the debugger writer and never for the debugged process.


Edit 2: It seems that the hotpatching function of DbgBreakPoint may be a way to intercept when the debugger wants to break the process.

The main idea of ​​attaching is that the debugger calls the "DebugActiveProcess" function, which ends with a call to "RtlCreateUserThread" to create a new remote thread in the target process, with the "DbgUiRemoteBreakin" function as the new entry point for the stream.

(from http://waleedassar.blogspot.de/2011/12/debuggers-anti-attaching-techniques.html )

DbgUiRemoteBreakin appears to be invoking DbgBreakPoint to actually stop the process.


3: DbgBreakPoint DebugBreak, , MSVC, . , ! , MSVC int 3 .


4: DebugBreak : 3 , ( ). , .

+4
6

: , .

( ), ?

. Process Explorer . , , .

, , MSVC ?

, , , , . . , .

, .

, OF_SHARE_DENY_NONE.

+1

, , , - .

bool executeMe = false;

if(executeMe) {
    // Code to release lock / close file ...
}

// ... normal code

executeMe true if.

EDIT: .etc. , , .

EDIT:

WRT, __debugbreak()? , , - , , .

bool debugging = false;

struct stat unused;
if(!stat('.\\.debugflag',&unused)) {
    // Release locks, Close files, Stop timers .etc.
    // ...
    // Hook debuger. At this point you should be right to debug
    debugging = true;
    __debugbreak();  
}

// Code to debug ....

if(debugging) {
    // Cleanup after debug
}
0

, .

MS Visual Studio , API

BOOL WINAPI IsDebuggerPresent(void);

MSDN: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680345%28v=vs.85%29.aspx

.

0

, ​​

void break( resource r1)
{
   release resource;
   throw false;
   rebind resource;
}

break, bool. , bool, MSVC . / , "", .

, , , , ,

0

SetUnhandledExceptionFilter , 3 ( )?

, EXCEPTION_CONTINUE_SEARCH, ?

, , EXCEPTION_RECORD:: ExceptionCode == EXCEPTION_BREAKPOINT

0

, , , - ( ) 0xCC ( int 3) / , , DebugBreakpoint().

, 0xCC int3 , /dll, , 0xCC . , QueueUserAPC(), , , int3, int3 int3.

: , 0xCC, .

0

All Articles