gdb - check changed variables' values on next step - c

is there a way to check the value of lvalue variables without using the print command when debugging the code step by step, what I'm looking to do is the following:
If I have the following code:
> x = 5;
y = 6;
when I'm debugging the code and I use next, I want gdb to display the value of x, that is the variable that changed in that instruction, I know I can watch the variable, but what I'm looking for is to be able to check variables on the fly without using print
is this possible?

You can use the display command:
(gdb) help display
Print value of expression EXP each time the program stops.
For instance if you display both you will get:
(gdb) next
4 y=6;
2: y = 0
1: x = 5
(gdb)
5 return 0;
2: y = 6
1: x = 5

Related

LLDB Floating point values displayed incorrectly unless printf is used

I am trying to debug a program using a coredump.
But I am having trouble getting LLDB to print flating point variables correctly.
Here is an example of a debugging session:
(lldb) run
...
(lldb) fr v
(float) sword_v_10 = 0.200000003 <- This is incorrect.
(float) sword_v_4 = 4.69999981 <- This is also incorrect.
(lldb) e printf("%f %f\n", sword_v_10, sword_v_4)
0.200000 4.700000 <- These are the correct value.
(int) $2 = 18
In the example above, I ran the program in the debugger so I was able to call printf.
However, when simply using coredumps I am unable to use printf.
So I was wondering if there was any way to display floating point variables correctly when I use the frame variable command.

Watching local variables in gdb without stopping execution

I am attempting to have GDB print the value of a variable when it changes.
Given an example program, I would like to get the value of x in func when it changes, but for the program to continue without prompt:
#include <stdio.h>
#include <stdlib.h>
int func(int x, int y);
int main(void) {
int x = 5;
int y;
y = func(x, 4);
printf("%d\n", x);
printf("%d\n", y);
return EXIT_SUCCESS;
}
int func(int x, int y) {
y *= 2;
x += y;
return x;
}
What I have attempted:
break func
commands
silent
watch x
commands
continue
end
continue
end
While this will successfully acquire the value of x when it changes, the problem is that when leaving the scope of x, gdb will stop to inform me that it is leaving the scope of x and that it is deleting the watchpoint. Is there any way to set GDB to go ahead and continue execution without a user prompt upon auto-removing a watchpoint?
I came across this question: gdb: do not break when watchpoint on local variable goes out of scope
However it never did receive a solution.
You can give gdb's watch command the -l option, and the watchpoint won't be deleted (nor execution stopped) when the variable goes out of scope.
But with this type of watchpoint, gdb will pick up changes that other functions make to that same address on the stack. So you can add the qualification if $_caller_is("func", 0) to the watchpoint so that gdb will only notify you if the variable changes within func.
(gdb) list func
18 int func(int x, int y) {
19 y *= 2;
20 x += y;
21 return x;
22 }
(gdb) b func
Breakpoint 1 at 0x400580: file s2.c, line 19.
(gdb) set $funcbp = $bpnum
(gdb) commands
Type commands for breakpoint(s) 1, one per line.
End with a line saying just "end".
># We can only set a watchpoint on a local var
># when it's visible, so we'll set it on entry to func.
># But we don't want to set it more than once
># if func is called more than once,
># so we disable the func breakpoint on first use.
>disable $funcbp
>watch -l x if $_caller_is("func", 0)
>commands
>continue
>end
>continue
>end
(gdb) r
Starting program: /home/mp/s2
Breakpoint 1, func (x=5, y=4) at s2.c:19
19 y *= 2;
Hardware watchpoint 2: -location x
Hardware watchpoint 2: -location x
Old value = 5
New value = 13
func (x=13, y=8) at s2.c:21
21 return x;
5
13
[Inferior 1 (process 29495) exited normally]
Is there any way to set GDB to go ahead and continue execution without a user prompt upon auto-removing a watchpoint?
No.
However you can add a breakpoint on return, and attach commands to that breakpoint to remove the watchpoint and continue. Then there will not be any active watchpoint for GDB to auto-remove, and so it will not stop when the function returns.

How to automatically analyse when a C pointer variable is assigned to NULL [duplicate]

I am running an application through gdb and I want to set a breakpoint for any time a specific variable is accessed / changed. Is there a good method for doing this? I would also be interested in other ways to monitor a variable in C/C++ to see if/when it changes.
watch only breaks on write, rwatch let you break on read, and awatch let you break on read/write.
You can set read watchpoints on memory locations:
gdb$ rwatch *0xfeedface
Hardware read watchpoint 2: *0xfeedface
but one limitation applies to the rwatch and awatch commands; you can't use gdb variables
in expressions:
gdb$ rwatch $ebx+0xec1a04f
Expression cannot be implemented with read/access watchpoint.
So you have to expand them yourself:
gdb$ print $ebx
$13 = 0x135700
gdb$ rwatch *0x135700+0xec1a04f
Hardware read watchpoint 3: *0x135700 + 0xec1a04f
gdb$ c
Hardware read watchpoint 3: *0x135700 + 0xec1a04f
Value = 0xec34daf
0x9527d6e7 in objc_msgSend ()
Edit: Oh, and by the way. You need either hardware or software support. Software is obviously much slower. To find out if your OS supports hardware watchpoints you can see the can-use-hw-watchpoints environment setting.
gdb$ show can-use-hw-watchpoints
Debugger's willingness to use watchpoint hardware is 1.
What you're looking for is called a watchpoint.
Usage
(gdb) watch foo: watch the value of variable foo
(gdb) watch *(int*)0x12345678: watch the value pointed by an address, casted to whatever type you want
(gdb) watch a*b + c/d: watch an arbitrarily complex expression, valid in the program's native language
Watchpoints are of three kinds:
watch: gdb will break when a write occurs
rwatch: gdb will break wnen a read occurs
awatch: gdb will break in both cases
You may choose the more appropriate for your needs.
For more information, check this out.
Assuming the first answer is referring to the C-like syntax (char *)(0x135700 +0xec1a04f) then the answer to do rwatch *0x135700+0xec1a04f is incorrect. The correct syntax is rwatch *(0x135700+0xec1a04f).
The lack of ()s there caused me a great deal of pain trying to use watchpoints myself.
I just tried the following:
$ cat gdbtest.c
int abc = 43;
int main()
{
abc = 10;
}
$ gcc -g -o gdbtest gdbtest.c
$ gdb gdbtest
...
(gdb) watch abc
Hardware watchpoint 1: abc
(gdb) r
Starting program: /home/mweerden/gdbtest
...
Old value = 43
New value = 10
main () at gdbtest.c:6
6 }
(gdb) quit
So it seems possible, but you do appear to need some hardware support.
Use watch to see when a variable is written to, rwatch when it is read and awatch when it is read/written from/to, as noted above. However, please note that to use this command, you must break the program, and the variable must be in scope when you've broken the program:
Use the watch command. The argument to the watch command is an
expression that is evaluated. This implies that the variabel you want
to set a watchpoint on must be in the current scope. So, to set a
watchpoint on a non-global variable, you must have set a breakpoint
that will stop your program when the variable is in scope. You set the
watchpoint after the program breaks.
In addition to what has already been answered/commented by asksol and Paolo M
I didn't at first read understand, why do we need to cast the results. Though I read this: https://sourceware.org/gdb/onlinedocs/gdb/Set-Watchpoints.html, yet it wasn't intuitive to me..
So I did an experiment to make the result clearer:
Code: (Let's say that int main() is at Line 3; int i=0 is at Line 5 and other code.. is from Line 10)
int main()
{
int i = 0;
int j;
i = 3840 // binary 1100 0000 0000 to take into account endianness
other code..
}
then i started gdb with the executable file
in my first attempt, i set the breakpoint on the location of variable without casting, following were the results displayed
Thread 1 "testing2" h
Breakpoint 2 at 0x10040109b: file testing2.c, line 10.
(gdb) s
7 i = 3840;
(gdb) p i
$1 = 0
(gdb) p &i
$2 = (int *) 0xffffcbfc
(gdb) watch *0xffffcbfc
Hardware watchpoint 3: *0xffffcbfc
(gdb) s
[New Thread 13168.0xa74]
Thread 1 "testing2" hit Breakpoint 2, main () at testing2.c:10
10 b = a;
(gdb) p i
$3 = 3840
(gdb) p *0xffffcbfc
$4 = 3840
(gdb) p/t *0xffffcbfc
$5 = 111100000000
as we could see breakpoint was hit for line 10 which was set by me. gdb didn't break because although variable i underwent change yet the location being watched didn't change (due to endianness, since it continued to remain all 0's)
in my second attempt, i did the casting on the address of the variable to watch for all the sizeof(int) bytes. this time:
(gdb) p &i
$6 = (int *) 0xffffcbfc
(gdb) p i
$7 = 0
(gdb) watch *(int *) 0xffffcbfc
Hardware watchpoint 6: *(int *) 0xffffcbfc
(gdb) b 10
Breakpoint 7 at 0x10040109b: file testing2.c, line 10.
(gdb) i b
Num Type Disp Enb Address What
6 hw watchpoint keep y *(int *) 0xffffcbfc
7 breakpoint keep y 0x000000010040109b in main at testing2.c:10
(gdb) n
[New Thread 21508.0x3c30]
Thread 1 "testing2" hit Hardware watchpoint 6: *(int *) 0xffffcbfc
Old value = 0
New value = 3840
Thread 1 "testing2" hit Breakpoint 7, main () at testing2.c:10
10 b = a;
gdb break since it detected the value has changed.

GDB: break if variable equal value

I like to make GDB set a break point when a variable equal some value I set, I tried this example:
#include <stdio.h>
main()
{
int i = 0;
for(i=0;i<7;++i)
printf("%d\n", i);
return 0;
}
Output from GDB:
(gdb) break if ((int)i == 5)
No default breakpoint address now.
(gdb) run
Starting program: /home/SIFE/run
0
1
2
3
4
5
6
Program exited normally.
(gdb)
Like you see, GDB didn't make any break point, is this possible with GDB?
in addition to a watchpoint nested inside a breakpoint
you can also set a single breakpoint on the 'filename:line_number' and use a condition.
I find it sometimes easier.
(gdb) break iter.c:6 if i == 5
Breakpoint 2 at 0x4004dc: file iter.c, line 6.
(gdb) c
Continuing.
0
1
2
3
4
Breakpoint 2, main () at iter.c:6
6 printf("%d\n", i);
If like me you get tired of line numbers changing, you can add a label
then set the breakpoint on the label like so:
#include <stdio.h>
main()
{
int i = 0;
for(i=0;i<7;++i) {
looping:
printf("%d\n", i);
}
return 0;
}
(gdb) break main:looping if i == 5
You can use a watchpoint for this (A breakpoint on data instead of code).
You can start by using watch i.
Then set a condition for it using condition <breakpoint num> i == 5
You can get the breakpoint number by using info watch
First, you need to compile your code with appropriate flags, enabling debug into code.
$ gcc -Wall -g -ggdb -o ex1 ex1.c
then just run you code with your favourite debugger
$ gdb ./ex1
show me the code.
(gdb) list
1 #include <stdio.h>
2 int main(void)
3 {
4 int i = 0;
5 for(i=0;i<7;++i)
6 printf("%d\n", i);
7
8 return 0;
9 }
break on lines 5 and looks if i == 5.
(gdb) b 5
Breakpoint 1 at 0x4004fb: file ex1.c, line 5.
(gdb) rwatch i if i==5
Hardware read watchpoint 5: i
checking breakpoints
(gdb) info b
Num Type Disp Enb Address What
1 breakpoint keep y 0x00000000004004fb in main at ex1.c:5
breakpoint already hit 1 time
5 read watchpoint keep y i
stop only if i==5
running the program
(gdb) c
Continuing.
0
1
2
3
4
Hardware read watchpoint 5: i
Value = 5
0x0000000000400523 in main () at ex1.c:5
5 for(i=0;i<7;++i)
There are hardware and software watchpoints. They are for reading and for writing a variable. You need to consult a tutorial:
http://www.unknownroad.com/rtfm/gdbtut/gdbwatch.html
To set a watchpoint, first you need to break the code into a place where the varianle i is present in the environment, and set the watchpoint.
watch command is used to set a watchpoit for writing, while rwatch for reading, and awatch for reading/writing.

How to go at particular value of counter in for loop using gdb?

I have a for loop of about 100 odd values. I want to have a breakpoint where I can set some value for the iterator variable and then directly go on that program execution state.
For example
for(int i=0;i<500;i++)
{
doSomething();
}
Here I want to have a breakpoint on i=100; and step through all values from 0 to 99 in one go. Is this possible in gdb and how do I do it?
In gdb you can set conditions on breakpoints.
break line if i == 100
Where "line" is the appropriate line number.
There is probably a better way, but I have gotten a lot of mileage out of doing things like this:
if (i == 100) {
int breakpoint = 10;
}
A conditional breakpoint is one that only transfers control to gdb only when a certain condition is true.
This can be useful when you only want gdb control after say 10 iterations of a loop.
To set a condition on a breakpoint, use the condition command with the number of the breakpoint followed by the condition on which to trigger the breakpoint.
Here is an example where setting a conditional breakpoint that will only be triggered when the "condition (i >= 10) is true":
(gdb) break 28 # set breakpoint at line 28
(gdb) info break # list breakpoint information
Num Type Disp Enb Address What
1 breakpoint keep y 0x080588a3 in loopit at loops.c:28
(gdb) condition 1 (i >= 10) # set condition on breakpoint 1
(gdb) run (or write `continue` if already running)

Resources