Reputation: 2093
I wrote following commands in my gdb command file.
while ($i < 3)
s
end
I got the error: Invalid type combination in ordering comparison.
Then I tried, the following:
while (((int) $i) < ((int) 3))
s
end
But then I got the error: Invalid cast.
How to write a loop in gdb command file?
Note: i
is the variable in my C program being debugged that is referred as $i in the command file.
I couldn't find any examples at this site, which gives some reference material on gdb.
Upvotes: 2
Views: 2187
Reputation:
First, I think it is more appropriate to use
watch i >= 3
In order to break when i becomes more than 2.
As for looping until a local varible in C is less than 3. This is a gdb script for it:
while (i < 3)
s
end
This in C++ code to demonstrate a gdb loop:
#include <stdio.h>
int main()
{
for (int i=0; i< 10; ++i) {
printf ("i: %d\n", i);
}
return 0;
}
This is a gdb test:
D:\>gdb -q a
Reading symbols from D:\a.exe...done.
(gdb) start
Temporary breakpoint 1 at 0x401395: file main.cpp, line 4.
Starting program: D:\a.exe
[New Thread 3780.0x144]
Temporary breakpoint 1, main () at main.cpp:4
4 {
(gdb) n
5 for (int i=0; i< 10; ++i) {
(gdb)
6 printf ("i: %d\n", i);
(gdb) while (i<3)
>s
>end
i: 0
5 for (int i=0; i< 10; ++i) {
6 printf ("i: %d\n", i);
i: 1
5 for (int i=0; i< 10; ++i) {
6 printf ("i: %d\n", i);
i: 2
5 for (int i=0; i< 10; ++i) {
6 printf ("i: %d\n", i);
(gdb) p i
$1 = 3
(gdb)
Upvotes: 1