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 / C++ / MFC
  4. Question when writing an Assert function

Question when writing an Assert function

Scheduled Pinned Locked Moved C / C++ / MFC
questionsharepointdata-structurestutorial
6 Posts 4 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.
  • N Offline
    N Offline
    Nacho Chip
    wrote on last edited by
    #1

    Hello all, I am writing an assert function and don't know how to do certain things, 1. How can I know the file name of the code that cause the assert? 2. How can I know the line number of the code that cause the assert? 3. How can I know the function name of the code that cause the assert? 4. How can I print out the call stack when I get the assert? 5. Can I get the PC and SP register information when I get the asset? Thanks! Nacho

    S G 2 Replies Last reply
    0
    • N Nacho Chip

      Hello all, I am writing an assert function and don't know how to do certain things, 1. How can I know the file name of the code that cause the assert? 2. How can I know the line number of the code that cause the assert? 3. How can I know the function name of the code that cause the assert? 4. How can I print out the call stack when I get the assert? 5. Can I get the PC and SP register information when I get the asset? Thanks! Nacho

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      1. How can I know the file name of the code that cause the assert?   A: The "__FILE__" macro. 2. How can I know the line number of the code that cause the assert?   A: The "__LINE__" macro. 3. How can I know the function name of the code that cause the assert?   A: Possible but hard - See 4. 4. How can I print out the call stack when I get the assert?   A: There are article on the codeproject and else where on this - It is a bit involved. 5. Can I get the PC and SP register information when I get the asset?   A: Inline assembler:

      #pragma optimize("y", on) // Force use of frame pointers
      DWORD GetMyReturnAddress()
      {
          DWORD ReturnAddress;
          __asm
          {
              mov eax, [ebp+4]
              mov dword ptr[ReturnAddress], eax
          }
          return ReturnAddress;
      }
      #pragma optimize("", on) // Resets the optimizations
      
      // To get EIP and ESP.
      DWORD _ESP;
      __asm
      {
          mov dword ptr[_ESP], ESP
      }
      DWORD _EIP = GetMyReturnAddress(); // Return address of statement after "GetMyReturnAddress()"
      

      Steve

      1 Reply Last reply
      0
      • N Nacho Chip

        Hello all, I am writing an assert function and don't know how to do certain things, 1. How can I know the file name of the code that cause the assert? 2. How can I know the line number of the code that cause the assert? 3. How can I know the function name of the code that cause the assert? 4. How can I print out the call stack when I get the assert? 5. Can I get the PC and SP register information when I get the asset? Thanks! Nacho

        G Offline
        G Offline
        Gary R Wheeler
        wrote on last edited by
        #3

        1: The __FILE__ macro. 2: The __LINE__ macro. 3: The __FUNCTION__ macro. 4: This requires that you be able to manipulate the symbolic debugging information for the program in question. Essentially, you have to be able to do the same things as a debugger. This is not trivial, and requires detailed specifications of the debugging symbols, which isn't always available. 5. Yes. Generally, in C, on entry to any given function the top-most item on the stack is the return address. Just above the return address are any function arguments. Since you are now inside your assert function, and you (I would assume) know the arguments, you can 'back up' the stack pointer to reach the stack contents at which your assert function was called. The question remains, why are you implementing your own assert function? assert is provided for you.


        Software Zen: delete this;

        Fold With Us![^]

        H 1 Reply Last reply
        0
        • G Gary R Wheeler

          1: The __FILE__ macro. 2: The __LINE__ macro. 3: The __FUNCTION__ macro. 4: This requires that you be able to manipulate the symbolic debugging information for the program in question. Essentially, you have to be able to do the same things as a debugger. This is not trivial, and requires detailed specifications of the debugging symbols, which isn't always available. 5. Yes. Generally, in C, on entry to any given function the top-most item on the stack is the return address. Just above the return address are any function arguments. Since you are now inside your assert function, and you (I would assume) know the arguments, you can 'back up' the stack pointer to reach the stack contents at which your assert function was called. The question remains, why are you implementing your own assert function? assert is provided for you.


          Software Zen: delete this;

          Fold With Us![^]

          H Offline
          H Offline
          Harold Bamford
          wrote on last edited by
          #4

          Gary R. Wheeler wrote:

          The question remains, why are you implementing your own assert function? assert is provided for you.

          I don't know Nacho's reason, but I have the need to be able to comprehensively report an error (file, line, function, and stack trace) and then continue the program with error recovery. The idea is that a customer could snap the info and report it back and then I would have a fighting chance of figuring out the problem. And if automatic error recovery worked, then the customer is not facing a complete crash like assert() gives. Further, not all code runs under Windows. An heretical thought, but there it is! :laugh:

          G 1 Reply Last reply
          0
          • H Harold Bamford

            Gary R. Wheeler wrote:

            The question remains, why are you implementing your own assert function? assert is provided for you.

            I don't know Nacho's reason, but I have the need to be able to comprehensively report an error (file, line, function, and stack trace) and then continue the program with error recovery. The idea is that a customer could snap the info and report it back and then I would have a fighting chance of figuring out the problem. And if automatic error recovery worked, then the customer is not facing a complete crash like assert() gives. Further, not all code runs under Windows. An heretical thought, but there it is! :laugh:

            G Offline
            G Offline
            Gary R Wheeler
            wrote on last edited by
            #5

            Harold Bamford wrote:

            not all code runs under Windows

            assert is a Standard C/C++ library feature, and it not Windows-specific.


            Software Zen: delete this;

            Fold With Us![^]

            H 1 Reply Last reply
            0
            • G Gary R Wheeler

              Harold Bamford wrote:

              not all code runs under Windows

              assert is a Standard C/C++ library feature, and it not Windows-specific.


              Software Zen: delete this;

              Fold With Us![^]

              H Offline
              H Offline
              Harold Bamford
              wrote on last edited by
              #6

              But it does cause an abort. Not always a nice thing for an embedded system. Thus the need to write an alternative.

              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