LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-23-2011, 11:45 AM   #1
napx
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Rep: Reputation: 0
Unhappy GDB doesn't provide the correct line in the source code


Hello,

I found a strange problem when I use gdb in eclipse to debug. When I step in the program, the register RIP(I'm using x86_64 machine and that's EIP in 32 bit mode) shows correctly but the line stepping in the source code may be far from the correct line. For example, when there is no loop in the source code, the RIP keeps on increasing and the current instruction pointer goes back to several lines back. I'm sure the source code is the right one because I when I checked the memory and local variables after several lines the values are all correct. The source code has many empty lines and does it matters with GDB?
 
Old 01-23-2011, 02:29 PM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Blank space shouldn't matter. Is it incorrectly-shown by gdb, or does Eclipse send you to the wrong line? Did you run a full backtrace in gdb?
Kevin Barry
 
Old 01-23-2011, 02:32 PM   #3
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
What was the optimization level?

It is very hard to say which flaws are in GCC vs. GDB vs. the debugging info formats that they must work with, but the symptoms described by the OP are fairly common and with increasing optimization levels those symptoms become far more common than "correct" behavior.

First, you cannot assume that the actual sequence of operations in the generated code is the same as in the source code. Whenever changing the sequence of operations would not change any defined behavior of the code, the compiler is free to change that sequence of operations. So the debugger might be showing you the correct source line, meaning the actual operations (or parts of them) occur in the sequence you see with the debugger rather than the sequence of the source code.

Second, actually more common in my experience, the association of ip value to source line understood by the debugger is often simply wrong. I don't know whether that is the fault of the compiler writing debug info or the debugger reading it. Such cases are very obscure situations when all optimizations are disabled, but become common at -O2 or -O3.

If you aren't a skilled asm programmer, you usually would have no way to distinguish operations happening out of source sequence vs. gdb simply wrong about the line number. I am a skilled asm programmer and I debug a lot of optimized code, so I have needed to understand that distinction quite often.

Quote:
Originally Posted by ta0kira View Post
Blank space shouldn't matter.
I agree. None of the problems I described above involve blank lines. That is a fact I normally use diagnostically as follows:

Frequently, the source line understood by gdb for an ip value is the wrong non blank line. That usually means one of the issues I described above.

Frequently, the source line understood by gdb for an ip value is not just the wrong line, but a blank line. So far as I understand, that universally indicates the source code is out of sync with the debug info (in Linux the debug info typically stays together with the binary, so that means this binary wasn't built from that source). Mistakes, optimizations, etc. in GCC or GDB that miss-associate ip values with source lines do not associate ip values with blank source lines.

So does gdb always point to a non blank line in the source? Or sometimes to a blank line?

Last edited by johnsfine; 01-23-2011 at 02:52 PM.
 
1 members found this post helpful.
Old 01-23-2011, 07:16 PM   #4
napx
LQ Newbie
 
Registered: Mar 2010
Posts: 2

Original Poster
Rep: Reputation: 0
Cool

Quote:
Originally Posted by johnsfine View Post
Second, actually more common in my experience, the association of ip value to source line understood by the debugger is often simply wrong. I don't know whether that is the fault of the compiler writing debug info or the debugger reading it. Such cases are very obscure situations when all optimizations are disabled, but become common at -O2 or -O3.
I turned the optimization level from -O2 to -O0 and GDB can track the source code line by line.
Quote:
Originally Posted by johnsfile View Post
If you aren't a skilled asm programmer, you usually would have no way to distinguish operations happening out of source sequence vs. gdb simply wrong about the line number. I am a skilled asm programmer and I debug a lot of optimized code, so I have needed to understand that distinction quite often.
I remembered when the optimization level is O2 sometime current line pointer will go even back to "{". This is really misleading.
Thank all of you.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to get the source code of command line ? the_cpp Programming 6 01-24-2009 09:01 AM
code line in gdb kshkid Programming 4 06-12-2008 02:06 AM
Help using gdb to provide a backtrace for kwin ? yossarianuk Linux - Software 0 04-03-2008 01:01 PM
can't find source files / line numbers with gdb dnijaguar Linux - Software 0 07-14-2003 01:30 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:47 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration