How to debug PhysX?

TL;DR What is the proper way to get UE4 to link to the debug version of PhysX?

I am attempting to work with the PhysX source code. I just want to be able to place breakpoints and step through the code. Mostly to dig around and maybe tweak a few things. I stumbled upon the PhysX project files in:

UnrealEngine\Engine\Source\ThirdParty\PhysX\PhysX-3.3\Source\compiler\vc12win64

Then to debug the physics engine I can run UE4, open the PhysX project, and attach the debugger to UE4. However there was a problem when stepping through the code (line by line), the debugger jumps all over the place across several lines of code. This happens because the PhysX code has been mostly optimized away because UE4 is using the production build of PhysX.

So clearly the solution is to get UE4 to use the debug build of PhysX.
I found the PhysX dll files in:

UnrealEngine\Engine\Binaries\ThirdParty\PhysX\PhysX-3.3\Win64\VS2013

There is PhysX3DEBUG_x64.dll and PhysX3PROFILE_x64.dll. I confirmed that UE4 is using the profile build by deleting PhysX3PROFILE_x64.dll and checking if UE4 complains, which it does.

Then In the UE4 source I found that the PhysX dll is loaded in PhysXLibs.cpp:

#if UE_BUILD_DEBUG && !defined(NDEBUG)	// Use !defined(NDEBUG) to check to see if we actually are linking with Debug third party libraries (bDebugBuildsActuallyUseDebugCRT)

  PhysX3CommonHandle = LoadLibraryW(*(RootPhysXPath + "PhysX3CommonDEBUG_x64.dll"));
  nvToolsExtHandle = LoadLibraryW(*(RootPhysXPath + "nvToolsExt64_1.dll"));
  PhysX3Handle = LoadLibraryW(*(RootPhysXPath + "PhysX3DEBUG_x64.dll"));
  #if WITH_PHYSICS_COOKING || WITH_RUNTIME_PHYSICS_COOKING
    PhysX3CookingHandle = LoadLibraryW(*(RootPhysXPath + "PhysX3CookingDEBUG_x64.dll"));
  #endif
...
#else	//UE_BUILD_DEBUG

  PhysX3CommonHandle = LoadLibraryW(*(RootPhysXPath + "PhysX3CommonPROFILE_x64.dll"));
  nvToolsExtHandle = LoadLibraryW(*(RootPhysXPath + "nvToolsExt64_1.dll"));
  PhysX3Handle = LoadLibraryW(*(RootPhysXPath + "PhysX3PROFILE_x64.dll"));
  #if WITH_PHYSICS_COOKING || WITH_RUNTIME_PHYSICS_COOKING
    PhysX3CookingHandle = LoadLibraryW(*(RootPhysXPath + "PhysX3CookingPROFILE_x64.dll"));
  #endif

From that code I figure that I need to enable UE_BUILD_DEBUG and undefine NDEBUG.
I figured that changing the project configuration to “Debug” would do both these things. But it didn’t.
So instead I tried going into UE4’s ‘Configuration Properties/NMake/Preprocessor Definitions’ and forcibly changing the line: ‘UE_BUILD_DEVELOPMENT=1’ with ‘UE_BUILD_DEBUG=1’ and removed the line ‘NDEBUG=1’

Then I rebuilt UE4. Now, in order to confirm that I really am using PhysX3DEBUG_x64.dll instead of PhysX3PROFILE_x64.dll:

I deleted PhysX3PROFILE_x64.dll but the engine complained about not finding PhysX3PROFILE_x64.dll

Then I did the opposite and deleted PhysX3DEBUG_x64.dll but the engine did not complain about not finding PhysX3DEBUG_x64.dll

Question: So what is the proper way to get UE4 to link to the debug version of PhysX?

P.S. I’m using version 4.8 commit: 1933f7b14b1ff111916a3b0d0a7f45736654d3f9

This question was solved by Gordon, an NVIDIA developer, at their dev forums:

@Mastodon : Unfortunately, the link is not working any longer.

Does anybody know how to debug PhysX?

The link is working fine for me, I also checked if it worked on friend’s machines and it works fine for them too. Without having an NVIDIA dev forum account.

But if it still doesn’t work for you try visiting the NVIDIA Developer Forums:

then go to “Visual and Game Development” > “PhysX and Physics Modeling” then my thread is “Visual Studio debugger on the PhysX code running inside UE4” by “Distro16”

Just in case, anyone still cannot access the link above… here is the post which solves it:-

You can compile UE4 with all third party libs in debug. In BuildConfiguration.cs just set

bDebugBuildsActuallyUseDebugCRT = true;

and recompile.

You can also compile with just physx in debug by modifying GetPhysXLibraryMode in PhysXBuild.cs. You will then need to make sure that the dlls also load with debug versions. This is done in LoadPhysXModules in PhysXLibs.cpp. The code should automatically load the correct dlls but if you get a crash it is likely that it has loaded profile dlls instead of the debug ones. It is straightforward to change the code to fix this.

With these changes you should be able to run in Development Editor config and have physx running in debug.

Cheers,

Gordon