Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
136 views
in Technique[技术] by (71.8m points)

c++ - How to set breakpoint at the very beginning of program execution

How can I stop the program before loading any of the linked DLLs?

I've tried to set LoadLibraryExW function in the Break At Function debugging option and it stops at that function, but before that I have the following in Visual Studio output windows:

'test.exe': Loaded 'C:WindowsSystem32
tdll.dll', Symbols loaded (source information stripped).
'test.exe': Loaded 'C:WindowsSystem32kernel32.dll', Symbols loaded (source information stripped).
'test.exe': Loaded 'C:WindowsSystem32KernelBase.dll', Symbols loaded (source information stripped).
'test.exe': Loaded 'C:WindowsSystem32uxtheme.dll', Symbols loaded (source information stripped).
'test.exe': Loaded 'C:WindowsSystem32msvcrt.dll', Symbols loaded (source information stripped).
---- plus about 30 DLLs ---

So how can I stop the program in the debugger before loading the ntdll.dll? Ok, not before loading, but before executing any of DllMain functions and before initializing any of static objects.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can do this by adding a registry key to "Image File Execution Options" with the name of your exe. Add a value of type string named "Debugger" and set it to vsjitdebugger.exe to launch the just-in-time debugger dialog. Which then lets you pick one of the available debuggers, including Visual Studio. This dialog is triggered right after Windows has loaded the EXE, before any code starts running.

Here's is a sample .reg file that triggers the dialog when you start notepad.exe. Modify the key name to your .exe:

REGEDIT4

[HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionImage File Execution Options
otepad.exe]
"Debugger"="vsjitdebugger.exe"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...