{"id":23730939,"url":"https://github.com/kevingosse/silhouette","last_synced_at":"2026-04-02T21:35:33.457Z","repository":{"id":40641945,"uuid":"502398329","full_name":"kevingosse/Silhouette","owner":"kevingosse","description":"A library to build .NET profilers in .NET","archived":false,"fork":false,"pushed_at":"2025-01-16T20:47:13.000Z","size":458,"stargazers_count":133,"open_issues_count":1,"forks_count":14,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-04-04T14:51:16.605Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/kevingosse.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2022-06-11T16:20:42.000Z","updated_at":"2025-03-15T19:45:32.000Z","dependencies_parsed_at":"2023-02-08T07:15:35.695Z","dependency_job_id":"56386625-f4bf-4fad-9f12-cc6fe0dee791","html_url":"https://github.com/kevingosse/Silhouette","commit_stats":null,"previous_names":["kevingosse/silhouette"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevingosse%2FSilhouette","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevingosse%2FSilhouette/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevingosse%2FSilhouette/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/kevingosse%2FSilhouette/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/kevingosse","download_url":"https://codeload.github.com/kevingosse/Silhouette/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247198446,"owners_count":20900079,"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","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":[],"created_at":"2024-12-31T03:09:42.024Z","updated_at":"2026-03-02T22:19:24.930Z","avatar_url":"https://github.com/kevingosse.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"Silhouette - A library to build .NET profilers in .NET\n=======================\n\n# Quick start\n\nCreate a new C# NativeAOT project. Reference the Silhouette nuget package and add a class inheriting from `Silhouette.CorProfilerCallback11Base` (you can use a different version of `CorProfilerCallbackBase` depending on the version of .NET you're targeting). Override the `Initialize` method. It will be called with the highest version number of `ICorProfilerInfo` supported by the target runtime.\n\n```csharp\n\nusing Silhouette;\n\n[Profiler(\"0A96F866-D763-4099-8E4E-ED1801BE9FBC\")] // Use your own profiler GUID here\ninternal partial class CorProfilerCallback : CorProfilerCallback11Base\n{\n    protected override HResult Initialize(int iCorProfilerInfoVersion)\n    {\n        if (iCorProfilerInfoVersion \u003c 11)\n        {\n            return HResult.E_FAIL;\n        }\n\n        var result = ICorProfilerInfo11.SetEventMask(COR_PRF_MONITOR.COR_PRF_ENABLE_STACK_SNAPSHOT | COR_PRF_MONITOR.COR_PRF_MONITOR_THREADS);\n\n        return result;\n    }\n}\n```\n\nThe `Profiler` attribute triggers a source-generator that emits the proper `DllGetClassObject` function and validates that the user is using the matching guid for the profiler. Alternatively, you can manually implement a `DllGetClassObject` method that will be called by the .NET runtime when initializing the profiler. Use the built-in `ClassFactory` implementation and give it an instance of your `CorProfiler` class.\n\n```csharp\nusing Silhouette;\nusing System.Runtime.InteropServices;\n\ninternal class DllMain\n{\n     // This code is automatically generated when using the `[Profiler]` attribute on `CorProfilerCallback`\n    [UnmanagedCallersOnly(EntryPoint = \"DllGetClassObject\")]\n    public static unsafe HResult DllGetClassObject(Guid* rclsid, Guid* riid, nint* ppv)\n    {\n        // Use your own profiler GUID here\n        if (*rclsid != new Guid(\"0A96F866-D763-4099-8E4E-ED1801BE9FBC\"))\n        {\n            return HResult.CORPROF_E_PROFILER_CANCEL_ACTIVATION;\n        }\n\n        *ppv = ClassFactory.For(new CorProfilerBase());\n\n        return HResult.S_OK;\n    }\n}\n```\n\n`CorProfilerXxBase` offers base virtual methods for all `ICorProfilerCallback` methods, so override the ones you're interested in:\n\n```csharp\n    protected override HResult ThreadCreated(ThreadId threadId)\n    {\n        Console.WriteLine($\"Thread created: {threadId.Value}\");\n        return HResult.S_OK;\n    }\n```\n\nUse the `ICorProfilerInfoXx` fields to access the `ICorProfilerInfo` APIs:\n\n```csharp\n    private unsafe string ResolveMethodName(nint ip)\n    {\n        try\n        {\n            var functionId = ICorProfilerInfo11.GetFunctionFromIP(ip).ThrowIfFailed();\n            var functionInfo = ICorProfilerInfo2.GetFunctionInfo(functionId).ThrowIfFailed();\n            using var metaDataImport = ICorProfilerInfo2.GetModuleMetaDataImport(functionInfo.ModuleId, CorOpenFlags.ofRead).ThrowIfFailed().Wrap();\n            var methodProperties = metaDataImport.Value.GetMethodProps(new MdMethodDef(functionInfo.Token)).ThrowIfFailed();\n            var typeDefProps = metaDataImport.Value.GetTypeDefProps(methodProperties.Class).ThrowIfFailed();\n\n            return $\"{typeDefProps.TypeName}.{methodProperties.Name}\";\n        }\n        catch (Win32Exception)\n        {\n            return \"\u003cunknown\u003e\";\n        }\n    }\n```\n\nMost methods return an instance of `HResult\u003cT\u003e`. You can deconstruct it into a `(HResult error, T result)` and manually check the error code. You can also use the `ThrowIfFailed()` method that will return only the result and throw a `Win32Exception` if the error code is not `S_OK`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevingosse%2Fsilhouette","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkevingosse%2Fsilhouette","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkevingosse%2Fsilhouette/lists"}