Stack Trace incorrect in release mode
-
One of the method is missed in the call stack when code is compiled in release mode. Here is the sample code: static void Main(string[] args) { MyFunc(); Console.ReadKey(false); } public static void MyFunc() { Console.WriteLine(Program.GetCallStack()); } public static string GetCallStack() { System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace(); string s = ""; int index = 0; while (true) { System.Diagnostics.StackFrame frame = callStack.GetFrame(index); if (frame == null) break; System.Reflection.MethodBase method = frame.GetMethod(); if (index 0) s = " --" + s; s = method.DeclaringType.Name + "." + method.Name + "()" + s; index++; } return (s); } Why is it so?
Gurpreet
-
One of the method is missed in the call stack when code is compiled in release mode. Here is the sample code: static void Main(string[] args) { MyFunc(); Console.ReadKey(false); } public static void MyFunc() { Console.WriteLine(Program.GetCallStack()); } public static string GetCallStack() { System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace(); string s = ""; int index = 0; while (true) { System.Diagnostics.StackFrame frame = callStack.GetFrame(index); if (frame == null) break; System.Reflection.MethodBase method = frame.GetMethod(); if (index 0) s = " --" + s; s = method.DeclaringType.Name + "." + method.Name + "()" + s; index++; } return (s); } Why is it so?
Gurpreet
Make sure that you're created PDB's when compiling in Release configuration. In VS2008, you can set the property using the project properties -> Build -> Advanced... -> Debug Info.
-
Make sure that you're created PDB's when compiling in Release configuration. In VS2008, you can set the property using the project properties -> Build -> Advanced... -> Debug Info.
My setting is Debug Info=pdb only but it does not work
Gurpreet
-
My setting is Debug Info=pdb only but it does not work
Gurpreet
Hmm, I take it that the PDB's are in the same folder as the dll? What specifically isn't working? When I run your code I get the following output (I inserted a Environment.Newline): Debug: ThreadHelper.ThreadStart() ExecutionContext.Run() HostProc.RunUsersAssembly() AppDomain._nExecuteAssembly() Program.Main() Program.MyFunc() Program.GetCallStack() Release (with PDB) ThreadHelper.ThreadStart() ExecutionContext.Run() HostProc.RunUsersAssembly() AppDomain._nExecuteAssembly() Program.Main() Program.MyFunc() Program.GetCallStack()
-
Hmm, I take it that the PDB's are in the same folder as the dll? What specifically isn't working? When I run your code I get the following output (I inserted a Environment.Newline): Debug: ThreadHelper.ThreadStart() ExecutionContext.Run() HostProc.RunUsersAssembly() AppDomain._nExecuteAssembly() Program.Main() Program.MyFunc() Program.GetCallStack() Release (with PDB) ThreadHelper.ThreadStart() ExecutionContext.Run() HostProc.RunUsersAssembly() AppDomain._nExecuteAssembly() Program.Main() Program.MyFunc() Program.GetCallStack()
Here is my output (RUN IT VIA COMMAND PROMPT AND NOT F5 in Visual Studio) C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Release>consolea pplication1 Program.Main()Program.GetCallStack() -- C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Release>cd..\deb ug C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Debug>consoleapp lication1 Program.Main()Program.MyFunc()Program.GetCallStack() -- NOTE : - Program.MyFunc() is missing in release mode.
Gurpreet
-
Here is my output (RUN IT VIA COMMAND PROMPT AND NOT F5 in Visual Studio) C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Release>consolea pplication1 Program.Main()Program.GetCallStack() -- C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Release>cd..\deb ug C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Debug>consoleapp lication1 Program.Main()Program.MyFunc()Program.GetCallStack() -- NOTE : - Program.MyFunc() is missing in release mode.
Gurpreet
Anyone has any information on this? Its bit urgent!!
Gurpreet
-
Here is my output (RUN IT VIA COMMAND PROMPT AND NOT F5 in Visual Studio) C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Release>consolea pplication1 Program.Main()Program.GetCallStack() -- C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Release>cd..\deb ug C:\Applications\poc\ConsoleApplication1\ConsoleApplication1\bin\Debug>consoleapp lication1 Program.Main()Program.MyFunc()Program.GetCallStack() -- NOTE : - Program.MyFunc() is missing in release mode.
Gurpreet
I've run the code from command prompt and you're right. I believe that this is a compiler optimisation as MyFunc is so simple. If you run the following code:
static void Main(string[] args)
{
MyFunc();
Console.ReadKey(false);
}public static void MyFunc() { Console.WriteLine("Begin"); // Simple Amend to prevent compiler optimization Console.WriteLine(Program.GetCallStack()); Console.WriteLine("End"); // Simple Amend to prevent compiler optimization } public static string GetCallStack() { System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace(); string s = ""; int index = 0; while (true) { System.Diagnostics.StackFrame frame = callStack.GetFrame(index); if (frame == null) break; System.Reflection.MethodBase method = frame.GetMethod(); if (index == 0) s = " --" + s; s = method.DeclaringType.Name + "." + method.Name + "()" + Environment.NewLine + s; index++; } return (s); }
You should get the following output: Debug: Begin Program.Main() Program.MyFunc() Program.GetCallStack() -- End Release (with PDB): Begin Program.Main() Program.MyFunc() Program.GetCallStack() -- End
-
I've run the code from command prompt and you're right. I believe that this is a compiler optimisation as MyFunc is so simple. If you run the following code:
static void Main(string[] args)
{
MyFunc();
Console.ReadKey(false);
}public static void MyFunc() { Console.WriteLine("Begin"); // Simple Amend to prevent compiler optimization Console.WriteLine(Program.GetCallStack()); Console.WriteLine("End"); // Simple Amend to prevent compiler optimization } public static string GetCallStack() { System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace(); string s = ""; int index = 0; while (true) { System.Diagnostics.StackFrame frame = callStack.GetFrame(index); if (frame == null) break; System.Reflection.MethodBase method = frame.GetMethod(); if (index == 0) s = " --" + s; s = method.DeclaringType.Name + "." + method.Name + "()" + Environment.NewLine + s; index++; } return (s); }
You should get the following output: Debug: Begin Program.Main() Program.MyFunc() Program.GetCallStack() -- End Release (with PDB): Begin Program.Main() Program.MyFunc() Program.GetCallStack() -- End
Thanks this works!! :)
Gurpreet
-
Thanks this works!! :)
Gurpreet
No Problem
-
One of the method is missed in the call stack when code is compiled in release mode. Here is the sample code: static void Main(string[] args) { MyFunc(); Console.ReadKey(false); } public static void MyFunc() { Console.WriteLine(Program.GetCallStack()); } public static string GetCallStack() { System.Diagnostics.StackTrace callStack = new System.Diagnostics.StackTrace(); string s = ""; int index = 0; while (true) { System.Diagnostics.StackFrame frame = callStack.GetFrame(index); if (frame == null) break; System.Reflection.MethodBase method = frame.GetMethod(); if (index 0) s = " --" + s; s = method.DeclaringType.Name + "." + method.Name + "()" + s; index++; } return (s); } Why is it so?
Gurpreet
You can try: [MethodImpl(MethodImplOptions.NoInlining)] In the method that is disappearing.