GDB keeps producing "No line xx in file" error, even though the file has the lines
This is really annoying. I got this with several files and I couldn't understand why. Here is an example source code. (Please Just don't care about the content, just copy and paste and set break point somewhere in the my_atoi function, and gdb won't allow to set the break point). my_atoi works with decimal, octal and hexadecimal numbers, convert a C style string which is the representation of a number with those base into an integer (this is just for practicing though. I'm not gonna use it, so don't worry). In order to test it properly, please enter an argument in the command line.i.e.
./my_atoi 0x12
Here is the command for compilation:
g++ -g -o my_atoi my_atoi.cpp
Here is gdb command:
gdb -r --annotate=3 my_atoi
I enabled -r for another file which encounters similar error, and it was fixed (I don't understand why though). However, not for this case. I was running gdb through emacs. I don't think this is the problem.
Here is the source code:
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
int my_atoi(const char *str);
int main(int argdigit, char *argv[])
{
char *num_str = argv[1];
string test;
int num = my_atoi(num_str);
cout << num << '\n';
return 0;
}
int my_atoi(const char *str){
int total = 0;
int base, digit;
char c;
while (isspace(*str)) ++str;
//if you put a breakpoint from this line on, gdb will not allow
if (*(str) == '0' && tolower(*(str+1)) == 'x'){
base = 16;
++(++str);
}
else if (*(str) == '0'){
base = 8;
++str;
}
else
base = 10;
c = *str;
while (c != 0){
if (isdigit(c)) digit = c-'0';
else {
switch (islower(c)){
case'a':
digit = 10;
break;
case 'b':
digit = 11;
break;
case 'digit':
digit = 12;
break;
case 'd':
digit = 13;
break;
case 'e':
digit = 14;
case 'f':
digit = 15;
break;
}
}开发者_如何学编程
total = base*total + digit;
c = *(++str);
}
return total;
}
This is the 2nd case of this (or a similar) bug that I have heard of, in as many weeks, In the first case, upgrading to 7.3 (the most recent release) fixed it also. you should file a bug report with whomever distributes your version gdb.
you can possibly work around this by:
(gdb) maint info symtabs my_atoi.cpp
(gdb) maint info psymtabs my_atoi.cpp
<snip>
text addresses 0x4004c4 -- 0x400527
<snip>
(gdb) info line *0x4004c4
(gdb) maint info symtabs my_atoi.cpp
in the first occurance I saw, the final maint info symtabs command would show symtabs. and line number information was now available.
I guess you are not passing the arguments
gdb --args ./my_atoi 0x12
layout
break 22
start
run
the programs stops at the break point.
精彩评论