简体   繁体   English

调试时无法进入功能

[英]cannot not step into function while debugging

my simple test cpp is followed: 我的简单测试cpp遵循:

#include <stdio.h>
#include <string>
#include <iostream>
using namespace std;

void hello(string str) {
    cout << str << endl;
}

int main(int argc, const char **argv) {
    string str = "hello world!";
    hello(str);
    return 0;
}

and I compile the cpp with command: 我用命令编译cpp:

g++ hello.cpp -o hello -g

and then run as debug mode: 然后以调试模式运行:

cgdb hello
(gdb) b main
(gdb) r
(gdb) n
(gdb) s

after I use step command in gdb, I got the following errors: 在gdb中使用step命令后,出现以下错误:

std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string (this=0x7fffffffe5c0, __str="hello world!") at /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h:399
399     /build/gcc/src/gcc-build/x86_64-pc-linux-gnu/libstdc++-v3/include/bits/basic_string.h: No such file or directory.

I found that this error only happen when the function has arguments with type of string. 我发现仅当函数具有字符串类型的参数时才会发生此错误。 For example: 例如:

void hello(int i);

I can step into the function hello without any problem. 我可以顺利进入功能问候。

I use the following command to find where allocator.h is: 我使用以下命令查找allocator.h在哪里:

sudo find / | grep allocator.h

and I got the results as follow(only list part of the results): 我得到的结果如下(仅列出部分结果):

/usr/include/c++/6.3.1/ext/bitmap_allocator.h
/usr/include/c++/6.3.1/ext/debug_allocator.h
/usr/include/c++/6.3.1/ext/new_allocator.h
/usr/include/c++/6.3.1/ext/extptr_allocator.h
/usr/include/c++/6.3.1/ext/throw_allocator.h
/usr/include/c++/6.3.1/ext/pool_allocator.h
/usr/include/c++/6.3.1/ext/array_allocator.h
/usr/include/c++/6.3.1/ext/malloc_allocator.h
/usr/include/c++/6.3.1/x86_64-pc-linux-gnu/bits/c++allocator.h
/usr/include/c++/6.3.1/bits/allocator.h
/usr/include/c++/6.3.1/bits/uses_allocator.h
/usr/include/gc/gc_allocator.h

Why would this happen? 为什么会这样? THX!!! 谢谢!!!

Why would this happen? 为什么会这样?

You wanted to step into void hello() but stepped into std::string copy constructor. 您想进入void hello()但进入std::string复制构造函数。 Now you can go out of std::string constructor using finish command and step into void hello() : 现在,您可以使用finish命令退出std::string构造函数,并进入void hello()

(gdb) finish
(gdb) step

Another option is to pass string argument to void hello() by reference to avoid unnecessary copying. 另一种选择是通过引用将字符串参数传递给void hello()以避免不必要的复制。 That way you will step into desired function with only a single step: 这样,您只需一步即可进入所需的功能:

void hello(const string& str) {
    cout << str << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM