Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Stack Trace incorrect in release mode

Stack Trace incorrect in release mode

Scheduled Pinned Locked Moved C#
databasedata-structuresdebuggingquestionannouncement
10 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    KaurGurpreet
    wrote on last edited by
    #1

    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

    M P 2 Replies Last reply
    0
    • K KaurGurpreet

      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

      M Offline
      M Offline
      Martin Jarvis
      wrote on last edited by
      #2

      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.

      K 1 Reply Last reply
      0
      • M Martin Jarvis

        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.

        K Offline
        K Offline
        KaurGurpreet
        wrote on last edited by
        #3

        My setting is Debug Info=pdb only but it does not work

        Gurpreet

        M 1 Reply Last reply
        0
        • K KaurGurpreet

          My setting is Debug Info=pdb only but it does not work

          Gurpreet

          M Offline
          M Offline
          Martin Jarvis
          wrote on last edited by
          #4

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

          K 1 Reply Last reply
          0
          • M Martin Jarvis

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

            K Offline
            K Offline
            KaurGurpreet
            wrote on last edited by
            #5

            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

            K M 2 Replies Last reply
            0
            • K KaurGurpreet

              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

              K Offline
              K Offline
              KaurGurpreet
              wrote on last edited by
              #6

              Anyone has any information on this? Its bit urgent!!

              Gurpreet

              1 Reply Last reply
              0
              • K KaurGurpreet

                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

                M Offline
                M Offline
                Martin Jarvis
                wrote on last edited by
                #7

                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

                K 1 Reply Last reply
                0
                • M Martin Jarvis

                  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

                  K Offline
                  K Offline
                  KaurGurpreet
                  wrote on last edited by
                  #8

                  Thanks this works!! :)

                  Gurpreet

                  M 1 Reply Last reply
                  0
                  • K KaurGurpreet

                    Thanks this works!! :)

                    Gurpreet

                    M Offline
                    M Offline
                    Martin Jarvis
                    wrote on last edited by
                    #9

                    No Problem

                    1 Reply Last reply
                    0
                    • K KaurGurpreet

                      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

                      P Offline
                      P Offline
                      Paulo Zemek
                      wrote on last edited by
                      #10

                      You can try: [MethodImpl(MethodImplOptions.NoInlining)] In the method that is disappearing.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups