Reputation: 12371
I'm trying to use GDB to debug my C++ program.
I'm thinking if it's possible to pass arguments to a function while using GDB.
For example, I have such a program as below:
#include <iostream>
void func(int a)
{
std::cout << a << std::endl;
}
int main(int argc, char **argv)
{
func(2222);
return EXIT_SUCCESS;
}
I set a breakpoint at the line func(2222)
in the function main
. My question is: is it possible to set another argument to the function func
, instead of 2222
, while using GDB on this program?
Upvotes: 1
Views: 4703
Reputation: 138
I don't think it's possible to change the value passed to the function before the call of func(2222)
. However, you are able to modify the values of the parameter after GDB has stepped into the function, before another code execution.
My favorite way to debug with GDB is within Visual Studio Code, it provides a GUI on top of all the GDB commands, and makes using advanced GDB debugging much easier. And you can set a new value for the variable simply by clicking on it. You can see an example below.
Upvotes: 0
Reputation: 588
Yes. You can evaluate expressions while debugging. You can use either call <expr>
or print <expr>
, but the expression must be known at the time of breakpoint.
In your example, you could do:
gdb ./test
(gdb) b main
...
(gdb) r
...
(gdb) call func(11)
11
More info about call/print: https://sourceware.org/gdb/onlinedocs/gdb/Calling.html
Upvotes: 2
Reputation: 3001
You can change the value a inside the function func())
.
For that you can use:
assign a = $value_you_want
Example
b func # set breakpoint
c # continue
assign a = 2 # breakpoint gets hit, change value from a = 222 to a = 2
Upvotes: 3