LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   gdb cannot display argc/argv? (https://www.linuxquestions.org/questions/programming-9/gdb-cannot-display-argc-argv-518283/)

Nick_Battle 01-11-2007 03:04 AM

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

vladmihaisima 01-12-2007 04:48 AM

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 ?

Nick_Battle 01-12-2007 05:18 AM

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

Nick_Battle 01-12-2007 12:06 PM

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


All times are GMT -5. The time now is 10:16 AM.