Break a program after looping

Is there a way to set a program to break after executing a statement a user defined number of times? I have a statment that calls a function from inside a for loop that goes through many iterations. It works fine until a floating point exception occurs during one of the iterations. I’m trying to discover why the floation point exception occurs, and so far all I have been able to find is where it occurs. I can find out how many iterations before the fpe occurs by other means, but I would then like to break the program during the iteration where the exception occurs and watch the variables to see what is being passed to the function and what is being returned. After searching through the manual and looking in the debugger I can’t find how to do this or if it is supported by this debugger.

Also I forgot to mention that this floating point exception only occurs when I compile the program with pgcc, it runs fine for gcc and MS Visual Studio. Not sure why this happens.

Hi mmartin,

In PGDBG, you can set conditional breakpoints. First set a breakpoint at the beginning of the function. Once in scope (i.e. you ran the “-g” compiled program to this first breakpoint), add an additional breakpoint using an if statement as decribed in section 1.5.8 of the PGI tools guide. For example

% cat tmp.c
#include <stdio.h>
int main() {
  int i = 0;
  int a[10000];

  for (i = 0; i < 10000; ++i) {
    a[i] = 0;
  }

}
% pgcc -g tmp.c
% pgdbg -text a.out
PGDBG Rel Dev x86 (Cluster, 4096 CPU)
Copyright 1989-2000, The Portland Group, Inc. All Rights Reserved.
Copyright 2000-2006, STMicroelectronics, Inc. All Rights Reserved.
***Reading DWARFv2 Information.
Loaded: a.out

pgdbg> break 3
(1)breakpoint set at: main line: "tmp.c"@3 address: 0x8048616
1
pgdbg> run
libpgc.so loaded by ld-linux.so.2.
libm.so.6 loaded by ld-linux.so.2.
libc.so.6 loaded by ld-linux.so.2.
Breakpoint at 0x8048616, function main, file tmp.c, line 3
 #3:       int i = 0;

pgdbg> break 7 if (i > 25)
(2)breakpoint set at: main line: "tmp.c"@7 address: 0x804862a
2
pgdbg> cont
Breakpoint at 0x804862a, function main, file tmp.c, line 7
 #7:         a[i] = 0;

pgdbg> print i
26

As for your particular error, if it occurs at “-O0 -g”, then it might be a portability problem such as a header file variable which is guarded by a “GNU” or “_MSC_VER”, a GNU extension, or a bug in the program, such as an off-by-one error or other memory issue. Different compilers lay out memory differently so these types of errors can “work” in some environments but fail on others. Note that if it is a bug where memory is being overwritten, the actual bug may be in a completely unrelated part of your code.

Hope this helps,
Mat

Another hint which I’ve found very useful at times is to
modify the source code to add a statement like:

if (error_happens) {
int xjunk = 0.0;
}

and then set a breakpoint on the “xjunk = 0.0” line. Conditional
breakpoints can sometimes make the debugger run very slowly,
making the compiler do the work for the condition tends to be
faster.

Catherine