Bug 322158 (MONO79452)

Summary: rethrow loses stack trace information
Product: [Mono] Mono: Runtime Reporter: Rodrigo Oliveira <rodrigobamboo>
Component: debugAssignee: Mono Bugs <mono-bugs>
Status: NEW --- QA Contact: Mono Bugs <mono-bugs>
Severity: Minor    
Priority: P2 - High CC: forgotten_vxPDddArjq, greg.smolyn, odie, tomas.matousek
Version: 2.6.x   
Target Milestone: ---   
Hardware: Other   
OS: Other   
Whiteboard:
Found By: --- Services Priority:
Business Priority: Blocker: ---
Marketing QA Status: --- IT Deployment: ---
Attachments: test case for rethrow bug

Description Thomas Wiest 2007-09-15 20:10:39 UTC


---- Reported by rodrigobamboo@gmail.com 2006-09-20 11:42:43 MST ----

namespace BugReport
{
    using System;

    class Foo
    {
        public static void Bar()
        {
            throw new ApplicationException("oh");
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                FooBar();
            }
            catch (Exception x)
            {
                Assert(-1 != x.StackTrace.IndexOf("BugReport.Program.Main"));
            }
            
            try
            {
                ReThrowFooBar();
            }
            catch (Exception x)
            {
                Assert(-1 != x.StackTrace.IndexOf("BugReport.Program.Main"));
            }
        }
        
        static void FooBar()
        {
            Foo.Bar();
        }
        
        static void ReThrowFooBar()
        {
            try
            {
                Foo.Bar();
            }
            catch (Exception)
            {
                throw; // <--- ms.net preserves stack trace here
            }
        }

        static void Assert(bool condition)
        {
            if (condition) return;
            throw new ApplicationException("Assertion failed.");
        }
    }
}



---- Additional Comments From vargaz@gmail.com 2006-09-20 12:12:34 MST ----

This is a known bug, it is a bit hard to figure out what part of the
stack trace to preserve in some situations.




---- Additional Comments From joe@otee.dk 2006-09-21 17:34:27 MST ----

Would be really useful to get fixed even though it's hard.
Stacktraces are pretty important when developing code.

Especially in boo (and the derived unityscript), this means most of the duck typed code will 
lose the important part of the stacktrace, so when you encounter an error, you have no clue 
where it happened.



---- Additional Comments From vargaz@gmail.com 2006-09-22 05:29:44 MST ----

We would fix it but don't know how to. For example, under MS, 
if I print the stack trace in ReThrowFooBar (), it doesn't include the
Main method, but when I print it out in Main, it does. So we _do_
preserve the stack trace, while MS changes it during a rethrow.




---- Additional Comments From rodrigobamboo@gmail.com 2006-09-22 08:30:33 MST ----

"For example, under MS, 
if I print the stack trace in ReThrowFooBar (), it doesn't include the
Main method, but when I print it out in Main, it does."

That's ok. An exception stack trace should include stack trace
information upto the method which handles it.

"So we _do_ preserve the stack trace, while MS changes it during a
rethrow."

Interesting.

Would it be possible to append stack trace information during a
rethrow to match the clr behavior then?

If it does not seem doable, would someone be kind enough to provide
some pointers on why and where to look in the source code to try to
come up with a patch?

This bug blocked bug(s) 71375.

Unknown operating system unknown. Setting to default OS "Other".

Comment 1 Martin Baulig 2007-10-08 12:29:40 UTC
Old.
Comment 2 Forgotten User vxPDddArjq 2007-10-08 14:34:47 UTC
It is old, but it is a pretty valid bug.
Comment 3 Greg Smolyn 2010-03-12 22:36:54 UTC
Created attachment 348292 [details]
test case for rethrow bug
Comment 4 Greg Smolyn 2010-03-12 22:40:05 UTC
This is a huge bug in the system:

For the attached code, mono prints out:

STACK bar
  at ExceptionTest.A.bar () [0x00000] in /home/strangeloop/ExceptionTest/Main.cs:23 
STACK baz
  at ExceptionTest.A.bar () [0x00000] in /home/strangeloop/ExceptionTest/Main.cs:23 


From Windows C#:

STACK bar
   at ExceptionTest.A.foo() in C:\Users\Administrator\Desktop\etest\ExceptionTest\Main.cs:line 18
   at ExceptionTest.A.bar() in C:\Users\Administrator\Desktop\etest\ExceptionTest\Main.cs:line 23
STACK baz
   at ExceptionTest.A.foo() in C:\Users\Administrator\Desktop\etest\ExceptionTest\Main.cs:line 18
   at ExceptionTest.A.bar() in C:\Users\Administrator\Desktop\etest\ExceptionTest\Main.cs:line 28
   at ExceptionTest.A.baz() in C:\Users\Administrator\Desktop\etest\ExceptionTest\Main.cs:line 34


I will also note that if the code is changed to do "throw e;" instead of just "throw;" the output also varies.

This is affecting our ability to debug problems on production systems.
Comment 5 Greg Smolyn 2010-03-12 23:06:23 UTC
Whoops put severity too high
Comment 6 Forgotten User vxPDddArjq 2010-10-04 00:11:10 UTC
*** Bug 643371 has been marked as a duplicate of this bug. ***