这很奇怪,我试图使用gdb调试我的一个程序。
以下是代码的相关部分
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 }
我有一个使用
编译文件的脚本g++ -g -O2 -std=gnu++0x -static -Wall -Wextra -std=c++11 -Isrc -rdynamic -fomit-frame-pointer -o addrev.out addrev.cpp
所以我做的是,我在第149行设置了一个断点。但是当我运行它时,它突破了158,这是我从未提及的。
这是gdb的输出:
-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*
chaudhary@localhost:~/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
是否存在某些特定原因导致它在函数结束时而不是断点处断开。
答案 0 :(得分:1)
GDB调试器通常会在最近的汇编语言指令处中断。
由于优化,有时候源代码列表与汇编语言列表不匹配。
尝试打印具有与源代码交错的汇编语言的列表。你会看到它在哪里破碎。
此外,设置断点的地方是调试器的策略。有些调试器喜欢在多行语句的第一行设置断点;其他人喜欢把它放在最后。
内联代码也将&#34;调整&#34;在断点触发时执行停止的位置以及可以设置断点的位置。
要获得源代码和调试器之间的最佳关联,请关闭所有优化。首先正确运行代码而不进行优化,然后根据需要启动优化。