gdb debugger accessing files that are not found

若如初见. 提交于 2020-01-23 01:02:09

问题


I am trying to do something rather simple, but don't understand why I am getting an error. I have a Makefile I will include so you can see what is going on. But when I step through the program I get libc-start.c: No such file or directory. Is there a way to handle this in the Makefile or avoid this?

Makefile

# makefile to build a program

# program depends on components: name and main 
myname:      main.o name.o 
    g++ -g -ggdb  main.o name.o -o myname

# name.cpp has it's own header file
name.o:        name.cpp name.h
    g++ -c -g -ggdb  name.cpp

# main.cpp also uses the header file name.h
main.o:        main.cpp name.h
    g++ -c -g -ggdb main.cpp

clean:
    /bin/rm -f myname *.o

main.cpp

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

int main () {
    name myName;

    myName.SetLast(LAST);
    myName.SetMiddle(MI);
    myName.SetFirst(FIRST);

    cout <<"My name is: ";
    myName.PrintFirst();
    myName.PrintMiddle();
    myName.PrintLast();

    return 0;
}

name.cpp

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

void name::GetFirst(string str) {
    str=first;
}

void name::SetFirst(string str) {
    first=str;
}

void name::GetMiddle(string str) {
    str=middle;
}

void name::SetMiddle(string str) {
    middle=str;
}

void name::GetLast(string str) {
    str=last;
}

void name::SetLast(string str) {
    last=str;
}

void name::PrintLast() {
    cout << last << "\n";
}
void name::PrintMiddle() {
    cout << middle;
}
void name::PrintFirst() {
    cout << first;
}

name.h

#define LAST    "Tank-Engine"
#define MI  "T. "
#define FIRST   "Thomas "

class name {

    private:
    string first;
    string middle;
    string last;

    public:
    void SetFirst(string str);
    void GetFirst(string str);

    void SetMiddle(string str);
    void GetMiddle(string str);

    void SetLast(string str);
    void GetLast(string str);

    void PrintLast();
    void PrintMiddle();
    void PrintFirst();

};

gdb error when stepping through

name::PrintLast (this=0x7fffffffe4a0) at name.cpp:31
31      cout << last << "\n";
(gdb) s
My name is: Thomas T. Tank-Engine
32  }
(gdb) s
main () at main.cpp:18
18          return 0;
(gdb) s
name::~name (this=0x7fffffffe4a0, __in_chrg=<optimized out>) at name.h:5
5   class name {
(gdb) s
main () at main.cpp:19
19  }
(gdb) s
__libc_start_main (main=0x400b86 <main()>, argc=1, argv=0x7fffffffe5b8, init=<optimized out>, 
fini=<optimized out>, rtld_fini=<optimized out>, stack_end=0x7fffffffe5a8) at libc-start.c:323
323 libc-start.c: No such file or directory.
(gdb) s
__GI_exit (status=0) at exit.c:104
104 exit.c: No such file or directory.
(gdb) s
103 in exit.c
(gdb) s
104 in exit.c
(gdb) s
__run_exit_handlers (status=0, listp=0x7ffff78ae698 <__exit_funcs>, 
run_list_atexit=run_list_atexit@entry=true) at exit.c:35
35  in exit.c
(gdb) 

回答1:


But when I step through the program I get libc-start.c: No such file or directory

There is no actual problem here (and GDB is most certainly not failing).

What's happening is that you step past the end of your program, and into libc (which is compiled with debug info, but you have not installed sources for it).

Many programmers believe that the execution of their program starts with main, and ends when main returns. But actually there are 1000s of instructions that run before and after main, as libc prepares your program for execution, and then cleans up after it. Unless you are a libc developer, usually you don't care about these before and after steps, and shouldn't try to step through that code.



来源:https://stackoverflow.com/questions/42014784/gdb-debugger-accessing-files-that-are-not-found

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