2013-08-08 3 views
1

Предположим, что в текущем каталоге есть/home/xxx/test, есть текстовый файл с именем «test.txt», который содержит одно слово «hello», а файл с именем «test.cpp» имеет следующий вид:Почему gdb переключился на домашний каталог?

#include <iostream> 
#include <fstream> 
#include <unistd.h> 
using namespace std; 

int main() 
{ 
     char cwd[1024]; 
     getcwd(cwd, 1024); 
     cout << cwd << endl; 

     string s; 
     ifstream i("test.txt"); 
     if (!i.good()) 
       cout << "Can't open test.txt" << endl; 

     i >> s; 
     i.close(); 

     cout << s << endl; 

     return 0; 
} 

test> g++ test.cpp 
test> ./a.out 
/home/xxx/test 
hello 
test> gdb a.out 
GNU gdb (GDB) Red Hat Enterprise Linux (7.2-60.el6_4.1) 
Copyright (C) 2010 Free Software Foundation, Inc. 
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> 
This is free software: you are free to change and redistribute it. 
There is NO WARRANTY, to the extent permitted by law. Type "show copying" 
and "show warranty" for details. 
This GDB was configured as "x86_64-redhat-linux-gnu". 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>... 
Reading symbols from /home/xxx/test/a.out...(no debugging symbols found)...done. 
(gdb) run 
Starting program: /home/xxx/test/a.out 
/home/xxx 
Can't open test.txt 


Program exited normally. 
(gdb) pwd 
Working directory /home/xxx/test. 
(gdb) shell pwd 
/home/xxx 

Мой вопрос: почему gdb переключился на домашний каталог, который дает «test.txt», не может быть найден? Почему «pwd» и «shell pwd» дают разные результаты?

Спасибо.

+0

gdb сам по себе не должен этого делать. Случается ли это с "gdb -nx"? Это отключает ваш .gdbinit. Кроме того, расхождение между gdb и представлением оболочки pwd нечетно. Интересно, почему это так. Вы должны показать результат «pwd» перед запуском gdb. –

+0

Привет, Том, спасибо за ваши намеки. –

ответ

1

В моем .cshrc есть «cd ~», это основная причина разницы между «shell pwd» и «pwd».

После этого устраните все проблемы.

Спасибо.