Skip to content

Commit 0f107d1

Browse files
authored
[mono] Add more detailed logs for the PInvoke resolution process (#41228)
Similar logging was added to the managed loading algorithm when it proved extremely useful for debugging a Blazor crash, so the equivalent is probably useful for the unmanaged library algorithm as well
1 parent c0a419d commit 0f107d1

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/mono/mono/metadata/native-library.c

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -772,6 +772,8 @@ netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, c
772772
g_free (error_msg);
773773
}
774774

775+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via __Internal: '%s'.", scope);
776+
775777
return module;
776778
}
777779

@@ -796,16 +798,22 @@ netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, c
796798
alc_pinvoke_lock (alc);
797799
module = netcore_check_alc_cache (alc, scope);
798800
alc_pinvoke_unlock (alc);
799-
if (module)
801+
if (module) {
802+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found in the active ALC cache: '%s'.", scope);
800803
goto leave;
804+
}
801805

802806
module = (MonoDl *)netcore_resolve_with_dll_import_resolver_nofail (alc, assembly, scope, flags);
803-
if (module)
807+
if (module) {
808+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via DllImportResolver: '%s'.", scope);
804809
goto add_to_alc_cache;
810+
}
805811

806812
module = (MonoDl *)netcore_resolve_with_load_nofail (alc, scope);
807-
if (module)
813+
if (module) {
814+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via LoadUnmanagedDll: '%s'.", scope);
808815
goto add_to_alc_cache;
816+
}
809817

810818
MONO_ENTER_GC_SAFE;
811819
mono_global_loader_data_lock ();
@@ -814,18 +822,24 @@ netcore_lookup_native_library (MonoAssemblyLoadContext *alc, MonoImage *image, c
814822
MONO_ENTER_GC_SAFE;
815823
mono_global_loader_data_unlock ();
816824
MONO_EXIT_GC_SAFE;
817-
if (module)
825+
if (module) {
826+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found in the global cache: '%s'.", scope);
818827
goto add_to_alc_cache;
828+
}
819829

820830
module = netcore_probe_for_module (image, scope, flags);
821-
if (module)
831+
if (module) {
832+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via filesystem probing: '%s'.", scope);
822833
goto add_to_global_cache;
834+
}
823835

824836
/* As this is last chance, I've opted not to put it in a cache, but that is not necessarily the correct decision.
825837
* It is rather convenient here, however, because it means the global cache will only be populated by libraries
826838
* resolved via netcore_probe_for_module and not NativeLibrary, eliminating potential races/conflicts.
827839
*/
828840
module = netcore_resolve_with_resolving_event_nofail (alc, assembly, scope);
841+
if (module)
842+
mono_trace (G_LOG_LEVEL_DEBUG, MONO_TRACE_DLLIMPORT, "Native library found via the Resolving event: '%s'.", scope);
829843
goto leave;
830844

831845
add_to_global_cache:

0 commit comments

Comments
 (0)