Skip to content

Commit 126bb0f

Browse files
committed
Merge pull request cpputest#33 from arstrube/master
Add example for IEEE754ExceptionsPlugin::checkIeee754ExeptionFlag(int flag)
2 parents 0755530 + 943f2ab commit 126bb0f

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

plugin_manual.markdown

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,66 @@ src/CppUTestExt/IEEE754ExceptionsPlugin.cpp:164: error:
189189
Errors (2 failures, 2 tests, 2 ran, 12 checks, 0 ignored, 0 filtered out, 39 ms)
190190
$
191191
{% endhighlight %}
192+
193+
### Debugging floating point failures
194+
195+
When a test fails due to a floating point error, it can be challenging to find the location of the offending operation, since the plugin has no knowledge of where the flag was originally set. To aid in debugging, there is a static method you can use to set up a watch:
196+
{% highlight c++ %}
197+
IEEE754ExceptionsPlugin::checkIeee754ExeptionFlag(int flag).
198+
{% endhighlight %}
199+
Unfortunately, the debugger has no knowledge of defined macros such as FE_DIVBYZERO. Therefore, you will need to find out the integer value of the macro you require, and pass that as argument. Here is an minimal example using Gdb, example.cpp:
200+
{% highlight c++ %}
201+
#include "CppUTest/TestHarness.h"
202+
#include "CppUTestExt/IEEE754ExceptionsPlugin.h"
203+
204+
static volatile float f = 1.0;
205+
static volatile IEEE754ExceptionsPlugin plugin; // Make sure this is linked, so the debugger knows it
206+
207+
int main(int, char**) {
208+
f /= 0.0f; // the offending statement
209+
return 0;
210+
}
211+
{% endhighlight %}
212+
1) Compile the example. Your command line will look roughly like this:
213+
{% highlight bash %}
214+
$ g++ -Wextra -Wall -Werror -g3 -O0 -std=c++11 -Iinclude -Llib example.cpp -lCppUTest -lCppUTestExt -o example.exe
215+
{% endhighlight %}
216+
2) Start the example in Gdb:
217+
{% highlight bash %}
218+
$ gdb ./example.exe
219+
GNU gdb (GDB) 7.6.50.20130728-cvs (cygwin-special)
220+
#...more gdb output here...
221+
Reading symbols from /cygdrive/c/data/00_Dev/06_Faking/MiscellanousTests/example.exe...done.
222+
(gdb)
223+
{% endhighlight %}
224+
3) Set a breakpoint and run the example:
225+
{% highlight bash %}
226+
(gdb) break main
227+
Breakpoint 1 at 0x4011ab: file example.cpp, line 8.
228+
(gdb) run
229+
Starting program: /cygdrive/c/data/00_Dev/06_Faking/MiscellanousTests/example.exe
230+
[New Thread 6476.0x2038]
231+
[New Thread 6476.0x239c]
232+
233+
Breakpoint 1, main () at example.cpp:8
234+
8 f /= 0.0f;
235+
(gdb)
236+
{% endhighlight %}
237+
4) Set up the watch you need (on this particular platform, FE_DIVBYZERO==0x04):
238+
{% highlight bash %}
239+
(gdb) watch IEEE754ExceptionsPlugin::checkIeee754ExeptionFlag(0x04)
240+
Watchpoint 2: IEEE754ExceptionsPlugin::checkIeee754ExeptionFlag(0x04)
241+
(gdb)
242+
{% endhighlight %}
243+
5) Stepping over the offending statement will change the value of your watch:
244+
{% highlight bash %}
245+
(gdb) next
246+
Watchpoint 2: IEEE754ExceptionsPlugin::checkIeee754ExeptionFlag(0x04)
247+
248+
Old value = false
249+
New value = true
250+
0x004011b5 in main () at example.cpp:8
251+
8 f /= 0.0f;
252+
(gdb)
253+
{% endhighlight %}
254+
Of course you don't have to use commandline Gdb to do this; you can debug your code from within your favorite IDE (Eclipse, Code::Blocks, ...) following basically the same procedure.

0 commit comments

Comments
 (0)