Debugging without an IDE
The debugger of an IDE is a convenience, and a container has no IDE. What remains is a program that prints what it knows, an exception that carries its own trace, and the runtime configuration of the environment. This lesson measures those three, and says which tools this image does not have.
Debug.WriteLine and Debugger
Debug.WriteLine writes to the trace listeners, and a console program has none: the line goes nowhere unless a debugger or a listener such as TextWriterTraceListener is attached. Debugger.IsAttached answers whether one is there:
using System.Diagnostics;
Console.WriteLine($"attached {Debugger.IsAttached}");
Debug.WriteLine("this line needs a listener");
Debug.WriteLine($"pid {Environment.ProcessId}");
dotnet run
attached False
The two Debug lines produced no output, which is the measured fact of the run, not a mistake. A program that must print its diagnostics in a container uses Console.WriteLine or a logging library; Debug is for the developer with a debugger, and the calls stay in the code behind the DEBUG symbol.
A stack trace
An exception carries the frames it travelled through. The program throws in a helper and catches in the top level, and prints the type, the method that threw and the number of frames:
using System.Diagnostics;
try
{
Fail();
}
catch (Exception e)
{
Console.WriteLine($"caught {e.GetType().Name}");
Console.WriteLine($"site {e.TargetSite?.Name}");
Console.WriteLine($"frames {e.StackTrace?.Split('\n').Length}");
}
Console.WriteLine($"environment {Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "(unset)"}");
Console.WriteLine($"stack {Environment.StackTrace.Length > 0}");
var missing = new[] { "dotnet-dump", "dotnet-trace", "dotnet-counters" }
.Where(name => !File.Exists($"/usr/local/bin/{name}") && !File.Exists($"/usr/bin/{name}"))
.ToArray();
Console.WriteLine($"not here {string.Join(",", missing)}");
static void Fail()
{
throw new InvalidOperationException("trace me");
}
dotnet run
caught InvalidOperationException
site <<Main>$>g__Fail|0_1
frames 2
environment (unset)
stack True
not here dotnet-dump,dotnet-trace,dotnet-counters
The site is the generated name of the local function, which is the method that threw; the two frames are the helper and the top-level method. A program that catches an exception and logs e.ToString() gets the type, the message and every frame with its file and line, because the build wrote a PDB. The last line is the check of this image: the three diagnostic tools of the .NET CLI are not installed, so the lesson cannot show a dump or a trace.
What is not installed
The tools that analyse a running or a crashed process are separate installations:
| Tool | What it does | Here |
|---|---|---|
| dotnet-dump | Collects and analyses a memory dump. | not installed |
| dotnet-trace | Records an event trace of a process. | not installed |
| dotnet-counters | Watches the counters of a live process. | not installed |
| dotnet-gcdump | Collects a dump of the managed heap. | not installed |
Each is a global tool, installed with dotnet tool install -g dotnet-dump, which needs the NuGet feed. This container has no network, so the table says what a reader has to install on their own machine. What the image does have is the runtime itself: a crash prints its trace, the GC counters are available through GC.GetGCMemoryInfo (lesson 068) and a program can write its own timings with Stopwatch.
DOTNET_ variables
The runtime reads configuration from variables that start with DOTNET_. The lessons of this part used DOTNET_gcServer, DOTNET_TieredCompilation and DOTNET_TieredPGO; the same family covers the JIT, the loader and the diagnostics:
- DOTNET_ENVIRONMENT is read by the hosting stack, not by the runtime, and it was unset here.
- DOTNET_EnableDiagnostics=0 turns off the diagnostic port, which a hardened container may want.
- DOTNET_ReadyToRun=0 ignores the precompiled code of an assembly, which is a way to compare startup.
- DOTNET_SHARED_STORE names a folder of assemblies shared by several apps, the deployment option of the publishing lesson.
Exercises
- Add a TextWriterTraceListener to a file and confirm that Debug.WriteLine reaches it.
- Print e.ToString() instead of the parts and read the whole trace.
- Run the second program with DOTNET_ENVIRONMENT=Development and read the line.
- Catch an exception from a method three calls deep and count the frames.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
Where does Debug.WriteLine write when no listener is attached?
Answer
Nowhere, the line is dropped
The measured program printed no line for its two Debug calls.
-
What does Debugger.IsAttached report?
Answer
Whether a debugger is attached to the process
The measured run printed False, and a debugger would make it True.
-
What does this program print?
using System.Diagnostics; Console.WriteLine(Debugger.IsAttached);Answer
False
No debugger is attached in the container.
-
How many frames did the caught exception have?
Answer
2
The helper and the top-level method, as the measured line showed.
-
What does this program print?
try { throw new Exception("x"); } catch (Exception e) { Console.WriteLine(e.Message); }Answer
x
The message of the exception is the text given to its constructor.
-
Which of these tools is not installed in the image?
Answer
dotnet-trace
The measured program listed dotnet-dump, dotnet-trace and dotnet-counters as missing.
-
What does DOTNET_ENVIRONMENT configure?
Answer
The environment name the hosting stack reads
It is an application convention, and it was unset in the measured run.
-
What does this program print?
Console.WriteLine(Environment.GetEnvironmentVariable("DOTNET_ENVIRONMENT") ?? "(unset)");Answer
(unset)
The variable is not in the environment of the run.
-
What is the fastest way to see where an exception came from in a container?
Answer
Catch it and log e.ToString(), which carries the frames
The trace is in the exception, and the build wrote the PDB with the lines.
-
How is one of those diagnostic tools installed on a machine with the network?
Answer
dotnet tool install -g <name>
They are global tools from the NuGet feed, which this container does not have.
The code on this page was built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64) in a Docker container.
Article author: Arthur Isaev