LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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-11-2007, 03:04 AM   #1
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Rep: Reputation: 33
gdb cannot display argc/argv?


If I compile a simple "int main(int argc, char *argv[])" program with -g, and debug with gdb, I'm getting an error when I break at the start and try to print argc or argv.

Cannot access memory at address 0x0

This is with SUSE 10.1, which is gcc 4.1.0. I happen to have some binaries of the *same* source that were compiled with gcc from 10.0 (which is gcc 4.0.2 I think) and these debug correctly.

The programs themselves work fine. It's just the debugger that has a problem.

Is this a known problem?

To reproduce, write the classic "hello world" main, compile with "gcc -o main -g main.c" or "g++ -o main -g main.cpp" (C and C++ give the same problem). Then "gdb main", "break main" and run to the breakpoint. "p argc" or "p argv" gives the error. If you (can) repeat this under SUSE 10.0 it may not show the problem.

Cheers,
-nick
 
Old 01-12-2007, 04:48 AM   #2
vladmihaisima
Member
 
Registered: Oct 2002
Location: Delft, Netherlands
Distribution: Gentoo
Posts: 196

Rep: Reputation: 33
On gcc 4.1 and gdb 6.5.

For me if the program doesn't uses argc/argv the values are optimized out (even if specifying O0). Is this your problem also ?
 
Old 01-12-2007, 05:18 AM   #3
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Original Poster
Rep: Reputation: 33
Quote:
Originally Posted by vladmihaisima
For me if the program doesn't uses argc/argv the values are optimized out (even if specifying O0). Is this your problem also ?
Ah! I didn't think of that. It would mean it's something that's changed recently (my 10.0 builds are OK), but it would explain it very neatly. I'll try later tonight and let you know.

Are all function arguments that aren't used removed from the debug symbol list, or is it just main's?

I only noticed it because the Eclipse C++ debug environment gives very nasty looking errors when it can't read the variables.

Cheers,
-nick
 
Old 01-12-2007, 12:06 PM   #4
Nick_Battle
Member
 
Registered: Dec 2006
Location: Bracknell, UK
Distribution: SUSE 13.1
Posts: 159

Original Poster
Rep: Reputation: 33
This does appear to be the explanation.

If you use argc/argv, then their values are reports as normal. Curiously, you have to actually step into main before they can be read - ie. when the program stops at the "break main" you can't see them, but after a "next" you can. It only applies to main; other functions show their parameters, even if they're not used in the body.

This almost fixes the problem with Eclipse. It still reports errors when you break at main, but it will let you view the pair once you've stepped into main. It also cures a problem where breakpoints in main weren't effective - my guess is the errors trying to read the locals confused it so much it couldn't successfully set the breakpoint either.

I'm told this doesn't happen with the 64-bit compiler in SUSE 10.1, incidentally.

Thanks for the help!

Cheers,
-nick
 
  


Reply



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
unable to display the souce lien in gdb gauri Debian 3 01-16-2006 08:42 AM
normal gdb and spec gdb for kgdb Igor007 Programming 1 09-23-2005 04:15 PM
how to pass argv[] & argc to functions? skie_knite007 Programming 2 05-13-2005 11:12 PM
main(int argc, char **argv) Longinus Programming 4 06-12-2004 07:22 AM
argc argv linuxanswer Programming 8 10-25-2003 07:54 PM

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

All times are GMT -5. The time now is 06:30 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