0

does anyone know of a way to define some behavior to be called on every function or line in a C++ program?

i would like to validate my software by essentially causing it to exit at certain points in the application, and make sure that the next boot up of the process can handle recovering in all of the points at which the process previously died.

essentially, im looking for a way to script failures in C++, so that I don't have to define points using some macros, and just tell the application to essentially cycle through all of these "Death points" and confirm that the process can recover from every single one of them, doing all of this in an automated fashion.

i could easily create a macro called DEATH_POINT() and it could essentially check whether or not it should exit the process or not, but I was hoping there was something a bit more elegant than having a bunch fo macros sitting in the code.

Michael Xu
  • 557
  • 1
  • 5
  • 14

2 Answers2

1

Depending on the compiler you are using there is probably a way to have it call a function automatically every time it enters a function. That might do what you need.

For Visual Studio there's info here: http://msdn.microsoft.com/en-us/library/c63a9b7h.aspx And the top answer here has information about doing it in GCC: Automatically adding Enter/Exit Function Logs to a Project

Community
  • 1
  • 1
Retired Ninja
  • 4,785
  • 3
  • 25
  • 35
  • wow, yeah. good find retired ninja. i could totally use the GCC instrument-functions. Is there a way to do this at a line granularity as opposed to a function granularity? Regardless, that is super helpful! – Michael Xu Feb 26 '12 at 20:03
  • No, this is just per function call. @BenVoigt is right about using the debugging API for more granularity. – Retired Ninja Feb 26 '12 at 20:17
1

I suggest you go one step beyond that, and test on an instruction-by-instruction basis, rather than entire expressions, statements, or lines.

You should be able to use the Debugger API to set a breakpoint programmatically (you'll need a helper process though, IIRC). The process will be suspended when the breakpoint is reached. And you can receive an event when the breakpoint is hit and terminate the process.

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • @MichaelXu: No reason you can't do it with linux, you'll just need to use the linux debugging APIs and dwarf2 debug information. I suggest reading the `gdb` source code, or some other open source debugger for linux. – Ben Voigt Feb 26 '12 at 20:03