{"id":27116877,"url":"https://github.com/mvenditto/managedcorprofiler","last_synced_at":"2025-09-06T02:41:30.513Z","repository":{"id":166185778,"uuid":"584205688","full_name":"mvenditto/ManagedCorProfiler","owner":"mvenditto","description":"Prototype NET (CLR) Profiler written in C#","archived":false,"fork":false,"pushed_at":"2024-02-10T20:49:45.000Z","size":501,"stargazers_count":25,"open_issues_count":0,"forks_count":1,"subscribers_count":4,"default_branch":"master","last_synced_at":"2025-04-07T05:52:55.206Z","etag":null,"topics":["clr","coreclr","csharp","dotnet","icorprofiler","icorprofilercallback","nativeaot","net6","net7","profiler"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mvenditto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2023-01-01T20:32:07.000Z","updated_at":"2025-01-13T13:37:16.000Z","dependencies_parsed_at":"2024-02-10T06:30:52.235Z","dependency_job_id":"2227cfe9-9278-4afe-8c2e-5c385f003eb1","html_url":"https://github.com/mvenditto/ManagedCorProfiler","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/mvenditto/ManagedCorProfiler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvenditto%2FManagedCorProfiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvenditto%2FManagedCorProfiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvenditto%2FManagedCorProfiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvenditto%2FManagedCorProfiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mvenditto","download_url":"https://codeload.github.com/mvenditto/ManagedCorProfiler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mvenditto%2FManagedCorProfiler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273850166,"owners_count":25179298,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-06T02:00:13.247Z","response_time":2576,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clr","coreclr","csharp","dotnet","icorprofiler","icorprofilercallback","nativeaot","net6","net7","profiler"],"created_at":"2025-04-07T05:53:02.940Z","updated_at":"2025-09-06T02:41:30.464Z","avatar_url":"https://github.com/mvenditto.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ManagedCorProfiler\n\nA prototype CLR profiler written in C# for learning and fun.\n\n\u003e [!WARNING]\n\u003e 🚧 WIP WIP WIP 🚧\n\n## Building blocks\n- [NativeAOT](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/) native compilation to build and link the native profiler library.\n- [ComWrappers API](https://learn.microsoft.com/en-us/dotnet/api/system.runtime.interopservices.comwrappers?view=net-7.0) to expose the `ICorProfilerCallbackX` to the CLR.\n- [CsWin32](https://github.com/microsoft/CsWin32) to generate C# bindings for the native [Profiling API](https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/).\n\n## Exposing a profiler callback\n```csharp\n///\n/// A sample profiler callback that prints the loaded modules\n///\n[ProfilerCallback(\"1E040027-162F-489B-B12F-F113E6AF40CF\")]\ninternal unsafe class MyProfiler : CorProfilerCallback2\n{\n    private ICorProfilerInfo2* _corProfilerInfo;\n\n    public override HRESULT Initialize(IUnknown* pICorProfilerInfoUnk)\n    {\n        // Query for a pointer to the ICorProfilerCallback2 interface\n        var hr = pICorProfilerInfoUnk-\u003eQueryInterface(ICorProfilerInfo2.IID_Guid, out var pinfo);\n\n        if (hr.Failed)\n        {\n            Console.WriteLine($\"FAIL QueryInterface hr={hr}\");\n            return HRESULT.E_FAIL;\n        }\n\n        // Track our reference\n        _corProfilerInfo = (ICorProfilerInfo2*)pinfo;\n        _corProfilerInfo-\u003eAddRef();\n\n        // Specify our profiler is interested in module load events (Module* callbacks)\n        hr = _corProfilerInfo-\u003eSetEventMask((uint)COR_PRF_MONITOR.COR_PRF_MONITOR_MODULE_LOADS);\n\n        if (hr.Failed)\n        {\n            Console.WriteLine($\"FAIL SetEventMask hr={hr}\");\n            return HRESULT.E_FAIL;\n        }\n\n        return HRESULT.S_OK;\n    }\n\n    public override HRESULT ModuleLoadFinished(nuint moduleId, HRESULT hrStatus)\n    {\n        if (hrStatus.Failed)\n        {\n            return HRESULT.S_OK;\n        }\n\n        const int NameBufferLength = 256; // char\n\n        // Allocate a buffer to hold the module name.\n        using var szNameBuffer = NativeBuffer\u003cchar\u003e.Alloc(NameBufferLength);\n\n        // A pointer to a string of wide-characters to pass in input to GetModuleInfo.\n        var szName = new PWSTR(szNameBuffer.Pointer);\n\n        uint pcchName = 0;\n\n        // Retrieve the file name of the module\n        var hr = _corProfilerInfo-\u003eGetModuleInfo(\n            moduleId,            // the target moduleId\n            null,\n            NameBufferLength,    // The length, in characters, of the szName return buffer\n            \u0026pcchName,           // A pointer to the total character length of the module's file name that is returned\n            szName,              // A caller-provided wide character buffer\n            null);\n\n        if (hr.Failed)\n        {\n            Console.WriteLine($\"FAIL GetModuleInfo hr={hr}\");\n            return hr;\n        }\n\n        var moduleName = szName.CopyToString(length: (int)pcchName);\n\n        Console.WriteLine($\"loaded module 0x{moduleId:x8} \u003c{moduleName}\u003e\");\n\n        return HRESULT.S_OK;\n    }\n\n    public override HRESULT Shutdown()\n    {\n        _corProfilerInfo-\u003eRelease();\n        return HRESULT.S_OK;\n    }\n}\n```\n\n### Sample output\n\u003cpre\u003e\u003csamp\u003eC:\\ManagedCorProfiler\\Samples\\ModuleLoadsProfiler\u003e \u003ckbd\u003e.\\run.cmd\u003c/kbd\u003e\n[... OMITTED ...]\nLoaded Module -\u003e 0x7ffeac1b4000 C:\\Users\\dev\\Source\\Repos\\runtime\\artifacts\\bin\\coreclr\\windows.x64.Debug\\System.Private.CoreLib.dll\nLoaded Module -\u003e 0x7ffeac702148 C:\\ManagedCorProfiler\\Samples\\SampleApp\\bin\\Debug\\net8.0\\SampleApp.dll\nLoaded Module -\u003e 0x7ffeac703e40 C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.0\\system.runtime.dll\nLoaded Module -\u003e 0x7ffeac8f9798 C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.0\\system.console.dll\nLoaded Module -\u003e 0x7ffeac8fc1f0 C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.0\\system.threading.dll\nLoaded Module -\u003e 0x7ffeac9317d0 C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.0\\system.text.encoding.extensions.dll\nLoaded Module -\u003e 0x7ffeac9389a0 C:\\Program Files\\dotnet\\shared\\Microsoft.NETCore.App\\8.0.0\\system.runtime.interopservices.dll\nHello World!\nC:\\ManagedCorProfiler\\Samples\\ModuleLoadsProfiler\u003e █\u003c/samp\u003e\u003c/pre\u003e\n\n## Compilation\n\n\u003cpicture\u003e\n\u003csource\n  srcset=\"/docs/images/compilation-dark.svg\"\n  media=\"(prefers-color-scheme: dark)\"\n/\u003e\n\u003csource\n  srcset=\"/docs/images/compilation-light.svg\"\n  media=\"(prefers-color-scheme: light), (prefers-color-scheme: no-preference)\"\n/\u003e\n\u003cimg src=\"/docs/images/compilation-light.svg\" width=\"800\"\u003e\n\u003c/picture\u003e\n\n## Dumpbin of the profiler DLL\n\u003cpre\u003e\u003csamp\u003eC:\\ManagedCorProfiler\\Samples\\ModuleLoadsProfiler\u003e \u003ckbd\u003edumpbin.exe /EXPORTS bin\\Release\\net8.0\\publish\\win-x64\\ModuleLoadsProfiler.dll\u003c/kbd\u003e\n[...OMITTED FOR BREVITY...]\n    ordinal hint RVA      name\n          1    0 00232660 DllCanUnloadNow = DllCanUnloadNow\n          2    1 002323A0 DllGetClassObject = DllGetClassObject\n          3    2 002326A0 DllMain = DllMain\n[...OMITTED FOR BREVITY...]\nC:\\ManagedCorProfiler\\Samples\\ModuleLoadsProfiler\u003e █\u003c/samp\u003e\u003c/pre\u003e\n\n## ELT Hooks\n.\n\n## How to Build\n.\n### Requirements\n.\n\n## Contributing\nAny contribution is welcome.\nI'm actually working on porting the tests at [dotnet/runtime/tree/main/src/tests/profiler](https://github.com/dotnet/runtime/tree/main/src/tests/profiler),\nthis is a good place to start contributing.\n\n## Resources\n### Misc\n- [Writing a .NET Profiler in C# by Kevin Gosse](https://minidump.net/writing-a-net-profiler-in-c-part-1-d3978aae9b12)\n- [https://github.com/dotnet/runtime](https://github.com/dotnet/runtime)\n### COM / COM Interop\n- [DllGetClassObject](https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-dllgetclassobject)\n- [ComWrappers Tutorial](https://learn.microsoft.com/en-us/dotnet/standard/native-interop/tutorial-comwrappers)\n- [ComWrappers interop sample](https://github.com/dotnet/samples/blob/main/core/interop/comwrappers/Tutorial/Program.cs)\n### Profiling\n- [Profiling Overview](https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/profiling-overview)\n- [Unmanaged profiling API](https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/)\n- [CoreCLR Profilin BOTR](https://github.com/dotnet/runtime/blob/main/docs/design/coreclr/botr/profiling.md)\n- [clr-sample ProfilingAPI](https://github.com/mvenditto/clr-samples/tree/master/ProfilingAPI)\n- [Profiling Interfaces](https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/profiling-interfaces)\n- [Profiling global static function](https://learn.microsoft.com/en-us/dotnet/framework/unmanaged-api/profiling/profiling-global-static-functions)\n### Native AOT\n- [NativeAOT Interop](https://github.com/dotnet/runtime/blob/main/src/coreclr/nativeaot/docs/interop.md)\n- [Deploying NativeAOT](https://learn.microsoft.com/en-us/dotnet/core/deploying/native-aot/)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmvenditto%2Fmanagedcorprofiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmvenditto%2Fmanagedcorprofiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmvenditto%2Fmanagedcorprofiler/lists"}