Skip to content

Commit

Permalink
StackOverflow debug improvements (#42673)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
noahfalk and tommcdon authored Sep 23, 2024
1 parent 55ab537 commit c4ba6c5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions docs/core/diagnostics/debug-stackoverflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ ms.date: 8/22/2024
---
# Debug StackOverflow errors

A <xref:System.StackOverflowException> is thrown when the execution stack overflows because it contains too many nested method calls.
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)).

For example, suppose you have an app as follows:

Expand Down Expand Up @@ -39,12 +39,15 @@ Stack overflow.
<this output repeats many more times>
````

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.
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.

> [!NOTE]
> 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).
## Using the debugger

## Example
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.

### [Linux](#tab/linux)

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.

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

Expand All @@ -64,7 +67,7 @@ Often just seeing this callstack is enough to identify the problematic repeating
dotnet-sos install
````

3. Debug the dump in lldb to see the failing stack
3. Open the dump in lldb and use the `bt` (backtrace) command to display the stack.

````
lldb --core /temp/coredump.6412
Expand All @@ -82,7 +85,7 @@ Often just seeing this callstack is enough to identify the problematic repeating
...
````

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
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

````
(lldb) ip2md 0x00007f59b40900cc
Expand All @@ -103,7 +106,19 @@ Often just seeing this callstack is enough to identify the problematic repeating
Source file: /temp/Program.cs @ 9
````

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.
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.

### [Windows](#tab/windows)

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.

[Visual Studio StackOverflowException dialog](media/visual_studio_stackoverflow_exception.png)

The callstack debugger window also shows the stack.

[Visual Studio StackOverflow callstack](media/visual_studio_stackoverflow_callstack.png)

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.

## See Also

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c4ba6c5

Please sign in to comment.