Question when writing an Assert function
-
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
-
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
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
-
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
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 ownassert
function?assert
is provided for you.
Software Zen:
delete this;
-
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 ownassert
function?assert
is provided for you.
Software Zen:
delete this;
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:
-
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:
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;
-
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;
But it does cause an abort. Not always a nice thing for an embedded system. Thus the need to write an alternative.