2014-11-15 1 views
1

글쎄, 이상하다. gdb를 사용하여 내 프로그램 중 하나를 디버깅하려고했다. 여기 GDB가 지정된 중단 점 대신 함수 끝에서 중단됩니다.

내가하지만, 내가 라인 149에 중단 점을 설정이었다 않았다 그래서

g++ -g -O2 -std=gnu++0x -static -Wall -Wextra -std=c++11 -Isrc -rdynamic -fomit-frame-pointer -o addrev.out addrev.cpp 

을 사용하여 파일을 컴파일 된 스크립트가

137 
    138 void solve() { 
    139  string input1, input2; 
    140  cin >> input1; 
    141  cin >> input2; 
    142  reverse(input1.begin(), input1.end()); 
    143  reverse(input2.begin(), input2.end()); 
    144  ll i1 = atoi(input1.c_str()); 
    145  ll i2 = atoi(input2.c_str()); 
    146  cout << i1<<" "<<i2; 
    147  ll out = i1+i2; 
    148  while(out%10 == 0) { 
    149   out = out%10; 
    150   cout << out; 
    151   cout <<"per: "<< out%10<<endl; 
    152  } 
    153  cout << "Sum: "<<out; 
    154  string output = to_string(out); 
    155  reverse(output.begin(), output.end()); 
    156  cout << endl; 
    157  cout << output << '\n'; 
    158 } 
    159 
    160 int main(int argc, char **argv) { 
    161  //string s = (argc == 1) ? argv[0] : "file.test"; 
    162  //freopen(s.substr(s.find("/") + 1,s.rfind(".")-2).append(".test").c_str(),"r",stdin); 
    163  ll t,j; 
    164  scanf("%lld",&t); 
    165  while(t--) { 
    166   solve(); 
    167  } 
    168  return 0; 
    169 } 

코드의 관련 부분입니다 내가 그것을 실행할 때, 나는 결코 언급하지 않은 158에서 깨진다.

-rw-r--r--. 1 chaudhary chaudhary 5.4K Nov 15 23:17 addrev.cpp 
-rwxrwxr-x. 1 chaudhary chaudhary 87K Nov 15 23:17 addrev.out* 
[email protected]:~/code/codpro/spoj$ gdb addrev.out 
GNU gdb (GDB) Fedora 7.7.1-21.fc20 
Copyright (C) 2014 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". 
Type "show configuration" for configuration details. 
For bug reporting instructions, please see: 
<http://www.gnu.org/software/gdb/bugs/>. 
Find the GDB manual and other documentation resources online at: 
<http://www.gnu.org/software/gdb/documentation/>. 
For help, type "help". 
Type "apropos word" to search for commands related to "word"... 
Reading symbols from addrev.out...done. 
(gdb) b 149 
Breakpoint 1 at 0x40186c: file addrev.cpp, line 149. 
(gdb) l 149 
144   ll i1 = atoi(input1.c_str()); 
145   ll i2 = atoi(input2.c_str()); 
146   cout << i1<<" "<<i2; 
147   ll out = i1+i2; 
148   while(out%10 == 0) { 
149    out = out%10; 
150    cout << out; 
151    cout <<"per: "<< out%10<<endl; 
152   } 
153   cout << "Sum: "<<out; 
(gdb) r < addrev.test 
Starting program: /home/chaudhary/code/codpro/spoj/addrev.out < addrev.test 
42 1Sum: 43 
34 

Breakpoint 1, solve() at addrev.cpp:158 
158  } 
(gdb) c 
Continuing. 
8534 457Sum: 8991 
1998 

Breakpoint 1, solve() at addrev.cpp:158 
158  } 
(gdb) c 
Continuing. 
503 4970per: 0 
0per: 0 
0per: 0 
0per: 0 
0per: 0 
0per: 0 
. 
. 
. 
Infinite loop 

대신 중단 점의 함수의 끝에서 휴식 왜 어떤 특별한 이유가 여기있다 :

은 GDB의 출력됩니다.

+4

최적화를 사용하여 디버깅해서는 안됩니다. '-O0' 또는'-Og' 사용 – Rapptz

+0

디버깅을 목표로 할 때 컴파일러 명령 줄에서 대부분의 최적화 플래그를 제거 할 것입니다.'-fomit-frame-pointer'도 삭제해야합니다. – didierc

답변

1

GDB 디버거는 일반적으로 가장 가까운 어셈블리 언어 명령에서 중단됩니다.

최적화로 인해 소스 코드 목록이 어셈블리 언어 목록과 일치하지 않는 경우가 있습니다.

어셈블리 언어가 소스 코드와 인터리브 된 목록을 인쇄 해보십시오. 어디에서 깨지는 지 알 수 있습니다.

또한 중단 점이 설정된 위치는 디버거의 정책입니다. 일부 디버거는 여러 줄 문장의 첫 줄에 중단 점을 설정하는 것을 좋아합니다. 다른 사람들은 그것을 마지막에 놓기를 좋아합니다.

인라인 코드는 중단 점이 트리거 될 때 실행이 중단되고 중단 점을 설정할 수있는 곳에서 "조정"됩니다.

소스 코드와 디버거 간의 최상의 상관 관계를 위해 모든 최적화를 해제하십시오. 최적화없이 코드를 올바르게 작동시킨 다음 필요에 따라 을 크랭크 업하십시오..