execute a function while in a breakpoint

For easier debugging, I would like to execute an own (predefined) function while beeing in a breakpoint (e.g. print some variables to a file).
Does there exists such a feature?

e.g.
- run program to a breakpoint
- a) press a keyboard-function-key (which calls that predefined function)
- b) or hover with cursor over a variable (and modify somehow the routine which
shows its content on the screen)?


What other things you want to do on this function/code branch you want to execute while the program is being debugged? In most cases, when you hit a break point, your debugger should be able to view memory contents (whether you are using an GUI IDE or a command line debugger). In fact, you should also be able to alter the memory data while debugging provided the bit mask 'write protect' is not enabled.

With GDB, it's as simple as using the 'print' and 'set' command. For example:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <vector>
int main(void)
{
    std::vector<int> myList = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    for (auto i : myList) {
        std::cout << i << "\n";
    }
    return (0);
}
// end debug.cc 


# compile
$ gcc -std=c++11 -O0 -g -o debug debug.cc -lstdc++
# debug with gdb
$ gdb debug
# set breakpoint
(gdb) break 6
Breakpoint 1 at 0x401401: file debug.cc, line 6.
(gdb) run
Breakpoint 1, main () at debug.cc:6
6           for (auto i : myList) {
(gdb) print myList
$1 = {<std::_Vector_base<int, std::allocator<int> >> = {
    _M_impl = {<std::allocator<int>> = {<__gnu_cxx::new_allocator<int>> = {<No d
ata fields>}, <No data fields>}, _M_start = 0x3c0d58, _M_finish = 0x3c0d80,
      _M_end_of_storage = 0x3c0d80}}, <No data fields>}
# print the content of the vector
(gdb) print myList._M_impl._M_start[2]
$2 = 2
# modify it
(gdb) set var myList._M_impl._M_start[2]=0
(gdb) continue
Continuing.
0
1
0
3
4
5
6
7
8
9
[Inferior 1 (process 4448) exited normally]
(gdb)


I am pretty sure GUI IDEs (which seems to be what you are using) will have the same set of features for debugging code.

If you wish to branch/call a function only when debugging, you can modify your code to use debugging macros that are defined when building debug mode. For example:
1
2
3
#ifdef _DEBUG
std::fprintf(stdout, "This line only prints when _DEBUG is defined.\n");
#endif // _DEBUG 


Just make sure to compile with your code appropriately with the pre-processor macro definitions you want to use.
Thanks for that detailed answer. I did not know 'gdb'. It seems that I need to look into its documentation.

What other things you want to do on this function/code branch you want to execute while the program is being debugged?

At a breakpoint, I'm not directly interested in the existing variables, but in a result of these variables.
Assuming that I have 2 variables a and b. At the breakpoint, I only want to know the addition of both variables (a+b). If I can see only the 2 variables, then I need to make the addition myself when waiting at a breakpoint (in fact, the calculation is so complex that it takes approx 100ms for calculation. I cannot do it myself at every breakpoint position).


If you wish to branch/call a function only when debugging, you can modify your code to use debugging macros that are defined when building debug mode.

The problem is, that I need to step through my code. After each step, the calculation (needing 100ms) needs to be re-executed.
That means, that I would need to insert these "#ifdef DEBUG" after every line of code, until I could identify the critical line of code.

In addition, my application is running on a machine (~real time). Introducing several of these "#ifdef DEBUG" would slow down the program so much (if no breaks were active), that the performance would be different.


Topic archived. No new replies allowed.