Skip to content

Commit c4ba6c5

Browse files
noahfalktommcdon
andauthored
StackOverflow debug improvements (#42673)
* StackOverflow debug improvements Added some debugging instructions for debugging a StackOverflow on Windows and made some minor improvements to the pre-existing text. Unfortunately the Windows and Linux instructions remain asymmetric because the dump debugging experience for StackOverflow on Windows isn't good (dotnet/diagnostics#4944). In the future we might also want to add instructions for using VSCode on Linux or add more to the lldb debugging instructions but I didn't want to expand the scope too much for now. Co-authored-by: Tom McDonald <[email protected]>
1 parent 55ab537 commit c4ba6c5

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

docs/core/diagnostics/debug-stackoverflow.md

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ ms.date: 8/22/2024
66
---
77
# Debug StackOverflow errors
88

9-
A <xref:System.StackOverflowException> is thrown when the execution stack overflows because it contains too many nested method calls.
9+
A <xref:System.StackOverflowException> is thrown when the execution stack overflows because it contains too many nested method calls. Very often this occurs because methods are calling each other [recursively](https://wikipedia.org/wiki/Recursion_(computer_science)).
1010

1111
For example, suppose you have an app as follows:
1212

@@ -39,12 +39,15 @@ Stack overflow.
3939
<this output repeats many more times>
4040
````
4141

42-
Often just seeing this callstack is enough to identify the problematic repeating method and locate the source code logic that is causing the recursive calls. However if the problem is still unclear you can debug further.
42+
When you see the program exit with output like this, you can find the source code for the repeating method(s) and investigate the logic that causes the large number of calls.
4343

44-
> [!NOTE]
45-
> This article describes how to debug a stack overflow with lldb on Linux. If you are running on Windows, we suggest debugging the app with [Visual Studio](/visualstudio/debugger/what-is-debugging) or [Visual Studio Code](https://code.visualstudio.com/Docs/editor/debugging).
44+
## Using the debugger
4645

47-
## Example
46+
Often just seeing this callstack on the console above is enough to identify the problematic code. However if the problem is still unclear you can debug further.
47+
48+
### [Linux](#tab/linux)
49+
50+
This example creates a core dump when the StackOverflowException occurs, then loads the dump into [lldb](https://lldb.llvm.org/) (a common Linux command-line debugger) and debugs it.
4851

4952
1. Run the app with it configured to collect a dump on crash
5053

@@ -64,7 +67,7 @@ Often just seeing this callstack is enough to identify the problematic repeating
6467
dotnet-sos install
6568
````
6669
67-
3. Debug the dump in lldb to see the failing stack
70+
3. Open the dump in lldb and use the `bt` (backtrace) command to display the stack.
6871
6972
````
7073
lldb --core /temp/coredump.6412
@@ -82,7 +85,7 @@ Often just seeing this callstack is enough to identify the problematic repeating
8285
...
8386
````
8487
85-
4. The top frame `0x00007f59b40900cc` is repeated several times. Use the [SOS](sos-debugging-extension.md) `ip2md` command to figure out what method is located at the `0x00007f59b40900cc` address
88+
4. The top frame `0x00007f59b40900cc` is repeated several times. Use the [SOS](sos-debugging-extension.md) `ip2md` command to figure out what managed method is located at the `0x00007f59b40900cc` address
8689
8790
````
8891
(lldb) ip2md 0x00007f59b40900cc
@@ -103,7 +106,19 @@ Often just seeing this callstack is enough to identify the problematic repeating
103106
Source file: /temp/Program.cs @ 9
104107
````
105108
106-
5. Go look at the indicated method temp.Program.Main(System.String[]) and source "/temp/Program.cs @ 9" to see if you can figure out what you did wrong. If it still wasn't clear you could use further debugger commands to inspect the current state of the process or add logging in that area of the code.
109+
5. Go look at the indicated method temp.Program.Main(System.String[]) and source "/temp/Program.cs @ 9" to see if you can figure out what the code is doing wrong. If additional information is needed, you can use further debugger or [SOS](sos-debugging-extension.md) commands to inspect the process.
110+
111+
### [Windows](#tab/windows)
112+
113+
Running the application under the debugger in [Visual Studio](/visualstudio/debugger/what-is-debugging) will show a StackOverflowException in the exception helper dialog and highlight the line of code responsible for making the final call that overflows the stack.
114+
115+
[Visual Studio StackOverflowException dialog](media/visual_studio_stackoverflow_exception.png)
116+
117+
The callstack debugger window also shows the stack.
118+
119+
[Visual Studio StackOverflow callstack](media/visual_studio_stackoverflow_callstack.png)
120+
121+
You can use all the normal Visual Studio debugger features to investigate each frame on the callstack, its source code, and the value of local variables.
107122
108123
## See Also
109124
21.7 KB
Loading
154 KB
Loading

0 commit comments

Comments
 (0)