简体   繁体   中英

cannot not step into function while debugging

my simple test cpp is followed:

#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:

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:

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:

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. Now you can go out of std::string constructor using finish command and step into void hello() :

(gdb) finish
(gdb) step

Another option is to pass string argument to void hello() by reference to avoid unnecessary copying. That way you will step into desired function with only a single step:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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