GDB breakpoints do not hit template functions

血红的双手。 提交于 2021-01-29 14:30:49

问题


I am trying set breakpoints in template functions in C++ via GDB I tried three possible approaches.

  1. break fileName:functionName => works for non-templated functions (specific to one function)
  2. rbreak fileName:. => breakpoints at all functions in a given file, but it does not seem to work for template functions
  3. break fileName:lineNumber => works for both non-templated and templated functions but problem here for me is I have to modify this linenumber everytime.

A overall objective is am using a script like below to trace the full codeflow via GDB, but my code has lot of template functions too. Sample GDB script below

set logging on
b func2
commands
silent
bt 1
continue
end
b func1
commands
silent
bt 1
set logging off
continue
end
  • One option was to use rbreak filename:.
  • run the code once, and
  • run the code again without exiting GDB. This time it recognizes the functions and breakpoints works.

Could you please help suggest a proper solution or let me know if I am missing something ? Any help/suggestion is highly appreciated and simplify my debug a lot.

Thanks a lot in advance !!


回答1:


The problem is most likely the function name you are using when trying to set a breakpoint in gdb.

The function names that gdb use are not what's in the source file, but what's in the binary. Template functions are not actually functions. They are just "recipe" and when you actually compile the code using the template the compiler will implement a function for you using the recipe (one for each combination of template parameters you have actually used).

Consider the code below

#include <iostream>

double tripleInput(double x) { return 3 * x; }

template <typename T>
inline T doubleInput(const T& x) {
    return 2 * x;
}


int main(int argc, char *argv[])
{
    std::cout << doubleInput(13) << std::endl;
    std::cout << doubleInput(1.72) << std::endl;

    std::cout << tripleInput(1.72) << std::endl;
    return 0;
}

When we compile this and debug in gdb there are three functions that gdb will see (besides main): tripleInput, doubleInput<int> and doubleInput<double>. If you write in gdb break tripleInput a breakpoint will be added in the tripleInput function, but if you write just break doubleInput gdb will say the function is not defined.

You need to write either break doubleInput<int> or break doubleInput<double>, but notice that adding a breakpoint like this will only stop in that specific instance of the template. It is different from adding a breakpoint to a line in the template. In that case gdb actually adds a breakpoint with multiple locations. Try info breakpoints after creating a breakpoint with both methods to see the different.

I don't know it is possible to do something such as break doubleInput<*>, but from a look in the documentation it does not seem to be possible.


TIP: In a gdb section TAB will complete the function names, including template instances.


Edit

I completely forgot about rbreak. It can set breakpoints on all functions matching a regular expression. This means that we can easily add a breakpoint in all function template instances with

rbreak doubleInput*

or even in all functions in a file with

rbreak main.cpp:.*


来源:https://stackoverflow.com/questions/61600798/gdb-breakpoints-do-not-hit-template-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!