Skip to content
← All posts
· Delta1 Labs DebuggingNebula

Keep debugger displays working after renaming

A [DebuggerDisplay] format string is a compile-time literal that survives obfuscation untouched — yet the watch window still shows {CustomerName} #{Id} or an error after you rename. This post shows why: the string is not text the debugger prints, it is a tiny expression the debugger's evaluator binds by member name at break time, so renaming the members it names makes the lookup miss. Then it scopes Nebula.NET so your own staging builds stay readable in the debugger without leaving those names in the shipped binary.

You attach the debugger to your own staging build to chase a bug. You hover an Invoice, and the watch window — which last release read Acme #4821 — now shows the literal text {CustomerName} #{Id}, or a red error CS1061: 'a' does not contain a definition for 'CustomerName'. The object is intact. Every field has the right value. Obfuscation did exactly what you asked. The debugger just lost the one thing you told it to show.

The real question

A [DebuggerDisplay] format string is a compile-time string literal. Renaming does not touch string literals — that is the whole reason a record’s ToString() or a Serilog message template survives obfuscation. So if the format string is intact, why is the display broken? Because the string is not text the debugger prints. It is a tiny program the debugger runs at break time, and that program looks up members by name — which is precisely the thing renaming rewrites.

The format string is a literal — and that is the trap

Take an ordinary annotated type.

[DebuggerDisplay("{CustomerName} #{Id}")]
public sealed class Invoice
{
    public string CustomerName { get; init; }
    public int Id { get; init; }
    public decimal Total { get; init; }
}

The attribute is stored in metadata as a custom-attribute row on Invoice, and the constructor argument — your format string — lives as length-prefixed UTF-8 bytes in the #Blob heap:

.custom instance void [System.Private.CoreLib]
        System.Diagnostics.DebuggerDisplayAttribute::.ctor(string) = (
    01 00 14 7B 43 75 73 74 6F 6D 65 72 4E 61 6D 65   // ...{CustomerName
    7D 20 23 7B 49 64 7D 00 00 )                       // } #{Id}..
)

Those 7B 43 75 73 74… bytes spell {CustomerName. They are an opaque constructor argument, not a metadata name — the obfuscator has no reason (and no safe way) to reach inside an arbitrary attribute blob and rewrite a substring there. So when Nebula renames the property row CustomerName to b, the format string keeps saying CustomerName. The literal survives perfectly. That is the problem, not the cure.

What the debugger does with it

At a breakpoint, Visual Studio’s expression evaluator reads the DebuggerDisplay attribute, splits the format on its {…} segments, and compiles each segment as an expression evaluated against the instance — this.CustomerName and this.Id. It binds those identifiers against the metadata of the assembly that is actually loaded in the process: the renamed one. There is no member called CustomerName on the loaded type anymore; it is called b. The bind fails, and the evaluator reports an error for that segment or falls back to showing the raw braces. The value "Acme" is sitting right there in field b — the debugger simply was not told to look there.

inv : Invoice(stopped at breakpoint)attribute blob — survives"{CustomerName} #{Id}"literal UTF-8 in #Blob heaprenaming never rewrites itproperty rows — renamedCustomerName → b Id → cthe live metadata names changethis is what the evaluator seesevaluator binds {CustomerName} → member 'CustomerName' → misswatch window shows: {CustomerName} #{Id}
The format string survives renaming because it is an opaque attribute blob; the member names it references do not. The debugger binds those names against the renamed metadata and misses.

This is the same split that governs Serilog destructuring and configuration binding: a value that is a compile-time literal survives renaming, but a name resolved at runtime follows it. DebuggerDisplay is unusual only in that the resolution happens at debug time, and it bundles both halves into one attribute — a surviving literal that names members the renamer has moved.

Contrast it with the no-attribute case. Remove [DebuggerDisplay] entirely and the debugger falls back to the type’s ToString(). For a record, ToString() is compiler-generated with each member name baked in as a literal, so it prints the same names before and after renaming. A plain type shows fine; adding a DebuggerDisplay that names members is what introduces the debug-time by-name dependency.

Scoping Nebula so your own builds stay readable

The key realization is who these attributes are for. DebuggerDisplay, DebuggerTypeProxy and DebuggerBrowsable exist for a human attaching a debugger — and on the build you ship to customers, nobody is. A hardened release with anti-debug and a virtualized hot path is built so that attaching a debugger is detected and stepping is meaningless; a broken watch display on that build costs you exactly nothing, because you never open it. So the first and best answer is: do not try to preserve the display on the customer build. Let it rename.

Where the display must survive is the builds you actually debug — local, and a staging build that mirrors production. If that staging build is lightly obfuscated or not at all, you are done; the names are intact. If it is fully renamed and you still need to attach, keep only what the display reads — and keep the leaf, not the type. The cheapest way is to route the whole display through one member and keep that single name:

[DebuggerDisplay("{DebugView,nq}")]
public sealed class Invoice
{
    public string CustomerName { get; init; }
    public int Id { get; init; }
    public decimal Total { get; init; }

    [Obfuscation(Feature = "renaming", Exclude = true)]   // the only name the debugger binds
    private string DebugView => $"{CustomerName} #{Id}";
}

Now the evaluator binds exactly one name — DebugView — which the attribute keeps. Inside that property, CustomerName, Id and Total are ordinary member accesses compiled to IL; renaming rewrites them consistently along with every other reference, so they still rename to b, c, d everywhere and leak nothing in a decompiler. You traded three kept property names for one kept indirection name, and the ,nq specifier drops the quotes so the display reads cleanly.

If you prefer a config rule over an attribute — say a whole diagnostics namespace of debug views — keep the leaf members there and rename everything else:

{
  "schemaVersion": 1,
  "inputs": [ "bin/Release/net8.0/Billing.dll" ],
  "outputDirectory": "protected",
  "encryptStrings": true,
  "controlFlowObfuscation": true,
  "renaming": {
    "rules": [
      { "pattern": "Billing.Diagnostics.DebugViews.*", "action": "keep", "members": "public" },
      { "pattern": "Billing.*",                         "action": "rename" }
    ]
  }
}
nebula protect --config nebula.config.json

A kept name is still a fully protected body — the kept DebugView member keeps its control-flow obfuscation and string encryption; only its name stays readable. That is the standard keep-list discipline: keep the smallest set of names something resolves as a string, here at debug time.

When it breaks

Three edges are worth knowing. First, DebuggerTypeProxy: its typeof(InvoiceDebugView) argument is a type reference Nebula rewrites consistently, so the proxy still attaches after renaming — but the proxy class’s own public property names are what the debugger expands into the watch tree, and those renamed to a, b, c. If you want that expanded view readable, keep the proxy type’s public member names; otherwise it is just more noise in a window you can let rename.

Second, string encryption does not cover the format string. encryptStrings rewrites ldstr loads in method bodies; the DebuggerDisplay argument is a constructor value in an attribute blob, not a method body, so it stays in cleartext even on the hardened build. The member names it mentions sit there in the open — a small leak, and another reason to prefer the single DebugView indirection (which names nothing) or to simply not carry the attribute into the shipped build.

Third, stepping attributes are moot, not broken. [DebuggerStepThrough] and [DebuggerHidden] affect only stepping, which renaming leaves untouched — but once a method is control-flow-flattened or virtualized there is no ordinary IL left to single-step, so those attributes have nothing to act on. Do not add keep rules for them; there is nothing to keep.

Takeaway

A [DebuggerDisplay] is a literal that names members, and renaming moves the members out from under the literal. The format string survives untouched; the debugger’s evaluator binds the names it contains against the renamed metadata and misses. Decide where the display actually needs to work — your debuggable builds, not the hardened customer one — and on those, route it through a single member you keep rather than naming several properties you then have to leave readable. Protect the rest with Nebula.NET, then open the result in Glass.NET to confirm your types read as a, b, c while the one DebugView your engineers rely on still spells its name.

Try Nebula.NET

Harden your .NET code in minutes — start with the free edition.