How do I control c++ DLL debug symbols loading? - c++

I have an EXE application running in debug mode that loads a DLL at runtime.
When I load the dll it automatically loads the dll's debug symbols for it.
But when I call FreeLibrary() on the dll the symbols won't get unloaded.
That is a problem for me because I use cl.exe to rebuild the DLL while the EXE is running so it needs to be freed and unloaded before I rebuild it.
here is the visual studio runtime debug console output when I load and unload the DLL
'Editor.exe' (Win32): Loaded GameAssembly.dll'. Symbols loaded. <--- automatically loads the symbols when I load the dll
'Editor.exe' (Win32): Unloaded GameAssembly.dll' <--- Dosen't unload the symbols (I know so because when I try to rebuild the dll I get the error (LNK1201 error writing to program database 'GameAssembly.pdb'; check for insufficient disk space, invalid path, or insufficient privilege)
and the functions I use to load and free the dll:
m_DLL = LoadLibraryA("GameAssembly.dll");
FreeLibrary((HMODULE)m_DLL);
now I want a way to unload the symbols when I free the dll, Any idea on how it's done ?

The symbols are not loaded in the app itself, they are loaded in Visual Studio's debugger. The good news is that what you are trying to do works just fine as long as you are not debugging the application.
Unfortunately, there is no way to unload symbols from a debugging session (as far as I know).
One way to fix this would be to tell Visual Studio to simply not load those symbols in the first place. You can do that from the modules window. However, this will prevent you from debugging GameAssembly.dll.
So the only alternative is to not be debugging when you rebuild/reload the dll. To do this, you have to create multiple debugging sessions for a single run of your app. Here's how you go about it
Start debugging the application as you normally would.
When you want to rebuild the dll:
Unload the dll
Detach the debugger by using Debug -> Detach All
Rebuild the DLL
Restart debugging the app by using Debug -> Attach to process
Reload the DLL
The order of the steps can be swapped around a bit, of course. As long as you detach before rebuilding, and don't reload the dll until after the build is done, you should be ok.

Related

PDB debug symbols not loaded during hotload

I'm trying to implement hot reloading for a C++ app that loads a DLL dynamically.
When the app runs (in debug mode), it will copy the DLL and associated PDB to an intermediate directory, and load that.
This is so that in subsequent compiles, the original files can be overwritten.
Since DLLs store the path to their PDB in the DLL header, the app patches the DLL header to point to the correct PDB.
To make sure the DLL size remains exactly the same, I only override 1 character of the PDB name.
When compiling the DLL, I set the PDB path to something like the following (using the /PDBALTPATH linker flag):
Z:/NGEN/Stage/Tmp/TheName._.pdb
Then, when copying the DLL to the Tmp directory, I patch the name to something like:
Z:/NGEN/Stage/Tmp/TheName.a.pdb
Z:/NGEN/Stage/Tmp/TheName.b.pdb
I verified that the DLLs have the correct patched PDB name after copying, using the dumpbin program.
Now, this works fine when I first run the app; the debug symbols are loaded correctly. However, when I rebuild the DLL (in another instance of MSVC) and the app loads the new DLL, MSVC doesn't load the debug symbol of the new DLL.
Instead, I get a message that different versions of the symbols have already been loaded.
Why does MSVC not load the PDB associated with the second DLL?
For your reference: Why does Visual Studio require debugger symbol files to exactly match the binary files that they were built with?
an internal time-stamp will be different. But even disregarding that,
the actual layout of the code could be
different.

Remote DLL Debugging from ctypes in Visual Studio

I'm making a dll for python using c++ and importing functions using ctypes library.
I know that to do remote debugging you go to Debug -> Attach to Process.
I put a breakpoint in python code before imported function is called, but I don't know what process to attach to. I tried attaching to python.exe, but that didn't work.
What process should I attach to or do I need to do remote debugging differently and if so then how?
Thanks
The process which need to be attached to is the process that needs to call the dll. If you want to remote debug dll, I suggest that you could refer to this link about Remote Debugging a C++ Project in Visual Studio. Also, you could refer to this link about Debug DLLs in Visual Studio.
I suggest that you could look in the modules windows. There are several situations:
1.No Symbols have been loaded for this document
Go to the Modules window (Debug > Windows > Modules) and check whether your module is loaded.
If your module is loaded, check the Symbol Status column to see
whether symbols have been loaded.
a.If symbols are not loaded, check the symbol status to diagnose the issue. From the context menu on a module in the Modules window, click Symbol Load Information... to see where the debugger looked to try and load symbols. For more information about loading symbols, see Specify Symbol (.pdb) and Source Files.
b.If symbols are loaded, the PDB does not contain information about your source files. These are a few possible causes:
If your source files were recently added, confirm that an up-to-date version of the module is being loaded.
It is possible to create stripped PDBs using the /PDBSTRIPPED linker option. Stripped PDBs do not contain source file information. Confirm you are working with a full PDB and not a stripped PDB.
The PDB file is partially corrupted. Delete the file and perform a clean build of the module to try to resolve the issue.
If your module is not loaded, check the following to find the cause:
a.Confirm that you are debugging the right process.
b.Check to see that you are debugging the right kind of code. You can find out what type of code the debugger is configured to debug in the Processes window (Debug > Windows > Processes). For example, if you are trying to debug C# code, confirm that your debugger is configured for the appropriate type and version of .NET (for example, Managed (v4*) versus Managed (v2*/v3*) versus Managed (CoreCLR)).
2.… the current source code is different from the version built into...
You could go to Debug > Options and Settings. On the Debugging/General page, clear the Require source files that exactly match the original version option. Make sure to reenable this option when you are finished debugging.
In addition, it may be that the dll has not been loaded into the .exe. You could set Properties->Debugging->Working Directory.

Any ideas on how to fix a Dll Initialization Error in Visual Studio 2019?

New to coding and while I was working on c++ console application, all of a sudden I was unable to open the application for debugging. It simply pops up and says "The application was unable to start correctly (0xc0000142)." No error/warning codes on visual studio either. Spent the last hour trying various different solutions I've read on forums and such. Fully updated windows, updated nvidia drivers, changed values in regedit, checked firewall, etc. Only thing left I can think of is to run sfc and possibly have to reinstall windows (which I don't want to do). In the output on visual studio after closing the pop up, it shows that this is due to a 'DLL Initialization' failure.
'tetris.exe' (Win32): Loaded 'C:\Windows\System32\ntdll.dll'. Symbols loaded.
'tetris.exe' (Win32): Loaded 'C:\Windows\System32\kernel32.dll'. Symbols loaded.
'tetris.exe' (Win32): Loaded 'C:\Windows\System32\KernelBase.dll'. Symbols loaded.
'tetris.exe' (Win32): Unloaded 'C:\Windows\System32\KernelBase.dll'
'tetris.exe' (Win32): Unloaded 'C:\Windows\System32\kernel32.dll'
The thread 0x3678 has exited with code -1073741502 (0xc0000142).
The program '[7708] tetris.exe' has exited with code -1073741502 (0xc0000142) 'DLL Initialization Failed'.
It shows my application successfully loading kernal32.dll and KernalBase.dll, but ntdll.dll is loading but not showing that its unloaded. Not totally sure if that's related though. Any help is much appreciated!
Error 0xC0000142 is STATUS_DLL_INIT_FAILED which probably comes from a missing dependency DLL or a missing function export in a dependency (for example from having an unexpected DLL version that doesn't export the expected function).
Try looking at your event log (eventvwr.exe) for any loader messages which may tell you the dependency that was missing or failed to load.
Use the Dependencies tool to analyze the dependency chain of your application for any missing DLL or export.
Try enabling "Loader Snaps" for your EXE filename using GFlags.exe and then check the debug output in Visual Studio for indications about what DLL failed to load.
Try using Process Monitor with a filter set to path containing DLL and process name matching your EXE filename to see what DLL files are being loaded or attempted to be loaded and look for the last NAME NOT FOUND chain not ending in a SUCCESS.
I had the same problem. If you are using the Windows terminal as debug console, try switching to the base cmd. I solved with this method.

C++, attach to process, breakpoint won't be hit

I have a big application and I want to debug a given class. So I compile the DLL the class is in, in release and replace, in the application setup directory, the DLL and also add its PDB. I cannot do it in Debug since the application then does not work properly.
After doing that, I start the application and attach to it with the VS debugger. But my four breakpoints in the four methods I wish to debug exhibit the message "The breakpoint will not currently be hit. No symbols have been loaded for this document".
In an unsuccessful attempt to fix this, I have added the pdb I have copied to the installation directory of my application to the pdb locations in Debug | Options and Settings | Debugging | Symbols.
Why is this happening, can something be done?

breakpoint will not currently be hit. no symbol have been loaded in dll in c++

I write a dll in c++ and another exe file in c++. I inject my dll with my exe in cpp to for example wordpad.exe. but that does not do what i want so i want to debug my dll.
I write both my dll and my exe in one solution and when i put the breakpoint in both of them, my exe start to debug but the breakpoint in my dll get error that said "the breakpoint will not currently be hit. no symbol have been loaded".
I do most of the solution in internet and put my dll pdb file in the right folder but it is not working.
by the way I write all of my projects in visual studio 2010.
can anyone help me please?
What I understand is that you want to debug your dll code after injecting it into wordpad.exe.
You should attach the visual studio debugger to the wordpad.exe process because that's the process where the dll will be loaded.